1/******************************************************************************
2** This file is an amalgamation of many separate C source files from SQLite
3** version 3.25.2. 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#define SQLITE_OMIT_LOAD_EXTENSION 1
23#ifndef SQLITE_PRIVATE
24# define SQLITE_PRIVATE static
25#endif
26/************** Begin file ctime.c *******************************************/
27/*
28** 2010 February 23
29**
30** The author disclaims copyright to this source code. In place of
31** a legal notice, here is a blessing:
32**
33** May you do good and not evil.
34** May you find forgiveness for yourself and forgive others.
35** May you share freely, never taking more than you give.
36**
37*************************************************************************
38**
39** This file implements routines used to report what compile-time options
40** SQLite was built with.
41*/
42
43#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
44
45/*
46** Include the configuration header output by 'configure' if we're using the
47** autoconf-based build
48*/
49#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
50#include "config.h"
51#define SQLITECONFIG_H 1
52#endif
53
54/* These macros are provided to "stringify" the value of the define
55** for those options in which the value is meaningful. */
56#define CTIMEOPT_VAL_(opt) #opt
57#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
58
59/* Like CTIMEOPT_VAL, but especially for SQLITE_DEFAULT_LOOKASIDE. This
60** option requires a separate macro because legal values contain a single
61** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
62#define CTIMEOPT_VAL2_(opt1,opt2) #opt1 "," #opt2
63#define CTIMEOPT_VAL2(opt) CTIMEOPT_VAL2_(opt)
64
65/*
66** An array of names of all compile-time options. This array should
67** be sorted A-Z.
68**
69** This array looks large, but in a typical installation actually uses
70** only a handful of compile-time options, so most times this array is usually
71** rather short and uses little memory space.
72*/
73static const char * const sqlite3azCompileOpt[] = {
74
75/*
76** BEGIN CODE GENERATED BY tool/mkctime.tcl
77*/
78#if SQLITE_32BIT_ROWID
79 "32BIT_ROWID",
80#endif
81#if SQLITE_4_BYTE_ALIGNED_MALLOC
82 "4_BYTE_ALIGNED_MALLOC",
83#endif
84#if SQLITE_64BIT_STATS
85 "64BIT_STATS",
86#endif
87#if SQLITE_ALLOW_COVERING_INDEX_SCAN
88 "ALLOW_COVERING_INDEX_SCAN",
89#endif
90#if SQLITE_ALLOW_URI_AUTHORITY
91 "ALLOW_URI_AUTHORITY",
92#endif
93#ifdef SQLITE_BITMASK_TYPE
94 "BITMASK_TYPE=" CTIMEOPT_VAL(SQLITE_BITMASK_TYPE),
95#endif
96#if SQLITE_BUG_COMPATIBLE_20160819
97 "BUG_COMPATIBLE_20160819",
98#endif
99#if SQLITE_CASE_SENSITIVE_LIKE
100 "CASE_SENSITIVE_LIKE",
101#endif
102#if SQLITE_CHECK_PAGES
103 "CHECK_PAGES",
104#endif
105#if defined(__clang__) && defined(__clang_major__)
106 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
107 CTIMEOPT_VAL(__clang_minor__) "."
108 CTIMEOPT_VAL(__clang_patchlevel__),
109#elif defined(_MSC_VER)
110 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
111#elif defined(__GNUC__) && defined(__VERSION__)
112 "COMPILER=gcc-" __VERSION__,
113#endif
114#if SQLITE_COVERAGE_TEST
115 "COVERAGE_TEST",
116#endif
117#if SQLITE_DEBUG
118 "DEBUG",
119#endif
120#if SQLITE_DEFAULT_AUTOMATIC_INDEX
121 "DEFAULT_AUTOMATIC_INDEX",
122#endif
123#if SQLITE_DEFAULT_AUTOVACUUM
124 "DEFAULT_AUTOVACUUM",
125#endif
126#ifdef SQLITE_DEFAULT_CACHE_SIZE
127 "DEFAULT_CACHE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_CACHE_SIZE),
128#endif
129#if SQLITE_DEFAULT_CKPTFULLFSYNC
130 "DEFAULT_CKPTFULLFSYNC",
131#endif
132#ifdef SQLITE_DEFAULT_FILE_FORMAT
133 "DEFAULT_FILE_FORMAT=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_FORMAT),
134#endif
135#ifdef SQLITE_DEFAULT_FILE_PERMISSIONS
136 "DEFAULT_FILE_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_PERMISSIONS),
137#endif
138#if SQLITE_DEFAULT_FOREIGN_KEYS
139 "DEFAULT_FOREIGN_KEYS",
140#endif
141#ifdef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
142 "DEFAULT_JOURNAL_SIZE_LIMIT=" CTIMEOPT_VAL(SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT),
143#endif
144#ifdef SQLITE_DEFAULT_LOCKING_MODE
145 "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
146#endif
147#ifdef SQLITE_DEFAULT_LOOKASIDE
148 "DEFAULT_LOOKASIDE=" CTIMEOPT_VAL2(SQLITE_DEFAULT_LOOKASIDE),
149#endif
150#if SQLITE_DEFAULT_MEMSTATUS
151 "DEFAULT_MEMSTATUS",
152#endif
153#ifdef SQLITE_DEFAULT_MMAP_SIZE
154 "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
155#endif
156#ifdef SQLITE_DEFAULT_PAGE_SIZE
157 "DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_PAGE_SIZE),
158#endif
159#ifdef SQLITE_DEFAULT_PCACHE_INITSZ
160 "DEFAULT_PCACHE_INITSZ=" CTIMEOPT_VAL(SQLITE_DEFAULT_PCACHE_INITSZ),
161#endif
162#ifdef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
163 "DEFAULT_PROXYDIR_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_PROXYDIR_PERMISSIONS),
164#endif
165#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
166 "DEFAULT_RECURSIVE_TRIGGERS",
167#endif
168#ifdef SQLITE_DEFAULT_ROWEST
169 "DEFAULT_ROWEST=" CTIMEOPT_VAL(SQLITE_DEFAULT_ROWEST),
170#endif
171#ifdef SQLITE_DEFAULT_SECTOR_SIZE
172 "DEFAULT_SECTOR_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_SECTOR_SIZE),
173#endif
174#ifdef SQLITE_DEFAULT_SYNCHRONOUS
175 "DEFAULT_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_SYNCHRONOUS),
176#endif
177#ifdef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
178 "DEFAULT_WAL_AUTOCHECKPOINT=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_AUTOCHECKPOINT),
179#endif
180#ifdef SQLITE_DEFAULT_WAL_SYNCHRONOUS
181 "DEFAULT_WAL_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_SYNCHRONOUS),
182#endif
183#ifdef SQLITE_DEFAULT_WORKER_THREADS
184 "DEFAULT_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WORKER_THREADS),
185#endif
186#if SQLITE_DIRECT_OVERFLOW_READ
187 "DIRECT_OVERFLOW_READ",
188#endif
189#if SQLITE_DISABLE_DIRSYNC
190 "DISABLE_DIRSYNC",
191#endif
192#if SQLITE_DISABLE_FTS3_UNICODE
193 "DISABLE_FTS3_UNICODE",
194#endif
195#if SQLITE_DISABLE_FTS4_DEFERRED
196 "DISABLE_FTS4_DEFERRED",
197#endif
198#if SQLITE_DISABLE_INTRINSIC
199 "DISABLE_INTRINSIC",
200#endif
201#if SQLITE_DISABLE_LFS
202 "DISABLE_LFS",
203#endif
204#if SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
205 "DISABLE_PAGECACHE_OVERFLOW_STATS",
206#endif
207#if SQLITE_DISABLE_SKIPAHEAD_DISTINCT
208 "DISABLE_SKIPAHEAD_DISTINCT",
209#endif
210#ifdef SQLITE_ENABLE_8_3_NAMES
211 "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES),
212#endif
213#if SQLITE_ENABLE_API_ARMOR
214 "ENABLE_API_ARMOR",
215#endif
216#if SQLITE_ENABLE_ATOMIC_WRITE
217 "ENABLE_ATOMIC_WRITE",
218#endif
219#if SQLITE_ENABLE_BATCH_ATOMIC_WRITE
220 "ENABLE_BATCH_ATOMIC_WRITE",
221#endif
222#if SQLITE_ENABLE_CEROD
223 "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD),
224#endif
225#if SQLITE_ENABLE_COLUMN_METADATA
226 "ENABLE_COLUMN_METADATA",
227#endif
228#if SQLITE_ENABLE_COLUMN_USED_MASK
229 "ENABLE_COLUMN_USED_MASK",
230#endif
231#if SQLITE_ENABLE_COSTMULT
232 "ENABLE_COSTMULT",
233#endif
234#if SQLITE_ENABLE_CURSOR_HINTS
235 "ENABLE_CURSOR_HINTS",
236#endif
237#if SQLITE_ENABLE_DBSTAT_VTAB
238 "ENABLE_DBSTAT_VTAB",
239#endif
240#if SQLITE_ENABLE_EXPENSIVE_ASSERT
241 "ENABLE_EXPENSIVE_ASSERT",
242#endif
243#if SQLITE_ENABLE_FTS1
244 "ENABLE_FTS1",
245#endif
246#if SQLITE_ENABLE_FTS2
247 "ENABLE_FTS2",
248#endif
249#if SQLITE_ENABLE_FTS3
250 "ENABLE_FTS3",
251#endif
252#if SQLITE_ENABLE_FTS3_PARENTHESIS
253 "ENABLE_FTS3_PARENTHESIS",
254#endif
255#if SQLITE_ENABLE_FTS3_TOKENIZER
256 "ENABLE_FTS3_TOKENIZER",
257#endif
258#if SQLITE_ENABLE_FTS4
259 "ENABLE_FTS4",
260#endif
261#if SQLITE_ENABLE_FTS5
262 "ENABLE_FTS5",
263#endif
264#if SQLITE_ENABLE_HIDDEN_COLUMNS
265 "ENABLE_HIDDEN_COLUMNS",
266#endif
267#if SQLITE_ENABLE_ICU
268 "ENABLE_ICU",
269#endif
270#if SQLITE_ENABLE_IOTRACE
271 "ENABLE_IOTRACE",
272#endif
273#if SQLITE_ENABLE_JSON1
274 "ENABLE_JSON1",
275#endif
276#if SQLITE_ENABLE_LOAD_EXTENSION
277 "ENABLE_LOAD_EXTENSION",
278#endif
279#ifdef SQLITE_ENABLE_LOCKING_STYLE
280 "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
281#endif
282#if SQLITE_ENABLE_MEMORY_MANAGEMENT
283 "ENABLE_MEMORY_MANAGEMENT",
284#endif
285#if SQLITE_ENABLE_MEMSYS3
286 "ENABLE_MEMSYS3",
287#endif
288#if SQLITE_ENABLE_MEMSYS5
289 "ENABLE_MEMSYS5",
290#endif
291#if SQLITE_ENABLE_MULTIPLEX
292 "ENABLE_MULTIPLEX",
293#endif
294#if SQLITE_ENABLE_NULL_TRIM
295 "ENABLE_NULL_TRIM",
296#endif
297#if SQLITE_ENABLE_OVERSIZE_CELL_CHECK
298 "ENABLE_OVERSIZE_CELL_CHECK",
299#endif
300#if SQLITE_ENABLE_PREUPDATE_HOOK
301 "ENABLE_PREUPDATE_HOOK",
302#endif
303#if SQLITE_ENABLE_QPSG
304 "ENABLE_QPSG",
305#endif
306#if SQLITE_ENABLE_RBU
307 "ENABLE_RBU",
308#endif
309#if SQLITE_ENABLE_RTREE
310 "ENABLE_RTREE",
311#endif
312#if SQLITE_ENABLE_SELECTTRACE
313 "ENABLE_SELECTTRACE",
314#endif
315#if SQLITE_ENABLE_SESSION
316 "ENABLE_SESSION",
317#endif
318#if SQLITE_ENABLE_SNAPSHOT
319 "ENABLE_SNAPSHOT",
320#endif
321#if SQLITE_ENABLE_SORTER_REFERENCES
322 "ENABLE_SORTER_REFERENCES",
323#endif
324#if SQLITE_ENABLE_SQLLOG
325 "ENABLE_SQLLOG",
326#endif
327#if defined(SQLITE_ENABLE_STAT4)
328 "ENABLE_STAT4",
329#elif defined(SQLITE_ENABLE_STAT3)
330 "ENABLE_STAT3",
331#endif
332#if SQLITE_ENABLE_STMTVTAB
333 "ENABLE_STMTVTAB",
334#endif
335#if SQLITE_ENABLE_STMT_SCANSTATUS
336 "ENABLE_STMT_SCANSTATUS",
337#endif
338#if SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
339 "ENABLE_UNKNOWN_SQL_FUNCTION",
340#endif
341#if SQLITE_ENABLE_UNLOCK_NOTIFY
342 "ENABLE_UNLOCK_NOTIFY",
343#endif
344#if SQLITE_ENABLE_UPDATE_DELETE_LIMIT
345 "ENABLE_UPDATE_DELETE_LIMIT",
346#endif
347#if SQLITE_ENABLE_URI_00_ERROR
348 "ENABLE_URI_00_ERROR",
349#endif
350#if SQLITE_ENABLE_VFSTRACE
351 "ENABLE_VFSTRACE",
352#endif
353#if SQLITE_ENABLE_WHERETRACE
354 "ENABLE_WHERETRACE",
355#endif
356#if SQLITE_ENABLE_ZIPVFS
357 "ENABLE_ZIPVFS",
358#endif
359#if SQLITE_EXPLAIN_ESTIMATED_ROWS
360 "EXPLAIN_ESTIMATED_ROWS",
361#endif
362#if SQLITE_EXTRA_IFNULLROW
363 "EXTRA_IFNULLROW",
364#endif
365#ifdef SQLITE_EXTRA_INIT
366 "EXTRA_INIT=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT),
367#endif
368#ifdef SQLITE_EXTRA_SHUTDOWN
369 "EXTRA_SHUTDOWN=" CTIMEOPT_VAL(SQLITE_EXTRA_SHUTDOWN),
370#endif
371#ifdef SQLITE_FTS3_MAX_EXPR_DEPTH
372 "FTS3_MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_FTS3_MAX_EXPR_DEPTH),
373#endif
374#if SQLITE_FTS5_ENABLE_TEST_MI
375 "FTS5_ENABLE_TEST_MI",
376#endif
377#if SQLITE_FTS5_NO_WITHOUT_ROWID
378 "FTS5_NO_WITHOUT_ROWID",
379#endif
380#if SQLITE_HAS_CODEC
381 "HAS_CODEC",
382#endif
383#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
384 "HAVE_ISNAN",
385#endif
386#if SQLITE_HOMEGROWN_RECURSIVE_MUTEX
387 "HOMEGROWN_RECURSIVE_MUTEX",
388#endif
389#if SQLITE_IGNORE_AFP_LOCK_ERRORS
390 "IGNORE_AFP_LOCK_ERRORS",
391#endif
392#if SQLITE_IGNORE_FLOCK_LOCK_ERRORS
393 "IGNORE_FLOCK_LOCK_ERRORS",
394#endif
395#if SQLITE_INLINE_MEMCPY
396 "INLINE_MEMCPY",
397#endif
398#if SQLITE_INT64_TYPE
399 "INT64_TYPE",
400#endif
401#ifdef SQLITE_INTEGRITY_CHECK_ERROR_MAX
402 "INTEGRITY_CHECK_ERROR_MAX=" CTIMEOPT_VAL(SQLITE_INTEGRITY_CHECK_ERROR_MAX),
403#endif
404#if SQLITE_LIKE_DOESNT_MATCH_BLOBS
405 "LIKE_DOESNT_MATCH_BLOBS",
406#endif
407#if SQLITE_LOCK_TRACE
408 "LOCK_TRACE",
409#endif
410#if SQLITE_LOG_CACHE_SPILL
411 "LOG_CACHE_SPILL",
412#endif
413#ifdef SQLITE_MALLOC_SOFT_LIMIT
414 "MALLOC_SOFT_LIMIT=" CTIMEOPT_VAL(SQLITE_MALLOC_SOFT_LIMIT),
415#endif
416#ifdef SQLITE_MAX_ATTACHED
417 "MAX_ATTACHED=" CTIMEOPT_VAL(SQLITE_MAX_ATTACHED),
418#endif
419#ifdef SQLITE_MAX_COLUMN
420 "MAX_COLUMN=" CTIMEOPT_VAL(SQLITE_MAX_COLUMN),
421#endif
422#ifdef SQLITE_MAX_COMPOUND_SELECT
423 "MAX_COMPOUND_SELECT=" CTIMEOPT_VAL(SQLITE_MAX_COMPOUND_SELECT),
424#endif
425#ifdef SQLITE_MAX_DEFAULT_PAGE_SIZE
426 "MAX_DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_DEFAULT_PAGE_SIZE),
427#endif
428#ifdef SQLITE_MAX_EXPR_DEPTH
429 "MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_EXPR_DEPTH),
430#endif
431#ifdef SQLITE_MAX_FUNCTION_ARG
432 "MAX_FUNCTION_ARG=" CTIMEOPT_VAL(SQLITE_MAX_FUNCTION_ARG),
433#endif
434#ifdef SQLITE_MAX_LENGTH
435 "MAX_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LENGTH),
436#endif
437#ifdef SQLITE_MAX_LIKE_PATTERN_LENGTH
438 "MAX_LIKE_PATTERN_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LIKE_PATTERN_LENGTH),
439#endif
440#ifdef SQLITE_MAX_MEMORY
441 "MAX_MEMORY=" CTIMEOPT_VAL(SQLITE_MAX_MEMORY),
442#endif
443#ifdef SQLITE_MAX_MMAP_SIZE
444 "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
445#endif
446#ifdef SQLITE_MAX_MMAP_SIZE_
447 "MAX_MMAP_SIZE_=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE_),
448#endif
449#ifdef SQLITE_MAX_PAGE_COUNT
450 "MAX_PAGE_COUNT=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_COUNT),
451#endif
452#ifdef SQLITE_MAX_PAGE_SIZE
453 "MAX_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_SIZE),
454#endif
455#ifdef SQLITE_MAX_SCHEMA_RETRY
456 "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
457#endif
458#ifdef SQLITE_MAX_SQL_LENGTH
459 "MAX_SQL_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_SQL_LENGTH),
460#endif
461#ifdef SQLITE_MAX_TRIGGER_DEPTH
462 "MAX_TRIGGER_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_TRIGGER_DEPTH),
463#endif
464#ifdef SQLITE_MAX_VARIABLE_NUMBER
465 "MAX_VARIABLE_NUMBER=" CTIMEOPT_VAL(SQLITE_MAX_VARIABLE_NUMBER),
466#endif
467#ifdef SQLITE_MAX_VDBE_OP
468 "MAX_VDBE_OP=" CTIMEOPT_VAL(SQLITE_MAX_VDBE_OP),
469#endif
470#ifdef SQLITE_MAX_WORKER_THREADS
471 "MAX_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_MAX_WORKER_THREADS),
472#endif
473#if SQLITE_MEMDEBUG
474 "MEMDEBUG",
475#endif
476#if SQLITE_MIXED_ENDIAN_64BIT_FLOAT
477 "MIXED_ENDIAN_64BIT_FLOAT",
478#endif
479#if SQLITE_MMAP_READWRITE
480 "MMAP_READWRITE",
481#endif
482#if SQLITE_MUTEX_NOOP
483 "MUTEX_NOOP",
484#endif
485#if SQLITE_MUTEX_NREF
486 "MUTEX_NREF",
487#endif
488#if SQLITE_MUTEX_OMIT
489 "MUTEX_OMIT",
490#endif
491#if SQLITE_MUTEX_PTHREADS
492 "MUTEX_PTHREADS",
493#endif
494#if SQLITE_MUTEX_W32
495 "MUTEX_W32",
496#endif
497#if SQLITE_NEED_ERR_NAME
498 "NEED_ERR_NAME",
499#endif
500#if SQLITE_NOINLINE
501 "NOINLINE",
502#endif
503#if SQLITE_NO_SYNC
504 "NO_SYNC",
505#endif
506#if SQLITE_OMIT_ALTERTABLE
507 "OMIT_ALTERTABLE",
508#endif
509#if SQLITE_OMIT_ANALYZE
510 "OMIT_ANALYZE",
511#endif
512#if SQLITE_OMIT_ATTACH
513 "OMIT_ATTACH",
514#endif
515#if SQLITE_OMIT_AUTHORIZATION
516 "OMIT_AUTHORIZATION",
517#endif
518#if SQLITE_OMIT_AUTOINCREMENT
519 "OMIT_AUTOINCREMENT",
520#endif
521#if SQLITE_OMIT_AUTOINIT
522 "OMIT_AUTOINIT",
523#endif
524#if SQLITE_OMIT_AUTOMATIC_INDEX
525 "OMIT_AUTOMATIC_INDEX",
526#endif
527#if SQLITE_OMIT_AUTORESET
528 "OMIT_AUTORESET",
529#endif
530#if SQLITE_OMIT_AUTOVACUUM
531 "OMIT_AUTOVACUUM",
532#endif
533#if SQLITE_OMIT_BETWEEN_OPTIMIZATION
534 "OMIT_BETWEEN_OPTIMIZATION",
535#endif
536#if SQLITE_OMIT_BLOB_LITERAL
537 "OMIT_BLOB_LITERAL",
538#endif
539#if SQLITE_OMIT_BTREECOUNT
540 "OMIT_BTREECOUNT",
541#endif
542#if SQLITE_OMIT_CAST
543 "OMIT_CAST",
544#endif
545#if SQLITE_OMIT_CHECK
546 "OMIT_CHECK",
547#endif
548#if SQLITE_OMIT_COMPLETE
549 "OMIT_COMPLETE",
550#endif
551#if SQLITE_OMIT_COMPOUND_SELECT
552 "OMIT_COMPOUND_SELECT",
553#endif
554#if SQLITE_OMIT_CONFLICT_CLAUSE
555 "OMIT_CONFLICT_CLAUSE",
556#endif
557#if SQLITE_OMIT_CTE
558 "OMIT_CTE",
559#endif
560#if SQLITE_OMIT_DATETIME_FUNCS
561 "OMIT_DATETIME_FUNCS",
562#endif
563#if SQLITE_OMIT_DECLTYPE
564 "OMIT_DECLTYPE",
565#endif
566#if SQLITE_OMIT_DEPRECATED
567 "OMIT_DEPRECATED",
568#endif
569#if SQLITE_OMIT_DISKIO
570 "OMIT_DISKIO",
571#endif
572#if SQLITE_OMIT_EXPLAIN
573 "OMIT_EXPLAIN",
574#endif
575#if SQLITE_OMIT_FLAG_PRAGMAS
576 "OMIT_FLAG_PRAGMAS",
577#endif
578#if SQLITE_OMIT_FLOATING_POINT
579 "OMIT_FLOATING_POINT",
580#endif
581#if SQLITE_OMIT_FOREIGN_KEY
582 "OMIT_FOREIGN_KEY",
583#endif
584#if SQLITE_OMIT_GET_TABLE
585 "OMIT_GET_TABLE",
586#endif
587#if SQLITE_OMIT_HEX_INTEGER
588 "OMIT_HEX_INTEGER",
589#endif
590#if SQLITE_OMIT_INCRBLOB
591 "OMIT_INCRBLOB",
592#endif
593#if SQLITE_OMIT_INTEGRITY_CHECK
594 "OMIT_INTEGRITY_CHECK",
595#endif
596#if SQLITE_OMIT_LIKE_OPTIMIZATION
597 "OMIT_LIKE_OPTIMIZATION",
598#endif
599#if SQLITE_OMIT_LOAD_EXTENSION
600 "OMIT_LOAD_EXTENSION",
601#endif
602#if SQLITE_OMIT_LOCALTIME
603 "OMIT_LOCALTIME",
604#endif
605#if SQLITE_OMIT_LOOKASIDE
606 "OMIT_LOOKASIDE",
607#endif
608#if SQLITE_OMIT_MEMORYDB
609 "OMIT_MEMORYDB",
610#endif
611#if SQLITE_OMIT_OR_OPTIMIZATION
612 "OMIT_OR_OPTIMIZATION",
613#endif
614#if SQLITE_OMIT_PAGER_PRAGMAS
615 "OMIT_PAGER_PRAGMAS",
616#endif
617#if SQLITE_OMIT_PARSER_TRACE
618 "OMIT_PARSER_TRACE",
619#endif
620#if SQLITE_OMIT_POPEN
621 "OMIT_POPEN",
622#endif
623#if SQLITE_OMIT_PRAGMA
624 "OMIT_PRAGMA",
625#endif
626#if SQLITE_OMIT_PROGRESS_CALLBACK
627 "OMIT_PROGRESS_CALLBACK",
628#endif
629#if SQLITE_OMIT_QUICKBALANCE
630 "OMIT_QUICKBALANCE",
631#endif
632#if SQLITE_OMIT_REINDEX
633 "OMIT_REINDEX",
634#endif
635#if SQLITE_OMIT_SCHEMA_PRAGMAS
636 "OMIT_SCHEMA_PRAGMAS",
637#endif
638#if SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
639 "OMIT_SCHEMA_VERSION_PRAGMAS",
640#endif
641#if SQLITE_OMIT_SHARED_CACHE
642 "OMIT_SHARED_CACHE",
643#endif
644#if SQLITE_OMIT_SHUTDOWN_DIRECTORIES
645 "OMIT_SHUTDOWN_DIRECTORIES",
646#endif
647#if SQLITE_OMIT_SUBQUERY
648 "OMIT_SUBQUERY",
649#endif
650#if SQLITE_OMIT_TCL_VARIABLE
651 "OMIT_TCL_VARIABLE",
652#endif
653#if SQLITE_OMIT_TEMPDB
654 "OMIT_TEMPDB",
655#endif
656#if SQLITE_OMIT_TEST_CONTROL
657 "OMIT_TEST_CONTROL",
658#endif
659#if SQLITE_OMIT_TRACE
660 "OMIT_TRACE",
661#endif
662#if SQLITE_OMIT_TRIGGER
663 "OMIT_TRIGGER",
664#endif
665#if SQLITE_OMIT_TRUNCATE_OPTIMIZATION
666 "OMIT_TRUNCATE_OPTIMIZATION",
667#endif
668#if SQLITE_OMIT_UTF16
669 "OMIT_UTF16",
670#endif
671#if SQLITE_OMIT_VACUUM
672 "OMIT_VACUUM",
673#endif
674#if SQLITE_OMIT_VIEW
675 "OMIT_VIEW",
676#endif
677#if SQLITE_OMIT_VIRTUALTABLE
678 "OMIT_VIRTUALTABLE",
679#endif
680#if SQLITE_OMIT_WAL
681 "OMIT_WAL",
682#endif
683#if SQLITE_OMIT_WSD
684 "OMIT_WSD",
685#endif
686#if SQLITE_OMIT_XFER_OPT
687 "OMIT_XFER_OPT",
688#endif
689#if SQLITE_PCACHE_SEPARATE_HEADER
690 "PCACHE_SEPARATE_HEADER",
691#endif
692#if SQLITE_PERFORMANCE_TRACE
693 "PERFORMANCE_TRACE",
694#endif
695#if SQLITE_POWERSAFE_OVERWRITE
696 "POWERSAFE_OVERWRITE",
697#endif
698#if SQLITE_PREFER_PROXY_LOCKING
699 "PREFER_PROXY_LOCKING",
700#endif
701#if SQLITE_PROXY_DEBUG
702 "PROXY_DEBUG",
703#endif
704#if SQLITE_REVERSE_UNORDERED_SELECTS
705 "REVERSE_UNORDERED_SELECTS",
706#endif
707#if SQLITE_RTREE_INT_ONLY
708 "RTREE_INT_ONLY",
709#endif
710#if SQLITE_SECURE_DELETE
711 "SECURE_DELETE",
712#endif
713#if SQLITE_SMALL_STACK
714 "SMALL_STACK",
715#endif
716#ifdef SQLITE_SORTER_PMASZ
717 "SORTER_PMASZ=" CTIMEOPT_VAL(SQLITE_SORTER_PMASZ),
718#endif
719#if SQLITE_SOUNDEX
720 "SOUNDEX",
721#endif
722#ifdef SQLITE_STAT4_SAMPLES
723 "STAT4_SAMPLES=" CTIMEOPT_VAL(SQLITE_STAT4_SAMPLES),
724#endif
725#ifdef SQLITE_STMTJRNL_SPILL
726 "STMTJRNL_SPILL=" CTIMEOPT_VAL(SQLITE_STMTJRNL_SPILL),
727#endif
728#if SQLITE_SUBSTR_COMPATIBILITY
729 "SUBSTR_COMPATIBILITY",
730#endif
731#if SQLITE_SYSTEM_MALLOC
732 "SYSTEM_MALLOC",
733#endif
734#if SQLITE_TCL
735 "TCL",
736#endif
737#ifdef SQLITE_TEMP_STORE
738 "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
739#endif
740#if SQLITE_TEST
741 "TEST",
742#endif
743#if defined(SQLITE_THREADSAFE)
744 "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
745#elif defined(THREADSAFE)
746 "THREADSAFE=" CTIMEOPT_VAL(THREADSAFE),
747#else
748 "THREADSAFE=1",
749#endif
750#if SQLITE_UNLINK_AFTER_CLOSE
751 "UNLINK_AFTER_CLOSE",
752#endif
753#if SQLITE_UNTESTABLE
754 "UNTESTABLE",
755#endif
756#if SQLITE_USER_AUTHENTICATION
757 "USER_AUTHENTICATION",
758#endif
759#if SQLITE_USE_ALLOCA
760 "USE_ALLOCA",
761#endif
762#if SQLITE_USE_FCNTL_TRACE
763 "USE_FCNTL_TRACE",
764#endif
765#if SQLITE_USE_URI
766 "USE_URI",
767#endif
768#if SQLITE_VDBE_COVERAGE
769 "VDBE_COVERAGE",
770#endif
771#if SQLITE_WIN32_MALLOC
772 "WIN32_MALLOC",
773#endif
774#if SQLITE_ZERO_MALLOC
775 "ZERO_MALLOC",
776#endif
777/*
778** END CODE GENERATED BY tool/mkctime.tcl
779*/
780};
781
782SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt){
783 *pnOpt = sizeof(sqlite3azCompileOpt) / sizeof(sqlite3azCompileOpt[0]);
784 return (const char**)sqlite3azCompileOpt;
785}
786
787#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
788
789/************** End of ctime.c ***********************************************/
790/************** Begin file sqliteInt.h ***************************************/
791/*
792** 2001 September 15
793**
794** The author disclaims copyright to this source code. In place of
795** a legal notice, here is a blessing:
796**
797** May you do good and not evil.
798** May you find forgiveness for yourself and forgive others.
799** May you share freely, never taking more than you give.
800**
801*************************************************************************
802** Internal interface definitions for SQLite.
803**
804*/
805#ifndef SQLITEINT_H
806#define SQLITEINT_H
807
808/* Special Comments:
809**
810** Some comments have special meaning to the tools that measure test
811** coverage:
812**
813** NO_TEST - The branches on this line are not
814** measured by branch coverage. This is
815** used on lines of code that actually
816** implement parts of coverage testing.
817**
818** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
819** and the correct answer is still obtained,
820** though perhaps more slowly.
821**
822** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
823** and the correct answer is still obtained,
824** though perhaps more slowly.
825**
826** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
827** that would be harmless and undetectable
828** if it did occur.
829**
830** In all cases, the special comment must be enclosed in the usual
831** slash-asterisk...asterisk-slash comment marks, with no spaces between the
832** asterisks and the comment text.
833*/
834
835/*
836** Make sure the Tcl calling convention macro is defined. This macro is
837** only used by test code and Tcl integration code.
838*/
839#ifndef SQLITE_TCLAPI
840# define SQLITE_TCLAPI
841#endif
842
843/*
844** Include the header file used to customize the compiler options for MSVC.
845** This should be done first so that it can successfully prevent spurious
846** compiler warnings due to subsequent content in this file and other files
847** that are included by this file.
848*/
849/************** Include msvc.h in the middle of sqliteInt.h ******************/
850/************** Begin file msvc.h ********************************************/
851/*
852** 2015 January 12
853**
854** The author disclaims copyright to this source code. In place of
855** a legal notice, here is a blessing:
856**
857** May you do good and not evil.
858** May you find forgiveness for yourself and forgive others.
859** May you share freely, never taking more than you give.
860**
861******************************************************************************
862**
863** This file contains code that is specific to MSVC.
864*/
865#ifndef SQLITE_MSVC_H
866#define SQLITE_MSVC_H
867
868#if defined(_MSC_VER)
869#pragma warning(disable : 4054)
870#pragma warning(disable : 4055)
871#pragma warning(disable : 4100)
872#pragma warning(disable : 4127)
873#pragma warning(disable : 4130)
874#pragma warning(disable : 4152)
875#pragma warning(disable : 4189)
876#pragma warning(disable : 4206)
877#pragma warning(disable : 4210)
878#pragma warning(disable : 4232)
879#pragma warning(disable : 4244)
880#pragma warning(disable : 4305)
881#pragma warning(disable : 4306)
882#pragma warning(disable : 4702)
883#pragma warning(disable : 4706)
884#endif /* defined(_MSC_VER) */
885
886#endif /* SQLITE_MSVC_H */
887
888/************** End of msvc.h ************************************************/
889/************** Continuing where we left off in sqliteInt.h ******************/
890
891/*
892** Special setup for VxWorks
893*/
894/************** Include vxworks.h in the middle of sqliteInt.h ***************/
895/************** Begin file vxworks.h *****************************************/
896/*
897** 2015-03-02
898**
899** The author disclaims copyright to this source code. In place of
900** a legal notice, here is a blessing:
901**
902** May you do good and not evil.
903** May you find forgiveness for yourself and forgive others.
904** May you share freely, never taking more than you give.
905**
906******************************************************************************
907**
908** This file contains code that is specific to Wind River's VxWorks
909*/
910#if defined(__RTP__) || defined(_WRS_KERNEL)
911/* This is VxWorks. Set up things specially for that OS
912*/
913#include <vxWorks.h>
914#include <pthread.h> /* amalgamator: dontcache */
915#define OS_VXWORKS 1
916#define SQLITE_OS_OTHER 0
917#define SQLITE_HOMEGROWN_RECURSIVE_MUTEX 1
918#define SQLITE_OMIT_LOAD_EXTENSION 1
919#define SQLITE_ENABLE_LOCKING_STYLE 0
920#define HAVE_UTIME 1
921#else
922/* This is not VxWorks. */
923#define OS_VXWORKS 0
924#define HAVE_FCHOWN 1
925#define HAVE_READLINK 1
926#define HAVE_LSTAT 1
927#endif /* defined(_WRS_KERNEL) */
928
929/************** End of vxworks.h *********************************************/
930/************** Continuing where we left off in sqliteInt.h ******************/
931
932/*
933** These #defines should enable >2GB file support on POSIX if the
934** underlying operating system supports it. If the OS lacks
935** large file support, or if the OS is windows, these should be no-ops.
936**
937** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
938** system #includes. Hence, this block of code must be the very first
939** code in all source files.
940**
941** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
942** on the compiler command line. This is necessary if you are compiling
943** on a recent machine (ex: Red Hat 7.2) but you want your code to work
944** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
945** without this option, LFS is enable. But LFS does not exist in the kernel
946** in Red Hat 6.0, so the code won't work. Hence, for maximum binary
947** portability you should omit LFS.
948**
949** The previous paragraph was written in 2005. (This paragraph is written
950** on 2008-11-28.) These days, all Linux kernels support large files, so
951** you should probably leave LFS enabled. But some embedded platforms might
952** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
953**
954** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
955*/
956#ifndef SQLITE_DISABLE_LFS
957# define _LARGE_FILE 1
958# ifndef _FILE_OFFSET_BITS
959# define _FILE_OFFSET_BITS 64
960# endif
961# define _LARGEFILE_SOURCE 1
962#endif
963
964/* The GCC_VERSION and MSVC_VERSION macros are used to
965** conditionally include optimizations for each of these compilers. A
966** value of 0 means that compiler is not being used. The
967** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
968** optimizations, and hence set all compiler macros to 0
969**
970** There was once also a CLANG_VERSION macro. However, we learn that the
971** version numbers in clang are for "marketing" only and are inconsistent
972** and unreliable. Fortunately, all versions of clang also recognize the
973** gcc version numbers and have reasonable settings for gcc version numbers,
974** so the GCC_VERSION macro will be set to a correct non-zero value even
975** when compiling with clang.
976*/
977#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
978# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
979#else
980# define GCC_VERSION 0
981#endif
982#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
983# define MSVC_VERSION _MSC_VER
984#else
985# define MSVC_VERSION 0
986#endif
987
988/* Needed for various definitions... */
989#if defined(__GNUC__) && !defined(_GNU_SOURCE)
990# define _GNU_SOURCE
991#endif
992
993#if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
994# define _BSD_SOURCE
995#endif
996
997/*
998** For MinGW, check to see if we can include the header file containing its
999** version information, among other things. Normally, this internal MinGW
1000** header file would [only] be included automatically by other MinGW header
1001** files; however, the contained version information is now required by this
1002** header file to work around binary compatibility issues (see below) and
1003** this is the only known way to reliably obtain it. This entire #if block
1004** would be completely unnecessary if there was any other way of detecting
1005** MinGW via their preprocessor (e.g. if they customized their GCC to define
1006** some MinGW-specific macros). When compiling for MinGW, either the
1007** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
1008** defined; otherwise, detection of conditions specific to MinGW will be
1009** disabled.
1010*/
1011#if defined(_HAVE_MINGW_H)
1012# include "mingw.h"
1013#elif defined(_HAVE__MINGW_H)
1014# include "_mingw.h"
1015#endif
1016
1017/*
1018** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
1019** define is required to maintain binary compatibility with the MSVC runtime
1020** library in use (e.g. for Windows XP).
1021*/
1022#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
1023 defined(_WIN32) && !defined(_WIN64) && \
1024 defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
1025 defined(__MSVCRT__)
1026# define _USE_32BIT_TIME_T
1027#endif
1028
1029/* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear
1030** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for
1031** MinGW.
1032*/
1033/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
1034/************** Begin file sqlite3.h *****************************************/
1035/*
1036** 2001-09-15
1037**
1038** The author disclaims copyright to this source code. In place of
1039** a legal notice, here is a blessing:
1040**
1041** May you do good and not evil.
1042** May you find forgiveness for yourself and forgive others.
1043** May you share freely, never taking more than you give.
1044**
1045*************************************************************************
1046** This header file defines the interface that the SQLite library
1047** presents to client programs. If a C-function, structure, datatype,
1048** or constant definition does not appear in this file, then it is
1049** not a published API of SQLite, is subject to change without
1050** notice, and should not be referenced by programs that use SQLite.
1051**
1052** Some of the definitions that are in this file are marked as
1053** "experimental". Experimental interfaces are normally new
1054** features recently added to SQLite. We do not anticipate changes
1055** to experimental interfaces but reserve the right to make minor changes
1056** if experience from use "in the wild" suggest such changes are prudent.
1057**
1058** The official C-language API documentation for SQLite is derived
1059** from comments in this file. This file is the authoritative source
1060** on how SQLite interfaces are supposed to operate.
1061**
1062** The name of this file under configuration management is "sqlite.h.in".
1063** The makefile makes some minor changes to this file (such as inserting
1064** the version number) and changes its name to "sqlite3.h" as
1065** part of the build process.
1066*/
1067#ifndef SQLITE3_H
1068#define SQLITE3_H
1069#include <stdarg.h> /* Needed for the definition of va_list */
1070
1071/*
1072** Make sure we can call this stuff from C++.
1073*/
1074#if 0
1075extern "C" {
1076#endif
1077
1078
1079/*
1080** Provide the ability to override linkage features of the interface.
1081*/
1082#ifndef SQLITE_EXTERN
1083# define SQLITE_EXTERN extern
1084#endif
1085#ifndef SQLITE_API
1086# define SQLITE_API
1087#endif
1088#ifndef SQLITE_CDECL
1089# define SQLITE_CDECL
1090#endif
1091#ifndef SQLITE_APICALL
1092# define SQLITE_APICALL
1093#endif
1094#ifndef SQLITE_STDCALL
1095# define SQLITE_STDCALL SQLITE_APICALL
1096#endif
1097#ifndef SQLITE_CALLBACK
1098# define SQLITE_CALLBACK
1099#endif
1100#ifndef SQLITE_SYSAPI
1101# define SQLITE_SYSAPI
1102#endif
1103
1104/*
1105** These no-op macros are used in front of interfaces to mark those
1106** interfaces as either deprecated or experimental. New applications
1107** should not use deprecated interfaces - they are supported for backwards
1108** compatibility only. Application writers should be aware that
1109** experimental interfaces are subject to change in point releases.
1110**
1111** These macros used to resolve to various kinds of compiler magic that
1112** would generate warning messages when they were used. But that
1113** compiler magic ended up generating such a flurry of bug reports
1114** that we have taken it all out and gone back to using simple
1115** noop macros.
1116*/
1117#define SQLITE_DEPRECATED
1118#define SQLITE_EXPERIMENTAL
1119
1120/*
1121** Ensure these symbols were not defined by some previous header file.
1122*/
1123#ifdef SQLITE_VERSION
1124# undef SQLITE_VERSION
1125#endif
1126#ifdef SQLITE_VERSION_NUMBER
1127# undef SQLITE_VERSION_NUMBER
1128#endif
1129
1130/*
1131** CAPI3REF: Compile-Time Library Version Numbers
1132**
1133** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
1134** evaluates to a string literal that is the SQLite version in the
1135** format "X.Y.Z" where X is the major version number (always 3 for
1136** SQLite3) and Y is the minor version number and Z is the release number.)^
1137** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
1138** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
1139** numbers used in [SQLITE_VERSION].)^
1140** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
1141** be larger than the release from which it is derived. Either Y will
1142** be held constant and Z will be incremented or else Y will be incremented
1143** and Z will be reset to zero.
1144**
1145** Since [version 3.6.18] ([dateof:3.6.18]),
1146** SQLite source code has been stored in the
1147** <a href="http://www.fossil-scm.org/">Fossil configuration management
1148** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
1149** a string which identifies a particular check-in of SQLite
1150** within its configuration management system. ^The SQLITE_SOURCE_ID
1151** string contains the date and time of the check-in (UTC) and a SHA1
1152** or SHA3-256 hash of the entire source tree. If the source code has
1153** been edited in any way since it was last checked in, then the last
1154** four hexadecimal digits of the hash may be modified.
1155**
1156** See also: [sqlite3_libversion()],
1157** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1158** [sqlite_version()] and [sqlite_source_id()].
1159*/
1160#define SQLITE_VERSION "3.25.2"
1161#define SQLITE_VERSION_NUMBER 3025002
1162#define SQLITE_SOURCE_ID "2018-09-25 19:08:10 fb90e7189ae6d62e77ba3a308ca5d683f90bbe633cf681865365b8e92792d1c7"
1163
1164/*
1165** CAPI3REF: Run-Time Library Version Numbers
1166** KEYWORDS: sqlite3_version sqlite3_sourceid
1167**
1168** These interfaces provide the same information as the [SQLITE_VERSION],
1169** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
1170** but are associated with the library instead of the header file. ^(Cautious
1171** programmers might include assert() statements in their application to
1172** verify that values returned by these interfaces match the macros in
1173** the header, and thus ensure that the application is
1174** compiled with matching library and header files.
1175**
1176** <blockquote><pre>
1177** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
1178** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
1179** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
1180** </pre></blockquote>)^
1181**
1182** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
1183** macro. ^The sqlite3_libversion() function returns a pointer to the
1184** to the sqlite3_version[] string constant. The sqlite3_libversion()
1185** function is provided for use in DLLs since DLL users usually do not have
1186** direct access to string constants within the DLL. ^The
1187** sqlite3_libversion_number() function returns an integer equal to
1188** [SQLITE_VERSION_NUMBER]. ^(The sqlite3_sourceid() function returns
1189** a pointer to a string constant whose value is the same as the
1190** [SQLITE_SOURCE_ID] C preprocessor macro. Except if SQLite is built
1191** using an edited copy of [the amalgamation], then the last four characters
1192** of the hash might be different from [SQLITE_SOURCE_ID].)^
1193**
1194** See also: [sqlite_version()] and [sqlite_source_id()].
1195*/
1196SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
1197SQLITE_API const char *sqlite3_libversion(void);
1198SQLITE_API const char *sqlite3_sourceid(void);
1199SQLITE_API int sqlite3_libversion_number(void);
1200
1201/*
1202** CAPI3REF: Run-Time Library Compilation Options Diagnostics
1203**
1204** ^The sqlite3_compileoption_used() function returns 0 or 1
1205** indicating whether the specified option was defined at
1206** compile time. ^The SQLITE_ prefix may be omitted from the
1207** option name passed to sqlite3_compileoption_used().
1208**
1209** ^The sqlite3_compileoption_get() function allows iterating
1210** over the list of options that were defined at compile time by
1211** returning the N-th compile time option string. ^If N is out of range,
1212** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
1213** prefix is omitted from any strings returned by
1214** sqlite3_compileoption_get().
1215**
1216** ^Support for the diagnostic functions sqlite3_compileoption_used()
1217** and sqlite3_compileoption_get() may be omitted by specifying the
1218** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
1219**
1220** See also: SQL functions [sqlite_compileoption_used()] and
1221** [sqlite_compileoption_get()] and the [compile_options pragma].
1222*/
1223#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1224SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
1225SQLITE_API const char *sqlite3_compileoption_get(int N);
1226#endif
1227
1228/*
1229** CAPI3REF: Test To See If The Library Is Threadsafe
1230**
1231** ^The sqlite3_threadsafe() function returns zero if and only if
1232** SQLite was compiled with mutexing code omitted due to the
1233** [SQLITE_THREADSAFE] compile-time option being set to 0.
1234**
1235** SQLite can be compiled with or without mutexes. When
1236** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
1237** are enabled and SQLite is threadsafe. When the
1238** [SQLITE_THREADSAFE] macro is 0,
1239** the mutexes are omitted. Without the mutexes, it is not safe
1240** to use SQLite concurrently from more than one thread.
1241**
1242** Enabling mutexes incurs a measurable performance penalty.
1243** So if speed is of utmost importance, it makes sense to disable
1244** the mutexes. But for maximum safety, mutexes should be enabled.
1245** ^The default behavior is for mutexes to be enabled.
1246**
1247** This interface can be used by an application to make sure that the
1248** version of SQLite that it is linking against was compiled with
1249** the desired setting of the [SQLITE_THREADSAFE] macro.
1250**
1251** This interface only reports on the compile-time mutex setting
1252** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with
1253** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
1254** can be fully or partially disabled using a call to [sqlite3_config()]
1255** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
1256** or [SQLITE_CONFIG_SERIALIZED]. ^(The return value of the
1257** sqlite3_threadsafe() function shows only the compile-time setting of
1258** thread safety, not any run-time changes to that setting made by
1259** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
1260** is unchanged by calls to sqlite3_config().)^
1261**
1262** See the [threading mode] documentation for additional information.
1263*/
1264SQLITE_API int sqlite3_threadsafe(void);
1265
1266/*
1267** CAPI3REF: Database Connection Handle
1268** KEYWORDS: {database connection} {database connections}
1269**
1270** Each open SQLite database is represented by a pointer to an instance of
1271** the opaque structure named "sqlite3". It is useful to think of an sqlite3
1272** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
1273** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
1274** and [sqlite3_close_v2()] are its destructors. There are many other
1275** interfaces (such as
1276** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
1277** [sqlite3_busy_timeout()] to name but three) that are methods on an
1278** sqlite3 object.
1279*/
1280typedef struct sqlite3 sqlite3;
1281
1282/*
1283** CAPI3REF: 64-Bit Integer Types
1284** KEYWORDS: sqlite_int64 sqlite_uint64
1285**
1286** Because there is no cross-platform way to specify 64-bit integer types
1287** SQLite includes typedefs for 64-bit signed and unsigned integers.
1288**
1289** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
1290** The sqlite_int64 and sqlite_uint64 types are supported for backwards
1291** compatibility only.
1292**
1293** ^The sqlite3_int64 and sqlite_int64 types can store integer values
1294** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
1295** sqlite3_uint64 and sqlite_uint64 types can store integer values
1296** between 0 and +18446744073709551615 inclusive.
1297*/
1298#ifdef SQLITE_INT64_TYPE
1299 typedef SQLITE_INT64_TYPE sqlite_int64;
1300# ifdef SQLITE_UINT64_TYPE
1301 typedef SQLITE_UINT64_TYPE sqlite_uint64;
1302# else
1303 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
1304# endif
1305#elif defined(_MSC_VER) || defined(__BORLANDC__)
1306 typedef __int64 sqlite_int64;
1307 typedef unsigned __int64 sqlite_uint64;
1308#else
1309 typedef long long int sqlite_int64;
1310 typedef unsigned long long int sqlite_uint64;
1311#endif
1312typedef sqlite_int64 sqlite3_int64;
1313typedef sqlite_uint64 sqlite3_uint64;
1314
1315/*
1316** If compiling for a processor that lacks floating point support,
1317** substitute integer for floating-point.
1318*/
1319#ifdef SQLITE_OMIT_FLOATING_POINT
1320# define double sqlite3_int64
1321#endif
1322
1323/*
1324** CAPI3REF: Closing A Database Connection
1325** DESTRUCTOR: sqlite3
1326**
1327** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
1328** for the [sqlite3] object.
1329** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
1330** the [sqlite3] object is successfully destroyed and all associated
1331** resources are deallocated.
1332**
1333** ^If the database connection is associated with unfinalized prepared
1334** statements or unfinished sqlite3_backup objects then sqlite3_close()
1335** will leave the database connection open and return [SQLITE_BUSY].
1336** ^If sqlite3_close_v2() is called with unfinalized prepared statements
1337** and/or unfinished sqlite3_backups, then the database connection becomes
1338** an unusable "zombie" which will automatically be deallocated when the
1339** last prepared statement is finalized or the last sqlite3_backup is
1340** finished. The sqlite3_close_v2() interface is intended for use with
1341** host languages that are garbage collected, and where the order in which
1342** destructors are called is arbitrary.
1343**
1344** Applications should [sqlite3_finalize | finalize] all [prepared statements],
1345** [sqlite3_blob_close | close] all [BLOB handles], and
1346** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
1347** with the [sqlite3] object prior to attempting to close the object. ^If
1348** sqlite3_close_v2() is called on a [database connection] that still has
1349** outstanding [prepared statements], [BLOB handles], and/or
1350** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation
1351** of resources is deferred until all [prepared statements], [BLOB handles],
1352** and [sqlite3_backup] objects are also destroyed.
1353**
1354** ^If an [sqlite3] object is destroyed while a transaction is open,
1355** the transaction is automatically rolled back.
1356**
1357** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
1358** must be either a NULL
1359** pointer or an [sqlite3] object pointer obtained
1360** from [sqlite3_open()], [sqlite3_open16()], or
1361** [sqlite3_open_v2()], and not previously closed.
1362** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
1363** argument is a harmless no-op.
1364*/
1365SQLITE_API int sqlite3_close(sqlite3*);
1366SQLITE_API int sqlite3_close_v2(sqlite3*);
1367
1368/*
1369** The type for a callback function.
1370** This is legacy and deprecated. It is included for historical
1371** compatibility and is not documented.
1372*/
1373typedef int (*sqlite3_callback)(void*,int,char**, char**);
1374
1375/*
1376** CAPI3REF: One-Step Query Execution Interface
1377** METHOD: sqlite3
1378**
1379** The sqlite3_exec() interface is a convenience wrapper around
1380** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
1381** that allows an application to run multiple statements of SQL
1382** without having to use a lot of C code.
1383**
1384** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
1385** semicolon-separate SQL statements passed into its 2nd argument,
1386** in the context of the [database connection] passed in as its 1st
1387** argument. ^If the callback function of the 3rd argument to
1388** sqlite3_exec() is not NULL, then it is invoked for each result row
1389** coming out of the evaluated SQL statements. ^The 4th argument to
1390** sqlite3_exec() is relayed through to the 1st argument of each
1391** callback invocation. ^If the callback pointer to sqlite3_exec()
1392** is NULL, then no callback is ever invoked and result rows are
1393** ignored.
1394**
1395** ^If an error occurs while evaluating the SQL statements passed into
1396** sqlite3_exec(), then execution of the current statement stops and
1397** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
1398** is not NULL then any error message is written into memory obtained
1399** from [sqlite3_malloc()] and passed back through the 5th parameter.
1400** To avoid memory leaks, the application should invoke [sqlite3_free()]
1401** on error message strings returned through the 5th parameter of
1402** sqlite3_exec() after the error message string is no longer needed.
1403** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
1404** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
1405** NULL before returning.
1406**
1407** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
1408** routine returns SQLITE_ABORT without invoking the callback again and
1409** without running any subsequent SQL statements.
1410**
1411** ^The 2nd argument to the sqlite3_exec() callback function is the
1412** number of columns in the result. ^The 3rd argument to the sqlite3_exec()
1413** callback is an array of pointers to strings obtained as if from
1414** [sqlite3_column_text()], one for each column. ^If an element of a
1415** result row is NULL then the corresponding string pointer for the
1416** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the
1417** sqlite3_exec() callback is an array of pointers to strings where each
1418** entry represents the name of corresponding result column as obtained
1419** from [sqlite3_column_name()].
1420**
1421** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
1422** to an empty string, or a pointer that contains only whitespace and/or
1423** SQL comments, then no SQL statements are evaluated and the database
1424** is not changed.
1425**
1426** Restrictions:
1427**
1428** <ul>
1429** <li> The application must ensure that the 1st parameter to sqlite3_exec()
1430** is a valid and open [database connection].
1431** <li> The application must not close the [database connection] specified by
1432** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
1433** <li> The application must not modify the SQL statement text passed into
1434** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
1435** </ul>
1436*/
1437SQLITE_API int sqlite3_exec(
1438 sqlite3*, /* An open database */
1439 const char *sql, /* SQL to be evaluated */
1440 int (*callback)(void*,int,char**,char**), /* Callback function */
1441 void *, /* 1st argument to callback */
1442 char **errmsg /* Error msg written here */
1443);
1444
1445/*
1446** CAPI3REF: Result Codes
1447** KEYWORDS: {result code definitions}
1448**
1449** Many SQLite functions return an integer result code from the set shown
1450** here in order to indicate success or failure.
1451**
1452** New error codes may be added in future versions of SQLite.
1453**
1454** See also: [extended result code definitions]
1455*/
1456#define SQLITE_OK 0 /* Successful result */
1457/* beginning-of-error-codes */
1458#define SQLITE_ERROR 1 /* Generic error */
1459#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
1460#define SQLITE_PERM 3 /* Access permission denied */
1461#define SQLITE_ABORT 4 /* Callback routine requested an abort */
1462#define SQLITE_BUSY 5 /* The database file is locked */
1463#define SQLITE_LOCKED 6 /* A table in the database is locked */
1464#define SQLITE_NOMEM 7 /* A malloc() failed */
1465#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
1466#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
1467#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
1468#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
1469#define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
1470#define SQLITE_FULL 13 /* Insertion failed because database is full */
1471#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
1472#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
1473#define SQLITE_EMPTY 16 /* Internal use only */
1474#define SQLITE_SCHEMA 17 /* The database schema changed */
1475#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
1476#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
1477#define SQLITE_MISMATCH 20 /* Data type mismatch */
1478#define SQLITE_MISUSE 21 /* Library used incorrectly */
1479#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
1480#define SQLITE_AUTH 23 /* Authorization denied */
1481#define SQLITE_FORMAT 24 /* Not used */
1482#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
1483#define SQLITE_NOTADB 26 /* File opened that is not a database file */
1484#define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */
1485#define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */
1486#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
1487#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
1488/* end-of-error-codes */
1489
1490/*
1491** CAPI3REF: Extended Result Codes
1492** KEYWORDS: {extended result code definitions}
1493**
1494** In its default configuration, SQLite API routines return one of 30 integer
1495** [result codes]. However, experience has shown that many of
1496** these result codes are too coarse-grained. They do not provide as
1497** much information about problems as programmers might like. In an effort to
1498** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
1499** and later) include
1500** support for additional result codes that provide more detailed information
1501** about errors. These [extended result codes] are enabled or disabled
1502** on a per database connection basis using the
1503** [sqlite3_extended_result_codes()] API. Or, the extended code for
1504** the most recent error can be obtained using
1505** [sqlite3_extended_errcode()].
1506*/
1507#define SQLITE_ERROR_MISSING_COLLSEQ (SQLITE_ERROR | (1<<8))
1508#define SQLITE_ERROR_RETRY (SQLITE_ERROR | (2<<8))
1509#define SQLITE_ERROR_SNAPSHOT (SQLITE_ERROR | (3<<8))
1510#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
1511#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
1512#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
1513#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
1514#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8))
1515#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8))
1516#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8))
1517#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8))
1518#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8))
1519#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8))
1520#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8))
1521#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8))
1522#define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
1523#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
1524#define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
1525#define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
1526#define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
1527#define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8))
1528#define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
1529#define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
1530#define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
1531#define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
1532#define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
1533#define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
1534#define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8))
1535#define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8))
1536#define SQLITE_IOERR_VNODE (SQLITE_IOERR | (27<<8))
1537#define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8))
1538#define SQLITE_IOERR_BEGIN_ATOMIC (SQLITE_IOERR | (29<<8))
1539#define SQLITE_IOERR_COMMIT_ATOMIC (SQLITE_IOERR | (30<<8))
1540#define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8))
1541#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
1542#define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8))
1543#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
1544#define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
1545#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
1546#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
1547#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
1548#define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
1549#define SQLITE_CANTOPEN_DIRTYWAL (SQLITE_CANTOPEN | (5<<8)) /* Not Used */
1550#define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
1551#define SQLITE_CORRUPT_SEQUENCE (SQLITE_CORRUPT | (2<<8))
1552#define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
1553#define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
1554#define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
1555#define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8))
1556#define SQLITE_READONLY_CANTINIT (SQLITE_READONLY | (5<<8))
1557#define SQLITE_READONLY_DIRECTORY (SQLITE_READONLY | (6<<8))
1558#define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8))
1559#define SQLITE_CONSTRAINT_CHECK (SQLITE_CONSTRAINT | (1<<8))
1560#define SQLITE_CONSTRAINT_COMMITHOOK (SQLITE_CONSTRAINT | (2<<8))
1561#define SQLITE_CONSTRAINT_FOREIGNKEY (SQLITE_CONSTRAINT | (3<<8))
1562#define SQLITE_CONSTRAINT_FUNCTION (SQLITE_CONSTRAINT | (4<<8))
1563#define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
1564#define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8))
1565#define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8))
1566#define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8))
1567#define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8))
1568#define SQLITE_CONSTRAINT_ROWID (SQLITE_CONSTRAINT |(10<<8))
1569#define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8))
1570#define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
1571#define SQLITE_WARNING_AUTOINDEX (SQLITE_WARNING | (1<<8))
1572#define SQLITE_AUTH_USER (SQLITE_AUTH | (1<<8))
1573#define SQLITE_OK_LOAD_PERMANENTLY (SQLITE_OK | (1<<8))
1574
1575/*
1576** CAPI3REF: Flags For File Open Operations
1577**
1578** These bit values are intended for use in the
1579** 3rd parameter to the [sqlite3_open_v2()] interface and
1580** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
1581*/
1582#define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */
1583#define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */
1584#define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */
1585#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */
1586#define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */
1587#define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */
1588#define SQLITE_OPEN_URI 0x00000040 /* Ok for sqlite3_open_v2() */
1589#define SQLITE_OPEN_MEMORY 0x00000080 /* Ok for sqlite3_open_v2() */
1590#define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */
1591#define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */
1592#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */
1593#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */
1594#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */
1595#define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */
1596#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */
1597#define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */
1598#define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */
1599#define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */
1600#define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */
1601#define SQLITE_OPEN_WAL 0x00080000 /* VFS only */
1602
1603/* Reserved: 0x00F00000 */
1604
1605/*
1606** CAPI3REF: Device Characteristics
1607**
1608** The xDeviceCharacteristics method of the [sqlite3_io_methods]
1609** object returns an integer which is a vector of these
1610** bit values expressing I/O characteristics of the mass storage
1611** device that holds the file that the [sqlite3_io_methods]
1612** refers to.
1613**
1614** The SQLITE_IOCAP_ATOMIC property means that all writes of
1615** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1616** mean that writes of blocks that are nnn bytes in size and
1617** are aligned to an address which is an integer multiple of
1618** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1619** that when data is appended to a file, the data is appended
1620** first then the size of the file is extended, never the other
1621** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1622** information is written to disk in the same order as calls
1623** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
1624** after reboot following a crash or power loss, the only bytes in a
1625** file that were written at the application level might have changed
1626** and that adjacent bytes, even bytes within the same sector are
1627** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
1628** flag indicates that a file cannot be deleted when open. The
1629** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
1630** read-only media and cannot be changed even by processes with
1631** elevated privileges.
1632**
1633** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
1634** filesystem supports doing multiple write operations atomically when those
1635** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
1636** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
1637*/
1638#define SQLITE_IOCAP_ATOMIC 0x00000001
1639#define SQLITE_IOCAP_ATOMIC512 0x00000002
1640#define SQLITE_IOCAP_ATOMIC1K 0x00000004
1641#define SQLITE_IOCAP_ATOMIC2K 0x00000008
1642#define SQLITE_IOCAP_ATOMIC4K 0x00000010
1643#define SQLITE_IOCAP_ATOMIC8K 0x00000020
1644#define SQLITE_IOCAP_ATOMIC16K 0x00000040
1645#define SQLITE_IOCAP_ATOMIC32K 0x00000080
1646#define SQLITE_IOCAP_ATOMIC64K 0x00000100
1647#define SQLITE_IOCAP_SAFE_APPEND 0x00000200
1648#define SQLITE_IOCAP_SEQUENTIAL 0x00000400
1649#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
1650#define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
1651#define SQLITE_IOCAP_IMMUTABLE 0x00002000
1652#define SQLITE_IOCAP_BATCH_ATOMIC 0x00004000
1653
1654/*
1655** CAPI3REF: File Locking Levels
1656**
1657** SQLite uses one of these integer values as the second
1658** argument to calls it makes to the xLock() and xUnlock() methods
1659** of an [sqlite3_io_methods] object.
1660*/
1661#define SQLITE_LOCK_NONE 0
1662#define SQLITE_LOCK_SHARED 1
1663#define SQLITE_LOCK_RESERVED 2
1664#define SQLITE_LOCK_PENDING 3
1665#define SQLITE_LOCK_EXCLUSIVE 4
1666
1667/*
1668** CAPI3REF: Synchronization Type Flags
1669**
1670** When SQLite invokes the xSync() method of an
1671** [sqlite3_io_methods] object it uses a combination of
1672** these integer values as the second argument.
1673**
1674** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
1675** sync operation only needs to flush data to mass storage. Inode
1676** information need not be flushed. If the lower four bits of the flag
1677** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
1678** If the lower four bits equal SQLITE_SYNC_FULL, that means
1679** to use Mac OS X style fullsync instead of fsync().
1680**
1681** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
1682** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
1683** settings. The [synchronous pragma] determines when calls to the
1684** xSync VFS method occur and applies uniformly across all platforms.
1685** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
1686** energetic or rigorous or forceful the sync operations are and
1687** only make a difference on Mac OSX for the default SQLite code.
1688** (Third-party VFS implementations might also make the distinction
1689** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
1690** operating systems natively supported by SQLite, only Mac OSX
1691** cares about the difference.)
1692*/
1693#define SQLITE_SYNC_NORMAL 0x00002
1694#define SQLITE_SYNC_FULL 0x00003
1695#define SQLITE_SYNC_DATAONLY 0x00010
1696
1697/*
1698** CAPI3REF: OS Interface Open File Handle
1699**
1700** An [sqlite3_file] object represents an open file in the
1701** [sqlite3_vfs | OS interface layer]. Individual OS interface
1702** implementations will
1703** want to subclass this object by appending additional fields
1704** for their own use. The pMethods entry is a pointer to an
1705** [sqlite3_io_methods] object that defines methods for performing
1706** I/O operations on the open file.
1707*/
1708typedef struct sqlite3_file sqlite3_file;
1709struct sqlite3_file {
1710 const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
1711};
1712
1713/*
1714** CAPI3REF: OS Interface File Virtual Methods Object
1715**
1716** Every file opened by the [sqlite3_vfs.xOpen] method populates an
1717** [sqlite3_file] object (or, more commonly, a subclass of the
1718** [sqlite3_file] object) with a pointer to an instance of this object.
1719** This object defines the methods used to perform various operations
1720** against the open file represented by the [sqlite3_file] object.
1721**
1722** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
1723** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1724** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The
1725** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
1726** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
1727** to NULL.
1728**
1729** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1730** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1731** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
1732** flag may be ORed in to indicate that only the data of the file
1733** and not its inode needs to be synced.
1734**
1735** The integer values to xLock() and xUnlock() are one of
1736** <ul>
1737** <li> [SQLITE_LOCK_NONE],
1738** <li> [SQLITE_LOCK_SHARED],
1739** <li> [SQLITE_LOCK_RESERVED],
1740** <li> [SQLITE_LOCK_PENDING], or
1741** <li> [SQLITE_LOCK_EXCLUSIVE].
1742** </ul>
1743** xLock() increases the lock. xUnlock() decreases the lock.
1744** The xCheckReservedLock() method checks whether any database connection,
1745** either in this process or in some other process, is holding a RESERVED,
1746** PENDING, or EXCLUSIVE lock on the file. It returns true
1747** if such a lock exists and false otherwise.
1748**
1749** The xFileControl() method is a generic interface that allows custom
1750** VFS implementations to directly control an open file using the
1751** [sqlite3_file_control()] interface. The second "op" argument is an
1752** integer opcode. The third argument is a generic pointer intended to
1753** point to a structure that may contain arguments or space in which to
1754** write return values. Potential uses for xFileControl() might be
1755** functions to enable blocking locks with timeouts, to change the
1756** locking strategy (for example to use dot-file locks), to inquire
1757** about the status of a lock, or to break stale locks. The SQLite
1758** core reserves all opcodes less than 100 for its own use.
1759** A [file control opcodes | list of opcodes] less than 100 is available.
1760** Applications that define a custom xFileControl method should use opcodes
1761** greater than 100 to avoid conflicts. VFS implementations should
1762** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1763** recognize.
1764**
1765** The xSectorSize() method returns the sector size of the
1766** device that underlies the file. The sector size is the
1767** minimum write that can be performed without disturbing
1768** other bytes in the file. The xDeviceCharacteristics()
1769** method returns a bit vector describing behaviors of the
1770** underlying device:
1771**
1772** <ul>
1773** <li> [SQLITE_IOCAP_ATOMIC]
1774** <li> [SQLITE_IOCAP_ATOMIC512]
1775** <li> [SQLITE_IOCAP_ATOMIC1K]
1776** <li> [SQLITE_IOCAP_ATOMIC2K]
1777** <li> [SQLITE_IOCAP_ATOMIC4K]
1778** <li> [SQLITE_IOCAP_ATOMIC8K]
1779** <li> [SQLITE_IOCAP_ATOMIC16K]
1780** <li> [SQLITE_IOCAP_ATOMIC32K]
1781** <li> [SQLITE_IOCAP_ATOMIC64K]
1782** <li> [SQLITE_IOCAP_SAFE_APPEND]
1783** <li> [SQLITE_IOCAP_SEQUENTIAL]
1784** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
1785** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
1786** <li> [SQLITE_IOCAP_IMMUTABLE]
1787** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
1788** </ul>
1789**
1790** The SQLITE_IOCAP_ATOMIC property means that all writes of
1791** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1792** mean that writes of blocks that are nnn bytes in size and
1793** are aligned to an address which is an integer multiple of
1794** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1795** that when data is appended to a file, the data is appended
1796** first then the size of the file is extended, never the other
1797** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1798** information is written to disk in the same order as calls
1799** to xWrite().
1800**
1801** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
1802** in the unread portions of the buffer with zeros. A VFS that
1803** fails to zero-fill short reads might seem to work. However,
1804** failure to zero-fill short reads will eventually lead to
1805** database corruption.
1806*/
1807typedef struct sqlite3_io_methods sqlite3_io_methods;
1808struct sqlite3_io_methods {
1809 int iVersion;
1810 int (*xClose)(sqlite3_file*);
1811 int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1812 int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1813 int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1814 int (*xSync)(sqlite3_file*, int flags);
1815 int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1816 int (*xLock)(sqlite3_file*, int);
1817 int (*xUnlock)(sqlite3_file*, int);
1818 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1819 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1820 int (*xSectorSize)(sqlite3_file*);
1821 int (*xDeviceCharacteristics)(sqlite3_file*);
1822 /* Methods above are valid for version 1 */
1823 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1824 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1825 void (*xShmBarrier)(sqlite3_file*);
1826 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1827 /* Methods above are valid for version 2 */
1828 int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
1829 int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
1830 /* Methods above are valid for version 3 */
1831 /* Additional methods may be added in future releases */
1832};
1833
1834/*
1835** CAPI3REF: Standard File Control Opcodes
1836** KEYWORDS: {file control opcodes} {file control opcode}
1837**
1838** These integer constants are opcodes for the xFileControl method
1839** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
1840** interface.
1841**
1842** <ul>
1843** <li>[[SQLITE_FCNTL_LOCKSTATE]]
1844** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
1845** opcode causes the xFileControl method to write the current state of
1846** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1847** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1848** into an integer that the pArg argument points to. This capability
1849** is used during testing and is only available when the SQLITE_TEST
1850** compile-time option is used.
1851**
1852** <li>[[SQLITE_FCNTL_SIZE_HINT]]
1853** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1854** layer a hint of how large the database file will grow to be during the
1855** current transaction. This hint is not guaranteed to be accurate but it
1856** is often close. The underlying VFS might choose to preallocate database
1857** file space based on this hint in order to help writes to the database
1858** file run faster.
1859**
1860** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
1861** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1862** extends and truncates the database file in chunks of a size specified
1863** by the user. The fourth argument to [sqlite3_file_control()] should
1864** point to an integer (type int) containing the new chunk-size to use
1865** for the nominated database. Allocating database file space in large
1866** chunks (say 1MB at a time), may reduce file-system fragmentation and
1867** improve performance on some systems.
1868**
1869** <li>[[SQLITE_FCNTL_FILE_POINTER]]
1870** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1871** to the [sqlite3_file] object associated with a particular database
1872** connection. See also [SQLITE_FCNTL_JOURNAL_POINTER].
1873**
1874** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]]
1875** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
1876** to the [sqlite3_file] object associated with the journal file (either
1877** the [rollback journal] or the [write-ahead log]) for a particular database
1878** connection. See also [SQLITE_FCNTL_FILE_POINTER].
1879**
1880** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
1881** No longer in use.
1882**
1883** <li>[[SQLITE_FCNTL_SYNC]]
1884** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
1885** sent to the VFS immediately before the xSync method is invoked on a
1886** database file descriptor. Or, if the xSync method is not invoked
1887** because the user has configured SQLite with
1888** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
1889** of the xSync method. In most cases, the pointer argument passed with
1890** this file-control is NULL. However, if the database file is being synced
1891** as part of a multi-database commit, the argument points to a nul-terminated
1892** string containing the transactions master-journal file name. VFSes that
1893** do not need this signal should silently ignore this opcode. Applications
1894** should not call [sqlite3_file_control()] with this opcode as doing so may
1895** disrupt the operation of the specialized VFSes that do require it.
1896**
1897** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
1898** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
1899** and sent to the VFS after a transaction has been committed immediately
1900** but before the database is unlocked. VFSes that do not need this signal
1901** should silently ignore this opcode. Applications should not call
1902** [sqlite3_file_control()] with this opcode as doing so may disrupt the
1903** operation of the specialized VFSes that do require it.
1904**
1905** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
1906** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1907** retry counts and intervals for certain disk I/O operations for the
1908** windows [VFS] in order to provide robustness in the presence of
1909** anti-virus programs. By default, the windows VFS will retry file read,
1910** file write, and file delete operations up to 10 times, with a delay
1911** of 25 milliseconds before the first retry and with the delay increasing
1912** by an additional 25 milliseconds with each subsequent retry. This
1913** opcode allows these two values (10 retries and 25 milliseconds of delay)
1914** to be adjusted. The values are changed for all database connections
1915** within the same process. The argument is a pointer to an array of two
1916** integers where the first integer is the new retry count and the second
1917** integer is the delay. If either integer is negative, then the setting
1918** is not changed but instead the prior value of that setting is written
1919** into the array entry, allowing the current retry settings to be
1920** interrogated. The zDbName parameter is ignored.
1921**
1922** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
1923** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1924** persistent [WAL | Write Ahead Log] setting. By default, the auxiliary
1925** write ahead log ([WAL file]) and shared memory
1926** files used for transaction control
1927** are automatically deleted when the latest connection to the database
1928** closes. Setting persistent WAL mode causes those files to persist after
1929** close. Persisting the files is useful when other processes that do not
1930** have write permission on the directory containing the database file want
1931** to read the database file, as the WAL and shared memory files must exist
1932** in order for the database to be readable. The fourth parameter to
1933** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1934** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1935** WAL mode. If the integer is -1, then it is overwritten with the current
1936** WAL persistence setting.
1937**
1938** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
1939** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
1940** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1941** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
1942** xDeviceCharacteristics methods. The fourth parameter to
1943** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1944** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1945** mode. If the integer is -1, then it is overwritten with the current
1946** zero-damage mode setting.
1947**
1948** <li>[[SQLITE_FCNTL_OVERWRITE]]
1949** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1950** a write transaction to indicate that, unless it is rolled back for some
1951** reason, the entire database file will be overwritten by the current
1952** transaction. This is used by VACUUM operations.
1953**
1954** <li>[[SQLITE_FCNTL_VFSNAME]]
1955** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
1956** all [VFSes] in the VFS stack. The names are of all VFS shims and the
1957** final bottom-level VFS are written into memory obtained from
1958** [sqlite3_malloc()] and the result is stored in the char* variable
1959** that the fourth parameter of [sqlite3_file_control()] points to.
1960** The caller is responsible for freeing the memory when done. As with
1961** all file-control actions, there is no guarantee that this will actually
1962** do anything. Callers should initialize the char* variable to a NULL
1963** pointer in case this file-control is not implemented. This file-control
1964** is intended for diagnostic use only.
1965**
1966** <li>[[SQLITE_FCNTL_VFS_POINTER]]
1967** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1968** [VFSes] currently in use. ^(The argument X in
1969** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
1970** of type "[sqlite3_vfs] **". This opcodes will set *X
1971** to a pointer to the top-level VFS.)^
1972** ^When there are multiple VFS shims in the stack, this opcode finds the
1973** upper-most shim only.
1974**
1975** <li>[[SQLITE_FCNTL_PRAGMA]]
1976** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
1977** file control is sent to the open [sqlite3_file] object corresponding
1978** to the database file to which the pragma statement refers. ^The argument
1979** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
1980** pointers to strings (char**) in which the second element of the array
1981** is the name of the pragma and the third element is the argument to the
1982** pragma or NULL if the pragma has no argument. ^The handler for an
1983** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
1984** of the char** argument point to a string obtained from [sqlite3_mprintf()]
1985** or the equivalent and that string will become the result of the pragma or
1986** the error message if the pragma fails. ^If the
1987** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
1988** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA]
1989** file control returns [SQLITE_OK], then the parser assumes that the
1990** VFS has handled the PRAGMA itself and the parser generates a no-op
1991** prepared statement if result string is NULL, or that returns a copy
1992** of the result string if the string is non-NULL.
1993** ^If the [SQLITE_FCNTL_PRAGMA] file control returns
1994** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
1995** that the VFS encountered an error while handling the [PRAGMA] and the
1996** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
1997** file control occurs at the beginning of pragma statement analysis and so
1998** it is able to override built-in [PRAGMA] statements.
1999**
2000** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
2001** ^The [SQLITE_FCNTL_BUSYHANDLER]
2002** file-control may be invoked by SQLite on the database file handle
2003** shortly after it is opened in order to provide a custom VFS with access
2004** to the connections busy-handler callback. The argument is of type (void **)
2005** - an array of two (void *) values. The first (void *) actually points
2006** to a function of type (int (*)(void *)). In order to invoke the connections
2007** busy-handler, this function should be invoked with the second (void *) in
2008** the array as the only argument. If it returns non-zero, then the operation
2009** should be retried. If it returns zero, the custom VFS should abandon the
2010** current operation.
2011**
2012** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
2013** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
2014** to have SQLite generate a
2015** temporary filename using the same algorithm that is followed to generate
2016** temporary filenames for TEMP tables and other internal uses. The
2017** argument should be a char** which will be filled with the filename
2018** written into memory obtained from [sqlite3_malloc()]. The caller should
2019** invoke [sqlite3_free()] on the result to avoid a memory leak.
2020**
2021** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
2022** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
2023** maximum number of bytes that will be used for memory-mapped I/O.
2024** The argument is a pointer to a value of type sqlite3_int64 that
2025** is an advisory maximum number of bytes in the file to memory map. The
2026** pointer is overwritten with the old value. The limit is not changed if
2027** the value originally pointed to is negative, and so the current limit
2028** can be queried by passing in a pointer to a negative number. This
2029** file-control is used internally to implement [PRAGMA mmap_size].
2030**
2031** <li>[[SQLITE_FCNTL_TRACE]]
2032** The [SQLITE_FCNTL_TRACE] file control provides advisory information
2033** to the VFS about what the higher layers of the SQLite stack are doing.
2034** This file control is used by some VFS activity tracing [shims].
2035** The argument is a zero-terminated string. Higher layers in the
2036** SQLite stack may generate instances of this file control if
2037** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
2038**
2039** <li>[[SQLITE_FCNTL_HAS_MOVED]]
2040** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
2041** pointer to an integer and it writes a boolean into that integer depending
2042** on whether or not the file has been renamed, moved, or deleted since it
2043** was first opened.
2044**
2045** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]]
2046** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the
2047** underlying native file handle associated with a file handle. This file
2048** control interprets its argument as a pointer to a native file handle and
2049** writes the resulting value there.
2050**
2051** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
2052** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
2053** opcode causes the xFileControl method to swap the file handle with the one
2054** pointed to by the pArg argument. This capability is used during testing
2055** and only needs to be supported when SQLITE_TEST is defined.
2056**
2057** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
2058** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
2059** be advantageous to block on the next WAL lock if the lock is not immediately
2060** available. The WAL subsystem issues this signal during rare
2061** circumstances in order to fix a problem with priority inversion.
2062** Applications should <em>not</em> use this file-control.
2063**
2064** <li>[[SQLITE_FCNTL_ZIPVFS]]
2065** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
2066** VFS should return SQLITE_NOTFOUND for this opcode.
2067**
2068** <li>[[SQLITE_FCNTL_RBU]]
2069** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
2070** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
2071** this opcode.
2072**
2073** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
2074** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
2075** the file descriptor is placed in "batch write mode", which
2076** means all subsequent write operations will be deferred and done
2077** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. Systems
2078** that do not support batch atomic writes will return SQLITE_NOTFOUND.
2079** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
2080** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
2081** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
2082** no VFS interface calls on the same [sqlite3_file] file descriptor
2083** except for calls to the xWrite method and the xFileControl method
2084** with [SQLITE_FCNTL_SIZE_HINT].
2085**
2086** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
2087** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
2088** operations since the previous successful call to
2089** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
2090** This file control returns [SQLITE_OK] if and only if the writes were
2091** all performed successfully and have been committed to persistent storage.
2092** ^Regardless of whether or not it is successful, this file control takes
2093** the file descriptor out of batch write mode so that all subsequent
2094** write operations are independent.
2095** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
2096** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
2097**
2098** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
2099** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
2100** operations since the previous successful call to
2101** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
2102** ^This file control takes the file descriptor out of batch write mode
2103** so that all subsequent write operations are independent.
2104** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
2105** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
2106**
2107** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
2108** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
2109** a file lock using the xLock or xShmLock methods of the VFS to wait
2110** for up to M milliseconds before failing, where M is the single
2111** unsigned integer parameter.
2112**
2113** <li>[[SQLITE_FCNTL_DATA_VERSION]]
2114** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
2115** a database file. The argument is a pointer to a 32-bit unsigned integer.
2116** The "data version" for the pager is written into the pointer. The
2117** "data version" changes whenever any change occurs to the corresponding
2118** database file, either through SQL statements on the same database
2119** connection or through transactions committed by separate database
2120** connections possibly in other processes. The [sqlite3_total_changes()]
2121** interface can be used to find if any database on the connection has changed,
2122** but that interface responds to changes on TEMP as well as MAIN and does
2123** not provide a mechanism to detect changes to MAIN only. Also, the
2124** [sqlite3_total_changes()] interface responds to internal changes only and
2125** omits changes made by other database connections. The
2126** [PRAGMA data_version] command provide a mechanism to detect changes to
2127** a single attached database that occur due to other database connections,
2128** but omits changes implemented by the database connection on which it is
2129** called. This file control is the only mechanism to detect changes that
2130** happen either internally or externally and that are associated with
2131** a particular attached database.
2132** </ul>
2133*/
2134#define SQLITE_FCNTL_LOCKSTATE 1
2135#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
2136#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3
2137#define SQLITE_FCNTL_LAST_ERRNO 4
2138#define SQLITE_FCNTL_SIZE_HINT 5
2139#define SQLITE_FCNTL_CHUNK_SIZE 6
2140#define SQLITE_FCNTL_FILE_POINTER 7
2141#define SQLITE_FCNTL_SYNC_OMITTED 8
2142#define SQLITE_FCNTL_WIN32_AV_RETRY 9
2143#define SQLITE_FCNTL_PERSIST_WAL 10
2144#define SQLITE_FCNTL_OVERWRITE 11
2145#define SQLITE_FCNTL_VFSNAME 12
2146#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
2147#define SQLITE_FCNTL_PRAGMA 14
2148#define SQLITE_FCNTL_BUSYHANDLER 15
2149#define SQLITE_FCNTL_TEMPFILENAME 16
2150#define SQLITE_FCNTL_MMAP_SIZE 18
2151#define SQLITE_FCNTL_TRACE 19
2152#define SQLITE_FCNTL_HAS_MOVED 20
2153#define SQLITE_FCNTL_SYNC 21
2154#define SQLITE_FCNTL_COMMIT_PHASETWO 22
2155#define SQLITE_FCNTL_WIN32_SET_HANDLE 23
2156#define SQLITE_FCNTL_WAL_BLOCK 24
2157#define SQLITE_FCNTL_ZIPVFS 25
2158#define SQLITE_FCNTL_RBU 26
2159#define SQLITE_FCNTL_VFS_POINTER 27
2160#define SQLITE_FCNTL_JOURNAL_POINTER 28
2161#define SQLITE_FCNTL_WIN32_GET_HANDLE 29
2162#define SQLITE_FCNTL_PDB 30
2163#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE 31
2164#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE 32
2165#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE 33
2166#define SQLITE_FCNTL_LOCK_TIMEOUT 34
2167#define SQLITE_FCNTL_DATA_VERSION 35
2168
2169/* deprecated names */
2170#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
2171#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
2172#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
2173
2174
2175/*
2176** CAPI3REF: Mutex Handle
2177**
2178** The mutex module within SQLite defines [sqlite3_mutex] to be an
2179** abstract type for a mutex object. The SQLite core never looks
2180** at the internal representation of an [sqlite3_mutex]. It only
2181** deals with pointers to the [sqlite3_mutex] object.
2182**
2183** Mutexes are created using [sqlite3_mutex_alloc()].
2184*/
2185typedef struct sqlite3_mutex sqlite3_mutex;
2186
2187/*
2188** CAPI3REF: Loadable Extension Thunk
2189**
2190** A pointer to the opaque sqlite3_api_routines structure is passed as
2191** the third parameter to entry points of [loadable extensions]. This
2192** structure must be typedefed in order to work around compiler warnings
2193** on some platforms.
2194*/
2195typedef struct sqlite3_api_routines sqlite3_api_routines;
2196
2197/*
2198** CAPI3REF: OS Interface Object
2199**
2200** An instance of the sqlite3_vfs object defines the interface between
2201** the SQLite core and the underlying operating system. The "vfs"
2202** in the name of the object stands for "virtual file system". See
2203** the [VFS | VFS documentation] for further information.
2204**
2205** The VFS interface is sometimes extended by adding new methods onto
2206** the end. Each time such an extension occurs, the iVersion field
2207** is incremented. The iVersion value started out as 1 in
2208** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
2209** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
2210** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6]. Additional fields
2211** may be appended to the sqlite3_vfs object and the iVersion value
2212** may increase again in future versions of SQLite.
2213** Note that the structure
2214** of the sqlite3_vfs object changes in the transition from
2215** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
2216** and yet the iVersion field was not modified.
2217**
2218** The szOsFile field is the size of the subclassed [sqlite3_file]
2219** structure used by this VFS. mxPathname is the maximum length of
2220** a pathname in this VFS.
2221**
2222** Registered sqlite3_vfs objects are kept on a linked list formed by
2223** the pNext pointer. The [sqlite3_vfs_register()]
2224** and [sqlite3_vfs_unregister()] interfaces manage this list
2225** in a thread-safe way. The [sqlite3_vfs_find()] interface
2226** searches the list. Neither the application code nor the VFS
2227** implementation should use the pNext pointer.
2228**
2229** The pNext field is the only field in the sqlite3_vfs
2230** structure that SQLite will ever modify. SQLite will only access
2231** or modify this field while holding a particular static mutex.
2232** The application should never modify anything within the sqlite3_vfs
2233** object once the object has been registered.
2234**
2235** The zName field holds the name of the VFS module. The name must
2236** be unique across all VFS modules.
2237**
2238** [[sqlite3_vfs.xOpen]]
2239** ^SQLite guarantees that the zFilename parameter to xOpen
2240** is either a NULL pointer or string obtained
2241** from xFullPathname() with an optional suffix added.
2242** ^If a suffix is added to the zFilename parameter, it will
2243** consist of a single "-" character followed by no more than
2244** 11 alphanumeric and/or "-" characters.
2245** ^SQLite further guarantees that
2246** the string will be valid and unchanged until xClose() is
2247** called. Because of the previous sentence,
2248** the [sqlite3_file] can safely store a pointer to the
2249** filename if it needs to remember the filename for some reason.
2250** If the zFilename parameter to xOpen is a NULL pointer then xOpen
2251** must invent its own temporary name for the file. ^Whenever the
2252** xFilename parameter is NULL it will also be the case that the
2253** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
2254**
2255** The flags argument to xOpen() includes all bits set in
2256** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
2257** or [sqlite3_open16()] is used, then flags includes at least
2258** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
2259** If xOpen() opens a file read-only then it sets *pOutFlags to
2260** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set.
2261**
2262** ^(SQLite will also add one of the following flags to the xOpen()
2263** call, depending on the object being opened:
2264**
2265** <ul>
2266** <li> [SQLITE_OPEN_MAIN_DB]
2267** <li> [SQLITE_OPEN_MAIN_JOURNAL]
2268** <li> [SQLITE_OPEN_TEMP_DB]
2269** <li> [SQLITE_OPEN_TEMP_JOURNAL]
2270** <li> [SQLITE_OPEN_TRANSIENT_DB]
2271** <li> [SQLITE_OPEN_SUBJOURNAL]
2272** <li> [SQLITE_OPEN_MASTER_JOURNAL]
2273** <li> [SQLITE_OPEN_WAL]
2274** </ul>)^
2275**
2276** The file I/O implementation can use the object type flags to
2277** change the way it deals with files. For example, an application
2278** that does not care about crash recovery or rollback might make
2279** the open of a journal file a no-op. Writes to this journal would
2280** also be no-ops, and any attempt to read the journal would return
2281** SQLITE_IOERR. Or the implementation might recognize that a database
2282** file will be doing page-aligned sector reads and writes in a random
2283** order and set up its I/O subsystem accordingly.
2284**
2285** SQLite might also add one of the following flags to the xOpen method:
2286**
2287** <ul>
2288** <li> [SQLITE_OPEN_DELETEONCLOSE]
2289** <li> [SQLITE_OPEN_EXCLUSIVE]
2290** </ul>
2291**
2292** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
2293** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE]
2294** will be set for TEMP databases and their journals, transient
2295** databases, and subjournals.
2296**
2297** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
2298** with the [SQLITE_OPEN_CREATE] flag, which are both directly
2299** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
2300** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
2301** SQLITE_OPEN_CREATE, is used to indicate that file should always
2302** be created, and that it is an error if it already exists.
2303** It is <i>not</i> used to indicate the file should be opened
2304** for exclusive access.
2305**
2306** ^At least szOsFile bytes of memory are allocated by SQLite
2307** to hold the [sqlite3_file] structure passed as the third
2308** argument to xOpen. The xOpen method does not have to
2309** allocate the structure; it should just fill it in. Note that
2310** the xOpen method must set the sqlite3_file.pMethods to either
2311** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
2312** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
2313** element will be valid after xOpen returns regardless of the success
2314** or failure of the xOpen call.
2315**
2316** [[sqlite3_vfs.xAccess]]
2317** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
2318** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
2319** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
2320** to test whether a file is at least readable. The file can be a
2321** directory.
2322**
2323** ^SQLite will always allocate at least mxPathname+1 bytes for the
2324** output buffer xFullPathname. The exact size of the output buffer
2325** is also passed as a parameter to both methods. If the output buffer
2326** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
2327** handled as a fatal error by SQLite, vfs implementations should endeavor
2328** to prevent this by setting mxPathname to a sufficiently large value.
2329**
2330** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
2331** interfaces are not strictly a part of the filesystem, but they are
2332** included in the VFS structure for completeness.
2333** The xRandomness() function attempts to return nBytes bytes
2334** of good-quality randomness into zOut. The return value is
2335** the actual number of bytes of randomness obtained.
2336** The xSleep() method causes the calling thread to sleep for at
2337** least the number of microseconds given. ^The xCurrentTime()
2338** method returns a Julian Day Number for the current date and time as
2339** a floating point value.
2340** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
2341** Day Number multiplied by 86400000 (the number of milliseconds in
2342** a 24-hour day).
2343** ^SQLite will use the xCurrentTimeInt64() method to get the current
2344** date and time if that method is available (if iVersion is 2 or
2345** greater and the function pointer is not NULL) and will fall back
2346** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
2347**
2348** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
2349** are not used by the SQLite core. These optional interfaces are provided
2350** by some VFSes to facilitate testing of the VFS code. By overriding
2351** system calls with functions under its control, a test program can
2352** simulate faults and error conditions that would otherwise be difficult
2353** or impossible to induce. The set of system calls that can be overridden
2354** varies from one VFS to another, and from one version of the same VFS to the
2355** next. Applications that use these interfaces must be prepared for any
2356** or all of these interfaces to be NULL or for their behavior to change
2357** from one release to the next. Applications must not attempt to access
2358** any of these methods if the iVersion of the VFS is less than 3.
2359*/
2360typedef struct sqlite3_vfs sqlite3_vfs;
2361typedef void (*sqlite3_syscall_ptr)(void);
2362struct sqlite3_vfs {
2363 int iVersion; /* Structure version number (currently 3) */
2364 int szOsFile; /* Size of subclassed sqlite3_file */
2365 int mxPathname; /* Maximum file pathname length */
2366 sqlite3_vfs *pNext; /* Next registered VFS */
2367 const char *zName; /* Name of this virtual file system */
2368 void *pAppData; /* Pointer to application-specific data */
2369 int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
2370 int flags, int *pOutFlags);
2371 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
2372 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
2373 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
2374 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
2375 void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
2376 void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
2377 void (*xDlClose)(sqlite3_vfs*, void*);
2378 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
2379 int (*xSleep)(sqlite3_vfs*, int microseconds);
2380 int (*xCurrentTime)(sqlite3_vfs*, double*);
2381 int (*xGetLastError)(sqlite3_vfs*, int, char *);
2382 /*
2383 ** The methods above are in version 1 of the sqlite_vfs object
2384 ** definition. Those that follow are added in version 2 or later
2385 */
2386 int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
2387 /*
2388 ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
2389 ** Those below are for version 3 and greater.
2390 */
2391 int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
2392 sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
2393 const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
2394 /*
2395 ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
2396 ** New fields may be appended in future versions. The iVersion
2397 ** value will increment whenever this happens.
2398 */
2399};
2400
2401/*
2402** CAPI3REF: Flags for the xAccess VFS method
2403**
2404** These integer constants can be used as the third parameter to
2405** the xAccess method of an [sqlite3_vfs] object. They determine
2406** what kind of permissions the xAccess method is looking for.
2407** With SQLITE_ACCESS_EXISTS, the xAccess method
2408** simply checks whether the file exists.
2409** With SQLITE_ACCESS_READWRITE, the xAccess method
2410** checks whether the named directory is both readable and writable
2411** (in other words, if files can be added, removed, and renamed within
2412** the directory).
2413** The SQLITE_ACCESS_READWRITE constant is currently used only by the
2414** [temp_store_directory pragma], though this could change in a future
2415** release of SQLite.
2416** With SQLITE_ACCESS_READ, the xAccess method
2417** checks whether the file is readable. The SQLITE_ACCESS_READ constant is
2418** currently unused, though it might be used in a future release of
2419** SQLite.
2420*/
2421#define SQLITE_ACCESS_EXISTS 0
2422#define SQLITE_ACCESS_READWRITE 1 /* Used by PRAGMA temp_store_directory */
2423#define SQLITE_ACCESS_READ 2 /* Unused */
2424
2425/*
2426** CAPI3REF: Flags for the xShmLock VFS method
2427**
2428** These integer constants define the various locking operations
2429** allowed by the xShmLock method of [sqlite3_io_methods]. The
2430** following are the only legal combinations of flags to the
2431** xShmLock method:
2432**
2433** <ul>
2434** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
2435** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
2436** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
2437** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
2438** </ul>
2439**
2440** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
2441** was given on the corresponding lock.
2442**
2443** The xShmLock method can transition between unlocked and SHARED or
2444** between unlocked and EXCLUSIVE. It cannot transition between SHARED
2445** and EXCLUSIVE.
2446*/
2447#define SQLITE_SHM_UNLOCK 1
2448#define SQLITE_SHM_LOCK 2
2449#define SQLITE_SHM_SHARED 4
2450#define SQLITE_SHM_EXCLUSIVE 8
2451
2452/*
2453** CAPI3REF: Maximum xShmLock index
2454**
2455** The xShmLock method on [sqlite3_io_methods] may use values
2456** between 0 and this upper bound as its "offset" argument.
2457** The SQLite core will never attempt to acquire or release a
2458** lock outside of this range
2459*/
2460#define SQLITE_SHM_NLOCK 8
2461
2462
2463/*
2464** CAPI3REF: Initialize The SQLite Library
2465**
2466** ^The sqlite3_initialize() routine initializes the
2467** SQLite library. ^The sqlite3_shutdown() routine
2468** deallocates any resources that were allocated by sqlite3_initialize().
2469** These routines are designed to aid in process initialization and
2470** shutdown on embedded systems. Workstation applications using
2471** SQLite normally do not need to invoke either of these routines.
2472**
2473** A call to sqlite3_initialize() is an "effective" call if it is
2474** the first time sqlite3_initialize() is invoked during the lifetime of
2475** the process, or if it is the first time sqlite3_initialize() is invoked
2476** following a call to sqlite3_shutdown(). ^(Only an effective call
2477** of sqlite3_initialize() does any initialization. All other calls
2478** are harmless no-ops.)^
2479**
2480** A call to sqlite3_shutdown() is an "effective" call if it is the first
2481** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only
2482** an effective call to sqlite3_shutdown() does any deinitialization.
2483** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
2484**
2485** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
2486** is not. The sqlite3_shutdown() interface must only be called from a
2487** single thread. All open [database connections] must be closed and all
2488** other SQLite resources must be deallocated prior to invoking
2489** sqlite3_shutdown().
2490**
2491** Among other things, ^sqlite3_initialize() will invoke
2492** sqlite3_os_init(). Similarly, ^sqlite3_shutdown()
2493** will invoke sqlite3_os_end().
2494**
2495** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
2496** ^If for some reason, sqlite3_initialize() is unable to initialize
2497** the library (perhaps it is unable to allocate a needed resource such
2498** as a mutex) it returns an [error code] other than [SQLITE_OK].
2499**
2500** ^The sqlite3_initialize() routine is called internally by many other
2501** SQLite interfaces so that an application usually does not need to
2502** invoke sqlite3_initialize() directly. For example, [sqlite3_open()]
2503** calls sqlite3_initialize() so the SQLite library will be automatically
2504** initialized when [sqlite3_open()] is called if it has not be initialized
2505** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
2506** compile-time option, then the automatic calls to sqlite3_initialize()
2507** are omitted and the application must call sqlite3_initialize() directly
2508** prior to using any other SQLite interface. For maximum portability,
2509** it is recommended that applications always invoke sqlite3_initialize()
2510** directly prior to using any other SQLite interface. Future releases
2511** of SQLite may require this. In other words, the behavior exhibited
2512** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
2513** default behavior in some future release of SQLite.
2514**
2515** The sqlite3_os_init() routine does operating-system specific
2516** initialization of the SQLite library. The sqlite3_os_end()
2517** routine undoes the effect of sqlite3_os_init(). Typical tasks
2518** performed by these routines include allocation or deallocation
2519** of static resources, initialization of global variables,
2520** setting up a default [sqlite3_vfs] module, or setting up
2521** a default configuration using [sqlite3_config()].
2522**
2523** The application should never invoke either sqlite3_os_init()
2524** or sqlite3_os_end() directly. The application should only invoke
2525** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init()
2526** interface is called automatically by sqlite3_initialize() and
2527** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate
2528** implementations for sqlite3_os_init() and sqlite3_os_end()
2529** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
2530** When [custom builds | built for other platforms]
2531** (using the [SQLITE_OS_OTHER=1] compile-time
2532** option) the application must supply a suitable implementation for
2533** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
2534** implementation of sqlite3_os_init() or sqlite3_os_end()
2535** must return [SQLITE_OK] on success and some other [error code] upon
2536** failure.
2537*/
2538SQLITE_API int sqlite3_initialize(void);
2539SQLITE_API int sqlite3_shutdown(void);
2540SQLITE_API int sqlite3_os_init(void);
2541SQLITE_API int sqlite3_os_end(void);
2542
2543/*
2544** CAPI3REF: Configuring The SQLite Library
2545**
2546** The sqlite3_config() interface is used to make global configuration
2547** changes to SQLite in order to tune SQLite to the specific needs of
2548** the application. The default configuration is recommended for most
2549** applications and so this routine is usually not necessary. It is
2550** provided to support rare applications with unusual needs.
2551**
2552** <b>The sqlite3_config() interface is not threadsafe. The application
2553** must ensure that no other SQLite interfaces are invoked by other
2554** threads while sqlite3_config() is running.</b>
2555**
2556** The sqlite3_config() interface
2557** may only be invoked prior to library initialization using
2558** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
2559** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
2560** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
2561** Note, however, that ^sqlite3_config() can be called as part of the
2562** implementation of an application-defined [sqlite3_os_init()].
2563**
2564** The first argument to sqlite3_config() is an integer
2565** [configuration option] that determines
2566** what property of SQLite is to be configured. Subsequent arguments
2567** vary depending on the [configuration option]
2568** in the first argument.
2569**
2570** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
2571** ^If the option is unknown or SQLite is unable to set the option
2572** then this routine returns a non-zero [error code].
2573*/
2574SQLITE_API int sqlite3_config(int, ...);
2575
2576/*
2577** CAPI3REF: Configure database connections
2578** METHOD: sqlite3
2579**
2580** The sqlite3_db_config() interface is used to make configuration
2581** changes to a [database connection]. The interface is similar to
2582** [sqlite3_config()] except that the changes apply to a single
2583** [database connection] (specified in the first argument).
2584**
2585** The second argument to sqlite3_db_config(D,V,...) is the
2586** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2587** that indicates what aspect of the [database connection] is being configured.
2588** Subsequent arguments vary depending on the configuration verb.
2589**
2590** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
2591** the call is considered successful.
2592*/
2593SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
2594
2595/*
2596** CAPI3REF: Memory Allocation Routines
2597**
2598** An instance of this object defines the interface between SQLite
2599** and low-level memory allocation routines.
2600**
2601** This object is used in only one place in the SQLite interface.
2602** A pointer to an instance of this object is the argument to
2603** [sqlite3_config()] when the configuration option is
2604** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
2605** By creating an instance of this object
2606** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
2607** during configuration, an application can specify an alternative
2608** memory allocation subsystem for SQLite to use for all of its
2609** dynamic memory needs.
2610**
2611** Note that SQLite comes with several [built-in memory allocators]
2612** that are perfectly adequate for the overwhelming majority of applications
2613** and that this object is only useful to a tiny minority of applications
2614** with specialized memory allocation requirements. This object is
2615** also used during testing of SQLite in order to specify an alternative
2616** memory allocator that simulates memory out-of-memory conditions in
2617** order to verify that SQLite recovers gracefully from such
2618** conditions.
2619**
2620** The xMalloc, xRealloc, and xFree methods must work like the
2621** malloc(), realloc() and free() functions from the standard C library.
2622** ^SQLite guarantees that the second argument to
2623** xRealloc is always a value returned by a prior call to xRoundup.
2624**
2625** xSize should return the allocated size of a memory allocation
2626** previously obtained from xMalloc or xRealloc. The allocated size
2627** is always at least as big as the requested size but may be larger.
2628**
2629** The xRoundup method returns what would be the allocated size of
2630** a memory allocation given a particular requested size. Most memory
2631** allocators round up memory allocations at least to the next multiple
2632** of 8. Some allocators round up to a larger multiple or to a power of 2.
2633** Every memory allocation request coming in through [sqlite3_malloc()]
2634** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
2635** that causes the corresponding memory allocation to fail.
2636**
2637** The xInit method initializes the memory allocator. For example,
2638** it might allocate any require mutexes or initialize internal data
2639** structures. The xShutdown method is invoked (indirectly) by
2640** [sqlite3_shutdown()] and should deallocate any resources acquired
2641** by xInit. The pAppData pointer is used as the only parameter to
2642** xInit and xShutdown.
2643**
2644** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
2645** the xInit method, so the xInit method need not be threadsafe. The
2646** xShutdown method is only called from [sqlite3_shutdown()] so it does
2647** not need to be threadsafe either. For all other methods, SQLite
2648** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
2649** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
2650** it is by default) and so the methods are automatically serialized.
2651** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
2652** methods must be threadsafe or else make their own arrangements for
2653** serialization.
2654**
2655** SQLite will never invoke xInit() more than once without an intervening
2656** call to xShutdown().
2657*/
2658typedef struct sqlite3_mem_methods sqlite3_mem_methods;
2659struct sqlite3_mem_methods {
2660 void *(*xMalloc)(int); /* Memory allocation function */
2661 void (*xFree)(void*); /* Free a prior allocation */
2662 void *(*xRealloc)(void*,int); /* Resize an allocation */
2663 int (*xSize)(void*); /* Return the size of an allocation */
2664 int (*xRoundup)(int); /* Round up request size to allocation size */
2665 int (*xInit)(void*); /* Initialize the memory allocator */
2666 void (*xShutdown)(void*); /* Deinitialize the memory allocator */
2667 void *pAppData; /* Argument to xInit() and xShutdown() */
2668};
2669
2670/*
2671** CAPI3REF: Configuration Options
2672** KEYWORDS: {configuration option}
2673**
2674** These constants are the available integer configuration options that
2675** can be passed as the first argument to the [sqlite3_config()] interface.
2676**
2677** New configuration options may be added in future releases of SQLite.
2678** Existing configuration options might be discontinued. Applications
2679** should check the return code from [sqlite3_config()] to make sure that
2680** the call worked. The [sqlite3_config()] interface will return a
2681** non-zero [error code] if a discontinued or unsupported configuration option
2682** is invoked.
2683**
2684** <dl>
2685** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
2686** <dd>There are no arguments to this option. ^This option sets the
2687** [threading mode] to Single-thread. In other words, it disables
2688** all mutexing and puts SQLite into a mode where it can only be used
2689** by a single thread. ^If SQLite is compiled with
2690** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2691** it is not possible to change the [threading mode] from its default
2692** value of Single-thread and so [sqlite3_config()] will return
2693** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
2694** configuration option.</dd>
2695**
2696** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
2697** <dd>There are no arguments to this option. ^This option sets the
2698** [threading mode] to Multi-thread. In other words, it disables
2699** mutexing on [database connection] and [prepared statement] objects.
2700** The application is responsible for serializing access to
2701** [database connections] and [prepared statements]. But other mutexes
2702** are enabled so that SQLite will be safe to use in a multi-threaded
2703** environment as long as no two threads attempt to use the same
2704** [database connection] at the same time. ^If SQLite is compiled with
2705** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2706** it is not possible to set the Multi-thread [threading mode] and
2707** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2708** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
2709**
2710** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
2711** <dd>There are no arguments to this option. ^This option sets the
2712** [threading mode] to Serialized. In other words, this option enables
2713** all mutexes including the recursive
2714** mutexes on [database connection] and [prepared statement] objects.
2715** In this mode (which is the default when SQLite is compiled with
2716** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
2717** to [database connections] and [prepared statements] so that the
2718** application is free to use the same [database connection] or the
2719** same [prepared statement] in different threads at the same time.
2720** ^If SQLite is compiled with
2721** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2722** it is not possible to set the Serialized [threading mode] and
2723** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2724** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
2725**
2726** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
2727** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
2728** a pointer to an instance of the [sqlite3_mem_methods] structure.
2729** The argument specifies
2730** alternative low-level memory allocation routines to be used in place of
2731** the memory allocation routines built into SQLite.)^ ^SQLite makes
2732** its own private copy of the content of the [sqlite3_mem_methods] structure
2733** before the [sqlite3_config()] call returns.</dd>
2734**
2735** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
2736** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
2737** is a pointer to an instance of the [sqlite3_mem_methods] structure.
2738** The [sqlite3_mem_methods]
2739** structure is filled with the currently defined memory allocation routines.)^
2740** This option can be used to overload the default memory allocation
2741** routines with a wrapper that simulations memory allocation failure or
2742** tracks memory usage, for example. </dd>
2743**
2744** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
2745** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
2746** type int, interpreted as a boolean, which if true provides a hint to
2747** SQLite that it should avoid large memory allocations if possible.
2748** SQLite will run faster if it is free to make large memory allocations,
2749** but some application might prefer to run slower in exchange for
2750** guarantees about memory fragmentation that are possible if large
2751** allocations are avoided. This hint is normally off.
2752** </dd>
2753**
2754** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
2755** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
2756** interpreted as a boolean, which enables or disables the collection of
2757** memory allocation statistics. ^(When memory allocation statistics are
2758** disabled, the following SQLite interfaces become non-operational:
2759** <ul>
2760** <li> [sqlite3_memory_used()]
2761** <li> [sqlite3_memory_highwater()]
2762** <li> [sqlite3_soft_heap_limit64()]
2763** <li> [sqlite3_status64()]
2764** </ul>)^
2765** ^Memory allocation statistics are enabled by default unless SQLite is
2766** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
2767** allocation statistics are disabled by default.
2768** </dd>
2769**
2770** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
2771** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
2772** </dd>
2773**
2774** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
2775** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
2776** that SQLite can use for the database page cache with the default page
2777** cache implementation.
2778** This configuration option is a no-op if an application-define page
2779** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
2780** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
2781** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2782** and the number of cache lines (N).
2783** The sz argument should be the size of the largest database page
2784** (a power of two between 512 and 65536) plus some extra bytes for each
2785** page header. ^The number of extra bytes needed by the page header
2786** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ].
2787** ^It is harmless, apart from the wasted memory,
2788** for the sz parameter to be larger than necessary. The pMem
2789** argument must be either a NULL pointer or a pointer to an 8-byte
2790** aligned block of memory of at least sz*N bytes, otherwise
2791** subsequent behavior is undefined.
2792** ^When pMem is not NULL, SQLite will strive to use the memory provided
2793** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
2794** a page cache line is larger than sz bytes or if all of the pMem buffer
2795** is exhausted.
2796** ^If pMem is NULL and N is non-zero, then each database connection
2797** does an initial bulk allocation for page cache memory
2798** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or
2799** of -1024*N bytes if N is negative, . ^If additional
2800** page cache memory is needed beyond what is provided by the initial
2801** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
2802** additional cache line. </dd>
2803**
2804** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
2805** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
2806** that SQLite will use for all of its dynamic memory allocation needs
2807** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
2808** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
2809** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
2810** [SQLITE_ERROR] if invoked otherwise.
2811** ^There are three arguments to SQLITE_CONFIG_HEAP:
2812** An 8-byte aligned pointer to the memory,
2813** the number of bytes in the memory buffer, and the minimum allocation size.
2814** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
2815** to using its default memory allocator (the system malloc() implementation),
2816** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
2817** memory pointer is not NULL then the alternative memory
2818** allocator is engaged to handle all of SQLites memory allocation needs.
2819** The first pointer (the memory pointer) must be aligned to an 8-byte
2820** boundary or subsequent behavior of SQLite will be undefined.
2821** The minimum allocation size is capped at 2**12. Reasonable values
2822** for the minimum allocation size are 2**5 through 2**8.</dd>
2823**
2824** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
2825** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
2826** pointer to an instance of the [sqlite3_mutex_methods] structure.
2827** The argument specifies alternative low-level mutex routines to be used
2828** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of
2829** the content of the [sqlite3_mutex_methods] structure before the call to
2830** [sqlite3_config()] returns. ^If SQLite is compiled with
2831** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2832** the entire mutexing subsystem is omitted from the build and hence calls to
2833** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
2834** return [SQLITE_ERROR].</dd>
2835**
2836** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
2837** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
2838** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
2839** [sqlite3_mutex_methods]
2840** structure is filled with the currently defined mutex routines.)^
2841** This option can be used to overload the default mutex allocation
2842** routines with a wrapper used to track mutex usage for performance
2843** profiling or testing, for example. ^If SQLite is compiled with
2844** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2845** the entire mutexing subsystem is omitted from the build and hence calls to
2846** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
2847** return [SQLITE_ERROR].</dd>
2848**
2849** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
2850** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
2851** the default size of lookaside memory on each [database connection].
2852** The first argument is the
2853** size of each lookaside buffer slot and the second is the number of
2854** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
2855** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
2856** option to [sqlite3_db_config()] can be used to change the lookaside
2857** configuration on individual connections.)^ </dd>
2858**
2859** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
2860** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
2861** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
2862** the interface to a custom page cache implementation.)^
2863** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
2864**
2865** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
2866** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
2867** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of
2868** the current page cache implementation into that object.)^ </dd>
2869**
2870** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
2871** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
2872** global [error log].
2873** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2874** function with a call signature of void(*)(void*,int,const char*),
2875** and a pointer to void. ^If the function pointer is not NULL, it is
2876** invoked by [sqlite3_log()] to process each logging event. ^If the
2877** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2878** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
2879** passed through as the first parameter to the application-defined logger
2880** function whenever that function is invoked. ^The second parameter to
2881** the logger function is a copy of the first parameter to the corresponding
2882** [sqlite3_log()] call and is intended to be a [result code] or an
2883** [extended result code]. ^The third parameter passed to the logger is
2884** log message after formatting via [sqlite3_snprintf()].
2885** The SQLite logging interface is not reentrant; the logger function
2886** supplied by the application must not invoke any SQLite interface.
2887** In a multi-threaded application, the application-defined logger
2888** function must be threadsafe. </dd>
2889**
2890** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
2891** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
2892** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2893** then URI handling is globally disabled.)^ ^If URI handling is globally
2894** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()],
2895** [sqlite3_open16()] or
2896** specified as part of [ATTACH] commands are interpreted as URIs, regardless
2897** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
2898** connection is opened. ^If it is globally disabled, filenames are
2899** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
2900** database connection is opened. ^(By default, URI handling is globally
2901** disabled. The default value may be changed by compiling with the
2902** [SQLITE_USE_URI] symbol defined.)^
2903**
2904** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
2905** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
2906** argument which is interpreted as a boolean in order to enable or disable
2907** the use of covering indices for full table scans in the query optimizer.
2908** ^The default setting is determined
2909** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2910** if that compile-time option is omitted.
2911** The ability to disable the use of covering indices for full table scans
2912** is because some incorrectly coded legacy applications might malfunction
2913** when the optimization is enabled. Providing the ability to
2914** disable the optimization allows the older, buggy application code to work
2915** without change even with newer versions of SQLite.
2916**
2917** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2918** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
2919** <dd> These options are obsolete and should not be used by new code.
2920** They are retained for backwards compatibility but are now no-ops.
2921** </dd>
2922**
2923** [[SQLITE_CONFIG_SQLLOG]]
2924** <dt>SQLITE_CONFIG_SQLLOG
2925** <dd>This option is only available if sqlite is compiled with the
2926** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2927** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2928** The second should be of type (void*). The callback is invoked by the library
2929** in three separate circumstances, identified by the value passed as the
2930** fourth parameter. If the fourth parameter is 0, then the database connection
2931** passed as the second argument has just been opened. The third argument
2932** points to a buffer containing the name of the main database file. If the
2933** fourth parameter is 1, then the SQL statement that the third parameter
2934** points to has just been executed. Or, if the fourth parameter is 2, then
2935** the connection being passed as the second parameter is being closed. The
2936** third parameter is passed NULL In this case. An example of using this
2937** configuration option can be seen in the "test_sqllog.c" source file in
2938** the canonical SQLite source tree.</dd>
2939**
2940** [[SQLITE_CONFIG_MMAP_SIZE]]
2941** <dt>SQLITE_CONFIG_MMAP_SIZE
2942** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2943** that are the default mmap size limit (the default setting for
2944** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
2945** ^The default setting can be overridden by each database connection using
2946** either the [PRAGMA mmap_size] command, or by using the
2947** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
2948** will be silently truncated if necessary so that it does not exceed the
2949** compile-time maximum mmap size set by the
2950** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2951** ^If either argument to this option is negative, then that argument is
2952** changed to its compile-time default.
2953**
2954** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
2955** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
2956** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
2957** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2958** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2959** that specifies the maximum size of the created heap.
2960**
2961** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
2962** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
2963** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
2964** is a pointer to an integer and writes into that integer the number of extra
2965** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
2966** The amount of extra space required can change depending on the compiler,
2967** target platform, and SQLite version.
2968**
2969** [[SQLITE_CONFIG_PMASZ]]
2970** <dt>SQLITE_CONFIG_PMASZ
2971** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which
2972** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded
2973** sorter to that integer. The default minimum PMA Size is set by the
2974** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2975** to help with sort operations when multithreaded sorting
2976** is enabled (using the [PRAGMA threads] command) and the amount of content
2977** to be sorted exceeds the page size times the minimum of the
2978** [PRAGMA cache_size] setting and this value.
2979**
2980** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
2981** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
2982** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
2983** becomes the [statement journal] spill-to-disk threshold.
2984** [Statement journals] are held in memory until their size (in bytes)
2985** exceeds this threshold, at which point they are written to disk.
2986** Or if the threshold is -1, statement journals are always held
2987** exclusively in memory.
2988** Since many statement journals never become large, setting the spill
2989** threshold to a value such as 64KiB can greatly reduce the amount of
2990** I/O required to support statement rollback.
2991** The default value for this setting is controlled by the
2992** [SQLITE_STMTJRNL_SPILL] compile-time option.
2993**
2994** [[SQLITE_CONFIG_SORTERREF_SIZE]]
2995** <dt>SQLITE_CONFIG_SORTERREF_SIZE
2996** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
2997** of type (int) - the new value of the sorter-reference size threshold.
2998** Usually, when SQLite uses an external sort to order records according
2999** to an ORDER BY clause, all fields required by the caller are present in the
3000** sorted records. However, if SQLite determines based on the declared type
3001** of a table column that its values are likely to be very large - larger
3002** than the configured sorter-reference size threshold - then a reference
3003** is stored in each sorted record and the required column values loaded
3004** from the database as records are returned in sorted order. The default
3005** value for this option is to never use this optimization. Specifying a
3006** negative value for this option restores the default behaviour.
3007** This option is only available if SQLite is compiled with the
3008** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
3009** </dl>
3010*/
3011#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
3012#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
3013#define SQLITE_CONFIG_SERIALIZED 3 /* nil */
3014#define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
3015#define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */
3016#define SQLITE_CONFIG_SCRATCH 6 /* No longer used */
3017#define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
3018#define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
3019#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
3020#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
3021#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
3022/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
3023#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
3024#define SQLITE_CONFIG_PCACHE 14 /* no-op */
3025#define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
3026#define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
3027#define SQLITE_CONFIG_URI 17 /* int */
3028#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
3029#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
3030#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
3031#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
3032#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
3033#define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */
3034#define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */
3035#define SQLITE_CONFIG_PMASZ 25 /* unsigned int szPma */
3036#define SQLITE_CONFIG_STMTJRNL_SPILL 26 /* int nByte */
3037#define SQLITE_CONFIG_SMALL_MALLOC 27 /* boolean */
3038#define SQLITE_CONFIG_SORTERREF_SIZE 28 /* int nByte */
3039
3040/*
3041** CAPI3REF: Database Connection Configuration Options
3042**
3043** These constants are the available integer configuration options that
3044** can be passed as the second argument to the [sqlite3_db_config()] interface.
3045**
3046** New configuration options may be added in future releases of SQLite.
3047** Existing configuration options might be discontinued. Applications
3048** should check the return code from [sqlite3_db_config()] to make sure that
3049** the call worked. ^The [sqlite3_db_config()] interface will return a
3050** non-zero [error code] if a discontinued or unsupported configuration option
3051** is invoked.
3052**
3053** <dl>
3054** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
3055** <dd> ^This option takes three additional arguments that determine the
3056** [lookaside memory allocator] configuration for the [database connection].
3057** ^The first argument (the third parameter to [sqlite3_db_config()] is a
3058** pointer to a memory buffer to use for lookaside memory.
3059** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
3060** may be NULL in which case SQLite will allocate the
3061** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
3062** size of each lookaside buffer slot. ^The third argument is the number of
3063** slots. The size of the buffer in the first argument must be greater than
3064** or equal to the product of the second and third arguments. The buffer
3065** must be aligned to an 8-byte boundary. ^If the second argument to
3066** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
3067** rounded down to the next smaller multiple of 8. ^(The lookaside memory
3068** configuration for a database connection can only be changed when that
3069** connection is not currently using lookaside memory, or in other words
3070** when the "current value" returned by
3071** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
3072** Any attempt to change the lookaside memory configuration when lookaside
3073** memory is in use leaves the configuration unchanged and returns
3074** [SQLITE_BUSY].)^</dd>
3075**
3076** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
3077** <dd> ^This option is used to enable or disable the enforcement of
3078** [foreign key constraints]. There should be two additional arguments.
3079** The first argument is an integer which is 0 to disable FK enforcement,
3080** positive to enable FK enforcement or negative to leave FK enforcement
3081** unchanged. The second parameter is a pointer to an integer into which
3082** is written 0 or 1 to indicate whether FK enforcement is off or on
3083** following this call. The second parameter may be a NULL pointer, in
3084** which case the FK enforcement setting is not reported back. </dd>
3085**
3086** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
3087** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
3088** There should be two additional arguments.
3089** The first argument is an integer which is 0 to disable triggers,
3090** positive to enable triggers or negative to leave the setting unchanged.
3091** The second parameter is a pointer to an integer into which
3092** is written 0 or 1 to indicate whether triggers are disabled or enabled
3093** following this call. The second parameter may be a NULL pointer, in
3094** which case the trigger setting is not reported back. </dd>
3095**
3096** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
3097** <dd> ^This option is used to enable or disable the two-argument
3098** version of the [fts3_tokenizer()] function which is part of the
3099** [FTS3] full-text search engine extension.
3100** There should be two additional arguments.
3101** The first argument is an integer which is 0 to disable fts3_tokenizer() or
3102** positive to enable fts3_tokenizer() or negative to leave the setting
3103** unchanged.
3104** The second parameter is a pointer to an integer into which
3105** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
3106** following this call. The second parameter may be a NULL pointer, in
3107** which case the new setting is not reported back. </dd>
3108**
3109** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
3110** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
3111** interface independently of the [load_extension()] SQL function.
3112** The [sqlite3_enable_load_extension()] API enables or disables both the
3113** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
3114** There should be two additional arguments.
3115** When the first argument to this interface is 1, then only the C-API is
3116** enabled and the SQL function remains disabled. If the first argument to
3117** this interface is 0, then both the C-API and the SQL function are disabled.
3118** If the first argument is -1, then no changes are made to state of either the
3119** C-API or the SQL function.
3120** The second parameter is a pointer to an integer into which
3121** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
3122** is disabled or enabled following this call. The second parameter may
3123** be a NULL pointer, in which case the new setting is not reported back.
3124** </dd>
3125**
3126** <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
3127** <dd> ^This option is used to change the name of the "main" database
3128** schema. ^The sole argument is a pointer to a constant UTF8 string
3129** which will become the new schema name in place of "main". ^SQLite
3130** does not make a copy of the new main schema name string, so the application
3131** must ensure that the argument passed into this DBCONFIG option is unchanged
3132** until after the database connection closes.
3133** </dd>
3134**
3135** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
3136** <dd> Usually, when a database in wal mode is closed or detached from a
3137** database handle, SQLite checks if this will mean that there are now no
3138** connections at all to the database. If so, it performs a checkpoint
3139** operation before closing the connection. This option may be used to
3140** override this behaviour. The first parameter passed to this operation
3141** is an integer - positive to disable checkpoints-on-close, or zero (the
3142** default) to enable them, and negative to leave the setting unchanged.
3143** The second parameter is a pointer to an integer
3144** into which is written 0 or 1 to indicate whether checkpoints-on-close
3145** have been disabled - 0 if they are not disabled, 1 if they are.
3146** </dd>
3147**
3148** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
3149** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
3150** the [query planner stability guarantee] (QPSG). When the QPSG is active,
3151** a single SQL query statement will always use the same algorithm regardless
3152** of values of [bound parameters].)^ The QPSG disables some query optimizations
3153** that look at the values of bound parameters, which can make some queries
3154** slower. But the QPSG has the advantage of more predictable behavior. With
3155** the QPSG active, SQLite will always use the same query plan in the field as
3156** was used during testing in the lab.
3157** The first argument to this setting is an integer which is 0 to disable
3158** the QPSG, positive to enable QPSG, or negative to leave the setting
3159** unchanged. The second parameter is a pointer to an integer into which
3160** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
3161** following this call.
3162** </dd>
3163**
3164** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
3165** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not
3166** include output for any operations performed by trigger programs. This
3167** option is used to set or clear (the default) a flag that governs this
3168** behavior. The first parameter passed to this operation is an integer -
3169** positive to enable output for trigger programs, or zero to disable it,
3170** or negative to leave the setting unchanged.
3171** The second parameter is a pointer to an integer into which is written
3172** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
3173** it is not disabled, 1 if it is.
3174** </dd>
3175**
3176** <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
3177** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
3178** [VACUUM] in order to reset a database back to an empty database
3179** with no schema and no content. The following process works even for
3180** a badly corrupted database file:
3181** <ol>
3182** <li> If the database connection is newly opened, make sure it has read the
3183** database schema by preparing then discarding some query against the
3184** database, or calling sqlite3_table_column_metadata(), ignoring any
3185** errors. This step is only necessary if the application desires to keep
3186** the database in WAL mode after the reset if it was in WAL mode before
3187** the reset.
3188** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
3189** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
3190** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
3191** </ol>
3192** Because resetting a database is destructive and irreversible, the
3193** process requires the use of this obscure API and multiple steps to help
3194** ensure that it does not happen by accident.
3195** </dd>
3196** </dl>
3197*/
3198#define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */
3199#define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
3200#define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
3201#define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */
3202#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
3203#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
3204#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */
3205#define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */
3206#define SQLITE_DBCONFIG_TRIGGER_EQP 1008 /* int int* */
3207#define SQLITE_DBCONFIG_RESET_DATABASE 1009 /* int int* */
3208#define SQLITE_DBCONFIG_MAX 1009 /* Largest DBCONFIG */
3209
3210/*
3211** CAPI3REF: Enable Or Disable Extended Result Codes
3212** METHOD: sqlite3
3213**
3214** ^The sqlite3_extended_result_codes() routine enables or disables the
3215** [extended result codes] feature of SQLite. ^The extended result
3216** codes are disabled by default for historical compatibility.
3217*/
3218SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
3219
3220/*
3221** CAPI3REF: Last Insert Rowid
3222** METHOD: sqlite3
3223**
3224** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
3225** has a unique 64-bit signed
3226** integer key called the [ROWID | "rowid"]. ^The rowid is always available
3227** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
3228** names are not also used by explicitly declared columns. ^If
3229** the table has a column of type [INTEGER PRIMARY KEY] then that column
3230** is another alias for the rowid.
3231**
3232** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
3233** the most recent successful [INSERT] into a rowid table or [virtual table]
3234** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
3235** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
3236** on the database connection D, then sqlite3_last_insert_rowid(D) returns
3237** zero.
3238**
3239** As well as being set automatically as rows are inserted into database
3240** tables, the value returned by this function may be set explicitly by
3241** [sqlite3_set_last_insert_rowid()]
3242**
3243** Some virtual table implementations may INSERT rows into rowid tables as
3244** part of committing a transaction (e.g. to flush data accumulated in memory
3245** to disk). In this case subsequent calls to this function return the rowid
3246** associated with these internal INSERT operations, which leads to
3247** unintuitive results. Virtual table implementations that do write to rowid
3248** tables in this way can avoid this problem by restoring the original
3249** rowid value using [sqlite3_set_last_insert_rowid()] before returning
3250** control to the user.
3251**
3252** ^(If an [INSERT] occurs within a trigger then this routine will
3253** return the [rowid] of the inserted row as long as the trigger is
3254** running. Once the trigger program ends, the value returned
3255** by this routine reverts to what it was before the trigger was fired.)^
3256**
3257** ^An [INSERT] that fails due to a constraint violation is not a
3258** successful [INSERT] and does not change the value returned by this
3259** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
3260** and INSERT OR ABORT make no changes to the return value of this
3261** routine when their insertion fails. ^(When INSERT OR REPLACE
3262** encounters a constraint violation, it does not fail. The
3263** INSERT continues to completion after deleting rows that caused
3264** the constraint problem so INSERT OR REPLACE will always change
3265** the return value of this interface.)^
3266**
3267** ^For the purposes of this routine, an [INSERT] is considered to
3268** be successful even if it is subsequently rolled back.
3269**
3270** This function is accessible to SQL statements via the
3271** [last_insert_rowid() SQL function].
3272**
3273** If a separate thread performs a new [INSERT] on the same
3274** database connection while the [sqlite3_last_insert_rowid()]
3275** function is running and thus changes the last insert [rowid],
3276** then the value returned by [sqlite3_last_insert_rowid()] is
3277** unpredictable and might not equal either the old or the new
3278** last insert [rowid].
3279*/
3280SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
3281
3282/*
3283** CAPI3REF: Set the Last Insert Rowid value.
3284** METHOD: sqlite3
3285**
3286** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
3287** set the value returned by calling sqlite3_last_insert_rowid(D) to R
3288** without inserting a row into the database.
3289*/
3290SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
3291
3292/*
3293** CAPI3REF: Count The Number Of Rows Modified
3294** METHOD: sqlite3
3295**
3296** ^This function returns the number of rows modified, inserted or
3297** deleted by the most recently completed INSERT, UPDATE or DELETE
3298** statement on the database connection specified by the only parameter.
3299** ^Executing any other type of SQL statement does not modify the value
3300** returned by this function.
3301**
3302** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
3303** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
3304** [foreign key actions] or [REPLACE] constraint resolution are not counted.
3305**
3306** Changes to a view that are intercepted by
3307** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
3308** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
3309** DELETE statement run on a view is always zero. Only changes made to real
3310** tables are counted.
3311**
3312** Things are more complicated if the sqlite3_changes() function is
3313** executed while a trigger program is running. This may happen if the
3314** program uses the [changes() SQL function], or if some other callback
3315** function invokes sqlite3_changes() directly. Essentially:
3316**
3317** <ul>
3318** <li> ^(Before entering a trigger program the value returned by
3319** sqlite3_changes() function is saved. After the trigger program
3320** has finished, the original value is restored.)^
3321**
3322** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
3323** statement sets the value returned by sqlite3_changes()
3324** upon completion as normal. Of course, this value will not include
3325** any changes performed by sub-triggers, as the sqlite3_changes()
3326** value will be saved and restored after each sub-trigger has run.)^
3327** </ul>
3328**
3329** ^This means that if the changes() SQL function (or similar) is used
3330** by the first INSERT, UPDATE or DELETE statement within a trigger, it
3331** returns the value as set when the calling statement began executing.
3332** ^If it is used by the second or subsequent such statement within a trigger
3333** program, the value returned reflects the number of rows modified by the
3334** previous INSERT, UPDATE or DELETE statement within the same trigger.
3335**
3336** If a separate thread makes changes on the same database connection
3337** while [sqlite3_changes()] is running then the value returned
3338** is unpredictable and not meaningful.
3339**
3340** See also:
3341** <ul>
3342** <li> the [sqlite3_total_changes()] interface
3343** <li> the [count_changes pragma]
3344** <li> the [changes() SQL function]
3345** <li> the [data_version pragma]
3346** </ul>
3347*/
3348SQLITE_API int sqlite3_changes(sqlite3*);
3349
3350/*
3351** CAPI3REF: Total Number Of Rows Modified
3352** METHOD: sqlite3
3353**
3354** ^This function returns the total number of rows inserted, modified or
3355** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
3356** since the database connection was opened, including those executed as
3357** part of trigger programs. ^Executing any other type of SQL statement
3358** does not affect the value returned by sqlite3_total_changes().
3359**
3360** ^Changes made as part of [foreign key actions] are included in the
3361** count, but those made as part of REPLACE constraint resolution are
3362** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
3363** are not counted.
3364**
3365** This the [sqlite3_total_changes(D)] interface only reports the number
3366** of rows that changed due to SQL statement run against database
3367** connection D. Any changes by other database connections are ignored.
3368** To detect changes against a database file from other database
3369** connections use the [PRAGMA data_version] command or the
3370** [SQLITE_FCNTL_DATA_VERSION] [file control].
3371**
3372** If a separate thread makes changes on the same database connection
3373** while [sqlite3_total_changes()] is running then the value
3374** returned is unpredictable and not meaningful.
3375**
3376** See also:
3377** <ul>
3378** <li> the [sqlite3_changes()] interface
3379** <li> the [count_changes pragma]
3380** <li> the [changes() SQL function]
3381** <li> the [data_version pragma]
3382** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
3383** </ul>
3384*/
3385SQLITE_API int sqlite3_total_changes(sqlite3*);
3386
3387/*
3388** CAPI3REF: Interrupt A Long-Running Query
3389** METHOD: sqlite3
3390**
3391** ^This function causes any pending database operation to abort and
3392** return at its earliest opportunity. This routine is typically
3393** called in response to a user action such as pressing "Cancel"
3394** or Ctrl-C where the user wants a long query operation to halt
3395** immediately.
3396**
3397** ^It is safe to call this routine from a thread different from the
3398** thread that is currently running the database operation. But it
3399** is not safe to call this routine with a [database connection] that
3400** is closed or might close before sqlite3_interrupt() returns.
3401**
3402** ^If an SQL operation is very nearly finished at the time when
3403** sqlite3_interrupt() is called, then it might not have an opportunity
3404** to be interrupted and might continue to completion.
3405**
3406** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
3407** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
3408** that is inside an explicit transaction, then the entire transaction
3409** will be rolled back automatically.
3410**
3411** ^The sqlite3_interrupt(D) call is in effect until all currently running
3412** SQL statements on [database connection] D complete. ^Any new SQL statements
3413** that are started after the sqlite3_interrupt() call and before the
3414** running statements reaches zero are interrupted as if they had been
3415** running prior to the sqlite3_interrupt() call. ^New SQL statements
3416** that are started after the running statement count reaches zero are
3417** not effected by the sqlite3_interrupt().
3418** ^A call to sqlite3_interrupt(D) that occurs when there are no running
3419** SQL statements is a no-op and has no effect on SQL statements
3420** that are started after the sqlite3_interrupt() call returns.
3421*/
3422SQLITE_API void sqlite3_interrupt(sqlite3*);
3423
3424/*
3425** CAPI3REF: Determine If An SQL Statement Is Complete
3426**
3427** These routines are useful during command-line input to determine if the
3428** currently entered text seems to form a complete SQL statement or
3429** if additional input is needed before sending the text into
3430** SQLite for parsing. ^These routines return 1 if the input string
3431** appears to be a complete SQL statement. ^A statement is judged to be
3432** complete if it ends with a semicolon token and is not a prefix of a
3433** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3434** string literals or quoted identifier names or comments are not
3435** independent tokens (they are part of the token in which they are
3436** embedded) and thus do not count as a statement terminator. ^Whitespace
3437** and comments that follow the final semicolon are ignored.
3438**
3439** ^These routines return 0 if the statement is incomplete. ^If a
3440** memory allocation fails, then SQLITE_NOMEM is returned.
3441**
3442** ^These routines do not parse the SQL statements thus
3443** will not detect syntactically incorrect SQL.
3444**
3445** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
3446** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
3447** automatically by sqlite3_complete16(). If that initialization fails,
3448** then the return value from sqlite3_complete16() will be non-zero
3449** regardless of whether or not the input SQL is complete.)^
3450**
3451** The input to [sqlite3_complete()] must be a zero-terminated
3452** UTF-8 string.
3453**
3454** The input to [sqlite3_complete16()] must be a zero-terminated
3455** UTF-16 string in native byte order.
3456*/
3457SQLITE_API int sqlite3_complete(const char *sql);
3458SQLITE_API int sqlite3_complete16(const void *sql);
3459
3460/*
3461** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
3462** KEYWORDS: {busy-handler callback} {busy handler}
3463** METHOD: sqlite3
3464**
3465** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
3466** that might be invoked with argument P whenever
3467** an attempt is made to access a database table associated with
3468** [database connection] D when another thread
3469** or process has the table locked.
3470** The sqlite3_busy_handler() interface is used to implement
3471** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
3472**
3473** ^If the busy callback is NULL, then [SQLITE_BUSY]
3474** is returned immediately upon encountering the lock. ^If the busy callback
3475** is not NULL, then the callback might be invoked with two arguments.
3476**
3477** ^The first argument to the busy handler is a copy of the void* pointer which
3478** is the third argument to sqlite3_busy_handler(). ^The second argument to
3479** the busy handler callback is the number of times that the busy handler has
3480** been invoked previously for the same locking event. ^If the
3481** busy callback returns 0, then no additional attempts are made to
3482** access the database and [SQLITE_BUSY] is returned
3483** to the application.
3484** ^If the callback returns non-zero, then another attempt
3485** is made to access the database and the cycle repeats.
3486**
3487** The presence of a busy handler does not guarantee that it will be invoked
3488** when there is lock contention. ^If SQLite determines that invoking the busy
3489** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
3490** to the application instead of invoking the
3491** busy handler.
3492** Consider a scenario where one process is holding a read lock that
3493** it is trying to promote to a reserved lock and
3494** a second process is holding a reserved lock that it is trying
3495** to promote to an exclusive lock. The first process cannot proceed
3496** because it is blocked by the second and the second process cannot
3497** proceed because it is blocked by the first. If both processes
3498** invoke the busy handlers, neither will make any progress. Therefore,
3499** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
3500** will induce the first process to release its read lock and allow
3501** the second process to proceed.
3502**
3503** ^The default busy callback is NULL.
3504**
3505** ^(There can only be a single busy handler defined for each
3506** [database connection]. Setting a new busy handler clears any
3507** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
3508** or evaluating [PRAGMA busy_timeout=N] will change the
3509** busy handler and thus clear any previously set busy handler.
3510**
3511** The busy callback should not take any actions which modify the
3512** database connection that invoked the busy handler. In other words,
3513** the busy handler is not reentrant. Any such actions
3514** result in undefined behavior.
3515**
3516** A busy handler must not close the database connection
3517** or [prepared statement] that invoked the busy handler.
3518*/
3519SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
3520
3521/*
3522** CAPI3REF: Set A Busy Timeout
3523** METHOD: sqlite3
3524**
3525** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
3526** for a specified amount of time when a table is locked. ^The handler
3527** will sleep multiple times until at least "ms" milliseconds of sleeping
3528** have accumulated. ^After at least "ms" milliseconds of sleeping,
3529** the handler returns 0 which causes [sqlite3_step()] to return
3530** [SQLITE_BUSY].
3531**
3532** ^Calling this routine with an argument less than or equal to zero
3533** turns off all busy handlers.
3534**
3535** ^(There can only be a single busy handler for a particular
3536** [database connection] at any given moment. If another busy handler
3537** was defined (using [sqlite3_busy_handler()]) prior to calling
3538** this routine, that other busy handler is cleared.)^
3539**
3540** See also: [PRAGMA busy_timeout]
3541*/
3542SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3543
3544/*
3545** CAPI3REF: Convenience Routines For Running Queries
3546** METHOD: sqlite3
3547**
3548** This is a legacy interface that is preserved for backwards compatibility.
3549** Use of this interface is not recommended.
3550**
3551** Definition: A <b>result table</b> is memory data structure created by the
3552** [sqlite3_get_table()] interface. A result table records the
3553** complete query results from one or more queries.
3554**
3555** The table conceptually has a number of rows and columns. But
3556** these numbers are not part of the result table itself. These
3557** numbers are obtained separately. Let N be the number of rows
3558** and M be the number of columns.
3559**
3560** A result table is an array of pointers to zero-terminated UTF-8 strings.
3561** There are (N+1)*M elements in the array. The first M pointers point
3562** to zero-terminated strings that contain the names of the columns.
3563** The remaining entries all point to query results. NULL values result
3564** in NULL pointers. All other values are in their UTF-8 zero-terminated
3565** string representation as returned by [sqlite3_column_text()].
3566**
3567** A result table might consist of one or more memory allocations.
3568** It is not safe to pass a result table directly to [sqlite3_free()].
3569** A result table should be deallocated using [sqlite3_free_table()].
3570**
3571** ^(As an example of the result table format, suppose a query result
3572** is as follows:
3573**
3574** <blockquote><pre>
3575** Name | Age
3576** -----------------------
3577** Alice | 43
3578** Bob | 28
3579** Cindy | 21
3580** </pre></blockquote>
3581**
3582** There are two column (M==2) and three rows (N==3). Thus the
3583** result table has 8 entries. Suppose the result table is stored
3584** in an array names azResult. Then azResult holds this content:
3585**
3586** <blockquote><pre>
3587** azResult&#91;0] = "Name";
3588** azResult&#91;1] = "Age";
3589** azResult&#91;2] = "Alice";
3590** azResult&#91;3] = "43";
3591** azResult&#91;4] = "Bob";
3592** azResult&#91;5] = "28";
3593** azResult&#91;6] = "Cindy";
3594** azResult&#91;7] = "21";
3595** </pre></blockquote>)^
3596**
3597** ^The sqlite3_get_table() function evaluates one or more
3598** semicolon-separated SQL statements in the zero-terminated UTF-8
3599** string of its 2nd parameter and returns a result table to the
3600** pointer given in its 3rd parameter.
3601**
3602** After the application has finished with the result from sqlite3_get_table(),
3603** it must pass the result table pointer to sqlite3_free_table() in order to
3604** release the memory that was malloced. Because of the way the
3605** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
3606** function must not try to call [sqlite3_free()] directly. Only
3607** [sqlite3_free_table()] is able to release the memory properly and safely.
3608**
3609** The sqlite3_get_table() interface is implemented as a wrapper around
3610** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access
3611** to any internal data structures of SQLite. It uses only the public
3612** interface defined here. As a consequence, errors that occur in the
3613** wrapper layer outside of the internal [sqlite3_exec()] call are not
3614** reflected in subsequent calls to [sqlite3_errcode()] or
3615** [sqlite3_errmsg()].
3616*/
3617SQLITE_API int sqlite3_get_table(
3618 sqlite3 *db, /* An open database */
3619 const char *zSql, /* SQL to be evaluated */
3620 char ***pazResult, /* Results of the query */
3621 int *pnRow, /* Number of result rows written here */
3622 int *pnColumn, /* Number of result columns written here */
3623 char **pzErrmsg /* Error msg written here */
3624);
3625SQLITE_API void sqlite3_free_table(char **result);
3626
3627/*
3628** CAPI3REF: Formatted String Printing Functions
3629**
3630** These routines are work-alikes of the "printf()" family of functions
3631** from the standard C library.
3632** These routines understand most of the common formatting options from
3633** the standard library printf()
3634** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3635** See the [built-in printf()] documentation for details.
3636**
3637** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
3638** results into memory obtained from [sqlite3_malloc64()].
3639** The strings returned by these two routines should be
3640** released by [sqlite3_free()]. ^Both routines return a
3641** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
3642** memory to hold the resulting string.
3643**
3644** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
3645** the standard C library. The result is written into the
3646** buffer supplied as the second parameter whose size is given by
3647** the first parameter. Note that the order of the
3648** first two parameters is reversed from snprintf().)^ This is an
3649** historical accident that cannot be fixed without breaking
3650** backwards compatibility. ^(Note also that sqlite3_snprintf()
3651** returns a pointer to its buffer instead of the number of
3652** characters actually written into the buffer.)^ We admit that
3653** the number of characters written would be a more useful return
3654** value but we cannot change the implementation of sqlite3_snprintf()
3655** now without breaking compatibility.
3656**
3657** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
3658** guarantees that the buffer is always zero-terminated. ^The first
3659** parameter "n" is the total size of the buffer, including space for
3660** the zero terminator. So the longest string that can be completely
3661** written will be n-1 characters.
3662**
3663** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
3664**
3665** See also: [built-in printf()], [printf() SQL function]
3666*/
3667SQLITE_API char *sqlite3_mprintf(const char*,...);
3668SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
3669SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
3670SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
3671
3672/*
3673** CAPI3REF: Memory Allocation Subsystem
3674**
3675** The SQLite core uses these three routines for all of its own
3676** internal memory allocation needs. "Core" in the previous sentence
3677** does not include operating-system specific VFS implementation. The
3678** Windows VFS uses native malloc() and free() for some operations.
3679**
3680** ^The sqlite3_malloc() routine returns a pointer to a block
3681** of memory at least N bytes in length, where N is the parameter.
3682** ^If sqlite3_malloc() is unable to obtain sufficient free
3683** memory, it returns a NULL pointer. ^If the parameter N to
3684** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
3685** a NULL pointer.
3686**
3687** ^The sqlite3_malloc64(N) routine works just like
3688** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3689** of a signed 32-bit integer.
3690**
3691** ^Calling sqlite3_free() with a pointer previously returned
3692** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
3693** that it might be reused. ^The sqlite3_free() routine is
3694** a no-op if is called with a NULL pointer. Passing a NULL pointer
3695** to sqlite3_free() is harmless. After being freed, memory
3696** should neither be read nor written. Even reading previously freed
3697** memory might result in a segmentation fault or other severe error.
3698** Memory corruption, a segmentation fault, or other severe error
3699** might result if sqlite3_free() is called with a non-NULL pointer that
3700** was not obtained from sqlite3_malloc() or sqlite3_realloc().
3701**
3702** ^The sqlite3_realloc(X,N) interface attempts to resize a
3703** prior memory allocation X to be at least N bytes.
3704** ^If the X parameter to sqlite3_realloc(X,N)
3705** is a NULL pointer then its behavior is identical to calling
3706** sqlite3_malloc(N).
3707** ^If the N parameter to sqlite3_realloc(X,N) is zero or
3708** negative then the behavior is exactly the same as calling
3709** sqlite3_free(X).
3710** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
3711** of at least N bytes in size or NULL if insufficient memory is available.
3712** ^If M is the size of the prior allocation, then min(N,M) bytes
3713** of the prior allocation are copied into the beginning of buffer returned
3714** by sqlite3_realloc(X,N) and the prior allocation is freed.
3715** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
3716** prior allocation is not freed.
3717**
3718** ^The sqlite3_realloc64(X,N) interfaces works the same as
3719** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3720** of a 32-bit signed integer.
3721**
3722** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
3723** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
3724** sqlite3_msize(X) returns the size of that memory allocation in bytes.
3725** ^The value returned by sqlite3_msize(X) might be larger than the number
3726** of bytes requested when X was allocated. ^If X is a NULL pointer then
3727** sqlite3_msize(X) returns zero. If X points to something that is not
3728** the beginning of memory allocation, or if it points to a formerly
3729** valid memory allocation that has now been freed, then the behavior
3730** of sqlite3_msize(X) is undefined and possibly harmful.
3731**
3732** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
3733** sqlite3_malloc64(), and sqlite3_realloc64()
3734** is always aligned to at least an 8 byte boundary, or to a
3735** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3736** option is used.
3737**
3738** In SQLite version 3.5.0 and 3.5.1, it was possible to define
3739** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
3740** implementation of these routines to be omitted. That capability
3741** is no longer provided. Only built-in memory allocators can be used.
3742**
3743** Prior to SQLite version 3.7.10, the Windows OS interface layer called
3744** the system malloc() and free() directly when converting
3745** filenames between the UTF-8 encoding used by SQLite
3746** and whatever filename encoding is used by the particular Windows
3747** installation. Memory allocation errors were detected, but
3748** they were reported back as [SQLITE_CANTOPEN] or
3749** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
3750**
3751** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
3752** must be either NULL or else pointers obtained from a prior
3753** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
3754** not yet been released.
3755**
3756** The application must not read or write any part of
3757** a block of memory after it has been released using
3758** [sqlite3_free()] or [sqlite3_realloc()].
3759*/
3760SQLITE_API void *sqlite3_malloc(int);
3761SQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
3762SQLITE_API void *sqlite3_realloc(void*, int);
3763SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
3764SQLITE_API void sqlite3_free(void*);
3765SQLITE_API sqlite3_uint64 sqlite3_msize(void*);
3766
3767/*
3768** CAPI3REF: Memory Allocator Statistics
3769**
3770** SQLite provides these two interfaces for reporting on the status
3771** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
3772** routines, which form the built-in memory allocation subsystem.
3773**
3774** ^The [sqlite3_memory_used()] routine returns the number of bytes
3775** of memory currently outstanding (malloced but not freed).
3776** ^The [sqlite3_memory_highwater()] routine returns the maximum
3777** value of [sqlite3_memory_used()] since the high-water mark
3778** was last reset. ^The values returned by [sqlite3_memory_used()] and
3779** [sqlite3_memory_highwater()] include any overhead
3780** added by SQLite in its implementation of [sqlite3_malloc()],
3781** but not overhead added by the any underlying system library
3782** routines that [sqlite3_malloc()] may call.
3783**
3784** ^The memory high-water mark is reset to the current value of
3785** [sqlite3_memory_used()] if and only if the parameter to
3786** [sqlite3_memory_highwater()] is true. ^The value returned
3787** by [sqlite3_memory_highwater(1)] is the high-water mark
3788** prior to the reset.
3789*/
3790SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
3791SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
3792
3793/*
3794** CAPI3REF: Pseudo-Random Number Generator
3795**
3796** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3797** select random [ROWID | ROWIDs] when inserting new records into a table that
3798** already uses the largest possible [ROWID]. The PRNG is also used for
3799** the build-in random() and randomblob() SQL functions. This interface allows
3800** applications to access the same PRNG for other purposes.
3801**
3802** ^A call to this routine stores N bytes of randomness into buffer P.
3803** ^The P parameter can be a NULL pointer.
3804**
3805** ^If this routine has not been previously called or if the previous
3806** call had N less than one or a NULL pointer for P, then the PRNG is
3807** seeded using randomness obtained from the xRandomness method of
3808** the default [sqlite3_vfs] object.
3809** ^If the previous call to this routine had an N of 1 or more and a
3810** non-NULL P then the pseudo-randomness is generated
3811** internally and without recourse to the [sqlite3_vfs] xRandomness
3812** method.
3813*/
3814SQLITE_API void sqlite3_randomness(int N, void *P);
3815
3816/*
3817** CAPI3REF: Compile-Time Authorization Callbacks
3818** METHOD: sqlite3
3819** KEYWORDS: {authorizer callback}
3820**
3821** ^This routine registers an authorizer callback with a particular
3822** [database connection], supplied in the first argument.
3823** ^The authorizer callback is invoked as SQL statements are being compiled
3824** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
3825** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
3826** and [sqlite3_prepare16_v3()]. ^At various
3827** points during the compilation process, as logic is being created
3828** to perform various actions, the authorizer callback is invoked to
3829** see if those actions are allowed. ^The authorizer callback should
3830** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
3831** specific action but allow the SQL statement to continue to be
3832** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
3833** rejected with an error. ^If the authorizer callback returns
3834** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
3835** then the [sqlite3_prepare_v2()] or equivalent call that triggered
3836** the authorizer will fail with an error message.
3837**
3838** When the callback returns [SQLITE_OK], that means the operation
3839** requested is ok. ^When the callback returns [SQLITE_DENY], the
3840** [sqlite3_prepare_v2()] or equivalent call that triggered the
3841** authorizer will fail with an error message explaining that
3842** access is denied.
3843**
3844** ^The first parameter to the authorizer callback is a copy of the third
3845** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
3846** to the callback is an integer [SQLITE_COPY | action code] that specifies
3847** the particular action to be authorized. ^The third through sixth parameters
3848** to the callback are either NULL pointers or zero-terminated strings
3849** that contain additional details about the action to be authorized.
3850** Applications must always be prepared to encounter a NULL pointer in any
3851** of the third through the sixth parameters of the authorization callback.
3852**
3853** ^If the action code is [SQLITE_READ]
3854** and the callback returns [SQLITE_IGNORE] then the
3855** [prepared statement] statement is constructed to substitute
3856** a NULL value in place of the table column that would have
3857** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE]
3858** return can be used to deny an untrusted user access to individual
3859** columns of a table.
3860** ^When a table is referenced by a [SELECT] but no column values are
3861** extracted from that table (for example in a query like
3862** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
3863** is invoked once for that table with a column name that is an empty string.
3864** ^If the action code is [SQLITE_DELETE] and the callback returns
3865** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
3866** [truncate optimization] is disabled and all rows are deleted individually.
3867**
3868** An authorizer is used when [sqlite3_prepare | preparing]
3869** SQL statements from an untrusted source, to ensure that the SQL statements
3870** do not try to access data they are not allowed to see, or that they do not
3871** try to execute malicious statements that damage the database. For
3872** example, an application may allow a user to enter arbitrary
3873** SQL queries for evaluation by a database. But the application does
3874** not want the user to be able to make arbitrary changes to the
3875** database. An authorizer could then be put in place while the
3876** user-entered SQL is being [sqlite3_prepare | prepared] that
3877** disallows everything except [SELECT] statements.
3878**
3879** Applications that need to process SQL from untrusted sources
3880** might also consider lowering resource limits using [sqlite3_limit()]
3881** and limiting database size using the [max_page_count] [PRAGMA]
3882** in addition to using an authorizer.
3883**
3884** ^(Only a single authorizer can be in place on a database connection
3885** at a time. Each call to sqlite3_set_authorizer overrides the
3886** previous call.)^ ^Disable the authorizer by installing a NULL callback.
3887** The authorizer is disabled by default.
3888**
3889** The authorizer callback must not do anything that will modify
3890** the database connection that invoked the authorizer callback.
3891** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3892** database connections for the meaning of "modify" in this paragraph.
3893**
3894** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
3895** statement might be re-prepared during [sqlite3_step()] due to a
3896** schema change. Hence, the application should ensure that the
3897** correct authorizer callback remains in place during the [sqlite3_step()].
3898**
3899** ^Note that the authorizer callback is invoked only during
3900** [sqlite3_prepare()] or its variants. Authorization is not
3901** performed during statement evaluation in [sqlite3_step()], unless
3902** as stated in the previous paragraph, sqlite3_step() invokes
3903** sqlite3_prepare_v2() to reprepare a statement after a schema change.
3904*/
3905SQLITE_API int sqlite3_set_authorizer(
3906 sqlite3*,
3907 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
3908 void *pUserData
3909);
3910
3911/*
3912** CAPI3REF: Authorizer Return Codes
3913**
3914** The [sqlite3_set_authorizer | authorizer callback function] must
3915** return either [SQLITE_OK] or one of these two constants in order
3916** to signal SQLite whether or not the action is permitted. See the
3917** [sqlite3_set_authorizer | authorizer documentation] for additional
3918** information.
3919**
3920** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
3921** returned from the [sqlite3_vtab_on_conflict()] interface.
3922*/
3923#define SQLITE_DENY 1 /* Abort the SQL statement with an error */
3924#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
3925
3926/*
3927** CAPI3REF: Authorizer Action Codes
3928**
3929** The [sqlite3_set_authorizer()] interface registers a callback function
3930** that is invoked to authorize certain SQL statement actions. The
3931** second parameter to the callback is an integer code that specifies
3932** what action is being authorized. These are the integer action codes that
3933** the authorizer callback may be passed.
3934**
3935** These action code values signify what kind of operation is to be
3936** authorized. The 3rd and 4th parameters to the authorization
3937** callback function will be parameters or NULL depending on which of these
3938** codes is used as the second parameter. ^(The 5th parameter to the
3939** authorizer callback is the name of the database ("main", "temp",
3940** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback
3941** is the name of the inner-most trigger or view that is responsible for
3942** the access attempt or NULL if this access attempt is directly from
3943** top-level SQL code.
3944*/
3945/******************************************* 3rd ************ 4th ***********/
3946#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
3947#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
3948#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
3949#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
3950#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
3951#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
3952#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
3953#define SQLITE_CREATE_VIEW 8 /* View Name NULL */
3954#define SQLITE_DELETE 9 /* Table Name NULL */
3955#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
3956#define SQLITE_DROP_TABLE 11 /* Table Name NULL */
3957#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
3958#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
3959#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
3960#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
3961#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
3962#define SQLITE_DROP_VIEW 17 /* View Name NULL */
3963#define SQLITE_INSERT 18 /* Table Name NULL */
3964#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
3965#define SQLITE_READ 20 /* Table Name Column Name */
3966#define SQLITE_SELECT 21 /* NULL NULL */
3967#define SQLITE_TRANSACTION 22 /* Operation NULL */
3968#define SQLITE_UPDATE 23 /* Table Name Column Name */
3969#define SQLITE_ATTACH 24 /* Filename NULL */
3970#define SQLITE_DETACH 25 /* Database Name NULL */
3971#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
3972#define SQLITE_REINDEX 27 /* Index Name NULL */
3973#define SQLITE_ANALYZE 28 /* Table Name NULL */
3974#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
3975#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
3976#define SQLITE_FUNCTION 31 /* NULL Function Name */
3977#define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */
3978#define SQLITE_COPY 0 /* No longer used */
3979#define SQLITE_RECURSIVE 33 /* NULL NULL */
3980
3981/*
3982** CAPI3REF: Tracing And Profiling Functions
3983** METHOD: sqlite3
3984**
3985** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
3986** instead of the routines described here.
3987**
3988** These routines register callback functions that can be used for
3989** tracing and profiling the execution of SQL statements.
3990**
3991** ^The callback function registered by sqlite3_trace() is invoked at
3992** various times when an SQL statement is being run by [sqlite3_step()].
3993** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3994** SQL statement text as the statement first begins executing.
3995** ^(Additional sqlite3_trace() callbacks might occur
3996** as each triggered subprogram is entered. The callbacks for triggers
3997** contain a UTF-8 SQL comment that identifies the trigger.)^
3998**
3999** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
4000** the length of [bound parameter] expansion in the output of sqlite3_trace().
4001**
4002** ^The callback function registered by sqlite3_profile() is invoked
4003** as each SQL statement finishes. ^The profile callback contains
4004** the original statement text and an estimate of wall-clock time
4005** of how long that statement took to run. ^The profile callback
4006** time is in units of nanoseconds, however the current implementation
4007** is only capable of millisecond resolution so the six least significant
4008** digits in the time are meaningless. Future versions of SQLite
4009** might provide greater resolution on the profiler callback. The
4010** sqlite3_profile() function is considered experimental and is
4011** subject to change in future versions of SQLite.
4012*/
4013SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
4014 void(*xTrace)(void*,const char*), void*);
4015SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
4016 void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
4017
4018/*
4019** CAPI3REF: SQL Trace Event Codes
4020** KEYWORDS: SQLITE_TRACE
4021**
4022** These constants identify classes of events that can be monitored
4023** using the [sqlite3_trace_v2()] tracing logic. The M argument
4024** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
4025** the following constants. ^The first argument to the trace callback
4026** is one of the following constants.
4027**
4028** New tracing constants may be added in future releases.
4029**
4030** ^A trace callback has four arguments: xCallback(T,C,P,X).
4031** ^The T argument is one of the integer type codes above.
4032** ^The C argument is a copy of the context pointer passed in as the
4033** fourth argument to [sqlite3_trace_v2()].
4034** The P and X arguments are pointers whose meanings depend on T.
4035**
4036** <dl>
4037** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
4038** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
4039** first begins running and possibly at other times during the
4040** execution of the prepared statement, such as at the start of each
4041** trigger subprogram. ^The P argument is a pointer to the
4042** [prepared statement]. ^The X argument is a pointer to a string which
4043** is the unexpanded SQL text of the prepared statement or an SQL comment
4044** that indicates the invocation of a trigger. ^The callback can compute
4045** the same text that would have been returned by the legacy [sqlite3_trace()]
4046** interface by using the X argument when X begins with "--" and invoking
4047** [sqlite3_expanded_sql(P)] otherwise.
4048**
4049** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
4050** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
4051** information as is provided by the [sqlite3_profile()] callback.
4052** ^The P argument is a pointer to the [prepared statement] and the
4053** X argument points to a 64-bit integer which is the estimated of
4054** the number of nanosecond that the prepared statement took to run.
4055** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
4056**
4057** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
4058** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
4059** statement generates a single row of result.
4060** ^The P argument is a pointer to the [prepared statement] and the
4061** X argument is unused.
4062**
4063** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
4064** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
4065** connection closes.
4066** ^The P argument is a pointer to the [database connection] object
4067** and the X argument is unused.
4068** </dl>
4069*/
4070#define SQLITE_TRACE_STMT 0x01
4071#define SQLITE_TRACE_PROFILE 0x02
4072#define SQLITE_TRACE_ROW 0x04
4073#define SQLITE_TRACE_CLOSE 0x08
4074
4075/*
4076** CAPI3REF: SQL Trace Hook
4077** METHOD: sqlite3
4078**
4079** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
4080** function X against [database connection] D, using property mask M
4081** and context pointer P. ^If the X callback is
4082** NULL or if the M mask is zero, then tracing is disabled. The
4083** M argument should be the bitwise OR-ed combination of
4084** zero or more [SQLITE_TRACE] constants.
4085**
4086** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides
4087** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2().
4088**
4089** ^The X callback is invoked whenever any of the events identified by
4090** mask M occur. ^The integer return value from the callback is currently
4091** ignored, though this may change in future releases. Callback
4092** implementations should return zero to ensure future compatibility.
4093**
4094** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
4095** ^The T argument is one of the [SQLITE_TRACE]
4096** constants to indicate why the callback was invoked.
4097** ^The C argument is a copy of the context pointer.
4098** The P and X arguments are pointers whose meanings depend on T.
4099**
4100** The sqlite3_trace_v2() interface is intended to replace the legacy
4101** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
4102** are deprecated.
4103*/
4104SQLITE_API int sqlite3_trace_v2(
4105 sqlite3*,
4106 unsigned uMask,
4107 int(*xCallback)(unsigned,void*,void*,void*),
4108 void *pCtx
4109);
4110
4111/*
4112** CAPI3REF: Query Progress Callbacks
4113** METHOD: sqlite3
4114**
4115** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
4116** function X to be invoked periodically during long running calls to
4117** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
4118** database connection D. An example use for this
4119** interface is to keep a GUI updated during a large query.
4120**
4121** ^The parameter P is passed through as the only parameter to the
4122** callback function X. ^The parameter N is the approximate number of
4123** [virtual machine instructions] that are evaluated between successive
4124** invocations of the callback X. ^If N is less than one then the progress
4125** handler is disabled.
4126**
4127** ^Only a single progress handler may be defined at one time per
4128** [database connection]; setting a new progress handler cancels the
4129** old one. ^Setting parameter X to NULL disables the progress handler.
4130** ^The progress handler is also disabled by setting N to a value less
4131** than 1.
4132**
4133** ^If the progress callback returns non-zero, the operation is
4134** interrupted. This feature can be used to implement a
4135** "Cancel" button on a GUI progress dialog box.
4136**
4137** The progress handler callback must not do anything that will modify
4138** the database connection that invoked the progress handler.
4139** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4140** database connections for the meaning of "modify" in this paragraph.
4141**
4142*/
4143SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
4144
4145/*
4146** CAPI3REF: Opening A New Database Connection
4147** CONSTRUCTOR: sqlite3
4148**
4149** ^These routines open an SQLite database file as specified by the
4150** filename argument. ^The filename argument is interpreted as UTF-8 for
4151** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
4152** order for sqlite3_open16(). ^(A [database connection] handle is usually
4153** returned in *ppDb, even if an error occurs. The only exception is that
4154** if SQLite is unable to allocate memory to hold the [sqlite3] object,
4155** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
4156** object.)^ ^(If the database is opened (and/or created) successfully, then
4157** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
4158** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
4159** an English language description of the error following a failure of any
4160** of the sqlite3_open() routines.
4161**
4162** ^The default encoding will be UTF-8 for databases created using
4163** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases
4164** created using sqlite3_open16() will be UTF-16 in the native byte order.
4165**
4166** Whether or not an error occurs when it is opened, resources
4167** associated with the [database connection] handle should be released by
4168** passing it to [sqlite3_close()] when it is no longer required.
4169**
4170** The sqlite3_open_v2() interface works like sqlite3_open()
4171** except that it accepts two additional parameters for additional control
4172** over the new database connection. ^(The flags parameter to
4173** sqlite3_open_v2() can take one of
4174** the following three values, optionally combined with the
4175** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
4176** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
4177**
4178** <dl>
4179** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
4180** <dd>The database is opened in read-only mode. If the database does not
4181** already exist, an error is returned.</dd>)^
4182**
4183** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
4184** <dd>The database is opened for reading and writing if possible, or reading
4185** only if the file is write protected by the operating system. In either
4186** case the database must already exist, otherwise an error is returned.</dd>)^
4187**
4188** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
4189** <dd>The database is opened for reading and writing, and is created if
4190** it does not already exist. This is the behavior that is always used for
4191** sqlite3_open() and sqlite3_open16().</dd>)^
4192** </dl>
4193**
4194** If the 3rd parameter to sqlite3_open_v2() is not one of the
4195** combinations shown above optionally combined with other
4196** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
4197** then the behavior is undefined.
4198**
4199** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
4200** opens in the multi-thread [threading mode] as long as the single-thread
4201** mode has not been set at compile-time or start-time. ^If the
4202** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
4203** in the serialized [threading mode] unless single-thread was
4204** previously selected at compile-time or start-time.
4205** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
4206** eligible to use [shared cache mode], regardless of whether or not shared
4207** cache is enabled using [sqlite3_enable_shared_cache()]. ^The
4208** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
4209** participate in [shared cache mode] even if it is enabled.
4210**
4211** ^The fourth parameter to sqlite3_open_v2() is the name of the
4212** [sqlite3_vfs] object that defines the operating system interface that
4213** the new database connection should use. ^If the fourth parameter is
4214** a NULL pointer then the default [sqlite3_vfs] object is used.
4215**
4216** ^If the filename is ":memory:", then a private, temporary in-memory database
4217** is created for the connection. ^This in-memory database will vanish when
4218** the database connection is closed. Future versions of SQLite might
4219** make use of additional special filenames that begin with the ":" character.
4220** It is recommended that when a database filename actually does begin with
4221** a ":" character you should prefix the filename with a pathname such as
4222** "./" to avoid ambiguity.
4223**
4224** ^If the filename is an empty string, then a private, temporary
4225** on-disk database will be created. ^This private database will be
4226** automatically deleted as soon as the database connection is closed.
4227**
4228** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
4229**
4230** ^If [URI filename] interpretation is enabled, and the filename argument
4231** begins with "file:", then the filename is interpreted as a URI. ^URI
4232** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
4233** set in the third argument to sqlite3_open_v2(), or if it has
4234** been enabled globally using the [SQLITE_CONFIG_URI] option with the
4235** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
4236** URI filename interpretation is turned off
4237** by default, but future releases of SQLite might enable URI filename
4238** interpretation by default. See "[URI filenames]" for additional
4239** information.
4240**
4241** URI filenames are parsed according to RFC 3986. ^If the URI contains an
4242** authority, then it must be either an empty string or the string
4243** "localhost". ^If the authority is not an empty string or "localhost", an
4244** error is returned to the caller. ^The fragment component of a URI, if
4245** present, is ignored.
4246**
4247** ^SQLite uses the path component of the URI as the name of the disk file
4248** which contains the database. ^If the path begins with a '/' character,
4249** then it is interpreted as an absolute path. ^If the path does not begin
4250** with a '/' (meaning that the authority section is omitted from the URI)
4251** then the path is interpreted as a relative path.
4252** ^(On windows, the first component of an absolute path
4253** is a drive specification (e.g. "C:").)^
4254**
4255** [[core URI query parameters]]
4256** The query component of a URI may contain parameters that are interpreted
4257** either by SQLite itself, or by a [VFS | custom VFS implementation].
4258** SQLite and its built-in [VFSes] interpret the
4259** following query parameters:
4260**
4261** <ul>
4262** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
4263** a VFS object that provides the operating system interface that should
4264** be used to access the database file on disk. ^If this option is set to
4265** an empty string the default VFS object is used. ^Specifying an unknown
4266** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
4267** present, then the VFS specified by the option takes precedence over
4268** the value passed as the fourth parameter to sqlite3_open_v2().
4269**
4270** <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
4271** "rwc", or "memory". Attempting to set it to any other value is
4272** an error)^.
4273** ^If "ro" is specified, then the database is opened for read-only
4274** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
4275** third argument to sqlite3_open_v2(). ^If the mode option is set to
4276** "rw", then the database is opened for read-write (but not create)
4277** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
4278** been set. ^Value "rwc" is equivalent to setting both
4279** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If the mode option is
4280** set to "memory" then a pure [in-memory database] that never reads
4281** or writes from disk is used. ^It is an error to specify a value for
4282** the mode parameter that is less restrictive than that specified by
4283** the flags passed in the third parameter to sqlite3_open_v2().
4284**
4285** <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
4286** "private". ^Setting it to "shared" is equivalent to setting the
4287** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
4288** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
4289** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
4290** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
4291** a URI filename, its value overrides any behavior requested by setting
4292** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
4293**
4294** <li> <b>psow</b>: ^The psow parameter indicates whether or not the
4295** [powersafe overwrite] property does or does not apply to the
4296** storage media on which the database file resides.
4297**
4298** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
4299** which if set disables file locking in rollback journal modes. This
4300** is useful for accessing a database on a filesystem that does not
4301** support locking. Caution: Database corruption might result if two
4302** or more processes write to the same database and any one of those
4303** processes uses nolock=1.
4304**
4305** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
4306** parameter that indicates that the database file is stored on
4307** read-only media. ^When immutable is set, SQLite assumes that the
4308** database file cannot be changed, even by a process with higher
4309** privilege, and so the database is opened read-only and all locking
4310** and change detection is disabled. Caution: Setting the immutable
4311** property on a database file that does in fact change can result
4312** in incorrect query results and/or [SQLITE_CORRUPT] errors.
4313** See also: [SQLITE_IOCAP_IMMUTABLE].
4314**
4315** </ul>
4316**
4317** ^Specifying an unknown parameter in the query component of a URI is not an
4318** error. Future versions of SQLite might understand additional query
4319** parameters. See "[query parameters with special meaning to SQLite]" for
4320** additional information.
4321**
4322** [[URI filename examples]] <h3>URI filename examples</h3>
4323**
4324** <table border="1" align=center cellpadding=5>
4325** <tr><th> URI filenames <th> Results
4326** <tr><td> file:data.db <td>
4327** Open the file "data.db" in the current directory.
4328** <tr><td> file:/home/fred/data.db<br>
4329** file:///home/fred/data.db <br>
4330** file://localhost/home/fred/data.db <br> <td>
4331** Open the database file "/home/fred/data.db".
4332** <tr><td> file://darkstar/home/fred/data.db <td>
4333** An error. "darkstar" is not a recognized authority.
4334** <tr><td style="white-space:nowrap">
4335** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
4336** <td> Windows only: Open the file "data.db" on fred's desktop on drive
4337** C:. Note that the %20 escaping in this example is not strictly
4338** necessary - space characters can be used literally
4339** in URI filenames.
4340** <tr><td> file:data.db?mode=ro&cache=private <td>
4341** Open file "data.db" in the current directory for read-only access.
4342** Regardless of whether or not shared-cache mode is enabled by
4343** default, use a private cache.
4344** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4345** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4346** that uses dot-files in place of posix advisory locking.
4347** <tr><td> file:data.db?mode=readonly <td>
4348** An error. "readonly" is not a valid option for the "mode" parameter.
4349** </table>
4350**
4351** ^URI hexadecimal escape sequences (%HH) are supported within the path and
4352** query components of a URI. A hexadecimal escape sequence consists of a
4353** percent sign - "%" - followed by exactly two hexadecimal digits
4354** specifying an octet value. ^Before the path or query components of a
4355** URI filename are interpreted, they are encoded using UTF-8 and all
4356** hexadecimal escape sequences replaced by a single byte containing the
4357** corresponding octet. If this process generates an invalid UTF-8 encoding,
4358** the results are undefined.
4359**
4360** <b>Note to Windows users:</b> The encoding used for the filename argument
4361** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4362** codepage is currently defined. Filenames containing international
4363** characters must be converted to UTF-8 prior to passing them into
4364** sqlite3_open() or sqlite3_open_v2().
4365**
4366** <b>Note to Windows Runtime users:</b> The temporary directory must be set
4367** prior to calling sqlite3_open() or sqlite3_open_v2(). Otherwise, various
4368** features that require the use of temporary files may fail.
4369**
4370** See also: [sqlite3_temp_directory]
4371*/
4372SQLITE_API int sqlite3_open(
4373 const char *filename, /* Database filename (UTF-8) */
4374 sqlite3 **ppDb /* OUT: SQLite db handle */
4375);
4376SQLITE_API int sqlite3_open16(
4377 const void *filename, /* Database filename (UTF-16) */
4378 sqlite3 **ppDb /* OUT: SQLite db handle */
4379);
4380SQLITE_API int sqlite3_open_v2(
4381 const char *filename, /* Database filename (UTF-8) */
4382 sqlite3 **ppDb, /* OUT: SQLite db handle */
4383 int flags, /* Flags */
4384 const char *zVfs /* Name of VFS module to use */
4385);
4386
4387/*
4388** CAPI3REF: Obtain Values For URI Parameters
4389**
4390** These are utility routines, useful to VFS implementations, that check
4391** to see if a database file was a URI that contained a specific query
4392** parameter, and if so obtains the value of that query parameter.
4393**
4394** If F is the database filename pointer passed into the xOpen() method of
4395** a VFS implementation when the flags parameter to xOpen() has one or
4396** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and
4397** P is the name of the query parameter, then
4398** sqlite3_uri_parameter(F,P) returns the value of the P
4399** parameter if it exists or a NULL pointer if P does not appear as a
4400** query parameter on F. If P is a query parameter of F
4401** has no explicit value, then sqlite3_uri_parameter(F,P) returns
4402** a pointer to an empty string.
4403**
4404** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
4405** parameter and returns true (1) or false (0) according to the value
4406** of P. The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
4407** value of query parameter P is one of "yes", "true", or "on" in any
4408** case or if the value begins with a non-zero number. The
4409** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
4410** query parameter P is one of "no", "false", or "off" in any case or
4411** if the value begins with a numeric zero. If P is not a query
4412** parameter on F or if the value of P is does not match any of the
4413** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
4414**
4415** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
4416** 64-bit signed integer and returns that integer, or D if P does not
4417** exist. If the value of P is something other than an integer, then
4418** zero is returned.
4419**
4420** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
4421** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
4422** is not a database file pathname pointer that SQLite passed into the xOpen
4423** VFS method, then the behavior of this routine is undefined and probably
4424** undesirable.
4425*/
4426SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
4427SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
4428SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
4429
4430
4431/*
4432** CAPI3REF: Error Codes And Messages
4433** METHOD: sqlite3
4434**
4435** ^If the most recent sqlite3_* API call associated with
4436** [database connection] D failed, then the sqlite3_errcode(D) interface
4437** returns the numeric [result code] or [extended result code] for that
4438** API call.
4439** ^The sqlite3_extended_errcode()
4440** interface is the same except that it always returns the
4441** [extended result code] even when extended result codes are
4442** disabled.
4443**
4444** The values returned by sqlite3_errcode() and/or
4445** sqlite3_extended_errcode() might change with each API call.
4446** Except, there are some interfaces that are guaranteed to never
4447** change the value of the error code. The error-code preserving
4448** interfaces are:
4449**
4450** <ul>
4451** <li> sqlite3_errcode()
4452** <li> sqlite3_extended_errcode()
4453** <li> sqlite3_errmsg()
4454** <li> sqlite3_errmsg16()
4455** </ul>
4456**
4457** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4458** text that describes the error, as either UTF-8 or UTF-16 respectively.
4459** ^(Memory to hold the error message string is managed internally.
4460** The application does not need to worry about freeing the result.
4461** However, the error string might be overwritten or deallocated by
4462** subsequent calls to other SQLite interface functions.)^
4463**
4464** ^The sqlite3_errstr() interface returns the English-language text
4465** that describes the [result code], as UTF-8.
4466** ^(Memory to hold the error message string is managed internally
4467** and must not be freed by the application)^.
4468**
4469** When the serialized [threading mode] is in use, it might be the
4470** case that a second error occurs on a separate thread in between
4471** the time of the first error and the call to these interfaces.
4472** When that happens, the second error will be reported since these
4473** interfaces always report the most recent result. To avoid
4474** this, each thread can obtain exclusive use of the [database connection] D
4475** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
4476** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
4477** all calls to the interfaces listed here are completed.
4478**
4479** If an interface fails with SQLITE_MISUSE, that means the interface
4480** was invoked incorrectly by the application. In that case, the
4481** error code and message may or may not be set.
4482*/
4483SQLITE_API int sqlite3_errcode(sqlite3 *db);
4484SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
4485SQLITE_API const char *sqlite3_errmsg(sqlite3*);
4486SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
4487SQLITE_API const char *sqlite3_errstr(int);
4488
4489/*
4490** CAPI3REF: Prepared Statement Object
4491** KEYWORDS: {prepared statement} {prepared statements}
4492**
4493** An instance of this object represents a single SQL statement that
4494** has been compiled into binary form and is ready to be evaluated.
4495**
4496** Think of each SQL statement as a separate computer program. The
4497** original SQL text is source code. A prepared statement object
4498** is the compiled object code. All SQL must be converted into a
4499** prepared statement before it can be run.
4500**
4501** The life-cycle of a prepared statement object usually goes like this:
4502**
4503** <ol>
4504** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
4505** <li> Bind values to [parameters] using the sqlite3_bind_*()
4506** interfaces.
4507** <li> Run the SQL by calling [sqlite3_step()] one or more times.
4508** <li> Reset the prepared statement using [sqlite3_reset()] then go back
4509** to step 2. Do this zero or more times.
4510** <li> Destroy the object using [sqlite3_finalize()].
4511** </ol>
4512*/
4513typedef struct sqlite3_stmt sqlite3_stmt;
4514
4515/*
4516** CAPI3REF: Run-time Limits
4517** METHOD: sqlite3
4518**
4519** ^(This interface allows the size of various constructs to be limited
4520** on a connection by connection basis. The first parameter is the
4521** [database connection] whose limit is to be set or queried. The
4522** second parameter is one of the [limit categories] that define a
4523** class of constructs to be size limited. The third parameter is the
4524** new limit for that construct.)^
4525**
4526** ^If the new limit is a negative number, the limit is unchanged.
4527** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
4528** [limits | hard upper bound]
4529** set at compile-time by a C preprocessor macro called
4530** [limits | SQLITE_MAX_<i>NAME</i>].
4531** (The "_LIMIT_" in the name is changed to "_MAX_".))^
4532** ^Attempts to increase a limit above its hard upper bound are
4533** silently truncated to the hard upper bound.
4534**
4535** ^Regardless of whether or not the limit was changed, the
4536** [sqlite3_limit()] interface returns the prior value of the limit.
4537** ^Hence, to find the current value of a limit without changing it,
4538** simply invoke this interface with the third parameter set to -1.
4539**
4540** Run-time limits are intended for use in applications that manage
4541** both their own internal database and also databases that are controlled
4542** by untrusted external sources. An example application might be a
4543** web browser that has its own databases for storing history and
4544** separate databases controlled by JavaScript applications downloaded
4545** off the Internet. The internal databases can be given the
4546** large, default limits. Databases managed by external sources can
4547** be given much smaller limits designed to prevent a denial of service
4548** attack. Developers might also want to use the [sqlite3_set_authorizer()]
4549** interface to further control untrusted SQL. The size of the database
4550** created by an untrusted script can be contained using the
4551** [max_page_count] [PRAGMA].
4552**
4553** New run-time limit categories may be added in future releases.
4554*/
4555SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
4556
4557/*
4558** CAPI3REF: Run-Time Limit Categories
4559** KEYWORDS: {limit category} {*limit categories}
4560**
4561** These constants define various performance limits
4562** that can be lowered at run-time using [sqlite3_limit()].
4563** The synopsis of the meanings of the various limits is shown below.
4564** Additional information is available at [limits | Limits in SQLite].
4565**
4566** <dl>
4567** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
4568** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
4569**
4570** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
4571** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
4572**
4573** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
4574** <dd>The maximum number of columns in a table definition or in the
4575** result set of a [SELECT] or the maximum number of columns in an index
4576** or in an ORDER BY or GROUP BY clause.</dd>)^
4577**
4578** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
4579** <dd>The maximum depth of the parse tree on any expression.</dd>)^
4580**
4581** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
4582** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
4583**
4584** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
4585** <dd>The maximum number of instructions in a virtual machine program
4586** used to implement an SQL statement. If [sqlite3_prepare_v2()] or
4587** the equivalent tries to allocate space for more than this many opcodes
4588** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
4589**
4590** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
4591** <dd>The maximum number of arguments on a function.</dd>)^
4592**
4593** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
4594** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
4595**
4596** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
4597** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
4598** <dd>The maximum length of the pattern argument to the [LIKE] or
4599** [GLOB] operators.</dd>)^
4600**
4601** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
4602** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
4603** <dd>The maximum index number of any [parameter] in an SQL statement.)^
4604**
4605** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
4606** <dd>The maximum depth of recursion for triggers.</dd>)^
4607**
4608** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt>
4609** <dd>The maximum number of auxiliary worker threads that a single
4610** [prepared statement] may start.</dd>)^
4611** </dl>
4612*/
4613#define SQLITE_LIMIT_LENGTH 0
4614#define SQLITE_LIMIT_SQL_LENGTH 1
4615#define SQLITE_LIMIT_COLUMN 2
4616#define SQLITE_LIMIT_EXPR_DEPTH 3
4617#define SQLITE_LIMIT_COMPOUND_SELECT 4
4618#define SQLITE_LIMIT_VDBE_OP 5
4619#define SQLITE_LIMIT_FUNCTION_ARG 6
4620#define SQLITE_LIMIT_ATTACHED 7
4621#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8
4622#define SQLITE_LIMIT_VARIABLE_NUMBER 9
4623#define SQLITE_LIMIT_TRIGGER_DEPTH 10
4624#define SQLITE_LIMIT_WORKER_THREADS 11
4625
4626/*
4627** CAPI3REF: Prepare Flags
4628**
4629** These constants define various flags that can be passed into
4630** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
4631** [sqlite3_prepare16_v3()] interfaces.
4632**
4633** New flags may be added in future releases of SQLite.
4634**
4635** <dl>
4636** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
4637** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
4638** that the prepared statement will be retained for a long time and
4639** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
4640** and [sqlite3_prepare16_v3()] assume that the prepared statement will
4641** be used just once or at most a few times and then destroyed using
4642** [sqlite3_finalize()] relatively soon. The current implementation acts
4643** on this hint by avoiding the use of [lookaside memory] so as not to
4644** deplete the limited store of lookaside memory. Future versions of
4645** SQLite may act on this hint differently.
4646** </dl>
4647*/
4648#define SQLITE_PREPARE_PERSISTENT 0x01
4649
4650/*
4651** CAPI3REF: Compiling An SQL Statement
4652** KEYWORDS: {SQL statement compiler}
4653** METHOD: sqlite3
4654** CONSTRUCTOR: sqlite3_stmt
4655**
4656** To execute an SQL statement, it must first be compiled into a byte-code
4657** program using one of these routines. Or, in other words, these routines
4658** are constructors for the [prepared statement] object.
4659**
4660** The preferred routine to use is [sqlite3_prepare_v2()]. The
4661** [sqlite3_prepare()] interface is legacy and should be avoided.
4662** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
4663** for special purposes.
4664**
4665** The use of the UTF-8 interfaces is preferred, as SQLite currently
4666** does all parsing using UTF-8. The UTF-16 interfaces are provided
4667** as a convenience. The UTF-16 interfaces work by converting the
4668** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4669**
4670** The first argument, "db", is a [database connection] obtained from a
4671** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
4672** [sqlite3_open16()]. The database connection must not have been closed.
4673**
4674** The second argument, "zSql", is the statement to be compiled, encoded
4675** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4676** and sqlite3_prepare_v3()
4677** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4678** and sqlite3_prepare16_v3() use UTF-16.
4679**
4680** ^If the nByte argument is negative, then zSql is read up to the
4681** first zero terminator. ^If nByte is positive, then it is the
4682** number of bytes read from zSql. ^If nByte is zero, then no prepared
4683** statement is generated.
4684** If the caller knows that the supplied string is nul-terminated, then
4685** there is a small performance advantage to passing an nByte parameter that
4686** is the number of bytes in the input string <i>including</i>
4687** the nul-terminator.
4688**
4689** ^If pzTail is not NULL then *pzTail is made to point to the first byte
4690** past the end of the first SQL statement in zSql. These routines only
4691** compile the first statement in zSql, so *pzTail is left pointing to
4692** what remains uncompiled.
4693**
4694** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
4695** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set
4696** to NULL. ^If the input text contains no SQL (if the input is an empty
4697** string or a comment) then *ppStmt is set to NULL.
4698** The calling procedure is responsible for deleting the compiled
4699** SQL statement using [sqlite3_finalize()] after it has finished with it.
4700** ppStmt may not be NULL.
4701**
4702** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
4703** otherwise an [error code] is returned.
4704**
4705** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
4706** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
4707** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
4708** are retained for backwards compatibility, but their use is discouraged.
4709** ^In the "vX" interfaces, the prepared statement
4710** that is returned (the [sqlite3_stmt] object) contains a copy of the
4711** original SQL text. This causes the [sqlite3_step()] interface to
4712** behave differently in three ways:
4713**
4714** <ol>
4715** <li>
4716** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
4717** always used to do, [sqlite3_step()] will automatically recompile the SQL
4718** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
4719** retries will occur before sqlite3_step() gives up and returns an error.
4720** </li>
4721**
4722** <li>
4723** ^When an error occurs, [sqlite3_step()] will return one of the detailed
4724** [error codes] or [extended error codes]. ^The legacy behavior was that
4725** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
4726** and the application would have to make a second call to [sqlite3_reset()]
4727** in order to find the underlying cause of the problem. With the "v2" prepare
4728** interfaces, the underlying reason for the error is returned immediately.
4729** </li>
4730**
4731** <li>
4732** ^If the specific value bound to [parameter | host parameter] in the
4733** WHERE clause might influence the choice of query plan for a statement,
4734** then the statement will be automatically recompiled, as if there had been
4735** a schema change, on the first [sqlite3_step()] call following any change
4736** to the [sqlite3_bind_text | bindings] of that [parameter].
4737** ^The specific value of WHERE-clause [parameter] might influence the
4738** choice of query plan if the parameter is the left-hand side of a [LIKE]
4739** or [GLOB] operator or if the parameter is compared to an indexed column
4740** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
4741** </li>
4742** </ol>
4743**
4744** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
4745** the extra prepFlags parameter, which is a bit array consisting of zero or
4746** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
4747** sqlite3_prepare_v2() interface works exactly the same as
4748** sqlite3_prepare_v3() with a zero prepFlags parameter.
4749*/
4750SQLITE_API int sqlite3_prepare(
4751 sqlite3 *db, /* Database handle */
4752 const char *zSql, /* SQL statement, UTF-8 encoded */
4753 int nByte, /* Maximum length of zSql in bytes. */
4754 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4755 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4756);
4757SQLITE_API int sqlite3_prepare_v2(
4758 sqlite3 *db, /* Database handle */
4759 const char *zSql, /* SQL statement, UTF-8 encoded */
4760 int nByte, /* Maximum length of zSql in bytes. */
4761 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4762 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4763);
4764SQLITE_API int sqlite3_prepare_v3(
4765 sqlite3 *db, /* Database handle */
4766 const char *zSql, /* SQL statement, UTF-8 encoded */
4767 int nByte, /* Maximum length of zSql in bytes. */
4768 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4769 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4770 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4771);
4772SQLITE_API int sqlite3_prepare16(
4773 sqlite3 *db, /* Database handle */
4774 const void *zSql, /* SQL statement, UTF-16 encoded */
4775 int nByte, /* Maximum length of zSql in bytes. */
4776 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4777 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4778);
4779SQLITE_API int sqlite3_prepare16_v2(
4780 sqlite3 *db, /* Database handle */
4781 const void *zSql, /* SQL statement, UTF-16 encoded */
4782 int nByte, /* Maximum length of zSql in bytes. */
4783 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4784 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4785);
4786SQLITE_API int sqlite3_prepare16_v3(
4787 sqlite3 *db, /* Database handle */
4788 const void *zSql, /* SQL statement, UTF-16 encoded */
4789 int nByte, /* Maximum length of zSql in bytes. */
4790 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4791 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4792 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4793);
4794
4795/*
4796** CAPI3REF: Retrieving Statement SQL
4797** METHOD: sqlite3_stmt
4798**
4799** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4800** SQL text used to create [prepared statement] P if P was
4801** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
4802** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
4803** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4804** string containing the SQL text of prepared statement P with
4805** [bound parameters] expanded.
4806**
4807** ^(For example, if a prepared statement is created using the SQL
4808** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
4809** and parameter :xyz is unbound, then sqlite3_sql() will return
4810** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
4811** will return "SELECT 2345,NULL".)^
4812**
4813** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
4814** is available to hold the result, or if the result would exceed the
4815** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
4816**
4817** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4818** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4819** option causes sqlite3_expanded_sql() to always return NULL.
4820**
4821** ^The string returned by sqlite3_sql(P) is managed by SQLite and is
4822** automatically freed when the prepared statement is finalized.
4823** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
4824** is obtained from [sqlite3_malloc()] and must be free by the application
4825** by passing it to [sqlite3_free()].
4826*/
4827SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
4828SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
4829
4830/*
4831** CAPI3REF: Determine If An SQL Statement Writes The Database
4832** METHOD: sqlite3_stmt
4833**
4834** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4835** and only if the [prepared statement] X makes no direct changes to
4836** the content of the database file.
4837**
4838** Note that [application-defined SQL functions] or
4839** [virtual tables] might change the database indirectly as a side effect.
4840** ^(For example, if an application defines a function "eval()" that
4841** calls [sqlite3_exec()], then the following SQL statement would
4842** change the database file through side-effects:
4843**
4844** <blockquote><pre>
4845** SELECT eval('DELETE FROM t1') FROM t2;
4846** </pre></blockquote>
4847**
4848** But because the [SELECT] statement does not change the database file
4849** directly, sqlite3_stmt_readonly() would still return true.)^
4850**
4851** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
4852** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
4853** since the statements themselves do not actually modify the database but
4854** rather they control the timing of when other statements modify the
4855** database. ^The [ATTACH] and [DETACH] statements also cause
4856** sqlite3_stmt_readonly() to return true since, while those statements
4857** change the configuration of a database connection, they do not make
4858** changes to the content of the database files on disk.
4859** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
4860** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
4861** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
4862** sqlite3_stmt_readonly() returns false for those commands.
4863*/
4864SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
4865
4866/*
4867** CAPI3REF: Determine If A Prepared Statement Has Been Reset
4868** METHOD: sqlite3_stmt
4869**
4870** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4871** [prepared statement] S has been stepped at least once using
4872** [sqlite3_step(S)] but has neither run to completion (returned
4873** [SQLITE_DONE] from [sqlite3_step(S)]) nor
4874** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
4875** interface returns false if S is a NULL pointer. If S is not a
4876** NULL pointer and is not a pointer to a valid [prepared statement]
4877** object, then the behavior is undefined and probably undesirable.
4878**
4879** This interface can be used in combination [sqlite3_next_stmt()]
4880** to locate all prepared statements associated with a database
4881** connection that are in need of being reset. This can be used,
4882** for example, in diagnostic routines to search for prepared
4883** statements that are holding a transaction open.
4884*/
4885SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
4886
4887/*
4888** CAPI3REF: Dynamically Typed Value Object
4889** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
4890**
4891** SQLite uses the sqlite3_value object to represent all values
4892** that can be stored in a database table. SQLite uses dynamic typing
4893** for the values it stores. ^Values stored in sqlite3_value objects
4894** can be integers, floating point values, strings, BLOBs, or NULL.
4895**
4896** An sqlite3_value object may be either "protected" or "unprotected".
4897** Some interfaces require a protected sqlite3_value. Other interfaces
4898** will accept either a protected or an unprotected sqlite3_value.
4899** Every interface that accepts sqlite3_value arguments specifies
4900** whether or not it requires a protected sqlite3_value. The
4901** [sqlite3_value_dup()] interface can be used to construct a new
4902** protected sqlite3_value from an unprotected sqlite3_value.
4903**
4904** The terms "protected" and "unprotected" refer to whether or not
4905** a mutex is held. An internal mutex is held for a protected
4906** sqlite3_value object but no mutex is held for an unprotected
4907** sqlite3_value object. If SQLite is compiled to be single-threaded
4908** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
4909** or if SQLite is run in one of reduced mutex modes
4910** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
4911** then there is no distinction between protected and unprotected
4912** sqlite3_value objects and they can be used interchangeably. However,
4913** for maximum code portability it is recommended that applications
4914** still make the distinction between protected and unprotected
4915** sqlite3_value objects even when not strictly required.
4916**
4917** ^The sqlite3_value objects that are passed as parameters into the
4918** implementation of [application-defined SQL functions] are protected.
4919** ^The sqlite3_value object returned by
4920** [sqlite3_column_value()] is unprotected.
4921** Unprotected sqlite3_value objects may only be used as arguments
4922** to [sqlite3_result_value()], [sqlite3_bind_value()], and
4923** [sqlite3_value_dup()].
4924** The [sqlite3_value_blob | sqlite3_value_type()] family of
4925** interfaces require protected sqlite3_value objects.
4926*/
4927typedef struct sqlite3_value sqlite3_value;
4928
4929/*
4930** CAPI3REF: SQL Function Context Object
4931**
4932** The context in which an SQL function executes is stored in an
4933** sqlite3_context object. ^A pointer to an sqlite3_context object
4934** is always first parameter to [application-defined SQL functions].
4935** The application-defined SQL function implementation will pass this
4936** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
4937** [sqlite3_aggregate_context()], [sqlite3_user_data()],
4938** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
4939** and/or [sqlite3_set_auxdata()].
4940*/
4941typedef struct sqlite3_context sqlite3_context;
4942
4943/*
4944** CAPI3REF: Binding Values To Prepared Statements
4945** KEYWORDS: {host parameter} {host parameters} {host parameter name}
4946** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
4947** METHOD: sqlite3_stmt
4948**
4949** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
4950** literals may be replaced by a [parameter] that matches one of following
4951** templates:
4952**
4953** <ul>
4954** <li> ?
4955** <li> ?NNN
4956** <li> :VVV
4957** <li> @VVV
4958** <li> $VVV
4959** </ul>
4960**
4961** In the templates above, NNN represents an integer literal,
4962** and VVV represents an alphanumeric identifier.)^ ^The values of these
4963** parameters (also called "host parameter names" or "SQL parameters")
4964** can be set using the sqlite3_bind_*() routines defined here.
4965**
4966** ^The first argument to the sqlite3_bind_*() routines is always
4967** a pointer to the [sqlite3_stmt] object returned from
4968** [sqlite3_prepare_v2()] or its variants.
4969**
4970** ^The second argument is the index of the SQL parameter to be set.
4971** ^The leftmost SQL parameter has an index of 1. ^When the same named
4972** SQL parameter is used more than once, second and subsequent
4973** occurrences have the same index as the first occurrence.
4974** ^The index for named parameters can be looked up using the
4975** [sqlite3_bind_parameter_index()] API if desired. ^The index
4976** for "?NNN" parameters is the value of NNN.
4977** ^The NNN value must be between 1 and the [sqlite3_limit()]
4978** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
4979**
4980** ^The third argument is the value to bind to the parameter.
4981** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
4982** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
4983** is ignored and the end result is the same as sqlite3_bind_null().
4984**
4985** ^(In those routines that have a fourth argument, its value is the
4986** number of bytes in the parameter. To be clear: the value is the
4987** number of <u>bytes</u> in the value, not the number of characters.)^
4988** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
4989** is negative, then the length of the string is
4990** the number of bytes up to the first zero terminator.
4991** If the fourth parameter to sqlite3_bind_blob() is negative, then
4992** the behavior is undefined.
4993** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4994** or sqlite3_bind_text16() or sqlite3_bind_text64() then
4995** that parameter must be the byte offset
4996** where the NUL terminator would occur assuming the string were NUL
4997** terminated. If any NUL characters occur at byte offsets less than
4998** the value of the fourth parameter then the resulting string value will
4999** contain embedded NULs. The result of expressions involving strings
5000** with embedded NULs is undefined.
5001**
5002** ^The fifth argument to the BLOB and string binding interfaces
5003** is a destructor used to dispose of the BLOB or
5004** string after SQLite has finished with it. ^The destructor is called
5005** to dispose of the BLOB or string even if the call to bind API fails.
5006** ^If the fifth argument is
5007** the special value [SQLITE_STATIC], then SQLite assumes that the
5008** information is in static, unmanaged space and does not need to be freed.
5009** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
5010** SQLite makes its own private copy of the data immediately, before
5011** the sqlite3_bind_*() routine returns.
5012**
5013** ^The sixth argument to sqlite3_bind_text64() must be one of
5014** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
5015** to specify the encoding of the text in the third parameter. If
5016** the sixth argument to sqlite3_bind_text64() is not one of the
5017** allowed values shown above, or if the text encoding is different
5018** from the encoding specified by the sixth parameter, then the behavior
5019** is undefined.
5020**
5021** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
5022** is filled with zeroes. ^A zeroblob uses a fixed amount of memory
5023** (just an integer to hold its size) while it is being processed.
5024** Zeroblobs are intended to serve as placeholders for BLOBs whose
5025** content is later written using
5026** [sqlite3_blob_open | incremental BLOB I/O] routines.
5027** ^A negative value for the zeroblob results in a zero-length BLOB.
5028**
5029** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
5030** [prepared statement] S to have an SQL value of NULL, but to also be
5031** associated with the pointer P of type T. ^D is either a NULL pointer or
5032** a pointer to a destructor function for P. ^SQLite will invoke the
5033** destructor D with a single argument of P when it is finished using
5034** P. The T parameter should be a static string, preferably a string
5035** literal. The sqlite3_bind_pointer() routine is part of the
5036** [pointer passing interface] added for SQLite 3.20.0.
5037**
5038** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
5039** for the [prepared statement] or with a prepared statement for which
5040** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
5041** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_()
5042** routine is passed a [prepared statement] that has been finalized, the
5043** result is undefined and probably harmful.
5044**
5045** ^Bindings are not cleared by the [sqlite3_reset()] routine.
5046** ^Unbound parameters are interpreted as NULL.
5047**
5048** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
5049** [error code] if anything goes wrong.
5050** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
5051** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
5052** [SQLITE_MAX_LENGTH].
5053** ^[SQLITE_RANGE] is returned if the parameter
5054** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails.
5055**
5056** See also: [sqlite3_bind_parameter_count()],
5057** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
5058*/
5059SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
5060SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
5061 void(*)(void*));
5062SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
5063SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
5064SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
5065SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
5066SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
5067SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
5068SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
5069 void(*)(void*), unsigned char encoding);
5070SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
5071SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
5072SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
5073SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
5074
5075/*
5076** CAPI3REF: Number Of SQL Parameters
5077** METHOD: sqlite3_stmt
5078**
5079** ^This routine can be used to find the number of [SQL parameters]
5080** in a [prepared statement]. SQL parameters are tokens of the
5081** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
5082** placeholders for values that are [sqlite3_bind_blob | bound]
5083** to the parameters at a later time.
5084**
5085** ^(This routine actually returns the index of the largest (rightmost)
5086** parameter. For all forms except ?NNN, this will correspond to the
5087** number of unique parameters. If parameters of the ?NNN form are used,
5088** there may be gaps in the list.)^
5089**
5090** See also: [sqlite3_bind_blob|sqlite3_bind()],
5091** [sqlite3_bind_parameter_name()], and
5092** [sqlite3_bind_parameter_index()].
5093*/
5094SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
5095
5096/*
5097** CAPI3REF: Name Of A Host Parameter
5098** METHOD: sqlite3_stmt
5099**
5100** ^The sqlite3_bind_parameter_name(P,N) interface returns
5101** the name of the N-th [SQL parameter] in the [prepared statement] P.
5102** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
5103** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
5104** respectively.
5105** In other words, the initial ":" or "$" or "@" or "?"
5106** is included as part of the name.)^
5107** ^Parameters of the form "?" without a following integer have no name
5108** and are referred to as "nameless" or "anonymous parameters".
5109**
5110** ^The first host parameter has an index of 1, not 0.
5111**
5112** ^If the value N is out of range or if the N-th parameter is
5113** nameless, then NULL is returned. ^The returned string is
5114** always in UTF-8 encoding even if the named parameter was
5115** originally specified as UTF-16 in [sqlite3_prepare16()],
5116** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
5117**
5118** See also: [sqlite3_bind_blob|sqlite3_bind()],
5119** [sqlite3_bind_parameter_count()], and
5120** [sqlite3_bind_parameter_index()].
5121*/
5122SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
5123
5124/*
5125** CAPI3REF: Index Of A Parameter With A Given Name
5126** METHOD: sqlite3_stmt
5127**
5128** ^Return the index of an SQL parameter given its name. ^The
5129** index value returned is suitable for use as the second
5130** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero
5131** is returned if no matching parameter is found. ^The parameter
5132** name must be given in UTF-8 even if the original statement
5133** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5134** [sqlite3_prepare16_v3()].
5135**
5136** See also: [sqlite3_bind_blob|sqlite3_bind()],
5137** [sqlite3_bind_parameter_count()], and
5138** [sqlite3_bind_parameter_name()].
5139*/
5140SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
5141
5142/*
5143** CAPI3REF: Reset All Bindings On A Prepared Statement
5144** METHOD: sqlite3_stmt
5145**
5146** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
5147** the [sqlite3_bind_blob | bindings] on a [prepared statement].
5148** ^Use this routine to reset all host parameters to NULL.
5149*/
5150SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
5151
5152/*
5153** CAPI3REF: Number Of Columns In A Result Set
5154** METHOD: sqlite3_stmt
5155**
5156** ^Return the number of columns in the result set returned by the
5157** [prepared statement]. ^If this routine returns 0, that means the
5158** [prepared statement] returns no data (for example an [UPDATE]).
5159** ^However, just because this routine returns a positive number does not
5160** mean that one or more rows of data will be returned. ^A SELECT statement
5161** will always have a positive sqlite3_column_count() but depending on the
5162** WHERE clause constraints and the table content, it might return no rows.
5163**
5164** See also: [sqlite3_data_count()]
5165*/
5166SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
5167
5168/*
5169** CAPI3REF: Column Names In A Result Set
5170** METHOD: sqlite3_stmt
5171**
5172** ^These routines return the name assigned to a particular column
5173** in the result set of a [SELECT] statement. ^The sqlite3_column_name()
5174** interface returns a pointer to a zero-terminated UTF-8 string
5175** and sqlite3_column_name16() returns a pointer to a zero-terminated
5176** UTF-16 string. ^The first parameter is the [prepared statement]
5177** that implements the [SELECT] statement. ^The second parameter is the
5178** column number. ^The leftmost column is number 0.
5179**
5180** ^The returned string pointer is valid until either the [prepared statement]
5181** is destroyed by [sqlite3_finalize()] or until the statement is automatically
5182** reprepared by the first call to [sqlite3_step()] for a particular run
5183** or until the next call to
5184** sqlite3_column_name() or sqlite3_column_name16() on the same column.
5185**
5186** ^If sqlite3_malloc() fails during the processing of either routine
5187** (for example during a conversion from UTF-8 to UTF-16) then a
5188** NULL pointer is returned.
5189**
5190** ^The name of a result column is the value of the "AS" clause for
5191** that column, if there is an AS clause. If there is no AS clause
5192** then the name of the column is unspecified and may change from
5193** one release of SQLite to the next.
5194*/
5195SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
5196SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
5197
5198/*
5199** CAPI3REF: Source Of Data In A Query Result
5200** METHOD: sqlite3_stmt
5201**
5202** ^These routines provide a means to determine the database, table, and
5203** table column that is the origin of a particular result column in
5204** [SELECT] statement.
5205** ^The name of the database or table or column can be returned as
5206** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5207** the database name, the _table_ routines return the table name, and
5208** the origin_ routines return the column name.
5209** ^The returned string is valid until the [prepared statement] is destroyed
5210** using [sqlite3_finalize()] or until the statement is automatically
5211** reprepared by the first call to [sqlite3_step()] for a particular run
5212** or until the same information is requested
5213** again in a different encoding.
5214**
5215** ^The names returned are the original un-aliased names of the
5216** database, table, and column.
5217**
5218** ^The first argument to these interfaces is a [prepared statement].
5219** ^These functions return information about the Nth result column returned by
5220** the statement, where N is the second function argument.
5221** ^The left-most column is column 0 for these routines.
5222**
5223** ^If the Nth column returned by the statement is an expression or
5224** subquery and is not a column value, then all of these functions return
5225** NULL. ^These routine might also return NULL if a memory allocation error
5226** occurs. ^Otherwise, they return the name of the attached database, table,
5227** or column that query result column was extracted from.
5228**
5229** ^As with all other SQLite APIs, those whose names end with "16" return
5230** UTF-16 encoded strings and the other functions return UTF-8.
5231**
5232** ^These APIs are only available if the library was compiled with the
5233** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5234**
5235** If two or more threads call one or more of these routines against the same
5236** prepared statement and column at the same time then the results are
5237** undefined.
5238**
5239** If two or more threads call one or more
5240** [sqlite3_column_database_name | column metadata interfaces]
5241** for the same [prepared statement] and result column
5242** at the same time then the results are undefined.
5243*/
5244SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
5245SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
5246SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
5247SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
5248SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
5249SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
5250
5251/*
5252** CAPI3REF: Declared Datatype Of A Query Result
5253** METHOD: sqlite3_stmt
5254**
5255** ^(The first parameter is a [prepared statement].
5256** If this statement is a [SELECT] statement and the Nth column of the
5257** returned result set of that [SELECT] is a table column (not an
5258** expression or subquery) then the declared type of the table
5259** column is returned.)^ ^If the Nth column of the result set is an
5260** expression or subquery, then a NULL pointer is returned.
5261** ^The returned string is always UTF-8 encoded.
5262**
5263** ^(For example, given the database schema:
5264**
5265** CREATE TABLE t1(c1 VARIANT);
5266**
5267** and the following statement to be compiled:
5268**
5269** SELECT c1 + 1, c1 FROM t1;
5270**
5271** this routine would return the string "VARIANT" for the second result
5272** column (i==1), and a NULL pointer for the first result column (i==0).)^
5273**
5274** ^SQLite uses dynamic run-time typing. ^So just because a column
5275** is declared to contain a particular type does not mean that the
5276** data stored in that column is of the declared type. SQLite is
5277** strongly typed, but the typing is dynamic not static. ^Type
5278** is associated with individual values, not with the containers
5279** used to hold those values.
5280*/
5281SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
5282SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
5283
5284/*
5285** CAPI3REF: Evaluate An SQL Statement
5286** METHOD: sqlite3_stmt
5287**
5288** After a [prepared statement] has been prepared using any of
5289** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
5290** or [sqlite3_prepare16_v3()] or one of the legacy
5291** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
5292** must be called one or more times to evaluate the statement.
5293**
5294** The details of the behavior of the sqlite3_step() interface depend
5295** on whether the statement was prepared using the newer "vX" interfaces
5296** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
5297** [sqlite3_prepare16_v2()] or the older legacy
5298** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the
5299** new "vX" interface is recommended for new applications but the legacy
5300** interface will continue to be supported.
5301**
5302** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
5303** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
5304** ^With the "v2" interface, any of the other [result codes] or
5305** [extended result codes] might be returned as well.
5306**
5307** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
5308** database locks it needs to do its job. ^If the statement is a [COMMIT]
5309** or occurs outside of an explicit transaction, then you can retry the
5310** statement. If the statement is not a [COMMIT] and occurs within an
5311** explicit transaction then you should rollback the transaction before
5312** continuing.
5313**
5314** ^[SQLITE_DONE] means that the statement has finished executing
5315** successfully. sqlite3_step() should not be called again on this virtual
5316** machine without first calling [sqlite3_reset()] to reset the virtual
5317** machine back to its initial state.
5318**
5319** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
5320** is returned each time a new row of data is ready for processing by the
5321** caller. The values may be accessed using the [column access functions].
5322** sqlite3_step() is called again to retrieve the next row of data.
5323**
5324** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5325** violation) has occurred. sqlite3_step() should not be called again on
5326** the VM. More information may be found by calling [sqlite3_errmsg()].
5327** ^With the legacy interface, a more specific error code (for example,
5328** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
5329** can be obtained by calling [sqlite3_reset()] on the
5330** [prepared statement]. ^In the "v2" interface,
5331** the more specific error code is returned directly by sqlite3_step().
5332**
5333** [SQLITE_MISUSE] means that the this routine was called inappropriately.
5334** Perhaps it was called on a [prepared statement] that has
5335** already been [sqlite3_finalize | finalized] or on one that had
5336** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
5337** be the case that the same database connection is being used by two or
5338** more threads at the same moment in time.
5339**
5340** For all versions of SQLite up to and including 3.6.23.1, a call to
5341** [sqlite3_reset()] was required after sqlite3_step() returned anything
5342** other than [SQLITE_ROW] before any subsequent invocation of
5343** sqlite3_step(). Failure to reset the prepared statement using
5344** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
5345** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1],
5346** sqlite3_step() began
5347** calling [sqlite3_reset()] automatically in this circumstance rather
5348** than returning [SQLITE_MISUSE]. This is not considered a compatibility
5349** break because any application that ever receives an SQLITE_MISUSE error
5350** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5351** can be used to restore the legacy behavior.
5352**
5353** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
5354** API always returns a generic error code, [SQLITE_ERROR], following any
5355** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
5356** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
5357** specific [error codes] that better describes the error.
5358** We admit that this is a goofy design. The problem has been fixed
5359** with the "v2" interface. If you prepare all of your SQL statements
5360** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
5361** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
5362** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
5363** then the more specific [error codes] are returned directly
5364** by sqlite3_step(). The use of the "vX" interfaces is recommended.
5365*/
5366SQLITE_API int sqlite3_step(sqlite3_stmt*);
5367
5368/*
5369** CAPI3REF: Number of columns in a result set
5370** METHOD: sqlite3_stmt
5371**
5372** ^The sqlite3_data_count(P) interface returns the number of columns in the
5373** current row of the result set of [prepared statement] P.
5374** ^If prepared statement P does not have results ready to return
5375** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
5376** interfaces) then sqlite3_data_count(P) returns 0.
5377** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
5378** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
5379** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
5380** will return non-zero if previous call to [sqlite3_step](P) returned
5381** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
5382** where it always returns zero since each step of that multi-step
5383** pragma returns 0 columns of data.
5384**
5385** See also: [sqlite3_column_count()]
5386*/
5387SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5388
5389/*
5390** CAPI3REF: Fundamental Datatypes
5391** KEYWORDS: SQLITE_TEXT
5392**
5393** ^(Every value in SQLite has one of five fundamental datatypes:
5394**
5395** <ul>
5396** <li> 64-bit signed integer
5397** <li> 64-bit IEEE floating point number
5398** <li> string
5399** <li> BLOB
5400** <li> NULL
5401** </ul>)^
5402**
5403** These constants are codes for each of those types.
5404**
5405** Note that the SQLITE_TEXT constant was also used in SQLite version 2
5406** for a completely different meaning. Software that links against both
5407** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
5408** SQLITE_TEXT.
5409*/
5410#define SQLITE_INTEGER 1
5411#define SQLITE_FLOAT 2
5412#define SQLITE_BLOB 4
5413#define SQLITE_NULL 5
5414#ifdef SQLITE_TEXT
5415# undef SQLITE_TEXT
5416#else
5417# define SQLITE_TEXT 3
5418#endif
5419#define SQLITE3_TEXT 3
5420
5421/*
5422** CAPI3REF: Result Values From A Query
5423** KEYWORDS: {column access functions}
5424** METHOD: sqlite3_stmt
5425**
5426** <b>Summary:</b>
5427** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5428** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
5429** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
5430** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5431** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5432** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5433** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5434** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an
5435** [sqlite3_value|unprotected sqlite3_value] object.
5436** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5437** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
5438** or a UTF-8 TEXT result in bytes
5439** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
5440** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5441** TEXT in bytes
5442** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
5443** datatype of the result
5444** </table></blockquote>
5445**
5446** <b>Details:</b>
5447**
5448** ^These routines return information about a single column of the current
5449** result row of a query. ^In every case the first argument is a pointer
5450** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
5451** that was returned from [sqlite3_prepare_v2()] or one of its variants)
5452** and the second argument is the index of the column for which information
5453** should be returned. ^The leftmost column of the result set has the index 0.
5454** ^The number of columns in the result can be determined using
5455** [sqlite3_column_count()].
5456**
5457** If the SQL statement does not currently point to a valid row, or if the
5458** column index is out of range, the result is undefined.
5459** These routines may only be called when the most recent call to
5460** [sqlite3_step()] has returned [SQLITE_ROW] and neither
5461** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
5462** If any of these routines are called after [sqlite3_reset()] or
5463** [sqlite3_finalize()] or after [sqlite3_step()] has returned
5464** something other than [SQLITE_ROW], the results are undefined.
5465** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
5466** are called from a different thread while any of these routines
5467** are pending, then the results are undefined.
5468**
5469** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
5470** each return the value of a result column in a specific data format. If
5471** the result column is not initially in the requested format (for example,
5472** if the query returns an integer but the sqlite3_column_text() interface
5473** is used to extract the value) then an automatic type conversion is performed.
5474**
5475** ^The sqlite3_column_type() routine returns the
5476** [SQLITE_INTEGER | datatype code] for the initial data type
5477** of the result column. ^The returned value is one of [SQLITE_INTEGER],
5478** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
5479** The return value of sqlite3_column_type() can be used to decide which
5480** of the first six interface should be used to extract the column value.
5481** The value returned by sqlite3_column_type() is only meaningful if no
5482** automatic type conversions have occurred for the value in question.
5483** After a type conversion, the result of calling sqlite3_column_type()
5484** is undefined, though harmless. Future
5485** versions of SQLite may change the behavior of sqlite3_column_type()
5486** following a type conversion.
5487**
5488** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
5489** or sqlite3_column_bytes16() interfaces can be used to determine the size
5490** of that BLOB or string.
5491**
5492** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5493** routine returns the number of bytes in that BLOB or string.
5494** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5495** the string to UTF-8 and then returns the number of bytes.
5496** ^If the result is a numeric value then sqlite3_column_bytes() uses
5497** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5498** the number of bytes in that string.
5499** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
5500**
5501** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5502** routine returns the number of bytes in that BLOB or string.
5503** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5504** the string to UTF-16 and then returns the number of bytes.
5505** ^If the result is a numeric value then sqlite3_column_bytes16() uses
5506** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5507** the number of bytes in that string.
5508** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
5509**
5510** ^The values returned by [sqlite3_column_bytes()] and
5511** [sqlite3_column_bytes16()] do not include the zero terminators at the end
5512** of the string. ^For clarity: the values returned by
5513** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
5514** bytes in the string, not the number of characters.
5515**
5516** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
5517** even empty strings, are always zero-terminated. ^The return
5518** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5519**
5520** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
5521** [unprotected sqlite3_value] object. In a multithreaded environment,
5522** an unprotected sqlite3_value object may only be used safely with
5523** [sqlite3_bind_value()] and [sqlite3_result_value()].
5524** If the [unprotected sqlite3_value] object returned by
5525** [sqlite3_column_value()] is used in any other way, including calls
5526** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
5527** or [sqlite3_value_bytes()], the behavior is not threadsafe.
5528** Hence, the sqlite3_column_value() interface
5529** is normally only useful within the implementation of
5530** [application-defined SQL functions] or [virtual tables], not within
5531** top-level application code.
5532**
5533** The these routines may attempt to convert the datatype of the result.
5534** ^For example, if the internal representation is FLOAT and a text result
5535** is requested, [sqlite3_snprintf()] is used internally to perform the
5536** conversion automatically. ^(The following table details the conversions
5537** that are applied:
5538**
5539** <blockquote>
5540** <table border="1">
5541** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion
5542**
5543** <tr><td> NULL <td> INTEGER <td> Result is 0
5544** <tr><td> NULL <td> FLOAT <td> Result is 0.0
5545** <tr><td> NULL <td> TEXT <td> Result is a NULL pointer
5546** <tr><td> NULL <td> BLOB <td> Result is a NULL pointer
5547** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float
5548** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer
5549** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5550** <tr><td> FLOAT <td> INTEGER <td> [CAST] to INTEGER
5551** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float
5552** <tr><td> FLOAT <td> BLOB <td> [CAST] to BLOB
5553** <tr><td> TEXT <td> INTEGER <td> [CAST] to INTEGER
5554** <tr><td> TEXT <td> FLOAT <td> [CAST] to REAL
5555** <tr><td> TEXT <td> BLOB <td> No change
5556** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER
5557** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
5558** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
5559** </table>
5560** </blockquote>)^
5561**
5562** Note that when type conversions occur, pointers returned by prior
5563** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
5564** sqlite3_column_text16() may be invalidated.
5565** Type conversions and pointer invalidations might occur
5566** in the following cases:
5567**
5568** <ul>
5569** <li> The initial content is a BLOB and sqlite3_column_text() or
5570** sqlite3_column_text16() is called. A zero-terminator might
5571** need to be added to the string.</li>
5572** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5573** sqlite3_column_text16() is called. The content must be converted
5574** to UTF-16.</li>
5575** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5576** sqlite3_column_text() is called. The content must be converted
5577** to UTF-8.</li>
5578** </ul>
5579**
5580** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5581** not invalidate a prior pointer, though of course the content of the buffer
5582** that the prior pointer references will have been modified. Other kinds
5583** of conversion are done in place when it is possible, but sometimes they
5584** are not possible and in those cases prior pointers are invalidated.
5585**
5586** The safest policy is to invoke these routines
5587** in one of the following ways:
5588**
5589** <ul>
5590** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
5591** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
5592** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
5593** </ul>
5594**
5595** In other words, you should call sqlite3_column_text(),
5596** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
5597** into the desired format, then invoke sqlite3_column_bytes() or
5598** sqlite3_column_bytes16() to find the size of the result. Do not mix calls
5599** to sqlite3_column_text() or sqlite3_column_blob() with calls to
5600** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
5601** with calls to sqlite3_column_bytes().
5602**
5603** ^The pointers returned are valid until a type conversion occurs as
5604** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
5605** [sqlite3_finalize()] is called. ^The memory space used to hold strings
5606** and BLOBs is freed automatically. Do not pass the pointers returned
5607** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
5608** [sqlite3_free()].
5609**
5610** As long as the input parameters are correct, these routines will only
5611** fail if an out-of-memory error occurs during a format conversion.
5612** Only the following subset of interfaces are subject to out-of-memory
5613** errors:
5614**
5615** <ul>
5616** <li> sqlite3_column_blob()
5617** <li> sqlite3_column_text()
5618** <li> sqlite3_column_text16()
5619** <li> sqlite3_column_bytes()
5620** <li> sqlite3_column_bytes16()
5621** </ul>
5622**
5623** If an out-of-memory error occurs, then the return value from these
5624** routines is the same as if the column had contained an SQL NULL value.
5625** Valid SQL NULL returns can be distinguished from out-of-memory errors
5626** by invoking the [sqlite3_errcode()] immediately after the suspect
5627** return value is obtained and before any
5628** other SQLite interface is called on the same [database connection].
5629*/
5630SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
5631SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
5632SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
5633SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
5634SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
5635SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
5636SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
5637SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
5638SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
5639SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
5640
5641/*
5642** CAPI3REF: Destroy A Prepared Statement Object
5643** DESTRUCTOR: sqlite3_stmt
5644**
5645** ^The sqlite3_finalize() function is called to delete a [prepared statement].
5646** ^If the most recent evaluation of the statement encountered no errors
5647** or if the statement is never been evaluated, then sqlite3_finalize() returns
5648** SQLITE_OK. ^If the most recent evaluation of statement S failed, then
5649** sqlite3_finalize(S) returns the appropriate [error code] or
5650** [extended error code].
5651**
5652** ^The sqlite3_finalize(S) routine can be called at any point during
5653** the life cycle of [prepared statement] S:
5654** before statement S is ever evaluated, after
5655** one or more calls to [sqlite3_reset()], or after any call
5656** to [sqlite3_step()] regardless of whether or not the statement has
5657** completed execution.
5658**
5659** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5660**
5661** The application must finalize every [prepared statement] in order to avoid
5662** resource leaks. It is a grievous error for the application to try to use
5663** a prepared statement after it has been finalized. Any use of a prepared
5664** statement after it has been finalized can result in undefined and
5665** undesirable behavior such as segfaults and heap corruption.
5666*/
5667SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
5668
5669/*
5670** CAPI3REF: Reset A Prepared Statement Object
5671** METHOD: sqlite3_stmt
5672**
5673** The sqlite3_reset() function is called to reset a [prepared statement]
5674** object back to its initial state, ready to be re-executed.
5675** ^Any SQL statement variables that had values bound to them using
5676** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
5677** Use [sqlite3_clear_bindings()] to reset the bindings.
5678**
5679** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
5680** back to the beginning of its program.
5681**
5682** ^If the most recent call to [sqlite3_step(S)] for the
5683** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
5684** or if [sqlite3_step(S)] has never before been called on S,
5685** then [sqlite3_reset(S)] returns [SQLITE_OK].
5686**
5687** ^If the most recent call to [sqlite3_step(S)] for the
5688** [prepared statement] S indicated an error, then
5689** [sqlite3_reset(S)] returns an appropriate [error code].
5690**
5691** ^The [sqlite3_reset(S)] interface does not change the values
5692** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
5693*/
5694SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
5695
5696/*
5697** CAPI3REF: Create Or Redefine SQL Functions
5698** KEYWORDS: {function creation routines}
5699** KEYWORDS: {application-defined SQL function}
5700** KEYWORDS: {application-defined SQL functions}
5701** METHOD: sqlite3
5702**
5703** ^These functions (collectively known as "function creation routines")
5704** are used to add SQL functions or aggregates or to redefine the behavior
5705** of existing SQL functions or aggregates. The only differences between
5706** the three "sqlite3_create_function*" routines are the text encoding
5707** expected for the second parameter (the name of the function being
5708** created) and the presence or absence of a destructor callback for
5709** the application data pointer. Function sqlite3_create_window_function()
5710** is similar, but allows the user to supply the extra callback functions
5711** needed by [aggregate window functions].
5712**
5713** ^The first parameter is the [database connection] to which the SQL
5714** function is to be added. ^If an application uses more than one database
5715** connection then application-defined SQL functions must be added
5716** to each database connection separately.
5717**
5718** ^The second parameter is the name of the SQL function to be created or
5719** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5720** representation, exclusive of the zero-terminator. ^Note that the name
5721** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5722** ^Any attempt to create a function with a longer name
5723** will result in [SQLITE_MISUSE] being returned.
5724**
5725** ^The third parameter (nArg)
5726** is the number of arguments that the SQL function or
5727** aggregate takes. ^If this parameter is -1, then the SQL function or
5728** aggregate may take any number of arguments between 0 and the limit
5729** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third
5730** parameter is less than -1 or greater than 127 then the behavior is
5731** undefined.
5732**
5733** ^The fourth parameter, eTextRep, specifies what
5734** [SQLITE_UTF8 | text encoding] this SQL function prefers for
5735** its parameters. The application should set this parameter to
5736** [SQLITE_UTF16LE] if the function implementation invokes
5737** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
5738** implementation invokes [sqlite3_value_text16be()] on an input, or
5739** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
5740** otherwise. ^The same SQL function may be registered multiple times using
5741** different preferred text encodings, with different implementations for
5742** each encoding.
5743** ^When multiple implementations of the same function are available, SQLite
5744** will pick the one that involves the least amount of data conversion.
5745**
5746** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
5747** to signal that the function will always return the same result given
5748** the same inputs within a single SQL statement. Most SQL functions are
5749** deterministic. The built-in [random()] SQL function is an example of a
5750** function that is not deterministic. The SQLite query planner is able to
5751** perform additional optimizations on deterministic functions, so use
5752** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
5753**
5754** ^(The fifth parameter is an arbitrary pointer. The implementation of the
5755** function can gain access to this pointer using [sqlite3_user_data()].)^
5756**
5757** ^The sixth, seventh and eighth parameters passed to the three
5758** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
5759** pointers to C-language functions that implement the SQL function or
5760** aggregate. ^A scalar SQL function requires an implementation of the xFunc
5761** callback only; NULL pointers must be passed as the xStep and xFinal
5762** parameters. ^An aggregate SQL function requires an implementation of xStep
5763** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
5764** SQL function or aggregate, pass NULL pointers for all three function
5765** callbacks.
5766**
5767** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue
5768** and xInverse) passed to sqlite3_create_window_function are pointers to
5769** C-language callbacks that implement the new function. xStep and xFinal
5770** must both be non-NULL. xValue and xInverse may either both be NULL, in
5771** which case a regular aggregate function is created, or must both be
5772** non-NULL, in which case the new function may be used as either an aggregate
5773** or aggregate window function. More details regarding the implementation
5774** of aggregate window functions are
5775** [user-defined window functions|available here].
5776**
5777** ^(If the final parameter to sqlite3_create_function_v2() or
5778** sqlite3_create_window_function() is not NULL, then it is destructor for
5779** the application data pointer. The destructor is invoked when the function
5780** is deleted, either by being overloaded or when the database connection
5781** closes.)^ ^The destructor is also invoked if the call to
5782** sqlite3_create_function_v2() fails. ^When the destructor callback is
5783** invoked, it is passed a single argument which is a copy of the application
5784** data pointer which was the fifth parameter to sqlite3_create_function_v2().
5785**
5786** ^It is permitted to register multiple implementations of the same
5787** functions with the same name but with either differing numbers of
5788** arguments or differing preferred text encodings. ^SQLite will use
5789** the implementation that most closely matches the way in which the
5790** SQL function is used. ^A function implementation with a non-negative
5791** nArg parameter is a better match than a function implementation with
5792** a negative nArg. ^A function where the preferred text encoding
5793** matches the database encoding is a better
5794** match than a function where the encoding is different.
5795** ^A function where the encoding difference is between UTF16le and UTF16be
5796** is a closer match than a function where the encoding difference is
5797** between UTF8 and UTF16.
5798**
5799** ^Built-in functions may be overloaded by new application-defined functions.
5800**
5801** ^An application-defined function is permitted to call other
5802** SQLite interfaces. However, such calls must not
5803** close the database connection nor finalize or reset the prepared
5804** statement in which the function is running.
5805*/
5806SQLITE_API int sqlite3_create_function(
5807 sqlite3 *db,
5808 const char *zFunctionName,
5809 int nArg,
5810 int eTextRep,
5811 void *pApp,
5812 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5813 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5814 void (*xFinal)(sqlite3_context*)
5815);
5816SQLITE_API int sqlite3_create_function16(
5817 sqlite3 *db,
5818 const void *zFunctionName,
5819 int nArg,
5820 int eTextRep,
5821 void *pApp,
5822 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5823 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5824 void (*xFinal)(sqlite3_context*)
5825);
5826SQLITE_API int sqlite3_create_function_v2(
5827 sqlite3 *db,
5828 const char *zFunctionName,
5829 int nArg,
5830 int eTextRep,
5831 void *pApp,
5832 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5833 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5834 void (*xFinal)(sqlite3_context*),
5835 void(*xDestroy)(void*)
5836);
5837SQLITE_API int sqlite3_create_window_function(
5838 sqlite3 *db,
5839 const char *zFunctionName,
5840 int nArg,
5841 int eTextRep,
5842 void *pApp,
5843 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5844 void (*xFinal)(sqlite3_context*),
5845 void (*xValue)(sqlite3_context*),
5846 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
5847 void(*xDestroy)(void*)
5848);
5849
5850/*
5851** CAPI3REF: Text Encodings
5852**
5853** These constant define integer codes that represent the various
5854** text encodings supported by SQLite.
5855*/
5856#define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5857#define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5858#define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5859#define SQLITE_UTF16 4 /* Use native byte order */
5860#define SQLITE_ANY 5 /* Deprecated */
5861#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
5862
5863/*
5864** CAPI3REF: Function Flags
5865**
5866** These constants may be ORed together with the
5867** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
5868** to [sqlite3_create_function()], [sqlite3_create_function16()], or
5869** [sqlite3_create_function_v2()].
5870*/
5871#define SQLITE_DETERMINISTIC 0x800
5872
5873/*
5874** CAPI3REF: Deprecated Functions
5875** DEPRECATED
5876**
5877** These functions are [deprecated]. In order to maintain
5878** backwards compatibility with older code, these functions continue
5879** to be supported. However, new applications should avoid
5880** the use of these functions. To encourage programmers to avoid
5881** these functions, we will not explain what they do.
5882*/
5883#ifndef SQLITE_OMIT_DEPRECATED
5884SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
5885SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
5886SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
5887SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
5888SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
5889SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
5890 void*,sqlite3_int64);
5891#endif
5892
5893/*
5894** CAPI3REF: Obtaining SQL Values
5895** METHOD: sqlite3_value
5896**
5897** <b>Summary:</b>
5898** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5899** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
5900** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
5901** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
5902** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
5903** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
5904** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
5905** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
5906** the native byteorder
5907** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
5908** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
5909** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5910** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
5911** or a UTF-8 TEXT in bytes
5912** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
5913** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5914** TEXT in bytes
5915** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
5916** datatype of the value
5917** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
5918** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
5919** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
5920** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
5921** against a virtual table.
5922** </table></blockquote>
5923**
5924** <b>Details:</b>
5925**
5926** These routines extract type, size, and content information from
5927** [protected sqlite3_value] objects. Protected sqlite3_value objects
5928** are used to pass parameter information into implementation of
5929** [application-defined SQL functions] and [virtual tables].
5930**
5931** These routines work only with [protected sqlite3_value] objects.
5932** Any attempt to use these routines on an [unprotected sqlite3_value]
5933** is not threadsafe.
5934**
5935** ^These routines work just like the corresponding [column access functions]
5936** except that these routines take a single [protected sqlite3_value] object
5937** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
5938**
5939** ^The sqlite3_value_text16() interface extracts a UTF-16 string
5940** in the native byte-order of the host machine. ^The
5941** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
5942** extract UTF-16 strings as big-endian and little-endian respectively.
5943**
5944** ^If [sqlite3_value] object V was initialized
5945** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
5946** and if X and Y are strings that compare equal according to strcmp(X,Y),
5947** then sqlite3_value_pointer(V,Y) will return the pointer P. ^Otherwise,
5948** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer()
5949** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
5950**
5951** ^(The sqlite3_value_type(V) interface returns the
5952** [SQLITE_INTEGER | datatype code] for the initial datatype of the
5953** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
5954** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
5955** Other interfaces might change the datatype for an sqlite3_value object.
5956** For example, if the datatype is initially SQLITE_INTEGER and
5957** sqlite3_value_text(V) is called to extract a text value for that
5958** integer, then subsequent calls to sqlite3_value_type(V) might return
5959** SQLITE_TEXT. Whether or not a persistent internal datatype conversion
5960** occurs is undefined and may change from one release of SQLite to the next.
5961**
5962** ^(The sqlite3_value_numeric_type() interface attempts to apply
5963** numeric affinity to the value. This means that an attempt is
5964** made to convert the value to an integer or floating point. If
5965** such a conversion is possible without loss of information (in other
5966** words, if the value is a string that looks like a number)
5967** then the conversion is performed. Otherwise no conversion occurs.
5968** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
5969**
5970** ^Within the [xUpdate] method of a [virtual table], the
5971** sqlite3_value_nochange(X) interface returns true if and only if
5972** the column corresponding to X is unchanged by the UPDATE operation
5973** that the xUpdate method call was invoked to implement and if
5974** and the prior [xColumn] method call that was invoked to extracted
5975** the value for that column returned without setting a result (probably
5976** because it queried [sqlite3_vtab_nochange()] and found that the column
5977** was unchanging). ^Within an [xUpdate] method, any value for which
5978** sqlite3_value_nochange(X) is true will in all other respects appear
5979** to be a NULL value. If sqlite3_value_nochange(X) is invoked anywhere other
5980** than within an [xUpdate] method call for an UPDATE statement, then
5981** the return value is arbitrary and meaningless.
5982**
5983** Please pay particular attention to the fact that the pointer returned
5984** from [sqlite3_value_blob()], [sqlite3_value_text()], or
5985** [sqlite3_value_text16()] can be invalidated by a subsequent call to
5986** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
5987** or [sqlite3_value_text16()].
5988**
5989** These routines must be called from the same thread as
5990** the SQL function that supplied the [sqlite3_value*] parameters.
5991**
5992** As long as the input parameter is correct, these routines can only
5993** fail if an out-of-memory error occurs during a format conversion.
5994** Only the following subset of interfaces are subject to out-of-memory
5995** errors:
5996**
5997** <ul>
5998** <li> sqlite3_value_blob()
5999** <li> sqlite3_value_text()
6000** <li> sqlite3_value_text16()
6001** <li> sqlite3_value_text16le()
6002** <li> sqlite3_value_text16be()
6003** <li> sqlite3_value_bytes()
6004** <li> sqlite3_value_bytes16()
6005** </ul>
6006**
6007** If an out-of-memory error occurs, then the return value from these
6008** routines is the same as if the column had contained an SQL NULL value.
6009** Valid SQL NULL returns can be distinguished from out-of-memory errors
6010** by invoking the [sqlite3_errcode()] immediately after the suspect
6011** return value is obtained and before any
6012** other SQLite interface is called on the same [database connection].
6013*/
6014SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
6015SQLITE_API double sqlite3_value_double(sqlite3_value*);
6016SQLITE_API int sqlite3_value_int(sqlite3_value*);
6017SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
6018SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
6019SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
6020SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
6021SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
6022SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
6023SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
6024SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
6025SQLITE_API int sqlite3_value_type(sqlite3_value*);
6026SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
6027SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
6028
6029/*
6030** CAPI3REF: Finding The Subtype Of SQL Values
6031** METHOD: sqlite3_value
6032**
6033** The sqlite3_value_subtype(V) function returns the subtype for
6034** an [application-defined SQL function] argument V. The subtype
6035** information can be used to pass a limited amount of context from
6036** one SQL function to another. Use the [sqlite3_result_subtype()]
6037** routine to set the subtype for the return value of an SQL function.
6038*/
6039SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6040
6041/*
6042** CAPI3REF: Copy And Free SQL Values
6043** METHOD: sqlite3_value
6044**
6045** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
6046** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
6047** is a [protected sqlite3_value] object even if the input is not.
6048** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
6049** memory allocation fails.
6050**
6051** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
6052** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
6053** then sqlite3_value_free(V) is a harmless no-op.
6054*/
6055SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
6056SQLITE_API void sqlite3_value_free(sqlite3_value*);
6057
6058/*
6059** CAPI3REF: Obtain Aggregate Function Context
6060** METHOD: sqlite3_context
6061**
6062** Implementations of aggregate SQL functions use this
6063** routine to allocate memory for storing their state.
6064**
6065** ^The first time the sqlite3_aggregate_context(C,N) routine is called
6066** for a particular aggregate function, SQLite
6067** allocates N of memory, zeroes out that memory, and returns a pointer
6068** to the new memory. ^On second and subsequent calls to
6069** sqlite3_aggregate_context() for the same aggregate function instance,
6070** the same buffer is returned. Sqlite3_aggregate_context() is normally
6071** called once for each invocation of the xStep callback and then one
6072** last time when the xFinal callback is invoked. ^(When no rows match
6073** an aggregate query, the xStep() callback of the aggregate function
6074** implementation is never called and xFinal() is called exactly once.
6075** In those cases, sqlite3_aggregate_context() might be called for the
6076** first time from within xFinal().)^
6077**
6078** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
6079** when first called if N is less than or equal to zero or if a memory
6080** allocate error occurs.
6081**
6082** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
6083** determined by the N parameter on first successful call. Changing the
6084** value of N in subsequent call to sqlite3_aggregate_context() within
6085** the same aggregate function instance will not resize the memory
6086** allocation.)^ Within the xFinal callback, it is customary to set
6087** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
6088** pointless memory allocations occur.
6089**
6090** ^SQLite automatically frees the memory allocated by
6091** sqlite3_aggregate_context() when the aggregate query concludes.
6092**
6093** The first parameter must be a copy of the
6094** [sqlite3_context | SQL function context] that is the first parameter
6095** to the xStep or xFinal callback routine that implements the aggregate
6096** function.
6097**
6098** This routine must be called from the same thread in which
6099** the aggregate SQL function is running.
6100*/
6101SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
6102
6103/*
6104** CAPI3REF: User Data For Functions
6105** METHOD: sqlite3_context
6106**
6107** ^The sqlite3_user_data() interface returns a copy of
6108** the pointer that was the pUserData parameter (the 5th parameter)
6109** of the [sqlite3_create_function()]
6110** and [sqlite3_create_function16()] routines that originally
6111** registered the application defined function.
6112**
6113** This routine must be called from the same thread in which
6114** the application-defined function is running.
6115*/
6116SQLITE_API void *sqlite3_user_data(sqlite3_context*);
6117
6118/*
6119** CAPI3REF: Database Connection For Functions
6120** METHOD: sqlite3_context
6121**
6122** ^The sqlite3_context_db_handle() interface returns a copy of
6123** the pointer to the [database connection] (the 1st parameter)
6124** of the [sqlite3_create_function()]
6125** and [sqlite3_create_function16()] routines that originally
6126** registered the application defined function.
6127*/
6128SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
6129
6130/*
6131** CAPI3REF: Function Auxiliary Data
6132** METHOD: sqlite3_context
6133**
6134** These functions may be used by (non-aggregate) SQL functions to
6135** associate metadata with argument values. If the same value is passed to
6136** multiple invocations of the same SQL function during query execution, under
6137** some circumstances the associated metadata may be preserved. An example
6138** of where this might be useful is in a regular-expression matching
6139** function. The compiled version of the regular expression can be stored as
6140** metadata associated with the pattern string.
6141** Then as long as the pattern string remains the same,
6142** the compiled regular expression can be reused on multiple
6143** invocations of the same function.
6144**
6145** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
6146** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
6147** value to the application-defined function. ^N is zero for the left-most
6148** function argument. ^If there is no metadata
6149** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
6150** returns a NULL pointer.
6151**
6152** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6153** argument of the application-defined function. ^Subsequent
6154** calls to sqlite3_get_auxdata(C,N) return P from the most recent
6155** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
6156** NULL if the metadata has been discarded.
6157** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
6158** SQLite will invoke the destructor function X with parameter P exactly
6159** once, when the metadata is discarded.
6160** SQLite is free to discard the metadata at any time, including: <ul>
6161** <li> ^(when the corresponding function parameter changes)^, or
6162** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
6163** SQL statement)^, or
6164** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6165** parameter)^, or
6166** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6167** allocation error occurs.)^ </ul>
6168**
6169** Note the last bullet in particular. The destructor X in
6170** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6171** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6172** should be called near the end of the function implementation and the
6173** function implementation should not make any use of P after
6174** sqlite3_set_auxdata() has been called.
6175**
6176** ^(In practice, metadata is preserved between function calls for
6177** function parameters that are compile-time constants, including literal
6178** values and [parameters] and expressions composed from the same.)^
6179**
6180** The value of the N parameter to these interfaces should be non-negative.
6181** Future enhancements may make use of negative N values to define new
6182** kinds of function caching behavior.
6183**
6184** These routines must be called from the same thread in which
6185** the SQL function is running.
6186*/
6187SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
6188SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
6189
6190
6191/*
6192** CAPI3REF: Constants Defining Special Destructor Behavior
6193**
6194** These are special values for the destructor that is passed in as the
6195** final argument to routines like [sqlite3_result_blob()]. ^If the destructor
6196** argument is SQLITE_STATIC, it means that the content pointer is constant
6197** and will never change. It does not need to be destroyed. ^The
6198** SQLITE_TRANSIENT value means that the content will likely change in
6199** the near future and that SQLite should make its own private copy of
6200** the content before returning.
6201**
6202** The typedef is necessary to work around problems in certain
6203** C++ compilers.
6204*/
6205typedef void (*sqlite3_destructor_type)(void*);
6206#define SQLITE_STATIC ((sqlite3_destructor_type)0)
6207#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6208
6209/*
6210** CAPI3REF: Setting The Result Of An SQL Function
6211** METHOD: sqlite3_context
6212**
6213** These routines are used by the xFunc or xFinal callbacks that
6214** implement SQL functions and aggregates. See
6215** [sqlite3_create_function()] and [sqlite3_create_function16()]
6216** for additional information.
6217**
6218** These functions work very much like the [parameter binding] family of
6219** functions used to bind values to host parameters in prepared statements.
6220** Refer to the [SQL parameter] documentation for additional information.
6221**
6222** ^The sqlite3_result_blob() interface sets the result from
6223** an application-defined function to be the BLOB whose content is pointed
6224** to by the second parameter and which is N bytes long where N is the
6225** third parameter.
6226**
6227** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
6228** interfaces set the result of the application-defined function to be
6229** a BLOB containing all zero bytes and N bytes in size.
6230**
6231** ^The sqlite3_result_double() interface sets the result from
6232** an application-defined function to be a floating point value specified
6233** by its 2nd argument.
6234**
6235** ^The sqlite3_result_error() and sqlite3_result_error16() functions
6236** cause the implemented SQL function to throw an exception.
6237** ^SQLite uses the string pointed to by the
6238** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
6239** as the text of an error message. ^SQLite interprets the error
6240** message string from sqlite3_result_error() as UTF-8. ^SQLite
6241** interprets the string from sqlite3_result_error16() as UTF-16 in native
6242** byte order. ^If the third parameter to sqlite3_result_error()
6243** or sqlite3_result_error16() is negative then SQLite takes as the error
6244** message all text up through the first zero character.
6245** ^If the third parameter to sqlite3_result_error() or
6246** sqlite3_result_error16() is non-negative then SQLite takes that many
6247** bytes (not characters) from the 2nd parameter as the error message.
6248** ^The sqlite3_result_error() and sqlite3_result_error16()
6249** routines make a private copy of the error message text before
6250** they return. Hence, the calling function can deallocate or
6251** modify the text after they return without harm.
6252** ^The sqlite3_result_error_code() function changes the error code
6253** returned by SQLite as a result of an error in a function. ^By default,
6254** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error()
6255** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
6256**
6257** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
6258** error indicating that a string or BLOB is too long to represent.
6259**
6260** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
6261** error indicating that a memory allocation failed.
6262**
6263** ^The sqlite3_result_int() interface sets the return value
6264** of the application-defined function to be the 32-bit signed integer
6265** value given in the 2nd argument.
6266** ^The sqlite3_result_int64() interface sets the return value
6267** of the application-defined function to be the 64-bit signed integer
6268** value given in the 2nd argument.
6269**
6270** ^The sqlite3_result_null() interface sets the return value
6271** of the application-defined function to be NULL.
6272**
6273** ^The sqlite3_result_text(), sqlite3_result_text16(),
6274** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6275** set the return value of the application-defined function to be
6276** a text string which is represented as UTF-8, UTF-16 native byte order,
6277** UTF-16 little endian, or UTF-16 big endian, respectively.
6278** ^The sqlite3_result_text64() interface sets the return value of an
6279** application-defined function to be a text string in an encoding
6280** specified by the fifth (and last) parameter, which must be one
6281** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6282** ^SQLite takes the text result from the application from
6283** the 2nd parameter of the sqlite3_result_text* interfaces.
6284** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6285** is negative, then SQLite takes result text from the 2nd parameter
6286** through the first zero character.
6287** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6288** is non-negative, then as many bytes (not characters) of the text
6289** pointed to by the 2nd parameter are taken as the application-defined
6290** function result. If the 3rd parameter is non-negative, then it
6291** must be the byte offset into the string where the NUL terminator would
6292** appear if the string where NUL terminated. If any NUL characters occur
6293** in the string at a byte offset that is less than the value of the 3rd
6294** parameter, then the resulting string will contain embedded NULs and the
6295** result of expressions operating on strings with embedded NULs is undefined.
6296** ^If the 4th parameter to the sqlite3_result_text* interfaces
6297** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6298** function as the destructor on the text or BLOB result when it has
6299** finished using that result.
6300** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
6301** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
6302** assumes that the text or BLOB result is in constant space and does not
6303** copy the content of the parameter nor call a destructor on the content
6304** when it has finished using that result.
6305** ^If the 4th parameter to the sqlite3_result_text* interfaces
6306** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
6307** then SQLite makes a copy of the result into space obtained
6308** from [sqlite3_malloc()] before it returns.
6309**
6310** ^The sqlite3_result_value() interface sets the result of
6311** the application-defined function to be a copy of the
6312** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
6313** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
6314** so that the [sqlite3_value] specified in the parameter may change or
6315** be deallocated after sqlite3_result_value() returns without harm.
6316** ^A [protected sqlite3_value] object may always be used where an
6317** [unprotected sqlite3_value] object is required, so either
6318** kind of [sqlite3_value] object can be used with this interface.
6319**
6320** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
6321** SQL NULL value, just like [sqlite3_result_null(C)], except that it
6322** also associates the host-language pointer P or type T with that
6323** NULL value such that the pointer can be retrieved within an
6324** [application-defined SQL function] using [sqlite3_value_pointer()].
6325** ^If the D parameter is not NULL, then it is a pointer to a destructor
6326** for the P parameter. ^SQLite invokes D with P as its only argument
6327** when SQLite is finished with P. The T parameter should be a static
6328** string and preferably a string literal. The sqlite3_result_pointer()
6329** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6330**
6331** If these routines are called from within the different thread
6332** than the one containing the application-defined function that received
6333** the [sqlite3_context] pointer, the results are undefined.
6334*/
6335SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
6336SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,
6337 sqlite3_uint64,void(*)(void*));
6338SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
6339SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
6340SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
6341SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
6342SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
6343SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6344SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6345SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6346SQLITE_API void sqlite3_result_null(sqlite3_context*);
6347SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6348SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6349 void(*)(void*), unsigned char encoding);
6350SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6351SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6352SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6353SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
6354SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
6355SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
6356SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6357
6358
6359/*
6360** CAPI3REF: Setting The Subtype Of An SQL Function
6361** METHOD: sqlite3_context
6362**
6363** The sqlite3_result_subtype(C,T) function causes the subtype of
6364** the result from the [application-defined SQL function] with
6365** [sqlite3_context] C to be the value T. Only the lower 8 bits
6366** of the subtype T are preserved in current versions of SQLite;
6367** higher order bits are discarded.
6368** The number of subtype bytes preserved by SQLite might increase
6369** in future releases of SQLite.
6370*/
6371SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6372
6373/*
6374** CAPI3REF: Define New Collating Sequences
6375** METHOD: sqlite3
6376**
6377** ^These functions add, remove, or modify a [collation] associated
6378** with the [database connection] specified as the first argument.
6379**
6380** ^The name of the collation is a UTF-8 string
6381** for sqlite3_create_collation() and sqlite3_create_collation_v2()
6382** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6383** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
6384** considered to be the same name.
6385**
6386** ^(The third argument (eTextRep) must be one of the constants:
6387** <ul>
6388** <li> [SQLITE_UTF8],
6389** <li> [SQLITE_UTF16LE],
6390** <li> [SQLITE_UTF16BE],
6391** <li> [SQLITE_UTF16], or
6392** <li> [SQLITE_UTF16_ALIGNED].
6393** </ul>)^
6394** ^The eTextRep argument determines the encoding of strings passed
6395** to the collating function callback, xCallback.
6396** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
6397** force strings to be UTF16 with native byte order.
6398** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
6399** on an even byte address.
6400**
6401** ^The fourth argument, pArg, is an application data pointer that is passed
6402** through as the first argument to the collating function callback.
6403**
6404** ^The fifth argument, xCallback, is a pointer to the collating function.
6405** ^Multiple collating functions can be registered using the same name but
6406** with different eTextRep parameters and SQLite will use whichever
6407** function requires the least amount of data transformation.
6408** ^If the xCallback argument is NULL then the collating function is
6409** deleted. ^When all collating functions having the same name are deleted,
6410** that collation is no longer usable.
6411**
6412** ^The collating function callback is invoked with a copy of the pArg
6413** application data pointer and with two strings in the encoding specified
6414** by the eTextRep argument. The collating function must return an
6415** integer that is negative, zero, or positive
6416** if the first string is less than, equal to, or greater than the second,
6417** respectively. A collating function must always return the same answer
6418** given the same inputs. If two or more collating functions are registered
6419** to the same collation name (using different eTextRep values) then all
6420** must give an equivalent answer when invoked with equivalent strings.
6421** The collating function must obey the following properties for all
6422** strings A, B, and C:
6423**
6424** <ol>
6425** <li> If A==B then B==A.
6426** <li> If A==B and B==C then A==C.
6427** <li> If A&lt;B THEN B&gt;A.
6428** <li> If A&lt;B and B&lt;C then A&lt;C.
6429** </ol>
6430**
6431** If a collating function fails any of the above constraints and that
6432** collating function is registered and used, then the behavior of SQLite
6433** is undefined.
6434**
6435** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
6436** with the addition that the xDestroy callback is invoked on pArg when
6437** the collating function is deleted.
6438** ^Collating functions are deleted when they are overridden by later
6439** calls to the collation creation functions or when the
6440** [database connection] is closed using [sqlite3_close()].
6441**
6442** ^The xDestroy callback is <u>not</u> called if the
6443** sqlite3_create_collation_v2() function fails. Applications that invoke
6444** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6445** check the return code and dispose of the application data pointer
6446** themselves rather than expecting SQLite to deal with it for them.
6447** This is different from every other SQLite interface. The inconsistency
6448** is unfortunate but cannot be changed without breaking backwards
6449** compatibility.
6450**
6451** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
6452*/
6453SQLITE_API int sqlite3_create_collation(
6454 sqlite3*,
6455 const char *zName,
6456 int eTextRep,
6457 void *pArg,
6458 int(*xCompare)(void*,int,const void*,int,const void*)
6459);
6460SQLITE_API int sqlite3_create_collation_v2(
6461 sqlite3*,
6462 const char *zName,
6463 int eTextRep,
6464 void *pArg,
6465 int(*xCompare)(void*,int,const void*,int,const void*),
6466 void(*xDestroy)(void*)
6467);
6468SQLITE_API int sqlite3_create_collation16(
6469 sqlite3*,
6470 const void *zName,
6471 int eTextRep,
6472 void *pArg,
6473 int(*xCompare)(void*,int,const void*,int,const void*)
6474);
6475
6476/*
6477** CAPI3REF: Collation Needed Callbacks
6478** METHOD: sqlite3
6479**
6480** ^To avoid having to register all collation sequences before a database
6481** can be used, a single callback function may be registered with the
6482** [database connection] to be invoked whenever an undefined collation
6483** sequence is required.
6484**
6485** ^If the function is registered using the sqlite3_collation_needed() API,
6486** then it is passed the names of undefined collation sequences as strings
6487** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6488** the names are passed as UTF-16 in machine native byte order.
6489** ^A call to either function replaces the existing collation-needed callback.
6490**
6491** ^(When the callback is invoked, the first argument passed is a copy
6492** of the second argument to sqlite3_collation_needed() or
6493** sqlite3_collation_needed16(). The second argument is the database
6494** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
6495** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
6496** sequence function required. The fourth parameter is the name of the
6497** required collation sequence.)^
6498**
6499** The callback function should register the desired collation using
6500** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
6501** [sqlite3_create_collation_v2()].
6502*/
6503SQLITE_API int sqlite3_collation_needed(
6504 sqlite3*,
6505 void*,
6506 void(*)(void*,sqlite3*,int eTextRep,const char*)
6507);
6508SQLITE_API int sqlite3_collation_needed16(
6509 sqlite3*,
6510 void*,
6511 void(*)(void*,sqlite3*,int eTextRep,const void*)
6512);
6513
6514#ifdef SQLITE_HAS_CODEC
6515/*
6516** Specify the key for an encrypted database. This routine should be
6517** called right after sqlite3_open().
6518**
6519** The code to implement this API is not available in the public release
6520** of SQLite.
6521*/
6522SQLITE_API int sqlite3_key(
6523 sqlite3 *db, /* Database to be rekeyed */
6524 const void *pKey, int nKey /* The key */
6525);
6526SQLITE_API int sqlite3_key_v2(
6527 sqlite3 *db, /* Database to be rekeyed */
6528 const char *zDbName, /* Name of the database */
6529 const void *pKey, int nKey /* The key */
6530);
6531
6532/*
6533** Change the key on an open database. If the current database is not
6534** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the
6535** database is decrypted.
6536**
6537** The code to implement this API is not available in the public release
6538** of SQLite.
6539*/
6540SQLITE_API int sqlite3_rekey(
6541 sqlite3 *db, /* Database to be rekeyed */
6542 const void *pKey, int nKey /* The new key */
6543);
6544SQLITE_API int sqlite3_rekey_v2(
6545 sqlite3 *db, /* Database to be rekeyed */
6546 const char *zDbName, /* Name of the database */
6547 const void *pKey, int nKey /* The new key */
6548);
6549
6550/*
6551** Specify the activation key for a SEE database. Unless
6552** activated, none of the SEE routines will work.
6553*/
6554SQLITE_API void sqlite3_activate_see(
6555 const char *zPassPhrase /* Activation phrase */
6556);
6557#endif
6558
6559#ifdef SQLITE_ENABLE_CEROD
6560/*
6561** Specify the activation key for a CEROD database. Unless
6562** activated, none of the CEROD routines will work.
6563*/
6564SQLITE_API void sqlite3_activate_cerod(
6565 const char *zPassPhrase /* Activation phrase */
6566);
6567#endif
6568
6569/*
6570** CAPI3REF: Suspend Execution For A Short Time
6571**
6572** The sqlite3_sleep() function causes the current thread to suspend execution
6573** for at least a number of milliseconds specified in its parameter.
6574**
6575** If the operating system does not support sleep requests with
6576** millisecond time resolution, then the time will be rounded up to
6577** the nearest second. The number of milliseconds of sleep actually
6578** requested from the operating system is returned.
6579**
6580** ^SQLite implements this interface by calling the xSleep()
6581** method of the default [sqlite3_vfs] object. If the xSleep() method
6582** of the default VFS is not implemented correctly, or not implemented at
6583** all, then the behavior of sqlite3_sleep() may deviate from the description
6584** in the previous paragraphs.
6585*/
6586SQLITE_API int sqlite3_sleep(int);
6587
6588/*
6589** CAPI3REF: Name Of The Folder Holding Temporary Files
6590**
6591** ^(If this global variable is made to point to a string which is
6592** the name of a folder (a.k.a. directory), then all temporary files
6593** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6594** will be placed in that directory.)^ ^If this variable
6595** is a NULL pointer, then SQLite performs a search for an appropriate
6596** temporary file directory.
6597**
6598** Applications are strongly discouraged from using this global variable.
6599** It is required to set a temporary folder on Windows Runtime (WinRT).
6600** But for all other platforms, it is highly recommended that applications
6601** neither read nor write this variable. This global variable is a relic
6602** that exists for backwards compatibility of legacy applications and should
6603** be avoided in new projects.
6604**
6605** It is not safe to read or modify this variable in more than one
6606** thread at a time. It is not safe to read or modify this variable
6607** if a [database connection] is being used at the same time in a separate
6608** thread.
6609** It is intended that this variable be set once
6610** as part of process initialization and before any SQLite interface
6611** routines have been called and that this variable remain unchanged
6612** thereafter.
6613**
6614** ^The [temp_store_directory pragma] may modify this variable and cause
6615** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6616** the [temp_store_directory pragma] always assumes that any string
6617** that this variable points to is held in memory obtained from
6618** [sqlite3_malloc] and the pragma may attempt to free that memory
6619** using [sqlite3_free].
6620** Hence, if this variable is modified directly, either it should be
6621** made NULL or made to point to memory obtained from [sqlite3_malloc]
6622** or else the use of the [temp_store_directory pragma] should be avoided.
6623** Except when requested by the [temp_store_directory pragma], SQLite
6624** does not free the memory that sqlite3_temp_directory points to. If
6625** the application wants that memory to be freed, it must do
6626** so itself, taking care to only do so after all [database connection]
6627** objects have been destroyed.
6628**
6629** <b>Note to Windows Runtime users:</b> The temporary directory must be set
6630** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
6631** features that require the use of temporary files may fail. Here is an
6632** example of how to do this using C++ with the Windows Runtime:
6633**
6634** <blockquote><pre>
6635** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6636** &nbsp; TemporaryFolder->Path->Data();
6637** char zPathBuf&#91;MAX_PATH + 1&#93;;
6638** memset(zPathBuf, 0, sizeof(zPathBuf));
6639** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6640** &nbsp; NULL, NULL);
6641** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
6642** </pre></blockquote>
6643*/
6644SQLITE_API char *sqlite3_temp_directory;
6645
6646/*
6647** CAPI3REF: Name Of The Folder Holding Database Files
6648**
6649** ^(If this global variable is made to point to a string which is
6650** the name of a folder (a.k.a. directory), then all database files
6651** specified with a relative pathname and created or accessed by
6652** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6653** to be relative to that directory.)^ ^If this variable is a NULL
6654** pointer, then SQLite assumes that all database files specified
6655** with a relative pathname are relative to the current directory
6656** for the process. Only the windows VFS makes use of this global
6657** variable; it is ignored by the unix VFS.
6658**
6659** Changing the value of this variable while a database connection is
6660** open can result in a corrupt database.
6661**
6662** It is not safe to read or modify this variable in more than one
6663** thread at a time. It is not safe to read or modify this variable
6664** if a [database connection] is being used at the same time in a separate
6665** thread.
6666** It is intended that this variable be set once
6667** as part of process initialization and before any SQLite interface
6668** routines have been called and that this variable remain unchanged
6669** thereafter.
6670**
6671** ^The [data_store_directory pragma] may modify this variable and cause
6672** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6673** the [data_store_directory pragma] always assumes that any string
6674** that this variable points to is held in memory obtained from
6675** [sqlite3_malloc] and the pragma may attempt to free that memory
6676** using [sqlite3_free].
6677** Hence, if this variable is modified directly, either it should be
6678** made NULL or made to point to memory obtained from [sqlite3_malloc]
6679** or else the use of the [data_store_directory pragma] should be avoided.
6680*/
6681SQLITE_API char *sqlite3_data_directory;
6682
6683/*
6684** CAPI3REF: Win32 Specific Interface
6685**
6686** These interfaces are available only on Windows. The
6687** [sqlite3_win32_set_directory] interface is used to set the value associated
6688** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
6689** zValue, depending on the value of the type parameter. The zValue parameter
6690** should be NULL to cause the previous value to be freed via [sqlite3_free];
6691** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6692** prior to being used. The [sqlite3_win32_set_directory] interface returns
6693** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
6694** or [SQLITE_NOMEM] if memory could not be allocated. The value of the
6695** [sqlite3_data_directory] variable is intended to act as a replacement for
6696** the current directory on the sub-platforms of Win32 where that concept is
6697** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
6698** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
6699** sqlite3_win32_set_directory interface except the string parameter must be
6700** UTF-8 or UTF-16, respectively.
6701*/
6702SQLITE_API int sqlite3_win32_set_directory(
6703 unsigned long type, /* Identifier for directory being set or reset */
6704 void *zValue /* New value for directory being set or reset */
6705);
6706SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
6707SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
6708
6709/*
6710** CAPI3REF: Win32 Directory Types
6711**
6712** These macros are only available on Windows. They define the allowed values
6713** for the type argument to the [sqlite3_win32_set_directory] interface.
6714*/
6715#define SQLITE_WIN32_DATA_DIRECTORY_TYPE 1
6716#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE 2
6717
6718/*
6719** CAPI3REF: Test For Auto-Commit Mode
6720** KEYWORDS: {autocommit mode}
6721** METHOD: sqlite3
6722**
6723** ^The sqlite3_get_autocommit() interface returns non-zero or
6724** zero if the given database connection is or is not in autocommit mode,
6725** respectively. ^Autocommit mode is on by default.
6726** ^Autocommit mode is disabled by a [BEGIN] statement.
6727** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6728**
6729** If certain kinds of errors occur on a statement within a multi-statement
6730** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
6731** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
6732** transaction might be rolled back automatically. The only way to
6733** find out whether SQLite automatically rolled back the transaction after
6734** an error is to use this function.
6735**
6736** If another thread changes the autocommit status of the database
6737** connection while this routine is running, then the return value
6738** is undefined.
6739*/
6740SQLITE_API int sqlite3_get_autocommit(sqlite3*);
6741
6742/*
6743** CAPI3REF: Find The Database Handle Of A Prepared Statement
6744** METHOD: sqlite3_stmt
6745**
6746** ^The sqlite3_db_handle interface returns the [database connection] handle
6747** to which a [prepared statement] belongs. ^The [database connection]
6748** returned by sqlite3_db_handle is the same [database connection]
6749** that was the first argument
6750** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
6751** create the statement in the first place.
6752*/
6753SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
6754
6755/*
6756** CAPI3REF: Return The Filename For A Database Connection
6757** METHOD: sqlite3
6758**
6759** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
6760** associated with database N of connection D. ^The main database file
6761** has the name "main". If there is no attached database N on the database
6762** connection D, or if database N is a temporary or in-memory database, then
6763** a NULL pointer is returned.
6764**
6765** ^The filename returned by this function is the output of the
6766** xFullPathname method of the [VFS]. ^In other words, the filename
6767** will be an absolute pathname, even if the filename used
6768** to open the database originally was a URI or relative pathname.
6769*/
6770SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
6771
6772/*
6773** CAPI3REF: Determine if a database is read-only
6774** METHOD: sqlite3
6775**
6776** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
6777** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6778** the name of a database on connection D.
6779*/
6780SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
6781
6782/*
6783** CAPI3REF: Find the next prepared statement
6784** METHOD: sqlite3
6785**
6786** ^This interface returns a pointer to the next [prepared statement] after
6787** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
6788** then this interface returns a pointer to the first prepared statement
6789** associated with the database connection pDb. ^If no prepared statement
6790** satisfies the conditions of this routine, it returns NULL.
6791**
6792** The [database connection] pointer D in a call to
6793** [sqlite3_next_stmt(D,S)] must refer to an open database
6794** connection and in particular must not be a NULL pointer.
6795*/
6796SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
6797
6798/*
6799** CAPI3REF: Commit And Rollback Notification Callbacks
6800** METHOD: sqlite3
6801**
6802** ^The sqlite3_commit_hook() interface registers a callback
6803** function to be invoked whenever a transaction is [COMMIT | committed].
6804** ^Any callback set by a previous call to sqlite3_commit_hook()
6805** for the same database connection is overridden.
6806** ^The sqlite3_rollback_hook() interface registers a callback
6807** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
6808** ^Any callback set by a previous call to sqlite3_rollback_hook()
6809** for the same database connection is overridden.
6810** ^The pArg argument is passed through to the callback.
6811** ^If the callback on a commit hook function returns non-zero,
6812** then the commit is converted into a rollback.
6813**
6814** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
6815** return the P argument from the previous call of the same function
6816** on the same [database connection] D, or NULL for
6817** the first call for each function on D.
6818**
6819** The commit and rollback hook callbacks are not reentrant.
6820** The callback implementation must not do anything that will modify
6821** the database connection that invoked the callback. Any actions
6822** to modify the database connection must be deferred until after the
6823** completion of the [sqlite3_step()] call that triggered the commit
6824** or rollback hook in the first place.
6825** Note that running any other SQL statements, including SELECT statements,
6826** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
6827** the database connections for the meaning of "modify" in this paragraph.
6828**
6829** ^Registering a NULL function disables the callback.
6830**
6831** ^When the commit hook callback routine returns zero, the [COMMIT]
6832** operation is allowed to continue normally. ^If the commit hook
6833** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6834** ^The rollback hook is invoked on a rollback that results from a commit
6835** hook returning non-zero, just as it would be with any other rollback.
6836**
6837** ^For the purposes of this API, a transaction is said to have been
6838** rolled back if an explicit "ROLLBACK" statement is executed, or
6839** an error or constraint causes an implicit rollback to occur.
6840** ^The rollback callback is not invoked if a transaction is
6841** automatically rolled back because the database connection is closed.
6842**
6843** See also the [sqlite3_update_hook()] interface.
6844*/
6845SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
6846SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
6847
6848/*
6849** CAPI3REF: Data Change Notification Callbacks
6850** METHOD: sqlite3
6851**
6852** ^The sqlite3_update_hook() interface registers a callback function
6853** with the [database connection] identified by the first argument
6854** to be invoked whenever a row is updated, inserted or deleted in
6855** a [rowid table].
6856** ^Any callback set by a previous call to this function
6857** for the same database connection is overridden.
6858**
6859** ^The second argument is a pointer to the function to invoke when a
6860** row is updated, inserted or deleted in a rowid table.
6861** ^The first argument to the callback is a copy of the third argument
6862** to sqlite3_update_hook().
6863** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
6864** or [SQLITE_UPDATE], depending on the operation that caused the callback
6865** to be invoked.
6866** ^The third and fourth arguments to the callback contain pointers to the
6867** database and table name containing the affected row.
6868** ^The final callback parameter is the [rowid] of the row.
6869** ^In the case of an update, this is the [rowid] after the update takes place.
6870**
6871** ^(The update hook is not invoked when internal system tables are
6872** modified (i.e. sqlite_master and sqlite_sequence).)^
6873** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
6874**
6875** ^In the current implementation, the update hook
6876** is not invoked when conflicting rows are deleted because of an
6877** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
6878** invoked when rows are deleted using the [truncate optimization].
6879** The exceptions defined in this paragraph might change in a future
6880** release of SQLite.
6881**
6882** The update hook implementation must not do anything that will modify
6883** the database connection that invoked the update hook. Any actions
6884** to modify the database connection must be deferred until after the
6885** completion of the [sqlite3_step()] call that triggered the update hook.
6886** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
6887** database connections for the meaning of "modify" in this paragraph.
6888**
6889** ^The sqlite3_update_hook(D,C,P) function
6890** returns the P argument from the previous call
6891** on the same [database connection] D, or NULL for
6892** the first call on D.
6893**
6894** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
6895** and [sqlite3_preupdate_hook()] interfaces.
6896*/
6897SQLITE_API void *sqlite3_update_hook(
6898 sqlite3*,
6899 void(*)(void *,int ,char const *,char const *,sqlite3_int64),
6900 void*
6901);
6902
6903/*
6904** CAPI3REF: Enable Or Disable Shared Pager Cache
6905**
6906** ^(This routine enables or disables the sharing of the database cache
6907** and schema data structures between [database connection | connections]
6908** to the same database. Sharing is enabled if the argument is true
6909** and disabled if the argument is false.)^
6910**
6911** ^Cache sharing is enabled and disabled for an entire process.
6912** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
6913** In prior versions of SQLite,
6914** sharing was enabled or disabled for each thread separately.
6915**
6916** ^(The cache sharing mode set by this interface effects all subsequent
6917** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
6918** Existing database connections continue use the sharing mode
6919** that was in effect at the time they were opened.)^
6920**
6921** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
6922** successfully. An [error code] is returned otherwise.)^
6923**
6924** ^Shared cache is disabled by default. But this might change in
6925** future releases of SQLite. Applications that care about shared
6926** cache setting should set it explicitly.
6927**
6928** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0
6929** and will always return SQLITE_MISUSE. On those systems,
6930** shared cache mode should be enabled per-database connection via
6931** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE].
6932**
6933** This interface is threadsafe on processors where writing a
6934** 32-bit integer is atomic.
6935**
6936** See Also: [SQLite Shared-Cache Mode]
6937*/
6938SQLITE_API int sqlite3_enable_shared_cache(int);
6939
6940/*
6941** CAPI3REF: Attempt To Free Heap Memory
6942**
6943** ^The sqlite3_release_memory() interface attempts to free N bytes
6944** of heap memory by deallocating non-essential memory allocations
6945** held by the database library. Memory used to cache database
6946** pages to improve performance is an example of non-essential memory.
6947** ^sqlite3_release_memory() returns the number of bytes actually freed,
6948** which might be more or less than the amount requested.
6949** ^The sqlite3_release_memory() routine is a no-op returning zero
6950** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
6951**
6952** See also: [sqlite3_db_release_memory()]
6953*/
6954SQLITE_API int sqlite3_release_memory(int);
6955
6956/*
6957** CAPI3REF: Free Memory Used By A Database Connection
6958** METHOD: sqlite3
6959**
6960** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
6961** memory as possible from database connection D. Unlike the
6962** [sqlite3_release_memory()] interface, this interface is in effect even
6963** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
6964** omitted.
6965**
6966** See also: [sqlite3_release_memory()]
6967*/
6968SQLITE_API int sqlite3_db_release_memory(sqlite3*);
6969
6970/*
6971** CAPI3REF: Impose A Limit On Heap Size
6972**
6973** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
6974** soft limit on the amount of heap memory that may be allocated by SQLite.
6975** ^SQLite strives to keep heap memory utilization below the soft heap
6976** limit by reducing the number of pages held in the page cache
6977** as heap memory usages approaches the limit.
6978** ^The soft heap limit is "soft" because even though SQLite strives to stay
6979** below the limit, it will exceed the limit rather than generate
6980** an [SQLITE_NOMEM] error. In other words, the soft heap limit
6981** is advisory only.
6982**
6983** ^The return value from sqlite3_soft_heap_limit64() is the size of
6984** the soft heap limit prior to the call, or negative in the case of an
6985** error. ^If the argument N is negative
6986** then no change is made to the soft heap limit. Hence, the current
6987** size of the soft heap limit can be determined by invoking
6988** sqlite3_soft_heap_limit64() with a negative argument.
6989**
6990** ^If the argument N is zero then the soft heap limit is disabled.
6991**
6992** ^(The soft heap limit is not enforced in the current implementation
6993** if one or more of following conditions are true:
6994**
6995** <ul>
6996** <li> The soft heap limit is set to zero.
6997** <li> Memory accounting is disabled using a combination of the
6998** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
6999** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7000** <li> An alternative page cache implementation is specified using
7001** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
7002** <li> The page cache allocates from its own memory pool supplied
7003** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
7004** from the heap.
7005** </ul>)^
7006**
7007** Beginning with SQLite [version 3.7.3] ([dateof:3.7.3]),
7008** the soft heap limit is enforced
7009** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
7010** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
7011** the soft heap limit is enforced on every memory allocation. Without
7012** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
7013** when memory is allocated by the page cache. Testing suggests that because
7014** the page cache is the predominate memory user in SQLite, most
7015** applications will achieve adequate soft heap limit enforcement without
7016** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
7017**
7018** The circumstances under which SQLite will enforce the soft heap limit may
7019** changes in future releases of SQLite.
7020*/
7021SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
7022
7023/*
7024** CAPI3REF: Deprecated Soft Heap Limit Interface
7025** DEPRECATED
7026**
7027** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
7028** interface. This routine is provided for historical compatibility
7029** only. All new applications should use the
7030** [sqlite3_soft_heap_limit64()] interface rather than this one.
7031*/
7032SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
7033
7034
7035/*
7036** CAPI3REF: Extract Metadata About A Column Of A Table
7037** METHOD: sqlite3
7038**
7039** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
7040** information about column C of table T in database D
7041** on [database connection] X.)^ ^The sqlite3_table_column_metadata()
7042** interface returns SQLITE_OK and fills in the non-NULL pointers in
7043** the final five arguments with appropriate values if the specified
7044** column exists. ^The sqlite3_table_column_metadata() interface returns
7045** SQLITE_ERROR and if the specified column does not exist.
7046** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7047** NULL pointer, then this routine simply checks for the existence of the
7048** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
7049** does not. If the table name parameter T in a call to
7050** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
7051** undefined behavior.
7052**
7053** ^The column is identified by the second, third and fourth parameters to
7054** this function. ^(The second parameter is either the name of the database
7055** (i.e. "main", "temp", or an attached database) containing the specified
7056** table or NULL.)^ ^If it is NULL, then all attached databases are searched
7057** for the table using the same algorithm used by the database engine to
7058** resolve unqualified table references.
7059**
7060** ^The third and fourth parameters to this function are the table and column
7061** name of the desired column, respectively.
7062**
7063** ^Metadata is returned by writing to the memory locations passed as the 5th
7064** and subsequent parameters to this function. ^Any of these arguments may be
7065** NULL, in which case the corresponding element of metadata is omitted.
7066**
7067** ^(<blockquote>
7068** <table border="1">
7069** <tr><th> Parameter <th> Output<br>Type <th> Description
7070**
7071** <tr><td> 5th <td> const char* <td> Data type
7072** <tr><td> 6th <td> const char* <td> Name of default collation sequence
7073** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
7074** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY
7075** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT]
7076** </table>
7077** </blockquote>)^
7078**
7079** ^The memory pointed to by the character pointers returned for the
7080** declaration type and collation sequence is valid until the next
7081** call to any SQLite API function.
7082**
7083** ^If the specified table is actually a view, an [error code] is returned.
7084**
7085** ^If the specified column is "rowid", "oid" or "_rowid_" and the table
7086** is not a [WITHOUT ROWID] table and an
7087** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
7088** parameters are set for the explicitly declared column. ^(If there is no
7089** [INTEGER PRIMARY KEY] column, then the outputs
7090** for the [rowid] are set as follows:
7091**
7092** <pre>
7093** data type: "INTEGER"
7094** collation sequence: "BINARY"
7095** not null: 0
7096** primary key: 1
7097** auto increment: 0
7098** </pre>)^
7099**
7100** ^This function causes all database schemas to be read from disk and
7101** parsed, if that has not already been done, and returns an error if
7102** any errors are encountered while loading the schema.
7103*/
7104SQLITE_API int sqlite3_table_column_metadata(
7105 sqlite3 *db, /* Connection handle */
7106 const char *zDbName, /* Database name or NULL */
7107 const char *zTableName, /* Table name */
7108 const char *zColumnName, /* Column name */
7109 char const **pzDataType, /* OUTPUT: Declared data type */
7110 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
7111 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
7112 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
7113 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7114);
7115
7116/*
7117** CAPI3REF: Load An Extension
7118** METHOD: sqlite3
7119**
7120** ^This interface loads an SQLite extension library from the named file.
7121**
7122** ^The sqlite3_load_extension() interface attempts to load an
7123** [SQLite extension] library contained in the file zFile. If
7124** the file cannot be loaded directly, attempts are made to load
7125** with various operating-system specific extensions added.
7126** So for example, if "samplelib" cannot be loaded, then names like
7127** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
7128** be tried also.
7129**
7130** ^The entry point is zProc.
7131** ^(zProc may be 0, in which case SQLite will try to come up with an
7132** entry point name on its own. It first tries "sqlite3_extension_init".
7133** If that does not work, it constructs a name "sqlite3_X_init" where the
7134** X is consists of the lower-case equivalent of all ASCII alphabetic
7135** characters in the filename from the last "/" to the first following
7136** "." and omitting any initial "lib".)^
7137** ^The sqlite3_load_extension() interface returns
7138** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
7139** ^If an error occurs and pzErrMsg is not 0, then the
7140** [sqlite3_load_extension()] interface shall attempt to
7141** fill *pzErrMsg with error message text stored in memory
7142** obtained from [sqlite3_malloc()]. The calling function
7143** should free this memory by calling [sqlite3_free()].
7144**
7145** ^Extension loading must be enabled using
7146** [sqlite3_enable_load_extension()] or
7147** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
7148** prior to calling this API,
7149** otherwise an error will be returned.
7150**
7151** <b>Security warning:</b> It is recommended that the
7152** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
7153** interface. The use of the [sqlite3_enable_load_extension()] interface
7154** should be avoided. This will keep the SQL function [load_extension()]
7155** disabled and prevent SQL injections from giving attackers
7156** access to extension loading capabilities.
7157**
7158** See also the [load_extension() SQL function].
7159*/
7160SQLITE_API int sqlite3_load_extension(
7161 sqlite3 *db, /* Load the extension into this database connection */
7162 const char *zFile, /* Name of the shared library containing extension */
7163 const char *zProc, /* Entry point. Derived from zFile if 0 */
7164 char **pzErrMsg /* Put error message here if not 0 */
7165);
7166
7167/*
7168** CAPI3REF: Enable Or Disable Extension Loading
7169** METHOD: sqlite3
7170**
7171** ^So as not to open security holes in older applications that are
7172** unprepared to deal with [extension loading], and as a means of disabling
7173** [extension loading] while evaluating user-entered SQL, the following API
7174** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
7175**
7176** ^Extension loading is off by default.
7177** ^Call the sqlite3_enable_load_extension() routine with onoff==1
7178** to turn extension loading on and call it with onoff==0 to turn
7179** it back off again.
7180**
7181** ^This interface enables or disables both the C-API
7182** [sqlite3_load_extension()] and the SQL function [load_extension()].
7183** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
7184** to enable or disable only the C-API.)^
7185**
7186** <b>Security warning:</b> It is recommended that extension loading
7187** be disabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
7188** rather than this interface, so the [load_extension()] SQL function
7189** remains disabled. This will prevent SQL injections from giving attackers
7190** access to extension loading capabilities.
7191*/
7192SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
7193
7194/*
7195** CAPI3REF: Automatically Load Statically Linked Extensions
7196**
7197** ^This interface causes the xEntryPoint() function to be invoked for
7198** each new [database connection] that is created. The idea here is that
7199** xEntryPoint() is the entry point for a statically linked [SQLite extension]
7200** that is to be automatically loaded into all new database connections.
7201**
7202** ^(Even though the function prototype shows that xEntryPoint() takes
7203** no arguments and returns void, SQLite invokes xEntryPoint() with three
7204** arguments and expects an integer result as if the signature of the
7205** entry point where as follows:
7206**
7207** <blockquote><pre>
7208** &nbsp; int xEntryPoint(
7209** &nbsp; sqlite3 *db,
7210** &nbsp; const char **pzErrMsg,
7211** &nbsp; const struct sqlite3_api_routines *pThunk
7212** &nbsp; );
7213** </pre></blockquote>)^
7214**
7215** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
7216** point to an appropriate error message (obtained from [sqlite3_mprintf()])
7217** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg
7218** is NULL before calling the xEntryPoint(). ^SQLite will invoke
7219** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any
7220** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
7221** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
7222**
7223** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
7224** on the list of automatic extensions is a harmless no-op. ^No entry point
7225** will be called more than once for each database connection that is opened.
7226**
7227** See also: [sqlite3_reset_auto_extension()]
7228** and [sqlite3_cancel_auto_extension()]
7229*/
7230SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
7231
7232/*
7233** CAPI3REF: Cancel Automatic Extension Loading
7234**
7235** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
7236** initialization routine X that was registered using a prior call to
7237** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)]
7238** routine returns 1 if initialization routine X was successfully
7239** unregistered and it returns 0 if X was not on the list of initialization
7240** routines.
7241*/
7242SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
7243
7244/*
7245** CAPI3REF: Reset Automatic Extension Loading
7246**
7247** ^This interface disables all automatic extensions previously
7248** registered using [sqlite3_auto_extension()].
7249*/
7250SQLITE_API void sqlite3_reset_auto_extension(void);
7251
7252/*
7253** The interface to the virtual-table mechanism is currently considered
7254** to be experimental. The interface might change in incompatible ways.
7255** If this is a problem for you, do not use the interface at this time.
7256**
7257** When the virtual-table mechanism stabilizes, we will declare the
7258** interface fixed, support it indefinitely, and remove this comment.
7259*/
7260
7261/*
7262** Structures used by the virtual table interface
7263*/
7264typedef struct sqlite3_vtab sqlite3_vtab;
7265typedef struct sqlite3_index_info sqlite3_index_info;
7266typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
7267typedef struct sqlite3_module sqlite3_module;
7268
7269/*
7270** CAPI3REF: Virtual Table Object
7271** KEYWORDS: sqlite3_module {virtual table module}
7272**
7273** This structure, sometimes called a "virtual table module",
7274** defines the implementation of a [virtual tables].
7275** This structure consists mostly of methods for the module.
7276**
7277** ^A virtual table module is created by filling in a persistent
7278** instance of this structure and passing a pointer to that instance
7279** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
7280** ^The registration remains valid until it is replaced by a different
7281** module or until the [database connection] closes. The content
7282** of this structure must not change while it is registered with
7283** any database connection.
7284*/
7285struct sqlite3_module {
7286 int iVersion;
7287 int (*xCreate)(sqlite3*, void *pAux,
7288 int argc, const char *const*argv,
7289 sqlite3_vtab **ppVTab, char**);
7290 int (*xConnect)(sqlite3*, void *pAux,
7291 int argc, const char *const*argv,
7292 sqlite3_vtab **ppVTab, char**);
7293 int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
7294 int (*xDisconnect)(sqlite3_vtab *pVTab);
7295 int (*xDestroy)(sqlite3_vtab *pVTab);
7296 int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
7297 int (*xClose)(sqlite3_vtab_cursor*);
7298 int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
7299 int argc, sqlite3_value **argv);
7300 int (*xNext)(sqlite3_vtab_cursor*);
7301 int (*xEof)(sqlite3_vtab_cursor*);
7302 int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
7303 int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
7304 int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
7305 int (*xBegin)(sqlite3_vtab *pVTab);
7306 int (*xSync)(sqlite3_vtab *pVTab);
7307 int (*xCommit)(sqlite3_vtab *pVTab);
7308 int (*xRollback)(sqlite3_vtab *pVTab);
7309 int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
7310 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
7311 void **ppArg);
7312 int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
7313 /* The methods above are in version 1 of the sqlite_module object. Those
7314 ** below are for version 2 and greater. */
7315 int (*xSavepoint)(sqlite3_vtab *pVTab, int);
7316 int (*xRelease)(sqlite3_vtab *pVTab, int);
7317 int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
7318};
7319
7320/*
7321** CAPI3REF: Virtual Table Indexing Information
7322** KEYWORDS: sqlite3_index_info
7323**
7324** The sqlite3_index_info structure and its substructures is used as part
7325** of the [virtual table] interface to
7326** pass information into and receive the reply from the [xBestIndex]
7327** method of a [virtual table module]. The fields under **Inputs** are the
7328** inputs to xBestIndex and are read-only. xBestIndex inserts its
7329** results into the **Outputs** fields.
7330**
7331** ^(The aConstraint[] array records WHERE clause constraints of the form:
7332**
7333** <blockquote>column OP expr</blockquote>
7334**
7335** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^ ^(The particular operator is
7336** stored in aConstraint[].op using one of the
7337** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
7338** ^(The index of the column is stored in
7339** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the
7340** expr on the right-hand side can be evaluated (and thus the constraint
7341** is usable) and false if it cannot.)^
7342**
7343** ^The optimizer automatically inverts terms of the form "expr OP column"
7344** and makes other simplifications to the WHERE clause in an attempt to
7345** get as many WHERE clause terms into the form shown above as possible.
7346** ^The aConstraint[] array only reports WHERE clause terms that are
7347** relevant to the particular virtual table being queried.
7348**
7349** ^Information about the ORDER BY clause is stored in aOrderBy[].
7350** ^Each term of aOrderBy records a column of the ORDER BY clause.
7351**
7352** The colUsed field indicates which columns of the virtual table may be
7353** required by the current scan. Virtual table columns are numbered from
7354** zero in the order in which they appear within the CREATE TABLE statement
7355** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7356** the corresponding bit is set within the colUsed mask if the column may be
7357** required by SQLite. If the table has at least 64 columns and any column
7358** to the right of the first 63 is required, then bit 63 of colUsed is also
7359** set. In other words, column iCol may be required if the expression
7360** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to
7361** non-zero.
7362**
7363** The [xBestIndex] method must fill aConstraintUsage[] with information
7364** about what parameters to pass to xFilter. ^If argvIndex>0 then
7365** the right-hand side of the corresponding aConstraint[] is evaluated
7366** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7367** is true, then the constraint is assumed to be fully handled by the
7368** virtual table and is not checked again by SQLite.)^
7369**
7370** ^The idxNum and idxPtr values are recorded and passed into the
7371** [xFilter] method.
7372** ^[sqlite3_free()] is used to free idxPtr if and only if
7373** needToFreeIdxPtr is true.
7374**
7375** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
7376** the correct order to satisfy the ORDER BY clause so that no separate
7377** sorting step is required.
7378**
7379** ^The estimatedCost value is an estimate of the cost of a particular
7380** strategy. A cost of N indicates that the cost of the strategy is similar
7381** to a linear scan of an SQLite table with N rows. A cost of log(N)
7382** indicates that the expense of the operation is similar to that of a
7383** binary search on a unique indexed field of an SQLite table with N rows.
7384**
7385** ^The estimatedRows value is an estimate of the number of rows that
7386** will be returned by the strategy.
7387**
7388** The xBestIndex method may optionally populate the idxFlags field with a
7389** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7390** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite
7391** assumes that the strategy may visit at most one row.
7392**
7393** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
7394** SQLite also assumes that if a call to the xUpdate() method is made as
7395** part of the same statement to delete or update a virtual table row and the
7396** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
7397** any database changes. In other words, if the xUpdate() returns
7398** SQLITE_CONSTRAINT, the database contents must be exactly as they were
7399** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
7400** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
7401** the xUpdate method are automatically rolled back by SQLite.
7402**
7403** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
7404** structure for SQLite [version 3.8.2] ([dateof:3.8.2]).
7405** If a virtual table extension is
7406** used with an SQLite version earlier than 3.8.2, the results of attempting
7407** to read or write the estimatedRows field are undefined (but are likely
7408** to included crashing the application). The estimatedRows field should
7409** therefore only be used if [sqlite3_libversion_number()] returns a
7410** value greater than or equal to 3008002. Similarly, the idxFlags field
7411** was added for [version 3.9.0] ([dateof:3.9.0]).
7412** It may therefore only be used if
7413** sqlite3_libversion_number() returns a value greater than or equal to
7414** 3009000.
7415*/
7416struct sqlite3_index_info {
7417 /* Inputs */
7418 int nConstraint; /* Number of entries in aConstraint */
7419 struct sqlite3_index_constraint {
7420 int iColumn; /* Column constrained. -1 for ROWID */
7421 unsigned char op; /* Constraint operator */
7422 unsigned char usable; /* True if this constraint is usable */
7423 int iTermOffset; /* Used internally - xBestIndex should ignore */
7424 } *aConstraint; /* Table of WHERE clause constraints */
7425 int nOrderBy; /* Number of terms in the ORDER BY clause */
7426 struct sqlite3_index_orderby {
7427 int iColumn; /* Column number */
7428 unsigned char desc; /* True for DESC. False for ASC. */
7429 } *aOrderBy; /* The ORDER BY clause */
7430 /* Outputs */
7431 struct sqlite3_index_constraint_usage {
7432 int argvIndex; /* if >0, constraint is part of argv to xFilter */
7433 unsigned char omit; /* Do not code a test for this constraint */
7434 } *aConstraintUsage;
7435 int idxNum; /* Number used to identify the index */
7436 char *idxStr; /* String, possibly obtained from sqlite3_malloc */
7437 int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
7438 int orderByConsumed; /* True if output is already ordered */
7439 double estimatedCost; /* Estimated cost of using this index */
7440 /* Fields below are only available in SQLite 3.8.2 and later */
7441 sqlite3_int64 estimatedRows; /* Estimated number of rows returned */
7442 /* Fields below are only available in SQLite 3.9.0 and later */
7443 int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */
7444 /* Fields below are only available in SQLite 3.10.0 and later */
7445 sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
7446};
7447
7448/*
7449** CAPI3REF: Virtual Table Scan Flags
7450**
7451** Virtual table implementations are allowed to set the
7452** [sqlite3_index_info].idxFlags field to some combination of
7453** these bits.
7454*/
7455#define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */
7456
7457/*
7458** CAPI3REF: Virtual Table Constraint Operator Codes
7459**
7460** These macros defined the allowed values for the
7461** [sqlite3_index_info].aConstraint[].op field. Each value represents
7462** an operator that is part of a constraint term in the wHERE clause of
7463** a query that uses a [virtual table].
7464*/
7465#define SQLITE_INDEX_CONSTRAINT_EQ 2
7466#define SQLITE_INDEX_CONSTRAINT_GT 4
7467#define SQLITE_INDEX_CONSTRAINT_LE 8
7468#define SQLITE_INDEX_CONSTRAINT_LT 16
7469#define SQLITE_INDEX_CONSTRAINT_GE 32
7470#define SQLITE_INDEX_CONSTRAINT_MATCH 64
7471#define SQLITE_INDEX_CONSTRAINT_LIKE 65
7472#define SQLITE_INDEX_CONSTRAINT_GLOB 66
7473#define SQLITE_INDEX_CONSTRAINT_REGEXP 67
7474#define SQLITE_INDEX_CONSTRAINT_NE 68
7475#define SQLITE_INDEX_CONSTRAINT_ISNOT 69
7476#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
7477#define SQLITE_INDEX_CONSTRAINT_ISNULL 71
7478#define SQLITE_INDEX_CONSTRAINT_IS 72
7479#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
7480
7481/*
7482** CAPI3REF: Register A Virtual Table Implementation
7483** METHOD: sqlite3
7484**
7485** ^These routines are used to register a new [virtual table module] name.
7486** ^Module names must be registered before
7487** creating a new [virtual table] using the module and before using a
7488** preexisting [virtual table] for the module.
7489**
7490** ^The module name is registered on the [database connection] specified
7491** by the first parameter. ^The name of the module is given by the
7492** second parameter. ^The third parameter is a pointer to
7493** the implementation of the [virtual table module]. ^The fourth
7494** parameter is an arbitrary client data pointer that is passed through
7495** into the [xCreate] and [xConnect] methods of the virtual table module
7496** when a new virtual table is be being created or reinitialized.
7497**
7498** ^The sqlite3_create_module_v2() interface has a fifth parameter which
7499** is a pointer to a destructor for the pClientData. ^SQLite will
7500** invoke the destructor function (if it is not NULL) when SQLite
7501** no longer needs the pClientData pointer. ^The destructor will also
7502** be invoked if the call to sqlite3_create_module_v2() fails.
7503** ^The sqlite3_create_module()
7504** interface is equivalent to sqlite3_create_module_v2() with a NULL
7505** destructor.
7506*/
7507SQLITE_API int sqlite3_create_module(
7508 sqlite3 *db, /* SQLite connection to register module with */
7509 const char *zName, /* Name of the module */
7510 const sqlite3_module *p, /* Methods for the module */
7511 void *pClientData /* Client data for xCreate/xConnect */
7512);
7513SQLITE_API int sqlite3_create_module_v2(
7514 sqlite3 *db, /* SQLite connection to register module with */
7515 const char *zName, /* Name of the module */
7516 const sqlite3_module *p, /* Methods for the module */
7517 void *pClientData, /* Client data for xCreate/xConnect */
7518 void(*xDestroy)(void*) /* Module destructor function */
7519);
7520
7521/*
7522** CAPI3REF: Virtual Table Instance Object
7523** KEYWORDS: sqlite3_vtab
7524**
7525** Every [virtual table module] implementation uses a subclass
7526** of this object to describe a particular instance
7527** of the [virtual table]. Each subclass will
7528** be tailored to the specific needs of the module implementation.
7529** The purpose of this superclass is to define certain fields that are
7530** common to all module implementations.
7531**
7532** ^Virtual tables methods can set an error message by assigning a
7533** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should
7534** take care that any prior string is freed by a call to [sqlite3_free()]
7535** prior to assigning a new string to zErrMsg. ^After the error message
7536** is delivered up to the client application, the string will be automatically
7537** freed by sqlite3_free() and the zErrMsg field will be zeroed.
7538*/
7539struct sqlite3_vtab {
7540 const sqlite3_module *pModule; /* The module for this virtual table */
7541 int nRef; /* Number of open cursors */
7542 char *zErrMsg; /* Error message from sqlite3_mprintf() */
7543 /* Virtual table implementations will typically add additional fields */
7544};
7545
7546/*
7547** CAPI3REF: Virtual Table Cursor Object
7548** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
7549**
7550** Every [virtual table module] implementation uses a subclass of the
7551** following structure to describe cursors that point into the
7552** [virtual table] and are used
7553** to loop through the virtual table. Cursors are created using the
7554** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
7555** by the [sqlite3_module.xClose | xClose] method. Cursors are used
7556** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
7557** of the module. Each module implementation will define
7558** the content of a cursor structure to suit its own needs.
7559**
7560** This superclass exists in order to define fields of the cursor that
7561** are common to all implementations.
7562*/
7563struct sqlite3_vtab_cursor {
7564 sqlite3_vtab *pVtab; /* Virtual table of this cursor */
7565 /* Virtual table implementations will typically add additional fields */
7566};
7567
7568/*
7569** CAPI3REF: Declare The Schema Of A Virtual Table
7570**
7571** ^The [xCreate] and [xConnect] methods of a
7572** [virtual table module] call this interface
7573** to declare the format (the names and datatypes of the columns) of
7574** the virtual tables they implement.
7575*/
7576SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
7577
7578/*
7579** CAPI3REF: Overload A Function For A Virtual Table
7580** METHOD: sqlite3
7581**
7582** ^(Virtual tables can provide alternative implementations of functions
7583** using the [xFindFunction] method of the [virtual table module].
7584** But global versions of those functions
7585** must exist in order to be overloaded.)^
7586**
7587** ^(This API makes sure a global version of a function with a particular
7588** name and number of parameters exists. If no such function exists
7589** before this API is called, a new function is created.)^ ^The implementation
7590** of the new function always causes an exception to be thrown. So
7591** the new function is not good for anything by itself. Its only
7592** purpose is to be a placeholder function that can be overloaded
7593** by a [virtual table].
7594*/
7595SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
7596
7597/*
7598** The interface to the virtual-table mechanism defined above (back up
7599** to a comment remarkably similar to this one) is currently considered
7600** to be experimental. The interface might change in incompatible ways.
7601** If this is a problem for you, do not use the interface at this time.
7602**
7603** When the virtual-table mechanism stabilizes, we will declare the
7604** interface fixed, support it indefinitely, and remove this comment.
7605*/
7606
7607/*
7608** CAPI3REF: A Handle To An Open BLOB
7609** KEYWORDS: {BLOB handle} {BLOB handles}
7610**
7611** An instance of this object represents an open BLOB on which
7612** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
7613** ^Objects of this type are created by [sqlite3_blob_open()]
7614** and destroyed by [sqlite3_blob_close()].
7615** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
7616** can be used to read or write small subsections of the BLOB.
7617** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
7618*/
7619typedef struct sqlite3_blob sqlite3_blob;
7620
7621/*
7622** CAPI3REF: Open A BLOB For Incremental I/O
7623** METHOD: sqlite3
7624** CONSTRUCTOR: sqlite3_blob
7625**
7626** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
7627** in row iRow, column zColumn, table zTable in database zDb;
7628** in other words, the same BLOB that would be selected by:
7629**
7630** <pre>
7631** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
7632** </pre>)^
7633**
7634** ^(Parameter zDb is not the filename that contains the database, but
7635** rather the symbolic name of the database. For attached databases, this is
7636** the name that appears after the AS keyword in the [ATTACH] statement.
7637** For the main database file, the database name is "main". For TEMP
7638** tables, the database name is "temp".)^
7639**
7640** ^If the flags parameter is non-zero, then the BLOB is opened for read
7641** and write access. ^If the flags parameter is zero, the BLOB is opened for
7642** read-only access.
7643**
7644** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
7645** in *ppBlob. Otherwise an [error code] is returned and, unless the error
7646** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
7647** the API is not misused, it is always safe to call [sqlite3_blob_close()]
7648** on *ppBlob after this function it returns.
7649**
7650** This function fails with SQLITE_ERROR if any of the following are true:
7651** <ul>
7652** <li> ^(Database zDb does not exist)^,
7653** <li> ^(Table zTable does not exist within database zDb)^,
7654** <li> ^(Table zTable is a WITHOUT ROWID table)^,
7655** <li> ^(Column zColumn does not exist)^,
7656** <li> ^(Row iRow is not present in the table)^,
7657** <li> ^(The specified column of row iRow contains a value that is not
7658** a TEXT or BLOB value)^,
7659** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
7660** constraint and the blob is being opened for read/write access)^,
7661** <li> ^([foreign key constraints | Foreign key constraints] are enabled,
7662** column zColumn is part of a [child key] definition and the blob is
7663** being opened for read/write access)^.
7664** </ul>
7665**
7666** ^Unless it returns SQLITE_MISUSE, this function sets the
7667** [database connection] error code and message accessible via
7668** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
7669**
7670** A BLOB referenced by sqlite3_blob_open() may be read using the
7671** [sqlite3_blob_read()] interface and modified by using
7672** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
7673** different row of the same table using the [sqlite3_blob_reopen()]
7674** interface. However, the column, table, or database of a [BLOB handle]
7675** cannot be changed after the [BLOB handle] is opened.
7676**
7677** ^(If the row that a BLOB handle points to is modified by an
7678** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7679** then the BLOB handle is marked as "expired".
7680** This is true if any column of the row is changed, even a column
7681** other than the one the BLOB handle is open on.)^
7682** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
7683** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
7684** ^(Changes written into a BLOB prior to the BLOB expiring are not
7685** rolled back by the expiration of the BLOB. Such changes will eventually
7686** commit if the transaction continues to completion.)^
7687**
7688** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
7689** the opened blob. ^The size of a blob may not be changed by this
7690** interface. Use the [UPDATE] SQL command to change the size of a
7691** blob.
7692**
7693** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
7694** and the built-in [zeroblob] SQL function may be used to create a
7695** zero-filled blob to read or write using the incremental-blob interface.
7696**
7697** To avoid a resource leak, every open [BLOB handle] should eventually
7698** be released by a call to [sqlite3_blob_close()].
7699**
7700** See also: [sqlite3_blob_close()],
7701** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
7702** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
7703*/
7704SQLITE_API int sqlite3_blob_open(
7705 sqlite3*,
7706 const char *zDb,
7707 const char *zTable,
7708 const char *zColumn,
7709 sqlite3_int64 iRow,
7710 int flags,
7711 sqlite3_blob **ppBlob
7712);
7713
7714/*
7715** CAPI3REF: Move a BLOB Handle to a New Row
7716** METHOD: sqlite3_blob
7717**
7718** ^This function is used to move an existing [BLOB handle] so that it points
7719** to a different row of the same database table. ^The new row is identified
7720** by the rowid value passed as the second argument. Only the row can be
7721** changed. ^The database, table and column on which the blob handle is open
7722** remain the same. Moving an existing [BLOB handle] to a new row is
7723** faster than closing the existing handle and opening a new one.
7724**
7725** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7726** it must exist and there must be either a blob or text value stored in
7727** the nominated column.)^ ^If the new row is not present in the table, or if
7728** it does not contain a blob or text value, or if another error occurs, an
7729** SQLite error code is returned and the blob handle is considered aborted.
7730** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
7731** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
7732** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
7733** always returns zero.
7734**
7735** ^This function sets the database handle error code and message.
7736*/
7737SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
7738
7739/*
7740** CAPI3REF: Close A BLOB Handle
7741** DESTRUCTOR: sqlite3_blob
7742**
7743** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
7744** unconditionally. Even if this routine returns an error code, the
7745** handle is still closed.)^
7746**
7747** ^If the blob handle being closed was opened for read-write access, and if
7748** the database is in auto-commit mode and there are no other open read-write
7749** blob handles or active write statements, the current transaction is
7750** committed. ^If an error occurs while committing the transaction, an error
7751** code is returned and the transaction rolled back.
7752**
7753** Calling this function with an argument that is not a NULL pointer or an
7754** open blob handle results in undefined behaviour. ^Calling this routine
7755** with a null pointer (such as would be returned by a failed call to
7756** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7757** is passed a valid open blob handle, the values returned by the
7758** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
7759*/
7760SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
7761
7762/*
7763** CAPI3REF: Return The Size Of An Open BLOB
7764** METHOD: sqlite3_blob
7765**
7766** ^Returns the size in bytes of the BLOB accessible via the
7767** successfully opened [BLOB handle] in its only argument. ^The
7768** incremental blob I/O routines can only read or overwriting existing
7769** blob content; they cannot change the size of a blob.
7770**
7771** This routine only works on a [BLOB handle] which has been created
7772** by a prior successful call to [sqlite3_blob_open()] and which has not
7773** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7774** to this routine results in undefined and probably undesirable behavior.
7775*/
7776SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
7777
7778/*
7779** CAPI3REF: Read Data From A BLOB Incrementally
7780** METHOD: sqlite3_blob
7781**
7782** ^(This function is used to read data from an open [BLOB handle] into a
7783** caller-supplied buffer. N bytes of data are copied into buffer Z
7784** from the open BLOB, starting at offset iOffset.)^
7785**
7786** ^If offset iOffset is less than N bytes from the end of the BLOB,
7787** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is
7788** less than zero, [SQLITE_ERROR] is returned and no data is read.
7789** ^The size of the blob (and hence the maximum value of N+iOffset)
7790** can be determined using the [sqlite3_blob_bytes()] interface.
7791**
7792** ^An attempt to read from an expired [BLOB handle] fails with an
7793** error code of [SQLITE_ABORT].
7794**
7795** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
7796** Otherwise, an [error code] or an [extended error code] is returned.)^
7797**
7798** This routine only works on a [BLOB handle] which has been created
7799** by a prior successful call to [sqlite3_blob_open()] and which has not
7800** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7801** to this routine results in undefined and probably undesirable behavior.
7802**
7803** See also: [sqlite3_blob_write()].
7804*/
7805SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
7806
7807/*
7808** CAPI3REF: Write Data Into A BLOB Incrementally
7809** METHOD: sqlite3_blob
7810**
7811** ^(This function is used to write data into an open [BLOB handle] from a
7812** caller-supplied buffer. N bytes of data are copied from the buffer Z
7813** into the open BLOB, starting at offset iOffset.)^
7814**
7815** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
7816** Otherwise, an [error code] or an [extended error code] is returned.)^
7817** ^Unless SQLITE_MISUSE is returned, this function sets the
7818** [database connection] error code and message accessible via
7819** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
7820**
7821** ^If the [BLOB handle] passed as the first argument was not opened for
7822** writing (the flags parameter to [sqlite3_blob_open()] was zero),
7823** this function returns [SQLITE_READONLY].
7824**
7825** This function may only modify the contents of the BLOB; it is
7826** not possible to increase the size of a BLOB using this API.
7827** ^If offset iOffset is less than N bytes from the end of the BLOB,
7828** [SQLITE_ERROR] is returned and no data is written. The size of the
7829** BLOB (and hence the maximum value of N+iOffset) can be determined
7830** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
7831** than zero [SQLITE_ERROR] is returned and no data is written.
7832**
7833** ^An attempt to write to an expired [BLOB handle] fails with an
7834** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
7835** before the [BLOB handle] expired are not rolled back by the
7836** expiration of the handle, though of course those changes might
7837** have been overwritten by the statement that expired the BLOB handle
7838** or by other independent statements.
7839**
7840** This routine only works on a [BLOB handle] which has been created
7841** by a prior successful call to [sqlite3_blob_open()] and which has not
7842** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7843** to this routine results in undefined and probably undesirable behavior.
7844**
7845** See also: [sqlite3_blob_read()].
7846*/
7847SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
7848
7849/*
7850** CAPI3REF: Virtual File System Objects
7851**
7852** A virtual filesystem (VFS) is an [sqlite3_vfs] object
7853** that SQLite uses to interact
7854** with the underlying operating system. Most SQLite builds come with a
7855** single default VFS that is appropriate for the host computer.
7856** New VFSes can be registered and existing VFSes can be unregistered.
7857** The following interfaces are provided.
7858**
7859** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
7860** ^Names are case sensitive.
7861** ^Names are zero-terminated UTF-8 strings.
7862** ^If there is no match, a NULL pointer is returned.
7863** ^If zVfsName is NULL then the default VFS is returned.
7864**
7865** ^New VFSes are registered with sqlite3_vfs_register().
7866** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
7867** ^The same VFS can be registered multiple times without injury.
7868** ^To make an existing VFS into the default VFS, register it again
7869** with the makeDflt flag set. If two different VFSes with the
7870** same name are registered, the behavior is undefined. If a
7871** VFS is registered with a name that is NULL or an empty string,
7872** then the behavior is undefined.
7873**
7874** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
7875** ^(If the default VFS is unregistered, another VFS is chosen as
7876** the default. The choice for the new VFS is arbitrary.)^
7877*/
7878SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
7879SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
7880SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
7881
7882/*
7883** CAPI3REF: Mutexes
7884**
7885** The SQLite core uses these routines for thread
7886** synchronization. Though they are intended for internal
7887** use by SQLite, code that links against SQLite is
7888** permitted to use any of these routines.
7889**
7890** The SQLite source code contains multiple implementations
7891** of these mutex routines. An appropriate implementation
7892** is selected automatically at compile-time. The following
7893** implementations are available in the SQLite core:
7894**
7895** <ul>
7896** <li> SQLITE_MUTEX_PTHREADS
7897** <li> SQLITE_MUTEX_W32
7898** <li> SQLITE_MUTEX_NOOP
7899** </ul>
7900**
7901** The SQLITE_MUTEX_NOOP implementation is a set of routines
7902** that does no real locking and is appropriate for use in
7903** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
7904** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
7905** and Windows.
7906**
7907** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
7908** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
7909** implementation is included with the library. In this case the
7910** application must supply a custom mutex implementation using the
7911** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
7912** before calling sqlite3_initialize() or any other public sqlite3_
7913** function that calls sqlite3_initialize().
7914**
7915** ^The sqlite3_mutex_alloc() routine allocates a new
7916** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc()
7917** routine returns NULL if it is unable to allocate the requested
7918** mutex. The argument to sqlite3_mutex_alloc() must one of these
7919** integer constants:
7920**
7921** <ul>
7922** <li> SQLITE_MUTEX_FAST
7923** <li> SQLITE_MUTEX_RECURSIVE
7924** <li> SQLITE_MUTEX_STATIC_MASTER
7925** <li> SQLITE_MUTEX_STATIC_MEM
7926** <li> SQLITE_MUTEX_STATIC_OPEN
7927** <li> SQLITE_MUTEX_STATIC_PRNG
7928** <li> SQLITE_MUTEX_STATIC_LRU
7929** <li> SQLITE_MUTEX_STATIC_PMEM
7930** <li> SQLITE_MUTEX_STATIC_APP1
7931** <li> SQLITE_MUTEX_STATIC_APP2
7932** <li> SQLITE_MUTEX_STATIC_APP3
7933** <li> SQLITE_MUTEX_STATIC_VFS1
7934** <li> SQLITE_MUTEX_STATIC_VFS2
7935** <li> SQLITE_MUTEX_STATIC_VFS3
7936** </ul>
7937**
7938** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
7939** cause sqlite3_mutex_alloc() to create
7940** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
7941** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
7942** The mutex implementation does not need to make a distinction
7943** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
7944** not want to. SQLite will only request a recursive mutex in
7945** cases where it really needs one. If a faster non-recursive mutex
7946** implementation is available on the host platform, the mutex subsystem
7947** might return such a mutex in response to SQLITE_MUTEX_FAST.
7948**
7949** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
7950** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
7951** a pointer to a static preexisting mutex. ^Nine static mutexes are
7952** used by the current version of SQLite. Future versions of SQLite
7953** may add additional static mutexes. Static mutexes are for internal
7954** use by SQLite only. Applications that use SQLite mutexes should
7955** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
7956** SQLITE_MUTEX_RECURSIVE.
7957**
7958** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
7959** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
7960** returns a different mutex on every call. ^For the static
7961** mutex types, the same mutex is returned on every call that has
7962** the same type number.
7963**
7964** ^The sqlite3_mutex_free() routine deallocates a previously
7965** allocated dynamic mutex. Attempting to deallocate a static
7966** mutex results in undefined behavior.
7967**
7968** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
7969** to enter a mutex. ^If another thread is already within the mutex,
7970** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
7971** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
7972** upon successful entry. ^(Mutexes created using
7973** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
7974** In such cases, the
7975** mutex must be exited an equal number of times before another thread
7976** can enter.)^ If the same thread tries to enter any mutex other
7977** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
7978**
7979** ^(Some systems (for example, Windows 95) do not support the operation
7980** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
7981** will always return SQLITE_BUSY. The SQLite core only ever uses
7982** sqlite3_mutex_try() as an optimization so this is acceptable
7983** behavior.)^
7984**
7985** ^The sqlite3_mutex_leave() routine exits a mutex that was
7986** previously entered by the same thread. The behavior
7987** is undefined if the mutex is not currently entered by the
7988** calling thread or is not currently allocated.
7989**
7990** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
7991** sqlite3_mutex_leave() is a NULL pointer, then all three routines
7992** behave as no-ops.
7993**
7994** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
7995*/
7996SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
7997SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
7998SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
7999SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
8000SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
8001
8002/*
8003** CAPI3REF: Mutex Methods Object
8004**
8005** An instance of this structure defines the low-level routines
8006** used to allocate and use mutexes.
8007**
8008** Usually, the default mutex implementations provided by SQLite are
8009** sufficient, however the application has the option of substituting a custom
8010** implementation for specialized deployments or systems for which SQLite
8011** does not provide a suitable implementation. In this case, the application
8012** creates and populates an instance of this structure to pass
8013** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
8014** Additionally, an instance of this structure can be used as an
8015** output variable when querying the system for the current mutex
8016** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
8017**
8018** ^The xMutexInit method defined by this structure is invoked as
8019** part of system initialization by the sqlite3_initialize() function.
8020** ^The xMutexInit routine is called by SQLite exactly once for each
8021** effective call to [sqlite3_initialize()].
8022**
8023** ^The xMutexEnd method defined by this structure is invoked as
8024** part of system shutdown by the sqlite3_shutdown() function. The
8025** implementation of this method is expected to release all outstanding
8026** resources obtained by the mutex methods implementation, especially
8027** those obtained by the xMutexInit method. ^The xMutexEnd()
8028** interface is invoked exactly once for each call to [sqlite3_shutdown()].
8029**
8030** ^(The remaining seven methods defined by this structure (xMutexAlloc,
8031** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
8032** xMutexNotheld) implement the following interfaces (respectively):
8033**
8034** <ul>
8035** <li> [sqlite3_mutex_alloc()] </li>
8036** <li> [sqlite3_mutex_free()] </li>
8037** <li> [sqlite3_mutex_enter()] </li>
8038** <li> [sqlite3_mutex_try()] </li>
8039** <li> [sqlite3_mutex_leave()] </li>
8040** <li> [sqlite3_mutex_held()] </li>
8041** <li> [sqlite3_mutex_notheld()] </li>
8042** </ul>)^
8043**
8044** The only difference is that the public sqlite3_XXX functions enumerated
8045** above silently ignore any invocations that pass a NULL pointer instead
8046** of a valid mutex handle. The implementations of the methods defined
8047** by this structure are not required to handle this case, the results
8048** of passing a NULL pointer instead of a valid mutex handle are undefined
8049** (i.e. it is acceptable to provide an implementation that segfaults if
8050** it is passed a NULL pointer).
8051**
8052** The xMutexInit() method must be threadsafe. It must be harmless to
8053** invoke xMutexInit() multiple times within the same process and without
8054** intervening calls to xMutexEnd(). Second and subsequent calls to
8055** xMutexInit() must be no-ops.
8056**
8057** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
8058** and its associates). Similarly, xMutexAlloc() must not use SQLite memory
8059** allocation for a static mutex. ^However xMutexAlloc() may use SQLite
8060** memory allocation for a fast or recursive mutex.
8061**
8062** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
8063** called, but only if the prior call to xMutexInit returned SQLITE_OK.
8064** If xMutexInit fails in any way, it is expected to clean up after itself
8065** prior to returning.
8066*/
8067typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
8068struct sqlite3_mutex_methods {
8069 int (*xMutexInit)(void);
8070 int (*xMutexEnd)(void);
8071 sqlite3_mutex *(*xMutexAlloc)(int);
8072 void (*xMutexFree)(sqlite3_mutex *);
8073 void (*xMutexEnter)(sqlite3_mutex *);
8074 int (*xMutexTry)(sqlite3_mutex *);
8075 void (*xMutexLeave)(sqlite3_mutex *);
8076 int (*xMutexHeld)(sqlite3_mutex *);
8077 int (*xMutexNotheld)(sqlite3_mutex *);
8078};
8079
8080/*
8081** CAPI3REF: Mutex Verification Routines
8082**
8083** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
8084** are intended for use inside assert() statements. The SQLite core
8085** never uses these routines except inside an assert() and applications
8086** are advised to follow the lead of the core. The SQLite core only
8087** provides implementations for these routines when it is compiled
8088** with the SQLITE_DEBUG flag. External mutex implementations
8089** are only required to provide these routines if SQLITE_DEBUG is
8090** defined and if NDEBUG is not defined.
8091**
8092** These routines should return true if the mutex in their argument
8093** is held or not held, respectively, by the calling thread.
8094**
8095** The implementation is not required to provide versions of these
8096** routines that actually work. If the implementation does not provide working
8097** versions of these routines, it should at least provide stubs that always
8098** return true so that one does not get spurious assertion failures.
8099**
8100** If the argument to sqlite3_mutex_held() is a NULL pointer then
8101** the routine should return 1. This seems counter-intuitive since
8102** clearly the mutex cannot be held if it does not exist. But
8103** the reason the mutex does not exist is because the build is not
8104** using mutexes. And we do not want the assert() containing the
8105** call to sqlite3_mutex_held() to fail, so a non-zero return is
8106** the appropriate thing to do. The sqlite3_mutex_notheld()
8107** interface should also return 1 when given a NULL pointer.
8108*/
8109#ifndef NDEBUG
8110SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
8111SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
8112#endif
8113
8114/*
8115** CAPI3REF: Mutex Types
8116**
8117** The [sqlite3_mutex_alloc()] interface takes a single argument
8118** which is one of these integer constants.
8119**
8120** The set of static mutexes may change from one SQLite release to the
8121** next. Applications that override the built-in mutex logic must be
8122** prepared to accommodate additional static mutexes.
8123*/
8124#define SQLITE_MUTEX_FAST 0
8125#define SQLITE_MUTEX_RECURSIVE 1
8126#define SQLITE_MUTEX_STATIC_MASTER 2
8127#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
8128#define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
8129#define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
8130#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_randomness() */
8131#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
8132#define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
8133#define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
8134#define SQLITE_MUTEX_STATIC_APP1 8 /* For use by application */
8135#define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */
8136#define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */
8137#define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8138#define SQLITE_MUTEX_STATIC_VFS2 12 /* For use by extension VFS */
8139#define SQLITE_MUTEX_STATIC_VFS3 13 /* For use by application VFS */
8140
8141/*
8142** CAPI3REF: Retrieve the mutex for a database connection
8143** METHOD: sqlite3
8144**
8145** ^This interface returns a pointer the [sqlite3_mutex] object that
8146** serializes access to the [database connection] given in the argument
8147** when the [threading mode] is Serialized.
8148** ^If the [threading mode] is Single-thread or Multi-thread then this
8149** routine returns a NULL pointer.
8150*/
8151SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
8152
8153/*
8154** CAPI3REF: Low-Level Control Of Database Files
8155** METHOD: sqlite3
8156** KEYWORDS: {file control}
8157**
8158** ^The [sqlite3_file_control()] interface makes a direct call to the
8159** xFileControl method for the [sqlite3_io_methods] object associated
8160** with a particular database identified by the second argument. ^The
8161** name of the database is "main" for the main database or "temp" for the
8162** TEMP database, or the name that appears after the AS keyword for
8163** databases that are added using the [ATTACH] SQL command.
8164** ^A NULL pointer can be used in place of "main" to refer to the
8165** main database file.
8166** ^The third and fourth parameters to this routine
8167** are passed directly through to the second and third parameters of
8168** the xFileControl method. ^The return value of the xFileControl
8169** method becomes the return value of this routine.
8170**
8171** A few opcodes for [sqlite3_file_control()] are handled directly
8172** by the SQLite core and never invoke the
8173** sqlite3_io_methods.xFileControl method.
8174** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
8175** a pointer to the underlying [sqlite3_file] object to be written into
8176** the space pointed to by the 4th parameter. The
8177** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
8178** the [sqlite3_file] object associated with the journal file instead of
8179** the main database. The [SQLITE_FCNTL_VFS_POINTER] opcode returns
8180** a pointer to the underlying [sqlite3_vfs] object for the file.
8181** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
8182** from the pager.
8183**
8184** ^If the second parameter (zDbName) does not match the name of any
8185** open database file, then SQLITE_ERROR is returned. ^This error
8186** code is not remembered and will not be recalled by [sqlite3_errcode()]
8187** or [sqlite3_errmsg()]. The underlying xFileControl method might
8188** also return SQLITE_ERROR. There is no way to distinguish between
8189** an incorrect zDbName and an SQLITE_ERROR return from the underlying
8190** xFileControl method.
8191**
8192** See also: [file control opcodes]
8193*/
8194SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
8195
8196/*
8197** CAPI3REF: Testing Interface
8198**
8199** ^The sqlite3_test_control() interface is used to read out internal
8200** state of SQLite and to inject faults into SQLite for testing
8201** purposes. ^The first parameter is an operation code that determines
8202** the number, meaning, and operation of all subsequent parameters.
8203**
8204** This interface is not for use by applications. It exists solely
8205** for verifying the correct operation of the SQLite library. Depending
8206** on how the SQLite library is compiled, this interface might not exist.
8207**
8208** The details of the operation codes, their meanings, the parameters
8209** they take, and what they do are all subject to change without notice.
8210** Unlike most of the SQLite API, this function is not guaranteed to
8211** operate consistently from one release to the next.
8212*/
8213SQLITE_API int sqlite3_test_control(int op, ...);
8214
8215/*
8216** CAPI3REF: Testing Interface Operation Codes
8217**
8218** These constants are the valid operation code parameters used
8219** as the first argument to [sqlite3_test_control()].
8220**
8221** These parameters and their meanings are subject to change
8222** without notice. These values are for testing purposes only.
8223** Applications should not use any of these parameters or the
8224** [sqlite3_test_control()] interface.
8225*/
8226#define SQLITE_TESTCTRL_FIRST 5
8227#define SQLITE_TESTCTRL_PRNG_SAVE 5
8228#define SQLITE_TESTCTRL_PRNG_RESTORE 6
8229#define SQLITE_TESTCTRL_PRNG_RESET 7
8230#define SQLITE_TESTCTRL_BITVEC_TEST 8
8231#define SQLITE_TESTCTRL_FAULT_INSTALL 9
8232#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10
8233#define SQLITE_TESTCTRL_PENDING_BYTE 11
8234#define SQLITE_TESTCTRL_ASSERT 12
8235#define SQLITE_TESTCTRL_ALWAYS 13
8236#define SQLITE_TESTCTRL_RESERVE 14
8237#define SQLITE_TESTCTRL_OPTIMIZATIONS 15
8238#define SQLITE_TESTCTRL_ISKEYWORD 16 /* NOT USED */
8239#define SQLITE_TESTCTRL_SCRATCHMALLOC 17 /* NOT USED */
8240#define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
8241#define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */
8242#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD 19
8243#define SQLITE_TESTCTRL_NEVER_CORRUPT 20
8244#define SQLITE_TESTCTRL_VDBE_COVERAGE 21
8245#define SQLITE_TESTCTRL_BYTEORDER 22
8246#define SQLITE_TESTCTRL_ISINIT 23
8247#define SQLITE_TESTCTRL_SORTER_MMAP 24
8248#define SQLITE_TESTCTRL_IMPOSTER 25
8249#define SQLITE_TESTCTRL_PARSER_COVERAGE 26
8250#define SQLITE_TESTCTRL_LAST 26 /* Largest TESTCTRL */
8251
8252/*
8253** CAPI3REF: SQL Keyword Checking
8254**
8255** These routines provide access to the set of SQL language keywords
8256** recognized by SQLite. Applications can uses these routines to determine
8257** whether or not a specific identifier needs to be escaped (for example,
8258** by enclosing in double-quotes) so as not to confuse the parser.
8259**
8260** The sqlite3_keyword_count() interface returns the number of distinct
8261** keywords understood by SQLite.
8262**
8263** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8264** makes *Z point to that keyword expressed as UTF8 and writes the number
8265** of bytes in the keyword into *L. The string that *Z points to is not
8266** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8267** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
8268** or L are NULL or invalid pointers then calls to
8269** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
8270**
8271** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
8272** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8273** if it is and zero if not.
8274**
8275** The parser used by SQLite is forgiving. It is often possible to use
8276** a keyword as an identifier as long as such use does not result in a
8277** parsing ambiguity. For example, the statement
8278** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
8279** creates a new table named "BEGIN" with three columns named
8280** "REPLACE", "PRAGMA", and "END". Nevertheless, best practice is to avoid
8281** using keywords as identifiers. Common techniques used to avoid keyword
8282** name collisions include:
8283** <ul>
8284** <li> Put all identifier names inside double-quotes. This is the official
8285** SQL way to escape identifier names.
8286** <li> Put identifier names inside &#91;...&#93;. This is not standard SQL,
8287** but it is what SQL Server does and so lots of programmers use this
8288** technique.
8289** <li> Begin every identifier with the letter "Z" as no SQL keywords start
8290** with "Z".
8291** <li> Include a digit somewhere in every identifier name.
8292** </ul>
8293**
8294** Note that the number of keywords understood by SQLite can depend on
8295** compile-time options. For example, "VACUUM" is not a keyword if
8296** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8297** new keywords may be added to future releases of SQLite.
8298*/
8299SQLITE_API int sqlite3_keyword_count(void);
8300SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
8301SQLITE_API int sqlite3_keyword_check(const char*,int);
8302
8303/*
8304** CAPI3REF: Dynamic String Object
8305** KEYWORDS: {dynamic string}
8306**
8307** An instance of the sqlite3_str object contains a dynamically-sized
8308** string under construction.
8309**
8310** The lifecycle of an sqlite3_str object is as follows:
8311** <ol>
8312** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
8313** <li> ^Text is appended to the sqlite3_str object using various
8314** methods, such as [sqlite3_str_appendf()].
8315** <li> ^The sqlite3_str object is destroyed and the string it created
8316** is returned using the [sqlite3_str_finish()] interface.
8317** </ol>
8318*/
8319typedef struct sqlite3_str sqlite3_str;
8320
8321/*
8322** CAPI3REF: Create A New Dynamic String Object
8323** CONSTRUCTOR: sqlite3_str
8324**
8325** ^The [sqlite3_str_new(D)] interface allocates and initializes
8326** a new [sqlite3_str] object. To avoid memory leaks, the object returned by
8327** [sqlite3_str_new()] must be freed by a subsequent call to
8328** [sqlite3_str_finish(X)].
8329**
8330** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
8331** valid [sqlite3_str] object, though in the event of an out-of-memory
8332** error the returned object might be a special singleton that will
8333** silently reject new text, always return SQLITE_NOMEM from
8334** [sqlite3_str_errcode()], always return 0 for
8335** [sqlite3_str_length()], and always return NULL from
8336** [sqlite3_str_finish(X)]. It is always safe to use the value
8337** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
8338** to any of the other [sqlite3_str] methods.
8339**
8340** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
8341** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
8342** length of the string contained in the [sqlite3_str] object will be
8343** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
8344** of [SQLITE_MAX_LENGTH].
8345*/
8346SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
8347
8348/*
8349** CAPI3REF: Finalize A Dynamic String
8350** DESTRUCTOR: sqlite3_str
8351**
8352** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
8353** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
8354** that contains the constructed string. The calling application should
8355** pass the returned value to [sqlite3_free()] to avoid a memory leak.
8356** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
8357** errors were encountered during construction of the string. ^The
8358** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
8359** string in [sqlite3_str] object X is zero bytes long.
8360*/
8361SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
8362
8363/*
8364** CAPI3REF: Add Content To A Dynamic String
8365** METHOD: sqlite3_str
8366**
8367** These interfaces add content to an sqlite3_str object previously obtained
8368** from [sqlite3_str_new()].
8369**
8370** ^The [sqlite3_str_appendf(X,F,...)] and
8371** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8372** functionality of SQLite to append formatted text onto the end of
8373** [sqlite3_str] object X.
8374**
8375** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
8376** onto the end of the [sqlite3_str] object X. N must be non-negative.
8377** S must contain at least N non-zero bytes of content. To append a
8378** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8379** method instead.
8380**
8381** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
8382** zero-terminated string S onto the end of [sqlite3_str] object X.
8383**
8384** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
8385** single-byte character C onto the end of [sqlite3_str] object X.
8386** ^This method can be used, for example, to add whitespace indentation.
8387**
8388** ^The [sqlite3_str_reset(X)] method resets the string under construction
8389** inside [sqlite3_str] object X back to zero bytes in length.
8390**
8391** These methods do not return a result code. ^If an error occurs, that fact
8392** is recorded in the [sqlite3_str] object and can be recovered by a
8393** subsequent call to [sqlite3_str_errcode(X)].
8394*/
8395SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
8396SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
8397SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
8398SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
8399SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
8400SQLITE_API void sqlite3_str_reset(sqlite3_str*);
8401
8402/*
8403** CAPI3REF: Status Of A Dynamic String
8404** METHOD: sqlite3_str
8405**
8406** These interfaces return the current status of an [sqlite3_str] object.
8407**
8408** ^If any prior errors have occurred while constructing the dynamic string
8409** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
8410** an appropriate error code. ^The [sqlite3_str_errcode(X)] method returns
8411** [SQLITE_NOMEM] following any out-of-memory error, or
8412** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
8413** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
8414**
8415** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
8416** of the dynamic string under construction in [sqlite3_str] object X.
8417** ^The length returned by [sqlite3_str_length(X)] does not include the
8418** zero-termination byte.
8419**
8420** ^The [sqlite3_str_value(X)] method returns a pointer to the current
8421** content of the dynamic string under construction in X. The value
8422** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
8423** and might be freed or altered by any subsequent method on the same
8424** [sqlite3_str] object. Applications must not used the pointer returned
8425** [sqlite3_str_value(X)] after any subsequent method call on the same
8426** object. ^Applications may change the content of the string returned
8427** by [sqlite3_str_value(X)] as long as they do not write into any bytes
8428** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
8429** write any byte after any subsequent sqlite3_str method call.
8430*/
8431SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
8432SQLITE_API int sqlite3_str_length(sqlite3_str*);
8433SQLITE_API char *sqlite3_str_value(sqlite3_str*);
8434
8435/*
8436** CAPI3REF: SQLite Runtime Status
8437**
8438** ^These interfaces are used to retrieve runtime status information
8439** about the performance of SQLite, and optionally to reset various
8440** highwater marks. ^The first argument is an integer code for
8441** the specific parameter to measure. ^(Recognized integer codes
8442** are of the form [status parameters | SQLITE_STATUS_...].)^
8443** ^The current value of the parameter is returned into *pCurrent.
8444** ^The highest recorded value is returned in *pHighwater. ^If the
8445** resetFlag is true, then the highest record value is reset after
8446** *pHighwater is written. ^(Some parameters do not record the highest
8447** value. For those parameters
8448** nothing is written into *pHighwater and the resetFlag is ignored.)^
8449** ^(Other parameters record only the highwater mark and not the current
8450** value. For these latter parameters nothing is written into *pCurrent.)^
8451**
8452** ^The sqlite3_status() and sqlite3_status64() routines return
8453** SQLITE_OK on success and a non-zero [error code] on failure.
8454**
8455** If either the current value or the highwater mark is too large to
8456** be represented by a 32-bit integer, then the values returned by
8457** sqlite3_status() are undefined.
8458**
8459** See also: [sqlite3_db_status()]
8460*/
8461SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
8462SQLITE_API int sqlite3_status64(
8463 int op,
8464 sqlite3_int64 *pCurrent,
8465 sqlite3_int64 *pHighwater,
8466 int resetFlag
8467);
8468
8469
8470/*
8471** CAPI3REF: Status Parameters
8472** KEYWORDS: {status parameters}
8473**
8474** These integer constants designate various run-time status parameters
8475** that can be returned by [sqlite3_status()].
8476**
8477** <dl>
8478** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
8479** <dd>This parameter is the current amount of memory checked out
8480** using [sqlite3_malloc()], either directly or indirectly. The
8481** figure includes calls made to [sqlite3_malloc()] by the application
8482** and internal memory usage by the SQLite library. Auxiliary page-cache
8483** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
8484** this parameter. The amount returned is the sum of the allocation
8485** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
8486**
8487** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
8488** <dd>This parameter records the largest memory allocation request
8489** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
8490** internal equivalents). Only the value returned in the
8491** *pHighwater parameter to [sqlite3_status()] is of interest.
8492** The value written into the *pCurrent parameter is undefined.</dd>)^
8493**
8494** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
8495** <dd>This parameter records the number of separate memory allocations
8496** currently checked out.</dd>)^
8497**
8498** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
8499** <dd>This parameter returns the number of pages used out of the
8500** [pagecache memory allocator] that was configured using
8501** [SQLITE_CONFIG_PAGECACHE]. The
8502** value returned is in pages, not in bytes.</dd>)^
8503**
8504** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
8505** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
8506** <dd>This parameter returns the number of bytes of page cache
8507** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
8508** buffer and where forced to overflow to [sqlite3_malloc()]. The
8509** returned value includes allocations that overflowed because they
8510** where too large (they were larger than the "sz" parameter to
8511** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
8512** no space was left in the page cache.</dd>)^
8513**
8514** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
8515** <dd>This parameter records the largest memory allocation request
8516** handed to [pagecache memory allocator]. Only the value returned in the
8517** *pHighwater parameter to [sqlite3_status()] is of interest.
8518** The value written into the *pCurrent parameter is undefined.</dd>)^
8519**
8520** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
8521** <dd>No longer used.</dd>
8522**
8523** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
8524** <dd>No longer used.</dd>
8525**
8526** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
8527** <dd>No longer used.</dd>
8528**
8529** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
8530** <dd>The *pHighwater parameter records the deepest parser stack.
8531** The *pCurrent value is undefined. The *pHighwater value is only
8532** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
8533** </dl>
8534**
8535** New status parameters may be added from time to time.
8536*/
8537#define SQLITE_STATUS_MEMORY_USED 0
8538#define SQLITE_STATUS_PAGECACHE_USED 1
8539#define SQLITE_STATUS_PAGECACHE_OVERFLOW 2
8540#define SQLITE_STATUS_SCRATCH_USED 3 /* NOT USED */
8541#define SQLITE_STATUS_SCRATCH_OVERFLOW 4 /* NOT USED */
8542#define SQLITE_STATUS_MALLOC_SIZE 5
8543#define SQLITE_STATUS_PARSER_STACK 6
8544#define SQLITE_STATUS_PAGECACHE_SIZE 7
8545#define SQLITE_STATUS_SCRATCH_SIZE 8 /* NOT USED */
8546#define SQLITE_STATUS_MALLOC_COUNT 9
8547
8548/*
8549** CAPI3REF: Database Connection Status
8550** METHOD: sqlite3
8551**
8552** ^This interface is used to retrieve runtime status information
8553** about a single [database connection]. ^The first argument is the
8554** database connection object to be interrogated. ^The second argument
8555** is an integer constant, taken from the set of
8556** [SQLITE_DBSTATUS options], that
8557** determines the parameter to interrogate. The set of
8558** [SQLITE_DBSTATUS options] is likely
8559** to grow in future releases of SQLite.
8560**
8561** ^The current value of the requested parameter is written into *pCur
8562** and the highest instantaneous value is written into *pHiwtr. ^If
8563** the resetFlg is true, then the highest instantaneous value is
8564** reset back down to the current value.
8565**
8566** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
8567** non-zero [error code] on failure.
8568**
8569** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
8570*/
8571SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
8572
8573/*
8574** CAPI3REF: Status Parameters for database connections
8575** KEYWORDS: {SQLITE_DBSTATUS options}
8576**
8577** These constants are the available integer "verbs" that can be passed as
8578** the second argument to the [sqlite3_db_status()] interface.
8579**
8580** New verbs may be added in future releases of SQLite. Existing verbs
8581** might be discontinued. Applications should check the return code from
8582** [sqlite3_db_status()] to make sure that the call worked.
8583** The [sqlite3_db_status()] interface will return a non-zero error code
8584** if a discontinued or unsupported verb is invoked.
8585**
8586** <dl>
8587** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
8588** <dd>This parameter returns the number of lookaside memory slots currently
8589** checked out.</dd>)^
8590**
8591** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
8592** <dd>This parameter returns the number malloc attempts that were
8593** satisfied using lookaside memory. Only the high-water value is meaningful;
8594** the current value is always zero.)^
8595**
8596** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
8597** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
8598** <dd>This parameter returns the number malloc attempts that might have
8599** been satisfied using lookaside memory but failed due to the amount of
8600** memory requested being larger than the lookaside slot size.
8601** Only the high-water value is meaningful;
8602** the current value is always zero.)^
8603**
8604** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
8605** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
8606** <dd>This parameter returns the number malloc attempts that might have
8607** been satisfied using lookaside memory but failed due to all lookaside
8608** memory already being in use.
8609** Only the high-water value is meaningful;
8610** the current value is always zero.)^
8611**
8612** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
8613** <dd>This parameter returns the approximate number of bytes of heap
8614** memory used by all pager caches associated with the database connection.)^
8615** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
8616**
8617** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]]
8618** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
8619** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
8620** pager cache is shared between two or more connections the bytes of heap
8621** memory used by that pager cache is divided evenly between the attached
8622** connections.)^ In other words, if none of the pager caches associated
8623** with the database connection are shared, this request returns the same
8624** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are
8625** shared, the value returned by this call will be smaller than that returned
8626** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
8627** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
8628**
8629** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
8630** <dd>This parameter returns the approximate number of bytes of heap
8631** memory used to store the schema for all databases associated
8632** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8633** ^The full amount of memory used by the schemas is reported, even if the
8634** schema memory is shared with other database connections due to
8635** [shared cache mode] being enabled.
8636** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
8637**
8638** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
8639** <dd>This parameter returns the approximate number of bytes of heap
8640** and lookaside memory used by all prepared statements associated with
8641** the database connection.)^
8642** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
8643** </dd>
8644**
8645** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
8646** <dd>This parameter returns the number of pager cache hits that have
8647** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
8648** is always 0.
8649** </dd>
8650**
8651** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
8652** <dd>This parameter returns the number of pager cache misses that have
8653** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
8654** is always 0.
8655** </dd>
8656**
8657** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
8658** <dd>This parameter returns the number of dirty cache entries that have
8659** been written to disk. Specifically, the number of pages written to the
8660** wal file in wal mode databases, or the number of pages written to the
8661** database file in rollback mode databases. Any pages written as part of
8662** transaction rollback or database recovery operations are not included.
8663** If an IO or other error occurs while writing a page to disk, the effect
8664** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
8665** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
8666** </dd>
8667**
8668** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
8669** <dd>This parameter returns the number of dirty cache entries that have
8670** been written to disk in the middle of a transaction due to the page
8671** cache overflowing. Transactions are more efficient if they are written
8672** to disk all at once. When pages spill mid-transaction, that introduces
8673** additional overhead. This parameter can be used help identify
8674** inefficiencies that can be resolve by increasing the cache size.
8675** </dd>
8676**
8677** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
8678** <dd>This parameter returns zero for the current value if and only if
8679** all foreign key constraints (deferred or immediate) have been
8680** resolved.)^ ^The highwater mark is always 0.
8681** </dd>
8682** </dl>
8683*/
8684#define SQLITE_DBSTATUS_LOOKASIDE_USED 0
8685#define SQLITE_DBSTATUS_CACHE_USED 1
8686#define SQLITE_DBSTATUS_SCHEMA_USED 2
8687#define SQLITE_DBSTATUS_STMT_USED 3
8688#define SQLITE_DBSTATUS_LOOKASIDE_HIT 4
8689#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
8690#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6
8691#define SQLITE_DBSTATUS_CACHE_HIT 7
8692#define SQLITE_DBSTATUS_CACHE_MISS 8
8693#define SQLITE_DBSTATUS_CACHE_WRITE 9
8694#define SQLITE_DBSTATUS_DEFERRED_FKS 10
8695#define SQLITE_DBSTATUS_CACHE_USED_SHARED 11
8696#define SQLITE_DBSTATUS_CACHE_SPILL 12
8697#define SQLITE_DBSTATUS_MAX 12 /* Largest defined DBSTATUS */
8698
8699
8700/*
8701** CAPI3REF: Prepared Statement Status
8702** METHOD: sqlite3_stmt
8703**
8704** ^(Each prepared statement maintains various
8705** [SQLITE_STMTSTATUS counters] that measure the number
8706** of times it has performed specific operations.)^ These counters can
8707** be used to monitor the performance characteristics of the prepared
8708** statements. For example, if the number of table steps greatly exceeds
8709** the number of table searches or result rows, that would tend to indicate
8710** that the prepared statement is using a full table scan rather than
8711** an index.
8712**
8713** ^(This interface is used to retrieve and reset counter values from
8714** a [prepared statement]. The first argument is the prepared statement
8715** object to be interrogated. The second argument
8716** is an integer code for a specific [SQLITE_STMTSTATUS counter]
8717** to be interrogated.)^
8718** ^The current value of the requested counter is returned.
8719** ^If the resetFlg is true, then the counter is reset to zero after this
8720** interface call returns.
8721**
8722** See also: [sqlite3_status()] and [sqlite3_db_status()].
8723*/
8724SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
8725
8726/*
8727** CAPI3REF: Status Parameters for prepared statements
8728** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
8729**
8730** These preprocessor macros define integer codes that name counter
8731** values associated with the [sqlite3_stmt_status()] interface.
8732** The meanings of the various counters are as follows:
8733**
8734** <dl>
8735** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
8736** <dd>^This is the number of times that SQLite has stepped forward in
8737** a table as part of a full table scan. Large numbers for this counter
8738** may indicate opportunities for performance improvement through
8739** careful use of indices.</dd>
8740**
8741** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
8742** <dd>^This is the number of sort operations that have occurred.
8743** A non-zero value in this counter may indicate an opportunity to
8744** improvement performance through careful use of indices.</dd>
8745**
8746** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
8747** <dd>^This is the number of rows inserted into transient indices that
8748** were created automatically in order to help joins run faster.
8749** A non-zero value in this counter may indicate an opportunity to
8750** improvement performance by adding permanent indices that do not
8751** need to be reinitialized each time the statement is run.</dd>
8752**
8753** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
8754** <dd>^This is the number of virtual machine operations executed
8755** by the prepared statement if that number is less than or equal
8756** to 2147483647. The number of virtual machine operations can be
8757** used as a proxy for the total work done by the prepared statement.
8758** If the number of virtual machine operations exceeds 2147483647
8759** then the value returned by this statement status code is undefined.
8760**
8761** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
8762** <dd>^This is the number of times that the prepare statement has been
8763** automatically regenerated due to schema changes or change to
8764** [bound parameters] that might affect the query plan.
8765**
8766** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
8767** <dd>^This is the number of times that the prepared statement has
8768** been run. A single "run" for the purposes of this counter is one
8769** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
8770** The counter is incremented on the first [sqlite3_step()] call of each
8771** cycle.
8772**
8773** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
8774** <dd>^This is the approximate number of bytes of heap memory
8775** used to store the prepared statement. ^This value is not actually
8776** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
8777** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
8778** </dd>
8779** </dl>
8780*/
8781#define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
8782#define SQLITE_STMTSTATUS_SORT 2
8783#define SQLITE_STMTSTATUS_AUTOINDEX 3
8784#define SQLITE_STMTSTATUS_VM_STEP 4
8785#define SQLITE_STMTSTATUS_REPREPARE 5
8786#define SQLITE_STMTSTATUS_RUN 6
8787#define SQLITE_STMTSTATUS_MEMUSED 99
8788
8789/*
8790** CAPI3REF: Custom Page Cache Object
8791**
8792** The sqlite3_pcache type is opaque. It is implemented by
8793** the pluggable module. The SQLite core has no knowledge of
8794** its size or internal structure and never deals with the
8795** sqlite3_pcache object except by holding and passing pointers
8796** to the object.
8797**
8798** See [sqlite3_pcache_methods2] for additional information.
8799*/
8800typedef struct sqlite3_pcache sqlite3_pcache;
8801
8802/*
8803** CAPI3REF: Custom Page Cache Object
8804**
8805** The sqlite3_pcache_page object represents a single page in the
8806** page cache. The page cache will allocate instances of this
8807** object. Various methods of the page cache use pointers to instances
8808** of this object as parameters or as their return value.
8809**
8810** See [sqlite3_pcache_methods2] for additional information.
8811*/
8812typedef struct sqlite3_pcache_page sqlite3_pcache_page;
8813struct sqlite3_pcache_page {
8814 void *pBuf; /* The content of the page */
8815 void *pExtra; /* Extra information associated with the page */
8816};
8817
8818/*
8819** CAPI3REF: Application Defined Page Cache.
8820** KEYWORDS: {page cache}
8821**
8822** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
8823** register an alternative page cache implementation by passing in an
8824** instance of the sqlite3_pcache_methods2 structure.)^
8825** In many applications, most of the heap memory allocated by
8826** SQLite is used for the page cache.
8827** By implementing a
8828** custom page cache using this API, an application can better control
8829** the amount of memory consumed by SQLite, the way in which
8830** that memory is allocated and released, and the policies used to
8831** determine exactly which parts of a database file are cached and for
8832** how long.
8833**
8834** The alternative page cache mechanism is an
8835** extreme measure that is only needed by the most demanding applications.
8836** The built-in page cache is recommended for most uses.
8837**
8838** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
8839** internal buffer by SQLite within the call to [sqlite3_config]. Hence
8840** the application may discard the parameter after the call to
8841** [sqlite3_config()] returns.)^
8842**
8843** [[the xInit() page cache method]]
8844** ^(The xInit() method is called once for each effective
8845** call to [sqlite3_initialize()])^
8846** (usually only once during the lifetime of the process). ^(The xInit()
8847** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
8848** The intent of the xInit() method is to set up global data structures
8849** required by the custom page cache implementation.
8850** ^(If the xInit() method is NULL, then the
8851** built-in default page cache is used instead of the application defined
8852** page cache.)^
8853**
8854** [[the xShutdown() page cache method]]
8855** ^The xShutdown() method is called by [sqlite3_shutdown()].
8856** It can be used to clean up
8857** any outstanding resources before process shutdown, if required.
8858** ^The xShutdown() method may be NULL.
8859**
8860** ^SQLite automatically serializes calls to the xInit method,
8861** so the xInit method need not be threadsafe. ^The
8862** xShutdown method is only called from [sqlite3_shutdown()] so it does
8863** not need to be threadsafe either. All other methods must be threadsafe
8864** in multithreaded applications.
8865**
8866** ^SQLite will never invoke xInit() more than once without an intervening
8867** call to xShutdown().
8868**
8869** [[the xCreate() page cache methods]]
8870** ^SQLite invokes the xCreate() method to construct a new cache instance.
8871** SQLite will typically create one cache instance for each open database file,
8872** though this is not guaranteed. ^The
8873** first parameter, szPage, is the size in bytes of the pages that must
8874** be allocated by the cache. ^szPage will always a power of two. ^The
8875** second parameter szExtra is a number of bytes of extra storage
8876** associated with each page cache entry. ^The szExtra parameter will
8877** a number less than 250. SQLite will use the
8878** extra szExtra bytes on each page to store metadata about the underlying
8879** database page on disk. The value passed into szExtra depends
8880** on the SQLite version, the target platform, and how SQLite was compiled.
8881** ^The third argument to xCreate(), bPurgeable, is true if the cache being
8882** created will be used to cache database pages of a file stored on disk, or
8883** false if it is used for an in-memory database. The cache implementation
8884** does not have to do anything special based with the value of bPurgeable;
8885** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
8886** never invoke xUnpin() except to deliberately delete a page.
8887** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
8888** false will always have the "discard" flag set to true.
8889** ^Hence, a cache created with bPurgeable false will
8890** never contain any unpinned pages.
8891**
8892** [[the xCachesize() page cache method]]
8893** ^(The xCachesize() method may be called at any time by SQLite to set the
8894** suggested maximum cache-size (number of pages stored by) the cache
8895** instance passed as the first argument. This is the value configured using
8896** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable
8897** parameter, the implementation is not required to do anything with this
8898** value; it is advisory only.
8899**
8900** [[the xPagecount() page cache methods]]
8901** The xPagecount() method must return the number of pages currently
8902** stored in the cache, both pinned and unpinned.
8903**
8904** [[the xFetch() page cache methods]]
8905** The xFetch() method locates a page in the cache and returns a pointer to
8906** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
8907** The pBuf element of the returned sqlite3_pcache_page object will be a
8908** pointer to a buffer of szPage bytes used to store the content of a
8909** single database page. The pExtra element of sqlite3_pcache_page will be
8910** a pointer to the szExtra bytes of extra storage that SQLite has requested
8911** for each entry in the page cache.
8912**
8913** The page to be fetched is determined by the key. ^The minimum key value
8914** is 1. After it has been retrieved using xFetch, the page is considered
8915** to be "pinned".
8916**
8917** If the requested page is already in the page cache, then the page cache
8918** implementation must return a pointer to the page buffer with its content
8919** intact. If the requested page is not already in the cache, then the
8920** cache implementation should use the value of the createFlag
8921** parameter to help it determined what action to take:
8922**
8923** <table border=1 width=85% align=center>
8924** <tr><th> createFlag <th> Behavior when page is not already in cache
8925** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
8926** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
8927** Otherwise return NULL.
8928** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
8929** NULL if allocating a new page is effectively impossible.
8930** </table>
8931**
8932** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite
8933** will only use a createFlag of 2 after a prior call with a createFlag of 1
8934** failed.)^ In between the to xFetch() calls, SQLite may
8935** attempt to unpin one or more cache pages by spilling the content of
8936** pinned pages to disk and synching the operating system disk cache.
8937**
8938** [[the xUnpin() page cache method]]
8939** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
8940** as its second argument. If the third parameter, discard, is non-zero,
8941** then the page must be evicted from the cache.
8942** ^If the discard parameter is
8943** zero, then the page may be discarded or retained at the discretion of
8944** page cache implementation. ^The page cache implementation
8945** may choose to evict unpinned pages at any time.
8946**
8947** The cache must not perform any reference counting. A single
8948** call to xUnpin() unpins the page regardless of the number of prior calls
8949** to xFetch().
8950**
8951** [[the xRekey() page cache methods]]
8952** The xRekey() method is used to change the key value associated with the
8953** page passed as the second argument. If the cache
8954** previously contains an entry associated with newKey, it must be
8955** discarded. ^Any prior cache entry associated with newKey is guaranteed not
8956** to be pinned.
8957**
8958** When SQLite calls the xTruncate() method, the cache must discard all
8959** existing cache entries with page numbers (keys) greater than or equal
8960** to the value of the iLimit parameter passed to xTruncate(). If any
8961** of these pages are pinned, they are implicitly unpinned, meaning that
8962** they can be safely discarded.
8963**
8964** [[the xDestroy() page cache method]]
8965** ^The xDestroy() method is used to delete a cache allocated by xCreate().
8966** All resources associated with the specified cache should be freed. ^After
8967** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
8968** handle invalid, and will not use it with any other sqlite3_pcache_methods2
8969** functions.
8970**
8971** [[the xShrink() page cache method]]
8972** ^SQLite invokes the xShrink() method when it wants the page cache to
8973** free up as much of heap memory as possible. The page cache implementation
8974** is not obligated to free any memory, but well-behaved implementations should
8975** do their best.
8976*/
8977typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
8978struct sqlite3_pcache_methods2 {
8979 int iVersion;
8980 void *pArg;
8981 int (*xInit)(void*);
8982 void (*xShutdown)(void*);
8983 sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
8984 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
8985 int (*xPagecount)(sqlite3_pcache*);
8986 sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
8987 void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
8988 void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
8989 unsigned oldKey, unsigned newKey);
8990 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
8991 void (*xDestroy)(sqlite3_pcache*);
8992 void (*xShrink)(sqlite3_pcache*);
8993};
8994
8995/*
8996** This is the obsolete pcache_methods object that has now been replaced
8997** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
8998** retained in the header file for backwards compatibility only.
8999*/
9000typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
9001struct sqlite3_pcache_methods {
9002 void *pArg;
9003 int (*xInit)(void*);
9004 void (*xShutdown)(void*);
9005 sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
9006 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9007 int (*xPagecount)(sqlite3_pcache*);
9008 void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9009 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
9010 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
9011 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9012 void (*xDestroy)(sqlite3_pcache*);
9013};
9014
9015
9016/*
9017** CAPI3REF: Online Backup Object
9018**
9019** The sqlite3_backup object records state information about an ongoing
9020** online backup operation. ^The sqlite3_backup object is created by
9021** a call to [sqlite3_backup_init()] and is destroyed by a call to
9022** [sqlite3_backup_finish()].
9023**
9024** See Also: [Using the SQLite Online Backup API]
9025*/
9026typedef struct sqlite3_backup sqlite3_backup;
9027
9028/*
9029** CAPI3REF: Online Backup API.
9030**
9031** The backup API copies the content of one database into another.
9032** It is useful either for creating backups of databases or
9033** for copying in-memory databases to or from persistent files.
9034**
9035** See Also: [Using the SQLite Online Backup API]
9036**
9037** ^SQLite holds a write transaction open on the destination database file
9038** for the duration of the backup operation.
9039** ^The source database is read-locked only while it is being read;
9040** it is not locked continuously for the entire backup operation.
9041** ^Thus, the backup may be performed on a live source database without
9042** preventing other database connections from
9043** reading or writing to the source database while the backup is underway.
9044**
9045** ^(To perform a backup operation:
9046** <ol>
9047** <li><b>sqlite3_backup_init()</b> is called once to initialize the
9048** backup,
9049** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
9050** the data between the two databases, and finally
9051** <li><b>sqlite3_backup_finish()</b> is called to release all resources
9052** associated with the backup operation.
9053** </ol>)^
9054** There should be exactly one call to sqlite3_backup_finish() for each
9055** successful call to sqlite3_backup_init().
9056**
9057** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
9058**
9059** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
9060** [database connection] associated with the destination database
9061** and the database name, respectively.
9062** ^The database name is "main" for the main database, "temp" for the
9063** temporary database, or the name specified after the AS keyword in
9064** an [ATTACH] statement for an attached database.
9065** ^The S and M arguments passed to
9066** sqlite3_backup_init(D,N,S,M) identify the [database connection]
9067** and database name of the source database, respectively.
9068** ^The source and destination [database connections] (parameters S and D)
9069** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
9070** an error.
9071**
9072** ^A call to sqlite3_backup_init() will fail, returning NULL, if
9073** there is already a read or read-write transaction open on the
9074** destination database.
9075**
9076** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
9077** returned and an error code and error message are stored in the
9078** destination [database connection] D.
9079** ^The error code and message for the failed call to sqlite3_backup_init()
9080** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
9081** [sqlite3_errmsg16()] functions.
9082** ^A successful call to sqlite3_backup_init() returns a pointer to an
9083** [sqlite3_backup] object.
9084** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
9085** sqlite3_backup_finish() functions to perform the specified backup
9086** operation.
9087**
9088** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
9089**
9090** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
9091** the source and destination databases specified by [sqlite3_backup] object B.
9092** ^If N is negative, all remaining source pages are copied.
9093** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
9094** are still more pages to be copied, then the function returns [SQLITE_OK].
9095** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
9096** from source to destination, then it returns [SQLITE_DONE].
9097** ^If an error occurs while running sqlite3_backup_step(B,N),
9098** then an [error code] is returned. ^As well as [SQLITE_OK] and
9099** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
9100** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
9101** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
9102**
9103** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
9104** <ol>
9105** <li> the destination database was opened read-only, or
9106** <li> the destination database is using write-ahead-log journaling
9107** and the destination and source page sizes differ, or
9108** <li> the destination database is an in-memory database and the
9109** destination and source page sizes differ.
9110** </ol>)^
9111**
9112** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9113** the [sqlite3_busy_handler | busy-handler function]
9114** is invoked (if one is specified). ^If the
9115** busy-handler returns non-zero before the lock is available, then
9116** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
9117** sqlite3_backup_step() can be retried later. ^If the source
9118** [database connection]
9119** is being used to write to the source database when sqlite3_backup_step()
9120** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
9121** case the call to sqlite3_backup_step() can be retried later on. ^(If
9122** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
9123** [SQLITE_READONLY] is returned, then
9124** there is no point in retrying the call to sqlite3_backup_step(). These
9125** errors are considered fatal.)^ The application must accept
9126** that the backup operation has failed and pass the backup operation handle
9127** to the sqlite3_backup_finish() to release associated resources.
9128**
9129** ^The first call to sqlite3_backup_step() obtains an exclusive lock
9130** on the destination file. ^The exclusive lock is not released until either
9131** sqlite3_backup_finish() is called or the backup operation is complete
9132** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to
9133** sqlite3_backup_step() obtains a [shared lock] on the source database that
9134** lasts for the duration of the sqlite3_backup_step() call.
9135** ^Because the source database is not locked between calls to
9136** sqlite3_backup_step(), the source database may be modified mid-way
9137** through the backup process. ^If the source database is modified by an
9138** external process or via a database connection other than the one being
9139** used by the backup operation, then the backup will be automatically
9140** restarted by the next call to sqlite3_backup_step(). ^If the source
9141** database is modified by the using the same database connection as is used
9142** by the backup operation, then the backup database is automatically
9143** updated at the same time.
9144**
9145** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
9146**
9147** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
9148** application wishes to abandon the backup operation, the application
9149** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
9150** ^The sqlite3_backup_finish() interfaces releases all
9151** resources associated with the [sqlite3_backup] object.
9152** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
9153** active write-transaction on the destination database is rolled back.
9154** The [sqlite3_backup] object is invalid
9155** and may not be used following a call to sqlite3_backup_finish().
9156**
9157** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
9158** sqlite3_backup_step() errors occurred, regardless or whether or not
9159** sqlite3_backup_step() completed.
9160** ^If an out-of-memory condition or IO error occurred during any prior
9161** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
9162** sqlite3_backup_finish() returns the corresponding [error code].
9163**
9164** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
9165** is not a permanent error and does not affect the return value of
9166** sqlite3_backup_finish().
9167**
9168** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]]
9169** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
9170**
9171** ^The sqlite3_backup_remaining() routine returns the number of pages still
9172** to be backed up at the conclusion of the most recent sqlite3_backup_step().
9173** ^The sqlite3_backup_pagecount() routine returns the total number of pages
9174** in the source database at the conclusion of the most recent
9175** sqlite3_backup_step().
9176** ^(The values returned by these functions are only updated by
9177** sqlite3_backup_step(). If the source database is modified in a way that
9178** changes the size of the source database or the number of pages remaining,
9179** those changes are not reflected in the output of sqlite3_backup_pagecount()
9180** and sqlite3_backup_remaining() until after the next
9181** sqlite3_backup_step().)^
9182**
9183** <b>Concurrent Usage of Database Handles</b>
9184**
9185** ^The source [database connection] may be used by the application for other
9186** purposes while a backup operation is underway or being initialized.
9187** ^If SQLite is compiled and configured to support threadsafe database
9188** connections, then the source database connection may be used concurrently
9189** from within other threads.
9190**
9191** However, the application must guarantee that the destination
9192** [database connection] is not passed to any other API (by any thread) after
9193** sqlite3_backup_init() is called and before the corresponding call to
9194** sqlite3_backup_finish(). SQLite does not currently check to see
9195** if the application incorrectly accesses the destination [database connection]
9196** and so no error code is reported, but the operations may malfunction
9197** nevertheless. Use of the destination database connection while a
9198** backup is in progress might also also cause a mutex deadlock.
9199**
9200** If running in [shared cache mode], the application must
9201** guarantee that the shared cache used by the destination database
9202** is not accessed while the backup is running. In practice this means
9203** that the application must guarantee that the disk file being
9204** backed up to is not accessed by any connection within the process,
9205** not just the specific connection that was passed to sqlite3_backup_init().
9206**
9207** The [sqlite3_backup] object itself is partially threadsafe. Multiple
9208** threads may safely make multiple concurrent calls to sqlite3_backup_step().
9209** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
9210** APIs are not strictly speaking threadsafe. If they are invoked at the
9211** same time as another thread is invoking sqlite3_backup_step() it is
9212** possible that they return invalid values.
9213*/
9214SQLITE_API sqlite3_backup *sqlite3_backup_init(
9215 sqlite3 *pDest, /* Destination database handle */
9216 const char *zDestName, /* Destination database name */
9217 sqlite3 *pSource, /* Source database handle */
9218 const char *zSourceName /* Source database name */
9219);
9220SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
9221SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
9222SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
9223SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
9224
9225/*
9226** CAPI3REF: Unlock Notification
9227** METHOD: sqlite3
9228**
9229** ^When running in shared-cache mode, a database operation may fail with
9230** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9231** individual tables within the shared-cache cannot be obtained. See
9232** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9233** ^This API may be used to register a callback that SQLite will invoke
9234** when the connection currently holding the required lock relinquishes it.
9235** ^This API is only available if the library was compiled with the
9236** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9237**
9238** See Also: [Using the SQLite Unlock Notification Feature].
9239**
9240** ^Shared-cache locks are released when a database connection concludes
9241** its current transaction, either by committing it or rolling it back.
9242**
9243** ^When a connection (known as the blocked connection) fails to obtain a
9244** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9245** identity of the database connection (the blocking connection) that
9246** has locked the required resource is stored internally. ^After an
9247** application receives an SQLITE_LOCKED error, it may call the
9248** sqlite3_unlock_notify() method with the blocked connection handle as
9249** the first argument to register for a callback that will be invoked
9250** when the blocking connections current transaction is concluded. ^The
9251** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
9252** call that concludes the blocking connections transaction.
9253**
9254** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9255** there is a chance that the blocking connection will have already
9256** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
9257** If this happens, then the specified callback is invoked immediately,
9258** from within the call to sqlite3_unlock_notify().)^
9259**
9260** ^If the blocked connection is attempting to obtain a write-lock on a
9261** shared-cache table, and more than one other connection currently holds
9262** a read-lock on the same table, then SQLite arbitrarily selects one of
9263** the other connections to use as the blocking connection.
9264**
9265** ^(There may be at most one unlock-notify callback registered by a
9266** blocked connection. If sqlite3_unlock_notify() is called when the
9267** blocked connection already has a registered unlock-notify callback,
9268** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
9269** called with a NULL pointer as its second argument, then any existing
9270** unlock-notify callback is canceled. ^The blocked connections
9271** unlock-notify callback may also be canceled by closing the blocked
9272** connection using [sqlite3_close()].
9273**
9274** The unlock-notify callback is not reentrant. If an application invokes
9275** any sqlite3_xxx API functions from within an unlock-notify callback, a
9276** crash or deadlock may be the result.
9277**
9278** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
9279** returns SQLITE_OK.
9280**
9281** <b>Callback Invocation Details</b>
9282**
9283** When an unlock-notify callback is registered, the application provides a
9284** single void* pointer that is passed to the callback when it is invoked.
9285** However, the signature of the callback function allows SQLite to pass
9286** it an array of void* context pointers. The first argument passed to
9287** an unlock-notify callback is a pointer to an array of void* pointers,
9288** and the second is the number of entries in the array.
9289**
9290** When a blocking connections transaction is concluded, there may be
9291** more than one blocked connection that has registered for an unlock-notify
9292** callback. ^If two or more such blocked connections have specified the
9293** same callback function, then instead of invoking the callback function
9294** multiple times, it is invoked once with the set of void* context pointers
9295** specified by the blocked connections bundled together into an array.
9296** This gives the application an opportunity to prioritize any actions
9297** related to the set of unblocked database connections.
9298**
9299** <b>Deadlock Detection</b>
9300**
9301** Assuming that after registering for an unlock-notify callback a
9302** database waits for the callback to be issued before taking any further
9303** action (a reasonable assumption), then using this API may cause the
9304** application to deadlock. For example, if connection X is waiting for
9305** connection Y's transaction to be concluded, and similarly connection
9306** Y is waiting on connection X's transaction, then neither connection
9307** will proceed and the system may remain deadlocked indefinitely.
9308**
9309** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
9310** detection. ^If a given call to sqlite3_unlock_notify() would put the
9311** system in a deadlocked state, then SQLITE_LOCKED is returned and no
9312** unlock-notify callback is registered. The system is said to be in
9313** a deadlocked state if connection A has registered for an unlock-notify
9314** callback on the conclusion of connection B's transaction, and connection
9315** B has itself registered for an unlock-notify callback when connection
9316** A's transaction is concluded. ^Indirect deadlock is also detected, so
9317** the system is also considered to be deadlocked if connection B has
9318** registered for an unlock-notify callback on the conclusion of connection
9319** C's transaction, where connection C is waiting on connection A. ^Any
9320** number of levels of indirection are allowed.
9321**
9322** <b>The "DROP TABLE" Exception</b>
9323**
9324** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
9325** always appropriate to call sqlite3_unlock_notify(). There is however,
9326** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
9327** SQLite checks if there are any currently executing SELECT statements
9328** that belong to the same connection. If there are, SQLITE_LOCKED is
9329** returned. In this case there is no "blocking connection", so invoking
9330** sqlite3_unlock_notify() results in the unlock-notify callback being
9331** invoked immediately. If the application then re-attempts the "DROP TABLE"
9332** or "DROP INDEX" query, an infinite loop might be the result.
9333**
9334** One way around this problem is to check the extended error code returned
9335** by an sqlite3_step() call. ^(If there is a blocking connection, then the
9336** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
9337** the special "DROP TABLE/INDEX" case, the extended error code is just
9338** SQLITE_LOCKED.)^
9339*/
9340SQLITE_API int sqlite3_unlock_notify(
9341 sqlite3 *pBlocked, /* Waiting connection */
9342 void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */
9343 void *pNotifyArg /* Argument to pass to xNotify */
9344);
9345
9346
9347/*
9348** CAPI3REF: String Comparison
9349**
9350** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
9351** and extensions to compare the contents of two buffers containing UTF-8
9352** strings in a case-independent fashion, using the same definition of "case
9353** independence" that SQLite uses internally when comparing identifiers.
9354*/
9355SQLITE_API int sqlite3_stricmp(const char *, const char *);
9356SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
9357
9358/*
9359** CAPI3REF: String Globbing
9360*
9361** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if
9362** string X matches the [GLOB] pattern P.
9363** ^The definition of [GLOB] pattern matching used in
9364** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
9365** SQL dialect understood by SQLite. ^The [sqlite3_strglob(P,X)] function
9366** is case sensitive.
9367**
9368** Note that this routine returns zero on a match and non-zero if the strings
9369** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9370**
9371** See also: [sqlite3_strlike()].
9372*/
9373SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
9374
9375/*
9376** CAPI3REF: String LIKE Matching
9377*
9378** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if
9379** string X matches the [LIKE] pattern P with escape character E.
9380** ^The definition of [LIKE] pattern matching used in
9381** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E"
9382** operator in the SQL dialect understood by SQLite. ^For "X LIKE P" without
9383** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0.
9384** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case
9385** insensitive - equivalent upper and lower case ASCII characters match
9386** one another.
9387**
9388** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though
9389** only ASCII characters are case folded.
9390**
9391** Note that this routine returns zero on a match and non-zero if the strings
9392** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9393**
9394** See also: [sqlite3_strglob()].
9395*/
9396SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
9397
9398/*
9399** CAPI3REF: Error Logging Interface
9400**
9401** ^The [sqlite3_log()] interface writes a message into the [error log]
9402** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
9403** ^If logging is enabled, the zFormat string and subsequent arguments are
9404** used with [sqlite3_snprintf()] to generate the final output string.
9405**
9406** The sqlite3_log() interface is intended for use by extensions such as
9407** virtual tables, collating functions, and SQL functions. While there is
9408** nothing to prevent an application from calling sqlite3_log(), doing so
9409** is considered bad form.
9410**
9411** The zFormat string must not be NULL.
9412**
9413** To avoid deadlocks and other threading problems, the sqlite3_log() routine
9414** will not use dynamically allocated memory. The log message is stored in
9415** a fixed-length buffer on the stack. If the log message is longer than
9416** a few hundred characters, it will be truncated to the length of the
9417** buffer.
9418*/
9419SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
9420
9421/*
9422** CAPI3REF: Write-Ahead Log Commit Hook
9423** METHOD: sqlite3
9424**
9425** ^The [sqlite3_wal_hook()] function is used to register a callback that
9426** is invoked each time data is committed to a database in wal mode.
9427**
9428** ^(The callback is invoked by SQLite after the commit has taken place and
9429** the associated write-lock on the database released)^, so the implementation
9430** may read, write or [checkpoint] the database as required.
9431**
9432** ^The first parameter passed to the callback function when it is invoked
9433** is a copy of the third parameter passed to sqlite3_wal_hook() when
9434** registering the callback. ^The second is a copy of the database handle.
9435** ^The third parameter is the name of the database that was written to -
9436** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9437** is the number of pages currently in the write-ahead log file,
9438** including those that were just committed.
9439**
9440** The callback function should normally return [SQLITE_OK]. ^If an error
9441** code is returned, that error will propagate back up through the
9442** SQLite code base to cause the statement that provoked the callback
9443** to report an error, though the commit will have still occurred. If the
9444** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
9445** that does not correspond to any valid SQLite error code, the results
9446** are undefined.
9447**
9448** A single database handle may have at most a single write-ahead log callback
9449** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
9450** previously registered write-ahead log callback. ^Note that the
9451** [sqlite3_wal_autocheckpoint()] interface and the
9452** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
9453** overwrite any prior [sqlite3_wal_hook()] settings.
9454*/
9455SQLITE_API void *sqlite3_wal_hook(
9456 sqlite3*,
9457 int(*)(void *,sqlite3*,const char*,int),
9458 void*
9459);
9460
9461/*
9462** CAPI3REF: Configure an auto-checkpoint
9463** METHOD: sqlite3
9464**
9465** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
9466** [sqlite3_wal_hook()] that causes any database on [database connection] D
9467** to automatically [checkpoint]
9468** after committing a transaction if there are N or
9469** more frames in the [write-ahead log] file. ^Passing zero or
9470** a negative value as the nFrame parameter disables automatic
9471** checkpoints entirely.
9472**
9473** ^The callback registered by this function replaces any existing callback
9474** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
9475** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
9476** configured by this function.
9477**
9478** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
9479** from SQL.
9480**
9481** ^Checkpoints initiated by this mechanism are
9482** [sqlite3_wal_checkpoint_v2|PASSIVE].
9483**
9484** ^Every new [database connection] defaults to having the auto-checkpoint
9485** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
9486** pages. The use of this interface
9487** is only necessary if the default setting is found to be suboptimal
9488** for a particular application.
9489*/
9490SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
9491
9492/*
9493** CAPI3REF: Checkpoint a database
9494** METHOD: sqlite3
9495**
9496** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
9497** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
9498**
9499** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the
9500** [write-ahead log] for database X on [database connection] D to be
9501** transferred into the database file and for the write-ahead log to
9502** be reset. See the [checkpointing] documentation for addition
9503** information.
9504**
9505** This interface used to be the only way to cause a checkpoint to
9506** occur. But then the newer and more powerful [sqlite3_wal_checkpoint_v2()]
9507** interface was added. This interface is retained for backwards
9508** compatibility and as a convenience for applications that need to manually
9509** start a callback but which do not need the full power (and corresponding
9510** complication) of [sqlite3_wal_checkpoint_v2()].
9511*/
9512SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
9513
9514/*
9515** CAPI3REF: Checkpoint a database
9516** METHOD: sqlite3
9517**
9518** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
9519** operation on database X of [database connection] D in mode M. Status
9520** information is written back into integers pointed to by L and C.)^
9521** ^(The M parameter must be a valid [checkpoint mode]:)^
9522**
9523** <dl>
9524** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
9525** ^Checkpoint as many frames as possible without waiting for any database
9526** readers or writers to finish, then sync the database file if all frames
9527** in the log were checkpointed. ^The [busy-handler callback]
9528** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode.
9529** ^On the other hand, passive mode might leave the checkpoint unfinished
9530** if there are concurrent readers or writers.
9531**
9532** <dt>SQLITE_CHECKPOINT_FULL<dd>
9533** ^This mode blocks (it invokes the
9534** [sqlite3_busy_handler|busy-handler callback]) until there is no
9535** database writer and all readers are reading from the most recent database
9536** snapshot. ^It then checkpoints all frames in the log file and syncs the
9537** database file. ^This mode blocks new database writers while it is pending,
9538** but new database readers are allowed to continue unimpeded.
9539**
9540** <dt>SQLITE_CHECKPOINT_RESTART<dd>
9541** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition
9542** that after checkpointing the log file it blocks (calls the
9543** [busy-handler callback])
9544** until all readers are reading from the database file only. ^This ensures
9545** that the next writer will restart the log file from the beginning.
9546** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new
9547** database writer attempts while it is pending, but does not impede readers.
9548**
9549** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
9550** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
9551** addition that it also truncates the log file to zero bytes just prior
9552** to a successful return.
9553** </dl>
9554**
9555** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
9556** the log file or to -1 if the checkpoint could not run because
9557** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
9558** NULL,then *pnCkpt is set to the total number of checkpointed frames in the
9559** log file (including any that were already checkpointed before the function
9560** was called) or to -1 if the checkpoint could not run due to an error or
9561** because the database is not in WAL mode. ^Note that upon successful
9562** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been
9563** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero.
9564**
9565** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If
9566** any other process is running a checkpoint operation at the same time, the
9567** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a
9568** busy-handler configured, it will not be invoked in this case.
9569**
9570** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the
9571** exclusive "writer" lock on the database file. ^If the writer lock cannot be
9572** obtained immediately, and a busy-handler is configured, it is invoked and
9573** the writer lock retried until either the busy-handler returns 0 or the lock
9574** is successfully obtained. ^The busy-handler is also invoked while waiting for
9575** database readers as described above. ^If the busy-handler returns 0 before
9576** the writer lock is obtained or while waiting for database readers, the
9577** checkpoint operation proceeds from that point in the same way as
9578** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
9579** without blocking any further. ^SQLITE_BUSY is returned in this case.
9580**
9581** ^If parameter zDb is NULL or points to a zero length string, then the
9582** specified operation is attempted on all WAL databases [attached] to
9583** [database connection] db. In this case the
9584** values written to output parameters *pnLog and *pnCkpt are undefined. ^If
9585** an SQLITE_BUSY error is encountered when processing one or more of the
9586** attached WAL databases, the operation is still attempted on any remaining
9587** attached databases and SQLITE_BUSY is returned at the end. ^If any other
9588** error occurs while processing an attached database, processing is abandoned
9589** and the error code is returned to the caller immediately. ^If no error
9590** (SQLITE_BUSY or otherwise) is encountered while processing the attached
9591** databases, SQLITE_OK is returned.
9592**
9593** ^If database zDb is the name of an attached database that is not in WAL
9594** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9595** zDb is not NULL (or a zero length string) and is not the name of any
9596** attached database, SQLITE_ERROR is returned to the caller.
9597**
9598** ^Unless it returns SQLITE_MISUSE,
9599** the sqlite3_wal_checkpoint_v2() interface
9600** sets the error information that is queried by
9601** [sqlite3_errcode()] and [sqlite3_errmsg()].
9602**
9603** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
9604** from SQL.
9605*/
9606SQLITE_API int sqlite3_wal_checkpoint_v2(
9607 sqlite3 *db, /* Database handle */
9608 const char *zDb, /* Name of attached database (or NULL) */
9609 int eMode, /* SQLITE_CHECKPOINT_* value */
9610 int *pnLog, /* OUT: Size of WAL log in frames */
9611 int *pnCkpt /* OUT: Total number of frames checkpointed */
9612);
9613
9614/*
9615** CAPI3REF: Checkpoint Mode Values
9616** KEYWORDS: {checkpoint mode}
9617**
9618** These constants define all valid values for the "checkpoint mode" passed
9619** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
9620** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
9621** meaning of each of these checkpoint modes.
9622*/
9623#define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */
9624#define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */
9625#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */
9626#define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */
9627
9628/*
9629** CAPI3REF: Virtual Table Interface Configuration
9630**
9631** This function may be called by either the [xConnect] or [xCreate] method
9632** of a [virtual table] implementation to configure
9633** various facets of the virtual table interface.
9634**
9635** If this interface is invoked outside the context of an xConnect or
9636** xCreate virtual table method then the behavior is undefined.
9637**
9638** At present, there is only one option that may be configured using
9639** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].) Further options
9640** may be added in the future.
9641*/
9642SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
9643
9644/*
9645** CAPI3REF: Virtual Table Configuration Options
9646**
9647** These macros define the various options to the
9648** [sqlite3_vtab_config()] interface that [virtual table] implementations
9649** can use to customize and optimize their behavior.
9650**
9651** <dl>
9652** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
9653** <dd>Calls of the form
9654** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
9655** where X is an integer. If X is zero, then the [virtual table] whose
9656** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
9657** support constraints. In this configuration (which is the default) if
9658** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
9659** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
9660** specified as part of the users SQL statement, regardless of the actual
9661** ON CONFLICT mode specified.
9662**
9663** If X is non-zero, then the virtual table implementation guarantees
9664** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
9665** any modifications to internal or persistent data structures have been made.
9666** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
9667** is able to roll back a statement or database transaction, and abandon
9668** or continue processing the current SQL statement as appropriate.
9669** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
9670** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
9671** had been ABORT.
9672**
9673** Virtual table implementations that are required to handle OR REPLACE
9674** must do so within the [xUpdate] method. If a call to the
9675** [sqlite3_vtab_on_conflict()] function indicates that the current ON
9676** CONFLICT policy is REPLACE, the virtual table implementation should
9677** silently replace the appropriate rows within the xUpdate callback and
9678** return SQLITE_OK. Or, if this is not possible, it may return
9679** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
9680** constraint handling.
9681** </dl>
9682*/
9683#define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
9684
9685/*
9686** CAPI3REF: Determine The Virtual Table Conflict Policy
9687**
9688** This function may only be called from within a call to the [xUpdate] method
9689** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
9690** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
9691** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
9692** of the SQL statement that triggered the call to the [xUpdate] method of the
9693** [virtual table].
9694*/
9695SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
9696
9697/*
9698** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
9699**
9700** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
9701** method of a [virtual table], then it returns true if and only if the
9702** column is being fetched as part of an UPDATE operation during which the
9703** column value will not change. Applications might use this to substitute
9704** a return value that is less expensive to compute and that the corresponding
9705** [xUpdate] method understands as a "no-change" value.
9706**
9707** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
9708** the column is not changed by the UPDATE statement, then the xColumn
9709** method can optionally return without setting a result, without calling
9710** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
9711** In that case, [sqlite3_value_nochange(X)] will return true for the
9712** same column in the [xUpdate] method.
9713*/
9714SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
9715
9716/*
9717** CAPI3REF: Determine The Collation For a Virtual Table Constraint
9718**
9719** This function may only be called from within a call to the [xBestIndex]
9720** method of a [virtual table].
9721**
9722** The first argument must be the sqlite3_index_info object that is the
9723** first parameter to the xBestIndex() method. The second argument must be
9724** an index into the aConstraint[] array belonging to the sqlite3_index_info
9725** structure passed to xBestIndex. This function returns a pointer to a buffer
9726** containing the name of the collation sequence for the corresponding
9727** constraint.
9728*/
9729SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
9730
9731/*
9732** CAPI3REF: Conflict resolution modes
9733** KEYWORDS: {conflict resolution mode}
9734**
9735** These constants are returned by [sqlite3_vtab_on_conflict()] to
9736** inform a [virtual table] implementation what the [ON CONFLICT] mode
9737** is for the SQL statement being evaluated.
9738**
9739** Note that the [SQLITE_IGNORE] constant is also used as a potential
9740** return value from the [sqlite3_set_authorizer()] callback and that
9741** [SQLITE_ABORT] is also a [result code].
9742*/
9743#define SQLITE_ROLLBACK 1
9744/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
9745#define SQLITE_FAIL 3
9746/* #define SQLITE_ABORT 4 // Also an error code */
9747#define SQLITE_REPLACE 5
9748
9749/*
9750** CAPI3REF: Prepared Statement Scan Status Opcodes
9751** KEYWORDS: {scanstatus options}
9752**
9753** The following constants can be used for the T parameter to the
9754** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
9755** different metric for sqlite3_stmt_scanstatus() to return.
9756**
9757** When the value returned to V is a string, space to hold that string is
9758** managed by the prepared statement S and will be automatically freed when
9759** S is finalized.
9760**
9761** <dl>
9762** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
9763** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be
9764** set to the total number of times that the X-th loop has run.</dd>
9765**
9766** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
9767** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set
9768** to the total number of rows examined by all iterations of the X-th loop.</dd>
9769**
9770** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
9771** <dd>^The "double" variable pointed to by the T parameter will be set to the
9772** query planner's estimate for the average number of rows output from each
9773** iteration of the X-th loop. If the query planner's estimates was accurate,
9774** then this value will approximate the quotient NVISIT/NLOOP and the
9775** product of this value for all prior loops with the same SELECTID will
9776** be the NLOOP value for the current loop.
9777**
9778** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
9779** <dd>^The "const char *" variable pointed to by the T parameter will be set
9780** to a zero-terminated UTF-8 string containing the name of the index or table
9781** used for the X-th loop.
9782**
9783** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
9784** <dd>^The "const char *" variable pointed to by the T parameter will be set
9785** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
9786** description for the X-th loop.
9787**
9788** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
9789** <dd>^The "int" variable pointed to by the T parameter will be set to the
9790** "select-id" for the X-th loop. The select-id identifies which query or
9791** subquery the loop is part of. The main query has a select-id of zero.
9792** The select-id is the same value as is output in the first column
9793** of an [EXPLAIN QUERY PLAN] query.
9794** </dl>
9795*/
9796#define SQLITE_SCANSTAT_NLOOP 0
9797#define SQLITE_SCANSTAT_NVISIT 1
9798#define SQLITE_SCANSTAT_EST 2
9799#define SQLITE_SCANSTAT_NAME 3
9800#define SQLITE_SCANSTAT_EXPLAIN 4
9801#define SQLITE_SCANSTAT_SELECTID 5
9802
9803/*
9804** CAPI3REF: Prepared Statement Scan Status
9805** METHOD: sqlite3_stmt
9806**
9807** This interface returns information about the predicted and measured
9808** performance for pStmt. Advanced applications can use this
9809** interface to compare the predicted and the measured performance and
9810** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
9811**
9812** Since this interface is expected to be rarely used, it is only
9813** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS]
9814** compile-time option.
9815**
9816** The "iScanStatusOp" parameter determines which status information to return.
9817** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior
9818** of this interface is undefined.
9819** ^The requested measurement is written into a variable pointed to by
9820** the "pOut" parameter.
9821** Parameter "idx" identifies the specific loop to retrieve statistics for.
9822** Loops are numbered starting from zero. ^If idx is out of range - less than
9823** zero or greater than or equal to the total number of loops used to implement
9824** the statement - a non-zero value is returned and the variable that pOut
9825** points to is unchanged.
9826**
9827** ^Statistics might not be available for all loops in all statements. ^In cases
9828** where there exist loops with no available statistics, this function behaves
9829** as if the loop did not exist - it returns non-zero and leave the variable
9830** that pOut points to unchanged.
9831**
9832** See also: [sqlite3_stmt_scanstatus_reset()]
9833*/
9834SQLITE_API int sqlite3_stmt_scanstatus(
9835 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
9836 int idx, /* Index of loop to report on */
9837 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
9838 void *pOut /* Result written here */
9839);
9840
9841/*
9842** CAPI3REF: Zero Scan-Status Counters
9843** METHOD: sqlite3_stmt
9844**
9845** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
9846**
9847** This API is only available if the library is built with pre-processor
9848** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
9849*/
9850SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
9851
9852/*
9853** CAPI3REF: Flush caches to disk mid-transaction
9854**
9855** ^If a write-transaction is open on [database connection] D when the
9856** [sqlite3_db_cacheflush(D)] interface invoked, any dirty
9857** pages in the pager-cache that are not currently in use are written out
9858** to disk. A dirty page may be in use if a database cursor created by an
9859** active SQL statement is reading from it, or if it is page 1 of a database
9860** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)]
9861** interface flushes caches for all schemas - "main", "temp", and
9862** any [attached] databases.
9863**
9864** ^If this function needs to obtain extra database locks before dirty pages
9865** can be flushed to disk, it does so. ^If those locks cannot be obtained
9866** immediately and there is a busy-handler callback configured, it is invoked
9867** in the usual manner. ^If the required lock still cannot be obtained, then
9868** the database is skipped and an attempt made to flush any dirty pages
9869** belonging to the next (if any) database. ^If any databases are skipped
9870** because locks cannot be obtained, but no other error occurs, this
9871** function returns SQLITE_BUSY.
9872**
9873** ^If any other error occurs while flushing dirty pages to disk (for
9874** example an IO error or out-of-memory condition), then processing is
9875** abandoned and an SQLite [error code] is returned to the caller immediately.
9876**
9877** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
9878**
9879** ^This function does not set the database handle error code or message
9880** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
9881*/
9882SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
9883
9884/*
9885** CAPI3REF: The pre-update hook.
9886**
9887** ^These interfaces are only available if SQLite is compiled using the
9888** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
9889**
9890** ^The [sqlite3_preupdate_hook()] interface registers a callback function
9891** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
9892** on a database table.
9893** ^At most one preupdate hook may be registered at a time on a single
9894** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
9895** the previous setting.
9896** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
9897** with a NULL pointer as the second parameter.
9898** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
9899** the first parameter to callbacks.
9900**
9901** ^The preupdate hook only fires for changes to real database tables; the
9902** preupdate hook is not invoked for changes to [virtual tables] or to
9903** system tables like sqlite_master or sqlite_stat1.
9904**
9905** ^The second parameter to the preupdate callback is a pointer to
9906** the [database connection] that registered the preupdate hook.
9907** ^The third parameter to the preupdate callback is one of the constants
9908** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
9909** kind of update operation that is about to occur.
9910** ^(The fourth parameter to the preupdate callback is the name of the
9911** database within the database connection that is being modified. This
9912** will be "main" for the main database or "temp" for TEMP tables or
9913** the name given after the AS keyword in the [ATTACH] statement for attached
9914** databases.)^
9915** ^The fifth parameter to the preupdate callback is the name of the
9916** table that is being modified.
9917**
9918** For an UPDATE or DELETE operation on a [rowid table], the sixth
9919** parameter passed to the preupdate callback is the initial [rowid] of the
9920** row being modified or deleted. For an INSERT operation on a rowid table,
9921** or any operation on a WITHOUT ROWID table, the value of the sixth
9922** parameter is undefined. For an INSERT or UPDATE on a rowid table the
9923** seventh parameter is the final rowid value of the row being inserted
9924** or updated. The value of the seventh parameter passed to the callback
9925** function is not defined for operations on WITHOUT ROWID tables, or for
9926** INSERT operations on rowid tables.
9927**
9928** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
9929** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
9930** provide additional information about a preupdate event. These routines
9931** may only be called from within a preupdate callback. Invoking any of
9932** these routines from outside of a preupdate callback or with a
9933** [database connection] pointer that is different from the one supplied
9934** to the preupdate callback results in undefined and probably undesirable
9935** behavior.
9936**
9937** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
9938** in the row that is being inserted, updated, or deleted.
9939**
9940** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
9941** a [protected sqlite3_value] that contains the value of the Nth column of
9942** the table row before it is updated. The N parameter must be between 0
9943** and one less than the number of columns or the behavior will be
9944** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
9945** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
9946** behavior is undefined. The [sqlite3_value] that P points to
9947** will be destroyed when the preupdate callback returns.
9948**
9949** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
9950** a [protected sqlite3_value] that contains the value of the Nth column of
9951** the table row after it is updated. The N parameter must be between 0
9952** and one less than the number of columns or the behavior will be
9953** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
9954** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
9955** behavior is undefined. The [sqlite3_value] that P points to
9956** will be destroyed when the preupdate callback returns.
9957**
9958** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
9959** callback was invoked as a result of a direct insert, update, or delete
9960** operation; or 1 for inserts, updates, or deletes invoked by top-level
9961** triggers; or 2 for changes resulting from triggers called by top-level
9962** triggers; and so forth.
9963**
9964** See also: [sqlite3_update_hook()]
9965*/
9966#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
9967SQLITE_API void *sqlite3_preupdate_hook(
9968 sqlite3 *db,
9969 void(*xPreUpdate)(
9970 void *pCtx, /* Copy of third arg to preupdate_hook() */
9971 sqlite3 *db, /* Database handle */
9972 int op, /* SQLITE_UPDATE, DELETE or INSERT */
9973 char const *zDb, /* Database name */
9974 char const *zName, /* Table name */
9975 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
9976 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
9977 ),
9978 void*
9979);
9980SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
9981SQLITE_API int sqlite3_preupdate_count(sqlite3 *);
9982SQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
9983SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
9984#endif
9985
9986/*
9987** CAPI3REF: Low-level system error code
9988**
9989** ^Attempt to return the underlying operating system error code or error
9990** number that caused the most recent I/O error or failure to open a file.
9991** The return value is OS-dependent. For example, on unix systems, after
9992** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
9993** called to get back the underlying "errno" that caused the problem, such
9994** as ENOSPC, EAUTH, EISDIR, and so forth.
9995*/
9996SQLITE_API int sqlite3_system_errno(sqlite3*);
9997
9998/*
9999** CAPI3REF: Database Snapshot
10000** KEYWORDS: {snapshot} {sqlite3_snapshot}
10001**
10002** An instance of the snapshot object records the state of a [WAL mode]
10003** database for some specific point in history.
10004**
10005** In [WAL mode], multiple [database connections] that are open on the
10006** same database file can each be reading a different historical version
10007** of the database file. When a [database connection] begins a read
10008** transaction, that connection sees an unchanging copy of the database
10009** as it existed for the point in time when the transaction first started.
10010** Subsequent changes to the database from other connections are not seen
10011** by the reader until a new read transaction is started.
10012**
10013** The sqlite3_snapshot object records state information about an historical
10014** version of the database file so that it is possible to later open a new read
10015** transaction that sees that historical version of the database rather than
10016** the most recent version.
10017*/
10018typedef struct sqlite3_snapshot {
10019 unsigned char hidden[48];
10020} sqlite3_snapshot;
10021
10022/*
10023** CAPI3REF: Record A Database Snapshot
10024** CONSTRUCTOR: sqlite3_snapshot
10025**
10026** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
10027** new [sqlite3_snapshot] object that records the current state of
10028** schema S in database connection D. ^On success, the
10029** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
10030** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
10031** If there is not already a read-transaction open on schema S when
10032** this function is called, one is opened automatically.
10033**
10034** The following must be true for this function to succeed. If any of
10035** the following statements are false when sqlite3_snapshot_get() is
10036** called, SQLITE_ERROR is returned. The final value of *P is undefined
10037** in this case.
10038**
10039** <ul>
10040** <li> The database handle must not be in [autocommit mode].
10041**
10042** <li> Schema S of [database connection] D must be a [WAL mode] database.
10043**
10044** <li> There must not be a write transaction open on schema S of database
10045** connection D.
10046**
10047** <li> One or more transactions must have been written to the current wal
10048** file since it was created on disk (by any connection). This means
10049** that a snapshot cannot be taken on a wal mode database with no wal
10050** file immediately after it is first opened. At least one transaction
10051** must be written to it first.
10052** </ul>
10053**
10054** This function may also return SQLITE_NOMEM. If it is called with the
10055** database handle in autocommit mode but fails for some other reason,
10056** whether or not a read transaction is opened on schema S is undefined.
10057**
10058** The [sqlite3_snapshot] object returned from a successful call to
10059** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
10060** to avoid a memory leak.
10061**
10062** The [sqlite3_snapshot_get()] interface is only available when the
10063** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10064*/
10065SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
10066 sqlite3 *db,
10067 const char *zSchema,
10068 sqlite3_snapshot **ppSnapshot
10069);
10070
10071/*
10072** CAPI3REF: Start a read transaction on an historical snapshot
10073** METHOD: sqlite3_snapshot
10074**
10075** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read
10076** transaction or upgrades an existing one for schema S of
10077** [database connection] D such that the read transaction refers to
10078** historical [snapshot] P, rather than the most recent change to the
10079** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK
10080** on success or an appropriate [error code] if it fails.
10081**
10082** ^In order to succeed, the database connection must not be in
10083** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
10084** is already a read transaction open on schema S, then the database handle
10085** must have no active statements (SELECT statements that have been passed
10086** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()).
10087** SQLITE_ERROR is returned if either of these conditions is violated, or
10088** if schema S does not exist, or if the snapshot object is invalid.
10089**
10090** ^A call to sqlite3_snapshot_open() will fail to open if the specified
10091** snapshot has been overwritten by a [checkpoint]. In this case
10092** SQLITE_ERROR_SNAPSHOT is returned.
10093**
10094** If there is already a read transaction open when this function is
10095** invoked, then the same read transaction remains open (on the same
10096** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
10097** is returned. If another error code - for example SQLITE_PROTOCOL or an
10098** SQLITE_IOERR error code - is returned, then the final state of the
10099** read transaction is undefined. If SQLITE_OK is returned, then the
10100** read transaction is now open on database snapshot P.
10101**
10102** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
10103** database connection D does not know that the database file for
10104** schema S is in [WAL mode]. A database connection might not know
10105** that the database file is in [WAL mode] if there has been no prior
10106** I/O on that database connection, or if the database entered [WAL mode]
10107** after the most recent I/O on the database connection.)^
10108** (Hint: Run "[PRAGMA application_id]" against a newly opened
10109** database connection in order to make it ready to use snapshots.)
10110**
10111** The [sqlite3_snapshot_open()] interface is only available when the
10112** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10113*/
10114SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
10115 sqlite3 *db,
10116 const char *zSchema,
10117 sqlite3_snapshot *pSnapshot
10118);
10119
10120/*
10121** CAPI3REF: Destroy a snapshot
10122** DESTRUCTOR: sqlite3_snapshot
10123**
10124** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
10125** The application must eventually free every [sqlite3_snapshot] object
10126** using this routine to avoid a memory leak.
10127**
10128** The [sqlite3_snapshot_free()] interface is only available when the
10129** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10130*/
10131SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
10132
10133/*
10134** CAPI3REF: Compare the ages of two snapshot handles.
10135** METHOD: sqlite3_snapshot
10136**
10137** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
10138** of two valid snapshot handles.
10139**
10140** If the two snapshot handles are not associated with the same database
10141** file, the result of the comparison is undefined.
10142**
10143** Additionally, the result of the comparison is only valid if both of the
10144** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
10145** last time the wal file was deleted. The wal file is deleted when the
10146** database is changed back to rollback mode or when the number of database
10147** clients drops to zero. If either snapshot handle was obtained before the
10148** wal file was last deleted, the value returned by this function
10149** is undefined.
10150**
10151** Otherwise, this API returns a negative value if P1 refers to an older
10152** snapshot than P2, zero if the two handles refer to the same database
10153** snapshot, and a positive value if P1 is a newer snapshot than P2.
10154**
10155** This interface is only available if SQLite is compiled with the
10156** [SQLITE_ENABLE_SNAPSHOT] option.
10157*/
10158SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
10159 sqlite3_snapshot *p1,
10160 sqlite3_snapshot *p2
10161);
10162
10163/*
10164** CAPI3REF: Recover snapshots from a wal file
10165** METHOD: sqlite3_snapshot
10166**
10167** If a [WAL file] remains on disk after all database connections close
10168** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
10169** or because the last process to have the database opened exited without
10170** calling [sqlite3_close()]) and a new connection is subsequently opened
10171** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
10172** will only be able to open the last transaction added to the WAL file
10173** even though the WAL file contains other valid transactions.
10174**
10175** This function attempts to scan the WAL file associated with database zDb
10176** of database handle db and make all valid snapshots available to
10177** sqlite3_snapshot_open(). It is an error if there is already a read
10178** transaction open on the database, or if the database is not a WAL mode
10179** database.
10180**
10181** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
10182**
10183** This interface is only available if SQLite is compiled with the
10184** [SQLITE_ENABLE_SNAPSHOT] option.
10185*/
10186SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
10187
10188/*
10189** CAPI3REF: Serialize a database
10190**
10191** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
10192** that is a serialization of the S database on [database connection] D.
10193** If P is not a NULL pointer, then the size of the database in bytes
10194** is written into *P.
10195**
10196** For an ordinary on-disk database file, the serialization is just a
10197** copy of the disk file. For an in-memory database or a "TEMP" database,
10198** the serialization is the same sequence of bytes which would be written
10199** to disk if that database where backed up to disk.
10200**
10201** The usual case is that sqlite3_serialize() copies the serialization of
10202** the database into memory obtained from [sqlite3_malloc64()] and returns
10203** a pointer to that memory. The caller is responsible for freeing the
10204** returned value to avoid a memory leak. However, if the F argument
10205** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
10206** are made, and the sqlite3_serialize() function will return a pointer
10207** to the contiguous memory representation of the database that SQLite
10208** is currently using for that database, or NULL if the no such contiguous
10209** memory representation of the database exists. A contiguous memory
10210** representation of the database will usually only exist if there has
10211** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
10212** values of D and S.
10213** The size of the database is written into *P even if the
10214** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
10215** of the database exists.
10216**
10217** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
10218** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
10219** allocation error occurs.
10220**
10221** This interface is only available if SQLite is compiled with the
10222** [SQLITE_ENABLE_DESERIALIZE] option.
10223*/
10224SQLITE_API unsigned char *sqlite3_serialize(
10225 sqlite3 *db, /* The database connection */
10226 const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */
10227 sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
10228 unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
10229);
10230
10231/*
10232** CAPI3REF: Flags for sqlite3_serialize
10233**
10234** Zero or more of the following constants can be OR-ed together for
10235** the F argument to [sqlite3_serialize(D,S,P,F)].
10236**
10237** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
10238** a pointer to contiguous in-memory database that it is currently using,
10239** without making a copy of the database. If SQLite is not currently using
10240** a contiguous in-memory database, then this option causes
10241** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be
10242** using a contiguous in-memory database if it has been initialized by a
10243** prior call to [sqlite3_deserialize()].
10244*/
10245#define SQLITE_SERIALIZE_NOCOPY 0x001 /* Do no memory allocations */
10246
10247/*
10248** CAPI3REF: Deserialize a database
10249**
10250** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
10251** [database connection] D to disconnect from database S and then
10252** reopen S as an in-memory database based on the serialization contained
10253** in P. The serialized database P is N bytes in size. M is the size of
10254** the buffer P, which might be larger than N. If M is larger than N, and
10255** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
10256** permitted to add content to the in-memory database as long as the total
10257** size does not exceed M bytes.
10258**
10259** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
10260** invoke sqlite3_free() on the serialization buffer when the database
10261** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
10262** SQLite will try to increase the buffer size using sqlite3_realloc64()
10263** if writes on the database cause it to grow larger than M bytes.
10264**
10265** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
10266** database is currently in a read transaction or is involved in a backup
10267** operation.
10268**
10269** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
10270** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
10271** [sqlite3_free()] is invoked on argument P prior to returning.
10272**
10273** This interface is only available if SQLite is compiled with the
10274** [SQLITE_ENABLE_DESERIALIZE] option.
10275*/
10276SQLITE_API int sqlite3_deserialize(
10277 sqlite3 *db, /* The database connection */
10278 const char *zSchema, /* Which DB to reopen with the deserialization */
10279 unsigned char *pData, /* The serialized database content */
10280 sqlite3_int64 szDb, /* Number bytes in the deserialization */
10281 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
10282 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
10283);
10284
10285/*
10286** CAPI3REF: Flags for sqlite3_deserialize()
10287**
10288** The following are allowed values for 6th argument (the F argument) to
10289** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
10290**
10291** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
10292** in the P argument is held in memory obtained from [sqlite3_malloc64()]
10293** and that SQLite should take ownership of this memory and automatically
10294** free it when it has finished using it. Without this flag, the caller
10295** is responsible for freeing any dynamically allocated memory.
10296**
10297** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
10298** grow the size of the database using calls to [sqlite3_realloc64()]. This
10299** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
10300** Without this flag, the deserialized database cannot increase in size beyond
10301** the number of bytes specified by the M parameter.
10302**
10303** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
10304** should be treated as read-only.
10305*/
10306#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
10307#define SQLITE_DESERIALIZE_RESIZEABLE 2 /* Resize using sqlite3_realloc64() */
10308#define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
10309
10310/*
10311** Undo the hack that converts floating point types to integer for
10312** builds on processors without floating point support.
10313*/
10314#ifdef SQLITE_OMIT_FLOATING_POINT
10315# undef double
10316#endif
10317
10318#if 0
10319} /* End of the 'extern "C"' block */
10320#endif
10321#endif /* SQLITE3_H */
10322
10323/******** Begin file sqlite3rtree.h *********/
10324/*
10325** 2010 August 30
10326**
10327** The author disclaims copyright to this source code. In place of
10328** a legal notice, here is a blessing:
10329**
10330** May you do good and not evil.
10331** May you find forgiveness for yourself and forgive others.
10332** May you share freely, never taking more than you give.
10333**
10334*************************************************************************
10335*/
10336
10337#ifndef _SQLITE3RTREE_H_
10338#define _SQLITE3RTREE_H_
10339
10340
10341#if 0
10342extern "C" {
10343#endif
10344
10345typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
10346typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
10347
10348/* The double-precision datatype used by RTree depends on the
10349** SQLITE_RTREE_INT_ONLY compile-time option.
10350*/
10351#ifdef SQLITE_RTREE_INT_ONLY
10352 typedef sqlite3_int64 sqlite3_rtree_dbl;
10353#else
10354 typedef double sqlite3_rtree_dbl;
10355#endif
10356
10357/*
10358** Register a geometry callback named zGeom that can be used as part of an
10359** R-Tree geometry query as follows:
10360**
10361** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
10362*/
10363SQLITE_API int sqlite3_rtree_geometry_callback(
10364 sqlite3 *db,
10365 const char *zGeom,
10366 int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
10367 void *pContext
10368);
10369
10370
10371/*
10372** A pointer to a structure of the following type is passed as the first
10373** argument to callbacks registered using rtree_geometry_callback().
10374*/
10375struct sqlite3_rtree_geometry {
10376 void *pContext; /* Copy of pContext passed to s_r_g_c() */
10377 int nParam; /* Size of array aParam[] */
10378 sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */
10379 void *pUser; /* Callback implementation user data */
10380 void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
10381};
10382
10383/*
10384** Register a 2nd-generation geometry callback named zScore that can be
10385** used as part of an R-Tree geometry query as follows:
10386**
10387** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
10388*/
10389SQLITE_API int sqlite3_rtree_query_callback(
10390 sqlite3 *db,
10391 const char *zQueryFunc,
10392 int (*xQueryFunc)(sqlite3_rtree_query_info*),
10393 void *pContext,
10394 void (*xDestructor)(void*)
10395);
10396
10397
10398/*
10399** A pointer to a structure of the following type is passed as the
10400** argument to scored geometry callback registered using
10401** sqlite3_rtree_query_callback().
10402**
10403** Note that the first 5 fields of this structure are identical to
10404** sqlite3_rtree_geometry. This structure is a subclass of
10405** sqlite3_rtree_geometry.
10406*/
10407struct sqlite3_rtree_query_info {
10408 void *pContext; /* pContext from when function registered */
10409 int nParam; /* Number of function parameters */
10410 sqlite3_rtree_dbl *aParam; /* value of function parameters */
10411 void *pUser; /* callback can use this, if desired */
10412 void (*xDelUser)(void*); /* function to free pUser */
10413 sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */
10414 unsigned int *anQueue; /* Number of pending entries in the queue */
10415 int nCoord; /* Number of coordinates */
10416 int iLevel; /* Level of current node or entry */
10417 int mxLevel; /* The largest iLevel value in the tree */
10418 sqlite3_int64 iRowid; /* Rowid for current entry */
10419 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
10420 int eParentWithin; /* Visibility of parent node */
10421 int eWithin; /* OUT: Visiblity */
10422 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
10423 /* The following fields are only available in 3.8.11 and later */
10424 sqlite3_value **apSqlParam; /* Original SQL values of parameters */
10425};
10426
10427/*
10428** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
10429*/
10430#define NOT_WITHIN 0 /* Object completely outside of query region */
10431#define PARTLY_WITHIN 1 /* Object partially overlaps query region */
10432#define FULLY_WITHIN 2 /* Object fully contained within query region */
10433
10434
10435#if 0
10436} /* end of the 'extern "C"' block */
10437#endif
10438
10439#endif /* ifndef _SQLITE3RTREE_H_ */
10440
10441/******** End of sqlite3rtree.h *********/
10442/******** Begin file sqlite3session.h *********/
10443
10444#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
10445#define __SQLITESESSION_H_ 1
10446
10447/*
10448** Make sure we can call this stuff from C++.
10449*/
10450#if 0
10451extern "C" {
10452#endif
10453
10454
10455/*
10456** CAPI3REF: Session Object Handle
10457**
10458** An instance of this object is a [session] that can be used to
10459** record changes to a database.
10460*/
10461typedef struct sqlite3_session sqlite3_session;
10462
10463/*
10464** CAPI3REF: Changeset Iterator Handle
10465**
10466** An instance of this object acts as a cursor for iterating
10467** over the elements of a [changeset] or [patchset].
10468*/
10469typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
10470
10471/*
10472** CAPI3REF: Create A New Session Object
10473** CONSTRUCTOR: sqlite3_session
10474**
10475** Create a new session object attached to database handle db. If successful,
10476** a pointer to the new object is written to *ppSession and SQLITE_OK is
10477** returned. If an error occurs, *ppSession is set to NULL and an SQLite
10478** error code (e.g. SQLITE_NOMEM) is returned.
10479**
10480** It is possible to create multiple session objects attached to a single
10481** database handle.
10482**
10483** Session objects created using this function should be deleted using the
10484** [sqlite3session_delete()] function before the database handle that they
10485** are attached to is itself closed. If the database handle is closed before
10486** the session object is deleted, then the results of calling any session
10487** module function, including [sqlite3session_delete()] on the session object
10488** are undefined.
10489**
10490** Because the session module uses the [sqlite3_preupdate_hook()] API, it
10491** is not possible for an application to register a pre-update hook on a
10492** database handle that has one or more session objects attached. Nor is
10493** it possible to create a session object attached to a database handle for
10494** which a pre-update hook is already defined. The results of attempting
10495** either of these things are undefined.
10496**
10497** The session object will be used to create changesets for tables in
10498** database zDb, where zDb is either "main", or "temp", or the name of an
10499** attached database. It is not an error if database zDb is not attached
10500** to the database when the session object is created.
10501*/
10502SQLITE_API int sqlite3session_create(
10503 sqlite3 *db, /* Database handle */
10504 const char *zDb, /* Name of db (e.g. "main") */
10505 sqlite3_session **ppSession /* OUT: New session object */
10506);
10507
10508/*
10509** CAPI3REF: Delete A Session Object
10510** DESTRUCTOR: sqlite3_session
10511**
10512** Delete a session object previously allocated using
10513** [sqlite3session_create()]. Once a session object has been deleted, the
10514** results of attempting to use pSession with any other session module
10515** function are undefined.
10516**
10517** Session objects must be deleted before the database handle to which they
10518** are attached is closed. Refer to the documentation for
10519** [sqlite3session_create()] for details.
10520*/
10521SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
10522
10523
10524/*
10525** CAPI3REF: Enable Or Disable A Session Object
10526** METHOD: sqlite3_session
10527**
10528** Enable or disable the recording of changes by a session object. When
10529** enabled, a session object records changes made to the database. When
10530** disabled - it does not. A newly created session object is enabled.
10531** Refer to the documentation for [sqlite3session_changeset()] for further
10532** details regarding how enabling and disabling a session object affects
10533** the eventual changesets.
10534**
10535** Passing zero to this function disables the session. Passing a value
10536** greater than zero enables it. Passing a value less than zero is a
10537** no-op, and may be used to query the current state of the session.
10538**
10539** The return value indicates the final state of the session object: 0 if
10540** the session is disabled, or 1 if it is enabled.
10541*/
10542SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
10543
10544/*
10545** CAPI3REF: Set Or Clear the Indirect Change Flag
10546** METHOD: sqlite3_session
10547**
10548** Each change recorded by a session object is marked as either direct or
10549** indirect. A change is marked as indirect if either:
10550**
10551** <ul>
10552** <li> The session object "indirect" flag is set when the change is
10553** made, or
10554** <li> The change is made by an SQL trigger or foreign key action
10555** instead of directly as a result of a users SQL statement.
10556** </ul>
10557**
10558** If a single row is affected by more than one operation within a session,
10559** then the change is considered indirect if all operations meet the criteria
10560** for an indirect change above, or direct otherwise.
10561**
10562** This function is used to set, clear or query the session object indirect
10563** flag. If the second argument passed to this function is zero, then the
10564** indirect flag is cleared. If it is greater than zero, the indirect flag
10565** is set. Passing a value less than zero does not modify the current value
10566** of the indirect flag, and may be used to query the current state of the
10567** indirect flag for the specified session object.
10568**
10569** The return value indicates the final state of the indirect flag: 0 if
10570** it is clear, or 1 if it is set.
10571*/
10572SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
10573
10574/*
10575** CAPI3REF: Attach A Table To A Session Object
10576** METHOD: sqlite3_session
10577**
10578** If argument zTab is not NULL, then it is the name of a table to attach
10579** to the session object passed as the first argument. All subsequent changes
10580** made to the table while the session object is enabled will be recorded. See
10581** documentation for [sqlite3session_changeset()] for further details.
10582**
10583** Or, if argument zTab is NULL, then changes are recorded for all tables
10584** in the database. If additional tables are added to the database (by
10585** executing "CREATE TABLE" statements) after this call is made, changes for
10586** the new tables are also recorded.
10587**
10588** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
10589** defined as part of their CREATE TABLE statement. It does not matter if the
10590** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
10591** KEY may consist of a single column, or may be a composite key.
10592**
10593** It is not an error if the named table does not exist in the database. Nor
10594** is it an error if the named table does not have a PRIMARY KEY. However,
10595** no changes will be recorded in either of these scenarios.
10596**
10597** Changes are not recorded for individual rows that have NULL values stored
10598** in one or more of their PRIMARY KEY columns.
10599**
10600** SQLITE_OK is returned if the call completes without error. Or, if an error
10601** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
10602**
10603** <h3>Special sqlite_stat1 Handling</h3>
10604**
10605** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
10606** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
10607** <pre>
10608** &nbsp; CREATE TABLE sqlite_stat1(tbl,idx,stat)
10609** </pre>
10610**
10611** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
10612** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
10613** are recorded for rows for which (idx IS NULL) is true. However, for such
10614** rows a zero-length blob (SQL value X'') is stored in the changeset or
10615** patchset instead of a NULL value. This allows such changesets to be
10616** manipulated by legacy implementations of sqlite3changeset_invert(),
10617** concat() and similar.
10618**
10619** The sqlite3changeset_apply() function automatically converts the
10620** zero-length blob back to a NULL value when updating the sqlite_stat1
10621** table. However, if the application calls sqlite3changeset_new(),
10622** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
10623** iterator directly (including on a changeset iterator passed to a
10624** conflict-handler callback) then the X'' value is returned. The application
10625** must translate X'' to NULL itself if required.
10626**
10627** Legacy (older than 3.22.0) versions of the sessions module cannot capture
10628** changes made to the sqlite_stat1 table. Legacy versions of the
10629** sqlite3changeset_apply() function silently ignore any modifications to the
10630** sqlite_stat1 table that are part of a changeset or patchset.
10631*/
10632SQLITE_API int sqlite3session_attach(
10633 sqlite3_session *pSession, /* Session object */
10634 const char *zTab /* Table name */
10635);
10636
10637/*
10638** CAPI3REF: Set a table filter on a Session Object.
10639** METHOD: sqlite3_session
10640**
10641** The second argument (xFilter) is the "filter callback". For changes to rows
10642** in tables that are not attached to the Session object, the filter is called
10643** to determine whether changes to the table's rows should be tracked or not.
10644** If xFilter returns 0, changes is not tracked. Note that once a table is
10645** attached, xFilter will not be called again.
10646*/
10647SQLITE_API void sqlite3session_table_filter(
10648 sqlite3_session *pSession, /* Session object */
10649 int(*xFilter)(
10650 void *pCtx, /* Copy of third arg to _filter_table() */
10651 const char *zTab /* Table name */
10652 ),
10653 void *pCtx /* First argument passed to xFilter */
10654);
10655
10656/*
10657** CAPI3REF: Generate A Changeset From A Session Object
10658** METHOD: sqlite3_session
10659**
10660** Obtain a changeset containing changes to the tables attached to the
10661** session object passed as the first argument. If successful,
10662** set *ppChangeset to point to a buffer containing the changeset
10663** and *pnChangeset to the size of the changeset in bytes before returning
10664** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
10665** zero and return an SQLite error code.
10666**
10667** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
10668** each representing a change to a single row of an attached table. An INSERT
10669** change contains the values of each field of a new database row. A DELETE
10670** contains the original values of each field of a deleted database row. An
10671** UPDATE change contains the original values of each field of an updated
10672** database row along with the updated values for each updated non-primary-key
10673** column. It is not possible for an UPDATE change to represent a change that
10674** modifies the values of primary key columns. If such a change is made, it
10675** is represented in a changeset as a DELETE followed by an INSERT.
10676**
10677** Changes are not recorded for rows that have NULL values stored in one or
10678** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
10679** no corresponding change is present in the changesets returned by this
10680** function. If an existing row with one or more NULL values stored in
10681** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
10682** only an INSERT is appears in the changeset. Similarly, if an existing row
10683** with non-NULL PRIMARY KEY values is updated so that one or more of its
10684** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
10685** DELETE change only.
10686**
10687** The contents of a changeset may be traversed using an iterator created
10688** using the [sqlite3changeset_start()] API. A changeset may be applied to
10689** a database with a compatible schema using the [sqlite3changeset_apply()]
10690** API.
10691**
10692** Within a changeset generated by this function, all changes related to a
10693** single table are grouped together. In other words, when iterating through
10694** a changeset or when applying a changeset to a database, all changes related
10695** to a single table are processed before moving on to the next table. Tables
10696** are sorted in the same order in which they were attached (or auto-attached)
10697** to the sqlite3_session object. The order in which the changes related to
10698** a single table are stored is undefined.
10699**
10700** Following a successful call to this function, it is the responsibility of
10701** the caller to eventually free the buffer that *ppChangeset points to using
10702** [sqlite3_free()].
10703**
10704** <h3>Changeset Generation</h3>
10705**
10706** Once a table has been attached to a session object, the session object
10707** records the primary key values of all new rows inserted into the table.
10708** It also records the original primary key and other column values of any
10709** deleted or updated rows. For each unique primary key value, data is only
10710** recorded once - the first time a row with said primary key is inserted,
10711** updated or deleted in the lifetime of the session.
10712**
10713** There is one exception to the previous paragraph: when a row is inserted,
10714** updated or deleted, if one or more of its primary key columns contain a
10715** NULL value, no record of the change is made.
10716**
10717** The session object therefore accumulates two types of records - those
10718** that consist of primary key values only (created when the user inserts
10719** a new record) and those that consist of the primary key values and the
10720** original values of other table columns (created when the users deletes
10721** or updates a record).
10722**
10723** When this function is called, the requested changeset is created using
10724** both the accumulated records and the current contents of the database
10725** file. Specifically:
10726**
10727** <ul>
10728** <li> For each record generated by an insert, the database is queried
10729** for a row with a matching primary key. If one is found, an INSERT
10730** change is added to the changeset. If no such row is found, no change
10731** is added to the changeset.
10732**
10733** <li> For each record generated by an update or delete, the database is
10734** queried for a row with a matching primary key. If such a row is
10735** found and one or more of the non-primary key fields have been
10736** modified from their original values, an UPDATE change is added to
10737** the changeset. Or, if no such row is found in the table, a DELETE
10738** change is added to the changeset. If there is a row with a matching
10739** primary key in the database, but all fields contain their original
10740** values, no change is added to the changeset.
10741** </ul>
10742**
10743** This means, amongst other things, that if a row is inserted and then later
10744** deleted while a session object is active, neither the insert nor the delete
10745** will be present in the changeset. Or if a row is deleted and then later a
10746** row with the same primary key values inserted while a session object is
10747** active, the resulting changeset will contain an UPDATE change instead of
10748** a DELETE and an INSERT.
10749**
10750** When a session object is disabled (see the [sqlite3session_enable()] API),
10751** it does not accumulate records when rows are inserted, updated or deleted.
10752** This may appear to have some counter-intuitive effects if a single row
10753** is written to more than once during a session. For example, if a row
10754** is inserted while a session object is enabled, then later deleted while
10755** the same session object is disabled, no INSERT record will appear in the
10756** changeset, even though the delete took place while the session was disabled.
10757** Or, if one field of a row is updated while a session is disabled, and
10758** another field of the same row is updated while the session is enabled, the
10759** resulting changeset will contain an UPDATE change that updates both fields.
10760*/
10761SQLITE_API int sqlite3session_changeset(
10762 sqlite3_session *pSession, /* Session object */
10763 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
10764 void **ppChangeset /* OUT: Buffer containing changeset */
10765);
10766
10767/*
10768** CAPI3REF: Load The Difference Between Tables Into A Session
10769** METHOD: sqlite3_session
10770**
10771** If it is not already attached to the session object passed as the first
10772** argument, this function attaches table zTbl in the same manner as the
10773** [sqlite3session_attach()] function. If zTbl does not exist, or if it
10774** does not have a primary key, this function is a no-op (but does not return
10775** an error).
10776**
10777** Argument zFromDb must be the name of a database ("main", "temp" etc.)
10778** attached to the same database handle as the session object that contains
10779** a table compatible with the table attached to the session by this function.
10780** A table is considered compatible if it:
10781**
10782** <ul>
10783** <li> Has the same name,
10784** <li> Has the same set of columns declared in the same order, and
10785** <li> Has the same PRIMARY KEY definition.
10786** </ul>
10787**
10788** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
10789** are compatible but do not have any PRIMARY KEY columns, it is not an error
10790** but no changes are added to the session object. As with other session
10791** APIs, tables without PRIMARY KEYs are simply ignored.
10792**
10793** This function adds a set of changes to the session object that could be
10794** used to update the table in database zFrom (call this the "from-table")
10795** so that its content is the same as the table attached to the session
10796** object (call this the "to-table"). Specifically:
10797**
10798** <ul>
10799** <li> For each row (primary key) that exists in the to-table but not in
10800** the from-table, an INSERT record is added to the session object.
10801**
10802** <li> For each row (primary key) that exists in the to-table but not in
10803** the from-table, a DELETE record is added to the session object.
10804**
10805** <li> For each row (primary key) that exists in both tables, but features
10806** different non-PK values in each, an UPDATE record is added to the
10807** session.
10808** </ul>
10809**
10810** To clarify, if this function is called and then a changeset constructed
10811** using [sqlite3session_changeset()], then after applying that changeset to
10812** database zFrom the contents of the two compatible tables would be
10813** identical.
10814**
10815** It an error if database zFrom does not exist or does not contain the
10816** required compatible table.
10817**
10818** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite
10819** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
10820** may be set to point to a buffer containing an English language error
10821** message. It is the responsibility of the caller to free this buffer using
10822** sqlite3_free().
10823*/
10824SQLITE_API int sqlite3session_diff(
10825 sqlite3_session *pSession,
10826 const char *zFromDb,
10827 const char *zTbl,
10828 char **pzErrMsg
10829);
10830
10831
10832/*
10833** CAPI3REF: Generate A Patchset From A Session Object
10834** METHOD: sqlite3_session
10835**
10836** The differences between a patchset and a changeset are that:
10837**
10838** <ul>
10839** <li> DELETE records consist of the primary key fields only. The
10840** original values of other fields are omitted.
10841** <li> The original values of any modified fields are omitted from
10842** UPDATE records.
10843** </ul>
10844**
10845** A patchset blob may be used with up to date versions of all
10846** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
10847** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
10848** attempting to use a patchset blob with old versions of the
10849** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
10850**
10851** Because the non-primary key "old.*" fields are omitted, no
10852** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
10853** is passed to the sqlite3changeset_apply() API. Other conflict types work
10854** in the same way as for changesets.
10855**
10856** Changes within a patchset are ordered in the same way as for changesets
10857** generated by the sqlite3session_changeset() function (i.e. all changes for
10858** a single table are grouped together, tables appear in the order in which
10859** they were attached to the session object).
10860*/
10861SQLITE_API int sqlite3session_patchset(
10862 sqlite3_session *pSession, /* Session object */
10863 int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */
10864 void **ppPatchset /* OUT: Buffer containing patchset */
10865);
10866
10867/*
10868** CAPI3REF: Test if a changeset has recorded any changes.
10869**
10870** Return non-zero if no changes to attached tables have been recorded by
10871** the session object passed as the first argument. Otherwise, if one or
10872** more changes have been recorded, return zero.
10873**
10874** Even if this function returns zero, it is possible that calling
10875** [sqlite3session_changeset()] on the session handle may still return a
10876** changeset that contains no changes. This can happen when a row in
10877** an attached table is modified and then later on the original values
10878** are restored. However, if this function returns non-zero, then it is
10879** guaranteed that a call to sqlite3session_changeset() will return a
10880** changeset containing zero changes.
10881*/
10882SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
10883
10884/*
10885** CAPI3REF: Create An Iterator To Traverse A Changeset
10886** CONSTRUCTOR: sqlite3_changeset_iter
10887**
10888** Create an iterator used to iterate through the contents of a changeset.
10889** If successful, *pp is set to point to the iterator handle and SQLITE_OK
10890** is returned. Otherwise, if an error occurs, *pp is set to zero and an
10891** SQLite error code is returned.
10892**
10893** The following functions can be used to advance and query a changeset
10894** iterator created by this function:
10895**
10896** <ul>
10897** <li> [sqlite3changeset_next()]
10898** <li> [sqlite3changeset_op()]
10899** <li> [sqlite3changeset_new()]
10900** <li> [sqlite3changeset_old()]
10901** </ul>
10902**
10903** It is the responsibility of the caller to eventually destroy the iterator
10904** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
10905** changeset (pChangeset) must remain valid until after the iterator is
10906** destroyed.
10907**
10908** Assuming the changeset blob was created by one of the
10909** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
10910** [sqlite3changeset_invert()] functions, all changes within the changeset
10911** that apply to a single table are grouped together. This means that when
10912** an application iterates through a changeset using an iterator created by
10913** this function, all changes that relate to a single table are visited
10914** consecutively. There is no chance that the iterator will visit a change
10915** the applies to table X, then one for table Y, and then later on visit
10916** another change for table X.
10917*/
10918SQLITE_API int sqlite3changeset_start(
10919 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
10920 int nChangeset, /* Size of changeset blob in bytes */
10921 void *pChangeset /* Pointer to blob containing changeset */
10922);
10923
10924
10925/*
10926** CAPI3REF: Advance A Changeset Iterator
10927** METHOD: sqlite3_changeset_iter
10928**
10929** This function may only be used with iterators created by function
10930** [sqlite3changeset_start()]. If it is called on an iterator passed to
10931** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
10932** is returned and the call has no effect.
10933**
10934** Immediately after an iterator is created by sqlite3changeset_start(), it
10935** does not point to any change in the changeset. Assuming the changeset
10936** is not empty, the first call to this function advances the iterator to
10937** point to the first change in the changeset. Each subsequent call advances
10938** the iterator to point to the next change in the changeset (if any). If
10939** no error occurs and the iterator points to a valid change after a call
10940** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
10941** Otherwise, if all changes in the changeset have already been visited,
10942** SQLITE_DONE is returned.
10943**
10944** If an error occurs, an SQLite error code is returned. Possible error
10945** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
10946** SQLITE_NOMEM.
10947*/
10948SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
10949
10950/*
10951** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
10952** METHOD: sqlite3_changeset_iter
10953**
10954** The pIter argument passed to this function may either be an iterator
10955** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10956** created by [sqlite3changeset_start()]. In the latter case, the most recent
10957** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
10958** is not the case, this function returns [SQLITE_MISUSE].
10959**
10960** If argument pzTab is not NULL, then *pzTab is set to point to a
10961** nul-terminated utf-8 encoded string containing the name of the table
10962** affected by the current change. The buffer remains valid until either
10963** sqlite3changeset_next() is called on the iterator or until the
10964** conflict-handler function returns. If pnCol is not NULL, then *pnCol is
10965** set to the number of columns in the table affected by the change. If
10966** pbIncorrect is not NULL, then *pbIndirect is set to true (1) if the change
10967** is an indirect change, or false (0) otherwise. See the documentation for
10968** [sqlite3session_indirect()] for a description of direct and indirect
10969** changes. Finally, if pOp is not NULL, then *pOp is set to one of
10970** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the
10971** type of change that the iterator currently points to.
10972**
10973** If no error occurs, SQLITE_OK is returned. If an error does occur, an
10974** SQLite error code is returned. The values of the output variables may not
10975** be trusted in this case.
10976*/
10977SQLITE_API int sqlite3changeset_op(
10978 sqlite3_changeset_iter *pIter, /* Iterator object */
10979 const char **pzTab, /* OUT: Pointer to table name */
10980 int *pnCol, /* OUT: Number of columns in table */
10981 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
10982 int *pbIndirect /* OUT: True for an 'indirect' change */
10983);
10984
10985/*
10986** CAPI3REF: Obtain The Primary Key Definition Of A Table
10987** METHOD: sqlite3_changeset_iter
10988**
10989** For each modified table, a changeset includes the following:
10990**
10991** <ul>
10992** <li> The number of columns in the table, and
10993** <li> Which of those columns make up the tables PRIMARY KEY.
10994** </ul>
10995**
10996** This function is used to find which columns comprise the PRIMARY KEY of
10997** the table modified by the change that iterator pIter currently points to.
10998** If successful, *pabPK is set to point to an array of nCol entries, where
10999** nCol is the number of columns in the table. Elements of *pabPK are set to
11000** 0x01 if the corresponding column is part of the tables primary key, or
11001** 0x00 if it is not.
11002**
11003** If argument pnCol is not NULL, then *pnCol is set to the number of columns
11004** in the table.
11005**
11006** If this function is called when the iterator does not point to a valid
11007** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
11008** SQLITE_OK is returned and the output variables populated as described
11009** above.
11010*/
11011SQLITE_API int sqlite3changeset_pk(
11012 sqlite3_changeset_iter *pIter, /* Iterator object */
11013 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11014 int *pnCol /* OUT: Number of entries in output array */
11015);
11016
11017/*
11018** CAPI3REF: Obtain old.* Values From A Changeset Iterator
11019** METHOD: sqlite3_changeset_iter
11020**
11021** The pIter argument passed to this function may either be an iterator
11022** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11023** created by [sqlite3changeset_start()]. In the latter case, the most recent
11024** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11025** Furthermore, it may only be called if the type of change that the iterator
11026** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
11027** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11028**
11029** Argument iVal must be greater than or equal to 0, and less than the number
11030** of columns in the table affected by the current change. Otherwise,
11031** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11032**
11033** If successful, this function sets *ppValue to point to a protected
11034** sqlite3_value object containing the iVal'th value from the vector of
11035** original row values stored as part of the UPDATE or DELETE change and
11036** returns SQLITE_OK. The name of the function comes from the fact that this
11037** is similar to the "old.*" columns available to update or delete triggers.
11038**
11039** If some other error occurs (e.g. an OOM condition), an SQLite error code
11040** is returned and *ppValue is set to NULL.
11041*/
11042SQLITE_API int sqlite3changeset_old(
11043 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11044 int iVal, /* Column number */
11045 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
11046);
11047
11048/*
11049** CAPI3REF: Obtain new.* Values From A Changeset Iterator
11050** METHOD: sqlite3_changeset_iter
11051**
11052** The pIter argument passed to this function may either be an iterator
11053** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11054** created by [sqlite3changeset_start()]. In the latter case, the most recent
11055** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11056** Furthermore, it may only be called if the type of change that the iterator
11057** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
11058** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11059**
11060** Argument iVal must be greater than or equal to 0, and less than the number
11061** of columns in the table affected by the current change. Otherwise,
11062** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11063**
11064** If successful, this function sets *ppValue to point to a protected
11065** sqlite3_value object containing the iVal'th value from the vector of
11066** new row values stored as part of the UPDATE or INSERT change and
11067** returns SQLITE_OK. If the change is an UPDATE and does not include
11068** a new value for the requested column, *ppValue is set to NULL and
11069** SQLITE_OK returned. The name of the function comes from the fact that
11070** this is similar to the "new.*" columns available to update or delete
11071** triggers.
11072**
11073** If some other error occurs (e.g. an OOM condition), an SQLite error code
11074** is returned and *ppValue is set to NULL.
11075*/
11076SQLITE_API int sqlite3changeset_new(
11077 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11078 int iVal, /* Column number */
11079 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
11080);
11081
11082/*
11083** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
11084** METHOD: sqlite3_changeset_iter
11085**
11086** This function should only be used with iterator objects passed to a
11087** conflict-handler callback by [sqlite3changeset_apply()] with either
11088** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
11089** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
11090** is set to NULL.
11091**
11092** Argument iVal must be greater than or equal to 0, and less than the number
11093** of columns in the table affected by the current change. Otherwise,
11094** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11095**
11096** If successful, this function sets *ppValue to point to a protected
11097** sqlite3_value object containing the iVal'th value from the
11098** "conflicting row" associated with the current conflict-handler callback
11099** and returns SQLITE_OK.
11100**
11101** If some other error occurs (e.g. an OOM condition), an SQLite error code
11102** is returned and *ppValue is set to NULL.
11103*/
11104SQLITE_API int sqlite3changeset_conflict(
11105 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11106 int iVal, /* Column number */
11107 sqlite3_value **ppValue /* OUT: Value from conflicting row */
11108);
11109
11110/*
11111** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
11112** METHOD: sqlite3_changeset_iter
11113**
11114** This function may only be called with an iterator passed to an
11115** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
11116** it sets the output variable to the total number of known foreign key
11117** violations in the destination database and returns SQLITE_OK.
11118**
11119** In all other cases this function returns SQLITE_MISUSE.
11120*/
11121SQLITE_API int sqlite3changeset_fk_conflicts(
11122 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11123 int *pnOut /* OUT: Number of FK violations */
11124);
11125
11126
11127/*
11128** CAPI3REF: Finalize A Changeset Iterator
11129** METHOD: sqlite3_changeset_iter
11130**
11131** This function is used to finalize an iterator allocated with
11132** [sqlite3changeset_start()].
11133**
11134** This function should only be called on iterators created using the
11135** [sqlite3changeset_start()] function. If an application calls this
11136** function with an iterator passed to a conflict-handler by
11137** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
11138** call has no effect.
11139**
11140** If an error was encountered within a call to an sqlite3changeset_xxx()
11141** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
11142** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
11143** to that error is returned by this function. Otherwise, SQLITE_OK is
11144** returned. This is to allow the following pattern (pseudo-code):
11145**
11146** <pre>
11147** sqlite3changeset_start();
11148** while( SQLITE_ROW==sqlite3changeset_next() ){
11149** // Do something with change.
11150** }
11151** rc = sqlite3changeset_finalize();
11152** if( rc!=SQLITE_OK ){
11153** // An error has occurred
11154** }
11155** </pre>
11156*/
11157SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
11158
11159/*
11160** CAPI3REF: Invert A Changeset
11161**
11162** This function is used to "invert" a changeset object. Applying an inverted
11163** changeset to a database reverses the effects of applying the uninverted
11164** changeset. Specifically:
11165**
11166** <ul>
11167** <li> Each DELETE change is changed to an INSERT, and
11168** <li> Each INSERT change is changed to a DELETE, and
11169** <li> For each UPDATE change, the old.* and new.* values are exchanged.
11170** </ul>
11171**
11172** This function does not change the order in which changes appear within
11173** the changeset. It merely reverses the sense of each individual change.
11174**
11175** If successful, a pointer to a buffer containing the inverted changeset
11176** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
11177** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
11178** zeroed and an SQLite error code returned.
11179**
11180** It is the responsibility of the caller to eventually call sqlite3_free()
11181** on the *ppOut pointer to free the buffer allocation following a successful
11182** call to this function.
11183**
11184** WARNING/TODO: This function currently assumes that the input is a valid
11185** changeset. If it is not, the results are undefined.
11186*/
11187SQLITE_API int sqlite3changeset_invert(
11188 int nIn, const void *pIn, /* Input changeset */
11189 int *pnOut, void **ppOut /* OUT: Inverse of input */
11190);
11191
11192/*
11193** CAPI3REF: Concatenate Two Changeset Objects
11194**
11195** This function is used to concatenate two changesets, A and B, into a
11196** single changeset. The result is a changeset equivalent to applying
11197** changeset A followed by changeset B.
11198**
11199** This function combines the two input changesets using an
11200** sqlite3_changegroup object. Calling it produces similar results as the
11201** following code fragment:
11202**
11203** <pre>
11204** sqlite3_changegroup *pGrp;
11205** rc = sqlite3_changegroup_new(&pGrp);
11206** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
11207** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
11208** if( rc==SQLITE_OK ){
11209** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
11210** }else{
11211** *ppOut = 0;
11212** *pnOut = 0;
11213** }
11214** </pre>
11215**
11216** Refer to the sqlite3_changegroup documentation below for details.
11217*/
11218SQLITE_API int sqlite3changeset_concat(
11219 int nA, /* Number of bytes in buffer pA */
11220 void *pA, /* Pointer to buffer containing changeset A */
11221 int nB, /* Number of bytes in buffer pB */
11222 void *pB, /* Pointer to buffer containing changeset B */
11223 int *pnOut, /* OUT: Number of bytes in output changeset */
11224 void **ppOut /* OUT: Buffer containing output changeset */
11225);
11226
11227
11228/*
11229** CAPI3REF: Changegroup Handle
11230**
11231** A changegroup is an object used to combine two or more
11232** [changesets] or [patchsets]
11233*/
11234typedef struct sqlite3_changegroup sqlite3_changegroup;
11235
11236/*
11237** CAPI3REF: Create A New Changegroup Object
11238** CONSTRUCTOR: sqlite3_changegroup
11239**
11240** An sqlite3_changegroup object is used to combine two or more changesets
11241** (or patchsets) into a single changeset (or patchset). A single changegroup
11242** object may combine changesets or patchsets, but not both. The output is
11243** always in the same format as the input.
11244**
11245** If successful, this function returns SQLITE_OK and populates (*pp) with
11246** a pointer to a new sqlite3_changegroup object before returning. The caller
11247** should eventually free the returned object using a call to
11248** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
11249** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
11250**
11251** The usual usage pattern for an sqlite3_changegroup object is as follows:
11252**
11253** <ul>
11254** <li> It is created using a call to sqlite3changegroup_new().
11255**
11256** <li> Zero or more changesets (or patchsets) are added to the object
11257** by calling sqlite3changegroup_add().
11258**
11259** <li> The result of combining all input changesets together is obtained
11260** by the application via a call to sqlite3changegroup_output().
11261**
11262** <li> The object is deleted using a call to sqlite3changegroup_delete().
11263** </ul>
11264**
11265** Any number of calls to add() and output() may be made between the calls to
11266** new() and delete(), and in any order.
11267**
11268** As well as the regular sqlite3changegroup_add() and
11269** sqlite3changegroup_output() functions, also available are the streaming
11270** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
11271*/
11272SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
11273
11274/*
11275** CAPI3REF: Add A Changeset To A Changegroup
11276** METHOD: sqlite3_changegroup
11277**
11278** Add all changes within the changeset (or patchset) in buffer pData (size
11279** nData bytes) to the changegroup.
11280**
11281** If the buffer contains a patchset, then all prior calls to this function
11282** on the same changegroup object must also have specified patchsets. Or, if
11283** the buffer contains a changeset, so must have the earlier calls to this
11284** function. Otherwise, SQLITE_ERROR is returned and no changes are added
11285** to the changegroup.
11286**
11287** Rows within the changeset and changegroup are identified by the values in
11288** their PRIMARY KEY columns. A change in the changeset is considered to
11289** apply to the same row as a change already present in the changegroup if
11290** the two rows have the same primary key.
11291**
11292** Changes to rows that do not already appear in the changegroup are
11293** simply copied into it. Or, if both the new changeset and the changegroup
11294** contain changes that apply to a single row, the final contents of the
11295** changegroup depends on the type of each change, as follows:
11296**
11297** <table border=1 style="margin-left:8ex;margin-right:8ex">
11298** <tr><th style="white-space:pre">Existing Change </th>
11299** <th style="white-space:pre">New Change </th>
11300** <th>Output Change
11301** <tr><td>INSERT <td>INSERT <td>
11302** The new change is ignored. This case does not occur if the new
11303** changeset was recorded immediately after the changesets already
11304** added to the changegroup.
11305** <tr><td>INSERT <td>UPDATE <td>
11306** The INSERT change remains in the changegroup. The values in the
11307** INSERT change are modified as if the row was inserted by the
11308** existing change and then updated according to the new change.
11309** <tr><td>INSERT <td>DELETE <td>
11310** The existing INSERT is removed from the changegroup. The DELETE is
11311** not added.
11312** <tr><td>UPDATE <td>INSERT <td>
11313** The new change is ignored. This case does not occur if the new
11314** changeset was recorded immediately after the changesets already
11315** added to the changegroup.
11316** <tr><td>UPDATE <td>UPDATE <td>
11317** The existing UPDATE remains within the changegroup. It is amended
11318** so that the accompanying values are as if the row was updated once
11319** by the existing change and then again by the new change.
11320** <tr><td>UPDATE <td>DELETE <td>
11321** The existing UPDATE is replaced by the new DELETE within the
11322** changegroup.
11323** <tr><td>DELETE <td>INSERT <td>
11324** If one or more of the column values in the row inserted by the
11325** new change differ from those in the row deleted by the existing
11326** change, the existing DELETE is replaced by an UPDATE within the
11327** changegroup. Otherwise, if the inserted row is exactly the same
11328** as the deleted row, the existing DELETE is simply discarded.
11329** <tr><td>DELETE <td>UPDATE <td>
11330** The new change is ignored. This case does not occur if the new
11331** changeset was recorded immediately after the changesets already
11332** added to the changegroup.
11333** <tr><td>DELETE <td>DELETE <td>
11334** The new change is ignored. This case does not occur if the new
11335** changeset was recorded immediately after the changesets already
11336** added to the changegroup.
11337** </table>
11338**
11339** If the new changeset contains changes to a table that is already present
11340** in the changegroup, then the number of columns and the position of the
11341** primary key columns for the table must be consistent. If this is not the
11342** case, this function fails with SQLITE_SCHEMA. If the input changeset
11343** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
11344** returned. Or, if an out-of-memory condition occurs during processing, this
11345** function returns SQLITE_NOMEM. In all cases, if an error occurs the
11346** final contents of the changegroup is undefined.
11347**
11348** If no error occurs, SQLITE_OK is returned.
11349*/
11350SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
11351
11352/*
11353** CAPI3REF: Obtain A Composite Changeset From A Changegroup
11354** METHOD: sqlite3_changegroup
11355**
11356** Obtain a buffer containing a changeset (or patchset) representing the
11357** current contents of the changegroup. If the inputs to the changegroup
11358** were themselves changesets, the output is a changeset. Or, if the
11359** inputs were patchsets, the output is also a patchset.
11360**
11361** As with the output of the sqlite3session_changeset() and
11362** sqlite3session_patchset() functions, all changes related to a single
11363** table are grouped together in the output of this function. Tables appear
11364** in the same order as for the very first changeset added to the changegroup.
11365** If the second or subsequent changesets added to the changegroup contain
11366** changes for tables that do not appear in the first changeset, they are
11367** appended onto the end of the output changeset, again in the order in
11368** which they are first encountered.
11369**
11370** If an error occurs, an SQLite error code is returned and the output
11371** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
11372** is returned and the output variables are set to the size of and a
11373** pointer to the output buffer, respectively. In this case it is the
11374** responsibility of the caller to eventually free the buffer using a
11375** call to sqlite3_free().
11376*/
11377SQLITE_API int sqlite3changegroup_output(
11378 sqlite3_changegroup*,
11379 int *pnData, /* OUT: Size of output buffer in bytes */
11380 void **ppData /* OUT: Pointer to output buffer */
11381);
11382
11383/*
11384** CAPI3REF: Delete A Changegroup Object
11385** DESTRUCTOR: sqlite3_changegroup
11386*/
11387SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
11388
11389/*
11390** CAPI3REF: Apply A Changeset To A Database
11391**
11392** Apply a changeset or patchset to a database. These functions attempt to
11393** update the "main" database attached to handle db with the changes found in
11394** the changeset passed via the second and third arguments.
11395**
11396** The fourth argument (xFilter) passed to these functions is the "filter
11397** callback". If it is not NULL, then for each table affected by at least one
11398** change in the changeset, the filter callback is invoked with
11399** the table name as the second argument, and a copy of the context pointer
11400** passed as the sixth argument as the first. If the "filter callback"
11401** returns zero, then no attempt is made to apply any changes to the table.
11402** Otherwise, if the return value is non-zero or the xFilter argument to
11403** is NULL, all changes related to the table are attempted.
11404**
11405** For each table that is not excluded by the filter callback, this function
11406** tests that the target database contains a compatible table. A table is
11407** considered compatible if all of the following are true:
11408**
11409** <ul>
11410** <li> The table has the same name as the name recorded in the
11411** changeset, and
11412** <li> The table has at least as many columns as recorded in the
11413** changeset, and
11414** <li> The table has primary key columns in the same position as
11415** recorded in the changeset.
11416** </ul>
11417**
11418** If there is no compatible table, it is not an error, but none of the
11419** changes associated with the table are applied. A warning message is issued
11420** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
11421** one such warning is issued for each table in the changeset.
11422**
11423** For each change for which there is a compatible table, an attempt is made
11424** to modify the table contents according to the UPDATE, INSERT or DELETE
11425** change. If a change cannot be applied cleanly, the conflict handler
11426** function passed as the fifth argument to sqlite3changeset_apply() may be
11427** invoked. A description of exactly when the conflict handler is invoked for
11428** each type of change is below.
11429**
11430** Unlike the xFilter argument, xConflict may not be passed NULL. The results
11431** of passing anything other than a valid function pointer as the xConflict
11432** argument are undefined.
11433**
11434** Each time the conflict handler function is invoked, it must return one
11435** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
11436** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
11437** if the second argument passed to the conflict handler is either
11438** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
11439** returns an illegal value, any changes already made are rolled back and
11440** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
11441** actions are taken by sqlite3changeset_apply() depending on the value
11442** returned by each invocation of the conflict-handler function. Refer to
11443** the documentation for the three
11444** [SQLITE_CHANGESET_OMIT|available return values] for details.
11445**
11446** <dl>
11447** <dt>DELETE Changes<dd>
11448** For each DELETE change, the function checks if the target database
11449** contains a row with the same primary key value (or values) as the
11450** original row values stored in the changeset. If it does, and the values
11451** stored in all non-primary key columns also match the values stored in
11452** the changeset the row is deleted from the target database.
11453**
11454** If a row with matching primary key values is found, but one or more of
11455** the non-primary key fields contains a value different from the original
11456** row value stored in the changeset, the conflict-handler function is
11457** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
11458** database table has more columns than are recorded in the changeset,
11459** only the values of those non-primary key fields are compared against
11460** the current database contents - any trailing database table columns
11461** are ignored.
11462**
11463** If no row with matching primary key values is found in the database,
11464** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11465** passed as the second argument.
11466**
11467** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
11468** (which can only happen if a foreign key constraint is violated), the
11469** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
11470** passed as the second argument. This includes the case where the DELETE
11471** operation is attempted because an earlier call to the conflict handler
11472** function returned [SQLITE_CHANGESET_REPLACE].
11473**
11474** <dt>INSERT Changes<dd>
11475** For each INSERT change, an attempt is made to insert the new row into
11476** the database. If the changeset row contains fewer fields than the
11477** database table, the trailing fields are populated with their default
11478** values.
11479**
11480** If the attempt to insert the row fails because the database already
11481** contains a row with the same primary key values, the conflict handler
11482** function is invoked with the second argument set to
11483** [SQLITE_CHANGESET_CONFLICT].
11484**
11485** If the attempt to insert the row fails because of some other constraint
11486** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
11487** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
11488** This includes the case where the INSERT operation is re-attempted because
11489** an earlier call to the conflict handler function returned
11490** [SQLITE_CHANGESET_REPLACE].
11491**
11492** <dt>UPDATE Changes<dd>
11493** For each UPDATE change, the function checks if the target database
11494** contains a row with the same primary key value (or values) as the
11495** original row values stored in the changeset. If it does, and the values
11496** stored in all modified non-primary key columns also match the values
11497** stored in the changeset the row is updated within the target database.
11498**
11499** If a row with matching primary key values is found, but one or more of
11500** the modified non-primary key fields contains a value different from an
11501** original row value stored in the changeset, the conflict-handler function
11502** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
11503** UPDATE changes only contain values for non-primary key fields that are
11504** to be modified, only those fields need to match the original values to
11505** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
11506**
11507** If no row with matching primary key values is found in the database,
11508** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11509** passed as the second argument.
11510**
11511** If the UPDATE operation is attempted, but SQLite returns
11512** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
11513** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
11514** This includes the case where the UPDATE operation is attempted after
11515** an earlier call to the conflict handler function returned
11516** [SQLITE_CHANGESET_REPLACE].
11517** </dl>
11518**
11519** It is safe to execute SQL statements, including those that write to the
11520** table that the callback related to, from within the xConflict callback.
11521** This can be used to further customize the applications conflict
11522** resolution strategy.
11523**
11524** All changes made by these functions are enclosed in a savepoint transaction.
11525** If any other error (aside from a constraint failure when attempting to
11526** write to the target database) occurs, then the savepoint transaction is
11527** rolled back, restoring the target database to its original state, and an
11528** SQLite error code returned.
11529**
11530** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
11531** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
11532** may set (*ppRebase) to point to a "rebase" that may be used with the
11533** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
11534** is set to the size of the buffer in bytes. It is the responsibility of the
11535** caller to eventually free any such buffer using sqlite3_free(). The buffer
11536** is only allocated and populated if one or more conflicts were encountered
11537** while applying the patchset. See comments surrounding the sqlite3_rebaser
11538** APIs for further details.
11539**
11540** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
11541** may be modified by passing a combination of
11542** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
11543**
11544** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
11545** and therefore subject to change.
11546*/
11547SQLITE_API int sqlite3changeset_apply(
11548 sqlite3 *db, /* Apply change to "main" db of this handle */
11549 int nChangeset, /* Size of changeset in bytes */
11550 void *pChangeset, /* Changeset blob */
11551 int(*xFilter)(
11552 void *pCtx, /* Copy of sixth arg to _apply() */
11553 const char *zTab /* Table name */
11554 ),
11555 int(*xConflict)(
11556 void *pCtx, /* Copy of sixth arg to _apply() */
11557 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11558 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11559 ),
11560 void *pCtx /* First argument passed to xConflict */
11561);
11562SQLITE_API int sqlite3changeset_apply_v2(
11563 sqlite3 *db, /* Apply change to "main" db of this handle */
11564 int nChangeset, /* Size of changeset in bytes */
11565 void *pChangeset, /* Changeset blob */
11566 int(*xFilter)(
11567 void *pCtx, /* Copy of sixth arg to _apply() */
11568 const char *zTab /* Table name */
11569 ),
11570 int(*xConflict)(
11571 void *pCtx, /* Copy of sixth arg to _apply() */
11572 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11573 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11574 ),
11575 void *pCtx, /* First argument passed to xConflict */
11576 void **ppRebase, int *pnRebase, /* OUT: Rebase data */
11577 int flags /* Combination of SESSION_APPLY_* flags */
11578);
11579
11580/*
11581** CAPI3REF: Flags for sqlite3changeset_apply_v2
11582**
11583** The following flags may passed via the 9th parameter to
11584** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
11585**
11586** <dl>
11587** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
11588** Usually, the sessions module encloses all operations performed by
11589** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
11590** SAVEPOINT is committed if the changeset or patchset is successfully
11591** applied, or rolled back if an error occurs. Specifying this flag
11592** causes the sessions module to omit this savepoint. In this case, if the
11593** caller has an open transaction or savepoint when apply_v2() is called,
11594** it may revert the partially applied changeset by rolling it back.
11595*/
11596#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001
11597
11598/*
11599** CAPI3REF: Constants Passed To The Conflict Handler
11600**
11601** Values that may be passed as the second argument to a conflict-handler.
11602**
11603** <dl>
11604** <dt>SQLITE_CHANGESET_DATA<dd>
11605** The conflict handler is invoked with CHANGESET_DATA as the second argument
11606** when processing a DELETE or UPDATE change if a row with the required
11607** PRIMARY KEY fields is present in the database, but one or more other
11608** (non primary-key) fields modified by the update do not contain the
11609** expected "before" values.
11610**
11611** The conflicting row, in this case, is the database row with the matching
11612** primary key.
11613**
11614** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
11615** The conflict handler is invoked with CHANGESET_NOTFOUND as the second
11616** argument when processing a DELETE or UPDATE change if a row with the
11617** required PRIMARY KEY fields is not present in the database.
11618**
11619** There is no conflicting row in this case. The results of invoking the
11620** sqlite3changeset_conflict() API are undefined.
11621**
11622** <dt>SQLITE_CHANGESET_CONFLICT<dd>
11623** CHANGESET_CONFLICT is passed as the second argument to the conflict
11624** handler while processing an INSERT change if the operation would result
11625** in duplicate primary key values.
11626**
11627** The conflicting row in this case is the database row with the matching
11628** primary key.
11629**
11630** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
11631** If foreign key handling is enabled, and applying a changeset leaves the
11632** database in a state containing foreign key violations, the conflict
11633** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
11634** exactly once before the changeset is committed. If the conflict handler
11635** returns CHANGESET_OMIT, the changes, including those that caused the
11636** foreign key constraint violation, are committed. Or, if it returns
11637** CHANGESET_ABORT, the changeset is rolled back.
11638**
11639** No current or conflicting row information is provided. The only function
11640** it is possible to call on the supplied sqlite3_changeset_iter handle
11641** is sqlite3changeset_fk_conflicts().
11642**
11643** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
11644** If any other constraint violation occurs while applying a change (i.e.
11645** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
11646** invoked with CHANGESET_CONSTRAINT as the second argument.
11647**
11648** There is no conflicting row in this case. The results of invoking the
11649** sqlite3changeset_conflict() API are undefined.
11650**
11651** </dl>
11652*/
11653#define SQLITE_CHANGESET_DATA 1
11654#define SQLITE_CHANGESET_NOTFOUND 2
11655#define SQLITE_CHANGESET_CONFLICT 3
11656#define SQLITE_CHANGESET_CONSTRAINT 4
11657#define SQLITE_CHANGESET_FOREIGN_KEY 5
11658
11659/*
11660** CAPI3REF: Constants Returned By The Conflict Handler
11661**
11662** A conflict handler callback must return one of the following three values.
11663**
11664** <dl>
11665** <dt>SQLITE_CHANGESET_OMIT<dd>
11666** If a conflict handler returns this value no special action is taken. The
11667** change that caused the conflict is not applied. The session module
11668** continues to the next change in the changeset.
11669**
11670** <dt>SQLITE_CHANGESET_REPLACE<dd>
11671** This value may only be returned if the second argument to the conflict
11672** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
11673** is not the case, any changes applied so far are rolled back and the
11674** call to sqlite3changeset_apply() returns SQLITE_MISUSE.
11675**
11676** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
11677** handler, then the conflicting row is either updated or deleted, depending
11678** on the type of change.
11679**
11680** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
11681** handler, then the conflicting row is removed from the database and a
11682** second attempt to apply the change is made. If this second attempt fails,
11683** the original row is restored to the database before continuing.
11684**
11685** <dt>SQLITE_CHANGESET_ABORT<dd>
11686** If this value is returned, any changes applied so far are rolled back
11687** and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
11688** </dl>
11689*/
11690#define SQLITE_CHANGESET_OMIT 0
11691#define SQLITE_CHANGESET_REPLACE 1
11692#define SQLITE_CHANGESET_ABORT 2
11693
11694/*
11695** CAPI3REF: Rebasing changesets
11696** EXPERIMENTAL
11697**
11698** Suppose there is a site hosting a database in state S0. And that
11699** modifications are made that move that database to state S1 and a
11700** changeset recorded (the "local" changeset). Then, a changeset based
11701** on S0 is received from another site (the "remote" changeset) and
11702** applied to the database. The database is then in state
11703** (S1+"remote"), where the exact state depends on any conflict
11704** resolution decisions (OMIT or REPLACE) made while applying "remote".
11705** Rebasing a changeset is to update it to take those conflict
11706** resolution decisions into account, so that the same conflicts
11707** do not have to be resolved elsewhere in the network.
11708**
11709** For example, if both the local and remote changesets contain an
11710** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
11711**
11712** local: INSERT INTO t1 VALUES(1, 'v1');
11713** remote: INSERT INTO t1 VALUES(1, 'v2');
11714**
11715** and the conflict resolution is REPLACE, then the INSERT change is
11716** removed from the local changeset (it was overridden). Or, if the
11717** conflict resolution was "OMIT", then the local changeset is modified
11718** to instead contain:
11719**
11720** UPDATE t1 SET b = 'v2' WHERE a=1;
11721**
11722** Changes within the local changeset are rebased as follows:
11723**
11724** <dl>
11725** <dt>Local INSERT<dd>
11726** This may only conflict with a remote INSERT. If the conflict
11727** resolution was OMIT, then add an UPDATE change to the rebased
11728** changeset. Or, if the conflict resolution was REPLACE, add
11729** nothing to the rebased changeset.
11730**
11731** <dt>Local DELETE<dd>
11732** This may conflict with a remote UPDATE or DELETE. In both cases the
11733** only possible resolution is OMIT. If the remote operation was a
11734** DELETE, then add no change to the rebased changeset. If the remote
11735** operation was an UPDATE, then the old.* fields of change are updated
11736** to reflect the new.* values in the UPDATE.
11737**
11738** <dt>Local UPDATE<dd>
11739** This may conflict with a remote UPDATE or DELETE. If it conflicts
11740** with a DELETE, and the conflict resolution was OMIT, then the update
11741** is changed into an INSERT. Any undefined values in the new.* record
11742** from the update change are filled in using the old.* values from
11743** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
11744** the UPDATE change is simply omitted from the rebased changeset.
11745**
11746** If conflict is with a remote UPDATE and the resolution is OMIT, then
11747** the old.* values are rebased using the new.* values in the remote
11748** change. Or, if the resolution is REPLACE, then the change is copied
11749** into the rebased changeset with updates to columns also updated by
11750** the conflicting remote UPDATE removed. If this means no columns would
11751** be updated, the change is omitted.
11752** </dl>
11753**
11754** A local change may be rebased against multiple remote changes
11755** simultaneously. If a single key is modified by multiple remote
11756** changesets, they are combined as follows before the local changeset
11757** is rebased:
11758**
11759** <ul>
11760** <li> If there has been one or more REPLACE resolutions on a
11761** key, it is rebased according to a REPLACE.
11762**
11763** <li> If there have been no REPLACE resolutions on a key, then
11764** the local changeset is rebased according to the most recent
11765** of the OMIT resolutions.
11766** </ul>
11767**
11768** Note that conflict resolutions from multiple remote changesets are
11769** combined on a per-field basis, not per-row. This means that in the
11770** case of multiple remote UPDATE operations, some fields of a single
11771** local change may be rebased for REPLACE while others are rebased for
11772** OMIT.
11773**
11774** In order to rebase a local changeset, the remote changeset must first
11775** be applied to the local database using sqlite3changeset_apply_v2() and
11776** the buffer of rebase information captured. Then:
11777**
11778** <ol>
11779** <li> An sqlite3_rebaser object is created by calling
11780** sqlite3rebaser_create().
11781** <li> The new object is configured with the rebase buffer obtained from
11782** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
11783** If the local changeset is to be rebased against multiple remote
11784** changesets, then sqlite3rebaser_configure() should be called
11785** multiple times, in the same order that the multiple
11786** sqlite3changeset_apply_v2() calls were made.
11787** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
11788** <li> The sqlite3_rebaser object is deleted by calling
11789** sqlite3rebaser_delete().
11790** </ol>
11791*/
11792typedef struct sqlite3_rebaser sqlite3_rebaser;
11793
11794/*
11795** CAPI3REF: Create a changeset rebaser object.
11796** EXPERIMENTAL
11797**
11798** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
11799** point to the new object and return SQLITE_OK. Otherwise, if an error
11800** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
11801** to NULL.
11802*/
11803SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
11804
11805/*
11806** CAPI3REF: Configure a changeset rebaser object.
11807** EXPERIMENTAL
11808**
11809** Configure the changeset rebaser object to rebase changesets according
11810** to the conflict resolutions described by buffer pRebase (size nRebase
11811** bytes), which must have been obtained from a previous call to
11812** sqlite3changeset_apply_v2().
11813*/
11814SQLITE_API int sqlite3rebaser_configure(
11815 sqlite3_rebaser*,
11816 int nRebase, const void *pRebase
11817);
11818
11819/*
11820** CAPI3REF: Rebase a changeset
11821** EXPERIMENTAL
11822**
11823** Argument pIn must point to a buffer containing a changeset nIn bytes
11824** in size. This function allocates and populates a buffer with a copy
11825** of the changeset rebased rebased according to the configuration of the
11826** rebaser object passed as the first argument. If successful, (*ppOut)
11827** is set to point to the new buffer containing the rebased changset and
11828** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
11829** responsibility of the caller to eventually free the new buffer using
11830** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
11831** are set to zero and an SQLite error code returned.
11832*/
11833SQLITE_API int sqlite3rebaser_rebase(
11834 sqlite3_rebaser*,
11835 int nIn, const void *pIn,
11836 int *pnOut, void **ppOut
11837);
11838
11839/*
11840** CAPI3REF: Delete a changeset rebaser object.
11841** EXPERIMENTAL
11842**
11843** Delete the changeset rebaser object and all associated resources. There
11844** should be one call to this function for each successful invocation
11845** of sqlite3rebaser_create().
11846*/
11847SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
11848
11849/*
11850** CAPI3REF: Streaming Versions of API functions.
11851**
11852** The six streaming API xxx_strm() functions serve similar purposes to the
11853** corresponding non-streaming API functions:
11854**
11855** <table border=1 style="margin-left:8ex;margin-right:8ex">
11856** <tr><th>Streaming function<th>Non-streaming equivalent</th>
11857** <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
11858** <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
11859** <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
11860** <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
11861** <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
11862** <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
11863** <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
11864** </table>
11865**
11866** Non-streaming functions that accept changesets (or patchsets) as input
11867** require that the entire changeset be stored in a single buffer in memory.
11868** Similarly, those that return a changeset or patchset do so by returning
11869** a pointer to a single large buffer allocated using sqlite3_malloc().
11870** Normally this is convenient. However, if an application running in a
11871** low-memory environment is required to handle very large changesets, the
11872** large contiguous memory allocations required can become onerous.
11873**
11874** In order to avoid this problem, instead of a single large buffer, input
11875** is passed to a streaming API functions by way of a callback function that
11876** the sessions module invokes to incrementally request input data as it is
11877** required. In all cases, a pair of API function parameters such as
11878**
11879** <pre>
11880** &nbsp; int nChangeset,
11881** &nbsp; void *pChangeset,
11882** </pre>
11883**
11884** Is replaced by:
11885**
11886** <pre>
11887** &nbsp; int (*xInput)(void *pIn, void *pData, int *pnData),
11888** &nbsp; void *pIn,
11889** </pre>
11890**
11891** Each time the xInput callback is invoked by the sessions module, the first
11892** argument passed is a copy of the supplied pIn context pointer. The second
11893** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
11894** error occurs the xInput method should copy up to (*pnData) bytes of data
11895** into the buffer and set (*pnData) to the actual number of bytes copied
11896** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
11897** should be set to zero to indicate this. Or, if an error occurs, an SQLite
11898** error code should be returned. In all cases, if an xInput callback returns
11899** an error, all processing is abandoned and the streaming API function
11900** returns a copy of the error code to the caller.
11901**
11902** In the case of sqlite3changeset_start_strm(), the xInput callback may be
11903** invoked by the sessions module at any point during the lifetime of the
11904** iterator. If such an xInput callback returns an error, the iterator enters
11905** an error state, whereby all subsequent calls to iterator functions
11906** immediately fail with the same error code as returned by xInput.
11907**
11908** Similarly, streaming API functions that return changesets (or patchsets)
11909** return them in chunks by way of a callback function instead of via a
11910** pointer to a single large buffer. In this case, a pair of parameters such
11911** as:
11912**
11913** <pre>
11914** &nbsp; int *pnChangeset,
11915** &nbsp; void **ppChangeset,
11916** </pre>
11917**
11918** Is replaced by:
11919**
11920** <pre>
11921** &nbsp; int (*xOutput)(void *pOut, const void *pData, int nData),
11922** &nbsp; void *pOut
11923** </pre>
11924**
11925** The xOutput callback is invoked zero or more times to return data to
11926** the application. The first parameter passed to each call is a copy of the
11927** pOut pointer supplied by the application. The second parameter, pData,
11928** points to a buffer nData bytes in size containing the chunk of output
11929** data being returned. If the xOutput callback successfully processes the
11930** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
11931** it should return some other SQLite error code. In this case processing
11932** is immediately abandoned and the streaming API function returns a copy
11933** of the xOutput error code to the application.
11934**
11935** The sessions module never invokes an xOutput callback with the third
11936** parameter set to a value less than or equal to zero. Other than this,
11937** no guarantees are made as to the size of the chunks of data returned.
11938*/
11939SQLITE_API int sqlite3changeset_apply_strm(
11940 sqlite3 *db, /* Apply change to "main" db of this handle */
11941 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
11942 void *pIn, /* First arg for xInput */
11943 int(*xFilter)(
11944 void *pCtx, /* Copy of sixth arg to _apply() */
11945 const char *zTab /* Table name */
11946 ),
11947 int(*xConflict)(
11948 void *pCtx, /* Copy of sixth arg to _apply() */
11949 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11950 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11951 ),
11952 void *pCtx /* First argument passed to xConflict */
11953);
11954SQLITE_API int sqlite3changeset_apply_v2_strm(
11955 sqlite3 *db, /* Apply change to "main" db of this handle */
11956 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
11957 void *pIn, /* First arg for xInput */
11958 int(*xFilter)(
11959 void *pCtx, /* Copy of sixth arg to _apply() */
11960 const char *zTab /* Table name */
11961 ),
11962 int(*xConflict)(
11963 void *pCtx, /* Copy of sixth arg to _apply() */
11964 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11965 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11966 ),
11967 void *pCtx, /* First argument passed to xConflict */
11968 void **ppRebase, int *pnRebase,
11969 int flags
11970);
11971SQLITE_API int sqlite3changeset_concat_strm(
11972 int (*xInputA)(void *pIn, void *pData, int *pnData),
11973 void *pInA,
11974 int (*xInputB)(void *pIn, void *pData, int *pnData),
11975 void *pInB,
11976 int (*xOutput)(void *pOut, const void *pData, int nData),
11977 void *pOut
11978);
11979SQLITE_API int sqlite3changeset_invert_strm(
11980 int (*xInput)(void *pIn, void *pData, int *pnData),
11981 void *pIn,
11982 int (*xOutput)(void *pOut, const void *pData, int nData),
11983 void *pOut
11984);
11985SQLITE_API int sqlite3changeset_start_strm(
11986 sqlite3_changeset_iter **pp,
11987 int (*xInput)(void *pIn, void *pData, int *pnData),
11988 void *pIn
11989);
11990SQLITE_API int sqlite3session_changeset_strm(
11991 sqlite3_session *pSession,
11992 int (*xOutput)(void *pOut, const void *pData, int nData),
11993 void *pOut
11994);
11995SQLITE_API int sqlite3session_patchset_strm(
11996 sqlite3_session *pSession,
11997 int (*xOutput)(void *pOut, const void *pData, int nData),
11998 void *pOut
11999);
12000SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*,
12001 int (*xInput)(void *pIn, void *pData, int *pnData),
12002 void *pIn
12003);
12004SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
12005 int (*xOutput)(void *pOut, const void *pData, int nData),
12006 void *pOut
12007);
12008SQLITE_API int sqlite3rebaser_rebase_strm(
12009 sqlite3_rebaser *pRebaser,
12010 int (*xInput)(void *pIn, void *pData, int *pnData),
12011 void *pIn,
12012 int (*xOutput)(void *pOut, const void *pData, int nData),
12013 void *pOut
12014);
12015
12016
12017/*
12018** Make sure we can call this stuff from C++.
12019*/
12020#if 0
12021}
12022#endif
12023
12024#endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
12025
12026/******** End of sqlite3session.h *********/
12027/******** Begin file fts5.h *********/
12028/*
12029** 2014 May 31
12030**
12031** The author disclaims copyright to this source code. In place of
12032** a legal notice, here is a blessing:
12033**
12034** May you do good and not evil.
12035** May you find forgiveness for yourself and forgive others.
12036** May you share freely, never taking more than you give.
12037**
12038******************************************************************************
12039**
12040** Interfaces to extend FTS5. Using the interfaces defined in this file,
12041** FTS5 may be extended with:
12042**
12043** * custom tokenizers, and
12044** * custom auxiliary functions.
12045*/
12046
12047
12048#ifndef _FTS5_H
12049#define _FTS5_H
12050
12051
12052#if 0
12053extern "C" {
12054#endif
12055
12056/*************************************************************************
12057** CUSTOM AUXILIARY FUNCTIONS
12058**
12059** Virtual table implementations may overload SQL functions by implementing
12060** the sqlite3_module.xFindFunction() method.
12061*/
12062
12063typedef struct Fts5ExtensionApi Fts5ExtensionApi;
12064typedef struct Fts5Context Fts5Context;
12065typedef struct Fts5PhraseIter Fts5PhraseIter;
12066
12067typedef void (*fts5_extension_function)(
12068 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
12069 Fts5Context *pFts, /* First arg to pass to pApi functions */
12070 sqlite3_context *pCtx, /* Context for returning result/error */
12071 int nVal, /* Number of values in apVal[] array */
12072 sqlite3_value **apVal /* Array of trailing arguments */
12073);
12074
12075struct Fts5PhraseIter {
12076 const unsigned char *a;
12077 const unsigned char *b;
12078};
12079
12080/*
12081** EXTENSION API FUNCTIONS
12082**
12083** xUserData(pFts):
12084** Return a copy of the context pointer the extension function was
12085** registered with.
12086**
12087** xColumnTotalSize(pFts, iCol, pnToken):
12088** If parameter iCol is less than zero, set output variable *pnToken
12089** to the total number of tokens in the FTS5 table. Or, if iCol is
12090** non-negative but less than the number of columns in the table, return
12091** the total number of tokens in column iCol, considering all rows in
12092** the FTS5 table.
12093**
12094** If parameter iCol is greater than or equal to the number of columns
12095** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12096** an OOM condition or IO error), an appropriate SQLite error code is
12097** returned.
12098**
12099** xColumnCount(pFts):
12100** Return the number of columns in the table.
12101**
12102** xColumnSize(pFts, iCol, pnToken):
12103** If parameter iCol is less than zero, set output variable *pnToken
12104** to the total number of tokens in the current row. Or, if iCol is
12105** non-negative but less than the number of columns in the table, set
12106** *pnToken to the number of tokens in column iCol of the current row.
12107**
12108** If parameter iCol is greater than or equal to the number of columns
12109** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12110** an OOM condition or IO error), an appropriate SQLite error code is
12111** returned.
12112**
12113** This function may be quite inefficient if used with an FTS5 table
12114** created with the "columnsize=0" option.
12115**
12116** xColumnText:
12117** This function attempts to retrieve the text of column iCol of the
12118** current document. If successful, (*pz) is set to point to a buffer
12119** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12120** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
12121** if an error occurs, an SQLite error code is returned and the final values
12122** of (*pz) and (*pn) are undefined.
12123**
12124** xPhraseCount:
12125** Returns the number of phrases in the current query expression.
12126**
12127** xPhraseSize:
12128** Returns the number of tokens in phrase iPhrase of the query. Phrases
12129** are numbered starting from zero.
12130**
12131** xInstCount:
12132** Set *pnInst to the total number of occurrences of all phrases within
12133** the query within the current row. Return SQLITE_OK if successful, or
12134** an error code (i.e. SQLITE_NOMEM) if an error occurs.
12135**
12136** This API can be quite slow if used with an FTS5 table created with the
12137** "detail=none" or "detail=column" option. If the FTS5 table is created
12138** with either "detail=none" or "detail=column" and "content=" option
12139** (i.e. if it is a contentless table), then this API always returns 0.
12140**
12141** xInst:
12142** Query for the details of phrase match iIdx within the current row.
12143** Phrase matches are numbered starting from zero, so the iIdx argument
12144** should be greater than or equal to zero and smaller than the value
12145** output by xInstCount().
12146**
12147** Usually, output parameter *piPhrase is set to the phrase number, *piCol
12148** to the column in which it occurs and *piOff the token offset of the
12149** first token of the phrase. The exception is if the table was created
12150** with the offsets=0 option specified. In this case *piOff is always
12151** set to -1.
12152**
12153** Returns SQLITE_OK if successful, or an error code (i.e. SQLITE_NOMEM)
12154** if an error occurs.
12155**
12156** This API can be quite slow if used with an FTS5 table created with the
12157** "detail=none" or "detail=column" option.
12158**
12159** xRowid:
12160** Returns the rowid of the current row.
12161**
12162** xTokenize:
12163** Tokenize text using the tokenizer belonging to the FTS5 table.
12164**
12165** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
12166** This API function is used to query the FTS table for phrase iPhrase
12167** of the current query. Specifically, a query equivalent to:
12168**
12169** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
12170**
12171** with $p set to a phrase equivalent to the phrase iPhrase of the
12172** current query is executed. Any column filter that applies to
12173** phrase iPhrase of the current query is included in $p. For each
12174** row visited, the callback function passed as the fourth argument
12175** is invoked. The context and API objects passed to the callback
12176** function may be used to access the properties of each matched row.
12177** Invoking Api.xUserData() returns a copy of the pointer passed as
12178** the third argument to pUserData.
12179**
12180** If the callback function returns any value other than SQLITE_OK, the
12181** query is abandoned and the xQueryPhrase function returns immediately.
12182** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
12183** Otherwise, the error code is propagated upwards.
12184**
12185** If the query runs to completion without incident, SQLITE_OK is returned.
12186** Or, if some error occurs before the query completes or is aborted by
12187** the callback, an SQLite error code is returned.
12188**
12189**
12190** xSetAuxdata(pFts5, pAux, xDelete)
12191**
12192** Save the pointer passed as the second argument as the extension functions
12193** "auxiliary data". The pointer may then be retrieved by the current or any
12194** future invocation of the same fts5 extension function made as part of
12195** of the same MATCH query using the xGetAuxdata() API.
12196**
12197** Each extension function is allocated a single auxiliary data slot for
12198** each FTS query (MATCH expression). If the extension function is invoked
12199** more than once for a single FTS query, then all invocations share a
12200** single auxiliary data context.
12201**
12202** If there is already an auxiliary data pointer when this function is
12203** invoked, then it is replaced by the new pointer. If an xDelete callback
12204** was specified along with the original pointer, it is invoked at this
12205** point.
12206**
12207** The xDelete callback, if one is specified, is also invoked on the
12208** auxiliary data pointer after the FTS5 query has finished.
12209**
12210** If an error (e.g. an OOM condition) occurs within this function, an
12211** the auxiliary data is set to NULL and an error code returned. If the
12212** xDelete parameter was not NULL, it is invoked on the auxiliary data
12213** pointer before returning.
12214**
12215**
12216** xGetAuxdata(pFts5, bClear)
12217**
12218** Returns the current auxiliary data pointer for the fts5 extension
12219** function. See the xSetAuxdata() method for details.
12220**
12221** If the bClear argument is non-zero, then the auxiliary data is cleared
12222** (set to NULL) before this function returns. In this case the xDelete,
12223** if any, is not invoked.
12224**
12225**
12226** xRowCount(pFts5, pnRow)
12227**
12228** This function is used to retrieve the total number of rows in the table.
12229** In other words, the same value that would be returned by:
12230**
12231** SELECT count(*) FROM ftstable;
12232**
12233** xPhraseFirst()
12234** This function is used, along with type Fts5PhraseIter and the xPhraseNext
12235** method, to iterate through all instances of a single query phrase within
12236** the current row. This is the same information as is accessible via the
12237** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
12238** to use, this API may be faster under some circumstances. To iterate
12239** through instances of phrase iPhrase, use the following code:
12240**
12241** Fts5PhraseIter iter;
12242** int iCol, iOff;
12243** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
12244** iCol>=0;
12245** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
12246** ){
12247** // An instance of phrase iPhrase at offset iOff of column iCol
12248** }
12249**
12250** The Fts5PhraseIter structure is defined above. Applications should not
12251** modify this structure directly - it should only be used as shown above
12252** with the xPhraseFirst() and xPhraseNext() API methods (and by
12253** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
12254**
12255** This API can be quite slow if used with an FTS5 table created with the
12256** "detail=none" or "detail=column" option. If the FTS5 table is created
12257** with either "detail=none" or "detail=column" and "content=" option
12258** (i.e. if it is a contentless table), then this API always iterates
12259** through an empty set (all calls to xPhraseFirst() set iCol to -1).
12260**
12261** xPhraseNext()
12262** See xPhraseFirst above.
12263**
12264** xPhraseFirstColumn()
12265** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
12266** and xPhraseNext() APIs described above. The difference is that instead
12267** of iterating through all instances of a phrase in the current row, these
12268** APIs are used to iterate through the set of columns in the current row
12269** that contain one or more instances of a specified phrase. For example:
12270**
12271** Fts5PhraseIter iter;
12272** int iCol;
12273** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
12274** iCol>=0;
12275** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
12276** ){
12277** // Column iCol contains at least one instance of phrase iPhrase
12278** }
12279**
12280** This API can be quite slow if used with an FTS5 table created with the
12281** "detail=none" option. If the FTS5 table is created with either
12282** "detail=none" "content=" option (i.e. if it is a contentless table),
12283** then this API always iterates through an empty set (all calls to
12284** xPhraseFirstColumn() set iCol to -1).
12285**
12286** The information accessed using this API and its companion
12287** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
12288** (or xInst/xInstCount). The chief advantage of this API is that it is
12289** significantly more efficient than those alternatives when used with
12290** "detail=column" tables.
12291**
12292** xPhraseNextColumn()
12293** See xPhraseFirstColumn above.
12294*/
12295struct Fts5ExtensionApi {
12296 int iVersion; /* Currently always set to 3 */
12297
12298 void *(*xUserData)(Fts5Context*);
12299
12300 int (*xColumnCount)(Fts5Context*);
12301 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
12302 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
12303
12304 int (*xTokenize)(Fts5Context*,
12305 const char *pText, int nText, /* Text to tokenize */
12306 void *pCtx, /* Context passed to xToken() */
12307 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
12308 );
12309
12310 int (*xPhraseCount)(Fts5Context*);
12311 int (*xPhraseSize)(Fts5Context*, int iPhrase);
12312
12313 int (*xInstCount)(Fts5Context*, int *pnInst);
12314 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
12315
12316 sqlite3_int64 (*xRowid)(Fts5Context*);
12317 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
12318 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
12319
12320 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
12321 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
12322 );
12323 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
12324 void *(*xGetAuxdata)(Fts5Context*, int bClear);
12325
12326 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
12327 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
12328
12329 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
12330 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
12331};
12332
12333/*
12334** CUSTOM AUXILIARY FUNCTIONS
12335*************************************************************************/
12336
12337/*************************************************************************
12338** CUSTOM TOKENIZERS
12339**
12340** Applications may also register custom tokenizer types. A tokenizer
12341** is registered by providing fts5 with a populated instance of the
12342** following structure. All structure methods must be defined, setting
12343** any member of the fts5_tokenizer struct to NULL leads to undefined
12344** behaviour. The structure methods are expected to function as follows:
12345**
12346** xCreate:
12347** This function is used to allocate and initialize a tokenizer instance.
12348** A tokenizer instance is required to actually tokenize text.
12349**
12350** The first argument passed to this function is a copy of the (void*)
12351** pointer provided by the application when the fts5_tokenizer object
12352** was registered with FTS5 (the third argument to xCreateTokenizer()).
12353** The second and third arguments are an array of nul-terminated strings
12354** containing the tokenizer arguments, if any, specified following the
12355** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
12356** to create the FTS5 table.
12357**
12358** The final argument is an output variable. If successful, (*ppOut)
12359** should be set to point to the new tokenizer handle and SQLITE_OK
12360** returned. If an error occurs, some value other than SQLITE_OK should
12361** be returned. In this case, fts5 assumes that the final value of *ppOut
12362** is undefined.
12363**
12364** xDelete:
12365** This function is invoked to delete a tokenizer handle previously
12366** allocated using xCreate(). Fts5 guarantees that this function will
12367** be invoked exactly once for each successful call to xCreate().
12368**
12369** xTokenize:
12370** This function is expected to tokenize the nText byte string indicated
12371** by argument pText. pText may or may not be nul-terminated. The first
12372** argument passed to this function is a pointer to an Fts5Tokenizer object
12373** returned by an earlier call to xCreate().
12374**
12375** The second argument indicates the reason that FTS5 is requesting
12376** tokenization of the supplied text. This is always one of the following
12377** four values:
12378**
12379** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
12380** or removed from the FTS table. The tokenizer is being invoked to
12381** determine the set of tokens to add to (or delete from) the
12382** FTS index.
12383**
12384** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
12385** against the FTS index. The tokenizer is being called to tokenize
12386** a bareword or quoted string specified as part of the query.
12387**
12388** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
12389** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
12390** followed by a "*" character, indicating that the last token
12391** returned by the tokenizer will be treated as a token prefix.
12392**
12393** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
12394** satisfy an fts5_api.xTokenize() request made by an auxiliary
12395** function. Or an fts5_api.xColumnSize() request made by the same
12396** on a columnsize=0 database.
12397** </ul>
12398**
12399** For each token in the input string, the supplied callback xToken() must
12400** be invoked. The first argument to it should be a copy of the pointer
12401** passed as the second argument to xTokenize(). The third and fourth
12402** arguments are a pointer to a buffer containing the token text, and the
12403** size of the token in bytes. The 4th and 5th arguments are the byte offsets
12404** of the first byte of and first byte immediately following the text from
12405** which the token is derived within the input.
12406**
12407** The second argument passed to the xToken() callback ("tflags") should
12408** normally be set to 0. The exception is if the tokenizer supports
12409** synonyms. In this case see the discussion below for details.
12410**
12411** FTS5 assumes the xToken() callback is invoked for each token in the
12412** order that they occur within the input text.
12413**
12414** If an xToken() callback returns any value other than SQLITE_OK, then
12415** the tokenization should be abandoned and the xTokenize() method should
12416** immediately return a copy of the xToken() return value. Or, if the
12417** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
12418** if an error occurs with the xTokenize() implementation itself, it
12419** may abandon the tokenization and return any error code other than
12420** SQLITE_OK or SQLITE_DONE.
12421**
12422** SYNONYM SUPPORT
12423**
12424** Custom tokenizers may also support synonyms. Consider a case in which a
12425** user wishes to query for a phrase such as "first place". Using the
12426** built-in tokenizers, the FTS5 query 'first + place' will match instances
12427** of "first place" within the document set, but not alternative forms
12428** such as "1st place". In some applications, it would be better to match
12429** all instances of "first place" or "1st place" regardless of which form
12430** the user specified in the MATCH query text.
12431**
12432** There are several ways to approach this in FTS5:
12433**
12434** <ol><li> By mapping all synonyms to a single token. In this case, the
12435** In the above example, this means that the tokenizer returns the
12436** same token for inputs "first" and "1st". Say that token is in
12437** fact "first", so that when the user inserts the document "I won
12438** 1st place" entries are added to the index for tokens "i", "won",
12439** "first" and "place". If the user then queries for '1st + place',
12440** the tokenizer substitutes "first" for "1st" and the query works
12441** as expected.
12442**
12443** <li> By adding multiple synonyms for a single term to the FTS index.
12444** In this case, when tokenizing query text, the tokenizer may
12445** provide multiple synonyms for a single term within the document.
12446** FTS5 then queries the index for each synonym individually. For
12447** example, faced with the query:
12448**
12449** <codeblock>
12450** ... MATCH 'first place'</codeblock>
12451**
12452** the tokenizer offers both "1st" and "first" as synonyms for the
12453** first token in the MATCH query and FTS5 effectively runs a query
12454** similar to:
12455**
12456** <codeblock>
12457** ... MATCH '(first OR 1st) place'</codeblock>
12458**
12459** except that, for the purposes of auxiliary functions, the query
12460** still appears to contain just two phrases - "(first OR 1st)"
12461** being treated as a single phrase.
12462**
12463** <li> By adding multiple synonyms for a single term to the FTS index.
12464** Using this method, when tokenizing document text, the tokenizer
12465** provides multiple synonyms for each token. So that when a
12466** document such as "I won first place" is tokenized, entries are
12467** added to the FTS index for "i", "won", "first", "1st" and
12468** "place".
12469**
12470** This way, even if the tokenizer does not provide synonyms
12471** when tokenizing query text (it should not - to do would be
12472** inefficient), it doesn't matter if the user queries for
12473** 'first + place' or '1st + place', as there are entries in the
12474** FTS index corresponding to both forms of the first token.
12475** </ol>
12476**
12477** Whether it is parsing document or query text, any call to xToken that
12478** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
12479** is considered to supply a synonym for the previous token. For example,
12480** when parsing the document "I won first place", a tokenizer that supports
12481** synonyms would call xToken() 5 times, as follows:
12482**
12483** <codeblock>
12484** xToken(pCtx, 0, "i", 1, 0, 1);
12485** xToken(pCtx, 0, "won", 3, 2, 5);
12486** xToken(pCtx, 0, "first", 5, 6, 11);
12487** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
12488** xToken(pCtx, 0, "place", 5, 12, 17);
12489**</codeblock>
12490**
12491** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
12492** xToken() is called. Multiple synonyms may be specified for a single token
12493** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
12494** There is no limit to the number of synonyms that may be provided for a
12495** single token.
12496**
12497** In many cases, method (1) above is the best approach. It does not add
12498** extra data to the FTS index or require FTS5 to query for multiple terms,
12499** so it is efficient in terms of disk space and query speed. However, it
12500** does not support prefix queries very well. If, as suggested above, the
12501** token "first" is substituted for "1st" by the tokenizer, then the query:
12502**
12503** <codeblock>
12504** ... MATCH '1s*'</codeblock>
12505**
12506** will not match documents that contain the token "1st" (as the tokenizer
12507** will probably not map "1s" to any prefix of "first").
12508**
12509** For full prefix support, method (3) may be preferred. In this case,
12510** because the index contains entries for both "first" and "1st", prefix
12511** queries such as 'fi*' or '1s*' will match correctly. However, because
12512** extra entries are added to the FTS index, this method uses more space
12513** within the database.
12514**
12515** Method (2) offers a midpoint between (1) and (3). Using this method,
12516** a query such as '1s*' will match documents that contain the literal
12517** token "1st", but not "first" (assuming the tokenizer is not able to
12518** provide synonyms for prefixes). However, a non-prefix query like '1st'
12519** will match against "1st" and "first". This method does not require
12520** extra disk space, as no extra entries are added to the FTS index.
12521** On the other hand, it may require more CPU cycles to run MATCH queries,
12522** as separate queries of the FTS index are required for each synonym.
12523**
12524** When using methods (2) or (3), it is important that the tokenizer only
12525** provide synonyms when tokenizing document text (method (2)) or query
12526** text (method (3)), not both. Doing so will not cause any errors, but is
12527** inefficient.
12528*/
12529typedef struct Fts5Tokenizer Fts5Tokenizer;
12530typedef struct fts5_tokenizer fts5_tokenizer;
12531struct fts5_tokenizer {
12532 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
12533 void (*xDelete)(Fts5Tokenizer*);
12534 int (*xTokenize)(Fts5Tokenizer*,
12535 void *pCtx,
12536 int flags, /* Mask of FTS5_TOKENIZE_* flags */
12537 const char *pText, int nText,
12538 int (*xToken)(
12539 void *pCtx, /* Copy of 2nd argument to xTokenize() */
12540 int tflags, /* Mask of FTS5_TOKEN_* flags */
12541 const char *pToken, /* Pointer to buffer containing token */
12542 int nToken, /* Size of token in bytes */
12543 int iStart, /* Byte offset of token within input text */
12544 int iEnd /* Byte offset of end of token within input text */
12545 )
12546 );
12547};
12548
12549/* Flags that may be passed as the third argument to xTokenize() */
12550#define FTS5_TOKENIZE_QUERY 0x0001
12551#define FTS5_TOKENIZE_PREFIX 0x0002
12552#define FTS5_TOKENIZE_DOCUMENT 0x0004
12553#define FTS5_TOKENIZE_AUX 0x0008
12554
12555/* Flags that may be passed by the tokenizer implementation back to FTS5
12556** as the third argument to the supplied xToken callback. */
12557#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */
12558
12559/*
12560** END OF CUSTOM TOKENIZERS
12561*************************************************************************/
12562
12563/*************************************************************************
12564** FTS5 EXTENSION REGISTRATION API
12565*/
12566typedef struct fts5_api fts5_api;
12567struct fts5_api {
12568 int iVersion; /* Currently always set to 2 */
12569
12570 /* Create a new tokenizer */
12571 int (*xCreateTokenizer)(
12572 fts5_api *pApi,
12573 const char *zName,
12574 void *pContext,
12575 fts5_tokenizer *pTokenizer,
12576 void (*xDestroy)(void*)
12577 );
12578
12579 /* Find an existing tokenizer */
12580 int (*xFindTokenizer)(
12581 fts5_api *pApi,
12582 const char *zName,
12583 void **ppContext,
12584 fts5_tokenizer *pTokenizer
12585 );
12586
12587 /* Create a new auxiliary function */
12588 int (*xCreateFunction)(
12589 fts5_api *pApi,
12590 const char *zName,
12591 void *pContext,
12592 fts5_extension_function xFunction,
12593 void (*xDestroy)(void*)
12594 );
12595};
12596
12597/*
12598** END OF REGISTRATION API
12599*************************************************************************/
12600
12601#if 0
12602} /* end of the 'extern "C"' block */
12603#endif
12604
12605#endif /* _FTS5_H */
12606
12607/******** End of fts5.h *********/
12608
12609/************** End of sqlite3.h *********************************************/
12610/************** Continuing where we left off in sqliteInt.h ******************/
12611
12612/*
12613** Include the configuration header output by 'configure' if we're using the
12614** autoconf-based build
12615*/
12616#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
12617/* #include "config.h" */
12618#define SQLITECONFIG_H 1
12619#endif
12620
12621/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
12622/************** Begin file sqliteLimit.h *************************************/
12623/*
12624** 2007 May 7
12625**
12626** The author disclaims copyright to this source code. In place of
12627** a legal notice, here is a blessing:
12628**
12629** May you do good and not evil.
12630** May you find forgiveness for yourself and forgive others.
12631** May you share freely, never taking more than you give.
12632**
12633*************************************************************************
12634**
12635** This file defines various limits of what SQLite can process.
12636*/
12637
12638/*
12639** The maximum length of a TEXT or BLOB in bytes. This also
12640** limits the size of a row in a table or index.
12641**
12642** The hard limit is the ability of a 32-bit signed integer
12643** to count the size: 2^31-1 or 2147483647.
12644*/
12645#ifndef SQLITE_MAX_LENGTH
12646# define SQLITE_MAX_LENGTH 1000000000
12647#endif
12648
12649/*
12650** This is the maximum number of
12651**
12652** * Columns in a table
12653** * Columns in an index
12654** * Columns in a view
12655** * Terms in the SET clause of an UPDATE statement
12656** * Terms in the result set of a SELECT statement
12657** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
12658** * Terms in the VALUES clause of an INSERT statement
12659**
12660** The hard upper limit here is 32676. Most database people will
12661** tell you that in a well-normalized database, you usually should
12662** not have more than a dozen or so columns in any table. And if
12663** that is the case, there is no point in having more than a few
12664** dozen values in any of the other situations described above.
12665*/
12666#ifndef SQLITE_MAX_COLUMN
12667# define SQLITE_MAX_COLUMN 2000
12668#endif
12669
12670/*
12671** The maximum length of a single SQL statement in bytes.
12672**
12673** It used to be the case that setting this value to zero would
12674** turn the limit off. That is no longer true. It is not possible
12675** to turn this limit off.
12676*/
12677#ifndef SQLITE_MAX_SQL_LENGTH
12678# define SQLITE_MAX_SQL_LENGTH 1000000000
12679#endif
12680
12681/*
12682** The maximum depth of an expression tree. This is limited to
12683** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
12684** want to place more severe limits on the complexity of an
12685** expression.
12686**
12687** A value of 0 used to mean that the limit was not enforced.
12688** But that is no longer true. The limit is now strictly enforced
12689** at all times.
12690*/
12691#ifndef SQLITE_MAX_EXPR_DEPTH
12692# define SQLITE_MAX_EXPR_DEPTH 1000
12693#endif
12694
12695/*
12696** The maximum number of terms in a compound SELECT statement.
12697** The code generator for compound SELECT statements does one
12698** level of recursion for each term. A stack overflow can result
12699** if the number of terms is too large. In practice, most SQL
12700** never has more than 3 or 4 terms. Use a value of 0 to disable
12701** any limit on the number of terms in a compount SELECT.
12702*/
12703#ifndef SQLITE_MAX_COMPOUND_SELECT
12704# define SQLITE_MAX_COMPOUND_SELECT 500
12705#endif
12706
12707/*
12708** The maximum number of opcodes in a VDBE program.
12709** Not currently enforced.
12710*/
12711#ifndef SQLITE_MAX_VDBE_OP
12712# define SQLITE_MAX_VDBE_OP 250000000
12713#endif
12714
12715/*
12716** The maximum number of arguments to an SQL function.
12717*/
12718#ifndef SQLITE_MAX_FUNCTION_ARG
12719# define SQLITE_MAX_FUNCTION_ARG 127
12720#endif
12721
12722/*
12723** The suggested maximum number of in-memory pages to use for
12724** the main database table and for temporary tables.
12725**
12726** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
12727** which means the cache size is limited to 2048000 bytes of memory.
12728** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
12729** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
12730*/
12731#ifndef SQLITE_DEFAULT_CACHE_SIZE
12732# define SQLITE_DEFAULT_CACHE_SIZE -2000
12733#endif
12734
12735/*
12736** The default number of frames to accumulate in the log file before
12737** checkpointing the database in WAL mode.
12738*/
12739#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
12740# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT 1000
12741#endif
12742
12743/*
12744** The maximum number of attached databases. This must be between 0
12745** and 125. The upper bound of 125 is because the attached databases are
12746** counted using a signed 8-bit integer which has a maximum value of 127
12747** and we have to allow 2 extra counts for the "main" and "temp" databases.
12748*/
12749#ifndef SQLITE_MAX_ATTACHED
12750# define SQLITE_MAX_ATTACHED 10
12751#endif
12752
12753
12754/*
12755** The maximum value of a ?nnn wildcard that the parser will accept.
12756*/
12757#ifndef SQLITE_MAX_VARIABLE_NUMBER
12758# define SQLITE_MAX_VARIABLE_NUMBER 999
12759#endif
12760
12761/* Maximum page size. The upper bound on this value is 65536. This a limit
12762** imposed by the use of 16-bit offsets within each page.
12763**
12764** Earlier versions of SQLite allowed the user to change this value at
12765** compile time. This is no longer permitted, on the grounds that it creates
12766** a library that is technically incompatible with an SQLite library
12767** compiled with a different limit. If a process operating on a database
12768** with a page-size of 65536 bytes crashes, then an instance of SQLite
12769** compiled with the default page-size limit will not be able to rollback
12770** the aborted transaction. This could lead to database corruption.
12771*/
12772#ifdef SQLITE_MAX_PAGE_SIZE
12773# undef SQLITE_MAX_PAGE_SIZE
12774#endif
12775#define SQLITE_MAX_PAGE_SIZE 65536
12776
12777
12778/*
12779** The default size of a database page.
12780*/
12781#ifndef SQLITE_DEFAULT_PAGE_SIZE
12782# define SQLITE_DEFAULT_PAGE_SIZE 4096
12783#endif
12784#if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
12785# undef SQLITE_DEFAULT_PAGE_SIZE
12786# define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
12787#endif
12788
12789/*
12790** Ordinarily, if no value is explicitly provided, SQLite creates databases
12791** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
12792** device characteristics (sector-size and atomic write() support),
12793** SQLite may choose a larger value. This constant is the maximum value
12794** SQLite will choose on its own.
12795*/
12796#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
12797# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
12798#endif
12799#if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
12800# undef SQLITE_MAX_DEFAULT_PAGE_SIZE
12801# define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
12802#endif
12803
12804
12805/*
12806** Maximum number of pages in one database file.
12807**
12808** This is really just the default value for the max_page_count pragma.
12809** This value can be lowered (or raised) at run-time using that the
12810** max_page_count macro.
12811*/
12812#ifndef SQLITE_MAX_PAGE_COUNT
12813# define SQLITE_MAX_PAGE_COUNT 1073741823
12814#endif
12815
12816/*
12817** Maximum length (in bytes) of the pattern in a LIKE or GLOB
12818** operator.
12819*/
12820#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
12821# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
12822#endif
12823
12824/*
12825** Maximum depth of recursion for triggers.
12826**
12827** A value of 1 means that a trigger program will not be able to itself
12828** fire any triggers. A value of 0 means that no trigger programs at all
12829** may be executed.
12830*/
12831#ifndef SQLITE_MAX_TRIGGER_DEPTH
12832# define SQLITE_MAX_TRIGGER_DEPTH 1000
12833#endif
12834
12835/************** End of sqliteLimit.h *****************************************/
12836/************** Continuing where we left off in sqliteInt.h ******************/
12837
12838/* Disable nuisance warnings on Borland compilers */
12839#if defined(__BORLANDC__)
12840#pragma warn -rch /* unreachable code */
12841#pragma warn -ccc /* Condition is always true or false */
12842#pragma warn -aus /* Assigned value is never used */
12843#pragma warn -csu /* Comparing signed and unsigned */
12844#pragma warn -spa /* Suspicious pointer arithmetic */
12845#endif
12846
12847/*
12848** Include standard header files as necessary
12849*/
12850#ifdef HAVE_STDINT_H
12851#include <stdint.h>
12852#endif
12853#ifdef HAVE_INTTYPES_H
12854#include <inttypes.h>
12855#endif
12856
12857/*
12858** The following macros are used to cast pointers to integers and
12859** integers to pointers. The way you do this varies from one compiler
12860** to the next, so we have developed the following set of #if statements
12861** to generate appropriate macros for a wide range of compilers.
12862**
12863** The correct "ANSI" way to do this is to use the intptr_t type.
12864** Unfortunately, that typedef is not available on all compilers, or
12865** if it is available, it requires an #include of specific headers
12866** that vary from one machine to the next.
12867**
12868** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
12869** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
12870** So we have to define the macros in different ways depending on the
12871** compiler.
12872*/
12873#if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
12874# define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
12875# define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
12876#elif !defined(__GNUC__) /* Works for compilers other than LLVM */
12877# define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X])
12878# define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
12879#elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
12880# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))
12881# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))
12882#else /* Generates a warning - but it always works */
12883# define SQLITE_INT_TO_PTR(X) ((void*)(X))
12884# define SQLITE_PTR_TO_INT(X) ((int)(X))
12885#endif
12886
12887/*
12888** A macro to hint to the compiler that a function should not be
12889** inlined.
12890*/
12891#if defined(__GNUC__)
12892# define SQLITE_NOINLINE __attribute__((noinline))
12893#elif defined(_MSC_VER) && _MSC_VER>=1310
12894# define SQLITE_NOINLINE __declspec(noinline)
12895#else
12896# define SQLITE_NOINLINE
12897#endif
12898
12899/*
12900** Make sure that the compiler intrinsics we desire are enabled when
12901** compiling with an appropriate version of MSVC unless prevented by
12902** the SQLITE_DISABLE_INTRINSIC define.
12903*/
12904#if !defined(SQLITE_DISABLE_INTRINSIC)
12905# if defined(_MSC_VER) && _MSC_VER>=1400
12906# if !defined(_WIN32_WCE)
12907# include <intrin.h>
12908# pragma intrinsic(_byteswap_ushort)
12909# pragma intrinsic(_byteswap_ulong)
12910# pragma intrinsic(_byteswap_uint64)
12911# pragma intrinsic(_ReadWriteBarrier)
12912# else
12913# include <cmnintrin.h>
12914# endif
12915# endif
12916#endif
12917
12918/*
12919** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
12920** 0 means mutexes are permanently disable and the library is never
12921** threadsafe. 1 means the library is serialized which is the highest
12922** level of threadsafety. 2 means the library is multithreaded - multiple
12923** threads can use SQLite as long as no two threads try to use the same
12924** database connection at the same time.
12925**
12926** Older versions of SQLite used an optional THREADSAFE macro.
12927** We support that for legacy.
12928**
12929** To ensure that the correct value of "THREADSAFE" is reported when querying
12930** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
12931** logic is partially replicated in ctime.c. If it is updated here, it should
12932** also be updated there.
12933*/
12934#if !defined(SQLITE_THREADSAFE)
12935# if defined(THREADSAFE)
12936# define SQLITE_THREADSAFE THREADSAFE
12937# else
12938# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
12939# endif
12940#endif
12941
12942/*
12943** Powersafe overwrite is on by default. But can be turned off using
12944** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
12945*/
12946#ifndef SQLITE_POWERSAFE_OVERWRITE
12947# define SQLITE_POWERSAFE_OVERWRITE 1
12948#endif
12949
12950/*
12951** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
12952** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
12953** which case memory allocation statistics are disabled by default.
12954*/
12955#if !defined(SQLITE_DEFAULT_MEMSTATUS)
12956# define SQLITE_DEFAULT_MEMSTATUS 1
12957#endif
12958
12959/*
12960** Exactly one of the following macros must be defined in order to
12961** specify which memory allocation subsystem to use.
12962**
12963** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
12964** SQLITE_WIN32_MALLOC // Use Win32 native heap API
12965** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
12966** SQLITE_MEMDEBUG // Debugging version of system malloc()
12967**
12968** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
12969** assert() macro is enabled, each call into the Win32 native heap subsystem
12970** will cause HeapValidate to be called. If heap validation should fail, an
12971** assertion will be triggered.
12972**
12973** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
12974** the default.
12975*/
12976#if defined(SQLITE_SYSTEM_MALLOC) \
12977 + defined(SQLITE_WIN32_MALLOC) \
12978 + defined(SQLITE_ZERO_MALLOC) \
12979 + defined(SQLITE_MEMDEBUG)>1
12980# error "Two or more of the following compile-time configuration options\
12981 are defined but at most one is allowed:\
12982 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
12983 SQLITE_ZERO_MALLOC"
12984#endif
12985#if defined(SQLITE_SYSTEM_MALLOC) \
12986 + defined(SQLITE_WIN32_MALLOC) \
12987 + defined(SQLITE_ZERO_MALLOC) \
12988 + defined(SQLITE_MEMDEBUG)==0
12989# define SQLITE_SYSTEM_MALLOC 1
12990#endif
12991
12992/*
12993** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
12994** sizes of memory allocations below this value where possible.
12995*/
12996#if !defined(SQLITE_MALLOC_SOFT_LIMIT)
12997# define SQLITE_MALLOC_SOFT_LIMIT 1024
12998#endif
12999
13000/*
13001** We need to define _XOPEN_SOURCE as follows in order to enable
13002** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
13003** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
13004** it.
13005*/
13006#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
13007# define _XOPEN_SOURCE 600
13008#endif
13009
13010/*
13011** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that
13012** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,
13013** make it true by defining or undefining NDEBUG.
13014**
13015** Setting NDEBUG makes the code smaller and faster by disabling the
13016** assert() statements in the code. So we want the default action
13017** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
13018** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
13019** feature.
13020*/
13021#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
13022# define NDEBUG 1
13023#endif
13024#if defined(NDEBUG) && defined(SQLITE_DEBUG)
13025# undef NDEBUG
13026#endif
13027
13028/*
13029** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
13030*/
13031#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
13032# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
13033#endif
13034
13035/*
13036** The testcase() macro is used to aid in coverage testing. When
13037** doing coverage testing, the condition inside the argument to
13038** testcase() must be evaluated both true and false in order to
13039** get full branch coverage. The testcase() macro is inserted
13040** to help ensure adequate test coverage in places where simple
13041** condition/decision coverage is inadequate. For example, testcase()
13042** can be used to make sure boundary values are tested. For
13043** bitmask tests, testcase() can be used to make sure each bit
13044** is significant and used at least once. On switch statements
13045** where multiple cases go to the same block of code, testcase()
13046** can insure that all cases are evaluated.
13047**
13048*/
13049#ifdef SQLITE_COVERAGE_TEST
13050SQLITE_PRIVATE void sqlite3Coverage(int);
13051# define testcase(X) if( X ){ sqlite3Coverage(__LINE__); }
13052#else
13053# define testcase(X)
13054#endif
13055
13056/*
13057** The TESTONLY macro is used to enclose variable declarations or
13058** other bits of code that are needed to support the arguments
13059** within testcase() and assert() macros.
13060*/
13061#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
13062# define TESTONLY(X) X
13063#else
13064# define TESTONLY(X)
13065#endif
13066
13067/*
13068** Sometimes we need a small amount of code such as a variable initialization
13069** to setup for a later assert() statement. We do not want this code to
13070** appear when assert() is disabled. The following macro is therefore
13071** used to contain that setup code. The "VVA" acronym stands for
13072** "Verification, Validation, and Accreditation". In other words, the
13073** code within VVA_ONLY() will only run during verification processes.
13074*/
13075#ifndef NDEBUG
13076# define VVA_ONLY(X) X
13077#else
13078# define VVA_ONLY(X)
13079#endif
13080
13081/*
13082** The ALWAYS and NEVER macros surround boolean expressions which
13083** are intended to always be true or false, respectively. Such
13084** expressions could be omitted from the code completely. But they
13085** are included in a few cases in order to enhance the resilience
13086** of SQLite to unexpected behavior - to make the code "self-healing"
13087** or "ductile" rather than being "brittle" and crashing at the first
13088** hint of unplanned behavior.
13089**
13090** In other words, ALWAYS and NEVER are added for defensive code.
13091**
13092** When doing coverage testing ALWAYS and NEVER are hard-coded to
13093** be true and false so that the unreachable code they specify will
13094** not be counted as untested code.
13095*/
13096#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
13097# define ALWAYS(X) (1)
13098# define NEVER(X) (0)
13099#elif !defined(NDEBUG)
13100# define ALWAYS(X) ((X)?1:(assert(0),0))
13101# define NEVER(X) ((X)?(assert(0),1):0)
13102#else
13103# define ALWAYS(X) (X)
13104# define NEVER(X) (X)
13105#endif
13106
13107/*
13108** Some conditionals are optimizations only. In other words, if the
13109** conditionals are replaced with a constant 1 (true) or 0 (false) then
13110** the correct answer is still obtained, though perhaps not as quickly.
13111**
13112** The following macros mark these optimizations conditionals.
13113*/
13114#if defined(SQLITE_MUTATION_TEST)
13115# define OK_IF_ALWAYS_TRUE(X) (1)
13116# define OK_IF_ALWAYS_FALSE(X) (0)
13117#else
13118# define OK_IF_ALWAYS_TRUE(X) (X)
13119# define OK_IF_ALWAYS_FALSE(X) (X)
13120#endif
13121
13122/*
13123** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
13124** defined. We need to defend against those failures when testing with
13125** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
13126** during a normal build. The following macro can be used to disable tests
13127** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
13128*/
13129#if defined(SQLITE_TEST_REALLOC_STRESS)
13130# define ONLY_IF_REALLOC_STRESS(X) (X)
13131#elif !defined(NDEBUG)
13132# define ONLY_IF_REALLOC_STRESS(X) ((X)?(assert(0),1):0)
13133#else
13134# define ONLY_IF_REALLOC_STRESS(X) (0)
13135#endif
13136
13137/*
13138** Declarations used for tracing the operating system interfaces.
13139*/
13140#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
13141 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
13142 extern int sqlite3OSTrace;
13143# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
13144# define SQLITE_HAVE_OS_TRACE
13145#else
13146# define OSTRACE(X)
13147# undef SQLITE_HAVE_OS_TRACE
13148#endif
13149
13150/*
13151** Is the sqlite3ErrName() function needed in the build? Currently,
13152** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
13153** OSTRACE is enabled), and by several "test*.c" files (which are
13154** compiled using SQLITE_TEST).
13155*/
13156#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
13157 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
13158# define SQLITE_NEED_ERR_NAME
13159#else
13160# undef SQLITE_NEED_ERR_NAME
13161#endif
13162
13163/*
13164** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
13165*/
13166#ifdef SQLITE_OMIT_EXPLAIN
13167# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
13168#endif
13169
13170/*
13171** Return true (non-zero) if the input is an integer that is too large
13172** to fit in 32-bits. This macro is used inside of various testcase()
13173** macros to verify that we have tested SQLite for large-file support.
13174*/
13175#define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0)
13176
13177/*
13178** The macro unlikely() is a hint that surrounds a boolean
13179** expression that is usually false. Macro likely() surrounds
13180** a boolean expression that is usually true. These hints could,
13181** in theory, be used by the compiler to generate better code, but
13182** currently they are just comments for human readers.
13183*/
13184#define likely(X) (X)
13185#define unlikely(X) (X)
13186
13187/************** Include hash.h in the middle of sqliteInt.h ******************/
13188/************** Begin file hash.h ********************************************/
13189/*
13190** 2001 September 22
13191**
13192** The author disclaims copyright to this source code. In place of
13193** a legal notice, here is a blessing:
13194**
13195** May you do good and not evil.
13196** May you find forgiveness for yourself and forgive others.
13197** May you share freely, never taking more than you give.
13198**
13199*************************************************************************
13200** This is the header file for the generic hash-table implementation
13201** used in SQLite.
13202*/
13203#ifndef SQLITE_HASH_H
13204#define SQLITE_HASH_H
13205
13206/* Forward declarations of structures. */
13207typedef struct Hash Hash;
13208typedef struct HashElem HashElem;
13209
13210/* A complete hash table is an instance of the following structure.
13211** The internals of this structure are intended to be opaque -- client
13212** code should not attempt to access or modify the fields of this structure
13213** directly. Change this structure only by using the routines below.
13214** However, some of the "procedures" and "functions" for modifying and
13215** accessing this structure are really macros, so we can't really make
13216** this structure opaque.
13217**
13218** All elements of the hash table are on a single doubly-linked list.
13219** Hash.first points to the head of this list.
13220**
13221** There are Hash.htsize buckets. Each bucket points to a spot in
13222** the global doubly-linked list. The contents of the bucket are the
13223** element pointed to plus the next _ht.count-1 elements in the list.
13224**
13225** Hash.htsize and Hash.ht may be zero. In that case lookup is done
13226** by a linear search of the global list. For small tables, the
13227** Hash.ht table is never allocated because if there are few elements
13228** in the table, it is faster to do a linear search than to manage
13229** the hash table.
13230*/
13231struct Hash {
13232 unsigned int htsize; /* Number of buckets in the hash table */
13233 unsigned int count; /* Number of entries in this table */
13234 HashElem *first; /* The first element of the array */
13235 struct _ht { /* the hash table */
13236 int count; /* Number of entries with this hash */
13237 HashElem *chain; /* Pointer to first entry with this hash */
13238 } *ht;
13239};
13240
13241/* Each element in the hash table is an instance of the following
13242** structure. All elements are stored on a single doubly-linked list.
13243**
13244** Again, this structure is intended to be opaque, but it can't really
13245** be opaque because it is used by macros.
13246*/
13247struct HashElem {
13248 HashElem *next, *prev; /* Next and previous elements in the table */
13249 void *data; /* Data associated with this element */
13250 const char *pKey; /* Key associated with this element */
13251};
13252
13253/*
13254** Access routines. To delete, insert a NULL pointer.
13255*/
13256SQLITE_PRIVATE void sqlite3HashInit(Hash*);
13257SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
13258SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey);
13259SQLITE_PRIVATE void sqlite3HashClear(Hash*);
13260
13261/*
13262** Macros for looping over all elements of a hash table. The idiom is
13263** like this:
13264**
13265** Hash h;
13266** HashElem *p;
13267** ...
13268** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
13269** SomeStructure *pData = sqliteHashData(p);
13270** // do something with pData
13271** }
13272*/
13273#define sqliteHashFirst(H) ((H)->first)
13274#define sqliteHashNext(E) ((E)->next)
13275#define sqliteHashData(E) ((E)->data)
13276/* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
13277/* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
13278
13279/*
13280** Number of entries in a hash table
13281*/
13282/* #define sqliteHashCount(H) ((H)->count) // NOT USED */
13283
13284#endif /* SQLITE_HASH_H */
13285
13286/************** End of hash.h ************************************************/
13287/************** Continuing where we left off in sqliteInt.h ******************/
13288/************** Include parse.h in the middle of sqliteInt.h *****************/
13289/************** Begin file parse.h *******************************************/
13290#define TK_SEMI 1
13291#define TK_EXPLAIN 2
13292#define TK_QUERY 3
13293#define TK_PLAN 4
13294#define TK_BEGIN 5
13295#define TK_TRANSACTION 6
13296#define TK_DEFERRED 7
13297#define TK_IMMEDIATE 8
13298#define TK_EXCLUSIVE 9
13299#define TK_COMMIT 10
13300#define TK_END 11
13301#define TK_ROLLBACK 12
13302#define TK_SAVEPOINT 13
13303#define TK_RELEASE 14
13304#define TK_TO 15
13305#define TK_TABLE 16
13306#define TK_CREATE 17
13307#define TK_IF 18
13308#define TK_NOT 19
13309#define TK_EXISTS 20
13310#define TK_TEMP 21
13311#define TK_LP 22
13312#define TK_RP 23
13313#define TK_AS 24
13314#define TK_WITHOUT 25
13315#define TK_COMMA 26
13316#define TK_ABORT 27
13317#define TK_ACTION 28
13318#define TK_AFTER 29
13319#define TK_ANALYZE 30
13320#define TK_ASC 31
13321#define TK_ATTACH 32
13322#define TK_BEFORE 33
13323#define TK_BY 34
13324#define TK_CASCADE 35
13325#define TK_CAST 36
13326#define TK_CONFLICT 37
13327#define TK_DATABASE 38
13328#define TK_DESC 39
13329#define TK_DETACH 40
13330#define TK_EACH 41
13331#define TK_FAIL 42
13332#define TK_OR 43
13333#define TK_AND 44
13334#define TK_IS 45
13335#define TK_MATCH 46
13336#define TK_LIKE_KW 47
13337#define TK_BETWEEN 48
13338#define TK_IN 49
13339#define TK_ISNULL 50
13340#define TK_NOTNULL 51
13341#define TK_NE 52
13342#define TK_EQ 53
13343#define TK_GT 54
13344#define TK_LE 55
13345#define TK_LT 56
13346#define TK_GE 57
13347#define TK_ESCAPE 58
13348#define TK_ID 59
13349#define TK_COLUMNKW 60
13350#define TK_DO 61
13351#define TK_FOR 62
13352#define TK_IGNORE 63
13353#define TK_INITIALLY 64
13354#define TK_INSTEAD 65
13355#define TK_NO 66
13356#define TK_KEY 67
13357#define TK_OF 68
13358#define TK_OFFSET 69
13359#define TK_PRAGMA 70
13360#define TK_RAISE 71
13361#define TK_RECURSIVE 72
13362#define TK_REPLACE 73
13363#define TK_RESTRICT 74
13364#define TK_ROW 75
13365#define TK_ROWS 76
13366#define TK_TRIGGER 77
13367#define TK_VACUUM 78
13368#define TK_VIEW 79
13369#define TK_VIRTUAL 80
13370#define TK_WITH 81
13371#define TK_CURRENT 82
13372#define TK_FOLLOWING 83
13373#define TK_PARTITION 84
13374#define TK_PRECEDING 85
13375#define TK_RANGE 86
13376#define TK_UNBOUNDED 87
13377#define TK_REINDEX 88
13378#define TK_RENAME 89
13379#define TK_CTIME_KW 90
13380#define TK_ANY 91
13381#define TK_BITAND 92
13382#define TK_BITOR 93
13383#define TK_LSHIFT 94
13384#define TK_RSHIFT 95
13385#define TK_PLUS 96
13386#define TK_MINUS 97
13387#define TK_STAR 98
13388#define TK_SLASH 99
13389#define TK_REM 100
13390#define TK_CONCAT 101
13391#define TK_COLLATE 102
13392#define TK_BITNOT 103
13393#define TK_ON 104
13394#define TK_INDEXED 105
13395#define TK_STRING 106
13396#define TK_JOIN_KW 107
13397#define TK_CONSTRAINT 108
13398#define TK_DEFAULT 109
13399#define TK_NULL 110
13400#define TK_PRIMARY 111
13401#define TK_UNIQUE 112
13402#define TK_CHECK 113
13403#define TK_REFERENCES 114
13404#define TK_AUTOINCR 115
13405#define TK_INSERT 116
13406#define TK_DELETE 117
13407#define TK_UPDATE 118
13408#define TK_SET 119
13409#define TK_DEFERRABLE 120
13410#define TK_FOREIGN 121
13411#define TK_DROP 122
13412#define TK_UNION 123
13413#define TK_ALL 124
13414#define TK_EXCEPT 125
13415#define TK_INTERSECT 126
13416#define TK_SELECT 127
13417#define TK_VALUES 128
13418#define TK_DISTINCT 129
13419#define TK_DOT 130
13420#define TK_FROM 131
13421#define TK_JOIN 132
13422#define TK_USING 133
13423#define TK_ORDER 134
13424#define TK_GROUP 135
13425#define TK_HAVING 136
13426#define TK_LIMIT 137
13427#define TK_WHERE 138
13428#define TK_INTO 139
13429#define TK_NOTHING 140
13430#define TK_FLOAT 141
13431#define TK_BLOB 142
13432#define TK_INTEGER 143
13433#define TK_VARIABLE 144
13434#define TK_CASE 145
13435#define TK_WHEN 146
13436#define TK_THEN 147
13437#define TK_ELSE 148
13438#define TK_INDEX 149
13439#define TK_ALTER 150
13440#define TK_ADD 151
13441#define TK_WINDOW 152
13442#define TK_OVER 153
13443#define TK_FILTER 154
13444#define TK_TRUEFALSE 155
13445#define TK_ISNOT 156
13446#define TK_FUNCTION 157
13447#define TK_COLUMN 158
13448#define TK_AGG_FUNCTION 159
13449#define TK_AGG_COLUMN 160
13450#define TK_UMINUS 161
13451#define TK_UPLUS 162
13452#define TK_TRUTH 163
13453#define TK_REGISTER 164
13454#define TK_VECTOR 165
13455#define TK_SELECT_COLUMN 166
13456#define TK_IF_NULL_ROW 167
13457#define TK_ASTERISK 168
13458#define TK_SPAN 169
13459#define TK_END_OF_FILE 170
13460#define TK_UNCLOSED_STRING 171
13461#define TK_SPACE 172
13462#define TK_ILLEGAL 173
13463
13464/* The token codes above must all fit in 8 bits */
13465#define TKFLG_MASK 0xff
13466
13467/* Flags that can be added to a token code when it is not
13468** being stored in a u8: */
13469#define TKFLG_DONTFOLD 0x100 /* Omit constant folding optimizations */
13470
13471/************** End of parse.h ***********************************************/
13472/************** Continuing where we left off in sqliteInt.h ******************/
13473#include <stdio.h>
13474#include <stdlib.h>
13475#include <string.h>
13476#include <assert.h>
13477#include <stddef.h>
13478
13479/*
13480** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
13481** This allows better measurements of where memcpy() is used when running
13482** cachegrind. But this macro version of memcpy() is very slow so it
13483** should not be used in production. This is a performance measurement
13484** hack only.
13485*/
13486#ifdef SQLITE_INLINE_MEMCPY
13487# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
13488 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
13489#endif
13490
13491/*
13492** If compiling for a processor that lacks floating point support,
13493** substitute integer for floating-point
13494*/
13495#ifdef SQLITE_OMIT_FLOATING_POINT
13496# define double sqlite_int64
13497# define float sqlite_int64
13498# define LONGDOUBLE_TYPE sqlite_int64
13499# ifndef SQLITE_BIG_DBL
13500# define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
13501# endif
13502# define SQLITE_OMIT_DATETIME_FUNCS 1
13503# define SQLITE_OMIT_TRACE 1
13504# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
13505# undef SQLITE_HAVE_ISNAN
13506#endif
13507#ifndef SQLITE_BIG_DBL
13508# define SQLITE_BIG_DBL (1e99)
13509#endif
13510
13511/*
13512** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
13513** afterward. Having this macro allows us to cause the C compiler
13514** to omit code used by TEMP tables without messy #ifndef statements.
13515*/
13516#ifdef SQLITE_OMIT_TEMPDB
13517#define OMIT_TEMPDB 1
13518#else
13519#define OMIT_TEMPDB 0
13520#endif
13521
13522/*
13523** The "file format" number is an integer that is incremented whenever
13524** the VDBE-level file format changes. The following macros define the
13525** the default file format for new databases and the maximum file format
13526** that the library can read.
13527*/
13528#define SQLITE_MAX_FILE_FORMAT 4
13529#ifndef SQLITE_DEFAULT_FILE_FORMAT
13530# define SQLITE_DEFAULT_FILE_FORMAT 4
13531#endif
13532
13533/*
13534** Determine whether triggers are recursive by default. This can be
13535** changed at run-time using a pragma.
13536*/
13537#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
13538# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
13539#endif
13540
13541/*
13542** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
13543** on the command-line
13544*/
13545#ifndef SQLITE_TEMP_STORE
13546# define SQLITE_TEMP_STORE 1
13547#endif
13548
13549/*
13550** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
13551** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
13552** to zero.
13553*/
13554#if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
13555# undef SQLITE_MAX_WORKER_THREADS
13556# define SQLITE_MAX_WORKER_THREADS 0
13557#endif
13558#ifndef SQLITE_MAX_WORKER_THREADS
13559# define SQLITE_MAX_WORKER_THREADS 8
13560#endif
13561#ifndef SQLITE_DEFAULT_WORKER_THREADS
13562# define SQLITE_DEFAULT_WORKER_THREADS 0
13563#endif
13564#if SQLITE_DEFAULT_WORKER_THREADS>SQLITE_MAX_WORKER_THREADS
13565# undef SQLITE_MAX_WORKER_THREADS
13566# define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS
13567#endif
13568
13569/*
13570** The default initial allocation for the pagecache when using separate
13571** pagecaches for each database connection. A positive number is the
13572** number of pages. A negative number N translations means that a buffer
13573** of -1024*N bytes is allocated and used for as many pages as it will hold.
13574**
13575** The default value of "20" was choosen to minimize the run-time of the
13576** speedtest1 test program with options: --shrink-memory --reprepare
13577*/
13578#ifndef SQLITE_DEFAULT_PCACHE_INITSZ
13579# define SQLITE_DEFAULT_PCACHE_INITSZ 20
13580#endif
13581
13582/*
13583** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
13584*/
13585#ifndef SQLITE_DEFAULT_SORTERREF_SIZE
13586# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff
13587#endif
13588
13589/*
13590** The compile-time options SQLITE_MMAP_READWRITE and
13591** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
13592** You must choose one or the other (or neither) but not both.
13593*/
13594#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
13595#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
13596#endif
13597
13598/*
13599** GCC does not define the offsetof() macro so we'll have to do it
13600** ourselves.
13601*/
13602#ifndef offsetof
13603#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
13604#endif
13605
13606/*
13607** Macros to compute minimum and maximum of two numbers.
13608*/
13609#ifndef MIN
13610# define MIN(A,B) ((A)<(B)?(A):(B))
13611#endif
13612#ifndef MAX
13613# define MAX(A,B) ((A)>(B)?(A):(B))
13614#endif
13615
13616/*
13617** Swap two objects of type TYPE.
13618*/
13619#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
13620
13621/*
13622** Check to see if this machine uses EBCDIC. (Yes, believe it or
13623** not, there are still machines out there that use EBCDIC.)
13624*/
13625#if 'A' == '\301'
13626# define SQLITE_EBCDIC 1
13627#else
13628# define SQLITE_ASCII 1
13629#endif
13630
13631/*
13632** Integers of known sizes. These typedefs might change for architectures
13633** where the sizes very. Preprocessor macros are available so that the
13634** types can be conveniently redefined at compile-type. Like this:
13635**
13636** cc '-DUINTPTR_TYPE=long long int' ...
13637*/
13638#ifndef UINT32_TYPE
13639# ifdef HAVE_UINT32_T
13640# define UINT32_TYPE uint32_t
13641# else
13642# define UINT32_TYPE unsigned int
13643# endif
13644#endif
13645#ifndef UINT16_TYPE
13646# ifdef HAVE_UINT16_T
13647# define UINT16_TYPE uint16_t
13648# else
13649# define UINT16_TYPE unsigned short int
13650# endif
13651#endif
13652#ifndef INT16_TYPE
13653# ifdef HAVE_INT16_T
13654# define INT16_TYPE int16_t
13655# else
13656# define INT16_TYPE short int
13657# endif
13658#endif
13659#ifndef UINT8_TYPE
13660# ifdef HAVE_UINT8_T
13661# define UINT8_TYPE uint8_t
13662# else
13663# define UINT8_TYPE unsigned char
13664# endif
13665#endif
13666#ifndef INT8_TYPE
13667# ifdef HAVE_INT8_T
13668# define INT8_TYPE int8_t
13669# else
13670# define INT8_TYPE signed char
13671# endif
13672#endif
13673#ifndef LONGDOUBLE_TYPE
13674# define LONGDOUBLE_TYPE long double
13675#endif
13676typedef sqlite_int64 i64; /* 8-byte signed integer */
13677typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
13678typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
13679typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
13680typedef INT16_TYPE i16; /* 2-byte signed integer */
13681typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
13682typedef INT8_TYPE i8; /* 1-byte signed integer */
13683
13684/*
13685** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
13686** that can be stored in a u32 without loss of data. The value
13687** is 0x00000000ffffffff. But because of quirks of some compilers, we
13688** have to specify the value in the less intuitive manner shown:
13689*/
13690#define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
13691
13692/*
13693** The datatype used to store estimates of the number of rows in a
13694** table or index. This is an unsigned integer type. For 99.9% of
13695** the world, a 32-bit integer is sufficient. But a 64-bit integer
13696** can be used at compile-time if desired.
13697*/
13698#ifdef SQLITE_64BIT_STATS
13699 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
13700#else
13701 typedef u32 tRowcnt; /* 32-bit is the default */
13702#endif
13703
13704/*
13705** Estimated quantities used for query planning are stored as 16-bit
13706** logarithms. For quantity X, the value stored is 10*log2(X). This
13707** gives a possible range of values of approximately 1.0e986 to 1e-986.
13708** But the allowed values are "grainy". Not every value is representable.
13709** For example, quantities 16 and 17 are both represented by a LogEst
13710** of 40. However, since LogEst quantities are suppose to be estimates,
13711** not exact values, this imprecision is not a problem.
13712**
13713** "LogEst" is short for "Logarithmic Estimate".
13714**
13715** Examples:
13716** 1 -> 0 20 -> 43 10000 -> 132
13717** 2 -> 10 25 -> 46 25000 -> 146
13718** 3 -> 16 100 -> 66 1000000 -> 199
13719** 4 -> 20 1000 -> 99 1048576 -> 200
13720** 10 -> 33 1024 -> 100 4294967296 -> 320
13721**
13722** The LogEst can be negative to indicate fractional values.
13723** Examples:
13724**
13725** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
13726*/
13727typedef INT16_TYPE LogEst;
13728
13729/*
13730** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
13731*/
13732#ifndef SQLITE_PTRSIZE
13733# if defined(__SIZEOF_POINTER__)
13734# define SQLITE_PTRSIZE __SIZEOF_POINTER__
13735# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
13736 defined(_M_ARM) || defined(__arm__) || defined(__x86) || \
13737 (defined(__TOS_AIX__) && !defined(__64BIT__))
13738# define SQLITE_PTRSIZE 4
13739# else
13740# define SQLITE_PTRSIZE 8
13741# endif
13742#endif
13743
13744/* The uptr type is an unsigned integer large enough to hold a pointer
13745*/
13746#if defined(HAVE_STDINT_H)
13747 typedef uintptr_t uptr;
13748#elif SQLITE_PTRSIZE==4
13749 typedef u32 uptr;
13750#else
13751 typedef u64 uptr;
13752#endif
13753
13754/*
13755** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
13756** something between S (inclusive) and E (exclusive).
13757**
13758** In other words, S is a buffer and E is a pointer to the first byte after
13759** the end of buffer S. This macro returns true if P points to something
13760** contained within the buffer S.
13761*/
13762#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
13763
13764
13765/*
13766** Macros to determine whether the machine is big or little endian,
13767** and whether or not that determination is run-time or compile-time.
13768**
13769** For best performance, an attempt is made to guess at the byte-order
13770** using C-preprocessor macros. If that is unsuccessful, or if
13771** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
13772** at run-time.
13773*/
13774#ifndef SQLITE_BYTEORDER
13775# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
13776 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
13777 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
13778 defined(__arm__) || defined(_M_ARM64)
13779# define SQLITE_BYTEORDER 1234
13780# elif defined(sparc) || defined(__ppc__)
13781# define SQLITE_BYTEORDER 4321
13782# else
13783# define SQLITE_BYTEORDER 0
13784# endif
13785#endif
13786#if SQLITE_BYTEORDER==4321
13787# define SQLITE_BIGENDIAN 1
13788# define SQLITE_LITTLEENDIAN 0
13789# define SQLITE_UTF16NATIVE SQLITE_UTF16BE
13790#elif SQLITE_BYTEORDER==1234
13791# define SQLITE_BIGENDIAN 0
13792# define SQLITE_LITTLEENDIAN 1
13793# define SQLITE_UTF16NATIVE SQLITE_UTF16LE
13794#else
13795# ifdef SQLITE_AMALGAMATION
13796 const int sqlite3one = 1;
13797# else
13798 extern const int sqlite3one;
13799# endif
13800# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
13801# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
13802# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
13803#endif
13804
13805/*
13806** Constants for the largest and smallest possible 64-bit signed integers.
13807** These macros are designed to work correctly on both 32-bit and 64-bit
13808** compilers.
13809*/
13810#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
13811#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
13812
13813/*
13814** Round up a number to the next larger multiple of 8. This is used
13815** to force 8-byte alignment on 64-bit architectures.
13816*/
13817#define ROUND8(x) (((x)+7)&~7)
13818
13819/*
13820** Round down to the nearest multiple of 8
13821*/
13822#define ROUNDDOWN8(x) ((x)&~7)
13823
13824/*
13825** Assert that the pointer X is aligned to an 8-byte boundary. This
13826** macro is used only within assert() to verify that the code gets
13827** all alignment restrictions correct.
13828**
13829** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
13830** underlying malloc() implementation might return us 4-byte aligned
13831** pointers. In that case, only verify 4-byte alignment.
13832*/
13833#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
13834# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
13835#else
13836# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
13837#endif
13838
13839/*
13840** Disable MMAP on platforms where it is known to not work
13841*/
13842#if defined(__OpenBSD__) || defined(__QNXNTO__)
13843# undef SQLITE_MAX_MMAP_SIZE
13844# define SQLITE_MAX_MMAP_SIZE 0
13845#endif
13846
13847/*
13848** Default maximum size of memory used by memory-mapped I/O in the VFS
13849*/
13850#ifdef __APPLE__
13851# include <TargetConditionals.h>
13852#endif
13853#ifndef SQLITE_MAX_MMAP_SIZE
13854# if defined(__linux__) \
13855 || defined(_WIN32) \
13856 || (defined(__APPLE__) && defined(__MACH__)) \
13857 || defined(__sun) \
13858 || defined(__FreeBSD__) \
13859 || defined(__DragonFly__)
13860# define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */
13861# else
13862# define SQLITE_MAX_MMAP_SIZE 0
13863# endif
13864#endif
13865
13866/*
13867** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
13868** default MMAP_SIZE is specified at compile-time, make sure that it does
13869** not exceed the maximum mmap size.
13870*/
13871#ifndef SQLITE_DEFAULT_MMAP_SIZE
13872# define SQLITE_DEFAULT_MMAP_SIZE 0
13873#endif
13874#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
13875# undef SQLITE_DEFAULT_MMAP_SIZE
13876# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
13877#endif
13878
13879/*
13880** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
13881** Priority is given to SQLITE_ENABLE_STAT4. If either are defined, also
13882** define SQLITE_ENABLE_STAT3_OR_STAT4
13883*/
13884#ifdef SQLITE_ENABLE_STAT4
13885# undef SQLITE_ENABLE_STAT3
13886# define SQLITE_ENABLE_STAT3_OR_STAT4 1
13887#elif SQLITE_ENABLE_STAT3
13888# define SQLITE_ENABLE_STAT3_OR_STAT4 1
13889#elif SQLITE_ENABLE_STAT3_OR_STAT4
13890# undef SQLITE_ENABLE_STAT3_OR_STAT4
13891#endif
13892
13893/*
13894** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
13895** the Select query generator tracing logic is turned on.
13896*/
13897#if defined(SQLITE_ENABLE_SELECTTRACE)
13898# define SELECTTRACE_ENABLED 1
13899#else
13900# define SELECTTRACE_ENABLED 0
13901#endif
13902
13903/*
13904** An instance of the following structure is used to store the busy-handler
13905** callback for a given sqlite handle.
13906**
13907** The sqlite.busyHandler member of the sqlite struct contains the busy
13908** callback for the database handle. Each pager opened via the sqlite
13909** handle is passed a pointer to sqlite.busyHandler. The busy-handler
13910** callback is currently invoked only from within pager.c.
13911*/
13912typedef struct BusyHandler BusyHandler;
13913struct BusyHandler {
13914 int (*xBusyHandler)(void *,int); /* The busy callback */
13915 void *pBusyArg; /* First arg to busy callback */
13916 int nBusy; /* Incremented with each busy call */
13917 u8 bExtraFileArg; /* Include sqlite3_file as callback arg */
13918};
13919
13920/*
13921** Name of the master database table. The master database table
13922** is a special table that holds the names and attributes of all
13923** user tables and indices.
13924*/
13925#define MASTER_NAME "sqlite_master"
13926#define TEMP_MASTER_NAME "sqlite_temp_master"
13927
13928/*
13929** The root-page of the master database table.
13930*/
13931#define MASTER_ROOT 1
13932
13933/*
13934** The name of the schema table.
13935*/
13936#define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
13937
13938/*
13939** A convenience macro that returns the number of elements in
13940** an array.
13941*/
13942#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
13943
13944/*
13945** Determine if the argument is a power of two
13946*/
13947#define IsPowerOfTwo(X) (((X)&((X)-1))==0)
13948
13949/*
13950** The following value as a destructor means to use sqlite3DbFree().
13951** The sqlite3DbFree() routine requires two parameters instead of the
13952** one parameter that destructors normally want. So we have to introduce
13953** this magic value that the code knows to handle differently. Any
13954** pointer will work here as long as it is distinct from SQLITE_STATIC
13955** and SQLITE_TRANSIENT.
13956*/
13957#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3MallocSize)
13958
13959/*
13960** When SQLITE_OMIT_WSD is defined, it means that the target platform does
13961** not support Writable Static Data (WSD) such as global and static variables.
13962** All variables must either be on the stack or dynamically allocated from
13963** the heap. When WSD is unsupported, the variable declarations scattered
13964** throughout the SQLite code must become constants instead. The SQLITE_WSD
13965** macro is used for this purpose. And instead of referencing the variable
13966** directly, we use its constant as a key to lookup the run-time allocated
13967** buffer that holds real variable. The constant is also the initializer
13968** for the run-time allocated buffer.
13969**
13970** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
13971** macros become no-ops and have zero performance impact.
13972*/
13973#ifdef SQLITE_OMIT_WSD
13974 #define SQLITE_WSD const
13975 #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
13976 #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
13977SQLITE_API int sqlite3_wsd_init(int N, int J);
13978SQLITE_API void *sqlite3_wsd_find(void *K, int L);
13979#else
13980 #define SQLITE_WSD
13981 #define GLOBAL(t,v) v
13982 #define sqlite3GlobalConfig sqlite3Config
13983#endif
13984
13985/*
13986** The following macros are used to suppress compiler warnings and to
13987** make it clear to human readers when a function parameter is deliberately
13988** left unused within the body of a function. This usually happens when
13989** a function is called via a function pointer. For example the
13990** implementation of an SQL aggregate step callback may not use the
13991** parameter indicating the number of arguments passed to the aggregate,
13992** if it knows that this is enforced elsewhere.
13993**
13994** When a function parameter is not used at all within the body of a function,
13995** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
13996** However, these macros may also be used to suppress warnings related to
13997** parameters that may or may not be used depending on compilation options.
13998** For example those parameters only used in assert() statements. In these
13999** cases the parameters are named as per the usual conventions.
14000*/
14001#define UNUSED_PARAMETER(x) (void)(x)
14002#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
14003
14004/*
14005** Forward references to structures
14006*/
14007typedef struct AggInfo AggInfo;
14008typedef struct AuthContext AuthContext;
14009typedef struct AutoincInfo AutoincInfo;
14010typedef struct Bitvec Bitvec;
14011typedef struct CollSeq CollSeq;
14012typedef struct Column Column;
14013typedef struct Db Db;
14014typedef struct Schema Schema;
14015typedef struct Expr Expr;
14016typedef struct ExprList ExprList;
14017typedef struct FKey FKey;
14018typedef struct FuncDestructor FuncDestructor;
14019typedef struct FuncDef FuncDef;
14020typedef struct FuncDefHash FuncDefHash;
14021typedef struct IdList IdList;
14022typedef struct Index Index;
14023typedef struct IndexSample IndexSample;
14024typedef struct KeyClass KeyClass;
14025typedef struct KeyInfo KeyInfo;
14026typedef struct Lookaside Lookaside;
14027typedef struct LookasideSlot LookasideSlot;
14028typedef struct Module Module;
14029typedef struct NameContext NameContext;
14030typedef struct Parse Parse;
14031typedef struct PreUpdate PreUpdate;
14032typedef struct PrintfArguments PrintfArguments;
14033typedef struct RenameToken RenameToken;
14034typedef struct RowSet RowSet;
14035typedef struct Savepoint Savepoint;
14036typedef struct Select Select;
14037typedef struct SQLiteThread SQLiteThread;
14038typedef struct SelectDest SelectDest;
14039typedef struct SrcList SrcList;
14040typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
14041typedef struct Table Table;
14042typedef struct TableLock TableLock;
14043typedef struct Token Token;
14044typedef struct TreeView TreeView;
14045typedef struct Trigger Trigger;
14046typedef struct TriggerPrg TriggerPrg;
14047typedef struct TriggerStep TriggerStep;
14048typedef struct UnpackedRecord UnpackedRecord;
14049typedef struct Upsert Upsert;
14050typedef struct VTable VTable;
14051typedef struct VtabCtx VtabCtx;
14052typedef struct Walker Walker;
14053typedef struct WhereInfo WhereInfo;
14054typedef struct Window Window;
14055typedef struct With With;
14056
14057
14058/*
14059** The bitmask datatype defined below is used for various optimizations.
14060**
14061** Changing this from a 64-bit to a 32-bit type limits the number of
14062** tables in a join to 32 instead of 64. But it also reduces the size
14063** of the library by 738 bytes on ix86.
14064*/
14065#ifdef SQLITE_BITMASK_TYPE
14066 typedef SQLITE_BITMASK_TYPE Bitmask;
14067#else
14068 typedef u64 Bitmask;
14069#endif
14070
14071/*
14072** The number of bits in a Bitmask. "BMS" means "BitMask Size".
14073*/
14074#define BMS ((int)(sizeof(Bitmask)*8))
14075
14076/*
14077** A bit in a Bitmask
14078*/
14079#define MASKBIT(n) (((Bitmask)1)<<(n))
14080#define MASKBIT32(n) (((unsigned int)1)<<(n))
14081#define ALLBITS ((Bitmask)-1)
14082
14083/* A VList object records a mapping between parameters/variables/wildcards
14084** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
14085** variable number associated with that parameter. See the format description
14086** on the sqlite3VListAdd() routine for more information. A VList is really
14087** just an array of integers.
14088*/
14089typedef int VList;
14090
14091/*
14092** Defer sourcing vdbe.h and btree.h until after the "u8" and
14093** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
14094** pointer types (i.e. FuncDef) defined above.
14095*/
14096/************** Include btree.h in the middle of sqliteInt.h *****************/
14097/************** Begin file btree.h *******************************************/
14098/*
14099** 2001 September 15
14100**
14101** The author disclaims copyright to this source code. In place of
14102** a legal notice, here is a blessing:
14103**
14104** May you do good and not evil.
14105** May you find forgiveness for yourself and forgive others.
14106** May you share freely, never taking more than you give.
14107**
14108*************************************************************************
14109** This header file defines the interface that the sqlite B-Tree file
14110** subsystem. See comments in the source code for a detailed description
14111** of what each interface routine does.
14112*/
14113#ifndef SQLITE_BTREE_H
14114#define SQLITE_BTREE_H
14115
14116/* TODO: This definition is just included so other modules compile. It
14117** needs to be revisited.
14118*/
14119#define SQLITE_N_BTREE_META 16
14120
14121/*
14122** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
14123** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
14124*/
14125#ifndef SQLITE_DEFAULT_AUTOVACUUM
14126 #define SQLITE_DEFAULT_AUTOVACUUM 0
14127#endif
14128
14129#define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
14130#define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
14131#define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */
14132
14133/*
14134** Forward declarations of structure
14135*/
14136typedef struct Btree Btree;
14137typedef struct BtCursor BtCursor;
14138typedef struct BtShared BtShared;
14139typedef struct BtreePayload BtreePayload;
14140
14141
14142SQLITE_PRIVATE int sqlite3BtreeOpen(
14143 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
14144 const char *zFilename, /* Name of database file to open */
14145 sqlite3 *db, /* Associated database connection */
14146 Btree **ppBtree, /* Return open Btree* here */
14147 int flags, /* Flags */
14148 int vfsFlags /* Flags passed through to VFS open */
14149);
14150
14151/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
14152** following values.
14153**
14154** NOTE: These values must match the corresponding PAGER_ values in
14155** pager.h.
14156*/
14157#define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */
14158#define BTREE_MEMORY 2 /* This is an in-memory DB */
14159#define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
14160#define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
14161
14162SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
14163SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
14164SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree*,int);
14165#if SQLITE_MAX_MMAP_SIZE>0
14166SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
14167#endif
14168SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
14169SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
14170SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
14171SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
14172SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
14173SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
14174SQLITE_PRIVATE int sqlite3BtreeGetOptimalReserve(Btree*);
14175SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
14176SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
14177SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
14178SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int,int*);
14179SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
14180SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
14181SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
14182SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int,int);
14183SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
14184SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
14185SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
14186SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
14187SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
14188SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
14189SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
14190#ifndef SQLITE_OMIT_SHARED_CACHE
14191SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
14192#endif
14193SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
14194
14195SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
14196SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
14197SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
14198
14199SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
14200
14201/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
14202** of the flags shown below.
14203**
14204** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
14205** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
14206** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
14207** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
14208** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
14209** indices.)
14210*/
14211#define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
14212#define BTREE_BLOBKEY 2 /* Table has keys only - no data */
14213
14214SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
14215SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
14216SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor*);
14217SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree*, int, int);
14218
14219SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
14220SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
14221
14222SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
14223
14224/*
14225** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
14226** should be one of the following values. The integer values are assigned
14227** to constants so that the offset of the corresponding field in an
14228** SQLite database header may be found using the following formula:
14229**
14230** offset = 36 + (idx * 4)
14231**
14232** For example, the free-page-count field is located at byte offset 36 of
14233** the database file header. The incr-vacuum-flag field is located at
14234** byte offset 64 (== 36+4*7).
14235**
14236** The BTREE_DATA_VERSION value is not really a value stored in the header.
14237** It is a read-only number computed by the pager. But we merge it with
14238** the header value access routines since its access pattern is the same.
14239** Call it a "virtual meta value".
14240*/
14241#define BTREE_FREE_PAGE_COUNT 0
14242#define BTREE_SCHEMA_VERSION 1
14243#define BTREE_FILE_FORMAT 2
14244#define BTREE_DEFAULT_CACHE_SIZE 3
14245#define BTREE_LARGEST_ROOT_PAGE 4
14246#define BTREE_TEXT_ENCODING 5
14247#define BTREE_USER_VERSION 6
14248#define BTREE_INCR_VACUUM 7
14249#define BTREE_APPLICATION_ID 8
14250#define BTREE_DATA_VERSION 15 /* A virtual meta-value */
14251
14252/*
14253** Kinds of hints that can be passed into the sqlite3BtreeCursorHint()
14254** interface.
14255**
14256** BTREE_HINT_RANGE (arguments: Expr*, Mem*)
14257**
14258** The first argument is an Expr* (which is guaranteed to be constant for
14259** the lifetime of the cursor) that defines constraints on which rows
14260** might be fetched with this cursor. The Expr* tree may contain
14261** TK_REGISTER nodes that refer to values stored in the array of registers
14262** passed as the second parameter. In other words, if Expr.op==TK_REGISTER
14263** then the value of the node is the value in Mem[pExpr.iTable]. Any
14264** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
14265** column of the b-tree of the cursor. The Expr tree will not contain
14266** any function calls nor subqueries nor references to b-trees other than
14267** the cursor being hinted.
14268**
14269** The design of the _RANGE hint is aid b-tree implementations that try
14270** to prefetch content from remote machines - to provide those
14271** implementations with limits on what needs to be prefetched and thereby
14272** reduce network bandwidth.
14273**
14274** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by
14275** standard SQLite. The other hints are provided for extentions that use
14276** the SQLite parser and code generator but substitute their own storage
14277** engine.
14278*/
14279#define BTREE_HINT_RANGE 0 /* Range constraints on queries */
14280
14281/*
14282** Values that may be OR'd together to form the argument to the
14283** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint():
14284**
14285** The BTREE_BULKLOAD flag is set on index cursors when the index is going
14286** to be filled with content that is already in sorted order.
14287**
14288** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
14289** OP_SeekLE opcodes for a range search, but where the range of entries
14290** selected will all have the same key. In other words, the cursor will
14291** be used only for equality key searches.
14292**
14293*/
14294#define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */
14295#define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
14296
14297/*
14298** Flags passed as the third argument to sqlite3BtreeCursor().
14299**
14300** For read-only cursors the wrFlag argument is always zero. For read-write
14301** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
14302** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
14303** only be used by SQLite for the following:
14304**
14305** * to seek to and then delete specific entries, and/or
14306**
14307** * to read values that will be used to create keys that other
14308** BTREE_FORDELETE cursors will seek to and delete.
14309**
14310** The BTREE_FORDELETE flag is an optimization hint. It is not used by
14311** by this, the native b-tree engine of SQLite, but it is available to
14312** alternative storage engines that might be substituted in place of this
14313** b-tree system. For alternative storage engines in which a delete of
14314** the main table row automatically deletes corresponding index rows,
14315** the FORDELETE flag hint allows those alternative storage engines to
14316** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK
14317** and DELETE operations as no-ops, and any READ operation against a
14318** FORDELETE cursor may return a null row: 0x01 0x00.
14319*/
14320#define BTREE_WRCSR 0x00000004 /* read-write cursor */
14321#define BTREE_FORDELETE 0x00000008 /* Cursor is for seek/delete only */
14322
14323SQLITE_PRIVATE int sqlite3BtreeCursor(
14324 Btree*, /* BTree containing table to open */
14325 int iTable, /* Index of root page */
14326 int wrFlag, /* 1 for writing. 0 for read-only */
14327 struct KeyInfo*, /* First argument to compare function */
14328 BtCursor *pCursor /* Space to write cursor structure */
14329);
14330SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void);
14331SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
14332SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
14333SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
14334#ifdef SQLITE_ENABLE_CURSOR_HINTS
14335SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor*, int, ...);
14336#endif
14337
14338SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
14339SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
14340 BtCursor*,
14341 UnpackedRecord *pUnKey,
14342 i64 intKey,
14343 int bias,
14344 int *pRes
14345);
14346SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
14347SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
14348SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
14349
14350/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
14351#define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
14352#define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
14353#define BTREE_APPEND 0x08 /* Insert is likely an append */
14354
14355/* An instance of the BtreePayload object describes the content of a single
14356** entry in either an index or table btree.
14357**
14358** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
14359** an arbitrary key and no data. These btrees have pKey,nKey set to the
14360** key and the pData,nData,nZero fields are uninitialized. The aMem,nMem
14361** fields give an array of Mem objects that are a decomposition of the key.
14362** The nMem field might be zero, indicating that no decomposition is available.
14363**
14364** Table btrees (used for rowid tables) contain an integer rowid used as
14365** the key and passed in the nKey field. The pKey field is zero.
14366** pData,nData hold the content of the new entry. nZero extra zero bytes
14367** are appended to the end of the content when constructing the entry.
14368** The aMem,nMem fields are uninitialized for table btrees.
14369**
14370** Field usage summary:
14371**
14372** Table BTrees Index Btrees
14373**
14374** pKey always NULL encoded key
14375** nKey the ROWID length of pKey
14376** pData data not used
14377** aMem not used decomposed key value
14378** nMem not used entries in aMem
14379** nData length of pData not used
14380** nZero extra zeros after pData not used
14381**
14382** This object is used to pass information into sqlite3BtreeInsert(). The
14383** same information used to be passed as five separate parameters. But placing
14384** the information into this object helps to keep the interface more
14385** organized and understandable, and it also helps the resulting code to
14386** run a little faster by using fewer registers for parameter passing.
14387*/
14388struct BtreePayload {
14389 const void *pKey; /* Key content for indexes. NULL for tables */
14390 sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */
14391 const void *pData; /* Data for tables. */
14392 sqlite3_value *aMem; /* First of nMem value in the unpacked pKey */
14393 u16 nMem; /* Number of aMem[] value. Might be zero */
14394 int nData; /* Size of pData. 0 if none. */
14395 int nZero; /* Extra zero data appended after pData,nData */
14396};
14397
14398SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
14399 int flags, int seekResult);
14400SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
14401#ifndef SQLITE_OMIT_WINDOWFUNC
14402SQLITE_PRIVATE void sqlite3BtreeSkipNext(BtCursor*);
14403#endif
14404SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
14405SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
14406SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
14407SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
14408SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
14409#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
14410SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*);
14411#endif
14412SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
14413SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
14414SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*);
14415
14416SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
14417SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
14418SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor*);
14419
14420#ifndef SQLITE_OMIT_INCRBLOB
14421SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
14422SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
14423SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
14424#endif
14425SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
14426SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
14427SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
14428SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
14429SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
14430
14431#ifndef NDEBUG
14432SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
14433#endif
14434SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor*);
14435
14436#ifndef SQLITE_OMIT_BTREECOUNT
14437SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
14438#endif
14439
14440#ifdef SQLITE_TEST
14441SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
14442SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
14443#endif
14444
14445#ifndef SQLITE_OMIT_WAL
14446SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
14447#endif
14448
14449/*
14450** If we are not using shared cache, then there is no need to
14451** use mutexes to access the BtShared structures. So make the
14452** Enter and Leave procedures no-ops.
14453*/
14454#ifndef SQLITE_OMIT_SHARED_CACHE
14455SQLITE_PRIVATE void sqlite3BtreeEnter(Btree*);
14456SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3*);
14457SQLITE_PRIVATE int sqlite3BtreeSharable(Btree*);
14458SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*);
14459SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree*);
14460#else
14461# define sqlite3BtreeEnter(X)
14462# define sqlite3BtreeEnterAll(X)
14463# define sqlite3BtreeSharable(X) 0
14464# define sqlite3BtreeEnterCursor(X)
14465# define sqlite3BtreeConnectionCount(X) 1
14466#endif
14467
14468#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
14469SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*);
14470SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*);
14471SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*);
14472#ifndef NDEBUG
14473 /* These routines are used inside assert() statements only. */
14474SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*);
14475SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*);
14476SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
14477#endif
14478#else
14479
14480# define sqlite3BtreeLeave(X)
14481# define sqlite3BtreeLeaveCursor(X)
14482# define sqlite3BtreeLeaveAll(X)
14483
14484# define sqlite3BtreeHoldsMutex(X) 1
14485# define sqlite3BtreeHoldsAllMutexes(X) 1
14486# define sqlite3SchemaMutexHeld(X,Y,Z) 1
14487#endif
14488
14489
14490#endif /* SQLITE_BTREE_H */
14491
14492/************** End of btree.h ***********************************************/
14493/************** Continuing where we left off in sqliteInt.h ******************/
14494/************** Include vdbe.h in the middle of sqliteInt.h ******************/
14495/************** Begin file vdbe.h ********************************************/
14496/*
14497** 2001 September 15
14498**
14499** The author disclaims copyright to this source code. In place of
14500** a legal notice, here is a blessing:
14501**
14502** May you do good and not evil.
14503** May you find forgiveness for yourself and forgive others.
14504** May you share freely, never taking more than you give.
14505**
14506*************************************************************************
14507** Header file for the Virtual DataBase Engine (VDBE)
14508**
14509** This header defines the interface to the virtual database engine
14510** or VDBE. The VDBE implements an abstract machine that runs a
14511** simple program to access and modify the underlying database.
14512*/
14513#ifndef SQLITE_VDBE_H
14514#define SQLITE_VDBE_H
14515/* #include <stdio.h> */
14516
14517/*
14518** A single VDBE is an opaque structure named "Vdbe". Only routines
14519** in the source file sqliteVdbe.c are allowed to see the insides
14520** of this structure.
14521*/
14522typedef struct Vdbe Vdbe;
14523
14524/*
14525** The names of the following types declared in vdbeInt.h are required
14526** for the VdbeOp definition.
14527*/
14528typedef struct sqlite3_value Mem;
14529typedef struct SubProgram SubProgram;
14530
14531/*
14532** A single instruction of the virtual machine has an opcode
14533** and as many as three operands. The instruction is recorded
14534** as an instance of the following structure:
14535*/
14536struct VdbeOp {
14537 u8 opcode; /* What operation to perform */
14538 signed char p4type; /* One of the P4_xxx constants for p4 */
14539 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
14540 int p1; /* First operand */
14541 int p2; /* Second parameter (often the jump destination) */
14542 int p3; /* The third parameter */
14543 union p4union { /* fourth parameter */
14544 int i; /* Integer value if p4type==P4_INT32 */
14545 void *p; /* Generic pointer */
14546 char *z; /* Pointer to data for string (char array) types */
14547 i64 *pI64; /* Used when p4type is P4_INT64 */
14548 double *pReal; /* Used when p4type is P4_REAL */
14549 FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
14550 sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
14551 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
14552 Mem *pMem; /* Used when p4type is P4_MEM */
14553 VTable *pVtab; /* Used when p4type is P4_VTAB */
14554 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
14555 int *ai; /* Used when p4type is P4_INTARRAY */
14556 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
14557 Table *pTab; /* Used when p4type is P4_TABLE */
14558#ifdef SQLITE_ENABLE_CURSOR_HINTS
14559 Expr *pExpr; /* Used when p4type is P4_EXPR */
14560#endif
14561 int (*xAdvance)(BtCursor *, int);
14562 } p4;
14563#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
14564 char *zComment; /* Comment to improve readability */
14565#endif
14566#ifdef VDBE_PROFILE
14567 u32 cnt; /* Number of times this instruction was executed */
14568 u64 cycles; /* Total time spent executing this instruction */
14569#endif
14570#ifdef SQLITE_VDBE_COVERAGE
14571 u32 iSrcLine; /* Source-code line that generated this opcode
14572 ** with flags in the upper 8 bits */
14573#endif
14574};
14575typedef struct VdbeOp VdbeOp;
14576
14577
14578/*
14579** A sub-routine used to implement a trigger program.
14580*/
14581struct SubProgram {
14582 VdbeOp *aOp; /* Array of opcodes for sub-program */
14583 int nOp; /* Elements in aOp[] */
14584 int nMem; /* Number of memory cells required */
14585 int nCsr; /* Number of cursors required */
14586 u8 *aOnce; /* Array of OP_Once flags */
14587 void *token; /* id that may be used to recursive triggers */
14588 SubProgram *pNext; /* Next sub-program already visited */
14589};
14590
14591/*
14592** A smaller version of VdbeOp used for the VdbeAddOpList() function because
14593** it takes up less space.
14594*/
14595struct VdbeOpList {
14596 u8 opcode; /* What operation to perform */
14597 signed char p1; /* First operand */
14598 signed char p2; /* Second parameter (often the jump destination) */
14599 signed char p3; /* Third parameter */
14600};
14601typedef struct VdbeOpList VdbeOpList;
14602
14603/*
14604** Allowed values of VdbeOp.p4type
14605*/
14606#define P4_NOTUSED 0 /* The P4 parameter is not used */
14607#define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */
14608#define P4_STATIC (-1) /* Pointer to a static string */
14609#define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
14610#define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
14611#define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
14612#define P4_ADVANCE (-5) /* P4 is a pointer to BtreeNext() or BtreePrev() */
14613#define P4_TABLE (-6) /* P4 is a pointer to a Table structure */
14614/* Above do not own any resources. Must free those below */
14615#define P4_FREE_IF_LE (-7)
14616#define P4_DYNAMIC (-7) /* Pointer to memory from sqliteMalloc() */
14617#define P4_FUNCDEF (-8) /* P4 is a pointer to a FuncDef structure */
14618#define P4_KEYINFO (-9) /* P4 is a pointer to a KeyInfo structure */
14619#define P4_EXPR (-10) /* P4 is a pointer to an Expr tree */
14620#define P4_MEM (-11) /* P4 is a pointer to a Mem* structure */
14621#define P4_VTAB (-12) /* P4 is a pointer to an sqlite3_vtab structure */
14622#define P4_REAL (-13) /* P4 is a 64-bit floating point value */
14623#define P4_INT64 (-14) /* P4 is a 64-bit signed integer */
14624#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
14625#define P4_FUNCCTX (-16) /* P4 is a pointer to an sqlite3_context object */
14626#define P4_DYNBLOB (-17) /* Pointer to memory from sqliteMalloc() */
14627
14628/* Error message codes for OP_Halt */
14629#define P5_ConstraintNotNull 1
14630#define P5_ConstraintUnique 2
14631#define P5_ConstraintCheck 3
14632#define P5_ConstraintFK 4
14633
14634/*
14635** The Vdbe.aColName array contains 5n Mem structures, where n is the
14636** number of columns of data returned by the statement.
14637*/
14638#define COLNAME_NAME 0
14639#define COLNAME_DECLTYPE 1
14640#define COLNAME_DATABASE 2
14641#define COLNAME_TABLE 3
14642#define COLNAME_COLUMN 4
14643#ifdef SQLITE_ENABLE_COLUMN_METADATA
14644# define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
14645#else
14646# ifdef SQLITE_OMIT_DECLTYPE
14647# define COLNAME_N 1 /* Store only the name */
14648# else
14649# define COLNAME_N 2 /* Store the name and decltype */
14650# endif
14651#endif
14652
14653/*
14654** The following macro converts a relative address in the p2 field
14655** of a VdbeOp structure into a negative number so that
14656** sqlite3VdbeAddOpList() knows that the address is relative. Calling
14657** the macro again restores the address.
14658*/
14659#define ADDR(X) (-1-(X))
14660
14661/*
14662** The makefile scans the vdbe.c source file and creates the "opcodes.h"
14663** header file that defines a number for each opcode used by the VDBE.
14664*/
14665/************** Include opcodes.h in the middle of vdbe.h ********************/
14666/************** Begin file opcodes.h *****************************************/
14667/* Automatically generated. Do not edit */
14668/* See the tool/mkopcodeh.tcl script for details */
14669#define OP_Savepoint 0
14670#define OP_AutoCommit 1
14671#define OP_Transaction 2
14672#define OP_SorterNext 3 /* jump */
14673#define OP_Prev 4 /* jump */
14674#define OP_Next 5 /* jump */
14675#define OP_Checkpoint 6
14676#define OP_JournalMode 7
14677#define OP_Vacuum 8
14678#define OP_VFilter 9 /* jump, synopsis: iplan=r[P3] zplan='P4' */
14679#define OP_VUpdate 10 /* synopsis: data=r[P3@P2] */
14680#define OP_Goto 11 /* jump */
14681#define OP_Gosub 12 /* jump */
14682#define OP_InitCoroutine 13 /* jump */
14683#define OP_Yield 14 /* jump */
14684#define OP_MustBeInt 15 /* jump */
14685#define OP_Jump 16 /* jump */
14686#define OP_Once 17 /* jump */
14687#define OP_If 18 /* jump */
14688#define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
14689#define OP_IfNot 20 /* jump */
14690#define OP_IfNullRow 21 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
14691#define OP_SeekLT 22 /* jump, synopsis: key=r[P3@P4] */
14692#define OP_SeekLE 23 /* jump, synopsis: key=r[P3@P4] */
14693#define OP_SeekGE 24 /* jump, synopsis: key=r[P3@P4] */
14694#define OP_SeekGT 25 /* jump, synopsis: key=r[P3@P4] */
14695#define OP_IfNoHope 26 /* jump, synopsis: key=r[P3@P4] */
14696#define OP_NoConflict 27 /* jump, synopsis: key=r[P3@P4] */
14697#define OP_NotFound 28 /* jump, synopsis: key=r[P3@P4] */
14698#define OP_Found 29 /* jump, synopsis: key=r[P3@P4] */
14699#define OP_SeekRowid 30 /* jump, synopsis: intkey=r[P3] */
14700#define OP_NotExists 31 /* jump, synopsis: intkey=r[P3] */
14701#define OP_Last 32 /* jump */
14702#define OP_IfSmaller 33 /* jump */
14703#define OP_SorterSort 34 /* jump */
14704#define OP_Sort 35 /* jump */
14705#define OP_Rewind 36 /* jump */
14706#define OP_IdxLE 37 /* jump, synopsis: key=r[P3@P4] */
14707#define OP_IdxGT 38 /* jump, synopsis: key=r[P3@P4] */
14708#define OP_IdxLT 39 /* jump, synopsis: key=r[P3@P4] */
14709#define OP_IdxGE 40 /* jump, synopsis: key=r[P3@P4] */
14710#define OP_RowSetRead 41 /* jump, synopsis: r[P3]=rowset(P1) */
14711#define OP_RowSetTest 42 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
14712#define OP_Or 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
14713#define OP_And 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
14714#define OP_Program 45 /* jump */
14715#define OP_FkIfZero 46 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
14716#define OP_IfPos 47 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
14717#define OP_IfNotZero 48 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
14718#define OP_DecrJumpZero 49 /* jump, synopsis: if (--r[P1])==0 goto P2 */
14719#define OP_IsNull 50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
14720#define OP_NotNull 51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
14721#define OP_Ne 52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
14722#define OP_Eq 53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
14723#define OP_Gt 54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
14724#define OP_Le 55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
14725#define OP_Lt 56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
14726#define OP_Ge 57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
14727#define OP_ElseNotEq 58 /* jump, same as TK_ESCAPE */
14728#define OP_IncrVacuum 59 /* jump */
14729#define OP_VNext 60 /* jump */
14730#define OP_Init 61 /* jump, synopsis: Start at P2 */
14731#define OP_PureFunc0 62
14732#define OP_Function0 63 /* synopsis: r[P3]=func(r[P2@P5]) */
14733#define OP_PureFunc 64
14734#define OP_Function 65 /* synopsis: r[P3]=func(r[P2@P5]) */
14735#define OP_Return 66
14736#define OP_EndCoroutine 67
14737#define OP_HaltIfNull 68 /* synopsis: if r[P3]=null halt */
14738#define OP_Halt 69
14739#define OP_Integer 70 /* synopsis: r[P2]=P1 */
14740#define OP_Int64 71 /* synopsis: r[P2]=P4 */
14741#define OP_String 72 /* synopsis: r[P2]='P4' (len=P1) */
14742#define OP_Null 73 /* synopsis: r[P2..P3]=NULL */
14743#define OP_SoftNull 74 /* synopsis: r[P1]=NULL */
14744#define OP_Blob 75 /* synopsis: r[P2]=P4 (len=P1) */
14745#define OP_Variable 76 /* synopsis: r[P2]=parameter(P1,P4) */
14746#define OP_Move 77 /* synopsis: r[P2@P3]=r[P1@P3] */
14747#define OP_Copy 78 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
14748#define OP_SCopy 79 /* synopsis: r[P2]=r[P1] */
14749#define OP_IntCopy 80 /* synopsis: r[P2]=r[P1] */
14750#define OP_ResultRow 81 /* synopsis: output=r[P1@P2] */
14751#define OP_CollSeq 82
14752#define OP_AddImm 83 /* synopsis: r[P1]=r[P1]+P2 */
14753#define OP_RealAffinity 84
14754#define OP_Cast 85 /* synopsis: affinity(r[P1]) */
14755#define OP_Permutation 86
14756#define OP_Compare 87 /* synopsis: r[P1@P3] <-> r[P2@P3] */
14757#define OP_IsTrue 88 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
14758#define OP_Offset 89 /* synopsis: r[P3] = sqlite_offset(P1) */
14759#define OP_Column 90 /* synopsis: r[P3]=PX */
14760#define OP_Affinity 91 /* synopsis: affinity(r[P1@P2]) */
14761#define OP_BitAnd 92 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
14762#define OP_BitOr 93 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
14763#define OP_ShiftLeft 94 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
14764#define OP_ShiftRight 95 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
14765#define OP_Add 96 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
14766#define OP_Subtract 97 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
14767#define OP_Multiply 98 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
14768#define OP_Divide 99 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
14769#define OP_Remainder 100 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
14770#define OP_Concat 101 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
14771#define OP_MakeRecord 102 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
14772#define OP_BitNot 103 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
14773#define OP_Count 104 /* synopsis: r[P2]=count() */
14774#define OP_ReadCookie 105
14775#define OP_String8 106 /* same as TK_STRING, synopsis: r[P2]='P4' */
14776#define OP_SetCookie 107
14777#define OP_ReopenIdx 108 /* synopsis: root=P2 iDb=P3 */
14778#define OP_OpenRead 109 /* synopsis: root=P2 iDb=P3 */
14779#define OP_OpenWrite 110 /* synopsis: root=P2 iDb=P3 */
14780#define OP_OpenDup 111
14781#define OP_OpenAutoindex 112 /* synopsis: nColumn=P2 */
14782#define OP_OpenEphemeral 113 /* synopsis: nColumn=P2 */
14783#define OP_SorterOpen 114
14784#define OP_SequenceTest 115 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
14785#define OP_OpenPseudo 116 /* synopsis: P3 columns in r[P2] */
14786#define OP_Close 117
14787#define OP_ColumnsUsed 118
14788#define OP_SeekHit 119 /* synopsis: seekHit=P2 */
14789#define OP_Sequence 120 /* synopsis: r[P2]=cursor[P1].ctr++ */
14790#define OP_NewRowid 121 /* synopsis: r[P2]=rowid */
14791#define OP_Insert 122 /* synopsis: intkey=r[P3] data=r[P2] */
14792#define OP_InsertInt 123 /* synopsis: intkey=P3 data=r[P2] */
14793#define OP_Delete 124
14794#define OP_ResetCount 125
14795#define OP_SorterCompare 126 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
14796#define OP_SorterData 127 /* synopsis: r[P2]=data */
14797#define OP_RowData 128 /* synopsis: r[P2]=data */
14798#define OP_Rowid 129 /* synopsis: r[P2]=rowid */
14799#define OP_NullRow 130
14800#define OP_SeekEnd 131
14801#define OP_SorterInsert 132 /* synopsis: key=r[P2] */
14802#define OP_IdxInsert 133 /* synopsis: key=r[P2] */
14803#define OP_IdxDelete 134 /* synopsis: key=r[P2@P3] */
14804#define OP_DeferredSeek 135 /* synopsis: Move P3 to P1.rowid if needed */
14805#define OP_IdxRowid 136 /* synopsis: r[P2]=rowid */
14806#define OP_Destroy 137
14807#define OP_Clear 138
14808#define OP_ResetSorter 139
14809#define OP_CreateBtree 140 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
14810#define OP_Real 141 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
14811#define OP_SqlExec 142
14812#define OP_ParseSchema 143
14813#define OP_LoadAnalysis 144
14814#define OP_DropTable 145
14815#define OP_DropIndex 146
14816#define OP_DropTrigger 147
14817#define OP_IntegrityCk 148
14818#define OP_RowSetAdd 149 /* synopsis: rowset(P1)=r[P2] */
14819#define OP_Param 150
14820#define OP_FkCounter 151 /* synopsis: fkctr[P1]+=P2 */
14821#define OP_MemMax 152 /* synopsis: r[P1]=max(r[P1],r[P2]) */
14822#define OP_OffsetLimit 153 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
14823#define OP_AggInverse 154 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */
14824#define OP_AggStep 155 /* synopsis: accum=r[P3] step(r[P2@P5]) */
14825#define OP_AggStep1 156 /* synopsis: accum=r[P3] step(r[P2@P5]) */
14826#define OP_AggValue 157 /* synopsis: r[P3]=value N=P2 */
14827#define OP_AggFinal 158 /* synopsis: accum=r[P1] N=P2 */
14828#define OP_Expire 159
14829#define OP_TableLock 160 /* synopsis: iDb=P1 root=P2 write=P3 */
14830#define OP_VBegin 161
14831#define OP_VCreate 162
14832#define OP_VDestroy 163
14833#define OP_VOpen 164
14834#define OP_VColumn 165 /* synopsis: r[P3]=vcolumn(P2) */
14835#define OP_VRename 166
14836#define OP_Pagecount 167
14837#define OP_MaxPgcnt 168
14838#define OP_Trace 169
14839#define OP_CursorHint 170
14840#define OP_Noop 171
14841#define OP_Explain 172
14842#define OP_Abortable 173
14843
14844/* Properties such as "out2" or "jump" that are specified in
14845** comments following the "case" for each opcode in the vdbe.c
14846** are encoded into bitvectors as follows:
14847*/
14848#define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */
14849#define OPFLG_IN1 0x02 /* in1: P1 is an input */
14850#define OPFLG_IN2 0x04 /* in2: P2 is an input */
14851#define OPFLG_IN3 0x08 /* in3: P3 is an input */
14852#define OPFLG_OUT2 0x10 /* out2: P2 is an output */
14853#define OPFLG_OUT3 0x20 /* out3: P3 is an output */
14854#define OPFLG_INITIALIZER {\
14855/* 0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x10,\
14856/* 8 */ 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03,\
14857/* 16 */ 0x01, 0x01, 0x03, 0x12, 0x03, 0x01, 0x09, 0x09,\
14858/* 24 */ 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\
14859/* 32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
14860/* 40 */ 0x01, 0x23, 0x0b, 0x26, 0x26, 0x01, 0x01, 0x03,\
14861/* 48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
14862/* 56 */ 0x0b, 0x0b, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,\
14863/* 64 */ 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10,\
14864/* 72 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
14865/* 80 */ 0x10, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,\
14866/* 88 */ 0x12, 0x20, 0x00, 0x00, 0x26, 0x26, 0x26, 0x26,\
14867/* 96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
14868/* 104 */ 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
14869/* 112 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14870/* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14871/* 128 */ 0x00, 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00,\
14872/* 136 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
14873/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x10, 0x00,\
14874/* 152 */ 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
14875/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
14876/* 168 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,}
14877
14878/* The sqlite3P2Values() routine is able to run faster if it knows
14879** the value of the largest JUMP opcode. The smaller the maximum
14880** JUMP opcode the better, so the mkopcodeh.tcl script that
14881** generated this include file strives to group all JUMP opcodes
14882** together near the beginning of the list.
14883*/
14884#define SQLITE_MX_JUMP_OPCODE 61 /* Maximum JUMP opcode */
14885
14886/************** End of opcodes.h *********************************************/
14887/************** Continuing where we left off in vdbe.h ***********************/
14888
14889/*
14890** Additional non-public SQLITE_PREPARE_* flags
14891*/
14892#define SQLITE_PREPARE_SAVESQL 0x80 /* Preserve SQL text */
14893#define SQLITE_PREPARE_MASK 0x0f /* Mask of public flags */
14894
14895/*
14896** Prototypes for the VDBE interface. See comments on the implementation
14897** for a description of what each of these routines does.
14898*/
14899SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
14900SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
14901SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
14902SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
14903SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe*,int);
14904SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe*,int,const char*);
14905SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
14906SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
14907SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
14908SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
14909SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
14910SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe*,int);
14911#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
14912SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
14913SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
14914#else
14915# define sqlite3VdbeVerifyNoMallocRequired(A,B)
14916# define sqlite3VdbeVerifyNoResultRow(A)
14917#endif
14918#if defined(SQLITE_DEBUG)
14919SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
14920#else
14921# define sqlite3VdbeVerifyAbortable(A,B)
14922#endif
14923SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
14924#ifndef SQLITE_OMIT_EXPLAIN
14925SQLITE_PRIVATE void sqlite3VdbeExplain(Parse*,u8,const char*,...);
14926SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse*);
14927SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse*);
14928# define ExplainQueryPlan(P) sqlite3VdbeExplain P
14929# define ExplainQueryPlanPop(P) sqlite3VdbeExplainPop(P)
14930# define ExplainQueryPlanParent(P) sqlite3VdbeExplainParent(P)
14931#else
14932# define ExplainQueryPlan(P)
14933# define ExplainQueryPlanPop(P)
14934# define ExplainQueryPlanParent(P) 0
14935#endif
14936SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
14937SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
14938SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
14939SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
14940SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
14941SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
14942SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
14943SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
14944SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
14945SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
14946SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
14947SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
14948SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
14949SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
14950SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
14951SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
14952SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
14953SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
14954SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
14955SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
14956SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
14957SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
14958SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
14959#ifdef SQLITE_DEBUG
14960SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *, int);
14961#endif
14962SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
14963SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
14964SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
14965SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
14966SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
14967SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
14968SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
14969SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe*);
14970SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
14971SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
14972SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
14973SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
14974SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
14975#ifndef SQLITE_OMIT_TRACE
14976SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
14977#endif
14978SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
14979SQLITE_PRIVATE int sqlite3BlobCompare(const Mem*, const Mem*);
14980
14981SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
14982SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
14983SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
14984SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
14985
14986typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
14987SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
14988
14989#ifndef SQLITE_OMIT_TRIGGER
14990SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
14991#endif
14992
14993SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context*);
14994
14995/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
14996** each VDBE opcode.
14997**
14998** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
14999** comments in VDBE programs that show key decision points in the code
15000** generator.
15001*/
15002#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
15003SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe*, const char*, ...);
15004# define VdbeComment(X) sqlite3VdbeComment X
15005SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
15006# define VdbeNoopComment(X) sqlite3VdbeNoopComment X
15007# ifdef SQLITE_ENABLE_MODULE_COMMENTS
15008# define VdbeModuleComment(X) sqlite3VdbeNoopComment X
15009# else
15010# define VdbeModuleComment(X)
15011# endif
15012#else
15013# define VdbeComment(X)
15014# define VdbeNoopComment(X)
15015# define VdbeModuleComment(X)
15016#endif
15017
15018/*
15019** The VdbeCoverage macros are used to set a coverage testing point
15020** for VDBE branch instructions. The coverage testing points are line
15021** numbers in the sqlite3.c source file. VDBE branch coverage testing
15022** only works with an amalagmation build. That's ok since a VDBE branch
15023** coverage build designed for testing the test suite only. No application
15024** should ever ship with VDBE branch coverage measuring turned on.
15025**
15026** VdbeCoverage(v) // Mark the previously coded instruction
15027** // as a branch
15028**
15029** VdbeCoverageIf(v, conditional) // Mark previous if conditional true
15030**
15031** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken
15032**
15033** VdbeCoverageNeverTaken(v) // Previous branch is never taken
15034**
15035** VdbeCoverageNeverNull(v) // Previous three-way branch is only
15036** // taken on the first two ways. The
15037** // NULL option is not possible
15038**
15039** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested
15040** // in distingishing equal and not-equal.
15041**
15042** Every VDBE branch operation must be tagged with one of the macros above.
15043** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
15044** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
15045** routine in vdbe.c, alerting the developer to the missed tag.
15046**
15047** During testing, the test application will invoke
15048** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
15049** routine that is invoked as each bytecode branch is taken. The callback
15050** contains the sqlite3.c source line number ov the VdbeCoverage macro and
15051** flags to indicate whether or not the branch was taken. The test application
15052** is responsible for keeping track of this and reporting byte-code branches
15053** that are never taken.
15054**
15055** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
15056** vdbe.c source file for additional information.
15057*/
15058#ifdef SQLITE_VDBE_COVERAGE
15059SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe*,int);
15060# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
15061# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
15062# define VdbeCoverageAlwaysTaken(v) \
15063 sqlite3VdbeSetLineNumber(v,__LINE__|0x5000000);
15064# define VdbeCoverageNeverTaken(v) \
15065 sqlite3VdbeSetLineNumber(v,__LINE__|0x6000000);
15066# define VdbeCoverageNeverNull(v) \
15067 sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
15068# define VdbeCoverageNeverNullIf(v,x) \
15069 if(x)sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
15070# define VdbeCoverageEqNe(v) \
15071 sqlite3VdbeSetLineNumber(v,__LINE__|0x8000000);
15072# define VDBE_OFFSET_LINENO(x) (__LINE__+x)
15073#else
15074# define VdbeCoverage(v)
15075# define VdbeCoverageIf(v,x)
15076# define VdbeCoverageAlwaysTaken(v)
15077# define VdbeCoverageNeverTaken(v)
15078# define VdbeCoverageNeverNull(v)
15079# define VdbeCoverageNeverNullIf(v,x)
15080# define VdbeCoverageEqNe(v)
15081# define VDBE_OFFSET_LINENO(x) 0
15082#endif
15083
15084#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
15085SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
15086#else
15087# define sqlite3VdbeScanStatus(a,b,c,d,e)
15088#endif
15089
15090#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
15091SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
15092#endif
15093
15094#endif /* SQLITE_VDBE_H */
15095
15096/************** End of vdbe.h ************************************************/
15097/************** Continuing where we left off in sqliteInt.h ******************/
15098/************** Include pager.h in the middle of sqliteInt.h *****************/
15099/************** Begin file pager.h *******************************************/
15100/*
15101** 2001 September 15
15102**
15103** The author disclaims copyright to this source code. In place of
15104** a legal notice, here is a blessing:
15105**
15106** May you do good and not evil.
15107** May you find forgiveness for yourself and forgive others.
15108** May you share freely, never taking more than you give.
15109**
15110*************************************************************************
15111** This header file defines the interface that the sqlite page cache
15112** subsystem. The page cache subsystem reads and writes a file a page
15113** at a time and provides a journal for rollback.
15114*/
15115
15116#ifndef SQLITE_PAGER_H
15117#define SQLITE_PAGER_H
15118
15119/*
15120** Default maximum size for persistent journal files. A negative
15121** value means no limit. This value may be overridden using the
15122** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
15123*/
15124#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
15125 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
15126#endif
15127
15128/*
15129** The type used to represent a page number. The first page in a file
15130** is called page 1. 0 is used to represent "not a page".
15131*/
15132typedef u32 Pgno;
15133
15134/*
15135** Each open file is managed by a separate instance of the "Pager" structure.
15136*/
15137typedef struct Pager Pager;
15138
15139/*
15140** Handle type for pages.
15141*/
15142typedef struct PgHdr DbPage;
15143
15144/*
15145** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
15146** reserved for working around a windows/posix incompatibility). It is
15147** used in the journal to signify that the remainder of the journal file
15148** is devoted to storing a master journal name - there are no more pages to
15149** roll back. See comments for function writeMasterJournal() in pager.c
15150** for details.
15151*/
15152#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
15153
15154/*
15155** Allowed values for the flags parameter to sqlite3PagerOpen().
15156**
15157** NOTE: These values must match the corresponding BTREE_ values in btree.h.
15158*/
15159#define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
15160#define PAGER_MEMORY 0x0002 /* In-memory database */
15161
15162/*
15163** Valid values for the second argument to sqlite3PagerLockingMode().
15164*/
15165#define PAGER_LOCKINGMODE_QUERY -1
15166#define PAGER_LOCKINGMODE_NORMAL 0
15167#define PAGER_LOCKINGMODE_EXCLUSIVE 1
15168
15169/*
15170** Numeric constants that encode the journalmode.
15171**
15172** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY)
15173** are exposed in the API via the "PRAGMA journal_mode" command and
15174** therefore cannot be changed without a compatibility break.
15175*/
15176#define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
15177#define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */
15178#define PAGER_JOURNALMODE_PERSIST 1 /* Commit by zeroing journal header */
15179#define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
15180#define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
15181#define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
15182#define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
15183
15184/*
15185** Flags that make up the mask passed to sqlite3PagerGet().
15186*/
15187#define PAGER_GET_NOCONTENT 0x01 /* Do not load data from disk */
15188#define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
15189
15190/*
15191** Flags for sqlite3PagerSetFlags()
15192**
15193** Value constraints (enforced via assert()):
15194** PAGER_FULLFSYNC == SQLITE_FullFSync
15195** PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync
15196** PAGER_CACHE_SPILL == SQLITE_CacheSpill
15197*/
15198#define PAGER_SYNCHRONOUS_OFF 0x01 /* PRAGMA synchronous=OFF */
15199#define PAGER_SYNCHRONOUS_NORMAL 0x02 /* PRAGMA synchronous=NORMAL */
15200#define PAGER_SYNCHRONOUS_FULL 0x03 /* PRAGMA synchronous=FULL */
15201#define PAGER_SYNCHRONOUS_EXTRA 0x04 /* PRAGMA synchronous=EXTRA */
15202#define PAGER_SYNCHRONOUS_MASK 0x07 /* Mask for four values above */
15203#define PAGER_FULLFSYNC 0x08 /* PRAGMA fullfsync=ON */
15204#define PAGER_CKPT_FULLFSYNC 0x10 /* PRAGMA checkpoint_fullfsync=ON */
15205#define PAGER_CACHESPILL 0x20 /* PRAGMA cache_spill=ON */
15206#define PAGER_FLAGS_MASK 0x38 /* All above except SYNCHRONOUS */
15207
15208/*
15209** The remainder of this file contains the declarations of the functions
15210** that make up the Pager sub-system API. See source code comments for
15211** a detailed description of each routine.
15212*/
15213
15214/* Open and close a Pager connection. */
15215SQLITE_PRIVATE int sqlite3PagerOpen(
15216 sqlite3_vfs*,
15217 Pager **ppPager,
15218 const char*,
15219 int,
15220 int,
15221 int,
15222 void(*)(DbPage*)
15223);
15224SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
15225SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
15226
15227/* Functions used to configure a Pager object. */
15228SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
15229SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
15230#ifdef SQLITE_HAS_CODEC
15231SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager*,Pager*);
15232#endif
15233SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
15234SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
15235SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int);
15236SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
15237SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
15238SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned);
15239SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
15240SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
15241SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
15242SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
15243SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
15244SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
15245SQLITE_PRIVATE int sqlite3PagerFlush(Pager*);
15246
15247/* Functions used to obtain and release page references. */
15248SQLITE_PRIVATE int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
15249SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
15250SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
15251SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
15252SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
15253SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage*);
15254
15255/* Operations on page references. */
15256SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
15257SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
15258SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
15259SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
15260SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
15261SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
15262
15263/* Functions used to manage pager transactions and savepoints. */
15264SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
15265SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
15266SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
15267SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
15268SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster);
15269SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
15270SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
15271SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
15272SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
15273SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
15274
15275#ifndef SQLITE_OMIT_WAL
15276SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
15277SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
15278SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
15279SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
15280SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
15281# ifdef SQLITE_DIRECT_OVERFLOW_READ
15282SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno);
15283# endif
15284# ifdef SQLITE_ENABLE_SNAPSHOT
15285SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
15286SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
15287SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager);
15288SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
15289SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager);
15290# endif
15291#else
15292# define sqlite3PagerUseWal(x,y) 0
15293#endif
15294
15295#ifdef SQLITE_ENABLE_ZIPVFS
15296SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager);
15297#endif
15298
15299/* Functions used to query pager state and configuration. */
15300SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
15301SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*);
15302#ifdef SQLITE_DEBUG
15303SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
15304#endif
15305SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
15306SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int);
15307SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager*);
15308SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
15309SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
15310SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
15311SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
15312SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
15313SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
15314SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
15315SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
15316#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
15317SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager);
15318#else
15319# define sqlite3PagerResetLockTimeout(X)
15320#endif
15321
15322/* Functions used to truncate the database file. */
15323SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
15324
15325SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
15326
15327#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
15328SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
15329#endif
15330
15331/* Functions to support testing and debugging. */
15332#if !defined(NDEBUG) || defined(SQLITE_TEST)
15333SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
15334SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
15335#endif
15336#ifdef SQLITE_TEST
15337SQLITE_PRIVATE int *sqlite3PagerStats(Pager*);
15338SQLITE_PRIVATE void sqlite3PagerRefdump(Pager*);
15339 void disable_simulated_io_errors(void);
15340 void enable_simulated_io_errors(void);
15341#else
15342# define disable_simulated_io_errors()
15343# define enable_simulated_io_errors()
15344#endif
15345
15346#endif /* SQLITE_PAGER_H */
15347
15348/************** End of pager.h ***********************************************/
15349/************** Continuing where we left off in sqliteInt.h ******************/
15350/************** Include pcache.h in the middle of sqliteInt.h ****************/
15351/************** Begin file pcache.h ******************************************/
15352/*
15353** 2008 August 05
15354**
15355** The author disclaims copyright to this source code. In place of
15356** a legal notice, here is a blessing:
15357**
15358** May you do good and not evil.
15359** May you find forgiveness for yourself and forgive others.
15360** May you share freely, never taking more than you give.
15361**
15362*************************************************************************
15363** This header file defines the interface that the sqlite page cache
15364** subsystem.
15365*/
15366
15367#ifndef _PCACHE_H_
15368
15369typedef struct PgHdr PgHdr;
15370typedef struct PCache PCache;
15371
15372/*
15373** Every page in the cache is controlled by an instance of the following
15374** structure.
15375*/
15376struct PgHdr {
15377 sqlite3_pcache_page *pPage; /* Pcache object page handle */
15378 void *pData; /* Page data */
15379 void *pExtra; /* Extra content */
15380 PCache *pCache; /* PRIVATE: Cache that owns this page */
15381 PgHdr *pDirty; /* Transient list of dirty sorted by pgno */
15382 Pager *pPager; /* The pager this page is part of */
15383 Pgno pgno; /* Page number for this page */
15384#ifdef SQLITE_CHECK_PAGES
15385 u32 pageHash; /* Hash of page content */
15386#endif
15387 u16 flags; /* PGHDR flags defined below */
15388
15389 /**********************************************************************
15390 ** Elements above, except pCache, are public. All that follow are
15391 ** private to pcache.c and should not be accessed by other modules.
15392 ** pCache is grouped with the public elements for efficiency.
15393 */
15394 i16 nRef; /* Number of users of this page */
15395 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
15396 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
15397 /* NB: pDirtyNext and pDirtyPrev are undefined if the
15398 ** PgHdr object is not dirty */
15399};
15400
15401/* Bit values for PgHdr.flags */
15402#define PGHDR_CLEAN 0x001 /* Page not on the PCache.pDirty list */
15403#define PGHDR_DIRTY 0x002 /* Page is on the PCache.pDirty list */
15404#define PGHDR_WRITEABLE 0x004 /* Journaled and ready to modify */
15405#define PGHDR_NEED_SYNC 0x008 /* Fsync the rollback journal before
15406 ** writing this page to the database */
15407#define PGHDR_DONT_WRITE 0x010 /* Do not write content to disk */
15408#define PGHDR_MMAP 0x020 /* This is an mmap page object */
15409
15410#define PGHDR_WAL_APPEND 0x040 /* Appended to wal file */
15411
15412/* Initialize and shutdown the page cache subsystem */
15413SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
15414SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
15415
15416/* Page cache buffer management:
15417** These routines implement SQLITE_CONFIG_PAGECACHE.
15418*/
15419SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
15420
15421/* Create a new pager cache.
15422** Under memory stress, invoke xStress to try to make pages clean.
15423** Only clean and unpinned pages can be reclaimed.
15424*/
15425SQLITE_PRIVATE int sqlite3PcacheOpen(
15426 int szPage, /* Size of every page */
15427 int szExtra, /* Extra space associated with each page */
15428 int bPurgeable, /* True if pages are on backing store */
15429 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
15430 void *pStress, /* Argument to xStress */
15431 PCache *pToInit /* Preallocated space for the PCache */
15432);
15433
15434/* Modify the page-size after the cache has been created. */
15435SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *, int);
15436
15437/* Return the size in bytes of a PCache object. Used to preallocate
15438** storage space.
15439*/
15440SQLITE_PRIVATE int sqlite3PcacheSize(void);
15441
15442/* One release per successful fetch. Page is pinned until released.
15443** Reference counted.
15444*/
15445SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
15446SQLITE_PRIVATE int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
15447SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
15448SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
15449
15450SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
15451SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
15452SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
15453SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
15454SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache*);
15455
15456/* Change a page number. Used by incr-vacuum. */
15457SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
15458
15459/* Remove all pages with pgno>x. Reset the cache if x==0 */
15460SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
15461
15462/* Get a list of all dirty pages in the cache, sorted by page number */
15463SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
15464
15465/* Reset and close the cache object */
15466SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
15467
15468/* Clear flags from pages of the page cache */
15469SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
15470
15471/* Discard the contents of the cache */
15472SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
15473
15474/* Return the total number of outstanding page references */
15475SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
15476
15477/* Increment the reference count of an existing page */
15478SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
15479
15480SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
15481
15482/* Return the total number of pages stored in the cache */
15483SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
15484
15485#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
15486/* Iterate through all dirty pages currently stored in the cache. This
15487** interface is only available if SQLITE_CHECK_PAGES is defined when the
15488** library is built.
15489*/
15490SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
15491#endif
15492
15493#if defined(SQLITE_DEBUG)
15494/* Check invariants on a PgHdr object */
15495SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr*);
15496#endif
15497
15498/* Set and get the suggested cache-size for the specified pager-cache.
15499**
15500** If no global maximum is configured, then the system attempts to limit
15501** the total number of pages cached by purgeable pager-caches to the sum
15502** of the suggested cache-sizes.
15503*/
15504SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
15505#ifdef SQLITE_TEST
15506SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
15507#endif
15508
15509/* Set or get the suggested spill-size for the specified pager-cache.
15510**
15511** The spill-size is the minimum number of pages in cache before the cache
15512** will attempt to spill dirty pages by calling xStress.
15513*/
15514SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *, int);
15515
15516/* Free up as much memory as possible from the page cache */
15517SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
15518
15519#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
15520/* Try to return memory used by the pcache module to the main memory heap */
15521SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
15522#endif
15523
15524#ifdef SQLITE_TEST
15525SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
15526#endif
15527
15528SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
15529
15530/* Return the header size */
15531SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
15532SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
15533
15534/* Number of dirty pages as a percentage of the configured cache size */
15535SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
15536
15537#endif /* _PCACHE_H_ */
15538
15539/************** End of pcache.h **********************************************/
15540/************** Continuing where we left off in sqliteInt.h ******************/
15541/************** Include os.h in the middle of sqliteInt.h ********************/
15542/************** Begin file os.h **********************************************/
15543/*
15544** 2001 September 16
15545**
15546** The author disclaims copyright to this source code. In place of
15547** a legal notice, here is a blessing:
15548**
15549** May you do good and not evil.
15550** May you find forgiveness for yourself and forgive others.
15551** May you share freely, never taking more than you give.
15552**
15553******************************************************************************
15554**
15555** This header file (together with is companion C source-code file
15556** "os.c") attempt to abstract the underlying operating system so that
15557** the SQLite library will work on both POSIX and windows systems.
15558**
15559** This header file is #include-ed by sqliteInt.h and thus ends up
15560** being included by every source file.
15561*/
15562#ifndef _SQLITE_OS_H_
15563#define _SQLITE_OS_H_
15564
15565/*
15566** Attempt to automatically detect the operating system and setup the
15567** necessary pre-processor macros for it.
15568*/
15569/************** Include os_setup.h in the middle of os.h *********************/
15570/************** Begin file os_setup.h ****************************************/
15571/*
15572** 2013 November 25
15573**
15574** The author disclaims copyright to this source code. In place of
15575** a legal notice, here is a blessing:
15576**
15577** May you do good and not evil.
15578** May you find forgiveness for yourself and forgive others.
15579** May you share freely, never taking more than you give.
15580**
15581******************************************************************************
15582**
15583** This file contains pre-processor directives related to operating system
15584** detection and/or setup.
15585*/
15586#ifndef SQLITE_OS_SETUP_H
15587#define SQLITE_OS_SETUP_H
15588
15589/*
15590** Figure out if we are dealing with Unix, Windows, or some other operating
15591** system.
15592**
15593** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
15594** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0. One of
15595** the three will be 1. The other two will be 0.
15596*/
15597#if defined(SQLITE_OS_OTHER)
15598# if SQLITE_OS_OTHER==1
15599# undef SQLITE_OS_UNIX
15600# define SQLITE_OS_UNIX 0
15601# undef SQLITE_OS_WIN
15602# define SQLITE_OS_WIN 0
15603# else
15604# undef SQLITE_OS_OTHER
15605# endif
15606#endif
15607#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
15608# define SQLITE_OS_OTHER 0
15609# ifndef SQLITE_OS_WIN
15610# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
15611 defined(__MINGW32__) || defined(__BORLANDC__)
15612# define SQLITE_OS_WIN 1
15613# define SQLITE_OS_UNIX 0
15614# else
15615# define SQLITE_OS_WIN 0
15616# define SQLITE_OS_UNIX 1
15617# endif
15618# else
15619# define SQLITE_OS_UNIX 0
15620# endif
15621#else
15622# ifndef SQLITE_OS_WIN
15623# define SQLITE_OS_WIN 0
15624# endif
15625#endif
15626
15627#endif /* SQLITE_OS_SETUP_H */
15628
15629/************** End of os_setup.h ********************************************/
15630/************** Continuing where we left off in os.h *************************/
15631
15632/* If the SET_FULLSYNC macro is not defined above, then make it
15633** a no-op
15634*/
15635#ifndef SET_FULLSYNC
15636# define SET_FULLSYNC(x,y)
15637#endif
15638
15639/*
15640** The default size of a disk sector
15641*/
15642#ifndef SQLITE_DEFAULT_SECTOR_SIZE
15643# define SQLITE_DEFAULT_SECTOR_SIZE 4096
15644#endif
15645
15646/*
15647** Temporary files are named starting with this prefix followed by 16 random
15648** alphanumeric characters, and no file extension. They are stored in the
15649** OS's standard temporary file directory, and are deleted prior to exit.
15650** If sqlite is being embedded in another program, you may wish to change the
15651** prefix to reflect your program's name, so that if your program exits
15652** prematurely, old temporary files can be easily identified. This can be done
15653** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
15654**
15655** 2006-10-31: The default prefix used to be "sqlite_". But then
15656** Mcafee started using SQLite in their anti-virus product and it
15657** started putting files with the "sqlite" name in the c:/temp folder.
15658** This annoyed many windows users. Those users would then do a
15659** Google search for "sqlite", find the telephone numbers of the
15660** developers and call to wake them up at night and complain.
15661** For this reason, the default name prefix is changed to be "sqlite"
15662** spelled backwards. So the temp files are still identified, but
15663** anybody smart enough to figure out the code is also likely smart
15664** enough to know that calling the developer will not help get rid
15665** of the file.
15666*/
15667#ifndef SQLITE_TEMP_FILE_PREFIX
15668# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
15669#endif
15670
15671/*
15672** The following values may be passed as the second argument to
15673** sqlite3OsLock(). The various locks exhibit the following semantics:
15674**
15675** SHARED: Any number of processes may hold a SHARED lock simultaneously.
15676** RESERVED: A single process may hold a RESERVED lock on a file at
15677** any time. Other processes may hold and obtain new SHARED locks.
15678** PENDING: A single process may hold a PENDING lock on a file at
15679** any one time. Existing SHARED locks may persist, but no new
15680** SHARED locks may be obtained by other processes.
15681** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
15682**
15683** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
15684** process that requests an EXCLUSIVE lock may actually obtain a PENDING
15685** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
15686** sqlite3OsLock().
15687*/
15688#define NO_LOCK 0
15689#define SHARED_LOCK 1
15690#define RESERVED_LOCK 2
15691#define PENDING_LOCK 3
15692#define EXCLUSIVE_LOCK 4
15693
15694/*
15695** File Locking Notes: (Mostly about windows but also some info for Unix)
15696**
15697** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
15698** those functions are not available. So we use only LockFile() and
15699** UnlockFile().
15700**
15701** LockFile() prevents not just writing but also reading by other processes.
15702** A SHARED_LOCK is obtained by locking a single randomly-chosen
15703** byte out of a specific range of bytes. The lock byte is obtained at
15704** random so two separate readers can probably access the file at the
15705** same time, unless they are unlucky and choose the same lock byte.
15706** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
15707** There can only be one writer. A RESERVED_LOCK is obtained by locking
15708** a single byte of the file that is designated as the reserved lock byte.
15709** A PENDING_LOCK is obtained by locking a designated byte different from
15710** the RESERVED_LOCK byte.
15711**
15712** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
15713** which means we can use reader/writer locks. When reader/writer locks
15714** are used, the lock is placed on the same range of bytes that is used
15715** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
15716** will support two or more Win95 readers or two or more WinNT readers.
15717** But a single Win95 reader will lock out all WinNT readers and a single
15718** WinNT reader will lock out all other Win95 readers.
15719**
15720** The following #defines specify the range of bytes used for locking.
15721** SHARED_SIZE is the number of bytes available in the pool from which
15722** a random byte is selected for a shared lock. The pool of bytes for
15723** shared locks begins at SHARED_FIRST.
15724**
15725** The same locking strategy and
15726** byte ranges are used for Unix. This leaves open the possibility of having
15727** clients on win95, winNT, and unix all talking to the same shared file
15728** and all locking correctly. To do so would require that samba (or whatever
15729** tool is being used for file sharing) implements locks correctly between
15730** windows and unix. I'm guessing that isn't likely to happen, but by
15731** using the same locking range we are at least open to the possibility.
15732**
15733** Locking in windows is manditory. For this reason, we cannot store
15734** actual data in the bytes used for locking. The pager never allocates
15735** the pages involved in locking therefore. SHARED_SIZE is selected so
15736** that all locks will fit on a single page even at the minimum page size.
15737** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
15738** is set high so that we don't have to allocate an unused page except
15739** for very large databases. But one should test the page skipping logic
15740** by setting PENDING_BYTE low and running the entire regression suite.
15741**
15742** Changing the value of PENDING_BYTE results in a subtly incompatible
15743** file format. Depending on how it is changed, you might not notice
15744** the incompatibility right away, even running a full regression test.
15745** The default location of PENDING_BYTE is the first byte past the
15746** 1GB boundary.
15747**
15748*/
15749#ifdef SQLITE_OMIT_WSD
15750# define PENDING_BYTE (0x40000000)
15751#else
15752# define PENDING_BYTE sqlite3PendingByte
15753#endif
15754#define RESERVED_BYTE (PENDING_BYTE+1)
15755#define SHARED_FIRST (PENDING_BYTE+2)
15756#define SHARED_SIZE 510
15757
15758/*
15759** Wrapper around OS specific sqlite3_os_init() function.
15760*/
15761SQLITE_PRIVATE int sqlite3OsInit(void);
15762
15763/*
15764** Functions for accessing sqlite3_file methods
15765*/
15766SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*);
15767SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
15768SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
15769SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
15770SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
15771SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
15772SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
15773SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
15774SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
15775SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
15776SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
15777#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
15778SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
15779SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
15780#ifndef SQLITE_OMIT_WAL
15781SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
15782SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
15783SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
15784SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
15785#endif /* SQLITE_OMIT_WAL */
15786SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
15787SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
15788
15789
15790/*
15791** Functions for accessing sqlite3_vfs methods
15792*/
15793SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
15794SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
15795SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
15796SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
15797#ifndef SQLITE_OMIT_LOAD_EXTENSION
15798SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
15799SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
15800SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
15801SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
15802#endif /* SQLITE_OMIT_LOAD_EXTENSION */
15803SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
15804SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
15805SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
15806SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
15807
15808/*
15809** Convenience functions for opening and closing files using
15810** sqlite3_malloc() to obtain space for the file-handle structure.
15811*/
15812SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
15813SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *);
15814
15815#endif /* _SQLITE_OS_H_ */
15816
15817/************** End of os.h **************************************************/
15818/************** Continuing where we left off in sqliteInt.h ******************/
15819/************** Include mutex.h in the middle of sqliteInt.h *****************/
15820/************** Begin file mutex.h *******************************************/
15821/*
15822** 2007 August 28
15823**
15824** The author disclaims copyright to this source code. In place of
15825** a legal notice, here is a blessing:
15826**
15827** May you do good and not evil.
15828** May you find forgiveness for yourself and forgive others.
15829** May you share freely, never taking more than you give.
15830**
15831*************************************************************************
15832**
15833** This file contains the common header for all mutex implementations.
15834** The sqliteInt.h header #includes this file so that it is available
15835** to all source files. We break it out in an effort to keep the code
15836** better organized.
15837**
15838** NOTE: source files should *not* #include this header file directly.
15839** Source files should #include the sqliteInt.h file and let that file
15840** include this one indirectly.
15841*/
15842
15843
15844/*
15845** Figure out what version of the code to use. The choices are
15846**
15847** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The
15848** mutexes implementation cannot be overridden
15849** at start-time.
15850**
15851** SQLITE_MUTEX_NOOP For single-threaded applications. No
15852** mutual exclusion is provided. But this
15853** implementation can be overridden at
15854** start-time.
15855**
15856** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
15857**
15858** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
15859*/
15860#if !SQLITE_THREADSAFE
15861# define SQLITE_MUTEX_OMIT
15862#endif
15863#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
15864# if SQLITE_OS_UNIX
15865# define SQLITE_MUTEX_PTHREADS
15866# elif SQLITE_OS_WIN
15867# define SQLITE_MUTEX_W32
15868# else
15869# define SQLITE_MUTEX_NOOP
15870# endif
15871#endif
15872
15873#ifdef SQLITE_MUTEX_OMIT
15874/*
15875** If this is a no-op implementation, implement everything as macros.
15876*/
15877#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
15878#define sqlite3_mutex_free(X)
15879#define sqlite3_mutex_enter(X)
15880#define sqlite3_mutex_try(X) SQLITE_OK
15881#define sqlite3_mutex_leave(X)
15882#define sqlite3_mutex_held(X) ((void)(X),1)
15883#define sqlite3_mutex_notheld(X) ((void)(X),1)
15884#define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
15885#define sqlite3MutexInit() SQLITE_OK
15886#define sqlite3MutexEnd()
15887#define MUTEX_LOGIC(X)
15888#else
15889#define MUTEX_LOGIC(X) X
15890#endif /* defined(SQLITE_MUTEX_OMIT) */
15891
15892/************** End of mutex.h ***********************************************/
15893/************** Continuing where we left off in sqliteInt.h ******************/
15894
15895/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
15896** synchronous setting to EXTRA. It is no longer supported.
15897*/
15898#ifdef SQLITE_EXTRA_DURABLE
15899# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE
15900# define SQLITE_DEFAULT_SYNCHRONOUS 3
15901#endif
15902
15903/*
15904** Default synchronous levels.
15905**
15906** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ
15907** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
15908**
15909** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS
15910** OFF 1 0
15911** NORMAL 2 1
15912** FULL 3 2
15913** EXTRA 4 3
15914**
15915** The "PRAGMA synchronous" statement also uses the zero-based numbers.
15916** In other words, the zero-based numbers are used for all external interfaces
15917** and the one-based values are used internally.
15918*/
15919#ifndef SQLITE_DEFAULT_SYNCHRONOUS
15920# define SQLITE_DEFAULT_SYNCHRONOUS 2
15921#endif
15922#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
15923# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
15924#endif
15925
15926/*
15927** Each database file to be accessed by the system is an instance
15928** of the following structure. There are normally two of these structures
15929** in the sqlite.aDb[] array. aDb[0] is the main database file and
15930** aDb[1] is the database file used to hold temporary tables. Additional
15931** databases may be attached.
15932*/
15933struct Db {
15934 char *zDbSName; /* Name of this database. (schema name, not filename) */
15935 Btree *pBt; /* The B*Tree structure for this database file */
15936 u8 safety_level; /* How aggressive at syncing data to disk */
15937 u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */
15938 Schema *pSchema; /* Pointer to database schema (possibly shared) */
15939};
15940
15941/*
15942** An instance of the following structure stores a database schema.
15943**
15944** Most Schema objects are associated with a Btree. The exception is
15945** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
15946** In shared cache mode, a single Schema object can be shared by multiple
15947** Btrees that refer to the same underlying BtShared object.
15948**
15949** Schema objects are automatically deallocated when the last Btree that
15950** references them is destroyed. The TEMP Schema is manually freed by
15951** sqlite3_close().
15952*
15953** A thread must be holding a mutex on the corresponding Btree in order
15954** to access Schema content. This implies that the thread must also be
15955** holding a mutex on the sqlite3 connection pointer that owns the Btree.
15956** For a TEMP Schema, only the connection mutex is required.
15957*/
15958struct Schema {
15959 int schema_cookie; /* Database schema version number for this file */
15960 int iGeneration; /* Generation counter. Incremented with each change */
15961 Hash tblHash; /* All tables indexed by name */
15962 Hash idxHash; /* All (named) indices indexed by name */
15963 Hash trigHash; /* All triggers indexed by name */
15964 Hash fkeyHash; /* All foreign keys by referenced table name */
15965 Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
15966 u8 file_format; /* Schema format version for this file */
15967 u8 enc; /* Text encoding used by this database */
15968 u16 schemaFlags; /* Flags associated with this schema */
15969 int cache_size; /* Number of pages to use in the cache */
15970};
15971
15972/*
15973** These macros can be used to test, set, or clear bits in the
15974** Db.pSchema->flags field.
15975*/
15976#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
15977#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
15978#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
15979#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
15980
15981/*
15982** Allowed values for the DB.pSchema->flags field.
15983**
15984** The DB_SchemaLoaded flag is set after the database schema has been
15985** read into internal hash tables.
15986**
15987** DB_UnresetViews means that one or more views have column names that
15988** have been filled out. If the schema changes, these column names might
15989** changes and so the view will need to be reset.
15990*/
15991#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
15992#define DB_UnresetViews 0x0002 /* Some views have defined column names */
15993#define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */
15994#define DB_ResetWanted 0x0008 /* Reset the schema when nSchemaLock==0 */
15995
15996/*
15997** The number of different kinds of things that can be limited
15998** using the sqlite3_limit() interface.
15999*/
16000#define SQLITE_N_LIMIT (SQLITE_LIMIT_WORKER_THREADS+1)
16001
16002/*
16003** Lookaside malloc is a set of fixed-size buffers that can be used
16004** to satisfy small transient memory allocation requests for objects
16005** associated with a particular database connection. The use of
16006** lookaside malloc provides a significant performance enhancement
16007** (approx 10%) by avoiding numerous malloc/free requests while parsing
16008** SQL statements.
16009**
16010** The Lookaside structure holds configuration information about the
16011** lookaside malloc subsystem. Each available memory allocation in
16012** the lookaside subsystem is stored on a linked list of LookasideSlot
16013** objects.
16014**
16015** Lookaside allocations are only allowed for objects that are associated
16016** with a particular database connection. Hence, schema information cannot
16017** be stored in lookaside because in shared cache mode the schema information
16018** is shared by multiple database connections. Therefore, while parsing
16019** schema information, the Lookaside.bEnabled flag is cleared so that
16020** lookaside allocations are not used to construct the schema objects.
16021*/
16022struct Lookaside {
16023 u32 bDisable; /* Only operate the lookaside when zero */
16024 u16 sz; /* Size of each buffer in bytes */
16025 u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
16026 u32 nSlot; /* Number of lookaside slots allocated */
16027 u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
16028 LookasideSlot *pInit; /* List of buffers not previously used */
16029 LookasideSlot *pFree; /* List of available buffers */
16030 void *pStart; /* First byte of available memory space */
16031 void *pEnd; /* First byte past end of available space */
16032};
16033struct LookasideSlot {
16034 LookasideSlot *pNext; /* Next buffer in the list of free buffers */
16035};
16036
16037/*
16038** A hash table for built-in function definitions. (Application-defined
16039** functions use a regular table table from hash.h.)
16040**
16041** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
16042** Collisions are on the FuncDef.u.pHash chain.
16043*/
16044#define SQLITE_FUNC_HASH_SZ 23
16045struct FuncDefHash {
16046 FuncDef *a[SQLITE_FUNC_HASH_SZ]; /* Hash table for functions */
16047};
16048
16049#ifdef SQLITE_USER_AUTHENTICATION
16050/*
16051** Information held in the "sqlite3" database connection object and used
16052** to manage user authentication.
16053*/
16054typedef struct sqlite3_userauth sqlite3_userauth;
16055struct sqlite3_userauth {
16056 u8 authLevel; /* Current authentication level */
16057 int nAuthPW; /* Size of the zAuthPW in bytes */
16058 char *zAuthPW; /* Password used to authenticate */
16059 char *zAuthUser; /* User name used to authenticate */
16060};
16061
16062/* Allowed values for sqlite3_userauth.authLevel */
16063#define UAUTH_Unknown 0 /* Authentication not yet checked */
16064#define UAUTH_Fail 1 /* User authentication failed */
16065#define UAUTH_User 2 /* Authenticated as a normal user */
16066#define UAUTH_Admin 3 /* Authenticated as an administrator */
16067
16068/* Functions used only by user authorization logic */
16069SQLITE_PRIVATE int sqlite3UserAuthTable(const char*);
16070SQLITE_PRIVATE int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
16071SQLITE_PRIVATE void sqlite3UserAuthInit(sqlite3*);
16072SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
16073
16074#endif /* SQLITE_USER_AUTHENTICATION */
16075
16076/*
16077** typedef for the authorization callback function.
16078*/
16079#ifdef SQLITE_USER_AUTHENTICATION
16080 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16081 const char*, const char*);
16082#else
16083 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16084 const char*);
16085#endif
16086
16087#ifndef SQLITE_OMIT_DEPRECATED
16088/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
16089** in the style of sqlite3_trace()
16090*/
16091#define SQLITE_TRACE_LEGACY 0x80
16092#else
16093#define SQLITE_TRACE_LEGACY 0
16094#endif /* SQLITE_OMIT_DEPRECATED */
16095
16096
16097/*
16098** Each database connection is an instance of the following structure.
16099*/
16100struct sqlite3 {
16101 sqlite3_vfs *pVfs; /* OS Interface */
16102 struct Vdbe *pVdbe; /* List of active virtual machines */
16103 CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
16104 sqlite3_mutex *mutex; /* Connection mutex */
16105 Db *aDb; /* All backends */
16106 int nDb; /* Number of backends currently in use */
16107 u32 mDbFlags; /* flags recording internal state */
16108 u32 flags; /* flags settable by pragmas. See below */
16109 i64 lastRowid; /* ROWID of most recent insert (see above) */
16110 i64 szMmap; /* Default mmap_size setting */
16111 u32 nSchemaLock; /* Do not reset the schema when non-zero */
16112 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
16113 int errCode; /* Most recent error code (SQLITE_*) */
16114 int errMask; /* & result codes with this before returning */
16115 int iSysErrno; /* Errno value from last system error */
16116 u16 dbOptFlags; /* Flags to enable/disable optimizations */
16117 u8 enc; /* Text encoding */
16118 u8 autoCommit; /* The auto-commit flag. */
16119 u8 temp_store; /* 1: file 2: memory 0: default */
16120 u8 mallocFailed; /* True if we have seen a malloc failure */
16121 u8 bBenignMalloc; /* Do not require OOMs if true */
16122 u8 dfltLockMode; /* Default locking-mode for attached dbs */
16123 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
16124 u8 suppressErr; /* Do not issue error messages if true */
16125 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
16126 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
16127 u8 mTrace; /* zero or more SQLITE_TRACE flags */
16128 u8 noSharedCache; /* True if no shared-cache backends */
16129 u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */
16130 int nextPagesize; /* Pagesize after VACUUM if >0 */
16131 u32 magic; /* Magic number for detect library misuse */
16132 int nChange; /* Value returned by sqlite3_changes() */
16133 int nTotalChange; /* Value returned by sqlite3_total_changes() */
16134 int aLimit[SQLITE_N_LIMIT]; /* Limits */
16135 int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
16136 struct sqlite3InitInfo { /* Information used during initialization */
16137 int newTnum; /* Rootpage of table being initialized */
16138 u8 iDb; /* Which db file is being initialized */
16139 u8 busy; /* TRUE if currently initializing */
16140 unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
16141 unsigned imposterTable : 1; /* Building an imposter table */
16142 unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
16143 } init;
16144 int nVdbeActive; /* Number of VDBEs currently running */
16145 int nVdbeRead; /* Number of active VDBEs that read or write */
16146 int nVdbeWrite; /* Number of active VDBEs that read and write */
16147 int nVdbeExec; /* Number of nested calls to VdbeExec() */
16148 int nVDestroy; /* Number of active OP_VDestroy operations */
16149 int nExtension; /* Number of loaded extensions */
16150 void **aExtension; /* Array of shared library handles */
16151 int (*xTrace)(u32,void*,void*,void*); /* Trace function */
16152 void *pTraceArg; /* Argument to the trace function */
16153 void (*xProfile)(void*,const char*,u64); /* Profiling function */
16154 void *pProfileArg; /* Argument to profile function */
16155 void *pCommitArg; /* Argument to xCommitCallback() */
16156 int (*xCommitCallback)(void*); /* Invoked at every commit. */
16157 void *pRollbackArg; /* Argument to xRollbackCallback() */
16158 void (*xRollbackCallback)(void*); /* Invoked at every commit. */
16159 void *pUpdateArg;
16160 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
16161#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
16162 void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
16163 void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */
16164 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
16165 );
16166 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
16167#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
16168#ifndef SQLITE_OMIT_WAL
16169 int (*xWalCallback)(void *, sqlite3 *, const char *, int);
16170 void *pWalArg;
16171#endif
16172 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
16173 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
16174 void *pCollNeededArg;
16175 sqlite3_value *pErr; /* Most recent error message */
16176 union {
16177 volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
16178 double notUsed1; /* Spacer */
16179 } u1;
16180 Lookaside lookaside; /* Lookaside malloc configuration */
16181#ifndef SQLITE_OMIT_AUTHORIZATION
16182 sqlite3_xauth xAuth; /* Access authorization function */
16183 void *pAuthArg; /* 1st argument to the access auth function */
16184#endif
16185#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
16186 int (*xProgress)(void *); /* The progress callback */
16187 void *pProgressArg; /* Argument to the progress callback */
16188 unsigned nProgressOps; /* Number of opcodes for progress callback */
16189#endif
16190#ifndef SQLITE_OMIT_VIRTUALTABLE
16191 int nVTrans; /* Allocated size of aVTrans */
16192 Hash aModule; /* populated by sqlite3_create_module() */
16193 VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
16194 VTable **aVTrans; /* Virtual tables with open transactions */
16195 VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
16196#endif
16197 Hash aFunc; /* Hash table of connection functions */
16198 Hash aCollSeq; /* All collating sequences */
16199 BusyHandler busyHandler; /* Busy callback */
16200 Db aDbStatic[2]; /* Static space for the 2 default backends */
16201 Savepoint *pSavepoint; /* List of active savepoints */
16202 int busyTimeout; /* Busy handler timeout, in msec */
16203 int nSavepoint; /* Number of non-transaction savepoints */
16204 int nStatement; /* Number of nested statement-transactions */
16205 i64 nDeferredCons; /* Net deferred constraints this transaction. */
16206 i64 nDeferredImmCons; /* Net deferred immediate constraints */
16207 int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
16208#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
16209 /* The following variables are all protected by the STATIC_MASTER
16210 ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
16211 **
16212 ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
16213 ** unlock so that it can proceed.
16214 **
16215 ** When X.pBlockingConnection==Y, that means that something that X tried
16216 ** tried to do recently failed with an SQLITE_LOCKED error due to locks
16217 ** held by Y.
16218 */
16219 sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
16220 sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
16221 void *pUnlockArg; /* Argument to xUnlockNotify */
16222 void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
16223 sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
16224#endif
16225#ifdef SQLITE_USER_AUTHENTICATION
16226 sqlite3_userauth auth; /* User authentication information */
16227#endif
16228};
16229
16230/*
16231** A macro to discover the encoding of a database.
16232*/
16233#define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
16234#define ENC(db) ((db)->enc)
16235
16236/*
16237** Possible values for the sqlite3.flags.
16238**
16239** Value constraints (enforced via assert()):
16240** SQLITE_FullFSync == PAGER_FULLFSYNC
16241** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
16242** SQLITE_CacheSpill == PAGER_CACHE_SPILL
16243*/
16244#define SQLITE_WriteSchema 0x00000001 /* OK to update SQLITE_MASTER */
16245#define SQLITE_LegacyFileFmt 0x00000002 /* Create new databases in format 1 */
16246#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
16247#define SQLITE_FullFSync 0x00000008 /* Use full fsync on the backend */
16248#define SQLITE_CkptFullFSync 0x00000010 /* Use full fsync for checkpoint */
16249#define SQLITE_CacheSpill 0x00000020 /* OK to spill pager cache */
16250#define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
16251#define SQLITE_CountRows 0x00000080 /* Count rows changed by INSERT, */
16252 /* DELETE, or UPDATE and return */
16253 /* the count using a callback. */
16254#define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
16255 /* result set is empty */
16256#define SQLITE_IgnoreChecks 0x00000200 /* Do not enforce check constraints */
16257#define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */
16258#define SQLITE_NoCkptOnClose 0x00000800 /* No checkpoint on close()/DETACH */
16259#define SQLITE_ReverseOrder 0x00001000 /* Reverse unordered SELECTs */
16260#define SQLITE_RecTriggers 0x00002000 /* Enable recursive triggers */
16261#define SQLITE_ForeignKeys 0x00004000 /* Enforce foreign key constraints */
16262#define SQLITE_AutoIndex 0x00008000 /* Enable automatic indexes */
16263#define SQLITE_LoadExtension 0x00010000 /* Enable load_extension */
16264#define SQLITE_LoadExtFunc 0x00020000 /* Enable load_extension() SQL func */
16265#define SQLITE_EnableTrigger 0x00040000 /* True to enable triggers */
16266#define SQLITE_DeferFKs 0x00080000 /* Defer all FK constraints */
16267#define SQLITE_QueryOnly 0x00100000 /* Disable database changes */
16268#define SQLITE_CellSizeCk 0x00200000 /* Check btree cell sizes on load */
16269#define SQLITE_Fts3Tokenizer 0x00400000 /* Enable fts3_tokenizer(2) */
16270#define SQLITE_EnableQPSG 0x00800000 /* Query Planner Stability Guarantee*/
16271#define SQLITE_TriggerEQP 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
16272#define SQLITE_ResetDatabase 0x02000000 /* Reset the database */
16273#define SQLITE_LegacyAlter 0x04000000 /* Legacy ALTER TABLE behaviour */
16274
16275/* Flags used only if debugging */
16276#ifdef SQLITE_DEBUG
16277#define SQLITE_SqlTrace 0x08000000 /* Debug print SQL as it executes */
16278#define SQLITE_VdbeListing 0x10000000 /* Debug listings of VDBE programs */
16279#define SQLITE_VdbeTrace 0x20000000 /* True to trace VDBE execution */
16280#define SQLITE_VdbeAddopTrace 0x40000000 /* Trace sqlite3VdbeAddOp() calls */
16281#define SQLITE_VdbeEQP 0x80000000 /* Debug EXPLAIN QUERY PLAN */
16282#endif
16283
16284/*
16285** Allowed values for sqlite3.mDbFlags
16286*/
16287#define DBFLAG_SchemaChange 0x0001 /* Uncommitted Hash table changes */
16288#define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
16289#define DBFLAG_Vacuum 0x0004 /* Currently in a VACUUM */
16290#define DBFLAG_SchemaKnownOk 0x0008 /* Schema is known to be valid */
16291
16292/*
16293** Bits of the sqlite3.dbOptFlags field that are used by the
16294** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
16295** selectively disable various optimizations.
16296*/
16297#define SQLITE_QueryFlattener 0x0001 /* Query flattening */
16298 /* 0x0002 available for reuse */
16299#define SQLITE_GroupByOrder 0x0004 /* GROUPBY cover of ORDERBY */
16300#define SQLITE_FactorOutConst 0x0008 /* Constant factoring */
16301#define SQLITE_DistinctOpt 0x0010 /* DISTINCT using indexes */
16302#define SQLITE_CoverIdxScan 0x0020 /* Covering index scans */
16303#define SQLITE_OrderByIdxJoin 0x0040 /* ORDER BY of joins via index */
16304#define SQLITE_Transitive 0x0080 /* Transitive constraints */
16305#define SQLITE_OmitNoopJoin 0x0100 /* Omit unused tables in joins */
16306#define SQLITE_CountOfView 0x0200 /* The count-of-view optimization */
16307#define SQLITE_CursorHints 0x0400 /* Add OP_CursorHint opcodes */
16308#define SQLITE_Stat34 0x0800 /* Use STAT3 or STAT4 data */
16309 /* TH3 expects the Stat34 ^^^^^^ value to be 0x0800. Don't change it */
16310#define SQLITE_PushDown 0x1000 /* The push-down optimization */
16311#define SQLITE_SimplifyJoin 0x2000 /* Convert LEFT JOIN to JOIN */
16312#define SQLITE_SkipScan 0x4000 /* Skip-scans */
16313#define SQLITE_PropagateConst 0x8000 /* The constant propagation opt */
16314#define SQLITE_AllOpts 0xffff /* All optimizations */
16315
16316/*
16317** Macros for testing whether or not optimizations are enabled or disabled.
16318*/
16319#define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
16320#define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
16321
16322/*
16323** Return true if it OK to factor constant expressions into the initialization
16324** code. The argument is a Parse object for the code generator.
16325*/
16326#define ConstFactorOk(P) ((P)->okConstFactor)
16327
16328/*
16329** Possible values for the sqlite.magic field.
16330** The numbers are obtained at random and have no special meaning, other
16331** than being distinct from one another.
16332*/
16333#define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */
16334#define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */
16335#define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */
16336#define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */
16337#define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */
16338#define SQLITE_MAGIC_ZOMBIE 0x64cffc7f /* Close with last statement close */
16339
16340/*
16341** Each SQL function is defined by an instance of the following
16342** structure. For global built-in functions (ex: substr(), max(), count())
16343** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
16344** For per-connection application-defined functions, a pointer to this
16345** structure is held in the db->aHash hash table.
16346**
16347** The u.pHash field is used by the global built-ins. The u.pDestructor
16348** field is used by per-connection app-def functions.
16349*/
16350struct FuncDef {
16351 i8 nArg; /* Number of arguments. -1 means unlimited */
16352 u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
16353 void *pUserData; /* User data parameter */
16354 FuncDef *pNext; /* Next function with same name */
16355 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
16356 void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
16357 void (*xValue)(sqlite3_context*); /* Current agg value */
16358 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
16359 const char *zName; /* SQL name of the function. */
16360 union {
16361 FuncDef *pHash; /* Next with a different name but the same hash */
16362 FuncDestructor *pDestructor; /* Reference counted destructor function */
16363 } u;
16364};
16365
16366/*
16367** This structure encapsulates a user-function destructor callback (as
16368** configured using create_function_v2()) and a reference counter. When
16369** create_function_v2() is called to create a function with a destructor,
16370** a single object of this type is allocated. FuncDestructor.nRef is set to
16371** the number of FuncDef objects created (either 1 or 3, depending on whether
16372** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
16373** member of each of the new FuncDef objects is set to point to the allocated
16374** FuncDestructor.
16375**
16376** Thereafter, when one of the FuncDef objects is deleted, the reference
16377** count on this object is decremented. When it reaches 0, the destructor
16378** is invoked and the FuncDestructor structure freed.
16379*/
16380struct FuncDestructor {
16381 int nRef;
16382 void (*xDestroy)(void *);
16383 void *pUserData;
16384};
16385
16386/*
16387** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF
16388** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And
16389** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There
16390** are assert() statements in the code to verify this.
16391**
16392** Value constraints (enforced via assert()):
16393** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg
16394** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG
16395** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG
16396** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API
16397** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API
16398*/
16399#define SQLITE_FUNC_ENCMASK 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
16400#define SQLITE_FUNC_LIKE 0x0004 /* Candidate for the LIKE optimization */
16401#define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
16402#define SQLITE_FUNC_EPHEM 0x0010 /* Ephemeral. Delete with VDBE */
16403#define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
16404#define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
16405#define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
16406#define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
16407#define SQLITE_FUNC_COALESCE 0x0200 /* Built-in coalesce() or ifnull() */
16408#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
16409#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
16410#define SQLITE_FUNC_MINMAX 0x1000 /* True for min() and max() aggregates */
16411#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
16412 ** single query - might change over time */
16413#define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
16414#define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */
16415#define SQLITE_FUNC_WINDOW 0x10000 /* Built-in window-only function */
16416#define SQLITE_FUNC_WINDOW_SIZE 0x20000 /* Requires partition size as arg. */
16417
16418/*
16419** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
16420** used to create the initializers for the FuncDef structures.
16421**
16422** FUNCTION(zName, nArg, iArg, bNC, xFunc)
16423** Used to create a scalar function definition of a function zName
16424** implemented by C function xFunc that accepts nArg arguments. The
16425** value passed as iArg is cast to a (void*) and made available
16426** as the user-data (sqlite3_user_data()) for the function. If
16427** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
16428**
16429** VFUNCTION(zName, nArg, iArg, bNC, xFunc)
16430** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
16431**
16432** DFUNCTION(zName, nArg, iArg, bNC, xFunc)
16433** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
16434** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions
16435** and functions like sqlite_version() that can change, but not during
16436** a single query. The iArg is ignored. The user-data is always set
16437** to a NULL pointer. The bNC parameter is not used.
16438**
16439** PURE_DATE(zName, nArg, iArg, bNC, xFunc)
16440** Used for "pure" date/time functions, this macro is like DFUNCTION
16441** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is
16442** ignored and the user-data for these functions is set to an
16443** arbitrary non-NULL pointer. The bNC parameter is not used.
16444**
16445** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
16446** Used to create an aggregate function definition implemented by
16447** the C functions xStep and xFinal. The first four parameters
16448** are interpreted in the same way as the first 4 parameters to
16449** FUNCTION().
16450**
16451** WFUNCTION(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
16452** Used to create an aggregate function definition implemented by
16453** the C functions xStep and xFinal. The first four parameters
16454** are interpreted in the same way as the first 4 parameters to
16455** FUNCTION().
16456**
16457** LIKEFUNC(zName, nArg, pArg, flags)
16458** Used to create a scalar function definition of a function zName
16459** that accepts nArg arguments and is implemented by a call to C
16460** function likeFunc. Argument pArg is cast to a (void *) and made
16461** available as the function user-data (sqlite3_user_data()). The
16462** FuncDef.flags variable is set to the value passed as the flags
16463** parameter.
16464*/
16465#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
16466 {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
16467 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
16468#define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
16469 {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
16470 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
16471#define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \
16472 {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8, \
16473 0, 0, xFunc, 0, 0, 0, #zName, {0} }
16474#define PURE_DATE(zName, nArg, iArg, bNC, xFunc) \
16475 {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
16476 (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
16477#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
16478 {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
16479 SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
16480#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
16481 {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
16482 pArg, 0, xFunc, 0, 0, 0, #zName, }
16483#define LIKEFUNC(zName, nArg, arg, flags) \
16484 {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
16485 (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
16486#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue) \
16487 {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
16488 SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,0,#zName, {0}}
16489#define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \
16490 {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
16491 SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xFinal,0,#zName, {0}}
16492
16493#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \
16494 {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \
16495 SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
16496
16497/*
16498** All current savepoints are stored in a linked list starting at
16499** sqlite3.pSavepoint. The first element in the list is the most recently
16500** opened savepoint. Savepoints are added to the list by the vdbe
16501** OP_Savepoint instruction.
16502*/
16503struct Savepoint {
16504 char *zName; /* Savepoint name (nul-terminated) */
16505 i64 nDeferredCons; /* Number of deferred fk violations */
16506 i64 nDeferredImmCons; /* Number of deferred imm fk. */
16507 Savepoint *pNext; /* Parent savepoint (if any) */
16508};
16509
16510/*
16511** The following are used as the second parameter to sqlite3Savepoint(),
16512** and as the P1 argument to the OP_Savepoint instruction.
16513*/
16514#define SAVEPOINT_BEGIN 0
16515#define SAVEPOINT_RELEASE 1
16516#define SAVEPOINT_ROLLBACK 2
16517
16518
16519/*
16520** Each SQLite module (virtual table definition) is defined by an
16521** instance of the following structure, stored in the sqlite3.aModule
16522** hash table.
16523*/
16524struct Module {
16525 const sqlite3_module *pModule; /* Callback pointers */
16526 const char *zName; /* Name passed to create_module() */
16527 void *pAux; /* pAux passed to create_module() */
16528 void (*xDestroy)(void *); /* Module destructor function */
16529 Table *pEpoTab; /* Eponymous table for this module */
16530};
16531
16532/*
16533** information about each column of an SQL table is held in an instance
16534** of this structure.
16535*/
16536struct Column {
16537 char *zName; /* Name of this column, \000, then the type */
16538 Expr *pDflt; /* Default value of this column */
16539 char *zColl; /* Collating sequence. If NULL, use the default */
16540 u8 notNull; /* An OE_ code for handling a NOT NULL constraint */
16541 char affinity; /* One of the SQLITE_AFF_... values */
16542 u8 szEst; /* Estimated size of value in this column. sizeof(INT)==1 */
16543 u8 colFlags; /* Boolean properties. See COLFLAG_ defines below */
16544};
16545
16546/* Allowed values for Column.colFlags:
16547*/
16548#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
16549#define COLFLAG_HIDDEN 0x0002 /* A hidden column in a virtual table */
16550#define COLFLAG_HASTYPE 0x0004 /* Type name follows column name */
16551#define COLFLAG_UNIQUE 0x0008 /* Column def contains "UNIQUE" or "PK" */
16552#define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
16553
16554/*
16555** A "Collating Sequence" is defined by an instance of the following
16556** structure. Conceptually, a collating sequence consists of a name and
16557** a comparison routine that defines the order of that sequence.
16558**
16559** If CollSeq.xCmp is NULL, it means that the
16560** collating sequence is undefined. Indices built on an undefined
16561** collating sequence may not be read or written.
16562*/
16563struct CollSeq {
16564 char *zName; /* Name of the collating sequence, UTF-8 encoded */
16565 u8 enc; /* Text encoding handled by xCmp() */
16566 void *pUser; /* First argument to xCmp() */
16567 int (*xCmp)(void*,int, const void*, int, const void*);
16568 void (*xDel)(void*); /* Destructor for pUser */
16569};
16570
16571/*
16572** A sort order can be either ASC or DESC.
16573*/
16574#define SQLITE_SO_ASC 0 /* Sort in ascending order */
16575#define SQLITE_SO_DESC 1 /* Sort in ascending order */
16576#define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
16577
16578/*
16579** Column affinity types.
16580**
16581** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
16582** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
16583** the speed a little by numbering the values consecutively.
16584**
16585** But rather than start with 0 or 1, we begin with 'A'. That way,
16586** when multiple affinity types are concatenated into a string and
16587** used as the P4 operand, they will be more readable.
16588**
16589** Note also that the numeric types are grouped together so that testing
16590** for a numeric type is a single comparison. And the BLOB type is first.
16591*/
16592#define SQLITE_AFF_BLOB 'A'
16593#define SQLITE_AFF_TEXT 'B'
16594#define SQLITE_AFF_NUMERIC 'C'
16595#define SQLITE_AFF_INTEGER 'D'
16596#define SQLITE_AFF_REAL 'E'
16597
16598#define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC)
16599
16600/*
16601** The SQLITE_AFF_MASK values masks off the significant bits of an
16602** affinity value.
16603*/
16604#define SQLITE_AFF_MASK 0x47
16605
16606/*
16607** Additional bit values that can be ORed with an affinity without
16608** changing the affinity.
16609**
16610** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
16611** It causes an assert() to fire if either operand to a comparison
16612** operator is NULL. It is added to certain comparison operators to
16613** prove that the operands are always NOT NULL.
16614*/
16615#define SQLITE_KEEPNULL 0x08 /* Used by vector == or <> */
16616#define SQLITE_JUMPIFNULL 0x10 /* jumps if either operand is NULL */
16617#define SQLITE_STOREP2 0x20 /* Store result in reg[P2] rather than jump */
16618#define SQLITE_NULLEQ 0x80 /* NULL=NULL */
16619#define SQLITE_NOTNULL 0x90 /* Assert that operands are never NULL */
16620
16621/*
16622** An object of this type is created for each virtual table present in
16623** the database schema.
16624**
16625** If the database schema is shared, then there is one instance of this
16626** structure for each database connection (sqlite3*) that uses the shared
16627** schema. This is because each database connection requires its own unique
16628** instance of the sqlite3_vtab* handle used to access the virtual table
16629** implementation. sqlite3_vtab* handles can not be shared between
16630** database connections, even when the rest of the in-memory database
16631** schema is shared, as the implementation often stores the database
16632** connection handle passed to it via the xConnect() or xCreate() method
16633** during initialization internally. This database connection handle may
16634** then be used by the virtual table implementation to access real tables
16635** within the database. So that they appear as part of the callers
16636** transaction, these accesses need to be made via the same database
16637** connection as that used to execute SQL operations on the virtual table.
16638**
16639** All VTable objects that correspond to a single table in a shared
16640** database schema are initially stored in a linked-list pointed to by
16641** the Table.pVTable member variable of the corresponding Table object.
16642** When an sqlite3_prepare() operation is required to access the virtual
16643** table, it searches the list for the VTable that corresponds to the
16644** database connection doing the preparing so as to use the correct
16645** sqlite3_vtab* handle in the compiled query.
16646**
16647** When an in-memory Table object is deleted (for example when the
16648** schema is being reloaded for some reason), the VTable objects are not
16649** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
16650** immediately. Instead, they are moved from the Table.pVTable list to
16651** another linked list headed by the sqlite3.pDisconnect member of the
16652** corresponding sqlite3 structure. They are then deleted/xDisconnected
16653** next time a statement is prepared using said sqlite3*. This is done
16654** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
16655** Refer to comments above function sqlite3VtabUnlockList() for an
16656** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
16657** list without holding the corresponding sqlite3.mutex mutex.
16658**
16659** The memory for objects of this type is always allocated by
16660** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
16661** the first argument.
16662*/
16663struct VTable {
16664 sqlite3 *db; /* Database connection associated with this table */
16665 Module *pMod; /* Pointer to module implementation */
16666 sqlite3_vtab *pVtab; /* Pointer to vtab instance */
16667 int nRef; /* Number of pointers to this structure */
16668 u8 bConstraint; /* True if constraints are supported */
16669 int iSavepoint; /* Depth of the SAVEPOINT stack */
16670 VTable *pNext; /* Next in linked list (see above) */
16671};
16672
16673/*
16674** The schema for each SQL table and view is represented in memory
16675** by an instance of the following structure.
16676*/
16677struct Table {
16678 char *zName; /* Name of the table or view */
16679 Column *aCol; /* Information about each column */
16680 Index *pIndex; /* List of SQL indexes on this table. */
16681 Select *pSelect; /* NULL for tables. Points to definition if a view. */
16682 FKey *pFKey; /* Linked list of all foreign keys in this table */
16683 char *zColAff; /* String defining the affinity of each column */
16684 ExprList *pCheck; /* All CHECK constraints */
16685 /* ... also used as column name list in a VIEW */
16686 int tnum; /* Root BTree page for this table */
16687 u32 nTabRef; /* Number of pointers to this Table */
16688 u32 tabFlags; /* Mask of TF_* values */
16689 i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */
16690 i16 nCol; /* Number of columns in this table */
16691 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
16692 LogEst szTabRow; /* Estimated size of each table row in bytes */
16693#ifdef SQLITE_ENABLE_COSTMULT
16694 LogEst costMult; /* Cost multiplier for using this table */
16695#endif
16696 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
16697#ifndef SQLITE_OMIT_ALTERTABLE
16698 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
16699#endif
16700#ifndef SQLITE_OMIT_VIRTUALTABLE
16701 int nModuleArg; /* Number of arguments to the module */
16702 char **azModuleArg; /* 0: module 1: schema 2: vtab name 3...: args */
16703 VTable *pVTable; /* List of VTable objects. */
16704#endif
16705 Trigger *pTrigger; /* List of triggers stored in pSchema */
16706 Schema *pSchema; /* Schema that contains this table */
16707 Table *pNextZombie; /* Next on the Parse.pZombieTab list */
16708};
16709
16710/*
16711** Allowed values for Table.tabFlags.
16712**
16713** TF_OOOHidden applies to tables or view that have hidden columns that are
16714** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
16715** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
16716** the TF_OOOHidden attribute would apply in this case. Such tables require
16717** special handling during INSERT processing.
16718*/
16719#define TF_Readonly 0x0001 /* Read-only system table */
16720#define TF_Ephemeral 0x0002 /* An ephemeral table */
16721#define TF_HasPrimaryKey 0x0004 /* Table has a primary key */
16722#define TF_Autoincrement 0x0008 /* Integer primary key is autoincrement */
16723#define TF_HasStat1 0x0010 /* nRowLogEst set from sqlite_stat1 */
16724#define TF_WithoutRowid 0x0020 /* No rowid. PRIMARY KEY is the key */
16725#define TF_NoVisibleRowid 0x0040 /* No user-visible "rowid" column */
16726#define TF_OOOHidden 0x0080 /* Out-of-Order hidden columns */
16727#define TF_StatsUsed 0x0100 /* Query planner decisions affected by
16728 ** Index.aiRowLogEst[] values */
16729#define TF_HasNotNull 0x0200 /* Contains NOT NULL constraints */
16730
16731/*
16732** Test to see whether or not a table is a virtual table. This is
16733** done as a macro so that it will be optimized out when virtual
16734** table support is omitted from the build.
16735*/
16736#ifndef SQLITE_OMIT_VIRTUALTABLE
16737# define IsVirtual(X) ((X)->nModuleArg)
16738#else
16739# define IsVirtual(X) 0
16740#endif
16741
16742/*
16743** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn()
16744** only works for non-virtual tables (ordinary tables and views) and is
16745** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The
16746** IsHiddenColumn() macro is general purpose.
16747*/
16748#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
16749# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
16750# define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
16751#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
16752# define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
16753# define IsOrdinaryHiddenColumn(X) 0
16754#else
16755# define IsHiddenColumn(X) 0
16756# define IsOrdinaryHiddenColumn(X) 0
16757#endif
16758
16759
16760/* Does the table have a rowid */
16761#define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
16762#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
16763
16764/*
16765** Each foreign key constraint is an instance of the following structure.
16766**
16767** A foreign key is associated with two tables. The "from" table is
16768** the table that contains the REFERENCES clause that creates the foreign
16769** key. The "to" table is the table that is named in the REFERENCES clause.
16770** Consider this example:
16771**
16772** CREATE TABLE ex1(
16773** a INTEGER PRIMARY KEY,
16774** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
16775** );
16776**
16777** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
16778** Equivalent names:
16779**
16780** from-table == child-table
16781** to-table == parent-table
16782**
16783** Each REFERENCES clause generates an instance of the following structure
16784** which is attached to the from-table. The to-table need not exist when
16785** the from-table is created. The existence of the to-table is not checked.
16786**
16787** The list of all parents for child Table X is held at X.pFKey.
16788**
16789** A list of all children for a table named Z (which might not even exist)
16790** is held in Schema.fkeyHash with a hash key of Z.
16791*/
16792struct FKey {
16793 Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
16794 FKey *pNextFrom; /* Next FKey with the same in pFrom. Next parent of pFrom */
16795 char *zTo; /* Name of table that the key points to (aka: Parent) */
16796 FKey *pNextTo; /* Next with the same zTo. Next child of zTo. */
16797 FKey *pPrevTo; /* Previous with the same zTo */
16798 int nCol; /* Number of columns in this key */
16799 /* EV: R-30323-21917 */
16800 u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
16801 u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
16802 Trigger *apTrigger[2];/* Triggers for aAction[] actions */
16803 struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
16804 int iFrom; /* Index of column in pFrom */
16805 char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */
16806 } aCol[1]; /* One entry for each of nCol columns */
16807};
16808
16809/*
16810** SQLite supports many different ways to resolve a constraint
16811** error. ROLLBACK processing means that a constraint violation
16812** causes the operation in process to fail and for the current transaction
16813** to be rolled back. ABORT processing means the operation in process
16814** fails and any prior changes from that one operation are backed out,
16815** but the transaction is not rolled back. FAIL processing means that
16816** the operation in progress stops and returns an error code. But prior
16817** changes due to the same operation are not backed out and no rollback
16818** occurs. IGNORE means that the particular row that caused the constraint
16819** error is not inserted or updated. Processing continues and no error
16820** is returned. REPLACE means that preexisting database rows that caused
16821** a UNIQUE constraint violation are removed so that the new insert or
16822** update can proceed. Processing continues and no error is reported.
16823**
16824** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
16825** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
16826** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
16827** key is set to NULL. CASCADE means that a DELETE or UPDATE of the
16828** referenced table row is propagated into the row that holds the
16829** foreign key.
16830**
16831** The following symbolic values are used to record which type
16832** of action to take.
16833*/
16834#define OE_None 0 /* There is no constraint to check */
16835#define OE_Rollback 1 /* Fail the operation and rollback the transaction */
16836#define OE_Abort 2 /* Back out changes but do no rollback transaction */
16837#define OE_Fail 3 /* Stop the operation but leave all prior changes */
16838#define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */
16839#define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
16840#define OE_Update 6 /* Process as a DO UPDATE in an upsert */
16841#define OE_Restrict 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
16842#define OE_SetNull 8 /* Set the foreign key value to NULL */
16843#define OE_SetDflt 9 /* Set the foreign key value to its default */
16844#define OE_Cascade 10 /* Cascade the changes */
16845#define OE_Default 11 /* Do whatever the default action is */
16846
16847
16848/*
16849** An instance of the following structure is passed as the first
16850** argument to sqlite3VdbeKeyCompare and is used to control the
16851** comparison of the two index keys.
16852**
16853** Note that aSortOrder[] and aColl[] have nField+1 slots. There
16854** are nField slots for the columns of an index then one extra slot
16855** for the rowid at the end.
16856*/
16857struct KeyInfo {
16858 u32 nRef; /* Number of references to this KeyInfo object */
16859 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
16860 u16 nKeyField; /* Number of key columns in the index */
16861 u16 nAllField; /* Total columns, including key plus others */
16862 sqlite3 *db; /* The database connection */
16863 u8 *aSortOrder; /* Sort order for each column. */
16864 CollSeq *aColl[1]; /* Collating sequence for each term of the key */
16865};
16866
16867/*
16868** This object holds a record which has been parsed out into individual
16869** fields, for the purposes of doing a comparison.
16870**
16871** A record is an object that contains one or more fields of data.
16872** Records are used to store the content of a table row and to store
16873** the key of an index. A blob encoding of a record is created by
16874** the OP_MakeRecord opcode of the VDBE and is disassembled by the
16875** OP_Column opcode.
16876**
16877** An instance of this object serves as a "key" for doing a search on
16878** an index b+tree. The goal of the search is to find the entry that
16879** is closed to the key described by this object. This object might hold
16880** just a prefix of the key. The number of fields is given by
16881** pKeyInfo->nField.
16882**
16883** The r1 and r2 fields are the values to return if this key is less than
16884** or greater than a key in the btree, respectively. These are normally
16885** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
16886** is in DESC order.
16887**
16888** The key comparison functions actually return default_rc when they find
16889** an equals comparison. default_rc can be -1, 0, or +1. If there are
16890** multiple entries in the b-tree with the same key (when only looking
16891** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
16892** cause the search to find the last match, or +1 to cause the search to
16893** find the first match.
16894**
16895** The key comparison functions will set eqSeen to true if they ever
16896** get and equal results when comparing this structure to a b-tree record.
16897** When default_rc!=0, the search might end up on the record immediately
16898** before the first match or immediately after the last match. The
16899** eqSeen field will indicate whether or not an exact match exists in the
16900** b-tree.
16901*/
16902struct UnpackedRecord {
16903 KeyInfo *pKeyInfo; /* Collation and sort-order information */
16904 Mem *aMem; /* Values */
16905 u16 nField; /* Number of entries in apMem[] */
16906 i8 default_rc; /* Comparison result if keys are equal */
16907 u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
16908 i8 r1; /* Value to return if (lhs < rhs) */
16909 i8 r2; /* Value to return if (lhs > rhs) */
16910 u8 eqSeen; /* True if an equality comparison has been seen */
16911};
16912
16913
16914/*
16915** Each SQL index is represented in memory by an
16916** instance of the following structure.
16917**
16918** The columns of the table that are to be indexed are described
16919** by the aiColumn[] field of this structure. For example, suppose
16920** we have the following table and index:
16921**
16922** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
16923** CREATE INDEX Ex2 ON Ex1(c3,c1);
16924**
16925** In the Table structure describing Ex1, nCol==3 because there are
16926** three columns in the table. In the Index structure describing
16927** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
16928** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
16929** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
16930** The second column to be indexed (c1) has an index of 0 in
16931** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
16932**
16933** The Index.onError field determines whether or not the indexed columns
16934** must be unique and what to do if they are not. When Index.onError=OE_None,
16935** it means this is not a unique index. Otherwise it is a unique index
16936** and the value of Index.onError indicate the which conflict resolution
16937** algorithm to employ whenever an attempt is made to insert a non-unique
16938** element.
16939**
16940** While parsing a CREATE TABLE or CREATE INDEX statement in order to
16941** generate VDBE code (as opposed to parsing one read from an sqlite_master
16942** table as part of parsing an existing database schema), transient instances
16943** of this structure may be created. In this case the Index.tnum variable is
16944** used to store the address of a VDBE instruction, not a database page
16945** number (it cannot - the database page is not allocated until the VDBE
16946** program is executed). See convertToWithoutRowidTable() for details.
16947*/
16948struct Index {
16949 char *zName; /* Name of this index */
16950 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
16951 LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
16952 Table *pTable; /* The SQL table being indexed */
16953 char *zColAff; /* String defining the affinity of each column */
16954 Index *pNext; /* The next index associated with the same table */
16955 Schema *pSchema; /* Schema containing this index */
16956 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
16957 const char **azColl; /* Array of collation sequence names for index */
16958 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
16959 ExprList *aColExpr; /* Column expressions */
16960 int tnum; /* DB Page containing root of this index */
16961 LogEst szIdxRow; /* Estimated average row size in bytes */
16962 u16 nKeyCol; /* Number of columns forming the key */
16963 u16 nColumn; /* Number of columns stored in the index */
16964 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
16965 unsigned idxType:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
16966 unsigned bUnordered:1; /* Use this index for == or IN queries only */
16967 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
16968 unsigned isResized:1; /* True if resizeIndexObject() has been called */
16969 unsigned isCovering:1; /* True if this is a covering index */
16970 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
16971 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
16972 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
16973#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
16974 int nSample; /* Number of elements in aSample[] */
16975 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
16976 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
16977 IndexSample *aSample; /* Samples of the left-most key */
16978 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
16979 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
16980#endif
16981 Bitmask colNotIdxed; /* 0 for unindexed columns in pTab */
16982};
16983
16984/*
16985** Allowed values for Index.idxType
16986*/
16987#define SQLITE_IDXTYPE_APPDEF 0 /* Created using CREATE INDEX */
16988#define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */
16989#define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */
16990
16991/* Return true if index X is a PRIMARY KEY index */
16992#define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
16993
16994/* Return true if index X is a UNIQUE index */
16995#define IsUniqueIndex(X) ((X)->onError!=OE_None)
16996
16997/* The Index.aiColumn[] values are normally positive integer. But
16998** there are some negative values that have special meaning:
16999*/
17000#define XN_ROWID (-1) /* Indexed column is the rowid */
17001#define XN_EXPR (-2) /* Indexed column is an expression */
17002
17003/*
17004** Each sample stored in the sqlite_stat3 table is represented in memory
17005** using a structure of this type. See documentation at the top of the
17006** analyze.c source file for additional information.
17007*/
17008struct IndexSample {
17009 void *p; /* Pointer to sampled record */
17010 int n; /* Size of record in bytes */
17011 tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
17012 tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
17013 tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
17014};
17015
17016/*
17017** Each token coming out of the lexer is an instance of
17018** this structure. Tokens are also used as part of an expression.
17019**
17020** The memory that "z" points to is owned by other objects. Take care
17021** that the owner of the "z" string does not deallocate the string before
17022** the Token goes out of scope! Very often, the "z" points to some place
17023** in the middle of the Parse.zSql text. But it might also point to a
17024** static string.
17025*/
17026struct Token {
17027 const char *z; /* Text of the token. Not NULL-terminated! */
17028 unsigned int n; /* Number of characters in this token */
17029};
17030
17031/*
17032** An instance of this structure contains information needed to generate
17033** code for a SELECT that contains aggregate functions.
17034**
17035** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
17036** pointer to this structure. The Expr.iColumn field is the index in
17037** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
17038** code for that node.
17039**
17040** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
17041** original Select structure that describes the SELECT statement. These
17042** fields do not need to be freed when deallocating the AggInfo structure.
17043*/
17044struct AggInfo {
17045 u8 directMode; /* Direct rendering mode means take data directly
17046 ** from source tables rather than from accumulators */
17047 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
17048 ** than the source table */
17049 int sortingIdx; /* Cursor number of the sorting index */
17050 int sortingIdxPTab; /* Cursor number of pseudo-table */
17051 int nSortingColumn; /* Number of columns in the sorting index */
17052 int mnReg, mxReg; /* Range of registers allocated for aCol and aFunc */
17053 ExprList *pGroupBy; /* The group by clause */
17054 struct AggInfo_col { /* For each column used in source tables */
17055 Table *pTab; /* Source table */
17056 int iTable; /* Cursor number of the source table */
17057 int iColumn; /* Column number within the source table */
17058 int iSorterColumn; /* Column number in the sorting index */
17059 int iMem; /* Memory location that acts as accumulator */
17060 Expr *pExpr; /* The original expression */
17061 } *aCol;
17062 int nColumn; /* Number of used entries in aCol[] */
17063 int nAccumulator; /* Number of columns that show through to the output.
17064 ** Additional columns are used only as parameters to
17065 ** aggregate functions */
17066 struct AggInfo_func { /* For each aggregate function */
17067 Expr *pExpr; /* Expression encoding the function */
17068 FuncDef *pFunc; /* The aggregate function implementation */
17069 int iMem; /* Memory location that acts as accumulator */
17070 int iDistinct; /* Ephemeral table used to enforce DISTINCT */
17071 } *aFunc;
17072 int nFunc; /* Number of entries in aFunc[] */
17073};
17074
17075/*
17076** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
17077** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
17078** than 32767 we have to make it 32-bit. 16-bit is preferred because
17079** it uses less memory in the Expr object, which is a big memory user
17080** in systems with lots of prepared statements. And few applications
17081** need more than about 10 or 20 variables. But some extreme users want
17082** to have prepared statements with over 32767 variables, and for them
17083** the option is available (at compile-time).
17084*/
17085#if SQLITE_MAX_VARIABLE_NUMBER<=32767
17086typedef i16 ynVar;
17087#else
17088typedef int ynVar;
17089#endif
17090
17091/*
17092** Each node of an expression in the parse tree is an instance
17093** of this structure.
17094**
17095** Expr.op is the opcode. The integer parser token codes are reused
17096** as opcodes here. For example, the parser defines TK_GE to be an integer
17097** code representing the ">=" operator. This same integer code is reused
17098** to represent the greater-than-or-equal-to operator in the expression
17099** tree.
17100**
17101** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
17102** or TK_STRING), then Expr.token contains the text of the SQL literal. If
17103** the expression is a variable (TK_VARIABLE), then Expr.token contains the
17104** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
17105** then Expr.token contains the name of the function.
17106**
17107** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
17108** binary operator. Either or both may be NULL.
17109**
17110** Expr.x.pList is a list of arguments if the expression is an SQL function,
17111** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
17112** Expr.x.pSelect is used if the expression is a sub-select or an expression of
17113** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
17114** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
17115** valid.
17116**
17117** An expression of the form ID or ID.ID refers to a column in a table.
17118** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
17119** the integer cursor number of a VDBE cursor pointing to that table and
17120** Expr.iColumn is the column number for the specific column. If the
17121** expression is used as a result in an aggregate SELECT, then the
17122** value is also stored in the Expr.iAgg column in the aggregate so that
17123** it can be accessed after all aggregates are computed.
17124**
17125** If the expression is an unbound variable marker (a question mark
17126** character '?' in the original SQL) then the Expr.iTable holds the index
17127** number for that variable.
17128**
17129** If the expression is a subquery then Expr.iColumn holds an integer
17130** register number containing the result of the subquery. If the
17131** subquery gives a constant result, then iTable is -1. If the subquery
17132** gives a different answer at different times during statement processing
17133** then iTable is the address of a subroutine that computes the subquery.
17134**
17135** If the Expr is of type OP_Column, and the table it is selecting from
17136** is a disk table or the "old.*" pseudo-table, then pTab points to the
17137** corresponding table definition.
17138**
17139** ALLOCATION NOTES:
17140**
17141** Expr objects can use a lot of memory space in database schema. To
17142** help reduce memory requirements, sometimes an Expr object will be
17143** truncated. And to reduce the number of memory allocations, sometimes
17144** two or more Expr objects will be stored in a single memory allocation,
17145** together with Expr.zToken strings.
17146**
17147** If the EP_Reduced and EP_TokenOnly flags are set when
17148** an Expr object is truncated. When EP_Reduced is set, then all
17149** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
17150** are contained within the same memory allocation. Note, however, that
17151** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
17152** allocated, regardless of whether or not EP_Reduced is set.
17153*/
17154struct Expr {
17155 u8 op; /* Operation performed by this node */
17156 char affinity; /* The affinity of the column or 0 if not a column */
17157 u32 flags; /* Various flags. EP_* See below */
17158 union {
17159 char *zToken; /* Token value. Zero terminated and dequoted */
17160 int iValue; /* Non-negative integer value if EP_IntValue */
17161 } u;
17162
17163 /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
17164 ** space is allocated for the fields below this point. An attempt to
17165 ** access them will result in a segfault or malfunction.
17166 *********************************************************************/
17167
17168 Expr *pLeft; /* Left subnode */
17169 Expr *pRight; /* Right subnode */
17170 union {
17171 ExprList *pList; /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
17172 Select *pSelect; /* EP_xIsSelect and op = IN, EXISTS, SELECT */
17173 } x;
17174
17175 /* If the EP_Reduced flag is set in the Expr.flags mask, then no
17176 ** space is allocated for the fields below this point. An attempt to
17177 ** access them will result in a segfault or malfunction.
17178 *********************************************************************/
17179
17180#if SQLITE_MAX_EXPR_DEPTH>0
17181 int nHeight; /* Height of the tree headed by this node */
17182#endif
17183 int iTable; /* TK_COLUMN: cursor number of table holding column
17184 ** TK_REGISTER: register number
17185 ** TK_TRIGGER: 1 -> new, 0 -> old
17186 ** EP_Unlikely: 134217728 times likelihood
17187 ** TK_SELECT: 1st register of result vector */
17188 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
17189 ** TK_VARIABLE: variable number (always >= 1).
17190 ** TK_SELECT_COLUMN: column of the result vector */
17191 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
17192 i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
17193 u8 op2; /* TK_REGISTER: original value of Expr.op
17194 ** TK_COLUMN: the value of p5 for OP_Column
17195 ** TK_AGG_FUNCTION: nesting depth */
17196 AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
17197 Table *pTab; /* Table for TK_COLUMN expressions. Can be NULL
17198 ** for a column of an index on an expression */
17199#ifndef SQLITE_OMIT_WINDOWFUNC
17200 Window *pWin; /* Window definition for window functions */
17201#endif
17202};
17203
17204/*
17205** The following are the meanings of bits in the Expr.flags field.
17206*/
17207#define EP_FromJoin 0x000001 /* Originates in ON/USING clause of outer join */
17208#define EP_Agg 0x000002 /* Contains one or more aggregate functions */
17209#define EP_HasFunc 0x000004 /* Contains one or more functions of any kind */
17210#define EP_FixedCol 0x000008 /* TK_Column with a known fixed value */
17211#define EP_Distinct 0x000010 /* Aggregate function with DISTINCT keyword */
17212#define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
17213#define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
17214#define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
17215#define EP_Collate 0x000100 /* Tree contains a TK_COLLATE operator */
17216#define EP_Generic 0x000200 /* Ignore COLLATE or affinity on this tree */
17217#define EP_IntValue 0x000400 /* Integer value contained in u.iValue */
17218#define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
17219#define EP_Skip 0x001000 /* COLLATE, AS, or UNLIKELY */
17220#define EP_Reduced 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
17221#define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
17222#define EP_Static 0x008000 /* Held in memory not obtained from malloc() */
17223#define EP_MemToken 0x010000 /* Need to sqlite3DbFree() Expr.zToken */
17224#define EP_NoReduce 0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
17225#define EP_Unlikely 0x040000 /* unlikely() or likelihood() function */
17226#define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
17227#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
17228#define EP_Subquery 0x200000 /* Tree contains a TK_SELECT operator */
17229#define EP_Alias 0x400000 /* Is an alias for a result set column */
17230#define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
17231
17232/*
17233** The EP_Propagate mask is a set of properties that automatically propagate
17234** upwards into parent nodes.
17235*/
17236#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
17237
17238/*
17239** These macros can be used to test, set, or clear bits in the
17240** Expr.flags field.
17241*/
17242#define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
17243#define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
17244#define ExprSetProperty(E,P) (E)->flags|=(P)
17245#define ExprClearProperty(E,P) (E)->flags&=~(P)
17246
17247/* The ExprSetVVAProperty() macro is used for Verification, Validation,
17248** and Accreditation only. It works like ExprSetProperty() during VVA
17249** processes but is a no-op for delivery.
17250*/
17251#ifdef SQLITE_DEBUG
17252# define ExprSetVVAProperty(E,P) (E)->flags|=(P)
17253#else
17254# define ExprSetVVAProperty(E,P)
17255#endif
17256
17257/*
17258** Macros to determine the number of bytes required by a normal Expr
17259** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
17260** and an Expr struct with the EP_TokenOnly flag set.
17261*/
17262#define EXPR_FULLSIZE sizeof(Expr) /* Full size */
17263#define EXPR_REDUCEDSIZE offsetof(Expr,iTable) /* Common features */
17264#define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */
17265
17266/*
17267** Flags passed to the sqlite3ExprDup() function. See the header comment
17268** above sqlite3ExprDup() for details.
17269*/
17270#define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
17271
17272/*
17273** A list of expressions. Each expression may optionally have a
17274** name. An expr/name combination can be used in several ways, such
17275** as the list of "expr AS ID" fields following a "SELECT" or in the
17276** list of "ID = expr" items in an UPDATE. A list of expressions can
17277** also be used as the argument to a function, in which case the a.zName
17278** field is not used.
17279**
17280** By default the Expr.zSpan field holds a human-readable description of
17281** the expression that is used in the generation of error messages and
17282** column labels. In this case, Expr.zSpan is typically the text of a
17283** column expression as it exists in a SELECT statement. However, if
17284** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
17285** of the result column in the form: DATABASE.TABLE.COLUMN. This later
17286** form is used for name resolution with nested FROM clauses.
17287*/
17288struct ExprList {
17289 int nExpr; /* Number of expressions on the list */
17290 struct ExprList_item { /* For each expression in the list */
17291 Expr *pExpr; /* The parse tree for this expression */
17292 char *zName; /* Token associated with this expression */
17293 char *zSpan; /* Original text of the expression */
17294 u8 sortOrder; /* 1 for DESC or 0 for ASC */
17295 unsigned done :1; /* A flag to indicate when processing is finished */
17296 unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
17297 unsigned reusable :1; /* Constant expression is reusable */
17298 unsigned bSorterRef :1; /* Defer evaluation until after sorting */
17299 union {
17300 struct {
17301 u16 iOrderByCol; /* For ORDER BY, column number in result set */
17302 u16 iAlias; /* Index into Parse.aAlias[] for zName */
17303 } x;
17304 int iConstExprReg; /* Register in which Expr value is cached */
17305 } u;
17306 } a[1]; /* One slot for each expression in the list */
17307};
17308
17309/*
17310** An instance of this structure can hold a simple list of identifiers,
17311** such as the list "a,b,c" in the following statements:
17312**
17313** INSERT INTO t(a,b,c) VALUES ...;
17314** CREATE INDEX idx ON t(a,b,c);
17315** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
17316**
17317** The IdList.a.idx field is used when the IdList represents the list of
17318** column names after a table name in an INSERT statement. In the statement
17319**
17320** INSERT INTO t(a,b,c) ...
17321**
17322** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
17323*/
17324struct IdList {
17325 struct IdList_item {
17326 char *zName; /* Name of the identifier */
17327 int idx; /* Index in some Table.aCol[] of a column named zName */
17328 } *a;
17329 int nId; /* Number of identifiers on the list */
17330};
17331
17332/*
17333** The following structure describes the FROM clause of a SELECT statement.
17334** Each table or subquery in the FROM clause is a separate element of
17335** the SrcList.a[] array.
17336**
17337** With the addition of multiple database support, the following structure
17338** can also be used to describe a particular table such as the table that
17339** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
17340** such a table must be a simple name: ID. But in SQLite, the table can
17341** now be identified by a database name, a dot, then the table name: ID.ID.
17342**
17343** The jointype starts out showing the join type between the current table
17344** and the next table on the list. The parser builds the list this way.
17345** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
17346** jointype expresses the join between the table and the previous table.
17347**
17348** In the colUsed field, the high-order bit (bit 63) is set if the table
17349** contains more than 63 columns and the 64-th or later column is used.
17350*/
17351struct SrcList {
17352 int nSrc; /* Number of tables or subqueries in the FROM clause */
17353 u32 nAlloc; /* Number of entries allocated in a[] below */
17354 struct SrcList_item {
17355 Schema *pSchema; /* Schema to which this item is fixed */
17356 char *zDatabase; /* Name of database holding this table */
17357 char *zName; /* Name of the table */
17358 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
17359 Table *pTab; /* An SQL table corresponding to zName */
17360 Select *pSelect; /* A SELECT statement used in place of a table name */
17361 int addrFillSub; /* Address of subroutine to manifest a subquery */
17362 int regReturn; /* Register holding return address of addrFillSub */
17363 int regResult; /* Registers holding results of a co-routine */
17364 struct {
17365 u8 jointype; /* Type of join between this table and the previous */
17366 unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
17367 unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
17368 unsigned isTabFunc :1; /* True if table-valued-function syntax */
17369 unsigned isCorrelated :1; /* True if sub-query is correlated */
17370 unsigned viaCoroutine :1; /* Implemented as a co-routine */
17371 unsigned isRecursive :1; /* True for recursive reference in WITH */
17372 } fg;
17373 int iCursor; /* The VDBE cursor number used to access this table */
17374 Expr *pOn; /* The ON clause of a join */
17375 IdList *pUsing; /* The USING clause of a join */
17376 Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
17377 union {
17378 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
17379 ExprList *pFuncArg; /* Arguments to table-valued-function */
17380 } u1;
17381 Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
17382 } a[1]; /* One entry for each identifier on the list */
17383};
17384
17385/*
17386** Permitted values of the SrcList.a.jointype field
17387*/
17388#define JT_INNER 0x0001 /* Any kind of inner or cross join */
17389#define JT_CROSS 0x0002 /* Explicit use of the CROSS keyword */
17390#define JT_NATURAL 0x0004 /* True for a "natural" join */
17391#define JT_LEFT 0x0008 /* Left outer join */
17392#define JT_RIGHT 0x0010 /* Right outer join */
17393#define JT_OUTER 0x0020 /* The "OUTER" keyword is present */
17394#define JT_ERROR 0x0040 /* unknown or unsupported join type */
17395
17396
17397/*
17398** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
17399** and the WhereInfo.wctrlFlags member.
17400**
17401** Value constraints (enforced via assert()):
17402** WHERE_USE_LIMIT == SF_FixedLimit
17403*/
17404#define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
17405#define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
17406#define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
17407#define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
17408#define WHERE_ONEPASS_MULTIROW 0x0008 /* ONEPASS is ok with multiple rows */
17409#define WHERE_DUPLICATES_OK 0x0010 /* Ok to return a row more than once */
17410#define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
17411 ** the OR optimization */
17412#define WHERE_GROUPBY 0x0040 /* pOrderBy is really a GROUP BY */
17413#define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */
17414#define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */
17415#define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */
17416#define WHERE_SEEK_TABLE 0x0400 /* Do not defer seeks on main table */
17417#define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */
17418#define WHERE_SEEK_UNIQ_TABLE 0x1000 /* Do not defer seeks if unique */
17419 /* 0x2000 not currently used */
17420#define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */
17421 /* 0x8000 not currently used */
17422
17423/* Allowed return values from sqlite3WhereIsDistinct()
17424*/
17425#define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
17426#define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
17427#define WHERE_DISTINCT_ORDERED 2 /* All duplicates are adjacent */
17428#define WHERE_DISTINCT_UNORDERED 3 /* Duplicates are scattered */
17429
17430/*
17431** A NameContext defines a context in which to resolve table and column
17432** names. The context consists of a list of tables (the pSrcList) field and
17433** a list of named expression (pEList). The named expression list may
17434** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
17435** to the table being operated on by INSERT, UPDATE, or DELETE. The
17436** pEList corresponds to the result set of a SELECT and is NULL for
17437** other statements.
17438**
17439** NameContexts can be nested. When resolving names, the inner-most
17440** context is searched first. If no match is found, the next outer
17441** context is checked. If there is still no match, the next context
17442** is checked. This process continues until either a match is found
17443** or all contexts are check. When a match is found, the nRef member of
17444** the context containing the match is incremented.
17445**
17446** Each subquery gets a new NameContext. The pNext field points to the
17447** NameContext in the parent query. Thus the process of scanning the
17448** NameContext list corresponds to searching through successively outer
17449** subqueries looking for a match.
17450*/
17451struct NameContext {
17452 Parse *pParse; /* The parser */
17453 SrcList *pSrcList; /* One or more tables used to resolve names */
17454 union {
17455 ExprList *pEList; /* Optional list of result-set columns */
17456 AggInfo *pAggInfo; /* Information about aggregates at this level */
17457 Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */
17458 } uNC;
17459 NameContext *pNext; /* Next outer name context. NULL for outermost */
17460 int nRef; /* Number of names resolved by this context */
17461 int nErr; /* Number of errors encountered while resolving names */
17462 u16 ncFlags; /* Zero or more NC_* flags defined below */
17463 Select *pWinSelect; /* SELECT statement for any window functions */
17464};
17465
17466/*
17467** Allowed values for the NameContext, ncFlags field.
17468**
17469** Value constraints (all checked via assert()):
17470** NC_HasAgg == SF_HasAgg
17471** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
17472**
17473*/
17474#define NC_AllowAgg 0x0001 /* Aggregate functions are allowed here */
17475#define NC_PartIdx 0x0002 /* True if resolving a partial index WHERE */
17476#define NC_IsCheck 0x0004 /* True if resolving names in a CHECK constraint */
17477#define NC_InAggFunc 0x0008 /* True if analyzing arguments to an agg func */
17478#define NC_HasAgg 0x0010 /* One or more aggregate functions seen */
17479#define NC_IdxExpr 0x0020 /* True if resolving columns of CREATE INDEX */
17480#define NC_VarSelect 0x0040 /* A correlated subquery has been seen */
17481#define NC_UEList 0x0080 /* True if uNC.pEList is used */
17482#define NC_UAggInfo 0x0100 /* True if uNC.pAggInfo is used */
17483#define NC_UUpsert 0x0200 /* True if uNC.pUpsert is used */
17484#define NC_MinMaxAgg 0x1000 /* min/max aggregates seen. See note above */
17485#define NC_Complex 0x2000 /* True if a function or subquery seen */
17486#define NC_AllowWin 0x4000 /* Window functions are allowed here */
17487
17488/*
17489** An instance of the following object describes a single ON CONFLICT
17490** clause in an upsert.
17491**
17492** The pUpsertTarget field is only set if the ON CONFLICT clause includes
17493** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
17494** conflict-target clause.) The pUpsertTargetWhere is the optional
17495** WHERE clause used to identify partial unique indexes.
17496**
17497** pUpsertSet is the list of column=expr terms of the UPDATE statement.
17498** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The
17499** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
17500** WHERE clause is omitted.
17501*/
17502struct Upsert {
17503 ExprList *pUpsertTarget; /* Optional description of conflicting index */
17504 Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
17505 ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
17506 Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
17507 /* The fields above comprise the parse tree for the upsert clause.
17508 ** The fields below are used to transfer information from the INSERT
17509 ** processing down into the UPDATE processing while generating code.
17510 ** Upsert owns the memory allocated above, but not the memory below. */
17511 Index *pUpsertIdx; /* Constraint that pUpsertTarget identifies */
17512 SrcList *pUpsertSrc; /* Table to be updated */
17513 int regData; /* First register holding array of VALUES */
17514 int iDataCur; /* Index of the data cursor */
17515 int iIdxCur; /* Index of the first index cursor */
17516};
17517
17518/*
17519** An instance of the following structure contains all information
17520** needed to generate code for a single SELECT statement.
17521**
17522** See the header comment on the computeLimitRegisters() routine for a
17523** detailed description of the meaning of the iLimit and iOffset fields.
17524**
17525** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
17526** These addresses must be stored so that we can go back and fill in
17527** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
17528** the number of columns in P2 can be computed at the same time
17529** as the OP_OpenEphm instruction is coded because not
17530** enough information about the compound query is known at that point.
17531** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
17532** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
17533** sequences for the ORDER BY clause.
17534*/
17535struct Select {
17536 ExprList *pEList; /* The fields of the result */
17537 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
17538 LogEst nSelectRow; /* Estimated number of result rows */
17539 u32 selFlags; /* Various SF_* values */
17540 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
17541 u32 selId; /* Unique identifier number for this SELECT */
17542 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
17543 SrcList *pSrc; /* The FROM clause */
17544 Expr *pWhere; /* The WHERE clause */
17545 ExprList *pGroupBy; /* The GROUP BY clause */
17546 Expr *pHaving; /* The HAVING clause */
17547 ExprList *pOrderBy; /* The ORDER BY clause */
17548 Select *pPrior; /* Prior select in a compound select statement */
17549 Select *pNext; /* Next select to the left in a compound */
17550 Expr *pLimit; /* LIMIT expression. NULL means not used. */
17551 With *pWith; /* WITH clause attached to this select. Or NULL. */
17552#ifndef SQLITE_OMIT_WINDOWFUNC
17553 Window *pWin; /* List of window functions */
17554 Window *pWinDefn; /* List of named window definitions */
17555#endif
17556};
17557
17558/*
17559** Allowed values for Select.selFlags. The "SF" prefix stands for
17560** "Select Flag".
17561**
17562** Value constraints (all checked via assert())
17563** SF_HasAgg == NC_HasAgg
17564** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX
17565** SF_FixedLimit == WHERE_USE_LIMIT
17566*/
17567#define SF_Distinct 0x00001 /* Output should be DISTINCT */
17568#define SF_All 0x00002 /* Includes the ALL keyword */
17569#define SF_Resolved 0x00004 /* Identifiers have been resolved */
17570#define SF_Aggregate 0x00008 /* Contains agg functions or a GROUP BY */
17571#define SF_HasAgg 0x00010 /* Contains aggregate functions */
17572#define SF_UsesEphemeral 0x00020 /* Uses the OpenEphemeral opcode */
17573#define SF_Expanded 0x00040 /* sqlite3SelectExpand() called on this */
17574#define SF_HasTypeInfo 0x00080 /* FROM subqueries have Table metadata */
17575#define SF_Compound 0x00100 /* Part of a compound query */
17576#define SF_Values 0x00200 /* Synthesized from VALUES clause */
17577#define SF_MultiValue 0x00400 /* Single VALUES term with multiple rows */
17578#define SF_NestedFrom 0x00800 /* Part of a parenthesized FROM clause */
17579#define SF_MinMaxAgg 0x01000 /* Aggregate containing min() or max() */
17580#define SF_Recursive 0x02000 /* The recursive part of a recursive CTE */
17581#define SF_FixedLimit 0x04000 /* nSelectRow set by a constant LIMIT */
17582#define SF_MaybeConvert 0x08000 /* Need convertCompoundSelectToSubquery() */
17583#define SF_Converted 0x10000 /* By convertCompoundSelectToSubquery() */
17584#define SF_IncludeHidden 0x20000 /* Include hidden columns in output */
17585#define SF_ComplexResult 0x40000 /* Result contains subquery or function */
17586
17587/*
17588** The results of a SELECT can be distributed in several ways, as defined
17589** by one of the following macros. The "SRT" prefix means "SELECT Result
17590** Type".
17591**
17592** SRT_Union Store results as a key in a temporary index
17593** identified by pDest->iSDParm.
17594**
17595** SRT_Except Remove results from the temporary index pDest->iSDParm.
17596**
17597** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
17598** set is not empty.
17599**
17600** SRT_Discard Throw the results away. This is used by SELECT
17601** statements within triggers whose only purpose is
17602** the side-effects of functions.
17603**
17604** All of the above are free to ignore their ORDER BY clause. Those that
17605** follow must honor the ORDER BY clause.
17606**
17607** SRT_Output Generate a row of output (using the OP_ResultRow
17608** opcode) for each row in the result set.
17609**
17610** SRT_Mem Only valid if the result is a single column.
17611** Store the first column of the first result row
17612** in register pDest->iSDParm then abandon the rest
17613** of the query. This destination implies "LIMIT 1".
17614**
17615** SRT_Set The result must be a single column. Store each
17616** row of result as the key in table pDest->iSDParm.
17617** Apply the affinity pDest->affSdst before storing
17618** results. Used to implement "IN (SELECT ...)".
17619**
17620** SRT_EphemTab Create an temporary table pDest->iSDParm and store
17621** the result there. The cursor is left open after
17622** returning. This is like SRT_Table except that
17623** this destination uses OP_OpenEphemeral to create
17624** the table first.
17625**
17626** SRT_Coroutine Generate a co-routine that returns a new row of
17627** results each time it is invoked. The entry point
17628** of the co-routine is stored in register pDest->iSDParm
17629** and the result row is stored in pDest->nDest registers
17630** starting with pDest->iSdst.
17631**
17632** SRT_Table Store results in temporary table pDest->iSDParm.
17633** SRT_Fifo This is like SRT_EphemTab except that the table
17634** is assumed to already be open. SRT_Fifo has
17635** the additional property of being able to ignore
17636** the ORDER BY clause.
17637**
17638** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
17639** But also use temporary table pDest->iSDParm+1 as
17640** a record of all prior results and ignore any duplicate
17641** rows. Name means: "Distinct Fifo".
17642**
17643** SRT_Queue Store results in priority queue pDest->iSDParm (really
17644** an index). Append a sequence number so that all entries
17645** are distinct.
17646**
17647** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
17648** the same record has never been stored before. The
17649** index at pDest->iSDParm+1 hold all prior stores.
17650*/
17651#define SRT_Union 1 /* Store result as keys in an index */
17652#define SRT_Except 2 /* Remove result from a UNION index */
17653#define SRT_Exists 3 /* Store 1 if the result is not empty */
17654#define SRT_Discard 4 /* Do not save the results anywhere */
17655#define SRT_Fifo 5 /* Store result as data with an automatic rowid */
17656#define SRT_DistFifo 6 /* Like SRT_Fifo, but unique results only */
17657#define SRT_Queue 7 /* Store result in an queue */
17658#define SRT_DistQueue 8 /* Like SRT_Queue, but unique results only */
17659
17660/* The ORDER BY clause is ignored for all of the above */
17661#define IgnorableOrderby(X) ((X->eDest)<=SRT_DistQueue)
17662
17663#define SRT_Output 9 /* Output each row of result */
17664#define SRT_Mem 10 /* Store result in a memory cell */
17665#define SRT_Set 11 /* Store results as keys in an index */
17666#define SRT_EphemTab 12 /* Create transient tab and store like SRT_Table */
17667#define SRT_Coroutine 13 /* Generate a single row of result */
17668#define SRT_Table 14 /* Store result as data with an automatic rowid */
17669
17670/*
17671** An instance of this object describes where to put of the results of
17672** a SELECT statement.
17673*/
17674struct SelectDest {
17675 u8 eDest; /* How to dispose of the results. On of SRT_* above. */
17676 int iSDParm; /* A parameter used by the eDest disposal method */
17677 int iSdst; /* Base register where results are written */
17678 int nSdst; /* Number of registers allocated */
17679 char *zAffSdst; /* Affinity used when eDest==SRT_Set */
17680 ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
17681};
17682
17683/*
17684** During code generation of statements that do inserts into AUTOINCREMENT
17685** tables, the following information is attached to the Table.u.autoInc.p
17686** pointer of each autoincrement table to record some side information that
17687** the code generator needs. We have to keep per-table autoincrement
17688** information in case inserts are done within triggers. Triggers do not
17689** normally coordinate their activities, but we do need to coordinate the
17690** loading and saving of autoincrement information.
17691*/
17692struct AutoincInfo {
17693 AutoincInfo *pNext; /* Next info block in a list of them all */
17694 Table *pTab; /* Table this info block refers to */
17695 int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
17696 int regCtr; /* Memory register holding the rowid counter */
17697};
17698
17699/*
17700** At least one instance of the following structure is created for each
17701** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
17702** statement. All such objects are stored in the linked list headed at
17703** Parse.pTriggerPrg and deleted once statement compilation has been
17704** completed.
17705**
17706** A Vdbe sub-program that implements the body and WHEN clause of trigger
17707** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
17708** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
17709** The Parse.pTriggerPrg list never contains two entries with the same
17710** values for both pTrigger and orconf.
17711**
17712** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
17713** accessed (or set to 0 for triggers fired as a result of INSERT
17714** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
17715** a mask of new.* columns used by the program.
17716*/
17717struct TriggerPrg {
17718 Trigger *pTrigger; /* Trigger this program was coded from */
17719 TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
17720 SubProgram *pProgram; /* Program implementing pTrigger/orconf */
17721 int orconf; /* Default ON CONFLICT policy */
17722 u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
17723};
17724
17725/*
17726** The yDbMask datatype for the bitmask of all attached databases.
17727*/
17728#if SQLITE_MAX_ATTACHED>30
17729 typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED+9)/8];
17730# define DbMaskTest(M,I) (((M)[(I)/8]&(1<<((I)&7)))!=0)
17731# define DbMaskZero(M) memset((M),0,sizeof(M))
17732# define DbMaskSet(M,I) (M)[(I)/8]|=(1<<((I)&7))
17733# define DbMaskAllZero(M) sqlite3DbMaskAllZero(M)
17734# define DbMaskNonZero(M) (sqlite3DbMaskAllZero(M)==0)
17735#else
17736 typedef unsigned int yDbMask;
17737# define DbMaskTest(M,I) (((M)&(((yDbMask)1)<<(I)))!=0)
17738# define DbMaskZero(M) (M)=0
17739# define DbMaskSet(M,I) (M)|=(((yDbMask)1)<<(I))
17740# define DbMaskAllZero(M) (M)==0
17741# define DbMaskNonZero(M) (M)!=0
17742#endif
17743
17744/*
17745** An SQL parser context. A copy of this structure is passed through
17746** the parser and down into all the parser action routine in order to
17747** carry around information that is global to the entire parse.
17748**
17749** The structure is divided into two parts. When the parser and code
17750** generate call themselves recursively, the first part of the structure
17751** is constant but the second part is reset at the beginning and end of
17752** each recursion.
17753**
17754** The nTableLock and aTableLock variables are only used if the shared-cache
17755** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
17756** used to store the set of table-locks required by the statement being
17757** compiled. Function sqlite3TableLock() is used to add entries to the
17758** list.
17759*/
17760struct Parse {
17761 sqlite3 *db; /* The main database structure */
17762 char *zErrMsg; /* An error message */
17763 Vdbe *pVdbe; /* An engine for executing database bytecode */
17764 int rc; /* Return code from execution */
17765 u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
17766 u8 checkSchema; /* Causes schema cookie check after an error */
17767 u8 nested; /* Number of nested calls to the parser/code generator */
17768 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
17769 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
17770 u8 mayAbort; /* True if statement may throw an ABORT exception */
17771 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
17772 u8 okConstFactor; /* OK to factor out constants */
17773 u8 disableLookaside; /* Number of times lookaside has been disabled */
17774 int nRangeReg; /* Size of the temporary register block */
17775 int iRangeReg; /* First register in temporary register block */
17776 int nErr; /* Number of errors seen */
17777 int nTab; /* Number of previously allocated VDBE cursors */
17778 int nMem; /* Number of memory cells used so far */
17779 int nOpAlloc; /* Number of slots allocated for Vdbe.aOp[] */
17780 int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */
17781 int iSelfTab; /* Table associated with an index on expr, or negative
17782 ** of the base register during check-constraint eval */
17783 int nLabel; /* Number of labels used */
17784 int *aLabel; /* Space to hold the labels */
17785 ExprList *pConstExpr;/* Constant expressions */
17786 Token constraintName;/* Name of the constraint currently being parsed */
17787 yDbMask writeMask; /* Start a write transaction on these databases */
17788 yDbMask cookieMask; /* Bitmask of schema verified databases */
17789 int regRowid; /* Register holding rowid of CREATE TABLE entry */
17790 int regRoot; /* Register holding root page number for new objects */
17791 int nMaxArg; /* Max args passed to user function by sub-program */
17792 int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
17793#ifndef SQLITE_OMIT_SHARED_CACHE
17794 int nTableLock; /* Number of locks in aTableLock */
17795 TableLock *aTableLock; /* Required table locks for shared-cache mode */
17796#endif
17797 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
17798 Parse *pToplevel; /* Parse structure for main program (or NULL) */
17799 Table *pTriggerTab; /* Table triggers are being coded for */
17800 int addrCrTab; /* Address of OP_CreateBtree opcode on CREATE TABLE */
17801 u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
17802 u32 oldmask; /* Mask of old.* columns referenced */
17803 u32 newmask; /* Mask of new.* columns referenced */
17804 u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
17805 u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
17806 u8 disableTriggers; /* True to disable triggers */
17807
17808 /**************************************************************************
17809 ** Fields above must be initialized to zero. The fields that follow,
17810 ** down to the beginning of the recursive section, do not need to be
17811 ** initialized as they will be set before being used. The boundary is
17812 ** determined by offsetof(Parse,aTempReg).
17813 **************************************************************************/
17814
17815 int aTempReg[8]; /* Holding area for temporary registers */
17816 Token sNameToken; /* Token with unqualified schema object name */
17817
17818 /************************************************************************
17819 ** Above is constant between recursions. Below is reset before and after
17820 ** each recursion. The boundary between these two regions is determined
17821 ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
17822 ** first field in the recursive region.
17823 ************************************************************************/
17824
17825 Token sLastToken; /* The last token parsed */
17826 ynVar nVar; /* Number of '?' variables seen in the SQL so far */
17827 u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */
17828 u8 explain; /* True if the EXPLAIN flag is found on the query */
17829#if !(defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE))
17830 u8 eParseMode; /* PARSE_MODE_XXX constant */
17831#endif
17832#ifndef SQLITE_OMIT_VIRTUALTABLE
17833 int nVtabLock; /* Number of virtual tables to lock */
17834#endif
17835 int nHeight; /* Expression tree height of current sub-select */
17836#ifndef SQLITE_OMIT_EXPLAIN
17837 int addrExplain; /* Address of current OP_Explain opcode */
17838#endif
17839 VList *pVList; /* Mapping between variable names and numbers */
17840 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
17841 const char *zTail; /* All SQL text past the last semicolon parsed */
17842 Table *pNewTable; /* A table being constructed by CREATE TABLE */
17843 Index *pNewIndex; /* An index being constructed by CREATE INDEX */
17844 Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
17845 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
17846#ifndef SQLITE_OMIT_VIRTUALTABLE
17847 Token sArg; /* Complete text of a module argument */
17848 Table **apVtabLock; /* Pointer to virtual tables needing locking */
17849#endif
17850 Table *pZombieTab; /* List of Table objects to delete after code gen */
17851 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
17852 With *pWith; /* Current WITH clause, or NULL */
17853 With *pWithToFree; /* Free this WITH object at the end of the parse */
17854#ifndef SQLITE_OMIT_ALTERTABLE
17855 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
17856#endif
17857};
17858
17859#define PARSE_MODE_NORMAL 0
17860#define PARSE_MODE_DECLARE_VTAB 1
17861#define PARSE_MODE_RENAME_COLUMN 2
17862#define PARSE_MODE_RENAME_TABLE 3
17863
17864/*
17865** Sizes and pointers of various parts of the Parse object.
17866*/
17867#define PARSE_HDR_SZ offsetof(Parse,aTempReg) /* Recursive part w/o aColCache*/
17868#define PARSE_RECURSE_SZ offsetof(Parse,sLastToken) /* Recursive part */
17869#define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
17870#define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ) /* Pointer to tail */
17871
17872/*
17873** Return true if currently inside an sqlite3_declare_vtab() call.
17874*/
17875#ifdef SQLITE_OMIT_VIRTUALTABLE
17876 #define IN_DECLARE_VTAB 0
17877#else
17878 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
17879#endif
17880
17881#if defined(SQLITE_OMIT_ALTERTABLE)
17882 #define IN_RENAME_OBJECT 0
17883#else
17884 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME_COLUMN)
17885#endif
17886
17887#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
17888 #define IN_SPECIAL_PARSE 0
17889#else
17890 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
17891#endif
17892
17893/*
17894** An instance of the following structure can be declared on a stack and used
17895** to save the Parse.zAuthContext value so that it can be restored later.
17896*/
17897struct AuthContext {
17898 const char *zAuthContext; /* Put saved Parse.zAuthContext here */
17899 Parse *pParse; /* The Parse structure */
17900};
17901
17902/*
17903** Bitfield flags for P5 value in various opcodes.
17904**
17905** Value constraints (enforced via assert()):
17906** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH
17907** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF
17908** OPFLAG_BULKCSR == BTREE_BULKLOAD
17909** OPFLAG_SEEKEQ == BTREE_SEEK_EQ
17910** OPFLAG_FORDELETE == BTREE_FORDELETE
17911** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
17912** OPFLAG_AUXDELETE == BTREE_AUXDELETE
17913*/
17914#define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
17915 /* Also used in P2 (not P5) of OP_Delete */
17916#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
17917#define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
17918#define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
17919#define OPFLAG_APPEND 0x08 /* This is likely to be an append */
17920#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
17921#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
17922#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
17923#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
17924#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
17925#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
17926#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
17927#define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
17928#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
17929#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete/Insert: save cursor pos */
17930#define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
17931#define OPFLAG_NOCHNG_MAGIC 0x6d /* OP_MakeRecord: serialtype 10 is ok */
17932
17933/*
17934 * Each trigger present in the database schema is stored as an instance of
17935 * struct Trigger.
17936 *
17937 * Pointers to instances of struct Trigger are stored in two ways.
17938 * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
17939 * database). This allows Trigger structures to be retrieved by name.
17940 * 2. All triggers associated with a single table form a linked list, using the
17941 * pNext member of struct Trigger. A pointer to the first element of the
17942 * linked list is stored as the "pTrigger" member of the associated
17943 * struct Table.
17944 *
17945 * The "step_list" member points to the first element of a linked list
17946 * containing the SQL statements specified as the trigger program.
17947 */
17948struct Trigger {
17949 char *zName; /* The name of the trigger */
17950 char *table; /* The table or view to which the trigger applies */
17951 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
17952 u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
17953 Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
17954 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
17955 the <column-list> is stored here */
17956 Schema *pSchema; /* Schema containing the trigger */
17957 Schema *pTabSchema; /* Schema containing the table */
17958 TriggerStep *step_list; /* Link list of trigger program steps */
17959 Trigger *pNext; /* Next trigger associated with the table */
17960};
17961
17962/*
17963** A trigger is either a BEFORE or an AFTER trigger. The following constants
17964** determine which.
17965**
17966** If there are multiple triggers, you might of some BEFORE and some AFTER.
17967** In that cases, the constants below can be ORed together.
17968*/
17969#define TRIGGER_BEFORE 1
17970#define TRIGGER_AFTER 2
17971
17972/*
17973 * An instance of struct TriggerStep is used to store a single SQL statement
17974 * that is a part of a trigger-program.
17975 *
17976 * Instances of struct TriggerStep are stored in a singly linked list (linked
17977 * using the "pNext" member) referenced by the "step_list" member of the
17978 * associated struct Trigger instance. The first element of the linked list is
17979 * the first step of the trigger-program.
17980 *
17981 * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
17982 * "SELECT" statement. The meanings of the other members is determined by the
17983 * value of "op" as follows:
17984 *
17985 * (op == TK_INSERT)
17986 * orconf -> stores the ON CONFLICT algorithm
17987 * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
17988 * this stores a pointer to the SELECT statement. Otherwise NULL.
17989 * zTarget -> Dequoted name of the table to insert into.
17990 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
17991 * this stores values to be inserted. Otherwise NULL.
17992 * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
17993 * statement, then this stores the column-names to be
17994 * inserted into.
17995 *
17996 * (op == TK_DELETE)
17997 * zTarget -> Dequoted name of the table to delete from.
17998 * pWhere -> The WHERE clause of the DELETE statement if one is specified.
17999 * Otherwise NULL.
18000 *
18001 * (op == TK_UPDATE)
18002 * zTarget -> Dequoted name of the table to update.
18003 * pWhere -> The WHERE clause of the UPDATE statement if one is specified.
18004 * Otherwise NULL.
18005 * pExprList -> A list of the columns to update and the expressions to update
18006 * them to. See sqlite3Update() documentation of "pChanges"
18007 * argument.
18008 *
18009 */
18010struct TriggerStep {
18011 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
18012 u8 orconf; /* OE_Rollback etc. */
18013 Trigger *pTrig; /* The trigger that this step is a part of */
18014 Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */
18015 char *zTarget; /* Target table for DELETE, UPDATE, INSERT */
18016 Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
18017 ExprList *pExprList; /* SET clause for UPDATE */
18018 IdList *pIdList; /* Column names for INSERT */
18019 Upsert *pUpsert; /* Upsert clauses on an INSERT */
18020 char *zSpan; /* Original SQL text of this command */
18021 TriggerStep *pNext; /* Next in the link-list */
18022 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
18023};
18024
18025/*
18026** The following structure contains information used by the sqliteFix...
18027** routines as they walk the parse tree to make database references
18028** explicit.
18029*/
18030typedef struct DbFixer DbFixer;
18031struct DbFixer {
18032 Parse *pParse; /* The parsing context. Error messages written here */
18033 Schema *pSchema; /* Fix items to this schema */
18034 int bVarOnly; /* Check for variable references only */
18035 const char *zDb; /* Make sure all objects are contained in this database */
18036 const char *zType; /* Type of the container - used for error messages */
18037 const Token *pName; /* Name of the container - used for error messages */
18038};
18039
18040/*
18041** An objected used to accumulate the text of a string where we
18042** do not necessarily know how big the string will be in the end.
18043*/
18044struct sqlite3_str {
18045 sqlite3 *db; /* Optional database for lookaside. Can be NULL */
18046 char *zText; /* The string collected so far */
18047 u32 nAlloc; /* Amount of space allocated in zText */
18048 u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
18049 u32 nChar; /* Length of the string so far */
18050 u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */
18051 u8 printfFlags; /* SQLITE_PRINTF flags below */
18052};
18053#define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
18054#define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */
18055#define SQLITE_PRINTF_MALLOCED 0x04 /* True if xText is allocated space */
18056
18057#define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
18058
18059
18060/*
18061** A pointer to this structure is used to communicate information
18062** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
18063*/
18064typedef struct {
18065 sqlite3 *db; /* The database being initialized */
18066 char **pzErrMsg; /* Error message stored here */
18067 int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
18068 int rc; /* Result code stored here */
18069 u32 mInitFlags; /* Flags controlling error messages */
18070} InitData;
18071
18072/*
18073** Allowed values for mInitFlags
18074*/
18075#define INITFLAG_AlterTable 0x0001 /* This is a reparse after ALTER TABLE */
18076
18077/*
18078** Structure containing global configuration data for the SQLite library.
18079**
18080** This structure also contains some state information.
18081*/
18082struct Sqlite3Config {
18083 int bMemstat; /* True to enable memory status */
18084 int bCoreMutex; /* True to enable core mutexing */
18085 int bFullMutex; /* True to enable full mutexing */
18086 int bOpenUri; /* True to interpret filenames as URIs */
18087 int bUseCis; /* Use covering indices for full-scans */
18088 int bSmallMalloc; /* Avoid large memory allocations if true */
18089 int mxStrlen; /* Maximum string length */
18090 int neverCorrupt; /* Database is always well-formed */
18091 int szLookaside; /* Default lookaside buffer size */
18092 int nLookaside; /* Default lookaside buffer count */
18093 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
18094 sqlite3_mem_methods m; /* Low-level memory allocation interface */
18095 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
18096 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
18097 void *pHeap; /* Heap storage space */
18098 int nHeap; /* Size of pHeap[] */
18099 int mnReq, mxReq; /* Min and max heap requests sizes */
18100 sqlite3_int64 szMmap; /* mmap() space per open file */
18101 sqlite3_int64 mxMmap; /* Maximum value for szMmap */
18102 void *pPage; /* Page cache memory */
18103 int szPage; /* Size of each page in pPage[] */
18104 int nPage; /* Number of pages in pPage[] */
18105 int mxParserStack; /* maximum depth of the parser stack */
18106 int sharedCacheEnabled; /* true if shared-cache mode enabled */
18107 u32 szPma; /* Maximum Sorter PMA size */
18108 /* The above might be initialized to non-zero. The following need to always
18109 ** initially be zero, however. */
18110 int isInit; /* True after initialization has finished */
18111 int inProgress; /* True while initialization in progress */
18112 int isMutexInit; /* True after mutexes are initialized */
18113 int isMallocInit; /* True after malloc is initialized */
18114 int isPCacheInit; /* True after malloc is initialized */
18115 int nRefInitMutex; /* Number of users of pInitMutex */
18116 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
18117 void (*xLog)(void*,int,const char*); /* Function for logging */
18118 void *pLogArg; /* First argument to xLog() */
18119#ifdef SQLITE_ENABLE_SQLLOG
18120 void(*xSqllog)(void*,sqlite3*,const char*, int);
18121 void *pSqllogArg;
18122#endif
18123#ifdef SQLITE_VDBE_COVERAGE
18124 /* The following callback (if not NULL) is invoked on every VDBE branch
18125 ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
18126 */
18127 void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */
18128 void *pVdbeBranchArg; /* 1st argument */
18129#endif
18130#ifndef SQLITE_UNTESTABLE
18131 int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
18132#endif
18133 int bLocaltimeFault; /* True to fail localtime() calls */
18134 int iOnceResetThreshold; /* When to reset OP_Once counters */
18135 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
18136};
18137
18138/*
18139** This macro is used inside of assert() statements to indicate that
18140** the assert is only valid on a well-formed database. Instead of:
18141**
18142** assert( X );
18143**
18144** One writes:
18145**
18146** assert( X || CORRUPT_DB );
18147**
18148** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate
18149** that the database is definitely corrupt, only that it might be corrupt.
18150** For most test cases, CORRUPT_DB is set to false using a special
18151** sqlite3_test_control(). This enables assert() statements to prove
18152** things that are always true for well-formed databases.
18153*/
18154#define CORRUPT_DB (sqlite3Config.neverCorrupt==0)
18155
18156/*
18157** Context pointer passed down through the tree-walk.
18158*/
18159struct Walker {
18160 Parse *pParse; /* Parser context. */
18161 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
18162 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
18163 void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
18164 int walkerDepth; /* Number of subqueries */
18165 u8 eCode; /* A small processing code */
18166 union { /* Extra data for callback */
18167 NameContext *pNC; /* Naming context */
18168 int n; /* A counter */
18169 int iCur; /* A cursor number */
18170 SrcList *pSrcList; /* FROM clause */
18171 struct SrcCount *pSrcCount; /* Counting column references */
18172 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
18173 int *aiCol; /* array of column indexes */
18174 struct IdxCover *pIdxCover; /* Check for index coverage */
18175 struct IdxExprTrans *pIdxTrans; /* Convert idxed expr to column */
18176 ExprList *pGroupBy; /* GROUP BY clause */
18177 Select *pSelect; /* HAVING to WHERE clause ctx */
18178 struct WindowRewrite *pRewrite; /* Window rewrite context */
18179 struct WhereConst *pConst; /* WHERE clause constants */
18180 struct RenameCtx *pRename; /* RENAME COLUMN context */
18181 } u;
18182};
18183
18184/* Forward declarations */
18185SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
18186SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
18187SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
18188SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
18189SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
18190SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*);
18191SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker*, Select*);
18192SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker*, Select*);
18193#ifdef SQLITE_DEBUG
18194SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker*, Select*);
18195#endif
18196
18197/*
18198** Return code from the parse-tree walking primitives and their
18199** callbacks.
18200*/
18201#define WRC_Continue 0 /* Continue down into children */
18202#define WRC_Prune 1 /* Omit children but continue walking siblings */
18203#define WRC_Abort 2 /* Abandon the tree walk */
18204
18205/*
18206** An instance of this structure represents a set of one or more CTEs
18207** (common table expressions) created by a single WITH clause.
18208*/
18209struct With {
18210 int nCte; /* Number of CTEs in the WITH clause */
18211 With *pOuter; /* Containing WITH clause, or NULL */
18212 struct Cte { /* For each CTE in the WITH clause.... */
18213 char *zName; /* Name of this CTE */
18214 ExprList *pCols; /* List of explicit column names, or NULL */
18215 Select *pSelect; /* The definition of this CTE */
18216 const char *zCteErr; /* Error message for circular references */
18217 } a[1];
18218};
18219
18220#ifdef SQLITE_DEBUG
18221/*
18222** An instance of the TreeView object is used for printing the content of
18223** data structures on sqlite3DebugPrintf() using a tree-like view.
18224*/
18225struct TreeView {
18226 int iLevel; /* Which level of the tree we are on */
18227 u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */
18228};
18229#endif /* SQLITE_DEBUG */
18230
18231/*
18232** This object is used in varioius ways, all related to window functions
18233**
18234** (1) A single instance of this structure is attached to the
18235** the Expr.pWin field for each window function in an expression tree.
18236** This object holds the information contained in the OVER clause,
18237** plus additional fields used during code generation.
18238**
18239** (2) All window functions in a single SELECT form a linked-list
18240** attached to Select.pWin. The Window.pFunc and Window.pExpr
18241** fields point back to the expression that is the window function.
18242**
18243** (3) The terms of the WINDOW clause of a SELECT are instances of this
18244** object on a linked list attached to Select.pWinDefn.
18245**
18246** The uses (1) and (2) are really the same Window object that just happens
18247** to be accessible in two different ways. Use (3) is are separate objects.
18248*/
18249struct Window {
18250 char *zName; /* Name of window (may be NULL) */
18251 ExprList *pPartition; /* PARTITION BY clause */
18252 ExprList *pOrderBy; /* ORDER BY clause */
18253 u8 eType; /* TK_RANGE or TK_ROWS */
18254 u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
18255 u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
18256 Expr *pStart; /* Expression for "<expr> PRECEDING" */
18257 Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
18258 Window *pNextWin; /* Next window function belonging to this SELECT */
18259 Expr *pFilter; /* The FILTER expression */
18260 FuncDef *pFunc; /* The function */
18261 int iEphCsr; /* Partition buffer or Peer buffer */
18262 int regAccum;
18263 int regResult;
18264 int csrApp; /* Function cursor (used by min/max) */
18265 int regApp; /* Function register (also used by min/max) */
18266 int regPart; /* First in a set of registers holding PARTITION BY
18267 ** and ORDER BY values for the window */
18268 Expr *pOwner; /* Expression object this window is attached to */
18269 int nBufferCol; /* Number of columns in buffer table */
18270 int iArgCol; /* Offset of first argument for this function */
18271};
18272
18273#ifndef SQLITE_OMIT_WINDOWFUNC
18274SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
18275SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p);
18276SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*);
18277SQLITE_PRIVATE void sqlite3WindowAttach(Parse*, Expr*, Window*);
18278SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*);
18279SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Window*);
18280SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
18281SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
18282SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
18283SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
18284SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
18285SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
18286SQLITE_PRIVATE void sqlite3WindowFunctions(void);
18287#else
18288# define sqlite3WindowDelete(a,b)
18289# define sqlite3WindowFunctions()
18290# define sqlite3WindowAttach(a,b,c)
18291#endif
18292
18293/*
18294** Assuming zIn points to the first byte of a UTF-8 character,
18295** advance zIn to point to the first byte of the next UTF-8 character.
18296*/
18297#define SQLITE_SKIP_UTF8(zIn) { \
18298 if( (*(zIn++))>=0xc0 ){ \
18299 while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
18300 } \
18301}
18302
18303/*
18304** The SQLITE_*_BKPT macros are substitutes for the error codes with
18305** the same name but without the _BKPT suffix. These macros invoke
18306** routines that report the line-number on which the error originated
18307** using sqlite3_log(). The routines also provide a convenient place
18308** to set a debugger breakpoint.
18309*/
18310SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType);
18311SQLITE_PRIVATE int sqlite3CorruptError(int);
18312SQLITE_PRIVATE int sqlite3MisuseError(int);
18313SQLITE_PRIVATE int sqlite3CantopenError(int);
18314#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
18315#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
18316#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
18317#ifdef SQLITE_DEBUG
18318SQLITE_PRIVATE int sqlite3NomemError(int);
18319SQLITE_PRIVATE int sqlite3IoerrnomemError(int);
18320SQLITE_PRIVATE int sqlite3CorruptPgnoError(int,Pgno);
18321# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__)
18322# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__)
18323# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptPgnoError(__LINE__,(P))
18324#else
18325# define SQLITE_NOMEM_BKPT SQLITE_NOMEM
18326# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM
18327# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptError(__LINE__)
18328#endif
18329
18330/*
18331** FTS3 and FTS4 both require virtual table support
18332*/
18333#if defined(SQLITE_OMIT_VIRTUALTABLE)
18334# undef SQLITE_ENABLE_FTS3
18335# undef SQLITE_ENABLE_FTS4
18336#endif
18337
18338/*
18339** FTS4 is really an extension for FTS3. It is enabled using the
18340** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
18341** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
18342*/
18343#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
18344# define SQLITE_ENABLE_FTS3 1
18345#endif
18346
18347/*
18348** The ctype.h header is needed for non-ASCII systems. It is also
18349** needed by FTS3 when FTS3 is included in the amalgamation.
18350*/
18351#if !defined(SQLITE_ASCII) || \
18352 (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
18353# include <ctype.h>
18354#endif
18355
18356/*
18357** The following macros mimic the standard library functions toupper(),
18358** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
18359** sqlite versions only work for ASCII characters, regardless of locale.
18360*/
18361#ifdef SQLITE_ASCII
18362# define sqlite3Toupper(x) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
18363# define sqlite3Isspace(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
18364# define sqlite3Isalnum(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
18365# define sqlite3Isalpha(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
18366# define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
18367# define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
18368# define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)])
18369# define sqlite3Isquote(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
18370#else
18371# define sqlite3Toupper(x) toupper((unsigned char)(x))
18372# define sqlite3Isspace(x) isspace((unsigned char)(x))
18373# define sqlite3Isalnum(x) isalnum((unsigned char)(x))
18374# define sqlite3Isalpha(x) isalpha((unsigned char)(x))
18375# define sqlite3Isdigit(x) isdigit((unsigned char)(x))
18376# define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
18377# define sqlite3Tolower(x) tolower((unsigned char)(x))
18378# define sqlite3Isquote(x) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
18379#endif
18380SQLITE_PRIVATE int sqlite3IsIdChar(u8);
18381
18382/*
18383** Internal function prototypes
18384*/
18385SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*);
18386SQLITE_PRIVATE int sqlite3Strlen30(const char*);
18387SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*);
18388#define sqlite3StrNICmp sqlite3_strnicmp
18389
18390SQLITE_PRIVATE int sqlite3MallocInit(void);
18391SQLITE_PRIVATE void sqlite3MallocEnd(void);
18392SQLITE_PRIVATE void *sqlite3Malloc(u64);
18393SQLITE_PRIVATE void *sqlite3MallocZero(u64);
18394SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64);
18395SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64);
18396SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3*, u64);
18397SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
18398SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
18399SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
18400SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
18401SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
18402SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
18403SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
18404SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*);
18405SQLITE_PRIVATE int sqlite3MallocSize(void*);
18406SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
18407SQLITE_PRIVATE void *sqlite3PageMalloc(int);
18408SQLITE_PRIVATE void sqlite3PageFree(void*);
18409SQLITE_PRIVATE void sqlite3MemSetDefault(void);
18410#ifndef SQLITE_UNTESTABLE
18411SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
18412#endif
18413SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
18414
18415/*
18416** On systems with ample stack space and that support alloca(), make
18417** use of alloca() to obtain space for large automatic objects. By default,
18418** obtain space from malloc().
18419**
18420** The alloca() routine never returns NULL. This will cause code paths
18421** that deal with sqlite3StackAlloc() failures to be unreachable.
18422*/
18423#ifdef SQLITE_USE_ALLOCA
18424# define sqlite3StackAllocRaw(D,N) alloca(N)
18425# define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N)
18426# define sqlite3StackFree(D,P)
18427#else
18428# define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N)
18429# define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N)
18430# define sqlite3StackFree(D,P) sqlite3DbFree(D,P)
18431#endif
18432
18433/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
18434** are, disable MEMSYS3
18435*/
18436#ifdef SQLITE_ENABLE_MEMSYS5
18437SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
18438#undef SQLITE_ENABLE_MEMSYS3
18439#endif
18440#ifdef SQLITE_ENABLE_MEMSYS3
18441SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
18442#endif
18443
18444
18445#ifndef SQLITE_MUTEX_OMIT
18446SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
18447SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void);
18448SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
18449SQLITE_PRIVATE int sqlite3MutexInit(void);
18450SQLITE_PRIVATE int sqlite3MutexEnd(void);
18451#endif
18452#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
18453SQLITE_PRIVATE void sqlite3MemoryBarrier(void);
18454#else
18455# define sqlite3MemoryBarrier()
18456#endif
18457
18458SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int);
18459SQLITE_PRIVATE void sqlite3StatusUp(int, int);
18460SQLITE_PRIVATE void sqlite3StatusDown(int, int);
18461SQLITE_PRIVATE void sqlite3StatusHighwater(int, int);
18462SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3*,int*);
18463
18464/* Access to mutexes used by sqlite3_status() */
18465SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void);
18466SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void);
18467
18468#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
18469SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex*);
18470#else
18471# define sqlite3MutexWarnOnContention(x)
18472#endif
18473
18474#ifndef SQLITE_OMIT_FLOATING_POINT
18475SQLITE_PRIVATE int sqlite3IsNaN(double);
18476#else
18477# define sqlite3IsNaN(X) 0
18478#endif
18479
18480/*
18481** An instance of the following structure holds information about SQL
18482** functions arguments that are the parameters to the printf() function.
18483*/
18484struct PrintfArguments {
18485 int nArg; /* Total number of arguments */
18486 int nUsed; /* Number of arguments used so far */
18487 sqlite3_value **apArg; /* The argument values */
18488};
18489
18490SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
18491SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
18492#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
18493SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
18494#endif
18495#if defined(SQLITE_TEST)
18496SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
18497#endif
18498
18499#if defined(SQLITE_DEBUG)
18500SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
18501SQLITE_PRIVATE void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
18502SQLITE_PRIVATE void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
18503SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
18504SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView*, const With*, u8);
18505#ifndef SQLITE_OMIT_WINDOWFUNC
18506SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
18507SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
18508#endif
18509#endif
18510
18511
18512SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
18513SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
18514SQLITE_PRIVATE void sqlite3Dequote(char*);
18515SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
18516SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
18517SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
18518SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
18519SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
18520SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
18521SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
18522SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
18523SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
18524#ifdef SQLITE_DEBUG
18525SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse*,int,int);
18526#endif
18527SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
18528SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
18529SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
18530SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
18531SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
18532SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
18533SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int);
18534SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
18535SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
18536SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
18537SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
18538SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int);
18539SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
18540SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
18541SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
18542SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*);
18543SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
18544SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
18545SQLITE_PRIVATE int sqlite3InitOne(sqlite3*, int, char**, u32);
18546SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
18547#ifndef SQLITE_OMIT_VIRTUALTABLE
18548SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
18549#endif
18550SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*);
18551SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
18552SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*);
18553SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
18554SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3*,Table*);
18555SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
18556SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*);
18557SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
18558SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
18559SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
18560SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index*, i16);
18561SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
18562#if SQLITE_ENABLE_HIDDEN_COLUMNS
18563SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table*, Column*);
18564#else
18565# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
18566#endif
18567SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*,Token*);
18568SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
18569SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
18570SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
18571SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
18572SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
18573SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
18574SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
18575 sqlite3_vfs**,char**,char **);
18576SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
18577
18578#ifdef SQLITE_UNTESTABLE
18579# define sqlite3FaultSim(X) SQLITE_OK
18580#else
18581SQLITE_PRIVATE int sqlite3FaultSim(int);
18582#endif
18583
18584SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
18585SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
18586SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32);
18587SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
18588SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
18589SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
18590SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
18591#ifndef SQLITE_UNTESTABLE
18592SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
18593#endif
18594
18595SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*);
18596SQLITE_PRIVATE void sqlite3RowSetDelete(void*);
18597SQLITE_PRIVATE void sqlite3RowSetClear(void*);
18598SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
18599SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
18600SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
18601
18602SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
18603
18604#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
18605SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse*,Table*);
18606#else
18607# define sqlite3ViewGetColumnNames(A,B) 0
18608#endif
18609
18610#if SQLITE_MAX_ATTACHED>30
18611SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask);
18612#endif
18613SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
18614SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
18615SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
18616SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3*, Index*);
18617#ifndef SQLITE_OMIT_AUTOINCREMENT
18618SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
18619SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
18620#else
18621# define sqlite3AutoincrementBegin(X)
18622# define sqlite3AutoincrementEnd(X)
18623#endif
18624SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
18625SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
18626SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
18627SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
18628SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
18629SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
18630SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
18631 Token*, Select*, Expr*, IdList*);
18632SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
18633SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
18634SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
18635SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
18636SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
18637SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
18638SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
18639SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
18640SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
18641 Expr*, int, int, u8);
18642SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
18643SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
18644SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
18645 Expr*,ExprList*,u32,Expr*);
18646SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
18647SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
18648SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
18649SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
18650#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
18651SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
18652#endif
18653SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
18654SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
18655 Upsert*);
18656SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
18657SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
18658SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
18659SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
18660SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
18661SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
18662SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
18663SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
18664SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
18665SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
18666#define ONEPASS_OFF 0 /* Use of ONEPASS not allowed */
18667#define ONEPASS_SINGLE 1 /* ONEPASS valid for a single row update */
18668#define ONEPASS_MULTI 2 /* ONEPASS is valid for multiple rows */
18669SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
18670SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
18671SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
18672SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
18673SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
18674SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
18675SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
18676SQLITE_PRIVATE int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
18677SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
18678SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
18679SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
18680SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
18681#define SQLITE_ECEL_DUP 0x01 /* Deep, not shallow copies */
18682#define SQLITE_ECEL_FACTOR 0x02 /* Factor out constant terms */
18683#define SQLITE_ECEL_REF 0x04 /* Use ExprList.u.x.iOrderByCol */
18684#define SQLITE_ECEL_OMITREF 0x08 /* Omit if ExprList.u.x.iOrderByCol */
18685SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
18686SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
18687SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
18688SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
18689#define LOCATE_VIEW 0x01
18690#define LOCATE_NOERR 0x02
18691SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
18692SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,u32 flags,struct SrcList_item *);
18693SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
18694SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
18695SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
18696SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*);
18697SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int);
18698SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
18699SQLITE_PRIVATE int sqlite3ExprCompare(Parse*,Expr*, Expr*, int);
18700SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*, Expr*, int);
18701SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
18702SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int);
18703SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int);
18704SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
18705SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
18706SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
18707SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
18708SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
18709#ifndef SQLITE_UNTESTABLE
18710SQLITE_PRIVATE void sqlite3PrngSaveState(void);
18711SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
18712#endif
18713SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
18714SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
18715SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
18716SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
18717SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
18718SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
18719SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
18720SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
18721SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
18722SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
18723SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
18724SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
18725SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
18726SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
18727SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
18728#ifdef SQLITE_ENABLE_CURSOR_HINTS
18729SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
18730#endif
18731SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
18732SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
18733SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
18734SQLITE_PRIVATE int sqlite3IsRowid(const char*);
18735SQLITE_PRIVATE void sqlite3GenerateRowDelete(
18736 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
18737SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
18738SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
18739SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
18740SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
18741 u8,u8,int,int*,int*,Upsert*);
18742#ifdef SQLITE_ENABLE_NULL_TRIM
18743SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe*,Table*);
18744#else
18745# define sqlite3SetMakeRecordP5(A,B)
18746#endif
18747SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
18748SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
18749SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
18750SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
18751SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
18752SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
18753SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
18754SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
18755SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
18756SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
18757SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
18758SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
18759SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
18760SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
18761SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
18762SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
18763SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
18764SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
18765SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
18766SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
18767SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
18768
18769#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
18770SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
18771#endif
18772
18773#ifndef SQLITE_OMIT_TRIGGER
18774SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
18775 Expr*,int, int);
18776SQLITE_PRIVATE void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
18777SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int);
18778SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse*, Trigger*);
18779SQLITE_PRIVATE Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
18780SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *, Table *);
18781SQLITE_PRIVATE void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
18782 int, int, int);
18783SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
18784 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
18785SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
18786SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
18787 const char*,const char*);
18788SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
18789 Select*,u8,Upsert*,
18790 const char*,const char*);
18791SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,ExprList*, Expr*, u8,
18792 const char*,const char*);
18793SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
18794 const char*,const char*);
18795SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3*, Trigger*);
18796SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
18797SQLITE_PRIVATE u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
18798# define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
18799# define sqlite3IsToplevel(p) ((p)->pToplevel==0)
18800#else
18801# define sqlite3TriggersExist(B,C,D,E,F) 0
18802# define sqlite3DeleteTrigger(A,B)
18803# define sqlite3DropTriggerPtr(A,B)
18804# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
18805# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
18806# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
18807# define sqlite3TriggerList(X, Y) 0
18808# define sqlite3ParseToplevel(p) p
18809# define sqlite3IsToplevel(p) 1
18810# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
18811#endif
18812
18813SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
18814SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
18815SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
18816#ifndef SQLITE_OMIT_AUTHORIZATION
18817SQLITE_PRIVATE void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
18818SQLITE_PRIVATE int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
18819SQLITE_PRIVATE void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
18820SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext*);
18821SQLITE_PRIVATE int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
18822#else
18823# define sqlite3AuthRead(a,b,c,d)
18824# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
18825# define sqlite3AuthContextPush(a,b,c)
18826# define sqlite3AuthContextPop(a) ((void)(a))
18827#endif
18828SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
18829SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
18830SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
18831SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
18832SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
18833SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
18834SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
18835SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
18836SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
18837SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
18838SQLITE_PRIVATE int sqlite3Atoi(const char*);
18839#ifndef SQLITE_OMIT_UTF16
18840SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
18841#endif
18842SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
18843SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
18844SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
18845SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
18846#ifndef SQLITE_OMIT_VIRTUALTABLE
18847SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
18848#endif
18849#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
18850 defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
18851 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
18852SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
18853#endif
18854SQLITE_PRIVATE VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
18855SQLITE_PRIVATE const char *sqlite3VListNumToName(VList*,int);
18856SQLITE_PRIVATE int sqlite3VListNameToNum(VList*,const char*,int);
18857
18858/*
18859** Routines to read and write variable-length integers. These used to
18860** be defined locally, but now we use the varint routines in the util.c
18861** file.
18862*/
18863SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
18864SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
18865SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
18866SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
18867
18868/*
18869** The common case is for a varint to be a single byte. They following
18870** macros handle the common case without a procedure call, but then call
18871** the procedure for larger varints.
18872*/
18873#define getVarint32(A,B) \
18874 (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
18875#define putVarint32(A,B) \
18876 (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
18877 sqlite3PutVarint((A),(B)))
18878#define getVarint sqlite3GetVarint
18879#define putVarint sqlite3PutVarint
18880
18881
18882SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
18883SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
18884SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
18885SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
18886SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table*,int);
18887SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
18888SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
18889SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
18890SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
18891SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
18892SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
18893SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
18894SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
18895SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
18896
18897#if defined(SQLITE_NEED_ERR_NAME)
18898SQLITE_PRIVATE const char *sqlite3ErrName(int);
18899#endif
18900
18901#ifdef SQLITE_ENABLE_DESERIALIZE
18902SQLITE_PRIVATE int sqlite3MemdbInit(void);
18903#endif
18904
18905SQLITE_PRIVATE const char *sqlite3ErrStr(int);
18906SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
18907SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
18908SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq*);
18909SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
18910SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
18911SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
18912SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*);
18913SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
18914SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
18915SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
18916SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
18917SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
18918SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
18919SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
18920SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
18921SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
18922SQLITE_PRIVATE int sqlite3AbsInt32(int);
18923#ifdef SQLITE_ENABLE_8_3_NAMES
18924SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
18925#else
18926# define sqlite3FileSuffix3(X,Y)
18927#endif
18928SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8);
18929
18930SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
18931SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
18932SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
18933 void(*)(void*));
18934SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
18935SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
18936SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
18937#ifndef SQLITE_OMIT_UTF16
18938SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
18939#endif
18940SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
18941SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
18942#ifndef SQLITE_AMALGAMATION
18943SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
18944SQLITE_PRIVATE const char sqlite3StrBINARY[];
18945SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
18946SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
18947SQLITE_PRIVATE const Token sqlite3IntTokens[];
18948SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
18949SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
18950#ifndef SQLITE_OMIT_WSD
18951SQLITE_PRIVATE int sqlite3PendingByte;
18952#endif
18953#endif
18954#ifdef VDBE_PROFILE
18955SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt;
18956#endif
18957SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
18958SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
18959SQLITE_PRIVATE void sqlite3AlterFunctions(void);
18960SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
18961SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
18962SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
18963SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
18964SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
18965SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr *, int, int);
18966SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
18967SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
18968SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
18969SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
18970SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
18971SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
18972SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
18973SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
18974SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
18975SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
18976SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
18977SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse*, void*, Token*);
18978SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse*, void *pTo, void *pFrom);
18979SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse*, Expr*);
18980SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
18981SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
18982SQLITE_PRIVATE char sqlite3AffinityType(const char*, Column*);
18983SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
18984SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
18985SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
18986SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
18987SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
18988SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
18989SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
18990SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
18991SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
18992SQLITE_PRIVATE void sqlite3SchemaClear(void *);
18993SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
18994SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
18995SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
18996SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
18997SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
18998SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
18999SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
19000
19001#ifdef SQLITE_DEBUG
19002SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
19003#endif
19004SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
19005 void (*)(sqlite3_context*,int,sqlite3_value **),
19006 void (*)(sqlite3_context*,int,sqlite3_value **),
19007 void (*)(sqlite3_context*),
19008 void (*)(sqlite3_context*),
19009 void (*)(sqlite3_context*,int,sqlite3_value **),
19010 FuncDestructor *pDestructor
19011);
19012SQLITE_PRIVATE void sqlite3NoopDestructor(void*);
19013SQLITE_PRIVATE void sqlite3OomFault(sqlite3*);
19014SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
19015SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
19016SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
19017
19018SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
19019SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
19020SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
19021SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
19022
19023SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
19024SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
19025
19026#ifndef SQLITE_OMIT_SUBQUERY
19027SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse*, Expr*);
19028#else
19029# define sqlite3ExprCheckIN(x,y) SQLITE_OK
19030#endif
19031
19032#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19033SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
19034SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
19035 Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
19036SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
19037SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
19038SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
19039SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
19040#endif
19041
19042/*
19043** The interface to the LEMON-generated parser
19044*/
19045#ifndef SQLITE_AMALGAMATION
19046SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
19047SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
19048#endif
19049SQLITE_PRIVATE void sqlite3Parser(void*, int, Token);
19050SQLITE_PRIVATE int sqlite3ParserFallback(int);
19051#ifdef YYTRACKMAXSTACKDEPTH
19052SQLITE_PRIVATE int sqlite3ParserStackPeak(void*);
19053#endif
19054
19055SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
19056#ifndef SQLITE_OMIT_LOAD_EXTENSION
19057SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3*);
19058#else
19059# define sqlite3CloseExtensions(X)
19060#endif
19061
19062#ifndef SQLITE_OMIT_SHARED_CACHE
19063SQLITE_PRIVATE void sqlite3TableLock(Parse *, int, int, u8, const char *);
19064#else
19065 #define sqlite3TableLock(v,w,x,y,z)
19066#endif
19067
19068#ifdef SQLITE_TEST
19069SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char*);
19070#endif
19071
19072#ifdef SQLITE_OMIT_VIRTUALTABLE
19073# define sqlite3VtabClear(Y)
19074# define sqlite3VtabSync(X,Y) SQLITE_OK
19075# define sqlite3VtabRollback(X)
19076# define sqlite3VtabCommit(X)
19077# define sqlite3VtabInSync(db) 0
19078# define sqlite3VtabLock(X)
19079# define sqlite3VtabUnlock(X)
19080# define sqlite3VtabUnlockList(X)
19081# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
19082# define sqlite3GetVTable(X,Y) ((VTable*)0)
19083#else
19084SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*);
19085SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
19086SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe*);
19087SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
19088SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
19089SQLITE_PRIVATE void sqlite3VtabLock(VTable *);
19090SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *);
19091SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*);
19092SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int);
19093SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
19094SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
19095SQLITE_PRIVATE Module *sqlite3VtabCreateModule(
19096 sqlite3*,
19097 const char*,
19098 const sqlite3_module*,
19099 void*,
19100 void(*)(void*)
19101 );
19102# define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
19103#endif
19104SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*);
19105SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
19106SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
19107SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
19108SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
19109SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
19110SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
19111SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
19112SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
19113SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
19114SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
19115SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
19116SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
19117SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
19118SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
19119SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
19120SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
19121SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
19122SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
19123SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
19124SQLITE_PRIVATE const char *sqlite3JournalModename(int);
19125#ifndef SQLITE_OMIT_WAL
19126SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
19127SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
19128#endif
19129#ifndef SQLITE_OMIT_CTE
19130SQLITE_PRIVATE With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
19131SQLITE_PRIVATE void sqlite3WithDelete(sqlite3*,With*);
19132SQLITE_PRIVATE void sqlite3WithPush(Parse*, With*, u8);
19133#else
19134#define sqlite3WithPush(x,y,z)
19135#define sqlite3WithDelete(x,y)
19136#endif
19137#ifndef SQLITE_OMIT_UPSERT
19138SQLITE_PRIVATE Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*);
19139SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3*,Upsert*);
19140SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
19141SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
19142SQLITE_PRIVATE void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
19143#else
19144#define sqlite3UpsertNew(v,w,x,y,z) ((Upsert*)0)
19145#define sqlite3UpsertDelete(x,y)
19146#define sqlite3UpsertDup(x,y) ((Upsert*)0)
19147#endif
19148
19149
19150/* Declarations for functions in fkey.c. All of these are replaced by
19151** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
19152** key functionality is available. If OMIT_TRIGGER is defined but
19153** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
19154** this case foreign keys are parsed, but no other functionality is
19155** provided (enforcement of FK constraints requires the triggers sub-system).
19156*/
19157#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
19158SQLITE_PRIVATE void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
19159SQLITE_PRIVATE void sqlite3FkDropTable(Parse*, SrcList *, Table*);
19160SQLITE_PRIVATE void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
19161SQLITE_PRIVATE int sqlite3FkRequired(Parse*, Table*, int*, int);
19162SQLITE_PRIVATE u32 sqlite3FkOldmask(Parse*, Table*);
19163SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *);
19164#else
19165 #define sqlite3FkActions(a,b,c,d,e,f)
19166 #define sqlite3FkCheck(a,b,c,d,e,f)
19167 #define sqlite3FkDropTable(a,b,c)
19168 #define sqlite3FkOldmask(a,b) 0
19169 #define sqlite3FkRequired(a,b,c,d) 0
19170 #define sqlite3FkReferences(a) 0
19171#endif
19172#ifndef SQLITE_OMIT_FOREIGN_KEY
19173SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*);
19174SQLITE_PRIVATE int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
19175#else
19176 #define sqlite3FkDelete(a,b)
19177 #define sqlite3FkLocateIndex(a,b,c,d,e)
19178#endif
19179
19180
19181/*
19182** Available fault injectors. Should be numbered beginning with 0.
19183*/
19184#define SQLITE_FAULTINJECTOR_MALLOC 0
19185#define SQLITE_FAULTINJECTOR_COUNT 1
19186
19187/*
19188** The interface to the code in fault.c used for identifying "benign"
19189** malloc failures. This is only present if SQLITE_UNTESTABLE
19190** is not defined.
19191*/
19192#ifndef SQLITE_UNTESTABLE
19193SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void);
19194SQLITE_PRIVATE void sqlite3EndBenignMalloc(void);
19195#else
19196 #define sqlite3BeginBenignMalloc()
19197 #define sqlite3EndBenignMalloc()
19198#endif
19199
19200/*
19201** Allowed return values from sqlite3FindInIndex()
19202*/
19203#define IN_INDEX_ROWID 1 /* Search the rowid of the table */
19204#define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
19205#define IN_INDEX_INDEX_ASC 3 /* Existing index ASCENDING */
19206#define IN_INDEX_INDEX_DESC 4 /* Existing index DESCENDING */
19207#define IN_INDEX_NOOP 5 /* No table available. Use comparisons */
19208/*
19209** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
19210*/
19211#define IN_INDEX_NOOP_OK 0x0001 /* OK to return IN_INDEX_NOOP */
19212#define IN_INDEX_MEMBERSHIP 0x0002 /* IN operator used for membership test */
19213#define IN_INDEX_LOOP 0x0004 /* IN operator used as a loop */
19214SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*);
19215
19216SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
19217SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
19218#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
19219 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
19220SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *);
19221#endif
19222
19223SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p);
19224SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
19225
19226SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
19227#if SQLITE_MAX_EXPR_DEPTH>0
19228SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *);
19229SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse*, int);
19230#else
19231 #define sqlite3SelectExprHeight(x) 0
19232 #define sqlite3ExprCheckHeight(x,y)
19233#endif
19234
19235SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
19236SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
19237
19238#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
19239SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
19240SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db);
19241SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db);
19242#else
19243 #define sqlite3ConnectionBlocked(x,y)
19244 #define sqlite3ConnectionUnlocked(x)
19245 #define sqlite3ConnectionClosed(x)
19246#endif
19247
19248#ifdef SQLITE_DEBUG
19249SQLITE_PRIVATE void sqlite3ParserTrace(FILE*, char *);
19250#endif
19251#if defined(YYCOVERAGE)
19252SQLITE_PRIVATE int sqlite3ParserCoverage(FILE*);
19253#endif
19254
19255/*
19256** If the SQLITE_ENABLE IOTRACE exists then the global variable
19257** sqlite3IoTrace is a pointer to a printf-like routine used to
19258** print I/O tracing messages.
19259*/
19260#ifdef SQLITE_ENABLE_IOTRACE
19261# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
19262SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe*);
19263SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
19264#else
19265# define IOTRACE(A)
19266# define sqlite3VdbeIOTraceSql(X)
19267#endif
19268
19269/*
19270** These routines are available for the mem2.c debugging memory allocator
19271** only. They are used to verify that different "types" of memory
19272** allocations are properly tracked by the system.
19273**
19274** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
19275** the MEMTYPE_* macros defined below. The type must be a bitmask with
19276** a single bit set.
19277**
19278** sqlite3MemdebugHasType() returns true if any of the bits in its second
19279** argument match the type set by the previous sqlite3MemdebugSetType().
19280** sqlite3MemdebugHasType() is intended for use inside assert() statements.
19281**
19282** sqlite3MemdebugNoType() returns true if none of the bits in its second
19283** argument match the type set by the previous sqlite3MemdebugSetType().
19284**
19285** Perhaps the most important point is the difference between MEMTYPE_HEAP
19286** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means
19287** it might have been allocated by lookaside, except the allocation was
19288** too large or lookaside was already full. It is important to verify
19289** that allocations that might have been satisfied by lookaside are not
19290** passed back to non-lookaside free() routines. Asserts such as the
19291** example above are placed on the non-lookaside free() routines to verify
19292** this constraint.
19293**
19294** All of this is no-op for a production build. It only comes into
19295** play when the SQLITE_MEMDEBUG compile-time option is used.
19296*/
19297#ifdef SQLITE_MEMDEBUG
19298SQLITE_PRIVATE void sqlite3MemdebugSetType(void*,u8);
19299SQLITE_PRIVATE int sqlite3MemdebugHasType(void*,u8);
19300SQLITE_PRIVATE int sqlite3MemdebugNoType(void*,u8);
19301#else
19302# define sqlite3MemdebugSetType(X,Y) /* no-op */
19303# define sqlite3MemdebugHasType(X,Y) 1
19304# define sqlite3MemdebugNoType(X,Y) 1
19305#endif
19306#define MEMTYPE_HEAP 0x01 /* General heap allocations */
19307#define MEMTYPE_LOOKASIDE 0x02 /* Heap that might have been lookaside */
19308#define MEMTYPE_PCACHE 0x04 /* Page cache allocations */
19309
19310/*
19311** Threading interface
19312*/
19313#if SQLITE_MAX_WORKER_THREADS>0
19314SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
19315SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
19316#endif
19317
19318#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
19319SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3*);
19320#endif
19321#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
19322SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
19323#endif
19324
19325SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr);
19326SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr);
19327SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr*, int);
19328SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(Parse*,Expr*,int);
19329SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*);
19330
19331#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
19332SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt);
19333#endif
19334
19335#endif /* SQLITEINT_H */
19336
19337/************** End of sqliteInt.h *******************************************/
19338/************** Begin file global.c ******************************************/
19339/*
19340** 2008 June 13
19341**
19342** The author disclaims copyright to this source code. In place of
19343** a legal notice, here is a blessing:
19344**
19345** May you do good and not evil.
19346** May you find forgiveness for yourself and forgive others.
19347** May you share freely, never taking more than you give.
19348**
19349*************************************************************************
19350**
19351** This file contains definitions of global variables and constants.
19352*/
19353/* #include "sqliteInt.h" */
19354
19355/* An array to map all upper-case characters into their corresponding
19356** lower-case character.
19357**
19358** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
19359** handle case conversions for the UTF character set since the tables
19360** involved are nearly as big or bigger than SQLite itself.
19361*/
19362SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
19363#ifdef SQLITE_ASCII
19364 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
19365 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
19366 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
19367 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
19368 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
19369 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
19370 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
19371 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
19372 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
19373 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
19374 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
19375 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
19376 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
19377 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
19378 252,253,254,255
19379#endif
19380#ifdef SQLITE_EBCDIC
19381 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
19382 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
19383 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
19384 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
19385 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
19386 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
19387 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 6x */
19388 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 7x */
19389 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
19390 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 9x */
19391 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
19392 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
19393 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
19394 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
19395 224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */
19396 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */
19397#endif
19398};
19399
19400/*
19401** The following 256 byte lookup table is used to support SQLites built-in
19402** equivalents to the following standard library functions:
19403**
19404** isspace() 0x01
19405** isalpha() 0x02
19406** isdigit() 0x04
19407** isalnum() 0x06
19408** isxdigit() 0x08
19409** toupper() 0x20
19410** SQLite identifier character 0x40
19411** Quote character 0x80
19412**
19413** Bit 0x20 is set if the mapped character requires translation to upper
19414** case. i.e. if the character is a lower-case ASCII character.
19415** If x is a lower-case ASCII character, then its upper-case equivalent
19416** is (x - 0x20). Therefore toupper() can be implemented as:
19417**
19418** (x & ~(map[x]&0x20))
19419**
19420** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
19421** array. tolower() is used more often than toupper() by SQLite.
19422**
19423** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
19424** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any
19425** non-ASCII UTF character. Hence the test for whether or not a character is
19426** part of an identifier is 0x46.
19427*/
19428#ifdef SQLITE_ASCII
19429SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
19430 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */
19431 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */
19432 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */
19433 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */
19434 0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80, /* 20..27 !"#$%&' */
19435 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
19436 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */
19437 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */
19438
19439 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */
19440 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */
19441 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */
19442 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */
19443 0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */
19444 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */
19445 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */
19446 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */
19447
19448 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 80..87 ........ */
19449 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 88..8f ........ */
19450 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 90..97 ........ */
19451 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 98..9f ........ */
19452 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a0..a7 ........ */
19453 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a8..af ........ */
19454 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b0..b7 ........ */
19455 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b8..bf ........ */
19456
19457 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c0..c7 ........ */
19458 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c8..cf ........ */
19459 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d0..d7 ........ */
19460 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d8..df ........ */
19461 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e0..e7 ........ */
19462 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e8..ef ........ */
19463 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
19464 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
19465};
19466#endif
19467
19468/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
19469** compatibility for legacy applications, the URI filename capability is
19470** disabled by default.
19471**
19472** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
19473** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
19474**
19475** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
19476** disabled. The default value may be changed by compiling with the
19477** SQLITE_USE_URI symbol defined.
19478**
19479** URI filenames are enabled by default if SQLITE_HAS_CODEC is
19480** enabled.
19481*/
19482#ifndef SQLITE_USE_URI
19483# ifdef SQLITE_HAS_CODEC
19484# define SQLITE_USE_URI 1
19485# else
19486# define SQLITE_USE_URI 0
19487# endif
19488#endif
19489
19490/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
19491** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
19492** that compile-time option is omitted.
19493*/
19494#ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
19495# define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
19496#endif
19497
19498/* The minimum PMA size is set to this value multiplied by the database
19499** page size in bytes.
19500*/
19501#ifndef SQLITE_SORTER_PMASZ
19502# define SQLITE_SORTER_PMASZ 250
19503#endif
19504
19505/* Statement journals spill to disk when their size exceeds the following
19506** threshold (in bytes). 0 means that statement journals are created and
19507** written to disk immediately (the default behavior for SQLite versions
19508** before 3.12.0). -1 means always keep the entire statement journal in
19509** memory. (The statement journal is also always held entirely in memory
19510** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
19511** setting.)
19512*/
19513#ifndef SQLITE_STMTJRNL_SPILL
19514# define SQLITE_STMTJRNL_SPILL (64*1024)
19515#endif
19516
19517/*
19518** The default lookaside-configuration, the format "SZ,N". SZ is the
19519** number of bytes in each lookaside slot (should be a multiple of 8)
19520** and N is the number of slots. The lookaside-configuration can be
19521** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
19522** or at run-time for an individual database connection using
19523** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
19524*/
19525#ifndef SQLITE_DEFAULT_LOOKASIDE
19526# define SQLITE_DEFAULT_LOOKASIDE 1200,100
19527#endif
19528
19529
19530/*
19531** The following singleton contains the global configuration for
19532** the SQLite library.
19533*/
19534SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
19535 SQLITE_DEFAULT_MEMSTATUS, /* bMemstat */
19536 1, /* bCoreMutex */
19537 SQLITE_THREADSAFE==1, /* bFullMutex */
19538 SQLITE_USE_URI, /* bOpenUri */
19539 SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
19540 0, /* bSmallMalloc */
19541 0x7ffffffe, /* mxStrlen */
19542 0, /* neverCorrupt */
19543 SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */
19544 SQLITE_STMTJRNL_SPILL, /* nStmtSpill */
19545 {0,0,0,0,0,0,0,0}, /* m */
19546 {0,0,0,0,0,0,0,0,0}, /* mutex */
19547 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
19548 (void*)0, /* pHeap */
19549 0, /* nHeap */
19550 0, 0, /* mnHeap, mxHeap */
19551 SQLITE_DEFAULT_MMAP_SIZE, /* szMmap */
19552 SQLITE_MAX_MMAP_SIZE, /* mxMmap */
19553 (void*)0, /* pPage */
19554 0, /* szPage */
19555 SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */
19556 0, /* mxParserStack */
19557 0, /* sharedCacheEnabled */
19558 SQLITE_SORTER_PMASZ, /* szPma */
19559 /* All the rest should always be initialized to zero */
19560 0, /* isInit */
19561 0, /* inProgress */
19562 0, /* isMutexInit */
19563 0, /* isMallocInit */
19564 0, /* isPCacheInit */
19565 0, /* nRefInitMutex */
19566 0, /* pInitMutex */
19567 0, /* xLog */
19568 0, /* pLogArg */
19569#ifdef SQLITE_ENABLE_SQLLOG
19570 0, /* xSqllog */
19571 0, /* pSqllogArg */
19572#endif
19573#ifdef SQLITE_VDBE_COVERAGE
19574 0, /* xVdbeBranch */
19575 0, /* pVbeBranchArg */
19576#endif
19577#ifndef SQLITE_UNTESTABLE
19578 0, /* xTestCallback */
19579#endif
19580 0, /* bLocaltimeFault */
19581 0x7ffffffe, /* iOnceResetThreshold */
19582 SQLITE_DEFAULT_SORTERREF_SIZE /* szSorterRef */
19583};
19584
19585/*
19586** Hash table for global functions - functions common to all
19587** database connections. After initialization, this table is
19588** read-only.
19589*/
19590SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
19591
19592/*
19593** Constant tokens for values 0 and 1.
19594*/
19595SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
19596 { "0", 1 },
19597 { "1", 1 }
19598};
19599
19600#ifdef VDBE_PROFILE
19601/*
19602** The following performance counter can be used in place of
19603** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
19604*/
19605SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt = 0;
19606#endif
19607
19608/*
19609** The value of the "pending" byte must be 0x40000000 (1 byte past the
19610** 1-gibabyte boundary) in a compatible database. SQLite never uses
19611** the database page that contains the pending byte. It never attempts
19612** to read or write that page. The pending byte page is set aside
19613** for use by the VFS layers as space for managing file locks.
19614**
19615** During testing, it is often desirable to move the pending byte to
19616** a different position in the file. This allows code that has to
19617** deal with the pending byte to run on files that are much smaller
19618** than 1 GiB. The sqlite3_test_control() interface can be used to
19619** move the pending byte.
19620**
19621** IMPORTANT: Changing the pending byte to any value other than
19622** 0x40000000 results in an incompatible database file format!
19623** Changing the pending byte during operation will result in undefined
19624** and incorrect behavior.
19625*/
19626#ifndef SQLITE_OMIT_WSD
19627SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
19628#endif
19629
19630/* #include "opcodes.h" */
19631/*
19632** Properties of opcodes. The OPFLG_INITIALIZER macro is
19633** created by mkopcodeh.awk during compilation. Data is obtained
19634** from the comments following the "case OP_xxxx:" statements in
19635** the vdbe.c file.
19636*/
19637SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
19638
19639/*
19640** Name of the default collating sequence
19641*/
19642SQLITE_PRIVATE const char sqlite3StrBINARY[] = "BINARY";
19643
19644/************** End of global.c **********************************************/
19645/************** Begin file status.c ******************************************/
19646/*
19647** 2008 June 18
19648**
19649** The author disclaims copyright to this source code. In place of
19650** a legal notice, here is a blessing:
19651**
19652** May you do good and not evil.
19653** May you find forgiveness for yourself and forgive others.
19654** May you share freely, never taking more than you give.
19655**
19656*************************************************************************
19657**
19658** This module implements the sqlite3_status() interface and related
19659** functionality.
19660*/
19661/* #include "sqliteInt.h" */
19662/************** Include vdbeInt.h in the middle of status.c ******************/
19663/************** Begin file vdbeInt.h *****************************************/
19664/*
19665** 2003 September 6
19666**
19667** The author disclaims copyright to this source code. In place of
19668** a legal notice, here is a blessing:
19669**
19670** May you do good and not evil.
19671** May you find forgiveness for yourself and forgive others.
19672** May you share freely, never taking more than you give.
19673**
19674*************************************************************************
19675** This is the header file for information that is private to the
19676** VDBE. This information used to all be at the top of the single
19677** source code file "vdbe.c". When that file became too big (over
19678** 6000 lines long) it was split up into several smaller files and
19679** this header information was factored out.
19680*/
19681#ifndef SQLITE_VDBEINT_H
19682#define SQLITE_VDBEINT_H
19683
19684/*
19685** The maximum number of times that a statement will try to reparse
19686** itself before giving up and returning SQLITE_SCHEMA.
19687*/
19688#ifndef SQLITE_MAX_SCHEMA_RETRY
19689# define SQLITE_MAX_SCHEMA_RETRY 50
19690#endif
19691
19692/*
19693** VDBE_DISPLAY_P4 is true or false depending on whether or not the
19694** "explain" P4 display logic is enabled.
19695*/
19696#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
19697 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
19698# define VDBE_DISPLAY_P4 1
19699#else
19700# define VDBE_DISPLAY_P4 0
19701#endif
19702
19703/*
19704** SQL is translated into a sequence of instructions to be
19705** executed by a virtual machine. Each instruction is an instance
19706** of the following structure.
19707*/
19708typedef struct VdbeOp Op;
19709
19710/*
19711** Boolean values
19712*/
19713typedef unsigned Bool;
19714
19715/* Opaque type used by code in vdbesort.c */
19716typedef struct VdbeSorter VdbeSorter;
19717
19718/* Elements of the linked list at Vdbe.pAuxData */
19719typedef struct AuxData AuxData;
19720
19721/* Types of VDBE cursors */
19722#define CURTYPE_BTREE 0
19723#define CURTYPE_SORTER 1
19724#define CURTYPE_VTAB 2
19725#define CURTYPE_PSEUDO 3
19726
19727/*
19728** A VdbeCursor is an superclass (a wrapper) for various cursor objects:
19729**
19730** * A b-tree cursor
19731** - In the main database or in an ephemeral database
19732** - On either an index or a table
19733** * A sorter
19734** * A virtual table
19735** * A one-row "pseudotable" stored in a single register
19736*/
19737typedef struct VdbeCursor VdbeCursor;
19738struct VdbeCursor {
19739 u8 eCurType; /* One of the CURTYPE_* values above */
19740 i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
19741 u8 nullRow; /* True if pointing to a row with no data */
19742 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
19743 u8 isTable; /* True for rowid tables. False for indexes */
19744#ifdef SQLITE_DEBUG
19745 u8 seekOp; /* Most recent seek operation on this cursor */
19746 u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */
19747#endif
19748 Bool isEphemeral:1; /* True for an ephemeral table */
19749 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
19750 Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */
19751 Bool seekHit:1; /* See the OP_SeekHit and OP_IfNoHope opcodes */
19752 Btree *pBtx; /* Separate file holding temporary table */
19753 i64 seqCount; /* Sequence counter */
19754 int *aAltMap; /* Mapping from table to index column numbers */
19755
19756 /* Cached OP_Column parse information is only valid if cacheStatus matches
19757 ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of
19758 ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that
19759 ** the cache is out of date. */
19760 u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */
19761 int seekResult; /* Result of previous sqlite3BtreeMoveto() or 0
19762 ** if there have been no prior seeks on the cursor. */
19763 /* seekResult does not distinguish between "no seeks have ever occurred
19764 ** on this cursor" and "the most recent seek was an exact match".
19765 ** For CURTYPE_PSEUDO, seekResult is the register holding the record */
19766
19767 /* When a new VdbeCursor is allocated, only the fields above are zeroed.
19768 ** The fields that follow are uninitialized, and must be individually
19769 ** initialized prior to first use. */
19770 VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
19771 union {
19772 BtCursor *pCursor; /* CURTYPE_BTREE or _PSEUDO. Btree cursor */
19773 sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB. Vtab cursor */
19774 VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */
19775 } uc;
19776 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
19777 u32 iHdrOffset; /* Offset to next unparsed byte of the header */
19778 Pgno pgnoRoot; /* Root page of the open btree cursor */
19779 i16 nField; /* Number of fields in the header */
19780 u16 nHdrParsed; /* Number of header fields parsed so far */
19781 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
19782 u32 *aOffset; /* Pointer to aType[nField] */
19783 const u8 *aRow; /* Data for the current row, if all on one page */
19784 u32 payloadSize; /* Total number of bytes in the record */
19785 u32 szRow; /* Byte available in aRow */
19786#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
19787 u64 maskUsed; /* Mask of columns used by this cursor */
19788#endif
19789
19790 /* 2*nField extra array elements allocated for aType[], beyond the one
19791 ** static element declared in the structure. nField total array slots for
19792 ** aType[] and nField+1 array slots for aOffset[] */
19793 u32 aType[1]; /* Type values record decode. MUST BE LAST */
19794};
19795
19796
19797/*
19798** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
19799*/
19800#define CACHE_STALE 0
19801
19802/*
19803** When a sub-program is executed (OP_Program), a structure of this type
19804** is allocated to store the current value of the program counter, as
19805** well as the current memory cell array and various other frame specific
19806** values stored in the Vdbe struct. When the sub-program is finished,
19807** these values are copied back to the Vdbe from the VdbeFrame structure,
19808** restoring the state of the VM to as it was before the sub-program
19809** began executing.
19810**
19811** The memory for a VdbeFrame object is allocated and managed by a memory
19812** cell in the parent (calling) frame. When the memory cell is deleted or
19813** overwritten, the VdbeFrame object is not freed immediately. Instead, it
19814** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
19815** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
19816** this instead of deleting the VdbeFrame immediately is to avoid recursive
19817** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
19818** child frame are released.
19819**
19820** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
19821** set to NULL if the currently executing frame is the main program.
19822*/
19823typedef struct VdbeFrame VdbeFrame;
19824struct VdbeFrame {
19825 Vdbe *v; /* VM this frame belongs to */
19826 VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
19827 Op *aOp; /* Program instructions for parent frame */
19828 i64 *anExec; /* Event counters from parent frame */
19829 Mem *aMem; /* Array of memory cells for parent frame */
19830 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
19831 u8 *aOnce; /* Bitmask used by OP_Once */
19832 void *token; /* Copy of SubProgram.token */
19833 i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
19834 AuxData *pAuxData; /* Linked list of auxdata allocations */
19835#if SQLITE_DEBUG
19836 u32 iFrameMagic; /* magic number for sanity checking */
19837#endif
19838 int nCursor; /* Number of entries in apCsr */
19839 int pc; /* Program Counter in parent (calling) frame */
19840 int nOp; /* Size of aOp array */
19841 int nMem; /* Number of entries in aMem */
19842 int nChildMem; /* Number of memory cells for child frame */
19843 int nChildCsr; /* Number of cursors for child frame */
19844 int nChange; /* Statement changes (Vdbe.nChange) */
19845 int nDbChange; /* Value of db->nChange */
19846};
19847
19848/* Magic number for sanity checking on VdbeFrame objects */
19849#define SQLITE_FRAME_MAGIC 0x879fb71e
19850
19851/*
19852** Return a pointer to the array of registers allocated for use
19853** by a VdbeFrame.
19854*/
19855#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
19856
19857/*
19858** Internally, the vdbe manipulates nearly all SQL values as Mem
19859** structures. Each Mem struct may cache multiple representations (string,
19860** integer etc.) of the same value.
19861*/
19862struct sqlite3_value {
19863 union MemValue {
19864 double r; /* Real value used when MEM_Real is set in flags */
19865 i64 i; /* Integer value used when MEM_Int is set in flags */
19866 int nZero; /* Extra zero bytes when MEM_Zero and MEM_Blob set */
19867 const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */
19868 FuncDef *pDef; /* Used only when flags==MEM_Agg */
19869 } u;
19870 u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
19871 u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
19872 u8 eSubtype; /* Subtype for this value */
19873 int n; /* Number of characters in string value, excluding '\0' */
19874 char *z; /* String or BLOB value */
19875 /* ShallowCopy only needs to copy the information above */
19876 char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
19877 int szMalloc; /* Size of the zMalloc allocation */
19878 u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */
19879 sqlite3 *db; /* The associated database connection */
19880 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
19881#ifdef SQLITE_DEBUG
19882 Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
19883 u16 mScopyFlags; /* flags value immediately after the shallow copy */
19884#endif
19885};
19886
19887/*
19888** Size of struct Mem not including the Mem.zMalloc member or anything that
19889** follows.
19890*/
19891#define MEMCELLSIZE offsetof(Mem,zMalloc)
19892
19893/* One or more of the following flags are set to indicate the validOK
19894** representations of the value stored in the Mem struct.
19895**
19896** If the MEM_Null flag is set, then the value is an SQL NULL value.
19897** For a pointer type created using sqlite3_bind_pointer() or
19898** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
19899**
19900** If the MEM_Str flag is set then Mem.z points at a string representation.
19901** Usually this is encoded in the same unicode encoding as the main
19902** database (see below for exceptions). If the MEM_Term flag is also
19903** set, then the string is nul terminated. The MEM_Int and MEM_Real
19904** flags may coexist with the MEM_Str flag.
19905*/
19906#define MEM_Null 0x0001 /* Value is NULL (or a pointer) */
19907#define MEM_Str 0x0002 /* Value is a string */
19908#define MEM_Int 0x0004 /* Value is an integer */
19909#define MEM_Real 0x0008 /* Value is a real number */
19910#define MEM_Blob 0x0010 /* Value is a BLOB */
19911#define MEM_AffMask 0x001f /* Mask of affinity bits */
19912/* Available 0x0020 */
19913/* Available 0x0040 */
19914#define MEM_Undefined 0x0080 /* Value is undefined */
19915#define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */
19916#define MEM_TypeMask 0xc1ff /* Mask of type bits */
19917
19918
19919/* Whenever Mem contains a valid string or blob representation, one of
19920** the following flags must be set to determine the memory management
19921** policy for Mem.z. The MEM_Term flag tells us whether or not the
19922** string is \000 or \u0000 terminated
19923*/
19924#define MEM_Term 0x0200 /* String in Mem.z is zero terminated */
19925#define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */
19926#define MEM_Static 0x0800 /* Mem.z points to a static string */
19927#define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
19928#define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
19929#define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
19930#define MEM_Subtype 0x8000 /* Mem.eSubtype is valid */
19931#ifdef SQLITE_OMIT_INCRBLOB
19932 #undef MEM_Zero
19933 #define MEM_Zero 0x0000
19934#endif
19935
19936/* Return TRUE if Mem X contains dynamically allocated content - anything
19937** that needs to be deallocated to avoid a leak.
19938*/
19939#define VdbeMemDynamic(X) \
19940 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
19941
19942/*
19943** Clear any existing type flags from a Mem and replace them with f
19944*/
19945#define MemSetTypeFlag(p, f) \
19946 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
19947
19948/*
19949** Return true if a memory cell is not marked as invalid. This macro
19950** is for use inside assert() statements only.
19951*/
19952#ifdef SQLITE_DEBUG
19953#define memIsValid(M) ((M)->flags & MEM_Undefined)==0
19954#endif
19955
19956/*
19957** Each auxiliary data pointer stored by a user defined function
19958** implementation calling sqlite3_set_auxdata() is stored in an instance
19959** of this structure. All such structures associated with a single VM
19960** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
19961** when the VM is halted (if not before).
19962*/
19963struct AuxData {
19964 int iAuxOp; /* Instruction number of OP_Function opcode */
19965 int iAuxArg; /* Index of function argument. */
19966 void *pAux; /* Aux data pointer */
19967 void (*xDeleteAux)(void*); /* Destructor for the aux data */
19968 AuxData *pNextAux; /* Next element in list */
19969};
19970
19971/*
19972** The "context" argument for an installable function. A pointer to an
19973** instance of this structure is the first argument to the routines used
19974** implement the SQL functions.
19975**
19976** There is a typedef for this structure in sqlite.h. So all routines,
19977** even the public interface to SQLite, can use a pointer to this structure.
19978** But this file is the only place where the internal details of this
19979** structure are known.
19980**
19981** This structure is defined inside of vdbeInt.h because it uses substructures
19982** (Mem) which are only defined there.
19983*/
19984struct sqlite3_context {
19985 Mem *pOut; /* The return value is stored here */
19986 FuncDef *pFunc; /* Pointer to function information */
19987 Mem *pMem; /* Memory cell used to store aggregate context */
19988 Vdbe *pVdbe; /* The VM that owns this context */
19989 int iOp; /* Instruction number of OP_Function */
19990 int isError; /* Error code returned by the function. */
19991 u8 skipFlag; /* Skip accumulator loading if true */
19992 u8 argc; /* Number of arguments */
19993 sqlite3_value *argv[1]; /* Argument set */
19994};
19995
19996/* A bitfield type for use inside of structures. Always follow with :N where
19997** N is the number of bits.
19998*/
19999typedef unsigned bft; /* Bit Field Type */
20000
20001typedef struct ScanStatus ScanStatus;
20002struct ScanStatus {
20003 int addrExplain; /* OP_Explain for loop */
20004 int addrLoop; /* Address of "loops" counter */
20005 int addrVisit; /* Address of "rows visited" counter */
20006 int iSelectID; /* The "Select-ID" for this loop */
20007 LogEst nEst; /* Estimated output rows per loop */
20008 char *zName; /* Name of table or index */
20009};
20010
20011/*
20012** An instance of the virtual machine. This structure contains the complete
20013** state of the virtual machine.
20014**
20015** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
20016** is really a pointer to an instance of this structure.
20017*/
20018struct Vdbe {
20019 sqlite3 *db; /* The database connection that owns this statement */
20020 Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
20021 Parse *pParse; /* Parsing context used to create this Vdbe */
20022 ynVar nVar; /* Number of entries in aVar[] */
20023 u32 magic; /* Magic number for sanity checking */
20024 int nMem; /* Number of memory locations currently allocated */
20025 int nCursor; /* Number of slots in apCsr[] */
20026 u32 cacheCtr; /* VdbeCursor row cache generation counter */
20027 int pc; /* The program counter */
20028 int rc; /* Value to return */
20029 int nChange; /* Number of db changes made since last reset */
20030 int iStatement; /* Statement number (or 0 if has not opened stmt) */
20031 i64 iCurrentTime; /* Value of julianday('now') for this statement */
20032 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
20033 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
20034 i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */
20035
20036 /* When allocating a new Vdbe object, all of the fields below should be
20037 ** initialized to zero or NULL */
20038
20039 Op *aOp; /* Space to hold the virtual machine's program */
20040 Mem *aMem; /* The memory locations */
20041 Mem **apArg; /* Arguments to currently executing user function */
20042 Mem *aColName; /* Column names to return */
20043 Mem *pResultSet; /* Pointer to an array of results */
20044 char *zErrMsg; /* Error message written here */
20045 VdbeCursor **apCsr; /* One element of this array for each open cursor */
20046 Mem *aVar; /* Values for the OP_Variable opcode. */
20047 VList *pVList; /* Name of variables */
20048#ifndef SQLITE_OMIT_TRACE
20049 i64 startTime; /* Time when query started - used for profiling */
20050#endif
20051 int nOp; /* Number of instructions in the program */
20052#ifdef SQLITE_DEBUG
20053 int rcApp; /* errcode set by sqlite3_result_error_code() */
20054 u32 nWrite; /* Number of write operations that have occurred */
20055#endif
20056 u16 nResColumn; /* Number of columns in one row of the result set */
20057 u8 errorAction; /* Recovery action to do in case of an error */
20058 u8 minWriteFileFormat; /* Minimum file format for writable database files */
20059 u8 prepFlags; /* SQLITE_PREPARE_* flags */
20060 bft expired:2; /* 1: recompile VM immediately 2: when convenient */
20061 bft explain:2; /* True if EXPLAIN present on SQL command */
20062 bft doingRerun:1; /* True if rerunning after an auto-reprepare */
20063 bft changeCntOn:1; /* True to update the change-counter */
20064 bft runOnlyOnce:1; /* Automatically expire on reset */
20065 bft usesStmtJournal:1; /* True if uses a statement journal */
20066 bft readOnly:1; /* True for statements that do not write */
20067 bft bIsReader:1; /* True for statements that read */
20068 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
20069 yDbMask lockMask; /* Subset of btreeMask that requires a lock */
20070 u32 aCounter[7]; /* Counters used by sqlite3_stmt_status() */
20071 char *zSql; /* Text of the SQL statement that generated this */
20072 void *pFree; /* Free this when deleting the vdbe */
20073 VdbeFrame *pFrame; /* Parent frame */
20074 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
20075 int nFrame; /* Number of frames in pFrame list */
20076 u32 expmask; /* Binding to these vars invalidates VM */
20077 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
20078 AuxData *pAuxData; /* Linked list of auxdata allocations */
20079#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
20080 i64 *anExec; /* Number of times each op has been executed */
20081 int nScan; /* Entries in aScan[] */
20082 ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
20083#endif
20084};
20085
20086/*
20087** The following are allowed values for Vdbe.magic
20088*/
20089#define VDBE_MAGIC_INIT 0x16bceaa5 /* Building a VDBE program */
20090#define VDBE_MAGIC_RUN 0x2df20da3 /* VDBE is ready to execute */
20091#define VDBE_MAGIC_HALT 0x319c2973 /* VDBE has completed execution */
20092#define VDBE_MAGIC_RESET 0x48fa9f76 /* Reset and ready to run again */
20093#define VDBE_MAGIC_DEAD 0x5606c3c8 /* The VDBE has been deallocated */
20094
20095/*
20096** Structure used to store the context required by the
20097** sqlite3_preupdate_*() API functions.
20098*/
20099struct PreUpdate {
20100 Vdbe *v;
20101 VdbeCursor *pCsr; /* Cursor to read old values from */
20102 int op; /* One of SQLITE_INSERT, UPDATE, DELETE */
20103 u8 *aRecord; /* old.* database record */
20104 KeyInfo keyinfo;
20105 UnpackedRecord *pUnpacked; /* Unpacked version of aRecord[] */
20106 UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */
20107 int iNewReg; /* Register for new.* values */
20108 i64 iKey1; /* First key value passed to hook */
20109 i64 iKey2; /* Second key value passed to hook */
20110 Mem *aNew; /* Array of new.* values */
20111 Table *pTab; /* Schema object being upated */
20112 Index *pPk; /* PK index if pTab is WITHOUT ROWID */
20113};
20114
20115/*
20116** Function prototypes
20117*/
20118SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
20119SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
20120void sqliteVdbePopStack(Vdbe*,int);
20121SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, int*);
20122SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
20123SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
20124SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8);
20125SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int, u32*);
20126SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
20127SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
20128SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
20129
20130int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
20131SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
20132SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
20133SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
20134SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
20135SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
20136SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
20137SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
20138SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
20139SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
20140SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
20141SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
20142SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
20143SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
20144#ifdef SQLITE_OMIT_FLOATING_POINT
20145# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
20146#else
20147SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double);
20148#endif
20149SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*));
20150SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
20151SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
20152SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
20153#ifdef SQLITE_DEBUG
20154SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem*);
20155#endif
20156SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem*);
20157SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
20158SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
20159SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
20160SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
20161SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
20162SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
20163SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
20164SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
20165SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
20166SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
20167SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
20168SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
20169SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
20170#ifndef SQLITE_OMIT_WINDOWFUNC
20171SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
20172#endif
20173SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
20174SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
20175SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
20176SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
20177#ifdef SQLITE_DEBUG
20178SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame*);
20179#endif
20180SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void*); /* Destructor on Mem */
20181SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */
20182SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
20183#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
20184SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int);
20185#endif
20186SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
20187
20188SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
20189SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
20190SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
20191SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
20192SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
20193SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
20194SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
20195SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
20196
20197#ifdef SQLITE_DEBUG
20198SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
20199SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe*);
20200#else
20201# define sqlite3VdbeIncrWriteCounter(V,C)
20202# define sqlite3VdbeAssertAbortable(V)
20203#endif
20204
20205#if !defined(SQLITE_OMIT_SHARED_CACHE)
20206SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
20207#else
20208# define sqlite3VdbeEnter(X)
20209#endif
20210
20211#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
20212SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
20213#else
20214# define sqlite3VdbeLeave(X)
20215#endif
20216
20217#ifdef SQLITE_DEBUG
20218SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
20219SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
20220#endif
20221
20222#ifndef SQLITE_OMIT_FOREIGN_KEY
20223SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
20224#else
20225# define sqlite3VdbeCheckFk(p,i) 0
20226#endif
20227
20228#ifdef SQLITE_DEBUG
20229SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*);
20230SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
20231#endif
20232#ifndef SQLITE_OMIT_UTF16
20233SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
20234SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
20235#endif
20236
20237#ifndef SQLITE_OMIT_INCRBLOB
20238SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *);
20239 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
20240#else
20241 #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
20242 #define ExpandBlob(P) SQLITE_OK
20243#endif
20244
20245#endif /* !defined(SQLITE_VDBEINT_H) */
20246
20247/************** End of vdbeInt.h *********************************************/
20248/************** Continuing where we left off in status.c *********************/
20249
20250/*
20251** Variables in which to record status information.
20252*/
20253#if SQLITE_PTRSIZE>4
20254typedef sqlite3_int64 sqlite3StatValueType;
20255#else
20256typedef u32 sqlite3StatValueType;
20257#endif
20258typedef struct sqlite3StatType sqlite3StatType;
20259static SQLITE_WSD struct sqlite3StatType {
20260 sqlite3StatValueType nowValue[10]; /* Current value */
20261 sqlite3StatValueType mxValue[10]; /* Maximum value */
20262} sqlite3Stat = { {0,}, {0,} };
20263
20264/*
20265** Elements of sqlite3Stat[] are protected by either the memory allocator
20266** mutex, or by the pcache1 mutex. The following array determines which.
20267*/
20268static const char statMutex[] = {
20269 0, /* SQLITE_STATUS_MEMORY_USED */
20270 1, /* SQLITE_STATUS_PAGECACHE_USED */
20271 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
20272 0, /* SQLITE_STATUS_SCRATCH_USED */
20273 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */
20274 0, /* SQLITE_STATUS_MALLOC_SIZE */
20275 0, /* SQLITE_STATUS_PARSER_STACK */
20276 1, /* SQLITE_STATUS_PAGECACHE_SIZE */
20277 0, /* SQLITE_STATUS_SCRATCH_SIZE */
20278 0, /* SQLITE_STATUS_MALLOC_COUNT */
20279};
20280
20281
20282/* The "wsdStat" macro will resolve to the status information
20283** state vector. If writable static data is unsupported on the target,
20284** we have to locate the state vector at run-time. In the more common
20285** case where writable static data is supported, wsdStat can refer directly
20286** to the "sqlite3Stat" state vector declared above.
20287*/
20288#ifdef SQLITE_OMIT_WSD
20289# define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
20290# define wsdStat x[0]
20291#else
20292# define wsdStatInit
20293# define wsdStat sqlite3Stat
20294#endif
20295
20296/*
20297** Return the current value of a status parameter. The caller must
20298** be holding the appropriate mutex.
20299*/
20300SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int op){
20301 wsdStatInit;
20302 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
20303 assert( op>=0 && op<ArraySize(statMutex) );
20304 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
20305 : sqlite3MallocMutex()) );
20306 return wsdStat.nowValue[op];
20307}
20308
20309/*
20310** Add N to the value of a status record. The caller must hold the
20311** appropriate mutex. (Locking is checked by assert()).
20312**
20313** The StatusUp() routine can accept positive or negative values for N.
20314** The value of N is added to the current status value and the high-water
20315** mark is adjusted if necessary.
20316**
20317** The StatusDown() routine lowers the current value by N. The highwater
20318** mark is unchanged. N must be non-negative for StatusDown().
20319*/
20320SQLITE_PRIVATE void sqlite3StatusUp(int op, int N){
20321 wsdStatInit;
20322 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
20323 assert( op>=0 && op<ArraySize(statMutex) );
20324 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
20325 : sqlite3MallocMutex()) );
20326 wsdStat.nowValue[op] += N;
20327 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
20328 wsdStat.mxValue[op] = wsdStat.nowValue[op];
20329 }
20330}
20331SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){
20332 wsdStatInit;
20333 assert( N>=0 );
20334 assert( op>=0 && op<ArraySize(statMutex) );
20335 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
20336 : sqlite3MallocMutex()) );
20337 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
20338 wsdStat.nowValue[op] -= N;
20339}
20340
20341/*
20342** Adjust the highwater mark if necessary.
20343** The caller must hold the appropriate mutex.
20344*/
20345SQLITE_PRIVATE void sqlite3StatusHighwater(int op, int X){
20346 sqlite3StatValueType newValue;
20347 wsdStatInit;
20348 assert( X>=0 );
20349 newValue = (sqlite3StatValueType)X;
20350 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
20351 assert( op>=0 && op<ArraySize(statMutex) );
20352 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
20353 : sqlite3MallocMutex()) );
20354 assert( op==SQLITE_STATUS_MALLOC_SIZE
20355 || op==SQLITE_STATUS_PAGECACHE_SIZE
20356 || op==SQLITE_STATUS_PARSER_STACK );
20357 if( newValue>wsdStat.mxValue[op] ){
20358 wsdStat.mxValue[op] = newValue;
20359 }
20360}
20361
20362/*
20363** Query status information.
20364*/
20365SQLITE_API int sqlite3_status64(
20366 int op,
20367 sqlite3_int64 *pCurrent,
20368 sqlite3_int64 *pHighwater,
20369 int resetFlag
20370){
20371 sqlite3_mutex *pMutex;
20372 wsdStatInit;
20373 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
20374 return SQLITE_MISUSE_BKPT;
20375 }
20376#ifdef SQLITE_ENABLE_API_ARMOR
20377 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
20378#endif
20379 pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
20380 sqlite3_mutex_enter(pMutex);
20381 *pCurrent = wsdStat.nowValue[op];
20382 *pHighwater = wsdStat.mxValue[op];
20383 if( resetFlag ){
20384 wsdStat.mxValue[op] = wsdStat.nowValue[op];
20385 }
20386 sqlite3_mutex_leave(pMutex);
20387 (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */
20388 return SQLITE_OK;
20389}
20390SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
20391 sqlite3_int64 iCur = 0, iHwtr = 0;
20392 int rc;
20393#ifdef SQLITE_ENABLE_API_ARMOR
20394 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
20395#endif
20396 rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
20397 if( rc==0 ){
20398 *pCurrent = (int)iCur;
20399 *pHighwater = (int)iHwtr;
20400 }
20401 return rc;
20402}
20403
20404/*
20405** Return the number of LookasideSlot elements on the linked list
20406*/
20407static u32 countLookasideSlots(LookasideSlot *p){
20408 u32 cnt = 0;
20409 while( p ){
20410 p = p->pNext;
20411 cnt++;
20412 }
20413 return cnt;
20414}
20415
20416/*
20417** Count the number of slots of lookaside memory that are outstanding
20418*/
20419SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
20420 u32 nInit = countLookasideSlots(db->lookaside.pInit);
20421 u32 nFree = countLookasideSlots(db->lookaside.pFree);
20422 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
20423 return db->lookaside.nSlot - (nInit+nFree);
20424}
20425
20426/*
20427** Query status information for a single database connection
20428*/
20429SQLITE_API int sqlite3_db_status(
20430 sqlite3 *db, /* The database connection whose status is desired */
20431 int op, /* Status verb */
20432 int *pCurrent, /* Write current value here */
20433 int *pHighwater, /* Write high-water mark here */
20434 int resetFlag /* Reset high-water mark if true */
20435){
20436 int rc = SQLITE_OK; /* Return code */
20437#ifdef SQLITE_ENABLE_API_ARMOR
20438 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
20439 return SQLITE_MISUSE_BKPT;
20440 }
20441#endif
20442 sqlite3_mutex_enter(db->mutex);
20443 switch( op ){
20444 case SQLITE_DBSTATUS_LOOKASIDE_USED: {
20445 *pCurrent = sqlite3LookasideUsed(db, pHighwater);
20446 if( resetFlag ){
20447 LookasideSlot *p = db->lookaside.pFree;
20448 if( p ){
20449 while( p->pNext ) p = p->pNext;
20450 p->pNext = db->lookaside.pInit;
20451 db->lookaside.pInit = db->lookaside.pFree;
20452 db->lookaside.pFree = 0;
20453 }
20454 }
20455 break;
20456 }
20457
20458 case SQLITE_DBSTATUS_LOOKASIDE_HIT:
20459 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
20460 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
20461 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
20462 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
20463 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
20464 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
20465 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
20466 *pCurrent = 0;
20467 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
20468 if( resetFlag ){
20469 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
20470 }
20471 break;
20472 }
20473
20474 /*
20475 ** Return an approximation for the amount of memory currently used
20476 ** by all pagers associated with the given database connection. The
20477 ** highwater mark is meaningless and is returned as zero.
20478 */
20479 case SQLITE_DBSTATUS_CACHE_USED_SHARED:
20480 case SQLITE_DBSTATUS_CACHE_USED: {
20481 int totalUsed = 0;
20482 int i;
20483 sqlite3BtreeEnterAll(db);
20484 for(i=0; i<db->nDb; i++){
20485 Btree *pBt = db->aDb[i].pBt;
20486 if( pBt ){
20487 Pager *pPager = sqlite3BtreePager(pBt);
20488 int nByte = sqlite3PagerMemUsed(pPager);
20489 if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){
20490 nByte = nByte / sqlite3BtreeConnectionCount(pBt);
20491 }
20492 totalUsed += nByte;
20493 }
20494 }
20495 sqlite3BtreeLeaveAll(db);
20496 *pCurrent = totalUsed;
20497 *pHighwater = 0;
20498 break;
20499 }
20500
20501 /*
20502 ** *pCurrent gets an accurate estimate of the amount of memory used
20503 ** to store the schema for all databases (main, temp, and any ATTACHed
20504 ** databases. *pHighwater is set to zero.
20505 */
20506 case SQLITE_DBSTATUS_SCHEMA_USED: {
20507 int i; /* Used to iterate through schemas */
20508 int nByte = 0; /* Used to accumulate return value */
20509
20510 sqlite3BtreeEnterAll(db);
20511 db->pnBytesFreed = &nByte;
20512 for(i=0; i<db->nDb; i++){
20513 Schema *pSchema = db->aDb[i].pSchema;
20514 if( ALWAYS(pSchema!=0) ){
20515 HashElem *p;
20516
20517 nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
20518 pSchema->tblHash.count
20519 + pSchema->trigHash.count
20520 + pSchema->idxHash.count
20521 + pSchema->fkeyHash.count
20522 );
20523 nByte += sqlite3_msize(pSchema->tblHash.ht);
20524 nByte += sqlite3_msize(pSchema->trigHash.ht);
20525 nByte += sqlite3_msize(pSchema->idxHash.ht);
20526 nByte += sqlite3_msize(pSchema->fkeyHash.ht);
20527
20528 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
20529 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
20530 }
20531 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
20532 sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
20533 }
20534 }
20535 }
20536 db->pnBytesFreed = 0;
20537 sqlite3BtreeLeaveAll(db);
20538
20539 *pHighwater = 0;
20540 *pCurrent = nByte;
20541 break;
20542 }
20543
20544 /*
20545 ** *pCurrent gets an accurate estimate of the amount of memory used
20546 ** to store all prepared statements.
20547 ** *pHighwater is set to zero.
20548 */
20549 case SQLITE_DBSTATUS_STMT_USED: {
20550 struct Vdbe *pVdbe; /* Used to iterate through VMs */
20551 int nByte = 0; /* Used to accumulate return value */
20552
20553 db->pnBytesFreed = &nByte;
20554 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
20555 sqlite3VdbeClearObject(db, pVdbe);
20556 sqlite3DbFree(db, pVdbe);
20557 }
20558 db->pnBytesFreed = 0;
20559
20560 *pHighwater = 0; /* IMP: R-64479-57858 */
20561 *pCurrent = nByte;
20562
20563 break;
20564 }
20565
20566 /*
20567 ** Set *pCurrent to the total cache hits or misses encountered by all
20568 ** pagers the database handle is connected to. *pHighwater is always set
20569 ** to zero.
20570 */
20571 case SQLITE_DBSTATUS_CACHE_SPILL:
20572 op = SQLITE_DBSTATUS_CACHE_WRITE+1;
20573 /* Fall through into the next case */
20574 case SQLITE_DBSTATUS_CACHE_HIT:
20575 case SQLITE_DBSTATUS_CACHE_MISS:
20576 case SQLITE_DBSTATUS_CACHE_WRITE:{
20577 int i;
20578 int nRet = 0;
20579 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
20580 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
20581
20582 for(i=0; i<db->nDb; i++){
20583 if( db->aDb[i].pBt ){
20584 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
20585 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
20586 }
20587 }
20588 *pHighwater = 0; /* IMP: R-42420-56072 */
20589 /* IMP: R-54100-20147 */
20590 /* IMP: R-29431-39229 */
20591 *pCurrent = nRet;
20592 break;
20593 }
20594
20595 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
20596 ** key constraints. Set *pCurrent to zero if all foreign key constraints
20597 ** have been satisfied. The *pHighwater is always set to zero.
20598 */
20599 case SQLITE_DBSTATUS_DEFERRED_FKS: {
20600 *pHighwater = 0; /* IMP: R-11967-56545 */
20601 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
20602 break;
20603 }
20604
20605 default: {
20606 rc = SQLITE_ERROR;
20607 }
20608 }
20609 sqlite3_mutex_leave(db->mutex);
20610 return rc;
20611}
20612
20613/************** End of status.c **********************************************/
20614/************** Begin file date.c ********************************************/
20615/*
20616** 2003 October 31
20617**
20618** The author disclaims copyright to this source code. In place of
20619** a legal notice, here is a blessing:
20620**
20621** May you do good and not evil.
20622** May you find forgiveness for yourself and forgive others.
20623** May you share freely, never taking more than you give.
20624**
20625*************************************************************************
20626** This file contains the C functions that implement date and time
20627** functions for SQLite.
20628**
20629** There is only one exported symbol in this file - the function
20630** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
20631** All other code has file scope.
20632**
20633** SQLite processes all times and dates as julian day numbers. The
20634** dates and times are stored as the number of days since noon
20635** in Greenwich on November 24, 4714 B.C. according to the Gregorian
20636** calendar system.
20637**
20638** 1970-01-01 00:00:00 is JD 2440587.5
20639** 2000-01-01 00:00:00 is JD 2451544.5
20640**
20641** This implementation requires years to be expressed as a 4-digit number
20642** which means that only dates between 0000-01-01 and 9999-12-31 can
20643** be represented, even though julian day numbers allow a much wider
20644** range of dates.
20645**
20646** The Gregorian calendar system is used for all dates and times,
20647** even those that predate the Gregorian calendar. Historians usually
20648** use the julian calendar for dates prior to 1582-10-15 and for some
20649** dates afterwards, depending on locale. Beware of this difference.
20650**
20651** The conversion algorithms are implemented based on descriptions
20652** in the following text:
20653**
20654** Jean Meeus
20655** Astronomical Algorithms, 2nd Edition, 1998
20656** ISBN 0-943396-61-1
20657** Willmann-Bell, Inc
20658** Richmond, Virginia (USA)
20659*/
20660/* #include "sqliteInt.h" */
20661/* #include <stdlib.h> */
20662/* #include <assert.h> */
20663#include <time.h>
20664
20665#ifndef SQLITE_OMIT_DATETIME_FUNCS
20666
20667/*
20668** The MSVC CRT on Windows CE may not have a localtime() function.
20669** So declare a substitute. The substitute function itself is
20670** defined in "os_win.c".
20671*/
20672#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
20673 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
20674struct tm *__cdecl localtime(const time_t *);
20675#endif
20676
20677/*
20678** A structure for holding a single date and time.
20679*/
20680typedef struct DateTime DateTime;
20681struct DateTime {
20682 sqlite3_int64 iJD; /* The julian day number times 86400000 */
20683 int Y, M, D; /* Year, month, and day */
20684 int h, m; /* Hour and minutes */
20685 int tz; /* Timezone offset in minutes */
20686 double s; /* Seconds */
20687 char validJD; /* True (1) if iJD is valid */
20688 char rawS; /* Raw numeric value stored in s */
20689 char validYMD; /* True (1) if Y,M,D are valid */
20690 char validHMS; /* True (1) if h,m,s are valid */
20691 char validTZ; /* True (1) if tz is valid */
20692 char tzSet; /* Timezone was set explicitly */
20693 char isError; /* An overflow has occurred */
20694};
20695
20696
20697/*
20698** Convert zDate into one or more integers according to the conversion
20699** specifier zFormat.
20700**
20701** zFormat[] contains 4 characters for each integer converted, except for
20702** the last integer which is specified by three characters. The meaning
20703** of a four-character format specifiers ABCD is:
20704**
20705** A: number of digits to convert. Always "2" or "4".
20706** B: minimum value. Always "0" or "1".
20707** C: maximum value, decoded as:
20708** a: 12
20709** b: 14
20710** c: 24
20711** d: 31
20712** e: 59
20713** f: 9999
20714** D: the separator character, or \000 to indicate this is the
20715** last number to convert.
20716**
20717** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
20718** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
20719** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
20720** the 2-digit day which is the last integer in the set.
20721**
20722** The function returns the number of successful conversions.
20723*/
20724static int getDigits(const char *zDate, const char *zFormat, ...){
20725 /* The aMx[] array translates the 3rd character of each format
20726 ** spec into a max size: a b c d e f */
20727 static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 };
20728 va_list ap;
20729 int cnt = 0;
20730 char nextC;
20731 va_start(ap, zFormat);
20732 do{
20733 char N = zFormat[0] - '0';
20734 char min = zFormat[1] - '0';
20735 int val = 0;
20736 u16 max;
20737
20738 assert( zFormat[2]>='a' && zFormat[2]<='f' );
20739 max = aMx[zFormat[2] - 'a'];
20740 nextC = zFormat[3];
20741 val = 0;
20742 while( N-- ){
20743 if( !sqlite3Isdigit(*zDate) ){
20744 goto end_getDigits;
20745 }
20746 val = val*10 + *zDate - '0';
20747 zDate++;
20748 }
20749 if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){
20750 goto end_getDigits;
20751 }
20752 *va_arg(ap,int*) = val;
20753 zDate++;
20754 cnt++;
20755 zFormat += 4;
20756 }while( nextC );
20757end_getDigits:
20758 va_end(ap);
20759 return cnt;
20760}
20761
20762/*
20763** Parse a timezone extension on the end of a date-time.
20764** The extension is of the form:
20765**
20766** (+/-)HH:MM
20767**
20768** Or the "zulu" notation:
20769**
20770** Z
20771**
20772** If the parse is successful, write the number of minutes
20773** of change in p->tz and return 0. If a parser error occurs,
20774** return non-zero.
20775**
20776** A missing specifier is not considered an error.
20777*/
20778static int parseTimezone(const char *zDate, DateTime *p){
20779 int sgn = 0;
20780 int nHr, nMn;
20781 int c;
20782 while( sqlite3Isspace(*zDate) ){ zDate++; }
20783 p->tz = 0;
20784 c = *zDate;
20785 if( c=='-' ){
20786 sgn = -1;
20787 }else if( c=='+' ){
20788 sgn = +1;
20789 }else if( c=='Z' || c=='z' ){
20790 zDate++;
20791 goto zulu_time;
20792 }else{
20793 return c!=0;
20794 }
20795 zDate++;
20796 if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){
20797 return 1;
20798 }
20799 zDate += 5;
20800 p->tz = sgn*(nMn + nHr*60);
20801zulu_time:
20802 while( sqlite3Isspace(*zDate) ){ zDate++; }
20803 p->tzSet = 1;
20804 return *zDate!=0;
20805}
20806
20807/*
20808** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
20809** The HH, MM, and SS must each be exactly 2 digits. The
20810** fractional seconds FFFF can be one or more digits.
20811**
20812** Return 1 if there is a parsing error and 0 on success.
20813*/
20814static int parseHhMmSs(const char *zDate, DateTime *p){
20815 int h, m, s;
20816 double ms = 0.0;
20817 if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){
20818 return 1;
20819 }
20820 zDate += 5;
20821 if( *zDate==':' ){
20822 zDate++;
20823 if( getDigits(zDate, "20e", &s)!=1 ){
20824 return 1;
20825 }
20826 zDate += 2;
20827 if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
20828 double rScale = 1.0;
20829 zDate++;
20830 while( sqlite3Isdigit(*zDate) ){
20831 ms = ms*10.0 + *zDate - '0';
20832 rScale *= 10.0;
20833 zDate++;
20834 }
20835 ms /= rScale;
20836 }
20837 }else{
20838 s = 0;
20839 }
20840 p->validJD = 0;
20841 p->rawS = 0;
20842 p->validHMS = 1;
20843 p->h = h;
20844 p->m = m;
20845 p->s = s + ms;
20846 if( parseTimezone(zDate, p) ) return 1;
20847 p->validTZ = (p->tz!=0)?1:0;
20848 return 0;
20849}
20850
20851/*
20852** Put the DateTime object into its error state.
20853*/
20854static void datetimeError(DateTime *p){
20855 memset(p, 0, sizeof(*p));
20856 p->isError = 1;
20857}
20858
20859/*
20860** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
20861** that the YYYY-MM-DD is according to the Gregorian calendar.
20862**
20863** Reference: Meeus page 61
20864*/
20865static void computeJD(DateTime *p){
20866 int Y, M, D, A, B, X1, X2;
20867
20868 if( p->validJD ) return;
20869 if( p->validYMD ){
20870 Y = p->Y;
20871 M = p->M;
20872 D = p->D;
20873 }else{
20874 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
20875 M = 1;
20876 D = 1;
20877 }
20878 if( Y<-4713 || Y>9999 || p->rawS ){
20879 datetimeError(p);
20880 return;
20881 }
20882 if( M<=2 ){
20883 Y--;
20884 M += 12;
20885 }
20886 A = Y/100;
20887 B = 2 - A + (A/4);
20888 X1 = 36525*(Y+4716)/100;
20889 X2 = 306001*(M+1)/10000;
20890 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
20891 p->validJD = 1;
20892 if( p->validHMS ){
20893 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
20894 if( p->validTZ ){
20895 p->iJD -= p->tz*60000;
20896 p->validYMD = 0;
20897 p->validHMS = 0;
20898 p->validTZ = 0;
20899 }
20900 }
20901}
20902
20903/*
20904** Parse dates of the form
20905**
20906** YYYY-MM-DD HH:MM:SS.FFF
20907** YYYY-MM-DD HH:MM:SS
20908** YYYY-MM-DD HH:MM
20909** YYYY-MM-DD
20910**
20911** Write the result into the DateTime structure and return 0
20912** on success and 1 if the input string is not a well-formed
20913** date.
20914*/
20915static int parseYyyyMmDd(const char *zDate, DateTime *p){
20916 int Y, M, D, neg;
20917
20918 if( zDate[0]=='-' ){
20919 zDate++;
20920 neg = 1;
20921 }else{
20922 neg = 0;
20923 }
20924 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
20925 return 1;
20926 }
20927 zDate += 10;
20928 while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
20929 if( parseHhMmSs(zDate, p)==0 ){
20930 /* We got the time */
20931 }else if( *zDate==0 ){
20932 p->validHMS = 0;
20933 }else{
20934 return 1;
20935 }
20936 p->validJD = 0;
20937 p->validYMD = 1;
20938 p->Y = neg ? -Y : Y;
20939 p->M = M;
20940 p->D = D;
20941 if( p->validTZ ){
20942 computeJD(p);
20943 }
20944 return 0;
20945}
20946
20947/*
20948** Set the time to the current time reported by the VFS.
20949**
20950** Return the number of errors.
20951*/
20952static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
20953 p->iJD = sqlite3StmtCurrentTime(context);
20954 if( p->iJD>0 ){
20955 p->validJD = 1;
20956 return 0;
20957 }else{
20958 return 1;
20959 }
20960}
20961
20962/*
20963** Input "r" is a numeric quantity which might be a julian day number,
20964** or the number of seconds since 1970. If the value if r is within
20965** range of a julian day number, install it as such and set validJD.
20966** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
20967*/
20968static void setRawDateNumber(DateTime *p, double r){
20969 p->s = r;
20970 p->rawS = 1;
20971 if( r>=0.0 && r<5373484.5 ){
20972 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
20973 p->validJD = 1;
20974 }
20975}
20976
20977/*
20978** Attempt to parse the given string into a julian day number. Return
20979** the number of errors.
20980**
20981** The following are acceptable forms for the input string:
20982**
20983** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
20984** DDDD.DD
20985** now
20986**
20987** In the first form, the +/-HH:MM is always optional. The fractional
20988** seconds extension (the ".FFF") is optional. The seconds portion
20989** (":SS.FFF") is option. The year and date can be omitted as long
20990** as there is a time string. The time string can be omitted as long
20991** as there is a year and date.
20992*/
20993static int parseDateOrTime(
20994 sqlite3_context *context,
20995 const char *zDate,
20996 DateTime *p
20997){
20998 double r;
20999 if( parseYyyyMmDd(zDate,p)==0 ){
21000 return 0;
21001 }else if( parseHhMmSs(zDate, p)==0 ){
21002 return 0;
21003 }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){
21004 return setDateTimeToCurrent(context, p);
21005 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
21006 setRawDateNumber(p, r);
21007 return 0;
21008 }
21009 return 1;
21010}
21011
21012/* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
21013** Multiplying this by 86400000 gives 464269060799999 as the maximum value
21014** for DateTime.iJD.
21015**
21016** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with
21017** such a large integer literal, so we have to encode it.
21018*/
21019#define INT_464269060799999 ((((i64)0x1a640)<<32)|0x1072fdff)
21020
21021/*
21022** Return TRUE if the given julian day number is within range.
21023**
21024** The input is the JulianDay times 86400000.
21025*/
21026static int validJulianDay(sqlite3_int64 iJD){
21027 return iJD>=0 && iJD<=INT_464269060799999;
21028}
21029
21030/*
21031** Compute the Year, Month, and Day from the julian day number.
21032*/
21033static void computeYMD(DateTime *p){
21034 int Z, A, B, C, D, E, X1;
21035 if( p->validYMD ) return;
21036 if( !p->validJD ){
21037 p->Y = 2000;
21038 p->M = 1;
21039 p->D = 1;
21040 }else if( !validJulianDay(p->iJD) ){
21041 datetimeError(p);
21042 return;
21043 }else{
21044 Z = (int)((p->iJD + 43200000)/86400000);
21045 A = (int)((Z - 1867216.25)/36524.25);
21046 A = Z + 1 + A - (A/4);
21047 B = A + 1524;
21048 C = (int)((B - 122.1)/365.25);
21049 D = (36525*(C&32767))/100;
21050 E = (int)((B-D)/30.6001);
21051 X1 = (int)(30.6001*E);
21052 p->D = B - D - X1;
21053 p->M = E<14 ? E-1 : E-13;
21054 p->Y = p->M>2 ? C - 4716 : C - 4715;
21055 }
21056 p->validYMD = 1;
21057}
21058
21059/*
21060** Compute the Hour, Minute, and Seconds from the julian day number.
21061*/
21062static void computeHMS(DateTime *p){
21063 int s;
21064 if( p->validHMS ) return;
21065 computeJD(p);
21066 s = (int)((p->iJD + 43200000) % 86400000);
21067 p->s = s/1000.0;
21068 s = (int)p->s;
21069 p->s -= s;
21070 p->h = s/3600;
21071 s -= p->h*3600;
21072 p->m = s/60;
21073 p->s += s - p->m*60;
21074 p->rawS = 0;
21075 p->validHMS = 1;
21076}
21077
21078/*
21079** Compute both YMD and HMS
21080*/
21081static void computeYMD_HMS(DateTime *p){
21082 computeYMD(p);
21083 computeHMS(p);
21084}
21085
21086/*
21087** Clear the YMD and HMS and the TZ
21088*/
21089static void clearYMD_HMS_TZ(DateTime *p){
21090 p->validYMD = 0;
21091 p->validHMS = 0;
21092 p->validTZ = 0;
21093}
21094
21095#ifndef SQLITE_OMIT_LOCALTIME
21096/*
21097** On recent Windows platforms, the localtime_s() function is available
21098** as part of the "Secure CRT". It is essentially equivalent to
21099** localtime_r() available under most POSIX platforms, except that the
21100** order of the parameters is reversed.
21101**
21102** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
21103**
21104** If the user has not indicated to use localtime_r() or localtime_s()
21105** already, check for an MSVC build environment that provides
21106** localtime_s().
21107*/
21108#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \
21109 && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
21110#undef HAVE_LOCALTIME_S
21111#define HAVE_LOCALTIME_S 1
21112#endif
21113
21114/*
21115** The following routine implements the rough equivalent of localtime_r()
21116** using whatever operating-system specific localtime facility that
21117** is available. This routine returns 0 on success and
21118** non-zero on any kind of error.
21119**
21120** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
21121** routine will always fail.
21122**
21123** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
21124** library function localtime_r() is used to assist in the calculation of
21125** local time.
21126*/
21127static int osLocaltime(time_t *t, struct tm *pTm){
21128 int rc;
21129#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S
21130 struct tm *pX;
21131#if SQLITE_THREADSAFE>0
21132 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
21133#endif
21134 sqlite3_mutex_enter(mutex);
21135 pX = localtime(t);
21136#ifndef SQLITE_UNTESTABLE
21137 if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
21138#endif
21139 if( pX ) *pTm = *pX;
21140 sqlite3_mutex_leave(mutex);
21141 rc = pX==0;
21142#else
21143#ifndef SQLITE_UNTESTABLE
21144 if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
21145#endif
21146#if HAVE_LOCALTIME_R
21147 rc = localtime_r(t, pTm)==0;
21148#else
21149 rc = localtime_s(pTm, t);
21150#endif /* HAVE_LOCALTIME_R */
21151#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
21152 return rc;
21153}
21154#endif /* SQLITE_OMIT_LOCALTIME */
21155
21156
21157#ifndef SQLITE_OMIT_LOCALTIME
21158/*
21159** Compute the difference (in milliseconds) between localtime and UTC
21160** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
21161** return this value and set *pRc to SQLITE_OK.
21162**
21163** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
21164** is undefined in this case.
21165*/
21166static sqlite3_int64 localtimeOffset(
21167 DateTime *p, /* Date at which to calculate offset */
21168 sqlite3_context *pCtx, /* Write error here if one occurs */
21169 int *pRc /* OUT: Error code. SQLITE_OK or ERROR */
21170){
21171 DateTime x, y;
21172 time_t t;
21173 struct tm sLocal;
21174
21175 /* Initialize the contents of sLocal to avoid a compiler warning. */
21176 memset(&sLocal, 0, sizeof(sLocal));
21177
21178 x = *p;
21179 computeYMD_HMS(&x);
21180 if( x.Y<1971 || x.Y>=2038 ){
21181 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
21182 ** works for years between 1970 and 2037. For dates outside this range,
21183 ** SQLite attempts to map the year into an equivalent year within this
21184 ** range, do the calculation, then map the year back.
21185 */
21186 x.Y = 2000;
21187 x.M = 1;
21188 x.D = 1;
21189 x.h = 0;
21190 x.m = 0;
21191 x.s = 0.0;
21192 } else {
21193 int s = (int)(x.s + 0.5);
21194 x.s = s;
21195 }
21196 x.tz = 0;
21197 x.validJD = 0;
21198 computeJD(&x);
21199 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
21200 if( osLocaltime(&t, &sLocal) ){
21201 sqlite3_result_error(pCtx, "local time unavailable", -1);
21202 *pRc = SQLITE_ERROR;
21203 return 0;
21204 }
21205 y.Y = sLocal.tm_year + 1900;
21206 y.M = sLocal.tm_mon + 1;
21207 y.D = sLocal.tm_mday;
21208 y.h = sLocal.tm_hour;
21209 y.m = sLocal.tm_min;
21210 y.s = sLocal.tm_sec;
21211 y.validYMD = 1;
21212 y.validHMS = 1;
21213 y.validJD = 0;
21214 y.rawS = 0;
21215 y.validTZ = 0;
21216 y.isError = 0;
21217 computeJD(&y);
21218 *pRc = SQLITE_OK;
21219 return y.iJD - x.iJD;
21220}
21221#endif /* SQLITE_OMIT_LOCALTIME */
21222
21223/*
21224** The following table defines various date transformations of the form
21225**
21226** 'NNN days'
21227**
21228** Where NNN is an arbitrary floating-point number and "days" can be one
21229** of several units of time.
21230*/
21231static const struct {
21232 u8 eType; /* Transformation type code */
21233 u8 nName; /* Length of th name */
21234 char *zName; /* Name of the transformation */
21235 double rLimit; /* Maximum NNN value for this transform */
21236 double rXform; /* Constant used for this transform */
21237} aXformType[] = {
21238 { 0, 6, "second", 464269060800.0, 86400000.0/(24.0*60.0*60.0) },
21239 { 0, 6, "minute", 7737817680.0, 86400000.0/(24.0*60.0) },
21240 { 0, 4, "hour", 128963628.0, 86400000.0/24.0 },
21241 { 0, 3, "day", 5373485.0, 86400000.0 },
21242 { 1, 5, "month", 176546.0, 30.0*86400000.0 },
21243 { 2, 4, "year", 14713.0, 365.0*86400000.0 },
21244};
21245
21246/*
21247** Process a modifier to a date-time stamp. The modifiers are
21248** as follows:
21249**
21250** NNN days
21251** NNN hours
21252** NNN minutes
21253** NNN.NNNN seconds
21254** NNN months
21255** NNN years
21256** start of month
21257** start of year
21258** start of week
21259** start of day
21260** weekday N
21261** unixepoch
21262** localtime
21263** utc
21264**
21265** Return 0 on success and 1 if there is any kind of error. If the error
21266** is in a system call (i.e. localtime()), then an error message is written
21267** to context pCtx. If the error is an unrecognized modifier, no error is
21268** written to pCtx.
21269*/
21270static int parseModifier(
21271 sqlite3_context *pCtx, /* Function context */
21272 const char *z, /* The text of the modifier */
21273 int n, /* Length of zMod in bytes */
21274 DateTime *p /* The date/time value to be modified */
21275){
21276 int rc = 1;
21277 double r;
21278 switch(sqlite3UpperToLower[(u8)z[0]] ){
21279#ifndef SQLITE_OMIT_LOCALTIME
21280 case 'l': {
21281 /* localtime
21282 **
21283 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
21284 ** show local time.
21285 */
21286 if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){
21287 computeJD(p);
21288 p->iJD += localtimeOffset(p, pCtx, &rc);
21289 clearYMD_HMS_TZ(p);
21290 }
21291 break;
21292 }
21293#endif
21294 case 'u': {
21295 /*
21296 ** unixepoch
21297 **
21298 ** Treat the current value of p->s as the number of
21299 ** seconds since 1970. Convert to a real julian day number.
21300 */
21301 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
21302 r = p->s*1000.0 + 210866760000000.0;
21303 if( r>=0.0 && r<464269060800000.0 ){
21304 clearYMD_HMS_TZ(p);
21305 p->iJD = (sqlite3_int64)r;
21306 p->validJD = 1;
21307 p->rawS = 0;
21308 rc = 0;
21309 }
21310 }
21311#ifndef SQLITE_OMIT_LOCALTIME
21312 else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){
21313 if( p->tzSet==0 ){
21314 sqlite3_int64 c1;
21315 computeJD(p);
21316 c1 = localtimeOffset(p, pCtx, &rc);
21317 if( rc==SQLITE_OK ){
21318 p->iJD -= c1;
21319 clearYMD_HMS_TZ(p);
21320 p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
21321 }
21322 p->tzSet = 1;
21323 }else{
21324 rc = SQLITE_OK;
21325 }
21326 }
21327#endif
21328 break;
21329 }
21330 case 'w': {
21331 /*
21332 ** weekday N
21333 **
21334 ** Move the date to the same time on the next occurrence of
21335 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
21336 ** date is already on the appropriate weekday, this is a no-op.
21337 */
21338 if( sqlite3_strnicmp(z, "weekday ", 8)==0
21339 && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
21340 && (n=(int)r)==r && n>=0 && r<7 ){
21341 sqlite3_int64 Z;
21342 computeYMD_HMS(p);
21343 p->validTZ = 0;
21344 p->validJD = 0;
21345 computeJD(p);
21346 Z = ((p->iJD + 129600000)/86400000) % 7;
21347 if( Z>n ) Z -= 7;
21348 p->iJD += (n - Z)*86400000;
21349 clearYMD_HMS_TZ(p);
21350 rc = 0;
21351 }
21352 break;
21353 }
21354 case 's': {
21355 /*
21356 ** start of TTTTT
21357 **
21358 ** Move the date backwards to the beginning of the current day,
21359 ** or month or year.
21360 */
21361 if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
21362 if( !p->validJD && !p->validYMD && !p->validHMS ) break;
21363 z += 9;
21364 computeYMD(p);
21365 p->validHMS = 1;
21366 p->h = p->m = 0;
21367 p->s = 0.0;
21368 p->rawS = 0;
21369 p->validTZ = 0;
21370 p->validJD = 0;
21371 if( sqlite3_stricmp(z,"month")==0 ){
21372 p->D = 1;
21373 rc = 0;
21374 }else if( sqlite3_stricmp(z,"year")==0 ){
21375 p->M = 1;
21376 p->D = 1;
21377 rc = 0;
21378 }else if( sqlite3_stricmp(z,"day")==0 ){
21379 rc = 0;
21380 }
21381 break;
21382 }
21383 case '+':
21384 case '-':
21385 case '0':
21386 case '1':
21387 case '2':
21388 case '3':
21389 case '4':
21390 case '5':
21391 case '6':
21392 case '7':
21393 case '8':
21394 case '9': {
21395 double rRounder;
21396 int i;
21397 for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
21398 if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
21399 rc = 1;
21400 break;
21401 }
21402 if( z[n]==':' ){
21403 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
21404 ** specified number of hours, minutes, seconds, and fractional seconds
21405 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
21406 ** omitted.
21407 */
21408 const char *z2 = z;
21409 DateTime tx;
21410 sqlite3_int64 day;
21411 if( !sqlite3Isdigit(*z2) ) z2++;
21412 memset(&tx, 0, sizeof(tx));
21413 if( parseHhMmSs(z2, &tx) ) break;
21414 computeJD(&tx);
21415 tx.iJD -= 43200000;
21416 day = tx.iJD/86400000;
21417 tx.iJD -= day*86400000;
21418 if( z[0]=='-' ) tx.iJD = -tx.iJD;
21419 computeJD(p);
21420 clearYMD_HMS_TZ(p);
21421 p->iJD += tx.iJD;
21422 rc = 0;
21423 break;
21424 }
21425
21426 /* If control reaches this point, it means the transformation is
21427 ** one of the forms like "+NNN days". */
21428 z += n;
21429 while( sqlite3Isspace(*z) ) z++;
21430 n = sqlite3Strlen30(z);
21431 if( n>10 || n<3 ) break;
21432 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
21433 computeJD(p);
21434 rc = 1;
21435 rRounder = r<0 ? -0.5 : +0.5;
21436 for(i=0; i<ArraySize(aXformType); i++){
21437 if( aXformType[i].nName==n
21438 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
21439 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
21440 ){
21441 switch( aXformType[i].eType ){
21442 case 1: { /* Special processing to add months */
21443 int x;
21444 computeYMD_HMS(p);
21445 p->M += (int)r;
21446 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
21447 p->Y += x;
21448 p->M -= x*12;
21449 p->validJD = 0;
21450 r -= (int)r;
21451 break;
21452 }
21453 case 2: { /* Special processing to add years */
21454 int y = (int)r;
21455 computeYMD_HMS(p);
21456 p->Y += y;
21457 p->validJD = 0;
21458 r -= (int)r;
21459 break;
21460 }
21461 }
21462 computeJD(p);
21463 p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder);
21464 rc = 0;
21465 break;
21466 }
21467 }
21468 clearYMD_HMS_TZ(p);
21469 break;
21470 }
21471 default: {
21472 break;
21473 }
21474 }
21475 return rc;
21476}
21477
21478/*
21479** Process time function arguments. argv[0] is a date-time stamp.
21480** argv[1] and following are modifiers. Parse them all and write
21481** the resulting time into the DateTime structure p. Return 0
21482** on success and 1 if there are any errors.
21483**
21484** If there are zero parameters (if even argv[0] is undefined)
21485** then assume a default value of "now" for argv[0].
21486*/
21487static int isDate(
21488 sqlite3_context *context,
21489 int argc,
21490 sqlite3_value **argv,
21491 DateTime *p
21492){
21493 int i, n;
21494 const unsigned char *z;
21495 int eType;
21496 memset(p, 0, sizeof(*p));
21497 if( argc==0 ){
21498 return setDateTimeToCurrent(context, p);
21499 }
21500 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
21501 || eType==SQLITE_INTEGER ){
21502 setRawDateNumber(p, sqlite3_value_double(argv[0]));
21503 }else{
21504 z = sqlite3_value_text(argv[0]);
21505 if( !z || parseDateOrTime(context, (char*)z, p) ){
21506 return 1;
21507 }
21508 }
21509 for(i=1; i<argc; i++){
21510 z = sqlite3_value_text(argv[i]);
21511 n = sqlite3_value_bytes(argv[i]);
21512 if( z==0 || parseModifier(context, (char*)z, n, p) ) return 1;
21513 }
21514 computeJD(p);
21515 if( p->isError || !validJulianDay(p->iJD) ) return 1;
21516 return 0;
21517}
21518
21519
21520/*
21521** The following routines implement the various date and time functions
21522** of SQLite.
21523*/
21524
21525/*
21526** julianday( TIMESTRING, MOD, MOD, ...)
21527**
21528** Return the julian day number of the date specified in the arguments
21529*/
21530static void juliandayFunc(
21531 sqlite3_context *context,
21532 int argc,
21533 sqlite3_value **argv
21534){
21535 DateTime x;
21536 if( isDate(context, argc, argv, &x)==0 ){
21537 computeJD(&x);
21538 sqlite3_result_double(context, x.iJD/86400000.0);
21539 }
21540}
21541
21542/*
21543** datetime( TIMESTRING, MOD, MOD, ...)
21544**
21545** Return YYYY-MM-DD HH:MM:SS
21546*/
21547static void datetimeFunc(
21548 sqlite3_context *context,
21549 int argc,
21550 sqlite3_value **argv
21551){
21552 DateTime x;
21553 if( isDate(context, argc, argv, &x)==0 ){
21554 char zBuf[100];
21555 computeYMD_HMS(&x);
21556 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
21557 x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
21558 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
21559 }
21560}
21561
21562/*
21563** time( TIMESTRING, MOD, MOD, ...)
21564**
21565** Return HH:MM:SS
21566*/
21567static void timeFunc(
21568 sqlite3_context *context,
21569 int argc,
21570 sqlite3_value **argv
21571){
21572 DateTime x;
21573 if( isDate(context, argc, argv, &x)==0 ){
21574 char zBuf[100];
21575 computeHMS(&x);
21576 sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
21577 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
21578 }
21579}
21580
21581/*
21582** date( TIMESTRING, MOD, MOD, ...)
21583**
21584** Return YYYY-MM-DD
21585*/
21586static void dateFunc(
21587 sqlite3_context *context,
21588 int argc,
21589 sqlite3_value **argv
21590){
21591 DateTime x;
21592 if( isDate(context, argc, argv, &x)==0 ){
21593 char zBuf[100];
21594 computeYMD(&x);
21595 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
21596 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
21597 }
21598}
21599
21600/*
21601** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
21602**
21603** Return a string described by FORMAT. Conversions as follows:
21604**
21605** %d day of month
21606** %f ** fractional seconds SS.SSS
21607** %H hour 00-24
21608** %j day of year 000-366
21609** %J ** julian day number
21610** %m month 01-12
21611** %M minute 00-59
21612** %s seconds since 1970-01-01
21613** %S seconds 00-59
21614** %w day of week 0-6 sunday==0
21615** %W week of year 00-53
21616** %Y year 0000-9999
21617** %% %
21618*/
21619static void strftimeFunc(
21620 sqlite3_context *context,
21621 int argc,
21622 sqlite3_value **argv
21623){
21624 DateTime x;
21625 u64 n;
21626 size_t i,j;
21627 char *z;
21628 sqlite3 *db;
21629 const char *zFmt;
21630 char zBuf[100];
21631 if( argc==0 ) return;
21632 zFmt = (const char*)sqlite3_value_text(argv[0]);
21633 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
21634 db = sqlite3_context_db_handle(context);
21635 for(i=0, n=1; zFmt[i]; i++, n++){
21636 if( zFmt[i]=='%' ){
21637 switch( zFmt[i+1] ){
21638 case 'd':
21639 case 'H':
21640 case 'm':
21641 case 'M':
21642 case 'S':
21643 case 'W':
21644 n++;
21645 /* fall thru */
21646 case 'w':
21647 case '%':
21648 break;
21649 case 'f':
21650 n += 8;
21651 break;
21652 case 'j':
21653 n += 3;
21654 break;
21655 case 'Y':
21656 n += 8;
21657 break;
21658 case 's':
21659 case 'J':
21660 n += 50;
21661 break;
21662 default:
21663 return; /* ERROR. return a NULL */
21664 }
21665 i++;
21666 }
21667 }
21668 testcase( n==sizeof(zBuf)-1 );
21669 testcase( n==sizeof(zBuf) );
21670 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
21671 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
21672 if( n<sizeof(zBuf) ){
21673 z = zBuf;
21674 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
21675 sqlite3_result_error_toobig(context);
21676 return;
21677 }else{
21678 z = sqlite3DbMallocRawNN(db, (int)n);
21679 if( z==0 ){
21680 sqlite3_result_error_nomem(context);
21681 return;
21682 }
21683 }
21684 computeJD(&x);
21685 computeYMD_HMS(&x);
21686 for(i=j=0; zFmt[i]; i++){
21687 if( zFmt[i]!='%' ){
21688 z[j++] = zFmt[i];
21689 }else{
21690 i++;
21691 switch( zFmt[i] ){
21692 case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
21693 case 'f': {
21694 double s = x.s;
21695 if( s>59.999 ) s = 59.999;
21696 sqlite3_snprintf(7, &z[j],"%06.3f", s);
21697 j += sqlite3Strlen30(&z[j]);
21698 break;
21699 }
21700 case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
21701 case 'W': /* Fall thru */
21702 case 'j': {
21703 int nDay; /* Number of days since 1st day of year */
21704 DateTime y = x;
21705 y.validJD = 0;
21706 y.M = 1;
21707 y.D = 1;
21708 computeJD(&y);
21709 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
21710 if( zFmt[i]=='W' ){
21711 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
21712 wd = (int)(((x.iJD+43200000)/86400000)%7);
21713 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
21714 j += 2;
21715 }else{
21716 sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
21717 j += 3;
21718 }
21719 break;
21720 }
21721 case 'J': {
21722 sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
21723 j+=sqlite3Strlen30(&z[j]);
21724 break;
21725 }
21726 case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
21727 case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
21728 case 's': {
21729 sqlite3_snprintf(30,&z[j],"%lld",
21730 (i64)(x.iJD/1000 - 21086676*(i64)10000));
21731 j += sqlite3Strlen30(&z[j]);
21732 break;
21733 }
21734 case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
21735 case 'w': {
21736 z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
21737 break;
21738 }
21739 case 'Y': {
21740 sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
21741 break;
21742 }
21743 default: z[j++] = '%'; break;
21744 }
21745 }
21746 }
21747 z[j] = 0;
21748 sqlite3_result_text(context, z, -1,
21749 z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
21750}
21751
21752/*
21753** current_time()
21754**
21755** This function returns the same value as time('now').
21756*/
21757static void ctimeFunc(
21758 sqlite3_context *context,
21759 int NotUsed,
21760 sqlite3_value **NotUsed2
21761){
21762 UNUSED_PARAMETER2(NotUsed, NotUsed2);
21763 timeFunc(context, 0, 0);
21764}
21765
21766/*
21767** current_date()
21768**
21769** This function returns the same value as date('now').
21770*/
21771static void cdateFunc(
21772 sqlite3_context *context,
21773 int NotUsed,
21774 sqlite3_value **NotUsed2
21775){
21776 UNUSED_PARAMETER2(NotUsed, NotUsed2);
21777 dateFunc(context, 0, 0);
21778}
21779
21780/*
21781** current_timestamp()
21782**
21783** This function returns the same value as datetime('now').
21784*/
21785static void ctimestampFunc(
21786 sqlite3_context *context,
21787 int NotUsed,
21788 sqlite3_value **NotUsed2
21789){
21790 UNUSED_PARAMETER2(NotUsed, NotUsed2);
21791 datetimeFunc(context, 0, 0);
21792}
21793#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
21794
21795#ifdef SQLITE_OMIT_DATETIME_FUNCS
21796/*
21797** If the library is compiled to omit the full-scale date and time
21798** handling (to get a smaller binary), the following minimal version
21799** of the functions current_time(), current_date() and current_timestamp()
21800** are included instead. This is to support column declarations that
21801** include "DEFAULT CURRENT_TIME" etc.
21802**
21803** This function uses the C-library functions time(), gmtime()
21804** and strftime(). The format string to pass to strftime() is supplied
21805** as the user-data for the function.
21806*/
21807static void currentTimeFunc(
21808 sqlite3_context *context,
21809 int argc,
21810 sqlite3_value **argv
21811){
21812 time_t t;
21813 char *zFormat = (char *)sqlite3_user_data(context);
21814 sqlite3_int64 iT;
21815 struct tm *pTm;
21816 struct tm sNow;
21817 char zBuf[20];
21818
21819 UNUSED_PARAMETER(argc);
21820 UNUSED_PARAMETER(argv);
21821
21822 iT = sqlite3StmtCurrentTime(context);
21823 if( iT<=0 ) return;
21824 t = iT/1000 - 10000*(sqlite3_int64)21086676;
21825#if HAVE_GMTIME_R
21826 pTm = gmtime_r(&t, &sNow);
21827#else
21828 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
21829 pTm = gmtime(&t);
21830 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
21831 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
21832#endif
21833 if( pTm ){
21834 strftime(zBuf, 20, zFormat, &sNow);
21835 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
21836 }
21837}
21838#endif
21839
21840/*
21841** This function registered all of the above C functions as SQL
21842** functions. This should be the only routine in this file with
21843** external linkage.
21844*/
21845SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
21846 static FuncDef aDateTimeFuncs[] = {
21847#ifndef SQLITE_OMIT_DATETIME_FUNCS
21848 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ),
21849 PURE_DATE(date, -1, 0, 0, dateFunc ),
21850 PURE_DATE(time, -1, 0, 0, timeFunc ),
21851 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ),
21852 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ),
21853 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ),
21854 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
21855 DFUNCTION(current_date, 0, 0, 0, cdateFunc ),
21856#else
21857 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc),
21858 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc),
21859 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
21860#endif
21861 };
21862 sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs));
21863}
21864
21865/************** End of date.c ************************************************/
21866/************** Begin file os.c **********************************************/
21867/*
21868** 2005 November 29
21869**
21870** The author disclaims copyright to this source code. In place of
21871** a legal notice, here is a blessing:
21872**
21873** May you do good and not evil.
21874** May you find forgiveness for yourself and forgive others.
21875** May you share freely, never taking more than you give.
21876**
21877******************************************************************************
21878**
21879** This file contains OS interface code that is common to all
21880** architectures.
21881*/
21882/* #include "sqliteInt.h" */
21883
21884/*
21885** If we compile with the SQLITE_TEST macro set, then the following block
21886** of code will give us the ability to simulate a disk I/O error. This
21887** is used for testing the I/O recovery logic.
21888*/
21889#if defined(SQLITE_TEST)
21890SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
21891SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
21892SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
21893SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
21894SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
21895SQLITE_API int sqlite3_diskfull_pending = 0;
21896SQLITE_API int sqlite3_diskfull = 0;
21897#endif /* defined(SQLITE_TEST) */
21898
21899/*
21900** When testing, also keep a count of the number of open files.
21901*/
21902#if defined(SQLITE_TEST)
21903SQLITE_API int sqlite3_open_file_count = 0;
21904#endif /* defined(SQLITE_TEST) */
21905
21906/*
21907** The default SQLite sqlite3_vfs implementations do not allocate
21908** memory (actually, os_unix.c allocates a small amount of memory
21909** from within OsOpen()), but some third-party implementations may.
21910** So we test the effects of a malloc() failing and the sqlite3OsXXX()
21911** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
21912**
21913** The following functions are instrumented for malloc() failure
21914** testing:
21915**
21916** sqlite3OsRead()
21917** sqlite3OsWrite()
21918** sqlite3OsSync()
21919** sqlite3OsFileSize()
21920** sqlite3OsLock()
21921** sqlite3OsCheckReservedLock()
21922** sqlite3OsFileControl()
21923** sqlite3OsShmMap()
21924** sqlite3OsOpen()
21925** sqlite3OsDelete()
21926** sqlite3OsAccess()
21927** sqlite3OsFullPathname()
21928**
21929*/
21930#if defined(SQLITE_TEST)
21931SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
21932 #define DO_OS_MALLOC_TEST(x) \
21933 if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
21934 void *pTstAlloc = sqlite3Malloc(10); \
21935 if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT; \
21936 sqlite3_free(pTstAlloc); \
21937 }
21938#else
21939 #define DO_OS_MALLOC_TEST(x)
21940#endif
21941
21942/*
21943** The following routines are convenience wrappers around methods
21944** of the sqlite3_file object. This is mostly just syntactic sugar. All
21945** of this would be completely automatic if SQLite were coded using
21946** C++ instead of plain old C.
21947*/
21948SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file *pId){
21949 if( pId->pMethods ){
21950 pId->pMethods->xClose(pId);
21951 pId->pMethods = 0;
21952 }
21953}
21954SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
21955 DO_OS_MALLOC_TEST(id);
21956 return id->pMethods->xRead(id, pBuf, amt, offset);
21957}
21958SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
21959 DO_OS_MALLOC_TEST(id);
21960 return id->pMethods->xWrite(id, pBuf, amt, offset);
21961}
21962SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
21963 return id->pMethods->xTruncate(id, size);
21964}
21965SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
21966 DO_OS_MALLOC_TEST(id);
21967 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
21968}
21969SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
21970 DO_OS_MALLOC_TEST(id);
21971 return id->pMethods->xFileSize(id, pSize);
21972}
21973SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
21974 DO_OS_MALLOC_TEST(id);
21975 return id->pMethods->xLock(id, lockType);
21976}
21977SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
21978 return id->pMethods->xUnlock(id, lockType);
21979}
21980SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
21981 DO_OS_MALLOC_TEST(id);
21982 return id->pMethods->xCheckReservedLock(id, pResOut);
21983}
21984
21985/*
21986** Use sqlite3OsFileControl() when we are doing something that might fail
21987** and we need to know about the failures. Use sqlite3OsFileControlHint()
21988** when simply tossing information over the wall to the VFS and we do not
21989** really care if the VFS receives and understands the information since it
21990** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
21991** routine has no return value since the return value would be meaningless.
21992*/
21993SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
21994 if( id->pMethods==0 ) return SQLITE_NOTFOUND;
21995#ifdef SQLITE_TEST
21996 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
21997 && op!=SQLITE_FCNTL_LOCK_TIMEOUT
21998 ){
21999 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
22000 ** is using a regular VFS, it is called after the corresponding
22001 ** transaction has been committed. Injecting a fault at this point
22002 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
22003 ** but the transaction is committed anyway.
22004 **
22005 ** The core must call OsFileControl() though, not OsFileControlHint(),
22006 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
22007 ** means the commit really has failed and an error should be returned
22008 ** to the user. */
22009 DO_OS_MALLOC_TEST(id);
22010 }
22011#endif
22012 return id->pMethods->xFileControl(id, op, pArg);
22013}
22014SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
22015 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
22016}
22017
22018SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
22019 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
22020 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
22021}
22022SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
22023 return id->pMethods->xDeviceCharacteristics(id);
22024}
22025#ifndef SQLITE_OMIT_WAL
22026SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
22027 return id->pMethods->xShmLock(id, offset, n, flags);
22028}
22029SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
22030 id->pMethods->xShmBarrier(id);
22031}
22032SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
22033 return id->pMethods->xShmUnmap(id, deleteFlag);
22034}
22035SQLITE_PRIVATE int sqlite3OsShmMap(
22036 sqlite3_file *id, /* Database file handle */
22037 int iPage,
22038 int pgsz,
22039 int bExtend, /* True to extend file if necessary */
22040 void volatile **pp /* OUT: Pointer to mapping */
22041){
22042 DO_OS_MALLOC_TEST(id);
22043 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
22044}
22045#endif /* SQLITE_OMIT_WAL */
22046
22047#if SQLITE_MAX_MMAP_SIZE>0
22048/* The real implementation of xFetch and xUnfetch */
22049SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
22050 DO_OS_MALLOC_TEST(id);
22051 return id->pMethods->xFetch(id, iOff, iAmt, pp);
22052}
22053SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
22054 return id->pMethods->xUnfetch(id, iOff, p);
22055}
22056#else
22057/* No-op stubs to use when memory-mapped I/O is disabled */
22058SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
22059 *pp = 0;
22060 return SQLITE_OK;
22061}
22062SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
22063 return SQLITE_OK;
22064}
22065#endif
22066
22067/*
22068** The next group of routines are convenience wrappers around the
22069** VFS methods.
22070*/
22071SQLITE_PRIVATE int sqlite3OsOpen(
22072 sqlite3_vfs *pVfs,
22073 const char *zPath,
22074 sqlite3_file *pFile,
22075 int flags,
22076 int *pFlagsOut
22077){
22078 int rc;
22079 DO_OS_MALLOC_TEST(0);
22080 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
22081 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
22082 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
22083 ** reaching the VFS. */
22084 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
22085 assert( rc==SQLITE_OK || pFile->pMethods==0 );
22086 return rc;
22087}
22088SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
22089 DO_OS_MALLOC_TEST(0);
22090 assert( dirSync==0 || dirSync==1 );
22091 return pVfs->xDelete(pVfs, zPath, dirSync);
22092}
22093SQLITE_PRIVATE int sqlite3OsAccess(
22094 sqlite3_vfs *pVfs,
22095 const char *zPath,
22096 int flags,
22097 int *pResOut
22098){
22099 DO_OS_MALLOC_TEST(0);
22100 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
22101}
22102SQLITE_PRIVATE int sqlite3OsFullPathname(
22103 sqlite3_vfs *pVfs,
22104 const char *zPath,
22105 int nPathOut,
22106 char *zPathOut
22107){
22108 DO_OS_MALLOC_TEST(0);
22109 zPathOut[0] = 0;
22110 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
22111}
22112#ifndef SQLITE_OMIT_LOAD_EXTENSION
22113SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
22114 return pVfs->xDlOpen(pVfs, zPath);
22115}
22116SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
22117 pVfs->xDlError(pVfs, nByte, zBufOut);
22118}
22119SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
22120 return pVfs->xDlSym(pVfs, pHdle, zSym);
22121}
22122SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
22123 pVfs->xDlClose(pVfs, pHandle);
22124}
22125#endif /* SQLITE_OMIT_LOAD_EXTENSION */
22126SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
22127 return pVfs->xRandomness(pVfs, nByte, zBufOut);
22128}
22129SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
22130 return pVfs->xSleep(pVfs, nMicro);
22131}
22132SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
22133 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
22134}
22135SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
22136 int rc;
22137 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
22138 ** method to get the current date and time if that method is available
22139 ** (if iVersion is 2 or greater and the function pointer is not NULL) and
22140 ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
22141 ** unavailable.
22142 */
22143 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
22144 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
22145 }else{
22146 double r;
22147 rc = pVfs->xCurrentTime(pVfs, &r);
22148 *pTimeOut = (sqlite3_int64)(r*86400000.0);
22149 }
22150 return rc;
22151}
22152
22153SQLITE_PRIVATE int sqlite3OsOpenMalloc(
22154 sqlite3_vfs *pVfs,
22155 const char *zFile,
22156 sqlite3_file **ppFile,
22157 int flags,
22158 int *pOutFlags
22159){
22160 int rc;
22161 sqlite3_file *pFile;
22162 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
22163 if( pFile ){
22164 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
22165 if( rc!=SQLITE_OK ){
22166 sqlite3_free(pFile);
22167 }else{
22168 *ppFile = pFile;
22169 }
22170 }else{
22171 rc = SQLITE_NOMEM_BKPT;
22172 }
22173 return rc;
22174}
22175SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){
22176 assert( pFile );
22177 sqlite3OsClose(pFile);
22178 sqlite3_free(pFile);
22179}
22180
22181/*
22182** This function is a wrapper around the OS specific implementation of
22183** sqlite3_os_init(). The purpose of the wrapper is to provide the
22184** ability to simulate a malloc failure, so that the handling of an
22185** error in sqlite3_os_init() by the upper layers can be tested.
22186*/
22187SQLITE_PRIVATE int sqlite3OsInit(void){
22188 void *p = sqlite3_malloc(10);
22189 if( p==0 ) return SQLITE_NOMEM_BKPT;
22190 sqlite3_free(p);
22191 return sqlite3_os_init();
22192}
22193
22194/*
22195** The list of all registered VFS implementations.
22196*/
22197static sqlite3_vfs * SQLITE_WSD vfsList = 0;
22198#define vfsList GLOBAL(sqlite3_vfs *, vfsList)
22199
22200/*
22201** Locate a VFS by name. If no name is given, simply return the
22202** first VFS on the list.
22203*/
22204SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
22205 sqlite3_vfs *pVfs = 0;
22206#if SQLITE_THREADSAFE
22207 sqlite3_mutex *mutex;
22208#endif
22209#ifndef SQLITE_OMIT_AUTOINIT
22210 int rc = sqlite3_initialize();
22211 if( rc ) return 0;
22212#endif
22213#if SQLITE_THREADSAFE
22214 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
22215#endif
22216 sqlite3_mutex_enter(mutex);
22217 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
22218 if( zVfs==0 ) break;
22219 if( strcmp(zVfs, pVfs->zName)==0 ) break;
22220 }
22221 sqlite3_mutex_leave(mutex);
22222 return pVfs;
22223}
22224
22225/*
22226** Unlink a VFS from the linked list
22227*/
22228static void vfsUnlink(sqlite3_vfs *pVfs){
22229 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
22230 if( pVfs==0 ){
22231 /* No-op */
22232 }else if( vfsList==pVfs ){
22233 vfsList = pVfs->pNext;
22234 }else if( vfsList ){
22235 sqlite3_vfs *p = vfsList;
22236 while( p->pNext && p->pNext!=pVfs ){
22237 p = p->pNext;
22238 }
22239 if( p->pNext==pVfs ){
22240 p->pNext = pVfs->pNext;
22241 }
22242 }
22243}
22244
22245/*
22246** Register a VFS with the system. It is harmless to register the same
22247** VFS multiple times. The new VFS becomes the default if makeDflt is
22248** true.
22249*/
22250SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
22251 MUTEX_LOGIC(sqlite3_mutex *mutex;)
22252#ifndef SQLITE_OMIT_AUTOINIT
22253 int rc = sqlite3_initialize();
22254 if( rc ) return rc;
22255#endif
22256#ifdef SQLITE_ENABLE_API_ARMOR
22257 if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
22258#endif
22259
22260 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
22261 sqlite3_mutex_enter(mutex);
22262 vfsUnlink(pVfs);
22263 if( makeDflt || vfsList==0 ){
22264 pVfs->pNext = vfsList;
22265 vfsList = pVfs;
22266 }else{
22267 pVfs->pNext = vfsList->pNext;
22268 vfsList->pNext = pVfs;
22269 }
22270 assert(vfsList);
22271 sqlite3_mutex_leave(mutex);
22272 return SQLITE_OK;
22273}
22274
22275/*
22276** Unregister a VFS so that it is no longer accessible.
22277*/
22278SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
22279 MUTEX_LOGIC(sqlite3_mutex *mutex;)
22280#ifndef SQLITE_OMIT_AUTOINIT
22281 int rc = sqlite3_initialize();
22282 if( rc ) return rc;
22283#endif
22284 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
22285 sqlite3_mutex_enter(mutex);
22286 vfsUnlink(pVfs);
22287 sqlite3_mutex_leave(mutex);
22288 return SQLITE_OK;
22289}
22290
22291/************** End of os.c **************************************************/
22292/************** Begin file fault.c *******************************************/
22293/*
22294** 2008 Jan 22
22295**
22296** The author disclaims copyright to this source code. In place of
22297** a legal notice, here is a blessing:
22298**
22299** May you do good and not evil.
22300** May you find forgiveness for yourself and forgive others.
22301** May you share freely, never taking more than you give.
22302**
22303*************************************************************************
22304**
22305** This file contains code to support the concept of "benign"
22306** malloc failures (when the xMalloc() or xRealloc() method of the
22307** sqlite3_mem_methods structure fails to allocate a block of memory
22308** and returns 0).
22309**
22310** Most malloc failures are non-benign. After they occur, SQLite
22311** abandons the current operation and returns an error code (usually
22312** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
22313** fatal. For example, if a malloc fails while resizing a hash table, this
22314** is completely recoverable simply by not carrying out the resize. The
22315** hash table will continue to function normally. So a malloc failure
22316** during a hash table resize is a benign fault.
22317*/
22318
22319/* #include "sqliteInt.h" */
22320
22321#ifndef SQLITE_UNTESTABLE
22322
22323/*
22324** Global variables.
22325*/
22326typedef struct BenignMallocHooks BenignMallocHooks;
22327static SQLITE_WSD struct BenignMallocHooks {
22328 void (*xBenignBegin)(void);
22329 void (*xBenignEnd)(void);
22330} sqlite3Hooks = { 0, 0 };
22331
22332/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
22333** structure. If writable static data is unsupported on the target,
22334** we have to locate the state vector at run-time. In the more common
22335** case where writable static data is supported, wsdHooks can refer directly
22336** to the "sqlite3Hooks" state vector declared above.
22337*/
22338#ifdef SQLITE_OMIT_WSD
22339# define wsdHooksInit \
22340 BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
22341# define wsdHooks x[0]
22342#else
22343# define wsdHooksInit
22344# define wsdHooks sqlite3Hooks
22345#endif
22346
22347
22348/*
22349** Register hooks to call when sqlite3BeginBenignMalloc() and
22350** sqlite3EndBenignMalloc() are called, respectively.
22351*/
22352SQLITE_PRIVATE void sqlite3BenignMallocHooks(
22353 void (*xBenignBegin)(void),
22354 void (*xBenignEnd)(void)
22355){
22356 wsdHooksInit;
22357 wsdHooks.xBenignBegin = xBenignBegin;
22358 wsdHooks.xBenignEnd = xBenignEnd;
22359}
22360
22361/*
22362** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
22363** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
22364** indicates that subsequent malloc failures are non-benign.
22365*/
22366SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
22367 wsdHooksInit;
22368 if( wsdHooks.xBenignBegin ){
22369 wsdHooks.xBenignBegin();
22370 }
22371}
22372SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
22373 wsdHooksInit;
22374 if( wsdHooks.xBenignEnd ){
22375 wsdHooks.xBenignEnd();
22376 }
22377}
22378
22379#endif /* #ifndef SQLITE_UNTESTABLE */
22380
22381/************** End of fault.c ***********************************************/
22382/************** Begin file mem0.c ********************************************/
22383/*
22384** 2008 October 28
22385**
22386** The author disclaims copyright to this source code. In place of
22387** a legal notice, here is a blessing:
22388**
22389** May you do good and not evil.
22390** May you find forgiveness for yourself and forgive others.
22391** May you share freely, never taking more than you give.
22392**
22393*************************************************************************
22394**
22395** This file contains a no-op memory allocation drivers for use when
22396** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
22397** here always fail. SQLite will not operate with these drivers. These
22398** are merely placeholders. Real drivers must be substituted using
22399** sqlite3_config() before SQLite will operate.
22400*/
22401/* #include "sqliteInt.h" */
22402
22403/*
22404** This version of the memory allocator is the default. It is
22405** used when no other memory allocator is specified using compile-time
22406** macros.
22407*/
22408#ifdef SQLITE_ZERO_MALLOC
22409
22410/*
22411** No-op versions of all memory allocation routines
22412*/
22413static void *sqlite3MemMalloc(int nByte){ return 0; }
22414static void sqlite3MemFree(void *pPrior){ return; }
22415static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
22416static int sqlite3MemSize(void *pPrior){ return 0; }
22417static int sqlite3MemRoundup(int n){ return n; }
22418static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
22419static void sqlite3MemShutdown(void *NotUsed){ return; }
22420
22421/*
22422** This routine is the only routine in this file with external linkage.
22423**
22424** Populate the low-level memory allocation function pointers in
22425** sqlite3GlobalConfig.m with pointers to the routines in this file.
22426*/
22427SQLITE_PRIVATE void sqlite3MemSetDefault(void){
22428 static const sqlite3_mem_methods defaultMethods = {
22429 sqlite3MemMalloc,
22430 sqlite3MemFree,
22431 sqlite3MemRealloc,
22432 sqlite3MemSize,
22433 sqlite3MemRoundup,
22434 sqlite3MemInit,
22435 sqlite3MemShutdown,
22436 0
22437 };
22438 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
22439}
22440
22441#endif /* SQLITE_ZERO_MALLOC */
22442
22443/************** End of mem0.c ************************************************/
22444/************** Begin file mem1.c ********************************************/
22445/*
22446** 2007 August 14
22447**
22448** The author disclaims copyright to this source code. In place of
22449** a legal notice, here is a blessing:
22450**
22451** May you do good and not evil.
22452** May you find forgiveness for yourself and forgive others.
22453** May you share freely, never taking more than you give.
22454**
22455*************************************************************************
22456**
22457** This file contains low-level memory allocation drivers for when
22458** SQLite will use the standard C-library malloc/realloc/free interface
22459** to obtain the memory it needs.
22460**
22461** This file contains implementations of the low-level memory allocation
22462** routines specified in the sqlite3_mem_methods object. The content of
22463** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The
22464** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
22465** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The
22466** default configuration is to use memory allocation routines in this
22467** file.
22468**
22469** C-preprocessor macro summary:
22470**
22471** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if
22472** the malloc_usable_size() interface exists
22473** on the target platform. Or, this symbol
22474** can be set manually, if desired.
22475** If an equivalent interface exists by
22476** a different name, using a separate -D
22477** option to rename it.
22478**
22479** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone
22480** memory allocator. Set this symbol to enable
22481** building on older macs.
22482**
22483** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
22484** _msize() on windows systems. This might
22485** be necessary when compiling for Delphi,
22486** for example.
22487*/
22488/* #include "sqliteInt.h" */
22489
22490/*
22491** This version of the memory allocator is the default. It is
22492** used when no other memory allocator is specified using compile-time
22493** macros.
22494*/
22495#ifdef SQLITE_SYSTEM_MALLOC
22496#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
22497
22498/*
22499** Use the zone allocator available on apple products unless the
22500** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
22501*/
22502#include <sys/sysctl.h>
22503#include <malloc/malloc.h>
22504#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
22505#include <libkern/OSAtomic.h>
22506#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
22507static malloc_zone_t* _sqliteZone_;
22508#define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
22509#define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
22510#define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
22511#define SQLITE_MALLOCSIZE(x) \
22512 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
22513
22514#else /* if not __APPLE__ */
22515
22516/*
22517** Use standard C library malloc and free on non-Apple systems.
22518** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
22519*/
22520#define SQLITE_MALLOC(x) malloc(x)
22521#define SQLITE_FREE(x) free(x)
22522#define SQLITE_REALLOC(x,y) realloc((x),(y))
22523
22524/*
22525** The malloc.h header file is needed for malloc_usable_size() function
22526** on some systems (e.g. Linux).
22527*/
22528#if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
22529# define SQLITE_USE_MALLOC_H 1
22530# define SQLITE_USE_MALLOC_USABLE_SIZE 1
22531/*
22532** The MSVCRT has malloc_usable_size(), but it is called _msize(). The
22533** use of _msize() is automatic, but can be disabled by compiling with
22534** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
22535** the malloc.h header file.
22536*/
22537#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
22538# define SQLITE_USE_MALLOC_H
22539# define SQLITE_USE_MSIZE
22540#endif
22541
22542/*
22543** Include the malloc.h header file, if necessary. Also set define macro
22544** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
22545** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
22546** The memory size function can always be overridden manually by defining
22547** the macro SQLITE_MALLOCSIZE to the desired function name.
22548*/
22549#if defined(SQLITE_USE_MALLOC_H)
22550# include <malloc.h>
22551# if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
22552# if !defined(SQLITE_MALLOCSIZE)
22553# define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
22554# endif
22555# elif defined(SQLITE_USE_MSIZE)
22556# if !defined(SQLITE_MALLOCSIZE)
22557# define SQLITE_MALLOCSIZE _msize
22558# endif
22559# endif
22560#endif /* defined(SQLITE_USE_MALLOC_H) */
22561
22562#endif /* __APPLE__ or not __APPLE__ */
22563
22564/*
22565** Like malloc(), but remember the size of the allocation
22566** so that we can find it later using sqlite3MemSize().
22567**
22568** For this low-level routine, we are guaranteed that nByte>0 because
22569** cases of nByte<=0 will be intercepted and dealt with by higher level
22570** routines.
22571*/
22572static void *sqlite3MemMalloc(int nByte){
22573#ifdef SQLITE_MALLOCSIZE
22574 void *p;
22575 testcase( ROUND8(nByte)==nByte );
22576 p = SQLITE_MALLOC( nByte );
22577 if( p==0 ){
22578 testcase( sqlite3GlobalConfig.xLog!=0 );
22579 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
22580 }
22581 return p;
22582#else
22583 sqlite3_int64 *p;
22584 assert( nByte>0 );
22585 testcase( ROUND8(nByte)!=nByte );
22586 p = SQLITE_MALLOC( nByte+8 );
22587 if( p ){
22588 p[0] = nByte;
22589 p++;
22590 }else{
22591 testcase( sqlite3GlobalConfig.xLog!=0 );
22592 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
22593 }
22594 return (void *)p;
22595#endif
22596}
22597
22598/*
22599** Like free() but works for allocations obtained from sqlite3MemMalloc()
22600** or sqlite3MemRealloc().
22601**
22602** For this low-level routine, we already know that pPrior!=0 since
22603** cases where pPrior==0 will have been intecepted and dealt with
22604** by higher-level routines.
22605*/
22606static void sqlite3MemFree(void *pPrior){
22607#ifdef SQLITE_MALLOCSIZE
22608 SQLITE_FREE(pPrior);
22609#else
22610 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
22611 assert( pPrior!=0 );
22612 p--;
22613 SQLITE_FREE(p);
22614#endif
22615}
22616
22617/*
22618** Report the allocated size of a prior return from xMalloc()
22619** or xRealloc().
22620*/
22621static int sqlite3MemSize(void *pPrior){
22622#ifdef SQLITE_MALLOCSIZE
22623 assert( pPrior!=0 );
22624 return (int)SQLITE_MALLOCSIZE(pPrior);
22625#else
22626 sqlite3_int64 *p;
22627 assert( pPrior!=0 );
22628 p = (sqlite3_int64*)pPrior;
22629 p--;
22630 return (int)p[0];
22631#endif
22632}
22633
22634/*
22635** Like realloc(). Resize an allocation previously obtained from
22636** sqlite3MemMalloc().
22637**
22638** For this low-level interface, we know that pPrior!=0. Cases where
22639** pPrior==0 while have been intercepted by higher-level routine and
22640** redirected to xMalloc. Similarly, we know that nByte>0 because
22641** cases where nByte<=0 will have been intercepted by higher-level
22642** routines and redirected to xFree.
22643*/
22644static void *sqlite3MemRealloc(void *pPrior, int nByte){
22645#ifdef SQLITE_MALLOCSIZE
22646 void *p = SQLITE_REALLOC(pPrior, nByte);
22647 if( p==0 ){
22648 testcase( sqlite3GlobalConfig.xLog!=0 );
22649 sqlite3_log(SQLITE_NOMEM,
22650 "failed memory resize %u to %u bytes",
22651 SQLITE_MALLOCSIZE(pPrior), nByte);
22652 }
22653 return p;
22654#else
22655 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
22656 assert( pPrior!=0 && nByte>0 );
22657 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
22658 p--;
22659 p = SQLITE_REALLOC(p, nByte+8 );
22660 if( p ){
22661 p[0] = nByte;
22662 p++;
22663 }else{
22664 testcase( sqlite3GlobalConfig.xLog!=0 );
22665 sqlite3_log(SQLITE_NOMEM,
22666 "failed memory resize %u to %u bytes",
22667 sqlite3MemSize(pPrior), nByte);
22668 }
22669 return (void*)p;
22670#endif
22671}
22672
22673/*
22674** Round up a request size to the next valid allocation size.
22675*/
22676static int sqlite3MemRoundup(int n){
22677 return ROUND8(n);
22678}
22679
22680/*
22681** Initialize this module.
22682*/
22683static int sqlite3MemInit(void *NotUsed){
22684#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
22685 int cpuCount;
22686 size_t len;
22687 if( _sqliteZone_ ){
22688 return SQLITE_OK;
22689 }
22690 len = sizeof(cpuCount);
22691 /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
22692 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
22693 if( cpuCount>1 ){
22694 /* defer MT decisions to system malloc */
22695 _sqliteZone_ = malloc_default_zone();
22696 }else{
22697 /* only 1 core, use our own zone to contention over global locks,
22698 ** e.g. we have our own dedicated locks */
22699 _sqliteZone_ = malloc_create_zone(4096, 0);
22700 malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
22701 }
22702#endif /* defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
22703 UNUSED_PARAMETER(NotUsed);
22704 return SQLITE_OK;
22705}
22706
22707/*
22708** Deinitialize this module.
22709*/
22710static void sqlite3MemShutdown(void *NotUsed){
22711 UNUSED_PARAMETER(NotUsed);
22712 return;
22713}
22714
22715/*
22716** This routine is the only routine in this file with external linkage.
22717**
22718** Populate the low-level memory allocation function pointers in
22719** sqlite3GlobalConfig.m with pointers to the routines in this file.
22720*/
22721SQLITE_PRIVATE void sqlite3MemSetDefault(void){
22722 static const sqlite3_mem_methods defaultMethods = {
22723 sqlite3MemMalloc,
22724 sqlite3MemFree,
22725 sqlite3MemRealloc,
22726 sqlite3MemSize,
22727 sqlite3MemRoundup,
22728 sqlite3MemInit,
22729 sqlite3MemShutdown,
22730 0
22731 };
22732 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
22733}
22734
22735#endif /* SQLITE_SYSTEM_MALLOC */
22736
22737/************** End of mem1.c ************************************************/
22738/************** Begin file mem2.c ********************************************/
22739/*
22740** 2007 August 15
22741**
22742** The author disclaims copyright to this source code. In place of
22743** a legal notice, here is a blessing:
22744**
22745** May you do good and not evil.
22746** May you find forgiveness for yourself and forgive others.
22747** May you share freely, never taking more than you give.
22748**
22749*************************************************************************
22750**
22751** This file contains low-level memory allocation drivers for when
22752** SQLite will use the standard C-library malloc/realloc/free interface
22753** to obtain the memory it needs while adding lots of additional debugging
22754** information to each allocation in order to help detect and fix memory
22755** leaks and memory usage errors.
22756**
22757** This file contains implementations of the low-level memory allocation
22758** routines specified in the sqlite3_mem_methods object.
22759*/
22760/* #include "sqliteInt.h" */
22761
22762/*
22763** This version of the memory allocator is used only if the
22764** SQLITE_MEMDEBUG macro is defined
22765*/
22766#ifdef SQLITE_MEMDEBUG
22767
22768/*
22769** The backtrace functionality is only available with GLIBC
22770*/
22771#ifdef __GLIBC__
22772 extern int backtrace(void**,int);
22773 extern void backtrace_symbols_fd(void*const*,int,int);
22774#else
22775# define backtrace(A,B) 1
22776# define backtrace_symbols_fd(A,B,C)
22777#endif
22778/* #include <stdio.h> */
22779
22780/*
22781** Each memory allocation looks like this:
22782**
22783** ------------------------------------------------------------------------
22784** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
22785** ------------------------------------------------------------------------
22786**
22787** The application code sees only a pointer to the allocation. We have
22788** to back up from the allocation pointer to find the MemBlockHdr. The
22789** MemBlockHdr tells us the size of the allocation and the number of
22790** backtrace pointers. There is also a guard word at the end of the
22791** MemBlockHdr.
22792*/
22793struct MemBlockHdr {
22794 i64 iSize; /* Size of this allocation */
22795 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
22796 char nBacktrace; /* Number of backtraces on this alloc */
22797 char nBacktraceSlots; /* Available backtrace slots */
22798 u8 nTitle; /* Bytes of title; includes '\0' */
22799 u8 eType; /* Allocation type code */
22800 int iForeGuard; /* Guard word for sanity */
22801};
22802
22803/*
22804** Guard words
22805*/
22806#define FOREGUARD 0x80F5E153
22807#define REARGUARD 0xE4676B53
22808
22809/*
22810** Number of malloc size increments to track.
22811*/
22812#define NCSIZE 1000
22813
22814/*
22815** All of the static variables used by this module are collected
22816** into a single structure named "mem". This is to keep the
22817** static variables organized and to reduce namespace pollution
22818** when this module is combined with other in the amalgamation.
22819*/
22820static struct {
22821
22822 /*
22823 ** Mutex to control access to the memory allocation subsystem.
22824 */
22825 sqlite3_mutex *mutex;
22826
22827 /*
22828 ** Head and tail of a linked list of all outstanding allocations
22829 */
22830 struct MemBlockHdr *pFirst;
22831 struct MemBlockHdr *pLast;
22832
22833 /*
22834 ** The number of levels of backtrace to save in new allocations.
22835 */
22836 int nBacktrace;
22837 void (*xBacktrace)(int, int, void **);
22838
22839 /*
22840 ** Title text to insert in front of each block
22841 */
22842 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
22843 char zTitle[100]; /* The title text */
22844
22845 /*
22846 ** sqlite3MallocDisallow() increments the following counter.
22847 ** sqlite3MallocAllow() decrements it.
22848 */
22849 int disallow; /* Do not allow memory allocation */
22850
22851 /*
22852 ** Gather statistics on the sizes of memory allocations.
22853 ** nAlloc[i] is the number of allocation attempts of i*8
22854 ** bytes. i==NCSIZE is the number of allocation attempts for
22855 ** sizes more than NCSIZE*8 bytes.
22856 */
22857 int nAlloc[NCSIZE]; /* Total number of allocations */
22858 int nCurrent[NCSIZE]; /* Current number of allocations */
22859 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
22860
22861} mem;
22862
22863
22864/*
22865** Adjust memory usage statistics
22866*/
22867static void adjustStats(int iSize, int increment){
22868 int i = ROUND8(iSize)/8;
22869 if( i>NCSIZE-1 ){
22870 i = NCSIZE - 1;
22871 }
22872 if( increment>0 ){
22873 mem.nAlloc[i]++;
22874 mem.nCurrent[i]++;
22875 if( mem.nCurrent[i]>mem.mxCurrent[i] ){
22876 mem.mxCurrent[i] = mem.nCurrent[i];
22877 }
22878 }else{
22879 mem.nCurrent[i]--;
22880 assert( mem.nCurrent[i]>=0 );
22881 }
22882}
22883
22884/*
22885** Given an allocation, find the MemBlockHdr for that allocation.
22886**
22887** This routine checks the guards at either end of the allocation and
22888** if they are incorrect it asserts.
22889*/
22890static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
22891 struct MemBlockHdr *p;
22892 int *pInt;
22893 u8 *pU8;
22894 int nReserve;
22895
22896 p = (struct MemBlockHdr*)pAllocation;
22897 p--;
22898 assert( p->iForeGuard==(int)FOREGUARD );
22899 nReserve = ROUND8(p->iSize);
22900 pInt = (int*)pAllocation;
22901 pU8 = (u8*)pAllocation;
22902 assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
22903 /* This checks any of the "extra" bytes allocated due
22904 ** to rounding up to an 8 byte boundary to ensure
22905 ** they haven't been overwritten.
22906 */
22907 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
22908 return p;
22909}
22910
22911/*
22912** Return the number of bytes currently allocated at address p.
22913*/
22914static int sqlite3MemSize(void *p){
22915 struct MemBlockHdr *pHdr;
22916 if( !p ){
22917 return 0;
22918 }
22919 pHdr = sqlite3MemsysGetHeader(p);
22920 return (int)pHdr->iSize;
22921}
22922
22923/*
22924** Initialize the memory allocation subsystem.
22925*/
22926static int sqlite3MemInit(void *NotUsed){
22927 UNUSED_PARAMETER(NotUsed);
22928 assert( (sizeof(struct MemBlockHdr)&7) == 0 );
22929 if( !sqlite3GlobalConfig.bMemstat ){
22930 /* If memory status is enabled, then the malloc.c wrapper will already
22931 ** hold the STATIC_MEM mutex when the routines here are invoked. */
22932 mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
22933 }
22934 return SQLITE_OK;
22935}
22936
22937/*
22938** Deinitialize the memory allocation subsystem.
22939*/
22940static void sqlite3MemShutdown(void *NotUsed){
22941 UNUSED_PARAMETER(NotUsed);
22942 mem.mutex = 0;
22943}
22944
22945/*
22946** Round up a request size to the next valid allocation size.
22947*/
22948static int sqlite3MemRoundup(int n){
22949 return ROUND8(n);
22950}
22951
22952/*
22953** Fill a buffer with pseudo-random bytes. This is used to preset
22954** the content of a new memory allocation to unpredictable values and
22955** to clear the content of a freed allocation to unpredictable values.
22956*/
22957static void randomFill(char *pBuf, int nByte){
22958 unsigned int x, y, r;
22959 x = SQLITE_PTR_TO_INT(pBuf);
22960 y = nByte | 1;
22961 while( nByte >= 4 ){
22962 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
22963 y = y*1103515245 + 12345;
22964 r = x ^ y;
22965 *(int*)pBuf = r;
22966 pBuf += 4;
22967 nByte -= 4;
22968 }
22969 while( nByte-- > 0 ){
22970 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
22971 y = y*1103515245 + 12345;
22972 r = x ^ y;
22973 *(pBuf++) = r & 0xff;
22974 }
22975}
22976
22977/*
22978** Allocate nByte bytes of memory.
22979*/
22980static void *sqlite3MemMalloc(int nByte){
22981 struct MemBlockHdr *pHdr;
22982 void **pBt;
22983 char *z;
22984 int *pInt;
22985 void *p = 0;
22986 int totalSize;
22987 int nReserve;
22988 sqlite3_mutex_enter(mem.mutex);
22989 assert( mem.disallow==0 );
22990 nReserve = ROUND8(nByte);
22991 totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
22992 mem.nBacktrace*sizeof(void*) + mem.nTitle;
22993 p = malloc(totalSize);
22994 if( p ){
22995 z = p;
22996 pBt = (void**)&z[mem.nTitle];
22997 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
22998 pHdr->pNext = 0;
22999 pHdr->pPrev = mem.pLast;
23000 if( mem.pLast ){
23001 mem.pLast->pNext = pHdr;
23002 }else{
23003 mem.pFirst = pHdr;
23004 }
23005 mem.pLast = pHdr;
23006 pHdr->iForeGuard = FOREGUARD;
23007 pHdr->eType = MEMTYPE_HEAP;
23008 pHdr->nBacktraceSlots = mem.nBacktrace;
23009 pHdr->nTitle = mem.nTitle;
23010 if( mem.nBacktrace ){
23011 void *aAddr[40];
23012 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
23013 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
23014 assert(pBt[0]);
23015 if( mem.xBacktrace ){
23016 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
23017 }
23018 }else{
23019 pHdr->nBacktrace = 0;
23020 }
23021 if( mem.nTitle ){
23022 memcpy(z, mem.zTitle, mem.nTitle);
23023 }
23024 pHdr->iSize = nByte;
23025 adjustStats(nByte, +1);
23026 pInt = (int*)&pHdr[1];
23027 pInt[nReserve/sizeof(int)] = REARGUARD;
23028 randomFill((char*)pInt, nByte);
23029 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
23030 p = (void*)pInt;
23031 }
23032 sqlite3_mutex_leave(mem.mutex);
23033 return p;
23034}
23035
23036/*
23037** Free memory.
23038*/
23039static void sqlite3MemFree(void *pPrior){
23040 struct MemBlockHdr *pHdr;
23041 void **pBt;
23042 char *z;
23043 assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
23044 || mem.mutex!=0 );
23045 pHdr = sqlite3MemsysGetHeader(pPrior);
23046 pBt = (void**)pHdr;
23047 pBt -= pHdr->nBacktraceSlots;
23048 sqlite3_mutex_enter(mem.mutex);
23049 if( pHdr->pPrev ){
23050 assert( pHdr->pPrev->pNext==pHdr );
23051 pHdr->pPrev->pNext = pHdr->pNext;
23052 }else{
23053 assert( mem.pFirst==pHdr );
23054 mem.pFirst = pHdr->pNext;
23055 }
23056 if( pHdr->pNext ){
23057 assert( pHdr->pNext->pPrev==pHdr );
23058 pHdr->pNext->pPrev = pHdr->pPrev;
23059 }else{
23060 assert( mem.pLast==pHdr );
23061 mem.pLast = pHdr->pPrev;
23062 }
23063 z = (char*)pBt;
23064 z -= pHdr->nTitle;
23065 adjustStats((int)pHdr->iSize, -1);
23066 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
23067 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
23068 free(z);
23069 sqlite3_mutex_leave(mem.mutex);
23070}
23071
23072/*
23073** Change the size of an existing memory allocation.
23074**
23075** For this debugging implementation, we *always* make a copy of the
23076** allocation into a new place in memory. In this way, if the
23077** higher level code is using pointer to the old allocation, it is
23078** much more likely to break and we are much more liking to find
23079** the error.
23080*/
23081static void *sqlite3MemRealloc(void *pPrior, int nByte){
23082 struct MemBlockHdr *pOldHdr;
23083 void *pNew;
23084 assert( mem.disallow==0 );
23085 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */
23086 pOldHdr = sqlite3MemsysGetHeader(pPrior);
23087 pNew = sqlite3MemMalloc(nByte);
23088 if( pNew ){
23089 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
23090 if( nByte>pOldHdr->iSize ){
23091 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
23092 }
23093 sqlite3MemFree(pPrior);
23094 }
23095 return pNew;
23096}
23097
23098/*
23099** Populate the low-level memory allocation function pointers in
23100** sqlite3GlobalConfig.m with pointers to the routines in this file.
23101*/
23102SQLITE_PRIVATE void sqlite3MemSetDefault(void){
23103 static const sqlite3_mem_methods defaultMethods = {
23104 sqlite3MemMalloc,
23105 sqlite3MemFree,
23106 sqlite3MemRealloc,
23107 sqlite3MemSize,
23108 sqlite3MemRoundup,
23109 sqlite3MemInit,
23110 sqlite3MemShutdown,
23111 0
23112 };
23113 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
23114}
23115
23116/*
23117** Set the "type" of an allocation.
23118*/
23119SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
23120 if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
23121 struct MemBlockHdr *pHdr;
23122 pHdr = sqlite3MemsysGetHeader(p);
23123 assert( pHdr->iForeGuard==FOREGUARD );
23124 pHdr->eType = eType;
23125 }
23126}
23127
23128/*
23129** Return TRUE if the mask of type in eType matches the type of the
23130** allocation p. Also return true if p==NULL.
23131**
23132** This routine is designed for use within an assert() statement, to
23133** verify the type of an allocation. For example:
23134**
23135** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
23136*/
23137SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
23138 int rc = 1;
23139 if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
23140 struct MemBlockHdr *pHdr;
23141 pHdr = sqlite3MemsysGetHeader(p);
23142 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
23143 if( (pHdr->eType&eType)==0 ){
23144 rc = 0;
23145 }
23146 }
23147 return rc;
23148}
23149
23150/*
23151** Return TRUE if the mask of type in eType matches no bits of the type of the
23152** allocation p. Also return true if p==NULL.
23153**
23154** This routine is designed for use within an assert() statement, to
23155** verify the type of an allocation. For example:
23156**
23157** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
23158*/
23159SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
23160 int rc = 1;
23161 if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
23162 struct MemBlockHdr *pHdr;
23163 pHdr = sqlite3MemsysGetHeader(p);
23164 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
23165 if( (pHdr->eType&eType)!=0 ){
23166 rc = 0;
23167 }
23168 }
23169 return rc;
23170}
23171
23172/*
23173** Set the number of backtrace levels kept for each allocation.
23174** A value of zero turns off backtracing. The number is always rounded
23175** up to a multiple of 2.
23176*/
23177SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
23178 if( depth<0 ){ depth = 0; }
23179 if( depth>20 ){ depth = 20; }
23180 depth = (depth+1)&0xfe;
23181 mem.nBacktrace = depth;
23182}
23183
23184SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
23185 mem.xBacktrace = xBacktrace;
23186}
23187
23188/*
23189** Set the title string for subsequent allocations.
23190*/
23191SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
23192 unsigned int n = sqlite3Strlen30(zTitle) + 1;
23193 sqlite3_mutex_enter(mem.mutex);
23194 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
23195 memcpy(mem.zTitle, zTitle, n);
23196 mem.zTitle[n] = 0;
23197 mem.nTitle = ROUND8(n);
23198 sqlite3_mutex_leave(mem.mutex);
23199}
23200
23201SQLITE_PRIVATE void sqlite3MemdebugSync(){
23202 struct MemBlockHdr *pHdr;
23203 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
23204 void **pBt = (void**)pHdr;
23205 pBt -= pHdr->nBacktraceSlots;
23206 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
23207 }
23208}
23209
23210/*
23211** Open the file indicated and write a log of all unfreed memory
23212** allocations into that log.
23213*/
23214SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
23215 FILE *out;
23216 struct MemBlockHdr *pHdr;
23217 void **pBt;
23218 int i;
23219 out = fopen(zFilename, "w");
23220 if( out==0 ){
23221 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
23222 zFilename);
23223 return;
23224 }
23225 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
23226 char *z = (char*)pHdr;
23227 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
23228 fprintf(out, "**** %lld bytes at %p from %s ****\n",
23229 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
23230 if( pHdr->nBacktrace ){
23231 fflush(out);
23232 pBt = (void**)pHdr;
23233 pBt -= pHdr->nBacktraceSlots;
23234 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
23235 fprintf(out, "\n");
23236 }
23237 }
23238 fprintf(out, "COUNTS:\n");
23239 for(i=0; i<NCSIZE-1; i++){
23240 if( mem.nAlloc[i] ){
23241 fprintf(out, " %5d: %10d %10d %10d\n",
23242 i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
23243 }
23244 }
23245 if( mem.nAlloc[NCSIZE-1] ){
23246 fprintf(out, " %5d: %10d %10d %10d\n",
23247 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
23248 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
23249 }
23250 fclose(out);
23251}
23252
23253/*
23254** Return the number of times sqlite3MemMalloc() has been called.
23255*/
23256SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
23257 int i;
23258 int nTotal = 0;
23259 for(i=0; i<NCSIZE; i++){
23260 nTotal += mem.nAlloc[i];
23261 }
23262 return nTotal;
23263}
23264
23265
23266#endif /* SQLITE_MEMDEBUG */
23267
23268/************** End of mem2.c ************************************************/
23269/************** Begin file mem3.c ********************************************/
23270/*
23271** 2007 October 14
23272**
23273** The author disclaims copyright to this source code. In place of
23274** a legal notice, here is a blessing:
23275**
23276** May you do good and not evil.
23277** May you find forgiveness for yourself and forgive others.
23278** May you share freely, never taking more than you give.
23279**
23280*************************************************************************
23281** This file contains the C functions that implement a memory
23282** allocation subsystem for use by SQLite.
23283**
23284** This version of the memory allocation subsystem omits all
23285** use of malloc(). The SQLite user supplies a block of memory
23286** before calling sqlite3_initialize() from which allocations
23287** are made and returned by the xMalloc() and xRealloc()
23288** implementations. Once sqlite3_initialize() has been called,
23289** the amount of memory available to SQLite is fixed and cannot
23290** be changed.
23291**
23292** This version of the memory allocation subsystem is included
23293** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
23294*/
23295/* #include "sqliteInt.h" */
23296
23297/*
23298** This version of the memory allocator is only built into the library
23299** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
23300** mean that the library will use a memory-pool by default, just that
23301** it is available. The mempool allocator is activated by calling
23302** sqlite3_config().
23303*/
23304#ifdef SQLITE_ENABLE_MEMSYS3
23305
23306/*
23307** Maximum size (in Mem3Blocks) of a "small" chunk.
23308*/
23309#define MX_SMALL 10
23310
23311
23312/*
23313** Number of freelist hash slots
23314*/
23315#define N_HASH 61
23316
23317/*
23318** A memory allocation (also called a "chunk") consists of two or
23319** more blocks where each block is 8 bytes. The first 8 bytes are
23320** a header that is not returned to the user.
23321**
23322** A chunk is two or more blocks that is either checked out or
23323** free. The first block has format u.hdr. u.hdr.size4x is 4 times the
23324** size of the allocation in blocks if the allocation is free.
23325** The u.hdr.size4x&1 bit is true if the chunk is checked out and
23326** false if the chunk is on the freelist. The u.hdr.size4x&2 bit
23327** is true if the previous chunk is checked out and false if the
23328** previous chunk is free. The u.hdr.prevSize field is the size of
23329** the previous chunk in blocks if the previous chunk is on the
23330** freelist. If the previous chunk is checked out, then
23331** u.hdr.prevSize can be part of the data for that chunk and should
23332** not be read or written.
23333**
23334** We often identify a chunk by its index in mem3.aPool[]. When
23335** this is done, the chunk index refers to the second block of
23336** the chunk. In this way, the first chunk has an index of 1.
23337** A chunk index of 0 means "no such chunk" and is the equivalent
23338** of a NULL pointer.
23339**
23340** The second block of free chunks is of the form u.list. The
23341** two fields form a double-linked list of chunks of related sizes.
23342** Pointers to the head of the list are stored in mem3.aiSmall[]
23343** for smaller chunks and mem3.aiHash[] for larger chunks.
23344**
23345** The second block of a chunk is user data if the chunk is checked
23346** out. If a chunk is checked out, the user data may extend into
23347** the u.hdr.prevSize value of the following chunk.
23348*/
23349typedef struct Mem3Block Mem3Block;
23350struct Mem3Block {
23351 union {
23352 struct {
23353 u32 prevSize; /* Size of previous chunk in Mem3Block elements */
23354 u32 size4x; /* 4x the size of current chunk in Mem3Block elements */
23355 } hdr;
23356 struct {
23357 u32 next; /* Index in mem3.aPool[] of next free chunk */
23358 u32 prev; /* Index in mem3.aPool[] of previous free chunk */
23359 } list;
23360 } u;
23361};
23362
23363/*
23364** All of the static variables used by this module are collected
23365** into a single structure named "mem3". This is to keep the
23366** static variables organized and to reduce namespace pollution
23367** when this module is combined with other in the amalgamation.
23368*/
23369static SQLITE_WSD struct Mem3Global {
23370 /*
23371 ** Memory available for allocation. nPool is the size of the array
23372 ** (in Mem3Blocks) pointed to by aPool less 2.
23373 */
23374 u32 nPool;
23375 Mem3Block *aPool;
23376
23377 /*
23378 ** True if we are evaluating an out-of-memory callback.
23379 */
23380 int alarmBusy;
23381
23382 /*
23383 ** Mutex to control access to the memory allocation subsystem.
23384 */
23385 sqlite3_mutex *mutex;
23386
23387 /*
23388 ** The minimum amount of free space that we have seen.
23389 */
23390 u32 mnMaster;
23391
23392 /*
23393 ** iMaster is the index of the master chunk. Most new allocations
23394 ** occur off of this chunk. szMaster is the size (in Mem3Blocks)
23395 ** of the current master. iMaster is 0 if there is not master chunk.
23396 ** The master chunk is not in either the aiHash[] or aiSmall[].
23397 */
23398 u32 iMaster;
23399 u32 szMaster;
23400
23401 /*
23402 ** Array of lists of free blocks according to the block size
23403 ** for smaller chunks, or a hash on the block size for larger
23404 ** chunks.
23405 */
23406 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
23407 u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */
23408} mem3 = { 97535575 };
23409
23410#define mem3 GLOBAL(struct Mem3Global, mem3)
23411
23412/*
23413** Unlink the chunk at mem3.aPool[i] from list it is currently
23414** on. *pRoot is the list that i is a member of.
23415*/
23416static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
23417 u32 next = mem3.aPool[i].u.list.next;
23418 u32 prev = mem3.aPool[i].u.list.prev;
23419 assert( sqlite3_mutex_held(mem3.mutex) );
23420 if( prev==0 ){
23421 *pRoot = next;
23422 }else{
23423 mem3.aPool[prev].u.list.next = next;
23424 }
23425 if( next ){
23426 mem3.aPool[next].u.list.prev = prev;
23427 }
23428 mem3.aPool[i].u.list.next = 0;
23429 mem3.aPool[i].u.list.prev = 0;
23430}
23431
23432/*
23433** Unlink the chunk at index i from
23434** whatever list is currently a member of.
23435*/
23436static void memsys3Unlink(u32 i){
23437 u32 size, hash;
23438 assert( sqlite3_mutex_held(mem3.mutex) );
23439 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
23440 assert( i>=1 );
23441 size = mem3.aPool[i-1].u.hdr.size4x/4;
23442 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
23443 assert( size>=2 );
23444 if( size <= MX_SMALL ){
23445 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
23446 }else{
23447 hash = size % N_HASH;
23448 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
23449 }
23450}
23451
23452/*
23453** Link the chunk at mem3.aPool[i] so that is on the list rooted
23454** at *pRoot.
23455*/
23456static void memsys3LinkIntoList(u32 i, u32 *pRoot){
23457 assert( sqlite3_mutex_held(mem3.mutex) );
23458 mem3.aPool[i].u.list.next = *pRoot;
23459 mem3.aPool[i].u.list.prev = 0;
23460 if( *pRoot ){
23461 mem3.aPool[*pRoot].u.list.prev = i;
23462 }
23463 *pRoot = i;
23464}
23465
23466/*
23467** Link the chunk at index i into either the appropriate
23468** small chunk list, or into the large chunk hash table.
23469*/
23470static void memsys3Link(u32 i){
23471 u32 size, hash;
23472 assert( sqlite3_mutex_held(mem3.mutex) );
23473 assert( i>=1 );
23474 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
23475 size = mem3.aPool[i-1].u.hdr.size4x/4;
23476 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
23477 assert( size>=2 );
23478 if( size <= MX_SMALL ){
23479 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
23480 }else{
23481 hash = size % N_HASH;
23482 memsys3LinkIntoList(i, &mem3.aiHash[hash]);
23483 }
23484}
23485
23486/*
23487** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
23488** will already be held (obtained by code in malloc.c) if
23489** sqlite3GlobalConfig.bMemStat is true.
23490*/
23491static void memsys3Enter(void){
23492 if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
23493 mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
23494 }
23495 sqlite3_mutex_enter(mem3.mutex);
23496}
23497static void memsys3Leave(void){
23498 sqlite3_mutex_leave(mem3.mutex);
23499}
23500
23501/*
23502** Called when we are unable to satisfy an allocation of nBytes.
23503*/
23504static void memsys3OutOfMemory(int nByte){
23505 if( !mem3.alarmBusy ){
23506 mem3.alarmBusy = 1;
23507 assert( sqlite3_mutex_held(mem3.mutex) );
23508 sqlite3_mutex_leave(mem3.mutex);
23509 sqlite3_release_memory(nByte);
23510 sqlite3_mutex_enter(mem3.mutex);
23511 mem3.alarmBusy = 0;
23512 }
23513}
23514
23515
23516/*
23517** Chunk i is a free chunk that has been unlinked. Adjust its
23518** size parameters for check-out and return a pointer to the
23519** user portion of the chunk.
23520*/
23521static void *memsys3Checkout(u32 i, u32 nBlock){
23522 u32 x;
23523 assert( sqlite3_mutex_held(mem3.mutex) );
23524 assert( i>=1 );
23525 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
23526 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
23527 x = mem3.aPool[i-1].u.hdr.size4x;
23528 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
23529 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
23530 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
23531 return &mem3.aPool[i];
23532}
23533
23534/*
23535** Carve a piece off of the end of the mem3.iMaster free chunk.
23536** Return a pointer to the new allocation. Or, if the master chunk
23537** is not large enough, return 0.
23538*/
23539static void *memsys3FromMaster(u32 nBlock){
23540 assert( sqlite3_mutex_held(mem3.mutex) );
23541 assert( mem3.szMaster>=nBlock );
23542 if( nBlock>=mem3.szMaster-1 ){
23543 /* Use the entire master */
23544 void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
23545 mem3.iMaster = 0;
23546 mem3.szMaster = 0;
23547 mem3.mnMaster = 0;
23548 return p;
23549 }else{
23550 /* Split the master block. Return the tail. */
23551 u32 newi, x;
23552 newi = mem3.iMaster + mem3.szMaster - nBlock;
23553 assert( newi > mem3.iMaster+1 );
23554 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
23555 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
23556 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
23557 mem3.szMaster -= nBlock;
23558 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
23559 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
23560 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
23561 if( mem3.szMaster < mem3.mnMaster ){
23562 mem3.mnMaster = mem3.szMaster;
23563 }
23564 return (void*)&mem3.aPool[newi];
23565 }
23566}
23567
23568/*
23569** *pRoot is the head of a list of free chunks of the same size
23570** or same size hash. In other words, *pRoot is an entry in either
23571** mem3.aiSmall[] or mem3.aiHash[].
23572**
23573** This routine examines all entries on the given list and tries
23574** to coalesce each entries with adjacent free chunks.
23575**
23576** If it sees a chunk that is larger than mem3.iMaster, it replaces
23577** the current mem3.iMaster with the new larger chunk. In order for
23578** this mem3.iMaster replacement to work, the master chunk must be
23579** linked into the hash tables. That is not the normal state of
23580** affairs, of course. The calling routine must link the master
23581** chunk before invoking this routine, then must unlink the (possibly
23582** changed) master chunk once this routine has finished.
23583*/
23584static void memsys3Merge(u32 *pRoot){
23585 u32 iNext, prev, size, i, x;
23586
23587 assert( sqlite3_mutex_held(mem3.mutex) );
23588 for(i=*pRoot; i>0; i=iNext){
23589 iNext = mem3.aPool[i].u.list.next;
23590 size = mem3.aPool[i-1].u.hdr.size4x;
23591 assert( (size&1)==0 );
23592 if( (size&2)==0 ){
23593 memsys3UnlinkFromList(i, pRoot);
23594 assert( i > mem3.aPool[i-1].u.hdr.prevSize );
23595 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
23596 if( prev==iNext ){
23597 iNext = mem3.aPool[prev].u.list.next;
23598 }
23599 memsys3Unlink(prev);
23600 size = i + size/4 - prev;
23601 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
23602 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
23603 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
23604 memsys3Link(prev);
23605 i = prev;
23606 }else{
23607 size /= 4;
23608 }
23609 if( size>mem3.szMaster ){
23610 mem3.iMaster = i;
23611 mem3.szMaster = size;
23612 }
23613 }
23614}
23615
23616/*
23617** Return a block of memory of at least nBytes in size.
23618** Return NULL if unable.
23619**
23620** This function assumes that the necessary mutexes, if any, are
23621** already held by the caller. Hence "Unsafe".
23622*/
23623static void *memsys3MallocUnsafe(int nByte){
23624 u32 i;
23625 u32 nBlock;
23626 u32 toFree;
23627
23628 assert( sqlite3_mutex_held(mem3.mutex) );
23629 assert( sizeof(Mem3Block)==8 );
23630 if( nByte<=12 ){
23631 nBlock = 2;
23632 }else{
23633 nBlock = (nByte + 11)/8;
23634 }
23635 assert( nBlock>=2 );
23636
23637 /* STEP 1:
23638 ** Look for an entry of the correct size in either the small
23639 ** chunk table or in the large chunk hash table. This is
23640 ** successful most of the time (about 9 times out of 10).
23641 */
23642 if( nBlock <= MX_SMALL ){
23643 i = mem3.aiSmall[nBlock-2];
23644 if( i>0 ){
23645 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
23646 return memsys3Checkout(i, nBlock);
23647 }
23648 }else{
23649 int hash = nBlock % N_HASH;
23650 for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
23651 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
23652 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
23653 return memsys3Checkout(i, nBlock);
23654 }
23655 }
23656 }
23657
23658 /* STEP 2:
23659 ** Try to satisfy the allocation by carving a piece off of the end
23660 ** of the master chunk. This step usually works if step 1 fails.
23661 */
23662 if( mem3.szMaster>=nBlock ){
23663 return memsys3FromMaster(nBlock);
23664 }
23665
23666
23667 /* STEP 3:
23668 ** Loop through the entire memory pool. Coalesce adjacent free
23669 ** chunks. Recompute the master chunk as the largest free chunk.
23670 ** Then try again to satisfy the allocation by carving a piece off
23671 ** of the end of the master chunk. This step happens very
23672 ** rarely (we hope!)
23673 */
23674 for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
23675 memsys3OutOfMemory(toFree);
23676 if( mem3.iMaster ){
23677 memsys3Link(mem3.iMaster);
23678 mem3.iMaster = 0;
23679 mem3.szMaster = 0;
23680 }
23681 for(i=0; i<N_HASH; i++){
23682 memsys3Merge(&mem3.aiHash[i]);
23683 }
23684 for(i=0; i<MX_SMALL-1; i++){
23685 memsys3Merge(&mem3.aiSmall[i]);
23686 }
23687 if( mem3.szMaster ){
23688 memsys3Unlink(mem3.iMaster);
23689 if( mem3.szMaster>=nBlock ){
23690 return memsys3FromMaster(nBlock);
23691 }
23692 }
23693 }
23694
23695 /* If none of the above worked, then we fail. */
23696 return 0;
23697}
23698
23699/*
23700** Free an outstanding memory allocation.
23701**
23702** This function assumes that the necessary mutexes, if any, are
23703** already held by the caller. Hence "Unsafe".
23704*/
23705static void memsys3FreeUnsafe(void *pOld){
23706 Mem3Block *p = (Mem3Block*)pOld;
23707 int i;
23708 u32 size, x;
23709 assert( sqlite3_mutex_held(mem3.mutex) );
23710 assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
23711 i = p - mem3.aPool;
23712 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
23713 size = mem3.aPool[i-1].u.hdr.size4x/4;
23714 assert( i+size<=mem3.nPool+1 );
23715 mem3.aPool[i-1].u.hdr.size4x &= ~1;
23716 mem3.aPool[i+size-1].u.hdr.prevSize = size;
23717 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
23718 memsys3Link(i);
23719
23720 /* Try to expand the master using the newly freed chunk */
23721 if( mem3.iMaster ){
23722 while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
23723 size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
23724 mem3.iMaster -= size;
23725 mem3.szMaster += size;
23726 memsys3Unlink(mem3.iMaster);
23727 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
23728 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
23729 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
23730 }
23731 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
23732 while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
23733 memsys3Unlink(mem3.iMaster+mem3.szMaster);
23734 mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
23735 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
23736 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
23737 }
23738 }
23739}
23740
23741/*
23742** Return the size of an outstanding allocation, in bytes. The
23743** size returned omits the 8-byte header overhead. This only
23744** works for chunks that are currently checked out.
23745*/
23746static int memsys3Size(void *p){
23747 Mem3Block *pBlock;
23748 assert( p!=0 );
23749 pBlock = (Mem3Block*)p;
23750 assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
23751 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
23752}
23753
23754/*
23755** Round up a request size to the next valid allocation size.
23756*/
23757static int memsys3Roundup(int n){
23758 if( n<=12 ){
23759 return 12;
23760 }else{
23761 return ((n+11)&~7) - 4;
23762 }
23763}
23764
23765/*
23766** Allocate nBytes of memory.
23767*/
23768static void *memsys3Malloc(int nBytes){
23769 sqlite3_int64 *p;
23770 assert( nBytes>0 ); /* malloc.c filters out 0 byte requests */
23771 memsys3Enter();
23772 p = memsys3MallocUnsafe(nBytes);
23773 memsys3Leave();
23774 return (void*)p;
23775}
23776
23777/*
23778** Free memory.
23779*/
23780static void memsys3Free(void *pPrior){
23781 assert( pPrior );
23782 memsys3Enter();
23783 memsys3FreeUnsafe(pPrior);
23784 memsys3Leave();
23785}
23786
23787/*
23788** Change the size of an existing memory allocation
23789*/
23790static void *memsys3Realloc(void *pPrior, int nBytes){
23791 int nOld;
23792 void *p;
23793 if( pPrior==0 ){
23794 return sqlite3_malloc(nBytes);
23795 }
23796 if( nBytes<=0 ){
23797 sqlite3_free(pPrior);
23798 return 0;
23799 }
23800 nOld = memsys3Size(pPrior);
23801 if( nBytes<=nOld && nBytes>=nOld-128 ){
23802 return pPrior;
23803 }
23804 memsys3Enter();
23805 p = memsys3MallocUnsafe(nBytes);
23806 if( p ){
23807 if( nOld<nBytes ){
23808 memcpy(p, pPrior, nOld);
23809 }else{
23810 memcpy(p, pPrior, nBytes);
23811 }
23812 memsys3FreeUnsafe(pPrior);
23813 }
23814 memsys3Leave();
23815 return p;
23816}
23817
23818/*
23819** Initialize this module.
23820*/
23821static int memsys3Init(void *NotUsed){
23822 UNUSED_PARAMETER(NotUsed);
23823 if( !sqlite3GlobalConfig.pHeap ){
23824 return SQLITE_ERROR;
23825 }
23826
23827 /* Store a pointer to the memory block in global structure mem3. */
23828 assert( sizeof(Mem3Block)==8 );
23829 mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
23830 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
23831
23832 /* Initialize the master block. */
23833 mem3.szMaster = mem3.nPool;
23834 mem3.mnMaster = mem3.szMaster;
23835 mem3.iMaster = 1;
23836 mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
23837 mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
23838 mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
23839
23840 return SQLITE_OK;
23841}
23842
23843/*
23844** Deinitialize this module.
23845*/
23846static void memsys3Shutdown(void *NotUsed){
23847 UNUSED_PARAMETER(NotUsed);
23848 mem3.mutex = 0;
23849 return;
23850}
23851
23852
23853
23854/*
23855** Open the file indicated and write a log of all unfreed memory
23856** allocations into that log.
23857*/
23858SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
23859#ifdef SQLITE_DEBUG
23860 FILE *out;
23861 u32 i, j;
23862 u32 size;
23863 if( zFilename==0 || zFilename[0]==0 ){
23864 out = stdout;
23865 }else{
23866 out = fopen(zFilename, "w");
23867 if( out==0 ){
23868 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
23869 zFilename);
23870 return;
23871 }
23872 }
23873 memsys3Enter();
23874 fprintf(out, "CHUNKS:\n");
23875 for(i=1; i<=mem3.nPool; i+=size/4){
23876 size = mem3.aPool[i-1].u.hdr.size4x;
23877 if( size/4<=1 ){
23878 fprintf(out, "%p size error\n", &mem3.aPool[i]);
23879 assert( 0 );
23880 break;
23881 }
23882 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
23883 fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
23884 assert( 0 );
23885 break;
23886 }
23887 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
23888 fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
23889 assert( 0 );
23890 break;
23891 }
23892 if( size&1 ){
23893 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
23894 }else{
23895 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
23896 i==mem3.iMaster ? " **master**" : "");
23897 }
23898 }
23899 for(i=0; i<MX_SMALL-1; i++){
23900 if( mem3.aiSmall[i]==0 ) continue;
23901 fprintf(out, "small(%2d):", i);
23902 for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
23903 fprintf(out, " %p(%d)", &mem3.aPool[j],
23904 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
23905 }
23906 fprintf(out, "\n");
23907 }
23908 for(i=0; i<N_HASH; i++){
23909 if( mem3.aiHash[i]==0 ) continue;
23910 fprintf(out, "hash(%2d):", i);
23911 for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
23912 fprintf(out, " %p(%d)", &mem3.aPool[j],
23913 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
23914 }
23915 fprintf(out, "\n");
23916 }
23917 fprintf(out, "master=%d\n", mem3.iMaster);
23918 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
23919 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
23920 sqlite3_mutex_leave(mem3.mutex);
23921 if( out==stdout ){
23922 fflush(stdout);
23923 }else{
23924 fclose(out);
23925 }
23926#else
23927 UNUSED_PARAMETER(zFilename);
23928#endif
23929}
23930
23931/*
23932** This routine is the only routine in this file with external
23933** linkage.
23934**
23935** Populate the low-level memory allocation function pointers in
23936** sqlite3GlobalConfig.m with pointers to the routines in this file. The
23937** arguments specify the block of memory to manage.
23938**
23939** This routine is only called by sqlite3_config(), and therefore
23940** is not required to be threadsafe (it is not).
23941*/
23942SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
23943 static const sqlite3_mem_methods mempoolMethods = {
23944 memsys3Malloc,
23945 memsys3Free,
23946 memsys3Realloc,
23947 memsys3Size,
23948 memsys3Roundup,
23949 memsys3Init,
23950 memsys3Shutdown,
23951 0
23952 };
23953 return &mempoolMethods;
23954}
23955
23956#endif /* SQLITE_ENABLE_MEMSYS3 */
23957
23958/************** End of mem3.c ************************************************/
23959/************** Begin file mem5.c ********************************************/
23960/*
23961** 2007 October 14
23962**
23963** The author disclaims copyright to this source code. In place of
23964** a legal notice, here is a blessing:
23965**
23966** May you do good and not evil.
23967** May you find forgiveness for yourself and forgive others.
23968** May you share freely, never taking more than you give.
23969**
23970*************************************************************************
23971** This file contains the C functions that implement a memory
23972** allocation subsystem for use by SQLite.
23973**
23974** This version of the memory allocation subsystem omits all
23975** use of malloc(). The application gives SQLite a block of memory
23976** before calling sqlite3_initialize() from which allocations
23977** are made and returned by the xMalloc() and xRealloc()
23978** implementations. Once sqlite3_initialize() has been called,
23979** the amount of memory available to SQLite is fixed and cannot
23980** be changed.
23981**
23982** This version of the memory allocation subsystem is included
23983** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
23984**
23985** This memory allocator uses the following algorithm:
23986**
23987** 1. All memory allocation sizes are rounded up to a power of 2.
23988**
23989** 2. If two adjacent free blocks are the halves of a larger block,
23990** then the two blocks are coalesced into the single larger block.
23991**
23992** 3. New memory is allocated from the first available free block.
23993**
23994** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
23995** Concerning Dynamic Storage Allocation". Journal of the Association for
23996** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
23997**
23998** Let n be the size of the largest allocation divided by the minimum
23999** allocation size (after rounding all sizes up to a power of 2.) Let M
24000** be the maximum amount of memory ever outstanding at one time. Let
24001** N be the total amount of memory available for allocation. Robson
24002** proved that this memory allocator will never breakdown due to
24003** fragmentation as long as the following constraint holds:
24004**
24005** N >= M*(1 + log2(n)/2) - n + 1
24006**
24007** The sqlite3_status() logic tracks the maximum values of n and M so
24008** that an application can, at any time, verify this constraint.
24009*/
24010/* #include "sqliteInt.h" */
24011
24012/*
24013** This version of the memory allocator is used only when
24014** SQLITE_ENABLE_MEMSYS5 is defined.
24015*/
24016#ifdef SQLITE_ENABLE_MEMSYS5
24017
24018/*
24019** A minimum allocation is an instance of the following structure.
24020** Larger allocations are an array of these structures where the
24021** size of the array is a power of 2.
24022**
24023** The size of this object must be a power of two. That fact is
24024** verified in memsys5Init().
24025*/
24026typedef struct Mem5Link Mem5Link;
24027struct Mem5Link {
24028 int next; /* Index of next free chunk */
24029 int prev; /* Index of previous free chunk */
24030};
24031
24032/*
24033** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
24034** mem5.szAtom is always at least 8 and 32-bit integers are used,
24035** it is not actually possible to reach this limit.
24036*/
24037#define LOGMAX 30
24038
24039/*
24040** Masks used for mem5.aCtrl[] elements.
24041*/
24042#define CTRL_LOGSIZE 0x1f /* Log2 Size of this block */
24043#define CTRL_FREE 0x20 /* True if not checked out */
24044
24045/*
24046** All of the static variables used by this module are collected
24047** into a single structure named "mem5". This is to keep the
24048** static variables organized and to reduce namespace pollution
24049** when this module is combined with other in the amalgamation.
24050*/
24051static SQLITE_WSD struct Mem5Global {
24052 /*
24053 ** Memory available for allocation
24054 */
24055 int szAtom; /* Smallest possible allocation in bytes */
24056 int nBlock; /* Number of szAtom sized blocks in zPool */
24057 u8 *zPool; /* Memory available to be allocated */
24058
24059 /*
24060 ** Mutex to control access to the memory allocation subsystem.
24061 */
24062 sqlite3_mutex *mutex;
24063
24064#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
24065 /*
24066 ** Performance statistics
24067 */
24068 u64 nAlloc; /* Total number of calls to malloc */
24069 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
24070 u64 totalExcess; /* Total internal fragmentation */
24071 u32 currentOut; /* Current checkout, including internal fragmentation */
24072 u32 currentCount; /* Current number of distinct checkouts */
24073 u32 maxOut; /* Maximum instantaneous currentOut */
24074 u32 maxCount; /* Maximum instantaneous currentCount */
24075 u32 maxRequest; /* Largest allocation (exclusive of internal frag) */
24076#endif
24077
24078 /*
24079 ** Lists of free blocks. aiFreelist[0] is a list of free blocks of
24080 ** size mem5.szAtom. aiFreelist[1] holds blocks of size szAtom*2.
24081 ** aiFreelist[2] holds free blocks of size szAtom*4. And so forth.
24082 */
24083 int aiFreelist[LOGMAX+1];
24084
24085 /*
24086 ** Space for tracking which blocks are checked out and the size
24087 ** of each block. One byte per block.
24088 */
24089 u8 *aCtrl;
24090
24091} mem5;
24092
24093/*
24094** Access the static variable through a macro for SQLITE_OMIT_WSD.
24095*/
24096#define mem5 GLOBAL(struct Mem5Global, mem5)
24097
24098/*
24099** Assuming mem5.zPool is divided up into an array of Mem5Link
24100** structures, return a pointer to the idx-th such link.
24101*/
24102#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
24103
24104/*
24105** Unlink the chunk at mem5.aPool[i] from list it is currently
24106** on. It should be found on mem5.aiFreelist[iLogsize].
24107*/
24108static void memsys5Unlink(int i, int iLogsize){
24109 int next, prev;
24110 assert( i>=0 && i<mem5.nBlock );
24111 assert( iLogsize>=0 && iLogsize<=LOGMAX );
24112 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
24113
24114 next = MEM5LINK(i)->next;
24115 prev = MEM5LINK(i)->prev;
24116 if( prev<0 ){
24117 mem5.aiFreelist[iLogsize] = next;
24118 }else{
24119 MEM5LINK(prev)->next = next;
24120 }
24121 if( next>=0 ){
24122 MEM5LINK(next)->prev = prev;
24123 }
24124}
24125
24126/*
24127** Link the chunk at mem5.aPool[i] so that is on the iLogsize
24128** free list.
24129*/
24130static void memsys5Link(int i, int iLogsize){
24131 int x;
24132 assert( sqlite3_mutex_held(mem5.mutex) );
24133 assert( i>=0 && i<mem5.nBlock );
24134 assert( iLogsize>=0 && iLogsize<=LOGMAX );
24135 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
24136
24137 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
24138 MEM5LINK(i)->prev = -1;
24139 if( x>=0 ){
24140 assert( x<mem5.nBlock );
24141 MEM5LINK(x)->prev = i;
24142 }
24143 mem5.aiFreelist[iLogsize] = i;
24144}
24145
24146/*
24147** Obtain or release the mutex needed to access global data structures.
24148*/
24149static void memsys5Enter(void){
24150 sqlite3_mutex_enter(mem5.mutex);
24151}
24152static void memsys5Leave(void){
24153 sqlite3_mutex_leave(mem5.mutex);
24154}
24155
24156/*
24157** Return the size of an outstanding allocation, in bytes.
24158** This only works for chunks that are currently checked out.
24159*/
24160static int memsys5Size(void *p){
24161 int iSize, i;
24162 assert( p!=0 );
24163 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
24164 assert( i>=0 && i<mem5.nBlock );
24165 iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
24166 return iSize;
24167}
24168
24169/*
24170** Return a block of memory of at least nBytes in size.
24171** Return NULL if unable. Return NULL if nBytes==0.
24172**
24173** The caller guarantees that nByte is positive.
24174**
24175** The caller has obtained a mutex prior to invoking this
24176** routine so there is never any chance that two or more
24177** threads can be in this routine at the same time.
24178*/
24179static void *memsys5MallocUnsafe(int nByte){
24180 int i; /* Index of a mem5.aPool[] slot */
24181 int iBin; /* Index into mem5.aiFreelist[] */
24182 int iFullSz; /* Size of allocation rounded up to power of 2 */
24183 int iLogsize; /* Log2 of iFullSz/POW2_MIN */
24184
24185 /* nByte must be a positive */
24186 assert( nByte>0 );
24187
24188 /* No more than 1GiB per allocation */
24189 if( nByte > 0x40000000 ) return 0;
24190
24191#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
24192 /* Keep track of the maximum allocation request. Even unfulfilled
24193 ** requests are counted */
24194 if( (u32)nByte>mem5.maxRequest ){
24195 mem5.maxRequest = nByte;
24196 }
24197#endif
24198
24199
24200 /* Round nByte up to the next valid power of two */
24201 for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
24202
24203 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
24204 ** block. If not, then split a block of the next larger power of
24205 ** two in order to create a new free block of size iLogsize.
24206 */
24207 for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
24208 if( iBin>LOGMAX ){
24209 testcase( sqlite3GlobalConfig.xLog!=0 );
24210 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
24211 return 0;
24212 }
24213 i = mem5.aiFreelist[iBin];
24214 memsys5Unlink(i, iBin);
24215 while( iBin>iLogsize ){
24216 int newSize;
24217
24218 iBin--;
24219 newSize = 1 << iBin;
24220 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
24221 memsys5Link(i+newSize, iBin);
24222 }
24223 mem5.aCtrl[i] = iLogsize;
24224
24225#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
24226 /* Update allocator performance statistics. */
24227 mem5.nAlloc++;
24228 mem5.totalAlloc += iFullSz;
24229 mem5.totalExcess += iFullSz - nByte;
24230 mem5.currentCount++;
24231 mem5.currentOut += iFullSz;
24232 if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
24233 if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
24234#endif
24235
24236#ifdef SQLITE_DEBUG
24237 /* Make sure the allocated memory does not assume that it is set to zero
24238 ** or retains a value from a previous allocation */
24239 memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
24240#endif
24241
24242 /* Return a pointer to the allocated memory. */
24243 return (void*)&mem5.zPool[i*mem5.szAtom];
24244}
24245
24246/*
24247** Free an outstanding memory allocation.
24248*/
24249static void memsys5FreeUnsafe(void *pOld){
24250 u32 size, iLogsize;
24251 int iBlock;
24252
24253 /* Set iBlock to the index of the block pointed to by pOld in
24254 ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
24255 */
24256 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
24257
24258 /* Check that the pointer pOld points to a valid, non-free block. */
24259 assert( iBlock>=0 && iBlock<mem5.nBlock );
24260 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
24261 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
24262
24263 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
24264 size = 1<<iLogsize;
24265 assert( iBlock+size-1<(u32)mem5.nBlock );
24266
24267 mem5.aCtrl[iBlock] |= CTRL_FREE;
24268 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
24269
24270#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
24271 assert( mem5.currentCount>0 );
24272 assert( mem5.currentOut>=(size*mem5.szAtom) );
24273 mem5.currentCount--;
24274 mem5.currentOut -= size*mem5.szAtom;
24275 assert( mem5.currentOut>0 || mem5.currentCount==0 );
24276 assert( mem5.currentCount>0 || mem5.currentOut==0 );
24277#endif
24278
24279 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
24280 while( ALWAYS(iLogsize<LOGMAX) ){
24281 int iBuddy;
24282 if( (iBlock>>iLogsize) & 1 ){
24283 iBuddy = iBlock - size;
24284 assert( iBuddy>=0 );
24285 }else{
24286 iBuddy = iBlock + size;
24287 if( iBuddy>=mem5.nBlock ) break;
24288 }
24289 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
24290 memsys5Unlink(iBuddy, iLogsize);
24291 iLogsize++;
24292 if( iBuddy<iBlock ){
24293 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
24294 mem5.aCtrl[iBlock] = 0;
24295 iBlock = iBuddy;
24296 }else{
24297 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
24298 mem5.aCtrl[iBuddy] = 0;
24299 }
24300 size *= 2;
24301 }
24302
24303#ifdef SQLITE_DEBUG
24304 /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
24305 ** not used after being freed */
24306 memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
24307#endif
24308
24309 memsys5Link(iBlock, iLogsize);
24310}
24311
24312/*
24313** Allocate nBytes of memory.
24314*/
24315static void *memsys5Malloc(int nBytes){
24316 sqlite3_int64 *p = 0;
24317 if( nBytes>0 ){
24318 memsys5Enter();
24319 p = memsys5MallocUnsafe(nBytes);
24320 memsys5Leave();
24321 }
24322 return (void*)p;
24323}
24324
24325/*
24326** Free memory.
24327**
24328** The outer layer memory allocator prevents this routine from
24329** being called with pPrior==0.
24330*/
24331static void memsys5Free(void *pPrior){
24332 assert( pPrior!=0 );
24333 memsys5Enter();
24334 memsys5FreeUnsafe(pPrior);
24335 memsys5Leave();
24336}
24337
24338/*
24339** Change the size of an existing memory allocation.
24340**
24341** The outer layer memory allocator prevents this routine from
24342** being called with pPrior==0.
24343**
24344** nBytes is always a value obtained from a prior call to
24345** memsys5Round(). Hence nBytes is always a non-negative power
24346** of two. If nBytes==0 that means that an oversize allocation
24347** (an allocation larger than 0x40000000) was requested and this
24348** routine should return 0 without freeing pPrior.
24349*/
24350static void *memsys5Realloc(void *pPrior, int nBytes){
24351 int nOld;
24352 void *p;
24353 assert( pPrior!=0 );
24354 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */
24355 assert( nBytes>=0 );
24356 if( nBytes==0 ){
24357 return 0;
24358 }
24359 nOld = memsys5Size(pPrior);
24360 if( nBytes<=nOld ){
24361 return pPrior;
24362 }
24363 p = memsys5Malloc(nBytes);
24364 if( p ){
24365 memcpy(p, pPrior, nOld);
24366 memsys5Free(pPrior);
24367 }
24368 return p;
24369}
24370
24371/*
24372** Round up a request size to the next valid allocation size. If
24373** the allocation is too large to be handled by this allocation system,
24374** return 0.
24375**
24376** All allocations must be a power of two and must be expressed by a
24377** 32-bit signed integer. Hence the largest allocation is 0x40000000
24378** or 1073741824 bytes.
24379*/
24380static int memsys5Roundup(int n){
24381 int iFullSz;
24382 if( n > 0x40000000 ) return 0;
24383 for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
24384 return iFullSz;
24385}
24386
24387/*
24388** Return the ceiling of the logarithm base 2 of iValue.
24389**
24390** Examples: memsys5Log(1) -> 0
24391** memsys5Log(2) -> 1
24392** memsys5Log(4) -> 2
24393** memsys5Log(5) -> 3
24394** memsys5Log(8) -> 3
24395** memsys5Log(9) -> 4
24396*/
24397static int memsys5Log(int iValue){
24398 int iLog;
24399 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
24400 return iLog;
24401}
24402
24403/*
24404** Initialize the memory allocator.
24405**
24406** This routine is not threadsafe. The caller must be holding a mutex
24407** to prevent multiple threads from entering at the same time.
24408*/
24409static int memsys5Init(void *NotUsed){
24410 int ii; /* Loop counter */
24411 int nByte; /* Number of bytes of memory available to this allocator */
24412 u8 *zByte; /* Memory usable by this allocator */
24413 int nMinLog; /* Log base 2 of minimum allocation size in bytes */
24414 int iOffset; /* An offset into mem5.aCtrl[] */
24415
24416 UNUSED_PARAMETER(NotUsed);
24417
24418 /* For the purposes of this routine, disable the mutex */
24419 mem5.mutex = 0;
24420
24421 /* The size of a Mem5Link object must be a power of two. Verify that
24422 ** this is case.
24423 */
24424 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
24425
24426 nByte = sqlite3GlobalConfig.nHeap;
24427 zByte = (u8*)sqlite3GlobalConfig.pHeap;
24428 assert( zByte!=0 ); /* sqlite3_config() does not allow otherwise */
24429
24430 /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
24431 nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
24432 mem5.szAtom = (1<<nMinLog);
24433 while( (int)sizeof(Mem5Link)>mem5.szAtom ){
24434 mem5.szAtom = mem5.szAtom << 1;
24435 }
24436
24437 mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
24438 mem5.zPool = zByte;
24439 mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
24440
24441 for(ii=0; ii<=LOGMAX; ii++){
24442 mem5.aiFreelist[ii] = -1;
24443 }
24444
24445 iOffset = 0;
24446 for(ii=LOGMAX; ii>=0; ii--){
24447 int nAlloc = (1<<ii);
24448 if( (iOffset+nAlloc)<=mem5.nBlock ){
24449 mem5.aCtrl[iOffset] = ii | CTRL_FREE;
24450 memsys5Link(iOffset, ii);
24451 iOffset += nAlloc;
24452 }
24453 assert((iOffset+nAlloc)>mem5.nBlock);
24454 }
24455
24456 /* If a mutex is required for normal operation, allocate one */
24457 if( sqlite3GlobalConfig.bMemstat==0 ){
24458 mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
24459 }
24460
24461 return SQLITE_OK;
24462}
24463
24464/*
24465** Deinitialize this module.
24466*/
24467static void memsys5Shutdown(void *NotUsed){
24468 UNUSED_PARAMETER(NotUsed);
24469 mem5.mutex = 0;
24470 return;
24471}
24472
24473#ifdef SQLITE_TEST
24474/*
24475** Open the file indicated and write a log of all unfreed memory
24476** allocations into that log.
24477*/
24478SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
24479 FILE *out;
24480 int i, j, n;
24481 int nMinLog;
24482
24483 if( zFilename==0 || zFilename[0]==0 ){
24484 out = stdout;
24485 }else{
24486 out = fopen(zFilename, "w");
24487 if( out==0 ){
24488 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
24489 zFilename);
24490 return;
24491 }
24492 }
24493 memsys5Enter();
24494 nMinLog = memsys5Log(mem5.szAtom);
24495 for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
24496 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
24497 fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
24498 }
24499 fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc);
24500 fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc);
24501 fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess);
24502 fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut);
24503 fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
24504 fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut);
24505 fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount);
24506 fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest);
24507 memsys5Leave();
24508 if( out==stdout ){
24509 fflush(stdout);
24510 }else{
24511 fclose(out);
24512 }
24513}
24514#endif
24515
24516/*
24517** This routine is the only routine in this file with external
24518** linkage. It returns a pointer to a static sqlite3_mem_methods
24519** struct populated with the memsys5 methods.
24520*/
24521SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
24522 static const sqlite3_mem_methods memsys5Methods = {
24523 memsys5Malloc,
24524 memsys5Free,
24525 memsys5Realloc,
24526 memsys5Size,
24527 memsys5Roundup,
24528 memsys5Init,
24529 memsys5Shutdown,
24530 0
24531 };
24532 return &memsys5Methods;
24533}
24534
24535#endif /* SQLITE_ENABLE_MEMSYS5 */
24536
24537/************** End of mem5.c ************************************************/
24538/************** Begin file mutex.c *******************************************/
24539/*
24540** 2007 August 14
24541**
24542** The author disclaims copyright to this source code. In place of
24543** a legal notice, here is a blessing:
24544**
24545** May you do good and not evil.
24546** May you find forgiveness for yourself and forgive others.
24547** May you share freely, never taking more than you give.
24548**
24549*************************************************************************
24550** This file contains the C functions that implement mutexes.
24551**
24552** This file contains code that is common across all mutex implementations.
24553*/
24554/* #include "sqliteInt.h" */
24555
24556#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
24557/*
24558** For debugging purposes, record when the mutex subsystem is initialized
24559** and uninitialized so that we can assert() if there is an attempt to
24560** allocate a mutex while the system is uninitialized.
24561*/
24562static SQLITE_WSD int mutexIsInit = 0;
24563#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
24564
24565
24566#ifndef SQLITE_MUTEX_OMIT
24567
24568#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
24569/*
24570** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains
24571** the implementation of a wrapper around the system default mutex
24572** implementation (sqlite3DefaultMutex()).
24573**
24574** Most calls are passed directly through to the underlying default
24575** mutex implementation. Except, if a mutex is configured by calling
24576** sqlite3MutexWarnOnContention() on it, then if contention is ever
24577** encountered within xMutexEnter() a warning is emitted via sqlite3_log().
24578**
24579** This type of mutex is used as the database handle mutex when testing
24580** apps that usually use SQLITE_CONFIG_MULTITHREAD mode.
24581*/
24582
24583/*
24584** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS
24585** is defined. Variable CheckMutex.mutex is a pointer to the real mutex
24586** allocated by the system mutex implementation. Variable iType is usually set
24587** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
24588** or one of the static mutex identifiers. Or, if this is a recursive mutex
24589** that has been configured using sqlite3MutexWarnOnContention(), it is
24590** set to SQLITE_MUTEX_WARNONCONTENTION.
24591*/
24592typedef struct CheckMutex CheckMutex;
24593struct CheckMutex {
24594 int iType;
24595 sqlite3_mutex *mutex;
24596};
24597
24598#define SQLITE_MUTEX_WARNONCONTENTION (-1)
24599
24600/*
24601** Pointer to real mutex methods object used by the CheckMutex
24602** implementation. Set by checkMutexInit().
24603*/
24604static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods;
24605
24606#ifdef SQLITE_DEBUG
24607static int checkMutexHeld(sqlite3_mutex *p){
24608 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
24609}
24610static int checkMutexNotheld(sqlite3_mutex *p){
24611 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
24612}
24613#endif
24614
24615/*
24616** Initialize and deinitialize the mutex subsystem.
24617*/
24618static int checkMutexInit(void){
24619 pGlobalMutexMethods = sqlite3DefaultMutex();
24620 return SQLITE_OK;
24621}
24622static int checkMutexEnd(void){
24623 pGlobalMutexMethods = 0;
24624 return SQLITE_OK;
24625}
24626
24627/*
24628** Allocate a mutex.
24629*/
24630static sqlite3_mutex *checkMutexAlloc(int iType){
24631 static CheckMutex staticMutexes[] = {
24632 {2, 0}, {3, 0}, {4, 0}, {5, 0},
24633 {6, 0}, {7, 0}, {8, 0}, {9, 0},
24634 {10, 0}, {11, 0}, {12, 0}, {13, 0}
24635 };
24636 CheckMutex *p = 0;
24637
24638 assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 );
24639 if( iType<2 ){
24640 p = sqlite3MallocZero(sizeof(CheckMutex));
24641 if( p==0 ) return 0;
24642 p->iType = iType;
24643 }else{
24644#ifdef SQLITE_ENABLE_API_ARMOR
24645 if( iType-2>=ArraySize(staticMutexes) ){
24646 (void)SQLITE_MISUSE_BKPT;
24647 return 0;
24648 }
24649#endif
24650 p = &staticMutexes[iType-2];
24651 }
24652
24653 if( p->mutex==0 ){
24654 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
24655 if( p->mutex==0 ){
24656 if( iType<2 ){
24657 sqlite3_free(p);
24658 }
24659 p = 0;
24660 }
24661 }
24662
24663 return (sqlite3_mutex*)p;
24664}
24665
24666/*
24667** Free a mutex.
24668*/
24669static void checkMutexFree(sqlite3_mutex *p){
24670 assert( SQLITE_MUTEX_RECURSIVE<2 );
24671 assert( SQLITE_MUTEX_FAST<2 );
24672 assert( SQLITE_MUTEX_WARNONCONTENTION<2 );
24673
24674#if SQLITE_ENABLE_API_ARMOR
24675 if( ((CheckMutex*)p)->iType<2 )
24676#endif
24677 {
24678 CheckMutex *pCheck = (CheckMutex*)p;
24679 pGlobalMutexMethods->xMutexFree(pCheck->mutex);
24680 sqlite3_free(pCheck);
24681 }
24682#ifdef SQLITE_ENABLE_API_ARMOR
24683 else{
24684 (void)SQLITE_MISUSE_BKPT;
24685 }
24686#endif
24687}
24688
24689/*
24690** Enter the mutex.
24691*/
24692static void checkMutexEnter(sqlite3_mutex *p){
24693 CheckMutex *pCheck = (CheckMutex*)p;
24694 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
24695 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
24696 return;
24697 }
24698 sqlite3_log(SQLITE_MISUSE,
24699 "illegal multi-threaded access to database connection"
24700 );
24701 }
24702 pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
24703}
24704
24705/*
24706** Enter the mutex (do not block).
24707*/
24708static int checkMutexTry(sqlite3_mutex *p){
24709 CheckMutex *pCheck = (CheckMutex*)p;
24710 return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
24711}
24712
24713/*
24714** Leave the mutex.
24715*/
24716static void checkMutexLeave(sqlite3_mutex *p){
24717 CheckMutex *pCheck = (CheckMutex*)p;
24718 pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
24719}
24720
24721sqlite3_mutex_methods const *multiThreadedCheckMutex(void){
24722 static const sqlite3_mutex_methods sMutex = {
24723 checkMutexInit,
24724 checkMutexEnd,
24725 checkMutexAlloc,
24726 checkMutexFree,
24727 checkMutexEnter,
24728 checkMutexTry,
24729 checkMutexLeave,
24730#ifdef SQLITE_DEBUG
24731 checkMutexHeld,
24732 checkMutexNotheld
24733#else
24734 0,
24735 0
24736#endif
24737 };
24738 return &sMutex;
24739}
24740
24741/*
24742** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as
24743** one on which there should be no contention.
24744*/
24745SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex *p){
24746 if( sqlite3GlobalConfig.mutex.xMutexAlloc==checkMutexAlloc ){
24747 CheckMutex *pCheck = (CheckMutex*)p;
24748 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE );
24749 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
24750 }
24751}
24752#endif /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */
24753
24754/*
24755** Initialize the mutex system.
24756*/
24757SQLITE_PRIVATE int sqlite3MutexInit(void){
24758 int rc = SQLITE_OK;
24759 if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
24760 /* If the xMutexAlloc method has not been set, then the user did not
24761 ** install a mutex implementation via sqlite3_config() prior to
24762 ** sqlite3_initialize() being called. This block copies pointers to
24763 ** the default implementation into the sqlite3GlobalConfig structure.
24764 */
24765 sqlite3_mutex_methods const *pFrom;
24766 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
24767
24768 if( sqlite3GlobalConfig.bCoreMutex ){
24769#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
24770 pFrom = multiThreadedCheckMutex();
24771#else
24772 pFrom = sqlite3DefaultMutex();
24773#endif
24774 }else{
24775 pFrom = sqlite3NoopMutex();
24776 }
24777 pTo->xMutexInit = pFrom->xMutexInit;
24778 pTo->xMutexEnd = pFrom->xMutexEnd;
24779 pTo->xMutexFree = pFrom->xMutexFree;
24780 pTo->xMutexEnter = pFrom->xMutexEnter;
24781 pTo->xMutexTry = pFrom->xMutexTry;
24782 pTo->xMutexLeave = pFrom->xMutexLeave;
24783 pTo->xMutexHeld = pFrom->xMutexHeld;
24784 pTo->xMutexNotheld = pFrom->xMutexNotheld;
24785 sqlite3MemoryBarrier();
24786 pTo->xMutexAlloc = pFrom->xMutexAlloc;
24787 }
24788 assert( sqlite3GlobalConfig.mutex.xMutexInit );
24789 rc = sqlite3GlobalConfig.mutex.xMutexInit();
24790
24791#ifdef SQLITE_DEBUG
24792 GLOBAL(int, mutexIsInit) = 1;
24793#endif
24794
24795 return rc;
24796}
24797
24798/*
24799** Shutdown the mutex system. This call frees resources allocated by
24800** sqlite3MutexInit().
24801*/
24802SQLITE_PRIVATE int sqlite3MutexEnd(void){
24803 int rc = SQLITE_OK;
24804 if( sqlite3GlobalConfig.mutex.xMutexEnd ){
24805 rc = sqlite3GlobalConfig.mutex.xMutexEnd();
24806 }
24807
24808#ifdef SQLITE_DEBUG
24809 GLOBAL(int, mutexIsInit) = 0;
24810#endif
24811
24812 return rc;
24813}
24814
24815/*
24816** Retrieve a pointer to a static mutex or allocate a new dynamic one.
24817*/
24818SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
24819#ifndef SQLITE_OMIT_AUTOINIT
24820 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
24821 if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
24822#endif
24823 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
24824 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
24825}
24826
24827SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
24828 if( !sqlite3GlobalConfig.bCoreMutex ){
24829 return 0;
24830 }
24831 assert( GLOBAL(int, mutexIsInit) );
24832 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
24833 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
24834}
24835
24836/*
24837** Free a dynamic mutex.
24838*/
24839SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
24840 if( p ){
24841 assert( sqlite3GlobalConfig.mutex.xMutexFree );
24842 sqlite3GlobalConfig.mutex.xMutexFree(p);
24843 }
24844}
24845
24846/*
24847** Obtain the mutex p. If some other thread already has the mutex, block
24848** until it can be obtained.
24849*/
24850SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
24851 if( p ){
24852 assert( sqlite3GlobalConfig.mutex.xMutexEnter );
24853 sqlite3GlobalConfig.mutex.xMutexEnter(p);
24854 }
24855}
24856
24857/*
24858** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
24859** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
24860*/
24861SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
24862 int rc = SQLITE_OK;
24863 if( p ){
24864 assert( sqlite3GlobalConfig.mutex.xMutexTry );
24865 return sqlite3GlobalConfig.mutex.xMutexTry(p);
24866 }
24867 return rc;
24868}
24869
24870/*
24871** The sqlite3_mutex_leave() routine exits a mutex that was previously
24872** entered by the same thread. The behavior is undefined if the mutex
24873** is not currently entered. If a NULL pointer is passed as an argument
24874** this function is a no-op.
24875*/
24876SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
24877 if( p ){
24878 assert( sqlite3GlobalConfig.mutex.xMutexLeave );
24879 sqlite3GlobalConfig.mutex.xMutexLeave(p);
24880 }
24881}
24882
24883#ifndef NDEBUG
24884/*
24885** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
24886** intended for use inside assert() statements.
24887*/
24888SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
24889 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld );
24890 return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
24891}
24892SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
24893 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
24894 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
24895}
24896#endif
24897
24898#endif /* !defined(SQLITE_MUTEX_OMIT) */
24899
24900/************** End of mutex.c ***********************************************/
24901/************** Begin file mutex_noop.c **************************************/
24902/*
24903** 2008 October 07
24904**
24905** The author disclaims copyright to this source code. In place of
24906** a legal notice, here is a blessing:
24907**
24908** May you do good and not evil.
24909** May you find forgiveness for yourself and forgive others.
24910** May you share freely, never taking more than you give.
24911**
24912*************************************************************************
24913** This file contains the C functions that implement mutexes.
24914**
24915** This implementation in this file does not provide any mutual
24916** exclusion and is thus suitable for use only in applications
24917** that use SQLite in a single thread. The routines defined
24918** here are place-holders. Applications can substitute working
24919** mutex routines at start-time using the
24920**
24921** sqlite3_config(SQLITE_CONFIG_MUTEX,...)
24922**
24923** interface.
24924**
24925** If compiled with SQLITE_DEBUG, then additional logic is inserted
24926** that does error checking on mutexes to make sure they are being
24927** called correctly.
24928*/
24929/* #include "sqliteInt.h" */
24930
24931#ifndef SQLITE_MUTEX_OMIT
24932
24933#ifndef SQLITE_DEBUG
24934/*
24935** Stub routines for all mutex methods.
24936**
24937** This routines provide no mutual exclusion or error checking.
24938*/
24939static int noopMutexInit(void){ return SQLITE_OK; }
24940static int noopMutexEnd(void){ return SQLITE_OK; }
24941static sqlite3_mutex *noopMutexAlloc(int id){
24942 UNUSED_PARAMETER(id);
24943 return (sqlite3_mutex*)8;
24944}
24945static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
24946static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
24947static int noopMutexTry(sqlite3_mutex *p){
24948 UNUSED_PARAMETER(p);
24949 return SQLITE_OK;
24950}
24951static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
24952
24953SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
24954 static const sqlite3_mutex_methods sMutex = {
24955 noopMutexInit,
24956 noopMutexEnd,
24957 noopMutexAlloc,
24958 noopMutexFree,
24959 noopMutexEnter,
24960 noopMutexTry,
24961 noopMutexLeave,
24962
24963 0,
24964 0,
24965 };
24966
24967 return &sMutex;
24968}
24969#endif /* !SQLITE_DEBUG */
24970
24971#ifdef SQLITE_DEBUG
24972/*
24973** In this implementation, error checking is provided for testing
24974** and debugging purposes. The mutexes still do not provide any
24975** mutual exclusion.
24976*/
24977
24978/*
24979** The mutex object
24980*/
24981typedef struct sqlite3_debug_mutex {
24982 int id; /* The mutex type */
24983 int cnt; /* Number of entries without a matching leave */
24984} sqlite3_debug_mutex;
24985
24986/*
24987** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
24988** intended for use inside assert() statements.
24989*/
24990static int debugMutexHeld(sqlite3_mutex *pX){
24991 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
24992 return p==0 || p->cnt>0;
24993}
24994static int debugMutexNotheld(sqlite3_mutex *pX){
24995 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
24996 return p==0 || p->cnt==0;
24997}
24998
24999/*
25000** Initialize and deinitialize the mutex subsystem.
25001*/
25002static int debugMutexInit(void){ return SQLITE_OK; }
25003static int debugMutexEnd(void){ return SQLITE_OK; }
25004
25005/*
25006** The sqlite3_mutex_alloc() routine allocates a new
25007** mutex and returns a pointer to it. If it returns NULL
25008** that means that a mutex could not be allocated.
25009*/
25010static sqlite3_mutex *debugMutexAlloc(int id){
25011 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1];
25012 sqlite3_debug_mutex *pNew = 0;
25013 switch( id ){
25014 case SQLITE_MUTEX_FAST:
25015 case SQLITE_MUTEX_RECURSIVE: {
25016 pNew = sqlite3Malloc(sizeof(*pNew));
25017 if( pNew ){
25018 pNew->id = id;
25019 pNew->cnt = 0;
25020 }
25021 break;
25022 }
25023 default: {
25024#ifdef SQLITE_ENABLE_API_ARMOR
25025 if( id-2<0 || id-2>=ArraySize(aStatic) ){
25026 (void)SQLITE_MISUSE_BKPT;
25027 return 0;
25028 }
25029#endif
25030 pNew = &aStatic[id-2];
25031 pNew->id = id;
25032 break;
25033 }
25034 }
25035 return (sqlite3_mutex*)pNew;
25036}
25037
25038/*
25039** This routine deallocates a previously allocated mutex.
25040*/
25041static void debugMutexFree(sqlite3_mutex *pX){
25042 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25043 assert( p->cnt==0 );
25044 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){
25045 sqlite3_free(p);
25046 }else{
25047#ifdef SQLITE_ENABLE_API_ARMOR
25048 (void)SQLITE_MISUSE_BKPT;
25049#endif
25050 }
25051}
25052
25053/*
25054** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
25055** to enter a mutex. If another thread is already within the mutex,
25056** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
25057** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
25058** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
25059** be entered multiple times by the same thread. In such cases the,
25060** mutex must be exited an equal number of times before another thread
25061** can enter. If the same thread tries to enter any other kind of mutex
25062** more than once, the behavior is undefined.
25063*/
25064static void debugMutexEnter(sqlite3_mutex *pX){
25065 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25066 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
25067 p->cnt++;
25068}
25069static int debugMutexTry(sqlite3_mutex *pX){
25070 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25071 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
25072 p->cnt++;
25073 return SQLITE_OK;
25074}
25075
25076/*
25077** The sqlite3_mutex_leave() routine exits a mutex that was
25078** previously entered by the same thread. The behavior
25079** is undefined if the mutex is not currently entered or
25080** is not currently allocated. SQLite will never do either.
25081*/
25082static void debugMutexLeave(sqlite3_mutex *pX){
25083 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25084 assert( debugMutexHeld(pX) );
25085 p->cnt--;
25086 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
25087}
25088
25089SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
25090 static const sqlite3_mutex_methods sMutex = {
25091 debugMutexInit,
25092 debugMutexEnd,
25093 debugMutexAlloc,
25094 debugMutexFree,
25095 debugMutexEnter,
25096 debugMutexTry,
25097 debugMutexLeave,
25098
25099 debugMutexHeld,
25100 debugMutexNotheld
25101 };
25102
25103 return &sMutex;
25104}
25105#endif /* SQLITE_DEBUG */
25106
25107/*
25108** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
25109** is used regardless of the run-time threadsafety setting.
25110*/
25111#ifdef SQLITE_MUTEX_NOOP
25112SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
25113 return sqlite3NoopMutex();
25114}
25115#endif /* defined(SQLITE_MUTEX_NOOP) */
25116#endif /* !defined(SQLITE_MUTEX_OMIT) */
25117
25118/************** End of mutex_noop.c ******************************************/
25119/************** Begin file mutex_unix.c **************************************/
25120/*
25121** 2007 August 28
25122**
25123** The author disclaims copyright to this source code. In place of
25124** a legal notice, here is a blessing:
25125**
25126** May you do good and not evil.
25127** May you find forgiveness for yourself and forgive others.
25128** May you share freely, never taking more than you give.
25129**
25130*************************************************************************
25131** This file contains the C functions that implement mutexes for pthreads
25132*/
25133/* #include "sqliteInt.h" */
25134
25135/*
25136** The code in this file is only used if we are compiling threadsafe
25137** under unix with pthreads.
25138**
25139** Note that this implementation requires a version of pthreads that
25140** supports recursive mutexes.
25141*/
25142#ifdef SQLITE_MUTEX_PTHREADS
25143
25144#include <pthread.h>
25145
25146/*
25147** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
25148** are necessary under two condidtions: (1) Debug builds and (2) using
25149** home-grown mutexes. Encapsulate these conditions into a single #define.
25150*/
25151#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
25152# define SQLITE_MUTEX_NREF 1
25153#else
25154# define SQLITE_MUTEX_NREF 0
25155#endif
25156
25157/*
25158** Each recursive mutex is an instance of the following structure.
25159*/
25160struct sqlite3_mutex {
25161 pthread_mutex_t mutex; /* Mutex controlling the lock */
25162#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
25163 int id; /* Mutex type */
25164#endif
25165#if SQLITE_MUTEX_NREF
25166 volatile int nRef; /* Number of entrances */
25167 volatile pthread_t owner; /* Thread that is within this mutex */
25168 int trace; /* True to trace changes */
25169#endif
25170};
25171#if SQLITE_MUTEX_NREF
25172# define SQLITE3_MUTEX_INITIALIZER(id) \
25173 {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
25174#elif defined(SQLITE_ENABLE_API_ARMOR)
25175# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
25176#else
25177#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
25178#endif
25179
25180/*
25181** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
25182** intended for use only inside assert() statements. On some platforms,
25183** there might be race conditions that can cause these routines to
25184** deliver incorrect results. In particular, if pthread_equal() is
25185** not an atomic operation, then these routines might delivery
25186** incorrect results. On most platforms, pthread_equal() is a
25187** comparison of two integers and is therefore atomic. But we are
25188** told that HPUX is not such a platform. If so, then these routines
25189** will not always work correctly on HPUX.
25190**
25191** On those platforms where pthread_equal() is not atomic, SQLite
25192** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
25193** make sure no assert() statements are evaluated and hence these
25194** routines are never called.
25195*/
25196#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
25197static int pthreadMutexHeld(sqlite3_mutex *p){
25198 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
25199}
25200static int pthreadMutexNotheld(sqlite3_mutex *p){
25201 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
25202}
25203#endif
25204
25205/*
25206** Try to provide a memory barrier operation, needed for initialization
25207** and also for the implementation of xShmBarrier in the VFS in cases
25208** where SQLite is compiled without mutexes.
25209*/
25210SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
25211#if defined(SQLITE_MEMORY_BARRIER)
25212 SQLITE_MEMORY_BARRIER;
25213#elif defined(__GNUC__) && GCC_VERSION>=4001000
25214 __sync_synchronize();
25215#endif
25216}
25217
25218/*
25219** Initialize and deinitialize the mutex subsystem.
25220*/
25221static int pthreadMutexInit(void){ return SQLITE_OK; }
25222static int pthreadMutexEnd(void){ return SQLITE_OK; }
25223
25224/*
25225** The sqlite3_mutex_alloc() routine allocates a new
25226** mutex and returns a pointer to it. If it returns NULL
25227** that means that a mutex could not be allocated. SQLite
25228** will unwind its stack and return an error. The argument
25229** to sqlite3_mutex_alloc() is one of these integer constants:
25230**
25231** <ul>
25232** <li> SQLITE_MUTEX_FAST
25233** <li> SQLITE_MUTEX_RECURSIVE
25234** <li> SQLITE_MUTEX_STATIC_MASTER
25235** <li> SQLITE_MUTEX_STATIC_MEM
25236** <li> SQLITE_MUTEX_STATIC_OPEN
25237** <li> SQLITE_MUTEX_STATIC_PRNG
25238** <li> SQLITE_MUTEX_STATIC_LRU
25239** <li> SQLITE_MUTEX_STATIC_PMEM
25240** <li> SQLITE_MUTEX_STATIC_APP1
25241** <li> SQLITE_MUTEX_STATIC_APP2
25242** <li> SQLITE_MUTEX_STATIC_APP3
25243** <li> SQLITE_MUTEX_STATIC_VFS1
25244** <li> SQLITE_MUTEX_STATIC_VFS2
25245** <li> SQLITE_MUTEX_STATIC_VFS3
25246** </ul>
25247**
25248** The first two constants cause sqlite3_mutex_alloc() to create
25249** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
25250** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
25251** The mutex implementation does not need to make a distinction
25252** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
25253** not want to. But SQLite will only request a recursive mutex in
25254** cases where it really needs one. If a faster non-recursive mutex
25255** implementation is available on the host platform, the mutex subsystem
25256** might return such a mutex in response to SQLITE_MUTEX_FAST.
25257**
25258** The other allowed parameters to sqlite3_mutex_alloc() each return
25259** a pointer to a static preexisting mutex. Six static mutexes are
25260** used by the current version of SQLite. Future versions of SQLite
25261** may add additional static mutexes. Static mutexes are for internal
25262** use by SQLite only. Applications that use SQLite mutexes should
25263** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
25264** SQLITE_MUTEX_RECURSIVE.
25265**
25266** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
25267** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
25268** returns a different mutex on every call. But for the static
25269** mutex types, the same mutex is returned on every call that has
25270** the same type number.
25271*/
25272static sqlite3_mutex *pthreadMutexAlloc(int iType){
25273 static sqlite3_mutex staticMutexes[] = {
25274 SQLITE3_MUTEX_INITIALIZER(2),
25275 SQLITE3_MUTEX_INITIALIZER(3),
25276 SQLITE3_MUTEX_INITIALIZER(4),
25277 SQLITE3_MUTEX_INITIALIZER(5),
25278 SQLITE3_MUTEX_INITIALIZER(6),
25279 SQLITE3_MUTEX_INITIALIZER(7),
25280 SQLITE3_MUTEX_INITIALIZER(8),
25281 SQLITE3_MUTEX_INITIALIZER(9),
25282 SQLITE3_MUTEX_INITIALIZER(10),
25283 SQLITE3_MUTEX_INITIALIZER(11),
25284 SQLITE3_MUTEX_INITIALIZER(12),
25285 SQLITE3_MUTEX_INITIALIZER(13)
25286 };
25287 sqlite3_mutex *p;
25288 switch( iType ){
25289 case SQLITE_MUTEX_RECURSIVE: {
25290 p = sqlite3MallocZero( sizeof(*p) );
25291 if( p ){
25292#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
25293 /* If recursive mutexes are not available, we will have to
25294 ** build our own. See below. */
25295 pthread_mutex_init(&p->mutex, 0);
25296#else
25297 /* Use a recursive mutex if it is available */
25298 pthread_mutexattr_t recursiveAttr;
25299 pthread_mutexattr_init(&recursiveAttr);
25300 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
25301 pthread_mutex_init(&p->mutex, &recursiveAttr);
25302 pthread_mutexattr_destroy(&recursiveAttr);
25303#endif
25304#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
25305 p->id = SQLITE_MUTEX_RECURSIVE;
25306#endif
25307 }
25308 break;
25309 }
25310 case SQLITE_MUTEX_FAST: {
25311 p = sqlite3MallocZero( sizeof(*p) );
25312 if( p ){
25313 pthread_mutex_init(&p->mutex, 0);
25314#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
25315 p->id = SQLITE_MUTEX_FAST;
25316#endif
25317 }
25318 break;
25319 }
25320 default: {
25321#ifdef SQLITE_ENABLE_API_ARMOR
25322 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
25323 (void)SQLITE_MISUSE_BKPT;
25324 return 0;
25325 }
25326#endif
25327 p = &staticMutexes[iType-2];
25328 break;
25329 }
25330 }
25331#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
25332 assert( p==0 || p->id==iType );
25333#endif
25334 return p;
25335}
25336
25337
25338/*
25339** This routine deallocates a previously
25340** allocated mutex. SQLite is careful to deallocate every
25341** mutex that it allocates.
25342*/
25343static void pthreadMutexFree(sqlite3_mutex *p){
25344 assert( p->nRef==0 );
25345#if SQLITE_ENABLE_API_ARMOR
25346 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
25347#endif
25348 {
25349 pthread_mutex_destroy(&p->mutex);
25350 sqlite3_free(p);
25351 }
25352#ifdef SQLITE_ENABLE_API_ARMOR
25353 else{
25354 (void)SQLITE_MISUSE_BKPT;
25355 }
25356#endif
25357}
25358
25359/*
25360** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
25361** to enter a mutex. If another thread is already within the mutex,
25362** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
25363** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
25364** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
25365** be entered multiple times by the same thread. In such cases the,
25366** mutex must be exited an equal number of times before another thread
25367** can enter. If the same thread tries to enter any other kind of mutex
25368** more than once, the behavior is undefined.
25369*/
25370static void pthreadMutexEnter(sqlite3_mutex *p){
25371 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
25372
25373#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
25374 /* If recursive mutexes are not available, then we have to grow
25375 ** our own. This implementation assumes that pthread_equal()
25376 ** is atomic - that it cannot be deceived into thinking self
25377 ** and p->owner are equal if p->owner changes between two values
25378 ** that are not equal to self while the comparison is taking place.
25379 ** This implementation also assumes a coherent cache - that
25380 ** separate processes cannot read different values from the same
25381 ** address at the same time. If either of these two conditions
25382 ** are not met, then the mutexes will fail and problems will result.
25383 */
25384 {
25385 pthread_t self = pthread_self();
25386 if( p->nRef>0 && pthread_equal(p->owner, self) ){
25387 p->nRef++;
25388 }else{
25389 pthread_mutex_lock(&p->mutex);
25390 assert( p->nRef==0 );
25391 p->owner = self;
25392 p->nRef = 1;
25393 }
25394 }
25395#else
25396 /* Use the built-in recursive mutexes if they are available.
25397 */
25398 pthread_mutex_lock(&p->mutex);
25399#if SQLITE_MUTEX_NREF
25400 assert( p->nRef>0 || p->owner==0 );
25401 p->owner = pthread_self();
25402 p->nRef++;
25403#endif
25404#endif
25405
25406#ifdef SQLITE_DEBUG
25407 if( p->trace ){
25408 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
25409 }
25410#endif
25411}
25412static int pthreadMutexTry(sqlite3_mutex *p){
25413 int rc;
25414 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
25415
25416#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
25417 /* If recursive mutexes are not available, then we have to grow
25418 ** our own. This implementation assumes that pthread_equal()
25419 ** is atomic - that it cannot be deceived into thinking self
25420 ** and p->owner are equal if p->owner changes between two values
25421 ** that are not equal to self while the comparison is taking place.
25422 ** This implementation also assumes a coherent cache - that
25423 ** separate processes cannot read different values from the same
25424 ** address at the same time. If either of these two conditions
25425 ** are not met, then the mutexes will fail and problems will result.
25426 */
25427 {
25428 pthread_t self = pthread_self();
25429 if( p->nRef>0 && pthread_equal(p->owner, self) ){
25430 p->nRef++;
25431 rc = SQLITE_OK;
25432 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
25433 assert( p->nRef==0 );
25434 p->owner = self;
25435 p->nRef = 1;
25436 rc = SQLITE_OK;
25437 }else{
25438 rc = SQLITE_BUSY;
25439 }
25440 }
25441#else
25442 /* Use the built-in recursive mutexes if they are available.
25443 */
25444 if( pthread_mutex_trylock(&p->mutex)==0 ){
25445#if SQLITE_MUTEX_NREF
25446 p->owner = pthread_self();
25447 p->nRef++;
25448#endif
25449 rc = SQLITE_OK;
25450 }else{
25451 rc = SQLITE_BUSY;
25452 }
25453#endif
25454
25455#ifdef SQLITE_DEBUG
25456 if( rc==SQLITE_OK && p->trace ){
25457 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
25458 }
25459#endif
25460 return rc;
25461}
25462
25463/*
25464** The sqlite3_mutex_leave() routine exits a mutex that was
25465** previously entered by the same thread. The behavior
25466** is undefined if the mutex is not currently entered or
25467** is not currently allocated. SQLite will never do either.
25468*/
25469static void pthreadMutexLeave(sqlite3_mutex *p){
25470 assert( pthreadMutexHeld(p) );
25471#if SQLITE_MUTEX_NREF
25472 p->nRef--;
25473 if( p->nRef==0 ) p->owner = 0;
25474#endif
25475 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
25476
25477#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
25478 if( p->nRef==0 ){
25479 pthread_mutex_unlock(&p->mutex);
25480 }
25481#else
25482 pthread_mutex_unlock(&p->mutex);
25483#endif
25484
25485#ifdef SQLITE_DEBUG
25486 if( p->trace ){
25487 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
25488 }
25489#endif
25490}
25491
25492SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
25493 static const sqlite3_mutex_methods sMutex = {
25494 pthreadMutexInit,
25495 pthreadMutexEnd,
25496 pthreadMutexAlloc,
25497 pthreadMutexFree,
25498 pthreadMutexEnter,
25499 pthreadMutexTry,
25500 pthreadMutexLeave,
25501#ifdef SQLITE_DEBUG
25502 pthreadMutexHeld,
25503 pthreadMutexNotheld
25504#else
25505 0,
25506 0
25507#endif
25508 };
25509
25510 return &sMutex;
25511}
25512
25513#endif /* SQLITE_MUTEX_PTHREADS */
25514
25515/************** End of mutex_unix.c ******************************************/
25516/************** Begin file mutex_w32.c ***************************************/
25517/*
25518** 2007 August 14
25519**
25520** The author disclaims copyright to this source code. In place of
25521** a legal notice, here is a blessing:
25522**
25523** May you do good and not evil.
25524** May you find forgiveness for yourself and forgive others.
25525** May you share freely, never taking more than you give.
25526**
25527*************************************************************************
25528** This file contains the C functions that implement mutexes for Win32.
25529*/
25530/* #include "sqliteInt.h" */
25531
25532#if SQLITE_OS_WIN
25533/*
25534** Include code that is common to all os_*.c files
25535*/
25536/************** Include os_common.h in the middle of mutex_w32.c *************/
25537/************** Begin file os_common.h ***************************************/
25538/*
25539** 2004 May 22
25540**
25541** The author disclaims copyright to this source code. In place of
25542** a legal notice, here is a blessing:
25543**
25544** May you do good and not evil.
25545** May you find forgiveness for yourself and forgive others.
25546** May you share freely, never taking more than you give.
25547**
25548******************************************************************************
25549**
25550** This file contains macros and a little bit of code that is common to
25551** all of the platform-specific files (os_*.c) and is #included into those
25552** files.
25553**
25554** This file should be #included by the os_*.c files only. It is not a
25555** general purpose header file.
25556*/
25557#ifndef _OS_COMMON_H_
25558#define _OS_COMMON_H_
25559
25560/*
25561** At least two bugs have slipped in because we changed the MEMORY_DEBUG
25562** macro to SQLITE_DEBUG and some older makefiles have not yet made the
25563** switch. The following code should catch this problem at compile-time.
25564*/
25565#ifdef MEMORY_DEBUG
25566# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
25567#endif
25568
25569/*
25570** Macros for performance tracing. Normally turned off. Only works
25571** on i486 hardware.
25572*/
25573#ifdef SQLITE_PERFORMANCE_TRACE
25574
25575/*
25576** hwtime.h contains inline assembler code for implementing
25577** high-performance timing routines.
25578*/
25579/************** Include hwtime.h in the middle of os_common.h ****************/
25580/************** Begin file hwtime.h ******************************************/
25581/*
25582** 2008 May 27
25583**
25584** The author disclaims copyright to this source code. In place of
25585** a legal notice, here is a blessing:
25586**
25587** May you do good and not evil.
25588** May you find forgiveness for yourself and forgive others.
25589** May you share freely, never taking more than you give.
25590**
25591******************************************************************************
25592**
25593** This file contains inline asm code for retrieving "high-performance"
25594** counters for x86 class CPUs.
25595*/
25596#ifndef SQLITE_HWTIME_H
25597#define SQLITE_HWTIME_H
25598
25599/*
25600** The following routine only works on pentium-class (or newer) processors.
25601** It uses the RDTSC opcode to read the cycle count value out of the
25602** processor and returns that value. This can be used for high-res
25603** profiling.
25604*/
25605#if (defined(__GNUC__) || defined(_MSC_VER)) && \
25606 (defined(i386) || defined(__i386__) || defined(_M_IX86))
25607
25608 #if defined(__GNUC__)
25609
25610 __inline__ sqlite_uint64 sqlite3Hwtime(void){
25611 unsigned int lo, hi;
25612 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
25613 return (sqlite_uint64)hi << 32 | lo;
25614 }
25615
25616 #elif defined(_MSC_VER)
25617
25618 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
25619 __asm {
25620 rdtsc
25621 ret ; return value at EDX:EAX
25622 }
25623 }
25624
25625 #endif
25626
25627#elif (defined(__GNUC__) && defined(__x86_64__))
25628
25629 __inline__ sqlite_uint64 sqlite3Hwtime(void){
25630 unsigned long val;
25631 __asm__ __volatile__ ("rdtsc" : "=A" (val));
25632 return val;
25633 }
25634
25635#elif (defined(__GNUC__) && defined(__ppc__))
25636
25637 __inline__ sqlite_uint64 sqlite3Hwtime(void){
25638 unsigned long long retval;
25639 unsigned long junk;
25640 __asm__ __volatile__ ("\n\
25641 1: mftbu %1\n\
25642 mftb %L0\n\
25643 mftbu %0\n\
25644 cmpw %0,%1\n\
25645 bne 1b"
25646 : "=r" (retval), "=r" (junk));
25647 return retval;
25648 }
25649
25650#else
25651
25652 #error Need implementation of sqlite3Hwtime() for your platform.
25653
25654 /*
25655 ** To compile without implementing sqlite3Hwtime() for your platform,
25656 ** you can remove the above #error and use the following
25657 ** stub function. You will lose timing support for many
25658 ** of the debugging and testing utilities, but it should at
25659 ** least compile and run.
25660 */
25661SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
25662
25663#endif
25664
25665#endif /* !defined(SQLITE_HWTIME_H) */
25666
25667/************** End of hwtime.h **********************************************/
25668/************** Continuing where we left off in os_common.h ******************/
25669
25670static sqlite_uint64 g_start;
25671static sqlite_uint64 g_elapsed;
25672#define TIMER_START g_start=sqlite3Hwtime()
25673#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
25674#define TIMER_ELAPSED g_elapsed
25675#else
25676#define TIMER_START
25677#define TIMER_END
25678#define TIMER_ELAPSED ((sqlite_uint64)0)
25679#endif
25680
25681/*
25682** If we compile with the SQLITE_TEST macro set, then the following block
25683** of code will give us the ability to simulate a disk I/O error. This
25684** is used for testing the I/O recovery logic.
25685*/
25686#if defined(SQLITE_TEST)
25687SQLITE_API extern int sqlite3_io_error_hit;
25688SQLITE_API extern int sqlite3_io_error_hardhit;
25689SQLITE_API extern int sqlite3_io_error_pending;
25690SQLITE_API extern int sqlite3_io_error_persist;
25691SQLITE_API extern int sqlite3_io_error_benign;
25692SQLITE_API extern int sqlite3_diskfull_pending;
25693SQLITE_API extern int sqlite3_diskfull;
25694#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
25695#define SimulateIOError(CODE) \
25696 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
25697 || sqlite3_io_error_pending-- == 1 ) \
25698 { local_ioerr(); CODE; }
25699static void local_ioerr(){
25700 IOTRACE(("IOERR\n"));
25701 sqlite3_io_error_hit++;
25702 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
25703}
25704#define SimulateDiskfullError(CODE) \
25705 if( sqlite3_diskfull_pending ){ \
25706 if( sqlite3_diskfull_pending == 1 ){ \
25707 local_ioerr(); \
25708 sqlite3_diskfull = 1; \
25709 sqlite3_io_error_hit = 1; \
25710 CODE; \
25711 }else{ \
25712 sqlite3_diskfull_pending--; \
25713 } \
25714 }
25715#else
25716#define SimulateIOErrorBenign(X)
25717#define SimulateIOError(A)
25718#define SimulateDiskfullError(A)
25719#endif /* defined(SQLITE_TEST) */
25720
25721/*
25722** When testing, keep a count of the number of open files.
25723*/
25724#if defined(SQLITE_TEST)
25725SQLITE_API extern int sqlite3_open_file_count;
25726#define OpenCounter(X) sqlite3_open_file_count+=(X)
25727#else
25728#define OpenCounter(X)
25729#endif /* defined(SQLITE_TEST) */
25730
25731#endif /* !defined(_OS_COMMON_H_) */
25732
25733/************** End of os_common.h *******************************************/
25734/************** Continuing where we left off in mutex_w32.c ******************/
25735
25736/*
25737** Include the header file for the Windows VFS.
25738*/
25739/************** Include os_win.h in the middle of mutex_w32.c ****************/
25740/************** Begin file os_win.h ******************************************/
25741/*
25742** 2013 November 25
25743**
25744** The author disclaims copyright to this source code. In place of
25745** a legal notice, here is a blessing:
25746**
25747** May you do good and not evil.
25748** May you find forgiveness for yourself and forgive others.
25749** May you share freely, never taking more than you give.
25750**
25751******************************************************************************
25752**
25753** This file contains code that is specific to Windows.
25754*/
25755#ifndef SQLITE_OS_WIN_H
25756#define SQLITE_OS_WIN_H
25757
25758/*
25759** Include the primary Windows SDK header file.
25760*/
25761#include "windows.h"
25762
25763#ifdef __CYGWIN__
25764# include <sys/cygwin.h>
25765# include <errno.h> /* amalgamator: dontcache */
25766#endif
25767
25768/*
25769** Determine if we are dealing with Windows NT.
25770**
25771** We ought to be able to determine if we are compiling for Windows 9x or
25772** Windows NT using the _WIN32_WINNT macro as follows:
25773**
25774** #if defined(_WIN32_WINNT)
25775** # define SQLITE_OS_WINNT 1
25776** #else
25777** # define SQLITE_OS_WINNT 0
25778** #endif
25779**
25780** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
25781** it ought to, so the above test does not work. We'll just assume that
25782** everything is Windows NT unless the programmer explicitly says otherwise
25783** by setting SQLITE_OS_WINNT to 0.
25784*/
25785#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
25786# define SQLITE_OS_WINNT 1
25787#endif
25788
25789/*
25790** Determine if we are dealing with Windows CE - which has a much reduced
25791** API.
25792*/
25793#if defined(_WIN32_WCE)
25794# define SQLITE_OS_WINCE 1
25795#else
25796# define SQLITE_OS_WINCE 0
25797#endif
25798
25799/*
25800** Determine if we are dealing with WinRT, which provides only a subset of
25801** the full Win32 API.
25802*/
25803#if !defined(SQLITE_OS_WINRT)
25804# define SQLITE_OS_WINRT 0
25805#endif
25806
25807/*
25808** For WinCE, some API function parameters do not appear to be declared as
25809** volatile.
25810*/
25811#if SQLITE_OS_WINCE
25812# define SQLITE_WIN32_VOLATILE
25813#else
25814# define SQLITE_WIN32_VOLATILE volatile
25815#endif
25816
25817/*
25818** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
25819** functions are not available (e.g. those not using MSVC, Cygwin, etc).
25820*/
25821#if SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
25822 SQLITE_THREADSAFE>0 && !defined(__CYGWIN__)
25823# define SQLITE_OS_WIN_THREADS 1
25824#else
25825# define SQLITE_OS_WIN_THREADS 0
25826#endif
25827
25828#endif /* SQLITE_OS_WIN_H */
25829
25830/************** End of os_win.h **********************************************/
25831/************** Continuing where we left off in mutex_w32.c ******************/
25832#endif
25833
25834/*
25835** The code in this file is only used if we are compiling multithreaded
25836** on a Win32 system.
25837*/
25838#ifdef SQLITE_MUTEX_W32
25839
25840/*
25841** Each recursive mutex is an instance of the following structure.
25842*/
25843struct sqlite3_mutex {
25844 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
25845 int id; /* Mutex type */
25846#ifdef SQLITE_DEBUG
25847 volatile int nRef; /* Number of enterances */
25848 volatile DWORD owner; /* Thread holding this mutex */
25849 volatile LONG trace; /* True to trace changes */
25850#endif
25851};
25852
25853/*
25854** These are the initializer values used when declaring a "static" mutex
25855** on Win32. It should be noted that all mutexes require initialization
25856** on the Win32 platform.
25857*/
25858#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
25859
25860#ifdef SQLITE_DEBUG
25861#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
25862 0L, (DWORD)0, 0 }
25863#else
25864#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
25865#endif
25866
25867#ifdef SQLITE_DEBUG
25868/*
25869** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
25870** intended for use only inside assert() statements.
25871*/
25872static int winMutexHeld(sqlite3_mutex *p){
25873 return p->nRef!=0 && p->owner==GetCurrentThreadId();
25874}
25875
25876static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
25877 return p->nRef==0 || p->owner!=tid;
25878}
25879
25880static int winMutexNotheld(sqlite3_mutex *p){
25881 DWORD tid = GetCurrentThreadId();
25882 return winMutexNotheld2(p, tid);
25883}
25884#endif
25885
25886/*
25887** Try to provide a memory barrier operation, needed for initialization
25888** and also for the xShmBarrier method of the VFS in cases when SQLite is
25889** compiled without mutexes (SQLITE_THREADSAFE=0).
25890*/
25891SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
25892#if defined(SQLITE_MEMORY_BARRIER)
25893 SQLITE_MEMORY_BARRIER;
25894#elif defined(__GNUC__)
25895 __sync_synchronize();
25896#elif MSVC_VERSION>=1300
25897 _ReadWriteBarrier();
25898#elif defined(MemoryBarrier)
25899 MemoryBarrier();
25900#endif
25901}
25902
25903/*
25904** Initialize and deinitialize the mutex subsystem.
25905*/
25906static sqlite3_mutex winMutex_staticMutexes[] = {
25907 SQLITE3_MUTEX_INITIALIZER(2),
25908 SQLITE3_MUTEX_INITIALIZER(3),
25909 SQLITE3_MUTEX_INITIALIZER(4),
25910 SQLITE3_MUTEX_INITIALIZER(5),
25911 SQLITE3_MUTEX_INITIALIZER(6),
25912 SQLITE3_MUTEX_INITIALIZER(7),
25913 SQLITE3_MUTEX_INITIALIZER(8),
25914 SQLITE3_MUTEX_INITIALIZER(9),
25915 SQLITE3_MUTEX_INITIALIZER(10),
25916 SQLITE3_MUTEX_INITIALIZER(11),
25917 SQLITE3_MUTEX_INITIALIZER(12),
25918 SQLITE3_MUTEX_INITIALIZER(13)
25919};
25920
25921static int winMutex_isInit = 0;
25922static int winMutex_isNt = -1; /* <0 means "need to query" */
25923
25924/* As the winMutexInit() and winMutexEnd() functions are called as part
25925** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
25926** "interlocked" magic used here is probably not strictly necessary.
25927*/
25928static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
25929
25930SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
25931SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
25932
25933static int winMutexInit(void){
25934 /* The first to increment to 1 does actual initialization */
25935 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
25936 int i;
25937 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
25938#if SQLITE_OS_WINRT
25939 InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
25940#else
25941 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
25942#endif
25943 }
25944 winMutex_isInit = 1;
25945 }else{
25946 /* Another thread is (in the process of) initializing the static
25947 ** mutexes */
25948 while( !winMutex_isInit ){
25949 sqlite3_win32_sleep(1);
25950 }
25951 }
25952 return SQLITE_OK;
25953}
25954
25955static int winMutexEnd(void){
25956 /* The first to decrement to 0 does actual shutdown
25957 ** (which should be the last to shutdown.) */
25958 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
25959 if( winMutex_isInit==1 ){
25960 int i;
25961 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
25962 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
25963 }
25964 winMutex_isInit = 0;
25965 }
25966 }
25967 return SQLITE_OK;
25968}
25969
25970/*
25971** The sqlite3_mutex_alloc() routine allocates a new
25972** mutex and returns a pointer to it. If it returns NULL
25973** that means that a mutex could not be allocated. SQLite
25974** will unwind its stack and return an error. The argument
25975** to sqlite3_mutex_alloc() is one of these integer constants:
25976**
25977** <ul>
25978** <li> SQLITE_MUTEX_FAST
25979** <li> SQLITE_MUTEX_RECURSIVE
25980** <li> SQLITE_MUTEX_STATIC_MASTER
25981** <li> SQLITE_MUTEX_STATIC_MEM
25982** <li> SQLITE_MUTEX_STATIC_OPEN
25983** <li> SQLITE_MUTEX_STATIC_PRNG
25984** <li> SQLITE_MUTEX_STATIC_LRU
25985** <li> SQLITE_MUTEX_STATIC_PMEM
25986** <li> SQLITE_MUTEX_STATIC_APP1
25987** <li> SQLITE_MUTEX_STATIC_APP2
25988** <li> SQLITE_MUTEX_STATIC_APP3
25989** <li> SQLITE_MUTEX_STATIC_VFS1
25990** <li> SQLITE_MUTEX_STATIC_VFS2
25991** <li> SQLITE_MUTEX_STATIC_VFS3
25992** </ul>
25993**
25994** The first two constants cause sqlite3_mutex_alloc() to create
25995** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
25996** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
25997** The mutex implementation does not need to make a distinction
25998** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
25999** not want to. But SQLite will only request a recursive mutex in
26000** cases where it really needs one. If a faster non-recursive mutex
26001** implementation is available on the host platform, the mutex subsystem
26002** might return such a mutex in response to SQLITE_MUTEX_FAST.
26003**
26004** The other allowed parameters to sqlite3_mutex_alloc() each return
26005** a pointer to a static preexisting mutex. Six static mutexes are
26006** used by the current version of SQLite. Future versions of SQLite
26007** may add additional static mutexes. Static mutexes are for internal
26008** use by SQLite only. Applications that use SQLite mutexes should
26009** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
26010** SQLITE_MUTEX_RECURSIVE.
26011**
26012** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
26013** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
26014** returns a different mutex on every call. But for the static
26015** mutex types, the same mutex is returned on every call that has
26016** the same type number.
26017*/
26018static sqlite3_mutex *winMutexAlloc(int iType){
26019 sqlite3_mutex *p;
26020
26021 switch( iType ){
26022 case SQLITE_MUTEX_FAST:
26023 case SQLITE_MUTEX_RECURSIVE: {
26024 p = sqlite3MallocZero( sizeof(*p) );
26025 if( p ){
26026 p->id = iType;
26027#ifdef SQLITE_DEBUG
26028#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
26029 p->trace = 1;
26030#endif
26031#endif
26032#if SQLITE_OS_WINRT
26033 InitializeCriticalSectionEx(&p->mutex, 0, 0);
26034#else
26035 InitializeCriticalSection(&p->mutex);
26036#endif
26037 }
26038 break;
26039 }
26040 default: {
26041#ifdef SQLITE_ENABLE_API_ARMOR
26042 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
26043 (void)SQLITE_MISUSE_BKPT;
26044 return 0;
26045 }
26046#endif
26047 p = &winMutex_staticMutexes[iType-2];
26048#ifdef SQLITE_DEBUG
26049#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
26050 InterlockedCompareExchange(&p->trace, 1, 0);
26051#endif
26052#endif
26053 break;
26054 }
26055 }
26056 assert( p==0 || p->id==iType );
26057 return p;
26058}
26059
26060
26061/*
26062** This routine deallocates a previously
26063** allocated mutex. SQLite is careful to deallocate every
26064** mutex that it allocates.
26065*/
26066static void winMutexFree(sqlite3_mutex *p){
26067 assert( p );
26068 assert( p->nRef==0 && p->owner==0 );
26069 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
26070 DeleteCriticalSection(&p->mutex);
26071 sqlite3_free(p);
26072 }else{
26073#ifdef SQLITE_ENABLE_API_ARMOR
26074 (void)SQLITE_MISUSE_BKPT;
26075#endif
26076 }
26077}
26078
26079/*
26080** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
26081** to enter a mutex. If another thread is already within the mutex,
26082** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
26083** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
26084** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
26085** be entered multiple times by the same thread. In such cases the,
26086** mutex must be exited an equal number of times before another thread
26087** can enter. If the same thread tries to enter any other kind of mutex
26088** more than once, the behavior is undefined.
26089*/
26090static void winMutexEnter(sqlite3_mutex *p){
26091#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26092 DWORD tid = GetCurrentThreadId();
26093#endif
26094#ifdef SQLITE_DEBUG
26095 assert( p );
26096 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
26097#else
26098 assert( p );
26099#endif
26100 assert( winMutex_isInit==1 );
26101 EnterCriticalSection(&p->mutex);
26102#ifdef SQLITE_DEBUG
26103 assert( p->nRef>0 || p->owner==0 );
26104 p->owner = tid;
26105 p->nRef++;
26106 if( p->trace ){
26107 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
26108 tid, p->id, p, p->trace, p->nRef));
26109 }
26110#endif
26111}
26112
26113static int winMutexTry(sqlite3_mutex *p){
26114#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26115 DWORD tid = GetCurrentThreadId();
26116#endif
26117 int rc = SQLITE_BUSY;
26118 assert( p );
26119 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
26120 /*
26121 ** The sqlite3_mutex_try() routine is very rarely used, and when it
26122 ** is used it is merely an optimization. So it is OK for it to always
26123 ** fail.
26124 **
26125 ** The TryEnterCriticalSection() interface is only available on WinNT.
26126 ** And some windows compilers complain if you try to use it without
26127 ** first doing some #defines that prevent SQLite from building on Win98.
26128 ** For that reason, we will omit this optimization for now. See
26129 ** ticket #2685.
26130 */
26131#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
26132 assert( winMutex_isInit==1 );
26133 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
26134 if( winMutex_isNt<0 ){
26135 winMutex_isNt = sqlite3_win32_is_nt();
26136 }
26137 assert( winMutex_isNt==0 || winMutex_isNt==1 );
26138 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
26139#ifdef SQLITE_DEBUG
26140 p->owner = tid;
26141 p->nRef++;
26142#endif
26143 rc = SQLITE_OK;
26144 }
26145#else
26146 UNUSED_PARAMETER(p);
26147#endif
26148#ifdef SQLITE_DEBUG
26149 if( p->trace ){
26150 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
26151 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
26152 }
26153#endif
26154 return rc;
26155}
26156
26157/*
26158** The sqlite3_mutex_leave() routine exits a mutex that was
26159** previously entered by the same thread. The behavior
26160** is undefined if the mutex is not currently entered or
26161** is not currently allocated. SQLite will never do either.
26162*/
26163static void winMutexLeave(sqlite3_mutex *p){
26164#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26165 DWORD tid = GetCurrentThreadId();
26166#endif
26167 assert( p );
26168#ifdef SQLITE_DEBUG
26169 assert( p->nRef>0 );
26170 assert( p->owner==tid );
26171 p->nRef--;
26172 if( p->nRef==0 ) p->owner = 0;
26173 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
26174#endif
26175 assert( winMutex_isInit==1 );
26176 LeaveCriticalSection(&p->mutex);
26177#ifdef SQLITE_DEBUG
26178 if( p->trace ){
26179 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
26180 tid, p->id, p, p->trace, p->nRef));
26181 }
26182#endif
26183}
26184
26185SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
26186 static const sqlite3_mutex_methods sMutex = {
26187 winMutexInit,
26188 winMutexEnd,
26189 winMutexAlloc,
26190 winMutexFree,
26191 winMutexEnter,
26192 winMutexTry,
26193 winMutexLeave,
26194#ifdef SQLITE_DEBUG
26195 winMutexHeld,
26196 winMutexNotheld
26197#else
26198 0,
26199 0
26200#endif
26201 };
26202 return &sMutex;
26203}
26204
26205#endif /* SQLITE_MUTEX_W32 */
26206
26207/************** End of mutex_w32.c *******************************************/
26208/************** Begin file malloc.c ******************************************/
26209/*
26210** 2001 September 15
26211**
26212** The author disclaims copyright to this source code. In place of
26213** a legal notice, here is a blessing:
26214**
26215** May you do good and not evil.
26216** May you find forgiveness for yourself and forgive others.
26217** May you share freely, never taking more than you give.
26218**
26219*************************************************************************
26220**
26221** Memory allocation functions used throughout sqlite.
26222*/
26223/* #include "sqliteInt.h" */
26224/* #include <stdarg.h> */
26225
26226/*
26227** Attempt to release up to n bytes of non-essential memory currently
26228** held by SQLite. An example of non-essential memory is memory used to
26229** cache database pages that are not currently in use.
26230*/
26231SQLITE_API int sqlite3_release_memory(int n){
26232#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
26233 return sqlite3PcacheReleaseMemory(n);
26234#else
26235 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
26236 ** is a no-op returning zero if SQLite is not compiled with
26237 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
26238 UNUSED_PARAMETER(n);
26239 return 0;
26240#endif
26241}
26242
26243/*
26244** State information local to the memory allocation subsystem.
26245*/
26246static SQLITE_WSD struct Mem0Global {
26247 sqlite3_mutex *mutex; /* Mutex to serialize access */
26248 sqlite3_int64 alarmThreshold; /* The soft heap limit */
26249
26250 /*
26251 ** True if heap is nearly "full" where "full" is defined by the
26252 ** sqlite3_soft_heap_limit() setting.
26253 */
26254 int nearlyFull;
26255} mem0 = { 0, 0, 0 };
26256
26257#define mem0 GLOBAL(struct Mem0Global, mem0)
26258
26259/*
26260** Return the memory allocator mutex. sqlite3_status() needs it.
26261*/
26262SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){
26263 return mem0.mutex;
26264}
26265
26266#ifndef SQLITE_OMIT_DEPRECATED
26267/*
26268** Deprecated external interface. It used to set an alarm callback
26269** that was invoked when memory usage grew too large. Now it is a
26270** no-op.
26271*/
26272SQLITE_API int sqlite3_memory_alarm(
26273 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
26274 void *pArg,
26275 sqlite3_int64 iThreshold
26276){
26277 (void)xCallback;
26278 (void)pArg;
26279 (void)iThreshold;
26280 return SQLITE_OK;
26281}
26282#endif
26283
26284/*
26285** Set the soft heap-size limit for the library. Passing a zero or
26286** negative value indicates no limit.
26287*/
26288SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
26289 sqlite3_int64 priorLimit;
26290 sqlite3_int64 excess;
26291 sqlite3_int64 nUsed;
26292#ifndef SQLITE_OMIT_AUTOINIT
26293 int rc = sqlite3_initialize();
26294 if( rc ) return -1;
26295#endif
26296 sqlite3_mutex_enter(mem0.mutex);
26297 priorLimit = mem0.alarmThreshold;
26298 if( n<0 ){
26299 sqlite3_mutex_leave(mem0.mutex);
26300 return priorLimit;
26301 }
26302 mem0.alarmThreshold = n;
26303 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
26304 mem0.nearlyFull = (n>0 && n<=nUsed);
26305 sqlite3_mutex_leave(mem0.mutex);
26306 excess = sqlite3_memory_used() - n;
26307 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
26308 return priorLimit;
26309}
26310SQLITE_API void sqlite3_soft_heap_limit(int n){
26311 if( n<0 ) n = 0;
26312 sqlite3_soft_heap_limit64(n);
26313}
26314
26315/*
26316** Initialize the memory allocation subsystem.
26317*/
26318SQLITE_PRIVATE int sqlite3MallocInit(void){
26319 int rc;
26320 if( sqlite3GlobalConfig.m.xMalloc==0 ){
26321 sqlite3MemSetDefault();
26322 }
26323 memset(&mem0, 0, sizeof(mem0));
26324 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
26325 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
26326 || sqlite3GlobalConfig.nPage<=0 ){
26327 sqlite3GlobalConfig.pPage = 0;
26328 sqlite3GlobalConfig.szPage = 0;
26329 }
26330 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
26331 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
26332 return rc;
26333}
26334
26335/*
26336** Return true if the heap is currently under memory pressure - in other
26337** words if the amount of heap used is close to the limit set by
26338** sqlite3_soft_heap_limit().
26339*/
26340SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
26341 return mem0.nearlyFull;
26342}
26343
26344/*
26345** Deinitialize the memory allocation subsystem.
26346*/
26347SQLITE_PRIVATE void sqlite3MallocEnd(void){
26348 if( sqlite3GlobalConfig.m.xShutdown ){
26349 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
26350 }
26351 memset(&mem0, 0, sizeof(mem0));
26352}
26353
26354/*
26355** Return the amount of memory currently checked out.
26356*/
26357SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
26358 sqlite3_int64 res, mx;
26359 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
26360 return res;
26361}
26362
26363/*
26364** Return the maximum amount of memory that has ever been
26365** checked out since either the beginning of this process
26366** or since the most recent reset.
26367*/
26368SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
26369 sqlite3_int64 res, mx;
26370 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
26371 return mx;
26372}
26373
26374/*
26375** Trigger the alarm
26376*/
26377static void sqlite3MallocAlarm(int nByte){
26378 if( mem0.alarmThreshold<=0 ) return;
26379 sqlite3_mutex_leave(mem0.mutex);
26380 sqlite3_release_memory(nByte);
26381 sqlite3_mutex_enter(mem0.mutex);
26382}
26383
26384/*
26385** Do a memory allocation with statistics and alarms. Assume the
26386** lock is already held.
26387*/
26388static void mallocWithAlarm(int n, void **pp){
26389 void *p;
26390 int nFull;
26391 assert( sqlite3_mutex_held(mem0.mutex) );
26392 assert( n>0 );
26393
26394 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
26395 ** implementation of malloc_good_size(), which must be called in debug
26396 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
26397 ** or else a crash results. Hence, do not attempt to optimize out the
26398 ** following xRoundup() call. */
26399 nFull = sqlite3GlobalConfig.m.xRoundup(n);
26400
26401#ifdef SQLITE_MAX_MEMORY
26402 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nFull>SQLITE_MAX_MEMORY ){
26403 *pp = 0;
26404 return;
26405 }
26406#endif
26407
26408 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
26409 if( mem0.alarmThreshold>0 ){
26410 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
26411 if( nUsed >= mem0.alarmThreshold - nFull ){
26412 mem0.nearlyFull = 1;
26413 sqlite3MallocAlarm(nFull);
26414 }else{
26415 mem0.nearlyFull = 0;
26416 }
26417 }
26418 p = sqlite3GlobalConfig.m.xMalloc(nFull);
26419#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
26420 if( p==0 && mem0.alarmThreshold>0 ){
26421 sqlite3MallocAlarm(nFull);
26422 p = sqlite3GlobalConfig.m.xMalloc(nFull);
26423 }
26424#endif
26425 if( p ){
26426 nFull = sqlite3MallocSize(p);
26427 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
26428 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
26429 }
26430 *pp = p;
26431}
26432
26433/*
26434** Allocate memory. This routine is like sqlite3_malloc() except that it
26435** assumes the memory subsystem has already been initialized.
26436*/
26437SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
26438 void *p;
26439 if( n==0 || n>=0x7fffff00 ){
26440 /* A memory allocation of a number of bytes which is near the maximum
26441 ** signed integer value might cause an integer overflow inside of the
26442 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
26443 ** 255 bytes of overhead. SQLite itself will never use anything near
26444 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
26445 p = 0;
26446 }else if( sqlite3GlobalConfig.bMemstat ){
26447 sqlite3_mutex_enter(mem0.mutex);
26448 mallocWithAlarm((int)n, &p);
26449 sqlite3_mutex_leave(mem0.mutex);
26450 }else{
26451 p = sqlite3GlobalConfig.m.xMalloc((int)n);
26452 }
26453 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
26454 return p;
26455}
26456
26457/*
26458** This version of the memory allocation is for use by the application.
26459** First make sure the memory subsystem is initialized, then do the
26460** allocation.
26461*/
26462SQLITE_API void *sqlite3_malloc(int n){
26463#ifndef SQLITE_OMIT_AUTOINIT
26464 if( sqlite3_initialize() ) return 0;
26465#endif
26466 return n<=0 ? 0 : sqlite3Malloc(n);
26467}
26468SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){
26469#ifndef SQLITE_OMIT_AUTOINIT
26470 if( sqlite3_initialize() ) return 0;
26471#endif
26472 return sqlite3Malloc(n);
26473}
26474
26475/*
26476** TRUE if p is a lookaside memory allocation from db
26477*/
26478#ifndef SQLITE_OMIT_LOOKASIDE
26479static int isLookaside(sqlite3 *db, void *p){
26480 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
26481}
26482#else
26483#define isLookaside(A,B) 0
26484#endif
26485
26486/*
26487** Return the size of a memory allocation previously obtained from
26488** sqlite3Malloc() or sqlite3_malloc().
26489*/
26490SQLITE_PRIVATE int sqlite3MallocSize(void *p){
26491 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
26492 return sqlite3GlobalConfig.m.xSize(p);
26493}
26494SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
26495 assert( p!=0 );
26496 if( db==0 || !isLookaside(db,p) ){
26497#ifdef SQLITE_DEBUG
26498 if( db==0 ){
26499 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
26500 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
26501 }else{
26502 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
26503 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
26504 }
26505#endif
26506 return sqlite3GlobalConfig.m.xSize(p);
26507 }else{
26508 assert( sqlite3_mutex_held(db->mutex) );
26509 return db->lookaside.sz;
26510 }
26511}
26512SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
26513 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
26514 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
26515 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
26516}
26517
26518/*
26519** Free memory previously obtained from sqlite3Malloc().
26520*/
26521SQLITE_API void sqlite3_free(void *p){
26522 if( p==0 ) return; /* IMP: R-49053-54554 */
26523 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
26524 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
26525 if( sqlite3GlobalConfig.bMemstat ){
26526 sqlite3_mutex_enter(mem0.mutex);
26527 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
26528 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
26529 sqlite3GlobalConfig.m.xFree(p);
26530 sqlite3_mutex_leave(mem0.mutex);
26531 }else{
26532 sqlite3GlobalConfig.m.xFree(p);
26533 }
26534}
26535
26536/*
26537** Add the size of memory allocation "p" to the count in
26538** *db->pnBytesFreed.
26539*/
26540static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
26541 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
26542}
26543
26544/*
26545** Free memory that might be associated with a particular database
26546** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
26547** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
26548*/
26549SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3 *db, void *p){
26550 assert( db==0 || sqlite3_mutex_held(db->mutex) );
26551 assert( p!=0 );
26552 if( db ){
26553 if( db->pnBytesFreed ){
26554 measureAllocationSize(db, p);
26555 return;
26556 }
26557 if( isLookaside(db, p) ){
26558 LookasideSlot *pBuf = (LookasideSlot*)p;
26559#ifdef SQLITE_DEBUG
26560 /* Trash all content in the buffer being freed */
26561 memset(p, 0xaa, db->lookaside.sz);
26562#endif
26563 pBuf->pNext = db->lookaside.pFree;
26564 db->lookaside.pFree = pBuf;
26565 return;
26566 }
26567 }
26568 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
26569 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
26570 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
26571 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
26572 sqlite3_free(p);
26573}
26574SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
26575 assert( db==0 || sqlite3_mutex_held(db->mutex) );
26576 if( p ) sqlite3DbFreeNN(db, p);
26577}
26578
26579/*
26580** Change the size of an existing memory allocation
26581*/
26582SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
26583 int nOld, nNew, nDiff;
26584 void *pNew;
26585 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
26586 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
26587 if( pOld==0 ){
26588 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
26589 }
26590 if( nBytes==0 ){
26591 sqlite3_free(pOld); /* IMP: R-26507-47431 */
26592 return 0;
26593 }
26594 if( nBytes>=0x7fffff00 ){
26595 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
26596 return 0;
26597 }
26598 nOld = sqlite3MallocSize(pOld);
26599 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
26600 ** argument to xRealloc is always a value returned by a prior call to
26601 ** xRoundup. */
26602 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
26603 if( nOld==nNew ){
26604 pNew = pOld;
26605 }else if( sqlite3GlobalConfig.bMemstat ){
26606 sqlite3_mutex_enter(mem0.mutex);
26607 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
26608 nDiff = nNew - nOld;
26609 if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
26610 mem0.alarmThreshold-nDiff ){
26611 sqlite3MallocAlarm(nDiff);
26612 }
26613 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
26614 if( pNew==0 && mem0.alarmThreshold>0 ){
26615 sqlite3MallocAlarm((int)nBytes);
26616 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
26617 }
26618 if( pNew ){
26619 nNew = sqlite3MallocSize(pNew);
26620 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
26621 }
26622 sqlite3_mutex_leave(mem0.mutex);
26623 }else{
26624 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
26625 }
26626 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
26627 return pNew;
26628}
26629
26630/*
26631** The public interface to sqlite3Realloc. Make sure that the memory
26632** subsystem is initialized prior to invoking sqliteRealloc.
26633*/
26634SQLITE_API void *sqlite3_realloc(void *pOld, int n){
26635#ifndef SQLITE_OMIT_AUTOINIT
26636 if( sqlite3_initialize() ) return 0;
26637#endif
26638 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
26639 return sqlite3Realloc(pOld, n);
26640}
26641SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
26642#ifndef SQLITE_OMIT_AUTOINIT
26643 if( sqlite3_initialize() ) return 0;
26644#endif
26645 return sqlite3Realloc(pOld, n);
26646}
26647
26648
26649/*
26650** Allocate and zero memory.
26651*/
26652SQLITE_PRIVATE void *sqlite3MallocZero(u64 n){
26653 void *p = sqlite3Malloc(n);
26654 if( p ){
26655 memset(p, 0, (size_t)n);
26656 }
26657 return p;
26658}
26659
26660/*
26661** Allocate and zero memory. If the allocation fails, make
26662** the mallocFailed flag in the connection pointer.
26663*/
26664SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
26665 void *p;
26666 testcase( db==0 );
26667 p = sqlite3DbMallocRaw(db, n);
26668 if( p ) memset(p, 0, (size_t)n);
26669 return p;
26670}
26671
26672
26673/* Finish the work of sqlite3DbMallocRawNN for the unusual and
26674** slower case when the allocation cannot be fulfilled using lookaside.
26675*/
26676static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
26677 void *p;
26678 assert( db!=0 );
26679 p = sqlite3Malloc(n);
26680 if( !p ) sqlite3OomFault(db);
26681 sqlite3MemdebugSetType(p,
26682 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
26683 return p;
26684}
26685
26686/*
26687** Allocate memory, either lookaside (if possible) or heap.
26688** If the allocation fails, set the mallocFailed flag in
26689** the connection pointer.
26690**
26691** If db!=0 and db->mallocFailed is true (indicating a prior malloc
26692** failure on the same database connection) then always return 0.
26693** Hence for a particular database connection, once malloc starts
26694** failing, it fails consistently until mallocFailed is reset.
26695** This is an important assumption. There are many places in the
26696** code that do things like this:
26697**
26698** int *a = (int*)sqlite3DbMallocRaw(db, 100);
26699** int *b = (int*)sqlite3DbMallocRaw(db, 200);
26700** if( b ) a[10] = 9;
26701**
26702** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
26703** that all prior mallocs (ex: "a") worked too.
26704**
26705** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
26706** not a NULL pointer.
26707*/
26708SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
26709 void *p;
26710 if( db ) return sqlite3DbMallocRawNN(db, n);
26711 p = sqlite3Malloc(n);
26712 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
26713 return p;
26714}
26715SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
26716#ifndef SQLITE_OMIT_LOOKASIDE
26717 LookasideSlot *pBuf;
26718 assert( db!=0 );
26719 assert( sqlite3_mutex_held(db->mutex) );
26720 assert( db->pnBytesFreed==0 );
26721 if( db->lookaside.bDisable==0 ){
26722 assert( db->mallocFailed==0 );
26723 if( n>db->lookaside.sz ){
26724 db->lookaside.anStat[1]++;
26725 }else if( (pBuf = db->lookaside.pFree)!=0 ){
26726 db->lookaside.pFree = pBuf->pNext;
26727 db->lookaside.anStat[0]++;
26728 return (void*)pBuf;
26729 }else if( (pBuf = db->lookaside.pInit)!=0 ){
26730 db->lookaside.pInit = pBuf->pNext;
26731 db->lookaside.anStat[0]++;
26732 return (void*)pBuf;
26733 }else{
26734 db->lookaside.anStat[2]++;
26735 }
26736 }else if( db->mallocFailed ){
26737 return 0;
26738 }
26739#else
26740 assert( db!=0 );
26741 assert( sqlite3_mutex_held(db->mutex) );
26742 assert( db->pnBytesFreed==0 );
26743 if( db->mallocFailed ){
26744 return 0;
26745 }
26746#endif
26747 return dbMallocRawFinish(db, n);
26748}
26749
26750/* Forward declaration */
26751static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
26752
26753/*
26754** Resize the block of memory pointed to by p to n bytes. If the
26755** resize fails, set the mallocFailed flag in the connection object.
26756*/
26757SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
26758 assert( db!=0 );
26759 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
26760 assert( sqlite3_mutex_held(db->mutex) );
26761 if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
26762 return dbReallocFinish(db, p, n);
26763}
26764static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
26765 void *pNew = 0;
26766 assert( db!=0 );
26767 assert( p!=0 );
26768 if( db->mallocFailed==0 ){
26769 if( isLookaside(db, p) ){
26770 pNew = sqlite3DbMallocRawNN(db, n);
26771 if( pNew ){
26772 memcpy(pNew, p, db->lookaside.sz);
26773 sqlite3DbFree(db, p);
26774 }
26775 }else{
26776 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
26777 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
26778 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
26779 pNew = sqlite3_realloc64(p, n);
26780 if( !pNew ){
26781 sqlite3OomFault(db);
26782 }
26783 sqlite3MemdebugSetType(pNew,
26784 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
26785 }
26786 }
26787 return pNew;
26788}
26789
26790/*
26791** Attempt to reallocate p. If the reallocation fails, then free p
26792** and set the mallocFailed flag in the database connection.
26793*/
26794SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
26795 void *pNew;
26796 pNew = sqlite3DbRealloc(db, p, n);
26797 if( !pNew ){
26798 sqlite3DbFree(db, p);
26799 }
26800 return pNew;
26801}
26802
26803/*
26804** Make a copy of a string in memory obtained from sqliteMalloc(). These
26805** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
26806** is because when memory debugging is turned on, these two functions are
26807** called via macros that record the current file and line number in the
26808** ThreadData structure.
26809*/
26810SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
26811 char *zNew;
26812 size_t n;
26813 if( z==0 ){
26814 return 0;
26815 }
26816 n = strlen(z) + 1;
26817 zNew = sqlite3DbMallocRaw(db, n);
26818 if( zNew ){
26819 memcpy(zNew, z, n);
26820 }
26821 return zNew;
26822}
26823SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
26824 char *zNew;
26825 assert( db!=0 );
26826 if( z==0 ){
26827 return 0;
26828 }
26829 assert( (n&0x7fffffff)==n );
26830 zNew = sqlite3DbMallocRawNN(db, n+1);
26831 if( zNew ){
26832 memcpy(zNew, z, (size_t)n);
26833 zNew[n] = 0;
26834 }
26835 return zNew;
26836}
26837
26838/*
26839** The text between zStart and zEnd represents a phrase within a larger
26840** SQL statement. Make a copy of this phrase in space obtained form
26841** sqlite3DbMalloc(). Omit leading and trailing whitespace.
26842*/
26843SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
26844 int n;
26845 while( sqlite3Isspace(zStart[0]) ) zStart++;
26846 n = (int)(zEnd - zStart);
26847 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
26848 return sqlite3DbStrNDup(db, zStart, n);
26849}
26850
26851/*
26852** Free any prior content in *pz and replace it with a copy of zNew.
26853*/
26854SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
26855 sqlite3DbFree(db, *pz);
26856 *pz = sqlite3DbStrDup(db, zNew);
26857}
26858
26859/*
26860** Call this routine to record the fact that an OOM (out-of-memory) error
26861** has happened. This routine will set db->mallocFailed, and also
26862** temporarily disable the lookaside memory allocator and interrupt
26863** any running VDBEs.
26864*/
26865SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){
26866 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
26867 db->mallocFailed = 1;
26868 if( db->nVdbeExec>0 ){
26869 db->u1.isInterrupted = 1;
26870 }
26871 db->lookaside.bDisable++;
26872 }
26873}
26874
26875/*
26876** This routine reactivates the memory allocator and clears the
26877** db->mallocFailed flag as necessary.
26878**
26879** The memory allocator is not restarted if there are running
26880** VDBEs.
26881*/
26882SQLITE_PRIVATE void sqlite3OomClear(sqlite3 *db){
26883 if( db->mallocFailed && db->nVdbeExec==0 ){
26884 db->mallocFailed = 0;
26885 db->u1.isInterrupted = 0;
26886 assert( db->lookaside.bDisable>0 );
26887 db->lookaside.bDisable--;
26888 }
26889}
26890
26891/*
26892** Take actions at the end of an API call to indicate an OOM error
26893*/
26894static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
26895 sqlite3OomClear(db);
26896 sqlite3Error(db, SQLITE_NOMEM);
26897 return SQLITE_NOMEM_BKPT;
26898}
26899
26900/*
26901** This function must be called before exiting any API function (i.e.
26902** returning control to the user) that has called sqlite3_malloc or
26903** sqlite3_realloc.
26904**
26905** The returned value is normally a copy of the second argument to this
26906** function. However, if a malloc() failure has occurred since the previous
26907** invocation SQLITE_NOMEM is returned instead.
26908**
26909** If an OOM as occurred, then the connection error-code (the value
26910** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
26911*/
26912SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
26913 /* If the db handle must hold the connection handle mutex here.
26914 ** Otherwise the read (and possible write) of db->mallocFailed
26915 ** is unsafe, as is the call to sqlite3Error().
26916 */
26917 assert( db!=0 );
26918 assert( sqlite3_mutex_held(db->mutex) );
26919 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
26920 return apiOomError(db);
26921 }
26922 return rc & db->errMask;
26923}
26924
26925/************** End of malloc.c **********************************************/
26926/************** Begin file printf.c ******************************************/
26927/*
26928** The "printf" code that follows dates from the 1980's. It is in
26929** the public domain.
26930**
26931**************************************************************************
26932**
26933** This file contains code for a set of "printf"-like routines. These
26934** routines format strings much like the printf() from the standard C
26935** library, though the implementation here has enhancements to support
26936** SQLite.
26937*/
26938/* #include "sqliteInt.h" */
26939
26940/*
26941** Conversion types fall into various categories as defined by the
26942** following enumeration.
26943*/
26944#define etRADIX 0 /* non-decimal integer types. %x %o */
26945#define etFLOAT 1 /* Floating point. %f */
26946#define etEXP 2 /* Exponentional notation. %e and %E */
26947#define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */
26948#define etSIZE 4 /* Return number of characters processed so far. %n */
26949#define etSTRING 5 /* Strings. %s */
26950#define etDYNSTRING 6 /* Dynamically allocated strings. %z */
26951#define etPERCENT 7 /* Percent symbol. %% */
26952#define etCHARX 8 /* Characters. %c */
26953/* The rest are extensions, not normally found in printf() */
26954#define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */
26955#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
26956 NULL pointers replaced by SQL NULL. %Q */
26957#define etTOKEN 11 /* a pointer to a Token structure */
26958#define etSRCLIST 12 /* a pointer to a SrcList */
26959#define etPOINTER 13 /* The %p conversion */
26960#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
26961#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
26962#define etDECIMAL 16 /* %d or %u, but not %x, %o */
26963
26964#define etINVALID 17 /* Any unrecognized conversion type */
26965
26966
26967/*
26968** An "etByte" is an 8-bit unsigned value.
26969*/
26970typedef unsigned char etByte;
26971
26972/*
26973** Each builtin conversion character (ex: the 'd' in "%d") is described
26974** by an instance of the following structure
26975*/
26976typedef struct et_info { /* Information about each format field */
26977 char fmttype; /* The format field code letter */
26978 etByte base; /* The base for radix conversion */
26979 etByte flags; /* One or more of FLAG_ constants below */
26980 etByte type; /* Conversion paradigm */
26981 etByte charset; /* Offset into aDigits[] of the digits string */
26982 etByte prefix; /* Offset into aPrefix[] of the prefix string */
26983} et_info;
26984
26985/*
26986** Allowed values for et_info.flags
26987*/
26988#define FLAG_SIGNED 1 /* True if the value to convert is signed */
26989#define FLAG_STRING 4 /* Allow infinite precision */
26990
26991
26992/*
26993** The following table is searched linearly, so it is good to put the
26994** most frequently used conversion types first.
26995*/
26996static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
26997static const char aPrefix[] = "-x0\000X0";
26998static const et_info fmtinfo[] = {
26999 { 'd', 10, 1, etDECIMAL, 0, 0 },
27000 { 's', 0, 4, etSTRING, 0, 0 },
27001 { 'g', 0, 1, etGENERIC, 30, 0 },
27002 { 'z', 0, 4, etDYNSTRING, 0, 0 },
27003 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
27004 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
27005 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
27006 { 'c', 0, 0, etCHARX, 0, 0 },
27007 { 'o', 8, 0, etRADIX, 0, 2 },
27008 { 'u', 10, 0, etDECIMAL, 0, 0 },
27009 { 'x', 16, 0, etRADIX, 16, 1 },
27010 { 'X', 16, 0, etRADIX, 0, 4 },
27011#ifndef SQLITE_OMIT_FLOATING_POINT
27012 { 'f', 0, 1, etFLOAT, 0, 0 },
27013 { 'e', 0, 1, etEXP, 30, 0 },
27014 { 'E', 0, 1, etEXP, 14, 0 },
27015 { 'G', 0, 1, etGENERIC, 14, 0 },
27016#endif
27017 { 'i', 10, 1, etDECIMAL, 0, 0 },
27018 { 'n', 0, 0, etSIZE, 0, 0 },
27019 { '%', 0, 0, etPERCENT, 0, 0 },
27020 { 'p', 16, 0, etPOINTER, 0, 1 },
27021
27022 /* All the rest are undocumented and are for internal use only */
27023 { 'T', 0, 0, etTOKEN, 0, 0 },
27024 { 'S', 0, 0, etSRCLIST, 0, 0 },
27025 { 'r', 10, 1, etORDINAL, 0, 0 },
27026};
27027
27028/*
27029** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
27030** conversions will work.
27031*/
27032#ifndef SQLITE_OMIT_FLOATING_POINT
27033/*
27034** "*val" is a double such that 0.1 <= *val < 10.0
27035** Return the ascii code for the leading digit of *val, then
27036** multiply "*val" by 10.0 to renormalize.
27037**
27038** Example:
27039** input: *val = 3.14159
27040** output: *val = 1.4159 function return = '3'
27041**
27042** The counter *cnt is incremented each time. After counter exceeds
27043** 16 (the number of significant digits in a 64-bit float) '0' is
27044** always returned.
27045*/
27046static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
27047 int digit;
27048 LONGDOUBLE_TYPE d;
27049 if( (*cnt)<=0 ) return '0';
27050 (*cnt)--;
27051 digit = (int)*val;
27052 d = digit;
27053 digit += '0';
27054 *val = (*val - d)*10.0;
27055 return (char)digit;
27056}
27057#endif /* SQLITE_OMIT_FLOATING_POINT */
27058
27059/*
27060** Set the StrAccum object to an error mode.
27061*/
27062static void setStrAccumError(StrAccum *p, u8 eError){
27063 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
27064 p->accError = eError;
27065 p->nAlloc = 0;
27066}
27067
27068/*
27069** Extra argument values from a PrintfArguments object
27070*/
27071static sqlite3_int64 getIntArg(PrintfArguments *p){
27072 if( p->nArg<=p->nUsed ) return 0;
27073 return sqlite3_value_int64(p->apArg[p->nUsed++]);
27074}
27075static double getDoubleArg(PrintfArguments *p){
27076 if( p->nArg<=p->nUsed ) return 0.0;
27077 return sqlite3_value_double(p->apArg[p->nUsed++]);
27078}
27079static char *getTextArg(PrintfArguments *p){
27080 if( p->nArg<=p->nUsed ) return 0;
27081 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
27082}
27083
27084
27085/*
27086** On machines with a small stack size, you can redefine the
27087** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
27088*/
27089#ifndef SQLITE_PRINT_BUF_SIZE
27090# define SQLITE_PRINT_BUF_SIZE 70
27091#endif
27092#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
27093
27094/*
27095** Render a string given by "fmt" into the StrAccum object.
27096*/
27097SQLITE_API void sqlite3_str_vappendf(
27098 sqlite3_str *pAccum, /* Accumulate results here */
27099 const char *fmt, /* Format string */
27100 va_list ap /* arguments */
27101){
27102 int c; /* Next character in the format string */
27103 char *bufpt; /* Pointer to the conversion buffer */
27104 int precision; /* Precision of the current field */
27105 int length; /* Length of the field */
27106 int idx; /* A general purpose loop counter */
27107 int width; /* Width of the current field */
27108 etByte flag_leftjustify; /* True if "-" flag is present */
27109 etByte flag_prefix; /* '+' or ' ' or 0 for prefix */
27110 etByte flag_alternateform; /* True if "#" flag is present */
27111 etByte flag_altform2; /* True if "!" flag is present */
27112 etByte flag_zeropad; /* True if field width constant starts with zero */
27113 etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */
27114 etByte done; /* Loop termination flag */
27115 etByte cThousand; /* Thousands separator for %d and %u */
27116 etByte xtype = etINVALID; /* Conversion paradigm */
27117 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
27118 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
27119 sqlite_uint64 longvalue; /* Value for integer types */
27120 LONGDOUBLE_TYPE realvalue; /* Value for real types */
27121 const et_info *infop; /* Pointer to the appropriate info structure */
27122 char *zOut; /* Rendering buffer */
27123 int nOut; /* Size of the rendering buffer */
27124 char *zExtra = 0; /* Malloced memory used by some conversion */
27125#ifndef SQLITE_OMIT_FLOATING_POINT
27126 int exp, e2; /* exponent of real numbers */
27127 int nsd; /* Number of significant digits returned */
27128 double rounder; /* Used for rounding floating point values */
27129 etByte flag_dp; /* True if decimal point should be shown */
27130 etByte flag_rtz; /* True if trailing zeros should be removed */
27131#endif
27132 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
27133 char buf[etBUFSIZE]; /* Conversion buffer */
27134
27135 /* pAccum never starts out with an empty buffer that was obtained from
27136 ** malloc(). This precondition is required by the mprintf("%z...")
27137 ** optimization. */
27138 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
27139
27140 bufpt = 0;
27141 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
27142 pArgList = va_arg(ap, PrintfArguments*);
27143 bArgList = 1;
27144 }else{
27145 bArgList = 0;
27146 }
27147 for(; (c=(*fmt))!=0; ++fmt){
27148 if( c!='%' ){
27149 bufpt = (char *)fmt;
27150#if HAVE_STRCHRNUL
27151 fmt = strchrnul(fmt, '%');
27152#else
27153 do{ fmt++; }while( *fmt && *fmt != '%' );
27154#endif
27155 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
27156 if( *fmt==0 ) break;
27157 }
27158 if( (c=(*++fmt))==0 ){
27159 sqlite3_str_append(pAccum, "%", 1);
27160 break;
27161 }
27162 /* Find out what flags are present */
27163 flag_leftjustify = flag_prefix = cThousand =
27164 flag_alternateform = flag_altform2 = flag_zeropad = 0;
27165 done = 0;
27166 do{
27167 switch( c ){
27168 case '-': flag_leftjustify = 1; break;
27169 case '+': flag_prefix = '+'; break;
27170 case ' ': flag_prefix = ' '; break;
27171 case '#': flag_alternateform = 1; break;
27172 case '!': flag_altform2 = 1; break;
27173 case '0': flag_zeropad = 1; break;
27174 case ',': cThousand = ','; break;
27175 default: done = 1; break;
27176 }
27177 }while( !done && (c=(*++fmt))!=0 );
27178 /* Get the field width */
27179 if( c=='*' ){
27180 if( bArgList ){
27181 width = (int)getIntArg(pArgList);
27182 }else{
27183 width = va_arg(ap,int);
27184 }
27185 if( width<0 ){
27186 flag_leftjustify = 1;
27187 width = width >= -2147483647 ? -width : 0;
27188 }
27189 c = *++fmt;
27190 }else{
27191 unsigned wx = 0;
27192 while( c>='0' && c<='9' ){
27193 wx = wx*10 + c - '0';
27194 c = *++fmt;
27195 }
27196 testcase( wx>0x7fffffff );
27197 width = wx & 0x7fffffff;
27198 }
27199 assert( width>=0 );
27200#ifdef SQLITE_PRINTF_PRECISION_LIMIT
27201 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
27202 width = SQLITE_PRINTF_PRECISION_LIMIT;
27203 }
27204#endif
27205
27206 /* Get the precision */
27207 if( c=='.' ){
27208 c = *++fmt;
27209 if( c=='*' ){
27210 if( bArgList ){
27211 precision = (int)getIntArg(pArgList);
27212 }else{
27213 precision = va_arg(ap,int);
27214 }
27215 c = *++fmt;
27216 if( precision<0 ){
27217 precision = precision >= -2147483647 ? -precision : -1;
27218 }
27219 }else{
27220 unsigned px = 0;
27221 while( c>='0' && c<='9' ){
27222 px = px*10 + c - '0';
27223 c = *++fmt;
27224 }
27225 testcase( px>0x7fffffff );
27226 precision = px & 0x7fffffff;
27227 }
27228 }else{
27229 precision = -1;
27230 }
27231 assert( precision>=(-1) );
27232#ifdef SQLITE_PRINTF_PRECISION_LIMIT
27233 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
27234 precision = SQLITE_PRINTF_PRECISION_LIMIT;
27235 }
27236#endif
27237
27238
27239 /* Get the conversion type modifier */
27240 if( c=='l' ){
27241 flag_long = 1;
27242 c = *++fmt;
27243 if( c=='l' ){
27244 flag_long = 2;
27245 c = *++fmt;
27246 }
27247 }else{
27248 flag_long = 0;
27249 }
27250 /* Fetch the info entry for the field */
27251 infop = &fmtinfo[0];
27252 xtype = etINVALID;
27253 for(idx=0; idx<ArraySize(fmtinfo); idx++){
27254 if( c==fmtinfo[idx].fmttype ){
27255 infop = &fmtinfo[idx];
27256 xtype = infop->type;
27257 break;
27258 }
27259 }
27260
27261 /*
27262 ** At this point, variables are initialized as follows:
27263 **
27264 ** flag_alternateform TRUE if a '#' is present.
27265 ** flag_altform2 TRUE if a '!' is present.
27266 ** flag_prefix '+' or ' ' or zero
27267 ** flag_leftjustify TRUE if a '-' is present or if the
27268 ** field width was negative.
27269 ** flag_zeropad TRUE if the width began with 0.
27270 ** flag_long 1 for "l", 2 for "ll"
27271 ** width The specified field width. This is
27272 ** always non-negative. Zero is the default.
27273 ** precision The specified precision. The default
27274 ** is -1.
27275 ** xtype The class of the conversion.
27276 ** infop Pointer to the appropriate info struct.
27277 */
27278 switch( xtype ){
27279 case etPOINTER:
27280 flag_long = sizeof(char*)==sizeof(i64) ? 2 :
27281 sizeof(char*)==sizeof(long int) ? 1 : 0;
27282 /* Fall through into the next case */
27283 case etORDINAL:
27284 case etRADIX:
27285 cThousand = 0;
27286 /* Fall through into the next case */
27287 case etDECIMAL:
27288 if( infop->flags & FLAG_SIGNED ){
27289 i64 v;
27290 if( bArgList ){
27291 v = getIntArg(pArgList);
27292 }else if( flag_long ){
27293 if( flag_long==2 ){
27294 v = va_arg(ap,i64) ;
27295 }else{
27296 v = va_arg(ap,long int);
27297 }
27298 }else{
27299 v = va_arg(ap,int);
27300 }
27301 if( v<0 ){
27302 if( v==SMALLEST_INT64 ){
27303 longvalue = ((u64)1)<<63;
27304 }else{
27305 longvalue = -v;
27306 }
27307 prefix = '-';
27308 }else{
27309 longvalue = v;
27310 prefix = flag_prefix;
27311 }
27312 }else{
27313 if( bArgList ){
27314 longvalue = (u64)getIntArg(pArgList);
27315 }else if( flag_long ){
27316 if( flag_long==2 ){
27317 longvalue = va_arg(ap,u64);
27318 }else{
27319 longvalue = va_arg(ap,unsigned long int);
27320 }
27321 }else{
27322 longvalue = va_arg(ap,unsigned int);
27323 }
27324 prefix = 0;
27325 }
27326 if( longvalue==0 ) flag_alternateform = 0;
27327 if( flag_zeropad && precision<width-(prefix!=0) ){
27328 precision = width-(prefix!=0);
27329 }
27330 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
27331 nOut = etBUFSIZE;
27332 zOut = buf;
27333 }else{
27334 u64 n = (u64)precision + 10 + precision/3;
27335 zOut = zExtra = sqlite3Malloc( n );
27336 if( zOut==0 ){
27337 setStrAccumError(pAccum, SQLITE_NOMEM);
27338 return;
27339 }
27340 nOut = (int)n;
27341 }
27342 bufpt = &zOut[nOut-1];
27343 if( xtype==etORDINAL ){
27344 static const char zOrd[] = "thstndrd";
27345 int x = (int)(longvalue % 10);
27346 if( x>=4 || (longvalue/10)%10==1 ){
27347 x = 0;
27348 }
27349 *(--bufpt) = zOrd[x*2+1];
27350 *(--bufpt) = zOrd[x*2];
27351 }
27352 {
27353 const char *cset = &aDigits[infop->charset];
27354 u8 base = infop->base;
27355 do{ /* Convert to ascii */
27356 *(--bufpt) = cset[longvalue%base];
27357 longvalue = longvalue/base;
27358 }while( longvalue>0 );
27359 }
27360 length = (int)(&zOut[nOut-1]-bufpt);
27361 while( precision>length ){
27362 *(--bufpt) = '0'; /* Zero pad */
27363 length++;
27364 }
27365 if( cThousand ){
27366 int nn = (length - 1)/3; /* Number of "," to insert */
27367 int ix = (length - 1)%3 + 1;
27368 bufpt -= nn;
27369 for(idx=0; nn>0; idx++){
27370 bufpt[idx] = bufpt[idx+nn];
27371 ix--;
27372 if( ix==0 ){
27373 bufpt[++idx] = cThousand;
27374 nn--;
27375 ix = 3;
27376 }
27377 }
27378 }
27379 if( prefix ) *(--bufpt) = prefix; /* Add sign */
27380 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
27381 const char *pre;
27382 char x;
27383 pre = &aPrefix[infop->prefix];
27384 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
27385 }
27386 length = (int)(&zOut[nOut-1]-bufpt);
27387 break;
27388 case etFLOAT:
27389 case etEXP:
27390 case etGENERIC:
27391 if( bArgList ){
27392 realvalue = getDoubleArg(pArgList);
27393 }else{
27394 realvalue = va_arg(ap,double);
27395 }
27396#ifdef SQLITE_OMIT_FLOATING_POINT
27397 length = 0;
27398#else
27399 if( precision<0 ) precision = 6; /* Set default precision */
27400 if( realvalue<0.0 ){
27401 realvalue = -realvalue;
27402 prefix = '-';
27403 }else{
27404 prefix = flag_prefix;
27405 }
27406 if( xtype==etGENERIC && precision>0 ) precision--;
27407 testcase( precision>0xfff );
27408 for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){}
27409 if( xtype==etFLOAT ) realvalue += rounder;
27410 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
27411 exp = 0;
27412 if( sqlite3IsNaN((double)realvalue) ){
27413 bufpt = "NaN";
27414 length = 3;
27415 break;
27416 }
27417 if( realvalue>0.0 ){
27418 LONGDOUBLE_TYPE scale = 1.0;
27419 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
27420 while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; }
27421 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
27422 realvalue /= scale;
27423 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
27424 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
27425 if( exp>350 ){
27426 bufpt = buf;
27427 buf[0] = prefix;
27428 memcpy(buf+(prefix!=0),"Inf",4);
27429 length = 3+(prefix!=0);
27430 break;
27431 }
27432 }
27433 bufpt = buf;
27434 /*
27435 ** If the field type is etGENERIC, then convert to either etEXP
27436 ** or etFLOAT, as appropriate.
27437 */
27438 if( xtype!=etFLOAT ){
27439 realvalue += rounder;
27440 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
27441 }
27442 if( xtype==etGENERIC ){
27443 flag_rtz = !flag_alternateform;
27444 if( exp<-4 || exp>precision ){
27445 xtype = etEXP;
27446 }else{
27447 precision = precision - exp;
27448 xtype = etFLOAT;
27449 }
27450 }else{
27451 flag_rtz = flag_altform2;
27452 }
27453 if( xtype==etEXP ){
27454 e2 = 0;
27455 }else{
27456 e2 = exp;
27457 }
27458 if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
27459 bufpt = zExtra
27460 = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 );
27461 if( bufpt==0 ){
27462 setStrAccumError(pAccum, SQLITE_NOMEM);
27463 return;
27464 }
27465 }
27466 zOut = bufpt;
27467 nsd = 16 + flag_altform2*10;
27468 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
27469 /* The sign in front of the number */
27470 if( prefix ){
27471 *(bufpt++) = prefix;
27472 }
27473 /* Digits prior to the decimal point */
27474 if( e2<0 ){
27475 *(bufpt++) = '0';
27476 }else{
27477 for(; e2>=0; e2--){
27478 *(bufpt++) = et_getdigit(&realvalue,&nsd);
27479 }
27480 }
27481 /* The decimal point */
27482 if( flag_dp ){
27483 *(bufpt++) = '.';
27484 }
27485 /* "0" digits after the decimal point but before the first
27486 ** significant digit of the number */
27487 for(e2++; e2<0; precision--, e2++){
27488 assert( precision>0 );
27489 *(bufpt++) = '0';
27490 }
27491 /* Significant digits after the decimal point */
27492 while( (precision--)>0 ){
27493 *(bufpt++) = et_getdigit(&realvalue,&nsd);
27494 }
27495 /* Remove trailing zeros and the "." if no digits follow the "." */
27496 if( flag_rtz && flag_dp ){
27497 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
27498 assert( bufpt>zOut );
27499 if( bufpt[-1]=='.' ){
27500 if( flag_altform2 ){
27501 *(bufpt++) = '0';
27502 }else{
27503 *(--bufpt) = 0;
27504 }
27505 }
27506 }
27507 /* Add the "eNNN" suffix */
27508 if( xtype==etEXP ){
27509 *(bufpt++) = aDigits[infop->charset];
27510 if( exp<0 ){
27511 *(bufpt++) = '-'; exp = -exp;
27512 }else{
27513 *(bufpt++) = '+';
27514 }
27515 if( exp>=100 ){
27516 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
27517 exp %= 100;
27518 }
27519 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
27520 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
27521 }
27522 *bufpt = 0;
27523
27524 /* The converted number is in buf[] and zero terminated. Output it.
27525 ** Note that the number is in the usual order, not reversed as with
27526 ** integer conversions. */
27527 length = (int)(bufpt-zOut);
27528 bufpt = zOut;
27529
27530 /* Special case: Add leading zeros if the flag_zeropad flag is
27531 ** set and we are not left justified */
27532 if( flag_zeropad && !flag_leftjustify && length < width){
27533 int i;
27534 int nPad = width - length;
27535 for(i=width; i>=nPad; i--){
27536 bufpt[i] = bufpt[i-nPad];
27537 }
27538 i = prefix!=0;
27539 while( nPad-- ) bufpt[i++] = '0';
27540 length = width;
27541 }
27542#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
27543 break;
27544 case etSIZE:
27545 if( !bArgList ){
27546 *(va_arg(ap,int*)) = pAccum->nChar;
27547 }
27548 length = width = 0;
27549 break;
27550 case etPERCENT:
27551 buf[0] = '%';
27552 bufpt = buf;
27553 length = 1;
27554 break;
27555 case etCHARX:
27556 if( bArgList ){
27557 bufpt = getTextArg(pArgList);
27558 length = 1;
27559 if( bufpt ){
27560 buf[0] = c = *(bufpt++);
27561 if( (c&0xc0)==0xc0 ){
27562 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
27563 buf[length++] = *(bufpt++);
27564 }
27565 }
27566 }else{
27567 buf[0] = 0;
27568 }
27569 }else{
27570 unsigned int ch = va_arg(ap,unsigned int);
27571 if( ch<0x00080 ){
27572 buf[0] = ch & 0xff;
27573 length = 1;
27574 }else if( ch<0x00800 ){
27575 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
27576 buf[1] = 0x80 + (u8)(ch & 0x3f);
27577 length = 2;
27578 }else if( ch<0x10000 ){
27579 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
27580 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
27581 buf[2] = 0x80 + (u8)(ch & 0x3f);
27582 length = 3;
27583 }else{
27584 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
27585 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
27586 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
27587 buf[3] = 0x80 + (u8)(ch & 0x3f);
27588 length = 4;
27589 }
27590 }
27591 if( precision>1 ){
27592 width -= precision-1;
27593 if( width>1 && !flag_leftjustify ){
27594 sqlite3_str_appendchar(pAccum, width-1, ' ');
27595 width = 0;
27596 }
27597 while( precision-- > 1 ){
27598 sqlite3_str_append(pAccum, buf, length);
27599 }
27600 }
27601 bufpt = buf;
27602 flag_altform2 = 1;
27603 goto adjust_width_for_utf8;
27604 case etSTRING:
27605 case etDYNSTRING:
27606 if( bArgList ){
27607 bufpt = getTextArg(pArgList);
27608 xtype = etSTRING;
27609 }else{
27610 bufpt = va_arg(ap,char*);
27611 }
27612 if( bufpt==0 ){
27613 bufpt = "";
27614 }else if( xtype==etDYNSTRING ){
27615 if( pAccum->nChar==0
27616 && pAccum->mxAlloc
27617 && width==0
27618 && precision<0
27619 && pAccum->accError==0
27620 ){
27621 /* Special optimization for sqlite3_mprintf("%z..."):
27622 ** Extend an existing memory allocation rather than creating
27623 ** a new one. */
27624 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
27625 pAccum->zText = bufpt;
27626 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
27627 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
27628 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
27629 length = 0;
27630 break;
27631 }
27632 zExtra = bufpt;
27633 }
27634 if( precision>=0 ){
27635 if( flag_altform2 ){
27636 /* Set length to the number of bytes needed in order to display
27637 ** precision characters */
27638 unsigned char *z = (unsigned char*)bufpt;
27639 while( precision-- > 0 && z[0] ){
27640 SQLITE_SKIP_UTF8(z);
27641 }
27642 length = (int)(z - (unsigned char*)bufpt);
27643 }else{
27644 for(length=0; length<precision && bufpt[length]; length++){}
27645 }
27646 }else{
27647 length = 0x7fffffff & (int)strlen(bufpt);
27648 }
27649 adjust_width_for_utf8:
27650 if( flag_altform2 && width>0 ){
27651 /* Adjust width to account for extra bytes in UTF-8 characters */
27652 int ii = length - 1;
27653 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
27654 }
27655 break;
27656 case etSQLESCAPE: /* %q: Escape ' characters */
27657 case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
27658 case etSQLESCAPE3: { /* %w: Escape " characters */
27659 int i, j, k, n, isnull;
27660 int needQuote;
27661 char ch;
27662 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
27663 char *escarg;
27664
27665 if( bArgList ){
27666 escarg = getTextArg(pArgList);
27667 }else{
27668 escarg = va_arg(ap,char*);
27669 }
27670 isnull = escarg==0;
27671 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
27672 /* For %q, %Q, and %w, the precision is the number of byte (or
27673 ** characters if the ! flags is present) to use from the input.
27674 ** Because of the extra quoting characters inserted, the number
27675 ** of output characters may be larger than the precision.
27676 */
27677 k = precision;
27678 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
27679 if( ch==q ) n++;
27680 if( flag_altform2 && (ch&0xc0)==0xc0 ){
27681 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
27682 }
27683 }
27684 needQuote = !isnull && xtype==etSQLESCAPE2;
27685 n += i + 3;
27686 if( n>etBUFSIZE ){
27687 bufpt = zExtra = sqlite3Malloc( n );
27688 if( bufpt==0 ){
27689 setStrAccumError(pAccum, SQLITE_NOMEM);
27690 return;
27691 }
27692 }else{
27693 bufpt = buf;
27694 }
27695 j = 0;
27696 if( needQuote ) bufpt[j++] = q;
27697 k = i;
27698 for(i=0; i<k; i++){
27699 bufpt[j++] = ch = escarg[i];
27700 if( ch==q ) bufpt[j++] = ch;
27701 }
27702 if( needQuote ) bufpt[j++] = q;
27703 bufpt[j] = 0;
27704 length = j;
27705 goto adjust_width_for_utf8;
27706 }
27707 case etTOKEN: {
27708 Token *pToken;
27709 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
27710 pToken = va_arg(ap, Token*);
27711 assert( bArgList==0 );
27712 if( pToken && pToken->n ){
27713 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
27714 }
27715 length = width = 0;
27716 break;
27717 }
27718 case etSRCLIST: {
27719 SrcList *pSrc;
27720 int k;
27721 struct SrcList_item *pItem;
27722 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
27723 pSrc = va_arg(ap, SrcList*);
27724 k = va_arg(ap, int);
27725 pItem = &pSrc->a[k];
27726 assert( bArgList==0 );
27727 assert( k>=0 && k<pSrc->nSrc );
27728 if( pItem->zDatabase ){
27729 sqlite3_str_appendall(pAccum, pItem->zDatabase);
27730 sqlite3_str_append(pAccum, ".", 1);
27731 }
27732 sqlite3_str_appendall(pAccum, pItem->zName);
27733 length = width = 0;
27734 break;
27735 }
27736 default: {
27737 assert( xtype==etINVALID );
27738 return;
27739 }
27740 }/* End switch over the format type */
27741 /*
27742 ** The text of the conversion is pointed to by "bufpt" and is
27743 ** "length" characters long. The field width is "width". Do
27744 ** the output. Both length and width are in bytes, not characters,
27745 ** at this point. If the "!" flag was present on string conversions
27746 ** indicating that width and precision should be expressed in characters,
27747 ** then the values have been translated prior to reaching this point.
27748 */
27749 width -= length;
27750 if( width>0 ){
27751 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
27752 sqlite3_str_append(pAccum, bufpt, length);
27753 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
27754 }else{
27755 sqlite3_str_append(pAccum, bufpt, length);
27756 }
27757
27758 if( zExtra ){
27759 sqlite3DbFree(pAccum->db, zExtra);
27760 zExtra = 0;
27761 }
27762 }/* End for loop over the format string */
27763} /* End of function */
27764
27765/*
27766** Enlarge the memory allocation on a StrAccum object so that it is
27767** able to accept at least N more bytes of text.
27768**
27769** Return the number of bytes of text that StrAccum is able to accept
27770** after the attempted enlargement. The value returned might be zero.
27771*/
27772static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
27773 char *zNew;
27774 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
27775 if( p->accError ){
27776 testcase(p->accError==SQLITE_TOOBIG);
27777 testcase(p->accError==SQLITE_NOMEM);
27778 return 0;
27779 }
27780 if( p->mxAlloc==0 ){
27781 N = p->nAlloc - p->nChar - 1;
27782 setStrAccumError(p, SQLITE_TOOBIG);
27783 return N;
27784 }else{
27785 char *zOld = isMalloced(p) ? p->zText : 0;
27786 i64 szNew = p->nChar;
27787 szNew += N + 1;
27788 if( szNew+p->nChar<=p->mxAlloc ){
27789 /* Force exponential buffer size growth as long as it does not overflow,
27790 ** to avoid having to call this routine too often */
27791 szNew += p->nChar;
27792 }
27793 if( szNew > p->mxAlloc ){
27794 sqlite3_str_reset(p);
27795 setStrAccumError(p, SQLITE_TOOBIG);
27796 return 0;
27797 }else{
27798 p->nAlloc = (int)szNew;
27799 }
27800 if( p->db ){
27801 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
27802 }else{
27803 zNew = sqlite3_realloc64(zOld, p->nAlloc);
27804 }
27805 if( zNew ){
27806 assert( p->zText!=0 || p->nChar==0 );
27807 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
27808 p->zText = zNew;
27809 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
27810 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
27811 }else{
27812 sqlite3_str_reset(p);
27813 setStrAccumError(p, SQLITE_NOMEM);
27814 return 0;
27815 }
27816 }
27817 return N;
27818}
27819
27820/*
27821** Append N copies of character c to the given string buffer.
27822*/
27823SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
27824 testcase( p->nChar + (i64)N > 0x7fffffff );
27825 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
27826 return;
27827 }
27828 while( (N--)>0 ) p->zText[p->nChar++] = c;
27829}
27830
27831/*
27832** The StrAccum "p" is not large enough to accept N new bytes of z[].
27833** So enlarge if first, then do the append.
27834**
27835** This is a helper routine to sqlite3_str_append() that does special-case
27836** work (enlarging the buffer) using tail recursion, so that the
27837** sqlite3_str_append() routine can use fast calling semantics.
27838*/
27839static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
27840 N = sqlite3StrAccumEnlarge(p, N);
27841 if( N>0 ){
27842 memcpy(&p->zText[p->nChar], z, N);
27843 p->nChar += N;
27844 }
27845}
27846
27847/*
27848** Append N bytes of text from z to the StrAccum object. Increase the
27849** size of the memory allocation for StrAccum if necessary.
27850*/
27851SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
27852 assert( z!=0 || N==0 );
27853 assert( p->zText!=0 || p->nChar==0 || p->accError );
27854 assert( N>=0 );
27855 assert( p->accError==0 || p->nAlloc==0 );
27856 if( p->nChar+N >= p->nAlloc ){
27857 enlargeAndAppend(p,z,N);
27858 }else if( N ){
27859 assert( p->zText );
27860 p->nChar += N;
27861 memcpy(&p->zText[p->nChar-N], z, N);
27862 }
27863}
27864
27865/*
27866** Append the complete text of zero-terminated string z[] to the p string.
27867*/
27868SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){
27869 sqlite3_str_append(p, z, sqlite3Strlen30(z));
27870}
27871
27872
27873/*
27874** Finish off a string by making sure it is zero-terminated.
27875** Return a pointer to the resulting string. Return a NULL
27876** pointer if any kind of error was encountered.
27877*/
27878static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
27879 char *zText;
27880 assert( p->mxAlloc>0 && !isMalloced(p) );
27881 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
27882 if( zText ){
27883 memcpy(zText, p->zText, p->nChar+1);
27884 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
27885 }else{
27886 setStrAccumError(p, SQLITE_NOMEM);
27887 }
27888 p->zText = zText;
27889 return zText;
27890}
27891SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
27892 if( p->zText ){
27893 p->zText[p->nChar] = 0;
27894 if( p->mxAlloc>0 && !isMalloced(p) ){
27895 return strAccumFinishRealloc(p);
27896 }
27897 }
27898 return p->zText;
27899}
27900
27901/*
27902** This singleton is an sqlite3_str object that is returned if
27903** sqlite3_malloc() fails to provide space for a real one. This
27904** sqlite3_str object accepts no new text and always returns
27905** an SQLITE_NOMEM error.
27906*/
27907static sqlite3_str sqlite3OomStr = {
27908 0, 0, 0, 0, 0, SQLITE_NOMEM, 0
27909};
27910
27911/* Finalize a string created using sqlite3_str_new().
27912*/
27913SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
27914 char *z;
27915 if( p!=0 && p!=&sqlite3OomStr ){
27916 z = sqlite3StrAccumFinish(p);
27917 sqlite3_free(p);
27918 }else{
27919 z = 0;
27920 }
27921 return z;
27922}
27923
27924/* Return any error code associated with p */
27925SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){
27926 return p ? p->accError : SQLITE_NOMEM;
27927}
27928
27929/* Return the current length of p in bytes */
27930SQLITE_API int sqlite3_str_length(sqlite3_str *p){
27931 return p ? p->nChar : 0;
27932}
27933
27934/* Return the current value for p */
27935SQLITE_API char *sqlite3_str_value(sqlite3_str *p){
27936 if( p==0 || p->nChar==0 ) return 0;
27937 p->zText[p->nChar] = 0;
27938 return p->zText;
27939}
27940
27941/*
27942** Reset an StrAccum string. Reclaim all malloced memory.
27943*/
27944SQLITE_API void sqlite3_str_reset(StrAccum *p){
27945 if( isMalloced(p) ){
27946 sqlite3DbFree(p->db, p->zText);
27947 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
27948 }
27949 p->nAlloc = 0;
27950 p->nChar = 0;
27951 p->zText = 0;
27952}
27953
27954/*
27955** Initialize a string accumulator.
27956**
27957** p: The accumulator to be initialized.
27958** db: Pointer to a database connection. May be NULL. Lookaside
27959** memory is used if not NULL. db->mallocFailed is set appropriately
27960** when not NULL.
27961** zBase: An initial buffer. May be NULL in which case the initial buffer
27962** is malloced.
27963** n: Size of zBase in bytes. If total space requirements never exceed
27964** n then no memory allocations ever occur.
27965** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
27966** allocations will ever occur.
27967*/
27968SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
27969 p->zText = zBase;
27970 p->db = db;
27971 p->nAlloc = n;
27972 p->mxAlloc = mx;
27973 p->nChar = 0;
27974 p->accError = 0;
27975 p->printfFlags = 0;
27976}
27977
27978/* Allocate and initialize a new dynamic string object */
27979SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
27980 sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
27981 if( p ){
27982 sqlite3StrAccumInit(p, 0, 0, 0,
27983 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
27984 }else{
27985 p = &sqlite3OomStr;
27986 }
27987 return p;
27988}
27989
27990/*
27991** Print into memory obtained from sqliteMalloc(). Use the internal
27992** %-conversion extensions.
27993*/
27994SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
27995 char *z;
27996 char zBase[SQLITE_PRINT_BUF_SIZE];
27997 StrAccum acc;
27998 assert( db!=0 );
27999 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
28000 db->aLimit[SQLITE_LIMIT_LENGTH]);
28001 acc.printfFlags = SQLITE_PRINTF_INTERNAL;
28002 sqlite3_str_vappendf(&acc, zFormat, ap);
28003 z = sqlite3StrAccumFinish(&acc);
28004 if( acc.accError==SQLITE_NOMEM ){
28005 sqlite3OomFault(db);
28006 }
28007 return z;
28008}
28009
28010/*
28011** Print into memory obtained from sqliteMalloc(). Use the internal
28012** %-conversion extensions.
28013*/
28014SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
28015 va_list ap;
28016 char *z;
28017 va_start(ap, zFormat);
28018 z = sqlite3VMPrintf(db, zFormat, ap);
28019 va_end(ap);
28020 return z;
28021}
28022
28023/*
28024** Print into memory obtained from sqlite3_malloc(). Omit the internal
28025** %-conversion extensions.
28026*/
28027SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
28028 char *z;
28029 char zBase[SQLITE_PRINT_BUF_SIZE];
28030 StrAccum acc;
28031
28032#ifdef SQLITE_ENABLE_API_ARMOR
28033 if( zFormat==0 ){
28034 (void)SQLITE_MISUSE_BKPT;
28035 return 0;
28036 }
28037#endif
28038#ifndef SQLITE_OMIT_AUTOINIT
28039 if( sqlite3_initialize() ) return 0;
28040#endif
28041 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
28042 sqlite3_str_vappendf(&acc, zFormat, ap);
28043 z = sqlite3StrAccumFinish(&acc);
28044 return z;
28045}
28046
28047/*
28048** Print into memory obtained from sqlite3_malloc()(). Omit the internal
28049** %-conversion extensions.
28050*/
28051SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
28052 va_list ap;
28053 char *z;
28054#ifndef SQLITE_OMIT_AUTOINIT
28055 if( sqlite3_initialize() ) return 0;
28056#endif
28057 va_start(ap, zFormat);
28058 z = sqlite3_vmprintf(zFormat, ap);
28059 va_end(ap);
28060 return z;
28061}
28062
28063/*
28064** sqlite3_snprintf() works like snprintf() except that it ignores the
28065** current locale settings. This is important for SQLite because we
28066** are not able to use a "," as the decimal point in place of "." as
28067** specified by some locales.
28068**
28069** Oops: The first two arguments of sqlite3_snprintf() are backwards
28070** from the snprintf() standard. Unfortunately, it is too late to change
28071** this without breaking compatibility, so we just have to live with the
28072** mistake.
28073**
28074** sqlite3_vsnprintf() is the varargs version.
28075*/
28076SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
28077 StrAccum acc;
28078 if( n<=0 ) return zBuf;
28079#ifdef SQLITE_ENABLE_API_ARMOR
28080 if( zBuf==0 || zFormat==0 ) {
28081 (void)SQLITE_MISUSE_BKPT;
28082 if( zBuf ) zBuf[0] = 0;
28083 return zBuf;
28084 }
28085#endif
28086 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
28087 sqlite3_str_vappendf(&acc, zFormat, ap);
28088 zBuf[acc.nChar] = 0;
28089 return zBuf;
28090}
28091SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
28092 char *z;
28093 va_list ap;
28094 va_start(ap,zFormat);
28095 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
28096 va_end(ap);
28097 return z;
28098}
28099
28100/*
28101** This is the routine that actually formats the sqlite3_log() message.
28102** We house it in a separate routine from sqlite3_log() to avoid using
28103** stack space on small-stack systems when logging is disabled.
28104**
28105** sqlite3_log() must render into a static buffer. It cannot dynamically
28106** allocate memory because it might be called while the memory allocator
28107** mutex is held.
28108**
28109** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
28110** certain format characters (%q) or for very large precisions or widths.
28111** Care must be taken that any sqlite3_log() calls that occur while the
28112** memory mutex is held do not use these mechanisms.
28113*/
28114static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
28115 StrAccum acc; /* String accumulator */
28116 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
28117
28118 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
28119 sqlite3_str_vappendf(&acc, zFormat, ap);
28120 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
28121 sqlite3StrAccumFinish(&acc));
28122}
28123
28124/*
28125** Format and write a message to the log if logging is enabled.
28126*/
28127SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
28128 va_list ap; /* Vararg list */
28129 if( sqlite3GlobalConfig.xLog ){
28130 va_start(ap, zFormat);
28131 renderLogMsg(iErrCode, zFormat, ap);
28132 va_end(ap);
28133 }
28134}
28135
28136#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
28137/*
28138** A version of printf() that understands %lld. Used for debugging.
28139** The printf() built into some versions of windows does not understand %lld
28140** and segfaults if you give it a long long int.
28141*/
28142SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
28143 va_list ap;
28144 StrAccum acc;
28145 char zBuf[500];
28146 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
28147 va_start(ap,zFormat);
28148 sqlite3_str_vappendf(&acc, zFormat, ap);
28149 va_end(ap);
28150 sqlite3StrAccumFinish(&acc);
28151#ifdef SQLITE_OS_TRACE_PROC
28152 {
28153 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
28154 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
28155 }
28156#else
28157 fprintf(stdout,"%s", zBuf);
28158 fflush(stdout);
28159#endif
28160}
28161#endif
28162
28163
28164/*
28165** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
28166** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
28167*/
28168SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
28169 va_list ap;
28170 va_start(ap,zFormat);
28171 sqlite3_str_vappendf(p, zFormat, ap);
28172 va_end(ap);
28173}
28174
28175/************** End of printf.c **********************************************/
28176/************** Begin file treeview.c ****************************************/
28177/*
28178** 2015-06-08
28179**
28180** The author disclaims copyright to this source code. In place of
28181** a legal notice, here is a blessing:
28182**
28183** May you do good and not evil.
28184** May you find forgiveness for yourself and forgive others.
28185** May you share freely, never taking more than you give.
28186**
28187*************************************************************************
28188**
28189** This file contains C code to implement the TreeView debugging routines.
28190** These routines print a parse tree to standard output for debugging and
28191** analysis.
28192**
28193** The interfaces in this file is only available when compiling
28194** with SQLITE_DEBUG.
28195*/
28196/* #include "sqliteInt.h" */
28197#ifdef SQLITE_DEBUG
28198
28199/*
28200** Add a new subitem to the tree. The moreToFollow flag indicates that this
28201** is not the last item in the tree.
28202*/
28203static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
28204 if( p==0 ){
28205 p = sqlite3_malloc64( sizeof(*p) );
28206 if( p==0 ) return 0;
28207 memset(p, 0, sizeof(*p));
28208 }else{
28209 p->iLevel++;
28210 }
28211 assert( moreToFollow==0 || moreToFollow==1 );
28212 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
28213 return p;
28214}
28215
28216/*
28217** Finished with one layer of the tree
28218*/
28219static void sqlite3TreeViewPop(TreeView *p){
28220 if( p==0 ) return;
28221 p->iLevel--;
28222 if( p->iLevel<0 ) sqlite3_free(p);
28223}
28224
28225/*
28226** Generate a single line of output for the tree, with a prefix that contains
28227** all the appropriate tree lines
28228*/
28229static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
28230 va_list ap;
28231 int i;
28232 StrAccum acc;
28233 char zBuf[500];
28234 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
28235 if( p ){
28236 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
28237 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
28238 }
28239 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
28240 }
28241 if( zFormat!=0 ){
28242 va_start(ap, zFormat);
28243 sqlite3_str_vappendf(&acc, zFormat, ap);
28244 va_end(ap);
28245 assert( acc.nChar>0 );
28246 sqlite3_str_append(&acc, "\n", 1);
28247 }
28248 sqlite3StrAccumFinish(&acc);
28249 fprintf(stdout,"%s", zBuf);
28250 fflush(stdout);
28251}
28252
28253/*
28254** Shorthand for starting a new tree item that consists of a single label
28255*/
28256static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
28257 p = sqlite3TreeViewPush(p, moreFollows);
28258 sqlite3TreeViewLine(p, "%s", zLabel);
28259}
28260
28261/*
28262** Generate a human-readable description of a WITH clause.
28263*/
28264SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
28265 int i;
28266 if( pWith==0 ) return;
28267 if( pWith->nCte==0 ) return;
28268 if( pWith->pOuter ){
28269 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
28270 }else{
28271 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
28272 }
28273 if( pWith->nCte>0 ){
28274 pView = sqlite3TreeViewPush(pView, 1);
28275 for(i=0; i<pWith->nCte; i++){
28276 StrAccum x;
28277 char zLine[1000];
28278 const struct Cte *pCte = &pWith->a[i];
28279 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
28280 sqlite3_str_appendf(&x, "%s", pCte->zName);
28281 if( pCte->pCols && pCte->pCols->nExpr>0 ){
28282 char cSep = '(';
28283 int j;
28284 for(j=0; j<pCte->pCols->nExpr; j++){
28285 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
28286 cSep = ',';
28287 }
28288 sqlite3_str_appendf(&x, ")");
28289 }
28290 sqlite3_str_appendf(&x, " AS");
28291 sqlite3StrAccumFinish(&x);
28292 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
28293 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
28294 sqlite3TreeViewPop(pView);
28295 }
28296 sqlite3TreeViewPop(pView);
28297 }
28298}
28299
28300
28301/*
28302** Generate a human-readable description of a Select object.
28303*/
28304SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
28305 int n = 0;
28306 int cnt = 0;
28307 if( p==0 ){
28308 sqlite3TreeViewLine(pView, "nil-SELECT");
28309 return;
28310 }
28311 pView = sqlite3TreeViewPush(pView, moreToFollow);
28312 if( p->pWith ){
28313 sqlite3TreeViewWith(pView, p->pWith, 1);
28314 cnt = 1;
28315 sqlite3TreeViewPush(pView, 1);
28316 }
28317 do{
28318 sqlite3TreeViewLine(pView,
28319 "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
28320 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
28321 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
28322 p->selId, p, p->selFlags,
28323 (int)p->nSelectRow
28324 );
28325 if( cnt++ ) sqlite3TreeViewPop(pView);
28326 if( p->pPrior ){
28327 n = 1000;
28328 }else{
28329 n = 0;
28330 if( p->pSrc && p->pSrc->nSrc ) n++;
28331 if( p->pWhere ) n++;
28332 if( p->pGroupBy ) n++;
28333 if( p->pHaving ) n++;
28334 if( p->pOrderBy ) n++;
28335 if( p->pLimit ) n++;
28336#ifndef SQLITE_OMIT_WINDOWFUNC
28337 if( p->pWin ) n++;
28338 if( p->pWinDefn ) n++;
28339#endif
28340 }
28341 sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
28342#ifndef SQLITE_OMIT_WINDOWFUNC
28343 if( p->pWin ){
28344 Window *pX;
28345 pView = sqlite3TreeViewPush(pView, (n--)>0);
28346 sqlite3TreeViewLine(pView, "window-functions");
28347 for(pX=p->pWin; pX; pX=pX->pNextWin){
28348 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
28349 }
28350 sqlite3TreeViewPop(pView);
28351 }
28352#endif
28353 if( p->pSrc && p->pSrc->nSrc ){
28354 int i;
28355 pView = sqlite3TreeViewPush(pView, (n--)>0);
28356 sqlite3TreeViewLine(pView, "FROM");
28357 for(i=0; i<p->pSrc->nSrc; i++){
28358 struct SrcList_item *pItem = &p->pSrc->a[i];
28359 StrAccum x;
28360 char zLine[100];
28361 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
28362 sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor);
28363 if( pItem->zDatabase ){
28364 sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
28365 }else if( pItem->zName ){
28366 sqlite3_str_appendf(&x, " %s", pItem->zName);
28367 }
28368 if( pItem->pTab ){
28369 sqlite3_str_appendf(&x, " tabname=%Q", pItem->pTab->zName);
28370 }
28371 if( pItem->zAlias ){
28372 sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
28373 }
28374 if( pItem->fg.jointype & JT_LEFT ){
28375 sqlite3_str_appendf(&x, " LEFT-JOIN");
28376 }
28377 sqlite3StrAccumFinish(&x);
28378 sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
28379 if( pItem->pSelect ){
28380 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
28381 }
28382 if( pItem->fg.isTabFunc ){
28383 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
28384 }
28385 sqlite3TreeViewPop(pView);
28386 }
28387 sqlite3TreeViewPop(pView);
28388 }
28389 if( p->pWhere ){
28390 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
28391 sqlite3TreeViewExpr(pView, p->pWhere, 0);
28392 sqlite3TreeViewPop(pView);
28393 }
28394 if( p->pGroupBy ){
28395 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
28396 }
28397 if( p->pHaving ){
28398 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
28399 sqlite3TreeViewExpr(pView, p->pHaving, 0);
28400 sqlite3TreeViewPop(pView);
28401 }
28402#ifndef SQLITE_OMIT_WINDOWFUNC
28403 if( p->pWinDefn ){
28404 Window *pX;
28405 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
28406 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
28407 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
28408 }
28409 sqlite3TreeViewPop(pView);
28410 }
28411#endif
28412 if( p->pOrderBy ){
28413 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
28414 }
28415 if( p->pLimit ){
28416 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
28417 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
28418 if( p->pLimit->pRight ){
28419 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
28420 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
28421 sqlite3TreeViewPop(pView);
28422 }
28423 sqlite3TreeViewPop(pView);
28424 }
28425 if( p->pPrior ){
28426 const char *zOp = "UNION";
28427 switch( p->op ){
28428 case TK_ALL: zOp = "UNION ALL"; break;
28429 case TK_INTERSECT: zOp = "INTERSECT"; break;
28430 case TK_EXCEPT: zOp = "EXCEPT"; break;
28431 }
28432 sqlite3TreeViewItem(pView, zOp, 1);
28433 }
28434 p = p->pPrior;
28435 }while( p!=0 );
28436 sqlite3TreeViewPop(pView);
28437}
28438
28439#ifndef SQLITE_OMIT_WINDOWFUNC
28440/*
28441** Generate a description of starting or stopping bounds
28442*/
28443SQLITE_PRIVATE void sqlite3TreeViewBound(
28444 TreeView *pView, /* View context */
28445 u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
28446 Expr *pExpr, /* Value for PRECEDING or FOLLOWING */
28447 u8 moreToFollow /* True if more to follow */
28448){
28449 switch( eBound ){
28450 case TK_UNBOUNDED: {
28451 sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
28452 sqlite3TreeViewPop(pView);
28453 break;
28454 }
28455 case TK_CURRENT: {
28456 sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
28457 sqlite3TreeViewPop(pView);
28458 break;
28459 }
28460 case TK_PRECEDING: {
28461 sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
28462 sqlite3TreeViewExpr(pView, pExpr, 0);
28463 sqlite3TreeViewPop(pView);
28464 break;
28465 }
28466 case TK_FOLLOWING: {
28467 sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
28468 sqlite3TreeViewExpr(pView, pExpr, 0);
28469 sqlite3TreeViewPop(pView);
28470 break;
28471 }
28472 }
28473}
28474#endif /* SQLITE_OMIT_WINDOWFUNC */
28475
28476#ifndef SQLITE_OMIT_WINDOWFUNC
28477/*
28478** Generate a human-readable explanation for a Window object
28479*/
28480SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
28481 pView = sqlite3TreeViewPush(pView, more);
28482 if( pWin->zName ){
28483 sqlite3TreeViewLine(pView, "OVER %s", pWin->zName);
28484 }else{
28485 sqlite3TreeViewLine(pView, "OVER");
28486 }
28487 if( pWin->pPartition ){
28488 sqlite3TreeViewExprList(pView, pWin->pPartition, 1, "PARTITION-BY");
28489 }
28490 if( pWin->pOrderBy ){
28491 sqlite3TreeViewExprList(pView, pWin->pOrderBy, 1, "ORDER-BY");
28492 }
28493 if( pWin->eType ){
28494 sqlite3TreeViewItem(pView, pWin->eType==TK_RANGE ? "RANGE" : "ROWS", 0);
28495 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
28496 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
28497 sqlite3TreeViewPop(pView);
28498 }
28499 sqlite3TreeViewPop(pView);
28500}
28501#endif /* SQLITE_OMIT_WINDOWFUNC */
28502
28503#ifndef SQLITE_OMIT_WINDOWFUNC
28504/*
28505** Generate a human-readable explanation for a Window Function object
28506*/
28507SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
28508 pView = sqlite3TreeViewPush(pView, more);
28509 sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
28510 pWin->pFunc->zName, pWin->pFunc->nArg);
28511 sqlite3TreeViewWindow(pView, pWin, 0);
28512 sqlite3TreeViewPop(pView);
28513}
28514#endif /* SQLITE_OMIT_WINDOWFUNC */
28515
28516/*
28517** Generate a human-readable explanation of an expression tree.
28518*/
28519SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
28520 const char *zBinOp = 0; /* Binary operator */
28521 const char *zUniOp = 0; /* Unary operator */
28522 char zFlgs[60];
28523 pView = sqlite3TreeViewPush(pView, moreToFollow);
28524 if( pExpr==0 ){
28525 sqlite3TreeViewLine(pView, "nil");
28526 sqlite3TreeViewPop(pView);
28527 return;
28528 }
28529 if( pExpr->flags ){
28530 if( ExprHasProperty(pExpr, EP_FromJoin) ){
28531 sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x iRJT=%d",
28532 pExpr->flags, pExpr->iRightJoinTable);
28533 }else{
28534 sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags);
28535 }
28536 }else{
28537 zFlgs[0] = 0;
28538 }
28539 switch( pExpr->op ){
28540 case TK_AGG_COLUMN: {
28541 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
28542 pExpr->iTable, pExpr->iColumn, zFlgs);
28543 break;
28544 }
28545 case TK_COLUMN: {
28546 if( pExpr->iTable<0 ){
28547 /* This only happens when coding check constraints */
28548 sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
28549 }else{
28550 sqlite3TreeViewLine(pView, "{%d:%d}%s",
28551 pExpr->iTable, pExpr->iColumn, zFlgs);
28552 }
28553 if( ExprHasProperty(pExpr, EP_FixedCol) ){
28554 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
28555 }
28556 break;
28557 }
28558 case TK_INTEGER: {
28559 if( pExpr->flags & EP_IntValue ){
28560 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
28561 }else{
28562 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
28563 }
28564 break;
28565 }
28566#ifndef SQLITE_OMIT_FLOATING_POINT
28567 case TK_FLOAT: {
28568 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
28569 break;
28570 }
28571#endif
28572 case TK_STRING: {
28573 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
28574 break;
28575 }
28576 case TK_NULL: {
28577 sqlite3TreeViewLine(pView,"NULL");
28578 break;
28579 }
28580 case TK_TRUEFALSE: {
28581 sqlite3TreeViewLine(pView,
28582 sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
28583 break;
28584 }
28585#ifndef SQLITE_OMIT_BLOB_LITERAL
28586 case TK_BLOB: {
28587 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
28588 break;
28589 }
28590#endif
28591 case TK_VARIABLE: {
28592 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
28593 pExpr->u.zToken, pExpr->iColumn);
28594 break;
28595 }
28596 case TK_REGISTER: {
28597 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
28598 break;
28599 }
28600 case TK_ID: {
28601 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
28602 break;
28603 }
28604#ifndef SQLITE_OMIT_CAST
28605 case TK_CAST: {
28606 /* Expressions of the form: CAST(pLeft AS token) */
28607 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
28608 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
28609 break;
28610 }
28611#endif /* SQLITE_OMIT_CAST */
28612 case TK_LT: zBinOp = "LT"; break;
28613 case TK_LE: zBinOp = "LE"; break;
28614 case TK_GT: zBinOp = "GT"; break;
28615 case TK_GE: zBinOp = "GE"; break;
28616 case TK_NE: zBinOp = "NE"; break;
28617 case TK_EQ: zBinOp = "EQ"; break;
28618 case TK_IS: zBinOp = "IS"; break;
28619 case TK_ISNOT: zBinOp = "ISNOT"; break;
28620 case TK_AND: zBinOp = "AND"; break;
28621 case TK_OR: zBinOp = "OR"; break;
28622 case TK_PLUS: zBinOp = "ADD"; break;
28623 case TK_STAR: zBinOp = "MUL"; break;
28624 case TK_MINUS: zBinOp = "SUB"; break;
28625 case TK_REM: zBinOp = "REM"; break;
28626 case TK_BITAND: zBinOp = "BITAND"; break;
28627 case TK_BITOR: zBinOp = "BITOR"; break;
28628 case TK_SLASH: zBinOp = "DIV"; break;
28629 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
28630 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
28631 case TK_CONCAT: zBinOp = "CONCAT"; break;
28632 case TK_DOT: zBinOp = "DOT"; break;
28633
28634 case TK_UMINUS: zUniOp = "UMINUS"; break;
28635 case TK_UPLUS: zUniOp = "UPLUS"; break;
28636 case TK_BITNOT: zUniOp = "BITNOT"; break;
28637 case TK_NOT: zUniOp = "NOT"; break;
28638 case TK_ISNULL: zUniOp = "ISNULL"; break;
28639 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
28640
28641 case TK_TRUTH: {
28642 int x;
28643 const char *azOp[] = {
28644 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
28645 };
28646 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
28647 assert( pExpr->pRight );
28648 assert( pExpr->pRight->op==TK_TRUEFALSE );
28649 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
28650 zUniOp = azOp[x];
28651 break;
28652 }
28653
28654 case TK_SPAN: {
28655 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
28656 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
28657 break;
28658 }
28659
28660 case TK_COLLATE: {
28661 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
28662 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
28663 break;
28664 }
28665
28666 case TK_AGG_FUNCTION:
28667 case TK_FUNCTION: {
28668 ExprList *pFarg; /* List of function arguments */
28669 Window *pWin;
28670 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
28671 pFarg = 0;
28672 pWin = 0;
28673 }else{
28674 pFarg = pExpr->x.pList;
28675#ifndef SQLITE_OMIT_WINDOWFUNC
28676 pWin = pExpr->pWin;
28677#else
28678 pWin = 0;
28679#endif
28680 }
28681 if( pExpr->op==TK_AGG_FUNCTION ){
28682 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
28683 pExpr->op2, pExpr->u.zToken);
28684 }else{
28685 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
28686 }
28687 if( pFarg ){
28688 sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
28689 }
28690#ifndef SQLITE_OMIT_WINDOWFUNC
28691 if( pWin ){
28692 sqlite3TreeViewWindow(pView, pWin, 0);
28693 }
28694#endif
28695 break;
28696 }
28697#ifndef SQLITE_OMIT_SUBQUERY
28698 case TK_EXISTS: {
28699 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
28700 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
28701 break;
28702 }
28703 case TK_SELECT: {
28704 sqlite3TreeViewLine(pView, "SELECT-expr flags=0x%x", pExpr->flags);
28705 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
28706 break;
28707 }
28708 case TK_IN: {
28709 sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
28710 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
28711 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
28712 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
28713 }else{
28714 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
28715 }
28716 break;
28717 }
28718#endif /* SQLITE_OMIT_SUBQUERY */
28719
28720 /*
28721 ** x BETWEEN y AND z
28722 **
28723 ** This is equivalent to
28724 **
28725 ** x>=y AND x<=z
28726 **
28727 ** X is stored in pExpr->pLeft.
28728 ** Y is stored in pExpr->pList->a[0].pExpr.
28729 ** Z is stored in pExpr->pList->a[1].pExpr.
28730 */
28731 case TK_BETWEEN: {
28732 Expr *pX = pExpr->pLeft;
28733 Expr *pY = pExpr->x.pList->a[0].pExpr;
28734 Expr *pZ = pExpr->x.pList->a[1].pExpr;
28735 sqlite3TreeViewLine(pView, "BETWEEN");
28736 sqlite3TreeViewExpr(pView, pX, 1);
28737 sqlite3TreeViewExpr(pView, pY, 1);
28738 sqlite3TreeViewExpr(pView, pZ, 0);
28739 break;
28740 }
28741 case TK_TRIGGER: {
28742 /* If the opcode is TK_TRIGGER, then the expression is a reference
28743 ** to a column in the new.* or old.* pseudo-tables available to
28744 ** trigger programs. In this case Expr.iTable is set to 1 for the
28745 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
28746 ** is set to the column of the pseudo-table to read, or to -1 to
28747 ** read the rowid field.
28748 */
28749 sqlite3TreeViewLine(pView, "%s(%d)",
28750 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
28751 break;
28752 }
28753 case TK_CASE: {
28754 sqlite3TreeViewLine(pView, "CASE");
28755 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
28756 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
28757 break;
28758 }
28759#ifndef SQLITE_OMIT_TRIGGER
28760 case TK_RAISE: {
28761 const char *zType = "unk";
28762 switch( pExpr->affinity ){
28763 case OE_Rollback: zType = "rollback"; break;
28764 case OE_Abort: zType = "abort"; break;
28765 case OE_Fail: zType = "fail"; break;
28766 case OE_Ignore: zType = "ignore"; break;
28767 }
28768 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
28769 break;
28770 }
28771#endif
28772 case TK_MATCH: {
28773 sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
28774 pExpr->iTable, pExpr->iColumn, zFlgs);
28775 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
28776 break;
28777 }
28778 case TK_VECTOR: {
28779 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, "VECTOR");
28780 break;
28781 }
28782 case TK_SELECT_COLUMN: {
28783 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
28784 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
28785 break;
28786 }
28787 case TK_IF_NULL_ROW: {
28788 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
28789 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
28790 break;
28791 }
28792 default: {
28793 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
28794 break;
28795 }
28796 }
28797 if( zBinOp ){
28798 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
28799 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
28800 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
28801 }else if( zUniOp ){
28802 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
28803 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
28804 }
28805 sqlite3TreeViewPop(pView);
28806}
28807
28808
28809/*
28810** Generate a human-readable explanation of an expression list.
28811*/
28812SQLITE_PRIVATE void sqlite3TreeViewBareExprList(
28813 TreeView *pView,
28814 const ExprList *pList,
28815 const char *zLabel
28816){
28817 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
28818 if( pList==0 ){
28819 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
28820 }else{
28821 int i;
28822 sqlite3TreeViewLine(pView, "%s", zLabel);
28823 for(i=0; i<pList->nExpr; i++){
28824 int j = pList->a[i].u.x.iOrderByCol;
28825 char *zName = pList->a[i].zName;
28826 int moreToFollow = i<pList->nExpr - 1;
28827 if( j || zName ){
28828 sqlite3TreeViewPush(pView, moreToFollow);
28829 moreToFollow = 0;
28830 sqlite3TreeViewLine(pView, 0);
28831 if( zName ){
28832 fprintf(stdout, "AS %s ", zName);
28833 }
28834 if( j ){
28835 fprintf(stdout, "iOrderByCol=%d", j);
28836 }
28837 fprintf(stdout, "\n");
28838 fflush(stdout);
28839 }
28840 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
28841 if( j || zName ){
28842 sqlite3TreeViewPop(pView);
28843 }
28844 }
28845 }
28846}
28847SQLITE_PRIVATE void sqlite3TreeViewExprList(
28848 TreeView *pView,
28849 const ExprList *pList,
28850 u8 moreToFollow,
28851 const char *zLabel
28852){
28853 pView = sqlite3TreeViewPush(pView, moreToFollow);
28854 sqlite3TreeViewBareExprList(pView, pList, zLabel);
28855 sqlite3TreeViewPop(pView);
28856}
28857
28858#endif /* SQLITE_DEBUG */
28859
28860/************** End of treeview.c ********************************************/
28861/************** Begin file random.c ******************************************/
28862/*
28863** 2001 September 15
28864**
28865** The author disclaims copyright to this source code. In place of
28866** a legal notice, here is a blessing:
28867**
28868** May you do good and not evil.
28869** May you find forgiveness for yourself and forgive others.
28870** May you share freely, never taking more than you give.
28871**
28872*************************************************************************
28873** This file contains code to implement a pseudo-random number
28874** generator (PRNG) for SQLite.
28875**
28876** Random numbers are used by some of the database backends in order
28877** to generate random integer keys for tables or random filenames.
28878*/
28879/* #include "sqliteInt.h" */
28880
28881
28882/* All threads share a single random number generator.
28883** This structure is the current state of the generator.
28884*/
28885static SQLITE_WSD struct sqlite3PrngType {
28886 unsigned char isInit; /* True if initialized */
28887 unsigned char i, j; /* State variables */
28888 unsigned char s[256]; /* State variables */
28889} sqlite3Prng;
28890
28891/*
28892** Return N random bytes.
28893*/
28894SQLITE_API void sqlite3_randomness(int N, void *pBuf){
28895 unsigned char t;
28896 unsigned char *zBuf = pBuf;
28897
28898 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
28899 ** state vector. If writable static data is unsupported on the target,
28900 ** we have to locate the state vector at run-time. In the more common
28901 ** case where writable static data is supported, wsdPrng can refer directly
28902 ** to the "sqlite3Prng" state vector declared above.
28903 */
28904#ifdef SQLITE_OMIT_WSD
28905 struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
28906# define wsdPrng p[0]
28907#else
28908# define wsdPrng sqlite3Prng
28909#endif
28910
28911#if SQLITE_THREADSAFE
28912 sqlite3_mutex *mutex;
28913#endif
28914
28915#ifndef SQLITE_OMIT_AUTOINIT
28916 if( sqlite3_initialize() ) return;
28917#endif
28918
28919#if SQLITE_THREADSAFE
28920 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
28921#endif
28922
28923 sqlite3_mutex_enter(mutex);
28924 if( N<=0 || pBuf==0 ){
28925 wsdPrng.isInit = 0;
28926 sqlite3_mutex_leave(mutex);
28927 return;
28928 }
28929
28930 /* Initialize the state of the random number generator once,
28931 ** the first time this routine is called. The seed value does
28932 ** not need to contain a lot of randomness since we are not
28933 ** trying to do secure encryption or anything like that...
28934 **
28935 ** Nothing in this file or anywhere else in SQLite does any kind of
28936 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
28937 ** number generator) not as an encryption device.
28938 */
28939 if( !wsdPrng.isInit ){
28940 int i;
28941 char k[256];
28942 wsdPrng.j = 0;
28943 wsdPrng.i = 0;
28944 sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
28945 for(i=0; i<256; i++){
28946 wsdPrng.s[i] = (u8)i;
28947 }
28948 for(i=0; i<256; i++){
28949 wsdPrng.j += wsdPrng.s[i] + k[i];
28950 t = wsdPrng.s[wsdPrng.j];
28951 wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
28952 wsdPrng.s[i] = t;
28953 }
28954 wsdPrng.isInit = 1;
28955 }
28956
28957 assert( N>0 );
28958 do{
28959 wsdPrng.i++;
28960 t = wsdPrng.s[wsdPrng.i];
28961 wsdPrng.j += t;
28962 wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
28963 wsdPrng.s[wsdPrng.j] = t;
28964 t += wsdPrng.s[wsdPrng.i];
28965 *(zBuf++) = wsdPrng.s[t];
28966 }while( --N );
28967 sqlite3_mutex_leave(mutex);
28968}
28969
28970#ifndef SQLITE_UNTESTABLE
28971/*
28972** For testing purposes, we sometimes want to preserve the state of
28973** PRNG and restore the PRNG to its saved state at a later time, or
28974** to reset the PRNG to its initial state. These routines accomplish
28975** those tasks.
28976**
28977** The sqlite3_test_control() interface calls these routines to
28978** control the PRNG.
28979*/
28980static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
28981SQLITE_PRIVATE void sqlite3PrngSaveState(void){
28982 memcpy(
28983 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
28984 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
28985 sizeof(sqlite3Prng)
28986 );
28987}
28988SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
28989 memcpy(
28990 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
28991 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
28992 sizeof(sqlite3Prng)
28993 );
28994}
28995#endif /* SQLITE_UNTESTABLE */
28996
28997/************** End of random.c **********************************************/
28998/************** Begin file threads.c *****************************************/
28999/*
29000** 2012 July 21
29001**
29002** The author disclaims copyright to this source code. In place of
29003** a legal notice, here is a blessing:
29004**
29005** May you do good and not evil.
29006** May you find forgiveness for yourself and forgive others.
29007** May you share freely, never taking more than you give.
29008**
29009******************************************************************************
29010**
29011** This file presents a simple cross-platform threading interface for
29012** use internally by SQLite.
29013**
29014** A "thread" can be created using sqlite3ThreadCreate(). This thread
29015** runs independently of its creator until it is joined using
29016** sqlite3ThreadJoin(), at which point it terminates.
29017**
29018** Threads do not have to be real. It could be that the work of the
29019** "thread" is done by the main thread at either the sqlite3ThreadCreate()
29020** or sqlite3ThreadJoin() call. This is, in fact, what happens in
29021** single threaded systems. Nothing in SQLite requires multiple threads.
29022** This interface exists so that applications that want to take advantage
29023** of multiple cores can do so, while also allowing applications to stay
29024** single-threaded if desired.
29025*/
29026/* #include "sqliteInt.h" */
29027#if SQLITE_OS_WIN
29028/* # include "os_win.h" */
29029#endif
29030
29031#if SQLITE_MAX_WORKER_THREADS>0
29032
29033/********************************* Unix Pthreads ****************************/
29034#if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0
29035
29036#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
29037/* #include <pthread.h> */
29038
29039/* A running thread */
29040struct SQLiteThread {
29041 pthread_t tid; /* Thread ID */
29042 int done; /* Set to true when thread finishes */
29043 void *pOut; /* Result returned by the thread */
29044 void *(*xTask)(void*); /* The thread routine */
29045 void *pIn; /* Argument to the thread */
29046};
29047
29048/* Create a new thread */
29049SQLITE_PRIVATE int sqlite3ThreadCreate(
29050 SQLiteThread **ppThread, /* OUT: Write the thread object here */
29051 void *(*xTask)(void*), /* Routine to run in a separate thread */
29052 void *pIn /* Argument passed into xTask() */
29053){
29054 SQLiteThread *p;
29055 int rc;
29056
29057 assert( ppThread!=0 );
29058 assert( xTask!=0 );
29059 /* This routine is never used in single-threaded mode */
29060 assert( sqlite3GlobalConfig.bCoreMutex!=0 );
29061
29062 *ppThread = 0;
29063 p = sqlite3Malloc(sizeof(*p));
29064 if( p==0 ) return SQLITE_NOMEM_BKPT;
29065 memset(p, 0, sizeof(*p));
29066 p->xTask = xTask;
29067 p->pIn = pIn;
29068 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
29069 ** function that returns SQLITE_ERROR when passed the argument 200, that
29070 ** forces worker threads to run sequentially and deterministically
29071 ** for testing purposes. */
29072 if( sqlite3FaultSim(200) ){
29073 rc = 1;
29074 }else{
29075 rc = pthread_create(&p->tid, 0, xTask, pIn);
29076 }
29077 if( rc ){
29078 p->done = 1;
29079 p->pOut = xTask(pIn);
29080 }
29081 *ppThread = p;
29082 return SQLITE_OK;
29083}
29084
29085/* Get the results of the thread */
29086SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
29087 int rc;
29088
29089 assert( ppOut!=0 );
29090 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
29091 if( p->done ){
29092 *ppOut = p->pOut;
29093 rc = SQLITE_OK;
29094 }else{
29095 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
29096 }
29097 sqlite3_free(p);
29098 return rc;
29099}
29100
29101#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
29102/******************************** End Unix Pthreads *************************/
29103
29104
29105/********************************* Win32 Threads ****************************/
29106#if SQLITE_OS_WIN_THREADS
29107
29108#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
29109#include <process.h>
29110
29111/* A running thread */
29112struct SQLiteThread {
29113 void *tid; /* The thread handle */
29114 unsigned id; /* The thread identifier */
29115 void *(*xTask)(void*); /* The routine to run as a thread */
29116 void *pIn; /* Argument to xTask */
29117 void *pResult; /* Result of xTask */
29118};
29119
29120/* Thread procedure Win32 compatibility shim */
29121static unsigned __stdcall sqlite3ThreadProc(
29122 void *pArg /* IN: Pointer to the SQLiteThread structure */
29123){
29124 SQLiteThread *p = (SQLiteThread *)pArg;
29125
29126 assert( p!=0 );
29127#if 0
29128 /*
29129 ** This assert appears to trigger spuriously on certain
29130 ** versions of Windows, possibly due to _beginthreadex()
29131 ** and/or CreateThread() not fully setting their thread
29132 ** ID parameter before starting the thread.
29133 */
29134 assert( p->id==GetCurrentThreadId() );
29135#endif
29136 assert( p->xTask!=0 );
29137 p->pResult = p->xTask(p->pIn);
29138
29139 _endthreadex(0);
29140 return 0; /* NOT REACHED */
29141}
29142
29143/* Create a new thread */
29144SQLITE_PRIVATE int sqlite3ThreadCreate(
29145 SQLiteThread **ppThread, /* OUT: Write the thread object here */
29146 void *(*xTask)(void*), /* Routine to run in a separate thread */
29147 void *pIn /* Argument passed into xTask() */
29148){
29149 SQLiteThread *p;
29150
29151 assert( ppThread!=0 );
29152 assert( xTask!=0 );
29153 *ppThread = 0;
29154 p = sqlite3Malloc(sizeof(*p));
29155 if( p==0 ) return SQLITE_NOMEM_BKPT;
29156 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
29157 ** function that returns SQLITE_ERROR when passed the argument 200, that
29158 ** forces worker threads to run sequentially and deterministically
29159 ** (via the sqlite3FaultSim() term of the conditional) for testing
29160 ** purposes. */
29161 if( sqlite3GlobalConfig.bCoreMutex==0 || sqlite3FaultSim(200) ){
29162 memset(p, 0, sizeof(*p));
29163 }else{
29164 p->xTask = xTask;
29165 p->pIn = pIn;
29166 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
29167 if( p->tid==0 ){
29168 memset(p, 0, sizeof(*p));
29169 }
29170 }
29171 if( p->xTask==0 ){
29172 p->id = GetCurrentThreadId();
29173 p->pResult = xTask(pIn);
29174 }
29175 *ppThread = p;
29176 return SQLITE_OK;
29177}
29178
29179SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */
29180
29181/* Get the results of the thread */
29182SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
29183 DWORD rc;
29184 BOOL bRc;
29185
29186 assert( ppOut!=0 );
29187 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
29188 if( p->xTask==0 ){
29189 /* assert( p->id==GetCurrentThreadId() ); */
29190 rc = WAIT_OBJECT_0;
29191 assert( p->tid==0 );
29192 }else{
29193 assert( p->id!=0 && p->id!=GetCurrentThreadId() );
29194 rc = sqlite3Win32Wait((HANDLE)p->tid);
29195 assert( rc!=WAIT_IO_COMPLETION );
29196 bRc = CloseHandle((HANDLE)p->tid);
29197 assert( bRc );
29198 }
29199 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
29200 sqlite3_free(p);
29201 return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR;
29202}
29203
29204#endif /* SQLITE_OS_WIN_THREADS */
29205/******************************** End Win32 Threads *************************/
29206
29207
29208/********************************* Single-Threaded **************************/
29209#ifndef SQLITE_THREADS_IMPLEMENTED
29210/*
29211** This implementation does not actually create a new thread. It does the
29212** work of the thread in the main thread, when either the thread is created
29213** or when it is joined
29214*/
29215
29216/* A running thread */
29217struct SQLiteThread {
29218 void *(*xTask)(void*); /* The routine to run as a thread */
29219 void *pIn; /* Argument to xTask */
29220 void *pResult; /* Result of xTask */
29221};
29222
29223/* Create a new thread */
29224SQLITE_PRIVATE int sqlite3ThreadCreate(
29225 SQLiteThread **ppThread, /* OUT: Write the thread object here */
29226 void *(*xTask)(void*), /* Routine to run in a separate thread */
29227 void *pIn /* Argument passed into xTask() */
29228){
29229 SQLiteThread *p;
29230
29231 assert( ppThread!=0 );
29232 assert( xTask!=0 );
29233 *ppThread = 0;
29234 p = sqlite3Malloc(sizeof(*p));
29235 if( p==0 ) return SQLITE_NOMEM_BKPT;
29236 if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
29237 p->xTask = xTask;
29238 p->pIn = pIn;
29239 }else{
29240 p->xTask = 0;
29241 p->pResult = xTask(pIn);
29242 }
29243 *ppThread = p;
29244 return SQLITE_OK;
29245}
29246
29247/* Get the results of the thread */
29248SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
29249
29250 assert( ppOut!=0 );
29251 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
29252 if( p->xTask ){
29253 *ppOut = p->xTask(p->pIn);
29254 }else{
29255 *ppOut = p->pResult;
29256 }
29257 sqlite3_free(p);
29258
29259#if defined(SQLITE_TEST)
29260 {
29261 void *pTstAlloc = sqlite3Malloc(10);
29262 if (!pTstAlloc) return SQLITE_NOMEM_BKPT;
29263 sqlite3_free(pTstAlloc);
29264 }
29265#endif
29266
29267 return SQLITE_OK;
29268}
29269
29270#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
29271/****************************** End Single-Threaded *************************/
29272#endif /* SQLITE_MAX_WORKER_THREADS>0 */
29273
29274/************** End of threads.c *********************************************/
29275/************** Begin file utf.c *********************************************/
29276/*
29277** 2004 April 13
29278**
29279** The author disclaims copyright to this source code. In place of
29280** a legal notice, here is a blessing:
29281**
29282** May you do good and not evil.
29283** May you find forgiveness for yourself and forgive others.
29284** May you share freely, never taking more than you give.
29285**
29286*************************************************************************
29287** This file contains routines used to translate between UTF-8,
29288** UTF-16, UTF-16BE, and UTF-16LE.
29289**
29290** Notes on UTF-8:
29291**
29292** Byte-0 Byte-1 Byte-2 Byte-3 Value
29293** 0xxxxxxx 00000000 00000000 0xxxxxxx
29294** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx
29295** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx
29296** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx
29297**
29298**
29299** Notes on UTF-16: (with wwww+1==uuuuu)
29300**
29301** Word-0 Word-1 Value
29302** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
29303** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
29304**
29305**
29306** BOM or Byte Order Mark:
29307** 0xff 0xfe little-endian utf-16 follows
29308** 0xfe 0xff big-endian utf-16 follows
29309**
29310*/
29311/* #include "sqliteInt.h" */
29312/* #include <assert.h> */
29313/* #include "vdbeInt.h" */
29314
29315#if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==0
29316/*
29317** The following constant value is used by the SQLITE_BIGENDIAN and
29318** SQLITE_LITTLEENDIAN macros.
29319*/
29320SQLITE_PRIVATE const int sqlite3one = 1;
29321#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */
29322
29323/*
29324** This lookup table is used to help decode the first byte of
29325** a multi-byte UTF8 character.
29326*/
29327static const unsigned char sqlite3Utf8Trans1[] = {
29328 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
29329 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
29330 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
29331 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
29332 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
29333 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
29334 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
29335 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
29336};
29337
29338
29339#define WRITE_UTF8(zOut, c) { \
29340 if( c<0x00080 ){ \
29341 *zOut++ = (u8)(c&0xFF); \
29342 } \
29343 else if( c<0x00800 ){ \
29344 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
29345 *zOut++ = 0x80 + (u8)(c & 0x3F); \
29346 } \
29347 else if( c<0x10000 ){ \
29348 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
29349 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
29350 *zOut++ = 0x80 + (u8)(c & 0x3F); \
29351 }else{ \
29352 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
29353 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
29354 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
29355 *zOut++ = 0x80 + (u8)(c & 0x3F); \
29356 } \
29357}
29358
29359#define WRITE_UTF16LE(zOut, c) { \
29360 if( c<=0xFFFF ){ \
29361 *zOut++ = (u8)(c&0x00FF); \
29362 *zOut++ = (u8)((c>>8)&0x00FF); \
29363 }else{ \
29364 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
29365 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
29366 *zOut++ = (u8)(c&0x00FF); \
29367 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
29368 } \
29369}
29370
29371#define WRITE_UTF16BE(zOut, c) { \
29372 if( c<=0xFFFF ){ \
29373 *zOut++ = (u8)((c>>8)&0x00FF); \
29374 *zOut++ = (u8)(c&0x00FF); \
29375 }else{ \
29376 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
29377 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
29378 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
29379 *zOut++ = (u8)(c&0x00FF); \
29380 } \
29381}
29382
29383#define READ_UTF16LE(zIn, TERM, c){ \
29384 c = (*zIn++); \
29385 c += ((*zIn++)<<8); \
29386 if( c>=0xD800 && c<0xE000 && TERM ){ \
29387 int c2 = (*zIn++); \
29388 c2 += ((*zIn++)<<8); \
29389 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
29390 } \
29391}
29392
29393#define READ_UTF16BE(zIn, TERM, c){ \
29394 c = ((*zIn++)<<8); \
29395 c += (*zIn++); \
29396 if( c>=0xD800 && c<0xE000 && TERM ){ \
29397 int c2 = ((*zIn++)<<8); \
29398 c2 += (*zIn++); \
29399 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
29400 } \
29401}
29402
29403/*
29404** Translate a single UTF-8 character. Return the unicode value.
29405**
29406** During translation, assume that the byte that zTerm points
29407** is a 0x00.
29408**
29409** Write a pointer to the next unread byte back into *pzNext.
29410**
29411** Notes On Invalid UTF-8:
29412**
29413** * This routine never allows a 7-bit character (0x00 through 0x7f) to
29414** be encoded as a multi-byte character. Any multi-byte character that
29415** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
29416**
29417** * This routine never allows a UTF16 surrogate value to be encoded.
29418** If a multi-byte character attempts to encode a value between
29419** 0xd800 and 0xe000 then it is rendered as 0xfffd.
29420**
29421** * Bytes in the range of 0x80 through 0xbf which occur as the first
29422** byte of a character are interpreted as single-byte characters
29423** and rendered as themselves even though they are technically
29424** invalid characters.
29425**
29426** * This routine accepts over-length UTF8 encodings
29427** for unicode values 0x80 and greater. It does not change over-length
29428** encodings to 0xfffd as some systems recommend.
29429*/
29430#define READ_UTF8(zIn, zTerm, c) \
29431 c = *(zIn++); \
29432 if( c>=0xc0 ){ \
29433 c = sqlite3Utf8Trans1[c-0xc0]; \
29434 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
29435 c = (c<<6) + (0x3f & *(zIn++)); \
29436 } \
29437 if( c<0x80 \
29438 || (c&0xFFFFF800)==0xD800 \
29439 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
29440 }
29441SQLITE_PRIVATE u32 sqlite3Utf8Read(
29442 const unsigned char **pz /* Pointer to string from which to read char */
29443){
29444 unsigned int c;
29445
29446 /* Same as READ_UTF8() above but without the zTerm parameter.
29447 ** For this routine, we assume the UTF8 string is always zero-terminated.
29448 */
29449 c = *((*pz)++);
29450 if( c>=0xc0 ){
29451 c = sqlite3Utf8Trans1[c-0xc0];
29452 while( (*(*pz) & 0xc0)==0x80 ){
29453 c = (c<<6) + (0x3f & *((*pz)++));
29454 }
29455 if( c<0x80
29456 || (c&0xFFFFF800)==0xD800
29457 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
29458 }
29459 return c;
29460}
29461
29462
29463
29464
29465/*
29466** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
29467** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
29468*/
29469/* #define TRANSLATE_TRACE 1 */
29470
29471#ifndef SQLITE_OMIT_UTF16
29472/*
29473** This routine transforms the internal text encoding used by pMem to
29474** desiredEnc. It is an error if the string is already of the desired
29475** encoding, or if *pMem does not contain a string value.
29476*/
29477SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
29478 int len; /* Maximum length of output string in bytes */
29479 unsigned char *zOut; /* Output buffer */
29480 unsigned char *zIn; /* Input iterator */
29481 unsigned char *zTerm; /* End of input */
29482 unsigned char *z; /* Output iterator */
29483 unsigned int c;
29484
29485 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
29486 assert( pMem->flags&MEM_Str );
29487 assert( pMem->enc!=desiredEnc );
29488 assert( pMem->enc!=0 );
29489 assert( pMem->n>=0 );
29490
29491#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
29492 {
29493 char zBuf[100];
29494 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
29495 fprintf(stderr, "INPUT: %s\n", zBuf);
29496 }
29497#endif
29498
29499 /* If the translation is between UTF-16 little and big endian, then
29500 ** all that is required is to swap the byte order. This case is handled
29501 ** differently from the others.
29502 */
29503 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
29504 u8 temp;
29505 int rc;
29506 rc = sqlite3VdbeMemMakeWriteable(pMem);
29507 if( rc!=SQLITE_OK ){
29508 assert( rc==SQLITE_NOMEM );
29509 return SQLITE_NOMEM_BKPT;
29510 }
29511 zIn = (u8*)pMem->z;
29512 zTerm = &zIn[pMem->n&~1];
29513 while( zIn<zTerm ){
29514 temp = *zIn;
29515 *zIn = *(zIn+1);
29516 zIn++;
29517 *zIn++ = temp;
29518 }
29519 pMem->enc = desiredEnc;
29520 goto translate_out;
29521 }
29522
29523 /* Set len to the maximum number of bytes required in the output buffer. */
29524 if( desiredEnc==SQLITE_UTF8 ){
29525 /* When converting from UTF-16, the maximum growth results from
29526 ** translating a 2-byte character to a 4-byte UTF-8 character.
29527 ** A single byte is required for the output string
29528 ** nul-terminator.
29529 */
29530 pMem->n &= ~1;
29531 len = pMem->n * 2 + 1;
29532 }else{
29533 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
29534 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
29535 ** character. Two bytes are required in the output buffer for the
29536 ** nul-terminator.
29537 */
29538 len = pMem->n * 2 + 2;
29539 }
29540
29541 /* Set zIn to point at the start of the input buffer and zTerm to point 1
29542 ** byte past the end.
29543 **
29544 ** Variable zOut is set to point at the output buffer, space obtained
29545 ** from sqlite3_malloc().
29546 */
29547 zIn = (u8*)pMem->z;
29548 zTerm = &zIn[pMem->n];
29549 zOut = sqlite3DbMallocRaw(pMem->db, len);
29550 if( !zOut ){
29551 return SQLITE_NOMEM_BKPT;
29552 }
29553 z = zOut;
29554
29555 if( pMem->enc==SQLITE_UTF8 ){
29556 if( desiredEnc==SQLITE_UTF16LE ){
29557 /* UTF-8 -> UTF-16 Little-endian */
29558 while( zIn<zTerm ){
29559 READ_UTF8(zIn, zTerm, c);
29560 WRITE_UTF16LE(z, c);
29561 }
29562 }else{
29563 assert( desiredEnc==SQLITE_UTF16BE );
29564 /* UTF-8 -> UTF-16 Big-endian */
29565 while( zIn<zTerm ){
29566 READ_UTF8(zIn, zTerm, c);
29567 WRITE_UTF16BE(z, c);
29568 }
29569 }
29570 pMem->n = (int)(z - zOut);
29571 *z++ = 0;
29572 }else{
29573 assert( desiredEnc==SQLITE_UTF8 );
29574 if( pMem->enc==SQLITE_UTF16LE ){
29575 /* UTF-16 Little-endian -> UTF-8 */
29576 while( zIn<zTerm ){
29577 READ_UTF16LE(zIn, zIn<zTerm, c);
29578 WRITE_UTF8(z, c);
29579 }
29580 }else{
29581 /* UTF-16 Big-endian -> UTF-8 */
29582 while( zIn<zTerm ){
29583 READ_UTF16BE(zIn, zIn<zTerm, c);
29584 WRITE_UTF8(z, c);
29585 }
29586 }
29587 pMem->n = (int)(z - zOut);
29588 }
29589 *z = 0;
29590 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
29591
29592 c = pMem->flags;
29593 sqlite3VdbeMemRelease(pMem);
29594 pMem->flags = MEM_Str|MEM_Term|(c&(MEM_AffMask|MEM_Subtype));
29595 pMem->enc = desiredEnc;
29596 pMem->z = (char*)zOut;
29597 pMem->zMalloc = pMem->z;
29598 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
29599
29600translate_out:
29601#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
29602 {
29603 char zBuf[100];
29604 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
29605 fprintf(stderr, "OUTPUT: %s\n", zBuf);
29606 }
29607#endif
29608 return SQLITE_OK;
29609}
29610#endif /* SQLITE_OMIT_UTF16 */
29611
29612#ifndef SQLITE_OMIT_UTF16
29613/*
29614** This routine checks for a byte-order mark at the beginning of the
29615** UTF-16 string stored in *pMem. If one is present, it is removed and
29616** the encoding of the Mem adjusted. This routine does not do any
29617** byte-swapping, it just sets Mem.enc appropriately.
29618**
29619** The allocation (static, dynamic etc.) and encoding of the Mem may be
29620** changed by this function.
29621*/
29622SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
29623 int rc = SQLITE_OK;
29624 u8 bom = 0;
29625
29626 assert( pMem->n>=0 );
29627 if( pMem->n>1 ){
29628 u8 b1 = *(u8 *)pMem->z;
29629 u8 b2 = *(((u8 *)pMem->z) + 1);
29630 if( b1==0xFE && b2==0xFF ){
29631 bom = SQLITE_UTF16BE;
29632 }
29633 if( b1==0xFF && b2==0xFE ){
29634 bom = SQLITE_UTF16LE;
29635 }
29636 }
29637
29638 if( bom ){
29639 rc = sqlite3VdbeMemMakeWriteable(pMem);
29640 if( rc==SQLITE_OK ){
29641 pMem->n -= 2;
29642 memmove(pMem->z, &pMem->z[2], pMem->n);
29643 pMem->z[pMem->n] = '\0';
29644 pMem->z[pMem->n+1] = '\0';
29645 pMem->flags |= MEM_Term;
29646 pMem->enc = bom;
29647 }
29648 }
29649 return rc;
29650}
29651#endif /* SQLITE_OMIT_UTF16 */
29652
29653/*
29654** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
29655** return the number of unicode characters in pZ up to (but not including)
29656** the first 0x00 byte. If nByte is not less than zero, return the
29657** number of unicode characters in the first nByte of pZ (or up to
29658** the first 0x00, whichever comes first).
29659*/
29660SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
29661 int r = 0;
29662 const u8 *z = (const u8*)zIn;
29663 const u8 *zTerm;
29664 if( nByte>=0 ){
29665 zTerm = &z[nByte];
29666 }else{
29667 zTerm = (const u8*)(-1);
29668 }
29669 assert( z<=zTerm );
29670 while( *z!=0 && z<zTerm ){
29671 SQLITE_SKIP_UTF8(z);
29672 r++;
29673 }
29674 return r;
29675}
29676
29677/* This test function is not currently used by the automated test-suite.
29678** Hence it is only available in debug builds.
29679*/
29680#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
29681/*
29682** Translate UTF-8 to UTF-8.
29683**
29684** This has the effect of making sure that the string is well-formed
29685** UTF-8. Miscoded characters are removed.
29686**
29687** The translation is done in-place and aborted if the output
29688** overruns the input.
29689*/
29690SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
29691 unsigned char *zOut = zIn;
29692 unsigned char *zStart = zIn;
29693 u32 c;
29694
29695 while( zIn[0] && zOut<=zIn ){
29696 c = sqlite3Utf8Read((const u8**)&zIn);
29697 if( c!=0xfffd ){
29698 WRITE_UTF8(zOut, c);
29699 }
29700 }
29701 *zOut = 0;
29702 return (int)(zOut - zStart);
29703}
29704#endif
29705
29706#ifndef SQLITE_OMIT_UTF16
29707/*
29708** Convert a UTF-16 string in the native encoding into a UTF-8 string.
29709** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
29710** be freed by the calling function.
29711**
29712** NULL is returned if there is an allocation error.
29713*/
29714SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
29715 Mem m;
29716 memset(&m, 0, sizeof(m));
29717 m.db = db;
29718 sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
29719 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
29720 if( db->mallocFailed ){
29721 sqlite3VdbeMemRelease(&m);
29722 m.z = 0;
29723 }
29724 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
29725 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
29726 assert( m.z || db->mallocFailed );
29727 return m.z;
29728}
29729
29730/*
29731** zIn is a UTF-16 encoded unicode string at least nChar characters long.
29732** Return the number of bytes in the first nChar unicode characters
29733** in pZ. nChar must be non-negative.
29734*/
29735SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
29736 int c;
29737 unsigned char const *z = zIn;
29738 int n = 0;
29739
29740 if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
29741 while( n<nChar ){
29742 READ_UTF16BE(z, 1, c);
29743 n++;
29744 }
29745 }else{
29746 while( n<nChar ){
29747 READ_UTF16LE(z, 1, c);
29748 n++;
29749 }
29750 }
29751 return (int)(z-(unsigned char const *)zIn);
29752}
29753
29754#if defined(SQLITE_TEST)
29755/*
29756** This routine is called from the TCL test function "translate_selftest".
29757** It checks that the primitives for serializing and deserializing
29758** characters in each encoding are inverses of each other.
29759*/
29760SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
29761 unsigned int i, t;
29762 unsigned char zBuf[20];
29763 unsigned char *z;
29764 int n;
29765 unsigned int c;
29766
29767 for(i=0; i<0x00110000; i++){
29768 z = zBuf;
29769 WRITE_UTF8(z, i);
29770 n = (int)(z-zBuf);
29771 assert( n>0 && n<=4 );
29772 z[0] = 0;
29773 z = zBuf;
29774 c = sqlite3Utf8Read((const u8**)&z);
29775 t = i;
29776 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
29777 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
29778 assert( c==t );
29779 assert( (z-zBuf)==n );
29780 }
29781 for(i=0; i<0x00110000; i++){
29782 if( i>=0xD800 && i<0xE000 ) continue;
29783 z = zBuf;
29784 WRITE_UTF16LE(z, i);
29785 n = (int)(z-zBuf);
29786 assert( n>0 && n<=4 );
29787 z[0] = 0;
29788 z = zBuf;
29789 READ_UTF16LE(z, 1, c);
29790 assert( c==i );
29791 assert( (z-zBuf)==n );
29792 }
29793 for(i=0; i<0x00110000; i++){
29794 if( i>=0xD800 && i<0xE000 ) continue;
29795 z = zBuf;
29796 WRITE_UTF16BE(z, i);
29797 n = (int)(z-zBuf);
29798 assert( n>0 && n<=4 );
29799 z[0] = 0;
29800 z = zBuf;
29801 READ_UTF16BE(z, 1, c);
29802 assert( c==i );
29803 assert( (z-zBuf)==n );
29804 }
29805}
29806#endif /* SQLITE_TEST */
29807#endif /* SQLITE_OMIT_UTF16 */
29808
29809/************** End of utf.c *************************************************/
29810/************** Begin file util.c ********************************************/
29811/*
29812** 2001 September 15
29813**
29814** The author disclaims copyright to this source code. In place of
29815** a legal notice, here is a blessing:
29816**
29817** May you do good and not evil.
29818** May you find forgiveness for yourself and forgive others.
29819** May you share freely, never taking more than you give.
29820**
29821*************************************************************************
29822** Utility functions used throughout sqlite.
29823**
29824** This file contains functions for allocating memory, comparing
29825** strings, and stuff like that.
29826**
29827*/
29828/* #include "sqliteInt.h" */
29829/* #include <stdarg.h> */
29830#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
29831# include <math.h>
29832#endif
29833
29834/*
29835** Routine needed to support the testcase() macro.
29836*/
29837#ifdef SQLITE_COVERAGE_TEST
29838SQLITE_PRIVATE void sqlite3Coverage(int x){
29839 static unsigned dummy = 0;
29840 dummy += (unsigned)x;
29841}
29842#endif
29843
29844/*
29845** Give a callback to the test harness that can be used to simulate faults
29846** in places where it is difficult or expensive to do so purely by means
29847** of inputs.
29848**
29849** The intent of the integer argument is to let the fault simulator know
29850** which of multiple sqlite3FaultSim() calls has been hit.
29851**
29852** Return whatever integer value the test callback returns, or return
29853** SQLITE_OK if no test callback is installed.
29854*/
29855#ifndef SQLITE_UNTESTABLE
29856SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
29857 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
29858 return xCallback ? xCallback(iTest) : SQLITE_OK;
29859}
29860#endif
29861
29862#ifndef SQLITE_OMIT_FLOATING_POINT
29863/*
29864** Return true if the floating point value is Not a Number (NaN).
29865**
29866** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
29867** Otherwise, we have our own implementation that works on most systems.
29868*/
29869SQLITE_PRIVATE int sqlite3IsNaN(double x){
29870 int rc; /* The value return */
29871#if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN
29872 /*
29873 ** Systems that support the isnan() library function should probably
29874 ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have
29875 ** found that many systems do not have a working isnan() function so
29876 ** this implementation is provided as an alternative.
29877 **
29878 ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
29879 ** On the other hand, the use of -ffast-math comes with the following
29880 ** warning:
29881 **
29882 ** This option [-ffast-math] should never be turned on by any
29883 ** -O option since it can result in incorrect output for programs
29884 ** which depend on an exact implementation of IEEE or ISO
29885 ** rules/specifications for math functions.
29886 **
29887 ** Under MSVC, this NaN test may fail if compiled with a floating-
29888 ** point precision mode other than /fp:precise. From the MSDN
29889 ** documentation:
29890 **
29891 ** The compiler [with /fp:precise] will properly handle comparisons
29892 ** involving NaN. For example, x != x evaluates to true if x is NaN
29893 ** ...
29894 */
29895#ifdef __FAST_MATH__
29896# error SQLite will not work correctly with the -ffast-math option of GCC.
29897#endif
29898 volatile double y = x;
29899 volatile double z = y;
29900 rc = (y!=z);
29901#else /* if HAVE_ISNAN */
29902 rc = isnan(x);
29903#endif /* HAVE_ISNAN */
29904 testcase( rc );
29905 return rc;
29906}
29907#endif /* SQLITE_OMIT_FLOATING_POINT */
29908
29909/*
29910** Compute a string length that is limited to what can be stored in
29911** lower 30 bits of a 32-bit signed integer.
29912**
29913** The value returned will never be negative. Nor will it ever be greater
29914** than the actual length of the string. For very long strings (greater
29915** than 1GiB) the value returned might be less than the true string length.
29916*/
29917SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
29918 if( z==0 ) return 0;
29919 return 0x3fffffff & (int)strlen(z);
29920}
29921
29922/*
29923** Return the declared type of a column. Or return zDflt if the column
29924** has no declared type.
29925**
29926** The column type is an extra string stored after the zero-terminator on
29927** the column name if and only if the COLFLAG_HASTYPE flag is set.
29928*/
29929SQLITE_PRIVATE char *sqlite3ColumnType(Column *pCol, char *zDflt){
29930 if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt;
29931 return pCol->zName + strlen(pCol->zName) + 1;
29932}
29933
29934/*
29935** Helper function for sqlite3Error() - called rarely. Broken out into
29936** a separate routine to avoid unnecessary register saves on entry to
29937** sqlite3Error().
29938*/
29939static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){
29940 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
29941 sqlite3SystemError(db, err_code);
29942}
29943
29944/*
29945** Set the current error code to err_code and clear any prior error message.
29946** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
29947** that would be appropriate.
29948*/
29949SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
29950 assert( db!=0 );
29951 db->errCode = err_code;
29952 if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
29953}
29954
29955/*
29956** Load the sqlite3.iSysErrno field if that is an appropriate thing
29957** to do based on the SQLite error code in rc.
29958*/
29959SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
29960 if( rc==SQLITE_IOERR_NOMEM ) return;
29961 rc &= 0xff;
29962 if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
29963 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
29964 }
29965}
29966
29967/*
29968** Set the most recent error code and error string for the sqlite
29969** handle "db". The error code is set to "err_code".
29970**
29971** If it is not NULL, string zFormat specifies the format of the
29972** error string in the style of the printf functions: The following
29973** format characters are allowed:
29974**
29975** %s Insert a string
29976** %z A string that should be freed after use
29977** %d Insert an integer
29978** %T Insert a token
29979** %S Insert the first element of a SrcList
29980**
29981** zFormat and any string tokens that follow it are assumed to be
29982** encoded in UTF-8.
29983**
29984** To clear the most recent error for sqlite handle "db", sqlite3Error
29985** should be called with err_code set to SQLITE_OK and zFormat set
29986** to NULL.
29987*/
29988SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
29989 assert( db!=0 );
29990 db->errCode = err_code;
29991 sqlite3SystemError(db, err_code);
29992 if( zFormat==0 ){
29993 sqlite3Error(db, err_code);
29994 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
29995 char *z;
29996 va_list ap;
29997 va_start(ap, zFormat);
29998 z = sqlite3VMPrintf(db, zFormat, ap);
29999 va_end(ap);
30000 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
30001 }
30002}
30003
30004/*
30005** Add an error message to pParse->zErrMsg and increment pParse->nErr.
30006** The following formatting characters are allowed:
30007**
30008** %s Insert a string
30009** %z A string that should be freed after use
30010** %d Insert an integer
30011** %T Insert a token
30012** %S Insert the first element of a SrcList
30013**
30014** This function should be used to report any error that occurs while
30015** compiling an SQL statement (i.e. within sqlite3_prepare()). The
30016** last thing the sqlite3_prepare() function does is copy the error
30017** stored by this function into the database handle using sqlite3Error().
30018** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
30019** during statement execution (sqlite3_step() etc.).
30020*/
30021SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
30022 char *zMsg;
30023 va_list ap;
30024 sqlite3 *db = pParse->db;
30025 va_start(ap, zFormat);
30026 zMsg = sqlite3VMPrintf(db, zFormat, ap);
30027 va_end(ap);
30028 if( db->suppressErr ){
30029 sqlite3DbFree(db, zMsg);
30030 }else{
30031 pParse->nErr++;
30032 sqlite3DbFree(db, pParse->zErrMsg);
30033 pParse->zErrMsg = zMsg;
30034 pParse->rc = SQLITE_ERROR;
30035 }
30036}
30037
30038/*
30039** Convert an SQL-style quoted string into a normal string by removing
30040** the quote characters. The conversion is done in-place. If the
30041** input does not begin with a quote character, then this routine
30042** is a no-op.
30043**
30044** The input string must be zero-terminated. A new zero-terminator
30045** is added to the dequoted string.
30046**
30047** The return value is -1 if no dequoting occurs or the length of the
30048** dequoted string, exclusive of the zero terminator, if dequoting does
30049** occur.
30050**
30051** 2002-Feb-14: This routine is extended to remove MS-Access style
30052** brackets from around identifiers. For example: "[a-b-c]" becomes
30053** "a-b-c".
30054*/
30055SQLITE_PRIVATE void sqlite3Dequote(char *z){
30056 char quote;
30057 int i, j;
30058 if( z==0 ) return;
30059 quote = z[0];
30060 if( !sqlite3Isquote(quote) ) return;
30061 if( quote=='[' ) quote = ']';
30062 for(i=1, j=0;; i++){
30063 assert( z[i] );
30064 if( z[i]==quote ){
30065 if( z[i+1]==quote ){
30066 z[j++] = quote;
30067 i++;
30068 }else{
30069 break;
30070 }
30071 }else{
30072 z[j++] = z[i];
30073 }
30074 }
30075 z[j] = 0;
30076}
30077
30078/*
30079** Generate a Token object from a string
30080*/
30081SQLITE_PRIVATE void sqlite3TokenInit(Token *p, char *z){
30082 p->z = z;
30083 p->n = sqlite3Strlen30(z);
30084}
30085
30086/* Convenient short-hand */
30087#define UpperToLower sqlite3UpperToLower
30088
30089/*
30090** Some systems have stricmp(). Others have strcasecmp(). Because
30091** there is no consistency, we will define our own.
30092**
30093** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
30094** sqlite3_strnicmp() APIs allow applications and extensions to compare
30095** the contents of two buffers containing UTF-8 strings in a
30096** case-independent fashion, using the same definition of "case
30097** independence" that SQLite uses internally when comparing identifiers.
30098*/
30099SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
30100 if( zLeft==0 ){
30101 return zRight ? -1 : 0;
30102 }else if( zRight==0 ){
30103 return 1;
30104 }
30105 return sqlite3StrICmp(zLeft, zRight);
30106}
30107SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
30108 unsigned char *a, *b;
30109 int c;
30110 a = (unsigned char *)zLeft;
30111 b = (unsigned char *)zRight;
30112 for(;;){
30113 c = (int)UpperToLower[*a] - (int)UpperToLower[*b];
30114 if( c || *a==0 ) break;
30115 a++;
30116 b++;
30117 }
30118 return c;
30119}
30120SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
30121 register unsigned char *a, *b;
30122 if( zLeft==0 ){
30123 return zRight ? -1 : 0;
30124 }else if( zRight==0 ){
30125 return 1;
30126 }
30127 a = (unsigned char *)zLeft;
30128 b = (unsigned char *)zRight;
30129 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
30130 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
30131}
30132
30133/*
30134** Compute 10 to the E-th power. Examples: E==1 results in 10.
30135** E==2 results in 100. E==50 results in 1.0e50.
30136**
30137** This routine only works for values of E between 1 and 341.
30138*/
30139static LONGDOUBLE_TYPE sqlite3Pow10(int E){
30140#if defined(_MSC_VER)
30141 static const LONGDOUBLE_TYPE x[] = {
30142 1.0e+001,
30143 1.0e+002,
30144 1.0e+004,
30145 1.0e+008,
30146 1.0e+016,
30147 1.0e+032,
30148 1.0e+064,
30149 1.0e+128,
30150 1.0e+256
30151 };
30152 LONGDOUBLE_TYPE r = 1.0;
30153 int i;
30154 assert( E>=0 && E<=307 );
30155 for(i=0; E!=0; i++, E >>=1){
30156 if( E & 1 ) r *= x[i];
30157 }
30158 return r;
30159#else
30160 LONGDOUBLE_TYPE x = 10.0;
30161 LONGDOUBLE_TYPE r = 1.0;
30162 while(1){
30163 if( E & 1 ) r *= x;
30164 E >>= 1;
30165 if( E==0 ) break;
30166 x *= x;
30167 }
30168 return r;
30169#endif
30170}
30171
30172/*
30173** The string z[] is an text representation of a real number.
30174** Convert this string to a double and write it into *pResult.
30175**
30176** The string z[] is length bytes in length (bytes, not characters) and
30177** uses the encoding enc. The string is not necessarily zero-terminated.
30178**
30179** Return TRUE if the result is a valid real number (or integer) and FALSE
30180** if the string is empty or contains extraneous text. Valid numbers
30181** are in one of these formats:
30182**
30183** [+-]digits[E[+-]digits]
30184** [+-]digits.[digits][E[+-]digits]
30185** [+-].digits[E[+-]digits]
30186**
30187** Leading and trailing whitespace is ignored for the purpose of determining
30188** validity.
30189**
30190** If some prefix of the input string is a valid number, this routine
30191** returns FALSE but it still converts the prefix and writes the result
30192** into *pResult.
30193*/
30194SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
30195#ifndef SQLITE_OMIT_FLOATING_POINT
30196 int incr;
30197 const char *zEnd = z + length;
30198 /* sign * significand * (10 ^ (esign * exponent)) */
30199 int sign = 1; /* sign of significand */
30200 i64 s = 0; /* significand */
30201 int d = 0; /* adjust exponent for shifting decimal point */
30202 int esign = 1; /* sign of exponent */
30203 int e = 0; /* exponent */
30204 int eValid = 1; /* True exponent is either not used or is well-formed */
30205 double result;
30206 int nDigits = 0;
30207 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
30208
30209 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
30210 *pResult = 0.0; /* Default return value, in case of an error */
30211
30212 if( enc==SQLITE_UTF8 ){
30213 incr = 1;
30214 }else{
30215 int i;
30216 incr = 2;
30217 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
30218 for(i=3-enc; i<length && z[i]==0; i+=2){}
30219 nonNum = i<length;
30220 zEnd = &z[i^1];
30221 z += (enc&1);
30222 }
30223
30224 /* skip leading spaces */
30225 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
30226 if( z>=zEnd ) return 0;
30227
30228 /* get sign of significand */
30229 if( *z=='-' ){
30230 sign = -1;
30231 z+=incr;
30232 }else if( *z=='+' ){
30233 z+=incr;
30234 }
30235
30236 /* copy max significant digits to significand */
30237 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
30238 s = s*10 + (*z - '0');
30239 z+=incr; nDigits++;
30240 }
30241
30242 /* skip non-significant significand digits
30243 ** (increase exponent by d to shift decimal left) */
30244 while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; nDigits++; d++; }
30245 if( z>=zEnd ) goto do_atof_calc;
30246
30247 /* if decimal point is present */
30248 if( *z=='.' ){
30249 z+=incr;
30250 /* copy digits from after decimal to significand
30251 ** (decrease exponent by d to shift decimal right) */
30252 while( z<zEnd && sqlite3Isdigit(*z) ){
30253 if( s<((LARGEST_INT64-9)/10) ){
30254 s = s*10 + (*z - '0');
30255 d--;
30256 }
30257 z+=incr; nDigits++;
30258 }
30259 }
30260 if( z>=zEnd ) goto do_atof_calc;
30261
30262 /* if exponent is present */
30263 if( *z=='e' || *z=='E' ){
30264 z+=incr;
30265 eValid = 0;
30266
30267 /* This branch is needed to avoid a (harmless) buffer overread. The
30268 ** special comment alerts the mutation tester that the correct answer
30269 ** is obtained even if the branch is omitted */
30270 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
30271
30272 /* get sign of exponent */
30273 if( *z=='-' ){
30274 esign = -1;
30275 z+=incr;
30276 }else if( *z=='+' ){
30277 z+=incr;
30278 }
30279 /* copy digits to exponent */
30280 while( z<zEnd && sqlite3Isdigit(*z) ){
30281 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
30282 z+=incr;
30283 eValid = 1;
30284 }
30285 }
30286
30287 /* skip trailing spaces */
30288 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
30289
30290do_atof_calc:
30291 /* adjust exponent by d, and update sign */
30292 e = (e*esign) + d;
30293 if( e<0 ) {
30294 esign = -1;
30295 e *= -1;
30296 } else {
30297 esign = 1;
30298 }
30299
30300 if( s==0 ) {
30301 /* In the IEEE 754 standard, zero is signed. */
30302 result = sign<0 ? -(double)0 : (double)0;
30303 } else {
30304 /* Attempt to reduce exponent.
30305 **
30306 ** Branches that are not required for the correct answer but which only
30307 ** help to obtain the correct answer faster are marked with special
30308 ** comments, as a hint to the mutation tester.
30309 */
30310 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
30311 if( esign>0 ){
30312 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/
30313 s *= 10;
30314 }else{
30315 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
30316 s /= 10;
30317 }
30318 e--;
30319 }
30320
30321 /* adjust the sign of significand */
30322 s = sign<0 ? -s : s;
30323
30324 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
30325 result = (double)s;
30326 }else{
30327 /* attempt to handle extremely small/large numbers better */
30328 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
30329 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
30330 LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
30331 if( esign<0 ){
30332 result = s / scale;
30333 result /= 1.0e+308;
30334 }else{
30335 result = s * scale;
30336 result *= 1.0e+308;
30337 }
30338 }else{ assert( e>=342 );
30339 if( esign<0 ){
30340 result = 0.0*s;
30341 }else{
30342#ifdef INFINITY
30343 result = INFINITY*s;
30344#else
30345 result = 1e308*1e308*s; /* Infinity */
30346#endif
30347 }
30348 }
30349 }else{
30350 LONGDOUBLE_TYPE scale = sqlite3Pow10(e);
30351 if( esign<0 ){
30352 result = s / scale;
30353 }else{
30354 result = s * scale;
30355 }
30356 }
30357 }
30358 }
30359
30360 /* store the result */
30361 *pResult = result;
30362
30363 /* return true if number and no extra non-whitespace chracters after */
30364 return z==zEnd && nDigits>0 && eValid && nonNum==0;
30365#else
30366 return !sqlite3Atoi64(z, pResult, length, enc);
30367#endif /* SQLITE_OMIT_FLOATING_POINT */
30368}
30369
30370/*
30371** Compare the 19-character string zNum against the text representation
30372** value 2^63: 9223372036854775808. Return negative, zero, or positive
30373** if zNum is less than, equal to, or greater than the string.
30374** Note that zNum must contain exactly 19 characters.
30375**
30376** Unlike memcmp() this routine is guaranteed to return the difference
30377** in the values of the last digit if the only difference is in the
30378** last digit. So, for example,
30379**
30380** compare2pow63("9223372036854775800", 1)
30381**
30382** will return -8.
30383*/
30384static int compare2pow63(const char *zNum, int incr){
30385 int c = 0;
30386 int i;
30387 /* 012345678901234567 */
30388 const char *pow63 = "922337203685477580";
30389 for(i=0; c==0 && i<18; i++){
30390 c = (zNum[i*incr]-pow63[i])*10;
30391 }
30392 if( c==0 ){
30393 c = zNum[18*incr] - '8';
30394 testcase( c==(-1) );
30395 testcase( c==0 );
30396 testcase( c==(+1) );
30397 }
30398 return c;
30399}
30400
30401/*
30402** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
30403** routine does *not* accept hexadecimal notation.
30404**
30405** Returns:
30406**
30407** 0 Successful transformation. Fits in a 64-bit signed integer.
30408** 1 Excess non-space text after the integer value
30409** 2 Integer too large for a 64-bit signed integer or is malformed
30410** 3 Special case of 9223372036854775808
30411**
30412** length is the number of bytes in the string (bytes, not characters).
30413** The string is not necessarily zero-terminated. The encoding is
30414** given by enc.
30415*/
30416SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
30417 int incr;
30418 u64 u = 0;
30419 int neg = 0; /* assume positive */
30420 int i;
30421 int c = 0;
30422 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
30423 int rc; /* Baseline return code */
30424 const char *zStart;
30425 const char *zEnd = zNum + length;
30426 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
30427 if( enc==SQLITE_UTF8 ){
30428 incr = 1;
30429 }else{
30430 incr = 2;
30431 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
30432 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
30433 nonNum = i<length;
30434 zEnd = &zNum[i^1];
30435 zNum += (enc&1);
30436 }
30437 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
30438 if( zNum<zEnd ){
30439 if( *zNum=='-' ){
30440 neg = 1;
30441 zNum+=incr;
30442 }else if( *zNum=='+' ){
30443 zNum+=incr;
30444 }
30445 }
30446 zStart = zNum;
30447 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
30448 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
30449 u = u*10 + c - '0';
30450 }
30451 testcase( i==18*incr );
30452 testcase( i==19*incr );
30453 testcase( i==20*incr );
30454 if( u>LARGEST_INT64 ){
30455 /* This test and assignment is needed only to suppress UB warnings
30456 ** from clang and -fsanitize=undefined. This test and assignment make
30457 ** the code a little larger and slower, and no harm comes from omitting
30458 ** them, but we must appaise the undefined-behavior pharisees. */
30459 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
30460 }else if( neg ){
30461 *pNum = -(i64)u;
30462 }else{
30463 *pNum = (i64)u;
30464 }
30465 rc = 0;
30466 if( (i==0 && zStart==zNum) /* No digits */
30467 || nonNum /* UTF16 with high-order bytes non-zero */
30468 ){
30469 rc = 1;
30470 }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */
30471 int jj = i;
30472 do{
30473 if( !sqlite3Isspace(zNum[jj]) ){
30474 rc = 1; /* Extra non-space text after the integer */
30475 break;
30476 }
30477 jj += incr;
30478 }while( &zNum[jj]<zEnd );
30479 }
30480 if( i<19*incr ){
30481 /* Less than 19 digits, so we know that it fits in 64 bits */
30482 assert( u<=LARGEST_INT64 );
30483 return rc;
30484 }else{
30485 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
30486 c = i>19*incr ? 1 : compare2pow63(zNum, incr);
30487 if( c<0 ){
30488 /* zNum is less than 9223372036854775808 so it fits */
30489 assert( u<=LARGEST_INT64 );
30490 return rc;
30491 }else{
30492 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
30493 if( c>0 ){
30494 /* zNum is greater than 9223372036854775808 so it overflows */
30495 return 2;
30496 }else{
30497 /* zNum is exactly 9223372036854775808. Fits if negative. The
30498 ** special case 2 overflow if positive */
30499 assert( u-1==LARGEST_INT64 );
30500 return neg ? rc : 3;
30501 }
30502 }
30503 }
30504}
30505
30506/*
30507** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
30508** into a 64-bit signed integer. This routine accepts hexadecimal literals,
30509** whereas sqlite3Atoi64() does not.
30510**
30511** Returns:
30512**
30513** 0 Successful transformation. Fits in a 64-bit signed integer.
30514** 1 Excess text after the integer value
30515** 2 Integer too large for a 64-bit signed integer or is malformed
30516** 3 Special case of 9223372036854775808
30517*/
30518SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
30519#ifndef SQLITE_OMIT_HEX_INTEGER
30520 if( z[0]=='0'
30521 && (z[1]=='x' || z[1]=='X')
30522 ){
30523 u64 u = 0;
30524 int i, k;
30525 for(i=2; z[i]=='0'; i++){}
30526 for(k=i; sqlite3Isxdigit(z[k]); k++){
30527 u = u*16 + sqlite3HexToInt(z[k]);
30528 }
30529 memcpy(pOut, &u, 8);
30530 return (z[k]==0 && k-i<=16) ? 0 : 2;
30531 }else
30532#endif /* SQLITE_OMIT_HEX_INTEGER */
30533 {
30534 return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
30535 }
30536}
30537
30538/*
30539** If zNum represents an integer that will fit in 32-bits, then set
30540** *pValue to that integer and return true. Otherwise return false.
30541**
30542** This routine accepts both decimal and hexadecimal notation for integers.
30543**
30544** Any non-numeric characters that following zNum are ignored.
30545** This is different from sqlite3Atoi64() which requires the
30546** input number to be zero-terminated.
30547*/
30548SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
30549 sqlite_int64 v = 0;
30550 int i, c;
30551 int neg = 0;
30552 if( zNum[0]=='-' ){
30553 neg = 1;
30554 zNum++;
30555 }else if( zNum[0]=='+' ){
30556 zNum++;
30557 }
30558#ifndef SQLITE_OMIT_HEX_INTEGER
30559 else if( zNum[0]=='0'
30560 && (zNum[1]=='x' || zNum[1]=='X')
30561 && sqlite3Isxdigit(zNum[2])
30562 ){
30563 u32 u = 0;
30564 zNum += 2;
30565 while( zNum[0]=='0' ) zNum++;
30566 for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){
30567 u = u*16 + sqlite3HexToInt(zNum[i]);
30568 }
30569 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
30570 memcpy(pValue, &u, 4);
30571 return 1;
30572 }else{
30573 return 0;
30574 }
30575 }
30576#endif
30577 if( !sqlite3Isdigit(zNum[0]) ) return 0;
30578 while( zNum[0]=='0' ) zNum++;
30579 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
30580 v = v*10 + c;
30581 }
30582
30583 /* The longest decimal representation of a 32 bit integer is 10 digits:
30584 **
30585 ** 1234567890
30586 ** 2^31 -> 2147483648
30587 */
30588 testcase( i==10 );
30589 if( i>10 ){
30590 return 0;
30591 }
30592 testcase( v-neg==2147483647 );
30593 if( v-neg>2147483647 ){
30594 return 0;
30595 }
30596 if( neg ){
30597 v = -v;
30598 }
30599 *pValue = (int)v;
30600 return 1;
30601}
30602
30603/*
30604** Return a 32-bit integer value extracted from a string. If the
30605** string is not an integer, just return 0.
30606*/
30607SQLITE_PRIVATE int sqlite3Atoi(const char *z){
30608 int x = 0;
30609 if( z ) sqlite3GetInt32(z, &x);
30610 return x;
30611}
30612
30613/*
30614** The variable-length integer encoding is as follows:
30615**
30616** KEY:
30617** A = 0xxxxxxx 7 bits of data and one flag bit
30618** B = 1xxxxxxx 7 bits of data and one flag bit
30619** C = xxxxxxxx 8 bits of data
30620**
30621** 7 bits - A
30622** 14 bits - BA
30623** 21 bits - BBA
30624** 28 bits - BBBA
30625** 35 bits - BBBBA
30626** 42 bits - BBBBBA
30627** 49 bits - BBBBBBA
30628** 56 bits - BBBBBBBA
30629** 64 bits - BBBBBBBBC
30630*/
30631
30632/*
30633** Write a 64-bit variable-length integer to memory starting at p[0].
30634** The length of data write will be between 1 and 9 bytes. The number
30635** of bytes written is returned.
30636**
30637** A variable-length integer consists of the lower 7 bits of each byte
30638** for all bytes that have the 8th bit set and one byte with the 8th
30639** bit clear. Except, if we get to the 9th byte, it stores the full
30640** 8 bits and is the last byte.
30641*/
30642static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){
30643 int i, j, n;
30644 u8 buf[10];
30645 if( v & (((u64)0xff000000)<<32) ){
30646 p[8] = (u8)v;
30647 v >>= 8;
30648 for(i=7; i>=0; i--){
30649 p[i] = (u8)((v & 0x7f) | 0x80);
30650 v >>= 7;
30651 }
30652 return 9;
30653 }
30654 n = 0;
30655 do{
30656 buf[n++] = (u8)((v & 0x7f) | 0x80);
30657 v >>= 7;
30658 }while( v!=0 );
30659 buf[0] &= 0x7f;
30660 assert( n<=9 );
30661 for(i=0, j=n-1; j>=0; j--, i++){
30662 p[i] = buf[j];
30663 }
30664 return n;
30665}
30666SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
30667 if( v<=0x7f ){
30668 p[0] = v&0x7f;
30669 return 1;
30670 }
30671 if( v<=0x3fff ){
30672 p[0] = ((v>>7)&0x7f)|0x80;
30673 p[1] = v&0x7f;
30674 return 2;
30675 }
30676 return putVarint64(p,v);
30677}
30678
30679/*
30680** Bitmasks used by sqlite3GetVarint(). These precomputed constants
30681** are defined here rather than simply putting the constant expressions
30682** inline in order to work around bugs in the RVT compiler.
30683**
30684** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
30685**
30686** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
30687*/
30688#define SLOT_2_0 0x001fc07f
30689#define SLOT_4_2_0 0xf01fc07f
30690
30691
30692/*
30693** Read a 64-bit variable-length integer from memory starting at p[0].
30694** Return the number of bytes read. The value is stored in *v.
30695*/
30696SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
30697 u32 a,b,s;
30698
30699 a = *p;
30700 /* a: p0 (unmasked) */
30701 if (!(a&0x80))
30702 {
30703 *v = a;
30704 return 1;
30705 }
30706
30707 p++;
30708 b = *p;
30709 /* b: p1 (unmasked) */
30710 if (!(b&0x80))
30711 {
30712 a &= 0x7f;
30713 a = a<<7;
30714 a |= b;
30715 *v = a;
30716 return 2;
30717 }
30718
30719 /* Verify that constants are precomputed correctly */
30720 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
30721 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
30722
30723 p++;
30724 a = a<<14;
30725 a |= *p;
30726 /* a: p0<<14 | p2 (unmasked) */
30727 if (!(a&0x80))
30728 {
30729 a &= SLOT_2_0;
30730 b &= 0x7f;
30731 b = b<<7;
30732 a |= b;
30733 *v = a;
30734 return 3;
30735 }
30736
30737 /* CSE1 from below */
30738 a &= SLOT_2_0;
30739 p++;
30740 b = b<<14;
30741 b |= *p;
30742 /* b: p1<<14 | p3 (unmasked) */
30743 if (!(b&0x80))
30744 {
30745 b &= SLOT_2_0;
30746 /* moved CSE1 up */
30747 /* a &= (0x7f<<14)|(0x7f); */
30748 a = a<<7;
30749 a |= b;
30750 *v = a;
30751 return 4;
30752 }
30753
30754 /* a: p0<<14 | p2 (masked) */
30755 /* b: p1<<14 | p3 (unmasked) */
30756 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
30757 /* moved CSE1 up */
30758 /* a &= (0x7f<<14)|(0x7f); */
30759 b &= SLOT_2_0;
30760 s = a;
30761 /* s: p0<<14 | p2 (masked) */
30762
30763 p++;
30764 a = a<<14;
30765 a |= *p;
30766 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
30767 if (!(a&0x80))
30768 {
30769 /* we can skip these cause they were (effectively) done above
30770 ** while calculating s */
30771 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
30772 /* b &= (0x7f<<14)|(0x7f); */
30773 b = b<<7;
30774 a |= b;
30775 s = s>>18;
30776 *v = ((u64)s)<<32 | a;
30777 return 5;
30778 }
30779
30780 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
30781 s = s<<7;
30782 s |= b;
30783 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
30784
30785 p++;
30786 b = b<<14;
30787 b |= *p;
30788 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
30789 if (!(b&0x80))
30790 {
30791 /* we can skip this cause it was (effectively) done above in calc'ing s */
30792 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
30793 a &= SLOT_2_0;
30794 a = a<<7;
30795 a |= b;
30796 s = s>>18;
30797 *v = ((u64)s)<<32 | a;
30798 return 6;
30799 }
30800
30801 p++;
30802 a = a<<14;
30803 a |= *p;
30804 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
30805 if (!(a&0x80))
30806 {
30807 a &= SLOT_4_2_0;
30808 b &= SLOT_2_0;
30809 b = b<<7;
30810 a |= b;
30811 s = s>>11;
30812 *v = ((u64)s)<<32 | a;
30813 return 7;
30814 }
30815
30816 /* CSE2 from below */
30817 a &= SLOT_2_0;
30818 p++;
30819 b = b<<14;
30820 b |= *p;
30821 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
30822 if (!(b&0x80))
30823 {
30824 b &= SLOT_4_2_0;
30825 /* moved CSE2 up */
30826 /* a &= (0x7f<<14)|(0x7f); */
30827 a = a<<7;
30828 a |= b;
30829 s = s>>4;
30830 *v = ((u64)s)<<32 | a;
30831 return 8;
30832 }
30833
30834 p++;
30835 a = a<<15;
30836 a |= *p;
30837 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
30838
30839 /* moved CSE2 up */
30840 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
30841 b &= SLOT_2_0;
30842 b = b<<8;
30843 a |= b;
30844
30845 s = s<<4;
30846 b = p[-4];
30847 b &= 0x7f;
30848 b = b>>3;
30849 s |= b;
30850
30851 *v = ((u64)s)<<32 | a;
30852
30853 return 9;
30854}
30855
30856/*
30857** Read a 32-bit variable-length integer from memory starting at p[0].
30858** Return the number of bytes read. The value is stored in *v.
30859**
30860** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
30861** integer, then set *v to 0xffffffff.
30862**
30863** A MACRO version, getVarint32, is provided which inlines the
30864** single-byte case. All code should use the MACRO version as
30865** this function assumes the single-byte case has already been handled.
30866*/
30867SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
30868 u32 a,b;
30869
30870 /* The 1-byte case. Overwhelmingly the most common. Handled inline
30871 ** by the getVarin32() macro */
30872 a = *p;
30873 /* a: p0 (unmasked) */
30874#ifndef getVarint32
30875 if (!(a&0x80))
30876 {
30877 /* Values between 0 and 127 */
30878 *v = a;
30879 return 1;
30880 }
30881#endif
30882
30883 /* The 2-byte case */
30884 p++;
30885 b = *p;
30886 /* b: p1 (unmasked) */
30887 if (!(b&0x80))
30888 {
30889 /* Values between 128 and 16383 */
30890 a &= 0x7f;
30891 a = a<<7;
30892 *v = a | b;
30893 return 2;
30894 }
30895
30896 /* The 3-byte case */
30897 p++;
30898 a = a<<14;
30899 a |= *p;
30900 /* a: p0<<14 | p2 (unmasked) */
30901 if (!(a&0x80))
30902 {
30903 /* Values between 16384 and 2097151 */
30904 a &= (0x7f<<14)|(0x7f);
30905 b &= 0x7f;
30906 b = b<<7;
30907 *v = a | b;
30908 return 3;
30909 }
30910
30911 /* A 32-bit varint is used to store size information in btrees.
30912 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
30913 ** A 3-byte varint is sufficient, for example, to record the size
30914 ** of a 1048569-byte BLOB or string.
30915 **
30916 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
30917 ** rare larger cases can be handled by the slower 64-bit varint
30918 ** routine.
30919 */
30920#if 1
30921 {
30922 u64 v64;
30923 u8 n;
30924
30925 p -= 2;
30926 n = sqlite3GetVarint(p, &v64);
30927 assert( n>3 && n<=9 );
30928 if( (v64 & SQLITE_MAX_U32)!=v64 ){
30929 *v = 0xffffffff;
30930 }else{
30931 *v = (u32)v64;
30932 }
30933 return n;
30934 }
30935
30936#else
30937 /* For following code (kept for historical record only) shows an
30938 ** unrolling for the 3- and 4-byte varint cases. This code is
30939 ** slightly faster, but it is also larger and much harder to test.
30940 */
30941 p++;
30942 b = b<<14;
30943 b |= *p;
30944 /* b: p1<<14 | p3 (unmasked) */
30945 if (!(b&0x80))
30946 {
30947 /* Values between 2097152 and 268435455 */
30948 b &= (0x7f<<14)|(0x7f);
30949 a &= (0x7f<<14)|(0x7f);
30950 a = a<<7;
30951 *v = a | b;
30952 return 4;
30953 }
30954
30955 p++;
30956 a = a<<14;
30957 a |= *p;
30958 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
30959 if (!(a&0x80))
30960 {
30961 /* Values between 268435456 and 34359738367 */
30962 a &= SLOT_4_2_0;
30963 b &= SLOT_4_2_0;
30964 b = b<<7;
30965 *v = a | b;
30966 return 5;
30967 }
30968
30969 /* We can only reach this point when reading a corrupt database
30970 ** file. In that case we are not in any hurry. Use the (relatively
30971 ** slow) general-purpose sqlite3GetVarint() routine to extract the
30972 ** value. */
30973 {
30974 u64 v64;
30975 u8 n;
30976
30977 p -= 4;
30978 n = sqlite3GetVarint(p, &v64);
30979 assert( n>5 && n<=9 );
30980 *v = (u32)v64;
30981 return n;
30982 }
30983#endif
30984}
30985
30986/*
30987** Return the number of bytes that will be needed to store the given
30988** 64-bit integer.
30989*/
30990SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
30991 int i;
30992 for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); }
30993 return i;
30994}
30995
30996
30997/*
30998** Read or write a four-byte big-endian integer value.
30999*/
31000SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
31001#if SQLITE_BYTEORDER==4321
31002 u32 x;
31003 memcpy(&x,p,4);
31004 return x;
31005#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
31006 u32 x;
31007 memcpy(&x,p,4);
31008 return __builtin_bswap32(x);
31009#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
31010 u32 x;
31011 memcpy(&x,p,4);
31012 return _byteswap_ulong(x);
31013#else
31014 testcase( p[0]&0x80 );
31015 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
31016#endif
31017}
31018SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
31019#if SQLITE_BYTEORDER==4321
31020 memcpy(p,&v,4);
31021#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
31022 u32 x = __builtin_bswap32(v);
31023 memcpy(p,&x,4);
31024#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
31025 u32 x = _byteswap_ulong(v);
31026 memcpy(p,&x,4);
31027#else
31028 p[0] = (u8)(v>>24);
31029 p[1] = (u8)(v>>16);
31030 p[2] = (u8)(v>>8);
31031 p[3] = (u8)v;
31032#endif
31033}
31034
31035
31036
31037/*
31038** Translate a single byte of Hex into an integer.
31039** This routine only works if h really is a valid hexadecimal
31040** character: 0..9a..fA..F
31041*/
31042SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
31043 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
31044#ifdef SQLITE_ASCII
31045 h += 9*(1&(h>>6));
31046#endif
31047#ifdef SQLITE_EBCDIC
31048 h += 9*(1&~(h>>4));
31049#endif
31050 return (u8)(h & 0xf);
31051}
31052
31053#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
31054/*
31055** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
31056** value. Return a pointer to its binary value. Space to hold the
31057** binary value has been obtained from malloc and must be freed by
31058** the calling routine.
31059*/
31060SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
31061 char *zBlob;
31062 int i;
31063
31064 zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
31065 n--;
31066 if( zBlob ){
31067 for(i=0; i<n; i+=2){
31068 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
31069 }
31070 zBlob[i/2] = 0;
31071 }
31072 return zBlob;
31073}
31074#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
31075
31076/*
31077** Log an error that is an API call on a connection pointer that should
31078** not have been used. The "type" of connection pointer is given as the
31079** argument. The zType is a word like "NULL" or "closed" or "invalid".
31080*/
31081static void logBadConnection(const char *zType){
31082 sqlite3_log(SQLITE_MISUSE,
31083 "API call with %s database connection pointer",
31084 zType
31085 );
31086}
31087
31088/*
31089** Check to make sure we have a valid db pointer. This test is not
31090** foolproof but it does provide some measure of protection against
31091** misuse of the interface such as passing in db pointers that are
31092** NULL or which have been previously closed. If this routine returns
31093** 1 it means that the db pointer is valid and 0 if it should not be
31094** dereferenced for any reason. The calling function should invoke
31095** SQLITE_MISUSE immediately.
31096**
31097** sqlite3SafetyCheckOk() requires that the db pointer be valid for
31098** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
31099** open properly and is not fit for general use but which can be
31100** used as an argument to sqlite3_errmsg() or sqlite3_close().
31101*/
31102SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
31103 u32 magic;
31104 if( db==0 ){
31105 logBadConnection("NULL");
31106 return 0;
31107 }
31108 magic = db->magic;
31109 if( magic!=SQLITE_MAGIC_OPEN ){
31110 if( sqlite3SafetyCheckSickOrOk(db) ){
31111 testcase( sqlite3GlobalConfig.xLog!=0 );
31112 logBadConnection("unopened");
31113 }
31114 return 0;
31115 }else{
31116 return 1;
31117 }
31118}
31119SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
31120 u32 magic;
31121 magic = db->magic;
31122 if( magic!=SQLITE_MAGIC_SICK &&
31123 magic!=SQLITE_MAGIC_OPEN &&
31124 magic!=SQLITE_MAGIC_BUSY ){
31125 testcase( sqlite3GlobalConfig.xLog!=0 );
31126 logBadConnection("invalid");
31127 return 0;
31128 }else{
31129 return 1;
31130 }
31131}
31132
31133/*
31134** Attempt to add, substract, or multiply the 64-bit signed value iB against
31135** the other 64-bit signed integer at *pA and store the result in *pA.
31136** Return 0 on success. Or if the operation would have resulted in an
31137** overflow, leave *pA unchanged and return 1.
31138*/
31139SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
31140#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
31141 return __builtin_add_overflow(*pA, iB, pA);
31142#else
31143 i64 iA = *pA;
31144 testcase( iA==0 ); testcase( iA==1 );
31145 testcase( iB==-1 ); testcase( iB==0 );
31146 if( iB>=0 ){
31147 testcase( iA>0 && LARGEST_INT64 - iA == iB );
31148 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
31149 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
31150 }else{
31151 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
31152 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
31153 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
31154 }
31155 *pA += iB;
31156 return 0;
31157#endif
31158}
31159SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
31160#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
31161 return __builtin_sub_overflow(*pA, iB, pA);
31162#else
31163 testcase( iB==SMALLEST_INT64+1 );
31164 if( iB==SMALLEST_INT64 ){
31165 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
31166 if( (*pA)>=0 ) return 1;
31167 *pA -= iB;
31168 return 0;
31169 }else{
31170 return sqlite3AddInt64(pA, -iB);
31171 }
31172#endif
31173}
31174SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
31175#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
31176 return __builtin_mul_overflow(*pA, iB, pA);
31177#else
31178 i64 iA = *pA;
31179 if( iB>0 ){
31180 if( iA>LARGEST_INT64/iB ) return 1;
31181 if( iA<SMALLEST_INT64/iB ) return 1;
31182 }else if( iB<0 ){
31183 if( iA>0 ){
31184 if( iB<SMALLEST_INT64/iA ) return 1;
31185 }else if( iA<0 ){
31186 if( iB==SMALLEST_INT64 ) return 1;
31187 if( iA==SMALLEST_INT64 ) return 1;
31188 if( -iA>LARGEST_INT64/-iB ) return 1;
31189 }
31190 }
31191 *pA = iA*iB;
31192 return 0;
31193#endif
31194}
31195
31196/*
31197** Compute the absolute value of a 32-bit signed integer, of possible. Or
31198** if the integer has a value of -2147483648, return +2147483647
31199*/
31200SQLITE_PRIVATE int sqlite3AbsInt32(int x){
31201 if( x>=0 ) return x;
31202 if( x==(int)0x80000000 ) return 0x7fffffff;
31203 return -x;
31204}
31205
31206#ifdef SQLITE_ENABLE_8_3_NAMES
31207/*
31208** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
31209** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
31210** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
31211** three characters, then shorten the suffix on z[] to be the last three
31212** characters of the original suffix.
31213**
31214** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
31215** do the suffix shortening regardless of URI parameter.
31216**
31217** Examples:
31218**
31219** test.db-journal => test.nal
31220** test.db-wal => test.wal
31221** test.db-shm => test.shm
31222** test.db-mj7f3319fa => test.9fa
31223*/
31224SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
31225#if SQLITE_ENABLE_8_3_NAMES<2
31226 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
31227#endif
31228 {
31229 int i, sz;
31230 sz = sqlite3Strlen30(z);
31231 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
31232 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
31233 }
31234}
31235#endif
31236
31237/*
31238** Find (an approximate) sum of two LogEst values. This computation is
31239** not a simple "+" operator because LogEst is stored as a logarithmic
31240** value.
31241**
31242*/
31243SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
31244 static const unsigned char x[] = {
31245 10, 10, /* 0,1 */
31246 9, 9, /* 2,3 */
31247 8, 8, /* 4,5 */
31248 7, 7, 7, /* 6,7,8 */
31249 6, 6, 6, /* 9,10,11 */
31250 5, 5, 5, /* 12-14 */
31251 4, 4, 4, 4, /* 15-18 */
31252 3, 3, 3, 3, 3, 3, /* 19-24 */
31253 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
31254 };
31255 if( a>=b ){
31256 if( a>b+49 ) return a;
31257 if( a>b+31 ) return a+1;
31258 return a+x[a-b];
31259 }else{
31260 if( b>a+49 ) return b;
31261 if( b>a+31 ) return b+1;
31262 return b+x[b-a];
31263 }
31264}
31265
31266/*
31267** Convert an integer into a LogEst. In other words, compute an
31268** approximation for 10*log2(x).
31269*/
31270SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
31271 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
31272 LogEst y = 40;
31273 if( x<8 ){
31274 if( x<2 ) return 0;
31275 while( x<8 ){ y -= 10; x <<= 1; }
31276 }else{
31277#if GCC_VERSION>=5004000
31278 int i = 60 - __builtin_clzll(x);
31279 y += i*10;
31280 x >>= i;
31281#else
31282 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
31283 while( x>15 ){ y += 10; x >>= 1; }
31284#endif
31285 }
31286 return a[x&7] + y - 10;
31287}
31288
31289#ifndef SQLITE_OMIT_VIRTUALTABLE
31290/*
31291** Convert a double into a LogEst
31292** In other words, compute an approximation for 10*log2(x).
31293*/
31294SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
31295 u64 a;
31296 LogEst e;
31297 assert( sizeof(x)==8 && sizeof(a)==8 );
31298 if( x<=1 ) return 0;
31299 if( x<=2000000000 ) return sqlite3LogEst((u64)x);
31300 memcpy(&a, &x, 8);
31301 e = (a>>52) - 1022;
31302 return e*10;
31303}
31304#endif /* SQLITE_OMIT_VIRTUALTABLE */
31305
31306#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
31307 defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
31308 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
31309/*
31310** Convert a LogEst into an integer.
31311**
31312** Note that this routine is only used when one or more of various
31313** non-standard compile-time options is enabled.
31314*/
31315SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
31316 u64 n;
31317 n = x%10;
31318 x /= 10;
31319 if( n>=5 ) n -= 2;
31320 else if( n>=1 ) n -= 1;
31321#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
31322 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
31323 if( x>60 ) return (u64)LARGEST_INT64;
31324#else
31325 /* If only SQLITE_ENABLE_STAT3_OR_STAT4 is on, then the largest input
31326 ** possible to this routine is 310, resulting in a maximum x of 31 */
31327 assert( x<=60 );
31328#endif
31329 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
31330}
31331#endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */
31332
31333/*
31334** Add a new name/number pair to a VList. This might require that the
31335** VList object be reallocated, so return the new VList. If an OOM
31336** error occurs, the original VList returned and the
31337** db->mallocFailed flag is set.
31338**
31339** A VList is really just an array of integers. To destroy a VList,
31340** simply pass it to sqlite3DbFree().
31341**
31342** The first integer is the number of integers allocated for the whole
31343** VList. The second integer is the number of integers actually used.
31344** Each name/number pair is encoded by subsequent groups of 3 or more
31345** integers.
31346**
31347** Each name/number pair starts with two integers which are the numeric
31348** value for the pair and the size of the name/number pair, respectively.
31349** The text name overlays one or more following integers. The text name
31350** is always zero-terminated.
31351**
31352** Conceptually:
31353**
31354** struct VList {
31355** int nAlloc; // Number of allocated slots
31356** int nUsed; // Number of used slots
31357** struct VListEntry {
31358** int iValue; // Value for this entry
31359** int nSlot; // Slots used by this entry
31360** // ... variable name goes here
31361** } a[0];
31362** }
31363**
31364** During code generation, pointers to the variable names within the
31365** VList are taken. When that happens, nAlloc is set to zero as an
31366** indication that the VList may never again be enlarged, since the
31367** accompanying realloc() would invalidate the pointers.
31368*/
31369SQLITE_PRIVATE VList *sqlite3VListAdd(
31370 sqlite3 *db, /* The database connection used for malloc() */
31371 VList *pIn, /* The input VList. Might be NULL */
31372 const char *zName, /* Name of symbol to add */
31373 int nName, /* Bytes of text in zName */
31374 int iVal /* Value to associate with zName */
31375){
31376 int nInt; /* number of sizeof(int) objects needed for zName */
31377 char *z; /* Pointer to where zName will be stored */
31378 int i; /* Index in pIn[] where zName is stored */
31379
31380 nInt = nName/4 + 3;
31381 assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */
31382 if( pIn==0 || pIn[1]+nInt > pIn[0] ){
31383 /* Enlarge the allocation */
31384 int nAlloc = (pIn ? pIn[0]*2 : 10) + nInt;
31385 VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
31386 if( pOut==0 ) return pIn;
31387 if( pIn==0 ) pOut[1] = 2;
31388 pIn = pOut;
31389 pIn[0] = nAlloc;
31390 }
31391 i = pIn[1];
31392 pIn[i] = iVal;
31393 pIn[i+1] = nInt;
31394 z = (char*)&pIn[i+2];
31395 pIn[1] = i+nInt;
31396 assert( pIn[1]<=pIn[0] );
31397 memcpy(z, zName, nName);
31398 z[nName] = 0;
31399 return pIn;
31400}
31401
31402/*
31403** Return a pointer to the name of a variable in the given VList that
31404** has the value iVal. Or return a NULL if there is no such variable in
31405** the list
31406*/
31407SQLITE_PRIVATE const char *sqlite3VListNumToName(VList *pIn, int iVal){
31408 int i, mx;
31409 if( pIn==0 ) return 0;
31410 mx = pIn[1];
31411 i = 2;
31412 do{
31413 if( pIn[i]==iVal ) return (char*)&pIn[i+2];
31414 i += pIn[i+1];
31415 }while( i<mx );
31416 return 0;
31417}
31418
31419/*
31420** Return the number of the variable named zName, if it is in VList.
31421** or return 0 if there is no such variable.
31422*/
31423SQLITE_PRIVATE int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
31424 int i, mx;
31425 if( pIn==0 ) return 0;
31426 mx = pIn[1];
31427 i = 2;
31428 do{
31429 const char *z = (const char*)&pIn[i+2];
31430 if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
31431 i += pIn[i+1];
31432 }while( i<mx );
31433 return 0;
31434}
31435
31436/************** End of util.c ************************************************/
31437/************** Begin file hash.c ********************************************/
31438/*
31439** 2001 September 22
31440**
31441** The author disclaims copyright to this source code. In place of
31442** a legal notice, here is a blessing:
31443**
31444** May you do good and not evil.
31445** May you find forgiveness for yourself and forgive others.
31446** May you share freely, never taking more than you give.
31447**
31448*************************************************************************
31449** This is the implementation of generic hash-tables
31450** used in SQLite.
31451*/
31452/* #include "sqliteInt.h" */
31453/* #include <assert.h> */
31454
31455/* Turn bulk memory into a hash table object by initializing the
31456** fields of the Hash structure.
31457**
31458** "pNew" is a pointer to the hash table that is to be initialized.
31459*/
31460SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
31461 assert( pNew!=0 );
31462 pNew->first = 0;
31463 pNew->count = 0;
31464 pNew->htsize = 0;
31465 pNew->ht = 0;
31466}
31467
31468/* Remove all entries from a hash table. Reclaim all memory.
31469** Call this routine to delete a hash table or to reset a hash table
31470** to the empty state.
31471*/
31472SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
31473 HashElem *elem; /* For looping over all elements of the table */
31474
31475 assert( pH!=0 );
31476 elem = pH->first;
31477 pH->first = 0;
31478 sqlite3_free(pH->ht);
31479 pH->ht = 0;
31480 pH->htsize = 0;
31481 while( elem ){
31482 HashElem *next_elem = elem->next;
31483 sqlite3_free(elem);
31484 elem = next_elem;
31485 }
31486 pH->count = 0;
31487}
31488
31489/*
31490** The hashing function.
31491*/
31492static unsigned int strHash(const char *z){
31493 unsigned int h = 0;
31494 unsigned char c;
31495 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
31496 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
31497 ** 0x9e3779b1 is 2654435761 which is the closest prime number to
31498 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
31499 h += sqlite3UpperToLower[c];
31500 h *= 0x9e3779b1;
31501 }
31502 return h;
31503}
31504
31505
31506/* Link pNew element into the hash table pH. If pEntry!=0 then also
31507** insert pNew into the pEntry hash bucket.
31508*/
31509static void insertElement(
31510 Hash *pH, /* The complete hash table */
31511 struct _ht *pEntry, /* The entry into which pNew is inserted */
31512 HashElem *pNew /* The element to be inserted */
31513){
31514 HashElem *pHead; /* First element already in pEntry */
31515 if( pEntry ){
31516 pHead = pEntry->count ? pEntry->chain : 0;
31517 pEntry->count++;
31518 pEntry->chain = pNew;
31519 }else{
31520 pHead = 0;
31521 }
31522 if( pHead ){
31523 pNew->next = pHead;
31524 pNew->prev = pHead->prev;
31525 if( pHead->prev ){ pHead->prev->next = pNew; }
31526 else { pH->first = pNew; }
31527 pHead->prev = pNew;
31528 }else{
31529 pNew->next = pH->first;
31530 if( pH->first ){ pH->first->prev = pNew; }
31531 pNew->prev = 0;
31532 pH->first = pNew;
31533 }
31534}
31535
31536
31537/* Resize the hash table so that it cantains "new_size" buckets.
31538**
31539** The hash table might fail to resize if sqlite3_malloc() fails or
31540** if the new size is the same as the prior size.
31541** Return TRUE if the resize occurs and false if not.
31542*/
31543static int rehash(Hash *pH, unsigned int new_size){
31544 struct _ht *new_ht; /* The new hash table */
31545 HashElem *elem, *next_elem; /* For looping over existing elements */
31546
31547#if SQLITE_MALLOC_SOFT_LIMIT>0
31548 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
31549 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
31550 }
31551 if( new_size==pH->htsize ) return 0;
31552#endif
31553
31554 /* The inability to allocates space for a larger hash table is
31555 ** a performance hit but it is not a fatal error. So mark the
31556 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
31557 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
31558 ** only zeroes the requested number of bytes whereas this module will
31559 ** use the actual amount of space allocated for the hash table (which
31560 ** may be larger than the requested amount).
31561 */
31562 sqlite3BeginBenignMalloc();
31563 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
31564 sqlite3EndBenignMalloc();
31565
31566 if( new_ht==0 ) return 0;
31567 sqlite3_free(pH->ht);
31568 pH->ht = new_ht;
31569 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
31570 memset(new_ht, 0, new_size*sizeof(struct _ht));
31571 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
31572 unsigned int h = strHash(elem->pKey) % new_size;
31573 next_elem = elem->next;
31574 insertElement(pH, &new_ht[h], elem);
31575 }
31576 return 1;
31577}
31578
31579/* This function (for internal use only) locates an element in an
31580** hash table that matches the given key. If no element is found,
31581** a pointer to a static null element with HashElem.data==0 is returned.
31582** If pH is not NULL, then the hash for this key is written to *pH.
31583*/
31584static HashElem *findElementWithHash(
31585 const Hash *pH, /* The pH to be searched */
31586 const char *pKey, /* The key we are searching for */
31587 unsigned int *pHash /* Write the hash value here */
31588){
31589 HashElem *elem; /* Used to loop thru the element list */
31590 int count; /* Number of elements left to test */
31591 unsigned int h; /* The computed hash */
31592 static HashElem nullElement = { 0, 0, 0, 0 };
31593
31594 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
31595 struct _ht *pEntry;
31596 h = strHash(pKey) % pH->htsize;
31597 pEntry = &pH->ht[h];
31598 elem = pEntry->chain;
31599 count = pEntry->count;
31600 }else{
31601 h = 0;
31602 elem = pH->first;
31603 count = pH->count;
31604 }
31605 if( pHash ) *pHash = h;
31606 while( count-- ){
31607 assert( elem!=0 );
31608 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
31609 return elem;
31610 }
31611 elem = elem->next;
31612 }
31613 return &nullElement;
31614}
31615
31616/* Remove a single entry from the hash table given a pointer to that
31617** element and a hash on the element's key.
31618*/
31619static void removeElementGivenHash(
31620 Hash *pH, /* The pH containing "elem" */
31621 HashElem* elem, /* The element to be removed from the pH */
31622 unsigned int h /* Hash value for the element */
31623){
31624 struct _ht *pEntry;
31625 if( elem->prev ){
31626 elem->prev->next = elem->next;
31627 }else{
31628 pH->first = elem->next;
31629 }
31630 if( elem->next ){
31631 elem->next->prev = elem->prev;
31632 }
31633 if( pH->ht ){
31634 pEntry = &pH->ht[h];
31635 if( pEntry->chain==elem ){
31636 pEntry->chain = elem->next;
31637 }
31638 pEntry->count--;
31639 assert( pEntry->count>=0 );
31640 }
31641 sqlite3_free( elem );
31642 pH->count--;
31643 if( pH->count==0 ){
31644 assert( pH->first==0 );
31645 assert( pH->count==0 );
31646 sqlite3HashClear(pH);
31647 }
31648}
31649
31650/* Attempt to locate an element of the hash table pH with a key
31651** that matches pKey. Return the data for this element if it is
31652** found, or NULL if there is no match.
31653*/
31654SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){
31655 assert( pH!=0 );
31656 assert( pKey!=0 );
31657 return findElementWithHash(pH, pKey, 0)->data;
31658}
31659
31660/* Insert an element into the hash table pH. The key is pKey
31661** and the data is "data".
31662**
31663** If no element exists with a matching key, then a new
31664** element is created and NULL is returned.
31665**
31666** If another element already exists with the same key, then the
31667** new data replaces the old data and the old data is returned.
31668** The key is not copied in this instance. If a malloc fails, then
31669** the new data is returned and the hash table is unchanged.
31670**
31671** If the "data" parameter to this function is NULL, then the
31672** element corresponding to "key" is removed from the hash table.
31673*/
31674SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
31675 unsigned int h; /* the hash of the key modulo hash table size */
31676 HashElem *elem; /* Used to loop thru the element list */
31677 HashElem *new_elem; /* New element added to the pH */
31678
31679 assert( pH!=0 );
31680 assert( pKey!=0 );
31681 elem = findElementWithHash(pH,pKey,&h);
31682 if( elem->data ){
31683 void *old_data = elem->data;
31684 if( data==0 ){
31685 removeElementGivenHash(pH,elem,h);
31686 }else{
31687 elem->data = data;
31688 elem->pKey = pKey;
31689 }
31690 return old_data;
31691 }
31692 if( data==0 ) return 0;
31693 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
31694 if( new_elem==0 ) return data;
31695 new_elem->pKey = pKey;
31696 new_elem->data = data;
31697 pH->count++;
31698 if( pH->count>=10 && pH->count > 2*pH->htsize ){
31699 if( rehash(pH, pH->count*2) ){
31700 assert( pH->htsize>0 );
31701 h = strHash(pKey) % pH->htsize;
31702 }
31703 }
31704 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
31705 return 0;
31706}
31707
31708/************** End of hash.c ************************************************/
31709/************** Begin file opcodes.c *****************************************/
31710/* Automatically generated. Do not edit */
31711/* See the tool/mkopcodec.tcl script for details. */
31712#if !defined(SQLITE_OMIT_EXPLAIN) \
31713 || defined(VDBE_PROFILE) \
31714 || defined(SQLITE_DEBUG)
31715#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
31716# define OpHelp(X) "\0" X
31717#else
31718# define OpHelp(X)
31719#endif
31720SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
31721 static const char *const azName[] = {
31722 /* 0 */ "Savepoint" OpHelp(""),
31723 /* 1 */ "AutoCommit" OpHelp(""),
31724 /* 2 */ "Transaction" OpHelp(""),
31725 /* 3 */ "SorterNext" OpHelp(""),
31726 /* 4 */ "Prev" OpHelp(""),
31727 /* 5 */ "Next" OpHelp(""),
31728 /* 6 */ "Checkpoint" OpHelp(""),
31729 /* 7 */ "JournalMode" OpHelp(""),
31730 /* 8 */ "Vacuum" OpHelp(""),
31731 /* 9 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
31732 /* 10 */ "VUpdate" OpHelp("data=r[P3@P2]"),
31733 /* 11 */ "Goto" OpHelp(""),
31734 /* 12 */ "Gosub" OpHelp(""),
31735 /* 13 */ "InitCoroutine" OpHelp(""),
31736 /* 14 */ "Yield" OpHelp(""),
31737 /* 15 */ "MustBeInt" OpHelp(""),
31738 /* 16 */ "Jump" OpHelp(""),
31739 /* 17 */ "Once" OpHelp(""),
31740 /* 18 */ "If" OpHelp(""),
31741 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
31742 /* 20 */ "IfNot" OpHelp(""),
31743 /* 21 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
31744 /* 22 */ "SeekLT" OpHelp("key=r[P3@P4]"),
31745 /* 23 */ "SeekLE" OpHelp("key=r[P3@P4]"),
31746 /* 24 */ "SeekGE" OpHelp("key=r[P3@P4]"),
31747 /* 25 */ "SeekGT" OpHelp("key=r[P3@P4]"),
31748 /* 26 */ "IfNoHope" OpHelp("key=r[P3@P4]"),
31749 /* 27 */ "NoConflict" OpHelp("key=r[P3@P4]"),
31750 /* 28 */ "NotFound" OpHelp("key=r[P3@P4]"),
31751 /* 29 */ "Found" OpHelp("key=r[P3@P4]"),
31752 /* 30 */ "SeekRowid" OpHelp("intkey=r[P3]"),
31753 /* 31 */ "NotExists" OpHelp("intkey=r[P3]"),
31754 /* 32 */ "Last" OpHelp(""),
31755 /* 33 */ "IfSmaller" OpHelp(""),
31756 /* 34 */ "SorterSort" OpHelp(""),
31757 /* 35 */ "Sort" OpHelp(""),
31758 /* 36 */ "Rewind" OpHelp(""),
31759 /* 37 */ "IdxLE" OpHelp("key=r[P3@P4]"),
31760 /* 38 */ "IdxGT" OpHelp("key=r[P3@P4]"),
31761 /* 39 */ "IdxLT" OpHelp("key=r[P3@P4]"),
31762 /* 40 */ "IdxGE" OpHelp("key=r[P3@P4]"),
31763 /* 41 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
31764 /* 42 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
31765 /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
31766 /* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
31767 /* 45 */ "Program" OpHelp(""),
31768 /* 46 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
31769 /* 47 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
31770 /* 48 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
31771 /* 49 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
31772 /* 50 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
31773 /* 51 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
31774 /* 52 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
31775 /* 53 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
31776 /* 54 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
31777 /* 55 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
31778 /* 56 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
31779 /* 57 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
31780 /* 58 */ "ElseNotEq" OpHelp(""),
31781 /* 59 */ "IncrVacuum" OpHelp(""),
31782 /* 60 */ "VNext" OpHelp(""),
31783 /* 61 */ "Init" OpHelp("Start at P2"),
31784 /* 62 */ "PureFunc0" OpHelp(""),
31785 /* 63 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
31786 /* 64 */ "PureFunc" OpHelp(""),
31787 /* 65 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
31788 /* 66 */ "Return" OpHelp(""),
31789 /* 67 */ "EndCoroutine" OpHelp(""),
31790 /* 68 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
31791 /* 69 */ "Halt" OpHelp(""),
31792 /* 70 */ "Integer" OpHelp("r[P2]=P1"),
31793 /* 71 */ "Int64" OpHelp("r[P2]=P4"),
31794 /* 72 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
31795 /* 73 */ "Null" OpHelp("r[P2..P3]=NULL"),
31796 /* 74 */ "SoftNull" OpHelp("r[P1]=NULL"),
31797 /* 75 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
31798 /* 76 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
31799 /* 77 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
31800 /* 78 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
31801 /* 79 */ "SCopy" OpHelp("r[P2]=r[P1]"),
31802 /* 80 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
31803 /* 81 */ "ResultRow" OpHelp("output=r[P1@P2]"),
31804 /* 82 */ "CollSeq" OpHelp(""),
31805 /* 83 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
31806 /* 84 */ "RealAffinity" OpHelp(""),
31807 /* 85 */ "Cast" OpHelp("affinity(r[P1])"),
31808 /* 86 */ "Permutation" OpHelp(""),
31809 /* 87 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
31810 /* 88 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
31811 /* 89 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
31812 /* 90 */ "Column" OpHelp("r[P3]=PX"),
31813 /* 91 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
31814 /* 92 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
31815 /* 93 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
31816 /* 94 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
31817 /* 95 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
31818 /* 96 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
31819 /* 97 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
31820 /* 98 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
31821 /* 99 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
31822 /* 100 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
31823 /* 101 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
31824 /* 102 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
31825 /* 103 */ "BitNot" OpHelp("r[P2]= ~r[P1]"),
31826 /* 104 */ "Count" OpHelp("r[P2]=count()"),
31827 /* 105 */ "ReadCookie" OpHelp(""),
31828 /* 106 */ "String8" OpHelp("r[P2]='P4'"),
31829 /* 107 */ "SetCookie" OpHelp(""),
31830 /* 108 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
31831 /* 109 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
31832 /* 110 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
31833 /* 111 */ "OpenDup" OpHelp(""),
31834 /* 112 */ "OpenAutoindex" OpHelp("nColumn=P2"),
31835 /* 113 */ "OpenEphemeral" OpHelp("nColumn=P2"),
31836 /* 114 */ "SorterOpen" OpHelp(""),
31837 /* 115 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
31838 /* 116 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
31839 /* 117 */ "Close" OpHelp(""),
31840 /* 118 */ "ColumnsUsed" OpHelp(""),
31841 /* 119 */ "SeekHit" OpHelp("seekHit=P2"),
31842 /* 120 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
31843 /* 121 */ "NewRowid" OpHelp("r[P2]=rowid"),
31844 /* 122 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
31845 /* 123 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
31846 /* 124 */ "Delete" OpHelp(""),
31847 /* 125 */ "ResetCount" OpHelp(""),
31848 /* 126 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
31849 /* 127 */ "SorterData" OpHelp("r[P2]=data"),
31850 /* 128 */ "RowData" OpHelp("r[P2]=data"),
31851 /* 129 */ "Rowid" OpHelp("r[P2]=rowid"),
31852 /* 130 */ "NullRow" OpHelp(""),
31853 /* 131 */ "SeekEnd" OpHelp(""),
31854 /* 132 */ "SorterInsert" OpHelp("key=r[P2]"),
31855 /* 133 */ "IdxInsert" OpHelp("key=r[P2]"),
31856 /* 134 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
31857 /* 135 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"),
31858 /* 136 */ "IdxRowid" OpHelp("r[P2]=rowid"),
31859 /* 137 */ "Destroy" OpHelp(""),
31860 /* 138 */ "Clear" OpHelp(""),
31861 /* 139 */ "ResetSorter" OpHelp(""),
31862 /* 140 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
31863 /* 141 */ "Real" OpHelp("r[P2]=P4"),
31864 /* 142 */ "SqlExec" OpHelp(""),
31865 /* 143 */ "ParseSchema" OpHelp(""),
31866 /* 144 */ "LoadAnalysis" OpHelp(""),
31867 /* 145 */ "DropTable" OpHelp(""),
31868 /* 146 */ "DropIndex" OpHelp(""),
31869 /* 147 */ "DropTrigger" OpHelp(""),
31870 /* 148 */ "IntegrityCk" OpHelp(""),
31871 /* 149 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
31872 /* 150 */ "Param" OpHelp(""),
31873 /* 151 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
31874 /* 152 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
31875 /* 153 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
31876 /* 154 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"),
31877 /* 155 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
31878 /* 156 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"),
31879 /* 157 */ "AggValue" OpHelp("r[P3]=value N=P2"),
31880 /* 158 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
31881 /* 159 */ "Expire" OpHelp(""),
31882 /* 160 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
31883 /* 161 */ "VBegin" OpHelp(""),
31884 /* 162 */ "VCreate" OpHelp(""),
31885 /* 163 */ "VDestroy" OpHelp(""),
31886 /* 164 */ "VOpen" OpHelp(""),
31887 /* 165 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
31888 /* 166 */ "VRename" OpHelp(""),
31889 /* 167 */ "Pagecount" OpHelp(""),
31890 /* 168 */ "MaxPgcnt" OpHelp(""),
31891 /* 169 */ "Trace" OpHelp(""),
31892 /* 170 */ "CursorHint" OpHelp(""),
31893 /* 171 */ "Noop" OpHelp(""),
31894 /* 172 */ "Explain" OpHelp(""),
31895 /* 173 */ "Abortable" OpHelp(""),
31896 };
31897 return azName[i];
31898}
31899#endif
31900
31901/************** End of opcodes.c *********************************************/
31902/************** Begin file os_unix.c *****************************************/
31903/*
31904** 2004 May 22
31905**
31906** The author disclaims copyright to this source code. In place of
31907** a legal notice, here is a blessing:
31908**
31909** May you do good and not evil.
31910** May you find forgiveness for yourself and forgive others.
31911** May you share freely, never taking more than you give.
31912**
31913******************************************************************************
31914**
31915** This file contains the VFS implementation for unix-like operating systems
31916** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
31917**
31918** There are actually several different VFS implementations in this file.
31919** The differences are in the way that file locking is done. The default
31920** implementation uses Posix Advisory Locks. Alternative implementations
31921** use flock(), dot-files, various proprietary locking schemas, or simply
31922** skip locking all together.
31923**
31924** This source file is organized into divisions where the logic for various
31925** subfunctions is contained within the appropriate division. PLEASE
31926** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
31927** in the correct division and should be clearly labeled.
31928**
31929** The layout of divisions is as follows:
31930**
31931** * General-purpose declarations and utility functions.
31932** * Unique file ID logic used by VxWorks.
31933** * Various locking primitive implementations (all except proxy locking):
31934** + for Posix Advisory Locks
31935** + for no-op locks
31936** + for dot-file locks
31937** + for flock() locking
31938** + for named semaphore locks (VxWorks only)
31939** + for AFP filesystem locks (MacOSX only)
31940** * sqlite3_file methods not associated with locking.
31941** * Definitions of sqlite3_io_methods objects for all locking
31942** methods plus "finder" functions for each locking method.
31943** * sqlite3_vfs method implementations.
31944** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
31945** * Definitions of sqlite3_vfs objects for all locking methods
31946** plus implementations of sqlite3_os_init() and sqlite3_os_end().
31947*/
31948/* #include "sqliteInt.h" */
31949#if SQLITE_OS_UNIX /* This file is used on unix only */
31950
31951/*
31952** There are various methods for file locking used for concurrency
31953** control:
31954**
31955** 1. POSIX locking (the default),
31956** 2. No locking,
31957** 3. Dot-file locking,
31958** 4. flock() locking,
31959** 5. AFP locking (OSX only),
31960** 6. Named POSIX semaphores (VXWorks only),
31961** 7. proxy locking. (OSX only)
31962**
31963** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
31964** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
31965** selection of the appropriate locking style based on the filesystem
31966** where the database is located.
31967*/
31968#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
31969# if defined(__APPLE__)
31970# define SQLITE_ENABLE_LOCKING_STYLE 1
31971# else
31972# define SQLITE_ENABLE_LOCKING_STYLE 0
31973# endif
31974#endif
31975
31976/* Use pread() and pwrite() if they are available */
31977#if defined(__APPLE__)
31978# define HAVE_PREAD 1
31979# define HAVE_PWRITE 1
31980#endif
31981#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
31982# undef USE_PREAD
31983# define USE_PREAD64 1
31984#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
31985# undef USE_PREAD64
31986# define USE_PREAD 1
31987#endif
31988
31989/*
31990** standard include files.
31991*/
31992#include <sys/types.h>
31993#include <sys/stat.h>
31994#include <fcntl.h>
31995#include <sys/ioctl.h>
31996#include <unistd.h>
31997/* #include <time.h> */
31998#include <sys/time.h>
31999#include <errno.h>
32000#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
32001# include <sys/mman.h>
32002#endif
32003
32004#if SQLITE_ENABLE_LOCKING_STYLE
32005/* # include <sys/ioctl.h> */
32006# include <sys/file.h>
32007# include <sys/param.h>
32008#endif /* SQLITE_ENABLE_LOCKING_STYLE */
32009
32010#if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
32011 (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
32012# if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
32013 && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))
32014# define HAVE_GETHOSTUUID 1
32015# else
32016# warning "gethostuuid() is disabled."
32017# endif
32018#endif
32019
32020
32021#if OS_VXWORKS
32022/* # include <sys/ioctl.h> */
32023# include <semaphore.h>
32024# include <limits.h>
32025#endif /* OS_VXWORKS */
32026
32027#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
32028# include <sys/mount.h>
32029#endif
32030
32031#ifdef HAVE_UTIME
32032# include <utime.h>
32033#endif
32034
32035/*
32036** Allowed values of unixFile.fsFlags
32037*/
32038#define SQLITE_FSFLAGS_IS_MSDOS 0x1
32039
32040/*
32041** If we are to be thread-safe, include the pthreads header and define
32042** the SQLITE_UNIX_THREADS macro.
32043*/
32044#if SQLITE_THREADSAFE
32045/* # include <pthread.h> */
32046# define SQLITE_UNIX_THREADS 1
32047#endif
32048
32049/*
32050** Default permissions when creating a new file
32051*/
32052#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
32053# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
32054#endif
32055
32056/*
32057** Default permissions when creating auto proxy dir
32058*/
32059#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
32060# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
32061#endif
32062
32063/*
32064** Maximum supported path-length.
32065*/
32066#define MAX_PATHNAME 512
32067
32068/*
32069** Maximum supported symbolic links
32070*/
32071#define SQLITE_MAX_SYMLINKS 100
32072
32073/* Always cast the getpid() return type for compatibility with
32074** kernel modules in VxWorks. */
32075#define osGetpid(X) (pid_t)getpid()
32076
32077/*
32078** Only set the lastErrno if the error code is a real error and not
32079** a normal expected return code of SQLITE_BUSY or SQLITE_OK
32080*/
32081#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
32082
32083/* Forward references */
32084typedef struct unixShm unixShm; /* Connection shared memory */
32085typedef struct unixShmNode unixShmNode; /* Shared memory instance */
32086typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
32087typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
32088
32089/*
32090** Sometimes, after a file handle is closed by SQLite, the file descriptor
32091** cannot be closed immediately. In these cases, instances of the following
32092** structure are used to store the file descriptor while waiting for an
32093** opportunity to either close or reuse it.
32094*/
32095struct UnixUnusedFd {
32096 int fd; /* File descriptor to close */
32097 int flags; /* Flags this file descriptor was opened with */
32098 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
32099};
32100
32101/*
32102** The unixFile structure is subclass of sqlite3_file specific to the unix
32103** VFS implementations.
32104*/
32105typedef struct unixFile unixFile;
32106struct unixFile {
32107 sqlite3_io_methods const *pMethod; /* Always the first entry */
32108 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
32109 unixInodeInfo *pInode; /* Info about locks on this inode */
32110 int h; /* The file descriptor */
32111 unsigned char eFileLock; /* The type of lock held on this fd */
32112 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
32113 int lastErrno; /* The unix errno from last I/O error */
32114 void *lockingContext; /* Locking style specific state */
32115 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
32116 const char *zPath; /* Name of the file */
32117 unixShm *pShm; /* Shared memory segment information */
32118 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
32119#if SQLITE_MAX_MMAP_SIZE>0
32120 int nFetchOut; /* Number of outstanding xFetch refs */
32121 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
32122 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
32123 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
32124 void *pMapRegion; /* Memory mapped region */
32125#endif
32126 int sectorSize; /* Device sector size */
32127 int deviceCharacteristics; /* Precomputed device characteristics */
32128#if SQLITE_ENABLE_LOCKING_STYLE
32129 int openFlags; /* The flags specified at open() */
32130#endif
32131#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
32132 unsigned fsFlags; /* cached details from statfs() */
32133#endif
32134#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
32135 unsigned iBusyTimeout; /* Wait this many millisec on locks */
32136#endif
32137#if OS_VXWORKS
32138 struct vxworksFileId *pId; /* Unique file ID */
32139#endif
32140#ifdef SQLITE_DEBUG
32141 /* The next group of variables are used to track whether or not the
32142 ** transaction counter in bytes 24-27 of database files are updated
32143 ** whenever any part of the database changes. An assertion fault will
32144 ** occur if a file is updated without also updating the transaction
32145 ** counter. This test is made to avoid new problems similar to the
32146 ** one described by ticket #3584.
32147 */
32148 unsigned char transCntrChng; /* True if the transaction counter changed */
32149 unsigned char dbUpdate; /* True if any part of database file changed */
32150 unsigned char inNormalWrite; /* True if in a normal write operation */
32151
32152#endif
32153
32154#ifdef SQLITE_TEST
32155 /* In test mode, increase the size of this structure a bit so that
32156 ** it is larger than the struct CrashFile defined in test6.c.
32157 */
32158 char aPadding[32];
32159#endif
32160};
32161
32162/* This variable holds the process id (pid) from when the xRandomness()
32163** method was called. If xOpen() is called from a different process id,
32164** indicating that a fork() has occurred, the PRNG will be reset.
32165*/
32166static pid_t randomnessPid = 0;
32167
32168/*
32169** Allowed values for the unixFile.ctrlFlags bitmask:
32170*/
32171#define UNIXFILE_EXCL 0x01 /* Connections from one process only */
32172#define UNIXFILE_RDONLY 0x02 /* Connection is read only */
32173#define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
32174#ifndef SQLITE_DISABLE_DIRSYNC
32175# define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
32176#else
32177# define UNIXFILE_DIRSYNC 0x00
32178#endif
32179#define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
32180#define UNIXFILE_DELETE 0x20 /* Delete on close */
32181#define UNIXFILE_URI 0x40 /* Filename might have query parameters */
32182#define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
32183
32184/*
32185** Include code that is common to all os_*.c files
32186*/
32187/************** Include os_common.h in the middle of os_unix.c ***************/
32188/************** Begin file os_common.h ***************************************/
32189/*
32190** 2004 May 22
32191**
32192** The author disclaims copyright to this source code. In place of
32193** a legal notice, here is a blessing:
32194**
32195** May you do good and not evil.
32196** May you find forgiveness for yourself and forgive others.
32197** May you share freely, never taking more than you give.
32198**
32199******************************************************************************
32200**
32201** This file contains macros and a little bit of code that is common to
32202** all of the platform-specific files (os_*.c) and is #included into those
32203** files.
32204**
32205** This file should be #included by the os_*.c files only. It is not a
32206** general purpose header file.
32207*/
32208#ifndef _OS_COMMON_H_
32209#define _OS_COMMON_H_
32210
32211/*
32212** At least two bugs have slipped in because we changed the MEMORY_DEBUG
32213** macro to SQLITE_DEBUG and some older makefiles have not yet made the
32214** switch. The following code should catch this problem at compile-time.
32215*/
32216#ifdef MEMORY_DEBUG
32217# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
32218#endif
32219
32220/*
32221** Macros for performance tracing. Normally turned off. Only works
32222** on i486 hardware.
32223*/
32224#ifdef SQLITE_PERFORMANCE_TRACE
32225
32226/*
32227** hwtime.h contains inline assembler code for implementing
32228** high-performance timing routines.
32229*/
32230/************** Include hwtime.h in the middle of os_common.h ****************/
32231/************** Begin file hwtime.h ******************************************/
32232/*
32233** 2008 May 27
32234**
32235** The author disclaims copyright to this source code. In place of
32236** a legal notice, here is a blessing:
32237**
32238** May you do good and not evil.
32239** May you find forgiveness for yourself and forgive others.
32240** May you share freely, never taking more than you give.
32241**
32242******************************************************************************
32243**
32244** This file contains inline asm code for retrieving "high-performance"
32245** counters for x86 class CPUs.
32246*/
32247#ifndef SQLITE_HWTIME_H
32248#define SQLITE_HWTIME_H
32249
32250/*
32251** The following routine only works on pentium-class (or newer) processors.
32252** It uses the RDTSC opcode to read the cycle count value out of the
32253** processor and returns that value. This can be used for high-res
32254** profiling.
32255*/
32256#if (defined(__GNUC__) || defined(_MSC_VER)) && \
32257 (defined(i386) || defined(__i386__) || defined(_M_IX86))
32258
32259 #if defined(__GNUC__)
32260
32261 __inline__ sqlite_uint64 sqlite3Hwtime(void){
32262 unsigned int lo, hi;
32263 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
32264 return (sqlite_uint64)hi << 32 | lo;
32265 }
32266
32267 #elif defined(_MSC_VER)
32268
32269 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
32270 __asm {
32271 rdtsc
32272 ret ; return value at EDX:EAX
32273 }
32274 }
32275
32276 #endif
32277
32278#elif (defined(__GNUC__) && defined(__x86_64__))
32279
32280 __inline__ sqlite_uint64 sqlite3Hwtime(void){
32281 unsigned long val;
32282 __asm__ __volatile__ ("rdtsc" : "=A" (val));
32283 return val;
32284 }
32285
32286#elif (defined(__GNUC__) && defined(__ppc__))
32287
32288 __inline__ sqlite_uint64 sqlite3Hwtime(void){
32289 unsigned long long retval;
32290 unsigned long junk;
32291 __asm__ __volatile__ ("\n\
32292 1: mftbu %1\n\
32293 mftb %L0\n\
32294 mftbu %0\n\
32295 cmpw %0,%1\n\
32296 bne 1b"
32297 : "=r" (retval), "=r" (junk));
32298 return retval;
32299 }
32300
32301#else
32302
32303 #error Need implementation of sqlite3Hwtime() for your platform.
32304
32305 /*
32306 ** To compile without implementing sqlite3Hwtime() for your platform,
32307 ** you can remove the above #error and use the following
32308 ** stub function. You will lose timing support for many
32309 ** of the debugging and testing utilities, but it should at
32310 ** least compile and run.
32311 */
32312SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
32313
32314#endif
32315
32316#endif /* !defined(SQLITE_HWTIME_H) */
32317
32318/************** End of hwtime.h **********************************************/
32319/************** Continuing where we left off in os_common.h ******************/
32320
32321static sqlite_uint64 g_start;
32322static sqlite_uint64 g_elapsed;
32323#define TIMER_START g_start=sqlite3Hwtime()
32324#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
32325#define TIMER_ELAPSED g_elapsed
32326#else
32327#define TIMER_START
32328#define TIMER_END
32329#define TIMER_ELAPSED ((sqlite_uint64)0)
32330#endif
32331
32332/*
32333** If we compile with the SQLITE_TEST macro set, then the following block
32334** of code will give us the ability to simulate a disk I/O error. This
32335** is used for testing the I/O recovery logic.
32336*/
32337#if defined(SQLITE_TEST)
32338SQLITE_API extern int sqlite3_io_error_hit;
32339SQLITE_API extern int sqlite3_io_error_hardhit;
32340SQLITE_API extern int sqlite3_io_error_pending;
32341SQLITE_API extern int sqlite3_io_error_persist;
32342SQLITE_API extern int sqlite3_io_error_benign;
32343SQLITE_API extern int sqlite3_diskfull_pending;
32344SQLITE_API extern int sqlite3_diskfull;
32345#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
32346#define SimulateIOError(CODE) \
32347 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
32348 || sqlite3_io_error_pending-- == 1 ) \
32349 { local_ioerr(); CODE; }
32350static void local_ioerr(){
32351 IOTRACE(("IOERR\n"));
32352 sqlite3_io_error_hit++;
32353 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
32354}
32355#define SimulateDiskfullError(CODE) \
32356 if( sqlite3_diskfull_pending ){ \
32357 if( sqlite3_diskfull_pending == 1 ){ \
32358 local_ioerr(); \
32359 sqlite3_diskfull = 1; \
32360 sqlite3_io_error_hit = 1; \
32361 CODE; \
32362 }else{ \
32363 sqlite3_diskfull_pending--; \
32364 } \
32365 }
32366#else
32367#define SimulateIOErrorBenign(X)
32368#define SimulateIOError(A)
32369#define SimulateDiskfullError(A)
32370#endif /* defined(SQLITE_TEST) */
32371
32372/*
32373** When testing, keep a count of the number of open files.
32374*/
32375#if defined(SQLITE_TEST)
32376SQLITE_API extern int sqlite3_open_file_count;
32377#define OpenCounter(X) sqlite3_open_file_count+=(X)
32378#else
32379#define OpenCounter(X)
32380#endif /* defined(SQLITE_TEST) */
32381
32382#endif /* !defined(_OS_COMMON_H_) */
32383
32384/************** End of os_common.h *******************************************/
32385/************** Continuing where we left off in os_unix.c ********************/
32386
32387/*
32388** Define various macros that are missing from some systems.
32389*/
32390#ifndef O_LARGEFILE
32391# define O_LARGEFILE 0
32392#endif
32393#ifdef SQLITE_DISABLE_LFS
32394# undef O_LARGEFILE
32395# define O_LARGEFILE 0
32396#endif
32397#ifndef O_NOFOLLOW
32398# define O_NOFOLLOW 0
32399#endif
32400#ifndef O_BINARY
32401# define O_BINARY 0
32402#endif
32403
32404/*
32405** The threadid macro resolves to the thread-id or to 0. Used for
32406** testing and debugging only.
32407*/
32408#if SQLITE_THREADSAFE
32409#define threadid pthread_self()
32410#else
32411#define threadid 0
32412#endif
32413
32414/*
32415** HAVE_MREMAP defaults to true on Linux and false everywhere else.
32416*/
32417#if !defined(HAVE_MREMAP)
32418# if defined(__linux__) && defined(_GNU_SOURCE)
32419# define HAVE_MREMAP 1
32420# else
32421# define HAVE_MREMAP 0
32422# endif
32423#endif
32424
32425/*
32426** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
32427** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
32428*/
32429#ifdef __ANDROID__
32430# define lseek lseek64
32431#endif
32432
32433#ifdef __linux__
32434/*
32435** Linux-specific IOCTL magic numbers used for controlling F2FS
32436*/
32437#define F2FS_IOCTL_MAGIC 0xf5
32438#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
32439#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
32440#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
32441#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
32442#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, u32)
32443#define F2FS_FEATURE_ATOMIC_WRITE 0x0004
32444#endif /* __linux__ */
32445
32446
32447/*
32448** Different Unix systems declare open() in different ways. Same use
32449** open(const char*,int,mode_t). Others use open(const char*,int,...).
32450** The difference is important when using a pointer to the function.
32451**
32452** The safest way to deal with the problem is to always use this wrapper
32453** which always has the same well-defined interface.
32454*/
32455static int posixOpen(const char *zFile, int flags, int mode){
32456 return open(zFile, flags, mode);
32457}
32458
32459/* Forward reference */
32460static int openDirectory(const char*, int*);
32461static int unixGetpagesize(void);
32462
32463/*
32464** Many system calls are accessed through pointer-to-functions so that
32465** they may be overridden at runtime to facilitate fault injection during
32466** testing and sandboxing. The following array holds the names and pointers
32467** to all overrideable system calls.
32468*/
32469static struct unix_syscall {
32470 const char *zName; /* Name of the system call */
32471 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
32472 sqlite3_syscall_ptr pDefault; /* Default value */
32473} aSyscall[] = {
32474 { "open", (sqlite3_syscall_ptr)posixOpen, 0 },
32475#define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
32476
32477 { "close", (sqlite3_syscall_ptr)close, 0 },
32478#define osClose ((int(*)(int))aSyscall[1].pCurrent)
32479
32480 { "access", (sqlite3_syscall_ptr)access, 0 },
32481#define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent)
32482
32483 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
32484#define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
32485
32486 { "stat", (sqlite3_syscall_ptr)stat, 0 },
32487#define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
32488
32489/*
32490** The DJGPP compiler environment looks mostly like Unix, but it
32491** lacks the fcntl() system call. So redefine fcntl() to be something
32492** that always succeeds. This means that locking does not occur under
32493** DJGPP. But it is DOS - what did you expect?
32494*/
32495#ifdef __DJGPP__
32496 { "fstat", 0, 0 },
32497#define osFstat(a,b,c) 0
32498#else
32499 { "fstat", (sqlite3_syscall_ptr)fstat, 0 },
32500#define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
32501#endif
32502
32503 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 },
32504#define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
32505
32506 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 },
32507#define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
32508
32509 { "read", (sqlite3_syscall_ptr)read, 0 },
32510#define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
32511
32512#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
32513 { "pread", (sqlite3_syscall_ptr)pread, 0 },
32514#else
32515 { "pread", (sqlite3_syscall_ptr)0, 0 },
32516#endif
32517#define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
32518
32519#if defined(USE_PREAD64)
32520 { "pread64", (sqlite3_syscall_ptr)pread64, 0 },
32521#else
32522 { "pread64", (sqlite3_syscall_ptr)0, 0 },
32523#endif
32524#define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
32525
32526 { "write", (sqlite3_syscall_ptr)write, 0 },
32527#define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
32528
32529#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
32530 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
32531#else
32532 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
32533#endif
32534#define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
32535 aSyscall[12].pCurrent)
32536
32537#if defined(USE_PREAD64)
32538 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 },
32539#else
32540 { "pwrite64", (sqlite3_syscall_ptr)0, 0 },
32541#endif
32542#define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\
32543 aSyscall[13].pCurrent)
32544
32545 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
32546#define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
32547
32548#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
32549 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
32550#else
32551 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
32552#endif
32553#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
32554
32555 { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
32556#define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
32557
32558 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
32559#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
32560
32561 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
32562#define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
32563
32564 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
32565#define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
32566
32567#if defined(HAVE_FCHOWN)
32568 { "fchown", (sqlite3_syscall_ptr)fchown, 0 },
32569#else
32570 { "fchown", (sqlite3_syscall_ptr)0, 0 },
32571#endif
32572#define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
32573
32574#if defined(HAVE_FCHOWN)
32575 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
32576#else
32577 { "geteuid", (sqlite3_syscall_ptr)0, 0 },
32578#endif
32579#define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent)
32580
32581#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
32582 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
32583#else
32584 { "mmap", (sqlite3_syscall_ptr)0, 0 },
32585#endif
32586#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
32587
32588#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
32589 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
32590#else
32591 { "munmap", (sqlite3_syscall_ptr)0, 0 },
32592#endif
32593#define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent)
32594
32595#if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
32596 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
32597#else
32598 { "mremap", (sqlite3_syscall_ptr)0, 0 },
32599#endif
32600#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
32601
32602#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
32603 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
32604#else
32605 { "getpagesize", (sqlite3_syscall_ptr)0, 0 },
32606#endif
32607#define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent)
32608
32609#if defined(HAVE_READLINK)
32610 { "readlink", (sqlite3_syscall_ptr)readlink, 0 },
32611#else
32612 { "readlink", (sqlite3_syscall_ptr)0, 0 },
32613#endif
32614#define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
32615
32616#if defined(HAVE_LSTAT)
32617 { "lstat", (sqlite3_syscall_ptr)lstat, 0 },
32618#else
32619 { "lstat", (sqlite3_syscall_ptr)0, 0 },
32620#endif
32621#define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
32622
32623#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
32624# ifdef __ANDROID__
32625 { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
32626# else
32627 { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 },
32628# endif
32629#else
32630 { "ioctl", (sqlite3_syscall_ptr)0, 0 },
32631#endif
32632#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
32633
32634}; /* End of the overrideable system calls */
32635
32636
32637/*
32638** On some systems, calls to fchown() will trigger a message in a security
32639** log if they come from non-root processes. So avoid calling fchown() if
32640** we are not running as root.
32641*/
32642static int robustFchown(int fd, uid_t uid, gid_t gid){
32643#if defined(HAVE_FCHOWN)
32644 return osGeteuid() ? 0 : osFchown(fd,uid,gid);
32645#else
32646 return 0;
32647#endif
32648}
32649
32650/*
32651** This is the xSetSystemCall() method of sqlite3_vfs for all of the
32652** "unix" VFSes. Return SQLITE_OK opon successfully updating the
32653** system call pointer, or SQLITE_NOTFOUND if there is no configurable
32654** system call named zName.
32655*/
32656static int unixSetSystemCall(
32657 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
32658 const char *zName, /* Name of system call to override */
32659 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
32660){
32661 unsigned int i;
32662 int rc = SQLITE_NOTFOUND;
32663
32664 UNUSED_PARAMETER(pNotUsed);
32665 if( zName==0 ){
32666 /* If no zName is given, restore all system calls to their default
32667 ** settings and return NULL
32668 */
32669 rc = SQLITE_OK;
32670 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32671 if( aSyscall[i].pDefault ){
32672 aSyscall[i].pCurrent = aSyscall[i].pDefault;
32673 }
32674 }
32675 }else{
32676 /* If zName is specified, operate on only the one system call
32677 ** specified.
32678 */
32679 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32680 if( strcmp(zName, aSyscall[i].zName)==0 ){
32681 if( aSyscall[i].pDefault==0 ){
32682 aSyscall[i].pDefault = aSyscall[i].pCurrent;
32683 }
32684 rc = SQLITE_OK;
32685 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
32686 aSyscall[i].pCurrent = pNewFunc;
32687 break;
32688 }
32689 }
32690 }
32691 return rc;
32692}
32693
32694/*
32695** Return the value of a system call. Return NULL if zName is not a
32696** recognized system call name. NULL is also returned if the system call
32697** is currently undefined.
32698*/
32699static sqlite3_syscall_ptr unixGetSystemCall(
32700 sqlite3_vfs *pNotUsed,
32701 const char *zName
32702){
32703 unsigned int i;
32704
32705 UNUSED_PARAMETER(pNotUsed);
32706 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32707 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
32708 }
32709 return 0;
32710}
32711
32712/*
32713** Return the name of the first system call after zName. If zName==NULL
32714** then return the name of the first system call. Return NULL if zName
32715** is the last system call or if zName is not the name of a valid
32716** system call.
32717*/
32718static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
32719 int i = -1;
32720
32721 UNUSED_PARAMETER(p);
32722 if( zName ){
32723 for(i=0; i<ArraySize(aSyscall)-1; i++){
32724 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
32725 }
32726 }
32727 for(i++; i<ArraySize(aSyscall); i++){
32728 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
32729 }
32730 return 0;
32731}
32732
32733/*
32734** Do not accept any file descriptor less than this value, in order to avoid
32735** opening database file using file descriptors that are commonly used for
32736** standard input, output, and error.
32737*/
32738#ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
32739# define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
32740#endif
32741
32742/*
32743** Invoke open(). Do so multiple times, until it either succeeds or
32744** fails for some reason other than EINTR.
32745**
32746** If the file creation mode "m" is 0 then set it to the default for
32747** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
32748** 0644) as modified by the system umask. If m is not 0, then
32749** make the file creation mode be exactly m ignoring the umask.
32750**
32751** The m parameter will be non-zero only when creating -wal, -journal,
32752** and -shm files. We want those files to have *exactly* the same
32753** permissions as their original database, unadulterated by the umask.
32754** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
32755** transaction crashes and leaves behind hot journals, then any
32756** process that is able to write to the database will also be able to
32757** recover the hot journals.
32758*/
32759static int robust_open(const char *z, int f, mode_t m){
32760 int fd;
32761 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
32762 while(1){
32763#if defined(O_CLOEXEC)
32764 fd = osOpen(z,f|O_CLOEXEC,m2);
32765#else
32766 fd = osOpen(z,f,m2);
32767#endif
32768 if( fd<0 ){
32769 if( errno==EINTR ) continue;
32770 break;
32771 }
32772 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
32773 osClose(fd);
32774 sqlite3_log(SQLITE_WARNING,
32775 "attempt to open \"%s\" as file descriptor %d", z, fd);
32776 fd = -1;
32777 if( osOpen("/dev/null", f, m)<0 ) break;
32778 }
32779 if( fd>=0 ){
32780 if( m!=0 ){
32781 struct stat statbuf;
32782 if( osFstat(fd, &statbuf)==0
32783 && statbuf.st_size==0
32784 && (statbuf.st_mode&0777)!=m
32785 ){
32786 osFchmod(fd, m);
32787 }
32788 }
32789#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
32790 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
32791#endif
32792 }
32793 return fd;
32794}
32795
32796/*
32797** Helper functions to obtain and relinquish the global mutex. The
32798** global mutex is used to protect the unixInodeInfo and
32799** vxworksFileId objects used by this file, all of which may be
32800** shared by multiple threads.
32801**
32802** Function unixMutexHeld() is used to assert() that the global mutex
32803** is held when required. This function is only used as part of assert()
32804** statements. e.g.
32805**
32806** unixEnterMutex()
32807** assert( unixMutexHeld() );
32808** unixEnterLeave()
32809**
32810** To prevent deadlock, the global unixBigLock must must be acquired
32811** before the unixInodeInfo.pLockMutex mutex, if both are held. It is
32812** OK to get the pLockMutex without holding unixBigLock first, but if
32813** that happens, the unixBigLock mutex must not be acquired until after
32814** pLockMutex is released.
32815**
32816** OK: enter(unixBigLock), enter(pLockInfo)
32817** OK: enter(unixBigLock)
32818** OK: enter(pLockInfo)
32819** ERROR: enter(pLockInfo), enter(unixBigLock)
32820*/
32821static sqlite3_mutex *unixBigLock = 0;
32822static void unixEnterMutex(void){
32823 assert( sqlite3_mutex_notheld(unixBigLock) ); /* Not a recursive mutex */
32824 sqlite3_mutex_enter(unixBigLock);
32825}
32826static void unixLeaveMutex(void){
32827 assert( sqlite3_mutex_held(unixBigLock) );
32828 sqlite3_mutex_leave(unixBigLock);
32829}
32830#ifdef SQLITE_DEBUG
32831static int unixMutexHeld(void) {
32832 return sqlite3_mutex_held(unixBigLock);
32833}
32834#endif
32835
32836
32837#ifdef SQLITE_HAVE_OS_TRACE
32838/*
32839** Helper function for printing out trace information from debugging
32840** binaries. This returns the string representation of the supplied
32841** integer lock-type.
32842*/
32843static const char *azFileLock(int eFileLock){
32844 switch( eFileLock ){
32845 case NO_LOCK: return "NONE";
32846 case SHARED_LOCK: return "SHARED";
32847 case RESERVED_LOCK: return "RESERVED";
32848 case PENDING_LOCK: return "PENDING";
32849 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
32850 }
32851 return "ERROR";
32852}
32853#endif
32854
32855#ifdef SQLITE_LOCK_TRACE
32856/*
32857** Print out information about all locking operations.
32858**
32859** This routine is used for troubleshooting locks on multithreaded
32860** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
32861** command-line option on the compiler. This code is normally
32862** turned off.
32863*/
32864static int lockTrace(int fd, int op, struct flock *p){
32865 char *zOpName, *zType;
32866 int s;
32867 int savedErrno;
32868 if( op==F_GETLK ){
32869 zOpName = "GETLK";
32870 }else if( op==F_SETLK ){
32871 zOpName = "SETLK";
32872 }else{
32873 s = osFcntl(fd, op, p);
32874 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
32875 return s;
32876 }
32877 if( p->l_type==F_RDLCK ){
32878 zType = "RDLCK";
32879 }else if( p->l_type==F_WRLCK ){
32880 zType = "WRLCK";
32881 }else if( p->l_type==F_UNLCK ){
32882 zType = "UNLCK";
32883 }else{
32884 assert( 0 );
32885 }
32886 assert( p->l_whence==SEEK_SET );
32887 s = osFcntl(fd, op, p);
32888 savedErrno = errno;
32889 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
32890 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
32891 (int)p->l_pid, s);
32892 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
32893 struct flock l2;
32894 l2 = *p;
32895 osFcntl(fd, F_GETLK, &l2);
32896 if( l2.l_type==F_RDLCK ){
32897 zType = "RDLCK";
32898 }else if( l2.l_type==F_WRLCK ){
32899 zType = "WRLCK";
32900 }else if( l2.l_type==F_UNLCK ){
32901 zType = "UNLCK";
32902 }else{
32903 assert( 0 );
32904 }
32905 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
32906 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
32907 }
32908 errno = savedErrno;
32909 return s;
32910}
32911#undef osFcntl
32912#define osFcntl lockTrace
32913#endif /* SQLITE_LOCK_TRACE */
32914
32915/*
32916** Retry ftruncate() calls that fail due to EINTR
32917**
32918** All calls to ftruncate() within this file should be made through
32919** this wrapper. On the Android platform, bypassing the logic below
32920** could lead to a corrupt database.
32921*/
32922static int robust_ftruncate(int h, sqlite3_int64 sz){
32923 int rc;
32924#ifdef __ANDROID__
32925 /* On Android, ftruncate() always uses 32-bit offsets, even if
32926 ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
32927 ** truncate a file to any size larger than 2GiB. Silently ignore any
32928 ** such attempts. */
32929 if( sz>(sqlite3_int64)0x7FFFFFFF ){
32930 rc = SQLITE_OK;
32931 }else
32932#endif
32933 do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
32934 return rc;
32935}
32936
32937/*
32938** This routine translates a standard POSIX errno code into something
32939** useful to the clients of the sqlite3 functions. Specifically, it is
32940** intended to translate a variety of "try again" errors into SQLITE_BUSY
32941** and a variety of "please close the file descriptor NOW" errors into
32942** SQLITE_IOERR
32943**
32944** Errors during initialization of locks, or file system support for locks,
32945** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
32946*/
32947static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
32948 assert( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
32949 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
32950 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
32951 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) );
32952 switch (posixError) {
32953 case EACCES:
32954 case EAGAIN:
32955 case ETIMEDOUT:
32956 case EBUSY:
32957 case EINTR:
32958 case ENOLCK:
32959 /* random NFS retry error, unless during file system support
32960 * introspection, in which it actually means what it says */
32961 return SQLITE_BUSY;
32962
32963 case EPERM:
32964 return SQLITE_PERM;
32965
32966 default:
32967 return sqliteIOErr;
32968 }
32969}
32970
32971
32972/******************************************************************************
32973****************** Begin Unique File ID Utility Used By VxWorks ***************
32974**
32975** On most versions of unix, we can get a unique ID for a file by concatenating
32976** the device number and the inode number. But this does not work on VxWorks.
32977** On VxWorks, a unique file id must be based on the canonical filename.
32978**
32979** A pointer to an instance of the following structure can be used as a
32980** unique file ID in VxWorks. Each instance of this structure contains
32981** a copy of the canonical filename. There is also a reference count.
32982** The structure is reclaimed when the number of pointers to it drops to
32983** zero.
32984**
32985** There are never very many files open at one time and lookups are not
32986** a performance-critical path, so it is sufficient to put these
32987** structures on a linked list.
32988*/
32989struct vxworksFileId {
32990 struct vxworksFileId *pNext; /* Next in a list of them all */
32991 int nRef; /* Number of references to this one */
32992 int nName; /* Length of the zCanonicalName[] string */
32993 char *zCanonicalName; /* Canonical filename */
32994};
32995
32996#if OS_VXWORKS
32997/*
32998** All unique filenames are held on a linked list headed by this
32999** variable:
33000*/
33001static struct vxworksFileId *vxworksFileList = 0;
33002
33003/*
33004** Simplify a filename into its canonical form
33005** by making the following changes:
33006**
33007** * removing any trailing and duplicate /
33008** * convert /./ into just /
33009** * convert /A/../ where A is any simple name into just /
33010**
33011** Changes are made in-place. Return the new name length.
33012**
33013** The original filename is in z[0..n-1]. Return the number of
33014** characters in the simplified name.
33015*/
33016static int vxworksSimplifyName(char *z, int n){
33017 int i, j;
33018 while( n>1 && z[n-1]=='/' ){ n--; }
33019 for(i=j=0; i<n; i++){
33020 if( z[i]=='/' ){
33021 if( z[i+1]=='/' ) continue;
33022 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
33023 i += 1;
33024 continue;
33025 }
33026 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
33027 while( j>0 && z[j-1]!='/' ){ j--; }
33028 if( j>0 ){ j--; }
33029 i += 2;
33030 continue;
33031 }
33032 }
33033 z[j++] = z[i];
33034 }
33035 z[j] = 0;
33036 return j;
33037}
33038
33039/*
33040** Find a unique file ID for the given absolute pathname. Return
33041** a pointer to the vxworksFileId object. This pointer is the unique
33042** file ID.
33043**
33044** The nRef field of the vxworksFileId object is incremented before
33045** the object is returned. A new vxworksFileId object is created
33046** and added to the global list if necessary.
33047**
33048** If a memory allocation error occurs, return NULL.
33049*/
33050static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
33051 struct vxworksFileId *pNew; /* search key and new file ID */
33052 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
33053 int n; /* Length of zAbsoluteName string */
33054
33055 assert( zAbsoluteName[0]=='/' );
33056 n = (int)strlen(zAbsoluteName);
33057 pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
33058 if( pNew==0 ) return 0;
33059 pNew->zCanonicalName = (char*)&pNew[1];
33060 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
33061 n = vxworksSimplifyName(pNew->zCanonicalName, n);
33062
33063 /* Search for an existing entry that matching the canonical name.
33064 ** If found, increment the reference count and return a pointer to
33065 ** the existing file ID.
33066 */
33067 unixEnterMutex();
33068 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
33069 if( pCandidate->nName==n
33070 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
33071 ){
33072 sqlite3_free(pNew);
33073 pCandidate->nRef++;
33074 unixLeaveMutex();
33075 return pCandidate;
33076 }
33077 }
33078
33079 /* No match was found. We will make a new file ID */
33080 pNew->nRef = 1;
33081 pNew->nName = n;
33082 pNew->pNext = vxworksFileList;
33083 vxworksFileList = pNew;
33084 unixLeaveMutex();
33085 return pNew;
33086}
33087
33088/*
33089** Decrement the reference count on a vxworksFileId object. Free
33090** the object when the reference count reaches zero.
33091*/
33092static void vxworksReleaseFileId(struct vxworksFileId *pId){
33093 unixEnterMutex();
33094 assert( pId->nRef>0 );
33095 pId->nRef--;
33096 if( pId->nRef==0 ){
33097 struct vxworksFileId **pp;
33098 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
33099 assert( *pp==pId );
33100 *pp = pId->pNext;
33101 sqlite3_free(pId);
33102 }
33103 unixLeaveMutex();
33104}
33105#endif /* OS_VXWORKS */
33106/*************** End of Unique File ID Utility Used By VxWorks ****************
33107******************************************************************************/
33108
33109
33110/******************************************************************************
33111*************************** Posix Advisory Locking ****************************
33112**
33113** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
33114** section 6.5.2.2 lines 483 through 490 specify that when a process
33115** sets or clears a lock, that operation overrides any prior locks set
33116** by the same process. It does not explicitly say so, but this implies
33117** that it overrides locks set by the same process using a different
33118** file descriptor. Consider this test case:
33119**
33120** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
33121** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
33122**
33123** Suppose ./file1 and ./file2 are really the same file (because
33124** one is a hard or symbolic link to the other) then if you set
33125** an exclusive lock on fd1, then try to get an exclusive lock
33126** on fd2, it works. I would have expected the second lock to
33127** fail since there was already a lock on the file due to fd1.
33128** But not so. Since both locks came from the same process, the
33129** second overrides the first, even though they were on different
33130** file descriptors opened on different file names.
33131**
33132** This means that we cannot use POSIX locks to synchronize file access
33133** among competing threads of the same process. POSIX locks will work fine
33134** to synchronize access for threads in separate processes, but not
33135** threads within the same process.
33136**
33137** To work around the problem, SQLite has to manage file locks internally
33138** on its own. Whenever a new database is opened, we have to find the
33139** specific inode of the database file (the inode is determined by the
33140** st_dev and st_ino fields of the stat structure that fstat() fills in)
33141** and check for locks already existing on that inode. When locks are
33142** created or removed, we have to look at our own internal record of the
33143** locks to see if another thread has previously set a lock on that same
33144** inode.
33145**
33146** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
33147** For VxWorks, we have to use the alternative unique ID system based on
33148** canonical filename and implemented in the previous division.)
33149**
33150** The sqlite3_file structure for POSIX is no longer just an integer file
33151** descriptor. It is now a structure that holds the integer file
33152** descriptor and a pointer to a structure that describes the internal
33153** locks on the corresponding inode. There is one locking structure
33154** per inode, so if the same inode is opened twice, both unixFile structures
33155** point to the same locking structure. The locking structure keeps
33156** a reference count (so we will know when to delete it) and a "cnt"
33157** field that tells us its internal lock status. cnt==0 means the
33158** file is unlocked. cnt==-1 means the file has an exclusive lock.
33159** cnt>0 means there are cnt shared locks on the file.
33160**
33161** Any attempt to lock or unlock a file first checks the locking
33162** structure. The fcntl() system call is only invoked to set a
33163** POSIX lock if the internal lock structure transitions between
33164** a locked and an unlocked state.
33165**
33166** But wait: there are yet more problems with POSIX advisory locks.
33167**
33168** If you close a file descriptor that points to a file that has locks,
33169** all locks on that file that are owned by the current process are
33170** released. To work around this problem, each unixInodeInfo object
33171** maintains a count of the number of pending locks on tha inode.
33172** When an attempt is made to close an unixFile, if there are
33173** other unixFile open on the same inode that are holding locks, the call
33174** to close() the file descriptor is deferred until all of the locks clear.
33175** The unixInodeInfo structure keeps a list of file descriptors that need to
33176** be closed and that list is walked (and cleared) when the last lock
33177** clears.
33178**
33179** Yet another problem: LinuxThreads do not play well with posix locks.
33180**
33181** Many older versions of linux use the LinuxThreads library which is
33182** not posix compliant. Under LinuxThreads, a lock created by thread
33183** A cannot be modified or overridden by a different thread B.
33184** Only thread A can modify the lock. Locking behavior is correct
33185** if the appliation uses the newer Native Posix Thread Library (NPTL)
33186** on linux - with NPTL a lock created by thread A can override locks
33187** in thread B. But there is no way to know at compile-time which
33188** threading library is being used. So there is no way to know at
33189** compile-time whether or not thread A can override locks on thread B.
33190** One has to do a run-time check to discover the behavior of the
33191** current process.
33192**
33193** SQLite used to support LinuxThreads. But support for LinuxThreads
33194** was dropped beginning with version 3.7.0. SQLite will still work with
33195** LinuxThreads provided that (1) there is no more than one connection
33196** per database file in the same process and (2) database connections
33197** do not move across threads.
33198*/
33199
33200/*
33201** An instance of the following structure serves as the key used
33202** to locate a particular unixInodeInfo object.
33203*/
33204struct unixFileId {
33205 dev_t dev; /* Device number */
33206#if OS_VXWORKS
33207 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
33208#else
33209 /* We are told that some versions of Android contain a bug that
33210 ** sizes ino_t at only 32-bits instead of 64-bits. (See
33211 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
33212 ** To work around this, always allocate 64-bits for the inode number.
33213 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
33214 ** but that should not be a big deal. */
33215 /* WAS: ino_t ino; */
33216 u64 ino; /* Inode number */
33217#endif
33218};
33219
33220/*
33221** An instance of the following structure is allocated for each open
33222** inode. Or, on LinuxThreads, there is one of these structures for
33223** each inode opened by each thread.
33224**
33225** A single inode can have multiple file descriptors, so each unixFile
33226** structure contains a pointer to an instance of this object and this
33227** object keeps a count of the number of unixFile pointing to it.
33228**
33229** Mutex rules:
33230**
33231** (1) Only the pLockMutex mutex must be held in order to read or write
33232** any of the locking fields:
33233** nShared, nLock, eFileLock, bProcessLock, pUnused
33234**
33235** (2) When nRef>0, then the following fields are unchanging and can
33236** be read (but not written) without holding any mutex:
33237** fileId, pLockMutex
33238**
33239** (3) With the exceptions above, all the fields may only be read
33240** or written while holding the global unixBigLock mutex.
33241**
33242** Deadlock prevention: The global unixBigLock mutex may not
33243** be acquired while holding the pLockMutex mutex. If both unixBigLock
33244** and pLockMutex are needed, then unixBigLock must be acquired first.
33245*/
33246struct unixInodeInfo {
33247 struct unixFileId fileId; /* The lookup key */
33248 sqlite3_mutex *pLockMutex; /* Hold this mutex for... */
33249 int nShared; /* Number of SHARED locks held */
33250 int nLock; /* Number of outstanding file locks */
33251 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
33252 unsigned char bProcessLock; /* An exclusive process lock is held */
33253 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
33254 int nRef; /* Number of pointers to this structure */
33255 unixShmNode *pShmNode; /* Shared memory associated with this inode */
33256 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
33257 unixInodeInfo *pPrev; /* .... doubly linked */
33258#if SQLITE_ENABLE_LOCKING_STYLE
33259 unsigned long long sharedByte; /* for AFP simulated shared lock */
33260#endif
33261#if OS_VXWORKS
33262 sem_t *pSem; /* Named POSIX semaphore */
33263 char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */
33264#endif
33265};
33266
33267/*
33268** A lists of all unixInodeInfo objects.
33269*/
33270static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */
33271
33272#ifdef SQLITE_DEBUG
33273/*
33274** True if the inode mutex is held, or not. Used only within assert()
33275** to help verify correct mutex usage.
33276*/
33277int unixFileMutexHeld(unixFile *pFile){
33278 assert( pFile->pInode );
33279 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
33280}
33281int unixFileMutexNotheld(unixFile *pFile){
33282 assert( pFile->pInode );
33283 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
33284}
33285#endif
33286
33287/*
33288**
33289** This function - unixLogErrorAtLine(), is only ever called via the macro
33290** unixLogError().
33291**
33292** It is invoked after an error occurs in an OS function and errno has been
33293** set. It logs a message using sqlite3_log() containing the current value of
33294** errno and, if possible, the human-readable equivalent from strerror() or
33295** strerror_r().
33296**
33297** The first argument passed to the macro should be the error code that
33298** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
33299** The two subsequent arguments should be the name of the OS function that
33300** failed (e.g. "unlink", "open") and the associated file-system path,
33301** if any.
33302*/
33303#define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__)
33304static int unixLogErrorAtLine(
33305 int errcode, /* SQLite error code */
33306 const char *zFunc, /* Name of OS function that failed */
33307 const char *zPath, /* File path associated with error */
33308 int iLine /* Source line number where error occurred */
33309){
33310 char *zErr; /* Message from strerror() or equivalent */
33311 int iErrno = errno; /* Saved syscall error number */
33312
33313 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
33314 ** the strerror() function to obtain the human-readable error message
33315 ** equivalent to errno. Otherwise, use strerror_r().
33316 */
33317#if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
33318 char aErr[80];
33319 memset(aErr, 0, sizeof(aErr));
33320 zErr = aErr;
33321
33322 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
33323 ** assume that the system provides the GNU version of strerror_r() that
33324 ** returns a pointer to a buffer containing the error message. That pointer
33325 ** may point to aErr[], or it may point to some static storage somewhere.
33326 ** Otherwise, assume that the system provides the POSIX version of
33327 ** strerror_r(), which always writes an error message into aErr[].
33328 **
33329 ** If the code incorrectly assumes that it is the POSIX version that is
33330 ** available, the error message will often be an empty string. Not a
33331 ** huge problem. Incorrectly concluding that the GNU version is available
33332 ** could lead to a segfault though.
33333 */
33334#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
33335 zErr =
33336# endif
33337 strerror_r(iErrno, aErr, sizeof(aErr)-1);
33338
33339#elif SQLITE_THREADSAFE
33340 /* This is a threadsafe build, but strerror_r() is not available. */
33341 zErr = "";
33342#else
33343 /* Non-threadsafe build, use strerror(). */
33344 zErr = strerror(iErrno);
33345#endif
33346
33347 if( zPath==0 ) zPath = "";
33348 sqlite3_log(errcode,
33349 "os_unix.c:%d: (%d) %s(%s) - %s",
33350 iLine, iErrno, zFunc, zPath, zErr
33351 );
33352
33353 return errcode;
33354}
33355
33356/*
33357** Close a file descriptor.
33358**
33359** We assume that close() almost always works, since it is only in a
33360** very sick application or on a very sick platform that it might fail.
33361** If it does fail, simply leak the file descriptor, but do log the
33362** error.
33363**
33364** Note that it is not safe to retry close() after EINTR since the
33365** file descriptor might have already been reused by another thread.
33366** So we don't even try to recover from an EINTR. Just log the error
33367** and move on.
33368*/
33369static void robust_close(unixFile *pFile, int h, int lineno){
33370 if( osClose(h) ){
33371 unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
33372 pFile ? pFile->zPath : 0, lineno);
33373 }
33374}
33375
33376/*
33377** Set the pFile->lastErrno. Do this in a subroutine as that provides
33378** a convenient place to set a breakpoint.
33379*/
33380static void storeLastErrno(unixFile *pFile, int error){
33381 pFile->lastErrno = error;
33382}
33383
33384/*
33385** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
33386*/
33387static void closePendingFds(unixFile *pFile){
33388 unixInodeInfo *pInode = pFile->pInode;
33389 UnixUnusedFd *p;
33390 UnixUnusedFd *pNext;
33391 assert( unixFileMutexHeld(pFile) );
33392 for(p=pInode->pUnused; p; p=pNext){
33393 pNext = p->pNext;
33394 robust_close(pFile, p->fd, __LINE__);
33395 sqlite3_free(p);
33396 }
33397 pInode->pUnused = 0;
33398}
33399
33400/*
33401** Release a unixInodeInfo structure previously allocated by findInodeInfo().
33402**
33403** The mutex entered using the unixEnterMutex() function must be held
33404** when this function is called.
33405*/
33406static void releaseInodeInfo(unixFile *pFile){
33407 unixInodeInfo *pInode = pFile->pInode;
33408 assert( unixMutexHeld() );
33409 assert( unixFileMutexNotheld(pFile) );
33410 if( ALWAYS(pInode) ){
33411 pInode->nRef--;
33412 if( pInode->nRef==0 ){
33413 assert( pInode->pShmNode==0 );
33414 sqlite3_mutex_enter(pInode->pLockMutex);
33415 closePendingFds(pFile);
33416 sqlite3_mutex_leave(pInode->pLockMutex);
33417 if( pInode->pPrev ){
33418 assert( pInode->pPrev->pNext==pInode );
33419 pInode->pPrev->pNext = pInode->pNext;
33420 }else{
33421 assert( inodeList==pInode );
33422 inodeList = pInode->pNext;
33423 }
33424 if( pInode->pNext ){
33425 assert( pInode->pNext->pPrev==pInode );
33426 pInode->pNext->pPrev = pInode->pPrev;
33427 }
33428 sqlite3_mutex_free(pInode->pLockMutex);
33429 sqlite3_free(pInode);
33430 }
33431 }
33432}
33433
33434/*
33435** Given a file descriptor, locate the unixInodeInfo object that
33436** describes that file descriptor. Create a new one if necessary. The
33437** return value might be uninitialized if an error occurs.
33438**
33439** The mutex entered using the unixEnterMutex() function must be held
33440** when this function is called.
33441**
33442** Return an appropriate error code.
33443*/
33444static int findInodeInfo(
33445 unixFile *pFile, /* Unix file with file desc used in the key */
33446 unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
33447){
33448 int rc; /* System call return code */
33449 int fd; /* The file descriptor for pFile */
33450 struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
33451 struct stat statbuf; /* Low-level file information */
33452 unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
33453
33454 assert( unixMutexHeld() );
33455
33456 /* Get low-level information about the file that we can used to
33457 ** create a unique name for the file.
33458 */
33459 fd = pFile->h;
33460 rc = osFstat(fd, &statbuf);
33461 if( rc!=0 ){
33462 storeLastErrno(pFile, errno);
33463#if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS)
33464 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
33465#endif
33466 return SQLITE_IOERR;
33467 }
33468
33469#ifdef __APPLE__
33470 /* On OS X on an msdos filesystem, the inode number is reported
33471 ** incorrectly for zero-size files. See ticket #3260. To work
33472 ** around this problem (we consider it a bug in OS X, not SQLite)
33473 ** we always increase the file size to 1 by writing a single byte
33474 ** prior to accessing the inode number. The one byte written is
33475 ** an ASCII 'S' character which also happens to be the first byte
33476 ** in the header of every SQLite database. In this way, if there
33477 ** is a race condition such that another thread has already populated
33478 ** the first page of the database, no damage is done.
33479 */
33480 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
33481 do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
33482 if( rc!=1 ){
33483 storeLastErrno(pFile, errno);
33484 return SQLITE_IOERR;
33485 }
33486 rc = osFstat(fd, &statbuf);
33487 if( rc!=0 ){
33488 storeLastErrno(pFile, errno);
33489 return SQLITE_IOERR;
33490 }
33491 }
33492#endif
33493
33494 memset(&fileId, 0, sizeof(fileId));
33495 fileId.dev = statbuf.st_dev;
33496#if OS_VXWORKS
33497 fileId.pId = pFile->pId;
33498#else
33499 fileId.ino = (u64)statbuf.st_ino;
33500#endif
33501 pInode = inodeList;
33502 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
33503 pInode = pInode->pNext;
33504 }
33505 if( pInode==0 ){
33506 pInode = sqlite3_malloc64( sizeof(*pInode) );
33507 if( pInode==0 ){
33508 return SQLITE_NOMEM_BKPT;
33509 }
33510 memset(pInode, 0, sizeof(*pInode));
33511 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
33512 if( sqlite3GlobalConfig.bCoreMutex ){
33513 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
33514 if( pInode->pLockMutex==0 ){
33515 sqlite3_free(pInode);
33516 return SQLITE_NOMEM_BKPT;
33517 }
33518 }
33519 pInode->nRef = 1;
33520 pInode->pNext = inodeList;
33521 pInode->pPrev = 0;
33522 if( inodeList ) inodeList->pPrev = pInode;
33523 inodeList = pInode;
33524 }else{
33525 pInode->nRef++;
33526 }
33527 *ppInode = pInode;
33528 return SQLITE_OK;
33529}
33530
33531/*
33532** Return TRUE if pFile has been renamed or unlinked since it was first opened.
33533*/
33534static int fileHasMoved(unixFile *pFile){
33535#if OS_VXWORKS
33536 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
33537#else
33538 struct stat buf;
33539 return pFile->pInode!=0 &&
33540 (osStat(pFile->zPath, &buf)!=0
33541 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
33542#endif
33543}
33544
33545
33546/*
33547** Check a unixFile that is a database. Verify the following:
33548**
33549** (1) There is exactly one hard link on the file
33550** (2) The file is not a symbolic link
33551** (3) The file has not been renamed or unlinked
33552**
33553** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
33554*/
33555static void verifyDbFile(unixFile *pFile){
33556 struct stat buf;
33557 int rc;
33558
33559 /* These verifications occurs for the main database only */
33560 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
33561
33562 rc = osFstat(pFile->h, &buf);
33563 if( rc!=0 ){
33564 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
33565 return;
33566 }
33567 if( buf.st_nlink==0 ){
33568 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
33569 return;
33570 }
33571 if( buf.st_nlink>1 ){
33572 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
33573 return;
33574 }
33575 if( fileHasMoved(pFile) ){
33576 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
33577 return;
33578 }
33579}
33580
33581
33582/*
33583** This routine checks if there is a RESERVED lock held on the specified
33584** file by this or any other process. If such a lock is held, set *pResOut
33585** to a non-zero value otherwise *pResOut is set to zero. The return value
33586** is set to SQLITE_OK unless an I/O error occurs during lock checking.
33587*/
33588static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
33589 int rc = SQLITE_OK;
33590 int reserved = 0;
33591 unixFile *pFile = (unixFile*)id;
33592
33593 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
33594
33595 assert( pFile );
33596 assert( pFile->eFileLock<=SHARED_LOCK );
33597 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
33598
33599 /* Check if a thread in this process holds such a lock */
33600 if( pFile->pInode->eFileLock>SHARED_LOCK ){
33601 reserved = 1;
33602 }
33603
33604 /* Otherwise see if some other process holds it.
33605 */
33606#ifndef __DJGPP__
33607 if( !reserved && !pFile->pInode->bProcessLock ){
33608 struct flock lock;
33609 lock.l_whence = SEEK_SET;
33610 lock.l_start = RESERVED_BYTE;
33611 lock.l_len = 1;
33612 lock.l_type = F_WRLCK;
33613 if( osFcntl(pFile->h, F_GETLK, &lock) ){
33614 rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
33615 storeLastErrno(pFile, errno);
33616 } else if( lock.l_type!=F_UNLCK ){
33617 reserved = 1;
33618 }
33619 }
33620#endif
33621
33622 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
33623 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
33624
33625 *pResOut = reserved;
33626 return rc;
33627}
33628
33629/*
33630** Set a posix-advisory-lock.
33631**
33632** There are two versions of this routine. If compiled with
33633** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
33634** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
33635** value is set, then it is the number of milliseconds to wait before
33636** failing the lock. The iBusyTimeout value is always reset back to
33637** zero on each call.
33638**
33639** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
33640** attempt to set the lock.
33641*/
33642#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
33643# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x)
33644#else
33645static int osSetPosixAdvisoryLock(
33646 int h, /* The file descriptor on which to take the lock */
33647 struct flock *pLock, /* The description of the lock */
33648 unixFile *pFile /* Structure holding timeout value */
33649){
33650 int rc = osFcntl(h,F_SETLK,pLock);
33651 while( rc<0 && pFile->iBusyTimeout>0 ){
33652 /* On systems that support some kind of blocking file lock with a timeout,
33653 ** make appropriate changes here to invoke that blocking file lock. On
33654 ** generic posix, however, there is no such API. So we simply try the
33655 ** lock once every millisecond until either the timeout expires, or until
33656 ** the lock is obtained. */
33657 usleep(1000);
33658 rc = osFcntl(h,F_SETLK,pLock);
33659 pFile->iBusyTimeout--;
33660 }
33661 return rc;
33662}
33663#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
33664
33665
33666/*
33667** Attempt to set a system-lock on the file pFile. The lock is
33668** described by pLock.
33669**
33670** If the pFile was opened read/write from unix-excl, then the only lock
33671** ever obtained is an exclusive lock, and it is obtained exactly once
33672** the first time any lock is attempted. All subsequent system locking
33673** operations become no-ops. Locking operations still happen internally,
33674** in order to coordinate access between separate database connections
33675** within this process, but all of that is handled in memory and the
33676** operating system does not participate.
33677**
33678** This function is a pass-through to fcntl(F_SETLK) if pFile is using
33679** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
33680** and is read-only.
33681**
33682** Zero is returned if the call completes successfully, or -1 if a call
33683** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
33684*/
33685static int unixFileLock(unixFile *pFile, struct flock *pLock){
33686 int rc;
33687 unixInodeInfo *pInode = pFile->pInode;
33688 assert( pInode!=0 );
33689 assert( sqlite3_mutex_held(pInode->pLockMutex) );
33690 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
33691 if( pInode->bProcessLock==0 ){
33692 struct flock lock;
33693 assert( pInode->nLock==0 );
33694 lock.l_whence = SEEK_SET;
33695 lock.l_start = SHARED_FIRST;
33696 lock.l_len = SHARED_SIZE;
33697 lock.l_type = F_WRLCK;
33698 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
33699 if( rc<0 ) return rc;
33700 pInode->bProcessLock = 1;
33701 pInode->nLock++;
33702 }else{
33703 rc = 0;
33704 }
33705 }else{
33706 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
33707 }
33708 return rc;
33709}
33710
33711/*
33712** Lock the file with the lock specified by parameter eFileLock - one
33713** of the following:
33714**
33715** (1) SHARED_LOCK
33716** (2) RESERVED_LOCK
33717** (3) PENDING_LOCK
33718** (4) EXCLUSIVE_LOCK
33719**
33720** Sometimes when requesting one lock state, additional lock states
33721** are inserted in between. The locking might fail on one of the later
33722** transitions leaving the lock state different from what it started but
33723** still short of its goal. The following chart shows the allowed
33724** transitions and the inserted intermediate states:
33725**
33726** UNLOCKED -> SHARED
33727** SHARED -> RESERVED
33728** SHARED -> (PENDING) -> EXCLUSIVE
33729** RESERVED -> (PENDING) -> EXCLUSIVE
33730** PENDING -> EXCLUSIVE
33731**
33732** This routine will only increase a lock. Use the sqlite3OsUnlock()
33733** routine to lower a locking level.
33734*/
33735static int unixLock(sqlite3_file *id, int eFileLock){
33736 /* The following describes the implementation of the various locks and
33737 ** lock transitions in terms of the POSIX advisory shared and exclusive
33738 ** lock primitives (called read-locks and write-locks below, to avoid
33739 ** confusion with SQLite lock names). The algorithms are complicated
33740 ** slightly in order to be compatible with Windows95 systems simultaneously
33741 ** accessing the same database file, in case that is ever required.
33742 **
33743 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
33744 ** byte', each single bytes at well known offsets, and the 'shared byte
33745 ** range', a range of 510 bytes at a well known offset.
33746 **
33747 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
33748 ** byte'. If this is successful, 'shared byte range' is read-locked
33749 ** and the lock on the 'pending byte' released. (Legacy note: When
33750 ** SQLite was first developed, Windows95 systems were still very common,
33751 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
33752 ** single randomly selected by from the 'shared byte range' is locked.
33753 ** Windows95 is now pretty much extinct, but this work-around for the
33754 ** lack of shared-locks on Windows95 lives on, for backwards
33755 ** compatibility.)
33756 **
33757 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
33758 ** A RESERVED lock is implemented by grabbing a write-lock on the
33759 ** 'reserved byte'.
33760 **
33761 ** A process may only obtain a PENDING lock after it has obtained a
33762 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
33763 ** on the 'pending byte'. This ensures that no new SHARED locks can be
33764 ** obtained, but existing SHARED locks are allowed to persist. A process
33765 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
33766 ** This property is used by the algorithm for rolling back a journal file
33767 ** after a crash.
33768 **
33769 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
33770 ** implemented by obtaining a write-lock on the entire 'shared byte
33771 ** range'. Since all other locks require a read-lock on one of the bytes
33772 ** within this range, this ensures that no other locks are held on the
33773 ** database.
33774 */
33775 int rc = SQLITE_OK;
33776 unixFile *pFile = (unixFile*)id;
33777 unixInodeInfo *pInode;
33778 struct flock lock;
33779 int tErrno = 0;
33780
33781 assert( pFile );
33782 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
33783 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
33784 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
33785 osGetpid(0)));
33786
33787 /* If there is already a lock of this type or more restrictive on the
33788 ** unixFile, do nothing. Don't use the end_lock: exit path, as
33789 ** unixEnterMutex() hasn't been called yet.
33790 */
33791 if( pFile->eFileLock>=eFileLock ){
33792 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
33793 azFileLock(eFileLock)));
33794 return SQLITE_OK;
33795 }
33796
33797 /* Make sure the locking sequence is correct.
33798 ** (1) We never move from unlocked to anything higher than shared lock.
33799 ** (2) SQLite never explicitly requests a pendig lock.
33800 ** (3) A shared lock is always held when a reserve lock is requested.
33801 */
33802 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
33803 assert( eFileLock!=PENDING_LOCK );
33804 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
33805
33806 /* This mutex is needed because pFile->pInode is shared across threads
33807 */
33808 pInode = pFile->pInode;
33809 sqlite3_mutex_enter(pInode->pLockMutex);
33810
33811 /* If some thread using this PID has a lock via a different unixFile*
33812 ** handle that precludes the requested lock, return BUSY.
33813 */
33814 if( (pFile->eFileLock!=pInode->eFileLock &&
33815 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
33816 ){
33817 rc = SQLITE_BUSY;
33818 goto end_lock;
33819 }
33820
33821 /* If a SHARED lock is requested, and some thread using this PID already
33822 ** has a SHARED or RESERVED lock, then increment reference counts and
33823 ** return SQLITE_OK.
33824 */
33825 if( eFileLock==SHARED_LOCK &&
33826 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
33827 assert( eFileLock==SHARED_LOCK );
33828 assert( pFile->eFileLock==0 );
33829 assert( pInode->nShared>0 );
33830 pFile->eFileLock = SHARED_LOCK;
33831 pInode->nShared++;
33832 pInode->nLock++;
33833 goto end_lock;
33834 }
33835
33836
33837 /* A PENDING lock is needed before acquiring a SHARED lock and before
33838 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
33839 ** be released.
33840 */
33841 lock.l_len = 1L;
33842 lock.l_whence = SEEK_SET;
33843 if( eFileLock==SHARED_LOCK
33844 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
33845 ){
33846 lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
33847 lock.l_start = PENDING_BYTE;
33848 if( unixFileLock(pFile, &lock) ){
33849 tErrno = errno;
33850 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
33851 if( rc!=SQLITE_BUSY ){
33852 storeLastErrno(pFile, tErrno);
33853 }
33854 goto end_lock;
33855 }
33856 }
33857
33858
33859 /* If control gets to this point, then actually go ahead and make
33860 ** operating system calls for the specified lock.
33861 */
33862 if( eFileLock==SHARED_LOCK ){
33863 assert( pInode->nShared==0 );
33864 assert( pInode->eFileLock==0 );
33865 assert( rc==SQLITE_OK );
33866
33867 /* Now get the read-lock */
33868 lock.l_start = SHARED_FIRST;
33869 lock.l_len = SHARED_SIZE;
33870 if( unixFileLock(pFile, &lock) ){
33871 tErrno = errno;
33872 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
33873 }
33874
33875 /* Drop the temporary PENDING lock */
33876 lock.l_start = PENDING_BYTE;
33877 lock.l_len = 1L;
33878 lock.l_type = F_UNLCK;
33879 if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
33880 /* This could happen with a network mount */
33881 tErrno = errno;
33882 rc = SQLITE_IOERR_UNLOCK;
33883 }
33884
33885 if( rc ){
33886 if( rc!=SQLITE_BUSY ){
33887 storeLastErrno(pFile, tErrno);
33888 }
33889 goto end_lock;
33890 }else{
33891 pFile->eFileLock = SHARED_LOCK;
33892 pInode->nLock++;
33893 pInode->nShared = 1;
33894 }
33895 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
33896 /* We are trying for an exclusive lock but another thread in this
33897 ** same process is still holding a shared lock. */
33898 rc = SQLITE_BUSY;
33899 }else{
33900 /* The request was for a RESERVED or EXCLUSIVE lock. It is
33901 ** assumed that there is a SHARED or greater lock on the file
33902 ** already.
33903 */
33904 assert( 0!=pFile->eFileLock );
33905 lock.l_type = F_WRLCK;
33906
33907 assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
33908 if( eFileLock==RESERVED_LOCK ){
33909 lock.l_start = RESERVED_BYTE;
33910 lock.l_len = 1L;
33911 }else{
33912 lock.l_start = SHARED_FIRST;
33913 lock.l_len = SHARED_SIZE;
33914 }
33915
33916 if( unixFileLock(pFile, &lock) ){
33917 tErrno = errno;
33918 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
33919 if( rc!=SQLITE_BUSY ){
33920 storeLastErrno(pFile, tErrno);
33921 }
33922 }
33923 }
33924
33925
33926#ifdef SQLITE_DEBUG
33927 /* Set up the transaction-counter change checking flags when
33928 ** transitioning from a SHARED to a RESERVED lock. The change
33929 ** from SHARED to RESERVED marks the beginning of a normal
33930 ** write operation (not a hot journal rollback).
33931 */
33932 if( rc==SQLITE_OK
33933 && pFile->eFileLock<=SHARED_LOCK
33934 && eFileLock==RESERVED_LOCK
33935 ){
33936 pFile->transCntrChng = 0;
33937 pFile->dbUpdate = 0;
33938 pFile->inNormalWrite = 1;
33939 }
33940#endif
33941
33942
33943 if( rc==SQLITE_OK ){
33944 pFile->eFileLock = eFileLock;
33945 pInode->eFileLock = eFileLock;
33946 }else if( eFileLock==EXCLUSIVE_LOCK ){
33947 pFile->eFileLock = PENDING_LOCK;
33948 pInode->eFileLock = PENDING_LOCK;
33949 }
33950
33951end_lock:
33952 sqlite3_mutex_leave(pInode->pLockMutex);
33953 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
33954 rc==SQLITE_OK ? "ok" : "failed"));
33955 return rc;
33956}
33957
33958/*
33959** Add the file descriptor used by file handle pFile to the corresponding
33960** pUnused list.
33961*/
33962static void setPendingFd(unixFile *pFile){
33963 unixInodeInfo *pInode = pFile->pInode;
33964 UnixUnusedFd *p = pFile->pPreallocatedUnused;
33965 assert( unixFileMutexHeld(pFile) );
33966 p->pNext = pInode->pUnused;
33967 pInode->pUnused = p;
33968 pFile->h = -1;
33969 pFile->pPreallocatedUnused = 0;
33970}
33971
33972/*
33973** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
33974** must be either NO_LOCK or SHARED_LOCK.
33975**
33976** If the locking level of the file descriptor is already at or below
33977** the requested locking level, this routine is a no-op.
33978**
33979** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
33980** the byte range is divided into 2 parts and the first part is unlocked then
33981** set to a read lock, then the other part is simply unlocked. This works
33982** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
33983** remove the write lock on a region when a read lock is set.
33984*/
33985static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
33986 unixFile *pFile = (unixFile*)id;
33987 unixInodeInfo *pInode;
33988 struct flock lock;
33989 int rc = SQLITE_OK;
33990
33991 assert( pFile );
33992 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
33993 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
33994 osGetpid(0)));
33995
33996 assert( eFileLock<=SHARED_LOCK );
33997 if( pFile->eFileLock<=eFileLock ){
33998 return SQLITE_OK;
33999 }
34000 pInode = pFile->pInode;
34001 sqlite3_mutex_enter(pInode->pLockMutex);
34002 assert( pInode->nShared!=0 );
34003 if( pFile->eFileLock>SHARED_LOCK ){
34004 assert( pInode->eFileLock==pFile->eFileLock );
34005
34006#ifdef SQLITE_DEBUG
34007 /* When reducing a lock such that other processes can start
34008 ** reading the database file again, make sure that the
34009 ** transaction counter was updated if any part of the database
34010 ** file changed. If the transaction counter is not updated,
34011 ** other connections to the same file might not realize that
34012 ** the file has changed and hence might not know to flush their
34013 ** cache. The use of a stale cache can lead to database corruption.
34014 */
34015 pFile->inNormalWrite = 0;
34016#endif
34017
34018 /* downgrading to a shared lock on NFS involves clearing the write lock
34019 ** before establishing the readlock - to avoid a race condition we downgrade
34020 ** the lock in 2 blocks, so that part of the range will be covered by a
34021 ** write lock until the rest is covered by a read lock:
34022 ** 1: [WWWWW]
34023 ** 2: [....W]
34024 ** 3: [RRRRW]
34025 ** 4: [RRRR.]
34026 */
34027 if( eFileLock==SHARED_LOCK ){
34028#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
34029 (void)handleNFSUnlock;
34030 assert( handleNFSUnlock==0 );
34031#endif
34032#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
34033 if( handleNFSUnlock ){
34034 int tErrno; /* Error code from system call errors */
34035 off_t divSize = SHARED_SIZE - 1;
34036
34037 lock.l_type = F_UNLCK;
34038 lock.l_whence = SEEK_SET;
34039 lock.l_start = SHARED_FIRST;
34040 lock.l_len = divSize;
34041 if( unixFileLock(pFile, &lock)==(-1) ){
34042 tErrno = errno;
34043 rc = SQLITE_IOERR_UNLOCK;
34044 storeLastErrno(pFile, tErrno);
34045 goto end_unlock;
34046 }
34047 lock.l_type = F_RDLCK;
34048 lock.l_whence = SEEK_SET;
34049 lock.l_start = SHARED_FIRST;
34050 lock.l_len = divSize;
34051 if( unixFileLock(pFile, &lock)==(-1) ){
34052 tErrno = errno;
34053 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
34054 if( IS_LOCK_ERROR(rc) ){
34055 storeLastErrno(pFile, tErrno);
34056 }
34057 goto end_unlock;
34058 }
34059 lock.l_type = F_UNLCK;
34060 lock.l_whence = SEEK_SET;
34061 lock.l_start = SHARED_FIRST+divSize;
34062 lock.l_len = SHARED_SIZE-divSize;
34063 if( unixFileLock(pFile, &lock)==(-1) ){
34064 tErrno = errno;
34065 rc = SQLITE_IOERR_UNLOCK;
34066 storeLastErrno(pFile, tErrno);
34067 goto end_unlock;
34068 }
34069 }else
34070#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
34071 {
34072 lock.l_type = F_RDLCK;
34073 lock.l_whence = SEEK_SET;
34074 lock.l_start = SHARED_FIRST;
34075 lock.l_len = SHARED_SIZE;
34076 if( unixFileLock(pFile, &lock) ){
34077 /* In theory, the call to unixFileLock() cannot fail because another
34078 ** process is holding an incompatible lock. If it does, this
34079 ** indicates that the other process is not following the locking
34080 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
34081 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
34082 ** an assert to fail). */
34083 rc = SQLITE_IOERR_RDLOCK;
34084 storeLastErrno(pFile, errno);
34085 goto end_unlock;
34086 }
34087 }
34088 }
34089 lock.l_type = F_UNLCK;
34090 lock.l_whence = SEEK_SET;
34091 lock.l_start = PENDING_BYTE;
34092 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
34093 if( unixFileLock(pFile, &lock)==0 ){
34094 pInode->eFileLock = SHARED_LOCK;
34095 }else{
34096 rc = SQLITE_IOERR_UNLOCK;
34097 storeLastErrno(pFile, errno);
34098 goto end_unlock;
34099 }
34100 }
34101 if( eFileLock==NO_LOCK ){
34102 /* Decrement the shared lock counter. Release the lock using an
34103 ** OS call only when all threads in this same process have released
34104 ** the lock.
34105 */
34106 pInode->nShared--;
34107 if( pInode->nShared==0 ){
34108 lock.l_type = F_UNLCK;
34109 lock.l_whence = SEEK_SET;
34110 lock.l_start = lock.l_len = 0L;
34111 if( unixFileLock(pFile, &lock)==0 ){
34112 pInode->eFileLock = NO_LOCK;
34113 }else{
34114 rc = SQLITE_IOERR_UNLOCK;
34115 storeLastErrno(pFile, errno);
34116 pInode->eFileLock = NO_LOCK;
34117 pFile->eFileLock = NO_LOCK;
34118 }
34119 }
34120
34121 /* Decrement the count of locks against this same file. When the
34122 ** count reaches zero, close any other file descriptors whose close
34123 ** was deferred because of outstanding locks.
34124 */
34125 pInode->nLock--;
34126 assert( pInode->nLock>=0 );
34127 if( pInode->nLock==0 ) closePendingFds(pFile);
34128 }
34129
34130end_unlock:
34131 sqlite3_mutex_leave(pInode->pLockMutex);
34132 if( rc==SQLITE_OK ){
34133 pFile->eFileLock = eFileLock;
34134 }
34135 return rc;
34136}
34137
34138/*
34139** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
34140** must be either NO_LOCK or SHARED_LOCK.
34141**
34142** If the locking level of the file descriptor is already at or below
34143** the requested locking level, this routine is a no-op.
34144*/
34145static int unixUnlock(sqlite3_file *id, int eFileLock){
34146#if SQLITE_MAX_MMAP_SIZE>0
34147 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
34148#endif
34149 return posixUnlock(id, eFileLock, 0);
34150}
34151
34152#if SQLITE_MAX_MMAP_SIZE>0
34153static int unixMapfile(unixFile *pFd, i64 nByte);
34154static void unixUnmapfile(unixFile *pFd);
34155#endif
34156
34157/*
34158** This function performs the parts of the "close file" operation
34159** common to all locking schemes. It closes the directory and file
34160** handles, if they are valid, and sets all fields of the unixFile
34161** structure to 0.
34162**
34163** It is *not* necessary to hold the mutex when this routine is called,
34164** even on VxWorks. A mutex will be acquired on VxWorks by the
34165** vxworksReleaseFileId() routine.
34166*/
34167static int closeUnixFile(sqlite3_file *id){
34168 unixFile *pFile = (unixFile*)id;
34169#if SQLITE_MAX_MMAP_SIZE>0
34170 unixUnmapfile(pFile);
34171#endif
34172 if( pFile->h>=0 ){
34173 robust_close(pFile, pFile->h, __LINE__);
34174 pFile->h = -1;
34175 }
34176#if OS_VXWORKS
34177 if( pFile->pId ){
34178 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
34179 osUnlink(pFile->pId->zCanonicalName);
34180 }
34181 vxworksReleaseFileId(pFile->pId);
34182 pFile->pId = 0;
34183 }
34184#endif
34185#ifdef SQLITE_UNLINK_AFTER_CLOSE
34186 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
34187 osUnlink(pFile->zPath);
34188 sqlite3_free(*(char**)&pFile->zPath);
34189 pFile->zPath = 0;
34190 }
34191#endif
34192 OSTRACE(("CLOSE %-3d\n", pFile->h));
34193 OpenCounter(-1);
34194 sqlite3_free(pFile->pPreallocatedUnused);
34195 memset(pFile, 0, sizeof(unixFile));
34196 return SQLITE_OK;
34197}
34198
34199/*
34200** Close a file.
34201*/
34202static int unixClose(sqlite3_file *id){
34203 int rc = SQLITE_OK;
34204 unixFile *pFile = (unixFile *)id;
34205 unixInodeInfo *pInode = pFile->pInode;
34206
34207 assert( pInode!=0 );
34208 verifyDbFile(pFile);
34209 unixUnlock(id, NO_LOCK);
34210 assert( unixFileMutexNotheld(pFile) );
34211 unixEnterMutex();
34212
34213 /* unixFile.pInode is always valid here. Otherwise, a different close
34214 ** routine (e.g. nolockClose()) would be called instead.
34215 */
34216 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
34217 sqlite3_mutex_enter(pInode->pLockMutex);
34218 if( pInode->nLock ){
34219 /* If there are outstanding locks, do not actually close the file just
34220 ** yet because that would clear those locks. Instead, add the file
34221 ** descriptor to pInode->pUnused list. It will be automatically closed
34222 ** when the last lock is cleared.
34223 */
34224 setPendingFd(pFile);
34225 }
34226 sqlite3_mutex_leave(pInode->pLockMutex);
34227 releaseInodeInfo(pFile);
34228 rc = closeUnixFile(id);
34229 unixLeaveMutex();
34230 return rc;
34231}
34232
34233/************** End of the posix advisory lock implementation *****************
34234******************************************************************************/
34235
34236/******************************************************************************
34237****************************** No-op Locking **********************************
34238**
34239** Of the various locking implementations available, this is by far the
34240** simplest: locking is ignored. No attempt is made to lock the database
34241** file for reading or writing.
34242**
34243** This locking mode is appropriate for use on read-only databases
34244** (ex: databases that are burned into CD-ROM, for example.) It can
34245** also be used if the application employs some external mechanism to
34246** prevent simultaneous access of the same database by two or more
34247** database connections. But there is a serious risk of database
34248** corruption if this locking mode is used in situations where multiple
34249** database connections are accessing the same database file at the same
34250** time and one or more of those connections are writing.
34251*/
34252
34253static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
34254 UNUSED_PARAMETER(NotUsed);
34255 *pResOut = 0;
34256 return SQLITE_OK;
34257}
34258static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
34259 UNUSED_PARAMETER2(NotUsed, NotUsed2);
34260 return SQLITE_OK;
34261}
34262static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
34263 UNUSED_PARAMETER2(NotUsed, NotUsed2);
34264 return SQLITE_OK;
34265}
34266
34267/*
34268** Close the file.
34269*/
34270static int nolockClose(sqlite3_file *id) {
34271 return closeUnixFile(id);
34272}
34273
34274/******************* End of the no-op lock implementation *********************
34275******************************************************************************/
34276
34277/******************************************************************************
34278************************* Begin dot-file Locking ******************************
34279**
34280** The dotfile locking implementation uses the existence of separate lock
34281** files (really a directory) to control access to the database. This works
34282** on just about every filesystem imaginable. But there are serious downsides:
34283**
34284** (1) There is zero concurrency. A single reader blocks all other
34285** connections from reading or writing the database.
34286**
34287** (2) An application crash or power loss can leave stale lock files
34288** sitting around that need to be cleared manually.
34289**
34290** Nevertheless, a dotlock is an appropriate locking mode for use if no
34291** other locking strategy is available.
34292**
34293** Dotfile locking works by creating a subdirectory in the same directory as
34294** the database and with the same name but with a ".lock" extension added.
34295** The existence of a lock directory implies an EXCLUSIVE lock. All other
34296** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
34297*/
34298
34299/*
34300** The file suffix added to the data base filename in order to create the
34301** lock directory.
34302*/
34303#define DOTLOCK_SUFFIX ".lock"
34304
34305/*
34306** This routine checks if there is a RESERVED lock held on the specified
34307** file by this or any other process. If such a lock is held, set *pResOut
34308** to a non-zero value otherwise *pResOut is set to zero. The return value
34309** is set to SQLITE_OK unless an I/O error occurs during lock checking.
34310**
34311** In dotfile locking, either a lock exists or it does not. So in this
34312** variation of CheckReservedLock(), *pResOut is set to true if any lock
34313** is held on the file and false if the file is unlocked.
34314*/
34315static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
34316 int rc = SQLITE_OK;
34317 int reserved = 0;
34318 unixFile *pFile = (unixFile*)id;
34319
34320 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34321
34322 assert( pFile );
34323 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
34324 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
34325 *pResOut = reserved;
34326 return rc;
34327}
34328
34329/*
34330** Lock the file with the lock specified by parameter eFileLock - one
34331** of the following:
34332**
34333** (1) SHARED_LOCK
34334** (2) RESERVED_LOCK
34335** (3) PENDING_LOCK
34336** (4) EXCLUSIVE_LOCK
34337**
34338** Sometimes when requesting one lock state, additional lock states
34339** are inserted in between. The locking might fail on one of the later
34340** transitions leaving the lock state different from what it started but
34341** still short of its goal. The following chart shows the allowed
34342** transitions and the inserted intermediate states:
34343**
34344** UNLOCKED -> SHARED
34345** SHARED -> RESERVED
34346** SHARED -> (PENDING) -> EXCLUSIVE
34347** RESERVED -> (PENDING) -> EXCLUSIVE
34348** PENDING -> EXCLUSIVE
34349**
34350** This routine will only increase a lock. Use the sqlite3OsUnlock()
34351** routine to lower a locking level.
34352**
34353** With dotfile locking, we really only support state (4): EXCLUSIVE.
34354** But we track the other locking levels internally.
34355*/
34356static int dotlockLock(sqlite3_file *id, int eFileLock) {
34357 unixFile *pFile = (unixFile*)id;
34358 char *zLockFile = (char *)pFile->lockingContext;
34359 int rc = SQLITE_OK;
34360
34361
34362 /* If we have any lock, then the lock file already exists. All we have
34363 ** to do is adjust our internal record of the lock level.
34364 */
34365 if( pFile->eFileLock > NO_LOCK ){
34366 pFile->eFileLock = eFileLock;
34367 /* Always update the timestamp on the old file */
34368#ifdef HAVE_UTIME
34369 utime(zLockFile, NULL);
34370#else
34371 utimes(zLockFile, NULL);
34372#endif
34373 return SQLITE_OK;
34374 }
34375
34376 /* grab an exclusive lock */
34377 rc = osMkdir(zLockFile, 0777);
34378 if( rc<0 ){
34379 /* failed to open/create the lock directory */
34380 int tErrno = errno;
34381 if( EEXIST == tErrno ){
34382 rc = SQLITE_BUSY;
34383 } else {
34384 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
34385 if( rc!=SQLITE_BUSY ){
34386 storeLastErrno(pFile, tErrno);
34387 }
34388 }
34389 return rc;
34390 }
34391
34392 /* got it, set the type and return ok */
34393 pFile->eFileLock = eFileLock;
34394 return rc;
34395}
34396
34397/*
34398** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
34399** must be either NO_LOCK or SHARED_LOCK.
34400**
34401** If the locking level of the file descriptor is already at or below
34402** the requested locking level, this routine is a no-op.
34403**
34404** When the locking level reaches NO_LOCK, delete the lock file.
34405*/
34406static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
34407 unixFile *pFile = (unixFile*)id;
34408 char *zLockFile = (char *)pFile->lockingContext;
34409 int rc;
34410
34411 assert( pFile );
34412 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
34413 pFile->eFileLock, osGetpid(0)));
34414 assert( eFileLock<=SHARED_LOCK );
34415
34416 /* no-op if possible */
34417 if( pFile->eFileLock==eFileLock ){
34418 return SQLITE_OK;
34419 }
34420
34421 /* To downgrade to shared, simply update our internal notion of the
34422 ** lock state. No need to mess with the file on disk.
34423 */
34424 if( eFileLock==SHARED_LOCK ){
34425 pFile->eFileLock = SHARED_LOCK;
34426 return SQLITE_OK;
34427 }
34428
34429 /* To fully unlock the database, delete the lock file */
34430 assert( eFileLock==NO_LOCK );
34431 rc = osRmdir(zLockFile);
34432 if( rc<0 ){
34433 int tErrno = errno;
34434 if( tErrno==ENOENT ){
34435 rc = SQLITE_OK;
34436 }else{
34437 rc = SQLITE_IOERR_UNLOCK;
34438 storeLastErrno(pFile, tErrno);
34439 }
34440 return rc;
34441 }
34442 pFile->eFileLock = NO_LOCK;
34443 return SQLITE_OK;
34444}
34445
34446/*
34447** Close a file. Make sure the lock has been released before closing.
34448*/
34449static int dotlockClose(sqlite3_file *id) {
34450 unixFile *pFile = (unixFile*)id;
34451 assert( id!=0 );
34452 dotlockUnlock(id, NO_LOCK);
34453 sqlite3_free(pFile->lockingContext);
34454 return closeUnixFile(id);
34455}
34456/****************** End of the dot-file lock implementation *******************
34457******************************************************************************/
34458
34459/******************************************************************************
34460************************** Begin flock Locking ********************************
34461**
34462** Use the flock() system call to do file locking.
34463**
34464** flock() locking is like dot-file locking in that the various
34465** fine-grain locking levels supported by SQLite are collapsed into
34466** a single exclusive lock. In other words, SHARED, RESERVED, and
34467** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
34468** still works when you do this, but concurrency is reduced since
34469** only a single process can be reading the database at a time.
34470**
34471** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
34472*/
34473#if SQLITE_ENABLE_LOCKING_STYLE
34474
34475/*
34476** Retry flock() calls that fail with EINTR
34477*/
34478#ifdef EINTR
34479static int robust_flock(int fd, int op){
34480 int rc;
34481 do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
34482 return rc;
34483}
34484#else
34485# define robust_flock(a,b) flock(a,b)
34486#endif
34487
34488
34489/*
34490** This routine checks if there is a RESERVED lock held on the specified
34491** file by this or any other process. If such a lock is held, set *pResOut
34492** to a non-zero value otherwise *pResOut is set to zero. The return value
34493** is set to SQLITE_OK unless an I/O error occurs during lock checking.
34494*/
34495static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
34496 int rc = SQLITE_OK;
34497 int reserved = 0;
34498 unixFile *pFile = (unixFile*)id;
34499
34500 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34501
34502 assert( pFile );
34503
34504 /* Check if a thread in this process holds such a lock */
34505 if( pFile->eFileLock>SHARED_LOCK ){
34506 reserved = 1;
34507 }
34508
34509 /* Otherwise see if some other process holds it. */
34510 if( !reserved ){
34511 /* attempt to get the lock */
34512 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
34513 if( !lrc ){
34514 /* got the lock, unlock it */
34515 lrc = robust_flock(pFile->h, LOCK_UN);
34516 if ( lrc ) {
34517 int tErrno = errno;
34518 /* unlock failed with an error */
34519 lrc = SQLITE_IOERR_UNLOCK;
34520 storeLastErrno(pFile, tErrno);
34521 rc = lrc;
34522 }
34523 } else {
34524 int tErrno = errno;
34525 reserved = 1;
34526 /* someone else might have it reserved */
34527 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
34528 if( IS_LOCK_ERROR(lrc) ){
34529 storeLastErrno(pFile, tErrno);
34530 rc = lrc;
34531 }
34532 }
34533 }
34534 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
34535
34536#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
34537 if( (rc & 0xff) == SQLITE_IOERR ){
34538 rc = SQLITE_OK;
34539 reserved=1;
34540 }
34541#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
34542 *pResOut = reserved;
34543 return rc;
34544}
34545
34546/*
34547** Lock the file with the lock specified by parameter eFileLock - one
34548** of the following:
34549**
34550** (1) SHARED_LOCK
34551** (2) RESERVED_LOCK
34552** (3) PENDING_LOCK
34553** (4) EXCLUSIVE_LOCK
34554**
34555** Sometimes when requesting one lock state, additional lock states
34556** are inserted in between. The locking might fail on one of the later
34557** transitions leaving the lock state different from what it started but
34558** still short of its goal. The following chart shows the allowed
34559** transitions and the inserted intermediate states:
34560**
34561** UNLOCKED -> SHARED
34562** SHARED -> RESERVED
34563** SHARED -> (PENDING) -> EXCLUSIVE
34564** RESERVED -> (PENDING) -> EXCLUSIVE
34565** PENDING -> EXCLUSIVE
34566**
34567** flock() only really support EXCLUSIVE locks. We track intermediate
34568** lock states in the sqlite3_file structure, but all locks SHARED or
34569** above are really EXCLUSIVE locks and exclude all other processes from
34570** access the file.
34571**
34572** This routine will only increase a lock. Use the sqlite3OsUnlock()
34573** routine to lower a locking level.
34574*/
34575static int flockLock(sqlite3_file *id, int eFileLock) {
34576 int rc = SQLITE_OK;
34577 unixFile *pFile = (unixFile*)id;
34578
34579 assert( pFile );
34580
34581 /* if we already have a lock, it is exclusive.
34582 ** Just adjust level and punt on outta here. */
34583 if (pFile->eFileLock > NO_LOCK) {
34584 pFile->eFileLock = eFileLock;
34585 return SQLITE_OK;
34586 }
34587
34588 /* grab an exclusive lock */
34589
34590 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
34591 int tErrno = errno;
34592 /* didn't get, must be busy */
34593 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
34594 if( IS_LOCK_ERROR(rc) ){
34595 storeLastErrno(pFile, tErrno);
34596 }
34597 } else {
34598 /* got it, set the type and return ok */
34599 pFile->eFileLock = eFileLock;
34600 }
34601 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
34602 rc==SQLITE_OK ? "ok" : "failed"));
34603#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
34604 if( (rc & 0xff) == SQLITE_IOERR ){
34605 rc = SQLITE_BUSY;
34606 }
34607#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
34608 return rc;
34609}
34610
34611
34612/*
34613** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
34614** must be either NO_LOCK or SHARED_LOCK.
34615**
34616** If the locking level of the file descriptor is already at or below
34617** the requested locking level, this routine is a no-op.
34618*/
34619static int flockUnlock(sqlite3_file *id, int eFileLock) {
34620 unixFile *pFile = (unixFile*)id;
34621
34622 assert( pFile );
34623 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
34624 pFile->eFileLock, osGetpid(0)));
34625 assert( eFileLock<=SHARED_LOCK );
34626
34627 /* no-op if possible */
34628 if( pFile->eFileLock==eFileLock ){
34629 return SQLITE_OK;
34630 }
34631
34632 /* shared can just be set because we always have an exclusive */
34633 if (eFileLock==SHARED_LOCK) {
34634 pFile->eFileLock = eFileLock;
34635 return SQLITE_OK;
34636 }
34637
34638 /* no, really, unlock. */
34639 if( robust_flock(pFile->h, LOCK_UN) ){
34640#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
34641 return SQLITE_OK;
34642#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
34643 return SQLITE_IOERR_UNLOCK;
34644 }else{
34645 pFile->eFileLock = NO_LOCK;
34646 return SQLITE_OK;
34647 }
34648}
34649
34650/*
34651** Close a file.
34652*/
34653static int flockClose(sqlite3_file *id) {
34654 assert( id!=0 );
34655 flockUnlock(id, NO_LOCK);
34656 return closeUnixFile(id);
34657}
34658
34659#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
34660
34661/******************* End of the flock lock implementation *********************
34662******************************************************************************/
34663
34664/******************************************************************************
34665************************ Begin Named Semaphore Locking ************************
34666**
34667** Named semaphore locking is only supported on VxWorks.
34668**
34669** Semaphore locking is like dot-lock and flock in that it really only
34670** supports EXCLUSIVE locking. Only a single process can read or write
34671** the database file at a time. This reduces potential concurrency, but
34672** makes the lock implementation much easier.
34673*/
34674#if OS_VXWORKS
34675
34676/*
34677** This routine checks if there is a RESERVED lock held on the specified
34678** file by this or any other process. If such a lock is held, set *pResOut
34679** to a non-zero value otherwise *pResOut is set to zero. The return value
34680** is set to SQLITE_OK unless an I/O error occurs during lock checking.
34681*/
34682static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
34683 int rc = SQLITE_OK;
34684 int reserved = 0;
34685 unixFile *pFile = (unixFile*)id;
34686
34687 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34688
34689 assert( pFile );
34690
34691 /* Check if a thread in this process holds such a lock */
34692 if( pFile->eFileLock>SHARED_LOCK ){
34693 reserved = 1;
34694 }
34695
34696 /* Otherwise see if some other process holds it. */
34697 if( !reserved ){
34698 sem_t *pSem = pFile->pInode->pSem;
34699
34700 if( sem_trywait(pSem)==-1 ){
34701 int tErrno = errno;
34702 if( EAGAIN != tErrno ){
34703 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
34704 storeLastErrno(pFile, tErrno);
34705 } else {
34706 /* someone else has the lock when we are in NO_LOCK */
34707 reserved = (pFile->eFileLock < SHARED_LOCK);
34708 }
34709 }else{
34710 /* we could have it if we want it */
34711 sem_post(pSem);
34712 }
34713 }
34714 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
34715
34716 *pResOut = reserved;
34717 return rc;
34718}
34719
34720/*
34721** Lock the file with the lock specified by parameter eFileLock - one
34722** of the following:
34723**
34724** (1) SHARED_LOCK
34725** (2) RESERVED_LOCK
34726** (3) PENDING_LOCK
34727** (4) EXCLUSIVE_LOCK
34728**
34729** Sometimes when requesting one lock state, additional lock states
34730** are inserted in between. The locking might fail on one of the later
34731** transitions leaving the lock state different from what it started but
34732** still short of its goal. The following chart shows the allowed
34733** transitions and the inserted intermediate states:
34734**
34735** UNLOCKED -> SHARED
34736** SHARED -> RESERVED
34737** SHARED -> (PENDING) -> EXCLUSIVE
34738** RESERVED -> (PENDING) -> EXCLUSIVE
34739** PENDING -> EXCLUSIVE
34740**
34741** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
34742** lock states in the sqlite3_file structure, but all locks SHARED or
34743** above are really EXCLUSIVE locks and exclude all other processes from
34744** access the file.
34745**
34746** This routine will only increase a lock. Use the sqlite3OsUnlock()
34747** routine to lower a locking level.
34748*/
34749static int semXLock(sqlite3_file *id, int eFileLock) {
34750 unixFile *pFile = (unixFile*)id;
34751 sem_t *pSem = pFile->pInode->pSem;
34752 int rc = SQLITE_OK;
34753
34754 /* if we already have a lock, it is exclusive.
34755 ** Just adjust level and punt on outta here. */
34756 if (pFile->eFileLock > NO_LOCK) {
34757 pFile->eFileLock = eFileLock;
34758 rc = SQLITE_OK;
34759 goto sem_end_lock;
34760 }
34761
34762 /* lock semaphore now but bail out when already locked. */
34763 if( sem_trywait(pSem)==-1 ){
34764 rc = SQLITE_BUSY;
34765 goto sem_end_lock;
34766 }
34767
34768 /* got it, set the type and return ok */
34769 pFile->eFileLock = eFileLock;
34770
34771 sem_end_lock:
34772 return rc;
34773}
34774
34775/*
34776** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
34777** must be either NO_LOCK or SHARED_LOCK.
34778**
34779** If the locking level of the file descriptor is already at or below
34780** the requested locking level, this routine is a no-op.
34781*/
34782static int semXUnlock(sqlite3_file *id, int eFileLock) {
34783 unixFile *pFile = (unixFile*)id;
34784 sem_t *pSem = pFile->pInode->pSem;
34785
34786 assert( pFile );
34787 assert( pSem );
34788 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
34789 pFile->eFileLock, osGetpid(0)));
34790 assert( eFileLock<=SHARED_LOCK );
34791
34792 /* no-op if possible */
34793 if( pFile->eFileLock==eFileLock ){
34794 return SQLITE_OK;
34795 }
34796
34797 /* shared can just be set because we always have an exclusive */
34798 if (eFileLock==SHARED_LOCK) {
34799 pFile->eFileLock = eFileLock;
34800 return SQLITE_OK;
34801 }
34802
34803 /* no, really unlock. */
34804 if ( sem_post(pSem)==-1 ) {
34805 int rc, tErrno = errno;
34806 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
34807 if( IS_LOCK_ERROR(rc) ){
34808 storeLastErrno(pFile, tErrno);
34809 }
34810 return rc;
34811 }
34812 pFile->eFileLock = NO_LOCK;
34813 return SQLITE_OK;
34814}
34815
34816/*
34817 ** Close a file.
34818 */
34819static int semXClose(sqlite3_file *id) {
34820 if( id ){
34821 unixFile *pFile = (unixFile*)id;
34822 semXUnlock(id, NO_LOCK);
34823 assert( pFile );
34824 assert( unixFileMutexNotheld(pFile) );
34825 unixEnterMutex();
34826 releaseInodeInfo(pFile);
34827 unixLeaveMutex();
34828 closeUnixFile(id);
34829 }
34830 return SQLITE_OK;
34831}
34832
34833#endif /* OS_VXWORKS */
34834/*
34835** Named semaphore locking is only available on VxWorks.
34836**
34837*************** End of the named semaphore lock implementation ****************
34838******************************************************************************/
34839
34840
34841/******************************************************************************
34842*************************** Begin AFP Locking *********************************
34843**
34844** AFP is the Apple Filing Protocol. AFP is a network filesystem found
34845** on Apple Macintosh computers - both OS9 and OSX.
34846**
34847** Third-party implementations of AFP are available. But this code here
34848** only works on OSX.
34849*/
34850
34851#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
34852/*
34853** The afpLockingContext structure contains all afp lock specific state
34854*/
34855typedef struct afpLockingContext afpLockingContext;
34856struct afpLockingContext {
34857 int reserved;
34858 const char *dbPath; /* Name of the open file */
34859};
34860
34861struct ByteRangeLockPB2
34862{
34863 unsigned long long offset; /* offset to first byte to lock */
34864 unsigned long long length; /* nbr of bytes to lock */
34865 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
34866 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
34867 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
34868 int fd; /* file desc to assoc this lock with */
34869};
34870
34871#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
34872
34873/*
34874** This is a utility for setting or clearing a bit-range lock on an
34875** AFP filesystem.
34876**
34877** Return SQLITE_OK on success, SQLITE_BUSY on failure.
34878*/
34879static int afpSetLock(
34880 const char *path, /* Name of the file to be locked or unlocked */
34881 unixFile *pFile, /* Open file descriptor on path */
34882 unsigned long long offset, /* First byte to be locked */
34883 unsigned long long length, /* Number of bytes to lock */
34884 int setLockFlag /* True to set lock. False to clear lock */
34885){
34886 struct ByteRangeLockPB2 pb;
34887 int err;
34888
34889 pb.unLockFlag = setLockFlag ? 0 : 1;
34890 pb.startEndFlag = 0;
34891 pb.offset = offset;
34892 pb.length = length;
34893 pb.fd = pFile->h;
34894
34895 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
34896 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
34897 offset, length));
34898 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
34899 if ( err==-1 ) {
34900 int rc;
34901 int tErrno = errno;
34902 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
34903 path, tErrno, strerror(tErrno)));
34904#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
34905 rc = SQLITE_BUSY;
34906#else
34907 rc = sqliteErrorFromPosixError(tErrno,
34908 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
34909#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
34910 if( IS_LOCK_ERROR(rc) ){
34911 storeLastErrno(pFile, tErrno);
34912 }
34913 return rc;
34914 } else {
34915 return SQLITE_OK;
34916 }
34917}
34918
34919/*
34920** This routine checks if there is a RESERVED lock held on the specified
34921** file by this or any other process. If such a lock is held, set *pResOut
34922** to a non-zero value otherwise *pResOut is set to zero. The return value
34923** is set to SQLITE_OK unless an I/O error occurs during lock checking.
34924*/
34925static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
34926 int rc = SQLITE_OK;
34927 int reserved = 0;
34928 unixFile *pFile = (unixFile*)id;
34929 afpLockingContext *context;
34930
34931 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34932
34933 assert( pFile );
34934 context = (afpLockingContext *) pFile->lockingContext;
34935 if( context->reserved ){
34936 *pResOut = 1;
34937 return SQLITE_OK;
34938 }
34939 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
34940 /* Check if a thread in this process holds such a lock */
34941 if( pFile->pInode->eFileLock>SHARED_LOCK ){
34942 reserved = 1;
34943 }
34944
34945 /* Otherwise see if some other process holds it.
34946 */
34947 if( !reserved ){
34948 /* lock the RESERVED byte */
34949 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
34950 if( SQLITE_OK==lrc ){
34951 /* if we succeeded in taking the reserved lock, unlock it to restore
34952 ** the original state */
34953 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
34954 } else {
34955 /* if we failed to get the lock then someone else must have it */
34956 reserved = 1;
34957 }
34958 if( IS_LOCK_ERROR(lrc) ){
34959 rc=lrc;
34960 }
34961 }
34962
34963 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
34964 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
34965
34966 *pResOut = reserved;
34967 return rc;
34968}
34969
34970/*
34971** Lock the file with the lock specified by parameter eFileLock - one
34972** of the following:
34973**
34974** (1) SHARED_LOCK
34975** (2) RESERVED_LOCK
34976** (3) PENDING_LOCK
34977** (4) EXCLUSIVE_LOCK
34978**
34979** Sometimes when requesting one lock state, additional lock states
34980** are inserted in between. The locking might fail on one of the later
34981** transitions leaving the lock state different from what it started but
34982** still short of its goal. The following chart shows the allowed
34983** transitions and the inserted intermediate states:
34984**
34985** UNLOCKED -> SHARED
34986** SHARED -> RESERVED
34987** SHARED -> (PENDING) -> EXCLUSIVE
34988** RESERVED -> (PENDING) -> EXCLUSIVE
34989** PENDING -> EXCLUSIVE
34990**
34991** This routine will only increase a lock. Use the sqlite3OsUnlock()
34992** routine to lower a locking level.
34993*/
34994static int afpLock(sqlite3_file *id, int eFileLock){
34995 int rc = SQLITE_OK;
34996 unixFile *pFile = (unixFile*)id;
34997 unixInodeInfo *pInode = pFile->pInode;
34998 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
34999
35000 assert( pFile );
35001 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
35002 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
35003 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
35004
35005 /* If there is already a lock of this type or more restrictive on the
35006 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
35007 ** unixEnterMutex() hasn't been called yet.
35008 */
35009 if( pFile->eFileLock>=eFileLock ){
35010 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
35011 azFileLock(eFileLock)));
35012 return SQLITE_OK;
35013 }
35014
35015 /* Make sure the locking sequence is correct
35016 ** (1) We never move from unlocked to anything higher than shared lock.
35017 ** (2) SQLite never explicitly requests a pendig lock.
35018 ** (3) A shared lock is always held when a reserve lock is requested.
35019 */
35020 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
35021 assert( eFileLock!=PENDING_LOCK );
35022 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
35023
35024 /* This mutex is needed because pFile->pInode is shared across threads
35025 */
35026 pInode = pFile->pInode;
35027 sqlite3_mutex_enter(pInode->pLockMutex);
35028
35029 /* If some thread using this PID has a lock via a different unixFile*
35030 ** handle that precludes the requested lock, return BUSY.
35031 */
35032 if( (pFile->eFileLock!=pInode->eFileLock &&
35033 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
35034 ){
35035 rc = SQLITE_BUSY;
35036 goto afp_end_lock;
35037 }
35038
35039 /* If a SHARED lock is requested, and some thread using this PID already
35040 ** has a SHARED or RESERVED lock, then increment reference counts and
35041 ** return SQLITE_OK.
35042 */
35043 if( eFileLock==SHARED_LOCK &&
35044 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
35045 assert( eFileLock==SHARED_LOCK );
35046 assert( pFile->eFileLock==0 );
35047 assert( pInode->nShared>0 );
35048 pFile->eFileLock = SHARED_LOCK;
35049 pInode->nShared++;
35050 pInode->nLock++;
35051 goto afp_end_lock;
35052 }
35053
35054 /* A PENDING lock is needed before acquiring a SHARED lock and before
35055 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
35056 ** be released.
35057 */
35058 if( eFileLock==SHARED_LOCK
35059 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
35060 ){
35061 int failed;
35062 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
35063 if (failed) {
35064 rc = failed;
35065 goto afp_end_lock;
35066 }
35067 }
35068
35069 /* If control gets to this point, then actually go ahead and make
35070 ** operating system calls for the specified lock.
35071 */
35072 if( eFileLock==SHARED_LOCK ){
35073 int lrc1, lrc2, lrc1Errno = 0;
35074 long lk, mask;
35075
35076 assert( pInode->nShared==0 );
35077 assert( pInode->eFileLock==0 );
35078
35079 mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
35080 /* Now get the read-lock SHARED_LOCK */
35081 /* note that the quality of the randomness doesn't matter that much */
35082 lk = random();
35083 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
35084 lrc1 = afpSetLock(context->dbPath, pFile,
35085 SHARED_FIRST+pInode->sharedByte, 1, 1);
35086 if( IS_LOCK_ERROR(lrc1) ){
35087 lrc1Errno = pFile->lastErrno;
35088 }
35089 /* Drop the temporary PENDING lock */
35090 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
35091
35092 if( IS_LOCK_ERROR(lrc1) ) {
35093 storeLastErrno(pFile, lrc1Errno);
35094 rc = lrc1;
35095 goto afp_end_lock;
35096 } else if( IS_LOCK_ERROR(lrc2) ){
35097 rc = lrc2;
35098 goto afp_end_lock;
35099 } else if( lrc1 != SQLITE_OK ) {
35100 rc = lrc1;
35101 } else {
35102 pFile->eFileLock = SHARED_LOCK;
35103 pInode->nLock++;
35104 pInode->nShared = 1;
35105 }
35106 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
35107 /* We are trying for an exclusive lock but another thread in this
35108 ** same process is still holding a shared lock. */
35109 rc = SQLITE_BUSY;
35110 }else{
35111 /* The request was for a RESERVED or EXCLUSIVE lock. It is
35112 ** assumed that there is a SHARED or greater lock on the file
35113 ** already.
35114 */
35115 int failed = 0;
35116 assert( 0!=pFile->eFileLock );
35117 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
35118 /* Acquire a RESERVED lock */
35119 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
35120 if( !failed ){
35121 context->reserved = 1;
35122 }
35123 }
35124 if (!failed && eFileLock == EXCLUSIVE_LOCK) {
35125 /* Acquire an EXCLUSIVE lock */
35126
35127 /* Remove the shared lock before trying the range. we'll need to
35128 ** reestablish the shared lock if we can't get the afpUnlock
35129 */
35130 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
35131 pInode->sharedByte, 1, 0)) ){
35132 int failed2 = SQLITE_OK;
35133 /* now attemmpt to get the exclusive lock range */
35134 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
35135 SHARED_SIZE, 1);
35136 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
35137 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
35138 /* Can't reestablish the shared lock. Sqlite can't deal, this is
35139 ** a critical I/O error
35140 */
35141 rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 :
35142 SQLITE_IOERR_LOCK;
35143 goto afp_end_lock;
35144 }
35145 }else{
35146 rc = failed;
35147 }
35148 }
35149 if( failed ){
35150 rc = failed;
35151 }
35152 }
35153
35154 if( rc==SQLITE_OK ){
35155 pFile->eFileLock = eFileLock;
35156 pInode->eFileLock = eFileLock;
35157 }else if( eFileLock==EXCLUSIVE_LOCK ){
35158 pFile->eFileLock = PENDING_LOCK;
35159 pInode->eFileLock = PENDING_LOCK;
35160 }
35161
35162afp_end_lock:
35163 sqlite3_mutex_leave(pInode->pLockMutex);
35164 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
35165 rc==SQLITE_OK ? "ok" : "failed"));
35166 return rc;
35167}
35168
35169/*
35170** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
35171** must be either NO_LOCK or SHARED_LOCK.
35172**
35173** If the locking level of the file descriptor is already at or below
35174** the requested locking level, this routine is a no-op.
35175*/
35176static int afpUnlock(sqlite3_file *id, int eFileLock) {
35177 int rc = SQLITE_OK;
35178 unixFile *pFile = (unixFile*)id;
35179 unixInodeInfo *pInode;
35180 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
35181 int skipShared = 0;
35182#ifdef SQLITE_TEST
35183 int h = pFile->h;
35184#endif
35185
35186 assert( pFile );
35187 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
35188 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
35189 osGetpid(0)));
35190
35191 assert( eFileLock<=SHARED_LOCK );
35192 if( pFile->eFileLock<=eFileLock ){
35193 return SQLITE_OK;
35194 }
35195 pInode = pFile->pInode;
35196 sqlite3_mutex_enter(pInode->pLockMutex);
35197 assert( pInode->nShared!=0 );
35198 if( pFile->eFileLock>SHARED_LOCK ){
35199 assert( pInode->eFileLock==pFile->eFileLock );
35200 SimulateIOErrorBenign(1);
35201 SimulateIOError( h=(-1) )
35202 SimulateIOErrorBenign(0);
35203
35204#ifdef SQLITE_DEBUG
35205 /* When reducing a lock such that other processes can start
35206 ** reading the database file again, make sure that the
35207 ** transaction counter was updated if any part of the database
35208 ** file changed. If the transaction counter is not updated,
35209 ** other connections to the same file might not realize that
35210 ** the file has changed and hence might not know to flush their
35211 ** cache. The use of a stale cache can lead to database corruption.
35212 */
35213 assert( pFile->inNormalWrite==0
35214 || pFile->dbUpdate==0
35215 || pFile->transCntrChng==1 );
35216 pFile->inNormalWrite = 0;
35217#endif
35218
35219 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
35220 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
35221 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
35222 /* only re-establish the shared lock if necessary */
35223 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
35224 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
35225 } else {
35226 skipShared = 1;
35227 }
35228 }
35229 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
35230 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
35231 }
35232 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
35233 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
35234 if( !rc ){
35235 context->reserved = 0;
35236 }
35237 }
35238 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
35239 pInode->eFileLock = SHARED_LOCK;
35240 }
35241 }
35242 if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
35243
35244 /* Decrement the shared lock counter. Release the lock using an
35245 ** OS call only when all threads in this same process have released
35246 ** the lock.
35247 */
35248 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
35249 pInode->nShared--;
35250 if( pInode->nShared==0 ){
35251 SimulateIOErrorBenign(1);
35252 SimulateIOError( h=(-1) )
35253 SimulateIOErrorBenign(0);
35254 if( !skipShared ){
35255 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
35256 }
35257 if( !rc ){
35258 pInode->eFileLock = NO_LOCK;
35259 pFile->eFileLock = NO_LOCK;
35260 }
35261 }
35262 if( rc==SQLITE_OK ){
35263 pInode->nLock--;
35264 assert( pInode->nLock>=0 );
35265 if( pInode->nLock==0 ) closePendingFds(pFile);
35266 }
35267 }
35268
35269 sqlite3_mutex_leave(pInode->pLockMutex);
35270 if( rc==SQLITE_OK ){
35271 pFile->eFileLock = eFileLock;
35272 }
35273 return rc;
35274}
35275
35276/*
35277** Close a file & cleanup AFP specific locking context
35278*/
35279static int afpClose(sqlite3_file *id) {
35280 int rc = SQLITE_OK;
35281 unixFile *pFile = (unixFile*)id;
35282 assert( id!=0 );
35283 afpUnlock(id, NO_LOCK);
35284 assert( unixFileMutexNotheld(pFile) );
35285 unixEnterMutex();
35286 if( pFile->pInode ){
35287 unixInodeInfo *pInode = pFile->pInode;
35288 sqlite3_mutex_enter(pInode->pLockMutex);
35289 if( pInode->nLock ){
35290 /* If there are outstanding locks, do not actually close the file just
35291 ** yet because that would clear those locks. Instead, add the file
35292 ** descriptor to pInode->aPending. It will be automatically closed when
35293 ** the last lock is cleared.
35294 */
35295 setPendingFd(pFile);
35296 }
35297 sqlite3_mutex_leave(pInode->pLockMutex);
35298 }
35299 releaseInodeInfo(pFile);
35300 sqlite3_free(pFile->lockingContext);
35301 rc = closeUnixFile(id);
35302 unixLeaveMutex();
35303 return rc;
35304}
35305
35306#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
35307/*
35308** The code above is the AFP lock implementation. The code is specific
35309** to MacOSX and does not work on other unix platforms. No alternative
35310** is available. If you don't compile for a mac, then the "unix-afp"
35311** VFS is not available.
35312**
35313********************* End of the AFP lock implementation **********************
35314******************************************************************************/
35315
35316/******************************************************************************
35317*************************** Begin NFS Locking ********************************/
35318
35319#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
35320/*
35321 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
35322 ** must be either NO_LOCK or SHARED_LOCK.
35323 **
35324 ** If the locking level of the file descriptor is already at or below
35325 ** the requested locking level, this routine is a no-op.
35326 */
35327static int nfsUnlock(sqlite3_file *id, int eFileLock){
35328 return posixUnlock(id, eFileLock, 1);
35329}
35330
35331#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
35332/*
35333** The code above is the NFS lock implementation. The code is specific
35334** to MacOSX and does not work on other unix platforms. No alternative
35335** is available.
35336**
35337********************* End of the NFS lock implementation **********************
35338******************************************************************************/
35339
35340/******************************************************************************
35341**************** Non-locking sqlite3_file methods *****************************
35342**
35343** The next division contains implementations for all methods of the
35344** sqlite3_file object other than the locking methods. The locking
35345** methods were defined in divisions above (one locking method per
35346** division). Those methods that are common to all locking modes
35347** are gather together into this division.
35348*/
35349
35350/*
35351** Seek to the offset passed as the second argument, then read cnt
35352** bytes into pBuf. Return the number of bytes actually read.
35353**
35354** NB: If you define USE_PREAD or USE_PREAD64, then it might also
35355** be necessary to define _XOPEN_SOURCE to be 500. This varies from
35356** one system to another. Since SQLite does not define USE_PREAD
35357** in any form by default, we will not attempt to define _XOPEN_SOURCE.
35358** See tickets #2741 and #2681.
35359**
35360** To avoid stomping the errno value on a failed read the lastErrno value
35361** is set before returning.
35362*/
35363static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
35364 int got;
35365 int prior = 0;
35366#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
35367 i64 newOffset;
35368#endif
35369 TIMER_START;
35370 assert( cnt==(cnt&0x1ffff) );
35371 assert( id->h>2 );
35372 do{
35373#if defined(USE_PREAD)
35374 got = osPread(id->h, pBuf, cnt, offset);
35375 SimulateIOError( got = -1 );
35376#elif defined(USE_PREAD64)
35377 got = osPread64(id->h, pBuf, cnt, offset);
35378 SimulateIOError( got = -1 );
35379#else
35380 newOffset = lseek(id->h, offset, SEEK_SET);
35381 SimulateIOError( newOffset = -1 );
35382 if( newOffset<0 ){
35383 storeLastErrno((unixFile*)id, errno);
35384 return -1;
35385 }
35386 got = osRead(id->h, pBuf, cnt);
35387#endif
35388 if( got==cnt ) break;
35389 if( got<0 ){
35390 if( errno==EINTR ){ got = 1; continue; }
35391 prior = 0;
35392 storeLastErrno((unixFile*)id, errno);
35393 break;
35394 }else if( got>0 ){
35395 cnt -= got;
35396 offset += got;
35397 prior += got;
35398 pBuf = (void*)(got + (char*)pBuf);
35399 }
35400 }while( got>0 );
35401 TIMER_END;
35402 OSTRACE(("READ %-3d %5d %7lld %llu\n",
35403 id->h, got+prior, offset-prior, TIMER_ELAPSED));
35404 return got+prior;
35405}
35406
35407/*
35408** Read data from a file into a buffer. Return SQLITE_OK if all
35409** bytes were read successfully and SQLITE_IOERR if anything goes
35410** wrong.
35411*/
35412static int unixRead(
35413 sqlite3_file *id,
35414 void *pBuf,
35415 int amt,
35416 sqlite3_int64 offset
35417){
35418 unixFile *pFile = (unixFile *)id;
35419 int got;
35420 assert( id );
35421 assert( offset>=0 );
35422 assert( amt>0 );
35423
35424 /* If this is a database file (not a journal, master-journal or temp
35425 ** file), the bytes in the locking range should never be read or written. */
35426#if 0
35427 assert( pFile->pPreallocatedUnused==0
35428 || offset>=PENDING_BYTE+512
35429 || offset+amt<=PENDING_BYTE
35430 );
35431#endif
35432
35433#if SQLITE_MAX_MMAP_SIZE>0
35434 /* Deal with as much of this read request as possible by transfering
35435 ** data from the memory mapping using memcpy(). */
35436 if( offset<pFile->mmapSize ){
35437 if( offset+amt <= pFile->mmapSize ){
35438 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
35439 return SQLITE_OK;
35440 }else{
35441 int nCopy = pFile->mmapSize - offset;
35442 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
35443 pBuf = &((u8 *)pBuf)[nCopy];
35444 amt -= nCopy;
35445 offset += nCopy;
35446 }
35447 }
35448#endif
35449
35450 got = seekAndRead(pFile, offset, pBuf, amt);
35451 if( got==amt ){
35452 return SQLITE_OK;
35453 }else if( got<0 ){
35454 /* lastErrno set by seekAndRead */
35455 return SQLITE_IOERR_READ;
35456 }else{
35457 storeLastErrno(pFile, 0); /* not a system error */
35458 /* Unread parts of the buffer must be zero-filled */
35459 memset(&((char*)pBuf)[got], 0, amt-got);
35460 return SQLITE_IOERR_SHORT_READ;
35461 }
35462}
35463
35464/*
35465** Attempt to seek the file-descriptor passed as the first argument to
35466** absolute offset iOff, then attempt to write nBuf bytes of data from
35467** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
35468** return the actual number of bytes written (which may be less than
35469** nBuf).
35470*/
35471static int seekAndWriteFd(
35472 int fd, /* File descriptor to write to */
35473 i64 iOff, /* File offset to begin writing at */
35474 const void *pBuf, /* Copy data from this buffer to the file */
35475 int nBuf, /* Size of buffer pBuf in bytes */
35476 int *piErrno /* OUT: Error number if error occurs */
35477){
35478 int rc = 0; /* Value returned by system call */
35479
35480 assert( nBuf==(nBuf&0x1ffff) );
35481 assert( fd>2 );
35482 assert( piErrno!=0 );
35483 nBuf &= 0x1ffff;
35484 TIMER_START;
35485
35486#if defined(USE_PREAD)
35487 do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
35488#elif defined(USE_PREAD64)
35489 do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
35490#else
35491 do{
35492 i64 iSeek = lseek(fd, iOff, SEEK_SET);
35493 SimulateIOError( iSeek = -1 );
35494 if( iSeek<0 ){
35495 rc = -1;
35496 break;
35497 }
35498 rc = osWrite(fd, pBuf, nBuf);
35499 }while( rc<0 && errno==EINTR );
35500#endif
35501
35502 TIMER_END;
35503 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
35504
35505 if( rc<0 ) *piErrno = errno;
35506 return rc;
35507}
35508
35509
35510/*
35511** Seek to the offset in id->offset then read cnt bytes into pBuf.
35512** Return the number of bytes actually read. Update the offset.
35513**
35514** To avoid stomping the errno value on a failed write the lastErrno value
35515** is set before returning.
35516*/
35517static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
35518 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
35519}
35520
35521
35522/*
35523** Write data from a buffer into a file. Return SQLITE_OK on success
35524** or some other error code on failure.
35525*/
35526static int unixWrite(
35527 sqlite3_file *id,
35528 const void *pBuf,
35529 int amt,
35530 sqlite3_int64 offset
35531){
35532 unixFile *pFile = (unixFile*)id;
35533 int wrote = 0;
35534 assert( id );
35535 assert( amt>0 );
35536
35537 /* If this is a database file (not a journal, master-journal or temp
35538 ** file), the bytes in the locking range should never be read or written. */
35539#if 0
35540 assert( pFile->pPreallocatedUnused==0
35541 || offset>=PENDING_BYTE+512
35542 || offset+amt<=PENDING_BYTE
35543 );
35544#endif
35545
35546#ifdef SQLITE_DEBUG
35547 /* If we are doing a normal write to a database file (as opposed to
35548 ** doing a hot-journal rollback or a write to some file other than a
35549 ** normal database file) then record the fact that the database
35550 ** has changed. If the transaction counter is modified, record that
35551 ** fact too.
35552 */
35553 if( pFile->inNormalWrite ){
35554 pFile->dbUpdate = 1; /* The database has been modified */
35555 if( offset<=24 && offset+amt>=27 ){
35556 int rc;
35557 char oldCntr[4];
35558 SimulateIOErrorBenign(1);
35559 rc = seekAndRead(pFile, 24, oldCntr, 4);
35560 SimulateIOErrorBenign(0);
35561 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
35562 pFile->transCntrChng = 1; /* The transaction counter has changed */
35563 }
35564 }
35565 }
35566#endif
35567
35568#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
35569 /* Deal with as much of this write request as possible by transfering
35570 ** data from the memory mapping using memcpy(). */
35571 if( offset<pFile->mmapSize ){
35572 if( offset+amt <= pFile->mmapSize ){
35573 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
35574 return SQLITE_OK;
35575 }else{
35576 int nCopy = pFile->mmapSize - offset;
35577 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
35578 pBuf = &((u8 *)pBuf)[nCopy];
35579 amt -= nCopy;
35580 offset += nCopy;
35581 }
35582 }
35583#endif
35584
35585 while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){
35586 amt -= wrote;
35587 offset += wrote;
35588 pBuf = &((char*)pBuf)[wrote];
35589 }
35590 SimulateIOError(( wrote=(-1), amt=1 ));
35591 SimulateDiskfullError(( wrote=0, amt=1 ));
35592
35593 if( amt>wrote ){
35594 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
35595 /* lastErrno set by seekAndWrite */
35596 return SQLITE_IOERR_WRITE;
35597 }else{
35598 storeLastErrno(pFile, 0); /* not a system error */
35599 return SQLITE_FULL;
35600 }
35601 }
35602
35603 return SQLITE_OK;
35604}
35605
35606#ifdef SQLITE_TEST
35607/*
35608** Count the number of fullsyncs and normal syncs. This is used to test
35609** that syncs and fullsyncs are occurring at the right times.
35610*/
35611SQLITE_API int sqlite3_sync_count = 0;
35612SQLITE_API int sqlite3_fullsync_count = 0;
35613#endif
35614
35615/*
35616** We do not trust systems to provide a working fdatasync(). Some do.
35617** Others do no. To be safe, we will stick with the (slightly slower)
35618** fsync(). If you know that your system does support fdatasync() correctly,
35619** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
35620*/
35621#if !defined(fdatasync) && !HAVE_FDATASYNC
35622# define fdatasync fsync
35623#endif
35624
35625/*
35626** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
35627** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
35628** only available on Mac OS X. But that could change.
35629*/
35630#ifdef F_FULLFSYNC
35631# define HAVE_FULLFSYNC 1
35632#else
35633# define HAVE_FULLFSYNC 0
35634#endif
35635
35636
35637/*
35638** The fsync() system call does not work as advertised on many
35639** unix systems. The following procedure is an attempt to make
35640** it work better.
35641**
35642** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
35643** for testing when we want to run through the test suite quickly.
35644** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
35645** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
35646** or power failure will likely corrupt the database file.
35647**
35648** SQLite sets the dataOnly flag if the size of the file is unchanged.
35649** The idea behind dataOnly is that it should only write the file content
35650** to disk, not the inode. We only set dataOnly if the file size is
35651** unchanged since the file size is part of the inode. However,
35652** Ted Ts'o tells us that fdatasync() will also write the inode if the
35653** file size has changed. The only real difference between fdatasync()
35654** and fsync(), Ted tells us, is that fdatasync() will not flush the
35655** inode if the mtime or owner or other inode attributes have changed.
35656** We only care about the file size, not the other file attributes, so
35657** as far as SQLite is concerned, an fdatasync() is always adequate.
35658** So, we always use fdatasync() if it is available, regardless of
35659** the value of the dataOnly flag.
35660*/
35661static int full_fsync(int fd, int fullSync, int dataOnly){
35662 int rc;
35663
35664 /* The following "ifdef/elif/else/" block has the same structure as
35665 ** the one below. It is replicated here solely to avoid cluttering
35666 ** up the real code with the UNUSED_PARAMETER() macros.
35667 */
35668#ifdef SQLITE_NO_SYNC
35669 UNUSED_PARAMETER(fd);
35670 UNUSED_PARAMETER(fullSync);
35671 UNUSED_PARAMETER(dataOnly);
35672#elif HAVE_FULLFSYNC
35673 UNUSED_PARAMETER(dataOnly);
35674#else
35675 UNUSED_PARAMETER(fullSync);
35676 UNUSED_PARAMETER(dataOnly);
35677#endif
35678
35679 /* Record the number of times that we do a normal fsync() and
35680 ** FULLSYNC. This is used during testing to verify that this procedure
35681 ** gets called with the correct arguments.
35682 */
35683#ifdef SQLITE_TEST
35684 if( fullSync ) sqlite3_fullsync_count++;
35685 sqlite3_sync_count++;
35686#endif
35687
35688 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
35689 ** no-op. But go ahead and call fstat() to validate the file
35690 ** descriptor as we need a method to provoke a failure during
35691 ** coverate testing.
35692 */
35693#ifdef SQLITE_NO_SYNC
35694 {
35695 struct stat buf;
35696 rc = osFstat(fd, &buf);
35697 }
35698#elif HAVE_FULLFSYNC
35699 if( fullSync ){
35700 rc = osFcntl(fd, F_FULLFSYNC, 0);
35701 }else{
35702 rc = 1;
35703 }
35704 /* If the FULLFSYNC failed, fall back to attempting an fsync().
35705 ** It shouldn't be possible for fullfsync to fail on the local
35706 ** file system (on OSX), so failure indicates that FULLFSYNC
35707 ** isn't supported for this file system. So, attempt an fsync
35708 ** and (for now) ignore the overhead of a superfluous fcntl call.
35709 ** It'd be better to detect fullfsync support once and avoid
35710 ** the fcntl call every time sync is called.
35711 */
35712 if( rc ) rc = fsync(fd);
35713
35714#elif defined(__APPLE__)
35715 /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
35716 ** so currently we default to the macro that redefines fdatasync to fsync
35717 */
35718 rc = fsync(fd);
35719#else
35720 rc = fdatasync(fd);
35721#if OS_VXWORKS
35722 if( rc==-1 && errno==ENOTSUP ){
35723 rc = fsync(fd);
35724 }
35725#endif /* OS_VXWORKS */
35726#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
35727
35728 if( OS_VXWORKS && rc!= -1 ){
35729 rc = 0;
35730 }
35731 return rc;
35732}
35733
35734/*
35735** Open a file descriptor to the directory containing file zFilename.
35736** If successful, *pFd is set to the opened file descriptor and
35737** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
35738** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
35739** value.
35740**
35741** The directory file descriptor is used for only one thing - to
35742** fsync() a directory to make sure file creation and deletion events
35743** are flushed to disk. Such fsyncs are not needed on newer
35744** journaling filesystems, but are required on older filesystems.
35745**
35746** This routine can be overridden using the xSetSysCall interface.
35747** The ability to override this routine was added in support of the
35748** chromium sandbox. Opening a directory is a security risk (we are
35749** told) so making it overrideable allows the chromium sandbox to
35750** replace this routine with a harmless no-op. To make this routine
35751** a no-op, replace it with a stub that returns SQLITE_OK but leaves
35752** *pFd set to a negative number.
35753**
35754** If SQLITE_OK is returned, the caller is responsible for closing
35755** the file descriptor *pFd using close().
35756*/
35757static int openDirectory(const char *zFilename, int *pFd){
35758 int ii;
35759 int fd = -1;
35760 char zDirname[MAX_PATHNAME+1];
35761
35762 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
35763 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
35764 if( ii>0 ){
35765 zDirname[ii] = '\0';
35766 }else{
35767 if( zDirname[0]!='/' ) zDirname[0] = '.';
35768 zDirname[1] = 0;
35769 }
35770 fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
35771 if( fd>=0 ){
35772 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
35773 }
35774 *pFd = fd;
35775 if( fd>=0 ) return SQLITE_OK;
35776 return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname);
35777}
35778
35779/*
35780** Make sure all writes to a particular file are committed to disk.
35781**
35782** If dataOnly==0 then both the file itself and its metadata (file
35783** size, access time, etc) are synced. If dataOnly!=0 then only the
35784** file data is synced.
35785**
35786** Under Unix, also make sure that the directory entry for the file
35787** has been created by fsync-ing the directory that contains the file.
35788** If we do not do this and we encounter a power failure, the directory
35789** entry for the journal might not exist after we reboot. The next
35790** SQLite to access the file will not know that the journal exists (because
35791** the directory entry for the journal was never created) and the transaction
35792** will not roll back - possibly leading to database corruption.
35793*/
35794static int unixSync(sqlite3_file *id, int flags){
35795 int rc;
35796 unixFile *pFile = (unixFile*)id;
35797
35798 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
35799 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
35800
35801 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
35802 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
35803 || (flags&0x0F)==SQLITE_SYNC_FULL
35804 );
35805
35806 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
35807 ** line is to test that doing so does not cause any problems.
35808 */
35809 SimulateDiskfullError( return SQLITE_FULL );
35810
35811 assert( pFile );
35812 OSTRACE(("SYNC %-3d\n", pFile->h));
35813 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
35814 SimulateIOError( rc=1 );
35815 if( rc ){
35816 storeLastErrno(pFile, errno);
35817 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
35818 }
35819
35820 /* Also fsync the directory containing the file if the DIRSYNC flag
35821 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
35822 ** are unable to fsync a directory, so ignore errors on the fsync.
35823 */
35824 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
35825 int dirfd;
35826 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
35827 HAVE_FULLFSYNC, isFullsync));
35828 rc = osOpenDirectory(pFile->zPath, &dirfd);
35829 if( rc==SQLITE_OK ){
35830 full_fsync(dirfd, 0, 0);
35831 robust_close(pFile, dirfd, __LINE__);
35832 }else{
35833 assert( rc==SQLITE_CANTOPEN );
35834 rc = SQLITE_OK;
35835 }
35836 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
35837 }
35838 return rc;
35839}
35840
35841/*
35842** Truncate an open file to a specified size
35843*/
35844static int unixTruncate(sqlite3_file *id, i64 nByte){
35845 unixFile *pFile = (unixFile *)id;
35846 int rc;
35847 assert( pFile );
35848 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
35849
35850 /* If the user has configured a chunk-size for this file, truncate the
35851 ** file so that it consists of an integer number of chunks (i.e. the
35852 ** actual file size after the operation may be larger than the requested
35853 ** size).
35854 */
35855 if( pFile->szChunk>0 ){
35856 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
35857 }
35858
35859 rc = robust_ftruncate(pFile->h, nByte);
35860 if( rc ){
35861 storeLastErrno(pFile, errno);
35862 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
35863 }else{
35864#ifdef SQLITE_DEBUG
35865 /* If we are doing a normal write to a database file (as opposed to
35866 ** doing a hot-journal rollback or a write to some file other than a
35867 ** normal database file) and we truncate the file to zero length,
35868 ** that effectively updates the change counter. This might happen
35869 ** when restoring a database using the backup API from a zero-length
35870 ** source.
35871 */
35872 if( pFile->inNormalWrite && nByte==0 ){
35873 pFile->transCntrChng = 1;
35874 }
35875#endif
35876
35877#if SQLITE_MAX_MMAP_SIZE>0
35878 /* If the file was just truncated to a size smaller than the currently
35879 ** mapped region, reduce the effective mapping size as well. SQLite will
35880 ** use read() and write() to access data beyond this point from now on.
35881 */
35882 if( nByte<pFile->mmapSize ){
35883 pFile->mmapSize = nByte;
35884 }
35885#endif
35886
35887 return SQLITE_OK;
35888 }
35889}
35890
35891/*
35892** Determine the current size of a file in bytes
35893*/
35894static int unixFileSize(sqlite3_file *id, i64 *pSize){
35895 int rc;
35896 struct stat buf;
35897 assert( id );
35898 rc = osFstat(((unixFile*)id)->h, &buf);
35899 SimulateIOError( rc=1 );
35900 if( rc!=0 ){
35901 storeLastErrno((unixFile*)id, errno);
35902 return SQLITE_IOERR_FSTAT;
35903 }
35904 *pSize = buf.st_size;
35905
35906 /* When opening a zero-size database, the findInodeInfo() procedure
35907 ** writes a single byte into that file in order to work around a bug
35908 ** in the OS-X msdos filesystem. In order to avoid problems with upper
35909 ** layers, we need to report this file size as zero even though it is
35910 ** really 1. Ticket #3260.
35911 */
35912 if( *pSize==1 ) *pSize = 0;
35913
35914
35915 return SQLITE_OK;
35916}
35917
35918#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
35919/*
35920** Handler for proxy-locking file-control verbs. Defined below in the
35921** proxying locking division.
35922*/
35923static int proxyFileControl(sqlite3_file*,int,void*);
35924#endif
35925
35926/*
35927** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
35928** file-control operation. Enlarge the database to nBytes in size
35929** (rounded up to the next chunk-size). If the database is already
35930** nBytes or larger, this routine is a no-op.
35931*/
35932static int fcntlSizeHint(unixFile *pFile, i64 nByte){
35933 if( pFile->szChunk>0 ){
35934 i64 nSize; /* Required file size */
35935 struct stat buf; /* Used to hold return values of fstat() */
35936
35937 if( osFstat(pFile->h, &buf) ){
35938 return SQLITE_IOERR_FSTAT;
35939 }
35940
35941 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
35942 if( nSize>(i64)buf.st_size ){
35943
35944#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
35945 /* The code below is handling the return value of osFallocate()
35946 ** correctly. posix_fallocate() is defined to "returns zero on success,
35947 ** or an error number on failure". See the manpage for details. */
35948 int err;
35949 do{
35950 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
35951 }while( err==EINTR );
35952 if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE;
35953#else
35954 /* If the OS does not have posix_fallocate(), fake it. Write a
35955 ** single byte to the last byte in each block that falls entirely
35956 ** within the extended region. Then, if required, a single byte
35957 ** at offset (nSize-1), to set the size of the file correctly.
35958 ** This is a similar technique to that used by glibc on systems
35959 ** that do not have a real fallocate() call.
35960 */
35961 int nBlk = buf.st_blksize; /* File-system block size */
35962 int nWrite = 0; /* Number of bytes written by seekAndWrite */
35963 i64 iWrite; /* Next offset to write to */
35964
35965 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
35966 assert( iWrite>=buf.st_size );
35967 assert( ((iWrite+1)%nBlk)==0 );
35968 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
35969 if( iWrite>=nSize ) iWrite = nSize - 1;
35970 nWrite = seekAndWrite(pFile, iWrite, "", 1);
35971 if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
35972 }
35973#endif
35974 }
35975 }
35976
35977#if SQLITE_MAX_MMAP_SIZE>0
35978 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
35979 int rc;
35980 if( pFile->szChunk<=0 ){
35981 if( robust_ftruncate(pFile->h, nByte) ){
35982 storeLastErrno(pFile, errno);
35983 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
35984 }
35985 }
35986
35987 rc = unixMapfile(pFile, nByte);
35988 return rc;
35989 }
35990#endif
35991
35992 return SQLITE_OK;
35993}
35994
35995/*
35996** If *pArg is initially negative then this is a query. Set *pArg to
35997** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
35998**
35999** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
36000*/
36001static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
36002 if( *pArg<0 ){
36003 *pArg = (pFile->ctrlFlags & mask)!=0;
36004 }else if( (*pArg)==0 ){
36005 pFile->ctrlFlags &= ~mask;
36006 }else{
36007 pFile->ctrlFlags |= mask;
36008 }
36009}
36010
36011/* Forward declaration */
36012static int unixGetTempname(int nBuf, char *zBuf);
36013
36014/*
36015** Information and control of an open file handle.
36016*/
36017static int unixFileControl(sqlite3_file *id, int op, void *pArg){
36018 unixFile *pFile = (unixFile*)id;
36019 switch( op ){
36020#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
36021 case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: {
36022 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
36023 return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK;
36024 }
36025 case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: {
36026 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
36027 return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK;
36028 }
36029 case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: {
36030 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
36031 return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK;
36032 }
36033#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
36034
36035 case SQLITE_FCNTL_LOCKSTATE: {
36036 *(int*)pArg = pFile->eFileLock;
36037 return SQLITE_OK;
36038 }
36039 case SQLITE_FCNTL_LAST_ERRNO: {
36040 *(int*)pArg = pFile->lastErrno;
36041 return SQLITE_OK;
36042 }
36043 case SQLITE_FCNTL_CHUNK_SIZE: {
36044 pFile->szChunk = *(int *)pArg;
36045 return SQLITE_OK;
36046 }
36047 case SQLITE_FCNTL_SIZE_HINT: {
36048 int rc;
36049 SimulateIOErrorBenign(1);
36050 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
36051 SimulateIOErrorBenign(0);
36052 return rc;
36053 }
36054 case SQLITE_FCNTL_PERSIST_WAL: {
36055 unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
36056 return SQLITE_OK;
36057 }
36058 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
36059 unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
36060 return SQLITE_OK;
36061 }
36062 case SQLITE_FCNTL_VFSNAME: {
36063 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
36064 return SQLITE_OK;
36065 }
36066 case SQLITE_FCNTL_TEMPFILENAME: {
36067 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
36068 if( zTFile ){
36069 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
36070 *(char**)pArg = zTFile;
36071 }
36072 return SQLITE_OK;
36073 }
36074 case SQLITE_FCNTL_HAS_MOVED: {
36075 *(int*)pArg = fileHasMoved(pFile);
36076 return SQLITE_OK;
36077 }
36078#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
36079 case SQLITE_FCNTL_LOCK_TIMEOUT: {
36080 pFile->iBusyTimeout = *(int*)pArg;
36081 return SQLITE_OK;
36082 }
36083#endif
36084#if SQLITE_MAX_MMAP_SIZE>0
36085 case SQLITE_FCNTL_MMAP_SIZE: {
36086 i64 newLimit = *(i64*)pArg;
36087 int rc = SQLITE_OK;
36088 if( newLimit>sqlite3GlobalConfig.mxMmap ){
36089 newLimit = sqlite3GlobalConfig.mxMmap;
36090 }
36091
36092 /* The value of newLimit may be eventually cast to (size_t) and passed
36093 ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a
36094 ** 64-bit type. */
36095 if( newLimit>0 && sizeof(size_t)<8 ){
36096 newLimit = (newLimit & 0x7FFFFFFF);
36097 }
36098
36099 *(i64*)pArg = pFile->mmapSizeMax;
36100 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
36101 pFile->mmapSizeMax = newLimit;
36102 if( pFile->mmapSize>0 ){
36103 unixUnmapfile(pFile);
36104 rc = unixMapfile(pFile, -1);
36105 }
36106 }
36107 return rc;
36108 }
36109#endif
36110#ifdef SQLITE_DEBUG
36111 /* The pager calls this method to signal that it has done
36112 ** a rollback and that the database is therefore unchanged and
36113 ** it hence it is OK for the transaction change counter to be
36114 ** unchanged.
36115 */
36116 case SQLITE_FCNTL_DB_UNCHANGED: {
36117 ((unixFile*)id)->dbUpdate = 0;
36118 return SQLITE_OK;
36119 }
36120#endif
36121#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
36122 case SQLITE_FCNTL_SET_LOCKPROXYFILE:
36123 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
36124 return proxyFileControl(id,op,pArg);
36125 }
36126#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
36127 }
36128 return SQLITE_NOTFOUND;
36129}
36130
36131/*
36132** If pFd->sectorSize is non-zero when this function is called, it is a
36133** no-op. Otherwise, the values of pFd->sectorSize and
36134** pFd->deviceCharacteristics are set according to the file-system
36135** characteristics.
36136**
36137** There are two versions of this function. One for QNX and one for all
36138** other systems.
36139*/
36140#ifndef __QNXNTO__
36141static void setDeviceCharacteristics(unixFile *pFd){
36142 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
36143 if( pFd->sectorSize==0 ){
36144#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
36145 int res;
36146 u32 f = 0;
36147
36148 /* Check for support for F2FS atomic batch writes. */
36149 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
36150 if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){
36151 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
36152 }
36153#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
36154
36155 /* Set the POWERSAFE_OVERWRITE flag if requested. */
36156 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
36157 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
36158 }
36159
36160 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
36161 }
36162}
36163#else
36164#include <sys/dcmd_blk.h>
36165#include <sys/statvfs.h>
36166static void setDeviceCharacteristics(unixFile *pFile){
36167 if( pFile->sectorSize == 0 ){
36168 struct statvfs fsInfo;
36169
36170 /* Set defaults for non-supported filesystems */
36171 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
36172 pFile->deviceCharacteristics = 0;
36173 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
36174 return;
36175 }
36176
36177 if( !strcmp(fsInfo.f_basetype, "tmp") ) {
36178 pFile->sectorSize = fsInfo.f_bsize;
36179 pFile->deviceCharacteristics =
36180 SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */
36181 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
36182 ** the write succeeds */
36183 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
36184 ** so it is ordered */
36185 0;
36186 }else if( strstr(fsInfo.f_basetype, "etfs") ){
36187 pFile->sectorSize = fsInfo.f_bsize;
36188 pFile->deviceCharacteristics =
36189 /* etfs cluster size writes are atomic */
36190 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
36191 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
36192 ** the write succeeds */
36193 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
36194 ** so it is ordered */
36195 0;
36196 }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
36197 pFile->sectorSize = fsInfo.f_bsize;
36198 pFile->deviceCharacteristics =
36199 SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */
36200 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
36201 ** the write succeeds */
36202 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
36203 ** so it is ordered */
36204 0;
36205 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
36206 pFile->sectorSize = fsInfo.f_bsize;
36207 pFile->deviceCharacteristics =
36208 /* full bitset of atomics from max sector size and smaller */
36209 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
36210 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
36211 ** so it is ordered */
36212 0;
36213 }else if( strstr(fsInfo.f_basetype, "dos") ){
36214 pFile->sectorSize = fsInfo.f_bsize;
36215 pFile->deviceCharacteristics =
36216 /* full bitset of atomics from max sector size and smaller */
36217 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
36218 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
36219 ** so it is ordered */
36220 0;
36221 }else{
36222 pFile->deviceCharacteristics =
36223 SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */
36224 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
36225 ** the write succeeds */
36226 0;
36227 }
36228 }
36229 /* Last chance verification. If the sector size isn't a multiple of 512
36230 ** then it isn't valid.*/
36231 if( pFile->sectorSize % 512 != 0 ){
36232 pFile->deviceCharacteristics = 0;
36233 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
36234 }
36235}
36236#endif
36237
36238/*
36239** Return the sector size in bytes of the underlying block device for
36240** the specified file. This is almost always 512 bytes, but may be
36241** larger for some devices.
36242**
36243** SQLite code assumes this function cannot fail. It also assumes that
36244** if two files are created in the same file-system directory (i.e.
36245** a database and its journal file) that the sector size will be the
36246** same for both.
36247*/
36248static int unixSectorSize(sqlite3_file *id){
36249 unixFile *pFd = (unixFile*)id;
36250 setDeviceCharacteristics(pFd);
36251 return pFd->sectorSize;
36252}
36253
36254/*
36255** Return the device characteristics for the file.
36256**
36257** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
36258** However, that choice is controversial since technically the underlying
36259** file system does not always provide powersafe overwrites. (In other
36260** words, after a power-loss event, parts of the file that were never
36261** written might end up being altered.) However, non-PSOW behavior is very,
36262** very rare. And asserting PSOW makes a large reduction in the amount
36263** of required I/O for journaling, since a lot of padding is eliminated.
36264** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
36265** available to turn it off and URI query parameter available to turn it off.
36266*/
36267static int unixDeviceCharacteristics(sqlite3_file *id){
36268 unixFile *pFd = (unixFile*)id;
36269 setDeviceCharacteristics(pFd);
36270 return pFd->deviceCharacteristics;
36271}
36272
36273#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
36274
36275/*
36276** Return the system page size.
36277**
36278** This function should not be called directly by other code in this file.
36279** Instead, it should be called via macro osGetpagesize().
36280*/
36281static int unixGetpagesize(void){
36282#if OS_VXWORKS
36283 return 1024;
36284#elif defined(_BSD_SOURCE)
36285 return getpagesize();
36286#else
36287 return (int)sysconf(_SC_PAGESIZE);
36288#endif
36289}
36290
36291#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
36292
36293#ifndef SQLITE_OMIT_WAL
36294
36295/*
36296** Object used to represent an shared memory buffer.
36297**
36298** When multiple threads all reference the same wal-index, each thread
36299** has its own unixShm object, but they all point to a single instance
36300** of this unixShmNode object. In other words, each wal-index is opened
36301** only once per process.
36302**
36303** Each unixShmNode object is connected to a single unixInodeInfo object.
36304** We could coalesce this object into unixInodeInfo, but that would mean
36305** every open file that does not use shared memory (in other words, most
36306** open files) would have to carry around this extra information. So
36307** the unixInodeInfo object contains a pointer to this unixShmNode object
36308** and the unixShmNode object is created only when needed.
36309**
36310** unixMutexHeld() must be true when creating or destroying
36311** this object or while reading or writing the following fields:
36312**
36313** nRef
36314**
36315** The following fields are read-only after the object is created:
36316**
36317** fid
36318** zFilename
36319**
36320** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
36321** unixMutexHeld() is true when reading or writing any other field
36322** in this structure.
36323*/
36324struct unixShmNode {
36325 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
36326 sqlite3_mutex *mutex; /* Mutex to access this object */
36327 char *zFilename; /* Name of the mmapped file */
36328 int h; /* Open file descriptor */
36329 int szRegion; /* Size of shared-memory regions */
36330 u16 nRegion; /* Size of array apRegion */
36331 u8 isReadonly; /* True if read-only */
36332 u8 isUnlocked; /* True if no DMS lock held */
36333 char **apRegion; /* Array of mapped shared-memory regions */
36334 int nRef; /* Number of unixShm objects pointing to this */
36335 unixShm *pFirst; /* All unixShm objects pointing to this */
36336#ifdef SQLITE_DEBUG
36337 u8 exclMask; /* Mask of exclusive locks held */
36338 u8 sharedMask; /* Mask of shared locks held */
36339 u8 nextShmId; /* Next available unixShm.id value */
36340#endif
36341};
36342
36343/*
36344** Structure used internally by this VFS to record the state of an
36345** open shared memory connection.
36346**
36347** The following fields are initialized when this object is created and
36348** are read-only thereafter:
36349**
36350** unixShm.pFile
36351** unixShm.id
36352**
36353** All other fields are read/write. The unixShm.pFile->mutex must be held
36354** while accessing any read/write fields.
36355*/
36356struct unixShm {
36357 unixShmNode *pShmNode; /* The underlying unixShmNode object */
36358 unixShm *pNext; /* Next unixShm with the same unixShmNode */
36359 u8 hasMutex; /* True if holding the unixShmNode mutex */
36360 u8 id; /* Id of this connection within its unixShmNode */
36361 u16 sharedMask; /* Mask of shared locks held */
36362 u16 exclMask; /* Mask of exclusive locks held */
36363};
36364
36365/*
36366** Constants used for locking
36367*/
36368#define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
36369#define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
36370
36371/*
36372** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
36373**
36374** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
36375** otherwise.
36376*/
36377static int unixShmSystemLock(
36378 unixFile *pFile, /* Open connection to the WAL file */
36379 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
36380 int ofst, /* First byte of the locking range */
36381 int n /* Number of bytes to lock */
36382){
36383 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
36384 struct flock f; /* The posix advisory locking structure */
36385 int rc = SQLITE_OK; /* Result code form fcntl() */
36386
36387 /* Access to the unixShmNode object is serialized by the caller */
36388 pShmNode = pFile->pInode->pShmNode;
36389 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->mutex) );
36390
36391 /* Shared locks never span more than one byte */
36392 assert( n==1 || lockType!=F_RDLCK );
36393
36394 /* Locks are within range */
36395 assert( n>=1 && n<=SQLITE_SHM_NLOCK );
36396
36397 if( pShmNode->h>=0 ){
36398 /* Initialize the locking parameters */
36399 f.l_type = lockType;
36400 f.l_whence = SEEK_SET;
36401 f.l_start = ofst;
36402 f.l_len = n;
36403 rc = osSetPosixAdvisoryLock(pShmNode->h, &f, pFile);
36404 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
36405 }
36406
36407 /* Update the global lock state and do debug tracing */
36408#ifdef SQLITE_DEBUG
36409 { u16 mask;
36410 OSTRACE(("SHM-LOCK "));
36411 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
36412 if( rc==SQLITE_OK ){
36413 if( lockType==F_UNLCK ){
36414 OSTRACE(("unlock %d ok", ofst));
36415 pShmNode->exclMask &= ~mask;
36416 pShmNode->sharedMask &= ~mask;
36417 }else if( lockType==F_RDLCK ){
36418 OSTRACE(("read-lock %d ok", ofst));
36419 pShmNode->exclMask &= ~mask;
36420 pShmNode->sharedMask |= mask;
36421 }else{
36422 assert( lockType==F_WRLCK );
36423 OSTRACE(("write-lock %d ok", ofst));
36424 pShmNode->exclMask |= mask;
36425 pShmNode->sharedMask &= ~mask;
36426 }
36427 }else{
36428 if( lockType==F_UNLCK ){
36429 OSTRACE(("unlock %d failed", ofst));
36430 }else if( lockType==F_RDLCK ){
36431 OSTRACE(("read-lock failed"));
36432 }else{
36433 assert( lockType==F_WRLCK );
36434 OSTRACE(("write-lock %d failed", ofst));
36435 }
36436 }
36437 OSTRACE((" - afterwards %03x,%03x\n",
36438 pShmNode->sharedMask, pShmNode->exclMask));
36439 }
36440#endif
36441
36442 return rc;
36443}
36444
36445/*
36446** Return the minimum number of 32KB shm regions that should be mapped at
36447** a time, assuming that each mapping must be an integer multiple of the
36448** current system page-size.
36449**
36450** Usually, this is 1. The exception seems to be systems that are configured
36451** to use 64KB pages - in this case each mapping must cover at least two
36452** shm regions.
36453*/
36454static int unixShmRegionPerMap(void){
36455 int shmsz = 32*1024; /* SHM region size */
36456 int pgsz = osGetpagesize(); /* System page size */
36457 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
36458 if( pgsz<shmsz ) return 1;
36459 return pgsz/shmsz;
36460}
36461
36462/*
36463** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
36464**
36465** This is not a VFS shared-memory method; it is a utility function called
36466** by VFS shared-memory methods.
36467*/
36468static void unixShmPurge(unixFile *pFd){
36469 unixShmNode *p = pFd->pInode->pShmNode;
36470 assert( unixMutexHeld() );
36471 if( p && ALWAYS(p->nRef==0) ){
36472 int nShmPerMap = unixShmRegionPerMap();
36473 int i;
36474 assert( p->pInode==pFd->pInode );
36475 sqlite3_mutex_free(p->mutex);
36476 for(i=0; i<p->nRegion; i+=nShmPerMap){
36477 if( p->h>=0 ){
36478 osMunmap(p->apRegion[i], p->szRegion);
36479 }else{
36480 sqlite3_free(p->apRegion[i]);
36481 }
36482 }
36483 sqlite3_free(p->apRegion);
36484 if( p->h>=0 ){
36485 robust_close(pFd, p->h, __LINE__);
36486 p->h = -1;
36487 }
36488 p->pInode->pShmNode = 0;
36489 sqlite3_free(p);
36490 }
36491}
36492
36493/*
36494** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
36495** take it now. Return SQLITE_OK if successful, or an SQLite error
36496** code otherwise.
36497**
36498** If the DMS cannot be locked because this is a readonly_shm=1
36499** connection and no other process already holds a lock, return
36500** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
36501*/
36502static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
36503 struct flock lock;
36504 int rc = SQLITE_OK;
36505
36506 /* Use F_GETLK to determine the locks other processes are holding
36507 ** on the DMS byte. If it indicates that another process is holding
36508 ** a SHARED lock, then this process may also take a SHARED lock
36509 ** and proceed with opening the *-shm file.
36510 **
36511 ** Or, if no other process is holding any lock, then this process
36512 ** is the first to open it. In this case take an EXCLUSIVE lock on the
36513 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
36514 ** downgrade to a SHARED lock on the DMS byte.
36515 **
36516 ** If another process is holding an EXCLUSIVE lock on the DMS byte,
36517 ** return SQLITE_BUSY to the caller (it will try again). An earlier
36518 ** version of this code attempted the SHARED lock at this point. But
36519 ** this introduced a subtle race condition: if the process holding
36520 ** EXCLUSIVE failed just before truncating the *-shm file, then this
36521 ** process might open and use the *-shm file without truncating it.
36522 ** And if the *-shm file has been corrupted by a power failure or
36523 ** system crash, the database itself may also become corrupt. */
36524 lock.l_whence = SEEK_SET;
36525 lock.l_start = UNIX_SHM_DMS;
36526 lock.l_len = 1;
36527 lock.l_type = F_WRLCK;
36528 if( osFcntl(pShmNode->h, F_GETLK, &lock)!=0 ) {
36529 rc = SQLITE_IOERR_LOCK;
36530 }else if( lock.l_type==F_UNLCK ){
36531 if( pShmNode->isReadonly ){
36532 pShmNode->isUnlocked = 1;
36533 rc = SQLITE_READONLY_CANTINIT;
36534 }else{
36535 rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1);
36536 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->h, 0) ){
36537 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
36538 }
36539 }
36540 }else if( lock.l_type==F_WRLCK ){
36541 rc = SQLITE_BUSY;
36542 }
36543
36544 if( rc==SQLITE_OK ){
36545 assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK );
36546 rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
36547 }
36548 return rc;
36549}
36550
36551/*
36552** Open a shared-memory area associated with open database file pDbFd.
36553** This particular implementation uses mmapped files.
36554**
36555** The file used to implement shared-memory is in the same directory
36556** as the open database file and has the same name as the open database
36557** file with the "-shm" suffix added. For example, if the database file
36558** is "/home/user1/config.db" then the file that is created and mmapped
36559** for shared memory will be called "/home/user1/config.db-shm".
36560**
36561** Another approach to is to use files in /dev/shm or /dev/tmp or an
36562** some other tmpfs mount. But if a file in a different directory
36563** from the database file is used, then differing access permissions
36564** or a chroot() might cause two different processes on the same
36565** database to end up using different files for shared memory -
36566** meaning that their memory would not really be shared - resulting
36567** in database corruption. Nevertheless, this tmpfs file usage
36568** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
36569** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
36570** option results in an incompatible build of SQLite; builds of SQLite
36571** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
36572** same database file at the same time, database corruption will likely
36573** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
36574** "unsupported" and may go away in a future SQLite release.
36575**
36576** When opening a new shared-memory file, if no other instances of that
36577** file are currently open, in this process or in other processes, then
36578** the file must be truncated to zero length or have its header cleared.
36579**
36580** If the original database file (pDbFd) is using the "unix-excl" VFS
36581** that means that an exclusive lock is held on the database file and
36582** that no other processes are able to read or write the database. In
36583** that case, we do not really need shared memory. No shared memory
36584** file is created. The shared memory will be simulated with heap memory.
36585*/
36586static int unixOpenSharedMemory(unixFile *pDbFd){
36587 struct unixShm *p = 0; /* The connection to be opened */
36588 struct unixShmNode *pShmNode; /* The underlying mmapped file */
36589 int rc = SQLITE_OK; /* Result code */
36590 unixInodeInfo *pInode; /* The inode of fd */
36591 char *zShm; /* Name of the file used for SHM */
36592 int nShmFilename; /* Size of the SHM filename in bytes */
36593
36594 /* Allocate space for the new unixShm object. */
36595 p = sqlite3_malloc64( sizeof(*p) );
36596 if( p==0 ) return SQLITE_NOMEM_BKPT;
36597 memset(p, 0, sizeof(*p));
36598 assert( pDbFd->pShm==0 );
36599
36600 /* Check to see if a unixShmNode object already exists. Reuse an existing
36601 ** one if present. Create a new one if necessary.
36602 */
36603 assert( unixFileMutexNotheld(pDbFd) );
36604 unixEnterMutex();
36605 pInode = pDbFd->pInode;
36606 pShmNode = pInode->pShmNode;
36607 if( pShmNode==0 ){
36608 struct stat sStat; /* fstat() info for database file */
36609#ifndef SQLITE_SHM_DIRECTORY
36610 const char *zBasePath = pDbFd->zPath;
36611#endif
36612
36613 /* Call fstat() to figure out the permissions on the database file. If
36614 ** a new *-shm file is created, an attempt will be made to create it
36615 ** with the same permissions.
36616 */
36617 if( osFstat(pDbFd->h, &sStat) ){
36618 rc = SQLITE_IOERR_FSTAT;
36619 goto shm_open_err;
36620 }
36621
36622#ifdef SQLITE_SHM_DIRECTORY
36623 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
36624#else
36625 nShmFilename = 6 + (int)strlen(zBasePath);
36626#endif
36627 pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
36628 if( pShmNode==0 ){
36629 rc = SQLITE_NOMEM_BKPT;
36630 goto shm_open_err;
36631 }
36632 memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
36633 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
36634#ifdef SQLITE_SHM_DIRECTORY
36635 sqlite3_snprintf(nShmFilename, zShm,
36636 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
36637 (u32)sStat.st_ino, (u32)sStat.st_dev);
36638#else
36639 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
36640 sqlite3FileSuffix3(pDbFd->zPath, zShm);
36641#endif
36642 pShmNode->h = -1;
36643 pDbFd->pInode->pShmNode = pShmNode;
36644 pShmNode->pInode = pDbFd->pInode;
36645 if( sqlite3GlobalConfig.bCoreMutex ){
36646 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
36647 if( pShmNode->mutex==0 ){
36648 rc = SQLITE_NOMEM_BKPT;
36649 goto shm_open_err;
36650 }
36651 }
36652
36653 if( pInode->bProcessLock==0 ){
36654 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
36655 pShmNode->h = robust_open(zShm, O_RDWR|O_CREAT, (sStat.st_mode&0777));
36656 }
36657 if( pShmNode->h<0 ){
36658 pShmNode->h = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777));
36659 if( pShmNode->h<0 ){
36660 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm);
36661 goto shm_open_err;
36662 }
36663 pShmNode->isReadonly = 1;
36664 }
36665
36666 /* If this process is running as root, make sure that the SHM file
36667 ** is owned by the same user that owns the original database. Otherwise,
36668 ** the original owner will not be able to connect.
36669 */
36670 robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
36671
36672 rc = unixLockSharedMemory(pDbFd, pShmNode);
36673 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
36674 }
36675 }
36676
36677 /* Make the new connection a child of the unixShmNode */
36678 p->pShmNode = pShmNode;
36679#ifdef SQLITE_DEBUG
36680 p->id = pShmNode->nextShmId++;
36681#endif
36682 pShmNode->nRef++;
36683 pDbFd->pShm = p;
36684 unixLeaveMutex();
36685
36686 /* The reference count on pShmNode has already been incremented under
36687 ** the cover of the unixEnterMutex() mutex and the pointer from the
36688 ** new (struct unixShm) object to the pShmNode has been set. All that is
36689 ** left to do is to link the new object into the linked list starting
36690 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
36691 ** mutex.
36692 */
36693 sqlite3_mutex_enter(pShmNode->mutex);
36694 p->pNext = pShmNode->pFirst;
36695 pShmNode->pFirst = p;
36696 sqlite3_mutex_leave(pShmNode->mutex);
36697 return rc;
36698
36699 /* Jump here on any error */
36700shm_open_err:
36701 unixShmPurge(pDbFd); /* This call frees pShmNode if required */
36702 sqlite3_free(p);
36703 unixLeaveMutex();
36704 return rc;
36705}
36706
36707/*
36708** This function is called to obtain a pointer to region iRegion of the
36709** shared-memory associated with the database file fd. Shared-memory regions
36710** are numbered starting from zero. Each shared-memory region is szRegion
36711** bytes in size.
36712**
36713** If an error occurs, an error code is returned and *pp is set to NULL.
36714**
36715** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
36716** region has not been allocated (by any client, including one running in a
36717** separate process), then *pp is set to NULL and SQLITE_OK returned. If
36718** bExtend is non-zero and the requested shared-memory region has not yet
36719** been allocated, it is allocated by this function.
36720**
36721** If the shared-memory region has already been allocated or is allocated by
36722** this call as described above, then it is mapped into this processes
36723** address space (if it is not already), *pp is set to point to the mapped
36724** memory and SQLITE_OK returned.
36725*/
36726static int unixShmMap(
36727 sqlite3_file *fd, /* Handle open on database file */
36728 int iRegion, /* Region to retrieve */
36729 int szRegion, /* Size of regions */
36730 int bExtend, /* True to extend file if necessary */
36731 void volatile **pp /* OUT: Mapped memory */
36732){
36733 unixFile *pDbFd = (unixFile*)fd;
36734 unixShm *p;
36735 unixShmNode *pShmNode;
36736 int rc = SQLITE_OK;
36737 int nShmPerMap = unixShmRegionPerMap();
36738 int nReqRegion;
36739
36740 /* If the shared-memory file has not yet been opened, open it now. */
36741 if( pDbFd->pShm==0 ){
36742 rc = unixOpenSharedMemory(pDbFd);
36743 if( rc!=SQLITE_OK ) return rc;
36744 }
36745
36746 p = pDbFd->pShm;
36747 pShmNode = p->pShmNode;
36748 sqlite3_mutex_enter(pShmNode->mutex);
36749 if( pShmNode->isUnlocked ){
36750 rc = unixLockSharedMemory(pDbFd, pShmNode);
36751 if( rc!=SQLITE_OK ) goto shmpage_out;
36752 pShmNode->isUnlocked = 0;
36753 }
36754 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
36755 assert( pShmNode->pInode==pDbFd->pInode );
36756 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
36757 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
36758
36759 /* Minimum number of regions required to be mapped. */
36760 nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
36761
36762 if( pShmNode->nRegion<nReqRegion ){
36763 char **apNew; /* New apRegion[] array */
36764 int nByte = nReqRegion*szRegion; /* Minimum required file size */
36765 struct stat sStat; /* Used by fstat() */
36766
36767 pShmNode->szRegion = szRegion;
36768
36769 if( pShmNode->h>=0 ){
36770 /* The requested region is not mapped into this processes address space.
36771 ** Check to see if it has been allocated (i.e. if the wal-index file is
36772 ** large enough to contain the requested region).
36773 */
36774 if( osFstat(pShmNode->h, &sStat) ){
36775 rc = SQLITE_IOERR_SHMSIZE;
36776 goto shmpage_out;
36777 }
36778
36779 if( sStat.st_size<nByte ){
36780 /* The requested memory region does not exist. If bExtend is set to
36781 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
36782 */
36783 if( !bExtend ){
36784 goto shmpage_out;
36785 }
36786
36787 /* Alternatively, if bExtend is true, extend the file. Do this by
36788 ** writing a single byte to the end of each (OS) page being
36789 ** allocated or extended. Technically, we need only write to the
36790 ** last page in order to extend the file. But writing to all new
36791 ** pages forces the OS to allocate them immediately, which reduces
36792 ** the chances of SIGBUS while accessing the mapped region later on.
36793 */
36794 else{
36795 static const int pgsz = 4096;
36796 int iPg;
36797
36798 /* Write to the last byte of each newly allocated or extended page */
36799 assert( (nByte % pgsz)==0 );
36800 for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
36801 int x = 0;
36802 if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){
36803 const char *zFile = pShmNode->zFilename;
36804 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
36805 goto shmpage_out;
36806 }
36807 }
36808 }
36809 }
36810 }
36811
36812 /* Map the requested memory region into this processes address space. */
36813 apNew = (char **)sqlite3_realloc(
36814 pShmNode->apRegion, nReqRegion*sizeof(char *)
36815 );
36816 if( !apNew ){
36817 rc = SQLITE_IOERR_NOMEM_BKPT;
36818 goto shmpage_out;
36819 }
36820 pShmNode->apRegion = apNew;
36821 while( pShmNode->nRegion<nReqRegion ){
36822 int nMap = szRegion*nShmPerMap;
36823 int i;
36824 void *pMem;
36825 if( pShmNode->h>=0 ){
36826 pMem = osMmap(0, nMap,
36827 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
36828 MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
36829 );
36830 if( pMem==MAP_FAILED ){
36831 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
36832 goto shmpage_out;
36833 }
36834 }else{
36835 pMem = sqlite3_malloc64(szRegion);
36836 if( pMem==0 ){
36837 rc = SQLITE_NOMEM_BKPT;
36838 goto shmpage_out;
36839 }
36840 memset(pMem, 0, szRegion);
36841 }
36842
36843 for(i=0; i<nShmPerMap; i++){
36844 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
36845 }
36846 pShmNode->nRegion += nShmPerMap;
36847 }
36848 }
36849
36850shmpage_out:
36851 if( pShmNode->nRegion>iRegion ){
36852 *pp = pShmNode->apRegion[iRegion];
36853 }else{
36854 *pp = 0;
36855 }
36856 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
36857 sqlite3_mutex_leave(pShmNode->mutex);
36858 return rc;
36859}
36860
36861/*
36862** Change the lock state for a shared-memory segment.
36863**
36864** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
36865** different here than in posix. In xShmLock(), one can go from unlocked
36866** to shared and back or from unlocked to exclusive and back. But one may
36867** not go from shared to exclusive or from exclusive to shared.
36868*/
36869static int unixShmLock(
36870 sqlite3_file *fd, /* Database file holding the shared memory */
36871 int ofst, /* First lock to acquire or release */
36872 int n, /* Number of locks to acquire or release */
36873 int flags /* What to do with the lock */
36874){
36875 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
36876 unixShm *p = pDbFd->pShm; /* The shared memory being locked */
36877 unixShm *pX; /* For looping over all siblings */
36878 unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
36879 int rc = SQLITE_OK; /* Result code */
36880 u16 mask; /* Mask of locks to take or release */
36881
36882 assert( pShmNode==pDbFd->pInode->pShmNode );
36883 assert( pShmNode->pInode==pDbFd->pInode );
36884 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
36885 assert( n>=1 );
36886 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
36887 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
36888 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
36889 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
36890 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
36891 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
36892 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
36893
36894 mask = (1<<(ofst+n)) - (1<<ofst);
36895 assert( n>1 || mask==(1<<ofst) );
36896 sqlite3_mutex_enter(pShmNode->mutex);
36897 if( flags & SQLITE_SHM_UNLOCK ){
36898 u16 allMask = 0; /* Mask of locks held by siblings */
36899
36900 /* See if any siblings hold this same lock */
36901 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
36902 if( pX==p ) continue;
36903 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
36904 allMask |= pX->sharedMask;
36905 }
36906
36907 /* Unlock the system-level locks */
36908 if( (mask & allMask)==0 ){
36909 rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n);
36910 }else{
36911 rc = SQLITE_OK;
36912 }
36913
36914 /* Undo the local locks */
36915 if( rc==SQLITE_OK ){
36916 p->exclMask &= ~mask;
36917 p->sharedMask &= ~mask;
36918 }
36919 }else if( flags & SQLITE_SHM_SHARED ){
36920 u16 allShared = 0; /* Union of locks held by connections other than "p" */
36921
36922 /* Find out which shared locks are already held by sibling connections.
36923 ** If any sibling already holds an exclusive lock, go ahead and return
36924 ** SQLITE_BUSY.
36925 */
36926 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
36927 if( (pX->exclMask & mask)!=0 ){
36928 rc = SQLITE_BUSY;
36929 break;
36930 }
36931 allShared |= pX->sharedMask;
36932 }
36933
36934 /* Get shared locks at the system level, if necessary */
36935 if( rc==SQLITE_OK ){
36936 if( (allShared & mask)==0 ){
36937 rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n);
36938 }else{
36939 rc = SQLITE_OK;
36940 }
36941 }
36942
36943 /* Get the local shared locks */
36944 if( rc==SQLITE_OK ){
36945 p->sharedMask |= mask;
36946 }
36947 }else{
36948 /* Make sure no sibling connections hold locks that will block this
36949 ** lock. If any do, return SQLITE_BUSY right away.
36950 */
36951 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
36952 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
36953 rc = SQLITE_BUSY;
36954 break;
36955 }
36956 }
36957
36958 /* Get the exclusive locks at the system level. Then if successful
36959 ** also mark the local connection as being locked.
36960 */
36961 if( rc==SQLITE_OK ){
36962 rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
36963 if( rc==SQLITE_OK ){
36964 assert( (p->sharedMask & mask)==0 );
36965 p->exclMask |= mask;
36966 }
36967 }
36968 }
36969 sqlite3_mutex_leave(pShmNode->mutex);
36970 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
36971 p->id, osGetpid(0), p->sharedMask, p->exclMask));
36972 return rc;
36973}
36974
36975/*
36976** Implement a memory barrier or memory fence on shared memory.
36977**
36978** All loads and stores begun before the barrier must complete before
36979** any load or store begun after the barrier.
36980*/
36981static void unixShmBarrier(
36982 sqlite3_file *fd /* Database file holding the shared memory */
36983){
36984 UNUSED_PARAMETER(fd);
36985 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
36986 assert( fd->pMethods->xLock==nolockLock
36987 || unixFileMutexNotheld((unixFile*)fd)
36988 );
36989 unixEnterMutex(); /* Also mutex, for redundancy */
36990 unixLeaveMutex();
36991}
36992
36993/*
36994** Close a connection to shared-memory. Delete the underlying
36995** storage if deleteFlag is true.
36996**
36997** If there is no shared memory associated with the connection then this
36998** routine is a harmless no-op.
36999*/
37000static int unixShmUnmap(
37001 sqlite3_file *fd, /* The underlying database file */
37002 int deleteFlag /* Delete shared-memory if true */
37003){
37004 unixShm *p; /* The connection to be closed */
37005 unixShmNode *pShmNode; /* The underlying shared-memory file */
37006 unixShm **pp; /* For looping over sibling connections */
37007 unixFile *pDbFd; /* The underlying database file */
37008
37009 pDbFd = (unixFile*)fd;
37010 p = pDbFd->pShm;
37011 if( p==0 ) return SQLITE_OK;
37012 pShmNode = p->pShmNode;
37013
37014 assert( pShmNode==pDbFd->pInode->pShmNode );
37015 assert( pShmNode->pInode==pDbFd->pInode );
37016
37017 /* Remove connection p from the set of connections associated
37018 ** with pShmNode */
37019 sqlite3_mutex_enter(pShmNode->mutex);
37020 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
37021 *pp = p->pNext;
37022
37023 /* Free the connection p */
37024 sqlite3_free(p);
37025 pDbFd->pShm = 0;
37026 sqlite3_mutex_leave(pShmNode->mutex);
37027
37028 /* If pShmNode->nRef has reached 0, then close the underlying
37029 ** shared-memory file, too */
37030 assert( unixFileMutexNotheld(pDbFd) );
37031 unixEnterMutex();
37032 assert( pShmNode->nRef>0 );
37033 pShmNode->nRef--;
37034 if( pShmNode->nRef==0 ){
37035 if( deleteFlag && pShmNode->h>=0 ){
37036 osUnlink(pShmNode->zFilename);
37037 }
37038 unixShmPurge(pDbFd);
37039 }
37040 unixLeaveMutex();
37041
37042 return SQLITE_OK;
37043}
37044
37045
37046#else
37047# define unixShmMap 0
37048# define unixShmLock 0
37049# define unixShmBarrier 0
37050# define unixShmUnmap 0
37051#endif /* #ifndef SQLITE_OMIT_WAL */
37052
37053#if SQLITE_MAX_MMAP_SIZE>0
37054/*
37055** If it is currently memory mapped, unmap file pFd.
37056*/
37057static void unixUnmapfile(unixFile *pFd){
37058 assert( pFd->nFetchOut==0 );
37059 if( pFd->pMapRegion ){
37060 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
37061 pFd->pMapRegion = 0;
37062 pFd->mmapSize = 0;
37063 pFd->mmapSizeActual = 0;
37064 }
37065}
37066
37067/*
37068** Attempt to set the size of the memory mapping maintained by file
37069** descriptor pFd to nNew bytes. Any existing mapping is discarded.
37070**
37071** If successful, this function sets the following variables:
37072**
37073** unixFile.pMapRegion
37074** unixFile.mmapSize
37075** unixFile.mmapSizeActual
37076**
37077** If unsuccessful, an error message is logged via sqlite3_log() and
37078** the three variables above are zeroed. In this case SQLite should
37079** continue accessing the database using the xRead() and xWrite()
37080** methods.
37081*/
37082static void unixRemapfile(
37083 unixFile *pFd, /* File descriptor object */
37084 i64 nNew /* Required mapping size */
37085){
37086 const char *zErr = "mmap";
37087 int h = pFd->h; /* File descriptor open on db file */
37088 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
37089 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
37090 u8 *pNew = 0; /* Location of new mapping */
37091 int flags = PROT_READ; /* Flags to pass to mmap() */
37092
37093 assert( pFd->nFetchOut==0 );
37094 assert( nNew>pFd->mmapSize );
37095 assert( nNew<=pFd->mmapSizeMax );
37096 assert( nNew>0 );
37097 assert( pFd->mmapSizeActual>=pFd->mmapSize );
37098 assert( MAP_FAILED!=0 );
37099
37100#ifdef SQLITE_MMAP_READWRITE
37101 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
37102#endif
37103
37104 if( pOrig ){
37105#if HAVE_MREMAP
37106 i64 nReuse = pFd->mmapSize;
37107#else
37108 const int szSyspage = osGetpagesize();
37109 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
37110#endif
37111 u8 *pReq = &pOrig[nReuse];
37112
37113 /* Unmap any pages of the existing mapping that cannot be reused. */
37114 if( nReuse!=nOrig ){
37115 osMunmap(pReq, nOrig-nReuse);
37116 }
37117
37118#if HAVE_MREMAP
37119 pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
37120 zErr = "mremap";
37121#else
37122 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
37123 if( pNew!=MAP_FAILED ){
37124 if( pNew!=pReq ){
37125 osMunmap(pNew, nNew - nReuse);
37126 pNew = 0;
37127 }else{
37128 pNew = pOrig;
37129 }
37130 }
37131#endif
37132
37133 /* The attempt to extend the existing mapping failed. Free it. */
37134 if( pNew==MAP_FAILED || pNew==0 ){
37135 osMunmap(pOrig, nReuse);
37136 }
37137 }
37138
37139 /* If pNew is still NULL, try to create an entirely new mapping. */
37140 if( pNew==0 ){
37141 pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
37142 }
37143
37144 if( pNew==MAP_FAILED ){
37145 pNew = 0;
37146 nNew = 0;
37147 unixLogError(SQLITE_OK, zErr, pFd->zPath);
37148
37149 /* If the mmap() above failed, assume that all subsequent mmap() calls
37150 ** will probably fail too. Fall back to using xRead/xWrite exclusively
37151 ** in this case. */
37152 pFd->mmapSizeMax = 0;
37153 }
37154 pFd->pMapRegion = (void *)pNew;
37155 pFd->mmapSize = pFd->mmapSizeActual = nNew;
37156}
37157
37158/*
37159** Memory map or remap the file opened by file-descriptor pFd (if the file
37160** is already mapped, the existing mapping is replaced by the new). Or, if
37161** there already exists a mapping for this file, and there are still
37162** outstanding xFetch() references to it, this function is a no-op.
37163**
37164** If parameter nByte is non-negative, then it is the requested size of
37165** the mapping to create. Otherwise, if nByte is less than zero, then the
37166** requested size is the size of the file on disk. The actual size of the
37167** created mapping is either the requested size or the value configured
37168** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
37169**
37170** SQLITE_OK is returned if no error occurs (even if the mapping is not
37171** recreated as a result of outstanding references) or an SQLite error
37172** code otherwise.
37173*/
37174static int unixMapfile(unixFile *pFd, i64 nMap){
37175 assert( nMap>=0 || pFd->nFetchOut==0 );
37176 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
37177 if( pFd->nFetchOut>0 ) return SQLITE_OK;
37178
37179 if( nMap<0 ){
37180 struct stat statbuf; /* Low-level file information */
37181 if( osFstat(pFd->h, &statbuf) ){
37182 return SQLITE_IOERR_FSTAT;
37183 }
37184 nMap = statbuf.st_size;
37185 }
37186 if( nMap>pFd->mmapSizeMax ){
37187 nMap = pFd->mmapSizeMax;
37188 }
37189
37190 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
37191 if( nMap!=pFd->mmapSize ){
37192 unixRemapfile(pFd, nMap);
37193 }
37194
37195 return SQLITE_OK;
37196}
37197#endif /* SQLITE_MAX_MMAP_SIZE>0 */
37198
37199/*
37200** If possible, return a pointer to a mapping of file fd starting at offset
37201** iOff. The mapping must be valid for at least nAmt bytes.
37202**
37203** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
37204** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
37205** Finally, if an error does occur, return an SQLite error code. The final
37206** value of *pp is undefined in this case.
37207**
37208** If this function does return a pointer, the caller must eventually
37209** release the reference by calling unixUnfetch().
37210*/
37211static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
37212#if SQLITE_MAX_MMAP_SIZE>0
37213 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
37214#endif
37215 *pp = 0;
37216
37217#if SQLITE_MAX_MMAP_SIZE>0
37218 if( pFd->mmapSizeMax>0 ){
37219 if( pFd->pMapRegion==0 ){
37220 int rc = unixMapfile(pFd, -1);
37221 if( rc!=SQLITE_OK ) return rc;
37222 }
37223 if( pFd->mmapSize >= iOff+nAmt ){
37224 *pp = &((u8 *)pFd->pMapRegion)[iOff];
37225 pFd->nFetchOut++;
37226 }
37227 }
37228#endif
37229 return SQLITE_OK;
37230}
37231
37232/*
37233** If the third argument is non-NULL, then this function releases a
37234** reference obtained by an earlier call to unixFetch(). The second
37235** argument passed to this function must be the same as the corresponding
37236** argument that was passed to the unixFetch() invocation.
37237**
37238** Or, if the third argument is NULL, then this function is being called
37239** to inform the VFS layer that, according to POSIX, any existing mapping
37240** may now be invalid and should be unmapped.
37241*/
37242static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
37243#if SQLITE_MAX_MMAP_SIZE>0
37244 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
37245 UNUSED_PARAMETER(iOff);
37246
37247 /* If p==0 (unmap the entire file) then there must be no outstanding
37248 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
37249 ** then there must be at least one outstanding. */
37250 assert( (p==0)==(pFd->nFetchOut==0) );
37251
37252 /* If p!=0, it must match the iOff value. */
37253 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
37254
37255 if( p ){
37256 pFd->nFetchOut--;
37257 }else{
37258 unixUnmapfile(pFd);
37259 }
37260
37261 assert( pFd->nFetchOut>=0 );
37262#else
37263 UNUSED_PARAMETER(fd);
37264 UNUSED_PARAMETER(p);
37265 UNUSED_PARAMETER(iOff);
37266#endif
37267 return SQLITE_OK;
37268}
37269
37270/*
37271** Here ends the implementation of all sqlite3_file methods.
37272**
37273********************** End sqlite3_file Methods *******************************
37274******************************************************************************/
37275
37276/*
37277** This division contains definitions of sqlite3_io_methods objects that
37278** implement various file locking strategies. It also contains definitions
37279** of "finder" functions. A finder-function is used to locate the appropriate
37280** sqlite3_io_methods object for a particular database file. The pAppData
37281** field of the sqlite3_vfs VFS objects are initialized to be pointers to
37282** the correct finder-function for that VFS.
37283**
37284** Most finder functions return a pointer to a fixed sqlite3_io_methods
37285** object. The only interesting finder-function is autolockIoFinder, which
37286** looks at the filesystem type and tries to guess the best locking
37287** strategy from that.
37288**
37289** For finder-function F, two objects are created:
37290**
37291** (1) The real finder-function named "FImpt()".
37292**
37293** (2) A constant pointer to this function named just "F".
37294**
37295**
37296** A pointer to the F pointer is used as the pAppData value for VFS
37297** objects. We have to do this instead of letting pAppData point
37298** directly at the finder-function since C90 rules prevent a void*
37299** from be cast into a function pointer.
37300**
37301**
37302** Each instance of this macro generates two objects:
37303**
37304** * A constant sqlite3_io_methods object call METHOD that has locking
37305** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
37306**
37307** * An I/O method finder function called FINDER that returns a pointer
37308** to the METHOD object in the previous bullet.
37309*/
37310#define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \
37311static const sqlite3_io_methods METHOD = { \
37312 VERSION, /* iVersion */ \
37313 CLOSE, /* xClose */ \
37314 unixRead, /* xRead */ \
37315 unixWrite, /* xWrite */ \
37316 unixTruncate, /* xTruncate */ \
37317 unixSync, /* xSync */ \
37318 unixFileSize, /* xFileSize */ \
37319 LOCK, /* xLock */ \
37320 UNLOCK, /* xUnlock */ \
37321 CKLOCK, /* xCheckReservedLock */ \
37322 unixFileControl, /* xFileControl */ \
37323 unixSectorSize, /* xSectorSize */ \
37324 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
37325 SHMMAP, /* xShmMap */ \
37326 unixShmLock, /* xShmLock */ \
37327 unixShmBarrier, /* xShmBarrier */ \
37328 unixShmUnmap, /* xShmUnmap */ \
37329 unixFetch, /* xFetch */ \
37330 unixUnfetch, /* xUnfetch */ \
37331}; \
37332static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
37333 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
37334 return &METHOD; \
37335} \
37336static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
37337 = FINDER##Impl;
37338
37339/*
37340** Here are all of the sqlite3_io_methods objects for each of the
37341** locking strategies. Functions that return pointers to these methods
37342** are also created.
37343*/
37344IOMETHODS(
37345 posixIoFinder, /* Finder function name */
37346 posixIoMethods, /* sqlite3_io_methods object name */
37347 3, /* shared memory and mmap are enabled */
37348 unixClose, /* xClose method */
37349 unixLock, /* xLock method */
37350 unixUnlock, /* xUnlock method */
37351 unixCheckReservedLock, /* xCheckReservedLock method */
37352 unixShmMap /* xShmMap method */
37353)
37354IOMETHODS(
37355 nolockIoFinder, /* Finder function name */
37356 nolockIoMethods, /* sqlite3_io_methods object name */
37357 3, /* shared memory and mmap are enabled */
37358 nolockClose, /* xClose method */
37359 nolockLock, /* xLock method */
37360 nolockUnlock, /* xUnlock method */
37361 nolockCheckReservedLock, /* xCheckReservedLock method */
37362 0 /* xShmMap method */
37363)
37364IOMETHODS(
37365 dotlockIoFinder, /* Finder function name */
37366 dotlockIoMethods, /* sqlite3_io_methods object name */
37367 1, /* shared memory is disabled */
37368 dotlockClose, /* xClose method */
37369 dotlockLock, /* xLock method */
37370 dotlockUnlock, /* xUnlock method */
37371 dotlockCheckReservedLock, /* xCheckReservedLock method */
37372 0 /* xShmMap method */
37373)
37374
37375#if SQLITE_ENABLE_LOCKING_STYLE
37376IOMETHODS(
37377 flockIoFinder, /* Finder function name */
37378 flockIoMethods, /* sqlite3_io_methods object name */
37379 1, /* shared memory is disabled */
37380 flockClose, /* xClose method */
37381 flockLock, /* xLock method */
37382 flockUnlock, /* xUnlock method */
37383 flockCheckReservedLock, /* xCheckReservedLock method */
37384 0 /* xShmMap method */
37385)
37386#endif
37387
37388#if OS_VXWORKS
37389IOMETHODS(
37390 semIoFinder, /* Finder function name */
37391 semIoMethods, /* sqlite3_io_methods object name */
37392 1, /* shared memory is disabled */
37393 semXClose, /* xClose method */
37394 semXLock, /* xLock method */
37395 semXUnlock, /* xUnlock method */
37396 semXCheckReservedLock, /* xCheckReservedLock method */
37397 0 /* xShmMap method */
37398)
37399#endif
37400
37401#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
37402IOMETHODS(
37403 afpIoFinder, /* Finder function name */
37404 afpIoMethods, /* sqlite3_io_methods object name */
37405 1, /* shared memory is disabled */
37406 afpClose, /* xClose method */
37407 afpLock, /* xLock method */
37408 afpUnlock, /* xUnlock method */
37409 afpCheckReservedLock, /* xCheckReservedLock method */
37410 0 /* xShmMap method */
37411)
37412#endif
37413
37414/*
37415** The proxy locking method is a "super-method" in the sense that it
37416** opens secondary file descriptors for the conch and lock files and
37417** it uses proxy, dot-file, AFP, and flock() locking methods on those
37418** secondary files. For this reason, the division that implements
37419** proxy locking is located much further down in the file. But we need
37420** to go ahead and define the sqlite3_io_methods and finder function
37421** for proxy locking here. So we forward declare the I/O methods.
37422*/
37423#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
37424static int proxyClose(sqlite3_file*);
37425static int proxyLock(sqlite3_file*, int);
37426static int proxyUnlock(sqlite3_file*, int);
37427static int proxyCheckReservedLock(sqlite3_file*, int*);
37428IOMETHODS(
37429 proxyIoFinder, /* Finder function name */
37430 proxyIoMethods, /* sqlite3_io_methods object name */
37431 1, /* shared memory is disabled */
37432 proxyClose, /* xClose method */
37433 proxyLock, /* xLock method */
37434 proxyUnlock, /* xUnlock method */
37435 proxyCheckReservedLock, /* xCheckReservedLock method */
37436 0 /* xShmMap method */
37437)
37438#endif
37439
37440/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
37441#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
37442IOMETHODS(
37443 nfsIoFinder, /* Finder function name */
37444 nfsIoMethods, /* sqlite3_io_methods object name */
37445 1, /* shared memory is disabled */
37446 unixClose, /* xClose method */
37447 unixLock, /* xLock method */
37448 nfsUnlock, /* xUnlock method */
37449 unixCheckReservedLock, /* xCheckReservedLock method */
37450 0 /* xShmMap method */
37451)
37452#endif
37453
37454#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
37455/*
37456** This "finder" function attempts to determine the best locking strategy
37457** for the database file "filePath". It then returns the sqlite3_io_methods
37458** object that implements that strategy.
37459**
37460** This is for MacOSX only.
37461*/
37462static const sqlite3_io_methods *autolockIoFinderImpl(
37463 const char *filePath, /* name of the database file */
37464 unixFile *pNew /* open file object for the database file */
37465){
37466 static const struct Mapping {
37467 const char *zFilesystem; /* Filesystem type name */
37468 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
37469 } aMap[] = {
37470 { "hfs", &posixIoMethods },
37471 { "ufs", &posixIoMethods },
37472 { "afpfs", &afpIoMethods },
37473 { "smbfs", &afpIoMethods },
37474 { "webdav", &nolockIoMethods },
37475 { 0, 0 }
37476 };
37477 int i;
37478 struct statfs fsInfo;
37479 struct flock lockInfo;
37480
37481 if( !filePath ){
37482 /* If filePath==NULL that means we are dealing with a transient file
37483 ** that does not need to be locked. */
37484 return &nolockIoMethods;
37485 }
37486 if( statfs(filePath, &fsInfo) != -1 ){
37487 if( fsInfo.f_flags & MNT_RDONLY ){
37488 return &nolockIoMethods;
37489 }
37490 for(i=0; aMap[i].zFilesystem; i++){
37491 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
37492 return aMap[i].pMethods;
37493 }
37494 }
37495 }
37496
37497 /* Default case. Handles, amongst others, "nfs".
37498 ** Test byte-range lock using fcntl(). If the call succeeds,
37499 ** assume that the file-system supports POSIX style locks.
37500 */
37501 lockInfo.l_len = 1;
37502 lockInfo.l_start = 0;
37503 lockInfo.l_whence = SEEK_SET;
37504 lockInfo.l_type = F_RDLCK;
37505 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
37506 if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
37507 return &nfsIoMethods;
37508 } else {
37509 return &posixIoMethods;
37510 }
37511 }else{
37512 return &dotlockIoMethods;
37513 }
37514}
37515static const sqlite3_io_methods
37516 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
37517
37518#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
37519
37520#if OS_VXWORKS
37521/*
37522** This "finder" function for VxWorks checks to see if posix advisory
37523** locking works. If it does, then that is what is used. If it does not
37524** work, then fallback to named semaphore locking.
37525*/
37526static const sqlite3_io_methods *vxworksIoFinderImpl(
37527 const char *filePath, /* name of the database file */
37528 unixFile *pNew /* the open file object */
37529){
37530 struct flock lockInfo;
37531
37532 if( !filePath ){
37533 /* If filePath==NULL that means we are dealing with a transient file
37534 ** that does not need to be locked. */
37535 return &nolockIoMethods;
37536 }
37537
37538 /* Test if fcntl() is supported and use POSIX style locks.
37539 ** Otherwise fall back to the named semaphore method.
37540 */
37541 lockInfo.l_len = 1;
37542 lockInfo.l_start = 0;
37543 lockInfo.l_whence = SEEK_SET;
37544 lockInfo.l_type = F_RDLCK;
37545 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
37546 return &posixIoMethods;
37547 }else{
37548 return &semIoMethods;
37549 }
37550}
37551static const sqlite3_io_methods
37552 *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
37553
37554#endif /* OS_VXWORKS */
37555
37556/*
37557** An abstract type for a pointer to an IO method finder function:
37558*/
37559typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
37560
37561
37562/****************************************************************************
37563**************************** sqlite3_vfs methods ****************************
37564**
37565** This division contains the implementation of methods on the
37566** sqlite3_vfs object.
37567*/
37568
37569/*
37570** Initialize the contents of the unixFile structure pointed to by pId.
37571*/
37572static int fillInUnixFile(
37573 sqlite3_vfs *pVfs, /* Pointer to vfs object */
37574 int h, /* Open file descriptor of file being opened */
37575 sqlite3_file *pId, /* Write to the unixFile structure here */
37576 const char *zFilename, /* Name of the file being opened */
37577 int ctrlFlags /* Zero or more UNIXFILE_* values */
37578){
37579 const sqlite3_io_methods *pLockingStyle;
37580 unixFile *pNew = (unixFile *)pId;
37581 int rc = SQLITE_OK;
37582
37583 assert( pNew->pInode==NULL );
37584
37585 /* No locking occurs in temporary files */
37586 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
37587
37588 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
37589 pNew->h = h;
37590 pNew->pVfs = pVfs;
37591 pNew->zPath = zFilename;
37592 pNew->ctrlFlags = (u8)ctrlFlags;
37593#if SQLITE_MAX_MMAP_SIZE>0
37594 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
37595#endif
37596 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
37597 "psow", SQLITE_POWERSAFE_OVERWRITE) ){
37598 pNew->ctrlFlags |= UNIXFILE_PSOW;
37599 }
37600 if( strcmp(pVfs->zName,"unix-excl")==0 ){
37601 pNew->ctrlFlags |= UNIXFILE_EXCL;
37602 }
37603
37604#if OS_VXWORKS
37605 pNew->pId = vxworksFindFileId(zFilename);
37606 if( pNew->pId==0 ){
37607 ctrlFlags |= UNIXFILE_NOLOCK;
37608 rc = SQLITE_NOMEM_BKPT;
37609 }
37610#endif
37611
37612 if( ctrlFlags & UNIXFILE_NOLOCK ){
37613 pLockingStyle = &nolockIoMethods;
37614 }else{
37615 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
37616#if SQLITE_ENABLE_LOCKING_STYLE
37617 /* Cache zFilename in the locking context (AFP and dotlock override) for
37618 ** proxyLock activation is possible (remote proxy is based on db name)
37619 ** zFilename remains valid until file is closed, to support */
37620 pNew->lockingContext = (void*)zFilename;
37621#endif
37622 }
37623
37624 if( pLockingStyle == &posixIoMethods
37625#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
37626 || pLockingStyle == &nfsIoMethods
37627#endif
37628 ){
37629 unixEnterMutex();
37630 rc = findInodeInfo(pNew, &pNew->pInode);
37631 if( rc!=SQLITE_OK ){
37632 /* If an error occurred in findInodeInfo(), close the file descriptor
37633 ** immediately, before releasing the mutex. findInodeInfo() may fail
37634 ** in two scenarios:
37635 **
37636 ** (a) A call to fstat() failed.
37637 ** (b) A malloc failed.
37638 **
37639 ** Scenario (b) may only occur if the process is holding no other
37640 ** file descriptors open on the same file. If there were other file
37641 ** descriptors on this file, then no malloc would be required by
37642 ** findInodeInfo(). If this is the case, it is quite safe to close
37643 ** handle h - as it is guaranteed that no posix locks will be released
37644 ** by doing so.
37645 **
37646 ** If scenario (a) caused the error then things are not so safe. The
37647 ** implicit assumption here is that if fstat() fails, things are in
37648 ** such bad shape that dropping a lock or two doesn't matter much.
37649 */
37650 robust_close(pNew, h, __LINE__);
37651 h = -1;
37652 }
37653 unixLeaveMutex();
37654 }
37655
37656#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
37657 else if( pLockingStyle == &afpIoMethods ){
37658 /* AFP locking uses the file path so it needs to be included in
37659 ** the afpLockingContext.
37660 */
37661 afpLockingContext *pCtx;
37662 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
37663 if( pCtx==0 ){
37664 rc = SQLITE_NOMEM_BKPT;
37665 }else{
37666 /* NB: zFilename exists and remains valid until the file is closed
37667 ** according to requirement F11141. So we do not need to make a
37668 ** copy of the filename. */
37669 pCtx->dbPath = zFilename;
37670 pCtx->reserved = 0;
37671 srandomdev();
37672 unixEnterMutex();
37673 rc = findInodeInfo(pNew, &pNew->pInode);
37674 if( rc!=SQLITE_OK ){
37675 sqlite3_free(pNew->lockingContext);
37676 robust_close(pNew, h, __LINE__);
37677 h = -1;
37678 }
37679 unixLeaveMutex();
37680 }
37681 }
37682#endif
37683
37684 else if( pLockingStyle == &dotlockIoMethods ){
37685 /* Dotfile locking uses the file path so it needs to be included in
37686 ** the dotlockLockingContext
37687 */
37688 char *zLockFile;
37689 int nFilename;
37690 assert( zFilename!=0 );
37691 nFilename = (int)strlen(zFilename) + 6;
37692 zLockFile = (char *)sqlite3_malloc64(nFilename);
37693 if( zLockFile==0 ){
37694 rc = SQLITE_NOMEM_BKPT;
37695 }else{
37696 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
37697 }
37698 pNew->lockingContext = zLockFile;
37699 }
37700
37701#if OS_VXWORKS
37702 else if( pLockingStyle == &semIoMethods ){
37703 /* Named semaphore locking uses the file path so it needs to be
37704 ** included in the semLockingContext
37705 */
37706 unixEnterMutex();
37707 rc = findInodeInfo(pNew, &pNew->pInode);
37708 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
37709 char *zSemName = pNew->pInode->aSemName;
37710 int n;
37711 sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
37712 pNew->pId->zCanonicalName);
37713 for( n=1; zSemName[n]; n++ )
37714 if( zSemName[n]=='/' ) zSemName[n] = '_';
37715 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
37716 if( pNew->pInode->pSem == SEM_FAILED ){
37717 rc = SQLITE_NOMEM_BKPT;
37718 pNew->pInode->aSemName[0] = '\0';
37719 }
37720 }
37721 unixLeaveMutex();
37722 }
37723#endif
37724
37725 storeLastErrno(pNew, 0);
37726#if OS_VXWORKS
37727 if( rc!=SQLITE_OK ){
37728 if( h>=0 ) robust_close(pNew, h, __LINE__);
37729 h = -1;
37730 osUnlink(zFilename);
37731 pNew->ctrlFlags |= UNIXFILE_DELETE;
37732 }
37733#endif
37734 if( rc!=SQLITE_OK ){
37735 if( h>=0 ) robust_close(pNew, h, __LINE__);
37736 }else{
37737 pNew->pMethod = pLockingStyle;
37738 OpenCounter(+1);
37739 verifyDbFile(pNew);
37740 }
37741 return rc;
37742}
37743
37744/*
37745** Return the name of a directory in which to put temporary files.
37746** If no suitable temporary file directory can be found, return NULL.
37747*/
37748static const char *unixTempFileDir(void){
37749 static const char *azDirs[] = {
37750 0,
37751 0,
37752 "/var/tmp",
37753 "/usr/tmp",
37754 "/tmp",
37755 "."
37756 };
37757 unsigned int i = 0;
37758 struct stat buf;
37759 const char *zDir = sqlite3_temp_directory;
37760
37761 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
37762 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
37763 while(1){
37764 if( zDir!=0
37765 && osStat(zDir, &buf)==0
37766 && S_ISDIR(buf.st_mode)
37767 && osAccess(zDir, 03)==0
37768 ){
37769 return zDir;
37770 }
37771 if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break;
37772 zDir = azDirs[i++];
37773 }
37774 return 0;
37775}
37776
37777/*
37778** Create a temporary file name in zBuf. zBuf must be allocated
37779** by the calling process and must be big enough to hold at least
37780** pVfs->mxPathname bytes.
37781*/
37782static int unixGetTempname(int nBuf, char *zBuf){
37783 const char *zDir;
37784 int iLimit = 0;
37785
37786 /* It's odd to simulate an io-error here, but really this is just
37787 ** using the io-error infrastructure to test that SQLite handles this
37788 ** function failing.
37789 */
37790 zBuf[0] = 0;
37791 SimulateIOError( return SQLITE_IOERR );
37792
37793 zDir = unixTempFileDir();
37794 if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
37795 do{
37796 u64 r;
37797 sqlite3_randomness(sizeof(r), &r);
37798 assert( nBuf>2 );
37799 zBuf[nBuf-2] = 0;
37800 sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
37801 zDir, r, 0);
37802 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
37803 }while( osAccess(zBuf,0)==0 );
37804 return SQLITE_OK;
37805}
37806
37807#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
37808/*
37809** Routine to transform a unixFile into a proxy-locking unixFile.
37810** Implementation in the proxy-lock division, but used by unixOpen()
37811** if SQLITE_PREFER_PROXY_LOCKING is defined.
37812*/
37813static int proxyTransformUnixFile(unixFile*, const char*);
37814#endif
37815
37816/*
37817** Search for an unused file descriptor that was opened on the database
37818** file (not a journal or master-journal file) identified by pathname
37819** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
37820** argument to this function.
37821**
37822** Such a file descriptor may exist if a database connection was closed
37823** but the associated file descriptor could not be closed because some
37824** other file descriptor open on the same file is holding a file-lock.
37825** Refer to comments in the unixClose() function and the lengthy comment
37826** describing "Posix Advisory Locking" at the start of this file for
37827** further details. Also, ticket #4018.
37828**
37829** If a suitable file descriptor is found, then it is returned. If no
37830** such file descriptor is located, -1 is returned.
37831*/
37832static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
37833 UnixUnusedFd *pUnused = 0;
37834
37835 /* Do not search for an unused file descriptor on vxworks. Not because
37836 ** vxworks would not benefit from the change (it might, we're not sure),
37837 ** but because no way to test it is currently available. It is better
37838 ** not to risk breaking vxworks support for the sake of such an obscure
37839 ** feature. */
37840#if !OS_VXWORKS
37841 struct stat sStat; /* Results of stat() call */
37842
37843 unixEnterMutex();
37844
37845 /* A stat() call may fail for various reasons. If this happens, it is
37846 ** almost certain that an open() call on the same path will also fail.
37847 ** For this reason, if an error occurs in the stat() call here, it is
37848 ** ignored and -1 is returned. The caller will try to open a new file
37849 ** descriptor on the same path, fail, and return an error to SQLite.
37850 **
37851 ** Even if a subsequent open() call does succeed, the consequences of
37852 ** not searching for a reusable file descriptor are not dire. */
37853 if( inodeList!=0 && 0==osStat(zPath, &sStat) ){
37854 unixInodeInfo *pInode;
37855
37856 pInode = inodeList;
37857 while( pInode && (pInode->fileId.dev!=sStat.st_dev
37858 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
37859 pInode = pInode->pNext;
37860 }
37861 if( pInode ){
37862 UnixUnusedFd **pp;
37863 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
37864 sqlite3_mutex_enter(pInode->pLockMutex);
37865 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
37866 pUnused = *pp;
37867 if( pUnused ){
37868 *pp = pUnused->pNext;
37869 }
37870 sqlite3_mutex_leave(pInode->pLockMutex);
37871 }
37872 }
37873 unixLeaveMutex();
37874#endif /* if !OS_VXWORKS */
37875 return pUnused;
37876}
37877
37878/*
37879** Find the mode, uid and gid of file zFile.
37880*/
37881static int getFileMode(
37882 const char *zFile, /* File name */
37883 mode_t *pMode, /* OUT: Permissions of zFile */
37884 uid_t *pUid, /* OUT: uid of zFile. */
37885 gid_t *pGid /* OUT: gid of zFile. */
37886){
37887 struct stat sStat; /* Output of stat() on database file */
37888 int rc = SQLITE_OK;
37889 if( 0==osStat(zFile, &sStat) ){
37890 *pMode = sStat.st_mode & 0777;
37891 *pUid = sStat.st_uid;
37892 *pGid = sStat.st_gid;
37893 }else{
37894 rc = SQLITE_IOERR_FSTAT;
37895 }
37896 return rc;
37897}
37898
37899/*
37900** This function is called by unixOpen() to determine the unix permissions
37901** to create new files with. If no error occurs, then SQLITE_OK is returned
37902** and a value suitable for passing as the third argument to open(2) is
37903** written to *pMode. If an IO error occurs, an SQLite error code is
37904** returned and the value of *pMode is not modified.
37905**
37906** In most cases, this routine sets *pMode to 0, which will become
37907** an indication to robust_open() to create the file using
37908** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
37909** But if the file being opened is a WAL or regular journal file, then
37910** this function queries the file-system for the permissions on the
37911** corresponding database file and sets *pMode to this value. Whenever
37912** possible, WAL and journal files are created using the same permissions
37913** as the associated database file.
37914**
37915** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
37916** original filename is unavailable. But 8_3_NAMES is only used for
37917** FAT filesystems and permissions do not matter there, so just use
37918** the default permissions.
37919*/
37920static int findCreateFileMode(
37921 const char *zPath, /* Path of file (possibly) being created */
37922 int flags, /* Flags passed as 4th argument to xOpen() */
37923 mode_t *pMode, /* OUT: Permissions to open file with */
37924 uid_t *pUid, /* OUT: uid to set on the file */
37925 gid_t *pGid /* OUT: gid to set on the file */
37926){
37927 int rc = SQLITE_OK; /* Return Code */
37928 *pMode = 0;
37929 *pUid = 0;
37930 *pGid = 0;
37931 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
37932 char zDb[MAX_PATHNAME+1]; /* Database file path */
37933 int nDb; /* Number of valid bytes in zDb */
37934
37935 /* zPath is a path to a WAL or journal file. The following block derives
37936 ** the path to the associated database file from zPath. This block handles
37937 ** the following naming conventions:
37938 **
37939 ** "<path to db>-journal"
37940 ** "<path to db>-wal"
37941 ** "<path to db>-journalNN"
37942 ** "<path to db>-walNN"
37943 **
37944 ** where NN is a decimal number. The NN naming schemes are
37945 ** used by the test_multiplex.c module.
37946 */
37947 nDb = sqlite3Strlen30(zPath) - 1;
37948 while( zPath[nDb]!='-' ){
37949 /* In normal operation, the journal file name will always contain
37950 ** a '-' character. However in 8+3 filename mode, or if a corrupt
37951 ** rollback journal specifies a master journal with a goofy name, then
37952 ** the '-' might be missing. */
37953 if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
37954 nDb--;
37955 }
37956 memcpy(zDb, zPath, nDb);
37957 zDb[nDb] = '\0';
37958
37959 rc = getFileMode(zDb, pMode, pUid, pGid);
37960 }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
37961 *pMode = 0600;
37962 }else if( flags & SQLITE_OPEN_URI ){
37963 /* If this is a main database file and the file was opened using a URI
37964 ** filename, check for the "modeof" parameter. If present, interpret
37965 ** its value as a filename and try to copy the mode, uid and gid from
37966 ** that file. */
37967 const char *z = sqlite3_uri_parameter(zPath, "modeof");
37968 if( z ){
37969 rc = getFileMode(z, pMode, pUid, pGid);
37970 }
37971 }
37972 return rc;
37973}
37974
37975/*
37976** Open the file zPath.
37977**
37978** Previously, the SQLite OS layer used three functions in place of this
37979** one:
37980**
37981** sqlite3OsOpenReadWrite();
37982** sqlite3OsOpenReadOnly();
37983** sqlite3OsOpenExclusive();
37984**
37985** These calls correspond to the following combinations of flags:
37986**
37987** ReadWrite() -> (READWRITE | CREATE)
37988** ReadOnly() -> (READONLY)
37989** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
37990**
37991** The old OpenExclusive() accepted a boolean argument - "delFlag". If
37992** true, the file was configured to be automatically deleted when the
37993** file handle closed. To achieve the same effect using this new
37994** interface, add the DELETEONCLOSE flag to those specified above for
37995** OpenExclusive().
37996*/
37997static int unixOpen(
37998 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
37999 const char *zPath, /* Pathname of file to be opened */
38000 sqlite3_file *pFile, /* The file descriptor to be filled in */
38001 int flags, /* Input flags to control the opening */
38002 int *pOutFlags /* Output flags returned to SQLite core */
38003){
38004 unixFile *p = (unixFile *)pFile;
38005 int fd = -1; /* File descriptor returned by open() */
38006 int openFlags = 0; /* Flags to pass to open() */
38007 int eType = flags&0xFFFFFF00; /* Type of file to open */
38008 int noLock; /* True to omit locking primitives */
38009 int rc = SQLITE_OK; /* Function Return Code */
38010 int ctrlFlags = 0; /* UNIXFILE_* flags */
38011
38012 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
38013 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
38014 int isCreate = (flags & SQLITE_OPEN_CREATE);
38015 int isReadonly = (flags & SQLITE_OPEN_READONLY);
38016 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
38017#if SQLITE_ENABLE_LOCKING_STYLE
38018 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
38019#endif
38020#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
38021 struct statfs fsInfo;
38022#endif
38023
38024 /* If creating a master or main-file journal, this function will open
38025 ** a file-descriptor on the directory too. The first time unixSync()
38026 ** is called the directory file descriptor will be fsync()ed and close()d.
38027 */
38028 int isNewJrnl = (isCreate && (
38029 eType==SQLITE_OPEN_MASTER_JOURNAL
38030 || eType==SQLITE_OPEN_MAIN_JOURNAL
38031 || eType==SQLITE_OPEN_WAL
38032 ));
38033
38034 /* If argument zPath is a NULL pointer, this function is required to open
38035 ** a temporary file. Use this buffer to store the file name in.
38036 */
38037 char zTmpname[MAX_PATHNAME+2];
38038 const char *zName = zPath;
38039
38040 /* Check the following statements are true:
38041 **
38042 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
38043 ** (b) if CREATE is set, then READWRITE must also be set, and
38044 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
38045 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
38046 */
38047 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
38048 assert(isCreate==0 || isReadWrite);
38049 assert(isExclusive==0 || isCreate);
38050 assert(isDelete==0 || isCreate);
38051
38052 /* The main DB, main journal, WAL file and master journal are never
38053 ** automatically deleted. Nor are they ever temporary files. */
38054 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
38055 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
38056 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
38057 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
38058
38059 /* Assert that the upper layer has set one of the "file-type" flags. */
38060 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
38061 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
38062 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
38063 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
38064 );
38065
38066 /* Detect a pid change and reset the PRNG. There is a race condition
38067 ** here such that two or more threads all trying to open databases at
38068 ** the same instant might all reset the PRNG. But multiple resets
38069 ** are harmless.
38070 */
38071 if( randomnessPid!=osGetpid(0) ){
38072 randomnessPid = osGetpid(0);
38073 sqlite3_randomness(0,0);
38074 }
38075 memset(p, 0, sizeof(unixFile));
38076
38077 if( eType==SQLITE_OPEN_MAIN_DB ){
38078 UnixUnusedFd *pUnused;
38079 pUnused = findReusableFd(zName, flags);
38080 if( pUnused ){
38081 fd = pUnused->fd;
38082 }else{
38083 pUnused = sqlite3_malloc64(sizeof(*pUnused));
38084 if( !pUnused ){
38085 return SQLITE_NOMEM_BKPT;
38086 }
38087 }
38088 p->pPreallocatedUnused = pUnused;
38089
38090 /* Database filenames are double-zero terminated if they are not
38091 ** URIs with parameters. Hence, they can always be passed into
38092 ** sqlite3_uri_parameter(). */
38093 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
38094
38095 }else if( !zName ){
38096 /* If zName is NULL, the upper layer is requesting a temp file. */
38097 assert(isDelete && !isNewJrnl);
38098 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
38099 if( rc!=SQLITE_OK ){
38100 return rc;
38101 }
38102 zName = zTmpname;
38103
38104 /* Generated temporary filenames are always double-zero terminated
38105 ** for use by sqlite3_uri_parameter(). */
38106 assert( zName[strlen(zName)+1]==0 );
38107 }
38108
38109 /* Determine the value of the flags parameter passed to POSIX function
38110 ** open(). These must be calculated even if open() is not called, as
38111 ** they may be stored as part of the file handle and used by the
38112 ** 'conch file' locking functions later on. */
38113 if( isReadonly ) openFlags |= O_RDONLY;
38114 if( isReadWrite ) openFlags |= O_RDWR;
38115 if( isCreate ) openFlags |= O_CREAT;
38116 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
38117 openFlags |= (O_LARGEFILE|O_BINARY);
38118
38119 if( fd<0 ){
38120 mode_t openMode; /* Permissions to create file with */
38121 uid_t uid; /* Userid for the file */
38122 gid_t gid; /* Groupid for the file */
38123 rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
38124 if( rc!=SQLITE_OK ){
38125 assert( !p->pPreallocatedUnused );
38126 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
38127 return rc;
38128 }
38129 fd = robust_open(zName, openFlags, openMode);
38130 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
38131 assert( !isExclusive || (openFlags & O_CREAT)!=0 );
38132 if( fd<0 ){
38133 if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){
38134 /* If unable to create a journal because the directory is not
38135 ** writable, change the error code to indicate that. */
38136 rc = SQLITE_READONLY_DIRECTORY;
38137 }else if( errno!=EISDIR && isReadWrite ){
38138 /* Failed to open the file for read/write access. Try read-only. */
38139 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
38140 openFlags &= ~(O_RDWR|O_CREAT);
38141 flags |= SQLITE_OPEN_READONLY;
38142 openFlags |= O_RDONLY;
38143 isReadonly = 1;
38144 fd = robust_open(zName, openFlags, openMode);
38145 }
38146 }
38147 if( fd<0 ){
38148 int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
38149 if( rc==SQLITE_OK ) rc = rc2;
38150 goto open_finished;
38151 }
38152
38153 /* If this process is running as root and if creating a new rollback
38154 ** journal or WAL file, set the ownership of the journal or WAL to be
38155 ** the same as the original database.
38156 */
38157 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
38158 robustFchown(fd, uid, gid);
38159 }
38160 }
38161 assert( fd>=0 );
38162 if( pOutFlags ){
38163 *pOutFlags = flags;
38164 }
38165
38166 if( p->pPreallocatedUnused ){
38167 p->pPreallocatedUnused->fd = fd;
38168 p->pPreallocatedUnused->flags = flags;
38169 }
38170
38171 if( isDelete ){
38172#if OS_VXWORKS
38173 zPath = zName;
38174#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
38175 zPath = sqlite3_mprintf("%s", zName);
38176 if( zPath==0 ){
38177 robust_close(p, fd, __LINE__);
38178 return SQLITE_NOMEM_BKPT;
38179 }
38180#else
38181 osUnlink(zName);
38182#endif
38183 }
38184#if SQLITE_ENABLE_LOCKING_STYLE
38185 else{
38186 p->openFlags = openFlags;
38187 }
38188#endif
38189
38190#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
38191 if( fstatfs(fd, &fsInfo) == -1 ){
38192 storeLastErrno(p, errno);
38193 robust_close(p, fd, __LINE__);
38194 return SQLITE_IOERR_ACCESS;
38195 }
38196 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
38197 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
38198 }
38199 if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
38200 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
38201 }
38202#endif
38203
38204 /* Set up appropriate ctrlFlags */
38205 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE;
38206 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY;
38207 noLock = eType!=SQLITE_OPEN_MAIN_DB;
38208 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK;
38209 if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC;
38210 if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
38211
38212#if SQLITE_ENABLE_LOCKING_STYLE
38213#if SQLITE_PREFER_PROXY_LOCKING
38214 isAutoProxy = 1;
38215#endif
38216 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
38217 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
38218 int useProxy = 0;
38219
38220 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
38221 ** never use proxy, NULL means use proxy for non-local files only. */
38222 if( envforce!=NULL ){
38223 useProxy = atoi(envforce)>0;
38224 }else{
38225 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
38226 }
38227 if( useProxy ){
38228 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
38229 if( rc==SQLITE_OK ){
38230 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
38231 if( rc!=SQLITE_OK ){
38232 /* Use unixClose to clean up the resources added in fillInUnixFile
38233 ** and clear all the structure's references. Specifically,
38234 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
38235 */
38236 unixClose(pFile);
38237 return rc;
38238 }
38239 }
38240 goto open_finished;
38241 }
38242 }
38243#endif
38244
38245 assert( zPath==0 || zPath[0]=='/'
38246 || eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL
38247 );
38248 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
38249
38250open_finished:
38251 if( rc!=SQLITE_OK ){
38252 sqlite3_free(p->pPreallocatedUnused);
38253 }
38254 return rc;
38255}
38256
38257
38258/*
38259** Delete the file at zPath. If the dirSync argument is true, fsync()
38260** the directory after deleting the file.
38261*/
38262static int unixDelete(
38263 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
38264 const char *zPath, /* Name of file to be deleted */
38265 int dirSync /* If true, fsync() directory after deleting file */
38266){
38267 int rc = SQLITE_OK;
38268 UNUSED_PARAMETER(NotUsed);
38269 SimulateIOError(return SQLITE_IOERR_DELETE);
38270 if( osUnlink(zPath)==(-1) ){
38271 if( errno==ENOENT
38272#if OS_VXWORKS
38273 || osAccess(zPath,0)!=0
38274#endif
38275 ){
38276 rc = SQLITE_IOERR_DELETE_NOENT;
38277 }else{
38278 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
38279 }
38280 return rc;
38281 }
38282#ifndef SQLITE_DISABLE_DIRSYNC
38283 if( (dirSync & 1)!=0 ){
38284 int fd;
38285 rc = osOpenDirectory(zPath, &fd);
38286 if( rc==SQLITE_OK ){
38287 if( full_fsync(fd,0,0) ){
38288 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
38289 }
38290 robust_close(0, fd, __LINE__);
38291 }else{
38292 assert( rc==SQLITE_CANTOPEN );
38293 rc = SQLITE_OK;
38294 }
38295 }
38296#endif
38297 return rc;
38298}
38299
38300/*
38301** Test the existence of or access permissions of file zPath. The
38302** test performed depends on the value of flags:
38303**
38304** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
38305** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
38306** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
38307**
38308** Otherwise return 0.
38309*/
38310static int unixAccess(
38311 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
38312 const char *zPath, /* Path of the file to examine */
38313 int flags, /* What do we want to learn about the zPath file? */
38314 int *pResOut /* Write result boolean here */
38315){
38316 UNUSED_PARAMETER(NotUsed);
38317 SimulateIOError( return SQLITE_IOERR_ACCESS; );
38318 assert( pResOut!=0 );
38319
38320 /* The spec says there are three possible values for flags. But only
38321 ** two of them are actually used */
38322 assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE );
38323
38324 if( flags==SQLITE_ACCESS_EXISTS ){
38325 struct stat buf;
38326 *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0);
38327 }else{
38328 *pResOut = osAccess(zPath, W_OK|R_OK)==0;
38329 }
38330 return SQLITE_OK;
38331}
38332
38333/*
38334**
38335*/
38336static int mkFullPathname(
38337 const char *zPath, /* Input path */
38338 char *zOut, /* Output buffer */
38339 int nOut /* Allocated size of buffer zOut */
38340){
38341 int nPath = sqlite3Strlen30(zPath);
38342 int iOff = 0;
38343 if( zPath[0]!='/' ){
38344 if( osGetcwd(zOut, nOut-2)==0 ){
38345 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
38346 }
38347 iOff = sqlite3Strlen30(zOut);
38348 zOut[iOff++] = '/';
38349 }
38350 if( (iOff+nPath+1)>nOut ){
38351 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
38352 ** even if it returns an error. */
38353 zOut[iOff] = '\0';
38354 return SQLITE_CANTOPEN_BKPT;
38355 }
38356 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
38357 return SQLITE_OK;
38358}
38359
38360/*
38361** Turn a relative pathname into a full pathname. The relative path
38362** is stored as a nul-terminated string in the buffer pointed to by
38363** zPath.
38364**
38365** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
38366** (in this case, MAX_PATHNAME bytes). The full-path is written to
38367** this buffer before returning.
38368*/
38369static int unixFullPathname(
38370 sqlite3_vfs *pVfs, /* Pointer to vfs object */
38371 const char *zPath, /* Possibly relative input path */
38372 int nOut, /* Size of output buffer in bytes */
38373 char *zOut /* Output buffer */
38374){
38375#if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT)
38376 return mkFullPathname(zPath, zOut, nOut);
38377#else
38378 int rc = SQLITE_OK;
38379 int nByte;
38380 int nLink = 1; /* Number of symbolic links followed so far */
38381 const char *zIn = zPath; /* Input path for each iteration of loop */
38382 char *zDel = 0;
38383
38384 assert( pVfs->mxPathname==MAX_PATHNAME );
38385 UNUSED_PARAMETER(pVfs);
38386
38387 /* It's odd to simulate an io-error here, but really this is just
38388 ** using the io-error infrastructure to test that SQLite handles this
38389 ** function failing. This function could fail if, for example, the
38390 ** current working directory has been unlinked.
38391 */
38392 SimulateIOError( return SQLITE_ERROR );
38393
38394 do {
38395
38396 /* Call stat() on path zIn. Set bLink to true if the path is a symbolic
38397 ** link, or false otherwise. */
38398 int bLink = 0;
38399 struct stat buf;
38400 if( osLstat(zIn, &buf)!=0 ){
38401 if( errno!=ENOENT ){
38402 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
38403 }
38404 }else{
38405 bLink = S_ISLNK(buf.st_mode);
38406 }
38407
38408 if( bLink ){
38409 if( zDel==0 ){
38410 zDel = sqlite3_malloc(nOut);
38411 if( zDel==0 ) rc = SQLITE_NOMEM_BKPT;
38412 }else if( ++nLink>SQLITE_MAX_SYMLINKS ){
38413 rc = SQLITE_CANTOPEN_BKPT;
38414 }
38415
38416 if( rc==SQLITE_OK ){
38417 nByte = osReadlink(zIn, zDel, nOut-1);
38418 if( nByte<0 ){
38419 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
38420 }else{
38421 if( zDel[0]!='/' ){
38422 int n;
38423 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
38424 if( nByte+n+1>nOut ){
38425 rc = SQLITE_CANTOPEN_BKPT;
38426 }else{
38427 memmove(&zDel[n], zDel, nByte+1);
38428 memcpy(zDel, zIn, n);
38429 nByte += n;
38430 }
38431 }
38432 zDel[nByte] = '\0';
38433 }
38434 }
38435
38436 zIn = zDel;
38437 }
38438
38439 assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' );
38440 if( rc==SQLITE_OK && zIn!=zOut ){
38441 rc = mkFullPathname(zIn, zOut, nOut);
38442 }
38443 if( bLink==0 ) break;
38444 zIn = zOut;
38445 }while( rc==SQLITE_OK );
38446
38447 sqlite3_free(zDel);
38448 return rc;
38449#endif /* HAVE_READLINK && HAVE_LSTAT */
38450}
38451
38452
38453#ifndef SQLITE_OMIT_LOAD_EXTENSION
38454/*
38455** Interfaces for opening a shared library, finding entry points
38456** within the shared library, and closing the shared library.
38457*/
38458#include <dlfcn.h>
38459static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
38460 UNUSED_PARAMETER(NotUsed);
38461 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
38462}
38463
38464/*
38465** SQLite calls this function immediately after a call to unixDlSym() or
38466** unixDlOpen() fails (returns a null pointer). If a more detailed error
38467** message is available, it is written to zBufOut. If no error message
38468** is available, zBufOut is left unmodified and SQLite uses a default
38469** error message.
38470*/
38471static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
38472 const char *zErr;
38473 UNUSED_PARAMETER(NotUsed);
38474 unixEnterMutex();
38475 zErr = dlerror();
38476 if( zErr ){
38477 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
38478 }
38479 unixLeaveMutex();
38480}
38481static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
38482 /*
38483 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
38484 ** cast into a pointer to a function. And yet the library dlsym() routine
38485 ** returns a void* which is really a pointer to a function. So how do we
38486 ** use dlsym() with -pedantic-errors?
38487 **
38488 ** Variable x below is defined to be a pointer to a function taking
38489 ** parameters void* and const char* and returning a pointer to a function.
38490 ** We initialize x by assigning it a pointer to the dlsym() function.
38491 ** (That assignment requires a cast.) Then we call the function that
38492 ** x points to.
38493 **
38494 ** This work-around is unlikely to work correctly on any system where
38495 ** you really cannot cast a function pointer into void*. But then, on the
38496 ** other hand, dlsym() will not work on such a system either, so we have
38497 ** not really lost anything.
38498 */
38499 void (*(*x)(void*,const char*))(void);
38500 UNUSED_PARAMETER(NotUsed);
38501 x = (void(*(*)(void*,const char*))(void))dlsym;
38502 return (*x)(p, zSym);
38503}
38504static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
38505 UNUSED_PARAMETER(NotUsed);
38506 dlclose(pHandle);
38507}
38508#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
38509 #define unixDlOpen 0
38510 #define unixDlError 0
38511 #define unixDlSym 0
38512 #define unixDlClose 0
38513#endif
38514
38515/*
38516** Write nBuf bytes of random data to the supplied buffer zBuf.
38517*/
38518static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
38519 UNUSED_PARAMETER(NotUsed);
38520 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
38521
38522 /* We have to initialize zBuf to prevent valgrind from reporting
38523 ** errors. The reports issued by valgrind are incorrect - we would
38524 ** prefer that the randomness be increased by making use of the
38525 ** uninitialized space in zBuf - but valgrind errors tend to worry
38526 ** some users. Rather than argue, it seems easier just to initialize
38527 ** the whole array and silence valgrind, even if that means less randomness
38528 ** in the random seed.
38529 **
38530 ** When testing, initializing zBuf[] to zero is all we do. That means
38531 ** that we always use the same random number sequence. This makes the
38532 ** tests repeatable.
38533 */
38534 memset(zBuf, 0, nBuf);
38535 randomnessPid = osGetpid(0);
38536#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
38537 {
38538 int fd, got;
38539 fd = robust_open("/dev/urandom", O_RDONLY, 0);
38540 if( fd<0 ){
38541 time_t t;
38542 time(&t);
38543 memcpy(zBuf, &t, sizeof(t));
38544 memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
38545 assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
38546 nBuf = sizeof(t) + sizeof(randomnessPid);
38547 }else{
38548 do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
38549 robust_close(0, fd, __LINE__);
38550 }
38551 }
38552#endif
38553 return nBuf;
38554}
38555
38556
38557/*
38558** Sleep for a little while. Return the amount of time slept.
38559** The argument is the number of microseconds we want to sleep.
38560** The return value is the number of microseconds of sleep actually
38561** requested from the underlying operating system, a number which
38562** might be greater than or equal to the argument, but not less
38563** than the argument.
38564*/
38565static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
38566#if OS_VXWORKS
38567 struct timespec sp;
38568
38569 sp.tv_sec = microseconds / 1000000;
38570 sp.tv_nsec = (microseconds % 1000000) * 1000;
38571 nanosleep(&sp, NULL);
38572 UNUSED_PARAMETER(NotUsed);
38573 return microseconds;
38574#elif defined(HAVE_USLEEP) && HAVE_USLEEP
38575 usleep(microseconds);
38576 UNUSED_PARAMETER(NotUsed);
38577 return microseconds;
38578#else
38579 int seconds = (microseconds+999999)/1000000;
38580 sleep(seconds);
38581 UNUSED_PARAMETER(NotUsed);
38582 return seconds*1000000;
38583#endif
38584}
38585
38586/*
38587** The following variable, if set to a non-zero value, is interpreted as
38588** the number of seconds since 1970 and is used to set the result of
38589** sqlite3OsCurrentTime() during testing.
38590*/
38591#ifdef SQLITE_TEST
38592SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
38593#endif
38594
38595/*
38596** Find the current time (in Universal Coordinated Time). Write into *piNow
38597** the current time and date as a Julian Day number times 86_400_000. In
38598** other words, write into *piNow the number of milliseconds since the Julian
38599** epoch of noon in Greenwich on November 24, 4714 B.C according to the
38600** proleptic Gregorian calendar.
38601**
38602** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
38603** cannot be found.
38604*/
38605static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
38606 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
38607 int rc = SQLITE_OK;
38608#if defined(NO_GETTOD)
38609 time_t t;
38610 time(&t);
38611 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
38612#elif OS_VXWORKS
38613 struct timespec sNow;
38614 clock_gettime(CLOCK_REALTIME, &sNow);
38615 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
38616#else
38617 struct timeval sNow;
38618 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
38619 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
38620#endif
38621
38622#ifdef SQLITE_TEST
38623 if( sqlite3_current_time ){
38624 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
38625 }
38626#endif
38627 UNUSED_PARAMETER(NotUsed);
38628 return rc;
38629}
38630
38631#ifndef SQLITE_OMIT_DEPRECATED
38632/*
38633** Find the current time (in Universal Coordinated Time). Write the
38634** current time and date as a Julian Day number into *prNow and
38635** return 0. Return 1 if the time and date cannot be found.
38636*/
38637static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
38638 sqlite3_int64 i = 0;
38639 int rc;
38640 UNUSED_PARAMETER(NotUsed);
38641 rc = unixCurrentTimeInt64(0, &i);
38642 *prNow = i/86400000.0;
38643 return rc;
38644}
38645#else
38646# define unixCurrentTime 0
38647#endif
38648
38649/*
38650** The xGetLastError() method is designed to return a better
38651** low-level error message when operating-system problems come up
38652** during SQLite operation. Only the integer return code is currently
38653** used.
38654*/
38655static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
38656 UNUSED_PARAMETER(NotUsed);
38657 UNUSED_PARAMETER(NotUsed2);
38658 UNUSED_PARAMETER(NotUsed3);
38659 return errno;
38660}
38661
38662
38663/*
38664************************ End of sqlite3_vfs methods ***************************
38665******************************************************************************/
38666
38667/******************************************************************************
38668************************** Begin Proxy Locking ********************************
38669**
38670** Proxy locking is a "uber-locking-method" in this sense: It uses the
38671** other locking methods on secondary lock files. Proxy locking is a
38672** meta-layer over top of the primitive locking implemented above. For
38673** this reason, the division that implements of proxy locking is deferred
38674** until late in the file (here) after all of the other I/O methods have
38675** been defined - so that the primitive locking methods are available
38676** as services to help with the implementation of proxy locking.
38677**
38678****
38679**
38680** The default locking schemes in SQLite use byte-range locks on the
38681** database file to coordinate safe, concurrent access by multiple readers
38682** and writers [http://sqlite.org/lockingv3.html]. The five file locking
38683** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
38684** as POSIX read & write locks over fixed set of locations (via fsctl),
38685** on AFP and SMB only exclusive byte-range locks are available via fsctl
38686** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
38687** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
38688** address in the shared range is taken for a SHARED lock, the entire
38689** shared range is taken for an EXCLUSIVE lock):
38690**
38691** PENDING_BYTE 0x40000000
38692** RESERVED_BYTE 0x40000001
38693** SHARED_RANGE 0x40000002 -> 0x40000200
38694**
38695** This works well on the local file system, but shows a nearly 100x
38696** slowdown in read performance on AFP because the AFP client disables
38697** the read cache when byte-range locks are present. Enabling the read
38698** cache exposes a cache coherency problem that is present on all OS X
38699** supported network file systems. NFS and AFP both observe the
38700** close-to-open semantics for ensuring cache coherency
38701** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
38702** address the requirements for concurrent database access by multiple
38703** readers and writers
38704** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
38705**
38706** To address the performance and cache coherency issues, proxy file locking
38707** changes the way database access is controlled by limiting access to a
38708** single host at a time and moving file locks off of the database file
38709** and onto a proxy file on the local file system.
38710**
38711**
38712** Using proxy locks
38713** -----------------
38714**
38715** C APIs
38716**
38717** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
38718** <proxy_path> | ":auto:");
38719** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
38720** &<proxy_path>);
38721**
38722**
38723** SQL pragmas
38724**
38725** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
38726** PRAGMA [database.]lock_proxy_file
38727**
38728** Specifying ":auto:" means that if there is a conch file with a matching
38729** host ID in it, the proxy path in the conch file will be used, otherwise
38730** a proxy path based on the user's temp dir
38731** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
38732** actual proxy file name is generated from the name and path of the
38733** database file. For example:
38734**
38735** For database path "/Users/me/foo.db"
38736** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
38737**
38738** Once a lock proxy is configured for a database connection, it can not
38739** be removed, however it may be switched to a different proxy path via
38740** the above APIs (assuming the conch file is not being held by another
38741** connection or process).
38742**
38743**
38744** How proxy locking works
38745** -----------------------
38746**
38747** Proxy file locking relies primarily on two new supporting files:
38748**
38749** * conch file to limit access to the database file to a single host
38750** at a time
38751**
38752** * proxy file to act as a proxy for the advisory locks normally
38753** taken on the database
38754**
38755** The conch file - to use a proxy file, sqlite must first "hold the conch"
38756** by taking an sqlite-style shared lock on the conch file, reading the
38757** contents and comparing the host's unique host ID (see below) and lock
38758** proxy path against the values stored in the conch. The conch file is
38759** stored in the same directory as the database file and the file name
38760** is patterned after the database file name as ".<databasename>-conch".
38761** If the conch file does not exist, or its contents do not match the
38762** host ID and/or proxy path, then the lock is escalated to an exclusive
38763** lock and the conch file contents is updated with the host ID and proxy
38764** path and the lock is downgraded to a shared lock again. If the conch
38765** is held by another process (with a shared lock), the exclusive lock
38766** will fail and SQLITE_BUSY is returned.
38767**
38768** The proxy file - a single-byte file used for all advisory file locks
38769** normally taken on the database file. This allows for safe sharing
38770** of the database file for multiple readers and writers on the same
38771** host (the conch ensures that they all use the same local lock file).
38772**
38773** Requesting the lock proxy does not immediately take the conch, it is
38774** only taken when the first request to lock database file is made.
38775** This matches the semantics of the traditional locking behavior, where
38776** opening a connection to a database file does not take a lock on it.
38777** The shared lock and an open file descriptor are maintained until
38778** the connection to the database is closed.
38779**
38780** The proxy file and the lock file are never deleted so they only need
38781** to be created the first time they are used.
38782**
38783** Configuration options
38784** ---------------------
38785**
38786** SQLITE_PREFER_PROXY_LOCKING
38787**
38788** Database files accessed on non-local file systems are
38789** automatically configured for proxy locking, lock files are
38790** named automatically using the same logic as
38791** PRAGMA lock_proxy_file=":auto:"
38792**
38793** SQLITE_PROXY_DEBUG
38794**
38795** Enables the logging of error messages during host id file
38796** retrieval and creation
38797**
38798** LOCKPROXYDIR
38799**
38800** Overrides the default directory used for lock proxy files that
38801** are named automatically via the ":auto:" setting
38802**
38803** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
38804**
38805** Permissions to use when creating a directory for storing the
38806** lock proxy files, only used when LOCKPROXYDIR is not set.
38807**
38808**
38809** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
38810** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
38811** force proxy locking to be used for every database file opened, and 0
38812** will force automatic proxy locking to be disabled for all database
38813** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
38814** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
38815*/
38816
38817/*
38818** Proxy locking is only available on MacOSX
38819*/
38820#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
38821
38822/*
38823** The proxyLockingContext has the path and file structures for the remote
38824** and local proxy files in it
38825*/
38826typedef struct proxyLockingContext proxyLockingContext;
38827struct proxyLockingContext {
38828 unixFile *conchFile; /* Open conch file */
38829 char *conchFilePath; /* Name of the conch file */
38830 unixFile *lockProxy; /* Open proxy lock file */
38831 char *lockProxyPath; /* Name of the proxy lock file */
38832 char *dbPath; /* Name of the open file */
38833 int conchHeld; /* 1 if the conch is held, -1 if lockless */
38834 int nFails; /* Number of conch taking failures */
38835 void *oldLockingContext; /* Original lockingcontext to restore on close */
38836 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
38837};
38838
38839/*
38840** The proxy lock file path for the database at dbPath is written into lPath,
38841** which must point to valid, writable memory large enough for a maxLen length
38842** file path.
38843*/
38844static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
38845 int len;
38846 int dbLen;
38847 int i;
38848
38849#ifdef LOCKPROXYDIR
38850 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
38851#else
38852# ifdef _CS_DARWIN_USER_TEMP_DIR
38853 {
38854 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
38855 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
38856 lPath, errno, osGetpid(0)));
38857 return SQLITE_IOERR_LOCK;
38858 }
38859 len = strlcat(lPath, "sqliteplocks", maxLen);
38860 }
38861# else
38862 len = strlcpy(lPath, "/tmp/", maxLen);
38863# endif
38864#endif
38865
38866 if( lPath[len-1]!='/' ){
38867 len = strlcat(lPath, "/", maxLen);
38868 }
38869
38870 /* transform the db path to a unique cache name */
38871 dbLen = (int)strlen(dbPath);
38872 for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
38873 char c = dbPath[i];
38874 lPath[i+len] = (c=='/')?'_':c;
38875 }
38876 lPath[i+len]='\0';
38877 strlcat(lPath, ":auto:", maxLen);
38878 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
38879 return SQLITE_OK;
38880}
38881
38882/*
38883 ** Creates the lock file and any missing directories in lockPath
38884 */
38885static int proxyCreateLockPath(const char *lockPath){
38886 int i, len;
38887 char buf[MAXPATHLEN];
38888 int start = 0;
38889
38890 assert(lockPath!=NULL);
38891 /* try to create all the intermediate directories */
38892 len = (int)strlen(lockPath);
38893 buf[0] = lockPath[0];
38894 for( i=1; i<len; i++ ){
38895 if( lockPath[i] == '/' && (i - start > 0) ){
38896 /* only mkdir if leaf dir != "." or "/" or ".." */
38897 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
38898 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
38899 buf[i]='\0';
38900 if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
38901 int err=errno;
38902 if( err!=EEXIST ) {
38903 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
38904 "'%s' proxy lock path=%s pid=%d\n",
38905 buf, strerror(err), lockPath, osGetpid(0)));
38906 return err;
38907 }
38908 }
38909 }
38910 start=i+1;
38911 }
38912 buf[i] = lockPath[i];
38913 }
38914 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
38915 return 0;
38916}
38917
38918/*
38919** Create a new VFS file descriptor (stored in memory obtained from
38920** sqlite3_malloc) and open the file named "path" in the file descriptor.
38921**
38922** The caller is responsible not only for closing the file descriptor
38923** but also for freeing the memory associated with the file descriptor.
38924*/
38925static int proxyCreateUnixFile(
38926 const char *path, /* path for the new unixFile */
38927 unixFile **ppFile, /* unixFile created and returned by ref */
38928 int islockfile /* if non zero missing dirs will be created */
38929) {
38930 int fd = -1;
38931 unixFile *pNew;
38932 int rc = SQLITE_OK;
38933 int openFlags = O_RDWR | O_CREAT;
38934 sqlite3_vfs dummyVfs;
38935 int terrno = 0;
38936 UnixUnusedFd *pUnused = NULL;
38937
38938 /* 1. first try to open/create the file
38939 ** 2. if that fails, and this is a lock file (not-conch), try creating
38940 ** the parent directories and then try again.
38941 ** 3. if that fails, try to open the file read-only
38942 ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
38943 */
38944 pUnused = findReusableFd(path, openFlags);
38945 if( pUnused ){
38946 fd = pUnused->fd;
38947 }else{
38948 pUnused = sqlite3_malloc64(sizeof(*pUnused));
38949 if( !pUnused ){
38950 return SQLITE_NOMEM_BKPT;
38951 }
38952 }
38953 if( fd<0 ){
38954 fd = robust_open(path, openFlags, 0);
38955 terrno = errno;
38956 if( fd<0 && errno==ENOENT && islockfile ){
38957 if( proxyCreateLockPath(path) == SQLITE_OK ){
38958 fd = robust_open(path, openFlags, 0);
38959 }
38960 }
38961 }
38962 if( fd<0 ){
38963 openFlags = O_RDONLY;
38964 fd = robust_open(path, openFlags, 0);
38965 terrno = errno;
38966 }
38967 if( fd<0 ){
38968 if( islockfile ){
38969 return SQLITE_BUSY;
38970 }
38971 switch (terrno) {
38972 case EACCES:
38973 return SQLITE_PERM;
38974 case EIO:
38975 return SQLITE_IOERR_LOCK; /* even though it is the conch */
38976 default:
38977 return SQLITE_CANTOPEN_BKPT;
38978 }
38979 }
38980
38981 pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
38982 if( pNew==NULL ){
38983 rc = SQLITE_NOMEM_BKPT;
38984 goto end_create_proxy;
38985 }
38986 memset(pNew, 0, sizeof(unixFile));
38987 pNew->openFlags = openFlags;
38988 memset(&dummyVfs, 0, sizeof(dummyVfs));
38989 dummyVfs.pAppData = (void*)&autolockIoFinder;
38990 dummyVfs.zName = "dummy";
38991 pUnused->fd = fd;
38992 pUnused->flags = openFlags;
38993 pNew->pPreallocatedUnused = pUnused;
38994
38995 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
38996 if( rc==SQLITE_OK ){
38997 *ppFile = pNew;
38998 return SQLITE_OK;
38999 }
39000end_create_proxy:
39001 robust_close(pNew, fd, __LINE__);
39002 sqlite3_free(pNew);
39003 sqlite3_free(pUnused);
39004 return rc;
39005}
39006
39007#ifdef SQLITE_TEST
39008/* simulate multiple hosts by creating unique hostid file paths */
39009SQLITE_API int sqlite3_hostid_num = 0;
39010#endif
39011
39012#define PROXY_HOSTIDLEN 16 /* conch file host id length */
39013
39014#ifdef HAVE_GETHOSTUUID
39015/* Not always defined in the headers as it ought to be */
39016extern int gethostuuid(uuid_t id, const struct timespec *wait);
39017#endif
39018
39019/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
39020** bytes of writable memory.
39021*/
39022static int proxyGetHostID(unsigned char *pHostID, int *pError){
39023 assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
39024 memset(pHostID, 0, PROXY_HOSTIDLEN);
39025#ifdef HAVE_GETHOSTUUID
39026 {
39027 struct timespec timeout = {1, 0}; /* 1 sec timeout */
39028 if( gethostuuid(pHostID, &timeout) ){
39029 int err = errno;
39030 if( pError ){
39031 *pError = err;
39032 }
39033 return SQLITE_IOERR;
39034 }
39035 }
39036#else
39037 UNUSED_PARAMETER(pError);
39038#endif
39039#ifdef SQLITE_TEST
39040 /* simulate multiple hosts by creating unique hostid file paths */
39041 if( sqlite3_hostid_num != 0){
39042 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
39043 }
39044#endif
39045
39046 return SQLITE_OK;
39047}
39048
39049/* The conch file contains the header, host id and lock file path
39050 */
39051#define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
39052#define PROXY_HEADERLEN 1 /* conch file header length */
39053#define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
39054#define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
39055
39056/*
39057** Takes an open conch file, copies the contents to a new path and then moves
39058** it back. The newly created file's file descriptor is assigned to the
39059** conch file structure and finally the original conch file descriptor is
39060** closed. Returns zero if successful.
39061*/
39062static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
39063 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39064 unixFile *conchFile = pCtx->conchFile;
39065 char tPath[MAXPATHLEN];
39066 char buf[PROXY_MAXCONCHLEN];
39067 char *cPath = pCtx->conchFilePath;
39068 size_t readLen = 0;
39069 size_t pathLen = 0;
39070 char errmsg[64] = "";
39071 int fd = -1;
39072 int rc = -1;
39073 UNUSED_PARAMETER(myHostID);
39074
39075 /* create a new path by replace the trailing '-conch' with '-break' */
39076 pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
39077 if( pathLen>MAXPATHLEN || pathLen<6 ||
39078 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
39079 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
39080 goto end_breaklock;
39081 }
39082 /* read the conch content */
39083 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
39084 if( readLen<PROXY_PATHINDEX ){
39085 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
39086 goto end_breaklock;
39087 }
39088 /* write it out to the temporary break file */
39089 fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0);
39090 if( fd<0 ){
39091 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
39092 goto end_breaklock;
39093 }
39094 if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
39095 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
39096 goto end_breaklock;
39097 }
39098 if( rename(tPath, cPath) ){
39099 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
39100 goto end_breaklock;
39101 }
39102 rc = 0;
39103 fprintf(stderr, "broke stale lock on %s\n", cPath);
39104 robust_close(pFile, conchFile->h, __LINE__);
39105 conchFile->h = fd;
39106 conchFile->openFlags = O_RDWR | O_CREAT;
39107
39108end_breaklock:
39109 if( rc ){
39110 if( fd>=0 ){
39111 osUnlink(tPath);
39112 robust_close(pFile, fd, __LINE__);
39113 }
39114 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
39115 }
39116 return rc;
39117}
39118
39119/* Take the requested lock on the conch file and break a stale lock if the
39120** host id matches.
39121*/
39122static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
39123 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39124 unixFile *conchFile = pCtx->conchFile;
39125 int rc = SQLITE_OK;
39126 int nTries = 0;
39127 struct timespec conchModTime;
39128
39129 memset(&conchModTime, 0, sizeof(conchModTime));
39130 do {
39131 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
39132 nTries ++;
39133 if( rc==SQLITE_BUSY ){
39134 /* If the lock failed (busy):
39135 * 1st try: get the mod time of the conch, wait 0.5s and try again.
39136 * 2nd try: fail if the mod time changed or host id is different, wait
39137 * 10 sec and try again
39138 * 3rd try: break the lock unless the mod time has changed.
39139 */
39140 struct stat buf;
39141 if( osFstat(conchFile->h, &buf) ){
39142 storeLastErrno(pFile, errno);
39143 return SQLITE_IOERR_LOCK;
39144 }
39145
39146 if( nTries==1 ){
39147 conchModTime = buf.st_mtimespec;
39148 usleep(500000); /* wait 0.5 sec and try the lock again*/
39149 continue;
39150 }
39151
39152 assert( nTries>1 );
39153 if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
39154 conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
39155 return SQLITE_BUSY;
39156 }
39157
39158 if( nTries==2 ){
39159 char tBuf[PROXY_MAXCONCHLEN];
39160 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
39161 if( len<0 ){
39162 storeLastErrno(pFile, errno);
39163 return SQLITE_IOERR_LOCK;
39164 }
39165 if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
39166 /* don't break the lock if the host id doesn't match */
39167 if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
39168 return SQLITE_BUSY;
39169 }
39170 }else{
39171 /* don't break the lock on short read or a version mismatch */
39172 return SQLITE_BUSY;
39173 }
39174 usleep(10000000); /* wait 10 sec and try the lock again */
39175 continue;
39176 }
39177
39178 assert( nTries==3 );
39179 if( 0==proxyBreakConchLock(pFile, myHostID) ){
39180 rc = SQLITE_OK;
39181 if( lockType==EXCLUSIVE_LOCK ){
39182 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
39183 }
39184 if( !rc ){
39185 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
39186 }
39187 }
39188 }
39189 } while( rc==SQLITE_BUSY && nTries<3 );
39190
39191 return rc;
39192}
39193
39194/* Takes the conch by taking a shared lock and read the contents conch, if
39195** lockPath is non-NULL, the host ID and lock file path must match. A NULL
39196** lockPath means that the lockPath in the conch file will be used if the
39197** host IDs match, or a new lock path will be generated automatically
39198** and written to the conch file.
39199*/
39200static int proxyTakeConch(unixFile *pFile){
39201 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39202
39203 if( pCtx->conchHeld!=0 ){
39204 return SQLITE_OK;
39205 }else{
39206 unixFile *conchFile = pCtx->conchFile;
39207 uuid_t myHostID;
39208 int pError = 0;
39209 char readBuf[PROXY_MAXCONCHLEN];
39210 char lockPath[MAXPATHLEN];
39211 char *tempLockPath = NULL;
39212 int rc = SQLITE_OK;
39213 int createConch = 0;
39214 int hostIdMatch = 0;
39215 int readLen = 0;
39216 int tryOldLockPath = 0;
39217 int forceNewLockPath = 0;
39218
39219 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
39220 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
39221 osGetpid(0)));
39222
39223 rc = proxyGetHostID(myHostID, &pError);
39224 if( (rc&0xff)==SQLITE_IOERR ){
39225 storeLastErrno(pFile, pError);
39226 goto end_takeconch;
39227 }
39228 rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
39229 if( rc!=SQLITE_OK ){
39230 goto end_takeconch;
39231 }
39232 /* read the existing conch file */
39233 readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
39234 if( readLen<0 ){
39235 /* I/O error: lastErrno set by seekAndRead */
39236 storeLastErrno(pFile, conchFile->lastErrno);
39237 rc = SQLITE_IOERR_READ;
39238 goto end_takeconch;
39239 }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
39240 readBuf[0]!=(char)PROXY_CONCHVERSION ){
39241 /* a short read or version format mismatch means we need to create a new
39242 ** conch file.
39243 */
39244 createConch = 1;
39245 }
39246 /* if the host id matches and the lock path already exists in the conch
39247 ** we'll try to use the path there, if we can't open that path, we'll
39248 ** retry with a new auto-generated path
39249 */
39250 do { /* in case we need to try again for an :auto: named lock file */
39251
39252 if( !createConch && !forceNewLockPath ){
39253 hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
39254 PROXY_HOSTIDLEN);
39255 /* if the conch has data compare the contents */
39256 if( !pCtx->lockProxyPath ){
39257 /* for auto-named local lock file, just check the host ID and we'll
39258 ** use the local lock file path that's already in there
39259 */
39260 if( hostIdMatch ){
39261 size_t pathLen = (readLen - PROXY_PATHINDEX);
39262
39263 if( pathLen>=MAXPATHLEN ){
39264 pathLen=MAXPATHLEN-1;
39265 }
39266 memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
39267 lockPath[pathLen] = 0;
39268 tempLockPath = lockPath;
39269 tryOldLockPath = 1;
39270 /* create a copy of the lock path if the conch is taken */
39271 goto end_takeconch;
39272 }
39273 }else if( hostIdMatch
39274 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
39275 readLen-PROXY_PATHINDEX)
39276 ){
39277 /* conch host and lock path match */
39278 goto end_takeconch;
39279 }
39280 }
39281
39282 /* if the conch isn't writable and doesn't match, we can't take it */
39283 if( (conchFile->openFlags&O_RDWR) == 0 ){
39284 rc = SQLITE_BUSY;
39285 goto end_takeconch;
39286 }
39287
39288 /* either the conch didn't match or we need to create a new one */
39289 if( !pCtx->lockProxyPath ){
39290 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
39291 tempLockPath = lockPath;
39292 /* create a copy of the lock path _only_ if the conch is taken */
39293 }
39294
39295 /* update conch with host and path (this will fail if other process
39296 ** has a shared lock already), if the host id matches, use the big
39297 ** stick.
39298 */
39299 futimes(conchFile->h, NULL);
39300 if( hostIdMatch && !createConch ){
39301 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
39302 /* We are trying for an exclusive lock but another thread in this
39303 ** same process is still holding a shared lock. */
39304 rc = SQLITE_BUSY;
39305 } else {
39306 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
39307 }
39308 }else{
39309 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
39310 }
39311 if( rc==SQLITE_OK ){
39312 char writeBuffer[PROXY_MAXCONCHLEN];
39313 int writeSize = 0;
39314
39315 writeBuffer[0] = (char)PROXY_CONCHVERSION;
39316 memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
39317 if( pCtx->lockProxyPath!=NULL ){
39318 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
39319 MAXPATHLEN);
39320 }else{
39321 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
39322 }
39323 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
39324 robust_ftruncate(conchFile->h, writeSize);
39325 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
39326 full_fsync(conchFile->h,0,0);
39327 /* If we created a new conch file (not just updated the contents of a
39328 ** valid conch file), try to match the permissions of the database
39329 */
39330 if( rc==SQLITE_OK && createConch ){
39331 struct stat buf;
39332 int err = osFstat(pFile->h, &buf);
39333 if( err==0 ){
39334 mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
39335 S_IROTH|S_IWOTH);
39336 /* try to match the database file R/W permissions, ignore failure */
39337#ifndef SQLITE_PROXY_DEBUG
39338 osFchmod(conchFile->h, cmode);
39339#else
39340 do{
39341 rc = osFchmod(conchFile->h, cmode);
39342 }while( rc==(-1) && errno==EINTR );
39343 if( rc!=0 ){
39344 int code = errno;
39345 fprintf(stderr, "fchmod %o FAILED with %d %s\n",
39346 cmode, code, strerror(code));
39347 } else {
39348 fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
39349 }
39350 }else{
39351 int code = errno;
39352 fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
39353 err, code, strerror(code));
39354#endif
39355 }
39356 }
39357 }
39358 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
39359
39360 end_takeconch:
39361 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
39362 if( rc==SQLITE_OK && pFile->openFlags ){
39363 int fd;
39364 if( pFile->h>=0 ){
39365 robust_close(pFile, pFile->h, __LINE__);
39366 }
39367 pFile->h = -1;
39368 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
39369 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
39370 if( fd>=0 ){
39371 pFile->h = fd;
39372 }else{
39373 rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
39374 during locking */
39375 }
39376 }
39377 if( rc==SQLITE_OK && !pCtx->lockProxy ){
39378 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
39379 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
39380 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
39381 /* we couldn't create the proxy lock file with the old lock file path
39382 ** so try again via auto-naming
39383 */
39384 forceNewLockPath = 1;
39385 tryOldLockPath = 0;
39386 continue; /* go back to the do {} while start point, try again */
39387 }
39388 }
39389 if( rc==SQLITE_OK ){
39390 /* Need to make a copy of path if we extracted the value
39391 ** from the conch file or the path was allocated on the stack
39392 */
39393 if( tempLockPath ){
39394 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
39395 if( !pCtx->lockProxyPath ){
39396 rc = SQLITE_NOMEM_BKPT;
39397 }
39398 }
39399 }
39400 if( rc==SQLITE_OK ){
39401 pCtx->conchHeld = 1;
39402
39403 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
39404 afpLockingContext *afpCtx;
39405 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
39406 afpCtx->dbPath = pCtx->lockProxyPath;
39407 }
39408 } else {
39409 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
39410 }
39411 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
39412 rc==SQLITE_OK?"ok":"failed"));
39413 return rc;
39414 } while (1); /* in case we need to retry the :auto: lock file -
39415 ** we should never get here except via the 'continue' call. */
39416 }
39417}
39418
39419/*
39420** If pFile holds a lock on a conch file, then release that lock.
39421*/
39422static int proxyReleaseConch(unixFile *pFile){
39423 int rc = SQLITE_OK; /* Subroutine return code */
39424 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
39425 unixFile *conchFile; /* Name of the conch file */
39426
39427 pCtx = (proxyLockingContext *)pFile->lockingContext;
39428 conchFile = pCtx->conchFile;
39429 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
39430 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
39431 osGetpid(0)));
39432 if( pCtx->conchHeld>0 ){
39433 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
39434 }
39435 pCtx->conchHeld = 0;
39436 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
39437 (rc==SQLITE_OK ? "ok" : "failed")));
39438 return rc;
39439}
39440
39441/*
39442** Given the name of a database file, compute the name of its conch file.
39443** Store the conch filename in memory obtained from sqlite3_malloc64().
39444** Make *pConchPath point to the new name. Return SQLITE_OK on success
39445** or SQLITE_NOMEM if unable to obtain memory.
39446**
39447** The caller is responsible for ensuring that the allocated memory
39448** space is eventually freed.
39449**
39450** *pConchPath is set to NULL if a memory allocation error occurs.
39451*/
39452static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
39453 int i; /* Loop counter */
39454 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
39455 char *conchPath; /* buffer in which to construct conch name */
39456
39457 /* Allocate space for the conch filename and initialize the name to
39458 ** the name of the original database file. */
39459 *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
39460 if( conchPath==0 ){
39461 return SQLITE_NOMEM_BKPT;
39462 }
39463 memcpy(conchPath, dbPath, len+1);
39464
39465 /* now insert a "." before the last / character */
39466 for( i=(len-1); i>=0; i-- ){
39467 if( conchPath[i]=='/' ){
39468 i++;
39469 break;
39470 }
39471 }
39472 conchPath[i]='.';
39473 while ( i<len ){
39474 conchPath[i+1]=dbPath[i];
39475 i++;
39476 }
39477
39478 /* append the "-conch" suffix to the file */
39479 memcpy(&conchPath[i+1], "-conch", 7);
39480 assert( (int)strlen(conchPath) == len+7 );
39481
39482 return SQLITE_OK;
39483}
39484
39485
39486/* Takes a fully configured proxy locking-style unix file and switches
39487** the local lock file path
39488*/
39489static int switchLockProxyPath(unixFile *pFile, const char *path) {
39490 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
39491 char *oldPath = pCtx->lockProxyPath;
39492 int rc = SQLITE_OK;
39493
39494 if( pFile->eFileLock!=NO_LOCK ){
39495 return SQLITE_BUSY;
39496 }
39497
39498 /* nothing to do if the path is NULL, :auto: or matches the existing path */
39499 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
39500 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
39501 return SQLITE_OK;
39502 }else{
39503 unixFile *lockProxy = pCtx->lockProxy;
39504 pCtx->lockProxy=NULL;
39505 pCtx->conchHeld = 0;
39506 if( lockProxy!=NULL ){
39507 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
39508 if( rc ) return rc;
39509 sqlite3_free(lockProxy);
39510 }
39511 sqlite3_free(oldPath);
39512 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
39513 }
39514
39515 return rc;
39516}
39517
39518/*
39519** pFile is a file that has been opened by a prior xOpen call. dbPath
39520** is a string buffer at least MAXPATHLEN+1 characters in size.
39521**
39522** This routine find the filename associated with pFile and writes it
39523** int dbPath.
39524*/
39525static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
39526#if defined(__APPLE__)
39527 if( pFile->pMethod == &afpIoMethods ){
39528 /* afp style keeps a reference to the db path in the filePath field
39529 ** of the struct */
39530 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
39531 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
39532 MAXPATHLEN);
39533 } else
39534#endif
39535 if( pFile->pMethod == &dotlockIoMethods ){
39536 /* dot lock style uses the locking context to store the dot lock
39537 ** file path */
39538 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
39539 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
39540 }else{
39541 /* all other styles use the locking context to store the db file path */
39542 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
39543 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
39544 }
39545 return SQLITE_OK;
39546}
39547
39548/*
39549** Takes an already filled in unix file and alters it so all file locking
39550** will be performed on the local proxy lock file. The following fields
39551** are preserved in the locking context so that they can be restored and
39552** the unix structure properly cleaned up at close time:
39553** ->lockingContext
39554** ->pMethod
39555*/
39556static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
39557 proxyLockingContext *pCtx;
39558 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
39559 char *lockPath=NULL;
39560 int rc = SQLITE_OK;
39561
39562 if( pFile->eFileLock!=NO_LOCK ){
39563 return SQLITE_BUSY;
39564 }
39565 proxyGetDbPathForUnixFile(pFile, dbPath);
39566 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
39567 lockPath=NULL;
39568 }else{
39569 lockPath=(char *)path;
39570 }
39571
39572 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
39573 (lockPath ? lockPath : ":auto:"), osGetpid(0)));
39574
39575 pCtx = sqlite3_malloc64( sizeof(*pCtx) );
39576 if( pCtx==0 ){
39577 return SQLITE_NOMEM_BKPT;
39578 }
39579 memset(pCtx, 0, sizeof(*pCtx));
39580
39581 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
39582 if( rc==SQLITE_OK ){
39583 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
39584 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
39585 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
39586 ** (c) the file system is read-only, then enable no-locking access.
39587 ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
39588 ** that openFlags will have only one of O_RDONLY or O_RDWR.
39589 */
39590 struct statfs fsInfo;
39591 struct stat conchInfo;
39592 int goLockless = 0;
39593
39594 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
39595 int err = errno;
39596 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
39597 goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
39598 }
39599 }
39600 if( goLockless ){
39601 pCtx->conchHeld = -1; /* read only FS/ lockless */
39602 rc = SQLITE_OK;
39603 }
39604 }
39605 }
39606 if( rc==SQLITE_OK && lockPath ){
39607 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
39608 }
39609
39610 if( rc==SQLITE_OK ){
39611 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
39612 if( pCtx->dbPath==NULL ){
39613 rc = SQLITE_NOMEM_BKPT;
39614 }
39615 }
39616 if( rc==SQLITE_OK ){
39617 /* all memory is allocated, proxys are created and assigned,
39618 ** switch the locking context and pMethod then return.
39619 */
39620 pCtx->oldLockingContext = pFile->lockingContext;
39621 pFile->lockingContext = pCtx;
39622 pCtx->pOldMethod = pFile->pMethod;
39623 pFile->pMethod = &proxyIoMethods;
39624 }else{
39625 if( pCtx->conchFile ){
39626 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
39627 sqlite3_free(pCtx->conchFile);
39628 }
39629 sqlite3DbFree(0, pCtx->lockProxyPath);
39630 sqlite3_free(pCtx->conchFilePath);
39631 sqlite3_free(pCtx);
39632 }
39633 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
39634 (rc==SQLITE_OK ? "ok" : "failed")));
39635 return rc;
39636}
39637
39638
39639/*
39640** This routine handles sqlite3_file_control() calls that are specific
39641** to proxy locking.
39642*/
39643static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
39644 switch( op ){
39645 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
39646 unixFile *pFile = (unixFile*)id;
39647 if( pFile->pMethod == &proxyIoMethods ){
39648 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
39649 proxyTakeConch(pFile);
39650 if( pCtx->lockProxyPath ){
39651 *(const char **)pArg = pCtx->lockProxyPath;
39652 }else{
39653 *(const char **)pArg = ":auto: (not held)";
39654 }
39655 } else {
39656 *(const char **)pArg = NULL;
39657 }
39658 return SQLITE_OK;
39659 }
39660 case SQLITE_FCNTL_SET_LOCKPROXYFILE: {
39661 unixFile *pFile = (unixFile*)id;
39662 int rc = SQLITE_OK;
39663 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
39664 if( pArg==NULL || (const char *)pArg==0 ){
39665 if( isProxyStyle ){
39666 /* turn off proxy locking - not supported. If support is added for
39667 ** switching proxy locking mode off then it will need to fail if
39668 ** the journal mode is WAL mode.
39669 */
39670 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
39671 }else{
39672 /* turn off proxy locking - already off - NOOP */
39673 rc = SQLITE_OK;
39674 }
39675 }else{
39676 const char *proxyPath = (const char *)pArg;
39677 if( isProxyStyle ){
39678 proxyLockingContext *pCtx =
39679 (proxyLockingContext*)pFile->lockingContext;
39680 if( !strcmp(pArg, ":auto:")
39681 || (pCtx->lockProxyPath &&
39682 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
39683 ){
39684 rc = SQLITE_OK;
39685 }else{
39686 rc = switchLockProxyPath(pFile, proxyPath);
39687 }
39688 }else{
39689 /* turn on proxy file locking */
39690 rc = proxyTransformUnixFile(pFile, proxyPath);
39691 }
39692 }
39693 return rc;
39694 }
39695 default: {
39696 assert( 0 ); /* The call assures that only valid opcodes are sent */
39697 }
39698 }
39699 /*NOTREACHED*/
39700 return SQLITE_ERROR;
39701}
39702
39703/*
39704** Within this division (the proxying locking implementation) the procedures
39705** above this point are all utilities. The lock-related methods of the
39706** proxy-locking sqlite3_io_method object follow.
39707*/
39708
39709
39710/*
39711** This routine checks if there is a RESERVED lock held on the specified
39712** file by this or any other process. If such a lock is held, set *pResOut
39713** to a non-zero value otherwise *pResOut is set to zero. The return value
39714** is set to SQLITE_OK unless an I/O error occurs during lock checking.
39715*/
39716static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
39717 unixFile *pFile = (unixFile*)id;
39718 int rc = proxyTakeConch(pFile);
39719 if( rc==SQLITE_OK ){
39720 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39721 if( pCtx->conchHeld>0 ){
39722 unixFile *proxy = pCtx->lockProxy;
39723 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
39724 }else{ /* conchHeld < 0 is lockless */
39725 pResOut=0;
39726 }
39727 }
39728 return rc;
39729}
39730
39731/*
39732** Lock the file with the lock specified by parameter eFileLock - one
39733** of the following:
39734**
39735** (1) SHARED_LOCK
39736** (2) RESERVED_LOCK
39737** (3) PENDING_LOCK
39738** (4) EXCLUSIVE_LOCK
39739**
39740** Sometimes when requesting one lock state, additional lock states
39741** are inserted in between. The locking might fail on one of the later
39742** transitions leaving the lock state different from what it started but
39743** still short of its goal. The following chart shows the allowed
39744** transitions and the inserted intermediate states:
39745**
39746** UNLOCKED -> SHARED
39747** SHARED -> RESERVED
39748** SHARED -> (PENDING) -> EXCLUSIVE
39749** RESERVED -> (PENDING) -> EXCLUSIVE
39750** PENDING -> EXCLUSIVE
39751**
39752** This routine will only increase a lock. Use the sqlite3OsUnlock()
39753** routine to lower a locking level.
39754*/
39755static int proxyLock(sqlite3_file *id, int eFileLock) {
39756 unixFile *pFile = (unixFile*)id;
39757 int rc = proxyTakeConch(pFile);
39758 if( rc==SQLITE_OK ){
39759 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39760 if( pCtx->conchHeld>0 ){
39761 unixFile *proxy = pCtx->lockProxy;
39762 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
39763 pFile->eFileLock = proxy->eFileLock;
39764 }else{
39765 /* conchHeld < 0 is lockless */
39766 }
39767 }
39768 return rc;
39769}
39770
39771
39772/*
39773** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
39774** must be either NO_LOCK or SHARED_LOCK.
39775**
39776** If the locking level of the file descriptor is already at or below
39777** the requested locking level, this routine is a no-op.
39778*/
39779static int proxyUnlock(sqlite3_file *id, int eFileLock) {
39780 unixFile *pFile = (unixFile*)id;
39781 int rc = proxyTakeConch(pFile);
39782 if( rc==SQLITE_OK ){
39783 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39784 if( pCtx->conchHeld>0 ){
39785 unixFile *proxy = pCtx->lockProxy;
39786 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
39787 pFile->eFileLock = proxy->eFileLock;
39788 }else{
39789 /* conchHeld < 0 is lockless */
39790 }
39791 }
39792 return rc;
39793}
39794
39795/*
39796** Close a file that uses proxy locks.
39797*/
39798static int proxyClose(sqlite3_file *id) {
39799 if( ALWAYS(id) ){
39800 unixFile *pFile = (unixFile*)id;
39801 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39802 unixFile *lockProxy = pCtx->lockProxy;
39803 unixFile *conchFile = pCtx->conchFile;
39804 int rc = SQLITE_OK;
39805
39806 if( lockProxy ){
39807 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
39808 if( rc ) return rc;
39809 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
39810 if( rc ) return rc;
39811 sqlite3_free(lockProxy);
39812 pCtx->lockProxy = 0;
39813 }
39814 if( conchFile ){
39815 if( pCtx->conchHeld ){
39816 rc = proxyReleaseConch(pFile);
39817 if( rc ) return rc;
39818 }
39819 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
39820 if( rc ) return rc;
39821 sqlite3_free(conchFile);
39822 }
39823 sqlite3DbFree(0, pCtx->lockProxyPath);
39824 sqlite3_free(pCtx->conchFilePath);
39825 sqlite3DbFree(0, pCtx->dbPath);
39826 /* restore the original locking context and pMethod then close it */
39827 pFile->lockingContext = pCtx->oldLockingContext;
39828 pFile->pMethod = pCtx->pOldMethod;
39829 sqlite3_free(pCtx);
39830 return pFile->pMethod->xClose(id);
39831 }
39832 return SQLITE_OK;
39833}
39834
39835
39836
39837#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
39838/*
39839** The proxy locking style is intended for use with AFP filesystems.
39840** And since AFP is only supported on MacOSX, the proxy locking is also
39841** restricted to MacOSX.
39842**
39843**
39844******************* End of the proxy lock implementation **********************
39845******************************************************************************/
39846
39847/*
39848** Initialize the operating system interface.
39849**
39850** This routine registers all VFS implementations for unix-like operating
39851** systems. This routine, and the sqlite3_os_end() routine that follows,
39852** should be the only routines in this file that are visible from other
39853** files.
39854**
39855** This routine is called once during SQLite initialization and by a
39856** single thread. The memory allocation and mutex subsystems have not
39857** necessarily been initialized when this routine is called, and so they
39858** should not be used.
39859*/
39860SQLITE_API int sqlite3_os_init(void){
39861 /*
39862 ** The following macro defines an initializer for an sqlite3_vfs object.
39863 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
39864 ** to the "finder" function. (pAppData is a pointer to a pointer because
39865 ** silly C90 rules prohibit a void* from being cast to a function pointer
39866 ** and so we have to go through the intermediate pointer to avoid problems
39867 ** when compiling with -pedantic-errors on GCC.)
39868 **
39869 ** The FINDER parameter to this macro is the name of the pointer to the
39870 ** finder-function. The finder-function returns a pointer to the
39871 ** sqlite_io_methods object that implements the desired locking
39872 ** behaviors. See the division above that contains the IOMETHODS
39873 ** macro for addition information on finder-functions.
39874 **
39875 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
39876 ** object. But the "autolockIoFinder" available on MacOSX does a little
39877 ** more than that; it looks at the filesystem type that hosts the
39878 ** database file and tries to choose an locking method appropriate for
39879 ** that filesystem time.
39880 */
39881 #define UNIXVFS(VFSNAME, FINDER) { \
39882 3, /* iVersion */ \
39883 sizeof(unixFile), /* szOsFile */ \
39884 MAX_PATHNAME, /* mxPathname */ \
39885 0, /* pNext */ \
39886 VFSNAME, /* zName */ \
39887 (void*)&FINDER, /* pAppData */ \
39888 unixOpen, /* xOpen */ \
39889 unixDelete, /* xDelete */ \
39890 unixAccess, /* xAccess */ \
39891 unixFullPathname, /* xFullPathname */ \
39892 unixDlOpen, /* xDlOpen */ \
39893 unixDlError, /* xDlError */ \
39894 unixDlSym, /* xDlSym */ \
39895 unixDlClose, /* xDlClose */ \
39896 unixRandomness, /* xRandomness */ \
39897 unixSleep, /* xSleep */ \
39898 unixCurrentTime, /* xCurrentTime */ \
39899 unixGetLastError, /* xGetLastError */ \
39900 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
39901 unixSetSystemCall, /* xSetSystemCall */ \
39902 unixGetSystemCall, /* xGetSystemCall */ \
39903 unixNextSystemCall, /* xNextSystemCall */ \
39904 }
39905
39906 /*
39907 ** All default VFSes for unix are contained in the following array.
39908 **
39909 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
39910 ** by the SQLite core when the VFS is registered. So the following
39911 ** array cannot be const.
39912 */
39913 static sqlite3_vfs aVfs[] = {
39914#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
39915 UNIXVFS("unix", autolockIoFinder ),
39916#elif OS_VXWORKS
39917 UNIXVFS("unix", vxworksIoFinder ),
39918#else
39919 UNIXVFS("unix", posixIoFinder ),
39920#endif
39921 UNIXVFS("unix-none", nolockIoFinder ),
39922 UNIXVFS("unix-dotfile", dotlockIoFinder ),
39923 UNIXVFS("unix-excl", posixIoFinder ),
39924#if OS_VXWORKS
39925 UNIXVFS("unix-namedsem", semIoFinder ),
39926#endif
39927#if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
39928 UNIXVFS("unix-posix", posixIoFinder ),
39929#endif
39930#if SQLITE_ENABLE_LOCKING_STYLE
39931 UNIXVFS("unix-flock", flockIoFinder ),
39932#endif
39933#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
39934 UNIXVFS("unix-afp", afpIoFinder ),
39935 UNIXVFS("unix-nfs", nfsIoFinder ),
39936 UNIXVFS("unix-proxy", proxyIoFinder ),
39937#endif
39938 };
39939 unsigned int i; /* Loop counter */
39940
39941 /* Double-check that the aSyscall[] array has been constructed
39942 ** correctly. See ticket [bb3a86e890c8e96ab] */
39943 assert( ArraySize(aSyscall)==29 );
39944
39945 /* Register all VFSes defined in the aVfs[] array */
39946 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
39947 sqlite3_vfs_register(&aVfs[i], i==0);
39948 }
39949 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
39950 return SQLITE_OK;
39951}
39952
39953/*
39954** Shutdown the operating system interface.
39955**
39956** Some operating systems might need to do some cleanup in this routine,
39957** to release dynamically allocated objects. But not on unix.
39958** This routine is a no-op for unix.
39959*/
39960SQLITE_API int sqlite3_os_end(void){
39961 unixBigLock = 0;
39962 return SQLITE_OK;
39963}
39964
39965#endif /* SQLITE_OS_UNIX */
39966
39967/************** End of os_unix.c *********************************************/
39968/************** Begin file os_win.c ******************************************/
39969/*
39970** 2004 May 22
39971**
39972** The author disclaims copyright to this source code. In place of
39973** a legal notice, here is a blessing:
39974**
39975** May you do good and not evil.
39976** May you find forgiveness for yourself and forgive others.
39977** May you share freely, never taking more than you give.
39978**
39979******************************************************************************
39980**
39981** This file contains code that is specific to Windows.
39982*/
39983/* #include "sqliteInt.h" */
39984#if SQLITE_OS_WIN /* This file is used for Windows only */
39985
39986/*
39987** Include code that is common to all os_*.c files
39988*/
39989/************** Include os_common.h in the middle of os_win.c ****************/
39990/************** Begin file os_common.h ***************************************/
39991/*
39992** 2004 May 22
39993**
39994** The author disclaims copyright to this source code. In place of
39995** a legal notice, here is a blessing:
39996**
39997** May you do good and not evil.
39998** May you find forgiveness for yourself and forgive others.
39999** May you share freely, never taking more than you give.
40000**
40001******************************************************************************
40002**
40003** This file contains macros and a little bit of code that is common to
40004** all of the platform-specific files (os_*.c) and is #included into those
40005** files.
40006**
40007** This file should be #included by the os_*.c files only. It is not a
40008** general purpose header file.
40009*/
40010#ifndef _OS_COMMON_H_
40011#define _OS_COMMON_H_
40012
40013/*
40014** At least two bugs have slipped in because we changed the MEMORY_DEBUG
40015** macro to SQLITE_DEBUG and some older makefiles have not yet made the
40016** switch. The following code should catch this problem at compile-time.
40017*/
40018#ifdef MEMORY_DEBUG
40019# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
40020#endif
40021
40022/*
40023** Macros for performance tracing. Normally turned off. Only works
40024** on i486 hardware.
40025*/
40026#ifdef SQLITE_PERFORMANCE_TRACE
40027
40028/*
40029** hwtime.h contains inline assembler code for implementing
40030** high-performance timing routines.
40031*/
40032/************** Include hwtime.h in the middle of os_common.h ****************/
40033/************** Begin file hwtime.h ******************************************/
40034/*
40035** 2008 May 27
40036**
40037** The author disclaims copyright to this source code. In place of
40038** a legal notice, here is a blessing:
40039**
40040** May you do good and not evil.
40041** May you find forgiveness for yourself and forgive others.
40042** May you share freely, never taking more than you give.
40043**
40044******************************************************************************
40045**
40046** This file contains inline asm code for retrieving "high-performance"
40047** counters for x86 class CPUs.
40048*/
40049#ifndef SQLITE_HWTIME_H
40050#define SQLITE_HWTIME_H
40051
40052/*
40053** The following routine only works on pentium-class (or newer) processors.
40054** It uses the RDTSC opcode to read the cycle count value out of the
40055** processor and returns that value. This can be used for high-res
40056** profiling.
40057*/
40058#if (defined(__GNUC__) || defined(_MSC_VER)) && \
40059 (defined(i386) || defined(__i386__) || defined(_M_IX86))
40060
40061 #if defined(__GNUC__)
40062
40063 __inline__ sqlite_uint64 sqlite3Hwtime(void){
40064 unsigned int lo, hi;
40065 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
40066 return (sqlite_uint64)hi << 32 | lo;
40067 }
40068
40069 #elif defined(_MSC_VER)
40070
40071 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
40072 __asm {
40073 rdtsc
40074 ret ; return value at EDX:EAX
40075 }
40076 }
40077
40078 #endif
40079
40080#elif (defined(__GNUC__) && defined(__x86_64__))
40081
40082 __inline__ sqlite_uint64 sqlite3Hwtime(void){
40083 unsigned long val;
40084 __asm__ __volatile__ ("rdtsc" : "=A" (val));
40085 return val;
40086 }
40087
40088#elif (defined(__GNUC__) && defined(__ppc__))
40089
40090 __inline__ sqlite_uint64 sqlite3Hwtime(void){
40091 unsigned long long retval;
40092 unsigned long junk;
40093 __asm__ __volatile__ ("\n\
40094 1: mftbu %1\n\
40095 mftb %L0\n\
40096 mftbu %0\n\
40097 cmpw %0,%1\n\
40098 bne 1b"
40099 : "=r" (retval), "=r" (junk));
40100 return retval;
40101 }
40102
40103#else
40104
40105 #error Need implementation of sqlite3Hwtime() for your platform.
40106
40107 /*
40108 ** To compile without implementing sqlite3Hwtime() for your platform,
40109 ** you can remove the above #error and use the following
40110 ** stub function. You will lose timing support for many
40111 ** of the debugging and testing utilities, but it should at
40112 ** least compile and run.
40113 */
40114SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
40115
40116#endif
40117
40118#endif /* !defined(SQLITE_HWTIME_H) */
40119
40120/************** End of hwtime.h **********************************************/
40121/************** Continuing where we left off in os_common.h ******************/
40122
40123static sqlite_uint64 g_start;
40124static sqlite_uint64 g_elapsed;
40125#define TIMER_START g_start=sqlite3Hwtime()
40126#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
40127#define TIMER_ELAPSED g_elapsed
40128#else
40129#define TIMER_START
40130#define TIMER_END
40131#define TIMER_ELAPSED ((sqlite_uint64)0)
40132#endif
40133
40134/*
40135** If we compile with the SQLITE_TEST macro set, then the following block
40136** of code will give us the ability to simulate a disk I/O error. This
40137** is used for testing the I/O recovery logic.
40138*/
40139#if defined(SQLITE_TEST)
40140SQLITE_API extern int sqlite3_io_error_hit;
40141SQLITE_API extern int sqlite3_io_error_hardhit;
40142SQLITE_API extern int sqlite3_io_error_pending;
40143SQLITE_API extern int sqlite3_io_error_persist;
40144SQLITE_API extern int sqlite3_io_error_benign;
40145SQLITE_API extern int sqlite3_diskfull_pending;
40146SQLITE_API extern int sqlite3_diskfull;
40147#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
40148#define SimulateIOError(CODE) \
40149 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
40150 || sqlite3_io_error_pending-- == 1 ) \
40151 { local_ioerr(); CODE; }
40152static void local_ioerr(){
40153 IOTRACE(("IOERR\n"));
40154 sqlite3_io_error_hit++;
40155 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
40156}
40157#define SimulateDiskfullError(CODE) \
40158 if( sqlite3_diskfull_pending ){ \
40159 if( sqlite3_diskfull_pending == 1 ){ \
40160 local_ioerr(); \
40161 sqlite3_diskfull = 1; \
40162 sqlite3_io_error_hit = 1; \
40163 CODE; \
40164 }else{ \
40165 sqlite3_diskfull_pending--; \
40166 } \
40167 }
40168#else
40169#define SimulateIOErrorBenign(X)
40170#define SimulateIOError(A)
40171#define SimulateDiskfullError(A)
40172#endif /* defined(SQLITE_TEST) */
40173
40174/*
40175** When testing, keep a count of the number of open files.
40176*/
40177#if defined(SQLITE_TEST)
40178SQLITE_API extern int sqlite3_open_file_count;
40179#define OpenCounter(X) sqlite3_open_file_count+=(X)
40180#else
40181#define OpenCounter(X)
40182#endif /* defined(SQLITE_TEST) */
40183
40184#endif /* !defined(_OS_COMMON_H_) */
40185
40186/************** End of os_common.h *******************************************/
40187/************** Continuing where we left off in os_win.c *********************/
40188
40189/*
40190** Include the header file for the Windows VFS.
40191*/
40192/* #include "os_win.h" */
40193
40194/*
40195** Compiling and using WAL mode requires several APIs that are only
40196** available in Windows platforms based on the NT kernel.
40197*/
40198#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
40199# error "WAL mode requires support from the Windows NT kernel, compile\
40200 with SQLITE_OMIT_WAL."
40201#endif
40202
40203#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0
40204# error "Memory mapped files require support from the Windows NT kernel,\
40205 compile with SQLITE_MAX_MMAP_SIZE=0."
40206#endif
40207
40208/*
40209** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
40210** based on the sub-platform)?
40211*/
40212#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
40213# define SQLITE_WIN32_HAS_ANSI
40214#endif
40215
40216/*
40217** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
40218** based on the sub-platform)?
40219*/
40220#if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
40221 !defined(SQLITE_WIN32_NO_WIDE)
40222# define SQLITE_WIN32_HAS_WIDE
40223#endif
40224
40225/*
40226** Make sure at least one set of Win32 APIs is available.
40227*/
40228#if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
40229# error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
40230 must be defined."
40231#endif
40232
40233/*
40234** Define the required Windows SDK version constants if they are not
40235** already available.
40236*/
40237#ifndef NTDDI_WIN8
40238# define NTDDI_WIN8 0x06020000
40239#endif
40240
40241#ifndef NTDDI_WINBLUE
40242# define NTDDI_WINBLUE 0x06030000
40243#endif
40244
40245#ifndef NTDDI_WINTHRESHOLD
40246# define NTDDI_WINTHRESHOLD 0x06040000
40247#endif
40248
40249/*
40250** Check to see if the GetVersionEx[AW] functions are deprecated on the
40251** target system. GetVersionEx was first deprecated in Win8.1.
40252*/
40253#ifndef SQLITE_WIN32_GETVERSIONEX
40254# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
40255# define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
40256# else
40257# define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
40258# endif
40259#endif
40260
40261/*
40262** Check to see if the CreateFileMappingA function is supported on the
40263** target system. It is unavailable when using "mincore.lib" on Win10.
40264** When compiling for Windows 10, always assume "mincore.lib" is in use.
40265*/
40266#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
40267# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
40268# define SQLITE_WIN32_CREATEFILEMAPPINGA 0
40269# else
40270# define SQLITE_WIN32_CREATEFILEMAPPINGA 1
40271# endif
40272#endif
40273
40274/*
40275** This constant should already be defined (in the "WinDef.h" SDK file).
40276*/
40277#ifndef MAX_PATH
40278# define MAX_PATH (260)
40279#endif
40280
40281/*
40282** Maximum pathname length (in chars) for Win32. This should normally be
40283** MAX_PATH.
40284*/
40285#ifndef SQLITE_WIN32_MAX_PATH_CHARS
40286# define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
40287#endif
40288
40289/*
40290** This constant should already be defined (in the "WinNT.h" SDK file).
40291*/
40292#ifndef UNICODE_STRING_MAX_CHARS
40293# define UNICODE_STRING_MAX_CHARS (32767)
40294#endif
40295
40296/*
40297** Maximum pathname length (in chars) for WinNT. This should normally be
40298** UNICODE_STRING_MAX_CHARS.
40299*/
40300#ifndef SQLITE_WINNT_MAX_PATH_CHARS
40301# define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS)
40302#endif
40303
40304/*
40305** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
40306** characters, so we allocate 4 bytes per character assuming worst-case of
40307** 4-bytes-per-character for UTF8.
40308*/
40309#ifndef SQLITE_WIN32_MAX_PATH_BYTES
40310# define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
40311#endif
40312
40313/*
40314** Maximum pathname length (in bytes) for WinNT. This should normally be
40315** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
40316*/
40317#ifndef SQLITE_WINNT_MAX_PATH_BYTES
40318# define SQLITE_WINNT_MAX_PATH_BYTES \
40319 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
40320#endif
40321
40322/*
40323** Maximum error message length (in chars) for WinRT.
40324*/
40325#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
40326# define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
40327#endif
40328
40329/*
40330** Returns non-zero if the character should be treated as a directory
40331** separator.
40332*/
40333#ifndef winIsDirSep
40334# define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
40335#endif
40336
40337/*
40338** This macro is used when a local variable is set to a value that is
40339** [sometimes] not used by the code (e.g. via conditional compilation).
40340*/
40341#ifndef UNUSED_VARIABLE_VALUE
40342# define UNUSED_VARIABLE_VALUE(x) (void)(x)
40343#endif
40344
40345/*
40346** Returns the character that should be used as the directory separator.
40347*/
40348#ifndef winGetDirSep
40349# define winGetDirSep() '\\'
40350#endif
40351
40352/*
40353** Do we need to manually define the Win32 file mapping APIs for use with WAL
40354** mode or memory mapped files (e.g. these APIs are available in the Windows
40355** CE SDK; however, they are not present in the header file)?
40356*/
40357#if SQLITE_WIN32_FILEMAPPING_API && \
40358 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
40359/*
40360** Two of the file mapping APIs are different under WinRT. Figure out which
40361** set we need.
40362*/
40363#if SQLITE_OS_WINRT
40364WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
40365 LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
40366
40367WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
40368#else
40369#if defined(SQLITE_WIN32_HAS_ANSI)
40370WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
40371 DWORD, DWORD, DWORD, LPCSTR);
40372#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
40373
40374#if defined(SQLITE_WIN32_HAS_WIDE)
40375WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
40376 DWORD, DWORD, DWORD, LPCWSTR);
40377#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
40378
40379WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
40380#endif /* SQLITE_OS_WINRT */
40381
40382/*
40383** These file mapping APIs are common to both Win32 and WinRT.
40384*/
40385
40386WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
40387WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
40388#endif /* SQLITE_WIN32_FILEMAPPING_API */
40389
40390/*
40391** Some Microsoft compilers lack this definition.
40392*/
40393#ifndef INVALID_FILE_ATTRIBUTES
40394# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
40395#endif
40396
40397#ifndef FILE_FLAG_MASK
40398# define FILE_FLAG_MASK (0xFF3C0000)
40399#endif
40400
40401#ifndef FILE_ATTRIBUTE_MASK
40402# define FILE_ATTRIBUTE_MASK (0x0003FFF7)
40403#endif
40404
40405#ifndef SQLITE_OMIT_WAL
40406/* Forward references to structures used for WAL */
40407typedef struct winShm winShm; /* A connection to shared-memory */
40408typedef struct winShmNode winShmNode; /* A region of shared-memory */
40409#endif
40410
40411/*
40412** WinCE lacks native support for file locking so we have to fake it
40413** with some code of our own.
40414*/
40415#if SQLITE_OS_WINCE
40416typedef struct winceLock {
40417 int nReaders; /* Number of reader locks obtained */
40418 BOOL bPending; /* Indicates a pending lock has been obtained */
40419 BOOL bReserved; /* Indicates a reserved lock has been obtained */
40420 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
40421} winceLock;
40422#endif
40423
40424/*
40425** The winFile structure is a subclass of sqlite3_file* specific to the win32
40426** portability layer.
40427*/
40428typedef struct winFile winFile;
40429struct winFile {
40430 const sqlite3_io_methods *pMethod; /*** Must be first ***/
40431 sqlite3_vfs *pVfs; /* The VFS used to open this file */
40432 HANDLE h; /* Handle for accessing the file */
40433 u8 locktype; /* Type of lock currently held on this file */
40434 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
40435 u8 ctrlFlags; /* Flags. See WINFILE_* below */
40436 DWORD lastErrno; /* The Windows errno from the last I/O error */
40437#ifndef SQLITE_OMIT_WAL
40438 winShm *pShm; /* Instance of shared memory on this file */
40439#endif
40440 const char *zPath; /* Full pathname of this file */
40441 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
40442#if SQLITE_OS_WINCE
40443 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
40444 HANDLE hMutex; /* Mutex used to control access to shared lock */
40445 HANDLE hShared; /* Shared memory segment used for locking */
40446 winceLock local; /* Locks obtained by this instance of winFile */
40447 winceLock *shared; /* Global shared lock memory for the file */
40448#endif
40449#if SQLITE_MAX_MMAP_SIZE>0
40450 int nFetchOut; /* Number of outstanding xFetch references */
40451 HANDLE hMap; /* Handle for accessing memory mapping */
40452 void *pMapRegion; /* Area memory mapped */
40453 sqlite3_int64 mmapSize; /* Usable size of mapped region */
40454 sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
40455 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
40456#endif
40457};
40458
40459/*
40460** The winVfsAppData structure is used for the pAppData member for all of the
40461** Win32 VFS variants.
40462*/
40463typedef struct winVfsAppData winVfsAppData;
40464struct winVfsAppData {
40465 const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
40466 void *pAppData; /* The extra pAppData, if any. */
40467 BOOL bNoLock; /* Non-zero if locking is disabled. */
40468};
40469
40470/*
40471** Allowed values for winFile.ctrlFlags
40472*/
40473#define WINFILE_RDONLY 0x02 /* Connection is read only */
40474#define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
40475#define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
40476
40477/*
40478 * The size of the buffer used by sqlite3_win32_write_debug().
40479 */
40480#ifndef SQLITE_WIN32_DBG_BUF_SIZE
40481# define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
40482#endif
40483
40484/*
40485 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
40486 * various Win32 API heap functions instead of our own.
40487 */
40488#ifdef SQLITE_WIN32_MALLOC
40489
40490/*
40491 * If this is non-zero, an isolated heap will be created by the native Win32
40492 * allocator subsystem; otherwise, the default process heap will be used. This
40493 * setting has no effect when compiling for WinRT. By default, this is enabled
40494 * and an isolated heap will be created to store all allocated data.
40495 *
40496 ******************************************************************************
40497 * WARNING: It is important to note that when this setting is non-zero and the
40498 * winMemShutdown function is called (e.g. by the sqlite3_shutdown
40499 * function), all data that was allocated using the isolated heap will
40500 * be freed immediately and any attempt to access any of that freed
40501 * data will almost certainly result in an immediate access violation.
40502 ******************************************************************************
40503 */
40504#ifndef SQLITE_WIN32_HEAP_CREATE
40505# define SQLITE_WIN32_HEAP_CREATE (TRUE)
40506#endif
40507
40508/*
40509 * This is the maximum possible initial size of the Win32-specific heap, in
40510 * bytes.
40511 */
40512#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
40513# define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
40514#endif
40515
40516/*
40517 * This is the extra space for the initial size of the Win32-specific heap,
40518 * in bytes. This value may be zero.
40519 */
40520#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
40521# define SQLITE_WIN32_HEAP_INIT_EXTRA (4194304)
40522#endif
40523
40524/*
40525 * Calculate the maximum legal cache size, in pages, based on the maximum
40526 * possible initial heap size and the default page size, setting aside the
40527 * needed extra space.
40528 */
40529#ifndef SQLITE_WIN32_MAX_CACHE_SIZE
40530# define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
40531 (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
40532 (SQLITE_DEFAULT_PAGE_SIZE))
40533#endif
40534
40535/*
40536 * This is cache size used in the calculation of the initial size of the
40537 * Win32-specific heap. It cannot be negative.
40538 */
40539#ifndef SQLITE_WIN32_CACHE_SIZE
40540# if SQLITE_DEFAULT_CACHE_SIZE>=0
40541# define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE)
40542# else
40543# define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
40544# endif
40545#endif
40546
40547/*
40548 * Make sure that the calculated cache size, in pages, cannot cause the
40549 * initial size of the Win32-specific heap to exceed the maximum amount
40550 * of memory that can be specified in the call to HeapCreate.
40551 */
40552#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
40553# undef SQLITE_WIN32_CACHE_SIZE
40554# define SQLITE_WIN32_CACHE_SIZE (2000)
40555#endif
40556
40557/*
40558 * The initial size of the Win32-specific heap. This value may be zero.
40559 */
40560#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
40561# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
40562 (SQLITE_DEFAULT_PAGE_SIZE) + \
40563 (SQLITE_WIN32_HEAP_INIT_EXTRA))
40564#endif
40565
40566/*
40567 * The maximum size of the Win32-specific heap. This value may be zero.
40568 */
40569#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
40570# define SQLITE_WIN32_HEAP_MAX_SIZE (0)
40571#endif
40572
40573/*
40574 * The extra flags to use in calls to the Win32 heap APIs. This value may be
40575 * zero for the default behavior.
40576 */
40577#ifndef SQLITE_WIN32_HEAP_FLAGS
40578# define SQLITE_WIN32_HEAP_FLAGS (0)
40579#endif
40580
40581
40582/*
40583** The winMemData structure stores information required by the Win32-specific
40584** sqlite3_mem_methods implementation.
40585*/
40586typedef struct winMemData winMemData;
40587struct winMemData {
40588#ifndef NDEBUG
40589 u32 magic1; /* Magic number to detect structure corruption. */
40590#endif
40591 HANDLE hHeap; /* The handle to our heap. */
40592 BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
40593#ifndef NDEBUG
40594 u32 magic2; /* Magic number to detect structure corruption. */
40595#endif
40596};
40597
40598#ifndef NDEBUG
40599#define WINMEM_MAGIC1 0x42b2830b
40600#define WINMEM_MAGIC2 0xbd4d7cf4
40601#endif
40602
40603static struct winMemData win_mem_data = {
40604#ifndef NDEBUG
40605 WINMEM_MAGIC1,
40606#endif
40607 NULL, FALSE
40608#ifndef NDEBUG
40609 ,WINMEM_MAGIC2
40610#endif
40611};
40612
40613#ifndef NDEBUG
40614#define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
40615#define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
40616#define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2();
40617#else
40618#define winMemAssertMagic()
40619#endif
40620
40621#define winMemGetDataPtr() &win_mem_data
40622#define winMemGetHeap() win_mem_data.hHeap
40623#define winMemGetOwned() win_mem_data.bOwned
40624
40625static void *winMemMalloc(int nBytes);
40626static void winMemFree(void *pPrior);
40627static void *winMemRealloc(void *pPrior, int nBytes);
40628static int winMemSize(void *p);
40629static int winMemRoundup(int n);
40630static int winMemInit(void *pAppData);
40631static void winMemShutdown(void *pAppData);
40632
40633SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
40634#endif /* SQLITE_WIN32_MALLOC */
40635
40636/*
40637** The following variable is (normally) set once and never changes
40638** thereafter. It records whether the operating system is Win9x
40639** or WinNT.
40640**
40641** 0: Operating system unknown.
40642** 1: Operating system is Win9x.
40643** 2: Operating system is WinNT.
40644**
40645** In order to facilitate testing on a WinNT system, the test fixture
40646** can manually set this value to 1 to emulate Win98 behavior.
40647*/
40648#ifdef SQLITE_TEST
40649SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
40650#else
40651static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
40652#endif
40653
40654#ifndef SYSCALL
40655# define SYSCALL sqlite3_syscall_ptr
40656#endif
40657
40658/*
40659** This function is not available on Windows CE or WinRT.
40660 */
40661
40662#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
40663# define osAreFileApisANSI() 1
40664#endif
40665
40666/*
40667** Many system calls are accessed through pointer-to-functions so that
40668** they may be overridden at runtime to facilitate fault injection during
40669** testing and sandboxing. The following array holds the names and pointers
40670** to all overrideable system calls.
40671*/
40672static struct win_syscall {
40673 const char *zName; /* Name of the system call */
40674 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
40675 sqlite3_syscall_ptr pDefault; /* Default value */
40676} aSyscall[] = {
40677#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
40678 { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
40679#else
40680 { "AreFileApisANSI", (SYSCALL)0, 0 },
40681#endif
40682
40683#ifndef osAreFileApisANSI
40684#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
40685#endif
40686
40687#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
40688 { "CharLowerW", (SYSCALL)CharLowerW, 0 },
40689#else
40690 { "CharLowerW", (SYSCALL)0, 0 },
40691#endif
40692
40693#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
40694
40695#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
40696 { "CharUpperW", (SYSCALL)CharUpperW, 0 },
40697#else
40698 { "CharUpperW", (SYSCALL)0, 0 },
40699#endif
40700
40701#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
40702
40703 { "CloseHandle", (SYSCALL)CloseHandle, 0 },
40704
40705#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
40706
40707#if defined(SQLITE_WIN32_HAS_ANSI)
40708 { "CreateFileA", (SYSCALL)CreateFileA, 0 },
40709#else
40710 { "CreateFileA", (SYSCALL)0, 0 },
40711#endif
40712
40713#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
40714 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
40715
40716#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
40717 { "CreateFileW", (SYSCALL)CreateFileW, 0 },
40718#else
40719 { "CreateFileW", (SYSCALL)0, 0 },
40720#endif
40721
40722#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
40723 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
40724
40725#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
40726 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \
40727 SQLITE_WIN32_CREATEFILEMAPPINGA
40728 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
40729#else
40730 { "CreateFileMappingA", (SYSCALL)0, 0 },
40731#endif
40732
40733#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
40734 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
40735
40736#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
40737 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
40738 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
40739#else
40740 { "CreateFileMappingW", (SYSCALL)0, 0 },
40741#endif
40742
40743#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
40744 DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
40745
40746#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
40747 { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
40748#else
40749 { "CreateMutexW", (SYSCALL)0, 0 },
40750#endif
40751
40752#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
40753 LPCWSTR))aSyscall[8].pCurrent)
40754
40755#if defined(SQLITE_WIN32_HAS_ANSI)
40756 { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
40757#else
40758 { "DeleteFileA", (SYSCALL)0, 0 },
40759#endif
40760
40761#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
40762
40763#if defined(SQLITE_WIN32_HAS_WIDE)
40764 { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
40765#else
40766 { "DeleteFileW", (SYSCALL)0, 0 },
40767#endif
40768
40769#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
40770
40771#if SQLITE_OS_WINCE
40772 { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
40773#else
40774 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
40775#endif
40776
40777#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
40778 LPFILETIME))aSyscall[11].pCurrent)
40779
40780#if SQLITE_OS_WINCE
40781 { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
40782#else
40783 { "FileTimeToSystemTime", (SYSCALL)0, 0 },
40784#endif
40785
40786#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
40787 LPSYSTEMTIME))aSyscall[12].pCurrent)
40788
40789 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
40790
40791#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
40792
40793#if defined(SQLITE_WIN32_HAS_ANSI)
40794 { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
40795#else
40796 { "FormatMessageA", (SYSCALL)0, 0 },
40797#endif
40798
40799#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
40800 DWORD,va_list*))aSyscall[14].pCurrent)
40801
40802#if defined(SQLITE_WIN32_HAS_WIDE)
40803 { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
40804#else
40805 { "FormatMessageW", (SYSCALL)0, 0 },
40806#endif
40807
40808#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
40809 DWORD,va_list*))aSyscall[15].pCurrent)
40810
40811#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
40812 { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
40813#else
40814 { "FreeLibrary", (SYSCALL)0, 0 },
40815#endif
40816
40817#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
40818
40819 { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
40820
40821#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
40822
40823#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
40824 { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
40825#else
40826 { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
40827#endif
40828
40829#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
40830 LPDWORD))aSyscall[18].pCurrent)
40831
40832#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
40833 { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
40834#else
40835 { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
40836#endif
40837
40838#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
40839 LPDWORD))aSyscall[19].pCurrent)
40840
40841#if defined(SQLITE_WIN32_HAS_ANSI)
40842 { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
40843#else
40844 { "GetFileAttributesA", (SYSCALL)0, 0 },
40845#endif
40846
40847#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
40848
40849#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
40850 { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
40851#else
40852 { "GetFileAttributesW", (SYSCALL)0, 0 },
40853#endif
40854
40855#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
40856
40857#if defined(SQLITE_WIN32_HAS_WIDE)
40858 { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
40859#else
40860 { "GetFileAttributesExW", (SYSCALL)0, 0 },
40861#endif
40862
40863#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
40864 LPVOID))aSyscall[22].pCurrent)
40865
40866#if !SQLITE_OS_WINRT
40867 { "GetFileSize", (SYSCALL)GetFileSize, 0 },
40868#else
40869 { "GetFileSize", (SYSCALL)0, 0 },
40870#endif
40871
40872#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
40873
40874#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
40875 { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
40876#else
40877 { "GetFullPathNameA", (SYSCALL)0, 0 },
40878#endif
40879
40880#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
40881 LPSTR*))aSyscall[24].pCurrent)
40882
40883#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
40884 { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
40885#else
40886 { "GetFullPathNameW", (SYSCALL)0, 0 },
40887#endif
40888
40889#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
40890 LPWSTR*))aSyscall[25].pCurrent)
40891
40892 { "GetLastError", (SYSCALL)GetLastError, 0 },
40893
40894#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
40895
40896#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
40897#if SQLITE_OS_WINCE
40898 /* The GetProcAddressA() routine is only available on Windows CE. */
40899 { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
40900#else
40901 /* All other Windows platforms expect GetProcAddress() to take
40902 ** an ANSI string regardless of the _UNICODE setting */
40903 { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
40904#endif
40905#else
40906 { "GetProcAddressA", (SYSCALL)0, 0 },
40907#endif
40908
40909#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
40910 LPCSTR))aSyscall[27].pCurrent)
40911
40912#if !SQLITE_OS_WINRT
40913 { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
40914#else
40915 { "GetSystemInfo", (SYSCALL)0, 0 },
40916#endif
40917
40918#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
40919
40920 { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
40921
40922#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
40923
40924#if !SQLITE_OS_WINCE
40925 { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
40926#else
40927 { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
40928#endif
40929
40930#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
40931 LPFILETIME))aSyscall[30].pCurrent)
40932
40933#if defined(SQLITE_WIN32_HAS_ANSI)
40934 { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
40935#else
40936 { "GetTempPathA", (SYSCALL)0, 0 },
40937#endif
40938
40939#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
40940
40941#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
40942 { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
40943#else
40944 { "GetTempPathW", (SYSCALL)0, 0 },
40945#endif
40946
40947#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
40948
40949#if !SQLITE_OS_WINRT
40950 { "GetTickCount", (SYSCALL)GetTickCount, 0 },
40951#else
40952 { "GetTickCount", (SYSCALL)0, 0 },
40953#endif
40954
40955#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
40956
40957#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
40958 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
40959#else
40960 { "GetVersionExA", (SYSCALL)0, 0 },
40961#endif
40962
40963#define osGetVersionExA ((BOOL(WINAPI*)( \
40964 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
40965
40966#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
40967 SQLITE_WIN32_GETVERSIONEX
40968 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
40969#else
40970 { "GetVersionExW", (SYSCALL)0, 0 },
40971#endif
40972
40973#define osGetVersionExW ((BOOL(WINAPI*)( \
40974 LPOSVERSIONINFOW))aSyscall[35].pCurrent)
40975
40976 { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
40977
40978#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
40979 SIZE_T))aSyscall[36].pCurrent)
40980
40981#if !SQLITE_OS_WINRT
40982 { "HeapCreate", (SYSCALL)HeapCreate, 0 },
40983#else
40984 { "HeapCreate", (SYSCALL)0, 0 },
40985#endif
40986
40987#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
40988 SIZE_T))aSyscall[37].pCurrent)
40989
40990#if !SQLITE_OS_WINRT
40991 { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
40992#else
40993 { "HeapDestroy", (SYSCALL)0, 0 },
40994#endif
40995
40996#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
40997
40998 { "HeapFree", (SYSCALL)HeapFree, 0 },
40999
41000#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
41001
41002 { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
41003
41004#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
41005 SIZE_T))aSyscall[40].pCurrent)
41006
41007 { "HeapSize", (SYSCALL)HeapSize, 0 },
41008
41009#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
41010 LPCVOID))aSyscall[41].pCurrent)
41011
41012#if !SQLITE_OS_WINRT
41013 { "HeapValidate", (SYSCALL)HeapValidate, 0 },
41014#else
41015 { "HeapValidate", (SYSCALL)0, 0 },
41016#endif
41017
41018#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
41019 LPCVOID))aSyscall[42].pCurrent)
41020
41021#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
41022 { "HeapCompact", (SYSCALL)HeapCompact, 0 },
41023#else
41024 { "HeapCompact", (SYSCALL)0, 0 },
41025#endif
41026
41027#define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
41028
41029#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
41030 { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
41031#else
41032 { "LoadLibraryA", (SYSCALL)0, 0 },
41033#endif
41034
41035#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
41036
41037#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
41038 !defined(SQLITE_OMIT_LOAD_EXTENSION)
41039 { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
41040#else
41041 { "LoadLibraryW", (SYSCALL)0, 0 },
41042#endif
41043
41044#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
41045
41046#if !SQLITE_OS_WINRT
41047 { "LocalFree", (SYSCALL)LocalFree, 0 },
41048#else
41049 { "LocalFree", (SYSCALL)0, 0 },
41050#endif
41051
41052#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
41053
41054#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
41055 { "LockFile", (SYSCALL)LockFile, 0 },
41056#else
41057 { "LockFile", (SYSCALL)0, 0 },
41058#endif
41059
41060#ifndef osLockFile
41061#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
41062 DWORD))aSyscall[47].pCurrent)
41063#endif
41064
41065#if !SQLITE_OS_WINCE
41066 { "LockFileEx", (SYSCALL)LockFileEx, 0 },
41067#else
41068 { "LockFileEx", (SYSCALL)0, 0 },
41069#endif
41070
41071#ifndef osLockFileEx
41072#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
41073 LPOVERLAPPED))aSyscall[48].pCurrent)
41074#endif
41075
41076#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
41077 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
41078 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
41079#else
41080 { "MapViewOfFile", (SYSCALL)0, 0 },
41081#endif
41082
41083#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
41084 SIZE_T))aSyscall[49].pCurrent)
41085
41086 { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
41087
41088#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
41089 int))aSyscall[50].pCurrent)
41090
41091 { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
41092
41093#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
41094 LARGE_INTEGER*))aSyscall[51].pCurrent)
41095
41096 { "ReadFile", (SYSCALL)ReadFile, 0 },
41097
41098#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
41099 LPOVERLAPPED))aSyscall[52].pCurrent)
41100
41101 { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
41102
41103#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
41104
41105#if !SQLITE_OS_WINRT
41106 { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
41107#else
41108 { "SetFilePointer", (SYSCALL)0, 0 },
41109#endif
41110
41111#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
41112 DWORD))aSyscall[54].pCurrent)
41113
41114#if !SQLITE_OS_WINRT
41115 { "Sleep", (SYSCALL)Sleep, 0 },
41116#else
41117 { "Sleep", (SYSCALL)0, 0 },
41118#endif
41119
41120#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
41121
41122 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
41123
41124#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
41125 LPFILETIME))aSyscall[56].pCurrent)
41126
41127#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
41128 { "UnlockFile", (SYSCALL)UnlockFile, 0 },
41129#else
41130 { "UnlockFile", (SYSCALL)0, 0 },
41131#endif
41132
41133#ifndef osUnlockFile
41134#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
41135 DWORD))aSyscall[57].pCurrent)
41136#endif
41137
41138#if !SQLITE_OS_WINCE
41139 { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
41140#else
41141 { "UnlockFileEx", (SYSCALL)0, 0 },
41142#endif
41143
41144#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
41145 LPOVERLAPPED))aSyscall[58].pCurrent)
41146
41147#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
41148 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
41149#else
41150 { "UnmapViewOfFile", (SYSCALL)0, 0 },
41151#endif
41152
41153#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
41154
41155 { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
41156
41157#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
41158 LPCSTR,LPBOOL))aSyscall[60].pCurrent)
41159
41160 { "WriteFile", (SYSCALL)WriteFile, 0 },
41161
41162#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
41163 LPOVERLAPPED))aSyscall[61].pCurrent)
41164
41165#if SQLITE_OS_WINRT
41166 { "CreateEventExW", (SYSCALL)CreateEventExW, 0 },
41167#else
41168 { "CreateEventExW", (SYSCALL)0, 0 },
41169#endif
41170
41171#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
41172 DWORD,DWORD))aSyscall[62].pCurrent)
41173
41174#if !SQLITE_OS_WINRT
41175 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
41176#else
41177 { "WaitForSingleObject", (SYSCALL)0, 0 },
41178#endif
41179
41180#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
41181 DWORD))aSyscall[63].pCurrent)
41182
41183#if !SQLITE_OS_WINCE
41184 { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 },
41185#else
41186 { "WaitForSingleObjectEx", (SYSCALL)0, 0 },
41187#endif
41188
41189#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
41190 BOOL))aSyscall[64].pCurrent)
41191
41192#if SQLITE_OS_WINRT
41193 { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 },
41194#else
41195 { "SetFilePointerEx", (SYSCALL)0, 0 },
41196#endif
41197
41198#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
41199 PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
41200
41201#if SQLITE_OS_WINRT
41202 { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
41203#else
41204 { "GetFileInformationByHandleEx", (SYSCALL)0, 0 },
41205#endif
41206
41207#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
41208 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
41209
41210#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
41211 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
41212#else
41213 { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
41214#endif
41215
41216#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
41217 SIZE_T))aSyscall[67].pCurrent)
41218
41219#if SQLITE_OS_WINRT
41220 { "CreateFile2", (SYSCALL)CreateFile2, 0 },
41221#else
41222 { "CreateFile2", (SYSCALL)0, 0 },
41223#endif
41224
41225#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
41226 LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
41227
41228#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
41229 { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 },
41230#else
41231 { "LoadPackagedLibrary", (SYSCALL)0, 0 },
41232#endif
41233
41234#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
41235 DWORD))aSyscall[69].pCurrent)
41236
41237#if SQLITE_OS_WINRT
41238 { "GetTickCount64", (SYSCALL)GetTickCount64, 0 },
41239#else
41240 { "GetTickCount64", (SYSCALL)0, 0 },
41241#endif
41242
41243#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
41244
41245#if SQLITE_OS_WINRT
41246 { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 },
41247#else
41248 { "GetNativeSystemInfo", (SYSCALL)0, 0 },
41249#endif
41250
41251#define osGetNativeSystemInfo ((VOID(WINAPI*)( \
41252 LPSYSTEM_INFO))aSyscall[71].pCurrent)
41253
41254#if defined(SQLITE_WIN32_HAS_ANSI)
41255 { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 },
41256#else
41257 { "OutputDebugStringA", (SYSCALL)0, 0 },
41258#endif
41259
41260#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
41261
41262#if defined(SQLITE_WIN32_HAS_WIDE)
41263 { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 },
41264#else
41265 { "OutputDebugStringW", (SYSCALL)0, 0 },
41266#endif
41267
41268#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
41269
41270 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
41271
41272#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
41273
41274#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
41275 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
41276#else
41277 { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
41278#endif
41279
41280#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
41281 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
41282
41283/*
41284** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
41285** is really just a macro that uses a compiler intrinsic (e.g. x64).
41286** So do not try to make this is into a redefinable interface.
41287*/
41288#if defined(InterlockedCompareExchange)
41289 { "InterlockedCompareExchange", (SYSCALL)0, 0 },
41290
41291#define osInterlockedCompareExchange InterlockedCompareExchange
41292#else
41293 { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
41294
41295#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
41296 SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
41297#endif /* defined(InterlockedCompareExchange) */
41298
41299#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
41300 { "UuidCreate", (SYSCALL)UuidCreate, 0 },
41301#else
41302 { "UuidCreate", (SYSCALL)0, 0 },
41303#endif
41304
41305#define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
41306
41307#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
41308 { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 },
41309#else
41310 { "UuidCreateSequential", (SYSCALL)0, 0 },
41311#endif
41312
41313#define osUuidCreateSequential \
41314 ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
41315
41316#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0
41317 { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 },
41318#else
41319 { "FlushViewOfFile", (SYSCALL)0, 0 },
41320#endif
41321
41322#define osFlushViewOfFile \
41323 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
41324
41325}; /* End of the overrideable system calls */
41326
41327/*
41328** This is the xSetSystemCall() method of sqlite3_vfs for all of the
41329** "win32" VFSes. Return SQLITE_OK opon successfully updating the
41330** system call pointer, or SQLITE_NOTFOUND if there is no configurable
41331** system call named zName.
41332*/
41333static int winSetSystemCall(
41334 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
41335 const char *zName, /* Name of system call to override */
41336 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
41337){
41338 unsigned int i;
41339 int rc = SQLITE_NOTFOUND;
41340
41341 UNUSED_PARAMETER(pNotUsed);
41342 if( zName==0 ){
41343 /* If no zName is given, restore all system calls to their default
41344 ** settings and return NULL
41345 */
41346 rc = SQLITE_OK;
41347 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
41348 if( aSyscall[i].pDefault ){
41349 aSyscall[i].pCurrent = aSyscall[i].pDefault;
41350 }
41351 }
41352 }else{
41353 /* If zName is specified, operate on only the one system call
41354 ** specified.
41355 */
41356 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
41357 if( strcmp(zName, aSyscall[i].zName)==0 ){
41358 if( aSyscall[i].pDefault==0 ){
41359 aSyscall[i].pDefault = aSyscall[i].pCurrent;
41360 }
41361 rc = SQLITE_OK;
41362 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
41363 aSyscall[i].pCurrent = pNewFunc;
41364 break;
41365 }
41366 }
41367 }
41368 return rc;
41369}
41370
41371/*
41372** Return the value of a system call. Return NULL if zName is not a
41373** recognized system call name. NULL is also returned if the system call
41374** is currently undefined.
41375*/
41376static sqlite3_syscall_ptr winGetSystemCall(
41377 sqlite3_vfs *pNotUsed,
41378 const char *zName
41379){
41380 unsigned int i;
41381
41382 UNUSED_PARAMETER(pNotUsed);
41383 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
41384 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
41385 }
41386 return 0;
41387}
41388
41389/*
41390** Return the name of the first system call after zName. If zName==NULL
41391** then return the name of the first system call. Return NULL if zName
41392** is the last system call or if zName is not the name of a valid
41393** system call.
41394*/
41395static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
41396 int i = -1;
41397
41398 UNUSED_PARAMETER(p);
41399 if( zName ){
41400 for(i=0; i<ArraySize(aSyscall)-1; i++){
41401 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
41402 }
41403 }
41404 for(i++; i<ArraySize(aSyscall); i++){
41405 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
41406 }
41407 return 0;
41408}
41409
41410#ifdef SQLITE_WIN32_MALLOC
41411/*
41412** If a Win32 native heap has been configured, this function will attempt to
41413** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one
41414** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The
41415** "pnLargest" argument, if non-zero, will be used to return the size of the
41416** largest committed free block in the heap, in bytes.
41417*/
41418SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
41419 int rc = SQLITE_OK;
41420 UINT nLargest = 0;
41421 HANDLE hHeap;
41422
41423 winMemAssertMagic();
41424 hHeap = winMemGetHeap();
41425 assert( hHeap!=0 );
41426 assert( hHeap!=INVALID_HANDLE_VALUE );
41427#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
41428 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
41429#endif
41430#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
41431 if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
41432 DWORD lastErrno = osGetLastError();
41433 if( lastErrno==NO_ERROR ){
41434 sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
41435 (void*)hHeap);
41436 rc = SQLITE_NOMEM_BKPT;
41437 }else{
41438 sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
41439 osGetLastError(), (void*)hHeap);
41440 rc = SQLITE_ERROR;
41441 }
41442 }
41443#else
41444 sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
41445 (void*)hHeap);
41446 rc = SQLITE_NOTFOUND;
41447#endif
41448 if( pnLargest ) *pnLargest = nLargest;
41449 return rc;
41450}
41451
41452/*
41453** If a Win32 native heap has been configured, this function will attempt to
41454** destroy and recreate it. If the Win32 native heap is not isolated and/or
41455** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
41456** be returned and no changes will be made to the Win32 native heap.
41457*/
41458SQLITE_API int sqlite3_win32_reset_heap(){
41459 int rc;
41460 MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
41461 MUTEX_LOGIC( sqlite3_mutex *pMem; ) /* The memsys static mutex */
41462 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
41463 MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
41464 sqlite3_mutex_enter(pMaster);
41465 sqlite3_mutex_enter(pMem);
41466 winMemAssertMagic();
41467 if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
41468 /*
41469 ** At this point, there should be no outstanding memory allocations on
41470 ** the heap. Also, since both the master and memsys locks are currently
41471 ** being held by us, no other function (i.e. from another thread) should
41472 ** be able to even access the heap. Attempt to destroy and recreate our
41473 ** isolated Win32 native heap now.
41474 */
41475 assert( winMemGetHeap()!=NULL );
41476 assert( winMemGetOwned() );
41477 assert( sqlite3_memory_used()==0 );
41478 winMemShutdown(winMemGetDataPtr());
41479 assert( winMemGetHeap()==NULL );
41480 assert( !winMemGetOwned() );
41481 assert( sqlite3_memory_used()==0 );
41482 rc = winMemInit(winMemGetDataPtr());
41483 assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
41484 assert( rc!=SQLITE_OK || winMemGetOwned() );
41485 assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
41486 }else{
41487 /*
41488 ** The Win32 native heap cannot be modified because it may be in use.
41489 */
41490 rc = SQLITE_BUSY;
41491 }
41492 sqlite3_mutex_leave(pMem);
41493 sqlite3_mutex_leave(pMaster);
41494 return rc;
41495}
41496#endif /* SQLITE_WIN32_MALLOC */
41497
41498/*
41499** This function outputs the specified (ANSI) string to the Win32 debugger
41500** (if available).
41501*/
41502
41503SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
41504 char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
41505 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
41506 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
41507 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
41508#ifdef SQLITE_ENABLE_API_ARMOR
41509 if( !zBuf ){
41510 (void)SQLITE_MISUSE_BKPT;
41511 return;
41512 }
41513#endif
41514#if defined(SQLITE_WIN32_HAS_ANSI)
41515 if( nMin>0 ){
41516 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
41517 memcpy(zDbgBuf, zBuf, nMin);
41518 osOutputDebugStringA(zDbgBuf);
41519 }else{
41520 osOutputDebugStringA(zBuf);
41521 }
41522#elif defined(SQLITE_WIN32_HAS_WIDE)
41523 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
41524 if ( osMultiByteToWideChar(
41525 osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
41526 nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
41527 return;
41528 }
41529 osOutputDebugStringW((LPCWSTR)zDbgBuf);
41530#else
41531 if( nMin>0 ){
41532 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
41533 memcpy(zDbgBuf, zBuf, nMin);
41534 fprintf(stderr, "%s", zDbgBuf);
41535 }else{
41536 fprintf(stderr, "%s", zBuf);
41537 }
41538#endif
41539}
41540
41541/*
41542** The following routine suspends the current thread for at least ms
41543** milliseconds. This is equivalent to the Win32 Sleep() interface.
41544*/
41545#if SQLITE_OS_WINRT
41546static HANDLE sleepObj = NULL;
41547#endif
41548
41549SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
41550#if SQLITE_OS_WINRT
41551 if ( sleepObj==NULL ){
41552 sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
41553 SYNCHRONIZE);
41554 }
41555 assert( sleepObj!=NULL );
41556 osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
41557#else
41558 osSleep(milliseconds);
41559#endif
41560}
41561
41562#if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
41563 SQLITE_THREADSAFE>0
41564SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){
41565 DWORD rc;
41566 while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
41567 TRUE))==WAIT_IO_COMPLETION ){}
41568 return rc;
41569}
41570#endif
41571
41572/*
41573** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
41574** or WinCE. Return false (zero) for Win95, Win98, or WinME.
41575**
41576** Here is an interesting observation: Win95, Win98, and WinME lack
41577** the LockFileEx() API. But we can still statically link against that
41578** API as long as we don't call it when running Win95/98/ME. A call to
41579** this routine is used to determine if the host is Win95/98/ME or
41580** WinNT/2K/XP so that we will know whether or not we can safely call
41581** the LockFileEx() API.
41582*/
41583
41584#if !SQLITE_WIN32_GETVERSIONEX
41585# define osIsNT() (1)
41586#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
41587# define osIsNT() (1)
41588#elif !defined(SQLITE_WIN32_HAS_WIDE)
41589# define osIsNT() (0)
41590#else
41591# define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
41592#endif
41593
41594/*
41595** This function determines if the machine is running a version of Windows
41596** based on the NT kernel.
41597*/
41598SQLITE_API int sqlite3_win32_is_nt(void){
41599#if SQLITE_OS_WINRT
41600 /*
41601 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
41602 ** kernel.
41603 */
41604 return 1;
41605#elif SQLITE_WIN32_GETVERSIONEX
41606 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
41607#if defined(SQLITE_WIN32_HAS_ANSI)
41608 OSVERSIONINFOA sInfo;
41609 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
41610 osGetVersionExA(&sInfo);
41611 osInterlockedCompareExchange(&sqlite3_os_type,
41612 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
41613#elif defined(SQLITE_WIN32_HAS_WIDE)
41614 OSVERSIONINFOW sInfo;
41615 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
41616 osGetVersionExW(&sInfo);
41617 osInterlockedCompareExchange(&sqlite3_os_type,
41618 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
41619#endif
41620 }
41621 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
41622#elif SQLITE_TEST
41623 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
41624#else
41625 /*
41626 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
41627 ** deprecated are always assumed to be based on the NT kernel.
41628 */
41629 return 1;
41630#endif
41631}
41632
41633#ifdef SQLITE_WIN32_MALLOC
41634/*
41635** Allocate nBytes of memory.
41636*/
41637static void *winMemMalloc(int nBytes){
41638 HANDLE hHeap;
41639 void *p;
41640
41641 winMemAssertMagic();
41642 hHeap = winMemGetHeap();
41643 assert( hHeap!=0 );
41644 assert( hHeap!=INVALID_HANDLE_VALUE );
41645#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
41646 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
41647#endif
41648 assert( nBytes>=0 );
41649 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
41650 if( !p ){
41651 sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
41652 nBytes, osGetLastError(), (void*)hHeap);
41653 }
41654 return p;
41655}
41656
41657/*
41658** Free memory.
41659*/
41660static void winMemFree(void *pPrior){
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, pPrior) );
41669#endif
41670 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
41671 if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
41672 sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
41673 pPrior, osGetLastError(), (void*)hHeap);
41674 }
41675}
41676
41677/*
41678** Change the size of an existing memory allocation
41679*/
41680static void *winMemRealloc(void *pPrior, int nBytes){
41681 HANDLE hHeap;
41682 void *p;
41683
41684 winMemAssertMagic();
41685 hHeap = winMemGetHeap();
41686 assert( hHeap!=0 );
41687 assert( hHeap!=INVALID_HANDLE_VALUE );
41688#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
41689 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
41690#endif
41691 assert( nBytes>=0 );
41692 if( !pPrior ){
41693 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
41694 }else{
41695 p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
41696 }
41697 if( !p ){
41698 sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p",
41699 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
41700 (void*)hHeap);
41701 }
41702 return p;
41703}
41704
41705/*
41706** Return the size of an outstanding allocation, in bytes.
41707*/
41708static int winMemSize(void *p){
41709 HANDLE hHeap;
41710 SIZE_T n;
41711
41712 winMemAssertMagic();
41713 hHeap = winMemGetHeap();
41714 assert( hHeap!=0 );
41715 assert( hHeap!=INVALID_HANDLE_VALUE );
41716#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
41717 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) );
41718#endif
41719 if( !p ) return 0;
41720 n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
41721 if( n==(SIZE_T)-1 ){
41722 sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
41723 p, osGetLastError(), (void*)hHeap);
41724 return 0;
41725 }
41726 return (int)n;
41727}
41728
41729/*
41730** Round up a request size to the next valid allocation size.
41731*/
41732static int winMemRoundup(int n){
41733 return n;
41734}
41735
41736/*
41737** Initialize this module.
41738*/
41739static int winMemInit(void *pAppData){
41740 winMemData *pWinMemData = (winMemData *)pAppData;
41741
41742 if( !pWinMemData ) return SQLITE_ERROR;
41743 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
41744 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
41745
41746#if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
41747 if( !pWinMemData->hHeap ){
41748 DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
41749 DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
41750 if( dwMaximumSize==0 ){
41751 dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
41752 }else if( dwInitialSize>dwMaximumSize ){
41753 dwInitialSize = dwMaximumSize;
41754 }
41755 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
41756 dwInitialSize, dwMaximumSize);
41757 if( !pWinMemData->hHeap ){
41758 sqlite3_log(SQLITE_NOMEM,
41759 "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
41760 osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
41761 dwMaximumSize);
41762 return SQLITE_NOMEM_BKPT;
41763 }
41764 pWinMemData->bOwned = TRUE;
41765 assert( pWinMemData->bOwned );
41766 }
41767#else
41768 pWinMemData->hHeap = osGetProcessHeap();
41769 if( !pWinMemData->hHeap ){
41770 sqlite3_log(SQLITE_NOMEM,
41771 "failed to GetProcessHeap (%lu)", osGetLastError());
41772 return SQLITE_NOMEM_BKPT;
41773 }
41774 pWinMemData->bOwned = FALSE;
41775 assert( !pWinMemData->bOwned );
41776#endif
41777 assert( pWinMemData->hHeap!=0 );
41778 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
41779#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
41780 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
41781#endif
41782 return SQLITE_OK;
41783}
41784
41785/*
41786** Deinitialize this module.
41787*/
41788static void winMemShutdown(void *pAppData){
41789 winMemData *pWinMemData = (winMemData *)pAppData;
41790
41791 if( !pWinMemData ) return;
41792 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
41793 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
41794
41795 if( pWinMemData->hHeap ){
41796 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
41797#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
41798 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
41799#endif
41800 if( pWinMemData->bOwned ){
41801 if( !osHeapDestroy(pWinMemData->hHeap) ){
41802 sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p",
41803 osGetLastError(), (void*)pWinMemData->hHeap);
41804 }
41805 pWinMemData->bOwned = FALSE;
41806 }
41807 pWinMemData->hHeap = NULL;
41808 }
41809}
41810
41811/*
41812** Populate the low-level memory allocation function pointers in
41813** sqlite3GlobalConfig.m with pointers to the routines in this file. The
41814** arguments specify the block of memory to manage.
41815**
41816** This routine is only called by sqlite3_config(), and therefore
41817** is not required to be threadsafe (it is not).
41818*/
41819SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
41820 static const sqlite3_mem_methods winMemMethods = {
41821 winMemMalloc,
41822 winMemFree,
41823 winMemRealloc,
41824 winMemSize,
41825 winMemRoundup,
41826 winMemInit,
41827 winMemShutdown,
41828 &win_mem_data
41829 };
41830 return &winMemMethods;
41831}
41832
41833SQLITE_PRIVATE void sqlite3MemSetDefault(void){
41834 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
41835}
41836#endif /* SQLITE_WIN32_MALLOC */
41837
41838/*
41839** Convert a UTF-8 string to Microsoft Unicode.
41840**
41841** Space to hold the returned string is obtained from sqlite3_malloc().
41842*/
41843static LPWSTR winUtf8ToUnicode(const char *zText){
41844 int nChar;
41845 LPWSTR zWideText;
41846
41847 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
41848 if( nChar==0 ){
41849 return 0;
41850 }
41851 zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
41852 if( zWideText==0 ){
41853 return 0;
41854 }
41855 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
41856 nChar);
41857 if( nChar==0 ){
41858 sqlite3_free(zWideText);
41859 zWideText = 0;
41860 }
41861 return zWideText;
41862}
41863
41864/*
41865** Convert a Microsoft Unicode string to UTF-8.
41866**
41867** Space to hold the returned string is obtained from sqlite3_malloc().
41868*/
41869static char *winUnicodeToUtf8(LPCWSTR zWideText){
41870 int nByte;
41871 char *zText;
41872
41873 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
41874 if( nByte == 0 ){
41875 return 0;
41876 }
41877 zText = sqlite3MallocZero( nByte );
41878 if( zText==0 ){
41879 return 0;
41880 }
41881 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
41882 0, 0);
41883 if( nByte == 0 ){
41884 sqlite3_free(zText);
41885 zText = 0;
41886 }
41887 return zText;
41888}
41889
41890/*
41891** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
41892** code page.
41893**
41894** Space to hold the returned string is obtained from sqlite3_malloc().
41895*/
41896static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
41897 int nByte;
41898 LPWSTR zMbcsText;
41899 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
41900
41901 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
41902 0)*sizeof(WCHAR);
41903 if( nByte==0 ){
41904 return 0;
41905 }
41906 zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
41907 if( zMbcsText==0 ){
41908 return 0;
41909 }
41910 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
41911 nByte);
41912 if( nByte==0 ){
41913 sqlite3_free(zMbcsText);
41914 zMbcsText = 0;
41915 }
41916 return zMbcsText;
41917}
41918
41919/*
41920** Convert a Microsoft Unicode string to a multi-byte character string,
41921** using the ANSI or OEM code page.
41922**
41923** Space to hold the returned string is obtained from sqlite3_malloc().
41924*/
41925static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
41926 int nByte;
41927 char *zText;
41928 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
41929
41930 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
41931 if( nByte == 0 ){
41932 return 0;
41933 }
41934 zText = sqlite3MallocZero( nByte );
41935 if( zText==0 ){
41936 return 0;
41937 }
41938 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
41939 nByte, 0, 0);
41940 if( nByte == 0 ){
41941 sqlite3_free(zText);
41942 zText = 0;
41943 }
41944 return zText;
41945}
41946
41947/*
41948** Convert a multi-byte character string to UTF-8.
41949**
41950** Space to hold the returned string is obtained from sqlite3_malloc().
41951*/
41952static char *winMbcsToUtf8(const char *zText, int useAnsi){
41953 char *zTextUtf8;
41954 LPWSTR zTmpWide;
41955
41956 zTmpWide = winMbcsToUnicode(zText, useAnsi);
41957 if( zTmpWide==0 ){
41958 return 0;
41959 }
41960 zTextUtf8 = winUnicodeToUtf8(zTmpWide);
41961 sqlite3_free(zTmpWide);
41962 return zTextUtf8;
41963}
41964
41965/*
41966** Convert a UTF-8 string to a multi-byte character string.
41967**
41968** Space to hold the returned string is obtained from sqlite3_malloc().
41969*/
41970static char *winUtf8ToMbcs(const char *zText, int useAnsi){
41971 char *zTextMbcs;
41972 LPWSTR zTmpWide;
41973
41974 zTmpWide = winUtf8ToUnicode(zText);
41975 if( zTmpWide==0 ){
41976 return 0;
41977 }
41978 zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
41979 sqlite3_free(zTmpWide);
41980 return zTextMbcs;
41981}
41982
41983/*
41984** This is a public wrapper for the winUtf8ToUnicode() function.
41985*/
41986SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
41987#ifdef SQLITE_ENABLE_API_ARMOR
41988 if( !zText ){
41989 (void)SQLITE_MISUSE_BKPT;
41990 return 0;
41991 }
41992#endif
41993#ifndef SQLITE_OMIT_AUTOINIT
41994 if( sqlite3_initialize() ) return 0;
41995#endif
41996 return winUtf8ToUnicode(zText);
41997}
41998
41999/*
42000** This is a public wrapper for the winUnicodeToUtf8() function.
42001*/
42002SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
42003#ifdef SQLITE_ENABLE_API_ARMOR
42004 if( !zWideText ){
42005 (void)SQLITE_MISUSE_BKPT;
42006 return 0;
42007 }
42008#endif
42009#ifndef SQLITE_OMIT_AUTOINIT
42010 if( sqlite3_initialize() ) return 0;
42011#endif
42012 return winUnicodeToUtf8(zWideText);
42013}
42014
42015/*
42016** This is a public wrapper for the winMbcsToUtf8() function.
42017*/
42018SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
42019#ifdef SQLITE_ENABLE_API_ARMOR
42020 if( !zText ){
42021 (void)SQLITE_MISUSE_BKPT;
42022 return 0;
42023 }
42024#endif
42025#ifndef SQLITE_OMIT_AUTOINIT
42026 if( sqlite3_initialize() ) return 0;
42027#endif
42028 return winMbcsToUtf8(zText, osAreFileApisANSI());
42029}
42030
42031/*
42032** This is a public wrapper for the winMbcsToUtf8() function.
42033*/
42034SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
42035#ifdef SQLITE_ENABLE_API_ARMOR
42036 if( !zText ){
42037 (void)SQLITE_MISUSE_BKPT;
42038 return 0;
42039 }
42040#endif
42041#ifndef SQLITE_OMIT_AUTOINIT
42042 if( sqlite3_initialize() ) return 0;
42043#endif
42044 return winMbcsToUtf8(zText, useAnsi);
42045}
42046
42047/*
42048** This is a public wrapper for the winUtf8ToMbcs() function.
42049*/
42050SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){
42051#ifdef SQLITE_ENABLE_API_ARMOR
42052 if( !zText ){
42053 (void)SQLITE_MISUSE_BKPT;
42054 return 0;
42055 }
42056#endif
42057#ifndef SQLITE_OMIT_AUTOINIT
42058 if( sqlite3_initialize() ) return 0;
42059#endif
42060 return winUtf8ToMbcs(zText, osAreFileApisANSI());
42061}
42062
42063/*
42064** This is a public wrapper for the winUtf8ToMbcs() function.
42065*/
42066SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
42067#ifdef SQLITE_ENABLE_API_ARMOR
42068 if( !zText ){
42069 (void)SQLITE_MISUSE_BKPT;
42070 return 0;
42071 }
42072#endif
42073#ifndef SQLITE_OMIT_AUTOINIT
42074 if( sqlite3_initialize() ) return 0;
42075#endif
42076 return winUtf8ToMbcs(zText, useAnsi);
42077}
42078
42079/*
42080** This function is the same as sqlite3_win32_set_directory (below); however,
42081** it accepts a UTF-8 string.
42082*/
42083SQLITE_API int sqlite3_win32_set_directory8(
42084 unsigned long type, /* Identifier for directory being set or reset */
42085 const char *zValue /* New value for directory being set or reset */
42086){
42087 char **ppDirectory = 0;
42088#ifndef SQLITE_OMIT_AUTOINIT
42089 int rc = sqlite3_initialize();
42090 if( rc ) return rc;
42091#endif
42092 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
42093 ppDirectory = &sqlite3_data_directory;
42094 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
42095 ppDirectory = &sqlite3_temp_directory;
42096 }
42097 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
42098 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
42099 );
42100 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
42101 if( ppDirectory ){
42102 char *zCopy = 0;
42103 if( zValue && zValue[0] ){
42104 zCopy = sqlite3_mprintf("%s", zValue);
42105 if ( zCopy==0 ){
42106 return SQLITE_NOMEM_BKPT;
42107 }
42108 }
42109 sqlite3_free(*ppDirectory);
42110 *ppDirectory = zCopy;
42111 return SQLITE_OK;
42112 }
42113 return SQLITE_ERROR;
42114}
42115
42116/*
42117** This function is the same as sqlite3_win32_set_directory (below); however,
42118** it accepts a UTF-16 string.
42119*/
42120SQLITE_API int sqlite3_win32_set_directory16(
42121 unsigned long type, /* Identifier for directory being set or reset */
42122 const void *zValue /* New value for directory being set or reset */
42123){
42124 int rc;
42125 char *zUtf8 = 0;
42126 if( zValue ){
42127 zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
42128 if( zUtf8==0 ) return SQLITE_NOMEM_BKPT;
42129 }
42130 rc = sqlite3_win32_set_directory8(type, zUtf8);
42131 if( zUtf8 ) sqlite3_free(zUtf8);
42132 return rc;
42133}
42134
42135/*
42136** This function sets the data directory or the temporary directory based on
42137** the provided arguments. The type argument must be 1 in order to set the
42138** data directory or 2 in order to set the temporary directory. The zValue
42139** argument is the name of the directory to use. The return value will be
42140** SQLITE_OK if successful.
42141*/
42142SQLITE_API int sqlite3_win32_set_directory(
42143 unsigned long type, /* Identifier for directory being set or reset */
42144 void *zValue /* New value for directory being set or reset */
42145){
42146 return sqlite3_win32_set_directory16(type, zValue);
42147}
42148
42149/*
42150** The return value of winGetLastErrorMsg
42151** is zero if the error message fits in the buffer, or non-zero
42152** otherwise (if the message was truncated).
42153*/
42154static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
42155 /* FormatMessage returns 0 on failure. Otherwise it
42156 ** returns the number of TCHARs written to the output
42157 ** buffer, excluding the terminating null char.
42158 */
42159 DWORD dwLen = 0;
42160 char *zOut = 0;
42161
42162 if( osIsNT() ){
42163#if SQLITE_OS_WINRT
42164 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
42165 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
42166 FORMAT_MESSAGE_IGNORE_INSERTS,
42167 NULL,
42168 lastErrno,
42169 0,
42170 zTempWide,
42171 SQLITE_WIN32_MAX_ERRMSG_CHARS,
42172 0);
42173#else
42174 LPWSTR zTempWide = NULL;
42175 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
42176 FORMAT_MESSAGE_FROM_SYSTEM |
42177 FORMAT_MESSAGE_IGNORE_INSERTS,
42178 NULL,
42179 lastErrno,
42180 0,
42181 (LPWSTR) &zTempWide,
42182 0,
42183 0);
42184#endif
42185 if( dwLen > 0 ){
42186 /* allocate a buffer and convert to UTF8 */
42187 sqlite3BeginBenignMalloc();
42188 zOut = winUnicodeToUtf8(zTempWide);
42189 sqlite3EndBenignMalloc();
42190#if !SQLITE_OS_WINRT
42191 /* free the system buffer allocated by FormatMessage */
42192 osLocalFree(zTempWide);
42193#endif
42194 }
42195 }
42196#ifdef SQLITE_WIN32_HAS_ANSI
42197 else{
42198 char *zTemp = NULL;
42199 dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
42200 FORMAT_MESSAGE_FROM_SYSTEM |
42201 FORMAT_MESSAGE_IGNORE_INSERTS,
42202 NULL,
42203 lastErrno,
42204 0,
42205 (LPSTR) &zTemp,
42206 0,
42207 0);
42208 if( dwLen > 0 ){
42209 /* allocate a buffer and convert to UTF8 */
42210 sqlite3BeginBenignMalloc();
42211 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
42212 sqlite3EndBenignMalloc();
42213 /* free the system buffer allocated by FormatMessage */
42214 osLocalFree(zTemp);
42215 }
42216 }
42217#endif
42218 if( 0 == dwLen ){
42219 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
42220 }else{
42221 /* copy a maximum of nBuf chars to output buffer */
42222 sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
42223 /* free the UTF8 buffer */
42224 sqlite3_free(zOut);
42225 }
42226 return 0;
42227}
42228
42229/*
42230**
42231** This function - winLogErrorAtLine() - is only ever called via the macro
42232** winLogError().
42233**
42234** This routine is invoked after an error occurs in an OS function.
42235** It logs a message using sqlite3_log() containing the current value of
42236** error code and, if possible, the human-readable equivalent from
42237** FormatMessage.
42238**
42239** The first argument passed to the macro should be the error code that
42240** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
42241** The two subsequent arguments should be the name of the OS function that
42242** failed and the associated file-system path, if any.
42243*/
42244#define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
42245static int winLogErrorAtLine(
42246 int errcode, /* SQLite error code */
42247 DWORD lastErrno, /* Win32 last error */
42248 const char *zFunc, /* Name of OS function that failed */
42249 const char *zPath, /* File path associated with error */
42250 int iLine /* Source line number where error occurred */
42251){
42252 char zMsg[500]; /* Human readable error text */
42253 int i; /* Loop counter */
42254
42255 zMsg[0] = 0;
42256 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
42257 assert( errcode!=SQLITE_OK );
42258 if( zPath==0 ) zPath = "";
42259 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
42260 zMsg[i] = 0;
42261 sqlite3_log(errcode,
42262 "os_win.c:%d: (%lu) %s(%s) - %s",
42263 iLine, lastErrno, zFunc, zPath, zMsg
42264 );
42265
42266 return errcode;
42267}
42268
42269/*
42270** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
42271** will be retried following a locking error - probably caused by
42272** antivirus software. Also the initial delay before the first retry.
42273** The delay increases linearly with each retry.
42274*/
42275#ifndef SQLITE_WIN32_IOERR_RETRY
42276# define SQLITE_WIN32_IOERR_RETRY 10
42277#endif
42278#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
42279# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
42280#endif
42281static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
42282static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
42283
42284/*
42285** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
42286** error code obtained via GetLastError() is eligible to be retried. It
42287** must accept the error code DWORD as its only argument and should return
42288** non-zero if the error code is transient in nature and the operation
42289** responsible for generating the original error might succeed upon being
42290** retried. The argument to this macro should be a variable.
42291**
42292** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
42293** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
42294** returns zero. The "winIoerrCanRetry2" macro is completely optional and
42295** may be used to include additional error codes in the set that should
42296** result in the failing I/O operation being retried by the caller. If
42297** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
42298** identical to those of the "winIoerrCanRetry1" macro.
42299*/
42300#if !defined(winIoerrCanRetry1)
42301#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
42302 ((a)==ERROR_SHARING_VIOLATION) || \
42303 ((a)==ERROR_LOCK_VIOLATION) || \
42304 ((a)==ERROR_DEV_NOT_EXIST) || \
42305 ((a)==ERROR_NETNAME_DELETED) || \
42306 ((a)==ERROR_SEM_TIMEOUT) || \
42307 ((a)==ERROR_NETWORK_UNREACHABLE))
42308#endif
42309
42310/*
42311** If a ReadFile() or WriteFile() error occurs, invoke this routine
42312** to see if it should be retried. Return TRUE to retry. Return FALSE
42313** to give up with an error.
42314*/
42315static int winRetryIoerr(int *pnRetry, DWORD *pError){
42316 DWORD e = osGetLastError();
42317 if( *pnRetry>=winIoerrRetry ){
42318 if( pError ){
42319 *pError = e;
42320 }
42321 return 0;
42322 }
42323 if( winIoerrCanRetry1(e) ){
42324 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
42325 ++*pnRetry;
42326 return 1;
42327 }
42328#if defined(winIoerrCanRetry2)
42329 else if( winIoerrCanRetry2(e) ){
42330 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
42331 ++*pnRetry;
42332 return 1;
42333 }
42334#endif
42335 if( pError ){
42336 *pError = e;
42337 }
42338 return 0;
42339}
42340
42341/*
42342** Log a I/O error retry episode.
42343*/
42344static void winLogIoerr(int nRetry, int lineno){
42345 if( nRetry ){
42346 sqlite3_log(SQLITE_NOTICE,
42347 "delayed %dms for lock/sharing conflict at line %d",
42348 winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
42349 );
42350 }
42351}
42352
42353/*
42354** This #if does not rely on the SQLITE_OS_WINCE define because the
42355** corresponding section in "date.c" cannot use it.
42356*/
42357#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
42358 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
42359/*
42360** The MSVC CRT on Windows CE may not have a localtime() function.
42361** So define a substitute.
42362*/
42363/* # include <time.h> */
42364struct tm *__cdecl localtime(const time_t *t)
42365{
42366 static struct tm y;
42367 FILETIME uTm, lTm;
42368 SYSTEMTIME pTm;
42369 sqlite3_int64 t64;
42370 t64 = *t;
42371 t64 = (t64 + 11644473600)*10000000;
42372 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
42373 uTm.dwHighDateTime= (DWORD)(t64 >> 32);
42374 osFileTimeToLocalFileTime(&uTm,&lTm);
42375 osFileTimeToSystemTime(&lTm,&pTm);
42376 y.tm_year = pTm.wYear - 1900;
42377 y.tm_mon = pTm.wMonth - 1;
42378 y.tm_wday = pTm.wDayOfWeek;
42379 y.tm_mday = pTm.wDay;
42380 y.tm_hour = pTm.wHour;
42381 y.tm_min = pTm.wMinute;
42382 y.tm_sec = pTm.wSecond;
42383 return &y;
42384}
42385#endif
42386
42387#if SQLITE_OS_WINCE
42388/*************************************************************************
42389** This section contains code for WinCE only.
42390*/
42391#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
42392
42393/*
42394** Acquire a lock on the handle h
42395*/
42396static void winceMutexAcquire(HANDLE h){
42397 DWORD dwErr;
42398 do {
42399 dwErr = osWaitForSingleObject(h, INFINITE);
42400 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
42401}
42402/*
42403** Release a lock acquired by winceMutexAcquire()
42404*/
42405#define winceMutexRelease(h) ReleaseMutex(h)
42406
42407/*
42408** Create the mutex and shared memory used for locking in the file
42409** descriptor pFile
42410*/
42411static int winceCreateLock(const char *zFilename, winFile *pFile){
42412 LPWSTR zTok;
42413 LPWSTR zName;
42414 DWORD lastErrno;
42415 BOOL bLogged = FALSE;
42416 BOOL bInit = TRUE;
42417
42418 zName = winUtf8ToUnicode(zFilename);
42419 if( zName==0 ){
42420 /* out of memory */
42421 return SQLITE_IOERR_NOMEM_BKPT;
42422 }
42423
42424 /* Initialize the local lockdata */
42425 memset(&pFile->local, 0, sizeof(pFile->local));
42426
42427 /* Replace the backslashes from the filename and lowercase it
42428 ** to derive a mutex name. */
42429 zTok = osCharLowerW(zName);
42430 for (;*zTok;zTok++){
42431 if (*zTok == '\\') *zTok = '_';
42432 }
42433
42434 /* Create/open the named mutex */
42435 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
42436 if (!pFile->hMutex){
42437 pFile->lastErrno = osGetLastError();
42438 sqlite3_free(zName);
42439 return winLogError(SQLITE_IOERR, pFile->lastErrno,
42440 "winceCreateLock1", zFilename);
42441 }
42442
42443 /* Acquire the mutex before continuing */
42444 winceMutexAcquire(pFile->hMutex);
42445
42446 /* Since the names of named mutexes, semaphores, file mappings etc are
42447 ** case-sensitive, take advantage of that by uppercasing the mutex name
42448 ** and using that as the shared filemapping name.
42449 */
42450 osCharUpperW(zName);
42451 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
42452 PAGE_READWRITE, 0, sizeof(winceLock),
42453 zName);
42454
42455 /* Set a flag that indicates we're the first to create the memory so it
42456 ** must be zero-initialized */
42457 lastErrno = osGetLastError();
42458 if (lastErrno == ERROR_ALREADY_EXISTS){
42459 bInit = FALSE;
42460 }
42461
42462 sqlite3_free(zName);
42463
42464 /* If we succeeded in making the shared memory handle, map it. */
42465 if( pFile->hShared ){
42466 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
42467 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
42468 /* If mapping failed, close the shared memory handle and erase it */
42469 if( !pFile->shared ){
42470 pFile->lastErrno = osGetLastError();
42471 winLogError(SQLITE_IOERR, pFile->lastErrno,
42472 "winceCreateLock2", zFilename);
42473 bLogged = TRUE;
42474 osCloseHandle(pFile->hShared);
42475 pFile->hShared = NULL;
42476 }
42477 }
42478
42479 /* If shared memory could not be created, then close the mutex and fail */
42480 if( pFile->hShared==NULL ){
42481 if( !bLogged ){
42482 pFile->lastErrno = lastErrno;
42483 winLogError(SQLITE_IOERR, pFile->lastErrno,
42484 "winceCreateLock3", zFilename);
42485 bLogged = TRUE;
42486 }
42487 winceMutexRelease(pFile->hMutex);
42488 osCloseHandle(pFile->hMutex);
42489 pFile->hMutex = NULL;
42490 return SQLITE_IOERR;
42491 }
42492
42493 /* Initialize the shared memory if we're supposed to */
42494 if( bInit ){
42495 memset(pFile->shared, 0, sizeof(winceLock));
42496 }
42497
42498 winceMutexRelease(pFile->hMutex);
42499 return SQLITE_OK;
42500}
42501
42502/*
42503** Destroy the part of winFile that deals with wince locks
42504*/
42505static void winceDestroyLock(winFile *pFile){
42506 if (pFile->hMutex){
42507 /* Acquire the mutex */
42508 winceMutexAcquire(pFile->hMutex);
42509
42510 /* The following blocks should probably assert in debug mode, but they
42511 are to cleanup in case any locks remained open */
42512 if (pFile->local.nReaders){
42513 pFile->shared->nReaders --;
42514 }
42515 if (pFile->local.bReserved){
42516 pFile->shared->bReserved = FALSE;
42517 }
42518 if (pFile->local.bPending){
42519 pFile->shared->bPending = FALSE;
42520 }
42521 if (pFile->local.bExclusive){
42522 pFile->shared->bExclusive = FALSE;
42523 }
42524
42525 /* De-reference and close our copy of the shared memory handle */
42526 osUnmapViewOfFile(pFile->shared);
42527 osCloseHandle(pFile->hShared);
42528
42529 /* Done with the mutex */
42530 winceMutexRelease(pFile->hMutex);
42531 osCloseHandle(pFile->hMutex);
42532 pFile->hMutex = NULL;
42533 }
42534}
42535
42536/*
42537** An implementation of the LockFile() API of Windows for CE
42538*/
42539static BOOL winceLockFile(
42540 LPHANDLE phFile,
42541 DWORD dwFileOffsetLow,
42542 DWORD dwFileOffsetHigh,
42543 DWORD nNumberOfBytesToLockLow,
42544 DWORD nNumberOfBytesToLockHigh
42545){
42546 winFile *pFile = HANDLE_TO_WINFILE(phFile);
42547 BOOL bReturn = FALSE;
42548
42549 UNUSED_PARAMETER(dwFileOffsetHigh);
42550 UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
42551
42552 if (!pFile->hMutex) return TRUE;
42553 winceMutexAcquire(pFile->hMutex);
42554
42555 /* Wanting an exclusive lock? */
42556 if (dwFileOffsetLow == (DWORD)SHARED_FIRST
42557 && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
42558 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
42559 pFile->shared->bExclusive = TRUE;
42560 pFile->local.bExclusive = TRUE;
42561 bReturn = TRUE;
42562 }
42563 }
42564
42565 /* Want a read-only lock? */
42566 else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
42567 nNumberOfBytesToLockLow == 1){
42568 if (pFile->shared->bExclusive == 0){
42569 pFile->local.nReaders ++;
42570 if (pFile->local.nReaders == 1){
42571 pFile->shared->nReaders ++;
42572 }
42573 bReturn = TRUE;
42574 }
42575 }
42576
42577 /* Want a pending lock? */
42578 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
42579 && nNumberOfBytesToLockLow == 1){
42580 /* If no pending lock has been acquired, then acquire it */
42581 if (pFile->shared->bPending == 0) {
42582 pFile->shared->bPending = TRUE;
42583 pFile->local.bPending = TRUE;
42584 bReturn = TRUE;
42585 }
42586 }
42587
42588 /* Want a reserved lock? */
42589 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
42590 && nNumberOfBytesToLockLow == 1){
42591 if (pFile->shared->bReserved == 0) {
42592 pFile->shared->bReserved = TRUE;
42593 pFile->local.bReserved = TRUE;
42594 bReturn = TRUE;
42595 }
42596 }
42597
42598 winceMutexRelease(pFile->hMutex);
42599 return bReturn;
42600}
42601
42602/*
42603** An implementation of the UnlockFile API of Windows for CE
42604*/
42605static BOOL winceUnlockFile(
42606 LPHANDLE phFile,
42607 DWORD dwFileOffsetLow,
42608 DWORD dwFileOffsetHigh,
42609 DWORD nNumberOfBytesToUnlockLow,
42610 DWORD nNumberOfBytesToUnlockHigh
42611){
42612 winFile *pFile = HANDLE_TO_WINFILE(phFile);
42613 BOOL bReturn = FALSE;
42614
42615 UNUSED_PARAMETER(dwFileOffsetHigh);
42616 UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
42617
42618 if (!pFile->hMutex) return TRUE;
42619 winceMutexAcquire(pFile->hMutex);
42620
42621 /* Releasing a reader lock or an exclusive lock */
42622 if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
42623 /* Did we have an exclusive lock? */
42624 if (pFile->local.bExclusive){
42625 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
42626 pFile->local.bExclusive = FALSE;
42627 pFile->shared->bExclusive = FALSE;
42628 bReturn = TRUE;
42629 }
42630
42631 /* Did we just have a reader lock? */
42632 else if (pFile->local.nReaders){
42633 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
42634 || nNumberOfBytesToUnlockLow == 1);
42635 pFile->local.nReaders --;
42636 if (pFile->local.nReaders == 0)
42637 {
42638 pFile->shared->nReaders --;
42639 }
42640 bReturn = TRUE;
42641 }
42642 }
42643
42644 /* Releasing a pending lock */
42645 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
42646 && nNumberOfBytesToUnlockLow == 1){
42647 if (pFile->local.bPending){
42648 pFile->local.bPending = FALSE;
42649 pFile->shared->bPending = FALSE;
42650 bReturn = TRUE;
42651 }
42652 }
42653 /* Releasing a reserved lock */
42654 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
42655 && nNumberOfBytesToUnlockLow == 1){
42656 if (pFile->local.bReserved) {
42657 pFile->local.bReserved = FALSE;
42658 pFile->shared->bReserved = FALSE;
42659 bReturn = TRUE;
42660 }
42661 }
42662
42663 winceMutexRelease(pFile->hMutex);
42664 return bReturn;
42665}
42666/*
42667** End of the special code for wince
42668*****************************************************************************/
42669#endif /* SQLITE_OS_WINCE */
42670
42671/*
42672** Lock a file region.
42673*/
42674static BOOL winLockFile(
42675 LPHANDLE phFile,
42676 DWORD flags,
42677 DWORD offsetLow,
42678 DWORD offsetHigh,
42679 DWORD numBytesLow,
42680 DWORD numBytesHigh
42681){
42682#if SQLITE_OS_WINCE
42683 /*
42684 ** NOTE: Windows CE is handled differently here due its lack of the Win32
42685 ** API LockFile.
42686 */
42687 return winceLockFile(phFile, offsetLow, offsetHigh,
42688 numBytesLow, numBytesHigh);
42689#else
42690 if( osIsNT() ){
42691 OVERLAPPED ovlp;
42692 memset(&ovlp, 0, sizeof(OVERLAPPED));
42693 ovlp.Offset = offsetLow;
42694 ovlp.OffsetHigh = offsetHigh;
42695 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
42696 }else{
42697 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
42698 numBytesHigh);
42699 }
42700#endif
42701}
42702
42703/*
42704** Unlock a file region.
42705 */
42706static BOOL winUnlockFile(
42707 LPHANDLE phFile,
42708 DWORD offsetLow,
42709 DWORD offsetHigh,
42710 DWORD numBytesLow,
42711 DWORD numBytesHigh
42712){
42713#if SQLITE_OS_WINCE
42714 /*
42715 ** NOTE: Windows CE is handled differently here due its lack of the Win32
42716 ** API UnlockFile.
42717 */
42718 return winceUnlockFile(phFile, offsetLow, offsetHigh,
42719 numBytesLow, numBytesHigh);
42720#else
42721 if( osIsNT() ){
42722 OVERLAPPED ovlp;
42723 memset(&ovlp, 0, sizeof(OVERLAPPED));
42724 ovlp.Offset = offsetLow;
42725 ovlp.OffsetHigh = offsetHigh;
42726 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
42727 }else{
42728 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
42729 numBytesHigh);
42730 }
42731#endif
42732}
42733
42734/*****************************************************************************
42735** The next group of routines implement the I/O methods specified
42736** by the sqlite3_io_methods object.
42737******************************************************************************/
42738
42739/*
42740** Some Microsoft compilers lack this definition.
42741*/
42742#ifndef INVALID_SET_FILE_POINTER
42743# define INVALID_SET_FILE_POINTER ((DWORD)-1)
42744#endif
42745
42746/*
42747** Move the current position of the file handle passed as the first
42748** argument to offset iOffset within the file. If successful, return 0.
42749** Otherwise, set pFile->lastErrno and return non-zero.
42750*/
42751static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
42752#if !SQLITE_OS_WINRT
42753 LONG upperBits; /* Most sig. 32 bits of new offset */
42754 LONG lowerBits; /* Least sig. 32 bits of new offset */
42755 DWORD dwRet; /* Value returned by SetFilePointer() */
42756 DWORD lastErrno; /* Value returned by GetLastError() */
42757
42758 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
42759
42760 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
42761 lowerBits = (LONG)(iOffset & 0xffffffff);
42762
42763 /* API oddity: If successful, SetFilePointer() returns a dword
42764 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
42765 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
42766 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
42767 ** whether an error has actually occurred, it is also necessary to call
42768 ** GetLastError().
42769 */
42770 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
42771
42772 if( (dwRet==INVALID_SET_FILE_POINTER
42773 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
42774 pFile->lastErrno = lastErrno;
42775 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
42776 "winSeekFile", pFile->zPath);
42777 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
42778 return 1;
42779 }
42780
42781 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
42782 return 0;
42783#else
42784 /*
42785 ** Same as above, except that this implementation works for WinRT.
42786 */
42787
42788 LARGE_INTEGER x; /* The new offset */
42789 BOOL bRet; /* Value returned by SetFilePointerEx() */
42790
42791 x.QuadPart = iOffset;
42792 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
42793
42794 if(!bRet){
42795 pFile->lastErrno = osGetLastError();
42796 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
42797 "winSeekFile", pFile->zPath);
42798 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
42799 return 1;
42800 }
42801
42802 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
42803 return 0;
42804#endif
42805}
42806
42807#if SQLITE_MAX_MMAP_SIZE>0
42808/* Forward references to VFS helper methods used for memory mapped files */
42809static int winMapfile(winFile*, sqlite3_int64);
42810static int winUnmapfile(winFile*);
42811#endif
42812
42813/*
42814** Close a file.
42815**
42816** It is reported that an attempt to close a handle might sometimes
42817** fail. This is a very unreasonable result, but Windows is notorious
42818** for being unreasonable so I do not doubt that it might happen. If
42819** the close fails, we pause for 100 milliseconds and try again. As
42820** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
42821** giving up and returning an error.
42822*/
42823#define MX_CLOSE_ATTEMPT 3
42824static int winClose(sqlite3_file *id){
42825 int rc, cnt = 0;
42826 winFile *pFile = (winFile*)id;
42827
42828 assert( id!=0 );
42829#ifndef SQLITE_OMIT_WAL
42830 assert( pFile->pShm==0 );
42831#endif
42832 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
42833 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
42834 osGetCurrentProcessId(), pFile, pFile->h));
42835
42836#if SQLITE_MAX_MMAP_SIZE>0
42837 winUnmapfile(pFile);
42838#endif
42839
42840 do{
42841 rc = osCloseHandle(pFile->h);
42842 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
42843 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
42844#if SQLITE_OS_WINCE
42845#define WINCE_DELETION_ATTEMPTS 3
42846 {
42847 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
42848 if( pAppData==NULL || !pAppData->bNoLock ){
42849 winceDestroyLock(pFile);
42850 }
42851 }
42852 if( pFile->zDeleteOnClose ){
42853 int cnt = 0;
42854 while(
42855 osDeleteFileW(pFile->zDeleteOnClose)==0
42856 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
42857 && cnt++ < WINCE_DELETION_ATTEMPTS
42858 ){
42859 sqlite3_win32_sleep(100); /* Wait a little before trying again */
42860 }
42861 sqlite3_free(pFile->zDeleteOnClose);
42862 }
42863#endif
42864 if( rc ){
42865 pFile->h = NULL;
42866 }
42867 OpenCounter(-1);
42868 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
42869 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
42870 return rc ? SQLITE_OK
42871 : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
42872 "winClose", pFile->zPath);
42873}
42874
42875/*
42876** Read data from a file into a buffer. Return SQLITE_OK if all
42877** bytes were read successfully and SQLITE_IOERR if anything goes
42878** wrong.
42879*/
42880static int winRead(
42881 sqlite3_file *id, /* File to read from */
42882 void *pBuf, /* Write content into this buffer */
42883 int amt, /* Number of bytes to read */
42884 sqlite3_int64 offset /* Begin reading at this offset */
42885){
42886#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
42887 OVERLAPPED overlapped; /* The offset for ReadFile. */
42888#endif
42889 winFile *pFile = (winFile*)id; /* file handle */
42890 DWORD nRead; /* Number of bytes actually read from file */
42891 int nRetry = 0; /* Number of retrys */
42892
42893 assert( id!=0 );
42894 assert( amt>0 );
42895 assert( offset>=0 );
42896 SimulateIOError(return SQLITE_IOERR_READ);
42897 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
42898 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
42899 pFile->h, pBuf, amt, offset, pFile->locktype));
42900
42901#if SQLITE_MAX_MMAP_SIZE>0
42902 /* Deal with as much of this read request as possible by transfering
42903 ** data from the memory mapping using memcpy(). */
42904 if( offset<pFile->mmapSize ){
42905 if( offset+amt <= pFile->mmapSize ){
42906 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
42907 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
42908 osGetCurrentProcessId(), pFile, pFile->h));
42909 return SQLITE_OK;
42910 }else{
42911 int nCopy = (int)(pFile->mmapSize - offset);
42912 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
42913 pBuf = &((u8 *)pBuf)[nCopy];
42914 amt -= nCopy;
42915 offset += nCopy;
42916 }
42917 }
42918#endif
42919
42920#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
42921 if( winSeekFile(pFile, offset) ){
42922 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
42923 osGetCurrentProcessId(), pFile, pFile->h));
42924 return SQLITE_FULL;
42925 }
42926 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
42927#else
42928 memset(&overlapped, 0, sizeof(OVERLAPPED));
42929 overlapped.Offset = (LONG)(offset & 0xffffffff);
42930 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
42931 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
42932 osGetLastError()!=ERROR_HANDLE_EOF ){
42933#endif
42934 DWORD lastErrno;
42935 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
42936 pFile->lastErrno = lastErrno;
42937 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
42938 osGetCurrentProcessId(), pFile, pFile->h));
42939 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
42940 "winRead", pFile->zPath);
42941 }
42942 winLogIoerr(nRetry, __LINE__);
42943 if( nRead<(DWORD)amt ){
42944 /* Unread parts of the buffer must be zero-filled */
42945 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
42946 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
42947 osGetCurrentProcessId(), pFile, pFile->h));
42948 return SQLITE_IOERR_SHORT_READ;
42949 }
42950
42951 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
42952 osGetCurrentProcessId(), pFile, pFile->h));
42953 return SQLITE_OK;
42954}
42955
42956/*
42957** Write data from a buffer into a file. Return SQLITE_OK on success
42958** or some other error code on failure.
42959*/
42960static int winWrite(
42961 sqlite3_file *id, /* File to write into */
42962 const void *pBuf, /* The bytes to be written */
42963 int amt, /* Number of bytes to write */
42964 sqlite3_int64 offset /* Offset into the file to begin writing at */
42965){
42966 int rc = 0; /* True if error has occurred, else false */
42967 winFile *pFile = (winFile*)id; /* File handle */
42968 int nRetry = 0; /* Number of retries */
42969
42970 assert( amt>0 );
42971 assert( pFile );
42972 SimulateIOError(return SQLITE_IOERR_WRITE);
42973 SimulateDiskfullError(return SQLITE_FULL);
42974
42975 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
42976 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
42977 pFile->h, pBuf, amt, offset, pFile->locktype));
42978
42979#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
42980 /* Deal with as much of this write request as possible by transfering
42981 ** data from the memory mapping using memcpy(). */
42982 if( offset<pFile->mmapSize ){
42983 if( offset+amt <= pFile->mmapSize ){
42984 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
42985 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
42986 osGetCurrentProcessId(), pFile, pFile->h));
42987 return SQLITE_OK;
42988 }else{
42989 int nCopy = (int)(pFile->mmapSize - offset);
42990 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
42991 pBuf = &((u8 *)pBuf)[nCopy];
42992 amt -= nCopy;
42993 offset += nCopy;
42994 }
42995 }
42996#endif
42997
42998#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
42999 rc = winSeekFile(pFile, offset);
43000 if( rc==0 ){
43001#else
43002 {
43003#endif
43004#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
43005 OVERLAPPED overlapped; /* The offset for WriteFile. */
43006#endif
43007 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
43008 int nRem = amt; /* Number of bytes yet to be written */
43009 DWORD nWrite; /* Bytes written by each WriteFile() call */
43010 DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
43011
43012#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
43013 memset(&overlapped, 0, sizeof(OVERLAPPED));
43014 overlapped.Offset = (LONG)(offset & 0xffffffff);
43015 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
43016#endif
43017
43018 while( nRem>0 ){
43019#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
43020 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
43021#else
43022 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
43023#endif
43024 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
43025 break;
43026 }
43027 assert( nWrite==0 || nWrite<=(DWORD)nRem );
43028 if( nWrite==0 || nWrite>(DWORD)nRem ){
43029 lastErrno = osGetLastError();
43030 break;
43031 }
43032#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
43033 offset += nWrite;
43034 overlapped.Offset = (LONG)(offset & 0xffffffff);
43035 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
43036#endif
43037 aRem += nWrite;
43038 nRem -= nWrite;
43039 }
43040 if( nRem>0 ){
43041 pFile->lastErrno = lastErrno;
43042 rc = 1;
43043 }
43044 }
43045
43046 if( rc ){
43047 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
43048 || ( pFile->lastErrno==ERROR_DISK_FULL )){
43049 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
43050 osGetCurrentProcessId(), pFile, pFile->h));
43051 return winLogError(SQLITE_FULL, pFile->lastErrno,
43052 "winWrite1", pFile->zPath);
43053 }
43054 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
43055 osGetCurrentProcessId(), pFile, pFile->h));
43056 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
43057 "winWrite2", pFile->zPath);
43058 }else{
43059 winLogIoerr(nRetry, __LINE__);
43060 }
43061 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43062 osGetCurrentProcessId(), pFile, pFile->h));
43063 return SQLITE_OK;
43064}
43065
43066/*
43067** Truncate an open file to a specified size
43068*/
43069static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
43070 winFile *pFile = (winFile*)id; /* File handle object */
43071 int rc = SQLITE_OK; /* Return code for this function */
43072 DWORD lastErrno;
43073#if SQLITE_MAX_MMAP_SIZE>0
43074 sqlite3_int64 oldMmapSize;
43075#endif
43076
43077 assert( pFile );
43078 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
43079 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
43080 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
43081
43082 /* If the user has configured a chunk-size for this file, truncate the
43083 ** file so that it consists of an integer number of chunks (i.e. the
43084 ** actual file size after the operation may be larger than the requested
43085 ** size).
43086 */
43087 if( pFile->szChunk>0 ){
43088 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
43089 }
43090
43091#if SQLITE_MAX_MMAP_SIZE>0
43092 if( pFile->pMapRegion ){
43093 oldMmapSize = pFile->mmapSize;
43094 }else{
43095 oldMmapSize = 0;
43096 }
43097 winUnmapfile(pFile);
43098#endif
43099
43100 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
43101 if( winSeekFile(pFile, nByte) ){
43102 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
43103 "winTruncate1", pFile->zPath);
43104 }else if( 0==osSetEndOfFile(pFile->h) &&
43105 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
43106 pFile->lastErrno = lastErrno;
43107 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
43108 "winTruncate2", pFile->zPath);
43109 }
43110
43111#if SQLITE_MAX_MMAP_SIZE>0
43112 if( rc==SQLITE_OK && oldMmapSize>0 ){
43113 if( oldMmapSize>nByte ){
43114 winMapfile(pFile, -1);
43115 }else{
43116 winMapfile(pFile, oldMmapSize);
43117 }
43118 }
43119#endif
43120
43121 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
43122 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
43123 return rc;
43124}
43125
43126#ifdef SQLITE_TEST
43127/*
43128** Count the number of fullsyncs and normal syncs. This is used to test
43129** that syncs and fullsyncs are occuring at the right times.
43130*/
43131SQLITE_API int sqlite3_sync_count = 0;
43132SQLITE_API int sqlite3_fullsync_count = 0;
43133#endif
43134
43135/*
43136** Make sure all writes to a particular file are committed to disk.
43137*/
43138static int winSync(sqlite3_file *id, int flags){
43139#ifndef SQLITE_NO_SYNC
43140 /*
43141 ** Used only when SQLITE_NO_SYNC is not defined.
43142 */
43143 BOOL rc;
43144#endif
43145#if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
43146 defined(SQLITE_HAVE_OS_TRACE)
43147 /*
43148 ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
43149 ** OSTRACE() macros.
43150 */
43151 winFile *pFile = (winFile*)id;
43152#else
43153 UNUSED_PARAMETER(id);
43154#endif
43155
43156 assert( pFile );
43157 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
43158 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
43159 || (flags&0x0F)==SQLITE_SYNC_FULL
43160 );
43161
43162 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
43163 ** line is to test that doing so does not cause any problems.
43164 */
43165 SimulateDiskfullError( return SQLITE_FULL );
43166
43167 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
43168 osGetCurrentProcessId(), pFile, pFile->h, flags,
43169 pFile->locktype));
43170
43171#ifndef SQLITE_TEST
43172 UNUSED_PARAMETER(flags);
43173#else
43174 if( (flags&0x0F)==SQLITE_SYNC_FULL ){
43175 sqlite3_fullsync_count++;
43176 }
43177 sqlite3_sync_count++;
43178#endif
43179
43180 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
43181 ** no-op
43182 */
43183#ifdef SQLITE_NO_SYNC
43184 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43185 osGetCurrentProcessId(), pFile, pFile->h));
43186 return SQLITE_OK;
43187#else
43188#if SQLITE_MAX_MMAP_SIZE>0
43189 if( pFile->pMapRegion ){
43190 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
43191 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
43192 "rc=SQLITE_OK\n", osGetCurrentProcessId(),
43193 pFile, pFile->pMapRegion));
43194 }else{
43195 pFile->lastErrno = osGetLastError();
43196 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
43197 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
43198 pFile, pFile->pMapRegion));
43199 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
43200 "winSync1", pFile->zPath);
43201 }
43202 }
43203#endif
43204 rc = osFlushFileBuffers(pFile->h);
43205 SimulateIOError( rc=FALSE );
43206 if( rc ){
43207 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43208 osGetCurrentProcessId(), pFile, pFile->h));
43209 return SQLITE_OK;
43210 }else{
43211 pFile->lastErrno = osGetLastError();
43212 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
43213 osGetCurrentProcessId(), pFile, pFile->h));
43214 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
43215 "winSync2", pFile->zPath);
43216 }
43217#endif
43218}
43219
43220/*
43221** Determine the current size of a file in bytes
43222*/
43223static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
43224 winFile *pFile = (winFile*)id;
43225 int rc = SQLITE_OK;
43226
43227 assert( id!=0 );
43228 assert( pSize!=0 );
43229 SimulateIOError(return SQLITE_IOERR_FSTAT);
43230 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
43231
43232#if SQLITE_OS_WINRT
43233 {
43234 FILE_STANDARD_INFO info;
43235 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
43236 &info, sizeof(info)) ){
43237 *pSize = info.EndOfFile.QuadPart;
43238 }else{
43239 pFile->lastErrno = osGetLastError();
43240 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
43241 "winFileSize", pFile->zPath);
43242 }
43243 }
43244#else
43245 {
43246 DWORD upperBits;
43247 DWORD lowerBits;
43248 DWORD lastErrno;
43249
43250 lowerBits = osGetFileSize(pFile->h, &upperBits);
43251 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
43252 if( (lowerBits == INVALID_FILE_SIZE)
43253 && ((lastErrno = osGetLastError())!=NO_ERROR) ){
43254 pFile->lastErrno = lastErrno;
43255 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
43256 "winFileSize", pFile->zPath);
43257 }
43258 }
43259#endif
43260 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
43261 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
43262 return rc;
43263}
43264
43265/*
43266** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
43267*/
43268#ifndef LOCKFILE_FAIL_IMMEDIATELY
43269# define LOCKFILE_FAIL_IMMEDIATELY 1
43270#endif
43271
43272#ifndef LOCKFILE_EXCLUSIVE_LOCK
43273# define LOCKFILE_EXCLUSIVE_LOCK 2
43274#endif
43275
43276/*
43277** Historically, SQLite has used both the LockFile and LockFileEx functions.
43278** When the LockFile function was used, it was always expected to fail
43279** immediately if the lock could not be obtained. Also, it always expected to
43280** obtain an exclusive lock. These flags are used with the LockFileEx function
43281** and reflect those expectations; therefore, they should not be changed.
43282*/
43283#ifndef SQLITE_LOCKFILE_FLAGS
43284# define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \
43285 LOCKFILE_EXCLUSIVE_LOCK)
43286#endif
43287
43288/*
43289** Currently, SQLite never calls the LockFileEx function without wanting the
43290** call to fail immediately if the lock cannot be obtained.
43291*/
43292#ifndef SQLITE_LOCKFILEEX_FLAGS
43293# define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
43294#endif
43295
43296/*
43297** Acquire a reader lock.
43298** Different API routines are called depending on whether or not this
43299** is Win9x or WinNT.
43300*/
43301static int winGetReadLock(winFile *pFile){
43302 int res;
43303 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
43304 if( osIsNT() ){
43305#if SQLITE_OS_WINCE
43306 /*
43307 ** NOTE: Windows CE is handled differently here due its lack of the Win32
43308 ** API LockFileEx.
43309 */
43310 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
43311#else
43312 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
43313 SHARED_SIZE, 0);
43314#endif
43315 }
43316#ifdef SQLITE_WIN32_HAS_ANSI
43317 else{
43318 int lk;
43319 sqlite3_randomness(sizeof(lk), &lk);
43320 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
43321 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
43322 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
43323 }
43324#endif
43325 if( res == 0 ){
43326 pFile->lastErrno = osGetLastError();
43327 /* No need to log a failure to lock */
43328 }
43329 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
43330 return res;
43331}
43332
43333/*
43334** Undo a readlock
43335*/
43336static int winUnlockReadLock(winFile *pFile){
43337 int res;
43338 DWORD lastErrno;
43339 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
43340 if( osIsNT() ){
43341 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
43342 }
43343#ifdef SQLITE_WIN32_HAS_ANSI
43344 else{
43345 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
43346 }
43347#endif
43348 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
43349 pFile->lastErrno = lastErrno;
43350 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
43351 "winUnlockReadLock", pFile->zPath);
43352 }
43353 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
43354 return res;
43355}
43356
43357/*
43358** Lock the file with the lock specified by parameter locktype - one
43359** of the following:
43360**
43361** (1) SHARED_LOCK
43362** (2) RESERVED_LOCK
43363** (3) PENDING_LOCK
43364** (4) EXCLUSIVE_LOCK
43365**
43366** Sometimes when requesting one lock state, additional lock states
43367** are inserted in between. The locking might fail on one of the later
43368** transitions leaving the lock state different from what it started but
43369** still short of its goal. The following chart shows the allowed
43370** transitions and the inserted intermediate states:
43371**
43372** UNLOCKED -> SHARED
43373** SHARED -> RESERVED
43374** SHARED -> (PENDING) -> EXCLUSIVE
43375** RESERVED -> (PENDING) -> EXCLUSIVE
43376** PENDING -> EXCLUSIVE
43377**
43378** This routine will only increase a lock. The winUnlock() routine
43379** erases all locks at once and returns us immediately to locking level 0.
43380** It is not possible to lower the locking level one step at a time. You
43381** must go straight to locking level 0.
43382*/
43383static int winLock(sqlite3_file *id, int locktype){
43384 int rc = SQLITE_OK; /* Return code from subroutines */
43385 int res = 1; /* Result of a Windows lock call */
43386 int newLocktype; /* Set pFile->locktype to this value before exiting */
43387 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
43388 winFile *pFile = (winFile*)id;
43389 DWORD lastErrno = NO_ERROR;
43390
43391 assert( id!=0 );
43392 OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
43393 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
43394
43395 /* If there is already a lock of this type or more restrictive on the
43396 ** OsFile, do nothing. Don't use the end_lock: exit path, as
43397 ** sqlite3OsEnterMutex() hasn't been called yet.
43398 */
43399 if( pFile->locktype>=locktype ){
43400 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
43401 return SQLITE_OK;
43402 }
43403
43404 /* Do not allow any kind of write-lock on a read-only database
43405 */
43406 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
43407 return SQLITE_IOERR_LOCK;
43408 }
43409
43410 /* Make sure the locking sequence is correct
43411 */
43412 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
43413 assert( locktype!=PENDING_LOCK );
43414 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
43415
43416 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
43417 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
43418 ** the PENDING_LOCK byte is temporary.
43419 */
43420 newLocktype = pFile->locktype;
43421 if( pFile->locktype==NO_LOCK
43422 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
43423 ){
43424 int cnt = 3;
43425 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
43426 PENDING_BYTE, 0, 1, 0))==0 ){
43427 /* Try 3 times to get the pending lock. This is needed to work
43428 ** around problems caused by indexing and/or anti-virus software on
43429 ** Windows systems.
43430 ** If you are using this code as a model for alternative VFSes, do not
43431 ** copy this retry logic. It is a hack intended for Windows only.
43432 */
43433 lastErrno = osGetLastError();
43434 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
43435 pFile->h, cnt, res));
43436 if( lastErrno==ERROR_INVALID_HANDLE ){
43437 pFile->lastErrno = lastErrno;
43438 rc = SQLITE_IOERR_LOCK;
43439 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
43440 pFile->h, cnt, sqlite3ErrName(rc)));
43441 return rc;
43442 }
43443 if( cnt ) sqlite3_win32_sleep(1);
43444 }
43445 gotPendingLock = res;
43446 if( !res ){
43447 lastErrno = osGetLastError();
43448 }
43449 }
43450
43451 /* Acquire a shared lock
43452 */
43453 if( locktype==SHARED_LOCK && res ){
43454 assert( pFile->locktype==NO_LOCK );
43455 res = winGetReadLock(pFile);
43456 if( res ){
43457 newLocktype = SHARED_LOCK;
43458 }else{
43459 lastErrno = osGetLastError();
43460 }
43461 }
43462
43463 /* Acquire a RESERVED lock
43464 */
43465 if( locktype==RESERVED_LOCK && res ){
43466 assert( pFile->locktype==SHARED_LOCK );
43467 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
43468 if( res ){
43469 newLocktype = RESERVED_LOCK;
43470 }else{
43471 lastErrno = osGetLastError();
43472 }
43473 }
43474
43475 /* Acquire a PENDING lock
43476 */
43477 if( locktype==EXCLUSIVE_LOCK && res ){
43478 newLocktype = PENDING_LOCK;
43479 gotPendingLock = 0;
43480 }
43481
43482 /* Acquire an EXCLUSIVE lock
43483 */
43484 if( locktype==EXCLUSIVE_LOCK && res ){
43485 assert( pFile->locktype>=SHARED_LOCK );
43486 res = winUnlockReadLock(pFile);
43487 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
43488 SHARED_SIZE, 0);
43489 if( res ){
43490 newLocktype = EXCLUSIVE_LOCK;
43491 }else{
43492 lastErrno = osGetLastError();
43493 winGetReadLock(pFile);
43494 }
43495 }
43496
43497 /* If we are holding a PENDING lock that ought to be released, then
43498 ** release it now.
43499 */
43500 if( gotPendingLock && locktype==SHARED_LOCK ){
43501 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
43502 }
43503
43504 /* Update the state of the lock has held in the file descriptor then
43505 ** return the appropriate result code.
43506 */
43507 if( res ){
43508 rc = SQLITE_OK;
43509 }else{
43510 pFile->lastErrno = lastErrno;
43511 rc = SQLITE_BUSY;
43512 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
43513 pFile->h, locktype, newLocktype));
43514 }
43515 pFile->locktype = (u8)newLocktype;
43516 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
43517 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
43518 return rc;
43519}
43520
43521/*
43522** This routine checks if there is a RESERVED lock held on the specified
43523** file by this or any other process. If such a lock is held, return
43524** non-zero, otherwise zero.
43525*/
43526static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
43527 int res;
43528 winFile *pFile = (winFile*)id;
43529
43530 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
43531 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
43532
43533 assert( id!=0 );
43534 if( pFile->locktype>=RESERVED_LOCK ){
43535 res = 1;
43536 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
43537 }else{
43538 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
43539 if( res ){
43540 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
43541 }
43542 res = !res;
43543 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
43544 }
43545 *pResOut = res;
43546 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
43547 pFile->h, pResOut, *pResOut));
43548 return SQLITE_OK;
43549}
43550
43551/*
43552** Lower the locking level on file descriptor id to locktype. locktype
43553** must be either NO_LOCK or SHARED_LOCK.
43554**
43555** If the locking level of the file descriptor is already at or below
43556** the requested locking level, this routine is a no-op.
43557**
43558** It is not possible for this routine to fail if the second argument
43559** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
43560** might return SQLITE_IOERR;
43561*/
43562static int winUnlock(sqlite3_file *id, int locktype){
43563 int type;
43564 winFile *pFile = (winFile*)id;
43565 int rc = SQLITE_OK;
43566 assert( pFile!=0 );
43567 assert( locktype<=SHARED_LOCK );
43568 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
43569 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
43570 type = pFile->locktype;
43571 if( type>=EXCLUSIVE_LOCK ){
43572 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
43573 if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
43574 /* This should never happen. We should always be able to
43575 ** reacquire the read lock */
43576 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
43577 "winUnlock", pFile->zPath);
43578 }
43579 }
43580 if( type>=RESERVED_LOCK ){
43581 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
43582 }
43583 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
43584 winUnlockReadLock(pFile);
43585 }
43586 if( type>=PENDING_LOCK ){
43587 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
43588 }
43589 pFile->locktype = (u8)locktype;
43590 OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
43591 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
43592 return rc;
43593}
43594
43595/******************************************************************************
43596****************************** No-op Locking **********************************
43597**
43598** Of the various locking implementations available, this is by far the
43599** simplest: locking is ignored. No attempt is made to lock the database
43600** file for reading or writing.
43601**
43602** This locking mode is appropriate for use on read-only databases
43603** (ex: databases that are burned into CD-ROM, for example.) It can
43604** also be used if the application employs some external mechanism to
43605** prevent simultaneous access of the same database by two or more
43606** database connections. But there is a serious risk of database
43607** corruption if this locking mode is used in situations where multiple
43608** database connections are accessing the same database file at the same
43609** time and one or more of those connections are writing.
43610*/
43611
43612static int winNolockLock(sqlite3_file *id, int locktype){
43613 UNUSED_PARAMETER(id);
43614 UNUSED_PARAMETER(locktype);
43615 return SQLITE_OK;
43616}
43617
43618static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
43619 UNUSED_PARAMETER(id);
43620 UNUSED_PARAMETER(pResOut);
43621 return SQLITE_OK;
43622}
43623
43624static int winNolockUnlock(sqlite3_file *id, int locktype){
43625 UNUSED_PARAMETER(id);
43626 UNUSED_PARAMETER(locktype);
43627 return SQLITE_OK;
43628}
43629
43630/******************* End of the no-op lock implementation *********************
43631******************************************************************************/
43632
43633/*
43634** If *pArg is initially negative then this is a query. Set *pArg to
43635** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
43636**
43637** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
43638*/
43639static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
43640 if( *pArg<0 ){
43641 *pArg = (pFile->ctrlFlags & mask)!=0;
43642 }else if( (*pArg)==0 ){
43643 pFile->ctrlFlags &= ~mask;
43644 }else{
43645 pFile->ctrlFlags |= mask;
43646 }
43647}
43648
43649/* Forward references to VFS helper methods used for temporary files */
43650static int winGetTempname(sqlite3_vfs *, char **);
43651static int winIsDir(const void *);
43652static BOOL winIsDriveLetterAndColon(const char *);
43653
43654/*
43655** Control and query of the open file handle.
43656*/
43657static int winFileControl(sqlite3_file *id, int op, void *pArg){
43658 winFile *pFile = (winFile*)id;
43659 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
43660 switch( op ){
43661 case SQLITE_FCNTL_LOCKSTATE: {
43662 *(int*)pArg = pFile->locktype;
43663 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43664 return SQLITE_OK;
43665 }
43666 case SQLITE_FCNTL_LAST_ERRNO: {
43667 *(int*)pArg = (int)pFile->lastErrno;
43668 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43669 return SQLITE_OK;
43670 }
43671 case SQLITE_FCNTL_CHUNK_SIZE: {
43672 pFile->szChunk = *(int *)pArg;
43673 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43674 return SQLITE_OK;
43675 }
43676 case SQLITE_FCNTL_SIZE_HINT: {
43677 if( pFile->szChunk>0 ){
43678 sqlite3_int64 oldSz;
43679 int rc = winFileSize(id, &oldSz);
43680 if( rc==SQLITE_OK ){
43681 sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
43682 if( newSz>oldSz ){
43683 SimulateIOErrorBenign(1);
43684 rc = winTruncate(id, newSz);
43685 SimulateIOErrorBenign(0);
43686 }
43687 }
43688 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
43689 return rc;
43690 }
43691 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43692 return SQLITE_OK;
43693 }
43694 case SQLITE_FCNTL_PERSIST_WAL: {
43695 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
43696 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43697 return SQLITE_OK;
43698 }
43699 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
43700 winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
43701 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43702 return SQLITE_OK;
43703 }
43704 case SQLITE_FCNTL_VFSNAME: {
43705 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
43706 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43707 return SQLITE_OK;
43708 }
43709 case SQLITE_FCNTL_WIN32_AV_RETRY: {
43710 int *a = (int*)pArg;
43711 if( a[0]>0 ){
43712 winIoerrRetry = a[0];
43713 }else{
43714 a[0] = winIoerrRetry;
43715 }
43716 if( a[1]>0 ){
43717 winIoerrRetryDelay = a[1];
43718 }else{
43719 a[1] = winIoerrRetryDelay;
43720 }
43721 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43722 return SQLITE_OK;
43723 }
43724 case SQLITE_FCNTL_WIN32_GET_HANDLE: {
43725 LPHANDLE phFile = (LPHANDLE)pArg;
43726 *phFile = pFile->h;
43727 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
43728 return SQLITE_OK;
43729 }
43730#ifdef SQLITE_TEST
43731 case SQLITE_FCNTL_WIN32_SET_HANDLE: {
43732 LPHANDLE phFile = (LPHANDLE)pArg;
43733 HANDLE hOldFile = pFile->h;
43734 pFile->h = *phFile;
43735 *phFile = hOldFile;
43736 OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
43737 hOldFile, pFile->h));
43738 return SQLITE_OK;
43739 }
43740#endif
43741 case SQLITE_FCNTL_TEMPFILENAME: {
43742 char *zTFile = 0;
43743 int rc = winGetTempname(pFile->pVfs, &zTFile);
43744 if( rc==SQLITE_OK ){
43745 *(char**)pArg = zTFile;
43746 }
43747 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
43748 return rc;
43749 }
43750#if SQLITE_MAX_MMAP_SIZE>0
43751 case SQLITE_FCNTL_MMAP_SIZE: {
43752 i64 newLimit = *(i64*)pArg;
43753 int rc = SQLITE_OK;
43754 if( newLimit>sqlite3GlobalConfig.mxMmap ){
43755 newLimit = sqlite3GlobalConfig.mxMmap;
43756 }
43757
43758 /* The value of newLimit may be eventually cast to (SIZE_T) and passed
43759 ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
43760 ** least a 64-bit type. */
43761 if( newLimit>0 && sizeof(SIZE_T)<8 ){
43762 newLimit = (newLimit & 0x7FFFFFFF);
43763 }
43764
43765 *(i64*)pArg = pFile->mmapSizeMax;
43766 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
43767 pFile->mmapSizeMax = newLimit;
43768 if( pFile->mmapSize>0 ){
43769 winUnmapfile(pFile);
43770 rc = winMapfile(pFile, -1);
43771 }
43772 }
43773 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
43774 return rc;
43775 }
43776#endif
43777 }
43778 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
43779 return SQLITE_NOTFOUND;
43780}
43781
43782/*
43783** Return the sector size in bytes of the underlying block device for
43784** the specified file. This is almost always 512 bytes, but may be
43785** larger for some devices.
43786**
43787** SQLite code assumes this function cannot fail. It also assumes that
43788** if two files are created in the same file-system directory (i.e.
43789** a database and its journal file) that the sector size will be the
43790** same for both.
43791*/
43792static int winSectorSize(sqlite3_file *id){
43793 (void)id;
43794 return SQLITE_DEFAULT_SECTOR_SIZE;
43795}
43796
43797/*
43798** Return a vector of device characteristics.
43799*/
43800static int winDeviceCharacteristics(sqlite3_file *id){
43801 winFile *p = (winFile*)id;
43802 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
43803 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
43804}
43805
43806/*
43807** Windows will only let you create file view mappings
43808** on allocation size granularity boundaries.
43809** During sqlite3_os_init() we do a GetSystemInfo()
43810** to get the granularity size.
43811*/
43812static SYSTEM_INFO winSysInfo;
43813
43814#ifndef SQLITE_OMIT_WAL
43815
43816/*
43817** Helper functions to obtain and relinquish the global mutex. The
43818** global mutex is used to protect the winLockInfo objects used by
43819** this file, all of which may be shared by multiple threads.
43820**
43821** Function winShmMutexHeld() is used to assert() that the global mutex
43822** is held when required. This function is only used as part of assert()
43823** statements. e.g.
43824**
43825** winShmEnterMutex()
43826** assert( winShmMutexHeld() );
43827** winShmLeaveMutex()
43828*/
43829static sqlite3_mutex *winBigLock = 0;
43830static void winShmEnterMutex(void){
43831 sqlite3_mutex_enter(winBigLock);
43832}
43833static void winShmLeaveMutex(void){
43834 sqlite3_mutex_leave(winBigLock);
43835}
43836#ifndef NDEBUG
43837static int winShmMutexHeld(void) {
43838 return sqlite3_mutex_held(winBigLock);
43839}
43840#endif
43841
43842/*
43843** Object used to represent a single file opened and mmapped to provide
43844** shared memory. When multiple threads all reference the same
43845** log-summary, each thread has its own winFile object, but they all
43846** point to a single instance of this object. In other words, each
43847** log-summary is opened only once per process.
43848**
43849** winShmMutexHeld() must be true when creating or destroying
43850** this object or while reading or writing the following fields:
43851**
43852** nRef
43853** pNext
43854**
43855** The following fields are read-only after the object is created:
43856**
43857** fid
43858** zFilename
43859**
43860** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
43861** winShmMutexHeld() is true when reading or writing any other field
43862** in this structure.
43863**
43864*/
43865struct winShmNode {
43866 sqlite3_mutex *mutex; /* Mutex to access this object */
43867 char *zFilename; /* Name of the file */
43868 winFile hFile; /* File handle from winOpen */
43869
43870 int szRegion; /* Size of shared-memory regions */
43871 int nRegion; /* Size of array apRegion */
43872 u8 isReadonly; /* True if read-only */
43873 u8 isUnlocked; /* True if no DMS lock held */
43874
43875 struct ShmRegion {
43876 HANDLE hMap; /* File handle from CreateFileMapping */
43877 void *pMap;
43878 } *aRegion;
43879 DWORD lastErrno; /* The Windows errno from the last I/O error */
43880
43881 int nRef; /* Number of winShm objects pointing to this */
43882 winShm *pFirst; /* All winShm objects pointing to this */
43883 winShmNode *pNext; /* Next in list of all winShmNode objects */
43884#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
43885 u8 nextShmId; /* Next available winShm.id value */
43886#endif
43887};
43888
43889/*
43890** A global array of all winShmNode objects.
43891**
43892** The winShmMutexHeld() must be true while reading or writing this list.
43893*/
43894static winShmNode *winShmNodeList = 0;
43895
43896/*
43897** Structure used internally by this VFS to record the state of an
43898** open shared memory connection.
43899**
43900** The following fields are initialized when this object is created and
43901** are read-only thereafter:
43902**
43903** winShm.pShmNode
43904** winShm.id
43905**
43906** All other fields are read/write. The winShm.pShmNode->mutex must be held
43907** while accessing any read/write fields.
43908*/
43909struct winShm {
43910 winShmNode *pShmNode; /* The underlying winShmNode object */
43911 winShm *pNext; /* Next winShm with the same winShmNode */
43912 u8 hasMutex; /* True if holding the winShmNode mutex */
43913 u16 sharedMask; /* Mask of shared locks held */
43914 u16 exclMask; /* Mask of exclusive locks held */
43915#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
43916 u8 id; /* Id of this connection with its winShmNode */
43917#endif
43918};
43919
43920/*
43921** Constants used for locking
43922*/
43923#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
43924#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
43925
43926/*
43927** Apply advisory locks for all n bytes beginning at ofst.
43928*/
43929#define WINSHM_UNLCK 1
43930#define WINSHM_RDLCK 2
43931#define WINSHM_WRLCK 3
43932static int winShmSystemLock(
43933 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
43934 int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
43935 int ofst, /* Offset to first byte to be locked/unlocked */
43936 int nByte /* Number of bytes to lock or unlock */
43937){
43938 int rc = 0; /* Result code form Lock/UnlockFileEx() */
43939
43940 /* Access to the winShmNode object is serialized by the caller */
43941 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
43942
43943 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
43944 pFile->hFile.h, lockType, ofst, nByte));
43945
43946 /* Release/Acquire the system-level lock */
43947 if( lockType==WINSHM_UNLCK ){
43948 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
43949 }else{
43950 /* Initialize the locking parameters */
43951 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
43952 if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
43953 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
43954 }
43955
43956 if( rc!= 0 ){
43957 rc = SQLITE_OK;
43958 }else{
43959 pFile->lastErrno = osGetLastError();
43960 rc = SQLITE_BUSY;
43961 }
43962
43963 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
43964 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
43965 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
43966
43967 return rc;
43968}
43969
43970/* Forward references to VFS methods */
43971static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
43972static int winDelete(sqlite3_vfs *,const char*,int);
43973
43974/*
43975** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
43976**
43977** This is not a VFS shared-memory method; it is a utility function called
43978** by VFS shared-memory methods.
43979*/
43980static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
43981 winShmNode **pp;
43982 winShmNode *p;
43983 assert( winShmMutexHeld() );
43984 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
43985 osGetCurrentProcessId(), deleteFlag));
43986 pp = &winShmNodeList;
43987 while( (p = *pp)!=0 ){
43988 if( p->nRef==0 ){
43989 int i;
43990 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
43991 for(i=0; i<p->nRegion; i++){
43992 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
43993 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
43994 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
43995 UNUSED_VARIABLE_VALUE(bRc);
43996 bRc = osCloseHandle(p->aRegion[i].hMap);
43997 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
43998 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
43999 UNUSED_VARIABLE_VALUE(bRc);
44000 }
44001 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
44002 SimulateIOErrorBenign(1);
44003 winClose((sqlite3_file *)&p->hFile);
44004 SimulateIOErrorBenign(0);
44005 }
44006 if( deleteFlag ){
44007 SimulateIOErrorBenign(1);
44008 sqlite3BeginBenignMalloc();
44009 winDelete(pVfs, p->zFilename, 0);
44010 sqlite3EndBenignMalloc();
44011 SimulateIOErrorBenign(0);
44012 }
44013 *pp = p->pNext;
44014 sqlite3_free(p->aRegion);
44015 sqlite3_free(p);
44016 }else{
44017 pp = &p->pNext;
44018 }
44019 }
44020}
44021
44022/*
44023** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
44024** take it now. Return SQLITE_OK if successful, or an SQLite error
44025** code otherwise.
44026**
44027** If the DMS cannot be locked because this is a readonly_shm=1
44028** connection and no other process already holds a lock, return
44029** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
44030*/
44031static int winLockSharedMemory(winShmNode *pShmNode){
44032 int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
44033
44034 if( rc==SQLITE_OK ){
44035 if( pShmNode->isReadonly ){
44036 pShmNode->isUnlocked = 1;
44037 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
44038 return SQLITE_READONLY_CANTINIT;
44039 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
44040 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
44041 return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
44042 "winLockSharedMemory", pShmNode->zFilename);
44043 }
44044 }
44045
44046 if( rc==SQLITE_OK ){
44047 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
44048 }
44049
44050 return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
44051}
44052
44053/*
44054** Open the shared-memory area associated with database file pDbFd.
44055**
44056** When opening a new shared-memory file, if no other instances of that
44057** file are currently open, in this process or in other processes, then
44058** the file must be truncated to zero length or have its header cleared.
44059*/
44060static int winOpenSharedMemory(winFile *pDbFd){
44061 struct winShm *p; /* The connection to be opened */
44062 winShmNode *pShmNode = 0; /* The underlying mmapped file */
44063 int rc = SQLITE_OK; /* Result code */
44064 winShmNode *pNew; /* Newly allocated winShmNode */
44065 int nName; /* Size of zName in bytes */
44066
44067 assert( pDbFd->pShm==0 ); /* Not previously opened */
44068
44069 /* Allocate space for the new sqlite3_shm object. Also speculatively
44070 ** allocate space for a new winShmNode and filename.
44071 */
44072 p = sqlite3MallocZero( sizeof(*p) );
44073 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
44074 nName = sqlite3Strlen30(pDbFd->zPath);
44075 pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
44076 if( pNew==0 ){
44077 sqlite3_free(p);
44078 return SQLITE_IOERR_NOMEM_BKPT;
44079 }
44080 pNew->zFilename = (char*)&pNew[1];
44081 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
44082 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
44083
44084 /* Look to see if there is an existing winShmNode that can be used.
44085 ** If no matching winShmNode currently exists, create a new one.
44086 */
44087 winShmEnterMutex();
44088 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
44089 /* TBD need to come up with better match here. Perhaps
44090 ** use FILE_ID_BOTH_DIR_INFO Structure.
44091 */
44092 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
44093 }
44094 if( pShmNode ){
44095 sqlite3_free(pNew);
44096 }else{
44097 int inFlags = SQLITE_OPEN_WAL;
44098 int outFlags = 0;
44099
44100 pShmNode = pNew;
44101 pNew = 0;
44102 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
44103 pShmNode->pNext = winShmNodeList;
44104 winShmNodeList = pShmNode;
44105
44106 if( sqlite3GlobalConfig.bCoreMutex ){
44107 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
44108 if( pShmNode->mutex==0 ){
44109 rc = SQLITE_IOERR_NOMEM_BKPT;
44110 goto shm_open_err;
44111 }
44112 }
44113
44114 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
44115 inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
44116 }else{
44117 inFlags |= SQLITE_OPEN_READONLY;
44118 }
44119 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
44120 (sqlite3_file*)&pShmNode->hFile,
44121 inFlags, &outFlags);
44122 if( rc!=SQLITE_OK ){
44123 rc = winLogError(rc, osGetLastError(), "winOpenShm",
44124 pShmNode->zFilename);
44125 goto shm_open_err;
44126 }
44127 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
44128
44129 rc = winLockSharedMemory(pShmNode);
44130 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
44131 }
44132
44133 /* Make the new connection a child of the winShmNode */
44134 p->pShmNode = pShmNode;
44135#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
44136 p->id = pShmNode->nextShmId++;
44137#endif
44138 pShmNode->nRef++;
44139 pDbFd->pShm = p;
44140 winShmLeaveMutex();
44141
44142 /* The reference count on pShmNode has already been incremented under
44143 ** the cover of the winShmEnterMutex() mutex and the pointer from the
44144 ** new (struct winShm) object to the pShmNode has been set. All that is
44145 ** left to do is to link the new object into the linked list starting
44146 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
44147 ** mutex.
44148 */
44149 sqlite3_mutex_enter(pShmNode->mutex);
44150 p->pNext = pShmNode->pFirst;
44151 pShmNode->pFirst = p;
44152 sqlite3_mutex_leave(pShmNode->mutex);
44153 return rc;
44154
44155 /* Jump here on any error */
44156shm_open_err:
44157 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
44158 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
44159 sqlite3_free(p);
44160 sqlite3_free(pNew);
44161 winShmLeaveMutex();
44162 return rc;
44163}
44164
44165/*
44166** Close a connection to shared-memory. Delete the underlying
44167** storage if deleteFlag is true.
44168*/
44169static int winShmUnmap(
44170 sqlite3_file *fd, /* Database holding shared memory */
44171 int deleteFlag /* Delete after closing if true */
44172){
44173 winFile *pDbFd; /* Database holding shared-memory */
44174 winShm *p; /* The connection to be closed */
44175 winShmNode *pShmNode; /* The underlying shared-memory file */
44176 winShm **pp; /* For looping over sibling connections */
44177
44178 pDbFd = (winFile*)fd;
44179 p = pDbFd->pShm;
44180 if( p==0 ) return SQLITE_OK;
44181 pShmNode = p->pShmNode;
44182
44183 /* Remove connection p from the set of connections associated
44184 ** with pShmNode */
44185 sqlite3_mutex_enter(pShmNode->mutex);
44186 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
44187 *pp = p->pNext;
44188
44189 /* Free the connection p */
44190 sqlite3_free(p);
44191 pDbFd->pShm = 0;
44192 sqlite3_mutex_leave(pShmNode->mutex);
44193
44194 /* If pShmNode->nRef has reached 0, then close the underlying
44195 ** shared-memory file, too */
44196 winShmEnterMutex();
44197 assert( pShmNode->nRef>0 );
44198 pShmNode->nRef--;
44199 if( pShmNode->nRef==0 ){
44200 winShmPurge(pDbFd->pVfs, deleteFlag);
44201 }
44202 winShmLeaveMutex();
44203
44204 return SQLITE_OK;
44205}
44206
44207/*
44208** Change the lock state for a shared-memory segment.
44209*/
44210static int winShmLock(
44211 sqlite3_file *fd, /* Database file holding the shared memory */
44212 int ofst, /* First lock to acquire or release */
44213 int n, /* Number of locks to acquire or release */
44214 int flags /* What to do with the lock */
44215){
44216 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
44217 winShm *p = pDbFd->pShm; /* The shared memory being locked */
44218 winShm *pX; /* For looping over all siblings */
44219 winShmNode *pShmNode = p->pShmNode;
44220 int rc = SQLITE_OK; /* Result code */
44221 u16 mask; /* Mask of locks to take or release */
44222
44223 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
44224 assert( n>=1 );
44225 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
44226 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
44227 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
44228 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
44229 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
44230
44231 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
44232 assert( n>1 || mask==(1<<ofst) );
44233 sqlite3_mutex_enter(pShmNode->mutex);
44234 if( flags & SQLITE_SHM_UNLOCK ){
44235 u16 allMask = 0; /* Mask of locks held by siblings */
44236
44237 /* See if any siblings hold this same lock */
44238 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
44239 if( pX==p ) continue;
44240 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
44241 allMask |= pX->sharedMask;
44242 }
44243
44244 /* Unlock the system-level locks */
44245 if( (mask & allMask)==0 ){
44246 rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
44247 }else{
44248 rc = SQLITE_OK;
44249 }
44250
44251 /* Undo the local locks */
44252 if( rc==SQLITE_OK ){
44253 p->exclMask &= ~mask;
44254 p->sharedMask &= ~mask;
44255 }
44256 }else if( flags & SQLITE_SHM_SHARED ){
44257 u16 allShared = 0; /* Union of locks held by connections other than "p" */
44258
44259 /* Find out which shared locks are already held by sibling connections.
44260 ** If any sibling already holds an exclusive lock, go ahead and return
44261 ** SQLITE_BUSY.
44262 */
44263 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
44264 if( (pX->exclMask & mask)!=0 ){
44265 rc = SQLITE_BUSY;
44266 break;
44267 }
44268 allShared |= pX->sharedMask;
44269 }
44270
44271 /* Get shared locks at the system level, if necessary */
44272 if( rc==SQLITE_OK ){
44273 if( (allShared & mask)==0 ){
44274 rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
44275 }else{
44276 rc = SQLITE_OK;
44277 }
44278 }
44279
44280 /* Get the local shared locks */
44281 if( rc==SQLITE_OK ){
44282 p->sharedMask |= mask;
44283 }
44284 }else{
44285 /* Make sure no sibling connections hold locks that will block this
44286 ** lock. If any do, return SQLITE_BUSY right away.
44287 */
44288 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
44289 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
44290 rc = SQLITE_BUSY;
44291 break;
44292 }
44293 }
44294
44295 /* Get the exclusive locks at the system level. Then if successful
44296 ** also mark the local connection as being locked.
44297 */
44298 if( rc==SQLITE_OK ){
44299 rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
44300 if( rc==SQLITE_OK ){
44301 assert( (p->sharedMask & mask)==0 );
44302 p->exclMask |= mask;
44303 }
44304 }
44305 }
44306 sqlite3_mutex_leave(pShmNode->mutex);
44307 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
44308 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
44309 sqlite3ErrName(rc)));
44310 return rc;
44311}
44312
44313/*
44314** Implement a memory barrier or memory fence on shared memory.
44315**
44316** All loads and stores begun before the barrier must complete before
44317** any load or store begun after the barrier.
44318*/
44319static void winShmBarrier(
44320 sqlite3_file *fd /* Database holding the shared memory */
44321){
44322 UNUSED_PARAMETER(fd);
44323 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
44324 winShmEnterMutex(); /* Also mutex, for redundancy */
44325 winShmLeaveMutex();
44326}
44327
44328/*
44329** This function is called to obtain a pointer to region iRegion of the
44330** shared-memory associated with the database file fd. Shared-memory regions
44331** are numbered starting from zero. Each shared-memory region is szRegion
44332** bytes in size.
44333**
44334** If an error occurs, an error code is returned and *pp is set to NULL.
44335**
44336** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
44337** region has not been allocated (by any client, including one running in a
44338** separate process), then *pp is set to NULL and SQLITE_OK returned. If
44339** isWrite is non-zero and the requested shared-memory region has not yet
44340** been allocated, it is allocated by this function.
44341**
44342** If the shared-memory region has already been allocated or is allocated by
44343** this call as described above, then it is mapped into this processes
44344** address space (if it is not already), *pp is set to point to the mapped
44345** memory and SQLITE_OK returned.
44346*/
44347static int winShmMap(
44348 sqlite3_file *fd, /* Handle open on database file */
44349 int iRegion, /* Region to retrieve */
44350 int szRegion, /* Size of regions */
44351 int isWrite, /* True to extend file if necessary */
44352 void volatile **pp /* OUT: Mapped memory */
44353){
44354 winFile *pDbFd = (winFile*)fd;
44355 winShm *pShm = pDbFd->pShm;
44356 winShmNode *pShmNode;
44357 DWORD protect = PAGE_READWRITE;
44358 DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
44359 int rc = SQLITE_OK;
44360
44361 if( !pShm ){
44362 rc = winOpenSharedMemory(pDbFd);
44363 if( rc!=SQLITE_OK ) return rc;
44364 pShm = pDbFd->pShm;
44365 }
44366 pShmNode = pShm->pShmNode;
44367
44368 sqlite3_mutex_enter(pShmNode->mutex);
44369 if( pShmNode->isUnlocked ){
44370 rc = winLockSharedMemory(pShmNode);
44371 if( rc!=SQLITE_OK ) goto shmpage_out;
44372 pShmNode->isUnlocked = 0;
44373 }
44374 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
44375
44376 if( pShmNode->nRegion<=iRegion ){
44377 struct ShmRegion *apNew; /* New aRegion[] array */
44378 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
44379 sqlite3_int64 sz; /* Current size of wal-index file */
44380
44381 pShmNode->szRegion = szRegion;
44382
44383 /* The requested region is not mapped into this processes address space.
44384 ** Check to see if it has been allocated (i.e. if the wal-index file is
44385 ** large enough to contain the requested region).
44386 */
44387 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
44388 if( rc!=SQLITE_OK ){
44389 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
44390 "winShmMap1", pDbFd->zPath);
44391 goto shmpage_out;
44392 }
44393
44394 if( sz<nByte ){
44395 /* The requested memory region does not exist. If isWrite is set to
44396 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
44397 **
44398 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
44399 ** the requested memory region.
44400 */
44401 if( !isWrite ) goto shmpage_out;
44402 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
44403 if( rc!=SQLITE_OK ){
44404 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
44405 "winShmMap2", pDbFd->zPath);
44406 goto shmpage_out;
44407 }
44408 }
44409
44410 /* Map the requested memory region into this processes address space. */
44411 apNew = (struct ShmRegion *)sqlite3_realloc64(
44412 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
44413 );
44414 if( !apNew ){
44415 rc = SQLITE_IOERR_NOMEM_BKPT;
44416 goto shmpage_out;
44417 }
44418 pShmNode->aRegion = apNew;
44419
44420 if( pShmNode->isReadonly ){
44421 protect = PAGE_READONLY;
44422 flags = FILE_MAP_READ;
44423 }
44424
44425 while( pShmNode->nRegion<=iRegion ){
44426 HANDLE hMap = NULL; /* file-mapping handle */
44427 void *pMap = 0; /* Mapped memory region */
44428
44429#if SQLITE_OS_WINRT
44430 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
44431 NULL, protect, nByte, NULL
44432 );
44433#elif defined(SQLITE_WIN32_HAS_WIDE)
44434 hMap = osCreateFileMappingW(pShmNode->hFile.h,
44435 NULL, protect, 0, nByte, NULL
44436 );
44437#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
44438 hMap = osCreateFileMappingA(pShmNode->hFile.h,
44439 NULL, protect, 0, nByte, NULL
44440 );
44441#endif
44442 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
44443 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
44444 hMap ? "ok" : "failed"));
44445 if( hMap ){
44446 int iOffset = pShmNode->nRegion*szRegion;
44447 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
44448#if SQLITE_OS_WINRT
44449 pMap = osMapViewOfFileFromApp(hMap, flags,
44450 iOffset - iOffsetShift, szRegion + iOffsetShift
44451 );
44452#else
44453 pMap = osMapViewOfFile(hMap, flags,
44454 0, iOffset - iOffsetShift, szRegion + iOffsetShift
44455 );
44456#endif
44457 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
44458 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
44459 szRegion, pMap ? "ok" : "failed"));
44460 }
44461 if( !pMap ){
44462 pShmNode->lastErrno = osGetLastError();
44463 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
44464 "winShmMap3", pDbFd->zPath);
44465 if( hMap ) osCloseHandle(hMap);
44466 goto shmpage_out;
44467 }
44468
44469 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
44470 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
44471 pShmNode->nRegion++;
44472 }
44473 }
44474
44475shmpage_out:
44476 if( pShmNode->nRegion>iRegion ){
44477 int iOffset = iRegion*szRegion;
44478 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
44479 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
44480 *pp = (void *)&p[iOffsetShift];
44481 }else{
44482 *pp = 0;
44483 }
44484 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
44485 sqlite3_mutex_leave(pShmNode->mutex);
44486 return rc;
44487}
44488
44489#else
44490# define winShmMap 0
44491# define winShmLock 0
44492# define winShmBarrier 0
44493# define winShmUnmap 0
44494#endif /* #ifndef SQLITE_OMIT_WAL */
44495
44496/*
44497** Cleans up the mapped region of the specified file, if any.
44498*/
44499#if SQLITE_MAX_MMAP_SIZE>0
44500static int winUnmapfile(winFile *pFile){
44501 assert( pFile!=0 );
44502 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
44503 "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n",
44504 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
44505 pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
44506 if( pFile->pMapRegion ){
44507 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
44508 pFile->lastErrno = osGetLastError();
44509 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
44510 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
44511 pFile->pMapRegion));
44512 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
44513 "winUnmapfile1", pFile->zPath);
44514 }
44515 pFile->pMapRegion = 0;
44516 pFile->mmapSize = 0;
44517 pFile->mmapSizeActual = 0;
44518 }
44519 if( pFile->hMap!=NULL ){
44520 if( !osCloseHandle(pFile->hMap) ){
44521 pFile->lastErrno = osGetLastError();
44522 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
44523 osGetCurrentProcessId(), pFile, pFile->hMap));
44524 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
44525 "winUnmapfile2", pFile->zPath);
44526 }
44527 pFile->hMap = NULL;
44528 }
44529 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
44530 osGetCurrentProcessId(), pFile));
44531 return SQLITE_OK;
44532}
44533
44534/*
44535** Memory map or remap the file opened by file-descriptor pFd (if the file
44536** is already mapped, the existing mapping is replaced by the new). Or, if
44537** there already exists a mapping for this file, and there are still
44538** outstanding xFetch() references to it, this function is a no-op.
44539**
44540** If parameter nByte is non-negative, then it is the requested size of
44541** the mapping to create. Otherwise, if nByte is less than zero, then the
44542** requested size is the size of the file on disk. The actual size of the
44543** created mapping is either the requested size or the value configured
44544** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
44545**
44546** SQLITE_OK is returned if no error occurs (even if the mapping is not
44547** recreated as a result of outstanding references) or an SQLite error
44548** code otherwise.
44549*/
44550static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
44551 sqlite3_int64 nMap = nByte;
44552 int rc;
44553
44554 assert( nMap>=0 || pFd->nFetchOut==0 );
44555 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
44556 osGetCurrentProcessId(), pFd, nByte));
44557
44558 if( pFd->nFetchOut>0 ) return SQLITE_OK;
44559
44560 if( nMap<0 ){
44561 rc = winFileSize((sqlite3_file*)pFd, &nMap);
44562 if( rc ){
44563 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
44564 osGetCurrentProcessId(), pFd));
44565 return SQLITE_IOERR_FSTAT;
44566 }
44567 }
44568 if( nMap>pFd->mmapSizeMax ){
44569 nMap = pFd->mmapSizeMax;
44570 }
44571 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
44572
44573 if( nMap==0 && pFd->mmapSize>0 ){
44574 winUnmapfile(pFd);
44575 }
44576 if( nMap!=pFd->mmapSize ){
44577 void *pNew = 0;
44578 DWORD protect = PAGE_READONLY;
44579 DWORD flags = FILE_MAP_READ;
44580
44581 winUnmapfile(pFd);
44582#ifdef SQLITE_MMAP_READWRITE
44583 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
44584 protect = PAGE_READWRITE;
44585 flags |= FILE_MAP_WRITE;
44586 }
44587#endif
44588#if SQLITE_OS_WINRT
44589 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
44590#elif defined(SQLITE_WIN32_HAS_WIDE)
44591 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
44592 (DWORD)((nMap>>32) & 0xffffffff),
44593 (DWORD)(nMap & 0xffffffff), NULL);
44594#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
44595 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
44596 (DWORD)((nMap>>32) & 0xffffffff),
44597 (DWORD)(nMap & 0xffffffff), NULL);
44598#endif
44599 if( pFd->hMap==NULL ){
44600 pFd->lastErrno = osGetLastError();
44601 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
44602 "winMapfile1", pFd->zPath);
44603 /* Log the error, but continue normal operation using xRead/xWrite */
44604 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
44605 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
44606 return SQLITE_OK;
44607 }
44608 assert( (nMap % winSysInfo.dwPageSize)==0 );
44609 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
44610#if SQLITE_OS_WINRT
44611 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
44612#else
44613 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
44614#endif
44615 if( pNew==NULL ){
44616 osCloseHandle(pFd->hMap);
44617 pFd->hMap = NULL;
44618 pFd->lastErrno = osGetLastError();
44619 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
44620 "winMapfile2", pFd->zPath);
44621 /* Log the error, but continue normal operation using xRead/xWrite */
44622 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
44623 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
44624 return SQLITE_OK;
44625 }
44626 pFd->pMapRegion = pNew;
44627 pFd->mmapSize = nMap;
44628 pFd->mmapSizeActual = nMap;
44629 }
44630
44631 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
44632 osGetCurrentProcessId(), pFd));
44633 return SQLITE_OK;
44634}
44635#endif /* SQLITE_MAX_MMAP_SIZE>0 */
44636
44637/*
44638** If possible, return a pointer to a mapping of file fd starting at offset
44639** iOff. The mapping must be valid for at least nAmt bytes.
44640**
44641** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
44642** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
44643** Finally, if an error does occur, return an SQLite error code. The final
44644** value of *pp is undefined in this case.
44645**
44646** If this function does return a pointer, the caller must eventually
44647** release the reference by calling winUnfetch().
44648*/
44649static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
44650#if SQLITE_MAX_MMAP_SIZE>0
44651 winFile *pFd = (winFile*)fd; /* The underlying database file */
44652#endif
44653 *pp = 0;
44654
44655 OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
44656 osGetCurrentProcessId(), fd, iOff, nAmt, pp));
44657
44658#if SQLITE_MAX_MMAP_SIZE>0
44659 if( pFd->mmapSizeMax>0 ){
44660 if( pFd->pMapRegion==0 ){
44661 int rc = winMapfile(pFd, -1);
44662 if( rc!=SQLITE_OK ){
44663 OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
44664 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
44665 return rc;
44666 }
44667 }
44668 if( pFd->mmapSize >= iOff+nAmt ){
44669 *pp = &((u8 *)pFd->pMapRegion)[iOff];
44670 pFd->nFetchOut++;
44671 }
44672 }
44673#endif
44674
44675 OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
44676 osGetCurrentProcessId(), fd, pp, *pp));
44677 return SQLITE_OK;
44678}
44679
44680/*
44681** If the third argument is non-NULL, then this function releases a
44682** reference obtained by an earlier call to winFetch(). The second
44683** argument passed to this function must be the same as the corresponding
44684** argument that was passed to the winFetch() invocation.
44685**
44686** Or, if the third argument is NULL, then this function is being called
44687** to inform the VFS layer that, according to POSIX, any existing mapping
44688** may now be invalid and should be unmapped.
44689*/
44690static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
44691#if SQLITE_MAX_MMAP_SIZE>0
44692 winFile *pFd = (winFile*)fd; /* The underlying database file */
44693
44694 /* If p==0 (unmap the entire file) then there must be no outstanding
44695 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
44696 ** then there must be at least one outstanding. */
44697 assert( (p==0)==(pFd->nFetchOut==0) );
44698
44699 /* If p!=0, it must match the iOff value. */
44700 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
44701
44702 OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
44703 osGetCurrentProcessId(), pFd, iOff, p));
44704
44705 if( p ){
44706 pFd->nFetchOut--;
44707 }else{
44708 /* FIXME: If Windows truly always prevents truncating or deleting a
44709 ** file while a mapping is held, then the following winUnmapfile() call
44710 ** is unnecessary can be omitted - potentially improving
44711 ** performance. */
44712 winUnmapfile(pFd);
44713 }
44714
44715 assert( pFd->nFetchOut>=0 );
44716#endif
44717
44718 OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
44719 osGetCurrentProcessId(), fd));
44720 return SQLITE_OK;
44721}
44722
44723/*
44724** Here ends the implementation of all sqlite3_file methods.
44725**
44726********************** End sqlite3_file Methods *******************************
44727******************************************************************************/
44728
44729/*
44730** This vector defines all the methods that can operate on an
44731** sqlite3_file for win32.
44732*/
44733static const sqlite3_io_methods winIoMethod = {
44734 3, /* iVersion */
44735 winClose, /* xClose */
44736 winRead, /* xRead */
44737 winWrite, /* xWrite */
44738 winTruncate, /* xTruncate */
44739 winSync, /* xSync */
44740 winFileSize, /* xFileSize */
44741 winLock, /* xLock */
44742 winUnlock, /* xUnlock */
44743 winCheckReservedLock, /* xCheckReservedLock */
44744 winFileControl, /* xFileControl */
44745 winSectorSize, /* xSectorSize */
44746 winDeviceCharacteristics, /* xDeviceCharacteristics */
44747 winShmMap, /* xShmMap */
44748 winShmLock, /* xShmLock */
44749 winShmBarrier, /* xShmBarrier */
44750 winShmUnmap, /* xShmUnmap */
44751 winFetch, /* xFetch */
44752 winUnfetch /* xUnfetch */
44753};
44754
44755/*
44756** This vector defines all the methods that can operate on an
44757** sqlite3_file for win32 without performing any locking.
44758*/
44759static const sqlite3_io_methods winIoNolockMethod = {
44760 3, /* iVersion */
44761 winClose, /* xClose */
44762 winRead, /* xRead */
44763 winWrite, /* xWrite */
44764 winTruncate, /* xTruncate */
44765 winSync, /* xSync */
44766 winFileSize, /* xFileSize */
44767 winNolockLock, /* xLock */
44768 winNolockUnlock, /* xUnlock */
44769 winNolockCheckReservedLock, /* xCheckReservedLock */
44770 winFileControl, /* xFileControl */
44771 winSectorSize, /* xSectorSize */
44772 winDeviceCharacteristics, /* xDeviceCharacteristics */
44773 winShmMap, /* xShmMap */
44774 winShmLock, /* xShmLock */
44775 winShmBarrier, /* xShmBarrier */
44776 winShmUnmap, /* xShmUnmap */
44777 winFetch, /* xFetch */
44778 winUnfetch /* xUnfetch */
44779};
44780
44781static winVfsAppData winAppData = {
44782 &winIoMethod, /* pMethod */
44783 0, /* pAppData */
44784 0 /* bNoLock */
44785};
44786
44787static winVfsAppData winNolockAppData = {
44788 &winIoNolockMethod, /* pMethod */
44789 0, /* pAppData */
44790 1 /* bNoLock */
44791};
44792
44793/****************************************************************************
44794**************************** sqlite3_vfs methods ****************************
44795**
44796** This division contains the implementation of methods on the
44797** sqlite3_vfs object.
44798*/
44799
44800#if defined(__CYGWIN__)
44801/*
44802** Convert a filename from whatever the underlying operating system
44803** supports for filenames into UTF-8. Space to hold the result is
44804** obtained from malloc and must be freed by the calling function.
44805*/
44806static char *winConvertToUtf8Filename(const void *zFilename){
44807 char *zConverted = 0;
44808 if( osIsNT() ){
44809 zConverted = winUnicodeToUtf8(zFilename);
44810 }
44811#ifdef SQLITE_WIN32_HAS_ANSI
44812 else{
44813 zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
44814 }
44815#endif
44816 /* caller will handle out of memory */
44817 return zConverted;
44818}
44819#endif
44820
44821/*
44822** Convert a UTF-8 filename into whatever form the underlying
44823** operating system wants filenames in. Space to hold the result
44824** is obtained from malloc and must be freed by the calling
44825** function.
44826*/
44827static void *winConvertFromUtf8Filename(const char *zFilename){
44828 void *zConverted = 0;
44829 if( osIsNT() ){
44830 zConverted = winUtf8ToUnicode(zFilename);
44831 }
44832#ifdef SQLITE_WIN32_HAS_ANSI
44833 else{
44834 zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
44835 }
44836#endif
44837 /* caller will handle out of memory */
44838 return zConverted;
44839}
44840
44841/*
44842** This function returns non-zero if the specified UTF-8 string buffer
44843** ends with a directory separator character or one was successfully
44844** added to it.
44845*/
44846static int winMakeEndInDirSep(int nBuf, char *zBuf){
44847 if( zBuf ){
44848 int nLen = sqlite3Strlen30(zBuf);
44849 if( nLen>0 ){
44850 if( winIsDirSep(zBuf[nLen-1]) ){
44851 return 1;
44852 }else if( nLen+1<nBuf ){
44853 zBuf[nLen] = winGetDirSep();
44854 zBuf[nLen+1] = '\0';
44855 return 1;
44856 }
44857 }
44858 }
44859 return 0;
44860}
44861
44862/*
44863** Create a temporary file name and store the resulting pointer into pzBuf.
44864** The pointer returned in pzBuf must be freed via sqlite3_free().
44865*/
44866static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
44867 static char zChars[] =
44868 "abcdefghijklmnopqrstuvwxyz"
44869 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
44870 "0123456789";
44871 size_t i, j;
44872 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
44873 int nMax, nBuf, nDir, nLen;
44874 char *zBuf;
44875
44876 /* It's odd to simulate an io-error here, but really this is just
44877 ** using the io-error infrastructure to test that SQLite handles this
44878 ** function failing.
44879 */
44880 SimulateIOError( return SQLITE_IOERR );
44881
44882 /* Allocate a temporary buffer to store the fully qualified file
44883 ** name for the temporary file. If this fails, we cannot continue.
44884 */
44885 nMax = pVfs->mxPathname; nBuf = nMax + 2;
44886 zBuf = sqlite3MallocZero( nBuf );
44887 if( !zBuf ){
44888 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
44889 return SQLITE_IOERR_NOMEM_BKPT;
44890 }
44891
44892 /* Figure out the effective temporary directory. First, check if one
44893 ** has been explicitly set by the application; otherwise, use the one
44894 ** configured by the operating system.
44895 */
44896 nDir = nMax - (nPre + 15);
44897 assert( nDir>0 );
44898 if( sqlite3_temp_directory ){
44899 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
44900 if( nDirLen>0 ){
44901 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
44902 nDirLen++;
44903 }
44904 if( nDirLen>nDir ){
44905 sqlite3_free(zBuf);
44906 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
44907 return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
44908 }
44909 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
44910 }
44911 }
44912#if defined(__CYGWIN__)
44913 else{
44914 static const char *azDirs[] = {
44915 0, /* getenv("SQLITE_TMPDIR") */
44916 0, /* getenv("TMPDIR") */
44917 0, /* getenv("TMP") */
44918 0, /* getenv("TEMP") */
44919 0, /* getenv("USERPROFILE") */
44920 "/var/tmp",
44921 "/usr/tmp",
44922 "/tmp",
44923 ".",
44924 0 /* List terminator */
44925 };
44926 unsigned int i;
44927 const char *zDir = 0;
44928
44929 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
44930 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
44931 if( !azDirs[2] ) azDirs[2] = getenv("TMP");
44932 if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
44933 if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
44934 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
44935 void *zConverted;
44936 if( zDir==0 ) continue;
44937 /* If the path starts with a drive letter followed by the colon
44938 ** character, assume it is already a native Win32 path; otherwise,
44939 ** it must be converted to a native Win32 path via the Cygwin API
44940 ** prior to using it.
44941 */
44942 if( winIsDriveLetterAndColon(zDir) ){
44943 zConverted = winConvertFromUtf8Filename(zDir);
44944 if( !zConverted ){
44945 sqlite3_free(zBuf);
44946 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
44947 return SQLITE_IOERR_NOMEM_BKPT;
44948 }
44949 if( winIsDir(zConverted) ){
44950 sqlite3_snprintf(nMax, zBuf, "%s", zDir);
44951 sqlite3_free(zConverted);
44952 break;
44953 }
44954 sqlite3_free(zConverted);
44955 }else{
44956 zConverted = sqlite3MallocZero( nMax+1 );
44957 if( !zConverted ){
44958 sqlite3_free(zBuf);
44959 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
44960 return SQLITE_IOERR_NOMEM_BKPT;
44961 }
44962 if( cygwin_conv_path(
44963 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
44964 zConverted, nMax+1)<0 ){
44965 sqlite3_free(zConverted);
44966 sqlite3_free(zBuf);
44967 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
44968 return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
44969 "winGetTempname2", zDir);
44970 }
44971 if( winIsDir(zConverted) ){
44972 /* At this point, we know the candidate directory exists and should
44973 ** be used. However, we may need to convert the string containing
44974 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
44975 */
44976 char *zUtf8 = winConvertToUtf8Filename(zConverted);
44977 if( !zUtf8 ){
44978 sqlite3_free(zConverted);
44979 sqlite3_free(zBuf);
44980 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
44981 return SQLITE_IOERR_NOMEM_BKPT;
44982 }
44983 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
44984 sqlite3_free(zUtf8);
44985 sqlite3_free(zConverted);
44986 break;
44987 }
44988 sqlite3_free(zConverted);
44989 }
44990 }
44991 }
44992#elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
44993 else if( osIsNT() ){
44994 char *zMulti;
44995 LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
44996 if( !zWidePath ){
44997 sqlite3_free(zBuf);
44998 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
44999 return SQLITE_IOERR_NOMEM_BKPT;
45000 }
45001 if( osGetTempPathW(nMax, zWidePath)==0 ){
45002 sqlite3_free(zWidePath);
45003 sqlite3_free(zBuf);
45004 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
45005 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
45006 "winGetTempname2", 0);
45007 }
45008 zMulti = winUnicodeToUtf8(zWidePath);
45009 if( zMulti ){
45010 sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
45011 sqlite3_free(zMulti);
45012 sqlite3_free(zWidePath);
45013 }else{
45014 sqlite3_free(zWidePath);
45015 sqlite3_free(zBuf);
45016 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45017 return SQLITE_IOERR_NOMEM_BKPT;
45018 }
45019 }
45020#ifdef SQLITE_WIN32_HAS_ANSI
45021 else{
45022 char *zUtf8;
45023 char *zMbcsPath = sqlite3MallocZero( nMax );
45024 if( !zMbcsPath ){
45025 sqlite3_free(zBuf);
45026 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45027 return SQLITE_IOERR_NOMEM_BKPT;
45028 }
45029 if( osGetTempPathA(nMax, zMbcsPath)==0 ){
45030 sqlite3_free(zBuf);
45031 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
45032 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
45033 "winGetTempname3", 0);
45034 }
45035 zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
45036 if( zUtf8 ){
45037 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
45038 sqlite3_free(zUtf8);
45039 }else{
45040 sqlite3_free(zBuf);
45041 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45042 return SQLITE_IOERR_NOMEM_BKPT;
45043 }
45044 }
45045#endif /* SQLITE_WIN32_HAS_ANSI */
45046#endif /* !SQLITE_OS_WINRT */
45047
45048 /*
45049 ** Check to make sure the temporary directory ends with an appropriate
45050 ** separator. If it does not and there is not enough space left to add
45051 ** one, fail.
45052 */
45053 if( !winMakeEndInDirSep(nDir+1, zBuf) ){
45054 sqlite3_free(zBuf);
45055 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
45056 return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
45057 }
45058
45059 /*
45060 ** Check that the output buffer is large enough for the temporary file
45061 ** name in the following format:
45062 **
45063 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
45064 **
45065 ** If not, return SQLITE_ERROR. The number 17 is used here in order to
45066 ** account for the space used by the 15 character random suffix and the
45067 ** two trailing NUL characters. The final directory separator character
45068 ** has already added if it was not already present.
45069 */
45070 nLen = sqlite3Strlen30(zBuf);
45071 if( (nLen + nPre + 17) > nBuf ){
45072 sqlite3_free(zBuf);
45073 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
45074 return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
45075 }
45076
45077 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
45078
45079 j = sqlite3Strlen30(zBuf);
45080 sqlite3_randomness(15, &zBuf[j]);
45081 for(i=0; i<15; i++, j++){
45082 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
45083 }
45084 zBuf[j] = 0;
45085 zBuf[j+1] = 0;
45086 *pzBuf = zBuf;
45087
45088 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
45089 return SQLITE_OK;
45090}
45091
45092/*
45093** Return TRUE if the named file is really a directory. Return false if
45094** it is something other than a directory, or if there is any kind of memory
45095** allocation failure.
45096*/
45097static int winIsDir(const void *zConverted){
45098 DWORD attr;
45099 int rc = 0;
45100 DWORD lastErrno;
45101
45102 if( osIsNT() ){
45103 int cnt = 0;
45104 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
45105 memset(&sAttrData, 0, sizeof(sAttrData));
45106 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
45107 GetFileExInfoStandard,
45108 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
45109 if( !rc ){
45110 return 0; /* Invalid name? */
45111 }
45112 attr = sAttrData.dwFileAttributes;
45113#if SQLITE_OS_WINCE==0
45114 }else{
45115 attr = osGetFileAttributesA((char*)zConverted);
45116#endif
45117 }
45118 return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
45119}
45120
45121/* forward reference */
45122static int winAccess(
45123 sqlite3_vfs *pVfs, /* Not used on win32 */
45124 const char *zFilename, /* Name of file to check */
45125 int flags, /* Type of test to make on this file */
45126 int *pResOut /* OUT: Result */
45127);
45128
45129/*
45130** Open a file.
45131*/
45132static int winOpen(
45133 sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */
45134 const char *zName, /* Name of the file (UTF-8) */
45135 sqlite3_file *id, /* Write the SQLite file handle here */
45136 int flags, /* Open mode flags */
45137 int *pOutFlags /* Status return flags */
45138){
45139 HANDLE h;
45140 DWORD lastErrno = 0;
45141 DWORD dwDesiredAccess;
45142 DWORD dwShareMode;
45143 DWORD dwCreationDisposition;
45144 DWORD dwFlagsAndAttributes = 0;
45145#if SQLITE_OS_WINCE
45146 int isTemp = 0;
45147#endif
45148 winVfsAppData *pAppData;
45149 winFile *pFile = (winFile*)id;
45150 void *zConverted; /* Filename in OS encoding */
45151 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
45152 int cnt = 0;
45153
45154 /* If argument zPath is a NULL pointer, this function is required to open
45155 ** a temporary file. Use this buffer to store the file name in.
45156 */
45157 char *zTmpname = 0; /* For temporary filename, if necessary. */
45158
45159 int rc = SQLITE_OK; /* Function Return Code */
45160#if !defined(NDEBUG) || SQLITE_OS_WINCE
45161 int eType = flags&0xFFFFFF00; /* Type of file to open */
45162#endif
45163
45164 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
45165 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
45166 int isCreate = (flags & SQLITE_OPEN_CREATE);
45167 int isReadonly = (flags & SQLITE_OPEN_READONLY);
45168 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
45169
45170#ifndef NDEBUG
45171 int isOpenJournal = (isCreate && (
45172 eType==SQLITE_OPEN_MASTER_JOURNAL
45173 || eType==SQLITE_OPEN_MAIN_JOURNAL
45174 || eType==SQLITE_OPEN_WAL
45175 ));
45176#endif
45177
45178 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
45179 zUtf8Name, id, flags, pOutFlags));
45180
45181 /* Check the following statements are true:
45182 **
45183 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
45184 ** (b) if CREATE is set, then READWRITE must also be set, and
45185 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
45186 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
45187 */
45188 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
45189 assert(isCreate==0 || isReadWrite);
45190 assert(isExclusive==0 || isCreate);
45191 assert(isDelete==0 || isCreate);
45192
45193 /* The main DB, main journal, WAL file and master journal are never
45194 ** automatically deleted. Nor are they ever temporary files. */
45195 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
45196 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
45197 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
45198 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
45199
45200 /* Assert that the upper layer has set one of the "file-type" flags. */
45201 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
45202 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
45203 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
45204 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
45205 );
45206
45207 assert( pFile!=0 );
45208 memset(pFile, 0, sizeof(winFile));
45209 pFile->h = INVALID_HANDLE_VALUE;
45210
45211#if SQLITE_OS_WINRT
45212 if( !zUtf8Name && !sqlite3_temp_directory ){
45213 sqlite3_log(SQLITE_ERROR,
45214 "sqlite3_temp_directory variable should be set for WinRT");
45215 }
45216#endif
45217
45218 /* If the second argument to this function is NULL, generate a
45219 ** temporary file name to use
45220 */
45221 if( !zUtf8Name ){
45222 assert( isDelete && !isOpenJournal );
45223 rc = winGetTempname(pVfs, &zTmpname);
45224 if( rc!=SQLITE_OK ){
45225 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
45226 return rc;
45227 }
45228 zUtf8Name = zTmpname;
45229 }
45230
45231 /* Database filenames are double-zero terminated if they are not
45232 ** URIs with parameters. Hence, they can always be passed into
45233 ** sqlite3_uri_parameter().
45234 */
45235 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
45236 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
45237
45238 /* Convert the filename to the system encoding. */
45239 zConverted = winConvertFromUtf8Filename(zUtf8Name);
45240 if( zConverted==0 ){
45241 sqlite3_free(zTmpname);
45242 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
45243 return SQLITE_IOERR_NOMEM_BKPT;
45244 }
45245
45246 if( winIsDir(zConverted) ){
45247 sqlite3_free(zConverted);
45248 sqlite3_free(zTmpname);
45249 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
45250 return SQLITE_CANTOPEN_ISDIR;
45251 }
45252
45253 if( isReadWrite ){
45254 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
45255 }else{
45256 dwDesiredAccess = GENERIC_READ;
45257 }
45258
45259 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
45260 ** created. SQLite doesn't use it to indicate "exclusive access"
45261 ** as it is usually understood.
45262 */
45263 if( isExclusive ){
45264 /* Creates a new file, only if it does not already exist. */
45265 /* If the file exists, it fails. */
45266 dwCreationDisposition = CREATE_NEW;
45267 }else if( isCreate ){
45268 /* Open existing file, or create if it doesn't exist */
45269 dwCreationDisposition = OPEN_ALWAYS;
45270 }else{
45271 /* Opens a file, only if it exists. */
45272 dwCreationDisposition = OPEN_EXISTING;
45273 }
45274
45275 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
45276
45277 if( isDelete ){
45278#if SQLITE_OS_WINCE
45279 dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
45280 isTemp = 1;
45281#else
45282 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
45283 | FILE_ATTRIBUTE_HIDDEN
45284 | FILE_FLAG_DELETE_ON_CLOSE;
45285#endif
45286 }else{
45287 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
45288 }
45289 /* Reports from the internet are that performance is always
45290 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
45291#if SQLITE_OS_WINCE
45292 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
45293#endif
45294
45295 if( osIsNT() ){
45296#if SQLITE_OS_WINRT
45297 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
45298 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
45299 extendedParameters.dwFileAttributes =
45300 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
45301 extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
45302 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
45303 extendedParameters.lpSecurityAttributes = NULL;
45304 extendedParameters.hTemplateFile = NULL;
45305 do{
45306 h = osCreateFile2((LPCWSTR)zConverted,
45307 dwDesiredAccess,
45308 dwShareMode,
45309 dwCreationDisposition,
45310 &extendedParameters);
45311 if( h!=INVALID_HANDLE_VALUE ) break;
45312 if( isReadWrite ){
45313 int rc2, isRO = 0;
45314 sqlite3BeginBenignMalloc();
45315 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
45316 sqlite3EndBenignMalloc();
45317 if( rc2==SQLITE_OK && isRO ) break;
45318 }
45319 }while( winRetryIoerr(&cnt, &lastErrno) );
45320#else
45321 do{
45322 h = osCreateFileW((LPCWSTR)zConverted,
45323 dwDesiredAccess,
45324 dwShareMode, NULL,
45325 dwCreationDisposition,
45326 dwFlagsAndAttributes,
45327 NULL);
45328 if( h!=INVALID_HANDLE_VALUE ) break;
45329 if( isReadWrite ){
45330 int rc2, isRO = 0;
45331 sqlite3BeginBenignMalloc();
45332 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
45333 sqlite3EndBenignMalloc();
45334 if( rc2==SQLITE_OK && isRO ) break;
45335 }
45336 }while( winRetryIoerr(&cnt, &lastErrno) );
45337#endif
45338 }
45339#ifdef SQLITE_WIN32_HAS_ANSI
45340 else{
45341 do{
45342 h = osCreateFileA((LPCSTR)zConverted,
45343 dwDesiredAccess,
45344 dwShareMode, NULL,
45345 dwCreationDisposition,
45346 dwFlagsAndAttributes,
45347 NULL);
45348 if( h!=INVALID_HANDLE_VALUE ) break;
45349 if( isReadWrite ){
45350 int rc2, isRO = 0;
45351 sqlite3BeginBenignMalloc();
45352 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
45353 sqlite3EndBenignMalloc();
45354 if( rc2==SQLITE_OK && isRO ) break;
45355 }
45356 }while( winRetryIoerr(&cnt, &lastErrno) );
45357 }
45358#endif
45359 winLogIoerr(cnt, __LINE__);
45360
45361 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
45362 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
45363
45364 if( h==INVALID_HANDLE_VALUE ){
45365 sqlite3_free(zConverted);
45366 sqlite3_free(zTmpname);
45367 if( isReadWrite && !isExclusive ){
45368 return winOpen(pVfs, zName, id,
45369 ((flags|SQLITE_OPEN_READONLY) &
45370 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
45371 pOutFlags);
45372 }else{
45373 pFile->lastErrno = lastErrno;
45374 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
45375 return SQLITE_CANTOPEN_BKPT;
45376 }
45377 }
45378
45379 if( pOutFlags ){
45380 if( isReadWrite ){
45381 *pOutFlags = SQLITE_OPEN_READWRITE;
45382 }else{
45383 *pOutFlags = SQLITE_OPEN_READONLY;
45384 }
45385 }
45386
45387 OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
45388 "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
45389 *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
45390
45391 pAppData = (winVfsAppData*)pVfs->pAppData;
45392
45393#if SQLITE_OS_WINCE
45394 {
45395 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
45396 && ((pAppData==NULL) || !pAppData->bNoLock)
45397 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
45398 ){
45399 osCloseHandle(h);
45400 sqlite3_free(zConverted);
45401 sqlite3_free(zTmpname);
45402 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
45403 return rc;
45404 }
45405 }
45406 if( isTemp ){
45407 pFile->zDeleteOnClose = zConverted;
45408 }else
45409#endif
45410 {
45411 sqlite3_free(zConverted);
45412 }
45413
45414 sqlite3_free(zTmpname);
45415 pFile->pMethod = pAppData ? pAppData->pMethod : &winIoMethod;
45416 pFile->pVfs = pVfs;
45417 pFile->h = h;
45418 if( isReadonly ){
45419 pFile->ctrlFlags |= WINFILE_RDONLY;
45420 }
45421 if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
45422 pFile->ctrlFlags |= WINFILE_PSOW;
45423 }
45424 pFile->lastErrno = NO_ERROR;
45425 pFile->zPath = zName;
45426#if SQLITE_MAX_MMAP_SIZE>0
45427 pFile->hMap = NULL;
45428 pFile->pMapRegion = 0;
45429 pFile->mmapSize = 0;
45430 pFile->mmapSizeActual = 0;
45431 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
45432#endif
45433
45434 OpenCounter(+1);
45435 return rc;
45436}
45437
45438/*
45439** Delete the named file.
45440**
45441** Note that Windows does not allow a file to be deleted if some other
45442** process has it open. Sometimes a virus scanner or indexing program
45443** will open a journal file shortly after it is created in order to do
45444** whatever it does. While this other process is holding the
45445** file open, we will be unable to delete it. To work around this
45446** problem, we delay 100 milliseconds and try to delete again. Up
45447** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
45448** up and returning an error.
45449*/
45450static int winDelete(
45451 sqlite3_vfs *pVfs, /* Not used on win32 */
45452 const char *zFilename, /* Name of file to delete */
45453 int syncDir /* Not used on win32 */
45454){
45455 int cnt = 0;
45456 int rc;
45457 DWORD attr;
45458 DWORD lastErrno = 0;
45459 void *zConverted;
45460 UNUSED_PARAMETER(pVfs);
45461 UNUSED_PARAMETER(syncDir);
45462
45463 SimulateIOError(return SQLITE_IOERR_DELETE);
45464 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
45465
45466 zConverted = winConvertFromUtf8Filename(zFilename);
45467 if( zConverted==0 ){
45468 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
45469 return SQLITE_IOERR_NOMEM_BKPT;
45470 }
45471 if( osIsNT() ){
45472 do {
45473#if SQLITE_OS_WINRT
45474 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
45475 memset(&sAttrData, 0, sizeof(sAttrData));
45476 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
45477 &sAttrData) ){
45478 attr = sAttrData.dwFileAttributes;
45479 }else{
45480 lastErrno = osGetLastError();
45481 if( lastErrno==ERROR_FILE_NOT_FOUND
45482 || lastErrno==ERROR_PATH_NOT_FOUND ){
45483 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
45484 }else{
45485 rc = SQLITE_ERROR;
45486 }
45487 break;
45488 }
45489#else
45490 attr = osGetFileAttributesW(zConverted);
45491#endif
45492 if ( attr==INVALID_FILE_ATTRIBUTES ){
45493 lastErrno = osGetLastError();
45494 if( lastErrno==ERROR_FILE_NOT_FOUND
45495 || lastErrno==ERROR_PATH_NOT_FOUND ){
45496 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
45497 }else{
45498 rc = SQLITE_ERROR;
45499 }
45500 break;
45501 }
45502 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
45503 rc = SQLITE_ERROR; /* Files only. */
45504 break;
45505 }
45506 if ( osDeleteFileW(zConverted) ){
45507 rc = SQLITE_OK; /* Deleted OK. */
45508 break;
45509 }
45510 if ( !winRetryIoerr(&cnt, &lastErrno) ){
45511 rc = SQLITE_ERROR; /* No more retries. */
45512 break;
45513 }
45514 } while(1);
45515 }
45516#ifdef SQLITE_WIN32_HAS_ANSI
45517 else{
45518 do {
45519 attr = osGetFileAttributesA(zConverted);
45520 if ( attr==INVALID_FILE_ATTRIBUTES ){
45521 lastErrno = osGetLastError();
45522 if( lastErrno==ERROR_FILE_NOT_FOUND
45523 || lastErrno==ERROR_PATH_NOT_FOUND ){
45524 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
45525 }else{
45526 rc = SQLITE_ERROR;
45527 }
45528 break;
45529 }
45530 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
45531 rc = SQLITE_ERROR; /* Files only. */
45532 break;
45533 }
45534 if ( osDeleteFileA(zConverted) ){
45535 rc = SQLITE_OK; /* Deleted OK. */
45536 break;
45537 }
45538 if ( !winRetryIoerr(&cnt, &lastErrno) ){
45539 rc = SQLITE_ERROR; /* No more retries. */
45540 break;
45541 }
45542 } while(1);
45543 }
45544#endif
45545 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
45546 rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
45547 }else{
45548 winLogIoerr(cnt, __LINE__);
45549 }
45550 sqlite3_free(zConverted);
45551 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
45552 return rc;
45553}
45554
45555/*
45556** Check the existence and status of a file.
45557*/
45558static int winAccess(
45559 sqlite3_vfs *pVfs, /* Not used on win32 */
45560 const char *zFilename, /* Name of file to check */
45561 int flags, /* Type of test to make on this file */
45562 int *pResOut /* OUT: Result */
45563){
45564 DWORD attr;
45565 int rc = 0;
45566 DWORD lastErrno = 0;
45567 void *zConverted;
45568 UNUSED_PARAMETER(pVfs);
45569
45570 SimulateIOError( return SQLITE_IOERR_ACCESS; );
45571 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
45572 zFilename, flags, pResOut));
45573
45574 zConverted = winConvertFromUtf8Filename(zFilename);
45575 if( zConverted==0 ){
45576 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
45577 return SQLITE_IOERR_NOMEM_BKPT;
45578 }
45579 if( osIsNT() ){
45580 int cnt = 0;
45581 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
45582 memset(&sAttrData, 0, sizeof(sAttrData));
45583 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
45584 GetFileExInfoStandard,
45585 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
45586 if( rc ){
45587 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
45588 ** as if it does not exist.
45589 */
45590 if( flags==SQLITE_ACCESS_EXISTS
45591 && sAttrData.nFileSizeHigh==0
45592 && sAttrData.nFileSizeLow==0 ){
45593 attr = INVALID_FILE_ATTRIBUTES;
45594 }else{
45595 attr = sAttrData.dwFileAttributes;
45596 }
45597 }else{
45598 winLogIoerr(cnt, __LINE__);
45599 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
45600 sqlite3_free(zConverted);
45601 return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
45602 zFilename);
45603 }else{
45604 attr = INVALID_FILE_ATTRIBUTES;
45605 }
45606 }
45607 }
45608#ifdef SQLITE_WIN32_HAS_ANSI
45609 else{
45610 attr = osGetFileAttributesA((char*)zConverted);
45611 }
45612#endif
45613 sqlite3_free(zConverted);
45614 switch( flags ){
45615 case SQLITE_ACCESS_READ:
45616 case SQLITE_ACCESS_EXISTS:
45617 rc = attr!=INVALID_FILE_ATTRIBUTES;
45618 break;
45619 case SQLITE_ACCESS_READWRITE:
45620 rc = attr!=INVALID_FILE_ATTRIBUTES &&
45621 (attr & FILE_ATTRIBUTE_READONLY)==0;
45622 break;
45623 default:
45624 assert(!"Invalid flags argument");
45625 }
45626 *pResOut = rc;
45627 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
45628 zFilename, pResOut, *pResOut));
45629 return SQLITE_OK;
45630}
45631
45632/*
45633** Returns non-zero if the specified path name starts with a drive letter
45634** followed by a colon character.
45635*/
45636static BOOL winIsDriveLetterAndColon(
45637 const char *zPathname
45638){
45639 return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
45640}
45641
45642/*
45643** Returns non-zero if the specified path name should be used verbatim. If
45644** non-zero is returned from this function, the calling function must simply
45645** use the provided path name verbatim -OR- resolve it into a full path name
45646** using the GetFullPathName Win32 API function (if available).
45647*/
45648static BOOL winIsVerbatimPathname(
45649 const char *zPathname
45650){
45651 /*
45652 ** If the path name starts with a forward slash or a backslash, it is either
45653 ** a legal UNC name, a volume relative path, or an absolute path name in the
45654 ** "Unix" format on Windows. There is no easy way to differentiate between
45655 ** the final two cases; therefore, we return the safer return value of TRUE
45656 ** so that callers of this function will simply use it verbatim.
45657 */
45658 if ( winIsDirSep(zPathname[0]) ){
45659 return TRUE;
45660 }
45661
45662 /*
45663 ** If the path name starts with a letter and a colon it is either a volume
45664 ** relative path or an absolute path. Callers of this function must not
45665 ** attempt to treat it as a relative path name (i.e. they should simply use
45666 ** it verbatim).
45667 */
45668 if ( winIsDriveLetterAndColon(zPathname) ){
45669 return TRUE;
45670 }
45671
45672 /*
45673 ** If we get to this point, the path name should almost certainly be a purely
45674 ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
45675 */
45676 return FALSE;
45677}
45678
45679/*
45680** Turn a relative pathname into a full pathname. Write the full
45681** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
45682** bytes in size.
45683*/
45684static int winFullPathname(
45685 sqlite3_vfs *pVfs, /* Pointer to vfs object */
45686 const char *zRelative, /* Possibly relative input path */
45687 int nFull, /* Size of output buffer in bytes */
45688 char *zFull /* Output buffer */
45689){
45690#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
45691 DWORD nByte;
45692 void *zConverted;
45693 char *zOut;
45694#endif
45695
45696 /* If this path name begins with "/X:", where "X" is any alphabetic
45697 ** character, discard the initial "/" from the pathname.
45698 */
45699 if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
45700 zRelative++;
45701 }
45702
45703#if defined(__CYGWIN__)
45704 SimulateIOError( return SQLITE_ERROR );
45705 UNUSED_PARAMETER(nFull);
45706 assert( nFull>=pVfs->mxPathname );
45707 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
45708 /*
45709 ** NOTE: We are dealing with a relative path name and the data
45710 ** directory has been set. Therefore, use it as the basis
45711 ** for converting the relative path name to an absolute
45712 ** one by prepending the data directory and a slash.
45713 */
45714 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
45715 if( !zOut ){
45716 return SQLITE_IOERR_NOMEM_BKPT;
45717 }
45718 if( cygwin_conv_path(
45719 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
45720 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
45721 sqlite3_free(zOut);
45722 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
45723 "winFullPathname1", zRelative);
45724 }else{
45725 char *zUtf8 = winConvertToUtf8Filename(zOut);
45726 if( !zUtf8 ){
45727 sqlite3_free(zOut);
45728 return SQLITE_IOERR_NOMEM_BKPT;
45729 }
45730 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
45731 sqlite3_data_directory, winGetDirSep(), zUtf8);
45732 sqlite3_free(zUtf8);
45733 sqlite3_free(zOut);
45734 }
45735 }else{
45736 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
45737 if( !zOut ){
45738 return SQLITE_IOERR_NOMEM_BKPT;
45739 }
45740 if( cygwin_conv_path(
45741 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
45742 zRelative, zOut, pVfs->mxPathname+1)<0 ){
45743 sqlite3_free(zOut);
45744 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
45745 "winFullPathname2", zRelative);
45746 }else{
45747 char *zUtf8 = winConvertToUtf8Filename(zOut);
45748 if( !zUtf8 ){
45749 sqlite3_free(zOut);
45750 return SQLITE_IOERR_NOMEM_BKPT;
45751 }
45752 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
45753 sqlite3_free(zUtf8);
45754 sqlite3_free(zOut);
45755 }
45756 }
45757 return SQLITE_OK;
45758#endif
45759
45760#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
45761 SimulateIOError( return SQLITE_ERROR );
45762 /* WinCE has no concept of a relative pathname, or so I am told. */
45763 /* WinRT has no way to convert a relative path to an absolute one. */
45764 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
45765 /*
45766 ** NOTE: We are dealing with a relative path name and the data
45767 ** directory has been set. Therefore, use it as the basis
45768 ** for converting the relative path name to an absolute
45769 ** one by prepending the data directory and a backslash.
45770 */
45771 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
45772 sqlite3_data_directory, winGetDirSep(), zRelative);
45773 }else{
45774 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
45775 }
45776 return SQLITE_OK;
45777#endif
45778
45779#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
45780 /* It's odd to simulate an io-error here, but really this is just
45781 ** using the io-error infrastructure to test that SQLite handles this
45782 ** function failing. This function could fail if, for example, the
45783 ** current working directory has been unlinked.
45784 */
45785 SimulateIOError( return SQLITE_ERROR );
45786 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
45787 /*
45788 ** NOTE: We are dealing with a relative path name and the data
45789 ** directory has been set. Therefore, use it as the basis
45790 ** for converting the relative path name to an absolute
45791 ** one by prepending the data directory and a backslash.
45792 */
45793 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
45794 sqlite3_data_directory, winGetDirSep(), zRelative);
45795 return SQLITE_OK;
45796 }
45797 zConverted = winConvertFromUtf8Filename(zRelative);
45798 if( zConverted==0 ){
45799 return SQLITE_IOERR_NOMEM_BKPT;
45800 }
45801 if( osIsNT() ){
45802 LPWSTR zTemp;
45803 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
45804 if( nByte==0 ){
45805 sqlite3_free(zConverted);
45806 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
45807 "winFullPathname1", zRelative);
45808 }
45809 nByte += 3;
45810 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
45811 if( zTemp==0 ){
45812 sqlite3_free(zConverted);
45813 return SQLITE_IOERR_NOMEM_BKPT;
45814 }
45815 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
45816 if( nByte==0 ){
45817 sqlite3_free(zConverted);
45818 sqlite3_free(zTemp);
45819 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
45820 "winFullPathname2", zRelative);
45821 }
45822 sqlite3_free(zConverted);
45823 zOut = winUnicodeToUtf8(zTemp);
45824 sqlite3_free(zTemp);
45825 }
45826#ifdef SQLITE_WIN32_HAS_ANSI
45827 else{
45828 char *zTemp;
45829 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
45830 if( nByte==0 ){
45831 sqlite3_free(zConverted);
45832 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
45833 "winFullPathname3", zRelative);
45834 }
45835 nByte += 3;
45836 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
45837 if( zTemp==0 ){
45838 sqlite3_free(zConverted);
45839 return SQLITE_IOERR_NOMEM_BKPT;
45840 }
45841 nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
45842 if( nByte==0 ){
45843 sqlite3_free(zConverted);
45844 sqlite3_free(zTemp);
45845 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
45846 "winFullPathname4", zRelative);
45847 }
45848 sqlite3_free(zConverted);
45849 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
45850 sqlite3_free(zTemp);
45851 }
45852#endif
45853 if( zOut ){
45854 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
45855 sqlite3_free(zOut);
45856 return SQLITE_OK;
45857 }else{
45858 return SQLITE_IOERR_NOMEM_BKPT;
45859 }
45860#endif
45861}
45862
45863#ifndef SQLITE_OMIT_LOAD_EXTENSION
45864/*
45865** Interfaces for opening a shared library, finding entry points
45866** within the shared library, and closing the shared library.
45867*/
45868static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
45869 HANDLE h;
45870#if defined(__CYGWIN__)
45871 int nFull = pVfs->mxPathname+1;
45872 char *zFull = sqlite3MallocZero( nFull );
45873 void *zConverted = 0;
45874 if( zFull==0 ){
45875 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
45876 return 0;
45877 }
45878 if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
45879 sqlite3_free(zFull);
45880 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
45881 return 0;
45882 }
45883 zConverted = winConvertFromUtf8Filename(zFull);
45884 sqlite3_free(zFull);
45885#else
45886 void *zConverted = winConvertFromUtf8Filename(zFilename);
45887 UNUSED_PARAMETER(pVfs);
45888#endif
45889 if( zConverted==0 ){
45890 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
45891 return 0;
45892 }
45893 if( osIsNT() ){
45894#if SQLITE_OS_WINRT
45895 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
45896#else
45897 h = osLoadLibraryW((LPCWSTR)zConverted);
45898#endif
45899 }
45900#ifdef SQLITE_WIN32_HAS_ANSI
45901 else{
45902 h = osLoadLibraryA((char*)zConverted);
45903 }
45904#endif
45905 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
45906 sqlite3_free(zConverted);
45907 return (void*)h;
45908}
45909static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
45910 UNUSED_PARAMETER(pVfs);
45911 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
45912}
45913static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
45914 FARPROC proc;
45915 UNUSED_PARAMETER(pVfs);
45916 proc = osGetProcAddressA((HANDLE)pH, zSym);
45917 OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
45918 (void*)pH, zSym, (void*)proc));
45919 return (void(*)(void))proc;
45920}
45921static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
45922 UNUSED_PARAMETER(pVfs);
45923 osFreeLibrary((HANDLE)pHandle);
45924 OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
45925}
45926#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
45927 #define winDlOpen 0
45928 #define winDlError 0
45929 #define winDlSym 0
45930 #define winDlClose 0
45931#endif
45932
45933/* State information for the randomness gatherer. */
45934typedef struct EntropyGatherer EntropyGatherer;
45935struct EntropyGatherer {
45936 unsigned char *a; /* Gather entropy into this buffer */
45937 int na; /* Size of a[] in bytes */
45938 int i; /* XOR next input into a[i] */
45939 int nXor; /* Number of XOR operations done */
45940};
45941
45942#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
45943/* Mix sz bytes of entropy into p. */
45944static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
45945 int j, k;
45946 for(j=0, k=p->i; j<sz; j++){
45947 p->a[k++] ^= x[j];
45948 if( k>=p->na ) k = 0;
45949 }
45950 p->i = k;
45951 p->nXor += sz;
45952}
45953#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
45954
45955/*
45956** Write up to nBuf bytes of randomness into zBuf.
45957*/
45958static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
45959#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
45960 UNUSED_PARAMETER(pVfs);
45961 memset(zBuf, 0, nBuf);
45962 return nBuf;
45963#else
45964 EntropyGatherer e;
45965 UNUSED_PARAMETER(pVfs);
45966 memset(zBuf, 0, nBuf);
45967 e.a = (unsigned char*)zBuf;
45968 e.na = nBuf;
45969 e.nXor = 0;
45970 e.i = 0;
45971 {
45972 SYSTEMTIME x;
45973 osGetSystemTime(&x);
45974 xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
45975 }
45976 {
45977 DWORD pid = osGetCurrentProcessId();
45978 xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
45979 }
45980#if SQLITE_OS_WINRT
45981 {
45982 ULONGLONG cnt = osGetTickCount64();
45983 xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
45984 }
45985#else
45986 {
45987 DWORD cnt = osGetTickCount();
45988 xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
45989 }
45990#endif /* SQLITE_OS_WINRT */
45991 {
45992 LARGE_INTEGER i;
45993 osQueryPerformanceCounter(&i);
45994 xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
45995 }
45996#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
45997 {
45998 UUID id;
45999 memset(&id, 0, sizeof(UUID));
46000 osUuidCreate(&id);
46001 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
46002 memset(&id, 0, sizeof(UUID));
46003 osUuidCreateSequential(&id);
46004 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
46005 }
46006#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
46007 return e.nXor>nBuf ? nBuf : e.nXor;
46008#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
46009}
46010
46011
46012/*
46013** Sleep for a little while. Return the amount of time slept.
46014*/
46015static int winSleep(sqlite3_vfs *pVfs, int microsec){
46016 sqlite3_win32_sleep((microsec+999)/1000);
46017 UNUSED_PARAMETER(pVfs);
46018 return ((microsec+999)/1000)*1000;
46019}
46020
46021/*
46022** The following variable, if set to a non-zero value, is interpreted as
46023** the number of seconds since 1970 and is used to set the result of
46024** sqlite3OsCurrentTime() during testing.
46025*/
46026#ifdef SQLITE_TEST
46027SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
46028#endif
46029
46030/*
46031** Find the current time (in Universal Coordinated Time). Write into *piNow
46032** the current time and date as a Julian Day number times 86_400_000. In
46033** other words, write into *piNow the number of milliseconds since the Julian
46034** epoch of noon in Greenwich on November 24, 4714 B.C according to the
46035** proleptic Gregorian calendar.
46036**
46037** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
46038** cannot be found.
46039*/
46040static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
46041 /* FILETIME structure is a 64-bit value representing the number of
46042 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
46043 */
46044 FILETIME ft;
46045 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
46046#ifdef SQLITE_TEST
46047 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
46048#endif
46049 /* 2^32 - to avoid use of LL and warnings in gcc */
46050 static const sqlite3_int64 max32BitValue =
46051 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
46052 (sqlite3_int64)294967296;
46053
46054#if SQLITE_OS_WINCE
46055 SYSTEMTIME time;
46056 osGetSystemTime(&time);
46057 /* if SystemTimeToFileTime() fails, it returns zero. */
46058 if (!osSystemTimeToFileTime(&time,&ft)){
46059 return SQLITE_ERROR;
46060 }
46061#else
46062 osGetSystemTimeAsFileTime( &ft );
46063#endif
46064
46065 *piNow = winFiletimeEpoch +
46066 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
46067 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
46068
46069#ifdef SQLITE_TEST
46070 if( sqlite3_current_time ){
46071 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
46072 }
46073#endif
46074 UNUSED_PARAMETER(pVfs);
46075 return SQLITE_OK;
46076}
46077
46078/*
46079** Find the current time (in Universal Coordinated Time). Write the
46080** current time and date as a Julian Day number into *prNow and
46081** return 0. Return 1 if the time and date cannot be found.
46082*/
46083static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
46084 int rc;
46085 sqlite3_int64 i;
46086 rc = winCurrentTimeInt64(pVfs, &i);
46087 if( !rc ){
46088 *prNow = i/86400000.0;
46089 }
46090 return rc;
46091}
46092
46093/*
46094** The idea is that this function works like a combination of
46095** GetLastError() and FormatMessage() on Windows (or errno and
46096** strerror_r() on Unix). After an error is returned by an OS
46097** function, SQLite calls this function with zBuf pointing to
46098** a buffer of nBuf bytes. The OS layer should populate the
46099** buffer with a nul-terminated UTF-8 encoded error message
46100** describing the last IO error to have occurred within the calling
46101** thread.
46102**
46103** If the error message is too large for the supplied buffer,
46104** it should be truncated. The return value of xGetLastError
46105** is zero if the error message fits in the buffer, or non-zero
46106** otherwise (if the message was truncated). If non-zero is returned,
46107** then it is not necessary to include the nul-terminator character
46108** in the output buffer.
46109**
46110** Not supplying an error message will have no adverse effect
46111** on SQLite. It is fine to have an implementation that never
46112** returns an error message:
46113**
46114** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
46115** assert(zBuf[0]=='\0');
46116** return 0;
46117** }
46118**
46119** However if an error message is supplied, it will be incorporated
46120** by sqlite into the error message available to the user using
46121** sqlite3_errmsg(), possibly making IO errors easier to debug.
46122*/
46123static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
46124 DWORD e = osGetLastError();
46125 UNUSED_PARAMETER(pVfs);
46126 if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
46127 return e;
46128}
46129
46130/*
46131** Initialize and deinitialize the operating system interface.
46132*/
46133SQLITE_API int sqlite3_os_init(void){
46134 static sqlite3_vfs winVfs = {
46135 3, /* iVersion */
46136 sizeof(winFile), /* szOsFile */
46137 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
46138 0, /* pNext */
46139 "win32", /* zName */
46140 &winAppData, /* pAppData */
46141 winOpen, /* xOpen */
46142 winDelete, /* xDelete */
46143 winAccess, /* xAccess */
46144 winFullPathname, /* xFullPathname */
46145 winDlOpen, /* xDlOpen */
46146 winDlError, /* xDlError */
46147 winDlSym, /* xDlSym */
46148 winDlClose, /* xDlClose */
46149 winRandomness, /* xRandomness */
46150 winSleep, /* xSleep */
46151 winCurrentTime, /* xCurrentTime */
46152 winGetLastError, /* xGetLastError */
46153 winCurrentTimeInt64, /* xCurrentTimeInt64 */
46154 winSetSystemCall, /* xSetSystemCall */
46155 winGetSystemCall, /* xGetSystemCall */
46156 winNextSystemCall, /* xNextSystemCall */
46157 };
46158#if defined(SQLITE_WIN32_HAS_WIDE)
46159 static sqlite3_vfs winLongPathVfs = {
46160 3, /* iVersion */
46161 sizeof(winFile), /* szOsFile */
46162 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
46163 0, /* pNext */
46164 "win32-longpath", /* zName */
46165 &winAppData, /* pAppData */
46166 winOpen, /* xOpen */
46167 winDelete, /* xDelete */
46168 winAccess, /* xAccess */
46169 winFullPathname, /* xFullPathname */
46170 winDlOpen, /* xDlOpen */
46171 winDlError, /* xDlError */
46172 winDlSym, /* xDlSym */
46173 winDlClose, /* xDlClose */
46174 winRandomness, /* xRandomness */
46175 winSleep, /* xSleep */
46176 winCurrentTime, /* xCurrentTime */
46177 winGetLastError, /* xGetLastError */
46178 winCurrentTimeInt64, /* xCurrentTimeInt64 */
46179 winSetSystemCall, /* xSetSystemCall */
46180 winGetSystemCall, /* xGetSystemCall */
46181 winNextSystemCall, /* xNextSystemCall */
46182 };
46183#endif
46184 static sqlite3_vfs winNolockVfs = {
46185 3, /* iVersion */
46186 sizeof(winFile), /* szOsFile */
46187 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
46188 0, /* pNext */
46189 "win32-none", /* zName */
46190 &winNolockAppData, /* pAppData */
46191 winOpen, /* xOpen */
46192 winDelete, /* xDelete */
46193 winAccess, /* xAccess */
46194 winFullPathname, /* xFullPathname */
46195 winDlOpen, /* xDlOpen */
46196 winDlError, /* xDlError */
46197 winDlSym, /* xDlSym */
46198 winDlClose, /* xDlClose */
46199 winRandomness, /* xRandomness */
46200 winSleep, /* xSleep */
46201 winCurrentTime, /* xCurrentTime */
46202 winGetLastError, /* xGetLastError */
46203 winCurrentTimeInt64, /* xCurrentTimeInt64 */
46204 winSetSystemCall, /* xSetSystemCall */
46205 winGetSystemCall, /* xGetSystemCall */
46206 winNextSystemCall, /* xNextSystemCall */
46207 };
46208#if defined(SQLITE_WIN32_HAS_WIDE)
46209 static sqlite3_vfs winLongPathNolockVfs = {
46210 3, /* iVersion */
46211 sizeof(winFile), /* szOsFile */
46212 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
46213 0, /* pNext */
46214 "win32-longpath-none", /* zName */
46215 &winNolockAppData, /* pAppData */
46216 winOpen, /* xOpen */
46217 winDelete, /* xDelete */
46218 winAccess, /* xAccess */
46219 winFullPathname, /* xFullPathname */
46220 winDlOpen, /* xDlOpen */
46221 winDlError, /* xDlError */
46222 winDlSym, /* xDlSym */
46223 winDlClose, /* xDlClose */
46224 winRandomness, /* xRandomness */
46225 winSleep, /* xSleep */
46226 winCurrentTime, /* xCurrentTime */
46227 winGetLastError, /* xGetLastError */
46228 winCurrentTimeInt64, /* xCurrentTimeInt64 */
46229 winSetSystemCall, /* xSetSystemCall */
46230 winGetSystemCall, /* xGetSystemCall */
46231 winNextSystemCall, /* xNextSystemCall */
46232 };
46233#endif
46234
46235 /* Double-check that the aSyscall[] array has been constructed
46236 ** correctly. See ticket [bb3a86e890c8e96ab] */
46237 assert( ArraySize(aSyscall)==80 );
46238
46239 /* get memory map allocation granularity */
46240 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
46241#if SQLITE_OS_WINRT
46242 osGetNativeSystemInfo(&winSysInfo);
46243#else
46244 osGetSystemInfo(&winSysInfo);
46245#endif
46246 assert( winSysInfo.dwAllocationGranularity>0 );
46247 assert( winSysInfo.dwPageSize>0 );
46248
46249 sqlite3_vfs_register(&winVfs, 1);
46250
46251#if defined(SQLITE_WIN32_HAS_WIDE)
46252 sqlite3_vfs_register(&winLongPathVfs, 0);
46253#endif
46254
46255 sqlite3_vfs_register(&winNolockVfs, 0);
46256
46257#if defined(SQLITE_WIN32_HAS_WIDE)
46258 sqlite3_vfs_register(&winLongPathNolockVfs, 0);
46259#endif
46260
46261#ifndef SQLITE_OMIT_WAL
46262 winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
46263#endif
46264
46265 return SQLITE_OK;
46266}
46267
46268SQLITE_API int sqlite3_os_end(void){
46269#if SQLITE_OS_WINRT
46270 if( sleepObj!=NULL ){
46271 osCloseHandle(sleepObj);
46272 sleepObj = NULL;
46273 }
46274#endif
46275
46276#ifndef SQLITE_OMIT_WAL
46277 winBigLock = 0;
46278#endif
46279
46280 return SQLITE_OK;
46281}
46282
46283#endif /* SQLITE_OS_WIN */
46284
46285/************** End of os_win.c **********************************************/
46286/************** Begin file memdb.c *******************************************/
46287/*
46288** 2016-09-07
46289**
46290** The author disclaims copyright to this source code. In place of
46291** a legal notice, here is a blessing:
46292**
46293** May you do good and not evil.
46294** May you find forgiveness for yourself and forgive others.
46295** May you share freely, never taking more than you give.
46296**
46297******************************************************************************
46298**
46299** This file implements an in-memory VFS. A database is held as a contiguous
46300** block of memory.
46301**
46302** This file also implements interface sqlite3_serialize() and
46303** sqlite3_deserialize().
46304*/
46305/* #include "sqliteInt.h" */
46306#ifdef SQLITE_ENABLE_DESERIALIZE
46307
46308/*
46309** Forward declaration of objects used by this utility
46310*/
46311typedef struct sqlite3_vfs MemVfs;
46312typedef struct MemFile MemFile;
46313
46314/* Access to a lower-level VFS that (might) implement dynamic loading,
46315** access to randomness, etc.
46316*/
46317#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
46318
46319/* An open file */
46320struct MemFile {
46321 sqlite3_file base; /* IO methods */
46322 sqlite3_int64 sz; /* Size of the file */
46323 sqlite3_int64 szMax; /* Space allocated to aData */
46324 unsigned char *aData; /* content of the file */
46325 int nMmap; /* Number of memory mapped pages */
46326 unsigned mFlags; /* Flags */
46327 int eLock; /* Most recent lock against this file */
46328};
46329
46330/*
46331** Methods for MemFile
46332*/
46333static int memdbClose(sqlite3_file*);
46334static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
46335static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
46336static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
46337static int memdbSync(sqlite3_file*, int flags);
46338static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
46339static int memdbLock(sqlite3_file*, int);
46340/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
46341static int memdbFileControl(sqlite3_file*, int op, void *pArg);
46342/* static int memdbSectorSize(sqlite3_file*); // not used */
46343static int memdbDeviceCharacteristics(sqlite3_file*);
46344static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
46345static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
46346
46347/*
46348** Methods for MemVfs
46349*/
46350static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
46351/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
46352static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
46353static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
46354static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
46355static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
46356static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
46357static void memdbDlClose(sqlite3_vfs*, void*);
46358static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
46359static int memdbSleep(sqlite3_vfs*, int microseconds);
46360/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
46361static int memdbGetLastError(sqlite3_vfs*, int, char *);
46362static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
46363
46364static sqlite3_vfs memdb_vfs = {
46365 2, /* iVersion */
46366 0, /* szOsFile (set when registered) */
46367 1024, /* mxPathname */
46368 0, /* pNext */
46369 "memdb", /* zName */
46370 0, /* pAppData (set when registered) */
46371 memdbOpen, /* xOpen */
46372 0, /* memdbDelete, */ /* xDelete */
46373 memdbAccess, /* xAccess */
46374 memdbFullPathname, /* xFullPathname */
46375 memdbDlOpen, /* xDlOpen */
46376 memdbDlError, /* xDlError */
46377 memdbDlSym, /* xDlSym */
46378 memdbDlClose, /* xDlClose */
46379 memdbRandomness, /* xRandomness */
46380 memdbSleep, /* xSleep */
46381 0, /* memdbCurrentTime, */ /* xCurrentTime */
46382 memdbGetLastError, /* xGetLastError */
46383 memdbCurrentTimeInt64 /* xCurrentTimeInt64 */
46384};
46385
46386static const sqlite3_io_methods memdb_io_methods = {
46387 3, /* iVersion */
46388 memdbClose, /* xClose */
46389 memdbRead, /* xRead */
46390 memdbWrite, /* xWrite */
46391 memdbTruncate, /* xTruncate */
46392 memdbSync, /* xSync */
46393 memdbFileSize, /* xFileSize */
46394 memdbLock, /* xLock */
46395 memdbLock, /* xUnlock - same as xLock in this case */
46396 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
46397 memdbFileControl, /* xFileControl */
46398 0, /* memdbSectorSize,*/ /* xSectorSize */
46399 memdbDeviceCharacteristics, /* xDeviceCharacteristics */
46400 0, /* xShmMap */
46401 0, /* xShmLock */
46402 0, /* xShmBarrier */
46403 0, /* xShmUnmap */
46404 memdbFetch, /* xFetch */
46405 memdbUnfetch /* xUnfetch */
46406};
46407
46408
46409
46410/*
46411** Close an memdb-file.
46412**
46413** The pData pointer is owned by the application, so there is nothing
46414** to free.
46415*/
46416static int memdbClose(sqlite3_file *pFile){
46417 MemFile *p = (MemFile *)pFile;
46418 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ) sqlite3_free(p->aData);
46419 return SQLITE_OK;
46420}
46421
46422/*
46423** Read data from an memdb-file.
46424*/
46425static int memdbRead(
46426 sqlite3_file *pFile,
46427 void *zBuf,
46428 int iAmt,
46429 sqlite_int64 iOfst
46430){
46431 MemFile *p = (MemFile *)pFile;
46432 if( iOfst+iAmt>p->sz ){
46433 memset(zBuf, 0, iAmt);
46434 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
46435 return SQLITE_IOERR_SHORT_READ;
46436 }
46437 memcpy(zBuf, p->aData+iOfst, iAmt);
46438 return SQLITE_OK;
46439}
46440
46441/*
46442** Try to enlarge the memory allocation to hold at least sz bytes
46443*/
46444static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){
46445 unsigned char *pNew;
46446 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){
46447 return SQLITE_FULL;
46448 }
46449 pNew = sqlite3_realloc64(p->aData, newSz);
46450 if( pNew==0 ) return SQLITE_NOMEM;
46451 p->aData = pNew;
46452 p->szMax = newSz;
46453 return SQLITE_OK;
46454}
46455
46456/*
46457** Write data to an memdb-file.
46458*/
46459static int memdbWrite(
46460 sqlite3_file *pFile,
46461 const void *z,
46462 int iAmt,
46463 sqlite_int64 iOfst
46464){
46465 MemFile *p = (MemFile *)pFile;
46466 if( iOfst+iAmt>p->sz ){
46467 int rc;
46468 if( iOfst+iAmt>p->szMax
46469 && (rc = memdbEnlarge(p, (iOfst+iAmt)*2))!=SQLITE_OK
46470 ){
46471 return rc;
46472 }
46473 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
46474 p->sz = iOfst+iAmt;
46475 }
46476 memcpy(p->aData+iOfst, z, iAmt);
46477 return SQLITE_OK;
46478}
46479
46480/*
46481** Truncate an memdb-file.
46482**
46483** In rollback mode (which is always the case for memdb, as it does not
46484** support WAL mode) the truncate() method is only used to reduce
46485** the size of a file, never to increase the size.
46486*/
46487static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
46488 MemFile *p = (MemFile *)pFile;
46489 if( NEVER(size>p->sz) ) return SQLITE_FULL;
46490 p->sz = size;
46491 return SQLITE_OK;
46492}
46493
46494/*
46495** Sync an memdb-file.
46496*/
46497static int memdbSync(sqlite3_file *pFile, int flags){
46498 return SQLITE_OK;
46499}
46500
46501/*
46502** Return the current file-size of an memdb-file.
46503*/
46504static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
46505 MemFile *p = (MemFile *)pFile;
46506 *pSize = p->sz;
46507 return SQLITE_OK;
46508}
46509
46510/*
46511** Lock an memdb-file.
46512*/
46513static int memdbLock(sqlite3_file *pFile, int eLock){
46514 MemFile *p = (MemFile *)pFile;
46515 p->eLock = eLock;
46516 return SQLITE_OK;
46517}
46518
46519#if 0 /* Never used because memdbAccess() always returns false */
46520/*
46521** Check if another file-handle holds a RESERVED lock on an memdb-file.
46522*/
46523static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
46524 *pResOut = 0;
46525 return SQLITE_OK;
46526}
46527#endif
46528
46529/*
46530** File control method. For custom operations on an memdb-file.
46531*/
46532static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
46533 MemFile *p = (MemFile *)pFile;
46534 int rc = SQLITE_NOTFOUND;
46535 if( op==SQLITE_FCNTL_VFSNAME ){
46536 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
46537 rc = SQLITE_OK;
46538 }
46539 return rc;
46540}
46541
46542#if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
46543/*
46544** Return the sector-size in bytes for an memdb-file.
46545*/
46546static int memdbSectorSize(sqlite3_file *pFile){
46547 return 1024;
46548}
46549#endif
46550
46551/*
46552** Return the device characteristic flags supported by an memdb-file.
46553*/
46554static int memdbDeviceCharacteristics(sqlite3_file *pFile){
46555 return SQLITE_IOCAP_ATOMIC |
46556 SQLITE_IOCAP_POWERSAFE_OVERWRITE |
46557 SQLITE_IOCAP_SAFE_APPEND |
46558 SQLITE_IOCAP_SEQUENTIAL;
46559}
46560
46561/* Fetch a page of a memory-mapped file */
46562static int memdbFetch(
46563 sqlite3_file *pFile,
46564 sqlite3_int64 iOfst,
46565 int iAmt,
46566 void **pp
46567){
46568 MemFile *p = (MemFile *)pFile;
46569 p->nMmap++;
46570 *pp = (void*)(p->aData + iOfst);
46571 return SQLITE_OK;
46572}
46573
46574/* Release a memory-mapped page */
46575static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
46576 MemFile *p = (MemFile *)pFile;
46577 p->nMmap--;
46578 return SQLITE_OK;
46579}
46580
46581/*
46582** Open an mem file handle.
46583*/
46584static int memdbOpen(
46585 sqlite3_vfs *pVfs,
46586 const char *zName,
46587 sqlite3_file *pFile,
46588 int flags,
46589 int *pOutFlags
46590){
46591 MemFile *p = (MemFile*)pFile;
46592 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
46593 return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
46594 }
46595 memset(p, 0, sizeof(*p));
46596 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
46597 assert( pOutFlags!=0 ); /* True because flags==SQLITE_OPEN_MAIN_DB */
46598 *pOutFlags = flags | SQLITE_OPEN_MEMORY;
46599 p->base.pMethods = &memdb_io_methods;
46600 return SQLITE_OK;
46601}
46602
46603#if 0 /* Only used to delete rollback journals, master journals, and WAL
46604 ** files, none of which exist in memdb. So this routine is never used */
46605/*
46606** Delete the file located at zPath. If the dirSync argument is true,
46607** ensure the file-system modifications are synced to disk before
46608** returning.
46609*/
46610static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
46611 return SQLITE_IOERR_DELETE;
46612}
46613#endif
46614
46615/*
46616** Test for access permissions. Return true if the requested permission
46617** is available, or false otherwise.
46618**
46619** With memdb, no files ever exist on disk. So always return false.
46620*/
46621static int memdbAccess(
46622 sqlite3_vfs *pVfs,
46623 const char *zPath,
46624 int flags,
46625 int *pResOut
46626){
46627 *pResOut = 0;
46628 return SQLITE_OK;
46629}
46630
46631/*
46632** Populate buffer zOut with the full canonical pathname corresponding
46633** to the pathname in zPath. zOut is guaranteed to point to a buffer
46634** of at least (INST_MAX_PATHNAME+1) bytes.
46635*/
46636static int memdbFullPathname(
46637 sqlite3_vfs *pVfs,
46638 const char *zPath,
46639 int nOut,
46640 char *zOut
46641){
46642 sqlite3_snprintf(nOut, zOut, "%s", zPath);
46643 return SQLITE_OK;
46644}
46645
46646/*
46647** Open the dynamic library located at zPath and return a handle.
46648*/
46649static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
46650 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
46651}
46652
46653/*
46654** Populate the buffer zErrMsg (size nByte bytes) with a human readable
46655** utf-8 string describing the most recent error encountered associated
46656** with dynamic libraries.
46657*/
46658static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
46659 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
46660}
46661
46662/*
46663** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
46664*/
46665static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
46666 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
46667}
46668
46669/*
46670** Close the dynamic library handle pHandle.
46671*/
46672static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
46673 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
46674}
46675
46676/*
46677** Populate the buffer pointed to by zBufOut with nByte bytes of
46678** random data.
46679*/
46680static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
46681 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
46682}
46683
46684/*
46685** Sleep for nMicro microseconds. Return the number of microseconds
46686** actually slept.
46687*/
46688static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
46689 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
46690}
46691
46692#if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */
46693/*
46694** Return the current time as a Julian Day number in *pTimeOut.
46695*/
46696static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
46697 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
46698}
46699#endif
46700
46701static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
46702 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
46703}
46704static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
46705 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
46706}
46707
46708/*
46709** Translate a database connection pointer and schema name into a
46710** MemFile pointer.
46711*/
46712static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
46713 MemFile *p = 0;
46714 int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
46715 if( rc ) return 0;
46716 if( p->base.pMethods!=&memdb_io_methods ) return 0;
46717 return p;
46718}
46719
46720/*
46721** Return the serialization of a database
46722*/
46723SQLITE_API unsigned char *sqlite3_serialize(
46724 sqlite3 *db, /* The database connection */
46725 const char *zSchema, /* Which database within the connection */
46726 sqlite3_int64 *piSize, /* Write size here, if not NULL */
46727 unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */
46728){
46729 MemFile *p;
46730 int iDb;
46731 Btree *pBt;
46732 sqlite3_int64 sz;
46733 int szPage = 0;
46734 sqlite3_stmt *pStmt = 0;
46735 unsigned char *pOut;
46736 char *zSql;
46737 int rc;
46738
46739#ifdef SQLITE_ENABLE_API_ARMOR
46740 if( !sqlite3SafetyCheckOk(db) ){
46741 (void)SQLITE_MISUSE_BKPT;
46742 return 0;
46743 }
46744#endif
46745
46746 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
46747 p = memdbFromDbSchema(db, zSchema);
46748 iDb = sqlite3FindDbName(db, zSchema);
46749 if( piSize ) *piSize = -1;
46750 if( iDb<0 ) return 0;
46751 if( p ){
46752 if( piSize ) *piSize = p->sz;
46753 if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
46754 pOut = p->aData;
46755 }else{
46756 pOut = sqlite3_malloc64( p->sz );
46757 if( pOut ) memcpy(pOut, p->aData, p->sz);
46758 }
46759 return pOut;
46760 }
46761 pBt = db->aDb[iDb].pBt;
46762 if( pBt==0 ) return 0;
46763 szPage = sqlite3BtreeGetPageSize(pBt);
46764 zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
46765 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
46766 sqlite3_free(zSql);
46767 if( rc ) return 0;
46768 rc = sqlite3_step(pStmt);
46769 if( rc!=SQLITE_ROW ){
46770 pOut = 0;
46771 }else{
46772 sz = sqlite3_column_int64(pStmt, 0)*szPage;
46773 if( piSize ) *piSize = sz;
46774 if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
46775 pOut = 0;
46776 }else{
46777 pOut = sqlite3_malloc64( sz );
46778 if( pOut ){
46779 int nPage = sqlite3_column_int(pStmt, 0);
46780 Pager *pPager = sqlite3BtreePager(pBt);
46781 int pgno;
46782 for(pgno=1; pgno<=nPage; pgno++){
46783 DbPage *pPage = 0;
46784 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
46785 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
46786 if( rc==SQLITE_OK ){
46787 memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
46788 }else{
46789 memset(pTo, 0, szPage);
46790 }
46791 sqlite3PagerUnref(pPage);
46792 }
46793 }
46794 }
46795 }
46796 sqlite3_finalize(pStmt);
46797 return pOut;
46798}
46799
46800/* Convert zSchema to a MemDB and initialize its content.
46801*/
46802SQLITE_API int sqlite3_deserialize(
46803 sqlite3 *db, /* The database connection */
46804 const char *zSchema, /* Which DB to reopen with the deserialization */
46805 unsigned char *pData, /* The serialized database content */
46806 sqlite3_int64 szDb, /* Number bytes in the deserialization */
46807 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
46808 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
46809){
46810 MemFile *p;
46811 char *zSql;
46812 sqlite3_stmt *pStmt = 0;
46813 int rc;
46814 int iDb;
46815
46816#ifdef SQLITE_ENABLE_API_ARMOR
46817 if( !sqlite3SafetyCheckOk(db) ){
46818 return SQLITE_MISUSE_BKPT;
46819 }
46820 if( szDb<0 ) return SQLITE_MISUSE_BKPT;
46821 if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
46822#endif
46823
46824 sqlite3_mutex_enter(db->mutex);
46825 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
46826 iDb = sqlite3FindDbName(db, zSchema);
46827 if( iDb<0 ){
46828 rc = SQLITE_ERROR;
46829 goto end_deserialize;
46830 }
46831 zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
46832 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
46833 sqlite3_free(zSql);
46834 if( rc ) goto end_deserialize;
46835 db->init.iDb = (u8)iDb;
46836 db->init.reopenMemdb = 1;
46837 rc = sqlite3_step(pStmt);
46838 db->init.reopenMemdb = 0;
46839 if( rc!=SQLITE_DONE ){
46840 rc = SQLITE_ERROR;
46841 goto end_deserialize;
46842 }
46843 p = memdbFromDbSchema(db, zSchema);
46844 if( p==0 ){
46845 rc = SQLITE_ERROR;
46846 }else{
46847 p->aData = pData;
46848 p->sz = szDb;
46849 p->szMax = szBuf;
46850 p->mFlags = mFlags;
46851 rc = SQLITE_OK;
46852 }
46853
46854end_deserialize:
46855 sqlite3_finalize(pStmt);
46856 sqlite3_mutex_leave(db->mutex);
46857 return rc;
46858}
46859
46860/*
46861** This routine is called when the extension is loaded.
46862** Register the new VFS.
46863*/
46864SQLITE_PRIVATE int sqlite3MemdbInit(void){
46865 sqlite3_vfs *pLower = sqlite3_vfs_find(0);
46866 int sz = pLower->szOsFile;
46867 memdb_vfs.pAppData = pLower;
46868 /* In all known configurations of SQLite, the size of a default
46869 ** sqlite3_file is greater than the size of a memdb sqlite3_file.
46870 ** Should that ever change, remove the following NEVER() */
46871 if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile);
46872 memdb_vfs.szOsFile = sz;
46873 return sqlite3_vfs_register(&memdb_vfs, 0);
46874}
46875#endif /* SQLITE_ENABLE_DESERIALIZE */
46876
46877/************** End of memdb.c ***********************************************/
46878/************** Begin file bitvec.c ******************************************/
46879/*
46880** 2008 February 16
46881**
46882** The author disclaims copyright to this source code. In place of
46883** a legal notice, here is a blessing:
46884**
46885** May you do good and not evil.
46886** May you find forgiveness for yourself and forgive others.
46887** May you share freely, never taking more than you give.
46888**
46889*************************************************************************
46890** This file implements an object that represents a fixed-length
46891** bitmap. Bits are numbered starting with 1.
46892**
46893** A bitmap is used to record which pages of a database file have been
46894** journalled during a transaction, or which pages have the "dont-write"
46895** property. Usually only a few pages are meet either condition.
46896** So the bitmap is usually sparse and has low cardinality.
46897** But sometimes (for example when during a DROP of a large table) most
46898** or all of the pages in a database can get journalled. In those cases,
46899** the bitmap becomes dense with high cardinality. The algorithm needs
46900** to handle both cases well.
46901**
46902** The size of the bitmap is fixed when the object is created.
46903**
46904** All bits are clear when the bitmap is created. Individual bits
46905** may be set or cleared one at a time.
46906**
46907** Test operations are about 100 times more common that set operations.
46908** Clear operations are exceedingly rare. There are usually between
46909** 5 and 500 set operations per Bitvec object, though the number of sets can
46910** sometimes grow into tens of thousands or larger. The size of the
46911** Bitvec object is the number of pages in the database file at the
46912** start of a transaction, and is thus usually less than a few thousand,
46913** but can be as large as 2 billion for a really big database.
46914*/
46915/* #include "sqliteInt.h" */
46916
46917/* Size of the Bitvec structure in bytes. */
46918#define BITVEC_SZ 512
46919
46920/* Round the union size down to the nearest pointer boundary, since that's how
46921** it will be aligned within the Bitvec struct. */
46922#define BITVEC_USIZE \
46923 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
46924
46925/* Type of the array "element" for the bitmap representation.
46926** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
46927** Setting this to the "natural word" size of your CPU may improve
46928** performance. */
46929#define BITVEC_TELEM u8
46930/* Size, in bits, of the bitmap element. */
46931#define BITVEC_SZELEM 8
46932/* Number of elements in a bitmap array. */
46933#define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
46934/* Number of bits in the bitmap array. */
46935#define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
46936
46937/* Number of u32 values in hash table. */
46938#define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
46939/* Maximum number of entries in hash table before
46940** sub-dividing and re-hashing. */
46941#define BITVEC_MXHASH (BITVEC_NINT/2)
46942/* Hashing function for the aHash representation.
46943** Empirical testing showed that the *37 multiplier
46944** (an arbitrary prime)in the hash function provided
46945** no fewer collisions than the no-op *1. */
46946#define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
46947
46948#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
46949
46950
46951/*
46952** A bitmap is an instance of the following structure.
46953**
46954** This bitmap records the existence of zero or more bits
46955** with values between 1 and iSize, inclusive.
46956**
46957** There are three possible representations of the bitmap.
46958** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
46959** bitmap. The least significant bit is bit 1.
46960**
46961** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
46962** a hash table that will hold up to BITVEC_MXHASH distinct values.
46963**
46964** Otherwise, the value i is redirected into one of BITVEC_NPTR
46965** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
46966** handles up to iDivisor separate values of i. apSub[0] holds
46967** values between 1 and iDivisor. apSub[1] holds values between
46968** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
46969** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
46970** to hold deal with values between 1 and iDivisor.
46971*/
46972struct Bitvec {
46973 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
46974 u32 nSet; /* Number of bits that are set - only valid for aHash
46975 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
46976 ** this would be 125. */
46977 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
46978 /* Should >=0 for apSub element. */
46979 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
46980 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
46981 union {
46982 BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
46983 u32 aHash[BITVEC_NINT]; /* Hash table representation */
46984 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
46985 } u;
46986};
46987
46988/*
46989** Create a new bitmap object able to handle bits between 0 and iSize,
46990** inclusive. Return a pointer to the new object. Return NULL if
46991** malloc fails.
46992*/
46993SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
46994 Bitvec *p;
46995 assert( sizeof(*p)==BITVEC_SZ );
46996 p = sqlite3MallocZero( sizeof(*p) );
46997 if( p ){
46998 p->iSize = iSize;
46999 }
47000 return p;
47001}
47002
47003/*
47004** Check to see if the i-th bit is set. Return true or false.
47005** If p is NULL (if the bitmap has not been created) or if
47006** i is out of range, then return false.
47007*/
47008SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
47009 assert( p!=0 );
47010 i--;
47011 if( i>=p->iSize ) return 0;
47012 while( p->iDivisor ){
47013 u32 bin = i/p->iDivisor;
47014 i = i%p->iDivisor;
47015 p = p->u.apSub[bin];
47016 if (!p) {
47017 return 0;
47018 }
47019 }
47020 if( p->iSize<=BITVEC_NBIT ){
47021 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
47022 } else{
47023 u32 h = BITVEC_HASH(i++);
47024 while( p->u.aHash[h] ){
47025 if( p->u.aHash[h]==i ) return 1;
47026 h = (h+1) % BITVEC_NINT;
47027 }
47028 return 0;
47029 }
47030}
47031SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
47032 return p!=0 && sqlite3BitvecTestNotNull(p,i);
47033}
47034
47035/*
47036** Set the i-th bit. Return 0 on success and an error code if
47037** anything goes wrong.
47038**
47039** This routine might cause sub-bitmaps to be allocated. Failing
47040** to get the memory needed to hold the sub-bitmap is the only
47041** that can go wrong with an insert, assuming p and i are valid.
47042**
47043** The calling function must ensure that p is a valid Bitvec object
47044** and that the value for "i" is within range of the Bitvec object.
47045** Otherwise the behavior is undefined.
47046*/
47047SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
47048 u32 h;
47049 if( p==0 ) return SQLITE_OK;
47050 assert( i>0 );
47051 assert( i<=p->iSize );
47052 i--;
47053 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
47054 u32 bin = i/p->iDivisor;
47055 i = i%p->iDivisor;
47056 if( p->u.apSub[bin]==0 ){
47057 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
47058 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
47059 }
47060 p = p->u.apSub[bin];
47061 }
47062 if( p->iSize<=BITVEC_NBIT ){
47063 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
47064 return SQLITE_OK;
47065 }
47066 h = BITVEC_HASH(i++);
47067 /* if there wasn't a hash collision, and this doesn't */
47068 /* completely fill the hash, then just add it without */
47069 /* worring about sub-dividing and re-hashing. */
47070 if( !p->u.aHash[h] ){
47071 if (p->nSet<(BITVEC_NINT-1)) {
47072 goto bitvec_set_end;
47073 } else {
47074 goto bitvec_set_rehash;
47075 }
47076 }
47077 /* there was a collision, check to see if it's already */
47078 /* in hash, if not, try to find a spot for it */
47079 do {
47080 if( p->u.aHash[h]==i ) return SQLITE_OK;
47081 h++;
47082 if( h>=BITVEC_NINT ) h = 0;
47083 } while( p->u.aHash[h] );
47084 /* we didn't find it in the hash. h points to the first */
47085 /* available free spot. check to see if this is going to */
47086 /* make our hash too "full". */
47087bitvec_set_rehash:
47088 if( p->nSet>=BITVEC_MXHASH ){
47089 unsigned int j;
47090 int rc;
47091 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
47092 if( aiValues==0 ){
47093 return SQLITE_NOMEM_BKPT;
47094 }else{
47095 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
47096 memset(p->u.apSub, 0, sizeof(p->u.apSub));
47097 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
47098 rc = sqlite3BitvecSet(p, i);
47099 for(j=0; j<BITVEC_NINT; j++){
47100 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
47101 }
47102 sqlite3StackFree(0, aiValues);
47103 return rc;
47104 }
47105 }
47106bitvec_set_end:
47107 p->nSet++;
47108 p->u.aHash[h] = i;
47109 return SQLITE_OK;
47110}
47111
47112/*
47113** Clear the i-th bit.
47114**
47115** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
47116** that BitvecClear can use to rebuilt its hash table.
47117*/
47118SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
47119 if( p==0 ) return;
47120 assert( i>0 );
47121 i--;
47122 while( p->iDivisor ){
47123 u32 bin = i/p->iDivisor;
47124 i = i%p->iDivisor;
47125 p = p->u.apSub[bin];
47126 if (!p) {
47127 return;
47128 }
47129 }
47130 if( p->iSize<=BITVEC_NBIT ){
47131 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
47132 }else{
47133 unsigned int j;
47134 u32 *aiValues = pBuf;
47135 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
47136 memset(p->u.aHash, 0, sizeof(p->u.aHash));
47137 p->nSet = 0;
47138 for(j=0; j<BITVEC_NINT; j++){
47139 if( aiValues[j] && aiValues[j]!=(i+1) ){
47140 u32 h = BITVEC_HASH(aiValues[j]-1);
47141 p->nSet++;
47142 while( p->u.aHash[h] ){
47143 h++;
47144 if( h>=BITVEC_NINT ) h = 0;
47145 }
47146 p->u.aHash[h] = aiValues[j];
47147 }
47148 }
47149 }
47150}
47151
47152/*
47153** Destroy a bitmap object. Reclaim all memory used.
47154*/
47155SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
47156 if( p==0 ) return;
47157 if( p->iDivisor ){
47158 unsigned int i;
47159 for(i=0; i<BITVEC_NPTR; i++){
47160 sqlite3BitvecDestroy(p->u.apSub[i]);
47161 }
47162 }
47163 sqlite3_free(p);
47164}
47165
47166/*
47167** Return the value of the iSize parameter specified when Bitvec *p
47168** was created.
47169*/
47170SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
47171 return p->iSize;
47172}
47173
47174#ifndef SQLITE_UNTESTABLE
47175/*
47176** Let V[] be an array of unsigned characters sufficient to hold
47177** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
47178** Then the following macros can be used to set, clear, or test
47179** individual bits within V.
47180*/
47181#define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
47182#define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
47183#define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
47184
47185/*
47186** This routine runs an extensive test of the Bitvec code.
47187**
47188** The input is an array of integers that acts as a program
47189** to test the Bitvec. The integers are opcodes followed
47190** by 0, 1, or 3 operands, depending on the opcode. Another
47191** opcode follows immediately after the last operand.
47192**
47193** There are 6 opcodes numbered from 0 through 5. 0 is the
47194** "halt" opcode and causes the test to end.
47195**
47196** 0 Halt and return the number of errors
47197** 1 N S X Set N bits beginning with S and incrementing by X
47198** 2 N S X Clear N bits beginning with S and incrementing by X
47199** 3 N Set N randomly chosen bits
47200** 4 N Clear N randomly chosen bits
47201** 5 N S X Set N bits from S increment X in array only, not in bitvec
47202**
47203** The opcodes 1 through 4 perform set and clear operations are performed
47204** on both a Bitvec object and on a linear array of bits obtained from malloc.
47205** Opcode 5 works on the linear array only, not on the Bitvec.
47206** Opcode 5 is used to deliberately induce a fault in order to
47207** confirm that error detection works.
47208**
47209** At the conclusion of the test the linear array is compared
47210** against the Bitvec object. If there are any differences,
47211** an error is returned. If they are the same, zero is returned.
47212**
47213** If a memory allocation error occurs, return -1.
47214*/
47215SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
47216 Bitvec *pBitvec = 0;
47217 unsigned char *pV = 0;
47218 int rc = -1;
47219 int i, nx, pc, op;
47220 void *pTmpSpace;
47221
47222 /* Allocate the Bitvec to be tested and a linear array of
47223 ** bits to act as the reference */
47224 pBitvec = sqlite3BitvecCreate( sz );
47225 pV = sqlite3MallocZero( (sz+7)/8 + 1 );
47226 pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
47227 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
47228
47229 /* NULL pBitvec tests */
47230 sqlite3BitvecSet(0, 1);
47231 sqlite3BitvecClear(0, 1, pTmpSpace);
47232
47233 /* Run the program */
47234 pc = 0;
47235 while( (op = aOp[pc])!=0 ){
47236 switch( op ){
47237 case 1:
47238 case 2:
47239 case 5: {
47240 nx = 4;
47241 i = aOp[pc+2] - 1;
47242 aOp[pc+2] += aOp[pc+3];
47243 break;
47244 }
47245 case 3:
47246 case 4:
47247 default: {
47248 nx = 2;
47249 sqlite3_randomness(sizeof(i), &i);
47250 break;
47251 }
47252 }
47253 if( (--aOp[pc+1]) > 0 ) nx = 0;
47254 pc += nx;
47255 i = (i & 0x7fffffff)%sz;
47256 if( (op & 1)!=0 ){
47257 SETBIT(pV, (i+1));
47258 if( op!=5 ){
47259 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
47260 }
47261 }else{
47262 CLEARBIT(pV, (i+1));
47263 sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
47264 }
47265 }
47266
47267 /* Test to make sure the linear array exactly matches the
47268 ** Bitvec object. Start with the assumption that they do
47269 ** match (rc==0). Change rc to non-zero if a discrepancy
47270 ** is found.
47271 */
47272 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
47273 + sqlite3BitvecTest(pBitvec, 0)
47274 + (sqlite3BitvecSize(pBitvec) - sz);
47275 for(i=1; i<=sz; i++){
47276 if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
47277 rc = i;
47278 break;
47279 }
47280 }
47281
47282 /* Free allocated structure */
47283bitvec_end:
47284 sqlite3_free(pTmpSpace);
47285 sqlite3_free(pV);
47286 sqlite3BitvecDestroy(pBitvec);
47287 return rc;
47288}
47289#endif /* SQLITE_UNTESTABLE */
47290
47291/************** End of bitvec.c **********************************************/
47292/************** Begin file pcache.c ******************************************/
47293/*
47294** 2008 August 05
47295**
47296** The author disclaims copyright to this source code. In place of
47297** a legal notice, here is a blessing:
47298**
47299** May you do good and not evil.
47300** May you find forgiveness for yourself and forgive others.
47301** May you share freely, never taking more than you give.
47302**
47303*************************************************************************
47304** This file implements that page cache.
47305*/
47306/* #include "sqliteInt.h" */
47307
47308/*
47309** A complete page cache is an instance of this structure. Every
47310** entry in the cache holds a single page of the database file. The
47311** btree layer only operates on the cached copy of the database pages.
47312**
47313** A page cache entry is "clean" if it exactly matches what is currently
47314** on disk. A page is "dirty" if it has been modified and needs to be
47315** persisted to disk.
47316**
47317** pDirty, pDirtyTail, pSynced:
47318** All dirty pages are linked into the doubly linked list using
47319** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
47320** such that p was added to the list more recently than p->pDirtyNext.
47321** PCache.pDirty points to the first (newest) element in the list and
47322** pDirtyTail to the last (oldest).
47323**
47324** The PCache.pSynced variable is used to optimize searching for a dirty
47325** page to eject from the cache mid-transaction. It is better to eject
47326** a page that does not require a journal sync than one that does.
47327** Therefore, pSynced is maintained to that it *almost* always points
47328** to either the oldest page in the pDirty/pDirtyTail list that has a
47329** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
47330** (so that the right page to eject can be found by following pDirtyPrev
47331** pointers).
47332*/
47333struct PCache {
47334 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
47335 PgHdr *pSynced; /* Last synced page in dirty page list */
47336 int nRefSum; /* Sum of ref counts over all pages */
47337 int szCache; /* Configured cache size */
47338 int szSpill; /* Size before spilling occurs */
47339 int szPage; /* Size of every page in this cache */
47340 int szExtra; /* Size of extra space for each page */
47341 u8 bPurgeable; /* True if pages are on backing store */
47342 u8 eCreate; /* eCreate value for for xFetch() */
47343 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
47344 void *pStress; /* Argument to xStress */
47345 sqlite3_pcache *pCache; /* Pluggable cache module */
47346};
47347
47348/********************************** Test and Debug Logic **********************/
47349/*
47350** Debug tracing macros. Enable by by changing the "0" to "1" and
47351** recompiling.
47352**
47353** When sqlite3PcacheTrace is 1, single line trace messages are issued.
47354** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
47355** is displayed for many operations, resulting in a lot of output.
47356*/
47357#if defined(SQLITE_DEBUG) && 0
47358 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
47359 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
47360# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
47361 void pcacheDump(PCache *pCache){
47362 int N;
47363 int i, j;
47364 sqlite3_pcache_page *pLower;
47365 PgHdr *pPg;
47366 unsigned char *a;
47367
47368 if( sqlite3PcacheTrace<2 ) return;
47369 if( pCache->pCache==0 ) return;
47370 N = sqlite3PcachePagecount(pCache);
47371 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
47372 for(i=1; i<=N; i++){
47373 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
47374 if( pLower==0 ) continue;
47375 pPg = (PgHdr*)pLower->pExtra;
47376 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
47377 a = (unsigned char *)pLower->pBuf;
47378 for(j=0; j<12; j++) printf("%02x", a[j]);
47379 printf("\n");
47380 if( pPg->pPage==0 ){
47381 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
47382 }
47383 }
47384 }
47385 #else
47386# define pcacheTrace(X)
47387# define pcacheDump(X)
47388#endif
47389
47390/*
47391** Check invariants on a PgHdr entry. Return true if everything is OK.
47392** Return false if any invariant is violated.
47393**
47394** This routine is for use inside of assert() statements only. For
47395** example:
47396**
47397** assert( sqlite3PcachePageSanity(pPg) );
47398*/
47399#ifdef SQLITE_DEBUG
47400SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
47401 PCache *pCache;
47402 assert( pPg!=0 );
47403 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
47404 pCache = pPg->pCache;
47405 assert( pCache!=0 ); /* Every page has an associated PCache */
47406 if( pPg->flags & PGHDR_CLEAN ){
47407 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
47408 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
47409 assert( pCache->pDirtyTail!=pPg );
47410 }
47411 /* WRITEABLE pages must also be DIRTY */
47412 if( pPg->flags & PGHDR_WRITEABLE ){
47413 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
47414 }
47415 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
47416 ** for example, when using the sqlite3PagerDontWrite() optimization:
47417 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
47418 ** (2) Page X moved to freelist, WRITEABLE is cleared
47419 ** (3) Page X reused, WRITEABLE is set again
47420 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
47421 ** in step 3, and page might be written into the database without first
47422 ** syncing the rollback journal, which might cause corruption on a power
47423 ** loss.
47424 **
47425 ** Another example is when the database page size is smaller than the
47426 ** disk sector size. When any page of a sector is journalled, all pages
47427 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
47428 ** in case they are later modified, since all pages in the same sector
47429 ** must be journalled and synced before any of those pages can be safely
47430 ** written.
47431 */
47432 return 1;
47433}
47434#endif /* SQLITE_DEBUG */
47435
47436
47437/********************************** Linked List Management ********************/
47438
47439/* Allowed values for second argument to pcacheManageDirtyList() */
47440#define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
47441#define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
47442#define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
47443
47444/*
47445** Manage pPage's participation on the dirty list. Bits of the addRemove
47446** argument determines what operation to do. The 0x01 bit means first
47447** remove pPage from the dirty list. The 0x02 means add pPage back to
47448** the dirty list. Doing both moves pPage to the front of the dirty list.
47449*/
47450static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
47451 PCache *p = pPage->pCache;
47452
47453 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
47454 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
47455 pPage->pgno));
47456 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
47457 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
47458 assert( pPage->pDirtyPrev || pPage==p->pDirty );
47459
47460 /* Update the PCache1.pSynced variable if necessary. */
47461 if( p->pSynced==pPage ){
47462 p->pSynced = pPage->pDirtyPrev;
47463 }
47464
47465 if( pPage->pDirtyNext ){
47466 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
47467 }else{
47468 assert( pPage==p->pDirtyTail );
47469 p->pDirtyTail = pPage->pDirtyPrev;
47470 }
47471 if( pPage->pDirtyPrev ){
47472 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
47473 }else{
47474 /* If there are now no dirty pages in the cache, set eCreate to 2.
47475 ** This is an optimization that allows sqlite3PcacheFetch() to skip
47476 ** searching for a dirty page to eject from the cache when it might
47477 ** otherwise have to. */
47478 assert( pPage==p->pDirty );
47479 p->pDirty = pPage->pDirtyNext;
47480 assert( p->bPurgeable || p->eCreate==2 );
47481 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
47482 assert( p->bPurgeable==0 || p->eCreate==1 );
47483 p->eCreate = 2;
47484 }
47485 }
47486 }
47487 if( addRemove & PCACHE_DIRTYLIST_ADD ){
47488 pPage->pDirtyPrev = 0;
47489 pPage->pDirtyNext = p->pDirty;
47490 if( pPage->pDirtyNext ){
47491 assert( pPage->pDirtyNext->pDirtyPrev==0 );
47492 pPage->pDirtyNext->pDirtyPrev = pPage;
47493 }else{
47494 p->pDirtyTail = pPage;
47495 if( p->bPurgeable ){
47496 assert( p->eCreate==2 );
47497 p->eCreate = 1;
47498 }
47499 }
47500 p->pDirty = pPage;
47501
47502 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
47503 ** pSynced to point to it. Checking the NEED_SYNC flag is an
47504 ** optimization, as if pSynced points to a page with the NEED_SYNC
47505 ** flag set sqlite3PcacheFetchStress() searches through all newer
47506 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
47507 if( !p->pSynced
47508 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
47509 ){
47510 p->pSynced = pPage;
47511 }
47512 }
47513 pcacheDump(p);
47514}
47515
47516/*
47517** Wrapper around the pluggable caches xUnpin method. If the cache is
47518** being used for an in-memory database, this function is a no-op.
47519*/
47520static void pcacheUnpin(PgHdr *p){
47521 if( p->pCache->bPurgeable ){
47522 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
47523 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
47524 pcacheDump(p->pCache);
47525 }
47526}
47527
47528/*
47529** Compute the number of pages of cache requested. p->szCache is the
47530** cache size requested by the "PRAGMA cache_size" statement.
47531*/
47532static int numberOfCachePages(PCache *p){
47533 if( p->szCache>=0 ){
47534 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
47535 ** suggested cache size is set to N. */
47536 return p->szCache;
47537 }else{
47538 /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then
47539 ** the number of cache pages is adjusted to use approximately abs(N*1024)
47540 ** bytes of memory. */
47541 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
47542 }
47543}
47544
47545/*************************************************** General Interfaces ******
47546**
47547** Initialize and shutdown the page cache subsystem. Neither of these
47548** functions are threadsafe.
47549*/
47550SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
47551 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
47552 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
47553 ** built-in default page cache is used instead of the application defined
47554 ** page cache. */
47555 sqlite3PCacheSetDefault();
47556 }
47557 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
47558}
47559SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
47560 if( sqlite3GlobalConfig.pcache2.xShutdown ){
47561 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
47562 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
47563 }
47564}
47565
47566/*
47567** Return the size in bytes of a PCache object.
47568*/
47569SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
47570
47571/*
47572** Create a new PCache object. Storage space to hold the object
47573** has already been allocated and is passed in as the p pointer.
47574** The caller discovers how much space needs to be allocated by
47575** calling sqlite3PcacheSize().
47576**
47577** szExtra is some extra space allocated for each page. The first
47578** 8 bytes of the extra space will be zeroed as the page is allocated,
47579** but remaining content will be uninitialized. Though it is opaque
47580** to this module, the extra space really ends up being the MemPage
47581** structure in the pager.
47582*/
47583SQLITE_PRIVATE int sqlite3PcacheOpen(
47584 int szPage, /* Size of every page */
47585 int szExtra, /* Extra space associated with each page */
47586 int bPurgeable, /* True if pages are on backing store */
47587 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
47588 void *pStress, /* Argument to xStress */
47589 PCache *p /* Preallocated space for the PCache */
47590){
47591 memset(p, 0, sizeof(PCache));
47592 p->szPage = 1;
47593 p->szExtra = szExtra;
47594 assert( szExtra>=8 ); /* First 8 bytes will be zeroed */
47595 p->bPurgeable = bPurgeable;
47596 p->eCreate = 2;
47597 p->xStress = xStress;
47598 p->pStress = pStress;
47599 p->szCache = 100;
47600 p->szSpill = 1;
47601 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
47602 return sqlite3PcacheSetPageSize(p, szPage);
47603}
47604
47605/*
47606** Change the page size for PCache object. The caller must ensure that there
47607** are no outstanding page references when this function is called.
47608*/
47609SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
47610 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
47611 if( pCache->szPage ){
47612 sqlite3_pcache *pNew;
47613 pNew = sqlite3GlobalConfig.pcache2.xCreate(
47614 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
47615 pCache->bPurgeable
47616 );
47617 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
47618 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
47619 if( pCache->pCache ){
47620 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
47621 }
47622 pCache->pCache = pNew;
47623 pCache->szPage = szPage;
47624 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
47625 }
47626 return SQLITE_OK;
47627}
47628
47629/*
47630** Try to obtain a page from the cache.
47631**
47632** This routine returns a pointer to an sqlite3_pcache_page object if
47633** such an object is already in cache, or if a new one is created.
47634** This routine returns a NULL pointer if the object was not in cache
47635** and could not be created.
47636**
47637** The createFlags should be 0 to check for existing pages and should
47638** be 3 (not 1, but 3) to try to create a new page.
47639**
47640** If the createFlag is 0, then NULL is always returned if the page
47641** is not already in the cache. If createFlag is 1, then a new page
47642** is created only if that can be done without spilling dirty pages
47643** and without exceeding the cache size limit.
47644**
47645** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
47646** initialize the sqlite3_pcache_page object and convert it into a
47647** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
47648** routines are split this way for performance reasons. When separated
47649** they can both (usually) operate without having to push values to
47650** the stack on entry and pop them back off on exit, which saves a
47651** lot of pushing and popping.
47652*/
47653SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(
47654 PCache *pCache, /* Obtain the page from this cache */
47655 Pgno pgno, /* Page number to obtain */
47656 int createFlag /* If true, create page if it does not exist already */
47657){
47658 int eCreate;
47659 sqlite3_pcache_page *pRes;
47660
47661 assert( pCache!=0 );
47662 assert( pCache->pCache!=0 );
47663 assert( createFlag==3 || createFlag==0 );
47664 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
47665
47666 /* eCreate defines what to do if the page does not exist.
47667 ** 0 Do not allocate a new page. (createFlag==0)
47668 ** 1 Allocate a new page if doing so is inexpensive.
47669 ** (createFlag==1 AND bPurgeable AND pDirty)
47670 ** 2 Allocate a new page even it doing so is difficult.
47671 ** (createFlag==1 AND !(bPurgeable AND pDirty)
47672 */
47673 eCreate = createFlag & pCache->eCreate;
47674 assert( eCreate==0 || eCreate==1 || eCreate==2 );
47675 assert( createFlag==0 || pCache->eCreate==eCreate );
47676 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
47677 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
47678 pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
47679 createFlag?" create":"",pRes));
47680 return pRes;
47681}
47682
47683/*
47684** If the sqlite3PcacheFetch() routine is unable to allocate a new
47685** page because no clean pages are available for reuse and the cache
47686** size limit has been reached, then this routine can be invoked to
47687** try harder to allocate a page. This routine might invoke the stress
47688** callback to spill dirty pages to the journal. It will then try to
47689** allocate the new page and will only fail to allocate a new page on
47690** an OOM error.
47691**
47692** This routine should be invoked only after sqlite3PcacheFetch() fails.
47693*/
47694SQLITE_PRIVATE int sqlite3PcacheFetchStress(
47695 PCache *pCache, /* Obtain the page from this cache */
47696 Pgno pgno, /* Page number to obtain */
47697 sqlite3_pcache_page **ppPage /* Write result here */
47698){
47699 PgHdr *pPg;
47700 if( pCache->eCreate==2 ) return 0;
47701
47702 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
47703 /* Find a dirty page to write-out and recycle. First try to find a
47704 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
47705 ** cleared), but if that is not possible settle for any other
47706 ** unreferenced dirty page.
47707 **
47708 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
47709 ** flag is currently referenced, then the following may leave pSynced
47710 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
47711 ** cleared). This is Ok, as pSynced is just an optimization. */
47712 for(pPg=pCache->pSynced;
47713 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
47714 pPg=pPg->pDirtyPrev
47715 );
47716 pCache->pSynced = pPg;
47717 if( !pPg ){
47718 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
47719 }
47720 if( pPg ){
47721 int rc;
47722#ifdef SQLITE_LOG_CACHE_SPILL
47723 sqlite3_log(SQLITE_FULL,
47724 "spill page %d making room for %d - cache used: %d/%d",
47725 pPg->pgno, pgno,
47726 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
47727 numberOfCachePages(pCache));
47728#endif
47729 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
47730 rc = pCache->xStress(pCache->pStress, pPg);
47731 pcacheDump(pCache);
47732 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
47733 return rc;
47734 }
47735 }
47736 }
47737 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
47738 return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
47739}
47740
47741/*
47742** This is a helper routine for sqlite3PcacheFetchFinish()
47743**
47744** In the uncommon case where the page being fetched has not been
47745** initialized, this routine is invoked to do the initialization.
47746** This routine is broken out into a separate function since it
47747** requires extra stack manipulation that can be avoided in the common
47748** case.
47749*/
47750static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
47751 PCache *pCache, /* Obtain the page from this cache */
47752 Pgno pgno, /* Page number obtained */
47753 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
47754){
47755 PgHdr *pPgHdr;
47756 assert( pPage!=0 );
47757 pPgHdr = (PgHdr*)pPage->pExtra;
47758 assert( pPgHdr->pPage==0 );
47759 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
47760 pPgHdr->pPage = pPage;
47761 pPgHdr->pData = pPage->pBuf;
47762 pPgHdr->pExtra = (void *)&pPgHdr[1];
47763 memset(pPgHdr->pExtra, 0, 8);
47764 pPgHdr->pCache = pCache;
47765 pPgHdr->pgno = pgno;
47766 pPgHdr->flags = PGHDR_CLEAN;
47767 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
47768}
47769
47770/*
47771** This routine converts the sqlite3_pcache_page object returned by
47772** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
47773** must be called after sqlite3PcacheFetch() in order to get a usable
47774** result.
47775*/
47776SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(
47777 PCache *pCache, /* Obtain the page from this cache */
47778 Pgno pgno, /* Page number obtained */
47779 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
47780){
47781 PgHdr *pPgHdr;
47782
47783 assert( pPage!=0 );
47784 pPgHdr = (PgHdr *)pPage->pExtra;
47785
47786 if( !pPgHdr->pPage ){
47787 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
47788 }
47789 pCache->nRefSum++;
47790 pPgHdr->nRef++;
47791 assert( sqlite3PcachePageSanity(pPgHdr) );
47792 return pPgHdr;
47793}
47794
47795/*
47796** Decrement the reference count on a page. If the page is clean and the
47797** reference count drops to 0, then it is made eligible for recycling.
47798*/
47799SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
47800 assert( p->nRef>0 );
47801 p->pCache->nRefSum--;
47802 if( (--p->nRef)==0 ){
47803 if( p->flags&PGHDR_CLEAN ){
47804 pcacheUnpin(p);
47805 }else{
47806 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
47807 }
47808 }
47809}
47810
47811/*
47812** Increase the reference count of a supplied page by 1.
47813*/
47814SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
47815 assert(p->nRef>0);
47816 assert( sqlite3PcachePageSanity(p) );
47817 p->nRef++;
47818 p->pCache->nRefSum++;
47819}
47820
47821/*
47822** Drop a page from the cache. There must be exactly one reference to the
47823** page. This function deletes that reference, so after it returns the
47824** page pointed to by p is invalid.
47825*/
47826SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
47827 assert( p->nRef==1 );
47828 assert( sqlite3PcachePageSanity(p) );
47829 if( p->flags&PGHDR_DIRTY ){
47830 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
47831 }
47832 p->pCache->nRefSum--;
47833 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
47834}
47835
47836/*
47837** Make sure the page is marked as dirty. If it isn't dirty already,
47838** make it so.
47839*/
47840SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
47841 assert( p->nRef>0 );
47842 assert( sqlite3PcachePageSanity(p) );
47843 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
47844 p->flags &= ~PGHDR_DONT_WRITE;
47845 if( p->flags & PGHDR_CLEAN ){
47846 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
47847 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
47848 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
47849 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
47850 }
47851 assert( sqlite3PcachePageSanity(p) );
47852 }
47853}
47854
47855/*
47856** Make sure the page is marked as clean. If it isn't clean already,
47857** make it so.
47858*/
47859SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
47860 assert( sqlite3PcachePageSanity(p) );
47861 assert( (p->flags & PGHDR_DIRTY)!=0 );
47862 assert( (p->flags & PGHDR_CLEAN)==0 );
47863 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
47864 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
47865 p->flags |= PGHDR_CLEAN;
47866 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
47867 assert( sqlite3PcachePageSanity(p) );
47868 if( p->nRef==0 ){
47869 pcacheUnpin(p);
47870 }
47871}
47872
47873/*
47874** Make every page in the cache clean.
47875*/
47876SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
47877 PgHdr *p;
47878 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
47879 while( (p = pCache->pDirty)!=0 ){
47880 sqlite3PcacheMakeClean(p);
47881 }
47882}
47883
47884/*
47885** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
47886*/
47887SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache *pCache){
47888 PgHdr *p;
47889 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
47890 for(p=pCache->pDirty; p; p=p->pDirtyNext){
47891 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
47892 }
47893 pCache->pSynced = pCache->pDirtyTail;
47894}
47895
47896/*
47897** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
47898*/
47899SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
47900 PgHdr *p;
47901 for(p=pCache->pDirty; p; p=p->pDirtyNext){
47902 p->flags &= ~PGHDR_NEED_SYNC;
47903 }
47904 pCache->pSynced = pCache->pDirtyTail;
47905}
47906
47907/*
47908** Change the page number of page p to newPgno.
47909*/
47910SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
47911 PCache *pCache = p->pCache;
47912 assert( p->nRef>0 );
47913 assert( newPgno>0 );
47914 assert( sqlite3PcachePageSanity(p) );
47915 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
47916 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
47917 p->pgno = newPgno;
47918 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
47919 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
47920 }
47921}
47922
47923/*
47924** Drop every cache entry whose page number is greater than "pgno". The
47925** caller must ensure that there are no outstanding references to any pages
47926** other than page 1 with a page number greater than pgno.
47927**
47928** If there is a reference to page 1 and the pgno parameter passed to this
47929** function is 0, then the data area associated with page 1 is zeroed, but
47930** the page object is not dropped.
47931*/
47932SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
47933 if( pCache->pCache ){
47934 PgHdr *p;
47935 PgHdr *pNext;
47936 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
47937 for(p=pCache->pDirty; p; p=pNext){
47938 pNext = p->pDirtyNext;
47939 /* This routine never gets call with a positive pgno except right
47940 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
47941 ** it must be that pgno==0.
47942 */
47943 assert( p->pgno>0 );
47944 if( p->pgno>pgno ){
47945 assert( p->flags&PGHDR_DIRTY );
47946 sqlite3PcacheMakeClean(p);
47947 }
47948 }
47949 if( pgno==0 && pCache->nRefSum ){
47950 sqlite3_pcache_page *pPage1;
47951 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
47952 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
47953 ** pCache->nRefSum>0 */
47954 memset(pPage1->pBuf, 0, pCache->szPage);
47955 pgno = 1;
47956 }
47957 }
47958 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
47959 }
47960}
47961
47962/*
47963** Close a cache.
47964*/
47965SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
47966 assert( pCache->pCache!=0 );
47967 pcacheTrace(("%p.CLOSE\n",pCache));
47968 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
47969}
47970
47971/*
47972** Discard the contents of the cache.
47973*/
47974SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
47975 sqlite3PcacheTruncate(pCache, 0);
47976}
47977
47978/*
47979** Merge two lists of pages connected by pDirty and in pgno order.
47980** Do not bother fixing the pDirtyPrev pointers.
47981*/
47982static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
47983 PgHdr result, *pTail;
47984 pTail = &result;
47985 assert( pA!=0 && pB!=0 );
47986 for(;;){
47987 if( pA->pgno<pB->pgno ){
47988 pTail->pDirty = pA;
47989 pTail = pA;
47990 pA = pA->pDirty;
47991 if( pA==0 ){
47992 pTail->pDirty = pB;
47993 break;
47994 }
47995 }else{
47996 pTail->pDirty = pB;
47997 pTail = pB;
47998 pB = pB->pDirty;
47999 if( pB==0 ){
48000 pTail->pDirty = pA;
48001 break;
48002 }
48003 }
48004 }
48005 return result.pDirty;
48006}
48007
48008/*
48009** Sort the list of pages in accending order by pgno. Pages are
48010** connected by pDirty pointers. The pDirtyPrev pointers are
48011** corrupted by this sort.
48012**
48013** Since there cannot be more than 2^31 distinct pages in a database,
48014** there cannot be more than 31 buckets required by the merge sorter.
48015** One extra bucket is added to catch overflow in case something
48016** ever changes to make the previous sentence incorrect.
48017*/
48018#define N_SORT_BUCKET 32
48019static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
48020 PgHdr *a[N_SORT_BUCKET], *p;
48021 int i;
48022 memset(a, 0, sizeof(a));
48023 while( pIn ){
48024 p = pIn;
48025 pIn = p->pDirty;
48026 p->pDirty = 0;
48027 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
48028 if( a[i]==0 ){
48029 a[i] = p;
48030 break;
48031 }else{
48032 p = pcacheMergeDirtyList(a[i], p);
48033 a[i] = 0;
48034 }
48035 }
48036 if( NEVER(i==N_SORT_BUCKET-1) ){
48037 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
48038 ** the input list. But that is impossible.
48039 */
48040 a[i] = pcacheMergeDirtyList(a[i], p);
48041 }
48042 }
48043 p = a[0];
48044 for(i=1; i<N_SORT_BUCKET; i++){
48045 if( a[i]==0 ) continue;
48046 p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
48047 }
48048 return p;
48049}
48050
48051/*
48052** Return a list of all dirty pages in the cache, sorted by page number.
48053*/
48054SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
48055 PgHdr *p;
48056 for(p=pCache->pDirty; p; p=p->pDirtyNext){
48057 p->pDirty = p->pDirtyNext;
48058 }
48059 return pcacheSortDirtyList(pCache->pDirty);
48060}
48061
48062/*
48063** Return the total number of references to all pages held by the cache.
48064**
48065** This is not the total number of pages referenced, but the sum of the
48066** reference count for all pages.
48067*/
48068SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
48069 return pCache->nRefSum;
48070}
48071
48072/*
48073** Return the number of references to the page supplied as an argument.
48074*/
48075SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
48076 return p->nRef;
48077}
48078
48079/*
48080** Return the total number of pages in the cache.
48081*/
48082SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
48083 assert( pCache->pCache!=0 );
48084 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
48085}
48086
48087#ifdef SQLITE_TEST
48088/*
48089** Get the suggested cache-size value.
48090*/
48091SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
48092 return numberOfCachePages(pCache);
48093}
48094#endif
48095
48096/*
48097** Set the suggested cache-size value.
48098*/
48099SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
48100 assert( pCache->pCache!=0 );
48101 pCache->szCache = mxPage;
48102 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
48103 numberOfCachePages(pCache));
48104}
48105
48106/*
48107** Set the suggested cache-spill value. Make no changes if if the
48108** argument is zero. Return the effective cache-spill size, which will
48109** be the larger of the szSpill and szCache.
48110*/
48111SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
48112 int res;
48113 assert( p->pCache!=0 );
48114 if( mxPage ){
48115 if( mxPage<0 ){
48116 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
48117 }
48118 p->szSpill = mxPage;
48119 }
48120 res = numberOfCachePages(p);
48121 if( res<p->szSpill ) res = p->szSpill;
48122 return res;
48123}
48124
48125/*
48126** Free up as much memory as possible from the page cache.
48127*/
48128SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
48129 assert( pCache->pCache!=0 );
48130 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
48131}
48132
48133/*
48134** Return the size of the header added by this middleware layer
48135** in the page-cache hierarchy.
48136*/
48137SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
48138
48139/*
48140** Return the number of dirty pages currently in the cache, as a percentage
48141** of the configured cache size.
48142*/
48143SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
48144 PgHdr *pDirty;
48145 int nDirty = 0;
48146 int nCache = numberOfCachePages(pCache);
48147 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
48148 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
48149}
48150
48151#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
48152/*
48153** For all dirty pages currently in the cache, invoke the specified
48154** callback. This is only used if the SQLITE_CHECK_PAGES macro is
48155** defined.
48156*/
48157SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
48158 PgHdr *pDirty;
48159 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
48160 xIter(pDirty);
48161 }
48162}
48163#endif
48164
48165/************** End of pcache.c **********************************************/
48166/************** Begin file pcache1.c *****************************************/
48167/*
48168** 2008 November 05
48169**
48170** The author disclaims copyright to this source code. In place of
48171** a legal notice, here is a blessing:
48172**
48173** May you do good and not evil.
48174** May you find forgiveness for yourself and forgive others.
48175** May you share freely, never taking more than you give.
48176**
48177*************************************************************************
48178**
48179** This file implements the default page cache implementation (the
48180** sqlite3_pcache interface). It also contains part of the implementation
48181** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
48182** If the default page cache implementation is overridden, then neither of
48183** these two features are available.
48184**
48185** A Page cache line looks like this:
48186**
48187** -------------------------------------------------------------
48188** | database page content | PgHdr1 | MemPage | PgHdr |
48189** -------------------------------------------------------------
48190**
48191** The database page content is up front (so that buffer overreads tend to
48192** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage
48193** is the extension added by the btree.c module containing information such
48194** as the database page number and how that database page is used. PgHdr
48195** is added by the pcache.c layer and contains information used to keep track
48196** of which pages are "dirty". PgHdr1 is an extension added by this
48197** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page.
48198** PgHdr1 contains information needed to look up a page by its page number.
48199** The superclass sqlite3_pcache_page.pBuf points to the start of the
48200** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
48201**
48202** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
48203** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
48204** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
48205** size can vary according to architecture, compile-time options, and
48206** SQLite library version number.
48207**
48208** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained
48209** using a separate memory allocation from the database page content. This
48210** seeks to overcome the "clownshoe" problem (also called "internal
48211** fragmentation" in academic literature) of allocating a few bytes more
48212** than a power of two with the memory allocator rounding up to the next
48213** power of two, and leaving the rounded-up space unused.
48214**
48215** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
48216** with this module. Information is passed back and forth as PgHdr1 pointers.
48217**
48218** The pcache.c and pager.c modules deal pointers to PgHdr objects.
48219** The btree.c module deals with pointers to MemPage objects.
48220**
48221** SOURCE OF PAGE CACHE MEMORY:
48222**
48223** Memory for a page might come from any of three sources:
48224**
48225** (1) The general-purpose memory allocator - sqlite3Malloc()
48226** (2) Global page-cache memory provided using sqlite3_config() with
48227** SQLITE_CONFIG_PAGECACHE.
48228** (3) PCache-local bulk allocation.
48229**
48230** The third case is a chunk of heap memory (defaulting to 100 pages worth)
48231** that is allocated when the page cache is created. The size of the local
48232** bulk allocation can be adjusted using
48233**
48234** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
48235**
48236** If N is positive, then N pages worth of memory are allocated using a single
48237** sqlite3Malloc() call and that memory is used for the first N pages allocated.
48238** Or if N is negative, then -1024*N bytes of memory are allocated and used
48239** for as many pages as can be accomodated.
48240**
48241** Only one of (2) or (3) can be used. Once the memory available to (2) or
48242** (3) is exhausted, subsequent allocations fail over to the general-purpose
48243** memory allocator (1).
48244**
48245** Earlier versions of SQLite used only methods (1) and (2). But experiments
48246** show that method (3) with N==100 provides about a 5% performance boost for
48247** common workloads.
48248*/
48249/* #include "sqliteInt.h" */
48250
48251typedef struct PCache1 PCache1;
48252typedef struct PgHdr1 PgHdr1;
48253typedef struct PgFreeslot PgFreeslot;
48254typedef struct PGroup PGroup;
48255
48256/*
48257** Each cache entry is represented by an instance of the following
48258** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
48259** PgHdr1.pCache->szPage bytes is allocated directly before this structure
48260** in memory.
48261*/
48262struct PgHdr1 {
48263 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
48264 unsigned int iKey; /* Key value (page number) */
48265 u8 isBulkLocal; /* This page from bulk local storage */
48266 u8 isAnchor; /* This is the PGroup.lru element */
48267 PgHdr1 *pNext; /* Next in hash table chain */
48268 PCache1 *pCache; /* Cache that currently owns this page */
48269 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
48270 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
48271};
48272
48273/*
48274** A page is pinned if it is no on the LRU list
48275*/
48276#define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
48277#define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
48278
48279/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
48280** of one or more PCaches that are able to recycle each other's unpinned
48281** pages when they are under memory pressure. A PGroup is an instance of
48282** the following object.
48283**
48284** This page cache implementation works in one of two modes:
48285**
48286** (1) Every PCache is the sole member of its own PGroup. There is
48287** one PGroup per PCache.
48288**
48289** (2) There is a single global PGroup that all PCaches are a member
48290** of.
48291**
48292** Mode 1 uses more memory (since PCache instances are not able to rob
48293** unused pages from other PCaches) but it also operates without a mutex,
48294** and is therefore often faster. Mode 2 requires a mutex in order to be
48295** threadsafe, but recycles pages more efficiently.
48296**
48297** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
48298** PGroup which is the pcache1.grp global variable and its mutex is
48299** SQLITE_MUTEX_STATIC_LRU.
48300*/
48301struct PGroup {
48302 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
48303 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
48304 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
48305 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
48306 unsigned int nPurgeable; /* Number of purgeable pages allocated */
48307 PgHdr1 lru; /* The beginning and end of the LRU list */
48308};
48309
48310/* Each page cache is an instance of the following object. Every
48311** open database file (including each in-memory database and each
48312** temporary or transient database) has a single page cache which
48313** is an instance of this object.
48314**
48315** Pointers to structures of this type are cast and returned as
48316** opaque sqlite3_pcache* handles.
48317*/
48318struct PCache1 {
48319 /* Cache configuration parameters. Page size (szPage) and the purgeable
48320 ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
48321 ** cache is created and are never changed thereafter. nMax may be
48322 ** modified at any time by a call to the pcache1Cachesize() method.
48323 ** The PGroup mutex must be held when accessing nMax.
48324 */
48325 PGroup *pGroup; /* PGroup this cache belongs to */
48326 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
48327 int szPage; /* Size of database content section */
48328 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
48329 int szAlloc; /* Total size of one pcache line */
48330 int bPurgeable; /* True if cache is purgeable */
48331 unsigned int nMin; /* Minimum number of pages reserved */
48332 unsigned int nMax; /* Configured "cache_size" value */
48333 unsigned int n90pct; /* nMax*9/10 */
48334 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
48335
48336 /* Hash table of all pages. The following variables may only be accessed
48337 ** when the accessor is holding the PGroup mutex.
48338 */
48339 unsigned int nRecyclable; /* Number of pages in the LRU list */
48340 unsigned int nPage; /* Total number of pages in apHash */
48341 unsigned int nHash; /* Number of slots in apHash[] */
48342 PgHdr1 **apHash; /* Hash table for fast lookup by key */
48343 PgHdr1 *pFree; /* List of unused pcache-local pages */
48344 void *pBulk; /* Bulk memory used by pcache-local */
48345};
48346
48347/*
48348** Free slots in the allocator used to divide up the global page cache
48349** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
48350*/
48351struct PgFreeslot {
48352 PgFreeslot *pNext; /* Next free slot */
48353};
48354
48355/*
48356** Global data used by this cache.
48357*/
48358static SQLITE_WSD struct PCacheGlobal {
48359 PGroup grp; /* The global PGroup for mode (2) */
48360
48361 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
48362 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
48363 ** fixed at sqlite3_initialize() time and do not require mutex protection.
48364 ** The nFreeSlot and pFree values do require mutex protection.
48365 */
48366 int isInit; /* True if initialized */
48367 int separateCache; /* Use a new PGroup for each PCache */
48368 int nInitPage; /* Initial bulk allocation size */
48369 int szSlot; /* Size of each free slot */
48370 int nSlot; /* The number of pcache slots */
48371 int nReserve; /* Try to keep nFreeSlot above this */
48372 void *pStart, *pEnd; /* Bounds of global page cache memory */
48373 /* Above requires no mutex. Use mutex below for variable that follow. */
48374 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
48375 PgFreeslot *pFree; /* Free page blocks */
48376 int nFreeSlot; /* Number of unused pcache slots */
48377 /* The following value requires a mutex to change. We skip the mutex on
48378 ** reading because (1) most platforms read a 32-bit integer atomically and
48379 ** (2) even if an incorrect value is read, no great harm is done since this
48380 ** is really just an optimization. */
48381 int bUnderPressure; /* True if low on PAGECACHE memory */
48382} pcache1_g;
48383
48384/*
48385** All code in this file should access the global structure above via the
48386** alias "pcache1". This ensures that the WSD emulation is used when
48387** compiling for systems that do not support real WSD.
48388*/
48389#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
48390
48391/*
48392** Macros to enter and leave the PCache LRU mutex.
48393*/
48394#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
48395# define pcache1EnterMutex(X) assert((X)->mutex==0)
48396# define pcache1LeaveMutex(X) assert((X)->mutex==0)
48397# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
48398#else
48399# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
48400# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
48401# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
48402#endif
48403
48404/******************************************************************************/
48405/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
48406
48407
48408/*
48409** This function is called during initialization if a static buffer is
48410** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
48411** verb to sqlite3_config(). Parameter pBuf points to an allocation large
48412** enough to contain 'n' buffers of 'sz' bytes each.
48413**
48414** This routine is called from sqlite3_initialize() and so it is guaranteed
48415** to be serialized already. There is no need for further mutexing.
48416*/
48417SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
48418 if( pcache1.isInit ){
48419 PgFreeslot *p;
48420 if( pBuf==0 ) sz = n = 0;
48421 if( n==0 ) sz = 0;
48422 sz = ROUNDDOWN8(sz);
48423 pcache1.szSlot = sz;
48424 pcache1.nSlot = pcache1.nFreeSlot = n;
48425 pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
48426 pcache1.pStart = pBuf;
48427 pcache1.pFree = 0;
48428 pcache1.bUnderPressure = 0;
48429 while( n-- ){
48430 p = (PgFreeslot*)pBuf;
48431 p->pNext = pcache1.pFree;
48432 pcache1.pFree = p;
48433 pBuf = (void*)&((char*)pBuf)[sz];
48434 }
48435 pcache1.pEnd = pBuf;
48436 }
48437}
48438
48439/*
48440** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
48441** true if pCache->pFree ends up containing one or more free pages.
48442*/
48443static int pcache1InitBulk(PCache1 *pCache){
48444 i64 szBulk;
48445 char *zBulk;
48446 if( pcache1.nInitPage==0 ) return 0;
48447 /* Do not bother with a bulk allocation if the cache size very small */
48448 if( pCache->nMax<3 ) return 0;
48449 sqlite3BeginBenignMalloc();
48450 if( pcache1.nInitPage>0 ){
48451 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
48452 }else{
48453 szBulk = -1024 * (i64)pcache1.nInitPage;
48454 }
48455 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
48456 szBulk = pCache->szAlloc*(i64)pCache->nMax;
48457 }
48458 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
48459 sqlite3EndBenignMalloc();
48460 if( zBulk ){
48461 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
48462 do{
48463 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
48464 pX->page.pBuf = zBulk;
48465 pX->page.pExtra = &pX[1];
48466 pX->isBulkLocal = 1;
48467 pX->isAnchor = 0;
48468 pX->pNext = pCache->pFree;
48469 pCache->pFree = pX;
48470 zBulk += pCache->szAlloc;
48471 }while( --nBulk );
48472 }
48473 return pCache->pFree!=0;
48474}
48475
48476/*
48477** Malloc function used within this file to allocate space from the buffer
48478** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
48479** such buffer exists or there is no space left in it, this function falls
48480** back to sqlite3Malloc().
48481**
48482** Multiple threads can run this routine at the same time. Global variables
48483** in pcache1 need to be protected via mutex.
48484*/
48485static void *pcache1Alloc(int nByte){
48486 void *p = 0;
48487 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
48488 if( nByte<=pcache1.szSlot ){
48489 sqlite3_mutex_enter(pcache1.mutex);
48490 p = (PgHdr1 *)pcache1.pFree;
48491 if( p ){
48492 pcache1.pFree = pcache1.pFree->pNext;
48493 pcache1.nFreeSlot--;
48494 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
48495 assert( pcache1.nFreeSlot>=0 );
48496 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
48497 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
48498 }
48499 sqlite3_mutex_leave(pcache1.mutex);
48500 }
48501 if( p==0 ){
48502 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
48503 ** it from sqlite3Malloc instead.
48504 */
48505 p = sqlite3Malloc(nByte);
48506#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
48507 if( p ){
48508 int sz = sqlite3MallocSize(p);
48509 sqlite3_mutex_enter(pcache1.mutex);
48510 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
48511 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
48512 sqlite3_mutex_leave(pcache1.mutex);
48513 }
48514#endif
48515 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
48516 }
48517 return p;
48518}
48519
48520/*
48521** Free an allocated buffer obtained from pcache1Alloc().
48522*/
48523static void pcache1Free(void *p){
48524 if( p==0 ) return;
48525 if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){
48526 PgFreeslot *pSlot;
48527 sqlite3_mutex_enter(pcache1.mutex);
48528 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
48529 pSlot = (PgFreeslot*)p;
48530 pSlot->pNext = pcache1.pFree;
48531 pcache1.pFree = pSlot;
48532 pcache1.nFreeSlot++;
48533 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
48534 assert( pcache1.nFreeSlot<=pcache1.nSlot );
48535 sqlite3_mutex_leave(pcache1.mutex);
48536 }else{
48537 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
48538 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
48539#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
48540 {
48541 int nFreed = 0;
48542 nFreed = sqlite3MallocSize(p);
48543 sqlite3_mutex_enter(pcache1.mutex);
48544 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
48545 sqlite3_mutex_leave(pcache1.mutex);
48546 }
48547#endif
48548 sqlite3_free(p);
48549 }
48550}
48551
48552#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
48553/*
48554** Return the size of a pcache allocation
48555*/
48556static int pcache1MemSize(void *p){
48557 if( p>=pcache1.pStart && p<pcache1.pEnd ){
48558 return pcache1.szSlot;
48559 }else{
48560 int iSize;
48561 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
48562 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
48563 iSize = sqlite3MallocSize(p);
48564 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
48565 return iSize;
48566 }
48567}
48568#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
48569
48570/*
48571** Allocate a new page object initially associated with cache pCache.
48572*/
48573static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
48574 PgHdr1 *p = 0;
48575 void *pPg;
48576
48577 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
48578 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
48579 p = pCache->pFree;
48580 pCache->pFree = p->pNext;
48581 p->pNext = 0;
48582 }else{
48583#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
48584 /* The group mutex must be released before pcache1Alloc() is called. This
48585 ** is because it might call sqlite3_release_memory(), which assumes that
48586 ** this mutex is not held. */
48587 assert( pcache1.separateCache==0 );
48588 assert( pCache->pGroup==&pcache1.grp );
48589 pcache1LeaveMutex(pCache->pGroup);
48590#endif
48591 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
48592#ifdef SQLITE_PCACHE_SEPARATE_HEADER
48593 pPg = pcache1Alloc(pCache->szPage);
48594 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
48595 if( !pPg || !p ){
48596 pcache1Free(pPg);
48597 sqlite3_free(p);
48598 pPg = 0;
48599 }
48600#else
48601 pPg = pcache1Alloc(pCache->szAlloc);
48602 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
48603#endif
48604 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
48605#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
48606 pcache1EnterMutex(pCache->pGroup);
48607#endif
48608 if( pPg==0 ) return 0;
48609 p->page.pBuf = pPg;
48610 p->page.pExtra = &p[1];
48611 p->isBulkLocal = 0;
48612 p->isAnchor = 0;
48613 }
48614 (*pCache->pnPurgeable)++;
48615 return p;
48616}
48617
48618/*
48619** Free a page object allocated by pcache1AllocPage().
48620*/
48621static void pcache1FreePage(PgHdr1 *p){
48622 PCache1 *pCache;
48623 assert( p!=0 );
48624 pCache = p->pCache;
48625 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
48626 if( p->isBulkLocal ){
48627 p->pNext = pCache->pFree;
48628 pCache->pFree = p;
48629 }else{
48630 pcache1Free(p->page.pBuf);
48631#ifdef SQLITE_PCACHE_SEPARATE_HEADER
48632 sqlite3_free(p);
48633#endif
48634 }
48635 (*pCache->pnPurgeable)--;
48636}
48637
48638/*
48639** Malloc function used by SQLite to obtain space from the buffer configured
48640** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
48641** exists, this function falls back to sqlite3Malloc().
48642*/
48643SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
48644 return pcache1Alloc(sz);
48645}
48646
48647/*
48648** Free an allocated buffer obtained from sqlite3PageMalloc().
48649*/
48650SQLITE_PRIVATE void sqlite3PageFree(void *p){
48651 pcache1Free(p);
48652}
48653
48654
48655/*
48656** Return true if it desirable to avoid allocating a new page cache
48657** entry.
48658**
48659** If memory was allocated specifically to the page cache using
48660** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
48661** it is desirable to avoid allocating a new page cache entry because
48662** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
48663** for all page cache needs and we should not need to spill the
48664** allocation onto the heap.
48665**
48666** Or, the heap is used for all page cache memory but the heap is
48667** under memory pressure, then again it is desirable to avoid
48668** allocating a new page cache entry in order to avoid stressing
48669** the heap even further.
48670*/
48671static int pcache1UnderMemoryPressure(PCache1 *pCache){
48672 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
48673 return pcache1.bUnderPressure;
48674 }else{
48675 return sqlite3HeapNearlyFull();
48676 }
48677}
48678
48679/******************************************************************************/
48680/******** General Implementation Functions ************************************/
48681
48682/*
48683** This function is used to resize the hash table used by the cache passed
48684** as the first argument.
48685**
48686** The PCache mutex must be held when this function is called.
48687*/
48688static void pcache1ResizeHash(PCache1 *p){
48689 PgHdr1 **apNew;
48690 unsigned int nNew;
48691 unsigned int i;
48692
48693 assert( sqlite3_mutex_held(p->pGroup->mutex) );
48694
48695 nNew = p->nHash*2;
48696 if( nNew<256 ){
48697 nNew = 256;
48698 }
48699
48700 pcache1LeaveMutex(p->pGroup);
48701 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
48702 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
48703 if( p->nHash ){ sqlite3EndBenignMalloc(); }
48704 pcache1EnterMutex(p->pGroup);
48705 if( apNew ){
48706 for(i=0; i<p->nHash; i++){
48707 PgHdr1 *pPage;
48708 PgHdr1 *pNext = p->apHash[i];
48709 while( (pPage = pNext)!=0 ){
48710 unsigned int h = pPage->iKey % nNew;
48711 pNext = pPage->pNext;
48712 pPage->pNext = apNew[h];
48713 apNew[h] = pPage;
48714 }
48715 }
48716 sqlite3_free(p->apHash);
48717 p->apHash = apNew;
48718 p->nHash = nNew;
48719 }
48720}
48721
48722/*
48723** This function is used internally to remove the page pPage from the
48724** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
48725** LRU list, then this function is a no-op.
48726**
48727** The PGroup mutex must be held when this function is called.
48728*/
48729static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
48730 assert( pPage!=0 );
48731 assert( PAGE_IS_UNPINNED(pPage) );
48732 assert( pPage->pLruNext );
48733 assert( pPage->pLruPrev );
48734 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
48735 pPage->pLruPrev->pLruNext = pPage->pLruNext;
48736 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
48737 pPage->pLruNext = 0;
48738 pPage->pLruPrev = 0;
48739 assert( pPage->isAnchor==0 );
48740 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
48741 pPage->pCache->nRecyclable--;
48742 return pPage;
48743}
48744
48745
48746/*
48747** Remove the page supplied as an argument from the hash table
48748** (PCache1.apHash structure) that it is currently stored in.
48749** Also free the page if freePage is true.
48750**
48751** The PGroup mutex must be held when this function is called.
48752*/
48753static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
48754 unsigned int h;
48755 PCache1 *pCache = pPage->pCache;
48756 PgHdr1 **pp;
48757
48758 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
48759 h = pPage->iKey % pCache->nHash;
48760 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
48761 *pp = (*pp)->pNext;
48762
48763 pCache->nPage--;
48764 if( freeFlag ) pcache1FreePage(pPage);
48765}
48766
48767/*
48768** If there are currently more than nMaxPage pages allocated, try
48769** to recycle pages to reduce the number allocated to nMaxPage.
48770*/
48771static void pcache1EnforceMaxPage(PCache1 *pCache){
48772 PGroup *pGroup = pCache->pGroup;
48773 PgHdr1 *p;
48774 assert( sqlite3_mutex_held(pGroup->mutex) );
48775 while( pGroup->nPurgeable>pGroup->nMaxPage
48776 && (p=pGroup->lru.pLruPrev)->isAnchor==0
48777 ){
48778 assert( p->pCache->pGroup==pGroup );
48779 assert( PAGE_IS_UNPINNED(p) );
48780 pcache1PinPage(p);
48781 pcache1RemoveFromHash(p, 1);
48782 }
48783 if( pCache->nPage==0 && pCache->pBulk ){
48784 sqlite3_free(pCache->pBulk);
48785 pCache->pBulk = pCache->pFree = 0;
48786 }
48787}
48788
48789/*
48790** Discard all pages from cache pCache with a page number (key value)
48791** greater than or equal to iLimit. Any pinned pages that meet this
48792** criteria are unpinned before they are discarded.
48793**
48794** The PCache mutex must be held when this function is called.
48795*/
48796static void pcache1TruncateUnsafe(
48797 PCache1 *pCache, /* The cache to truncate */
48798 unsigned int iLimit /* Drop pages with this pgno or larger */
48799){
48800 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
48801 unsigned int h, iStop;
48802 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
48803 assert( pCache->iMaxKey >= iLimit );
48804 assert( pCache->nHash > 0 );
48805 if( pCache->iMaxKey - iLimit < pCache->nHash ){
48806 /* If we are just shaving the last few pages off the end of the
48807 ** cache, then there is no point in scanning the entire hash table.
48808 ** Only scan those hash slots that might contain pages that need to
48809 ** be removed. */
48810 h = iLimit % pCache->nHash;
48811 iStop = pCache->iMaxKey % pCache->nHash;
48812 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
48813 }else{
48814 /* This is the general case where many pages are being removed.
48815 ** It is necessary to scan the entire hash table */
48816 h = pCache->nHash/2;
48817 iStop = h - 1;
48818 }
48819 for(;;){
48820 PgHdr1 **pp;
48821 PgHdr1 *pPage;
48822 assert( h<pCache->nHash );
48823 pp = &pCache->apHash[h];
48824 while( (pPage = *pp)!=0 ){
48825 if( pPage->iKey>=iLimit ){
48826 pCache->nPage--;
48827 *pp = pPage->pNext;
48828 if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage);
48829 pcache1FreePage(pPage);
48830 }else{
48831 pp = &pPage->pNext;
48832 TESTONLY( if( nPage>=0 ) nPage++; )
48833 }
48834 }
48835 if( h==iStop ) break;
48836 h = (h+1) % pCache->nHash;
48837 }
48838 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
48839}
48840
48841/******************************************************************************/
48842/******** sqlite3_pcache Methods **********************************************/
48843
48844/*
48845** Implementation of the sqlite3_pcache.xInit method.
48846*/
48847static int pcache1Init(void *NotUsed){
48848 UNUSED_PARAMETER(NotUsed);
48849 assert( pcache1.isInit==0 );
48850 memset(&pcache1, 0, sizeof(pcache1));
48851
48852
48853 /*
48854 ** The pcache1.separateCache variable is true if each PCache has its own
48855 ** private PGroup (mode-1). pcache1.separateCache is false if the single
48856 ** PGroup in pcache1.grp is used for all page caches (mode-2).
48857 **
48858 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
48859 **
48860 ** * Use a unified cache in single-threaded applications that have
48861 ** configured a start-time buffer for use as page-cache memory using
48862 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
48863 ** pBuf argument.
48864 **
48865 ** * Otherwise use separate caches (mode-1)
48866 */
48867#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
48868 pcache1.separateCache = 0;
48869#elif SQLITE_THREADSAFE
48870 pcache1.separateCache = sqlite3GlobalConfig.pPage==0
48871 || sqlite3GlobalConfig.bCoreMutex>0;
48872#else
48873 pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
48874#endif
48875
48876#if SQLITE_THREADSAFE
48877 if( sqlite3GlobalConfig.bCoreMutex ){
48878 pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU);
48879 pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM);
48880 }
48881#endif
48882 if( pcache1.separateCache
48883 && sqlite3GlobalConfig.nPage!=0
48884 && sqlite3GlobalConfig.pPage==0
48885 ){
48886 pcache1.nInitPage = sqlite3GlobalConfig.nPage;
48887 }else{
48888 pcache1.nInitPage = 0;
48889 }
48890 pcache1.grp.mxPinned = 10;
48891 pcache1.isInit = 1;
48892 return SQLITE_OK;
48893}
48894
48895/*
48896** Implementation of the sqlite3_pcache.xShutdown method.
48897** Note that the static mutex allocated in xInit does
48898** not need to be freed.
48899*/
48900static void pcache1Shutdown(void *NotUsed){
48901 UNUSED_PARAMETER(NotUsed);
48902 assert( pcache1.isInit!=0 );
48903 memset(&pcache1, 0, sizeof(pcache1));
48904}
48905
48906/* forward declaration */
48907static void pcache1Destroy(sqlite3_pcache *p);
48908
48909/*
48910** Implementation of the sqlite3_pcache.xCreate method.
48911**
48912** Allocate a new cache.
48913*/
48914static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
48915 PCache1 *pCache; /* The newly created page cache */
48916 PGroup *pGroup; /* The group the new page cache will belong to */
48917 int sz; /* Bytes of memory required to allocate the new cache */
48918
48919 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
48920 assert( szExtra < 300 );
48921
48922 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache;
48923 pCache = (PCache1 *)sqlite3MallocZero(sz);
48924 if( pCache ){
48925 if( pcache1.separateCache ){
48926 pGroup = (PGroup*)&pCache[1];
48927 pGroup->mxPinned = 10;
48928 }else{
48929 pGroup = &pcache1.grp;
48930 }
48931 if( pGroup->lru.isAnchor==0 ){
48932 pGroup->lru.isAnchor = 1;
48933 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
48934 }
48935 pCache->pGroup = pGroup;
48936 pCache->szPage = szPage;
48937 pCache->szExtra = szExtra;
48938 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
48939 pCache->bPurgeable = (bPurgeable ? 1 : 0);
48940 pcache1EnterMutex(pGroup);
48941 pcache1ResizeHash(pCache);
48942 if( bPurgeable ){
48943 pCache->nMin = 10;
48944 pGroup->nMinPage += pCache->nMin;
48945 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
48946 pCache->pnPurgeable = &pGroup->nPurgeable;
48947 }else{
48948 static unsigned int dummyCurrentPage;
48949 pCache->pnPurgeable = &dummyCurrentPage;
48950 }
48951 pcache1LeaveMutex(pGroup);
48952 if( pCache->nHash==0 ){
48953 pcache1Destroy((sqlite3_pcache*)pCache);
48954 pCache = 0;
48955 }
48956 }
48957 return (sqlite3_pcache *)pCache;
48958}
48959
48960/*
48961** Implementation of the sqlite3_pcache.xCachesize method.
48962**
48963** Configure the cache_size limit for a cache.
48964*/
48965static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
48966 PCache1 *pCache = (PCache1 *)p;
48967 if( pCache->bPurgeable ){
48968 PGroup *pGroup = pCache->pGroup;
48969 pcache1EnterMutex(pGroup);
48970 pGroup->nMaxPage += (nMax - pCache->nMax);
48971 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
48972 pCache->nMax = nMax;
48973 pCache->n90pct = pCache->nMax*9/10;
48974 pcache1EnforceMaxPage(pCache);
48975 pcache1LeaveMutex(pGroup);
48976 }
48977}
48978
48979/*
48980** Implementation of the sqlite3_pcache.xShrink method.
48981**
48982** Free up as much memory as possible.
48983*/
48984static void pcache1Shrink(sqlite3_pcache *p){
48985 PCache1 *pCache = (PCache1*)p;
48986 if( pCache->bPurgeable ){
48987 PGroup *pGroup = pCache->pGroup;
48988 int savedMaxPage;
48989 pcache1EnterMutex(pGroup);
48990 savedMaxPage = pGroup->nMaxPage;
48991 pGroup->nMaxPage = 0;
48992 pcache1EnforceMaxPage(pCache);
48993 pGroup->nMaxPage = savedMaxPage;
48994 pcache1LeaveMutex(pGroup);
48995 }
48996}
48997
48998/*
48999** Implementation of the sqlite3_pcache.xPagecount method.
49000*/
49001static int pcache1Pagecount(sqlite3_pcache *p){
49002 int n;
49003 PCache1 *pCache = (PCache1*)p;
49004 pcache1EnterMutex(pCache->pGroup);
49005 n = pCache->nPage;
49006 pcache1LeaveMutex(pCache->pGroup);
49007 return n;
49008}
49009
49010
49011/*
49012** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
49013** in the header of the pcache1Fetch() procedure.
49014**
49015** This steps are broken out into a separate procedure because they are
49016** usually not needed, and by avoiding the stack initialization required
49017** for these steps, the main pcache1Fetch() procedure can run faster.
49018*/
49019static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
49020 PCache1 *pCache,
49021 unsigned int iKey,
49022 int createFlag
49023){
49024 unsigned int nPinned;
49025 PGroup *pGroup = pCache->pGroup;
49026 PgHdr1 *pPage = 0;
49027
49028 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
49029 assert( pCache->nPage >= pCache->nRecyclable );
49030 nPinned = pCache->nPage - pCache->nRecyclable;
49031 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
49032 assert( pCache->n90pct == pCache->nMax*9/10 );
49033 if( createFlag==1 && (
49034 nPinned>=pGroup->mxPinned
49035 || nPinned>=pCache->n90pct
49036 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
49037 )){
49038 return 0;
49039 }
49040
49041 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
49042 assert( pCache->nHash>0 && pCache->apHash );
49043
49044 /* Step 4. Try to recycle a page. */
49045 if( pCache->bPurgeable
49046 && !pGroup->lru.pLruPrev->isAnchor
49047 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
49048 ){
49049 PCache1 *pOther;
49050 pPage = pGroup->lru.pLruPrev;
49051 assert( PAGE_IS_UNPINNED(pPage) );
49052 pcache1RemoveFromHash(pPage, 0);
49053 pcache1PinPage(pPage);
49054 pOther = pPage->pCache;
49055 if( pOther->szAlloc != pCache->szAlloc ){
49056 pcache1FreePage(pPage);
49057 pPage = 0;
49058 }else{
49059 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
49060 }
49061 }
49062
49063 /* Step 5. If a usable page buffer has still not been found,
49064 ** attempt to allocate a new one.
49065 */
49066 if( !pPage ){
49067 pPage = pcache1AllocPage(pCache, createFlag==1);
49068 }
49069
49070 if( pPage ){
49071 unsigned int h = iKey % pCache->nHash;
49072 pCache->nPage++;
49073 pPage->iKey = iKey;
49074 pPage->pNext = pCache->apHash[h];
49075 pPage->pCache = pCache;
49076 pPage->pLruPrev = 0;
49077 pPage->pLruNext = 0;
49078 *(void **)pPage->page.pExtra = 0;
49079 pCache->apHash[h] = pPage;
49080 if( iKey>pCache->iMaxKey ){
49081 pCache->iMaxKey = iKey;
49082 }
49083 }
49084 return pPage;
49085}
49086
49087/*
49088** Implementation of the sqlite3_pcache.xFetch method.
49089**
49090** Fetch a page by key value.
49091**
49092** Whether or not a new page may be allocated by this function depends on
49093** the value of the createFlag argument. 0 means do not allocate a new
49094** page. 1 means allocate a new page if space is easily available. 2
49095** means to try really hard to allocate a new page.
49096**
49097** For a non-purgeable cache (a cache used as the storage for an in-memory
49098** database) there is really no difference between createFlag 1 and 2. So
49099** the calling function (pcache.c) will never have a createFlag of 1 on
49100** a non-purgeable cache.
49101**
49102** There are three different approaches to obtaining space for a page,
49103** depending on the value of parameter createFlag (which may be 0, 1 or 2).
49104**
49105** 1. Regardless of the value of createFlag, the cache is searched for a
49106** copy of the requested page. If one is found, it is returned.
49107**
49108** 2. If createFlag==0 and the page is not already in the cache, NULL is
49109** returned.
49110**
49111** 3. If createFlag is 1, and the page is not already in the cache, then
49112** return NULL (do not allocate a new page) if any of the following
49113** conditions are true:
49114**
49115** (a) the number of pages pinned by the cache is greater than
49116** PCache1.nMax, or
49117**
49118** (b) the number of pages pinned by the cache is greater than
49119** the sum of nMax for all purgeable caches, less the sum of
49120** nMin for all other purgeable caches, or
49121**
49122** 4. If none of the first three conditions apply and the cache is marked
49123** as purgeable, and if one of the following is true:
49124**
49125** (a) The number of pages allocated for the cache is already
49126** PCache1.nMax, or
49127**
49128** (b) The number of pages allocated for all purgeable caches is
49129** already equal to or greater than the sum of nMax for all
49130** purgeable caches,
49131**
49132** (c) The system is under memory pressure and wants to avoid
49133** unnecessary pages cache entry allocations
49134**
49135** then attempt to recycle a page from the LRU list. If it is the right
49136** size, return the recycled buffer. Otherwise, free the buffer and
49137** proceed to step 5.
49138**
49139** 5. Otherwise, allocate and return a new page buffer.
49140**
49141** There are two versions of this routine. pcache1FetchWithMutex() is
49142** the general case. pcache1FetchNoMutex() is a faster implementation for
49143** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
49144** invokes the appropriate routine.
49145*/
49146static PgHdr1 *pcache1FetchNoMutex(
49147 sqlite3_pcache *p,
49148 unsigned int iKey,
49149 int createFlag
49150){
49151 PCache1 *pCache = (PCache1 *)p;
49152 PgHdr1 *pPage = 0;
49153
49154 /* Step 1: Search the hash table for an existing entry. */
49155 pPage = pCache->apHash[iKey % pCache->nHash];
49156 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
49157
49158 /* Step 2: If the page was found in the hash table, then return it.
49159 ** If the page was not in the hash table and createFlag is 0, abort.
49160 ** Otherwise (page not in hash and createFlag!=0) continue with
49161 ** subsequent steps to try to create the page. */
49162 if( pPage ){
49163 if( PAGE_IS_UNPINNED(pPage) ){
49164 return pcache1PinPage(pPage);
49165 }else{
49166 return pPage;
49167 }
49168 }else if( createFlag ){
49169 /* Steps 3, 4, and 5 implemented by this subroutine */
49170 return pcache1FetchStage2(pCache, iKey, createFlag);
49171 }else{
49172 return 0;
49173 }
49174}
49175#if PCACHE1_MIGHT_USE_GROUP_MUTEX
49176static PgHdr1 *pcache1FetchWithMutex(
49177 sqlite3_pcache *p,
49178 unsigned int iKey,
49179 int createFlag
49180){
49181 PCache1 *pCache = (PCache1 *)p;
49182 PgHdr1 *pPage;
49183
49184 pcache1EnterMutex(pCache->pGroup);
49185 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
49186 assert( pPage==0 || pCache->iMaxKey>=iKey );
49187 pcache1LeaveMutex(pCache->pGroup);
49188 return pPage;
49189}
49190#endif
49191static sqlite3_pcache_page *pcache1Fetch(
49192 sqlite3_pcache *p,
49193 unsigned int iKey,
49194 int createFlag
49195){
49196#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
49197 PCache1 *pCache = (PCache1 *)p;
49198#endif
49199
49200 assert( offsetof(PgHdr1,page)==0 );
49201 assert( pCache->bPurgeable || createFlag!=1 );
49202 assert( pCache->bPurgeable || pCache->nMin==0 );
49203 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
49204 assert( pCache->nMin==0 || pCache->bPurgeable );
49205 assert( pCache->nHash>0 );
49206#if PCACHE1_MIGHT_USE_GROUP_MUTEX
49207 if( pCache->pGroup->mutex ){
49208 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
49209 }else
49210#endif
49211 {
49212 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
49213 }
49214}
49215
49216
49217/*
49218** Implementation of the sqlite3_pcache.xUnpin method.
49219**
49220** Mark a page as unpinned (eligible for asynchronous recycling).
49221*/
49222static void pcache1Unpin(
49223 sqlite3_pcache *p,
49224 sqlite3_pcache_page *pPg,
49225 int reuseUnlikely
49226){
49227 PCache1 *pCache = (PCache1 *)p;
49228 PgHdr1 *pPage = (PgHdr1 *)pPg;
49229 PGroup *pGroup = pCache->pGroup;
49230
49231 assert( pPage->pCache==pCache );
49232 pcache1EnterMutex(pGroup);
49233
49234 /* It is an error to call this function if the page is already
49235 ** part of the PGroup LRU list.
49236 */
49237 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
49238 assert( PAGE_IS_PINNED(pPage) );
49239
49240 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
49241 pcache1RemoveFromHash(pPage, 1);
49242 }else{
49243 /* Add the page to the PGroup LRU list. */
49244 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
49245 pPage->pLruPrev = &pGroup->lru;
49246 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
49247 *ppFirst = pPage;
49248 pCache->nRecyclable++;
49249 }
49250
49251 pcache1LeaveMutex(pCache->pGroup);
49252}
49253
49254/*
49255** Implementation of the sqlite3_pcache.xRekey method.
49256*/
49257static void pcache1Rekey(
49258 sqlite3_pcache *p,
49259 sqlite3_pcache_page *pPg,
49260 unsigned int iOld,
49261 unsigned int iNew
49262){
49263 PCache1 *pCache = (PCache1 *)p;
49264 PgHdr1 *pPage = (PgHdr1 *)pPg;
49265 PgHdr1 **pp;
49266 unsigned int h;
49267 assert( pPage->iKey==iOld );
49268 assert( pPage->pCache==pCache );
49269
49270 pcache1EnterMutex(pCache->pGroup);
49271
49272 h = iOld%pCache->nHash;
49273 pp = &pCache->apHash[h];
49274 while( (*pp)!=pPage ){
49275 pp = &(*pp)->pNext;
49276 }
49277 *pp = pPage->pNext;
49278
49279 h = iNew%pCache->nHash;
49280 pPage->iKey = iNew;
49281 pPage->pNext = pCache->apHash[h];
49282 pCache->apHash[h] = pPage;
49283 if( iNew>pCache->iMaxKey ){
49284 pCache->iMaxKey = iNew;
49285 }
49286
49287 pcache1LeaveMutex(pCache->pGroup);
49288}
49289
49290/*
49291** Implementation of the sqlite3_pcache.xTruncate method.
49292**
49293** Discard all unpinned pages in the cache with a page number equal to
49294** or greater than parameter iLimit. Any pinned pages with a page number
49295** equal to or greater than iLimit are implicitly unpinned.
49296*/
49297static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
49298 PCache1 *pCache = (PCache1 *)p;
49299 pcache1EnterMutex(pCache->pGroup);
49300 if( iLimit<=pCache->iMaxKey ){
49301 pcache1TruncateUnsafe(pCache, iLimit);
49302 pCache->iMaxKey = iLimit-1;
49303 }
49304 pcache1LeaveMutex(pCache->pGroup);
49305}
49306
49307/*
49308** Implementation of the sqlite3_pcache.xDestroy method.
49309**
49310** Destroy a cache allocated using pcache1Create().
49311*/
49312static void pcache1Destroy(sqlite3_pcache *p){
49313 PCache1 *pCache = (PCache1 *)p;
49314 PGroup *pGroup = pCache->pGroup;
49315 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
49316 pcache1EnterMutex(pGroup);
49317 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
49318 assert( pGroup->nMaxPage >= pCache->nMax );
49319 pGroup->nMaxPage -= pCache->nMax;
49320 assert( pGroup->nMinPage >= pCache->nMin );
49321 pGroup->nMinPage -= pCache->nMin;
49322 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
49323 pcache1EnforceMaxPage(pCache);
49324 pcache1LeaveMutex(pGroup);
49325 sqlite3_free(pCache->pBulk);
49326 sqlite3_free(pCache->apHash);
49327 sqlite3_free(pCache);
49328}
49329
49330/*
49331** This function is called during initialization (sqlite3_initialize()) to
49332** install the default pluggable cache module, assuming the user has not
49333** already provided an alternative.
49334*/
49335SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
49336 static const sqlite3_pcache_methods2 defaultMethods = {
49337 1, /* iVersion */
49338 0, /* pArg */
49339 pcache1Init, /* xInit */
49340 pcache1Shutdown, /* xShutdown */
49341 pcache1Create, /* xCreate */
49342 pcache1Cachesize, /* xCachesize */
49343 pcache1Pagecount, /* xPagecount */
49344 pcache1Fetch, /* xFetch */
49345 pcache1Unpin, /* xUnpin */
49346 pcache1Rekey, /* xRekey */
49347 pcache1Truncate, /* xTruncate */
49348 pcache1Destroy, /* xDestroy */
49349 pcache1Shrink /* xShrink */
49350 };
49351 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
49352}
49353
49354/*
49355** Return the size of the header on each page of this PCACHE implementation.
49356*/
49357SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1)); }
49358
49359/*
49360** Return the global mutex used by this PCACHE implementation. The
49361** sqlite3_status() routine needs access to this mutex.
49362*/
49363SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void){
49364 return pcache1.mutex;
49365}
49366
49367#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
49368/*
49369** This function is called to free superfluous dynamically allocated memory
49370** held by the pager system. Memory in use by any SQLite pager allocated
49371** by the current thread may be sqlite3_free()ed.
49372**
49373** nReq is the number of bytes of memory required. Once this much has
49374** been released, the function returns. The return value is the total number
49375** of bytes of memory released.
49376*/
49377SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
49378 int nFree = 0;
49379 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
49380 assert( sqlite3_mutex_notheld(pcache1.mutex) );
49381 if( sqlite3GlobalConfig.pPage==0 ){
49382 PgHdr1 *p;
49383 pcache1EnterMutex(&pcache1.grp);
49384 while( (nReq<0 || nFree<nReq)
49385 && (p=pcache1.grp.lru.pLruPrev)!=0
49386 && p->isAnchor==0
49387 ){
49388 nFree += pcache1MemSize(p->page.pBuf);
49389#ifdef SQLITE_PCACHE_SEPARATE_HEADER
49390 nFree += sqlite3MemSize(p);
49391#endif
49392 assert( PAGE_IS_UNPINNED(p) );
49393 pcache1PinPage(p);
49394 pcache1RemoveFromHash(p, 1);
49395 }
49396 pcache1LeaveMutex(&pcache1.grp);
49397 }
49398 return nFree;
49399}
49400#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
49401
49402#ifdef SQLITE_TEST
49403/*
49404** This function is used by test procedures to inspect the internal state
49405** of the global cache.
49406*/
49407SQLITE_PRIVATE void sqlite3PcacheStats(
49408 int *pnCurrent, /* OUT: Total number of pages cached */
49409 int *pnMax, /* OUT: Global maximum cache size */
49410 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
49411 int *pnRecyclable /* OUT: Total number of pages available for recycling */
49412){
49413 PgHdr1 *p;
49414 int nRecyclable = 0;
49415 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
49416 assert( PAGE_IS_UNPINNED(p) );
49417 nRecyclable++;
49418 }
49419 *pnCurrent = pcache1.grp.nPurgeable;
49420 *pnMax = (int)pcache1.grp.nMaxPage;
49421 *pnMin = (int)pcache1.grp.nMinPage;
49422 *pnRecyclable = nRecyclable;
49423}
49424#endif
49425
49426/************** End of pcache1.c *********************************************/
49427/************** Begin file rowset.c ******************************************/
49428/*
49429** 2008 December 3
49430**
49431** The author disclaims copyright to this source code. In place of
49432** a legal notice, here is a blessing:
49433**
49434** May you do good and not evil.
49435** May you find forgiveness for yourself and forgive others.
49436** May you share freely, never taking more than you give.
49437**
49438*************************************************************************
49439**
49440** This module implements an object we call a "RowSet".
49441**
49442** The RowSet object is a collection of rowids. Rowids
49443** are inserted into the RowSet in an arbitrary order. Inserts
49444** can be intermixed with tests to see if a given rowid has been
49445** previously inserted into the RowSet.
49446**
49447** After all inserts are finished, it is possible to extract the
49448** elements of the RowSet in sorted order. Once this extraction
49449** process has started, no new elements may be inserted.
49450**
49451** Hence, the primitive operations for a RowSet are:
49452**
49453** CREATE
49454** INSERT
49455** TEST
49456** SMALLEST
49457** DESTROY
49458**
49459** The CREATE and DESTROY primitives are the constructor and destructor,
49460** obviously. The INSERT primitive adds a new element to the RowSet.
49461** TEST checks to see if an element is already in the RowSet. SMALLEST
49462** extracts the least value from the RowSet.
49463**
49464** The INSERT primitive might allocate additional memory. Memory is
49465** allocated in chunks so most INSERTs do no allocation. There is an
49466** upper bound on the size of allocated memory. No memory is freed
49467** until DESTROY.
49468**
49469** The TEST primitive includes a "batch" number. The TEST primitive
49470** will only see elements that were inserted before the last change
49471** in the batch number. In other words, if an INSERT occurs between
49472** two TESTs where the TESTs have the same batch nubmer, then the
49473** value added by the INSERT will not be visible to the second TEST.
49474** The initial batch number is zero, so if the very first TEST contains
49475** a non-zero batch number, it will see all prior INSERTs.
49476**
49477** No INSERTs may occurs after a SMALLEST. An assertion will fail if
49478** that is attempted.
49479**
49480** The cost of an INSERT is roughly constant. (Sometimes new memory
49481** has to be allocated on an INSERT.) The cost of a TEST with a new
49482** batch number is O(NlogN) where N is the number of elements in the RowSet.
49483** The cost of a TEST using the same batch number is O(logN). The cost
49484** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST
49485** primitives are constant time. The cost of DESTROY is O(N).
49486**
49487** TEST and SMALLEST may not be used by the same RowSet. This used to
49488** be possible, but the feature was not used, so it was removed in order
49489** to simplify the code.
49490*/
49491/* #include "sqliteInt.h" */
49492
49493
49494/*
49495** Target size for allocation chunks.
49496*/
49497#define ROWSET_ALLOCATION_SIZE 1024
49498
49499/*
49500** The number of rowset entries per allocation chunk.
49501*/
49502#define ROWSET_ENTRY_PER_CHUNK \
49503 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
49504
49505/*
49506** Each entry in a RowSet is an instance of the following object.
49507**
49508** This same object is reused to store a linked list of trees of RowSetEntry
49509** objects. In that alternative use, pRight points to the next entry
49510** in the list, pLeft points to the tree, and v is unused. The
49511** RowSet.pForest value points to the head of this forest list.
49512*/
49513struct RowSetEntry {
49514 i64 v; /* ROWID value for this entry */
49515 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
49516 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
49517};
49518
49519/*
49520** RowSetEntry objects are allocated in large chunks (instances of the
49521** following structure) to reduce memory allocation overhead. The
49522** chunks are kept on a linked list so that they can be deallocated
49523** when the RowSet is destroyed.
49524*/
49525struct RowSetChunk {
49526 struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */
49527 struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
49528};
49529
49530/*
49531** A RowSet in an instance of the following structure.
49532**
49533** A typedef of this structure if found in sqliteInt.h.
49534*/
49535struct RowSet {
49536 struct RowSetChunk *pChunk; /* List of all chunk allocations */
49537 sqlite3 *db; /* The database connection */
49538 struct RowSetEntry *pEntry; /* List of entries using pRight */
49539 struct RowSetEntry *pLast; /* Last entry on the pEntry list */
49540 struct RowSetEntry *pFresh; /* Source of new entry objects */
49541 struct RowSetEntry *pForest; /* List of binary trees of entries */
49542 u16 nFresh; /* Number of objects on pFresh */
49543 u16 rsFlags; /* Various flags */
49544 int iBatch; /* Current insert batch */
49545};
49546
49547/*
49548** Allowed values for RowSet.rsFlags
49549*/
49550#define ROWSET_SORTED 0x01 /* True if RowSet.pEntry is sorted */
49551#define ROWSET_NEXT 0x02 /* True if sqlite3RowSetNext() has been called */
49552
49553/*
49554** Allocate a RowSet object. Return NULL if a memory allocation
49555** error occurs.
49556*/
49557SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db){
49558 RowSet *p = sqlite3DbMallocRawNN(db, sizeof(*p));
49559 if( p ){
49560 int N = sqlite3DbMallocSize(db, p);
49561 p->pChunk = 0;
49562 p->db = db;
49563 p->pEntry = 0;
49564 p->pLast = 0;
49565 p->pForest = 0;
49566 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
49567 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
49568 p->rsFlags = ROWSET_SORTED;
49569 p->iBatch = 0;
49570 }
49571 return p;
49572}
49573
49574/*
49575** Deallocate all chunks from a RowSet. This frees all memory that
49576** the RowSet has allocated over its lifetime. This routine is
49577** the destructor for the RowSet.
49578*/
49579SQLITE_PRIVATE void sqlite3RowSetClear(void *pArg){
49580 RowSet *p = (RowSet*)pArg;
49581 struct RowSetChunk *pChunk, *pNextChunk;
49582 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
49583 pNextChunk = pChunk->pNextChunk;
49584 sqlite3DbFree(p->db, pChunk);
49585 }
49586 p->pChunk = 0;
49587 p->nFresh = 0;
49588 p->pEntry = 0;
49589 p->pLast = 0;
49590 p->pForest = 0;
49591 p->rsFlags = ROWSET_SORTED;
49592}
49593
49594/*
49595** Deallocate all chunks from a RowSet. This frees all memory that
49596** the RowSet has allocated over its lifetime. This routine is
49597** the destructor for the RowSet.
49598*/
49599SQLITE_PRIVATE void sqlite3RowSetDelete(void *pArg){
49600 sqlite3RowSetClear(pArg);
49601 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
49602}
49603
49604/*
49605** Allocate a new RowSetEntry object that is associated with the
49606** given RowSet. Return a pointer to the new and completely uninitialized
49607** objected.
49608**
49609** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
49610** routine returns NULL.
49611*/
49612static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
49613 assert( p!=0 );
49614 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
49615 /* We could allocate a fresh RowSetEntry each time one is needed, but it
49616 ** is more efficient to pull a preallocated entry from the pool */
49617 struct RowSetChunk *pNew;
49618 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
49619 if( pNew==0 ){
49620 return 0;
49621 }
49622 pNew->pNextChunk = p->pChunk;
49623 p->pChunk = pNew;
49624 p->pFresh = pNew->aEntry;
49625 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
49626 }
49627 p->nFresh--;
49628 return p->pFresh++;
49629}
49630
49631/*
49632** Insert a new value into a RowSet.
49633**
49634** The mallocFailed flag of the database connection is set if a
49635** memory allocation fails.
49636*/
49637SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
49638 struct RowSetEntry *pEntry; /* The new entry */
49639 struct RowSetEntry *pLast; /* The last prior entry */
49640
49641 /* This routine is never called after sqlite3RowSetNext() */
49642 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
49643
49644 pEntry = rowSetEntryAlloc(p);
49645 if( pEntry==0 ) return;
49646 pEntry->v = rowid;
49647 pEntry->pRight = 0;
49648 pLast = p->pLast;
49649 if( pLast ){
49650 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
49651 /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
49652 ** where possible */
49653 p->rsFlags &= ~ROWSET_SORTED;
49654 }
49655 pLast->pRight = pEntry;
49656 }else{
49657 p->pEntry = pEntry;
49658 }
49659 p->pLast = pEntry;
49660}
49661
49662/*
49663** Merge two lists of RowSetEntry objects. Remove duplicates.
49664**
49665** The input lists are connected via pRight pointers and are
49666** assumed to each already be in sorted order.
49667*/
49668static struct RowSetEntry *rowSetEntryMerge(
49669 struct RowSetEntry *pA, /* First sorted list to be merged */
49670 struct RowSetEntry *pB /* Second sorted list to be merged */
49671){
49672 struct RowSetEntry head;
49673 struct RowSetEntry *pTail;
49674
49675 pTail = &head;
49676 assert( pA!=0 && pB!=0 );
49677 for(;;){
49678 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
49679 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
49680 if( pA->v<=pB->v ){
49681 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
49682 pA = pA->pRight;
49683 if( pA==0 ){
49684 pTail->pRight = pB;
49685 break;
49686 }
49687 }else{
49688 pTail = pTail->pRight = pB;
49689 pB = pB->pRight;
49690 if( pB==0 ){
49691 pTail->pRight = pA;
49692 break;
49693 }
49694 }
49695 }
49696 return head.pRight;
49697}
49698
49699/*
49700** Sort all elements on the list of RowSetEntry objects into order of
49701** increasing v.
49702*/
49703static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
49704 unsigned int i;
49705 struct RowSetEntry *pNext, *aBucket[40];
49706
49707 memset(aBucket, 0, sizeof(aBucket));
49708 while( pIn ){
49709 pNext = pIn->pRight;
49710 pIn->pRight = 0;
49711 for(i=0; aBucket[i]; i++){
49712 pIn = rowSetEntryMerge(aBucket[i], pIn);
49713 aBucket[i] = 0;
49714 }
49715 aBucket[i] = pIn;
49716 pIn = pNext;
49717 }
49718 pIn = aBucket[0];
49719 for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
49720 if( aBucket[i]==0 ) continue;
49721 pIn = pIn ? rowSetEntryMerge(pIn, aBucket[i]) : aBucket[i];
49722 }
49723 return pIn;
49724}
49725
49726
49727/*
49728** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
49729** Convert this tree into a linked list connected by the pRight pointers
49730** and return pointers to the first and last elements of the new list.
49731*/
49732static void rowSetTreeToList(
49733 struct RowSetEntry *pIn, /* Root of the input tree */
49734 struct RowSetEntry **ppFirst, /* Write head of the output list here */
49735 struct RowSetEntry **ppLast /* Write tail of the output list here */
49736){
49737 assert( pIn!=0 );
49738 if( pIn->pLeft ){
49739 struct RowSetEntry *p;
49740 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
49741 p->pRight = pIn;
49742 }else{
49743 *ppFirst = pIn;
49744 }
49745 if( pIn->pRight ){
49746 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
49747 }else{
49748 *ppLast = pIn;
49749 }
49750 assert( (*ppLast)->pRight==0 );
49751}
49752
49753
49754/*
49755** Convert a sorted list of elements (connected by pRight) into a binary
49756** tree with depth of iDepth. A depth of 1 means the tree contains a single
49757** node taken from the head of *ppList. A depth of 2 means a tree with
49758** three nodes. And so forth.
49759**
49760** Use as many entries from the input list as required and update the
49761** *ppList to point to the unused elements of the list. If the input
49762** list contains too few elements, then construct an incomplete tree
49763** and leave *ppList set to NULL.
49764**
49765** Return a pointer to the root of the constructed binary tree.
49766*/
49767static struct RowSetEntry *rowSetNDeepTree(
49768 struct RowSetEntry **ppList,
49769 int iDepth
49770){
49771 struct RowSetEntry *p; /* Root of the new tree */
49772 struct RowSetEntry *pLeft; /* Left subtree */
49773 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
49774 /* Prevent unnecessary deep recursion when we run out of entries */
49775 return 0;
49776 }
49777 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
49778 /* This branch causes a *balanced* tree to be generated. A valid tree
49779 ** is still generated without this branch, but the tree is wildly
49780 ** unbalanced and inefficient. */
49781 pLeft = rowSetNDeepTree(ppList, iDepth-1);
49782 p = *ppList;
49783 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
49784 /* It is safe to always return here, but the resulting tree
49785 ** would be unbalanced */
49786 return pLeft;
49787 }
49788 p->pLeft = pLeft;
49789 *ppList = p->pRight;
49790 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
49791 }else{
49792 p = *ppList;
49793 *ppList = p->pRight;
49794 p->pLeft = p->pRight = 0;
49795 }
49796 return p;
49797}
49798
49799/*
49800** Convert a sorted list of elements into a binary tree. Make the tree
49801** as deep as it needs to be in order to contain the entire list.
49802*/
49803static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
49804 int iDepth; /* Depth of the tree so far */
49805 struct RowSetEntry *p; /* Current tree root */
49806 struct RowSetEntry *pLeft; /* Left subtree */
49807
49808 assert( pList!=0 );
49809 p = pList;
49810 pList = p->pRight;
49811 p->pLeft = p->pRight = 0;
49812 for(iDepth=1; pList; iDepth++){
49813 pLeft = p;
49814 p = pList;
49815 pList = p->pRight;
49816 p->pLeft = pLeft;
49817 p->pRight = rowSetNDeepTree(&pList, iDepth);
49818 }
49819 return p;
49820}
49821
49822/*
49823** Extract the smallest element from the RowSet.
49824** Write the element into *pRowid. Return 1 on success. Return
49825** 0 if the RowSet is already empty.
49826**
49827** After this routine has been called, the sqlite3RowSetInsert()
49828** routine may not be called again.
49829**
49830** This routine may not be called after sqlite3RowSetTest() has
49831** been used. Older versions of RowSet allowed that, but as the
49832** capability was not used by the code generator, it was removed
49833** for code economy.
49834*/
49835SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
49836 assert( p!=0 );
49837 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
49838
49839 /* Merge the forest into a single sorted list on first call */
49840 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
49841 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
49842 p->pEntry = rowSetEntrySort(p->pEntry);
49843 }
49844 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
49845 }
49846
49847 /* Return the next entry on the list */
49848 if( p->pEntry ){
49849 *pRowid = p->pEntry->v;
49850 p->pEntry = p->pEntry->pRight;
49851 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
49852 /* Free memory immediately, rather than waiting on sqlite3_finalize() */
49853 sqlite3RowSetClear(p);
49854 }
49855 return 1;
49856 }else{
49857 return 0;
49858 }
49859}
49860
49861/*
49862** Check to see if element iRowid was inserted into the rowset as
49863** part of any insert batch prior to iBatch. Return 1 or 0.
49864**
49865** If this is the first test of a new batch and if there exist entries
49866** on pRowSet->pEntry, then sort those entries into the forest at
49867** pRowSet->pForest so that they can be tested.
49868*/
49869SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
49870 struct RowSetEntry *p, *pTree;
49871
49872 /* This routine is never called after sqlite3RowSetNext() */
49873 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
49874
49875 /* Sort entries into the forest on the first test of a new batch.
49876 ** To save unnecessary work, only do this when the batch number changes.
49877 */
49878 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
49879 p = pRowSet->pEntry;
49880 if( p ){
49881 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
49882 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
49883 /* Only sort the current set of entiries if they need it */
49884 p = rowSetEntrySort(p);
49885 }
49886 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
49887 ppPrevTree = &pTree->pRight;
49888 if( pTree->pLeft==0 ){
49889 pTree->pLeft = rowSetListToTree(p);
49890 break;
49891 }else{
49892 struct RowSetEntry *pAux, *pTail;
49893 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
49894 pTree->pLeft = 0;
49895 p = rowSetEntryMerge(pAux, p);
49896 }
49897 }
49898 if( pTree==0 ){
49899 *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
49900 if( pTree ){
49901 pTree->v = 0;
49902 pTree->pRight = 0;
49903 pTree->pLeft = rowSetListToTree(p);
49904 }
49905 }
49906 pRowSet->pEntry = 0;
49907 pRowSet->pLast = 0;
49908 pRowSet->rsFlags |= ROWSET_SORTED;
49909 }
49910 pRowSet->iBatch = iBatch;
49911 }
49912
49913 /* Test to see if the iRowid value appears anywhere in the forest.
49914 ** Return 1 if it does and 0 if not.
49915 */
49916 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
49917 p = pTree->pLeft;
49918 while( p ){
49919 if( p->v<iRowid ){
49920 p = p->pRight;
49921 }else if( p->v>iRowid ){
49922 p = p->pLeft;
49923 }else{
49924 return 1;
49925 }
49926 }
49927 }
49928 return 0;
49929}
49930
49931/************** End of rowset.c **********************************************/
49932/************** Begin file pager.c *******************************************/
49933/*
49934** 2001 September 15
49935**
49936** The author disclaims copyright to this source code. In place of
49937** a legal notice, here is a blessing:
49938**
49939** May you do good and not evil.
49940** May you find forgiveness for yourself and forgive others.
49941** May you share freely, never taking more than you give.
49942**
49943*************************************************************************
49944** This is the implementation of the page cache subsystem or "pager".
49945**
49946** The pager is used to access a database disk file. It implements
49947** atomic commit and rollback through the use of a journal file that
49948** is separate from the database file. The pager also implements file
49949** locking to prevent two processes from writing the same database
49950** file simultaneously, or one process from reading the database while
49951** another is writing.
49952*/
49953#ifndef SQLITE_OMIT_DISKIO
49954/* #include "sqliteInt.h" */
49955/************** Include wal.h in the middle of pager.c ***********************/
49956/************** Begin file wal.h *********************************************/
49957/*
49958** 2010 February 1
49959**
49960** The author disclaims copyright to this source code. In place of
49961** a legal notice, here is a blessing:
49962**
49963** May you do good and not evil.
49964** May you find forgiveness for yourself and forgive others.
49965** May you share freely, never taking more than you give.
49966**
49967*************************************************************************
49968** This header file defines the interface to the write-ahead logging
49969** system. Refer to the comments below and the header comment attached to
49970** the implementation of each function in log.c for further details.
49971*/
49972
49973#ifndef SQLITE_WAL_H
49974#define SQLITE_WAL_H
49975
49976/* #include "sqliteInt.h" */
49977
49978/* Macros for extracting appropriate sync flags for either transaction
49979** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
49980*/
49981#define WAL_SYNC_FLAGS(X) ((X)&0x03)
49982#define CKPT_SYNC_FLAGS(X) (((X)>>2)&0x03)
49983
49984#ifdef SQLITE_OMIT_WAL
49985# define sqlite3WalOpen(x,y,z) 0
49986# define sqlite3WalLimit(x,y)
49987# define sqlite3WalClose(v,w,x,y,z) 0
49988# define sqlite3WalBeginReadTransaction(y,z) 0
49989# define sqlite3WalEndReadTransaction(z)
49990# define sqlite3WalDbsize(y) 0
49991# define sqlite3WalBeginWriteTransaction(y) 0
49992# define sqlite3WalEndWriteTransaction(x) 0
49993# define sqlite3WalUndo(x,y,z) 0
49994# define sqlite3WalSavepoint(y,z)
49995# define sqlite3WalSavepointUndo(y,z) 0
49996# define sqlite3WalFrames(u,v,w,x,y,z) 0
49997# define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0
49998# define sqlite3WalCallback(z) 0
49999# define sqlite3WalExclusiveMode(y,z) 0
50000# define sqlite3WalHeapMemory(z) 0
50001# define sqlite3WalFramesize(z) 0
50002# define sqlite3WalFindFrame(x,y,z) 0
50003# define sqlite3WalFile(x) 0
50004#else
50005
50006#define WAL_SAVEPOINT_NDATA 4
50007
50008/* Connection to a write-ahead log (WAL) file.
50009** There is one object of this type for each pager.
50010*/
50011typedef struct Wal Wal;
50012
50013/* Open and close a connection to a write-ahead log. */
50014SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
50015SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *);
50016
50017/* Set the limiting size of a WAL file. */
50018SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
50019
50020/* Used by readers to open (lock) and close (unlock) a snapshot. A
50021** snapshot is like a read-transaction. It is the state of the database
50022** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and
50023** preserves the current state even if the other threads or processes
50024** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the
50025** transaction and releases the lock.
50026*/
50027SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
50028SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
50029
50030/* Read a page from the write-ahead log, if it is present. */
50031SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
50032SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
50033
50034/* If the WAL is not empty, return the size of the database. */
50035SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
50036
50037/* Obtain or release the WRITER lock. */
50038SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
50039SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
50040
50041/* Undo any frames written (but not committed) to the log */
50042SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
50043
50044/* Return an integer that records the current (uncommitted) write
50045** position in the WAL */
50046SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
50047
50048/* Move the write position of the WAL back to iFrame. Called in
50049** response to a ROLLBACK TO command. */
50050SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
50051
50052/* Write a frame or frames to the log. */
50053SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
50054
50055/* Copy pages from the log to the database file */
50056SQLITE_PRIVATE int sqlite3WalCheckpoint(
50057 Wal *pWal, /* Write-ahead log connection */
50058 sqlite3 *db, /* Check this handle's interrupt flag */
50059 int eMode, /* One of PASSIVE, FULL and RESTART */
50060 int (*xBusy)(void*), /* Function to call when busy */
50061 void *pBusyArg, /* Context argument for xBusyHandler */
50062 int sync_flags, /* Flags to sync db file with (or 0) */
50063 int nBuf, /* Size of buffer nBuf */
50064 u8 *zBuf, /* Temporary buffer to use */
50065 int *pnLog, /* OUT: Number of frames in WAL */
50066 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
50067);
50068
50069/* Return the value to pass to a sqlite3_wal_hook callback, the
50070** number of frames in the WAL at the point of the last commit since
50071** sqlite3WalCallback() was called. If no commits have occurred since
50072** the last call, then return 0.
50073*/
50074SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
50075
50076/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
50077** by the pager layer on the database file.
50078*/
50079SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
50080
50081/* Return true if the argument is non-NULL and the WAL module is using
50082** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
50083** WAL module is using shared-memory, return false.
50084*/
50085SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
50086
50087#ifdef SQLITE_ENABLE_SNAPSHOT
50088SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
50089SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
50090SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal);
50091SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot);
50092SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal);
50093#endif
50094
50095#ifdef SQLITE_ENABLE_ZIPVFS
50096/* If the WAL file is not empty, return the number of bytes of content
50097** stored in each frame (i.e. the db page-size when the WAL was created).
50098*/
50099SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
50100#endif
50101
50102/* Return the sqlite3_file object for the WAL file */
50103SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal);
50104
50105#endif /* ifndef SQLITE_OMIT_WAL */
50106#endif /* SQLITE_WAL_H */
50107
50108/************** End of wal.h *************************************************/
50109/************** Continuing where we left off in pager.c **********************/
50110
50111
50112/******************* NOTES ON THE DESIGN OF THE PAGER ************************
50113**
50114** This comment block describes invariants that hold when using a rollback
50115** journal. These invariants do not apply for journal_mode=WAL,
50116** journal_mode=MEMORY, or journal_mode=OFF.
50117**
50118** Within this comment block, a page is deemed to have been synced
50119** automatically as soon as it is written when PRAGMA synchronous=OFF.
50120** Otherwise, the page is not synced until the xSync method of the VFS
50121** is called successfully on the file containing the page.
50122**
50123** Definition: A page of the database file is said to be "overwriteable" if
50124** one or more of the following are true about the page:
50125**
50126** (a) The original content of the page as it was at the beginning of
50127** the transaction has been written into the rollback journal and
50128** synced.
50129**
50130** (b) The page was a freelist leaf page at the start of the transaction.
50131**
50132** (c) The page number is greater than the largest page that existed in
50133** the database file at the start of the transaction.
50134**
50135** (1) A page of the database file is never overwritten unless one of the
50136** following are true:
50137**
50138** (a) The page and all other pages on the same sector are overwriteable.
50139**
50140** (b) The atomic page write optimization is enabled, and the entire
50141** transaction other than the update of the transaction sequence
50142** number consists of a single page change.
50143**
50144** (2) The content of a page written into the rollback journal exactly matches
50145** both the content in the database when the rollback journal was written
50146** and the content in the database at the beginning of the current
50147** transaction.
50148**
50149** (3) Writes to the database file are an integer multiple of the page size
50150** in length and are aligned on a page boundary.
50151**
50152** (4) Reads from the database file are either aligned on a page boundary and
50153** an integer multiple of the page size in length or are taken from the
50154** first 100 bytes of the database file.
50155**
50156** (5) All writes to the database file are synced prior to the rollback journal
50157** being deleted, truncated, or zeroed.
50158**
50159** (6) If a master journal file is used, then all writes to the database file
50160** are synced prior to the master journal being deleted.
50161**
50162** Definition: Two databases (or the same database at two points it time)
50163** are said to be "logically equivalent" if they give the same answer to
50164** all queries. Note in particular the content of freelist leaf
50165** pages can be changed arbitrarily without affecting the logical equivalence
50166** of the database.
50167**
50168** (7) At any time, if any subset, including the empty set and the total set,
50169** of the unsynced changes to a rollback journal are removed and the
50170** journal is rolled back, the resulting database file will be logically
50171** equivalent to the database file at the beginning of the transaction.
50172**
50173** (8) When a transaction is rolled back, the xTruncate method of the VFS
50174** is called to restore the database file to the same size it was at
50175** the beginning of the transaction. (In some VFSes, the xTruncate
50176** method is a no-op, but that does not change the fact the SQLite will
50177** invoke it.)
50178**
50179** (9) Whenever the database file is modified, at least one bit in the range
50180** of bytes from 24 through 39 inclusive will be changed prior to releasing
50181** the EXCLUSIVE lock, thus signaling other connections on the same
50182** database to flush their caches.
50183**
50184** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
50185** than one billion transactions.
50186**
50187** (11) A database file is well-formed at the beginning and at the conclusion
50188** of every transaction.
50189**
50190** (12) An EXCLUSIVE lock is held on the database file when writing to
50191** the database file.
50192**
50193** (13) A SHARED lock is held on the database file while reading any
50194** content out of the database file.
50195**
50196******************************************************************************/
50197
50198/*
50199** Macros for troubleshooting. Normally turned off
50200*/
50201#if 0
50202int sqlite3PagerTrace=1; /* True to enable tracing */
50203#define sqlite3DebugPrintf printf
50204#define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
50205#else
50206#define PAGERTRACE(X)
50207#endif
50208
50209/*
50210** The following two macros are used within the PAGERTRACE() macros above
50211** to print out file-descriptors.
50212**
50213** PAGERID() takes a pointer to a Pager struct as its argument. The
50214** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
50215** struct as its argument.
50216*/
50217#define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
50218#define FILEHANDLEID(fd) (SQLITE_PTR_TO_INT(fd))
50219
50220/*
50221** The Pager.eState variable stores the current 'state' of a pager. A
50222** pager may be in any one of the seven states shown in the following
50223** state diagram.
50224**
50225** OPEN <------+------+
50226** | | |
50227** V | |
50228** +---------> READER-------+ |
50229** | | |
50230** | V |
50231** |<-------WRITER_LOCKED------> ERROR
50232** | | ^
50233** | V |
50234** |<------WRITER_CACHEMOD-------->|
50235** | | |
50236** | V |
50237** |<-------WRITER_DBMOD---------->|
50238** | | |
50239** | V |
50240** +<------WRITER_FINISHED-------->+
50241**
50242**
50243** List of state transitions and the C [function] that performs each:
50244**
50245** OPEN -> READER [sqlite3PagerSharedLock]
50246** READER -> OPEN [pager_unlock]
50247**
50248** READER -> WRITER_LOCKED [sqlite3PagerBegin]
50249** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
50250** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
50251** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
50252** WRITER_*** -> READER [pager_end_transaction]
50253**
50254** WRITER_*** -> ERROR [pager_error]
50255** ERROR -> OPEN [pager_unlock]
50256**
50257**
50258** OPEN:
50259**
50260** The pager starts up in this state. Nothing is guaranteed in this
50261** state - the file may or may not be locked and the database size is
50262** unknown. The database may not be read or written.
50263**
50264** * No read or write transaction is active.
50265** * Any lock, or no lock at all, may be held on the database file.
50266** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
50267**
50268** READER:
50269**
50270** In this state all the requirements for reading the database in
50271** rollback (non-WAL) mode are met. Unless the pager is (or recently
50272** was) in exclusive-locking mode, a user-level read transaction is
50273** open. The database size is known in this state.
50274**
50275** A connection running with locking_mode=normal enters this state when
50276** it opens a read-transaction on the database and returns to state
50277** OPEN after the read-transaction is completed. However a connection
50278** running in locking_mode=exclusive (including temp databases) remains in
50279** this state even after the read-transaction is closed. The only way
50280** a locking_mode=exclusive connection can transition from READER to OPEN
50281** is via the ERROR state (see below).
50282**
50283** * A read transaction may be active (but a write-transaction cannot).
50284** * A SHARED or greater lock is held on the database file.
50285** * The dbSize variable may be trusted (even if a user-level read
50286** transaction is not active). The dbOrigSize and dbFileSize variables
50287** may not be trusted at this point.
50288** * If the database is a WAL database, then the WAL connection is open.
50289** * Even if a read-transaction is not open, it is guaranteed that
50290** there is no hot-journal in the file-system.
50291**
50292** WRITER_LOCKED:
50293**
50294** The pager moves to this state from READER when a write-transaction
50295** is first opened on the database. In WRITER_LOCKED state, all locks
50296** required to start a write-transaction are held, but no actual
50297** modifications to the cache or database have taken place.
50298**
50299** In rollback mode, a RESERVED or (if the transaction was opened with
50300** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
50301** moving to this state, but the journal file is not written to or opened
50302** to in this state. If the transaction is committed or rolled back while
50303** in WRITER_LOCKED state, all that is required is to unlock the database
50304** file.
50305**
50306** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
50307** If the connection is running with locking_mode=exclusive, an attempt
50308** is made to obtain an EXCLUSIVE lock on the database file.
50309**
50310** * A write transaction is active.
50311** * If the connection is open in rollback-mode, a RESERVED or greater
50312** lock is held on the database file.
50313** * If the connection is open in WAL-mode, a WAL write transaction
50314** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
50315** called).
50316** * The dbSize, dbOrigSize and dbFileSize variables are all valid.
50317** * The contents of the pager cache have not been modified.
50318** * The journal file may or may not be open.
50319** * Nothing (not even the first header) has been written to the journal.
50320**
50321** WRITER_CACHEMOD:
50322**
50323** A pager moves from WRITER_LOCKED state to this state when a page is
50324** first modified by the upper layer. In rollback mode the journal file
50325** is opened (if it is not already open) and a header written to the
50326** start of it. The database file on disk has not been modified.
50327**
50328** * A write transaction is active.
50329** * A RESERVED or greater lock is held on the database file.
50330** * The journal file is open and the first header has been written
50331** to it, but the header has not been synced to disk.
50332** * The contents of the page cache have been modified.
50333**
50334** WRITER_DBMOD:
50335**
50336** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
50337** when it modifies the contents of the database file. WAL connections
50338** never enter this state (since they do not modify the database file,
50339** just the log file).
50340**
50341** * A write transaction is active.
50342** * An EXCLUSIVE or greater lock is held on the database file.
50343** * The journal file is open and the first header has been written
50344** and synced to disk.
50345** * The contents of the page cache have been modified (and possibly
50346** written to disk).
50347**
50348** WRITER_FINISHED:
50349**
50350** It is not possible for a WAL connection to enter this state.
50351**
50352** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
50353** state after the entire transaction has been successfully written into the
50354** database file. In this state the transaction may be committed simply
50355** by finalizing the journal file. Once in WRITER_FINISHED state, it is
50356** not possible to modify the database further. At this point, the upper
50357** layer must either commit or rollback the transaction.
50358**
50359** * A write transaction is active.
50360** * An EXCLUSIVE or greater lock is held on the database file.
50361** * All writing and syncing of journal and database data has finished.
50362** If no error occurred, all that remains is to finalize the journal to
50363** commit the transaction. If an error did occur, the caller will need
50364** to rollback the transaction.
50365**
50366** ERROR:
50367**
50368** The ERROR state is entered when an IO or disk-full error (including
50369** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
50370** difficult to be sure that the in-memory pager state (cache contents,
50371** db size etc.) are consistent with the contents of the file-system.
50372**
50373** Temporary pager files may enter the ERROR state, but in-memory pagers
50374** cannot.
50375**
50376** For example, if an IO error occurs while performing a rollback,
50377** the contents of the page-cache may be left in an inconsistent state.
50378** At this point it would be dangerous to change back to READER state
50379** (as usually happens after a rollback). Any subsequent readers might
50380** report database corruption (due to the inconsistent cache), and if
50381** they upgrade to writers, they may inadvertently corrupt the database
50382** file. To avoid this hazard, the pager switches into the ERROR state
50383** instead of READER following such an error.
50384**
50385** Once it has entered the ERROR state, any attempt to use the pager
50386** to read or write data returns an error. Eventually, once all
50387** outstanding transactions have been abandoned, the pager is able to
50388** transition back to OPEN state, discarding the contents of the
50389** page-cache and any other in-memory state at the same time. Everything
50390** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
50391** when a read-transaction is next opened on the pager (transitioning
50392** the pager into READER state). At that point the system has recovered
50393** from the error.
50394**
50395** Specifically, the pager jumps into the ERROR state if:
50396**
50397** 1. An error occurs while attempting a rollback. This happens in
50398** function sqlite3PagerRollback().
50399**
50400** 2. An error occurs while attempting to finalize a journal file
50401** following a commit in function sqlite3PagerCommitPhaseTwo().
50402**
50403** 3. An error occurs while attempting to write to the journal or
50404** database file in function pagerStress() in order to free up
50405** memory.
50406**
50407** In other cases, the error is returned to the b-tree layer. The b-tree
50408** layer then attempts a rollback operation. If the error condition
50409** persists, the pager enters the ERROR state via condition (1) above.
50410**
50411** Condition (3) is necessary because it can be triggered by a read-only
50412** statement executed within a transaction. In this case, if the error
50413** code were simply returned to the user, the b-tree layer would not
50414** automatically attempt a rollback, as it assumes that an error in a
50415** read-only statement cannot leave the pager in an internally inconsistent
50416** state.
50417**
50418** * The Pager.errCode variable is set to something other than SQLITE_OK.
50419** * There are one or more outstanding references to pages (after the
50420** last reference is dropped the pager should move back to OPEN state).
50421** * The pager is not an in-memory pager.
50422**
50423**
50424** Notes:
50425**
50426** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
50427** connection is open in WAL mode. A WAL connection is always in one
50428** of the first four states.
50429**
50430** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
50431** state. There are two exceptions: immediately after exclusive-mode has
50432** been turned on (and before any read or write transactions are
50433** executed), and when the pager is leaving the "error state".
50434**
50435** * See also: assert_pager_state().
50436*/
50437#define PAGER_OPEN 0
50438#define PAGER_READER 1
50439#define PAGER_WRITER_LOCKED 2
50440#define PAGER_WRITER_CACHEMOD 3
50441#define PAGER_WRITER_DBMOD 4
50442#define PAGER_WRITER_FINISHED 5
50443#define PAGER_ERROR 6
50444
50445/*
50446** The Pager.eLock variable is almost always set to one of the
50447** following locking-states, according to the lock currently held on
50448** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
50449** This variable is kept up to date as locks are taken and released by
50450** the pagerLockDb() and pagerUnlockDb() wrappers.
50451**
50452** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
50453** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
50454** the operation was successful. In these circumstances pagerLockDb() and
50455** pagerUnlockDb() take a conservative approach - eLock is always updated
50456** when unlocking the file, and only updated when locking the file if the
50457** VFS call is successful. This way, the Pager.eLock variable may be set
50458** to a less exclusive (lower) value than the lock that is actually held
50459** at the system level, but it is never set to a more exclusive value.
50460**
50461** This is usually safe. If an xUnlock fails or appears to fail, there may
50462** be a few redundant xLock() calls or a lock may be held for longer than
50463** required, but nothing really goes wrong.
50464**
50465** The exception is when the database file is unlocked as the pager moves
50466** from ERROR to OPEN state. At this point there may be a hot-journal file
50467** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
50468** transition, by the same pager or any other). If the call to xUnlock()
50469** fails at this point and the pager is left holding an EXCLUSIVE lock, this
50470** can confuse the call to xCheckReservedLock() call made later as part
50471** of hot-journal detection.
50472**
50473** xCheckReservedLock() is defined as returning true "if there is a RESERVED
50474** lock held by this process or any others". So xCheckReservedLock may
50475** return true because the caller itself is holding an EXCLUSIVE lock (but
50476** doesn't know it because of a previous error in xUnlock). If this happens
50477** a hot-journal may be mistaken for a journal being created by an active
50478** transaction in another process, causing SQLite to read from the database
50479** without rolling it back.
50480**
50481** To work around this, if a call to xUnlock() fails when unlocking the
50482** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
50483** is only changed back to a real locking state after a successful call
50484** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
50485** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
50486** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
50487** lock on the database file before attempting to roll it back. See function
50488** PagerSharedLock() for more detail.
50489**
50490** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
50491** PAGER_OPEN state.
50492*/
50493#define UNKNOWN_LOCK (EXCLUSIVE_LOCK+1)
50494
50495/*
50496** A macro used for invoking the codec if there is one
50497*/
50498#ifdef SQLITE_HAS_CODEC
50499# define CODEC1(P,D,N,X,E) \
50500 if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
50501# define CODEC2(P,D,N,X,E,O) \
50502 if( P->xCodec==0 ){ O=(char*)D; }else \
50503 if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
50504#else
50505# define CODEC1(P,D,N,X,E) /* NO-OP */
50506# define CODEC2(P,D,N,X,E,O) O=(char*)D
50507#endif
50508
50509/*
50510** The maximum allowed sector size. 64KiB. If the xSectorsize() method
50511** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
50512** This could conceivably cause corruption following a power failure on
50513** such a system. This is currently an undocumented limit.
50514*/
50515#define MAX_SECTOR_SIZE 0x10000
50516
50517
50518/*
50519** An instance of the following structure is allocated for each active
50520** savepoint and statement transaction in the system. All such structures
50521** are stored in the Pager.aSavepoint[] array, which is allocated and
50522** resized using sqlite3Realloc().
50523**
50524** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
50525** set to 0. If a journal-header is written into the main journal while
50526** the savepoint is active, then iHdrOffset is set to the byte offset
50527** immediately following the last journal record written into the main
50528** journal before the journal-header. This is required during savepoint
50529** rollback (see pagerPlaybackSavepoint()).
50530*/
50531typedef struct PagerSavepoint PagerSavepoint;
50532struct PagerSavepoint {
50533 i64 iOffset; /* Starting offset in main journal */
50534 i64 iHdrOffset; /* See above */
50535 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
50536 Pgno nOrig; /* Original number of pages in file */
50537 Pgno iSubRec; /* Index of first record in sub-journal */
50538#ifndef SQLITE_OMIT_WAL
50539 u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
50540#endif
50541};
50542
50543/*
50544** Bits of the Pager.doNotSpill flag. See further description below.
50545*/
50546#define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */
50547#define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */
50548#define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */
50549
50550/*
50551** An open page cache is an instance of struct Pager. A description of
50552** some of the more important member variables follows:
50553**
50554** eState
50555**
50556** The current 'state' of the pager object. See the comment and state
50557** diagram above for a description of the pager state.
50558**
50559** eLock
50560**
50561** For a real on-disk database, the current lock held on the database file -
50562** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
50563**
50564** For a temporary or in-memory database (neither of which require any
50565** locks), this variable is always set to EXCLUSIVE_LOCK. Since such
50566** databases always have Pager.exclusiveMode==1, this tricks the pager
50567** logic into thinking that it already has all the locks it will ever
50568** need (and no reason to release them).
50569**
50570** In some (obscure) circumstances, this variable may also be set to
50571** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
50572** details.
50573**
50574** changeCountDone
50575**
50576** This boolean variable is used to make sure that the change-counter
50577** (the 4-byte header field at byte offset 24 of the database file) is
50578** not updated more often than necessary.
50579**
50580** It is set to true when the change-counter field is updated, which
50581** can only happen if an exclusive lock is held on the database file.
50582** It is cleared (set to false) whenever an exclusive lock is
50583** relinquished on the database file. Each time a transaction is committed,
50584** The changeCountDone flag is inspected. If it is true, the work of
50585** updating the change-counter is omitted for the current transaction.
50586**
50587** This mechanism means that when running in exclusive mode, a connection
50588** need only update the change-counter once, for the first transaction
50589** committed.
50590**
50591** setMaster
50592**
50593** When PagerCommitPhaseOne() is called to commit a transaction, it may
50594** (or may not) specify a master-journal name to be written into the
50595** journal file before it is synced to disk.
50596**
50597** Whether or not a journal file contains a master-journal pointer affects
50598** the way in which the journal file is finalized after the transaction is
50599** committed or rolled back when running in "journal_mode=PERSIST" mode.
50600** If a journal file does not contain a master-journal pointer, it is
50601** finalized by overwriting the first journal header with zeroes. If
50602** it does contain a master-journal pointer the journal file is finalized
50603** by truncating it to zero bytes, just as if the connection were
50604** running in "journal_mode=truncate" mode.
50605**
50606** Journal files that contain master journal pointers cannot be finalized
50607** simply by overwriting the first journal-header with zeroes, as the
50608** master journal pointer could interfere with hot-journal rollback of any
50609** subsequently interrupted transaction that reuses the journal file.
50610**
50611** The flag is cleared as soon as the journal file is finalized (either
50612** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
50613** journal file from being successfully finalized, the setMaster flag
50614** is cleared anyway (and the pager will move to ERROR state).
50615**
50616** doNotSpill
50617**
50618** This variables control the behavior of cache-spills (calls made by
50619** the pcache module to the pagerStress() routine to write cached data
50620** to the file-system in order to free up memory).
50621**
50622** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
50623** writing to the database from pagerStress() is disabled altogether.
50624** The SPILLFLAG_ROLLBACK case is done in a very obscure case that
50625** comes up during savepoint rollback that requires the pcache module
50626** to allocate a new page to prevent the journal file from being written
50627** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
50628** case is a user preference.
50629**
50630** If the SPILLFLAG_NOSYNC bit is set, writing to the database from
50631** pagerStress() is permitted, but syncing the journal file is not.
50632** This flag is set by sqlite3PagerWrite() when the file-system sector-size
50633** is larger than the database page-size in order to prevent a journal sync
50634** from happening in between the journalling of two pages on the same sector.
50635**
50636** subjInMemory
50637**
50638** This is a boolean variable. If true, then any required sub-journal
50639** is opened as an in-memory journal file. If false, then in-memory
50640** sub-journals are only used for in-memory pager files.
50641**
50642** This variable is updated by the upper layer each time a new
50643** write-transaction is opened.
50644**
50645** dbSize, dbOrigSize, dbFileSize
50646**
50647** Variable dbSize is set to the number of pages in the database file.
50648** It is valid in PAGER_READER and higher states (all states except for
50649** OPEN and ERROR).
50650**
50651** dbSize is set based on the size of the database file, which may be
50652** larger than the size of the database (the value stored at offset
50653** 28 of the database header by the btree). If the size of the file
50654** is not an integer multiple of the page-size, the value stored in
50655** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
50656** Except, any file that is greater than 0 bytes in size is considered
50657** to have at least one page. (i.e. a 1KB file with 2K page-size leads
50658** to dbSize==1).
50659**
50660** During a write-transaction, if pages with page-numbers greater than
50661** dbSize are modified in the cache, dbSize is updated accordingly.
50662** Similarly, if the database is truncated using PagerTruncateImage(),
50663** dbSize is updated.
50664**
50665** Variables dbOrigSize and dbFileSize are valid in states
50666** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
50667** variable at the start of the transaction. It is used during rollback,
50668** and to determine whether or not pages need to be journalled before
50669** being modified.
50670**
50671** Throughout a write-transaction, dbFileSize contains the size of
50672** the file on disk in pages. It is set to a copy of dbSize when the
50673** write-transaction is first opened, and updated when VFS calls are made
50674** to write or truncate the database file on disk.
50675**
50676** The only reason the dbFileSize variable is required is to suppress
50677** unnecessary calls to xTruncate() after committing a transaction. If,
50678** when a transaction is committed, the dbFileSize variable indicates
50679** that the database file is larger than the database image (Pager.dbSize),
50680** pager_truncate() is called. The pager_truncate() call uses xFilesize()
50681** to measure the database file on disk, and then truncates it if required.
50682** dbFileSize is not used when rolling back a transaction. In this case
50683** pager_truncate() is called unconditionally (which means there may be
50684** a call to xFilesize() that is not strictly required). In either case,
50685** pager_truncate() may cause the file to become smaller or larger.
50686**
50687** dbHintSize
50688**
50689** The dbHintSize variable is used to limit the number of calls made to
50690** the VFS xFileControl(FCNTL_SIZE_HINT) method.
50691**
50692** dbHintSize is set to a copy of the dbSize variable when a
50693** write-transaction is opened (at the same time as dbFileSize and
50694** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
50695** dbHintSize is increased to the number of pages that correspond to the
50696** size-hint passed to the method call. See pager_write_pagelist() for
50697** details.
50698**
50699** errCode
50700**
50701** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
50702** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
50703** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
50704** sub-codes.
50705**
50706** syncFlags, walSyncFlags
50707**
50708** syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03).
50709** syncFlags is used for rollback mode. walSyncFlags is used for WAL mode
50710** and contains the flags used to sync the checkpoint operations in the
50711** lower two bits, and sync flags used for transaction commits in the WAL
50712** file in bits 0x04 and 0x08. In other words, to get the correct sync flags
50713** for checkpoint operations, use (walSyncFlags&0x03) and to get the correct
50714** sync flags for transaction commit, use ((walSyncFlags>>2)&0x03). Note
50715** that with synchronous=NORMAL in WAL mode, transaction commit is not synced
50716** meaning that the 0x04 and 0x08 bits are both zero.
50717*/
50718struct Pager {
50719 sqlite3_vfs *pVfs; /* OS functions to use for IO */
50720 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
50721 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
50722 u8 useJournal; /* Use a rollback journal on this file */
50723 u8 noSync; /* Do not sync the journal if true */
50724 u8 fullSync; /* Do extra syncs of the journal for robustness */
50725 u8 extraSync; /* sync directory after journal delete */
50726 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
50727 u8 walSyncFlags; /* See description above */
50728 u8 tempFile; /* zFilename is a temporary or immutable file */
50729 u8 noLock; /* Do not lock (except in WAL mode) */
50730 u8 readOnly; /* True for a read-only database */
50731 u8 memDb; /* True to inhibit all file I/O */
50732
50733 /**************************************************************************
50734 ** The following block contains those class members that change during
50735 ** routine operation. Class members not in this block are either fixed
50736 ** when the pager is first created or else only change when there is a
50737 ** significant mode change (such as changing the page_size, locking_mode,
50738 ** or the journal_mode). From another view, these class members describe
50739 ** the "state" of the pager, while other class members describe the
50740 ** "configuration" of the pager.
50741 */
50742 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
50743 u8 eLock; /* Current lock held on database file */
50744 u8 changeCountDone; /* Set after incrementing the change-counter */
50745 u8 setMaster; /* True if a m-j name has been written to jrnl */
50746 u8 doNotSpill; /* Do not spill the cache when non-zero */
50747 u8 subjInMemory; /* True to use in-memory sub-journals */
50748 u8 bUseFetch; /* True to use xFetch() */
50749 u8 hasHeldSharedLock; /* True if a shared lock has ever been held */
50750 Pgno dbSize; /* Number of pages in the database */
50751 Pgno dbOrigSize; /* dbSize before the current transaction */
50752 Pgno dbFileSize; /* Number of pages in the database file */
50753 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
50754 int errCode; /* One of several kinds of errors */
50755 int nRec; /* Pages journalled since last j-header written */
50756 u32 cksumInit; /* Quasi-random value added to every checksum */
50757 u32 nSubRec; /* Number of records written to sub-journal */
50758 Bitvec *pInJournal; /* One bit for each page in the database file */
50759 sqlite3_file *fd; /* File descriptor for database */
50760 sqlite3_file *jfd; /* File descriptor for main journal */
50761 sqlite3_file *sjfd; /* File descriptor for sub-journal */
50762 i64 journalOff; /* Current write offset in the journal file */
50763 i64 journalHdr; /* Byte offset to previous journal header */
50764 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
50765 PagerSavepoint *aSavepoint; /* Array of active savepoints */
50766 int nSavepoint; /* Number of elements in aSavepoint[] */
50767 u32 iDataVersion; /* Changes whenever database content changes */
50768 char dbFileVers[16]; /* Changes whenever database file changes */
50769
50770 int nMmapOut; /* Number of mmap pages currently outstanding */
50771 sqlite3_int64 szMmap; /* Desired maximum mmap size */
50772 PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
50773 /*
50774 ** End of the routinely-changing class members
50775 ***************************************************************************/
50776
50777 u16 nExtra; /* Add this many bytes to each in-memory page */
50778 i16 nReserve; /* Number of unused bytes at end of each page */
50779 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
50780 u32 sectorSize; /* Assumed sector size during rollback */
50781 int pageSize; /* Number of bytes in a page */
50782 Pgno mxPgno; /* Maximum allowed size of the database */
50783 i64 journalSizeLimit; /* Size limit for persistent journal files */
50784 char *zFilename; /* Name of the database file */
50785 char *zJournal; /* Name of the journal file */
50786 int (*xBusyHandler)(void*); /* Function to call when busy */
50787 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
50788 int aStat[4]; /* Total cache hits, misses, writes, spills */
50789#ifdef SQLITE_TEST
50790 int nRead; /* Database pages read */
50791#endif
50792 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
50793 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
50794#ifdef SQLITE_HAS_CODEC
50795 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
50796 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
50797 void (*xCodecFree)(void*); /* Destructor for the codec */
50798 void *pCodec; /* First argument to xCodec... methods */
50799#endif
50800 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
50801 PCache *pPCache; /* Pointer to page cache object */
50802#ifndef SQLITE_OMIT_WAL
50803 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
50804 char *zWal; /* File name for write-ahead log */
50805#endif
50806};
50807
50808/*
50809** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
50810** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
50811** or CACHE_WRITE to sqlite3_db_status().
50812*/
50813#define PAGER_STAT_HIT 0
50814#define PAGER_STAT_MISS 1
50815#define PAGER_STAT_WRITE 2
50816#define PAGER_STAT_SPILL 3
50817
50818/*
50819** The following global variables hold counters used for
50820** testing purposes only. These variables do not exist in
50821** a non-testing build. These variables are not thread-safe.
50822*/
50823#ifdef SQLITE_TEST
50824SQLITE_API int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
50825SQLITE_API int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
50826SQLITE_API int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
50827# define PAGER_INCR(v) v++
50828#else
50829# define PAGER_INCR(v)
50830#endif
50831
50832
50833
50834/*
50835** Journal files begin with the following magic string. The data
50836** was obtained from /dev/random. It is used only as a sanity check.
50837**
50838** Since version 2.8.0, the journal format contains additional sanity
50839** checking information. If the power fails while the journal is being
50840** written, semi-random garbage data might appear in the journal
50841** file after power is restored. If an attempt is then made
50842** to roll the journal back, the database could be corrupted. The additional
50843** sanity checking data is an attempt to discover the garbage in the
50844** journal and ignore it.
50845**
50846** The sanity checking information for the new journal format consists
50847** of a 32-bit checksum on each page of data. The checksum covers both
50848** the page number and the pPager->pageSize bytes of data for the page.
50849** This cksum is initialized to a 32-bit random value that appears in the
50850** journal file right after the header. The random initializer is important,
50851** because garbage data that appears at the end of a journal is likely
50852** data that was once in other files that have now been deleted. If the
50853** garbage data came from an obsolete journal file, the checksums might
50854** be correct. But by initializing the checksum to random value which
50855** is different for every journal, we minimize that risk.
50856*/
50857static const unsigned char aJournalMagic[] = {
50858 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
50859};
50860
50861/*
50862** The size of the of each page record in the journal is given by
50863** the following macro.
50864*/
50865#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
50866
50867/*
50868** The journal header size for this pager. This is usually the same
50869** size as a single disk sector. See also setSectorSize().
50870*/
50871#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
50872
50873/*
50874** The macro MEMDB is true if we are dealing with an in-memory database.
50875** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
50876** the value of MEMDB will be a constant and the compiler will optimize
50877** out code that would never execute.
50878*/
50879#ifdef SQLITE_OMIT_MEMORYDB
50880# define MEMDB 0
50881#else
50882# define MEMDB pPager->memDb
50883#endif
50884
50885/*
50886** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
50887** interfaces to access the database using memory-mapped I/O.
50888*/
50889#if SQLITE_MAX_MMAP_SIZE>0
50890# define USEFETCH(x) ((x)->bUseFetch)
50891#else
50892# define USEFETCH(x) 0
50893#endif
50894
50895/*
50896** The maximum legal page number is (2^31 - 1).
50897*/
50898#define PAGER_MAX_PGNO 2147483647
50899
50900/*
50901** The argument to this macro is a file descriptor (type sqlite3_file*).
50902** Return 0 if it is not open, or non-zero (but not 1) if it is.
50903**
50904** This is so that expressions can be written as:
50905**
50906** if( isOpen(pPager->jfd) ){ ...
50907**
50908** instead of
50909**
50910** if( pPager->jfd->pMethods ){ ...
50911*/
50912#define isOpen(pFd) ((pFd)->pMethods!=0)
50913
50914/*
50915** Return true if this pager uses a write-ahead log to read page pgno.
50916** Return false if the pager reads pgno directly from the database.
50917*/
50918#if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ)
50919SQLITE_PRIVATE int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){
50920 u32 iRead = 0;
50921 int rc;
50922 if( pPager->pWal==0 ) return 0;
50923 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
50924 return rc || iRead;
50925}
50926#endif
50927#ifndef SQLITE_OMIT_WAL
50928# define pagerUseWal(x) ((x)->pWal!=0)
50929#else
50930# define pagerUseWal(x) 0
50931# define pagerRollbackWal(x) 0
50932# define pagerWalFrames(v,w,x,y) 0
50933# define pagerOpenWalIfPresent(z) SQLITE_OK
50934# define pagerBeginReadTransaction(z) SQLITE_OK
50935#endif
50936
50937#ifndef NDEBUG
50938/*
50939** Usage:
50940**
50941** assert( assert_pager_state(pPager) );
50942**
50943** This function runs many asserts to try to find inconsistencies in
50944** the internal state of the Pager object.
50945*/
50946static int assert_pager_state(Pager *p){
50947 Pager *pPager = p;
50948
50949 /* State must be valid. */
50950 assert( p->eState==PAGER_OPEN
50951 || p->eState==PAGER_READER
50952 || p->eState==PAGER_WRITER_LOCKED
50953 || p->eState==PAGER_WRITER_CACHEMOD
50954 || p->eState==PAGER_WRITER_DBMOD
50955 || p->eState==PAGER_WRITER_FINISHED
50956 || p->eState==PAGER_ERROR
50957 );
50958
50959 /* Regardless of the current state, a temp-file connection always behaves
50960 ** as if it has an exclusive lock on the database file. It never updates
50961 ** the change-counter field, so the changeCountDone flag is always set.
50962 */
50963 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
50964 assert( p->tempFile==0 || pPager->changeCountDone );
50965
50966 /* If the useJournal flag is clear, the journal-mode must be "OFF".
50967 ** And if the journal-mode is "OFF", the journal file must not be open.
50968 */
50969 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
50970 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
50971
50972 /* Check that MEMDB implies noSync. And an in-memory journal. Since
50973 ** this means an in-memory pager performs no IO at all, it cannot encounter
50974 ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
50975 ** a journal file. (although the in-memory journal implementation may
50976 ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
50977 ** is therefore not possible for an in-memory pager to enter the ERROR
50978 ** state.
50979 */
50980 if( MEMDB ){
50981 assert( !isOpen(p->fd) );
50982 assert( p->noSync );
50983 assert( p->journalMode==PAGER_JOURNALMODE_OFF
50984 || p->journalMode==PAGER_JOURNALMODE_MEMORY
50985 );
50986 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
50987 assert( pagerUseWal(p)==0 );
50988 }
50989
50990 /* If changeCountDone is set, a RESERVED lock or greater must be held
50991 ** on the file.
50992 */
50993 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
50994 assert( p->eLock!=PENDING_LOCK );
50995
50996 switch( p->eState ){
50997 case PAGER_OPEN:
50998 assert( !MEMDB );
50999 assert( pPager->errCode==SQLITE_OK );
51000 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
51001 break;
51002
51003 case PAGER_READER:
51004 assert( pPager->errCode==SQLITE_OK );
51005 assert( p->eLock!=UNKNOWN_LOCK );
51006 assert( p->eLock>=SHARED_LOCK );
51007 break;
51008
51009 case PAGER_WRITER_LOCKED:
51010 assert( p->eLock!=UNKNOWN_LOCK );
51011 assert( pPager->errCode==SQLITE_OK );
51012 if( !pagerUseWal(pPager) ){
51013 assert( p->eLock>=RESERVED_LOCK );
51014 }
51015 assert( pPager->dbSize==pPager->dbOrigSize );
51016 assert( pPager->dbOrigSize==pPager->dbFileSize );
51017 assert( pPager->dbOrigSize==pPager->dbHintSize );
51018 assert( pPager->setMaster==0 );
51019 break;
51020
51021 case PAGER_WRITER_CACHEMOD:
51022 assert( p->eLock!=UNKNOWN_LOCK );
51023 assert( pPager->errCode==SQLITE_OK );
51024 if( !pagerUseWal(pPager) ){
51025 /* It is possible that if journal_mode=wal here that neither the
51026 ** journal file nor the WAL file are open. This happens during
51027 ** a rollback transaction that switches from journal_mode=off
51028 ** to journal_mode=wal.
51029 */
51030 assert( p->eLock>=RESERVED_LOCK );
51031 assert( isOpen(p->jfd)
51032 || p->journalMode==PAGER_JOURNALMODE_OFF
51033 || p->journalMode==PAGER_JOURNALMODE_WAL
51034 );
51035 }
51036 assert( pPager->dbOrigSize==pPager->dbFileSize );
51037 assert( pPager->dbOrigSize==pPager->dbHintSize );
51038 break;
51039
51040 case PAGER_WRITER_DBMOD:
51041 assert( p->eLock==EXCLUSIVE_LOCK );
51042 assert( pPager->errCode==SQLITE_OK );
51043 assert( !pagerUseWal(pPager) );
51044 assert( p->eLock>=EXCLUSIVE_LOCK );
51045 assert( isOpen(p->jfd)
51046 || p->journalMode==PAGER_JOURNALMODE_OFF
51047 || p->journalMode==PAGER_JOURNALMODE_WAL
51048 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
51049 );
51050 assert( pPager->dbOrigSize<=pPager->dbHintSize );
51051 break;
51052
51053 case PAGER_WRITER_FINISHED:
51054 assert( p->eLock==EXCLUSIVE_LOCK );
51055 assert( pPager->errCode==SQLITE_OK );
51056 assert( !pagerUseWal(pPager) );
51057 assert( isOpen(p->jfd)
51058 || p->journalMode==PAGER_JOURNALMODE_OFF
51059 || p->journalMode==PAGER_JOURNALMODE_WAL
51060 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
51061 );
51062 break;
51063
51064 case PAGER_ERROR:
51065 /* There must be at least one outstanding reference to the pager if
51066 ** in ERROR state. Otherwise the pager should have already dropped
51067 ** back to OPEN state.
51068 */
51069 assert( pPager->errCode!=SQLITE_OK );
51070 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
51071 break;
51072 }
51073
51074 return 1;
51075}
51076#endif /* ifndef NDEBUG */
51077
51078#ifdef SQLITE_DEBUG
51079/*
51080** Return a pointer to a human readable string in a static buffer
51081** containing the state of the Pager object passed as an argument. This
51082** is intended to be used within debuggers. For example, as an alternative
51083** to "print *pPager" in gdb:
51084**
51085** (gdb) printf "%s", print_pager_state(pPager)
51086**
51087** This routine has external linkage in order to suppress compiler warnings
51088** about an unused function. It is enclosed within SQLITE_DEBUG and so does
51089** not appear in normal builds.
51090*/
51091char *print_pager_state(Pager *p){
51092 static char zRet[1024];
51093
51094 sqlite3_snprintf(1024, zRet,
51095 "Filename: %s\n"
51096 "State: %s errCode=%d\n"
51097 "Lock: %s\n"
51098 "Locking mode: locking_mode=%s\n"
51099 "Journal mode: journal_mode=%s\n"
51100 "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
51101 "Journal: journalOff=%lld journalHdr=%lld\n"
51102 "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
51103 , p->zFilename
51104 , p->eState==PAGER_OPEN ? "OPEN" :
51105 p->eState==PAGER_READER ? "READER" :
51106 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
51107 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
51108 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
51109 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
51110 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
51111 , (int)p->errCode
51112 , p->eLock==NO_LOCK ? "NO_LOCK" :
51113 p->eLock==RESERVED_LOCK ? "RESERVED" :
51114 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
51115 p->eLock==SHARED_LOCK ? "SHARED" :
51116 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
51117 , p->exclusiveMode ? "exclusive" : "normal"
51118 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
51119 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
51120 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
51121 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
51122 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
51123 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
51124 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
51125 , p->journalOff, p->journalHdr
51126 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
51127 );
51128
51129 return zRet;
51130}
51131#endif
51132
51133/* Forward references to the various page getters */
51134static int getPageNormal(Pager*,Pgno,DbPage**,int);
51135static int getPageError(Pager*,Pgno,DbPage**,int);
51136#if SQLITE_MAX_MMAP_SIZE>0
51137static int getPageMMap(Pager*,Pgno,DbPage**,int);
51138#endif
51139
51140/*
51141** Set the Pager.xGet method for the appropriate routine used to fetch
51142** content from the pager.
51143*/
51144static void setGetterMethod(Pager *pPager){
51145 if( pPager->errCode ){
51146 pPager->xGet = getPageError;
51147#if SQLITE_MAX_MMAP_SIZE>0
51148 }else if( USEFETCH(pPager)
51149#ifdef SQLITE_HAS_CODEC
51150 && pPager->xCodec==0
51151#endif
51152 ){
51153 pPager->xGet = getPageMMap;
51154#endif /* SQLITE_MAX_MMAP_SIZE>0 */
51155 }else{
51156 pPager->xGet = getPageNormal;
51157 }
51158}
51159
51160/*
51161** Return true if it is necessary to write page *pPg into the sub-journal.
51162** A page needs to be written into the sub-journal if there exists one
51163** or more open savepoints for which:
51164**
51165** * The page-number is less than or equal to PagerSavepoint.nOrig, and
51166** * The bit corresponding to the page-number is not set in
51167** PagerSavepoint.pInSavepoint.
51168*/
51169static int subjRequiresPage(PgHdr *pPg){
51170 Pager *pPager = pPg->pPager;
51171 PagerSavepoint *p;
51172 Pgno pgno = pPg->pgno;
51173 int i;
51174 for(i=0; i<pPager->nSavepoint; i++){
51175 p = &pPager->aSavepoint[i];
51176 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
51177 return 1;
51178 }
51179 }
51180 return 0;
51181}
51182
51183#ifdef SQLITE_DEBUG
51184/*
51185** Return true if the page is already in the journal file.
51186*/
51187static int pageInJournal(Pager *pPager, PgHdr *pPg){
51188 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
51189}
51190#endif
51191
51192/*
51193** Read a 32-bit integer from the given file descriptor. Store the integer
51194** that is read in *pRes. Return SQLITE_OK if everything worked, or an
51195** error code is something goes wrong.
51196**
51197** All values are stored on disk as big-endian.
51198*/
51199static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
51200 unsigned char ac[4];
51201 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
51202 if( rc==SQLITE_OK ){
51203 *pRes = sqlite3Get4byte(ac);
51204 }
51205 return rc;
51206}
51207
51208/*
51209** Write a 32-bit integer into a string buffer in big-endian byte order.
51210*/
51211#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
51212
51213
51214/*
51215** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
51216** on success or an error code is something goes wrong.
51217*/
51218static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
51219 char ac[4];
51220 put32bits(ac, val);
51221 return sqlite3OsWrite(fd, ac, 4, offset);
51222}
51223
51224/*
51225** Unlock the database file to level eLock, which must be either NO_LOCK
51226** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
51227** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
51228**
51229** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
51230** called, do not modify it. See the comment above the #define of
51231** UNKNOWN_LOCK for an explanation of this.
51232*/
51233static int pagerUnlockDb(Pager *pPager, int eLock){
51234 int rc = SQLITE_OK;
51235
51236 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
51237 assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
51238 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
51239 if( isOpen(pPager->fd) ){
51240 assert( pPager->eLock>=eLock );
51241 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
51242 if( pPager->eLock!=UNKNOWN_LOCK ){
51243 pPager->eLock = (u8)eLock;
51244 }
51245 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
51246 }
51247 return rc;
51248}
51249
51250/*
51251** Lock the database file to level eLock, which must be either SHARED_LOCK,
51252** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
51253** Pager.eLock variable to the new locking state.
51254**
51255** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
51256** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
51257** See the comment above the #define of UNKNOWN_LOCK for an explanation
51258** of this.
51259*/
51260static int pagerLockDb(Pager *pPager, int eLock){
51261 int rc = SQLITE_OK;
51262
51263 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
51264 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
51265 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
51266 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
51267 pPager->eLock = (u8)eLock;
51268 IOTRACE(("LOCK %p %d\n", pPager, eLock))
51269 }
51270 }
51271 return rc;
51272}
51273
51274/*
51275** This function determines whether or not the atomic-write or
51276** atomic-batch-write optimizations can be used with this pager. The
51277** atomic-write optimization can be used if:
51278**
51279** (a) the value returned by OsDeviceCharacteristics() indicates that
51280** a database page may be written atomically, and
51281** (b) the value returned by OsSectorSize() is less than or equal
51282** to the page size.
51283**
51284** If it can be used, then the value returned is the size of the journal
51285** file when it contains rollback data for exactly one page.
51286**
51287** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
51288** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
51289** returned in this case.
51290**
51291** If neither optimization can be used, 0 is returned.
51292*/
51293static int jrnlBufferSize(Pager *pPager){
51294 assert( !MEMDB );
51295
51296#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
51297 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
51298 int dc; /* Device characteristics */
51299
51300 assert( isOpen(pPager->fd) );
51301 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
51302#else
51303 UNUSED_PARAMETER(pPager);
51304#endif
51305
51306#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
51307 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
51308 return -1;
51309 }
51310#endif
51311
51312#ifdef SQLITE_ENABLE_ATOMIC_WRITE
51313 {
51314 int nSector = pPager->sectorSize;
51315 int szPage = pPager->pageSize;
51316
51317 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
51318 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
51319 if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
51320 return 0;
51321 }
51322 }
51323
51324 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
51325#endif
51326
51327 return 0;
51328}
51329
51330/*
51331** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
51332** on the cache using a hash function. This is used for testing
51333** and debugging only.
51334*/
51335#ifdef SQLITE_CHECK_PAGES
51336/*
51337** Return a 32-bit hash of the page data for pPage.
51338*/
51339static u32 pager_datahash(int nByte, unsigned char *pData){
51340 u32 hash = 0;
51341 int i;
51342 for(i=0; i<nByte; i++){
51343 hash = (hash*1039) + pData[i];
51344 }
51345 return hash;
51346}
51347static u32 pager_pagehash(PgHdr *pPage){
51348 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
51349}
51350static void pager_set_pagehash(PgHdr *pPage){
51351 pPage->pageHash = pager_pagehash(pPage);
51352}
51353
51354/*
51355** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
51356** is defined, and NDEBUG is not defined, an assert() statement checks
51357** that the page is either dirty or still matches the calculated page-hash.
51358*/
51359#define CHECK_PAGE(x) checkPage(x)
51360static void checkPage(PgHdr *pPg){
51361 Pager *pPager = pPg->pPager;
51362 assert( pPager->eState!=PAGER_ERROR );
51363 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
51364}
51365
51366#else
51367#define pager_datahash(X,Y) 0
51368#define pager_pagehash(X) 0
51369#define pager_set_pagehash(X)
51370#define CHECK_PAGE(x)
51371#endif /* SQLITE_CHECK_PAGES */
51372
51373/*
51374** When this is called the journal file for pager pPager must be open.
51375** This function attempts to read a master journal file name from the
51376** end of the file and, if successful, copies it into memory supplied
51377** by the caller. See comments above writeMasterJournal() for the format
51378** used to store a master journal file name at the end of a journal file.
51379**
51380** zMaster must point to a buffer of at least nMaster bytes allocated by
51381** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
51382** enough space to write the master journal name). If the master journal
51383** name in the journal is longer than nMaster bytes (including a
51384** nul-terminator), then this is handled as if no master journal name
51385** were present in the journal.
51386**
51387** If a master journal file name is present at the end of the journal
51388** file, then it is copied into the buffer pointed to by zMaster. A
51389** nul-terminator byte is appended to the buffer following the master
51390** journal file name.
51391**
51392** If it is determined that no master journal file name is present
51393** zMaster[0] is set to 0 and SQLITE_OK returned.
51394**
51395** If an error occurs while reading from the journal file, an SQLite
51396** error code is returned.
51397*/
51398static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
51399 int rc; /* Return code */
51400 u32 len; /* Length in bytes of master journal name */
51401 i64 szJ; /* Total size in bytes of journal file pJrnl */
51402 u32 cksum; /* MJ checksum value read from journal */
51403 u32 u; /* Unsigned loop counter */
51404 unsigned char aMagic[8]; /* A buffer to hold the magic header */
51405 zMaster[0] = '\0';
51406
51407 if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
51408 || szJ<16
51409 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
51410 || len>=nMaster
51411 || len>szJ-16
51412 || len==0
51413 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
51414 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
51415 || memcmp(aMagic, aJournalMagic, 8)
51416 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
51417 ){
51418 return rc;
51419 }
51420
51421 /* See if the checksum matches the master journal name */
51422 for(u=0; u<len; u++){
51423 cksum -= zMaster[u];
51424 }
51425 if( cksum ){
51426 /* If the checksum doesn't add up, then one or more of the disk sectors
51427 ** containing the master journal filename is corrupted. This means
51428 ** definitely roll back, so just return SQLITE_OK and report a (nul)
51429 ** master-journal filename.
51430 */
51431 len = 0;
51432 }
51433 zMaster[len] = '\0';
51434
51435 return SQLITE_OK;
51436}
51437
51438/*
51439** Return the offset of the sector boundary at or immediately
51440** following the value in pPager->journalOff, assuming a sector
51441** size of pPager->sectorSize bytes.
51442**
51443** i.e for a sector size of 512:
51444**
51445** Pager.journalOff Return value
51446** ---------------------------------------
51447** 0 0
51448** 512 512
51449** 100 512
51450** 2000 2048
51451**
51452*/
51453static i64 journalHdrOffset(Pager *pPager){
51454 i64 offset = 0;
51455 i64 c = pPager->journalOff;
51456 if( c ){
51457 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
51458 }
51459 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
51460 assert( offset>=c );
51461 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
51462 return offset;
51463}
51464
51465/*
51466** The journal file must be open when this function is called.
51467**
51468** This function is a no-op if the journal file has not been written to
51469** within the current transaction (i.e. if Pager.journalOff==0).
51470**
51471** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
51472** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
51473** zero the 28-byte header at the start of the journal file. In either case,
51474** if the pager is not in no-sync mode, sync the journal file immediately
51475** after writing or truncating it.
51476**
51477** If Pager.journalSizeLimit is set to a positive, non-zero value, and
51478** following the truncation or zeroing described above the size of the
51479** journal file in bytes is larger than this value, then truncate the
51480** journal file to Pager.journalSizeLimit bytes. The journal file does
51481** not need to be synced following this operation.
51482**
51483** If an IO error occurs, abandon processing and return the IO error code.
51484** Otherwise, return SQLITE_OK.
51485*/
51486static int zeroJournalHdr(Pager *pPager, int doTruncate){
51487 int rc = SQLITE_OK; /* Return code */
51488 assert( isOpen(pPager->jfd) );
51489 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
51490 if( pPager->journalOff ){
51491 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
51492
51493 IOTRACE(("JZEROHDR %p\n", pPager))
51494 if( doTruncate || iLimit==0 ){
51495 rc = sqlite3OsTruncate(pPager->jfd, 0);
51496 }else{
51497 static const char zeroHdr[28] = {0};
51498 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
51499 }
51500 if( rc==SQLITE_OK && !pPager->noSync ){
51501 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
51502 }
51503
51504 /* At this point the transaction is committed but the write lock
51505 ** is still held on the file. If there is a size limit configured for
51506 ** the persistent journal and the journal file currently consumes more
51507 ** space than that limit allows for, truncate it now. There is no need
51508 ** to sync the file following this operation.
51509 */
51510 if( rc==SQLITE_OK && iLimit>0 ){
51511 i64 sz;
51512 rc = sqlite3OsFileSize(pPager->jfd, &sz);
51513 if( rc==SQLITE_OK && sz>iLimit ){
51514 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
51515 }
51516 }
51517 }
51518 return rc;
51519}
51520
51521/*
51522** The journal file must be open when this routine is called. A journal
51523** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
51524** current location.
51525**
51526** The format for the journal header is as follows:
51527** - 8 bytes: Magic identifying journal format.
51528** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
51529** - 4 bytes: Random number used for page hash.
51530** - 4 bytes: Initial database page count.
51531** - 4 bytes: Sector size used by the process that wrote this journal.
51532** - 4 bytes: Database page size.
51533**
51534** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
51535*/
51536static int writeJournalHdr(Pager *pPager){
51537 int rc = SQLITE_OK; /* Return code */
51538 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
51539 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
51540 u32 nWrite; /* Bytes of header sector written */
51541 int ii; /* Loop counter */
51542
51543 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
51544
51545 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
51546 nHeader = JOURNAL_HDR_SZ(pPager);
51547 }
51548
51549 /* If there are active savepoints and any of them were created
51550 ** since the most recent journal header was written, update the
51551 ** PagerSavepoint.iHdrOffset fields now.
51552 */
51553 for(ii=0; ii<pPager->nSavepoint; ii++){
51554 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
51555 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
51556 }
51557 }
51558
51559 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
51560
51561 /*
51562 ** Write the nRec Field - the number of page records that follow this
51563 ** journal header. Normally, zero is written to this value at this time.
51564 ** After the records are added to the journal (and the journal synced,
51565 ** if in full-sync mode), the zero is overwritten with the true number
51566 ** of records (see syncJournal()).
51567 **
51568 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
51569 ** reading the journal this value tells SQLite to assume that the
51570 ** rest of the journal file contains valid page records. This assumption
51571 ** is dangerous, as if a failure occurred whilst writing to the journal
51572 ** file it may contain some garbage data. There are two scenarios
51573 ** where this risk can be ignored:
51574 **
51575 ** * When the pager is in no-sync mode. Corruption can follow a
51576 ** power failure in this case anyway.
51577 **
51578 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
51579 ** that garbage data is never appended to the journal file.
51580 */
51581 assert( isOpen(pPager->fd) || pPager->noSync );
51582 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
51583 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
51584 ){
51585 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
51586 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
51587 }else{
51588 memset(zHeader, 0, sizeof(aJournalMagic)+4);
51589 }
51590
51591 /* The random check-hash initializer */
51592 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
51593 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
51594 /* The initial database size */
51595 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
51596 /* The assumed sector size for this process */
51597 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
51598
51599 /* The page size */
51600 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
51601
51602 /* Initializing the tail of the buffer is not necessary. Everything
51603 ** works find if the following memset() is omitted. But initializing
51604 ** the memory prevents valgrind from complaining, so we are willing to
51605 ** take the performance hit.
51606 */
51607 memset(&zHeader[sizeof(aJournalMagic)+20], 0,
51608 nHeader-(sizeof(aJournalMagic)+20));
51609
51610 /* In theory, it is only necessary to write the 28 bytes that the
51611 ** journal header consumes to the journal file here. Then increment the
51612 ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
51613 ** record is written to the following sector (leaving a gap in the file
51614 ** that will be implicitly filled in by the OS).
51615 **
51616 ** However it has been discovered that on some systems this pattern can
51617 ** be significantly slower than contiguously writing data to the file,
51618 ** even if that means explicitly writing data to the block of
51619 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
51620 ** is done.
51621 **
51622 ** The loop is required here in case the sector-size is larger than the
51623 ** database page size. Since the zHeader buffer is only Pager.pageSize
51624 ** bytes in size, more than one call to sqlite3OsWrite() may be required
51625 ** to populate the entire journal header sector.
51626 */
51627 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
51628 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
51629 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
51630 assert( pPager->journalHdr <= pPager->journalOff );
51631 pPager->journalOff += nHeader;
51632 }
51633
51634 return rc;
51635}
51636
51637/*
51638** The journal file must be open when this is called. A journal header file
51639** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
51640** file. The current location in the journal file is given by
51641** pPager->journalOff. See comments above function writeJournalHdr() for
51642** a description of the journal header format.
51643**
51644** If the header is read successfully, *pNRec is set to the number of
51645** page records following this header and *pDbSize is set to the size of the
51646** database before the transaction began, in pages. Also, pPager->cksumInit
51647** is set to the value read from the journal header. SQLITE_OK is returned
51648** in this case.
51649**
51650** If the journal header file appears to be corrupted, SQLITE_DONE is
51651** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
51652** cannot be read from the journal file an error code is returned.
51653*/
51654static int readJournalHdr(
51655 Pager *pPager, /* Pager object */
51656 int isHot,
51657 i64 journalSize, /* Size of the open journal file in bytes */
51658 u32 *pNRec, /* OUT: Value read from the nRec field */
51659 u32 *pDbSize /* OUT: Value of original database size field */
51660){
51661 int rc; /* Return code */
51662 unsigned char aMagic[8]; /* A buffer to hold the magic header */
51663 i64 iHdrOff; /* Offset of journal header being read */
51664
51665 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
51666
51667 /* Advance Pager.journalOff to the start of the next sector. If the
51668 ** journal file is too small for there to be a header stored at this
51669 ** point, return SQLITE_DONE.
51670 */
51671 pPager->journalOff = journalHdrOffset(pPager);
51672 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
51673 return SQLITE_DONE;
51674 }
51675 iHdrOff = pPager->journalOff;
51676
51677 /* Read in the first 8 bytes of the journal header. If they do not match
51678 ** the magic string found at the start of each journal header, return
51679 ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
51680 ** proceed.
51681 */
51682 if( isHot || iHdrOff!=pPager->journalHdr ){
51683 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
51684 if( rc ){
51685 return rc;
51686 }
51687 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
51688 return SQLITE_DONE;
51689 }
51690 }
51691
51692 /* Read the first three 32-bit fields of the journal header: The nRec
51693 ** field, the checksum-initializer and the database size at the start
51694 ** of the transaction. Return an error code if anything goes wrong.
51695 */
51696 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
51697 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
51698 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
51699 ){
51700 return rc;
51701 }
51702
51703 if( pPager->journalOff==0 ){
51704 u32 iPageSize; /* Page-size field of journal header */
51705 u32 iSectorSize; /* Sector-size field of journal header */
51706
51707 /* Read the page-size and sector-size journal header fields. */
51708 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
51709 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
51710 ){
51711 return rc;
51712 }
51713
51714 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
51715 ** journal header to zero. In this case, assume that the Pager.pageSize
51716 ** variable is already set to the correct page size.
51717 */
51718 if( iPageSize==0 ){
51719 iPageSize = pPager->pageSize;
51720 }
51721
51722 /* Check that the values read from the page-size and sector-size fields
51723 ** are within range. To be 'in range', both values need to be a power
51724 ** of two greater than or equal to 512 or 32, and not greater than their
51725 ** respective compile time maximum limits.
51726 */
51727 if( iPageSize<512 || iSectorSize<32
51728 || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
51729 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
51730 ){
51731 /* If the either the page-size or sector-size in the journal-header is
51732 ** invalid, then the process that wrote the journal-header must have
51733 ** crashed before the header was synced. In this case stop reading
51734 ** the journal file here.
51735 */
51736 return SQLITE_DONE;
51737 }
51738
51739 /* Update the page-size to match the value read from the journal.
51740 ** Use a testcase() macro to make sure that malloc failure within
51741 ** PagerSetPagesize() is tested.
51742 */
51743 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
51744 testcase( rc!=SQLITE_OK );
51745
51746 /* Update the assumed sector-size to match the value used by
51747 ** the process that created this journal. If this journal was
51748 ** created by a process other than this one, then this routine
51749 ** is being called from within pager_playback(). The local value
51750 ** of Pager.sectorSize is restored at the end of that routine.
51751 */
51752 pPager->sectorSize = iSectorSize;
51753 }
51754
51755 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
51756 return rc;
51757}
51758
51759
51760/*
51761** Write the supplied master journal name into the journal file for pager
51762** pPager at the current location. The master journal name must be the last
51763** thing written to a journal file. If the pager is in full-sync mode, the
51764** journal file descriptor is advanced to the next sector boundary before
51765** anything is written. The format is:
51766**
51767** + 4 bytes: PAGER_MJ_PGNO.
51768** + N bytes: Master journal filename in utf-8.
51769** + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
51770** + 4 bytes: Master journal name checksum.
51771** + 8 bytes: aJournalMagic[].
51772**
51773** The master journal page checksum is the sum of the bytes in the master
51774** journal name, where each byte is interpreted as a signed 8-bit integer.
51775**
51776** If zMaster is a NULL pointer (occurs for a single database transaction),
51777** this call is a no-op.
51778*/
51779static int writeMasterJournal(Pager *pPager, const char *zMaster){
51780 int rc; /* Return code */
51781 int nMaster; /* Length of string zMaster */
51782 i64 iHdrOff; /* Offset of header in journal file */
51783 i64 jrnlSize; /* Size of journal file on disk */
51784 u32 cksum = 0; /* Checksum of string zMaster */
51785
51786 assert( pPager->setMaster==0 );
51787 assert( !pagerUseWal(pPager) );
51788
51789 if( !zMaster
51790 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
51791 || !isOpen(pPager->jfd)
51792 ){
51793 return SQLITE_OK;
51794 }
51795 pPager->setMaster = 1;
51796 assert( pPager->journalHdr <= pPager->journalOff );
51797
51798 /* Calculate the length in bytes and the checksum of zMaster */
51799 for(nMaster=0; zMaster[nMaster]; nMaster++){
51800 cksum += zMaster[nMaster];
51801 }
51802
51803 /* If in full-sync mode, advance to the next disk sector before writing
51804 ** the master journal name. This is in case the previous page written to
51805 ** the journal has already been synced.
51806 */
51807 if( pPager->fullSync ){
51808 pPager->journalOff = journalHdrOffset(pPager);
51809 }
51810 iHdrOff = pPager->journalOff;
51811
51812 /* Write the master journal data to the end of the journal file. If
51813 ** an error occurs, return the error code to the caller.
51814 */
51815 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
51816 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
51817 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
51818 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
51819 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
51820 iHdrOff+4+nMaster+8)))
51821 ){
51822 return rc;
51823 }
51824 pPager->journalOff += (nMaster+20);
51825
51826 /* If the pager is in peristent-journal mode, then the physical
51827 ** journal-file may extend past the end of the master-journal name
51828 ** and 8 bytes of magic data just written to the file. This is
51829 ** dangerous because the code to rollback a hot-journal file
51830 ** will not be able to find the master-journal name to determine
51831 ** whether or not the journal is hot.
51832 **
51833 ** Easiest thing to do in this scenario is to truncate the journal
51834 ** file to the required size.
51835 */
51836 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
51837 && jrnlSize>pPager->journalOff
51838 ){
51839 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
51840 }
51841 return rc;
51842}
51843
51844/*
51845** Discard the entire contents of the in-memory page-cache.
51846*/
51847static void pager_reset(Pager *pPager){
51848 pPager->iDataVersion++;
51849 sqlite3BackupRestart(pPager->pBackup);
51850 sqlite3PcacheClear(pPager->pPCache);
51851}
51852
51853/*
51854** Return the pPager->iDataVersion value
51855*/
51856SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager *pPager){
51857 return pPager->iDataVersion;
51858}
51859
51860/*
51861** Free all structures in the Pager.aSavepoint[] array and set both
51862** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
51863** if it is open and the pager is not in exclusive mode.
51864*/
51865static void releaseAllSavepoints(Pager *pPager){
51866 int ii; /* Iterator for looping through Pager.aSavepoint */
51867 for(ii=0; ii<pPager->nSavepoint; ii++){
51868 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
51869 }
51870 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
51871 sqlite3OsClose(pPager->sjfd);
51872 }
51873 sqlite3_free(pPager->aSavepoint);
51874 pPager->aSavepoint = 0;
51875 pPager->nSavepoint = 0;
51876 pPager->nSubRec = 0;
51877}
51878
51879/*
51880** Set the bit number pgno in the PagerSavepoint.pInSavepoint
51881** bitvecs of all open savepoints. Return SQLITE_OK if successful
51882** or SQLITE_NOMEM if a malloc failure occurs.
51883*/
51884static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
51885 int ii; /* Loop counter */
51886 int rc = SQLITE_OK; /* Result code */
51887
51888 for(ii=0; ii<pPager->nSavepoint; ii++){
51889 PagerSavepoint *p = &pPager->aSavepoint[ii];
51890 if( pgno<=p->nOrig ){
51891 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
51892 testcase( rc==SQLITE_NOMEM );
51893 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
51894 }
51895 }
51896 return rc;
51897}
51898
51899/*
51900** This function is a no-op if the pager is in exclusive mode and not
51901** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
51902** state.
51903**
51904** If the pager is not in exclusive-access mode, the database file is
51905** completely unlocked. If the file is unlocked and the file-system does
51906** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
51907** closed (if it is open).
51908**
51909** If the pager is in ERROR state when this function is called, the
51910** contents of the pager cache are discarded before switching back to
51911** the OPEN state. Regardless of whether the pager is in exclusive-mode
51912** or not, any journal file left in the file-system will be treated
51913** as a hot-journal and rolled back the next time a read-transaction
51914** is opened (by this or by any other connection).
51915*/
51916static void pager_unlock(Pager *pPager){
51917
51918 assert( pPager->eState==PAGER_READER
51919 || pPager->eState==PAGER_OPEN
51920 || pPager->eState==PAGER_ERROR
51921 );
51922
51923 sqlite3BitvecDestroy(pPager->pInJournal);
51924 pPager->pInJournal = 0;
51925 releaseAllSavepoints(pPager);
51926
51927 if( pagerUseWal(pPager) ){
51928 assert( !isOpen(pPager->jfd) );
51929 sqlite3WalEndReadTransaction(pPager->pWal);
51930 pPager->eState = PAGER_OPEN;
51931 }else if( !pPager->exclusiveMode ){
51932 int rc; /* Error code returned by pagerUnlockDb() */
51933 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
51934
51935 /* If the operating system support deletion of open files, then
51936 ** close the journal file when dropping the database lock. Otherwise
51937 ** another connection with journal_mode=delete might delete the file
51938 ** out from under us.
51939 */
51940 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 );
51941 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 );
51942 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 );
51943 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 );
51944 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
51945 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
51946 if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
51947 || 1!=(pPager->journalMode & 5)
51948 ){
51949 sqlite3OsClose(pPager->jfd);
51950 }
51951
51952 /* If the pager is in the ERROR state and the call to unlock the database
51953 ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
51954 ** above the #define for UNKNOWN_LOCK for an explanation of why this
51955 ** is necessary.
51956 */
51957 rc = pagerUnlockDb(pPager, NO_LOCK);
51958 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
51959 pPager->eLock = UNKNOWN_LOCK;
51960 }
51961
51962 /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
51963 ** without clearing the error code. This is intentional - the error
51964 ** code is cleared and the cache reset in the block below.
51965 */
51966 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
51967 pPager->changeCountDone = 0;
51968 pPager->eState = PAGER_OPEN;
51969 }
51970
51971 /* If Pager.errCode is set, the contents of the pager cache cannot be
51972 ** trusted. Now that there are no outstanding references to the pager,
51973 ** it can safely move back to PAGER_OPEN state. This happens in both
51974 ** normal and exclusive-locking mode.
51975 */
51976 assert( pPager->errCode==SQLITE_OK || !MEMDB );
51977 if( pPager->errCode ){
51978 if( pPager->tempFile==0 ){
51979 pager_reset(pPager);
51980 pPager->changeCountDone = 0;
51981 pPager->eState = PAGER_OPEN;
51982 }else{
51983 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
51984 }
51985 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
51986 pPager->errCode = SQLITE_OK;
51987 setGetterMethod(pPager);
51988 }
51989
51990 pPager->journalOff = 0;
51991 pPager->journalHdr = 0;
51992 pPager->setMaster = 0;
51993}
51994
51995/*
51996** This function is called whenever an IOERR or FULL error that requires
51997** the pager to transition into the ERROR state may ahve occurred.
51998** The first argument is a pointer to the pager structure, the second
51999** the error-code about to be returned by a pager API function. The
52000** value returned is a copy of the second argument to this function.
52001**
52002** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
52003** IOERR sub-codes, the pager enters the ERROR state and the error code
52004** is stored in Pager.errCode. While the pager remains in the ERROR state,
52005** all major API calls on the Pager will immediately return Pager.errCode.
52006**
52007** The ERROR state indicates that the contents of the pager-cache
52008** cannot be trusted. This state can be cleared by completely discarding
52009** the contents of the pager-cache. If a transaction was active when
52010** the persistent error occurred, then the rollback journal may need
52011** to be replayed to restore the contents of the database file (as if
52012** it were a hot-journal).
52013*/
52014static int pager_error(Pager *pPager, int rc){
52015 int rc2 = rc & 0xff;
52016 assert( rc==SQLITE_OK || !MEMDB );
52017 assert(
52018 pPager->errCode==SQLITE_FULL ||
52019 pPager->errCode==SQLITE_OK ||
52020 (pPager->errCode & 0xff)==SQLITE_IOERR
52021 );
52022 if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
52023 pPager->errCode = rc;
52024 pPager->eState = PAGER_ERROR;
52025 setGetterMethod(pPager);
52026 }
52027 return rc;
52028}
52029
52030static int pager_truncate(Pager *pPager, Pgno nPage);
52031
52032/*
52033** The write transaction open on pPager is being committed (bCommit==1)
52034** or rolled back (bCommit==0).
52035**
52036** Return TRUE if and only if all dirty pages should be flushed to disk.
52037**
52038** Rules:
52039**
52040** * For non-TEMP databases, always sync to disk. This is necessary
52041** for transactions to be durable.
52042**
52043** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
52044** file has been created already (via a spill on pagerStress()) and
52045** when the number of dirty pages in memory exceeds 25% of the total
52046** cache size.
52047*/
52048static int pagerFlushOnCommit(Pager *pPager, int bCommit){
52049 if( pPager->tempFile==0 ) return 1;
52050 if( !bCommit ) return 0;
52051 if( !isOpen(pPager->fd) ) return 0;
52052 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
52053}
52054
52055/*
52056** This routine ends a transaction. A transaction is usually ended by
52057** either a COMMIT or a ROLLBACK operation. This routine may be called
52058** after rollback of a hot-journal, or if an error occurs while opening
52059** the journal file or writing the very first journal-header of a
52060** database transaction.
52061**
52062** This routine is never called in PAGER_ERROR state. If it is called
52063** in PAGER_NONE or PAGER_SHARED state and the lock held is less
52064** exclusive than a RESERVED lock, it is a no-op.
52065**
52066** Otherwise, any active savepoints are released.
52067**
52068** If the journal file is open, then it is "finalized". Once a journal
52069** file has been finalized it is not possible to use it to roll back a
52070** transaction. Nor will it be considered to be a hot-journal by this
52071** or any other database connection. Exactly how a journal is finalized
52072** depends on whether or not the pager is running in exclusive mode and
52073** the current journal-mode (Pager.journalMode value), as follows:
52074**
52075** journalMode==MEMORY
52076** Journal file descriptor is simply closed. This destroys an
52077** in-memory journal.
52078**
52079** journalMode==TRUNCATE
52080** Journal file is truncated to zero bytes in size.
52081**
52082** journalMode==PERSIST
52083** The first 28 bytes of the journal file are zeroed. This invalidates
52084** the first journal header in the file, and hence the entire journal
52085** file. An invalid journal file cannot be rolled back.
52086**
52087** journalMode==DELETE
52088** The journal file is closed and deleted using sqlite3OsDelete().
52089**
52090** If the pager is running in exclusive mode, this method of finalizing
52091** the journal file is never used. Instead, if the journalMode is
52092** DELETE and the pager is in exclusive mode, the method described under
52093** journalMode==PERSIST is used instead.
52094**
52095** After the journal is finalized, the pager moves to PAGER_READER state.
52096** If running in non-exclusive rollback mode, the lock on the file is
52097** downgraded to a SHARED_LOCK.
52098**
52099** SQLITE_OK is returned if no error occurs. If an error occurs during
52100** any of the IO operations to finalize the journal file or unlock the
52101** database then the IO error code is returned to the user. If the
52102** operation to finalize the journal file fails, then the code still
52103** tries to unlock the database file if not in exclusive mode. If the
52104** unlock operation fails as well, then the first error code related
52105** to the first error encountered (the journal finalization one) is
52106** returned.
52107*/
52108static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
52109 int rc = SQLITE_OK; /* Error code from journal finalization operation */
52110 int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
52111
52112 /* Do nothing if the pager does not have an open write transaction
52113 ** or at least a RESERVED lock. This function may be called when there
52114 ** is no write-transaction active but a RESERVED or greater lock is
52115 ** held under two circumstances:
52116 **
52117 ** 1. After a successful hot-journal rollback, it is called with
52118 ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
52119 **
52120 ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
52121 ** lock switches back to locking_mode=normal and then executes a
52122 ** read-transaction, this function is called with eState==PAGER_READER
52123 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
52124 */
52125 assert( assert_pager_state(pPager) );
52126 assert( pPager->eState!=PAGER_ERROR );
52127 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
52128 return SQLITE_OK;
52129 }
52130
52131 releaseAllSavepoints(pPager);
52132 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
52133 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
52134 );
52135 if( isOpen(pPager->jfd) ){
52136 assert( !pagerUseWal(pPager) );
52137
52138 /* Finalize the journal file. */
52139 if( sqlite3JournalIsInMemory(pPager->jfd) ){
52140 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
52141 sqlite3OsClose(pPager->jfd);
52142 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
52143 if( pPager->journalOff==0 ){
52144 rc = SQLITE_OK;
52145 }else{
52146 rc = sqlite3OsTruncate(pPager->jfd, 0);
52147 if( rc==SQLITE_OK && pPager->fullSync ){
52148 /* Make sure the new file size is written into the inode right away.
52149 ** Otherwise the journal might resurrect following a power loss and
52150 ** cause the last transaction to roll back. See
52151 ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773
52152 */
52153 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
52154 }
52155 }
52156 pPager->journalOff = 0;
52157 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
52158 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
52159 ){
52160 rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile);
52161 pPager->journalOff = 0;
52162 }else{
52163 /* This branch may be executed with Pager.journalMode==MEMORY if
52164 ** a hot-journal was just rolled back. In this case the journal
52165 ** file should be closed and deleted. If this connection writes to
52166 ** the database file, it will do so using an in-memory journal.
52167 */
52168 int bDelete = !pPager->tempFile;
52169 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
52170 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
52171 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
52172 || pPager->journalMode==PAGER_JOURNALMODE_WAL
52173 );
52174 sqlite3OsClose(pPager->jfd);
52175 if( bDelete ){
52176 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
52177 }
52178 }
52179 }
52180
52181#ifdef SQLITE_CHECK_PAGES
52182 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
52183 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
52184 PgHdr *p = sqlite3PagerLookup(pPager, 1);
52185 if( p ){
52186 p->pageHash = 0;
52187 sqlite3PagerUnrefNotNull(p);
52188 }
52189 }
52190#endif
52191
52192 sqlite3BitvecDestroy(pPager->pInJournal);
52193 pPager->pInJournal = 0;
52194 pPager->nRec = 0;
52195 if( rc==SQLITE_OK ){
52196 if( MEMDB || pagerFlushOnCommit(pPager, bCommit) ){
52197 sqlite3PcacheCleanAll(pPager->pPCache);
52198 }else{
52199 sqlite3PcacheClearWritable(pPager->pPCache);
52200 }
52201 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
52202 }
52203
52204 if( pagerUseWal(pPager) ){
52205 /* Drop the WAL write-lock, if any. Also, if the connection was in
52206 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
52207 ** lock held on the database file.
52208 */
52209 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
52210 assert( rc2==SQLITE_OK );
52211 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
52212 /* This branch is taken when committing a transaction in rollback-journal
52213 ** mode if the database file on disk is larger than the database image.
52214 ** At this point the journal has been finalized and the transaction
52215 ** successfully committed, but the EXCLUSIVE lock is still held on the
52216 ** file. So it is safe to truncate the database file to its minimum
52217 ** required size. */
52218 assert( pPager->eLock==EXCLUSIVE_LOCK );
52219 rc = pager_truncate(pPager, pPager->dbSize);
52220 }
52221
52222 if( rc==SQLITE_OK && bCommit ){
52223 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
52224 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
52225 }
52226
52227 if( !pPager->exclusiveMode
52228 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
52229 ){
52230 rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
52231 pPager->changeCountDone = 0;
52232 }
52233 pPager->eState = PAGER_READER;
52234 pPager->setMaster = 0;
52235
52236 return (rc==SQLITE_OK?rc2:rc);
52237}
52238
52239/*
52240** Execute a rollback if a transaction is active and unlock the
52241** database file.
52242**
52243** If the pager has already entered the ERROR state, do not attempt
52244** the rollback at this time. Instead, pager_unlock() is called. The
52245** call to pager_unlock() will discard all in-memory pages, unlock
52246** the database file and move the pager back to OPEN state. If this
52247** means that there is a hot-journal left in the file-system, the next
52248** connection to obtain a shared lock on the pager (which may be this one)
52249** will roll it back.
52250**
52251** If the pager has not already entered the ERROR state, but an IO or
52252** malloc error occurs during a rollback, then this will itself cause
52253** the pager to enter the ERROR state. Which will be cleared by the
52254** call to pager_unlock(), as described above.
52255*/
52256static void pagerUnlockAndRollback(Pager *pPager){
52257 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
52258 assert( assert_pager_state(pPager) );
52259 if( pPager->eState>=PAGER_WRITER_LOCKED ){
52260 sqlite3BeginBenignMalloc();
52261 sqlite3PagerRollback(pPager);
52262 sqlite3EndBenignMalloc();
52263 }else if( !pPager->exclusiveMode ){
52264 assert( pPager->eState==PAGER_READER );
52265 pager_end_transaction(pPager, 0, 0);
52266 }
52267 }
52268 pager_unlock(pPager);
52269}
52270
52271/*
52272** Parameter aData must point to a buffer of pPager->pageSize bytes
52273** of data. Compute and return a checksum based ont the contents of the
52274** page of data and the current value of pPager->cksumInit.
52275**
52276** This is not a real checksum. It is really just the sum of the
52277** random initial value (pPager->cksumInit) and every 200th byte
52278** of the page data, starting with byte offset (pPager->pageSize%200).
52279** Each byte is interpreted as an 8-bit unsigned integer.
52280**
52281** Changing the formula used to compute this checksum results in an
52282** incompatible journal file format.
52283**
52284** If journal corruption occurs due to a power failure, the most likely
52285** scenario is that one end or the other of the record will be changed.
52286** It is much less likely that the two ends of the journal record will be
52287** correct and the middle be corrupt. Thus, this "checksum" scheme,
52288** though fast and simple, catches the mostly likely kind of corruption.
52289*/
52290static u32 pager_cksum(Pager *pPager, const u8 *aData){
52291 u32 cksum = pPager->cksumInit; /* Checksum value to return */
52292 int i = pPager->pageSize-200; /* Loop counter */
52293 while( i>0 ){
52294 cksum += aData[i];
52295 i -= 200;
52296 }
52297 return cksum;
52298}
52299
52300/*
52301** Report the current page size and number of reserved bytes back
52302** to the codec.
52303*/
52304#ifdef SQLITE_HAS_CODEC
52305static void pagerReportSize(Pager *pPager){
52306 if( pPager->xCodecSizeChng ){
52307 pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
52308 (int)pPager->nReserve);
52309 }
52310}
52311#else
52312# define pagerReportSize(X) /* No-op if we do not support a codec */
52313#endif
52314
52315#ifdef SQLITE_HAS_CODEC
52316/*
52317** Make sure the number of reserved bits is the same in the destination
52318** pager as it is in the source. This comes up when a VACUUM changes the
52319** number of reserved bits to the "optimal" amount.
52320*/
52321SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager *pDest, Pager *pSrc){
52322 if( pDest->nReserve!=pSrc->nReserve ){
52323 pDest->nReserve = pSrc->nReserve;
52324 pagerReportSize(pDest);
52325 }
52326}
52327#endif
52328
52329/*
52330** Read a single page from either the journal file (if isMainJrnl==1) or
52331** from the sub-journal (if isMainJrnl==0) and playback that page.
52332** The page begins at offset *pOffset into the file. The *pOffset
52333** value is increased to the start of the next page in the journal.
52334**
52335** The main rollback journal uses checksums - the statement journal does
52336** not.
52337**
52338** If the page number of the page record read from the (sub-)journal file
52339** is greater than the current value of Pager.dbSize, then playback is
52340** skipped and SQLITE_OK is returned.
52341**
52342** If pDone is not NULL, then it is a record of pages that have already
52343** been played back. If the page at *pOffset has already been played back
52344** (if the corresponding pDone bit is set) then skip the playback.
52345** Make sure the pDone bit corresponding to the *pOffset page is set
52346** prior to returning.
52347**
52348** If the page record is successfully read from the (sub-)journal file
52349** and played back, then SQLITE_OK is returned. If an IO error occurs
52350** while reading the record from the (sub-)journal file or while writing
52351** to the database file, then the IO error code is returned. If data
52352** is successfully read from the (sub-)journal file but appears to be
52353** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
52354** two circumstances:
52355**
52356** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
52357** * If the record is being rolled back from the main journal file
52358** and the checksum field does not match the record content.
52359**
52360** Neither of these two scenarios are possible during a savepoint rollback.
52361**
52362** If this is a savepoint rollback, then memory may have to be dynamically
52363** allocated by this function. If this is the case and an allocation fails,
52364** SQLITE_NOMEM is returned.
52365*/
52366static int pager_playback_one_page(
52367 Pager *pPager, /* The pager being played back */
52368 i64 *pOffset, /* Offset of record to playback */
52369 Bitvec *pDone, /* Bitvec of pages already played back */
52370 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
52371 int isSavepnt /* True for a savepoint rollback */
52372){
52373 int rc;
52374 PgHdr *pPg; /* An existing page in the cache */
52375 Pgno pgno; /* The page number of a page in journal */
52376 u32 cksum; /* Checksum used for sanity checking */
52377 char *aData; /* Temporary storage for the page */
52378 sqlite3_file *jfd; /* The file descriptor for the journal file */
52379 int isSynced; /* True if journal page is synced */
52380#ifdef SQLITE_HAS_CODEC
52381 /* The jrnlEnc flag is true if Journal pages should be passed through
52382 ** the codec. It is false for pure in-memory journals. */
52383 const int jrnlEnc = (isMainJrnl || pPager->subjInMemory==0);
52384#endif
52385
52386 assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */
52387 assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */
52388 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
52389 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
52390
52391 aData = pPager->pTmpSpace;
52392 assert( aData ); /* Temp storage must have already been allocated */
52393 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
52394
52395 /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
52396 ** or savepoint rollback done at the request of the caller) or this is
52397 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
52398 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
52399 ** only reads from the main journal, not the sub-journal.
52400 */
52401 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
52402 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
52403 );
52404 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
52405
52406 /* Read the page number and page data from the journal or sub-journal
52407 ** file. Return an error code to the caller if an IO error occurs.
52408 */
52409 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
52410 rc = read32bits(jfd, *pOffset, &pgno);
52411 if( rc!=SQLITE_OK ) return rc;
52412 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
52413 if( rc!=SQLITE_OK ) return rc;
52414 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
52415
52416 /* Sanity checking on the page. This is more important that I originally
52417 ** thought. If a power failure occurs while the journal is being written,
52418 ** it could cause invalid data to be written into the journal. We need to
52419 ** detect this invalid data (with high probability) and ignore it.
52420 */
52421 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
52422 assert( !isSavepnt );
52423 return SQLITE_DONE;
52424 }
52425 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
52426 return SQLITE_OK;
52427 }
52428 if( isMainJrnl ){
52429 rc = read32bits(jfd, (*pOffset)-4, &cksum);
52430 if( rc ) return rc;
52431 if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
52432 return SQLITE_DONE;
52433 }
52434 }
52435
52436 /* If this page has already been played back before during the current
52437 ** rollback, then don't bother to play it back again.
52438 */
52439 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
52440 return rc;
52441 }
52442
52443 /* When playing back page 1, restore the nReserve setting
52444 */
52445 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
52446 pPager->nReserve = ((u8*)aData)[20];
52447 pagerReportSize(pPager);
52448 }
52449
52450 /* If the pager is in CACHEMOD state, then there must be a copy of this
52451 ** page in the pager cache. In this case just update the pager cache,
52452 ** not the database file. The page is left marked dirty in this case.
52453 **
52454 ** An exception to the above rule: If the database is in no-sync mode
52455 ** and a page is moved during an incremental vacuum then the page may
52456 ** not be in the pager cache. Later: if a malloc() or IO error occurs
52457 ** during a Movepage() call, then the page may not be in the cache
52458 ** either. So the condition described in the above paragraph is not
52459 ** assert()able.
52460 **
52461 ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
52462 ** pager cache if it exists and the main file. The page is then marked
52463 ** not dirty. Since this code is only executed in PAGER_OPEN state for
52464 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
52465 ** if the pager is in OPEN state.
52466 **
52467 ** Ticket #1171: The statement journal might contain page content that is
52468 ** different from the page content at the start of the transaction.
52469 ** This occurs when a page is changed prior to the start of a statement
52470 ** then changed again within the statement. When rolling back such a
52471 ** statement we must not write to the original database unless we know
52472 ** for certain that original page contents are synced into the main rollback
52473 ** journal. Otherwise, a power loss might leave modified data in the
52474 ** database file without an entry in the rollback journal that can
52475 ** restore the database to its original form. Two conditions must be
52476 ** met before writing to the database files. (1) the database must be
52477 ** locked. (2) we know that the original page content is fully synced
52478 ** in the main journal either because the page is not in cache or else
52479 ** the page is marked as needSync==0.
52480 **
52481 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
52482 ** is possible to fail a statement on a database that does not yet exist.
52483 ** Do not attempt to write if database file has never been opened.
52484 */
52485 if( pagerUseWal(pPager) ){
52486 pPg = 0;
52487 }else{
52488 pPg = sqlite3PagerLookup(pPager, pgno);
52489 }
52490 assert( pPg || !MEMDB );
52491 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
52492 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
52493 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
52494 (isMainJrnl?"main-journal":"sub-journal")
52495 ));
52496 if( isMainJrnl ){
52497 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
52498 }else{
52499 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
52500 }
52501 if( isOpen(pPager->fd)
52502 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
52503 && isSynced
52504 ){
52505 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
52506 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
52507 assert( !pagerUseWal(pPager) );
52508
52509 /* Write the data read from the journal back into the database file.
52510 ** This is usually safe even for an encrypted database - as the data
52511 ** was encrypted before it was written to the journal file. The exception
52512 ** is if the data was just read from an in-memory sub-journal. In that
52513 ** case it must be encrypted here before it is copied into the database
52514 ** file. */
52515#ifdef SQLITE_HAS_CODEC
52516 if( !jrnlEnc ){
52517 CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT, aData);
52518 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
52519 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT);
52520 }else
52521#endif
52522 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
52523
52524 if( pgno>pPager->dbFileSize ){
52525 pPager->dbFileSize = pgno;
52526 }
52527 if( pPager->pBackup ){
52528#ifdef SQLITE_HAS_CODEC
52529 if( jrnlEnc ){
52530 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT);
52531 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
52532 CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT,aData);
52533 }else
52534#endif
52535 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
52536 }
52537 }else if( !isMainJrnl && pPg==0 ){
52538 /* If this is a rollback of a savepoint and data was not written to
52539 ** the database and the page is not in-memory, there is a potential
52540 ** problem. When the page is next fetched by the b-tree layer, it
52541 ** will be read from the database file, which may or may not be
52542 ** current.
52543 **
52544 ** There are a couple of different ways this can happen. All are quite
52545 ** obscure. When running in synchronous mode, this can only happen
52546 ** if the page is on the free-list at the start of the transaction, then
52547 ** populated, then moved using sqlite3PagerMovepage().
52548 **
52549 ** The solution is to add an in-memory page to the cache containing
52550 ** the data just read from the sub-journal. Mark the page as dirty
52551 ** and if the pager requires a journal-sync, then mark the page as
52552 ** requiring a journal-sync before it is written.
52553 */
52554 assert( isSavepnt );
52555 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
52556 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
52557 rc = sqlite3PagerGet(pPager, pgno, &pPg, 1);
52558 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
52559 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
52560 if( rc!=SQLITE_OK ) return rc;
52561 sqlite3PcacheMakeDirty(pPg);
52562 }
52563 if( pPg ){
52564 /* No page should ever be explicitly rolled back that is in use, except
52565 ** for page 1 which is held in use in order to keep the lock on the
52566 ** database active. However such a page may be rolled back as a result
52567 ** of an internal error resulting in an automatic call to
52568 ** sqlite3PagerRollback().
52569 */
52570 void *pData;
52571 pData = pPg->pData;
52572 memcpy(pData, (u8*)aData, pPager->pageSize);
52573 pPager->xReiniter(pPg);
52574 /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But
52575 ** that call was dangerous and had no detectable benefit since the cache
52576 ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
52577 ** has been removed. */
52578 pager_set_pagehash(pPg);
52579
52580 /* If this was page 1, then restore the value of Pager.dbFileVers.
52581 ** Do this before any decoding. */
52582 if( pgno==1 ){
52583 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
52584 }
52585
52586 /* Decode the page just read from disk */
52587#if SQLITE_HAS_CODEC
52588 if( jrnlEnc ){ CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT); }
52589#endif
52590 sqlite3PcacheRelease(pPg);
52591 }
52592 return rc;
52593}
52594
52595/*
52596** Parameter zMaster is the name of a master journal file. A single journal
52597** file that referred to the master journal file has just been rolled back.
52598** This routine checks if it is possible to delete the master journal file,
52599** and does so if it is.
52600**
52601** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
52602** available for use within this function.
52603**
52604** When a master journal file is created, it is populated with the names
52605** of all of its child journals, one after another, formatted as utf-8
52606** encoded text. The end of each child journal file is marked with a
52607** nul-terminator byte (0x00). i.e. the entire contents of a master journal
52608** file for a transaction involving two databases might be:
52609**
52610** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
52611**
52612** A master journal file may only be deleted once all of its child
52613** journals have been rolled back.
52614**
52615** This function reads the contents of the master-journal file into
52616** memory and loops through each of the child journal names. For
52617** each child journal, it checks if:
52618**
52619** * if the child journal exists, and if so
52620** * if the child journal contains a reference to master journal
52621** file zMaster
52622**
52623** If a child journal can be found that matches both of the criteria
52624** above, this function returns without doing anything. Otherwise, if
52625** no such child journal can be found, file zMaster is deleted from
52626** the file-system using sqlite3OsDelete().
52627**
52628** If an IO error within this function, an error code is returned. This
52629** function allocates memory by calling sqlite3Malloc(). If an allocation
52630** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
52631** occur, SQLITE_OK is returned.
52632**
52633** TODO: This function allocates a single block of memory to load
52634** the entire contents of the master journal file. This could be
52635** a couple of kilobytes or so - potentially larger than the page
52636** size.
52637*/
52638static int pager_delmaster(Pager *pPager, const char *zMaster){
52639 sqlite3_vfs *pVfs = pPager->pVfs;
52640 int rc; /* Return code */
52641 sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */
52642 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
52643 char *zMasterJournal = 0; /* Contents of master journal file */
52644 i64 nMasterJournal; /* Size of master journal file */
52645 char *zJournal; /* Pointer to one journal within MJ file */
52646 char *zMasterPtr; /* Space to hold MJ filename from a journal file */
52647 int nMasterPtr; /* Amount of space allocated to zMasterPtr[] */
52648
52649 /* Allocate space for both the pJournal and pMaster file descriptors.
52650 ** If successful, open the master journal file for reading.
52651 */
52652 pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
52653 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
52654 if( !pMaster ){
52655 rc = SQLITE_NOMEM_BKPT;
52656 }else{
52657 const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
52658 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
52659 }
52660 if( rc!=SQLITE_OK ) goto delmaster_out;
52661
52662 /* Load the entire master journal file into space obtained from
52663 ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain
52664 ** sufficient space (in zMasterPtr) to hold the names of master
52665 ** journal files extracted from regular rollback-journals.
52666 */
52667 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
52668 if( rc!=SQLITE_OK ) goto delmaster_out;
52669 nMasterPtr = pVfs->mxPathname+1;
52670 zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1);
52671 if( !zMasterJournal ){
52672 rc = SQLITE_NOMEM_BKPT;
52673 goto delmaster_out;
52674 }
52675 zMasterPtr = &zMasterJournal[nMasterJournal+1];
52676 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
52677 if( rc!=SQLITE_OK ) goto delmaster_out;
52678 zMasterJournal[nMasterJournal] = 0;
52679
52680 zJournal = zMasterJournal;
52681 while( (zJournal-zMasterJournal)<nMasterJournal ){
52682 int exists;
52683 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
52684 if( rc!=SQLITE_OK ){
52685 goto delmaster_out;
52686 }
52687 if( exists ){
52688 /* One of the journals pointed to by the master journal exists.
52689 ** Open it and check if it points at the master journal. If
52690 ** so, return without deleting the master journal file.
52691 */
52692 int c;
52693 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
52694 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
52695 if( rc!=SQLITE_OK ){
52696 goto delmaster_out;
52697 }
52698
52699 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
52700 sqlite3OsClose(pJournal);
52701 if( rc!=SQLITE_OK ){
52702 goto delmaster_out;
52703 }
52704
52705 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
52706 if( c ){
52707 /* We have a match. Do not delete the master journal file. */
52708 goto delmaster_out;
52709 }
52710 }
52711 zJournal += (sqlite3Strlen30(zJournal)+1);
52712 }
52713
52714 sqlite3OsClose(pMaster);
52715 rc = sqlite3OsDelete(pVfs, zMaster, 0);
52716
52717delmaster_out:
52718 sqlite3_free(zMasterJournal);
52719 if( pMaster ){
52720 sqlite3OsClose(pMaster);
52721 assert( !isOpen(pJournal) );
52722 sqlite3_free(pMaster);
52723 }
52724 return rc;
52725}
52726
52727
52728/*
52729** This function is used to change the actual size of the database
52730** file in the file-system. This only happens when committing a transaction,
52731** or rolling back a transaction (including rolling back a hot-journal).
52732**
52733** If the main database file is not open, or the pager is not in either
52734** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
52735** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
52736** If the file on disk is currently larger than nPage pages, then use the VFS
52737** xTruncate() method to truncate it.
52738**
52739** Or, it might be the case that the file on disk is smaller than
52740** nPage pages. Some operating system implementations can get confused if
52741** you try to truncate a file to some size that is larger than it
52742** currently is, so detect this case and write a single zero byte to
52743** the end of the new file instead.
52744**
52745** If successful, return SQLITE_OK. If an IO error occurs while modifying
52746** the database file, return the error code to the caller.
52747*/
52748static int pager_truncate(Pager *pPager, Pgno nPage){
52749 int rc = SQLITE_OK;
52750 assert( pPager->eState!=PAGER_ERROR );
52751 assert( pPager->eState!=PAGER_READER );
52752
52753 if( isOpen(pPager->fd)
52754 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
52755 ){
52756 i64 currentSize, newSize;
52757 int szPage = pPager->pageSize;
52758 assert( pPager->eLock==EXCLUSIVE_LOCK );
52759 /* TODO: Is it safe to use Pager.dbFileSize here? */
52760 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
52761 newSize = szPage*(i64)nPage;
52762 if( rc==SQLITE_OK && currentSize!=newSize ){
52763 if( currentSize>newSize ){
52764 rc = sqlite3OsTruncate(pPager->fd, newSize);
52765 }else if( (currentSize+szPage)<=newSize ){
52766 char *pTmp = pPager->pTmpSpace;
52767 memset(pTmp, 0, szPage);
52768 testcase( (newSize-szPage) == currentSize );
52769 testcase( (newSize-szPage) > currentSize );
52770 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
52771 }
52772 if( rc==SQLITE_OK ){
52773 pPager->dbFileSize = nPage;
52774 }
52775 }
52776 }
52777 return rc;
52778}
52779
52780/*
52781** Return a sanitized version of the sector-size of OS file pFile. The
52782** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
52783*/
52784SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
52785 int iRet = sqlite3OsSectorSize(pFile);
52786 if( iRet<32 ){
52787 iRet = 512;
52788 }else if( iRet>MAX_SECTOR_SIZE ){
52789 assert( MAX_SECTOR_SIZE>=512 );
52790 iRet = MAX_SECTOR_SIZE;
52791 }
52792 return iRet;
52793}
52794
52795/*
52796** Set the value of the Pager.sectorSize variable for the given
52797** pager based on the value returned by the xSectorSize method
52798** of the open database file. The sector size will be used
52799** to determine the size and alignment of journal header and
52800** master journal pointers within created journal files.
52801**
52802** For temporary files the effective sector size is always 512 bytes.
52803**
52804** Otherwise, for non-temporary files, the effective sector size is
52805** the value returned by the xSectorSize() method rounded up to 32 if
52806** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
52807** is greater than MAX_SECTOR_SIZE.
52808**
52809** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
52810** the effective sector size to its minimum value (512). The purpose of
52811** pPager->sectorSize is to define the "blast radius" of bytes that
52812** might change if a crash occurs while writing to a single byte in
52813** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero
52814** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
52815** size. For backwards compatibility of the rollback journal file format,
52816** we cannot reduce the effective sector size below 512.
52817*/
52818static void setSectorSize(Pager *pPager){
52819 assert( isOpen(pPager->fd) || pPager->tempFile );
52820
52821 if( pPager->tempFile
52822 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
52823 SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
52824 ){
52825 /* Sector size doesn't matter for temporary files. Also, the file
52826 ** may not have been opened yet, in which case the OsSectorSize()
52827 ** call will segfault. */
52828 pPager->sectorSize = 512;
52829 }else{
52830 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
52831 }
52832}
52833
52834/*
52835** Playback the journal and thus restore the database file to
52836** the state it was in before we started making changes.
52837**
52838** The journal file format is as follows:
52839**
52840** (1) 8 byte prefix. A copy of aJournalMagic[].
52841** (2) 4 byte big-endian integer which is the number of valid page records
52842** in the journal. If this value is 0xffffffff, then compute the
52843** number of page records from the journal size.
52844** (3) 4 byte big-endian integer which is the initial value for the
52845** sanity checksum.
52846** (4) 4 byte integer which is the number of pages to truncate the
52847** database to during a rollback.
52848** (5) 4 byte big-endian integer which is the sector size. The header
52849** is this many bytes in size.
52850** (6) 4 byte big-endian integer which is the page size.
52851** (7) zero padding out to the next sector size.
52852** (8) Zero or more pages instances, each as follows:
52853** + 4 byte page number.
52854** + pPager->pageSize bytes of data.
52855** + 4 byte checksum
52856**
52857** When we speak of the journal header, we mean the first 7 items above.
52858** Each entry in the journal is an instance of the 8th item.
52859**
52860** Call the value from the second bullet "nRec". nRec is the number of
52861** valid page entries in the journal. In most cases, you can compute the
52862** value of nRec from the size of the journal file. But if a power
52863** failure occurred while the journal was being written, it could be the
52864** case that the size of the journal file had already been increased but
52865** the extra entries had not yet made it safely to disk. In such a case,
52866** the value of nRec computed from the file size would be too large. For
52867** that reason, we always use the nRec value in the header.
52868**
52869** If the nRec value is 0xffffffff it means that nRec should be computed
52870** from the file size. This value is used when the user selects the
52871** no-sync option for the journal. A power failure could lead to corruption
52872** in this case. But for things like temporary table (which will be
52873** deleted when the power is restored) we don't care.
52874**
52875** If the file opened as the journal file is not a well-formed
52876** journal file then all pages up to the first corrupted page are rolled
52877** back (or no pages if the journal header is corrupted). The journal file
52878** is then deleted and SQLITE_OK returned, just as if no corruption had
52879** been encountered.
52880**
52881** If an I/O or malloc() error occurs, the journal-file is not deleted
52882** and an error code is returned.
52883**
52884** The isHot parameter indicates that we are trying to rollback a journal
52885** that might be a hot journal. Or, it could be that the journal is
52886** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
52887** If the journal really is hot, reset the pager cache prior rolling
52888** back any content. If the journal is merely persistent, no reset is
52889** needed.
52890*/
52891static int pager_playback(Pager *pPager, int isHot){
52892 sqlite3_vfs *pVfs = pPager->pVfs;
52893 i64 szJ; /* Size of the journal file in bytes */
52894 u32 nRec; /* Number of Records in the journal */
52895 u32 u; /* Unsigned loop counter */
52896 Pgno mxPg = 0; /* Size of the original file in pages */
52897 int rc; /* Result code of a subroutine */
52898 int res = 1; /* Value returned by sqlite3OsAccess() */
52899 char *zMaster = 0; /* Name of master journal file if any */
52900 int needPagerReset; /* True to reset page prior to first page rollback */
52901 int nPlayback = 0; /* Total number of pages restored from journal */
52902 u32 savedPageSize = pPager->pageSize;
52903
52904 /* Figure out how many records are in the journal. Abort early if
52905 ** the journal is empty.
52906 */
52907 assert( isOpen(pPager->jfd) );
52908 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
52909 if( rc!=SQLITE_OK ){
52910 goto end_playback;
52911 }
52912
52913 /* Read the master journal name from the journal, if it is present.
52914 ** If a master journal file name is specified, but the file is not
52915 ** present on disk, then the journal is not hot and does not need to be
52916 ** played back.
52917 **
52918 ** TODO: Technically the following is an error because it assumes that
52919 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
52920 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
52921 ** mxPathname is 512, which is the same as the minimum allowable value
52922 ** for pageSize.
52923 */
52924 zMaster = pPager->pTmpSpace;
52925 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
52926 if( rc==SQLITE_OK && zMaster[0] ){
52927 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
52928 }
52929 zMaster = 0;
52930 if( rc!=SQLITE_OK || !res ){
52931 goto end_playback;
52932 }
52933 pPager->journalOff = 0;
52934 needPagerReset = isHot;
52935
52936 /* This loop terminates either when a readJournalHdr() or
52937 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
52938 ** occurs.
52939 */
52940 while( 1 ){
52941 /* Read the next journal header from the journal file. If there are
52942 ** not enough bytes left in the journal file for a complete header, or
52943 ** it is corrupted, then a process must have failed while writing it.
52944 ** This indicates nothing more needs to be rolled back.
52945 */
52946 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
52947 if( rc!=SQLITE_OK ){
52948 if( rc==SQLITE_DONE ){
52949 rc = SQLITE_OK;
52950 }
52951 goto end_playback;
52952 }
52953
52954 /* If nRec is 0xffffffff, then this journal was created by a process
52955 ** working in no-sync mode. This means that the rest of the journal
52956 ** file consists of pages, there are no more journal headers. Compute
52957 ** the value of nRec based on this assumption.
52958 */
52959 if( nRec==0xffffffff ){
52960 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
52961 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
52962 }
52963
52964 /* If nRec is 0 and this rollback is of a transaction created by this
52965 ** process and if this is the final header in the journal, then it means
52966 ** that this part of the journal was being filled but has not yet been
52967 ** synced to disk. Compute the number of pages based on the remaining
52968 ** size of the file.
52969 **
52970 ** The third term of the test was added to fix ticket #2565.
52971 ** When rolling back a hot journal, nRec==0 always means that the next
52972 ** chunk of the journal contains zero pages to be rolled back. But
52973 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
52974 ** the journal, it means that the journal might contain additional
52975 ** pages that need to be rolled back and that the number of pages
52976 ** should be computed based on the journal file size.
52977 */
52978 if( nRec==0 && !isHot &&
52979 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
52980 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
52981 }
52982
52983 /* If this is the first header read from the journal, truncate the
52984 ** database file back to its original size.
52985 */
52986 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
52987 rc = pager_truncate(pPager, mxPg);
52988 if( rc!=SQLITE_OK ){
52989 goto end_playback;
52990 }
52991 pPager->dbSize = mxPg;
52992 }
52993
52994 /* Copy original pages out of the journal and back into the
52995 ** database file and/or page cache.
52996 */
52997 for(u=0; u<nRec; u++){
52998 if( needPagerReset ){
52999 pager_reset(pPager);
53000 needPagerReset = 0;
53001 }
53002 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
53003 if( rc==SQLITE_OK ){
53004 nPlayback++;
53005 }else{
53006 if( rc==SQLITE_DONE ){
53007 pPager->journalOff = szJ;
53008 break;
53009 }else if( rc==SQLITE_IOERR_SHORT_READ ){
53010 /* If the journal has been truncated, simply stop reading and
53011 ** processing the journal. This might happen if the journal was
53012 ** not completely written and synced prior to a crash. In that
53013 ** case, the database should have never been written in the
53014 ** first place so it is OK to simply abandon the rollback. */
53015 rc = SQLITE_OK;
53016 goto end_playback;
53017 }else{
53018 /* If we are unable to rollback, quit and return the error
53019 ** code. This will cause the pager to enter the error state
53020 ** so that no further harm will be done. Perhaps the next
53021 ** process to come along will be able to rollback the database.
53022 */
53023 goto end_playback;
53024 }
53025 }
53026 }
53027 }
53028 /*NOTREACHED*/
53029 assert( 0 );
53030
53031end_playback:
53032 if( rc==SQLITE_OK ){
53033 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
53034 }
53035 /* Following a rollback, the database file should be back in its original
53036 ** state prior to the start of the transaction, so invoke the
53037 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
53038 ** assertion that the transaction counter was modified.
53039 */
53040#ifdef SQLITE_DEBUG
53041 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
53042#endif
53043
53044 /* If this playback is happening automatically as a result of an IO or
53045 ** malloc error that occurred after the change-counter was updated but
53046 ** before the transaction was committed, then the change-counter
53047 ** modification may just have been reverted. If this happens in exclusive
53048 ** mode, then subsequent transactions performed by the connection will not
53049 ** update the change-counter at all. This may lead to cache inconsistency
53050 ** problems for other processes at some point in the future. So, just
53051 ** in case this has happened, clear the changeCountDone flag now.
53052 */
53053 pPager->changeCountDone = pPager->tempFile;
53054
53055 if( rc==SQLITE_OK ){
53056 zMaster = pPager->pTmpSpace;
53057 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
53058 testcase( rc!=SQLITE_OK );
53059 }
53060 if( rc==SQLITE_OK
53061 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
53062 ){
53063 rc = sqlite3PagerSync(pPager, 0);
53064 }
53065 if( rc==SQLITE_OK ){
53066 rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
53067 testcase( rc!=SQLITE_OK );
53068 }
53069 if( rc==SQLITE_OK && zMaster[0] && res ){
53070 /* If there was a master journal and this routine will return success,
53071 ** see if it is possible to delete the master journal.
53072 */
53073 rc = pager_delmaster(pPager, zMaster);
53074 testcase( rc!=SQLITE_OK );
53075 }
53076 if( isHot && nPlayback ){
53077 sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s",
53078 nPlayback, pPager->zJournal);
53079 }
53080
53081 /* The Pager.sectorSize variable may have been updated while rolling
53082 ** back a journal created by a process with a different sector size
53083 ** value. Reset it to the correct value for this process.
53084 */
53085 setSectorSize(pPager);
53086 return rc;
53087}
53088
53089
53090/*
53091** Read the content for page pPg out of the database file (or out of
53092** the WAL if that is where the most recent copy if found) into
53093** pPg->pData. A shared lock or greater must be held on the database
53094** file before this function is called.
53095**
53096** If page 1 is read, then the value of Pager.dbFileVers[] is set to
53097** the value read from the database file.
53098**
53099** If an IO error occurs, then the IO error is returned to the caller.
53100** Otherwise, SQLITE_OK is returned.
53101*/
53102static int readDbPage(PgHdr *pPg){
53103 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
53104 int rc = SQLITE_OK; /* Return code */
53105
53106#ifndef SQLITE_OMIT_WAL
53107 u32 iFrame = 0; /* Frame of WAL containing pgno */
53108
53109 assert( pPager->eState>=PAGER_READER && !MEMDB );
53110 assert( isOpen(pPager->fd) );
53111
53112 if( pagerUseWal(pPager) ){
53113 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
53114 if( rc ) return rc;
53115 }
53116 if( iFrame ){
53117 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
53118 }else
53119#endif
53120 {
53121 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
53122 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
53123 if( rc==SQLITE_IOERR_SHORT_READ ){
53124 rc = SQLITE_OK;
53125 }
53126 }
53127
53128 if( pPg->pgno==1 ){
53129 if( rc ){
53130 /* If the read is unsuccessful, set the dbFileVers[] to something
53131 ** that will never be a valid file version. dbFileVers[] is a copy
53132 ** of bytes 24..39 of the database. Bytes 28..31 should always be
53133 ** zero or the size of the database in page. Bytes 32..35 and 35..39
53134 ** should be page numbers which are never 0xffffffff. So filling
53135 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
53136 **
53137 ** For an encrypted database, the situation is more complex: bytes
53138 ** 24..39 of the database are white noise. But the probability of
53139 ** white noise equaling 16 bytes of 0xff is vanishingly small so
53140 ** we should still be ok.
53141 */
53142 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
53143 }else{
53144 u8 *dbFileVers = &((u8*)pPg->pData)[24];
53145 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
53146 }
53147 }
53148 CODEC1(pPager, pPg->pData, pPg->pgno, 3, rc = SQLITE_NOMEM_BKPT);
53149
53150 PAGER_INCR(sqlite3_pager_readdb_count);
53151 PAGER_INCR(pPager->nRead);
53152 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
53153 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
53154 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
53155
53156 return rc;
53157}
53158
53159/*
53160** Update the value of the change-counter at offsets 24 and 92 in
53161** the header and the sqlite version number at offset 96.
53162**
53163** This is an unconditional update. See also the pager_incr_changecounter()
53164** routine which only updates the change-counter if the update is actually
53165** needed, as determined by the pPager->changeCountDone state variable.
53166*/
53167static void pager_write_changecounter(PgHdr *pPg){
53168 u32 change_counter;
53169
53170 /* Increment the value just read and write it back to byte 24. */
53171 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
53172 put32bits(((char*)pPg->pData)+24, change_counter);
53173
53174 /* Also store the SQLite version number in bytes 96..99 and in
53175 ** bytes 92..95 store the change counter for which the version number
53176 ** is valid. */
53177 put32bits(((char*)pPg->pData)+92, change_counter);
53178 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
53179}
53180
53181#ifndef SQLITE_OMIT_WAL
53182/*
53183** This function is invoked once for each page that has already been
53184** written into the log file when a WAL transaction is rolled back.
53185** Parameter iPg is the page number of said page. The pCtx argument
53186** is actually a pointer to the Pager structure.
53187**
53188** If page iPg is present in the cache, and has no outstanding references,
53189** it is discarded. Otherwise, if there are one or more outstanding
53190** references, the page content is reloaded from the database. If the
53191** attempt to reload content from the database is required and fails,
53192** return an SQLite error code. Otherwise, SQLITE_OK.
53193*/
53194static int pagerUndoCallback(void *pCtx, Pgno iPg){
53195 int rc = SQLITE_OK;
53196 Pager *pPager = (Pager *)pCtx;
53197 PgHdr *pPg;
53198
53199 assert( pagerUseWal(pPager) );
53200 pPg = sqlite3PagerLookup(pPager, iPg);
53201 if( pPg ){
53202 if( sqlite3PcachePageRefcount(pPg)==1 ){
53203 sqlite3PcacheDrop(pPg);
53204 }else{
53205 rc = readDbPage(pPg);
53206 if( rc==SQLITE_OK ){
53207 pPager->xReiniter(pPg);
53208 }
53209 sqlite3PagerUnrefNotNull(pPg);
53210 }
53211 }
53212
53213 /* Normally, if a transaction is rolled back, any backup processes are
53214 ** updated as data is copied out of the rollback journal and into the
53215 ** database. This is not generally possible with a WAL database, as
53216 ** rollback involves simply truncating the log file. Therefore, if one
53217 ** or more frames have already been written to the log (and therefore
53218 ** also copied into the backup databases) as part of this transaction,
53219 ** the backups must be restarted.
53220 */
53221 sqlite3BackupRestart(pPager->pBackup);
53222
53223 return rc;
53224}
53225
53226/*
53227** This function is called to rollback a transaction on a WAL database.
53228*/
53229static int pagerRollbackWal(Pager *pPager){
53230 int rc; /* Return Code */
53231 PgHdr *pList; /* List of dirty pages to revert */
53232
53233 /* For all pages in the cache that are currently dirty or have already
53234 ** been written (but not committed) to the log file, do one of the
53235 ** following:
53236 **
53237 ** + Discard the cached page (if refcount==0), or
53238 ** + Reload page content from the database (if refcount>0).
53239 */
53240 pPager->dbSize = pPager->dbOrigSize;
53241 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
53242 pList = sqlite3PcacheDirtyList(pPager->pPCache);
53243 while( pList && rc==SQLITE_OK ){
53244 PgHdr *pNext = pList->pDirty;
53245 rc = pagerUndoCallback((void *)pPager, pList->pgno);
53246 pList = pNext;
53247 }
53248
53249 return rc;
53250}
53251
53252/*
53253** This function is a wrapper around sqlite3WalFrames(). As well as logging
53254** the contents of the list of pages headed by pList (connected by pDirty),
53255** this function notifies any active backup processes that the pages have
53256** changed.
53257**
53258** The list of pages passed into this routine is always sorted by page number.
53259** Hence, if page 1 appears anywhere on the list, it will be the first page.
53260*/
53261static int pagerWalFrames(
53262 Pager *pPager, /* Pager object */
53263 PgHdr *pList, /* List of frames to log */
53264 Pgno nTruncate, /* Database size after this commit */
53265 int isCommit /* True if this is a commit */
53266){
53267 int rc; /* Return code */
53268 int nList; /* Number of pages in pList */
53269 PgHdr *p; /* For looping over pages */
53270
53271 assert( pPager->pWal );
53272 assert( pList );
53273#ifdef SQLITE_DEBUG
53274 /* Verify that the page list is in accending order */
53275 for(p=pList; p && p->pDirty; p=p->pDirty){
53276 assert( p->pgno < p->pDirty->pgno );
53277 }
53278#endif
53279
53280 assert( pList->pDirty==0 || isCommit );
53281 if( isCommit ){
53282 /* If a WAL transaction is being committed, there is no point in writing
53283 ** any pages with page numbers greater than nTruncate into the WAL file.
53284 ** They will never be read by any client. So remove them from the pDirty
53285 ** list here. */
53286 PgHdr **ppNext = &pList;
53287 nList = 0;
53288 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
53289 if( p->pgno<=nTruncate ){
53290 ppNext = &p->pDirty;
53291 nList++;
53292 }
53293 }
53294 assert( pList );
53295 }else{
53296 nList = 1;
53297 }
53298 pPager->aStat[PAGER_STAT_WRITE] += nList;
53299
53300 if( pList->pgno==1 ) pager_write_changecounter(pList);
53301 rc = sqlite3WalFrames(pPager->pWal,
53302 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
53303 );
53304 if( rc==SQLITE_OK && pPager->pBackup ){
53305 for(p=pList; p; p=p->pDirty){
53306 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
53307 }
53308 }
53309
53310#ifdef SQLITE_CHECK_PAGES
53311 pList = sqlite3PcacheDirtyList(pPager->pPCache);
53312 for(p=pList; p; p=p->pDirty){
53313 pager_set_pagehash(p);
53314 }
53315#endif
53316
53317 return rc;
53318}
53319
53320/*
53321** Begin a read transaction on the WAL.
53322**
53323** This routine used to be called "pagerOpenSnapshot()" because it essentially
53324** makes a snapshot of the database at the current point in time and preserves
53325** that snapshot for use by the reader in spite of concurrently changes by
53326** other writers or checkpointers.
53327*/
53328static int pagerBeginReadTransaction(Pager *pPager){
53329 int rc; /* Return code */
53330 int changed = 0; /* True if cache must be reset */
53331
53332 assert( pagerUseWal(pPager) );
53333 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
53334
53335 /* sqlite3WalEndReadTransaction() was not called for the previous
53336 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
53337 ** are in locking_mode=NORMAL and EndRead() was previously called,
53338 ** the duplicate call is harmless.
53339 */
53340 sqlite3WalEndReadTransaction(pPager->pWal);
53341
53342 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
53343 if( rc!=SQLITE_OK || changed ){
53344 pager_reset(pPager);
53345 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
53346 }
53347
53348 return rc;
53349}
53350#endif
53351
53352/*
53353** This function is called as part of the transition from PAGER_OPEN
53354** to PAGER_READER state to determine the size of the database file
53355** in pages (assuming the page size currently stored in Pager.pageSize).
53356**
53357** If no error occurs, SQLITE_OK is returned and the size of the database
53358** in pages is stored in *pnPage. Otherwise, an error code (perhaps
53359** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
53360*/
53361static int pagerPagecount(Pager *pPager, Pgno *pnPage){
53362 Pgno nPage; /* Value to return via *pnPage */
53363
53364 /* Query the WAL sub-system for the database size. The WalDbsize()
53365 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
53366 ** if the database size is not available. The database size is not
53367 ** available from the WAL sub-system if the log file is empty or
53368 ** contains no valid committed transactions.
53369 */
53370 assert( pPager->eState==PAGER_OPEN );
53371 assert( pPager->eLock>=SHARED_LOCK );
53372 assert( isOpen(pPager->fd) );
53373 assert( pPager->tempFile==0 );
53374 nPage = sqlite3WalDbsize(pPager->pWal);
53375
53376 /* If the number of pages in the database is not available from the
53377 ** WAL sub-system, determine the page count based on the size of
53378 ** the database file. If the size of the database file is not an
53379 ** integer multiple of the page-size, round up the result.
53380 */
53381 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
53382 i64 n = 0; /* Size of db file in bytes */
53383 int rc = sqlite3OsFileSize(pPager->fd, &n);
53384 if( rc!=SQLITE_OK ){
53385 return rc;
53386 }
53387 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
53388 }
53389
53390 /* If the current number of pages in the file is greater than the
53391 ** configured maximum pager number, increase the allowed limit so
53392 ** that the file can be read.
53393 */
53394 if( nPage>pPager->mxPgno ){
53395 pPager->mxPgno = (Pgno)nPage;
53396 }
53397
53398 *pnPage = nPage;
53399 return SQLITE_OK;
53400}
53401
53402#ifndef SQLITE_OMIT_WAL
53403/*
53404** Check if the *-wal file that corresponds to the database opened by pPager
53405** exists if the database is not empy, or verify that the *-wal file does
53406** not exist (by deleting it) if the database file is empty.
53407**
53408** If the database is not empty and the *-wal file exists, open the pager
53409** in WAL mode. If the database is empty or if no *-wal file exists and
53410** if no error occurs, make sure Pager.journalMode is not set to
53411** PAGER_JOURNALMODE_WAL.
53412**
53413** Return SQLITE_OK or an error code.
53414**
53415** The caller must hold a SHARED lock on the database file to call this
53416** function. Because an EXCLUSIVE lock on the db file is required to delete
53417** a WAL on a none-empty database, this ensures there is no race condition
53418** between the xAccess() below and an xDelete() being executed by some
53419** other connection.
53420*/
53421static int pagerOpenWalIfPresent(Pager *pPager){
53422 int rc = SQLITE_OK;
53423 assert( pPager->eState==PAGER_OPEN );
53424 assert( pPager->eLock>=SHARED_LOCK );
53425
53426 if( !pPager->tempFile ){
53427 int isWal; /* True if WAL file exists */
53428 rc = sqlite3OsAccess(
53429 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
53430 );
53431 if( rc==SQLITE_OK ){
53432 if( isWal ){
53433 Pgno nPage; /* Size of the database file */
53434
53435 rc = pagerPagecount(pPager, &nPage);
53436 if( rc ) return rc;
53437 if( nPage==0 ){
53438 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
53439 }else{
53440 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
53441 rc = sqlite3PagerOpenWal(pPager, 0);
53442 }
53443 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
53444 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
53445 }
53446 }
53447 }
53448 return rc;
53449}
53450#endif
53451
53452/*
53453** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
53454** the entire master journal file. The case pSavepoint==NULL occurs when
53455** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
53456** savepoint.
53457**
53458** When pSavepoint is not NULL (meaning a non-transaction savepoint is
53459** being rolled back), then the rollback consists of up to three stages,
53460** performed in the order specified:
53461**
53462** * Pages are played back from the main journal starting at byte
53463** offset PagerSavepoint.iOffset and continuing to
53464** PagerSavepoint.iHdrOffset, or to the end of the main journal
53465** file if PagerSavepoint.iHdrOffset is zero.
53466**
53467** * If PagerSavepoint.iHdrOffset is not zero, then pages are played
53468** back starting from the journal header immediately following
53469** PagerSavepoint.iHdrOffset to the end of the main journal file.
53470**
53471** * Pages are then played back from the sub-journal file, starting
53472** with the PagerSavepoint.iSubRec and continuing to the end of
53473** the journal file.
53474**
53475** Throughout the rollback process, each time a page is rolled back, the
53476** corresponding bit is set in a bitvec structure (variable pDone in the
53477** implementation below). This is used to ensure that a page is only
53478** rolled back the first time it is encountered in either journal.
53479**
53480** If pSavepoint is NULL, then pages are only played back from the main
53481** journal file. There is no need for a bitvec in this case.
53482**
53483** In either case, before playback commences the Pager.dbSize variable
53484** is reset to the value that it held at the start of the savepoint
53485** (or transaction). No page with a page-number greater than this value
53486** is played back. If one is encountered it is simply skipped.
53487*/
53488static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
53489 i64 szJ; /* Effective size of the main journal */
53490 i64 iHdrOff; /* End of first segment of main-journal records */
53491 int rc = SQLITE_OK; /* Return code */
53492 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
53493
53494 assert( pPager->eState!=PAGER_ERROR );
53495 assert( pPager->eState>=PAGER_WRITER_LOCKED );
53496
53497 /* Allocate a bitvec to use to store the set of pages rolled back */
53498 if( pSavepoint ){
53499 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
53500 if( !pDone ){
53501 return SQLITE_NOMEM_BKPT;
53502 }
53503 }
53504
53505 /* Set the database size back to the value it was before the savepoint
53506 ** being reverted was opened.
53507 */
53508 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
53509 pPager->changeCountDone = pPager->tempFile;
53510
53511 if( !pSavepoint && pagerUseWal(pPager) ){
53512 return pagerRollbackWal(pPager);
53513 }
53514
53515 /* Use pPager->journalOff as the effective size of the main rollback
53516 ** journal. The actual file might be larger than this in
53517 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
53518 ** past pPager->journalOff is off-limits to us.
53519 */
53520 szJ = pPager->journalOff;
53521 assert( pagerUseWal(pPager)==0 || szJ==0 );
53522
53523 /* Begin by rolling back records from the main journal starting at
53524 ** PagerSavepoint.iOffset and continuing to the next journal header.
53525 ** There might be records in the main journal that have a page number
53526 ** greater than the current database size (pPager->dbSize) but those
53527 ** will be skipped automatically. Pages are added to pDone as they
53528 ** are played back.
53529 */
53530 if( pSavepoint && !pagerUseWal(pPager) ){
53531 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
53532 pPager->journalOff = pSavepoint->iOffset;
53533 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
53534 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
53535 }
53536 assert( rc!=SQLITE_DONE );
53537 }else{
53538 pPager->journalOff = 0;
53539 }
53540
53541 /* Continue rolling back records out of the main journal starting at
53542 ** the first journal header seen and continuing until the effective end
53543 ** of the main journal file. Continue to skip out-of-range pages and
53544 ** continue adding pages rolled back to pDone.
53545 */
53546 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
53547 u32 ii; /* Loop counter */
53548 u32 nJRec = 0; /* Number of Journal Records */
53549 u32 dummy;
53550 rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
53551 assert( rc!=SQLITE_DONE );
53552
53553 /*
53554 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
53555 ** test is related to ticket #2565. See the discussion in the
53556 ** pager_playback() function for additional information.
53557 */
53558 if( nJRec==0
53559 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
53560 ){
53561 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
53562 }
53563 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
53564 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
53565 }
53566 assert( rc!=SQLITE_DONE );
53567 }
53568 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
53569
53570 /* Finally, rollback pages from the sub-journal. Page that were
53571 ** previously rolled back out of the main journal (and are hence in pDone)
53572 ** will be skipped. Out-of-range pages are also skipped.
53573 */
53574 if( pSavepoint ){
53575 u32 ii; /* Loop counter */
53576 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
53577
53578 if( pagerUseWal(pPager) ){
53579 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
53580 }
53581 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
53582 assert( offset==(i64)ii*(4+pPager->pageSize) );
53583 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
53584 }
53585 assert( rc!=SQLITE_DONE );
53586 }
53587
53588 sqlite3BitvecDestroy(pDone);
53589 if( rc==SQLITE_OK ){
53590 pPager->journalOff = szJ;
53591 }
53592
53593 return rc;
53594}
53595
53596/*
53597** Change the maximum number of in-memory pages that are allowed
53598** before attempting to recycle clean and unused pages.
53599*/
53600SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
53601 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
53602}
53603
53604/*
53605** Change the maximum number of in-memory pages that are allowed
53606** before attempting to spill pages to journal.
53607*/
53608SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){
53609 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
53610}
53611
53612/*
53613** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
53614*/
53615static void pagerFixMaplimit(Pager *pPager){
53616#if SQLITE_MAX_MMAP_SIZE>0
53617 sqlite3_file *fd = pPager->fd;
53618 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
53619 sqlite3_int64 sz;
53620 sz = pPager->szMmap;
53621 pPager->bUseFetch = (sz>0);
53622 setGetterMethod(pPager);
53623 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
53624 }
53625#endif
53626}
53627
53628/*
53629** Change the maximum size of any memory mapping made of the database file.
53630*/
53631SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
53632 pPager->szMmap = szMmap;
53633 pagerFixMaplimit(pPager);
53634}
53635
53636/*
53637** Free as much memory as possible from the pager.
53638*/
53639SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
53640 sqlite3PcacheShrink(pPager->pPCache);
53641}
53642
53643/*
53644** Adjust settings of the pager to those specified in the pgFlags parameter.
53645**
53646** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
53647** of the database to damage due to OS crashes or power failures by
53648** changing the number of syncs()s when writing the journals.
53649** There are four levels:
53650**
53651** OFF sqlite3OsSync() is never called. This is the default
53652** for temporary and transient files.
53653**
53654** NORMAL The journal is synced once before writes begin on the
53655** database. This is normally adequate protection, but
53656** it is theoretically possible, though very unlikely,
53657** that an inopertune power failure could leave the journal
53658** in a state which would cause damage to the database
53659** when it is rolled back.
53660**
53661** FULL The journal is synced twice before writes begin on the
53662** database (with some additional information - the nRec field
53663** of the journal header - being written in between the two
53664** syncs). If we assume that writing a
53665** single disk sector is atomic, then this mode provides
53666** assurance that the journal will not be corrupted to the
53667** point of causing damage to the database during rollback.
53668**
53669** EXTRA This is like FULL except that is also syncs the directory
53670** that contains the rollback journal after the rollback
53671** journal is unlinked.
53672**
53673** The above is for a rollback-journal mode. For WAL mode, OFF continues
53674** to mean that no syncs ever occur. NORMAL means that the WAL is synced
53675** prior to the start of checkpoint and that the database file is synced
53676** at the conclusion of the checkpoint if the entire content of the WAL
53677** was written back into the database. But no sync operations occur for
53678** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL
53679** file is synced following each commit operation, in addition to the
53680** syncs associated with NORMAL. There is no difference between FULL
53681** and EXTRA for WAL mode.
53682**
53683** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The
53684** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
53685** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an
53686** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL
53687** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the
53688** synchronous=FULL versus synchronous=NORMAL setting determines when
53689** the xSync primitive is called and is relevant to all platforms.
53690**
53691** Numeric values associated with these states are OFF==1, NORMAL=2,
53692** and FULL=3.
53693*/
53694#ifndef SQLITE_OMIT_PAGER_PRAGMAS
53695SQLITE_PRIVATE void sqlite3PagerSetFlags(
53696 Pager *pPager, /* The pager to set safety level for */
53697 unsigned pgFlags /* Various flags */
53698){
53699 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
53700 if( pPager->tempFile ){
53701 pPager->noSync = 1;
53702 pPager->fullSync = 0;
53703 pPager->extraSync = 0;
53704 }else{
53705 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
53706 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
53707 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
53708 }
53709 if( pPager->noSync ){
53710 pPager->syncFlags = 0;
53711 }else if( pgFlags & PAGER_FULLFSYNC ){
53712 pPager->syncFlags = SQLITE_SYNC_FULL;
53713 }else{
53714 pPager->syncFlags = SQLITE_SYNC_NORMAL;
53715 }
53716 pPager->walSyncFlags = (pPager->syncFlags<<2);
53717 if( pPager->fullSync ){
53718 pPager->walSyncFlags |= pPager->syncFlags;
53719 }
53720 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
53721 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
53722 }
53723 if( pgFlags & PAGER_CACHESPILL ){
53724 pPager->doNotSpill &= ~SPILLFLAG_OFF;
53725 }else{
53726 pPager->doNotSpill |= SPILLFLAG_OFF;
53727 }
53728}
53729#endif
53730
53731/*
53732** The following global variable is incremented whenever the library
53733** attempts to open a temporary file. This information is used for
53734** testing and analysis only.
53735*/
53736#ifdef SQLITE_TEST
53737SQLITE_API int sqlite3_opentemp_count = 0;
53738#endif
53739
53740/*
53741** Open a temporary file.
53742**
53743** Write the file descriptor into *pFile. Return SQLITE_OK on success
53744** or some other error code if we fail. The OS will automatically
53745** delete the temporary file when it is closed.
53746**
53747** The flags passed to the VFS layer xOpen() call are those specified
53748** by parameter vfsFlags ORed with the following:
53749**
53750** SQLITE_OPEN_READWRITE
53751** SQLITE_OPEN_CREATE
53752** SQLITE_OPEN_EXCLUSIVE
53753** SQLITE_OPEN_DELETEONCLOSE
53754*/
53755static int pagerOpentemp(
53756 Pager *pPager, /* The pager object */
53757 sqlite3_file *pFile, /* Write the file descriptor here */
53758 int vfsFlags /* Flags passed through to the VFS */
53759){
53760 int rc; /* Return code */
53761
53762#ifdef SQLITE_TEST
53763 sqlite3_opentemp_count++; /* Used for testing and analysis only */
53764#endif
53765
53766 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
53767 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
53768 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
53769 assert( rc!=SQLITE_OK || isOpen(pFile) );
53770 return rc;
53771}
53772
53773/*
53774** Set the busy handler function.
53775**
53776** The pager invokes the busy-handler if sqlite3OsLock() returns
53777** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
53778** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
53779** lock. It does *not* invoke the busy handler when upgrading from
53780** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
53781** (which occurs during hot-journal rollback). Summary:
53782**
53783** Transition | Invokes xBusyHandler
53784** --------------------------------------------------------
53785** NO_LOCK -> SHARED_LOCK | Yes
53786** SHARED_LOCK -> RESERVED_LOCK | No
53787** SHARED_LOCK -> EXCLUSIVE_LOCK | No
53788** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
53789**
53790** If the busy-handler callback returns non-zero, the lock is
53791** retried. If it returns zero, then the SQLITE_BUSY error is
53792** returned to the caller of the pager API function.
53793*/
53794SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(
53795 Pager *pPager, /* Pager object */
53796 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
53797 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
53798){
53799 void **ap;
53800 pPager->xBusyHandler = xBusyHandler;
53801 pPager->pBusyHandlerArg = pBusyHandlerArg;
53802 ap = (void **)&pPager->xBusyHandler;
53803 assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
53804 assert( ap[1]==pBusyHandlerArg );
53805 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
53806}
53807
53808/*
53809** Change the page size used by the Pager object. The new page size
53810** is passed in *pPageSize.
53811**
53812** If the pager is in the error state when this function is called, it
53813** is a no-op. The value returned is the error state error code (i.e.
53814** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
53815**
53816** Otherwise, if all of the following are true:
53817**
53818** * the new page size (value of *pPageSize) is valid (a power
53819** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
53820**
53821** * there are no outstanding page references, and
53822**
53823** * the database is either not an in-memory database or it is
53824** an in-memory database that currently consists of zero pages.
53825**
53826** then the pager object page size is set to *pPageSize.
53827**
53828** If the page size is changed, then this function uses sqlite3PagerMalloc()
53829** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
53830** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
53831** In all other cases, SQLITE_OK is returned.
53832**
53833** If the page size is not changed, either because one of the enumerated
53834** conditions above is not true, the pager was in error state when this
53835** function was called, or because the memory allocation attempt failed,
53836** then *pPageSize is set to the old, retained page size before returning.
53837*/
53838SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
53839 int rc = SQLITE_OK;
53840
53841 /* It is not possible to do a full assert_pager_state() here, as this
53842 ** function may be called from within PagerOpen(), before the state
53843 ** of the Pager object is internally consistent.
53844 **
53845 ** At one point this function returned an error if the pager was in
53846 ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
53847 ** there is at least one outstanding page reference, this function
53848 ** is a no-op for that case anyhow.
53849 */
53850
53851 u32 pageSize = *pPageSize;
53852 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
53853 if( (pPager->memDb==0 || pPager->dbSize==0)
53854 && sqlite3PcacheRefCount(pPager->pPCache)==0
53855 && pageSize && pageSize!=(u32)pPager->pageSize
53856 ){
53857 char *pNew = NULL; /* New temp space */
53858 i64 nByte = 0;
53859
53860 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
53861 rc = sqlite3OsFileSize(pPager->fd, &nByte);
53862 }
53863 if( rc==SQLITE_OK ){
53864 pNew = (char *)sqlite3PageMalloc(pageSize);
53865 if( !pNew ) rc = SQLITE_NOMEM_BKPT;
53866 }
53867
53868 if( rc==SQLITE_OK ){
53869 pager_reset(pPager);
53870 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
53871 }
53872 if( rc==SQLITE_OK ){
53873 sqlite3PageFree(pPager->pTmpSpace);
53874 pPager->pTmpSpace = pNew;
53875 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
53876 pPager->pageSize = pageSize;
53877 }else{
53878 sqlite3PageFree(pNew);
53879 }
53880 }
53881
53882 *pPageSize = pPager->pageSize;
53883 if( rc==SQLITE_OK ){
53884 if( nReserve<0 ) nReserve = pPager->nReserve;
53885 assert( nReserve>=0 && nReserve<1000 );
53886 pPager->nReserve = (i16)nReserve;
53887 pagerReportSize(pPager);
53888 pagerFixMaplimit(pPager);
53889 }
53890 return rc;
53891}
53892
53893/*
53894** Return a pointer to the "temporary page" buffer held internally
53895** by the pager. This is a buffer that is big enough to hold the
53896** entire content of a database page. This buffer is used internally
53897** during rollback and will be overwritten whenever a rollback
53898** occurs. But other modules are free to use it too, as long as
53899** no rollbacks are happening.
53900*/
53901SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
53902 return pPager->pTmpSpace;
53903}
53904
53905/*
53906** Attempt to set the maximum database page count if mxPage is positive.
53907** Make no changes if mxPage is zero or negative. And never reduce the
53908** maximum page count below the current size of the database.
53909**
53910** Regardless of mxPage, return the current maximum page count.
53911*/
53912SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
53913 if( mxPage>0 ){
53914 pPager->mxPgno = mxPage;
53915 }
53916 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
53917 assert( pPager->mxPgno>=pPager->dbSize ); /* OP_MaxPgcnt enforces this */
53918 return pPager->mxPgno;
53919}
53920
53921/*
53922** The following set of routines are used to disable the simulated
53923** I/O error mechanism. These routines are used to avoid simulated
53924** errors in places where we do not care about errors.
53925**
53926** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
53927** and generate no code.
53928*/
53929#ifdef SQLITE_TEST
53930SQLITE_API extern int sqlite3_io_error_pending;
53931SQLITE_API extern int sqlite3_io_error_hit;
53932static int saved_cnt;
53933void disable_simulated_io_errors(void){
53934 saved_cnt = sqlite3_io_error_pending;
53935 sqlite3_io_error_pending = -1;
53936}
53937void enable_simulated_io_errors(void){
53938 sqlite3_io_error_pending = saved_cnt;
53939}
53940#else
53941# define disable_simulated_io_errors()
53942# define enable_simulated_io_errors()
53943#endif
53944
53945/*
53946** Read the first N bytes from the beginning of the file into memory
53947** that pDest points to.
53948**
53949** If the pager was opened on a transient file (zFilename==""), or
53950** opened on a file less than N bytes in size, the output buffer is
53951** zeroed and SQLITE_OK returned. The rationale for this is that this
53952** function is used to read database headers, and a new transient or
53953** zero sized database has a header than consists entirely of zeroes.
53954**
53955** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
53956** the error code is returned to the caller and the contents of the
53957** output buffer undefined.
53958*/
53959SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
53960 int rc = SQLITE_OK;
53961 memset(pDest, 0, N);
53962 assert( isOpen(pPager->fd) || pPager->tempFile );
53963
53964 /* This routine is only called by btree immediately after creating
53965 ** the Pager object. There has not been an opportunity to transition
53966 ** to WAL mode yet.
53967 */
53968 assert( !pagerUseWal(pPager) );
53969
53970 if( isOpen(pPager->fd) ){
53971 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
53972 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
53973 if( rc==SQLITE_IOERR_SHORT_READ ){
53974 rc = SQLITE_OK;
53975 }
53976 }
53977 return rc;
53978}
53979
53980/*
53981** This function may only be called when a read-transaction is open on
53982** the pager. It returns the total number of pages in the database.
53983**
53984** However, if the file is between 1 and <page-size> bytes in size, then
53985** this is considered a 1 page file.
53986*/
53987SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
53988 assert( pPager->eState>=PAGER_READER );
53989 assert( pPager->eState!=PAGER_WRITER_FINISHED );
53990 *pnPage = (int)pPager->dbSize;
53991}
53992
53993
53994/*
53995** Try to obtain a lock of type locktype on the database file. If
53996** a similar or greater lock is already held, this function is a no-op
53997** (returning SQLITE_OK immediately).
53998**
53999** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
54000** the busy callback if the lock is currently not available. Repeat
54001** until the busy callback returns false or until the attempt to
54002** obtain the lock succeeds.
54003**
54004** Return SQLITE_OK on success and an error code if we cannot obtain
54005** the lock. If the lock is obtained successfully, set the Pager.state
54006** variable to locktype before returning.
54007*/
54008static int pager_wait_on_lock(Pager *pPager, int locktype){
54009 int rc; /* Return code */
54010
54011 /* Check that this is either a no-op (because the requested lock is
54012 ** already held), or one of the transitions that the busy-handler
54013 ** may be invoked during, according to the comment above
54014 ** sqlite3PagerSetBusyhandler().
54015 */
54016 assert( (pPager->eLock>=locktype)
54017 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
54018 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
54019 );
54020
54021 do {
54022 rc = pagerLockDb(pPager, locktype);
54023 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
54024 return rc;
54025}
54026
54027/*
54028** Function assertTruncateConstraint(pPager) checks that one of the
54029** following is true for all dirty pages currently in the page-cache:
54030**
54031** a) The page number is less than or equal to the size of the
54032** current database image, in pages, OR
54033**
54034** b) if the page content were written at this time, it would not
54035** be necessary to write the current content out to the sub-journal
54036** (as determined by function subjRequiresPage()).
54037**
54038** If the condition asserted by this function were not true, and the
54039** dirty page were to be discarded from the cache via the pagerStress()
54040** routine, pagerStress() would not write the current page content to
54041** the database file. If a savepoint transaction were rolled back after
54042** this happened, the correct behavior would be to restore the current
54043** content of the page. However, since this content is not present in either
54044** the database file or the portion of the rollback journal and
54045** sub-journal rolled back the content could not be restored and the
54046** database image would become corrupt. It is therefore fortunate that
54047** this circumstance cannot arise.
54048*/
54049#if defined(SQLITE_DEBUG)
54050static void assertTruncateConstraintCb(PgHdr *pPg){
54051 assert( pPg->flags&PGHDR_DIRTY );
54052 assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
54053}
54054static void assertTruncateConstraint(Pager *pPager){
54055 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
54056}
54057#else
54058# define assertTruncateConstraint(pPager)
54059#endif
54060
54061/*
54062** Truncate the in-memory database file image to nPage pages. This
54063** function does not actually modify the database file on disk. It
54064** just sets the internal state of the pager object so that the
54065** truncation will be done when the current transaction is committed.
54066**
54067** This function is only called right before committing a transaction.
54068** Once this function has been called, the transaction must either be
54069** rolled back or committed. It is not safe to call this function and
54070** then continue writing to the database.
54071*/
54072SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
54073 assert( pPager->dbSize>=nPage );
54074 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
54075 pPager->dbSize = nPage;
54076
54077 /* At one point the code here called assertTruncateConstraint() to
54078 ** ensure that all pages being truncated away by this operation are,
54079 ** if one or more savepoints are open, present in the savepoint
54080 ** journal so that they can be restored if the savepoint is rolled
54081 ** back. This is no longer necessary as this function is now only
54082 ** called right before committing a transaction. So although the
54083 ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
54084 ** they cannot be rolled back. So the assertTruncateConstraint() call
54085 ** is no longer correct. */
54086}
54087
54088
54089/*
54090** This function is called before attempting a hot-journal rollback. It
54091** syncs the journal file to disk, then sets pPager->journalHdr to the
54092** size of the journal file so that the pager_playback() routine knows
54093** that the entire journal file has been synced.
54094**
54095** Syncing a hot-journal to disk before attempting to roll it back ensures
54096** that if a power-failure occurs during the rollback, the process that
54097** attempts rollback following system recovery sees the same journal
54098** content as this process.
54099**
54100** If everything goes as planned, SQLITE_OK is returned. Otherwise,
54101** an SQLite error code.
54102*/
54103static int pagerSyncHotJournal(Pager *pPager){
54104 int rc = SQLITE_OK;
54105 if( !pPager->noSync ){
54106 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
54107 }
54108 if( rc==SQLITE_OK ){
54109 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
54110 }
54111 return rc;
54112}
54113
54114#if SQLITE_MAX_MMAP_SIZE>0
54115/*
54116** Obtain a reference to a memory mapped page object for page number pgno.
54117** The new object will use the pointer pData, obtained from xFetch().
54118** If successful, set *ppPage to point to the new page reference
54119** and return SQLITE_OK. Otherwise, return an SQLite error code and set
54120** *ppPage to zero.
54121**
54122** Page references obtained by calling this function should be released
54123** by calling pagerReleaseMapPage().
54124*/
54125static int pagerAcquireMapPage(
54126 Pager *pPager, /* Pager object */
54127 Pgno pgno, /* Page number */
54128 void *pData, /* xFetch()'d data for this page */
54129 PgHdr **ppPage /* OUT: Acquired page object */
54130){
54131 PgHdr *p; /* Memory mapped page to return */
54132
54133 if( pPager->pMmapFreelist ){
54134 *ppPage = p = pPager->pMmapFreelist;
54135 pPager->pMmapFreelist = p->pDirty;
54136 p->pDirty = 0;
54137 assert( pPager->nExtra>=8 );
54138 memset(p->pExtra, 0, 8);
54139 }else{
54140 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
54141 if( p==0 ){
54142 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
54143 return SQLITE_NOMEM_BKPT;
54144 }
54145 p->pExtra = (void *)&p[1];
54146 p->flags = PGHDR_MMAP;
54147 p->nRef = 1;
54148 p->pPager = pPager;
54149 }
54150
54151 assert( p->pExtra==(void *)&p[1] );
54152 assert( p->pPage==0 );
54153 assert( p->flags==PGHDR_MMAP );
54154 assert( p->pPager==pPager );
54155 assert( p->nRef==1 );
54156
54157 p->pgno = pgno;
54158 p->pData = pData;
54159 pPager->nMmapOut++;
54160
54161 return SQLITE_OK;
54162}
54163#endif
54164
54165/*
54166** Release a reference to page pPg. pPg must have been returned by an
54167** earlier call to pagerAcquireMapPage().
54168*/
54169static void pagerReleaseMapPage(PgHdr *pPg){
54170 Pager *pPager = pPg->pPager;
54171 pPager->nMmapOut--;
54172 pPg->pDirty = pPager->pMmapFreelist;
54173 pPager->pMmapFreelist = pPg;
54174
54175 assert( pPager->fd->pMethods->iVersion>=3 );
54176 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
54177}
54178
54179/*
54180** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
54181*/
54182static void pagerFreeMapHdrs(Pager *pPager){
54183 PgHdr *p;
54184 PgHdr *pNext;
54185 for(p=pPager->pMmapFreelist; p; p=pNext){
54186 pNext = p->pDirty;
54187 sqlite3_free(p);
54188 }
54189}
54190
54191/* Verify that the database file has not be deleted or renamed out from
54192** under the pager. Return SQLITE_OK if the database is still where it ought
54193** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
54194** code from sqlite3OsAccess()) if the database has gone missing.
54195*/
54196static int databaseIsUnmoved(Pager *pPager){
54197 int bHasMoved = 0;
54198 int rc;
54199
54200 if( pPager->tempFile ) return SQLITE_OK;
54201 if( pPager->dbSize==0 ) return SQLITE_OK;
54202 assert( pPager->zFilename && pPager->zFilename[0] );
54203 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
54204 if( rc==SQLITE_NOTFOUND ){
54205 /* If the HAS_MOVED file-control is unimplemented, assume that the file
54206 ** has not been moved. That is the historical behavior of SQLite: prior to
54207 ** version 3.8.3, it never checked */
54208 rc = SQLITE_OK;
54209 }else if( rc==SQLITE_OK && bHasMoved ){
54210 rc = SQLITE_READONLY_DBMOVED;
54211 }
54212 return rc;
54213}
54214
54215
54216/*
54217** Shutdown the page cache. Free all memory and close all files.
54218**
54219** If a transaction was in progress when this routine is called, that
54220** transaction is rolled back. All outstanding pages are invalidated
54221** and their memory is freed. Any attempt to use a page associated
54222** with this page cache after this function returns will likely
54223** result in a coredump.
54224**
54225** This function always succeeds. If a transaction is active an attempt
54226** is made to roll it back. If an error occurs during the rollback
54227** a hot journal may be left in the filesystem but no error is returned
54228** to the caller.
54229*/
54230SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
54231 u8 *pTmp = (u8*)pPager->pTmpSpace;
54232 assert( db || pagerUseWal(pPager)==0 );
54233 assert( assert_pager_state(pPager) );
54234 disable_simulated_io_errors();
54235 sqlite3BeginBenignMalloc();
54236 pagerFreeMapHdrs(pPager);
54237 /* pPager->errCode = 0; */
54238 pPager->exclusiveMode = 0;
54239#ifndef SQLITE_OMIT_WAL
54240 {
54241 u8 *a = 0;
54242 assert( db || pPager->pWal==0 );
54243 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
54244 && SQLITE_OK==databaseIsUnmoved(pPager)
54245 ){
54246 a = pTmp;
54247 }
54248 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
54249 pPager->pWal = 0;
54250 }
54251#endif
54252 pager_reset(pPager);
54253 if( MEMDB ){
54254 pager_unlock(pPager);
54255 }else{
54256 /* If it is open, sync the journal file before calling UnlockAndRollback.
54257 ** If this is not done, then an unsynced portion of the open journal
54258 ** file may be played back into the database. If a power failure occurs
54259 ** while this is happening, the database could become corrupt.
54260 **
54261 ** If an error occurs while trying to sync the journal, shift the pager
54262 ** into the ERROR state. This causes UnlockAndRollback to unlock the
54263 ** database and close the journal file without attempting to roll it
54264 ** back or finalize it. The next database user will have to do hot-journal
54265 ** rollback before accessing the database file.
54266 */
54267 if( isOpen(pPager->jfd) ){
54268 pager_error(pPager, pagerSyncHotJournal(pPager));
54269 }
54270 pagerUnlockAndRollback(pPager);
54271 }
54272 sqlite3EndBenignMalloc();
54273 enable_simulated_io_errors();
54274 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
54275 IOTRACE(("CLOSE %p\n", pPager))
54276 sqlite3OsClose(pPager->jfd);
54277 sqlite3OsClose(pPager->fd);
54278 sqlite3PageFree(pTmp);
54279 sqlite3PcacheClose(pPager->pPCache);
54280
54281#ifdef SQLITE_HAS_CODEC
54282 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
54283#endif
54284
54285 assert( !pPager->aSavepoint && !pPager->pInJournal );
54286 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
54287
54288 sqlite3_free(pPager);
54289 return SQLITE_OK;
54290}
54291
54292#if !defined(NDEBUG) || defined(SQLITE_TEST)
54293/*
54294** Return the page number for page pPg.
54295*/
54296SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
54297 return pPg->pgno;
54298}
54299#endif
54300
54301/*
54302** Increment the reference count for page pPg.
54303*/
54304SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
54305 sqlite3PcacheRef(pPg);
54306}
54307
54308/*
54309** Sync the journal. In other words, make sure all the pages that have
54310** been written to the journal have actually reached the surface of the
54311** disk and can be restored in the event of a hot-journal rollback.
54312**
54313** If the Pager.noSync flag is set, then this function is a no-op.
54314** Otherwise, the actions required depend on the journal-mode and the
54315** device characteristics of the file-system, as follows:
54316**
54317** * If the journal file is an in-memory journal file, no action need
54318** be taken.
54319**
54320** * Otherwise, if the device does not support the SAFE_APPEND property,
54321** then the nRec field of the most recently written journal header
54322** is updated to contain the number of journal records that have
54323** been written following it. If the pager is operating in full-sync
54324** mode, then the journal file is synced before this field is updated.
54325**
54326** * If the device does not support the SEQUENTIAL property, then
54327** journal file is synced.
54328**
54329** Or, in pseudo-code:
54330**
54331** if( NOT <in-memory journal> ){
54332** if( NOT SAFE_APPEND ){
54333** if( <full-sync mode> ) xSync(<journal file>);
54334** <update nRec field>
54335** }
54336** if( NOT SEQUENTIAL ) xSync(<journal file>);
54337** }
54338**
54339** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
54340** page currently held in memory before returning SQLITE_OK. If an IO
54341** error is encountered, then the IO error code is returned to the caller.
54342*/
54343static int syncJournal(Pager *pPager, int newHdr){
54344 int rc; /* Return code */
54345
54346 assert( pPager->eState==PAGER_WRITER_CACHEMOD
54347 || pPager->eState==PAGER_WRITER_DBMOD
54348 );
54349 assert( assert_pager_state(pPager) );
54350 assert( !pagerUseWal(pPager) );
54351
54352 rc = sqlite3PagerExclusiveLock(pPager);
54353 if( rc!=SQLITE_OK ) return rc;
54354
54355 if( !pPager->noSync ){
54356 assert( !pPager->tempFile );
54357 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
54358 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
54359 assert( isOpen(pPager->jfd) );
54360
54361 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
54362 /* This block deals with an obscure problem. If the last connection
54363 ** that wrote to this database was operating in persistent-journal
54364 ** mode, then the journal file may at this point actually be larger
54365 ** than Pager.journalOff bytes. If the next thing in the journal
54366 ** file happens to be a journal-header (written as part of the
54367 ** previous connection's transaction), and a crash or power-failure
54368 ** occurs after nRec is updated but before this connection writes
54369 ** anything else to the journal file (or commits/rolls back its
54370 ** transaction), then SQLite may become confused when doing the
54371 ** hot-journal rollback following recovery. It may roll back all
54372 ** of this connections data, then proceed to rolling back the old,
54373 ** out-of-date data that follows it. Database corruption.
54374 **
54375 ** To work around this, if the journal file does appear to contain
54376 ** a valid header following Pager.journalOff, then write a 0x00
54377 ** byte to the start of it to prevent it from being recognized.
54378 **
54379 ** Variable iNextHdrOffset is set to the offset at which this
54380 ** problematic header will occur, if it exists. aMagic is used
54381 ** as a temporary buffer to inspect the first couple of bytes of
54382 ** the potential journal header.
54383 */
54384 i64 iNextHdrOffset;
54385 u8 aMagic[8];
54386 u8 zHeader[sizeof(aJournalMagic)+4];
54387
54388 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
54389 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
54390
54391 iNextHdrOffset = journalHdrOffset(pPager);
54392 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
54393 if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
54394 static const u8 zerobyte = 0;
54395 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
54396 }
54397 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
54398 return rc;
54399 }
54400
54401 /* Write the nRec value into the journal file header. If in
54402 ** full-synchronous mode, sync the journal first. This ensures that
54403 ** all data has really hit the disk before nRec is updated to mark
54404 ** it as a candidate for rollback.
54405 **
54406 ** This is not required if the persistent media supports the
54407 ** SAFE_APPEND property. Because in this case it is not possible
54408 ** for garbage data to be appended to the file, the nRec field
54409 ** is populated with 0xFFFFFFFF when the journal header is written
54410 ** and never needs to be updated.
54411 */
54412 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
54413 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
54414 IOTRACE(("JSYNC %p\n", pPager))
54415 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
54416 if( rc!=SQLITE_OK ) return rc;
54417 }
54418 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
54419 rc = sqlite3OsWrite(
54420 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
54421 );
54422 if( rc!=SQLITE_OK ) return rc;
54423 }
54424 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
54425 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
54426 IOTRACE(("JSYNC %p\n", pPager))
54427 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
54428 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
54429 );
54430 if( rc!=SQLITE_OK ) return rc;
54431 }
54432
54433 pPager->journalHdr = pPager->journalOff;
54434 if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
54435 pPager->nRec = 0;
54436 rc = writeJournalHdr(pPager);
54437 if( rc!=SQLITE_OK ) return rc;
54438 }
54439 }else{
54440 pPager->journalHdr = pPager->journalOff;
54441 }
54442 }
54443
54444 /* Unless the pager is in noSync mode, the journal file was just
54445 ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
54446 ** all pages.
54447 */
54448 sqlite3PcacheClearSyncFlags(pPager->pPCache);
54449 pPager->eState = PAGER_WRITER_DBMOD;
54450 assert( assert_pager_state(pPager) );
54451 return SQLITE_OK;
54452}
54453
54454/*
54455** The argument is the first in a linked list of dirty pages connected
54456** by the PgHdr.pDirty pointer. This function writes each one of the
54457** in-memory pages in the list to the database file. The argument may
54458** be NULL, representing an empty list. In this case this function is
54459** a no-op.
54460**
54461** The pager must hold at least a RESERVED lock when this function
54462** is called. Before writing anything to the database file, this lock
54463** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
54464** SQLITE_BUSY is returned and no data is written to the database file.
54465**
54466** If the pager is a temp-file pager and the actual file-system file
54467** is not yet open, it is created and opened before any data is
54468** written out.
54469**
54470** Once the lock has been upgraded and, if necessary, the file opened,
54471** the pages are written out to the database file in list order. Writing
54472** a page is skipped if it meets either of the following criteria:
54473**
54474** * The page number is greater than Pager.dbSize, or
54475** * The PGHDR_DONT_WRITE flag is set on the page.
54476**
54477** If writing out a page causes the database file to grow, Pager.dbFileSize
54478** is updated accordingly. If page 1 is written out, then the value cached
54479** in Pager.dbFileVers[] is updated to match the new value stored in
54480** the database file.
54481**
54482** If everything is successful, SQLITE_OK is returned. If an IO error
54483** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
54484** be obtained, SQLITE_BUSY is returned.
54485*/
54486static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
54487 int rc = SQLITE_OK; /* Return code */
54488
54489 /* This function is only called for rollback pagers in WRITER_DBMOD state. */
54490 assert( !pagerUseWal(pPager) );
54491 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
54492 assert( pPager->eLock==EXCLUSIVE_LOCK );
54493 assert( isOpen(pPager->fd) || pList->pDirty==0 );
54494
54495 /* If the file is a temp-file has not yet been opened, open it now. It
54496 ** is not possible for rc to be other than SQLITE_OK if this branch
54497 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
54498 */
54499 if( !isOpen(pPager->fd) ){
54500 assert( pPager->tempFile && rc==SQLITE_OK );
54501 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
54502 }
54503
54504 /* Before the first write, give the VFS a hint of what the final
54505 ** file size will be.
54506 */
54507 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
54508 if( rc==SQLITE_OK
54509 && pPager->dbHintSize<pPager->dbSize
54510 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
54511 ){
54512 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
54513 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
54514 pPager->dbHintSize = pPager->dbSize;
54515 }
54516
54517 while( rc==SQLITE_OK && pList ){
54518 Pgno pgno = pList->pgno;
54519
54520 /* If there are dirty pages in the page cache with page numbers greater
54521 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
54522 ** make the file smaller (presumably by auto-vacuum code). Do not write
54523 ** any such pages to the file.
54524 **
54525 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
54526 ** set (set by sqlite3PagerDontWrite()).
54527 */
54528 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
54529 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
54530 char *pData; /* Data to write */
54531
54532 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
54533 if( pList->pgno==1 ) pager_write_changecounter(pList);
54534
54535 /* Encode the database */
54536 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData);
54537
54538 /* Write out the page data. */
54539 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
54540
54541 /* If page 1 was just written, update Pager.dbFileVers to match
54542 ** the value now stored in the database file. If writing this
54543 ** page caused the database file to grow, update dbFileSize.
54544 */
54545 if( pgno==1 ){
54546 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
54547 }
54548 if( pgno>pPager->dbFileSize ){
54549 pPager->dbFileSize = pgno;
54550 }
54551 pPager->aStat[PAGER_STAT_WRITE]++;
54552
54553 /* Update any backup objects copying the contents of this pager. */
54554 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
54555
54556 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
54557 PAGERID(pPager), pgno, pager_pagehash(pList)));
54558 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
54559 PAGER_INCR(sqlite3_pager_writedb_count);
54560 }else{
54561 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
54562 }
54563 pager_set_pagehash(pList);
54564 pList = pList->pDirty;
54565 }
54566
54567 return rc;
54568}
54569
54570/*
54571** Ensure that the sub-journal file is open. If it is already open, this
54572** function is a no-op.
54573**
54574** SQLITE_OK is returned if everything goes according to plan. An
54575** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
54576** fails.
54577*/
54578static int openSubJournal(Pager *pPager){
54579 int rc = SQLITE_OK;
54580 if( !isOpen(pPager->sjfd) ){
54581 const int flags = SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE
54582 | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE
54583 | SQLITE_OPEN_DELETEONCLOSE;
54584 int nStmtSpill = sqlite3Config.nStmtSpill;
54585 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
54586 nStmtSpill = -1;
54587 }
54588 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
54589 }
54590 return rc;
54591}
54592
54593/*
54594** Append a record of the current state of page pPg to the sub-journal.
54595**
54596** If successful, set the bit corresponding to pPg->pgno in the bitvecs
54597** for all open savepoints before returning.
54598**
54599** This function returns SQLITE_OK if everything is successful, an IO
54600** error code if the attempt to write to the sub-journal fails, or
54601** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
54602** bitvec.
54603*/
54604static int subjournalPage(PgHdr *pPg){
54605 int rc = SQLITE_OK;
54606 Pager *pPager = pPg->pPager;
54607 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
54608
54609 /* Open the sub-journal, if it has not already been opened */
54610 assert( pPager->useJournal );
54611 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
54612 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
54613 assert( pagerUseWal(pPager)
54614 || pageInJournal(pPager, pPg)
54615 || pPg->pgno>pPager->dbOrigSize
54616 );
54617 rc = openSubJournal(pPager);
54618
54619 /* If the sub-journal was opened successfully (or was already open),
54620 ** write the journal record into the file. */
54621 if( rc==SQLITE_OK ){
54622 void *pData = pPg->pData;
54623 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
54624 char *pData2;
54625
54626#if SQLITE_HAS_CODEC
54627 if( !pPager->subjInMemory ){
54628 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
54629 }else
54630#endif
54631 pData2 = pData;
54632 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
54633 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
54634 if( rc==SQLITE_OK ){
54635 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
54636 }
54637 }
54638 }
54639 if( rc==SQLITE_OK ){
54640 pPager->nSubRec++;
54641 assert( pPager->nSavepoint>0 );
54642 rc = addToSavepointBitvecs(pPager, pPg->pgno);
54643 }
54644 return rc;
54645}
54646static int subjournalPageIfRequired(PgHdr *pPg){
54647 if( subjRequiresPage(pPg) ){
54648 return subjournalPage(pPg);
54649 }else{
54650 return SQLITE_OK;
54651 }
54652}
54653
54654/*
54655** This function is called by the pcache layer when it has reached some
54656** soft memory limit. The first argument is a pointer to a Pager object
54657** (cast as a void*). The pager is always 'purgeable' (not an in-memory
54658** database). The second argument is a reference to a page that is
54659** currently dirty but has no outstanding references. The page
54660** is always associated with the Pager object passed as the first
54661** argument.
54662**
54663** The job of this function is to make pPg clean by writing its contents
54664** out to the database file, if possible. This may involve syncing the
54665** journal file.
54666**
54667** If successful, sqlite3PcacheMakeClean() is called on the page and
54668** SQLITE_OK returned. If an IO error occurs while trying to make the
54669** page clean, the IO error code is returned. If the page cannot be
54670** made clean for some other reason, but no error occurs, then SQLITE_OK
54671** is returned by sqlite3PcacheMakeClean() is not called.
54672*/
54673static int pagerStress(void *p, PgHdr *pPg){
54674 Pager *pPager = (Pager *)p;
54675 int rc = SQLITE_OK;
54676
54677 assert( pPg->pPager==pPager );
54678 assert( pPg->flags&PGHDR_DIRTY );
54679
54680 /* The doNotSpill NOSYNC bit is set during times when doing a sync of
54681 ** journal (and adding a new header) is not allowed. This occurs
54682 ** during calls to sqlite3PagerWrite() while trying to journal multiple
54683 ** pages belonging to the same sector.
54684 **
54685 ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
54686 ** regardless of whether or not a sync is required. This is set during
54687 ** a rollback or by user request, respectively.
54688 **
54689 ** Spilling is also prohibited when in an error state since that could
54690 ** lead to database corruption. In the current implementation it
54691 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
54692 ** while in the error state, hence it is impossible for this routine to
54693 ** be called in the error state. Nevertheless, we include a NEVER()
54694 ** test for the error state as a safeguard against future changes.
54695 */
54696 if( NEVER(pPager->errCode) ) return SQLITE_OK;
54697 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
54698 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
54699 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
54700 if( pPager->doNotSpill
54701 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
54702 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
54703 ){
54704 return SQLITE_OK;
54705 }
54706
54707 pPager->aStat[PAGER_STAT_SPILL]++;
54708 pPg->pDirty = 0;
54709 if( pagerUseWal(pPager) ){
54710 /* Write a single frame for this page to the log. */
54711 rc = subjournalPageIfRequired(pPg);
54712 if( rc==SQLITE_OK ){
54713 rc = pagerWalFrames(pPager, pPg, 0, 0);
54714 }
54715 }else{
54716
54717#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
54718 if( pPager->tempFile==0 ){
54719 rc = sqlite3JournalCreate(pPager->jfd);
54720 if( rc!=SQLITE_OK ) return pager_error(pPager, rc);
54721 }
54722#endif
54723
54724 /* Sync the journal file if required. */
54725 if( pPg->flags&PGHDR_NEED_SYNC
54726 || pPager->eState==PAGER_WRITER_CACHEMOD
54727 ){
54728 rc = syncJournal(pPager, 1);
54729 }
54730
54731 /* Write the contents of the page out to the database file. */
54732 if( rc==SQLITE_OK ){
54733 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
54734 rc = pager_write_pagelist(pPager, pPg);
54735 }
54736 }
54737
54738 /* Mark the page as clean. */
54739 if( rc==SQLITE_OK ){
54740 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
54741 sqlite3PcacheMakeClean(pPg);
54742 }
54743
54744 return pager_error(pPager, rc);
54745}
54746
54747/*
54748** Flush all unreferenced dirty pages to disk.
54749*/
54750SQLITE_PRIVATE int sqlite3PagerFlush(Pager *pPager){
54751 int rc = pPager->errCode;
54752 if( !MEMDB ){
54753 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
54754 assert( assert_pager_state(pPager) );
54755 while( rc==SQLITE_OK && pList ){
54756 PgHdr *pNext = pList->pDirty;
54757 if( pList->nRef==0 ){
54758 rc = pagerStress((void*)pPager, pList);
54759 }
54760 pList = pNext;
54761 }
54762 }
54763
54764 return rc;
54765}
54766
54767/*
54768** Allocate and initialize a new Pager object and put a pointer to it
54769** in *ppPager. The pager should eventually be freed by passing it
54770** to sqlite3PagerClose().
54771**
54772** The zFilename argument is the path to the database file to open.
54773** If zFilename is NULL then a randomly-named temporary file is created
54774** and used as the file to be cached. Temporary files are be deleted
54775** automatically when they are closed. If zFilename is ":memory:" then
54776** all information is held in cache. It is never written to disk.
54777** This can be used to implement an in-memory database.
54778**
54779** The nExtra parameter specifies the number of bytes of space allocated
54780** along with each page reference. This space is available to the user
54781** via the sqlite3PagerGetExtra() API. When a new page is allocated, the
54782** first 8 bytes of this space are zeroed but the remainder is uninitialized.
54783** (The extra space is used by btree as the MemPage object.)
54784**
54785** The flags argument is used to specify properties that affect the
54786** operation of the pager. It should be passed some bitwise combination
54787** of the PAGER_* flags.
54788**
54789** The vfsFlags parameter is a bitmask to pass to the flags parameter
54790** of the xOpen() method of the supplied VFS when opening files.
54791**
54792** If the pager object is allocated and the specified file opened
54793** successfully, SQLITE_OK is returned and *ppPager set to point to
54794** the new pager object. If an error occurs, *ppPager is set to NULL
54795** and error code returned. This function may return SQLITE_NOMEM
54796** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
54797** various SQLITE_IO_XXX errors.
54798*/
54799SQLITE_PRIVATE int sqlite3PagerOpen(
54800 sqlite3_vfs *pVfs, /* The virtual file system to use */
54801 Pager **ppPager, /* OUT: Return the Pager structure here */
54802 const char *zFilename, /* Name of the database file to open */
54803 int nExtra, /* Extra bytes append to each in-memory page */
54804 int flags, /* flags controlling this file */
54805 int vfsFlags, /* flags passed through to sqlite3_vfs.xOpen() */
54806 void (*xReinit)(DbPage*) /* Function to reinitialize pages */
54807){
54808 u8 *pPtr;
54809 Pager *pPager = 0; /* Pager object to allocate and return */
54810 int rc = SQLITE_OK; /* Return code */
54811 int tempFile = 0; /* True for temp files (incl. in-memory files) */
54812 int memDb = 0; /* True if this is an in-memory file */
54813#ifdef SQLITE_ENABLE_DESERIALIZE
54814 int memJM = 0; /* Memory journal mode */
54815#else
54816# define memJM 0
54817#endif
54818 int readOnly = 0; /* True if this is a read-only file */
54819 int journalFileSize; /* Bytes to allocate for each journal fd */
54820 char *zPathname = 0; /* Full path to database file */
54821 int nPathname = 0; /* Number of bytes in zPathname */
54822 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
54823 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
54824 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
54825 const char *zUri = 0; /* URI args to copy */
54826 int nUri = 0; /* Number of bytes of URI args at *zUri */
54827
54828 /* Figure out how much space is required for each journal file-handle
54829 ** (there are two of them, the main journal and the sub-journal). */
54830 journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
54831
54832 /* Set the output variable to NULL in case an error occurs. */
54833 *ppPager = 0;
54834
54835#ifndef SQLITE_OMIT_MEMORYDB
54836 if( flags & PAGER_MEMORY ){
54837 memDb = 1;
54838 if( zFilename && zFilename[0] ){
54839 zPathname = sqlite3DbStrDup(0, zFilename);
54840 if( zPathname==0 ) return SQLITE_NOMEM_BKPT;
54841 nPathname = sqlite3Strlen30(zPathname);
54842 zFilename = 0;
54843 }
54844 }
54845#endif
54846
54847 /* Compute and store the full pathname in an allocated buffer pointed
54848 ** to by zPathname, length nPathname. Or, if this is a temporary file,
54849 ** leave both nPathname and zPathname set to 0.
54850 */
54851 if( zFilename && zFilename[0] ){
54852 const char *z;
54853 nPathname = pVfs->mxPathname+1;
54854 zPathname = sqlite3DbMallocRaw(0, nPathname*2);
54855 if( zPathname==0 ){
54856 return SQLITE_NOMEM_BKPT;
54857 }
54858 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
54859 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
54860 nPathname = sqlite3Strlen30(zPathname);
54861 z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
54862 while( *z ){
54863 z += sqlite3Strlen30(z)+1;
54864 z += sqlite3Strlen30(z)+1;
54865 }
54866 nUri = (int)(&z[1] - zUri);
54867 assert( nUri>=0 );
54868 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
54869 /* This branch is taken when the journal path required by
54870 ** the database being opened will be more than pVfs->mxPathname
54871 ** bytes in length. This means the database cannot be opened,
54872 ** as it will not be possible to open the journal file or even
54873 ** check for a hot-journal before reading.
54874 */
54875 rc = SQLITE_CANTOPEN_BKPT;
54876 }
54877 if( rc!=SQLITE_OK ){
54878 sqlite3DbFree(0, zPathname);
54879 return rc;
54880 }
54881 }
54882
54883 /* Allocate memory for the Pager structure, PCache object, the
54884 ** three file descriptors, the database file name and the journal
54885 ** file name. The layout in memory is as follows:
54886 **
54887 ** Pager object (sizeof(Pager) bytes)
54888 ** PCache object (sqlite3PcacheSize() bytes)
54889 ** Database file handle (pVfs->szOsFile bytes)
54890 ** Sub-journal file handle (journalFileSize bytes)
54891 ** Main journal file handle (journalFileSize bytes)
54892 ** Database file name (nPathname+1 bytes)
54893 ** Journal file name (nPathname+8+1 bytes)
54894 */
54895 pPtr = (u8 *)sqlite3MallocZero(
54896 ROUND8(sizeof(*pPager)) + /* Pager structure */
54897 ROUND8(pcacheSize) + /* PCache object */
54898 ROUND8(pVfs->szOsFile) + /* The main db file */
54899 journalFileSize * 2 + /* The two journal files */
54900 nPathname + 1 + nUri + /* zFilename */
54901 nPathname + 8 + 2 /* zJournal */
54902#ifndef SQLITE_OMIT_WAL
54903 + nPathname + 4 + 2 /* zWal */
54904#endif
54905 );
54906 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
54907 if( !pPtr ){
54908 sqlite3DbFree(0, zPathname);
54909 return SQLITE_NOMEM_BKPT;
54910 }
54911 pPager = (Pager*)(pPtr);
54912 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
54913 pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
54914 pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
54915 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize);
54916 pPager->zFilename = (char*)(pPtr += journalFileSize);
54917 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
54918
54919 /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
54920 if( zPathname ){
54921 assert( nPathname>0 );
54922 pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri);
54923 memcpy(pPager->zFilename, zPathname, nPathname);
54924 if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
54925 memcpy(pPager->zJournal, zPathname, nPathname);
54926 memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
54927 sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
54928#ifndef SQLITE_OMIT_WAL
54929 pPager->zWal = &pPager->zJournal[nPathname+8+1];
54930 memcpy(pPager->zWal, zPathname, nPathname);
54931 memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
54932 sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
54933#endif
54934 sqlite3DbFree(0, zPathname);
54935 }
54936 pPager->pVfs = pVfs;
54937 pPager->vfsFlags = vfsFlags;
54938
54939 /* Open the pager file.
54940 */
54941 if( zFilename && zFilename[0] ){
54942 int fout = 0; /* VFS flags returned by xOpen() */
54943 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
54944 assert( !memDb );
54945#ifdef SQLITE_ENABLE_DESERIALIZE
54946 memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
54947#endif
54948 readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
54949
54950 /* If the file was successfully opened for read/write access,
54951 ** choose a default page size in case we have to create the
54952 ** database file. The default page size is the maximum of:
54953 **
54954 ** + SQLITE_DEFAULT_PAGE_SIZE,
54955 ** + The value returned by sqlite3OsSectorSize()
54956 ** + The largest page size that can be written atomically.
54957 */
54958 if( rc==SQLITE_OK ){
54959 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
54960 if( !readOnly ){
54961 setSectorSize(pPager);
54962 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
54963 if( szPageDflt<pPager->sectorSize ){
54964 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
54965 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
54966 }else{
54967 szPageDflt = (u32)pPager->sectorSize;
54968 }
54969 }
54970#ifdef SQLITE_ENABLE_ATOMIC_WRITE
54971 {
54972 int ii;
54973 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
54974 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
54975 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
54976 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
54977 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
54978 szPageDflt = ii;
54979 }
54980 }
54981 }
54982#endif
54983 }
54984 pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0);
54985 if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0
54986 || sqlite3_uri_boolean(zFilename, "immutable", 0) ){
54987 vfsFlags |= SQLITE_OPEN_READONLY;
54988 goto act_like_temp_file;
54989 }
54990 }
54991 }else{
54992 /* If a temporary file is requested, it is not opened immediately.
54993 ** In this case we accept the default page size and delay actually
54994 ** opening the file until the first call to OsWrite().
54995 **
54996 ** This branch is also run for an in-memory database. An in-memory
54997 ** database is the same as a temp-file that is never written out to
54998 ** disk and uses an in-memory rollback journal.
54999 **
55000 ** This branch also runs for files marked as immutable.
55001 */
55002act_like_temp_file:
55003 tempFile = 1;
55004 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
55005 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
55006 pPager->noLock = 1; /* Do no locking */
55007 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
55008 }
55009
55010 /* The following call to PagerSetPagesize() serves to set the value of
55011 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
55012 */
55013 if( rc==SQLITE_OK ){
55014 assert( pPager->memDb==0 );
55015 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
55016 testcase( rc!=SQLITE_OK );
55017 }
55018
55019 /* Initialize the PCache object. */
55020 if( rc==SQLITE_OK ){
55021 nExtra = ROUND8(nExtra);
55022 assert( nExtra>=8 && nExtra<1000 );
55023 rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
55024 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
55025 }
55026
55027 /* If an error occurred above, free the Pager structure and close the file.
55028 */
55029 if( rc!=SQLITE_OK ){
55030 sqlite3OsClose(pPager->fd);
55031 sqlite3PageFree(pPager->pTmpSpace);
55032 sqlite3_free(pPager);
55033 return rc;
55034 }
55035
55036 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
55037 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
55038
55039 pPager->useJournal = (u8)useJournal;
55040 /* pPager->stmtOpen = 0; */
55041 /* pPager->stmtInUse = 0; */
55042 /* pPager->nRef = 0; */
55043 /* pPager->stmtSize = 0; */
55044 /* pPager->stmtJSize = 0; */
55045 /* pPager->nPage = 0; */
55046 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
55047 /* pPager->state = PAGER_UNLOCK; */
55048 /* pPager->errMask = 0; */
55049 pPager->tempFile = (u8)tempFile;
55050 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
55051 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
55052 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
55053 pPager->exclusiveMode = (u8)tempFile;
55054 pPager->changeCountDone = pPager->tempFile;
55055 pPager->memDb = (u8)memDb;
55056 pPager->readOnly = (u8)readOnly;
55057 assert( useJournal || pPager->tempFile );
55058 pPager->noSync = pPager->tempFile;
55059 if( pPager->noSync ){
55060 assert( pPager->fullSync==0 );
55061 assert( pPager->extraSync==0 );
55062 assert( pPager->syncFlags==0 );
55063 assert( pPager->walSyncFlags==0 );
55064 }else{
55065 pPager->fullSync = 1;
55066 pPager->extraSync = 0;
55067 pPager->syncFlags = SQLITE_SYNC_NORMAL;
55068 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2);
55069 }
55070 /* pPager->pFirst = 0; */
55071 /* pPager->pFirstSynced = 0; */
55072 /* pPager->pLast = 0; */
55073 pPager->nExtra = (u16)nExtra;
55074 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
55075 assert( isOpen(pPager->fd) || tempFile );
55076 setSectorSize(pPager);
55077 if( !useJournal ){
55078 pPager->journalMode = PAGER_JOURNALMODE_OFF;
55079 }else if( memDb || memJM ){
55080 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
55081 }
55082 /* pPager->xBusyHandler = 0; */
55083 /* pPager->pBusyHandlerArg = 0; */
55084 pPager->xReiniter = xReinit;
55085 setGetterMethod(pPager);
55086 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
55087 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
55088
55089 *ppPager = pPager;
55090 return SQLITE_OK;
55091}
55092
55093
55094
55095/*
55096** This function is called after transitioning from PAGER_UNLOCK to
55097** PAGER_SHARED state. It tests if there is a hot journal present in
55098** the file-system for the given pager. A hot journal is one that
55099** needs to be played back. According to this function, a hot-journal
55100** file exists if the following criteria are met:
55101**
55102** * The journal file exists in the file system, and
55103** * No process holds a RESERVED or greater lock on the database file, and
55104** * The database file itself is greater than 0 bytes in size, and
55105** * The first byte of the journal file exists and is not 0x00.
55106**
55107** If the current size of the database file is 0 but a journal file
55108** exists, that is probably an old journal left over from a prior
55109** database with the same name. In this case the journal file is
55110** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
55111** is returned.
55112**
55113** This routine does not check if there is a master journal filename
55114** at the end of the file. If there is, and that master journal file
55115** does not exist, then the journal file is not really hot. In this
55116** case this routine will return a false-positive. The pager_playback()
55117** routine will discover that the journal file is not really hot and
55118** will not roll it back.
55119**
55120** If a hot-journal file is found to exist, *pExists is set to 1 and
55121** SQLITE_OK returned. If no hot-journal file is present, *pExists is
55122** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
55123** to determine whether or not a hot-journal file exists, the IO error
55124** code is returned and the value of *pExists is undefined.
55125*/
55126static int hasHotJournal(Pager *pPager, int *pExists){
55127 sqlite3_vfs * const pVfs = pPager->pVfs;
55128 int rc = SQLITE_OK; /* Return code */
55129 int exists = 1; /* True if a journal file is present */
55130 int jrnlOpen = !!isOpen(pPager->jfd);
55131
55132 assert( pPager->useJournal );
55133 assert( isOpen(pPager->fd) );
55134 assert( pPager->eState==PAGER_OPEN );
55135
55136 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
55137 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
55138 ));
55139
55140 *pExists = 0;
55141 if( !jrnlOpen ){
55142 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
55143 }
55144 if( rc==SQLITE_OK && exists ){
55145 int locked = 0; /* True if some process holds a RESERVED lock */
55146
55147 /* Race condition here: Another process might have been holding the
55148 ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
55149 ** call above, but then delete the journal and drop the lock before
55150 ** we get to the following sqlite3OsCheckReservedLock() call. If that
55151 ** is the case, this routine might think there is a hot journal when
55152 ** in fact there is none. This results in a false-positive which will
55153 ** be dealt with by the playback routine. Ticket #3883.
55154 */
55155 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
55156 if( rc==SQLITE_OK && !locked ){
55157 Pgno nPage; /* Number of pages in database file */
55158
55159 assert( pPager->tempFile==0 );
55160 rc = pagerPagecount(pPager, &nPage);
55161 if( rc==SQLITE_OK ){
55162 /* If the database is zero pages in size, that means that either (1) the
55163 ** journal is a remnant from a prior database with the same name where
55164 ** the database file but not the journal was deleted, or (2) the initial
55165 ** transaction that populates a new database is being rolled back.
55166 ** In either case, the journal file can be deleted. However, take care
55167 ** not to delete the journal file if it is already open due to
55168 ** journal_mode=PERSIST.
55169 */
55170 if( nPage==0 && !jrnlOpen ){
55171 sqlite3BeginBenignMalloc();
55172 if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
55173 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
55174 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
55175 }
55176 sqlite3EndBenignMalloc();
55177 }else{
55178 /* The journal file exists and no other connection has a reserved
55179 ** or greater lock on the database file. Now check that there is
55180 ** at least one non-zero bytes at the start of the journal file.
55181 ** If there is, then we consider this journal to be hot. If not,
55182 ** it can be ignored.
55183 */
55184 if( !jrnlOpen ){
55185 int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
55186 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
55187 }
55188 if( rc==SQLITE_OK ){
55189 u8 first = 0;
55190 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
55191 if( rc==SQLITE_IOERR_SHORT_READ ){
55192 rc = SQLITE_OK;
55193 }
55194 if( !jrnlOpen ){
55195 sqlite3OsClose(pPager->jfd);
55196 }
55197 *pExists = (first!=0);
55198 }else if( rc==SQLITE_CANTOPEN ){
55199 /* If we cannot open the rollback journal file in order to see if
55200 ** it has a zero header, that might be due to an I/O error, or
55201 ** it might be due to the race condition described above and in
55202 ** ticket #3883. Either way, assume that the journal is hot.
55203 ** This might be a false positive. But if it is, then the
55204 ** automatic journal playback and recovery mechanism will deal
55205 ** with it under an EXCLUSIVE lock where we do not need to
55206 ** worry so much with race conditions.
55207 */
55208 *pExists = 1;
55209 rc = SQLITE_OK;
55210 }
55211 }
55212 }
55213 }
55214 }
55215
55216 return rc;
55217}
55218
55219/*
55220** This function is called to obtain a shared lock on the database file.
55221** It is illegal to call sqlite3PagerGet() until after this function
55222** has been successfully called. If a shared-lock is already held when
55223** this function is called, it is a no-op.
55224**
55225** The following operations are also performed by this function.
55226**
55227** 1) If the pager is currently in PAGER_OPEN state (no lock held
55228** on the database file), then an attempt is made to obtain a
55229** SHARED lock on the database file. Immediately after obtaining
55230** the SHARED lock, the file-system is checked for a hot-journal,
55231** which is played back if present. Following any hot-journal
55232** rollback, the contents of the cache are validated by checking
55233** the 'change-counter' field of the database file header and
55234** discarded if they are found to be invalid.
55235**
55236** 2) If the pager is running in exclusive-mode, and there are currently
55237** no outstanding references to any pages, and is in the error state,
55238** then an attempt is made to clear the error state by discarding
55239** the contents of the page cache and rolling back any open journal
55240** file.
55241**
55242** If everything is successful, SQLITE_OK is returned. If an IO error
55243** occurs while locking the database, checking for a hot-journal file or
55244** rolling back a journal file, the IO error code is returned.
55245*/
55246SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
55247 int rc = SQLITE_OK; /* Return code */
55248
55249 /* This routine is only called from b-tree and only when there are no
55250 ** outstanding pages. This implies that the pager state should either
55251 ** be OPEN or READER. READER is only possible if the pager is or was in
55252 ** exclusive access mode. */
55253 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
55254 assert( assert_pager_state(pPager) );
55255 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
55256 assert( pPager->errCode==SQLITE_OK );
55257
55258 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
55259 int bHotJournal = 1; /* True if there exists a hot journal-file */
55260
55261 assert( !MEMDB );
55262 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
55263
55264 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
55265 if( rc!=SQLITE_OK ){
55266 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
55267 goto failed;
55268 }
55269
55270 /* If a journal file exists, and there is no RESERVED lock on the
55271 ** database file, then it either needs to be played back or deleted.
55272 */
55273 if( pPager->eLock<=SHARED_LOCK ){
55274 rc = hasHotJournal(pPager, &bHotJournal);
55275 }
55276 if( rc!=SQLITE_OK ){
55277 goto failed;
55278 }
55279 if( bHotJournal ){
55280 if( pPager->readOnly ){
55281 rc = SQLITE_READONLY_ROLLBACK;
55282 goto failed;
55283 }
55284
55285 /* Get an EXCLUSIVE lock on the database file. At this point it is
55286 ** important that a RESERVED lock is not obtained on the way to the
55287 ** EXCLUSIVE lock. If it were, another process might open the
55288 ** database file, detect the RESERVED lock, and conclude that the
55289 ** database is safe to read while this process is still rolling the
55290 ** hot-journal back.
55291 **
55292 ** Because the intermediate RESERVED lock is not requested, any
55293 ** other process attempting to access the database file will get to
55294 ** this point in the code and fail to obtain its own EXCLUSIVE lock
55295 ** on the database file.
55296 **
55297 ** Unless the pager is in locking_mode=exclusive mode, the lock is
55298 ** downgraded to SHARED_LOCK before this function returns.
55299 */
55300 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
55301 if( rc!=SQLITE_OK ){
55302 goto failed;
55303 }
55304
55305 /* If it is not already open and the file exists on disk, open the
55306 ** journal for read/write access. Write access is required because
55307 ** in exclusive-access mode the file descriptor will be kept open
55308 ** and possibly used for a transaction later on. Also, write-access
55309 ** is usually required to finalize the journal in journal_mode=persist
55310 ** mode (and also for journal_mode=truncate on some systems).
55311 **
55312 ** If the journal does not exist, it usually means that some
55313 ** other connection managed to get in and roll it back before
55314 ** this connection obtained the exclusive lock above. Or, it
55315 ** may mean that the pager was in the error-state when this
55316 ** function was called and the journal file does not exist.
55317 */
55318 if( !isOpen(pPager->jfd) ){
55319 sqlite3_vfs * const pVfs = pPager->pVfs;
55320 int bExists; /* True if journal file exists */
55321 rc = sqlite3OsAccess(
55322 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
55323 if( rc==SQLITE_OK && bExists ){
55324 int fout = 0;
55325 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
55326 assert( !pPager->tempFile );
55327 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
55328 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
55329 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
55330 rc = SQLITE_CANTOPEN_BKPT;
55331 sqlite3OsClose(pPager->jfd);
55332 }
55333 }
55334 }
55335
55336 /* Playback and delete the journal. Drop the database write
55337 ** lock and reacquire the read lock. Purge the cache before
55338 ** playing back the hot-journal so that we don't end up with
55339 ** an inconsistent cache. Sync the hot journal before playing
55340 ** it back since the process that crashed and left the hot journal
55341 ** probably did not sync it and we are required to always sync
55342 ** the journal before playing it back.
55343 */
55344 if( isOpen(pPager->jfd) ){
55345 assert( rc==SQLITE_OK );
55346 rc = pagerSyncHotJournal(pPager);
55347 if( rc==SQLITE_OK ){
55348 rc = pager_playback(pPager, !pPager->tempFile);
55349 pPager->eState = PAGER_OPEN;
55350 }
55351 }else if( !pPager->exclusiveMode ){
55352 pagerUnlockDb(pPager, SHARED_LOCK);
55353 }
55354
55355 if( rc!=SQLITE_OK ){
55356 /* This branch is taken if an error occurs while trying to open
55357 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
55358 ** pager_unlock() routine will be called before returning to unlock
55359 ** the file. If the unlock attempt fails, then Pager.eLock must be
55360 ** set to UNKNOWN_LOCK (see the comment above the #define for
55361 ** UNKNOWN_LOCK above for an explanation).
55362 **
55363 ** In order to get pager_unlock() to do this, set Pager.eState to
55364 ** PAGER_ERROR now. This is not actually counted as a transition
55365 ** to ERROR state in the state diagram at the top of this file,
55366 ** since we know that the same call to pager_unlock() will very
55367 ** shortly transition the pager object to the OPEN state. Calling
55368 ** assert_pager_state() would fail now, as it should not be possible
55369 ** to be in ERROR state when there are zero outstanding page
55370 ** references.
55371 */
55372 pager_error(pPager, rc);
55373 goto failed;
55374 }
55375
55376 assert( pPager->eState==PAGER_OPEN );
55377 assert( (pPager->eLock==SHARED_LOCK)
55378 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
55379 );
55380 }
55381
55382 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
55383 /* The shared-lock has just been acquired then check to
55384 ** see if the database has been modified. If the database has changed,
55385 ** flush the cache. The hasHeldSharedLock flag prevents this from
55386 ** occurring on the very first access to a file, in order to save a
55387 ** single unnecessary sqlite3OsRead() call at the start-up.
55388 **
55389 ** Database changes are detected by looking at 15 bytes beginning
55390 ** at offset 24 into the file. The first 4 of these 16 bytes are
55391 ** a 32-bit counter that is incremented with each change. The
55392 ** other bytes change randomly with each file change when
55393 ** a codec is in use.
55394 **
55395 ** There is a vanishingly small chance that a change will not be
55396 ** detected. The chance of an undetected change is so small that
55397 ** it can be neglected.
55398 */
55399 char dbFileVers[sizeof(pPager->dbFileVers)];
55400
55401 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
55402 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
55403 if( rc!=SQLITE_OK ){
55404 if( rc!=SQLITE_IOERR_SHORT_READ ){
55405 goto failed;
55406 }
55407 memset(dbFileVers, 0, sizeof(dbFileVers));
55408 }
55409
55410 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
55411 pager_reset(pPager);
55412
55413 /* Unmap the database file. It is possible that external processes
55414 ** may have truncated the database file and then extended it back
55415 ** to its original size while this process was not holding a lock.
55416 ** In this case there may exist a Pager.pMap mapping that appears
55417 ** to be the right size but is not actually valid. Avoid this
55418 ** possibility by unmapping the db here. */
55419 if( USEFETCH(pPager) ){
55420 sqlite3OsUnfetch(pPager->fd, 0, 0);
55421 }
55422 }
55423 }
55424
55425 /* If there is a WAL file in the file-system, open this database in WAL
55426 ** mode. Otherwise, the following function call is a no-op.
55427 */
55428 rc = pagerOpenWalIfPresent(pPager);
55429#ifndef SQLITE_OMIT_WAL
55430 assert( pPager->pWal==0 || rc==SQLITE_OK );
55431#endif
55432 }
55433
55434 if( pagerUseWal(pPager) ){
55435 assert( rc==SQLITE_OK );
55436 rc = pagerBeginReadTransaction(pPager);
55437 }
55438
55439 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
55440 rc = pagerPagecount(pPager, &pPager->dbSize);
55441 }
55442
55443 failed:
55444 if( rc!=SQLITE_OK ){
55445 assert( !MEMDB );
55446 pager_unlock(pPager);
55447 assert( pPager->eState==PAGER_OPEN );
55448 }else{
55449 pPager->eState = PAGER_READER;
55450 pPager->hasHeldSharedLock = 1;
55451 }
55452 return rc;
55453}
55454
55455/*
55456** If the reference count has reached zero, rollback any active
55457** transaction and unlock the pager.
55458**
55459** Except, in locking_mode=EXCLUSIVE when there is nothing to in
55460** the rollback journal, the unlock is not performed and there is
55461** nothing to rollback, so this routine is a no-op.
55462*/
55463static void pagerUnlockIfUnused(Pager *pPager){
55464 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
55465 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
55466 pagerUnlockAndRollback(pPager);
55467 }
55468}
55469
55470/*
55471** The page getter methods each try to acquire a reference to a
55472** page with page number pgno. If the requested reference is
55473** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
55474**
55475** There are different implementations of the getter method depending
55476** on the current state of the pager.
55477**
55478** getPageNormal() -- The normal getter
55479** getPageError() -- Used if the pager is in an error state
55480** getPageMmap() -- Used if memory-mapped I/O is enabled
55481**
55482** If the requested page is already in the cache, it is returned.
55483** Otherwise, a new page object is allocated and populated with data
55484** read from the database file. In some cases, the pcache module may
55485** choose not to allocate a new page object and may reuse an existing
55486** object with no outstanding references.
55487**
55488** The extra data appended to a page is always initialized to zeros the
55489** first time a page is loaded into memory. If the page requested is
55490** already in the cache when this function is called, then the extra
55491** data is left as it was when the page object was last used.
55492**
55493** If the database image is smaller than the requested page or if
55494** the flags parameter contains the PAGER_GET_NOCONTENT bit and the
55495** requested page is not already stored in the cache, then no
55496** actual disk read occurs. In this case the memory image of the
55497** page is initialized to all zeros.
55498**
55499** If PAGER_GET_NOCONTENT is true, it means that we do not care about
55500** the contents of the page. This occurs in two scenarios:
55501**
55502** a) When reading a free-list leaf page from the database, and
55503**
55504** b) When a savepoint is being rolled back and we need to load
55505** a new page into the cache to be filled with the data read
55506** from the savepoint journal.
55507**
55508** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead
55509** of being read from the database. Additionally, the bits corresponding
55510** to pgno in Pager.pInJournal (bitvec of pages already written to the
55511** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
55512** savepoints are set. This means if the page is made writable at any
55513** point in the future, using a call to sqlite3PagerWrite(), its contents
55514** will not be journaled. This saves IO.
55515**
55516** The acquisition might fail for several reasons. In all cases,
55517** an appropriate error code is returned and *ppPage is set to NULL.
55518**
55519** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
55520** to find a page in the in-memory cache first. If the page is not already
55521** in memory, this routine goes to disk to read it in whereas Lookup()
55522** just returns 0. This routine acquires a read-lock the first time it
55523** has to go to disk, and could also playback an old journal if necessary.
55524** Since Lookup() never goes to disk, it never has to deal with locks
55525** or journal files.
55526*/
55527static int getPageNormal(
55528 Pager *pPager, /* The pager open on the database file */
55529 Pgno pgno, /* Page number to fetch */
55530 DbPage **ppPage, /* Write a pointer to the page here */
55531 int flags /* PAGER_GET_XXX flags */
55532){
55533 int rc = SQLITE_OK;
55534 PgHdr *pPg;
55535 u8 noContent; /* True if PAGER_GET_NOCONTENT is set */
55536 sqlite3_pcache_page *pBase;
55537
55538 assert( pPager->errCode==SQLITE_OK );
55539 assert( pPager->eState>=PAGER_READER );
55540 assert( assert_pager_state(pPager) );
55541 assert( pPager->hasHeldSharedLock==1 );
55542
55543 if( pgno==0 ) return SQLITE_CORRUPT_BKPT;
55544 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
55545 if( pBase==0 ){
55546 pPg = 0;
55547 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
55548 if( rc!=SQLITE_OK ) goto pager_acquire_err;
55549 if( pBase==0 ){
55550 rc = SQLITE_NOMEM_BKPT;
55551 goto pager_acquire_err;
55552 }
55553 }
55554 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
55555 assert( pPg==(*ppPage) );
55556 assert( pPg->pgno==pgno );
55557 assert( pPg->pPager==pPager || pPg->pPager==0 );
55558
55559 noContent = (flags & PAGER_GET_NOCONTENT)!=0;
55560 if( pPg->pPager && !noContent ){
55561 /* In this case the pcache already contains an initialized copy of
55562 ** the page. Return without further ado. */
55563 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
55564 pPager->aStat[PAGER_STAT_HIT]++;
55565 return SQLITE_OK;
55566
55567 }else{
55568 /* The pager cache has created a new page. Its content needs to
55569 ** be initialized. But first some error checks:
55570 **
55571 ** (1) The maximum page number is 2^31
55572 ** (2) Never try to fetch the locking page
55573 */
55574 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
55575 rc = SQLITE_CORRUPT_BKPT;
55576 goto pager_acquire_err;
55577 }
55578
55579 pPg->pPager = pPager;
55580
55581 assert( !isOpen(pPager->fd) || !MEMDB );
55582 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
55583 if( pgno>pPager->mxPgno ){
55584 rc = SQLITE_FULL;
55585 goto pager_acquire_err;
55586 }
55587 if( noContent ){
55588 /* Failure to set the bits in the InJournal bit-vectors is benign.
55589 ** It merely means that we might do some extra work to journal a
55590 ** page that does not need to be journaled. Nevertheless, be sure
55591 ** to test the case where a malloc error occurs while trying to set
55592 ** a bit in a bit vector.
55593 */
55594 sqlite3BeginBenignMalloc();
55595 if( pgno<=pPager->dbOrigSize ){
55596 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
55597 testcase( rc==SQLITE_NOMEM );
55598 }
55599 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
55600 testcase( rc==SQLITE_NOMEM );
55601 sqlite3EndBenignMalloc();
55602 }
55603 memset(pPg->pData, 0, pPager->pageSize);
55604 IOTRACE(("ZERO %p %d\n", pPager, pgno));
55605 }else{
55606 assert( pPg->pPager==pPager );
55607 pPager->aStat[PAGER_STAT_MISS]++;
55608 rc = readDbPage(pPg);
55609 if( rc!=SQLITE_OK ){
55610 goto pager_acquire_err;
55611 }
55612 }
55613 pager_set_pagehash(pPg);
55614 }
55615 return SQLITE_OK;
55616
55617pager_acquire_err:
55618 assert( rc!=SQLITE_OK );
55619 if( pPg ){
55620 sqlite3PcacheDrop(pPg);
55621 }
55622 pagerUnlockIfUnused(pPager);
55623 *ppPage = 0;
55624 return rc;
55625}
55626
55627#if SQLITE_MAX_MMAP_SIZE>0
55628/* The page getter for when memory-mapped I/O is enabled */
55629static int getPageMMap(
55630 Pager *pPager, /* The pager open on the database file */
55631 Pgno pgno, /* Page number to fetch */
55632 DbPage **ppPage, /* Write a pointer to the page here */
55633 int flags /* PAGER_GET_XXX flags */
55634){
55635 int rc = SQLITE_OK;
55636 PgHdr *pPg = 0;
55637 u32 iFrame = 0; /* Frame to read from WAL file */
55638
55639 /* It is acceptable to use a read-only (mmap) page for any page except
55640 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
55641 ** flag was specified by the caller. And so long as the db is not a
55642 ** temporary or in-memory database. */
55643 const int bMmapOk = (pgno>1
55644 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
55645 );
55646
55647 assert( USEFETCH(pPager) );
55648#ifdef SQLITE_HAS_CODEC
55649 assert( pPager->xCodec==0 );
55650#endif
55651
55652 /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here
55653 ** allows the compiler optimizer to reuse the results of the "pgno>1"
55654 ** test in the previous statement, and avoid testing pgno==0 in the
55655 ** common case where pgno is large. */
55656 if( pgno<=1 && pgno==0 ){
55657 return SQLITE_CORRUPT_BKPT;
55658 }
55659 assert( pPager->eState>=PAGER_READER );
55660 assert( assert_pager_state(pPager) );
55661 assert( pPager->hasHeldSharedLock==1 );
55662 assert( pPager->errCode==SQLITE_OK );
55663
55664 if( bMmapOk && pagerUseWal(pPager) ){
55665 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
55666 if( rc!=SQLITE_OK ){
55667 *ppPage = 0;
55668 return rc;
55669 }
55670 }
55671 if( bMmapOk && iFrame==0 ){
55672 void *pData = 0;
55673 rc = sqlite3OsFetch(pPager->fd,
55674 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
55675 );
55676 if( rc==SQLITE_OK && pData ){
55677 if( pPager->eState>PAGER_READER || pPager->tempFile ){
55678 pPg = sqlite3PagerLookup(pPager, pgno);
55679 }
55680 if( pPg==0 ){
55681 rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
55682 }else{
55683 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
55684 }
55685 if( pPg ){
55686 assert( rc==SQLITE_OK );
55687 *ppPage = pPg;
55688 return SQLITE_OK;
55689 }
55690 }
55691 if( rc!=SQLITE_OK ){
55692 *ppPage = 0;
55693 return rc;
55694 }
55695 }
55696 return getPageNormal(pPager, pgno, ppPage, flags);
55697}
55698#endif /* SQLITE_MAX_MMAP_SIZE>0 */
55699
55700/* The page getter method for when the pager is an error state */
55701static int getPageError(
55702 Pager *pPager, /* The pager open on the database file */
55703 Pgno pgno, /* Page number to fetch */
55704 DbPage **ppPage, /* Write a pointer to the page here */
55705 int flags /* PAGER_GET_XXX flags */
55706){
55707 UNUSED_PARAMETER(pgno);
55708 UNUSED_PARAMETER(flags);
55709 assert( pPager->errCode!=SQLITE_OK );
55710 *ppPage = 0;
55711 return pPager->errCode;
55712}
55713
55714
55715/* Dispatch all page fetch requests to the appropriate getter method.
55716*/
55717SQLITE_PRIVATE int sqlite3PagerGet(
55718 Pager *pPager, /* The pager open on the database file */
55719 Pgno pgno, /* Page number to fetch */
55720 DbPage **ppPage, /* Write a pointer to the page here */
55721 int flags /* PAGER_GET_XXX flags */
55722){
55723 return pPager->xGet(pPager, pgno, ppPage, flags);
55724}
55725
55726/*
55727** Acquire a page if it is already in the in-memory cache. Do
55728** not read the page from disk. Return a pointer to the page,
55729** or 0 if the page is not in cache.
55730**
55731** See also sqlite3PagerGet(). The difference between this routine
55732** and sqlite3PagerGet() is that _get() will go to the disk and read
55733** in the page if the page is not already in cache. This routine
55734** returns NULL if the page is not in cache or if a disk I/O error
55735** has ever happened.
55736*/
55737SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
55738 sqlite3_pcache_page *pPage;
55739 assert( pPager!=0 );
55740 assert( pgno!=0 );
55741 assert( pPager->pPCache!=0 );
55742 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
55743 assert( pPage==0 || pPager->hasHeldSharedLock );
55744 if( pPage==0 ) return 0;
55745 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
55746}
55747
55748/*
55749** Release a page reference.
55750**
55751** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be
55752** used if we know that the page being released is not the last page.
55753** The btree layer always holds page1 open until the end, so these first
55754** to routines can be used to release any page other than BtShared.pPage1.
55755**
55756** Use sqlite3PagerUnrefPageOne() to release page1. This latter routine
55757** checks the total number of outstanding pages and if the number of
55758** pages reaches zero it drops the database lock.
55759*/
55760SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
55761 TESTONLY( Pager *pPager = pPg->pPager; )
55762 assert( pPg!=0 );
55763 if( pPg->flags & PGHDR_MMAP ){
55764 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
55765 pagerReleaseMapPage(pPg);
55766 }else{
55767 sqlite3PcacheRelease(pPg);
55768 }
55769 /* Do not use this routine to release the last reference to page1 */
55770 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
55771}
55772SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
55773 if( pPg ) sqlite3PagerUnrefNotNull(pPg);
55774}
55775SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage *pPg){
55776 Pager *pPager;
55777 assert( pPg!=0 );
55778 assert( pPg->pgno==1 );
55779 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
55780 pPager = pPg->pPager;
55781 sqlite3PagerResetLockTimeout(pPager);
55782 sqlite3PcacheRelease(pPg);
55783 pagerUnlockIfUnused(pPager);
55784}
55785
55786/*
55787** This function is called at the start of every write transaction.
55788** There must already be a RESERVED or EXCLUSIVE lock on the database
55789** file when this routine is called.
55790**
55791** Open the journal file for pager pPager and write a journal header
55792** to the start of it. If there are active savepoints, open the sub-journal
55793** as well. This function is only used when the journal file is being
55794** opened to write a rollback log for a transaction. It is not used
55795** when opening a hot journal file to roll it back.
55796**
55797** If the journal file is already open (as it may be in exclusive mode),
55798** then this function just writes a journal header to the start of the
55799** already open file.
55800**
55801** Whether or not the journal file is opened by this function, the
55802** Pager.pInJournal bitvec structure is allocated.
55803**
55804** Return SQLITE_OK if everything is successful. Otherwise, return
55805** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
55806** an IO error code if opening or writing the journal file fails.
55807*/
55808static int pager_open_journal(Pager *pPager){
55809 int rc = SQLITE_OK; /* Return code */
55810 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
55811
55812 assert( pPager->eState==PAGER_WRITER_LOCKED );
55813 assert( assert_pager_state(pPager) );
55814 assert( pPager->pInJournal==0 );
55815
55816 /* If already in the error state, this function is a no-op. But on
55817 ** the other hand, this routine is never called if we are already in
55818 ** an error state. */
55819 if( NEVER(pPager->errCode) ) return pPager->errCode;
55820
55821 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
55822 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
55823 if( pPager->pInJournal==0 ){
55824 return SQLITE_NOMEM_BKPT;
55825 }
55826
55827 /* Open the journal file if it is not already open. */
55828 if( !isOpen(pPager->jfd) ){
55829 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
55830 sqlite3MemJournalOpen(pPager->jfd);
55831 }else{
55832 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
55833 int nSpill;
55834
55835 if( pPager->tempFile ){
55836 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
55837 nSpill = sqlite3Config.nStmtSpill;
55838 }else{
55839 flags |= SQLITE_OPEN_MAIN_JOURNAL;
55840 nSpill = jrnlBufferSize(pPager);
55841 }
55842
55843 /* Verify that the database still has the same name as it did when
55844 ** it was originally opened. */
55845 rc = databaseIsUnmoved(pPager);
55846 if( rc==SQLITE_OK ){
55847 rc = sqlite3JournalOpen (
55848 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
55849 );
55850 }
55851 }
55852 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
55853 }
55854
55855
55856 /* Write the first journal header to the journal file and open
55857 ** the sub-journal if necessary.
55858 */
55859 if( rc==SQLITE_OK ){
55860 /* TODO: Check if all of these are really required. */
55861 pPager->nRec = 0;
55862 pPager->journalOff = 0;
55863 pPager->setMaster = 0;
55864 pPager->journalHdr = 0;
55865 rc = writeJournalHdr(pPager);
55866 }
55867 }
55868
55869 if( rc!=SQLITE_OK ){
55870 sqlite3BitvecDestroy(pPager->pInJournal);
55871 pPager->pInJournal = 0;
55872 }else{
55873 assert( pPager->eState==PAGER_WRITER_LOCKED );
55874 pPager->eState = PAGER_WRITER_CACHEMOD;
55875 }
55876
55877 return rc;
55878}
55879
55880/*
55881** Begin a write-transaction on the specified pager object. If a
55882** write-transaction has already been opened, this function is a no-op.
55883**
55884** If the exFlag argument is false, then acquire at least a RESERVED
55885** lock on the database file. If exFlag is true, then acquire at least
55886** an EXCLUSIVE lock. If such a lock is already held, no locking
55887** functions need be called.
55888**
55889** If the subjInMemory argument is non-zero, then any sub-journal opened
55890** within this transaction will be opened as an in-memory file. This
55891** has no effect if the sub-journal is already opened (as it may be when
55892** running in exclusive mode) or if the transaction does not require a
55893** sub-journal. If the subjInMemory argument is zero, then any required
55894** sub-journal is implemented in-memory if pPager is an in-memory database,
55895** or using a temporary file otherwise.
55896*/
55897SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
55898 int rc = SQLITE_OK;
55899
55900 if( pPager->errCode ) return pPager->errCode;
55901 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
55902 pPager->subjInMemory = (u8)subjInMemory;
55903
55904 if( ALWAYS(pPager->eState==PAGER_READER) ){
55905 assert( pPager->pInJournal==0 );
55906
55907 if( pagerUseWal(pPager) ){
55908 /* If the pager is configured to use locking_mode=exclusive, and an
55909 ** exclusive lock on the database is not already held, obtain it now.
55910 */
55911 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
55912 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
55913 if( rc!=SQLITE_OK ){
55914 return rc;
55915 }
55916 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
55917 }
55918
55919 /* Grab the write lock on the log file. If successful, upgrade to
55920 ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
55921 ** The busy-handler is not invoked if another connection already
55922 ** holds the write-lock. If possible, the upper layer will call it.
55923 */
55924 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
55925 }else{
55926 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
55927 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
55928 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
55929 ** lock, but not when obtaining the RESERVED lock.
55930 */
55931 rc = pagerLockDb(pPager, RESERVED_LOCK);
55932 if( rc==SQLITE_OK && exFlag ){
55933 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
55934 }
55935 }
55936
55937 if( rc==SQLITE_OK ){
55938 /* Change to WRITER_LOCKED state.
55939 **
55940 ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
55941 ** when it has an open transaction, but never to DBMOD or FINISHED.
55942 ** This is because in those states the code to roll back savepoint
55943 ** transactions may copy data from the sub-journal into the database
55944 ** file as well as into the page cache. Which would be incorrect in
55945 ** WAL mode.
55946 */
55947 pPager->eState = PAGER_WRITER_LOCKED;
55948 pPager->dbHintSize = pPager->dbSize;
55949 pPager->dbFileSize = pPager->dbSize;
55950 pPager->dbOrigSize = pPager->dbSize;
55951 pPager->journalOff = 0;
55952 }
55953
55954 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
55955 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
55956 assert( assert_pager_state(pPager) );
55957 }
55958
55959 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
55960 return rc;
55961}
55962
55963/*
55964** Write page pPg onto the end of the rollback journal.
55965*/
55966static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){
55967 Pager *pPager = pPg->pPager;
55968 int rc;
55969 u32 cksum;
55970 char *pData2;
55971 i64 iOff = pPager->journalOff;
55972
55973 /* We should never write to the journal file the page that
55974 ** contains the database locks. The following assert verifies
55975 ** that we do not. */
55976 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
55977
55978 assert( pPager->journalHdr<=pPager->journalOff );
55979 CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
55980 cksum = pager_cksum(pPager, (u8*)pData2);
55981
55982 /* Even if an IO or diskfull error occurs while journalling the
55983 ** page in the block above, set the need-sync flag for the page.
55984 ** Otherwise, when the transaction is rolled back, the logic in
55985 ** playback_one_page() will think that the page needs to be restored
55986 ** in the database file. And if an IO error occurs while doing so,
55987 ** then corruption may follow.
55988 */
55989 pPg->flags |= PGHDR_NEED_SYNC;
55990
55991 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
55992 if( rc!=SQLITE_OK ) return rc;
55993 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
55994 if( rc!=SQLITE_OK ) return rc;
55995 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
55996 if( rc!=SQLITE_OK ) return rc;
55997
55998 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
55999 pPager->journalOff, pPager->pageSize));
56000 PAGER_INCR(sqlite3_pager_writej_count);
56001 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
56002 PAGERID(pPager), pPg->pgno,
56003 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
56004
56005 pPager->journalOff += 8 + pPager->pageSize;
56006 pPager->nRec++;
56007 assert( pPager->pInJournal!=0 );
56008 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
56009 testcase( rc==SQLITE_NOMEM );
56010 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
56011 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
56012 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
56013 return rc;
56014}
56015
56016/*
56017** Mark a single data page as writeable. The page is written into the
56018** main journal or sub-journal as required. If the page is written into
56019** one of the journals, the corresponding bit is set in the
56020** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
56021** of any open savepoints as appropriate.
56022*/
56023static int pager_write(PgHdr *pPg){
56024 Pager *pPager = pPg->pPager;
56025 int rc = SQLITE_OK;
56026
56027 /* This routine is not called unless a write-transaction has already
56028 ** been started. The journal file may or may not be open at this point.
56029 ** It is never called in the ERROR state.
56030 */
56031 assert( pPager->eState==PAGER_WRITER_LOCKED
56032 || pPager->eState==PAGER_WRITER_CACHEMOD
56033 || pPager->eState==PAGER_WRITER_DBMOD
56034 );
56035 assert( assert_pager_state(pPager) );
56036 assert( pPager->errCode==0 );
56037 assert( pPager->readOnly==0 );
56038 CHECK_PAGE(pPg);
56039
56040 /* The journal file needs to be opened. Higher level routines have already
56041 ** obtained the necessary locks to begin the write-transaction, but the
56042 ** rollback journal might not yet be open. Open it now if this is the case.
56043 **
56044 ** This is done before calling sqlite3PcacheMakeDirty() on the page.
56045 ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
56046 ** an error might occur and the pager would end up in WRITER_LOCKED state
56047 ** with pages marked as dirty in the cache.
56048 */
56049 if( pPager->eState==PAGER_WRITER_LOCKED ){
56050 rc = pager_open_journal(pPager);
56051 if( rc!=SQLITE_OK ) return rc;
56052 }
56053 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
56054 assert( assert_pager_state(pPager) );
56055
56056 /* Mark the page that is about to be modified as dirty. */
56057 sqlite3PcacheMakeDirty(pPg);
56058
56059 /* If a rollback journal is in use, them make sure the page that is about
56060 ** to change is in the rollback journal, or if the page is a new page off
56061 ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
56062 */
56063 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
56064 if( pPager->pInJournal!=0
56065 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
56066 ){
56067 assert( pagerUseWal(pPager)==0 );
56068 if( pPg->pgno<=pPager->dbOrigSize ){
56069 rc = pagerAddPageToRollbackJournal(pPg);
56070 if( rc!=SQLITE_OK ){
56071 return rc;
56072 }
56073 }else{
56074 if( pPager->eState!=PAGER_WRITER_DBMOD ){
56075 pPg->flags |= PGHDR_NEED_SYNC;
56076 }
56077 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
56078 PAGERID(pPager), pPg->pgno,
56079 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
56080 }
56081 }
56082
56083 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
56084 ** and before writing the page into the rollback journal. Wait until now,
56085 ** after the page has been successfully journalled, before setting the
56086 ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
56087 */
56088 pPg->flags |= PGHDR_WRITEABLE;
56089
56090 /* If the statement journal is open and the page is not in it,
56091 ** then write the page into the statement journal.
56092 */
56093 if( pPager->nSavepoint>0 ){
56094 rc = subjournalPageIfRequired(pPg);
56095 }
56096
56097 /* Update the database size and return. */
56098 if( pPager->dbSize<pPg->pgno ){
56099 pPager->dbSize = pPg->pgno;
56100 }
56101 return rc;
56102}
56103
56104/*
56105** This is a variant of sqlite3PagerWrite() that runs when the sector size
56106** is larger than the page size. SQLite makes the (reasonable) assumption that
56107** all bytes of a sector are written together by hardware. Hence, all bytes of
56108** a sector need to be journalled in case of a power loss in the middle of
56109** a write.
56110**
56111** Usually, the sector size is less than or equal to the page size, in which
56112** case pages can be individually written. This routine only runs in the
56113** exceptional case where the page size is smaller than the sector size.
56114*/
56115static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
56116 int rc = SQLITE_OK; /* Return code */
56117 Pgno nPageCount; /* Total number of pages in database file */
56118 Pgno pg1; /* First page of the sector pPg is located on. */
56119 int nPage = 0; /* Number of pages starting at pg1 to journal */
56120 int ii; /* Loop counter */
56121 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
56122 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
56123 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
56124
56125 /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
56126 ** a journal header to be written between the pages journaled by
56127 ** this function.
56128 */
56129 assert( !MEMDB );
56130 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
56131 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
56132
56133 /* This trick assumes that both the page-size and sector-size are
56134 ** an integer power of 2. It sets variable pg1 to the identifier
56135 ** of the first page of the sector pPg is located on.
56136 */
56137 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
56138
56139 nPageCount = pPager->dbSize;
56140 if( pPg->pgno>nPageCount ){
56141 nPage = (pPg->pgno - pg1)+1;
56142 }else if( (pg1+nPagePerSector-1)>nPageCount ){
56143 nPage = nPageCount+1-pg1;
56144 }else{
56145 nPage = nPagePerSector;
56146 }
56147 assert(nPage>0);
56148 assert(pg1<=pPg->pgno);
56149 assert((pg1+nPage)>pPg->pgno);
56150
56151 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
56152 Pgno pg = pg1+ii;
56153 PgHdr *pPage;
56154 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
56155 if( pg!=PAGER_MJ_PGNO(pPager) ){
56156 rc = sqlite3PagerGet(pPager, pg, &pPage, 0);
56157 if( rc==SQLITE_OK ){
56158 rc = pager_write(pPage);
56159 if( pPage->flags&PGHDR_NEED_SYNC ){
56160 needSync = 1;
56161 }
56162 sqlite3PagerUnrefNotNull(pPage);
56163 }
56164 }
56165 }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){
56166 if( pPage->flags&PGHDR_NEED_SYNC ){
56167 needSync = 1;
56168 }
56169 sqlite3PagerUnrefNotNull(pPage);
56170 }
56171 }
56172
56173 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
56174 ** starting at pg1, then it needs to be set for all of them. Because
56175 ** writing to any of these nPage pages may damage the others, the
56176 ** journal file must contain sync()ed copies of all of them
56177 ** before any of them can be written out to the database file.
56178 */
56179 if( rc==SQLITE_OK && needSync ){
56180 assert( !MEMDB );
56181 for(ii=0; ii<nPage; ii++){
56182 PgHdr *pPage = sqlite3PagerLookup(pPager, pg1+ii);
56183 if( pPage ){
56184 pPage->flags |= PGHDR_NEED_SYNC;
56185 sqlite3PagerUnrefNotNull(pPage);
56186 }
56187 }
56188 }
56189
56190 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
56191 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
56192 return rc;
56193}
56194
56195/*
56196** Mark a data page as writeable. This routine must be called before
56197** making changes to a page. The caller must check the return value
56198** of this function and be careful not to change any page data unless
56199** this routine returns SQLITE_OK.
56200**
56201** The difference between this function and pager_write() is that this
56202** function also deals with the special case where 2 or more pages
56203** fit on a single disk sector. In this case all co-resident pages
56204** must have been written to the journal file before returning.
56205**
56206** If an error occurs, SQLITE_NOMEM or an IO error code is returned
56207** as appropriate. Otherwise, SQLITE_OK.
56208*/
56209SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){
56210 Pager *pPager = pPg->pPager;
56211 assert( (pPg->flags & PGHDR_MMAP)==0 );
56212 assert( pPager->eState>=PAGER_WRITER_LOCKED );
56213 assert( assert_pager_state(pPager) );
56214 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
56215 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
56216 return SQLITE_OK;
56217 }else if( pPager->errCode ){
56218 return pPager->errCode;
56219 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
56220 assert( pPager->tempFile==0 );
56221 return pagerWriteLargeSector(pPg);
56222 }else{
56223 return pager_write(pPg);
56224 }
56225}
56226
56227/*
56228** Return TRUE if the page given in the argument was previously passed
56229** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
56230** to change the content of the page.
56231*/
56232#ifndef NDEBUG
56233SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
56234 return pPg->flags & PGHDR_WRITEABLE;
56235}
56236#endif
56237
56238/*
56239** A call to this routine tells the pager that it is not necessary to
56240** write the information on page pPg back to the disk, even though
56241** that page might be marked as dirty. This happens, for example, when
56242** the page has been added as a leaf of the freelist and so its
56243** content no longer matters.
56244**
56245** The overlying software layer calls this routine when all of the data
56246** on the given page is unused. The pager marks the page as clean so
56247** that it does not get written to disk.
56248**
56249** Tests show that this optimization can quadruple the speed of large
56250** DELETE operations.
56251**
56252** This optimization cannot be used with a temp-file, as the page may
56253** have been dirty at the start of the transaction. In that case, if
56254** memory pressure forces page pPg out of the cache, the data does need
56255** to be written out to disk so that it may be read back in if the
56256** current transaction is rolled back.
56257*/
56258SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
56259 Pager *pPager = pPg->pPager;
56260 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
56261 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
56262 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
56263 pPg->flags |= PGHDR_DONT_WRITE;
56264 pPg->flags &= ~PGHDR_WRITEABLE;
56265 testcase( pPg->flags & PGHDR_NEED_SYNC );
56266 pager_set_pagehash(pPg);
56267 }
56268}
56269
56270/*
56271** This routine is called to increment the value of the database file
56272** change-counter, stored as a 4-byte big-endian integer starting at
56273** byte offset 24 of the pager file. The secondary change counter at
56274** 92 is also updated, as is the SQLite version number at offset 96.
56275**
56276** But this only happens if the pPager->changeCountDone flag is false.
56277** To avoid excess churning of page 1, the update only happens once.
56278** See also the pager_write_changecounter() routine that does an
56279** unconditional update of the change counters.
56280**
56281** If the isDirectMode flag is zero, then this is done by calling
56282** sqlite3PagerWrite() on page 1, then modifying the contents of the
56283** page data. In this case the file will be updated when the current
56284** transaction is committed.
56285**
56286** The isDirectMode flag may only be non-zero if the library was compiled
56287** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
56288** if isDirect is non-zero, then the database file is updated directly
56289** by writing an updated version of page 1 using a call to the
56290** sqlite3OsWrite() function.
56291*/
56292static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
56293 int rc = SQLITE_OK;
56294
56295 assert( pPager->eState==PAGER_WRITER_CACHEMOD
56296 || pPager->eState==PAGER_WRITER_DBMOD
56297 );
56298 assert( assert_pager_state(pPager) );
56299
56300 /* Declare and initialize constant integer 'isDirect'. If the
56301 ** atomic-write optimization is enabled in this build, then isDirect
56302 ** is initialized to the value passed as the isDirectMode parameter
56303 ** to this function. Otherwise, it is always set to zero.
56304 **
56305 ** The idea is that if the atomic-write optimization is not
56306 ** enabled at compile time, the compiler can omit the tests of
56307 ** 'isDirect' below, as well as the block enclosed in the
56308 ** "if( isDirect )" condition.
56309 */
56310#ifndef SQLITE_ENABLE_ATOMIC_WRITE
56311# define DIRECT_MODE 0
56312 assert( isDirectMode==0 );
56313 UNUSED_PARAMETER(isDirectMode);
56314#else
56315# define DIRECT_MODE isDirectMode
56316#endif
56317
56318 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
56319 PgHdr *pPgHdr; /* Reference to page 1 */
56320
56321 assert( !pPager->tempFile && isOpen(pPager->fd) );
56322
56323 /* Open page 1 of the file for writing. */
56324 rc = sqlite3PagerGet(pPager, 1, &pPgHdr, 0);
56325 assert( pPgHdr==0 || rc==SQLITE_OK );
56326
56327 /* If page one was fetched successfully, and this function is not
56328 ** operating in direct-mode, make page 1 writable. When not in
56329 ** direct mode, page 1 is always held in cache and hence the PagerGet()
56330 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
56331 */
56332 if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
56333 rc = sqlite3PagerWrite(pPgHdr);
56334 }
56335
56336 if( rc==SQLITE_OK ){
56337 /* Actually do the update of the change counter */
56338 pager_write_changecounter(pPgHdr);
56339
56340 /* If running in direct mode, write the contents of page 1 to the file. */
56341 if( DIRECT_MODE ){
56342 const void *zBuf;
56343 assert( pPager->dbFileSize>0 );
56344 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf);
56345 if( rc==SQLITE_OK ){
56346 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
56347 pPager->aStat[PAGER_STAT_WRITE]++;
56348 }
56349 if( rc==SQLITE_OK ){
56350 /* Update the pager's copy of the change-counter. Otherwise, the
56351 ** next time a read transaction is opened the cache will be
56352 ** flushed (as the change-counter values will not match). */
56353 const void *pCopy = (const void *)&((const char *)zBuf)[24];
56354 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
56355 pPager->changeCountDone = 1;
56356 }
56357 }else{
56358 pPager->changeCountDone = 1;
56359 }
56360 }
56361
56362 /* Release the page reference. */
56363 sqlite3PagerUnref(pPgHdr);
56364 }
56365 return rc;
56366}
56367
56368/*
56369** Sync the database file to disk. This is a no-op for in-memory databases
56370** or pages with the Pager.noSync flag set.
56371**
56372** If successful, or if called on a pager for which it is a no-op, this
56373** function returns SQLITE_OK. Otherwise, an IO error code is returned.
56374*/
56375SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
56376 int rc = SQLITE_OK;
56377 void *pArg = (void*)zMaster;
56378 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
56379 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
56380 if( rc==SQLITE_OK && !pPager->noSync ){
56381 assert( !MEMDB );
56382 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
56383 }
56384 return rc;
56385}
56386
56387/*
56388** This function may only be called while a write-transaction is active in
56389** rollback. If the connection is in WAL mode, this call is a no-op.
56390** Otherwise, if the connection does not already have an EXCLUSIVE lock on
56391** the database file, an attempt is made to obtain one.
56392**
56393** If the EXCLUSIVE lock is already held or the attempt to obtain it is
56394** successful, or the connection is in WAL mode, SQLITE_OK is returned.
56395** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
56396** returned.
56397*/
56398SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
56399 int rc = pPager->errCode;
56400 assert( assert_pager_state(pPager) );
56401 if( rc==SQLITE_OK ){
56402 assert( pPager->eState==PAGER_WRITER_CACHEMOD
56403 || pPager->eState==PAGER_WRITER_DBMOD
56404 || pPager->eState==PAGER_WRITER_LOCKED
56405 );
56406 assert( assert_pager_state(pPager) );
56407 if( 0==pagerUseWal(pPager) ){
56408 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
56409 }
56410 }
56411 return rc;
56412}
56413
56414/*
56415** Sync the database file for the pager pPager. zMaster points to the name
56416** of a master journal file that should be written into the individual
56417** journal file. zMaster may be NULL, which is interpreted as no master
56418** journal (a single database transaction).
56419**
56420** This routine ensures that:
56421**
56422** * The database file change-counter is updated,
56423** * the journal is synced (unless the atomic-write optimization is used),
56424** * all dirty pages are written to the database file,
56425** * the database file is truncated (if required), and
56426** * the database file synced.
56427**
56428** The only thing that remains to commit the transaction is to finalize
56429** (delete, truncate or zero the first part of) the journal file (or
56430** delete the master journal file if specified).
56431**
56432** Note that if zMaster==NULL, this does not overwrite a previous value
56433** passed to an sqlite3PagerCommitPhaseOne() call.
56434**
56435** If the final parameter - noSync - is true, then the database file itself
56436** is not synced. The caller must call sqlite3PagerSync() directly to
56437** sync the database file before calling CommitPhaseTwo() to delete the
56438** journal file in this case.
56439*/
56440SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
56441 Pager *pPager, /* Pager object */
56442 const char *zMaster, /* If not NULL, the master journal name */
56443 int noSync /* True to omit the xSync on the db file */
56444){
56445 int rc = SQLITE_OK; /* Return code */
56446
56447 assert( pPager->eState==PAGER_WRITER_LOCKED
56448 || pPager->eState==PAGER_WRITER_CACHEMOD
56449 || pPager->eState==PAGER_WRITER_DBMOD
56450 || pPager->eState==PAGER_ERROR
56451 );
56452 assert( assert_pager_state(pPager) );
56453
56454 /* If a prior error occurred, report that error again. */
56455 if( NEVER(pPager->errCode) ) return pPager->errCode;
56456
56457 /* Provide the ability to easily simulate an I/O error during testing */
56458 if( sqlite3FaultSim(400) ) return SQLITE_IOERR;
56459
56460 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
56461 pPager->zFilename, zMaster, pPager->dbSize));
56462
56463 /* If no database changes have been made, return early. */
56464 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
56465
56466 assert( MEMDB==0 || pPager->tempFile );
56467 assert( isOpen(pPager->fd) || pPager->tempFile );
56468 if( 0==pagerFlushOnCommit(pPager, 1) ){
56469 /* If this is an in-memory db, or no pages have been written to, or this
56470 ** function has already been called, it is mostly a no-op. However, any
56471 ** backup in progress needs to be restarted. */
56472 sqlite3BackupRestart(pPager->pBackup);
56473 }else{
56474 PgHdr *pList;
56475 if( pagerUseWal(pPager) ){
56476 PgHdr *pPageOne = 0;
56477 pList = sqlite3PcacheDirtyList(pPager->pPCache);
56478 if( pList==0 ){
56479 /* Must have at least one page for the WAL commit flag.
56480 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
56481 rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0);
56482 pList = pPageOne;
56483 pList->pDirty = 0;
56484 }
56485 assert( rc==SQLITE_OK );
56486 if( ALWAYS(pList) ){
56487 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
56488 }
56489 sqlite3PagerUnref(pPageOne);
56490 if( rc==SQLITE_OK ){
56491 sqlite3PcacheCleanAll(pPager->pPCache);
56492 }
56493 }else{
56494 /* The bBatch boolean is true if the batch-atomic-write commit method
56495 ** should be used. No rollback journal is created if batch-atomic-write
56496 ** is enabled.
56497 */
56498#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
56499 sqlite3_file *fd = pPager->fd;
56500 int bBatch = zMaster==0 /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
56501 && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC)
56502 && !pPager->noSync
56503 && sqlite3JournalIsInMemory(pPager->jfd);
56504#else
56505# define bBatch 0
56506#endif
56507
56508#ifdef SQLITE_ENABLE_ATOMIC_WRITE
56509 /* The following block updates the change-counter. Exactly how it
56510 ** does this depends on whether or not the atomic-update optimization
56511 ** was enabled at compile time, and if this transaction meets the
56512 ** runtime criteria to use the operation:
56513 **
56514 ** * The file-system supports the atomic-write property for
56515 ** blocks of size page-size, and
56516 ** * This commit is not part of a multi-file transaction, and
56517 ** * Exactly one page has been modified and store in the journal file.
56518 **
56519 ** If the optimization was not enabled at compile time, then the
56520 ** pager_incr_changecounter() function is called to update the change
56521 ** counter in 'indirect-mode'. If the optimization is compiled in but
56522 ** is not applicable to this transaction, call sqlite3JournalCreate()
56523 ** to make sure the journal file has actually been created, then call
56524 ** pager_incr_changecounter() to update the change-counter in indirect
56525 ** mode.
56526 **
56527 ** Otherwise, if the optimization is both enabled and applicable,
56528 ** then call pager_incr_changecounter() to update the change-counter
56529 ** in 'direct' mode. In this case the journal file will never be
56530 ** created for this transaction.
56531 */
56532 if( bBatch==0 ){
56533 PgHdr *pPg;
56534 assert( isOpen(pPager->jfd)
56535 || pPager->journalMode==PAGER_JOURNALMODE_OFF
56536 || pPager->journalMode==PAGER_JOURNALMODE_WAL
56537 );
56538 if( !zMaster && isOpen(pPager->jfd)
56539 && pPager->journalOff==jrnlBufferSize(pPager)
56540 && pPager->dbSize>=pPager->dbOrigSize
56541 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
56542 ){
56543 /* Update the db file change counter via the direct-write method. The
56544 ** following call will modify the in-memory representation of page 1
56545 ** to include the updated change counter and then write page 1
56546 ** directly to the database file. Because of the atomic-write
56547 ** property of the host file-system, this is safe.
56548 */
56549 rc = pager_incr_changecounter(pPager, 1);
56550 }else{
56551 rc = sqlite3JournalCreate(pPager->jfd);
56552 if( rc==SQLITE_OK ){
56553 rc = pager_incr_changecounter(pPager, 0);
56554 }
56555 }
56556 }
56557#else /* SQLITE_ENABLE_ATOMIC_WRITE */
56558#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
56559 if( zMaster ){
56560 rc = sqlite3JournalCreate(pPager->jfd);
56561 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
56562 assert( bBatch==0 );
56563 }
56564#endif
56565 rc = pager_incr_changecounter(pPager, 0);
56566#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */
56567 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
56568
56569 /* Write the master journal name into the journal file. If a master
56570 ** journal file name has already been written to the journal file,
56571 ** or if zMaster is NULL (no master journal), then this call is a no-op.
56572 */
56573 rc = writeMasterJournal(pPager, zMaster);
56574 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
56575
56576 /* Sync the journal file and write all dirty pages to the database.
56577 ** If the atomic-update optimization is being used, this sync will not
56578 ** create the journal file or perform any real IO.
56579 **
56580 ** Because the change-counter page was just modified, unless the
56581 ** atomic-update optimization is used it is almost certain that the
56582 ** journal requires a sync here. However, in locking_mode=exclusive
56583 ** on a system under memory pressure it is just possible that this is
56584 ** not the case. In this case it is likely enough that the redundant
56585 ** xSync() call will be changed to a no-op by the OS anyhow.
56586 */
56587 rc = syncJournal(pPager, 0);
56588 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
56589
56590 pList = sqlite3PcacheDirtyList(pPager->pPCache);
56591#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
56592 if( bBatch ){
56593 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
56594 if( rc==SQLITE_OK ){
56595 rc = pager_write_pagelist(pPager, pList);
56596 if( rc==SQLITE_OK ){
56597 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
56598 }
56599 if( rc!=SQLITE_OK ){
56600 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
56601 }
56602 }
56603
56604 if( (rc&0xFF)==SQLITE_IOERR && rc!=SQLITE_IOERR_NOMEM ){
56605 rc = sqlite3JournalCreate(pPager->jfd);
56606 if( rc!=SQLITE_OK ){
56607 sqlite3OsClose(pPager->jfd);
56608 goto commit_phase_one_exit;
56609 }
56610 bBatch = 0;
56611 }else{
56612 sqlite3OsClose(pPager->jfd);
56613 }
56614 }
56615#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
56616
56617 if( bBatch==0 ){
56618 rc = pager_write_pagelist(pPager, pList);
56619 }
56620 if( rc!=SQLITE_OK ){
56621 assert( rc!=SQLITE_IOERR_BLOCKED );
56622 goto commit_phase_one_exit;
56623 }
56624 sqlite3PcacheCleanAll(pPager->pPCache);
56625
56626 /* If the file on disk is smaller than the database image, use
56627 ** pager_truncate to grow the file here. This can happen if the database
56628 ** image was extended as part of the current transaction and then the
56629 ** last page in the db image moved to the free-list. In this case the
56630 ** last page is never written out to disk, leaving the database file
56631 ** undersized. Fix this now if it is the case. */
56632 if( pPager->dbSize>pPager->dbFileSize ){
56633 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
56634 assert( pPager->eState==PAGER_WRITER_DBMOD );
56635 rc = pager_truncate(pPager, nNew);
56636 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
56637 }
56638
56639 /* Finally, sync the database file. */
56640 if( !noSync ){
56641 rc = sqlite3PagerSync(pPager, zMaster);
56642 }
56643 IOTRACE(("DBSYNC %p\n", pPager))
56644 }
56645 }
56646
56647commit_phase_one_exit:
56648 if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
56649 pPager->eState = PAGER_WRITER_FINISHED;
56650 }
56651 return rc;
56652}
56653
56654
56655/*
56656** When this function is called, the database file has been completely
56657** updated to reflect the changes made by the current transaction and
56658** synced to disk. The journal file still exists in the file-system
56659** though, and if a failure occurs at this point it will eventually
56660** be used as a hot-journal and the current transaction rolled back.
56661**
56662** This function finalizes the journal file, either by deleting,
56663** truncating or partially zeroing it, so that it cannot be used
56664** for hot-journal rollback. Once this is done the transaction is
56665** irrevocably committed.
56666**
56667** If an error occurs, an IO error code is returned and the pager
56668** moves into the error state. Otherwise, SQLITE_OK is returned.
56669*/
56670SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
56671 int rc = SQLITE_OK; /* Return code */
56672
56673 /* This routine should not be called if a prior error has occurred.
56674 ** But if (due to a coding error elsewhere in the system) it does get
56675 ** called, just return the same error code without doing anything. */
56676 if( NEVER(pPager->errCode) ) return pPager->errCode;
56677
56678 assert( pPager->eState==PAGER_WRITER_LOCKED
56679 || pPager->eState==PAGER_WRITER_FINISHED
56680 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
56681 );
56682 assert( assert_pager_state(pPager) );
56683
56684 /* An optimization. If the database was not actually modified during
56685 ** this transaction, the pager is running in exclusive-mode and is
56686 ** using persistent journals, then this function is a no-op.
56687 **
56688 ** The start of the journal file currently contains a single journal
56689 ** header with the nRec field set to 0. If such a journal is used as
56690 ** a hot-journal during hot-journal rollback, 0 changes will be made
56691 ** to the database file. So there is no need to zero the journal
56692 ** header. Since the pager is in exclusive mode, there is no need
56693 ** to drop any locks either.
56694 */
56695 if( pPager->eState==PAGER_WRITER_LOCKED
56696 && pPager->exclusiveMode
56697 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
56698 ){
56699 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
56700 pPager->eState = PAGER_READER;
56701 return SQLITE_OK;
56702 }
56703
56704 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
56705 pPager->iDataVersion++;
56706 rc = pager_end_transaction(pPager, pPager->setMaster, 1);
56707 return pager_error(pPager, rc);
56708}
56709
56710/*
56711** If a write transaction is open, then all changes made within the
56712** transaction are reverted and the current write-transaction is closed.
56713** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
56714** state if an error occurs.
56715**
56716** If the pager is already in PAGER_ERROR state when this function is called,
56717** it returns Pager.errCode immediately. No work is performed in this case.
56718**
56719** Otherwise, in rollback mode, this function performs two functions:
56720**
56721** 1) It rolls back the journal file, restoring all database file and
56722** in-memory cache pages to the state they were in when the transaction
56723** was opened, and
56724**
56725** 2) It finalizes the journal file, so that it is not used for hot
56726** rollback at any point in the future.
56727**
56728** Finalization of the journal file (task 2) is only performed if the
56729** rollback is successful.
56730**
56731** In WAL mode, all cache-entries containing data modified within the
56732** current transaction are either expelled from the cache or reverted to
56733** their pre-transaction state by re-reading data from the database or
56734** WAL files. The WAL transaction is then closed.
56735*/
56736SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
56737 int rc = SQLITE_OK; /* Return code */
56738 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
56739
56740 /* PagerRollback() is a no-op if called in READER or OPEN state. If
56741 ** the pager is already in the ERROR state, the rollback is not
56742 ** attempted here. Instead, the error code is returned to the caller.
56743 */
56744 assert( assert_pager_state(pPager) );
56745 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
56746 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
56747
56748 if( pagerUseWal(pPager) ){
56749 int rc2;
56750 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
56751 rc2 = pager_end_transaction(pPager, pPager->setMaster, 0);
56752 if( rc==SQLITE_OK ) rc = rc2;
56753 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
56754 int eState = pPager->eState;
56755 rc = pager_end_transaction(pPager, 0, 0);
56756 if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
56757 /* This can happen using journal_mode=off. Move the pager to the error
56758 ** state to indicate that the contents of the cache may not be trusted.
56759 ** Any active readers will get SQLITE_ABORT.
56760 */
56761 pPager->errCode = SQLITE_ABORT;
56762 pPager->eState = PAGER_ERROR;
56763 setGetterMethod(pPager);
56764 return rc;
56765 }
56766 }else{
56767 rc = pager_playback(pPager, 0);
56768 }
56769
56770 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
56771 assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
56772 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR
56773 || rc==SQLITE_CANTOPEN
56774 );
56775
56776 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
56777 ** cache. So call pager_error() on the way out to make any error persistent.
56778 */
56779 return pager_error(pPager, rc);
56780}
56781
56782/*
56783** Return TRUE if the database file is opened read-only. Return FALSE
56784** if the database is (in theory) writable.
56785*/
56786SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
56787 return pPager->readOnly;
56788}
56789
56790#ifdef SQLITE_DEBUG
56791/*
56792** Return the sum of the reference counts for all pages held by pPager.
56793*/
56794SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
56795 return sqlite3PcacheRefCount(pPager->pPCache);
56796}
56797#endif
56798
56799/*
56800** Return the approximate number of bytes of memory currently
56801** used by the pager and its associated cache.
56802*/
56803SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
56804 int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
56805 + 5*sizeof(void*);
56806 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
56807 + sqlite3MallocSize(pPager)
56808 + pPager->pageSize;
56809}
56810
56811/*
56812** Return the number of references to the specified page.
56813*/
56814SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
56815 return sqlite3PcachePageRefcount(pPage);
56816}
56817
56818#ifdef SQLITE_TEST
56819/*
56820** This routine is used for testing and analysis only.
56821*/
56822SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
56823 static int a[11];
56824 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
56825 a[1] = sqlite3PcachePagecount(pPager->pPCache);
56826 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
56827 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
56828 a[4] = pPager->eState;
56829 a[5] = pPager->errCode;
56830 a[6] = pPager->aStat[PAGER_STAT_HIT];
56831 a[7] = pPager->aStat[PAGER_STAT_MISS];
56832 a[8] = 0; /* Used to be pPager->nOvfl */
56833 a[9] = pPager->nRead;
56834 a[10] = pPager->aStat[PAGER_STAT_WRITE];
56835 return a;
56836}
56837#endif
56838
56839/*
56840** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
56841** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
56842** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because
56843** it was added later.
56844**
56845** Before returning, *pnVal is incremented by the
56846** current cache hit or miss count, according to the value of eStat. If the
56847** reset parameter is non-zero, the cache hit or miss count is zeroed before
56848** returning.
56849*/
56850SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
56851
56852 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
56853 || eStat==SQLITE_DBSTATUS_CACHE_MISS
56854 || eStat==SQLITE_DBSTATUS_CACHE_WRITE
56855 || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
56856 );
56857
56858 assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
56859 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
56860 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
56861 && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
56862
56863 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
56864 *pnVal += pPager->aStat[eStat];
56865 if( reset ){
56866 pPager->aStat[eStat] = 0;
56867 }
56868}
56869
56870/*
56871** Return true if this is an in-memory or temp-file backed pager.
56872*/
56873SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
56874 return pPager->tempFile;
56875}
56876
56877/*
56878** Check that there are at least nSavepoint savepoints open. If there are
56879** currently less than nSavepoints open, then open one or more savepoints
56880** to make up the difference. If the number of savepoints is already
56881** equal to nSavepoint, then this function is a no-op.
56882**
56883** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
56884** occurs while opening the sub-journal file, then an IO error code is
56885** returned. Otherwise, SQLITE_OK.
56886*/
56887static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
56888 int rc = SQLITE_OK; /* Return code */
56889 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
56890 int ii; /* Iterator variable */
56891 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
56892
56893 assert( pPager->eState>=PAGER_WRITER_LOCKED );
56894 assert( assert_pager_state(pPager) );
56895 assert( nSavepoint>nCurrent && pPager->useJournal );
56896
56897 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
56898 ** if the allocation fails. Otherwise, zero the new portion in case a
56899 ** malloc failure occurs while populating it in the for(...) loop below.
56900 */
56901 aNew = (PagerSavepoint *)sqlite3Realloc(
56902 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
56903 );
56904 if( !aNew ){
56905 return SQLITE_NOMEM_BKPT;
56906 }
56907 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
56908 pPager->aSavepoint = aNew;
56909
56910 /* Populate the PagerSavepoint structures just allocated. */
56911 for(ii=nCurrent; ii<nSavepoint; ii++){
56912 aNew[ii].nOrig = pPager->dbSize;
56913 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
56914 aNew[ii].iOffset = pPager->journalOff;
56915 }else{
56916 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
56917 }
56918 aNew[ii].iSubRec = pPager->nSubRec;
56919 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
56920 if( !aNew[ii].pInSavepoint ){
56921 return SQLITE_NOMEM_BKPT;
56922 }
56923 if( pagerUseWal(pPager) ){
56924 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
56925 }
56926 pPager->nSavepoint = ii+1;
56927 }
56928 assert( pPager->nSavepoint==nSavepoint );
56929 assertTruncateConstraint(pPager);
56930 return rc;
56931}
56932SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
56933 assert( pPager->eState>=PAGER_WRITER_LOCKED );
56934 assert( assert_pager_state(pPager) );
56935
56936 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
56937 return pagerOpenSavepoint(pPager, nSavepoint);
56938 }else{
56939 return SQLITE_OK;
56940 }
56941}
56942
56943
56944/*
56945** This function is called to rollback or release (commit) a savepoint.
56946** The savepoint to release or rollback need not be the most recently
56947** created savepoint.
56948**
56949** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
56950** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
56951** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
56952** that have occurred since the specified savepoint was created.
56953**
56954** The savepoint to rollback or release is identified by parameter
56955** iSavepoint. A value of 0 means to operate on the outermost savepoint
56956** (the first created). A value of (Pager.nSavepoint-1) means operate
56957** on the most recently created savepoint. If iSavepoint is greater than
56958** (Pager.nSavepoint-1), then this function is a no-op.
56959**
56960** If a negative value is passed to this function, then the current
56961** transaction is rolled back. This is different to calling
56962** sqlite3PagerRollback() because this function does not terminate
56963** the transaction or unlock the database, it just restores the
56964** contents of the database to its original state.
56965**
56966** In any case, all savepoints with an index greater than iSavepoint
56967** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
56968** then savepoint iSavepoint is also destroyed.
56969**
56970** This function may return SQLITE_NOMEM if a memory allocation fails,
56971** or an IO error code if an IO error occurs while rolling back a
56972** savepoint. If no errors occur, SQLITE_OK is returned.
56973*/
56974SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
56975 int rc = pPager->errCode;
56976
56977#ifdef SQLITE_ENABLE_ZIPVFS
56978 if( op==SAVEPOINT_RELEASE ) rc = SQLITE_OK;
56979#endif
56980
56981 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
56982 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
56983
56984 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
56985 int ii; /* Iterator variable */
56986 int nNew; /* Number of remaining savepoints after this op. */
56987
56988 /* Figure out how many savepoints will still be active after this
56989 ** operation. Store this value in nNew. Then free resources associated
56990 ** with any savepoints that are destroyed by this operation.
56991 */
56992 nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
56993 for(ii=nNew; ii<pPager->nSavepoint; ii++){
56994 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
56995 }
56996 pPager->nSavepoint = nNew;
56997
56998 /* If this is a release of the outermost savepoint, truncate
56999 ** the sub-journal to zero bytes in size. */
57000 if( op==SAVEPOINT_RELEASE ){
57001 if( nNew==0 && isOpen(pPager->sjfd) ){
57002 /* Only truncate if it is an in-memory sub-journal. */
57003 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
57004 rc = sqlite3OsTruncate(pPager->sjfd, 0);
57005 assert( rc==SQLITE_OK );
57006 }
57007 pPager->nSubRec = 0;
57008 }
57009 }
57010 /* Else this is a rollback operation, playback the specified savepoint.
57011 ** If this is a temp-file, it is possible that the journal file has
57012 ** not yet been opened. In this case there have been no changes to
57013 ** the database file, so the playback operation can be skipped.
57014 */
57015 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
57016 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
57017 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
57018 assert(rc!=SQLITE_DONE);
57019 }
57020
57021#ifdef SQLITE_ENABLE_ZIPVFS
57022 /* If the cache has been modified but the savepoint cannot be rolled
57023 ** back journal_mode=off, put the pager in the error state. This way,
57024 ** if the VFS used by this pager includes ZipVFS, the entire transaction
57025 ** can be rolled back at the ZipVFS level. */
57026 else if(
57027 pPager->journalMode==PAGER_JOURNALMODE_OFF
57028 && pPager->eState>=PAGER_WRITER_CACHEMOD
57029 ){
57030 pPager->errCode = SQLITE_ABORT;
57031 pPager->eState = PAGER_ERROR;
57032 setGetterMethod(pPager);
57033 }
57034#endif
57035 }
57036
57037 return rc;
57038}
57039
57040/*
57041** Return the full pathname of the database file.
57042**
57043** Except, if the pager is in-memory only, then return an empty string if
57044** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when
57045** used to report the filename to the user, for compatibility with legacy
57046** behavior. But when the Btree needs to know the filename for matching to
57047** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
57048** participate in shared-cache.
57049*/
57050SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){
57051 return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename;
57052}
57053
57054/*
57055** Return the VFS structure for the pager.
57056*/
57057SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
57058 return pPager->pVfs;
57059}
57060
57061/*
57062** Return the file handle for the database file associated
57063** with the pager. This might return NULL if the file has
57064** not yet been opened.
57065*/
57066SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
57067 return pPager->fd;
57068}
57069
57070#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
57071/*
57072** Reset the lock timeout for pager.
57073*/
57074SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager){
57075 int x = 0;
57076 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_LOCK_TIMEOUT, &x);
57077}
57078#endif
57079
57080/*
57081** Return the file handle for the journal file (if it exists).
57082** This will be either the rollback journal or the WAL file.
57083*/
57084SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
57085#if SQLITE_OMIT_WAL
57086 return pPager->jfd;
57087#else
57088 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
57089#endif
57090}
57091
57092/*
57093** Return the full pathname of the journal file.
57094*/
57095SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
57096 return pPager->zJournal;
57097}
57098
57099#ifdef SQLITE_HAS_CODEC
57100/*
57101** Set or retrieve the codec for this pager
57102*/
57103SQLITE_PRIVATE void sqlite3PagerSetCodec(
57104 Pager *pPager,
57105 void *(*xCodec)(void*,void*,Pgno,int),
57106 void (*xCodecSizeChng)(void*,int,int),
57107 void (*xCodecFree)(void*),
57108 void *pCodec
57109){
57110 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
57111 pPager->xCodec = pPager->memDb ? 0 : xCodec;
57112 pPager->xCodecSizeChng = xCodecSizeChng;
57113 pPager->xCodecFree = xCodecFree;
57114 pPager->pCodec = pCodec;
57115 setGetterMethod(pPager);
57116 pagerReportSize(pPager);
57117}
57118SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
57119 return pPager->pCodec;
57120}
57121
57122/*
57123** This function is called by the wal module when writing page content
57124** into the log file.
57125**
57126** This function returns a pointer to a buffer containing the encrypted
57127** page content. If a malloc fails, this function may return NULL.
57128*/
57129SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
57130 void *aData = 0;
57131 CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
57132 return aData;
57133}
57134
57135/*
57136** Return the current pager state
57137*/
57138SQLITE_PRIVATE int sqlite3PagerState(Pager *pPager){
57139 return pPager->eState;
57140}
57141#endif /* SQLITE_HAS_CODEC */
57142
57143#ifndef SQLITE_OMIT_AUTOVACUUM
57144/*
57145** Move the page pPg to location pgno in the file.
57146**
57147** There must be no references to the page previously located at
57148** pgno (which we call pPgOld) though that page is allowed to be
57149** in cache. If the page previously located at pgno is not already
57150** in the rollback journal, it is not put there by by this routine.
57151**
57152** References to the page pPg remain valid. Updating any
57153** meta-data associated with pPg (i.e. data stored in the nExtra bytes
57154** allocated along with the page) is the responsibility of the caller.
57155**
57156** A transaction must be active when this routine is called. It used to be
57157** required that a statement transaction was not active, but this restriction
57158** has been removed (CREATE INDEX needs to move a page when a statement
57159** transaction is active).
57160**
57161** If the fourth argument, isCommit, is non-zero, then this page is being
57162** moved as part of a database reorganization just before the transaction
57163** is being committed. In this case, it is guaranteed that the database page
57164** pPg refers to will not be written to again within this transaction.
57165**
57166** This function may return SQLITE_NOMEM or an IO error code if an error
57167** occurs. Otherwise, it returns SQLITE_OK.
57168*/
57169SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
57170 PgHdr *pPgOld; /* The page being overwritten. */
57171 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
57172 int rc; /* Return code */
57173 Pgno origPgno; /* The original page number */
57174
57175 assert( pPg->nRef>0 );
57176 assert( pPager->eState==PAGER_WRITER_CACHEMOD
57177 || pPager->eState==PAGER_WRITER_DBMOD
57178 );
57179 assert( assert_pager_state(pPager) );
57180
57181 /* In order to be able to rollback, an in-memory database must journal
57182 ** the page we are moving from.
57183 */
57184 assert( pPager->tempFile || !MEMDB );
57185 if( pPager->tempFile ){
57186 rc = sqlite3PagerWrite(pPg);
57187 if( rc ) return rc;
57188 }
57189
57190 /* If the page being moved is dirty and has not been saved by the latest
57191 ** savepoint, then save the current contents of the page into the
57192 ** sub-journal now. This is required to handle the following scenario:
57193 **
57194 ** BEGIN;
57195 ** <journal page X, then modify it in memory>
57196 ** SAVEPOINT one;
57197 ** <Move page X to location Y>
57198 ** ROLLBACK TO one;
57199 **
57200 ** If page X were not written to the sub-journal here, it would not
57201 ** be possible to restore its contents when the "ROLLBACK TO one"
57202 ** statement were is processed.
57203 **
57204 ** subjournalPage() may need to allocate space to store pPg->pgno into
57205 ** one or more savepoint bitvecs. This is the reason this function
57206 ** may return SQLITE_NOMEM.
57207 */
57208 if( (pPg->flags & PGHDR_DIRTY)!=0
57209 && SQLITE_OK!=(rc = subjournalPageIfRequired(pPg))
57210 ){
57211 return rc;
57212 }
57213
57214 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
57215 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
57216 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
57217
57218 /* If the journal needs to be sync()ed before page pPg->pgno can
57219 ** be written to, store pPg->pgno in local variable needSyncPgno.
57220 **
57221 ** If the isCommit flag is set, there is no need to remember that
57222 ** the journal needs to be sync()ed before database page pPg->pgno
57223 ** can be written to. The caller has already promised not to write to it.
57224 */
57225 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
57226 needSyncPgno = pPg->pgno;
57227 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
57228 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
57229 assert( pPg->flags&PGHDR_DIRTY );
57230 }
57231
57232 /* If the cache contains a page with page-number pgno, remove it
57233 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
57234 ** page pgno before the 'move' operation, it needs to be retained
57235 ** for the page moved there.
57236 */
57237 pPg->flags &= ~PGHDR_NEED_SYNC;
57238 pPgOld = sqlite3PagerLookup(pPager, pgno);
57239 assert( !pPgOld || pPgOld->nRef==1 );
57240 if( pPgOld ){
57241 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
57242 if( pPager->tempFile ){
57243 /* Do not discard pages from an in-memory database since we might
57244 ** need to rollback later. Just move the page out of the way. */
57245 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
57246 }else{
57247 sqlite3PcacheDrop(pPgOld);
57248 }
57249 }
57250
57251 origPgno = pPg->pgno;
57252 sqlite3PcacheMove(pPg, pgno);
57253 sqlite3PcacheMakeDirty(pPg);
57254
57255 /* For an in-memory database, make sure the original page continues
57256 ** to exist, in case the transaction needs to roll back. Use pPgOld
57257 ** as the original page since it has already been allocated.
57258 */
57259 if( pPager->tempFile && pPgOld ){
57260 sqlite3PcacheMove(pPgOld, origPgno);
57261 sqlite3PagerUnrefNotNull(pPgOld);
57262 }
57263
57264 if( needSyncPgno ){
57265 /* If needSyncPgno is non-zero, then the journal file needs to be
57266 ** sync()ed before any data is written to database file page needSyncPgno.
57267 ** Currently, no such page exists in the page-cache and the
57268 ** "is journaled" bitvec flag has been set. This needs to be remedied by
57269 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
57270 ** flag.
57271 **
57272 ** If the attempt to load the page into the page-cache fails, (due
57273 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
57274 ** array. Otherwise, if the page is loaded and written again in
57275 ** this transaction, it may be written to the database file before
57276 ** it is synced into the journal file. This way, it may end up in
57277 ** the journal file twice, but that is not a problem.
57278 */
57279 PgHdr *pPgHdr;
57280 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr, 0);
57281 if( rc!=SQLITE_OK ){
57282 if( needSyncPgno<=pPager->dbOrigSize ){
57283 assert( pPager->pTmpSpace!=0 );
57284 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
57285 }
57286 return rc;
57287 }
57288 pPgHdr->flags |= PGHDR_NEED_SYNC;
57289 sqlite3PcacheMakeDirty(pPgHdr);
57290 sqlite3PagerUnrefNotNull(pPgHdr);
57291 }
57292
57293 return SQLITE_OK;
57294}
57295#endif
57296
57297/*
57298** The page handle passed as the first argument refers to a dirty page
57299** with a page number other than iNew. This function changes the page's
57300** page number to iNew and sets the value of the PgHdr.flags field to
57301** the value passed as the third parameter.
57302*/
57303SQLITE_PRIVATE void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){
57304 assert( pPg->pgno!=iNew );
57305 pPg->flags = flags;
57306 sqlite3PcacheMove(pPg, iNew);
57307}
57308
57309/*
57310** Return a pointer to the data for the specified page.
57311*/
57312SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
57313 assert( pPg->nRef>0 || pPg->pPager->memDb );
57314 return pPg->pData;
57315}
57316
57317/*
57318** Return a pointer to the Pager.nExtra bytes of "extra" space
57319** allocated along with the specified page.
57320*/
57321SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
57322 return pPg->pExtra;
57323}
57324
57325/*
57326** Get/set the locking-mode for this pager. Parameter eMode must be one
57327** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
57328** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
57329** the locking-mode is set to the value specified.
57330**
57331** The returned value is either PAGER_LOCKINGMODE_NORMAL or
57332** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
57333** locking-mode.
57334*/
57335SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
57336 assert( eMode==PAGER_LOCKINGMODE_QUERY
57337 || eMode==PAGER_LOCKINGMODE_NORMAL
57338 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
57339 assert( PAGER_LOCKINGMODE_QUERY<0 );
57340 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
57341 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
57342 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
57343 pPager->exclusiveMode = (u8)eMode;
57344 }
57345 return (int)pPager->exclusiveMode;
57346}
57347
57348/*
57349** Set the journal-mode for this pager. Parameter eMode must be one of:
57350**
57351** PAGER_JOURNALMODE_DELETE
57352** PAGER_JOURNALMODE_TRUNCATE
57353** PAGER_JOURNALMODE_PERSIST
57354** PAGER_JOURNALMODE_OFF
57355** PAGER_JOURNALMODE_MEMORY
57356** PAGER_JOURNALMODE_WAL
57357**
57358** The journalmode is set to the value specified if the change is allowed.
57359** The change may be disallowed for the following reasons:
57360**
57361** * An in-memory database can only have its journal_mode set to _OFF
57362** or _MEMORY.
57363**
57364** * Temporary databases cannot have _WAL journalmode.
57365**
57366** The returned indicate the current (possibly updated) journal-mode.
57367*/
57368SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
57369 u8 eOld = pPager->journalMode; /* Prior journalmode */
57370
57371 /* The eMode parameter is always valid */
57372 assert( eMode==PAGER_JOURNALMODE_DELETE
57373 || eMode==PAGER_JOURNALMODE_TRUNCATE
57374 || eMode==PAGER_JOURNALMODE_PERSIST
57375 || eMode==PAGER_JOURNALMODE_OFF
57376 || eMode==PAGER_JOURNALMODE_WAL
57377 || eMode==PAGER_JOURNALMODE_MEMORY );
57378
57379 /* This routine is only called from the OP_JournalMode opcode, and
57380 ** the logic there will never allow a temporary file to be changed
57381 ** to WAL mode.
57382 */
57383 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
57384
57385 /* Do allow the journalmode of an in-memory database to be set to
57386 ** anything other than MEMORY or OFF
57387 */
57388 if( MEMDB ){
57389 assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
57390 if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
57391 eMode = eOld;
57392 }
57393 }
57394
57395 if( eMode!=eOld ){
57396
57397 /* Change the journal mode. */
57398 assert( pPager->eState!=PAGER_ERROR );
57399 pPager->journalMode = (u8)eMode;
57400
57401 /* When transistioning from TRUNCATE or PERSIST to any other journal
57402 ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
57403 ** delete the journal file.
57404 */
57405 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
57406 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
57407 assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
57408 assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
57409 assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
57410 assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
57411
57412 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
57413 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
57414
57415 /* In this case we would like to delete the journal file. If it is
57416 ** not possible, then that is not a problem. Deleting the journal file
57417 ** here is an optimization only.
57418 **
57419 ** Before deleting the journal file, obtain a RESERVED lock on the
57420 ** database file. This ensures that the journal file is not deleted
57421 ** while it is in use by some other client.
57422 */
57423 sqlite3OsClose(pPager->jfd);
57424 if( pPager->eLock>=RESERVED_LOCK ){
57425 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
57426 }else{
57427 int rc = SQLITE_OK;
57428 int state = pPager->eState;
57429 assert( state==PAGER_OPEN || state==PAGER_READER );
57430 if( state==PAGER_OPEN ){
57431 rc = sqlite3PagerSharedLock(pPager);
57432 }
57433 if( pPager->eState==PAGER_READER ){
57434 assert( rc==SQLITE_OK );
57435 rc = pagerLockDb(pPager, RESERVED_LOCK);
57436 }
57437 if( rc==SQLITE_OK ){
57438 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
57439 }
57440 if( rc==SQLITE_OK && state==PAGER_READER ){
57441 pagerUnlockDb(pPager, SHARED_LOCK);
57442 }else if( state==PAGER_OPEN ){
57443 pager_unlock(pPager);
57444 }
57445 assert( state==pPager->eState );
57446 }
57447 }else if( eMode==PAGER_JOURNALMODE_OFF ){
57448 sqlite3OsClose(pPager->jfd);
57449 }
57450 }
57451
57452 /* Return the new journal mode */
57453 return (int)pPager->journalMode;
57454}
57455
57456/*
57457** Return the current journal mode.
57458*/
57459SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
57460 return (int)pPager->journalMode;
57461}
57462
57463/*
57464** Return TRUE if the pager is in a state where it is OK to change the
57465** journalmode. Journalmode changes can only happen when the database
57466** is unmodified.
57467*/
57468SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
57469 assert( assert_pager_state(pPager) );
57470 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
57471 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
57472 return 1;
57473}
57474
57475/*
57476** Get/set the size-limit used for persistent journal files.
57477**
57478** Setting the size limit to -1 means no limit is enforced.
57479** An attempt to set a limit smaller than -1 is a no-op.
57480*/
57481SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
57482 if( iLimit>=-1 ){
57483 pPager->journalSizeLimit = iLimit;
57484 sqlite3WalLimit(pPager->pWal, iLimit);
57485 }
57486 return pPager->journalSizeLimit;
57487}
57488
57489/*
57490** Return a pointer to the pPager->pBackup variable. The backup module
57491** in backup.c maintains the content of this variable. This module
57492** uses it opaquely as an argument to sqlite3BackupRestart() and
57493** sqlite3BackupUpdate() only.
57494*/
57495SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
57496 return &pPager->pBackup;
57497}
57498
57499#ifndef SQLITE_OMIT_VACUUM
57500/*
57501** Unless this is an in-memory or temporary database, clear the pager cache.
57502*/
57503SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
57504 assert( MEMDB==0 || pPager->tempFile );
57505 if( pPager->tempFile==0 ) pager_reset(pPager);
57506}
57507#endif
57508
57509
57510#ifndef SQLITE_OMIT_WAL
57511/*
57512** This function is called when the user invokes "PRAGMA wal_checkpoint",
57513** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
57514** or wal_blocking_checkpoint() API functions.
57515**
57516** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
57517*/
57518SQLITE_PRIVATE int sqlite3PagerCheckpoint(
57519 Pager *pPager, /* Checkpoint on this pager */
57520 sqlite3 *db, /* Db handle used to check for interrupts */
57521 int eMode, /* Type of checkpoint */
57522 int *pnLog, /* OUT: Final number of frames in log */
57523 int *pnCkpt /* OUT: Final number of checkpointed frames */
57524){
57525 int rc = SQLITE_OK;
57526 if( pPager->pWal ){
57527 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
57528 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
57529 pPager->pBusyHandlerArg,
57530 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
57531 pnLog, pnCkpt
57532 );
57533 sqlite3PagerResetLockTimeout(pPager);
57534 }
57535 return rc;
57536}
57537
57538SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
57539 return sqlite3WalCallback(pPager->pWal);
57540}
57541
57542/*
57543** Return true if the underlying VFS for the given pager supports the
57544** primitives necessary for write-ahead logging.
57545*/
57546SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
57547 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
57548 if( pPager->noLock ) return 0;
57549 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
57550}
57551
57552/*
57553** Attempt to take an exclusive lock on the database file. If a PENDING lock
57554** is obtained instead, immediately release it.
57555*/
57556static int pagerExclusiveLock(Pager *pPager){
57557 int rc; /* Return code */
57558
57559 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
57560 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
57561 if( rc!=SQLITE_OK ){
57562 /* If the attempt to grab the exclusive lock failed, release the
57563 ** pending lock that may have been obtained instead. */
57564 pagerUnlockDb(pPager, SHARED_LOCK);
57565 }
57566
57567 return rc;
57568}
57569
57570/*
57571** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
57572** exclusive-locking mode when this function is called, take an EXCLUSIVE
57573** lock on the database file and use heap-memory to store the wal-index
57574** in. Otherwise, use the normal shared-memory.
57575*/
57576static int pagerOpenWal(Pager *pPager){
57577 int rc = SQLITE_OK;
57578
57579 assert( pPager->pWal==0 && pPager->tempFile==0 );
57580 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
57581
57582 /* If the pager is already in exclusive-mode, the WAL module will use
57583 ** heap-memory for the wal-index instead of the VFS shared-memory
57584 ** implementation. Take the exclusive lock now, before opening the WAL
57585 ** file, to make sure this is safe.
57586 */
57587 if( pPager->exclusiveMode ){
57588 rc = pagerExclusiveLock(pPager);
57589 }
57590
57591 /* Open the connection to the log file. If this operation fails,
57592 ** (e.g. due to malloc() failure), return an error code.
57593 */
57594 if( rc==SQLITE_OK ){
57595 rc = sqlite3WalOpen(pPager->pVfs,
57596 pPager->fd, pPager->zWal, pPager->exclusiveMode,
57597 pPager->journalSizeLimit, &pPager->pWal
57598 );
57599 }
57600 pagerFixMaplimit(pPager);
57601
57602 return rc;
57603}
57604
57605
57606/*
57607** The caller must be holding a SHARED lock on the database file to call
57608** this function.
57609**
57610** If the pager passed as the first argument is open on a real database
57611** file (not a temp file or an in-memory database), and the WAL file
57612** is not already open, make an attempt to open it now. If successful,
57613** return SQLITE_OK. If an error occurs or the VFS used by the pager does
57614** not support the xShmXXX() methods, return an error code. *pbOpen is
57615** not modified in either case.
57616**
57617** If the pager is open on a temp-file (or in-memory database), or if
57618** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
57619** without doing anything.
57620*/
57621SQLITE_PRIVATE int sqlite3PagerOpenWal(
57622 Pager *pPager, /* Pager object */
57623 int *pbOpen /* OUT: Set to true if call is a no-op */
57624){
57625 int rc = SQLITE_OK; /* Return code */
57626
57627 assert( assert_pager_state(pPager) );
57628 assert( pPager->eState==PAGER_OPEN || pbOpen );
57629 assert( pPager->eState==PAGER_READER || !pbOpen );
57630 assert( pbOpen==0 || *pbOpen==0 );
57631 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
57632
57633 if( !pPager->tempFile && !pPager->pWal ){
57634 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
57635
57636 /* Close any rollback journal previously open */
57637 sqlite3OsClose(pPager->jfd);
57638
57639 rc = pagerOpenWal(pPager);
57640 if( rc==SQLITE_OK ){
57641 pPager->journalMode = PAGER_JOURNALMODE_WAL;
57642 pPager->eState = PAGER_OPEN;
57643 }
57644 }else{
57645 *pbOpen = 1;
57646 }
57647
57648 return rc;
57649}
57650
57651/*
57652** This function is called to close the connection to the log file prior
57653** to switching from WAL to rollback mode.
57654**
57655** Before closing the log file, this function attempts to take an
57656** EXCLUSIVE lock on the database file. If this cannot be obtained, an
57657** error (SQLITE_BUSY) is returned and the log connection is not closed.
57658** If successful, the EXCLUSIVE lock is not released before returning.
57659*/
57660SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
57661 int rc = SQLITE_OK;
57662
57663 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
57664
57665 /* If the log file is not already open, but does exist in the file-system,
57666 ** it may need to be checkpointed before the connection can switch to
57667 ** rollback mode. Open it now so this can happen.
57668 */
57669 if( !pPager->pWal ){
57670 int logexists = 0;
57671 rc = pagerLockDb(pPager, SHARED_LOCK);
57672 if( rc==SQLITE_OK ){
57673 rc = sqlite3OsAccess(
57674 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
57675 );
57676 }
57677 if( rc==SQLITE_OK && logexists ){
57678 rc = pagerOpenWal(pPager);
57679 }
57680 }
57681
57682 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
57683 ** the database file, the log and log-summary files will be deleted.
57684 */
57685 if( rc==SQLITE_OK && pPager->pWal ){
57686 rc = pagerExclusiveLock(pPager);
57687 if( rc==SQLITE_OK ){
57688 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
57689 pPager->pageSize, (u8*)pPager->pTmpSpace);
57690 pPager->pWal = 0;
57691 pagerFixMaplimit(pPager);
57692 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
57693 }
57694 }
57695 return rc;
57696}
57697
57698#ifdef SQLITE_ENABLE_SNAPSHOT
57699/*
57700** If this is a WAL database, obtain a snapshot handle for the snapshot
57701** currently open. Otherwise, return an error.
57702*/
57703SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){
57704 int rc = SQLITE_ERROR;
57705 if( pPager->pWal ){
57706 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
57707 }
57708 return rc;
57709}
57710
57711/*
57712** If this is a WAL database, store a pointer to pSnapshot. Next time a
57713** read transaction is opened, attempt to read from the snapshot it
57714** identifies. If this is not a WAL database, return an error.
57715*/
57716SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){
57717 int rc = SQLITE_OK;
57718 if( pPager->pWal ){
57719 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
57720 }else{
57721 rc = SQLITE_ERROR;
57722 }
57723 return rc;
57724}
57725
57726/*
57727** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this
57728** is not a WAL database, return an error.
57729*/
57730SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager){
57731 int rc;
57732 if( pPager->pWal ){
57733 rc = sqlite3WalSnapshotRecover(pPager->pWal);
57734 }else{
57735 rc = SQLITE_ERROR;
57736 }
57737 return rc;
57738}
57739
57740/*
57741** The caller currently has a read transaction open on the database.
57742** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise,
57743** this function takes a SHARED lock on the CHECKPOINTER slot and then
57744** checks if the snapshot passed as the second argument is still
57745** available. If so, SQLITE_OK is returned.
57746**
57747** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
57748** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
57749** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
57750** lock is released before returning.
57751*/
57752SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){
57753 int rc;
57754 if( pPager->pWal ){
57755 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
57756 }else{
57757 rc = SQLITE_ERROR;
57758 }
57759 return rc;
57760}
57761
57762/*
57763** Release a lock obtained by an earlier successful call to
57764** sqlite3PagerSnapshotCheck().
57765*/
57766SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager){
57767 assert( pPager->pWal );
57768 return sqlite3WalSnapshotUnlock(pPager->pWal);
57769}
57770
57771#endif /* SQLITE_ENABLE_SNAPSHOT */
57772#endif /* !SQLITE_OMIT_WAL */
57773
57774#ifdef SQLITE_ENABLE_ZIPVFS
57775/*
57776** A read-lock must be held on the pager when this function is called. If
57777** the pager is in WAL mode and the WAL file currently contains one or more
57778** frames, return the size in bytes of the page images stored within the
57779** WAL frames. Otherwise, if this is not a WAL database or the WAL file
57780** is empty, return 0.
57781*/
57782SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
57783 assert( pPager->eState>=PAGER_READER );
57784 return sqlite3WalFramesize(pPager->pWal);
57785}
57786#endif
57787
57788#endif /* SQLITE_OMIT_DISKIO */
57789
57790/************** End of pager.c ***********************************************/
57791/************** Begin file wal.c *********************************************/
57792/*
57793** 2010 February 1
57794**
57795** The author disclaims copyright to this source code. In place of
57796** a legal notice, here is a blessing:
57797**
57798** May you do good and not evil.
57799** May you find forgiveness for yourself and forgive others.
57800** May you share freely, never taking more than you give.
57801**
57802*************************************************************************
57803**
57804** This file contains the implementation of a write-ahead log (WAL) used in
57805** "journal_mode=WAL" mode.
57806**
57807** WRITE-AHEAD LOG (WAL) FILE FORMAT
57808**
57809** A WAL file consists of a header followed by zero or more "frames".
57810** Each frame records the revised content of a single page from the
57811** database file. All changes to the database are recorded by writing
57812** frames into the WAL. Transactions commit when a frame is written that
57813** contains a commit marker. A single WAL can and usually does record
57814** multiple transactions. Periodically, the content of the WAL is
57815** transferred back into the database file in an operation called a
57816** "checkpoint".
57817**
57818** A single WAL file can be used multiple times. In other words, the
57819** WAL can fill up with frames and then be checkpointed and then new
57820** frames can overwrite the old ones. A WAL always grows from beginning
57821** toward the end. Checksums and counters attached to each frame are
57822** used to determine which frames within the WAL are valid and which
57823** are leftovers from prior checkpoints.
57824**
57825** The WAL header is 32 bytes in size and consists of the following eight
57826** big-endian 32-bit unsigned integer values:
57827**
57828** 0: Magic number. 0x377f0682 or 0x377f0683
57829** 4: File format version. Currently 3007000
57830** 8: Database page size. Example: 1024
57831** 12: Checkpoint sequence number
57832** 16: Salt-1, random integer incremented with each checkpoint
57833** 20: Salt-2, a different random integer changing with each ckpt
57834** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
57835** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
57836**
57837** Immediately following the wal-header are zero or more frames. Each
57838** frame consists of a 24-byte frame-header followed by a <page-size> bytes
57839** of page data. The frame-header is six big-endian 32-bit unsigned
57840** integer values, as follows:
57841**
57842** 0: Page number.
57843** 4: For commit records, the size of the database image in pages
57844** after the commit. For all other records, zero.
57845** 8: Salt-1 (copied from the header)
57846** 12: Salt-2 (copied from the header)
57847** 16: Checksum-1.
57848** 20: Checksum-2.
57849**
57850** A frame is considered valid if and only if the following conditions are
57851** true:
57852**
57853** (1) The salt-1 and salt-2 values in the frame-header match
57854** salt values in the wal-header
57855**
57856** (2) The checksum values in the final 8 bytes of the frame-header
57857** exactly match the checksum computed consecutively on the
57858** WAL header and the first 8 bytes and the content of all frames
57859** up to and including the current frame.
57860**
57861** The checksum is computed using 32-bit big-endian integers if the
57862** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
57863** is computed using little-endian if the magic number is 0x377f0682.
57864** The checksum values are always stored in the frame header in a
57865** big-endian format regardless of which byte order is used to compute
57866** the checksum. The checksum is computed by interpreting the input as
57867** an even number of unsigned 32-bit integers: x[0] through x[N]. The
57868** algorithm used for the checksum is as follows:
57869**
57870** for i from 0 to n-1 step 2:
57871** s0 += x[i] + s1;
57872** s1 += x[i+1] + s0;
57873** endfor
57874**
57875** Note that s0 and s1 are both weighted checksums using fibonacci weights
57876** in reverse order (the largest fibonacci weight occurs on the first element
57877** of the sequence being summed.) The s1 value spans all 32-bit
57878** terms of the sequence whereas s0 omits the final term.
57879**
57880** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
57881** WAL is transferred into the database, then the database is VFS.xSync-ed.
57882** The VFS.xSync operations serve as write barriers - all writes launched
57883** before the xSync must complete before any write that launches after the
57884** xSync begins.
57885**
57886** After each checkpoint, the salt-1 value is incremented and the salt-2
57887** value is randomized. This prevents old and new frames in the WAL from
57888** being considered valid at the same time and being checkpointing together
57889** following a crash.
57890**
57891** READER ALGORITHM
57892**
57893** To read a page from the database (call it page number P), a reader
57894** first checks the WAL to see if it contains page P. If so, then the
57895** last valid instance of page P that is a followed by a commit frame
57896** or is a commit frame itself becomes the value read. If the WAL
57897** contains no copies of page P that are valid and which are a commit
57898** frame or are followed by a commit frame, then page P is read from
57899** the database file.
57900**
57901** To start a read transaction, the reader records the index of the last
57902** valid frame in the WAL. The reader uses this recorded "mxFrame" value
57903** for all subsequent read operations. New transactions can be appended
57904** to the WAL, but as long as the reader uses its original mxFrame value
57905** and ignores the newly appended content, it will see a consistent snapshot
57906** of the database from a single point in time. This technique allows
57907** multiple concurrent readers to view different versions of the database
57908** content simultaneously.
57909**
57910** The reader algorithm in the previous paragraphs works correctly, but
57911** because frames for page P can appear anywhere within the WAL, the
57912** reader has to scan the entire WAL looking for page P frames. If the
57913** WAL is large (multiple megabytes is typical) that scan can be slow,
57914** and read performance suffers. To overcome this problem, a separate
57915** data structure called the wal-index is maintained to expedite the
57916** search for frames of a particular page.
57917**
57918** WAL-INDEX FORMAT
57919**
57920** Conceptually, the wal-index is shared memory, though VFS implementations
57921** might choose to implement the wal-index using a mmapped file. Because
57922** the wal-index is shared memory, SQLite does not support journal_mode=WAL
57923** on a network filesystem. All users of the database must be able to
57924** share memory.
57925**
57926** In the default unix and windows implementation, the wal-index is a mmapped
57927** file whose name is the database name with a "-shm" suffix added. For that
57928** reason, the wal-index is sometimes called the "shm" file.
57929**
57930** The wal-index is transient. After a crash, the wal-index can (and should
57931** be) reconstructed from the original WAL file. In fact, the VFS is required
57932** to either truncate or zero the header of the wal-index when the last
57933** connection to it closes. Because the wal-index is transient, it can
57934** use an architecture-specific format; it does not have to be cross-platform.
57935** Hence, unlike the database and WAL file formats which store all values
57936** as big endian, the wal-index can store multi-byte values in the native
57937** byte order of the host computer.
57938**
57939** The purpose of the wal-index is to answer this question quickly: Given
57940** a page number P and a maximum frame index M, return the index of the
57941** last frame in the wal before frame M for page P in the WAL, or return
57942** NULL if there are no frames for page P in the WAL prior to M.
57943**
57944** The wal-index consists of a header region, followed by an one or
57945** more index blocks.
57946**
57947** The wal-index header contains the total number of frames within the WAL
57948** in the mxFrame field.
57949**
57950** Each index block except for the first contains information on
57951** HASHTABLE_NPAGE frames. The first index block contains information on
57952** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
57953** HASHTABLE_NPAGE are selected so that together the wal-index header and
57954** first index block are the same size as all other index blocks in the
57955** wal-index.
57956**
57957** Each index block contains two sections, a page-mapping that contains the
57958** database page number associated with each wal frame, and a hash-table
57959** that allows readers to query an index block for a specific page number.
57960** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
57961** for the first index block) 32-bit page numbers. The first entry in the
57962** first index-block contains the database page number corresponding to the
57963** first frame in the WAL file. The first entry in the second index block
57964** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
57965** the log, and so on.
57966**
57967** The last index block in a wal-index usually contains less than the full
57968** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
57969** depending on the contents of the WAL file. This does not change the
57970** allocated size of the page-mapping array - the page-mapping array merely
57971** contains unused entries.
57972**
57973** Even without using the hash table, the last frame for page P
57974** can be found by scanning the page-mapping sections of each index block
57975** starting with the last index block and moving toward the first, and
57976** within each index block, starting at the end and moving toward the
57977** beginning. The first entry that equals P corresponds to the frame
57978** holding the content for that page.
57979**
57980** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
57981** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
57982** hash table for each page number in the mapping section, so the hash
57983** table is never more than half full. The expected number of collisions
57984** prior to finding a match is 1. Each entry of the hash table is an
57985** 1-based index of an entry in the mapping section of the same
57986** index block. Let K be the 1-based index of the largest entry in
57987** the mapping section. (For index blocks other than the last, K will
57988** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
57989** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
57990** contain a value of 0.
57991**
57992** To look for page P in the hash table, first compute a hash iKey on
57993** P as follows:
57994**
57995** iKey = (P * 383) % HASHTABLE_NSLOT
57996**
57997** Then start scanning entries of the hash table, starting with iKey
57998** (wrapping around to the beginning when the end of the hash table is
57999** reached) until an unused hash slot is found. Let the first unused slot
58000** be at index iUnused. (iUnused might be less than iKey if there was
58001** wrap-around.) Because the hash table is never more than half full,
58002** the search is guaranteed to eventually hit an unused entry. Let
58003** iMax be the value between iKey and iUnused, closest to iUnused,
58004** where aHash[iMax]==P. If there is no iMax entry (if there exists
58005** no hash slot such that aHash[i]==p) then page P is not in the
58006** current index block. Otherwise the iMax-th mapping entry of the
58007** current index block corresponds to the last entry that references
58008** page P.
58009**
58010** A hash search begins with the last index block and moves toward the
58011** first index block, looking for entries corresponding to page P. On
58012** average, only two or three slots in each index block need to be
58013** examined in order to either find the last entry for page P, or to
58014** establish that no such entry exists in the block. Each index block
58015** holds over 4000 entries. So two or three index blocks are sufficient
58016** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
58017** comparisons (on average) suffice to either locate a frame in the
58018** WAL or to establish that the frame does not exist in the WAL. This
58019** is much faster than scanning the entire 10MB WAL.
58020**
58021** Note that entries are added in order of increasing K. Hence, one
58022** reader might be using some value K0 and a second reader that started
58023** at a later time (after additional transactions were added to the WAL
58024** and to the wal-index) might be using a different value K1, where K1>K0.
58025** Both readers can use the same hash table and mapping section to get
58026** the correct result. There may be entries in the hash table with
58027** K>K0 but to the first reader, those entries will appear to be unused
58028** slots in the hash table and so the first reader will get an answer as
58029** if no values greater than K0 had ever been inserted into the hash table
58030** in the first place - which is what reader one wants. Meanwhile, the
58031** second reader using K1 will see additional values that were inserted
58032** later, which is exactly what reader two wants.
58033**
58034** When a rollback occurs, the value of K is decreased. Hash table entries
58035** that correspond to frames greater than the new K value are removed
58036** from the hash table at this point.
58037*/
58038#ifndef SQLITE_OMIT_WAL
58039
58040/* #include "wal.h" */
58041
58042/*
58043** Trace output macros
58044*/
58045#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
58046SQLITE_PRIVATE int sqlite3WalTrace = 0;
58047# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
58048#else
58049# define WALTRACE(X)
58050#endif
58051
58052/*
58053** WAL mode depends on atomic aligned 32-bit loads and stores in a few
58054** places. The following macros try to make this explicit.
58055*/
58056#if GCC_VESRION>=5004000
58057# define AtomicLoad(PTR) __atomic_load_n((PTR),__ATOMIC_RELAXED)
58058# define AtomicStore(PTR,VAL) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED)
58059#else
58060# define AtomicLoad(PTR) (*(PTR))
58061# define AtomicStore(PTR,VAL) (*(PTR) = (VAL))
58062#endif
58063
58064/*
58065** The maximum (and only) versions of the wal and wal-index formats
58066** that may be interpreted by this version of SQLite.
58067**
58068** If a client begins recovering a WAL file and finds that (a) the checksum
58069** values in the wal-header are correct and (b) the version field is not
58070** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
58071**
58072** Similarly, if a client successfully reads a wal-index header (i.e. the
58073** checksum test is successful) and finds that the version field is not
58074** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
58075** returns SQLITE_CANTOPEN.
58076*/
58077#define WAL_MAX_VERSION 3007000
58078#define WALINDEX_MAX_VERSION 3007000
58079
58080/*
58081** Index numbers for various locking bytes. WAL_NREADER is the number
58082** of available reader locks and should be at least 3. The default
58083** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5.
58084**
58085** Technically, the various VFSes are free to implement these locks however
58086** they see fit. However, compatibility is encouraged so that VFSes can
58087** interoperate. The standard implemention used on both unix and windows
58088** is for the index number to indicate a byte offset into the
58089** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
58090** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which
58091** should be 120) is the location in the shm file for the first locking
58092** byte.
58093*/
58094#define WAL_WRITE_LOCK 0
58095#define WAL_ALL_BUT_WRITE 1
58096#define WAL_CKPT_LOCK 1
58097#define WAL_RECOVER_LOCK 2
58098#define WAL_READ_LOCK(I) (3+(I))
58099#define WAL_NREADER (SQLITE_SHM_NLOCK-3)
58100
58101
58102/* Object declarations */
58103typedef struct WalIndexHdr WalIndexHdr;
58104typedef struct WalIterator WalIterator;
58105typedef struct WalCkptInfo WalCkptInfo;
58106
58107
58108/*
58109** The following object holds a copy of the wal-index header content.
58110**
58111** The actual header in the wal-index consists of two copies of this
58112** object followed by one instance of the WalCkptInfo object.
58113** For all versions of SQLite through 3.10.0 and probably beyond,
58114** the locking bytes (WalCkptInfo.aLock) start at offset 120 and
58115** the total header size is 136 bytes.
58116**
58117** The szPage value can be any power of 2 between 512 and 32768, inclusive.
58118** Or it can be 1 to represent a 65536-byte page. The latter case was
58119** added in 3.7.1 when support for 64K pages was added.
58120*/
58121struct WalIndexHdr {
58122 u32 iVersion; /* Wal-index version */
58123 u32 unused; /* Unused (padding) field */
58124 u32 iChange; /* Counter incremented each transaction */
58125 u8 isInit; /* 1 when initialized */
58126 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
58127 u16 szPage; /* Database page size in bytes. 1==64K */
58128 u32 mxFrame; /* Index of last valid frame in the WAL */
58129 u32 nPage; /* Size of database in pages */
58130 u32 aFrameCksum[2]; /* Checksum of last frame in log */
58131 u32 aSalt[2]; /* Two salt values copied from WAL header */
58132 u32 aCksum[2]; /* Checksum over all prior fields */
58133};
58134
58135/*
58136** A copy of the following object occurs in the wal-index immediately
58137** following the second copy of the WalIndexHdr. This object stores
58138** information used by checkpoint.
58139**
58140** nBackfill is the number of frames in the WAL that have been written
58141** back into the database. (We call the act of moving content from WAL to
58142** database "backfilling".) The nBackfill number is never greater than
58143** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
58144** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
58145** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
58146** mxFrame back to zero when the WAL is reset.
58147**
58148** nBackfillAttempted is the largest value of nBackfill that a checkpoint
58149** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however
58150** the nBackfillAttempted is set before any backfilling is done and the
58151** nBackfill is only set after all backfilling completes. So if a checkpoint
58152** crashes, nBackfillAttempted might be larger than nBackfill. The
58153** WalIndexHdr.mxFrame must never be less than nBackfillAttempted.
58154**
58155** The aLock[] field is a set of bytes used for locking. These bytes should
58156** never be read or written.
58157**
58158** There is one entry in aReadMark[] for each reader lock. If a reader
58159** holds read-lock K, then the value in aReadMark[K] is no greater than
58160** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
58161** for any aReadMark[] means that entry is unused. aReadMark[0] is
58162** a special case; its value is never used and it exists as a place-holder
58163** to avoid having to offset aReadMark[] indexs by one. Readers holding
58164** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
58165** directly from the database.
58166**
58167** The value of aReadMark[K] may only be changed by a thread that
58168** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
58169** aReadMark[K] cannot changed while there is a reader is using that mark
58170** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
58171**
58172** The checkpointer may only transfer frames from WAL to database where
58173** the frame numbers are less than or equal to every aReadMark[] that is
58174** in use (that is, every aReadMark[j] for which there is a corresponding
58175** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
58176** largest value and will increase an unused aReadMark[] to mxFrame if there
58177** is not already an aReadMark[] equal to mxFrame. The exception to the
58178** previous sentence is when nBackfill equals mxFrame (meaning that everything
58179** in the WAL has been backfilled into the database) then new readers
58180** will choose aReadMark[0] which has value 0 and hence such reader will
58181** get all their all content directly from the database file and ignore
58182** the WAL.
58183**
58184** Writers normally append new frames to the end of the WAL. However,
58185** if nBackfill equals mxFrame (meaning that all WAL content has been
58186** written back into the database) and if no readers are using the WAL
58187** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
58188** the writer will first "reset" the WAL back to the beginning and start
58189** writing new content beginning at frame 1.
58190**
58191** We assume that 32-bit loads are atomic and so no locks are needed in
58192** order to read from any aReadMark[] entries.
58193*/
58194struct WalCkptInfo {
58195 u32 nBackfill; /* Number of WAL frames backfilled into DB */
58196 u32 aReadMark[WAL_NREADER]; /* Reader marks */
58197 u8 aLock[SQLITE_SHM_NLOCK]; /* Reserved space for locks */
58198 u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */
58199 u32 notUsed0; /* Available for future enhancements */
58200};
58201#define READMARK_NOT_USED 0xffffffff
58202
58203
58204/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
58205** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
58206** only support mandatory file-locks, we do not read or write data
58207** from the region of the file on which locks are applied.
58208*/
58209#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock))
58210#define WALINDEX_HDR_SIZE (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
58211
58212/* Size of header before each frame in wal */
58213#define WAL_FRAME_HDRSIZE 24
58214
58215/* Size of write ahead log header, including checksum. */
58216#define WAL_HDRSIZE 32
58217
58218/* WAL magic value. Either this value, or the same value with the least
58219** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
58220** big-endian format in the first 4 bytes of a WAL file.
58221**
58222** If the LSB is set, then the checksums for each frame within the WAL
58223** file are calculated by treating all data as an array of 32-bit
58224** big-endian words. Otherwise, they are calculated by interpreting
58225** all data as 32-bit little-endian words.
58226*/
58227#define WAL_MAGIC 0x377f0682
58228
58229/*
58230** Return the offset of frame iFrame in the write-ahead log file,
58231** assuming a database page size of szPage bytes. The offset returned
58232** is to the start of the write-ahead log frame-header.
58233*/
58234#define walFrameOffset(iFrame, szPage) ( \
58235 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
58236)
58237
58238/*
58239** An open write-ahead log file is represented by an instance of the
58240** following object.
58241*/
58242struct Wal {
58243 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
58244 sqlite3_file *pDbFd; /* File handle for the database file */
58245 sqlite3_file *pWalFd; /* File handle for WAL file */
58246 u32 iCallback; /* Value to pass to log callback (or 0) */
58247 i64 mxWalSize; /* Truncate WAL to this size upon reset */
58248 int nWiData; /* Size of array apWiData */
58249 int szFirstBlock; /* Size of first block written to WAL file */
58250 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
58251 u32 szPage; /* Database page size */
58252 i16 readLock; /* Which read lock is being held. -1 for none */
58253 u8 syncFlags; /* Flags to use to sync header writes */
58254 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
58255 u8 writeLock; /* True if in a write transaction */
58256 u8 ckptLock; /* True if holding a checkpoint lock */
58257 u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
58258 u8 truncateOnCommit; /* True to truncate WAL file on commit */
58259 u8 syncHeader; /* Fsync the WAL header if true */
58260 u8 padToSectorBoundary; /* Pad transactions out to the next sector */
58261 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
58262 WalIndexHdr hdr; /* Wal-index header for current transaction */
58263 u32 minFrame; /* Ignore wal frames before this one */
58264 u32 iReCksum; /* On commit, recalculate checksums from here */
58265 const char *zWalName; /* Name of WAL file */
58266 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
58267#ifdef SQLITE_DEBUG
58268 u8 lockError; /* True if a locking error has occurred */
58269#endif
58270#ifdef SQLITE_ENABLE_SNAPSHOT
58271 WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */
58272#endif
58273};
58274
58275/*
58276** Candidate values for Wal.exclusiveMode.
58277*/
58278#define WAL_NORMAL_MODE 0
58279#define WAL_EXCLUSIVE_MODE 1
58280#define WAL_HEAPMEMORY_MODE 2
58281
58282/*
58283** Possible values for WAL.readOnly
58284*/
58285#define WAL_RDWR 0 /* Normal read/write connection */
58286#define WAL_RDONLY 1 /* The WAL file is readonly */
58287#define WAL_SHM_RDONLY 2 /* The SHM file is readonly */
58288
58289/*
58290** Each page of the wal-index mapping contains a hash-table made up of
58291** an array of HASHTABLE_NSLOT elements of the following type.
58292*/
58293typedef u16 ht_slot;
58294
58295/*
58296** This structure is used to implement an iterator that loops through
58297** all frames in the WAL in database page order. Where two or more frames
58298** correspond to the same database page, the iterator visits only the
58299** frame most recently written to the WAL (in other words, the frame with
58300** the largest index).
58301**
58302** The internals of this structure are only accessed by:
58303**
58304** walIteratorInit() - Create a new iterator,
58305** walIteratorNext() - Step an iterator,
58306** walIteratorFree() - Free an iterator.
58307**
58308** This functionality is used by the checkpoint code (see walCheckpoint()).
58309*/
58310struct WalIterator {
58311 int iPrior; /* Last result returned from the iterator */
58312 int nSegment; /* Number of entries in aSegment[] */
58313 struct WalSegment {
58314 int iNext; /* Next slot in aIndex[] not yet returned */
58315 ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
58316 u32 *aPgno; /* Array of page numbers. */
58317 int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */
58318 int iZero; /* Frame number associated with aPgno[0] */
58319 } aSegment[1]; /* One for every 32KB page in the wal-index */
58320};
58321
58322/*
58323** Define the parameters of the hash tables in the wal-index file. There
58324** is a hash-table following every HASHTABLE_NPAGE page numbers in the
58325** wal-index.
58326**
58327** Changing any of these constants will alter the wal-index format and
58328** create incompatibilities.
58329*/
58330#define HASHTABLE_NPAGE 4096 /* Must be power of 2 */
58331#define HASHTABLE_HASH_1 383 /* Should be prime */
58332#define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */
58333
58334/*
58335** The block of page numbers associated with the first hash-table in a
58336** wal-index is smaller than usual. This is so that there is a complete
58337** hash-table on each aligned 32KB page of the wal-index.
58338*/
58339#define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
58340
58341/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
58342#define WALINDEX_PGSZ ( \
58343 sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
58344)
58345
58346/*
58347** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
58348** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
58349** numbered from zero.
58350**
58351** If the wal-index is currently smaller the iPage pages then the size
58352** of the wal-index might be increased, but only if it is safe to do
58353** so. It is safe to enlarge the wal-index if pWal->writeLock is true
58354** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
58355**
58356** If this call is successful, *ppPage is set to point to the wal-index
58357** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
58358** then an SQLite error code is returned and *ppPage is set to 0.
58359*/
58360static SQLITE_NOINLINE int walIndexPageRealloc(
58361 Wal *pWal, /* The WAL context */
58362 int iPage, /* The page we seek */
58363 volatile u32 **ppPage /* Write the page pointer here */
58364){
58365 int rc = SQLITE_OK;
58366
58367 /* Enlarge the pWal->apWiData[] array if required */
58368 if( pWal->nWiData<=iPage ){
58369 int nByte = sizeof(u32*)*(iPage+1);
58370 volatile u32 **apNew;
58371 apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
58372 if( !apNew ){
58373 *ppPage = 0;
58374 return SQLITE_NOMEM_BKPT;
58375 }
58376 memset((void*)&apNew[pWal->nWiData], 0,
58377 sizeof(u32*)*(iPage+1-pWal->nWiData));
58378 pWal->apWiData = apNew;
58379 pWal->nWiData = iPage+1;
58380 }
58381
58382 /* Request a pointer to the required page from the VFS */
58383 assert( pWal->apWiData[iPage]==0 );
58384 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
58385 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
58386 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
58387 }else{
58388 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
58389 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
58390 );
58391 assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
58392 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
58393 if( (rc&0xff)==SQLITE_READONLY ){
58394 pWal->readOnly |= WAL_SHM_RDONLY;
58395 if( rc==SQLITE_READONLY ){
58396 rc = SQLITE_OK;
58397 }
58398 }
58399 }
58400
58401 *ppPage = pWal->apWiData[iPage];
58402 assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
58403 return rc;
58404}
58405static int walIndexPage(
58406 Wal *pWal, /* The WAL context */
58407 int iPage, /* The page we seek */
58408 volatile u32 **ppPage /* Write the page pointer here */
58409){
58410 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
58411 return walIndexPageRealloc(pWal, iPage, ppPage);
58412 }
58413 return SQLITE_OK;
58414}
58415
58416/*
58417** Return a pointer to the WalCkptInfo structure in the wal-index.
58418*/
58419static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
58420 assert( pWal->nWiData>0 && pWal->apWiData[0] );
58421 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
58422}
58423
58424/*
58425** Return a pointer to the WalIndexHdr structure in the wal-index.
58426*/
58427static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
58428 assert( pWal->nWiData>0 && pWal->apWiData[0] );
58429 return (volatile WalIndexHdr*)pWal->apWiData[0];
58430}
58431
58432/*
58433** The argument to this macro must be of type u32. On a little-endian
58434** architecture, it returns the u32 value that results from interpreting
58435** the 4 bytes as a big-endian value. On a big-endian architecture, it
58436** returns the value that would be produced by interpreting the 4 bytes
58437** of the input value as a little-endian integer.
58438*/
58439#define BYTESWAP32(x) ( \
58440 (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
58441 + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
58442)
58443
58444/*
58445** Generate or extend an 8 byte checksum based on the data in
58446** array aByte[] and the initial values of aIn[0] and aIn[1] (or
58447** initial values of 0 and 0 if aIn==NULL).
58448**
58449** The checksum is written back into aOut[] before returning.
58450**
58451** nByte must be a positive multiple of 8.
58452*/
58453static void walChecksumBytes(
58454 int nativeCksum, /* True for native byte-order, false for non-native */
58455 u8 *a, /* Content to be checksummed */
58456 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
58457 const u32 *aIn, /* Initial checksum value input */
58458 u32 *aOut /* OUT: Final checksum value output */
58459){
58460 u32 s1, s2;
58461 u32 *aData = (u32 *)a;
58462 u32 *aEnd = (u32 *)&a[nByte];
58463
58464 if( aIn ){
58465 s1 = aIn[0];
58466 s2 = aIn[1];
58467 }else{
58468 s1 = s2 = 0;
58469 }
58470
58471 assert( nByte>=8 );
58472 assert( (nByte&0x00000007)==0 );
58473
58474 if( nativeCksum ){
58475 do {
58476 s1 += *aData++ + s2;
58477 s2 += *aData++ + s1;
58478 }while( aData<aEnd );
58479 }else{
58480 do {
58481 s1 += BYTESWAP32(aData[0]) + s2;
58482 s2 += BYTESWAP32(aData[1]) + s1;
58483 aData += 2;
58484 }while( aData<aEnd );
58485 }
58486
58487 aOut[0] = s1;
58488 aOut[1] = s2;
58489}
58490
58491static void walShmBarrier(Wal *pWal){
58492 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
58493 sqlite3OsShmBarrier(pWal->pDbFd);
58494 }
58495}
58496
58497/*
58498** Write the header information in pWal->hdr into the wal-index.
58499**
58500** The checksum on pWal->hdr is updated before it is written.
58501*/
58502static void walIndexWriteHdr(Wal *pWal){
58503 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
58504 const int nCksum = offsetof(WalIndexHdr, aCksum);
58505
58506 assert( pWal->writeLock );
58507 pWal->hdr.isInit = 1;
58508 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
58509 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
58510 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
58511 walShmBarrier(pWal);
58512 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
58513}
58514
58515/*
58516** This function encodes a single frame header and writes it to a buffer
58517** supplied by the caller. A frame-header is made up of a series of
58518** 4-byte big-endian integers, as follows:
58519**
58520** 0: Page number.
58521** 4: For commit records, the size of the database image in pages
58522** after the commit. For all other records, zero.
58523** 8: Salt-1 (copied from the wal-header)
58524** 12: Salt-2 (copied from the wal-header)
58525** 16: Checksum-1.
58526** 20: Checksum-2.
58527*/
58528static void walEncodeFrame(
58529 Wal *pWal, /* The write-ahead log */
58530 u32 iPage, /* Database page number for frame */
58531 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
58532 u8 *aData, /* Pointer to page data */
58533 u8 *aFrame /* OUT: Write encoded frame here */
58534){
58535 int nativeCksum; /* True for native byte-order checksums */
58536 u32 *aCksum = pWal->hdr.aFrameCksum;
58537 assert( WAL_FRAME_HDRSIZE==24 );
58538 sqlite3Put4byte(&aFrame[0], iPage);
58539 sqlite3Put4byte(&aFrame[4], nTruncate);
58540 if( pWal->iReCksum==0 ){
58541 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
58542
58543 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
58544 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
58545 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
58546
58547 sqlite3Put4byte(&aFrame[16], aCksum[0]);
58548 sqlite3Put4byte(&aFrame[20], aCksum[1]);
58549 }else{
58550 memset(&aFrame[8], 0, 16);
58551 }
58552}
58553
58554/*
58555** Check to see if the frame with header in aFrame[] and content
58556** in aData[] is valid. If it is a valid frame, fill *piPage and
58557** *pnTruncate and return true. Return if the frame is not valid.
58558*/
58559static int walDecodeFrame(
58560 Wal *pWal, /* The write-ahead log */
58561 u32 *piPage, /* OUT: Database page number for frame */
58562 u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
58563 u8 *aData, /* Pointer to page data (for checksum) */
58564 u8 *aFrame /* Frame data */
58565){
58566 int nativeCksum; /* True for native byte-order checksums */
58567 u32 *aCksum = pWal->hdr.aFrameCksum;
58568 u32 pgno; /* Page number of the frame */
58569 assert( WAL_FRAME_HDRSIZE==24 );
58570
58571 /* A frame is only valid if the salt values in the frame-header
58572 ** match the salt values in the wal-header.
58573 */
58574 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
58575 return 0;
58576 }
58577
58578 /* A frame is only valid if the page number is creater than zero.
58579 */
58580 pgno = sqlite3Get4byte(&aFrame[0]);
58581 if( pgno==0 ){
58582 return 0;
58583 }
58584
58585 /* A frame is only valid if a checksum of the WAL header,
58586 ** all prior frams, the first 16 bytes of this frame-header,
58587 ** and the frame-data matches the checksum in the last 8
58588 ** bytes of this frame-header.
58589 */
58590 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
58591 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
58592 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
58593 if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
58594 || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
58595 ){
58596 /* Checksum failed. */
58597 return 0;
58598 }
58599
58600 /* If we reach this point, the frame is valid. Return the page number
58601 ** and the new database size.
58602 */
58603 *piPage = pgno;
58604 *pnTruncate = sqlite3Get4byte(&aFrame[4]);
58605 return 1;
58606}
58607
58608
58609#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
58610/*
58611** Names of locks. This routine is used to provide debugging output and is not
58612** a part of an ordinary build.
58613*/
58614static const char *walLockName(int lockIdx){
58615 if( lockIdx==WAL_WRITE_LOCK ){
58616 return "WRITE-LOCK";
58617 }else if( lockIdx==WAL_CKPT_LOCK ){
58618 return "CKPT-LOCK";
58619 }else if( lockIdx==WAL_RECOVER_LOCK ){
58620 return "RECOVER-LOCK";
58621 }else{
58622 static char zName[15];
58623 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
58624 lockIdx-WAL_READ_LOCK(0));
58625 return zName;
58626 }
58627}
58628#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
58629
58630
58631/*
58632** Set or release locks on the WAL. Locks are either shared or exclusive.
58633** A lock cannot be moved directly between shared and exclusive - it must go
58634** through the unlocked state first.
58635**
58636** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
58637*/
58638static int walLockShared(Wal *pWal, int lockIdx){
58639 int rc;
58640 if( pWal->exclusiveMode ) return SQLITE_OK;
58641 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
58642 SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
58643 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
58644 walLockName(lockIdx), rc ? "failed" : "ok"));
58645 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
58646 return rc;
58647}
58648static void walUnlockShared(Wal *pWal, int lockIdx){
58649 if( pWal->exclusiveMode ) return;
58650 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
58651 SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
58652 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
58653}
58654static int walLockExclusive(Wal *pWal, int lockIdx, int n){
58655 int rc;
58656 if( pWal->exclusiveMode ) return SQLITE_OK;
58657 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
58658 SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
58659 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
58660 walLockName(lockIdx), n, rc ? "failed" : "ok"));
58661 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
58662 return rc;
58663}
58664static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
58665 if( pWal->exclusiveMode ) return;
58666 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
58667 SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
58668 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
58669 walLockName(lockIdx), n));
58670}
58671
58672/*
58673** Compute a hash on a page number. The resulting hash value must land
58674** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
58675** the hash to the next value in the event of a collision.
58676*/
58677static int walHash(u32 iPage){
58678 assert( iPage>0 );
58679 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
58680 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
58681}
58682static int walNextHash(int iPriorHash){
58683 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
58684}
58685
58686/*
58687** An instance of the WalHashLoc object is used to describe the location
58688** of a page hash table in the wal-index. This becomes the return value
58689** from walHashGet().
58690*/
58691typedef struct WalHashLoc WalHashLoc;
58692struct WalHashLoc {
58693 volatile ht_slot *aHash; /* Start of the wal-index hash table */
58694 volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */
58695 u32 iZero; /* One less than the frame number of first indexed*/
58696};
58697
58698/*
58699** Return pointers to the hash table and page number array stored on
58700** page iHash of the wal-index. The wal-index is broken into 32KB pages
58701** numbered starting from 0.
58702**
58703** Set output variable pLoc->aHash to point to the start of the hash table
58704** in the wal-index file. Set pLoc->iZero to one less than the frame
58705** number of the first frame indexed by this hash table. If a
58706** slot in the hash table is set to N, it refers to frame number
58707** (pLoc->iZero+N) in the log.
58708**
58709** Finally, set pLoc->aPgno so that pLoc->aPgno[1] is the page number of the
58710** first frame indexed by the hash table, frame (pLoc->iZero+1).
58711*/
58712static int walHashGet(
58713 Wal *pWal, /* WAL handle */
58714 int iHash, /* Find the iHash'th table */
58715 WalHashLoc *pLoc /* OUT: Hash table location */
58716){
58717 int rc; /* Return code */
58718
58719 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
58720 assert( rc==SQLITE_OK || iHash>0 );
58721
58722 if( rc==SQLITE_OK ){
58723 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
58724 if( iHash==0 ){
58725 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
58726 pLoc->iZero = 0;
58727 }else{
58728 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
58729 }
58730 pLoc->aPgno = &pLoc->aPgno[-1];
58731 }
58732 return rc;
58733}
58734
58735/*
58736** Return the number of the wal-index page that contains the hash-table
58737** and page-number array that contain entries corresponding to WAL frame
58738** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
58739** are numbered starting from 0.
58740*/
58741static int walFramePage(u32 iFrame){
58742 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
58743 assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
58744 && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
58745 && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
58746 && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
58747 && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
58748 );
58749 return iHash;
58750}
58751
58752/*
58753** Return the page number associated with frame iFrame in this WAL.
58754*/
58755static u32 walFramePgno(Wal *pWal, u32 iFrame){
58756 int iHash = walFramePage(iFrame);
58757 if( iHash==0 ){
58758 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
58759 }
58760 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
58761}
58762
58763/*
58764** Remove entries from the hash table that point to WAL slots greater
58765** than pWal->hdr.mxFrame.
58766**
58767** This function is called whenever pWal->hdr.mxFrame is decreased due
58768** to a rollback or savepoint.
58769**
58770** At most only the hash table containing pWal->hdr.mxFrame needs to be
58771** updated. Any later hash tables will be automatically cleared when
58772** pWal->hdr.mxFrame advances to the point where those hash tables are
58773** actually needed.
58774*/
58775static void walCleanupHash(Wal *pWal){
58776 WalHashLoc sLoc; /* Hash table location */
58777 int iLimit = 0; /* Zero values greater than this */
58778 int nByte; /* Number of bytes to zero in aPgno[] */
58779 int i; /* Used to iterate through aHash[] */
58780
58781 assert( pWal->writeLock );
58782 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
58783 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
58784 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
58785
58786 if( pWal->hdr.mxFrame==0 ) return;
58787
58788 /* Obtain pointers to the hash-table and page-number array containing
58789 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
58790 ** that the page said hash-table and array reside on is already mapped.
58791 */
58792 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
58793 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
58794 walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
58795
58796 /* Zero all hash-table entries that correspond to frame numbers greater
58797 ** than pWal->hdr.mxFrame.
58798 */
58799 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
58800 assert( iLimit>0 );
58801 for(i=0; i<HASHTABLE_NSLOT; i++){
58802 if( sLoc.aHash[i]>iLimit ){
58803 sLoc.aHash[i] = 0;
58804 }
58805 }
58806
58807 /* Zero the entries in the aPgno array that correspond to frames with
58808 ** frame numbers greater than pWal->hdr.mxFrame.
58809 */
58810 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit+1]);
58811 memset((void *)&sLoc.aPgno[iLimit+1], 0, nByte);
58812
58813#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
58814 /* Verify that the every entry in the mapping region is still reachable
58815 ** via the hash table even after the cleanup.
58816 */
58817 if( iLimit ){
58818 int j; /* Loop counter */
58819 int iKey; /* Hash key */
58820 for(j=1; j<=iLimit; j++){
58821 for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){
58822 if( sLoc.aHash[iKey]==j ) break;
58823 }
58824 assert( sLoc.aHash[iKey]==j );
58825 }
58826 }
58827#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
58828}
58829
58830
58831/*
58832** Set an entry in the wal-index that will map database page number
58833** pPage into WAL frame iFrame.
58834*/
58835static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
58836 int rc; /* Return code */
58837 WalHashLoc sLoc; /* Wal-index hash table location */
58838
58839 rc = walHashGet(pWal, walFramePage(iFrame), &sLoc);
58840
58841 /* Assuming the wal-index file was successfully mapped, populate the
58842 ** page number array and hash table entry.
58843 */
58844 if( rc==SQLITE_OK ){
58845 int iKey; /* Hash table key */
58846 int idx; /* Value to write to hash-table slot */
58847 int nCollide; /* Number of hash collisions */
58848
58849 idx = iFrame - sLoc.iZero;
58850 assert( idx <= HASHTABLE_NSLOT/2 + 1 );
58851
58852 /* If this is the first entry to be added to this hash-table, zero the
58853 ** entire hash table and aPgno[] array before proceeding.
58854 */
58855 if( idx==1 ){
58856 int nByte = (int)((u8 *)&sLoc.aHash[HASHTABLE_NSLOT]
58857 - (u8 *)&sLoc.aPgno[1]);
58858 memset((void*)&sLoc.aPgno[1], 0, nByte);
58859 }
58860
58861 /* If the entry in aPgno[] is already set, then the previous writer
58862 ** must have exited unexpectedly in the middle of a transaction (after
58863 ** writing one or more dirty pages to the WAL to free up memory).
58864 ** Remove the remnants of that writers uncommitted transaction from
58865 ** the hash-table before writing any new entries.
58866 */
58867 if( sLoc.aPgno[idx] ){
58868 walCleanupHash(pWal);
58869 assert( !sLoc.aPgno[idx] );
58870 }
58871
58872 /* Write the aPgno[] array entry and the hash-table slot. */
58873 nCollide = idx;
58874 for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
58875 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
58876 }
58877 sLoc.aPgno[idx] = iPage;
58878 sLoc.aHash[iKey] = (ht_slot)idx;
58879
58880#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
58881 /* Verify that the number of entries in the hash table exactly equals
58882 ** the number of entries in the mapping region.
58883 */
58884 {
58885 int i; /* Loop counter */
58886 int nEntry = 0; /* Number of entries in the hash table */
58887 for(i=0; i<HASHTABLE_NSLOT; i++){ if( sLoc.aHash[i] ) nEntry++; }
58888 assert( nEntry==idx );
58889 }
58890
58891 /* Verify that the every entry in the mapping region is reachable
58892 ** via the hash table. This turns out to be a really, really expensive
58893 ** thing to check, so only do this occasionally - not on every
58894 ** iteration.
58895 */
58896 if( (idx&0x3ff)==0 ){
58897 int i; /* Loop counter */
58898 for(i=1; i<=idx; i++){
58899 for(iKey=walHash(sLoc.aPgno[i]);
58900 sLoc.aHash[iKey];
58901 iKey=walNextHash(iKey)){
58902 if( sLoc.aHash[iKey]==i ) break;
58903 }
58904 assert( sLoc.aHash[iKey]==i );
58905 }
58906 }
58907#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
58908 }
58909
58910
58911 return rc;
58912}
58913
58914
58915/*
58916** Recover the wal-index by reading the write-ahead log file.
58917**
58918** This routine first tries to establish an exclusive lock on the
58919** wal-index to prevent other threads/processes from doing anything
58920** with the WAL or wal-index while recovery is running. The
58921** WAL_RECOVER_LOCK is also held so that other threads will know
58922** that this thread is running recovery. If unable to establish
58923** the necessary locks, this routine returns SQLITE_BUSY.
58924*/
58925static int walIndexRecover(Wal *pWal){
58926 int rc; /* Return Code */
58927 i64 nSize; /* Size of log file */
58928 u32 aFrameCksum[2] = {0, 0};
58929 int iLock; /* Lock offset to lock for checkpoint */
58930
58931 /* Obtain an exclusive lock on all byte in the locking range not already
58932 ** locked by the caller. The caller is guaranteed to have locked the
58933 ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
58934 ** If successful, the same bytes that are locked here are unlocked before
58935 ** this function returns.
58936 */
58937 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
58938 assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
58939 assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
58940 assert( pWal->writeLock );
58941 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
58942 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
58943 if( rc==SQLITE_OK ){
58944 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
58945 if( rc!=SQLITE_OK ){
58946 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
58947 }
58948 }
58949 if( rc ){
58950 return rc;
58951 }
58952
58953 WALTRACE(("WAL%p: recovery begin...\n", pWal));
58954
58955 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
58956
58957 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
58958 if( rc!=SQLITE_OK ){
58959 goto recovery_error;
58960 }
58961
58962 if( nSize>WAL_HDRSIZE ){
58963 u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
58964 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
58965 int szFrame; /* Number of bytes in buffer aFrame[] */
58966 u8 *aData; /* Pointer to data part of aFrame buffer */
58967 int iFrame; /* Index of last frame read */
58968 i64 iOffset; /* Next offset to read from log file */
58969 int szPage; /* Page size according to the log */
58970 u32 magic; /* Magic value read from WAL header */
58971 u32 version; /* Magic value read from WAL header */
58972 int isValid; /* True if this frame is valid */
58973
58974 /* Read in the WAL header. */
58975 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
58976 if( rc!=SQLITE_OK ){
58977 goto recovery_error;
58978 }
58979
58980 /* If the database page size is not a power of two, or is greater than
58981 ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
58982 ** data. Similarly, if the 'magic' value is invalid, ignore the whole
58983 ** WAL file.
58984 */
58985 magic = sqlite3Get4byte(&aBuf[0]);
58986 szPage = sqlite3Get4byte(&aBuf[8]);
58987 if( (magic&0xFFFFFFFE)!=WAL_MAGIC
58988 || szPage&(szPage-1)
58989 || szPage>SQLITE_MAX_PAGE_SIZE
58990 || szPage<512
58991 ){
58992 goto finished;
58993 }
58994 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
58995 pWal->szPage = szPage;
58996 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
58997 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
58998
58999 /* Verify that the WAL header checksum is correct */
59000 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
59001 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
59002 );
59003 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
59004 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
59005 ){
59006 goto finished;
59007 }
59008
59009 /* Verify that the version number on the WAL format is one that
59010 ** are able to understand */
59011 version = sqlite3Get4byte(&aBuf[4]);
59012 if( version!=WAL_MAX_VERSION ){
59013 rc = SQLITE_CANTOPEN_BKPT;
59014 goto finished;
59015 }
59016
59017 /* Malloc a buffer to read frames into. */
59018 szFrame = szPage + WAL_FRAME_HDRSIZE;
59019 aFrame = (u8 *)sqlite3_malloc64(szFrame);
59020 if( !aFrame ){
59021 rc = SQLITE_NOMEM_BKPT;
59022 goto recovery_error;
59023 }
59024 aData = &aFrame[WAL_FRAME_HDRSIZE];
59025
59026 /* Read all frames from the log file. */
59027 iFrame = 0;
59028 for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
59029 u32 pgno; /* Database page number for frame */
59030 u32 nTruncate; /* dbsize field from frame header */
59031
59032 /* Read and decode the next log frame. */
59033 iFrame++;
59034 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
59035 if( rc!=SQLITE_OK ) break;
59036 isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
59037 if( !isValid ) break;
59038 rc = walIndexAppend(pWal, iFrame, pgno);
59039 if( rc!=SQLITE_OK ) break;
59040
59041 /* If nTruncate is non-zero, this is a commit record. */
59042 if( nTruncate ){
59043 pWal->hdr.mxFrame = iFrame;
59044 pWal->hdr.nPage = nTruncate;
59045 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
59046 testcase( szPage<=32768 );
59047 testcase( szPage>=65536 );
59048 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
59049 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
59050 }
59051 }
59052
59053 sqlite3_free(aFrame);
59054 }
59055
59056finished:
59057 if( rc==SQLITE_OK ){
59058 volatile WalCkptInfo *pInfo;
59059 int i;
59060 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
59061 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
59062 walIndexWriteHdr(pWal);
59063
59064 /* Reset the checkpoint-header. This is safe because this thread is
59065 ** currently holding locks that exclude all other readers, writers and
59066 ** checkpointers.
59067 */
59068 pInfo = walCkptInfo(pWal);
59069 pInfo->nBackfill = 0;
59070 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
59071 pInfo->aReadMark[0] = 0;
59072 for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
59073 if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
59074
59075 /* If more than one frame was recovered from the log file, report an
59076 ** event via sqlite3_log(). This is to help with identifying performance
59077 ** problems caused by applications routinely shutting down without
59078 ** checkpointing the log file.
59079 */
59080 if( pWal->hdr.nPage ){
59081 sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
59082 "recovered %d frames from WAL file %s",
59083 pWal->hdr.mxFrame, pWal->zWalName
59084 );
59085 }
59086 }
59087
59088recovery_error:
59089 WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
59090 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
59091 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
59092 return rc;
59093}
59094
59095/*
59096** Close an open wal-index.
59097*/
59098static void walIndexClose(Wal *pWal, int isDelete){
59099 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
59100 int i;
59101 for(i=0; i<pWal->nWiData; i++){
59102 sqlite3_free((void *)pWal->apWiData[i]);
59103 pWal->apWiData[i] = 0;
59104 }
59105 }
59106 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
59107 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
59108 }
59109}
59110
59111/*
59112** Open a connection to the WAL file zWalName. The database file must
59113** already be opened on connection pDbFd. The buffer that zWalName points
59114** to must remain valid for the lifetime of the returned Wal* handle.
59115**
59116** A SHARED lock should be held on the database file when this function
59117** is called. The purpose of this SHARED lock is to prevent any other
59118** client from unlinking the WAL or wal-index file. If another process
59119** were to do this just after this client opened one of these files, the
59120** system would be badly broken.
59121**
59122** If the log file is successfully opened, SQLITE_OK is returned and
59123** *ppWal is set to point to a new WAL handle. If an error occurs,
59124** an SQLite error code is returned and *ppWal is left unmodified.
59125*/
59126SQLITE_PRIVATE int sqlite3WalOpen(
59127 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
59128 sqlite3_file *pDbFd, /* The open database file */
59129 const char *zWalName, /* Name of the WAL file */
59130 int bNoShm, /* True to run in heap-memory mode */
59131 i64 mxWalSize, /* Truncate WAL to this size on reset */
59132 Wal **ppWal /* OUT: Allocated Wal handle */
59133){
59134 int rc; /* Return Code */
59135 Wal *pRet; /* Object to allocate and return */
59136 int flags; /* Flags passed to OsOpen() */
59137
59138 assert( zWalName && zWalName[0] );
59139 assert( pDbFd );
59140
59141 /* In the amalgamation, the os_unix.c and os_win.c source files come before
59142 ** this source file. Verify that the #defines of the locking byte offsets
59143 ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
59144 ** For that matter, if the lock offset ever changes from its initial design
59145 ** value of 120, we need to know that so there is an assert() to check it.
59146 */
59147 assert( 120==WALINDEX_LOCK_OFFSET );
59148 assert( 136==WALINDEX_HDR_SIZE );
59149#ifdef WIN_SHM_BASE
59150 assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
59151#endif
59152#ifdef UNIX_SHM_BASE
59153 assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
59154#endif
59155
59156
59157 /* Allocate an instance of struct Wal to return. */
59158 *ppWal = 0;
59159 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
59160 if( !pRet ){
59161 return SQLITE_NOMEM_BKPT;
59162 }
59163
59164 pRet->pVfs = pVfs;
59165 pRet->pWalFd = (sqlite3_file *)&pRet[1];
59166 pRet->pDbFd = pDbFd;
59167 pRet->readLock = -1;
59168 pRet->mxWalSize = mxWalSize;
59169 pRet->zWalName = zWalName;
59170 pRet->syncHeader = 1;
59171 pRet->padToSectorBoundary = 1;
59172 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
59173
59174 /* Open file handle on the write-ahead log file. */
59175 flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
59176 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
59177 if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
59178 pRet->readOnly = WAL_RDONLY;
59179 }
59180
59181 if( rc!=SQLITE_OK ){
59182 walIndexClose(pRet, 0);
59183 sqlite3OsClose(pRet->pWalFd);
59184 sqlite3_free(pRet);
59185 }else{
59186 int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
59187 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
59188 if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
59189 pRet->padToSectorBoundary = 0;
59190 }
59191 *ppWal = pRet;
59192 WALTRACE(("WAL%d: opened\n", pRet));
59193 }
59194 return rc;
59195}
59196
59197/*
59198** Change the size to which the WAL file is trucated on each reset.
59199*/
59200SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
59201 if( pWal ) pWal->mxWalSize = iLimit;
59202}
59203
59204/*
59205** Find the smallest page number out of all pages held in the WAL that
59206** has not been returned by any prior invocation of this method on the
59207** same WalIterator object. Write into *piFrame the frame index where
59208** that page was last written into the WAL. Write into *piPage the page
59209** number.
59210**
59211** Return 0 on success. If there are no pages in the WAL with a page
59212** number larger than *piPage, then return 1.
59213*/
59214static int walIteratorNext(
59215 WalIterator *p, /* Iterator */
59216 u32 *piPage, /* OUT: The page number of the next page */
59217 u32 *piFrame /* OUT: Wal frame index of next page */
59218){
59219 u32 iMin; /* Result pgno must be greater than iMin */
59220 u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
59221 int i; /* For looping through segments */
59222
59223 iMin = p->iPrior;
59224 assert( iMin<0xffffffff );
59225 for(i=p->nSegment-1; i>=0; i--){
59226 struct WalSegment *pSegment = &p->aSegment[i];
59227 while( pSegment->iNext<pSegment->nEntry ){
59228 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
59229 if( iPg>iMin ){
59230 if( iPg<iRet ){
59231 iRet = iPg;
59232 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
59233 }
59234 break;
59235 }
59236 pSegment->iNext++;
59237 }
59238 }
59239
59240 *piPage = p->iPrior = iRet;
59241 return (iRet==0xFFFFFFFF);
59242}
59243
59244/*
59245** This function merges two sorted lists into a single sorted list.
59246**
59247** aLeft[] and aRight[] are arrays of indices. The sort key is
59248** aContent[aLeft[]] and aContent[aRight[]]. Upon entry, the following
59249** is guaranteed for all J<K:
59250**
59251** aContent[aLeft[J]] < aContent[aLeft[K]]
59252** aContent[aRight[J]] < aContent[aRight[K]]
59253**
59254** This routine overwrites aRight[] with a new (probably longer) sequence
59255** of indices such that the aRight[] contains every index that appears in
59256** either aLeft[] or the old aRight[] and such that the second condition
59257** above is still met.
59258**
59259** The aContent[aLeft[X]] values will be unique for all X. And the
59260** aContent[aRight[X]] values will be unique too. But there might be
59261** one or more combinations of X and Y such that
59262**
59263** aLeft[X]!=aRight[Y] && aContent[aLeft[X]] == aContent[aRight[Y]]
59264**
59265** When that happens, omit the aLeft[X] and use the aRight[Y] index.
59266*/
59267static void walMerge(
59268 const u32 *aContent, /* Pages in wal - keys for the sort */
59269 ht_slot *aLeft, /* IN: Left hand input list */
59270 int nLeft, /* IN: Elements in array *paLeft */
59271 ht_slot **paRight, /* IN/OUT: Right hand input list */
59272 int *pnRight, /* IN/OUT: Elements in *paRight */
59273 ht_slot *aTmp /* Temporary buffer */
59274){
59275 int iLeft = 0; /* Current index in aLeft */
59276 int iRight = 0; /* Current index in aRight */
59277 int iOut = 0; /* Current index in output buffer */
59278 int nRight = *pnRight;
59279 ht_slot *aRight = *paRight;
59280
59281 assert( nLeft>0 && nRight>0 );
59282 while( iRight<nRight || iLeft<nLeft ){
59283 ht_slot logpage;
59284 Pgno dbpage;
59285
59286 if( (iLeft<nLeft)
59287 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
59288 ){
59289 logpage = aLeft[iLeft++];
59290 }else{
59291 logpage = aRight[iRight++];
59292 }
59293 dbpage = aContent[logpage];
59294
59295 aTmp[iOut++] = logpage;
59296 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
59297
59298 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
59299 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
59300 }
59301
59302 *paRight = aLeft;
59303 *pnRight = iOut;
59304 memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
59305}
59306
59307/*
59308** Sort the elements in list aList using aContent[] as the sort key.
59309** Remove elements with duplicate keys, preferring to keep the
59310** larger aList[] values.
59311**
59312** The aList[] entries are indices into aContent[]. The values in
59313** aList[] are to be sorted so that for all J<K:
59314**
59315** aContent[aList[J]] < aContent[aList[K]]
59316**
59317** For any X and Y such that
59318**
59319** aContent[aList[X]] == aContent[aList[Y]]
59320**
59321** Keep the larger of the two values aList[X] and aList[Y] and discard
59322** the smaller.
59323*/
59324static void walMergesort(
59325 const u32 *aContent, /* Pages in wal */
59326 ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
59327 ht_slot *aList, /* IN/OUT: List to sort */
59328 int *pnList /* IN/OUT: Number of elements in aList[] */
59329){
59330 struct Sublist {
59331 int nList; /* Number of elements in aList */
59332 ht_slot *aList; /* Pointer to sub-list content */
59333 };
59334
59335 const int nList = *pnList; /* Size of input list */
59336 int nMerge = 0; /* Number of elements in list aMerge */
59337 ht_slot *aMerge = 0; /* List to be merged */
59338 int iList; /* Index into input list */
59339 u32 iSub = 0; /* Index into aSub array */
59340 struct Sublist aSub[13]; /* Array of sub-lists */
59341
59342 memset(aSub, 0, sizeof(aSub));
59343 assert( nList<=HASHTABLE_NPAGE && nList>0 );
59344 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
59345
59346 for(iList=0; iList<nList; iList++){
59347 nMerge = 1;
59348 aMerge = &aList[iList];
59349 for(iSub=0; iList & (1<<iSub); iSub++){
59350 struct Sublist *p;
59351 assert( iSub<ArraySize(aSub) );
59352 p = &aSub[iSub];
59353 assert( p->aList && p->nList<=(1<<iSub) );
59354 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
59355 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
59356 }
59357 aSub[iSub].aList = aMerge;
59358 aSub[iSub].nList = nMerge;
59359 }
59360
59361 for(iSub++; iSub<ArraySize(aSub); iSub++){
59362 if( nList & (1<<iSub) ){
59363 struct Sublist *p;
59364 assert( iSub<ArraySize(aSub) );
59365 p = &aSub[iSub];
59366 assert( p->nList<=(1<<iSub) );
59367 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
59368 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
59369 }
59370 }
59371 assert( aMerge==aList );
59372 *pnList = nMerge;
59373
59374#ifdef SQLITE_DEBUG
59375 {
59376 int i;
59377 for(i=1; i<*pnList; i++){
59378 assert( aContent[aList[i]] > aContent[aList[i-1]] );
59379 }
59380 }
59381#endif
59382}
59383
59384/*
59385** Free an iterator allocated by walIteratorInit().
59386*/
59387static void walIteratorFree(WalIterator *p){
59388 sqlite3_free(p);
59389}
59390
59391/*
59392** Construct a WalInterator object that can be used to loop over all
59393** pages in the WAL following frame nBackfill in ascending order. Frames
59394** nBackfill or earlier may be included - excluding them is an optimization
59395** only. The caller must hold the checkpoint lock.
59396**
59397** On success, make *pp point to the newly allocated WalInterator object
59398** return SQLITE_OK. Otherwise, return an error code. If this routine
59399** returns an error, the value of *pp is undefined.
59400**
59401** The calling routine should invoke walIteratorFree() to destroy the
59402** WalIterator object when it has finished with it.
59403*/
59404static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
59405 WalIterator *p; /* Return value */
59406 int nSegment; /* Number of segments to merge */
59407 u32 iLast; /* Last frame in log */
59408 int nByte; /* Number of bytes to allocate */
59409 int i; /* Iterator variable */
59410 ht_slot *aTmp; /* Temp space used by merge-sort */
59411 int rc = SQLITE_OK; /* Return Code */
59412
59413 /* This routine only runs while holding the checkpoint lock. And
59414 ** it only runs if there is actually content in the log (mxFrame>0).
59415 */
59416 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
59417 iLast = pWal->hdr.mxFrame;
59418
59419 /* Allocate space for the WalIterator object. */
59420 nSegment = walFramePage(iLast) + 1;
59421 nByte = sizeof(WalIterator)
59422 + (nSegment-1)*sizeof(struct WalSegment)
59423 + iLast*sizeof(ht_slot);
59424 p = (WalIterator *)sqlite3_malloc64(nByte);
59425 if( !p ){
59426 return SQLITE_NOMEM_BKPT;
59427 }
59428 memset(p, 0, nByte);
59429 p->nSegment = nSegment;
59430
59431 /* Allocate temporary space used by the merge-sort routine. This block
59432 ** of memory will be freed before this function returns.
59433 */
59434 aTmp = (ht_slot *)sqlite3_malloc64(
59435 sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
59436 );
59437 if( !aTmp ){
59438 rc = SQLITE_NOMEM_BKPT;
59439 }
59440
59441 for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
59442 WalHashLoc sLoc;
59443
59444 rc = walHashGet(pWal, i, &sLoc);
59445 if( rc==SQLITE_OK ){
59446 int j; /* Counter variable */
59447 int nEntry; /* Number of entries in this segment */
59448 ht_slot *aIndex; /* Sorted index for this segment */
59449
59450 sLoc.aPgno++;
59451 if( (i+1)==nSegment ){
59452 nEntry = (int)(iLast - sLoc.iZero);
59453 }else{
59454 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
59455 }
59456 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
59457 sLoc.iZero++;
59458
59459 for(j=0; j<nEntry; j++){
59460 aIndex[j] = (ht_slot)j;
59461 }
59462 walMergesort((u32 *)sLoc.aPgno, aTmp, aIndex, &nEntry);
59463 p->aSegment[i].iZero = sLoc.iZero;
59464 p->aSegment[i].nEntry = nEntry;
59465 p->aSegment[i].aIndex = aIndex;
59466 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
59467 }
59468 }
59469 sqlite3_free(aTmp);
59470
59471 if( rc!=SQLITE_OK ){
59472 walIteratorFree(p);
59473 p = 0;
59474 }
59475 *pp = p;
59476 return rc;
59477}
59478
59479/*
59480** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
59481** n. If the attempt fails and parameter xBusy is not NULL, then it is a
59482** busy-handler function. Invoke it and retry the lock until either the
59483** lock is successfully obtained or the busy-handler returns 0.
59484*/
59485static int walBusyLock(
59486 Wal *pWal, /* WAL connection */
59487 int (*xBusy)(void*), /* Function to call when busy */
59488 void *pBusyArg, /* Context argument for xBusyHandler */
59489 int lockIdx, /* Offset of first byte to lock */
59490 int n /* Number of bytes to lock */
59491){
59492 int rc;
59493 do {
59494 rc = walLockExclusive(pWal, lockIdx, n);
59495 }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
59496 return rc;
59497}
59498
59499/*
59500** The cache of the wal-index header must be valid to call this function.
59501** Return the page-size in bytes used by the database.
59502*/
59503static int walPagesize(Wal *pWal){
59504 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
59505}
59506
59507/*
59508** The following is guaranteed when this function is called:
59509**
59510** a) the WRITER lock is held,
59511** b) the entire log file has been checkpointed, and
59512** c) any existing readers are reading exclusively from the database
59513** file - there are no readers that may attempt to read a frame from
59514** the log file.
59515**
59516** This function updates the shared-memory structures so that the next
59517** client to write to the database (which may be this one) does so by
59518** writing frames into the start of the log file.
59519**
59520** The value of parameter salt1 is used as the aSalt[1] value in the
59521** new wal-index header. It should be passed a pseudo-random value (i.e.
59522** one obtained from sqlite3_randomness()).
59523*/
59524static void walRestartHdr(Wal *pWal, u32 salt1){
59525 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
59526 int i; /* Loop counter */
59527 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
59528 pWal->nCkpt++;
59529 pWal->hdr.mxFrame = 0;
59530 sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
59531 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
59532 walIndexWriteHdr(pWal);
59533 pInfo->nBackfill = 0;
59534 pInfo->nBackfillAttempted = 0;
59535 pInfo->aReadMark[1] = 0;
59536 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
59537 assert( pInfo->aReadMark[0]==0 );
59538}
59539
59540/*
59541** Copy as much content as we can from the WAL back into the database file
59542** in response to an sqlite3_wal_checkpoint() request or the equivalent.
59543**
59544** The amount of information copies from WAL to database might be limited
59545** by active readers. This routine will never overwrite a database page
59546** that a concurrent reader might be using.
59547**
59548** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
59549** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
59550** checkpoints are always run by a background thread or background
59551** process, foreground threads will never block on a lengthy fsync call.
59552**
59553** Fsync is called on the WAL before writing content out of the WAL and
59554** into the database. This ensures that if the new content is persistent
59555** in the WAL and can be recovered following a power-loss or hard reset.
59556**
59557** Fsync is also called on the database file if (and only if) the entire
59558** WAL content is copied into the database file. This second fsync makes
59559** it safe to delete the WAL since the new content will persist in the
59560** database file.
59561**
59562** This routine uses and updates the nBackfill field of the wal-index header.
59563** This is the only routine that will increase the value of nBackfill.
59564** (A WAL reset or recovery will revert nBackfill to zero, but not increase
59565** its value.)
59566**
59567** The caller must be holding sufficient locks to ensure that no other
59568** checkpoint is running (in any other thread or process) at the same
59569** time.
59570*/
59571static int walCheckpoint(
59572 Wal *pWal, /* Wal connection */
59573 sqlite3 *db, /* Check for interrupts on this handle */
59574 int eMode, /* One of PASSIVE, FULL or RESTART */
59575 int (*xBusy)(void*), /* Function to call when busy */
59576 void *pBusyArg, /* Context argument for xBusyHandler */
59577 int sync_flags, /* Flags for OsSync() (or 0) */
59578 u8 *zBuf /* Temporary buffer to use */
59579){
59580 int rc = SQLITE_OK; /* Return code */
59581 int szPage; /* Database page-size */
59582 WalIterator *pIter = 0; /* Wal iterator context */
59583 u32 iDbpage = 0; /* Next database page to write */
59584 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
59585 u32 mxSafeFrame; /* Max frame that can be backfilled */
59586 u32 mxPage; /* Max database page to write */
59587 int i; /* Loop counter */
59588 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
59589
59590 szPage = walPagesize(pWal);
59591 testcase( szPage<=32768 );
59592 testcase( szPage>=65536 );
59593 pInfo = walCkptInfo(pWal);
59594 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
59595
59596 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
59597 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
59598 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
59599
59600 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
59601 ** safe to write into the database. Frames beyond mxSafeFrame might
59602 ** overwrite database pages that are in use by active readers and thus
59603 ** cannot be backfilled from the WAL.
59604 */
59605 mxSafeFrame = pWal->hdr.mxFrame;
59606 mxPage = pWal->hdr.nPage;
59607 for(i=1; i<WAL_NREADER; i++){
59608 /* Thread-sanitizer reports that the following is an unsafe read,
59609 ** as some other thread may be in the process of updating the value
59610 ** of the aReadMark[] slot. The assumption here is that if that is
59611 ** happening, the other client may only be increasing the value,
59612 ** not decreasing it. So assuming either that either the "old" or
59613 ** "new" version of the value is read, and not some arbitrary value
59614 ** that would never be written by a real client, things are still
59615 ** safe. */
59616 u32 y = pInfo->aReadMark[i];
59617 if( mxSafeFrame>y ){
59618 assert( y<=pWal->hdr.mxFrame );
59619 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
59620 if( rc==SQLITE_OK ){
59621 pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
59622 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
59623 }else if( rc==SQLITE_BUSY ){
59624 mxSafeFrame = y;
59625 xBusy = 0;
59626 }else{
59627 goto walcheckpoint_out;
59628 }
59629 }
59630 }
59631
59632 /* Allocate the iterator */
59633 if( pInfo->nBackfill<mxSafeFrame ){
59634 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
59635 assert( rc==SQLITE_OK || pIter==0 );
59636 }
59637
59638 if( pIter
59639 && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK
59640 ){
59641 u32 nBackfill = pInfo->nBackfill;
59642
59643 pInfo->nBackfillAttempted = mxSafeFrame;
59644
59645 /* Sync the WAL to disk */
59646 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
59647
59648 /* If the database may grow as a result of this checkpoint, hint
59649 ** about the eventual size of the db file to the VFS layer.
59650 */
59651 if( rc==SQLITE_OK ){
59652 i64 nReq = ((i64)mxPage * szPage);
59653 i64 nSize; /* Current size of database file */
59654 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
59655 if( rc==SQLITE_OK && nSize<nReq ){
59656 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
59657 }
59658 }
59659
59660
59661 /* Iterate through the contents of the WAL, copying data to the db file */
59662 while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
59663 i64 iOffset;
59664 assert( walFramePgno(pWal, iFrame)==iDbpage );
59665 if( db->u1.isInterrupted ){
59666 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
59667 break;
59668 }
59669 if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
59670 continue;
59671 }
59672 iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
59673 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
59674 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
59675 if( rc!=SQLITE_OK ) break;
59676 iOffset = (iDbpage-1)*(i64)szPage;
59677 testcase( IS_BIG_INT(iOffset) );
59678 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
59679 if( rc!=SQLITE_OK ) break;
59680 }
59681
59682 /* If work was actually accomplished... */
59683 if( rc==SQLITE_OK ){
59684 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
59685 i64 szDb = pWal->hdr.nPage*(i64)szPage;
59686 testcase( IS_BIG_INT(szDb) );
59687 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
59688 if( rc==SQLITE_OK ){
59689 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
59690 }
59691 }
59692 if( rc==SQLITE_OK ){
59693 pInfo->nBackfill = mxSafeFrame;
59694 }
59695 }
59696
59697 /* Release the reader lock held while backfilling */
59698 walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
59699 }
59700
59701 if( rc==SQLITE_BUSY ){
59702 /* Reset the return code so as not to report a checkpoint failure
59703 ** just because there are active readers. */
59704 rc = SQLITE_OK;
59705 }
59706 }
59707
59708 /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
59709 ** entire wal file has been copied into the database file, then block
59710 ** until all readers have finished using the wal file. This ensures that
59711 ** the next process to write to the database restarts the wal file.
59712 */
59713 if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
59714 assert( pWal->writeLock );
59715 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
59716 rc = SQLITE_BUSY;
59717 }else if( eMode>=SQLITE_CHECKPOINT_RESTART ){
59718 u32 salt1;
59719 sqlite3_randomness(4, &salt1);
59720 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
59721 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
59722 if( rc==SQLITE_OK ){
59723 if( eMode==SQLITE_CHECKPOINT_TRUNCATE ){
59724 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
59725 ** SQLITE_CHECKPOINT_RESTART with the addition that it also
59726 ** truncates the log file to zero bytes just prior to a
59727 ** successful return.
59728 **
59729 ** In theory, it might be safe to do this without updating the
59730 ** wal-index header in shared memory, as all subsequent reader or
59731 ** writer clients should see that the entire log file has been
59732 ** checkpointed and behave accordingly. This seems unsafe though,
59733 ** as it would leave the system in a state where the contents of
59734 ** the wal-index header do not match the contents of the
59735 ** file-system. To avoid this, update the wal-index header to
59736 ** indicate that the log file contains zero valid frames. */
59737 walRestartHdr(pWal, salt1);
59738 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
59739 }
59740 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
59741 }
59742 }
59743 }
59744
59745 walcheckpoint_out:
59746 walIteratorFree(pIter);
59747 return rc;
59748}
59749
59750/*
59751** If the WAL file is currently larger than nMax bytes in size, truncate
59752** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
59753*/
59754static void walLimitSize(Wal *pWal, i64 nMax){
59755 i64 sz;
59756 int rx;
59757 sqlite3BeginBenignMalloc();
59758 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
59759 if( rx==SQLITE_OK && (sz > nMax ) ){
59760 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
59761 }
59762 sqlite3EndBenignMalloc();
59763 if( rx ){
59764 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
59765 }
59766}
59767
59768/*
59769** Close a connection to a log file.
59770*/
59771SQLITE_PRIVATE int sqlite3WalClose(
59772 Wal *pWal, /* Wal to close */
59773 sqlite3 *db, /* For interrupt flag */
59774 int sync_flags, /* Flags to pass to OsSync() (or 0) */
59775 int nBuf,
59776 u8 *zBuf /* Buffer of at least nBuf bytes */
59777){
59778 int rc = SQLITE_OK;
59779 if( pWal ){
59780 int isDelete = 0; /* True to unlink wal and wal-index files */
59781
59782 /* If an EXCLUSIVE lock can be obtained on the database file (using the
59783 ** ordinary, rollback-mode locking methods, this guarantees that the
59784 ** connection associated with this log file is the only connection to
59785 ** the database. In this case checkpoint the database and unlink both
59786 ** the wal and wal-index files.
59787 **
59788 ** The EXCLUSIVE lock is not released before returning.
59789 */
59790 if( zBuf!=0
59791 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
59792 ){
59793 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
59794 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
59795 }
59796 rc = sqlite3WalCheckpoint(pWal, db,
59797 SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
59798 );
59799 if( rc==SQLITE_OK ){
59800 int bPersist = -1;
59801 sqlite3OsFileControlHint(
59802 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
59803 );
59804 if( bPersist!=1 ){
59805 /* Try to delete the WAL file if the checkpoint completed and
59806 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
59807 ** mode (!bPersist) */
59808 isDelete = 1;
59809 }else if( pWal->mxWalSize>=0 ){
59810 /* Try to truncate the WAL file to zero bytes if the checkpoint
59811 ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
59812 ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
59813 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
59814 ** to zero bytes as truncating to the journal_size_limit might
59815 ** leave a corrupt WAL file on disk. */
59816 walLimitSize(pWal, 0);
59817 }
59818 }
59819 }
59820
59821 walIndexClose(pWal, isDelete);
59822 sqlite3OsClose(pWal->pWalFd);
59823 if( isDelete ){
59824 sqlite3BeginBenignMalloc();
59825 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
59826 sqlite3EndBenignMalloc();
59827 }
59828 WALTRACE(("WAL%p: closed\n", pWal));
59829 sqlite3_free((void *)pWal->apWiData);
59830 sqlite3_free(pWal);
59831 }
59832 return rc;
59833}
59834
59835/*
59836** Try to read the wal-index header. Return 0 on success and 1 if
59837** there is a problem.
59838**
59839** The wal-index is in shared memory. Another thread or process might
59840** be writing the header at the same time this procedure is trying to
59841** read it, which might result in inconsistency. A dirty read is detected
59842** by verifying that both copies of the header are the same and also by
59843** a checksum on the header.
59844**
59845** If and only if the read is consistent and the header is different from
59846** pWal->hdr, then pWal->hdr is updated to the content of the new header
59847** and *pChanged is set to 1.
59848**
59849** If the checksum cannot be verified return non-zero. If the header
59850** is read successfully and the checksum verified, return zero.
59851*/
59852static int walIndexTryHdr(Wal *pWal, int *pChanged){
59853 u32 aCksum[2]; /* Checksum on the header content */
59854 WalIndexHdr h1, h2; /* Two copies of the header content */
59855 WalIndexHdr volatile *aHdr; /* Header in shared memory */
59856
59857 /* The first page of the wal-index must be mapped at this point. */
59858 assert( pWal->nWiData>0 && pWal->apWiData[0] );
59859
59860 /* Read the header. This might happen concurrently with a write to the
59861 ** same area of shared memory on a different CPU in a SMP,
59862 ** meaning it is possible that an inconsistent snapshot is read
59863 ** from the file. If this happens, return non-zero.
59864 **
59865 ** There are two copies of the header at the beginning of the wal-index.
59866 ** When reading, read [0] first then [1]. Writes are in the reverse order.
59867 ** Memory barriers are used to prevent the compiler or the hardware from
59868 ** reordering the reads and writes.
59869 */
59870 aHdr = walIndexHdr(pWal);
59871 memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
59872 walShmBarrier(pWal);
59873 memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
59874
59875 if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
59876 return 1; /* Dirty read */
59877 }
59878 if( h1.isInit==0 ){
59879 return 1; /* Malformed header - probably all zeros */
59880 }
59881 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
59882 if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
59883 return 1; /* Checksum does not match */
59884 }
59885
59886 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
59887 *pChanged = 1;
59888 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
59889 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
59890 testcase( pWal->szPage<=32768 );
59891 testcase( pWal->szPage>=65536 );
59892 }
59893
59894 /* The header was successfully read. Return zero. */
59895 return 0;
59896}
59897
59898/*
59899** This is the value that walTryBeginRead returns when it needs to
59900** be retried.
59901*/
59902#define WAL_RETRY (-1)
59903
59904/*
59905** Read the wal-index header from the wal-index and into pWal->hdr.
59906** If the wal-header appears to be corrupt, try to reconstruct the
59907** wal-index from the WAL before returning.
59908**
59909** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
59910** changed by this operation. If pWal->hdr is unchanged, set *pChanged
59911** to 0.
59912**
59913** If the wal-index header is successfully read, return SQLITE_OK.
59914** Otherwise an SQLite error code.
59915*/
59916static int walIndexReadHdr(Wal *pWal, int *pChanged){
59917 int rc; /* Return code */
59918 int badHdr; /* True if a header read failed */
59919 volatile u32 *page0; /* Chunk of wal-index containing header */
59920
59921 /* Ensure that page 0 of the wal-index (the page that contains the
59922 ** wal-index header) is mapped. Return early if an error occurs here.
59923 */
59924 assert( pChanged );
59925 rc = walIndexPage(pWal, 0, &page0);
59926 if( rc!=SQLITE_OK ){
59927 assert( rc!=SQLITE_READONLY ); /* READONLY changed to OK in walIndexPage */
59928 if( rc==SQLITE_READONLY_CANTINIT ){
59929 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
59930 ** was openable but is not writable, and this thread is unable to
59931 ** confirm that another write-capable connection has the shared-memory
59932 ** open, and hence the content of the shared-memory is unreliable,
59933 ** since the shared-memory might be inconsistent with the WAL file
59934 ** and there is no writer on hand to fix it. */
59935 assert( page0==0 );
59936 assert( pWal->writeLock==0 );
59937 assert( pWal->readOnly & WAL_SHM_RDONLY );
59938 pWal->bShmUnreliable = 1;
59939 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
59940 *pChanged = 1;
59941 }else{
59942 return rc; /* Any other non-OK return is just an error */
59943 }
59944 }else{
59945 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
59946 ** is zero, which prevents the SHM from growing */
59947 testcase( page0!=0 );
59948 }
59949 assert( page0!=0 || pWal->writeLock==0 );
59950
59951 /* If the first page of the wal-index has been mapped, try to read the
59952 ** wal-index header immediately, without holding any lock. This usually
59953 ** works, but may fail if the wal-index header is corrupt or currently
59954 ** being modified by another thread or process.
59955 */
59956 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
59957
59958 /* If the first attempt failed, it might have been due to a race
59959 ** with a writer. So get a WRITE lock and try again.
59960 */
59961 assert( badHdr==0 || pWal->writeLock==0 );
59962 if( badHdr ){
59963 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
59964 if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
59965 walUnlockShared(pWal, WAL_WRITE_LOCK);
59966 rc = SQLITE_READONLY_RECOVERY;
59967 }
59968 }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
59969 pWal->writeLock = 1;
59970 if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
59971 badHdr = walIndexTryHdr(pWal, pChanged);
59972 if( badHdr ){
59973 /* If the wal-index header is still malformed even while holding
59974 ** a WRITE lock, it can only mean that the header is corrupted and
59975 ** needs to be reconstructed. So run recovery to do exactly that.
59976 */
59977 rc = walIndexRecover(pWal);
59978 *pChanged = 1;
59979 }
59980 }
59981 pWal->writeLock = 0;
59982 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
59983 }
59984 }
59985
59986 /* If the header is read successfully, check the version number to make
59987 ** sure the wal-index was not constructed with some future format that
59988 ** this version of SQLite cannot understand.
59989 */
59990 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
59991 rc = SQLITE_CANTOPEN_BKPT;
59992 }
59993 if( pWal->bShmUnreliable ){
59994 if( rc!=SQLITE_OK ){
59995 walIndexClose(pWal, 0);
59996 pWal->bShmUnreliable = 0;
59997 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
59998 /* walIndexRecover() might have returned SHORT_READ if a concurrent
59999 ** writer truncated the WAL out from under it. If that happens, it
60000 ** indicates that a writer has fixed the SHM file for us, so retry */
60001 if( rc==SQLITE_IOERR_SHORT_READ ) rc = WAL_RETRY;
60002 }
60003 pWal->exclusiveMode = WAL_NORMAL_MODE;
60004 }
60005
60006 return rc;
60007}
60008
60009/*
60010** Open a transaction in a connection where the shared-memory is read-only
60011** and where we cannot verify that there is a separate write-capable connection
60012** on hand to keep the shared-memory up-to-date with the WAL file.
60013**
60014** This can happen, for example, when the shared-memory is implemented by
60015** memory-mapping a *-shm file, where a prior writer has shut down and
60016** left the *-shm file on disk, and now the present connection is trying
60017** to use that database but lacks write permission on the *-shm file.
60018** Other scenarios are also possible, depending on the VFS implementation.
60019**
60020** Precondition:
60021**
60022** The *-wal file has been read and an appropriate wal-index has been
60023** constructed in pWal->apWiData[] using heap memory instead of shared
60024** memory.
60025**
60026** If this function returns SQLITE_OK, then the read transaction has
60027** been successfully opened. In this case output variable (*pChanged)
60028** is set to true before returning if the caller should discard the
60029** contents of the page cache before proceeding. Or, if it returns
60030** WAL_RETRY, then the heap memory wal-index has been discarded and
60031** the caller should retry opening the read transaction from the
60032** beginning (including attempting to map the *-shm file).
60033**
60034** If an error occurs, an SQLite error code is returned.
60035*/
60036static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
60037 i64 szWal; /* Size of wal file on disk in bytes */
60038 i64 iOffset; /* Current offset when reading wal file */
60039 u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
60040 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
60041 int szFrame; /* Number of bytes in buffer aFrame[] */
60042 u8 *aData; /* Pointer to data part of aFrame buffer */
60043 volatile void *pDummy; /* Dummy argument for xShmMap */
60044 int rc; /* Return code */
60045 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
60046
60047 assert( pWal->bShmUnreliable );
60048 assert( pWal->readOnly & WAL_SHM_RDONLY );
60049 assert( pWal->nWiData>0 && pWal->apWiData[0] );
60050
60051 /* Take WAL_READ_LOCK(0). This has the effect of preventing any
60052 ** writers from running a checkpoint, but does not stop them
60053 ** from running recovery. */
60054 rc = walLockShared(pWal, WAL_READ_LOCK(0));
60055 if( rc!=SQLITE_OK ){
60056 if( rc==SQLITE_BUSY ) rc = WAL_RETRY;
60057 goto begin_unreliable_shm_out;
60058 }
60059 pWal->readLock = 0;
60060
60061 /* Check to see if a separate writer has attached to the shared-memory area,
60062 ** thus making the shared-memory "reliable" again. Do this by invoking
60063 ** the xShmMap() routine of the VFS and looking to see if the return
60064 ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
60065 **
60066 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
60067 ** cause the heap-memory WAL-index to be discarded and the actual
60068 ** shared memory to be used in its place.
60069 **
60070 ** This step is important because, even though this connection is holding
60071 ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
60072 ** have already checkpointed the WAL file and, while the current
60073 ** is active, wrap the WAL and start overwriting frames that this
60074 ** process wants to use.
60075 **
60076 ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
60077 ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
60078 ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
60079 ** even if some external agent does a "chmod" to make the shared-memory
60080 ** writable by us, until sqlite3OsShmUnmap() has been called.
60081 ** This is a requirement on the VFS implementation.
60082 */
60083 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
60084 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
60085 if( rc!=SQLITE_READONLY_CANTINIT ){
60086 rc = (rc==SQLITE_READONLY ? WAL_RETRY : rc);
60087 goto begin_unreliable_shm_out;
60088 }
60089
60090 /* We reach this point only if the real shared-memory is still unreliable.
60091 ** Assume the in-memory WAL-index substitute is correct and load it
60092 ** into pWal->hdr.
60093 */
60094 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
60095
60096 /* Make sure some writer hasn't come in and changed the WAL file out
60097 ** from under us, then disconnected, while we were not looking.
60098 */
60099 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
60100 if( rc!=SQLITE_OK ){
60101 goto begin_unreliable_shm_out;
60102 }
60103 if( szWal<WAL_HDRSIZE ){
60104 /* If the wal file is too small to contain a wal-header and the
60105 ** wal-index header has mxFrame==0, then it must be safe to proceed
60106 ** reading the database file only. However, the page cache cannot
60107 ** be trusted, as a read/write connection may have connected, written
60108 ** the db, run a checkpoint, truncated the wal file and disconnected
60109 ** since this client's last read transaction. */
60110 *pChanged = 1;
60111 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
60112 goto begin_unreliable_shm_out;
60113 }
60114
60115 /* Check the salt keys at the start of the wal file still match. */
60116 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
60117 if( rc!=SQLITE_OK ){
60118 goto begin_unreliable_shm_out;
60119 }
60120 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
60121 /* Some writer has wrapped the WAL file while we were not looking.
60122 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
60123 ** rebuilt. */
60124 rc = WAL_RETRY;
60125 goto begin_unreliable_shm_out;
60126 }
60127
60128 /* Allocate a buffer to read frames into */
60129 szFrame = pWal->hdr.szPage + WAL_FRAME_HDRSIZE;
60130 aFrame = (u8 *)sqlite3_malloc64(szFrame);
60131 if( aFrame==0 ){
60132 rc = SQLITE_NOMEM_BKPT;
60133 goto begin_unreliable_shm_out;
60134 }
60135 aData = &aFrame[WAL_FRAME_HDRSIZE];
60136
60137 /* Check to see if a complete transaction has been appended to the
60138 ** wal file since the heap-memory wal-index was created. If so, the
60139 ** heap-memory wal-index is discarded and WAL_RETRY returned to
60140 ** the caller. */
60141 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
60142 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
60143 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->hdr.szPage);
60144 iOffset+szFrame<=szWal;
60145 iOffset+=szFrame
60146 ){
60147 u32 pgno; /* Database page number for frame */
60148 u32 nTruncate; /* dbsize field from frame header */
60149
60150 /* Read and decode the next log frame. */
60151 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
60152 if( rc!=SQLITE_OK ) break;
60153 if( !walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame) ) break;
60154
60155 /* If nTruncate is non-zero, then a complete transaction has been
60156 ** appended to this wal file. Set rc to WAL_RETRY and break out of
60157 ** the loop. */
60158 if( nTruncate ){
60159 rc = WAL_RETRY;
60160 break;
60161 }
60162 }
60163 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
60164 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
60165
60166 begin_unreliable_shm_out:
60167 sqlite3_free(aFrame);
60168 if( rc!=SQLITE_OK ){
60169 int i;
60170 for(i=0; i<pWal->nWiData; i++){
60171 sqlite3_free((void*)pWal->apWiData[i]);
60172 pWal->apWiData[i] = 0;
60173 }
60174 pWal->bShmUnreliable = 0;
60175 sqlite3WalEndReadTransaction(pWal);
60176 *pChanged = 1;
60177 }
60178 return rc;
60179}
60180
60181/*
60182** Attempt to start a read transaction. This might fail due to a race or
60183** other transient condition. When that happens, it returns WAL_RETRY to
60184** indicate to the caller that it is safe to retry immediately.
60185**
60186** On success return SQLITE_OK. On a permanent failure (such an
60187** I/O error or an SQLITE_BUSY because another process is running
60188** recovery) return a positive error code.
60189**
60190** The useWal parameter is true to force the use of the WAL and disable
60191** the case where the WAL is bypassed because it has been completely
60192** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
60193** to make a copy of the wal-index header into pWal->hdr. If the
60194** wal-index header has changed, *pChanged is set to 1 (as an indication
60195** to the caller that the local page cache is obsolete and needs to be
60196** flushed.) When useWal==1, the wal-index header is assumed to already
60197** be loaded and the pChanged parameter is unused.
60198**
60199** The caller must set the cnt parameter to the number of prior calls to
60200** this routine during the current read attempt that returned WAL_RETRY.
60201** This routine will start taking more aggressive measures to clear the
60202** race conditions after multiple WAL_RETRY returns, and after an excessive
60203** number of errors will ultimately return SQLITE_PROTOCOL. The
60204** SQLITE_PROTOCOL return indicates that some other process has gone rogue
60205** and is not honoring the locking protocol. There is a vanishingly small
60206** chance that SQLITE_PROTOCOL could be returned because of a run of really
60207** bad luck when there is lots of contention for the wal-index, but that
60208** possibility is so small that it can be safely neglected, we believe.
60209**
60210** On success, this routine obtains a read lock on
60211** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
60212** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
60213** that means the Wal does not hold any read lock. The reader must not
60214** access any database page that is modified by a WAL frame up to and
60215** including frame number aReadMark[pWal->readLock]. The reader will
60216** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
60217** Or if pWal->readLock==0, then the reader will ignore the WAL
60218** completely and get all content directly from the database file.
60219** If the useWal parameter is 1 then the WAL will never be ignored and
60220** this routine will always set pWal->readLock>0 on success.
60221** When the read transaction is completed, the caller must release the
60222** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
60223**
60224** This routine uses the nBackfill and aReadMark[] fields of the header
60225** to select a particular WAL_READ_LOCK() that strives to let the
60226** checkpoint process do as much work as possible. This routine might
60227** update values of the aReadMark[] array in the header, but if it does
60228** so it takes care to hold an exclusive lock on the corresponding
60229** WAL_READ_LOCK() while changing values.
60230*/
60231static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
60232 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
60233 u32 mxReadMark; /* Largest aReadMark[] value */
60234 int mxI; /* Index of largest aReadMark[] value */
60235 int i; /* Loop counter */
60236 int rc = SQLITE_OK; /* Return code */
60237 u32 mxFrame; /* Wal frame to lock to */
60238
60239 assert( pWal->readLock<0 ); /* Not currently locked */
60240
60241 /* useWal may only be set for read/write connections */
60242 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
60243
60244 /* Take steps to avoid spinning forever if there is a protocol error.
60245 **
60246 ** Circumstances that cause a RETRY should only last for the briefest
60247 ** instances of time. No I/O or other system calls are done while the
60248 ** locks are held, so the locks should not be held for very long. But
60249 ** if we are unlucky, another process that is holding a lock might get
60250 ** paged out or take a page-fault that is time-consuming to resolve,
60251 ** during the few nanoseconds that it is holding the lock. In that case,
60252 ** it might take longer than normal for the lock to free.
60253 **
60254 ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
60255 ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
60256 ** is more of a scheduler yield than an actual delay. But on the 10th
60257 ** an subsequent retries, the delays start becoming longer and longer,
60258 ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
60259 ** The total delay time before giving up is less than 10 seconds.
60260 */
60261 if( cnt>5 ){
60262 int nDelay = 1; /* Pause time in microseconds */
60263 if( cnt>100 ){
60264 VVA_ONLY( pWal->lockError = 1; )
60265 return SQLITE_PROTOCOL;
60266 }
60267 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
60268 sqlite3OsSleep(pWal->pVfs, nDelay);
60269 }
60270
60271 if( !useWal ){
60272 assert( rc==SQLITE_OK );
60273 if( pWal->bShmUnreliable==0 ){
60274 rc = walIndexReadHdr(pWal, pChanged);
60275 }
60276 if( rc==SQLITE_BUSY ){
60277 /* If there is not a recovery running in another thread or process
60278 ** then convert BUSY errors to WAL_RETRY. If recovery is known to
60279 ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
60280 ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
60281 ** would be technically correct. But the race is benign since with
60282 ** WAL_RETRY this routine will be called again and will probably be
60283 ** right on the second iteration.
60284 */
60285 if( pWal->apWiData[0]==0 ){
60286 /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
60287 ** We assume this is a transient condition, so return WAL_RETRY. The
60288 ** xShmMap() implementation used by the default unix and win32 VFS
60289 ** modules may return SQLITE_BUSY due to a race condition in the
60290 ** code that determines whether or not the shared-memory region
60291 ** must be zeroed before the requested page is returned.
60292 */
60293 rc = WAL_RETRY;
60294 }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
60295 walUnlockShared(pWal, WAL_RECOVER_LOCK);
60296 rc = WAL_RETRY;
60297 }else if( rc==SQLITE_BUSY ){
60298 rc = SQLITE_BUSY_RECOVERY;
60299 }
60300 }
60301 if( rc!=SQLITE_OK ){
60302 return rc;
60303 }
60304 else if( pWal->bShmUnreliable ){
60305 return walBeginShmUnreliable(pWal, pChanged);
60306 }
60307 }
60308
60309 assert( pWal->nWiData>0 );
60310 assert( pWal->apWiData[0]!=0 );
60311 pInfo = walCkptInfo(pWal);
60312 if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame
60313#ifdef SQLITE_ENABLE_SNAPSHOT
60314 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
60315#endif
60316 ){
60317 /* The WAL has been completely backfilled (or it is empty).
60318 ** and can be safely ignored.
60319 */
60320 rc = walLockShared(pWal, WAL_READ_LOCK(0));
60321 walShmBarrier(pWal);
60322 if( rc==SQLITE_OK ){
60323 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
60324 /* It is not safe to allow the reader to continue here if frames
60325 ** may have been appended to the log before READ_LOCK(0) was obtained.
60326 ** When holding READ_LOCK(0), the reader ignores the entire log file,
60327 ** which implies that the database file contains a trustworthy
60328 ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
60329 ** happening, this is usually correct.
60330 **
60331 ** However, if frames have been appended to the log (or if the log
60332 ** is wrapped and written for that matter) before the READ_LOCK(0)
60333 ** is obtained, that is not necessarily true. A checkpointer may
60334 ** have started to backfill the appended frames but crashed before
60335 ** it finished. Leaving a corrupt image in the database file.
60336 */
60337 walUnlockShared(pWal, WAL_READ_LOCK(0));
60338 return WAL_RETRY;
60339 }
60340 pWal->readLock = 0;
60341 return SQLITE_OK;
60342 }else if( rc!=SQLITE_BUSY ){
60343 return rc;
60344 }
60345 }
60346
60347 /* If we get this far, it means that the reader will want to use
60348 ** the WAL to get at content from recent commits. The job now is
60349 ** to select one of the aReadMark[] entries that is closest to
60350 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
60351 */
60352 mxReadMark = 0;
60353 mxI = 0;
60354 mxFrame = pWal->hdr.mxFrame;
60355#ifdef SQLITE_ENABLE_SNAPSHOT
60356 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
60357 mxFrame = pWal->pSnapshot->mxFrame;
60358 }
60359#endif
60360 for(i=1; i<WAL_NREADER; i++){
60361 u32 thisMark = AtomicLoad(pInfo->aReadMark+i);
60362 if( mxReadMark<=thisMark && thisMark<=mxFrame ){
60363 assert( thisMark!=READMARK_NOT_USED );
60364 mxReadMark = thisMark;
60365 mxI = i;
60366 }
60367 }
60368 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
60369 && (mxReadMark<mxFrame || mxI==0)
60370 ){
60371 for(i=1; i<WAL_NREADER; i++){
60372 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
60373 if( rc==SQLITE_OK ){
60374 mxReadMark = AtomicStore(pInfo->aReadMark+i,mxFrame);
60375 mxI = i;
60376 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
60377 break;
60378 }else if( rc!=SQLITE_BUSY ){
60379 return rc;
60380 }
60381 }
60382 }
60383 if( mxI==0 ){
60384 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
60385 return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
60386 }
60387
60388 rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
60389 if( rc ){
60390 return rc==SQLITE_BUSY ? WAL_RETRY : rc;
60391 }
60392 /* Now that the read-lock has been obtained, check that neither the
60393 ** value in the aReadMark[] array or the contents of the wal-index
60394 ** header have changed.
60395 **
60396 ** It is necessary to check that the wal-index header did not change
60397 ** between the time it was read and when the shared-lock was obtained
60398 ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
60399 ** that the log file may have been wrapped by a writer, or that frames
60400 ** that occur later in the log than pWal->hdr.mxFrame may have been
60401 ** copied into the database by a checkpointer. If either of these things
60402 ** happened, then reading the database with the current value of
60403 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
60404 ** instead.
60405 **
60406 ** Before checking that the live wal-index header has not changed
60407 ** since it was read, set Wal.minFrame to the first frame in the wal
60408 ** file that has not yet been checkpointed. This client will not need
60409 ** to read any frames earlier than minFrame from the wal file - they
60410 ** can be safely read directly from the database file.
60411 **
60412 ** Because a ShmBarrier() call is made between taking the copy of
60413 ** nBackfill and checking that the wal-header in shared-memory still
60414 ** matches the one cached in pWal->hdr, it is guaranteed that the
60415 ** checkpointer that set nBackfill was not working with a wal-index
60416 ** header newer than that cached in pWal->hdr. If it were, that could
60417 ** cause a problem. The checkpointer could omit to checkpoint
60418 ** a version of page X that lies before pWal->minFrame (call that version
60419 ** A) on the basis that there is a newer version (version B) of the same
60420 ** page later in the wal file. But if version B happens to like past
60421 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
60422 ** that it can read version A from the database file. However, since
60423 ** we can guarantee that the checkpointer that set nBackfill could not
60424 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
60425 */
60426 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1;
60427 walShmBarrier(pWal);
60428 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
60429 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
60430 ){
60431 walUnlockShared(pWal, WAL_READ_LOCK(mxI));
60432 return WAL_RETRY;
60433 }else{
60434 assert( mxReadMark<=pWal->hdr.mxFrame );
60435 pWal->readLock = (i16)mxI;
60436 }
60437 return rc;
60438}
60439
60440#ifdef SQLITE_ENABLE_SNAPSHOT
60441/*
60442** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted
60443** variable so that older snapshots can be accessed. To do this, loop
60444** through all wal frames from nBackfillAttempted to (nBackfill+1),
60445** comparing their content to the corresponding page with the database
60446** file, if any. Set nBackfillAttempted to the frame number of the
60447** first frame for which the wal file content matches the db file.
60448**
60449** This is only really safe if the file-system is such that any page
60450** writes made by earlier checkpointers were atomic operations, which
60451** is not always true. It is also possible that nBackfillAttempted
60452** may be left set to a value larger than expected, if a wal frame
60453** contains content that duplicate of an earlier version of the same
60454** page.
60455**
60456** SQLITE_OK is returned if successful, or an SQLite error code if an
60457** error occurs. It is not an error if nBackfillAttempted cannot be
60458** decreased at all.
60459*/
60460SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal){
60461 int rc;
60462
60463 assert( pWal->readLock>=0 );
60464 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
60465 if( rc==SQLITE_OK ){
60466 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
60467 int szPage = (int)pWal->szPage;
60468 i64 szDb; /* Size of db file in bytes */
60469
60470 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
60471 if( rc==SQLITE_OK ){
60472 void *pBuf1 = sqlite3_malloc(szPage);
60473 void *pBuf2 = sqlite3_malloc(szPage);
60474 if( pBuf1==0 || pBuf2==0 ){
60475 rc = SQLITE_NOMEM;
60476 }else{
60477 u32 i = pInfo->nBackfillAttempted;
60478 for(i=pInfo->nBackfillAttempted; i>pInfo->nBackfill; i--){
60479 WalHashLoc sLoc; /* Hash table location */
60480 u32 pgno; /* Page number in db file */
60481 i64 iDbOff; /* Offset of db file entry */
60482 i64 iWalOff; /* Offset of wal file entry */
60483
60484 rc = walHashGet(pWal, walFramePage(i), &sLoc);
60485 if( rc!=SQLITE_OK ) break;
60486 pgno = sLoc.aPgno[i-sLoc.iZero];
60487 iDbOff = (i64)(pgno-1) * szPage;
60488
60489 if( iDbOff+szPage<=szDb ){
60490 iWalOff = walFrameOffset(i, szPage) + WAL_FRAME_HDRSIZE;
60491 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
60492
60493 if( rc==SQLITE_OK ){
60494 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
60495 }
60496
60497 if( rc!=SQLITE_OK || 0==memcmp(pBuf1, pBuf2, szPage) ){
60498 break;
60499 }
60500 }
60501
60502 pInfo->nBackfillAttempted = i-1;
60503 }
60504 }
60505
60506 sqlite3_free(pBuf1);
60507 sqlite3_free(pBuf2);
60508 }
60509 walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
60510 }
60511
60512 return rc;
60513}
60514#endif /* SQLITE_ENABLE_SNAPSHOT */
60515
60516/*
60517** Begin a read transaction on the database.
60518**
60519** This routine used to be called sqlite3OpenSnapshot() and with good reason:
60520** it takes a snapshot of the state of the WAL and wal-index for the current
60521** instant in time. The current thread will continue to use this snapshot.
60522** Other threads might append new content to the WAL and wal-index but
60523** that extra content is ignored by the current thread.
60524**
60525** If the database contents have changes since the previous read
60526** transaction, then *pChanged is set to 1 before returning. The
60527** Pager layer will use this to know that its cache is stale and
60528** needs to be flushed.
60529*/
60530SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
60531 int rc; /* Return code */
60532 int cnt = 0; /* Number of TryBeginRead attempts */
60533
60534#ifdef SQLITE_ENABLE_SNAPSHOT
60535 int bChanged = 0;
60536 WalIndexHdr *pSnapshot = pWal->pSnapshot;
60537 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
60538 bChanged = 1;
60539 }
60540#endif
60541
60542 do{
60543 rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
60544 }while( rc==WAL_RETRY );
60545 testcase( (rc&0xff)==SQLITE_BUSY );
60546 testcase( (rc&0xff)==SQLITE_IOERR );
60547 testcase( rc==SQLITE_PROTOCOL );
60548 testcase( rc==SQLITE_OK );
60549
60550#ifdef SQLITE_ENABLE_SNAPSHOT
60551 if( rc==SQLITE_OK ){
60552 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
60553 /* At this point the client has a lock on an aReadMark[] slot holding
60554 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
60555 ** is populated with the wal-index header corresponding to the head
60556 ** of the wal file. Verify that pSnapshot is still valid before
60557 ** continuing. Reasons why pSnapshot might no longer be valid:
60558 **
60559 ** (1) The WAL file has been reset since the snapshot was taken.
60560 ** In this case, the salt will have changed.
60561 **
60562 ** (2) A checkpoint as been attempted that wrote frames past
60563 ** pSnapshot->mxFrame into the database file. Note that the
60564 ** checkpoint need not have completed for this to cause problems.
60565 */
60566 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
60567
60568 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
60569 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
60570
60571 /* It is possible that there is a checkpointer thread running
60572 ** concurrent with this code. If this is the case, it may be that the
60573 ** checkpointer has already determined that it will checkpoint
60574 ** snapshot X, where X is later in the wal file than pSnapshot, but
60575 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
60576 ** its intent. To avoid the race condition this leads to, ensure that
60577 ** there is no checkpointer process by taking a shared CKPT lock
60578 ** before checking pInfo->nBackfillAttempted.
60579 **
60580 ** TODO: Does the aReadMark[] lock prevent a checkpointer from doing
60581 ** this already?
60582 */
60583 rc = walLockShared(pWal, WAL_CKPT_LOCK);
60584
60585 if( rc==SQLITE_OK ){
60586 /* Check that the wal file has not been wrapped. Assuming that it has
60587 ** not, also check that no checkpointer has attempted to checkpoint any
60588 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
60589 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
60590 ** with *pSnapshot and set *pChanged as appropriate for opening the
60591 ** snapshot. */
60592 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
60593 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
60594 ){
60595 assert( pWal->readLock>0 );
60596 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
60597 *pChanged = bChanged;
60598 }else{
60599 rc = SQLITE_ERROR_SNAPSHOT;
60600 }
60601
60602 /* Release the shared CKPT lock obtained above. */
60603 walUnlockShared(pWal, WAL_CKPT_LOCK);
60604 pWal->minFrame = 1;
60605 }
60606
60607
60608 if( rc!=SQLITE_OK ){
60609 sqlite3WalEndReadTransaction(pWal);
60610 }
60611 }
60612 }
60613#endif
60614 return rc;
60615}
60616
60617/*
60618** Finish with a read transaction. All this does is release the
60619** read-lock.
60620*/
60621SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
60622 sqlite3WalEndWriteTransaction(pWal);
60623 if( pWal->readLock>=0 ){
60624 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
60625 pWal->readLock = -1;
60626 }
60627}
60628
60629/*
60630** Search the wal file for page pgno. If found, set *piRead to the frame that
60631** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
60632** to zero.
60633**
60634** Return SQLITE_OK if successful, or an error code if an error occurs. If an
60635** error does occur, the final value of *piRead is undefined.
60636*/
60637SQLITE_PRIVATE int sqlite3WalFindFrame(
60638 Wal *pWal, /* WAL handle */
60639 Pgno pgno, /* Database page number to read data for */
60640 u32 *piRead /* OUT: Frame number (or zero) */
60641){
60642 u32 iRead = 0; /* If !=0, WAL frame to return data from */
60643 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
60644 int iHash; /* Used to loop through N hash tables */
60645 int iMinHash;
60646
60647 /* This routine is only be called from within a read transaction. */
60648 assert( pWal->readLock>=0 || pWal->lockError );
60649
60650 /* If the "last page" field of the wal-index header snapshot is 0, then
60651 ** no data will be read from the wal under any circumstances. Return early
60652 ** in this case as an optimization. Likewise, if pWal->readLock==0,
60653 ** then the WAL is ignored by the reader so return early, as if the
60654 ** WAL were empty.
60655 */
60656 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
60657 *piRead = 0;
60658 return SQLITE_OK;
60659 }
60660
60661 /* Search the hash table or tables for an entry matching page number
60662 ** pgno. Each iteration of the following for() loop searches one
60663 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
60664 **
60665 ** This code might run concurrently to the code in walIndexAppend()
60666 ** that adds entries to the wal-index (and possibly to this hash
60667 ** table). This means the value just read from the hash
60668 ** slot (aHash[iKey]) may have been added before or after the
60669 ** current read transaction was opened. Values added after the
60670 ** read transaction was opened may have been written incorrectly -
60671 ** i.e. these slots may contain garbage data. However, we assume
60672 ** that any slots written before the current read transaction was
60673 ** opened remain unmodified.
60674 **
60675 ** For the reasons above, the if(...) condition featured in the inner
60676 ** loop of the following block is more stringent that would be required
60677 ** if we had exclusive access to the hash-table:
60678 **
60679 ** (aPgno[iFrame]==pgno):
60680 ** This condition filters out normal hash-table collisions.
60681 **
60682 ** (iFrame<=iLast):
60683 ** This condition filters out entries that were added to the hash
60684 ** table after the current read-transaction had started.
60685 */
60686 iMinHash = walFramePage(pWal->minFrame);
60687 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
60688 WalHashLoc sLoc; /* Hash table location */
60689 int iKey; /* Hash slot index */
60690 int nCollide; /* Number of hash collisions remaining */
60691 int rc; /* Error code */
60692
60693 rc = walHashGet(pWal, iHash, &sLoc);
60694 if( rc!=SQLITE_OK ){
60695 return rc;
60696 }
60697 nCollide = HASHTABLE_NSLOT;
60698 for(iKey=walHash(pgno); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
60699 u32 iFrame = sLoc.aHash[iKey] + sLoc.iZero;
60700 if( iFrame<=iLast && iFrame>=pWal->minFrame
60701 && sLoc.aPgno[sLoc.aHash[iKey]]==pgno ){
60702 assert( iFrame>iRead || CORRUPT_DB );
60703 iRead = iFrame;
60704 }
60705 if( (nCollide--)==0 ){
60706 return SQLITE_CORRUPT_BKPT;
60707 }
60708 }
60709 if( iRead ) break;
60710 }
60711
60712#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
60713 /* If expensive assert() statements are available, do a linear search
60714 ** of the wal-index file content. Make sure the results agree with the
60715 ** result obtained using the hash indexes above. */
60716 {
60717 u32 iRead2 = 0;
60718 u32 iTest;
60719 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
60720 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
60721 if( walFramePgno(pWal, iTest)==pgno ){
60722 iRead2 = iTest;
60723 break;
60724 }
60725 }
60726 assert( iRead==iRead2 );
60727 }
60728#endif
60729
60730 *piRead = iRead;
60731 return SQLITE_OK;
60732}
60733
60734/*
60735** Read the contents of frame iRead from the wal file into buffer pOut
60736** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
60737** error code otherwise.
60738*/
60739SQLITE_PRIVATE int sqlite3WalReadFrame(
60740 Wal *pWal, /* WAL handle */
60741 u32 iRead, /* Frame to read */
60742 int nOut, /* Size of buffer pOut in bytes */
60743 u8 *pOut /* Buffer to write page data to */
60744){
60745 int sz;
60746 i64 iOffset;
60747 sz = pWal->hdr.szPage;
60748 sz = (sz&0xfe00) + ((sz&0x0001)<<16);
60749 testcase( sz<=32768 );
60750 testcase( sz>=65536 );
60751 iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
60752 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
60753 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
60754}
60755
60756/*
60757** Return the size of the database in pages (or zero, if unknown).
60758*/
60759SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
60760 if( pWal && ALWAYS(pWal->readLock>=0) ){
60761 return pWal->hdr.nPage;
60762 }
60763 return 0;
60764}
60765
60766
60767/*
60768** This function starts a write transaction on the WAL.
60769**
60770** A read transaction must have already been started by a prior call
60771** to sqlite3WalBeginReadTransaction().
60772**
60773** If another thread or process has written into the database since
60774** the read transaction was started, then it is not possible for this
60775** thread to write as doing so would cause a fork. So this routine
60776** returns SQLITE_BUSY in that case and no write transaction is started.
60777**
60778** There can only be a single writer active at a time.
60779*/
60780SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
60781 int rc;
60782
60783 /* Cannot start a write transaction without first holding a read
60784 ** transaction. */
60785 assert( pWal->readLock>=0 );
60786 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
60787
60788 if( pWal->readOnly ){
60789 return SQLITE_READONLY;
60790 }
60791
60792 /* Only one writer allowed at a time. Get the write lock. Return
60793 ** SQLITE_BUSY if unable.
60794 */
60795 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
60796 if( rc ){
60797 return rc;
60798 }
60799 pWal->writeLock = 1;
60800
60801 /* If another connection has written to the database file since the
60802 ** time the read transaction on this connection was started, then
60803 ** the write is disallowed.
60804 */
60805 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
60806 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
60807 pWal->writeLock = 0;
60808 rc = SQLITE_BUSY_SNAPSHOT;
60809 }
60810
60811 return rc;
60812}
60813
60814/*
60815** End a write transaction. The commit has already been done. This
60816** routine merely releases the lock.
60817*/
60818SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
60819 if( pWal->writeLock ){
60820 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
60821 pWal->writeLock = 0;
60822 pWal->iReCksum = 0;
60823 pWal->truncateOnCommit = 0;
60824 }
60825 return SQLITE_OK;
60826}
60827
60828/*
60829** If any data has been written (but not committed) to the log file, this
60830** function moves the write-pointer back to the start of the transaction.
60831**
60832** Additionally, the callback function is invoked for each frame written
60833** to the WAL since the start of the transaction. If the callback returns
60834** other than SQLITE_OK, it is not invoked again and the error code is
60835** returned to the caller.
60836**
60837** Otherwise, if the callback function does not return an error, this
60838** function returns SQLITE_OK.
60839*/
60840SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
60841 int rc = SQLITE_OK;
60842 if( ALWAYS(pWal->writeLock) ){
60843 Pgno iMax = pWal->hdr.mxFrame;
60844 Pgno iFrame;
60845
60846 /* Restore the clients cache of the wal-index header to the state it
60847 ** was in before the client began writing to the database.
60848 */
60849 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
60850
60851 for(iFrame=pWal->hdr.mxFrame+1;
60852 ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
60853 iFrame++
60854 ){
60855 /* This call cannot fail. Unless the page for which the page number
60856 ** is passed as the second argument is (a) in the cache and
60857 ** (b) has an outstanding reference, then xUndo is either a no-op
60858 ** (if (a) is false) or simply expels the page from the cache (if (b)
60859 ** is false).
60860 **
60861 ** If the upper layer is doing a rollback, it is guaranteed that there
60862 ** are no outstanding references to any page other than page 1. And
60863 ** page 1 is never written to the log until the transaction is
60864 ** committed. As a result, the call to xUndo may not fail.
60865 */
60866 assert( walFramePgno(pWal, iFrame)!=1 );
60867 rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
60868 }
60869 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
60870 }
60871 return rc;
60872}
60873
60874/*
60875** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
60876** values. This function populates the array with values required to
60877** "rollback" the write position of the WAL handle back to the current
60878** point in the event of a savepoint rollback (via WalSavepointUndo()).
60879*/
60880SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
60881 assert( pWal->writeLock );
60882 aWalData[0] = pWal->hdr.mxFrame;
60883 aWalData[1] = pWal->hdr.aFrameCksum[0];
60884 aWalData[2] = pWal->hdr.aFrameCksum[1];
60885 aWalData[3] = pWal->nCkpt;
60886}
60887
60888/*
60889** Move the write position of the WAL back to the point identified by
60890** the values in the aWalData[] array. aWalData must point to an array
60891** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
60892** by a call to WalSavepoint().
60893*/
60894SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
60895 int rc = SQLITE_OK;
60896
60897 assert( pWal->writeLock );
60898 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
60899
60900 if( aWalData[3]!=pWal->nCkpt ){
60901 /* This savepoint was opened immediately after the write-transaction
60902 ** was started. Right after that, the writer decided to wrap around
60903 ** to the start of the log. Update the savepoint values to match.
60904 */
60905 aWalData[0] = 0;
60906 aWalData[3] = pWal->nCkpt;
60907 }
60908
60909 if( aWalData[0]<pWal->hdr.mxFrame ){
60910 pWal->hdr.mxFrame = aWalData[0];
60911 pWal->hdr.aFrameCksum[0] = aWalData[1];
60912 pWal->hdr.aFrameCksum[1] = aWalData[2];
60913 walCleanupHash(pWal);
60914 }
60915
60916 return rc;
60917}
60918
60919/*
60920** This function is called just before writing a set of frames to the log
60921** file (see sqlite3WalFrames()). It checks to see if, instead of appending
60922** to the current log file, it is possible to overwrite the start of the
60923** existing log file with the new frames (i.e. "reset" the log). If so,
60924** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
60925** unchanged.
60926**
60927** SQLITE_OK is returned if no error is encountered (regardless of whether
60928** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
60929** if an error occurs.
60930*/
60931static int walRestartLog(Wal *pWal){
60932 int rc = SQLITE_OK;
60933 int cnt;
60934
60935 if( pWal->readLock==0 ){
60936 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
60937 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
60938 if( pInfo->nBackfill>0 ){
60939 u32 salt1;
60940 sqlite3_randomness(4, &salt1);
60941 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
60942 if( rc==SQLITE_OK ){
60943 /* If all readers are using WAL_READ_LOCK(0) (in other words if no
60944 ** readers are currently using the WAL), then the transactions
60945 ** frames will overwrite the start of the existing log. Update the
60946 ** wal-index header to reflect this.
60947 **
60948 ** In theory it would be Ok to update the cache of the header only
60949 ** at this point. But updating the actual wal-index header is also
60950 ** safe and means there is no special case for sqlite3WalUndo()
60951 ** to handle if this transaction is rolled back. */
60952 walRestartHdr(pWal, salt1);
60953 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
60954 }else if( rc!=SQLITE_BUSY ){
60955 return rc;
60956 }
60957 }
60958 walUnlockShared(pWal, WAL_READ_LOCK(0));
60959 pWal->readLock = -1;
60960 cnt = 0;
60961 do{
60962 int notUsed;
60963 rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
60964 }while( rc==WAL_RETRY );
60965 assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
60966 testcase( (rc&0xff)==SQLITE_IOERR );
60967 testcase( rc==SQLITE_PROTOCOL );
60968 testcase( rc==SQLITE_OK );
60969 }
60970 return rc;
60971}
60972
60973/*
60974** Information about the current state of the WAL file and where
60975** the next fsync should occur - passed from sqlite3WalFrames() into
60976** walWriteToLog().
60977*/
60978typedef struct WalWriter {
60979 Wal *pWal; /* The complete WAL information */
60980 sqlite3_file *pFd; /* The WAL file to which we write */
60981 sqlite3_int64 iSyncPoint; /* Fsync at this offset */
60982 int syncFlags; /* Flags for the fsync */
60983 int szPage; /* Size of one page */
60984} WalWriter;
60985
60986/*
60987** Write iAmt bytes of content into the WAL file beginning at iOffset.
60988** Do a sync when crossing the p->iSyncPoint boundary.
60989**
60990** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
60991** first write the part before iSyncPoint, then sync, then write the
60992** rest.
60993*/
60994static int walWriteToLog(
60995 WalWriter *p, /* WAL to write to */
60996 void *pContent, /* Content to be written */
60997 int iAmt, /* Number of bytes to write */
60998 sqlite3_int64 iOffset /* Start writing at this offset */
60999){
61000 int rc;
61001 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
61002 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
61003 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
61004 if( rc ) return rc;
61005 iOffset += iFirstAmt;
61006 iAmt -= iFirstAmt;
61007 pContent = (void*)(iFirstAmt + (char*)pContent);
61008 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
61009 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
61010 if( iAmt==0 || rc ) return rc;
61011 }
61012 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
61013 return rc;
61014}
61015
61016/*
61017** Write out a single frame of the WAL
61018*/
61019static int walWriteOneFrame(
61020 WalWriter *p, /* Where to write the frame */
61021 PgHdr *pPage, /* The page of the frame to be written */
61022 int nTruncate, /* The commit flag. Usually 0. >0 for commit */
61023 sqlite3_int64 iOffset /* Byte offset at which to write */
61024){
61025 int rc; /* Result code from subfunctions */
61026 void *pData; /* Data actually written */
61027 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
61028#if defined(SQLITE_HAS_CODEC)
61029 if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM_BKPT;
61030#else
61031 pData = pPage->pData;
61032#endif
61033 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
61034 rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
61035 if( rc ) return rc;
61036 /* Write the page data */
61037 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
61038 return rc;
61039}
61040
61041/*
61042** This function is called as part of committing a transaction within which
61043** one or more frames have been overwritten. It updates the checksums for
61044** all frames written to the wal file by the current transaction starting
61045** with the earliest to have been overwritten.
61046**
61047** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
61048*/
61049static int walRewriteChecksums(Wal *pWal, u32 iLast){
61050 const int szPage = pWal->szPage;/* Database page size */
61051 int rc = SQLITE_OK; /* Return code */
61052 u8 *aBuf; /* Buffer to load data from wal file into */
61053 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
61054 u32 iRead; /* Next frame to read from wal file */
61055 i64 iCksumOff;
61056
61057 aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE);
61058 if( aBuf==0 ) return SQLITE_NOMEM_BKPT;
61059
61060 /* Find the checksum values to use as input for the recalculating the
61061 ** first checksum. If the first frame is frame 1 (implying that the current
61062 ** transaction restarted the wal file), these values must be read from the
61063 ** wal-file header. Otherwise, read them from the frame header of the
61064 ** previous frame. */
61065 assert( pWal->iReCksum>0 );
61066 if( pWal->iReCksum==1 ){
61067 iCksumOff = 24;
61068 }else{
61069 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
61070 }
61071 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
61072 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
61073 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
61074
61075 iRead = pWal->iReCksum;
61076 pWal->iReCksum = 0;
61077 for(; rc==SQLITE_OK && iRead<=iLast; iRead++){
61078 i64 iOff = walFrameOffset(iRead, szPage);
61079 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
61080 if( rc==SQLITE_OK ){
61081 u32 iPgno, nDbSize;
61082 iPgno = sqlite3Get4byte(aBuf);
61083 nDbSize = sqlite3Get4byte(&aBuf[4]);
61084
61085 walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE], aFrame);
61086 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
61087 }
61088 }
61089
61090 sqlite3_free(aBuf);
61091 return rc;
61092}
61093
61094/*
61095** Write a set of frames to the log. The caller must hold the write-lock
61096** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
61097*/
61098SQLITE_PRIVATE int sqlite3WalFrames(
61099 Wal *pWal, /* Wal handle to write to */
61100 int szPage, /* Database page-size in bytes */
61101 PgHdr *pList, /* List of dirty pages to write */
61102 Pgno nTruncate, /* Database size after this commit */
61103 int isCommit, /* True if this is a commit */
61104 int sync_flags /* Flags to pass to OsSync() (or 0) */
61105){
61106 int rc; /* Used to catch return codes */
61107 u32 iFrame; /* Next frame address */
61108 PgHdr *p; /* Iterator to run through pList with. */
61109 PgHdr *pLast = 0; /* Last frame in list */
61110 int nExtra = 0; /* Number of extra copies of last page */
61111 int szFrame; /* The size of a single frame */
61112 i64 iOffset; /* Next byte to write in WAL file */
61113 WalWriter w; /* The writer */
61114 u32 iFirst = 0; /* First frame that may be overwritten */
61115 WalIndexHdr *pLive; /* Pointer to shared header */
61116
61117 assert( pList );
61118 assert( pWal->writeLock );
61119
61120 /* If this frame set completes a transaction, then nTruncate>0. If
61121 ** nTruncate==0 then this frame set does not complete the transaction. */
61122 assert( (isCommit!=0)==(nTruncate!=0) );
61123
61124#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
61125 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
61126 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
61127 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
61128 }
61129#endif
61130
61131 pLive = (WalIndexHdr*)walIndexHdr(pWal);
61132 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
61133 iFirst = pLive->mxFrame+1;
61134 }
61135
61136 /* See if it is possible to write these frames into the start of the
61137 ** log file, instead of appending to it at pWal->hdr.mxFrame.
61138 */
61139 if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
61140 return rc;
61141 }
61142
61143 /* If this is the first frame written into the log, write the WAL
61144 ** header to the start of the WAL file. See comments at the top of
61145 ** this source file for a description of the WAL header format.
61146 */
61147 iFrame = pWal->hdr.mxFrame;
61148 if( iFrame==0 ){
61149 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
61150 u32 aCksum[2]; /* Checksum for wal-header */
61151
61152 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
61153 sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
61154 sqlite3Put4byte(&aWalHdr[8], szPage);
61155 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
61156 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
61157 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
61158 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
61159 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
61160 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
61161
61162 pWal->szPage = szPage;
61163 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
61164 pWal->hdr.aFrameCksum[0] = aCksum[0];
61165 pWal->hdr.aFrameCksum[1] = aCksum[1];
61166 pWal->truncateOnCommit = 1;
61167
61168 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
61169 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
61170 if( rc!=SQLITE_OK ){
61171 return rc;
61172 }
61173
61174 /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
61175 ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise
61176 ** an out-of-order write following a WAL restart could result in
61177 ** database corruption. See the ticket:
61178 **
61179 ** https://sqlite.org/src/info/ff5be73dee
61180 */
61181 if( pWal->syncHeader ){
61182 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
61183 if( rc ) return rc;
61184 }
61185 }
61186 assert( (int)pWal->szPage==szPage );
61187
61188 /* Setup information needed to write frames into the WAL */
61189 w.pWal = pWal;
61190 w.pFd = pWal->pWalFd;
61191 w.iSyncPoint = 0;
61192 w.syncFlags = sync_flags;
61193 w.szPage = szPage;
61194 iOffset = walFrameOffset(iFrame+1, szPage);
61195 szFrame = szPage + WAL_FRAME_HDRSIZE;
61196
61197 /* Write all frames into the log file exactly once */
61198 for(p=pList; p; p=p->pDirty){
61199 int nDbSize; /* 0 normally. Positive == commit flag */
61200
61201 /* Check if this page has already been written into the wal file by
61202 ** the current transaction. If so, overwrite the existing frame and
61203 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
61204 ** checksums must be recomputed when the transaction is committed. */
61205 if( iFirst && (p->pDirty || isCommit==0) ){
61206 u32 iWrite = 0;
61207 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
61208 assert( rc==SQLITE_OK || iWrite==0 );
61209 if( iWrite>=iFirst ){
61210 i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE;
61211 void *pData;
61212 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
61213 pWal->iReCksum = iWrite;
61214 }
61215#if defined(SQLITE_HAS_CODEC)
61216 if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM;
61217#else
61218 pData = p->pData;
61219#endif
61220 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
61221 if( rc ) return rc;
61222 p->flags &= ~PGHDR_WAL_APPEND;
61223 continue;
61224 }
61225 }
61226
61227 iFrame++;
61228 assert( iOffset==walFrameOffset(iFrame, szPage) );
61229 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
61230 rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
61231 if( rc ) return rc;
61232 pLast = p;
61233 iOffset += szFrame;
61234 p->flags |= PGHDR_WAL_APPEND;
61235 }
61236
61237 /* Recalculate checksums within the wal file if required. */
61238 if( isCommit && pWal->iReCksum ){
61239 rc = walRewriteChecksums(pWal, iFrame);
61240 if( rc ) return rc;
61241 }
61242
61243 /* If this is the end of a transaction, then we might need to pad
61244 ** the transaction and/or sync the WAL file.
61245 **
61246 ** Padding and syncing only occur if this set of frames complete a
61247 ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL
61248 ** or synchronous==OFF, then no padding or syncing are needed.
61249 **
61250 ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
61251 ** needed and only the sync is done. If padding is needed, then the
61252 ** final frame is repeated (with its commit mark) until the next sector
61253 ** boundary is crossed. Only the part of the WAL prior to the last
61254 ** sector boundary is synced; the part of the last frame that extends
61255 ** past the sector boundary is written after the sync.
61256 */
61257 if( isCommit && WAL_SYNC_FLAGS(sync_flags)!=0 ){
61258 int bSync = 1;
61259 if( pWal->padToSectorBoundary ){
61260 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
61261 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
61262 bSync = (w.iSyncPoint==iOffset);
61263 testcase( bSync );
61264 while( iOffset<w.iSyncPoint ){
61265 rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
61266 if( rc ) return rc;
61267 iOffset += szFrame;
61268 nExtra++;
61269 }
61270 }
61271 if( bSync ){
61272 assert( rc==SQLITE_OK );
61273 rc = sqlite3OsSync(w.pFd, WAL_SYNC_FLAGS(sync_flags));
61274 }
61275 }
61276
61277 /* If this frame set completes the first transaction in the WAL and
61278 ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
61279 ** journal size limit, if possible.
61280 */
61281 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
61282 i64 sz = pWal->mxWalSize;
61283 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
61284 sz = walFrameOffset(iFrame+nExtra+1, szPage);
61285 }
61286 walLimitSize(pWal, sz);
61287 pWal->truncateOnCommit = 0;
61288 }
61289
61290 /* Append data to the wal-index. It is not necessary to lock the
61291 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
61292 ** guarantees that there are no other writers, and no data that may
61293 ** be in use by existing readers is being overwritten.
61294 */
61295 iFrame = pWal->hdr.mxFrame;
61296 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
61297 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
61298 iFrame++;
61299 rc = walIndexAppend(pWal, iFrame, p->pgno);
61300 }
61301 while( rc==SQLITE_OK && nExtra>0 ){
61302 iFrame++;
61303 nExtra--;
61304 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
61305 }
61306
61307 if( rc==SQLITE_OK ){
61308 /* Update the private copy of the header. */
61309 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
61310 testcase( szPage<=32768 );
61311 testcase( szPage>=65536 );
61312 pWal->hdr.mxFrame = iFrame;
61313 if( isCommit ){
61314 pWal->hdr.iChange++;
61315 pWal->hdr.nPage = nTruncate;
61316 }
61317 /* If this is a commit, update the wal-index header too. */
61318 if( isCommit ){
61319 walIndexWriteHdr(pWal);
61320 pWal->iCallback = iFrame;
61321 }
61322 }
61323
61324 WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
61325 return rc;
61326}
61327
61328/*
61329** This routine is called to implement sqlite3_wal_checkpoint() and
61330** related interfaces.
61331**
61332** Obtain a CHECKPOINT lock and then backfill as much information as
61333** we can from WAL into the database.
61334**
61335** If parameter xBusy is not NULL, it is a pointer to a busy-handler
61336** callback. In this case this function runs a blocking checkpoint.
61337*/
61338SQLITE_PRIVATE int sqlite3WalCheckpoint(
61339 Wal *pWal, /* Wal connection */
61340 sqlite3 *db, /* Check this handle's interrupt flag */
61341 int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */
61342 int (*xBusy)(void*), /* Function to call when busy */
61343 void *pBusyArg, /* Context argument for xBusyHandler */
61344 int sync_flags, /* Flags to sync db file with (or 0) */
61345 int nBuf, /* Size of temporary buffer */
61346 u8 *zBuf, /* Temporary buffer to use */
61347 int *pnLog, /* OUT: Number of frames in WAL */
61348 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
61349){
61350 int rc; /* Return code */
61351 int isChanged = 0; /* True if a new wal-index header is loaded */
61352 int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
61353 int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */
61354
61355 assert( pWal->ckptLock==0 );
61356 assert( pWal->writeLock==0 );
61357
61358 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
61359 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
61360 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
61361
61362 if( pWal->readOnly ) return SQLITE_READONLY;
61363 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
61364
61365 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
61366 ** "checkpoint" lock on the database file. */
61367 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
61368 if( rc ){
61369 /* EVIDENCE-OF: R-10421-19736 If any other process is running a
61370 ** checkpoint operation at the same time, the lock cannot be obtained and
61371 ** SQLITE_BUSY is returned.
61372 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
61373 ** it will not be invoked in this case.
61374 */
61375 testcase( rc==SQLITE_BUSY );
61376 testcase( xBusy!=0 );
61377 return rc;
61378 }
61379 pWal->ckptLock = 1;
61380
61381 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
61382 ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
61383 ** file.
61384 **
61385 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
61386 ** immediately, and a busy-handler is configured, it is invoked and the
61387 ** writer lock retried until either the busy-handler returns 0 or the
61388 ** lock is successfully obtained.
61389 */
61390 if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
61391 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
61392 if( rc==SQLITE_OK ){
61393 pWal->writeLock = 1;
61394 }else if( rc==SQLITE_BUSY ){
61395 eMode2 = SQLITE_CHECKPOINT_PASSIVE;
61396 xBusy2 = 0;
61397 rc = SQLITE_OK;
61398 }
61399 }
61400
61401 /* Read the wal-index header. */
61402 if( rc==SQLITE_OK ){
61403 rc = walIndexReadHdr(pWal, &isChanged);
61404 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
61405 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
61406 }
61407 }
61408
61409 /* Copy data from the log to the database file. */
61410 if( rc==SQLITE_OK ){
61411
61412 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
61413 rc = SQLITE_CORRUPT_BKPT;
61414 }else{
61415 rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf);
61416 }
61417
61418 /* If no error occurred, set the output variables. */
61419 if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
61420 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
61421 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
61422 }
61423 }
61424
61425 if( isChanged ){
61426 /* If a new wal-index header was loaded before the checkpoint was
61427 ** performed, then the pager-cache associated with pWal is now
61428 ** out of date. So zero the cached wal-index header to ensure that
61429 ** next time the pager opens a snapshot on this database it knows that
61430 ** the cache needs to be reset.
61431 */
61432 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
61433 }
61434
61435 /* Release the locks. */
61436 sqlite3WalEndWriteTransaction(pWal);
61437 walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
61438 pWal->ckptLock = 0;
61439 WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
61440 return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
61441}
61442
61443/* Return the value to pass to a sqlite3_wal_hook callback, the
61444** number of frames in the WAL at the point of the last commit since
61445** sqlite3WalCallback() was called. If no commits have occurred since
61446** the last call, then return 0.
61447*/
61448SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
61449 u32 ret = 0;
61450 if( pWal ){
61451 ret = pWal->iCallback;
61452 pWal->iCallback = 0;
61453 }
61454 return (int)ret;
61455}
61456
61457/*
61458** This function is called to change the WAL subsystem into or out
61459** of locking_mode=EXCLUSIVE.
61460**
61461** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
61462** into locking_mode=NORMAL. This means that we must acquire a lock
61463** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
61464** or if the acquisition of the lock fails, then return 0. If the
61465** transition out of exclusive-mode is successful, return 1. This
61466** operation must occur while the pager is still holding the exclusive
61467** lock on the main database file.
61468**
61469** If op is one, then change from locking_mode=NORMAL into
61470** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
61471** be released. Return 1 if the transition is made and 0 if the
61472** WAL is already in exclusive-locking mode - meaning that this
61473** routine is a no-op. The pager must already hold the exclusive lock
61474** on the main database file before invoking this operation.
61475**
61476** If op is negative, then do a dry-run of the op==1 case but do
61477** not actually change anything. The pager uses this to see if it
61478** should acquire the database exclusive lock prior to invoking
61479** the op==1 case.
61480*/
61481SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
61482 int rc;
61483 assert( pWal->writeLock==0 );
61484 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
61485
61486 /* pWal->readLock is usually set, but might be -1 if there was a
61487 ** prior error while attempting to acquire are read-lock. This cannot
61488 ** happen if the connection is actually in exclusive mode (as no xShmLock
61489 ** locks are taken in this case). Nor should the pager attempt to
61490 ** upgrade to exclusive-mode following such an error.
61491 */
61492 assert( pWal->readLock>=0 || pWal->lockError );
61493 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
61494
61495 if( op==0 ){
61496 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
61497 pWal->exclusiveMode = WAL_NORMAL_MODE;
61498 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
61499 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
61500 }
61501 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
61502 }else{
61503 /* Already in locking_mode=NORMAL */
61504 rc = 0;
61505 }
61506 }else if( op>0 ){
61507 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
61508 assert( pWal->readLock>=0 );
61509 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
61510 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
61511 rc = 1;
61512 }else{
61513 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
61514 }
61515 return rc;
61516}
61517
61518/*
61519** Return true if the argument is non-NULL and the WAL module is using
61520** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
61521** WAL module is using shared-memory, return false.
61522*/
61523SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
61524 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
61525}
61526
61527#ifdef SQLITE_ENABLE_SNAPSHOT
61528/* Create a snapshot object. The content of a snapshot is opaque to
61529** every other subsystem, so the WAL module can put whatever it needs
61530** in the object.
61531*/
61532SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
61533 int rc = SQLITE_OK;
61534 WalIndexHdr *pRet;
61535 static const u32 aZero[4] = { 0, 0, 0, 0 };
61536
61537 assert( pWal->readLock>=0 && pWal->writeLock==0 );
61538
61539 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
61540 *ppSnapshot = 0;
61541 return SQLITE_ERROR;
61542 }
61543 pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
61544 if( pRet==0 ){
61545 rc = SQLITE_NOMEM_BKPT;
61546 }else{
61547 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
61548 *ppSnapshot = (sqlite3_snapshot*)pRet;
61549 }
61550
61551 return rc;
61552}
61553
61554/* Try to open on pSnapshot when the next read-transaction starts
61555*/
61556SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){
61557 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
61558}
61559
61560/*
61561** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
61562** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
61563*/
61564SQLITE_API int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
61565 WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
61566 WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
61567
61568 /* aSalt[0] is a copy of the value stored in the wal file header. It
61569 ** is incremented each time the wal file is restarted. */
61570 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
61571 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
61572 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
61573 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
61574 return 0;
61575}
61576
61577/*
61578** The caller currently has a read transaction open on the database.
61579** This function takes a SHARED lock on the CHECKPOINTER slot and then
61580** checks if the snapshot passed as the second argument is still
61581** available. If so, SQLITE_OK is returned.
61582**
61583** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
61584** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
61585** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
61586** lock is released before returning.
61587*/
61588SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){
61589 int rc;
61590 rc = walLockShared(pWal, WAL_CKPT_LOCK);
61591 if( rc==SQLITE_OK ){
61592 WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot;
61593 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
61594 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
61595 ){
61596 rc = SQLITE_ERROR_SNAPSHOT;
61597 walUnlockShared(pWal, WAL_CKPT_LOCK);
61598 }
61599 }
61600 return rc;
61601}
61602
61603/*
61604** Release a lock obtained by an earlier successful call to
61605** sqlite3WalSnapshotCheck().
61606*/
61607SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal){
61608 assert( pWal );
61609 walUnlockShared(pWal, WAL_CKPT_LOCK);
61610}
61611
61612
61613#endif /* SQLITE_ENABLE_SNAPSHOT */
61614
61615#ifdef SQLITE_ENABLE_ZIPVFS
61616/*
61617** If the argument is not NULL, it points to a Wal object that holds a
61618** read-lock. This function returns the database page-size if it is known,
61619** or zero if it is not (or if pWal is NULL).
61620*/
61621SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
61622 assert( pWal==0 || pWal->readLock>=0 );
61623 return (pWal ? pWal->szPage : 0);
61624}
61625#endif
61626
61627/* Return the sqlite3_file object for the WAL file
61628*/
61629SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal){
61630 return pWal->pWalFd;
61631}
61632
61633#endif /* #ifndef SQLITE_OMIT_WAL */
61634
61635/************** End of wal.c *************************************************/
61636/************** Begin file btmutex.c *****************************************/
61637/*
61638** 2007 August 27
61639**
61640** The author disclaims copyright to this source code. In place of
61641** a legal notice, here is a blessing:
61642**
61643** May you do good and not evil.
61644** May you find forgiveness for yourself and forgive others.
61645** May you share freely, never taking more than you give.
61646**
61647*************************************************************************
61648**
61649** This file contains code used to implement mutexes on Btree objects.
61650** This code really belongs in btree.c. But btree.c is getting too
61651** big and we want to break it down some. This packaged seemed like
61652** a good breakout.
61653*/
61654/************** Include btreeInt.h in the middle of btmutex.c ****************/
61655/************** Begin file btreeInt.h ****************************************/
61656/*
61657** 2004 April 6
61658**
61659** The author disclaims copyright to this source code. In place of
61660** a legal notice, here is a blessing:
61661**
61662** May you do good and not evil.
61663** May you find forgiveness for yourself and forgive others.
61664** May you share freely, never taking more than you give.
61665**
61666*************************************************************************
61667** This file implements an external (disk-based) database using BTrees.
61668** For a detailed discussion of BTrees, refer to
61669**
61670** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
61671** "Sorting And Searching", pages 473-480. Addison-Wesley
61672** Publishing Company, Reading, Massachusetts.
61673**
61674** The basic idea is that each page of the file contains N database
61675** entries and N+1 pointers to subpages.
61676**
61677** ----------------------------------------------------------------
61678** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
61679** ----------------------------------------------------------------
61680**
61681** All of the keys on the page that Ptr(0) points to have values less
61682** than Key(0). All of the keys on page Ptr(1) and its subpages have
61683** values greater than Key(0) and less than Key(1). All of the keys
61684** on Ptr(N) and its subpages have values greater than Key(N-1). And
61685** so forth.
61686**
61687** Finding a particular key requires reading O(log(M)) pages from the
61688** disk where M is the number of entries in the tree.
61689**
61690** In this implementation, a single file can hold one or more separate
61691** BTrees. Each BTree is identified by the index of its root page. The
61692** key and data for any entry are combined to form the "payload". A
61693** fixed amount of payload can be carried directly on the database
61694** page. If the payload is larger than the preset amount then surplus
61695** bytes are stored on overflow pages. The payload for an entry
61696** and the preceding pointer are combined to form a "Cell". Each
61697** page has a small header which contains the Ptr(N) pointer and other
61698** information such as the size of key and data.
61699**
61700** FORMAT DETAILS
61701**
61702** The file is divided into pages. The first page is called page 1,
61703** the second is page 2, and so forth. A page number of zero indicates
61704** "no such page". The page size can be any power of 2 between 512 and 65536.
61705** Each page can be either a btree page, a freelist page, an overflow
61706** page, or a pointer-map page.
61707**
61708** The first page is always a btree page. The first 100 bytes of the first
61709** page contain a special header (the "file header") that describes the file.
61710** The format of the file header is as follows:
61711**
61712** OFFSET SIZE DESCRIPTION
61713** 0 16 Header string: "SQLite format 3\000"
61714** 16 2 Page size in bytes. (1 means 65536)
61715** 18 1 File format write version
61716** 19 1 File format read version
61717** 20 1 Bytes of unused space at the end of each page
61718** 21 1 Max embedded payload fraction (must be 64)
61719** 22 1 Min embedded payload fraction (must be 32)
61720** 23 1 Min leaf payload fraction (must be 32)
61721** 24 4 File change counter
61722** 28 4 Reserved for future use
61723** 32 4 First freelist page
61724** 36 4 Number of freelist pages in the file
61725** 40 60 15 4-byte meta values passed to higher layers
61726**
61727** 40 4 Schema cookie
61728** 44 4 File format of schema layer
61729** 48 4 Size of page cache
61730** 52 4 Largest root-page (auto/incr_vacuum)
61731** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
61732** 60 4 User version
61733** 64 4 Incremental vacuum mode
61734** 68 4 Application-ID
61735** 72 20 unused
61736** 92 4 The version-valid-for number
61737** 96 4 SQLITE_VERSION_NUMBER
61738**
61739** All of the integer values are big-endian (most significant byte first).
61740**
61741** The file change counter is incremented when the database is changed
61742** This counter allows other processes to know when the file has changed
61743** and thus when they need to flush their cache.
61744**
61745** The max embedded payload fraction is the amount of the total usable
61746** space in a page that can be consumed by a single cell for standard
61747** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
61748** is to limit the maximum cell size so that at least 4 cells will fit
61749** on one page. Thus the default max embedded payload fraction is 64.
61750**
61751** If the payload for a cell is larger than the max payload, then extra
61752** payload is spilled to overflow pages. Once an overflow page is allocated,
61753** as many bytes as possible are moved into the overflow pages without letting
61754** the cell size drop below the min embedded payload fraction.
61755**
61756** The min leaf payload fraction is like the min embedded payload fraction
61757** except that it applies to leaf nodes in a LEAFDATA tree. The maximum
61758** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
61759** not specified in the header.
61760**
61761** Each btree pages is divided into three sections: The header, the
61762** cell pointer array, and the cell content area. Page 1 also has a 100-byte
61763** file header that occurs before the page header.
61764**
61765** |----------------|
61766** | file header | 100 bytes. Page 1 only.
61767** |----------------|
61768** | page header | 8 bytes for leaves. 12 bytes for interior nodes
61769** |----------------|
61770** | cell pointer | | 2 bytes per cell. Sorted order.
61771** | array | | Grows downward
61772** | | v
61773** |----------------|
61774** | unallocated |
61775** | space |
61776** |----------------| ^ Grows upwards
61777** | cell content | | Arbitrary order interspersed with freeblocks.
61778** | area | | and free space fragments.
61779** |----------------|
61780**
61781** The page headers looks like this:
61782**
61783** OFFSET SIZE DESCRIPTION
61784** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
61785** 1 2 byte offset to the first freeblock
61786** 3 2 number of cells on this page
61787** 5 2 first byte of the cell content area
61788** 7 1 number of fragmented free bytes
61789** 8 4 Right child (the Ptr(N) value). Omitted on leaves.
61790**
61791** The flags define the format of this btree page. The leaf flag means that
61792** this page has no children. The zerodata flag means that this page carries
61793** only keys and no data. The intkey flag means that the key is an integer
61794** which is stored in the key size entry of the cell header rather than in
61795** the payload area.
61796**
61797** The cell pointer array begins on the first byte after the page header.
61798** The cell pointer array contains zero or more 2-byte numbers which are
61799** offsets from the beginning of the page to the cell content in the cell
61800** content area. The cell pointers occur in sorted order. The system strives
61801** to keep free space after the last cell pointer so that new cells can
61802** be easily added without having to defragment the page.
61803**
61804** Cell content is stored at the very end of the page and grows toward the
61805** beginning of the page.
61806**
61807** Unused space within the cell content area is collected into a linked list of
61808** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset
61809** to the first freeblock is given in the header. Freeblocks occur in
61810** increasing order. Because a freeblock must be at least 4 bytes in size,
61811** any group of 3 or fewer unused bytes in the cell content area cannot
61812** exist on the freeblock chain. A group of 3 or fewer free bytes is called
61813** a fragment. The total number of bytes in all fragments is recorded.
61814** in the page header at offset 7.
61815**
61816** SIZE DESCRIPTION
61817** 2 Byte offset of the next freeblock
61818** 2 Bytes in this freeblock
61819**
61820** Cells are of variable length. Cells are stored in the cell content area at
61821** the end of the page. Pointers to the cells are in the cell pointer array
61822** that immediately follows the page header. Cells is not necessarily
61823** contiguous or in order, but cell pointers are contiguous and in order.
61824**
61825** Cell content makes use of variable length integers. A variable
61826** length integer is 1 to 9 bytes where the lower 7 bits of each
61827** byte are used. The integer consists of all bytes that have bit 8 set and
61828** the first byte with bit 8 clear. The most significant byte of the integer
61829** appears first. A variable-length integer may not be more than 9 bytes long.
61830** As a special case, all 8 bytes of the 9th byte are used as data. This
61831** allows a 64-bit integer to be encoded in 9 bytes.
61832**
61833** 0x00 becomes 0x00000000
61834** 0x7f becomes 0x0000007f
61835** 0x81 0x00 becomes 0x00000080
61836** 0x82 0x00 becomes 0x00000100
61837** 0x80 0x7f becomes 0x0000007f
61838** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678
61839** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081
61840**
61841** Variable length integers are used for rowids and to hold the number of
61842** bytes of key and data in a btree cell.
61843**
61844** The content of a cell looks like this:
61845**
61846** SIZE DESCRIPTION
61847** 4 Page number of the left child. Omitted if leaf flag is set.
61848** var Number of bytes of data. Omitted if the zerodata flag is set.
61849** var Number of bytes of key. Or the key itself if intkey flag is set.
61850** * Payload
61851** 4 First page of the overflow chain. Omitted if no overflow
61852**
61853** Overflow pages form a linked list. Each page except the last is completely
61854** filled with data (pagesize - 4 bytes). The last page can have as little
61855** as 1 byte of data.
61856**
61857** SIZE DESCRIPTION
61858** 4 Page number of next overflow page
61859** * Data
61860**
61861** Freelist pages come in two subtypes: trunk pages and leaf pages. The
61862** file header points to the first in a linked list of trunk page. Each trunk
61863** page points to multiple leaf pages. The content of a leaf page is
61864** unspecified. A trunk page looks like this:
61865**
61866** SIZE DESCRIPTION
61867** 4 Page number of next trunk page
61868** 4 Number of leaf pointers on this page
61869** * zero or more pages numbers of leaves
61870*/
61871/* #include "sqliteInt.h" */
61872
61873
61874/* The following value is the maximum cell size assuming a maximum page
61875** size give above.
61876*/
61877#define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
61878
61879/* The maximum number of cells on a single page of the database. This
61880** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself
61881** plus 2 bytes for the index to the cell in the page header). Such
61882** small cells will be rare, but they are possible.
61883*/
61884#define MX_CELL(pBt) ((pBt->pageSize-8)/6)
61885
61886/* Forward declarations */
61887typedef struct MemPage MemPage;
61888typedef struct BtLock BtLock;
61889typedef struct CellInfo CellInfo;
61890
61891/*
61892** This is a magic string that appears at the beginning of every
61893** SQLite database in order to identify the file as a real database.
61894**
61895** You can change this value at compile-time by specifying a
61896** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
61897** header must be exactly 16 bytes including the zero-terminator so
61898** the string itself should be 15 characters long. If you change
61899** the header, then your custom library will not be able to read
61900** databases generated by the standard tools and the standard tools
61901** will not be able to read databases created by your custom library.
61902*/
61903#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
61904# define SQLITE_FILE_HEADER "SQLite format 3"
61905#endif
61906
61907/*
61908** Page type flags. An ORed combination of these flags appear as the
61909** first byte of on-disk image of every BTree page.
61910*/
61911#define PTF_INTKEY 0x01
61912#define PTF_ZERODATA 0x02
61913#define PTF_LEAFDATA 0x04
61914#define PTF_LEAF 0x08
61915
61916/*
61917** An instance of this object stores information about each a single database
61918** page that has been loaded into memory. The information in this object
61919** is derived from the raw on-disk page content.
61920**
61921** As each database page is loaded into memory, the pager allocats an
61922** instance of this object and zeros the first 8 bytes. (This is the
61923** "extra" information associated with each page of the pager.)
61924**
61925** Access to all fields of this structure is controlled by the mutex
61926** stored in MemPage.pBt->mutex.
61927*/
61928struct MemPage {
61929 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
61930 u8 bBusy; /* Prevent endless loops on corrupt database files */
61931 u8 intKey; /* True if table b-trees. False for index b-trees */
61932 u8 intKeyLeaf; /* True if the leaf of an intKey table */
61933 Pgno pgno; /* Page number for this page */
61934 /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
61935 ** is allocated. All fields that follow must be initialized before use */
61936 u8 leaf; /* True if a leaf page */
61937 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
61938 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
61939 u8 max1bytePayload; /* min(maxLocal,127) */
61940 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
61941 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
61942 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
61943 u16 cellOffset; /* Index in aData of first cell pointer */
61944 u16 nFree; /* Number of free bytes on the page */
61945 u16 nCell; /* Number of cells on this page, local and ovfl */
61946 u16 maskPage; /* Mask for page offset */
61947 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
61948 ** non-overflow cell */
61949 u8 *apOvfl[4]; /* Pointers to the body of overflow cells */
61950 BtShared *pBt; /* Pointer to BtShared that this page is part of */
61951 u8 *aData; /* Pointer to disk image of the page data */
61952 u8 *aDataEnd; /* One byte past the end of usable data */
61953 u8 *aCellIdx; /* The cell index area */
61954 u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
61955 DbPage *pDbPage; /* Pager page handle */
61956 u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */
61957 void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
61958};
61959
61960/*
61961** A linked list of the following structures is stored at BtShared.pLock.
61962** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
61963** is opened on the table with root page BtShared.iTable. Locks are removed
61964** from this list when a transaction is committed or rolled back, or when
61965** a btree handle is closed.
61966*/
61967struct BtLock {
61968 Btree *pBtree; /* Btree handle holding this lock */
61969 Pgno iTable; /* Root page of table */
61970 u8 eLock; /* READ_LOCK or WRITE_LOCK */
61971 BtLock *pNext; /* Next in BtShared.pLock list */
61972};
61973
61974/* Candidate values for BtLock.eLock */
61975#define READ_LOCK 1
61976#define WRITE_LOCK 2
61977
61978/* A Btree handle
61979**
61980** A database connection contains a pointer to an instance of
61981** this object for every database file that it has open. This structure
61982** is opaque to the database connection. The database connection cannot
61983** see the internals of this structure and only deals with pointers to
61984** this structure.
61985**
61986** For some database files, the same underlying database cache might be
61987** shared between multiple connections. In that case, each connection
61988** has it own instance of this object. But each instance of this object
61989** points to the same BtShared object. The database cache and the
61990** schema associated with the database file are all contained within
61991** the BtShared object.
61992**
61993** All fields in this structure are accessed under sqlite3.mutex.
61994** The pBt pointer itself may not be changed while there exists cursors
61995** in the referenced BtShared that point back to this Btree since those
61996** cursors have to go through this Btree to find their BtShared and
61997** they often do so without holding sqlite3.mutex.
61998*/
61999struct Btree {
62000 sqlite3 *db; /* The database connection holding this btree */
62001 BtShared *pBt; /* Sharable content of this btree */
62002 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
62003 u8 sharable; /* True if we can share pBt with another db */
62004 u8 locked; /* True if db currently has pBt locked */
62005 u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
62006 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
62007 int nBackup; /* Number of backup operations reading this btree */
62008 u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */
62009 Btree *pNext; /* List of other sharable Btrees from the same db */
62010 Btree *pPrev; /* Back pointer of the same list */
62011#ifndef SQLITE_OMIT_SHARED_CACHE
62012 BtLock lock; /* Object used to lock page 1 */
62013#endif
62014};
62015
62016/*
62017** Btree.inTrans may take one of the following values.
62018**
62019** If the shared-data extension is enabled, there may be multiple users
62020** of the Btree structure. At most one of these may open a write transaction,
62021** but any number may have active read transactions.
62022*/
62023#define TRANS_NONE 0
62024#define TRANS_READ 1
62025#define TRANS_WRITE 2
62026
62027/*
62028** An instance of this object represents a single database file.
62029**
62030** A single database file can be in use at the same time by two
62031** or more database connections. When two or more connections are
62032** sharing the same database file, each connection has it own
62033** private Btree object for the file and each of those Btrees points
62034** to this one BtShared object. BtShared.nRef is the number of
62035** connections currently sharing this database file.
62036**
62037** Fields in this structure are accessed under the BtShared.mutex
62038** mutex, except for nRef and pNext which are accessed under the
62039** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field
62040** may not be modified once it is initially set as long as nRef>0.
62041** The pSchema field may be set once under BtShared.mutex and
62042** thereafter is unchanged as long as nRef>0.
62043**
62044** isPending:
62045**
62046** If a BtShared client fails to obtain a write-lock on a database
62047** table (because there exists one or more read-locks on the table),
62048** the shared-cache enters 'pending-lock' state and isPending is
62049** set to true.
62050**
62051** The shared-cache leaves the 'pending lock' state when either of
62052** the following occur:
62053**
62054** 1) The current writer (BtShared.pWriter) concludes its transaction, OR
62055** 2) The number of locks held by other connections drops to zero.
62056**
62057** while in the 'pending-lock' state, no connection may start a new
62058** transaction.
62059**
62060** This feature is included to help prevent writer-starvation.
62061*/
62062struct BtShared {
62063 Pager *pPager; /* The page cache */
62064 sqlite3 *db; /* Database connection currently using this Btree */
62065 BtCursor *pCursor; /* A list of all open cursors */
62066 MemPage *pPage1; /* First page of the database */
62067 u8 openFlags; /* Flags to sqlite3BtreeOpen() */
62068#ifndef SQLITE_OMIT_AUTOVACUUM
62069 u8 autoVacuum; /* True if auto-vacuum is enabled */
62070 u8 incrVacuum; /* True if incr-vacuum is enabled */
62071 u8 bDoTruncate; /* True to truncate db on commit */
62072#endif
62073 u8 inTransaction; /* Transaction state */
62074 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
62075#ifdef SQLITE_HAS_CODEC
62076 u8 optimalReserve; /* Desired amount of reserved space per page */
62077#endif
62078 u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
62079 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
62080 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
62081 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
62082 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
62083 u32 pageSize; /* Total number of bytes on a page */
62084 u32 usableSize; /* Number of usable bytes on each page */
62085 int nTransaction; /* Number of open transactions (read + write) */
62086 u32 nPage; /* Number of pages in the database */
62087 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
62088 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
62089 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
62090 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
62091#ifndef SQLITE_OMIT_SHARED_CACHE
62092 int nRef; /* Number of references to this structure */
62093 BtShared *pNext; /* Next on a list of sharable BtShared structs */
62094 BtLock *pLock; /* List of locks held on this shared-btree struct */
62095 Btree *pWriter; /* Btree with currently open write transaction */
62096#endif
62097 u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */
62098};
62099
62100/*
62101** Allowed values for BtShared.btsFlags
62102*/
62103#define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */
62104#define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */
62105#define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */
62106#define BTS_OVERWRITE 0x0008 /* Overwrite deleted content with zeros */
62107#define BTS_FAST_SECURE 0x000c /* Combination of the previous two */
62108#define BTS_INITIALLY_EMPTY 0x0010 /* Database was empty at trans start */
62109#define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
62110#define BTS_EXCLUSIVE 0x0040 /* pWriter has an exclusive lock */
62111#define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
62112
62113/*
62114** An instance of the following structure is used to hold information
62115** about a cell. The parseCellPtr() function fills in this structure
62116** based on information extract from the raw disk page.
62117*/
62118struct CellInfo {
62119 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */
62120 u8 *pPayload; /* Pointer to the start of payload */
62121 u32 nPayload; /* Bytes of payload */
62122 u16 nLocal; /* Amount of payload held locally, not on overflow */
62123 u16 nSize; /* Size of the cell content on the main b-tree page */
62124};
62125
62126/*
62127** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
62128** this will be declared corrupt. This value is calculated based on a
62129** maximum database size of 2^31 pages a minimum fanout of 2 for a
62130** root-node and 3 for all other internal nodes.
62131**
62132** If a tree that appears to be taller than this is encountered, it is
62133** assumed that the database is corrupt.
62134*/
62135#define BTCURSOR_MAX_DEPTH 20
62136
62137/*
62138** A cursor is a pointer to a particular entry within a particular
62139** b-tree within a database file.
62140**
62141** The entry is identified by its MemPage and the index in
62142** MemPage.aCell[] of the entry.
62143**
62144** A single database file can be shared by two more database connections,
62145** but cursors cannot be shared. Each cursor is associated with a
62146** particular database connection identified BtCursor.pBtree.db.
62147**
62148** Fields in this structure are accessed under the BtShared.mutex
62149** found at self->pBt->mutex.
62150**
62151** skipNext meaning:
62152** eState==SKIPNEXT && skipNext>0: Next sqlite3BtreeNext() is no-op.
62153** eState==SKIPNEXT && skipNext<0: Next sqlite3BtreePrevious() is no-op.
62154** eState==FAULT: Cursor fault with skipNext as error code.
62155*/
62156struct BtCursor {
62157 u8 eState; /* One of the CURSOR_XXX constants (see below) */
62158 u8 curFlags; /* zero or more BTCF_* flags defined below */
62159 u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */
62160 u8 hints; /* As configured by CursorSetHints() */
62161 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
62162 ** Error code if eState==CURSOR_FAULT */
62163 Btree *pBtree; /* The Btree to which this cursor belongs */
62164 Pgno *aOverflow; /* Cache of overflow page locations */
62165 void *pKey; /* Saved key that was cursor last known position */
62166 /* All fields above are zeroed when the cursor is allocated. See
62167 ** sqlite3BtreeCursorZero(). Fields that follow must be manually
62168 ** initialized. */
62169#define BTCURSOR_FIRST_UNINIT pBt /* Name of first uninitialized field */
62170 BtShared *pBt; /* The BtShared this cursor points to */
62171 BtCursor *pNext; /* Forms a linked list of all cursors */
62172 CellInfo info; /* A parse of the cell we are pointing at */
62173 i64 nKey; /* Size of pKey, or last integer key */
62174 Pgno pgnoRoot; /* The root page of this tree */
62175 i8 iPage; /* Index of current page in apPage */
62176 u8 curIntKey; /* Value of apPage[0]->intKey */
62177 u16 ix; /* Current index for apPage[iPage] */
62178 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
62179 struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */
62180 MemPage *pPage; /* Current page */
62181 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
62182};
62183
62184/*
62185** Legal values for BtCursor.curFlags
62186*/
62187#define BTCF_WriteFlag 0x01 /* True if a write cursor */
62188#define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */
62189#define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */
62190#define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */
62191#define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */
62192#define BTCF_Multiple 0x20 /* Maybe another cursor on the same btree */
62193
62194/*
62195** Potential values for BtCursor.eState.
62196**
62197** CURSOR_INVALID:
62198** Cursor does not point to a valid entry. This can happen (for example)
62199** because the table is empty or because BtreeCursorFirst() has not been
62200** called.
62201**
62202** CURSOR_VALID:
62203** Cursor points to a valid entry. getPayload() etc. may be called.
62204**
62205** CURSOR_SKIPNEXT:
62206** Cursor is valid except that the Cursor.skipNext field is non-zero
62207** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
62208** operation should be a no-op.
62209**
62210** CURSOR_REQUIRESEEK:
62211** The table that this cursor was opened on still exists, but has been
62212** modified since the cursor was last used. The cursor position is saved
62213** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
62214** this state, restoreCursorPosition() can be called to attempt to
62215** seek the cursor to the saved position.
62216**
62217** CURSOR_FAULT:
62218** An unrecoverable error (an I/O error or a malloc failure) has occurred
62219** on a different connection that shares the BtShared cache with this
62220** cursor. The error has left the cache in an inconsistent state.
62221** Do nothing else with this cursor. Any attempt to use the cursor
62222** should return the error code stored in BtCursor.skipNext
62223*/
62224#define CURSOR_VALID 0
62225#define CURSOR_INVALID 1
62226#define CURSOR_SKIPNEXT 2
62227#define CURSOR_REQUIRESEEK 3
62228#define CURSOR_FAULT 4
62229
62230/*
62231** The database page the PENDING_BYTE occupies. This page is never used.
62232*/
62233# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
62234
62235/*
62236** These macros define the location of the pointer-map entry for a
62237** database page. The first argument to each is the number of usable
62238** bytes on each page of the database (often 1024). The second is the
62239** page number to look up in the pointer map.
62240**
62241** PTRMAP_PAGENO returns the database page number of the pointer-map
62242** page that stores the required pointer. PTRMAP_PTROFFSET returns
62243** the offset of the requested map entry.
62244**
62245** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
62246** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
62247** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
62248** this test.
62249*/
62250#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
62251#define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
62252#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
62253
62254/*
62255** The pointer map is a lookup table that identifies the parent page for
62256** each child page in the database file. The parent page is the page that
62257** contains a pointer to the child. Every page in the database contains
62258** 0 or 1 parent pages. (In this context 'database page' refers
62259** to any page that is not part of the pointer map itself.) Each pointer map
62260** entry consists of a single byte 'type' and a 4 byte parent page number.
62261** The PTRMAP_XXX identifiers below are the valid types.
62262**
62263** The purpose of the pointer map is to facility moving pages from one
62264** position in the file to another as part of autovacuum. When a page
62265** is moved, the pointer in its parent must be updated to point to the
62266** new location. The pointer map is used to locate the parent page quickly.
62267**
62268** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
62269** used in this case.
62270**
62271** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
62272** is not used in this case.
62273**
62274** PTRMAP_OVERFLOW1: The database page is the first page in a list of
62275** overflow pages. The page number identifies the page that
62276** contains the cell with a pointer to this overflow page.
62277**
62278** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
62279** overflow pages. The page-number identifies the previous
62280** page in the overflow page list.
62281**
62282** PTRMAP_BTREE: The database page is a non-root btree page. The page number
62283** identifies the parent page in the btree.
62284*/
62285#define PTRMAP_ROOTPAGE 1
62286#define PTRMAP_FREEPAGE 2
62287#define PTRMAP_OVERFLOW1 3
62288#define PTRMAP_OVERFLOW2 4
62289#define PTRMAP_BTREE 5
62290
62291/* A bunch of assert() statements to check the transaction state variables
62292** of handle p (type Btree*) are internally consistent.
62293*/
62294#define btreeIntegrity(p) \
62295 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
62296 assert( p->pBt->inTransaction>=p->inTrans );
62297
62298
62299/*
62300** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
62301** if the database supports auto-vacuum or not. Because it is used
62302** within an expression that is an argument to another macro
62303** (sqliteMallocRaw), it is not possible to use conditional compilation.
62304** So, this macro is defined instead.
62305*/
62306#ifndef SQLITE_OMIT_AUTOVACUUM
62307#define ISAUTOVACUUM (pBt->autoVacuum)
62308#else
62309#define ISAUTOVACUUM 0
62310#endif
62311
62312
62313/*
62314** This structure is passed around through all the sanity checking routines
62315** in order to keep track of some global state information.
62316**
62317** The aRef[] array is allocated so that there is 1 bit for each page in
62318** the database. As the integrity-check proceeds, for each page used in
62319** the database the corresponding bit is set. This allows integrity-check to
62320** detect pages that are used twice and orphaned pages (both of which
62321** indicate corruption).
62322*/
62323typedef struct IntegrityCk IntegrityCk;
62324struct IntegrityCk {
62325 BtShared *pBt; /* The tree being checked out */
62326 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
62327 u8 *aPgRef; /* 1 bit per page in the db (see above) */
62328 Pgno nPage; /* Number of pages in the database */
62329 int mxErr; /* Stop accumulating errors when this reaches zero */
62330 int nErr; /* Number of messages written to zErrMsg so far */
62331 int mallocFailed; /* A memory allocation error has occurred */
62332 const char *zPfx; /* Error message prefix */
62333 int v1, v2; /* Values for up to two %d fields in zPfx */
62334 StrAccum errMsg; /* Accumulate the error message text here */
62335 u32 *heap; /* Min-heap used for analyzing cell coverage */
62336};
62337
62338/*
62339** Routines to read or write a two- and four-byte big-endian integer values.
62340*/
62341#define get2byte(x) ((x)[0]<<8 | (x)[1])
62342#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
62343#define get4byte sqlite3Get4byte
62344#define put4byte sqlite3Put4byte
62345
62346/*
62347** get2byteAligned(), unlike get2byte(), requires that its argument point to a
62348** two-byte aligned address. get2bytea() is only used for accessing the
62349** cell addresses in a btree header.
62350*/
62351#if SQLITE_BYTEORDER==4321
62352# define get2byteAligned(x) (*(u16*)(x))
62353#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
62354# define get2byteAligned(x) __builtin_bswap16(*(u16*)(x))
62355#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
62356# define get2byteAligned(x) _byteswap_ushort(*(u16*)(x))
62357#else
62358# define get2byteAligned(x) ((x)[0]<<8 | (x)[1])
62359#endif
62360
62361/************** End of btreeInt.h ********************************************/
62362/************** Continuing where we left off in btmutex.c ********************/
62363#ifndef SQLITE_OMIT_SHARED_CACHE
62364#if SQLITE_THREADSAFE
62365
62366/*
62367** Obtain the BtShared mutex associated with B-Tree handle p. Also,
62368** set BtShared.db to the database handle associated with p and the
62369** p->locked boolean to true.
62370*/
62371static void lockBtreeMutex(Btree *p){
62372 assert( p->locked==0 );
62373 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
62374 assert( sqlite3_mutex_held(p->db->mutex) );
62375
62376 sqlite3_mutex_enter(p->pBt->mutex);
62377 p->pBt->db = p->db;
62378 p->locked = 1;
62379}
62380
62381/*
62382** Release the BtShared mutex associated with B-Tree handle p and
62383** clear the p->locked boolean.
62384*/
62385static void SQLITE_NOINLINE unlockBtreeMutex(Btree *p){
62386 BtShared *pBt = p->pBt;
62387 assert( p->locked==1 );
62388 assert( sqlite3_mutex_held(pBt->mutex) );
62389 assert( sqlite3_mutex_held(p->db->mutex) );
62390 assert( p->db==pBt->db );
62391
62392 sqlite3_mutex_leave(pBt->mutex);
62393 p->locked = 0;
62394}
62395
62396/* Forward reference */
62397static void SQLITE_NOINLINE btreeLockCarefully(Btree *p);
62398
62399/*
62400** Enter a mutex on the given BTree object.
62401**
62402** If the object is not sharable, then no mutex is ever required
62403** and this routine is a no-op. The underlying mutex is non-recursive.
62404** But we keep a reference count in Btree.wantToLock so the behavior
62405** of this interface is recursive.
62406**
62407** To avoid deadlocks, multiple Btrees are locked in the same order
62408** by all database connections. The p->pNext is a list of other
62409** Btrees belonging to the same database connection as the p Btree
62410** which need to be locked after p. If we cannot get a lock on
62411** p, then first unlock all of the others on p->pNext, then wait
62412** for the lock to become available on p, then relock all of the
62413** subsequent Btrees that desire a lock.
62414*/
62415SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
62416 /* Some basic sanity checking on the Btree. The list of Btrees
62417 ** connected by pNext and pPrev should be in sorted order by
62418 ** Btree.pBt value. All elements of the list should belong to
62419 ** the same connection. Only shared Btrees are on the list. */
62420 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
62421 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
62422 assert( p->pNext==0 || p->pNext->db==p->db );
62423 assert( p->pPrev==0 || p->pPrev->db==p->db );
62424 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
62425
62426 /* Check for locking consistency */
62427 assert( !p->locked || p->wantToLock>0 );
62428 assert( p->sharable || p->wantToLock==0 );
62429
62430 /* We should already hold a lock on the database connection */
62431 assert( sqlite3_mutex_held(p->db->mutex) );
62432
62433 /* Unless the database is sharable and unlocked, then BtShared.db
62434 ** should already be set correctly. */
62435 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
62436
62437 if( !p->sharable ) return;
62438 p->wantToLock++;
62439 if( p->locked ) return;
62440 btreeLockCarefully(p);
62441}
62442
62443/* This is a helper function for sqlite3BtreeLock(). By moving
62444** complex, but seldom used logic, out of sqlite3BtreeLock() and
62445** into this routine, we avoid unnecessary stack pointer changes
62446** and thus help the sqlite3BtreeLock() routine to run much faster
62447** in the common case.
62448*/
62449static void SQLITE_NOINLINE btreeLockCarefully(Btree *p){
62450 Btree *pLater;
62451
62452 /* In most cases, we should be able to acquire the lock we
62453 ** want without having to go through the ascending lock
62454 ** procedure that follows. Just be sure not to block.
62455 */
62456 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
62457 p->pBt->db = p->db;
62458 p->locked = 1;
62459 return;
62460 }
62461
62462 /* To avoid deadlock, first release all locks with a larger
62463 ** BtShared address. Then acquire our lock. Then reacquire
62464 ** the other BtShared locks that we used to hold in ascending
62465 ** order.
62466 */
62467 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
62468 assert( pLater->sharable );
62469 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
62470 assert( !pLater->locked || pLater->wantToLock>0 );
62471 if( pLater->locked ){
62472 unlockBtreeMutex(pLater);
62473 }
62474 }
62475 lockBtreeMutex(p);
62476 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
62477 if( pLater->wantToLock ){
62478 lockBtreeMutex(pLater);
62479 }
62480 }
62481}
62482
62483
62484/*
62485** Exit the recursive mutex on a Btree.
62486*/
62487SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
62488 assert( sqlite3_mutex_held(p->db->mutex) );
62489 if( p->sharable ){
62490 assert( p->wantToLock>0 );
62491 p->wantToLock--;
62492 if( p->wantToLock==0 ){
62493 unlockBtreeMutex(p);
62494 }
62495 }
62496}
62497
62498#ifndef NDEBUG
62499/*
62500** Return true if the BtShared mutex is held on the btree, or if the
62501** B-Tree is not marked as sharable.
62502**
62503** This routine is used only from within assert() statements.
62504*/
62505SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
62506 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
62507 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
62508 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
62509 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
62510
62511 return (p->sharable==0 || p->locked);
62512}
62513#endif
62514
62515
62516/*
62517** Enter the mutex on every Btree associated with a database
62518** connection. This is needed (for example) prior to parsing
62519** a statement since we will be comparing table and column names
62520** against all schemas and we do not want those schemas being
62521** reset out from under us.
62522**
62523** There is a corresponding leave-all procedures.
62524**
62525** Enter the mutexes in accending order by BtShared pointer address
62526** to avoid the possibility of deadlock when two threads with
62527** two or more btrees in common both try to lock all their btrees
62528** at the same instant.
62529*/
62530static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
62531 int i;
62532 int skipOk = 1;
62533 Btree *p;
62534 assert( sqlite3_mutex_held(db->mutex) );
62535 for(i=0; i<db->nDb; i++){
62536 p = db->aDb[i].pBt;
62537 if( p && p->sharable ){
62538 sqlite3BtreeEnter(p);
62539 skipOk = 0;
62540 }
62541 }
62542 db->noSharedCache = skipOk;
62543}
62544SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
62545 if( db->noSharedCache==0 ) btreeEnterAll(db);
62546}
62547static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
62548 int i;
62549 Btree *p;
62550 assert( sqlite3_mutex_held(db->mutex) );
62551 for(i=0; i<db->nDb; i++){
62552 p = db->aDb[i].pBt;
62553 if( p ) sqlite3BtreeLeave(p);
62554 }
62555}
62556SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
62557 if( db->noSharedCache==0 ) btreeLeaveAll(db);
62558}
62559
62560#ifndef NDEBUG
62561/*
62562** Return true if the current thread holds the database connection
62563** mutex and all required BtShared mutexes.
62564**
62565** This routine is used inside assert() statements only.
62566*/
62567SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
62568 int i;
62569 if( !sqlite3_mutex_held(db->mutex) ){
62570 return 0;
62571 }
62572 for(i=0; i<db->nDb; i++){
62573 Btree *p;
62574 p = db->aDb[i].pBt;
62575 if( p && p->sharable &&
62576 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
62577 return 0;
62578 }
62579 }
62580 return 1;
62581}
62582#endif /* NDEBUG */
62583
62584#ifndef NDEBUG
62585/*
62586** Return true if the correct mutexes are held for accessing the
62587** db->aDb[iDb].pSchema structure. The mutexes required for schema
62588** access are:
62589**
62590** (1) The mutex on db
62591** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
62592**
62593** If pSchema is not NULL, then iDb is computed from pSchema and
62594** db using sqlite3SchemaToIndex().
62595*/
62596SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
62597 Btree *p;
62598 assert( db!=0 );
62599 if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
62600 assert( iDb>=0 && iDb<db->nDb );
62601 if( !sqlite3_mutex_held(db->mutex) ) return 0;
62602 if( iDb==1 ) return 1;
62603 p = db->aDb[iDb].pBt;
62604 assert( p!=0 );
62605 return p->sharable==0 || p->locked==1;
62606}
62607#endif /* NDEBUG */
62608
62609#else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
62610/*
62611** The following are special cases for mutex enter routines for use
62612** in single threaded applications that use shared cache. Except for
62613** these two routines, all mutex operations are no-ops in that case and
62614** are null #defines in btree.h.
62615**
62616** If shared cache is disabled, then all btree mutex routines, including
62617** the ones below, are no-ops and are null #defines in btree.h.
62618*/
62619
62620SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
62621 p->pBt->db = p->db;
62622}
62623SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
62624 int i;
62625 for(i=0; i<db->nDb; i++){
62626 Btree *p = db->aDb[i].pBt;
62627 if( p ){
62628 p->pBt->db = p->db;
62629 }
62630 }
62631}
62632#endif /* if SQLITE_THREADSAFE */
62633
62634#ifndef SQLITE_OMIT_INCRBLOB
62635/*
62636** Enter a mutex on a Btree given a cursor owned by that Btree.
62637**
62638** These entry points are used by incremental I/O only. Enter() is required
62639** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not
62640** the build is threadsafe. Leave() is only required by threadsafe builds.
62641*/
62642SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
62643 sqlite3BtreeEnter(pCur->pBtree);
62644}
62645# if SQLITE_THREADSAFE
62646SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
62647 sqlite3BtreeLeave(pCur->pBtree);
62648}
62649# endif
62650#endif /* ifndef SQLITE_OMIT_INCRBLOB */
62651
62652#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
62653
62654/************** End of btmutex.c *********************************************/
62655/************** Begin file btree.c *******************************************/
62656/*
62657** 2004 April 6
62658**
62659** The author disclaims copyright to this source code. In place of
62660** a legal notice, here is a blessing:
62661**
62662** May you do good and not evil.
62663** May you find forgiveness for yourself and forgive others.
62664** May you share freely, never taking more than you give.
62665**
62666*************************************************************************
62667** This file implements an external (disk-based) database using BTrees.
62668** See the header comment on "btreeInt.h" for additional information.
62669** Including a description of file format and an overview of operation.
62670*/
62671/* #include "btreeInt.h" */
62672
62673/*
62674** The header string that appears at the beginning of every
62675** SQLite database.
62676*/
62677static const char zMagicHeader[] = SQLITE_FILE_HEADER;
62678
62679/*
62680** Set this global variable to 1 to enable tracing using the TRACE
62681** macro.
62682*/
62683#if 0
62684int sqlite3BtreeTrace=1; /* True to enable tracing */
62685# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
62686#else
62687# define TRACE(X)
62688#endif
62689
62690/*
62691** Extract a 2-byte big-endian integer from an array of unsigned bytes.
62692** But if the value is zero, make it 65536.
62693**
62694** This routine is used to extract the "offset to cell content area" value
62695** from the header of a btree page. If the page size is 65536 and the page
62696** is empty, the offset should be 65536, but the 2-byte value stores zero.
62697** This routine makes the necessary adjustment to 65536.
62698*/
62699#define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
62700
62701/*
62702** Values passed as the 5th argument to allocateBtreePage()
62703*/
62704#define BTALLOC_ANY 0 /* Allocate any page */
62705#define BTALLOC_EXACT 1 /* Allocate exact page if possible */
62706#define BTALLOC_LE 2 /* Allocate any page <= the parameter */
62707
62708/*
62709** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
62710** defined, or 0 if it is. For example:
62711**
62712** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
62713*/
62714#ifndef SQLITE_OMIT_AUTOVACUUM
62715#define IfNotOmitAV(expr) (expr)
62716#else
62717#define IfNotOmitAV(expr) 0
62718#endif
62719
62720#ifndef SQLITE_OMIT_SHARED_CACHE
62721/*
62722** A list of BtShared objects that are eligible for participation
62723** in shared cache. This variable has file scope during normal builds,
62724** but the test harness needs to access it so we make it global for
62725** test builds.
62726**
62727** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
62728*/
62729#ifdef SQLITE_TEST
62730SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
62731#else
62732static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
62733#endif
62734#endif /* SQLITE_OMIT_SHARED_CACHE */
62735
62736#ifndef SQLITE_OMIT_SHARED_CACHE
62737/*
62738** Enable or disable the shared pager and schema features.
62739**
62740** This routine has no effect on existing database connections.
62741** The shared cache setting effects only future calls to
62742** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
62743*/
62744SQLITE_API int sqlite3_enable_shared_cache(int enable){
62745 sqlite3GlobalConfig.sharedCacheEnabled = enable;
62746 return SQLITE_OK;
62747}
62748#endif
62749
62750
62751
62752#ifdef SQLITE_OMIT_SHARED_CACHE
62753 /*
62754 ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
62755 ** and clearAllSharedCacheTableLocks()
62756 ** manipulate entries in the BtShared.pLock linked list used to store
62757 ** shared-cache table level locks. If the library is compiled with the
62758 ** shared-cache feature disabled, then there is only ever one user
62759 ** of each BtShared structure and so this locking is not necessary.
62760 ** So define the lock related functions as no-ops.
62761 */
62762 #define querySharedCacheTableLock(a,b,c) SQLITE_OK
62763 #define setSharedCacheTableLock(a,b,c) SQLITE_OK
62764 #define clearAllSharedCacheTableLocks(a)
62765 #define downgradeAllSharedCacheTableLocks(a)
62766 #define hasSharedCacheTableLock(a,b,c,d) 1
62767 #define hasReadConflicts(a, b) 0
62768#endif
62769
62770/*
62771** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
62772** (MemPage*) as an argument. The (MemPage*) must not be NULL.
62773**
62774** If SQLITE_DEBUG is not defined, then this macro is equivalent to
62775** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
62776** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
62777** with the page number and filename associated with the (MemPage*).
62778*/
62779#ifdef SQLITE_DEBUG
62780int corruptPageError(int lineno, MemPage *p){
62781 char *zMsg;
62782 sqlite3BeginBenignMalloc();
62783 zMsg = sqlite3_mprintf("database corruption page %d of %s",
62784 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
62785 );
62786 sqlite3EndBenignMalloc();
62787 if( zMsg ){
62788 sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
62789 }
62790 sqlite3_free(zMsg);
62791 return SQLITE_CORRUPT_BKPT;
62792}
62793# define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage)
62794#else
62795# define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
62796#endif
62797
62798#ifndef SQLITE_OMIT_SHARED_CACHE
62799
62800#ifdef SQLITE_DEBUG
62801/*
62802**** This function is only used as part of an assert() statement. ***
62803**
62804** Check to see if pBtree holds the required locks to read or write to the
62805** table with root page iRoot. Return 1 if it does and 0 if not.
62806**
62807** For example, when writing to a table with root-page iRoot via
62808** Btree connection pBtree:
62809**
62810** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
62811**
62812** When writing to an index that resides in a sharable database, the
62813** caller should have first obtained a lock specifying the root page of
62814** the corresponding table. This makes things a bit more complicated,
62815** as this module treats each table as a separate structure. To determine
62816** the table corresponding to the index being written, this
62817** function has to search through the database schema.
62818**
62819** Instead of a lock on the table/index rooted at page iRoot, the caller may
62820** hold a write-lock on the schema table (root page 1). This is also
62821** acceptable.
62822*/
62823static int hasSharedCacheTableLock(
62824 Btree *pBtree, /* Handle that must hold lock */
62825 Pgno iRoot, /* Root page of b-tree */
62826 int isIndex, /* True if iRoot is the root of an index b-tree */
62827 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
62828){
62829 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
62830 Pgno iTab = 0;
62831 BtLock *pLock;
62832
62833 /* If this database is not shareable, or if the client is reading
62834 ** and has the read-uncommitted flag set, then no lock is required.
62835 ** Return true immediately.
62836 */
62837 if( (pBtree->sharable==0)
62838 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
62839 ){
62840 return 1;
62841 }
62842
62843 /* If the client is reading or writing an index and the schema is
62844 ** not loaded, then it is too difficult to actually check to see if
62845 ** the correct locks are held. So do not bother - just return true.
62846 ** This case does not come up very often anyhow.
62847 */
62848 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
62849 return 1;
62850 }
62851
62852 /* Figure out the root-page that the lock should be held on. For table
62853 ** b-trees, this is just the root page of the b-tree being read or
62854 ** written. For index b-trees, it is the root page of the associated
62855 ** table. */
62856 if( isIndex ){
62857 HashElem *p;
62858 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
62859 Index *pIdx = (Index *)sqliteHashData(p);
62860 if( pIdx->tnum==(int)iRoot ){
62861 if( iTab ){
62862 /* Two or more indexes share the same root page. There must
62863 ** be imposter tables. So just return true. The assert is not
62864 ** useful in that case. */
62865 return 1;
62866 }
62867 iTab = pIdx->pTable->tnum;
62868 }
62869 }
62870 }else{
62871 iTab = iRoot;
62872 }
62873
62874 /* Search for the required lock. Either a write-lock on root-page iTab, a
62875 ** write-lock on the schema table, or (if the client is reading) a
62876 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
62877 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
62878 if( pLock->pBtree==pBtree
62879 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
62880 && pLock->eLock>=eLockType
62881 ){
62882 return 1;
62883 }
62884 }
62885
62886 /* Failed to find the required lock. */
62887 return 0;
62888}
62889#endif /* SQLITE_DEBUG */
62890
62891#ifdef SQLITE_DEBUG
62892/*
62893**** This function may be used as part of assert() statements only. ****
62894**
62895** Return true if it would be illegal for pBtree to write into the
62896** table or index rooted at iRoot because other shared connections are
62897** simultaneously reading that same table or index.
62898**
62899** It is illegal for pBtree to write if some other Btree object that
62900** shares the same BtShared object is currently reading or writing
62901** the iRoot table. Except, if the other Btree object has the
62902** read-uncommitted flag set, then it is OK for the other object to
62903** have a read cursor.
62904**
62905** For example, before writing to any part of the table or index
62906** rooted at page iRoot, one should call:
62907**
62908** assert( !hasReadConflicts(pBtree, iRoot) );
62909*/
62910static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
62911 BtCursor *p;
62912 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
62913 if( p->pgnoRoot==iRoot
62914 && p->pBtree!=pBtree
62915 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
62916 ){
62917 return 1;
62918 }
62919 }
62920 return 0;
62921}
62922#endif /* #ifdef SQLITE_DEBUG */
62923
62924/*
62925** Query to see if Btree handle p may obtain a lock of type eLock
62926** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
62927** SQLITE_OK if the lock may be obtained (by calling
62928** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
62929*/
62930static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
62931 BtShared *pBt = p->pBt;
62932 BtLock *pIter;
62933
62934 assert( sqlite3BtreeHoldsMutex(p) );
62935 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
62936 assert( p->db!=0 );
62937 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
62938
62939 /* If requesting a write-lock, then the Btree must have an open write
62940 ** transaction on this file. And, obviously, for this to be so there
62941 ** must be an open write transaction on the file itself.
62942 */
62943 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
62944 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
62945
62946 /* This routine is a no-op if the shared-cache is not enabled */
62947 if( !p->sharable ){
62948 return SQLITE_OK;
62949 }
62950
62951 /* If some other connection is holding an exclusive lock, the
62952 ** requested lock may not be obtained.
62953 */
62954 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
62955 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
62956 return SQLITE_LOCKED_SHAREDCACHE;
62957 }
62958
62959 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
62960 /* The condition (pIter->eLock!=eLock) in the following if(...)
62961 ** statement is a simplification of:
62962 **
62963 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
62964 **
62965 ** since we know that if eLock==WRITE_LOCK, then no other connection
62966 ** may hold a WRITE_LOCK on any table in this file (since there can
62967 ** only be a single writer).
62968 */
62969 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
62970 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
62971 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
62972 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
62973 if( eLock==WRITE_LOCK ){
62974 assert( p==pBt->pWriter );
62975 pBt->btsFlags |= BTS_PENDING;
62976 }
62977 return SQLITE_LOCKED_SHAREDCACHE;
62978 }
62979 }
62980 return SQLITE_OK;
62981}
62982#endif /* !SQLITE_OMIT_SHARED_CACHE */
62983
62984#ifndef SQLITE_OMIT_SHARED_CACHE
62985/*
62986** Add a lock on the table with root-page iTable to the shared-btree used
62987** by Btree handle p. Parameter eLock must be either READ_LOCK or
62988** WRITE_LOCK.
62989**
62990** This function assumes the following:
62991**
62992** (a) The specified Btree object p is connected to a sharable
62993** database (one with the BtShared.sharable flag set), and
62994**
62995** (b) No other Btree objects hold a lock that conflicts
62996** with the requested lock (i.e. querySharedCacheTableLock() has
62997** already been called and returned SQLITE_OK).
62998**
62999** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
63000** is returned if a malloc attempt fails.
63001*/
63002static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
63003 BtShared *pBt = p->pBt;
63004 BtLock *pLock = 0;
63005 BtLock *pIter;
63006
63007 assert( sqlite3BtreeHoldsMutex(p) );
63008 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
63009 assert( p->db!=0 );
63010
63011 /* A connection with the read-uncommitted flag set will never try to
63012 ** obtain a read-lock using this function. The only read-lock obtained
63013 ** by a connection in read-uncommitted mode is on the sqlite_master
63014 ** table, and that lock is obtained in BtreeBeginTrans(). */
63015 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
63016
63017 /* This function should only be called on a sharable b-tree after it
63018 ** has been determined that no other b-tree holds a conflicting lock. */
63019 assert( p->sharable );
63020 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
63021
63022 /* First search the list for an existing lock on this table. */
63023 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
63024 if( pIter->iTable==iTable && pIter->pBtree==p ){
63025 pLock = pIter;
63026 break;
63027 }
63028 }
63029
63030 /* If the above search did not find a BtLock struct associating Btree p
63031 ** with table iTable, allocate one and link it into the list.
63032 */
63033 if( !pLock ){
63034 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
63035 if( !pLock ){
63036 return SQLITE_NOMEM_BKPT;
63037 }
63038 pLock->iTable = iTable;
63039 pLock->pBtree = p;
63040 pLock->pNext = pBt->pLock;
63041 pBt->pLock = pLock;
63042 }
63043
63044 /* Set the BtLock.eLock variable to the maximum of the current lock
63045 ** and the requested lock. This means if a write-lock was already held
63046 ** and a read-lock requested, we don't incorrectly downgrade the lock.
63047 */
63048 assert( WRITE_LOCK>READ_LOCK );
63049 if( eLock>pLock->eLock ){
63050 pLock->eLock = eLock;
63051 }
63052
63053 return SQLITE_OK;
63054}
63055#endif /* !SQLITE_OMIT_SHARED_CACHE */
63056
63057#ifndef SQLITE_OMIT_SHARED_CACHE
63058/*
63059** Release all the table locks (locks obtained via calls to
63060** the setSharedCacheTableLock() procedure) held by Btree object p.
63061**
63062** This function assumes that Btree p has an open read or write
63063** transaction. If it does not, then the BTS_PENDING flag
63064** may be incorrectly cleared.
63065*/
63066static void clearAllSharedCacheTableLocks(Btree *p){
63067 BtShared *pBt = p->pBt;
63068 BtLock **ppIter = &pBt->pLock;
63069
63070 assert( sqlite3BtreeHoldsMutex(p) );
63071 assert( p->sharable || 0==*ppIter );
63072 assert( p->inTrans>0 );
63073
63074 while( *ppIter ){
63075 BtLock *pLock = *ppIter;
63076 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
63077 assert( pLock->pBtree->inTrans>=pLock->eLock );
63078 if( pLock->pBtree==p ){
63079 *ppIter = pLock->pNext;
63080 assert( pLock->iTable!=1 || pLock==&p->lock );
63081 if( pLock->iTable!=1 ){
63082 sqlite3_free(pLock);
63083 }
63084 }else{
63085 ppIter = &pLock->pNext;
63086 }
63087 }
63088
63089 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
63090 if( pBt->pWriter==p ){
63091 pBt->pWriter = 0;
63092 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
63093 }else if( pBt->nTransaction==2 ){
63094 /* This function is called when Btree p is concluding its
63095 ** transaction. If there currently exists a writer, and p is not
63096 ** that writer, then the number of locks held by connections other
63097 ** than the writer must be about to drop to zero. In this case
63098 ** set the BTS_PENDING flag to 0.
63099 **
63100 ** If there is not currently a writer, then BTS_PENDING must
63101 ** be zero already. So this next line is harmless in that case.
63102 */
63103 pBt->btsFlags &= ~BTS_PENDING;
63104 }
63105}
63106
63107/*
63108** This function changes all write-locks held by Btree p into read-locks.
63109*/
63110static void downgradeAllSharedCacheTableLocks(Btree *p){
63111 BtShared *pBt = p->pBt;
63112 if( pBt->pWriter==p ){
63113 BtLock *pLock;
63114 pBt->pWriter = 0;
63115 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
63116 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
63117 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
63118 pLock->eLock = READ_LOCK;
63119 }
63120 }
63121}
63122
63123#endif /* SQLITE_OMIT_SHARED_CACHE */
63124
63125static void releasePage(MemPage *pPage); /* Forward reference */
63126static void releasePageOne(MemPage *pPage); /* Forward reference */
63127static void releasePageNotNull(MemPage *pPage); /* Forward reference */
63128
63129/*
63130***** This routine is used inside of assert() only ****
63131**
63132** Verify that the cursor holds the mutex on its BtShared
63133*/
63134#ifdef SQLITE_DEBUG
63135static int cursorHoldsMutex(BtCursor *p){
63136 return sqlite3_mutex_held(p->pBt->mutex);
63137}
63138
63139/* Verify that the cursor and the BtShared agree about what is the current
63140** database connetion. This is important in shared-cache mode. If the database
63141** connection pointers get out-of-sync, it is possible for routines like
63142** btreeInitPage() to reference an stale connection pointer that references a
63143** a connection that has already closed. This routine is used inside assert()
63144** statements only and for the purpose of double-checking that the btree code
63145** does keep the database connection pointers up-to-date.
63146*/
63147static int cursorOwnsBtShared(BtCursor *p){
63148 assert( cursorHoldsMutex(p) );
63149 return (p->pBtree->db==p->pBt->db);
63150}
63151#endif
63152
63153/*
63154** Invalidate the overflow cache of the cursor passed as the first argument.
63155** on the shared btree structure pBt.
63156*/
63157#define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
63158
63159/*
63160** Invalidate the overflow page-list cache for all cursors opened
63161** on the shared btree structure pBt.
63162*/
63163static void invalidateAllOverflowCache(BtShared *pBt){
63164 BtCursor *p;
63165 assert( sqlite3_mutex_held(pBt->mutex) );
63166 for(p=pBt->pCursor; p; p=p->pNext){
63167 invalidateOverflowCache(p);
63168 }
63169}
63170
63171#ifndef SQLITE_OMIT_INCRBLOB
63172/*
63173** This function is called before modifying the contents of a table
63174** to invalidate any incrblob cursors that are open on the
63175** row or one of the rows being modified.
63176**
63177** If argument isClearTable is true, then the entire contents of the
63178** table is about to be deleted. In this case invalidate all incrblob
63179** cursors open on any row within the table with root-page pgnoRoot.
63180**
63181** Otherwise, if argument isClearTable is false, then the row with
63182** rowid iRow is being replaced or deleted. In this case invalidate
63183** only those incrblob cursors open on that specific row.
63184*/
63185static void invalidateIncrblobCursors(
63186 Btree *pBtree, /* The database file to check */
63187 Pgno pgnoRoot, /* The table that might be changing */
63188 i64 iRow, /* The rowid that might be changing */
63189 int isClearTable /* True if all rows are being deleted */
63190){
63191 BtCursor *p;
63192 if( pBtree->hasIncrblobCur==0 ) return;
63193 assert( sqlite3BtreeHoldsMutex(pBtree) );
63194 pBtree->hasIncrblobCur = 0;
63195 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
63196 if( (p->curFlags & BTCF_Incrblob)!=0 ){
63197 pBtree->hasIncrblobCur = 1;
63198 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
63199 p->eState = CURSOR_INVALID;
63200 }
63201 }
63202 }
63203}
63204
63205#else
63206 /* Stub function when INCRBLOB is omitted */
63207 #define invalidateIncrblobCursors(w,x,y,z)
63208#endif /* SQLITE_OMIT_INCRBLOB */
63209
63210/*
63211** Set bit pgno of the BtShared.pHasContent bitvec. This is called
63212** when a page that previously contained data becomes a free-list leaf
63213** page.
63214**
63215** The BtShared.pHasContent bitvec exists to work around an obscure
63216** bug caused by the interaction of two useful IO optimizations surrounding
63217** free-list leaf pages:
63218**
63219** 1) When all data is deleted from a page and the page becomes
63220** a free-list leaf page, the page is not written to the database
63221** (as free-list leaf pages contain no meaningful data). Sometimes
63222** such a page is not even journalled (as it will not be modified,
63223** why bother journalling it?).
63224**
63225** 2) When a free-list leaf page is reused, its content is not read
63226** from the database or written to the journal file (why should it
63227** be, if it is not at all meaningful?).
63228**
63229** By themselves, these optimizations work fine and provide a handy
63230** performance boost to bulk delete or insert operations. However, if
63231** a page is moved to the free-list and then reused within the same
63232** transaction, a problem comes up. If the page is not journalled when
63233** it is moved to the free-list and it is also not journalled when it
63234** is extracted from the free-list and reused, then the original data
63235** may be lost. In the event of a rollback, it may not be possible
63236** to restore the database to its original configuration.
63237**
63238** The solution is the BtShared.pHasContent bitvec. Whenever a page is
63239** moved to become a free-list leaf page, the corresponding bit is
63240** set in the bitvec. Whenever a leaf page is extracted from the free-list,
63241** optimization 2 above is omitted if the corresponding bit is already
63242** set in BtShared.pHasContent. The contents of the bitvec are cleared
63243** at the end of every transaction.
63244*/
63245static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
63246 int rc = SQLITE_OK;
63247 if( !pBt->pHasContent ){
63248 assert( pgno<=pBt->nPage );
63249 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
63250 if( !pBt->pHasContent ){
63251 rc = SQLITE_NOMEM_BKPT;
63252 }
63253 }
63254 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
63255 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
63256 }
63257 return rc;
63258}
63259
63260/*
63261** Query the BtShared.pHasContent vector.
63262**
63263** This function is called when a free-list leaf page is removed from the
63264** free-list for reuse. It returns false if it is safe to retrieve the
63265** page from the pager layer with the 'no-content' flag set. True otherwise.
63266*/
63267static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
63268 Bitvec *p = pBt->pHasContent;
63269 return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
63270}
63271
63272/*
63273** Clear (destroy) the BtShared.pHasContent bitvec. This should be
63274** invoked at the conclusion of each write-transaction.
63275*/
63276static void btreeClearHasContent(BtShared *pBt){
63277 sqlite3BitvecDestroy(pBt->pHasContent);
63278 pBt->pHasContent = 0;
63279}
63280
63281/*
63282** Release all of the apPage[] pages for a cursor.
63283*/
63284static void btreeReleaseAllCursorPages(BtCursor *pCur){
63285 int i;
63286 if( pCur->iPage>=0 ){
63287 for(i=0; i<pCur->iPage; i++){
63288 releasePageNotNull(pCur->apPage[i]);
63289 }
63290 releasePageNotNull(pCur->pPage);
63291 pCur->iPage = -1;
63292 }
63293}
63294
63295/*
63296** The cursor passed as the only argument must point to a valid entry
63297** when this function is called (i.e. have eState==CURSOR_VALID). This
63298** function saves the current cursor key in variables pCur->nKey and
63299** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
63300** code otherwise.
63301**
63302** If the cursor is open on an intkey table, then the integer key
63303** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
63304** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
63305** set to point to a malloced buffer pCur->nKey bytes in size containing
63306** the key.
63307*/
63308static int saveCursorKey(BtCursor *pCur){
63309 int rc = SQLITE_OK;
63310 assert( CURSOR_VALID==pCur->eState );
63311 assert( 0==pCur->pKey );
63312 assert( cursorHoldsMutex(pCur) );
63313
63314 if( pCur->curIntKey ){
63315 /* Only the rowid is required for a table btree */
63316 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
63317 }else{
63318 /* For an index btree, save the complete key content */
63319 void *pKey;
63320 pCur->nKey = sqlite3BtreePayloadSize(pCur);
63321 pKey = sqlite3Malloc( pCur->nKey );
63322 if( pKey ){
63323 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
63324 if( rc==SQLITE_OK ){
63325 pCur->pKey = pKey;
63326 }else{
63327 sqlite3_free(pKey);
63328 }
63329 }else{
63330 rc = SQLITE_NOMEM_BKPT;
63331 }
63332 }
63333 assert( !pCur->curIntKey || !pCur->pKey );
63334 return rc;
63335}
63336
63337/*
63338** Save the current cursor position in the variables BtCursor.nKey
63339** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
63340**
63341** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
63342** prior to calling this routine.
63343*/
63344static int saveCursorPosition(BtCursor *pCur){
63345 int rc;
63346
63347 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
63348 assert( 0==pCur->pKey );
63349 assert( cursorHoldsMutex(pCur) );
63350
63351 if( pCur->eState==CURSOR_SKIPNEXT ){
63352 pCur->eState = CURSOR_VALID;
63353 }else{
63354 pCur->skipNext = 0;
63355 }
63356
63357 rc = saveCursorKey(pCur);
63358 if( rc==SQLITE_OK ){
63359 btreeReleaseAllCursorPages(pCur);
63360 pCur->eState = CURSOR_REQUIRESEEK;
63361 }
63362
63363 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
63364 return rc;
63365}
63366
63367/* Forward reference */
63368static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
63369
63370/*
63371** Save the positions of all cursors (except pExcept) that are open on
63372** the table with root-page iRoot. "Saving the cursor position" means that
63373** the location in the btree is remembered in such a way that it can be
63374** moved back to the same spot after the btree has been modified. This
63375** routine is called just before cursor pExcept is used to modify the
63376** table, for example in BtreeDelete() or BtreeInsert().
63377**
63378** If there are two or more cursors on the same btree, then all such
63379** cursors should have their BTCF_Multiple flag set. The btreeCursor()
63380** routine enforces that rule. This routine only needs to be called in
63381** the uncommon case when pExpect has the BTCF_Multiple flag set.
63382**
63383** If pExpect!=NULL and if no other cursors are found on the same root-page,
63384** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
63385** pointless call to this routine.
63386**
63387** Implementation note: This routine merely checks to see if any cursors
63388** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
63389** event that cursors are in need to being saved.
63390*/
63391static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
63392 BtCursor *p;
63393 assert( sqlite3_mutex_held(pBt->mutex) );
63394 assert( pExcept==0 || pExcept->pBt==pBt );
63395 for(p=pBt->pCursor; p; p=p->pNext){
63396 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
63397 }
63398 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
63399 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
63400 return SQLITE_OK;
63401}
63402
63403/* This helper routine to saveAllCursors does the actual work of saving
63404** the cursors if and when a cursor is found that actually requires saving.
63405** The common case is that no cursors need to be saved, so this routine is
63406** broken out from its caller to avoid unnecessary stack pointer movement.
63407*/
63408static int SQLITE_NOINLINE saveCursorsOnList(
63409 BtCursor *p, /* The first cursor that needs saving */
63410 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */
63411 BtCursor *pExcept /* Do not save this cursor */
63412){
63413 do{
63414 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
63415 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
63416 int rc = saveCursorPosition(p);
63417 if( SQLITE_OK!=rc ){
63418 return rc;
63419 }
63420 }else{
63421 testcase( p->iPage>=0 );
63422 btreeReleaseAllCursorPages(p);
63423 }
63424 }
63425 p = p->pNext;
63426 }while( p );
63427 return SQLITE_OK;
63428}
63429
63430/*
63431** Clear the current cursor position.
63432*/
63433SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
63434 assert( cursorHoldsMutex(pCur) );
63435 sqlite3_free(pCur->pKey);
63436 pCur->pKey = 0;
63437 pCur->eState = CURSOR_INVALID;
63438}
63439
63440/*
63441** In this version of BtreeMoveto, pKey is a packed index record
63442** such as is generated by the OP_MakeRecord opcode. Unpack the
63443** record and then call BtreeMovetoUnpacked() to do the work.
63444*/
63445static int btreeMoveto(
63446 BtCursor *pCur, /* Cursor open on the btree to be searched */
63447 const void *pKey, /* Packed key if the btree is an index */
63448 i64 nKey, /* Integer key for tables. Size of pKey for indices */
63449 int bias, /* Bias search to the high end */
63450 int *pRes /* Write search results here */
63451){
63452 int rc; /* Status code */
63453 UnpackedRecord *pIdxKey; /* Unpacked index key */
63454
63455 if( pKey ){
63456 assert( nKey==(i64)(int)nKey );
63457 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pCur->pKeyInfo);
63458 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
63459 sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
63460 if( pIdxKey->nField==0 ){
63461 rc = SQLITE_CORRUPT_BKPT;
63462 goto moveto_done;
63463 }
63464 }else{
63465 pIdxKey = 0;
63466 }
63467 rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
63468moveto_done:
63469 if( pIdxKey ){
63470 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
63471 }
63472 return rc;
63473}
63474
63475/*
63476** Restore the cursor to the position it was in (or as close to as possible)
63477** when saveCursorPosition() was called. Note that this call deletes the
63478** saved position info stored by saveCursorPosition(), so there can be
63479** at most one effective restoreCursorPosition() call after each
63480** saveCursorPosition().
63481*/
63482static int btreeRestoreCursorPosition(BtCursor *pCur){
63483 int rc;
63484 int skipNext;
63485 assert( cursorOwnsBtShared(pCur) );
63486 assert( pCur->eState>=CURSOR_REQUIRESEEK );
63487 if( pCur->eState==CURSOR_FAULT ){
63488 return pCur->skipNext;
63489 }
63490 pCur->eState = CURSOR_INVALID;
63491 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
63492 if( rc==SQLITE_OK ){
63493 sqlite3_free(pCur->pKey);
63494 pCur->pKey = 0;
63495 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
63496 pCur->skipNext |= skipNext;
63497 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
63498 pCur->eState = CURSOR_SKIPNEXT;
63499 }
63500 }
63501 return rc;
63502}
63503
63504#define restoreCursorPosition(p) \
63505 (p->eState>=CURSOR_REQUIRESEEK ? \
63506 btreeRestoreCursorPosition(p) : \
63507 SQLITE_OK)
63508
63509/*
63510** Determine whether or not a cursor has moved from the position where
63511** it was last placed, or has been invalidated for any other reason.
63512** Cursors can move when the row they are pointing at is deleted out
63513** from under them, for example. Cursor might also move if a btree
63514** is rebalanced.
63515**
63516** Calling this routine with a NULL cursor pointer returns false.
63517**
63518** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
63519** back to where it ought to be if this routine returns true.
63520*/
63521SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
63522 assert( EIGHT_BYTE_ALIGNMENT(pCur)
63523 || pCur==sqlite3BtreeFakeValidCursor() );
63524 assert( offsetof(BtCursor, eState)==0 );
63525 assert( sizeof(pCur->eState)==1 );
63526 return CURSOR_VALID != *(u8*)pCur;
63527}
63528
63529/*
63530** Return a pointer to a fake BtCursor object that will always answer
63531** false to the sqlite3BtreeCursorHasMoved() routine above. The fake
63532** cursor returned must not be used with any other Btree interface.
63533*/
63534SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void){
63535 static u8 fakeCursor = CURSOR_VALID;
63536 assert( offsetof(BtCursor, eState)==0 );
63537 return (BtCursor*)&fakeCursor;
63538}
63539
63540/*
63541** This routine restores a cursor back to its original position after it
63542** has been moved by some outside activity (such as a btree rebalance or
63543** a row having been deleted out from under the cursor).
63544**
63545** On success, the *pDifferentRow parameter is false if the cursor is left
63546** pointing at exactly the same row. *pDifferntRow is the row the cursor
63547** was pointing to has been deleted, forcing the cursor to point to some
63548** nearby row.
63549**
63550** This routine should only be called for a cursor that just returned
63551** TRUE from sqlite3BtreeCursorHasMoved().
63552*/
63553SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
63554 int rc;
63555
63556 assert( pCur!=0 );
63557 assert( pCur->eState!=CURSOR_VALID );
63558 rc = restoreCursorPosition(pCur);
63559 if( rc ){
63560 *pDifferentRow = 1;
63561 return rc;
63562 }
63563 if( pCur->eState!=CURSOR_VALID ){
63564 *pDifferentRow = 1;
63565 }else{
63566 assert( pCur->skipNext==0 );
63567 *pDifferentRow = 0;
63568 }
63569 return SQLITE_OK;
63570}
63571
63572#ifdef SQLITE_ENABLE_CURSOR_HINTS
63573/*
63574** Provide hints to the cursor. The particular hint given (and the type
63575** and number of the varargs parameters) is determined by the eHintType
63576** parameter. See the definitions of the BTREE_HINT_* macros for details.
63577*/
63578SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
63579 /* Used only by system that substitute their own storage engine */
63580}
63581#endif
63582
63583/*
63584** Provide flag hints to the cursor.
63585*/
63586SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
63587 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
63588 pCur->hints = x;
63589}
63590
63591
63592#ifndef SQLITE_OMIT_AUTOVACUUM
63593/*
63594** Given a page number of a regular database page, return the page
63595** number for the pointer-map page that contains the entry for the
63596** input page number.
63597**
63598** Return 0 (not a valid page) for pgno==1 since there is
63599** no pointer map associated with page 1. The integrity_check logic
63600** requires that ptrmapPageno(*,1)!=1.
63601*/
63602static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
63603 int nPagesPerMapPage;
63604 Pgno iPtrMap, ret;
63605 assert( sqlite3_mutex_held(pBt->mutex) );
63606 if( pgno<2 ) return 0;
63607 nPagesPerMapPage = (pBt->usableSize/5)+1;
63608 iPtrMap = (pgno-2)/nPagesPerMapPage;
63609 ret = (iPtrMap*nPagesPerMapPage) + 2;
63610 if( ret==PENDING_BYTE_PAGE(pBt) ){
63611 ret++;
63612 }
63613 return ret;
63614}
63615
63616/*
63617** Write an entry into the pointer map.
63618**
63619** This routine updates the pointer map entry for page number 'key'
63620** so that it maps to type 'eType' and parent page number 'pgno'.
63621**
63622** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
63623** a no-op. If an error occurs, the appropriate error code is written
63624** into *pRC.
63625*/
63626static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
63627 DbPage *pDbPage; /* The pointer map page */
63628 u8 *pPtrmap; /* The pointer map data */
63629 Pgno iPtrmap; /* The pointer map page number */
63630 int offset; /* Offset in pointer map page */
63631 int rc; /* Return code from subfunctions */
63632
63633 if( *pRC ) return;
63634
63635 assert( sqlite3_mutex_held(pBt->mutex) );
63636 /* The master-journal page number must never be used as a pointer map page */
63637 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
63638
63639 assert( pBt->autoVacuum );
63640 if( key==0 ){
63641 *pRC = SQLITE_CORRUPT_BKPT;
63642 return;
63643 }
63644 iPtrmap = PTRMAP_PAGENO(pBt, key);
63645 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
63646 if( rc!=SQLITE_OK ){
63647 *pRC = rc;
63648 return;
63649 }
63650 offset = PTRMAP_PTROFFSET(iPtrmap, key);
63651 if( offset<0 ){
63652 *pRC = SQLITE_CORRUPT_BKPT;
63653 goto ptrmap_exit;
63654 }
63655 assert( offset <= (int)pBt->usableSize-5 );
63656 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
63657
63658 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
63659 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
63660 *pRC= rc = sqlite3PagerWrite(pDbPage);
63661 if( rc==SQLITE_OK ){
63662 pPtrmap[offset] = eType;
63663 put4byte(&pPtrmap[offset+1], parent);
63664 }
63665 }
63666
63667ptrmap_exit:
63668 sqlite3PagerUnref(pDbPage);
63669}
63670
63671/*
63672** Read an entry from the pointer map.
63673**
63674** This routine retrieves the pointer map entry for page 'key', writing
63675** the type and parent page number to *pEType and *pPgno respectively.
63676** An error code is returned if something goes wrong, otherwise SQLITE_OK.
63677*/
63678static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
63679 DbPage *pDbPage; /* The pointer map page */
63680 int iPtrmap; /* Pointer map page index */
63681 u8 *pPtrmap; /* Pointer map page data */
63682 int offset; /* Offset of entry in pointer map */
63683 int rc;
63684
63685 assert( sqlite3_mutex_held(pBt->mutex) );
63686
63687 iPtrmap = PTRMAP_PAGENO(pBt, key);
63688 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
63689 if( rc!=0 ){
63690 return rc;
63691 }
63692 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
63693
63694 offset = PTRMAP_PTROFFSET(iPtrmap, key);
63695 if( offset<0 ){
63696 sqlite3PagerUnref(pDbPage);
63697 return SQLITE_CORRUPT_BKPT;
63698 }
63699 assert( offset <= (int)pBt->usableSize-5 );
63700 assert( pEType!=0 );
63701 *pEType = pPtrmap[offset];
63702 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
63703
63704 sqlite3PagerUnref(pDbPage);
63705 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap);
63706 return SQLITE_OK;
63707}
63708
63709#else /* if defined SQLITE_OMIT_AUTOVACUUM */
63710 #define ptrmapPut(w,x,y,z,rc)
63711 #define ptrmapGet(w,x,y,z) SQLITE_OK
63712 #define ptrmapPutOvflPtr(x, y, rc)
63713#endif
63714
63715/*
63716** Given a btree page and a cell index (0 means the first cell on
63717** the page, 1 means the second cell, and so forth) return a pointer
63718** to the cell content.
63719**
63720** findCellPastPtr() does the same except it skips past the initial
63721** 4-byte child pointer found on interior pages, if there is one.
63722**
63723** This routine works only for pages that do not contain overflow cells.
63724*/
63725#define findCell(P,I) \
63726 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
63727#define findCellPastPtr(P,I) \
63728 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
63729
63730
63731/*
63732** This is common tail processing for btreeParseCellPtr() and
63733** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
63734** on a single B-tree page. Make necessary adjustments to the CellInfo
63735** structure.
63736*/
63737static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
63738 MemPage *pPage, /* Page containing the cell */
63739 u8 *pCell, /* Pointer to the cell text. */
63740 CellInfo *pInfo /* Fill in this structure */
63741){
63742 /* If the payload will not fit completely on the local page, we have
63743 ** to decide how much to store locally and how much to spill onto
63744 ** overflow pages. The strategy is to minimize the amount of unused
63745 ** space on overflow pages while keeping the amount of local storage
63746 ** in between minLocal and maxLocal.
63747 **
63748 ** Warning: changing the way overflow payload is distributed in any
63749 ** way will result in an incompatible file format.
63750 */
63751 int minLocal; /* Minimum amount of payload held locally */
63752 int maxLocal; /* Maximum amount of payload held locally */
63753 int surplus; /* Overflow payload available for local storage */
63754
63755 minLocal = pPage->minLocal;
63756 maxLocal = pPage->maxLocal;
63757 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
63758 testcase( surplus==maxLocal );
63759 testcase( surplus==maxLocal+1 );
63760 if( surplus <= maxLocal ){
63761 pInfo->nLocal = (u16)surplus;
63762 }else{
63763 pInfo->nLocal = (u16)minLocal;
63764 }
63765 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
63766}
63767
63768/*
63769** The following routines are implementations of the MemPage.xParseCell()
63770** method.
63771**
63772** Parse a cell content block and fill in the CellInfo structure.
63773**
63774** btreeParseCellPtr() => table btree leaf nodes
63775** btreeParseCellNoPayload() => table btree internal nodes
63776** btreeParseCellPtrIndex() => index btree nodes
63777**
63778** There is also a wrapper function btreeParseCell() that works for
63779** all MemPage types and that references the cell by index rather than
63780** by pointer.
63781*/
63782static void btreeParseCellPtrNoPayload(
63783 MemPage *pPage, /* Page containing the cell */
63784 u8 *pCell, /* Pointer to the cell text. */
63785 CellInfo *pInfo /* Fill in this structure */
63786){
63787 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
63788 assert( pPage->leaf==0 );
63789 assert( pPage->childPtrSize==4 );
63790#ifndef SQLITE_DEBUG
63791 UNUSED_PARAMETER(pPage);
63792#endif
63793 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
63794 pInfo->nPayload = 0;
63795 pInfo->nLocal = 0;
63796 pInfo->pPayload = 0;
63797 return;
63798}
63799static void btreeParseCellPtr(
63800 MemPage *pPage, /* Page containing the cell */
63801 u8 *pCell, /* Pointer to the cell text. */
63802 CellInfo *pInfo /* Fill in this structure */
63803){
63804 u8 *pIter; /* For scanning through pCell */
63805 u32 nPayload; /* Number of bytes of cell payload */
63806 u64 iKey; /* Extracted Key value */
63807
63808 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
63809 assert( pPage->leaf==0 || pPage->leaf==1 );
63810 assert( pPage->intKeyLeaf );
63811 assert( pPage->childPtrSize==0 );
63812 pIter = pCell;
63813
63814 /* The next block of code is equivalent to:
63815 **
63816 ** pIter += getVarint32(pIter, nPayload);
63817 **
63818 ** The code is inlined to avoid a function call.
63819 */
63820 nPayload = *pIter;
63821 if( nPayload>=0x80 ){
63822 u8 *pEnd = &pIter[8];
63823 nPayload &= 0x7f;
63824 do{
63825 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
63826 }while( (*pIter)>=0x80 && pIter<pEnd );
63827 }
63828 pIter++;
63829
63830 /* The next block of code is equivalent to:
63831 **
63832 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
63833 **
63834 ** The code is inlined to avoid a function call.
63835 */
63836 iKey = *pIter;
63837 if( iKey>=0x80 ){
63838 u8 *pEnd = &pIter[7];
63839 iKey &= 0x7f;
63840 while(1){
63841 iKey = (iKey<<7) | (*++pIter & 0x7f);
63842 if( (*pIter)<0x80 ) break;
63843 if( pIter>=pEnd ){
63844 iKey = (iKey<<8) | *++pIter;
63845 break;
63846 }
63847 }
63848 }
63849 pIter++;
63850
63851 pInfo->nKey = *(i64*)&iKey;
63852 pInfo->nPayload = nPayload;
63853 pInfo->pPayload = pIter;
63854 testcase( nPayload==pPage->maxLocal );
63855 testcase( nPayload==pPage->maxLocal+1 );
63856 if( nPayload<=pPage->maxLocal ){
63857 /* This is the (easy) common case where the entire payload fits
63858 ** on the local page. No overflow is required.
63859 */
63860 pInfo->nSize = nPayload + (u16)(pIter - pCell);
63861 if( pInfo->nSize<4 ) pInfo->nSize = 4;
63862 pInfo->nLocal = (u16)nPayload;
63863 }else{
63864 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
63865 }
63866}
63867static void btreeParseCellPtrIndex(
63868 MemPage *pPage, /* Page containing the cell */
63869 u8 *pCell, /* Pointer to the cell text. */
63870 CellInfo *pInfo /* Fill in this structure */
63871){
63872 u8 *pIter; /* For scanning through pCell */
63873 u32 nPayload; /* Number of bytes of cell payload */
63874
63875 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
63876 assert( pPage->leaf==0 || pPage->leaf==1 );
63877 assert( pPage->intKeyLeaf==0 );
63878 pIter = pCell + pPage->childPtrSize;
63879 nPayload = *pIter;
63880 if( nPayload>=0x80 ){
63881 u8 *pEnd = &pIter[8];
63882 nPayload &= 0x7f;
63883 do{
63884 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
63885 }while( *(pIter)>=0x80 && pIter<pEnd );
63886 }
63887 pIter++;
63888 pInfo->nKey = nPayload;
63889 pInfo->nPayload = nPayload;
63890 pInfo->pPayload = pIter;
63891 testcase( nPayload==pPage->maxLocal );
63892 testcase( nPayload==pPage->maxLocal+1 );
63893 if( nPayload<=pPage->maxLocal ){
63894 /* This is the (easy) common case where the entire payload fits
63895 ** on the local page. No overflow is required.
63896 */
63897 pInfo->nSize = nPayload + (u16)(pIter - pCell);
63898 if( pInfo->nSize<4 ) pInfo->nSize = 4;
63899 pInfo->nLocal = (u16)nPayload;
63900 }else{
63901 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
63902 }
63903}
63904static void btreeParseCell(
63905 MemPage *pPage, /* Page containing the cell */
63906 int iCell, /* The cell index. First cell is 0 */
63907 CellInfo *pInfo /* Fill in this structure */
63908){
63909 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
63910}
63911
63912/*
63913** The following routines are implementations of the MemPage.xCellSize
63914** method.
63915**
63916** Compute the total number of bytes that a Cell needs in the cell
63917** data area of the btree-page. The return number includes the cell
63918** data header and the local payload, but not any overflow page or
63919** the space used by the cell pointer.
63920**
63921** cellSizePtrNoPayload() => table internal nodes
63922** cellSizePtr() => all index nodes & table leaf nodes
63923*/
63924static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
63925 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
63926 u8 *pEnd; /* End mark for a varint */
63927 u32 nSize; /* Size value to return */
63928
63929#ifdef SQLITE_DEBUG
63930 /* The value returned by this function should always be the same as
63931 ** the (CellInfo.nSize) value found by doing a full parse of the
63932 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
63933 ** this function verifies that this invariant is not violated. */
63934 CellInfo debuginfo;
63935 pPage->xParseCell(pPage, pCell, &debuginfo);
63936#endif
63937
63938 nSize = *pIter;
63939 if( nSize>=0x80 ){
63940 pEnd = &pIter[8];
63941 nSize &= 0x7f;
63942 do{
63943 nSize = (nSize<<7) | (*++pIter & 0x7f);
63944 }while( *(pIter)>=0x80 && pIter<pEnd );
63945 }
63946 pIter++;
63947 if( pPage->intKey ){
63948 /* pIter now points at the 64-bit integer key value, a variable length
63949 ** integer. The following block moves pIter to point at the first byte
63950 ** past the end of the key value. */
63951 pEnd = &pIter[9];
63952 while( (*pIter++)&0x80 && pIter<pEnd );
63953 }
63954 testcase( nSize==pPage->maxLocal );
63955 testcase( nSize==pPage->maxLocal+1 );
63956 if( nSize<=pPage->maxLocal ){
63957 nSize += (u32)(pIter - pCell);
63958 if( nSize<4 ) nSize = 4;
63959 }else{
63960 int minLocal = pPage->minLocal;
63961 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
63962 testcase( nSize==pPage->maxLocal );
63963 testcase( nSize==pPage->maxLocal+1 );
63964 if( nSize>pPage->maxLocal ){
63965 nSize = minLocal;
63966 }
63967 nSize += 4 + (u16)(pIter - pCell);
63968 }
63969 assert( nSize==debuginfo.nSize || CORRUPT_DB );
63970 return (u16)nSize;
63971}
63972static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
63973 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
63974 u8 *pEnd; /* End mark for a varint */
63975
63976#ifdef SQLITE_DEBUG
63977 /* The value returned by this function should always be the same as
63978 ** the (CellInfo.nSize) value found by doing a full parse of the
63979 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
63980 ** this function verifies that this invariant is not violated. */
63981 CellInfo debuginfo;
63982 pPage->xParseCell(pPage, pCell, &debuginfo);
63983#else
63984 UNUSED_PARAMETER(pPage);
63985#endif
63986
63987 assert( pPage->childPtrSize==4 );
63988 pEnd = pIter + 9;
63989 while( (*pIter++)&0x80 && pIter<pEnd );
63990 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
63991 return (u16)(pIter - pCell);
63992}
63993
63994
63995#ifdef SQLITE_DEBUG
63996/* This variation on cellSizePtr() is used inside of assert() statements
63997** only. */
63998static u16 cellSize(MemPage *pPage, int iCell){
63999 return pPage->xCellSize(pPage, findCell(pPage, iCell));
64000}
64001#endif
64002
64003#ifndef SQLITE_OMIT_AUTOVACUUM
64004/*
64005** If the cell pCell, part of page pPage contains a pointer
64006** to an overflow page, insert an entry into the pointer-map
64007** for the overflow page.
64008*/
64009static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
64010 CellInfo info;
64011 if( *pRC ) return;
64012 assert( pCell!=0 );
64013 pPage->xParseCell(pPage, pCell, &info);
64014 if( info.nLocal<info.nPayload ){
64015 Pgno ovfl = get4byte(&pCell[info.nSize-4]);
64016 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
64017 }
64018}
64019#endif
64020
64021
64022/*
64023** Defragment the page given. This routine reorganizes cells within the
64024** page so that there are no free-blocks on the free-block list.
64025**
64026** Parameter nMaxFrag is the maximum amount of fragmented space that may be
64027** present in the page after this routine returns.
64028**
64029** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
64030** b-tree page so that there are no freeblocks or fragment bytes, all
64031** unused bytes are contained in the unallocated space region, and all
64032** cells are packed tightly at the end of the page.
64033*/
64034static int defragmentPage(MemPage *pPage, int nMaxFrag){
64035 int i; /* Loop counter */
64036 int pc; /* Address of the i-th cell */
64037 int hdr; /* Offset to the page header */
64038 int size; /* Size of a cell */
64039 int usableSize; /* Number of usable bytes on a page */
64040 int cellOffset; /* Offset to the cell pointer array */
64041 int cbrk; /* Offset to the cell content area */
64042 int nCell; /* Number of cells on the page */
64043 unsigned char *data; /* The page data */
64044 unsigned char *temp; /* Temp area for cell content */
64045 unsigned char *src; /* Source of content */
64046 int iCellFirst; /* First allowable cell index */
64047 int iCellLast; /* Last possible cell index */
64048
64049 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
64050 assert( pPage->pBt!=0 );
64051 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
64052 assert( pPage->nOverflow==0 );
64053 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64054 temp = 0;
64055 src = data = pPage->aData;
64056 hdr = pPage->hdrOffset;
64057 cellOffset = pPage->cellOffset;
64058 nCell = pPage->nCell;
64059 assert( nCell==get2byte(&data[hdr+3]) );
64060 iCellFirst = cellOffset + 2*nCell;
64061 usableSize = pPage->pBt->usableSize;
64062
64063 /* This block handles pages with two or fewer free blocks and nMaxFrag
64064 ** or fewer fragmented bytes. In this case it is faster to move the
64065 ** two (or one) blocks of cells using memmove() and add the required
64066 ** offsets to each pointer in the cell-pointer array than it is to
64067 ** reconstruct the entire page. */
64068 if( (int)data[hdr+7]<=nMaxFrag ){
64069 int iFree = get2byte(&data[hdr+1]);
64070 if( iFree ){
64071 int iFree2 = get2byte(&data[iFree]);
64072
64073 /* pageFindSlot() has already verified that free blocks are sorted
64074 ** in order of offset within the page, and that no block extends
64075 ** past the end of the page. Provided the two free slots do not
64076 ** overlap, this guarantees that the memmove() calls below will not
64077 ** overwrite the usableSize byte buffer, even if the database page
64078 ** is corrupt. */
64079 assert( iFree2==0 || iFree2>iFree );
64080 assert( iFree+get2byte(&data[iFree+2]) <= usableSize );
64081 assert( iFree2==0 || iFree2+get2byte(&data[iFree2+2]) <= usableSize );
64082
64083 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
64084 u8 *pEnd = &data[cellOffset + nCell*2];
64085 u8 *pAddr;
64086 int sz2 = 0;
64087 int sz = get2byte(&data[iFree+2]);
64088 int top = get2byte(&data[hdr+5]);
64089 if( top>=iFree ){
64090 return SQLITE_CORRUPT_PAGE(pPage);
64091 }
64092 if( iFree2 ){
64093 assert( iFree+sz<=iFree2 ); /* Verified by pageFindSlot() */
64094 sz2 = get2byte(&data[iFree2+2]);
64095 assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
64096 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
64097 sz += sz2;
64098 }
64099 cbrk = top+sz;
64100 assert( cbrk+(iFree-top) <= usableSize );
64101 memmove(&data[cbrk], &data[top], iFree-top);
64102 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
64103 pc = get2byte(pAddr);
64104 if( pc<iFree ){ put2byte(pAddr, pc+sz); }
64105 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
64106 }
64107 goto defragment_out;
64108 }
64109 }
64110 }
64111
64112 cbrk = usableSize;
64113 iCellLast = usableSize - 4;
64114 for(i=0; i<nCell; i++){
64115 u8 *pAddr; /* The i-th cell pointer */
64116 pAddr = &data[cellOffset + i*2];
64117 pc = get2byte(pAddr);
64118 testcase( pc==iCellFirst );
64119 testcase( pc==iCellLast );
64120 /* These conditions have already been verified in btreeInitPage()
64121 ** if PRAGMA cell_size_check=ON.
64122 */
64123 if( pc<iCellFirst || pc>iCellLast ){
64124 return SQLITE_CORRUPT_PAGE(pPage);
64125 }
64126 assert( pc>=iCellFirst && pc<=iCellLast );
64127 size = pPage->xCellSize(pPage, &src[pc]);
64128 cbrk -= size;
64129 if( cbrk<iCellFirst || pc+size>usableSize ){
64130 return SQLITE_CORRUPT_PAGE(pPage);
64131 }
64132 assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
64133 testcase( cbrk+size==usableSize );
64134 testcase( pc+size==usableSize );
64135 put2byte(pAddr, cbrk);
64136 if( temp==0 ){
64137 int x;
64138 if( cbrk==pc ) continue;
64139 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
64140 x = get2byte(&data[hdr+5]);
64141 memcpy(&temp[x], &data[x], (cbrk+size) - x);
64142 src = temp;
64143 }
64144 memcpy(&data[cbrk], &src[pc], size);
64145 }
64146 data[hdr+7] = 0;
64147
64148 defragment_out:
64149 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
64150 return SQLITE_CORRUPT_PAGE(pPage);
64151 }
64152 assert( cbrk>=iCellFirst );
64153 put2byte(&data[hdr+5], cbrk);
64154 data[hdr+1] = 0;
64155 data[hdr+2] = 0;
64156 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
64157 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
64158 return SQLITE_OK;
64159}
64160
64161/*
64162** Search the free-list on page pPg for space to store a cell nByte bytes in
64163** size. If one can be found, return a pointer to the space and remove it
64164** from the free-list.
64165**
64166** If no suitable space can be found on the free-list, return NULL.
64167**
64168** This function may detect corruption within pPg. If corruption is
64169** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
64170**
64171** Slots on the free list that are between 1 and 3 bytes larger than nByte
64172** will be ignored if adding the extra space to the fragmentation count
64173** causes the fragmentation count to exceed 60.
64174*/
64175static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
64176 const int hdr = pPg->hdrOffset;
64177 u8 * const aData = pPg->aData;
64178 int iAddr = hdr + 1;
64179 int pc = get2byte(&aData[iAddr]);
64180 int x;
64181 int usableSize = pPg->pBt->usableSize;
64182 int size; /* Size of the free slot */
64183
64184 assert( pc>0 );
64185 while( pc<=usableSize-4 ){
64186 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
64187 ** freeblock form a big-endian integer which is the size of the freeblock
64188 ** in bytes, including the 4-byte header. */
64189 size = get2byte(&aData[pc+2]);
64190 if( (x = size - nByte)>=0 ){
64191 testcase( x==4 );
64192 testcase( x==3 );
64193 if( size+pc > usableSize ){
64194 *pRc = SQLITE_CORRUPT_PAGE(pPg);
64195 return 0;
64196 }else if( x<4 ){
64197 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
64198 ** number of bytes in fragments may not exceed 60. */
64199 if( aData[hdr+7]>57 ) return 0;
64200
64201 /* Remove the slot from the free-list. Update the number of
64202 ** fragmented bytes within the page. */
64203 memcpy(&aData[iAddr], &aData[pc], 2);
64204 aData[hdr+7] += (u8)x;
64205 }else{
64206 /* The slot remains on the free-list. Reduce its size to account
64207 ** for the portion used by the new allocation. */
64208 put2byte(&aData[pc+2], x);
64209 }
64210 return &aData[pc + x];
64211 }
64212 iAddr = pc;
64213 pc = get2byte(&aData[pc]);
64214 if( pc<iAddr+size ) break;
64215 }
64216 if( pc ){
64217 *pRc = SQLITE_CORRUPT_PAGE(pPg);
64218 }
64219
64220 return 0;
64221}
64222
64223/*
64224** Allocate nByte bytes of space from within the B-Tree page passed
64225** as the first argument. Write into *pIdx the index into pPage->aData[]
64226** of the first byte of allocated space. Return either SQLITE_OK or
64227** an error code (usually SQLITE_CORRUPT).
64228**
64229** The caller guarantees that there is sufficient space to make the
64230** allocation. This routine might need to defragment in order to bring
64231** all the space together, however. This routine will avoid using
64232** the first two bytes past the cell pointer area since presumably this
64233** allocation is being made in order to insert a new cell, so we will
64234** also end up needing a new cell pointer.
64235*/
64236static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
64237 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
64238 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
64239 int top; /* First byte of cell content area */
64240 int rc = SQLITE_OK; /* Integer return code */
64241 int gap; /* First byte of gap between cell pointers and cell content */
64242
64243 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
64244 assert( pPage->pBt );
64245 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64246 assert( nByte>=0 ); /* Minimum cell size is 4 */
64247 assert( pPage->nFree>=nByte );
64248 assert( pPage->nOverflow==0 );
64249 assert( nByte < (int)(pPage->pBt->usableSize-8) );
64250
64251 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
64252 gap = pPage->cellOffset + 2*pPage->nCell;
64253 assert( gap<=65536 );
64254 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
64255 ** and the reserved space is zero (the usual value for reserved space)
64256 ** then the cell content offset of an empty page wants to be 65536.
64257 ** However, that integer is too large to be stored in a 2-byte unsigned
64258 ** integer, so a value of 0 is used in its place. */
64259 top = get2byte(&data[hdr+5]);
64260 assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
64261 if( gap>top ){
64262 if( top==0 && pPage->pBt->usableSize==65536 ){
64263 top = 65536;
64264 }else{
64265 return SQLITE_CORRUPT_PAGE(pPage);
64266 }
64267 }
64268
64269 /* If there is enough space between gap and top for one more cell pointer
64270 ** array entry offset, and if the freelist is not empty, then search the
64271 ** freelist looking for a free slot big enough to satisfy the request.
64272 */
64273 testcase( gap+2==top );
64274 testcase( gap+1==top );
64275 testcase( gap==top );
64276 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
64277 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
64278 if( pSpace ){
64279 assert( pSpace>=data && (pSpace - data)<65536 );
64280 *pIdx = (int)(pSpace - data);
64281 return SQLITE_OK;
64282 }else if( rc ){
64283 return rc;
64284 }
64285 }
64286
64287 /* The request could not be fulfilled using a freelist slot. Check
64288 ** to see if defragmentation is necessary.
64289 */
64290 testcase( gap+2+nByte==top );
64291 if( gap+2+nByte>top ){
64292 assert( pPage->nCell>0 || CORRUPT_DB );
64293 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
64294 if( rc ) return rc;
64295 top = get2byteNotZero(&data[hdr+5]);
64296 assert( gap+2+nByte<=top );
64297 }
64298
64299
64300 /* Allocate memory from the gap in between the cell pointer array
64301 ** and the cell content area. The btreeInitPage() call has already
64302 ** validated the freelist. Given that the freelist is valid, there
64303 ** is no way that the allocation can extend off the end of the page.
64304 ** The assert() below verifies the previous sentence.
64305 */
64306 top -= nByte;
64307 put2byte(&data[hdr+5], top);
64308 assert( top+nByte <= (int)pPage->pBt->usableSize );
64309 *pIdx = top;
64310 return SQLITE_OK;
64311}
64312
64313/*
64314** Return a section of the pPage->aData to the freelist.
64315** The first byte of the new free block is pPage->aData[iStart]
64316** and the size of the block is iSize bytes.
64317**
64318** Adjacent freeblocks are coalesced.
64319**
64320** Note that even though the freeblock list was checked by btreeInitPage(),
64321** that routine will not detect overlap between cells or freeblocks. Nor
64322** does it detect cells or freeblocks that encrouch into the reserved bytes
64323** at the end of the page. So do additional corruption checks inside this
64324** routine and return SQLITE_CORRUPT if any problems are found.
64325*/
64326static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
64327 u16 iPtr; /* Address of ptr to next freeblock */
64328 u16 iFreeBlk; /* Address of the next freeblock */
64329 u8 hdr; /* Page header size. 0 or 100 */
64330 u8 nFrag = 0; /* Reduction in fragmentation */
64331 u16 iOrigSize = iSize; /* Original value of iSize */
64332 u16 x; /* Offset to cell content area */
64333 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
64334 unsigned char *data = pPage->aData; /* Page content */
64335
64336 assert( pPage->pBt!=0 );
64337 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
64338 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
64339 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
64340 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64341 assert( iSize>=4 ); /* Minimum cell size is 4 */
64342 assert( iStart<=pPage->pBt->usableSize-4 );
64343
64344 /* The list of freeblocks must be in ascending order. Find the
64345 ** spot on the list where iStart should be inserted.
64346 */
64347 hdr = pPage->hdrOffset;
64348 iPtr = hdr + 1;
64349 if( data[iPtr+1]==0 && data[iPtr]==0 ){
64350 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
64351 }else{
64352 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
64353 if( iFreeBlk<iPtr+4 ){
64354 if( iFreeBlk==0 ) break;
64355 return SQLITE_CORRUPT_PAGE(pPage);
64356 }
64357 iPtr = iFreeBlk;
64358 }
64359 if( iFreeBlk>pPage->pBt->usableSize-4 ){
64360 return SQLITE_CORRUPT_PAGE(pPage);
64361 }
64362 assert( iFreeBlk>iPtr || iFreeBlk==0 );
64363
64364 /* At this point:
64365 ** iFreeBlk: First freeblock after iStart, or zero if none
64366 ** iPtr: The address of a pointer to iFreeBlk
64367 **
64368 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
64369 */
64370 if( iFreeBlk && iEnd+3>=iFreeBlk ){
64371 nFrag = iFreeBlk - iEnd;
64372 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
64373 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
64374 if( iEnd > pPage->pBt->usableSize ){
64375 return SQLITE_CORRUPT_PAGE(pPage);
64376 }
64377 iSize = iEnd - iStart;
64378 iFreeBlk = get2byte(&data[iFreeBlk]);
64379 }
64380
64381 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
64382 ** pointer in the page header) then check to see if iStart should be
64383 ** coalesced onto the end of iPtr.
64384 */
64385 if( iPtr>hdr+1 ){
64386 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
64387 if( iPtrEnd+3>=iStart ){
64388 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
64389 nFrag += iStart - iPtrEnd;
64390 iSize = iEnd - iPtr;
64391 iStart = iPtr;
64392 }
64393 }
64394 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
64395 data[hdr+7] -= nFrag;
64396 }
64397 x = get2byte(&data[hdr+5]);
64398 if( iStart<=x ){
64399 /* The new freeblock is at the beginning of the cell content area,
64400 ** so just extend the cell content area rather than create another
64401 ** freelist entry */
64402 if( iStart<x || iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
64403 put2byte(&data[hdr+1], iFreeBlk);
64404 put2byte(&data[hdr+5], iEnd);
64405 }else{
64406 /* Insert the new freeblock into the freelist */
64407 put2byte(&data[iPtr], iStart);
64408 }
64409 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
64410 /* Overwrite deleted information with zeros when the secure_delete
64411 ** option is enabled */
64412 memset(&data[iStart], 0, iSize);
64413 }
64414 put2byte(&data[iStart], iFreeBlk);
64415 put2byte(&data[iStart+2], iSize);
64416 pPage->nFree += iOrigSize;
64417 return SQLITE_OK;
64418}
64419
64420/*
64421** Decode the flags byte (the first byte of the header) for a page
64422** and initialize fields of the MemPage structure accordingly.
64423**
64424** Only the following combinations are supported. Anything different
64425** indicates a corrupt database files:
64426**
64427** PTF_ZERODATA
64428** PTF_ZERODATA | PTF_LEAF
64429** PTF_LEAFDATA | PTF_INTKEY
64430** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
64431*/
64432static int decodeFlags(MemPage *pPage, int flagByte){
64433 BtShared *pBt; /* A copy of pPage->pBt */
64434
64435 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
64436 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64437 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
64438 flagByte &= ~PTF_LEAF;
64439 pPage->childPtrSize = 4-4*pPage->leaf;
64440 pPage->xCellSize = cellSizePtr;
64441 pBt = pPage->pBt;
64442 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
64443 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
64444 ** interior table b-tree page. */
64445 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
64446 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
64447 ** leaf table b-tree page. */
64448 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
64449 pPage->intKey = 1;
64450 if( pPage->leaf ){
64451 pPage->intKeyLeaf = 1;
64452 pPage->xParseCell = btreeParseCellPtr;
64453 }else{
64454 pPage->intKeyLeaf = 0;
64455 pPage->xCellSize = cellSizePtrNoPayload;
64456 pPage->xParseCell = btreeParseCellPtrNoPayload;
64457 }
64458 pPage->maxLocal = pBt->maxLeaf;
64459 pPage->minLocal = pBt->minLeaf;
64460 }else if( flagByte==PTF_ZERODATA ){
64461 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
64462 ** interior index b-tree page. */
64463 assert( (PTF_ZERODATA)==2 );
64464 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
64465 ** leaf index b-tree page. */
64466 assert( (PTF_ZERODATA|PTF_LEAF)==10 );
64467 pPage->intKey = 0;
64468 pPage->intKeyLeaf = 0;
64469 pPage->xParseCell = btreeParseCellPtrIndex;
64470 pPage->maxLocal = pBt->maxLocal;
64471 pPage->minLocal = pBt->minLocal;
64472 }else{
64473 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
64474 ** an error. */
64475 return SQLITE_CORRUPT_PAGE(pPage);
64476 }
64477 pPage->max1bytePayload = pBt->max1bytePayload;
64478 return SQLITE_OK;
64479}
64480
64481/*
64482** Initialize the auxiliary information for a disk block.
64483**
64484** Return SQLITE_OK on success. If we see that the page does
64485** not contain a well-formed database page, then return
64486** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
64487** guarantee that the page is well-formed. It only shows that
64488** we failed to detect any corruption.
64489*/
64490static int btreeInitPage(MemPage *pPage){
64491 int pc; /* Address of a freeblock within pPage->aData[] */
64492 u8 hdr; /* Offset to beginning of page header */
64493 u8 *data; /* Equal to pPage->aData */
64494 BtShared *pBt; /* The main btree structure */
64495 int usableSize; /* Amount of usable space on each page */
64496 u16 cellOffset; /* Offset from start of page to first cell pointer */
64497 int nFree; /* Number of unused bytes on the page */
64498 int top; /* First byte of the cell content area */
64499 int iCellFirst; /* First allowable cell or freeblock offset */
64500 int iCellLast; /* Last possible cell or freeblock offset */
64501
64502 assert( pPage->pBt!=0 );
64503 assert( pPage->pBt->db!=0 );
64504 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64505 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
64506 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
64507 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
64508 assert( pPage->isInit==0 );
64509
64510 pBt = pPage->pBt;
64511 hdr = pPage->hdrOffset;
64512 data = pPage->aData;
64513 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
64514 ** the b-tree page type. */
64515 if( decodeFlags(pPage, data[hdr]) ){
64516 return SQLITE_CORRUPT_PAGE(pPage);
64517 }
64518 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
64519 pPage->maskPage = (u16)(pBt->pageSize - 1);
64520 pPage->nOverflow = 0;
64521 usableSize = pBt->usableSize;
64522 pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
64523 pPage->aDataEnd = &data[usableSize];
64524 pPage->aCellIdx = &data[cellOffset];
64525 pPage->aDataOfst = &data[pPage->childPtrSize];
64526 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
64527 ** the start of the cell content area. A zero value for this integer is
64528 ** interpreted as 65536. */
64529 top = get2byteNotZero(&data[hdr+5]);
64530 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
64531 ** number of cells on the page. */
64532 pPage->nCell = get2byte(&data[hdr+3]);
64533 if( pPage->nCell>MX_CELL(pBt) ){
64534 /* To many cells for a single page. The page must be corrupt */
64535 return SQLITE_CORRUPT_PAGE(pPage);
64536 }
64537 testcase( pPage->nCell==MX_CELL(pBt) );
64538 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
64539 ** possible for a root page of a table that contains no rows) then the
64540 ** offset to the cell content area will equal the page size minus the
64541 ** bytes of reserved space. */
64542 assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
64543
64544 /* A malformed database page might cause us to read past the end
64545 ** of page when parsing a cell.
64546 **
64547 ** The following block of code checks early to see if a cell extends
64548 ** past the end of a page boundary and causes SQLITE_CORRUPT to be
64549 ** returned if it does.
64550 */
64551 iCellFirst = cellOffset + 2*pPage->nCell;
64552 iCellLast = usableSize - 4;
64553 if( pBt->db->flags & SQLITE_CellSizeCk ){
64554 int i; /* Index into the cell pointer array */
64555 int sz; /* Size of a cell */
64556
64557 if( !pPage->leaf ) iCellLast--;
64558 for(i=0; i<pPage->nCell; i++){
64559 pc = get2byteAligned(&data[cellOffset+i*2]);
64560 testcase( pc==iCellFirst );
64561 testcase( pc==iCellLast );
64562 if( pc<iCellFirst || pc>iCellLast ){
64563 return SQLITE_CORRUPT_PAGE(pPage);
64564 }
64565 sz = pPage->xCellSize(pPage, &data[pc]);
64566 testcase( pc+sz==usableSize );
64567 if( pc+sz>usableSize ){
64568 return SQLITE_CORRUPT_PAGE(pPage);
64569 }
64570 }
64571 if( !pPage->leaf ) iCellLast++;
64572 }
64573
64574 /* Compute the total free space on the page
64575 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
64576 ** start of the first freeblock on the page, or is zero if there are no
64577 ** freeblocks. */
64578 pc = get2byte(&data[hdr+1]);
64579 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
64580 if( pc>0 ){
64581 u32 next, size;
64582 if( pc<iCellFirst ){
64583 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
64584 ** always be at least one cell before the first freeblock.
64585 */
64586 return SQLITE_CORRUPT_PAGE(pPage);
64587 }
64588 while( 1 ){
64589 if( pc>iCellLast ){
64590 /* Freeblock off the end of the page */
64591 return SQLITE_CORRUPT_PAGE(pPage);
64592 }
64593 next = get2byte(&data[pc]);
64594 size = get2byte(&data[pc+2]);
64595 nFree = nFree + size;
64596 if( next<=pc+size+3 ) break;
64597 pc = next;
64598 }
64599 if( next>0 ){
64600 /* Freeblock not in ascending order */
64601 return SQLITE_CORRUPT_PAGE(pPage);
64602 }
64603 if( pc+size>(unsigned int)usableSize ){
64604 /* Last freeblock extends past page end */
64605 return SQLITE_CORRUPT_PAGE(pPage);
64606 }
64607 }
64608
64609 /* At this point, nFree contains the sum of the offset to the start
64610 ** of the cell-content area plus the number of free bytes within
64611 ** the cell-content area. If this is greater than the usable-size
64612 ** of the page, then the page must be corrupted. This check also
64613 ** serves to verify that the offset to the start of the cell-content
64614 ** area, according to the page header, lies within the page.
64615 */
64616 if( nFree>usableSize ){
64617 return SQLITE_CORRUPT_PAGE(pPage);
64618 }
64619 pPage->nFree = (u16)(nFree - iCellFirst);
64620 pPage->isInit = 1;
64621 return SQLITE_OK;
64622}
64623
64624/*
64625** Set up a raw page so that it looks like a database page holding
64626** no entries.
64627*/
64628static void zeroPage(MemPage *pPage, int flags){
64629 unsigned char *data = pPage->aData;
64630 BtShared *pBt = pPage->pBt;
64631 u8 hdr = pPage->hdrOffset;
64632 u16 first;
64633
64634 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
64635 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
64636 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
64637 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
64638 assert( sqlite3_mutex_held(pBt->mutex) );
64639 if( pBt->btsFlags & BTS_FAST_SECURE ){
64640 memset(&data[hdr], 0, pBt->usableSize - hdr);
64641 }
64642 data[hdr] = (char)flags;
64643 first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
64644 memset(&data[hdr+1], 0, 4);
64645 data[hdr+7] = 0;
64646 put2byte(&data[hdr+5], pBt->usableSize);
64647 pPage->nFree = (u16)(pBt->usableSize - first);
64648 decodeFlags(pPage, flags);
64649 pPage->cellOffset = first;
64650 pPage->aDataEnd = &data[pBt->usableSize];
64651 pPage->aCellIdx = &data[first];
64652 pPage->aDataOfst = &data[pPage->childPtrSize];
64653 pPage->nOverflow = 0;
64654 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
64655 pPage->maskPage = (u16)(pBt->pageSize - 1);
64656 pPage->nCell = 0;
64657 pPage->isInit = 1;
64658}
64659
64660
64661/*
64662** Convert a DbPage obtained from the pager into a MemPage used by
64663** the btree layer.
64664*/
64665static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
64666 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
64667 if( pgno!=pPage->pgno ){
64668 pPage->aData = sqlite3PagerGetData(pDbPage);
64669 pPage->pDbPage = pDbPage;
64670 pPage->pBt = pBt;
64671 pPage->pgno = pgno;
64672 pPage->hdrOffset = pgno==1 ? 100 : 0;
64673 }
64674 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
64675 return pPage;
64676}
64677
64678/*
64679** Get a page from the pager. Initialize the MemPage.pBt and
64680** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
64681**
64682** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
64683** about the content of the page at this time. So do not go to the disk
64684** to fetch the content. Just fill in the content with zeros for now.
64685** If in the future we call sqlite3PagerWrite() on this page, that
64686** means we have started to be concerned about content and the disk
64687** read should occur at that point.
64688*/
64689static int btreeGetPage(
64690 BtShared *pBt, /* The btree */
64691 Pgno pgno, /* Number of the page to fetch */
64692 MemPage **ppPage, /* Return the page in this parameter */
64693 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
64694){
64695 int rc;
64696 DbPage *pDbPage;
64697
64698 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
64699 assert( sqlite3_mutex_held(pBt->mutex) );
64700 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
64701 if( rc ) return rc;
64702 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
64703 return SQLITE_OK;
64704}
64705
64706/*
64707** Retrieve a page from the pager cache. If the requested page is not
64708** already in the pager cache return NULL. Initialize the MemPage.pBt and
64709** MemPage.aData elements if needed.
64710*/
64711static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
64712 DbPage *pDbPage;
64713 assert( sqlite3_mutex_held(pBt->mutex) );
64714 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
64715 if( pDbPage ){
64716 return btreePageFromDbPage(pDbPage, pgno, pBt);
64717 }
64718 return 0;
64719}
64720
64721/*
64722** Return the size of the database file in pages. If there is any kind of
64723** error, return ((unsigned int)-1).
64724*/
64725static Pgno btreePagecount(BtShared *pBt){
64726 return pBt->nPage;
64727}
64728SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
64729 assert( sqlite3BtreeHoldsMutex(p) );
64730 assert( ((p->pBt->nPage)&0x80000000)==0 );
64731 return btreePagecount(p->pBt);
64732}
64733
64734/*
64735** Get a page from the pager and initialize it.
64736**
64737** If pCur!=0 then the page is being fetched as part of a moveToChild()
64738** call. Do additional sanity checking on the page in this case.
64739** And if the fetch fails, this routine must decrement pCur->iPage.
64740**
64741** The page is fetched as read-write unless pCur is not NULL and is
64742** a read-only cursor.
64743**
64744** If an error occurs, then *ppPage is undefined. It
64745** may remain unchanged, or it may be set to an invalid value.
64746*/
64747static int getAndInitPage(
64748 BtShared *pBt, /* The database file */
64749 Pgno pgno, /* Number of the page to get */
64750 MemPage **ppPage, /* Write the page pointer here */
64751 BtCursor *pCur, /* Cursor to receive the page, or NULL */
64752 int bReadOnly /* True for a read-only page */
64753){
64754 int rc;
64755 DbPage *pDbPage;
64756 assert( sqlite3_mutex_held(pBt->mutex) );
64757 assert( pCur==0 || ppPage==&pCur->pPage );
64758 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
64759 assert( pCur==0 || pCur->iPage>0 );
64760
64761 if( pgno>btreePagecount(pBt) ){
64762 rc = SQLITE_CORRUPT_BKPT;
64763 goto getAndInitPage_error;
64764 }
64765 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
64766 if( rc ){
64767 goto getAndInitPage_error;
64768 }
64769 *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
64770 if( (*ppPage)->isInit==0 ){
64771 btreePageFromDbPage(pDbPage, pgno, pBt);
64772 rc = btreeInitPage(*ppPage);
64773 if( rc!=SQLITE_OK ){
64774 releasePage(*ppPage);
64775 goto getAndInitPage_error;
64776 }
64777 }
64778 assert( (*ppPage)->pgno==pgno );
64779 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
64780
64781 /* If obtaining a child page for a cursor, we must verify that the page is
64782 ** compatible with the root page. */
64783 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
64784 rc = SQLITE_CORRUPT_PGNO(pgno);
64785 releasePage(*ppPage);
64786 goto getAndInitPage_error;
64787 }
64788 return SQLITE_OK;
64789
64790getAndInitPage_error:
64791 if( pCur ){
64792 pCur->iPage--;
64793 pCur->pPage = pCur->apPage[pCur->iPage];
64794 }
64795 testcase( pgno==0 );
64796 assert( pgno!=0 || rc==SQLITE_CORRUPT );
64797 return rc;
64798}
64799
64800/*
64801** Release a MemPage. This should be called once for each prior
64802** call to btreeGetPage.
64803**
64804** Page1 is a special case and must be released using releasePageOne().
64805*/
64806static void releasePageNotNull(MemPage *pPage){
64807 assert( pPage->aData );
64808 assert( pPage->pBt );
64809 assert( pPage->pDbPage!=0 );
64810 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
64811 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
64812 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64813 sqlite3PagerUnrefNotNull(pPage->pDbPage);
64814}
64815static void releasePage(MemPage *pPage){
64816 if( pPage ) releasePageNotNull(pPage);
64817}
64818static void releasePageOne(MemPage *pPage){
64819 assert( pPage!=0 );
64820 assert( pPage->aData );
64821 assert( pPage->pBt );
64822 assert( pPage->pDbPage!=0 );
64823 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
64824 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
64825 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64826 sqlite3PagerUnrefPageOne(pPage->pDbPage);
64827}
64828
64829/*
64830** Get an unused page.
64831**
64832** This works just like btreeGetPage() with the addition:
64833**
64834** * If the page is already in use for some other purpose, immediately
64835** release it and return an SQLITE_CURRUPT error.
64836** * Make sure the isInit flag is clear
64837*/
64838static int btreeGetUnusedPage(
64839 BtShared *pBt, /* The btree */
64840 Pgno pgno, /* Number of the page to fetch */
64841 MemPage **ppPage, /* Return the page in this parameter */
64842 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
64843){
64844 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
64845 if( rc==SQLITE_OK ){
64846 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
64847 releasePage(*ppPage);
64848 *ppPage = 0;
64849 return SQLITE_CORRUPT_BKPT;
64850 }
64851 (*ppPage)->isInit = 0;
64852 }else{
64853 *ppPage = 0;
64854 }
64855 return rc;
64856}
64857
64858
64859/*
64860** During a rollback, when the pager reloads information into the cache
64861** so that the cache is restored to its original state at the start of
64862** the transaction, for each page restored this routine is called.
64863**
64864** This routine needs to reset the extra data section at the end of the
64865** page to agree with the restored data.
64866*/
64867static void pageReinit(DbPage *pData){
64868 MemPage *pPage;
64869 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
64870 assert( sqlite3PagerPageRefcount(pData)>0 );
64871 if( pPage->isInit ){
64872 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
64873 pPage->isInit = 0;
64874 if( sqlite3PagerPageRefcount(pData)>1 ){
64875 /* pPage might not be a btree page; it might be an overflow page
64876 ** or ptrmap page or a free page. In those cases, the following
64877 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
64878 ** But no harm is done by this. And it is very important that
64879 ** btreeInitPage() be called on every btree page so we make
64880 ** the call for every page that comes in for re-initing. */
64881 btreeInitPage(pPage);
64882 }
64883 }
64884}
64885
64886/*
64887** Invoke the busy handler for a btree.
64888*/
64889static int btreeInvokeBusyHandler(void *pArg){
64890 BtShared *pBt = (BtShared*)pArg;
64891 assert( pBt->db );
64892 assert( sqlite3_mutex_held(pBt->db->mutex) );
64893 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler,
64894 sqlite3PagerFile(pBt->pPager));
64895}
64896
64897/*
64898** Open a database file.
64899**
64900** zFilename is the name of the database file. If zFilename is NULL
64901** then an ephemeral database is created. The ephemeral database might
64902** be exclusively in memory, or it might use a disk-based memory cache.
64903** Either way, the ephemeral database will be automatically deleted
64904** when sqlite3BtreeClose() is called.
64905**
64906** If zFilename is ":memory:" then an in-memory database is created
64907** that is automatically destroyed when it is closed.
64908**
64909** The "flags" parameter is a bitmask that might contain bits like
64910** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
64911**
64912** If the database is already opened in the same database connection
64913** and we are in shared cache mode, then the open will fail with an
64914** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
64915** objects in the same database connection since doing so will lead
64916** to problems with locking.
64917*/
64918SQLITE_PRIVATE int sqlite3BtreeOpen(
64919 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
64920 const char *zFilename, /* Name of the file containing the BTree database */
64921 sqlite3 *db, /* Associated database handle */
64922 Btree **ppBtree, /* Pointer to new Btree object written here */
64923 int flags, /* Options */
64924 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
64925){
64926 BtShared *pBt = 0; /* Shared part of btree structure */
64927 Btree *p; /* Handle to return */
64928 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
64929 int rc = SQLITE_OK; /* Result code from this function */
64930 u8 nReserve; /* Byte of unused space on each page */
64931 unsigned char zDbHeader[100]; /* Database header content */
64932
64933 /* True if opening an ephemeral, temporary database */
64934 const int isTempDb = zFilename==0 || zFilename[0]==0;
64935
64936 /* Set the variable isMemdb to true for an in-memory database, or
64937 ** false for a file-based database.
64938 */
64939#ifdef SQLITE_OMIT_MEMORYDB
64940 const int isMemdb = 0;
64941#else
64942 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
64943 || (isTempDb && sqlite3TempInMemory(db))
64944 || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
64945#endif
64946
64947 assert( db!=0 );
64948 assert( pVfs!=0 );
64949 assert( sqlite3_mutex_held(db->mutex) );
64950 assert( (flags&0xff)==flags ); /* flags fit in 8 bits */
64951
64952 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
64953 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
64954
64955 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
64956 assert( (flags & BTREE_SINGLE)==0 || isTempDb );
64957
64958 if( isMemdb ){
64959 flags |= BTREE_MEMORY;
64960 }
64961 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
64962 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
64963 }
64964 p = sqlite3MallocZero(sizeof(Btree));
64965 if( !p ){
64966 return SQLITE_NOMEM_BKPT;
64967 }
64968 p->inTrans = TRANS_NONE;
64969 p->db = db;
64970#ifndef SQLITE_OMIT_SHARED_CACHE
64971 p->lock.pBtree = p;
64972 p->lock.iTable = 1;
64973#endif
64974
64975#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
64976 /*
64977 ** If this Btree is a candidate for shared cache, try to find an
64978 ** existing BtShared object that we can share with
64979 */
64980 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
64981 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
64982 int nFilename = sqlite3Strlen30(zFilename)+1;
64983 int nFullPathname = pVfs->mxPathname+1;
64984 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
64985 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
64986
64987 p->sharable = 1;
64988 if( !zFullPathname ){
64989 sqlite3_free(p);
64990 return SQLITE_NOMEM_BKPT;
64991 }
64992 if( isMemdb ){
64993 memcpy(zFullPathname, zFilename, nFilename);
64994 }else{
64995 rc = sqlite3OsFullPathname(pVfs, zFilename,
64996 nFullPathname, zFullPathname);
64997 if( rc ){
64998 sqlite3_free(zFullPathname);
64999 sqlite3_free(p);
65000 return rc;
65001 }
65002 }
65003#if SQLITE_THREADSAFE
65004 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
65005 sqlite3_mutex_enter(mutexOpen);
65006 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
65007 sqlite3_mutex_enter(mutexShared);
65008#endif
65009 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
65010 assert( pBt->nRef>0 );
65011 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
65012 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
65013 int iDb;
65014 for(iDb=db->nDb-1; iDb>=0; iDb--){
65015 Btree *pExisting = db->aDb[iDb].pBt;
65016 if( pExisting && pExisting->pBt==pBt ){
65017 sqlite3_mutex_leave(mutexShared);
65018 sqlite3_mutex_leave(mutexOpen);
65019 sqlite3_free(zFullPathname);
65020 sqlite3_free(p);
65021 return SQLITE_CONSTRAINT;
65022 }
65023 }
65024 p->pBt = pBt;
65025 pBt->nRef++;
65026 break;
65027 }
65028 }
65029 sqlite3_mutex_leave(mutexShared);
65030 sqlite3_free(zFullPathname);
65031 }
65032#ifdef SQLITE_DEBUG
65033 else{
65034 /* In debug mode, we mark all persistent databases as sharable
65035 ** even when they are not. This exercises the locking code and
65036 ** gives more opportunity for asserts(sqlite3_mutex_held())
65037 ** statements to find locking problems.
65038 */
65039 p->sharable = 1;
65040 }
65041#endif
65042 }
65043#endif
65044 if( pBt==0 ){
65045 /*
65046 ** The following asserts make sure that structures used by the btree are
65047 ** the right size. This is to guard against size changes that result
65048 ** when compiling on a different architecture.
65049 */
65050 assert( sizeof(i64)==8 );
65051 assert( sizeof(u64)==8 );
65052 assert( sizeof(u32)==4 );
65053 assert( sizeof(u16)==2 );
65054 assert( sizeof(Pgno)==4 );
65055
65056 pBt = sqlite3MallocZero( sizeof(*pBt) );
65057 if( pBt==0 ){
65058 rc = SQLITE_NOMEM_BKPT;
65059 goto btree_open_out;
65060 }
65061 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
65062 sizeof(MemPage), flags, vfsFlags, pageReinit);
65063 if( rc==SQLITE_OK ){
65064 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
65065 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
65066 }
65067 if( rc!=SQLITE_OK ){
65068 goto btree_open_out;
65069 }
65070 pBt->openFlags = (u8)flags;
65071 pBt->db = db;
65072 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
65073 p->pBt = pBt;
65074
65075 pBt->pCursor = 0;
65076 pBt->pPage1 = 0;
65077 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
65078#if defined(SQLITE_SECURE_DELETE)
65079 pBt->btsFlags |= BTS_SECURE_DELETE;
65080#elif defined(SQLITE_FAST_SECURE_DELETE)
65081 pBt->btsFlags |= BTS_OVERWRITE;
65082#endif
65083 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
65084 ** determined by the 2-byte integer located at an offset of 16 bytes from
65085 ** the beginning of the database file. */
65086 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
65087 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
65088 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
65089 pBt->pageSize = 0;
65090#ifndef SQLITE_OMIT_AUTOVACUUM
65091 /* If the magic name ":memory:" will create an in-memory database, then
65092 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
65093 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
65094 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
65095 ** regular file-name. In this case the auto-vacuum applies as per normal.
65096 */
65097 if( zFilename && !isMemdb ){
65098 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
65099 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
65100 }
65101#endif
65102 nReserve = 0;
65103 }else{
65104 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
65105 ** determined by the one-byte unsigned integer found at an offset of 20
65106 ** into the database file header. */
65107 nReserve = zDbHeader[20];
65108 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
65109#ifndef SQLITE_OMIT_AUTOVACUUM
65110 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
65111 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
65112#endif
65113 }
65114 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
65115 if( rc ) goto btree_open_out;
65116 pBt->usableSize = pBt->pageSize - nReserve;
65117 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
65118
65119#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
65120 /* Add the new BtShared object to the linked list sharable BtShareds.
65121 */
65122 pBt->nRef = 1;
65123 if( p->sharable ){
65124 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
65125 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
65126 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
65127 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
65128 if( pBt->mutex==0 ){
65129 rc = SQLITE_NOMEM_BKPT;
65130 goto btree_open_out;
65131 }
65132 }
65133 sqlite3_mutex_enter(mutexShared);
65134 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
65135 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
65136 sqlite3_mutex_leave(mutexShared);
65137 }
65138#endif
65139 }
65140
65141#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
65142 /* If the new Btree uses a sharable pBtShared, then link the new
65143 ** Btree into the list of all sharable Btrees for the same connection.
65144 ** The list is kept in ascending order by pBt address.
65145 */
65146 if( p->sharable ){
65147 int i;
65148 Btree *pSib;
65149 for(i=0; i<db->nDb; i++){
65150 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
65151 while( pSib->pPrev ){ pSib = pSib->pPrev; }
65152 if( (uptr)p->pBt<(uptr)pSib->pBt ){
65153 p->pNext = pSib;
65154 p->pPrev = 0;
65155 pSib->pPrev = p;
65156 }else{
65157 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
65158 pSib = pSib->pNext;
65159 }
65160 p->pNext = pSib->pNext;
65161 p->pPrev = pSib;
65162 if( p->pNext ){
65163 p->pNext->pPrev = p;
65164 }
65165 pSib->pNext = p;
65166 }
65167 break;
65168 }
65169 }
65170 }
65171#endif
65172 *ppBtree = p;
65173
65174btree_open_out:
65175 if( rc!=SQLITE_OK ){
65176 if( pBt && pBt->pPager ){
65177 sqlite3PagerClose(pBt->pPager, 0);
65178 }
65179 sqlite3_free(pBt);
65180 sqlite3_free(p);
65181 *ppBtree = 0;
65182 }else{
65183 sqlite3_file *pFile;
65184
65185 /* If the B-Tree was successfully opened, set the pager-cache size to the
65186 ** default value. Except, when opening on an existing shared pager-cache,
65187 ** do not change the pager-cache size.
65188 */
65189 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
65190 sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
65191 }
65192
65193 pFile = sqlite3PagerFile(pBt->pPager);
65194 if( pFile->pMethods ){
65195 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
65196 }
65197 }
65198 if( mutexOpen ){
65199 assert( sqlite3_mutex_held(mutexOpen) );
65200 sqlite3_mutex_leave(mutexOpen);
65201 }
65202 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
65203 return rc;
65204}
65205
65206/*
65207** Decrement the BtShared.nRef counter. When it reaches zero,
65208** remove the BtShared structure from the sharing list. Return
65209** true if the BtShared.nRef counter reaches zero and return
65210** false if it is still positive.
65211*/
65212static int removeFromSharingList(BtShared *pBt){
65213#ifndef SQLITE_OMIT_SHARED_CACHE
65214 MUTEX_LOGIC( sqlite3_mutex *pMaster; )
65215 BtShared *pList;
65216 int removed = 0;
65217
65218 assert( sqlite3_mutex_notheld(pBt->mutex) );
65219 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
65220 sqlite3_mutex_enter(pMaster);
65221 pBt->nRef--;
65222 if( pBt->nRef<=0 ){
65223 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
65224 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
65225 }else{
65226 pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
65227 while( ALWAYS(pList) && pList->pNext!=pBt ){
65228 pList=pList->pNext;
65229 }
65230 if( ALWAYS(pList) ){
65231 pList->pNext = pBt->pNext;
65232 }
65233 }
65234 if( SQLITE_THREADSAFE ){
65235 sqlite3_mutex_free(pBt->mutex);
65236 }
65237 removed = 1;
65238 }
65239 sqlite3_mutex_leave(pMaster);
65240 return removed;
65241#else
65242 return 1;
65243#endif
65244}
65245
65246/*
65247** Make sure pBt->pTmpSpace points to an allocation of
65248** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
65249** pointer.
65250*/
65251static void allocateTempSpace(BtShared *pBt){
65252 if( !pBt->pTmpSpace ){
65253 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
65254
65255 /* One of the uses of pBt->pTmpSpace is to format cells before
65256 ** inserting them into a leaf page (function fillInCell()). If
65257 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
65258 ** by the various routines that manipulate binary cells. Which
65259 ** can mean that fillInCell() only initializes the first 2 or 3
65260 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
65261 ** it into a database page. This is not actually a problem, but it
65262 ** does cause a valgrind error when the 1 or 2 bytes of unitialized
65263 ** data is passed to system call write(). So to avoid this error,
65264 ** zero the first 4 bytes of temp space here.
65265 **
65266 ** Also: Provide four bytes of initialized space before the
65267 ** beginning of pTmpSpace as an area available to prepend the
65268 ** left-child pointer to the beginning of a cell.
65269 */
65270 if( pBt->pTmpSpace ){
65271 memset(pBt->pTmpSpace, 0, 8);
65272 pBt->pTmpSpace += 4;
65273 }
65274 }
65275}
65276
65277/*
65278** Free the pBt->pTmpSpace allocation
65279*/
65280static void freeTempSpace(BtShared *pBt){
65281 if( pBt->pTmpSpace ){
65282 pBt->pTmpSpace -= 4;
65283 sqlite3PageFree(pBt->pTmpSpace);
65284 pBt->pTmpSpace = 0;
65285 }
65286}
65287
65288/*
65289** Close an open database and invalidate all cursors.
65290*/
65291SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
65292 BtShared *pBt = p->pBt;
65293 BtCursor *pCur;
65294
65295 /* Close all cursors opened via this handle. */
65296 assert( sqlite3_mutex_held(p->db->mutex) );
65297 sqlite3BtreeEnter(p);
65298 pCur = pBt->pCursor;
65299 while( pCur ){
65300 BtCursor *pTmp = pCur;
65301 pCur = pCur->pNext;
65302 if( pTmp->pBtree==p ){
65303 sqlite3BtreeCloseCursor(pTmp);
65304 }
65305 }
65306
65307 /* Rollback any active transaction and free the handle structure.
65308 ** The call to sqlite3BtreeRollback() drops any table-locks held by
65309 ** this handle.
65310 */
65311 sqlite3BtreeRollback(p, SQLITE_OK, 0);
65312 sqlite3BtreeLeave(p);
65313
65314 /* If there are still other outstanding references to the shared-btree
65315 ** structure, return now. The remainder of this procedure cleans
65316 ** up the shared-btree.
65317 */
65318 assert( p->wantToLock==0 && p->locked==0 );
65319 if( !p->sharable || removeFromSharingList(pBt) ){
65320 /* The pBt is no longer on the sharing list, so we can access
65321 ** it without having to hold the mutex.
65322 **
65323 ** Clean out and delete the BtShared object.
65324 */
65325 assert( !pBt->pCursor );
65326 sqlite3PagerClose(pBt->pPager, p->db);
65327 if( pBt->xFreeSchema && pBt->pSchema ){
65328 pBt->xFreeSchema(pBt->pSchema);
65329 }
65330 sqlite3DbFree(0, pBt->pSchema);
65331 freeTempSpace(pBt);
65332 sqlite3_free(pBt);
65333 }
65334
65335#ifndef SQLITE_OMIT_SHARED_CACHE
65336 assert( p->wantToLock==0 );
65337 assert( p->locked==0 );
65338 if( p->pPrev ) p->pPrev->pNext = p->pNext;
65339 if( p->pNext ) p->pNext->pPrev = p->pPrev;
65340#endif
65341
65342 sqlite3_free(p);
65343 return SQLITE_OK;
65344}
65345
65346/*
65347** Change the "soft" limit on the number of pages in the cache.
65348** Unused and unmodified pages will be recycled when the number of
65349** pages in the cache exceeds this soft limit. But the size of the
65350** cache is allowed to grow larger than this limit if it contains
65351** dirty pages or pages still in active use.
65352*/
65353SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
65354 BtShared *pBt = p->pBt;
65355 assert( sqlite3_mutex_held(p->db->mutex) );
65356 sqlite3BtreeEnter(p);
65357 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
65358 sqlite3BtreeLeave(p);
65359 return SQLITE_OK;
65360}
65361
65362/*
65363** Change the "spill" limit on the number of pages in the cache.
65364** If the number of pages exceeds this limit during a write transaction,
65365** the pager might attempt to "spill" pages to the journal early in
65366** order to free up memory.
65367**
65368** The value returned is the current spill size. If zero is passed
65369** as an argument, no changes are made to the spill size setting, so
65370** using mxPage of 0 is a way to query the current spill size.
65371*/
65372SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
65373 BtShared *pBt = p->pBt;
65374 int res;
65375 assert( sqlite3_mutex_held(p->db->mutex) );
65376 sqlite3BtreeEnter(p);
65377 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
65378 sqlite3BtreeLeave(p);
65379 return res;
65380}
65381
65382#if SQLITE_MAX_MMAP_SIZE>0
65383/*
65384** Change the limit on the amount of the database file that may be
65385** memory mapped.
65386*/
65387SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
65388 BtShared *pBt = p->pBt;
65389 assert( sqlite3_mutex_held(p->db->mutex) );
65390 sqlite3BtreeEnter(p);
65391 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
65392 sqlite3BtreeLeave(p);
65393 return SQLITE_OK;
65394}
65395#endif /* SQLITE_MAX_MMAP_SIZE>0 */
65396
65397/*
65398** Change the way data is synced to disk in order to increase or decrease
65399** how well the database resists damage due to OS crashes and power
65400** failures. Level 1 is the same as asynchronous (no syncs() occur and
65401** there is a high probability of damage) Level 2 is the default. There
65402** is a very low but non-zero probability of damage. Level 3 reduces the
65403** probability of damage to near zero but with a write performance reduction.
65404*/
65405#ifndef SQLITE_OMIT_PAGER_PRAGMAS
65406SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
65407 Btree *p, /* The btree to set the safety level on */
65408 unsigned pgFlags /* Various PAGER_* flags */
65409){
65410 BtShared *pBt = p->pBt;
65411 assert( sqlite3_mutex_held(p->db->mutex) );
65412 sqlite3BtreeEnter(p);
65413 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
65414 sqlite3BtreeLeave(p);
65415 return SQLITE_OK;
65416}
65417#endif
65418
65419/*
65420** Change the default pages size and the number of reserved bytes per page.
65421** Or, if the page size has already been fixed, return SQLITE_READONLY
65422** without changing anything.
65423**
65424** The page size must be a power of 2 between 512 and 65536. If the page
65425** size supplied does not meet this constraint then the page size is not
65426** changed.
65427**
65428** Page sizes are constrained to be a power of two so that the region
65429** of the database file used for locking (beginning at PENDING_BYTE,
65430** the first byte past the 1GB boundary, 0x40000000) needs to occur
65431** at the beginning of a page.
65432**
65433** If parameter nReserve is less than zero, then the number of reserved
65434** bytes per page is left unchanged.
65435**
65436** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
65437** and autovacuum mode can no longer be changed.
65438*/
65439SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
65440 int rc = SQLITE_OK;
65441 BtShared *pBt = p->pBt;
65442 assert( nReserve>=-1 && nReserve<=255 );
65443 sqlite3BtreeEnter(p);
65444#if SQLITE_HAS_CODEC
65445 if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve;
65446#endif
65447 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
65448 sqlite3BtreeLeave(p);
65449 return SQLITE_READONLY;
65450 }
65451 if( nReserve<0 ){
65452 nReserve = pBt->pageSize - pBt->usableSize;
65453 }
65454 assert( nReserve>=0 && nReserve<=255 );
65455 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
65456 ((pageSize-1)&pageSize)==0 ){
65457 assert( (pageSize & 7)==0 );
65458 assert( !pBt->pCursor );
65459 pBt->pageSize = (u32)pageSize;
65460 freeTempSpace(pBt);
65461 }
65462 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
65463 pBt->usableSize = pBt->pageSize - (u16)nReserve;
65464 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
65465 sqlite3BtreeLeave(p);
65466 return rc;
65467}
65468
65469/*
65470** Return the currently defined page size
65471*/
65472SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
65473 return p->pBt->pageSize;
65474}
65475
65476/*
65477** This function is similar to sqlite3BtreeGetReserve(), except that it
65478** may only be called if it is guaranteed that the b-tree mutex is already
65479** held.
65480**
65481** This is useful in one special case in the backup API code where it is
65482** known that the shared b-tree mutex is held, but the mutex on the
65483** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
65484** were to be called, it might collide with some other operation on the
65485** database handle that owns *p, causing undefined behavior.
65486*/
65487SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
65488 int n;
65489 assert( sqlite3_mutex_held(p->pBt->mutex) );
65490 n = p->pBt->pageSize - p->pBt->usableSize;
65491 return n;
65492}
65493
65494/*
65495** Return the number of bytes of space at the end of every page that
65496** are intentually left unused. This is the "reserved" space that is
65497** sometimes used by extensions.
65498**
65499** If SQLITE_HAS_MUTEX is defined then the number returned is the
65500** greater of the current reserved space and the maximum requested
65501** reserve space.
65502*/
65503SQLITE_PRIVATE int sqlite3BtreeGetOptimalReserve(Btree *p){
65504 int n;
65505 sqlite3BtreeEnter(p);
65506 n = sqlite3BtreeGetReserveNoMutex(p);
65507#ifdef SQLITE_HAS_CODEC
65508 if( n<p->pBt->optimalReserve ) n = p->pBt->optimalReserve;
65509#endif
65510 sqlite3BtreeLeave(p);
65511 return n;
65512}
65513
65514
65515/*
65516** Set the maximum page count for a database if mxPage is positive.
65517** No changes are made if mxPage is 0 or negative.
65518** Regardless of the value of mxPage, return the maximum page count.
65519*/
65520SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
65521 int n;
65522 sqlite3BtreeEnter(p);
65523 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
65524 sqlite3BtreeLeave(p);
65525 return n;
65526}
65527
65528/*
65529** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
65530**
65531** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
65532** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
65533** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
65534** newFlag==(-1) No changes
65535**
65536** This routine acts as a query if newFlag is less than zero
65537**
65538** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
65539** freelist leaf pages are not written back to the database. Thus in-page
65540** deleted content is cleared, but freelist deleted content is not.
65541**
65542** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
65543** that freelist leaf pages are written back into the database, increasing
65544** the amount of disk I/O.
65545*/
65546SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
65547 int b;
65548 if( p==0 ) return 0;
65549 sqlite3BtreeEnter(p);
65550 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
65551 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
65552 if( newFlag>=0 ){
65553 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
65554 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
65555 }
65556 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
65557 sqlite3BtreeLeave(p);
65558 return b;
65559}
65560
65561/*
65562** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
65563** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
65564** is disabled. The default value for the auto-vacuum property is
65565** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
65566*/
65567SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
65568#ifdef SQLITE_OMIT_AUTOVACUUM
65569 return SQLITE_READONLY;
65570#else
65571 BtShared *pBt = p->pBt;
65572 int rc = SQLITE_OK;
65573 u8 av = (u8)autoVacuum;
65574
65575 sqlite3BtreeEnter(p);
65576 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
65577 rc = SQLITE_READONLY;
65578 }else{
65579 pBt->autoVacuum = av ?1:0;
65580 pBt->incrVacuum = av==2 ?1:0;
65581 }
65582 sqlite3BtreeLeave(p);
65583 return rc;
65584#endif
65585}
65586
65587/*
65588** Return the value of the 'auto-vacuum' property. If auto-vacuum is
65589** enabled 1 is returned. Otherwise 0.
65590*/
65591SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
65592#ifdef SQLITE_OMIT_AUTOVACUUM
65593 return BTREE_AUTOVACUUM_NONE;
65594#else
65595 int rc;
65596 sqlite3BtreeEnter(p);
65597 rc = (
65598 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
65599 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
65600 BTREE_AUTOVACUUM_INCR
65601 );
65602 sqlite3BtreeLeave(p);
65603 return rc;
65604#endif
65605}
65606
65607/*
65608** If the user has not set the safety-level for this database connection
65609** using "PRAGMA synchronous", and if the safety-level is not already
65610** set to the value passed to this function as the second parameter,
65611** set it so.
65612*/
65613#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \
65614 && !defined(SQLITE_OMIT_WAL)
65615static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
65616 sqlite3 *db;
65617 Db *pDb;
65618 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
65619 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
65620 if( pDb->bSyncSet==0
65621 && pDb->safety_level!=safety_level
65622 && pDb!=&db->aDb[1]
65623 ){
65624 pDb->safety_level = safety_level;
65625 sqlite3PagerSetFlags(pBt->pPager,
65626 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
65627 }
65628 }
65629}
65630#else
65631# define setDefaultSyncFlag(pBt,safety_level)
65632#endif
65633
65634/* Forward declaration */
65635static int newDatabase(BtShared*);
65636
65637
65638/*
65639** Get a reference to pPage1 of the database file. This will
65640** also acquire a readlock on that file.
65641**
65642** SQLITE_OK is returned on success. If the file is not a
65643** well-formed database file, then SQLITE_CORRUPT is returned.
65644** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
65645** is returned if we run out of memory.
65646*/
65647static int lockBtree(BtShared *pBt){
65648 int rc; /* Result code from subfunctions */
65649 MemPage *pPage1; /* Page 1 of the database file */
65650 int nPage; /* Number of pages in the database */
65651 int nPageFile = 0; /* Number of pages in the database file */
65652 int nPageHeader; /* Number of pages in the database according to hdr */
65653
65654 assert( sqlite3_mutex_held(pBt->mutex) );
65655 assert( pBt->pPage1==0 );
65656 rc = sqlite3PagerSharedLock(pBt->pPager);
65657 if( rc!=SQLITE_OK ) return rc;
65658 rc = btreeGetPage(pBt, 1, &pPage1, 0);
65659 if( rc!=SQLITE_OK ) return rc;
65660
65661 /* Do some checking to help insure the file we opened really is
65662 ** a valid database file.
65663 */
65664 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
65665 sqlite3PagerPagecount(pBt->pPager, &nPageFile);
65666 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
65667 nPage = nPageFile;
65668 }
65669 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
65670 nPage = 0;
65671 }
65672 if( nPage>0 ){
65673 u32 pageSize;
65674 u32 usableSize;
65675 u8 *page1 = pPage1->aData;
65676 rc = SQLITE_NOTADB;
65677 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
65678 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
65679 ** 61 74 20 33 00. */
65680 if( memcmp(page1, zMagicHeader, 16)!=0 ){
65681 goto page1_init_failed;
65682 }
65683
65684#ifdef SQLITE_OMIT_WAL
65685 if( page1[18]>1 ){
65686 pBt->btsFlags |= BTS_READ_ONLY;
65687 }
65688 if( page1[19]>1 ){
65689 goto page1_init_failed;
65690 }
65691#else
65692 if( page1[18]>2 ){
65693 pBt->btsFlags |= BTS_READ_ONLY;
65694 }
65695 if( page1[19]>2 ){
65696 goto page1_init_failed;
65697 }
65698
65699 /* If the write version is set to 2, this database should be accessed
65700 ** in WAL mode. If the log is not already open, open it now. Then
65701 ** return SQLITE_OK and return without populating BtShared.pPage1.
65702 ** The caller detects this and calls this function again. This is
65703 ** required as the version of page 1 currently in the page1 buffer
65704 ** may not be the latest version - there may be a newer one in the log
65705 ** file.
65706 */
65707 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
65708 int isOpen = 0;
65709 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
65710 if( rc!=SQLITE_OK ){
65711 goto page1_init_failed;
65712 }else{
65713 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
65714 if( isOpen==0 ){
65715 releasePageOne(pPage1);
65716 return SQLITE_OK;
65717 }
65718 }
65719 rc = SQLITE_NOTADB;
65720 }else{
65721 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
65722 }
65723#endif
65724
65725 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
65726 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
65727 **
65728 ** The original design allowed these amounts to vary, but as of
65729 ** version 3.6.0, we require them to be fixed.
65730 */
65731 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
65732 goto page1_init_failed;
65733 }
65734 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
65735 ** determined by the 2-byte integer located at an offset of 16 bytes from
65736 ** the beginning of the database file. */
65737 pageSize = (page1[16]<<8) | (page1[17]<<16);
65738 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
65739 ** between 512 and 65536 inclusive. */
65740 if( ((pageSize-1)&pageSize)!=0
65741 || pageSize>SQLITE_MAX_PAGE_SIZE
65742 || pageSize<=256
65743 ){
65744 goto page1_init_failed;
65745 }
65746 assert( (pageSize & 7)==0 );
65747 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
65748 ** integer at offset 20 is the number of bytes of space at the end of
65749 ** each page to reserve for extensions.
65750 **
65751 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
65752 ** determined by the one-byte unsigned integer found at an offset of 20
65753 ** into the database file header. */
65754 usableSize = pageSize - page1[20];
65755 if( (u32)pageSize!=pBt->pageSize ){
65756 /* After reading the first page of the database assuming a page size
65757 ** of BtShared.pageSize, we have discovered that the page-size is
65758 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
65759 ** zero and return SQLITE_OK. The caller will call this function
65760 ** again with the correct page-size.
65761 */
65762 releasePageOne(pPage1);
65763 pBt->usableSize = usableSize;
65764 pBt->pageSize = pageSize;
65765 freeTempSpace(pBt);
65766 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
65767 pageSize-usableSize);
65768 return rc;
65769 }
65770 if( (pBt->db->flags & SQLITE_WriteSchema)==0 && nPage>nPageFile ){
65771 rc = SQLITE_CORRUPT_BKPT;
65772 goto page1_init_failed;
65773 }
65774 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
65775 ** be less than 480. In other words, if the page size is 512, then the
65776 ** reserved space size cannot exceed 32. */
65777 if( usableSize<480 ){
65778 goto page1_init_failed;
65779 }
65780 pBt->pageSize = pageSize;
65781 pBt->usableSize = usableSize;
65782#ifndef SQLITE_OMIT_AUTOVACUUM
65783 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
65784 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
65785#endif
65786 }
65787
65788 /* maxLocal is the maximum amount of payload to store locally for
65789 ** a cell. Make sure it is small enough so that at least minFanout
65790 ** cells can will fit on one page. We assume a 10-byte page header.
65791 ** Besides the payload, the cell must store:
65792 ** 2-byte pointer to the cell
65793 ** 4-byte child pointer
65794 ** 9-byte nKey value
65795 ** 4-byte nData value
65796 ** 4-byte overflow page pointer
65797 ** So a cell consists of a 2-byte pointer, a header which is as much as
65798 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
65799 ** page pointer.
65800 */
65801 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
65802 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
65803 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
65804 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
65805 if( pBt->maxLocal>127 ){
65806 pBt->max1bytePayload = 127;
65807 }else{
65808 pBt->max1bytePayload = (u8)pBt->maxLocal;
65809 }
65810 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
65811 pBt->pPage1 = pPage1;
65812 pBt->nPage = nPage;
65813 return SQLITE_OK;
65814
65815page1_init_failed:
65816 releasePageOne(pPage1);
65817 pBt->pPage1 = 0;
65818 return rc;
65819}
65820
65821#ifndef NDEBUG
65822/*
65823** Return the number of cursors open on pBt. This is for use
65824** in assert() expressions, so it is only compiled if NDEBUG is not
65825** defined.
65826**
65827** Only write cursors are counted if wrOnly is true. If wrOnly is
65828** false then all cursors are counted.
65829**
65830** For the purposes of this routine, a cursor is any cursor that
65831** is capable of reading or writing to the database. Cursors that
65832** have been tripped into the CURSOR_FAULT state are not counted.
65833*/
65834static int countValidCursors(BtShared *pBt, int wrOnly){
65835 BtCursor *pCur;
65836 int r = 0;
65837 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
65838 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
65839 && pCur->eState!=CURSOR_FAULT ) r++;
65840 }
65841 return r;
65842}
65843#endif
65844
65845/*
65846** If there are no outstanding cursors and we are not in the middle
65847** of a transaction but there is a read lock on the database, then
65848** this routine unrefs the first page of the database file which
65849** has the effect of releasing the read lock.
65850**
65851** If there is a transaction in progress, this routine is a no-op.
65852*/
65853static void unlockBtreeIfUnused(BtShared *pBt){
65854 assert( sqlite3_mutex_held(pBt->mutex) );
65855 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
65856 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
65857 MemPage *pPage1 = pBt->pPage1;
65858 assert( pPage1->aData );
65859 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
65860 pBt->pPage1 = 0;
65861 releasePageOne(pPage1);
65862 }
65863}
65864
65865/*
65866** If pBt points to an empty file then convert that empty file
65867** into a new empty database by initializing the first page of
65868** the database.
65869*/
65870static int newDatabase(BtShared *pBt){
65871 MemPage *pP1;
65872 unsigned char *data;
65873 int rc;
65874
65875 assert( sqlite3_mutex_held(pBt->mutex) );
65876 if( pBt->nPage>0 ){
65877 return SQLITE_OK;
65878 }
65879 pP1 = pBt->pPage1;
65880 assert( pP1!=0 );
65881 data = pP1->aData;
65882 rc = sqlite3PagerWrite(pP1->pDbPage);
65883 if( rc ) return rc;
65884 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
65885 assert( sizeof(zMagicHeader)==16 );
65886 data[16] = (u8)((pBt->pageSize>>8)&0xff);
65887 data[17] = (u8)((pBt->pageSize>>16)&0xff);
65888 data[18] = 1;
65889 data[19] = 1;
65890 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
65891 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
65892 data[21] = 64;
65893 data[22] = 32;
65894 data[23] = 32;
65895 memset(&data[24], 0, 100-24);
65896 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
65897 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
65898#ifndef SQLITE_OMIT_AUTOVACUUM
65899 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
65900 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
65901 put4byte(&data[36 + 4*4], pBt->autoVacuum);
65902 put4byte(&data[36 + 7*4], pBt->incrVacuum);
65903#endif
65904 pBt->nPage = 1;
65905 data[31] = 1;
65906 return SQLITE_OK;
65907}
65908
65909/*
65910** Initialize the first page of the database file (creating a database
65911** consisting of a single page and no schema objects). Return SQLITE_OK
65912** if successful, or an SQLite error code otherwise.
65913*/
65914SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
65915 int rc;
65916 sqlite3BtreeEnter(p);
65917 p->pBt->nPage = 0;
65918 rc = newDatabase(p->pBt);
65919 sqlite3BtreeLeave(p);
65920 return rc;
65921}
65922
65923/*
65924** Attempt to start a new transaction. A write-transaction
65925** is started if the second argument is nonzero, otherwise a read-
65926** transaction. If the second argument is 2 or more and exclusive
65927** transaction is started, meaning that no other process is allowed
65928** to access the database. A preexisting transaction may not be
65929** upgraded to exclusive by calling this routine a second time - the
65930** exclusivity flag only works for a new transaction.
65931**
65932** A write-transaction must be started before attempting any
65933** changes to the database. None of the following routines
65934** will work unless a transaction is started first:
65935**
65936** sqlite3BtreeCreateTable()
65937** sqlite3BtreeCreateIndex()
65938** sqlite3BtreeClearTable()
65939** sqlite3BtreeDropTable()
65940** sqlite3BtreeInsert()
65941** sqlite3BtreeDelete()
65942** sqlite3BtreeUpdateMeta()
65943**
65944** If an initial attempt to acquire the lock fails because of lock contention
65945** and the database was previously unlocked, then invoke the busy handler
65946** if there is one. But if there was previously a read-lock, do not
65947** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
65948** returned when there is already a read-lock in order to avoid a deadlock.
65949**
65950** Suppose there are two processes A and B. A has a read lock and B has
65951** a reserved lock. B tries to promote to exclusive but is blocked because
65952** of A's read lock. A tries to promote to reserved but is blocked by B.
65953** One or the other of the two processes must give way or there can be
65954** no progress. By returning SQLITE_BUSY and not invoking the busy callback
65955** when A already has a read lock, we encourage A to give up and let B
65956** proceed.
65957*/
65958SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
65959 BtShared *pBt = p->pBt;
65960 int rc = SQLITE_OK;
65961
65962 sqlite3BtreeEnter(p);
65963 btreeIntegrity(p);
65964
65965 /* If the btree is already in a write-transaction, or it
65966 ** is already in a read-transaction and a read-transaction
65967 ** is requested, this is a no-op.
65968 */
65969 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
65970 goto trans_begun;
65971 }
65972 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
65973
65974 if( (p->db->flags & SQLITE_ResetDatabase)
65975 && sqlite3PagerIsreadonly(pBt->pPager)==0
65976 ){
65977 pBt->btsFlags &= ~BTS_READ_ONLY;
65978 }
65979
65980 /* Write transactions are not possible on a read-only database */
65981 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
65982 rc = SQLITE_READONLY;
65983 goto trans_begun;
65984 }
65985
65986#ifndef SQLITE_OMIT_SHARED_CACHE
65987 {
65988 sqlite3 *pBlock = 0;
65989 /* If another database handle has already opened a write transaction
65990 ** on this shared-btree structure and a second write transaction is
65991 ** requested, return SQLITE_LOCKED.
65992 */
65993 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
65994 || (pBt->btsFlags & BTS_PENDING)!=0
65995 ){
65996 pBlock = pBt->pWriter->db;
65997 }else if( wrflag>1 ){
65998 BtLock *pIter;
65999 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
66000 if( pIter->pBtree!=p ){
66001 pBlock = pIter->pBtree->db;
66002 break;
66003 }
66004 }
66005 }
66006 if( pBlock ){
66007 sqlite3ConnectionBlocked(p->db, pBlock);
66008 rc = SQLITE_LOCKED_SHAREDCACHE;
66009 goto trans_begun;
66010 }
66011 }
66012#endif
66013
66014 /* Any read-only or read-write transaction implies a read-lock on
66015 ** page 1. So if some other shared-cache client already has a write-lock
66016 ** on page 1, the transaction cannot be opened. */
66017 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
66018 if( SQLITE_OK!=rc ) goto trans_begun;
66019
66020 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
66021 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
66022 do {
66023 /* Call lockBtree() until either pBt->pPage1 is populated or
66024 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
66025 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
66026 ** reading page 1 it discovers that the page-size of the database
66027 ** file is not pBt->pageSize. In this case lockBtree() will update
66028 ** pBt->pageSize to the page-size of the file on disk.
66029 */
66030 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
66031
66032 if( rc==SQLITE_OK && wrflag ){
66033 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
66034 rc = SQLITE_READONLY;
66035 }else{
66036 rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
66037 if( rc==SQLITE_OK ){
66038 rc = newDatabase(pBt);
66039 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
66040 /* if there was no transaction opened when this function was
66041 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
66042 ** code to SQLITE_BUSY. */
66043 rc = SQLITE_BUSY;
66044 }
66045 }
66046 }
66047
66048 if( rc!=SQLITE_OK ){
66049 unlockBtreeIfUnused(pBt);
66050 }
66051 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
66052 btreeInvokeBusyHandler(pBt) );
66053 sqlite3PagerResetLockTimeout(pBt->pPager);
66054
66055 if( rc==SQLITE_OK ){
66056 if( p->inTrans==TRANS_NONE ){
66057 pBt->nTransaction++;
66058#ifndef SQLITE_OMIT_SHARED_CACHE
66059 if( p->sharable ){
66060 assert( p->lock.pBtree==p && p->lock.iTable==1 );
66061 p->lock.eLock = READ_LOCK;
66062 p->lock.pNext = pBt->pLock;
66063 pBt->pLock = &p->lock;
66064 }
66065#endif
66066 }
66067 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
66068 if( p->inTrans>pBt->inTransaction ){
66069 pBt->inTransaction = p->inTrans;
66070 }
66071 if( wrflag ){
66072 MemPage *pPage1 = pBt->pPage1;
66073#ifndef SQLITE_OMIT_SHARED_CACHE
66074 assert( !pBt->pWriter );
66075 pBt->pWriter = p;
66076 pBt->btsFlags &= ~BTS_EXCLUSIVE;
66077 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
66078#endif
66079
66080 /* If the db-size header field is incorrect (as it may be if an old
66081 ** client has been writing the database file), update it now. Doing
66082 ** this sooner rather than later means the database size can safely
66083 ** re-read the database size from page 1 if a savepoint or transaction
66084 ** rollback occurs within the transaction.
66085 */
66086 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
66087 rc = sqlite3PagerWrite(pPage1->pDbPage);
66088 if( rc==SQLITE_OK ){
66089 put4byte(&pPage1->aData[28], pBt->nPage);
66090 }
66091 }
66092 }
66093 }
66094
66095trans_begun:
66096 if( rc==SQLITE_OK ){
66097 if( pSchemaVersion ){
66098 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
66099 }
66100 if( wrflag ){
66101 /* This call makes sure that the pager has the correct number of
66102 ** open savepoints. If the second parameter is greater than 0 and
66103 ** the sub-journal is not already open, then it will be opened here.
66104 */
66105 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
66106 }
66107 }
66108
66109 btreeIntegrity(p);
66110 sqlite3BtreeLeave(p);
66111 return rc;
66112}
66113
66114#ifndef SQLITE_OMIT_AUTOVACUUM
66115
66116/*
66117** Set the pointer-map entries for all children of page pPage. Also, if
66118** pPage contains cells that point to overflow pages, set the pointer
66119** map entries for the overflow pages as well.
66120*/
66121static int setChildPtrmaps(MemPage *pPage){
66122 int i; /* Counter variable */
66123 int nCell; /* Number of cells in page pPage */
66124 int rc; /* Return code */
66125 BtShared *pBt = pPage->pBt;
66126 Pgno pgno = pPage->pgno;
66127
66128 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66129 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
66130 if( rc!=SQLITE_OK ) return rc;
66131 nCell = pPage->nCell;
66132
66133 for(i=0; i<nCell; i++){
66134 u8 *pCell = findCell(pPage, i);
66135
66136 ptrmapPutOvflPtr(pPage, pCell, &rc);
66137
66138 if( !pPage->leaf ){
66139 Pgno childPgno = get4byte(pCell);
66140 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
66141 }
66142 }
66143
66144 if( !pPage->leaf ){
66145 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
66146 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
66147 }
66148
66149 return rc;
66150}
66151
66152/*
66153** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
66154** that it points to iTo. Parameter eType describes the type of pointer to
66155** be modified, as follows:
66156**
66157** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
66158** page of pPage.
66159**
66160** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
66161** page pointed to by one of the cells on pPage.
66162**
66163** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
66164** overflow page in the list.
66165*/
66166static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
66167 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66168 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66169 if( eType==PTRMAP_OVERFLOW2 ){
66170 /* The pointer is always the first 4 bytes of the page in this case. */
66171 if( get4byte(pPage->aData)!=iFrom ){
66172 return SQLITE_CORRUPT_PAGE(pPage);
66173 }
66174 put4byte(pPage->aData, iTo);
66175 }else{
66176 int i;
66177 int nCell;
66178 int rc;
66179
66180 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
66181 if( rc ) return rc;
66182 nCell = pPage->nCell;
66183
66184 for(i=0; i<nCell; i++){
66185 u8 *pCell = findCell(pPage, i);
66186 if( eType==PTRMAP_OVERFLOW1 ){
66187 CellInfo info;
66188 pPage->xParseCell(pPage, pCell, &info);
66189 if( info.nLocal<info.nPayload ){
66190 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
66191 return SQLITE_CORRUPT_PAGE(pPage);
66192 }
66193 if( iFrom==get4byte(pCell+info.nSize-4) ){
66194 put4byte(pCell+info.nSize-4, iTo);
66195 break;
66196 }
66197 }
66198 }else{
66199 if( get4byte(pCell)==iFrom ){
66200 put4byte(pCell, iTo);
66201 break;
66202 }
66203 }
66204 }
66205
66206 if( i==nCell ){
66207 if( eType!=PTRMAP_BTREE ||
66208 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
66209 return SQLITE_CORRUPT_PAGE(pPage);
66210 }
66211 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
66212 }
66213 }
66214 return SQLITE_OK;
66215}
66216
66217
66218/*
66219** Move the open database page pDbPage to location iFreePage in the
66220** database. The pDbPage reference remains valid.
66221**
66222** The isCommit flag indicates that there is no need to remember that
66223** the journal needs to be sync()ed before database page pDbPage->pgno
66224** can be written to. The caller has already promised not to write to that
66225** page.
66226*/
66227static int relocatePage(
66228 BtShared *pBt, /* Btree */
66229 MemPage *pDbPage, /* Open page to move */
66230 u8 eType, /* Pointer map 'type' entry for pDbPage */
66231 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
66232 Pgno iFreePage, /* The location to move pDbPage to */
66233 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
66234){
66235 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
66236 Pgno iDbPage = pDbPage->pgno;
66237 Pager *pPager = pBt->pPager;
66238 int rc;
66239
66240 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
66241 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
66242 assert( sqlite3_mutex_held(pBt->mutex) );
66243 assert( pDbPage->pBt==pBt );
66244
66245 /* Move page iDbPage from its current location to page number iFreePage */
66246 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
66247 iDbPage, iFreePage, iPtrPage, eType));
66248 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
66249 if( rc!=SQLITE_OK ){
66250 return rc;
66251 }
66252 pDbPage->pgno = iFreePage;
66253
66254 /* If pDbPage was a btree-page, then it may have child pages and/or cells
66255 ** that point to overflow pages. The pointer map entries for all these
66256 ** pages need to be changed.
66257 **
66258 ** If pDbPage is an overflow page, then the first 4 bytes may store a
66259 ** pointer to a subsequent overflow page. If this is the case, then
66260 ** the pointer map needs to be updated for the subsequent overflow page.
66261 */
66262 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
66263 rc = setChildPtrmaps(pDbPage);
66264 if( rc!=SQLITE_OK ){
66265 return rc;
66266 }
66267 }else{
66268 Pgno nextOvfl = get4byte(pDbPage->aData);
66269 if( nextOvfl!=0 ){
66270 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
66271 if( rc!=SQLITE_OK ){
66272 return rc;
66273 }
66274 }
66275 }
66276
66277 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
66278 ** that it points at iFreePage. Also fix the pointer map entry for
66279 ** iPtrPage.
66280 */
66281 if( eType!=PTRMAP_ROOTPAGE ){
66282 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
66283 if( rc!=SQLITE_OK ){
66284 return rc;
66285 }
66286 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
66287 if( rc!=SQLITE_OK ){
66288 releasePage(pPtrPage);
66289 return rc;
66290 }
66291 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
66292 releasePage(pPtrPage);
66293 if( rc==SQLITE_OK ){
66294 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
66295 }
66296 }
66297 return rc;
66298}
66299
66300/* Forward declaration required by incrVacuumStep(). */
66301static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
66302
66303/*
66304** Perform a single step of an incremental-vacuum. If successful, return
66305** SQLITE_OK. If there is no work to do (and therefore no point in
66306** calling this function again), return SQLITE_DONE. Or, if an error
66307** occurs, return some other error code.
66308**
66309** More specifically, this function attempts to re-organize the database so
66310** that the last page of the file currently in use is no longer in use.
66311**
66312** Parameter nFin is the number of pages that this database would contain
66313** were this function called until it returns SQLITE_DONE.
66314**
66315** If the bCommit parameter is non-zero, this function assumes that the
66316** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
66317** or an error. bCommit is passed true for an auto-vacuum-on-commit
66318** operation, or false for an incremental vacuum.
66319*/
66320static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
66321 Pgno nFreeList; /* Number of pages still on the free-list */
66322 int rc;
66323
66324 assert( sqlite3_mutex_held(pBt->mutex) );
66325 assert( iLastPg>nFin );
66326
66327 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
66328 u8 eType;
66329 Pgno iPtrPage;
66330
66331 nFreeList = get4byte(&pBt->pPage1->aData[36]);
66332 if( nFreeList==0 ){
66333 return SQLITE_DONE;
66334 }
66335
66336 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
66337 if( rc!=SQLITE_OK ){
66338 return rc;
66339 }
66340 if( eType==PTRMAP_ROOTPAGE ){
66341 return SQLITE_CORRUPT_BKPT;
66342 }
66343
66344 if( eType==PTRMAP_FREEPAGE ){
66345 if( bCommit==0 ){
66346 /* Remove the page from the files free-list. This is not required
66347 ** if bCommit is non-zero. In that case, the free-list will be
66348 ** truncated to zero after this function returns, so it doesn't
66349 ** matter if it still contains some garbage entries.
66350 */
66351 Pgno iFreePg;
66352 MemPage *pFreePg;
66353 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
66354 if( rc!=SQLITE_OK ){
66355 return rc;
66356 }
66357 assert( iFreePg==iLastPg );
66358 releasePage(pFreePg);
66359 }
66360 } else {
66361 Pgno iFreePg; /* Index of free page to move pLastPg to */
66362 MemPage *pLastPg;
66363 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
66364 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
66365
66366 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
66367 if( rc!=SQLITE_OK ){
66368 return rc;
66369 }
66370
66371 /* If bCommit is zero, this loop runs exactly once and page pLastPg
66372 ** is swapped with the first free page pulled off the free list.
66373 **
66374 ** On the other hand, if bCommit is greater than zero, then keep
66375 ** looping until a free-page located within the first nFin pages
66376 ** of the file is found.
66377 */
66378 if( bCommit==0 ){
66379 eMode = BTALLOC_LE;
66380 iNear = nFin;
66381 }
66382 do {
66383 MemPage *pFreePg;
66384 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
66385 if( rc!=SQLITE_OK ){
66386 releasePage(pLastPg);
66387 return rc;
66388 }
66389 releasePage(pFreePg);
66390 }while( bCommit && iFreePg>nFin );
66391 assert( iFreePg<iLastPg );
66392
66393 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
66394 releasePage(pLastPg);
66395 if( rc!=SQLITE_OK ){
66396 return rc;
66397 }
66398 }
66399 }
66400
66401 if( bCommit==0 ){
66402 do {
66403 iLastPg--;
66404 }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
66405 pBt->bDoTruncate = 1;
66406 pBt->nPage = iLastPg;
66407 }
66408 return SQLITE_OK;
66409}
66410
66411/*
66412** The database opened by the first argument is an auto-vacuum database
66413** nOrig pages in size containing nFree free pages. Return the expected
66414** size of the database in pages following an auto-vacuum operation.
66415*/
66416static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
66417 int nEntry; /* Number of entries on one ptrmap page */
66418 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
66419 Pgno nFin; /* Return value */
66420
66421 nEntry = pBt->usableSize/5;
66422 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
66423 nFin = nOrig - nFree - nPtrmap;
66424 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
66425 nFin--;
66426 }
66427 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
66428 nFin--;
66429 }
66430
66431 return nFin;
66432}
66433
66434/*
66435** A write-transaction must be opened before calling this function.
66436** It performs a single unit of work towards an incremental vacuum.
66437**
66438** If the incremental vacuum is finished after this function has run,
66439** SQLITE_DONE is returned. If it is not finished, but no error occurred,
66440** SQLITE_OK is returned. Otherwise an SQLite error code.
66441*/
66442SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
66443 int rc;
66444 BtShared *pBt = p->pBt;
66445
66446 sqlite3BtreeEnter(p);
66447 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
66448 if( !pBt->autoVacuum ){
66449 rc = SQLITE_DONE;
66450 }else{
66451 Pgno nOrig = btreePagecount(pBt);
66452 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
66453 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
66454
66455 if( nOrig<nFin ){
66456 rc = SQLITE_CORRUPT_BKPT;
66457 }else if( nFree>0 ){
66458 rc = saveAllCursors(pBt, 0, 0);
66459 if( rc==SQLITE_OK ){
66460 invalidateAllOverflowCache(pBt);
66461 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
66462 }
66463 if( rc==SQLITE_OK ){
66464 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
66465 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
66466 }
66467 }else{
66468 rc = SQLITE_DONE;
66469 }
66470 }
66471 sqlite3BtreeLeave(p);
66472 return rc;
66473}
66474
66475/*
66476** This routine is called prior to sqlite3PagerCommit when a transaction
66477** is committed for an auto-vacuum database.
66478**
66479** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
66480** the database file should be truncated to during the commit process.
66481** i.e. the database has been reorganized so that only the first *pnTrunc
66482** pages are in use.
66483*/
66484static int autoVacuumCommit(BtShared *pBt){
66485 int rc = SQLITE_OK;
66486 Pager *pPager = pBt->pPager;
66487 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
66488
66489 assert( sqlite3_mutex_held(pBt->mutex) );
66490 invalidateAllOverflowCache(pBt);
66491 assert(pBt->autoVacuum);
66492 if( !pBt->incrVacuum ){
66493 Pgno nFin; /* Number of pages in database after autovacuuming */
66494 Pgno nFree; /* Number of pages on the freelist initially */
66495 Pgno iFree; /* The next page to be freed */
66496 Pgno nOrig; /* Database size before freeing */
66497
66498 nOrig = btreePagecount(pBt);
66499 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
66500 /* It is not possible to create a database for which the final page
66501 ** is either a pointer-map page or the pending-byte page. If one
66502 ** is encountered, this indicates corruption.
66503 */
66504 return SQLITE_CORRUPT_BKPT;
66505 }
66506
66507 nFree = get4byte(&pBt->pPage1->aData[36]);
66508 nFin = finalDbSize(pBt, nOrig, nFree);
66509 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
66510 if( nFin<nOrig ){
66511 rc = saveAllCursors(pBt, 0, 0);
66512 }
66513 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
66514 rc = incrVacuumStep(pBt, nFin, iFree, 1);
66515 }
66516 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
66517 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
66518 put4byte(&pBt->pPage1->aData[32], 0);
66519 put4byte(&pBt->pPage1->aData[36], 0);
66520 put4byte(&pBt->pPage1->aData[28], nFin);
66521 pBt->bDoTruncate = 1;
66522 pBt->nPage = nFin;
66523 }
66524 if( rc!=SQLITE_OK ){
66525 sqlite3PagerRollback(pPager);
66526 }
66527 }
66528
66529 assert( nRef>=sqlite3PagerRefcount(pPager) );
66530 return rc;
66531}
66532
66533#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
66534# define setChildPtrmaps(x) SQLITE_OK
66535#endif
66536
66537/*
66538** This routine does the first phase of a two-phase commit. This routine
66539** causes a rollback journal to be created (if it does not already exist)
66540** and populated with enough information so that if a power loss occurs
66541** the database can be restored to its original state by playing back
66542** the journal. Then the contents of the journal are flushed out to
66543** the disk. After the journal is safely on oxide, the changes to the
66544** database are written into the database file and flushed to oxide.
66545** At the end of this call, the rollback journal still exists on the
66546** disk and we are still holding all locks, so the transaction has not
66547** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
66548** commit process.
66549**
66550** This call is a no-op if no write-transaction is currently active on pBt.
66551**
66552** Otherwise, sync the database file for the btree pBt. zMaster points to
66553** the name of a master journal file that should be written into the
66554** individual journal file, or is NULL, indicating no master journal file
66555** (single database transaction).
66556**
66557** When this is called, the master journal should already have been
66558** created, populated with this journal pointer and synced to disk.
66559**
66560** Once this is routine has returned, the only thing required to commit
66561** the write-transaction for this database file is to delete the journal.
66562*/
66563SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
66564 int rc = SQLITE_OK;
66565 if( p->inTrans==TRANS_WRITE ){
66566 BtShared *pBt = p->pBt;
66567 sqlite3BtreeEnter(p);
66568#ifndef SQLITE_OMIT_AUTOVACUUM
66569 if( pBt->autoVacuum ){
66570 rc = autoVacuumCommit(pBt);
66571 if( rc!=SQLITE_OK ){
66572 sqlite3BtreeLeave(p);
66573 return rc;
66574 }
66575 }
66576 if( pBt->bDoTruncate ){
66577 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
66578 }
66579#endif
66580 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
66581 sqlite3BtreeLeave(p);
66582 }
66583 return rc;
66584}
66585
66586/*
66587** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
66588** at the conclusion of a transaction.
66589*/
66590static void btreeEndTransaction(Btree *p){
66591 BtShared *pBt = p->pBt;
66592 sqlite3 *db = p->db;
66593 assert( sqlite3BtreeHoldsMutex(p) );
66594
66595#ifndef SQLITE_OMIT_AUTOVACUUM
66596 pBt->bDoTruncate = 0;
66597#endif
66598 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
66599 /* If there are other active statements that belong to this database
66600 ** handle, downgrade to a read-only transaction. The other statements
66601 ** may still be reading from the database. */
66602 downgradeAllSharedCacheTableLocks(p);
66603 p->inTrans = TRANS_READ;
66604 }else{
66605 /* If the handle had any kind of transaction open, decrement the
66606 ** transaction count of the shared btree. If the transaction count
66607 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
66608 ** call below will unlock the pager. */
66609 if( p->inTrans!=TRANS_NONE ){
66610 clearAllSharedCacheTableLocks(p);
66611 pBt->nTransaction--;
66612 if( 0==pBt->nTransaction ){
66613 pBt->inTransaction = TRANS_NONE;
66614 }
66615 }
66616
66617 /* Set the current transaction state to TRANS_NONE and unlock the
66618 ** pager if this call closed the only read or write transaction. */
66619 p->inTrans = TRANS_NONE;
66620 unlockBtreeIfUnused(pBt);
66621 }
66622
66623 btreeIntegrity(p);
66624}
66625
66626/*
66627** Commit the transaction currently in progress.
66628**
66629** This routine implements the second phase of a 2-phase commit. The
66630** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
66631** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
66632** routine did all the work of writing information out to disk and flushing the
66633** contents so that they are written onto the disk platter. All this
66634** routine has to do is delete or truncate or zero the header in the
66635** the rollback journal (which causes the transaction to commit) and
66636** drop locks.
66637**
66638** Normally, if an error occurs while the pager layer is attempting to
66639** finalize the underlying journal file, this function returns an error and
66640** the upper layer will attempt a rollback. However, if the second argument
66641** is non-zero then this b-tree transaction is part of a multi-file
66642** transaction. In this case, the transaction has already been committed
66643** (by deleting a master journal file) and the caller will ignore this
66644** functions return code. So, even if an error occurs in the pager layer,
66645** reset the b-tree objects internal state to indicate that the write
66646** transaction has been closed. This is quite safe, as the pager will have
66647** transitioned to the error state.
66648**
66649** This will release the write lock on the database file. If there
66650** are no active cursors, it also releases the read lock.
66651*/
66652SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
66653
66654 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
66655 sqlite3BtreeEnter(p);
66656 btreeIntegrity(p);
66657
66658 /* If the handle has a write-transaction open, commit the shared-btrees
66659 ** transaction and set the shared state to TRANS_READ.
66660 */
66661 if( p->inTrans==TRANS_WRITE ){
66662 int rc;
66663 BtShared *pBt = p->pBt;
66664 assert( pBt->inTransaction==TRANS_WRITE );
66665 assert( pBt->nTransaction>0 );
66666 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
66667 if( rc!=SQLITE_OK && bCleanup==0 ){
66668 sqlite3BtreeLeave(p);
66669 return rc;
66670 }
66671 p->iDataVersion--; /* Compensate for pPager->iDataVersion++; */
66672 pBt->inTransaction = TRANS_READ;
66673 btreeClearHasContent(pBt);
66674 }
66675
66676 btreeEndTransaction(p);
66677 sqlite3BtreeLeave(p);
66678 return SQLITE_OK;
66679}
66680
66681/*
66682** Do both phases of a commit.
66683*/
66684SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
66685 int rc;
66686 sqlite3BtreeEnter(p);
66687 rc = sqlite3BtreeCommitPhaseOne(p, 0);
66688 if( rc==SQLITE_OK ){
66689 rc = sqlite3BtreeCommitPhaseTwo(p, 0);
66690 }
66691 sqlite3BtreeLeave(p);
66692 return rc;
66693}
66694
66695/*
66696** This routine sets the state to CURSOR_FAULT and the error
66697** code to errCode for every cursor on any BtShared that pBtree
66698** references. Or if the writeOnly flag is set to 1, then only
66699** trip write cursors and leave read cursors unchanged.
66700**
66701** Every cursor is a candidate to be tripped, including cursors
66702** that belong to other database connections that happen to be
66703** sharing the cache with pBtree.
66704**
66705** This routine gets called when a rollback occurs. If the writeOnly
66706** flag is true, then only write-cursors need be tripped - read-only
66707** cursors save their current positions so that they may continue
66708** following the rollback. Or, if writeOnly is false, all cursors are
66709** tripped. In general, writeOnly is false if the transaction being
66710** rolled back modified the database schema. In this case b-tree root
66711** pages may be moved or deleted from the database altogether, making
66712** it unsafe for read cursors to continue.
66713**
66714** If the writeOnly flag is true and an error is encountered while
66715** saving the current position of a read-only cursor, all cursors,
66716** including all read-cursors are tripped.
66717**
66718** SQLITE_OK is returned if successful, or if an error occurs while
66719** saving a cursor position, an SQLite error code.
66720*/
66721SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
66722 BtCursor *p;
66723 int rc = SQLITE_OK;
66724
66725 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
66726 if( pBtree ){
66727 sqlite3BtreeEnter(pBtree);
66728 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
66729 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
66730 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
66731 rc = saveCursorPosition(p);
66732 if( rc!=SQLITE_OK ){
66733 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
66734 break;
66735 }
66736 }
66737 }else{
66738 sqlite3BtreeClearCursor(p);
66739 p->eState = CURSOR_FAULT;
66740 p->skipNext = errCode;
66741 }
66742 btreeReleaseAllCursorPages(p);
66743 }
66744 sqlite3BtreeLeave(pBtree);
66745 }
66746 return rc;
66747}
66748
66749/*
66750** Rollback the transaction in progress.
66751**
66752** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
66753** Only write cursors are tripped if writeOnly is true but all cursors are
66754** tripped if writeOnly is false. Any attempt to use
66755** a tripped cursor will result in an error.
66756**
66757** This will release the write lock on the database file. If there
66758** are no active cursors, it also releases the read lock.
66759*/
66760SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
66761 int rc;
66762 BtShared *pBt = p->pBt;
66763 MemPage *pPage1;
66764
66765 assert( writeOnly==1 || writeOnly==0 );
66766 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
66767 sqlite3BtreeEnter(p);
66768 if( tripCode==SQLITE_OK ){
66769 rc = tripCode = saveAllCursors(pBt, 0, 0);
66770 if( rc ) writeOnly = 0;
66771 }else{
66772 rc = SQLITE_OK;
66773 }
66774 if( tripCode ){
66775 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
66776 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) );
66777 if( rc2!=SQLITE_OK ) rc = rc2;
66778 }
66779 btreeIntegrity(p);
66780
66781 if( p->inTrans==TRANS_WRITE ){
66782 int rc2;
66783
66784 assert( TRANS_WRITE==pBt->inTransaction );
66785 rc2 = sqlite3PagerRollback(pBt->pPager);
66786 if( rc2!=SQLITE_OK ){
66787 rc = rc2;
66788 }
66789
66790 /* The rollback may have destroyed the pPage1->aData value. So
66791 ** call btreeGetPage() on page 1 again to make
66792 ** sure pPage1->aData is set correctly. */
66793 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
66794 int nPage = get4byte(28+(u8*)pPage1->aData);
66795 testcase( nPage==0 );
66796 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
66797 testcase( pBt->nPage!=nPage );
66798 pBt->nPage = nPage;
66799 releasePageOne(pPage1);
66800 }
66801 assert( countValidCursors(pBt, 1)==0 );
66802 pBt->inTransaction = TRANS_READ;
66803 btreeClearHasContent(pBt);
66804 }
66805
66806 btreeEndTransaction(p);
66807 sqlite3BtreeLeave(p);
66808 return rc;
66809}
66810
66811/*
66812** Start a statement subtransaction. The subtransaction can be rolled
66813** back independently of the main transaction. You must start a transaction
66814** before starting a subtransaction. The subtransaction is ended automatically
66815** if the main transaction commits or rolls back.
66816**
66817** Statement subtransactions are used around individual SQL statements
66818** that are contained within a BEGIN...COMMIT block. If a constraint
66819** error occurs within the statement, the effect of that one statement
66820** can be rolled back without having to rollback the entire transaction.
66821**
66822** A statement sub-transaction is implemented as an anonymous savepoint. The
66823** value passed as the second parameter is the total number of savepoints,
66824** including the new anonymous savepoint, open on the B-Tree. i.e. if there
66825** are no active savepoints and no other statement-transactions open,
66826** iStatement is 1. This anonymous savepoint can be released or rolled back
66827** using the sqlite3BtreeSavepoint() function.
66828*/
66829SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
66830 int rc;
66831 BtShared *pBt = p->pBt;
66832 sqlite3BtreeEnter(p);
66833 assert( p->inTrans==TRANS_WRITE );
66834 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
66835 assert( iStatement>0 );
66836 assert( iStatement>p->db->nSavepoint );
66837 assert( pBt->inTransaction==TRANS_WRITE );
66838 /* At the pager level, a statement transaction is a savepoint with
66839 ** an index greater than all savepoints created explicitly using
66840 ** SQL statements. It is illegal to open, release or rollback any
66841 ** such savepoints while the statement transaction savepoint is active.
66842 */
66843 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
66844 sqlite3BtreeLeave(p);
66845 return rc;
66846}
66847
66848/*
66849** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
66850** or SAVEPOINT_RELEASE. This function either releases or rolls back the
66851** savepoint identified by parameter iSavepoint, depending on the value
66852** of op.
66853**
66854** Normally, iSavepoint is greater than or equal to zero. However, if op is
66855** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
66856** contents of the entire transaction are rolled back. This is different
66857** from a normal transaction rollback, as no locks are released and the
66858** transaction remains open.
66859*/
66860SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
66861 int rc = SQLITE_OK;
66862 if( p && p->inTrans==TRANS_WRITE ){
66863 BtShared *pBt = p->pBt;
66864 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
66865 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
66866 sqlite3BtreeEnter(p);
66867 if( op==SAVEPOINT_ROLLBACK ){
66868 rc = saveAllCursors(pBt, 0, 0);
66869 }
66870 if( rc==SQLITE_OK ){
66871 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
66872 }
66873 if( rc==SQLITE_OK ){
66874 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
66875 pBt->nPage = 0;
66876 }
66877 rc = newDatabase(pBt);
66878 pBt->nPage = get4byte(28 + pBt->pPage1->aData);
66879
66880 /* The database size was written into the offset 28 of the header
66881 ** when the transaction started, so we know that the value at offset
66882 ** 28 is nonzero. */
66883 assert( pBt->nPage>0 );
66884 }
66885 sqlite3BtreeLeave(p);
66886 }
66887 return rc;
66888}
66889
66890/*
66891** Create a new cursor for the BTree whose root is on the page
66892** iTable. If a read-only cursor is requested, it is assumed that
66893** the caller already has at least a read-only transaction open
66894** on the database already. If a write-cursor is requested, then
66895** the caller is assumed to have an open write transaction.
66896**
66897** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
66898** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
66899** can be used for reading or for writing if other conditions for writing
66900** are also met. These are the conditions that must be met in order
66901** for writing to be allowed:
66902**
66903** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
66904**
66905** 2: Other database connections that share the same pager cache
66906** but which are not in the READ_UNCOMMITTED state may not have
66907** cursors open with wrFlag==0 on the same table. Otherwise
66908** the changes made by this write cursor would be visible to
66909** the read cursors in the other database connection.
66910**
66911** 3: The database must be writable (not on read-only media)
66912**
66913** 4: There must be an active transaction.
66914**
66915** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
66916** is set. If FORDELETE is set, that is a hint to the implementation that
66917** this cursor will only be used to seek to and delete entries of an index
66918** as part of a larger DELETE statement. The FORDELETE hint is not used by
66919** this implementation. But in a hypothetical alternative storage engine
66920** in which index entries are automatically deleted when corresponding table
66921** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
66922** operations on this cursor can be no-ops and all READ operations can
66923** return a null row (2-bytes: 0x01 0x00).
66924**
66925** No checking is done to make sure that page iTable really is the
66926** root page of a b-tree. If it is not, then the cursor acquired
66927** will not work correctly.
66928**
66929** It is assumed that the sqlite3BtreeCursorZero() has been called
66930** on pCur to initialize the memory space prior to invoking this routine.
66931*/
66932static int btreeCursor(
66933 Btree *p, /* The btree */
66934 int iTable, /* Root page of table to open */
66935 int wrFlag, /* 1 to write. 0 read-only */
66936 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
66937 BtCursor *pCur /* Space for new cursor */
66938){
66939 BtShared *pBt = p->pBt; /* Shared b-tree handle */
66940 BtCursor *pX; /* Looping over other all cursors */
66941
66942 assert( sqlite3BtreeHoldsMutex(p) );
66943 assert( wrFlag==0
66944 || wrFlag==BTREE_WRCSR
66945 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)
66946 );
66947
66948 /* The following assert statements verify that if this is a sharable
66949 ** b-tree database, the connection is holding the required table locks,
66950 ** and that no other connection has any open cursor that conflicts with
66951 ** this lock. */
66952 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) );
66953 assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
66954
66955 /* Assert that the caller has opened the required transaction. */
66956 assert( p->inTrans>TRANS_NONE );
66957 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
66958 assert( pBt->pPage1 && pBt->pPage1->aData );
66959 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
66960
66961 if( wrFlag ){
66962 allocateTempSpace(pBt);
66963 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
66964 }
66965 if( iTable==1 && btreePagecount(pBt)==0 ){
66966 assert( wrFlag==0 );
66967 iTable = 0;
66968 }
66969
66970 /* Now that no other errors can occur, finish filling in the BtCursor
66971 ** variables and link the cursor into the BtShared list. */
66972 pCur->pgnoRoot = (Pgno)iTable;
66973 pCur->iPage = -1;
66974 pCur->pKeyInfo = pKeyInfo;
66975 pCur->pBtree = p;
66976 pCur->pBt = pBt;
66977 pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0;
66978 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
66979 /* If there are two or more cursors on the same btree, then all such
66980 ** cursors *must* have the BTCF_Multiple flag set. */
66981 for(pX=pBt->pCursor; pX; pX=pX->pNext){
66982 if( pX->pgnoRoot==(Pgno)iTable ){
66983 pX->curFlags |= BTCF_Multiple;
66984 pCur->curFlags |= BTCF_Multiple;
66985 }
66986 }
66987 pCur->pNext = pBt->pCursor;
66988 pBt->pCursor = pCur;
66989 pCur->eState = CURSOR_INVALID;
66990 return SQLITE_OK;
66991}
66992SQLITE_PRIVATE int sqlite3BtreeCursor(
66993 Btree *p, /* The btree */
66994 int iTable, /* Root page of table to open */
66995 int wrFlag, /* 1 to write. 0 read-only */
66996 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
66997 BtCursor *pCur /* Write new cursor here */
66998){
66999 int rc;
67000 if( iTable<1 ){
67001 rc = SQLITE_CORRUPT_BKPT;
67002 }else{
67003 sqlite3BtreeEnter(p);
67004 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
67005 sqlite3BtreeLeave(p);
67006 }
67007 return rc;
67008}
67009
67010/*
67011** Return the size of a BtCursor object in bytes.
67012**
67013** This interfaces is needed so that users of cursors can preallocate
67014** sufficient storage to hold a cursor. The BtCursor object is opaque
67015** to users so they cannot do the sizeof() themselves - they must call
67016** this routine.
67017*/
67018SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
67019 return ROUND8(sizeof(BtCursor));
67020}
67021
67022/*
67023** Initialize memory that will be converted into a BtCursor object.
67024**
67025** The simple approach here would be to memset() the entire object
67026** to zero. But it turns out that the apPage[] and aiIdx[] arrays
67027** do not need to be zeroed and they are large, so we can save a lot
67028** of run-time by skipping the initialization of those elements.
67029*/
67030SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
67031 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
67032}
67033
67034/*
67035** Close a cursor. The read lock on the database file is released
67036** when the last cursor is closed.
67037*/
67038SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
67039 Btree *pBtree = pCur->pBtree;
67040 if( pBtree ){
67041 BtShared *pBt = pCur->pBt;
67042 sqlite3BtreeEnter(pBtree);
67043 assert( pBt->pCursor!=0 );
67044 if( pBt->pCursor==pCur ){
67045 pBt->pCursor = pCur->pNext;
67046 }else{
67047 BtCursor *pPrev = pBt->pCursor;
67048 do{
67049 if( pPrev->pNext==pCur ){
67050 pPrev->pNext = pCur->pNext;
67051 break;
67052 }
67053 pPrev = pPrev->pNext;
67054 }while( ALWAYS(pPrev) );
67055 }
67056 btreeReleaseAllCursorPages(pCur);
67057 unlockBtreeIfUnused(pBt);
67058 sqlite3_free(pCur->aOverflow);
67059 sqlite3_free(pCur->pKey);
67060 sqlite3BtreeLeave(pBtree);
67061 }
67062 return SQLITE_OK;
67063}
67064
67065/*
67066** Make sure the BtCursor* given in the argument has a valid
67067** BtCursor.info structure. If it is not already valid, call
67068** btreeParseCell() to fill it in.
67069**
67070** BtCursor.info is a cache of the information in the current cell.
67071** Using this cache reduces the number of calls to btreeParseCell().
67072*/
67073#ifndef NDEBUG
67074 static int cellInfoEqual(CellInfo *a, CellInfo *b){
67075 if( a->nKey!=b->nKey ) return 0;
67076 if( a->pPayload!=b->pPayload ) return 0;
67077 if( a->nPayload!=b->nPayload ) return 0;
67078 if( a->nLocal!=b->nLocal ) return 0;
67079 if( a->nSize!=b->nSize ) return 0;
67080 return 1;
67081 }
67082 static void assertCellInfo(BtCursor *pCur){
67083 CellInfo info;
67084 memset(&info, 0, sizeof(info));
67085 btreeParseCell(pCur->pPage, pCur->ix, &info);
67086 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
67087 }
67088#else
67089 #define assertCellInfo(x)
67090#endif
67091static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
67092 if( pCur->info.nSize==0 ){
67093 pCur->curFlags |= BTCF_ValidNKey;
67094 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
67095 }else{
67096 assertCellInfo(pCur);
67097 }
67098}
67099
67100#ifndef NDEBUG /* The next routine used only within assert() statements */
67101/*
67102** Return true if the given BtCursor is valid. A valid cursor is one
67103** that is currently pointing to a row in a (non-empty) table.
67104** This is a verification routine is used only within assert() statements.
67105*/
67106SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
67107 return pCur && pCur->eState==CURSOR_VALID;
67108}
67109#endif /* NDEBUG */
67110SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
67111 assert( pCur!=0 );
67112 return pCur->eState==CURSOR_VALID;
67113}
67114
67115/*
67116** Return the value of the integer key or "rowid" for a table btree.
67117** This routine is only valid for a cursor that is pointing into a
67118** ordinary table btree. If the cursor points to an index btree or
67119** is invalid, the result of this routine is undefined.
67120*/
67121SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
67122 assert( cursorHoldsMutex(pCur) );
67123 assert( pCur->eState==CURSOR_VALID );
67124 assert( pCur->curIntKey );
67125 getCellInfo(pCur);
67126 return pCur->info.nKey;
67127}
67128
67129#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
67130/*
67131** Return the offset into the database file for the start of the
67132** payload to which the cursor is pointing.
67133*/
67134SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor *pCur){
67135 assert( cursorHoldsMutex(pCur) );
67136 assert( pCur->eState==CURSOR_VALID );
67137 getCellInfo(pCur);
67138 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
67139 (i64)(pCur->info.pPayload - pCur->pPage->aData);
67140}
67141#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
67142
67143/*
67144** Return the number of bytes of payload for the entry that pCur is
67145** currently pointing to. For table btrees, this will be the amount
67146** of data. For index btrees, this will be the size of the key.
67147**
67148** The caller must guarantee that the cursor is pointing to a non-NULL
67149** valid entry. In other words, the calling procedure must guarantee
67150** that the cursor has Cursor.eState==CURSOR_VALID.
67151*/
67152SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor *pCur){
67153 assert( cursorHoldsMutex(pCur) );
67154 assert( pCur->eState==CURSOR_VALID );
67155 getCellInfo(pCur);
67156 return pCur->info.nPayload;
67157}
67158
67159/*
67160** Given the page number of an overflow page in the database (parameter
67161** ovfl), this function finds the page number of the next page in the
67162** linked list of overflow pages. If possible, it uses the auto-vacuum
67163** pointer-map data instead of reading the content of page ovfl to do so.
67164**
67165** If an error occurs an SQLite error code is returned. Otherwise:
67166**
67167** The page number of the next overflow page in the linked list is
67168** written to *pPgnoNext. If page ovfl is the last page in its linked
67169** list, *pPgnoNext is set to zero.
67170**
67171** If ppPage is not NULL, and a reference to the MemPage object corresponding
67172** to page number pOvfl was obtained, then *ppPage is set to point to that
67173** reference. It is the responsibility of the caller to call releasePage()
67174** on *ppPage to free the reference. In no reference was obtained (because
67175** the pointer-map was used to obtain the value for *pPgnoNext), then
67176** *ppPage is set to zero.
67177*/
67178static int getOverflowPage(
67179 BtShared *pBt, /* The database file */
67180 Pgno ovfl, /* Current overflow page number */
67181 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
67182 Pgno *pPgnoNext /* OUT: Next overflow page number */
67183){
67184 Pgno next = 0;
67185 MemPage *pPage = 0;
67186 int rc = SQLITE_OK;
67187
67188 assert( sqlite3_mutex_held(pBt->mutex) );
67189 assert(pPgnoNext);
67190
67191#ifndef SQLITE_OMIT_AUTOVACUUM
67192 /* Try to find the next page in the overflow list using the
67193 ** autovacuum pointer-map pages. Guess that the next page in
67194 ** the overflow list is page number (ovfl+1). If that guess turns
67195 ** out to be wrong, fall back to loading the data of page
67196 ** number ovfl to determine the next page number.
67197 */
67198 if( pBt->autoVacuum ){
67199 Pgno pgno;
67200 Pgno iGuess = ovfl+1;
67201 u8 eType;
67202
67203 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
67204 iGuess++;
67205 }
67206
67207 if( iGuess<=btreePagecount(pBt) ){
67208 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
67209 if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
67210 next = iGuess;
67211 rc = SQLITE_DONE;
67212 }
67213 }
67214 }
67215#endif
67216
67217 assert( next==0 || rc==SQLITE_DONE );
67218 if( rc==SQLITE_OK ){
67219 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
67220 assert( rc==SQLITE_OK || pPage==0 );
67221 if( rc==SQLITE_OK ){
67222 next = get4byte(pPage->aData);
67223 }
67224 }
67225
67226 *pPgnoNext = next;
67227 if( ppPage ){
67228 *ppPage = pPage;
67229 }else{
67230 releasePage(pPage);
67231 }
67232 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
67233}
67234
67235/*
67236** Copy data from a buffer to a page, or from a page to a buffer.
67237**
67238** pPayload is a pointer to data stored on database page pDbPage.
67239** If argument eOp is false, then nByte bytes of data are copied
67240** from pPayload to the buffer pointed at by pBuf. If eOp is true,
67241** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
67242** of data are copied from the buffer pBuf to pPayload.
67243**
67244** SQLITE_OK is returned on success, otherwise an error code.
67245*/
67246static int copyPayload(
67247 void *pPayload, /* Pointer to page data */
67248 void *pBuf, /* Pointer to buffer */
67249 int nByte, /* Number of bytes to copy */
67250 int eOp, /* 0 -> copy from page, 1 -> copy to page */
67251 DbPage *pDbPage /* Page containing pPayload */
67252){
67253 if( eOp ){
67254 /* Copy data from buffer to page (a write operation) */
67255 int rc = sqlite3PagerWrite(pDbPage);
67256 if( rc!=SQLITE_OK ){
67257 return rc;
67258 }
67259 memcpy(pPayload, pBuf, nByte);
67260 }else{
67261 /* Copy data from page to buffer (a read operation) */
67262 memcpy(pBuf, pPayload, nByte);
67263 }
67264 return SQLITE_OK;
67265}
67266
67267/*
67268** This function is used to read or overwrite payload information
67269** for the entry that the pCur cursor is pointing to. The eOp
67270** argument is interpreted as follows:
67271**
67272** 0: The operation is a read. Populate the overflow cache.
67273** 1: The operation is a write. Populate the overflow cache.
67274**
67275** A total of "amt" bytes are read or written beginning at "offset".
67276** Data is read to or from the buffer pBuf.
67277**
67278** The content being read or written might appear on the main page
67279** or be scattered out on multiple overflow pages.
67280**
67281** If the current cursor entry uses one or more overflow pages
67282** this function may allocate space for and lazily populate
67283** the overflow page-list cache array (BtCursor.aOverflow).
67284** Subsequent calls use this cache to make seeking to the supplied offset
67285** more efficient.
67286**
67287** Once an overflow page-list cache has been allocated, it must be
67288** invalidated if some other cursor writes to the same table, or if
67289** the cursor is moved to a different row. Additionally, in auto-vacuum
67290** mode, the following events may invalidate an overflow page-list cache.
67291**
67292** * An incremental vacuum,
67293** * A commit in auto_vacuum="full" mode,
67294** * Creating a table (may require moving an overflow page).
67295*/
67296static int accessPayload(
67297 BtCursor *pCur, /* Cursor pointing to entry to read from */
67298 u32 offset, /* Begin reading this far into payload */
67299 u32 amt, /* Read this many bytes */
67300 unsigned char *pBuf, /* Write the bytes into this buffer */
67301 int eOp /* zero to read. non-zero to write. */
67302){
67303 unsigned char *aPayload;
67304 int rc = SQLITE_OK;
67305 int iIdx = 0;
67306 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
67307 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
67308#ifdef SQLITE_DIRECT_OVERFLOW_READ
67309 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
67310#endif
67311
67312 assert( pPage );
67313 assert( eOp==0 || eOp==1 );
67314 assert( pCur->eState==CURSOR_VALID );
67315 assert( pCur->ix<pPage->nCell );
67316 assert( cursorHoldsMutex(pCur) );
67317
67318 getCellInfo(pCur);
67319 aPayload = pCur->info.pPayload;
67320 assert( offset+amt <= pCur->info.nPayload );
67321
67322 assert( aPayload > pPage->aData );
67323 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
67324 /* Trying to read or write past the end of the data is an error. The
67325 ** conditional above is really:
67326 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
67327 ** but is recast into its current form to avoid integer overflow problems
67328 */
67329 return SQLITE_CORRUPT_PAGE(pPage);
67330 }
67331
67332 /* Check if data must be read/written to/from the btree page itself. */
67333 if( offset<pCur->info.nLocal ){
67334 int a = amt;
67335 if( a+offset>pCur->info.nLocal ){
67336 a = pCur->info.nLocal - offset;
67337 }
67338 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
67339 offset = 0;
67340 pBuf += a;
67341 amt -= a;
67342 }else{
67343 offset -= pCur->info.nLocal;
67344 }
67345
67346
67347 if( rc==SQLITE_OK && amt>0 ){
67348 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
67349 Pgno nextPage;
67350
67351 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
67352
67353 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
67354 **
67355 ** The aOverflow[] array is sized at one entry for each overflow page
67356 ** in the overflow chain. The page number of the first overflow page is
67357 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
67358 ** means "not yet known" (the cache is lazily populated).
67359 */
67360 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
67361 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
67362 if( pCur->aOverflow==0
67363 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
67364 ){
67365 Pgno *aNew = (Pgno*)sqlite3Realloc(
67366 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
67367 );
67368 if( aNew==0 ){
67369 return SQLITE_NOMEM_BKPT;
67370 }else{
67371 pCur->aOverflow = aNew;
67372 }
67373 }
67374 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
67375 pCur->curFlags |= BTCF_ValidOvfl;
67376 }else{
67377 /* If the overflow page-list cache has been allocated and the
67378 ** entry for the first required overflow page is valid, skip
67379 ** directly to it.
67380 */
67381 if( pCur->aOverflow[offset/ovflSize] ){
67382 iIdx = (offset/ovflSize);
67383 nextPage = pCur->aOverflow[iIdx];
67384 offset = (offset%ovflSize);
67385 }
67386 }
67387
67388 assert( rc==SQLITE_OK && amt>0 );
67389 while( nextPage ){
67390 /* If required, populate the overflow page-list cache. */
67391 assert( pCur->aOverflow[iIdx]==0
67392 || pCur->aOverflow[iIdx]==nextPage
67393 || CORRUPT_DB );
67394 pCur->aOverflow[iIdx] = nextPage;
67395
67396 if( offset>=ovflSize ){
67397 /* The only reason to read this page is to obtain the page
67398 ** number for the next page in the overflow chain. The page
67399 ** data is not required. So first try to lookup the overflow
67400 ** page-list cache, if any, then fall back to the getOverflowPage()
67401 ** function.
67402 */
67403 assert( pCur->curFlags & BTCF_ValidOvfl );
67404 assert( pCur->pBtree->db==pBt->db );
67405 if( pCur->aOverflow[iIdx+1] ){
67406 nextPage = pCur->aOverflow[iIdx+1];
67407 }else{
67408 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
67409 }
67410 offset -= ovflSize;
67411 }else{
67412 /* Need to read this page properly. It contains some of the
67413 ** range of data that is being read (eOp==0) or written (eOp!=0).
67414 */
67415#ifdef SQLITE_DIRECT_OVERFLOW_READ
67416 sqlite3_file *fd; /* File from which to do direct overflow read */
67417#endif
67418 int a = amt;
67419 if( a + offset > ovflSize ){
67420 a = ovflSize - offset;
67421 }
67422
67423#ifdef SQLITE_DIRECT_OVERFLOW_READ
67424 /* If all the following are true:
67425 **
67426 ** 1) this is a read operation, and
67427 ** 2) data is required from the start of this overflow page, and
67428 ** 3) there is no open write-transaction, and
67429 ** 4) the database is file-backed, and
67430 ** 5) the page is not in the WAL file
67431 ** 6) at least 4 bytes have already been read into the output buffer
67432 **
67433 ** then data can be read directly from the database file into the
67434 ** output buffer, bypassing the page-cache altogether. This speeds
67435 ** up loading large records that span many overflow pages.
67436 */
67437 if( eOp==0 /* (1) */
67438 && offset==0 /* (2) */
67439 && pBt->inTransaction==TRANS_READ /* (3) */
67440 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (4) */
67441 && 0==sqlite3PagerUseWal(pBt->pPager, nextPage) /* (5) */
67442 && &pBuf[-4]>=pBufStart /* (6) */
67443 ){
67444 u8 aSave[4];
67445 u8 *aWrite = &pBuf[-4];
67446 assert( aWrite>=pBufStart ); /* due to (6) */
67447 memcpy(aSave, aWrite, 4);
67448 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
67449 nextPage = get4byte(aWrite);
67450 memcpy(aWrite, aSave, 4);
67451 }else
67452#endif
67453
67454 {
67455 DbPage *pDbPage;
67456 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
67457 (eOp==0 ? PAGER_GET_READONLY : 0)
67458 );
67459 if( rc==SQLITE_OK ){
67460 aPayload = sqlite3PagerGetData(pDbPage);
67461 nextPage = get4byte(aPayload);
67462 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
67463 sqlite3PagerUnref(pDbPage);
67464 offset = 0;
67465 }
67466 }
67467 amt -= a;
67468 if( amt==0 ) return rc;
67469 pBuf += a;
67470 }
67471 if( rc ) break;
67472 iIdx++;
67473 }
67474 }
67475
67476 if( rc==SQLITE_OK && amt>0 ){
67477 /* Overflow chain ends prematurely */
67478 return SQLITE_CORRUPT_PAGE(pPage);
67479 }
67480 return rc;
67481}
67482
67483/*
67484** Read part of the payload for the row at which that cursor pCur is currently
67485** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
67486** begins at "offset".
67487**
67488** pCur can be pointing to either a table or an index b-tree.
67489** If pointing to a table btree, then the content section is read. If
67490** pCur is pointing to an index b-tree then the key section is read.
67491**
67492** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
67493** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
67494** cursor might be invalid or might need to be restored before being read.
67495**
67496** Return SQLITE_OK on success or an error code if anything goes
67497** wrong. An error is returned if "offset+amt" is larger than
67498** the available payload.
67499*/
67500SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
67501 assert( cursorHoldsMutex(pCur) );
67502 assert( pCur->eState==CURSOR_VALID );
67503 assert( pCur->iPage>=0 && pCur->pPage );
67504 assert( pCur->ix<pCur->pPage->nCell );
67505 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
67506}
67507
67508/*
67509** This variant of sqlite3BtreePayload() works even if the cursor has not
67510** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
67511** interface.
67512*/
67513#ifndef SQLITE_OMIT_INCRBLOB
67514static SQLITE_NOINLINE int accessPayloadChecked(
67515 BtCursor *pCur,
67516 u32 offset,
67517 u32 amt,
67518 void *pBuf
67519){
67520 int rc;
67521 if ( pCur->eState==CURSOR_INVALID ){
67522 return SQLITE_ABORT;
67523 }
67524 assert( cursorOwnsBtShared(pCur) );
67525 rc = btreeRestoreCursorPosition(pCur);
67526 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
67527}
67528SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
67529 if( pCur->eState==CURSOR_VALID ){
67530 assert( cursorOwnsBtShared(pCur) );
67531 return accessPayload(pCur, offset, amt, pBuf, 0);
67532 }else{
67533 return accessPayloadChecked(pCur, offset, amt, pBuf);
67534 }
67535}
67536#endif /* SQLITE_OMIT_INCRBLOB */
67537
67538/*
67539** Return a pointer to payload information from the entry that the
67540** pCur cursor is pointing to. The pointer is to the beginning of
67541** the key if index btrees (pPage->intKey==0) and is the data for
67542** table btrees (pPage->intKey==1). The number of bytes of available
67543** key/data is written into *pAmt. If *pAmt==0, then the value
67544** returned will not be a valid pointer.
67545**
67546** This routine is an optimization. It is common for the entire key
67547** and data to fit on the local page and for there to be no overflow
67548** pages. When that is so, this routine can be used to access the
67549** key and data without making a copy. If the key and/or data spills
67550** onto overflow pages, then accessPayload() must be used to reassemble
67551** the key/data and copy it into a preallocated buffer.
67552**
67553** The pointer returned by this routine looks directly into the cached
67554** page of the database. The data might change or move the next time
67555** any btree routine is called.
67556*/
67557static const void *fetchPayload(
67558 BtCursor *pCur, /* Cursor pointing to entry to read from */
67559 u32 *pAmt /* Write the number of available bytes here */
67560){
67561 int amt;
67562 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
67563 assert( pCur->eState==CURSOR_VALID );
67564 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
67565 assert( cursorOwnsBtShared(pCur) );
67566 assert( pCur->ix<pCur->pPage->nCell );
67567 assert( pCur->info.nSize>0 );
67568 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
67569 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
67570 amt = pCur->info.nLocal;
67571 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
67572 /* There is too little space on the page for the expected amount
67573 ** of local content. Database must be corrupt. */
67574 assert( CORRUPT_DB );
67575 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
67576 }
67577 *pAmt = (u32)amt;
67578 return (void*)pCur->info.pPayload;
67579}
67580
67581
67582/*
67583** For the entry that cursor pCur is point to, return as
67584** many bytes of the key or data as are available on the local
67585** b-tree page. Write the number of available bytes into *pAmt.
67586**
67587** The pointer returned is ephemeral. The key/data may move
67588** or be destroyed on the next call to any Btree routine,
67589** including calls from other threads against the same cache.
67590** Hence, a mutex on the BtShared should be held prior to calling
67591** this routine.
67592**
67593** These routines is used to get quick access to key and data
67594** in the common case where no overflow pages are used.
67595*/
67596SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
67597 return fetchPayload(pCur, pAmt);
67598}
67599
67600
67601/*
67602** Move the cursor down to a new child page. The newPgno argument is the
67603** page number of the child page to move to.
67604**
67605** This function returns SQLITE_CORRUPT if the page-header flags field of
67606** the new child page does not match the flags field of the parent (i.e.
67607** if an intkey page appears to be the parent of a non-intkey page, or
67608** vice-versa).
67609*/
67610static int moveToChild(BtCursor *pCur, u32 newPgno){
67611 BtShared *pBt = pCur->pBt;
67612
67613 assert( cursorOwnsBtShared(pCur) );
67614 assert( pCur->eState==CURSOR_VALID );
67615 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
67616 assert( pCur->iPage>=0 );
67617 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
67618 return SQLITE_CORRUPT_BKPT;
67619 }
67620 pCur->info.nSize = 0;
67621 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
67622 pCur->aiIdx[pCur->iPage] = pCur->ix;
67623 pCur->apPage[pCur->iPage] = pCur->pPage;
67624 pCur->ix = 0;
67625 pCur->iPage++;
67626 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
67627}
67628
67629#ifdef SQLITE_DEBUG
67630/*
67631** Page pParent is an internal (non-leaf) tree page. This function
67632** asserts that page number iChild is the left-child if the iIdx'th
67633** cell in page pParent. Or, if iIdx is equal to the total number of
67634** cells in pParent, that page number iChild is the right-child of
67635** the page.
67636*/
67637static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
67638 if( CORRUPT_DB ) return; /* The conditions tested below might not be true
67639 ** in a corrupt database */
67640 assert( iIdx<=pParent->nCell );
67641 if( iIdx==pParent->nCell ){
67642 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
67643 }else{
67644 assert( get4byte(findCell(pParent, iIdx))==iChild );
67645 }
67646}
67647#else
67648# define assertParentIndex(x,y,z)
67649#endif
67650
67651/*
67652** Move the cursor up to the parent page.
67653**
67654** pCur->idx is set to the cell index that contains the pointer
67655** to the page we are coming from. If we are coming from the
67656** right-most child page then pCur->idx is set to one more than
67657** the largest cell index.
67658*/
67659static void moveToParent(BtCursor *pCur){
67660 MemPage *pLeaf;
67661 assert( cursorOwnsBtShared(pCur) );
67662 assert( pCur->eState==CURSOR_VALID );
67663 assert( pCur->iPage>0 );
67664 assert( pCur->pPage );
67665 assertParentIndex(
67666 pCur->apPage[pCur->iPage-1],
67667 pCur->aiIdx[pCur->iPage-1],
67668 pCur->pPage->pgno
67669 );
67670 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
67671 pCur->info.nSize = 0;
67672 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
67673 pCur->ix = pCur->aiIdx[pCur->iPage-1];
67674 pLeaf = pCur->pPage;
67675 pCur->pPage = pCur->apPage[--pCur->iPage];
67676 releasePageNotNull(pLeaf);
67677}
67678
67679/*
67680** Move the cursor to point to the root page of its b-tree structure.
67681**
67682** If the table has a virtual root page, then the cursor is moved to point
67683** to the virtual root page instead of the actual root page. A table has a
67684** virtual root page when the actual root page contains no cells and a
67685** single child page. This can only happen with the table rooted at page 1.
67686**
67687** If the b-tree structure is empty, the cursor state is set to
67688** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
67689** the cursor is set to point to the first cell located on the root
67690** (or virtual root) page and the cursor state is set to CURSOR_VALID.
67691**
67692** If this function returns successfully, it may be assumed that the
67693** page-header flags indicate that the [virtual] root-page is the expected
67694** kind of b-tree page (i.e. if when opening the cursor the caller did not
67695** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
67696** indicating a table b-tree, or if the caller did specify a KeyInfo
67697** structure the flags byte is set to 0x02 or 0x0A, indicating an index
67698** b-tree).
67699*/
67700static int moveToRoot(BtCursor *pCur){
67701 MemPage *pRoot;
67702 int rc = SQLITE_OK;
67703
67704 assert( cursorOwnsBtShared(pCur) );
67705 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
67706 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
67707 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
67708 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
67709 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
67710
67711 if( pCur->iPage>=0 ){
67712 if( pCur->iPage ){
67713 releasePageNotNull(pCur->pPage);
67714 while( --pCur->iPage ){
67715 releasePageNotNull(pCur->apPage[pCur->iPage]);
67716 }
67717 pCur->pPage = pCur->apPage[0];
67718 goto skip_init;
67719 }
67720 }else if( pCur->pgnoRoot==0 ){
67721 pCur->eState = CURSOR_INVALID;
67722 return SQLITE_EMPTY;
67723 }else{
67724 assert( pCur->iPage==(-1) );
67725 if( pCur->eState>=CURSOR_REQUIRESEEK ){
67726 if( pCur->eState==CURSOR_FAULT ){
67727 assert( pCur->skipNext!=SQLITE_OK );
67728 return pCur->skipNext;
67729 }
67730 sqlite3BtreeClearCursor(pCur);
67731 }
67732 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
67733 0, pCur->curPagerFlags);
67734 if( rc!=SQLITE_OK ){
67735 pCur->eState = CURSOR_INVALID;
67736 return rc;
67737 }
67738 pCur->iPage = 0;
67739 pCur->curIntKey = pCur->pPage->intKey;
67740 }
67741 pRoot = pCur->pPage;
67742 assert( pRoot->pgno==pCur->pgnoRoot );
67743
67744 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
67745 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
67746 ** NULL, the caller expects a table b-tree. If this is not the case,
67747 ** return an SQLITE_CORRUPT error.
67748 **
67749 ** Earlier versions of SQLite assumed that this test could not fail
67750 ** if the root page was already loaded when this function was called (i.e.
67751 ** if pCur->iPage>=0). But this is not so if the database is corrupted
67752 ** in such a way that page pRoot is linked into a second b-tree table
67753 ** (or the freelist). */
67754 assert( pRoot->intKey==1 || pRoot->intKey==0 );
67755 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
67756 return SQLITE_CORRUPT_PAGE(pCur->pPage);
67757 }
67758
67759skip_init:
67760 pCur->ix = 0;
67761 pCur->info.nSize = 0;
67762 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
67763
67764 pRoot = pCur->pPage;
67765 if( pRoot->nCell>0 ){
67766 pCur->eState = CURSOR_VALID;
67767 }else if( !pRoot->leaf ){
67768 Pgno subpage;
67769 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
67770 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
67771 pCur->eState = CURSOR_VALID;
67772 rc = moveToChild(pCur, subpage);
67773 }else{
67774 pCur->eState = CURSOR_INVALID;
67775 rc = SQLITE_EMPTY;
67776 }
67777 return rc;
67778}
67779
67780/*
67781** Move the cursor down to the left-most leaf entry beneath the
67782** entry to which it is currently pointing.
67783**
67784** The left-most leaf is the one with the smallest key - the first
67785** in ascending order.
67786*/
67787static int moveToLeftmost(BtCursor *pCur){
67788 Pgno pgno;
67789 int rc = SQLITE_OK;
67790 MemPage *pPage;
67791
67792 assert( cursorOwnsBtShared(pCur) );
67793 assert( pCur->eState==CURSOR_VALID );
67794 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
67795 assert( pCur->ix<pPage->nCell );
67796 pgno = get4byte(findCell(pPage, pCur->ix));
67797 rc = moveToChild(pCur, pgno);
67798 }
67799 return rc;
67800}
67801
67802/*
67803** Move the cursor down to the right-most leaf entry beneath the
67804** page to which it is currently pointing. Notice the difference
67805** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
67806** finds the left-most entry beneath the *entry* whereas moveToRightmost()
67807** finds the right-most entry beneath the *page*.
67808**
67809** The right-most entry is the one with the largest key - the last
67810** key in ascending order.
67811*/
67812static int moveToRightmost(BtCursor *pCur){
67813 Pgno pgno;
67814 int rc = SQLITE_OK;
67815 MemPage *pPage = 0;
67816
67817 assert( cursorOwnsBtShared(pCur) );
67818 assert( pCur->eState==CURSOR_VALID );
67819 while( !(pPage = pCur->pPage)->leaf ){
67820 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
67821 pCur->ix = pPage->nCell;
67822 rc = moveToChild(pCur, pgno);
67823 if( rc ) return rc;
67824 }
67825 pCur->ix = pPage->nCell-1;
67826 assert( pCur->info.nSize==0 );
67827 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
67828 return SQLITE_OK;
67829}
67830
67831/* Move the cursor to the first entry in the table. Return SQLITE_OK
67832** on success. Set *pRes to 0 if the cursor actually points to something
67833** or set *pRes to 1 if the table is empty.
67834*/
67835SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
67836 int rc;
67837
67838 assert( cursorOwnsBtShared(pCur) );
67839 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
67840 rc = moveToRoot(pCur);
67841 if( rc==SQLITE_OK ){
67842 assert( pCur->pPage->nCell>0 );
67843 *pRes = 0;
67844 rc = moveToLeftmost(pCur);
67845 }else if( rc==SQLITE_EMPTY ){
67846 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
67847 *pRes = 1;
67848 rc = SQLITE_OK;
67849 }
67850 return rc;
67851}
67852
67853/*
67854** This function is a no-op if cursor pCur does not point to a valid row.
67855** Otherwise, if pCur is valid, configure it so that the next call to
67856** sqlite3BtreeNext() is a no-op.
67857*/
67858#ifndef SQLITE_OMIT_WINDOWFUNC
67859SQLITE_PRIVATE void sqlite3BtreeSkipNext(BtCursor *pCur){
67860 /* We believe that the cursor must always be in the valid state when
67861 ** this routine is called, but the proof is difficult, so we add an
67862 ** ALWaYS() test just in case we are wrong. */
67863 if( ALWAYS(pCur->eState==CURSOR_VALID) ){
67864 pCur->eState = CURSOR_SKIPNEXT;
67865 pCur->skipNext = 1;
67866 }
67867}
67868#endif /* SQLITE_OMIT_WINDOWFUNC */
67869
67870/* Move the cursor to the last entry in the table. Return SQLITE_OK
67871** on success. Set *pRes to 0 if the cursor actually points to something
67872** or set *pRes to 1 if the table is empty.
67873*/
67874SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
67875 int rc;
67876
67877 assert( cursorOwnsBtShared(pCur) );
67878 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
67879
67880 /* If the cursor already points to the last entry, this is a no-op. */
67881 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
67882#ifdef SQLITE_DEBUG
67883 /* This block serves to assert() that the cursor really does point
67884 ** to the last entry in the b-tree. */
67885 int ii;
67886 for(ii=0; ii<pCur->iPage; ii++){
67887 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
67888 }
67889 assert( pCur->ix==pCur->pPage->nCell-1 );
67890 assert( pCur->pPage->leaf );
67891#endif
67892 return SQLITE_OK;
67893 }
67894
67895 rc = moveToRoot(pCur);
67896 if( rc==SQLITE_OK ){
67897 assert( pCur->eState==CURSOR_VALID );
67898 *pRes = 0;
67899 rc = moveToRightmost(pCur);
67900 if( rc==SQLITE_OK ){
67901 pCur->curFlags |= BTCF_AtLast;
67902 }else{
67903 pCur->curFlags &= ~BTCF_AtLast;
67904 }
67905 }else if( rc==SQLITE_EMPTY ){
67906 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
67907 *pRes = 1;
67908 rc = SQLITE_OK;
67909 }
67910 return rc;
67911}
67912
67913/* Move the cursor so that it points to an entry near the key
67914** specified by pIdxKey or intKey. Return a success code.
67915**
67916** For INTKEY tables, the intKey parameter is used. pIdxKey
67917** must be NULL. For index tables, pIdxKey is used and intKey
67918** is ignored.
67919**
67920** If an exact match is not found, then the cursor is always
67921** left pointing at a leaf page which would hold the entry if it
67922** were present. The cursor might point to an entry that comes
67923** before or after the key.
67924**
67925** An integer is written into *pRes which is the result of
67926** comparing the key with the entry to which the cursor is
67927** pointing. The meaning of the integer written into
67928** *pRes is as follows:
67929**
67930** *pRes<0 The cursor is left pointing at an entry that
67931** is smaller than intKey/pIdxKey or if the table is empty
67932** and the cursor is therefore left point to nothing.
67933**
67934** *pRes==0 The cursor is left pointing at an entry that
67935** exactly matches intKey/pIdxKey.
67936**
67937** *pRes>0 The cursor is left pointing at an entry that
67938** is larger than intKey/pIdxKey.
67939**
67940** For index tables, the pIdxKey->eqSeen field is set to 1 if there
67941** exists an entry in the table that exactly matches pIdxKey.
67942*/
67943SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
67944 BtCursor *pCur, /* The cursor to be moved */
67945 UnpackedRecord *pIdxKey, /* Unpacked index key */
67946 i64 intKey, /* The table key */
67947 int biasRight, /* If true, bias the search to the high end */
67948 int *pRes /* Write search results here */
67949){
67950 int rc;
67951 RecordCompare xRecordCompare;
67952
67953 assert( cursorOwnsBtShared(pCur) );
67954 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
67955 assert( pRes );
67956 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
67957 assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
67958
67959 /* If the cursor is already positioned at the point we are trying
67960 ** to move to, then just return without doing any work */
67961 if( pIdxKey==0
67962 && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
67963 ){
67964 if( pCur->info.nKey==intKey ){
67965 *pRes = 0;
67966 return SQLITE_OK;
67967 }
67968 if( pCur->info.nKey<intKey ){
67969 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
67970 *pRes = -1;
67971 return SQLITE_OK;
67972 }
67973 /* If the requested key is one more than the previous key, then
67974 ** try to get there using sqlite3BtreeNext() rather than a full
67975 ** binary search. This is an optimization only. The correct answer
67976 ** is still obtained without this case, only a little more slowely */
67977 if( pCur->info.nKey+1==intKey && !pCur->skipNext ){
67978 *pRes = 0;
67979 rc = sqlite3BtreeNext(pCur, 0);
67980 if( rc==SQLITE_OK ){
67981 getCellInfo(pCur);
67982 if( pCur->info.nKey==intKey ){
67983 return SQLITE_OK;
67984 }
67985 }else if( rc==SQLITE_DONE ){
67986 rc = SQLITE_OK;
67987 }else{
67988 return rc;
67989 }
67990 }
67991 }
67992 }
67993
67994 if( pIdxKey ){
67995 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
67996 pIdxKey->errCode = 0;
67997 assert( pIdxKey->default_rc==1
67998 || pIdxKey->default_rc==0
67999 || pIdxKey->default_rc==-1
68000 );
68001 }else{
68002 xRecordCompare = 0; /* All keys are integers */
68003 }
68004
68005 rc = moveToRoot(pCur);
68006 if( rc ){
68007 if( rc==SQLITE_EMPTY ){
68008 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
68009 *pRes = -1;
68010 return SQLITE_OK;
68011 }
68012 return rc;
68013 }
68014 assert( pCur->pPage );
68015 assert( pCur->pPage->isInit );
68016 assert( pCur->eState==CURSOR_VALID );
68017 assert( pCur->pPage->nCell > 0 );
68018 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
68019 assert( pCur->curIntKey || pIdxKey );
68020 for(;;){
68021 int lwr, upr, idx, c;
68022 Pgno chldPg;
68023 MemPage *pPage = pCur->pPage;
68024 u8 *pCell; /* Pointer to current cell in pPage */
68025
68026 /* pPage->nCell must be greater than zero. If this is the root-page
68027 ** the cursor would have been INVALID above and this for(;;) loop
68028 ** not run. If this is not the root-page, then the moveToChild() routine
68029 ** would have already detected db corruption. Similarly, pPage must
68030 ** be the right kind (index or table) of b-tree page. Otherwise
68031 ** a moveToChild() or moveToRoot() call would have detected corruption. */
68032 assert( pPage->nCell>0 );
68033 assert( pPage->intKey==(pIdxKey==0) );
68034 lwr = 0;
68035 upr = pPage->nCell-1;
68036 assert( biasRight==0 || biasRight==1 );
68037 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
68038 pCur->ix = (u16)idx;
68039 if( xRecordCompare==0 ){
68040 for(;;){
68041 i64 nCellKey;
68042 pCell = findCellPastPtr(pPage, idx);
68043 if( pPage->intKeyLeaf ){
68044 while( 0x80 <= *(pCell++) ){
68045 if( pCell>=pPage->aDataEnd ){
68046 return SQLITE_CORRUPT_PAGE(pPage);
68047 }
68048 }
68049 }
68050 getVarint(pCell, (u64*)&nCellKey);
68051 if( nCellKey<intKey ){
68052 lwr = idx+1;
68053 if( lwr>upr ){ c = -1; break; }
68054 }else if( nCellKey>intKey ){
68055 upr = idx-1;
68056 if( lwr>upr ){ c = +1; break; }
68057 }else{
68058 assert( nCellKey==intKey );
68059 pCur->ix = (u16)idx;
68060 if( !pPage->leaf ){
68061 lwr = idx;
68062 goto moveto_next_layer;
68063 }else{
68064 pCur->curFlags |= BTCF_ValidNKey;
68065 pCur->info.nKey = nCellKey;
68066 pCur->info.nSize = 0;
68067 *pRes = 0;
68068 return SQLITE_OK;
68069 }
68070 }
68071 assert( lwr+upr>=0 );
68072 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
68073 }
68074 }else{
68075 for(;;){
68076 int nCell; /* Size of the pCell cell in bytes */
68077 pCell = findCellPastPtr(pPage, idx);
68078
68079 /* The maximum supported page-size is 65536 bytes. This means that
68080 ** the maximum number of record bytes stored on an index B-Tree
68081 ** page is less than 16384 bytes and may be stored as a 2-byte
68082 ** varint. This information is used to attempt to avoid parsing
68083 ** the entire cell by checking for the cases where the record is
68084 ** stored entirely within the b-tree page by inspecting the first
68085 ** 2 bytes of the cell.
68086 */
68087 nCell = pCell[0];
68088 if( nCell<=pPage->max1bytePayload ){
68089 /* This branch runs if the record-size field of the cell is a
68090 ** single byte varint and the record fits entirely on the main
68091 ** b-tree page. */
68092 testcase( pCell+nCell+1==pPage->aDataEnd );
68093 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
68094 }else if( !(pCell[1] & 0x80)
68095 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
68096 ){
68097 /* The record-size field is a 2 byte varint and the record
68098 ** fits entirely on the main b-tree page. */
68099 testcase( pCell+nCell+2==pPage->aDataEnd );
68100 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
68101 }else{
68102 /* The record flows over onto one or more overflow pages. In
68103 ** this case the whole cell needs to be parsed, a buffer allocated
68104 ** and accessPayload() used to retrieve the record into the
68105 ** buffer before VdbeRecordCompare() can be called.
68106 **
68107 ** If the record is corrupt, the xRecordCompare routine may read
68108 ** up to two varints past the end of the buffer. An extra 18
68109 ** bytes of padding is allocated at the end of the buffer in
68110 ** case this happens. */
68111 void *pCellKey;
68112 u8 * const pCellBody = pCell - pPage->childPtrSize;
68113 pPage->xParseCell(pPage, pCellBody, &pCur->info);
68114 nCell = (int)pCur->info.nKey;
68115 testcase( nCell<0 ); /* True if key size is 2^32 or more */
68116 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
68117 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
68118 testcase( nCell==2 ); /* Minimum legal index key size */
68119 if( nCell<2 ){
68120 rc = SQLITE_CORRUPT_PAGE(pPage);
68121 goto moveto_finish;
68122 }
68123 pCellKey = sqlite3Malloc( nCell+18 );
68124 if( pCellKey==0 ){
68125 rc = SQLITE_NOMEM_BKPT;
68126 goto moveto_finish;
68127 }
68128 pCur->ix = (u16)idx;
68129 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
68130 pCur->curFlags &= ~BTCF_ValidOvfl;
68131 if( rc ){
68132 sqlite3_free(pCellKey);
68133 goto moveto_finish;
68134 }
68135 c = xRecordCompare(nCell, pCellKey, pIdxKey);
68136 sqlite3_free(pCellKey);
68137 }
68138 assert(
68139 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
68140 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
68141 );
68142 if( c<0 ){
68143 lwr = idx+1;
68144 }else if( c>0 ){
68145 upr = idx-1;
68146 }else{
68147 assert( c==0 );
68148 *pRes = 0;
68149 rc = SQLITE_OK;
68150 pCur->ix = (u16)idx;
68151 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
68152 goto moveto_finish;
68153 }
68154 if( lwr>upr ) break;
68155 assert( lwr+upr>=0 );
68156 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
68157 }
68158 }
68159 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
68160 assert( pPage->isInit );
68161 if( pPage->leaf ){
68162 assert( pCur->ix<pCur->pPage->nCell );
68163 pCur->ix = (u16)idx;
68164 *pRes = c;
68165 rc = SQLITE_OK;
68166 goto moveto_finish;
68167 }
68168moveto_next_layer:
68169 if( lwr>=pPage->nCell ){
68170 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
68171 }else{
68172 chldPg = get4byte(findCell(pPage, lwr));
68173 }
68174 pCur->ix = (u16)lwr;
68175 rc = moveToChild(pCur, chldPg);
68176 if( rc ) break;
68177 }
68178moveto_finish:
68179 pCur->info.nSize = 0;
68180 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
68181 return rc;
68182}
68183
68184
68185/*
68186** Return TRUE if the cursor is not pointing at an entry of the table.
68187**
68188** TRUE will be returned after a call to sqlite3BtreeNext() moves
68189** past the last entry in the table or sqlite3BtreePrev() moves past
68190** the first entry. TRUE is also returned if the table is empty.
68191*/
68192SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
68193 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
68194 ** have been deleted? This API will need to change to return an error code
68195 ** as well as the boolean result value.
68196 */
68197 return (CURSOR_VALID!=pCur->eState);
68198}
68199
68200/*
68201** Return an estimate for the number of rows in the table that pCur is
68202** pointing to. Return a negative number if no estimate is currently
68203** available.
68204*/
68205SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
68206 i64 n;
68207 u8 i;
68208
68209 assert( cursorOwnsBtShared(pCur) );
68210 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
68211
68212 /* Currently this interface is only called by the OP_IfSmaller
68213 ** opcode, and it that case the cursor will always be valid and
68214 ** will always point to a leaf node. */
68215 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
68216 if( NEVER(pCur->pPage->leaf==0) ) return -1;
68217
68218 n = pCur->pPage->nCell;
68219 for(i=0; i<pCur->iPage; i++){
68220 n *= pCur->apPage[i]->nCell;
68221 }
68222 return n;
68223}
68224
68225/*
68226** Advance the cursor to the next entry in the database.
68227** Return value:
68228**
68229** SQLITE_OK success
68230** SQLITE_DONE cursor is already pointing at the last element
68231** otherwise some kind of error occurred
68232**
68233** The main entry point is sqlite3BtreeNext(). That routine is optimized
68234** for the common case of merely incrementing the cell counter BtCursor.aiIdx
68235** to the next cell on the current page. The (slower) btreeNext() helper
68236** routine is called when it is necessary to move to a different page or
68237** to restore the cursor.
68238**
68239** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
68240** cursor corresponds to an SQL index and this routine could have been
68241** skipped if the SQL index had been a unique index. The F argument
68242** is a hint to the implement. SQLite btree implementation does not use
68243** this hint, but COMDB2 does.
68244*/
68245static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){
68246 int rc;
68247 int idx;
68248 MemPage *pPage;
68249
68250 assert( cursorOwnsBtShared(pCur) );
68251 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
68252 if( pCur->eState!=CURSOR_VALID ){
68253 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
68254 rc = restoreCursorPosition(pCur);
68255 if( rc!=SQLITE_OK ){
68256 return rc;
68257 }
68258 if( CURSOR_INVALID==pCur->eState ){
68259 return SQLITE_DONE;
68260 }
68261 if( pCur->skipNext ){
68262 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
68263 pCur->eState = CURSOR_VALID;
68264 if( pCur->skipNext>0 ){
68265 pCur->skipNext = 0;
68266 return SQLITE_OK;
68267 }
68268 pCur->skipNext = 0;
68269 }
68270 }
68271
68272 pPage = pCur->pPage;
68273 idx = ++pCur->ix;
68274 if( !pPage->isInit ){
68275 /* The only known way for this to happen is for there to be a
68276 ** recursive SQL function that does a DELETE operation as part of a
68277 ** SELECT which deletes content out from under an active cursor
68278 ** in a corrupt database file where the table being DELETE-ed from
68279 ** has pages in common with the table being queried. See TH3
68280 ** module cov1/btree78.test testcase 220 (2018-06-08) for an
68281 ** example. */
68282 return SQLITE_CORRUPT_BKPT;
68283 }
68284
68285 /* If the database file is corrupt, it is possible for the value of idx
68286 ** to be invalid here. This can only occur if a second cursor modifies
68287 ** the page while cursor pCur is holding a reference to it. Which can
68288 ** only happen if the database is corrupt in such a way as to link the
68289 ** page into more than one b-tree structure. */
68290 testcase( idx>pPage->nCell );
68291
68292 if( idx>=pPage->nCell ){
68293 if( !pPage->leaf ){
68294 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
68295 if( rc ) return rc;
68296 return moveToLeftmost(pCur);
68297 }
68298 do{
68299 if( pCur->iPage==0 ){
68300 pCur->eState = CURSOR_INVALID;
68301 return SQLITE_DONE;
68302 }
68303 moveToParent(pCur);
68304 pPage = pCur->pPage;
68305 }while( pCur->ix>=pPage->nCell );
68306 if( pPage->intKey ){
68307 return sqlite3BtreeNext(pCur, 0);
68308 }else{
68309 return SQLITE_OK;
68310 }
68311 }
68312 if( pPage->leaf ){
68313 return SQLITE_OK;
68314 }else{
68315 return moveToLeftmost(pCur);
68316 }
68317}
68318SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int flags){
68319 MemPage *pPage;
68320 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
68321 assert( cursorOwnsBtShared(pCur) );
68322 assert( flags==0 || flags==1 );
68323 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
68324 pCur->info.nSize = 0;
68325 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
68326 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
68327 pPage = pCur->pPage;
68328 if( (++pCur->ix)>=pPage->nCell ){
68329 pCur->ix--;
68330 return btreeNext(pCur);
68331 }
68332 if( pPage->leaf ){
68333 return SQLITE_OK;
68334 }else{
68335 return moveToLeftmost(pCur);
68336 }
68337}
68338
68339/*
68340** Step the cursor to the back to the previous entry in the database.
68341** Return values:
68342**
68343** SQLITE_OK success
68344** SQLITE_DONE the cursor is already on the first element of the table
68345** otherwise some kind of error occurred
68346**
68347** The main entry point is sqlite3BtreePrevious(). That routine is optimized
68348** for the common case of merely decrementing the cell counter BtCursor.aiIdx
68349** to the previous cell on the current page. The (slower) btreePrevious()
68350** helper routine is called when it is necessary to move to a different page
68351** or to restore the cursor.
68352**
68353** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
68354** the cursor corresponds to an SQL index and this routine could have been
68355** skipped if the SQL index had been a unique index. The F argument is a
68356** hint to the implement. The native SQLite btree implementation does not
68357** use this hint, but COMDB2 does.
68358*/
68359static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){
68360 int rc;
68361 MemPage *pPage;
68362
68363 assert( cursorOwnsBtShared(pCur) );
68364 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
68365 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
68366 assert( pCur->info.nSize==0 );
68367 if( pCur->eState!=CURSOR_VALID ){
68368 rc = restoreCursorPosition(pCur);
68369 if( rc!=SQLITE_OK ){
68370 return rc;
68371 }
68372 if( CURSOR_INVALID==pCur->eState ){
68373 return SQLITE_DONE;
68374 }
68375 if( pCur->skipNext ){
68376 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
68377 pCur->eState = CURSOR_VALID;
68378 if( pCur->skipNext<0 ){
68379 pCur->skipNext = 0;
68380 return SQLITE_OK;
68381 }
68382 pCur->skipNext = 0;
68383 }
68384 }
68385
68386 pPage = pCur->pPage;
68387 assert( pPage->isInit );
68388 if( !pPage->leaf ){
68389 int idx = pCur->ix;
68390 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
68391 if( rc ) return rc;
68392 rc = moveToRightmost(pCur);
68393 }else{
68394 while( pCur->ix==0 ){
68395 if( pCur->iPage==0 ){
68396 pCur->eState = CURSOR_INVALID;
68397 return SQLITE_DONE;
68398 }
68399 moveToParent(pCur);
68400 }
68401 assert( pCur->info.nSize==0 );
68402 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
68403
68404 pCur->ix--;
68405 pPage = pCur->pPage;
68406 if( pPage->intKey && !pPage->leaf ){
68407 rc = sqlite3BtreePrevious(pCur, 0);
68408 }else{
68409 rc = SQLITE_OK;
68410 }
68411 }
68412 return rc;
68413}
68414SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int flags){
68415 assert( cursorOwnsBtShared(pCur) );
68416 assert( flags==0 || flags==1 );
68417 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
68418 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
68419 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
68420 pCur->info.nSize = 0;
68421 if( pCur->eState!=CURSOR_VALID
68422 || pCur->ix==0
68423 || pCur->pPage->leaf==0
68424 ){
68425 return btreePrevious(pCur);
68426 }
68427 pCur->ix--;
68428 return SQLITE_OK;
68429}
68430
68431/*
68432** Allocate a new page from the database file.
68433**
68434** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
68435** has already been called on the new page.) The new page has also
68436** been referenced and the calling routine is responsible for calling
68437** sqlite3PagerUnref() on the new page when it is done.
68438**
68439** SQLITE_OK is returned on success. Any other return value indicates
68440** an error. *ppPage is set to NULL in the event of an error.
68441**
68442** If the "nearby" parameter is not 0, then an effort is made to
68443** locate a page close to the page number "nearby". This can be used in an
68444** attempt to keep related pages close to each other in the database file,
68445** which in turn can make database access faster.
68446**
68447** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
68448** anywhere on the free-list, then it is guaranteed to be returned. If
68449** eMode is BTALLOC_LT then the page returned will be less than or equal
68450** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
68451** are no restrictions on which page is returned.
68452*/
68453static int allocateBtreePage(
68454 BtShared *pBt, /* The btree */
68455 MemPage **ppPage, /* Store pointer to the allocated page here */
68456 Pgno *pPgno, /* Store the page number here */
68457 Pgno nearby, /* Search for a page near this one */
68458 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
68459){
68460 MemPage *pPage1;
68461 int rc;
68462 u32 n; /* Number of pages on the freelist */
68463 u32 k; /* Number of leaves on the trunk of the freelist */
68464 MemPage *pTrunk = 0;
68465 MemPage *pPrevTrunk = 0;
68466 Pgno mxPage; /* Total size of the database file */
68467
68468 assert( sqlite3_mutex_held(pBt->mutex) );
68469 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
68470 pPage1 = pBt->pPage1;
68471 mxPage = btreePagecount(pBt);
68472 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
68473 ** stores stores the total number of pages on the freelist. */
68474 n = get4byte(&pPage1->aData[36]);
68475 testcase( n==mxPage-1 );
68476 if( n>=mxPage ){
68477 return SQLITE_CORRUPT_BKPT;
68478 }
68479 if( n>0 ){
68480 /* There are pages on the freelist. Reuse one of those pages. */
68481 Pgno iTrunk;
68482 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
68483 u32 nSearch = 0; /* Count of the number of search attempts */
68484
68485 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
68486 ** shows that the page 'nearby' is somewhere on the free-list, then
68487 ** the entire-list will be searched for that page.
68488 */
68489#ifndef SQLITE_OMIT_AUTOVACUUM
68490 if( eMode==BTALLOC_EXACT ){
68491 if( nearby<=mxPage ){
68492 u8 eType;
68493 assert( nearby>0 );
68494 assert( pBt->autoVacuum );
68495 rc = ptrmapGet(pBt, nearby, &eType, 0);
68496 if( rc ) return rc;
68497 if( eType==PTRMAP_FREEPAGE ){
68498 searchList = 1;
68499 }
68500 }
68501 }else if( eMode==BTALLOC_LE ){
68502 searchList = 1;
68503 }
68504#endif
68505
68506 /* Decrement the free-list count by 1. Set iTrunk to the index of the
68507 ** first free-list trunk page. iPrevTrunk is initially 1.
68508 */
68509 rc = sqlite3PagerWrite(pPage1->pDbPage);
68510 if( rc ) return rc;
68511 put4byte(&pPage1->aData[36], n-1);
68512
68513 /* The code within this loop is run only once if the 'searchList' variable
68514 ** is not true. Otherwise, it runs once for each trunk-page on the
68515 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
68516 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
68517 */
68518 do {
68519 pPrevTrunk = pTrunk;
68520 if( pPrevTrunk ){
68521 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
68522 ** is the page number of the next freelist trunk page in the list or
68523 ** zero if this is the last freelist trunk page. */
68524 iTrunk = get4byte(&pPrevTrunk->aData[0]);
68525 }else{
68526 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
68527 ** stores the page number of the first page of the freelist, or zero if
68528 ** the freelist is empty. */
68529 iTrunk = get4byte(&pPage1->aData[32]);
68530 }
68531 testcase( iTrunk==mxPage );
68532 if( iTrunk>mxPage || nSearch++ > n ){
68533 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
68534 }else{
68535 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
68536 }
68537 if( rc ){
68538 pTrunk = 0;
68539 goto end_allocate_page;
68540 }
68541 assert( pTrunk!=0 );
68542 assert( pTrunk->aData!=0 );
68543 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
68544 ** is the number of leaf page pointers to follow. */
68545 k = get4byte(&pTrunk->aData[4]);
68546 if( k==0 && !searchList ){
68547 /* The trunk has no leaves and the list is not being searched.
68548 ** So extract the trunk page itself and use it as the newly
68549 ** allocated page */
68550 assert( pPrevTrunk==0 );
68551 rc = sqlite3PagerWrite(pTrunk->pDbPage);
68552 if( rc ){
68553 goto end_allocate_page;
68554 }
68555 *pPgno = iTrunk;
68556 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
68557 *ppPage = pTrunk;
68558 pTrunk = 0;
68559 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
68560 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
68561 /* Value of k is out of range. Database corruption */
68562 rc = SQLITE_CORRUPT_PGNO(iTrunk);
68563 goto end_allocate_page;
68564#ifndef SQLITE_OMIT_AUTOVACUUM
68565 }else if( searchList
68566 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
68567 ){
68568 /* The list is being searched and this trunk page is the page
68569 ** to allocate, regardless of whether it has leaves.
68570 */
68571 *pPgno = iTrunk;
68572 *ppPage = pTrunk;
68573 searchList = 0;
68574 rc = sqlite3PagerWrite(pTrunk->pDbPage);
68575 if( rc ){
68576 goto end_allocate_page;
68577 }
68578 if( k==0 ){
68579 if( !pPrevTrunk ){
68580 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
68581 }else{
68582 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
68583 if( rc!=SQLITE_OK ){
68584 goto end_allocate_page;
68585 }
68586 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
68587 }
68588 }else{
68589 /* The trunk page is required by the caller but it contains
68590 ** pointers to free-list leaves. The first leaf becomes a trunk
68591 ** page in this case.
68592 */
68593 MemPage *pNewTrunk;
68594 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
68595 if( iNewTrunk>mxPage ){
68596 rc = SQLITE_CORRUPT_PGNO(iTrunk);
68597 goto end_allocate_page;
68598 }
68599 testcase( iNewTrunk==mxPage );
68600 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
68601 if( rc!=SQLITE_OK ){
68602 goto end_allocate_page;
68603 }
68604 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
68605 if( rc!=SQLITE_OK ){
68606 releasePage(pNewTrunk);
68607 goto end_allocate_page;
68608 }
68609 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
68610 put4byte(&pNewTrunk->aData[4], k-1);
68611 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
68612 releasePage(pNewTrunk);
68613 if( !pPrevTrunk ){
68614 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
68615 put4byte(&pPage1->aData[32], iNewTrunk);
68616 }else{
68617 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
68618 if( rc ){
68619 goto end_allocate_page;
68620 }
68621 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
68622 }
68623 }
68624 pTrunk = 0;
68625 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
68626#endif
68627 }else if( k>0 ){
68628 /* Extract a leaf from the trunk */
68629 u32 closest;
68630 Pgno iPage;
68631 unsigned char *aData = pTrunk->aData;
68632 if( nearby>0 ){
68633 u32 i;
68634 closest = 0;
68635 if( eMode==BTALLOC_LE ){
68636 for(i=0; i<k; i++){
68637 iPage = get4byte(&aData[8+i*4]);
68638 if( iPage<=nearby ){
68639 closest = i;
68640 break;
68641 }
68642 }
68643 }else{
68644 int dist;
68645 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
68646 for(i=1; i<k; i++){
68647 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
68648 if( d2<dist ){
68649 closest = i;
68650 dist = d2;
68651 }
68652 }
68653 }
68654 }else{
68655 closest = 0;
68656 }
68657
68658 iPage = get4byte(&aData[8+closest*4]);
68659 testcase( iPage==mxPage );
68660 if( iPage>mxPage ){
68661 rc = SQLITE_CORRUPT_PGNO(iTrunk);
68662 goto end_allocate_page;
68663 }
68664 testcase( iPage==mxPage );
68665 if( !searchList
68666 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
68667 ){
68668 int noContent;
68669 *pPgno = iPage;
68670 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
68671 ": %d more free pages\n",
68672 *pPgno, closest+1, k, pTrunk->pgno, n-1));
68673 rc = sqlite3PagerWrite(pTrunk->pDbPage);
68674 if( rc ) goto end_allocate_page;
68675 if( closest<k-1 ){
68676 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
68677 }
68678 put4byte(&aData[4], k-1);
68679 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
68680 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
68681 if( rc==SQLITE_OK ){
68682 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
68683 if( rc!=SQLITE_OK ){
68684 releasePage(*ppPage);
68685 *ppPage = 0;
68686 }
68687 }
68688 searchList = 0;
68689 }
68690 }
68691 releasePage(pPrevTrunk);
68692 pPrevTrunk = 0;
68693 }while( searchList );
68694 }else{
68695 /* There are no pages on the freelist, so append a new page to the
68696 ** database image.
68697 **
68698 ** Normally, new pages allocated by this block can be requested from the
68699 ** pager layer with the 'no-content' flag set. This prevents the pager
68700 ** from trying to read the pages content from disk. However, if the
68701 ** current transaction has already run one or more incremental-vacuum
68702 ** steps, then the page we are about to allocate may contain content
68703 ** that is required in the event of a rollback. In this case, do
68704 ** not set the no-content flag. This causes the pager to load and journal
68705 ** the current page content before overwriting it.
68706 **
68707 ** Note that the pager will not actually attempt to load or journal
68708 ** content for any page that really does lie past the end of the database
68709 ** file on disk. So the effects of disabling the no-content optimization
68710 ** here are confined to those pages that lie between the end of the
68711 ** database image and the end of the database file.
68712 */
68713 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
68714
68715 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
68716 if( rc ) return rc;
68717 pBt->nPage++;
68718 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
68719
68720#ifndef SQLITE_OMIT_AUTOVACUUM
68721 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
68722 /* If *pPgno refers to a pointer-map page, allocate two new pages
68723 ** at the end of the file instead of one. The first allocated page
68724 ** becomes a new pointer-map page, the second is used by the caller.
68725 */
68726 MemPage *pPg = 0;
68727 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
68728 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
68729 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
68730 if( rc==SQLITE_OK ){
68731 rc = sqlite3PagerWrite(pPg->pDbPage);
68732 releasePage(pPg);
68733 }
68734 if( rc ) return rc;
68735 pBt->nPage++;
68736 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
68737 }
68738#endif
68739 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
68740 *pPgno = pBt->nPage;
68741
68742 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
68743 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
68744 if( rc ) return rc;
68745 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
68746 if( rc!=SQLITE_OK ){
68747 releasePage(*ppPage);
68748 *ppPage = 0;
68749 }
68750 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
68751 }
68752
68753 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
68754
68755end_allocate_page:
68756 releasePage(pTrunk);
68757 releasePage(pPrevTrunk);
68758 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
68759 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
68760 return rc;
68761}
68762
68763/*
68764** This function is used to add page iPage to the database file free-list.
68765** It is assumed that the page is not already a part of the free-list.
68766**
68767** The value passed as the second argument to this function is optional.
68768** If the caller happens to have a pointer to the MemPage object
68769** corresponding to page iPage handy, it may pass it as the second value.
68770** Otherwise, it may pass NULL.
68771**
68772** If a pointer to a MemPage object is passed as the second argument,
68773** its reference count is not altered by this function.
68774*/
68775static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
68776 MemPage *pTrunk = 0; /* Free-list trunk page */
68777 Pgno iTrunk = 0; /* Page number of free-list trunk page */
68778 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
68779 MemPage *pPage; /* Page being freed. May be NULL. */
68780 int rc; /* Return Code */
68781 int nFree; /* Initial number of pages on free-list */
68782
68783 assert( sqlite3_mutex_held(pBt->mutex) );
68784 assert( CORRUPT_DB || iPage>1 );
68785 assert( !pMemPage || pMemPage->pgno==iPage );
68786
68787 if( iPage<2 ) return SQLITE_CORRUPT_BKPT;
68788 if( pMemPage ){
68789 pPage = pMemPage;
68790 sqlite3PagerRef(pPage->pDbPage);
68791 }else{
68792 pPage = btreePageLookup(pBt, iPage);
68793 }
68794
68795 /* Increment the free page count on pPage1 */
68796 rc = sqlite3PagerWrite(pPage1->pDbPage);
68797 if( rc ) goto freepage_out;
68798 nFree = get4byte(&pPage1->aData[36]);
68799 put4byte(&pPage1->aData[36], nFree+1);
68800
68801 if( pBt->btsFlags & BTS_SECURE_DELETE ){
68802 /* If the secure_delete option is enabled, then
68803 ** always fully overwrite deleted information with zeros.
68804 */
68805 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
68806 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
68807 ){
68808 goto freepage_out;
68809 }
68810 memset(pPage->aData, 0, pPage->pBt->pageSize);
68811 }
68812
68813 /* If the database supports auto-vacuum, write an entry in the pointer-map
68814 ** to indicate that the page is free.
68815 */
68816 if( ISAUTOVACUUM ){
68817 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
68818 if( rc ) goto freepage_out;
68819 }
68820
68821 /* Now manipulate the actual database free-list structure. There are two
68822 ** possibilities. If the free-list is currently empty, or if the first
68823 ** trunk page in the free-list is full, then this page will become a
68824 ** new free-list trunk page. Otherwise, it will become a leaf of the
68825 ** first trunk page in the current free-list. This block tests if it
68826 ** is possible to add the page as a new free-list leaf.
68827 */
68828 if( nFree!=0 ){
68829 u32 nLeaf; /* Initial number of leaf cells on trunk page */
68830
68831 iTrunk = get4byte(&pPage1->aData[32]);
68832 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
68833 if( rc!=SQLITE_OK ){
68834 goto freepage_out;
68835 }
68836
68837 nLeaf = get4byte(&pTrunk->aData[4]);
68838 assert( pBt->usableSize>32 );
68839 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
68840 rc = SQLITE_CORRUPT_BKPT;
68841 goto freepage_out;
68842 }
68843 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
68844 /* In this case there is room on the trunk page to insert the page
68845 ** being freed as a new leaf.
68846 **
68847 ** Note that the trunk page is not really full until it contains
68848 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
68849 ** coded. But due to a coding error in versions of SQLite prior to
68850 ** 3.6.0, databases with freelist trunk pages holding more than
68851 ** usableSize/4 - 8 entries will be reported as corrupt. In order
68852 ** to maintain backwards compatibility with older versions of SQLite,
68853 ** we will continue to restrict the number of entries to usableSize/4 - 8
68854 ** for now. At some point in the future (once everyone has upgraded
68855 ** to 3.6.0 or later) we should consider fixing the conditional above
68856 ** to read "usableSize/4-2" instead of "usableSize/4-8".
68857 **
68858 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
68859 ** avoid using the last six entries in the freelist trunk page array in
68860 ** order that database files created by newer versions of SQLite can be
68861 ** read by older versions of SQLite.
68862 */
68863 rc = sqlite3PagerWrite(pTrunk->pDbPage);
68864 if( rc==SQLITE_OK ){
68865 put4byte(&pTrunk->aData[4], nLeaf+1);
68866 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
68867 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
68868 sqlite3PagerDontWrite(pPage->pDbPage);
68869 }
68870 rc = btreeSetHasContent(pBt, iPage);
68871 }
68872 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
68873 goto freepage_out;
68874 }
68875 }
68876
68877 /* If control flows to this point, then it was not possible to add the
68878 ** the page being freed as a leaf page of the first trunk in the free-list.
68879 ** Possibly because the free-list is empty, or possibly because the
68880 ** first trunk in the free-list is full. Either way, the page being freed
68881 ** will become the new first trunk page in the free-list.
68882 */
68883 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
68884 goto freepage_out;
68885 }
68886 rc = sqlite3PagerWrite(pPage->pDbPage);
68887 if( rc!=SQLITE_OK ){
68888 goto freepage_out;
68889 }
68890 put4byte(pPage->aData, iTrunk);
68891 put4byte(&pPage->aData[4], 0);
68892 put4byte(&pPage1->aData[32], iPage);
68893 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
68894
68895freepage_out:
68896 if( pPage ){
68897 pPage->isInit = 0;
68898 }
68899 releasePage(pPage);
68900 releasePage(pTrunk);
68901 return rc;
68902}
68903static void freePage(MemPage *pPage, int *pRC){
68904 if( (*pRC)==SQLITE_OK ){
68905 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
68906 }
68907}
68908
68909/*
68910** Free any overflow pages associated with the given Cell. Store
68911** size information about the cell in pInfo.
68912*/
68913static int clearCell(
68914 MemPage *pPage, /* The page that contains the Cell */
68915 unsigned char *pCell, /* First byte of the Cell */
68916 CellInfo *pInfo /* Size information about the cell */
68917){
68918 BtShared *pBt;
68919 Pgno ovflPgno;
68920 int rc;
68921 int nOvfl;
68922 u32 ovflPageSize;
68923
68924 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68925 pPage->xParseCell(pPage, pCell, pInfo);
68926 if( pInfo->nLocal==pInfo->nPayload ){
68927 return SQLITE_OK; /* No overflow pages. Return without doing anything */
68928 }
68929 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
68930 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
68931 if( pCell + pInfo->nSize > pPage->aDataEnd ){
68932 /* Cell extends past end of page */
68933 return SQLITE_CORRUPT_PAGE(pPage);
68934 }
68935 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
68936 pBt = pPage->pBt;
68937 assert( pBt->usableSize > 4 );
68938 ovflPageSize = pBt->usableSize - 4;
68939 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
68940 assert( nOvfl>0 ||
68941 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
68942 );
68943 while( nOvfl-- ){
68944 Pgno iNext = 0;
68945 MemPage *pOvfl = 0;
68946 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
68947 /* 0 is not a legal page number and page 1 cannot be an
68948 ** overflow page. Therefore if ovflPgno<2 or past the end of the
68949 ** file the database must be corrupt. */
68950 return SQLITE_CORRUPT_BKPT;
68951 }
68952 if( nOvfl ){
68953 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
68954 if( rc ) return rc;
68955 }
68956
68957 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
68958 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
68959 ){
68960 /* There is no reason any cursor should have an outstanding reference
68961 ** to an overflow page belonging to a cell that is being deleted/updated.
68962 ** So if there exists more than one reference to this page, then it
68963 ** must not really be an overflow page and the database must be corrupt.
68964 ** It is helpful to detect this before calling freePage2(), as
68965 ** freePage2() may zero the page contents if secure-delete mode is
68966 ** enabled. If this 'overflow' page happens to be a page that the
68967 ** caller is iterating through or using in some other way, this
68968 ** can be problematic.
68969 */
68970 rc = SQLITE_CORRUPT_BKPT;
68971 }else{
68972 rc = freePage2(pBt, pOvfl, ovflPgno);
68973 }
68974
68975 if( pOvfl ){
68976 sqlite3PagerUnref(pOvfl->pDbPage);
68977 }
68978 if( rc ) return rc;
68979 ovflPgno = iNext;
68980 }
68981 return SQLITE_OK;
68982}
68983
68984/*
68985** Create the byte sequence used to represent a cell on page pPage
68986** and write that byte sequence into pCell[]. Overflow pages are
68987** allocated and filled in as necessary. The calling procedure
68988** is responsible for making sure sufficient space has been allocated
68989** for pCell[].
68990**
68991** Note that pCell does not necessary need to point to the pPage->aData
68992** area. pCell might point to some temporary storage. The cell will
68993** be constructed in this temporary area then copied into pPage->aData
68994** later.
68995*/
68996static int fillInCell(
68997 MemPage *pPage, /* The page that contains the cell */
68998 unsigned char *pCell, /* Complete text of the cell */
68999 const BtreePayload *pX, /* Payload with which to construct the cell */
69000 int *pnSize /* Write cell size here */
69001){
69002 int nPayload;
69003 const u8 *pSrc;
69004 int nSrc, n, rc, mn;
69005 int spaceLeft;
69006 MemPage *pToRelease;
69007 unsigned char *pPrior;
69008 unsigned char *pPayload;
69009 BtShared *pBt;
69010 Pgno pgnoOvfl;
69011 int nHeader;
69012
69013 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69014
69015 /* pPage is not necessarily writeable since pCell might be auxiliary
69016 ** buffer space that is separate from the pPage buffer area */
69017 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
69018 || sqlite3PagerIswriteable(pPage->pDbPage) );
69019
69020 /* Fill in the header. */
69021 nHeader = pPage->childPtrSize;
69022 if( pPage->intKey ){
69023 nPayload = pX->nData + pX->nZero;
69024 pSrc = pX->pData;
69025 nSrc = pX->nData;
69026 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
69027 nHeader += putVarint32(&pCell[nHeader], nPayload);
69028 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
69029 }else{
69030 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
69031 nSrc = nPayload = (int)pX->nKey;
69032 pSrc = pX->pKey;
69033 nHeader += putVarint32(&pCell[nHeader], nPayload);
69034 }
69035
69036 /* Fill in the payload */
69037 pPayload = &pCell[nHeader];
69038 if( nPayload<=pPage->maxLocal ){
69039 /* This is the common case where everything fits on the btree page
69040 ** and no overflow pages are required. */
69041 n = nHeader + nPayload;
69042 testcase( n==3 );
69043 testcase( n==4 );
69044 if( n<4 ) n = 4;
69045 *pnSize = n;
69046 assert( nSrc<=nPayload );
69047 testcase( nSrc<nPayload );
69048 memcpy(pPayload, pSrc, nSrc);
69049 memset(pPayload+nSrc, 0, nPayload-nSrc);
69050 return SQLITE_OK;
69051 }
69052
69053 /* If we reach this point, it means that some of the content will need
69054 ** to spill onto overflow pages.
69055 */
69056 mn = pPage->minLocal;
69057 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
69058 testcase( n==pPage->maxLocal );
69059 testcase( n==pPage->maxLocal+1 );
69060 if( n > pPage->maxLocal ) n = mn;
69061 spaceLeft = n;
69062 *pnSize = n + nHeader + 4;
69063 pPrior = &pCell[nHeader+n];
69064 pToRelease = 0;
69065 pgnoOvfl = 0;
69066 pBt = pPage->pBt;
69067
69068 /* At this point variables should be set as follows:
69069 **
69070 ** nPayload Total payload size in bytes
69071 ** pPayload Begin writing payload here
69072 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
69073 ** that means content must spill into overflow pages.
69074 ** *pnSize Size of the local cell (not counting overflow pages)
69075 ** pPrior Where to write the pgno of the first overflow page
69076 **
69077 ** Use a call to btreeParseCellPtr() to verify that the values above
69078 ** were computed correctly.
69079 */
69080#ifdef SQLITE_DEBUG
69081 {
69082 CellInfo info;
69083 pPage->xParseCell(pPage, pCell, &info);
69084 assert( nHeader==(int)(info.pPayload - pCell) );
69085 assert( info.nKey==pX->nKey );
69086 assert( *pnSize == info.nSize );
69087 assert( spaceLeft == info.nLocal );
69088 }
69089#endif
69090
69091 /* Write the payload into the local Cell and any extra into overflow pages */
69092 while( 1 ){
69093 n = nPayload;
69094 if( n>spaceLeft ) n = spaceLeft;
69095
69096 /* If pToRelease is not zero than pPayload points into the data area
69097 ** of pToRelease. Make sure pToRelease is still writeable. */
69098 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
69099
69100 /* If pPayload is part of the data area of pPage, then make sure pPage
69101 ** is still writeable */
69102 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
69103 || sqlite3PagerIswriteable(pPage->pDbPage) );
69104
69105 if( nSrc>=n ){
69106 memcpy(pPayload, pSrc, n);
69107 }else if( nSrc>0 ){
69108 n = nSrc;
69109 memcpy(pPayload, pSrc, n);
69110 }else{
69111 memset(pPayload, 0, n);
69112 }
69113 nPayload -= n;
69114 if( nPayload<=0 ) break;
69115 pPayload += n;
69116 pSrc += n;
69117 nSrc -= n;
69118 spaceLeft -= n;
69119 if( spaceLeft==0 ){
69120 MemPage *pOvfl = 0;
69121#ifndef SQLITE_OMIT_AUTOVACUUM
69122 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
69123 if( pBt->autoVacuum ){
69124 do{
69125 pgnoOvfl++;
69126 } while(
69127 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
69128 );
69129 }
69130#endif
69131 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
69132#ifndef SQLITE_OMIT_AUTOVACUUM
69133 /* If the database supports auto-vacuum, and the second or subsequent
69134 ** overflow page is being allocated, add an entry to the pointer-map
69135 ** for that page now.
69136 **
69137 ** If this is the first overflow page, then write a partial entry
69138 ** to the pointer-map. If we write nothing to this pointer-map slot,
69139 ** then the optimistic overflow chain processing in clearCell()
69140 ** may misinterpret the uninitialized values and delete the
69141 ** wrong pages from the database.
69142 */
69143 if( pBt->autoVacuum && rc==SQLITE_OK ){
69144 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
69145 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
69146 if( rc ){
69147 releasePage(pOvfl);
69148 }
69149 }
69150#endif
69151 if( rc ){
69152 releasePage(pToRelease);
69153 return rc;
69154 }
69155
69156 /* If pToRelease is not zero than pPrior points into the data area
69157 ** of pToRelease. Make sure pToRelease is still writeable. */
69158 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
69159
69160 /* If pPrior is part of the data area of pPage, then make sure pPage
69161 ** is still writeable */
69162 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
69163 || sqlite3PagerIswriteable(pPage->pDbPage) );
69164
69165 put4byte(pPrior, pgnoOvfl);
69166 releasePage(pToRelease);
69167 pToRelease = pOvfl;
69168 pPrior = pOvfl->aData;
69169 put4byte(pPrior, 0);
69170 pPayload = &pOvfl->aData[4];
69171 spaceLeft = pBt->usableSize - 4;
69172 }
69173 }
69174 releasePage(pToRelease);
69175 return SQLITE_OK;
69176}
69177
69178/*
69179** Remove the i-th cell from pPage. This routine effects pPage only.
69180** The cell content is not freed or deallocated. It is assumed that
69181** the cell content has been copied someplace else. This routine just
69182** removes the reference to the cell from pPage.
69183**
69184** "sz" must be the number of bytes in the cell.
69185*/
69186static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
69187 u32 pc; /* Offset to cell content of cell being deleted */
69188 u8 *data; /* pPage->aData */
69189 u8 *ptr; /* Used to move bytes around within data[] */
69190 int rc; /* The return code */
69191 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
69192
69193 if( *pRC ) return;
69194 assert( idx>=0 && idx<pPage->nCell );
69195 assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
69196 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
69197 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69198 data = pPage->aData;
69199 ptr = &pPage->aCellIdx[2*idx];
69200 pc = get2byte(ptr);
69201 hdr = pPage->hdrOffset;
69202 testcase( pc==get2byte(&data[hdr+5]) );
69203 testcase( pc+sz==pPage->pBt->usableSize );
69204 if( pc+sz > pPage->pBt->usableSize ){
69205 *pRC = SQLITE_CORRUPT_BKPT;
69206 return;
69207 }
69208 rc = freeSpace(pPage, pc, sz);
69209 if( rc ){
69210 *pRC = rc;
69211 return;
69212 }
69213 pPage->nCell--;
69214 if( pPage->nCell==0 ){
69215 memset(&data[hdr+1], 0, 4);
69216 data[hdr+7] = 0;
69217 put2byte(&data[hdr+5], pPage->pBt->usableSize);
69218 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
69219 - pPage->childPtrSize - 8;
69220 }else{
69221 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
69222 put2byte(&data[hdr+3], pPage->nCell);
69223 pPage->nFree += 2;
69224 }
69225}
69226
69227/*
69228** Insert a new cell on pPage at cell index "i". pCell points to the
69229** content of the cell.
69230**
69231** If the cell content will fit on the page, then put it there. If it
69232** will not fit, then make a copy of the cell content into pTemp if
69233** pTemp is not null. Regardless of pTemp, allocate a new entry
69234** in pPage->apOvfl[] and make it point to the cell content (either
69235** in pTemp or the original pCell) and also record its index.
69236** Allocating a new entry in pPage->aCell[] implies that
69237** pPage->nOverflow is incremented.
69238**
69239** *pRC must be SQLITE_OK when this routine is called.
69240*/
69241static void insertCell(
69242 MemPage *pPage, /* Page into which we are copying */
69243 int i, /* New cell becomes the i-th cell of the page */
69244 u8 *pCell, /* Content of the new cell */
69245 int sz, /* Bytes of content in pCell */
69246 u8 *pTemp, /* Temp storage space for pCell, if needed */
69247 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
69248 int *pRC /* Read and write return code from here */
69249){
69250 int idx = 0; /* Where to write new cell content in data[] */
69251 int j; /* Loop counter */
69252 u8 *data; /* The content of the whole page */
69253 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
69254
69255 assert( *pRC==SQLITE_OK );
69256 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
69257 assert( MX_CELL(pPage->pBt)<=10921 );
69258 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
69259 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
69260 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
69261 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69262 /* The cell should normally be sized correctly. However, when moving a
69263 ** malformed cell from a leaf page to an interior page, if the cell size
69264 ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
69265 ** might be less than 8 (leaf-size + pointer) on the interior node. Hence
69266 ** the term after the || in the following assert(). */
69267 assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
69268 if( pPage->nOverflow || sz+2>pPage->nFree ){
69269 if( pTemp ){
69270 memcpy(pTemp, pCell, sz);
69271 pCell = pTemp;
69272 }
69273 if( iChild ){
69274 put4byte(pCell, iChild);
69275 }
69276 j = pPage->nOverflow++;
69277 /* Comparison against ArraySize-1 since we hold back one extra slot
69278 ** as a contingency. In other words, never need more than 3 overflow
69279 ** slots but 4 are allocated, just to be safe. */
69280 assert( j < ArraySize(pPage->apOvfl)-1 );
69281 pPage->apOvfl[j] = pCell;
69282 pPage->aiOvfl[j] = (u16)i;
69283
69284 /* When multiple overflows occur, they are always sequential and in
69285 ** sorted order. This invariants arise because multiple overflows can
69286 ** only occur when inserting divider cells into the parent page during
69287 ** balancing, and the dividers are adjacent and sorted.
69288 */
69289 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
69290 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
69291 }else{
69292 int rc = sqlite3PagerWrite(pPage->pDbPage);
69293 if( rc!=SQLITE_OK ){
69294 *pRC = rc;
69295 return;
69296 }
69297 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
69298 data = pPage->aData;
69299 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
69300 rc = allocateSpace(pPage, sz, &idx);
69301 if( rc ){ *pRC = rc; return; }
69302 /* The allocateSpace() routine guarantees the following properties
69303 ** if it returns successfully */
69304 assert( idx >= 0 );
69305 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
69306 assert( idx+sz <= (int)pPage->pBt->usableSize );
69307 pPage->nFree -= (u16)(2 + sz);
69308 memcpy(&data[idx], pCell, sz);
69309 if( iChild ){
69310 put4byte(&data[idx], iChild);
69311 }
69312 pIns = pPage->aCellIdx + i*2;
69313 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
69314 put2byte(pIns, idx);
69315 pPage->nCell++;
69316 /* increment the cell count */
69317 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
69318 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
69319#ifndef SQLITE_OMIT_AUTOVACUUM
69320 if( pPage->pBt->autoVacuum ){
69321 /* The cell may contain a pointer to an overflow page. If so, write
69322 ** the entry for the overflow page into the pointer map.
69323 */
69324 ptrmapPutOvflPtr(pPage, pCell, pRC);
69325 }
69326#endif
69327 }
69328}
69329
69330/*
69331** A CellArray object contains a cache of pointers and sizes for a
69332** consecutive sequence of cells that might be held on multiple pages.
69333*/
69334typedef struct CellArray CellArray;
69335struct CellArray {
69336 int nCell; /* Number of cells in apCell[] */
69337 MemPage *pRef; /* Reference page */
69338 u8 **apCell; /* All cells begin balanced */
69339 u16 *szCell; /* Local size of all cells in apCell[] */
69340};
69341
69342/*
69343** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
69344** computed.
69345*/
69346static void populateCellCache(CellArray *p, int idx, int N){
69347 assert( idx>=0 && idx+N<=p->nCell );
69348 while( N>0 ){
69349 assert( p->apCell[idx]!=0 );
69350 if( p->szCell[idx]==0 ){
69351 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
69352 }else{
69353 assert( CORRUPT_DB ||
69354 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
69355 }
69356 idx++;
69357 N--;
69358 }
69359}
69360
69361/*
69362** Return the size of the Nth element of the cell array
69363*/
69364static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
69365 assert( N>=0 && N<p->nCell );
69366 assert( p->szCell[N]==0 );
69367 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
69368 return p->szCell[N];
69369}
69370static u16 cachedCellSize(CellArray *p, int N){
69371 assert( N>=0 && N<p->nCell );
69372 if( p->szCell[N] ) return p->szCell[N];
69373 return computeCellSize(p, N);
69374}
69375
69376/*
69377** Array apCell[] contains pointers to nCell b-tree page cells. The
69378** szCell[] array contains the size in bytes of each cell. This function
69379** replaces the current contents of page pPg with the contents of the cell
69380** array.
69381**
69382** Some of the cells in apCell[] may currently be stored in pPg. This
69383** function works around problems caused by this by making a copy of any
69384** such cells before overwriting the page data.
69385**
69386** The MemPage.nFree field is invalidated by this function. It is the
69387** responsibility of the caller to set it correctly.
69388*/
69389static int rebuildPage(
69390 MemPage *pPg, /* Edit this page */
69391 int nCell, /* Final number of cells on page */
69392 u8 **apCell, /* Array of cells */
69393 u16 *szCell /* Array of cell sizes */
69394){
69395 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
69396 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
69397 const int usableSize = pPg->pBt->usableSize;
69398 u8 * const pEnd = &aData[usableSize];
69399 int i;
69400 u8 *pCellptr = pPg->aCellIdx;
69401 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
69402 u8 *pData;
69403
69404 i = get2byte(&aData[hdr+5]);
69405 memcpy(&pTmp[i], &aData[i], usableSize - i);
69406
69407 pData = pEnd;
69408 for(i=0; i<nCell; i++){
69409 u8 *pCell = apCell[i];
69410 if( SQLITE_WITHIN(pCell,aData,pEnd) ){
69411 pCell = &pTmp[pCell - aData];
69412 }
69413 pData -= szCell[i];
69414 put2byte(pCellptr, (pData - aData));
69415 pCellptr += 2;
69416 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
69417 memcpy(pData, pCell, szCell[i]);
69418 assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
69419 testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) );
69420 }
69421
69422 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
69423 pPg->nCell = nCell;
69424 pPg->nOverflow = 0;
69425
69426 put2byte(&aData[hdr+1], 0);
69427 put2byte(&aData[hdr+3], pPg->nCell);
69428 put2byte(&aData[hdr+5], pData - aData);
69429 aData[hdr+7] = 0x00;
69430 return SQLITE_OK;
69431}
69432
69433/*
69434** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
69435** contains the size in bytes of each such cell. This function attempts to
69436** add the cells stored in the array to page pPg. If it cannot (because
69437** the page needs to be defragmented before the cells will fit), non-zero
69438** is returned. Otherwise, if the cells are added successfully, zero is
69439** returned.
69440**
69441** Argument pCellptr points to the first entry in the cell-pointer array
69442** (part of page pPg) to populate. After cell apCell[0] is written to the
69443** page body, a 16-bit offset is written to pCellptr. And so on, for each
69444** cell in the array. It is the responsibility of the caller to ensure
69445** that it is safe to overwrite this part of the cell-pointer array.
69446**
69447** When this function is called, *ppData points to the start of the
69448** content area on page pPg. If the size of the content area is extended,
69449** *ppData is updated to point to the new start of the content area
69450** before returning.
69451**
69452** Finally, argument pBegin points to the byte immediately following the
69453** end of the space required by this page for the cell-pointer area (for
69454** all cells - not just those inserted by the current call). If the content
69455** area must be extended to before this point in order to accomodate all
69456** cells in apCell[], then the cells do not fit and non-zero is returned.
69457*/
69458static int pageInsertArray(
69459 MemPage *pPg, /* Page to add cells to */
69460 u8 *pBegin, /* End of cell-pointer array */
69461 u8 **ppData, /* IN/OUT: Page content -area pointer */
69462 u8 *pCellptr, /* Pointer to cell-pointer area */
69463 int iFirst, /* Index of first cell to add */
69464 int nCell, /* Number of cells to add to pPg */
69465 CellArray *pCArray /* Array of cells */
69466){
69467 int i;
69468 u8 *aData = pPg->aData;
69469 u8 *pData = *ppData;
69470 int iEnd = iFirst + nCell;
69471 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
69472 for(i=iFirst; i<iEnd; i++){
69473 int sz, rc;
69474 u8 *pSlot;
69475 sz = cachedCellSize(pCArray, i);
69476 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
69477 if( (pData - pBegin)<sz ) return 1;
69478 pData -= sz;
69479 pSlot = pData;
69480 }
69481 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
69482 ** database. But they might for a corrupt database. Hence use memmove()
69483 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
69484 assert( (pSlot+sz)<=pCArray->apCell[i]
69485 || pSlot>=(pCArray->apCell[i]+sz)
69486 || CORRUPT_DB );
69487 memmove(pSlot, pCArray->apCell[i], sz);
69488 put2byte(pCellptr, (pSlot - aData));
69489 pCellptr += 2;
69490 }
69491 *ppData = pData;
69492 return 0;
69493}
69494
69495/*
69496** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
69497** contains the size in bytes of each such cell. This function adds the
69498** space associated with each cell in the array that is currently stored
69499** within the body of pPg to the pPg free-list. The cell-pointers and other
69500** fields of the page are not updated.
69501**
69502** This function returns the total number of cells added to the free-list.
69503*/
69504static int pageFreeArray(
69505 MemPage *pPg, /* Page to edit */
69506 int iFirst, /* First cell to delete */
69507 int nCell, /* Cells to delete */
69508 CellArray *pCArray /* Array of cells */
69509){
69510 u8 * const aData = pPg->aData;
69511 u8 * const pEnd = &aData[pPg->pBt->usableSize];
69512 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
69513 int nRet = 0;
69514 int i;
69515 int iEnd = iFirst + nCell;
69516 u8 *pFree = 0;
69517 int szFree = 0;
69518
69519 for(i=iFirst; i<iEnd; i++){
69520 u8 *pCell = pCArray->apCell[i];
69521 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
69522 int sz;
69523 /* No need to use cachedCellSize() here. The sizes of all cells that
69524 ** are to be freed have already been computing while deciding which
69525 ** cells need freeing */
69526 sz = pCArray->szCell[i]; assert( sz>0 );
69527 if( pFree!=(pCell + sz) ){
69528 if( pFree ){
69529 assert( pFree>aData && (pFree - aData)<65536 );
69530 freeSpace(pPg, (u16)(pFree - aData), szFree);
69531 }
69532 pFree = pCell;
69533 szFree = sz;
69534 if( pFree+sz>pEnd ) return 0;
69535 }else{
69536 pFree = pCell;
69537 szFree += sz;
69538 }
69539 nRet++;
69540 }
69541 }
69542 if( pFree ){
69543 assert( pFree>aData && (pFree - aData)<65536 );
69544 freeSpace(pPg, (u16)(pFree - aData), szFree);
69545 }
69546 return nRet;
69547}
69548
69549/*
69550** apCell[] and szCell[] contains pointers to and sizes of all cells in the
69551** pages being balanced. The current page, pPg, has pPg->nCell cells starting
69552** with apCell[iOld]. After balancing, this page should hold nNew cells
69553** starting at apCell[iNew].
69554**
69555** This routine makes the necessary adjustments to pPg so that it contains
69556** the correct cells after being balanced.
69557**
69558** The pPg->nFree field is invalid when this function returns. It is the
69559** responsibility of the caller to set it correctly.
69560*/
69561static int editPage(
69562 MemPage *pPg, /* Edit this page */
69563 int iOld, /* Index of first cell currently on page */
69564 int iNew, /* Index of new first cell on page */
69565 int nNew, /* Final number of cells on page */
69566 CellArray *pCArray /* Array of cells and sizes */
69567){
69568 u8 * const aData = pPg->aData;
69569 const int hdr = pPg->hdrOffset;
69570 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
69571 int nCell = pPg->nCell; /* Cells stored on pPg */
69572 u8 *pData;
69573 u8 *pCellptr;
69574 int i;
69575 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
69576 int iNewEnd = iNew + nNew;
69577
69578#ifdef SQLITE_DEBUG
69579 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
69580 memcpy(pTmp, aData, pPg->pBt->usableSize);
69581#endif
69582
69583 /* Remove cells from the start and end of the page */
69584 if( iOld<iNew ){
69585 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
69586 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
69587 nCell -= nShift;
69588 }
69589 if( iNewEnd < iOldEnd ){
69590 nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
69591 }
69592
69593 pData = &aData[get2byteNotZero(&aData[hdr+5])];
69594 if( pData<pBegin ) goto editpage_fail;
69595
69596 /* Add cells to the start of the page */
69597 if( iNew<iOld ){
69598 int nAdd = MIN(nNew,iOld-iNew);
69599 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
69600 pCellptr = pPg->aCellIdx;
69601 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
69602 if( pageInsertArray(
69603 pPg, pBegin, &pData, pCellptr,
69604 iNew, nAdd, pCArray
69605 ) ) goto editpage_fail;
69606 nCell += nAdd;
69607 }
69608
69609 /* Add any overflow cells */
69610 for(i=0; i<pPg->nOverflow; i++){
69611 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
69612 if( iCell>=0 && iCell<nNew ){
69613 pCellptr = &pPg->aCellIdx[iCell * 2];
69614 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
69615 nCell++;
69616 if( pageInsertArray(
69617 pPg, pBegin, &pData, pCellptr,
69618 iCell+iNew, 1, pCArray
69619 ) ) goto editpage_fail;
69620 }
69621 }
69622
69623 /* Append cells to the end of the page */
69624 pCellptr = &pPg->aCellIdx[nCell*2];
69625 if( pageInsertArray(
69626 pPg, pBegin, &pData, pCellptr,
69627 iNew+nCell, nNew-nCell, pCArray
69628 ) ) goto editpage_fail;
69629
69630 pPg->nCell = nNew;
69631 pPg->nOverflow = 0;
69632
69633 put2byte(&aData[hdr+3], pPg->nCell);
69634 put2byte(&aData[hdr+5], pData - aData);
69635
69636#ifdef SQLITE_DEBUG
69637 for(i=0; i<nNew && !CORRUPT_DB; i++){
69638 u8 *pCell = pCArray->apCell[i+iNew];
69639 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
69640 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
69641 pCell = &pTmp[pCell - aData];
69642 }
69643 assert( 0==memcmp(pCell, &aData[iOff],
69644 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
69645 }
69646#endif
69647
69648 return SQLITE_OK;
69649 editpage_fail:
69650 /* Unable to edit this page. Rebuild it from scratch instead. */
69651 populateCellCache(pCArray, iNew, nNew);
69652 return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]);
69653}
69654
69655/*
69656** The following parameters determine how many adjacent pages get involved
69657** in a balancing operation. NN is the number of neighbors on either side
69658** of the page that participate in the balancing operation. NB is the
69659** total number of pages that participate, including the target page and
69660** NN neighbors on either side.
69661**
69662** The minimum value of NN is 1 (of course). Increasing NN above 1
69663** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
69664** in exchange for a larger degradation in INSERT and UPDATE performance.
69665** The value of NN appears to give the best results overall.
69666*/
69667#define NN 1 /* Number of neighbors on either side of pPage */
69668#define NB (NN*2+1) /* Total pages involved in the balance */
69669
69670
69671#ifndef SQLITE_OMIT_QUICKBALANCE
69672/*
69673** This version of balance() handles the common special case where
69674** a new entry is being inserted on the extreme right-end of the
69675** tree, in other words, when the new entry will become the largest
69676** entry in the tree.
69677**
69678** Instead of trying to balance the 3 right-most leaf pages, just add
69679** a new page to the right-hand side and put the one new entry in
69680** that page. This leaves the right side of the tree somewhat
69681** unbalanced. But odds are that we will be inserting new entries
69682** at the end soon afterwards so the nearly empty page will quickly
69683** fill up. On average.
69684**
69685** pPage is the leaf page which is the right-most page in the tree.
69686** pParent is its parent. pPage must have a single overflow entry
69687** which is also the right-most entry on the page.
69688**
69689** The pSpace buffer is used to store a temporary copy of the divider
69690** cell that will be inserted into pParent. Such a cell consists of a 4
69691** byte page number followed by a variable length integer. In other
69692** words, at most 13 bytes. Hence the pSpace buffer must be at
69693** least 13 bytes in size.
69694*/
69695static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
69696 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
69697 MemPage *pNew; /* Newly allocated page */
69698 int rc; /* Return Code */
69699 Pgno pgnoNew; /* Page number of pNew */
69700
69701 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69702 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
69703 assert( pPage->nOverflow==1 );
69704
69705 /* This error condition is now caught prior to reaching this function */
69706 if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT;
69707
69708 /* Allocate a new page. This page will become the right-sibling of
69709 ** pPage. Make the parent page writable, so that the new divider cell
69710 ** may be inserted. If both these operations are successful, proceed.
69711 */
69712 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
69713
69714 if( rc==SQLITE_OK ){
69715
69716 u8 *pOut = &pSpace[4];
69717 u8 *pCell = pPage->apOvfl[0];
69718 u16 szCell = pPage->xCellSize(pPage, pCell);
69719 u8 *pStop;
69720
69721 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
69722 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
69723 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
69724 rc = rebuildPage(pNew, 1, &pCell, &szCell);
69725 if( NEVER(rc) ) return rc;
69726 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
69727
69728 /* If this is an auto-vacuum database, update the pointer map
69729 ** with entries for the new page, and any pointer from the
69730 ** cell on the page to an overflow page. If either of these
69731 ** operations fails, the return code is set, but the contents
69732 ** of the parent page are still manipulated by thh code below.
69733 ** That is Ok, at this point the parent page is guaranteed to
69734 ** be marked as dirty. Returning an error code will cause a
69735 ** rollback, undoing any changes made to the parent page.
69736 */
69737 if( ISAUTOVACUUM ){
69738 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
69739 if( szCell>pNew->minLocal ){
69740 ptrmapPutOvflPtr(pNew, pCell, &rc);
69741 }
69742 }
69743
69744 /* Create a divider cell to insert into pParent. The divider cell
69745 ** consists of a 4-byte page number (the page number of pPage) and
69746 ** a variable length key value (which must be the same value as the
69747 ** largest key on pPage).
69748 **
69749 ** To find the largest key value on pPage, first find the right-most
69750 ** cell on pPage. The first two fields of this cell are the
69751 ** record-length (a variable length integer at most 32-bits in size)
69752 ** and the key value (a variable length integer, may have any value).
69753 ** The first of the while(...) loops below skips over the record-length
69754 ** field. The second while(...) loop copies the key value from the
69755 ** cell on pPage into the pSpace buffer.
69756 */
69757 pCell = findCell(pPage, pPage->nCell-1);
69758 pStop = &pCell[9];
69759 while( (*(pCell++)&0x80) && pCell<pStop );
69760 pStop = &pCell[9];
69761 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
69762
69763 /* Insert the new divider cell into pParent. */
69764 if( rc==SQLITE_OK ){
69765 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
69766 0, pPage->pgno, &rc);
69767 }
69768
69769 /* Set the right-child pointer of pParent to point to the new page. */
69770 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
69771
69772 /* Release the reference to the new page. */
69773 releasePage(pNew);
69774 }
69775
69776 return rc;
69777}
69778#endif /* SQLITE_OMIT_QUICKBALANCE */
69779
69780#if 0
69781/*
69782** This function does not contribute anything to the operation of SQLite.
69783** it is sometimes activated temporarily while debugging code responsible
69784** for setting pointer-map entries.
69785*/
69786static int ptrmapCheckPages(MemPage **apPage, int nPage){
69787 int i, j;
69788 for(i=0; i<nPage; i++){
69789 Pgno n;
69790 u8 e;
69791 MemPage *pPage = apPage[i];
69792 BtShared *pBt = pPage->pBt;
69793 assert( pPage->isInit );
69794
69795 for(j=0; j<pPage->nCell; j++){
69796 CellInfo info;
69797 u8 *z;
69798
69799 z = findCell(pPage, j);
69800 pPage->xParseCell(pPage, z, &info);
69801 if( info.nLocal<info.nPayload ){
69802 Pgno ovfl = get4byte(&z[info.nSize-4]);
69803 ptrmapGet(pBt, ovfl, &e, &n);
69804 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
69805 }
69806 if( !pPage->leaf ){
69807 Pgno child = get4byte(z);
69808 ptrmapGet(pBt, child, &e, &n);
69809 assert( n==pPage->pgno && e==PTRMAP_BTREE );
69810 }
69811 }
69812 if( !pPage->leaf ){
69813 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
69814 ptrmapGet(pBt, child, &e, &n);
69815 assert( n==pPage->pgno && e==PTRMAP_BTREE );
69816 }
69817 }
69818 return 1;
69819}
69820#endif
69821
69822/*
69823** This function is used to copy the contents of the b-tree node stored
69824** on page pFrom to page pTo. If page pFrom was not a leaf page, then
69825** the pointer-map entries for each child page are updated so that the
69826** parent page stored in the pointer map is page pTo. If pFrom contained
69827** any cells with overflow page pointers, then the corresponding pointer
69828** map entries are also updated so that the parent page is page pTo.
69829**
69830** If pFrom is currently carrying any overflow cells (entries in the
69831** MemPage.apOvfl[] array), they are not copied to pTo.
69832**
69833** Before returning, page pTo is reinitialized using btreeInitPage().
69834**
69835** The performance of this function is not critical. It is only used by
69836** the balance_shallower() and balance_deeper() procedures, neither of
69837** which are called often under normal circumstances.
69838*/
69839static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
69840 if( (*pRC)==SQLITE_OK ){
69841 BtShared * const pBt = pFrom->pBt;
69842 u8 * const aFrom = pFrom->aData;
69843 u8 * const aTo = pTo->aData;
69844 int const iFromHdr = pFrom->hdrOffset;
69845 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
69846 int rc;
69847 int iData;
69848
69849
69850 assert( pFrom->isInit );
69851 assert( pFrom->nFree>=iToHdr );
69852 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
69853
69854 /* Copy the b-tree node content from page pFrom to page pTo. */
69855 iData = get2byte(&aFrom[iFromHdr+5]);
69856 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
69857 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
69858
69859 /* Reinitialize page pTo so that the contents of the MemPage structure
69860 ** match the new data. The initialization of pTo can actually fail under
69861 ** fairly obscure circumstances, even though it is a copy of initialized
69862 ** page pFrom.
69863 */
69864 pTo->isInit = 0;
69865 rc = btreeInitPage(pTo);
69866 if( rc!=SQLITE_OK ){
69867 *pRC = rc;
69868 return;
69869 }
69870
69871 /* If this is an auto-vacuum database, update the pointer-map entries
69872 ** for any b-tree or overflow pages that pTo now contains the pointers to.
69873 */
69874 if( ISAUTOVACUUM ){
69875 *pRC = setChildPtrmaps(pTo);
69876 }
69877 }
69878}
69879
69880/*
69881** This routine redistributes cells on the iParentIdx'th child of pParent
69882** (hereafter "the page") and up to 2 siblings so that all pages have about the
69883** same amount of free space. Usually a single sibling on either side of the
69884** page are used in the balancing, though both siblings might come from one
69885** side if the page is the first or last child of its parent. If the page
69886** has fewer than 2 siblings (something which can only happen if the page
69887** is a root page or a child of a root page) then all available siblings
69888** participate in the balancing.
69889**
69890** The number of siblings of the page might be increased or decreased by
69891** one or two in an effort to keep pages nearly full but not over full.
69892**
69893** Note that when this routine is called, some of the cells on the page
69894** might not actually be stored in MemPage.aData[]. This can happen
69895** if the page is overfull. This routine ensures that all cells allocated
69896** to the page and its siblings fit into MemPage.aData[] before returning.
69897**
69898** In the course of balancing the page and its siblings, cells may be
69899** inserted into or removed from the parent page (pParent). Doing so
69900** may cause the parent page to become overfull or underfull. If this
69901** happens, it is the responsibility of the caller to invoke the correct
69902** balancing routine to fix this problem (see the balance() routine).
69903**
69904** If this routine fails for any reason, it might leave the database
69905** in a corrupted state. So if this routine fails, the database should
69906** be rolled back.
69907**
69908** The third argument to this function, aOvflSpace, is a pointer to a
69909** buffer big enough to hold one page. If while inserting cells into the parent
69910** page (pParent) the parent page becomes overfull, this buffer is
69911** used to store the parent's overflow cells. Because this function inserts
69912** a maximum of four divider cells into the parent page, and the maximum
69913** size of a cell stored within an internal node is always less than 1/4
69914** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
69915** enough for all overflow cells.
69916**
69917** If aOvflSpace is set to a null pointer, this function returns
69918** SQLITE_NOMEM.
69919*/
69920static int balance_nonroot(
69921 MemPage *pParent, /* Parent page of siblings being balanced */
69922 int iParentIdx, /* Index of "the page" in pParent */
69923 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
69924 int isRoot, /* True if pParent is a root-page */
69925 int bBulk /* True if this call is part of a bulk load */
69926){
69927 BtShared *pBt; /* The whole database */
69928 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
69929 int nNew = 0; /* Number of pages in apNew[] */
69930 int nOld; /* Number of pages in apOld[] */
69931 int i, j, k; /* Loop counters */
69932 int nxDiv; /* Next divider slot in pParent->aCell[] */
69933 int rc = SQLITE_OK; /* The return code */
69934 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
69935 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
69936 int usableSpace; /* Bytes in pPage beyond the header */
69937 int pageFlags; /* Value of pPage->aData[0] */
69938 int iSpace1 = 0; /* First unused byte of aSpace1[] */
69939 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
69940 int szScratch; /* Size of scratch memory requested */
69941 MemPage *apOld[NB]; /* pPage and up to two siblings */
69942 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
69943 u8 *pRight; /* Location in parent of right-sibling pointer */
69944 u8 *apDiv[NB-1]; /* Divider cells in pParent */
69945 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
69946 int cntOld[NB+2]; /* Old index in b.apCell[] */
69947 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
69948 u8 *aSpace1; /* Space for copies of dividers cells */
69949 Pgno pgno; /* Temp var to store a page number in */
69950 u8 abDone[NB+2]; /* True after i'th new page is populated */
69951 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
69952 Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
69953 u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
69954 CellArray b; /* Parsed information on cells being balanced */
69955
69956 memset(abDone, 0, sizeof(abDone));
69957 b.nCell = 0;
69958 b.apCell = 0;
69959 pBt = pParent->pBt;
69960 assert( sqlite3_mutex_held(pBt->mutex) );
69961 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
69962
69963#if 0
69964 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
69965#endif
69966
69967 /* At this point pParent may have at most one overflow cell. And if
69968 ** this overflow cell is present, it must be the cell with
69969 ** index iParentIdx. This scenario comes about when this function
69970 ** is called (indirectly) from sqlite3BtreeDelete().
69971 */
69972 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
69973 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
69974
69975 if( !aOvflSpace ){
69976 return SQLITE_NOMEM_BKPT;
69977 }
69978
69979 /* Find the sibling pages to balance. Also locate the cells in pParent
69980 ** that divide the siblings. An attempt is made to find NN siblings on
69981 ** either side of pPage. More siblings are taken from one side, however,
69982 ** if there are fewer than NN siblings on the other side. If pParent
69983 ** has NB or fewer children then all children of pParent are taken.
69984 **
69985 ** This loop also drops the divider cells from the parent page. This
69986 ** way, the remainder of the function does not have to deal with any
69987 ** overflow cells in the parent page, since if any existed they will
69988 ** have already been removed.
69989 */
69990 i = pParent->nOverflow + pParent->nCell;
69991 if( i<2 ){
69992 nxDiv = 0;
69993 }else{
69994 assert( bBulk==0 || bBulk==1 );
69995 if( iParentIdx==0 ){
69996 nxDiv = 0;
69997 }else if( iParentIdx==i ){
69998 nxDiv = i-2+bBulk;
69999 }else{
70000 nxDiv = iParentIdx-1;
70001 }
70002 i = 2-bBulk;
70003 }
70004 nOld = i+1;
70005 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
70006 pRight = &pParent->aData[pParent->hdrOffset+8];
70007 }else{
70008 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
70009 }
70010 pgno = get4byte(pRight);
70011 while( 1 ){
70012 rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
70013 if( rc ){
70014 memset(apOld, 0, (i+1)*sizeof(MemPage*));
70015 goto balance_cleanup;
70016 }
70017 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
70018 if( (i--)==0 ) break;
70019
70020 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
70021 apDiv[i] = pParent->apOvfl[0];
70022 pgno = get4byte(apDiv[i]);
70023 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
70024 pParent->nOverflow = 0;
70025 }else{
70026 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
70027 pgno = get4byte(apDiv[i]);
70028 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
70029
70030 /* Drop the cell from the parent page. apDiv[i] still points to
70031 ** the cell within the parent, even though it has been dropped.
70032 ** This is safe because dropping a cell only overwrites the first
70033 ** four bytes of it, and this function does not need the first
70034 ** four bytes of the divider cell. So the pointer is safe to use
70035 ** later on.
70036 **
70037 ** But not if we are in secure-delete mode. In secure-delete mode,
70038 ** the dropCell() routine will overwrite the entire cell with zeroes.
70039 ** In this case, temporarily copy the cell into the aOvflSpace[]
70040 ** buffer. It will be copied out again as soon as the aSpace[] buffer
70041 ** is allocated. */
70042 if( pBt->btsFlags & BTS_FAST_SECURE ){
70043 int iOff;
70044
70045 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
70046 if( (iOff+szNew[i])>(int)pBt->usableSize ){
70047 rc = SQLITE_CORRUPT_BKPT;
70048 memset(apOld, 0, (i+1)*sizeof(MemPage*));
70049 goto balance_cleanup;
70050 }else{
70051 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
70052 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
70053 }
70054 }
70055 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
70056 }
70057 }
70058
70059 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
70060 ** alignment */
70061 nMaxCells = (nMaxCells + 3)&~3;
70062
70063 /*
70064 ** Allocate space for memory structures
70065 */
70066 szScratch =
70067 nMaxCells*sizeof(u8*) /* b.apCell */
70068 + nMaxCells*sizeof(u16) /* b.szCell */
70069 + pBt->pageSize; /* aSpace1 */
70070
70071 assert( szScratch<=6*(int)pBt->pageSize );
70072 b.apCell = sqlite3StackAllocRaw(0, szScratch );
70073 if( b.apCell==0 ){
70074 rc = SQLITE_NOMEM_BKPT;
70075 goto balance_cleanup;
70076 }
70077 b.szCell = (u16*)&b.apCell[nMaxCells];
70078 aSpace1 = (u8*)&b.szCell[nMaxCells];
70079 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
70080
70081 /*
70082 ** Load pointers to all cells on sibling pages and the divider cells
70083 ** into the local b.apCell[] array. Make copies of the divider cells
70084 ** into space obtained from aSpace1[]. The divider cells have already
70085 ** been removed from pParent.
70086 **
70087 ** If the siblings are on leaf pages, then the child pointers of the
70088 ** divider cells are stripped from the cells before they are copied
70089 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
70090 ** child pointers. If siblings are not leaves, then all cell in
70091 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
70092 ** are alike.
70093 **
70094 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
70095 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
70096 */
70097 b.pRef = apOld[0];
70098 leafCorrection = b.pRef->leaf*4;
70099 leafData = b.pRef->intKeyLeaf;
70100 for(i=0; i<nOld; i++){
70101 MemPage *pOld = apOld[i];
70102 int limit = pOld->nCell;
70103 u8 *aData = pOld->aData;
70104 u16 maskPage = pOld->maskPage;
70105 u8 *piCell = aData + pOld->cellOffset;
70106 u8 *piEnd;
70107
70108 /* Verify that all sibling pages are of the same "type" (table-leaf,
70109 ** table-interior, index-leaf, or index-interior).
70110 */
70111 if( pOld->aData[0]!=apOld[0]->aData[0] ){
70112 rc = SQLITE_CORRUPT_BKPT;
70113 goto balance_cleanup;
70114 }
70115
70116 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
70117 ** contains overflow cells, include them in the b.apCell[] array
70118 ** in the correct spot.
70119 **
70120 ** Note that when there are multiple overflow cells, it is always the
70121 ** case that they are sequential and adjacent. This invariant arises
70122 ** because multiple overflows can only occurs when inserting divider
70123 ** cells into a parent on a prior balance, and divider cells are always
70124 ** adjacent and are inserted in order. There is an assert() tagged
70125 ** with "NOTE 1" in the overflow cell insertion loop to prove this
70126 ** invariant.
70127 **
70128 ** This must be done in advance. Once the balance starts, the cell
70129 ** offset section of the btree page will be overwritten and we will no
70130 ** long be able to find the cells if a pointer to each cell is not saved
70131 ** first.
70132 */
70133 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
70134 if( pOld->nOverflow>0 ){
70135 limit = pOld->aiOvfl[0];
70136 for(j=0; j<limit; j++){
70137 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
70138 piCell += 2;
70139 b.nCell++;
70140 }
70141 for(k=0; k<pOld->nOverflow; k++){
70142 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
70143 b.apCell[b.nCell] = pOld->apOvfl[k];
70144 b.nCell++;
70145 }
70146 }
70147 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
70148 while( piCell<piEnd ){
70149 assert( b.nCell<nMaxCells );
70150 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
70151 piCell += 2;
70152 b.nCell++;
70153 }
70154
70155 cntOld[i] = b.nCell;
70156 if( i<nOld-1 && !leafData){
70157 u16 sz = (u16)szNew[i];
70158 u8 *pTemp;
70159 assert( b.nCell<nMaxCells );
70160 b.szCell[b.nCell] = sz;
70161 pTemp = &aSpace1[iSpace1];
70162 iSpace1 += sz;
70163 assert( sz<=pBt->maxLocal+23 );
70164 assert( iSpace1 <= (int)pBt->pageSize );
70165 memcpy(pTemp, apDiv[i], sz);
70166 b.apCell[b.nCell] = pTemp+leafCorrection;
70167 assert( leafCorrection==0 || leafCorrection==4 );
70168 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
70169 if( !pOld->leaf ){
70170 assert( leafCorrection==0 );
70171 assert( pOld->hdrOffset==0 );
70172 /* The right pointer of the child page pOld becomes the left
70173 ** pointer of the divider cell */
70174 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
70175 }else{
70176 assert( leafCorrection==4 );
70177 while( b.szCell[b.nCell]<4 ){
70178 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
70179 ** does exist, pad it with 0x00 bytes. */
70180 assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
70181 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
70182 aSpace1[iSpace1++] = 0x00;
70183 b.szCell[b.nCell]++;
70184 }
70185 }
70186 b.nCell++;
70187 }
70188 }
70189
70190 /*
70191 ** Figure out the number of pages needed to hold all b.nCell cells.
70192 ** Store this number in "k". Also compute szNew[] which is the total
70193 ** size of all cells on the i-th page and cntNew[] which is the index
70194 ** in b.apCell[] of the cell that divides page i from page i+1.
70195 ** cntNew[k] should equal b.nCell.
70196 **
70197 ** Values computed by this block:
70198 **
70199 ** k: The total number of sibling pages
70200 ** szNew[i]: Spaced used on the i-th sibling page.
70201 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
70202 ** the right of the i-th sibling page.
70203 ** usableSpace: Number of bytes of space available on each sibling.
70204 **
70205 */
70206 usableSpace = pBt->usableSize - 12 + leafCorrection;
70207 for(i=0; i<nOld; i++){
70208 MemPage *p = apOld[i];
70209 szNew[i] = usableSpace - p->nFree;
70210 for(j=0; j<p->nOverflow; j++){
70211 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
70212 }
70213 cntNew[i] = cntOld[i];
70214 }
70215 k = nOld;
70216 for(i=0; i<k; i++){
70217 int sz;
70218 while( szNew[i]>usableSpace ){
70219 if( i+1>=k ){
70220 k = i+2;
70221 if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
70222 szNew[k-1] = 0;
70223 cntNew[k-1] = b.nCell;
70224 }
70225 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
70226 szNew[i] -= sz;
70227 if( !leafData ){
70228 if( cntNew[i]<b.nCell ){
70229 sz = 2 + cachedCellSize(&b, cntNew[i]);
70230 }else{
70231 sz = 0;
70232 }
70233 }
70234 szNew[i+1] += sz;
70235 cntNew[i]--;
70236 }
70237 while( cntNew[i]<b.nCell ){
70238 sz = 2 + cachedCellSize(&b, cntNew[i]);
70239 if( szNew[i]+sz>usableSpace ) break;
70240 szNew[i] += sz;
70241 cntNew[i]++;
70242 if( !leafData ){
70243 if( cntNew[i]<b.nCell ){
70244 sz = 2 + cachedCellSize(&b, cntNew[i]);
70245 }else{
70246 sz = 0;
70247 }
70248 }
70249 szNew[i+1] -= sz;
70250 }
70251 if( cntNew[i]>=b.nCell ){
70252 k = i+1;
70253 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
70254 rc = SQLITE_CORRUPT_BKPT;
70255 goto balance_cleanup;
70256 }
70257 }
70258
70259 /*
70260 ** The packing computed by the previous block is biased toward the siblings
70261 ** on the left side (siblings with smaller keys). The left siblings are
70262 ** always nearly full, while the right-most sibling might be nearly empty.
70263 ** The next block of code attempts to adjust the packing of siblings to
70264 ** get a better balance.
70265 **
70266 ** This adjustment is more than an optimization. The packing above might
70267 ** be so out of balance as to be illegal. For example, the right-most
70268 ** sibling might be completely empty. This adjustment is not optional.
70269 */
70270 for(i=k-1; i>0; i--){
70271 int szRight = szNew[i]; /* Size of sibling on the right */
70272 int szLeft = szNew[i-1]; /* Size of sibling on the left */
70273 int r; /* Index of right-most cell in left sibling */
70274 int d; /* Index of first cell to the left of right sibling */
70275
70276 r = cntNew[i-1] - 1;
70277 d = r + 1 - leafData;
70278 (void)cachedCellSize(&b, d);
70279 do{
70280 assert( d<nMaxCells );
70281 assert( r<nMaxCells );
70282 (void)cachedCellSize(&b, r);
70283 if( szRight!=0
70284 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
70285 break;
70286 }
70287 szRight += b.szCell[d] + 2;
70288 szLeft -= b.szCell[r] + 2;
70289 cntNew[i-1] = r;
70290 r--;
70291 d--;
70292 }while( r>=0 );
70293 szNew[i] = szRight;
70294 szNew[i-1] = szLeft;
70295 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
70296 rc = SQLITE_CORRUPT_BKPT;
70297 goto balance_cleanup;
70298 }
70299 }
70300
70301 /* Sanity check: For a non-corrupt database file one of the follwing
70302 ** must be true:
70303 ** (1) We found one or more cells (cntNew[0])>0), or
70304 ** (2) pPage is a virtual root page. A virtual root page is when
70305 ** the real root page is page 1 and we are the only child of
70306 ** that page.
70307 */
70308 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
70309 TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n",
70310 apOld[0]->pgno, apOld[0]->nCell,
70311 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
70312 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
70313 ));
70314
70315 /*
70316 ** Allocate k new pages. Reuse old pages where possible.
70317 */
70318 pageFlags = apOld[0]->aData[0];
70319 for(i=0; i<k; i++){
70320 MemPage *pNew;
70321 if( i<nOld ){
70322 pNew = apNew[i] = apOld[i];
70323 apOld[i] = 0;
70324 rc = sqlite3PagerWrite(pNew->pDbPage);
70325 nNew++;
70326 if( rc ) goto balance_cleanup;
70327 }else{
70328 assert( i>0 );
70329 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
70330 if( rc ) goto balance_cleanup;
70331 zeroPage(pNew, pageFlags);
70332 apNew[i] = pNew;
70333 nNew++;
70334 cntOld[i] = b.nCell;
70335
70336 /* Set the pointer-map entry for the new sibling page. */
70337 if( ISAUTOVACUUM ){
70338 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
70339 if( rc!=SQLITE_OK ){
70340 goto balance_cleanup;
70341 }
70342 }
70343 }
70344 }
70345
70346 /*
70347 ** Reassign page numbers so that the new pages are in ascending order.
70348 ** This helps to keep entries in the disk file in order so that a scan
70349 ** of the table is closer to a linear scan through the file. That in turn
70350 ** helps the operating system to deliver pages from the disk more rapidly.
70351 **
70352 ** An O(n^2) insertion sort algorithm is used, but since n is never more
70353 ** than (NB+2) (a small constant), that should not be a problem.
70354 **
70355 ** When NB==3, this one optimization makes the database about 25% faster
70356 ** for large insertions and deletions.
70357 */
70358 for(i=0; i<nNew; i++){
70359 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
70360 aPgFlags[i] = apNew[i]->pDbPage->flags;
70361 for(j=0; j<i; j++){
70362 if( aPgno[j]==aPgno[i] ){
70363 /* This branch is taken if the set of sibling pages somehow contains
70364 ** duplicate entries. This can happen if the database is corrupt.
70365 ** It would be simpler to detect this as part of the loop below, but
70366 ** we do the detection here in order to avoid populating the pager
70367 ** cache with two separate objects associated with the same
70368 ** page number. */
70369 assert( CORRUPT_DB );
70370 rc = SQLITE_CORRUPT_BKPT;
70371 goto balance_cleanup;
70372 }
70373 }
70374 }
70375 for(i=0; i<nNew; i++){
70376 int iBest = 0; /* aPgno[] index of page number to use */
70377 for(j=1; j<nNew; j++){
70378 if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
70379 }
70380 pgno = aPgOrder[iBest];
70381 aPgOrder[iBest] = 0xffffffff;
70382 if( iBest!=i ){
70383 if( iBest>i ){
70384 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
70385 }
70386 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
70387 apNew[i]->pgno = pgno;
70388 }
70389 }
70390
70391 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
70392 "%d(%d nc=%d) %d(%d nc=%d)\n",
70393 apNew[0]->pgno, szNew[0], cntNew[0],
70394 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
70395 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
70396 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
70397 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
70398 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
70399 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
70400 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
70401 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
70402 ));
70403
70404 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
70405 put4byte(pRight, apNew[nNew-1]->pgno);
70406
70407 /* If the sibling pages are not leaves, ensure that the right-child pointer
70408 ** of the right-most new sibling page is set to the value that was
70409 ** originally in the same field of the right-most old sibling page. */
70410 if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
70411 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
70412 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
70413 }
70414
70415 /* Make any required updates to pointer map entries associated with
70416 ** cells stored on sibling pages following the balance operation. Pointer
70417 ** map entries associated with divider cells are set by the insertCell()
70418 ** routine. The associated pointer map entries are:
70419 **
70420 ** a) if the cell contains a reference to an overflow chain, the
70421 ** entry associated with the first page in the overflow chain, and
70422 **
70423 ** b) if the sibling pages are not leaves, the child page associated
70424 ** with the cell.
70425 **
70426 ** If the sibling pages are not leaves, then the pointer map entry
70427 ** associated with the right-child of each sibling may also need to be
70428 ** updated. This happens below, after the sibling pages have been
70429 ** populated, not here.
70430 */
70431 if( ISAUTOVACUUM ){
70432 MemPage *pNew = apNew[0];
70433 u8 *aOld = pNew->aData;
70434 int cntOldNext = pNew->nCell + pNew->nOverflow;
70435 int usableSize = pBt->usableSize;
70436 int iNew = 0;
70437 int iOld = 0;
70438
70439 for(i=0; i<b.nCell; i++){
70440 u8 *pCell = b.apCell[i];
70441 if( i==cntOldNext ){
70442 MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
70443 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
70444 aOld = pOld->aData;
70445 }
70446 if( i==cntNew[iNew] ){
70447 pNew = apNew[++iNew];
70448 if( !leafData ) continue;
70449 }
70450
70451 /* Cell pCell is destined for new sibling page pNew. Originally, it
70452 ** was either part of sibling page iOld (possibly an overflow cell),
70453 ** or else the divider cell to the left of sibling page iOld. So,
70454 ** if sibling page iOld had the same page number as pNew, and if
70455 ** pCell really was a part of sibling page iOld (not a divider or
70456 ** overflow cell), we can skip updating the pointer map entries. */
70457 if( iOld>=nNew
70458 || pNew->pgno!=aPgno[iOld]
70459 || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize])
70460 ){
70461 if( !leafCorrection ){
70462 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
70463 }
70464 if( cachedCellSize(&b,i)>pNew->minLocal ){
70465 ptrmapPutOvflPtr(pNew, pCell, &rc);
70466 }
70467 if( rc ) goto balance_cleanup;
70468 }
70469 }
70470 }
70471
70472 /* Insert new divider cells into pParent. */
70473 for(i=0; i<nNew-1; i++){
70474 u8 *pCell;
70475 u8 *pTemp;
70476 int sz;
70477 MemPage *pNew = apNew[i];
70478 j = cntNew[i];
70479
70480 assert( j<nMaxCells );
70481 assert( b.apCell[j]!=0 );
70482 pCell = b.apCell[j];
70483 sz = b.szCell[j] + leafCorrection;
70484 pTemp = &aOvflSpace[iOvflSpace];
70485 if( !pNew->leaf ){
70486 memcpy(&pNew->aData[8], pCell, 4);
70487 }else if( leafData ){
70488 /* If the tree is a leaf-data tree, and the siblings are leaves,
70489 ** then there is no divider cell in b.apCell[]. Instead, the divider
70490 ** cell consists of the integer key for the right-most cell of
70491 ** the sibling-page assembled above only.
70492 */
70493 CellInfo info;
70494 j--;
70495 pNew->xParseCell(pNew, b.apCell[j], &info);
70496 pCell = pTemp;
70497 sz = 4 + putVarint(&pCell[4], info.nKey);
70498 pTemp = 0;
70499 }else{
70500 pCell -= 4;
70501 /* Obscure case for non-leaf-data trees: If the cell at pCell was
70502 ** previously stored on a leaf node, and its reported size was 4
70503 ** bytes, then it may actually be smaller than this
70504 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
70505 ** any cell). But it is important to pass the correct size to
70506 ** insertCell(), so reparse the cell now.
70507 **
70508 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
70509 ** and WITHOUT ROWID tables with exactly one column which is the
70510 ** primary key.
70511 */
70512 if( b.szCell[j]==4 ){
70513 assert(leafCorrection==4);
70514 sz = pParent->xCellSize(pParent, pCell);
70515 }
70516 }
70517 iOvflSpace += sz;
70518 assert( sz<=pBt->maxLocal+23 );
70519 assert( iOvflSpace <= (int)pBt->pageSize );
70520 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
70521 if( rc!=SQLITE_OK ) goto balance_cleanup;
70522 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
70523 }
70524
70525 /* Now update the actual sibling pages. The order in which they are updated
70526 ** is important, as this code needs to avoid disrupting any page from which
70527 ** cells may still to be read. In practice, this means:
70528 **
70529 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
70530 ** then it is not safe to update page apNew[iPg] until after
70531 ** the left-hand sibling apNew[iPg-1] has been updated.
70532 **
70533 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
70534 ** then it is not safe to update page apNew[iPg] until after
70535 ** the right-hand sibling apNew[iPg+1] has been updated.
70536 **
70537 ** If neither of the above apply, the page is safe to update.
70538 **
70539 ** The iPg value in the following loop starts at nNew-1 goes down
70540 ** to 0, then back up to nNew-1 again, thus making two passes over
70541 ** the pages. On the initial downward pass, only condition (1) above
70542 ** needs to be tested because (2) will always be true from the previous
70543 ** step. On the upward pass, both conditions are always true, so the
70544 ** upwards pass simply processes pages that were missed on the downward
70545 ** pass.
70546 */
70547 for(i=1-nNew; i<nNew; i++){
70548 int iPg = i<0 ? -i : i;
70549 assert( iPg>=0 && iPg<nNew );
70550 if( abDone[iPg] ) continue; /* Skip pages already processed */
70551 if( i>=0 /* On the upwards pass, or... */
70552 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
70553 ){
70554 int iNew;
70555 int iOld;
70556 int nNewCell;
70557
70558 /* Verify condition (1): If cells are moving left, update iPg
70559 ** only after iPg-1 has already been updated. */
70560 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
70561
70562 /* Verify condition (2): If cells are moving right, update iPg
70563 ** only after iPg+1 has already been updated. */
70564 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
70565
70566 if( iPg==0 ){
70567 iNew = iOld = 0;
70568 nNewCell = cntNew[0];
70569 }else{
70570 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
70571 iNew = cntNew[iPg-1] + !leafData;
70572 nNewCell = cntNew[iPg] - iNew;
70573 }
70574
70575 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
70576 if( rc ) goto balance_cleanup;
70577 abDone[iPg]++;
70578 apNew[iPg]->nFree = usableSpace-szNew[iPg];
70579 assert( apNew[iPg]->nOverflow==0 );
70580 assert( apNew[iPg]->nCell==nNewCell );
70581 }
70582 }
70583
70584 /* All pages have been processed exactly once */
70585 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
70586
70587 assert( nOld>0 );
70588 assert( nNew>0 );
70589
70590 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
70591 /* The root page of the b-tree now contains no cells. The only sibling
70592 ** page is the right-child of the parent. Copy the contents of the
70593 ** child page into the parent, decreasing the overall height of the
70594 ** b-tree structure by one. This is described as the "balance-shallower"
70595 ** sub-algorithm in some documentation.
70596 **
70597 ** If this is an auto-vacuum database, the call to copyNodeContent()
70598 ** sets all pointer-map entries corresponding to database image pages
70599 ** for which the pointer is stored within the content being copied.
70600 **
70601 ** It is critical that the child page be defragmented before being
70602 ** copied into the parent, because if the parent is page 1 then it will
70603 ** by smaller than the child due to the database header, and so all the
70604 ** free space needs to be up front.
70605 */
70606 assert( nNew==1 || CORRUPT_DB );
70607 rc = defragmentPage(apNew[0], -1);
70608 testcase( rc!=SQLITE_OK );
70609 assert( apNew[0]->nFree ==
70610 (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
70611 || rc!=SQLITE_OK
70612 );
70613 copyNodeContent(apNew[0], pParent, &rc);
70614 freePage(apNew[0], &rc);
70615 }else if( ISAUTOVACUUM && !leafCorrection ){
70616 /* Fix the pointer map entries associated with the right-child of each
70617 ** sibling page. All other pointer map entries have already been taken
70618 ** care of. */
70619 for(i=0; i<nNew; i++){
70620 u32 key = get4byte(&apNew[i]->aData[8]);
70621 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
70622 }
70623 }
70624
70625 assert( pParent->isInit );
70626 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
70627 nOld, nNew, b.nCell));
70628
70629 /* Free any old pages that were not reused as new pages.
70630 */
70631 for(i=nNew; i<nOld; i++){
70632 freePage(apOld[i], &rc);
70633 }
70634
70635#if 0
70636 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
70637 /* The ptrmapCheckPages() contains assert() statements that verify that
70638 ** all pointer map pages are set correctly. This is helpful while
70639 ** debugging. This is usually disabled because a corrupt database may
70640 ** cause an assert() statement to fail. */
70641 ptrmapCheckPages(apNew, nNew);
70642 ptrmapCheckPages(&pParent, 1);
70643 }
70644#endif
70645
70646 /*
70647 ** Cleanup before returning.
70648 */
70649balance_cleanup:
70650 sqlite3StackFree(0, b.apCell);
70651 for(i=0; i<nOld; i++){
70652 releasePage(apOld[i]);
70653 }
70654 for(i=0; i<nNew; i++){
70655 releasePage(apNew[i]);
70656 }
70657
70658 return rc;
70659}
70660
70661
70662/*
70663** This function is called when the root page of a b-tree structure is
70664** overfull (has one or more overflow pages).
70665**
70666** A new child page is allocated and the contents of the current root
70667** page, including overflow cells, are copied into the child. The root
70668** page is then overwritten to make it an empty page with the right-child
70669** pointer pointing to the new page.
70670**
70671** Before returning, all pointer-map entries corresponding to pages
70672** that the new child-page now contains pointers to are updated. The
70673** entry corresponding to the new right-child pointer of the root
70674** page is also updated.
70675**
70676** If successful, *ppChild is set to contain a reference to the child
70677** page and SQLITE_OK is returned. In this case the caller is required
70678** to call releasePage() on *ppChild exactly once. If an error occurs,
70679** an error code is returned and *ppChild is set to 0.
70680*/
70681static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
70682 int rc; /* Return value from subprocedures */
70683 MemPage *pChild = 0; /* Pointer to a new child page */
70684 Pgno pgnoChild = 0; /* Page number of the new child page */
70685 BtShared *pBt = pRoot->pBt; /* The BTree */
70686
70687 assert( pRoot->nOverflow>0 );
70688 assert( sqlite3_mutex_held(pBt->mutex) );
70689
70690 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
70691 ** page that will become the new right-child of pPage. Copy the contents
70692 ** of the node stored on pRoot into the new child page.
70693 */
70694 rc = sqlite3PagerWrite(pRoot->pDbPage);
70695 if( rc==SQLITE_OK ){
70696 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
70697 copyNodeContent(pRoot, pChild, &rc);
70698 if( ISAUTOVACUUM ){
70699 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
70700 }
70701 }
70702 if( rc ){
70703 *ppChild = 0;
70704 releasePage(pChild);
70705 return rc;
70706 }
70707 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
70708 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
70709 assert( pChild->nCell==pRoot->nCell );
70710
70711 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
70712
70713 /* Copy the overflow cells from pRoot to pChild */
70714 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
70715 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
70716 memcpy(pChild->apOvfl, pRoot->apOvfl,
70717 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
70718 pChild->nOverflow = pRoot->nOverflow;
70719
70720 /* Zero the contents of pRoot. Then install pChild as the right-child. */
70721 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
70722 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
70723
70724 *ppChild = pChild;
70725 return SQLITE_OK;
70726}
70727
70728/*
70729** The page that pCur currently points to has just been modified in
70730** some way. This function figures out if this modification means the
70731** tree needs to be balanced, and if so calls the appropriate balancing
70732** routine. Balancing routines are:
70733**
70734** balance_quick()
70735** balance_deeper()
70736** balance_nonroot()
70737*/
70738static int balance(BtCursor *pCur){
70739 int rc = SQLITE_OK;
70740 const int nMin = pCur->pBt->usableSize * 2 / 3;
70741 u8 aBalanceQuickSpace[13];
70742 u8 *pFree = 0;
70743
70744 VVA_ONLY( int balance_quick_called = 0 );
70745 VVA_ONLY( int balance_deeper_called = 0 );
70746
70747 do {
70748 int iPage = pCur->iPage;
70749 MemPage *pPage = pCur->pPage;
70750
70751 if( iPage==0 ){
70752 if( pPage->nOverflow ){
70753 /* The root page of the b-tree is overfull. In this case call the
70754 ** balance_deeper() function to create a new child for the root-page
70755 ** and copy the current contents of the root-page to it. The
70756 ** next iteration of the do-loop will balance the child page.
70757 */
70758 assert( balance_deeper_called==0 );
70759 VVA_ONLY( balance_deeper_called++ );
70760 rc = balance_deeper(pPage, &pCur->apPage[1]);
70761 if( rc==SQLITE_OK ){
70762 pCur->iPage = 1;
70763 pCur->ix = 0;
70764 pCur->aiIdx[0] = 0;
70765 pCur->apPage[0] = pPage;
70766 pCur->pPage = pCur->apPage[1];
70767 assert( pCur->pPage->nOverflow );
70768 }
70769 }else{
70770 break;
70771 }
70772 }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
70773 break;
70774 }else{
70775 MemPage * const pParent = pCur->apPage[iPage-1];
70776 int const iIdx = pCur->aiIdx[iPage-1];
70777
70778 rc = sqlite3PagerWrite(pParent->pDbPage);
70779 if( rc==SQLITE_OK ){
70780#ifndef SQLITE_OMIT_QUICKBALANCE
70781 if( pPage->intKeyLeaf
70782 && pPage->nOverflow==1
70783 && pPage->aiOvfl[0]==pPage->nCell
70784 && pParent->pgno!=1
70785 && pParent->nCell==iIdx
70786 ){
70787 /* Call balance_quick() to create a new sibling of pPage on which
70788 ** to store the overflow cell. balance_quick() inserts a new cell
70789 ** into pParent, which may cause pParent overflow. If this
70790 ** happens, the next iteration of the do-loop will balance pParent
70791 ** use either balance_nonroot() or balance_deeper(). Until this
70792 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
70793 ** buffer.
70794 **
70795 ** The purpose of the following assert() is to check that only a
70796 ** single call to balance_quick() is made for each call to this
70797 ** function. If this were not verified, a subtle bug involving reuse
70798 ** of the aBalanceQuickSpace[] might sneak in.
70799 */
70800 assert( balance_quick_called==0 );
70801 VVA_ONLY( balance_quick_called++ );
70802 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
70803 }else
70804#endif
70805 {
70806 /* In this case, call balance_nonroot() to redistribute cells
70807 ** between pPage and up to 2 of its sibling pages. This involves
70808 ** modifying the contents of pParent, which may cause pParent to
70809 ** become overfull or underfull. The next iteration of the do-loop
70810 ** will balance the parent page to correct this.
70811 **
70812 ** If the parent page becomes overfull, the overflow cell or cells
70813 ** are stored in the pSpace buffer allocated immediately below.
70814 ** A subsequent iteration of the do-loop will deal with this by
70815 ** calling balance_nonroot() (balance_deeper() may be called first,
70816 ** but it doesn't deal with overflow cells - just moves them to a
70817 ** different page). Once this subsequent call to balance_nonroot()
70818 ** has completed, it is safe to release the pSpace buffer used by
70819 ** the previous call, as the overflow cell data will have been
70820 ** copied either into the body of a database page or into the new
70821 ** pSpace buffer passed to the latter call to balance_nonroot().
70822 */
70823 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
70824 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
70825 pCur->hints&BTREE_BULKLOAD);
70826 if( pFree ){
70827 /* If pFree is not NULL, it points to the pSpace buffer used
70828 ** by a previous call to balance_nonroot(). Its contents are
70829 ** now stored either on real database pages or within the
70830 ** new pSpace buffer, so it may be safely freed here. */
70831 sqlite3PageFree(pFree);
70832 }
70833
70834 /* The pSpace buffer will be freed after the next call to
70835 ** balance_nonroot(), or just before this function returns, whichever
70836 ** comes first. */
70837 pFree = pSpace;
70838 }
70839 }
70840
70841 pPage->nOverflow = 0;
70842
70843 /* The next iteration of the do-loop balances the parent page. */
70844 releasePage(pPage);
70845 pCur->iPage--;
70846 assert( pCur->iPage>=0 );
70847 pCur->pPage = pCur->apPage[pCur->iPage];
70848 }
70849 }while( rc==SQLITE_OK );
70850
70851 if( pFree ){
70852 sqlite3PageFree(pFree);
70853 }
70854 return rc;
70855}
70856
70857/* Overwrite content from pX into pDest. Only do the write if the
70858** content is different from what is already there.
70859*/
70860static int btreeOverwriteContent(
70861 MemPage *pPage, /* MemPage on which writing will occur */
70862 u8 *pDest, /* Pointer to the place to start writing */
70863 const BtreePayload *pX, /* Source of data to write */
70864 int iOffset, /* Offset of first byte to write */
70865 int iAmt /* Number of bytes to be written */
70866){
70867 int nData = pX->nData - iOffset;
70868 if( nData<=0 ){
70869 /* Overwritting with zeros */
70870 int i;
70871 for(i=0; i<iAmt && pDest[i]==0; i++){}
70872 if( i<iAmt ){
70873 int rc = sqlite3PagerWrite(pPage->pDbPage);
70874 if( rc ) return rc;
70875 memset(pDest + i, 0, iAmt - i);
70876 }
70877 }else{
70878 if( nData<iAmt ){
70879 /* Mixed read data and zeros at the end. Make a recursive call
70880 ** to write the zeros then fall through to write the real data */
70881 int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
70882 iAmt-nData);
70883 if( rc ) return rc;
70884 iAmt = nData;
70885 }
70886 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
70887 int rc = sqlite3PagerWrite(pPage->pDbPage);
70888 if( rc ) return rc;
70889 memcpy(pDest, ((u8*)pX->pData) + iOffset, iAmt);
70890 }
70891 }
70892 return SQLITE_OK;
70893}
70894
70895/*
70896** Overwrite the cell that cursor pCur is pointing to with fresh content
70897** contained in pX.
70898*/
70899static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
70900 int iOffset; /* Next byte of pX->pData to write */
70901 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
70902 int rc; /* Return code */
70903 MemPage *pPage = pCur->pPage; /* Page being written */
70904 BtShared *pBt; /* Btree */
70905 Pgno ovflPgno; /* Next overflow page to write */
70906 u32 ovflPageSize; /* Size to write on overflow page */
70907
70908 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd ){
70909 return SQLITE_CORRUPT_BKPT;
70910 }
70911 /* Overwrite the local portion first */
70912 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
70913 0, pCur->info.nLocal);
70914 if( rc ) return rc;
70915 if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
70916
70917 /* Now overwrite the overflow pages */
70918 iOffset = pCur->info.nLocal;
70919 assert( nTotal>=0 );
70920 assert( iOffset>=0 );
70921 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
70922 pBt = pPage->pBt;
70923 ovflPageSize = pBt->usableSize - 4;
70924 do{
70925 rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
70926 if( rc ) return rc;
70927 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 ){
70928 rc = SQLITE_CORRUPT_BKPT;
70929 }else{
70930 if( iOffset+ovflPageSize<(u32)nTotal ){
70931 ovflPgno = get4byte(pPage->aData);
70932 }else{
70933 ovflPageSize = nTotal - iOffset;
70934 }
70935 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
70936 iOffset, ovflPageSize);
70937 }
70938 sqlite3PagerUnref(pPage->pDbPage);
70939 if( rc ) return rc;
70940 iOffset += ovflPageSize;
70941 }while( iOffset<nTotal );
70942 return SQLITE_OK;
70943}
70944
70945
70946/*
70947** Insert a new record into the BTree. The content of the new record
70948** is described by the pX object. The pCur cursor is used only to
70949** define what table the record should be inserted into, and is left
70950** pointing at a random location.
70951**
70952** For a table btree (used for rowid tables), only the pX.nKey value of
70953** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
70954** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
70955** hold the content of the row.
70956**
70957** For an index btree (used for indexes and WITHOUT ROWID tables), the
70958** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
70959** pX.pData,nData,nZero fields must be zero.
70960**
70961** If the seekResult parameter is non-zero, then a successful call to
70962** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already
70963** been performed. In other words, if seekResult!=0 then the cursor
70964** is currently pointing to a cell that will be adjacent to the cell
70965** to be inserted. If seekResult<0 then pCur points to a cell that is
70966** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
70967** that is larger than (pKey,nKey).
70968**
70969** If seekResult==0, that means pCur is pointing at some unknown location.
70970** In that case, this routine must seek the cursor to the correct insertion
70971** point for (pKey,nKey) before doing the insertion. For index btrees,
70972** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
70973** key values and pX->aMem can be used instead of pX->pKey to avoid having
70974** to decode the key.
70975*/
70976SQLITE_PRIVATE int sqlite3BtreeInsert(
70977 BtCursor *pCur, /* Insert data into the table of this cursor */
70978 const BtreePayload *pX, /* Content of the row to be inserted */
70979 int flags, /* True if this is likely an append */
70980 int seekResult /* Result of prior MovetoUnpacked() call */
70981){
70982 int rc;
70983 int loc = seekResult; /* -1: before desired location +1: after */
70984 int szNew = 0;
70985 int idx;
70986 MemPage *pPage;
70987 Btree *p = pCur->pBtree;
70988 BtShared *pBt = p->pBt;
70989 unsigned char *oldCell;
70990 unsigned char *newCell = 0;
70991
70992 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
70993
70994 if( pCur->eState==CURSOR_FAULT ){
70995 assert( pCur->skipNext!=SQLITE_OK );
70996 return pCur->skipNext;
70997 }
70998
70999 assert( cursorOwnsBtShared(pCur) );
71000 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
71001 && pBt->inTransaction==TRANS_WRITE
71002 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
71003 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
71004
71005 /* Assert that the caller has been consistent. If this cursor was opened
71006 ** expecting an index b-tree, then the caller should be inserting blob
71007 ** keys with no associated data. If the cursor was opened expecting an
71008 ** intkey table, the caller should be inserting integer keys with a
71009 ** blob of associated data. */
71010 assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
71011
71012 /* Save the positions of any other cursors open on this table.
71013 **
71014 ** In some cases, the call to btreeMoveto() below is a no-op. For
71015 ** example, when inserting data into a table with auto-generated integer
71016 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
71017 ** integer key to use. It then calls this function to actually insert the
71018 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
71019 ** that the cursor is already where it needs to be and returns without
71020 ** doing any work. To avoid thwarting these optimizations, it is important
71021 ** not to clear the cursor here.
71022 */
71023 if( pCur->curFlags & BTCF_Multiple ){
71024 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
71025 if( rc ) return rc;
71026 }
71027
71028 if( pCur->pKeyInfo==0 ){
71029 assert( pX->pKey==0 );
71030 /* If this is an insert into a table b-tree, invalidate any incrblob
71031 ** cursors open on the row being replaced */
71032 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
71033
71034 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
71035 ** to a row with the same key as the new entry being inserted.
71036 */
71037#ifdef SQLITE_DEBUG
71038 if( flags & BTREE_SAVEPOSITION ){
71039 assert( pCur->curFlags & BTCF_ValidNKey );
71040 assert( pX->nKey==pCur->info.nKey );
71041 assert( pCur->info.nSize!=0 );
71042 assert( loc==0 );
71043 }
71044#endif
71045
71046 /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
71047 ** that the cursor is not pointing to a row to be overwritten.
71048 ** So do a complete check.
71049 */
71050 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
71051 /* The cursor is pointing to the entry that is to be
71052 ** overwritten */
71053 assert( pX->nData>=0 && pX->nZero>=0 );
71054 if( pCur->info.nSize!=0
71055 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
71056 ){
71057 /* New entry is the same size as the old. Do an overwrite */
71058 return btreeOverwriteCell(pCur, pX);
71059 }
71060 assert( loc==0 );
71061 }else if( loc==0 ){
71062 /* The cursor is *not* pointing to the cell to be overwritten, nor
71063 ** to an adjacent cell. Move the cursor so that it is pointing either
71064 ** to the cell to be overwritten or an adjacent cell.
71065 */
71066 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
71067 if( rc ) return rc;
71068 }
71069 }else{
71070 /* This is an index or a WITHOUT ROWID table */
71071
71072 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
71073 ** to a row with the same key as the new entry being inserted.
71074 */
71075 assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 );
71076
71077 /* If the cursor is not already pointing either to the cell to be
71078 ** overwritten, or if a new cell is being inserted, if the cursor is
71079 ** not pointing to an immediately adjacent cell, then move the cursor
71080 ** so that it does.
71081 */
71082 if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
71083 if( pX->nMem ){
71084 UnpackedRecord r;
71085 r.pKeyInfo = pCur->pKeyInfo;
71086 r.aMem = pX->aMem;
71087 r.nField = pX->nMem;
71088 r.default_rc = 0;
71089 r.errCode = 0;
71090 r.r1 = 0;
71091 r.r2 = 0;
71092 r.eqSeen = 0;
71093 rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
71094 }else{
71095 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
71096 }
71097 if( rc ) return rc;
71098 }
71099
71100 /* If the cursor is currently pointing to an entry to be overwritten
71101 ** and the new content is the same as as the old, then use the
71102 ** overwrite optimization.
71103 */
71104 if( loc==0 ){
71105 getCellInfo(pCur);
71106 if( pCur->info.nKey==pX->nKey ){
71107 BtreePayload x2;
71108 x2.pData = pX->pKey;
71109 x2.nData = pX->nKey;
71110 x2.nZero = 0;
71111 return btreeOverwriteCell(pCur, &x2);
71112 }
71113 }
71114
71115 }
71116 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
71117
71118 pPage = pCur->pPage;
71119 assert( pPage->intKey || pX->nKey>=0 );
71120 assert( pPage->leaf || !pPage->intKey );
71121
71122 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
71123 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
71124 loc==0 ? "overwrite" : "new entry"));
71125 assert( pPage->isInit );
71126 newCell = pBt->pTmpSpace;
71127 assert( newCell!=0 );
71128 rc = fillInCell(pPage, newCell, pX, &szNew);
71129 if( rc ) goto end_insert;
71130 assert( szNew==pPage->xCellSize(pPage, newCell) );
71131 assert( szNew <= MX_CELL_SIZE(pBt) );
71132 idx = pCur->ix;
71133 if( loc==0 ){
71134 CellInfo info;
71135 assert( idx<pPage->nCell );
71136 rc = sqlite3PagerWrite(pPage->pDbPage);
71137 if( rc ){
71138 goto end_insert;
71139 }
71140 oldCell = findCell(pPage, idx);
71141 if( !pPage->leaf ){
71142 memcpy(newCell, oldCell, 4);
71143 }
71144 rc = clearCell(pPage, oldCell, &info);
71145 if( info.nSize==szNew && info.nLocal==info.nPayload
71146 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
71147 ){
71148 /* Overwrite the old cell with the new if they are the same size.
71149 ** We could also try to do this if the old cell is smaller, then add
71150 ** the leftover space to the free list. But experiments show that
71151 ** doing that is no faster then skipping this optimization and just
71152 ** calling dropCell() and insertCell().
71153 **
71154 ** This optimization cannot be used on an autovacuum database if the
71155 ** new entry uses overflow pages, as the insertCell() call below is
71156 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
71157 assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
71158 if( oldCell+szNew > pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
71159 memcpy(oldCell, newCell, szNew);
71160 return SQLITE_OK;
71161 }
71162 dropCell(pPage, idx, info.nSize, &rc);
71163 if( rc ) goto end_insert;
71164 }else if( loc<0 && pPage->nCell>0 ){
71165 assert( pPage->leaf );
71166 idx = ++pCur->ix;
71167 pCur->curFlags &= ~BTCF_ValidNKey;
71168 }else{
71169 assert( pPage->leaf );
71170 }
71171 insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
71172 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
71173 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
71174
71175 /* If no error has occurred and pPage has an overflow cell, call balance()
71176 ** to redistribute the cells within the tree. Since balance() may move
71177 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
71178 ** variables.
71179 **
71180 ** Previous versions of SQLite called moveToRoot() to move the cursor
71181 ** back to the root page as balance() used to invalidate the contents
71182 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
71183 ** set the cursor state to "invalid". This makes common insert operations
71184 ** slightly faster.
71185 **
71186 ** There is a subtle but important optimization here too. When inserting
71187 ** multiple records into an intkey b-tree using a single cursor (as can
71188 ** happen while processing an "INSERT INTO ... SELECT" statement), it
71189 ** is advantageous to leave the cursor pointing to the last entry in
71190 ** the b-tree if possible. If the cursor is left pointing to the last
71191 ** entry in the table, and the next row inserted has an integer key
71192 ** larger than the largest existing key, it is possible to insert the
71193 ** row without seeking the cursor. This can be a big performance boost.
71194 */
71195 pCur->info.nSize = 0;
71196 if( pPage->nOverflow ){
71197 assert( rc==SQLITE_OK );
71198 pCur->curFlags &= ~(BTCF_ValidNKey);
71199 rc = balance(pCur);
71200
71201 /* Must make sure nOverflow is reset to zero even if the balance()
71202 ** fails. Internal data structure corruption will result otherwise.
71203 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
71204 ** from trying to save the current position of the cursor. */
71205 pCur->pPage->nOverflow = 0;
71206 pCur->eState = CURSOR_INVALID;
71207 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
71208 btreeReleaseAllCursorPages(pCur);
71209 if( pCur->pKeyInfo ){
71210 assert( pCur->pKey==0 );
71211 pCur->pKey = sqlite3Malloc( pX->nKey );
71212 if( pCur->pKey==0 ){
71213 rc = SQLITE_NOMEM;
71214 }else{
71215 memcpy(pCur->pKey, pX->pKey, pX->nKey);
71216 }
71217 }
71218 pCur->eState = CURSOR_REQUIRESEEK;
71219 pCur->nKey = pX->nKey;
71220 }
71221 }
71222 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
71223
71224end_insert:
71225 return rc;
71226}
71227
71228/*
71229** Delete the entry that the cursor is pointing to.
71230**
71231** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
71232** the cursor is left pointing at an arbitrary location after the delete.
71233** But if that bit is set, then the cursor is left in a state such that
71234** the next call to BtreeNext() or BtreePrev() moves it to the same row
71235** as it would have been on if the call to BtreeDelete() had been omitted.
71236**
71237** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
71238** associated with a single table entry and its indexes. Only one of those
71239** deletes is considered the "primary" delete. The primary delete occurs
71240** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
71241** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
71242** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
71243** but which might be used by alternative storage engines.
71244*/
71245SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
71246 Btree *p = pCur->pBtree;
71247 BtShared *pBt = p->pBt;
71248 int rc; /* Return code */
71249 MemPage *pPage; /* Page to delete cell from */
71250 unsigned char *pCell; /* Pointer to cell to delete */
71251 int iCellIdx; /* Index of cell to delete */
71252 int iCellDepth; /* Depth of node containing pCell */
71253 CellInfo info; /* Size of the cell being deleted */
71254 int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */
71255 u8 bPreserve = flags & BTREE_SAVEPOSITION; /* Keep cursor valid */
71256
71257 assert( cursorOwnsBtShared(pCur) );
71258 assert( pBt->inTransaction==TRANS_WRITE );
71259 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
71260 assert( pCur->curFlags & BTCF_WriteFlag );
71261 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
71262 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
71263 assert( pCur->ix<pCur->pPage->nCell );
71264 assert( pCur->eState==CURSOR_VALID );
71265 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
71266
71267 iCellDepth = pCur->iPage;
71268 iCellIdx = pCur->ix;
71269 pPage = pCur->pPage;
71270 pCell = findCell(pPage, iCellIdx);
71271
71272 /* If the bPreserve flag is set to true, then the cursor position must
71273 ** be preserved following this delete operation. If the current delete
71274 ** will cause a b-tree rebalance, then this is done by saving the cursor
71275 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
71276 ** returning.
71277 **
71278 ** Or, if the current delete will not cause a rebalance, then the cursor
71279 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
71280 ** before or after the deleted entry. In this case set bSkipnext to true. */
71281 if( bPreserve ){
71282 if( !pPage->leaf
71283 || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
71284 ){
71285 /* A b-tree rebalance will be required after deleting this entry.
71286 ** Save the cursor key. */
71287 rc = saveCursorKey(pCur);
71288 if( rc ) return rc;
71289 }else{
71290 bSkipnext = 1;
71291 }
71292 }
71293
71294 /* If the page containing the entry to delete is not a leaf page, move
71295 ** the cursor to the largest entry in the tree that is smaller than
71296 ** the entry being deleted. This cell will replace the cell being deleted
71297 ** from the internal node. The 'previous' entry is used for this instead
71298 ** of the 'next' entry, as the previous entry is always a part of the
71299 ** sub-tree headed by the child page of the cell being deleted. This makes
71300 ** balancing the tree following the delete operation easier. */
71301 if( !pPage->leaf ){
71302 rc = sqlite3BtreePrevious(pCur, 0);
71303 assert( rc!=SQLITE_DONE );
71304 if( rc ) return rc;
71305 }
71306
71307 /* Save the positions of any other cursors open on this table before
71308 ** making any modifications. */
71309 if( pCur->curFlags & BTCF_Multiple ){
71310 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
71311 if( rc ) return rc;
71312 }
71313
71314 /* If this is a delete operation to remove a row from a table b-tree,
71315 ** invalidate any incrblob cursors open on the row being deleted. */
71316 if( pCur->pKeyInfo==0 ){
71317 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
71318 }
71319
71320 /* Make the page containing the entry to be deleted writable. Then free any
71321 ** overflow pages associated with the entry and finally remove the cell
71322 ** itself from within the page. */
71323 rc = sqlite3PagerWrite(pPage->pDbPage);
71324 if( rc ) return rc;
71325 rc = clearCell(pPage, pCell, &info);
71326 dropCell(pPage, iCellIdx, info.nSize, &rc);
71327 if( rc ) return rc;
71328
71329 /* If the cell deleted was not located on a leaf page, then the cursor
71330 ** is currently pointing to the largest entry in the sub-tree headed
71331 ** by the child-page of the cell that was just deleted from an internal
71332 ** node. The cell from the leaf node needs to be moved to the internal
71333 ** node to replace the deleted cell. */
71334 if( !pPage->leaf ){
71335 MemPage *pLeaf = pCur->pPage;
71336 int nCell;
71337 Pgno n;
71338 unsigned char *pTmp;
71339
71340 if( iCellDepth<pCur->iPage-1 ){
71341 n = pCur->apPage[iCellDepth+1]->pgno;
71342 }else{
71343 n = pCur->pPage->pgno;
71344 }
71345 pCell = findCell(pLeaf, pLeaf->nCell-1);
71346 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
71347 nCell = pLeaf->xCellSize(pLeaf, pCell);
71348 assert( MX_CELL_SIZE(pBt) >= nCell );
71349 pTmp = pBt->pTmpSpace;
71350 assert( pTmp!=0 );
71351 rc = sqlite3PagerWrite(pLeaf->pDbPage);
71352 if( rc==SQLITE_OK ){
71353 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
71354 }
71355 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
71356 if( rc ) return rc;
71357 }
71358
71359 /* Balance the tree. If the entry deleted was located on a leaf page,
71360 ** then the cursor still points to that page. In this case the first
71361 ** call to balance() repairs the tree, and the if(...) condition is
71362 ** never true.
71363 **
71364 ** Otherwise, if the entry deleted was on an internal node page, then
71365 ** pCur is pointing to the leaf page from which a cell was removed to
71366 ** replace the cell deleted from the internal node. This is slightly
71367 ** tricky as the leaf node may be underfull, and the internal node may
71368 ** be either under or overfull. In this case run the balancing algorithm
71369 ** on the leaf node first. If the balance proceeds far enough up the
71370 ** tree that we can be sure that any problem in the internal node has
71371 ** been corrected, so be it. Otherwise, after balancing the leaf node,
71372 ** walk the cursor up the tree to the internal node and balance it as
71373 ** well. */
71374 rc = balance(pCur);
71375 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
71376 releasePageNotNull(pCur->pPage);
71377 pCur->iPage--;
71378 while( pCur->iPage>iCellDepth ){
71379 releasePage(pCur->apPage[pCur->iPage--]);
71380 }
71381 pCur->pPage = pCur->apPage[pCur->iPage];
71382 rc = balance(pCur);
71383 }
71384
71385 if( rc==SQLITE_OK ){
71386 if( bSkipnext ){
71387 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
71388 assert( pPage==pCur->pPage || CORRUPT_DB );
71389 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
71390 pCur->eState = CURSOR_SKIPNEXT;
71391 if( iCellIdx>=pPage->nCell ){
71392 pCur->skipNext = -1;
71393 pCur->ix = pPage->nCell-1;
71394 }else{
71395 pCur->skipNext = 1;
71396 }
71397 }else{
71398 rc = moveToRoot(pCur);
71399 if( bPreserve ){
71400 btreeReleaseAllCursorPages(pCur);
71401 pCur->eState = CURSOR_REQUIRESEEK;
71402 }
71403 if( rc==SQLITE_EMPTY ) rc = SQLITE_OK;
71404 }
71405 }
71406 return rc;
71407}
71408
71409/*
71410** Create a new BTree table. Write into *piTable the page
71411** number for the root page of the new table.
71412**
71413** The type of type is determined by the flags parameter. Only the
71414** following values of flags are currently in use. Other values for
71415** flags might not work:
71416**
71417** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
71418** BTREE_ZERODATA Used for SQL indices
71419*/
71420static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
71421 BtShared *pBt = p->pBt;
71422 MemPage *pRoot;
71423 Pgno pgnoRoot;
71424 int rc;
71425 int ptfFlags; /* Page-type flage for the root page of new table */
71426
71427 assert( sqlite3BtreeHoldsMutex(p) );
71428 assert( pBt->inTransaction==TRANS_WRITE );
71429 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
71430
71431#ifdef SQLITE_OMIT_AUTOVACUUM
71432 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
71433 if( rc ){
71434 return rc;
71435 }
71436#else
71437 if( pBt->autoVacuum ){
71438 Pgno pgnoMove; /* Move a page here to make room for the root-page */
71439 MemPage *pPageMove; /* The page to move to. */
71440
71441 /* Creating a new table may probably require moving an existing database
71442 ** to make room for the new tables root page. In case this page turns
71443 ** out to be an overflow page, delete all overflow page-map caches
71444 ** held by open cursors.
71445 */
71446 invalidateAllOverflowCache(pBt);
71447
71448 /* Read the value of meta[3] from the database to determine where the
71449 ** root page of the new table should go. meta[3] is the largest root-page
71450 ** created so far, so the new root-page is (meta[3]+1).
71451 */
71452 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
71453 pgnoRoot++;
71454
71455 /* The new root-page may not be allocated on a pointer-map page, or the
71456 ** PENDING_BYTE page.
71457 */
71458 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
71459 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
71460 pgnoRoot++;
71461 }
71462 assert( pgnoRoot>=3 || CORRUPT_DB );
71463 testcase( pgnoRoot<3 );
71464
71465 /* Allocate a page. The page that currently resides at pgnoRoot will
71466 ** be moved to the allocated page (unless the allocated page happens
71467 ** to reside at pgnoRoot).
71468 */
71469 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
71470 if( rc!=SQLITE_OK ){
71471 return rc;
71472 }
71473
71474 if( pgnoMove!=pgnoRoot ){
71475 /* pgnoRoot is the page that will be used for the root-page of
71476 ** the new table (assuming an error did not occur). But we were
71477 ** allocated pgnoMove. If required (i.e. if it was not allocated
71478 ** by extending the file), the current page at position pgnoMove
71479 ** is already journaled.
71480 */
71481 u8 eType = 0;
71482 Pgno iPtrPage = 0;
71483
71484 /* Save the positions of any open cursors. This is required in
71485 ** case they are holding a reference to an xFetch reference
71486 ** corresponding to page pgnoRoot. */
71487 rc = saveAllCursors(pBt, 0, 0);
71488 releasePage(pPageMove);
71489 if( rc!=SQLITE_OK ){
71490 return rc;
71491 }
71492
71493 /* Move the page currently at pgnoRoot to pgnoMove. */
71494 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
71495 if( rc!=SQLITE_OK ){
71496 return rc;
71497 }
71498 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
71499 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
71500 rc = SQLITE_CORRUPT_BKPT;
71501 }
71502 if( rc!=SQLITE_OK ){
71503 releasePage(pRoot);
71504 return rc;
71505 }
71506 assert( eType!=PTRMAP_ROOTPAGE );
71507 assert( eType!=PTRMAP_FREEPAGE );
71508 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
71509 releasePage(pRoot);
71510
71511 /* Obtain the page at pgnoRoot */
71512 if( rc!=SQLITE_OK ){
71513 return rc;
71514 }
71515 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
71516 if( rc!=SQLITE_OK ){
71517 return rc;
71518 }
71519 rc = sqlite3PagerWrite(pRoot->pDbPage);
71520 if( rc!=SQLITE_OK ){
71521 releasePage(pRoot);
71522 return rc;
71523 }
71524 }else{
71525 pRoot = pPageMove;
71526 }
71527
71528 /* Update the pointer-map and meta-data with the new root-page number. */
71529 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
71530 if( rc ){
71531 releasePage(pRoot);
71532 return rc;
71533 }
71534
71535 /* When the new root page was allocated, page 1 was made writable in
71536 ** order either to increase the database filesize, or to decrement the
71537 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
71538 */
71539 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
71540 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
71541 if( NEVER(rc) ){
71542 releasePage(pRoot);
71543 return rc;
71544 }
71545
71546 }else{
71547 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
71548 if( rc ) return rc;
71549 }
71550#endif
71551 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
71552 if( createTabFlags & BTREE_INTKEY ){
71553 ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
71554 }else{
71555 ptfFlags = PTF_ZERODATA | PTF_LEAF;
71556 }
71557 zeroPage(pRoot, ptfFlags);
71558 sqlite3PagerUnref(pRoot->pDbPage);
71559 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
71560 *piTable = (int)pgnoRoot;
71561 return SQLITE_OK;
71562}
71563SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
71564 int rc;
71565 sqlite3BtreeEnter(p);
71566 rc = btreeCreateTable(p, piTable, flags);
71567 sqlite3BtreeLeave(p);
71568 return rc;
71569}
71570
71571/*
71572** Erase the given database page and all its children. Return
71573** the page to the freelist.
71574*/
71575static int clearDatabasePage(
71576 BtShared *pBt, /* The BTree that contains the table */
71577 Pgno pgno, /* Page number to clear */
71578 int freePageFlag, /* Deallocate page if true */
71579 int *pnChange /* Add number of Cells freed to this counter */
71580){
71581 MemPage *pPage;
71582 int rc;
71583 unsigned char *pCell;
71584 int i;
71585 int hdr;
71586 CellInfo info;
71587
71588 assert( sqlite3_mutex_held(pBt->mutex) );
71589 if( pgno>btreePagecount(pBt) ){
71590 return SQLITE_CORRUPT_BKPT;
71591 }
71592 rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
71593 if( rc ) return rc;
71594 if( pPage->bBusy ){
71595 rc = SQLITE_CORRUPT_BKPT;
71596 goto cleardatabasepage_out;
71597 }
71598 pPage->bBusy = 1;
71599 hdr = pPage->hdrOffset;
71600 for(i=0; i<pPage->nCell; i++){
71601 pCell = findCell(pPage, i);
71602 if( !pPage->leaf ){
71603 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
71604 if( rc ) goto cleardatabasepage_out;
71605 }
71606 rc = clearCell(pPage, pCell, &info);
71607 if( rc ) goto cleardatabasepage_out;
71608 }
71609 if( !pPage->leaf ){
71610 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
71611 if( rc ) goto cleardatabasepage_out;
71612 }else if( pnChange ){
71613 assert( pPage->intKey || CORRUPT_DB );
71614 testcase( !pPage->intKey );
71615 *pnChange += pPage->nCell;
71616 }
71617 if( freePageFlag ){
71618 freePage(pPage, &rc);
71619 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
71620 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
71621 }
71622
71623cleardatabasepage_out:
71624 pPage->bBusy = 0;
71625 releasePage(pPage);
71626 return rc;
71627}
71628
71629/*
71630** Delete all information from a single table in the database. iTable is
71631** the page number of the root of the table. After this routine returns,
71632** the root page is empty, but still exists.
71633**
71634** This routine will fail with SQLITE_LOCKED if there are any open
71635** read cursors on the table. Open write cursors are moved to the
71636** root of the table.
71637**
71638** If pnChange is not NULL, then table iTable must be an intkey table. The
71639** integer value pointed to by pnChange is incremented by the number of
71640** entries in the table.
71641*/
71642SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
71643 int rc;
71644 BtShared *pBt = p->pBt;
71645 sqlite3BtreeEnter(p);
71646 assert( p->inTrans==TRANS_WRITE );
71647
71648 rc = saveAllCursors(pBt, (Pgno)iTable, 0);
71649
71650 if( SQLITE_OK==rc ){
71651 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
71652 ** is the root of a table b-tree - if it is not, the following call is
71653 ** a no-op). */
71654 invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
71655 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
71656 }
71657 sqlite3BtreeLeave(p);
71658 return rc;
71659}
71660
71661/*
71662** Delete all information from the single table that pCur is open on.
71663**
71664** This routine only work for pCur on an ephemeral table.
71665*/
71666SQLITE_PRIVATE int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
71667 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
71668}
71669
71670/*
71671** Erase all information in a table and add the root of the table to
71672** the freelist. Except, the root of the principle table (the one on
71673** page 1) is never added to the freelist.
71674**
71675** This routine will fail with SQLITE_LOCKED if there are any open
71676** cursors on the table.
71677**
71678** If AUTOVACUUM is enabled and the page at iTable is not the last
71679** root page in the database file, then the last root page
71680** in the database file is moved into the slot formerly occupied by
71681** iTable and that last slot formerly occupied by the last root page
71682** is added to the freelist instead of iTable. In this say, all
71683** root pages are kept at the beginning of the database file, which
71684** is necessary for AUTOVACUUM to work right. *piMoved is set to the
71685** page number that used to be the last root page in the file before
71686** the move. If no page gets moved, *piMoved is set to 0.
71687** The last root page is recorded in meta[3] and the value of
71688** meta[3] is updated by this procedure.
71689*/
71690static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
71691 int rc;
71692 MemPage *pPage = 0;
71693 BtShared *pBt = p->pBt;
71694
71695 assert( sqlite3BtreeHoldsMutex(p) );
71696 assert( p->inTrans==TRANS_WRITE );
71697 assert( iTable>=2 );
71698
71699 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
71700 if( rc ) return rc;
71701 rc = sqlite3BtreeClearTable(p, iTable, 0);
71702 if( rc ){
71703 releasePage(pPage);
71704 return rc;
71705 }
71706
71707 *piMoved = 0;
71708
71709#ifdef SQLITE_OMIT_AUTOVACUUM
71710 freePage(pPage, &rc);
71711 releasePage(pPage);
71712#else
71713 if( pBt->autoVacuum ){
71714 Pgno maxRootPgno;
71715 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
71716
71717 if( iTable==maxRootPgno ){
71718 /* If the table being dropped is the table with the largest root-page
71719 ** number in the database, put the root page on the free list.
71720 */
71721 freePage(pPage, &rc);
71722 releasePage(pPage);
71723 if( rc!=SQLITE_OK ){
71724 return rc;
71725 }
71726 }else{
71727 /* The table being dropped does not have the largest root-page
71728 ** number in the database. So move the page that does into the
71729 ** gap left by the deleted root-page.
71730 */
71731 MemPage *pMove;
71732 releasePage(pPage);
71733 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
71734 if( rc!=SQLITE_OK ){
71735 return rc;
71736 }
71737 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
71738 releasePage(pMove);
71739 if( rc!=SQLITE_OK ){
71740 return rc;
71741 }
71742 pMove = 0;
71743 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
71744 freePage(pMove, &rc);
71745 releasePage(pMove);
71746 if( rc!=SQLITE_OK ){
71747 return rc;
71748 }
71749 *piMoved = maxRootPgno;
71750 }
71751
71752 /* Set the new 'max-root-page' value in the database header. This
71753 ** is the old value less one, less one more if that happens to
71754 ** be a root-page number, less one again if that is the
71755 ** PENDING_BYTE_PAGE.
71756 */
71757 maxRootPgno--;
71758 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
71759 || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
71760 maxRootPgno--;
71761 }
71762 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
71763
71764 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
71765 }else{
71766 freePage(pPage, &rc);
71767 releasePage(pPage);
71768 }
71769#endif
71770 return rc;
71771}
71772SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
71773 int rc;
71774 sqlite3BtreeEnter(p);
71775 rc = btreeDropTable(p, iTable, piMoved);
71776 sqlite3BtreeLeave(p);
71777 return rc;
71778}
71779
71780
71781/*
71782** This function may only be called if the b-tree connection already
71783** has a read or write transaction open on the database.
71784**
71785** Read the meta-information out of a database file. Meta[0]
71786** is the number of free pages currently in the database. Meta[1]
71787** through meta[15] are available for use by higher layers. Meta[0]
71788** is read-only, the others are read/write.
71789**
71790** The schema layer numbers meta values differently. At the schema
71791** layer (and the SetCookie and ReadCookie opcodes) the number of
71792** free pages is not visible. So Cookie[0] is the same as Meta[1].
71793**
71794** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
71795** of reading the value out of the header, it instead loads the "DataVersion"
71796** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
71797** database file. It is a number computed by the pager. But its access
71798** pattern is the same as header meta values, and so it is convenient to
71799** read it from this routine.
71800*/
71801SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
71802 BtShared *pBt = p->pBt;
71803
71804 sqlite3BtreeEnter(p);
71805 assert( p->inTrans>TRANS_NONE );
71806 assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
71807 assert( pBt->pPage1 );
71808 assert( idx>=0 && idx<=15 );
71809
71810 if( idx==BTREE_DATA_VERSION ){
71811 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion;
71812 }else{
71813 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
71814 }
71815
71816 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
71817 ** database, mark the database as read-only. */
71818#ifdef SQLITE_OMIT_AUTOVACUUM
71819 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
71820 pBt->btsFlags |= BTS_READ_ONLY;
71821 }
71822#endif
71823
71824 sqlite3BtreeLeave(p);
71825}
71826
71827/*
71828** Write meta-information back into the database. Meta[0] is
71829** read-only and may not be written.
71830*/
71831SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
71832 BtShared *pBt = p->pBt;
71833 unsigned char *pP1;
71834 int rc;
71835 assert( idx>=1 && idx<=15 );
71836 sqlite3BtreeEnter(p);
71837 assert( p->inTrans==TRANS_WRITE );
71838 assert( pBt->pPage1!=0 );
71839 pP1 = pBt->pPage1->aData;
71840 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
71841 if( rc==SQLITE_OK ){
71842 put4byte(&pP1[36 + idx*4], iMeta);
71843#ifndef SQLITE_OMIT_AUTOVACUUM
71844 if( idx==BTREE_INCR_VACUUM ){
71845 assert( pBt->autoVacuum || iMeta==0 );
71846 assert( iMeta==0 || iMeta==1 );
71847 pBt->incrVacuum = (u8)iMeta;
71848 }
71849#endif
71850 }
71851 sqlite3BtreeLeave(p);
71852 return rc;
71853}
71854
71855#ifndef SQLITE_OMIT_BTREECOUNT
71856/*
71857** The first argument, pCur, is a cursor opened on some b-tree. Count the
71858** number of entries in the b-tree and write the result to *pnEntry.
71859**
71860** SQLITE_OK is returned if the operation is successfully executed.
71861** Otherwise, if an error is encountered (i.e. an IO error or database
71862** corruption) an SQLite error code is returned.
71863*/
71864SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
71865 i64 nEntry = 0; /* Value to return in *pnEntry */
71866 int rc; /* Return code */
71867
71868 rc = moveToRoot(pCur);
71869 if( rc==SQLITE_EMPTY ){
71870 *pnEntry = 0;
71871 return SQLITE_OK;
71872 }
71873
71874 /* Unless an error occurs, the following loop runs one iteration for each
71875 ** page in the B-Tree structure (not including overflow pages).
71876 */
71877 while( rc==SQLITE_OK ){
71878 int iIdx; /* Index of child node in parent */
71879 MemPage *pPage; /* Current page of the b-tree */
71880
71881 /* If this is a leaf page or the tree is not an int-key tree, then
71882 ** this page contains countable entries. Increment the entry counter
71883 ** accordingly.
71884 */
71885 pPage = pCur->pPage;
71886 if( pPage->leaf || !pPage->intKey ){
71887 nEntry += pPage->nCell;
71888 }
71889
71890 /* pPage is a leaf node. This loop navigates the cursor so that it
71891 ** points to the first interior cell that it points to the parent of
71892 ** the next page in the tree that has not yet been visited. The
71893 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
71894 ** of the page, or to the number of cells in the page if the next page
71895 ** to visit is the right-child of its parent.
71896 **
71897 ** If all pages in the tree have been visited, return SQLITE_OK to the
71898 ** caller.
71899 */
71900 if( pPage->leaf ){
71901 do {
71902 if( pCur->iPage==0 ){
71903 /* All pages of the b-tree have been visited. Return successfully. */
71904 *pnEntry = nEntry;
71905 return moveToRoot(pCur);
71906 }
71907 moveToParent(pCur);
71908 }while ( pCur->ix>=pCur->pPage->nCell );
71909
71910 pCur->ix++;
71911 pPage = pCur->pPage;
71912 }
71913
71914 /* Descend to the child node of the cell that the cursor currently
71915 ** points at. This is the right-child if (iIdx==pPage->nCell).
71916 */
71917 iIdx = pCur->ix;
71918 if( iIdx==pPage->nCell ){
71919 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
71920 }else{
71921 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
71922 }
71923 }
71924
71925 /* An error has occurred. Return an error code. */
71926 return rc;
71927}
71928#endif
71929
71930/*
71931** Return the pager associated with a BTree. This routine is used for
71932** testing and debugging only.
71933*/
71934SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
71935 return p->pBt->pPager;
71936}
71937
71938#ifndef SQLITE_OMIT_INTEGRITY_CHECK
71939/*
71940** Append a message to the error message string.
71941*/
71942static void checkAppendMsg(
71943 IntegrityCk *pCheck,
71944 const char *zFormat,
71945 ...
71946){
71947 va_list ap;
71948 if( !pCheck->mxErr ) return;
71949 pCheck->mxErr--;
71950 pCheck->nErr++;
71951 va_start(ap, zFormat);
71952 if( pCheck->errMsg.nChar ){
71953 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
71954 }
71955 if( pCheck->zPfx ){
71956 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
71957 }
71958 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
71959 va_end(ap);
71960 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
71961 pCheck->mallocFailed = 1;
71962 }
71963}
71964#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
71965
71966#ifndef SQLITE_OMIT_INTEGRITY_CHECK
71967
71968/*
71969** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
71970** corresponds to page iPg is already set.
71971*/
71972static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
71973 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
71974 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
71975}
71976
71977/*
71978** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
71979*/
71980static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
71981 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
71982 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
71983}
71984
71985
71986/*
71987** Add 1 to the reference count for page iPage. If this is the second
71988** reference to the page, add an error message to pCheck->zErrMsg.
71989** Return 1 if there are 2 or more references to the page and 0 if
71990** if this is the first reference to the page.
71991**
71992** Also check that the page number is in bounds.
71993*/
71994static int checkRef(IntegrityCk *pCheck, Pgno iPage){
71995 if( iPage>pCheck->nPage || iPage==0 ){
71996 checkAppendMsg(pCheck, "invalid page number %d", iPage);
71997 return 1;
71998 }
71999 if( getPageReferenced(pCheck, iPage) ){
72000 checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
72001 return 1;
72002 }
72003 setPageReferenced(pCheck, iPage);
72004 return 0;
72005}
72006
72007#ifndef SQLITE_OMIT_AUTOVACUUM
72008/*
72009** Check that the entry in the pointer-map for page iChild maps to
72010** page iParent, pointer type ptrType. If not, append an error message
72011** to pCheck.
72012*/
72013static void checkPtrmap(
72014 IntegrityCk *pCheck, /* Integrity check context */
72015 Pgno iChild, /* Child page number */
72016 u8 eType, /* Expected pointer map type */
72017 Pgno iParent /* Expected pointer map parent page number */
72018){
72019 int rc;
72020 u8 ePtrmapType;
72021 Pgno iPtrmapParent;
72022
72023 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
72024 if( rc!=SQLITE_OK ){
72025 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
72026 checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild);
72027 return;
72028 }
72029
72030 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
72031 checkAppendMsg(pCheck,
72032 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
72033 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
72034 }
72035}
72036#endif
72037
72038/*
72039** Check the integrity of the freelist or of an overflow page list.
72040** Verify that the number of pages on the list is N.
72041*/
72042static void checkList(
72043 IntegrityCk *pCheck, /* Integrity checking context */
72044 int isFreeList, /* True for a freelist. False for overflow page list */
72045 int iPage, /* Page number for first page in the list */
72046 int N /* Expected number of pages in the list */
72047){
72048 int i;
72049 int expected = N;
72050 int nErrAtStart = pCheck->nErr;
72051 while( iPage!=0 && pCheck->mxErr ){
72052 DbPage *pOvflPage;
72053 unsigned char *pOvflData;
72054 if( checkRef(pCheck, iPage) ) break;
72055 N--;
72056 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
72057 checkAppendMsg(pCheck, "failed to get page %d", iPage);
72058 break;
72059 }
72060 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
72061 if( isFreeList ){
72062 int n = get4byte(&pOvflData[4]);
72063#ifndef SQLITE_OMIT_AUTOVACUUM
72064 if( pCheck->pBt->autoVacuum ){
72065 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
72066 }
72067#endif
72068 if( n>(int)pCheck->pBt->usableSize/4-2 ){
72069 checkAppendMsg(pCheck,
72070 "freelist leaf count too big on page %d", iPage);
72071 N--;
72072 }else{
72073 for(i=0; i<n; i++){
72074 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
72075#ifndef SQLITE_OMIT_AUTOVACUUM
72076 if( pCheck->pBt->autoVacuum ){
72077 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0);
72078 }
72079#endif
72080 checkRef(pCheck, iFreePage);
72081 }
72082 N -= n;
72083 }
72084 }
72085#ifndef SQLITE_OMIT_AUTOVACUUM
72086 else{
72087 /* If this database supports auto-vacuum and iPage is not the last
72088 ** page in this overflow list, check that the pointer-map entry for
72089 ** the following page matches iPage.
72090 */
72091 if( pCheck->pBt->autoVacuum && N>0 ){
72092 i = get4byte(pOvflData);
72093 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
72094 }
72095 }
72096#endif
72097 iPage = get4byte(pOvflData);
72098 sqlite3PagerUnref(pOvflPage);
72099 }
72100 if( N && nErrAtStart==pCheck->nErr ){
72101 checkAppendMsg(pCheck,
72102 "%s is %d but should be %d",
72103 isFreeList ? "size" : "overflow list length",
72104 expected-N, expected);
72105 }
72106}
72107#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
72108
72109/*
72110** An implementation of a min-heap.
72111**
72112** aHeap[0] is the number of elements on the heap. aHeap[1] is the
72113** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
72114** and aHeap[N*2+1].
72115**
72116** The heap property is this: Every node is less than or equal to both
72117** of its daughter nodes. A consequence of the heap property is that the
72118** root node aHeap[1] is always the minimum value currently in the heap.
72119**
72120** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
72121** the heap, preserving the heap property. The btreeHeapPull() routine
72122** removes the root element from the heap (the minimum value in the heap)
72123** and then moves other nodes around as necessary to preserve the heap
72124** property.
72125**
72126** This heap is used for cell overlap and coverage testing. Each u32
72127** entry represents the span of a cell or freeblock on a btree page.
72128** The upper 16 bits are the index of the first byte of a range and the
72129** lower 16 bits are the index of the last byte of that range.
72130*/
72131static void btreeHeapInsert(u32 *aHeap, u32 x){
72132 u32 j, i = ++aHeap[0];
72133 aHeap[i] = x;
72134 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
72135 x = aHeap[j];
72136 aHeap[j] = aHeap[i];
72137 aHeap[i] = x;
72138 i = j;
72139 }
72140}
72141static int btreeHeapPull(u32 *aHeap, u32 *pOut){
72142 u32 j, i, x;
72143 if( (x = aHeap[0])==0 ) return 0;
72144 *pOut = aHeap[1];
72145 aHeap[1] = aHeap[x];
72146 aHeap[x] = 0xffffffff;
72147 aHeap[0]--;
72148 i = 1;
72149 while( (j = i*2)<=aHeap[0] ){
72150 if( aHeap[j]>aHeap[j+1] ) j++;
72151 if( aHeap[i]<aHeap[j] ) break;
72152 x = aHeap[i];
72153 aHeap[i] = aHeap[j];
72154 aHeap[j] = x;
72155 i = j;
72156 }
72157 return 1;
72158}
72159
72160#ifndef SQLITE_OMIT_INTEGRITY_CHECK
72161/*
72162** Do various sanity checks on a single page of a tree. Return
72163** the tree depth. Root pages return 0. Parents of root pages
72164** return 1, and so forth.
72165**
72166** These checks are done:
72167**
72168** 1. Make sure that cells and freeblocks do not overlap
72169** but combine to completely cover the page.
72170** 2. Make sure integer cell keys are in order.
72171** 3. Check the integrity of overflow pages.
72172** 4. Recursively call checkTreePage on all children.
72173** 5. Verify that the depth of all children is the same.
72174*/
72175static int checkTreePage(
72176 IntegrityCk *pCheck, /* Context for the sanity check */
72177 int iPage, /* Page number of the page to check */
72178 i64 *piMinKey, /* Write minimum integer primary key here */
72179 i64 maxKey /* Error if integer primary key greater than this */
72180){
72181 MemPage *pPage = 0; /* The page being analyzed */
72182 int i; /* Loop counter */
72183 int rc; /* Result code from subroutine call */
72184 int depth = -1, d2; /* Depth of a subtree */
72185 int pgno; /* Page number */
72186 int nFrag; /* Number of fragmented bytes on the page */
72187 int hdr; /* Offset to the page header */
72188 int cellStart; /* Offset to the start of the cell pointer array */
72189 int nCell; /* Number of cells */
72190 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
72191 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
72192 ** False if IPK must be strictly less than maxKey */
72193 u8 *data; /* Page content */
72194 u8 *pCell; /* Cell content */
72195 u8 *pCellIdx; /* Next element of the cell pointer array */
72196 BtShared *pBt; /* The BtShared object that owns pPage */
72197 u32 pc; /* Address of a cell */
72198 u32 usableSize; /* Usable size of the page */
72199 u32 contentOffset; /* Offset to the start of the cell content area */
72200 u32 *heap = 0; /* Min-heap used for checking cell coverage */
72201 u32 x, prev = 0; /* Next and previous entry on the min-heap */
72202 const char *saved_zPfx = pCheck->zPfx;
72203 int saved_v1 = pCheck->v1;
72204 int saved_v2 = pCheck->v2;
72205 u8 savedIsInit = 0;
72206
72207 /* Check that the page exists
72208 */
72209 pBt = pCheck->pBt;
72210 usableSize = pBt->usableSize;
72211 if( iPage==0 ) return 0;
72212 if( checkRef(pCheck, iPage) ) return 0;
72213 pCheck->zPfx = "Page %d: ";
72214 pCheck->v1 = iPage;
72215 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
72216 checkAppendMsg(pCheck,
72217 "unable to get the page. error code=%d", rc);
72218 goto end_of_check;
72219 }
72220
72221 /* Clear MemPage.isInit to make sure the corruption detection code in
72222 ** btreeInitPage() is executed. */
72223 savedIsInit = pPage->isInit;
72224 pPage->isInit = 0;
72225 if( (rc = btreeInitPage(pPage))!=0 ){
72226 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
72227 checkAppendMsg(pCheck,
72228 "btreeInitPage() returns error code %d", rc);
72229 goto end_of_check;
72230 }
72231 data = pPage->aData;
72232 hdr = pPage->hdrOffset;
72233
72234 /* Set up for cell analysis */
72235 pCheck->zPfx = "On tree page %d cell %d: ";
72236 contentOffset = get2byteNotZero(&data[hdr+5]);
72237 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
72238
72239 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
72240 ** number of cells on the page. */
72241 nCell = get2byte(&data[hdr+3]);
72242 assert( pPage->nCell==nCell );
72243
72244 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
72245 ** immediately follows the b-tree page header. */
72246 cellStart = hdr + 12 - 4*pPage->leaf;
72247 assert( pPage->aCellIdx==&data[cellStart] );
72248 pCellIdx = &data[cellStart + 2*(nCell-1)];
72249
72250 if( !pPage->leaf ){
72251 /* Analyze the right-child page of internal pages */
72252 pgno = get4byte(&data[hdr+8]);
72253#ifndef SQLITE_OMIT_AUTOVACUUM
72254 if( pBt->autoVacuum ){
72255 pCheck->zPfx = "On page %d at right child: ";
72256 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
72257 }
72258#endif
72259 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
72260 keyCanBeEqual = 0;
72261 }else{
72262 /* For leaf pages, the coverage check will occur in the same loop
72263 ** as the other cell checks, so initialize the heap. */
72264 heap = pCheck->heap;
72265 heap[0] = 0;
72266 }
72267
72268 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
72269 ** integer offsets to the cell contents. */
72270 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
72271 CellInfo info;
72272
72273 /* Check cell size */
72274 pCheck->v2 = i;
72275 assert( pCellIdx==&data[cellStart + i*2] );
72276 pc = get2byteAligned(pCellIdx);
72277 pCellIdx -= 2;
72278 if( pc<contentOffset || pc>usableSize-4 ){
72279 checkAppendMsg(pCheck, "Offset %d out of range %d..%d",
72280 pc, contentOffset, usableSize-4);
72281 doCoverageCheck = 0;
72282 continue;
72283 }
72284 pCell = &data[pc];
72285 pPage->xParseCell(pPage, pCell, &info);
72286 if( pc+info.nSize>usableSize ){
72287 checkAppendMsg(pCheck, "Extends off end of page");
72288 doCoverageCheck = 0;
72289 continue;
72290 }
72291
72292 /* Check for integer primary key out of range */
72293 if( pPage->intKey ){
72294 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
72295 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
72296 }
72297 maxKey = info.nKey;
72298 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
72299 }
72300
72301 /* Check the content overflow list */
72302 if( info.nPayload>info.nLocal ){
72303 int nPage; /* Number of pages on the overflow chain */
72304 Pgno pgnoOvfl; /* First page of the overflow chain */
72305 assert( pc + info.nSize - 4 <= usableSize );
72306 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
72307 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
72308#ifndef SQLITE_OMIT_AUTOVACUUM
72309 if( pBt->autoVacuum ){
72310 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
72311 }
72312#endif
72313 checkList(pCheck, 0, pgnoOvfl, nPage);
72314 }
72315
72316 if( !pPage->leaf ){
72317 /* Check sanity of left child page for internal pages */
72318 pgno = get4byte(pCell);
72319#ifndef SQLITE_OMIT_AUTOVACUUM
72320 if( pBt->autoVacuum ){
72321 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
72322 }
72323#endif
72324 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
72325 keyCanBeEqual = 0;
72326 if( d2!=depth ){
72327 checkAppendMsg(pCheck, "Child page depth differs");
72328 depth = d2;
72329 }
72330 }else{
72331 /* Populate the coverage-checking heap for leaf pages */
72332 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
72333 }
72334 }
72335 *piMinKey = maxKey;
72336
72337 /* Check for complete coverage of the page
72338 */
72339 pCheck->zPfx = 0;
72340 if( doCoverageCheck && pCheck->mxErr>0 ){
72341 /* For leaf pages, the min-heap has already been initialized and the
72342 ** cells have already been inserted. But for internal pages, that has
72343 ** not yet been done, so do it now */
72344 if( !pPage->leaf ){
72345 heap = pCheck->heap;
72346 heap[0] = 0;
72347 for(i=nCell-1; i>=0; i--){
72348 u32 size;
72349 pc = get2byteAligned(&data[cellStart+i*2]);
72350 size = pPage->xCellSize(pPage, &data[pc]);
72351 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
72352 }
72353 }
72354 /* Add the freeblocks to the min-heap
72355 **
72356 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
72357 ** is the offset of the first freeblock, or zero if there are no
72358 ** freeblocks on the page.
72359 */
72360 i = get2byte(&data[hdr+1]);
72361 while( i>0 ){
72362 int size, j;
72363 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */
72364 size = get2byte(&data[i+2]);
72365 assert( (u32)(i+size)<=usableSize ); /* Enforced by btreeInitPage() */
72366 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
72367 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
72368 ** big-endian integer which is the offset in the b-tree page of the next
72369 ** freeblock in the chain, or zero if the freeblock is the last on the
72370 ** chain. */
72371 j = get2byte(&data[i]);
72372 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
72373 ** increasing offset. */
72374 assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */
72375 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */
72376 i = j;
72377 }
72378 /* Analyze the min-heap looking for overlap between cells and/or
72379 ** freeblocks, and counting the number of untracked bytes in nFrag.
72380 **
72381 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
72382 ** There is an implied first entry the covers the page header, the cell
72383 ** pointer index, and the gap between the cell pointer index and the start
72384 ** of cell content.
72385 **
72386 ** The loop below pulls entries from the min-heap in order and compares
72387 ** the start_address against the previous end_address. If there is an
72388 ** overlap, that means bytes are used multiple times. If there is a gap,
72389 ** that gap is added to the fragmentation count.
72390 */
72391 nFrag = 0;
72392 prev = contentOffset - 1; /* Implied first min-heap entry */
72393 while( btreeHeapPull(heap,&x) ){
72394 if( (prev&0xffff)>=(x>>16) ){
72395 checkAppendMsg(pCheck,
72396 "Multiple uses for byte %u of page %d", x>>16, iPage);
72397 break;
72398 }else{
72399 nFrag += (x>>16) - (prev&0xffff) - 1;
72400 prev = x;
72401 }
72402 }
72403 nFrag += usableSize - (prev&0xffff) - 1;
72404 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
72405 ** is stored in the fifth field of the b-tree page header.
72406 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
72407 ** number of fragmented free bytes within the cell content area.
72408 */
72409 if( heap[0]==0 && nFrag!=data[hdr+7] ){
72410 checkAppendMsg(pCheck,
72411 "Fragmentation of %d bytes reported as %d on page %d",
72412 nFrag, data[hdr+7], iPage);
72413 }
72414 }
72415
72416end_of_check:
72417 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
72418 releasePage(pPage);
72419 pCheck->zPfx = saved_zPfx;
72420 pCheck->v1 = saved_v1;
72421 pCheck->v2 = saved_v2;
72422 return depth+1;
72423}
72424#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
72425
72426#ifndef SQLITE_OMIT_INTEGRITY_CHECK
72427/*
72428** This routine does a complete check of the given BTree file. aRoot[] is
72429** an array of pages numbers were each page number is the root page of
72430** a table. nRoot is the number of entries in aRoot.
72431**
72432** A read-only or read-write transaction must be opened before calling
72433** this function.
72434**
72435** Write the number of error seen in *pnErr. Except for some memory
72436** allocation errors, an error message held in memory obtained from
72437** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
72438** returned. If a memory allocation error occurs, NULL is returned.
72439*/
72440SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
72441 Btree *p, /* The btree to be checked */
72442 int *aRoot, /* An array of root pages numbers for individual trees */
72443 int nRoot, /* Number of entries in aRoot[] */
72444 int mxErr, /* Stop reporting errors after this many */
72445 int *pnErr /* Write number of errors seen to this variable */
72446){
72447 Pgno i;
72448 IntegrityCk sCheck;
72449 BtShared *pBt = p->pBt;
72450 int savedDbFlags = pBt->db->flags;
72451 char zErr[100];
72452 VVA_ONLY( int nRef );
72453
72454 sqlite3BtreeEnter(p);
72455 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
72456 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
72457 assert( nRef>=0 );
72458 sCheck.pBt = pBt;
72459 sCheck.pPager = pBt->pPager;
72460 sCheck.nPage = btreePagecount(sCheck.pBt);
72461 sCheck.mxErr = mxErr;
72462 sCheck.nErr = 0;
72463 sCheck.mallocFailed = 0;
72464 sCheck.zPfx = 0;
72465 sCheck.v1 = 0;
72466 sCheck.v2 = 0;
72467 sCheck.aPgRef = 0;
72468 sCheck.heap = 0;
72469 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
72470 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
72471 if( sCheck.nPage==0 ){
72472 goto integrity_ck_cleanup;
72473 }
72474
72475 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
72476 if( !sCheck.aPgRef ){
72477 sCheck.mallocFailed = 1;
72478 goto integrity_ck_cleanup;
72479 }
72480 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
72481 if( sCheck.heap==0 ){
72482 sCheck.mallocFailed = 1;
72483 goto integrity_ck_cleanup;
72484 }
72485
72486 i = PENDING_BYTE_PAGE(pBt);
72487 if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
72488
72489 /* Check the integrity of the freelist
72490 */
72491 sCheck.zPfx = "Main freelist: ";
72492 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
72493 get4byte(&pBt->pPage1->aData[36]));
72494 sCheck.zPfx = 0;
72495
72496 /* Check all the tables.
72497 */
72498#ifndef SQLITE_OMIT_AUTOVACUUM
72499 if( pBt->autoVacuum ){
72500 int mx = 0;
72501 int mxInHdr;
72502 for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
72503 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
72504 if( mx!=mxInHdr ){
72505 checkAppendMsg(&sCheck,
72506 "max rootpage (%d) disagrees with header (%d)",
72507 mx, mxInHdr
72508 );
72509 }
72510 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
72511 checkAppendMsg(&sCheck,
72512 "incremental_vacuum enabled with a max rootpage of zero"
72513 );
72514 }
72515#endif
72516 testcase( pBt->db->flags & SQLITE_CellSizeCk );
72517 pBt->db->flags &= ~SQLITE_CellSizeCk;
72518 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
72519 i64 notUsed;
72520 if( aRoot[i]==0 ) continue;
72521#ifndef SQLITE_OMIT_AUTOVACUUM
72522 if( pBt->autoVacuum && aRoot[i]>1 ){
72523 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
72524 }
72525#endif
72526 checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
72527 }
72528 pBt->db->flags = savedDbFlags;
72529
72530 /* Make sure every page in the file is referenced
72531 */
72532 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
72533#ifdef SQLITE_OMIT_AUTOVACUUM
72534 if( getPageReferenced(&sCheck, i)==0 ){
72535 checkAppendMsg(&sCheck, "Page %d is never used", i);
72536 }
72537#else
72538 /* If the database supports auto-vacuum, make sure no tables contain
72539 ** references to pointer-map pages.
72540 */
72541 if( getPageReferenced(&sCheck, i)==0 &&
72542 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
72543 checkAppendMsg(&sCheck, "Page %d is never used", i);
72544 }
72545 if( getPageReferenced(&sCheck, i)!=0 &&
72546 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
72547 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
72548 }
72549#endif
72550 }
72551
72552 /* Clean up and report errors.
72553 */
72554integrity_ck_cleanup:
72555 sqlite3PageFree(sCheck.heap);
72556 sqlite3_free(sCheck.aPgRef);
72557 if( sCheck.mallocFailed ){
72558 sqlite3_str_reset(&sCheck.errMsg);
72559 sCheck.nErr++;
72560 }
72561 *pnErr = sCheck.nErr;
72562 if( sCheck.nErr==0 ) sqlite3_str_reset(&sCheck.errMsg);
72563 /* Make sure this analysis did not leave any unref() pages. */
72564 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
72565 sqlite3BtreeLeave(p);
72566 return sqlite3StrAccumFinish(&sCheck.errMsg);
72567}
72568#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
72569
72570/*
72571** Return the full pathname of the underlying database file. Return
72572** an empty string if the database is in-memory or a TEMP database.
72573**
72574** The pager filename is invariant as long as the pager is
72575** open so it is safe to access without the BtShared mutex.
72576*/
72577SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
72578 assert( p->pBt->pPager!=0 );
72579 return sqlite3PagerFilename(p->pBt->pPager, 1);
72580}
72581
72582/*
72583** Return the pathname of the journal file for this database. The return
72584** value of this routine is the same regardless of whether the journal file
72585** has been created or not.
72586**
72587** The pager journal filename is invariant as long as the pager is
72588** open so it is safe to access without the BtShared mutex.
72589*/
72590SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
72591 assert( p->pBt->pPager!=0 );
72592 return sqlite3PagerJournalname(p->pBt->pPager);
72593}
72594
72595/*
72596** Return non-zero if a transaction is active.
72597*/
72598SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
72599 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
72600 return (p && (p->inTrans==TRANS_WRITE));
72601}
72602
72603#ifndef SQLITE_OMIT_WAL
72604/*
72605** Run a checkpoint on the Btree passed as the first argument.
72606**
72607** Return SQLITE_LOCKED if this or any other connection has an open
72608** transaction on the shared-cache the argument Btree is connected to.
72609**
72610** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
72611*/
72612SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
72613 int rc = SQLITE_OK;
72614 if( p ){
72615 BtShared *pBt = p->pBt;
72616 sqlite3BtreeEnter(p);
72617 if( pBt->inTransaction!=TRANS_NONE ){
72618 rc = SQLITE_LOCKED;
72619 }else{
72620 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
72621 }
72622 sqlite3BtreeLeave(p);
72623 }
72624 return rc;
72625}
72626#endif
72627
72628/*
72629** Return non-zero if a read (or write) transaction is active.
72630*/
72631SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
72632 assert( p );
72633 assert( sqlite3_mutex_held(p->db->mutex) );
72634 return p->inTrans!=TRANS_NONE;
72635}
72636
72637SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
72638 assert( p );
72639 assert( sqlite3_mutex_held(p->db->mutex) );
72640 return p->nBackup!=0;
72641}
72642
72643/*
72644** This function returns a pointer to a blob of memory associated with
72645** a single shared-btree. The memory is used by client code for its own
72646** purposes (for example, to store a high-level schema associated with
72647** the shared-btree). The btree layer manages reference counting issues.
72648**
72649** The first time this is called on a shared-btree, nBytes bytes of memory
72650** are allocated, zeroed, and returned to the caller. For each subsequent
72651** call the nBytes parameter is ignored and a pointer to the same blob
72652** of memory returned.
72653**
72654** If the nBytes parameter is 0 and the blob of memory has not yet been
72655** allocated, a null pointer is returned. If the blob has already been
72656** allocated, it is returned as normal.
72657**
72658** Just before the shared-btree is closed, the function passed as the
72659** xFree argument when the memory allocation was made is invoked on the
72660** blob of allocated memory. The xFree function should not call sqlite3_free()
72661** on the memory, the btree layer does that.
72662*/
72663SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
72664 BtShared *pBt = p->pBt;
72665 sqlite3BtreeEnter(p);
72666 if( !pBt->pSchema && nBytes ){
72667 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
72668 pBt->xFreeSchema = xFree;
72669 }
72670 sqlite3BtreeLeave(p);
72671 return pBt->pSchema;
72672}
72673
72674/*
72675** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
72676** btree as the argument handle holds an exclusive lock on the
72677** sqlite_master table. Otherwise SQLITE_OK.
72678*/
72679SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
72680 int rc;
72681 assert( sqlite3_mutex_held(p->db->mutex) );
72682 sqlite3BtreeEnter(p);
72683 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
72684 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
72685 sqlite3BtreeLeave(p);
72686 return rc;
72687}
72688
72689
72690#ifndef SQLITE_OMIT_SHARED_CACHE
72691/*
72692** Obtain a lock on the table whose root page is iTab. The
72693** lock is a write lock if isWritelock is true or a read lock
72694** if it is false.
72695*/
72696SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
72697 int rc = SQLITE_OK;
72698 assert( p->inTrans!=TRANS_NONE );
72699 if( p->sharable ){
72700 u8 lockType = READ_LOCK + isWriteLock;
72701 assert( READ_LOCK+1==WRITE_LOCK );
72702 assert( isWriteLock==0 || isWriteLock==1 );
72703
72704 sqlite3BtreeEnter(p);
72705 rc = querySharedCacheTableLock(p, iTab, lockType);
72706 if( rc==SQLITE_OK ){
72707 rc = setSharedCacheTableLock(p, iTab, lockType);
72708 }
72709 sqlite3BtreeLeave(p);
72710 }
72711 return rc;
72712}
72713#endif
72714
72715#ifndef SQLITE_OMIT_INCRBLOB
72716/*
72717** Argument pCsr must be a cursor opened for writing on an
72718** INTKEY table currently pointing at a valid table entry.
72719** This function modifies the data stored as part of that entry.
72720**
72721** Only the data content may only be modified, it is not possible to
72722** change the length of the data stored. If this function is called with
72723** parameters that attempt to write past the end of the existing data,
72724** no modifications are made and SQLITE_CORRUPT is returned.
72725*/
72726SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
72727 int rc;
72728 assert( cursorOwnsBtShared(pCsr) );
72729 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
72730 assert( pCsr->curFlags & BTCF_Incrblob );
72731
72732 rc = restoreCursorPosition(pCsr);
72733 if( rc!=SQLITE_OK ){
72734 return rc;
72735 }
72736 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
72737 if( pCsr->eState!=CURSOR_VALID ){
72738 return SQLITE_ABORT;
72739 }
72740
72741 /* Save the positions of all other cursors open on this table. This is
72742 ** required in case any of them are holding references to an xFetch
72743 ** version of the b-tree page modified by the accessPayload call below.
72744 **
72745 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
72746 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
72747 ** saveAllCursors can only return SQLITE_OK.
72748 */
72749 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
72750 assert( rc==SQLITE_OK );
72751
72752 /* Check some assumptions:
72753 ** (a) the cursor is open for writing,
72754 ** (b) there is a read/write transaction open,
72755 ** (c) the connection holds a write-lock on the table (if required),
72756 ** (d) there are no conflicting read-locks, and
72757 ** (e) the cursor points at a valid row of an intKey table.
72758 */
72759 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
72760 return SQLITE_READONLY;
72761 }
72762 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
72763 && pCsr->pBt->inTransaction==TRANS_WRITE );
72764 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
72765 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
72766 assert( pCsr->pPage->intKey );
72767
72768 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
72769}
72770
72771/*
72772** Mark this cursor as an incremental blob cursor.
72773*/
72774SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
72775 pCur->curFlags |= BTCF_Incrblob;
72776 pCur->pBtree->hasIncrblobCur = 1;
72777}
72778#endif
72779
72780/*
72781** Set both the "read version" (single byte at byte offset 18) and
72782** "write version" (single byte at byte offset 19) fields in the database
72783** header to iVersion.
72784*/
72785SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
72786 BtShared *pBt = pBtree->pBt;
72787 int rc; /* Return code */
72788
72789 assert( iVersion==1 || iVersion==2 );
72790
72791 /* If setting the version fields to 1, do not automatically open the
72792 ** WAL connection, even if the version fields are currently set to 2.
72793 */
72794 pBt->btsFlags &= ~BTS_NO_WAL;
72795 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
72796
72797 rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
72798 if( rc==SQLITE_OK ){
72799 u8 *aData = pBt->pPage1->aData;
72800 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
72801 rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
72802 if( rc==SQLITE_OK ){
72803 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
72804 if( rc==SQLITE_OK ){
72805 aData[18] = (u8)iVersion;
72806 aData[19] = (u8)iVersion;
72807 }
72808 }
72809 }
72810 }
72811
72812 pBt->btsFlags &= ~BTS_NO_WAL;
72813 return rc;
72814}
72815
72816/*
72817** Return true if the cursor has a hint specified. This routine is
72818** only used from within assert() statements
72819*/
72820SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
72821 return (pCsr->hints & mask)!=0;
72822}
72823
72824/*
72825** Return true if the given Btree is read-only.
72826*/
72827SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
72828 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
72829}
72830
72831/*
72832** Return the size of the header added to each page by this module.
72833*/
72834SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
72835
72836#if !defined(SQLITE_OMIT_SHARED_CACHE)
72837/*
72838** Return true if the Btree passed as the only argument is sharable.
72839*/
72840SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
72841 return p->sharable;
72842}
72843
72844/*
72845** Return the number of connections to the BtShared object accessed by
72846** the Btree handle passed as the only argument. For private caches
72847** this is always 1. For shared caches it may be 1 or greater.
72848*/
72849SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree *p){
72850 testcase( p->sharable );
72851 return p->pBt->nRef;
72852}
72853#endif
72854
72855/************** End of btree.c ***********************************************/
72856/************** Begin file backup.c ******************************************/
72857/*
72858** 2009 January 28
72859**
72860** The author disclaims copyright to this source code. In place of
72861** a legal notice, here is a blessing:
72862**
72863** May you do good and not evil.
72864** May you find forgiveness for yourself and forgive others.
72865** May you share freely, never taking more than you give.
72866**
72867*************************************************************************
72868** This file contains the implementation of the sqlite3_backup_XXX()
72869** API functions and the related features.
72870*/
72871/* #include "sqliteInt.h" */
72872/* #include "btreeInt.h" */
72873
72874/*
72875** Structure allocated for each backup operation.
72876*/
72877struct sqlite3_backup {
72878 sqlite3* pDestDb; /* Destination database handle */
72879 Btree *pDest; /* Destination b-tree file */
72880 u32 iDestSchema; /* Original schema cookie in destination */
72881 int bDestLocked; /* True once a write-transaction is open on pDest */
72882
72883 Pgno iNext; /* Page number of the next source page to copy */
72884 sqlite3* pSrcDb; /* Source database handle */
72885 Btree *pSrc; /* Source b-tree file */
72886
72887 int rc; /* Backup process error code */
72888
72889 /* These two variables are set by every call to backup_step(). They are
72890 ** read by calls to backup_remaining() and backup_pagecount().
72891 */
72892 Pgno nRemaining; /* Number of pages left to copy */
72893 Pgno nPagecount; /* Total number of pages to copy */
72894
72895 int isAttached; /* True once backup has been registered with pager */
72896 sqlite3_backup *pNext; /* Next backup associated with source pager */
72897};
72898
72899/*
72900** THREAD SAFETY NOTES:
72901**
72902** Once it has been created using backup_init(), a single sqlite3_backup
72903** structure may be accessed via two groups of thread-safe entry points:
72904**
72905** * Via the sqlite3_backup_XXX() API function backup_step() and
72906** backup_finish(). Both these functions obtain the source database
72907** handle mutex and the mutex associated with the source BtShared
72908** structure, in that order.
72909**
72910** * Via the BackupUpdate() and BackupRestart() functions, which are
72911** invoked by the pager layer to report various state changes in
72912** the page cache associated with the source database. The mutex
72913** associated with the source database BtShared structure will always
72914** be held when either of these functions are invoked.
72915**
72916** The other sqlite3_backup_XXX() API functions, backup_remaining() and
72917** backup_pagecount() are not thread-safe functions. If they are called
72918** while some other thread is calling backup_step() or backup_finish(),
72919** the values returned may be invalid. There is no way for a call to
72920** BackupUpdate() or BackupRestart() to interfere with backup_remaining()
72921** or backup_pagecount().
72922**
72923** Depending on the SQLite configuration, the database handles and/or
72924** the Btree objects may have their own mutexes that require locking.
72925** Non-sharable Btrees (in-memory databases for example), do not have
72926** associated mutexes.
72927*/
72928
72929/*
72930** Return a pointer corresponding to database zDb (i.e. "main", "temp")
72931** in connection handle pDb. If such a database cannot be found, return
72932** a NULL pointer and write an error message to pErrorDb.
72933**
72934** If the "temp" database is requested, it may need to be opened by this
72935** function. If an error occurs while doing so, return 0 and write an
72936** error message to pErrorDb.
72937*/
72938static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
72939 int i = sqlite3FindDbName(pDb, zDb);
72940
72941 if( i==1 ){
72942 Parse sParse;
72943 int rc = 0;
72944 memset(&sParse, 0, sizeof(sParse));
72945 sParse.db = pDb;
72946 if( sqlite3OpenTempDatabase(&sParse) ){
72947 sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
72948 rc = SQLITE_ERROR;
72949 }
72950 sqlite3DbFree(pErrorDb, sParse.zErrMsg);
72951 sqlite3ParserReset(&sParse);
72952 if( rc ){
72953 return 0;
72954 }
72955 }
72956
72957 if( i<0 ){
72958 sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
72959 return 0;
72960 }
72961
72962 return pDb->aDb[i].pBt;
72963}
72964
72965/*
72966** Attempt to set the page size of the destination to match the page size
72967** of the source.
72968*/
72969static int setDestPgsz(sqlite3_backup *p){
72970 int rc;
72971 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
72972 return rc;
72973}
72974
72975/*
72976** Check that there is no open read-transaction on the b-tree passed as the
72977** second argument. If there is not, return SQLITE_OK. Otherwise, if there
72978** is an open read-transaction, return SQLITE_ERROR and leave an error
72979** message in database handle db.
72980*/
72981static int checkReadTransaction(sqlite3 *db, Btree *p){
72982 if( sqlite3BtreeIsInReadTrans(p) ){
72983 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "destination database is in use");
72984 return SQLITE_ERROR;
72985 }
72986 return SQLITE_OK;
72987}
72988
72989/*
72990** Create an sqlite3_backup process to copy the contents of zSrcDb from
72991** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
72992** a pointer to the new sqlite3_backup object.
72993**
72994** If an error occurs, NULL is returned and an error code and error message
72995** stored in database handle pDestDb.
72996*/
72997SQLITE_API sqlite3_backup *sqlite3_backup_init(
72998 sqlite3* pDestDb, /* Database to write to */
72999 const char *zDestDb, /* Name of database within pDestDb */
73000 sqlite3* pSrcDb, /* Database connection to read from */
73001 const char *zSrcDb /* Name of database within pSrcDb */
73002){
73003 sqlite3_backup *p; /* Value to return */
73004
73005#ifdef SQLITE_ENABLE_API_ARMOR
73006 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
73007 (void)SQLITE_MISUSE_BKPT;
73008 return 0;
73009 }
73010#endif
73011
73012 /* Lock the source database handle. The destination database
73013 ** handle is not locked in this routine, but it is locked in
73014 ** sqlite3_backup_step(). The user is required to ensure that no
73015 ** other thread accesses the destination handle for the duration
73016 ** of the backup operation. Any attempt to use the destination
73017 ** database connection while a backup is in progress may cause
73018 ** a malfunction or a deadlock.
73019 */
73020 sqlite3_mutex_enter(pSrcDb->mutex);
73021 sqlite3_mutex_enter(pDestDb->mutex);
73022
73023 if( pSrcDb==pDestDb ){
73024 sqlite3ErrorWithMsg(
73025 pDestDb, SQLITE_ERROR, "source and destination must be distinct"
73026 );
73027 p = 0;
73028 }else {
73029 /* Allocate space for a new sqlite3_backup object...
73030 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
73031 ** call to sqlite3_backup_init() and is destroyed by a call to
73032 ** sqlite3_backup_finish(). */
73033 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
73034 if( !p ){
73035 sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT);
73036 }
73037 }
73038
73039 /* If the allocation succeeded, populate the new object. */
73040 if( p ){
73041 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
73042 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
73043 p->pDestDb = pDestDb;
73044 p->pSrcDb = pSrcDb;
73045 p->iNext = 1;
73046 p->isAttached = 0;
73047
73048 if( 0==p->pSrc || 0==p->pDest
73049 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
73050 ){
73051 /* One (or both) of the named databases did not exist or an OOM
73052 ** error was hit. Or there is a transaction open on the destination
73053 ** database. The error has already been written into the pDestDb
73054 ** handle. All that is left to do here is free the sqlite3_backup
73055 ** structure. */
73056 sqlite3_free(p);
73057 p = 0;
73058 }
73059 }
73060 if( p ){
73061 p->pSrc->nBackup++;
73062 }
73063
73064 sqlite3_mutex_leave(pDestDb->mutex);
73065 sqlite3_mutex_leave(pSrcDb->mutex);
73066 return p;
73067}
73068
73069/*
73070** Argument rc is an SQLite error code. Return true if this error is
73071** considered fatal if encountered during a backup operation. All errors
73072** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
73073*/
73074static int isFatalError(int rc){
73075 return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
73076}
73077
73078/*
73079** Parameter zSrcData points to a buffer containing the data for
73080** page iSrcPg from the source database. Copy this data into the
73081** destination database.
73082*/
73083static int backupOnePage(
73084 sqlite3_backup *p, /* Backup handle */
73085 Pgno iSrcPg, /* Source database page to backup */
73086 const u8 *zSrcData, /* Source database page data */
73087 int bUpdate /* True for an update, false otherwise */
73088){
73089 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
73090 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
73091 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
73092 const int nCopy = MIN(nSrcPgsz, nDestPgsz);
73093 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
73094#ifdef SQLITE_HAS_CODEC
73095 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
73096 ** guaranteed that the shared-mutex is held by this thread, handle
73097 ** p->pSrc may not actually be the owner. */
73098 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
73099 int nDestReserve = sqlite3BtreeGetOptimalReserve(p->pDest);
73100#endif
73101 int rc = SQLITE_OK;
73102 i64 iOff;
73103
73104 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
73105 assert( p->bDestLocked );
73106 assert( !isFatalError(p->rc) );
73107 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
73108 assert( zSrcData );
73109
73110 /* Catch the case where the destination is an in-memory database and the
73111 ** page sizes of the source and destination differ.
73112 */
73113 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
73114 rc = SQLITE_READONLY;
73115 }
73116
73117#ifdef SQLITE_HAS_CODEC
73118 /* Backup is not possible if the page size of the destination is changing
73119 ** and a codec is in use.
73120 */
73121 if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
73122 rc = SQLITE_READONLY;
73123 }
73124
73125 /* Backup is not possible if the number of bytes of reserve space differ
73126 ** between source and destination. If there is a difference, try to
73127 ** fix the destination to agree with the source. If that is not possible,
73128 ** then the backup cannot proceed.
73129 */
73130 if( nSrcReserve!=nDestReserve ){
73131 u32 newPgsz = nSrcPgsz;
73132 rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
73133 if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
73134 }
73135#endif
73136
73137 /* This loop runs once for each destination page spanned by the source
73138 ** page. For each iteration, variable iOff is set to the byte offset
73139 ** of the destination page.
73140 */
73141 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
73142 DbPage *pDestPg = 0;
73143 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
73144 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
73145 if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
73146 && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
73147 ){
73148 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
73149 u8 *zDestData = sqlite3PagerGetData(pDestPg);
73150 u8 *zOut = &zDestData[iOff%nDestPgsz];
73151
73152 /* Copy the data from the source page into the destination page.
73153 ** Then clear the Btree layer MemPage.isInit flag. Both this module
73154 ** and the pager code use this trick (clearing the first byte
73155 ** of the page 'extra' space to invalidate the Btree layers
73156 ** cached parse of the page). MemPage.isInit is marked
73157 ** "MUST BE FIRST" for this purpose.
73158 */
73159 memcpy(zOut, zIn, nCopy);
73160 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
73161 if( iOff==0 && bUpdate==0 ){
73162 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
73163 }
73164 }
73165 sqlite3PagerUnref(pDestPg);
73166 }
73167
73168 return rc;
73169}
73170
73171/*
73172** If pFile is currently larger than iSize bytes, then truncate it to
73173** exactly iSize bytes. If pFile is not larger than iSize bytes, then
73174** this function is a no-op.
73175**
73176** Return SQLITE_OK if everything is successful, or an SQLite error
73177** code if an error occurs.
73178*/
73179static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
73180 i64 iCurrent;
73181 int rc = sqlite3OsFileSize(pFile, &iCurrent);
73182 if( rc==SQLITE_OK && iCurrent>iSize ){
73183 rc = sqlite3OsTruncate(pFile, iSize);
73184 }
73185 return rc;
73186}
73187
73188/*
73189** Register this backup object with the associated source pager for
73190** callbacks when pages are changed or the cache invalidated.
73191*/
73192static void attachBackupObject(sqlite3_backup *p){
73193 sqlite3_backup **pp;
73194 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
73195 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
73196 p->pNext = *pp;
73197 *pp = p;
73198 p->isAttached = 1;
73199}
73200
73201/*
73202** Copy nPage pages from the source b-tree to the destination.
73203*/
73204SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
73205 int rc;
73206 int destMode; /* Destination journal mode */
73207 int pgszSrc = 0; /* Source page size */
73208 int pgszDest = 0; /* Destination page size */
73209
73210#ifdef SQLITE_ENABLE_API_ARMOR
73211 if( p==0 ) return SQLITE_MISUSE_BKPT;
73212#endif
73213 sqlite3_mutex_enter(p->pSrcDb->mutex);
73214 sqlite3BtreeEnter(p->pSrc);
73215 if( p->pDestDb ){
73216 sqlite3_mutex_enter(p->pDestDb->mutex);
73217 }
73218
73219 rc = p->rc;
73220 if( !isFatalError(rc) ){
73221 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
73222 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
73223 int ii; /* Iterator variable */
73224 int nSrcPage = -1; /* Size of source db in pages */
73225 int bCloseTrans = 0; /* True if src db requires unlocking */
73226
73227 /* If the source pager is currently in a write-transaction, return
73228 ** SQLITE_BUSY immediately.
73229 */
73230 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
73231 rc = SQLITE_BUSY;
73232 }else{
73233 rc = SQLITE_OK;
73234 }
73235
73236 /* If there is no open read-transaction on the source database, open
73237 ** one now. If a transaction is opened here, then it will be closed
73238 ** before this function exits.
73239 */
73240 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
73241 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
73242 bCloseTrans = 1;
73243 }
73244
73245 /* If the destination database has not yet been locked (i.e. if this
73246 ** is the first call to backup_step() for the current backup operation),
73247 ** try to set its page size to the same as the source database. This
73248 ** is especially important on ZipVFS systems, as in that case it is
73249 ** not possible to create a database file that uses one page size by
73250 ** writing to it with another. */
73251 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
73252 rc = SQLITE_NOMEM;
73253 }
73254
73255 /* Lock the destination database, if it is not locked already. */
73256 if( SQLITE_OK==rc && p->bDestLocked==0
73257 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
73258 (int*)&p->iDestSchema))
73259 ){
73260 p->bDestLocked = 1;
73261 }
73262
73263 /* Do not allow backup if the destination database is in WAL mode
73264 ** and the page sizes are different between source and destination */
73265 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
73266 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
73267 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
73268 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
73269 rc = SQLITE_READONLY;
73270 }
73271
73272 /* Now that there is a read-lock on the source database, query the
73273 ** source pager for the number of pages in the database.
73274 */
73275 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
73276 assert( nSrcPage>=0 );
73277 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
73278 const Pgno iSrcPg = p->iNext; /* Source page number */
73279 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
73280 DbPage *pSrcPg; /* Source page object */
73281 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY);
73282 if( rc==SQLITE_OK ){
73283 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
73284 sqlite3PagerUnref(pSrcPg);
73285 }
73286 }
73287 p->iNext++;
73288 }
73289 if( rc==SQLITE_OK ){
73290 p->nPagecount = nSrcPage;
73291 p->nRemaining = nSrcPage+1-p->iNext;
73292 if( p->iNext>(Pgno)nSrcPage ){
73293 rc = SQLITE_DONE;
73294 }else if( !p->isAttached ){
73295 attachBackupObject(p);
73296 }
73297 }
73298
73299 /* Update the schema version field in the destination database. This
73300 ** is to make sure that the schema-version really does change in
73301 ** the case where the source and destination databases have the
73302 ** same schema version.
73303 */
73304 if( rc==SQLITE_DONE ){
73305 if( nSrcPage==0 ){
73306 rc = sqlite3BtreeNewDb(p->pDest);
73307 nSrcPage = 1;
73308 }
73309 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
73310 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
73311 }
73312 if( rc==SQLITE_OK ){
73313 if( p->pDestDb ){
73314 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
73315 }
73316 if( destMode==PAGER_JOURNALMODE_WAL ){
73317 rc = sqlite3BtreeSetVersion(p->pDest, 2);
73318 }
73319 }
73320 if( rc==SQLITE_OK ){
73321 int nDestTruncate;
73322 /* Set nDestTruncate to the final number of pages in the destination
73323 ** database. The complication here is that the destination page
73324 ** size may be different to the source page size.
73325 **
73326 ** If the source page size is smaller than the destination page size,
73327 ** round up. In this case the call to sqlite3OsTruncate() below will
73328 ** fix the size of the file. However it is important to call
73329 ** sqlite3PagerTruncateImage() here so that any pages in the
73330 ** destination file that lie beyond the nDestTruncate page mark are
73331 ** journalled by PagerCommitPhaseOne() before they are destroyed
73332 ** by the file truncation.
73333 */
73334 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
73335 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
73336 if( pgszSrc<pgszDest ){
73337 int ratio = pgszDest/pgszSrc;
73338 nDestTruncate = (nSrcPage+ratio-1)/ratio;
73339 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
73340 nDestTruncate--;
73341 }
73342 }else{
73343 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
73344 }
73345 assert( nDestTruncate>0 );
73346
73347 if( pgszSrc<pgszDest ){
73348 /* If the source page-size is smaller than the destination page-size,
73349 ** two extra things may need to happen:
73350 **
73351 ** * The destination may need to be truncated, and
73352 **
73353 ** * Data stored on the pages immediately following the
73354 ** pending-byte page in the source database may need to be
73355 ** copied into the destination database.
73356 */
73357 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
73358 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
73359 Pgno iPg;
73360 int nDstPage;
73361 i64 iOff;
73362 i64 iEnd;
73363
73364 assert( pFile );
73365 assert( nDestTruncate==0
73366 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
73367 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
73368 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
73369 ));
73370
73371 /* This block ensures that all data required to recreate the original
73372 ** database has been stored in the journal for pDestPager and the
73373 ** journal synced to disk. So at this point we may safely modify
73374 ** the database file in any way, knowing that if a power failure
73375 ** occurs, the original database will be reconstructed from the
73376 ** journal file. */
73377 sqlite3PagerPagecount(pDestPager, &nDstPage);
73378 for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
73379 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
73380 DbPage *pPg;
73381 rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
73382 if( rc==SQLITE_OK ){
73383 rc = sqlite3PagerWrite(pPg);
73384 sqlite3PagerUnref(pPg);
73385 }
73386 }
73387 }
73388 if( rc==SQLITE_OK ){
73389 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
73390 }
73391
73392 /* Write the extra pages and truncate the database file as required */
73393 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
73394 for(
73395 iOff=PENDING_BYTE+pgszSrc;
73396 rc==SQLITE_OK && iOff<iEnd;
73397 iOff+=pgszSrc
73398 ){
73399 PgHdr *pSrcPg = 0;
73400 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
73401 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
73402 if( rc==SQLITE_OK ){
73403 u8 *zData = sqlite3PagerGetData(pSrcPg);
73404 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
73405 }
73406 sqlite3PagerUnref(pSrcPg);
73407 }
73408 if( rc==SQLITE_OK ){
73409 rc = backupTruncateFile(pFile, iSize);
73410 }
73411
73412 /* Sync the database file to disk. */
73413 if( rc==SQLITE_OK ){
73414 rc = sqlite3PagerSync(pDestPager, 0);
73415 }
73416 }else{
73417 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
73418 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
73419 }
73420
73421 /* Finish committing the transaction to the destination database. */
73422 if( SQLITE_OK==rc
73423 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
73424 ){
73425 rc = SQLITE_DONE;
73426 }
73427 }
73428 }
73429
73430 /* If bCloseTrans is true, then this function opened a read transaction
73431 ** on the source database. Close the read transaction here. There is
73432 ** no need to check the return values of the btree methods here, as
73433 ** "committing" a read-only transaction cannot fail.
73434 */
73435 if( bCloseTrans ){
73436 TESTONLY( int rc2 );
73437 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
73438 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
73439 assert( rc2==SQLITE_OK );
73440 }
73441
73442 if( rc==SQLITE_IOERR_NOMEM ){
73443 rc = SQLITE_NOMEM_BKPT;
73444 }
73445 p->rc = rc;
73446 }
73447 if( p->pDestDb ){
73448 sqlite3_mutex_leave(p->pDestDb->mutex);
73449 }
73450 sqlite3BtreeLeave(p->pSrc);
73451 sqlite3_mutex_leave(p->pSrcDb->mutex);
73452 return rc;
73453}
73454
73455/*
73456** Release all resources associated with an sqlite3_backup* handle.
73457*/
73458SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
73459 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
73460 sqlite3 *pSrcDb; /* Source database connection */
73461 int rc; /* Value to return */
73462
73463 /* Enter the mutexes */
73464 if( p==0 ) return SQLITE_OK;
73465 pSrcDb = p->pSrcDb;
73466 sqlite3_mutex_enter(pSrcDb->mutex);
73467 sqlite3BtreeEnter(p->pSrc);
73468 if( p->pDestDb ){
73469 sqlite3_mutex_enter(p->pDestDb->mutex);
73470 }
73471
73472 /* Detach this backup from the source pager. */
73473 if( p->pDestDb ){
73474 p->pSrc->nBackup--;
73475 }
73476 if( p->isAttached ){
73477 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
73478 while( *pp!=p ){
73479 pp = &(*pp)->pNext;
73480 }
73481 *pp = p->pNext;
73482 }
73483
73484 /* If a transaction is still open on the Btree, roll it back. */
73485 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
73486
73487 /* Set the error code of the destination database handle. */
73488 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
73489 if( p->pDestDb ){
73490 sqlite3Error(p->pDestDb, rc);
73491
73492 /* Exit the mutexes and free the backup context structure. */
73493 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
73494 }
73495 sqlite3BtreeLeave(p->pSrc);
73496 if( p->pDestDb ){
73497 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
73498 ** call to sqlite3_backup_init() and is destroyed by a call to
73499 ** sqlite3_backup_finish(). */
73500 sqlite3_free(p);
73501 }
73502 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
73503 return rc;
73504}
73505
73506/*
73507** Return the number of pages still to be backed up as of the most recent
73508** call to sqlite3_backup_step().
73509*/
73510SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
73511#ifdef SQLITE_ENABLE_API_ARMOR
73512 if( p==0 ){
73513 (void)SQLITE_MISUSE_BKPT;
73514 return 0;
73515 }
73516#endif
73517 return p->nRemaining;
73518}
73519
73520/*
73521** Return the total number of pages in the source database as of the most
73522** recent call to sqlite3_backup_step().
73523*/
73524SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
73525#ifdef SQLITE_ENABLE_API_ARMOR
73526 if( p==0 ){
73527 (void)SQLITE_MISUSE_BKPT;
73528 return 0;
73529 }
73530#endif
73531 return p->nPagecount;
73532}
73533
73534/*
73535** This function is called after the contents of page iPage of the
73536** source database have been modified. If page iPage has already been
73537** copied into the destination database, then the data written to the
73538** destination is now invalidated. The destination copy of iPage needs
73539** to be updated with the new data before the backup operation is
73540** complete.
73541**
73542** It is assumed that the mutex associated with the BtShared object
73543** corresponding to the source database is held when this function is
73544** called.
73545*/
73546static SQLITE_NOINLINE void backupUpdate(
73547 sqlite3_backup *p,
73548 Pgno iPage,
73549 const u8 *aData
73550){
73551 assert( p!=0 );
73552 do{
73553 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
73554 if( !isFatalError(p->rc) && iPage<p->iNext ){
73555 /* The backup process p has already copied page iPage. But now it
73556 ** has been modified by a transaction on the source pager. Copy
73557 ** the new data into the backup.
73558 */
73559 int rc;
73560 assert( p->pDestDb );
73561 sqlite3_mutex_enter(p->pDestDb->mutex);
73562 rc = backupOnePage(p, iPage, aData, 1);
73563 sqlite3_mutex_leave(p->pDestDb->mutex);
73564 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
73565 if( rc!=SQLITE_OK ){
73566 p->rc = rc;
73567 }
73568 }
73569 }while( (p = p->pNext)!=0 );
73570}
73571SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
73572 if( pBackup ) backupUpdate(pBackup, iPage, aData);
73573}
73574
73575/*
73576** Restart the backup process. This is called when the pager layer
73577** detects that the database has been modified by an external database
73578** connection. In this case there is no way of knowing which of the
73579** pages that have been copied into the destination database are still
73580** valid and which are not, so the entire process needs to be restarted.
73581**
73582** It is assumed that the mutex associated with the BtShared object
73583** corresponding to the source database is held when this function is
73584** called.
73585*/
73586SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
73587 sqlite3_backup *p; /* Iterator variable */
73588 for(p=pBackup; p; p=p->pNext){
73589 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
73590 p->iNext = 1;
73591 }
73592}
73593
73594#ifndef SQLITE_OMIT_VACUUM
73595/*
73596** Copy the complete content of pBtFrom into pBtTo. A transaction
73597** must be active for both files.
73598**
73599** The size of file pTo may be reduced by this operation. If anything
73600** goes wrong, the transaction on pTo is rolled back. If successful, the
73601** transaction is committed before returning.
73602*/
73603SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
73604 int rc;
73605 sqlite3_file *pFd; /* File descriptor for database pTo */
73606 sqlite3_backup b;
73607 sqlite3BtreeEnter(pTo);
73608 sqlite3BtreeEnter(pFrom);
73609
73610 assert( sqlite3BtreeIsInTrans(pTo) );
73611 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
73612 if( pFd->pMethods ){
73613 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
73614 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
73615 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
73616 if( rc ) goto copy_finished;
73617 }
73618
73619 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
73620 ** to 0. This is used by the implementations of sqlite3_backup_step()
73621 ** and sqlite3_backup_finish() to detect that they are being called
73622 ** from this function, not directly by the user.
73623 */
73624 memset(&b, 0, sizeof(b));
73625 b.pSrcDb = pFrom->db;
73626 b.pSrc = pFrom;
73627 b.pDest = pTo;
73628 b.iNext = 1;
73629
73630#ifdef SQLITE_HAS_CODEC
73631 sqlite3PagerAlignReserve(sqlite3BtreePager(pTo), sqlite3BtreePager(pFrom));
73632#endif
73633
73634 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
73635 ** file. By passing this as the number of pages to copy to
73636 ** sqlite3_backup_step(), we can guarantee that the copy finishes
73637 ** within a single call (unless an error occurs). The assert() statement
73638 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
73639 ** or an error code. */
73640 sqlite3_backup_step(&b, 0x7FFFFFFF);
73641 assert( b.rc!=SQLITE_OK );
73642
73643 rc = sqlite3_backup_finish(&b);
73644 if( rc==SQLITE_OK ){
73645 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
73646 }else{
73647 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
73648 }
73649
73650 assert( sqlite3BtreeIsInTrans(pTo)==0 );
73651copy_finished:
73652 sqlite3BtreeLeave(pFrom);
73653 sqlite3BtreeLeave(pTo);
73654 return rc;
73655}
73656#endif /* SQLITE_OMIT_VACUUM */
73657
73658/************** End of backup.c **********************************************/
73659/************** Begin file vdbemem.c *****************************************/
73660/*
73661** 2004 May 26
73662**
73663** The author disclaims copyright to this source code. In place of
73664** a legal notice, here is a blessing:
73665**
73666** May you do good and not evil.
73667** May you find forgiveness for yourself and forgive others.
73668** May you share freely, never taking more than you give.
73669**
73670*************************************************************************
73671**
73672** This file contains code use to manipulate "Mem" structure. A "Mem"
73673** stores a single value in the VDBE. Mem is an opaque structure visible
73674** only within the VDBE. Interface routines refer to a Mem using the
73675** name sqlite_value
73676*/
73677/* #include "sqliteInt.h" */
73678/* #include "vdbeInt.h" */
73679
73680#ifdef SQLITE_DEBUG
73681/*
73682** Check invariants on a Mem object.
73683**
73684** This routine is intended for use inside of assert() statements, like
73685** this: assert( sqlite3VdbeCheckMemInvariants(pMem) );
73686*/
73687SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
73688 /* If MEM_Dyn is set then Mem.xDel!=0.
73689 ** Mem.xDel might not be initialized if MEM_Dyn is clear.
73690 */
73691 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
73692
73693 /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we
73694 ** ensure that if Mem.szMalloc>0 then it is safe to do
73695 ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
73696 ** That saves a few cycles in inner loops. */
73697 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
73698
73699 /* Cannot be both MEM_Int and MEM_Real at the same time */
73700 assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
73701
73702 if( p->flags & MEM_Null ){
73703 /* Cannot be both MEM_Null and some other type */
73704 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
73705
73706 /* If MEM_Null is set, then either the value is a pure NULL (the usual
73707 ** case) or it is a pointer set using sqlite3_bind_pointer() or
73708 ** sqlite3_result_pointer(). If a pointer, then MEM_Term must also be
73709 ** set.
73710 */
73711 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
73712 /* This is a pointer type. There may be a flag to indicate what to
73713 ** do with the pointer. */
73714 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
73715 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
73716 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
73717
73718 /* No other bits set */
73719 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype
73720 |MEM_Dyn|MEM_Ephem|MEM_Static))==0 );
73721 }else{
73722 /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
73723 ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
73724 }
73725 }else{
73726 /* The MEM_Cleared bit is only allowed on NULLs */
73727 assert( (p->flags & MEM_Cleared)==0 );
73728 }
73729
73730 /* The szMalloc field holds the correct memory allocation size */
73731 assert( p->szMalloc==0
73732 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
73733
73734 /* If p holds a string or blob, the Mem.z must point to exactly
73735 ** one of the following:
73736 **
73737 ** (1) Memory in Mem.zMalloc and managed by the Mem object
73738 ** (2) Memory to be freed using Mem.xDel
73739 ** (3) An ephemeral string or blob
73740 ** (4) A static string or blob
73741 */
73742 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
73743 assert(
73744 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
73745 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
73746 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
73747 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
73748 );
73749 }
73750 return 1;
73751}
73752#endif
73753
73754#ifdef SQLITE_DEBUG
73755/*
73756** Check that string value of pMem agrees with its integer or real value.
73757**
73758** A single int or real value always converts to the same strings. But
73759** many different strings can be converted into the same int or real.
73760** If a table contains a numeric value and an index is based on the
73761** corresponding string value, then it is important that the string be
73762** derived from the numeric value, not the other way around, to ensure
73763** that the index and table are consistent. See ticket
73764** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
73765** an example.
73766**
73767** This routine looks at pMem to verify that if it has both a numeric
73768** representation and a string representation then the string rep has
73769** been derived from the numeric and not the other way around. It returns
73770** true if everything is ok and false if there is a problem.
73771**
73772** This routine is for use inside of assert() statements only.
73773*/
73774SQLITE_PRIVATE int sqlite3VdbeMemConsistentDualRep(Mem *p){
73775 char zBuf[100];
73776 char *z;
73777 int i, j, incr;
73778 if( (p->flags & MEM_Str)==0 ) return 1;
73779 if( (p->flags & (MEM_Int|MEM_Real))==0 ) return 1;
73780 if( p->flags & MEM_Int ){
73781 sqlite3_snprintf(sizeof(zBuf),zBuf,"%lld",p->u.i);
73782 }else{
73783 sqlite3_snprintf(sizeof(zBuf),zBuf,"%!.15g",p->u.r);
73784 }
73785 z = p->z;
73786 i = j = 0;
73787 incr = 1;
73788 if( p->enc!=SQLITE_UTF8 ){
73789 incr = 2;
73790 if( p->enc==SQLITE_UTF16BE ) z++;
73791 }
73792 while( zBuf[j] ){
73793 if( zBuf[j++]!=z[i] ) return 0;
73794 i += incr;
73795 }
73796 return 1;
73797}
73798#endif /* SQLITE_DEBUG */
73799
73800/*
73801** If pMem is an object with a valid string representation, this routine
73802** ensures the internal encoding for the string representation is
73803** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
73804**
73805** If pMem is not a string object, or the encoding of the string
73806** representation is already stored using the requested encoding, then this
73807** routine is a no-op.
73808**
73809** SQLITE_OK is returned if the conversion is successful (or not required).
73810** SQLITE_NOMEM may be returned if a malloc() fails during conversion
73811** between formats.
73812*/
73813SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
73814#ifndef SQLITE_OMIT_UTF16
73815 int rc;
73816#endif
73817 assert( !sqlite3VdbeMemIsRowSet(pMem) );
73818 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
73819 || desiredEnc==SQLITE_UTF16BE );
73820 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
73821 return SQLITE_OK;
73822 }
73823 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
73824#ifdef SQLITE_OMIT_UTF16
73825 return SQLITE_ERROR;
73826#else
73827
73828 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
73829 ** then the encoding of the value may not have changed.
73830 */
73831 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
73832 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
73833 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
73834 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
73835 return rc;
73836#endif
73837}
73838
73839/*
73840** Make sure pMem->z points to a writable allocation of at least
73841** min(n,32) bytes.
73842**
73843** If the bPreserve argument is true, then copy of the content of
73844** pMem->z into the new allocation. pMem must be either a string or
73845** blob if bPreserve is true. If bPreserve is false, any prior content
73846** in pMem->z is discarded.
73847*/
73848SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
73849 assert( sqlite3VdbeCheckMemInvariants(pMem) );
73850 assert( !sqlite3VdbeMemIsRowSet(pMem) );
73851 testcase( pMem->db==0 );
73852
73853 /* If the bPreserve flag is set to true, then the memory cell must already
73854 ** contain a valid string or blob value. */
73855 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
73856 testcase( bPreserve && pMem->z==0 );
73857
73858 assert( pMem->szMalloc==0
73859 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
73860 if( n<32 ) n = 32;
73861 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
73862 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
73863 bPreserve = 0;
73864 }else{
73865 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
73866 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
73867 }
73868 if( pMem->zMalloc==0 ){
73869 sqlite3VdbeMemSetNull(pMem);
73870 pMem->z = 0;
73871 pMem->szMalloc = 0;
73872 return SQLITE_NOMEM_BKPT;
73873 }else{
73874 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
73875 }
73876
73877 if( bPreserve && pMem->z ){
73878 assert( pMem->z!=pMem->zMalloc );
73879 memcpy(pMem->zMalloc, pMem->z, pMem->n);
73880 }
73881 if( (pMem->flags&MEM_Dyn)!=0 ){
73882 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
73883 pMem->xDel((void *)(pMem->z));
73884 }
73885
73886 pMem->z = pMem->zMalloc;
73887 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
73888 return SQLITE_OK;
73889}
73890
73891/*
73892** Change the pMem->zMalloc allocation to be at least szNew bytes.
73893** If pMem->zMalloc already meets or exceeds the requested size, this
73894** routine is a no-op.
73895**
73896** Any prior string or blob content in the pMem object may be discarded.
73897** The pMem->xDel destructor is called, if it exists. Though MEM_Str
73898** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null
73899** values are preserved.
73900**
73901** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
73902** if unable to complete the resizing.
73903*/
73904SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
73905 assert( szNew>0 );
73906 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
73907 if( pMem->szMalloc<szNew ){
73908 return sqlite3VdbeMemGrow(pMem, szNew, 0);
73909 }
73910 assert( (pMem->flags & MEM_Dyn)==0 );
73911 pMem->z = pMem->zMalloc;
73912 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
73913 return SQLITE_OK;
73914}
73915
73916/*
73917** It is already known that pMem contains an unterminated string.
73918** Add the zero terminator.
73919*/
73920static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
73921 if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
73922 return SQLITE_NOMEM_BKPT;
73923 }
73924 pMem->z[pMem->n] = 0;
73925 pMem->z[pMem->n+1] = 0;
73926 pMem->flags |= MEM_Term;
73927 return SQLITE_OK;
73928}
73929
73930/*
73931** Change pMem so that its MEM_Str or MEM_Blob value is stored in
73932** MEM.zMalloc, where it can be safely written.
73933**
73934** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
73935*/
73936SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
73937 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
73938 assert( !sqlite3VdbeMemIsRowSet(pMem) );
73939 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
73940 if( ExpandBlob(pMem) ) return SQLITE_NOMEM;
73941 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
73942 int rc = vdbeMemAddTerminator(pMem);
73943 if( rc ) return rc;
73944 }
73945 }
73946 pMem->flags &= ~MEM_Ephem;
73947#ifdef SQLITE_DEBUG
73948 pMem->pScopyFrom = 0;
73949#endif
73950
73951 return SQLITE_OK;
73952}
73953
73954/*
73955** If the given Mem* has a zero-filled tail, turn it into an ordinary
73956** blob stored in dynamically allocated space.
73957*/
73958#ifndef SQLITE_OMIT_INCRBLOB
73959SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
73960 int nByte;
73961 assert( pMem->flags & MEM_Zero );
73962 assert( pMem->flags&MEM_Blob );
73963 assert( !sqlite3VdbeMemIsRowSet(pMem) );
73964 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
73965
73966 /* Set nByte to the number of bytes required to store the expanded blob. */
73967 nByte = pMem->n + pMem->u.nZero;
73968 if( nByte<=0 ){
73969 nByte = 1;
73970 }
73971 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
73972 return SQLITE_NOMEM_BKPT;
73973 }
73974
73975 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
73976 pMem->n += pMem->u.nZero;
73977 pMem->flags &= ~(MEM_Zero|MEM_Term);
73978 return SQLITE_OK;
73979}
73980#endif
73981
73982/*
73983** Make sure the given Mem is \u0000 terminated.
73984*/
73985SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
73986 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
73987 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
73988 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
73989 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
73990 return SQLITE_OK; /* Nothing to do */
73991 }else{
73992 return vdbeMemAddTerminator(pMem);
73993 }
73994}
73995
73996/*
73997** Add MEM_Str to the set of representations for the given Mem. Numbers
73998** are converted using sqlite3_snprintf(). Converting a BLOB to a string
73999** is a no-op.
74000**
74001** Existing representations MEM_Int and MEM_Real are invalidated if
74002** bForce is true but are retained if bForce is false.
74003**
74004** A MEM_Null value will never be passed to this function. This function is
74005** used for converting values to text for returning to the user (i.e. via
74006** sqlite3_value_text()), or for ensuring that values to be used as btree
74007** keys are strings. In the former case a NULL pointer is returned the
74008** user and the latter is an internal programming error.
74009*/
74010SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
74011 int fg = pMem->flags;
74012 const int nByte = 32;
74013
74014 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74015 assert( !(fg&MEM_Zero) );
74016 assert( !(fg&(MEM_Str|MEM_Blob)) );
74017 assert( fg&(MEM_Int|MEM_Real) );
74018 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74019 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
74020
74021
74022 if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
74023 pMem->enc = 0;
74024 return SQLITE_NOMEM_BKPT;
74025 }
74026
74027 /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
74028 ** string representation of the value. Then, if the required encoding
74029 ** is UTF-16le or UTF-16be do a translation.
74030 **
74031 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
74032 */
74033 if( fg & MEM_Int ){
74034 sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
74035 }else{
74036 assert( fg & MEM_Real );
74037 sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
74038 }
74039 pMem->n = sqlite3Strlen30(pMem->z);
74040 pMem->enc = SQLITE_UTF8;
74041 pMem->flags |= MEM_Str|MEM_Term;
74042 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
74043 sqlite3VdbeChangeEncoding(pMem, enc);
74044 return SQLITE_OK;
74045}
74046
74047/*
74048** Memory cell pMem contains the context of an aggregate function.
74049** This routine calls the finalize method for that function. The
74050** result of the aggregate is stored back into pMem.
74051**
74052** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
74053** otherwise.
74054*/
74055SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
74056 sqlite3_context ctx;
74057 Mem t;
74058 assert( pFunc!=0 );
74059 assert( pFunc->xFinalize!=0 );
74060 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
74061 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74062 memset(&ctx, 0, sizeof(ctx));
74063 memset(&t, 0, sizeof(t));
74064 t.flags = MEM_Null;
74065 t.db = pMem->db;
74066 ctx.pOut = &t;
74067 ctx.pMem = pMem;
74068 ctx.pFunc = pFunc;
74069 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
74070 assert( (pMem->flags & MEM_Dyn)==0 );
74071 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
74072 memcpy(pMem, &t, sizeof(t));
74073 return ctx.isError;
74074}
74075
74076/*
74077** Memory cell pAccum contains the context of an aggregate function.
74078** This routine calls the xValue method for that function and stores
74079** the results in memory cell pMem.
74080**
74081** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK
74082** otherwise.
74083*/
74084#ifndef SQLITE_OMIT_WINDOWFUNC
74085SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){
74086 sqlite3_context ctx;
74087 Mem t;
74088 assert( pFunc!=0 );
74089 assert( pFunc->xValue!=0 );
74090 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
74091 assert( pAccum->db==0 || sqlite3_mutex_held(pAccum->db->mutex) );
74092 memset(&ctx, 0, sizeof(ctx));
74093 memset(&t, 0, sizeof(t));
74094 t.flags = MEM_Null;
74095 t.db = pAccum->db;
74096 sqlite3VdbeMemSetNull(pOut);
74097 ctx.pOut = pOut;
74098 ctx.pMem = pAccum;
74099 ctx.pFunc = pFunc;
74100 pFunc->xValue(&ctx);
74101 return ctx.isError;
74102}
74103#endif /* SQLITE_OMIT_WINDOWFUNC */
74104
74105/*
74106** If the memory cell contains a value that must be freed by
74107** invoking the external callback in Mem.xDel, then this routine
74108** will free that value. It also sets Mem.flags to MEM_Null.
74109**
74110** This is a helper routine for sqlite3VdbeMemSetNull() and
74111** for sqlite3VdbeMemRelease(). Use those other routines as the
74112** entry point for releasing Mem resources.
74113*/
74114static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
74115 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
74116 assert( VdbeMemDynamic(p) );
74117 if( p->flags&MEM_Agg ){
74118 sqlite3VdbeMemFinalize(p, p->u.pDef);
74119 assert( (p->flags & MEM_Agg)==0 );
74120 testcase( p->flags & MEM_Dyn );
74121 }
74122 if( p->flags&MEM_Dyn ){
74123 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
74124 p->xDel((void *)p->z);
74125 }
74126 p->flags = MEM_Null;
74127}
74128
74129/*
74130** Release memory held by the Mem p, both external memory cleared
74131** by p->xDel and memory in p->zMalloc.
74132**
74133** This is a helper routine invoked by sqlite3VdbeMemRelease() in
74134** the unusual case where there really is memory in p that needs
74135** to be freed.
74136*/
74137static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
74138 if( VdbeMemDynamic(p) ){
74139 vdbeMemClearExternAndSetNull(p);
74140 }
74141 if( p->szMalloc ){
74142 sqlite3DbFreeNN(p->db, p->zMalloc);
74143 p->szMalloc = 0;
74144 }
74145 p->z = 0;
74146}
74147
74148/*
74149** Release any memory resources held by the Mem. Both the memory that is
74150** free by Mem.xDel and the Mem.zMalloc allocation are freed.
74151**
74152** Use this routine prior to clean up prior to abandoning a Mem, or to
74153** reset a Mem back to its minimum memory utilization.
74154**
74155** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
74156** prior to inserting new content into the Mem.
74157*/
74158SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
74159 assert( sqlite3VdbeCheckMemInvariants(p) );
74160 if( VdbeMemDynamic(p) || p->szMalloc ){
74161 vdbeMemClear(p);
74162 }
74163}
74164
74165/*
74166** Convert a 64-bit IEEE double into a 64-bit signed integer.
74167** If the double is out of range of a 64-bit signed integer then
74168** return the closest available 64-bit signed integer.
74169*/
74170static SQLITE_NOINLINE i64 doubleToInt64(double r){
74171#ifdef SQLITE_OMIT_FLOATING_POINT
74172 /* When floating-point is omitted, double and int64 are the same thing */
74173 return r;
74174#else
74175 /*
74176 ** Many compilers we encounter do not define constants for the
74177 ** minimum and maximum 64-bit integers, or they define them
74178 ** inconsistently. And many do not understand the "LL" notation.
74179 ** So we define our own static constants here using nothing
74180 ** larger than a 32-bit integer constant.
74181 */
74182 static const i64 maxInt = LARGEST_INT64;
74183 static const i64 minInt = SMALLEST_INT64;
74184
74185 if( r<=(double)minInt ){
74186 return minInt;
74187 }else if( r>=(double)maxInt ){
74188 return maxInt;
74189 }else{
74190 return (i64)r;
74191 }
74192#endif
74193}
74194
74195/*
74196** Return some kind of integer value which is the best we can do
74197** at representing the value that *pMem describes as an integer.
74198** If pMem is an integer, then the value is exact. If pMem is
74199** a floating-point then the value returned is the integer part.
74200** If pMem is a string or blob, then we make an attempt to convert
74201** it into an integer and return that. If pMem represents an
74202** an SQL-NULL value, return 0.
74203**
74204** If pMem represents a string value, its encoding might be changed.
74205*/
74206static SQLITE_NOINLINE i64 memIntValue(Mem *pMem){
74207 i64 value = 0;
74208 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
74209 return value;
74210}
74211SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
74212 int flags;
74213 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74214 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
74215 flags = pMem->flags;
74216 if( flags & MEM_Int ){
74217 return pMem->u.i;
74218 }else if( flags & MEM_Real ){
74219 return doubleToInt64(pMem->u.r);
74220 }else if( flags & (MEM_Str|MEM_Blob) ){
74221 assert( pMem->z || pMem->n==0 );
74222 return memIntValue(pMem);
74223 }else{
74224 return 0;
74225 }
74226}
74227
74228/*
74229** Return the best representation of pMem that we can get into a
74230** double. If pMem is already a double or an integer, return its
74231** value. If it is a string or blob, try to convert it to a double.
74232** If it is a NULL, return 0.0.
74233*/
74234static SQLITE_NOINLINE double memRealValue(Mem *pMem){
74235 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
74236 double val = (double)0;
74237 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
74238 return val;
74239}
74240SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
74241 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74242 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
74243 if( pMem->flags & MEM_Real ){
74244 return pMem->u.r;
74245 }else if( pMem->flags & MEM_Int ){
74246 return (double)pMem->u.i;
74247 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
74248 return memRealValue(pMem);
74249 }else{
74250 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
74251 return (double)0;
74252 }
74253}
74254
74255/*
74256** Return 1 if pMem represents true, and return 0 if pMem represents false.
74257** Return the value ifNull if pMem is NULL.
74258*/
74259SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
74260 if( pMem->flags & MEM_Int ) return pMem->u.i!=0;
74261 if( pMem->flags & MEM_Null ) return ifNull;
74262 return sqlite3VdbeRealValue(pMem)!=0.0;
74263}
74264
74265/*
74266** The MEM structure is already a MEM_Real. Try to also make it a
74267** MEM_Int if we can.
74268*/
74269SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
74270 i64 ix;
74271 assert( pMem->flags & MEM_Real );
74272 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74273 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74274 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
74275
74276 ix = doubleToInt64(pMem->u.r);
74277
74278 /* Only mark the value as an integer if
74279 **
74280 ** (1) the round-trip conversion real->int->real is a no-op, and
74281 ** (2) The integer is neither the largest nor the smallest
74282 ** possible integer (ticket #3922)
74283 **
74284 ** The second and third terms in the following conditional enforces
74285 ** the second condition under the assumption that addition overflow causes
74286 ** values to wrap around.
74287 */
74288 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
74289 pMem->u.i = ix;
74290 MemSetTypeFlag(pMem, MEM_Int);
74291 }
74292}
74293
74294/*
74295** Convert pMem to type integer. Invalidate any prior representations.
74296*/
74297SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
74298 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74299 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74300 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
74301
74302 pMem->u.i = sqlite3VdbeIntValue(pMem);
74303 MemSetTypeFlag(pMem, MEM_Int);
74304 return SQLITE_OK;
74305}
74306
74307/*
74308** Convert pMem so that it is of type MEM_Real.
74309** Invalidate any prior representations.
74310*/
74311SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
74312 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74313 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
74314
74315 pMem->u.r = sqlite3VdbeRealValue(pMem);
74316 MemSetTypeFlag(pMem, MEM_Real);
74317 return SQLITE_OK;
74318}
74319
74320/* Compare a floating point value to an integer. Return true if the two
74321** values are the same within the precision of the floating point value.
74322**
74323** For some versions of GCC on 32-bit machines, if you do the more obvious
74324** comparison of "r1==(double)i" you sometimes get an answer of false even
74325** though the r1 and (double)i values are bit-for-bit the same.
74326*/
74327static int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
74328 double r2 = (double)i;
74329 return memcmp(&r1, &r2, sizeof(r1))==0;
74330}
74331
74332/*
74333** Convert pMem so that it has types MEM_Real or MEM_Int or both.
74334** Invalidate any prior representations.
74335**
74336** Every effort is made to force the conversion, even if the input
74337** is a string that does not look completely like a number. Convert
74338** as much of the string as we can and ignore the rest.
74339*/
74340SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
74341 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
74342 int rc;
74343 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
74344 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74345 rc = sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc);
74346 if( rc==0 ){
74347 MemSetTypeFlag(pMem, MEM_Int);
74348 }else{
74349 i64 i = pMem->u.i;
74350 sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
74351 if( rc==1 && sqlite3RealSameAsInt(pMem->u.r, i) ){
74352 pMem->u.i = i;
74353 MemSetTypeFlag(pMem, MEM_Int);
74354 }else{
74355 MemSetTypeFlag(pMem, MEM_Real);
74356 }
74357 }
74358 }
74359 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
74360 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
74361 return SQLITE_OK;
74362}
74363
74364/*
74365** Cast the datatype of the value in pMem according to the affinity
74366** "aff". Casting is different from applying affinity in that a cast
74367** is forced. In other words, the value is converted into the desired
74368** affinity even if that results in loss of data. This routine is
74369** used (for example) to implement the SQL "cast()" operator.
74370*/
74371SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
74372 if( pMem->flags & MEM_Null ) return;
74373 switch( aff ){
74374 case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */
74375 if( (pMem->flags & MEM_Blob)==0 ){
74376 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
74377 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
74378 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
74379 }else{
74380 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
74381 }
74382 break;
74383 }
74384 case SQLITE_AFF_NUMERIC: {
74385 sqlite3VdbeMemNumerify(pMem);
74386 break;
74387 }
74388 case SQLITE_AFF_INTEGER: {
74389 sqlite3VdbeMemIntegerify(pMem);
74390 break;
74391 }
74392 case SQLITE_AFF_REAL: {
74393 sqlite3VdbeMemRealify(pMem);
74394 break;
74395 }
74396 default: {
74397 assert( aff==SQLITE_AFF_TEXT );
74398 assert( MEM_Str==(MEM_Blob>>3) );
74399 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
74400 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
74401 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
74402 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
74403 break;
74404 }
74405 }
74406}
74407
74408/*
74409** Initialize bulk memory to be a consistent Mem object.
74410**
74411** The minimum amount of initialization feasible is performed.
74412*/
74413SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
74414 assert( (flags & ~MEM_TypeMask)==0 );
74415 pMem->flags = flags;
74416 pMem->db = db;
74417 pMem->szMalloc = 0;
74418}
74419
74420
74421/*
74422** Delete any previous value and set the value stored in *pMem to NULL.
74423**
74424** This routine calls the Mem.xDel destructor to dispose of values that
74425** require the destructor. But it preserves the Mem.zMalloc memory allocation.
74426** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
74427** routine to invoke the destructor and deallocates Mem.zMalloc.
74428**
74429** Use this routine to reset the Mem prior to insert a new value.
74430**
74431** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
74432*/
74433SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
74434 if( VdbeMemDynamic(pMem) ){
74435 vdbeMemClearExternAndSetNull(pMem);
74436 }else{
74437 pMem->flags = MEM_Null;
74438 }
74439}
74440SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){
74441 sqlite3VdbeMemSetNull((Mem*)p);
74442}
74443
74444/*
74445** Delete any previous value and set the value to be a BLOB of length
74446** n containing all zeros.
74447*/
74448SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
74449 sqlite3VdbeMemRelease(pMem);
74450 pMem->flags = MEM_Blob|MEM_Zero;
74451 pMem->n = 0;
74452 if( n<0 ) n = 0;
74453 pMem->u.nZero = n;
74454 pMem->enc = SQLITE_UTF8;
74455 pMem->z = 0;
74456}
74457
74458/*
74459** The pMem is known to contain content that needs to be destroyed prior
74460** to a value change. So invoke the destructor, then set the value to
74461** a 64-bit integer.
74462*/
74463static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
74464 sqlite3VdbeMemSetNull(pMem);
74465 pMem->u.i = val;
74466 pMem->flags = MEM_Int;
74467}
74468
74469/*
74470** Delete any previous value and set the value stored in *pMem to val,
74471** manifest type INTEGER.
74472*/
74473SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
74474 if( VdbeMemDynamic(pMem) ){
74475 vdbeReleaseAndSetInt64(pMem, val);
74476 }else{
74477 pMem->u.i = val;
74478 pMem->flags = MEM_Int;
74479 }
74480}
74481
74482/* A no-op destructor */
74483SQLITE_PRIVATE void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
74484
74485/*
74486** Set the value stored in *pMem should already be a NULL.
74487** Also store a pointer to go with it.
74488*/
74489SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(
74490 Mem *pMem,
74491 void *pPtr,
74492 const char *zPType,
74493 void (*xDestructor)(void*)
74494){
74495 assert( pMem->flags==MEM_Null );
74496 pMem->u.zPType = zPType ? zPType : "";
74497 pMem->z = pPtr;
74498 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
74499 pMem->eSubtype = 'p';
74500 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
74501}
74502
74503#ifndef SQLITE_OMIT_FLOATING_POINT
74504/*
74505** Delete any previous value and set the value stored in *pMem to val,
74506** manifest type REAL.
74507*/
74508SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
74509 sqlite3VdbeMemSetNull(pMem);
74510 if( !sqlite3IsNaN(val) ){
74511 pMem->u.r = val;
74512 pMem->flags = MEM_Real;
74513 }
74514}
74515#endif
74516
74517#ifdef SQLITE_DEBUG
74518/*
74519** Return true if the Mem holds a RowSet object. This routine is intended
74520** for use inside of assert() statements.
74521*/
74522SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem *pMem){
74523 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
74524 && pMem->xDel==sqlite3RowSetDelete;
74525}
74526#endif
74527
74528/*
74529** Delete any previous value and set the value of pMem to be an
74530** empty boolean index.
74531**
74532** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation
74533** error occurs.
74534*/
74535SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem *pMem){
74536 sqlite3 *db = pMem->db;
74537 RowSet *p;
74538 assert( db!=0 );
74539 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74540 sqlite3VdbeMemRelease(pMem);
74541 p = sqlite3RowSetInit(db);
74542 if( p==0 ) return SQLITE_NOMEM;
74543 pMem->z = (char*)p;
74544 pMem->flags = MEM_Blob|MEM_Dyn;
74545 pMem->xDel = sqlite3RowSetDelete;
74546 return SQLITE_OK;
74547}
74548
74549/*
74550** Return true if the Mem object contains a TEXT or BLOB that is
74551** too large - whose size exceeds SQLITE_MAX_LENGTH.
74552*/
74553SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
74554 assert( p->db!=0 );
74555 if( p->flags & (MEM_Str|MEM_Blob) ){
74556 int n = p->n;
74557 if( p->flags & MEM_Zero ){
74558 n += p->u.nZero;
74559 }
74560 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
74561 }
74562 return 0;
74563}
74564
74565#ifdef SQLITE_DEBUG
74566/*
74567** This routine prepares a memory cell for modification by breaking
74568** its link to a shallow copy and by marking any current shallow
74569** copies of this cell as invalid.
74570**
74571** This is used for testing and debugging only - to make sure shallow
74572** copies are not misused.
74573*/
74574SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
74575 int i;
74576 Mem *pX;
74577 for(i=0, pX=pVdbe->aMem; i<pVdbe->nMem; i++, pX++){
74578 if( pX->pScopyFrom==pMem ){
74579 /* If pX is marked as a shallow copy of pMem, then verify that
74580 ** no significant changes have been made to pX since the OP_SCopy.
74581 ** A significant change would indicated a missed call to this
74582 ** function for pX. Minor changes, such as adding or removing a
74583 ** dual type, are allowed, as long as the underlying value is the
74584 ** same. */
74585 u16 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
74586 assert( (mFlags&MEM_Int)==0 || pMem->u.i==pX->u.i );
74587 assert( (mFlags&MEM_Real)==0 || pMem->u.r==pX->u.r );
74588 assert( (mFlags&MEM_Str)==0 || (pMem->n==pX->n && pMem->z==pX->z) );
74589 assert( (mFlags&MEM_Blob)==0 || sqlite3BlobCompare(pMem,pX)==0 );
74590
74591 /* pMem is the register that is changing. But also mark pX as
74592 ** undefined so that we can quickly detect the shallow-copy error */
74593 pX->flags = MEM_Undefined;
74594 pX->pScopyFrom = 0;
74595 }
74596 }
74597 pMem->pScopyFrom = 0;
74598}
74599#endif /* SQLITE_DEBUG */
74600
74601
74602/*
74603** Make an shallow copy of pFrom into pTo. Prior contents of
74604** pTo are freed. The pFrom->z field is not duplicated. If
74605** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
74606** and flags gets srcType (either MEM_Ephem or MEM_Static).
74607*/
74608static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
74609 vdbeMemClearExternAndSetNull(pTo);
74610 assert( !VdbeMemDynamic(pTo) );
74611 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
74612}
74613SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
74614 assert( !sqlite3VdbeMemIsRowSet(pFrom) );
74615 assert( pTo->db==pFrom->db );
74616 if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
74617 memcpy(pTo, pFrom, MEMCELLSIZE);
74618 if( (pFrom->flags&MEM_Static)==0 ){
74619 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
74620 assert( srcType==MEM_Ephem || srcType==MEM_Static );
74621 pTo->flags |= srcType;
74622 }
74623}
74624
74625/*
74626** Make a full copy of pFrom into pTo. Prior contents of pTo are
74627** freed before the copy is made.
74628*/
74629SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
74630 int rc = SQLITE_OK;
74631
74632 assert( !sqlite3VdbeMemIsRowSet(pFrom) );
74633 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
74634 memcpy(pTo, pFrom, MEMCELLSIZE);
74635 pTo->flags &= ~MEM_Dyn;
74636 if( pTo->flags&(MEM_Str|MEM_Blob) ){
74637 if( 0==(pFrom->flags&MEM_Static) ){
74638 pTo->flags |= MEM_Ephem;
74639 rc = sqlite3VdbeMemMakeWriteable(pTo);
74640 }
74641 }
74642
74643 return rc;
74644}
74645
74646/*
74647** Transfer the contents of pFrom to pTo. Any existing value in pTo is
74648** freed. If pFrom contains ephemeral data, a copy is made.
74649**
74650** pFrom contains an SQL NULL when this routine returns.
74651*/
74652SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
74653 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
74654 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
74655 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
74656
74657 sqlite3VdbeMemRelease(pTo);
74658 memcpy(pTo, pFrom, sizeof(Mem));
74659 pFrom->flags = MEM_Null;
74660 pFrom->szMalloc = 0;
74661}
74662
74663/*
74664** Change the value of a Mem to be a string or a BLOB.
74665**
74666** The memory management strategy depends on the value of the xDel
74667** parameter. If the value passed is SQLITE_TRANSIENT, then the
74668** string is copied into a (possibly existing) buffer managed by the
74669** Mem structure. Otherwise, any existing buffer is freed and the
74670** pointer copied.
74671**
74672** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
74673** size limit) then no memory allocation occurs. If the string can be
74674** stored without allocating memory, then it is. If a memory allocation
74675** is required to store the string, then value of pMem is unchanged. In
74676** either case, SQLITE_TOOBIG is returned.
74677*/
74678SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
74679 Mem *pMem, /* Memory cell to set to string value */
74680 const char *z, /* String pointer */
74681 int n, /* Bytes in string, or negative */
74682 u8 enc, /* Encoding of z. 0 for BLOBs */
74683 void (*xDel)(void*) /* Destructor function */
74684){
74685 int nByte = n; /* New value for pMem->n */
74686 int iLimit; /* Maximum allowed string or blob size */
74687 u16 flags = 0; /* New value for pMem->flags */
74688
74689 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
74690 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74691
74692 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
74693 if( !z ){
74694 sqlite3VdbeMemSetNull(pMem);
74695 return SQLITE_OK;
74696 }
74697
74698 if( pMem->db ){
74699 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
74700 }else{
74701 iLimit = SQLITE_MAX_LENGTH;
74702 }
74703 flags = (enc==0?MEM_Blob:MEM_Str);
74704 if( nByte<0 ){
74705 assert( enc!=0 );
74706 if( enc==SQLITE_UTF8 ){
74707 nByte = 0x7fffffff & (int)strlen(z);
74708 if( nByte>iLimit ) nByte = iLimit+1;
74709 }else{
74710 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
74711 }
74712 flags |= MEM_Term;
74713 }
74714
74715 /* The following block sets the new values of Mem.z and Mem.xDel. It
74716 ** also sets a flag in local variable "flags" to indicate the memory
74717 ** management (one of MEM_Dyn or MEM_Static).
74718 */
74719 if( xDel==SQLITE_TRANSIENT ){
74720 int nAlloc = nByte;
74721 if( flags&MEM_Term ){
74722 nAlloc += (enc==SQLITE_UTF8?1:2);
74723 }
74724 if( nByte>iLimit ){
74725 return SQLITE_TOOBIG;
74726 }
74727 testcase( nAlloc==0 );
74728 testcase( nAlloc==31 );
74729 testcase( nAlloc==32 );
74730 if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
74731 return SQLITE_NOMEM_BKPT;
74732 }
74733 memcpy(pMem->z, z, nAlloc);
74734 }else if( xDel==SQLITE_DYNAMIC ){
74735 sqlite3VdbeMemRelease(pMem);
74736 pMem->zMalloc = pMem->z = (char *)z;
74737 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
74738 }else{
74739 sqlite3VdbeMemRelease(pMem);
74740 pMem->z = (char *)z;
74741 pMem->xDel = xDel;
74742 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
74743 }
74744
74745 pMem->n = nByte;
74746 pMem->flags = flags;
74747 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
74748
74749#ifndef SQLITE_OMIT_UTF16
74750 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
74751 return SQLITE_NOMEM_BKPT;
74752 }
74753#endif
74754
74755 if( nByte>iLimit ){
74756 return SQLITE_TOOBIG;
74757 }
74758
74759 return SQLITE_OK;
74760}
74761
74762/*
74763** Move data out of a btree key or data field and into a Mem structure.
74764** The data is payload from the entry that pCur is currently pointing
74765** to. offset and amt determine what portion of the data or key to retrieve.
74766** The result is written into the pMem element.
74767**
74768** The pMem object must have been initialized. This routine will use
74769** pMem->zMalloc to hold the content from the btree, if possible. New
74770** pMem->zMalloc space will be allocated if necessary. The calling routine
74771** is responsible for making sure that the pMem object is eventually
74772** destroyed.
74773**
74774** If this routine fails for any reason (malloc returns NULL or unable
74775** to read from the disk) then the pMem is left in an inconsistent state.
74776*/
74777static SQLITE_NOINLINE int vdbeMemFromBtreeResize(
74778 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
74779 u32 offset, /* Offset from the start of data to return bytes from. */
74780 u32 amt, /* Number of bytes to return. */
74781 Mem *pMem /* OUT: Return data in this Mem structure. */
74782){
74783 int rc;
74784 pMem->flags = MEM_Null;
74785 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
74786 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
74787 if( rc==SQLITE_OK ){
74788 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
74789 pMem->flags = MEM_Blob;
74790 pMem->n = (int)amt;
74791 }else{
74792 sqlite3VdbeMemRelease(pMem);
74793 }
74794 }
74795 return rc;
74796}
74797SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
74798 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
74799 u32 offset, /* Offset from the start of data to return bytes from. */
74800 u32 amt, /* Number of bytes to return. */
74801 Mem *pMem /* OUT: Return data in this Mem structure. */
74802){
74803 char *zData; /* Data from the btree layer */
74804 u32 available = 0; /* Number of bytes available on the local btree page */
74805 int rc = SQLITE_OK; /* Return code */
74806
74807 assert( sqlite3BtreeCursorIsValid(pCur) );
74808 assert( !VdbeMemDynamic(pMem) );
74809
74810 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
74811 ** that both the BtShared and database handle mutexes are held. */
74812 assert( !sqlite3VdbeMemIsRowSet(pMem) );
74813 zData = (char *)sqlite3BtreePayloadFetch(pCur, &available);
74814 assert( zData!=0 );
74815
74816 if( offset+amt<=available ){
74817 pMem->z = &zData[offset];
74818 pMem->flags = MEM_Blob|MEM_Ephem;
74819 pMem->n = (int)amt;
74820 }else{
74821 rc = vdbeMemFromBtreeResize(pCur, offset, amt, pMem);
74822 }
74823
74824 return rc;
74825}
74826
74827/*
74828** The pVal argument is known to be a value other than NULL.
74829** Convert it into a string with encoding enc and return a pointer
74830** to a zero-terminated version of that string.
74831*/
74832static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
74833 assert( pVal!=0 );
74834 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
74835 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
74836 assert( !sqlite3VdbeMemIsRowSet(pVal) );
74837 assert( (pVal->flags & (MEM_Null))==0 );
74838 if( pVal->flags & (MEM_Blob|MEM_Str) ){
74839 if( ExpandBlob(pVal) ) return 0;
74840 pVal->flags |= MEM_Str;
74841 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
74842 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
74843 }
74844 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
74845 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
74846 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
74847 return 0;
74848 }
74849 }
74850 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
74851 }else{
74852 sqlite3VdbeMemStringify(pVal, enc, 0);
74853 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
74854 }
74855 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
74856 || pVal->db->mallocFailed );
74857 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
74858 assert( sqlite3VdbeMemConsistentDualRep(pVal) );
74859 return pVal->z;
74860 }else{
74861 return 0;
74862 }
74863}
74864
74865/* This function is only available internally, it is not part of the
74866** external API. It works in a similar way to sqlite3_value_text(),
74867** except the data returned is in the encoding specified by the second
74868** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
74869** SQLITE_UTF8.
74870**
74871** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
74872** If that is the case, then the result must be aligned on an even byte
74873** boundary.
74874*/
74875SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
74876 if( !pVal ) return 0;
74877 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
74878 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
74879 assert( !sqlite3VdbeMemIsRowSet(pVal) );
74880 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
74881 assert( sqlite3VdbeMemConsistentDualRep(pVal) );
74882 return pVal->z;
74883 }
74884 if( pVal->flags&MEM_Null ){
74885 return 0;
74886 }
74887 return valueToText(pVal, enc);
74888}
74889
74890/*
74891** Create a new sqlite3_value object.
74892*/
74893SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
74894 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
74895 if( p ){
74896 p->flags = MEM_Null;
74897 p->db = db;
74898 }
74899 return p;
74900}
74901
74902/*
74903** Context object passed by sqlite3Stat4ProbeSetValue() through to
74904** valueNew(). See comments above valueNew() for details.
74905*/
74906struct ValueNewStat4Ctx {
74907 Parse *pParse;
74908 Index *pIdx;
74909 UnpackedRecord **ppRec;
74910 int iVal;
74911};
74912
74913/*
74914** Allocate and return a pointer to a new sqlite3_value object. If
74915** the second argument to this function is NULL, the object is allocated
74916** by calling sqlite3ValueNew().
74917**
74918** Otherwise, if the second argument is non-zero, then this function is
74919** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
74920** already been allocated, allocate the UnpackedRecord structure that
74921** that function will return to its caller here. Then return a pointer to
74922** an sqlite3_value within the UnpackedRecord.a[] array.
74923*/
74924static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
74925#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
74926 if( p ){
74927 UnpackedRecord *pRec = p->ppRec[0];
74928
74929 if( pRec==0 ){
74930 Index *pIdx = p->pIdx; /* Index being probed */
74931 int nByte; /* Bytes of space to allocate */
74932 int i; /* Counter variable */
74933 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
74934
74935 nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
74936 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
74937 if( pRec ){
74938 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
74939 if( pRec->pKeyInfo ){
74940 assert( pRec->pKeyInfo->nAllField==nCol );
74941 assert( pRec->pKeyInfo->enc==ENC(db) );
74942 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
74943 for(i=0; i<nCol; i++){
74944 pRec->aMem[i].flags = MEM_Null;
74945 pRec->aMem[i].db = db;
74946 }
74947 }else{
74948 sqlite3DbFreeNN(db, pRec);
74949 pRec = 0;
74950 }
74951 }
74952 if( pRec==0 ) return 0;
74953 p->ppRec[0] = pRec;
74954 }
74955
74956 pRec->nField = p->iVal+1;
74957 return &pRec->aMem[p->iVal];
74958 }
74959#else
74960 UNUSED_PARAMETER(p);
74961#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
74962 return sqlite3ValueNew(db);
74963}
74964
74965/*
74966** The expression object indicated by the second argument is guaranteed
74967** to be a scalar SQL function. If
74968**
74969** * all function arguments are SQL literals,
74970** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
74971** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
74972**
74973** then this routine attempts to invoke the SQL function. Assuming no
74974** error occurs, output parameter (*ppVal) is set to point to a value
74975** object containing the result before returning SQLITE_OK.
74976**
74977** Affinity aff is applied to the result of the function before returning.
74978** If the result is a text value, the sqlite3_value object uses encoding
74979** enc.
74980**
74981** If the conditions above are not met, this function returns SQLITE_OK
74982** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
74983** NULL and an SQLite error code returned.
74984*/
74985#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
74986static int valueFromFunction(
74987 sqlite3 *db, /* The database connection */
74988 Expr *p, /* The expression to evaluate */
74989 u8 enc, /* Encoding to use */
74990 u8 aff, /* Affinity to use */
74991 sqlite3_value **ppVal, /* Write the new value here */
74992 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
74993){
74994 sqlite3_context ctx; /* Context object for function invocation */
74995 sqlite3_value **apVal = 0; /* Function arguments */
74996 int nVal = 0; /* Size of apVal[] array */
74997 FuncDef *pFunc = 0; /* Function definition */
74998 sqlite3_value *pVal = 0; /* New value */
74999 int rc = SQLITE_OK; /* Return code */
75000 ExprList *pList = 0; /* Function arguments */
75001 int i; /* Iterator variable */
75002
75003 assert( pCtx!=0 );
75004 assert( (p->flags & EP_TokenOnly)==0 );
75005 pList = p->x.pList;
75006 if( pList ) nVal = pList->nExpr;
75007 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
75008 assert( pFunc );
75009 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
75010 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
75011 ){
75012 return SQLITE_OK;
75013 }
75014
75015 if( pList ){
75016 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
75017 if( apVal==0 ){
75018 rc = SQLITE_NOMEM_BKPT;
75019 goto value_from_function_out;
75020 }
75021 for(i=0; i<nVal; i++){
75022 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
75023 if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out;
75024 }
75025 }
75026
75027 pVal = valueNew(db, pCtx);
75028 if( pVal==0 ){
75029 rc = SQLITE_NOMEM_BKPT;
75030 goto value_from_function_out;
75031 }
75032
75033 assert( pCtx->pParse->rc==SQLITE_OK );
75034 memset(&ctx, 0, sizeof(ctx));
75035 ctx.pOut = pVal;
75036 ctx.pFunc = pFunc;
75037 pFunc->xSFunc(&ctx, nVal, apVal);
75038 if( ctx.isError ){
75039 rc = ctx.isError;
75040 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
75041 }else{
75042 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
75043 assert( rc==SQLITE_OK );
75044 rc = sqlite3VdbeChangeEncoding(pVal, enc);
75045 if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){
75046 rc = SQLITE_TOOBIG;
75047 pCtx->pParse->nErr++;
75048 }
75049 }
75050 pCtx->pParse->rc = rc;
75051
75052 value_from_function_out:
75053 if( rc!=SQLITE_OK ){
75054 pVal = 0;
75055 }
75056 if( apVal ){
75057 for(i=0; i<nVal; i++){
75058 sqlite3ValueFree(apVal[i]);
75059 }
75060 sqlite3DbFreeNN(db, apVal);
75061 }
75062
75063 *ppVal = pVal;
75064 return rc;
75065}
75066#else
75067# define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
75068#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
75069
75070/*
75071** Extract a value from the supplied expression in the manner described
75072** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
75073** using valueNew().
75074**
75075** If pCtx is NULL and an error occurs after the sqlite3_value object
75076** has been allocated, it is freed before returning. Or, if pCtx is not
75077** NULL, it is assumed that the caller will free any allocated object
75078** in all cases.
75079*/
75080static int valueFromExpr(
75081 sqlite3 *db, /* The database connection */
75082 Expr *pExpr, /* The expression to evaluate */
75083 u8 enc, /* Encoding to use */
75084 u8 affinity, /* Affinity to use */
75085 sqlite3_value **ppVal, /* Write the new value here */
75086 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
75087){
75088 int op;
75089 char *zVal = 0;
75090 sqlite3_value *pVal = 0;
75091 int negInt = 1;
75092 const char *zNeg = "";
75093 int rc = SQLITE_OK;
75094
75095 assert( pExpr!=0 );
75096 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
75097#if defined(SQLITE_ENABLE_STAT3_OR_STAT4)
75098 if( op==TK_REGISTER ) op = pExpr->op2;
75099#else
75100 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
75101#endif
75102
75103 /* Compressed expressions only appear when parsing the DEFAULT clause
75104 ** on a table column definition, and hence only when pCtx==0. This
75105 ** check ensures that an EP_TokenOnly expression is never passed down
75106 ** into valueFromFunction(). */
75107 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
75108
75109 if( op==TK_CAST ){
75110 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
75111 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
75112 testcase( rc!=SQLITE_OK );
75113 if( *ppVal ){
75114 sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8);
75115 sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8);
75116 }
75117 return rc;
75118 }
75119
75120 /* Handle negative integers in a single step. This is needed in the
75121 ** case when the value is -9223372036854775808.
75122 */
75123 if( op==TK_UMINUS
75124 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
75125 pExpr = pExpr->pLeft;
75126 op = pExpr->op;
75127 negInt = -1;
75128 zNeg = "-";
75129 }
75130
75131 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
75132 pVal = valueNew(db, pCtx);
75133 if( pVal==0 ) goto no_mem;
75134 if( ExprHasProperty(pExpr, EP_IntValue) ){
75135 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
75136 }else{
75137 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
75138 if( zVal==0 ) goto no_mem;
75139 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
75140 }
75141 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
75142 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
75143 }else{
75144 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
75145 }
75146 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
75147 if( enc!=SQLITE_UTF8 ){
75148 rc = sqlite3VdbeChangeEncoding(pVal, enc);
75149 }
75150 }else if( op==TK_UMINUS ) {
75151 /* This branch happens for multiple negative signs. Ex: -(-5) */
75152 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
75153 && pVal!=0
75154 ){
75155 sqlite3VdbeMemNumerify(pVal);
75156 if( pVal->flags & MEM_Real ){
75157 pVal->u.r = -pVal->u.r;
75158 }else if( pVal->u.i==SMALLEST_INT64 ){
75159 pVal->u.r = -(double)SMALLEST_INT64;
75160 MemSetTypeFlag(pVal, MEM_Real);
75161 }else{
75162 pVal->u.i = -pVal->u.i;
75163 }
75164 sqlite3ValueApplyAffinity(pVal, affinity, enc);
75165 }
75166 }else if( op==TK_NULL ){
75167 pVal = valueNew(db, pCtx);
75168 if( pVal==0 ) goto no_mem;
75169 sqlite3VdbeMemNumerify(pVal);
75170 }
75171#ifndef SQLITE_OMIT_BLOB_LITERAL
75172 else if( op==TK_BLOB ){
75173 int nVal;
75174 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
75175 assert( pExpr->u.zToken[1]=='\'' );
75176 pVal = valueNew(db, pCtx);
75177 if( !pVal ) goto no_mem;
75178 zVal = &pExpr->u.zToken[2];
75179 nVal = sqlite3Strlen30(zVal)-1;
75180 assert( zVal[nVal]=='\'' );
75181 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
75182 0, SQLITE_DYNAMIC);
75183 }
75184#endif
75185#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
75186 else if( op==TK_FUNCTION && pCtx!=0 ){
75187 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
75188 }
75189#endif
75190 else if( op==TK_TRUEFALSE ){
75191 pVal = valueNew(db, pCtx);
75192 pVal->flags = MEM_Int;
75193 pVal->u.i = pExpr->u.zToken[4]==0;
75194 }
75195
75196 *ppVal = pVal;
75197 return rc;
75198
75199no_mem:
75200#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
75201 if( pCtx==0 || pCtx->pParse->nErr==0 )
75202#endif
75203 sqlite3OomFault(db);
75204 sqlite3DbFree(db, zVal);
75205 assert( *ppVal==0 );
75206#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
75207 if( pCtx==0 ) sqlite3ValueFree(pVal);
75208#else
75209 assert( pCtx==0 ); sqlite3ValueFree(pVal);
75210#endif
75211 return SQLITE_NOMEM_BKPT;
75212}
75213
75214/*
75215** Create a new sqlite3_value object, containing the value of pExpr.
75216**
75217** This only works for very simple expressions that consist of one constant
75218** token (i.e. "5", "5.1", "'a string'"). If the expression can
75219** be converted directly into a value, then the value is allocated and
75220** a pointer written to *ppVal. The caller is responsible for deallocating
75221** the value by passing it to sqlite3ValueFree() later on. If the expression
75222** cannot be converted to a value, then *ppVal is set to NULL.
75223*/
75224SQLITE_PRIVATE int sqlite3ValueFromExpr(
75225 sqlite3 *db, /* The database connection */
75226 Expr *pExpr, /* The expression to evaluate */
75227 u8 enc, /* Encoding to use */
75228 u8 affinity, /* Affinity to use */
75229 sqlite3_value **ppVal /* Write the new value here */
75230){
75231 return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0;
75232}
75233
75234#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
75235/*
75236** The implementation of the sqlite_record() function. This function accepts
75237** a single argument of any type. The return value is a formatted database
75238** record (a blob) containing the argument value.
75239**
75240** This is used to convert the value stored in the 'sample' column of the
75241** sqlite_stat3 table to the record format SQLite uses internally.
75242*/
75243static void recordFunc(
75244 sqlite3_context *context,
75245 int argc,
75246 sqlite3_value **argv
75247){
75248 const int file_format = 1;
75249 u32 iSerial; /* Serial type */
75250 int nSerial; /* Bytes of space for iSerial as varint */
75251 u32 nVal; /* Bytes of space required for argv[0] */
75252 int nRet;
75253 sqlite3 *db;
75254 u8 *aRet;
75255
75256 UNUSED_PARAMETER( argc );
75257 iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal);
75258 nSerial = sqlite3VarintLen(iSerial);
75259 db = sqlite3_context_db_handle(context);
75260
75261 nRet = 1 + nSerial + nVal;
75262 aRet = sqlite3DbMallocRawNN(db, nRet);
75263 if( aRet==0 ){
75264 sqlite3_result_error_nomem(context);
75265 }else{
75266 aRet[0] = nSerial+1;
75267 putVarint32(&aRet[1], iSerial);
75268 sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
75269 sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
75270 sqlite3DbFreeNN(db, aRet);
75271 }
75272}
75273
75274/*
75275** Register built-in functions used to help read ANALYZE data.
75276*/
75277SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){
75278 static FuncDef aAnalyzeTableFuncs[] = {
75279 FUNCTION(sqlite_record, 1, 0, 0, recordFunc),
75280 };
75281 sqlite3InsertBuiltinFuncs(aAnalyzeTableFuncs, ArraySize(aAnalyzeTableFuncs));
75282}
75283
75284/*
75285** Attempt to extract a value from pExpr and use it to construct *ppVal.
75286**
75287** If pAlloc is not NULL, then an UnpackedRecord object is created for
75288** pAlloc if one does not exist and the new value is added to the
75289** UnpackedRecord object.
75290**
75291** A value is extracted in the following cases:
75292**
75293** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
75294**
75295** * The expression is a bound variable, and this is a reprepare, or
75296**
75297** * The expression is a literal value.
75298**
75299** On success, *ppVal is made to point to the extracted value. The caller
75300** is responsible for ensuring that the value is eventually freed.
75301*/
75302static int stat4ValueFromExpr(
75303 Parse *pParse, /* Parse context */
75304 Expr *pExpr, /* The expression to extract a value from */
75305 u8 affinity, /* Affinity to use */
75306 struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
75307 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
75308){
75309 int rc = SQLITE_OK;
75310 sqlite3_value *pVal = 0;
75311 sqlite3 *db = pParse->db;
75312
75313 /* Skip over any TK_COLLATE nodes */
75314 pExpr = sqlite3ExprSkipCollate(pExpr);
75315
75316 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
75317 if( !pExpr ){
75318 pVal = valueNew(db, pAlloc);
75319 if( pVal ){
75320 sqlite3VdbeMemSetNull((Mem*)pVal);
75321 }
75322 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
75323 Vdbe *v;
75324 int iBindVar = pExpr->iColumn;
75325 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
75326 if( (v = pParse->pReprepare)!=0 ){
75327 pVal = valueNew(db, pAlloc);
75328 if( pVal ){
75329 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
75330 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
75331 pVal->db = pParse->db;
75332 }
75333 }
75334 }else{
75335 rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc);
75336 }
75337
75338 assert( pVal==0 || pVal->db==db );
75339 *ppVal = pVal;
75340 return rc;
75341}
75342
75343/*
75344** This function is used to allocate and populate UnpackedRecord
75345** structures intended to be compared against sample index keys stored
75346** in the sqlite_stat4 table.
75347**
75348** A single call to this function populates zero or more fields of the
75349** record starting with field iVal (fields are numbered from left to
75350** right starting with 0). A single field is populated if:
75351**
75352** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
75353**
75354** * The expression is a bound variable, and this is a reprepare, or
75355**
75356** * The sqlite3ValueFromExpr() function is able to extract a value
75357** from the expression (i.e. the expression is a literal value).
75358**
75359** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
75360** vector components that match either of the two latter criteria listed
75361** above.
75362**
75363** Before any value is appended to the record, the affinity of the
75364** corresponding column within index pIdx is applied to it. Before
75365** this function returns, output parameter *pnExtract is set to the
75366** number of values appended to the record.
75367**
75368** When this function is called, *ppRec must either point to an object
75369** allocated by an earlier call to this function, or must be NULL. If it
75370** is NULL and a value can be successfully extracted, a new UnpackedRecord
75371** is allocated (and *ppRec set to point to it) before returning.
75372**
75373** Unless an error is encountered, SQLITE_OK is returned. It is not an
75374** error if a value cannot be extracted from pExpr. If an error does
75375** occur, an SQLite error code is returned.
75376*/
75377SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
75378 Parse *pParse, /* Parse context */
75379 Index *pIdx, /* Index being probed */
75380 UnpackedRecord **ppRec, /* IN/OUT: Probe record */
75381 Expr *pExpr, /* The expression to extract a value from */
75382 int nElem, /* Maximum number of values to append */
75383 int iVal, /* Array element to populate */
75384 int *pnExtract /* OUT: Values appended to the record */
75385){
75386 int rc = SQLITE_OK;
75387 int nExtract = 0;
75388
75389 if( pExpr==0 || pExpr->op!=TK_SELECT ){
75390 int i;
75391 struct ValueNewStat4Ctx alloc;
75392
75393 alloc.pParse = pParse;
75394 alloc.pIdx = pIdx;
75395 alloc.ppRec = ppRec;
75396
75397 for(i=0; i<nElem; i++){
75398 sqlite3_value *pVal = 0;
75399 Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
75400 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
75401 alloc.iVal = iVal+i;
75402 rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
75403 if( !pVal ) break;
75404 nExtract++;
75405 }
75406 }
75407
75408 *pnExtract = nExtract;
75409 return rc;
75410}
75411
75412/*
75413** Attempt to extract a value from expression pExpr using the methods
75414** as described for sqlite3Stat4ProbeSetValue() above.
75415**
75416** If successful, set *ppVal to point to a new value object and return
75417** SQLITE_OK. If no value can be extracted, but no other error occurs
75418** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
75419** does occur, return an SQLite error code. The final value of *ppVal
75420** is undefined in this case.
75421*/
75422SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(
75423 Parse *pParse, /* Parse context */
75424 Expr *pExpr, /* The expression to extract a value from */
75425 u8 affinity, /* Affinity to use */
75426 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
75427){
75428 return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
75429}
75430
75431/*
75432** Extract the iCol-th column from the nRec-byte record in pRec. Write
75433** the column value into *ppVal. If *ppVal is initially NULL then a new
75434** sqlite3_value object is allocated.
75435**
75436** If *ppVal is initially NULL then the caller is responsible for
75437** ensuring that the value written into *ppVal is eventually freed.
75438*/
75439SQLITE_PRIVATE int sqlite3Stat4Column(
75440 sqlite3 *db, /* Database handle */
75441 const void *pRec, /* Pointer to buffer containing record */
75442 int nRec, /* Size of buffer pRec in bytes */
75443 int iCol, /* Column to extract */
75444 sqlite3_value **ppVal /* OUT: Extracted value */
75445){
75446 u32 t = 0; /* a column type code */
75447 int nHdr; /* Size of the header in the record */
75448 int iHdr; /* Next unread header byte */
75449 int iField; /* Next unread data byte */
75450 int szField = 0; /* Size of the current data field */
75451 int i; /* Column index */
75452 u8 *a = (u8*)pRec; /* Typecast byte array */
75453 Mem *pMem = *ppVal; /* Write result into this Mem object */
75454
75455 assert( iCol>0 );
75456 iHdr = getVarint32(a, nHdr);
75457 if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
75458 iField = nHdr;
75459 for(i=0; i<=iCol; i++){
75460 iHdr += getVarint32(&a[iHdr], t);
75461 testcase( iHdr==nHdr );
75462 testcase( iHdr==nHdr+1 );
75463 if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT;
75464 szField = sqlite3VdbeSerialTypeLen(t);
75465 iField += szField;
75466 }
75467 testcase( iField==nRec );
75468 testcase( iField==nRec+1 );
75469 if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
75470 if( pMem==0 ){
75471 pMem = *ppVal = sqlite3ValueNew(db);
75472 if( pMem==0 ) return SQLITE_NOMEM_BKPT;
75473 }
75474 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
75475 pMem->enc = ENC(db);
75476 return SQLITE_OK;
75477}
75478
75479/*
75480** Unless it is NULL, the argument must be an UnpackedRecord object returned
75481** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
75482** the object.
75483*/
75484SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
75485 if( pRec ){
75486 int i;
75487 int nCol = pRec->pKeyInfo->nAllField;
75488 Mem *aMem = pRec->aMem;
75489 sqlite3 *db = aMem[0].db;
75490 for(i=0; i<nCol; i++){
75491 sqlite3VdbeMemRelease(&aMem[i]);
75492 }
75493 sqlite3KeyInfoUnref(pRec->pKeyInfo);
75494 sqlite3DbFreeNN(db, pRec);
75495 }
75496}
75497#endif /* ifdef SQLITE_ENABLE_STAT4 */
75498
75499/*
75500** Change the string value of an sqlite3_value object
75501*/
75502SQLITE_PRIVATE void sqlite3ValueSetStr(
75503 sqlite3_value *v, /* Value to be set */
75504 int n, /* Length of string z */
75505 const void *z, /* Text of the new string */
75506 u8 enc, /* Encoding to use */
75507 void (*xDel)(void*) /* Destructor for the string */
75508){
75509 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
75510}
75511
75512/*
75513** Free an sqlite3_value object
75514*/
75515SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
75516 if( !v ) return;
75517 sqlite3VdbeMemRelease((Mem *)v);
75518 sqlite3DbFreeNN(((Mem*)v)->db, v);
75519}
75520
75521/*
75522** The sqlite3ValueBytes() routine returns the number of bytes in the
75523** sqlite3_value object assuming that it uses the encoding "enc".
75524** The valueBytes() routine is a helper function.
75525*/
75526static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
75527 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
75528}
75529SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
75530 Mem *p = (Mem*)pVal;
75531 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
75532 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
75533 return p->n;
75534 }
75535 if( (p->flags & MEM_Blob)!=0 ){
75536 if( p->flags & MEM_Zero ){
75537 return p->n + p->u.nZero;
75538 }else{
75539 return p->n;
75540 }
75541 }
75542 if( p->flags & MEM_Null ) return 0;
75543 return valueBytes(pVal, enc);
75544}
75545
75546/************** End of vdbemem.c *********************************************/
75547/************** Begin file vdbeaux.c *****************************************/
75548/*
75549** 2003 September 6
75550**
75551** The author disclaims copyright to this source code. In place of
75552** a legal notice, here is a blessing:
75553**
75554** May you do good and not evil.
75555** May you find forgiveness for yourself and forgive others.
75556** May you share freely, never taking more than you give.
75557**
75558*************************************************************************
75559** This file contains code used for creating, destroying, and populating
75560** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
75561*/
75562/* #include "sqliteInt.h" */
75563/* #include "vdbeInt.h" */
75564
75565/*
75566** Create a new virtual database engine.
75567*/
75568SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
75569 sqlite3 *db = pParse->db;
75570 Vdbe *p;
75571 p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
75572 if( p==0 ) return 0;
75573 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
75574 p->db = db;
75575 if( db->pVdbe ){
75576 db->pVdbe->pPrev = p;
75577 }
75578 p->pNext = db->pVdbe;
75579 p->pPrev = 0;
75580 db->pVdbe = p;
75581 p->magic = VDBE_MAGIC_INIT;
75582 p->pParse = pParse;
75583 pParse->pVdbe = p;
75584 assert( pParse->aLabel==0 );
75585 assert( pParse->nLabel==0 );
75586 assert( pParse->nOpAlloc==0 );
75587 assert( pParse->szOpAlloc==0 );
75588 sqlite3VdbeAddOp2(p, OP_Init, 0, 1);
75589 return p;
75590}
75591
75592/*
75593** Change the error string stored in Vdbe.zErrMsg
75594*/
75595SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
75596 va_list ap;
75597 sqlite3DbFree(p->db, p->zErrMsg);
75598 va_start(ap, zFormat);
75599 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
75600 va_end(ap);
75601}
75602
75603/*
75604** Remember the SQL string for a prepared statement.
75605*/
75606SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){
75607 if( p==0 ) return;
75608 p->prepFlags = prepFlags;
75609 if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
75610 p->expmask = 0;
75611 }
75612 assert( p->zSql==0 );
75613 p->zSql = sqlite3DbStrNDup(p->db, z, n);
75614}
75615
75616/*
75617** Swap all content between two VDBE structures.
75618*/
75619SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
75620 Vdbe tmp, *pTmp;
75621 char *zTmp;
75622 assert( pA->db==pB->db );
75623 tmp = *pA;
75624 *pA = *pB;
75625 *pB = tmp;
75626 pTmp = pA->pNext;
75627 pA->pNext = pB->pNext;
75628 pB->pNext = pTmp;
75629 pTmp = pA->pPrev;
75630 pA->pPrev = pB->pPrev;
75631 pB->pPrev = pTmp;
75632 zTmp = pA->zSql;
75633 pA->zSql = pB->zSql;
75634 pB->zSql = zTmp;
75635 pB->expmask = pA->expmask;
75636 pB->prepFlags = pA->prepFlags;
75637 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
75638 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
75639}
75640
75641/*
75642** Resize the Vdbe.aOp array so that it is at least nOp elements larger
75643** than its current size. nOp is guaranteed to be less than or equal
75644** to 1024/sizeof(Op).
75645**
75646** If an out-of-memory error occurs while resizing the array, return
75647** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain
75648** unchanged (this is so that any opcodes already allocated can be
75649** correctly deallocated along with the rest of the Vdbe).
75650*/
75651static int growOpArray(Vdbe *v, int nOp){
75652 VdbeOp *pNew;
75653 Parse *p = v->pParse;
75654
75655 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
75656 ** more frequent reallocs and hence provide more opportunities for
75657 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
75658 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
75659 ** by the minimum* amount required until the size reaches 512. Normal
75660 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
75661 ** size of the op array or add 1KB of space, whichever is smaller. */
75662#ifdef SQLITE_TEST_REALLOC_STRESS
75663 int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
75664#else
75665 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
75666 UNUSED_PARAMETER(nOp);
75667#endif
75668
75669 /* Ensure that the size of a VDBE does not grow too large */
75670 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
75671 sqlite3OomFault(p->db);
75672 return SQLITE_NOMEM;
75673 }
75674
75675 assert( nOp<=(1024/sizeof(Op)) );
75676 assert( nNew>=(p->nOpAlloc+nOp) );
75677 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
75678 if( pNew ){
75679 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
75680 p->nOpAlloc = p->szOpAlloc/sizeof(Op);
75681 v->aOp = pNew;
75682 }
75683 return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT);
75684}
75685
75686#ifdef SQLITE_DEBUG
75687/* This routine is just a convenient place to set a breakpoint that will
75688** fire after each opcode is inserted and displayed using
75689** "PRAGMA vdbe_addoptrace=on".
75690*/
75691static void test_addop_breakpoint(void){
75692 static int n = 0;
75693 n++;
75694}
75695#endif
75696
75697/*
75698** Add a new instruction to the list of instructions current in the
75699** VDBE. Return the address of the new instruction.
75700**
75701** Parameters:
75702**
75703** p Pointer to the VDBE
75704**
75705** op The opcode for this instruction
75706**
75707** p1, p2, p3 Operands
75708**
75709** Use the sqlite3VdbeResolveLabel() function to fix an address and
75710** the sqlite3VdbeChangeP4() function to change the value of the P4
75711** operand.
75712*/
75713static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
75714 assert( p->pParse->nOpAlloc<=p->nOp );
75715 if( growOpArray(p, 1) ) return 1;
75716 assert( p->pParse->nOpAlloc>p->nOp );
75717 return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
75718}
75719SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
75720 int i;
75721 VdbeOp *pOp;
75722
75723 i = p->nOp;
75724 assert( p->magic==VDBE_MAGIC_INIT );
75725 assert( op>=0 && op<0xff );
75726 if( p->pParse->nOpAlloc<=i ){
75727 return growOp3(p, op, p1, p2, p3);
75728 }
75729 p->nOp++;
75730 pOp = &p->aOp[i];
75731 pOp->opcode = (u8)op;
75732 pOp->p5 = 0;
75733 pOp->p1 = p1;
75734 pOp->p2 = p2;
75735 pOp->p3 = p3;
75736 pOp->p4.p = 0;
75737 pOp->p4type = P4_NOTUSED;
75738#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
75739 pOp->zComment = 0;
75740#endif
75741#ifdef SQLITE_DEBUG
75742 if( p->db->flags & SQLITE_VdbeAddopTrace ){
75743 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
75744 test_addop_breakpoint();
75745 }
75746#endif
75747#ifdef VDBE_PROFILE
75748 pOp->cycles = 0;
75749 pOp->cnt = 0;
75750#endif
75751#ifdef SQLITE_VDBE_COVERAGE
75752 pOp->iSrcLine = 0;
75753#endif
75754 return i;
75755}
75756SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
75757 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
75758}
75759SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
75760 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
75761}
75762SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
75763 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
75764}
75765
75766/* Generate code for an unconditional jump to instruction iDest
75767*/
75768SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe *p, int iDest){
75769 return sqlite3VdbeAddOp3(p, OP_Goto, 0, iDest, 0);
75770}
75771
75772/* Generate code to cause the string zStr to be loaded into
75773** register iDest
75774*/
75775SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
75776 return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zStr, 0);
75777}
75778
75779/*
75780** Generate code that initializes multiple registers to string or integer
75781** constants. The registers begin with iDest and increase consecutively.
75782** One register is initialized for each characgter in zTypes[]. For each
75783** "s" character in zTypes[], the register is a string if the argument is
75784** not NULL, or OP_Null if the value is a null pointer. For each "i" character
75785** in zTypes[], the register is initialized to an integer.
75786**
75787** If the input string does not end with "X" then an OP_ResultRow instruction
75788** is generated for the values inserted.
75789*/
75790SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
75791 va_list ap;
75792 int i;
75793 char c;
75794 va_start(ap, zTypes);
75795 for(i=0; (c = zTypes[i])!=0; i++){
75796 if( c=='s' ){
75797 const char *z = va_arg(ap, const char*);
75798 sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest+i, 0, z, 0);
75799 }else if( c=='i' ){
75800 sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest+i);
75801 }else{
75802 goto skip_op_resultrow;
75803 }
75804 }
75805 sqlite3VdbeAddOp2(p, OP_ResultRow, iDest, i);
75806skip_op_resultrow:
75807 va_end(ap);
75808}
75809
75810/*
75811** Add an opcode that includes the p4 value as a pointer.
75812*/
75813SQLITE_PRIVATE int sqlite3VdbeAddOp4(
75814 Vdbe *p, /* Add the opcode to this VM */
75815 int op, /* The new opcode */
75816 int p1, /* The P1 operand */
75817 int p2, /* The P2 operand */
75818 int p3, /* The P3 operand */
75819 const char *zP4, /* The P4 operand */
75820 int p4type /* P4 operand type */
75821){
75822 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
75823 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
75824 return addr;
75825}
75826
75827/*
75828** Add an opcode that includes the p4 value with a P4_INT64 or
75829** P4_REAL type.
75830*/
75831SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
75832 Vdbe *p, /* Add the opcode to this VM */
75833 int op, /* The new opcode */
75834 int p1, /* The P1 operand */
75835 int p2, /* The P2 operand */
75836 int p3, /* The P3 operand */
75837 const u8 *zP4, /* The P4 operand */
75838 int p4type /* P4 operand type */
75839){
75840 char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
75841 if( p4copy ) memcpy(p4copy, zP4, 8);
75842 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
75843}
75844
75845#ifndef SQLITE_OMIT_EXPLAIN
75846/*
75847** Return the address of the current EXPLAIN QUERY PLAN baseline.
75848** 0 means "none".
75849*/
75850SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse *pParse){
75851 VdbeOp *pOp;
75852 if( pParse->addrExplain==0 ) return 0;
75853 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
75854 return pOp->p2;
75855}
75856
75857/*
75858** Add a new OP_Explain opcode.
75859**
75860** If the bPush flag is true, then make this opcode the parent for
75861** subsequent Explains until sqlite3VdbeExplainPop() is called.
75862*/
75863SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
75864 if( pParse->explain==2 ){
75865 char *zMsg;
75866 Vdbe *v;
75867 va_list ap;
75868 int iThis;
75869 va_start(ap, zFmt);
75870 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
75871 va_end(ap);
75872 v = pParse->pVdbe;
75873 iThis = v->nOp;
75874 sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
75875 zMsg, P4_DYNAMIC);
75876 if( bPush) pParse->addrExplain = iThis;
75877 }
75878}
75879
75880/*
75881** Pop the EXPLAIN QUERY PLAN stack one level.
75882*/
75883SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse *pParse){
75884 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
75885}
75886#endif /* SQLITE_OMIT_EXPLAIN */
75887
75888/*
75889** Add an OP_ParseSchema opcode. This routine is broken out from
75890** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
75891** as having been used.
75892**
75893** The zWhere string must have been obtained from sqlite3_malloc().
75894** This routine will take ownership of the allocated memory.
75895*/
75896SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
75897 int j;
75898 sqlite3VdbeAddOp4(p, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
75899 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
75900}
75901
75902/*
75903** Add an opcode that includes the p4 value as an integer.
75904*/
75905SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
75906 Vdbe *p, /* Add the opcode to this VM */
75907 int op, /* The new opcode */
75908 int p1, /* The P1 operand */
75909 int p2, /* The P2 operand */
75910 int p3, /* The P3 operand */
75911 int p4 /* The P4 operand as an integer */
75912){
75913 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
75914 if( p->db->mallocFailed==0 ){
75915 VdbeOp *pOp = &p->aOp[addr];
75916 pOp->p4type = P4_INT32;
75917 pOp->p4.i = p4;
75918 }
75919 return addr;
75920}
75921
75922/* Insert the end of a co-routine
75923*/
75924SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
75925 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
75926
75927 /* Clear the temporary register cache, thereby ensuring that each
75928 ** co-routine has its own independent set of registers, because co-routines
75929 ** might expect their registers to be preserved across an OP_Yield, and
75930 ** that could cause problems if two or more co-routines are using the same
75931 ** temporary register.
75932 */
75933 v->pParse->nTempReg = 0;
75934 v->pParse->nRangeReg = 0;
75935}
75936
75937/*
75938** Create a new symbolic label for an instruction that has yet to be
75939** coded. The symbolic label is really just a negative number. The
75940** label can be used as the P2 value of an operation. Later, when
75941** the label is resolved to a specific address, the VDBE will scan
75942** through its operation list and change all values of P2 which match
75943** the label into the resolved address.
75944**
75945** The VDBE knows that a P2 value is a label because labels are
75946** always negative and P2 values are suppose to be non-negative.
75947** Hence, a negative P2 value is a label that has yet to be resolved.
75948**
75949** Zero is returned if a malloc() fails.
75950*/
75951SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *v){
75952 Parse *p = v->pParse;
75953 int i = p->nLabel++;
75954 assert( v->magic==VDBE_MAGIC_INIT );
75955 if( (i & (i-1))==0 ){
75956 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
75957 (i*2+1)*sizeof(p->aLabel[0]));
75958 }
75959 if( p->aLabel ){
75960 p->aLabel[i] = -1;
75961 }
75962 return ADDR(i);
75963}
75964
75965/*
75966** Resolve label "x" to be the address of the next instruction to
75967** be inserted. The parameter "x" must have been obtained from
75968** a prior call to sqlite3VdbeMakeLabel().
75969*/
75970SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
75971 Parse *p = v->pParse;
75972 int j = ADDR(x);
75973 assert( v->magic==VDBE_MAGIC_INIT );
75974 assert( j<p->nLabel );
75975 assert( j>=0 );
75976 if( p->aLabel ){
75977#ifdef SQLITE_DEBUG
75978 if( p->db->flags & SQLITE_VdbeAddopTrace ){
75979 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
75980 }
75981#endif
75982 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
75983 p->aLabel[j] = v->nOp;
75984 }
75985}
75986
75987/*
75988** Mark the VDBE as one that can only be run one time.
75989*/
75990SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
75991 p->runOnlyOnce = 1;
75992}
75993
75994/*
75995** Mark the VDBE as one that can only be run multiple times.
75996*/
75997SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe *p){
75998 p->runOnlyOnce = 0;
75999}
76000
76001#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
76002
76003/*
76004** The following type and function are used to iterate through all opcodes
76005** in a Vdbe main program and each of the sub-programs (triggers) it may
76006** invoke directly or indirectly. It should be used as follows:
76007**
76008** Op *pOp;
76009** VdbeOpIter sIter;
76010**
76011** memset(&sIter, 0, sizeof(sIter));
76012** sIter.v = v; // v is of type Vdbe*
76013** while( (pOp = opIterNext(&sIter)) ){
76014** // Do something with pOp
76015** }
76016** sqlite3DbFree(v->db, sIter.apSub);
76017**
76018*/
76019typedef struct VdbeOpIter VdbeOpIter;
76020struct VdbeOpIter {
76021 Vdbe *v; /* Vdbe to iterate through the opcodes of */
76022 SubProgram **apSub; /* Array of subprograms */
76023 int nSub; /* Number of entries in apSub */
76024 int iAddr; /* Address of next instruction to return */
76025 int iSub; /* 0 = main program, 1 = first sub-program etc. */
76026};
76027static Op *opIterNext(VdbeOpIter *p){
76028 Vdbe *v = p->v;
76029 Op *pRet = 0;
76030 Op *aOp;
76031 int nOp;
76032
76033 if( p->iSub<=p->nSub ){
76034
76035 if( p->iSub==0 ){
76036 aOp = v->aOp;
76037 nOp = v->nOp;
76038 }else{
76039 aOp = p->apSub[p->iSub-1]->aOp;
76040 nOp = p->apSub[p->iSub-1]->nOp;
76041 }
76042 assert( p->iAddr<nOp );
76043
76044 pRet = &aOp[p->iAddr];
76045 p->iAddr++;
76046 if( p->iAddr==nOp ){
76047 p->iSub++;
76048 p->iAddr = 0;
76049 }
76050
76051 if( pRet->p4type==P4_SUBPROGRAM ){
76052 int nByte = (p->nSub+1)*sizeof(SubProgram*);
76053 int j;
76054 for(j=0; j<p->nSub; j++){
76055 if( p->apSub[j]==pRet->p4.pProgram ) break;
76056 }
76057 if( j==p->nSub ){
76058 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
76059 if( !p->apSub ){
76060 pRet = 0;
76061 }else{
76062 p->apSub[p->nSub++] = pRet->p4.pProgram;
76063 }
76064 }
76065 }
76066 }
76067
76068 return pRet;
76069}
76070
76071/*
76072** Check if the program stored in the VM associated with pParse may
76073** throw an ABORT exception (causing the statement, but not entire transaction
76074** to be rolled back). This condition is true if the main program or any
76075** sub-programs contains any of the following:
76076**
76077** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
76078** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
76079** * OP_Destroy
76080** * OP_VUpdate
76081** * OP_VRename
76082** * OP_FkCounter with P2==0 (immediate foreign key constraint)
76083** * OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine
76084** (for CREATE TABLE AS SELECT ...)
76085**
76086** Then check that the value of Parse.mayAbort is true if an
76087** ABORT may be thrown, or false otherwise. Return true if it does
76088** match, or false otherwise. This function is intended to be used as
76089** part of an assert statement in the compiler. Similar to:
76090**
76091** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
76092*/
76093SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
76094 int hasAbort = 0;
76095 int hasFkCounter = 0;
76096 int hasCreateTable = 0;
76097 int hasInitCoroutine = 0;
76098 Op *pOp;
76099 VdbeOpIter sIter;
76100 memset(&sIter, 0, sizeof(sIter));
76101 sIter.v = v;
76102
76103 while( (pOp = opIterNext(&sIter))!=0 ){
76104 int opcode = pOp->opcode;
76105 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
76106 || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
76107 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
76108 ){
76109 hasAbort = 1;
76110 break;
76111 }
76112 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
76113 if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
76114#ifndef SQLITE_OMIT_FOREIGN_KEY
76115 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
76116 hasFkCounter = 1;
76117 }
76118#endif
76119 }
76120 sqlite3DbFree(v->db, sIter.apSub);
76121
76122 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
76123 ** If malloc failed, then the while() loop above may not have iterated
76124 ** through all opcodes and hasAbort may be set incorrectly. Return
76125 ** true for this case to prevent the assert() in the callers frame
76126 ** from failing. */
76127 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
76128 || (hasCreateTable && hasInitCoroutine) );
76129}
76130#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
76131
76132#ifdef SQLITE_DEBUG
76133/*
76134** Increment the nWrite counter in the VDBE if the cursor is not an
76135** ephemeral cursor, or if the cursor argument is NULL.
76136*/
76137SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
76138 if( pC==0
76139 || (pC->eCurType!=CURTYPE_SORTER
76140 && pC->eCurType!=CURTYPE_PSEUDO
76141 && !pC->isEphemeral)
76142 ){
76143 p->nWrite++;
76144 }
76145}
76146#endif
76147
76148#ifdef SQLITE_DEBUG
76149/*
76150** Assert if an Abort at this point in time might result in a corrupt
76151** database.
76152*/
76153SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){
76154 assert( p->nWrite==0 || p->usesStmtJournal );
76155}
76156#endif
76157
76158/*
76159** This routine is called after all opcodes have been inserted. It loops
76160** through all the opcodes and fixes up some details.
76161**
76162** (1) For each jump instruction with a negative P2 value (a label)
76163** resolve the P2 value to an actual address.
76164**
76165** (2) Compute the maximum number of arguments used by any SQL function
76166** and store that value in *pMaxFuncArgs.
76167**
76168** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
76169** indicate what the prepared statement actually does.
76170**
76171** (4) Initialize the p4.xAdvance pointer on opcodes that use it.
76172**
76173** (5) Reclaim the memory allocated for storing labels.
76174**
76175** This routine will only function correctly if the mkopcodeh.tcl generator
76176** script numbers the opcodes correctly. Changes to this routine must be
76177** coordinated with changes to mkopcodeh.tcl.
76178*/
76179static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
76180 int nMaxArgs = *pMaxFuncArgs;
76181 Op *pOp;
76182 Parse *pParse = p->pParse;
76183 int *aLabel = pParse->aLabel;
76184 p->readOnly = 1;
76185 p->bIsReader = 0;
76186 pOp = &p->aOp[p->nOp-1];
76187 while(1){
76188
76189 /* Only JUMP opcodes and the short list of special opcodes in the switch
76190 ** below need to be considered. The mkopcodeh.tcl generator script groups
76191 ** all these opcodes together near the front of the opcode list. Skip
76192 ** any opcode that does not need processing by virtual of the fact that
76193 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
76194 */
76195 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
76196 /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
76197 ** cases from this switch! */
76198 switch( pOp->opcode ){
76199 case OP_Transaction: {
76200 if( pOp->p2!=0 ) p->readOnly = 0;
76201 /* fall thru */
76202 }
76203 case OP_AutoCommit:
76204 case OP_Savepoint: {
76205 p->bIsReader = 1;
76206 break;
76207 }
76208#ifndef SQLITE_OMIT_WAL
76209 case OP_Checkpoint:
76210#endif
76211 case OP_Vacuum:
76212 case OP_JournalMode: {
76213 p->readOnly = 0;
76214 p->bIsReader = 1;
76215 break;
76216 }
76217 case OP_Next:
76218 case OP_SorterNext: {
76219 pOp->p4.xAdvance = sqlite3BtreeNext;
76220 pOp->p4type = P4_ADVANCE;
76221 /* The code generator never codes any of these opcodes as a jump
76222 ** to a label. They are always coded as a jump backwards to a
76223 ** known address */
76224 assert( pOp->p2>=0 );
76225 break;
76226 }
76227 case OP_Prev: {
76228 pOp->p4.xAdvance = sqlite3BtreePrevious;
76229 pOp->p4type = P4_ADVANCE;
76230 /* The code generator never codes any of these opcodes as a jump
76231 ** to a label. They are always coded as a jump backwards to a
76232 ** known address */
76233 assert( pOp->p2>=0 );
76234 break;
76235 }
76236#ifndef SQLITE_OMIT_VIRTUALTABLE
76237 case OP_VUpdate: {
76238 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
76239 break;
76240 }
76241 case OP_VFilter: {
76242 int n;
76243 assert( (pOp - p->aOp) >= 3 );
76244 assert( pOp[-1].opcode==OP_Integer );
76245 n = pOp[-1].p1;
76246 if( n>nMaxArgs ) nMaxArgs = n;
76247 /* Fall through into the default case */
76248 }
76249#endif
76250 default: {
76251 if( pOp->p2<0 ){
76252 /* The mkopcodeh.tcl script has so arranged things that the only
76253 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
76254 ** have non-negative values for P2. */
76255 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
76256 assert( ADDR(pOp->p2)<pParse->nLabel );
76257 pOp->p2 = aLabel[ADDR(pOp->p2)];
76258 }
76259 break;
76260 }
76261 }
76262 /* The mkopcodeh.tcl script has so arranged things that the only
76263 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
76264 ** have non-negative values for P2. */
76265 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
76266 }
76267 if( pOp==p->aOp ) break;
76268 pOp--;
76269 }
76270 sqlite3DbFree(p->db, pParse->aLabel);
76271 pParse->aLabel = 0;
76272 pParse->nLabel = 0;
76273 *pMaxFuncArgs = nMaxArgs;
76274 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
76275}
76276
76277/*
76278** Return the address of the next instruction to be inserted.
76279*/
76280SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
76281 assert( p->magic==VDBE_MAGIC_INIT );
76282 return p->nOp;
76283}
76284
76285/*
76286** Verify that at least N opcode slots are available in p without
76287** having to malloc for more space (except when compiled using
76288** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing
76289** to verify that certain calls to sqlite3VdbeAddOpList() can never
76290** fail due to a OOM fault and hence that the return value from
76291** sqlite3VdbeAddOpList() will always be non-NULL.
76292*/
76293#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
76294SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
76295 assert( p->nOp + N <= p->pParse->nOpAlloc );
76296}
76297#endif
76298
76299/*
76300** Verify that the VM passed as the only argument does not contain
76301** an OP_ResultRow opcode. Fail an assert() if it does. This is used
76302** by code in pragma.c to ensure that the implementation of certain
76303** pragmas comports with the flags specified in the mkpragmatab.tcl
76304** script.
76305*/
76306#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
76307SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
76308 int i;
76309 for(i=0; i<p->nOp; i++){
76310 assert( p->aOp[i].opcode!=OP_ResultRow );
76311 }
76312}
76313#endif
76314
76315/*
76316** Generate code (a single OP_Abortable opcode) that will
76317** verify that the VDBE program can safely call Abort in the current
76318** context.
76319*/
76320#if defined(SQLITE_DEBUG)
76321SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
76322 if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable);
76323}
76324#endif
76325
76326/*
76327** This function returns a pointer to the array of opcodes associated with
76328** the Vdbe passed as the first argument. It is the callers responsibility
76329** to arrange for the returned array to be eventually freed using the
76330** vdbeFreeOpArray() function.
76331**
76332** Before returning, *pnOp is set to the number of entries in the returned
76333** array. Also, *pnMaxArg is set to the larger of its current value and
76334** the number of entries in the Vdbe.apArg[] array required to execute the
76335** returned program.
76336*/
76337SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
76338 VdbeOp *aOp = p->aOp;
76339 assert( aOp && !p->db->mallocFailed );
76340
76341 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
76342 assert( DbMaskAllZero(p->btreeMask) );
76343
76344 resolveP2Values(p, pnMaxArg);
76345 *pnOp = p->nOp;
76346 p->aOp = 0;
76347 return aOp;
76348}
76349
76350/*
76351** Add a whole list of operations to the operation stack. Return a
76352** pointer to the first operation inserted.
76353**
76354** Non-zero P2 arguments to jump instructions are automatically adjusted
76355** so that the jump target is relative to the first operation inserted.
76356*/
76357SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(
76358 Vdbe *p, /* Add opcodes to the prepared statement */
76359 int nOp, /* Number of opcodes to add */
76360 VdbeOpList const *aOp, /* The opcodes to be added */
76361 int iLineno /* Source-file line number of first opcode */
76362){
76363 int i;
76364 VdbeOp *pOut, *pFirst;
76365 assert( nOp>0 );
76366 assert( p->magic==VDBE_MAGIC_INIT );
76367 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
76368 return 0;
76369 }
76370 pFirst = pOut = &p->aOp[p->nOp];
76371 for(i=0; i<nOp; i++, aOp++, pOut++){
76372 pOut->opcode = aOp->opcode;
76373 pOut->p1 = aOp->p1;
76374 pOut->p2 = aOp->p2;
76375 assert( aOp->p2>=0 );
76376 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
76377 pOut->p2 += p->nOp;
76378 }
76379 pOut->p3 = aOp->p3;
76380 pOut->p4type = P4_NOTUSED;
76381 pOut->p4.p = 0;
76382 pOut->p5 = 0;
76383#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
76384 pOut->zComment = 0;
76385#endif
76386#ifdef SQLITE_VDBE_COVERAGE
76387 pOut->iSrcLine = iLineno+i;
76388#else
76389 (void)iLineno;
76390#endif
76391#ifdef SQLITE_DEBUG
76392 if( p->db->flags & SQLITE_VdbeAddopTrace ){
76393 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
76394 }
76395#endif
76396 }
76397 p->nOp += nOp;
76398 return pFirst;
76399}
76400
76401#if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
76402/*
76403** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
76404*/
76405SQLITE_PRIVATE void sqlite3VdbeScanStatus(
76406 Vdbe *p, /* VM to add scanstatus() to */
76407 int addrExplain, /* Address of OP_Explain (or 0) */
76408 int addrLoop, /* Address of loop counter */
76409 int addrVisit, /* Address of rows visited counter */
76410 LogEst nEst, /* Estimated number of output rows */
76411 const char *zName /* Name of table or index being scanned */
76412){
76413 int nByte = (p->nScan+1) * sizeof(ScanStatus);
76414 ScanStatus *aNew;
76415 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
76416 if( aNew ){
76417 ScanStatus *pNew = &aNew[p->nScan++];
76418 pNew->addrExplain = addrExplain;
76419 pNew->addrLoop = addrLoop;
76420 pNew->addrVisit = addrVisit;
76421 pNew->nEst = nEst;
76422 pNew->zName = sqlite3DbStrDup(p->db, zName);
76423 p->aScan = aNew;
76424 }
76425}
76426#endif
76427
76428
76429/*
76430** Change the value of the opcode, or P1, P2, P3, or P5 operands
76431** for a specific instruction.
76432*/
76433SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, u32 addr, u8 iNewOpcode){
76434 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
76435}
76436SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
76437 sqlite3VdbeGetOp(p,addr)->p1 = val;
76438}
76439SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
76440 sqlite3VdbeGetOp(p,addr)->p2 = val;
76441}
76442SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
76443 sqlite3VdbeGetOp(p,addr)->p3 = val;
76444}
76445SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
76446 assert( p->nOp>0 || p->db->mallocFailed );
76447 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
76448}
76449
76450/*
76451** Change the P2 operand of instruction addr so that it points to
76452** the address of the next instruction to be coded.
76453*/
76454SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
76455 sqlite3VdbeChangeP2(p, addr, p->nOp);
76456}
76457
76458
76459/*
76460** If the input FuncDef structure is ephemeral, then free it. If
76461** the FuncDef is not ephermal, then do nothing.
76462*/
76463static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
76464 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
76465 sqlite3DbFreeNN(db, pDef);
76466 }
76467}
76468
76469static void vdbeFreeOpArray(sqlite3 *, Op *, int);
76470
76471/*
76472** Delete a P4 value if necessary.
76473*/
76474static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
76475 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
76476 sqlite3DbFreeNN(db, p);
76477}
76478static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
76479 freeEphemeralFunction(db, p->pFunc);
76480 sqlite3DbFreeNN(db, p);
76481}
76482static void freeP4(sqlite3 *db, int p4type, void *p4){
76483 assert( db );
76484 switch( p4type ){
76485 case P4_FUNCCTX: {
76486 freeP4FuncCtx(db, (sqlite3_context*)p4);
76487 break;
76488 }
76489 case P4_REAL:
76490 case P4_INT64:
76491 case P4_DYNAMIC:
76492 case P4_DYNBLOB:
76493 case P4_INTARRAY: {
76494 sqlite3DbFree(db, p4);
76495 break;
76496 }
76497 case P4_KEYINFO: {
76498 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
76499 break;
76500 }
76501#ifdef SQLITE_ENABLE_CURSOR_HINTS
76502 case P4_EXPR: {
76503 sqlite3ExprDelete(db, (Expr*)p4);
76504 break;
76505 }
76506#endif
76507 case P4_FUNCDEF: {
76508 freeEphemeralFunction(db, (FuncDef*)p4);
76509 break;
76510 }
76511 case P4_MEM: {
76512 if( db->pnBytesFreed==0 ){
76513 sqlite3ValueFree((sqlite3_value*)p4);
76514 }else{
76515 freeP4Mem(db, (Mem*)p4);
76516 }
76517 break;
76518 }
76519 case P4_VTAB : {
76520 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
76521 break;
76522 }
76523 }
76524}
76525
76526/*
76527** Free the space allocated for aOp and any p4 values allocated for the
76528** opcodes contained within. If aOp is not NULL it is assumed to contain
76529** nOp entries.
76530*/
76531static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
76532 if( aOp ){
76533 Op *pOp;
76534 for(pOp=&aOp[nOp-1]; pOp>=aOp; pOp--){
76535 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
76536#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
76537 sqlite3DbFree(db, pOp->zComment);
76538#endif
76539 }
76540 sqlite3DbFreeNN(db, aOp);
76541 }
76542}
76543
76544/*
76545** Link the SubProgram object passed as the second argument into the linked
76546** list at Vdbe.pSubProgram. This list is used to delete all sub-program
76547** objects when the VM is no longer required.
76548*/
76549SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
76550 p->pNext = pVdbe->pProgram;
76551 pVdbe->pProgram = p;
76552}
76553
76554/*
76555** Change the opcode at addr into OP_Noop
76556*/
76557SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
76558 VdbeOp *pOp;
76559 if( p->db->mallocFailed ) return 0;
76560 assert( addr>=0 && addr<p->nOp );
76561 pOp = &p->aOp[addr];
76562 freeP4(p->db, pOp->p4type, pOp->p4.p);
76563 pOp->p4type = P4_NOTUSED;
76564 pOp->p4.z = 0;
76565 pOp->opcode = OP_Noop;
76566 return 1;
76567}
76568
76569/*
76570** If the last opcode is "op" and it is not a jump destination,
76571** then remove it. Return true if and only if an opcode was removed.
76572*/
76573SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
76574 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
76575 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
76576 }else{
76577 return 0;
76578 }
76579}
76580
76581/*
76582** Change the value of the P4 operand for a specific instruction.
76583** This routine is useful when a large program is loaded from a
76584** static array using sqlite3VdbeAddOpList but we want to make a
76585** few minor changes to the program.
76586**
76587** If n>=0 then the P4 operand is dynamic, meaning that a copy of
76588** the string is made into memory obtained from sqlite3_malloc().
76589** A value of n==0 means copy bytes of zP4 up to and including the
76590** first null byte. If n>0 then copy n+1 bytes of zP4.
76591**
76592** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
76593** to a string or structure that is guaranteed to exist for the lifetime of
76594** the Vdbe. In these cases we can just copy the pointer.
76595**
76596** If addr<0 then change P4 on the most recently inserted instruction.
76597*/
76598static void SQLITE_NOINLINE vdbeChangeP4Full(
76599 Vdbe *p,
76600 Op *pOp,
76601 const char *zP4,
76602 int n
76603){
76604 if( pOp->p4type ){
76605 freeP4(p->db, pOp->p4type, pOp->p4.p);
76606 pOp->p4type = 0;
76607 pOp->p4.p = 0;
76608 }
76609 if( n<0 ){
76610 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
76611 }else{
76612 if( n==0 ) n = sqlite3Strlen30(zP4);
76613 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
76614 pOp->p4type = P4_DYNAMIC;
76615 }
76616}
76617SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
76618 Op *pOp;
76619 sqlite3 *db;
76620 assert( p!=0 );
76621 db = p->db;
76622 assert( p->magic==VDBE_MAGIC_INIT );
76623 assert( p->aOp!=0 || db->mallocFailed );
76624 if( db->mallocFailed ){
76625 if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4);
76626 return;
76627 }
76628 assert( p->nOp>0 );
76629 assert( addr<p->nOp );
76630 if( addr<0 ){
76631 addr = p->nOp - 1;
76632 }
76633 pOp = &p->aOp[addr];
76634 if( n>=0 || pOp->p4type ){
76635 vdbeChangeP4Full(p, pOp, zP4, n);
76636 return;
76637 }
76638 if( n==P4_INT32 ){
76639 /* Note: this cast is safe, because the origin data point was an int
76640 ** that was cast to a (const char *). */
76641 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
76642 pOp->p4type = P4_INT32;
76643 }else if( zP4!=0 ){
76644 assert( n<0 );
76645 pOp->p4.p = (void*)zP4;
76646 pOp->p4type = (signed char)n;
76647 if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4);
76648 }
76649}
76650
76651/*
76652** Change the P4 operand of the most recently coded instruction
76653** to the value defined by the arguments. This is a high-speed
76654** version of sqlite3VdbeChangeP4().
76655**
76656** The P4 operand must not have been previously defined. And the new
76657** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of
76658** those cases.
76659*/
76660SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
76661 VdbeOp *pOp;
76662 assert( n!=P4_INT32 && n!=P4_VTAB );
76663 assert( n<=0 );
76664 if( p->db->mallocFailed ){
76665 freeP4(p->db, n, pP4);
76666 }else{
76667 assert( pP4!=0 );
76668 assert( p->nOp>0 );
76669 pOp = &p->aOp[p->nOp-1];
76670 assert( pOp->p4type==P4_NOTUSED );
76671 pOp->p4type = n;
76672 pOp->p4.p = pP4;
76673 }
76674}
76675
76676/*
76677** Set the P4 on the most recently added opcode to the KeyInfo for the
76678** index given.
76679*/
76680SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
76681 Vdbe *v = pParse->pVdbe;
76682 KeyInfo *pKeyInfo;
76683 assert( v!=0 );
76684 assert( pIdx!=0 );
76685 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
76686 if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
76687}
76688
76689#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
76690/*
76691** Change the comment on the most recently coded instruction. Or
76692** insert a No-op and add the comment to that new instruction. This
76693** makes the code easier to read during debugging. None of this happens
76694** in a production build.
76695*/
76696static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
76697 assert( p->nOp>0 || p->aOp==0 );
76698 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
76699 if( p->nOp ){
76700 assert( p->aOp );
76701 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
76702 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
76703 }
76704}
76705SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
76706 va_list ap;
76707 if( p ){
76708 va_start(ap, zFormat);
76709 vdbeVComment(p, zFormat, ap);
76710 va_end(ap);
76711 }
76712}
76713SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
76714 va_list ap;
76715 if( p ){
76716 sqlite3VdbeAddOp0(p, OP_Noop);
76717 va_start(ap, zFormat);
76718 vdbeVComment(p, zFormat, ap);
76719 va_end(ap);
76720 }
76721}
76722#endif /* NDEBUG */
76723
76724#ifdef SQLITE_VDBE_COVERAGE
76725/*
76726** Set the value if the iSrcLine field for the previously coded instruction.
76727*/
76728SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
76729 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
76730}
76731#endif /* SQLITE_VDBE_COVERAGE */
76732
76733/*
76734** Return the opcode for a given address. If the address is -1, then
76735** return the most recently inserted opcode.
76736**
76737** If a memory allocation error has occurred prior to the calling of this
76738** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
76739** is readable but not writable, though it is cast to a writable value.
76740** The return of a dummy opcode allows the call to continue functioning
76741** after an OOM fault without having to check to see if the return from
76742** this routine is a valid pointer. But because the dummy.opcode is 0,
76743** dummy will never be written to. This is verified by code inspection and
76744** by running with Valgrind.
76745*/
76746SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
76747 /* C89 specifies that the constant "dummy" will be initialized to all
76748 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
76749 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
76750 assert( p->magic==VDBE_MAGIC_INIT );
76751 if( addr<0 ){
76752 addr = p->nOp - 1;
76753 }
76754 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
76755 if( p->db->mallocFailed ){
76756 return (VdbeOp*)&dummy;
76757 }else{
76758 return &p->aOp[addr];
76759 }
76760}
76761
76762#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
76763/*
76764** Return an integer value for one of the parameters to the opcode pOp
76765** determined by character c.
76766*/
76767static int translateP(char c, const Op *pOp){
76768 if( c=='1' ) return pOp->p1;
76769 if( c=='2' ) return pOp->p2;
76770 if( c=='3' ) return pOp->p3;
76771 if( c=='4' ) return pOp->p4.i;
76772 return pOp->p5;
76773}
76774
76775/*
76776** Compute a string for the "comment" field of a VDBE opcode listing.
76777**
76778** The Synopsis: field in comments in the vdbe.c source file gets converted
76779** to an extra string that is appended to the sqlite3OpcodeName(). In the
76780** absence of other comments, this synopsis becomes the comment on the opcode.
76781** Some translation occurs:
76782**
76783** "PX" -> "r[X]"
76784** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
76785** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
76786** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
76787*/
76788static int displayComment(
76789 const Op *pOp, /* The opcode to be commented */
76790 const char *zP4, /* Previously obtained value for P4 */
76791 char *zTemp, /* Write result here */
76792 int nTemp /* Space available in zTemp[] */
76793){
76794 const char *zOpName;
76795 const char *zSynopsis;
76796 int nOpName;
76797 int ii, jj;
76798 char zAlt[50];
76799 zOpName = sqlite3OpcodeName(pOp->opcode);
76800 nOpName = sqlite3Strlen30(zOpName);
76801 if( zOpName[nOpName+1] ){
76802 int seenCom = 0;
76803 char c;
76804 zSynopsis = zOpName += nOpName + 1;
76805 if( strncmp(zSynopsis,"IF ",3)==0 ){
76806 if( pOp->p5 & SQLITE_STOREP2 ){
76807 sqlite3_snprintf(sizeof(zAlt), zAlt, "r[P2] = (%s)", zSynopsis+3);
76808 }else{
76809 sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
76810 }
76811 zSynopsis = zAlt;
76812 }
76813 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
76814 if( c=='P' ){
76815 c = zSynopsis[++ii];
76816 if( c=='4' ){
76817 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
76818 }else if( c=='X' ){
76819 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
76820 seenCom = 1;
76821 }else{
76822 int v1 = translateP(c, pOp);
76823 int v2;
76824 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
76825 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
76826 ii += 3;
76827 jj += sqlite3Strlen30(zTemp+jj);
76828 v2 = translateP(zSynopsis[ii], pOp);
76829 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
76830 ii += 2;
76831 v2++;
76832 }
76833 if( v2>1 ){
76834 sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
76835 }
76836 }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
76837 ii += 4;
76838 }
76839 }
76840 jj += sqlite3Strlen30(zTemp+jj);
76841 }else{
76842 zTemp[jj++] = c;
76843 }
76844 }
76845 if( !seenCom && jj<nTemp-5 && pOp->zComment ){
76846 sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
76847 jj += sqlite3Strlen30(zTemp+jj);
76848 }
76849 if( jj<nTemp ) zTemp[jj] = 0;
76850 }else if( pOp->zComment ){
76851 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
76852 jj = sqlite3Strlen30(zTemp);
76853 }else{
76854 zTemp[0] = 0;
76855 jj = 0;
76856 }
76857 return jj;
76858}
76859#endif /* SQLITE_DEBUG */
76860
76861#if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
76862/*
76863** Translate the P4.pExpr value for an OP_CursorHint opcode into text
76864** that can be displayed in the P4 column of EXPLAIN output.
76865*/
76866static void displayP4Expr(StrAccum *p, Expr *pExpr){
76867 const char *zOp = 0;
76868 switch( pExpr->op ){
76869 case TK_STRING:
76870 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
76871 break;
76872 case TK_INTEGER:
76873 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
76874 break;
76875 case TK_NULL:
76876 sqlite3_str_appendf(p, "NULL");
76877 break;
76878 case TK_REGISTER: {
76879 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
76880 break;
76881 }
76882 case TK_COLUMN: {
76883 if( pExpr->iColumn<0 ){
76884 sqlite3_str_appendf(p, "rowid");
76885 }else{
76886 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
76887 }
76888 break;
76889 }
76890 case TK_LT: zOp = "LT"; break;
76891 case TK_LE: zOp = "LE"; break;
76892 case TK_GT: zOp = "GT"; break;
76893 case TK_GE: zOp = "GE"; break;
76894 case TK_NE: zOp = "NE"; break;
76895 case TK_EQ: zOp = "EQ"; break;
76896 case TK_IS: zOp = "IS"; break;
76897 case TK_ISNOT: zOp = "ISNOT"; break;
76898 case TK_AND: zOp = "AND"; break;
76899 case TK_OR: zOp = "OR"; break;
76900 case TK_PLUS: zOp = "ADD"; break;
76901 case TK_STAR: zOp = "MUL"; break;
76902 case TK_MINUS: zOp = "SUB"; break;
76903 case TK_REM: zOp = "REM"; break;
76904 case TK_BITAND: zOp = "BITAND"; break;
76905 case TK_BITOR: zOp = "BITOR"; break;
76906 case TK_SLASH: zOp = "DIV"; break;
76907 case TK_LSHIFT: zOp = "LSHIFT"; break;
76908 case TK_RSHIFT: zOp = "RSHIFT"; break;
76909 case TK_CONCAT: zOp = "CONCAT"; break;
76910 case TK_UMINUS: zOp = "MINUS"; break;
76911 case TK_UPLUS: zOp = "PLUS"; break;
76912 case TK_BITNOT: zOp = "BITNOT"; break;
76913 case TK_NOT: zOp = "NOT"; break;
76914 case TK_ISNULL: zOp = "ISNULL"; break;
76915 case TK_NOTNULL: zOp = "NOTNULL"; break;
76916
76917 default:
76918 sqlite3_str_appendf(p, "%s", "expr");
76919 break;
76920 }
76921
76922 if( zOp ){
76923 sqlite3_str_appendf(p, "%s(", zOp);
76924 displayP4Expr(p, pExpr->pLeft);
76925 if( pExpr->pRight ){
76926 sqlite3_str_append(p, ",", 1);
76927 displayP4Expr(p, pExpr->pRight);
76928 }
76929 sqlite3_str_append(p, ")", 1);
76930 }
76931}
76932#endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
76933
76934
76935#if VDBE_DISPLAY_P4
76936/*
76937** Compute a string that describes the P4 parameter for an opcode.
76938** Use zTemp for any required temporary buffer space.
76939*/
76940static char *displayP4(Op *pOp, char *zTemp, int nTemp){
76941 char *zP4 = zTemp;
76942 StrAccum x;
76943 assert( nTemp>=20 );
76944 sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0);
76945 switch( pOp->p4type ){
76946 case P4_KEYINFO: {
76947 int j;
76948 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
76949 assert( pKeyInfo->aSortOrder!=0 );
76950 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
76951 for(j=0; j<pKeyInfo->nKeyField; j++){
76952 CollSeq *pColl = pKeyInfo->aColl[j];
76953 const char *zColl = pColl ? pColl->zName : "";
76954 if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
76955 sqlite3_str_appendf(&x, ",%s%s",
76956 pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
76957 }
76958 sqlite3_str_append(&x, ")", 1);
76959 break;
76960 }
76961#ifdef SQLITE_ENABLE_CURSOR_HINTS
76962 case P4_EXPR: {
76963 displayP4Expr(&x, pOp->p4.pExpr);
76964 break;
76965 }
76966#endif
76967 case P4_COLLSEQ: {
76968 CollSeq *pColl = pOp->p4.pColl;
76969 sqlite3_str_appendf(&x, "(%.20s)", pColl->zName);
76970 break;
76971 }
76972 case P4_FUNCDEF: {
76973 FuncDef *pDef = pOp->p4.pFunc;
76974 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
76975 break;
76976 }
76977#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
76978 case P4_FUNCCTX: {
76979 FuncDef *pDef = pOp->p4.pCtx->pFunc;
76980 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
76981 break;
76982 }
76983#endif
76984 case P4_INT64: {
76985 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
76986 break;
76987 }
76988 case P4_INT32: {
76989 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
76990 break;
76991 }
76992 case P4_REAL: {
76993 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
76994 break;
76995 }
76996 case P4_MEM: {
76997 Mem *pMem = pOp->p4.pMem;
76998 if( pMem->flags & MEM_Str ){
76999 zP4 = pMem->z;
77000 }else if( pMem->flags & MEM_Int ){
77001 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
77002 }else if( pMem->flags & MEM_Real ){
77003 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
77004 }else if( pMem->flags & MEM_Null ){
77005 zP4 = "NULL";
77006 }else{
77007 assert( pMem->flags & MEM_Blob );
77008 zP4 = "(blob)";
77009 }
77010 break;
77011 }
77012#ifndef SQLITE_OMIT_VIRTUALTABLE
77013 case P4_VTAB: {
77014 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
77015 sqlite3_str_appendf(&x, "vtab:%p", pVtab);
77016 break;
77017 }
77018#endif
77019 case P4_INTARRAY: {
77020 int i;
77021 int *ai = pOp->p4.ai;
77022 int n = ai[0]; /* The first element of an INTARRAY is always the
77023 ** count of the number of elements to follow */
77024 for(i=1; i<=n; i++){
77025 sqlite3_str_appendf(&x, ",%d", ai[i]);
77026 }
77027 zTemp[0] = '[';
77028 sqlite3_str_append(&x, "]", 1);
77029 break;
77030 }
77031 case P4_SUBPROGRAM: {
77032 sqlite3_str_appendf(&x, "program");
77033 break;
77034 }
77035 case P4_DYNBLOB:
77036 case P4_ADVANCE: {
77037 zTemp[0] = 0;
77038 break;
77039 }
77040 case P4_TABLE: {
77041 sqlite3_str_appendf(&x, "%s", pOp->p4.pTab->zName);
77042 break;
77043 }
77044 default: {
77045 zP4 = pOp->p4.z;
77046 if( zP4==0 ){
77047 zP4 = zTemp;
77048 zTemp[0] = 0;
77049 }
77050 }
77051 }
77052 sqlite3StrAccumFinish(&x);
77053 assert( zP4!=0 );
77054 return zP4;
77055}
77056#endif /* VDBE_DISPLAY_P4 */
77057
77058/*
77059** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
77060**
77061** The prepared statements need to know in advance the complete set of
77062** attached databases that will be use. A mask of these databases
77063** is maintained in p->btreeMask. The p->lockMask value is the subset of
77064** p->btreeMask of databases that will require a lock.
77065*/
77066SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
77067 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
77068 assert( i<(int)sizeof(p->btreeMask)*8 );
77069 DbMaskSet(p->btreeMask, i);
77070 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
77071 DbMaskSet(p->lockMask, i);
77072 }
77073}
77074
77075#if !defined(SQLITE_OMIT_SHARED_CACHE)
77076/*
77077** If SQLite is compiled to support shared-cache mode and to be threadsafe,
77078** this routine obtains the mutex associated with each BtShared structure
77079** that may be accessed by the VM passed as an argument. In doing so it also
77080** sets the BtShared.db member of each of the BtShared structures, ensuring
77081** that the correct busy-handler callback is invoked if required.
77082**
77083** If SQLite is not threadsafe but does support shared-cache mode, then
77084** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
77085** of all of BtShared structures accessible via the database handle
77086** associated with the VM.
77087**
77088** If SQLite is not threadsafe and does not support shared-cache mode, this
77089** function is a no-op.
77090**
77091** The p->btreeMask field is a bitmask of all btrees that the prepared
77092** statement p will ever use. Let N be the number of bits in p->btreeMask
77093** corresponding to btrees that use shared cache. Then the runtime of
77094** this routine is N*N. But as N is rarely more than 1, this should not
77095** be a problem.
77096*/
77097SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
77098 int i;
77099 sqlite3 *db;
77100 Db *aDb;
77101 int nDb;
77102 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
77103 db = p->db;
77104 aDb = db->aDb;
77105 nDb = db->nDb;
77106 for(i=0; i<nDb; i++){
77107 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
77108 sqlite3BtreeEnter(aDb[i].pBt);
77109 }
77110 }
77111}
77112#endif
77113
77114#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
77115/*
77116** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
77117*/
77118static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
77119 int i;
77120 sqlite3 *db;
77121 Db *aDb;
77122 int nDb;
77123 db = p->db;
77124 aDb = db->aDb;
77125 nDb = db->nDb;
77126 for(i=0; i<nDb; i++){
77127 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
77128 sqlite3BtreeLeave(aDb[i].pBt);
77129 }
77130 }
77131}
77132SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
77133 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
77134 vdbeLeave(p);
77135}
77136#endif
77137
77138#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
77139/*
77140** Print a single opcode. This routine is used for debugging only.
77141*/
77142SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
77143 char *zP4;
77144 char zPtr[50];
77145 char zCom[100];
77146 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
77147 if( pOut==0 ) pOut = stdout;
77148 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
77149#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
77150 displayComment(pOp, zP4, zCom, sizeof(zCom));
77151#else
77152 zCom[0] = 0;
77153#endif
77154 /* NB: The sqlite3OpcodeName() function is implemented by code created
77155 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
77156 ** information from the vdbe.c source text */
77157 fprintf(pOut, zFormat1, pc,
77158 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
77159 zCom
77160 );
77161 fflush(pOut);
77162}
77163#endif
77164
77165/*
77166** Initialize an array of N Mem element.
77167*/
77168static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
77169 while( (N--)>0 ){
77170 p->db = db;
77171 p->flags = flags;
77172 p->szMalloc = 0;
77173#ifdef SQLITE_DEBUG
77174 p->pScopyFrom = 0;
77175#endif
77176 p++;
77177 }
77178}
77179
77180/*
77181** Release an array of N Mem elements
77182*/
77183static void releaseMemArray(Mem *p, int N){
77184 if( p && N ){
77185 Mem *pEnd = &p[N];
77186 sqlite3 *db = p->db;
77187 if( db->pnBytesFreed ){
77188 do{
77189 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
77190 }while( (++p)<pEnd );
77191 return;
77192 }
77193 do{
77194 assert( (&p[1])==pEnd || p[0].db==p[1].db );
77195 assert( sqlite3VdbeCheckMemInvariants(p) );
77196
77197 /* This block is really an inlined version of sqlite3VdbeMemRelease()
77198 ** that takes advantage of the fact that the memory cell value is
77199 ** being set to NULL after releasing any dynamic resources.
77200 **
77201 ** The justification for duplicating code is that according to
77202 ** callgrind, this causes a certain test case to hit the CPU 4.7
77203 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
77204 ** sqlite3MemRelease() were called from here. With -O2, this jumps
77205 ** to 6.6 percent. The test case is inserting 1000 rows into a table
77206 ** with no indexes using a single prepared INSERT statement, bind()
77207 ** and reset(). Inserts are grouped into a transaction.
77208 */
77209 testcase( p->flags & MEM_Agg );
77210 testcase( p->flags & MEM_Dyn );
77211 testcase( p->xDel==sqlite3VdbeFrameMemDel );
77212 if( p->flags&(MEM_Agg|MEM_Dyn) ){
77213 sqlite3VdbeMemRelease(p);
77214 }else if( p->szMalloc ){
77215 sqlite3DbFreeNN(db, p->zMalloc);
77216 p->szMalloc = 0;
77217 }
77218
77219 p->flags = MEM_Undefined;
77220 }while( (++p)<pEnd );
77221 }
77222}
77223
77224#ifdef SQLITE_DEBUG
77225/*
77226** Verify that pFrame is a valid VdbeFrame pointer. Return true if it is
77227** and false if something is wrong.
77228**
77229** This routine is intended for use inside of assert() statements only.
77230*/
77231SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){
77232 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
77233 return 1;
77234}
77235#endif
77236
77237
77238/*
77239** This is a destructor on a Mem object (which is really an sqlite3_value)
77240** that deletes the Frame object that is attached to it as a blob.
77241**
77242** This routine does not delete the Frame right away. It merely adds the
77243** frame to a list of frames to be deleted when the Vdbe halts.
77244*/
77245SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void *pArg){
77246 VdbeFrame *pFrame = (VdbeFrame*)pArg;
77247 assert( sqlite3VdbeFrameIsValid(pFrame) );
77248 pFrame->pParent = pFrame->v->pDelFrame;
77249 pFrame->v->pDelFrame = pFrame;
77250}
77251
77252
77253/*
77254** Delete a VdbeFrame object and its contents. VdbeFrame objects are
77255** allocated by the OP_Program opcode in sqlite3VdbeExec().
77256*/
77257SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
77258 int i;
77259 Mem *aMem = VdbeFrameMem(p);
77260 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
77261 assert( sqlite3VdbeFrameIsValid(p) );
77262 for(i=0; i<p->nChildCsr; i++){
77263 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
77264 }
77265 releaseMemArray(aMem, p->nChildMem);
77266 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
77267 sqlite3DbFree(p->v->db, p);
77268}
77269
77270#ifndef SQLITE_OMIT_EXPLAIN
77271/*
77272** Give a listing of the program in the virtual machine.
77273**
77274** The interface is the same as sqlite3VdbeExec(). But instead of
77275** running the code, it invokes the callback once for each instruction.
77276** This feature is used to implement "EXPLAIN".
77277**
77278** When p->explain==1, each instruction is listed. When
77279** p->explain==2, only OP_Explain instructions are listed and these
77280** are shown in a different format. p->explain==2 is used to implement
77281** EXPLAIN QUERY PLAN.
77282** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
77283** are also shown, so that the boundaries between the main program and
77284** each trigger are clear.
77285**
77286** When p->explain==1, first the main program is listed, then each of
77287** the trigger subprograms are listed one by one.
77288*/
77289SQLITE_PRIVATE int sqlite3VdbeList(
77290 Vdbe *p /* The VDBE */
77291){
77292 int nRow; /* Stop when row count reaches this */
77293 int nSub = 0; /* Number of sub-vdbes seen so far */
77294 SubProgram **apSub = 0; /* Array of sub-vdbes */
77295 Mem *pSub = 0; /* Memory cell hold array of subprogs */
77296 sqlite3 *db = p->db; /* The database connection */
77297 int i; /* Loop counter */
77298 int rc = SQLITE_OK; /* Return code */
77299 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
77300 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
77301 Op *pOp = 0;
77302
77303 assert( p->explain );
77304 assert( p->magic==VDBE_MAGIC_RUN );
77305 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
77306
77307 /* Even though this opcode does not use dynamic strings for
77308 ** the result, result columns may become dynamic if the user calls
77309 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
77310 */
77311 releaseMemArray(pMem, 8);
77312 p->pResultSet = 0;
77313
77314 if( p->rc==SQLITE_NOMEM ){
77315 /* This happens if a malloc() inside a call to sqlite3_column_text() or
77316 ** sqlite3_column_text16() failed. */
77317 sqlite3OomFault(db);
77318 return SQLITE_ERROR;
77319 }
77320
77321 /* When the number of output rows reaches nRow, that means the
77322 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
77323 ** nRow is the sum of the number of rows in the main program, plus
77324 ** the sum of the number of rows in all trigger subprograms encountered
77325 ** so far. The nRow value will increase as new trigger subprograms are
77326 ** encountered, but p->pc will eventually catch up to nRow.
77327 */
77328 nRow = p->nOp;
77329 if( bListSubprogs ){
77330 /* The first 8 memory cells are used for the result set. So we will
77331 ** commandeer the 9th cell to use as storage for an array of pointers
77332 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
77333 ** cells. */
77334 assert( p->nMem>9 );
77335 pSub = &p->aMem[9];
77336 if( pSub->flags&MEM_Blob ){
77337 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
77338 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
77339 nSub = pSub->n/sizeof(Vdbe*);
77340 apSub = (SubProgram **)pSub->z;
77341 }
77342 for(i=0; i<nSub; i++){
77343 nRow += apSub[i]->nOp;
77344 }
77345 }
77346
77347 while(1){ /* Loop exits via break */
77348 i = p->pc++;
77349 if( i>=nRow ){
77350 p->rc = SQLITE_OK;
77351 rc = SQLITE_DONE;
77352 break;
77353 }
77354 if( i<p->nOp ){
77355 /* The output line number is small enough that we are still in the
77356 ** main program. */
77357 pOp = &p->aOp[i];
77358 }else{
77359 /* We are currently listing subprograms. Figure out which one and
77360 ** pick up the appropriate opcode. */
77361 int j;
77362 i -= p->nOp;
77363 for(j=0; i>=apSub[j]->nOp; j++){
77364 i -= apSub[j]->nOp;
77365 }
77366 pOp = &apSub[j]->aOp[i];
77367 }
77368
77369 /* When an OP_Program opcode is encounter (the only opcode that has
77370 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
77371 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
77372 ** has not already been seen.
77373 */
77374 if( bListSubprogs && pOp->p4type==P4_SUBPROGRAM ){
77375 int nByte = (nSub+1)*sizeof(SubProgram*);
77376 int j;
77377 for(j=0; j<nSub; j++){
77378 if( apSub[j]==pOp->p4.pProgram ) break;
77379 }
77380 if( j==nSub ){
77381 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
77382 if( p->rc!=SQLITE_OK ){
77383 rc = SQLITE_ERROR;
77384 break;
77385 }
77386 apSub = (SubProgram **)pSub->z;
77387 apSub[nSub++] = pOp->p4.pProgram;
77388 pSub->flags |= MEM_Blob;
77389 pSub->n = nSub*sizeof(SubProgram*);
77390 nRow += pOp->p4.pProgram->nOp;
77391 }
77392 }
77393 if( p->explain<2 ) break;
77394 if( pOp->opcode==OP_Explain ) break;
77395 if( pOp->opcode==OP_Init && p->pc>1 ) break;
77396 }
77397
77398 if( rc==SQLITE_OK ){
77399 if( db->u1.isInterrupted ){
77400 p->rc = SQLITE_INTERRUPT;
77401 rc = SQLITE_ERROR;
77402 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
77403 }else{
77404 char *zP4;
77405 if( p->explain==1 ){
77406 pMem->flags = MEM_Int;
77407 pMem->u.i = i; /* Program counter */
77408 pMem++;
77409
77410 pMem->flags = MEM_Static|MEM_Str|MEM_Term;
77411 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
77412 assert( pMem->z!=0 );
77413 pMem->n = sqlite3Strlen30(pMem->z);
77414 pMem->enc = SQLITE_UTF8;
77415 pMem++;
77416 }
77417
77418 pMem->flags = MEM_Int;
77419 pMem->u.i = pOp->p1; /* P1 */
77420 pMem++;
77421
77422 pMem->flags = MEM_Int;
77423 pMem->u.i = pOp->p2; /* P2 */
77424 pMem++;
77425
77426 pMem->flags = MEM_Int;
77427 pMem->u.i = pOp->p3; /* P3 */
77428 pMem++;
77429
77430 if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */
77431 assert( p->db->mallocFailed );
77432 return SQLITE_ERROR;
77433 }
77434 pMem->flags = MEM_Str|MEM_Term;
77435 zP4 = displayP4(pOp, pMem->z, pMem->szMalloc);
77436 if( zP4!=pMem->z ){
77437 pMem->n = 0;
77438 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
77439 }else{
77440 assert( pMem->z!=0 );
77441 pMem->n = sqlite3Strlen30(pMem->z);
77442 pMem->enc = SQLITE_UTF8;
77443 }
77444 pMem++;
77445
77446 if( p->explain==1 ){
77447 if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
77448 assert( p->db->mallocFailed );
77449 return SQLITE_ERROR;
77450 }
77451 pMem->flags = MEM_Str|MEM_Term;
77452 pMem->n = 2;
77453 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
77454 pMem->enc = SQLITE_UTF8;
77455 pMem++;
77456
77457#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
77458 if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
77459 assert( p->db->mallocFailed );
77460 return SQLITE_ERROR;
77461 }
77462 pMem->flags = MEM_Str|MEM_Term;
77463 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
77464 pMem->enc = SQLITE_UTF8;
77465#else
77466 pMem->flags = MEM_Null; /* Comment */
77467#endif
77468 }
77469
77470 p->nResColumn = 8 - 4*(p->explain-1);
77471 p->pResultSet = &p->aMem[1];
77472 p->rc = SQLITE_OK;
77473 rc = SQLITE_ROW;
77474 }
77475 }
77476 return rc;
77477}
77478#endif /* SQLITE_OMIT_EXPLAIN */
77479
77480#ifdef SQLITE_DEBUG
77481/*
77482** Print the SQL that was used to generate a VDBE program.
77483*/
77484SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
77485 const char *z = 0;
77486 if( p->zSql ){
77487 z = p->zSql;
77488 }else if( p->nOp>=1 ){
77489 const VdbeOp *pOp = &p->aOp[0];
77490 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
77491 z = pOp->p4.z;
77492 while( sqlite3Isspace(*z) ) z++;
77493 }
77494 }
77495 if( z ) printf("SQL: [%s]\n", z);
77496}
77497#endif
77498
77499#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
77500/*
77501** Print an IOTRACE message showing SQL content.
77502*/
77503SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
77504 int nOp = p->nOp;
77505 VdbeOp *pOp;
77506 if( sqlite3IoTrace==0 ) return;
77507 if( nOp<1 ) return;
77508 pOp = &p->aOp[0];
77509 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
77510 int i, j;
77511 char z[1000];
77512 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
77513 for(i=0; sqlite3Isspace(z[i]); i++){}
77514 for(j=0; z[i]; i++){
77515 if( sqlite3Isspace(z[i]) ){
77516 if( z[i-1]!=' ' ){
77517 z[j++] = ' ';
77518 }
77519 }else{
77520 z[j++] = z[i];
77521 }
77522 }
77523 z[j] = 0;
77524 sqlite3IoTrace("SQL %s\n", z);
77525 }
77526}
77527#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
77528
77529/* An instance of this object describes bulk memory available for use
77530** by subcomponents of a prepared statement. Space is allocated out
77531** of a ReusableSpace object by the allocSpace() routine below.
77532*/
77533struct ReusableSpace {
77534 u8 *pSpace; /* Available memory */
77535 int nFree; /* Bytes of available memory */
77536 int nNeeded; /* Total bytes that could not be allocated */
77537};
77538
77539/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
77540** from the ReusableSpace object. Return a pointer to the allocated
77541** memory on success. If insufficient memory is available in the
77542** ReusableSpace object, increase the ReusableSpace.nNeeded
77543** value by the amount needed and return NULL.
77544**
77545** If pBuf is not initially NULL, that means that the memory has already
77546** been allocated by a prior call to this routine, so just return a copy
77547** of pBuf and leave ReusableSpace unchanged.
77548**
77549** This allocator is employed to repurpose unused slots at the end of the
77550** opcode array of prepared state for other memory needs of the prepared
77551** statement.
77552*/
77553static void *allocSpace(
77554 struct ReusableSpace *p, /* Bulk memory available for allocation */
77555 void *pBuf, /* Pointer to a prior allocation */
77556 int nByte /* Bytes of memory needed */
77557){
77558 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
77559 if( pBuf==0 ){
77560 nByte = ROUND8(nByte);
77561 if( nByte <= p->nFree ){
77562 p->nFree -= nByte;
77563 pBuf = &p->pSpace[p->nFree];
77564 }else{
77565 p->nNeeded += nByte;
77566 }
77567 }
77568 assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
77569 return pBuf;
77570}
77571
77572/*
77573** Rewind the VDBE back to the beginning in preparation for
77574** running it.
77575*/
77576SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
77577#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
77578 int i;
77579#endif
77580 assert( p!=0 );
77581 assert( p->magic==VDBE_MAGIC_INIT || p->magic==VDBE_MAGIC_RESET );
77582
77583 /* There should be at least one opcode.
77584 */
77585 assert( p->nOp>0 );
77586
77587 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
77588 p->magic = VDBE_MAGIC_RUN;
77589
77590#ifdef SQLITE_DEBUG
77591 for(i=0; i<p->nMem; i++){
77592 assert( p->aMem[i].db==p->db );
77593 }
77594#endif
77595 p->pc = -1;
77596 p->rc = SQLITE_OK;
77597 p->errorAction = OE_Abort;
77598 p->nChange = 0;
77599 p->cacheCtr = 1;
77600 p->minWriteFileFormat = 255;
77601 p->iStatement = 0;
77602 p->nFkConstraint = 0;
77603#ifdef VDBE_PROFILE
77604 for(i=0; i<p->nOp; i++){
77605 p->aOp[i].cnt = 0;
77606 p->aOp[i].cycles = 0;
77607 }
77608#endif
77609}
77610
77611/*
77612** Prepare a virtual machine for execution for the first time after
77613** creating the virtual machine. This involves things such
77614** as allocating registers and initializing the program counter.
77615** After the VDBE has be prepped, it can be executed by one or more
77616** calls to sqlite3VdbeExec().
77617**
77618** This function may be called exactly once on each virtual machine.
77619** After this routine is called the VM has been "packaged" and is ready
77620** to run. After this routine is called, further calls to
77621** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
77622** the Vdbe from the Parse object that helped generate it so that the
77623** the Vdbe becomes an independent entity and the Parse object can be
77624** destroyed.
77625**
77626** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
77627** to its initial state after it has been run.
77628*/
77629SQLITE_PRIVATE void sqlite3VdbeMakeReady(
77630 Vdbe *p, /* The VDBE */
77631 Parse *pParse /* Parsing context */
77632){
77633 sqlite3 *db; /* The database connection */
77634 int nVar; /* Number of parameters */
77635 int nMem; /* Number of VM memory registers */
77636 int nCursor; /* Number of cursors required */
77637 int nArg; /* Number of arguments in subprograms */
77638 int n; /* Loop counter */
77639 struct ReusableSpace x; /* Reusable bulk memory */
77640
77641 assert( p!=0 );
77642 assert( p->nOp>0 );
77643 assert( pParse!=0 );
77644 assert( p->magic==VDBE_MAGIC_INIT );
77645 assert( pParse==p->pParse );
77646 db = p->db;
77647 assert( db->mallocFailed==0 );
77648 nVar = pParse->nVar;
77649 nMem = pParse->nMem;
77650 nCursor = pParse->nTab;
77651 nArg = pParse->nMaxArg;
77652
77653 /* Each cursor uses a memory cell. The first cursor (cursor 0) can
77654 ** use aMem[0] which is not otherwise used by the VDBE program. Allocate
77655 ** space at the end of aMem[] for cursors 1 and greater.
77656 ** See also: allocateCursor().
77657 */
77658 nMem += nCursor;
77659 if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */
77660
77661 /* Figure out how much reusable memory is available at the end of the
77662 ** opcode array. This extra memory will be reallocated for other elements
77663 ** of the prepared statement.
77664 */
77665 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
77666 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
77667 assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
77668 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
77669 assert( x.nFree>=0 );
77670 assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
77671
77672 resolveP2Values(p, &nArg);
77673 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
77674 if( pParse->explain && nMem<10 ){
77675 nMem = 10;
77676 }
77677 p->expired = 0;
77678
77679 /* Memory for registers, parameters, cursor, etc, is allocated in one or two
77680 ** passes. On the first pass, we try to reuse unused memory at the
77681 ** end of the opcode array. If we are unable to satisfy all memory
77682 ** requirements by reusing the opcode array tail, then the second
77683 ** pass will fill in the remainder using a fresh memory allocation.
77684 **
77685 ** This two-pass approach that reuses as much memory as possible from
77686 ** the leftover memory at the end of the opcode array. This can significantly
77687 ** reduce the amount of memory held by a prepared statement.
77688 */
77689 do {
77690 x.nNeeded = 0;
77691 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
77692 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
77693 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
77694 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
77695#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
77696 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
77697#endif
77698 if( x.nNeeded==0 ) break;
77699 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
77700 x.nFree = x.nNeeded;
77701 }while( !db->mallocFailed );
77702
77703 p->pVList = pParse->pVList;
77704 pParse->pVList = 0;
77705 p->explain = pParse->explain;
77706 if( db->mallocFailed ){
77707 p->nVar = 0;
77708 p->nCursor = 0;
77709 p->nMem = 0;
77710 }else{
77711 p->nCursor = nCursor;
77712 p->nVar = (ynVar)nVar;
77713 initMemArray(p->aVar, nVar, db, MEM_Null);
77714 p->nMem = nMem;
77715 initMemArray(p->aMem, nMem, db, MEM_Undefined);
77716 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
77717#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
77718 memset(p->anExec, 0, p->nOp*sizeof(i64));
77719#endif
77720 }
77721 sqlite3VdbeRewind(p);
77722}
77723
77724/*
77725** Close a VDBE cursor and release all the resources that cursor
77726** happens to hold.
77727*/
77728SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
77729 if( pCx==0 ){
77730 return;
77731 }
77732 assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE );
77733 switch( pCx->eCurType ){
77734 case CURTYPE_SORTER: {
77735 sqlite3VdbeSorterClose(p->db, pCx);
77736 break;
77737 }
77738 case CURTYPE_BTREE: {
77739 if( pCx->isEphemeral ){
77740 if( pCx->pBtx ) sqlite3BtreeClose(pCx->pBtx);
77741 /* The pCx->pCursor will be close automatically, if it exists, by
77742 ** the call above. */
77743 }else{
77744 assert( pCx->uc.pCursor!=0 );
77745 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
77746 }
77747 break;
77748 }
77749#ifndef SQLITE_OMIT_VIRTUALTABLE
77750 case CURTYPE_VTAB: {
77751 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
77752 const sqlite3_module *pModule = pVCur->pVtab->pModule;
77753 assert( pVCur->pVtab->nRef>0 );
77754 pVCur->pVtab->nRef--;
77755 pModule->xClose(pVCur);
77756 break;
77757 }
77758#endif
77759 }
77760}
77761
77762/*
77763** Close all cursors in the current frame.
77764*/
77765static void closeCursorsInFrame(Vdbe *p){
77766 if( p->apCsr ){
77767 int i;
77768 for(i=0; i<p->nCursor; i++){
77769 VdbeCursor *pC = p->apCsr[i];
77770 if( pC ){
77771 sqlite3VdbeFreeCursor(p, pC);
77772 p->apCsr[i] = 0;
77773 }
77774 }
77775 }
77776}
77777
77778/*
77779** Copy the values stored in the VdbeFrame structure to its Vdbe. This
77780** is used, for example, when a trigger sub-program is halted to restore
77781** control to the main program.
77782*/
77783SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
77784 Vdbe *v = pFrame->v;
77785 closeCursorsInFrame(v);
77786#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
77787 v->anExec = pFrame->anExec;
77788#endif
77789 v->aOp = pFrame->aOp;
77790 v->nOp = pFrame->nOp;
77791 v->aMem = pFrame->aMem;
77792 v->nMem = pFrame->nMem;
77793 v->apCsr = pFrame->apCsr;
77794 v->nCursor = pFrame->nCursor;
77795 v->db->lastRowid = pFrame->lastRowid;
77796 v->nChange = pFrame->nChange;
77797 v->db->nChange = pFrame->nDbChange;
77798 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
77799 v->pAuxData = pFrame->pAuxData;
77800 pFrame->pAuxData = 0;
77801 return pFrame->pc;
77802}
77803
77804/*
77805** Close all cursors.
77806**
77807** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
77808** cell array. This is necessary as the memory cell array may contain
77809** pointers to VdbeFrame objects, which may in turn contain pointers to
77810** open cursors.
77811*/
77812static void closeAllCursors(Vdbe *p){
77813 if( p->pFrame ){
77814 VdbeFrame *pFrame;
77815 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
77816 sqlite3VdbeFrameRestore(pFrame);
77817 p->pFrame = 0;
77818 p->nFrame = 0;
77819 }
77820 assert( p->nFrame==0 );
77821 closeCursorsInFrame(p);
77822 if( p->aMem ){
77823 releaseMemArray(p->aMem, p->nMem);
77824 }
77825 while( p->pDelFrame ){
77826 VdbeFrame *pDel = p->pDelFrame;
77827 p->pDelFrame = pDel->pParent;
77828 sqlite3VdbeFrameDelete(pDel);
77829 }
77830
77831 /* Delete any auxdata allocations made by the VM */
77832 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
77833 assert( p->pAuxData==0 );
77834}
77835
77836/*
77837** Set the number of result columns that will be returned by this SQL
77838** statement. This is now set at compile time, rather than during
77839** execution of the vdbe program so that sqlite3_column_count() can
77840** be called on an SQL statement before sqlite3_step().
77841*/
77842SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
77843 int n;
77844 sqlite3 *db = p->db;
77845
77846 if( p->nResColumn ){
77847 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
77848 sqlite3DbFree(db, p->aColName);
77849 }
77850 n = nResColumn*COLNAME_N;
77851 p->nResColumn = (u16)nResColumn;
77852 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
77853 if( p->aColName==0 ) return;
77854 initMemArray(p->aColName, n, db, MEM_Null);
77855}
77856
77857/*
77858** Set the name of the idx'th column to be returned by the SQL statement.
77859** zName must be a pointer to a nul terminated string.
77860**
77861** This call must be made after a call to sqlite3VdbeSetNumCols().
77862**
77863** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
77864** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
77865** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
77866*/
77867SQLITE_PRIVATE int sqlite3VdbeSetColName(
77868 Vdbe *p, /* Vdbe being configured */
77869 int idx, /* Index of column zName applies to */
77870 int var, /* One of the COLNAME_* constants */
77871 const char *zName, /* Pointer to buffer containing name */
77872 void (*xDel)(void*) /* Memory management strategy for zName */
77873){
77874 int rc;
77875 Mem *pColName;
77876 assert( idx<p->nResColumn );
77877 assert( var<COLNAME_N );
77878 if( p->db->mallocFailed ){
77879 assert( !zName || xDel!=SQLITE_DYNAMIC );
77880 return SQLITE_NOMEM_BKPT;
77881 }
77882 assert( p->aColName!=0 );
77883 pColName = &(p->aColName[idx+var*p->nResColumn]);
77884 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
77885 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
77886 return rc;
77887}
77888
77889/*
77890** A read or write transaction may or may not be active on database handle
77891** db. If a transaction is active, commit it. If there is a
77892** write-transaction spanning more than one database file, this routine
77893** takes care of the master journal trickery.
77894*/
77895static int vdbeCommit(sqlite3 *db, Vdbe *p){
77896 int i;
77897 int nTrans = 0; /* Number of databases with an active write-transaction
77898 ** that are candidates for a two-phase commit using a
77899 ** master-journal */
77900 int rc = SQLITE_OK;
77901 int needXcommit = 0;
77902
77903#ifdef SQLITE_OMIT_VIRTUALTABLE
77904 /* With this option, sqlite3VtabSync() is defined to be simply
77905 ** SQLITE_OK so p is not used.
77906 */
77907 UNUSED_PARAMETER(p);
77908#endif
77909
77910 /* Before doing anything else, call the xSync() callback for any
77911 ** virtual module tables written in this transaction. This has to
77912 ** be done before determining whether a master journal file is
77913 ** required, as an xSync() callback may add an attached database
77914 ** to the transaction.
77915 */
77916 rc = sqlite3VtabSync(db, p);
77917
77918 /* This loop determines (a) if the commit hook should be invoked and
77919 ** (b) how many database files have open write transactions, not
77920 ** including the temp database. (b) is important because if more than
77921 ** one database file has an open write transaction, a master journal
77922 ** file is required for an atomic commit.
77923 */
77924 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
77925 Btree *pBt = db->aDb[i].pBt;
77926 if( sqlite3BtreeIsInTrans(pBt) ){
77927 /* Whether or not a database might need a master journal depends upon
77928 ** its journal mode (among other things). This matrix determines which
77929 ** journal modes use a master journal and which do not */
77930 static const u8 aMJNeeded[] = {
77931 /* DELETE */ 1,
77932 /* PERSIST */ 1,
77933 /* OFF */ 0,
77934 /* TRUNCATE */ 1,
77935 /* MEMORY */ 0,
77936 /* WAL */ 0
77937 };
77938 Pager *pPager; /* Pager associated with pBt */
77939 needXcommit = 1;
77940 sqlite3BtreeEnter(pBt);
77941 pPager = sqlite3BtreePager(pBt);
77942 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
77943 && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
77944 && sqlite3PagerIsMemdb(pPager)==0
77945 ){
77946 assert( i!=1 );
77947 nTrans++;
77948 }
77949 rc = sqlite3PagerExclusiveLock(pPager);
77950 sqlite3BtreeLeave(pBt);
77951 }
77952 }
77953 if( rc!=SQLITE_OK ){
77954 return rc;
77955 }
77956
77957 /* If there are any write-transactions at all, invoke the commit hook */
77958 if( needXcommit && db->xCommitCallback ){
77959 rc = db->xCommitCallback(db->pCommitArg);
77960 if( rc ){
77961 return SQLITE_CONSTRAINT_COMMITHOOK;
77962 }
77963 }
77964
77965 /* The simple case - no more than one database file (not counting the
77966 ** TEMP database) has a transaction active. There is no need for the
77967 ** master-journal.
77968 **
77969 ** If the return value of sqlite3BtreeGetFilename() is a zero length
77970 ** string, it means the main database is :memory: or a temp file. In
77971 ** that case we do not support atomic multi-file commits, so use the
77972 ** simple case then too.
77973 */
77974 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
77975 || nTrans<=1
77976 ){
77977 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
77978 Btree *pBt = db->aDb[i].pBt;
77979 if( pBt ){
77980 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
77981 }
77982 }
77983
77984 /* Do the commit only if all databases successfully complete phase 1.
77985 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
77986 ** IO error while deleting or truncating a journal file. It is unlikely,
77987 ** but could happen. In this case abandon processing and return the error.
77988 */
77989 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
77990 Btree *pBt = db->aDb[i].pBt;
77991 if( pBt ){
77992 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
77993 }
77994 }
77995 if( rc==SQLITE_OK ){
77996 sqlite3VtabCommit(db);
77997 }
77998 }
77999
78000 /* The complex case - There is a multi-file write-transaction active.
78001 ** This requires a master journal file to ensure the transaction is
78002 ** committed atomically.
78003 */
78004#ifndef SQLITE_OMIT_DISKIO
78005 else{
78006 sqlite3_vfs *pVfs = db->pVfs;
78007 char *zMaster = 0; /* File-name for the master journal */
78008 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
78009 sqlite3_file *pMaster = 0;
78010 i64 offset = 0;
78011 int res;
78012 int retryCount = 0;
78013 int nMainFile;
78014
78015 /* Select a master journal file name */
78016 nMainFile = sqlite3Strlen30(zMainFile);
78017 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
78018 if( zMaster==0 ) return SQLITE_NOMEM_BKPT;
78019 do {
78020 u32 iRandom;
78021 if( retryCount ){
78022 if( retryCount>100 ){
78023 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
78024 sqlite3OsDelete(pVfs, zMaster, 0);
78025 break;
78026 }else if( retryCount==1 ){
78027 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
78028 }
78029 }
78030 retryCount++;
78031 sqlite3_randomness(sizeof(iRandom), &iRandom);
78032 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
78033 (iRandom>>8)&0xffffff, iRandom&0xff);
78034 /* The antipenultimate character of the master journal name must
78035 ** be "9" to avoid name collisions when using 8+3 filenames. */
78036 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
78037 sqlite3FileSuffix3(zMainFile, zMaster);
78038 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
78039 }while( rc==SQLITE_OK && res );
78040 if( rc==SQLITE_OK ){
78041 /* Open the master journal. */
78042 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
78043 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
78044 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
78045 );
78046 }
78047 if( rc!=SQLITE_OK ){
78048 sqlite3DbFree(db, zMaster);
78049 return rc;
78050 }
78051
78052 /* Write the name of each database file in the transaction into the new
78053 ** master journal file. If an error occurs at this point close
78054 ** and delete the master journal file. All the individual journal files
78055 ** still have 'null' as the master journal pointer, so they will roll
78056 ** back independently if a failure occurs.
78057 */
78058 for(i=0; i<db->nDb; i++){
78059 Btree *pBt = db->aDb[i].pBt;
78060 if( sqlite3BtreeIsInTrans(pBt) ){
78061 char const *zFile = sqlite3BtreeGetJournalname(pBt);
78062 if( zFile==0 ){
78063 continue; /* Ignore TEMP and :memory: databases */
78064 }
78065 assert( zFile[0]!=0 );
78066 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
78067 offset += sqlite3Strlen30(zFile)+1;
78068 if( rc!=SQLITE_OK ){
78069 sqlite3OsCloseFree(pMaster);
78070 sqlite3OsDelete(pVfs, zMaster, 0);
78071 sqlite3DbFree(db, zMaster);
78072 return rc;
78073 }
78074 }
78075 }
78076
78077 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
78078 ** flag is set this is not required.
78079 */
78080 if( 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
78081 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
78082 ){
78083 sqlite3OsCloseFree(pMaster);
78084 sqlite3OsDelete(pVfs, zMaster, 0);
78085 sqlite3DbFree(db, zMaster);
78086 return rc;
78087 }
78088
78089 /* Sync all the db files involved in the transaction. The same call
78090 ** sets the master journal pointer in each individual journal. If
78091 ** an error occurs here, do not delete the master journal file.
78092 **
78093 ** If the error occurs during the first call to
78094 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
78095 ** master journal file will be orphaned. But we cannot delete it,
78096 ** in case the master journal file name was written into the journal
78097 ** file before the failure occurred.
78098 */
78099 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
78100 Btree *pBt = db->aDb[i].pBt;
78101 if( pBt ){
78102 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
78103 }
78104 }
78105 sqlite3OsCloseFree(pMaster);
78106 assert( rc!=SQLITE_BUSY );
78107 if( rc!=SQLITE_OK ){
78108 sqlite3DbFree(db, zMaster);
78109 return rc;
78110 }
78111
78112 /* Delete the master journal file. This commits the transaction. After
78113 ** doing this the directory is synced again before any individual
78114 ** transaction files are deleted.
78115 */
78116 rc = sqlite3OsDelete(pVfs, zMaster, 1);
78117 sqlite3DbFree(db, zMaster);
78118 zMaster = 0;
78119 if( rc ){
78120 return rc;
78121 }
78122
78123 /* All files and directories have already been synced, so the following
78124 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
78125 ** deleting or truncating journals. If something goes wrong while
78126 ** this is happening we don't really care. The integrity of the
78127 ** transaction is already guaranteed, but some stray 'cold' journals
78128 ** may be lying around. Returning an error code won't help matters.
78129 */
78130 disable_simulated_io_errors();
78131 sqlite3BeginBenignMalloc();
78132 for(i=0; i<db->nDb; i++){
78133 Btree *pBt = db->aDb[i].pBt;
78134 if( pBt ){
78135 sqlite3BtreeCommitPhaseTwo(pBt, 1);
78136 }
78137 }
78138 sqlite3EndBenignMalloc();
78139 enable_simulated_io_errors();
78140
78141 sqlite3VtabCommit(db);
78142 }
78143#endif
78144
78145 return rc;
78146}
78147
78148/*
78149** This routine checks that the sqlite3.nVdbeActive count variable
78150** matches the number of vdbe's in the list sqlite3.pVdbe that are
78151** currently active. An assertion fails if the two counts do not match.
78152** This is an internal self-check only - it is not an essential processing
78153** step.
78154**
78155** This is a no-op if NDEBUG is defined.
78156*/
78157#ifndef NDEBUG
78158static void checkActiveVdbeCnt(sqlite3 *db){
78159 Vdbe *p;
78160 int cnt = 0;
78161 int nWrite = 0;
78162 int nRead = 0;
78163 p = db->pVdbe;
78164 while( p ){
78165 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
78166 cnt++;
78167 if( p->readOnly==0 ) nWrite++;
78168 if( p->bIsReader ) nRead++;
78169 }
78170 p = p->pNext;
78171 }
78172 assert( cnt==db->nVdbeActive );
78173 assert( nWrite==db->nVdbeWrite );
78174 assert( nRead==db->nVdbeRead );
78175}
78176#else
78177#define checkActiveVdbeCnt(x)
78178#endif
78179
78180/*
78181** If the Vdbe passed as the first argument opened a statement-transaction,
78182** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
78183** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
78184** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
78185** statement transaction is committed.
78186**
78187** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
78188** Otherwise SQLITE_OK.
78189*/
78190static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
78191 sqlite3 *const db = p->db;
78192 int rc = SQLITE_OK;
78193 int i;
78194 const int iSavepoint = p->iStatement-1;
78195
78196 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
78197 assert( db->nStatement>0 );
78198 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
78199
78200 for(i=0; i<db->nDb; i++){
78201 int rc2 = SQLITE_OK;
78202 Btree *pBt = db->aDb[i].pBt;
78203 if( pBt ){
78204 if( eOp==SAVEPOINT_ROLLBACK ){
78205 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
78206 }
78207 if( rc2==SQLITE_OK ){
78208 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
78209 }
78210 if( rc==SQLITE_OK ){
78211 rc = rc2;
78212 }
78213 }
78214 }
78215 db->nStatement--;
78216 p->iStatement = 0;
78217
78218 if( rc==SQLITE_OK ){
78219 if( eOp==SAVEPOINT_ROLLBACK ){
78220 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
78221 }
78222 if( rc==SQLITE_OK ){
78223 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
78224 }
78225 }
78226
78227 /* If the statement transaction is being rolled back, also restore the
78228 ** database handles deferred constraint counter to the value it had when
78229 ** the statement transaction was opened. */
78230 if( eOp==SAVEPOINT_ROLLBACK ){
78231 db->nDeferredCons = p->nStmtDefCons;
78232 db->nDeferredImmCons = p->nStmtDefImmCons;
78233 }
78234 return rc;
78235}
78236SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
78237 if( p->db->nStatement && p->iStatement ){
78238 return vdbeCloseStatement(p, eOp);
78239 }
78240 return SQLITE_OK;
78241}
78242
78243
78244/*
78245** This function is called when a transaction opened by the database
78246** handle associated with the VM passed as an argument is about to be
78247** committed. If there are outstanding deferred foreign key constraint
78248** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
78249**
78250** If there are outstanding FK violations and this function returns
78251** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
78252** and write an error message to it. Then return SQLITE_ERROR.
78253*/
78254#ifndef SQLITE_OMIT_FOREIGN_KEY
78255SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
78256 sqlite3 *db = p->db;
78257 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
78258 || (!deferred && p->nFkConstraint>0)
78259 ){
78260 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
78261 p->errorAction = OE_Abort;
78262 sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
78263 return SQLITE_ERROR;
78264 }
78265 return SQLITE_OK;
78266}
78267#endif
78268
78269/*
78270** This routine is called the when a VDBE tries to halt. If the VDBE
78271** has made changes and is in autocommit mode, then commit those
78272** changes. If a rollback is needed, then do the rollback.
78273**
78274** This routine is the only way to move the state of a VM from
78275** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
78276** call this on a VM that is in the SQLITE_MAGIC_HALT state.
78277**
78278** Return an error code. If the commit could not complete because of
78279** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
78280** means the close did not happen and needs to be repeated.
78281*/
78282SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
78283 int rc; /* Used to store transient return codes */
78284 sqlite3 *db = p->db;
78285
78286 /* This function contains the logic that determines if a statement or
78287 ** transaction will be committed or rolled back as a result of the
78288 ** execution of this virtual machine.
78289 **
78290 ** If any of the following errors occur:
78291 **
78292 ** SQLITE_NOMEM
78293 ** SQLITE_IOERR
78294 ** SQLITE_FULL
78295 ** SQLITE_INTERRUPT
78296 **
78297 ** Then the internal cache might have been left in an inconsistent
78298 ** state. We need to rollback the statement transaction, if there is
78299 ** one, or the complete transaction if there is no statement transaction.
78300 */
78301
78302 if( p->magic!=VDBE_MAGIC_RUN ){
78303 return SQLITE_OK;
78304 }
78305 if( db->mallocFailed ){
78306 p->rc = SQLITE_NOMEM_BKPT;
78307 }
78308 closeAllCursors(p);
78309 checkActiveVdbeCnt(db);
78310
78311 /* No commit or rollback needed if the program never started or if the
78312 ** SQL statement does not read or write a database file. */
78313 if( p->pc>=0 && p->bIsReader ){
78314 int mrc; /* Primary error code from p->rc */
78315 int eStatementOp = 0;
78316 int isSpecialError; /* Set to true if a 'special' error */
78317
78318 /* Lock all btrees used by the statement */
78319 sqlite3VdbeEnter(p);
78320
78321 /* Check for one of the special errors */
78322 mrc = p->rc & 0xff;
78323 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
78324 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
78325 if( isSpecialError ){
78326 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
78327 ** no rollback is necessary. Otherwise, at least a savepoint
78328 ** transaction must be rolled back to restore the database to a
78329 ** consistent state.
78330 **
78331 ** Even if the statement is read-only, it is important to perform
78332 ** a statement or transaction rollback operation. If the error
78333 ** occurred while writing to the journal, sub-journal or database
78334 ** file as part of an effort to free up cache space (see function
78335 ** pagerStress() in pager.c), the rollback is required to restore
78336 ** the pager to a consistent state.
78337 */
78338 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
78339 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
78340 eStatementOp = SAVEPOINT_ROLLBACK;
78341 }else{
78342 /* We are forced to roll back the active transaction. Before doing
78343 ** so, abort any other statements this handle currently has active.
78344 */
78345 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
78346 sqlite3CloseSavepoints(db);
78347 db->autoCommit = 1;
78348 p->nChange = 0;
78349 }
78350 }
78351 }
78352
78353 /* Check for immediate foreign key violations. */
78354 if( p->rc==SQLITE_OK ){
78355 sqlite3VdbeCheckFk(p, 0);
78356 }
78357
78358 /* If the auto-commit flag is set and this is the only active writer
78359 ** VM, then we do either a commit or rollback of the current transaction.
78360 **
78361 ** Note: This block also runs if one of the special errors handled
78362 ** above has occurred.
78363 */
78364 if( !sqlite3VtabInSync(db)
78365 && db->autoCommit
78366 && db->nVdbeWrite==(p->readOnly==0)
78367 ){
78368 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
78369 rc = sqlite3VdbeCheckFk(p, 1);
78370 if( rc!=SQLITE_OK ){
78371 if( NEVER(p->readOnly) ){
78372 sqlite3VdbeLeave(p);
78373 return SQLITE_ERROR;
78374 }
78375 rc = SQLITE_CONSTRAINT_FOREIGNKEY;
78376 }else{
78377 /* The auto-commit flag is true, the vdbe program was successful
78378 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
78379 ** key constraints to hold up the transaction. This means a commit
78380 ** is required. */
78381 rc = vdbeCommit(db, p);
78382 }
78383 if( rc==SQLITE_BUSY && p->readOnly ){
78384 sqlite3VdbeLeave(p);
78385 return SQLITE_BUSY;
78386 }else if( rc!=SQLITE_OK ){
78387 p->rc = rc;
78388 sqlite3RollbackAll(db, SQLITE_OK);
78389 p->nChange = 0;
78390 }else{
78391 db->nDeferredCons = 0;
78392 db->nDeferredImmCons = 0;
78393 db->flags &= ~SQLITE_DeferFKs;
78394 sqlite3CommitInternalChanges(db);
78395 }
78396 }else{
78397 sqlite3RollbackAll(db, SQLITE_OK);
78398 p->nChange = 0;
78399 }
78400 db->nStatement = 0;
78401 }else if( eStatementOp==0 ){
78402 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
78403 eStatementOp = SAVEPOINT_RELEASE;
78404 }else if( p->errorAction==OE_Abort ){
78405 eStatementOp = SAVEPOINT_ROLLBACK;
78406 }else{
78407 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
78408 sqlite3CloseSavepoints(db);
78409 db->autoCommit = 1;
78410 p->nChange = 0;
78411 }
78412 }
78413
78414 /* If eStatementOp is non-zero, then a statement transaction needs to
78415 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
78416 ** do so. If this operation returns an error, and the current statement
78417 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
78418 ** current statement error code.
78419 */
78420 if( eStatementOp ){
78421 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
78422 if( rc ){
78423 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
78424 p->rc = rc;
78425 sqlite3DbFree(db, p->zErrMsg);
78426 p->zErrMsg = 0;
78427 }
78428 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
78429 sqlite3CloseSavepoints(db);
78430 db->autoCommit = 1;
78431 p->nChange = 0;
78432 }
78433 }
78434
78435 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
78436 ** has been rolled back, update the database connection change-counter.
78437 */
78438 if( p->changeCntOn ){
78439 if( eStatementOp!=SAVEPOINT_ROLLBACK ){
78440 sqlite3VdbeSetChanges(db, p->nChange);
78441 }else{
78442 sqlite3VdbeSetChanges(db, 0);
78443 }
78444 p->nChange = 0;
78445 }
78446
78447 /* Release the locks */
78448 sqlite3VdbeLeave(p);
78449 }
78450
78451 /* We have successfully halted and closed the VM. Record this fact. */
78452 if( p->pc>=0 ){
78453 db->nVdbeActive--;
78454 if( !p->readOnly ) db->nVdbeWrite--;
78455 if( p->bIsReader ) db->nVdbeRead--;
78456 assert( db->nVdbeActive>=db->nVdbeRead );
78457 assert( db->nVdbeRead>=db->nVdbeWrite );
78458 assert( db->nVdbeWrite>=0 );
78459 }
78460 p->magic = VDBE_MAGIC_HALT;
78461 checkActiveVdbeCnt(db);
78462 if( db->mallocFailed ){
78463 p->rc = SQLITE_NOMEM_BKPT;
78464 }
78465
78466 /* If the auto-commit flag is set to true, then any locks that were held
78467 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
78468 ** to invoke any required unlock-notify callbacks.
78469 */
78470 if( db->autoCommit ){
78471 sqlite3ConnectionUnlocked(db);
78472 }
78473
78474 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
78475 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
78476}
78477
78478
78479/*
78480** Each VDBE holds the result of the most recent sqlite3_step() call
78481** in p->rc. This routine sets that result back to SQLITE_OK.
78482*/
78483SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
78484 p->rc = SQLITE_OK;
78485}
78486
78487/*
78488** Copy the error code and error message belonging to the VDBE passed
78489** as the first argument to its database handle (so that they will be
78490** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
78491**
78492** This function does not clear the VDBE error code or message, just
78493** copies them to the database handle.
78494*/
78495SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
78496 sqlite3 *db = p->db;
78497 int rc = p->rc;
78498 if( p->zErrMsg ){
78499 db->bBenignMalloc++;
78500 sqlite3BeginBenignMalloc();
78501 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
78502 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
78503 sqlite3EndBenignMalloc();
78504 db->bBenignMalloc--;
78505 }else if( db->pErr ){
78506 sqlite3ValueSetNull(db->pErr);
78507 }
78508 db->errCode = rc;
78509 return rc;
78510}
78511
78512#ifdef SQLITE_ENABLE_SQLLOG
78513/*
78514** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
78515** invoke it.
78516*/
78517static void vdbeInvokeSqllog(Vdbe *v){
78518 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
78519 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
78520 assert( v->db->init.busy==0 );
78521 if( zExpanded ){
78522 sqlite3GlobalConfig.xSqllog(
78523 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
78524 );
78525 sqlite3DbFree(v->db, zExpanded);
78526 }
78527 }
78528}
78529#else
78530# define vdbeInvokeSqllog(x)
78531#endif
78532
78533/*
78534** Clean up a VDBE after execution but do not delete the VDBE just yet.
78535** Write any error messages into *pzErrMsg. Return the result code.
78536**
78537** After this routine is run, the VDBE should be ready to be executed
78538** again.
78539**
78540** To look at it another way, this routine resets the state of the
78541** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
78542** VDBE_MAGIC_INIT.
78543*/
78544SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
78545#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
78546 int i;
78547#endif
78548
78549 sqlite3 *db;
78550 db = p->db;
78551
78552 /* If the VM did not run to completion or if it encountered an
78553 ** error, then it might not have been halted properly. So halt
78554 ** it now.
78555 */
78556 sqlite3VdbeHalt(p);
78557
78558 /* If the VDBE has been run even partially, then transfer the error code
78559 ** and error message from the VDBE into the main database structure. But
78560 ** if the VDBE has just been set to run but has not actually executed any
78561 ** instructions yet, leave the main database error information unchanged.
78562 */
78563 if( p->pc>=0 ){
78564 vdbeInvokeSqllog(p);
78565 sqlite3VdbeTransferError(p);
78566 if( p->runOnlyOnce ) p->expired = 1;
78567 }else if( p->rc && p->expired ){
78568 /* The expired flag was set on the VDBE before the first call
78569 ** to sqlite3_step(). For consistency (since sqlite3_step() was
78570 ** called), set the database error in this case as well.
78571 */
78572 sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
78573 }
78574
78575 /* Reset register contents and reclaim error message memory.
78576 */
78577#ifdef SQLITE_DEBUG
78578 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
78579 ** Vdbe.aMem[] arrays have already been cleaned up. */
78580 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
78581 if( p->aMem ){
78582 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
78583 }
78584#endif
78585 sqlite3DbFree(db, p->zErrMsg);
78586 p->zErrMsg = 0;
78587 p->pResultSet = 0;
78588#ifdef SQLITE_DEBUG
78589 p->nWrite = 0;
78590#endif
78591
78592 /* Save profiling information from this VDBE run.
78593 */
78594#ifdef VDBE_PROFILE
78595 {
78596 FILE *out = fopen("vdbe_profile.out", "a");
78597 if( out ){
78598 fprintf(out, "---- ");
78599 for(i=0; i<p->nOp; i++){
78600 fprintf(out, "%02x", p->aOp[i].opcode);
78601 }
78602 fprintf(out, "\n");
78603 if( p->zSql ){
78604 char c, pc = 0;
78605 fprintf(out, "-- ");
78606 for(i=0; (c = p->zSql[i])!=0; i++){
78607 if( pc=='\n' ) fprintf(out, "-- ");
78608 putc(c, out);
78609 pc = c;
78610 }
78611 if( pc!='\n' ) fprintf(out, "\n");
78612 }
78613 for(i=0; i<p->nOp; i++){
78614 char zHdr[100];
78615 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
78616 p->aOp[i].cnt,
78617 p->aOp[i].cycles,
78618 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
78619 );
78620 fprintf(out, "%s", zHdr);
78621 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
78622 }
78623 fclose(out);
78624 }
78625 }
78626#endif
78627 p->magic = VDBE_MAGIC_RESET;
78628 return p->rc & db->errMask;
78629}
78630
78631/*
78632** Clean up and delete a VDBE after execution. Return an integer which is
78633** the result code. Write any error message text into *pzErrMsg.
78634*/
78635SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
78636 int rc = SQLITE_OK;
78637 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
78638 rc = sqlite3VdbeReset(p);
78639 assert( (rc & p->db->errMask)==rc );
78640 }
78641 sqlite3VdbeDelete(p);
78642 return rc;
78643}
78644
78645/*
78646** If parameter iOp is less than zero, then invoke the destructor for
78647** all auxiliary data pointers currently cached by the VM passed as
78648** the first argument.
78649**
78650** Or, if iOp is greater than or equal to zero, then the destructor is
78651** only invoked for those auxiliary data pointers created by the user
78652** function invoked by the OP_Function opcode at instruction iOp of
78653** VM pVdbe, and only then if:
78654**
78655** * the associated function parameter is the 32nd or later (counting
78656** from left to right), or
78657**
78658** * the corresponding bit in argument mask is clear (where the first
78659** function parameter corresponds to bit 0 etc.).
78660*/
78661SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
78662 while( *pp ){
78663 AuxData *pAux = *pp;
78664 if( (iOp<0)
78665 || (pAux->iAuxOp==iOp
78666 && pAux->iAuxArg>=0
78667 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
78668 ){
78669 testcase( pAux->iAuxArg==31 );
78670 if( pAux->xDeleteAux ){
78671 pAux->xDeleteAux(pAux->pAux);
78672 }
78673 *pp = pAux->pNextAux;
78674 sqlite3DbFree(db, pAux);
78675 }else{
78676 pp= &pAux->pNextAux;
78677 }
78678 }
78679}
78680
78681/*
78682** Free all memory associated with the Vdbe passed as the second argument,
78683** except for object itself, which is preserved.
78684**
78685** The difference between this function and sqlite3VdbeDelete() is that
78686** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
78687** the database connection and frees the object itself.
78688*/
78689SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
78690 SubProgram *pSub, *pNext;
78691 assert( p->db==0 || p->db==db );
78692 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
78693 for(pSub=p->pProgram; pSub; pSub=pNext){
78694 pNext = pSub->pNext;
78695 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
78696 sqlite3DbFree(db, pSub);
78697 }
78698 if( p->magic!=VDBE_MAGIC_INIT ){
78699 releaseMemArray(p->aVar, p->nVar);
78700 sqlite3DbFree(db, p->pVList);
78701 sqlite3DbFree(db, p->pFree);
78702 }
78703 vdbeFreeOpArray(db, p->aOp, p->nOp);
78704 sqlite3DbFree(db, p->aColName);
78705 sqlite3DbFree(db, p->zSql);
78706#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
78707 {
78708 int i;
78709 for(i=0; i<p->nScan; i++){
78710 sqlite3DbFree(db, p->aScan[i].zName);
78711 }
78712 sqlite3DbFree(db, p->aScan);
78713 }
78714#endif
78715}
78716
78717/*
78718** Delete an entire VDBE.
78719*/
78720SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
78721 sqlite3 *db;
78722
78723 assert( p!=0 );
78724 db = p->db;
78725 assert( sqlite3_mutex_held(db->mutex) );
78726 sqlite3VdbeClearObject(db, p);
78727 if( p->pPrev ){
78728 p->pPrev->pNext = p->pNext;
78729 }else{
78730 assert( db->pVdbe==p );
78731 db->pVdbe = p->pNext;
78732 }
78733 if( p->pNext ){
78734 p->pNext->pPrev = p->pPrev;
78735 }
78736 p->magic = VDBE_MAGIC_DEAD;
78737 p->db = 0;
78738 sqlite3DbFreeNN(db, p);
78739}
78740
78741/*
78742** The cursor "p" has a pending seek operation that has not yet been
78743** carried out. Seek the cursor now. If an error occurs, return
78744** the appropriate error code.
78745*/
78746static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){
78747 int res, rc;
78748#ifdef SQLITE_TEST
78749 extern int sqlite3_search_count;
78750#endif
78751 assert( p->deferredMoveto );
78752 assert( p->isTable );
78753 assert( p->eCurType==CURTYPE_BTREE );
78754 rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
78755 if( rc ) return rc;
78756 if( res!=0 ) return SQLITE_CORRUPT_BKPT;
78757#ifdef SQLITE_TEST
78758 sqlite3_search_count++;
78759#endif
78760 p->deferredMoveto = 0;
78761 p->cacheStatus = CACHE_STALE;
78762 return SQLITE_OK;
78763}
78764
78765/*
78766** Something has moved cursor "p" out of place. Maybe the row it was
78767** pointed to was deleted out from under it. Or maybe the btree was
78768** rebalanced. Whatever the cause, try to restore "p" to the place it
78769** is supposed to be pointing. If the row was deleted out from under the
78770** cursor, set the cursor to point to a NULL row.
78771*/
78772static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
78773 int isDifferentRow, rc;
78774 assert( p->eCurType==CURTYPE_BTREE );
78775 assert( p->uc.pCursor!=0 );
78776 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
78777 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
78778 p->cacheStatus = CACHE_STALE;
78779 if( isDifferentRow ) p->nullRow = 1;
78780 return rc;
78781}
78782
78783/*
78784** Check to ensure that the cursor is valid. Restore the cursor
78785** if need be. Return any I/O error from the restore operation.
78786*/
78787SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){
78788 assert( p->eCurType==CURTYPE_BTREE );
78789 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
78790 return handleMovedCursor(p);
78791 }
78792 return SQLITE_OK;
78793}
78794
78795/*
78796** Make sure the cursor p is ready to read or write the row to which it
78797** was last positioned. Return an error code if an OOM fault or I/O error
78798** prevents us from positioning the cursor to its correct position.
78799**
78800** If a MoveTo operation is pending on the given cursor, then do that
78801** MoveTo now. If no move is pending, check to see if the row has been
78802** deleted out from under the cursor and if it has, mark the row as
78803** a NULL row.
78804**
78805** If the cursor is already pointing to the correct row and that row has
78806** not been deleted out from under the cursor, then this routine is a no-op.
78807*/
78808SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){
78809 VdbeCursor *p = *pp;
78810 assert( p->eCurType==CURTYPE_BTREE || p->eCurType==CURTYPE_PSEUDO );
78811 if( p->deferredMoveto ){
78812 int iMap;
78813 if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){
78814 *pp = p->pAltCursor;
78815 *piCol = iMap - 1;
78816 return SQLITE_OK;
78817 }
78818 return handleDeferredMoveto(p);
78819 }
78820 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
78821 return handleMovedCursor(p);
78822 }
78823 return SQLITE_OK;
78824}
78825
78826/*
78827** The following functions:
78828**
78829** sqlite3VdbeSerialType()
78830** sqlite3VdbeSerialTypeLen()
78831** sqlite3VdbeSerialLen()
78832** sqlite3VdbeSerialPut()
78833** sqlite3VdbeSerialGet()
78834**
78835** encapsulate the code that serializes values for storage in SQLite
78836** data and index records. Each serialized value consists of a
78837** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
78838** integer, stored as a varint.
78839**
78840** In an SQLite index record, the serial type is stored directly before
78841** the blob of data that it corresponds to. In a table record, all serial
78842** types are stored at the start of the record, and the blobs of data at
78843** the end. Hence these functions allow the caller to handle the
78844** serial-type and data blob separately.
78845**
78846** The following table describes the various storage classes for data:
78847**
78848** serial type bytes of data type
78849** -------------- --------------- ---------------
78850** 0 0 NULL
78851** 1 1 signed integer
78852** 2 2 signed integer
78853** 3 3 signed integer
78854** 4 4 signed integer
78855** 5 6 signed integer
78856** 6 8 signed integer
78857** 7 8 IEEE float
78858** 8 0 Integer constant 0
78859** 9 0 Integer constant 1
78860** 10,11 reserved for expansion
78861** N>=12 and even (N-12)/2 BLOB
78862** N>=13 and odd (N-13)/2 text
78863**
78864** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
78865** of SQLite will not understand those serial types.
78866*/
78867
78868/*
78869** Return the serial-type for the value stored in pMem.
78870*/
78871SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
78872 int flags = pMem->flags;
78873 u32 n;
78874
78875 assert( pLen!=0 );
78876 if( flags&MEM_Null ){
78877 *pLen = 0;
78878 return 0;
78879 }
78880 if( flags&MEM_Int ){
78881 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
78882# define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
78883 i64 i = pMem->u.i;
78884 u64 u;
78885 if( i<0 ){
78886 u = ~i;
78887 }else{
78888 u = i;
78889 }
78890 if( u<=127 ){
78891 if( (i&1)==i && file_format>=4 ){
78892 *pLen = 0;
78893 return 8+(u32)u;
78894 }else{
78895 *pLen = 1;
78896 return 1;
78897 }
78898 }
78899 if( u<=32767 ){ *pLen = 2; return 2; }
78900 if( u<=8388607 ){ *pLen = 3; return 3; }
78901 if( u<=2147483647 ){ *pLen = 4; return 4; }
78902 if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
78903 *pLen = 8;
78904 return 6;
78905 }
78906 if( flags&MEM_Real ){
78907 *pLen = 8;
78908 return 7;
78909 }
78910 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
78911 assert( pMem->n>=0 );
78912 n = (u32)pMem->n;
78913 if( flags & MEM_Zero ){
78914 n += pMem->u.nZero;
78915 }
78916 *pLen = n;
78917 return ((n*2) + 12 + ((flags&MEM_Str)!=0));
78918}
78919
78920/*
78921** The sizes for serial types less than 128
78922*/
78923static const u8 sqlite3SmallTypeSizes[] = {
78924 /* 0 1 2 3 4 5 6 7 8 9 */
78925/* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0,
78926/* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
78927/* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
78928/* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
78929/* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
78930/* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
78931/* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
78932/* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
78933/* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
78934/* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
78935/* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
78936/* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
78937/* 120 */ 54, 54, 55, 55, 56, 56, 57, 57
78938};
78939
78940/*
78941** Return the length of the data corresponding to the supplied serial-type.
78942*/
78943SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
78944 if( serial_type>=128 ){
78945 return (serial_type-12)/2;
78946 }else{
78947 assert( serial_type<12
78948 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
78949 return sqlite3SmallTypeSizes[serial_type];
78950 }
78951}
78952SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
78953 assert( serial_type<128 );
78954 return sqlite3SmallTypeSizes[serial_type];
78955}
78956
78957/*
78958** If we are on an architecture with mixed-endian floating
78959** points (ex: ARM7) then swap the lower 4 bytes with the
78960** upper 4 bytes. Return the result.
78961**
78962** For most architectures, this is a no-op.
78963**
78964** (later): It is reported to me that the mixed-endian problem
78965** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
78966** that early versions of GCC stored the two words of a 64-bit
78967** float in the wrong order. And that error has been propagated
78968** ever since. The blame is not necessarily with GCC, though.
78969** GCC might have just copying the problem from a prior compiler.
78970** I am also told that newer versions of GCC that follow a different
78971** ABI get the byte order right.
78972**
78973** Developers using SQLite on an ARM7 should compile and run their
78974** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
78975** enabled, some asserts below will ensure that the byte order of
78976** floating point values is correct.
78977**
78978** (2007-08-30) Frank van Vugt has studied this problem closely
78979** and has send his findings to the SQLite developers. Frank
78980** writes that some Linux kernels offer floating point hardware
78981** emulation that uses only 32-bit mantissas instead of a full
78982** 48-bits as required by the IEEE standard. (This is the
78983** CONFIG_FPE_FASTFPE option.) On such systems, floating point
78984** byte swapping becomes very complicated. To avoid problems,
78985** the necessary byte swapping is carried out using a 64-bit integer
78986** rather than a 64-bit float. Frank assures us that the code here
78987** works for him. We, the developers, have no way to independently
78988** verify this, but Frank seems to know what he is talking about
78989** so we trust him.
78990*/
78991#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
78992static u64 floatSwap(u64 in){
78993 union {
78994 u64 r;
78995 u32 i[2];
78996 } u;
78997 u32 t;
78998
78999 u.r = in;
79000 t = u.i[0];
79001 u.i[0] = u.i[1];
79002 u.i[1] = t;
79003 return u.r;
79004}
79005# define swapMixedEndianFloat(X) X = floatSwap(X)
79006#else
79007# define swapMixedEndianFloat(X)
79008#endif
79009
79010/*
79011** Write the serialized data blob for the value stored in pMem into
79012** buf. It is assumed that the caller has allocated sufficient space.
79013** Return the number of bytes written.
79014**
79015** nBuf is the amount of space left in buf[]. The caller is responsible
79016** for allocating enough space to buf[] to hold the entire field, exclusive
79017** of the pMem->u.nZero bytes for a MEM_Zero value.
79018**
79019** Return the number of bytes actually written into buf[]. The number
79020** of bytes in the zero-filled tail is included in the return value only
79021** if those bytes were zeroed in buf[].
79022*/
79023SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
79024 u32 len;
79025
79026 /* Integer and Real */
79027 if( serial_type<=7 && serial_type>0 ){
79028 u64 v;
79029 u32 i;
79030 if( serial_type==7 ){
79031 assert( sizeof(v)==sizeof(pMem->u.r) );
79032 memcpy(&v, &pMem->u.r, sizeof(v));
79033 swapMixedEndianFloat(v);
79034 }else{
79035 v = pMem->u.i;
79036 }
79037 len = i = sqlite3SmallTypeSizes[serial_type];
79038 assert( i>0 );
79039 do{
79040 buf[--i] = (u8)(v&0xFF);
79041 v >>= 8;
79042 }while( i );
79043 return len;
79044 }
79045
79046 /* String or blob */
79047 if( serial_type>=12 ){
79048 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
79049 == (int)sqlite3VdbeSerialTypeLen(serial_type) );
79050 len = pMem->n;
79051 if( len>0 ) memcpy(buf, pMem->z, len);
79052 return len;
79053 }
79054
79055 /* NULL or constants 0 or 1 */
79056 return 0;
79057}
79058
79059/* Input "x" is a sequence of unsigned characters that represent a
79060** big-endian integer. Return the equivalent native integer
79061*/
79062#define ONE_BYTE_INT(x) ((i8)(x)[0])
79063#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
79064#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
79065#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
79066#define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
79067
79068/*
79069** Deserialize the data blob pointed to by buf as serial type serial_type
79070** and store the result in pMem. Return the number of bytes read.
79071**
79072** This function is implemented as two separate routines for performance.
79073** The few cases that require local variables are broken out into a separate
79074** routine so that in most cases the overhead of moving the stack pointer
79075** is avoided.
79076*/
79077static u32 SQLITE_NOINLINE serialGet(
79078 const unsigned char *buf, /* Buffer to deserialize from */
79079 u32 serial_type, /* Serial type to deserialize */
79080 Mem *pMem /* Memory cell to write value into */
79081){
79082 u64 x = FOUR_BYTE_UINT(buf);
79083 u32 y = FOUR_BYTE_UINT(buf+4);
79084 x = (x<<32) + y;
79085 if( serial_type==6 ){
79086 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
79087 ** twos-complement integer. */
79088 pMem->u.i = *(i64*)&x;
79089 pMem->flags = MEM_Int;
79090 testcase( pMem->u.i<0 );
79091 }else{
79092 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
79093 ** floating point number. */
79094#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
79095 /* Verify that integers and floating point values use the same
79096 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
79097 ** defined that 64-bit floating point values really are mixed
79098 ** endian.
79099 */
79100 static const u64 t1 = ((u64)0x3ff00000)<<32;
79101 static const double r1 = 1.0;
79102 u64 t2 = t1;
79103 swapMixedEndianFloat(t2);
79104 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
79105#endif
79106 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
79107 swapMixedEndianFloat(x);
79108 memcpy(&pMem->u.r, &x, sizeof(x));
79109 pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real;
79110 }
79111 return 8;
79112}
79113SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
79114 const unsigned char *buf, /* Buffer to deserialize from */
79115 u32 serial_type, /* Serial type to deserialize */
79116 Mem *pMem /* Memory cell to write value into */
79117){
79118 switch( serial_type ){
79119 case 10: { /* Internal use only: NULL with virtual table
79120 ** UPDATE no-change flag set */
79121 pMem->flags = MEM_Null|MEM_Zero;
79122 pMem->n = 0;
79123 pMem->u.nZero = 0;
79124 break;
79125 }
79126 case 11: /* Reserved for future use */
79127 case 0: { /* Null */
79128 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
79129 pMem->flags = MEM_Null;
79130 break;
79131 }
79132 case 1: {
79133 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
79134 ** integer. */
79135 pMem->u.i = ONE_BYTE_INT(buf);
79136 pMem->flags = MEM_Int;
79137 testcase( pMem->u.i<0 );
79138 return 1;
79139 }
79140 case 2: { /* 2-byte signed integer */
79141 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
79142 ** twos-complement integer. */
79143 pMem->u.i = TWO_BYTE_INT(buf);
79144 pMem->flags = MEM_Int;
79145 testcase( pMem->u.i<0 );
79146 return 2;
79147 }
79148 case 3: { /* 3-byte signed integer */
79149 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
79150 ** twos-complement integer. */
79151 pMem->u.i = THREE_BYTE_INT(buf);
79152 pMem->flags = MEM_Int;
79153 testcase( pMem->u.i<0 );
79154 return 3;
79155 }
79156 case 4: { /* 4-byte signed integer */
79157 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
79158 ** twos-complement integer. */
79159 pMem->u.i = FOUR_BYTE_INT(buf);
79160#ifdef __HP_cc
79161 /* Work around a sign-extension bug in the HP compiler for HP/UX */
79162 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
79163#endif
79164 pMem->flags = MEM_Int;
79165 testcase( pMem->u.i<0 );
79166 return 4;
79167 }
79168 case 5: { /* 6-byte signed integer */
79169 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
79170 ** twos-complement integer. */
79171 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
79172 pMem->flags = MEM_Int;
79173 testcase( pMem->u.i<0 );
79174 return 6;
79175 }
79176 case 6: /* 8-byte signed integer */
79177 case 7: { /* IEEE floating point */
79178 /* These use local variables, so do them in a separate routine
79179 ** to avoid having to move the frame pointer in the common case */
79180 return serialGet(buf,serial_type,pMem);
79181 }
79182 case 8: /* Integer 0 */
79183 case 9: { /* Integer 1 */
79184 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
79185 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
79186 pMem->u.i = serial_type-8;
79187 pMem->flags = MEM_Int;
79188 return 0;
79189 }
79190 default: {
79191 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
79192 ** length.
79193 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
79194 ** (N-13)/2 bytes in length. */
79195 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
79196 pMem->z = (char *)buf;
79197 pMem->n = (serial_type-12)/2;
79198 pMem->flags = aFlag[serial_type&1];
79199 return pMem->n;
79200 }
79201 }
79202 return 0;
79203}
79204/*
79205** This routine is used to allocate sufficient space for an UnpackedRecord
79206** structure large enough to be used with sqlite3VdbeRecordUnpack() if
79207** the first argument is a pointer to KeyInfo structure pKeyInfo.
79208**
79209** The space is either allocated using sqlite3DbMallocRaw() or from within
79210** the unaligned buffer passed via the second and third arguments (presumably
79211** stack space). If the former, then *ppFree is set to a pointer that should
79212** be eventually freed by the caller using sqlite3DbFree(). Or, if the
79213** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
79214** before returning.
79215**
79216** If an OOM error occurs, NULL is returned.
79217*/
79218SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
79219 KeyInfo *pKeyInfo /* Description of the record */
79220){
79221 UnpackedRecord *p; /* Unpacked record to return */
79222 int nByte; /* Number of bytes required for *p */
79223 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
79224 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
79225 if( !p ) return 0;
79226 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
79227 assert( pKeyInfo->aSortOrder!=0 );
79228 p->pKeyInfo = pKeyInfo;
79229 p->nField = pKeyInfo->nKeyField + 1;
79230 return p;
79231}
79232
79233/*
79234** Given the nKey-byte encoding of a record in pKey[], populate the
79235** UnpackedRecord structure indicated by the fourth argument with the
79236** contents of the decoded record.
79237*/
79238SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
79239 KeyInfo *pKeyInfo, /* Information about the record format */
79240 int nKey, /* Size of the binary record */
79241 const void *pKey, /* The binary record */
79242 UnpackedRecord *p /* Populate this structure before returning. */
79243){
79244 const unsigned char *aKey = (const unsigned char *)pKey;
79245 int d;
79246 u32 idx; /* Offset in aKey[] to read from */
79247 u16 u; /* Unsigned loop counter */
79248 u32 szHdr;
79249 Mem *pMem = p->aMem;
79250
79251 p->default_rc = 0;
79252 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
79253 idx = getVarint32(aKey, szHdr);
79254 d = szHdr;
79255 u = 0;
79256 while( idx<szHdr && d<=nKey ){
79257 u32 serial_type;
79258
79259 idx += getVarint32(&aKey[idx], serial_type);
79260 pMem->enc = pKeyInfo->enc;
79261 pMem->db = pKeyInfo->db;
79262 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
79263 pMem->szMalloc = 0;
79264 pMem->z = 0;
79265 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
79266 pMem++;
79267 if( (++u)>=p->nField ) break;
79268 }
79269 assert( u<=pKeyInfo->nKeyField + 1 );
79270 p->nField = u;
79271}
79272
79273#ifdef SQLITE_DEBUG
79274/*
79275** This function compares two index or table record keys in the same way
79276** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
79277** this function deserializes and compares values using the
79278** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
79279** in assert() statements to ensure that the optimized code in
79280** sqlite3VdbeRecordCompare() returns results with these two primitives.
79281**
79282** Return true if the result of comparison is equivalent to desiredResult.
79283** Return false if there is a disagreement.
79284*/
79285static int vdbeRecordCompareDebug(
79286 int nKey1, const void *pKey1, /* Left key */
79287 const UnpackedRecord *pPKey2, /* Right key */
79288 int desiredResult /* Correct answer */
79289){
79290 u32 d1; /* Offset into aKey[] of next data element */
79291 u32 idx1; /* Offset into aKey[] of next header element */
79292 u32 szHdr1; /* Number of bytes in header */
79293 int i = 0;
79294 int rc = 0;
79295 const unsigned char *aKey1 = (const unsigned char *)pKey1;
79296 KeyInfo *pKeyInfo;
79297 Mem mem1;
79298
79299 pKeyInfo = pPKey2->pKeyInfo;
79300 if( pKeyInfo->db==0 ) return 1;
79301 mem1.enc = pKeyInfo->enc;
79302 mem1.db = pKeyInfo->db;
79303 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
79304 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
79305
79306 /* Compilers may complain that mem1.u.i is potentially uninitialized.
79307 ** We could initialize it, as shown here, to silence those complaints.
79308 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
79309 ** the unnecessary initialization has a measurable negative performance
79310 ** impact, since this routine is a very high runner. And so, we choose
79311 ** to ignore the compiler warnings and leave this variable uninitialized.
79312 */
79313 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
79314
79315 idx1 = getVarint32(aKey1, szHdr1);
79316 if( szHdr1>98307 ) return SQLITE_CORRUPT;
79317 d1 = szHdr1;
79318 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
79319 assert( pKeyInfo->aSortOrder!=0 );
79320 assert( pKeyInfo->nKeyField>0 );
79321 assert( idx1<=szHdr1 || CORRUPT_DB );
79322 do{
79323 u32 serial_type1;
79324
79325 /* Read the serial types for the next element in each key. */
79326 idx1 += getVarint32( aKey1+idx1, serial_type1 );
79327
79328 /* Verify that there is enough key space remaining to avoid
79329 ** a buffer overread. The "d1+serial_type1+2" subexpression will
79330 ** always be greater than or equal to the amount of required key space.
79331 ** Use that approximation to avoid the more expensive call to
79332 ** sqlite3VdbeSerialTypeLen() in the common case.
79333 */
79334 if( d1+serial_type1+2>(u32)nKey1
79335 && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
79336 ){
79337 break;
79338 }
79339
79340 /* Extract the values to be compared.
79341 */
79342 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
79343
79344 /* Do the comparison
79345 */
79346 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
79347 if( rc!=0 ){
79348 assert( mem1.szMalloc==0 ); /* See comment below */
79349 if( pKeyInfo->aSortOrder[i] ){
79350 rc = -rc; /* Invert the result for DESC sort order. */
79351 }
79352 goto debugCompareEnd;
79353 }
79354 i++;
79355 }while( idx1<szHdr1 && i<pPKey2->nField );
79356
79357 /* No memory allocation is ever used on mem1. Prove this using
79358 ** the following assert(). If the assert() fails, it indicates a
79359 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
79360 */
79361 assert( mem1.szMalloc==0 );
79362
79363 /* rc==0 here means that one of the keys ran out of fields and
79364 ** all the fields up to that point were equal. Return the default_rc
79365 ** value. */
79366 rc = pPKey2->default_rc;
79367
79368debugCompareEnd:
79369 if( desiredResult==0 && rc==0 ) return 1;
79370 if( desiredResult<0 && rc<0 ) return 1;
79371 if( desiredResult>0 && rc>0 ) return 1;
79372 if( CORRUPT_DB ) return 1;
79373 if( pKeyInfo->db->mallocFailed ) return 1;
79374 return 0;
79375}
79376#endif
79377
79378#ifdef SQLITE_DEBUG
79379/*
79380** Count the number of fields (a.k.a. columns) in the record given by
79381** pKey,nKey. The verify that this count is less than or equal to the
79382** limit given by pKeyInfo->nAllField.
79383**
79384** If this constraint is not satisfied, it means that the high-speed
79385** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
79386** not work correctly. If this assert() ever fires, it probably means
79387** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed
79388** incorrectly.
79389*/
79390static void vdbeAssertFieldCountWithinLimits(
79391 int nKey, const void *pKey, /* The record to verify */
79392 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */
79393){
79394 int nField = 0;
79395 u32 szHdr;
79396 u32 idx;
79397 u32 notUsed;
79398 const unsigned char *aKey = (const unsigned char*)pKey;
79399
79400 if( CORRUPT_DB ) return;
79401 idx = getVarint32(aKey, szHdr);
79402 assert( nKey>=0 );
79403 assert( szHdr<=(u32)nKey );
79404 while( idx<szHdr ){
79405 idx += getVarint32(aKey+idx, notUsed);
79406 nField++;
79407 }
79408 assert( nField <= pKeyInfo->nAllField );
79409}
79410#else
79411# define vdbeAssertFieldCountWithinLimits(A,B,C)
79412#endif
79413
79414/*
79415** Both *pMem1 and *pMem2 contain string values. Compare the two values
79416** using the collation sequence pColl. As usual, return a negative , zero
79417** or positive value if *pMem1 is less than, equal to or greater than
79418** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
79419*/
79420static int vdbeCompareMemString(
79421 const Mem *pMem1,
79422 const Mem *pMem2,
79423 const CollSeq *pColl,
79424 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */
79425){
79426 if( pMem1->enc==pColl->enc ){
79427 /* The strings are already in the correct encoding. Call the
79428 ** comparison function directly */
79429 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
79430 }else{
79431 int rc;
79432 const void *v1, *v2;
79433 Mem c1;
79434 Mem c2;
79435 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
79436 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
79437 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
79438 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
79439 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
79440 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
79441 if( (v1==0 || v2==0) ){
79442 if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT;
79443 rc = 0;
79444 }else{
79445 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
79446 }
79447 sqlite3VdbeMemRelease(&c1);
79448 sqlite3VdbeMemRelease(&c2);
79449 return rc;
79450 }
79451}
79452
79453/*
79454** The input pBlob is guaranteed to be a Blob that is not marked
79455** with MEM_Zero. Return true if it could be a zero-blob.
79456*/
79457static int isAllZero(const char *z, int n){
79458 int i;
79459 for(i=0; i<n; i++){
79460 if( z[i] ) return 0;
79461 }
79462 return 1;
79463}
79464
79465/*
79466** Compare two blobs. Return negative, zero, or positive if the first
79467** is less than, equal to, or greater than the second, respectively.
79468** If one blob is a prefix of the other, then the shorter is the lessor.
79469*/
79470SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
79471 int c;
79472 int n1 = pB1->n;
79473 int n2 = pB2->n;
79474
79475 /* It is possible to have a Blob value that has some non-zero content
79476 ** followed by zero content. But that only comes up for Blobs formed
79477 ** by the OP_MakeRecord opcode, and such Blobs never get passed into
79478 ** sqlite3MemCompare(). */
79479 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
79480 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
79481
79482 if( (pB1->flags|pB2->flags) & MEM_Zero ){
79483 if( pB1->flags & pB2->flags & MEM_Zero ){
79484 return pB1->u.nZero - pB2->u.nZero;
79485 }else if( pB1->flags & MEM_Zero ){
79486 if( !isAllZero(pB2->z, pB2->n) ) return -1;
79487 return pB1->u.nZero - n2;
79488 }else{
79489 if( !isAllZero(pB1->z, pB1->n) ) return +1;
79490 return n1 - pB2->u.nZero;
79491 }
79492 }
79493 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
79494 if( c ) return c;
79495 return n1 - n2;
79496}
79497
79498/*
79499** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
79500** number. Return negative, zero, or positive if the first (i64) is less than,
79501** equal to, or greater than the second (double).
79502*/
79503static int sqlite3IntFloatCompare(i64 i, double r){
79504 if( sizeof(LONGDOUBLE_TYPE)>8 ){
79505 LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i;
79506 if( x<r ) return -1;
79507 if( x>r ) return +1;
79508 return 0;
79509 }else{
79510 i64 y;
79511 double s;
79512 if( r<-9223372036854775808.0 ) return +1;
79513 if( r>=9223372036854775808.0 ) return -1;
79514 y = (i64)r;
79515 if( i<y ) return -1;
79516 if( i>y ) return +1;
79517 s = (double)i;
79518 if( s<r ) return -1;
79519 if( s>r ) return +1;
79520 return 0;
79521 }
79522}
79523
79524/*
79525** Compare the values contained by the two memory cells, returning
79526** negative, zero or positive if pMem1 is less than, equal to, or greater
79527** than pMem2. Sorting order is NULL's first, followed by numbers (integers
79528** and reals) sorted numerically, followed by text ordered by the collating
79529** sequence pColl and finally blob's ordered by memcmp().
79530**
79531** Two NULL values are considered equal by this function.
79532*/
79533SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
79534 int f1, f2;
79535 int combined_flags;
79536
79537 f1 = pMem1->flags;
79538 f2 = pMem2->flags;
79539 combined_flags = f1|f2;
79540 assert( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) );
79541
79542 /* If one value is NULL, it is less than the other. If both values
79543 ** are NULL, return 0.
79544 */
79545 if( combined_flags&MEM_Null ){
79546 return (f2&MEM_Null) - (f1&MEM_Null);
79547 }
79548
79549 /* At least one of the two values is a number
79550 */
79551 if( combined_flags&(MEM_Int|MEM_Real) ){
79552 if( (f1 & f2 & MEM_Int)!=0 ){
79553 if( pMem1->u.i < pMem2->u.i ) return -1;
79554 if( pMem1->u.i > pMem2->u.i ) return +1;
79555 return 0;
79556 }
79557 if( (f1 & f2 & MEM_Real)!=0 ){
79558 if( pMem1->u.r < pMem2->u.r ) return -1;
79559 if( pMem1->u.r > pMem2->u.r ) return +1;
79560 return 0;
79561 }
79562 if( (f1&MEM_Int)!=0 ){
79563 if( (f2&MEM_Real)!=0 ){
79564 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
79565 }else{
79566 return -1;
79567 }
79568 }
79569 if( (f1&MEM_Real)!=0 ){
79570 if( (f2&MEM_Int)!=0 ){
79571 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
79572 }else{
79573 return -1;
79574 }
79575 }
79576 return +1;
79577 }
79578
79579 /* If one value is a string and the other is a blob, the string is less.
79580 ** If both are strings, compare using the collating functions.
79581 */
79582 if( combined_flags&MEM_Str ){
79583 if( (f1 & MEM_Str)==0 ){
79584 return 1;
79585 }
79586 if( (f2 & MEM_Str)==0 ){
79587 return -1;
79588 }
79589
79590 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
79591 assert( pMem1->enc==SQLITE_UTF8 ||
79592 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
79593
79594 /* The collation sequence must be defined at this point, even if
79595 ** the user deletes the collation sequence after the vdbe program is
79596 ** compiled (this was not always the case).
79597 */
79598 assert( !pColl || pColl->xCmp );
79599
79600 if( pColl ){
79601 return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
79602 }
79603 /* If a NULL pointer was passed as the collate function, fall through
79604 ** to the blob case and use memcmp(). */
79605 }
79606
79607 /* Both values must be blobs. Compare using memcmp(). */
79608 return sqlite3BlobCompare(pMem1, pMem2);
79609}
79610
79611
79612/*
79613** The first argument passed to this function is a serial-type that
79614** corresponds to an integer - all values between 1 and 9 inclusive
79615** except 7. The second points to a buffer containing an integer value
79616** serialized according to serial_type. This function deserializes
79617** and returns the value.
79618*/
79619static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
79620 u32 y;
79621 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
79622 switch( serial_type ){
79623 case 0:
79624 case 1:
79625 testcase( aKey[0]&0x80 );
79626 return ONE_BYTE_INT(aKey);
79627 case 2:
79628 testcase( aKey[0]&0x80 );
79629 return TWO_BYTE_INT(aKey);
79630 case 3:
79631 testcase( aKey[0]&0x80 );
79632 return THREE_BYTE_INT(aKey);
79633 case 4: {
79634 testcase( aKey[0]&0x80 );
79635 y = FOUR_BYTE_UINT(aKey);
79636 return (i64)*(int*)&y;
79637 }
79638 case 5: {
79639 testcase( aKey[0]&0x80 );
79640 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
79641 }
79642 case 6: {
79643 u64 x = FOUR_BYTE_UINT(aKey);
79644 testcase( aKey[0]&0x80 );
79645 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
79646 return (i64)*(i64*)&x;
79647 }
79648 }
79649
79650 return (serial_type - 8);
79651}
79652
79653/*
79654** This function compares the two table rows or index records
79655** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
79656** or positive integer if key1 is less than, equal to or
79657** greater than key2. The {nKey1, pKey1} key must be a blob
79658** created by the OP_MakeRecord opcode of the VDBE. The pPKey2
79659** key must be a parsed key such as obtained from
79660** sqlite3VdbeParseRecord.
79661**
79662** If argument bSkip is non-zero, it is assumed that the caller has already
79663** determined that the first fields of the keys are equal.
79664**
79665** Key1 and Key2 do not have to contain the same number of fields. If all
79666** fields that appear in both keys are equal, then pPKey2->default_rc is
79667** returned.
79668**
79669** If database corruption is discovered, set pPKey2->errCode to
79670** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
79671** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
79672** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
79673*/
79674SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
79675 int nKey1, const void *pKey1, /* Left key */
79676 UnpackedRecord *pPKey2, /* Right key */
79677 int bSkip /* If true, skip the first field */
79678){
79679 u32 d1; /* Offset into aKey[] of next data element */
79680 int i; /* Index of next field to compare */
79681 u32 szHdr1; /* Size of record header in bytes */
79682 u32 idx1; /* Offset of first type in header */
79683 int rc = 0; /* Return value */
79684 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
79685 KeyInfo *pKeyInfo;
79686 const unsigned char *aKey1 = (const unsigned char *)pKey1;
79687 Mem mem1;
79688
79689 /* If bSkip is true, then the caller has already determined that the first
79690 ** two elements in the keys are equal. Fix the various stack variables so
79691 ** that this routine begins comparing at the second field. */
79692 if( bSkip ){
79693 u32 s1;
79694 idx1 = 1 + getVarint32(&aKey1[1], s1);
79695 szHdr1 = aKey1[0];
79696 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
79697 i = 1;
79698 pRhs++;
79699 }else{
79700 idx1 = getVarint32(aKey1, szHdr1);
79701 d1 = szHdr1;
79702 if( d1>(unsigned)nKey1 ){
79703 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
79704 return 0; /* Corruption */
79705 }
79706 i = 0;
79707 }
79708
79709 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
79710 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
79711 || CORRUPT_DB );
79712 assert( pPKey2->pKeyInfo->aSortOrder!=0 );
79713 assert( pPKey2->pKeyInfo->nKeyField>0 );
79714 assert( idx1<=szHdr1 || CORRUPT_DB );
79715 do{
79716 u32 serial_type;
79717
79718 /* RHS is an integer */
79719 if( pRhs->flags & MEM_Int ){
79720 serial_type = aKey1[idx1];
79721 testcase( serial_type==12 );
79722 if( serial_type>=10 ){
79723 rc = +1;
79724 }else if( serial_type==0 ){
79725 rc = -1;
79726 }else if( serial_type==7 ){
79727 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
79728 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
79729 }else{
79730 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
79731 i64 rhs = pRhs->u.i;
79732 if( lhs<rhs ){
79733 rc = -1;
79734 }else if( lhs>rhs ){
79735 rc = +1;
79736 }
79737 }
79738 }
79739
79740 /* RHS is real */
79741 else if( pRhs->flags & MEM_Real ){
79742 serial_type = aKey1[idx1];
79743 if( serial_type>=10 ){
79744 /* Serial types 12 or greater are strings and blobs (greater than
79745 ** numbers). Types 10 and 11 are currently "reserved for future
79746 ** use", so it doesn't really matter what the results of comparing
79747 ** them to numberic values are. */
79748 rc = +1;
79749 }else if( serial_type==0 ){
79750 rc = -1;
79751 }else{
79752 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
79753 if( serial_type==7 ){
79754 if( mem1.u.r<pRhs->u.r ){
79755 rc = -1;
79756 }else if( mem1.u.r>pRhs->u.r ){
79757 rc = +1;
79758 }
79759 }else{
79760 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
79761 }
79762 }
79763 }
79764
79765 /* RHS is a string */
79766 else if( pRhs->flags & MEM_Str ){
79767 getVarint32(&aKey1[idx1], serial_type);
79768 testcase( serial_type==12 );
79769 if( serial_type<12 ){
79770 rc = -1;
79771 }else if( !(serial_type & 0x01) ){
79772 rc = +1;
79773 }else{
79774 mem1.n = (serial_type - 12) / 2;
79775 testcase( (d1+mem1.n)==(unsigned)nKey1 );
79776 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
79777 if( (d1+mem1.n) > (unsigned)nKey1 ){
79778 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
79779 return 0; /* Corruption */
79780 }else if( (pKeyInfo = pPKey2->pKeyInfo)->aColl[i] ){
79781 mem1.enc = pKeyInfo->enc;
79782 mem1.db = pKeyInfo->db;
79783 mem1.flags = MEM_Str;
79784 mem1.z = (char*)&aKey1[d1];
79785 rc = vdbeCompareMemString(
79786 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
79787 );
79788 }else{
79789 int nCmp = MIN(mem1.n, pRhs->n);
79790 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
79791 if( rc==0 ) rc = mem1.n - pRhs->n;
79792 }
79793 }
79794 }
79795
79796 /* RHS is a blob */
79797 else if( pRhs->flags & MEM_Blob ){
79798 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
79799 getVarint32(&aKey1[idx1], serial_type);
79800 testcase( serial_type==12 );
79801 if( serial_type<12 || (serial_type & 0x01) ){
79802 rc = -1;
79803 }else{
79804 int nStr = (serial_type - 12) / 2;
79805 testcase( (d1+nStr)==(unsigned)nKey1 );
79806 testcase( (d1+nStr+1)==(unsigned)nKey1 );
79807 if( (d1+nStr) > (unsigned)nKey1 ){
79808 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
79809 return 0; /* Corruption */
79810 }else if( pRhs->flags & MEM_Zero ){
79811 if( !isAllZero((const char*)&aKey1[d1],nStr) ){
79812 rc = 1;
79813 }else{
79814 rc = nStr - pRhs->u.nZero;
79815 }
79816 }else{
79817 int nCmp = MIN(nStr, pRhs->n);
79818 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
79819 if( rc==0 ) rc = nStr - pRhs->n;
79820 }
79821 }
79822 }
79823
79824 /* RHS is null */
79825 else{
79826 serial_type = aKey1[idx1];
79827 rc = (serial_type!=0);
79828 }
79829
79830 if( rc!=0 ){
79831 if( pPKey2->pKeyInfo->aSortOrder[i] ){
79832 rc = -rc;
79833 }
79834 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
79835 assert( mem1.szMalloc==0 ); /* See comment below */
79836 return rc;
79837 }
79838
79839 i++;
79840 if( i==pPKey2->nField ) break;
79841 pRhs++;
79842 d1 += sqlite3VdbeSerialTypeLen(serial_type);
79843 idx1 += sqlite3VarintLen(serial_type);
79844 }while( idx1<(unsigned)szHdr1 && d1<=(unsigned)nKey1 );
79845
79846 /* No memory allocation is ever used on mem1. Prove this using
79847 ** the following assert(). If the assert() fails, it indicates a
79848 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
79849 assert( mem1.szMalloc==0 );
79850
79851 /* rc==0 here means that one or both of the keys ran out of fields and
79852 ** all the fields up to that point were equal. Return the default_rc
79853 ** value. */
79854 assert( CORRUPT_DB
79855 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
79856 || pPKey2->pKeyInfo->db->mallocFailed
79857 );
79858 pPKey2->eqSeen = 1;
79859 return pPKey2->default_rc;
79860}
79861SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
79862 int nKey1, const void *pKey1, /* Left key */
79863 UnpackedRecord *pPKey2 /* Right key */
79864){
79865 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
79866}
79867
79868
79869/*
79870** This function is an optimized version of sqlite3VdbeRecordCompare()
79871** that (a) the first field of pPKey2 is an integer, and (b) the
79872** size-of-header varint at the start of (pKey1/nKey1) fits in a single
79873** byte (i.e. is less than 128).
79874**
79875** To avoid concerns about buffer overreads, this routine is only used
79876** on schemas where the maximum valid header size is 63 bytes or less.
79877*/
79878static int vdbeRecordCompareInt(
79879 int nKey1, const void *pKey1, /* Left key */
79880 UnpackedRecord *pPKey2 /* Right key */
79881){
79882 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
79883 int serial_type = ((const u8*)pKey1)[1];
79884 int res;
79885 u32 y;
79886 u64 x;
79887 i64 v;
79888 i64 lhs;
79889
79890 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
79891 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB );
79892 switch( serial_type ){
79893 case 1: { /* 1-byte signed integer */
79894 lhs = ONE_BYTE_INT(aKey);
79895 testcase( lhs<0 );
79896 break;
79897 }
79898 case 2: { /* 2-byte signed integer */
79899 lhs = TWO_BYTE_INT(aKey);
79900 testcase( lhs<0 );
79901 break;
79902 }
79903 case 3: { /* 3-byte signed integer */
79904 lhs = THREE_BYTE_INT(aKey);
79905 testcase( lhs<0 );
79906 break;
79907 }
79908 case 4: { /* 4-byte signed integer */
79909 y = FOUR_BYTE_UINT(aKey);
79910 lhs = (i64)*(int*)&y;
79911 testcase( lhs<0 );
79912 break;
79913 }
79914 case 5: { /* 6-byte signed integer */
79915 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
79916 testcase( lhs<0 );
79917 break;
79918 }
79919 case 6: { /* 8-byte signed integer */
79920 x = FOUR_BYTE_UINT(aKey);
79921 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
79922 lhs = *(i64*)&x;
79923 testcase( lhs<0 );
79924 break;
79925 }
79926 case 8:
79927 lhs = 0;
79928 break;
79929 case 9:
79930 lhs = 1;
79931 break;
79932
79933 /* This case could be removed without changing the results of running
79934 ** this code. Including it causes gcc to generate a faster switch
79935 ** statement (since the range of switch targets now starts at zero and
79936 ** is contiguous) but does not cause any duplicate code to be generated
79937 ** (as gcc is clever enough to combine the two like cases). Other
79938 ** compilers might be similar. */
79939 case 0: case 7:
79940 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
79941
79942 default:
79943 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
79944 }
79945
79946 v = pPKey2->aMem[0].u.i;
79947 if( v>lhs ){
79948 res = pPKey2->r1;
79949 }else if( v<lhs ){
79950 res = pPKey2->r2;
79951 }else if( pPKey2->nField>1 ){
79952 /* The first fields of the two keys are equal. Compare the trailing
79953 ** fields. */
79954 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
79955 }else{
79956 /* The first fields of the two keys are equal and there are no trailing
79957 ** fields. Return pPKey2->default_rc in this case. */
79958 res = pPKey2->default_rc;
79959 pPKey2->eqSeen = 1;
79960 }
79961
79962 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
79963 return res;
79964}
79965
79966/*
79967** This function is an optimized version of sqlite3VdbeRecordCompare()
79968** that (a) the first field of pPKey2 is a string, that (b) the first field
79969** uses the collation sequence BINARY and (c) that the size-of-header varint
79970** at the start of (pKey1/nKey1) fits in a single byte.
79971*/
79972static int vdbeRecordCompareString(
79973 int nKey1, const void *pKey1, /* Left key */
79974 UnpackedRecord *pPKey2 /* Right key */
79975){
79976 const u8 *aKey1 = (const u8*)pKey1;
79977 int serial_type;
79978 int res;
79979
79980 assert( pPKey2->aMem[0].flags & MEM_Str );
79981 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
79982 getVarint32(&aKey1[1], serial_type);
79983 if( serial_type<12 ){
79984 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
79985 }else if( !(serial_type & 0x01) ){
79986 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
79987 }else{
79988 int nCmp;
79989 int nStr;
79990 int szHdr = aKey1[0];
79991
79992 nStr = (serial_type-12) / 2;
79993 if( (szHdr + nStr) > nKey1 ){
79994 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
79995 return 0; /* Corruption */
79996 }
79997 nCmp = MIN( pPKey2->aMem[0].n, nStr );
79998 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
79999
80000 if( res==0 ){
80001 res = nStr - pPKey2->aMem[0].n;
80002 if( res==0 ){
80003 if( pPKey2->nField>1 ){
80004 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
80005 }else{
80006 res = pPKey2->default_rc;
80007 pPKey2->eqSeen = 1;
80008 }
80009 }else if( res>0 ){
80010 res = pPKey2->r2;
80011 }else{
80012 res = pPKey2->r1;
80013 }
80014 }else if( res>0 ){
80015 res = pPKey2->r2;
80016 }else{
80017 res = pPKey2->r1;
80018 }
80019 }
80020
80021 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
80022 || CORRUPT_DB
80023 || pPKey2->pKeyInfo->db->mallocFailed
80024 );
80025 return res;
80026}
80027
80028/*
80029** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
80030** suitable for comparing serialized records to the unpacked record passed
80031** as the only argument.
80032*/
80033SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
80034 /* varintRecordCompareInt() and varintRecordCompareString() both assume
80035 ** that the size-of-header varint that occurs at the start of each record
80036 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
80037 ** also assumes that it is safe to overread a buffer by at least the
80038 ** maximum possible legal header size plus 8 bytes. Because there is
80039 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
80040 ** buffer passed to varintRecordCompareInt() this makes it convenient to
80041 ** limit the size of the header to 64 bytes in cases where the first field
80042 ** is an integer.
80043 **
80044 ** The easiest way to enforce this limit is to consider only records with
80045 ** 13 fields or less. If the first field is an integer, the maximum legal
80046 ** header size is (12*5 + 1 + 1) bytes. */
80047 if( p->pKeyInfo->nAllField<=13 ){
80048 int flags = p->aMem[0].flags;
80049 if( p->pKeyInfo->aSortOrder[0] ){
80050 p->r1 = 1;
80051 p->r2 = -1;
80052 }else{
80053 p->r1 = -1;
80054 p->r2 = 1;
80055 }
80056 if( (flags & MEM_Int) ){
80057 return vdbeRecordCompareInt;
80058 }
80059 testcase( flags & MEM_Real );
80060 testcase( flags & MEM_Null );
80061 testcase( flags & MEM_Blob );
80062 if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
80063 assert( flags & MEM_Str );
80064 return vdbeRecordCompareString;
80065 }
80066 }
80067
80068 return sqlite3VdbeRecordCompare;
80069}
80070
80071/*
80072** pCur points at an index entry created using the OP_MakeRecord opcode.
80073** Read the rowid (the last field in the record) and store it in *rowid.
80074** Return SQLITE_OK if everything works, or an error code otherwise.
80075**
80076** pCur might be pointing to text obtained from a corrupt database file.
80077** So the content cannot be trusted. Do appropriate checks on the content.
80078*/
80079SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
80080 i64 nCellKey = 0;
80081 int rc;
80082 u32 szHdr; /* Size of the header */
80083 u32 typeRowid; /* Serial type of the rowid */
80084 u32 lenRowid; /* Size of the rowid */
80085 Mem m, v;
80086
80087 /* Get the size of the index entry. Only indices entries of less
80088 ** than 2GiB are support - anything large must be database corruption.
80089 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
80090 ** this code can safely assume that nCellKey is 32-bits
80091 */
80092 assert( sqlite3BtreeCursorIsValid(pCur) );
80093 nCellKey = sqlite3BtreePayloadSize(pCur);
80094 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
80095
80096 /* Read in the complete content of the index entry */
80097 sqlite3VdbeMemInit(&m, db, 0);
80098 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
80099 if( rc ){
80100 return rc;
80101 }
80102
80103 /* The index entry must begin with a header size */
80104 (void)getVarint32((u8*)m.z, szHdr);
80105 testcase( szHdr==3 );
80106 testcase( szHdr==m.n );
80107 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
80108 goto idx_rowid_corruption;
80109 }
80110
80111 /* The last field of the index should be an integer - the ROWID.
80112 ** Verify that the last entry really is an integer. */
80113 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
80114 testcase( typeRowid==1 );
80115 testcase( typeRowid==2 );
80116 testcase( typeRowid==3 );
80117 testcase( typeRowid==4 );
80118 testcase( typeRowid==5 );
80119 testcase( typeRowid==6 );
80120 testcase( typeRowid==8 );
80121 testcase( typeRowid==9 );
80122 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
80123 goto idx_rowid_corruption;
80124 }
80125 lenRowid = sqlite3SmallTypeSizes[typeRowid];
80126 testcase( (u32)m.n==szHdr+lenRowid );
80127 if( unlikely((u32)m.n<szHdr+lenRowid) ){
80128 goto idx_rowid_corruption;
80129 }
80130
80131 /* Fetch the integer off the end of the index record */
80132 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
80133 *rowid = v.u.i;
80134 sqlite3VdbeMemRelease(&m);
80135 return SQLITE_OK;
80136
80137 /* Jump here if database corruption is detected after m has been
80138 ** allocated. Free the m object and return SQLITE_CORRUPT. */
80139idx_rowid_corruption:
80140 testcase( m.szMalloc!=0 );
80141 sqlite3VdbeMemRelease(&m);
80142 return SQLITE_CORRUPT_BKPT;
80143}
80144
80145/*
80146** Compare the key of the index entry that cursor pC is pointing to against
80147** the key string in pUnpacked. Write into *pRes a number
80148** that is negative, zero, or positive if pC is less than, equal to,
80149** or greater than pUnpacked. Return SQLITE_OK on success.
80150**
80151** pUnpacked is either created without a rowid or is truncated so that it
80152** omits the rowid at the end. The rowid at the end of the index entry
80153** is ignored as well. Hence, this routine only compares the prefixes
80154** of the keys prior to the final rowid, not the entire key.
80155*/
80156SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
80157 sqlite3 *db, /* Database connection */
80158 VdbeCursor *pC, /* The cursor to compare against */
80159 UnpackedRecord *pUnpacked, /* Unpacked version of key */
80160 int *res /* Write the comparison result here */
80161){
80162 i64 nCellKey = 0;
80163 int rc;
80164 BtCursor *pCur;
80165 Mem m;
80166
80167 assert( pC->eCurType==CURTYPE_BTREE );
80168 pCur = pC->uc.pCursor;
80169 assert( sqlite3BtreeCursorIsValid(pCur) );
80170 nCellKey = sqlite3BtreePayloadSize(pCur);
80171 /* nCellKey will always be between 0 and 0xffffffff because of the way
80172 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
80173 if( nCellKey<=0 || nCellKey>0x7fffffff ){
80174 *res = 0;
80175 return SQLITE_CORRUPT_BKPT;
80176 }
80177 sqlite3VdbeMemInit(&m, db, 0);
80178 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
80179 if( rc ){
80180 return rc;
80181 }
80182 *res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, pUnpacked, 0);
80183 sqlite3VdbeMemRelease(&m);
80184 return SQLITE_OK;
80185}
80186
80187/*
80188** This routine sets the value to be returned by subsequent calls to
80189** sqlite3_changes() on the database handle 'db'.
80190*/
80191SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
80192 assert( sqlite3_mutex_held(db->mutex) );
80193 db->nChange = nChange;
80194 db->nTotalChange += nChange;
80195}
80196
80197/*
80198** Set a flag in the vdbe to update the change counter when it is finalised
80199** or reset.
80200*/
80201SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
80202 v->changeCntOn = 1;
80203}
80204
80205/*
80206** Mark every prepared statement associated with a database connection
80207** as expired.
80208**
80209** An expired statement means that recompilation of the statement is
80210** recommend. Statements expire when things happen that make their
80211** programs obsolete. Removing user-defined functions or collating
80212** sequences, or changing an authorization function are the types of
80213** things that make prepared statements obsolete.
80214**
80215** If iCode is 1, then expiration is advisory. The statement should
80216** be reprepared before being restarted, but if it is already running
80217** it is allowed to run to completion.
80218**
80219** Internally, this function just sets the Vdbe.expired flag on all
80220** prepared statements. The flag is set to 1 for an immediate expiration
80221** and set to 2 for an advisory expiration.
80222*/
80223SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
80224 Vdbe *p;
80225 for(p = db->pVdbe; p; p=p->pNext){
80226 p->expired = iCode+1;
80227 }
80228}
80229
80230/*
80231** Return the database associated with the Vdbe.
80232*/
80233SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
80234 return v->db;
80235}
80236
80237/*
80238** Return the SQLITE_PREPARE flags for a Vdbe.
80239*/
80240SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe *v){
80241 return v->prepFlags;
80242}
80243
80244/*
80245** Return a pointer to an sqlite3_value structure containing the value bound
80246** parameter iVar of VM v. Except, if the value is an SQL NULL, return
80247** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
80248** constants) to the value before returning it.
80249**
80250** The returned value must be freed by the caller using sqlite3ValueFree().
80251*/
80252SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
80253 assert( iVar>0 );
80254 if( v ){
80255 Mem *pMem = &v->aVar[iVar-1];
80256 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
80257 if( 0==(pMem->flags & MEM_Null) ){
80258 sqlite3_value *pRet = sqlite3ValueNew(v->db);
80259 if( pRet ){
80260 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
80261 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
80262 }
80263 return pRet;
80264 }
80265 }
80266 return 0;
80267}
80268
80269/*
80270** Configure SQL variable iVar so that binding a new value to it signals
80271** to sqlite3_reoptimize() that re-preparing the statement may result
80272** in a better query plan.
80273*/
80274SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
80275 assert( iVar>0 );
80276 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
80277 if( iVar>=32 ){
80278 v->expmask |= 0x80000000;
80279 }else{
80280 v->expmask |= ((u32)1 << (iVar-1));
80281 }
80282}
80283
80284/*
80285** Cause a function to throw an error if it was call from OP_PureFunc
80286** rather than OP_Function.
80287**
80288** OP_PureFunc means that the function must be deterministic, and should
80289** throw an error if it is given inputs that would make it non-deterministic.
80290** This routine is invoked by date/time functions that use non-deterministic
80291** features such as 'now'.
80292*/
80293SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context *pCtx){
80294#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
80295 if( pCtx->pVdbe==0 ) return 1;
80296#endif
80297 if( pCtx->pVdbe->aOp[pCtx->iOp].opcode==OP_PureFunc ){
80298 sqlite3_result_error(pCtx,
80299 "non-deterministic function in index expression or CHECK constraint",
80300 -1);
80301 return 0;
80302 }
80303 return 1;
80304}
80305
80306#ifndef SQLITE_OMIT_VIRTUALTABLE
80307/*
80308** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
80309** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
80310** in memory obtained from sqlite3DbMalloc).
80311*/
80312SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
80313 if( pVtab->zErrMsg ){
80314 sqlite3 *db = p->db;
80315 sqlite3DbFree(db, p->zErrMsg);
80316 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
80317 sqlite3_free(pVtab->zErrMsg);
80318 pVtab->zErrMsg = 0;
80319 }
80320}
80321#endif /* SQLITE_OMIT_VIRTUALTABLE */
80322
80323#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
80324
80325/*
80326** If the second argument is not NULL, release any allocations associated
80327** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
80328** structure itself, using sqlite3DbFree().
80329**
80330** This function is used to free UnpackedRecord structures allocated by
80331** the vdbeUnpackRecord() function found in vdbeapi.c.
80332*/
80333static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
80334 if( p ){
80335 int i;
80336 for(i=0; i<nField; i++){
80337 Mem *pMem = &p->aMem[i];
80338 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
80339 }
80340 sqlite3DbFreeNN(db, p);
80341 }
80342}
80343#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
80344
80345#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
80346/*
80347** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
80348** then cursor passed as the second argument should point to the row about
80349** to be update or deleted. If the application calls sqlite3_preupdate_old(),
80350** the required value will be read from the row the cursor points to.
80351*/
80352SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(
80353 Vdbe *v, /* Vdbe pre-update hook is invoked by */
80354 VdbeCursor *pCsr, /* Cursor to grab old.* values from */
80355 int op, /* SQLITE_INSERT, UPDATE or DELETE */
80356 const char *zDb, /* Database name */
80357 Table *pTab, /* Modified table */
80358 i64 iKey1, /* Initial key value */
80359 int iReg /* Register for new.* record */
80360){
80361 sqlite3 *db = v->db;
80362 i64 iKey2;
80363 PreUpdate preupdate;
80364 const char *zTbl = pTab->zName;
80365 static const u8 fakeSortOrder = 0;
80366
80367 assert( db->pPreUpdate==0 );
80368 memset(&preupdate, 0, sizeof(PreUpdate));
80369 if( HasRowid(pTab)==0 ){
80370 iKey1 = iKey2 = 0;
80371 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
80372 }else{
80373 if( op==SQLITE_UPDATE ){
80374 iKey2 = v->aMem[iReg].u.i;
80375 }else{
80376 iKey2 = iKey1;
80377 }
80378 }
80379
80380 assert( pCsr->nField==pTab->nCol
80381 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
80382 );
80383
80384 preupdate.v = v;
80385 preupdate.pCsr = pCsr;
80386 preupdate.op = op;
80387 preupdate.iNewReg = iReg;
80388 preupdate.keyinfo.db = db;
80389 preupdate.keyinfo.enc = ENC(db);
80390 preupdate.keyinfo.nKeyField = pTab->nCol;
80391 preupdate.keyinfo.aSortOrder = (u8*)&fakeSortOrder;
80392 preupdate.iKey1 = iKey1;
80393 preupdate.iKey2 = iKey2;
80394 preupdate.pTab = pTab;
80395
80396 db->pPreUpdate = &preupdate;
80397 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
80398 db->pPreUpdate = 0;
80399 sqlite3DbFree(db, preupdate.aRecord);
80400 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
80401 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
80402 if( preupdate.aNew ){
80403 int i;
80404 for(i=0; i<pCsr->nField; i++){
80405 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
80406 }
80407 sqlite3DbFreeNN(db, preupdate.aNew);
80408 }
80409}
80410#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
80411
80412/************** End of vdbeaux.c *********************************************/
80413/************** Begin file vdbeapi.c *****************************************/
80414/*
80415** 2004 May 26
80416**
80417** The author disclaims copyright to this source code. In place of
80418** a legal notice, here is a blessing:
80419**
80420** May you do good and not evil.
80421** May you find forgiveness for yourself and forgive others.
80422** May you share freely, never taking more than you give.
80423**
80424*************************************************************************
80425**
80426** This file contains code use to implement APIs that are part of the
80427** VDBE.
80428*/
80429/* #include "sqliteInt.h" */
80430/* #include "vdbeInt.h" */
80431
80432#ifndef SQLITE_OMIT_DEPRECATED
80433/*
80434** Return TRUE (non-zero) of the statement supplied as an argument needs
80435** to be recompiled. A statement needs to be recompiled whenever the
80436** execution environment changes in a way that would alter the program
80437** that sqlite3_prepare() generates. For example, if new functions or
80438** collating sequences are registered or if an authorizer function is
80439** added or changed.
80440*/
80441SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
80442 Vdbe *p = (Vdbe*)pStmt;
80443 return p==0 || p->expired;
80444}
80445#endif
80446
80447/*
80448** Check on a Vdbe to make sure it has not been finalized. Log
80449** an error and return true if it has been finalized (or is otherwise
80450** invalid). Return false if it is ok.
80451*/
80452static int vdbeSafety(Vdbe *p){
80453 if( p->db==0 ){
80454 sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
80455 return 1;
80456 }else{
80457 return 0;
80458 }
80459}
80460static int vdbeSafetyNotNull(Vdbe *p){
80461 if( p==0 ){
80462 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
80463 return 1;
80464 }else{
80465 return vdbeSafety(p);
80466 }
80467}
80468
80469#ifndef SQLITE_OMIT_TRACE
80470/*
80471** Invoke the profile callback. This routine is only called if we already
80472** know that the profile callback is defined and needs to be invoked.
80473*/
80474static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
80475 sqlite3_int64 iNow;
80476 sqlite3_int64 iElapse;
80477 assert( p->startTime>0 );
80478 assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 );
80479 assert( db->init.busy==0 );
80480 assert( p->zSql!=0 );
80481 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
80482 iElapse = (iNow - p->startTime)*1000000;
80483 if( db->xProfile ){
80484 db->xProfile(db->pProfileArg, p->zSql, iElapse);
80485 }
80486 if( db->mTrace & SQLITE_TRACE_PROFILE ){
80487 db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
80488 }
80489 p->startTime = 0;
80490}
80491/*
80492** The checkProfileCallback(DB,P) macro checks to see if a profile callback
80493** is needed, and it invokes the callback if it is needed.
80494*/
80495# define checkProfileCallback(DB,P) \
80496 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
80497#else
80498# define checkProfileCallback(DB,P) /*no-op*/
80499#endif
80500
80501/*
80502** The following routine destroys a virtual machine that is created by
80503** the sqlite3_compile() routine. The integer returned is an SQLITE_
80504** success/failure code that describes the result of executing the virtual
80505** machine.
80506**
80507** This routine sets the error code and string returned by
80508** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
80509*/
80510SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
80511 int rc;
80512 if( pStmt==0 ){
80513 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
80514 ** pointer is a harmless no-op. */
80515 rc = SQLITE_OK;
80516 }else{
80517 Vdbe *v = (Vdbe*)pStmt;
80518 sqlite3 *db = v->db;
80519 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
80520 sqlite3_mutex_enter(db->mutex);
80521 checkProfileCallback(db, v);
80522 rc = sqlite3VdbeFinalize(v);
80523 rc = sqlite3ApiExit(db, rc);
80524 sqlite3LeaveMutexAndCloseZombie(db);
80525 }
80526 return rc;
80527}
80528
80529/*
80530** Terminate the current execution of an SQL statement and reset it
80531** back to its starting state so that it can be reused. A success code from
80532** the prior execution is returned.
80533**
80534** This routine sets the error code and string returned by
80535** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
80536*/
80537SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
80538 int rc;
80539 if( pStmt==0 ){
80540 rc = SQLITE_OK;
80541 }else{
80542 Vdbe *v = (Vdbe*)pStmt;
80543 sqlite3 *db = v->db;
80544 sqlite3_mutex_enter(db->mutex);
80545 checkProfileCallback(db, v);
80546 rc = sqlite3VdbeReset(v);
80547 sqlite3VdbeRewind(v);
80548 assert( (rc & (db->errMask))==rc );
80549 rc = sqlite3ApiExit(db, rc);
80550 sqlite3_mutex_leave(db->mutex);
80551 }
80552 return rc;
80553}
80554
80555/*
80556** Set all the parameters in the compiled SQL statement to NULL.
80557*/
80558SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
80559 int i;
80560 int rc = SQLITE_OK;
80561 Vdbe *p = (Vdbe*)pStmt;
80562#if SQLITE_THREADSAFE
80563 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
80564#endif
80565 sqlite3_mutex_enter(mutex);
80566 for(i=0; i<p->nVar; i++){
80567 sqlite3VdbeMemRelease(&p->aVar[i]);
80568 p->aVar[i].flags = MEM_Null;
80569 }
80570 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
80571 if( p->expmask ){
80572 p->expired = 1;
80573 }
80574 sqlite3_mutex_leave(mutex);
80575 return rc;
80576}
80577
80578
80579/**************************** sqlite3_value_ *******************************
80580** The following routines extract information from a Mem or sqlite3_value
80581** structure.
80582*/
80583SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
80584 Mem *p = (Mem*)pVal;
80585 if( p->flags & (MEM_Blob|MEM_Str) ){
80586 if( ExpandBlob(p)!=SQLITE_OK ){
80587 assert( p->flags==MEM_Null && p->z==0 );
80588 return 0;
80589 }
80590 p->flags |= MEM_Blob;
80591 return p->n ? p->z : 0;
80592 }else{
80593 return sqlite3_value_text(pVal);
80594 }
80595}
80596SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
80597 return sqlite3ValueBytes(pVal, SQLITE_UTF8);
80598}
80599SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
80600 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
80601}
80602SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
80603 return sqlite3VdbeRealValue((Mem*)pVal);
80604}
80605SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
80606 return (int)sqlite3VdbeIntValue((Mem*)pVal);
80607}
80608SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
80609 return sqlite3VdbeIntValue((Mem*)pVal);
80610}
80611SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
80612 Mem *pMem = (Mem*)pVal;
80613 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
80614}
80615SQLITE_API void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
80616 Mem *p = (Mem*)pVal;
80617 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
80618 (MEM_Null|MEM_Term|MEM_Subtype)
80619 && zPType!=0
80620 && p->eSubtype=='p'
80621 && strcmp(p->u.zPType, zPType)==0
80622 ){
80623 return (void*)p->z;
80624 }else{
80625 return 0;
80626 }
80627}
80628SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
80629 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
80630}
80631#ifndef SQLITE_OMIT_UTF16
80632SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
80633 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
80634}
80635SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
80636 return sqlite3ValueText(pVal, SQLITE_UTF16BE);
80637}
80638SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
80639 return sqlite3ValueText(pVal, SQLITE_UTF16LE);
80640}
80641#endif /* SQLITE_OMIT_UTF16 */
80642/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
80643** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
80644** point number string BLOB NULL
80645*/
80646SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
80647 static const u8 aType[] = {
80648 SQLITE_BLOB, /* 0x00 */
80649 SQLITE_NULL, /* 0x01 */
80650 SQLITE_TEXT, /* 0x02 */
80651 SQLITE_NULL, /* 0x03 */
80652 SQLITE_INTEGER, /* 0x04 */
80653 SQLITE_NULL, /* 0x05 */
80654 SQLITE_INTEGER, /* 0x06 */
80655 SQLITE_NULL, /* 0x07 */
80656 SQLITE_FLOAT, /* 0x08 */
80657 SQLITE_NULL, /* 0x09 */
80658 SQLITE_FLOAT, /* 0x0a */
80659 SQLITE_NULL, /* 0x0b */
80660 SQLITE_INTEGER, /* 0x0c */
80661 SQLITE_NULL, /* 0x0d */
80662 SQLITE_INTEGER, /* 0x0e */
80663 SQLITE_NULL, /* 0x0f */
80664 SQLITE_BLOB, /* 0x10 */
80665 SQLITE_NULL, /* 0x11 */
80666 SQLITE_TEXT, /* 0x12 */
80667 SQLITE_NULL, /* 0x13 */
80668 SQLITE_INTEGER, /* 0x14 */
80669 SQLITE_NULL, /* 0x15 */
80670 SQLITE_INTEGER, /* 0x16 */
80671 SQLITE_NULL, /* 0x17 */
80672 SQLITE_FLOAT, /* 0x18 */
80673 SQLITE_NULL, /* 0x19 */
80674 SQLITE_FLOAT, /* 0x1a */
80675 SQLITE_NULL, /* 0x1b */
80676 SQLITE_INTEGER, /* 0x1c */
80677 SQLITE_NULL, /* 0x1d */
80678 SQLITE_INTEGER, /* 0x1e */
80679 SQLITE_NULL, /* 0x1f */
80680 };
80681 return aType[pVal->flags&MEM_AffMask];
80682}
80683
80684/* Return true if a parameter to xUpdate represents an unchanged column */
80685SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
80686 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
80687}
80688
80689/* Make a copy of an sqlite3_value object
80690*/
80691SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
80692 sqlite3_value *pNew;
80693 if( pOrig==0 ) return 0;
80694 pNew = sqlite3_malloc( sizeof(*pNew) );
80695 if( pNew==0 ) return 0;
80696 memset(pNew, 0, sizeof(*pNew));
80697 memcpy(pNew, pOrig, MEMCELLSIZE);
80698 pNew->flags &= ~MEM_Dyn;
80699 pNew->db = 0;
80700 if( pNew->flags&(MEM_Str|MEM_Blob) ){
80701 pNew->flags &= ~(MEM_Static|MEM_Dyn);
80702 pNew->flags |= MEM_Ephem;
80703 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
80704 sqlite3ValueFree(pNew);
80705 pNew = 0;
80706 }
80707 }
80708 return pNew;
80709}
80710
80711/* Destroy an sqlite3_value object previously obtained from
80712** sqlite3_value_dup().
80713*/
80714SQLITE_API void sqlite3_value_free(sqlite3_value *pOld){
80715 sqlite3ValueFree(pOld);
80716}
80717
80718
80719/**************************** sqlite3_result_ *******************************
80720** The following routines are used by user-defined functions to specify
80721** the function result.
80722**
80723** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
80724** result as a string or blob but if the string or blob is too large, it
80725** then sets the error code to SQLITE_TOOBIG
80726**
80727** The invokeValueDestructor(P,X) routine invokes destructor function X()
80728** on value P is not going to be used and need to be destroyed.
80729*/
80730static void setResultStrOrError(
80731 sqlite3_context *pCtx, /* Function context */
80732 const char *z, /* String pointer */
80733 int n, /* Bytes in string, or negative */
80734 u8 enc, /* Encoding of z. 0 for BLOBs */
80735 void (*xDel)(void*) /* Destructor function */
80736){
80737 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
80738 sqlite3_result_error_toobig(pCtx);
80739 }
80740}
80741static int invokeValueDestructor(
80742 const void *p, /* Value to destroy */
80743 void (*xDel)(void*), /* The destructor */
80744 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
80745){
80746 assert( xDel!=SQLITE_DYNAMIC );
80747 if( xDel==0 ){
80748 /* noop */
80749 }else if( xDel==SQLITE_TRANSIENT ){
80750 /* noop */
80751 }else{
80752 xDel((void*)p);
80753 }
80754 if( pCtx ) sqlite3_result_error_toobig(pCtx);
80755 return SQLITE_TOOBIG;
80756}
80757SQLITE_API void sqlite3_result_blob(
80758 sqlite3_context *pCtx,
80759 const void *z,
80760 int n,
80761 void (*xDel)(void *)
80762){
80763 assert( n>=0 );
80764 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80765 setResultStrOrError(pCtx, z, n, 0, xDel);
80766}
80767SQLITE_API void sqlite3_result_blob64(
80768 sqlite3_context *pCtx,
80769 const void *z,
80770 sqlite3_uint64 n,
80771 void (*xDel)(void *)
80772){
80773 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80774 assert( xDel!=SQLITE_DYNAMIC );
80775 if( n>0x7fffffff ){
80776 (void)invokeValueDestructor(z, xDel, pCtx);
80777 }else{
80778 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
80779 }
80780}
80781SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
80782 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80783 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
80784}
80785SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
80786 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80787 pCtx->isError = SQLITE_ERROR;
80788 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
80789}
80790#ifndef SQLITE_OMIT_UTF16
80791SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
80792 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80793 pCtx->isError = SQLITE_ERROR;
80794 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
80795}
80796#endif
80797SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
80798 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80799 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
80800}
80801SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
80802 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80803 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
80804}
80805SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
80806 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80807 sqlite3VdbeMemSetNull(pCtx->pOut);
80808}
80809SQLITE_API void sqlite3_result_pointer(
80810 sqlite3_context *pCtx,
80811 void *pPtr,
80812 const char *zPType,
80813 void (*xDestructor)(void*)
80814){
80815 Mem *pOut = pCtx->pOut;
80816 assert( sqlite3_mutex_held(pOut->db->mutex) );
80817 sqlite3VdbeMemRelease(pOut);
80818 pOut->flags = MEM_Null;
80819 sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);
80820}
80821SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
80822 Mem *pOut = pCtx->pOut;
80823 assert( sqlite3_mutex_held(pOut->db->mutex) );
80824 pOut->eSubtype = eSubtype & 0xff;
80825 pOut->flags |= MEM_Subtype;
80826}
80827SQLITE_API void sqlite3_result_text(
80828 sqlite3_context *pCtx,
80829 const char *z,
80830 int n,
80831 void (*xDel)(void *)
80832){
80833 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80834 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
80835}
80836SQLITE_API void sqlite3_result_text64(
80837 sqlite3_context *pCtx,
80838 const char *z,
80839 sqlite3_uint64 n,
80840 void (*xDel)(void *),
80841 unsigned char enc
80842){
80843 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80844 assert( xDel!=SQLITE_DYNAMIC );
80845 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
80846 if( n>0x7fffffff ){
80847 (void)invokeValueDestructor(z, xDel, pCtx);
80848 }else{
80849 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
80850 }
80851}
80852#ifndef SQLITE_OMIT_UTF16
80853SQLITE_API void sqlite3_result_text16(
80854 sqlite3_context *pCtx,
80855 const void *z,
80856 int n,
80857 void (*xDel)(void *)
80858){
80859 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80860 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
80861}
80862SQLITE_API void sqlite3_result_text16be(
80863 sqlite3_context *pCtx,
80864 const void *z,
80865 int n,
80866 void (*xDel)(void *)
80867){
80868 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80869 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
80870}
80871SQLITE_API void sqlite3_result_text16le(
80872 sqlite3_context *pCtx,
80873 const void *z,
80874 int n,
80875 void (*xDel)(void *)
80876){
80877 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80878 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
80879}
80880#endif /* SQLITE_OMIT_UTF16 */
80881SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
80882 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80883 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
80884}
80885SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
80886 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80887 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
80888}
80889SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
80890 Mem *pOut = pCtx->pOut;
80891 assert( sqlite3_mutex_held(pOut->db->mutex) );
80892 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
80893 return SQLITE_TOOBIG;
80894 }
80895 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
80896 return SQLITE_OK;
80897}
80898SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
80899 pCtx->isError = errCode ? errCode : -1;
80900#ifdef SQLITE_DEBUG
80901 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
80902#endif
80903 if( pCtx->pOut->flags & MEM_Null ){
80904 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
80905 SQLITE_UTF8, SQLITE_STATIC);
80906 }
80907}
80908
80909/* Force an SQLITE_TOOBIG error. */
80910SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
80911 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80912 pCtx->isError = SQLITE_TOOBIG;
80913 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
80914 SQLITE_UTF8, SQLITE_STATIC);
80915}
80916
80917/* An SQLITE_NOMEM error. */
80918SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
80919 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
80920 sqlite3VdbeMemSetNull(pCtx->pOut);
80921 pCtx->isError = SQLITE_NOMEM_BKPT;
80922 sqlite3OomFault(pCtx->pOut->db);
80923}
80924
80925/*
80926** This function is called after a transaction has been committed. It
80927** invokes callbacks registered with sqlite3_wal_hook() as required.
80928*/
80929static int doWalCallbacks(sqlite3 *db){
80930 int rc = SQLITE_OK;
80931#ifndef SQLITE_OMIT_WAL
80932 int i;
80933 for(i=0; i<db->nDb; i++){
80934 Btree *pBt = db->aDb[i].pBt;
80935 if( pBt ){
80936 int nEntry;
80937 sqlite3BtreeEnter(pBt);
80938 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
80939 sqlite3BtreeLeave(pBt);
80940 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
80941 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
80942 }
80943 }
80944 }
80945#endif
80946 return rc;
80947}
80948
80949
80950/*
80951** Execute the statement pStmt, either until a row of data is ready, the
80952** statement is completely executed or an error occurs.
80953**
80954** This routine implements the bulk of the logic behind the sqlite_step()
80955** API. The only thing omitted is the automatic recompile if a
80956** schema change has occurred. That detail is handled by the
80957** outer sqlite3_step() wrapper procedure.
80958*/
80959static int sqlite3Step(Vdbe *p){
80960 sqlite3 *db;
80961 int rc;
80962
80963 assert(p);
80964 if( p->magic!=VDBE_MAGIC_RUN ){
80965 /* We used to require that sqlite3_reset() be called before retrying
80966 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
80967 ** with version 3.7.0, we changed this so that sqlite3_reset() would
80968 ** be called automatically instead of throwing the SQLITE_MISUSE error.
80969 ** This "automatic-reset" change is not technically an incompatibility,
80970 ** since any application that receives an SQLITE_MISUSE is broken by
80971 ** definition.
80972 **
80973 ** Nevertheless, some published applications that were originally written
80974 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
80975 ** returns, and those were broken by the automatic-reset change. As a
80976 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
80977 ** legacy behavior of returning SQLITE_MISUSE for cases where the
80978 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
80979 ** or SQLITE_BUSY error.
80980 */
80981#ifdef SQLITE_OMIT_AUTORESET
80982 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
80983 sqlite3_reset((sqlite3_stmt*)p);
80984 }else{
80985 return SQLITE_MISUSE_BKPT;
80986 }
80987#else
80988 sqlite3_reset((sqlite3_stmt*)p);
80989#endif
80990 }
80991
80992 /* Check that malloc() has not failed. If it has, return early. */
80993 db = p->db;
80994 if( db->mallocFailed ){
80995 p->rc = SQLITE_NOMEM;
80996 return SQLITE_NOMEM_BKPT;
80997 }
80998
80999 if( p->pc<=0 && p->expired ){
81000 p->rc = SQLITE_SCHEMA;
81001 rc = SQLITE_ERROR;
81002 goto end_of_step;
81003 }
81004 if( p->pc<0 ){
81005 /* If there are no other statements currently running, then
81006 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
81007 ** from interrupting a statement that has not yet started.
81008 */
81009 if( db->nVdbeActive==0 ){
81010 db->u1.isInterrupted = 0;
81011 }
81012
81013 assert( db->nVdbeWrite>0 || db->autoCommit==0
81014 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
81015 );
81016
81017#ifndef SQLITE_OMIT_TRACE
81018 if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0)
81019 && !db->init.busy && p->zSql ){
81020 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
81021 }else{
81022 assert( p->startTime==0 );
81023 }
81024#endif
81025
81026 db->nVdbeActive++;
81027 if( p->readOnly==0 ) db->nVdbeWrite++;
81028 if( p->bIsReader ) db->nVdbeRead++;
81029 p->pc = 0;
81030 }
81031#ifdef SQLITE_DEBUG
81032 p->rcApp = SQLITE_OK;
81033#endif
81034#ifndef SQLITE_OMIT_EXPLAIN
81035 if( p->explain ){
81036 rc = sqlite3VdbeList(p);
81037 }else
81038#endif /* SQLITE_OMIT_EXPLAIN */
81039 {
81040 db->nVdbeExec++;
81041 rc = sqlite3VdbeExec(p);
81042 db->nVdbeExec--;
81043 }
81044
81045#ifndef SQLITE_OMIT_TRACE
81046 /* If the statement completed successfully, invoke the profile callback */
81047 if( rc!=SQLITE_ROW ) checkProfileCallback(db, p);
81048#endif
81049
81050 if( rc==SQLITE_DONE && db->autoCommit ){
81051 assert( p->rc==SQLITE_OK );
81052 p->rc = doWalCallbacks(db);
81053 if( p->rc!=SQLITE_OK ){
81054 rc = SQLITE_ERROR;
81055 }
81056 }
81057
81058 db->errCode = rc;
81059 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
81060 p->rc = SQLITE_NOMEM_BKPT;
81061 }
81062end_of_step:
81063 /* At this point local variable rc holds the value that should be
81064 ** returned if this statement was compiled using the legacy
81065 ** sqlite3_prepare() interface. According to the docs, this can only
81066 ** be one of the values in the first assert() below. Variable p->rc
81067 ** contains the value that would be returned if sqlite3_finalize()
81068 ** were called on statement p.
81069 */
81070 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR
81071 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
81072 );
81073 assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
81074 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
81075 && rc!=SQLITE_ROW
81076 && rc!=SQLITE_DONE
81077 ){
81078 /* If this statement was prepared using saved SQL and an
81079 ** error has occurred, then return the error code in p->rc to the
81080 ** caller. Set the error code in the database handle to the same value.
81081 */
81082 rc = sqlite3VdbeTransferError(p);
81083 }
81084 return (rc&db->errMask);
81085}
81086
81087/*
81088** This is the top-level implementation of sqlite3_step(). Call
81089** sqlite3Step() to do most of the work. If a schema error occurs,
81090** call sqlite3Reprepare() and try again.
81091*/
81092SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
81093 int rc = SQLITE_OK; /* Result from sqlite3Step() */
81094 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
81095 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
81096 sqlite3 *db; /* The database connection */
81097
81098 if( vdbeSafetyNotNull(v) ){
81099 return SQLITE_MISUSE_BKPT;
81100 }
81101 db = v->db;
81102 sqlite3_mutex_enter(db->mutex);
81103 v->doingRerun = 0;
81104 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
81105 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
81106 int savedPc = v->pc;
81107 rc = sqlite3Reprepare(v);
81108 if( rc!=SQLITE_OK ){
81109 /* This case occurs after failing to recompile an sql statement.
81110 ** The error message from the SQL compiler has already been loaded
81111 ** into the database handle. This block copies the error message
81112 ** from the database handle into the statement and sets the statement
81113 ** program counter to 0 to ensure that when the statement is
81114 ** finalized or reset the parser error message is available via
81115 ** sqlite3_errmsg() and sqlite3_errcode().
81116 */
81117 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
81118 sqlite3DbFree(db, v->zErrMsg);
81119 if( !db->mallocFailed ){
81120 v->zErrMsg = sqlite3DbStrDup(db, zErr);
81121 v->rc = rc = sqlite3ApiExit(db, rc);
81122 } else {
81123 v->zErrMsg = 0;
81124 v->rc = rc = SQLITE_NOMEM_BKPT;
81125 }
81126 break;
81127 }
81128 sqlite3_reset(pStmt);
81129 if( savedPc>=0 ) v->doingRerun = 1;
81130 assert( v->expired==0 );
81131 }
81132 sqlite3_mutex_leave(db->mutex);
81133 return rc;
81134}
81135
81136
81137/*
81138** Extract the user data from a sqlite3_context structure and return a
81139** pointer to it.
81140*/
81141SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
81142 assert( p && p->pFunc );
81143 return p->pFunc->pUserData;
81144}
81145
81146/*
81147** Extract the user data from a sqlite3_context structure and return a
81148** pointer to it.
81149**
81150** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
81151** returns a copy of the pointer to the database connection (the 1st
81152** parameter) of the sqlite3_create_function() and
81153** sqlite3_create_function16() routines that originally registered the
81154** application defined function.
81155*/
81156SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
81157 assert( p && p->pOut );
81158 return p->pOut->db;
81159}
81160
81161/*
81162** If this routine is invoked from within an xColumn method of a virtual
81163** table, then it returns true if and only if the the call is during an
81164** UPDATE operation and the value of the column will not be modified
81165** by the UPDATE.
81166**
81167** If this routine is called from any context other than within the
81168** xColumn method of a virtual table, then the return value is meaningless
81169** and arbitrary.
81170**
81171** Virtual table implements might use this routine to optimize their
81172** performance by substituting a NULL result, or some other light-weight
81173** value, as a signal to the xUpdate routine that the column is unchanged.
81174*/
81175SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){
81176 assert( p );
81177 return sqlite3_value_nochange(p->pOut);
81178}
81179
81180/*
81181** Return the current time for a statement. If the current time
81182** is requested more than once within the same run of a single prepared
81183** statement, the exact same time is returned for each invocation regardless
81184** of the amount of time that elapses between invocations. In other words,
81185** the time returned is always the time of the first call.
81186*/
81187SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
81188 int rc;
81189#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
81190 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
81191 assert( p->pVdbe!=0 );
81192#else
81193 sqlite3_int64 iTime = 0;
81194 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
81195#endif
81196 if( *piTime==0 ){
81197 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
81198 if( rc ) *piTime = 0;
81199 }
81200 return *piTime;
81201}
81202
81203/*
81204** Create a new aggregate context for p and return a pointer to
81205** its pMem->z element.
81206*/
81207static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
81208 Mem *pMem = p->pMem;
81209 assert( (pMem->flags & MEM_Agg)==0 );
81210 if( nByte<=0 ){
81211 sqlite3VdbeMemSetNull(pMem);
81212 pMem->z = 0;
81213 }else{
81214 sqlite3VdbeMemClearAndResize(pMem, nByte);
81215 pMem->flags = MEM_Agg;
81216 pMem->u.pDef = p->pFunc;
81217 if( pMem->z ){
81218 memset(pMem->z, 0, nByte);
81219 }
81220 }
81221 return (void*)pMem->z;
81222}
81223
81224/*
81225** Allocate or return the aggregate context for a user function. A new
81226** context is allocated on the first call. Subsequent calls return the
81227** same context that was returned on prior calls.
81228*/
81229SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
81230 assert( p && p->pFunc && p->pFunc->xFinalize );
81231 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
81232 testcase( nByte<0 );
81233 if( (p->pMem->flags & MEM_Agg)==0 ){
81234 return createAggContext(p, nByte);
81235 }else{
81236 return (void*)p->pMem->z;
81237 }
81238}
81239
81240/*
81241** Return the auxiliary data pointer, if any, for the iArg'th argument to
81242** the user-function defined by pCtx.
81243**
81244** The left-most argument is 0.
81245**
81246** Undocumented behavior: If iArg is negative then access a cache of
81247** auxiliary data pointers that is available to all functions within a
81248** single prepared statement. The iArg values must match.
81249*/
81250SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
81251 AuxData *pAuxData;
81252
81253 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81254#if SQLITE_ENABLE_STAT3_OR_STAT4
81255 if( pCtx->pVdbe==0 ) return 0;
81256#else
81257 assert( pCtx->pVdbe!=0 );
81258#endif
81259 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
81260 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
81261 return pAuxData->pAux;
81262 }
81263 }
81264 return 0;
81265}
81266
81267/*
81268** Set the auxiliary data pointer and delete function, for the iArg'th
81269** argument to the user-function defined by pCtx. Any previous value is
81270** deleted by calling the delete function specified when it was set.
81271**
81272** The left-most argument is 0.
81273**
81274** Undocumented behavior: If iArg is negative then make the data available
81275** to all functions within the current prepared statement using iArg as an
81276** access code.
81277*/
81278SQLITE_API void sqlite3_set_auxdata(
81279 sqlite3_context *pCtx,
81280 int iArg,
81281 void *pAux,
81282 void (*xDelete)(void*)
81283){
81284 AuxData *pAuxData;
81285 Vdbe *pVdbe = pCtx->pVdbe;
81286
81287 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
81288#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81289 if( pVdbe==0 ) goto failed;
81290#else
81291 assert( pVdbe!=0 );
81292#endif
81293
81294 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
81295 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
81296 break;
81297 }
81298 }
81299 if( pAuxData==0 ){
81300 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
81301 if( !pAuxData ) goto failed;
81302 pAuxData->iAuxOp = pCtx->iOp;
81303 pAuxData->iAuxArg = iArg;
81304 pAuxData->pNextAux = pVdbe->pAuxData;
81305 pVdbe->pAuxData = pAuxData;
81306 if( pCtx->isError==0 ) pCtx->isError = -1;
81307 }else if( pAuxData->xDeleteAux ){
81308 pAuxData->xDeleteAux(pAuxData->pAux);
81309 }
81310
81311 pAuxData->pAux = pAux;
81312 pAuxData->xDeleteAux = xDelete;
81313 return;
81314
81315failed:
81316 if( xDelete ){
81317 xDelete(pAux);
81318 }
81319}
81320
81321#ifndef SQLITE_OMIT_DEPRECATED
81322/*
81323** Return the number of times the Step function of an aggregate has been
81324** called.
81325**
81326** This function is deprecated. Do not use it for new code. It is
81327** provide only to avoid breaking legacy code. New aggregate function
81328** implementations should keep their own counts within their aggregate
81329** context.
81330*/
81331SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
81332 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
81333 return p->pMem->n;
81334}
81335#endif
81336
81337/*
81338** Return the number of columns in the result set for the statement pStmt.
81339*/
81340SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
81341 Vdbe *pVm = (Vdbe *)pStmt;
81342 return pVm ? pVm->nResColumn : 0;
81343}
81344
81345/*
81346** Return the number of values available from the current row of the
81347** currently executing statement pStmt.
81348*/
81349SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
81350 Vdbe *pVm = (Vdbe *)pStmt;
81351 if( pVm==0 || pVm->pResultSet==0 ) return 0;
81352 return pVm->nResColumn;
81353}
81354
81355/*
81356** Return a pointer to static memory containing an SQL NULL value.
81357*/
81358static const Mem *columnNullValue(void){
81359 /* Even though the Mem structure contains an element
81360 ** of type i64, on certain architectures (x86) with certain compiler
81361 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
81362 ** instead of an 8-byte one. This all works fine, except that when
81363 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
81364 ** that a Mem structure is located on an 8-byte boundary. To prevent
81365 ** these assert()s from failing, when building with SQLITE_DEBUG defined
81366 ** using gcc, we force nullMem to be 8-byte aligned using the magical
81367 ** __attribute__((aligned(8))) macro. */
81368 static const Mem nullMem
81369#if defined(SQLITE_DEBUG) && defined(__GNUC__)
81370 __attribute__((aligned(8)))
81371#endif
81372 = {
81373 /* .u = */ {0},
81374 /* .flags = */ (u16)MEM_Null,
81375 /* .enc = */ (u8)0,
81376 /* .eSubtype = */ (u8)0,
81377 /* .n = */ (int)0,
81378 /* .z = */ (char*)0,
81379 /* .zMalloc = */ (char*)0,
81380 /* .szMalloc = */ (int)0,
81381 /* .uTemp = */ (u32)0,
81382 /* .db = */ (sqlite3*)0,
81383 /* .xDel = */ (void(*)(void*))0,
81384#ifdef SQLITE_DEBUG
81385 /* .pScopyFrom = */ (Mem*)0,
81386 /* .mScopyFlags= */ 0,
81387#endif
81388 };
81389 return &nullMem;
81390}
81391
81392/*
81393** Check to see if column iCol of the given statement is valid. If
81394** it is, return a pointer to the Mem for the value of that column.
81395** If iCol is not valid, return a pointer to a Mem which has a value
81396** of NULL.
81397*/
81398static Mem *columnMem(sqlite3_stmt *pStmt, int i){
81399 Vdbe *pVm;
81400 Mem *pOut;
81401
81402 pVm = (Vdbe *)pStmt;
81403 if( pVm==0 ) return (Mem*)columnNullValue();
81404 assert( pVm->db );
81405 sqlite3_mutex_enter(pVm->db->mutex);
81406 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
81407 pOut = &pVm->pResultSet[i];
81408 }else{
81409 sqlite3Error(pVm->db, SQLITE_RANGE);
81410 pOut = (Mem*)columnNullValue();
81411 }
81412 return pOut;
81413}
81414
81415/*
81416** This function is called after invoking an sqlite3_value_XXX function on a
81417** column value (i.e. a value returned by evaluating an SQL expression in the
81418** select list of a SELECT statement) that may cause a malloc() failure. If
81419** malloc() has failed, the threads mallocFailed flag is cleared and the result
81420** code of statement pStmt set to SQLITE_NOMEM.
81421**
81422** Specifically, this is called from within:
81423**
81424** sqlite3_column_int()
81425** sqlite3_column_int64()
81426** sqlite3_column_text()
81427** sqlite3_column_text16()
81428** sqlite3_column_real()
81429** sqlite3_column_bytes()
81430** sqlite3_column_bytes16()
81431** sqiite3_column_blob()
81432*/
81433static void columnMallocFailure(sqlite3_stmt *pStmt)
81434{
81435 /* If malloc() failed during an encoding conversion within an
81436 ** sqlite3_column_XXX API, then set the return code of the statement to
81437 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
81438 ** and _finalize() will return NOMEM.
81439 */
81440 Vdbe *p = (Vdbe *)pStmt;
81441 if( p ){
81442 assert( p->db!=0 );
81443 assert( sqlite3_mutex_held(p->db->mutex) );
81444 p->rc = sqlite3ApiExit(p->db, p->rc);
81445 sqlite3_mutex_leave(p->db->mutex);
81446 }
81447}
81448
81449/**************************** sqlite3_column_ *******************************
81450** The following routines are used to access elements of the current row
81451** in the result set.
81452*/
81453SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
81454 const void *val;
81455 val = sqlite3_value_blob( columnMem(pStmt,i) );
81456 /* Even though there is no encoding conversion, value_blob() might
81457 ** need to call malloc() to expand the result of a zeroblob()
81458 ** expression.
81459 */
81460 columnMallocFailure(pStmt);
81461 return val;
81462}
81463SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
81464 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
81465 columnMallocFailure(pStmt);
81466 return val;
81467}
81468SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
81469 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
81470 columnMallocFailure(pStmt);
81471 return val;
81472}
81473SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
81474 double val = sqlite3_value_double( columnMem(pStmt,i) );
81475 columnMallocFailure(pStmt);
81476 return val;
81477}
81478SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
81479 int val = sqlite3_value_int( columnMem(pStmt,i) );
81480 columnMallocFailure(pStmt);
81481 return val;
81482}
81483SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
81484 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
81485 columnMallocFailure(pStmt);
81486 return val;
81487}
81488SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
81489 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
81490 columnMallocFailure(pStmt);
81491 return val;
81492}
81493SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
81494 Mem *pOut = columnMem(pStmt, i);
81495 if( pOut->flags&MEM_Static ){
81496 pOut->flags &= ~MEM_Static;
81497 pOut->flags |= MEM_Ephem;
81498 }
81499 columnMallocFailure(pStmt);
81500 return (sqlite3_value *)pOut;
81501}
81502#ifndef SQLITE_OMIT_UTF16
81503SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
81504 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
81505 columnMallocFailure(pStmt);
81506 return val;
81507}
81508#endif /* SQLITE_OMIT_UTF16 */
81509SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
81510 int iType = sqlite3_value_type( columnMem(pStmt,i) );
81511 columnMallocFailure(pStmt);
81512 return iType;
81513}
81514
81515/*
81516** Convert the N-th element of pStmt->pColName[] into a string using
81517** xFunc() then return that string. If N is out of range, return 0.
81518**
81519** There are up to 5 names for each column. useType determines which
81520** name is returned. Here are the names:
81521**
81522** 0 The column name as it should be displayed for output
81523** 1 The datatype name for the column
81524** 2 The name of the database that the column derives from
81525** 3 The name of the table that the column derives from
81526** 4 The name of the table column that the result column derives from
81527**
81528** If the result is not a simple column reference (if it is an expression
81529** or a constant) then useTypes 2, 3, and 4 return NULL.
81530*/
81531static const void *columnName(
81532 sqlite3_stmt *pStmt,
81533 int N,
81534 const void *(*xFunc)(Mem*),
81535 int useType
81536){
81537 const void *ret;
81538 Vdbe *p;
81539 int n;
81540 sqlite3 *db;
81541#ifdef SQLITE_ENABLE_API_ARMOR
81542 if( pStmt==0 ){
81543 (void)SQLITE_MISUSE_BKPT;
81544 return 0;
81545 }
81546#endif
81547 ret = 0;
81548 p = (Vdbe *)pStmt;
81549 db = p->db;
81550 assert( db!=0 );
81551 n = sqlite3_column_count(pStmt);
81552 if( N<n && N>=0 ){
81553 N += useType*n;
81554 sqlite3_mutex_enter(db->mutex);
81555 assert( db->mallocFailed==0 );
81556 ret = xFunc(&p->aColName[N]);
81557 /* A malloc may have failed inside of the xFunc() call. If this
81558 ** is the case, clear the mallocFailed flag and return NULL.
81559 */
81560 if( db->mallocFailed ){
81561 sqlite3OomClear(db);
81562 ret = 0;
81563 }
81564 sqlite3_mutex_leave(db->mutex);
81565 }
81566 return ret;
81567}
81568
81569/*
81570** Return the name of the Nth column of the result set returned by SQL
81571** statement pStmt.
81572*/
81573SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
81574 return columnName(
81575 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
81576}
81577#ifndef SQLITE_OMIT_UTF16
81578SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
81579 return columnName(
81580 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
81581}
81582#endif
81583
81584/*
81585** Constraint: If you have ENABLE_COLUMN_METADATA then you must
81586** not define OMIT_DECLTYPE.
81587*/
81588#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
81589# error "Must not define both SQLITE_OMIT_DECLTYPE \
81590 and SQLITE_ENABLE_COLUMN_METADATA"
81591#endif
81592
81593#ifndef SQLITE_OMIT_DECLTYPE
81594/*
81595** Return the column declaration type (if applicable) of the 'i'th column
81596** of the result set of SQL statement pStmt.
81597*/
81598SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
81599 return columnName(
81600 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
81601}
81602#ifndef SQLITE_OMIT_UTF16
81603SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
81604 return columnName(
81605 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
81606}
81607#endif /* SQLITE_OMIT_UTF16 */
81608#endif /* SQLITE_OMIT_DECLTYPE */
81609
81610#ifdef SQLITE_ENABLE_COLUMN_METADATA
81611/*
81612** Return the name of the database from which a result column derives.
81613** NULL is returned if the result column is an expression or constant or
81614** anything else which is not an unambiguous reference to a database column.
81615*/
81616SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
81617 return columnName(
81618 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
81619}
81620#ifndef SQLITE_OMIT_UTF16
81621SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
81622 return columnName(
81623 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
81624}
81625#endif /* SQLITE_OMIT_UTF16 */
81626
81627/*
81628** Return the name of the table from which a result column derives.
81629** NULL is returned if the result column is an expression or constant or
81630** anything else which is not an unambiguous reference to a database column.
81631*/
81632SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
81633 return columnName(
81634 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
81635}
81636#ifndef SQLITE_OMIT_UTF16
81637SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
81638 return columnName(
81639 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
81640}
81641#endif /* SQLITE_OMIT_UTF16 */
81642
81643/*
81644** Return the name of the table column from which a result column derives.
81645** NULL is returned if the result column is an expression or constant or
81646** anything else which is not an unambiguous reference to a database column.
81647*/
81648SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
81649 return columnName(
81650 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
81651}
81652#ifndef SQLITE_OMIT_UTF16
81653SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
81654 return columnName(
81655 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
81656}
81657#endif /* SQLITE_OMIT_UTF16 */
81658#endif /* SQLITE_ENABLE_COLUMN_METADATA */
81659
81660
81661/******************************* sqlite3_bind_ ***************************
81662**
81663** Routines used to attach values to wildcards in a compiled SQL statement.
81664*/
81665/*
81666** Unbind the value bound to variable i in virtual machine p. This is the
81667** the same as binding a NULL value to the column. If the "i" parameter is
81668** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
81669**
81670** A successful evaluation of this routine acquires the mutex on p.
81671** the mutex is released if any kind of error occurs.
81672**
81673** The error code stored in database p->db is overwritten with the return
81674** value in any case.
81675*/
81676static int vdbeUnbind(Vdbe *p, int i){
81677 Mem *pVar;
81678 if( vdbeSafetyNotNull(p) ){
81679 return SQLITE_MISUSE_BKPT;
81680 }
81681 sqlite3_mutex_enter(p->db->mutex);
81682 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
81683 sqlite3Error(p->db, SQLITE_MISUSE);
81684 sqlite3_mutex_leave(p->db->mutex);
81685 sqlite3_log(SQLITE_MISUSE,
81686 "bind on a busy prepared statement: [%s]", p->zSql);
81687 return SQLITE_MISUSE_BKPT;
81688 }
81689 if( i<1 || i>p->nVar ){
81690 sqlite3Error(p->db, SQLITE_RANGE);
81691 sqlite3_mutex_leave(p->db->mutex);
81692 return SQLITE_RANGE;
81693 }
81694 i--;
81695 pVar = &p->aVar[i];
81696 sqlite3VdbeMemRelease(pVar);
81697 pVar->flags = MEM_Null;
81698 sqlite3Error(p->db, SQLITE_OK);
81699
81700 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
81701 ** binding a new value to this variable invalidates the current query plan.
81702 **
81703 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
81704 ** parameter in the WHERE clause might influence the choice of query plan
81705 ** for a statement, then the statement will be automatically recompiled,
81706 ** as if there had been a schema change, on the first sqlite3_step() call
81707 ** following any change to the bindings of that parameter.
81708 */
81709 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
81710 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
81711 p->expired = 1;
81712 }
81713 return SQLITE_OK;
81714}
81715
81716/*
81717** Bind a text or BLOB value.
81718*/
81719static int bindText(
81720 sqlite3_stmt *pStmt, /* The statement to bind against */
81721 int i, /* Index of the parameter to bind */
81722 const void *zData, /* Pointer to the data to be bound */
81723 int nData, /* Number of bytes of data to be bound */
81724 void (*xDel)(void*), /* Destructor for the data */
81725 u8 encoding /* Encoding for the data */
81726){
81727 Vdbe *p = (Vdbe *)pStmt;
81728 Mem *pVar;
81729 int rc;
81730
81731 rc = vdbeUnbind(p, i);
81732 if( rc==SQLITE_OK ){
81733 if( zData!=0 ){
81734 pVar = &p->aVar[i-1];
81735 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
81736 if( rc==SQLITE_OK && encoding!=0 ){
81737 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
81738 }
81739 if( rc ){
81740 sqlite3Error(p->db, rc);
81741 rc = sqlite3ApiExit(p->db, rc);
81742 }
81743 }
81744 sqlite3_mutex_leave(p->db->mutex);
81745 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
81746 xDel((void*)zData);
81747 }
81748 return rc;
81749}
81750
81751
81752/*
81753** Bind a blob value to an SQL statement variable.
81754*/
81755SQLITE_API int sqlite3_bind_blob(
81756 sqlite3_stmt *pStmt,
81757 int i,
81758 const void *zData,
81759 int nData,
81760 void (*xDel)(void*)
81761){
81762#ifdef SQLITE_ENABLE_API_ARMOR
81763 if( nData<0 ) return SQLITE_MISUSE_BKPT;
81764#endif
81765 return bindText(pStmt, i, zData, nData, xDel, 0);
81766}
81767SQLITE_API int sqlite3_bind_blob64(
81768 sqlite3_stmt *pStmt,
81769 int i,
81770 const void *zData,
81771 sqlite3_uint64 nData,
81772 void (*xDel)(void*)
81773){
81774 assert( xDel!=SQLITE_DYNAMIC );
81775 if( nData>0x7fffffff ){
81776 return invokeValueDestructor(zData, xDel, 0);
81777 }else{
81778 return bindText(pStmt, i, zData, (int)nData, xDel, 0);
81779 }
81780}
81781SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
81782 int rc;
81783 Vdbe *p = (Vdbe *)pStmt;
81784 rc = vdbeUnbind(p, i);
81785 if( rc==SQLITE_OK ){
81786 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
81787 sqlite3_mutex_leave(p->db->mutex);
81788 }
81789 return rc;
81790}
81791SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
81792 return sqlite3_bind_int64(p, i, (i64)iValue);
81793}
81794SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
81795 int rc;
81796 Vdbe *p = (Vdbe *)pStmt;
81797 rc = vdbeUnbind(p, i);
81798 if( rc==SQLITE_OK ){
81799 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
81800 sqlite3_mutex_leave(p->db->mutex);
81801 }
81802 return rc;
81803}
81804SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
81805 int rc;
81806 Vdbe *p = (Vdbe*)pStmt;
81807 rc = vdbeUnbind(p, i);
81808 if( rc==SQLITE_OK ){
81809 sqlite3_mutex_leave(p->db->mutex);
81810 }
81811 return rc;
81812}
81813SQLITE_API int sqlite3_bind_pointer(
81814 sqlite3_stmt *pStmt,
81815 int i,
81816 void *pPtr,
81817 const char *zPTtype,
81818 void (*xDestructor)(void*)
81819){
81820 int rc;
81821 Vdbe *p = (Vdbe*)pStmt;
81822 rc = vdbeUnbind(p, i);
81823 if( rc==SQLITE_OK ){
81824 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
81825 sqlite3_mutex_leave(p->db->mutex);
81826 }else if( xDestructor ){
81827 xDestructor(pPtr);
81828 }
81829 return rc;
81830}
81831SQLITE_API int sqlite3_bind_text(
81832 sqlite3_stmt *pStmt,
81833 int i,
81834 const char *zData,
81835 int nData,
81836 void (*xDel)(void*)
81837){
81838 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
81839}
81840SQLITE_API int sqlite3_bind_text64(
81841 sqlite3_stmt *pStmt,
81842 int i,
81843 const char *zData,
81844 sqlite3_uint64 nData,
81845 void (*xDel)(void*),
81846 unsigned char enc
81847){
81848 assert( xDel!=SQLITE_DYNAMIC );
81849 if( nData>0x7fffffff ){
81850 return invokeValueDestructor(zData, xDel, 0);
81851 }else{
81852 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
81853 return bindText(pStmt, i, zData, (int)nData, xDel, enc);
81854 }
81855}
81856#ifndef SQLITE_OMIT_UTF16
81857SQLITE_API int sqlite3_bind_text16(
81858 sqlite3_stmt *pStmt,
81859 int i,
81860 const void *zData,
81861 int nData,
81862 void (*xDel)(void*)
81863){
81864 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
81865}
81866#endif /* SQLITE_OMIT_UTF16 */
81867SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
81868 int rc;
81869 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
81870 case SQLITE_INTEGER: {
81871 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
81872 break;
81873 }
81874 case SQLITE_FLOAT: {
81875 rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
81876 break;
81877 }
81878 case SQLITE_BLOB: {
81879 if( pValue->flags & MEM_Zero ){
81880 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
81881 }else{
81882 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
81883 }
81884 break;
81885 }
81886 case SQLITE_TEXT: {
81887 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
81888 pValue->enc);
81889 break;
81890 }
81891 default: {
81892 rc = sqlite3_bind_null(pStmt, i);
81893 break;
81894 }
81895 }
81896 return rc;
81897}
81898SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
81899 int rc;
81900 Vdbe *p = (Vdbe *)pStmt;
81901 rc = vdbeUnbind(p, i);
81902 if( rc==SQLITE_OK ){
81903 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
81904 sqlite3_mutex_leave(p->db->mutex);
81905 }
81906 return rc;
81907}
81908SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
81909 int rc;
81910 Vdbe *p = (Vdbe *)pStmt;
81911 sqlite3_mutex_enter(p->db->mutex);
81912 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
81913 rc = SQLITE_TOOBIG;
81914 }else{
81915 assert( (n & 0x7FFFFFFF)==n );
81916 rc = sqlite3_bind_zeroblob(pStmt, i, n);
81917 }
81918 rc = sqlite3ApiExit(p->db, rc);
81919 sqlite3_mutex_leave(p->db->mutex);
81920 return rc;
81921}
81922
81923/*
81924** Return the number of wildcards that can be potentially bound to.
81925** This routine is added to support DBD::SQLite.
81926*/
81927SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
81928 Vdbe *p = (Vdbe*)pStmt;
81929 return p ? p->nVar : 0;
81930}
81931
81932/*
81933** Return the name of a wildcard parameter. Return NULL if the index
81934** is out of range or if the wildcard is unnamed.
81935**
81936** The result is always UTF-8.
81937*/
81938SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
81939 Vdbe *p = (Vdbe*)pStmt;
81940 if( p==0 ) return 0;
81941 return sqlite3VListNumToName(p->pVList, i);
81942}
81943
81944/*
81945** Given a wildcard parameter name, return the index of the variable
81946** with that name. If there is no variable with the given name,
81947** return 0.
81948*/
81949SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
81950 if( p==0 || zName==0 ) return 0;
81951 return sqlite3VListNameToNum(p->pVList, zName, nName);
81952}
81953SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
81954 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
81955}
81956
81957/*
81958** Transfer all bindings from the first statement over to the second.
81959*/
81960SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
81961 Vdbe *pFrom = (Vdbe*)pFromStmt;
81962 Vdbe *pTo = (Vdbe*)pToStmt;
81963 int i;
81964 assert( pTo->db==pFrom->db );
81965 assert( pTo->nVar==pFrom->nVar );
81966 sqlite3_mutex_enter(pTo->db->mutex);
81967 for(i=0; i<pFrom->nVar; i++){
81968 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
81969 }
81970 sqlite3_mutex_leave(pTo->db->mutex);
81971 return SQLITE_OK;
81972}
81973
81974#ifndef SQLITE_OMIT_DEPRECATED
81975/*
81976** Deprecated external interface. Internal/core SQLite code
81977** should call sqlite3TransferBindings.
81978**
81979** It is misuse to call this routine with statements from different
81980** database connections. But as this is a deprecated interface, we
81981** will not bother to check for that condition.
81982**
81983** If the two statements contain a different number of bindings, then
81984** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
81985** SQLITE_OK is returned.
81986*/
81987SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
81988 Vdbe *pFrom = (Vdbe*)pFromStmt;
81989 Vdbe *pTo = (Vdbe*)pToStmt;
81990 if( pFrom->nVar!=pTo->nVar ){
81991 return SQLITE_ERROR;
81992 }
81993 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
81994 if( pTo->expmask ){
81995 pTo->expired = 1;
81996 }
81997 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
81998 if( pFrom->expmask ){
81999 pFrom->expired = 1;
82000 }
82001 return sqlite3TransferBindings(pFromStmt, pToStmt);
82002}
82003#endif
82004
82005/*
82006** Return the sqlite3* database handle to which the prepared statement given
82007** in the argument belongs. This is the same database handle that was
82008** the first argument to the sqlite3_prepare() that was used to create
82009** the statement in the first place.
82010*/
82011SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
82012 return pStmt ? ((Vdbe*)pStmt)->db : 0;
82013}
82014
82015/*
82016** Return true if the prepared statement is guaranteed to not modify the
82017** database.
82018*/
82019SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
82020 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
82021}
82022
82023/*
82024** Return true if the prepared statement is in need of being reset.
82025*/
82026SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
82027 Vdbe *v = (Vdbe*)pStmt;
82028 return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
82029}
82030
82031/*
82032** Return a pointer to the next prepared statement after pStmt associated
82033** with database connection pDb. If pStmt is NULL, return the first
82034** prepared statement for the database connection. Return NULL if there
82035** are no more.
82036*/
82037SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
82038 sqlite3_stmt *pNext;
82039#ifdef SQLITE_ENABLE_API_ARMOR
82040 if( !sqlite3SafetyCheckOk(pDb) ){
82041 (void)SQLITE_MISUSE_BKPT;
82042 return 0;
82043 }
82044#endif
82045 sqlite3_mutex_enter(pDb->mutex);
82046 if( pStmt==0 ){
82047 pNext = (sqlite3_stmt*)pDb->pVdbe;
82048 }else{
82049 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
82050 }
82051 sqlite3_mutex_leave(pDb->mutex);
82052 return pNext;
82053}
82054
82055/*
82056** Return the value of a status counter for a prepared statement
82057*/
82058SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
82059 Vdbe *pVdbe = (Vdbe*)pStmt;
82060 u32 v;
82061#ifdef SQLITE_ENABLE_API_ARMOR
82062 if( !pStmt
82063 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
82064 ){
82065 (void)SQLITE_MISUSE_BKPT;
82066 return 0;
82067 }
82068#endif
82069 if( op==SQLITE_STMTSTATUS_MEMUSED ){
82070 sqlite3 *db = pVdbe->db;
82071 sqlite3_mutex_enter(db->mutex);
82072 v = 0;
82073 db->pnBytesFreed = (int*)&v;
82074 sqlite3VdbeClearObject(db, pVdbe);
82075 sqlite3DbFree(db, pVdbe);
82076 db->pnBytesFreed = 0;
82077 sqlite3_mutex_leave(db->mutex);
82078 }else{
82079 v = pVdbe->aCounter[op];
82080 if( resetFlag ) pVdbe->aCounter[op] = 0;
82081 }
82082 return (int)v;
82083}
82084
82085/*
82086** Return the SQL associated with a prepared statement
82087*/
82088SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
82089 Vdbe *p = (Vdbe *)pStmt;
82090 return p ? p->zSql : 0;
82091}
82092
82093/*
82094** Return the SQL associated with a prepared statement with
82095** bound parameters expanded. Space to hold the returned string is
82096** obtained from sqlite3_malloc(). The caller is responsible for
82097** freeing the returned string by passing it to sqlite3_free().
82098**
82099** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
82100** expanded bound parameters.
82101*/
82102SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
82103#ifdef SQLITE_OMIT_TRACE
82104 return 0;
82105#else
82106 char *z = 0;
82107 const char *zSql = sqlite3_sql(pStmt);
82108 if( zSql ){
82109 Vdbe *p = (Vdbe *)pStmt;
82110 sqlite3_mutex_enter(p->db->mutex);
82111 z = sqlite3VdbeExpandSql(p, zSql);
82112 sqlite3_mutex_leave(p->db->mutex);
82113 }
82114 return z;
82115#endif
82116}
82117
82118#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82119/*
82120** Allocate and populate an UnpackedRecord structure based on the serialized
82121** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
82122** if successful, or a NULL pointer if an OOM error is encountered.
82123*/
82124static UnpackedRecord *vdbeUnpackRecord(
82125 KeyInfo *pKeyInfo,
82126 int nKey,
82127 const void *pKey
82128){
82129 UnpackedRecord *pRet; /* Return value */
82130
82131 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
82132 if( pRet ){
82133 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
82134 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
82135 }
82136 return pRet;
82137}
82138
82139/*
82140** This function is called from within a pre-update callback to retrieve
82141** a field of the row currently being updated or deleted.
82142*/
82143SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
82144 PreUpdate *p = db->pPreUpdate;
82145 Mem *pMem;
82146 int rc = SQLITE_OK;
82147
82148 /* Test that this call is being made from within an SQLITE_DELETE or
82149 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
82150 if( !p || p->op==SQLITE_INSERT ){
82151 rc = SQLITE_MISUSE_BKPT;
82152 goto preupdate_old_out;
82153 }
82154 if( p->pPk ){
82155 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
82156 }
82157 if( iIdx>=p->pCsr->nField || iIdx<0 ){
82158 rc = SQLITE_RANGE;
82159 goto preupdate_old_out;
82160 }
82161
82162 /* If the old.* record has not yet been loaded into memory, do so now. */
82163 if( p->pUnpacked==0 ){
82164 u32 nRec;
82165 u8 *aRec;
82166
82167 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
82168 aRec = sqlite3DbMallocRaw(db, nRec);
82169 if( !aRec ) goto preupdate_old_out;
82170 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
82171 if( rc==SQLITE_OK ){
82172 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
82173 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
82174 }
82175 if( rc!=SQLITE_OK ){
82176 sqlite3DbFree(db, aRec);
82177 goto preupdate_old_out;
82178 }
82179 p->aRecord = aRec;
82180 }
82181
82182 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
82183 if( iIdx==p->pTab->iPKey ){
82184 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
82185 }else if( iIdx>=p->pUnpacked->nField ){
82186 *ppValue = (sqlite3_value *)columnNullValue();
82187 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
82188 if( pMem->flags & MEM_Int ){
82189 sqlite3VdbeMemRealify(pMem);
82190 }
82191 }
82192
82193 preupdate_old_out:
82194 sqlite3Error(db, rc);
82195 return sqlite3ApiExit(db, rc);
82196}
82197#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
82198
82199#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82200/*
82201** This function is called from within a pre-update callback to retrieve
82202** the number of columns in the row being updated, deleted or inserted.
82203*/
82204SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
82205 PreUpdate *p = db->pPreUpdate;
82206 return (p ? p->keyinfo.nKeyField : 0);
82207}
82208#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
82209
82210#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82211/*
82212** This function is designed to be called from within a pre-update callback
82213** only. It returns zero if the change that caused the callback was made
82214** immediately by a user SQL statement. Or, if the change was made by a
82215** trigger program, it returns the number of trigger programs currently
82216** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
82217** top-level trigger etc.).
82218**
82219** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
82220** or SET DEFAULT action is considered a trigger.
82221*/
82222SQLITE_API int sqlite3_preupdate_depth(sqlite3 *db){
82223 PreUpdate *p = db->pPreUpdate;
82224 return (p ? p->v->nFrame : 0);
82225}
82226#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
82227
82228#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82229/*
82230** This function is called from within a pre-update callback to retrieve
82231** a field of the row currently being updated or inserted.
82232*/
82233SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
82234 PreUpdate *p = db->pPreUpdate;
82235 int rc = SQLITE_OK;
82236 Mem *pMem;
82237
82238 if( !p || p->op==SQLITE_DELETE ){
82239 rc = SQLITE_MISUSE_BKPT;
82240 goto preupdate_new_out;
82241 }
82242 if( p->pPk && p->op!=SQLITE_UPDATE ){
82243 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
82244 }
82245 if( iIdx>=p->pCsr->nField || iIdx<0 ){
82246 rc = SQLITE_RANGE;
82247 goto preupdate_new_out;
82248 }
82249
82250 if( p->op==SQLITE_INSERT ){
82251 /* For an INSERT, memory cell p->iNewReg contains the serialized record
82252 ** that is being inserted. Deserialize it. */
82253 UnpackedRecord *pUnpack = p->pNewUnpacked;
82254 if( !pUnpack ){
82255 Mem *pData = &p->v->aMem[p->iNewReg];
82256 rc = ExpandBlob(pData);
82257 if( rc!=SQLITE_OK ) goto preupdate_new_out;
82258 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
82259 if( !pUnpack ){
82260 rc = SQLITE_NOMEM;
82261 goto preupdate_new_out;
82262 }
82263 p->pNewUnpacked = pUnpack;
82264 }
82265 pMem = &pUnpack->aMem[iIdx];
82266 if( iIdx==p->pTab->iPKey ){
82267 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
82268 }else if( iIdx>=pUnpack->nField ){
82269 pMem = (sqlite3_value *)columnNullValue();
82270 }
82271 }else{
82272 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
82273 ** value. Make a copy of the cell contents and return a pointer to it.
82274 ** It is not safe to return a pointer to the memory cell itself as the
82275 ** caller may modify the value text encoding.
82276 */
82277 assert( p->op==SQLITE_UPDATE );
82278 if( !p->aNew ){
82279 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
82280 if( !p->aNew ){
82281 rc = SQLITE_NOMEM;
82282 goto preupdate_new_out;
82283 }
82284 }
82285 assert( iIdx>=0 && iIdx<p->pCsr->nField );
82286 pMem = &p->aNew[iIdx];
82287 if( pMem->flags==0 ){
82288 if( iIdx==p->pTab->iPKey ){
82289 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
82290 }else{
82291 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
82292 if( rc!=SQLITE_OK ) goto preupdate_new_out;
82293 }
82294 }
82295 }
82296 *ppValue = pMem;
82297
82298 preupdate_new_out:
82299 sqlite3Error(db, rc);
82300 return sqlite3ApiExit(db, rc);
82301}
82302#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
82303
82304#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
82305/*
82306** Return status data for a single loop within query pStmt.
82307*/
82308SQLITE_API int sqlite3_stmt_scanstatus(
82309 sqlite3_stmt *pStmt, /* Prepared statement being queried */
82310 int idx, /* Index of loop to report on */
82311 int iScanStatusOp, /* Which metric to return */
82312 void *pOut /* OUT: Write the answer here */
82313){
82314 Vdbe *p = (Vdbe*)pStmt;
82315 ScanStatus *pScan;
82316 if( idx<0 || idx>=p->nScan ) return 1;
82317 pScan = &p->aScan[idx];
82318 switch( iScanStatusOp ){
82319 case SQLITE_SCANSTAT_NLOOP: {
82320 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
82321 break;
82322 }
82323 case SQLITE_SCANSTAT_NVISIT: {
82324 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
82325 break;
82326 }
82327 case SQLITE_SCANSTAT_EST: {
82328 double r = 1.0;
82329 LogEst x = pScan->nEst;
82330 while( x<100 ){
82331 x += 10;
82332 r *= 0.5;
82333 }
82334 *(double*)pOut = r*sqlite3LogEstToInt(x);
82335 break;
82336 }
82337 case SQLITE_SCANSTAT_NAME: {
82338 *(const char**)pOut = pScan->zName;
82339 break;
82340 }
82341 case SQLITE_SCANSTAT_EXPLAIN: {
82342 if( pScan->addrExplain ){
82343 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
82344 }else{
82345 *(const char**)pOut = 0;
82346 }
82347 break;
82348 }
82349 case SQLITE_SCANSTAT_SELECTID: {
82350 if( pScan->addrExplain ){
82351 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
82352 }else{
82353 *(int*)pOut = -1;
82354 }
82355 break;
82356 }
82357 default: {
82358 return 1;
82359 }
82360 }
82361 return 0;
82362}
82363
82364/*
82365** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
82366*/
82367SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
82368 Vdbe *p = (Vdbe*)pStmt;
82369 memset(p->anExec, 0, p->nOp * sizeof(i64));
82370}
82371#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
82372
82373/************** End of vdbeapi.c *********************************************/
82374/************** Begin file vdbetrace.c ***************************************/
82375/*
82376** 2009 November 25
82377**
82378** The author disclaims copyright to this source code. In place of
82379** a legal notice, here is a blessing:
82380**
82381** May you do good and not evil.
82382** May you find forgiveness for yourself and forgive others.
82383** May you share freely, never taking more than you give.
82384**
82385*************************************************************************
82386**
82387** This file contains code used to insert the values of host parameters
82388** (aka "wildcards") into the SQL text output by sqlite3_trace().
82389**
82390** The Vdbe parse-tree explainer is also found here.
82391*/
82392/* #include "sqliteInt.h" */
82393/* #include "vdbeInt.h" */
82394
82395#ifndef SQLITE_OMIT_TRACE
82396
82397/*
82398** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
82399** bytes in this text up to but excluding the first character in
82400** a host parameter. If the text contains no host parameters, return
82401** the total number of bytes in the text.
82402*/
82403static int findNextHostParameter(const char *zSql, int *pnToken){
82404 int tokenType;
82405 int nTotal = 0;
82406 int n;
82407
82408 *pnToken = 0;
82409 while( zSql[0] ){
82410 n = sqlite3GetToken((u8*)zSql, &tokenType);
82411 assert( n>0 && tokenType!=TK_ILLEGAL );
82412 if( tokenType==TK_VARIABLE ){
82413 *pnToken = n;
82414 break;
82415 }
82416 nTotal += n;
82417 zSql += n;
82418 }
82419 return nTotal;
82420}
82421
82422/*
82423** This function returns a pointer to a nul-terminated string in memory
82424** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
82425** string contains a copy of zRawSql but with host parameters expanded to
82426** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
82427** then the returned string holds a copy of zRawSql with "-- " prepended
82428** to each line of text.
82429**
82430** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
82431** then long strings and blobs are truncated to that many bytes. This
82432** can be used to prevent unreasonably large trace strings when dealing
82433** with large (multi-megabyte) strings and blobs.
82434**
82435** The calling function is responsible for making sure the memory returned
82436** is eventually freed.
82437**
82438** ALGORITHM: Scan the input string looking for host parameters in any of
82439** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
82440** string literals, quoted identifier names, and comments. For text forms,
82441** the host parameter index is found by scanning the prepared
82442** statement for the corresponding OP_Variable opcode. Once the host
82443** parameter index is known, locate the value in p->aVar[]. Then render
82444** the value as a literal in place of the host parameter name.
82445*/
82446SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
82447 Vdbe *p, /* The prepared statement being evaluated */
82448 const char *zRawSql /* Raw text of the SQL statement */
82449){
82450 sqlite3 *db; /* The database connection */
82451 int idx = 0; /* Index of a host parameter */
82452 int nextIndex = 1; /* Index of next ? host parameter */
82453 int n; /* Length of a token prefix */
82454 int nToken; /* Length of the parameter token */
82455 int i; /* Loop counter */
82456 Mem *pVar; /* Value of a host parameter */
82457 StrAccum out; /* Accumulate the output here */
82458#ifndef SQLITE_OMIT_UTF16
82459 Mem utf8; /* Used to convert UTF16 into UTF8 for display */
82460#endif
82461 char zBase[100]; /* Initial working space */
82462
82463 db = p->db;
82464 sqlite3StrAccumInit(&out, 0, zBase, sizeof(zBase),
82465 db->aLimit[SQLITE_LIMIT_LENGTH]);
82466 if( db->nVdbeExec>1 ){
82467 while( *zRawSql ){
82468 const char *zStart = zRawSql;
82469 while( *(zRawSql++)!='\n' && *zRawSql );
82470 sqlite3_str_append(&out, "-- ", 3);
82471 assert( (zRawSql - zStart) > 0 );
82472 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
82473 }
82474 }else if( p->nVar==0 ){
82475 sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql));
82476 }else{
82477 while( zRawSql[0] ){
82478 n = findNextHostParameter(zRawSql, &nToken);
82479 assert( n>0 );
82480 sqlite3_str_append(&out, zRawSql, n);
82481 zRawSql += n;
82482 assert( zRawSql[0] || nToken==0 );
82483 if( nToken==0 ) break;
82484 if( zRawSql[0]=='?' ){
82485 if( nToken>1 ){
82486 assert( sqlite3Isdigit(zRawSql[1]) );
82487 sqlite3GetInt32(&zRawSql[1], &idx);
82488 }else{
82489 idx = nextIndex;
82490 }
82491 }else{
82492 assert( zRawSql[0]==':' || zRawSql[0]=='$' ||
82493 zRawSql[0]=='@' || zRawSql[0]=='#' );
82494 testcase( zRawSql[0]==':' );
82495 testcase( zRawSql[0]=='$' );
82496 testcase( zRawSql[0]=='@' );
82497 testcase( zRawSql[0]=='#' );
82498 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
82499 assert( idx>0 );
82500 }
82501 zRawSql += nToken;
82502 nextIndex = idx + 1;
82503 assert( idx>0 && idx<=p->nVar );
82504 pVar = &p->aVar[idx-1];
82505 if( pVar->flags & MEM_Null ){
82506 sqlite3_str_append(&out, "NULL", 4);
82507 }else if( pVar->flags & MEM_Int ){
82508 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
82509 }else if( pVar->flags & MEM_Real ){
82510 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
82511 }else if( pVar->flags & MEM_Str ){
82512 int nOut; /* Number of bytes of the string text to include in output */
82513#ifndef SQLITE_OMIT_UTF16
82514 u8 enc = ENC(db);
82515 if( enc!=SQLITE_UTF8 ){
82516 memset(&utf8, 0, sizeof(utf8));
82517 utf8.db = db;
82518 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
82519 if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8) ){
82520 out.accError = SQLITE_NOMEM;
82521 out.nAlloc = 0;
82522 }
82523 pVar = &utf8;
82524 }
82525#endif
82526 nOut = pVar->n;
82527#ifdef SQLITE_TRACE_SIZE_LIMIT
82528 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
82529 nOut = SQLITE_TRACE_SIZE_LIMIT;
82530 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
82531 }
82532#endif
82533 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
82534#ifdef SQLITE_TRACE_SIZE_LIMIT
82535 if( nOut<pVar->n ){
82536 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
82537 }
82538#endif
82539#ifndef SQLITE_OMIT_UTF16
82540 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
82541#endif
82542 }else if( pVar->flags & MEM_Zero ){
82543 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
82544 }else{
82545 int nOut; /* Number of bytes of the blob to include in output */
82546 assert( pVar->flags & MEM_Blob );
82547 sqlite3_str_append(&out, "x'", 2);
82548 nOut = pVar->n;
82549#ifdef SQLITE_TRACE_SIZE_LIMIT
82550 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
82551#endif
82552 for(i=0; i<nOut; i++){
82553 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
82554 }
82555 sqlite3_str_append(&out, "'", 1);
82556#ifdef SQLITE_TRACE_SIZE_LIMIT
82557 if( nOut<pVar->n ){
82558 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
82559 }
82560#endif
82561 }
82562 }
82563 }
82564 if( out.accError ) sqlite3_str_reset(&out);
82565 return sqlite3StrAccumFinish(&out);
82566}
82567
82568#endif /* #ifndef SQLITE_OMIT_TRACE */
82569
82570/************** End of vdbetrace.c *******************************************/
82571/************** Begin file vdbe.c ********************************************/
82572/*
82573** 2001 September 15
82574**
82575** The author disclaims copyright to this source code. In place of
82576** a legal notice, here is a blessing:
82577**
82578** May you do good and not evil.
82579** May you find forgiveness for yourself and forgive others.
82580** May you share freely, never taking more than you give.
82581**
82582*************************************************************************
82583** The code in this file implements the function that runs the
82584** bytecode of a prepared statement.
82585**
82586** Various scripts scan this source file in order to generate HTML
82587** documentation, headers files, or other derived files. The formatting
82588** of the code in this file is, therefore, important. See other comments
82589** in this file for details. If in doubt, do not deviate from existing
82590** commenting and indentation practices when changing or adding code.
82591*/
82592/* #include "sqliteInt.h" */
82593/* #include "vdbeInt.h" */
82594
82595/*
82596** Invoke this macro on memory cells just prior to changing the
82597** value of the cell. This macro verifies that shallow copies are
82598** not misused. A shallow copy of a string or blob just copies a
82599** pointer to the string or blob, not the content. If the original
82600** is changed while the copy is still in use, the string or blob might
82601** be changed out from under the copy. This macro verifies that nothing
82602** like that ever happens.
82603*/
82604#ifdef SQLITE_DEBUG
82605# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
82606#else
82607# define memAboutToChange(P,M)
82608#endif
82609
82610/*
82611** The following global variable is incremented every time a cursor
82612** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
82613** procedures use this information to make sure that indices are
82614** working correctly. This variable has no function other than to
82615** help verify the correct operation of the library.
82616*/
82617#ifdef SQLITE_TEST
82618SQLITE_API int sqlite3_search_count = 0;
82619#endif
82620
82621/*
82622** When this global variable is positive, it gets decremented once before
82623** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
82624** field of the sqlite3 structure is set in order to simulate an interrupt.
82625**
82626** This facility is used for testing purposes only. It does not function
82627** in an ordinary build.
82628*/
82629#ifdef SQLITE_TEST
82630SQLITE_API int sqlite3_interrupt_count = 0;
82631#endif
82632
82633/*
82634** The next global variable is incremented each type the OP_Sort opcode
82635** is executed. The test procedures use this information to make sure that
82636** sorting is occurring or not occurring at appropriate times. This variable
82637** has no function other than to help verify the correct operation of the
82638** library.
82639*/
82640#ifdef SQLITE_TEST
82641SQLITE_API int sqlite3_sort_count = 0;
82642#endif
82643
82644/*
82645** The next global variable records the size of the largest MEM_Blob
82646** or MEM_Str that has been used by a VDBE opcode. The test procedures
82647** use this information to make sure that the zero-blob functionality
82648** is working correctly. This variable has no function other than to
82649** help verify the correct operation of the library.
82650*/
82651#ifdef SQLITE_TEST
82652SQLITE_API int sqlite3_max_blobsize = 0;
82653static void updateMaxBlobsize(Mem *p){
82654 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
82655 sqlite3_max_blobsize = p->n;
82656 }
82657}
82658#endif
82659
82660/*
82661** This macro evaluates to true if either the update hook or the preupdate
82662** hook are enabled for database connect DB.
82663*/
82664#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
82665# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
82666#else
82667# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
82668#endif
82669
82670/*
82671** The next global variable is incremented each time the OP_Found opcode
82672** is executed. This is used to test whether or not the foreign key
82673** operation implemented using OP_FkIsZero is working. This variable
82674** has no function other than to help verify the correct operation of the
82675** library.
82676*/
82677#ifdef SQLITE_TEST
82678SQLITE_API int sqlite3_found_count = 0;
82679#endif
82680
82681/*
82682** Test a register to see if it exceeds the current maximum blob size.
82683** If it does, record the new maximum blob size.
82684*/
82685#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
82686# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
82687#else
82688# define UPDATE_MAX_BLOBSIZE(P)
82689#endif
82690
82691/*
82692** Invoke the VDBE coverage callback, if that callback is defined. This
82693** feature is used for test suite validation only and does not appear an
82694** production builds.
82695**
82696** M is an integer between 2 and 4. 2 indicates a ordinary two-way
82697** branch (I=0 means fall through and I=1 means taken). 3 indicates
82698** a 3-way branch where the third way is when one of the operands is
82699** NULL. 4 indicates the OP_Jump instruction which has three destinations
82700** depending on whether the first operand is less than, equal to, or greater
82701** than the second.
82702**
82703** iSrcLine is the source code line (from the __LINE__ macro) that
82704** generated the VDBE instruction combined with flag bits. The source
82705** code line number is in the lower 24 bits of iSrcLine and the upper
82706** 8 bytes are flags. The lower three bits of the flags indicate
82707** values for I that should never occur. For example, if the branch is
82708** always taken, the flags should be 0x05 since the fall-through and
82709** alternate branch are never taken. If a branch is never taken then
82710** flags should be 0x06 since only the fall-through approach is allowed.
82711**
82712** Bit 0x04 of the flags indicates an OP_Jump opcode that is only
82713** interested in equal or not-equal. In other words, I==0 and I==2
82714** should be treated the same.
82715**
82716** Since only a line number is retained, not the filename, this macro
82717** only works for amalgamation builds. But that is ok, since these macros
82718** should be no-ops except for special builds used to measure test coverage.
82719*/
82720#if !defined(SQLITE_VDBE_COVERAGE)
82721# define VdbeBranchTaken(I,M)
82722#else
82723# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
82724 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
82725 u8 mNever;
82726 assert( I<=2 ); /* 0: fall through, 1: taken, 2: alternate taken */
82727 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
82728 assert( I<M ); /* I can only be 2 if M is 3 or 4 */
82729 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
82730 I = 1<<I;
82731 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
82732 ** the flags indicate directions that the branch can never go. If
82733 ** a branch really does go in one of those directions, assert right
82734 ** away. */
82735 mNever = iSrcLine >> 24;
82736 assert( (I & mNever)==0 );
82737 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
82738 I |= mNever;
82739 if( M==2 ) I |= 0x04;
82740 if( M==4 ){
82741 I |= 0x08;
82742 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
82743 }
82744 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
82745 iSrcLine&0xffffff, I, M);
82746 }
82747#endif
82748
82749/*
82750** Convert the given register into a string if it isn't one
82751** already. Return non-zero if a malloc() fails.
82752*/
82753#define Stringify(P, enc) \
82754 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
82755 { goto no_mem; }
82756
82757/*
82758** An ephemeral string value (signified by the MEM_Ephem flag) contains
82759** a pointer to a dynamically allocated string where some other entity
82760** is responsible for deallocating that string. Because the register
82761** does not control the string, it might be deleted without the register
82762** knowing it.
82763**
82764** This routine converts an ephemeral string into a dynamically allocated
82765** string that the register itself controls. In other words, it
82766** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
82767*/
82768#define Deephemeralize(P) \
82769 if( ((P)->flags&MEM_Ephem)!=0 \
82770 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
82771
82772/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
82773#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
82774
82775/*
82776** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
82777** if we run out of memory.
82778*/
82779static VdbeCursor *allocateCursor(
82780 Vdbe *p, /* The virtual machine */
82781 int iCur, /* Index of the new VdbeCursor */
82782 int nField, /* Number of fields in the table or index */
82783 int iDb, /* Database the cursor belongs to, or -1 */
82784 u8 eCurType /* Type of the new cursor */
82785){
82786 /* Find the memory cell that will be used to store the blob of memory
82787 ** required for this VdbeCursor structure. It is convenient to use a
82788 ** vdbe memory cell to manage the memory allocation required for a
82789 ** VdbeCursor structure for the following reasons:
82790 **
82791 ** * Sometimes cursor numbers are used for a couple of different
82792 ** purposes in a vdbe program. The different uses might require
82793 ** different sized allocations. Memory cells provide growable
82794 ** allocations.
82795 **
82796 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
82797 ** be freed lazily via the sqlite3_release_memory() API. This
82798 ** minimizes the number of malloc calls made by the system.
82799 **
82800 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
82801 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
82802 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
82803 */
82804 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
82805
82806 int nByte;
82807 VdbeCursor *pCx = 0;
82808 nByte =
82809 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
82810 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
82811
82812 assert( iCur>=0 && iCur<p->nCursor );
82813 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
82814 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
82815 p->apCsr[iCur] = 0;
82816 }
82817 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
82818 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
82819 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
82820 pCx->eCurType = eCurType;
82821 pCx->iDb = iDb;
82822 pCx->nField = nField;
82823 pCx->aOffset = &pCx->aType[nField];
82824 if( eCurType==CURTYPE_BTREE ){
82825 pCx->uc.pCursor = (BtCursor*)
82826 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
82827 sqlite3BtreeCursorZero(pCx->uc.pCursor);
82828 }
82829 }
82830 return pCx;
82831}
82832
82833/*
82834** Try to convert a value into a numeric representation if we can
82835** do so without loss of information. In other words, if the string
82836** looks like a number, convert it into a number. If it does not
82837** look like a number, leave it alone.
82838**
82839** If the bTryForInt flag is true, then extra effort is made to give
82840** an integer representation. Strings that look like floating point
82841** values but which have no fractional component (example: '48.00')
82842** will have a MEM_Int representation when bTryForInt is true.
82843**
82844** If bTryForInt is false, then if the input string contains a decimal
82845** point or exponential notation, the result is only MEM_Real, even
82846** if there is an exact integer representation of the quantity.
82847*/
82848static void applyNumericAffinity(Mem *pRec, int bTryForInt){
82849 double rValue;
82850 i64 iValue;
82851 u8 enc = pRec->enc;
82852 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
82853 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
82854 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
82855 pRec->u.i = iValue;
82856 pRec->flags |= MEM_Int;
82857 }else{
82858 pRec->u.r = rValue;
82859 pRec->flags |= MEM_Real;
82860 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
82861 }
82862 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
82863 ** string representation after computing a numeric equivalent, because the
82864 ** string representation might not be the canonical representation for the
82865 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
82866 pRec->flags &= ~MEM_Str;
82867}
82868
82869/*
82870** Processing is determine by the affinity parameter:
82871**
82872** SQLITE_AFF_INTEGER:
82873** SQLITE_AFF_REAL:
82874** SQLITE_AFF_NUMERIC:
82875** Try to convert pRec to an integer representation or a
82876** floating-point representation if an integer representation
82877** is not possible. Note that the integer representation is
82878** always preferred, even if the affinity is REAL, because
82879** an integer representation is more space efficient on disk.
82880**
82881** SQLITE_AFF_TEXT:
82882** Convert pRec to a text representation.
82883**
82884** SQLITE_AFF_BLOB:
82885** No-op. pRec is unchanged.
82886*/
82887static void applyAffinity(
82888 Mem *pRec, /* The value to apply affinity to */
82889 char affinity, /* The affinity to be applied */
82890 u8 enc /* Use this text encoding */
82891){
82892 if( affinity>=SQLITE_AFF_NUMERIC ){
82893 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
82894 || affinity==SQLITE_AFF_NUMERIC );
82895 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
82896 if( (pRec->flags & MEM_Real)==0 ){
82897 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
82898 }else{
82899 sqlite3VdbeIntegerAffinity(pRec);
82900 }
82901 }
82902 }else if( affinity==SQLITE_AFF_TEXT ){
82903 /* Only attempt the conversion to TEXT if there is an integer or real
82904 ** representation (blob and NULL do not get converted) but no string
82905 ** representation. It would be harmless to repeat the conversion if
82906 ** there is already a string rep, but it is pointless to waste those
82907 ** CPU cycles. */
82908 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
82909 if( (pRec->flags&(MEM_Real|MEM_Int)) ){
82910 sqlite3VdbeMemStringify(pRec, enc, 1);
82911 }
82912 }
82913 pRec->flags &= ~(MEM_Real|MEM_Int);
82914 }
82915}
82916
82917/*
82918** Try to convert the type of a function argument or a result column
82919** into a numeric representation. Use either INTEGER or REAL whichever
82920** is appropriate. But only do the conversion if it is possible without
82921** loss of information and return the revised type of the argument.
82922*/
82923SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
82924 int eType = sqlite3_value_type(pVal);
82925 if( eType==SQLITE_TEXT ){
82926 Mem *pMem = (Mem*)pVal;
82927 applyNumericAffinity(pMem, 0);
82928 eType = sqlite3_value_type(pVal);
82929 }
82930 return eType;
82931}
82932
82933/*
82934** Exported version of applyAffinity(). This one works on sqlite3_value*,
82935** not the internal Mem* type.
82936*/
82937SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
82938 sqlite3_value *pVal,
82939 u8 affinity,
82940 u8 enc
82941){
82942 applyAffinity((Mem *)pVal, affinity, enc);
82943}
82944
82945/*
82946** pMem currently only holds a string type (or maybe a BLOB that we can
82947** interpret as a string if we want to). Compute its corresponding
82948** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
82949** accordingly.
82950*/
82951static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
82952 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
82953 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
82954 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
82955 return 0;
82956 }
82957 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){
82958 return MEM_Int;
82959 }
82960 return MEM_Real;
82961}
82962
82963/*
82964** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
82965** none.
82966**
82967** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
82968** But it does set pMem->u.r and pMem->u.i appropriately.
82969*/
82970static u16 numericType(Mem *pMem){
82971 if( pMem->flags & (MEM_Int|MEM_Real) ){
82972 return pMem->flags & (MEM_Int|MEM_Real);
82973 }
82974 if( pMem->flags & (MEM_Str|MEM_Blob) ){
82975 return computeNumericType(pMem);
82976 }
82977 return 0;
82978}
82979
82980#ifdef SQLITE_DEBUG
82981/*
82982** Write a nice string representation of the contents of cell pMem
82983** into buffer zBuf, length nBuf.
82984*/
82985SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
82986 char *zCsr = zBuf;
82987 int f = pMem->flags;
82988
82989 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
82990
82991 if( f&MEM_Blob ){
82992 int i;
82993 char c;
82994 if( f & MEM_Dyn ){
82995 c = 'z';
82996 assert( (f & (MEM_Static|MEM_Ephem))==0 );
82997 }else if( f & MEM_Static ){
82998 c = 't';
82999 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
83000 }else if( f & MEM_Ephem ){
83001 c = 'e';
83002 assert( (f & (MEM_Static|MEM_Dyn))==0 );
83003 }else{
83004 c = 's';
83005 }
83006 *(zCsr++) = c;
83007 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
83008 zCsr += sqlite3Strlen30(zCsr);
83009 for(i=0; i<16 && i<pMem->n; i++){
83010 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
83011 zCsr += sqlite3Strlen30(zCsr);
83012 }
83013 for(i=0; i<16 && i<pMem->n; i++){
83014 char z = pMem->z[i];
83015 if( z<32 || z>126 ) *zCsr++ = '.';
83016 else *zCsr++ = z;
83017 }
83018 *(zCsr++) = ']';
83019 if( f & MEM_Zero ){
83020 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
83021 zCsr += sqlite3Strlen30(zCsr);
83022 }
83023 *zCsr = '\0';
83024 }else if( f & MEM_Str ){
83025 int j, k;
83026 zBuf[0] = ' ';
83027 if( f & MEM_Dyn ){
83028 zBuf[1] = 'z';
83029 assert( (f & (MEM_Static|MEM_Ephem))==0 );
83030 }else if( f & MEM_Static ){
83031 zBuf[1] = 't';
83032 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
83033 }else if( f & MEM_Ephem ){
83034 zBuf[1] = 'e';
83035 assert( (f & (MEM_Static|MEM_Dyn))==0 );
83036 }else{
83037 zBuf[1] = 's';
83038 }
83039 k = 2;
83040 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
83041 k += sqlite3Strlen30(&zBuf[k]);
83042 zBuf[k++] = '[';
83043 for(j=0; j<15 && j<pMem->n; j++){
83044 u8 c = pMem->z[j];
83045 if( c>=0x20 && c<0x7f ){
83046 zBuf[k++] = c;
83047 }else{
83048 zBuf[k++] = '.';
83049 }
83050 }
83051 zBuf[k++] = ']';
83052 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
83053 k += sqlite3Strlen30(&zBuf[k]);
83054 zBuf[k++] = 0;
83055 }
83056}
83057#endif
83058
83059#ifdef SQLITE_DEBUG
83060/*
83061** Print the value of a register for tracing purposes:
83062*/
83063static void memTracePrint(Mem *p){
83064 if( p->flags & MEM_Undefined ){
83065 printf(" undefined");
83066 }else if( p->flags & MEM_Null ){
83067 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
83068 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
83069 printf(" si:%lld", p->u.i);
83070 }else if( p->flags & MEM_Int ){
83071 printf(" i:%lld", p->u.i);
83072#ifndef SQLITE_OMIT_FLOATING_POINT
83073 }else if( p->flags & MEM_Real ){
83074 printf(" r:%g", p->u.r);
83075#endif
83076 }else if( sqlite3VdbeMemIsRowSet(p) ){
83077 printf(" (rowset)");
83078 }else{
83079 char zBuf[200];
83080 sqlite3VdbeMemPrettyPrint(p, zBuf);
83081 printf(" %s", zBuf);
83082 }
83083 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
83084}
83085static void registerTrace(int iReg, Mem *p){
83086 printf("REG[%d] = ", iReg);
83087 memTracePrint(p);
83088 printf("\n");
83089 sqlite3VdbeCheckMemInvariants(p);
83090}
83091#endif
83092
83093#ifdef SQLITE_DEBUG
83094# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
83095#else
83096# define REGISTER_TRACE(R,M)
83097#endif
83098
83099
83100#ifdef VDBE_PROFILE
83101
83102/*
83103** hwtime.h contains inline assembler code for implementing
83104** high-performance timing routines.
83105*/
83106/************** Include hwtime.h in the middle of vdbe.c *********************/
83107/************** Begin file hwtime.h ******************************************/
83108/*
83109** 2008 May 27
83110**
83111** The author disclaims copyright to this source code. In place of
83112** a legal notice, here is a blessing:
83113**
83114** May you do good and not evil.
83115** May you find forgiveness for yourself and forgive others.
83116** May you share freely, never taking more than you give.
83117**
83118******************************************************************************
83119**
83120** This file contains inline asm code for retrieving "high-performance"
83121** counters for x86 class CPUs.
83122*/
83123#ifndef SQLITE_HWTIME_H
83124#define SQLITE_HWTIME_H
83125
83126/*
83127** The following routine only works on pentium-class (or newer) processors.
83128** It uses the RDTSC opcode to read the cycle count value out of the
83129** processor and returns that value. This can be used for high-res
83130** profiling.
83131*/
83132#if (defined(__GNUC__) || defined(_MSC_VER)) && \
83133 (defined(i386) || defined(__i386__) || defined(_M_IX86))
83134
83135 #if defined(__GNUC__)
83136
83137 __inline__ sqlite_uint64 sqlite3Hwtime(void){
83138 unsigned int lo, hi;
83139 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
83140 return (sqlite_uint64)hi << 32 | lo;
83141 }
83142
83143 #elif defined(_MSC_VER)
83144
83145 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
83146 __asm {
83147 rdtsc
83148 ret ; return value at EDX:EAX
83149 }
83150 }
83151
83152 #endif
83153
83154#elif (defined(__GNUC__) && defined(__x86_64__))
83155
83156 __inline__ sqlite_uint64 sqlite3Hwtime(void){
83157 unsigned long val;
83158 __asm__ __volatile__ ("rdtsc" : "=A" (val));
83159 return val;
83160 }
83161
83162#elif (defined(__GNUC__) && defined(__ppc__))
83163
83164 __inline__ sqlite_uint64 sqlite3Hwtime(void){
83165 unsigned long long retval;
83166 unsigned long junk;
83167 __asm__ __volatile__ ("\n\
83168 1: mftbu %1\n\
83169 mftb %L0\n\
83170 mftbu %0\n\
83171 cmpw %0,%1\n\
83172 bne 1b"
83173 : "=r" (retval), "=r" (junk));
83174 return retval;
83175 }
83176
83177#else
83178
83179 #error Need implementation of sqlite3Hwtime() for your platform.
83180
83181 /*
83182 ** To compile without implementing sqlite3Hwtime() for your platform,
83183 ** you can remove the above #error and use the following
83184 ** stub function. You will lose timing support for many
83185 ** of the debugging and testing utilities, but it should at
83186 ** least compile and run.
83187 */
83188SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
83189
83190#endif
83191
83192#endif /* !defined(SQLITE_HWTIME_H) */
83193
83194/************** End of hwtime.h **********************************************/
83195/************** Continuing where we left off in vdbe.c ***********************/
83196
83197#endif
83198
83199#ifndef NDEBUG
83200/*
83201** This function is only called from within an assert() expression. It
83202** checks that the sqlite3.nTransaction variable is correctly set to
83203** the number of non-transaction savepoints currently in the
83204** linked list starting at sqlite3.pSavepoint.
83205**
83206** Usage:
83207**
83208** assert( checkSavepointCount(db) );
83209*/
83210static int checkSavepointCount(sqlite3 *db){
83211 int n = 0;
83212 Savepoint *p;
83213 for(p=db->pSavepoint; p; p=p->pNext) n++;
83214 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
83215 return 1;
83216}
83217#endif
83218
83219/*
83220** Return the register of pOp->p2 after first preparing it to be
83221** overwritten with an integer value.
83222*/
83223static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
83224 sqlite3VdbeMemSetNull(pOut);
83225 pOut->flags = MEM_Int;
83226 return pOut;
83227}
83228static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
83229 Mem *pOut;
83230 assert( pOp->p2>0 );
83231 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
83232 pOut = &p->aMem[pOp->p2];
83233 memAboutToChange(p, pOut);
83234 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
83235 return out2PrereleaseWithClear(pOut);
83236 }else{
83237 pOut->flags = MEM_Int;
83238 return pOut;
83239 }
83240}
83241
83242
83243/*
83244** Execute as much of a VDBE program as we can.
83245** This is the core of sqlite3_step().
83246*/
83247SQLITE_PRIVATE int sqlite3VdbeExec(
83248 Vdbe *p /* The VDBE */
83249){
83250 Op *aOp = p->aOp; /* Copy of p->aOp */
83251 Op *pOp = aOp; /* Current operation */
83252#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
83253 Op *pOrigOp; /* Value of pOp at the top of the loop */
83254#endif
83255#ifdef SQLITE_DEBUG
83256 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
83257#endif
83258 int rc = SQLITE_OK; /* Value to return */
83259 sqlite3 *db = p->db; /* The database */
83260 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
83261 u8 encoding = ENC(db); /* The database encoding */
83262 int iCompare = 0; /* Result of last comparison */
83263 unsigned nVmStep = 0; /* Number of virtual machine steps */
83264#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
83265 unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
83266#endif
83267 Mem *aMem = p->aMem; /* Copy of p->aMem */
83268 Mem *pIn1 = 0; /* 1st input operand */
83269 Mem *pIn2 = 0; /* 2nd input operand */
83270 Mem *pIn3 = 0; /* 3rd input operand */
83271 Mem *pOut = 0; /* Output operand */
83272#ifdef VDBE_PROFILE
83273 u64 start; /* CPU clock count at start of opcode */
83274#endif
83275 /*** INSERT STACK UNION HERE ***/
83276
83277 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
83278 sqlite3VdbeEnter(p);
83279 if( p->rc==SQLITE_NOMEM ){
83280 /* This happens if a malloc() inside a call to sqlite3_column_text() or
83281 ** sqlite3_column_text16() failed. */
83282 goto no_mem;
83283 }
83284 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
83285 assert( p->bIsReader || p->readOnly!=0 );
83286 p->iCurrentTime = 0;
83287 assert( p->explain==0 );
83288 p->pResultSet = 0;
83289 db->busyHandler.nBusy = 0;
83290 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
83291 sqlite3VdbeIOTraceSql(p);
83292#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
83293 if( db->xProgress ){
83294 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
83295 assert( 0 < db->nProgressOps );
83296 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
83297 }else{
83298 nProgressLimit = 0xffffffff;
83299 }
83300#endif
83301#ifdef SQLITE_DEBUG
83302 sqlite3BeginBenignMalloc();
83303 if( p->pc==0
83304 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
83305 ){
83306 int i;
83307 int once = 1;
83308 sqlite3VdbePrintSql(p);
83309 if( p->db->flags & SQLITE_VdbeListing ){
83310 printf("VDBE Program Listing:\n");
83311 for(i=0; i<p->nOp; i++){
83312 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
83313 }
83314 }
83315 if( p->db->flags & SQLITE_VdbeEQP ){
83316 for(i=0; i<p->nOp; i++){
83317 if( aOp[i].opcode==OP_Explain ){
83318 if( once ) printf("VDBE Query Plan:\n");
83319 printf("%s\n", aOp[i].p4.z);
83320 once = 0;
83321 }
83322 }
83323 }
83324 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
83325 }
83326 sqlite3EndBenignMalloc();
83327#endif
83328 for(pOp=&aOp[p->pc]; 1; pOp++){
83329 /* Errors are detected by individual opcodes, with an immediate
83330 ** jumps to abort_due_to_error. */
83331 assert( rc==SQLITE_OK );
83332
83333 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
83334#ifdef VDBE_PROFILE
83335 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
83336#endif
83337 nVmStep++;
83338#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
83339 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
83340#endif
83341
83342 /* Only allow tracing if SQLITE_DEBUG is defined.
83343 */
83344#ifdef SQLITE_DEBUG
83345 if( db->flags & SQLITE_VdbeTrace ){
83346 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
83347 }
83348#endif
83349
83350
83351 /* Check to see if we need to simulate an interrupt. This only happens
83352 ** if we have a special test build.
83353 */
83354#ifdef SQLITE_TEST
83355 if( sqlite3_interrupt_count>0 ){
83356 sqlite3_interrupt_count--;
83357 if( sqlite3_interrupt_count==0 ){
83358 sqlite3_interrupt(db);
83359 }
83360 }
83361#endif
83362
83363 /* Sanity checking on other operands */
83364#ifdef SQLITE_DEBUG
83365 {
83366 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
83367 if( (opProperty & OPFLG_IN1)!=0 ){
83368 assert( pOp->p1>0 );
83369 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
83370 assert( memIsValid(&aMem[pOp->p1]) );
83371 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
83372 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
83373 }
83374 if( (opProperty & OPFLG_IN2)!=0 ){
83375 assert( pOp->p2>0 );
83376 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
83377 assert( memIsValid(&aMem[pOp->p2]) );
83378 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
83379 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
83380 }
83381 if( (opProperty & OPFLG_IN3)!=0 ){
83382 assert( pOp->p3>0 );
83383 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
83384 assert( memIsValid(&aMem[pOp->p3]) );
83385 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
83386 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
83387 }
83388 if( (opProperty & OPFLG_OUT2)!=0 ){
83389 assert( pOp->p2>0 );
83390 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
83391 memAboutToChange(p, &aMem[pOp->p2]);
83392 }
83393 if( (opProperty & OPFLG_OUT3)!=0 ){
83394 assert( pOp->p3>0 );
83395 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
83396 memAboutToChange(p, &aMem[pOp->p3]);
83397 }
83398 }
83399#endif
83400#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
83401 pOrigOp = pOp;
83402#endif
83403
83404 switch( pOp->opcode ){
83405
83406/*****************************************************************************
83407** What follows is a massive switch statement where each case implements a
83408** separate instruction in the virtual machine. If we follow the usual
83409** indentation conventions, each case should be indented by 6 spaces. But
83410** that is a lot of wasted space on the left margin. So the code within
83411** the switch statement will break with convention and be flush-left. Another
83412** big comment (similar to this one) will mark the point in the code where
83413** we transition back to normal indentation.
83414**
83415** The formatting of each case is important. The makefile for SQLite
83416** generates two C files "opcodes.h" and "opcodes.c" by scanning this
83417** file looking for lines that begin with "case OP_". The opcodes.h files
83418** will be filled with #defines that give unique integer values to each
83419** opcode and the opcodes.c file is filled with an array of strings where
83420** each string is the symbolic name for the corresponding opcode. If the
83421** case statement is followed by a comment of the form "/# same as ... #/"
83422** that comment is used to determine the particular value of the opcode.
83423**
83424** Other keywords in the comment that follows each case are used to
83425** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
83426** Keywords include: in1, in2, in3, out2, out3. See
83427** the mkopcodeh.awk script for additional information.
83428**
83429** Documentation about VDBE opcodes is generated by scanning this file
83430** for lines of that contain "Opcode:". That line and all subsequent
83431** comment lines are used in the generation of the opcode.html documentation
83432** file.
83433**
83434** SUMMARY:
83435**
83436** Formatting is important to scripts that scan this file.
83437** Do not deviate from the formatting style currently in use.
83438**
83439*****************************************************************************/
83440
83441/* Opcode: Goto * P2 * * *
83442**
83443** An unconditional jump to address P2.
83444** The next instruction executed will be
83445** the one at index P2 from the beginning of
83446** the program.
83447**
83448** The P1 parameter is not actually used by this opcode. However, it
83449** is sometimes set to 1 instead of 0 as a hint to the command-line shell
83450** that this Goto is the bottom of a loop and that the lines from P2 down
83451** to the current line should be indented for EXPLAIN output.
83452*/
83453case OP_Goto: { /* jump */
83454jump_to_p2_and_check_for_interrupt:
83455 pOp = &aOp[pOp->p2 - 1];
83456
83457 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
83458 ** OP_VNext, or OP_SorterNext) all jump here upon
83459 ** completion. Check to see if sqlite3_interrupt() has been called
83460 ** or if the progress callback needs to be invoked.
83461 **
83462 ** This code uses unstructured "goto" statements and does not look clean.
83463 ** But that is not due to sloppy coding habits. The code is written this
83464 ** way for performance, to avoid having to run the interrupt and progress
83465 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
83466 ** faster according to "valgrind --tool=cachegrind" */
83467check_for_interrupt:
83468 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
83469#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
83470 /* Call the progress callback if it is configured and the required number
83471 ** of VDBE ops have been executed (either since this invocation of
83472 ** sqlite3VdbeExec() or since last time the progress callback was called).
83473 ** If the progress callback returns non-zero, exit the virtual machine with
83474 ** a return code SQLITE_ABORT.
83475 */
83476 if( nVmStep>=nProgressLimit && db->xProgress!=0 ){
83477 assert( db->nProgressOps!=0 );
83478 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
83479 if( db->xProgress(db->pProgressArg) ){
83480 rc = SQLITE_INTERRUPT;
83481 goto abort_due_to_error;
83482 }
83483 }
83484#endif
83485
83486 break;
83487}
83488
83489/* Opcode: Gosub P1 P2 * * *
83490**
83491** Write the current address onto register P1
83492** and then jump to address P2.
83493*/
83494case OP_Gosub: { /* jump */
83495 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
83496 pIn1 = &aMem[pOp->p1];
83497 assert( VdbeMemDynamic(pIn1)==0 );
83498 memAboutToChange(p, pIn1);
83499 pIn1->flags = MEM_Int;
83500 pIn1->u.i = (int)(pOp-aOp);
83501 REGISTER_TRACE(pOp->p1, pIn1);
83502
83503 /* Most jump operations do a goto to this spot in order to update
83504 ** the pOp pointer. */
83505jump_to_p2:
83506 pOp = &aOp[pOp->p2 - 1];
83507 break;
83508}
83509
83510/* Opcode: Return P1 * * * *
83511**
83512** Jump to the next instruction after the address in register P1. After
83513** the jump, register P1 becomes undefined.
83514*/
83515case OP_Return: { /* in1 */
83516 pIn1 = &aMem[pOp->p1];
83517 assert( pIn1->flags==MEM_Int );
83518 pOp = &aOp[pIn1->u.i];
83519 pIn1->flags = MEM_Undefined;
83520 break;
83521}
83522
83523/* Opcode: InitCoroutine P1 P2 P3 * *
83524**
83525** Set up register P1 so that it will Yield to the coroutine
83526** located at address P3.
83527**
83528** If P2!=0 then the coroutine implementation immediately follows
83529** this opcode. So jump over the coroutine implementation to
83530** address P2.
83531**
83532** See also: EndCoroutine
83533*/
83534case OP_InitCoroutine: { /* jump */
83535 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
83536 assert( pOp->p2>=0 && pOp->p2<p->nOp );
83537 assert( pOp->p3>=0 && pOp->p3<p->nOp );
83538 pOut = &aMem[pOp->p1];
83539 assert( !VdbeMemDynamic(pOut) );
83540 pOut->u.i = pOp->p3 - 1;
83541 pOut->flags = MEM_Int;
83542 if( pOp->p2 ) goto jump_to_p2;
83543 break;
83544}
83545
83546/* Opcode: EndCoroutine P1 * * * *
83547**
83548** The instruction at the address in register P1 is a Yield.
83549** Jump to the P2 parameter of that Yield.
83550** After the jump, register P1 becomes undefined.
83551**
83552** See also: InitCoroutine
83553*/
83554case OP_EndCoroutine: { /* in1 */
83555 VdbeOp *pCaller;
83556 pIn1 = &aMem[pOp->p1];
83557 assert( pIn1->flags==MEM_Int );
83558 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
83559 pCaller = &aOp[pIn1->u.i];
83560 assert( pCaller->opcode==OP_Yield );
83561 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
83562 pOp = &aOp[pCaller->p2 - 1];
83563 pIn1->flags = MEM_Undefined;
83564 break;
83565}
83566
83567/* Opcode: Yield P1 P2 * * *
83568**
83569** Swap the program counter with the value in register P1. This
83570** has the effect of yielding to a coroutine.
83571**
83572** If the coroutine that is launched by this instruction ends with
83573** Yield or Return then continue to the next instruction. But if
83574** the coroutine launched by this instruction ends with
83575** EndCoroutine, then jump to P2 rather than continuing with the
83576** next instruction.
83577**
83578** See also: InitCoroutine
83579*/
83580case OP_Yield: { /* in1, jump */
83581 int pcDest;
83582 pIn1 = &aMem[pOp->p1];
83583 assert( VdbeMemDynamic(pIn1)==0 );
83584 pIn1->flags = MEM_Int;
83585 pcDest = (int)pIn1->u.i;
83586 pIn1->u.i = (int)(pOp - aOp);
83587 REGISTER_TRACE(pOp->p1, pIn1);
83588 pOp = &aOp[pcDest];
83589 break;
83590}
83591
83592/* Opcode: HaltIfNull P1 P2 P3 P4 P5
83593** Synopsis: if r[P3]=null halt
83594**
83595** Check the value in register P3. If it is NULL then Halt using
83596** parameter P1, P2, and P4 as if this were a Halt instruction. If the
83597** value in register P3 is not NULL, then this routine is a no-op.
83598** The P5 parameter should be 1.
83599*/
83600case OP_HaltIfNull: { /* in3 */
83601 pIn3 = &aMem[pOp->p3];
83602#ifdef SQLITE_DEBUG
83603 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
83604#endif
83605 if( (pIn3->flags & MEM_Null)==0 ) break;
83606 /* Fall through into OP_Halt */
83607}
83608
83609/* Opcode: Halt P1 P2 * P4 P5
83610**
83611** Exit immediately. All open cursors, etc are closed
83612** automatically.
83613**
83614** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
83615** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
83616** For errors, it can be some other value. If P1!=0 then P2 will determine
83617** whether or not to rollback the current transaction. Do not rollback
83618** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
83619** then back out all changes that have occurred during this execution of the
83620** VDBE, but do not rollback the transaction.
83621**
83622** If P4 is not null then it is an error message string.
83623**
83624** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
83625**
83626** 0: (no change)
83627** 1: NOT NULL contraint failed: P4
83628** 2: UNIQUE constraint failed: P4
83629** 3: CHECK constraint failed: P4
83630** 4: FOREIGN KEY constraint failed: P4
83631**
83632** If P5 is not zero and P4 is NULL, then everything after the ":" is
83633** omitted.
83634**
83635** There is an implied "Halt 0 0 0" instruction inserted at the very end of
83636** every program. So a jump past the last instruction of the program
83637** is the same as executing Halt.
83638*/
83639case OP_Halt: {
83640 VdbeFrame *pFrame;
83641 int pcx;
83642
83643 pcx = (int)(pOp - aOp);
83644#ifdef SQLITE_DEBUG
83645 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
83646#endif
83647 if( pOp->p1==SQLITE_OK && p->pFrame ){
83648 /* Halt the sub-program. Return control to the parent frame. */
83649 pFrame = p->pFrame;
83650 p->pFrame = pFrame->pParent;
83651 p->nFrame--;
83652 sqlite3VdbeSetChanges(db, p->nChange);
83653 pcx = sqlite3VdbeFrameRestore(pFrame);
83654 if( pOp->p2==OE_Ignore ){
83655 /* Instruction pcx is the OP_Program that invoked the sub-program
83656 ** currently being halted. If the p2 instruction of this OP_Halt
83657 ** instruction is set to OE_Ignore, then the sub-program is throwing
83658 ** an IGNORE exception. In this case jump to the address specified
83659 ** as the p2 of the calling OP_Program. */
83660 pcx = p->aOp[pcx].p2-1;
83661 }
83662 aOp = p->aOp;
83663 aMem = p->aMem;
83664 pOp = &aOp[pcx];
83665 break;
83666 }
83667 p->rc = pOp->p1;
83668 p->errorAction = (u8)pOp->p2;
83669 p->pc = pcx;
83670 assert( pOp->p5<=4 );
83671 if( p->rc ){
83672 if( pOp->p5 ){
83673 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
83674 "FOREIGN KEY" };
83675 testcase( pOp->p5==1 );
83676 testcase( pOp->p5==2 );
83677 testcase( pOp->p5==3 );
83678 testcase( pOp->p5==4 );
83679 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
83680 if( pOp->p4.z ){
83681 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
83682 }
83683 }else{
83684 sqlite3VdbeError(p, "%s", pOp->p4.z);
83685 }
83686 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
83687 }
83688 rc = sqlite3VdbeHalt(p);
83689 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
83690 if( rc==SQLITE_BUSY ){
83691 p->rc = SQLITE_BUSY;
83692 }else{
83693 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
83694 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
83695 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
83696 }
83697 goto vdbe_return;
83698}
83699
83700/* Opcode: Integer P1 P2 * * *
83701** Synopsis: r[P2]=P1
83702**
83703** The 32-bit integer value P1 is written into register P2.
83704*/
83705case OP_Integer: { /* out2 */
83706 pOut = out2Prerelease(p, pOp);
83707 pOut->u.i = pOp->p1;
83708 break;
83709}
83710
83711/* Opcode: Int64 * P2 * P4 *
83712** Synopsis: r[P2]=P4
83713**
83714** P4 is a pointer to a 64-bit integer value.
83715** Write that value into register P2.
83716*/
83717case OP_Int64: { /* out2 */
83718 pOut = out2Prerelease(p, pOp);
83719 assert( pOp->p4.pI64!=0 );
83720 pOut->u.i = *pOp->p4.pI64;
83721 break;
83722}
83723
83724#ifndef SQLITE_OMIT_FLOATING_POINT
83725/* Opcode: Real * P2 * P4 *
83726** Synopsis: r[P2]=P4
83727**
83728** P4 is a pointer to a 64-bit floating point value.
83729** Write that value into register P2.
83730*/
83731case OP_Real: { /* same as TK_FLOAT, out2 */
83732 pOut = out2Prerelease(p, pOp);
83733 pOut->flags = MEM_Real;
83734 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
83735 pOut->u.r = *pOp->p4.pReal;
83736 break;
83737}
83738#endif
83739
83740/* Opcode: String8 * P2 * P4 *
83741** Synopsis: r[P2]='P4'
83742**
83743** P4 points to a nul terminated UTF-8 string. This opcode is transformed
83744** into a String opcode before it is executed for the first time. During
83745** this transformation, the length of string P4 is computed and stored
83746** as the P1 parameter.
83747*/
83748case OP_String8: { /* same as TK_STRING, out2 */
83749 assert( pOp->p4.z!=0 );
83750 pOut = out2Prerelease(p, pOp);
83751 pOp->opcode = OP_String;
83752 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
83753
83754#ifndef SQLITE_OMIT_UTF16
83755 if( encoding!=SQLITE_UTF8 ){
83756 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
83757 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
83758 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
83759 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
83760 assert( VdbeMemDynamic(pOut)==0 );
83761 pOut->szMalloc = 0;
83762 pOut->flags |= MEM_Static;
83763 if( pOp->p4type==P4_DYNAMIC ){
83764 sqlite3DbFree(db, pOp->p4.z);
83765 }
83766 pOp->p4type = P4_DYNAMIC;
83767 pOp->p4.z = pOut->z;
83768 pOp->p1 = pOut->n;
83769 }
83770 testcase( rc==SQLITE_TOOBIG );
83771#endif
83772 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
83773 goto too_big;
83774 }
83775 assert( rc==SQLITE_OK );
83776 /* Fall through to the next case, OP_String */
83777}
83778
83779/* Opcode: String P1 P2 P3 P4 P5
83780** Synopsis: r[P2]='P4' (len=P1)
83781**
83782** The string value P4 of length P1 (bytes) is stored in register P2.
83783**
83784** If P3 is not zero and the content of register P3 is equal to P5, then
83785** the datatype of the register P2 is converted to BLOB. The content is
83786** the same sequence of bytes, it is merely interpreted as a BLOB instead
83787** of a string, as if it had been CAST. In other words:
83788**
83789** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
83790*/
83791case OP_String: { /* out2 */
83792 assert( pOp->p4.z!=0 );
83793 pOut = out2Prerelease(p, pOp);
83794 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
83795 pOut->z = pOp->p4.z;
83796 pOut->n = pOp->p1;
83797 pOut->enc = encoding;
83798 UPDATE_MAX_BLOBSIZE(pOut);
83799#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
83800 if( pOp->p3>0 ){
83801 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
83802 pIn3 = &aMem[pOp->p3];
83803 assert( pIn3->flags & MEM_Int );
83804 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
83805 }
83806#endif
83807 break;
83808}
83809
83810/* Opcode: Null P1 P2 P3 * *
83811** Synopsis: r[P2..P3]=NULL
83812**
83813** Write a NULL into registers P2. If P3 greater than P2, then also write
83814** NULL into register P3 and every register in between P2 and P3. If P3
83815** is less than P2 (typically P3 is zero) then only register P2 is
83816** set to NULL.
83817**
83818** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
83819** NULL values will not compare equal even if SQLITE_NULLEQ is set on
83820** OP_Ne or OP_Eq.
83821*/
83822case OP_Null: { /* out2 */
83823 int cnt;
83824 u16 nullFlag;
83825 pOut = out2Prerelease(p, pOp);
83826 cnt = pOp->p3-pOp->p2;
83827 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
83828 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
83829 pOut->n = 0;
83830#ifdef SQLITE_DEBUG
83831 pOut->uTemp = 0;
83832#endif
83833 while( cnt>0 ){
83834 pOut++;
83835 memAboutToChange(p, pOut);
83836 sqlite3VdbeMemSetNull(pOut);
83837 pOut->flags = nullFlag;
83838 pOut->n = 0;
83839 cnt--;
83840 }
83841 break;
83842}
83843
83844/* Opcode: SoftNull P1 * * * *
83845** Synopsis: r[P1]=NULL
83846**
83847** Set register P1 to have the value NULL as seen by the OP_MakeRecord
83848** instruction, but do not free any string or blob memory associated with
83849** the register, so that if the value was a string or blob that was
83850** previously copied using OP_SCopy, the copies will continue to be valid.
83851*/
83852case OP_SoftNull: {
83853 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
83854 pOut = &aMem[pOp->p1];
83855 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
83856 break;
83857}
83858
83859/* Opcode: Blob P1 P2 * P4 *
83860** Synopsis: r[P2]=P4 (len=P1)
83861**
83862** P4 points to a blob of data P1 bytes long. Store this
83863** blob in register P2.
83864*/
83865case OP_Blob: { /* out2 */
83866 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
83867 pOut = out2Prerelease(p, pOp);
83868 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
83869 pOut->enc = encoding;
83870 UPDATE_MAX_BLOBSIZE(pOut);
83871 break;
83872}
83873
83874/* Opcode: Variable P1 P2 * P4 *
83875** Synopsis: r[P2]=parameter(P1,P4)
83876**
83877** Transfer the values of bound parameter P1 into register P2
83878**
83879** If the parameter is named, then its name appears in P4.
83880** The P4 value is used by sqlite3_bind_parameter_name().
83881*/
83882case OP_Variable: { /* out2 */
83883 Mem *pVar; /* Value being transferred */
83884
83885 assert( pOp->p1>0 && pOp->p1<=p->nVar );
83886 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
83887 pVar = &p->aVar[pOp->p1 - 1];
83888 if( sqlite3VdbeMemTooBig(pVar) ){
83889 goto too_big;
83890 }
83891 pOut = &aMem[pOp->p2];
83892 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
83893 UPDATE_MAX_BLOBSIZE(pOut);
83894 break;
83895}
83896
83897/* Opcode: Move P1 P2 P3 * *
83898** Synopsis: r[P2@P3]=r[P1@P3]
83899**
83900** Move the P3 values in register P1..P1+P3-1 over into
83901** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
83902** left holding a NULL. It is an error for register ranges
83903** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
83904** for P3 to be less than 1.
83905*/
83906case OP_Move: {
83907 int n; /* Number of registers left to copy */
83908 int p1; /* Register to copy from */
83909 int p2; /* Register to copy to */
83910
83911 n = pOp->p3;
83912 p1 = pOp->p1;
83913 p2 = pOp->p2;
83914 assert( n>0 && p1>0 && p2>0 );
83915 assert( p1+n<=p2 || p2+n<=p1 );
83916
83917 pIn1 = &aMem[p1];
83918 pOut = &aMem[p2];
83919 do{
83920 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
83921 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
83922 assert( memIsValid(pIn1) );
83923 memAboutToChange(p, pOut);
83924 sqlite3VdbeMemMove(pOut, pIn1);
83925#ifdef SQLITE_DEBUG
83926 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
83927 pOut->pScopyFrom += pOp->p2 - p1;
83928 }
83929#endif
83930 Deephemeralize(pOut);
83931 REGISTER_TRACE(p2++, pOut);
83932 pIn1++;
83933 pOut++;
83934 }while( --n );
83935 break;
83936}
83937
83938/* Opcode: Copy P1 P2 P3 * *
83939** Synopsis: r[P2@P3+1]=r[P1@P3+1]
83940**
83941** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
83942**
83943** This instruction makes a deep copy of the value. A duplicate
83944** is made of any string or blob constant. See also OP_SCopy.
83945*/
83946case OP_Copy: {
83947 int n;
83948
83949 n = pOp->p3;
83950 pIn1 = &aMem[pOp->p1];
83951 pOut = &aMem[pOp->p2];
83952 assert( pOut!=pIn1 );
83953 while( 1 ){
83954 memAboutToChange(p, pOut);
83955 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
83956 Deephemeralize(pOut);
83957#ifdef SQLITE_DEBUG
83958 pOut->pScopyFrom = 0;
83959#endif
83960 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
83961 if( (n--)==0 ) break;
83962 pOut++;
83963 pIn1++;
83964 }
83965 break;
83966}
83967
83968/* Opcode: SCopy P1 P2 * * *
83969** Synopsis: r[P2]=r[P1]
83970**
83971** Make a shallow copy of register P1 into register P2.
83972**
83973** This instruction makes a shallow copy of the value. If the value
83974** is a string or blob, then the copy is only a pointer to the
83975** original and hence if the original changes so will the copy.
83976** Worse, if the original is deallocated, the copy becomes invalid.
83977** Thus the program must guarantee that the original will not change
83978** during the lifetime of the copy. Use OP_Copy to make a complete
83979** copy.
83980*/
83981case OP_SCopy: { /* out2 */
83982 pIn1 = &aMem[pOp->p1];
83983 pOut = &aMem[pOp->p2];
83984 assert( pOut!=pIn1 );
83985 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
83986#ifdef SQLITE_DEBUG
83987 pOut->pScopyFrom = pIn1;
83988 pOut->mScopyFlags = pIn1->flags;
83989#endif
83990 break;
83991}
83992
83993/* Opcode: IntCopy P1 P2 * * *
83994** Synopsis: r[P2]=r[P1]
83995**
83996** Transfer the integer value held in register P1 into register P2.
83997**
83998** This is an optimized version of SCopy that works only for integer
83999** values.
84000*/
84001case OP_IntCopy: { /* out2 */
84002 pIn1 = &aMem[pOp->p1];
84003 assert( (pIn1->flags & MEM_Int)!=0 );
84004 pOut = &aMem[pOp->p2];
84005 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
84006 break;
84007}
84008
84009/* Opcode: ResultRow P1 P2 * * *
84010** Synopsis: output=r[P1@P2]
84011**
84012** The registers P1 through P1+P2-1 contain a single row of
84013** results. This opcode causes the sqlite3_step() call to terminate
84014** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
84015** structure to provide access to the r(P1)..r(P1+P2-1) values as
84016** the result row.
84017*/
84018case OP_ResultRow: {
84019 Mem *pMem;
84020 int i;
84021 assert( p->nResColumn==pOp->p2 );
84022 assert( pOp->p1>0 );
84023 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
84024
84025#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
84026 /* Run the progress counter just before returning.
84027 */
84028 if( db->xProgress!=0
84029 && nVmStep>=nProgressLimit
84030 && db->xProgress(db->pProgressArg)!=0
84031 ){
84032 rc = SQLITE_INTERRUPT;
84033 goto abort_due_to_error;
84034 }
84035#endif
84036
84037 /* If this statement has violated immediate foreign key constraints, do
84038 ** not return the number of rows modified. And do not RELEASE the statement
84039 ** transaction. It needs to be rolled back. */
84040 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
84041 assert( db->flags&SQLITE_CountRows );
84042 assert( p->usesStmtJournal );
84043 goto abort_due_to_error;
84044 }
84045
84046 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
84047 ** DML statements invoke this opcode to return the number of rows
84048 ** modified to the user. This is the only way that a VM that
84049 ** opens a statement transaction may invoke this opcode.
84050 **
84051 ** In case this is such a statement, close any statement transaction
84052 ** opened by this VM before returning control to the user. This is to
84053 ** ensure that statement-transactions are always nested, not overlapping.
84054 ** If the open statement-transaction is not closed here, then the user
84055 ** may step another VM that opens its own statement transaction. This
84056 ** may lead to overlapping statement transactions.
84057 **
84058 ** The statement transaction is never a top-level transaction. Hence
84059 ** the RELEASE call below can never fail.
84060 */
84061 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
84062 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
84063 assert( rc==SQLITE_OK );
84064
84065 /* Invalidate all ephemeral cursor row caches */
84066 p->cacheCtr = (p->cacheCtr + 2)|1;
84067
84068 /* Make sure the results of the current row are \000 terminated
84069 ** and have an assigned type. The results are de-ephemeralized as
84070 ** a side effect.
84071 */
84072 pMem = p->pResultSet = &aMem[pOp->p1];
84073 for(i=0; i<pOp->p2; i++){
84074 assert( memIsValid(&pMem[i]) );
84075 Deephemeralize(&pMem[i]);
84076 assert( (pMem[i].flags & MEM_Ephem)==0
84077 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
84078 sqlite3VdbeMemNulTerminate(&pMem[i]);
84079 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
84080 }
84081 if( db->mallocFailed ) goto no_mem;
84082
84083 if( db->mTrace & SQLITE_TRACE_ROW ){
84084 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
84085 }
84086
84087 /* Return SQLITE_ROW
84088 */
84089 p->pc = (int)(pOp - aOp) + 1;
84090 rc = SQLITE_ROW;
84091 goto vdbe_return;
84092}
84093
84094/* Opcode: Concat P1 P2 P3 * *
84095** Synopsis: r[P3]=r[P2]+r[P1]
84096**
84097** Add the text in register P1 onto the end of the text in
84098** register P2 and store the result in register P3.
84099** If either the P1 or P2 text are NULL then store NULL in P3.
84100**
84101** P3 = P2 || P1
84102**
84103** It is illegal for P1 and P3 to be the same register. Sometimes,
84104** if P3 is the same register as P2, the implementation is able
84105** to avoid a memcpy().
84106*/
84107case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
84108 i64 nByte;
84109
84110 pIn1 = &aMem[pOp->p1];
84111 pIn2 = &aMem[pOp->p2];
84112 pOut = &aMem[pOp->p3];
84113 assert( pIn1!=pOut );
84114 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
84115 sqlite3VdbeMemSetNull(pOut);
84116 break;
84117 }
84118 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
84119 Stringify(pIn1, encoding);
84120 Stringify(pIn2, encoding);
84121 nByte = pIn1->n + pIn2->n;
84122 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
84123 goto too_big;
84124 }
84125 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
84126 goto no_mem;
84127 }
84128 MemSetTypeFlag(pOut, MEM_Str);
84129 if( pOut!=pIn2 ){
84130 memcpy(pOut->z, pIn2->z, pIn2->n);
84131 }
84132 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
84133 pOut->z[nByte]=0;
84134 pOut->z[nByte+1] = 0;
84135 pOut->flags |= MEM_Term;
84136 pOut->n = (int)nByte;
84137 pOut->enc = encoding;
84138 UPDATE_MAX_BLOBSIZE(pOut);
84139 break;
84140}
84141
84142/* Opcode: Add P1 P2 P3 * *
84143** Synopsis: r[P3]=r[P1]+r[P2]
84144**
84145** Add the value in register P1 to the value in register P2
84146** and store the result in register P3.
84147** If either input is NULL, the result is NULL.
84148*/
84149/* Opcode: Multiply P1 P2 P3 * *
84150** Synopsis: r[P3]=r[P1]*r[P2]
84151**
84152**
84153** Multiply the value in register P1 by the value in register P2
84154** and store the result in register P3.
84155** If either input is NULL, the result is NULL.
84156*/
84157/* Opcode: Subtract P1 P2 P3 * *
84158** Synopsis: r[P3]=r[P2]-r[P1]
84159**
84160** Subtract the value in register P1 from the value in register P2
84161** and store the result in register P3.
84162** If either input is NULL, the result is NULL.
84163*/
84164/* Opcode: Divide P1 P2 P3 * *
84165** Synopsis: r[P3]=r[P2]/r[P1]
84166**
84167** Divide the value in register P1 by the value in register P2
84168** and store the result in register P3 (P3=P2/P1). If the value in
84169** register P1 is zero, then the result is NULL. If either input is
84170** NULL, the result is NULL.
84171*/
84172/* Opcode: Remainder P1 P2 P3 * *
84173** Synopsis: r[P3]=r[P2]%r[P1]
84174**
84175** Compute the remainder after integer register P2 is divided by
84176** register P1 and store the result in register P3.
84177** If the value in register P1 is zero the result is NULL.
84178** If either operand is NULL, the result is NULL.
84179*/
84180case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
84181case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
84182case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
84183case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
84184case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
84185 char bIntint; /* Started out as two integer operands */
84186 u16 flags; /* Combined MEM_* flags from both inputs */
84187 u16 type1; /* Numeric type of left operand */
84188 u16 type2; /* Numeric type of right operand */
84189 i64 iA; /* Integer value of left operand */
84190 i64 iB; /* Integer value of right operand */
84191 double rA; /* Real value of left operand */
84192 double rB; /* Real value of right operand */
84193
84194 pIn1 = &aMem[pOp->p1];
84195 type1 = numericType(pIn1);
84196 pIn2 = &aMem[pOp->p2];
84197 type2 = numericType(pIn2);
84198 pOut = &aMem[pOp->p3];
84199 flags = pIn1->flags | pIn2->flags;
84200 if( (type1 & type2 & MEM_Int)!=0 ){
84201 iA = pIn1->u.i;
84202 iB = pIn2->u.i;
84203 bIntint = 1;
84204 switch( pOp->opcode ){
84205 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
84206 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
84207 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
84208 case OP_Divide: {
84209 if( iA==0 ) goto arithmetic_result_is_null;
84210 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
84211 iB /= iA;
84212 break;
84213 }
84214 default: {
84215 if( iA==0 ) goto arithmetic_result_is_null;
84216 if( iA==-1 ) iA = 1;
84217 iB %= iA;
84218 break;
84219 }
84220 }
84221 pOut->u.i = iB;
84222 MemSetTypeFlag(pOut, MEM_Int);
84223 }else if( (flags & MEM_Null)!=0 ){
84224 goto arithmetic_result_is_null;
84225 }else{
84226 bIntint = 0;
84227fp_math:
84228 rA = sqlite3VdbeRealValue(pIn1);
84229 rB = sqlite3VdbeRealValue(pIn2);
84230 switch( pOp->opcode ){
84231 case OP_Add: rB += rA; break;
84232 case OP_Subtract: rB -= rA; break;
84233 case OP_Multiply: rB *= rA; break;
84234 case OP_Divide: {
84235 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
84236 if( rA==(double)0 ) goto arithmetic_result_is_null;
84237 rB /= rA;
84238 break;
84239 }
84240 default: {
84241 iA = (i64)rA;
84242 iB = (i64)rB;
84243 if( iA==0 ) goto arithmetic_result_is_null;
84244 if( iA==-1 ) iA = 1;
84245 rB = (double)(iB % iA);
84246 break;
84247 }
84248 }
84249#ifdef SQLITE_OMIT_FLOATING_POINT
84250 pOut->u.i = rB;
84251 MemSetTypeFlag(pOut, MEM_Int);
84252#else
84253 if( sqlite3IsNaN(rB) ){
84254 goto arithmetic_result_is_null;
84255 }
84256 pOut->u.r = rB;
84257 MemSetTypeFlag(pOut, MEM_Real);
84258 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
84259 sqlite3VdbeIntegerAffinity(pOut);
84260 }
84261#endif
84262 }
84263 break;
84264
84265arithmetic_result_is_null:
84266 sqlite3VdbeMemSetNull(pOut);
84267 break;
84268}
84269
84270/* Opcode: CollSeq P1 * * P4
84271**
84272** P4 is a pointer to a CollSeq object. If the next call to a user function
84273** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
84274** be returned. This is used by the built-in min(), max() and nullif()
84275** functions.
84276**
84277** If P1 is not zero, then it is a register that a subsequent min() or
84278** max() aggregate will set to 1 if the current row is not the minimum or
84279** maximum. The P1 register is initialized to 0 by this instruction.
84280**
84281** The interface used by the implementation of the aforementioned functions
84282** to retrieve the collation sequence set by this opcode is not available
84283** publicly. Only built-in functions have access to this feature.
84284*/
84285case OP_CollSeq: {
84286 assert( pOp->p4type==P4_COLLSEQ );
84287 if( pOp->p1 ){
84288 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
84289 }
84290 break;
84291}
84292
84293/* Opcode: BitAnd P1 P2 P3 * *
84294** Synopsis: r[P3]=r[P1]&r[P2]
84295**
84296** Take the bit-wise AND of the values in register P1 and P2 and
84297** store the result in register P3.
84298** If either input is NULL, the result is NULL.
84299*/
84300/* Opcode: BitOr P1 P2 P3 * *
84301** Synopsis: r[P3]=r[P1]|r[P2]
84302**
84303** Take the bit-wise OR of the values in register P1 and P2 and
84304** store the result in register P3.
84305** If either input is NULL, the result is NULL.
84306*/
84307/* Opcode: ShiftLeft P1 P2 P3 * *
84308** Synopsis: r[P3]=r[P2]<<r[P1]
84309**
84310** Shift the integer value in register P2 to the left by the
84311** number of bits specified by the integer in register P1.
84312** Store the result in register P3.
84313** If either input is NULL, the result is NULL.
84314*/
84315/* Opcode: ShiftRight P1 P2 P3 * *
84316** Synopsis: r[P3]=r[P2]>>r[P1]
84317**
84318** Shift the integer value in register P2 to the right by the
84319** number of bits specified by the integer in register P1.
84320** Store the result in register P3.
84321** If either input is NULL, the result is NULL.
84322*/
84323case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
84324case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
84325case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
84326case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
84327 i64 iA;
84328 u64 uA;
84329 i64 iB;
84330 u8 op;
84331
84332 pIn1 = &aMem[pOp->p1];
84333 pIn2 = &aMem[pOp->p2];
84334 pOut = &aMem[pOp->p3];
84335 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
84336 sqlite3VdbeMemSetNull(pOut);
84337 break;
84338 }
84339 iA = sqlite3VdbeIntValue(pIn2);
84340 iB = sqlite3VdbeIntValue(pIn1);
84341 op = pOp->opcode;
84342 if( op==OP_BitAnd ){
84343 iA &= iB;
84344 }else if( op==OP_BitOr ){
84345 iA |= iB;
84346 }else if( iB!=0 ){
84347 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
84348
84349 /* If shifting by a negative amount, shift in the other direction */
84350 if( iB<0 ){
84351 assert( OP_ShiftRight==OP_ShiftLeft+1 );
84352 op = 2*OP_ShiftLeft + 1 - op;
84353 iB = iB>(-64) ? -iB : 64;
84354 }
84355
84356 if( iB>=64 ){
84357 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
84358 }else{
84359 memcpy(&uA, &iA, sizeof(uA));
84360 if( op==OP_ShiftLeft ){
84361 uA <<= iB;
84362 }else{
84363 uA >>= iB;
84364 /* Sign-extend on a right shift of a negative number */
84365 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
84366 }
84367 memcpy(&iA, &uA, sizeof(iA));
84368 }
84369 }
84370 pOut->u.i = iA;
84371 MemSetTypeFlag(pOut, MEM_Int);
84372 break;
84373}
84374
84375/* Opcode: AddImm P1 P2 * * *
84376** Synopsis: r[P1]=r[P1]+P2
84377**
84378** Add the constant P2 to the value in register P1.
84379** The result is always an integer.
84380**
84381** To force any register to be an integer, just add 0.
84382*/
84383case OP_AddImm: { /* in1 */
84384 pIn1 = &aMem[pOp->p1];
84385 memAboutToChange(p, pIn1);
84386 sqlite3VdbeMemIntegerify(pIn1);
84387 pIn1->u.i += pOp->p2;
84388 break;
84389}
84390
84391/* Opcode: MustBeInt P1 P2 * * *
84392**
84393** Force the value in register P1 to be an integer. If the value
84394** in P1 is not an integer and cannot be converted into an integer
84395** without data loss, then jump immediately to P2, or if P2==0
84396** raise an SQLITE_MISMATCH exception.
84397*/
84398case OP_MustBeInt: { /* jump, in1 */
84399 pIn1 = &aMem[pOp->p1];
84400 if( (pIn1->flags & MEM_Int)==0 ){
84401 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
84402 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
84403 if( (pIn1->flags & MEM_Int)==0 ){
84404 if( pOp->p2==0 ){
84405 rc = SQLITE_MISMATCH;
84406 goto abort_due_to_error;
84407 }else{
84408 goto jump_to_p2;
84409 }
84410 }
84411 }
84412 MemSetTypeFlag(pIn1, MEM_Int);
84413 break;
84414}
84415
84416#ifndef SQLITE_OMIT_FLOATING_POINT
84417/* Opcode: RealAffinity P1 * * * *
84418**
84419** If register P1 holds an integer convert it to a real value.
84420**
84421** This opcode is used when extracting information from a column that
84422** has REAL affinity. Such column values may still be stored as
84423** integers, for space efficiency, but after extraction we want them
84424** to have only a real value.
84425*/
84426case OP_RealAffinity: { /* in1 */
84427 pIn1 = &aMem[pOp->p1];
84428 if( pIn1->flags & MEM_Int ){
84429 sqlite3VdbeMemRealify(pIn1);
84430 }
84431 break;
84432}
84433#endif
84434
84435#ifndef SQLITE_OMIT_CAST
84436/* Opcode: Cast P1 P2 * * *
84437** Synopsis: affinity(r[P1])
84438**
84439** Force the value in register P1 to be the type defined by P2.
84440**
84441** <ul>
84442** <li> P2=='A' &rarr; BLOB
84443** <li> P2=='B' &rarr; TEXT
84444** <li> P2=='C' &rarr; NUMERIC
84445** <li> P2=='D' &rarr; INTEGER
84446** <li> P2=='E' &rarr; REAL
84447** </ul>
84448**
84449** A NULL value is not changed by this routine. It remains NULL.
84450*/
84451case OP_Cast: { /* in1 */
84452 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
84453 testcase( pOp->p2==SQLITE_AFF_TEXT );
84454 testcase( pOp->p2==SQLITE_AFF_BLOB );
84455 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
84456 testcase( pOp->p2==SQLITE_AFF_INTEGER );
84457 testcase( pOp->p2==SQLITE_AFF_REAL );
84458 pIn1 = &aMem[pOp->p1];
84459 memAboutToChange(p, pIn1);
84460 rc = ExpandBlob(pIn1);
84461 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
84462 UPDATE_MAX_BLOBSIZE(pIn1);
84463 if( rc ) goto abort_due_to_error;
84464 break;
84465}
84466#endif /* SQLITE_OMIT_CAST */
84467
84468/* Opcode: Eq P1 P2 P3 P4 P5
84469** Synopsis: IF r[P3]==r[P1]
84470**
84471** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
84472** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
84473** store the result of comparison in register P2.
84474**
84475** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
84476** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
84477** to coerce both inputs according to this affinity before the
84478** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
84479** affinity is used. Note that the affinity conversions are stored
84480** back into the input registers P1 and P3. So this opcode can cause
84481** persistent changes to registers P1 and P3.
84482**
84483** Once any conversions have taken place, and neither value is NULL,
84484** the values are compared. If both values are blobs then memcmp() is
84485** used to determine the results of the comparison. If both values
84486** are text, then the appropriate collating function specified in
84487** P4 is used to do the comparison. If P4 is not specified then
84488** memcmp() is used to compare text string. If both values are
84489** numeric, then a numeric comparison is used. If the two values
84490** are of different types, then numbers are considered less than
84491** strings and strings are considered less than blobs.
84492**
84493** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
84494** true or false and is never NULL. If both operands are NULL then the result
84495** of comparison is true. If either operand is NULL then the result is false.
84496** If neither operand is NULL the result is the same as it would be if
84497** the SQLITE_NULLEQ flag were omitted from P5.
84498**
84499** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
84500** content of r[P2] is only changed if the new value is NULL or 0 (false).
84501** In other words, a prior r[P2] value will not be overwritten by 1 (true).
84502*/
84503/* Opcode: Ne P1 P2 P3 P4 P5
84504** Synopsis: IF r[P3]!=r[P1]
84505**
84506** This works just like the Eq opcode except that the jump is taken if
84507** the operands in registers P1 and P3 are not equal. See the Eq opcode for
84508** additional information.
84509**
84510** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
84511** content of r[P2] is only changed if the new value is NULL or 1 (true).
84512** In other words, a prior r[P2] value will not be overwritten by 0 (false).
84513*/
84514/* Opcode: Lt P1 P2 P3 P4 P5
84515** Synopsis: IF r[P3]<r[P1]
84516**
84517** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
84518** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
84519** the result of comparison (0 or 1 or NULL) into register P2.
84520**
84521** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
84522** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
84523** bit is clear then fall through if either operand is NULL.
84524**
84525** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
84526** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
84527** to coerce both inputs according to this affinity before the
84528** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
84529** affinity is used. Note that the affinity conversions are stored
84530** back into the input registers P1 and P3. So this opcode can cause
84531** persistent changes to registers P1 and P3.
84532**
84533** Once any conversions have taken place, and neither value is NULL,
84534** the values are compared. If both values are blobs then memcmp() is
84535** used to determine the results of the comparison. If both values
84536** are text, then the appropriate collating function specified in
84537** P4 is used to do the comparison. If P4 is not specified then
84538** memcmp() is used to compare text string. If both values are
84539** numeric, then a numeric comparison is used. If the two values
84540** are of different types, then numbers are considered less than
84541** strings and strings are considered less than blobs.
84542*/
84543/* Opcode: Le P1 P2 P3 P4 P5
84544** Synopsis: IF r[P3]<=r[P1]
84545**
84546** This works just like the Lt opcode except that the jump is taken if
84547** the content of register P3 is less than or equal to the content of
84548** register P1. See the Lt opcode for additional information.
84549*/
84550/* Opcode: Gt P1 P2 P3 P4 P5
84551** Synopsis: IF r[P3]>r[P1]
84552**
84553** This works just like the Lt opcode except that the jump is taken if
84554** the content of register P3 is greater than the content of
84555** register P1. See the Lt opcode for additional information.
84556*/
84557/* Opcode: Ge P1 P2 P3 P4 P5
84558** Synopsis: IF r[P3]>=r[P1]
84559**
84560** This works just like the Lt opcode except that the jump is taken if
84561** the content of register P3 is greater than or equal to the content of
84562** register P1. See the Lt opcode for additional information.
84563*/
84564case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
84565case OP_Ne: /* same as TK_NE, jump, in1, in3 */
84566case OP_Lt: /* same as TK_LT, jump, in1, in3 */
84567case OP_Le: /* same as TK_LE, jump, in1, in3 */
84568case OP_Gt: /* same as TK_GT, jump, in1, in3 */
84569case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
84570 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
84571 char affinity; /* Affinity to use for comparison */
84572 u16 flags1; /* Copy of initial value of pIn1->flags */
84573 u16 flags3; /* Copy of initial value of pIn3->flags */
84574
84575 pIn1 = &aMem[pOp->p1];
84576 pIn3 = &aMem[pOp->p3];
84577 flags1 = pIn1->flags;
84578 flags3 = pIn3->flags;
84579 if( (flags1 | flags3)&MEM_Null ){
84580 /* One or both operands are NULL */
84581 if( pOp->p5 & SQLITE_NULLEQ ){
84582 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
84583 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
84584 ** or not both operands are null.
84585 */
84586 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
84587 assert( (flags1 & MEM_Cleared)==0 );
84588 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
84589 if( (flags1&flags3&MEM_Null)!=0
84590 && (flags3&MEM_Cleared)==0
84591 ){
84592 res = 0; /* Operands are equal */
84593 }else{
84594 res = 1; /* Operands are not equal */
84595 }
84596 }else{
84597 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
84598 ** then the result is always NULL.
84599 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
84600 */
84601 if( pOp->p5 & SQLITE_STOREP2 ){
84602 pOut = &aMem[pOp->p2];
84603 iCompare = 1; /* Operands are not equal */
84604 memAboutToChange(p, pOut);
84605 MemSetTypeFlag(pOut, MEM_Null);
84606 REGISTER_TRACE(pOp->p2, pOut);
84607 }else{
84608 VdbeBranchTaken(2,3);
84609 if( pOp->p5 & SQLITE_JUMPIFNULL ){
84610 goto jump_to_p2;
84611 }
84612 }
84613 break;
84614 }
84615 }else{
84616 /* Neither operand is NULL. Do a comparison. */
84617 affinity = pOp->p5 & SQLITE_AFF_MASK;
84618 if( affinity>=SQLITE_AFF_NUMERIC ){
84619 if( (flags1 | flags3)&MEM_Str ){
84620 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
84621 applyNumericAffinity(pIn1,0);
84622 assert( flags3==pIn3->flags );
84623 /* testcase( flags3!=pIn3->flags );
84624 ** this used to be possible with pIn1==pIn3, but not since
84625 ** the column cache was removed. The following assignment
84626 ** is essentially a no-op. But, it provides defense-in-depth
84627 ** in case our analysis is incorrect, so it is left in. */
84628 flags3 = pIn3->flags;
84629 }
84630 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
84631 applyNumericAffinity(pIn3,0);
84632 }
84633 }
84634 /* Handle the common case of integer comparison here, as an
84635 ** optimization, to avoid a call to sqlite3MemCompare() */
84636 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
84637 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
84638 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
84639 res = 0;
84640 goto compare_op;
84641 }
84642 }else if( affinity==SQLITE_AFF_TEXT ){
84643 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
84644 testcase( pIn1->flags & MEM_Int );
84645 testcase( pIn1->flags & MEM_Real );
84646 sqlite3VdbeMemStringify(pIn1, encoding, 1);
84647 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
84648 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
84649 assert( pIn1!=pIn3 );
84650 }
84651 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
84652 testcase( pIn3->flags & MEM_Int );
84653 testcase( pIn3->flags & MEM_Real );
84654 sqlite3VdbeMemStringify(pIn3, encoding, 1);
84655 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
84656 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
84657 }
84658 }
84659 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
84660 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
84661 }
84662compare_op:
84663 /* At this point, res is negative, zero, or positive if reg[P1] is
84664 ** less than, equal to, or greater than reg[P3], respectively. Compute
84665 ** the answer to this operator in res2, depending on what the comparison
84666 ** operator actually is. The next block of code depends on the fact
84667 ** that the 6 comparison operators are consecutive integers in this
84668 ** order: NE, EQ, GT, LE, LT, GE */
84669 assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
84670 assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
84671 if( res<0 ){ /* ne, eq, gt, le, lt, ge */
84672 static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 };
84673 res2 = aLTb[pOp->opcode - OP_Ne];
84674 }else if( res==0 ){
84675 static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 };
84676 res2 = aEQb[pOp->opcode - OP_Ne];
84677 }else{
84678 static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 };
84679 res2 = aGTb[pOp->opcode - OP_Ne];
84680 }
84681
84682 /* Undo any changes made by applyAffinity() to the input registers. */
84683 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
84684 pIn1->flags = flags1;
84685 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
84686 pIn3->flags = flags3;
84687
84688 if( pOp->p5 & SQLITE_STOREP2 ){
84689 pOut = &aMem[pOp->p2];
84690 iCompare = res;
84691 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
84692 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
84693 ** and prevents OP_Ne from overwriting NULL with 0. This flag
84694 ** is only used in contexts where either:
84695 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
84696 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
84697 ** Therefore it is not necessary to check the content of r[P2] for
84698 ** NULL. */
84699 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
84700 assert( res2==0 || res2==1 );
84701 testcase( res2==0 && pOp->opcode==OP_Eq );
84702 testcase( res2==1 && pOp->opcode==OP_Eq );
84703 testcase( res2==0 && pOp->opcode==OP_Ne );
84704 testcase( res2==1 && pOp->opcode==OP_Ne );
84705 if( (pOp->opcode==OP_Eq)==res2 ) break;
84706 }
84707 memAboutToChange(p, pOut);
84708 MemSetTypeFlag(pOut, MEM_Int);
84709 pOut->u.i = res2;
84710 REGISTER_TRACE(pOp->p2, pOut);
84711 }else{
84712 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
84713 if( res2 ){
84714 goto jump_to_p2;
84715 }
84716 }
84717 break;
84718}
84719
84720/* Opcode: ElseNotEq * P2 * * *
84721**
84722** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
84723** If result of an OP_Eq comparison on the same two operands
84724** would have be NULL or false (0), then then jump to P2.
84725** If the result of an OP_Eq comparison on the two previous operands
84726** would have been true (1), then fall through.
84727*/
84728case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
84729 assert( pOp>aOp );
84730 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
84731 assert( pOp[-1].p5 & SQLITE_STOREP2 );
84732 VdbeBranchTaken(iCompare!=0, 2);
84733 if( iCompare!=0 ) goto jump_to_p2;
84734 break;
84735}
84736
84737
84738/* Opcode: Permutation * * * P4 *
84739**
84740** Set the permutation used by the OP_Compare operator in the next
84741** instruction. The permutation is stored in the P4 operand.
84742**
84743** The permutation is only valid until the next OP_Compare that has
84744** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
84745** occur immediately prior to the OP_Compare.
84746**
84747** The first integer in the P4 integer array is the length of the array
84748** and does not become part of the permutation.
84749*/
84750case OP_Permutation: {
84751 assert( pOp->p4type==P4_INTARRAY );
84752 assert( pOp->p4.ai );
84753 assert( pOp[1].opcode==OP_Compare );
84754 assert( pOp[1].p5 & OPFLAG_PERMUTE );
84755 break;
84756}
84757
84758/* Opcode: Compare P1 P2 P3 P4 P5
84759** Synopsis: r[P1@P3] <-> r[P2@P3]
84760**
84761** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
84762** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
84763** the comparison for use by the next OP_Jump instruct.
84764**
84765** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
84766** determined by the most recent OP_Permutation operator. If the
84767** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
84768** order.
84769**
84770** P4 is a KeyInfo structure that defines collating sequences and sort
84771** orders for the comparison. The permutation applies to registers
84772** only. The KeyInfo elements are used sequentially.
84773**
84774** The comparison is a sort comparison, so NULLs compare equal,
84775** NULLs are less than numbers, numbers are less than strings,
84776** and strings are less than blobs.
84777*/
84778case OP_Compare: {
84779 int n;
84780 int i;
84781 int p1;
84782 int p2;
84783 const KeyInfo *pKeyInfo;
84784 int idx;
84785 CollSeq *pColl; /* Collating sequence to use on this term */
84786 int bRev; /* True for DESCENDING sort order */
84787 int *aPermute; /* The permutation */
84788
84789 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
84790 aPermute = 0;
84791 }else{
84792 assert( pOp>aOp );
84793 assert( pOp[-1].opcode==OP_Permutation );
84794 assert( pOp[-1].p4type==P4_INTARRAY );
84795 aPermute = pOp[-1].p4.ai + 1;
84796 assert( aPermute!=0 );
84797 }
84798 n = pOp->p3;
84799 pKeyInfo = pOp->p4.pKeyInfo;
84800 assert( n>0 );
84801 assert( pKeyInfo!=0 );
84802 p1 = pOp->p1;
84803 p2 = pOp->p2;
84804#ifdef SQLITE_DEBUG
84805 if( aPermute ){
84806 int k, mx = 0;
84807 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
84808 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
84809 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
84810 }else{
84811 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
84812 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
84813 }
84814#endif /* SQLITE_DEBUG */
84815 for(i=0; i<n; i++){
84816 idx = aPermute ? aPermute[i] : i;
84817 assert( memIsValid(&aMem[p1+idx]) );
84818 assert( memIsValid(&aMem[p2+idx]) );
84819 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
84820 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
84821 assert( i<pKeyInfo->nKeyField );
84822 pColl = pKeyInfo->aColl[i];
84823 bRev = pKeyInfo->aSortOrder[i];
84824 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
84825 if( iCompare ){
84826 if( bRev ) iCompare = -iCompare;
84827 break;
84828 }
84829 }
84830 break;
84831}
84832
84833/* Opcode: Jump P1 P2 P3 * *
84834**
84835** Jump to the instruction at address P1, P2, or P3 depending on whether
84836** in the most recent OP_Compare instruction the P1 vector was less than
84837** equal to, or greater than the P2 vector, respectively.
84838*/
84839case OP_Jump: { /* jump */
84840 if( iCompare<0 ){
84841 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
84842 }else if( iCompare==0 ){
84843 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
84844 }else{
84845 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
84846 }
84847 break;
84848}
84849
84850/* Opcode: And P1 P2 P3 * *
84851** Synopsis: r[P3]=(r[P1] && r[P2])
84852**
84853** Take the logical AND of the values in registers P1 and P2 and
84854** write the result into register P3.
84855**
84856** If either P1 or P2 is 0 (false) then the result is 0 even if
84857** the other input is NULL. A NULL and true or two NULLs give
84858** a NULL output.
84859*/
84860/* Opcode: Or P1 P2 P3 * *
84861** Synopsis: r[P3]=(r[P1] || r[P2])
84862**
84863** Take the logical OR of the values in register P1 and P2 and
84864** store the answer in register P3.
84865**
84866** If either P1 or P2 is nonzero (true) then the result is 1 (true)
84867** even if the other input is NULL. A NULL and false or two NULLs
84868** give a NULL output.
84869*/
84870case OP_And: /* same as TK_AND, in1, in2, out3 */
84871case OP_Or: { /* same as TK_OR, in1, in2, out3 */
84872 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
84873 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
84874
84875 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
84876 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
84877 if( pOp->opcode==OP_And ){
84878 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
84879 v1 = and_logic[v1*3+v2];
84880 }else{
84881 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
84882 v1 = or_logic[v1*3+v2];
84883 }
84884 pOut = &aMem[pOp->p3];
84885 if( v1==2 ){
84886 MemSetTypeFlag(pOut, MEM_Null);
84887 }else{
84888 pOut->u.i = v1;
84889 MemSetTypeFlag(pOut, MEM_Int);
84890 }
84891 break;
84892}
84893
84894/* Opcode: IsTrue P1 P2 P3 P4 *
84895** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
84896**
84897** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
84898** IS NOT FALSE operators.
84899**
84900** Interpret the value in register P1 as a boolean value. Store that
84901** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
84902** NULL, then the P3 is stored in register P2. Invert the answer if P4
84903** is 1.
84904**
84905** The logic is summarized like this:
84906**
84907** <ul>
84908** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
84909** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
84910** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
84911** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
84912** </ul>
84913*/
84914case OP_IsTrue: { /* in1, out2 */
84915 assert( pOp->p4type==P4_INT32 );
84916 assert( pOp->p4.i==0 || pOp->p4.i==1 );
84917 assert( pOp->p3==0 || pOp->p3==1 );
84918 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
84919 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
84920 break;
84921}
84922
84923/* Opcode: Not P1 P2 * * *
84924** Synopsis: r[P2]= !r[P1]
84925**
84926** Interpret the value in register P1 as a boolean value. Store the
84927** boolean complement in register P2. If the value in register P1 is
84928** NULL, then a NULL is stored in P2.
84929*/
84930case OP_Not: { /* same as TK_NOT, in1, out2 */
84931 pIn1 = &aMem[pOp->p1];
84932 pOut = &aMem[pOp->p2];
84933 if( (pIn1->flags & MEM_Null)==0 ){
84934 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
84935 }else{
84936 sqlite3VdbeMemSetNull(pOut);
84937 }
84938 break;
84939}
84940
84941/* Opcode: BitNot P1 P2 * * *
84942** Synopsis: r[P2]= ~r[P1]
84943**
84944** Interpret the content of register P1 as an integer. Store the
84945** ones-complement of the P1 value into register P2. If P1 holds
84946** a NULL then store a NULL in P2.
84947*/
84948case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
84949 pIn1 = &aMem[pOp->p1];
84950 pOut = &aMem[pOp->p2];
84951 sqlite3VdbeMemSetNull(pOut);
84952 if( (pIn1->flags & MEM_Null)==0 ){
84953 pOut->flags = MEM_Int;
84954 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
84955 }
84956 break;
84957}
84958
84959/* Opcode: Once P1 P2 * * *
84960**
84961** Fall through to the next instruction the first time this opcode is
84962** encountered on each invocation of the byte-code program. Jump to P2
84963** on the second and all subsequent encounters during the same invocation.
84964**
84965** Top-level programs determine first invocation by comparing the P1
84966** operand against the P1 operand on the OP_Init opcode at the beginning
84967** of the program. If the P1 values differ, then fall through and make
84968** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
84969** the same then take the jump.
84970**
84971** For subprograms, there is a bitmask in the VdbeFrame that determines
84972** whether or not the jump should be taken. The bitmask is necessary
84973** because the self-altering code trick does not work for recursive
84974** triggers.
84975*/
84976case OP_Once: { /* jump */
84977 u32 iAddr; /* Address of this instruction */
84978 assert( p->aOp[0].opcode==OP_Init );
84979 if( p->pFrame ){
84980 iAddr = (int)(pOp - p->aOp);
84981 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
84982 VdbeBranchTaken(1, 2);
84983 goto jump_to_p2;
84984 }
84985 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
84986 }else{
84987 if( p->aOp[0].p1==pOp->p1 ){
84988 VdbeBranchTaken(1, 2);
84989 goto jump_to_p2;
84990 }
84991 }
84992 VdbeBranchTaken(0, 2);
84993 pOp->p1 = p->aOp[0].p1;
84994 break;
84995}
84996
84997/* Opcode: If P1 P2 P3 * *
84998**
84999** Jump to P2 if the value in register P1 is true. The value
85000** is considered true if it is numeric and non-zero. If the value
85001** in P1 is NULL then take the jump if and only if P3 is non-zero.
85002*/
85003case OP_If: { /* jump, in1 */
85004 int c;
85005 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
85006 VdbeBranchTaken(c!=0, 2);
85007 if( c ) goto jump_to_p2;
85008 break;
85009}
85010
85011/* Opcode: IfNot P1 P2 P3 * *
85012**
85013** Jump to P2 if the value in register P1 is False. The value
85014** is considered false if it has a numeric value of zero. If the value
85015** in P1 is NULL then take the jump if and only if P3 is non-zero.
85016*/
85017case OP_IfNot: { /* jump, in1 */
85018 int c;
85019 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
85020 VdbeBranchTaken(c!=0, 2);
85021 if( c ) goto jump_to_p2;
85022 break;
85023}
85024
85025/* Opcode: IsNull P1 P2 * * *
85026** Synopsis: if r[P1]==NULL goto P2
85027**
85028** Jump to P2 if the value in register P1 is NULL.
85029*/
85030case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
85031 pIn1 = &aMem[pOp->p1];
85032 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
85033 if( (pIn1->flags & MEM_Null)!=0 ){
85034 goto jump_to_p2;
85035 }
85036 break;
85037}
85038
85039/* Opcode: NotNull P1 P2 * * *
85040** Synopsis: if r[P1]!=NULL goto P2
85041**
85042** Jump to P2 if the value in register P1 is not NULL.
85043*/
85044case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
85045 pIn1 = &aMem[pOp->p1];
85046 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
85047 if( (pIn1->flags & MEM_Null)==0 ){
85048 goto jump_to_p2;
85049 }
85050 break;
85051}
85052
85053/* Opcode: IfNullRow P1 P2 P3 * *
85054** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
85055**
85056** Check the cursor P1 to see if it is currently pointing at a NULL row.
85057** If it is, then set register P3 to NULL and jump immediately to P2.
85058** If P1 is not on a NULL row, then fall through without making any
85059** changes.
85060*/
85061case OP_IfNullRow: { /* jump */
85062 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
85063 assert( p->apCsr[pOp->p1]!=0 );
85064 if( p->apCsr[pOp->p1]->nullRow ){
85065 sqlite3VdbeMemSetNull(aMem + pOp->p3);
85066 goto jump_to_p2;
85067 }
85068 break;
85069}
85070
85071#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
85072/* Opcode: Offset P1 P2 P3 * *
85073** Synopsis: r[P3] = sqlite_offset(P1)
85074**
85075** Store in register r[P3] the byte offset into the database file that is the
85076** start of the payload for the record at which that cursor P1 is currently
85077** pointing.
85078**
85079** P2 is the column number for the argument to the sqlite_offset() function.
85080** This opcode does not use P2 itself, but the P2 value is used by the
85081** code generator. The P1, P2, and P3 operands to this opcode are the
85082** same as for OP_Column.
85083**
85084** This opcode is only available if SQLite is compiled with the
85085** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
85086*/
85087case OP_Offset: { /* out3 */
85088 VdbeCursor *pC; /* The VDBE cursor */
85089 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
85090 pC = p->apCsr[pOp->p1];
85091 pOut = &p->aMem[pOp->p3];
85092 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
85093 sqlite3VdbeMemSetNull(pOut);
85094 }else{
85095 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
85096 }
85097 break;
85098}
85099#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
85100
85101/* Opcode: Column P1 P2 P3 P4 P5
85102** Synopsis: r[P3]=PX
85103**
85104** Interpret the data that cursor P1 points to as a structure built using
85105** the MakeRecord instruction. (See the MakeRecord opcode for additional
85106** information about the format of the data.) Extract the P2-th column
85107** from this record. If there are less that (P2+1)
85108** values in the record, extract a NULL.
85109**
85110** The value extracted is stored in register P3.
85111**
85112** If the record contains fewer than P2 fields, then extract a NULL. Or,
85113** if the P4 argument is a P4_MEM use the value of the P4 argument as
85114** the result.
85115**
85116** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
85117** then the cache of the cursor is reset prior to extracting the column.
85118** The first OP_Column against a pseudo-table after the value of the content
85119** register has changed should have this bit set.
85120**
85121** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
85122** the result is guaranteed to only be used as the argument of a length()
85123** or typeof() function, respectively. The loading of large blobs can be
85124** skipped for length() and all content loading can be skipped for typeof().
85125*/
85126case OP_Column: {
85127 int p2; /* column number to retrieve */
85128 VdbeCursor *pC; /* The VDBE cursor */
85129 BtCursor *pCrsr; /* The BTree cursor */
85130 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
85131 int len; /* The length of the serialized data for the column */
85132 int i; /* Loop counter */
85133 Mem *pDest; /* Where to write the extracted value */
85134 Mem sMem; /* For storing the record being decoded */
85135 const u8 *zData; /* Part of the record being decoded */
85136 const u8 *zHdr; /* Next unparsed byte of the header */
85137 const u8 *zEndHdr; /* Pointer to first byte after the header */
85138 u64 offset64; /* 64-bit offset */
85139 u32 t; /* A type code from the record header */
85140 Mem *pReg; /* PseudoTable input register */
85141
85142 pC = p->apCsr[pOp->p1];
85143 p2 = pOp->p2;
85144
85145 /* If the cursor cache is stale (meaning it is not currently point at
85146 ** the correct row) then bring it up-to-date by doing the necessary
85147 ** B-Tree seek. */
85148 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
85149 if( rc ) goto abort_due_to_error;
85150
85151 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
85152 pDest = &aMem[pOp->p3];
85153 memAboutToChange(p, pDest);
85154 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
85155 assert( pC!=0 );
85156 assert( p2<pC->nField );
85157 aOffset = pC->aOffset;
85158 assert( pC->eCurType!=CURTYPE_VTAB );
85159 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
85160 assert( pC->eCurType!=CURTYPE_SORTER );
85161
85162 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
85163 if( pC->nullRow ){
85164 if( pC->eCurType==CURTYPE_PSEUDO ){
85165 /* For the special case of as pseudo-cursor, the seekResult field
85166 ** identifies the register that holds the record */
85167 assert( pC->seekResult>0 );
85168 pReg = &aMem[pC->seekResult];
85169 assert( pReg->flags & MEM_Blob );
85170 assert( memIsValid(pReg) );
85171 pC->payloadSize = pC->szRow = pReg->n;
85172 pC->aRow = (u8*)pReg->z;
85173 }else{
85174 sqlite3VdbeMemSetNull(pDest);
85175 goto op_column_out;
85176 }
85177 }else{
85178 pCrsr = pC->uc.pCursor;
85179 assert( pC->eCurType==CURTYPE_BTREE );
85180 assert( pCrsr );
85181 assert( sqlite3BtreeCursorIsValid(pCrsr) );
85182 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
85183 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
85184 assert( pC->szRow<=pC->payloadSize );
85185 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
85186 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
85187 goto too_big;
85188 }
85189 }
85190 pC->cacheStatus = p->cacheCtr;
85191 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
85192 pC->nHdrParsed = 0;
85193
85194
85195 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
85196 /* pC->aRow does not have to hold the entire row, but it does at least
85197 ** need to cover the header of the record. If pC->aRow does not contain
85198 ** the complete header, then set it to zero, forcing the header to be
85199 ** dynamically allocated. */
85200 pC->aRow = 0;
85201 pC->szRow = 0;
85202
85203 /* Make sure a corrupt database has not given us an oversize header.
85204 ** Do this now to avoid an oversize memory allocation.
85205 **
85206 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
85207 ** types use so much data space that there can only be 4096 and 32 of
85208 ** them, respectively. So the maximum header length results from a
85209 ** 3-byte type for each of the maximum of 32768 columns plus three
85210 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
85211 */
85212 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
85213 goto op_column_corrupt;
85214 }
85215 }else{
85216 /* This is an optimization. By skipping over the first few tests
85217 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
85218 ** measurable performance gain.
85219 **
85220 ** This branch is taken even if aOffset[0]==0. Such a record is never
85221 ** generated by SQLite, and could be considered corruption, but we
85222 ** accept it for historical reasons. When aOffset[0]==0, the code this
85223 ** branch jumps to reads past the end of the record, but never more
85224 ** than a few bytes. Even if the record occurs at the end of the page
85225 ** content area, the "page header" comes after the page content and so
85226 ** this overread is harmless. Similar overreads can occur for a corrupt
85227 ** database file.
85228 */
85229 zData = pC->aRow;
85230 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
85231 testcase( aOffset[0]==0 );
85232 goto op_column_read_header;
85233 }
85234 }
85235
85236 /* Make sure at least the first p2+1 entries of the header have been
85237 ** parsed and valid information is in aOffset[] and pC->aType[].
85238 */
85239 if( pC->nHdrParsed<=p2 ){
85240 /* If there is more header available for parsing in the record, try
85241 ** to extract additional fields up through the p2+1-th field
85242 */
85243 if( pC->iHdrOffset<aOffset[0] ){
85244 /* Make sure zData points to enough of the record to cover the header. */
85245 if( pC->aRow==0 ){
85246 memset(&sMem, 0, sizeof(sMem));
85247 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
85248 if( rc!=SQLITE_OK ) goto abort_due_to_error;
85249 zData = (u8*)sMem.z;
85250 }else{
85251 zData = pC->aRow;
85252 }
85253
85254 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
85255 op_column_read_header:
85256 i = pC->nHdrParsed;
85257 offset64 = aOffset[i];
85258 zHdr = zData + pC->iHdrOffset;
85259 zEndHdr = zData + aOffset[0];
85260 testcase( zHdr>=zEndHdr );
85261 do{
85262 if( (t = zHdr[0])<0x80 ){
85263 zHdr++;
85264 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
85265 }else{
85266 zHdr += sqlite3GetVarint32(zHdr, &t);
85267 offset64 += sqlite3VdbeSerialTypeLen(t);
85268 }
85269 pC->aType[i++] = t;
85270 aOffset[i] = (u32)(offset64 & 0xffffffff);
85271 }while( i<=p2 && zHdr<zEndHdr );
85272
85273 /* The record is corrupt if any of the following are true:
85274 ** (1) the bytes of the header extend past the declared header size
85275 ** (2) the entire header was used but not all data was used
85276 ** (3) the end of the data extends beyond the end of the record.
85277 */
85278 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
85279 || (offset64 > pC->payloadSize)
85280 ){
85281 if( aOffset[0]==0 ){
85282 i = 0;
85283 zHdr = zEndHdr;
85284 }else{
85285 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
85286 goto op_column_corrupt;
85287 }
85288 }
85289
85290 pC->nHdrParsed = i;
85291 pC->iHdrOffset = (u32)(zHdr - zData);
85292 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
85293 }else{
85294 t = 0;
85295 }
85296
85297 /* If after trying to extract new entries from the header, nHdrParsed is
85298 ** still not up to p2, that means that the record has fewer than p2
85299 ** columns. So the result will be either the default value or a NULL.
85300 */
85301 if( pC->nHdrParsed<=p2 ){
85302 if( pOp->p4type==P4_MEM ){
85303 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
85304 }else{
85305 sqlite3VdbeMemSetNull(pDest);
85306 }
85307 goto op_column_out;
85308 }
85309 }else{
85310 t = pC->aType[p2];
85311 }
85312
85313 /* Extract the content for the p2+1-th column. Control can only
85314 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
85315 ** all valid.
85316 */
85317 assert( p2<pC->nHdrParsed );
85318 assert( rc==SQLITE_OK );
85319 assert( sqlite3VdbeCheckMemInvariants(pDest) );
85320 if( VdbeMemDynamic(pDest) ){
85321 sqlite3VdbeMemSetNull(pDest);
85322 }
85323 assert( t==pC->aType[p2] );
85324 if( pC->szRow>=aOffset[p2+1] ){
85325 /* This is the common case where the desired content fits on the original
85326 ** page - where the content is not on an overflow page */
85327 zData = pC->aRow + aOffset[p2];
85328 if( t<12 ){
85329 sqlite3VdbeSerialGet(zData, t, pDest);
85330 }else{
85331 /* If the column value is a string, we need a persistent value, not
85332 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
85333 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
85334 */
85335 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
85336 pDest->n = len = (t-12)/2;
85337 pDest->enc = encoding;
85338 if( pDest->szMalloc < len+2 ){
85339 pDest->flags = MEM_Null;
85340 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
85341 }else{
85342 pDest->z = pDest->zMalloc;
85343 }
85344 memcpy(pDest->z, zData, len);
85345 pDest->z[len] = 0;
85346 pDest->z[len+1] = 0;
85347 pDest->flags = aFlag[t&1];
85348 }
85349 }else{
85350 pDest->enc = encoding;
85351 /* This branch happens only when content is on overflow pages */
85352 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
85353 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
85354 || (len = sqlite3VdbeSerialTypeLen(t))==0
85355 ){
85356 /* Content is irrelevant for
85357 ** 1. the typeof() function,
85358 ** 2. the length(X) function if X is a blob, and
85359 ** 3. if the content length is zero.
85360 ** So we might as well use bogus content rather than reading
85361 ** content from disk.
85362 **
85363 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
85364 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
85365 ** read up to 16. So 16 bytes of bogus content is supplied.
85366 */
85367 static u8 aZero[16]; /* This is the bogus content */
85368 sqlite3VdbeSerialGet(aZero, t, pDest);
85369 }else{
85370 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
85371 if( rc!=SQLITE_OK ) goto abort_due_to_error;
85372 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
85373 pDest->flags &= ~MEM_Ephem;
85374 }
85375 }
85376
85377op_column_out:
85378 UPDATE_MAX_BLOBSIZE(pDest);
85379 REGISTER_TRACE(pOp->p3, pDest);
85380 break;
85381
85382op_column_corrupt:
85383 if( aOp[0].p3>0 ){
85384 pOp = &aOp[aOp[0].p3-1];
85385 break;
85386 }else{
85387 rc = SQLITE_CORRUPT_BKPT;
85388 goto abort_due_to_error;
85389 }
85390}
85391
85392/* Opcode: Affinity P1 P2 * P4 *
85393** Synopsis: affinity(r[P1@P2])
85394**
85395** Apply affinities to a range of P2 registers starting with P1.
85396**
85397** P4 is a string that is P2 characters long. The N-th character of the
85398** string indicates the column affinity that should be used for the N-th
85399** memory cell in the range.
85400*/
85401case OP_Affinity: {
85402 const char *zAffinity; /* The affinity to be applied */
85403
85404 zAffinity = pOp->p4.z;
85405 assert( zAffinity!=0 );
85406 assert( pOp->p2>0 );
85407 assert( zAffinity[pOp->p2]==0 );
85408 pIn1 = &aMem[pOp->p1];
85409 do{
85410 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
85411 assert( memIsValid(pIn1) );
85412 applyAffinity(pIn1, *(zAffinity++), encoding);
85413 pIn1++;
85414 }while( zAffinity[0] );
85415 break;
85416}
85417
85418/* Opcode: MakeRecord P1 P2 P3 P4 *
85419** Synopsis: r[P3]=mkrec(r[P1@P2])
85420**
85421** Convert P2 registers beginning with P1 into the [record format]
85422** use as a data record in a database table or as a key
85423** in an index. The OP_Column opcode can decode the record later.
85424**
85425** P4 may be a string that is P2 characters long. The N-th character of the
85426** string indicates the column affinity that should be used for the N-th
85427** field of the index key.
85428**
85429** The mapping from character to affinity is given by the SQLITE_AFF_
85430** macros defined in sqliteInt.h.
85431**
85432** If P4 is NULL then all index fields have the affinity BLOB.
85433*/
85434case OP_MakeRecord: {
85435 u8 *zNewRecord; /* A buffer to hold the data for the new record */
85436 Mem *pRec; /* The new record */
85437 u64 nData; /* Number of bytes of data space */
85438 int nHdr; /* Number of bytes of header space */
85439 i64 nByte; /* Data space required for this record */
85440 i64 nZero; /* Number of zero bytes at the end of the record */
85441 int nVarint; /* Number of bytes in a varint */
85442 u32 serial_type; /* Type field */
85443 Mem *pData0; /* First field to be combined into the record */
85444 Mem *pLast; /* Last field of the record */
85445 int nField; /* Number of fields in the record */
85446 char *zAffinity; /* The affinity string for the record */
85447 int file_format; /* File format to use for encoding */
85448 int i; /* Space used in zNewRecord[] header */
85449 int j; /* Space used in zNewRecord[] content */
85450 u32 len; /* Length of a field */
85451
85452 /* Assuming the record contains N fields, the record format looks
85453 ** like this:
85454 **
85455 ** ------------------------------------------------------------------------
85456 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
85457 ** ------------------------------------------------------------------------
85458 **
85459 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
85460 ** and so forth.
85461 **
85462 ** Each type field is a varint representing the serial type of the
85463 ** corresponding data element (see sqlite3VdbeSerialType()). The
85464 ** hdr-size field is also a varint which is the offset from the beginning
85465 ** of the record to data0.
85466 */
85467 nData = 0; /* Number of bytes of data space */
85468 nHdr = 0; /* Number of bytes of header space */
85469 nZero = 0; /* Number of zero bytes at the end of the record */
85470 nField = pOp->p1;
85471 zAffinity = pOp->p4.z;
85472 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
85473 pData0 = &aMem[nField];
85474 nField = pOp->p2;
85475 pLast = &pData0[nField-1];
85476 file_format = p->minWriteFileFormat;
85477
85478 /* Identify the output register */
85479 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
85480 pOut = &aMem[pOp->p3];
85481 memAboutToChange(p, pOut);
85482
85483 /* Apply the requested affinity to all inputs
85484 */
85485 assert( pData0<=pLast );
85486 if( zAffinity ){
85487 pRec = pData0;
85488 do{
85489 applyAffinity(pRec++, *(zAffinity++), encoding);
85490 assert( zAffinity[0]==0 || pRec<=pLast );
85491 }while( zAffinity[0] );
85492 }
85493
85494#ifdef SQLITE_ENABLE_NULL_TRIM
85495 /* NULLs can be safely trimmed from the end of the record, as long as
85496 ** as the schema format is 2 or more and none of the omitted columns
85497 ** have a non-NULL default value. Also, the record must be left with
85498 ** at least one field. If P5>0 then it will be one more than the
85499 ** index of the right-most column with a non-NULL default value */
85500 if( pOp->p5 ){
85501 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
85502 pLast--;
85503 nField--;
85504 }
85505 }
85506#endif
85507
85508 /* Loop through the elements that will make up the record to figure
85509 ** out how much space is required for the new record.
85510 */
85511 pRec = pLast;
85512 do{
85513 assert( memIsValid(pRec) );
85514 serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
85515 if( pRec->flags & MEM_Zero ){
85516 if( serial_type==0 ){
85517 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
85518 ** table methods that never invoke sqlite3_result_xxxxx() while
85519 ** computing an unchanging column value in an UPDATE statement.
85520 ** Give such values a special internal-use-only serial-type of 10
85521 ** so that they can be passed through to xUpdate and have
85522 ** a true sqlite3_value_nochange(). */
85523 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
85524 serial_type = 10;
85525 }else if( nData ){
85526 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
85527 }else{
85528 nZero += pRec->u.nZero;
85529 len -= pRec->u.nZero;
85530 }
85531 }
85532 nData += len;
85533 testcase( serial_type==127 );
85534 testcase( serial_type==128 );
85535 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
85536 pRec->uTemp = serial_type;
85537 if( pRec==pData0 ) break;
85538 pRec--;
85539 }while(1);
85540
85541 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
85542 ** which determines the total number of bytes in the header. The varint
85543 ** value is the size of the header in bytes including the size varint
85544 ** itself. */
85545 testcase( nHdr==126 );
85546 testcase( nHdr==127 );
85547 if( nHdr<=126 ){
85548 /* The common case */
85549 nHdr += 1;
85550 }else{
85551 /* Rare case of a really large header */
85552 nVarint = sqlite3VarintLen(nHdr);
85553 nHdr += nVarint;
85554 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
85555 }
85556 nByte = nHdr+nData;
85557 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
85558 goto too_big;
85559 }
85560
85561 /* Make sure the output register has a buffer large enough to store
85562 ** the new record. The output register (pOp->p3) is not allowed to
85563 ** be one of the input registers (because the following call to
85564 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
85565 */
85566 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
85567 goto no_mem;
85568 }
85569 zNewRecord = (u8 *)pOut->z;
85570
85571 /* Write the record */
85572 i = putVarint32(zNewRecord, nHdr);
85573 j = nHdr;
85574 assert( pData0<=pLast );
85575 pRec = pData0;
85576 do{
85577 serial_type = pRec->uTemp;
85578 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
85579 ** additional varints, one per column. */
85580 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
85581 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
85582 ** immediately follow the header. */
85583 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
85584 }while( (++pRec)<=pLast );
85585 assert( i==nHdr );
85586 assert( j==nByte );
85587
85588 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
85589 pOut->n = (int)nByte;
85590 pOut->flags = MEM_Blob;
85591 if( nZero ){
85592 pOut->u.nZero = nZero;
85593 pOut->flags |= MEM_Zero;
85594 }
85595 REGISTER_TRACE(pOp->p3, pOut);
85596 UPDATE_MAX_BLOBSIZE(pOut);
85597 break;
85598}
85599
85600/* Opcode: Count P1 P2 * * *
85601** Synopsis: r[P2]=count()
85602**
85603** Store the number of entries (an integer value) in the table or index
85604** opened by cursor P1 in register P2
85605*/
85606#ifndef SQLITE_OMIT_BTREECOUNT
85607case OP_Count: { /* out2 */
85608 i64 nEntry;
85609 BtCursor *pCrsr;
85610
85611 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
85612 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
85613 assert( pCrsr );
85614 nEntry = 0; /* Not needed. Only used to silence a warning. */
85615 rc = sqlite3BtreeCount(pCrsr, &nEntry);
85616 if( rc ) goto abort_due_to_error;
85617 pOut = out2Prerelease(p, pOp);
85618 pOut->u.i = nEntry;
85619 break;
85620}
85621#endif
85622
85623/* Opcode: Savepoint P1 * * P4 *
85624**
85625** Open, release or rollback the savepoint named by parameter P4, depending
85626** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
85627** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
85628*/
85629case OP_Savepoint: {
85630 int p1; /* Value of P1 operand */
85631 char *zName; /* Name of savepoint */
85632 int nName;
85633 Savepoint *pNew;
85634 Savepoint *pSavepoint;
85635 Savepoint *pTmp;
85636 int iSavepoint;
85637 int ii;
85638
85639 p1 = pOp->p1;
85640 zName = pOp->p4.z;
85641
85642 /* Assert that the p1 parameter is valid. Also that if there is no open
85643 ** transaction, then there cannot be any savepoints.
85644 */
85645 assert( db->pSavepoint==0 || db->autoCommit==0 );
85646 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
85647 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
85648 assert( checkSavepointCount(db) );
85649 assert( p->bIsReader );
85650
85651 if( p1==SAVEPOINT_BEGIN ){
85652 if( db->nVdbeWrite>0 ){
85653 /* A new savepoint cannot be created if there are active write
85654 ** statements (i.e. open read/write incremental blob handles).
85655 */
85656 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
85657 rc = SQLITE_BUSY;
85658 }else{
85659 nName = sqlite3Strlen30(zName);
85660
85661#ifndef SQLITE_OMIT_VIRTUALTABLE
85662 /* This call is Ok even if this savepoint is actually a transaction
85663 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
85664 ** If this is a transaction savepoint being opened, it is guaranteed
85665 ** that the db->aVTrans[] array is empty. */
85666 assert( db->autoCommit==0 || db->nVTrans==0 );
85667 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
85668 db->nStatement+db->nSavepoint);
85669 if( rc!=SQLITE_OK ) goto abort_due_to_error;
85670#endif
85671
85672 /* Create a new savepoint structure. */
85673 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
85674 if( pNew ){
85675 pNew->zName = (char *)&pNew[1];
85676 memcpy(pNew->zName, zName, nName+1);
85677
85678 /* If there is no open transaction, then mark this as a special
85679 ** "transaction savepoint". */
85680 if( db->autoCommit ){
85681 db->autoCommit = 0;
85682 db->isTransactionSavepoint = 1;
85683 }else{
85684 db->nSavepoint++;
85685 }
85686
85687 /* Link the new savepoint into the database handle's list. */
85688 pNew->pNext = db->pSavepoint;
85689 db->pSavepoint = pNew;
85690 pNew->nDeferredCons = db->nDeferredCons;
85691 pNew->nDeferredImmCons = db->nDeferredImmCons;
85692 }
85693 }
85694 }else{
85695 iSavepoint = 0;
85696
85697 /* Find the named savepoint. If there is no such savepoint, then an
85698 ** an error is returned to the user. */
85699 for(
85700 pSavepoint = db->pSavepoint;
85701 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
85702 pSavepoint = pSavepoint->pNext
85703 ){
85704 iSavepoint++;
85705 }
85706 if( !pSavepoint ){
85707 sqlite3VdbeError(p, "no such savepoint: %s", zName);
85708 rc = SQLITE_ERROR;
85709 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
85710 /* It is not possible to release (commit) a savepoint if there are
85711 ** active write statements.
85712 */
85713 sqlite3VdbeError(p, "cannot release savepoint - "
85714 "SQL statements in progress");
85715 rc = SQLITE_BUSY;
85716 }else{
85717
85718 /* Determine whether or not this is a transaction savepoint. If so,
85719 ** and this is a RELEASE command, then the current transaction
85720 ** is committed.
85721 */
85722 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
85723 if( isTransaction && p1==SAVEPOINT_RELEASE ){
85724 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
85725 goto vdbe_return;
85726 }
85727 db->autoCommit = 1;
85728 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
85729 p->pc = (int)(pOp - aOp);
85730 db->autoCommit = 0;
85731 p->rc = rc = SQLITE_BUSY;
85732 goto vdbe_return;
85733 }
85734 db->isTransactionSavepoint = 0;
85735 rc = p->rc;
85736 }else{
85737 int isSchemaChange;
85738 iSavepoint = db->nSavepoint - iSavepoint - 1;
85739 if( p1==SAVEPOINT_ROLLBACK ){
85740 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
85741 for(ii=0; ii<db->nDb; ii++){
85742 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
85743 SQLITE_ABORT_ROLLBACK,
85744 isSchemaChange==0);
85745 if( rc!=SQLITE_OK ) goto abort_due_to_error;
85746 }
85747 }else{
85748 isSchemaChange = 0;
85749 }
85750 for(ii=0; ii<db->nDb; ii++){
85751 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
85752 if( rc!=SQLITE_OK ){
85753 goto abort_due_to_error;
85754 }
85755 }
85756 if( isSchemaChange ){
85757 sqlite3ExpirePreparedStatements(db, 0);
85758 sqlite3ResetAllSchemasOfConnection(db);
85759 db->mDbFlags |= DBFLAG_SchemaChange;
85760 }
85761 }
85762
85763 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
85764 ** savepoints nested inside of the savepoint being operated on. */
85765 while( db->pSavepoint!=pSavepoint ){
85766 pTmp = db->pSavepoint;
85767 db->pSavepoint = pTmp->pNext;
85768 sqlite3DbFree(db, pTmp);
85769 db->nSavepoint--;
85770 }
85771
85772 /* If it is a RELEASE, then destroy the savepoint being operated on
85773 ** too. If it is a ROLLBACK TO, then set the number of deferred
85774 ** constraint violations present in the database to the value stored
85775 ** when the savepoint was created. */
85776 if( p1==SAVEPOINT_RELEASE ){
85777 assert( pSavepoint==db->pSavepoint );
85778 db->pSavepoint = pSavepoint->pNext;
85779 sqlite3DbFree(db, pSavepoint);
85780 if( !isTransaction ){
85781 db->nSavepoint--;
85782 }
85783 }else{
85784 db->nDeferredCons = pSavepoint->nDeferredCons;
85785 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
85786 }
85787
85788 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
85789 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
85790 if( rc!=SQLITE_OK ) goto abort_due_to_error;
85791 }
85792 }
85793 }
85794 if( rc ) goto abort_due_to_error;
85795
85796 break;
85797}
85798
85799/* Opcode: AutoCommit P1 P2 * * *
85800**
85801** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
85802** back any currently active btree transactions. If there are any active
85803** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
85804** there are active writing VMs or active VMs that use shared cache.
85805**
85806** This instruction causes the VM to halt.
85807*/
85808case OP_AutoCommit: {
85809 int desiredAutoCommit;
85810 int iRollback;
85811
85812 desiredAutoCommit = pOp->p1;
85813 iRollback = pOp->p2;
85814 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
85815 assert( desiredAutoCommit==1 || iRollback==0 );
85816 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
85817 assert( p->bIsReader );
85818
85819 if( desiredAutoCommit!=db->autoCommit ){
85820 if( iRollback ){
85821 assert( desiredAutoCommit==1 );
85822 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
85823 db->autoCommit = 1;
85824 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
85825 /* If this instruction implements a COMMIT and other VMs are writing
85826 ** return an error indicating that the other VMs must complete first.
85827 */
85828 sqlite3VdbeError(p, "cannot commit transaction - "
85829 "SQL statements in progress");
85830 rc = SQLITE_BUSY;
85831 goto abort_due_to_error;
85832 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
85833 goto vdbe_return;
85834 }else{
85835 db->autoCommit = (u8)desiredAutoCommit;
85836 }
85837 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
85838 p->pc = (int)(pOp - aOp);
85839 db->autoCommit = (u8)(1-desiredAutoCommit);
85840 p->rc = rc = SQLITE_BUSY;
85841 goto vdbe_return;
85842 }
85843 assert( db->nStatement==0 );
85844 sqlite3CloseSavepoints(db);
85845 if( p->rc==SQLITE_OK ){
85846 rc = SQLITE_DONE;
85847 }else{
85848 rc = SQLITE_ERROR;
85849 }
85850 goto vdbe_return;
85851 }else{
85852 sqlite3VdbeError(p,
85853 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
85854 (iRollback)?"cannot rollback - no transaction is active":
85855 "cannot commit - no transaction is active"));
85856
85857 rc = SQLITE_ERROR;
85858 goto abort_due_to_error;
85859 }
85860 break;
85861}
85862
85863/* Opcode: Transaction P1 P2 P3 P4 P5
85864**
85865** Begin a transaction on database P1 if a transaction is not already
85866** active.
85867** If P2 is non-zero, then a write-transaction is started, or if a
85868** read-transaction is already active, it is upgraded to a write-transaction.
85869** If P2 is zero, then a read-transaction is started.
85870**
85871** P1 is the index of the database file on which the transaction is
85872** started. Index 0 is the main database file and index 1 is the
85873** file used for temporary tables. Indices of 2 or more are used for
85874** attached databases.
85875**
85876** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
85877** true (this flag is set if the Vdbe may modify more than one row and may
85878** throw an ABORT exception), a statement transaction may also be opened.
85879** More specifically, a statement transaction is opened iff the database
85880** connection is currently not in autocommit mode, or if there are other
85881** active statements. A statement transaction allows the changes made by this
85882** VDBE to be rolled back after an error without having to roll back the
85883** entire transaction. If no error is encountered, the statement transaction
85884** will automatically commit when the VDBE halts.
85885**
85886** If P5!=0 then this opcode also checks the schema cookie against P3
85887** and the schema generation counter against P4.
85888** The cookie changes its value whenever the database schema changes.
85889** This operation is used to detect when that the cookie has changed
85890** and that the current process needs to reread the schema. If the schema
85891** cookie in P3 differs from the schema cookie in the database header or
85892** if the schema generation counter in P4 differs from the current
85893** generation counter, then an SQLITE_SCHEMA error is raised and execution
85894** halts. The sqlite3_step() wrapper function might then reprepare the
85895** statement and rerun it from the beginning.
85896*/
85897case OP_Transaction: {
85898 Btree *pBt;
85899 int iMeta = 0;
85900
85901 assert( p->bIsReader );
85902 assert( p->readOnly==0 || pOp->p2==0 );
85903 assert( pOp->p1>=0 && pOp->p1<db->nDb );
85904 assert( DbMaskTest(p->btreeMask, pOp->p1) );
85905 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
85906 rc = SQLITE_READONLY;
85907 goto abort_due_to_error;
85908 }
85909 pBt = db->aDb[pOp->p1].pBt;
85910
85911 if( pBt ){
85912 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
85913 testcase( rc==SQLITE_BUSY_SNAPSHOT );
85914 testcase( rc==SQLITE_BUSY_RECOVERY );
85915 if( rc!=SQLITE_OK ){
85916 if( (rc&0xff)==SQLITE_BUSY ){
85917 p->pc = (int)(pOp - aOp);
85918 p->rc = rc;
85919 goto vdbe_return;
85920 }
85921 goto abort_due_to_error;
85922 }
85923
85924 if( pOp->p2 && p->usesStmtJournal
85925 && (db->autoCommit==0 || db->nVdbeRead>1)
85926 ){
85927 assert( sqlite3BtreeIsInTrans(pBt) );
85928 if( p->iStatement==0 ){
85929 assert( db->nStatement>=0 && db->nSavepoint>=0 );
85930 db->nStatement++;
85931 p->iStatement = db->nSavepoint + db->nStatement;
85932 }
85933
85934 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
85935 if( rc==SQLITE_OK ){
85936 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
85937 }
85938
85939 /* Store the current value of the database handles deferred constraint
85940 ** counter. If the statement transaction needs to be rolled back,
85941 ** the value of this counter needs to be restored too. */
85942 p->nStmtDefCons = db->nDeferredCons;
85943 p->nStmtDefImmCons = db->nDeferredImmCons;
85944 }
85945 }
85946 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
85947 if( pOp->p5
85948 && (iMeta!=pOp->p3
85949 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
85950 ){
85951 /*
85952 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
85953 ** version is checked to ensure that the schema has not changed since the
85954 ** SQL statement was prepared.
85955 */
85956 sqlite3DbFree(db, p->zErrMsg);
85957 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
85958 /* If the schema-cookie from the database file matches the cookie
85959 ** stored with the in-memory representation of the schema, do
85960 ** not reload the schema from the database file.
85961 **
85962 ** If virtual-tables are in use, this is not just an optimization.
85963 ** Often, v-tables store their data in other SQLite tables, which
85964 ** are queried from within xNext() and other v-table methods using
85965 ** prepared queries. If such a query is out-of-date, we do not want to
85966 ** discard the database schema, as the user code implementing the
85967 ** v-table would have to be ready for the sqlite3_vtab structure itself
85968 ** to be invalidated whenever sqlite3_step() is called from within
85969 ** a v-table method.
85970 */
85971 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
85972 sqlite3ResetOneSchema(db, pOp->p1);
85973 }
85974 p->expired = 1;
85975 rc = SQLITE_SCHEMA;
85976 }
85977 if( rc ) goto abort_due_to_error;
85978 break;
85979}
85980
85981/* Opcode: ReadCookie P1 P2 P3 * *
85982**
85983** Read cookie number P3 from database P1 and write it into register P2.
85984** P3==1 is the schema version. P3==2 is the database format.
85985** P3==3 is the recommended pager cache size, and so forth. P1==0 is
85986** the main database file and P1==1 is the database file used to store
85987** temporary tables.
85988**
85989** There must be a read-lock on the database (either a transaction
85990** must be started or there must be an open cursor) before
85991** executing this instruction.
85992*/
85993case OP_ReadCookie: { /* out2 */
85994 int iMeta;
85995 int iDb;
85996 int iCookie;
85997
85998 assert( p->bIsReader );
85999 iDb = pOp->p1;
86000 iCookie = pOp->p3;
86001 assert( pOp->p3<SQLITE_N_BTREE_META );
86002 assert( iDb>=0 && iDb<db->nDb );
86003 assert( db->aDb[iDb].pBt!=0 );
86004 assert( DbMaskTest(p->btreeMask, iDb) );
86005
86006 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
86007 pOut = out2Prerelease(p, pOp);
86008 pOut->u.i = iMeta;
86009 break;
86010}
86011
86012/* Opcode: SetCookie P1 P2 P3 * *
86013**
86014** Write the integer value P3 into cookie number P2 of database P1.
86015** P2==1 is the schema version. P2==2 is the database format.
86016** P2==3 is the recommended pager cache
86017** size, and so forth. P1==0 is the main database file and P1==1 is the
86018** database file used to store temporary tables.
86019**
86020** A transaction must be started before executing this opcode.
86021*/
86022case OP_SetCookie: {
86023 Db *pDb;
86024
86025 sqlite3VdbeIncrWriteCounter(p, 0);
86026 assert( pOp->p2<SQLITE_N_BTREE_META );
86027 assert( pOp->p1>=0 && pOp->p1<db->nDb );
86028 assert( DbMaskTest(p->btreeMask, pOp->p1) );
86029 assert( p->readOnly==0 );
86030 pDb = &db->aDb[pOp->p1];
86031 assert( pDb->pBt!=0 );
86032 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
86033 /* See note about index shifting on OP_ReadCookie */
86034 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
86035 if( pOp->p2==BTREE_SCHEMA_VERSION ){
86036 /* When the schema cookie changes, record the new cookie internally */
86037 pDb->pSchema->schema_cookie = pOp->p3;
86038 db->mDbFlags |= DBFLAG_SchemaChange;
86039 }else if( pOp->p2==BTREE_FILE_FORMAT ){
86040 /* Record changes in the file format */
86041 pDb->pSchema->file_format = pOp->p3;
86042 }
86043 if( pOp->p1==1 ){
86044 /* Invalidate all prepared statements whenever the TEMP database
86045 ** schema is changed. Ticket #1644 */
86046 sqlite3ExpirePreparedStatements(db, 0);
86047 p->expired = 0;
86048 }
86049 if( rc ) goto abort_due_to_error;
86050 break;
86051}
86052
86053/* Opcode: OpenRead P1 P2 P3 P4 P5
86054** Synopsis: root=P2 iDb=P3
86055**
86056** Open a read-only cursor for the database table whose root page is
86057** P2 in a database file. The database file is determined by P3.
86058** P3==0 means the main database, P3==1 means the database used for
86059** temporary tables, and P3>1 means used the corresponding attached
86060** database. Give the new cursor an identifier of P1. The P1
86061** values need not be contiguous but all P1 values should be small integers.
86062** It is an error for P1 to be negative.
86063**
86064** Allowed P5 bits:
86065** <ul>
86066** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
86067** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
86068** of OP_SeekLE/OP_IdxGT)
86069** </ul>
86070**
86071** The P4 value may be either an integer (P4_INT32) or a pointer to
86072** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
86073** object, then table being opened must be an [index b-tree] where the
86074** KeyInfo object defines the content and collating
86075** sequence of that index b-tree. Otherwise, if P4 is an integer
86076** value, then the table being opened must be a [table b-tree] with a
86077** number of columns no less than the value of P4.
86078**
86079** See also: OpenWrite, ReopenIdx
86080*/
86081/* Opcode: ReopenIdx P1 P2 P3 P4 P5
86082** Synopsis: root=P2 iDb=P3
86083**
86084** The ReopenIdx opcode works like OP_OpenRead except that it first
86085** checks to see if the cursor on P1 is already open on the same
86086** b-tree and if it is this opcode becomes a no-op. In other words,
86087** if the cursor is already open, do not reopen it.
86088**
86089** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
86090** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
86091** be the same as every other ReopenIdx or OpenRead for the same cursor
86092** number.
86093**
86094** Allowed P5 bits:
86095** <ul>
86096** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
86097** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
86098** of OP_SeekLE/OP_IdxGT)
86099** </ul>
86100**
86101** See also: OP_OpenRead, OP_OpenWrite
86102*/
86103/* Opcode: OpenWrite P1 P2 P3 P4 P5
86104** Synopsis: root=P2 iDb=P3
86105**
86106** Open a read/write cursor named P1 on the table or index whose root
86107** page is P2 (or whose root page is held in register P2 if the
86108** OPFLAG_P2ISREG bit is set in P5 - see below).
86109**
86110** The P4 value may be either an integer (P4_INT32) or a pointer to
86111** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
86112** object, then table being opened must be an [index b-tree] where the
86113** KeyInfo object defines the content and collating
86114** sequence of that index b-tree. Otherwise, if P4 is an integer
86115** value, then the table being opened must be a [table b-tree] with a
86116** number of columns no less than the value of P4.
86117**
86118** Allowed P5 bits:
86119** <ul>
86120** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
86121** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
86122** of OP_SeekLE/OP_IdxGT)
86123** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
86124** and subsequently delete entries in an index btree. This is a
86125** hint to the storage engine that the storage engine is allowed to
86126** ignore. The hint is not used by the official SQLite b*tree storage
86127** engine, but is used by COMDB2.
86128** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
86129** as the root page, not the value of P2 itself.
86130** </ul>
86131**
86132** This instruction works like OpenRead except that it opens the cursor
86133** in read/write mode.
86134**
86135** See also: OP_OpenRead, OP_ReopenIdx
86136*/
86137case OP_ReopenIdx: {
86138 int nField;
86139 KeyInfo *pKeyInfo;
86140 int p2;
86141 int iDb;
86142 int wrFlag;
86143 Btree *pX;
86144 VdbeCursor *pCur;
86145 Db *pDb;
86146
86147 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
86148 assert( pOp->p4type==P4_KEYINFO );
86149 pCur = p->apCsr[pOp->p1];
86150 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
86151 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
86152 goto open_cursor_set_hints;
86153 }
86154 /* If the cursor is not currently open or is open on a different
86155 ** index, then fall through into OP_OpenRead to force a reopen */
86156case OP_OpenRead:
86157case OP_OpenWrite:
86158
86159 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
86160 assert( p->bIsReader );
86161 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
86162 || p->readOnly==0 );
86163
86164 if( p->expired==1 ){
86165 rc = SQLITE_ABORT_ROLLBACK;
86166 goto abort_due_to_error;
86167 }
86168
86169 nField = 0;
86170 pKeyInfo = 0;
86171 p2 = pOp->p2;
86172 iDb = pOp->p3;
86173 assert( iDb>=0 && iDb<db->nDb );
86174 assert( DbMaskTest(p->btreeMask, iDb) );
86175 pDb = &db->aDb[iDb];
86176 pX = pDb->pBt;
86177 assert( pX!=0 );
86178 if( pOp->opcode==OP_OpenWrite ){
86179 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
86180 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
86181 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
86182 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
86183 p->minWriteFileFormat = pDb->pSchema->file_format;
86184 }
86185 }else{
86186 wrFlag = 0;
86187 }
86188 if( pOp->p5 & OPFLAG_P2ISREG ){
86189 assert( p2>0 );
86190 assert( p2<=(p->nMem+1 - p->nCursor) );
86191 assert( pOp->opcode==OP_OpenWrite );
86192 pIn2 = &aMem[p2];
86193 assert( memIsValid(pIn2) );
86194 assert( (pIn2->flags & MEM_Int)!=0 );
86195 sqlite3VdbeMemIntegerify(pIn2);
86196 p2 = (int)pIn2->u.i;
86197 /* The p2 value always comes from a prior OP_CreateBtree opcode and
86198 ** that opcode will always set the p2 value to 2 or more or else fail.
86199 ** If there were a failure, the prepared statement would have halted
86200 ** before reaching this instruction. */
86201 assert( p2>=2 );
86202 }
86203 if( pOp->p4type==P4_KEYINFO ){
86204 pKeyInfo = pOp->p4.pKeyInfo;
86205 assert( pKeyInfo->enc==ENC(db) );
86206 assert( pKeyInfo->db==db );
86207 nField = pKeyInfo->nAllField;
86208 }else if( pOp->p4type==P4_INT32 ){
86209 nField = pOp->p4.i;
86210 }
86211 assert( pOp->p1>=0 );
86212 assert( nField>=0 );
86213 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
86214 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
86215 if( pCur==0 ) goto no_mem;
86216 pCur->nullRow = 1;
86217 pCur->isOrdered = 1;
86218 pCur->pgnoRoot = p2;
86219#ifdef SQLITE_DEBUG
86220 pCur->wrFlag = wrFlag;
86221#endif
86222 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
86223 pCur->pKeyInfo = pKeyInfo;
86224 /* Set the VdbeCursor.isTable variable. Previous versions of
86225 ** SQLite used to check if the root-page flags were sane at this point
86226 ** and report database corruption if they were not, but this check has
86227 ** since moved into the btree layer. */
86228 pCur->isTable = pOp->p4type!=P4_KEYINFO;
86229
86230open_cursor_set_hints:
86231 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
86232 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
86233 testcase( pOp->p5 & OPFLAG_BULKCSR );
86234#ifdef SQLITE_ENABLE_CURSOR_HINTS
86235 testcase( pOp->p2 & OPFLAG_SEEKEQ );
86236#endif
86237 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
86238 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
86239 if( rc ) goto abort_due_to_error;
86240 break;
86241}
86242
86243/* Opcode: OpenDup P1 P2 * * *
86244**
86245** Open a new cursor P1 that points to the same ephemeral table as
86246** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
86247** opcode. Only ephemeral cursors may be duplicated.
86248**
86249** Duplicate ephemeral cursors are used for self-joins of materialized views.
86250*/
86251case OP_OpenDup: {
86252 VdbeCursor *pOrig; /* The original cursor to be duplicated */
86253 VdbeCursor *pCx; /* The new cursor */
86254
86255 pOrig = p->apCsr[pOp->p2];
86256 assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */
86257
86258 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
86259 if( pCx==0 ) goto no_mem;
86260 pCx->nullRow = 1;
86261 pCx->isEphemeral = 1;
86262 pCx->pKeyInfo = pOrig->pKeyInfo;
86263 pCx->isTable = pOrig->isTable;
86264 rc = sqlite3BtreeCursor(pOrig->pBtx, MASTER_ROOT, BTREE_WRCSR,
86265 pCx->pKeyInfo, pCx->uc.pCursor);
86266 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
86267 ** opened for a database. Since there is already an open cursor when this
86268 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
86269 assert( rc==SQLITE_OK );
86270 break;
86271}
86272
86273
86274/* Opcode: OpenEphemeral P1 P2 * P4 P5
86275** Synopsis: nColumn=P2
86276**
86277** Open a new cursor P1 to a transient table.
86278** The cursor is always opened read/write even if
86279** the main database is read-only. The ephemeral
86280** table is deleted automatically when the cursor is closed.
86281**
86282** P2 is the number of columns in the ephemeral table.
86283** The cursor points to a BTree table if P4==0 and to a BTree index
86284** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
86285** that defines the format of keys in the index.
86286**
86287** The P5 parameter can be a mask of the BTREE_* flags defined
86288** in btree.h. These flags control aspects of the operation of
86289** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
86290** added automatically.
86291*/
86292/* Opcode: OpenAutoindex P1 P2 * P4 *
86293** Synopsis: nColumn=P2
86294**
86295** This opcode works the same as OP_OpenEphemeral. It has a
86296** different name to distinguish its use. Tables created using
86297** by this opcode will be used for automatically created transient
86298** indices in joins.
86299*/
86300case OP_OpenAutoindex:
86301case OP_OpenEphemeral: {
86302 VdbeCursor *pCx;
86303 KeyInfo *pKeyInfo;
86304
86305 static const int vfsFlags =
86306 SQLITE_OPEN_READWRITE |
86307 SQLITE_OPEN_CREATE |
86308 SQLITE_OPEN_EXCLUSIVE |
86309 SQLITE_OPEN_DELETEONCLOSE |
86310 SQLITE_OPEN_TRANSIENT_DB;
86311 assert( pOp->p1>=0 );
86312 assert( pOp->p2>=0 );
86313 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
86314 if( pCx==0 ) goto no_mem;
86315 pCx->nullRow = 1;
86316 pCx->isEphemeral = 1;
86317 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
86318 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
86319 if( rc==SQLITE_OK ){
86320 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
86321 }
86322 if( rc==SQLITE_OK ){
86323 /* If a transient index is required, create it by calling
86324 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
86325 ** opening it. If a transient table is required, just use the
86326 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
86327 */
86328 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
86329 int pgno;
86330 assert( pOp->p4type==P4_KEYINFO );
86331 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5);
86332 if( rc==SQLITE_OK ){
86333 assert( pgno==MASTER_ROOT+1 );
86334 assert( pKeyInfo->db==db );
86335 assert( pKeyInfo->enc==ENC(db) );
86336 rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR,
86337 pKeyInfo, pCx->uc.pCursor);
86338 }
86339 pCx->isTable = 0;
86340 }else{
86341 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
86342 0, pCx->uc.pCursor);
86343 pCx->isTable = 1;
86344 }
86345 }
86346 if( rc ) goto abort_due_to_error;
86347 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
86348 break;
86349}
86350
86351/* Opcode: SorterOpen P1 P2 P3 P4 *
86352**
86353** This opcode works like OP_OpenEphemeral except that it opens
86354** a transient index that is specifically designed to sort large
86355** tables using an external merge-sort algorithm.
86356**
86357** If argument P3 is non-zero, then it indicates that the sorter may
86358** assume that a stable sort considering the first P3 fields of each
86359** key is sufficient to produce the required results.
86360*/
86361case OP_SorterOpen: {
86362 VdbeCursor *pCx;
86363
86364 assert( pOp->p1>=0 );
86365 assert( pOp->p2>=0 );
86366 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
86367 if( pCx==0 ) goto no_mem;
86368 pCx->pKeyInfo = pOp->p4.pKeyInfo;
86369 assert( pCx->pKeyInfo->db==db );
86370 assert( pCx->pKeyInfo->enc==ENC(db) );
86371 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
86372 if( rc ) goto abort_due_to_error;
86373 break;
86374}
86375
86376/* Opcode: SequenceTest P1 P2 * * *
86377** Synopsis: if( cursor[P1].ctr++ ) pc = P2
86378**
86379** P1 is a sorter cursor. If the sequence counter is currently zero, jump
86380** to P2. Regardless of whether or not the jump is taken, increment the
86381** the sequence value.
86382*/
86383case OP_SequenceTest: {
86384 VdbeCursor *pC;
86385 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
86386 pC = p->apCsr[pOp->p1];
86387 assert( isSorter(pC) );
86388 if( (pC->seqCount++)==0 ){
86389 goto jump_to_p2;
86390 }
86391 break;
86392}
86393
86394/* Opcode: OpenPseudo P1 P2 P3 * *
86395** Synopsis: P3 columns in r[P2]
86396**
86397** Open a new cursor that points to a fake table that contains a single
86398** row of data. The content of that one row is the content of memory
86399** register P2. In other words, cursor P1 becomes an alias for the
86400** MEM_Blob content contained in register P2.
86401**
86402** A pseudo-table created by this opcode is used to hold a single
86403** row output from the sorter so that the row can be decomposed into
86404** individual columns using the OP_Column opcode. The OP_Column opcode
86405** is the only cursor opcode that works with a pseudo-table.
86406**
86407** P3 is the number of fields in the records that will be stored by
86408** the pseudo-table.
86409*/
86410case OP_OpenPseudo: {
86411 VdbeCursor *pCx;
86412
86413 assert( pOp->p1>=0 );
86414 assert( pOp->p3>=0 );
86415 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
86416 if( pCx==0 ) goto no_mem;
86417 pCx->nullRow = 1;
86418 pCx->seekResult = pOp->p2;
86419 pCx->isTable = 1;
86420 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
86421 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
86422 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
86423 ** which is a performance optimization */
86424 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
86425 assert( pOp->p5==0 );
86426 break;
86427}
86428
86429/* Opcode: Close P1 * * * *
86430**
86431** Close a cursor previously opened as P1. If P1 is not
86432** currently open, this instruction is a no-op.
86433*/
86434case OP_Close: {
86435 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
86436 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
86437 p->apCsr[pOp->p1] = 0;
86438 break;
86439}
86440
86441#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
86442/* Opcode: ColumnsUsed P1 * * P4 *
86443**
86444** This opcode (which only exists if SQLite was compiled with
86445** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
86446** table or index for cursor P1 are used. P4 is a 64-bit integer
86447** (P4_INT64) in which the first 63 bits are one for each of the
86448** first 63 columns of the table or index that are actually used
86449** by the cursor. The high-order bit is set if any column after
86450** the 64th is used.
86451*/
86452case OP_ColumnsUsed: {
86453 VdbeCursor *pC;
86454 pC = p->apCsr[pOp->p1];
86455 assert( pC->eCurType==CURTYPE_BTREE );
86456 pC->maskUsed = *(u64*)pOp->p4.pI64;
86457 break;
86458}
86459#endif
86460
86461/* Opcode: SeekGE P1 P2 P3 P4 *
86462** Synopsis: key=r[P3@P4]
86463**
86464** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
86465** use the value in register P3 as the key. If cursor P1 refers
86466** to an SQL index, then P3 is the first in an array of P4 registers
86467** that are used as an unpacked index key.
86468**
86469** Reposition cursor P1 so that it points to the smallest entry that
86470** is greater than or equal to the key value. If there are no records
86471** greater than or equal to the key and P2 is not zero, then jump to P2.
86472**
86473** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
86474** opcode will always land on a record that equally equals the key, or
86475** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
86476** opcode must be followed by an IdxLE opcode with the same arguments.
86477** The IdxLE opcode will be skipped if this opcode succeeds, but the
86478** IdxLE opcode will be used on subsequent loop iterations.
86479**
86480** This opcode leaves the cursor configured to move in forward order,
86481** from the beginning toward the end. In other words, the cursor is
86482** configured to use Next, not Prev.
86483**
86484** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
86485*/
86486/* Opcode: SeekGT P1 P2 P3 P4 *
86487** Synopsis: key=r[P3@P4]
86488**
86489** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
86490** use the value in register P3 as a key. If cursor P1 refers
86491** to an SQL index, then P3 is the first in an array of P4 registers
86492** that are used as an unpacked index key.
86493**
86494** Reposition cursor P1 so that it points to the smallest entry that
86495** is greater than the key value. If there are no records greater than
86496** the key and P2 is not zero, then jump to P2.
86497**
86498** This opcode leaves the cursor configured to move in forward order,
86499** from the beginning toward the end. In other words, the cursor is
86500** configured to use Next, not Prev.
86501**
86502** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
86503*/
86504/* Opcode: SeekLT P1 P2 P3 P4 *
86505** Synopsis: key=r[P3@P4]
86506**
86507** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
86508** use the value in register P3 as a key. If cursor P1 refers
86509** to an SQL index, then P3 is the first in an array of P4 registers
86510** that are used as an unpacked index key.
86511**
86512** Reposition cursor P1 so that it points to the largest entry that
86513** is less than the key value. If there are no records less than
86514** the key and P2 is not zero, then jump to P2.
86515**
86516** This opcode leaves the cursor configured to move in reverse order,
86517** from the end toward the beginning. In other words, the cursor is
86518** configured to use Prev, not Next.
86519**
86520** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
86521*/
86522/* Opcode: SeekLE P1 P2 P3 P4 *
86523** Synopsis: key=r[P3@P4]
86524**
86525** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
86526** use the value in register P3 as a key. If cursor P1 refers
86527** to an SQL index, then P3 is the first in an array of P4 registers
86528** that are used as an unpacked index key.
86529**
86530** Reposition cursor P1 so that it points to the largest entry that
86531** is less than or equal to the key value. If there are no records
86532** less than or equal to the key and P2 is not zero, then jump to P2.
86533**
86534** This opcode leaves the cursor configured to move in reverse order,
86535** from the end toward the beginning. In other words, the cursor is
86536** configured to use Prev, not Next.
86537**
86538** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
86539** opcode will always land on a record that equally equals the key, or
86540** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
86541** opcode must be followed by an IdxGE opcode with the same arguments.
86542** The IdxGE opcode will be skipped if this opcode succeeds, but the
86543** IdxGE opcode will be used on subsequent loop iterations.
86544**
86545** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
86546*/
86547case OP_SeekLT: /* jump, in3, group */
86548case OP_SeekLE: /* jump, in3, group */
86549case OP_SeekGE: /* jump, in3, group */
86550case OP_SeekGT: { /* jump, in3, group */
86551 int res; /* Comparison result */
86552 int oc; /* Opcode */
86553 VdbeCursor *pC; /* The cursor to seek */
86554 UnpackedRecord r; /* The key to seek for */
86555 int nField; /* Number of columns or fields in the key */
86556 i64 iKey; /* The rowid we are to seek to */
86557 int eqOnly; /* Only interested in == results */
86558
86559 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
86560 assert( pOp->p2!=0 );
86561 pC = p->apCsr[pOp->p1];
86562 assert( pC!=0 );
86563 assert( pC->eCurType==CURTYPE_BTREE );
86564 assert( OP_SeekLE == OP_SeekLT+1 );
86565 assert( OP_SeekGE == OP_SeekLT+2 );
86566 assert( OP_SeekGT == OP_SeekLT+3 );
86567 assert( pC->isOrdered );
86568 assert( pC->uc.pCursor!=0 );
86569 oc = pOp->opcode;
86570 eqOnly = 0;
86571 pC->nullRow = 0;
86572#ifdef SQLITE_DEBUG
86573 pC->seekOp = pOp->opcode;
86574#endif
86575
86576 if( pC->isTable ){
86577 /* The BTREE_SEEK_EQ flag is only set on index cursors */
86578 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
86579 || CORRUPT_DB );
86580
86581 /* The input value in P3 might be of any type: integer, real, string,
86582 ** blob, or NULL. But it needs to be an integer before we can do
86583 ** the seek, so convert it. */
86584 pIn3 = &aMem[pOp->p3];
86585 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
86586 applyNumericAffinity(pIn3, 0);
86587 }
86588 iKey = sqlite3VdbeIntValue(pIn3);
86589
86590 /* If the P3 value could not be converted into an integer without
86591 ** loss of information, then special processing is required... */
86592 if( (pIn3->flags & MEM_Int)==0 ){
86593 if( (pIn3->flags & MEM_Real)==0 ){
86594 /* If the P3 value cannot be converted into any kind of a number,
86595 ** then the seek is not possible, so jump to P2 */
86596 VdbeBranchTaken(1,2); goto jump_to_p2;
86597 break;
86598 }
86599
86600 /* If the approximation iKey is larger than the actual real search
86601 ** term, substitute >= for > and < for <=. e.g. if the search term
86602 ** is 4.9 and the integer approximation 5:
86603 **
86604 ** (x > 4.9) -> (x >= 5)
86605 ** (x <= 4.9) -> (x < 5)
86606 */
86607 if( pIn3->u.r<(double)iKey ){
86608 assert( OP_SeekGE==(OP_SeekGT-1) );
86609 assert( OP_SeekLT==(OP_SeekLE-1) );
86610 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
86611 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
86612 }
86613
86614 /* If the approximation iKey is smaller than the actual real search
86615 ** term, substitute <= for < and > for >=. */
86616 else if( pIn3->u.r>(double)iKey ){
86617 assert( OP_SeekLE==(OP_SeekLT+1) );
86618 assert( OP_SeekGT==(OP_SeekGE+1) );
86619 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
86620 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
86621 }
86622 }
86623 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
86624 pC->movetoTarget = iKey; /* Used by OP_Delete */
86625 if( rc!=SQLITE_OK ){
86626 goto abort_due_to_error;
86627 }
86628 }else{
86629 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
86630 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
86631 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
86632 */
86633 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
86634 eqOnly = 1;
86635 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
86636 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
86637 assert( pOp[1].p1==pOp[0].p1 );
86638 assert( pOp[1].p2==pOp[0].p2 );
86639 assert( pOp[1].p3==pOp[0].p3 );
86640 assert( pOp[1].p4.i==pOp[0].p4.i );
86641 }
86642
86643 nField = pOp->p4.i;
86644 assert( pOp->p4type==P4_INT32 );
86645 assert( nField>0 );
86646 r.pKeyInfo = pC->pKeyInfo;
86647 r.nField = (u16)nField;
86648
86649 /* The next line of code computes as follows, only faster:
86650 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
86651 ** r.default_rc = -1;
86652 ** }else{
86653 ** r.default_rc = +1;
86654 ** }
86655 */
86656 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
86657 assert( oc!=OP_SeekGT || r.default_rc==-1 );
86658 assert( oc!=OP_SeekLE || r.default_rc==-1 );
86659 assert( oc!=OP_SeekGE || r.default_rc==+1 );
86660 assert( oc!=OP_SeekLT || r.default_rc==+1 );
86661
86662 r.aMem = &aMem[pOp->p3];
86663#ifdef SQLITE_DEBUG
86664 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
86665#endif
86666 r.eqSeen = 0;
86667 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
86668 if( rc!=SQLITE_OK ){
86669 goto abort_due_to_error;
86670 }
86671 if( eqOnly && r.eqSeen==0 ){
86672 assert( res!=0 );
86673 goto seek_not_found;
86674 }
86675 }
86676 pC->deferredMoveto = 0;
86677 pC->cacheStatus = CACHE_STALE;
86678#ifdef SQLITE_TEST
86679 sqlite3_search_count++;
86680#endif
86681 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
86682 if( res<0 || (res==0 && oc==OP_SeekGT) ){
86683 res = 0;
86684 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
86685 if( rc!=SQLITE_OK ){
86686 if( rc==SQLITE_DONE ){
86687 rc = SQLITE_OK;
86688 res = 1;
86689 }else{
86690 goto abort_due_to_error;
86691 }
86692 }
86693 }else{
86694 res = 0;
86695 }
86696 }else{
86697 assert( oc==OP_SeekLT || oc==OP_SeekLE );
86698 if( res>0 || (res==0 && oc==OP_SeekLT) ){
86699 res = 0;
86700 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
86701 if( rc!=SQLITE_OK ){
86702 if( rc==SQLITE_DONE ){
86703 rc = SQLITE_OK;
86704 res = 1;
86705 }else{
86706 goto abort_due_to_error;
86707 }
86708 }
86709 }else{
86710 /* res might be negative because the table is empty. Check to
86711 ** see if this is the case.
86712 */
86713 res = sqlite3BtreeEof(pC->uc.pCursor);
86714 }
86715 }
86716seek_not_found:
86717 assert( pOp->p2>0 );
86718 VdbeBranchTaken(res!=0,2);
86719 if( res ){
86720 goto jump_to_p2;
86721 }else if( eqOnly ){
86722 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
86723 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
86724 }
86725 break;
86726}
86727
86728/* Opcode: SeekHit P1 P2 * * *
86729** Synopsis: seekHit=P2
86730**
86731** Set the seekHit flag on cursor P1 to the value in P2.
86732** The seekHit flag is used by the IfNoHope opcode.
86733**
86734** P1 must be a valid b-tree cursor. P2 must be a boolean value,
86735** either 0 or 1.
86736*/
86737case OP_SeekHit: {
86738 VdbeCursor *pC;
86739 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
86740 pC = p->apCsr[pOp->p1];
86741 assert( pC!=0 );
86742 assert( pOp->p2==0 || pOp->p2==1 );
86743 pC->seekHit = pOp->p2 & 1;
86744 break;
86745}
86746
86747/* Opcode: Found P1 P2 P3 P4 *
86748** Synopsis: key=r[P3@P4]
86749**
86750** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
86751** P4>0 then register P3 is the first of P4 registers that form an unpacked
86752** record.
86753**
86754** Cursor P1 is on an index btree. If the record identified by P3 and P4
86755** is a prefix of any entry in P1 then a jump is made to P2 and
86756** P1 is left pointing at the matching entry.
86757**
86758** This operation leaves the cursor in a state where it can be
86759** advanced in the forward direction. The Next instruction will work,
86760** but not the Prev instruction.
86761**
86762** See also: NotFound, NoConflict, NotExists. SeekGe
86763*/
86764/* Opcode: NotFound P1 P2 P3 P4 *
86765** Synopsis: key=r[P3@P4]
86766**
86767** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
86768** P4>0 then register P3 is the first of P4 registers that form an unpacked
86769** record.
86770**
86771** Cursor P1 is on an index btree. If the record identified by P3 and P4
86772** is not the prefix of any entry in P1 then a jump is made to P2. If P1
86773** does contain an entry whose prefix matches the P3/P4 record then control
86774** falls through to the next instruction and P1 is left pointing at the
86775** matching entry.
86776**
86777** This operation leaves the cursor in a state where it cannot be
86778** advanced in either direction. In other words, the Next and Prev
86779** opcodes do not work after this operation.
86780**
86781** See also: Found, NotExists, NoConflict, IfNoHope
86782*/
86783/* Opcode: IfNoHope P1 P2 P3 P4 *
86784** Synopsis: key=r[P3@P4]
86785**
86786** Register P3 is the first of P4 registers that form an unpacked
86787** record.
86788**
86789** Cursor P1 is on an index btree. If the seekHit flag is set on P1, then
86790** this opcode is a no-op. But if the seekHit flag of P1 is clear, then
86791** check to see if there is any entry in P1 that matches the
86792** prefix identified by P3 and P4. If no entry matches the prefix,
86793** jump to P2. Otherwise fall through.
86794**
86795** This opcode behaves like OP_NotFound if the seekHit
86796** flag is clear and it behaves like OP_Noop if the seekHit flag is set.
86797**
86798** This opcode is used in IN clause processing for a multi-column key.
86799** If an IN clause is attached to an element of the key other than the
86800** left-most element, and if there are no matches on the most recent
86801** seek over the whole key, then it might be that one of the key element
86802** to the left is prohibiting a match, and hence there is "no hope" of
86803** any match regardless of how many IN clause elements are checked.
86804** In such a case, we abandon the IN clause search early, using this
86805** opcode. The opcode name comes from the fact that the
86806** jump is taken if there is "no hope" of achieving a match.
86807**
86808** See also: NotFound, SeekHit
86809*/
86810/* Opcode: NoConflict P1 P2 P3 P4 *
86811** Synopsis: key=r[P3@P4]
86812**
86813** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
86814** P4>0 then register P3 is the first of P4 registers that form an unpacked
86815** record.
86816**
86817** Cursor P1 is on an index btree. If the record identified by P3 and P4
86818** contains any NULL value, jump immediately to P2. If all terms of the
86819** record are not-NULL then a check is done to determine if any row in the
86820** P1 index btree has a matching key prefix. If there are no matches, jump
86821** immediately to P2. If there is a match, fall through and leave the P1
86822** cursor pointing to the matching row.
86823**
86824** This opcode is similar to OP_NotFound with the exceptions that the
86825** branch is always taken if any part of the search key input is NULL.
86826**
86827** This operation leaves the cursor in a state where it cannot be
86828** advanced in either direction. In other words, the Next and Prev
86829** opcodes do not work after this operation.
86830**
86831** See also: NotFound, Found, NotExists
86832*/
86833case OP_IfNoHope: { /* jump, in3 */
86834 VdbeCursor *pC;
86835 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
86836 pC = p->apCsr[pOp->p1];
86837 assert( pC!=0 );
86838 if( pC->seekHit ) break;
86839 /* Fall through into OP_NotFound */
86840}
86841case OP_NoConflict: /* jump, in3 */
86842case OP_NotFound: /* jump, in3 */
86843case OP_Found: { /* jump, in3 */
86844 int alreadyExists;
86845 int takeJump;
86846 int ii;
86847 VdbeCursor *pC;
86848 int res;
86849 UnpackedRecord *pFree;
86850 UnpackedRecord *pIdxKey;
86851 UnpackedRecord r;
86852
86853#ifdef SQLITE_TEST
86854 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
86855#endif
86856
86857 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
86858 assert( pOp->p4type==P4_INT32 );
86859 pC = p->apCsr[pOp->p1];
86860 assert( pC!=0 );
86861#ifdef SQLITE_DEBUG
86862 pC->seekOp = pOp->opcode;
86863#endif
86864 pIn3 = &aMem[pOp->p3];
86865 assert( pC->eCurType==CURTYPE_BTREE );
86866 assert( pC->uc.pCursor!=0 );
86867 assert( pC->isTable==0 );
86868 if( pOp->p4.i>0 ){
86869 r.pKeyInfo = pC->pKeyInfo;
86870 r.nField = (u16)pOp->p4.i;
86871 r.aMem = pIn3;
86872#ifdef SQLITE_DEBUG
86873 for(ii=0; ii<r.nField; ii++){
86874 assert( memIsValid(&r.aMem[ii]) );
86875 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
86876 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
86877 }
86878#endif
86879 pIdxKey = &r;
86880 pFree = 0;
86881 }else{
86882 assert( pIn3->flags & MEM_Blob );
86883 rc = ExpandBlob(pIn3);
86884 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
86885 if( rc ) goto no_mem;
86886 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
86887 if( pIdxKey==0 ) goto no_mem;
86888 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
86889 }
86890 pIdxKey->default_rc = 0;
86891 takeJump = 0;
86892 if( pOp->opcode==OP_NoConflict ){
86893 /* For the OP_NoConflict opcode, take the jump if any of the
86894 ** input fields are NULL, since any key with a NULL will not
86895 ** conflict */
86896 for(ii=0; ii<pIdxKey->nField; ii++){
86897 if( pIdxKey->aMem[ii].flags & MEM_Null ){
86898 takeJump = 1;
86899 break;
86900 }
86901 }
86902 }
86903 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
86904 if( pFree ) sqlite3DbFreeNN(db, pFree);
86905 if( rc!=SQLITE_OK ){
86906 goto abort_due_to_error;
86907 }
86908 pC->seekResult = res;
86909 alreadyExists = (res==0);
86910 pC->nullRow = 1-alreadyExists;
86911 pC->deferredMoveto = 0;
86912 pC->cacheStatus = CACHE_STALE;
86913 if( pOp->opcode==OP_Found ){
86914 VdbeBranchTaken(alreadyExists!=0,2);
86915 if( alreadyExists ) goto jump_to_p2;
86916 }else{
86917 VdbeBranchTaken(takeJump||alreadyExists==0,2);
86918 if( takeJump || !alreadyExists ) goto jump_to_p2;
86919 }
86920 break;
86921}
86922
86923/* Opcode: SeekRowid P1 P2 P3 * *
86924** Synopsis: intkey=r[P3]
86925**
86926** P1 is the index of a cursor open on an SQL table btree (with integer
86927** keys). If register P3 does not contain an integer or if P1 does not
86928** contain a record with rowid P3 then jump immediately to P2.
86929** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
86930** a record with rowid P3 then
86931** leave the cursor pointing at that record and fall through to the next
86932** instruction.
86933**
86934** The OP_NotExists opcode performs the same operation, but with OP_NotExists
86935** the P3 register must be guaranteed to contain an integer value. With this
86936** opcode, register P3 might not contain an integer.
86937**
86938** The OP_NotFound opcode performs the same operation on index btrees
86939** (with arbitrary multi-value keys).
86940**
86941** This opcode leaves the cursor in a state where it cannot be advanced
86942** in either direction. In other words, the Next and Prev opcodes will
86943** not work following this opcode.
86944**
86945** See also: Found, NotFound, NoConflict, SeekRowid
86946*/
86947/* Opcode: NotExists P1 P2 P3 * *
86948** Synopsis: intkey=r[P3]
86949**
86950** P1 is the index of a cursor open on an SQL table btree (with integer
86951** keys). P3 is an integer rowid. If P1 does not contain a record with
86952** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
86953** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
86954** leave the cursor pointing at that record and fall through to the next
86955** instruction.
86956**
86957** The OP_SeekRowid opcode performs the same operation but also allows the
86958** P3 register to contain a non-integer value, in which case the jump is
86959** always taken. This opcode requires that P3 always contain an integer.
86960**
86961** The OP_NotFound opcode performs the same operation on index btrees
86962** (with arbitrary multi-value keys).
86963**
86964** This opcode leaves the cursor in a state where it cannot be advanced
86965** in either direction. In other words, the Next and Prev opcodes will
86966** not work following this opcode.
86967**
86968** See also: Found, NotFound, NoConflict, SeekRowid
86969*/
86970case OP_SeekRowid: { /* jump, in3 */
86971 VdbeCursor *pC;
86972 BtCursor *pCrsr;
86973 int res;
86974 u64 iKey;
86975
86976 pIn3 = &aMem[pOp->p3];
86977 if( (pIn3->flags & MEM_Int)==0 ){
86978 /* Make sure pIn3->u.i contains a valid integer representation of
86979 ** the key value, but do not change the datatype of the register, as
86980 ** other parts of the perpared statement might be depending on the
86981 ** current datatype. */
86982 u16 origFlags = pIn3->flags;
86983 int isNotInt;
86984 applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);
86985 isNotInt = (pIn3->flags & MEM_Int)==0;
86986 pIn3->flags = origFlags;
86987 if( isNotInt ) goto jump_to_p2;
86988 }
86989 /* Fall through into OP_NotExists */
86990case OP_NotExists: /* jump, in3 */
86991 pIn3 = &aMem[pOp->p3];
86992 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
86993 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
86994 pC = p->apCsr[pOp->p1];
86995 assert( pC!=0 );
86996#ifdef SQLITE_DEBUG
86997 pC->seekOp = OP_SeekRowid;
86998#endif
86999 assert( pC->isTable );
87000 assert( pC->eCurType==CURTYPE_BTREE );
87001 pCrsr = pC->uc.pCursor;
87002 assert( pCrsr!=0 );
87003 res = 0;
87004 iKey = pIn3->u.i;
87005 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
87006 assert( rc==SQLITE_OK || res==0 );
87007 pC->movetoTarget = iKey; /* Used by OP_Delete */
87008 pC->nullRow = 0;
87009 pC->cacheStatus = CACHE_STALE;
87010 pC->deferredMoveto = 0;
87011 VdbeBranchTaken(res!=0,2);
87012 pC->seekResult = res;
87013 if( res!=0 ){
87014 assert( rc==SQLITE_OK );
87015 if( pOp->p2==0 ){
87016 rc = SQLITE_CORRUPT_BKPT;
87017 }else{
87018 goto jump_to_p2;
87019 }
87020 }
87021 if( rc ) goto abort_due_to_error;
87022 break;
87023}
87024
87025/* Opcode: Sequence P1 P2 * * *
87026** Synopsis: r[P2]=cursor[P1].ctr++
87027**
87028** Find the next available sequence number for cursor P1.
87029** Write the sequence number into register P2.
87030** The sequence number on the cursor is incremented after this
87031** instruction.
87032*/
87033case OP_Sequence: { /* out2 */
87034 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87035 assert( p->apCsr[pOp->p1]!=0 );
87036 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
87037 pOut = out2Prerelease(p, pOp);
87038 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
87039 break;
87040}
87041
87042
87043/* Opcode: NewRowid P1 P2 P3 * *
87044** Synopsis: r[P2]=rowid
87045**
87046** Get a new integer record number (a.k.a "rowid") used as the key to a table.
87047** The record number is not previously used as a key in the database
87048** table that cursor P1 points to. The new record number is written
87049** written to register P2.
87050**
87051** If P3>0 then P3 is a register in the root frame of this VDBE that holds
87052** the largest previously generated record number. No new record numbers are
87053** allowed to be less than this value. When this value reaches its maximum,
87054** an SQLITE_FULL error is generated. The P3 register is updated with the '
87055** generated record number. This P3 mechanism is used to help implement the
87056** AUTOINCREMENT feature.
87057*/
87058case OP_NewRowid: { /* out2 */
87059 i64 v; /* The new rowid */
87060 VdbeCursor *pC; /* Cursor of table to get the new rowid */
87061 int res; /* Result of an sqlite3BtreeLast() */
87062 int cnt; /* Counter to limit the number of searches */
87063 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
87064 VdbeFrame *pFrame; /* Root frame of VDBE */
87065
87066 v = 0;
87067 res = 0;
87068 pOut = out2Prerelease(p, pOp);
87069 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87070 pC = p->apCsr[pOp->p1];
87071 assert( pC!=0 );
87072 assert( pC->isTable );
87073 assert( pC->eCurType==CURTYPE_BTREE );
87074 assert( pC->uc.pCursor!=0 );
87075 {
87076 /* The next rowid or record number (different terms for the same
87077 ** thing) is obtained in a two-step algorithm.
87078 **
87079 ** First we attempt to find the largest existing rowid and add one
87080 ** to that. But if the largest existing rowid is already the maximum
87081 ** positive integer, we have to fall through to the second
87082 ** probabilistic algorithm
87083 **
87084 ** The second algorithm is to select a rowid at random and see if
87085 ** it already exists in the table. If it does not exist, we have
87086 ** succeeded. If the random rowid does exist, we select a new one
87087 ** and try again, up to 100 times.
87088 */
87089 assert( pC->isTable );
87090
87091#ifdef SQLITE_32BIT_ROWID
87092# define MAX_ROWID 0x7fffffff
87093#else
87094 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
87095 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
87096 ** to provide the constant while making all compilers happy.
87097 */
87098# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
87099#endif
87100
87101 if( !pC->useRandomRowid ){
87102 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
87103 if( rc!=SQLITE_OK ){
87104 goto abort_due_to_error;
87105 }
87106 if( res ){
87107 v = 1; /* IMP: R-61914-48074 */
87108 }else{
87109 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
87110 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
87111 if( v>=MAX_ROWID ){
87112 pC->useRandomRowid = 1;
87113 }else{
87114 v++; /* IMP: R-29538-34987 */
87115 }
87116 }
87117 }
87118
87119#ifndef SQLITE_OMIT_AUTOINCREMENT
87120 if( pOp->p3 ){
87121 /* Assert that P3 is a valid memory cell. */
87122 assert( pOp->p3>0 );
87123 if( p->pFrame ){
87124 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
87125 /* Assert that P3 is a valid memory cell. */
87126 assert( pOp->p3<=pFrame->nMem );
87127 pMem = &pFrame->aMem[pOp->p3];
87128 }else{
87129 /* Assert that P3 is a valid memory cell. */
87130 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
87131 pMem = &aMem[pOp->p3];
87132 memAboutToChange(p, pMem);
87133 }
87134 assert( memIsValid(pMem) );
87135
87136 REGISTER_TRACE(pOp->p3, pMem);
87137 sqlite3VdbeMemIntegerify(pMem);
87138 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
87139 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
87140 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
87141 goto abort_due_to_error;
87142 }
87143 if( v<pMem->u.i+1 ){
87144 v = pMem->u.i + 1;
87145 }
87146 pMem->u.i = v;
87147 }
87148#endif
87149 if( pC->useRandomRowid ){
87150 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
87151 ** largest possible integer (9223372036854775807) then the database
87152 ** engine starts picking positive candidate ROWIDs at random until
87153 ** it finds one that is not previously used. */
87154 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
87155 ** an AUTOINCREMENT table. */
87156 cnt = 0;
87157 do{
87158 sqlite3_randomness(sizeof(v), &v);
87159 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
87160 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
87161 0, &res))==SQLITE_OK)
87162 && (res==0)
87163 && (++cnt<100));
87164 if( rc ) goto abort_due_to_error;
87165 if( res==0 ){
87166 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
87167 goto abort_due_to_error;
87168 }
87169 assert( v>0 ); /* EV: R-40812-03570 */
87170 }
87171 pC->deferredMoveto = 0;
87172 pC->cacheStatus = CACHE_STALE;
87173 }
87174 pOut->u.i = v;
87175 break;
87176}
87177
87178/* Opcode: Insert P1 P2 P3 P4 P5
87179** Synopsis: intkey=r[P3] data=r[P2]
87180**
87181** Write an entry into the table of cursor P1. A new entry is
87182** created if it doesn't already exist or the data for an existing
87183** entry is overwritten. The data is the value MEM_Blob stored in register
87184** number P2. The key is stored in register P3. The key must
87185** be a MEM_Int.
87186**
87187** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
87188** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
87189** then rowid is stored for subsequent return by the
87190** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
87191**
87192** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
87193** run faster by avoiding an unnecessary seek on cursor P1. However,
87194** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
87195** seeks on the cursor or if the most recent seek used a key equal to P3.
87196**
87197** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
87198** UPDATE operation. Otherwise (if the flag is clear) then this opcode
87199** is part of an INSERT operation. The difference is only important to
87200** the update hook.
87201**
87202** Parameter P4 may point to a Table structure, or may be NULL. If it is
87203** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
87204** following a successful insert.
87205**
87206** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
87207** allocated, then ownership of P2 is transferred to the pseudo-cursor
87208** and register P2 becomes ephemeral. If the cursor is changed, the
87209** value of register P2 will then change. Make sure this does not
87210** cause any problems.)
87211**
87212** This instruction only works on tables. The equivalent instruction
87213** for indices is OP_IdxInsert.
87214*/
87215/* Opcode: InsertInt P1 P2 P3 P4 P5
87216** Synopsis: intkey=P3 data=r[P2]
87217**
87218** This works exactly like OP_Insert except that the key is the
87219** integer value P3, not the value of the integer stored in register P3.
87220*/
87221case OP_Insert:
87222case OP_InsertInt: {
87223 Mem *pData; /* MEM cell holding data for the record to be inserted */
87224 Mem *pKey; /* MEM cell holding key for the record */
87225 VdbeCursor *pC; /* Cursor to table into which insert is written */
87226 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
87227 const char *zDb; /* database name - used by the update hook */
87228 Table *pTab; /* Table structure - used by update and pre-update hooks */
87229 BtreePayload x; /* Payload to be inserted */
87230
87231 pData = &aMem[pOp->p2];
87232 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87233 assert( memIsValid(pData) );
87234 pC = p->apCsr[pOp->p1];
87235 assert( pC!=0 );
87236 assert( pC->eCurType==CURTYPE_BTREE );
87237 assert( pC->uc.pCursor!=0 );
87238 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
87239 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
87240 REGISTER_TRACE(pOp->p2, pData);
87241 sqlite3VdbeIncrWriteCounter(p, pC);
87242
87243 if( pOp->opcode==OP_Insert ){
87244 pKey = &aMem[pOp->p3];
87245 assert( pKey->flags & MEM_Int );
87246 assert( memIsValid(pKey) );
87247 REGISTER_TRACE(pOp->p3, pKey);
87248 x.nKey = pKey->u.i;
87249 }else{
87250 assert( pOp->opcode==OP_InsertInt );
87251 x.nKey = pOp->p3;
87252 }
87253
87254 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
87255 assert( pC->iDb>=0 );
87256 zDb = db->aDb[pC->iDb].zDbSName;
87257 pTab = pOp->p4.pTab;
87258 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
87259 }else{
87260 pTab = 0;
87261 zDb = 0; /* Not needed. Silence a compiler warning. */
87262 }
87263
87264#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
87265 /* Invoke the pre-update hook, if any */
87266 if( pTab ){
87267 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
87268 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
87269 }
87270 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
87271 /* Prevent post-update hook from running in cases when it should not */
87272 pTab = 0;
87273 }
87274 }
87275 if( pOp->p5 & OPFLAG_ISNOOP ) break;
87276#endif
87277
87278 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
87279 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
87280 assert( pData->flags & (MEM_Blob|MEM_Str) );
87281 x.pData = pData->z;
87282 x.nData = pData->n;
87283 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
87284 if( pData->flags & MEM_Zero ){
87285 x.nZero = pData->u.nZero;
87286 }else{
87287 x.nZero = 0;
87288 }
87289 x.pKey = 0;
87290 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
87291 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
87292 );
87293 pC->deferredMoveto = 0;
87294 pC->cacheStatus = CACHE_STALE;
87295
87296 /* Invoke the update-hook if required. */
87297 if( rc ) goto abort_due_to_error;
87298 if( pTab ){
87299 assert( db->xUpdateCallback!=0 );
87300 assert( pTab->aCol!=0 );
87301 db->xUpdateCallback(db->pUpdateArg,
87302 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
87303 zDb, pTab->zName, x.nKey);
87304 }
87305 break;
87306}
87307
87308/* Opcode: Delete P1 P2 P3 P4 P5
87309**
87310** Delete the record at which the P1 cursor is currently pointing.
87311**
87312** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
87313** the cursor will be left pointing at either the next or the previous
87314** record in the table. If it is left pointing at the next record, then
87315** the next Next instruction will be a no-op. As a result, in this case
87316** it is ok to delete a record from within a Next loop. If
87317** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
87318** left in an undefined state.
87319**
87320** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
87321** delete one of several associated with deleting a table row and all its
87322** associated index entries. Exactly one of those deletes is the "primary"
87323** delete. The others are all on OPFLAG_FORDELETE cursors or else are
87324** marked with the AUXDELETE flag.
87325**
87326** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
87327** change count is incremented (otherwise not).
87328**
87329** P1 must not be pseudo-table. It has to be a real table with
87330** multiple rows.
87331**
87332** If P4 is not NULL then it points to a Table object. In this case either
87333** the update or pre-update hook, or both, may be invoked. The P1 cursor must
87334** have been positioned using OP_NotFound prior to invoking this opcode in
87335** this case. Specifically, if one is configured, the pre-update hook is
87336** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
87337** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
87338**
87339** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
87340** of the memory cell that contains the value that the rowid of the row will
87341** be set to by the update.
87342*/
87343case OP_Delete: {
87344 VdbeCursor *pC;
87345 const char *zDb;
87346 Table *pTab;
87347 int opflags;
87348
87349 opflags = pOp->p2;
87350 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87351 pC = p->apCsr[pOp->p1];
87352 assert( pC!=0 );
87353 assert( pC->eCurType==CURTYPE_BTREE );
87354 assert( pC->uc.pCursor!=0 );
87355 assert( pC->deferredMoveto==0 );
87356 sqlite3VdbeIncrWriteCounter(p, pC);
87357
87358#ifdef SQLITE_DEBUG
87359 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
87360 /* If p5 is zero, the seek operation that positioned the cursor prior to
87361 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
87362 ** the row that is being deleted */
87363 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
87364 assert( pC->movetoTarget==iKey );
87365 }
87366#endif
87367
87368 /* If the update-hook or pre-update-hook will be invoked, set zDb to
87369 ** the name of the db to pass as to it. Also set local pTab to a copy
87370 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
87371 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
87372 ** VdbeCursor.movetoTarget to the current rowid. */
87373 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
87374 assert( pC->iDb>=0 );
87375 assert( pOp->p4.pTab!=0 );
87376 zDb = db->aDb[pC->iDb].zDbSName;
87377 pTab = pOp->p4.pTab;
87378 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
87379 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
87380 }
87381 }else{
87382 zDb = 0; /* Not needed. Silence a compiler warning. */
87383 pTab = 0; /* Not needed. Silence a compiler warning. */
87384 }
87385
87386#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
87387 /* Invoke the pre-update-hook if required. */
87388 if( db->xPreUpdateCallback && pOp->p4.pTab ){
87389 assert( !(opflags & OPFLAG_ISUPDATE)
87390 || HasRowid(pTab)==0
87391 || (aMem[pOp->p3].flags & MEM_Int)
87392 );
87393 sqlite3VdbePreUpdateHook(p, pC,
87394 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
87395 zDb, pTab, pC->movetoTarget,
87396 pOp->p3
87397 );
87398 }
87399 if( opflags & OPFLAG_ISNOOP ) break;
87400#endif
87401
87402 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
87403 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
87404 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
87405 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
87406
87407#ifdef SQLITE_DEBUG
87408 if( p->pFrame==0 ){
87409 if( pC->isEphemeral==0
87410 && (pOp->p5 & OPFLAG_AUXDELETE)==0
87411 && (pC->wrFlag & OPFLAG_FORDELETE)==0
87412 ){
87413 nExtraDelete++;
87414 }
87415 if( pOp->p2 & OPFLAG_NCHANGE ){
87416 nExtraDelete--;
87417 }
87418 }
87419#endif
87420
87421 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
87422 pC->cacheStatus = CACHE_STALE;
87423 pC->seekResult = 0;
87424 if( rc ) goto abort_due_to_error;
87425
87426 /* Invoke the update-hook if required. */
87427 if( opflags & OPFLAG_NCHANGE ){
87428 p->nChange++;
87429 if( db->xUpdateCallback && HasRowid(pTab) ){
87430 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
87431 pC->movetoTarget);
87432 assert( pC->iDb>=0 );
87433 }
87434 }
87435
87436 break;
87437}
87438/* Opcode: ResetCount * * * * *
87439**
87440** The value of the change counter is copied to the database handle
87441** change counter (returned by subsequent calls to sqlite3_changes()).
87442** Then the VMs internal change counter resets to 0.
87443** This is used by trigger programs.
87444*/
87445case OP_ResetCount: {
87446 sqlite3VdbeSetChanges(db, p->nChange);
87447 p->nChange = 0;
87448 break;
87449}
87450
87451/* Opcode: SorterCompare P1 P2 P3 P4
87452** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
87453**
87454** P1 is a sorter cursor. This instruction compares a prefix of the
87455** record blob in register P3 against a prefix of the entry that
87456** the sorter cursor currently points to. Only the first P4 fields
87457** of r[P3] and the sorter record are compared.
87458**
87459** If either P3 or the sorter contains a NULL in one of their significant
87460** fields (not counting the P4 fields at the end which are ignored) then
87461** the comparison is assumed to be equal.
87462**
87463** Fall through to next instruction if the two records compare equal to
87464** each other. Jump to P2 if they are different.
87465*/
87466case OP_SorterCompare: {
87467 VdbeCursor *pC;
87468 int res;
87469 int nKeyCol;
87470
87471 pC = p->apCsr[pOp->p1];
87472 assert( isSorter(pC) );
87473 assert( pOp->p4type==P4_INT32 );
87474 pIn3 = &aMem[pOp->p3];
87475 nKeyCol = pOp->p4.i;
87476 res = 0;
87477 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
87478 VdbeBranchTaken(res!=0,2);
87479 if( rc ) goto abort_due_to_error;
87480 if( res ) goto jump_to_p2;
87481 break;
87482};
87483
87484/* Opcode: SorterData P1 P2 P3 * *
87485** Synopsis: r[P2]=data
87486**
87487** Write into register P2 the current sorter data for sorter cursor P1.
87488** Then clear the column header cache on cursor P3.
87489**
87490** This opcode is normally use to move a record out of the sorter and into
87491** a register that is the source for a pseudo-table cursor created using
87492** OpenPseudo. That pseudo-table cursor is the one that is identified by
87493** parameter P3. Clearing the P3 column cache as part of this opcode saves
87494** us from having to issue a separate NullRow instruction to clear that cache.
87495*/
87496case OP_SorterData: {
87497 VdbeCursor *pC;
87498
87499 pOut = &aMem[pOp->p2];
87500 pC = p->apCsr[pOp->p1];
87501 assert( isSorter(pC) );
87502 rc = sqlite3VdbeSorterRowkey(pC, pOut);
87503 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
87504 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87505 if( rc ) goto abort_due_to_error;
87506 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
87507 break;
87508}
87509
87510/* Opcode: RowData P1 P2 P3 * *
87511** Synopsis: r[P2]=data
87512**
87513** Write into register P2 the complete row content for the row at
87514** which cursor P1 is currently pointing.
87515** There is no interpretation of the data.
87516** It is just copied onto the P2 register exactly as
87517** it is found in the database file.
87518**
87519** If cursor P1 is an index, then the content is the key of the row.
87520** If cursor P2 is a table, then the content extracted is the data.
87521**
87522** If the P1 cursor must be pointing to a valid row (not a NULL row)
87523** of a real table, not a pseudo-table.
87524**
87525** If P3!=0 then this opcode is allowed to make an ephemeral pointer
87526** into the database page. That means that the content of the output
87527** register will be invalidated as soon as the cursor moves - including
87528** moves caused by other cursors that "save" the current cursors
87529** position in order that they can write to the same table. If P3==0
87530** then a copy of the data is made into memory. P3!=0 is faster, but
87531** P3==0 is safer.
87532**
87533** If P3!=0 then the content of the P2 register is unsuitable for use
87534** in OP_Result and any OP_Result will invalidate the P2 register content.
87535** The P2 register content is invalidated by opcodes like OP_Function or
87536** by any use of another cursor pointing to the same table.
87537*/
87538case OP_RowData: {
87539 VdbeCursor *pC;
87540 BtCursor *pCrsr;
87541 u32 n;
87542
87543 pOut = out2Prerelease(p, pOp);
87544
87545 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87546 pC = p->apCsr[pOp->p1];
87547 assert( pC!=0 );
87548 assert( pC->eCurType==CURTYPE_BTREE );
87549 assert( isSorter(pC)==0 );
87550 assert( pC->nullRow==0 );
87551 assert( pC->uc.pCursor!=0 );
87552 pCrsr = pC->uc.pCursor;
87553
87554 /* The OP_RowData opcodes always follow OP_NotExists or
87555 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
87556 ** that might invalidate the cursor.
87557 ** If this where not the case, on of the following assert()s
87558 ** would fail. Should this ever change (because of changes in the code
87559 ** generator) then the fix would be to insert a call to
87560 ** sqlite3VdbeCursorMoveto().
87561 */
87562 assert( pC->deferredMoveto==0 );
87563 assert( sqlite3BtreeCursorIsValid(pCrsr) );
87564#if 0 /* Not required due to the previous to assert() statements */
87565 rc = sqlite3VdbeCursorMoveto(pC);
87566 if( rc!=SQLITE_OK ) goto abort_due_to_error;
87567#endif
87568
87569 n = sqlite3BtreePayloadSize(pCrsr);
87570 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
87571 goto too_big;
87572 }
87573 testcase( n==0 );
87574 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
87575 if( rc ) goto abort_due_to_error;
87576 if( !pOp->p3 ) Deephemeralize(pOut);
87577 UPDATE_MAX_BLOBSIZE(pOut);
87578 REGISTER_TRACE(pOp->p2, pOut);
87579 break;
87580}
87581
87582/* Opcode: Rowid P1 P2 * * *
87583** Synopsis: r[P2]=rowid
87584**
87585** Store in register P2 an integer which is the key of the table entry that
87586** P1 is currently point to.
87587**
87588** P1 can be either an ordinary table or a virtual table. There used to
87589** be a separate OP_VRowid opcode for use with virtual tables, but this
87590** one opcode now works for both table types.
87591*/
87592case OP_Rowid: { /* out2 */
87593 VdbeCursor *pC;
87594 i64 v;
87595 sqlite3_vtab *pVtab;
87596 const sqlite3_module *pModule;
87597
87598 pOut = out2Prerelease(p, pOp);
87599 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87600 pC = p->apCsr[pOp->p1];
87601 assert( pC!=0 );
87602 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
87603 if( pC->nullRow ){
87604 pOut->flags = MEM_Null;
87605 break;
87606 }else if( pC->deferredMoveto ){
87607 v = pC->movetoTarget;
87608#ifndef SQLITE_OMIT_VIRTUALTABLE
87609 }else if( pC->eCurType==CURTYPE_VTAB ){
87610 assert( pC->uc.pVCur!=0 );
87611 pVtab = pC->uc.pVCur->pVtab;
87612 pModule = pVtab->pModule;
87613 assert( pModule->xRowid );
87614 rc = pModule->xRowid(pC->uc.pVCur, &v);
87615 sqlite3VtabImportErrmsg(p, pVtab);
87616 if( rc ) goto abort_due_to_error;
87617#endif /* SQLITE_OMIT_VIRTUALTABLE */
87618 }else{
87619 assert( pC->eCurType==CURTYPE_BTREE );
87620 assert( pC->uc.pCursor!=0 );
87621 rc = sqlite3VdbeCursorRestore(pC);
87622 if( rc ) goto abort_due_to_error;
87623 if( pC->nullRow ){
87624 pOut->flags = MEM_Null;
87625 break;
87626 }
87627 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
87628 }
87629 pOut->u.i = v;
87630 break;
87631}
87632
87633/* Opcode: NullRow P1 * * * *
87634**
87635** Move the cursor P1 to a null row. Any OP_Column operations
87636** that occur while the cursor is on the null row will always
87637** write a NULL.
87638*/
87639case OP_NullRow: {
87640 VdbeCursor *pC;
87641
87642 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87643 pC = p->apCsr[pOp->p1];
87644 assert( pC!=0 );
87645 pC->nullRow = 1;
87646 pC->cacheStatus = CACHE_STALE;
87647 if( pC->eCurType==CURTYPE_BTREE ){
87648 assert( pC->uc.pCursor!=0 );
87649 sqlite3BtreeClearCursor(pC->uc.pCursor);
87650 }
87651#ifdef SQLITE_DEBUG
87652 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
87653#endif
87654 break;
87655}
87656
87657/* Opcode: SeekEnd P1 * * * *
87658**
87659** Position cursor P1 at the end of the btree for the purpose of
87660** appending a new entry onto the btree.
87661**
87662** It is assumed that the cursor is used only for appending and so
87663** if the cursor is valid, then the cursor must already be pointing
87664** at the end of the btree and so no changes are made to
87665** the cursor.
87666*/
87667/* Opcode: Last P1 P2 * * *
87668**
87669** The next use of the Rowid or Column or Prev instruction for P1
87670** will refer to the last entry in the database table or index.
87671** If the table or index is empty and P2>0, then jump immediately to P2.
87672** If P2 is 0 or if the table or index is not empty, fall through
87673** to the following instruction.
87674**
87675** This opcode leaves the cursor configured to move in reverse order,
87676** from the end toward the beginning. In other words, the cursor is
87677** configured to use Prev, not Next.
87678*/
87679case OP_SeekEnd:
87680case OP_Last: { /* jump */
87681 VdbeCursor *pC;
87682 BtCursor *pCrsr;
87683 int res;
87684
87685 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87686 pC = p->apCsr[pOp->p1];
87687 assert( pC!=0 );
87688 assert( pC->eCurType==CURTYPE_BTREE );
87689 pCrsr = pC->uc.pCursor;
87690 res = 0;
87691 assert( pCrsr!=0 );
87692#ifdef SQLITE_DEBUG
87693 pC->seekOp = pOp->opcode;
87694#endif
87695 if( pOp->opcode==OP_SeekEnd ){
87696 assert( pOp->p2==0 );
87697 pC->seekResult = -1;
87698 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
87699 break;
87700 }
87701 }
87702 rc = sqlite3BtreeLast(pCrsr, &res);
87703 pC->nullRow = (u8)res;
87704 pC->deferredMoveto = 0;
87705 pC->cacheStatus = CACHE_STALE;
87706 if( rc ) goto abort_due_to_error;
87707 if( pOp->p2>0 ){
87708 VdbeBranchTaken(res!=0,2);
87709 if( res ) goto jump_to_p2;
87710 }
87711 break;
87712}
87713
87714/* Opcode: IfSmaller P1 P2 P3 * *
87715**
87716** Estimate the number of rows in the table P1. Jump to P2 if that
87717** estimate is less than approximately 2**(0.1*P3).
87718*/
87719case OP_IfSmaller: { /* jump */
87720 VdbeCursor *pC;
87721 BtCursor *pCrsr;
87722 int res;
87723 i64 sz;
87724
87725 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87726 pC = p->apCsr[pOp->p1];
87727 assert( pC!=0 );
87728 pCrsr = pC->uc.pCursor;
87729 assert( pCrsr );
87730 rc = sqlite3BtreeFirst(pCrsr, &res);
87731 if( rc ) goto abort_due_to_error;
87732 if( res==0 ){
87733 sz = sqlite3BtreeRowCountEst(pCrsr);
87734 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
87735 }
87736 VdbeBranchTaken(res!=0,2);
87737 if( res ) goto jump_to_p2;
87738 break;
87739}
87740
87741
87742/* Opcode: SorterSort P1 P2 * * *
87743**
87744** After all records have been inserted into the Sorter object
87745** identified by P1, invoke this opcode to actually do the sorting.
87746** Jump to P2 if there are no records to be sorted.
87747**
87748** This opcode is an alias for OP_Sort and OP_Rewind that is used
87749** for Sorter objects.
87750*/
87751/* Opcode: Sort P1 P2 * * *
87752**
87753** This opcode does exactly the same thing as OP_Rewind except that
87754** it increments an undocumented global variable used for testing.
87755**
87756** Sorting is accomplished by writing records into a sorting index,
87757** then rewinding that index and playing it back from beginning to
87758** end. We use the OP_Sort opcode instead of OP_Rewind to do the
87759** rewinding so that the global variable will be incremented and
87760** regression tests can determine whether or not the optimizer is
87761** correctly optimizing out sorts.
87762*/
87763case OP_SorterSort: /* jump */
87764case OP_Sort: { /* jump */
87765#ifdef SQLITE_TEST
87766 sqlite3_sort_count++;
87767 sqlite3_search_count--;
87768#endif
87769 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
87770 /* Fall through into OP_Rewind */
87771}
87772/* Opcode: Rewind P1 P2 * * P5
87773**
87774** The next use of the Rowid or Column or Next instruction for P1
87775** will refer to the first entry in the database table or index.
87776** If the table or index is empty, jump immediately to P2.
87777** If the table or index is not empty, fall through to the following
87778** instruction.
87779**
87780** If P5 is non-zero and the table is not empty, then the "skip-next"
87781** flag is set on the cursor so that the next OP_Next instruction
87782** executed on it is a no-op.
87783**
87784** This opcode leaves the cursor configured to move in forward order,
87785** from the beginning toward the end. In other words, the cursor is
87786** configured to use Next, not Prev.
87787*/
87788case OP_Rewind: { /* jump */
87789 VdbeCursor *pC;
87790 BtCursor *pCrsr;
87791 int res;
87792
87793 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87794 pC = p->apCsr[pOp->p1];
87795 assert( pC!=0 );
87796 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
87797 res = 1;
87798#ifdef SQLITE_DEBUG
87799 pC->seekOp = OP_Rewind;
87800#endif
87801 if( isSorter(pC) ){
87802 rc = sqlite3VdbeSorterRewind(pC, &res);
87803 }else{
87804 assert( pC->eCurType==CURTYPE_BTREE );
87805 pCrsr = pC->uc.pCursor;
87806 assert( pCrsr );
87807 rc = sqlite3BtreeFirst(pCrsr, &res);
87808#ifndef SQLITE_OMIT_WINDOWFUNC
87809 if( pOp->p5 ) sqlite3BtreeSkipNext(pCrsr);
87810#endif
87811 pC->deferredMoveto = 0;
87812 pC->cacheStatus = CACHE_STALE;
87813 }
87814 if( rc ) goto abort_due_to_error;
87815 pC->nullRow = (u8)res;
87816 assert( pOp->p2>0 && pOp->p2<p->nOp );
87817 VdbeBranchTaken(res!=0,2);
87818 if( res ) goto jump_to_p2;
87819 break;
87820}
87821
87822/* Opcode: Next P1 P2 P3 P4 P5
87823**
87824** Advance cursor P1 so that it points to the next key/data pair in its
87825** table or index. If there are no more key/value pairs then fall through
87826** to the following instruction. But if the cursor advance was successful,
87827** jump immediately to P2.
87828**
87829** The Next opcode is only valid following an SeekGT, SeekGE, or
87830** OP_Rewind opcode used to position the cursor. Next is not allowed
87831** to follow SeekLT, SeekLE, or OP_Last.
87832**
87833** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
87834** been opened prior to this opcode or the program will segfault.
87835**
87836** The P3 value is a hint to the btree implementation. If P3==1, that
87837** means P1 is an SQL index and that this instruction could have been
87838** omitted if that index had been unique. P3 is usually 0. P3 is
87839** always either 0 or 1.
87840**
87841** P4 is always of type P4_ADVANCE. The function pointer points to
87842** sqlite3BtreeNext().
87843**
87844** If P5 is positive and the jump is taken, then event counter
87845** number P5-1 in the prepared statement is incremented.
87846**
87847** See also: Prev
87848*/
87849/* Opcode: Prev P1 P2 P3 P4 P5
87850**
87851** Back up cursor P1 so that it points to the previous key/data pair in its
87852** table or index. If there is no previous key/value pairs then fall through
87853** to the following instruction. But if the cursor backup was successful,
87854** jump immediately to P2.
87855**
87856**
87857** The Prev opcode is only valid following an SeekLT, SeekLE, or
87858** OP_Last opcode used to position the cursor. Prev is not allowed
87859** to follow SeekGT, SeekGE, or OP_Rewind.
87860**
87861** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
87862** not open then the behavior is undefined.
87863**
87864** The P3 value is a hint to the btree implementation. If P3==1, that
87865** means P1 is an SQL index and that this instruction could have been
87866** omitted if that index had been unique. P3 is usually 0. P3 is
87867** always either 0 or 1.
87868**
87869** P4 is always of type P4_ADVANCE. The function pointer points to
87870** sqlite3BtreePrevious().
87871**
87872** If P5 is positive and the jump is taken, then event counter
87873** number P5-1 in the prepared statement is incremented.
87874*/
87875/* Opcode: SorterNext P1 P2 * * P5
87876**
87877** This opcode works just like OP_Next except that P1 must be a
87878** sorter object for which the OP_SorterSort opcode has been
87879** invoked. This opcode advances the cursor to the next sorted
87880** record, or jumps to P2 if there are no more sorted records.
87881*/
87882case OP_SorterNext: { /* jump */
87883 VdbeCursor *pC;
87884
87885 pC = p->apCsr[pOp->p1];
87886 assert( isSorter(pC) );
87887 rc = sqlite3VdbeSorterNext(db, pC);
87888 goto next_tail;
87889case OP_Prev: /* jump */
87890case OP_Next: /* jump */
87891 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87892 assert( pOp->p5<ArraySize(p->aCounter) );
87893 pC = p->apCsr[pOp->p1];
87894 assert( pC!=0 );
87895 assert( pC->deferredMoveto==0 );
87896 assert( pC->eCurType==CURTYPE_BTREE );
87897 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
87898 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
87899
87900 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
87901 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
87902 assert( pOp->opcode!=OP_Next
87903 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
87904 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
87905 || pC->seekOp==OP_NullRow);
87906 assert( pOp->opcode!=OP_Prev
87907 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
87908 || pC->seekOp==OP_Last
87909 || pC->seekOp==OP_NullRow);
87910
87911 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
87912next_tail:
87913 pC->cacheStatus = CACHE_STALE;
87914 VdbeBranchTaken(rc==SQLITE_OK,2);
87915 if( rc==SQLITE_OK ){
87916 pC->nullRow = 0;
87917 p->aCounter[pOp->p5]++;
87918#ifdef SQLITE_TEST
87919 sqlite3_search_count++;
87920#endif
87921 goto jump_to_p2_and_check_for_interrupt;
87922 }
87923 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
87924 rc = SQLITE_OK;
87925 pC->nullRow = 1;
87926 goto check_for_interrupt;
87927}
87928
87929/* Opcode: IdxInsert P1 P2 P3 P4 P5
87930** Synopsis: key=r[P2]
87931**
87932** Register P2 holds an SQL index key made using the
87933** MakeRecord instructions. This opcode writes that key
87934** into the index P1. Data for the entry is nil.
87935**
87936** If P4 is not zero, then it is the number of values in the unpacked
87937** key of reg(P2). In that case, P3 is the index of the first register
87938** for the unpacked key. The availability of the unpacked key can sometimes
87939** be an optimization.
87940**
87941** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
87942** that this insert is likely to be an append.
87943**
87944** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
87945** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
87946** then the change counter is unchanged.
87947**
87948** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
87949** run faster by avoiding an unnecessary seek on cursor P1. However,
87950** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
87951** seeks on the cursor or if the most recent seek used a key equivalent
87952** to P2.
87953**
87954** This instruction only works for indices. The equivalent instruction
87955** for tables is OP_Insert.
87956*/
87957/* Opcode: SorterInsert P1 P2 * * *
87958** Synopsis: key=r[P2]
87959**
87960** Register P2 holds an SQL index key made using the
87961** MakeRecord instructions. This opcode writes that key
87962** into the sorter P1. Data for the entry is nil.
87963*/
87964case OP_SorterInsert: /* in2 */
87965case OP_IdxInsert: { /* in2 */
87966 VdbeCursor *pC;
87967 BtreePayload x;
87968
87969 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
87970 pC = p->apCsr[pOp->p1];
87971 sqlite3VdbeIncrWriteCounter(p, pC);
87972 assert( pC!=0 );
87973 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
87974 pIn2 = &aMem[pOp->p2];
87975 assert( pIn2->flags & MEM_Blob );
87976 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
87977 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
87978 assert( pC->isTable==0 );
87979 rc = ExpandBlob(pIn2);
87980 if( rc ) goto abort_due_to_error;
87981 if( pOp->opcode==OP_SorterInsert ){
87982 rc = sqlite3VdbeSorterWrite(pC, pIn2);
87983 }else{
87984 x.nKey = pIn2->n;
87985 x.pKey = pIn2->z;
87986 x.aMem = aMem + pOp->p3;
87987 x.nMem = (u16)pOp->p4.i;
87988 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
87989 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
87990 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
87991 );
87992 assert( pC->deferredMoveto==0 );
87993 pC->cacheStatus = CACHE_STALE;
87994 }
87995 if( rc) goto abort_due_to_error;
87996 break;
87997}
87998
87999/* Opcode: IdxDelete P1 P2 P3 * *
88000** Synopsis: key=r[P2@P3]
88001**
88002** The content of P3 registers starting at register P2 form
88003** an unpacked index key. This opcode removes that entry from the
88004** index opened by cursor P1.
88005*/
88006case OP_IdxDelete: {
88007 VdbeCursor *pC;
88008 BtCursor *pCrsr;
88009 int res;
88010 UnpackedRecord r;
88011
88012 assert( pOp->p3>0 );
88013 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
88014 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88015 pC = p->apCsr[pOp->p1];
88016 assert( pC!=0 );
88017 assert( pC->eCurType==CURTYPE_BTREE );
88018 sqlite3VdbeIncrWriteCounter(p, pC);
88019 pCrsr = pC->uc.pCursor;
88020 assert( pCrsr!=0 );
88021 assert( pOp->p5==0 );
88022 r.pKeyInfo = pC->pKeyInfo;
88023 r.nField = (u16)pOp->p3;
88024 r.default_rc = 0;
88025 r.aMem = &aMem[pOp->p2];
88026 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
88027 if( rc ) goto abort_due_to_error;
88028 if( res==0 ){
88029 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
88030 if( rc ) goto abort_due_to_error;
88031 }
88032 assert( pC->deferredMoveto==0 );
88033 pC->cacheStatus = CACHE_STALE;
88034 pC->seekResult = 0;
88035 break;
88036}
88037
88038/* Opcode: DeferredSeek P1 * P3 P4 *
88039** Synopsis: Move P3 to P1.rowid if needed
88040**
88041** P1 is an open index cursor and P3 is a cursor on the corresponding
88042** table. This opcode does a deferred seek of the P3 table cursor
88043** to the row that corresponds to the current row of P1.
88044**
88045** This is a deferred seek. Nothing actually happens until
88046** the cursor is used to read a record. That way, if no reads
88047** occur, no unnecessary I/O happens.
88048**
88049** P4 may be an array of integers (type P4_INTARRAY) containing
88050** one entry for each column in the P3 table. If array entry a(i)
88051** is non-zero, then reading column a(i)-1 from cursor P3 is
88052** equivalent to performing the deferred seek and then reading column i
88053** from P1. This information is stored in P3 and used to redirect
88054** reads against P3 over to P1, thus possibly avoiding the need to
88055** seek and read cursor P3.
88056*/
88057/* Opcode: IdxRowid P1 P2 * * *
88058** Synopsis: r[P2]=rowid
88059**
88060** Write into register P2 an integer which is the last entry in the record at
88061** the end of the index key pointed to by cursor P1. This integer should be
88062** the rowid of the table entry to which this index entry points.
88063**
88064** See also: Rowid, MakeRecord.
88065*/
88066case OP_DeferredSeek:
88067case OP_IdxRowid: { /* out2 */
88068 VdbeCursor *pC; /* The P1 index cursor */
88069 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
88070 i64 rowid; /* Rowid that P1 current points to */
88071
88072 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88073 pC = p->apCsr[pOp->p1];
88074 assert( pC!=0 );
88075 assert( pC->eCurType==CURTYPE_BTREE );
88076 assert( pC->uc.pCursor!=0 );
88077 assert( pC->isTable==0 );
88078 assert( pC->deferredMoveto==0 );
88079 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
88080
88081 /* The IdxRowid and Seek opcodes are combined because of the commonality
88082 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
88083 rc = sqlite3VdbeCursorRestore(pC);
88084
88085 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
88086 ** out from under the cursor. That will never happens for an IdxRowid
88087 ** or Seek opcode */
88088 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
88089
88090 if( !pC->nullRow ){
88091 rowid = 0; /* Not needed. Only used to silence a warning. */
88092 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
88093 if( rc!=SQLITE_OK ){
88094 goto abort_due_to_error;
88095 }
88096 if( pOp->opcode==OP_DeferredSeek ){
88097 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
88098 pTabCur = p->apCsr[pOp->p3];
88099 assert( pTabCur!=0 );
88100 assert( pTabCur->eCurType==CURTYPE_BTREE );
88101 assert( pTabCur->uc.pCursor!=0 );
88102 assert( pTabCur->isTable );
88103 pTabCur->nullRow = 0;
88104 pTabCur->movetoTarget = rowid;
88105 pTabCur->deferredMoveto = 1;
88106 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
88107 pTabCur->aAltMap = pOp->p4.ai;
88108 pTabCur->pAltCursor = pC;
88109 }else{
88110 pOut = out2Prerelease(p, pOp);
88111 pOut->u.i = rowid;
88112 }
88113 }else{
88114 assert( pOp->opcode==OP_IdxRowid );
88115 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
88116 }
88117 break;
88118}
88119
88120/* Opcode: IdxGE P1 P2 P3 P4 P5
88121** Synopsis: key=r[P3@P4]
88122**
88123** The P4 register values beginning with P3 form an unpacked index
88124** key that omits the PRIMARY KEY. Compare this key value against the index
88125** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
88126** fields at the end.
88127**
88128** If the P1 index entry is greater than or equal to the key value
88129** then jump to P2. Otherwise fall through to the next instruction.
88130*/
88131/* Opcode: IdxGT P1 P2 P3 P4 P5
88132** Synopsis: key=r[P3@P4]
88133**
88134** The P4 register values beginning with P3 form an unpacked index
88135** key that omits the PRIMARY KEY. Compare this key value against the index
88136** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
88137** fields at the end.
88138**
88139** If the P1 index entry is greater than the key value
88140** then jump to P2. Otherwise fall through to the next instruction.
88141*/
88142/* Opcode: IdxLT P1 P2 P3 P4 P5
88143** Synopsis: key=r[P3@P4]
88144**
88145** The P4 register values beginning with P3 form an unpacked index
88146** key that omits the PRIMARY KEY or ROWID. Compare this key value against
88147** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
88148** ROWID on the P1 index.
88149**
88150** If the P1 index entry is less than the key value then jump to P2.
88151** Otherwise fall through to the next instruction.
88152*/
88153/* Opcode: IdxLE P1 P2 P3 P4 P5
88154** Synopsis: key=r[P3@P4]
88155**
88156** The P4 register values beginning with P3 form an unpacked index
88157** key that omits the PRIMARY KEY or ROWID. Compare this key value against
88158** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
88159** ROWID on the P1 index.
88160**
88161** If the P1 index entry is less than or equal to the key value then jump
88162** to P2. Otherwise fall through to the next instruction.
88163*/
88164case OP_IdxLE: /* jump */
88165case OP_IdxGT: /* jump */
88166case OP_IdxLT: /* jump */
88167case OP_IdxGE: { /* jump */
88168 VdbeCursor *pC;
88169 int res;
88170 UnpackedRecord r;
88171
88172 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88173 pC = p->apCsr[pOp->p1];
88174 assert( pC!=0 );
88175 assert( pC->isOrdered );
88176 assert( pC->eCurType==CURTYPE_BTREE );
88177 assert( pC->uc.pCursor!=0);
88178 assert( pC->deferredMoveto==0 );
88179 assert( pOp->p5==0 || pOp->p5==1 );
88180 assert( pOp->p4type==P4_INT32 );
88181 r.pKeyInfo = pC->pKeyInfo;
88182 r.nField = (u16)pOp->p4.i;
88183 if( pOp->opcode<OP_IdxLT ){
88184 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
88185 r.default_rc = -1;
88186 }else{
88187 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
88188 r.default_rc = 0;
88189 }
88190 r.aMem = &aMem[pOp->p3];
88191#ifdef SQLITE_DEBUG
88192 {
88193 int i;
88194 for(i=0; i<r.nField; i++){
88195 assert( memIsValid(&r.aMem[i]) );
88196 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
88197 }
88198 }
88199#endif
88200 res = 0; /* Not needed. Only used to silence a warning. */
88201 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
88202 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
88203 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
88204 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
88205 res = -res;
88206 }else{
88207 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
88208 res++;
88209 }
88210 VdbeBranchTaken(res>0,2);
88211 if( rc ) goto abort_due_to_error;
88212 if( res>0 ) goto jump_to_p2;
88213 break;
88214}
88215
88216/* Opcode: Destroy P1 P2 P3 * *
88217**
88218** Delete an entire database table or index whose root page in the database
88219** file is given by P1.
88220**
88221** The table being destroyed is in the main database file if P3==0. If
88222** P3==1 then the table to be clear is in the auxiliary database file
88223** that is used to store tables create using CREATE TEMPORARY TABLE.
88224**
88225** If AUTOVACUUM is enabled then it is possible that another root page
88226** might be moved into the newly deleted root page in order to keep all
88227** root pages contiguous at the beginning of the database. The former
88228** value of the root page that moved - its value before the move occurred -
88229** is stored in register P2. If no page movement was required (because the
88230** table being dropped was already the last one in the database) then a
88231** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
88232** is stored in register P2.
88233**
88234** This opcode throws an error if there are any active reader VMs when
88235** it is invoked. This is done to avoid the difficulty associated with
88236** updating existing cursors when a root page is moved in an AUTOVACUUM
88237** database. This error is thrown even if the database is not an AUTOVACUUM
88238** db in order to avoid introducing an incompatibility between autovacuum
88239** and non-autovacuum modes.
88240**
88241** See also: Clear
88242*/
88243case OP_Destroy: { /* out2 */
88244 int iMoved;
88245 int iDb;
88246
88247 sqlite3VdbeIncrWriteCounter(p, 0);
88248 assert( p->readOnly==0 );
88249 assert( pOp->p1>1 );
88250 pOut = out2Prerelease(p, pOp);
88251 pOut->flags = MEM_Null;
88252 if( db->nVdbeRead > db->nVDestroy+1 ){
88253 rc = SQLITE_LOCKED;
88254 p->errorAction = OE_Abort;
88255 goto abort_due_to_error;
88256 }else{
88257 iDb = pOp->p3;
88258 assert( DbMaskTest(p->btreeMask, iDb) );
88259 iMoved = 0; /* Not needed. Only to silence a warning. */
88260 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
88261 pOut->flags = MEM_Int;
88262 pOut->u.i = iMoved;
88263 if( rc ) goto abort_due_to_error;
88264#ifndef SQLITE_OMIT_AUTOVACUUM
88265 if( iMoved!=0 ){
88266 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
88267 /* All OP_Destroy operations occur on the same btree */
88268 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
88269 resetSchemaOnFault = iDb+1;
88270 }
88271#endif
88272 }
88273 break;
88274}
88275
88276/* Opcode: Clear P1 P2 P3
88277**
88278** Delete all contents of the database table or index whose root page
88279** in the database file is given by P1. But, unlike Destroy, do not
88280** remove the table or index from the database file.
88281**
88282** The table being clear is in the main database file if P2==0. If
88283** P2==1 then the table to be clear is in the auxiliary database file
88284** that is used to store tables create using CREATE TEMPORARY TABLE.
88285**
88286** If the P3 value is non-zero, then the table referred to must be an
88287** intkey table (an SQL table, not an index). In this case the row change
88288** count is incremented by the number of rows in the table being cleared.
88289** If P3 is greater than zero, then the value stored in register P3 is
88290** also incremented by the number of rows in the table being cleared.
88291**
88292** See also: Destroy
88293*/
88294case OP_Clear: {
88295 int nChange;
88296
88297 sqlite3VdbeIncrWriteCounter(p, 0);
88298 nChange = 0;
88299 assert( p->readOnly==0 );
88300 assert( DbMaskTest(p->btreeMask, pOp->p2) );
88301 rc = sqlite3BtreeClearTable(
88302 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
88303 );
88304 if( pOp->p3 ){
88305 p->nChange += nChange;
88306 if( pOp->p3>0 ){
88307 assert( memIsValid(&aMem[pOp->p3]) );
88308 memAboutToChange(p, &aMem[pOp->p3]);
88309 aMem[pOp->p3].u.i += nChange;
88310 }
88311 }
88312 if( rc ) goto abort_due_to_error;
88313 break;
88314}
88315
88316/* Opcode: ResetSorter P1 * * * *
88317**
88318** Delete all contents from the ephemeral table or sorter
88319** that is open on cursor P1.
88320**
88321** This opcode only works for cursors used for sorting and
88322** opened with OP_OpenEphemeral or OP_SorterOpen.
88323*/
88324case OP_ResetSorter: {
88325 VdbeCursor *pC;
88326
88327 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88328 pC = p->apCsr[pOp->p1];
88329 assert( pC!=0 );
88330 if( isSorter(pC) ){
88331 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
88332 }else{
88333 assert( pC->eCurType==CURTYPE_BTREE );
88334 assert( pC->isEphemeral );
88335 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
88336 if( rc ) goto abort_due_to_error;
88337 }
88338 break;
88339}
88340
88341/* Opcode: CreateBtree P1 P2 P3 * *
88342** Synopsis: r[P2]=root iDb=P1 flags=P3
88343**
88344** Allocate a new b-tree in the main database file if P1==0 or in the
88345** TEMP database file if P1==1 or in an attached database if
88346** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
88347** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
88348** The root page number of the new b-tree is stored in register P2.
88349*/
88350case OP_CreateBtree: { /* out2 */
88351 int pgno;
88352 Db *pDb;
88353
88354 sqlite3VdbeIncrWriteCounter(p, 0);
88355 pOut = out2Prerelease(p, pOp);
88356 pgno = 0;
88357 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
88358 assert( pOp->p1>=0 && pOp->p1<db->nDb );
88359 assert( DbMaskTest(p->btreeMask, pOp->p1) );
88360 assert( p->readOnly==0 );
88361 pDb = &db->aDb[pOp->p1];
88362 assert( pDb->pBt!=0 );
88363 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
88364 if( rc ) goto abort_due_to_error;
88365 pOut->u.i = pgno;
88366 break;
88367}
88368
88369/* Opcode: SqlExec * * * P4 *
88370**
88371** Run the SQL statement or statements specified in the P4 string.
88372*/
88373case OP_SqlExec: {
88374 sqlite3VdbeIncrWriteCounter(p, 0);
88375 db->nSqlExec++;
88376 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
88377 db->nSqlExec--;
88378 if( rc ) goto abort_due_to_error;
88379 break;
88380}
88381
88382/* Opcode: ParseSchema P1 * * P4 *
88383**
88384** Read and parse all entries from the SQLITE_MASTER table of database P1
88385** that match the WHERE clause P4. If P4 is a NULL pointer, then the
88386** entire schema for P1 is reparsed.
88387**
88388** This opcode invokes the parser to create a new virtual machine,
88389** then runs the new virtual machine. It is thus a re-entrant opcode.
88390*/
88391case OP_ParseSchema: {
88392 int iDb;
88393 const char *zMaster;
88394 char *zSql;
88395 InitData initData;
88396
88397 /* Any prepared statement that invokes this opcode will hold mutexes
88398 ** on every btree. This is a prerequisite for invoking
88399 ** sqlite3InitCallback().
88400 */
88401#ifdef SQLITE_DEBUG
88402 for(iDb=0; iDb<db->nDb; iDb++){
88403 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
88404 }
88405#endif
88406
88407 iDb = pOp->p1;
88408 assert( iDb>=0 && iDb<db->nDb );
88409 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
88410
88411#ifndef SQLITE_OMIT_ALTERTABLE
88412 if( pOp->p4.z==0 ){
88413 sqlite3SchemaClear(db->aDb[iDb].pSchema);
88414 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
88415 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, INITFLAG_AlterTable);
88416 db->mDbFlags |= DBFLAG_SchemaChange;
88417 p->expired = 0;
88418 }else
88419#endif
88420 {
88421 zMaster = MASTER_NAME;
88422 initData.db = db;
88423 initData.iDb = pOp->p1;
88424 initData.pzErrMsg = &p->zErrMsg;
88425 initData.mInitFlags = 0;
88426 zSql = sqlite3MPrintf(db,
88427 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
88428 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
88429 if( zSql==0 ){
88430 rc = SQLITE_NOMEM_BKPT;
88431 }else{
88432 assert( db->init.busy==0 );
88433 db->init.busy = 1;
88434 initData.rc = SQLITE_OK;
88435 assert( !db->mallocFailed );
88436 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
88437 if( rc==SQLITE_OK ) rc = initData.rc;
88438 sqlite3DbFreeNN(db, zSql);
88439 db->init.busy = 0;
88440 }
88441 }
88442 if( rc ){
88443 sqlite3ResetAllSchemasOfConnection(db);
88444 if( rc==SQLITE_NOMEM ){
88445 goto no_mem;
88446 }
88447 goto abort_due_to_error;
88448 }
88449 break;
88450}
88451
88452#if !defined(SQLITE_OMIT_ANALYZE)
88453/* Opcode: LoadAnalysis P1 * * * *
88454**
88455** Read the sqlite_stat1 table for database P1 and load the content
88456** of that table into the internal index hash table. This will cause
88457** the analysis to be used when preparing all subsequent queries.
88458*/
88459case OP_LoadAnalysis: {
88460 assert( pOp->p1>=0 && pOp->p1<db->nDb );
88461 rc = sqlite3AnalysisLoad(db, pOp->p1);
88462 if( rc ) goto abort_due_to_error;
88463 break;
88464}
88465#endif /* !defined(SQLITE_OMIT_ANALYZE) */
88466
88467/* Opcode: DropTable P1 * * P4 *
88468**
88469** Remove the internal (in-memory) data structures that describe
88470** the table named P4 in database P1. This is called after a table
88471** is dropped from disk (using the Destroy opcode) in order to keep
88472** the internal representation of the
88473** schema consistent with what is on disk.
88474*/
88475case OP_DropTable: {
88476 sqlite3VdbeIncrWriteCounter(p, 0);
88477 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
88478 break;
88479}
88480
88481/* Opcode: DropIndex P1 * * P4 *
88482**
88483** Remove the internal (in-memory) data structures that describe
88484** the index named P4 in database P1. This is called after an index
88485** is dropped from disk (using the Destroy opcode)
88486** in order to keep the internal representation of the
88487** schema consistent with what is on disk.
88488*/
88489case OP_DropIndex: {
88490 sqlite3VdbeIncrWriteCounter(p, 0);
88491 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
88492 break;
88493}
88494
88495/* Opcode: DropTrigger P1 * * P4 *
88496**
88497** Remove the internal (in-memory) data structures that describe
88498** the trigger named P4 in database P1. This is called after a trigger
88499** is dropped from disk (using the Destroy opcode) in order to keep
88500** the internal representation of the
88501** schema consistent with what is on disk.
88502*/
88503case OP_DropTrigger: {
88504 sqlite3VdbeIncrWriteCounter(p, 0);
88505 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
88506 break;
88507}
88508
88509
88510#ifndef SQLITE_OMIT_INTEGRITY_CHECK
88511/* Opcode: IntegrityCk P1 P2 P3 P4 P5
88512**
88513** Do an analysis of the currently open database. Store in
88514** register P1 the text of an error message describing any problems.
88515** If no problems are found, store a NULL in register P1.
88516**
88517** The register P3 contains one less than the maximum number of allowed errors.
88518** At most reg(P3) errors will be reported.
88519** In other words, the analysis stops as soon as reg(P1) errors are
88520** seen. Reg(P1) is updated with the number of errors remaining.
88521**
88522** The root page numbers of all tables in the database are integers
88523** stored in P4_INTARRAY argument.
88524**
88525** If P5 is not zero, the check is done on the auxiliary database
88526** file, not the main database file.
88527**
88528** This opcode is used to implement the integrity_check pragma.
88529*/
88530case OP_IntegrityCk: {
88531 int nRoot; /* Number of tables to check. (Number of root pages.) */
88532 int *aRoot; /* Array of rootpage numbers for tables to be checked */
88533 int nErr; /* Number of errors reported */
88534 char *z; /* Text of the error report */
88535 Mem *pnErr; /* Register keeping track of errors remaining */
88536
88537 assert( p->bIsReader );
88538 nRoot = pOp->p2;
88539 aRoot = pOp->p4.ai;
88540 assert( nRoot>0 );
88541 assert( aRoot[0]==nRoot );
88542 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
88543 pnErr = &aMem[pOp->p3];
88544 assert( (pnErr->flags & MEM_Int)!=0 );
88545 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
88546 pIn1 = &aMem[pOp->p1];
88547 assert( pOp->p5<db->nDb );
88548 assert( DbMaskTest(p->btreeMask, pOp->p5) );
88549 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
88550 (int)pnErr->u.i+1, &nErr);
88551 sqlite3VdbeMemSetNull(pIn1);
88552 if( nErr==0 ){
88553 assert( z==0 );
88554 }else if( z==0 ){
88555 goto no_mem;
88556 }else{
88557 pnErr->u.i -= nErr-1;
88558 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
88559 }
88560 UPDATE_MAX_BLOBSIZE(pIn1);
88561 sqlite3VdbeChangeEncoding(pIn1, encoding);
88562 break;
88563}
88564#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
88565
88566/* Opcode: RowSetAdd P1 P2 * * *
88567** Synopsis: rowset(P1)=r[P2]
88568**
88569** Insert the integer value held by register P2 into a RowSet object
88570** held in register P1.
88571**
88572** An assertion fails if P2 is not an integer.
88573*/
88574case OP_RowSetAdd: { /* in1, in2 */
88575 pIn1 = &aMem[pOp->p1];
88576 pIn2 = &aMem[pOp->p2];
88577 assert( (pIn2->flags & MEM_Int)!=0 );
88578 if( (pIn1->flags & MEM_Blob)==0 ){
88579 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
88580 }
88581 assert( sqlite3VdbeMemIsRowSet(pIn1) );
88582 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
88583 break;
88584}
88585
88586/* Opcode: RowSetRead P1 P2 P3 * *
88587** Synopsis: r[P3]=rowset(P1)
88588**
88589** Extract the smallest value from the RowSet object in P1
88590** and put that value into register P3.
88591** Or, if RowSet object P1 is initially empty, leave P3
88592** unchanged and jump to instruction P2.
88593*/
88594case OP_RowSetRead: { /* jump, in1, out3 */
88595 i64 val;
88596
88597 pIn1 = &aMem[pOp->p1];
88598 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
88599 if( (pIn1->flags & MEM_Blob)==0
88600 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
88601 ){
88602 /* The boolean index is empty */
88603 sqlite3VdbeMemSetNull(pIn1);
88604 VdbeBranchTaken(1,2);
88605 goto jump_to_p2_and_check_for_interrupt;
88606 }else{
88607 /* A value was pulled from the index */
88608 VdbeBranchTaken(0,2);
88609 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
88610 }
88611 goto check_for_interrupt;
88612}
88613
88614/* Opcode: RowSetTest P1 P2 P3 P4
88615** Synopsis: if r[P3] in rowset(P1) goto P2
88616**
88617** Register P3 is assumed to hold a 64-bit integer value. If register P1
88618** contains a RowSet object and that RowSet object contains
88619** the value held in P3, jump to register P2. Otherwise, insert the
88620** integer in P3 into the RowSet and continue on to the
88621** next opcode.
88622**
88623** The RowSet object is optimized for the case where sets of integers
88624** are inserted in distinct phases, which each set contains no duplicates.
88625** Each set is identified by a unique P4 value. The first set
88626** must have P4==0, the final set must have P4==-1, and for all other sets
88627** must have P4>0.
88628**
88629** This allows optimizations: (a) when P4==0 there is no need to test
88630** the RowSet object for P3, as it is guaranteed not to contain it,
88631** (b) when P4==-1 there is no need to insert the value, as it will
88632** never be tested for, and (c) when a value that is part of set X is
88633** inserted, there is no need to search to see if the same value was
88634** previously inserted as part of set X (only if it was previously
88635** inserted as part of some other set).
88636*/
88637case OP_RowSetTest: { /* jump, in1, in3 */
88638 int iSet;
88639 int exists;
88640
88641 pIn1 = &aMem[pOp->p1];
88642 pIn3 = &aMem[pOp->p3];
88643 iSet = pOp->p4.i;
88644 assert( pIn3->flags&MEM_Int );
88645
88646 /* If there is anything other than a rowset object in memory cell P1,
88647 ** delete it now and initialize P1 with an empty rowset
88648 */
88649 if( (pIn1->flags & MEM_Blob)==0 ){
88650 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
88651 }
88652 assert( sqlite3VdbeMemIsRowSet(pIn1) );
88653 assert( pOp->p4type==P4_INT32 );
88654 assert( iSet==-1 || iSet>=0 );
88655 if( iSet ){
88656 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
88657 VdbeBranchTaken(exists!=0,2);
88658 if( exists ) goto jump_to_p2;
88659 }
88660 if( iSet>=0 ){
88661 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
88662 }
88663 break;
88664}
88665
88666
88667#ifndef SQLITE_OMIT_TRIGGER
88668
88669/* Opcode: Program P1 P2 P3 P4 P5
88670**
88671** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
88672**
88673** P1 contains the address of the memory cell that contains the first memory
88674** cell in an array of values used as arguments to the sub-program. P2
88675** contains the address to jump to if the sub-program throws an IGNORE
88676** exception using the RAISE() function. Register P3 contains the address
88677** of a memory cell in this (the parent) VM that is used to allocate the
88678** memory required by the sub-vdbe at runtime.
88679**
88680** P4 is a pointer to the VM containing the trigger program.
88681**
88682** If P5 is non-zero, then recursive program invocation is enabled.
88683*/
88684case OP_Program: { /* jump */
88685 int nMem; /* Number of memory registers for sub-program */
88686 int nByte; /* Bytes of runtime space required for sub-program */
88687 Mem *pRt; /* Register to allocate runtime space */
88688 Mem *pMem; /* Used to iterate through memory cells */
88689 Mem *pEnd; /* Last memory cell in new array */
88690 VdbeFrame *pFrame; /* New vdbe frame to execute in */
88691 SubProgram *pProgram; /* Sub-program to execute */
88692 void *t; /* Token identifying trigger */
88693
88694 pProgram = pOp->p4.pProgram;
88695 pRt = &aMem[pOp->p3];
88696 assert( pProgram->nOp>0 );
88697
88698 /* If the p5 flag is clear, then recursive invocation of triggers is
88699 ** disabled for backwards compatibility (p5 is set if this sub-program
88700 ** is really a trigger, not a foreign key action, and the flag set
88701 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
88702 **
88703 ** It is recursive invocation of triggers, at the SQL level, that is
88704 ** disabled. In some cases a single trigger may generate more than one
88705 ** SubProgram (if the trigger may be executed with more than one different
88706 ** ON CONFLICT algorithm). SubProgram structures associated with a
88707 ** single trigger all have the same value for the SubProgram.token
88708 ** variable. */
88709 if( pOp->p5 ){
88710 t = pProgram->token;
88711 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
88712 if( pFrame ) break;
88713 }
88714
88715 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
88716 rc = SQLITE_ERROR;
88717 sqlite3VdbeError(p, "too many levels of trigger recursion");
88718 goto abort_due_to_error;
88719 }
88720
88721 /* Register pRt is used to store the memory required to save the state
88722 ** of the current program, and the memory required at runtime to execute
88723 ** the trigger program. If this trigger has been fired before, then pRt
88724 ** is already allocated. Otherwise, it must be initialized. */
88725 if( (pRt->flags&MEM_Blob)==0 ){
88726 /* SubProgram.nMem is set to the number of memory cells used by the
88727 ** program stored in SubProgram.aOp. As well as these, one memory
88728 ** cell is required for each cursor used by the program. Set local
88729 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
88730 */
88731 nMem = pProgram->nMem + pProgram->nCsr;
88732 assert( nMem>0 );
88733 if( pProgram->nCsr==0 ) nMem++;
88734 nByte = ROUND8(sizeof(VdbeFrame))
88735 + nMem * sizeof(Mem)
88736 + pProgram->nCsr * sizeof(VdbeCursor*)
88737 + (pProgram->nOp + 7)/8;
88738 pFrame = sqlite3DbMallocZero(db, nByte);
88739 if( !pFrame ){
88740 goto no_mem;
88741 }
88742 sqlite3VdbeMemRelease(pRt);
88743 pRt->flags = MEM_Blob|MEM_Dyn;
88744 pRt->z = (char*)pFrame;
88745 pRt->n = nByte;
88746 pRt->xDel = sqlite3VdbeFrameMemDel;
88747
88748 pFrame->v = p;
88749 pFrame->nChildMem = nMem;
88750 pFrame->nChildCsr = pProgram->nCsr;
88751 pFrame->pc = (int)(pOp - aOp);
88752 pFrame->aMem = p->aMem;
88753 pFrame->nMem = p->nMem;
88754 pFrame->apCsr = p->apCsr;
88755 pFrame->nCursor = p->nCursor;
88756 pFrame->aOp = p->aOp;
88757 pFrame->nOp = p->nOp;
88758 pFrame->token = pProgram->token;
88759#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
88760 pFrame->anExec = p->anExec;
88761#endif
88762#ifdef SQLITE_DEBUG
88763 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
88764#endif
88765
88766 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
88767 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
88768 pMem->flags = MEM_Undefined;
88769 pMem->db = db;
88770 }
88771 }else{
88772 pFrame = (VdbeFrame*)pRt->z;
88773 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
88774 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
88775 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
88776 assert( pProgram->nCsr==pFrame->nChildCsr );
88777 assert( (int)(pOp - aOp)==pFrame->pc );
88778 }
88779
88780 p->nFrame++;
88781 pFrame->pParent = p->pFrame;
88782 pFrame->lastRowid = db->lastRowid;
88783 pFrame->nChange = p->nChange;
88784 pFrame->nDbChange = p->db->nChange;
88785 assert( pFrame->pAuxData==0 );
88786 pFrame->pAuxData = p->pAuxData;
88787 p->pAuxData = 0;
88788 p->nChange = 0;
88789 p->pFrame = pFrame;
88790 p->aMem = aMem = VdbeFrameMem(pFrame);
88791 p->nMem = pFrame->nChildMem;
88792 p->nCursor = (u16)pFrame->nChildCsr;
88793 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
88794 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
88795 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
88796 p->aOp = aOp = pProgram->aOp;
88797 p->nOp = pProgram->nOp;
88798#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
88799 p->anExec = 0;
88800#endif
88801 pOp = &aOp[-1];
88802
88803 break;
88804}
88805
88806/* Opcode: Param P1 P2 * * *
88807**
88808** This opcode is only ever present in sub-programs called via the
88809** OP_Program instruction. Copy a value currently stored in a memory
88810** cell of the calling (parent) frame to cell P2 in the current frames
88811** address space. This is used by trigger programs to access the new.*
88812** and old.* values.
88813**
88814** The address of the cell in the parent frame is determined by adding
88815** the value of the P1 argument to the value of the P1 argument to the
88816** calling OP_Program instruction.
88817*/
88818case OP_Param: { /* out2 */
88819 VdbeFrame *pFrame;
88820 Mem *pIn;
88821 pOut = out2Prerelease(p, pOp);
88822 pFrame = p->pFrame;
88823 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
88824 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
88825 break;
88826}
88827
88828#endif /* #ifndef SQLITE_OMIT_TRIGGER */
88829
88830#ifndef SQLITE_OMIT_FOREIGN_KEY
88831/* Opcode: FkCounter P1 P2 * * *
88832** Synopsis: fkctr[P1]+=P2
88833**
88834** Increment a "constraint counter" by P2 (P2 may be negative or positive).
88835** If P1 is non-zero, the database constraint counter is incremented
88836** (deferred foreign key constraints). Otherwise, if P1 is zero, the
88837** statement counter is incremented (immediate foreign key constraints).
88838*/
88839case OP_FkCounter: {
88840 if( db->flags & SQLITE_DeferFKs ){
88841 db->nDeferredImmCons += pOp->p2;
88842 }else if( pOp->p1 ){
88843 db->nDeferredCons += pOp->p2;
88844 }else{
88845 p->nFkConstraint += pOp->p2;
88846 }
88847 break;
88848}
88849
88850/* Opcode: FkIfZero P1 P2 * * *
88851** Synopsis: if fkctr[P1]==0 goto P2
88852**
88853** This opcode tests if a foreign key constraint-counter is currently zero.
88854** If so, jump to instruction P2. Otherwise, fall through to the next
88855** instruction.
88856**
88857** If P1 is non-zero, then the jump is taken if the database constraint-counter
88858** is zero (the one that counts deferred constraint violations). If P1 is
88859** zero, the jump is taken if the statement constraint-counter is zero
88860** (immediate foreign key constraint violations).
88861*/
88862case OP_FkIfZero: { /* jump */
88863 if( pOp->p1 ){
88864 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
88865 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
88866 }else{
88867 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
88868 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
88869 }
88870 break;
88871}
88872#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
88873
88874#ifndef SQLITE_OMIT_AUTOINCREMENT
88875/* Opcode: MemMax P1 P2 * * *
88876** Synopsis: r[P1]=max(r[P1],r[P2])
88877**
88878** P1 is a register in the root frame of this VM (the root frame is
88879** different from the current frame if this instruction is being executed
88880** within a sub-program). Set the value of register P1 to the maximum of
88881** its current value and the value in register P2.
88882**
88883** This instruction throws an error if the memory cell is not initially
88884** an integer.
88885*/
88886case OP_MemMax: { /* in2 */
88887 VdbeFrame *pFrame;
88888 if( p->pFrame ){
88889 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
88890 pIn1 = &pFrame->aMem[pOp->p1];
88891 }else{
88892 pIn1 = &aMem[pOp->p1];
88893 }
88894 assert( memIsValid(pIn1) );
88895 sqlite3VdbeMemIntegerify(pIn1);
88896 pIn2 = &aMem[pOp->p2];
88897 sqlite3VdbeMemIntegerify(pIn2);
88898 if( pIn1->u.i<pIn2->u.i){
88899 pIn1->u.i = pIn2->u.i;
88900 }
88901 break;
88902}
88903#endif /* SQLITE_OMIT_AUTOINCREMENT */
88904
88905/* Opcode: IfPos P1 P2 P3 * *
88906** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
88907**
88908** Register P1 must contain an integer.
88909** If the value of register P1 is 1 or greater, subtract P3 from the
88910** value in P1 and jump to P2.
88911**
88912** If the initial value of register P1 is less than 1, then the
88913** value is unchanged and control passes through to the next instruction.
88914*/
88915case OP_IfPos: { /* jump, in1 */
88916 pIn1 = &aMem[pOp->p1];
88917 assert( pIn1->flags&MEM_Int );
88918 VdbeBranchTaken( pIn1->u.i>0, 2);
88919 if( pIn1->u.i>0 ){
88920 pIn1->u.i -= pOp->p3;
88921 goto jump_to_p2;
88922 }
88923 break;
88924}
88925
88926/* Opcode: OffsetLimit P1 P2 P3 * *
88927** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
88928**
88929** This opcode performs a commonly used computation associated with
88930** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
88931** holds the offset counter. The opcode computes the combined value
88932** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
88933** value computed is the total number of rows that will need to be
88934** visited in order to complete the query.
88935**
88936** If r[P3] is zero or negative, that means there is no OFFSET
88937** and r[P2] is set to be the value of the LIMIT, r[P1].
88938**
88939** if r[P1] is zero or negative, that means there is no LIMIT
88940** and r[P2] is set to -1.
88941**
88942** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
88943*/
88944case OP_OffsetLimit: { /* in1, out2, in3 */
88945 i64 x;
88946 pIn1 = &aMem[pOp->p1];
88947 pIn3 = &aMem[pOp->p3];
88948 pOut = out2Prerelease(p, pOp);
88949 assert( pIn1->flags & MEM_Int );
88950 assert( pIn3->flags & MEM_Int );
88951 x = pIn1->u.i;
88952 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
88953 /* If the LIMIT is less than or equal to zero, loop forever. This
88954 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
88955 ** also loop forever. This is undocumented. In fact, one could argue
88956 ** that the loop should terminate. But assuming 1 billion iterations
88957 ** per second (far exceeding the capabilities of any current hardware)
88958 ** it would take nearly 300 years to actually reach the limit. So
88959 ** looping forever is a reasonable approximation. */
88960 pOut->u.i = -1;
88961 }else{
88962 pOut->u.i = x;
88963 }
88964 break;
88965}
88966
88967/* Opcode: IfNotZero P1 P2 * * *
88968** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
88969**
88970** Register P1 must contain an integer. If the content of register P1 is
88971** initially greater than zero, then decrement the value in register P1.
88972** If it is non-zero (negative or positive) and then also jump to P2.
88973** If register P1 is initially zero, leave it unchanged and fall through.
88974*/
88975case OP_IfNotZero: { /* jump, in1 */
88976 pIn1 = &aMem[pOp->p1];
88977 assert( pIn1->flags&MEM_Int );
88978 VdbeBranchTaken(pIn1->u.i<0, 2);
88979 if( pIn1->u.i ){
88980 if( pIn1->u.i>0 ) pIn1->u.i--;
88981 goto jump_to_p2;
88982 }
88983 break;
88984}
88985
88986/* Opcode: DecrJumpZero P1 P2 * * *
88987** Synopsis: if (--r[P1])==0 goto P2
88988**
88989** Register P1 must hold an integer. Decrement the value in P1
88990** and jump to P2 if the new value is exactly zero.
88991*/
88992case OP_DecrJumpZero: { /* jump, in1 */
88993 pIn1 = &aMem[pOp->p1];
88994 assert( pIn1->flags&MEM_Int );
88995 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
88996 VdbeBranchTaken(pIn1->u.i==0, 2);
88997 if( pIn1->u.i==0 ) goto jump_to_p2;
88998 break;
88999}
89000
89001
89002/* Opcode: AggStep * P2 P3 P4 P5
89003** Synopsis: accum=r[P3] step(r[P2@P5])
89004**
89005** Execute the xStep function for an aggregate.
89006** The function has P5 arguments. P4 is a pointer to the
89007** FuncDef structure that specifies the function. Register P3 is the
89008** accumulator.
89009**
89010** The P5 arguments are taken from register P2 and its
89011** successors.
89012*/
89013/* Opcode: AggInverse * P2 P3 P4 P5
89014** Synopsis: accum=r[P3] inverse(r[P2@P5])
89015**
89016** Execute the xInverse function for an aggregate.
89017** The function has P5 arguments. P4 is a pointer to the
89018** FuncDef structure that specifies the function. Register P3 is the
89019** accumulator.
89020**
89021** The P5 arguments are taken from register P2 and its
89022** successors.
89023*/
89024/* Opcode: AggStep1 P1 P2 P3 P4 P5
89025** Synopsis: accum=r[P3] step(r[P2@P5])
89026**
89027** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
89028** aggregate. The function has P5 arguments. P4 is a pointer to the
89029** FuncDef structure that specifies the function. Register P3 is the
89030** accumulator.
89031**
89032** The P5 arguments are taken from register P2 and its
89033** successors.
89034**
89035** This opcode is initially coded as OP_AggStep0. On first evaluation,
89036** the FuncDef stored in P4 is converted into an sqlite3_context and
89037** the opcode is changed. In this way, the initialization of the
89038** sqlite3_context only happens once, instead of on each call to the
89039** step function.
89040*/
89041case OP_AggInverse:
89042case OP_AggStep: {
89043 int n;
89044 sqlite3_context *pCtx;
89045
89046 assert( pOp->p4type==P4_FUNCDEF );
89047 n = pOp->p5;
89048 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
89049 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
89050 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
89051 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
89052 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
89053 if( pCtx==0 ) goto no_mem;
89054 pCtx->pMem = 0;
89055 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
89056 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
89057 pCtx->pFunc = pOp->p4.pFunc;
89058 pCtx->iOp = (int)(pOp - aOp);
89059 pCtx->pVdbe = p;
89060 pCtx->skipFlag = 0;
89061 pCtx->isError = 0;
89062 pCtx->argc = n;
89063 pOp->p4type = P4_FUNCCTX;
89064 pOp->p4.pCtx = pCtx;
89065
89066 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
89067 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
89068
89069 pOp->opcode = OP_AggStep1;
89070 /* Fall through into OP_AggStep */
89071}
89072case OP_AggStep1: {
89073 int i;
89074 sqlite3_context *pCtx;
89075 Mem *pMem;
89076
89077 assert( pOp->p4type==P4_FUNCCTX );
89078 pCtx = pOp->p4.pCtx;
89079 pMem = &aMem[pOp->p3];
89080
89081#ifdef SQLITE_DEBUG
89082 if( pOp->p1 ){
89083 /* This is an OP_AggInverse call. Verify that xStep has always
89084 ** been called at least once prior to any xInverse call. */
89085 assert( pMem->uTemp==0x1122e0e3 );
89086 }else{
89087 /* This is an OP_AggStep call. Mark it as such. */
89088 pMem->uTemp = 0x1122e0e3;
89089 }
89090#endif
89091
89092 /* If this function is inside of a trigger, the register array in aMem[]
89093 ** might change from one evaluation to the next. The next block of code
89094 ** checks to see if the register array has changed, and if so it
89095 ** reinitializes the relavant parts of the sqlite3_context object */
89096 if( pCtx->pMem != pMem ){
89097 pCtx->pMem = pMem;
89098 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
89099 }
89100
89101#ifdef SQLITE_DEBUG
89102 for(i=0; i<pCtx->argc; i++){
89103 assert( memIsValid(pCtx->argv[i]) );
89104 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
89105 }
89106#endif
89107
89108 pMem->n++;
89109 assert( pCtx->pOut->flags==MEM_Null );
89110 assert( pCtx->isError==0 );
89111 assert( pCtx->skipFlag==0 );
89112#ifndef SQLITE_OMIT_WINDOWFUNC
89113 if( pOp->p1 ){
89114 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
89115 }else
89116#endif
89117 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
89118
89119 if( pCtx->isError ){
89120 if( pCtx->isError>0 ){
89121 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
89122 rc = pCtx->isError;
89123 }
89124 if( pCtx->skipFlag ){
89125 assert( pOp[-1].opcode==OP_CollSeq );
89126 i = pOp[-1].p1;
89127 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
89128 pCtx->skipFlag = 0;
89129 }
89130 sqlite3VdbeMemRelease(pCtx->pOut);
89131 pCtx->pOut->flags = MEM_Null;
89132 pCtx->isError = 0;
89133 if( rc ) goto abort_due_to_error;
89134 }
89135 assert( pCtx->pOut->flags==MEM_Null );
89136 assert( pCtx->skipFlag==0 );
89137 break;
89138}
89139
89140/* Opcode: AggFinal P1 P2 * P4 *
89141** Synopsis: accum=r[P1] N=P2
89142**
89143** P1 is the memory location that is the accumulator for an aggregate
89144** or window function. Execute the finalizer function
89145** for an aggregate and store the result in P1.
89146**
89147** P2 is the number of arguments that the step function takes and
89148** P4 is a pointer to the FuncDef for this function. The P2
89149** argument is not used by this opcode. It is only there to disambiguate
89150** functions that can take varying numbers of arguments. The
89151** P4 argument is only needed for the case where
89152** the step function was not previously called.
89153*/
89154/* Opcode: AggValue * P2 P3 P4 *
89155** Synopsis: r[P3]=value N=P2
89156**
89157** Invoke the xValue() function and store the result in register P3.
89158**
89159** P2 is the number of arguments that the step function takes and
89160** P4 is a pointer to the FuncDef for this function. The P2
89161** argument is not used by this opcode. It is only there to disambiguate
89162** functions that can take varying numbers of arguments. The
89163** P4 argument is only needed for the case where
89164** the step function was not previously called.
89165*/
89166case OP_AggValue:
89167case OP_AggFinal: {
89168 Mem *pMem;
89169 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
89170 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
89171 pMem = &aMem[pOp->p1];
89172 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
89173#ifndef SQLITE_OMIT_WINDOWFUNC
89174 if( pOp->p3 ){
89175 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
89176 pMem = &aMem[pOp->p3];
89177 }else
89178#endif
89179 {
89180 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
89181 }
89182
89183 if( rc ){
89184 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
89185 goto abort_due_to_error;
89186 }
89187 sqlite3VdbeChangeEncoding(pMem, encoding);
89188 UPDATE_MAX_BLOBSIZE(pMem);
89189 if( sqlite3VdbeMemTooBig(pMem) ){
89190 goto too_big;
89191 }
89192 break;
89193}
89194
89195#ifndef SQLITE_OMIT_WAL
89196/* Opcode: Checkpoint P1 P2 P3 * *
89197**
89198** Checkpoint database P1. This is a no-op if P1 is not currently in
89199** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
89200** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
89201** SQLITE_BUSY or not, respectively. Write the number of pages in the
89202** WAL after the checkpoint into mem[P3+1] and the number of pages
89203** in the WAL that have been checkpointed after the checkpoint
89204** completes into mem[P3+2]. However on an error, mem[P3+1] and
89205** mem[P3+2] are initialized to -1.
89206*/
89207case OP_Checkpoint: {
89208 int i; /* Loop counter */
89209 int aRes[3]; /* Results */
89210 Mem *pMem; /* Write results here */
89211
89212 assert( p->readOnly==0 );
89213 aRes[0] = 0;
89214 aRes[1] = aRes[2] = -1;
89215 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
89216 || pOp->p2==SQLITE_CHECKPOINT_FULL
89217 || pOp->p2==SQLITE_CHECKPOINT_RESTART
89218 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
89219 );
89220 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
89221 if( rc ){
89222 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
89223 rc = SQLITE_OK;
89224 aRes[0] = 1;
89225 }
89226 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
89227 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
89228 }
89229 break;
89230};
89231#endif
89232
89233#ifndef SQLITE_OMIT_PRAGMA
89234/* Opcode: JournalMode P1 P2 P3 * *
89235**
89236** Change the journal mode of database P1 to P3. P3 must be one of the
89237** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
89238** modes (delete, truncate, persist, off and memory), this is a simple
89239** operation. No IO is required.
89240**
89241** If changing into or out of WAL mode the procedure is more complicated.
89242**
89243** Write a string containing the final journal-mode to register P2.
89244*/
89245case OP_JournalMode: { /* out2 */
89246 Btree *pBt; /* Btree to change journal mode of */
89247 Pager *pPager; /* Pager associated with pBt */
89248 int eNew; /* New journal mode */
89249 int eOld; /* The old journal mode */
89250#ifndef SQLITE_OMIT_WAL
89251 const char *zFilename; /* Name of database file for pPager */
89252#endif
89253
89254 pOut = out2Prerelease(p, pOp);
89255 eNew = pOp->p3;
89256 assert( eNew==PAGER_JOURNALMODE_DELETE
89257 || eNew==PAGER_JOURNALMODE_TRUNCATE
89258 || eNew==PAGER_JOURNALMODE_PERSIST
89259 || eNew==PAGER_JOURNALMODE_OFF
89260 || eNew==PAGER_JOURNALMODE_MEMORY
89261 || eNew==PAGER_JOURNALMODE_WAL
89262 || eNew==PAGER_JOURNALMODE_QUERY
89263 );
89264 assert( pOp->p1>=0 && pOp->p1<db->nDb );
89265 assert( p->readOnly==0 );
89266
89267 pBt = db->aDb[pOp->p1].pBt;
89268 pPager = sqlite3BtreePager(pBt);
89269 eOld = sqlite3PagerGetJournalMode(pPager);
89270 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
89271 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
89272
89273#ifndef SQLITE_OMIT_WAL
89274 zFilename = sqlite3PagerFilename(pPager, 1);
89275
89276 /* Do not allow a transition to journal_mode=WAL for a database
89277 ** in temporary storage or if the VFS does not support shared memory
89278 */
89279 if( eNew==PAGER_JOURNALMODE_WAL
89280 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
89281 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
89282 ){
89283 eNew = eOld;
89284 }
89285
89286 if( (eNew!=eOld)
89287 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
89288 ){
89289 if( !db->autoCommit || db->nVdbeRead>1 ){
89290 rc = SQLITE_ERROR;
89291 sqlite3VdbeError(p,
89292 "cannot change %s wal mode from within a transaction",
89293 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
89294 );
89295 goto abort_due_to_error;
89296 }else{
89297
89298 if( eOld==PAGER_JOURNALMODE_WAL ){
89299 /* If leaving WAL mode, close the log file. If successful, the call
89300 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
89301 ** file. An EXCLUSIVE lock may still be held on the database file
89302 ** after a successful return.
89303 */
89304 rc = sqlite3PagerCloseWal(pPager, db);
89305 if( rc==SQLITE_OK ){
89306 sqlite3PagerSetJournalMode(pPager, eNew);
89307 }
89308 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
89309 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
89310 ** as an intermediate */
89311 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
89312 }
89313
89314 /* Open a transaction on the database file. Regardless of the journal
89315 ** mode, this transaction always uses a rollback journal.
89316 */
89317 assert( sqlite3BtreeIsInTrans(pBt)==0 );
89318 if( rc==SQLITE_OK ){
89319 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
89320 }
89321 }
89322 }
89323#endif /* ifndef SQLITE_OMIT_WAL */
89324
89325 if( rc ) eNew = eOld;
89326 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
89327
89328 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
89329 pOut->z = (char *)sqlite3JournalModename(eNew);
89330 pOut->n = sqlite3Strlen30(pOut->z);
89331 pOut->enc = SQLITE_UTF8;
89332 sqlite3VdbeChangeEncoding(pOut, encoding);
89333 if( rc ) goto abort_due_to_error;
89334 break;
89335};
89336#endif /* SQLITE_OMIT_PRAGMA */
89337
89338#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
89339/* Opcode: Vacuum P1 * * * *
89340**
89341** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
89342** for an attached database. The "temp" database may not be vacuumed.
89343*/
89344case OP_Vacuum: {
89345 assert( p->readOnly==0 );
89346 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1);
89347 if( rc ) goto abort_due_to_error;
89348 break;
89349}
89350#endif
89351
89352#if !defined(SQLITE_OMIT_AUTOVACUUM)
89353/* Opcode: IncrVacuum P1 P2 * * *
89354**
89355** Perform a single step of the incremental vacuum procedure on
89356** the P1 database. If the vacuum has finished, jump to instruction
89357** P2. Otherwise, fall through to the next instruction.
89358*/
89359case OP_IncrVacuum: { /* jump */
89360 Btree *pBt;
89361
89362 assert( pOp->p1>=0 && pOp->p1<db->nDb );
89363 assert( DbMaskTest(p->btreeMask, pOp->p1) );
89364 assert( p->readOnly==0 );
89365 pBt = db->aDb[pOp->p1].pBt;
89366 rc = sqlite3BtreeIncrVacuum(pBt);
89367 VdbeBranchTaken(rc==SQLITE_DONE,2);
89368 if( rc ){
89369 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
89370 rc = SQLITE_OK;
89371 goto jump_to_p2;
89372 }
89373 break;
89374}
89375#endif
89376
89377/* Opcode: Expire P1 P2 * * *
89378**
89379** Cause precompiled statements to expire. When an expired statement
89380** is executed using sqlite3_step() it will either automatically
89381** reprepare itself (if it was originally created using sqlite3_prepare_v2())
89382** or it will fail with SQLITE_SCHEMA.
89383**
89384** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
89385** then only the currently executing statement is expired.
89386**
89387** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
89388** then running SQL statements are allowed to continue to run to completion.
89389** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
89390** that might help the statement run faster but which does not affect the
89391** correctness of operation.
89392*/
89393case OP_Expire: {
89394 assert( pOp->p2==0 || pOp->p2==1 );
89395 if( !pOp->p1 ){
89396 sqlite3ExpirePreparedStatements(db, pOp->p2);
89397 }else{
89398 p->expired = pOp->p2+1;
89399 }
89400 break;
89401}
89402
89403#ifndef SQLITE_OMIT_SHARED_CACHE
89404/* Opcode: TableLock P1 P2 P3 P4 *
89405** Synopsis: iDb=P1 root=P2 write=P3
89406**
89407** Obtain a lock on a particular table. This instruction is only used when
89408** the shared-cache feature is enabled.
89409**
89410** P1 is the index of the database in sqlite3.aDb[] of the database
89411** on which the lock is acquired. A readlock is obtained if P3==0 or
89412** a write lock if P3==1.
89413**
89414** P2 contains the root-page of the table to lock.
89415**
89416** P4 contains a pointer to the name of the table being locked. This is only
89417** used to generate an error message if the lock cannot be obtained.
89418*/
89419case OP_TableLock: {
89420 u8 isWriteLock = (u8)pOp->p3;
89421 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
89422 int p1 = pOp->p1;
89423 assert( p1>=0 && p1<db->nDb );
89424 assert( DbMaskTest(p->btreeMask, p1) );
89425 assert( isWriteLock==0 || isWriteLock==1 );
89426 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
89427 if( rc ){
89428 if( (rc&0xFF)==SQLITE_LOCKED ){
89429 const char *z = pOp->p4.z;
89430 sqlite3VdbeError(p, "database table is locked: %s", z);
89431 }
89432 goto abort_due_to_error;
89433 }
89434 }
89435 break;
89436}
89437#endif /* SQLITE_OMIT_SHARED_CACHE */
89438
89439#ifndef SQLITE_OMIT_VIRTUALTABLE
89440/* Opcode: VBegin * * * P4 *
89441**
89442** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
89443** xBegin method for that table.
89444**
89445** Also, whether or not P4 is set, check that this is not being called from
89446** within a callback to a virtual table xSync() method. If it is, the error
89447** code will be set to SQLITE_LOCKED.
89448*/
89449case OP_VBegin: {
89450 VTable *pVTab;
89451 pVTab = pOp->p4.pVtab;
89452 rc = sqlite3VtabBegin(db, pVTab);
89453 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
89454 if( rc ) goto abort_due_to_error;
89455 break;
89456}
89457#endif /* SQLITE_OMIT_VIRTUALTABLE */
89458
89459#ifndef SQLITE_OMIT_VIRTUALTABLE
89460/* Opcode: VCreate P1 P2 * * *
89461**
89462** P2 is a register that holds the name of a virtual table in database
89463** P1. Call the xCreate method for that table.
89464*/
89465case OP_VCreate: {
89466 Mem sMem; /* For storing the record being decoded */
89467 const char *zTab; /* Name of the virtual table */
89468
89469 memset(&sMem, 0, sizeof(sMem));
89470 sMem.db = db;
89471 /* Because P2 is always a static string, it is impossible for the
89472 ** sqlite3VdbeMemCopy() to fail */
89473 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
89474 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
89475 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
89476 assert( rc==SQLITE_OK );
89477 zTab = (const char*)sqlite3_value_text(&sMem);
89478 assert( zTab || db->mallocFailed );
89479 if( zTab ){
89480 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
89481 }
89482 sqlite3VdbeMemRelease(&sMem);
89483 if( rc ) goto abort_due_to_error;
89484 break;
89485}
89486#endif /* SQLITE_OMIT_VIRTUALTABLE */
89487
89488#ifndef SQLITE_OMIT_VIRTUALTABLE
89489/* Opcode: VDestroy P1 * * P4 *
89490**
89491** P4 is the name of a virtual table in database P1. Call the xDestroy method
89492** of that table.
89493*/
89494case OP_VDestroy: {
89495 db->nVDestroy++;
89496 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
89497 db->nVDestroy--;
89498 if( rc ) goto abort_due_to_error;
89499 break;
89500}
89501#endif /* SQLITE_OMIT_VIRTUALTABLE */
89502
89503#ifndef SQLITE_OMIT_VIRTUALTABLE
89504/* Opcode: VOpen P1 * * P4 *
89505**
89506** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
89507** P1 is a cursor number. This opcode opens a cursor to the virtual
89508** table and stores that cursor in P1.
89509*/
89510case OP_VOpen: {
89511 VdbeCursor *pCur;
89512 sqlite3_vtab_cursor *pVCur;
89513 sqlite3_vtab *pVtab;
89514 const sqlite3_module *pModule;
89515
89516 assert( p->bIsReader );
89517 pCur = 0;
89518 pVCur = 0;
89519 pVtab = pOp->p4.pVtab->pVtab;
89520 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
89521 rc = SQLITE_LOCKED;
89522 goto abort_due_to_error;
89523 }
89524 pModule = pVtab->pModule;
89525 rc = pModule->xOpen(pVtab, &pVCur);
89526 sqlite3VtabImportErrmsg(p, pVtab);
89527 if( rc ) goto abort_due_to_error;
89528
89529 /* Initialize sqlite3_vtab_cursor base class */
89530 pVCur->pVtab = pVtab;
89531
89532 /* Initialize vdbe cursor object */
89533 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
89534 if( pCur ){
89535 pCur->uc.pVCur = pVCur;
89536 pVtab->nRef++;
89537 }else{
89538 assert( db->mallocFailed );
89539 pModule->xClose(pVCur);
89540 goto no_mem;
89541 }
89542 break;
89543}
89544#endif /* SQLITE_OMIT_VIRTUALTABLE */
89545
89546#ifndef SQLITE_OMIT_VIRTUALTABLE
89547/* Opcode: VFilter P1 P2 P3 P4 *
89548** Synopsis: iplan=r[P3] zplan='P4'
89549**
89550** P1 is a cursor opened using VOpen. P2 is an address to jump to if
89551** the filtered result set is empty.
89552**
89553** P4 is either NULL or a string that was generated by the xBestIndex
89554** method of the module. The interpretation of the P4 string is left
89555** to the module implementation.
89556**
89557** This opcode invokes the xFilter method on the virtual table specified
89558** by P1. The integer query plan parameter to xFilter is stored in register
89559** P3. Register P3+1 stores the argc parameter to be passed to the
89560** xFilter method. Registers P3+2..P3+1+argc are the argc
89561** additional parameters which are passed to
89562** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
89563**
89564** A jump is made to P2 if the result set after filtering would be empty.
89565*/
89566case OP_VFilter: { /* jump */
89567 int nArg;
89568 int iQuery;
89569 const sqlite3_module *pModule;
89570 Mem *pQuery;
89571 Mem *pArgc;
89572 sqlite3_vtab_cursor *pVCur;
89573 sqlite3_vtab *pVtab;
89574 VdbeCursor *pCur;
89575 int res;
89576 int i;
89577 Mem **apArg;
89578
89579 pQuery = &aMem[pOp->p3];
89580 pArgc = &pQuery[1];
89581 pCur = p->apCsr[pOp->p1];
89582 assert( memIsValid(pQuery) );
89583 REGISTER_TRACE(pOp->p3, pQuery);
89584 assert( pCur->eCurType==CURTYPE_VTAB );
89585 pVCur = pCur->uc.pVCur;
89586 pVtab = pVCur->pVtab;
89587 pModule = pVtab->pModule;
89588
89589 /* Grab the index number and argc parameters */
89590 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
89591 nArg = (int)pArgc->u.i;
89592 iQuery = (int)pQuery->u.i;
89593
89594 /* Invoke the xFilter method */
89595 res = 0;
89596 apArg = p->apArg;
89597 for(i = 0; i<nArg; i++){
89598 apArg[i] = &pArgc[i+1];
89599 }
89600 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
89601 sqlite3VtabImportErrmsg(p, pVtab);
89602 if( rc ) goto abort_due_to_error;
89603 res = pModule->xEof(pVCur);
89604 pCur->nullRow = 0;
89605 VdbeBranchTaken(res!=0,2);
89606 if( res ) goto jump_to_p2;
89607 break;
89608}
89609#endif /* SQLITE_OMIT_VIRTUALTABLE */
89610
89611#ifndef SQLITE_OMIT_VIRTUALTABLE
89612/* Opcode: VColumn P1 P2 P3 * P5
89613** Synopsis: r[P3]=vcolumn(P2)
89614**
89615** Store in register P3 the value of the P2-th column of
89616** the current row of the virtual-table of cursor P1.
89617**
89618** If the VColumn opcode is being used to fetch the value of
89619** an unchanging column during an UPDATE operation, then the P5
89620** value is 1. Otherwise, P5 is 0. The P5 value is returned
89621** by sqlite3_vtab_nochange() routine and can be used
89622** by virtual table implementations to return special "no-change"
89623** marks which can be more efficient, depending on the virtual table.
89624*/
89625case OP_VColumn: {
89626 sqlite3_vtab *pVtab;
89627 const sqlite3_module *pModule;
89628 Mem *pDest;
89629 sqlite3_context sContext;
89630
89631 VdbeCursor *pCur = p->apCsr[pOp->p1];
89632 assert( pCur->eCurType==CURTYPE_VTAB );
89633 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
89634 pDest = &aMem[pOp->p3];
89635 memAboutToChange(p, pDest);
89636 if( pCur->nullRow ){
89637 sqlite3VdbeMemSetNull(pDest);
89638 break;
89639 }
89640 pVtab = pCur->uc.pVCur->pVtab;
89641 pModule = pVtab->pModule;
89642 assert( pModule->xColumn );
89643 memset(&sContext, 0, sizeof(sContext));
89644 sContext.pOut = pDest;
89645 if( pOp->p5 ){
89646 sqlite3VdbeMemSetNull(pDest);
89647 pDest->flags = MEM_Null|MEM_Zero;
89648 pDest->u.nZero = 0;
89649 }else{
89650 MemSetTypeFlag(pDest, MEM_Null);
89651 }
89652 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
89653 sqlite3VtabImportErrmsg(p, pVtab);
89654 if( sContext.isError>0 ){
89655 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
89656 rc = sContext.isError;
89657 }
89658 sqlite3VdbeChangeEncoding(pDest, encoding);
89659 REGISTER_TRACE(pOp->p3, pDest);
89660 UPDATE_MAX_BLOBSIZE(pDest);
89661
89662 if( sqlite3VdbeMemTooBig(pDest) ){
89663 goto too_big;
89664 }
89665 if( rc ) goto abort_due_to_error;
89666 break;
89667}
89668#endif /* SQLITE_OMIT_VIRTUALTABLE */
89669
89670#ifndef SQLITE_OMIT_VIRTUALTABLE
89671/* Opcode: VNext P1 P2 * * *
89672**
89673** Advance virtual table P1 to the next row in its result set and
89674** jump to instruction P2. Or, if the virtual table has reached
89675** the end of its result set, then fall through to the next instruction.
89676*/
89677case OP_VNext: { /* jump */
89678 sqlite3_vtab *pVtab;
89679 const sqlite3_module *pModule;
89680 int res;
89681 VdbeCursor *pCur;
89682
89683 res = 0;
89684 pCur = p->apCsr[pOp->p1];
89685 assert( pCur->eCurType==CURTYPE_VTAB );
89686 if( pCur->nullRow ){
89687 break;
89688 }
89689 pVtab = pCur->uc.pVCur->pVtab;
89690 pModule = pVtab->pModule;
89691 assert( pModule->xNext );
89692
89693 /* Invoke the xNext() method of the module. There is no way for the
89694 ** underlying implementation to return an error if one occurs during
89695 ** xNext(). Instead, if an error occurs, true is returned (indicating that
89696 ** data is available) and the error code returned when xColumn or
89697 ** some other method is next invoked on the save virtual table cursor.
89698 */
89699 rc = pModule->xNext(pCur->uc.pVCur);
89700 sqlite3VtabImportErrmsg(p, pVtab);
89701 if( rc ) goto abort_due_to_error;
89702 res = pModule->xEof(pCur->uc.pVCur);
89703 VdbeBranchTaken(!res,2);
89704 if( !res ){
89705 /* If there is data, jump to P2 */
89706 goto jump_to_p2_and_check_for_interrupt;
89707 }
89708 goto check_for_interrupt;
89709}
89710#endif /* SQLITE_OMIT_VIRTUALTABLE */
89711
89712#ifndef SQLITE_OMIT_VIRTUALTABLE
89713/* Opcode: VRename P1 * * P4 *
89714**
89715** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
89716** This opcode invokes the corresponding xRename method. The value
89717** in register P1 is passed as the zName argument to the xRename method.
89718*/
89719case OP_VRename: {
89720 sqlite3_vtab *pVtab;
89721 Mem *pName;
89722 int isLegacy;
89723
89724 isLegacy = (db->flags & SQLITE_LegacyAlter);
89725 db->flags |= SQLITE_LegacyAlter;
89726 pVtab = pOp->p4.pVtab->pVtab;
89727 pName = &aMem[pOp->p1];
89728 assert( pVtab->pModule->xRename );
89729 assert( memIsValid(pName) );
89730 assert( p->readOnly==0 );
89731 REGISTER_TRACE(pOp->p1, pName);
89732 assert( pName->flags & MEM_Str );
89733 testcase( pName->enc==SQLITE_UTF8 );
89734 testcase( pName->enc==SQLITE_UTF16BE );
89735 testcase( pName->enc==SQLITE_UTF16LE );
89736 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
89737 if( rc ) goto abort_due_to_error;
89738 rc = pVtab->pModule->xRename(pVtab, pName->z);
89739 if( isLegacy==0 ) db->flags &= ~SQLITE_LegacyAlter;
89740 sqlite3VtabImportErrmsg(p, pVtab);
89741 p->expired = 0;
89742 if( rc ) goto abort_due_to_error;
89743 break;
89744}
89745#endif
89746
89747#ifndef SQLITE_OMIT_VIRTUALTABLE
89748/* Opcode: VUpdate P1 P2 P3 P4 P5
89749** Synopsis: data=r[P3@P2]
89750**
89751** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
89752** This opcode invokes the corresponding xUpdate method. P2 values
89753** are contiguous memory cells starting at P3 to pass to the xUpdate
89754** invocation. The value in register (P3+P2-1) corresponds to the
89755** p2th element of the argv array passed to xUpdate.
89756**
89757** The xUpdate method will do a DELETE or an INSERT or both.
89758** The argv[0] element (which corresponds to memory cell P3)
89759** is the rowid of a row to delete. If argv[0] is NULL then no
89760** deletion occurs. The argv[1] element is the rowid of the new
89761** row. This can be NULL to have the virtual table select the new
89762** rowid for itself. The subsequent elements in the array are
89763** the values of columns in the new row.
89764**
89765** If P2==1 then no insert is performed. argv[0] is the rowid of
89766** a row to delete.
89767**
89768** P1 is a boolean flag. If it is set to true and the xUpdate call
89769** is successful, then the value returned by sqlite3_last_insert_rowid()
89770** is set to the value of the rowid for the row just inserted.
89771**
89772** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
89773** apply in the case of a constraint failure on an insert or update.
89774*/
89775case OP_VUpdate: {
89776 sqlite3_vtab *pVtab;
89777 const sqlite3_module *pModule;
89778 int nArg;
89779 int i;
89780 sqlite_int64 rowid;
89781 Mem **apArg;
89782 Mem *pX;
89783
89784 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
89785 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
89786 );
89787 assert( p->readOnly==0 );
89788 if( db->mallocFailed ) goto no_mem;
89789 sqlite3VdbeIncrWriteCounter(p, 0);
89790 pVtab = pOp->p4.pVtab->pVtab;
89791 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
89792 rc = SQLITE_LOCKED;
89793 goto abort_due_to_error;
89794 }
89795 pModule = pVtab->pModule;
89796 nArg = pOp->p2;
89797 assert( pOp->p4type==P4_VTAB );
89798 if( ALWAYS(pModule->xUpdate) ){
89799 u8 vtabOnConflict = db->vtabOnConflict;
89800 apArg = p->apArg;
89801 pX = &aMem[pOp->p3];
89802 for(i=0; i<nArg; i++){
89803 assert( memIsValid(pX) );
89804 memAboutToChange(p, pX);
89805 apArg[i] = pX;
89806 pX++;
89807 }
89808 db->vtabOnConflict = pOp->p5;
89809 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
89810 db->vtabOnConflict = vtabOnConflict;
89811 sqlite3VtabImportErrmsg(p, pVtab);
89812 if( rc==SQLITE_OK && pOp->p1 ){
89813 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
89814 db->lastRowid = rowid;
89815 }
89816 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
89817 if( pOp->p5==OE_Ignore ){
89818 rc = SQLITE_OK;
89819 }else{
89820 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
89821 }
89822 }else{
89823 p->nChange++;
89824 }
89825 if( rc ) goto abort_due_to_error;
89826 }
89827 break;
89828}
89829#endif /* SQLITE_OMIT_VIRTUALTABLE */
89830
89831#ifndef SQLITE_OMIT_PAGER_PRAGMAS
89832/* Opcode: Pagecount P1 P2 * * *
89833**
89834** Write the current number of pages in database P1 to memory cell P2.
89835*/
89836case OP_Pagecount: { /* out2 */
89837 pOut = out2Prerelease(p, pOp);
89838 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
89839 break;
89840}
89841#endif
89842
89843
89844#ifndef SQLITE_OMIT_PAGER_PRAGMAS
89845/* Opcode: MaxPgcnt P1 P2 P3 * *
89846**
89847** Try to set the maximum page count for database P1 to the value in P3.
89848** Do not let the maximum page count fall below the current page count and
89849** do not change the maximum page count value if P3==0.
89850**
89851** Store the maximum page count after the change in register P2.
89852*/
89853case OP_MaxPgcnt: { /* out2 */
89854 unsigned int newMax;
89855 Btree *pBt;
89856
89857 pOut = out2Prerelease(p, pOp);
89858 pBt = db->aDb[pOp->p1].pBt;
89859 newMax = 0;
89860 if( pOp->p3 ){
89861 newMax = sqlite3BtreeLastPage(pBt);
89862 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
89863 }
89864 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
89865 break;
89866}
89867#endif
89868
89869/* Opcode: Function0 P1 P2 P3 P4 P5
89870** Synopsis: r[P3]=func(r[P2@P5])
89871**
89872** Invoke a user function (P4 is a pointer to a FuncDef object that
89873** defines the function) with P5 arguments taken from register P2 and
89874** successors. The result of the function is stored in register P3.
89875** Register P3 must not be one of the function inputs.
89876**
89877** P1 is a 32-bit bitmask indicating whether or not each argument to the
89878** function was determined to be constant at compile time. If the first
89879** argument was constant then bit 0 of P1 is set. This is used to determine
89880** whether meta data associated with a user function argument using the
89881** sqlite3_set_auxdata() API may be safely retained until the next
89882** invocation of this opcode.
89883**
89884** See also: Function, AggStep, AggFinal
89885*/
89886/* Opcode: Function P1 P2 P3 P4 P5
89887** Synopsis: r[P3]=func(r[P2@P5])
89888**
89889** Invoke a user function (P4 is a pointer to an sqlite3_context object that
89890** contains a pointer to the function to be run) with P5 arguments taken
89891** from register P2 and successors. The result of the function is stored
89892** in register P3. Register P3 must not be one of the function inputs.
89893**
89894** P1 is a 32-bit bitmask indicating whether or not each argument to the
89895** function was determined to be constant at compile time. If the first
89896** argument was constant then bit 0 of P1 is set. This is used to determine
89897** whether meta data associated with a user function argument using the
89898** sqlite3_set_auxdata() API may be safely retained until the next
89899** invocation of this opcode.
89900**
89901** SQL functions are initially coded as OP_Function0 with P4 pointing
89902** to a FuncDef object. But on first evaluation, the P4 operand is
89903** automatically converted into an sqlite3_context object and the operation
89904** changed to this OP_Function opcode. In this way, the initialization of
89905** the sqlite3_context object occurs only once, rather than once for each
89906** evaluation of the function.
89907**
89908** See also: Function0, AggStep, AggFinal
89909*/
89910case OP_PureFunc0: /* group */
89911case OP_Function0: { /* group */
89912 int n;
89913 sqlite3_context *pCtx;
89914
89915 assert( pOp->p4type==P4_FUNCDEF );
89916 n = pOp->p5;
89917 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
89918 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
89919 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
89920 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
89921 if( pCtx==0 ) goto no_mem;
89922 pCtx->pOut = 0;
89923 pCtx->pFunc = pOp->p4.pFunc;
89924 pCtx->iOp = (int)(pOp - aOp);
89925 pCtx->pVdbe = p;
89926 pCtx->isError = 0;
89927 pCtx->argc = n;
89928 pOp->p4type = P4_FUNCCTX;
89929 pOp->p4.pCtx = pCtx;
89930 assert( OP_PureFunc == OP_PureFunc0+2 );
89931 assert( OP_Function == OP_Function0+2 );
89932 pOp->opcode += 2;
89933 /* Fall through into OP_Function */
89934}
89935case OP_PureFunc: /* group */
89936case OP_Function: { /* group */
89937 int i;
89938 sqlite3_context *pCtx;
89939
89940 assert( pOp->p4type==P4_FUNCCTX );
89941 pCtx = pOp->p4.pCtx;
89942
89943 /* If this function is inside of a trigger, the register array in aMem[]
89944 ** might change from one evaluation to the next. The next block of code
89945 ** checks to see if the register array has changed, and if so it
89946 ** reinitializes the relavant parts of the sqlite3_context object */
89947 pOut = &aMem[pOp->p3];
89948 if( pCtx->pOut != pOut ){
89949 pCtx->pOut = pOut;
89950 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
89951 }
89952
89953 memAboutToChange(p, pOut);
89954#ifdef SQLITE_DEBUG
89955 for(i=0; i<pCtx->argc; i++){
89956 assert( memIsValid(pCtx->argv[i]) );
89957 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
89958 }
89959#endif
89960 MemSetTypeFlag(pOut, MEM_Null);
89961 assert( pCtx->isError==0 );
89962 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
89963
89964 /* If the function returned an error, throw an exception */
89965 if( pCtx->isError ){
89966 if( pCtx->isError>0 ){
89967 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
89968 rc = pCtx->isError;
89969 }
89970 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
89971 pCtx->isError = 0;
89972 if( rc ) goto abort_due_to_error;
89973 }
89974
89975 /* Copy the result of the function into register P3 */
89976 if( pOut->flags & (MEM_Str|MEM_Blob) ){
89977 sqlite3VdbeChangeEncoding(pOut, encoding);
89978 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
89979 }
89980
89981 REGISTER_TRACE(pOp->p3, pOut);
89982 UPDATE_MAX_BLOBSIZE(pOut);
89983 break;
89984}
89985
89986/* Opcode: Trace P1 P2 * P4 *
89987**
89988** Write P4 on the statement trace output if statement tracing is
89989** enabled.
89990**
89991** Operand P1 must be 0x7fffffff and P2 must positive.
89992*/
89993/* Opcode: Init P1 P2 P3 P4 *
89994** Synopsis: Start at P2
89995**
89996** Programs contain a single instance of this opcode as the very first
89997** opcode.
89998**
89999** If tracing is enabled (by the sqlite3_trace()) interface, then
90000** the UTF-8 string contained in P4 is emitted on the trace callback.
90001** Or if P4 is blank, use the string returned by sqlite3_sql().
90002**
90003** If P2 is not zero, jump to instruction P2.
90004**
90005** Increment the value of P1 so that OP_Once opcodes will jump the
90006** first time they are evaluated for this run.
90007**
90008** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
90009** error is encountered.
90010*/
90011case OP_Trace:
90012case OP_Init: { /* jump */
90013 int i;
90014#ifndef SQLITE_OMIT_TRACE
90015 char *zTrace;
90016#endif
90017
90018 /* If the P4 argument is not NULL, then it must be an SQL comment string.
90019 ** The "--" string is broken up to prevent false-positives with srcck1.c.
90020 **
90021 ** This assert() provides evidence for:
90022 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
90023 ** would have been returned by the legacy sqlite3_trace() interface by
90024 ** using the X argument when X begins with "--" and invoking
90025 ** sqlite3_expanded_sql(P) otherwise.
90026 */
90027 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
90028
90029 /* OP_Init is always instruction 0 */
90030 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
90031
90032#ifndef SQLITE_OMIT_TRACE
90033 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
90034 && !p->doingRerun
90035 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
90036 ){
90037#ifndef SQLITE_OMIT_DEPRECATED
90038 if( db->mTrace & SQLITE_TRACE_LEGACY ){
90039 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
90040 char *z = sqlite3VdbeExpandSql(p, zTrace);
90041 x(db->pTraceArg, z);
90042 sqlite3_free(z);
90043 }else
90044#endif
90045 if( db->nVdbeExec>1 ){
90046 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
90047 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
90048 sqlite3DbFree(db, z);
90049 }else{
90050 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
90051 }
90052 }
90053#ifdef SQLITE_USE_FCNTL_TRACE
90054 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
90055 if( zTrace ){
90056 int j;
90057 for(j=0; j<db->nDb; j++){
90058 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
90059 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
90060 }
90061 }
90062#endif /* SQLITE_USE_FCNTL_TRACE */
90063#ifdef SQLITE_DEBUG
90064 if( (db->flags & SQLITE_SqlTrace)!=0
90065 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
90066 ){
90067 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
90068 }
90069#endif /* SQLITE_DEBUG */
90070#endif /* SQLITE_OMIT_TRACE */
90071 assert( pOp->p2>0 );
90072 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
90073 if( pOp->opcode==OP_Trace ) break;
90074 for(i=1; i<p->nOp; i++){
90075 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
90076 }
90077 pOp->p1 = 0;
90078 }
90079 pOp->p1++;
90080 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
90081 goto jump_to_p2;
90082}
90083
90084#ifdef SQLITE_ENABLE_CURSOR_HINTS
90085/* Opcode: CursorHint P1 * * P4 *
90086**
90087** Provide a hint to cursor P1 that it only needs to return rows that
90088** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
90089** to values currently held in registers. TK_COLUMN terms in the P4
90090** expression refer to columns in the b-tree to which cursor P1 is pointing.
90091*/
90092case OP_CursorHint: {
90093 VdbeCursor *pC;
90094
90095 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90096 assert( pOp->p4type==P4_EXPR );
90097 pC = p->apCsr[pOp->p1];
90098 if( pC ){
90099 assert( pC->eCurType==CURTYPE_BTREE );
90100 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
90101 pOp->p4.pExpr, aMem);
90102 }
90103 break;
90104}
90105#endif /* SQLITE_ENABLE_CURSOR_HINTS */
90106
90107#ifdef SQLITE_DEBUG
90108/* Opcode: Abortable * * * * *
90109**
90110** Verify that an Abort can happen. Assert if an Abort at this point
90111** might cause database corruption. This opcode only appears in debugging
90112** builds.
90113**
90114** An Abort is safe if either there have been no writes, or if there is
90115** an active statement journal.
90116*/
90117case OP_Abortable: {
90118 sqlite3VdbeAssertAbortable(p);
90119 break;
90120}
90121#endif
90122
90123/* Opcode: Noop * * * * *
90124**
90125** Do nothing. This instruction is often useful as a jump
90126** destination.
90127*/
90128/*
90129** The magic Explain opcode are only inserted when explain==2 (which
90130** is to say when the EXPLAIN QUERY PLAN syntax is used.)
90131** This opcode records information from the optimizer. It is the
90132** the same as a no-op. This opcodesnever appears in a real VM program.
90133*/
90134default: { /* This is really OP_Noop, OP_Explain */
90135 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
90136
90137 break;
90138}
90139
90140/*****************************************************************************
90141** The cases of the switch statement above this line should all be indented
90142** by 6 spaces. But the left-most 6 spaces have been removed to improve the
90143** readability. From this point on down, the normal indentation rules are
90144** restored.
90145*****************************************************************************/
90146 }
90147
90148#ifdef VDBE_PROFILE
90149 {
90150 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
90151 if( endTime>start ) pOrigOp->cycles += endTime - start;
90152 pOrigOp->cnt++;
90153 }
90154#endif
90155
90156 /* The following code adds nothing to the actual functionality
90157 ** of the program. It is only here for testing and debugging.
90158 ** On the other hand, it does burn CPU cycles every time through
90159 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
90160 */
90161#ifndef NDEBUG
90162 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
90163
90164#ifdef SQLITE_DEBUG
90165 if( db->flags & SQLITE_VdbeTrace ){
90166 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
90167 if( rc!=0 ) printf("rc=%d\n",rc);
90168 if( opProperty & (OPFLG_OUT2) ){
90169 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
90170 }
90171 if( opProperty & OPFLG_OUT3 ){
90172 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
90173 }
90174 }
90175#endif /* SQLITE_DEBUG */
90176#endif /* NDEBUG */
90177 } /* The end of the for(;;) loop the loops through opcodes */
90178
90179 /* If we reach this point, it means that execution is finished with
90180 ** an error of some kind.
90181 */
90182abort_due_to_error:
90183 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
90184 assert( rc );
90185 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
90186 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
90187 }
90188 p->rc = rc;
90189 sqlite3SystemError(db, rc);
90190 testcase( sqlite3GlobalConfig.xLog!=0 );
90191 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
90192 (int)(pOp - aOp), p->zSql, p->zErrMsg);
90193 sqlite3VdbeHalt(p);
90194 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
90195 rc = SQLITE_ERROR;
90196 if( resetSchemaOnFault>0 ){
90197 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
90198 }
90199
90200 /* This is the only way out of this procedure. We have to
90201 ** release the mutexes on btrees that were acquired at the
90202 ** top. */
90203vdbe_return:
90204 testcase( nVmStep>0 );
90205 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
90206 sqlite3VdbeLeave(p);
90207 assert( rc!=SQLITE_OK || nExtraDelete==0
90208 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
90209 );
90210 return rc;
90211
90212 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
90213 ** is encountered.
90214 */
90215too_big:
90216 sqlite3VdbeError(p, "string or blob too big");
90217 rc = SQLITE_TOOBIG;
90218 goto abort_due_to_error;
90219
90220 /* Jump to here if a malloc() fails.
90221 */
90222no_mem:
90223 sqlite3OomFault(db);
90224 sqlite3VdbeError(p, "out of memory");
90225 rc = SQLITE_NOMEM_BKPT;
90226 goto abort_due_to_error;
90227
90228 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
90229 ** flag.
90230 */
90231abort_due_to_interrupt:
90232 assert( db->u1.isInterrupted );
90233 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
90234 p->rc = rc;
90235 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
90236 goto abort_due_to_error;
90237}
90238
90239
90240/************** End of vdbe.c ************************************************/
90241/************** Begin file vdbeblob.c ****************************************/
90242/*
90243** 2007 May 1
90244**
90245** The author disclaims copyright to this source code. In place of
90246** a legal notice, here is a blessing:
90247**
90248** May you do good and not evil.
90249** May you find forgiveness for yourself and forgive others.
90250** May you share freely, never taking more than you give.
90251**
90252*************************************************************************
90253**
90254** This file contains code used to implement incremental BLOB I/O.
90255*/
90256
90257/* #include "sqliteInt.h" */
90258/* #include "vdbeInt.h" */
90259
90260#ifndef SQLITE_OMIT_INCRBLOB
90261
90262/*
90263** Valid sqlite3_blob* handles point to Incrblob structures.
90264*/
90265typedef struct Incrblob Incrblob;
90266struct Incrblob {
90267 int nByte; /* Size of open blob, in bytes */
90268 int iOffset; /* Byte offset of blob in cursor data */
90269 u16 iCol; /* Table column this handle is open on */
90270 BtCursor *pCsr; /* Cursor pointing at blob row */
90271 sqlite3_stmt *pStmt; /* Statement holding cursor open */
90272 sqlite3 *db; /* The associated database */
90273 char *zDb; /* Database name */
90274 Table *pTab; /* Table object */
90275};
90276
90277
90278/*
90279** This function is used by both blob_open() and blob_reopen(). It seeks
90280** the b-tree cursor associated with blob handle p to point to row iRow.
90281** If successful, SQLITE_OK is returned and subsequent calls to
90282** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
90283**
90284** If an error occurs, or if the specified row does not exist or does not
90285** contain a value of type TEXT or BLOB in the column nominated when the
90286** blob handle was opened, then an error code is returned and *pzErr may
90287** be set to point to a buffer containing an error message. It is the
90288** responsibility of the caller to free the error message buffer using
90289** sqlite3DbFree().
90290**
90291** If an error does occur, then the b-tree cursor is closed. All subsequent
90292** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
90293** immediately return SQLITE_ABORT.
90294*/
90295static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
90296 int rc; /* Error code */
90297 char *zErr = 0; /* Error message */
90298 Vdbe *v = (Vdbe *)p->pStmt;
90299
90300 /* Set the value of register r[1] in the SQL statement to integer iRow.
90301 ** This is done directly as a performance optimization
90302 */
90303 v->aMem[1].flags = MEM_Int;
90304 v->aMem[1].u.i = iRow;
90305
90306 /* If the statement has been run before (and is paused at the OP_ResultRow)
90307 ** then back it up to the point where it does the OP_NotExists. This could
90308 ** have been down with an extra OP_Goto, but simply setting the program
90309 ** counter is faster. */
90310 if( v->pc>4 ){
90311 v->pc = 4;
90312 assert( v->aOp[v->pc].opcode==OP_NotExists );
90313 rc = sqlite3VdbeExec(v);
90314 }else{
90315 rc = sqlite3_step(p->pStmt);
90316 }
90317 if( rc==SQLITE_ROW ){
90318 VdbeCursor *pC = v->apCsr[0];
90319 u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
90320 testcase( pC->nHdrParsed==p->iCol );
90321 testcase( pC->nHdrParsed==p->iCol+1 );
90322 if( type<12 ){
90323 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
90324 type==0?"null": type==7?"real": "integer"
90325 );
90326 rc = SQLITE_ERROR;
90327 sqlite3_finalize(p->pStmt);
90328 p->pStmt = 0;
90329 }else{
90330 p->iOffset = pC->aType[p->iCol + pC->nField];
90331 p->nByte = sqlite3VdbeSerialTypeLen(type);
90332 p->pCsr = pC->uc.pCursor;
90333 sqlite3BtreeIncrblobCursor(p->pCsr);
90334 }
90335 }
90336
90337 if( rc==SQLITE_ROW ){
90338 rc = SQLITE_OK;
90339 }else if( p->pStmt ){
90340 rc = sqlite3_finalize(p->pStmt);
90341 p->pStmt = 0;
90342 if( rc==SQLITE_OK ){
90343 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
90344 rc = SQLITE_ERROR;
90345 }else{
90346 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
90347 }
90348 }
90349
90350 assert( rc!=SQLITE_OK || zErr==0 );
90351 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
90352
90353 *pzErr = zErr;
90354 return rc;
90355}
90356
90357/*
90358** Open a blob handle.
90359*/
90360SQLITE_API int sqlite3_blob_open(
90361 sqlite3* db, /* The database connection */
90362 const char *zDb, /* The attached database containing the blob */
90363 const char *zTable, /* The table containing the blob */
90364 const char *zColumn, /* The column containing the blob */
90365 sqlite_int64 iRow, /* The row containing the glob */
90366 int wrFlag, /* True -> read/write access, false -> read-only */
90367 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
90368){
90369 int nAttempt = 0;
90370 int iCol; /* Index of zColumn in row-record */
90371 int rc = SQLITE_OK;
90372 char *zErr = 0;
90373 Table *pTab;
90374 Incrblob *pBlob = 0;
90375 Parse sParse;
90376
90377#ifdef SQLITE_ENABLE_API_ARMOR
90378 if( ppBlob==0 ){
90379 return SQLITE_MISUSE_BKPT;
90380 }
90381#endif
90382 *ppBlob = 0;
90383#ifdef SQLITE_ENABLE_API_ARMOR
90384 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
90385 return SQLITE_MISUSE_BKPT;
90386 }
90387#endif
90388 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
90389
90390 sqlite3_mutex_enter(db->mutex);
90391
90392 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
90393 do {
90394 memset(&sParse, 0, sizeof(Parse));
90395 if( !pBlob ) goto blob_open_out;
90396 sParse.db = db;
90397 sqlite3DbFree(db, zErr);
90398 zErr = 0;
90399
90400 sqlite3BtreeEnterAll(db);
90401 pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb);
90402 if( pTab && IsVirtual(pTab) ){
90403 pTab = 0;
90404 sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable);
90405 }
90406 if( pTab && !HasRowid(pTab) ){
90407 pTab = 0;
90408 sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable);
90409 }
90410#ifndef SQLITE_OMIT_VIEW
90411 if( pTab && pTab->pSelect ){
90412 pTab = 0;
90413 sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
90414 }
90415#endif
90416 if( !pTab ){
90417 if( sParse.zErrMsg ){
90418 sqlite3DbFree(db, zErr);
90419 zErr = sParse.zErrMsg;
90420 sParse.zErrMsg = 0;
90421 }
90422 rc = SQLITE_ERROR;
90423 sqlite3BtreeLeaveAll(db);
90424 goto blob_open_out;
90425 }
90426 pBlob->pTab = pTab;
90427 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
90428
90429 /* Now search pTab for the exact column. */
90430 for(iCol=0; iCol<pTab->nCol; iCol++) {
90431 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
90432 break;
90433 }
90434 }
90435 if( iCol==pTab->nCol ){
90436 sqlite3DbFree(db, zErr);
90437 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
90438 rc = SQLITE_ERROR;
90439 sqlite3BtreeLeaveAll(db);
90440 goto blob_open_out;
90441 }
90442
90443 /* If the value is being opened for writing, check that the
90444 ** column is not indexed, and that it is not part of a foreign key.
90445 */
90446 if( wrFlag ){
90447 const char *zFault = 0;
90448 Index *pIdx;
90449#ifndef SQLITE_OMIT_FOREIGN_KEY
90450 if( db->flags&SQLITE_ForeignKeys ){
90451 /* Check that the column is not part of an FK child key definition. It
90452 ** is not necessary to check if it is part of a parent key, as parent
90453 ** key columns must be indexed. The check below will pick up this
90454 ** case. */
90455 FKey *pFKey;
90456 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
90457 int j;
90458 for(j=0; j<pFKey->nCol; j++){
90459 if( pFKey->aCol[j].iFrom==iCol ){
90460 zFault = "foreign key";
90461 }
90462 }
90463 }
90464 }
90465#endif
90466 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
90467 int j;
90468 for(j=0; j<pIdx->nKeyCol; j++){
90469 /* FIXME: Be smarter about indexes that use expressions */
90470 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
90471 zFault = "indexed";
90472 }
90473 }
90474 }
90475 if( zFault ){
90476 sqlite3DbFree(db, zErr);
90477 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
90478 rc = SQLITE_ERROR;
90479 sqlite3BtreeLeaveAll(db);
90480 goto blob_open_out;
90481 }
90482 }
90483
90484 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
90485 assert( pBlob->pStmt || db->mallocFailed );
90486 if( pBlob->pStmt ){
90487
90488 /* This VDBE program seeks a btree cursor to the identified
90489 ** db/table/row entry. The reason for using a vdbe program instead
90490 ** of writing code to use the b-tree layer directly is that the
90491 ** vdbe program will take advantage of the various transaction,
90492 ** locking and error handling infrastructure built into the vdbe.
90493 **
90494 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
90495 ** Code external to the Vdbe then "borrows" the b-tree cursor and
90496 ** uses it to implement the blob_read(), blob_write() and
90497 ** blob_bytes() functions.
90498 **
90499 ** The sqlite3_blob_close() function finalizes the vdbe program,
90500 ** which closes the b-tree cursor and (possibly) commits the
90501 ** transaction.
90502 */
90503 static const int iLn = VDBE_OFFSET_LINENO(2);
90504 static const VdbeOpList openBlob[] = {
90505 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
90506 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
90507 /* blobSeekToRow() will initialize r[1] to the desired rowid */
90508 {OP_NotExists, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
90509 {OP_Column, 0, 0, 1}, /* 3 */
90510 {OP_ResultRow, 1, 0, 0}, /* 4 */
90511 {OP_Halt, 0, 0, 0}, /* 5 */
90512 };
90513 Vdbe *v = (Vdbe *)pBlob->pStmt;
90514 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
90515 VdbeOp *aOp;
90516
90517 sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag,
90518 pTab->pSchema->schema_cookie,
90519 pTab->pSchema->iGeneration);
90520 sqlite3VdbeChangeP5(v, 1);
90521 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
90522 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
90523
90524 /* Make sure a mutex is held on the table to be accessed */
90525 sqlite3VdbeUsesBtree(v, iDb);
90526
90527 if( db->mallocFailed==0 ){
90528 assert( aOp!=0 );
90529 /* Configure the OP_TableLock instruction */
90530#ifdef SQLITE_OMIT_SHARED_CACHE
90531 aOp[0].opcode = OP_Noop;
90532#else
90533 aOp[0].p1 = iDb;
90534 aOp[0].p2 = pTab->tnum;
90535 aOp[0].p3 = wrFlag;
90536 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
90537 }
90538 if( db->mallocFailed==0 ){
90539#endif
90540
90541 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
90542 ** parameter of the other to pTab->tnum. */
90543 if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
90544 aOp[1].p2 = pTab->tnum;
90545 aOp[1].p3 = iDb;
90546
90547 /* Configure the number of columns. Configure the cursor to
90548 ** think that the table has one more column than it really
90549 ** does. An OP_Column to retrieve this imaginary column will
90550 ** always return an SQL NULL. This is useful because it means
90551 ** we can invoke OP_Column to fill in the vdbe cursors type
90552 ** and offset cache without causing any IO.
90553 */
90554 aOp[1].p4type = P4_INT32;
90555 aOp[1].p4.i = pTab->nCol+1;
90556 aOp[3].p2 = pTab->nCol;
90557
90558 sParse.nVar = 0;
90559 sParse.nMem = 1;
90560 sParse.nTab = 1;
90561 sqlite3VdbeMakeReady(v, &sParse);
90562 }
90563 }
90564
90565 pBlob->iCol = iCol;
90566 pBlob->db = db;
90567 sqlite3BtreeLeaveAll(db);
90568 if( db->mallocFailed ){
90569 goto blob_open_out;
90570 }
90571 rc = blobSeekToRow(pBlob, iRow, &zErr);
90572 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
90573
90574blob_open_out:
90575 if( rc==SQLITE_OK && db->mallocFailed==0 ){
90576 *ppBlob = (sqlite3_blob *)pBlob;
90577 }else{
90578 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
90579 sqlite3DbFree(db, pBlob);
90580 }
90581 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
90582 sqlite3DbFree(db, zErr);
90583 sqlite3ParserReset(&sParse);
90584 rc = sqlite3ApiExit(db, rc);
90585 sqlite3_mutex_leave(db->mutex);
90586 return rc;
90587}
90588
90589/*
90590** Close a blob handle that was previously created using
90591** sqlite3_blob_open().
90592*/
90593SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
90594 Incrblob *p = (Incrblob *)pBlob;
90595 int rc;
90596 sqlite3 *db;
90597
90598 if( p ){
90599 db = p->db;
90600 sqlite3_mutex_enter(db->mutex);
90601 rc = sqlite3_finalize(p->pStmt);
90602 sqlite3DbFree(db, p);
90603 sqlite3_mutex_leave(db->mutex);
90604 }else{
90605 rc = SQLITE_OK;
90606 }
90607 return rc;
90608}
90609
90610/*
90611** Perform a read or write operation on a blob
90612*/
90613static int blobReadWrite(
90614 sqlite3_blob *pBlob,
90615 void *z,
90616 int n,
90617 int iOffset,
90618 int (*xCall)(BtCursor*, u32, u32, void*)
90619){
90620 int rc;
90621 Incrblob *p = (Incrblob *)pBlob;
90622 Vdbe *v;
90623 sqlite3 *db;
90624
90625 if( p==0 ) return SQLITE_MISUSE_BKPT;
90626 db = p->db;
90627 sqlite3_mutex_enter(db->mutex);
90628 v = (Vdbe*)p->pStmt;
90629
90630 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
90631 /* Request is out of range. Return a transient error. */
90632 rc = SQLITE_ERROR;
90633 }else if( v==0 ){
90634 /* If there is no statement handle, then the blob-handle has
90635 ** already been invalidated. Return SQLITE_ABORT in this case.
90636 */
90637 rc = SQLITE_ABORT;
90638 }else{
90639 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
90640 ** returned, clean-up the statement handle.
90641 */
90642 assert( db == v->db );
90643 sqlite3BtreeEnterCursor(p->pCsr);
90644
90645#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
90646 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
90647 /* If a pre-update hook is registered and this is a write cursor,
90648 ** invoke it here.
90649 **
90650 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
90651 ** operation should really be an SQLITE_UPDATE. This is probably
90652 ** incorrect, but is convenient because at this point the new.* values
90653 ** are not easily obtainable. And for the sessions module, an
90654 ** SQLITE_UPDATE where the PK columns do not change is handled in the
90655 ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
90656 ** slightly more efficient). Since you cannot write to a PK column
90657 ** using the incremental-blob API, this works. For the sessions module
90658 ** anyhow.
90659 */
90660 sqlite3_int64 iKey;
90661 iKey = sqlite3BtreeIntegerKey(p->pCsr);
90662 sqlite3VdbePreUpdateHook(
90663 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1
90664 );
90665 }
90666#endif
90667
90668 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
90669 sqlite3BtreeLeaveCursor(p->pCsr);
90670 if( rc==SQLITE_ABORT ){
90671 sqlite3VdbeFinalize(v);
90672 p->pStmt = 0;
90673 }else{
90674 v->rc = rc;
90675 }
90676 }
90677 sqlite3Error(db, rc);
90678 rc = sqlite3ApiExit(db, rc);
90679 sqlite3_mutex_leave(db->mutex);
90680 return rc;
90681}
90682
90683/*
90684** Read data from a blob handle.
90685*/
90686SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
90687 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked);
90688}
90689
90690/*
90691** Write data to a blob handle.
90692*/
90693SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
90694 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
90695}
90696
90697/*
90698** Query a blob handle for the size of the data.
90699**
90700** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
90701** so no mutex is required for access.
90702*/
90703SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
90704 Incrblob *p = (Incrblob *)pBlob;
90705 return (p && p->pStmt) ? p->nByte : 0;
90706}
90707
90708/*
90709** Move an existing blob handle to point to a different row of the same
90710** database table.
90711**
90712** If an error occurs, or if the specified row does not exist or does not
90713** contain a blob or text value, then an error code is returned and the
90714** database handle error code and message set. If this happens, then all
90715** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
90716** immediately return SQLITE_ABORT.
90717*/
90718SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
90719 int rc;
90720 Incrblob *p = (Incrblob *)pBlob;
90721 sqlite3 *db;
90722
90723 if( p==0 ) return SQLITE_MISUSE_BKPT;
90724 db = p->db;
90725 sqlite3_mutex_enter(db->mutex);
90726
90727 if( p->pStmt==0 ){
90728 /* If there is no statement handle, then the blob-handle has
90729 ** already been invalidated. Return SQLITE_ABORT in this case.
90730 */
90731 rc = SQLITE_ABORT;
90732 }else{
90733 char *zErr;
90734 rc = blobSeekToRow(p, iRow, &zErr);
90735 if( rc!=SQLITE_OK ){
90736 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
90737 sqlite3DbFree(db, zErr);
90738 }
90739 assert( rc!=SQLITE_SCHEMA );
90740 }
90741
90742 rc = sqlite3ApiExit(db, rc);
90743 assert( rc==SQLITE_OK || p->pStmt==0 );
90744 sqlite3_mutex_leave(db->mutex);
90745 return rc;
90746}
90747
90748#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
90749
90750/************** End of vdbeblob.c ********************************************/
90751/************** Begin file vdbesort.c ****************************************/
90752/*
90753** 2011-07-09
90754**
90755** The author disclaims copyright to this source code. In place of
90756** a legal notice, here is a blessing:
90757**
90758** May you do good and not evil.
90759** May you find forgiveness for yourself and forgive others.
90760** May you share freely, never taking more than you give.
90761**
90762*************************************************************************
90763** This file contains code for the VdbeSorter object, used in concert with
90764** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
90765** or by SELECT statements with ORDER BY clauses that cannot be satisfied
90766** using indexes and without LIMIT clauses.
90767**
90768** The VdbeSorter object implements a multi-threaded external merge sort
90769** algorithm that is efficient even if the number of elements being sorted
90770** exceeds the available memory.
90771**
90772** Here is the (internal, non-API) interface between this module and the
90773** rest of the SQLite system:
90774**
90775** sqlite3VdbeSorterInit() Create a new VdbeSorter object.
90776**
90777** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter
90778** object. The row is a binary blob in the
90779** OP_MakeRecord format that contains both
90780** the ORDER BY key columns and result columns
90781** in the case of a SELECT w/ ORDER BY, or
90782** the complete record for an index entry
90783** in the case of a CREATE INDEX.
90784**
90785** sqlite3VdbeSorterRewind() Sort all content previously added.
90786** Position the read cursor on the
90787** first sorted element.
90788**
90789** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted
90790** element.
90791**
90792** sqlite3VdbeSorterRowkey() Return the complete binary blob for the
90793** row currently under the read cursor.
90794**
90795** sqlite3VdbeSorterCompare() Compare the binary blob for the row
90796** currently under the read cursor against
90797** another binary blob X and report if
90798** X is strictly less than the read cursor.
90799** Used to enforce uniqueness in a
90800** CREATE UNIQUE INDEX statement.
90801**
90802** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim
90803** all resources.
90804**
90805** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This
90806** is like Close() followed by Init() only
90807** much faster.
90808**
90809** The interfaces above must be called in a particular order. Write() can
90810** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and
90811** Compare() can only occur in between Rewind() and Close()/Reset(). i.e.
90812**
90813** Init()
90814** for each record: Write()
90815** Rewind()
90816** Rowkey()/Compare()
90817** Next()
90818** Close()
90819**
90820** Algorithm:
90821**
90822** Records passed to the sorter via calls to Write() are initially held
90823** unsorted in main memory. Assuming the amount of memory used never exceeds
90824** a threshold, when Rewind() is called the set of records is sorted using
90825** an in-memory merge sort. In this case, no temporary files are required
90826** and subsequent calls to Rowkey(), Next() and Compare() read records
90827** directly from main memory.
90828**
90829** If the amount of space used to store records in main memory exceeds the
90830** threshold, then the set of records currently in memory are sorted and
90831** written to a temporary file in "Packed Memory Array" (PMA) format.
90832** A PMA created at this point is known as a "level-0 PMA". Higher levels
90833** of PMAs may be created by merging existing PMAs together - for example
90834** merging two or more level-0 PMAs together creates a level-1 PMA.
90835**
90836** The threshold for the amount of main memory to use before flushing
90837** records to a PMA is roughly the same as the limit configured for the
90838** page-cache of the main database. Specifically, the threshold is set to
90839** the value returned by "PRAGMA main.page_size" multipled by
90840** that returned by "PRAGMA main.cache_size", in bytes.
90841**
90842** If the sorter is running in single-threaded mode, then all PMAs generated
90843** are appended to a single temporary file. Or, if the sorter is running in
90844** multi-threaded mode then up to (N+1) temporary files may be opened, where
90845** N is the configured number of worker threads. In this case, instead of
90846** sorting the records and writing the PMA to a temporary file itself, the
90847** calling thread usually launches a worker thread to do so. Except, if
90848** there are already N worker threads running, the main thread does the work
90849** itself.
90850**
90851** The sorter is running in multi-threaded mode if (a) the library was built
90852** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
90853** than zero, and (b) worker threads have been enabled at runtime by calling
90854** "PRAGMA threads=N" with some value of N greater than 0.
90855**
90856** When Rewind() is called, any data remaining in memory is flushed to a
90857** final PMA. So at this point the data is stored in some number of sorted
90858** PMAs within temporary files on disk.
90859**
90860** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the
90861** sorter is running in single-threaded mode, then these PMAs are merged
90862** incrementally as keys are retreived from the sorter by the VDBE. The
90863** MergeEngine object, described in further detail below, performs this
90864** merge.
90865**
90866** Or, if running in multi-threaded mode, then a background thread is
90867** launched to merge the existing PMAs. Once the background thread has
90868** merged T bytes of data into a single sorted PMA, the main thread
90869** begins reading keys from that PMA while the background thread proceeds
90870** with merging the next T bytes of data. And so on.
90871**
90872** Parameter T is set to half the value of the memory threshold used
90873** by Write() above to determine when to create a new PMA.
90874**
90875** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when
90876** Rewind() is called, then a hierarchy of incremental-merges is used.
90877** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on
90878** disk are merged together. Then T bytes of data from the second set, and
90879** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT
90880** PMAs at a time. This done is to improve locality.
90881**
90882** If running in multi-threaded mode and there are more than
90883** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more
90884** than one background thread may be created. Specifically, there may be
90885** one background thread for each temporary file on disk, and one background
90886** thread to merge the output of each of the others to a single PMA for
90887** the main thread to read from.
90888*/
90889/* #include "sqliteInt.h" */
90890/* #include "vdbeInt.h" */
90891
90892/*
90893** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
90894** messages to stderr that may be helpful in understanding the performance
90895** characteristics of the sorter in multi-threaded mode.
90896*/
90897#if 0
90898# define SQLITE_DEBUG_SORTER_THREADS 1
90899#endif
90900
90901/*
90902** Hard-coded maximum amount of data to accumulate in memory before flushing
90903** to a level 0 PMA. The purpose of this limit is to prevent various integer
90904** overflows. 512MiB.
90905*/
90906#define SQLITE_MAX_PMASZ (1<<29)
90907
90908/*
90909** Private objects used by the sorter
90910*/
90911typedef struct MergeEngine MergeEngine; /* Merge PMAs together */
90912typedef struct PmaReader PmaReader; /* Incrementally read one PMA */
90913typedef struct PmaWriter PmaWriter; /* Incrementally write one PMA */
90914typedef struct SorterRecord SorterRecord; /* A record being sorted */
90915typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
90916typedef struct SorterFile SorterFile; /* Temporary file object wrapper */
90917typedef struct SorterList SorterList; /* In-memory list of records */
90918typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */
90919
90920/*
90921** A container for a temp file handle and the current amount of data
90922** stored in the file.
90923*/
90924struct SorterFile {
90925 sqlite3_file *pFd; /* File handle */
90926 i64 iEof; /* Bytes of data stored in pFd */
90927};
90928
90929/*
90930** An in-memory list of objects to be sorted.
90931**
90932** If aMemory==0 then each object is allocated separately and the objects
90933** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects
90934** are stored in the aMemory[] bulk memory, one right after the other, and
90935** are connected using SorterRecord.u.iNext.
90936*/
90937struct SorterList {
90938 SorterRecord *pList; /* Linked list of records */
90939 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
90940 int szPMA; /* Size of pList as PMA in bytes */
90941};
90942
90943/*
90944** The MergeEngine object is used to combine two or more smaller PMAs into
90945** one big PMA using a merge operation. Separate PMAs all need to be
90946** combined into one big PMA in order to be able to step through the sorted
90947** records in order.
90948**
90949** The aReadr[] array contains a PmaReader object for each of the PMAs being
90950** merged. An aReadr[] object either points to a valid key or else is at EOF.
90951** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.)
90952** For the purposes of the paragraphs below, we assume that the array is
90953** actually N elements in size, where N is the smallest power of 2 greater
90954** to or equal to the number of PMAs being merged. The extra aReadr[] elements
90955** are treated as if they are empty (always at EOF).
90956**
90957** The aTree[] array is also N elements in size. The value of N is stored in
90958** the MergeEngine.nTree variable.
90959**
90960** The final (N/2) elements of aTree[] contain the results of comparing
90961** pairs of PMA keys together. Element i contains the result of
90962** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
90963** aTree element is set to the index of it.
90964**
90965** For the purposes of this comparison, EOF is considered greater than any
90966** other key value. If the keys are equal (only possible with two EOF
90967** values), it doesn't matter which index is stored.
90968**
90969** The (N/4) elements of aTree[] that precede the final (N/2) described
90970** above contains the index of the smallest of each block of 4 PmaReaders
90971** And so on. So that aTree[1] contains the index of the PmaReader that
90972** currently points to the smallest key value. aTree[0] is unused.
90973**
90974** Example:
90975**
90976** aReadr[0] -> Banana
90977** aReadr[1] -> Feijoa
90978** aReadr[2] -> Elderberry
90979** aReadr[3] -> Currant
90980** aReadr[4] -> Grapefruit
90981** aReadr[5] -> Apple
90982** aReadr[6] -> Durian
90983** aReadr[7] -> EOF
90984**
90985** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
90986**
90987** The current element is "Apple" (the value of the key indicated by
90988** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
90989** be advanced to the next key in its segment. Say the next key is
90990** "Eggplant":
90991**
90992** aReadr[5] -> Eggplant
90993**
90994** The contents of aTree[] are updated first by comparing the new PmaReader
90995** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
90996** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
90997** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
90998** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
90999** so the value written into element 1 of the array is 0. As follows:
91000**
91001** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
91002**
91003** In other words, each time we advance to the next sorter element, log2(N)
91004** key comparison operations are required, where N is the number of segments
91005** being merged (rounded up to the next power of 2).
91006*/
91007struct MergeEngine {
91008 int nTree; /* Used size of aTree/aReadr (power of 2) */
91009 SortSubtask *pTask; /* Used by this thread only */
91010 int *aTree; /* Current state of incremental merge */
91011 PmaReader *aReadr; /* Array of PmaReaders to merge data from */
91012};
91013
91014/*
91015** This object represents a single thread of control in a sort operation.
91016** Exactly VdbeSorter.nTask instances of this object are allocated
91017** as part of each VdbeSorter object. Instances are never allocated any
91018** other way. VdbeSorter.nTask is set to the number of worker threads allowed
91019** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread). Thus for
91020** single-threaded operation, there is exactly one instance of this object
91021** and for multi-threaded operation there are two or more instances.
91022**
91023** Essentially, this structure contains all those fields of the VdbeSorter
91024** structure for which each thread requires a separate instance. For example,
91025** each thread requries its own UnpackedRecord object to unpack records in
91026** as part of comparison operations.
91027**
91028** Before a background thread is launched, variable bDone is set to 0. Then,
91029** right before it exits, the thread itself sets bDone to 1. This is used for
91030** two purposes:
91031**
91032** 1. When flushing the contents of memory to a level-0 PMA on disk, to
91033** attempt to select a SortSubtask for which there is not already an
91034** active background thread (since doing so causes the main thread
91035** to block until it finishes).
91036**
91037** 2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
91038** to sqlite3ThreadJoin() is likely to block. Cases that are likely to
91039** block provoke debugging output.
91040**
91041** In both cases, the effects of the main thread seeing (bDone==0) even
91042** after the thread has finished are not dire. So we don't worry about
91043** memory barriers and such here.
91044*/
91045typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
91046struct SortSubtask {
91047 SQLiteThread *pThread; /* Background thread, if any */
91048 int bDone; /* Set if thread is finished but not joined */
91049 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
91050 UnpackedRecord *pUnpacked; /* Space to unpack a record */
91051 SorterList list; /* List for thread to write to a PMA */
91052 int nPMA; /* Number of PMAs currently in file */
91053 SorterCompare xCompare; /* Compare function to use */
91054 SorterFile file; /* Temp file for level-0 PMAs */
91055 SorterFile file2; /* Space for other PMAs */
91056};
91057
91058
91059/*
91060** Main sorter structure. A single instance of this is allocated for each
91061** sorter cursor created by the VDBE.
91062**
91063** mxKeysize:
91064** As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
91065** this variable is updated so as to be set to the size on disk of the
91066** largest record in the sorter.
91067*/
91068struct VdbeSorter {
91069 int mnPmaSize; /* Minimum PMA size, in bytes */
91070 int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
91071 int mxKeysize; /* Largest serialized key seen so far */
91072 int pgsz; /* Main database page size */
91073 PmaReader *pReader; /* Readr data from here after Rewind() */
91074 MergeEngine *pMerger; /* Or here, if bUseThreads==0 */
91075 sqlite3 *db; /* Database connection */
91076 KeyInfo *pKeyInfo; /* How to compare records */
91077 UnpackedRecord *pUnpacked; /* Used by VdbeSorterCompare() */
91078 SorterList list; /* List of in-memory records */
91079 int iMemory; /* Offset of free space in list.aMemory */
91080 int nMemory; /* Size of list.aMemory allocation in bytes */
91081 u8 bUsePMA; /* True if one or more PMAs created */
91082 u8 bUseThreads; /* True to use background threads */
91083 u8 iPrev; /* Previous thread used to flush PMA */
91084 u8 nTask; /* Size of aTask[] array */
91085 u8 typeMask;
91086 SortSubtask aTask[1]; /* One or more subtasks */
91087};
91088
91089#define SORTER_TYPE_INTEGER 0x01
91090#define SORTER_TYPE_TEXT 0x02
91091
91092/*
91093** An instance of the following object is used to read records out of a
91094** PMA, in sorted order. The next key to be read is cached in nKey/aKey.
91095** aKey might point into aMap or into aBuffer. If neither of those locations
91096** contain a contiguous representation of the key, then aAlloc is allocated
91097** and the key is copied into aAlloc and aKey is made to poitn to aAlloc.
91098**
91099** pFd==0 at EOF.
91100*/
91101struct PmaReader {
91102 i64 iReadOff; /* Current read offset */
91103 i64 iEof; /* 1 byte past EOF for this PmaReader */
91104 int nAlloc; /* Bytes of space at aAlloc */
91105 int nKey; /* Number of bytes in key */
91106 sqlite3_file *pFd; /* File handle we are reading from */
91107 u8 *aAlloc; /* Space for aKey if aBuffer and pMap wont work */
91108 u8 *aKey; /* Pointer to current key */
91109 u8 *aBuffer; /* Current read buffer */
91110 int nBuffer; /* Size of read buffer in bytes */
91111 u8 *aMap; /* Pointer to mapping of entire file */
91112 IncrMerger *pIncr; /* Incremental merger */
91113};
91114
91115/*
91116** Normally, a PmaReader object iterates through an existing PMA stored
91117** within a temp file. However, if the PmaReader.pIncr variable points to
91118** an object of the following type, it may be used to iterate/merge through
91119** multiple PMAs simultaneously.
91120**
91121** There are two types of IncrMerger object - single (bUseThread==0) and
91122** multi-threaded (bUseThread==1).
91123**
91124** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
91125** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in
91126** size. When the IncrMerger is initialized, it reads enough data from
91127** pMerger to populate aFile[0]. It then sets variables within the
91128** corresponding PmaReader object to read from that file and kicks off
91129** a background thread to populate aFile[1] with the next mxSz bytes of
91130** sorted record data from pMerger.
91131**
91132** When the PmaReader reaches the end of aFile[0], it blocks until the
91133** background thread has finished populating aFile[1]. It then exchanges
91134** the contents of the aFile[0] and aFile[1] variables within this structure,
91135** sets the PmaReader fields to read from the new aFile[0] and kicks off
91136** another background thread to populate the new aFile[1]. And so on, until
91137** the contents of pMerger are exhausted.
91138**
91139** A single-threaded IncrMerger does not open any temporary files of its
91140** own. Instead, it has exclusive access to mxSz bytes of space beginning
91141** at offset iStartOff of file pTask->file2. And instead of using a
91142** background thread to prepare data for the PmaReader, with a single
91143** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
91144** keys from pMerger by the calling thread whenever the PmaReader runs out
91145** of data.
91146*/
91147struct IncrMerger {
91148 SortSubtask *pTask; /* Task that owns this merger */
91149 MergeEngine *pMerger; /* Merge engine thread reads data from */
91150 i64 iStartOff; /* Offset to start writing file at */
91151 int mxSz; /* Maximum bytes of data to store */
91152 int bEof; /* Set to true when merge is finished */
91153 int bUseThread; /* True to use a bg thread for this object */
91154 SorterFile aFile[2]; /* aFile[0] for reading, [1] for writing */
91155};
91156
91157/*
91158** An instance of this object is used for writing a PMA.
91159**
91160** The PMA is written one record at a time. Each record is of an arbitrary
91161** size. But I/O is more efficient if it occurs in page-sized blocks where
91162** each block is aligned on a page boundary. This object caches writes to
91163** the PMA so that aligned, page-size blocks are written.
91164*/
91165struct PmaWriter {
91166 int eFWErr; /* Non-zero if in an error state */
91167 u8 *aBuffer; /* Pointer to write buffer */
91168 int nBuffer; /* Size of write buffer in bytes */
91169 int iBufStart; /* First byte of buffer to write */
91170 int iBufEnd; /* Last byte of buffer to write */
91171 i64 iWriteOff; /* Offset of start of buffer in file */
91172 sqlite3_file *pFd; /* File handle to write to */
91173};
91174
91175/*
91176** This object is the header on a single record while that record is being
91177** held in memory and prior to being written out as part of a PMA.
91178**
91179** How the linked list is connected depends on how memory is being managed
91180** by this module. If using a separate allocation for each in-memory record
91181** (VdbeSorter.list.aMemory==0), then the list is always connected using the
91182** SorterRecord.u.pNext pointers.
91183**
91184** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0),
91185** then while records are being accumulated the list is linked using the
91186** SorterRecord.u.iNext offset. This is because the aMemory[] array may
91187** be sqlite3Realloc()ed while records are being accumulated. Once the VM
91188** has finished passing records to the sorter, or when the in-memory buffer
91189** is full, the list is sorted. As part of the sorting process, it is
91190** converted to use the SorterRecord.u.pNext pointers. See function
91191** vdbeSorterSort() for details.
91192*/
91193struct SorterRecord {
91194 int nVal; /* Size of the record in bytes */
91195 union {
91196 SorterRecord *pNext; /* Pointer to next record in list */
91197 int iNext; /* Offset within aMemory of next record */
91198 } u;
91199 /* The data for the record immediately follows this header */
91200};
91201
91202/* Return a pointer to the buffer containing the record data for SorterRecord
91203** object p. Should be used as if:
91204**
91205** void *SRVAL(SorterRecord *p) { return (void*)&p[1]; }
91206*/
91207#define SRVAL(p) ((void*)((SorterRecord*)(p) + 1))
91208
91209
91210/* Maximum number of PMAs that a single MergeEngine can merge */
91211#define SORTER_MAX_MERGE_COUNT 16
91212
91213static int vdbeIncrSwap(IncrMerger*);
91214static void vdbeIncrFree(IncrMerger *);
91215
91216/*
91217** Free all memory belonging to the PmaReader object passed as the
91218** argument. All structure fields are set to zero before returning.
91219*/
91220static void vdbePmaReaderClear(PmaReader *pReadr){
91221 sqlite3_free(pReadr->aAlloc);
91222 sqlite3_free(pReadr->aBuffer);
91223 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
91224 vdbeIncrFree(pReadr->pIncr);
91225 memset(pReadr, 0, sizeof(PmaReader));
91226}
91227
91228/*
91229** Read the next nByte bytes of data from the PMA p.
91230** If successful, set *ppOut to point to a buffer containing the data
91231** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
91232** error code.
91233**
91234** The buffer returned in *ppOut is only valid until the
91235** next call to this function.
91236*/
91237static int vdbePmaReadBlob(
91238 PmaReader *p, /* PmaReader from which to take the blob */
91239 int nByte, /* Bytes of data to read */
91240 u8 **ppOut /* OUT: Pointer to buffer containing data */
91241){
91242 int iBuf; /* Offset within buffer to read from */
91243 int nAvail; /* Bytes of data available in buffer */
91244
91245 if( p->aMap ){
91246 *ppOut = &p->aMap[p->iReadOff];
91247 p->iReadOff += nByte;
91248 return SQLITE_OK;
91249 }
91250
91251 assert( p->aBuffer );
91252
91253 /* If there is no more data to be read from the buffer, read the next
91254 ** p->nBuffer bytes of data from the file into it. Or, if there are less
91255 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
91256 iBuf = p->iReadOff % p->nBuffer;
91257 if( iBuf==0 ){
91258 int nRead; /* Bytes to read from disk */
91259 int rc; /* sqlite3OsRead() return code */
91260
91261 /* Determine how many bytes of data to read. */
91262 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
91263 nRead = p->nBuffer;
91264 }else{
91265 nRead = (int)(p->iEof - p->iReadOff);
91266 }
91267 assert( nRead>0 );
91268
91269 /* Readr data from the file. Return early if an error occurs. */
91270 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
91271 assert( rc!=SQLITE_IOERR_SHORT_READ );
91272 if( rc!=SQLITE_OK ) return rc;
91273 }
91274 nAvail = p->nBuffer - iBuf;
91275
91276 if( nByte<=nAvail ){
91277 /* The requested data is available in the in-memory buffer. In this
91278 ** case there is no need to make a copy of the data, just return a
91279 ** pointer into the buffer to the caller. */
91280 *ppOut = &p->aBuffer[iBuf];
91281 p->iReadOff += nByte;
91282 }else{
91283 /* The requested data is not all available in the in-memory buffer.
91284 ** In this case, allocate space at p->aAlloc[] to copy the requested
91285 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
91286 int nRem; /* Bytes remaining to copy */
91287
91288 /* Extend the p->aAlloc[] allocation if required. */
91289 if( p->nAlloc<nByte ){
91290 u8 *aNew;
91291 int nNew = MAX(128, p->nAlloc*2);
91292 while( nByte>nNew ) nNew = nNew*2;
91293 aNew = sqlite3Realloc(p->aAlloc, nNew);
91294 if( !aNew ) return SQLITE_NOMEM_BKPT;
91295 p->nAlloc = nNew;
91296 p->aAlloc = aNew;
91297 }
91298
91299 /* Copy as much data as is available in the buffer into the start of
91300 ** p->aAlloc[]. */
91301 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
91302 p->iReadOff += nAvail;
91303 nRem = nByte - nAvail;
91304
91305 /* The following loop copies up to p->nBuffer bytes per iteration into
91306 ** the p->aAlloc[] buffer. */
91307 while( nRem>0 ){
91308 int rc; /* vdbePmaReadBlob() return code */
91309 int nCopy; /* Number of bytes to copy */
91310 u8 *aNext; /* Pointer to buffer to copy data from */
91311
91312 nCopy = nRem;
91313 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
91314 rc = vdbePmaReadBlob(p, nCopy, &aNext);
91315 if( rc!=SQLITE_OK ) return rc;
91316 assert( aNext!=p->aAlloc );
91317 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
91318 nRem -= nCopy;
91319 }
91320
91321 *ppOut = p->aAlloc;
91322 }
91323
91324 return SQLITE_OK;
91325}
91326
91327/*
91328** Read a varint from the stream of data accessed by p. Set *pnOut to
91329** the value read.
91330*/
91331static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
91332 int iBuf;
91333
91334 if( p->aMap ){
91335 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
91336 }else{
91337 iBuf = p->iReadOff % p->nBuffer;
91338 if( iBuf && (p->nBuffer-iBuf)>=9 ){
91339 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
91340 }else{
91341 u8 aVarint[16], *a;
91342 int i = 0, rc;
91343 do{
91344 rc = vdbePmaReadBlob(p, 1, &a);
91345 if( rc ) return rc;
91346 aVarint[(i++)&0xf] = a[0];
91347 }while( (a[0]&0x80)!=0 );
91348 sqlite3GetVarint(aVarint, pnOut);
91349 }
91350 }
91351
91352 return SQLITE_OK;
91353}
91354
91355/*
91356** Attempt to memory map file pFile. If successful, set *pp to point to the
91357** new mapping and return SQLITE_OK. If the mapping is not attempted
91358** (because the file is too large or the VFS layer is configured not to use
91359** mmap), return SQLITE_OK and set *pp to NULL.
91360**
91361** Or, if an error occurs, return an SQLite error code. The final value of
91362** *pp is undefined in this case.
91363*/
91364static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
91365 int rc = SQLITE_OK;
91366 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
91367 sqlite3_file *pFd = pFile->pFd;
91368 if( pFd->pMethods->iVersion>=3 ){
91369 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
91370 testcase( rc!=SQLITE_OK );
91371 }
91372 }
91373 return rc;
91374}
91375
91376/*
91377** Attach PmaReader pReadr to file pFile (if it is not already attached to
91378** that file) and seek it to offset iOff within the file. Return SQLITE_OK
91379** if successful, or an SQLite error code if an error occurs.
91380*/
91381static int vdbePmaReaderSeek(
91382 SortSubtask *pTask, /* Task context */
91383 PmaReader *pReadr, /* Reader whose cursor is to be moved */
91384 SorterFile *pFile, /* Sorter file to read from */
91385 i64 iOff /* Offset in pFile */
91386){
91387 int rc = SQLITE_OK;
91388
91389 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
91390
91391 if( sqlite3FaultSim(201) ) return SQLITE_IOERR_READ;
91392 if( pReadr->aMap ){
91393 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
91394 pReadr->aMap = 0;
91395 }
91396 pReadr->iReadOff = iOff;
91397 pReadr->iEof = pFile->iEof;
91398 pReadr->pFd = pFile->pFd;
91399
91400 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
91401 if( rc==SQLITE_OK && pReadr->aMap==0 ){
91402 int pgsz = pTask->pSorter->pgsz;
91403 int iBuf = pReadr->iReadOff % pgsz;
91404 if( pReadr->aBuffer==0 ){
91405 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
91406 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
91407 pReadr->nBuffer = pgsz;
91408 }
91409 if( rc==SQLITE_OK && iBuf ){
91410 int nRead = pgsz - iBuf;
91411 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
91412 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
91413 }
91414 rc = sqlite3OsRead(
91415 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
91416 );
91417 testcase( rc!=SQLITE_OK );
91418 }
91419 }
91420
91421 return rc;
91422}
91423
91424/*
91425** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
91426** no error occurs, or an SQLite error code if one does.
91427*/
91428static int vdbePmaReaderNext(PmaReader *pReadr){
91429 int rc = SQLITE_OK; /* Return Code */
91430 u64 nRec = 0; /* Size of record in bytes */
91431
91432
91433 if( pReadr->iReadOff>=pReadr->iEof ){
91434 IncrMerger *pIncr = pReadr->pIncr;
91435 int bEof = 1;
91436 if( pIncr ){
91437 rc = vdbeIncrSwap(pIncr);
91438 if( rc==SQLITE_OK && pIncr->bEof==0 ){
91439 rc = vdbePmaReaderSeek(
91440 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
91441 );
91442 bEof = 0;
91443 }
91444 }
91445
91446 if( bEof ){
91447 /* This is an EOF condition */
91448 vdbePmaReaderClear(pReadr);
91449 testcase( rc!=SQLITE_OK );
91450 return rc;
91451 }
91452 }
91453
91454 if( rc==SQLITE_OK ){
91455 rc = vdbePmaReadVarint(pReadr, &nRec);
91456 }
91457 if( rc==SQLITE_OK ){
91458 pReadr->nKey = (int)nRec;
91459 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
91460 testcase( rc!=SQLITE_OK );
91461 }
91462
91463 return rc;
91464}
91465
91466/*
91467** Initialize PmaReader pReadr to scan through the PMA stored in file pFile
91468** starting at offset iStart and ending at offset iEof-1. This function
91469** leaves the PmaReader pointing to the first key in the PMA (or EOF if the
91470** PMA is empty).
91471**
91472** If the pnByte parameter is NULL, then it is assumed that the file
91473** contains a single PMA, and that that PMA omits the initial length varint.
91474*/
91475static int vdbePmaReaderInit(
91476 SortSubtask *pTask, /* Task context */
91477 SorterFile *pFile, /* Sorter file to read from */
91478 i64 iStart, /* Start offset in pFile */
91479 PmaReader *pReadr, /* PmaReader to populate */
91480 i64 *pnByte /* IN/OUT: Increment this value by PMA size */
91481){
91482 int rc;
91483
91484 assert( pFile->iEof>iStart );
91485 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
91486 assert( pReadr->aBuffer==0 );
91487 assert( pReadr->aMap==0 );
91488
91489 rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart);
91490 if( rc==SQLITE_OK ){
91491 u64 nByte = 0; /* Size of PMA in bytes */
91492 rc = vdbePmaReadVarint(pReadr, &nByte);
91493 pReadr->iEof = pReadr->iReadOff + nByte;
91494 *pnByte += nByte;
91495 }
91496
91497 if( rc==SQLITE_OK ){
91498 rc = vdbePmaReaderNext(pReadr);
91499 }
91500 return rc;
91501}
91502
91503/*
91504** A version of vdbeSorterCompare() that assumes that it has already been
91505** determined that the first field of key1 is equal to the first field of
91506** key2.
91507*/
91508static int vdbeSorterCompareTail(
91509 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
91510 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
91511 const void *pKey1, int nKey1, /* Left side of comparison */
91512 const void *pKey2, int nKey2 /* Right side of comparison */
91513){
91514 UnpackedRecord *r2 = pTask->pUnpacked;
91515 if( *pbKey2Cached==0 ){
91516 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
91517 *pbKey2Cached = 1;
91518 }
91519 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1);
91520}
91521
91522/*
91523** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
91524** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
91525** used by the comparison. Return the result of the comparison.
91526**
91527** If IN/OUT parameter *pbKey2Cached is true when this function is called,
91528** it is assumed that (pTask->pUnpacked) contains the unpacked version
91529** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
91530** version of key2 and *pbKey2Cached set to true before returning.
91531**
91532** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
91533** to SQLITE_NOMEM.
91534*/
91535static int vdbeSorterCompare(
91536 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
91537 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
91538 const void *pKey1, int nKey1, /* Left side of comparison */
91539 const void *pKey2, int nKey2 /* Right side of comparison */
91540){
91541 UnpackedRecord *r2 = pTask->pUnpacked;
91542 if( !*pbKey2Cached ){
91543 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
91544 *pbKey2Cached = 1;
91545 }
91546 return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
91547}
91548
91549/*
91550** A specially optimized version of vdbeSorterCompare() that assumes that
91551** the first field of each key is a TEXT value and that the collation
91552** sequence to compare them with is BINARY.
91553*/
91554static int vdbeSorterCompareText(
91555 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
91556 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
91557 const void *pKey1, int nKey1, /* Left side of comparison */
91558 const void *pKey2, int nKey2 /* Right side of comparison */
91559){
91560 const u8 * const p1 = (const u8 * const)pKey1;
91561 const u8 * const p2 = (const u8 * const)pKey2;
91562 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
91563 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
91564
91565 int n1;
91566 int n2;
91567 int res;
91568
91569 getVarint32(&p1[1], n1);
91570 getVarint32(&p2[1], n2);
91571 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
91572 if( res==0 ){
91573 res = n1 - n2;
91574 }
91575
91576 if( res==0 ){
91577 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
91578 res = vdbeSorterCompareTail(
91579 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
91580 );
91581 }
91582 }else{
91583 if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
91584 res = res * -1;
91585 }
91586 }
91587
91588 return res;
91589}
91590
91591/*
91592** A specially optimized version of vdbeSorterCompare() that assumes that
91593** the first field of each key is an INTEGER value.
91594*/
91595static int vdbeSorterCompareInt(
91596 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
91597 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
91598 const void *pKey1, int nKey1, /* Left side of comparison */
91599 const void *pKey2, int nKey2 /* Right side of comparison */
91600){
91601 const u8 * const p1 = (const u8 * const)pKey1;
91602 const u8 * const p2 = (const u8 * const)pKey2;
91603 const int s1 = p1[1]; /* Left hand serial type */
91604 const int s2 = p2[1]; /* Right hand serial type */
91605 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
91606 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
91607 int res; /* Return value */
91608
91609 assert( (s1>0 && s1<7) || s1==8 || s1==9 );
91610 assert( (s2>0 && s2<7) || s2==8 || s2==9 );
91611
91612 if( s1==s2 ){
91613 /* The two values have the same sign. Compare using memcmp(). */
91614 static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8, 0, 0, 0 };
91615 const u8 n = aLen[s1];
91616 int i;
91617 res = 0;
91618 for(i=0; i<n; i++){
91619 if( (res = v1[i] - v2[i])!=0 ){
91620 if( ((v1[0] ^ v2[0]) & 0x80)!=0 ){
91621 res = v1[0] & 0x80 ? -1 : +1;
91622 }
91623 break;
91624 }
91625 }
91626 }else if( s1>7 && s2>7 ){
91627 res = s1 - s2;
91628 }else{
91629 if( s2>7 ){
91630 res = +1;
91631 }else if( s1>7 ){
91632 res = -1;
91633 }else{
91634 res = s1 - s2;
91635 }
91636 assert( res!=0 );
91637
91638 if( res>0 ){
91639 if( *v1 & 0x80 ) res = -1;
91640 }else{
91641 if( *v2 & 0x80 ) res = +1;
91642 }
91643 }
91644
91645 if( res==0 ){
91646 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
91647 res = vdbeSorterCompareTail(
91648 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
91649 );
91650 }
91651 }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
91652 res = res * -1;
91653 }
91654
91655 return res;
91656}
91657
91658/*
91659** Initialize the temporary index cursor just opened as a sorter cursor.
91660**
91661** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
91662** to determine the number of fields that should be compared from the
91663** records being sorted. However, if the value passed as argument nField
91664** is non-zero and the sorter is able to guarantee a stable sort, nField
91665** is used instead. This is used when sorting records for a CREATE INDEX
91666** statement. In this case, keys are always delivered to the sorter in
91667** order of the primary key, which happens to be make up the final part
91668** of the records being sorted. So if the sort is stable, there is never
91669** any reason to compare PK fields and they can be ignored for a small
91670** performance boost.
91671**
91672** The sorter can guarantee a stable sort when running in single-threaded
91673** mode, but not in multi-threaded mode.
91674**
91675** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
91676*/
91677SQLITE_PRIVATE int sqlite3VdbeSorterInit(
91678 sqlite3 *db, /* Database connection (for malloc()) */
91679 int nField, /* Number of key fields in each record */
91680 VdbeCursor *pCsr /* Cursor that holds the new sorter */
91681){
91682 int pgsz; /* Page size of main database */
91683 int i; /* Used to iterate through aTask[] */
91684 VdbeSorter *pSorter; /* The new sorter */
91685 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
91686 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
91687 int sz; /* Size of pSorter in bytes */
91688 int rc = SQLITE_OK;
91689#if SQLITE_MAX_WORKER_THREADS==0
91690# define nWorker 0
91691#else
91692 int nWorker;
91693#endif
91694
91695 /* Initialize the upper limit on the number of worker threads */
91696#if SQLITE_MAX_WORKER_THREADS>0
91697 if( sqlite3TempInMemory(db) || sqlite3GlobalConfig.bCoreMutex==0 ){
91698 nWorker = 0;
91699 }else{
91700 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
91701 }
91702#endif
91703
91704 /* Do not allow the total number of threads (main thread + all workers)
91705 ** to exceed the maximum merge count */
91706#if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT
91707 if( nWorker>=SORTER_MAX_MERGE_COUNT ){
91708 nWorker = SORTER_MAX_MERGE_COUNT-1;
91709 }
91710#endif
91711
91712 assert( pCsr->pKeyInfo && pCsr->pBtx==0 );
91713 assert( pCsr->eCurType==CURTYPE_SORTER );
91714 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
91715 sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
91716
91717 pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
91718 pCsr->uc.pSorter = pSorter;
91719 if( pSorter==0 ){
91720 rc = SQLITE_NOMEM_BKPT;
91721 }else{
91722 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
91723 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
91724 pKeyInfo->db = 0;
91725 if( nField && nWorker==0 ){
91726 pKeyInfo->nKeyField = nField;
91727 }
91728 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
91729 pSorter->nTask = nWorker + 1;
91730 pSorter->iPrev = (u8)(nWorker - 1);
91731 pSorter->bUseThreads = (pSorter->nTask>1);
91732 pSorter->db = db;
91733 for(i=0; i<pSorter->nTask; i++){
91734 SortSubtask *pTask = &pSorter->aTask[i];
91735 pTask->pSorter = pSorter;
91736 }
91737
91738 if( !sqlite3TempInMemory(db) ){
91739 i64 mxCache; /* Cache size in bytes*/
91740 u32 szPma = sqlite3GlobalConfig.szPma;
91741 pSorter->mnPmaSize = szPma * pgsz;
91742
91743 mxCache = db->aDb[0].pSchema->cache_size;
91744 if( mxCache<0 ){
91745 /* A negative cache-size value C indicates that the cache is abs(C)
91746 ** KiB in size. */
91747 mxCache = mxCache * -1024;
91748 }else{
91749 mxCache = mxCache * pgsz;
91750 }
91751 mxCache = MIN(mxCache, SQLITE_MAX_PMASZ);
91752 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
91753
91754 /* Avoid large memory allocations if the application has requested
91755 ** SQLITE_CONFIG_SMALL_MALLOC. */
91756 if( sqlite3GlobalConfig.bSmallMalloc==0 ){
91757 assert( pSorter->iMemory==0 );
91758 pSorter->nMemory = pgsz;
91759 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
91760 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
91761 }
91762 }
91763
91764 if( pKeyInfo->nAllField<13
91765 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
91766 ){
91767 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
91768 }
91769 }
91770
91771 return rc;
91772}
91773#undef nWorker /* Defined at the top of this function */
91774
91775/*
91776** Free the list of sorted records starting at pRecord.
91777*/
91778static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
91779 SorterRecord *p;
91780 SorterRecord *pNext;
91781 for(p=pRecord; p; p=pNext){
91782 pNext = p->u.pNext;
91783 sqlite3DbFree(db, p);
91784 }
91785}
91786
91787/*
91788** Free all resources owned by the object indicated by argument pTask. All
91789** fields of *pTask are zeroed before returning.
91790*/
91791static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
91792 sqlite3DbFree(db, pTask->pUnpacked);
91793#if SQLITE_MAX_WORKER_THREADS>0
91794 /* pTask->list.aMemory can only be non-zero if it was handed memory
91795 ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */
91796 if( pTask->list.aMemory ){
91797 sqlite3_free(pTask->list.aMemory);
91798 }else
91799#endif
91800 {
91801 assert( pTask->list.aMemory==0 );
91802 vdbeSorterRecordFree(0, pTask->list.pList);
91803 }
91804 if( pTask->file.pFd ){
91805 sqlite3OsCloseFree(pTask->file.pFd);
91806 }
91807 if( pTask->file2.pFd ){
91808 sqlite3OsCloseFree(pTask->file2.pFd);
91809 }
91810 memset(pTask, 0, sizeof(SortSubtask));
91811}
91812
91813#ifdef SQLITE_DEBUG_SORTER_THREADS
91814static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
91815 i64 t;
91816 int iTask = (pTask - pTask->pSorter->aTask);
91817 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
91818 fprintf(stderr, "%lld:%d %s\n", t, iTask, zEvent);
91819}
91820static void vdbeSorterRewindDebug(const char *zEvent){
91821 i64 t;
91822 sqlite3OsCurrentTimeInt64(sqlite3_vfs_find(0), &t);
91823 fprintf(stderr, "%lld:X %s\n", t, zEvent);
91824}
91825static void vdbeSorterPopulateDebug(
91826 SortSubtask *pTask,
91827 const char *zEvent
91828){
91829 i64 t;
91830 int iTask = (pTask - pTask->pSorter->aTask);
91831 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
91832 fprintf(stderr, "%lld:bg%d %s\n", t, iTask, zEvent);
91833}
91834static void vdbeSorterBlockDebug(
91835 SortSubtask *pTask,
91836 int bBlocked,
91837 const char *zEvent
91838){
91839 if( bBlocked ){
91840 i64 t;
91841 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
91842 fprintf(stderr, "%lld:main %s\n", t, zEvent);
91843 }
91844}
91845#else
91846# define vdbeSorterWorkDebug(x,y)
91847# define vdbeSorterRewindDebug(y)
91848# define vdbeSorterPopulateDebug(x,y)
91849# define vdbeSorterBlockDebug(x,y,z)
91850#endif
91851
91852#if SQLITE_MAX_WORKER_THREADS>0
91853/*
91854** Join thread pTask->thread.
91855*/
91856static int vdbeSorterJoinThread(SortSubtask *pTask){
91857 int rc = SQLITE_OK;
91858 if( pTask->pThread ){
91859#ifdef SQLITE_DEBUG_SORTER_THREADS
91860 int bDone = pTask->bDone;
91861#endif
91862 void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR);
91863 vdbeSorterBlockDebug(pTask, !bDone, "enter");
91864 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
91865 vdbeSorterBlockDebug(pTask, !bDone, "exit");
91866 rc = SQLITE_PTR_TO_INT(pRet);
91867 assert( pTask->bDone==1 );
91868 pTask->bDone = 0;
91869 pTask->pThread = 0;
91870 }
91871 return rc;
91872}
91873
91874/*
91875** Launch a background thread to run xTask(pIn).
91876*/
91877static int vdbeSorterCreateThread(
91878 SortSubtask *pTask, /* Thread will use this task object */
91879 void *(*xTask)(void*), /* Routine to run in a separate thread */
91880 void *pIn /* Argument passed into xTask() */
91881){
91882 assert( pTask->pThread==0 && pTask->bDone==0 );
91883 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
91884}
91885
91886/*
91887** Join all outstanding threads launched by SorterWrite() to create
91888** level-0 PMAs.
91889*/
91890static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
91891 int rc = rcin;
91892 int i;
91893
91894 /* This function is always called by the main user thread.
91895 **
91896 ** If this function is being called after SorterRewind() has been called,
91897 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
91898 ** is currently attempt to join one of the other threads. To avoid a race
91899 ** condition where this thread also attempts to join the same object, join
91900 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
91901 for(i=pSorter->nTask-1; i>=0; i--){
91902 SortSubtask *pTask = &pSorter->aTask[i];
91903 int rc2 = vdbeSorterJoinThread(pTask);
91904 if( rc==SQLITE_OK ) rc = rc2;
91905 }
91906 return rc;
91907}
91908#else
91909# define vdbeSorterJoinAll(x,rcin) (rcin)
91910# define vdbeSorterJoinThread(pTask) SQLITE_OK
91911#endif
91912
91913/*
91914** Allocate a new MergeEngine object capable of handling up to
91915** nReader PmaReader inputs.
91916**
91917** nReader is automatically rounded up to the next power of two.
91918** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
91919*/
91920static MergeEngine *vdbeMergeEngineNew(int nReader){
91921 int N = 2; /* Smallest power of two >= nReader */
91922 int nByte; /* Total bytes of space to allocate */
91923 MergeEngine *pNew; /* Pointer to allocated object to return */
91924
91925 assert( nReader<=SORTER_MAX_MERGE_COUNT );
91926
91927 while( N<nReader ) N += N;
91928 nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
91929
91930 pNew = sqlite3FaultSim(100) ? 0 : (MergeEngine*)sqlite3MallocZero(nByte);
91931 if( pNew ){
91932 pNew->nTree = N;
91933 pNew->pTask = 0;
91934 pNew->aReadr = (PmaReader*)&pNew[1];
91935 pNew->aTree = (int*)&pNew->aReadr[N];
91936 }
91937 return pNew;
91938}
91939
91940/*
91941** Free the MergeEngine object passed as the only argument.
91942*/
91943static void vdbeMergeEngineFree(MergeEngine *pMerger){
91944 int i;
91945 if( pMerger ){
91946 for(i=0; i<pMerger->nTree; i++){
91947 vdbePmaReaderClear(&pMerger->aReadr[i]);
91948 }
91949 }
91950 sqlite3_free(pMerger);
91951}
91952
91953/*
91954** Free all resources associated with the IncrMerger object indicated by
91955** the first argument.
91956*/
91957static void vdbeIncrFree(IncrMerger *pIncr){
91958 if( pIncr ){
91959#if SQLITE_MAX_WORKER_THREADS>0
91960 if( pIncr->bUseThread ){
91961 vdbeSorterJoinThread(pIncr->pTask);
91962 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
91963 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
91964 }
91965#endif
91966 vdbeMergeEngineFree(pIncr->pMerger);
91967 sqlite3_free(pIncr);
91968 }
91969}
91970
91971/*
91972** Reset a sorting cursor back to its original empty state.
91973*/
91974SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
91975 int i;
91976 (void)vdbeSorterJoinAll(pSorter, SQLITE_OK);
91977 assert( pSorter->bUseThreads || pSorter->pReader==0 );
91978#if SQLITE_MAX_WORKER_THREADS>0
91979 if( pSorter->pReader ){
91980 vdbePmaReaderClear(pSorter->pReader);
91981 sqlite3DbFree(db, pSorter->pReader);
91982 pSorter->pReader = 0;
91983 }
91984#endif
91985 vdbeMergeEngineFree(pSorter->pMerger);
91986 pSorter->pMerger = 0;
91987 for(i=0; i<pSorter->nTask; i++){
91988 SortSubtask *pTask = &pSorter->aTask[i];
91989 vdbeSortSubtaskCleanup(db, pTask);
91990 pTask->pSorter = pSorter;
91991 }
91992 if( pSorter->list.aMemory==0 ){
91993 vdbeSorterRecordFree(0, pSorter->list.pList);
91994 }
91995 pSorter->list.pList = 0;
91996 pSorter->list.szPMA = 0;
91997 pSorter->bUsePMA = 0;
91998 pSorter->iMemory = 0;
91999 pSorter->mxKeysize = 0;
92000 sqlite3DbFree(db, pSorter->pUnpacked);
92001 pSorter->pUnpacked = 0;
92002}
92003
92004/*
92005** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
92006*/
92007SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
92008 VdbeSorter *pSorter;
92009 assert( pCsr->eCurType==CURTYPE_SORTER );
92010 pSorter = pCsr->uc.pSorter;
92011 if( pSorter ){
92012 sqlite3VdbeSorterReset(db, pSorter);
92013 sqlite3_free(pSorter->list.aMemory);
92014 sqlite3DbFree(db, pSorter);
92015 pCsr->uc.pSorter = 0;
92016 }
92017}
92018
92019#if SQLITE_MAX_MMAP_SIZE>0
92020/*
92021** The first argument is a file-handle open on a temporary file. The file
92022** is guaranteed to be nByte bytes or smaller in size. This function
92023** attempts to extend the file to nByte bytes in size and to ensure that
92024** the VFS has memory mapped it.
92025**
92026** Whether or not the file does end up memory mapped of course depends on
92027** the specific VFS implementation.
92028*/
92029static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
92030 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
92031 void *p = 0;
92032 int chunksize = 4*1024;
92033 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE, &chunksize);
92034 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT, &nByte);
92035 sqlite3OsFetch(pFd, 0, (int)nByte, &p);
92036 sqlite3OsUnfetch(pFd, 0, p);
92037 }
92038}
92039#else
92040# define vdbeSorterExtendFile(x,y,z)
92041#endif
92042
92043/*
92044** Allocate space for a file-handle and open a temporary file. If successful,
92045** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
92046** Otherwise, set *ppFd to 0 and return an SQLite error code.
92047*/
92048static int vdbeSorterOpenTempFile(
92049 sqlite3 *db, /* Database handle doing sort */
92050 i64 nExtend, /* Attempt to extend file to this size */
92051 sqlite3_file **ppFd
92052){
92053 int rc;
92054 if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS;
92055 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
92056 SQLITE_OPEN_TEMP_JOURNAL |
92057 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
92058 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &rc
92059 );
92060 if( rc==SQLITE_OK ){
92061 i64 max = SQLITE_MAX_MMAP_SIZE;
92062 sqlite3OsFileControlHint(*ppFd, SQLITE_FCNTL_MMAP_SIZE, (void*)&max);
92063 if( nExtend>0 ){
92064 vdbeSorterExtendFile(db, *ppFd, nExtend);
92065 }
92066 }
92067 return rc;
92068}
92069
92070/*
92071** If it has not already been allocated, allocate the UnpackedRecord
92072** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
92073** if no allocation was required), or SQLITE_NOMEM otherwise.
92074*/
92075static int vdbeSortAllocUnpacked(SortSubtask *pTask){
92076 if( pTask->pUnpacked==0 ){
92077 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
92078 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
92079 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
92080 pTask->pUnpacked->errCode = 0;
92081 }
92082 return SQLITE_OK;
92083}
92084
92085
92086/*
92087** Merge the two sorted lists p1 and p2 into a single list.
92088*/
92089static SorterRecord *vdbeSorterMerge(
92090 SortSubtask *pTask, /* Calling thread context */
92091 SorterRecord *p1, /* First list to merge */
92092 SorterRecord *p2 /* Second list to merge */
92093){
92094 SorterRecord *pFinal = 0;
92095 SorterRecord **pp = &pFinal;
92096 int bCached = 0;
92097
92098 assert( p1!=0 && p2!=0 );
92099 for(;;){
92100 int res;
92101 res = pTask->xCompare(
92102 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
92103 );
92104
92105 if( res<=0 ){
92106 *pp = p1;
92107 pp = &p1->u.pNext;
92108 p1 = p1->u.pNext;
92109 if( p1==0 ){
92110 *pp = p2;
92111 break;
92112 }
92113 }else{
92114 *pp = p2;
92115 pp = &p2->u.pNext;
92116 p2 = p2->u.pNext;
92117 bCached = 0;
92118 if( p2==0 ){
92119 *pp = p1;
92120 break;
92121 }
92122 }
92123 }
92124 return pFinal;
92125}
92126
92127/*
92128** Return the SorterCompare function to compare values collected by the
92129** sorter object passed as the only argument.
92130*/
92131static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
92132 if( p->typeMask==SORTER_TYPE_INTEGER ){
92133 return vdbeSorterCompareInt;
92134 }else if( p->typeMask==SORTER_TYPE_TEXT ){
92135 return vdbeSorterCompareText;
92136 }
92137 return vdbeSorterCompare;
92138}
92139
92140/*
92141** Sort the linked list of records headed at pTask->pList. Return
92142** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
92143** an error occurs.
92144*/
92145static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
92146 int i;
92147 SorterRecord **aSlot;
92148 SorterRecord *p;
92149 int rc;
92150
92151 rc = vdbeSortAllocUnpacked(pTask);
92152 if( rc!=SQLITE_OK ) return rc;
92153
92154 p = pList->pList;
92155 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
92156
92157 aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
92158 if( !aSlot ){
92159 return SQLITE_NOMEM_BKPT;
92160 }
92161
92162 while( p ){
92163 SorterRecord *pNext;
92164 if( pList->aMemory ){
92165 if( (u8*)p==pList->aMemory ){
92166 pNext = 0;
92167 }else{
92168 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
92169 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
92170 }
92171 }else{
92172 pNext = p->u.pNext;
92173 }
92174
92175 p->u.pNext = 0;
92176 for(i=0; aSlot[i]; i++){
92177 p = vdbeSorterMerge(pTask, p, aSlot[i]);
92178 aSlot[i] = 0;
92179 }
92180 aSlot[i] = p;
92181 p = pNext;
92182 }
92183
92184 p = 0;
92185 for(i=0; i<64; i++){
92186 if( aSlot[i]==0 ) continue;
92187 p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i];
92188 }
92189 pList->pList = p;
92190
92191 sqlite3_free(aSlot);
92192 assert( pTask->pUnpacked->errCode==SQLITE_OK
92193 || pTask->pUnpacked->errCode==SQLITE_NOMEM
92194 );
92195 return pTask->pUnpacked->errCode;
92196}
92197
92198/*
92199** Initialize a PMA-writer object.
92200*/
92201static void vdbePmaWriterInit(
92202 sqlite3_file *pFd, /* File handle to write to */
92203 PmaWriter *p, /* Object to populate */
92204 int nBuf, /* Buffer size */
92205 i64 iStart /* Offset of pFd to begin writing at */
92206){
92207 memset(p, 0, sizeof(PmaWriter));
92208 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
92209 if( !p->aBuffer ){
92210 p->eFWErr = SQLITE_NOMEM_BKPT;
92211 }else{
92212 p->iBufEnd = p->iBufStart = (iStart % nBuf);
92213 p->iWriteOff = iStart - p->iBufStart;
92214 p->nBuffer = nBuf;
92215 p->pFd = pFd;
92216 }
92217}
92218
92219/*
92220** Write nData bytes of data to the PMA. Return SQLITE_OK
92221** if successful, or an SQLite error code if an error occurs.
92222*/
92223static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
92224 int nRem = nData;
92225 while( nRem>0 && p->eFWErr==0 ){
92226 int nCopy = nRem;
92227 if( nCopy>(p->nBuffer - p->iBufEnd) ){
92228 nCopy = p->nBuffer - p->iBufEnd;
92229 }
92230
92231 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
92232 p->iBufEnd += nCopy;
92233 if( p->iBufEnd==p->nBuffer ){
92234 p->eFWErr = sqlite3OsWrite(p->pFd,
92235 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
92236 p->iWriteOff + p->iBufStart
92237 );
92238 p->iBufStart = p->iBufEnd = 0;
92239 p->iWriteOff += p->nBuffer;
92240 }
92241 assert( p->iBufEnd<p->nBuffer );
92242
92243 nRem -= nCopy;
92244 }
92245}
92246
92247/*
92248** Flush any buffered data to disk and clean up the PMA-writer object.
92249** The results of using the PMA-writer after this call are undefined.
92250** Return SQLITE_OK if flushing the buffered data succeeds or is not
92251** required. Otherwise, return an SQLite error code.
92252**
92253** Before returning, set *piEof to the offset immediately following the
92254** last byte written to the file.
92255*/
92256static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
92257 int rc;
92258 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
92259 p->eFWErr = sqlite3OsWrite(p->pFd,
92260 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
92261 p->iWriteOff + p->iBufStart
92262 );
92263 }
92264 *piEof = (p->iWriteOff + p->iBufEnd);
92265 sqlite3_free(p->aBuffer);
92266 rc = p->eFWErr;
92267 memset(p, 0, sizeof(PmaWriter));
92268 return rc;
92269}
92270
92271/*
92272** Write value iVal encoded as a varint to the PMA. Return
92273** SQLITE_OK if successful, or an SQLite error code if an error occurs.
92274*/
92275static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){
92276 int nByte;
92277 u8 aByte[10];
92278 nByte = sqlite3PutVarint(aByte, iVal);
92279 vdbePmaWriteBlob(p, aByte, nByte);
92280}
92281
92282/*
92283** Write the current contents of in-memory linked-list pList to a level-0
92284** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
92285** successful, or an SQLite error code otherwise.
92286**
92287** The format of a PMA is:
92288**
92289** * A varint. This varint contains the total number of bytes of content
92290** in the PMA (not including the varint itself).
92291**
92292** * One or more records packed end-to-end in order of ascending keys.
92293** Each record consists of a varint followed by a blob of data (the
92294** key). The varint is the number of bytes in the blob of data.
92295*/
92296static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
92297 sqlite3 *db = pTask->pSorter->db;
92298 int rc = SQLITE_OK; /* Return code */
92299 PmaWriter writer; /* Object used to write to the file */
92300
92301#ifdef SQLITE_DEBUG
92302 /* Set iSz to the expected size of file pTask->file after writing the PMA.
92303 ** This is used by an assert() statement at the end of this function. */
92304 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
92305#endif
92306
92307 vdbeSorterWorkDebug(pTask, "enter");
92308 memset(&writer, 0, sizeof(PmaWriter));
92309 assert( pList->szPMA>0 );
92310
92311 /* If the first temporary PMA file has not been opened, open it now. */
92312 if( pTask->file.pFd==0 ){
92313 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
92314 assert( rc!=SQLITE_OK || pTask->file.pFd );
92315 assert( pTask->file.iEof==0 );
92316 assert( pTask->nPMA==0 );
92317 }
92318
92319 /* Try to get the file to memory map */
92320 if( rc==SQLITE_OK ){
92321 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
92322 }
92323
92324 /* Sort the list */
92325 if( rc==SQLITE_OK ){
92326 rc = vdbeSorterSort(pTask, pList);
92327 }
92328
92329 if( rc==SQLITE_OK ){
92330 SorterRecord *p;
92331 SorterRecord *pNext = 0;
92332
92333 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
92334 pTask->file.iEof);
92335 pTask->nPMA++;
92336 vdbePmaWriteVarint(&writer, pList->szPMA);
92337 for(p=pList->pList; p; p=pNext){
92338 pNext = p->u.pNext;
92339 vdbePmaWriteVarint(&writer, p->nVal);
92340 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
92341 if( pList->aMemory==0 ) sqlite3_free(p);
92342 }
92343 pList->pList = p;
92344 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
92345 }
92346
92347 vdbeSorterWorkDebug(pTask, "exit");
92348 assert( rc!=SQLITE_OK || pList->pList==0 );
92349 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
92350 return rc;
92351}
92352
92353/*
92354** Advance the MergeEngine to its next entry.
92355** Set *pbEof to true there is no next entry because
92356** the MergeEngine has reached the end of all its inputs.
92357**
92358** Return SQLITE_OK if successful or an error code if an error occurs.
92359*/
92360static int vdbeMergeEngineStep(
92361 MergeEngine *pMerger, /* The merge engine to advance to the next row */
92362 int *pbEof /* Set TRUE at EOF. Set false for more content */
92363){
92364 int rc;
92365 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
92366 SortSubtask *pTask = pMerger->pTask;
92367
92368 /* Advance the current PmaReader */
92369 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
92370
92371 /* Update contents of aTree[] */
92372 if( rc==SQLITE_OK ){
92373 int i; /* Index of aTree[] to recalculate */
92374 PmaReader *pReadr1; /* First PmaReader to compare */
92375 PmaReader *pReadr2; /* Second PmaReader to compare */
92376 int bCached = 0;
92377
92378 /* Find the first two PmaReaders to compare. The one that was just
92379 ** advanced (iPrev) and the one next to it in the array. */
92380 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
92381 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
92382
92383 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
92384 /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
92385 int iRes;
92386 if( pReadr1->pFd==0 ){
92387 iRes = +1;
92388 }else if( pReadr2->pFd==0 ){
92389 iRes = -1;
92390 }else{
92391 iRes = pTask->xCompare(pTask, &bCached,
92392 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
92393 );
92394 }
92395
92396 /* If pReadr1 contained the smaller value, set aTree[i] to its index.
92397 ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
92398 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
92399 ** pKey2 to point to the record belonging to pReadr2.
92400 **
92401 ** Alternatively, if pReadr2 contains the smaller of the two values,
92402 ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
92403 ** was actually called above, then pTask->pUnpacked now contains
92404 ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
92405 ** vdbeSorterCompare() from decoding pReadr2 again.
92406 **
92407 ** If the two values were equal, then the value from the oldest
92408 ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
92409 ** is sorted from oldest to newest, so pReadr1 contains older values
92410 ** than pReadr2 iff (pReadr1<pReadr2). */
92411 if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
92412 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
92413 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
92414 bCached = 0;
92415 }else{
92416 if( pReadr1->pFd ) bCached = 0;
92417 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
92418 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
92419 }
92420 }
92421 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
92422 }
92423
92424 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
92425}
92426
92427#if SQLITE_MAX_WORKER_THREADS>0
92428/*
92429** The main routine for background threads that write level-0 PMAs.
92430*/
92431static void *vdbeSorterFlushThread(void *pCtx){
92432 SortSubtask *pTask = (SortSubtask*)pCtx;
92433 int rc; /* Return code */
92434 assert( pTask->bDone==0 );
92435 rc = vdbeSorterListToPMA(pTask, &pTask->list);
92436 pTask->bDone = 1;
92437 return SQLITE_INT_TO_PTR(rc);
92438}
92439#endif /* SQLITE_MAX_WORKER_THREADS>0 */
92440
92441/*
92442** Flush the current contents of VdbeSorter.list to a new PMA, possibly
92443** using a background thread.
92444*/
92445static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
92446#if SQLITE_MAX_WORKER_THREADS==0
92447 pSorter->bUsePMA = 1;
92448 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
92449#else
92450 int rc = SQLITE_OK;
92451 int i;
92452 SortSubtask *pTask = 0; /* Thread context used to create new PMA */
92453 int nWorker = (pSorter->nTask-1);
92454
92455 /* Set the flag to indicate that at least one PMA has been written.
92456 ** Or will be, anyhow. */
92457 pSorter->bUsePMA = 1;
92458
92459 /* Select a sub-task to sort and flush the current list of in-memory
92460 ** records to disk. If the sorter is running in multi-threaded mode,
92461 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
92462 ** the background thread from a sub-tasks previous turn is still running,
92463 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
92464 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
92465 ** sub-tasks are prefered as they use background threads - the final
92466 ** sub-task uses the main thread. */
92467 for(i=0; i<nWorker; i++){
92468 int iTest = (pSorter->iPrev + i + 1) % nWorker;
92469 pTask = &pSorter->aTask[iTest];
92470 if( pTask->bDone ){
92471 rc = vdbeSorterJoinThread(pTask);
92472 }
92473 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
92474 }
92475
92476 if( rc==SQLITE_OK ){
92477 if( i==nWorker ){
92478 /* Use the foreground thread for this operation */
92479 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
92480 }else{
92481 /* Launch a background thread for this operation */
92482 u8 *aMem = pTask->list.aMemory;
92483 void *pCtx = (void*)pTask;
92484
92485 assert( pTask->pThread==0 && pTask->bDone==0 );
92486 assert( pTask->list.pList==0 );
92487 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
92488
92489 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
92490 pTask->list = pSorter->list;
92491 pSorter->list.pList = 0;
92492 pSorter->list.szPMA = 0;
92493 if( aMem ){
92494 pSorter->list.aMemory = aMem;
92495 pSorter->nMemory = sqlite3MallocSize(aMem);
92496 }else if( pSorter->list.aMemory ){
92497 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
92498 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
92499 }
92500
92501 rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx);
92502 }
92503 }
92504
92505 return rc;
92506#endif /* SQLITE_MAX_WORKER_THREADS!=0 */
92507}
92508
92509/*
92510** Add a record to the sorter.
92511*/
92512SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
92513 const VdbeCursor *pCsr, /* Sorter cursor */
92514 Mem *pVal /* Memory cell containing record */
92515){
92516 VdbeSorter *pSorter;
92517 int rc = SQLITE_OK; /* Return Code */
92518 SorterRecord *pNew; /* New list element */
92519 int bFlush; /* True to flush contents of memory to PMA */
92520 int nReq; /* Bytes of memory required */
92521 int nPMA; /* Bytes of PMA space required */
92522 int t; /* serial type of first record field */
92523
92524 assert( pCsr->eCurType==CURTYPE_SORTER );
92525 pSorter = pCsr->uc.pSorter;
92526 getVarint32((const u8*)&pVal->z[1], t);
92527 if( t>0 && t<10 && t!=7 ){
92528 pSorter->typeMask &= SORTER_TYPE_INTEGER;
92529 }else if( t>10 && (t & 0x01) ){
92530 pSorter->typeMask &= SORTER_TYPE_TEXT;
92531 }else{
92532 pSorter->typeMask = 0;
92533 }
92534
92535 assert( pSorter );
92536
92537 /* Figure out whether or not the current contents of memory should be
92538 ** flushed to a PMA before continuing. If so, do so.
92539 **
92540 ** If using the single large allocation mode (pSorter->aMemory!=0), then
92541 ** flush the contents of memory to a new PMA if (a) at least one value is
92542 ** already in memory and (b) the new value will not fit in memory.
92543 **
92544 ** Or, if using separate allocations for each record, flush the contents
92545 ** of memory to a PMA if either of the following are true:
92546 **
92547 ** * The total memory allocated for the in-memory list is greater
92548 ** than (page-size * cache-size), or
92549 **
92550 ** * The total memory allocated for the in-memory list is greater
92551 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
92552 */
92553 nReq = pVal->n + sizeof(SorterRecord);
92554 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
92555 if( pSorter->mxPmaSize ){
92556 if( pSorter->list.aMemory ){
92557 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
92558 }else{
92559 bFlush = (
92560 (pSorter->list.szPMA > pSorter->mxPmaSize)
92561 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
92562 );
92563 }
92564 if( bFlush ){
92565 rc = vdbeSorterFlushPMA(pSorter);
92566 pSorter->list.szPMA = 0;
92567 pSorter->iMemory = 0;
92568 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
92569 }
92570 }
92571
92572 pSorter->list.szPMA += nPMA;
92573 if( nPMA>pSorter->mxKeysize ){
92574 pSorter->mxKeysize = nPMA;
92575 }
92576
92577 if( pSorter->list.aMemory ){
92578 int nMin = pSorter->iMemory + nReq;
92579
92580 if( nMin>pSorter->nMemory ){
92581 u8 *aNew;
92582 int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
92583 int nNew = pSorter->nMemory * 2;
92584 while( nNew < nMin ) nNew = nNew*2;
92585 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
92586 if( nNew < nMin ) nNew = nMin;
92587
92588 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
92589 if( !aNew ) return SQLITE_NOMEM_BKPT;
92590 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
92591 pSorter->list.aMemory = aNew;
92592 pSorter->nMemory = nNew;
92593 }
92594
92595 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
92596 pSorter->iMemory += ROUND8(nReq);
92597 if( pSorter->list.pList ){
92598 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
92599 }
92600 }else{
92601 pNew = (SorterRecord *)sqlite3Malloc(nReq);
92602 if( pNew==0 ){
92603 return SQLITE_NOMEM_BKPT;
92604 }
92605 pNew->u.pNext = pSorter->list.pList;
92606 }
92607
92608 memcpy(SRVAL(pNew), pVal->z, pVal->n);
92609 pNew->nVal = pVal->n;
92610 pSorter->list.pList = pNew;
92611
92612 return rc;
92613}
92614
92615/*
92616** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
92617** of the data stored in aFile[1] is the same as that used by regular PMAs,
92618** except that the number-of-bytes varint is omitted from the start.
92619*/
92620static int vdbeIncrPopulate(IncrMerger *pIncr){
92621 int rc = SQLITE_OK;
92622 int rc2;
92623 i64 iStart = pIncr->iStartOff;
92624 SorterFile *pOut = &pIncr->aFile[1];
92625 SortSubtask *pTask = pIncr->pTask;
92626 MergeEngine *pMerger = pIncr->pMerger;
92627 PmaWriter writer;
92628 assert( pIncr->bEof==0 );
92629
92630 vdbeSorterPopulateDebug(pTask, "enter");
92631
92632 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
92633 while( rc==SQLITE_OK ){
92634 int dummy;
92635 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
92636 int nKey = pReader->nKey;
92637 i64 iEof = writer.iWriteOff + writer.iBufEnd;
92638
92639 /* Check if the output file is full or if the input has been exhausted.
92640 ** In either case exit the loop. */
92641 if( pReader->pFd==0 ) break;
92642 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
92643
92644 /* Write the next key to the output. */
92645 vdbePmaWriteVarint(&writer, nKey);
92646 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
92647 assert( pIncr->pMerger->pTask==pTask );
92648 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
92649 }
92650
92651 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
92652 if( rc==SQLITE_OK ) rc = rc2;
92653 vdbeSorterPopulateDebug(pTask, "exit");
92654 return rc;
92655}
92656
92657#if SQLITE_MAX_WORKER_THREADS>0
92658/*
92659** The main routine for background threads that populate aFile[1] of
92660** multi-threaded IncrMerger objects.
92661*/
92662static void *vdbeIncrPopulateThread(void *pCtx){
92663 IncrMerger *pIncr = (IncrMerger*)pCtx;
92664 void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) );
92665 pIncr->pTask->bDone = 1;
92666 return pRet;
92667}
92668
92669/*
92670** Launch a background thread to populate aFile[1] of pIncr.
92671*/
92672static int vdbeIncrBgPopulate(IncrMerger *pIncr){
92673 void *p = (void*)pIncr;
92674 assert( pIncr->bUseThread );
92675 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
92676}
92677#endif
92678
92679/*
92680** This function is called when the PmaReader corresponding to pIncr has
92681** finished reading the contents of aFile[0]. Its purpose is to "refill"
92682** aFile[0] such that the PmaReader should start rereading it from the
92683** beginning.
92684**
92685** For single-threaded objects, this is accomplished by literally reading
92686** keys from pIncr->pMerger and repopulating aFile[0].
92687**
92688** For multi-threaded objects, all that is required is to wait until the
92689** background thread is finished (if it is not already) and then swap
92690** aFile[0] and aFile[1] in place. If the contents of pMerger have not
92691** been exhausted, this function also launches a new background thread
92692** to populate the new aFile[1].
92693**
92694** SQLITE_OK is returned on success, or an SQLite error code otherwise.
92695*/
92696static int vdbeIncrSwap(IncrMerger *pIncr){
92697 int rc = SQLITE_OK;
92698
92699#if SQLITE_MAX_WORKER_THREADS>0
92700 if( pIncr->bUseThread ){
92701 rc = vdbeSorterJoinThread(pIncr->pTask);
92702
92703 if( rc==SQLITE_OK ){
92704 SorterFile f0 = pIncr->aFile[0];
92705 pIncr->aFile[0] = pIncr->aFile[1];
92706 pIncr->aFile[1] = f0;
92707 }
92708
92709 if( rc==SQLITE_OK ){
92710 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
92711 pIncr->bEof = 1;
92712 }else{
92713 rc = vdbeIncrBgPopulate(pIncr);
92714 }
92715 }
92716 }else
92717#endif
92718 {
92719 rc = vdbeIncrPopulate(pIncr);
92720 pIncr->aFile[0] = pIncr->aFile[1];
92721 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
92722 pIncr->bEof = 1;
92723 }
92724 }
92725
92726 return rc;
92727}
92728
92729/*
92730** Allocate and return a new IncrMerger object to read data from pMerger.
92731**
92732** If an OOM condition is encountered, return NULL. In this case free the
92733** pMerger argument before returning.
92734*/
92735static int vdbeIncrMergerNew(
92736 SortSubtask *pTask, /* The thread that will be using the new IncrMerger */
92737 MergeEngine *pMerger, /* The MergeEngine that the IncrMerger will control */
92738 IncrMerger **ppOut /* Write the new IncrMerger here */
92739){
92740 int rc = SQLITE_OK;
92741 IncrMerger *pIncr = *ppOut = (IncrMerger*)
92742 (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr)));
92743 if( pIncr ){
92744 pIncr->pMerger = pMerger;
92745 pIncr->pTask = pTask;
92746 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
92747 pTask->file2.iEof += pIncr->mxSz;
92748 }else{
92749 vdbeMergeEngineFree(pMerger);
92750 rc = SQLITE_NOMEM_BKPT;
92751 }
92752 return rc;
92753}
92754
92755#if SQLITE_MAX_WORKER_THREADS>0
92756/*
92757** Set the "use-threads" flag on object pIncr.
92758*/
92759static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
92760 pIncr->bUseThread = 1;
92761 pIncr->pTask->file2.iEof -= pIncr->mxSz;
92762}
92763#endif /* SQLITE_MAX_WORKER_THREADS>0 */
92764
92765
92766
92767/*
92768** Recompute pMerger->aTree[iOut] by comparing the next keys on the
92769** two PmaReaders that feed that entry. Neither of the PmaReaders
92770** are advanced. This routine merely does the comparison.
92771*/
92772static void vdbeMergeEngineCompare(
92773 MergeEngine *pMerger, /* Merge engine containing PmaReaders to compare */
92774 int iOut /* Store the result in pMerger->aTree[iOut] */
92775){
92776 int i1;
92777 int i2;
92778 int iRes;
92779 PmaReader *p1;
92780 PmaReader *p2;
92781
92782 assert( iOut<pMerger->nTree && iOut>0 );
92783
92784 if( iOut>=(pMerger->nTree/2) ){
92785 i1 = (iOut - pMerger->nTree/2) * 2;
92786 i2 = i1 + 1;
92787 }else{
92788 i1 = pMerger->aTree[iOut*2];
92789 i2 = pMerger->aTree[iOut*2+1];
92790 }
92791
92792 p1 = &pMerger->aReadr[i1];
92793 p2 = &pMerger->aReadr[i2];
92794
92795 if( p1->pFd==0 ){
92796 iRes = i2;
92797 }else if( p2->pFd==0 ){
92798 iRes = i1;
92799 }else{
92800 SortSubtask *pTask = pMerger->pTask;
92801 int bCached = 0;
92802 int res;
92803 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
92804 res = pTask->xCompare(
92805 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
92806 );
92807 if( res<=0 ){
92808 iRes = i1;
92809 }else{
92810 iRes = i2;
92811 }
92812 }
92813
92814 pMerger->aTree[iOut] = iRes;
92815}
92816
92817/*
92818** Allowed values for the eMode parameter to vdbeMergeEngineInit()
92819** and vdbePmaReaderIncrMergeInit().
92820**
92821** Only INCRINIT_NORMAL is valid in single-threaded builds (when
92822** SQLITE_MAX_WORKER_THREADS==0). The other values are only used
92823** when there exists one or more separate worker threads.
92824*/
92825#define INCRINIT_NORMAL 0
92826#define INCRINIT_TASK 1
92827#define INCRINIT_ROOT 2
92828
92829/*
92830** Forward reference required as the vdbeIncrMergeInit() and
92831** vdbePmaReaderIncrInit() routines are called mutually recursively when
92832** building a merge tree.
92833*/
92834static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
92835
92836/*
92837** Initialize the MergeEngine object passed as the second argument. Once this
92838** function returns, the first key of merged data may be read from the
92839** MergeEngine object in the usual fashion.
92840**
92841** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
92842** objects attached to the PmaReader objects that the merger reads from have
92843** already been populated, but that they have not yet populated aFile[0] and
92844** set the PmaReader objects up to read from it. In this case all that is
92845** required is to call vdbePmaReaderNext() on each PmaReader to point it at
92846** its first key.
92847**
92848** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
92849** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
92850** to pMerger.
92851**
92852** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
92853*/
92854static int vdbeMergeEngineInit(
92855 SortSubtask *pTask, /* Thread that will run pMerger */
92856 MergeEngine *pMerger, /* MergeEngine to initialize */
92857 int eMode /* One of the INCRINIT_XXX constants */
92858){
92859 int rc = SQLITE_OK; /* Return code */
92860 int i; /* For looping over PmaReader objects */
92861 int nTree; /* Number of subtrees to merge */
92862
92863 /* Failure to allocate the merge would have been detected prior to
92864 ** invoking this routine */
92865 assert( pMerger!=0 );
92866
92867 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
92868 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
92869
92870 /* Verify that the MergeEngine is assigned to a single thread */
92871 assert( pMerger->pTask==0 );
92872 pMerger->pTask = pTask;
92873
92874 nTree = pMerger->nTree;
92875 for(i=0; i<nTree; i++){
92876 if( SQLITE_MAX_WORKER_THREADS>0 && eMode==INCRINIT_ROOT ){
92877 /* PmaReaders should be normally initialized in order, as if they are
92878 ** reading from the same temp file this makes for more linear file IO.
92879 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
92880 ** in use it will block the vdbePmaReaderNext() call while it uses
92881 ** the main thread to fill its buffer. So calling PmaReaderNext()
92882 ** on this PmaReader before any of the multi-threaded PmaReaders takes
92883 ** better advantage of multi-processor hardware. */
92884 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
92885 }else{
92886 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
92887 }
92888 if( rc!=SQLITE_OK ) return rc;
92889 }
92890
92891 for(i=pMerger->nTree-1; i>0; i--){
92892 vdbeMergeEngineCompare(pMerger, i);
92893 }
92894 return pTask->pUnpacked->errCode;
92895}
92896
92897/*
92898** The PmaReader passed as the first argument is guaranteed to be an
92899** incremental-reader (pReadr->pIncr!=0). This function serves to open
92900** and/or initialize the temp file related fields of the IncrMerge
92901** object at (pReadr->pIncr).
92902**
92903** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
92904** in the sub-tree headed by pReadr are also initialized. Data is then
92905** loaded into the buffers belonging to pReadr and it is set to point to
92906** the first key in its range.
92907**
92908** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
92909** to be a multi-threaded PmaReader and this function is being called in a
92910** background thread. In this case all PmaReaders in the sub-tree are
92911** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
92912** pReadr is populated. However, pReadr itself is not set up to point
92913** to its first key. A call to vdbePmaReaderNext() is still required to do
92914** that.
92915**
92916** The reason this function does not call vdbePmaReaderNext() immediately
92917** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has
92918** to block on thread (pTask->thread) before accessing aFile[1]. But, since
92919** this entire function is being run by thread (pTask->thread), that will
92920** lead to the current background thread attempting to join itself.
92921**
92922** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed
92923** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
92924** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
92925** In this case vdbePmaReaderNext() is called on all child PmaReaders and
92926** the current PmaReader set to point to the first key in its range.
92927**
92928** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
92929*/
92930static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
92931 int rc = SQLITE_OK;
92932 IncrMerger *pIncr = pReadr->pIncr;
92933 SortSubtask *pTask = pIncr->pTask;
92934 sqlite3 *db = pTask->pSorter->db;
92935
92936 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
92937 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
92938
92939 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
92940
92941 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
92942 ** requires two temp files to itself, whereas a single-threaded object
92943 ** only requires a region of pTask->file2. */
92944 if( rc==SQLITE_OK ){
92945 int mxSz = pIncr->mxSz;
92946#if SQLITE_MAX_WORKER_THREADS>0
92947 if( pIncr->bUseThread ){
92948 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
92949 if( rc==SQLITE_OK ){
92950 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
92951 }
92952 }else
92953#endif
92954 /*if( !pIncr->bUseThread )*/{
92955 if( pTask->file2.pFd==0 ){
92956 assert( pTask->file2.iEof>0 );
92957 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
92958 pTask->file2.iEof = 0;
92959 }
92960 if( rc==SQLITE_OK ){
92961 pIncr->aFile[1].pFd = pTask->file2.pFd;
92962 pIncr->iStartOff = pTask->file2.iEof;
92963 pTask->file2.iEof += mxSz;
92964 }
92965 }
92966 }
92967
92968#if SQLITE_MAX_WORKER_THREADS>0
92969 if( rc==SQLITE_OK && pIncr->bUseThread ){
92970 /* Use the current thread to populate aFile[1], even though this
92971 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
92972 ** then this function is already running in background thread
92973 ** pIncr->pTask->thread.
92974 **
92975 ** If this is the INCRINIT_ROOT object, then it is running in the
92976 ** main VDBE thread. But that is Ok, as that thread cannot return
92977 ** control to the VDBE or proceed with anything useful until the
92978 ** first results are ready from this merger object anyway.
92979 */
92980 assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK );
92981 rc = vdbeIncrPopulate(pIncr);
92982 }
92983#endif
92984
92985 if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){
92986 rc = vdbePmaReaderNext(pReadr);
92987 }
92988
92989 return rc;
92990}
92991
92992#if SQLITE_MAX_WORKER_THREADS>0
92993/*
92994** The main routine for vdbePmaReaderIncrMergeInit() operations run in
92995** background threads.
92996*/
92997static void *vdbePmaReaderBgIncrInit(void *pCtx){
92998 PmaReader *pReader = (PmaReader*)pCtx;
92999 void *pRet = SQLITE_INT_TO_PTR(
93000 vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)
93001 );
93002 pReader->pIncr->pTask->bDone = 1;
93003 return pRet;
93004}
93005#endif
93006
93007/*
93008** If the PmaReader passed as the first argument is not an incremental-reader
93009** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
93010** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
93011** this routine to initialize the incremental merge.
93012**
93013** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
93014** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
93015** Or, if the IncrMerger is single threaded, the same function is called
93016** using the current thread.
93017*/
93018static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
93019 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
93020 int rc = SQLITE_OK; /* Return code */
93021 if( pIncr ){
93022#if SQLITE_MAX_WORKER_THREADS>0
93023 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
93024 if( pIncr->bUseThread ){
93025 void *pCtx = (void*)pReadr;
93026 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
93027 }else
93028#endif
93029 {
93030 rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
93031 }
93032 }
93033 return rc;
93034}
93035
93036/*
93037** Allocate a new MergeEngine object to merge the contents of nPMA level-0
93038** PMAs from pTask->file. If no error occurs, set *ppOut to point to
93039** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
93040** to NULL and return an SQLite error code.
93041**
93042** When this function is called, *piOffset is set to the offset of the
93043** first PMA to read from pTask->file. Assuming no error occurs, it is
93044** set to the offset immediately following the last byte of the last
93045** PMA before returning. If an error does occur, then the final value of
93046** *piOffset is undefined.
93047*/
93048static int vdbeMergeEngineLevel0(
93049 SortSubtask *pTask, /* Sorter task to read from */
93050 int nPMA, /* Number of PMAs to read */
93051 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
93052 MergeEngine **ppOut /* OUT: New merge-engine */
93053){
93054 MergeEngine *pNew; /* Merge engine to return */
93055 i64 iOff = *piOffset;
93056 int i;
93057 int rc = SQLITE_OK;
93058
93059 *ppOut = pNew = vdbeMergeEngineNew(nPMA);
93060 if( pNew==0 ) rc = SQLITE_NOMEM_BKPT;
93061
93062 for(i=0; i<nPMA && rc==SQLITE_OK; i++){
93063 i64 nDummy = 0;
93064 PmaReader *pReadr = &pNew->aReadr[i];
93065 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
93066 iOff = pReadr->iEof;
93067 }
93068
93069 if( rc!=SQLITE_OK ){
93070 vdbeMergeEngineFree(pNew);
93071 *ppOut = 0;
93072 }
93073 *piOffset = iOff;
93074 return rc;
93075}
93076
93077/*
93078** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
93079** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes.
93080**
93081** i.e.
93082**
93083** nPMA<=16 -> TreeDepth() == 0
93084** nPMA<=256 -> TreeDepth() == 1
93085** nPMA<=65536 -> TreeDepth() == 2
93086*/
93087static int vdbeSorterTreeDepth(int nPMA){
93088 int nDepth = 0;
93089 i64 nDiv = SORTER_MAX_MERGE_COUNT;
93090 while( nDiv < (i64)nPMA ){
93091 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
93092 nDepth++;
93093 }
93094 return nDepth;
93095}
93096
93097/*
93098** pRoot is the root of an incremental merge-tree with depth nDepth (according
93099** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the
93100** tree, counting from zero. This function adds pLeaf to the tree.
93101**
93102** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error
93103** code is returned and pLeaf is freed.
93104*/
93105static int vdbeSorterAddToTree(
93106 SortSubtask *pTask, /* Task context */
93107 int nDepth, /* Depth of tree according to TreeDepth() */
93108 int iSeq, /* Sequence number of leaf within tree */
93109 MergeEngine *pRoot, /* Root of tree */
93110 MergeEngine *pLeaf /* Leaf to add to tree */
93111){
93112 int rc = SQLITE_OK;
93113 int nDiv = 1;
93114 int i;
93115 MergeEngine *p = pRoot;
93116 IncrMerger *pIncr;
93117
93118 rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr);
93119
93120 for(i=1; i<nDepth; i++){
93121 nDiv = nDiv * SORTER_MAX_MERGE_COUNT;
93122 }
93123
93124 for(i=1; i<nDepth && rc==SQLITE_OK; i++){
93125 int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT;
93126 PmaReader *pReadr = &p->aReadr[iIter];
93127
93128 if( pReadr->pIncr==0 ){
93129 MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
93130 if( pNew==0 ){
93131 rc = SQLITE_NOMEM_BKPT;
93132 }else{
93133 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
93134 }
93135 }
93136 if( rc==SQLITE_OK ){
93137 p = pReadr->pIncr->pMerger;
93138 nDiv = nDiv / SORTER_MAX_MERGE_COUNT;
93139 }
93140 }
93141
93142 if( rc==SQLITE_OK ){
93143 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
93144 }else{
93145 vdbeIncrFree(pIncr);
93146 }
93147 return rc;
93148}
93149
93150/*
93151** This function is called as part of a SorterRewind() operation on a sorter
93152** that has already written two or more level-0 PMAs to one or more temp
93153** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
93154** can be used to incrementally merge all PMAs on disk.
93155**
93156** If successful, SQLITE_OK is returned and *ppOut set to point to the
93157** MergeEngine object at the root of the tree before returning. Or, if an
93158** error occurs, an SQLite error code is returned and the final value
93159** of *ppOut is undefined.
93160*/
93161static int vdbeSorterMergeTreeBuild(
93162 VdbeSorter *pSorter, /* The VDBE cursor that implements the sort */
93163 MergeEngine **ppOut /* Write the MergeEngine here */
93164){
93165 MergeEngine *pMain = 0;
93166 int rc = SQLITE_OK;
93167 int iTask;
93168
93169#if SQLITE_MAX_WORKER_THREADS>0
93170 /* If the sorter uses more than one task, then create the top-level
93171 ** MergeEngine here. This MergeEngine will read data from exactly
93172 ** one PmaReader per sub-task. */
93173 assert( pSorter->bUseThreads || pSorter->nTask==1 );
93174 if( pSorter->nTask>1 ){
93175 pMain = vdbeMergeEngineNew(pSorter->nTask);
93176 if( pMain==0 ) rc = SQLITE_NOMEM_BKPT;
93177 }
93178#endif
93179
93180 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
93181 SortSubtask *pTask = &pSorter->aTask[iTask];
93182 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
93183 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
93184 MergeEngine *pRoot = 0; /* Root node of tree for this task */
93185 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
93186 i64 iReadOff = 0;
93187
93188 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
93189 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
93190 }else{
93191 int i;
93192 int iSeq = 0;
93193 pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
93194 if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT;
93195 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
93196 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
93197 int nReader; /* Number of level-0 PMAs to merge */
93198
93199 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
93200 rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger);
93201 if( rc==SQLITE_OK ){
93202 rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger);
93203 }
93204 }
93205 }
93206
93207 if( rc==SQLITE_OK ){
93208#if SQLITE_MAX_WORKER_THREADS>0
93209 if( pMain!=0 ){
93210 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
93211 }else
93212#endif
93213 {
93214 assert( pMain==0 );
93215 pMain = pRoot;
93216 }
93217 }else{
93218 vdbeMergeEngineFree(pRoot);
93219 }
93220 }
93221 }
93222
93223 if( rc!=SQLITE_OK ){
93224 vdbeMergeEngineFree(pMain);
93225 pMain = 0;
93226 }
93227 *ppOut = pMain;
93228 return rc;
93229}
93230
93231/*
93232** This function is called as part of an sqlite3VdbeSorterRewind() operation
93233** on a sorter that has written two or more PMAs to temporary files. It sets
93234** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
93235** (for multi-threaded sorters) so that it can be used to iterate through
93236** all records stored in the sorter.
93237**
93238** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
93239*/
93240static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
93241 int rc; /* Return code */
93242 SortSubtask *pTask0 = &pSorter->aTask[0];
93243 MergeEngine *pMain = 0;
93244#if SQLITE_MAX_WORKER_THREADS
93245 sqlite3 *db = pTask0->pSorter->db;
93246 int i;
93247 SorterCompare xCompare = vdbeSorterGetCompare(pSorter);
93248 for(i=0; i<pSorter->nTask; i++){
93249 pSorter->aTask[i].xCompare = xCompare;
93250 }
93251#endif
93252
93253 rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
93254 if( rc==SQLITE_OK ){
93255#if SQLITE_MAX_WORKER_THREADS
93256 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
93257 if( pSorter->bUseThreads ){
93258 int iTask;
93259 PmaReader *pReadr = 0;
93260 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
93261 rc = vdbeSortAllocUnpacked(pLast);
93262 if( rc==SQLITE_OK ){
93263 pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader));
93264 pSorter->pReader = pReadr;
93265 if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT;
93266 }
93267 if( rc==SQLITE_OK ){
93268 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
93269 if( rc==SQLITE_OK ){
93270 vdbeIncrMergerSetThreads(pReadr->pIncr);
93271 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
93272 IncrMerger *pIncr;
93273 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
93274 vdbeIncrMergerSetThreads(pIncr);
93275 assert( pIncr->pTask!=pLast );
93276 }
93277 }
93278 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
93279 /* Check that:
93280 **
93281 ** a) The incremental merge object is configured to use the
93282 ** right task, and
93283 ** b) If it is using task (nTask-1), it is configured to run
93284 ** in single-threaded mode. This is important, as the
93285 ** root merge (INCRINIT_ROOT) will be using the same task
93286 ** object.
93287 */
93288 PmaReader *p = &pMain->aReadr[iTask];
93289 assert( p->pIncr==0 || (
93290 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
93291 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
93292 ));
93293 rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK);
93294 }
93295 }
93296 pMain = 0;
93297 }
93298 if( rc==SQLITE_OK ){
93299 rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT);
93300 }
93301 }else
93302#endif
93303 {
93304 rc = vdbeMergeEngineInit(pTask0, pMain, INCRINIT_NORMAL);
93305 pSorter->pMerger = pMain;
93306 pMain = 0;
93307 }
93308 }
93309
93310 if( rc!=SQLITE_OK ){
93311 vdbeMergeEngineFree(pMain);
93312 }
93313 return rc;
93314}
93315
93316
93317/*
93318** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
93319** this function is called to prepare for iterating through the records
93320** in sorted order.
93321*/
93322SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
93323 VdbeSorter *pSorter;
93324 int rc = SQLITE_OK; /* Return code */
93325
93326 assert( pCsr->eCurType==CURTYPE_SORTER );
93327 pSorter = pCsr->uc.pSorter;
93328 assert( pSorter );
93329
93330 /* If no data has been written to disk, then do not do so now. Instead,
93331 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
93332 ** from the in-memory list. */
93333 if( pSorter->bUsePMA==0 ){
93334 if( pSorter->list.pList ){
93335 *pbEof = 0;
93336 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
93337 }else{
93338 *pbEof = 1;
93339 }
93340 return rc;
93341 }
93342
93343 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
93344 ** function flushes the contents of memory to disk, it immediately always
93345 ** creates a new list consisting of a single key immediately afterwards.
93346 ** So the list is never empty at this point. */
93347 assert( pSorter->list.pList );
93348 rc = vdbeSorterFlushPMA(pSorter);
93349
93350 /* Join all threads */
93351 rc = vdbeSorterJoinAll(pSorter, rc);
93352
93353 vdbeSorterRewindDebug("rewind");
93354
93355 /* Assuming no errors have occurred, set up a merger structure to
93356 ** incrementally read and merge all remaining PMAs. */
93357 assert( pSorter->pReader==0 );
93358 if( rc==SQLITE_OK ){
93359 rc = vdbeSorterSetupMerge(pSorter);
93360 *pbEof = 0;
93361 }
93362
93363 vdbeSorterRewindDebug("rewinddone");
93364 return rc;
93365}
93366
93367/*
93368** Advance to the next element in the sorter. Return value:
93369**
93370** SQLITE_OK success
93371** SQLITE_DONE end of data
93372** otherwise some kind of error.
93373*/
93374SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr){
93375 VdbeSorter *pSorter;
93376 int rc; /* Return code */
93377
93378 assert( pCsr->eCurType==CURTYPE_SORTER );
93379 pSorter = pCsr->uc.pSorter;
93380 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
93381 if( pSorter->bUsePMA ){
93382 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
93383 assert( pSorter->bUseThreads==0 || pSorter->pReader );
93384 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
93385#if SQLITE_MAX_WORKER_THREADS>0
93386 if( pSorter->bUseThreads ){
93387 rc = vdbePmaReaderNext(pSorter->pReader);
93388 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
93389 }else
93390#endif
93391 /*if( !pSorter->bUseThreads )*/ {
93392 int res = 0;
93393 assert( pSorter->pMerger!=0 );
93394 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
93395 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
93396 if( rc==SQLITE_OK && res ) rc = SQLITE_DONE;
93397 }
93398 }else{
93399 SorterRecord *pFree = pSorter->list.pList;
93400 pSorter->list.pList = pFree->u.pNext;
93401 pFree->u.pNext = 0;
93402 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
93403 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
93404 }
93405 return rc;
93406}
93407
93408/*
93409** Return a pointer to a buffer owned by the sorter that contains the
93410** current key.
93411*/
93412static void *vdbeSorterRowkey(
93413 const VdbeSorter *pSorter, /* Sorter object */
93414 int *pnKey /* OUT: Size of current key in bytes */
93415){
93416 void *pKey;
93417 if( pSorter->bUsePMA ){
93418 PmaReader *pReader;
93419#if SQLITE_MAX_WORKER_THREADS>0
93420 if( pSorter->bUseThreads ){
93421 pReader = pSorter->pReader;
93422 }else
93423#endif
93424 /*if( !pSorter->bUseThreads )*/{
93425 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
93426 }
93427 *pnKey = pReader->nKey;
93428 pKey = pReader->aKey;
93429 }else{
93430 *pnKey = pSorter->list.pList->nVal;
93431 pKey = SRVAL(pSorter->list.pList);
93432 }
93433 return pKey;
93434}
93435
93436/*
93437** Copy the current sorter key into the memory cell pOut.
93438*/
93439SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
93440 VdbeSorter *pSorter;
93441 void *pKey; int nKey; /* Sorter key to copy into pOut */
93442
93443 assert( pCsr->eCurType==CURTYPE_SORTER );
93444 pSorter = pCsr->uc.pSorter;
93445 pKey = vdbeSorterRowkey(pSorter, &nKey);
93446 if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
93447 return SQLITE_NOMEM_BKPT;
93448 }
93449 pOut->n = nKey;
93450 MemSetTypeFlag(pOut, MEM_Blob);
93451 memcpy(pOut->z, pKey, nKey);
93452
93453 return SQLITE_OK;
93454}
93455
93456/*
93457** Compare the key in memory cell pVal with the key that the sorter cursor
93458** passed as the first argument currently points to. For the purposes of
93459** the comparison, ignore the rowid field at the end of each record.
93460**
93461** If the sorter cursor key contains any NULL values, consider it to be
93462** less than pVal. Even if pVal also contains NULL values.
93463**
93464** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
93465** Otherwise, set *pRes to a negative, zero or positive value if the
93466** key in pVal is smaller than, equal to or larger than the current sorter
93467** key.
93468**
93469** This routine forms the core of the OP_SorterCompare opcode, which in
93470** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
93471*/
93472SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
93473 const VdbeCursor *pCsr, /* Sorter cursor */
93474 Mem *pVal, /* Value to compare to current sorter key */
93475 int nKeyCol, /* Compare this many columns */
93476 int *pRes /* OUT: Result of comparison */
93477){
93478 VdbeSorter *pSorter;
93479 UnpackedRecord *r2;
93480 KeyInfo *pKeyInfo;
93481 int i;
93482 void *pKey; int nKey; /* Sorter key to compare pVal with */
93483
93484 assert( pCsr->eCurType==CURTYPE_SORTER );
93485 pSorter = pCsr->uc.pSorter;
93486 r2 = pSorter->pUnpacked;
93487 pKeyInfo = pCsr->pKeyInfo;
93488 if( r2==0 ){
93489 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
93490 if( r2==0 ) return SQLITE_NOMEM_BKPT;
93491 r2->nField = nKeyCol;
93492 }
93493 assert( r2->nField==nKeyCol );
93494
93495 pKey = vdbeSorterRowkey(pSorter, &nKey);
93496 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2);
93497 for(i=0; i<nKeyCol; i++){
93498 if( r2->aMem[i].flags & MEM_Null ){
93499 *pRes = -1;
93500 return SQLITE_OK;
93501 }
93502 }
93503
93504 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
93505 return SQLITE_OK;
93506}
93507
93508/************** End of vdbesort.c ********************************************/
93509/************** Begin file memjournal.c **************************************/
93510/*
93511** 2008 October 7
93512**
93513** The author disclaims copyright to this source code. In place of
93514** a legal notice, here is a blessing:
93515**
93516** May you do good and not evil.
93517** May you find forgiveness for yourself and forgive others.
93518** May you share freely, never taking more than you give.
93519**
93520*************************************************************************
93521**
93522** This file contains code use to implement an in-memory rollback journal.
93523** The in-memory rollback journal is used to journal transactions for
93524** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
93525**
93526** Update: The in-memory journal is also used to temporarily cache
93527** smaller journals that are not critical for power-loss recovery.
93528** For example, statement journals that are not too big will be held
93529** entirely in memory, thus reducing the number of file I/O calls, and
93530** more importantly, reducing temporary file creation events. If these
93531** journals become too large for memory, they are spilled to disk. But
93532** in the common case, they are usually small and no file I/O needs to
93533** occur.
93534*/
93535/* #include "sqliteInt.h" */
93536
93537/* Forward references to internal structures */
93538typedef struct MemJournal MemJournal;
93539typedef struct FilePoint FilePoint;
93540typedef struct FileChunk FileChunk;
93541
93542/*
93543** The rollback journal is composed of a linked list of these structures.
93544**
93545** The zChunk array is always at least 8 bytes in size - usually much more.
93546** Its actual size is stored in the MemJournal.nChunkSize variable.
93547*/
93548struct FileChunk {
93549 FileChunk *pNext; /* Next chunk in the journal */
93550 u8 zChunk[8]; /* Content of this chunk */
93551};
93552
93553/*
93554** By default, allocate this many bytes of memory for each FileChunk object.
93555*/
93556#define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024
93557
93558/*
93559** For chunk size nChunkSize, return the number of bytes that should
93560** be allocated for each FileChunk structure.
93561*/
93562#define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
93563
93564/*
93565** An instance of this object serves as a cursor into the rollback journal.
93566** The cursor can be either for reading or writing.
93567*/
93568struct FilePoint {
93569 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
93570 FileChunk *pChunk; /* Specific chunk into which cursor points */
93571};
93572
93573/*
93574** This structure is a subclass of sqlite3_file. Each open memory-journal
93575** is an instance of this class.
93576*/
93577struct MemJournal {
93578 const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
93579 int nChunkSize; /* In-memory chunk-size */
93580
93581 int nSpill; /* Bytes of data before flushing */
93582 int nSize; /* Bytes of data currently in memory */
93583 FileChunk *pFirst; /* Head of in-memory chunk-list */
93584 FilePoint endpoint; /* Pointer to the end of the file */
93585 FilePoint readpoint; /* Pointer to the end of the last xRead() */
93586
93587 int flags; /* xOpen flags */
93588 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
93589 const char *zJournal; /* Name of the journal file */
93590};
93591
93592/*
93593** Read data from the in-memory journal file. This is the implementation
93594** of the sqlite3_vfs.xRead method.
93595*/
93596static int memjrnlRead(
93597 sqlite3_file *pJfd, /* The journal file from which to read */
93598 void *zBuf, /* Put the results here */
93599 int iAmt, /* Number of bytes to read */
93600 sqlite_int64 iOfst /* Begin reading at this offset */
93601){
93602 MemJournal *p = (MemJournal *)pJfd;
93603 u8 *zOut = zBuf;
93604 int nRead = iAmt;
93605 int iChunkOffset;
93606 FileChunk *pChunk;
93607
93608#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
93609 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
93610 if( (iAmt+iOfst)>p->endpoint.iOffset ){
93611 return SQLITE_IOERR_SHORT_READ;
93612 }
93613#endif
93614
93615 assert( (iAmt+iOfst)<=p->endpoint.iOffset );
93616 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
93617 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
93618 sqlite3_int64 iOff = 0;
93619 for(pChunk=p->pFirst;
93620 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
93621 pChunk=pChunk->pNext
93622 ){
93623 iOff += p->nChunkSize;
93624 }
93625 }else{
93626 pChunk = p->readpoint.pChunk;
93627 assert( pChunk!=0 );
93628 }
93629
93630 iChunkOffset = (int)(iOfst%p->nChunkSize);
93631 do {
93632 int iSpace = p->nChunkSize - iChunkOffset;
93633 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
93634 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
93635 zOut += nCopy;
93636 nRead -= iSpace;
93637 iChunkOffset = 0;
93638 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
93639 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
93640 p->readpoint.pChunk = pChunk;
93641
93642 return SQLITE_OK;
93643}
93644
93645/*
93646** Free the list of FileChunk structures headed at MemJournal.pFirst.
93647*/
93648static void memjrnlFreeChunks(MemJournal *p){
93649 FileChunk *pIter;
93650 FileChunk *pNext;
93651 for(pIter=p->pFirst; pIter; pIter=pNext){
93652 pNext = pIter->pNext;
93653 sqlite3_free(pIter);
93654 }
93655 p->pFirst = 0;
93656}
93657
93658/*
93659** Flush the contents of memory to a real file on disk.
93660*/
93661static int memjrnlCreateFile(MemJournal *p){
93662 int rc;
93663 sqlite3_file *pReal = (sqlite3_file*)p;
93664 MemJournal copy = *p;
93665
93666 memset(p, 0, sizeof(MemJournal));
93667 rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
93668 if( rc==SQLITE_OK ){
93669 int nChunk = copy.nChunkSize;
93670 i64 iOff = 0;
93671 FileChunk *pIter;
93672 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
93673 if( iOff + nChunk > copy.endpoint.iOffset ){
93674 nChunk = copy.endpoint.iOffset - iOff;
93675 }
93676 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
93677 if( rc ) break;
93678 iOff += nChunk;
93679 }
93680 if( rc==SQLITE_OK ){
93681 /* No error has occurred. Free the in-memory buffers. */
93682 memjrnlFreeChunks(&copy);
93683 }
93684 }
93685 if( rc!=SQLITE_OK ){
93686 /* If an error occurred while creating or writing to the file, restore
93687 ** the original before returning. This way, SQLite uses the in-memory
93688 ** journal data to roll back changes made to the internal page-cache
93689 ** before this function was called. */
93690 sqlite3OsClose(pReal);
93691 *p = copy;
93692 }
93693 return rc;
93694}
93695
93696
93697/*
93698** Write data to the file.
93699*/
93700static int memjrnlWrite(
93701 sqlite3_file *pJfd, /* The journal file into which to write */
93702 const void *zBuf, /* Take data to be written from here */
93703 int iAmt, /* Number of bytes to write */
93704 sqlite_int64 iOfst /* Begin writing at this offset into the file */
93705){
93706 MemJournal *p = (MemJournal *)pJfd;
93707 int nWrite = iAmt;
93708 u8 *zWrite = (u8 *)zBuf;
93709
93710 /* If the file should be created now, create it and write the new data
93711 ** into the file on disk. */
93712 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
93713 int rc = memjrnlCreateFile(p);
93714 if( rc==SQLITE_OK ){
93715 rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
93716 }
93717 return rc;
93718 }
93719
93720 /* If the contents of this write should be stored in memory */
93721 else{
93722 /* An in-memory journal file should only ever be appended to. Random
93723 ** access writes are not required. The only exception to this is when
93724 ** the in-memory journal is being used by a connection using the
93725 ** atomic-write optimization. In this case the first 28 bytes of the
93726 ** journal file may be written as part of committing the transaction. */
93727 assert( iOfst==p->endpoint.iOffset || iOfst==0 );
93728#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
93729 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
93730 if( iOfst==0 && p->pFirst ){
93731 assert( p->nChunkSize>iAmt );
93732 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
93733 }else
93734#else
93735 assert( iOfst>0 || p->pFirst==0 );
93736#endif
93737 {
93738 while( nWrite>0 ){
93739 FileChunk *pChunk = p->endpoint.pChunk;
93740 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
93741 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
93742
93743 if( iChunkOffset==0 ){
93744 /* New chunk is required to extend the file. */
93745 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
93746 if( !pNew ){
93747 return SQLITE_IOERR_NOMEM_BKPT;
93748 }
93749 pNew->pNext = 0;
93750 if( pChunk ){
93751 assert( p->pFirst );
93752 pChunk->pNext = pNew;
93753 }else{
93754 assert( !p->pFirst );
93755 p->pFirst = pNew;
93756 }
93757 p->endpoint.pChunk = pNew;
93758 }
93759
93760 memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace);
93761 zWrite += iSpace;
93762 nWrite -= iSpace;
93763 p->endpoint.iOffset += iSpace;
93764 }
93765 p->nSize = iAmt + iOfst;
93766 }
93767 }
93768
93769 return SQLITE_OK;
93770}
93771
93772/*
93773** Truncate the file.
93774**
93775** If the journal file is already on disk, truncate it there. Or, if it
93776** is still in main memory but is being truncated to zero bytes in size,
93777** ignore
93778*/
93779static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
93780 MemJournal *p = (MemJournal *)pJfd;
93781 if( ALWAYS(size==0) ){
93782 memjrnlFreeChunks(p);
93783 p->nSize = 0;
93784 p->endpoint.pChunk = 0;
93785 p->endpoint.iOffset = 0;
93786 p->readpoint.pChunk = 0;
93787 p->readpoint.iOffset = 0;
93788 }
93789 return SQLITE_OK;
93790}
93791
93792/*
93793** Close the file.
93794*/
93795static int memjrnlClose(sqlite3_file *pJfd){
93796 MemJournal *p = (MemJournal *)pJfd;
93797 memjrnlFreeChunks(p);
93798 return SQLITE_OK;
93799}
93800
93801/*
93802** Sync the file.
93803**
93804** If the real file has been created, call its xSync method. Otherwise,
93805** syncing an in-memory journal is a no-op.
93806*/
93807static int memjrnlSync(sqlite3_file *pJfd, int flags){
93808 UNUSED_PARAMETER2(pJfd, flags);
93809 return SQLITE_OK;
93810}
93811
93812/*
93813** Query the size of the file in bytes.
93814*/
93815static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
93816 MemJournal *p = (MemJournal *)pJfd;
93817 *pSize = (sqlite_int64) p->endpoint.iOffset;
93818 return SQLITE_OK;
93819}
93820
93821/*
93822** Table of methods for MemJournal sqlite3_file object.
93823*/
93824static const struct sqlite3_io_methods MemJournalMethods = {
93825 1, /* iVersion */
93826 memjrnlClose, /* xClose */
93827 memjrnlRead, /* xRead */
93828 memjrnlWrite, /* xWrite */
93829 memjrnlTruncate, /* xTruncate */
93830 memjrnlSync, /* xSync */
93831 memjrnlFileSize, /* xFileSize */
93832 0, /* xLock */
93833 0, /* xUnlock */
93834 0, /* xCheckReservedLock */
93835 0, /* xFileControl */
93836 0, /* xSectorSize */
93837 0, /* xDeviceCharacteristics */
93838 0, /* xShmMap */
93839 0, /* xShmLock */
93840 0, /* xShmBarrier */
93841 0, /* xShmUnmap */
93842 0, /* xFetch */
93843 0 /* xUnfetch */
93844};
93845
93846/*
93847** Open a journal file.
93848**
93849** The behaviour of the journal file depends on the value of parameter
93850** nSpill. If nSpill is 0, then the journal file is always create and
93851** accessed using the underlying VFS. If nSpill is less than zero, then
93852** all content is always stored in main-memory. Finally, if nSpill is a
93853** positive value, then the journal file is initially created in-memory
93854** but may be flushed to disk later on. In this case the journal file is
93855** flushed to disk either when it grows larger than nSpill bytes in size,
93856** or when sqlite3JournalCreate() is called.
93857*/
93858SQLITE_PRIVATE int sqlite3JournalOpen(
93859 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
93860 const char *zName, /* Name of the journal file */
93861 sqlite3_file *pJfd, /* Preallocated, blank file handle */
93862 int flags, /* Opening flags */
93863 int nSpill /* Bytes buffered before opening the file */
93864){
93865 MemJournal *p = (MemJournal*)pJfd;
93866
93867 /* Zero the file-handle object. If nSpill was passed zero, initialize
93868 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
93869 ** case none of the code in this module is executed as a result of calls
93870 ** made on the journal file-handle. */
93871 memset(p, 0, sizeof(MemJournal));
93872 if( nSpill==0 ){
93873 return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
93874 }
93875
93876 if( nSpill>0 ){
93877 p->nChunkSize = nSpill;
93878 }else{
93879 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
93880 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
93881 }
93882
93883 p->pMethod = (const sqlite3_io_methods*)&MemJournalMethods;
93884 p->nSpill = nSpill;
93885 p->flags = flags;
93886 p->zJournal = zName;
93887 p->pVfs = pVfs;
93888 return SQLITE_OK;
93889}
93890
93891/*
93892** Open an in-memory journal file.
93893*/
93894SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
93895 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
93896}
93897
93898#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
93899 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
93900/*
93901** If the argument p points to a MemJournal structure that is not an
93902** in-memory-only journal file (i.e. is one that was opened with a +ve
93903** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying
93904** file has not yet been created, create it now.
93905*/
93906SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *pJfd){
93907 int rc = SQLITE_OK;
93908 MemJournal *p = (MemJournal*)pJfd;
93909 if( p->pMethod==&MemJournalMethods && (
93910#ifdef SQLITE_ENABLE_ATOMIC_WRITE
93911 p->nSpill>0
93912#else
93913 /* While this appears to not be possible without ATOMIC_WRITE, the
93914 ** paths are complex, so it seems prudent to leave the test in as
93915 ** a NEVER(), in case our analysis is subtly flawed. */
93916 NEVER(p->nSpill>0)
93917#endif
93918#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
93919 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
93920#endif
93921 )){
93922 rc = memjrnlCreateFile(p);
93923 }
93924 return rc;
93925}
93926#endif
93927
93928/*
93929** The file-handle passed as the only argument is open on a journal file.
93930** Return true if this "journal file" is currently stored in heap memory,
93931** or false otherwise.
93932*/
93933SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p){
93934 return p->pMethods==&MemJournalMethods;
93935}
93936
93937/*
93938** Return the number of bytes required to store a JournalFile that uses vfs
93939** pVfs to create the underlying on-disk files.
93940*/
93941SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
93942 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
93943}
93944
93945/************** End of memjournal.c ******************************************/
93946/************** Begin file walker.c ******************************************/
93947/*
93948** 2008 August 16
93949**
93950** The author disclaims copyright to this source code. In place of
93951** a legal notice, here is a blessing:
93952**
93953** May you do good and not evil.
93954** May you find forgiveness for yourself and forgive others.
93955** May you share freely, never taking more than you give.
93956**
93957*************************************************************************
93958** This file contains routines used for walking the parser tree for
93959** an SQL statement.
93960*/
93961/* #include "sqliteInt.h" */
93962/* #include <stdlib.h> */
93963/* #include <string.h> */
93964
93965
93966/*
93967** Walk an expression tree. Invoke the callback once for each node
93968** of the expression, while descending. (In other words, the callback
93969** is invoked before visiting children.)
93970**
93971** The return value from the callback should be one of the WRC_*
93972** constants to specify how to proceed with the walk.
93973**
93974** WRC_Continue Continue descending down the tree.
93975**
93976** WRC_Prune Do not descend into child nodes, but allow
93977** the walk to continue with sibling nodes.
93978**
93979** WRC_Abort Do no more callbacks. Unwind the stack and
93980** return from the top-level walk call.
93981**
93982** The return value from this routine is WRC_Abort to abandon the tree walk
93983** and WRC_Continue to continue.
93984*/
93985static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
93986 int rc;
93987 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
93988 testcase( ExprHasProperty(pExpr, EP_Reduced) );
93989 while(1){
93990 rc = pWalker->xExprCallback(pWalker, pExpr);
93991 if( rc ) return rc & WRC_Abort;
93992 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
93993 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
93994 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
93995 if( pExpr->pRight ){
93996 pExpr = pExpr->pRight;
93997 continue;
93998 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
93999 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
94000 }else if( pExpr->x.pList ){
94001 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
94002 }
94003#ifndef SQLITE_OMIT_WINDOWFUNC
94004 if( !ExprHasProperty(pExpr, EP_Reduced) && pExpr->pWin ){
94005 Window *pWin = pExpr->pWin;
94006 if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort;
94007 if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort;
94008 if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort;
94009 }
94010#endif
94011 }
94012 break;
94013 }
94014 return WRC_Continue;
94015}
94016SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
94017 return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
94018}
94019
94020/*
94021** Call sqlite3WalkExpr() for every expression in list p or until
94022** an abort request is seen.
94023*/
94024SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
94025 int i;
94026 struct ExprList_item *pItem;
94027 if( p ){
94028 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
94029 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
94030 }
94031 }
94032 return WRC_Continue;
94033}
94034
94035/*
94036** Walk all expressions associated with SELECT statement p. Do
94037** not invoke the SELECT callback on p, but do (of course) invoke
94038** any expr callbacks and SELECT callbacks that come from subqueries.
94039** Return WRC_Abort or WRC_Continue.
94040*/
94041SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
94042 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
94043 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
94044 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
94045 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
94046 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
94047 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
94048 return WRC_Continue;
94049}
94050
94051/*
94052** Walk the parse trees associated with all subqueries in the
94053** FROM clause of SELECT statement p. Do not invoke the select
94054** callback on p, but do invoke it on each FROM clause subquery
94055** and on any subqueries further down in the tree. Return
94056** WRC_Abort or WRC_Continue;
94057*/
94058SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
94059 SrcList *pSrc;
94060 int i;
94061 struct SrcList_item *pItem;
94062
94063 pSrc = p->pSrc;
94064 assert( pSrc!=0 );
94065 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
94066 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
94067 return WRC_Abort;
94068 }
94069 if( pItem->fg.isTabFunc
94070 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
94071 ){
94072 return WRC_Abort;
94073 }
94074 }
94075 return WRC_Continue;
94076}
94077
94078/*
94079** Call sqlite3WalkExpr() for every expression in Select statement p.
94080** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
94081** on the compound select chain, p->pPrior.
94082**
94083** If it is not NULL, the xSelectCallback() callback is invoked before
94084** the walk of the expressions and FROM clause. The xSelectCallback2()
94085** method is invoked following the walk of the expressions and FROM clause,
94086** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
94087** and if the expressions and FROM clause both return WRC_Continue;
94088**
94089** Return WRC_Continue under normal conditions. Return WRC_Abort if
94090** there is an abort request.
94091**
94092** If the Walker does not have an xSelectCallback() then this routine
94093** is a no-op returning WRC_Continue.
94094*/
94095SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
94096 int rc;
94097 if( p==0 ) return WRC_Continue;
94098 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
94099 do{
94100 rc = pWalker->xSelectCallback(pWalker, p);
94101 if( rc ) return rc & WRC_Abort;
94102 if( sqlite3WalkSelectExpr(pWalker, p)
94103 || sqlite3WalkSelectFrom(pWalker, p)
94104 ){
94105 return WRC_Abort;
94106 }
94107 if( pWalker->xSelectCallback2 ){
94108 pWalker->xSelectCallback2(pWalker, p);
94109 }
94110 p = p->pPrior;
94111 }while( p!=0 );
94112 return WRC_Continue;
94113}
94114
94115/************** End of walker.c **********************************************/
94116/************** Begin file resolve.c *****************************************/
94117/*
94118** 2008 August 18
94119**
94120** The author disclaims copyright to this source code. In place of
94121** a legal notice, here is a blessing:
94122**
94123** May you do good and not evil.
94124** May you find forgiveness for yourself and forgive others.
94125** May you share freely, never taking more than you give.
94126**
94127*************************************************************************
94128**
94129** This file contains routines used for walking the parser tree and
94130** resolve all identifiers by associating them with a particular
94131** table and column.
94132*/
94133/* #include "sqliteInt.h" */
94134
94135/*
94136** Walk the expression tree pExpr and increase the aggregate function
94137** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
94138** This needs to occur when copying a TK_AGG_FUNCTION node from an
94139** outer query into an inner subquery.
94140**
94141** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
94142** is a helper function - a callback for the tree walker.
94143*/
94144static int incrAggDepth(Walker *pWalker, Expr *pExpr){
94145 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
94146 return WRC_Continue;
94147}
94148static void incrAggFunctionDepth(Expr *pExpr, int N){
94149 if( N>0 ){
94150 Walker w;
94151 memset(&w, 0, sizeof(w));
94152 w.xExprCallback = incrAggDepth;
94153 w.u.n = N;
94154 sqlite3WalkExpr(&w, pExpr);
94155 }
94156}
94157
94158/*
94159** Turn the pExpr expression into an alias for the iCol-th column of the
94160** result set in pEList.
94161**
94162** If the reference is followed by a COLLATE operator, then make sure
94163** the COLLATE operator is preserved. For example:
94164**
94165** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
94166**
94167** Should be transformed into:
94168**
94169** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
94170**
94171** The nSubquery parameter specifies how many levels of subquery the
94172** alias is removed from the original expression. The usual value is
94173** zero but it might be more if the alias is contained within a subquery
94174** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
94175** structures must be increased by the nSubquery amount.
94176*/
94177static void resolveAlias(
94178 Parse *pParse, /* Parsing context */
94179 ExprList *pEList, /* A result set */
94180 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
94181 Expr *pExpr, /* Transform this into an alias to the result set */
94182 const char *zType, /* "GROUP" or "ORDER" or "" */
94183 int nSubquery /* Number of subqueries that the label is moving */
94184){
94185 Expr *pOrig; /* The iCol-th column of the result set */
94186 Expr *pDup; /* Copy of pOrig */
94187 sqlite3 *db; /* The database connection */
94188
94189 assert( iCol>=0 && iCol<pEList->nExpr );
94190 pOrig = pEList->a[iCol].pExpr;
94191 assert( pOrig!=0 );
94192 db = pParse->db;
94193 pDup = sqlite3ExprDup(db, pOrig, 0);
94194 if( pDup!=0 ){
94195 if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
94196 if( pExpr->op==TK_COLLATE ){
94197 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
94198 }
94199 ExprSetProperty(pDup, EP_Alias);
94200
94201 /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
94202 ** prevents ExprDelete() from deleting the Expr structure itself,
94203 ** allowing it to be repopulated by the memcpy() on the following line.
94204 ** The pExpr->u.zToken might point into memory that will be freed by the
94205 ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
94206 ** make a copy of the token before doing the sqlite3DbFree().
94207 */
94208 ExprSetProperty(pExpr, EP_Static);
94209 sqlite3ExprDelete(db, pExpr);
94210 memcpy(pExpr, pDup, sizeof(*pExpr));
94211 if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
94212 assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
94213 pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
94214 pExpr->flags |= EP_MemToken;
94215 }
94216 sqlite3DbFree(db, pDup);
94217 }
94218 ExprSetProperty(pExpr, EP_Alias);
94219}
94220
94221
94222/*
94223** Return TRUE if the name zCol occurs anywhere in the USING clause.
94224**
94225** Return FALSE if the USING clause is NULL or if it does not contain
94226** zCol.
94227*/
94228static int nameInUsingClause(IdList *pUsing, const char *zCol){
94229 if( pUsing ){
94230 int k;
94231 for(k=0; k<pUsing->nId; k++){
94232 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
94233 }
94234 }
94235 return 0;
94236}
94237
94238/*
94239** Subqueries stores the original database, table and column names for their
94240** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
94241** Check to see if the zSpan given to this routine matches the zDb, zTab,
94242** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will
94243** match anything.
94244*/
94245SQLITE_PRIVATE int sqlite3MatchSpanName(
94246 const char *zSpan,
94247 const char *zCol,
94248 const char *zTab,
94249 const char *zDb
94250){
94251 int n;
94252 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
94253 if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
94254 return 0;
94255 }
94256 zSpan += n+1;
94257 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
94258 if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
94259 return 0;
94260 }
94261 zSpan += n+1;
94262 if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
94263 return 0;
94264 }
94265 return 1;
94266}
94267
94268/*
94269** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
94270** that name in the set of source tables in pSrcList and make the pExpr
94271** expression node refer back to that source column. The following changes
94272** are made to pExpr:
94273**
94274** pExpr->iDb Set the index in db->aDb[] of the database X
94275** (even if X is implied).
94276** pExpr->iTable Set to the cursor number for the table obtained
94277** from pSrcList.
94278** pExpr->pTab Points to the Table structure of X.Y (even if
94279** X and/or Y are implied.)
94280** pExpr->iColumn Set to the column number within the table.
94281** pExpr->op Set to TK_COLUMN.
94282** pExpr->pLeft Any expression this points to is deleted
94283** pExpr->pRight Any expression this points to is deleted.
94284**
94285** The zDb variable is the name of the database (the "X"). This value may be
94286** NULL meaning that name is of the form Y.Z or Z. Any available database
94287** can be used. The zTable variable is the name of the table (the "Y"). This
94288** value can be NULL if zDb is also NULL. If zTable is NULL it
94289** means that the form of the name is Z and that columns from any table
94290** can be used.
94291**
94292** If the name cannot be resolved unambiguously, leave an error message
94293** in pParse and return WRC_Abort. Return WRC_Prune on success.
94294*/
94295static int lookupName(
94296 Parse *pParse, /* The parsing context */
94297 const char *zDb, /* Name of the database containing table, or NULL */
94298 const char *zTab, /* Name of table containing column, or NULL */
94299 const char *zCol, /* Name of the column. */
94300 NameContext *pNC, /* The name context used to resolve the name */
94301 Expr *pExpr /* Make this EXPR node point to the selected column */
94302){
94303 int i, j; /* Loop counters */
94304 int cnt = 0; /* Number of matching column names */
94305 int cntTab = 0; /* Number of matching table names */
94306 int nSubquery = 0; /* How many levels of subquery */
94307 sqlite3 *db = pParse->db; /* The database connection */
94308 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
94309 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
94310 NameContext *pTopNC = pNC; /* First namecontext in the list */
94311 Schema *pSchema = 0; /* Schema of the expression */
94312 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
94313 Table *pTab = 0; /* Table hold the row */
94314 Column *pCol; /* A column of pTab */
94315
94316 assert( pNC ); /* the name context cannot be NULL. */
94317 assert( zCol ); /* The Z in X.Y.Z cannot be NULL */
94318 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
94319
94320 /* Initialize the node to no-match */
94321 pExpr->iTable = -1;
94322 pExpr->pTab = 0;
94323 ExprSetVVAProperty(pExpr, EP_NoReduce);
94324
94325 /* Translate the schema name in zDb into a pointer to the corresponding
94326 ** schema. If not found, pSchema will remain NULL and nothing will match
94327 ** resulting in an appropriate error message toward the end of this routine
94328 */
94329 if( zDb ){
94330 testcase( pNC->ncFlags & NC_PartIdx );
94331 testcase( pNC->ncFlags & NC_IsCheck );
94332 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
94333 /* Silently ignore database qualifiers inside CHECK constraints and
94334 ** partial indices. Do not raise errors because that might break
94335 ** legacy and because it does not hurt anything to just ignore the
94336 ** database name. */
94337 zDb = 0;
94338 }else{
94339 for(i=0; i<db->nDb; i++){
94340 assert( db->aDb[i].zDbSName );
94341 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
94342 pSchema = db->aDb[i].pSchema;
94343 break;
94344 }
94345 }
94346 }
94347 }
94348
94349 /* Start at the inner-most context and move outward until a match is found */
94350 assert( pNC && cnt==0 );
94351 do{
94352 ExprList *pEList;
94353 SrcList *pSrcList = pNC->pSrcList;
94354
94355 if( pSrcList ){
94356 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
94357 pTab = pItem->pTab;
94358 assert( pTab!=0 && pTab->zName!=0 );
94359 assert( pTab->nCol>0 );
94360 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
94361 int hit = 0;
94362 pEList = pItem->pSelect->pEList;
94363 for(j=0; j<pEList->nExpr; j++){
94364 if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
94365 cnt++;
94366 cntTab = 2;
94367 pMatch = pItem;
94368 pExpr->iColumn = j;
94369 hit = 1;
94370 }
94371 }
94372 if( hit || zTab==0 ) continue;
94373 }
94374 if( zDb && pTab->pSchema!=pSchema ){
94375 continue;
94376 }
94377 if( zTab ){
94378 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
94379 assert( zTabName!=0 );
94380 if( sqlite3StrICmp(zTabName, zTab)!=0 ){
94381 continue;
94382 }
94383 if( IN_RENAME_OBJECT && pItem->zAlias ){
94384 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->pTab);
94385 }
94386 }
94387 if( 0==(cntTab++) ){
94388 pMatch = pItem;
94389 }
94390 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
94391 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
94392 /* If there has been exactly one prior match and this match
94393 ** is for the right-hand table of a NATURAL JOIN or is in a
94394 ** USING clause, then skip this match.
94395 */
94396 if( cnt==1 ){
94397 if( pItem->fg.jointype & JT_NATURAL ) continue;
94398 if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
94399 }
94400 cnt++;
94401 pMatch = pItem;
94402 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
94403 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
94404 break;
94405 }
94406 }
94407 }
94408 if( pMatch ){
94409 pExpr->iTable = pMatch->iCursor;
94410 pExpr->pTab = pMatch->pTab;
94411 /* RIGHT JOIN not (yet) supported */
94412 assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
94413 if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
94414 ExprSetProperty(pExpr, EP_CanBeNull);
94415 }
94416 pSchema = pExpr->pTab->pSchema;
94417 }
94418 } /* if( pSrcList ) */
94419
94420#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
94421 /* If we have not already resolved the name, then maybe
94422 ** it is a new.* or old.* trigger argument reference. Or
94423 ** maybe it is an excluded.* from an upsert.
94424 */
94425 if( zDb==0 && zTab!=0 && cntTab==0 ){
94426 pTab = 0;
94427#ifndef SQLITE_OMIT_TRIGGER
94428 if( pParse->pTriggerTab!=0 ){
94429 int op = pParse->eTriggerOp;
94430 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
94431 if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
94432 pExpr->iTable = 1;
94433 pTab = pParse->pTriggerTab;
94434 }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
94435 pExpr->iTable = 0;
94436 pTab = pParse->pTriggerTab;
94437 }
94438 }
94439#endif /* SQLITE_OMIT_TRIGGER */
94440#ifndef SQLITE_OMIT_UPSERT
94441 if( (pNC->ncFlags & NC_UUpsert)!=0 ){
94442 Upsert *pUpsert = pNC->uNC.pUpsert;
94443 if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
94444 pTab = pUpsert->pUpsertSrc->a[0].pTab;
94445 pExpr->iTable = 2;
94446 }
94447 }
94448#endif /* SQLITE_OMIT_UPSERT */
94449
94450 if( pTab ){
94451 int iCol;
94452 pSchema = pTab->pSchema;
94453 cntTab++;
94454 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
94455 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
94456 if( iCol==pTab->iPKey ){
94457 iCol = -1;
94458 }
94459 break;
94460 }
94461 }
94462 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
94463 /* IMP: R-51414-32910 */
94464 iCol = -1;
94465 }
94466 if( iCol<pTab->nCol ){
94467 cnt++;
94468#ifndef SQLITE_OMIT_UPSERT
94469 if( pExpr->iTable==2 ){
94470 testcase( iCol==(-1) );
94471 if( IN_RENAME_OBJECT ){
94472 pExpr->iColumn = iCol;
94473 pExpr->pTab = pTab;
94474 eNewExprOp = TK_COLUMN;
94475 }else{
94476 pExpr->iTable = pNC->uNC.pUpsert->regData + iCol;
94477 eNewExprOp = TK_REGISTER;
94478 ExprSetProperty(pExpr, EP_Alias);
94479 }
94480 }else
94481#endif /* SQLITE_OMIT_UPSERT */
94482 {
94483#ifndef SQLITE_OMIT_TRIGGER
94484 if( iCol<0 ){
94485 pExpr->affinity = SQLITE_AFF_INTEGER;
94486 }else if( pExpr->iTable==0 ){
94487 testcase( iCol==31 );
94488 testcase( iCol==32 );
94489 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
94490 }else{
94491 testcase( iCol==31 );
94492 testcase( iCol==32 );
94493 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
94494 }
94495 pExpr->pTab = pTab;
94496 pExpr->iColumn = (i16)iCol;
94497 eNewExprOp = TK_TRIGGER;
94498#endif /* SQLITE_OMIT_TRIGGER */
94499 }
94500 }
94501 }
94502 }
94503#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
94504
94505 /*
94506 ** Perhaps the name is a reference to the ROWID
94507 */
94508 if( cnt==0
94509 && cntTab==1
94510 && pMatch
94511 && (pNC->ncFlags & NC_IdxExpr)==0
94512 && sqlite3IsRowid(zCol)
94513 && VisibleRowid(pMatch->pTab)
94514 ){
94515 cnt = 1;
94516 pExpr->iColumn = -1;
94517 pExpr->affinity = SQLITE_AFF_INTEGER;
94518 }
94519
94520 /*
94521 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
94522 ** might refer to an result-set alias. This happens, for example, when
94523 ** we are resolving names in the WHERE clause of the following command:
94524 **
94525 ** SELECT a+b AS x FROM table WHERE x<10;
94526 **
94527 ** In cases like this, replace pExpr with a copy of the expression that
94528 ** forms the result set entry ("a+b" in the example) and return immediately.
94529 ** Note that the expression in the result set should have already been
94530 ** resolved by the time the WHERE clause is resolved.
94531 **
94532 ** The ability to use an output result-set column in the WHERE, GROUP BY,
94533 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
94534 ** clause is not standard SQL. This is a (goofy) SQLite extension, that
94535 ** is supported for backwards compatibility only. Hence, we issue a warning
94536 ** on sqlite3_log() whenever the capability is used.
94537 */
94538 if( (pNC->ncFlags & NC_UEList)!=0
94539 && cnt==0
94540 && zTab==0
94541 ){
94542 pEList = pNC->uNC.pEList;
94543 assert( pEList!=0 );
94544 for(j=0; j<pEList->nExpr; j++){
94545 char *zAs = pEList->a[j].zName;
94546 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
94547 Expr *pOrig;
94548 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
94549 assert( pExpr->x.pList==0 );
94550 assert( pExpr->x.pSelect==0 );
94551 pOrig = pEList->a[j].pExpr;
94552 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
94553 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
94554 return WRC_Abort;
94555 }
94556 if( sqlite3ExprVectorSize(pOrig)!=1 ){
94557 sqlite3ErrorMsg(pParse, "row value misused");
94558 return WRC_Abort;
94559 }
94560 resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
94561 cnt = 1;
94562 pMatch = 0;
94563 assert( zTab==0 && zDb==0 );
94564 if( IN_RENAME_OBJECT ){
94565 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
94566 }
94567 goto lookupname_end;
94568 }
94569 }
94570 }
94571
94572 /* Advance to the next name context. The loop will exit when either
94573 ** we have a match (cnt>0) or when we run out of name contexts.
94574 */
94575 if( cnt ) break;
94576 pNC = pNC->pNext;
94577 nSubquery++;
94578 }while( pNC );
94579
94580
94581 /*
94582 ** If X and Y are NULL (in other words if only the column name Z is
94583 ** supplied) and the value of Z is enclosed in double-quotes, then
94584 ** Z is a string literal if it doesn't match any column names. In that
94585 ** case, we need to return right away and not make any changes to
94586 ** pExpr.
94587 **
94588 ** Because no reference was made to outer contexts, the pNC->nRef
94589 ** fields are not changed in any context.
94590 */
94591 if( cnt==0 && zTab==0 ){
94592 assert( pExpr->op==TK_ID );
94593 if( ExprHasProperty(pExpr,EP_DblQuoted) ){
94594 pExpr->op = TK_STRING;
94595 pExpr->pTab = 0;
94596 return WRC_Prune;
94597 }
94598 if( sqlite3ExprIdToTrueFalse(pExpr) ){
94599 return WRC_Prune;
94600 }
94601 }
94602
94603 /*
94604 ** cnt==0 means there was not match. cnt>1 means there were two or
94605 ** more matches. Either way, we have an error.
94606 */
94607 if( cnt!=1 ){
94608 const char *zErr;
94609 zErr = cnt==0 ? "no such column" : "ambiguous column name";
94610 if( zDb ){
94611 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
94612 }else if( zTab ){
94613 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
94614 }else{
94615 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
94616 }
94617 pParse->checkSchema = 1;
94618 pTopNC->nErr++;
94619 }
94620
94621 /* If a column from a table in pSrcList is referenced, then record
94622 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
94623 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
94624 ** column number is greater than the number of bits in the bitmask
94625 ** then set the high-order bit of the bitmask.
94626 */
94627 if( pExpr->iColumn>=0 && pMatch!=0 ){
94628 int n = pExpr->iColumn;
94629 testcase( n==BMS-1 );
94630 if( n>=BMS ){
94631 n = BMS-1;
94632 }
94633 assert( pMatch->iCursor==pExpr->iTable );
94634 pMatch->colUsed |= ((Bitmask)1)<<n;
94635 }
94636
94637 /* Clean up and return
94638 */
94639 sqlite3ExprDelete(db, pExpr->pLeft);
94640 pExpr->pLeft = 0;
94641 sqlite3ExprDelete(db, pExpr->pRight);
94642 pExpr->pRight = 0;
94643 pExpr->op = eNewExprOp;
94644 ExprSetProperty(pExpr, EP_Leaf);
94645lookupname_end:
94646 if( cnt==1 ){
94647 assert( pNC!=0 );
94648 if( !ExprHasProperty(pExpr, EP_Alias) ){
94649 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
94650 }
94651 /* Increment the nRef value on all name contexts from TopNC up to
94652 ** the point where the name matched. */
94653 for(;;){
94654 assert( pTopNC!=0 );
94655 pTopNC->nRef++;
94656 if( pTopNC==pNC ) break;
94657 pTopNC = pTopNC->pNext;
94658 }
94659 return WRC_Prune;
94660 } else {
94661 return WRC_Abort;
94662 }
94663}
94664
94665/*
94666** Allocate and return a pointer to an expression to load the column iCol
94667** from datasource iSrc in SrcList pSrc.
94668*/
94669SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
94670 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
94671 if( p ){
94672 struct SrcList_item *pItem = &pSrc->a[iSrc];
94673 p->pTab = pItem->pTab;
94674 p->iTable = pItem->iCursor;
94675 if( p->pTab->iPKey==iCol ){
94676 p->iColumn = -1;
94677 }else{
94678 p->iColumn = (ynVar)iCol;
94679 testcase( iCol==BMS );
94680 testcase( iCol==BMS-1 );
94681 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
94682 }
94683 }
94684 return p;
94685}
94686
94687/*
94688** Report an error that an expression is not valid for some set of
94689** pNC->ncFlags values determined by validMask.
94690*/
94691static void notValid(
94692 Parse *pParse, /* Leave error message here */
94693 NameContext *pNC, /* The name context */
94694 const char *zMsg, /* Type of error */
94695 int validMask /* Set of contexts for which prohibited */
94696){
94697 assert( (validMask&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr))==0 );
94698 if( (pNC->ncFlags & validMask)!=0 ){
94699 const char *zIn = "partial index WHERE clauses";
94700 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
94701#ifndef SQLITE_OMIT_CHECK
94702 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
94703#endif
94704 sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
94705 }
94706}
94707
94708/*
94709** Expression p should encode a floating point value between 1.0 and 0.0.
94710** Return 1024 times this value. Or return -1 if p is not a floating point
94711** value between 1.0 and 0.0.
94712*/
94713static int exprProbability(Expr *p){
94714 double r = -1.0;
94715 if( p->op!=TK_FLOAT ) return -1;
94716 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
94717 assert( r>=0.0 );
94718 if( r>1.0 ) return -1;
94719 return (int)(r*134217728.0);
94720}
94721
94722/*
94723** This routine is callback for sqlite3WalkExpr().
94724**
94725** Resolve symbolic names into TK_COLUMN operators for the current
94726** node in the expression tree. Return 0 to continue the search down
94727** the tree or 2 to abort the tree walk.
94728**
94729** This routine also does error checking and name resolution for
94730** function names. The operator for aggregate functions is changed
94731** to TK_AGG_FUNCTION.
94732*/
94733static int resolveExprStep(Walker *pWalker, Expr *pExpr){
94734 NameContext *pNC;
94735 Parse *pParse;
94736
94737 pNC = pWalker->u.pNC;
94738 assert( pNC!=0 );
94739 pParse = pNC->pParse;
94740 assert( pParse==pWalker->pParse );
94741
94742#ifndef NDEBUG
94743 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
94744 SrcList *pSrcList = pNC->pSrcList;
94745 int i;
94746 for(i=0; i<pNC->pSrcList->nSrc; i++){
94747 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
94748 }
94749 }
94750#endif
94751 switch( pExpr->op ){
94752
94753#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
94754 /* The special operator TK_ROW means use the rowid for the first
94755 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
94756 ** clause processing on UPDATE and DELETE statements.
94757 */
94758 case TK_ROW: {
94759 SrcList *pSrcList = pNC->pSrcList;
94760 struct SrcList_item *pItem;
94761 assert( pSrcList && pSrcList->nSrc==1 );
94762 pItem = pSrcList->a;
94763 assert( HasRowid(pItem->pTab) && pItem->pTab->pSelect==0 );
94764 pExpr->op = TK_COLUMN;
94765 pExpr->pTab = pItem->pTab;
94766 pExpr->iTable = pItem->iCursor;
94767 pExpr->iColumn = -1;
94768 pExpr->affinity = SQLITE_AFF_INTEGER;
94769 break;
94770 }
94771#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
94772 && !defined(SQLITE_OMIT_SUBQUERY) */
94773
94774 /* A column name: ID
94775 ** Or table name and column name: ID.ID
94776 ** Or a database, table and column: ID.ID.ID
94777 **
94778 ** The TK_ID and TK_OUT cases are combined so that there will only
94779 ** be one call to lookupName(). Then the compiler will in-line
94780 ** lookupName() for a size reduction and performance increase.
94781 */
94782 case TK_ID:
94783 case TK_DOT: {
94784 const char *zColumn;
94785 const char *zTable;
94786 const char *zDb;
94787 Expr *pRight;
94788
94789 if( pExpr->op==TK_ID ){
94790 zDb = 0;
94791 zTable = 0;
94792 zColumn = pExpr->u.zToken;
94793 }else{
94794 Expr *pLeft = pExpr->pLeft;
94795 notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
94796 pRight = pExpr->pRight;
94797 if( pRight->op==TK_ID ){
94798 zDb = 0;
94799 }else{
94800 assert( pRight->op==TK_DOT );
94801 zDb = pLeft->u.zToken;
94802 pLeft = pRight->pLeft;
94803 pRight = pRight->pRight;
94804 }
94805 zTable = pLeft->u.zToken;
94806 zColumn = pRight->u.zToken;
94807 if( IN_RENAME_OBJECT ){
94808 sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
94809 }
94810 if( IN_RENAME_OBJECT ){
94811 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->pTab, (void*)pLeft);
94812 }
94813 }
94814 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
94815 }
94816
94817 /* Resolve function names
94818 */
94819 case TK_FUNCTION: {
94820 ExprList *pList = pExpr->x.pList; /* The argument list */
94821 int n = pList ? pList->nExpr : 0; /* Number of arguments */
94822 int no_such_func = 0; /* True if no such function exists */
94823 int wrong_num_args = 0; /* True if wrong number of arguments */
94824 int is_agg = 0; /* True if is an aggregate function */
94825 int nId; /* Number of characters in function name */
94826 const char *zId; /* The function name. */
94827 FuncDef *pDef; /* Information about the function */
94828 u8 enc = ENC(pParse->db); /* The database encoding */
94829
94830 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
94831 zId = pExpr->u.zToken;
94832 nId = sqlite3Strlen30(zId);
94833 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
94834 if( pDef==0 ){
94835 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
94836 if( pDef==0 ){
94837 no_such_func = 1;
94838 }else{
94839 wrong_num_args = 1;
94840 }
94841 }else{
94842 is_agg = pDef->xFinalize!=0;
94843 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
94844 ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
94845 if( n==2 ){
94846 pExpr->iTable = exprProbability(pList->a[1].pExpr);
94847 if( pExpr->iTable<0 ){
94848 sqlite3ErrorMsg(pParse,
94849 "second argument to likelihood() must be a "
94850 "constant between 0.0 and 1.0");
94851 pNC->nErr++;
94852 }
94853 }else{
94854 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
94855 ** equivalent to likelihood(X, 0.0625).
94856 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
94857 ** short-hand for likelihood(X,0.0625).
94858 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
94859 ** for likelihood(X,0.9375).
94860 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
94861 ** to likelihood(X,0.9375). */
94862 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
94863 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
94864 }
94865 }
94866#ifndef SQLITE_OMIT_AUTHORIZATION
94867 {
94868 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
94869 if( auth!=SQLITE_OK ){
94870 if( auth==SQLITE_DENY ){
94871 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
94872 pDef->zName);
94873 pNC->nErr++;
94874 }
94875 pExpr->op = TK_NULL;
94876 return WRC_Prune;
94877 }
94878 }
94879#endif
94880 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
94881 /* For the purposes of the EP_ConstFunc flag, date and time
94882 ** functions and other functions that change slowly are considered
94883 ** constant because they are constant for the duration of one query */
94884 ExprSetProperty(pExpr,EP_ConstFunc);
94885 }
94886 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
94887 /* Date/time functions that use 'now', and other functions like
94888 ** sqlite_version() that might change over time cannot be used
94889 ** in an index. */
94890 notValid(pParse, pNC, "non-deterministic functions",
94891 NC_IdxExpr|NC_PartIdx);
94892 }
94893 }
94894
94895 if( 0==IN_RENAME_OBJECT ){
94896#ifndef SQLITE_OMIT_WINDOWFUNC
94897 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
94898 || (pDef->xValue==0 && pDef->xInverse==0)
94899 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
94900 );
94901 if( pDef && pDef->xValue==0 && pExpr->pWin ){
94902 sqlite3ErrorMsg(pParse,
94903 "%.*s() may not be used as a window function", nId, zId
94904 );
94905 pNC->nErr++;
94906 }else if(
94907 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
94908 || (is_agg && (pDef->funcFlags & SQLITE_FUNC_WINDOW) && !pExpr->pWin)
94909 || (is_agg && pExpr->pWin && (pNC->ncFlags & NC_AllowWin)==0)
94910 ){
94911 const char *zType;
94912 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->pWin ){
94913 zType = "window";
94914 }else{
94915 zType = "aggregate";
94916 }
94917 sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId);
94918 pNC->nErr++;
94919 is_agg = 0;
94920 }
94921#else
94922 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
94923 sqlite3ErrorMsg(pParse,"misuse of aggregate function %.*s()",nId,zId);
94924 pNC->nErr++;
94925 is_agg = 0;
94926 }
94927#endif
94928 else if( no_such_func && pParse->db->init.busy==0
94929#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
94930 && pParse->explain==0
94931#endif
94932 ){
94933 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
94934 pNC->nErr++;
94935 }else if( wrong_num_args ){
94936 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
94937 nId, zId);
94938 pNC->nErr++;
94939 }
94940 if( is_agg ){
94941#ifndef SQLITE_OMIT_WINDOWFUNC
94942 pNC->ncFlags &= ~(pExpr->pWin ? NC_AllowWin : NC_AllowAgg);
94943#else
94944 pNC->ncFlags &= ~NC_AllowAgg;
94945#endif
94946 }
94947 }
94948 sqlite3WalkExprList(pWalker, pList);
94949 if( is_agg ){
94950#ifndef SQLITE_OMIT_WINDOWFUNC
94951 if( pExpr->pWin ){
94952 Select *pSel = pNC->pWinSelect;
94953 sqlite3WalkExprList(pWalker, pExpr->pWin->pPartition);
94954 sqlite3WalkExprList(pWalker, pExpr->pWin->pOrderBy);
94955 sqlite3WalkExpr(pWalker, pExpr->pWin->pFilter);
94956 sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->pWin, pDef);
94957 if( 0==pSel->pWin
94958 || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->pWin)
94959 ){
94960 pExpr->pWin->pNextWin = pSel->pWin;
94961 pSel->pWin = pExpr->pWin;
94962 }
94963 pNC->ncFlags |= NC_AllowWin;
94964 }else
94965#endif /* SQLITE_OMIT_WINDOWFUNC */
94966 {
94967 NameContext *pNC2 = pNC;
94968 pExpr->op = TK_AGG_FUNCTION;
94969 pExpr->op2 = 0;
94970 while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
94971 pExpr->op2++;
94972 pNC2 = pNC2->pNext;
94973 }
94974 assert( pDef!=0 );
94975 if( pNC2 ){
94976 assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
94977 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
94978 pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
94979
94980 }
94981 pNC->ncFlags |= NC_AllowAgg;
94982 }
94983 }
94984 /* FIX ME: Compute pExpr->affinity based on the expected return
94985 ** type of the function
94986 */
94987 return WRC_Prune;
94988 }
94989#ifndef SQLITE_OMIT_SUBQUERY
94990 case TK_SELECT:
94991 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
94992#endif
94993 case TK_IN: {
94994 testcase( pExpr->op==TK_IN );
94995 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
94996 int nRef = pNC->nRef;
94997 notValid(pParse, pNC, "subqueries", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
94998 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
94999 assert( pNC->nRef>=nRef );
95000 if( nRef!=pNC->nRef ){
95001 ExprSetProperty(pExpr, EP_VarSelect);
95002 pNC->ncFlags |= NC_VarSelect;
95003 }
95004 }
95005 break;
95006 }
95007 case TK_VARIABLE: {
95008 notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
95009 break;
95010 }
95011 case TK_IS:
95012 case TK_ISNOT: {
95013 Expr *pRight;
95014 assert( !ExprHasProperty(pExpr, EP_Reduced) );
95015 /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
95016 ** and "x IS NOT FALSE". */
95017 if( (pRight = pExpr->pRight)->op==TK_ID ){
95018 int rc = resolveExprStep(pWalker, pRight);
95019 if( rc==WRC_Abort ) return WRC_Abort;
95020 if( pRight->op==TK_TRUEFALSE ){
95021 pExpr->op2 = pExpr->op;
95022 pExpr->op = TK_TRUTH;
95023 return WRC_Continue;
95024 }
95025 }
95026 /* Fall thru */
95027 }
95028 case TK_BETWEEN:
95029 case TK_EQ:
95030 case TK_NE:
95031 case TK_LT:
95032 case TK_LE:
95033 case TK_GT:
95034 case TK_GE: {
95035 int nLeft, nRight;
95036 if( pParse->db->mallocFailed ) break;
95037 assert( pExpr->pLeft!=0 );
95038 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
95039 if( pExpr->op==TK_BETWEEN ){
95040 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
95041 if( nRight==nLeft ){
95042 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
95043 }
95044 }else{
95045 assert( pExpr->pRight!=0 );
95046 nRight = sqlite3ExprVectorSize(pExpr->pRight);
95047 }
95048 if( nLeft!=nRight ){
95049 testcase( pExpr->op==TK_EQ );
95050 testcase( pExpr->op==TK_NE );
95051 testcase( pExpr->op==TK_LT );
95052 testcase( pExpr->op==TK_LE );
95053 testcase( pExpr->op==TK_GT );
95054 testcase( pExpr->op==TK_GE );
95055 testcase( pExpr->op==TK_IS );
95056 testcase( pExpr->op==TK_ISNOT );
95057 testcase( pExpr->op==TK_BETWEEN );
95058 sqlite3ErrorMsg(pParse, "row value misused");
95059 }
95060 break;
95061 }
95062 }
95063 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
95064}
95065
95066/*
95067** pEList is a list of expressions which are really the result set of the
95068** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
95069** This routine checks to see if pE is a simple identifier which corresponds
95070** to the AS-name of one of the terms of the expression list. If it is,
95071** this routine return an integer between 1 and N where N is the number of
95072** elements in pEList, corresponding to the matching entry. If there is
95073** no match, or if pE is not a simple identifier, then this routine
95074** return 0.
95075**
95076** pEList has been resolved. pE has not.
95077*/
95078static int resolveAsName(
95079 Parse *pParse, /* Parsing context for error messages */
95080 ExprList *pEList, /* List of expressions to scan */
95081 Expr *pE /* Expression we are trying to match */
95082){
95083 int i; /* Loop counter */
95084
95085 UNUSED_PARAMETER(pParse);
95086
95087 if( pE->op==TK_ID ){
95088 char *zCol = pE->u.zToken;
95089 for(i=0; i<pEList->nExpr; i++){
95090 char *zAs = pEList->a[i].zName;
95091 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
95092 return i+1;
95093 }
95094 }
95095 }
95096 return 0;
95097}
95098
95099/*
95100** pE is a pointer to an expression which is a single term in the
95101** ORDER BY of a compound SELECT. The expression has not been
95102** name resolved.
95103**
95104** At the point this routine is called, we already know that the
95105** ORDER BY term is not an integer index into the result set. That
95106** case is handled by the calling routine.
95107**
95108** Attempt to match pE against result set columns in the left-most
95109** SELECT statement. Return the index i of the matching column,
95110** as an indication to the caller that it should sort by the i-th column.
95111** The left-most column is 1. In other words, the value returned is the
95112** same integer value that would be used in the SQL statement to indicate
95113** the column.
95114**
95115** If there is no match, return 0. Return -1 if an error occurs.
95116*/
95117static int resolveOrderByTermToExprList(
95118 Parse *pParse, /* Parsing context for error messages */
95119 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
95120 Expr *pE /* The specific ORDER BY term */
95121){
95122 int i; /* Loop counter */
95123 ExprList *pEList; /* The columns of the result set */
95124 NameContext nc; /* Name context for resolving pE */
95125 sqlite3 *db; /* Database connection */
95126 int rc; /* Return code from subprocedures */
95127 u8 savedSuppErr; /* Saved value of db->suppressErr */
95128
95129 assert( sqlite3ExprIsInteger(pE, &i)==0 );
95130 pEList = pSelect->pEList;
95131
95132 /* Resolve all names in the ORDER BY term expression
95133 */
95134 memset(&nc, 0, sizeof(nc));
95135 nc.pParse = pParse;
95136 nc.pSrcList = pSelect->pSrc;
95137 nc.uNC.pEList = pEList;
95138 nc.ncFlags = NC_AllowAgg|NC_UEList;
95139 nc.nErr = 0;
95140 db = pParse->db;
95141 savedSuppErr = db->suppressErr;
95142 db->suppressErr = 1;
95143 rc = sqlite3ResolveExprNames(&nc, pE);
95144 db->suppressErr = savedSuppErr;
95145 if( rc ) return 0;
95146
95147 /* Try to match the ORDER BY expression against an expression
95148 ** in the result set. Return an 1-based index of the matching
95149 ** result-set entry.
95150 */
95151 for(i=0; i<pEList->nExpr; i++){
95152 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
95153 return i+1;
95154 }
95155 }
95156
95157 /* If no match, return 0. */
95158 return 0;
95159}
95160
95161/*
95162** Generate an ORDER BY or GROUP BY term out-of-range error.
95163*/
95164static void resolveOutOfRangeError(
95165 Parse *pParse, /* The error context into which to write the error */
95166 const char *zType, /* "ORDER" or "GROUP" */
95167 int i, /* The index (1-based) of the term out of range */
95168 int mx /* Largest permissible value of i */
95169){
95170 sqlite3ErrorMsg(pParse,
95171 "%r %s BY term out of range - should be "
95172 "between 1 and %d", i, zType, mx);
95173}
95174
95175/*
95176** Analyze the ORDER BY clause in a compound SELECT statement. Modify
95177** each term of the ORDER BY clause is a constant integer between 1
95178** and N where N is the number of columns in the compound SELECT.
95179**
95180** ORDER BY terms that are already an integer between 1 and N are
95181** unmodified. ORDER BY terms that are integers outside the range of
95182** 1 through N generate an error. ORDER BY terms that are expressions
95183** are matched against result set expressions of compound SELECT
95184** beginning with the left-most SELECT and working toward the right.
95185** At the first match, the ORDER BY expression is transformed into
95186** the integer column number.
95187**
95188** Return the number of errors seen.
95189*/
95190static int resolveCompoundOrderBy(
95191 Parse *pParse, /* Parsing context. Leave error messages here */
95192 Select *pSelect /* The SELECT statement containing the ORDER BY */
95193){
95194 int i;
95195 ExprList *pOrderBy;
95196 ExprList *pEList;
95197 sqlite3 *db;
95198 int moreToDo = 1;
95199
95200 pOrderBy = pSelect->pOrderBy;
95201 if( pOrderBy==0 ) return 0;
95202 db = pParse->db;
95203 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
95204 sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
95205 return 1;
95206 }
95207 for(i=0; i<pOrderBy->nExpr; i++){
95208 pOrderBy->a[i].done = 0;
95209 }
95210 pSelect->pNext = 0;
95211 while( pSelect->pPrior ){
95212 pSelect->pPrior->pNext = pSelect;
95213 pSelect = pSelect->pPrior;
95214 }
95215 while( pSelect && moreToDo ){
95216 struct ExprList_item *pItem;
95217 moreToDo = 0;
95218 pEList = pSelect->pEList;
95219 assert( pEList!=0 );
95220 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
95221 int iCol = -1;
95222 Expr *pE, *pDup;
95223 if( pItem->done ) continue;
95224 pE = sqlite3ExprSkipCollate(pItem->pExpr);
95225 if( sqlite3ExprIsInteger(pE, &iCol) ){
95226 if( iCol<=0 || iCol>pEList->nExpr ){
95227 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
95228 return 1;
95229 }
95230 }else{
95231 iCol = resolveAsName(pParse, pEList, pE);
95232 if( iCol==0 ){
95233 pDup = sqlite3ExprDup(db, pE, 0);
95234 if( !db->mallocFailed ){
95235 assert(pDup);
95236 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
95237 }
95238 sqlite3ExprDelete(db, pDup);
95239 }
95240 }
95241 if( iCol>0 ){
95242 /* Convert the ORDER BY term into an integer column number iCol,
95243 ** taking care to preserve the COLLATE clause if it exists */
95244 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
95245 if( pNew==0 ) return 1;
95246 pNew->flags |= EP_IntValue;
95247 pNew->u.iValue = iCol;
95248 if( pItem->pExpr==pE ){
95249 pItem->pExpr = pNew;
95250 }else{
95251 Expr *pParent = pItem->pExpr;
95252 assert( pParent->op==TK_COLLATE );
95253 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
95254 assert( pParent->pLeft==pE );
95255 pParent->pLeft = pNew;
95256 }
95257 sqlite3ExprDelete(db, pE);
95258 pItem->u.x.iOrderByCol = (u16)iCol;
95259 pItem->done = 1;
95260 }else{
95261 moreToDo = 1;
95262 }
95263 }
95264 pSelect = pSelect->pNext;
95265 }
95266 for(i=0; i<pOrderBy->nExpr; i++){
95267 if( pOrderBy->a[i].done==0 ){
95268 sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
95269 "column in the result set", i+1);
95270 return 1;
95271 }
95272 }
95273 return 0;
95274}
95275
95276/*
95277** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
95278** the SELECT statement pSelect. If any term is reference to a
95279** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
95280** field) then convert that term into a copy of the corresponding result set
95281** column.
95282**
95283** If any errors are detected, add an error message to pParse and
95284** return non-zero. Return zero if no errors are seen.
95285*/
95286SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
95287 Parse *pParse, /* Parsing context. Leave error messages here */
95288 Select *pSelect, /* The SELECT statement containing the clause */
95289 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
95290 const char *zType /* "ORDER" or "GROUP" */
95291){
95292 int i;
95293 sqlite3 *db = pParse->db;
95294 ExprList *pEList;
95295 struct ExprList_item *pItem;
95296
95297 if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
95298 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
95299 sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
95300 return 1;
95301 }
95302 pEList = pSelect->pEList;
95303 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
95304 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
95305 if( pItem->u.x.iOrderByCol ){
95306 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
95307 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
95308 return 1;
95309 }
95310 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
95311 zType,0);
95312 }
95313 }
95314 return 0;
95315}
95316
95317/*
95318** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
95319** The Name context of the SELECT statement is pNC. zType is either
95320** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
95321**
95322** This routine resolves each term of the clause into an expression.
95323** If the order-by term is an integer I between 1 and N (where N is the
95324** number of columns in the result set of the SELECT) then the expression
95325** in the resolution is a copy of the I-th result-set expression. If
95326** the order-by term is an identifier that corresponds to the AS-name of
95327** a result-set expression, then the term resolves to a copy of the
95328** result-set expression. Otherwise, the expression is resolved in
95329** the usual way - using sqlite3ResolveExprNames().
95330**
95331** This routine returns the number of errors. If errors occur, then
95332** an appropriate error message might be left in pParse. (OOM errors
95333** excepted.)
95334*/
95335static int resolveOrderGroupBy(
95336 NameContext *pNC, /* The name context of the SELECT statement */
95337 Select *pSelect, /* The SELECT statement holding pOrderBy */
95338 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
95339 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
95340){
95341 int i, j; /* Loop counters */
95342 int iCol; /* Column number */
95343 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
95344 Parse *pParse; /* Parsing context */
95345 int nResult; /* Number of terms in the result set */
95346
95347 if( pOrderBy==0 ) return 0;
95348 nResult = pSelect->pEList->nExpr;
95349 pParse = pNC->pParse;
95350 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
95351 Expr *pE = pItem->pExpr;
95352 Expr *pE2 = sqlite3ExprSkipCollate(pE);
95353 if( zType[0]!='G' ){
95354 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
95355 if( iCol>0 ){
95356 /* If an AS-name match is found, mark this ORDER BY column as being
95357 ** a copy of the iCol-th result-set column. The subsequent call to
95358 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
95359 ** copy of the iCol-th result-set expression. */
95360 pItem->u.x.iOrderByCol = (u16)iCol;
95361 continue;
95362 }
95363 }
95364 if( sqlite3ExprIsInteger(pE2, &iCol) ){
95365 /* The ORDER BY term is an integer constant. Again, set the column
95366 ** number so that sqlite3ResolveOrderGroupBy() will convert the
95367 ** order-by term to a copy of the result-set expression */
95368 if( iCol<1 || iCol>0xffff ){
95369 resolveOutOfRangeError(pParse, zType, i+1, nResult);
95370 return 1;
95371 }
95372 pItem->u.x.iOrderByCol = (u16)iCol;
95373 continue;
95374 }
95375
95376 /* Otherwise, treat the ORDER BY term as an ordinary expression */
95377 pItem->u.x.iOrderByCol = 0;
95378 if( sqlite3ResolveExprNames(pNC, pE) ){
95379 return 1;
95380 }
95381 for(j=0; j<pSelect->pEList->nExpr; j++){
95382 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
95383#ifndef SQLITE_OMIT_WINDOWFUNC
95384 if( pE->pWin ){
95385 /* Since this window function is being changed into a reference
95386 ** to the same window function the result set, remove the instance
95387 ** of this window function from the Select.pWin list. */
95388 Window **pp;
95389 for(pp=&pSelect->pWin; *pp; pp=&(*pp)->pNextWin){
95390 if( *pp==pE->pWin ){
95391 *pp = (*pp)->pNextWin;
95392 }
95393 }
95394 }
95395#endif
95396 pItem->u.x.iOrderByCol = j+1;
95397 }
95398 }
95399 }
95400 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
95401}
95402
95403/*
95404** Resolve names in the SELECT statement p and all of its descendants.
95405*/
95406static int resolveSelectStep(Walker *pWalker, Select *p){
95407 NameContext *pOuterNC; /* Context that contains this SELECT */
95408 NameContext sNC; /* Name context of this SELECT */
95409 int isCompound; /* True if p is a compound select */
95410 int nCompound; /* Number of compound terms processed so far */
95411 Parse *pParse; /* Parsing context */
95412 int i; /* Loop counter */
95413 ExprList *pGroupBy; /* The GROUP BY clause */
95414 Select *pLeftmost; /* Left-most of SELECT of a compound */
95415 sqlite3 *db; /* Database connection */
95416
95417
95418 assert( p!=0 );
95419 if( p->selFlags & SF_Resolved ){
95420 return WRC_Prune;
95421 }
95422 pOuterNC = pWalker->u.pNC;
95423 pParse = pWalker->pParse;
95424 db = pParse->db;
95425
95426 /* Normally sqlite3SelectExpand() will be called first and will have
95427 ** already expanded this SELECT. However, if this is a subquery within
95428 ** an expression, sqlite3ResolveExprNames() will be called without a
95429 ** prior call to sqlite3SelectExpand(). When that happens, let
95430 ** sqlite3SelectPrep() do all of the processing for this SELECT.
95431 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
95432 ** this routine in the correct order.
95433 */
95434 if( (p->selFlags & SF_Expanded)==0 ){
95435 sqlite3SelectPrep(pParse, p, pOuterNC);
95436 return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
95437 }
95438
95439 isCompound = p->pPrior!=0;
95440 nCompound = 0;
95441 pLeftmost = p;
95442 while( p ){
95443 assert( (p->selFlags & SF_Expanded)!=0 );
95444 assert( (p->selFlags & SF_Resolved)==0 );
95445 p->selFlags |= SF_Resolved;
95446
95447 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
95448 ** are not allowed to refer to any names, so pass an empty NameContext.
95449 */
95450 memset(&sNC, 0, sizeof(sNC));
95451 sNC.pParse = pParse;
95452 sNC.pWinSelect = p;
95453 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
95454 return WRC_Abort;
95455 }
95456
95457 /* If the SF_Converted flags is set, then this Select object was
95458 ** was created by the convertCompoundSelectToSubquery() function.
95459 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
95460 ** as if it were part of the sub-query, not the parent. This block
95461 ** moves the pOrderBy down to the sub-query. It will be moved back
95462 ** after the names have been resolved. */
95463 if( p->selFlags & SF_Converted ){
95464 Select *pSub = p->pSrc->a[0].pSelect;
95465 assert( p->pSrc->nSrc==1 && p->pOrderBy );
95466 assert( pSub->pPrior && pSub->pOrderBy==0 );
95467 pSub->pOrderBy = p->pOrderBy;
95468 p->pOrderBy = 0;
95469 }
95470
95471 /* Recursively resolve names in all subqueries
95472 */
95473 for(i=0; i<p->pSrc->nSrc; i++){
95474 struct SrcList_item *pItem = &p->pSrc->a[i];
95475 if( pItem->pSelect ){
95476 NameContext *pNC; /* Used to iterate name contexts */
95477 int nRef = 0; /* Refcount for pOuterNC and outer contexts */
95478 const char *zSavedContext = pParse->zAuthContext;
95479
95480 /* Count the total number of references to pOuterNC and all of its
95481 ** parent contexts. After resolving references to expressions in
95482 ** pItem->pSelect, check if this value has changed. If so, then
95483 ** SELECT statement pItem->pSelect must be correlated. Set the
95484 ** pItem->fg.isCorrelated flag if this is the case. */
95485 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
95486
95487 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
95488 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
95489 pParse->zAuthContext = zSavedContext;
95490 if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
95491
95492 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
95493 assert( pItem->fg.isCorrelated==0 && nRef<=0 );
95494 pItem->fg.isCorrelated = (nRef!=0);
95495 }
95496 }
95497
95498 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
95499 ** resolve the result-set expression list.
95500 */
95501 sNC.ncFlags = NC_AllowAgg|NC_AllowWin;
95502 sNC.pSrcList = p->pSrc;
95503 sNC.pNext = pOuterNC;
95504
95505 /* Resolve names in the result set. */
95506 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
95507 sNC.ncFlags &= ~NC_AllowWin;
95508
95509 /* If there are no aggregate functions in the result-set, and no GROUP BY
95510 ** expression, do not allow aggregates in any of the other expressions.
95511 */
95512 assert( (p->selFlags & SF_Aggregate)==0 );
95513 pGroupBy = p->pGroupBy;
95514 if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
95515 assert( NC_MinMaxAgg==SF_MinMaxAgg );
95516 p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
95517 }else{
95518 sNC.ncFlags &= ~NC_AllowAgg;
95519 }
95520
95521 /* If a HAVING clause is present, then there must be a GROUP BY clause.
95522 */
95523 if( p->pHaving && !pGroupBy ){
95524 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
95525 return WRC_Abort;
95526 }
95527
95528 /* Add the output column list to the name-context before parsing the
95529 ** other expressions in the SELECT statement. This is so that
95530 ** expressions in the WHERE clause (etc.) can refer to expressions by
95531 ** aliases in the result set.
95532 **
95533 ** Minor point: If this is the case, then the expression will be
95534 ** re-evaluated for each reference to it.
95535 */
95536 assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert))==0 );
95537 sNC.uNC.pEList = p->pEList;
95538 sNC.ncFlags |= NC_UEList;
95539 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
95540 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
95541
95542 /* Resolve names in table-valued-function arguments */
95543 for(i=0; i<p->pSrc->nSrc; i++){
95544 struct SrcList_item *pItem = &p->pSrc->a[i];
95545 if( pItem->fg.isTabFunc
95546 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
95547 ){
95548 return WRC_Abort;
95549 }
95550 }
95551
95552 /* The ORDER BY and GROUP BY clauses may not refer to terms in
95553 ** outer queries
95554 */
95555 sNC.pNext = 0;
95556 sNC.ncFlags |= NC_AllowAgg|NC_AllowWin;
95557
95558 /* If this is a converted compound query, move the ORDER BY clause from
95559 ** the sub-query back to the parent query. At this point each term
95560 ** within the ORDER BY clause has been transformed to an integer value.
95561 ** These integers will be replaced by copies of the corresponding result
95562 ** set expressions by the call to resolveOrderGroupBy() below. */
95563 if( p->selFlags & SF_Converted ){
95564 Select *pSub = p->pSrc->a[0].pSelect;
95565 p->pOrderBy = pSub->pOrderBy;
95566 pSub->pOrderBy = 0;
95567 }
95568
95569 /* Process the ORDER BY clause for singleton SELECT statements.
95570 ** The ORDER BY clause for compounds SELECT statements is handled
95571 ** below, after all of the result-sets for all of the elements of
95572 ** the compound have been resolved.
95573 **
95574 ** If there is an ORDER BY clause on a term of a compound-select other
95575 ** than the right-most term, then that is a syntax error. But the error
95576 ** is not detected until much later, and so we need to go ahead and
95577 ** resolve those symbols on the incorrect ORDER BY for consistency.
95578 */
95579 if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
95580 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
95581 ){
95582 return WRC_Abort;
95583 }
95584 if( db->mallocFailed ){
95585 return WRC_Abort;
95586 }
95587 sNC.ncFlags &= ~NC_AllowWin;
95588
95589 /* Resolve the GROUP BY clause. At the same time, make sure
95590 ** the GROUP BY clause does not contain aggregate functions.
95591 */
95592 if( pGroupBy ){
95593 struct ExprList_item *pItem;
95594
95595 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
95596 return WRC_Abort;
95597 }
95598 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
95599 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
95600 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
95601 "the GROUP BY clause");
95602 return WRC_Abort;
95603 }
95604 }
95605 }
95606
95607 /* If this is part of a compound SELECT, check that it has the right
95608 ** number of expressions in the select list. */
95609 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
95610 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
95611 return WRC_Abort;
95612 }
95613
95614 /* Advance to the next term of the compound
95615 */
95616 p = p->pPrior;
95617 nCompound++;
95618 }
95619
95620 /* Resolve the ORDER BY on a compound SELECT after all terms of
95621 ** the compound have been resolved.
95622 */
95623 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
95624 return WRC_Abort;
95625 }
95626
95627 return WRC_Prune;
95628}
95629
95630/*
95631** This routine walks an expression tree and resolves references to
95632** table columns and result-set columns. At the same time, do error
95633** checking on function usage and set a flag if any aggregate functions
95634** are seen.
95635**
95636** To resolve table columns references we look for nodes (or subtrees) of the
95637** form X.Y.Z or Y.Z or just Z where
95638**
95639** X: The name of a database. Ex: "main" or "temp" or
95640** the symbolic name assigned to an ATTACH-ed database.
95641**
95642** Y: The name of a table in a FROM clause. Or in a trigger
95643** one of the special names "old" or "new".
95644**
95645** Z: The name of a column in table Y.
95646**
95647** The node at the root of the subtree is modified as follows:
95648**
95649** Expr.op Changed to TK_COLUMN
95650** Expr.pTab Points to the Table object for X.Y
95651** Expr.iColumn The column index in X.Y. -1 for the rowid.
95652** Expr.iTable The VDBE cursor number for X.Y
95653**
95654**
95655** To resolve result-set references, look for expression nodes of the
95656** form Z (with no X and Y prefix) where the Z matches the right-hand
95657** size of an AS clause in the result-set of a SELECT. The Z expression
95658** is replaced by a copy of the left-hand side of the result-set expression.
95659** Table-name and function resolution occurs on the substituted expression
95660** tree. For example, in:
95661**
95662** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
95663**
95664** The "x" term of the order by is replaced by "a+b" to render:
95665**
95666** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
95667**
95668** Function calls are checked to make sure that the function is
95669** defined and that the correct number of arguments are specified.
95670** If the function is an aggregate function, then the NC_HasAgg flag is
95671** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
95672** If an expression contains aggregate functions then the EP_Agg
95673** property on the expression is set.
95674**
95675** An error message is left in pParse if anything is amiss. The number
95676** if errors is returned.
95677*/
95678SQLITE_PRIVATE int sqlite3ResolveExprNames(
95679 NameContext *pNC, /* Namespace to resolve expressions in. */
95680 Expr *pExpr /* The expression to be analyzed. */
95681){
95682 u16 savedHasAgg;
95683 Walker w;
95684
95685 if( pExpr==0 ) return SQLITE_OK;
95686 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
95687 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
95688 w.pParse = pNC->pParse;
95689 w.xExprCallback = resolveExprStep;
95690 w.xSelectCallback = resolveSelectStep;
95691 w.xSelectCallback2 = 0;
95692 w.u.pNC = pNC;
95693#if SQLITE_MAX_EXPR_DEPTH>0
95694 w.pParse->nHeight += pExpr->nHeight;
95695 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
95696 return SQLITE_ERROR;
95697 }
95698#endif
95699 sqlite3WalkExpr(&w, pExpr);
95700#if SQLITE_MAX_EXPR_DEPTH>0
95701 w.pParse->nHeight -= pExpr->nHeight;
95702#endif
95703 if( pNC->ncFlags & NC_HasAgg ){
95704 ExprSetProperty(pExpr, EP_Agg);
95705 }
95706 pNC->ncFlags |= savedHasAgg;
95707 return pNC->nErr>0 || w.pParse->nErr>0;
95708}
95709
95710/*
95711** Resolve all names for all expression in an expression list. This is
95712** just like sqlite3ResolveExprNames() except that it works for an expression
95713** list rather than a single expression.
95714*/
95715SQLITE_PRIVATE int sqlite3ResolveExprListNames(
95716 NameContext *pNC, /* Namespace to resolve expressions in. */
95717 ExprList *pList /* The expression list to be analyzed. */
95718){
95719 int i;
95720 if( pList ){
95721 for(i=0; i<pList->nExpr; i++){
95722 if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort;
95723 }
95724 }
95725 return WRC_Continue;
95726}
95727
95728/*
95729** Resolve all names in all expressions of a SELECT and in all
95730** decendents of the SELECT, including compounds off of p->pPrior,
95731** subqueries in expressions, and subqueries used as FROM clause
95732** terms.
95733**
95734** See sqlite3ResolveExprNames() for a description of the kinds of
95735** transformations that occur.
95736**
95737** All SELECT statements should have been expanded using
95738** sqlite3SelectExpand() prior to invoking this routine.
95739*/
95740SQLITE_PRIVATE void sqlite3ResolveSelectNames(
95741 Parse *pParse, /* The parser context */
95742 Select *p, /* The SELECT statement being coded. */
95743 NameContext *pOuterNC /* Name context for parent SELECT statement */
95744){
95745 Walker w;
95746
95747 assert( p!=0 );
95748 w.xExprCallback = resolveExprStep;
95749 w.xSelectCallback = resolveSelectStep;
95750 w.xSelectCallback2 = 0;
95751 w.pParse = pParse;
95752 w.u.pNC = pOuterNC;
95753 sqlite3WalkSelect(&w, p);
95754}
95755
95756/*
95757** Resolve names in expressions that can only reference a single table:
95758**
95759** * CHECK constraints
95760** * WHERE clauses on partial indices
95761**
95762** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
95763** is set to -1 and the Expr.iColumn value is set to the column number.
95764**
95765** Any errors cause an error message to be set in pParse.
95766*/
95767SQLITE_PRIVATE void sqlite3ResolveSelfReference(
95768 Parse *pParse, /* Parsing context */
95769 Table *pTab, /* The table being referenced */
95770 int type, /* NC_IsCheck or NC_PartIdx or NC_IdxExpr */
95771 Expr *pExpr, /* Expression to resolve. May be NULL. */
95772 ExprList *pList /* Expression list to resolve. May be NULL. */
95773){
95774 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
95775 NameContext sNC; /* Name context for pParse->pNewTable */
95776
95777 assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr );
95778 memset(&sNC, 0, sizeof(sNC));
95779 memset(&sSrc, 0, sizeof(sSrc));
95780 sSrc.nSrc = 1;
95781 sSrc.a[0].zName = pTab->zName;
95782 sSrc.a[0].pTab = pTab;
95783 sSrc.a[0].iCursor = -1;
95784 sNC.pParse = pParse;
95785 sNC.pSrcList = &sSrc;
95786 sNC.ncFlags = type;
95787 if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
95788 if( pList ) sqlite3ResolveExprListNames(&sNC, pList);
95789}
95790
95791/************** End of resolve.c *********************************************/
95792/************** Begin file expr.c ********************************************/
95793/*
95794** 2001 September 15
95795**
95796** The author disclaims copyright to this source code. In place of
95797** a legal notice, here is a blessing:
95798**
95799** May you do good and not evil.
95800** May you find forgiveness for yourself and forgive others.
95801** May you share freely, never taking more than you give.
95802**
95803*************************************************************************
95804** This file contains routines used for analyzing expressions and
95805** for generating VDBE code that evaluates expressions in SQLite.
95806*/
95807/* #include "sqliteInt.h" */
95808
95809/* Forward declarations */
95810static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
95811static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
95812
95813/*
95814** Return the affinity character for a single column of a table.
95815*/
95816SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table *pTab, int iCol){
95817 assert( iCol<pTab->nCol );
95818 return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
95819}
95820
95821/*
95822** Return the 'affinity' of the expression pExpr if any.
95823**
95824** If pExpr is a column, a reference to a column via an 'AS' alias,
95825** or a sub-select with a column as the return value, then the
95826** affinity of that column is returned. Otherwise, 0x00 is returned,
95827** indicating no affinity for the expression.
95828**
95829** i.e. the WHERE clause expressions in the following statements all
95830** have an affinity:
95831**
95832** CREATE TABLE t1(a);
95833** SELECT * FROM t1 WHERE a;
95834** SELECT a AS b FROM t1 WHERE b;
95835** SELECT * FROM t1 WHERE (select a from t1);
95836*/
95837SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
95838 int op;
95839 pExpr = sqlite3ExprSkipCollate(pExpr);
95840 if( pExpr->flags & EP_Generic ) return 0;
95841 op = pExpr->op;
95842 if( op==TK_SELECT ){
95843 assert( pExpr->flags&EP_xIsSelect );
95844 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
95845 }
95846 if( op==TK_REGISTER ) op = pExpr->op2;
95847#ifndef SQLITE_OMIT_CAST
95848 if( op==TK_CAST ){
95849 assert( !ExprHasProperty(pExpr, EP_IntValue) );
95850 return sqlite3AffinityType(pExpr->u.zToken, 0);
95851 }
95852#endif
95853 if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->pTab ){
95854 return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
95855 }
95856 if( op==TK_SELECT_COLUMN ){
95857 assert( pExpr->pLeft->flags&EP_xIsSelect );
95858 return sqlite3ExprAffinity(
95859 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
95860 );
95861 }
95862 return pExpr->affinity;
95863}
95864
95865/*
95866** Set the collating sequence for expression pExpr to be the collating
95867** sequence named by pToken. Return a pointer to a new Expr node that
95868** implements the COLLATE operator.
95869**
95870** If a memory allocation error occurs, that fact is recorded in pParse->db
95871** and the pExpr parameter is returned unchanged.
95872*/
95873SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
95874 Parse *pParse, /* Parsing context */
95875 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
95876 const Token *pCollName, /* Name of collating sequence */
95877 int dequote /* True to dequote pCollName */
95878){
95879 if( pCollName->n>0 ){
95880 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
95881 if( pNew ){
95882 pNew->pLeft = pExpr;
95883 pNew->flags |= EP_Collate|EP_Skip;
95884 pExpr = pNew;
95885 }
95886 }
95887 return pExpr;
95888}
95889SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
95890 Token s;
95891 assert( zC!=0 );
95892 sqlite3TokenInit(&s, (char*)zC);
95893 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
95894}
95895
95896/*
95897** Skip over any TK_COLLATE operators and any unlikely()
95898** or likelihood() function at the root of an expression.
95899*/
95900SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
95901 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
95902 if( ExprHasProperty(pExpr, EP_Unlikely) ){
95903 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
95904 assert( pExpr->x.pList->nExpr>0 );
95905 assert( pExpr->op==TK_FUNCTION );
95906 pExpr = pExpr->x.pList->a[0].pExpr;
95907 }else{
95908 assert( pExpr->op==TK_COLLATE );
95909 pExpr = pExpr->pLeft;
95910 }
95911 }
95912 return pExpr;
95913}
95914
95915/*
95916** Return the collation sequence for the expression pExpr. If
95917** there is no defined collating sequence, return NULL.
95918**
95919** See also: sqlite3ExprNNCollSeq()
95920**
95921** The sqlite3ExprNNCollSeq() works the same exact that it returns the
95922** default collation if pExpr has no defined collation.
95923**
95924** The collating sequence might be determined by a COLLATE operator
95925** or by the presence of a column with a defined collating sequence.
95926** COLLATE operators take first precedence. Left operands take
95927** precedence over right operands.
95928*/
95929SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
95930 sqlite3 *db = pParse->db;
95931 CollSeq *pColl = 0;
95932 Expr *p = pExpr;
95933 while( p ){
95934 int op = p->op;
95935 if( p->flags & EP_Generic ) break;
95936 if( (op==TK_AGG_COLUMN || op==TK_COLUMN
95937 || op==TK_REGISTER || op==TK_TRIGGER)
95938 && p->pTab!=0
95939 ){
95940 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
95941 ** a TK_COLUMN but was previously evaluated and cached in a register */
95942 int j = p->iColumn;
95943 if( j>=0 ){
95944 const char *zColl = p->pTab->aCol[j].zColl;
95945 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
95946 }
95947 break;
95948 }
95949 if( op==TK_CAST || op==TK_UPLUS ){
95950 p = p->pLeft;
95951 continue;
95952 }
95953 if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
95954 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
95955 break;
95956 }
95957 if( p->flags & EP_Collate ){
95958 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
95959 p = p->pLeft;
95960 }else{
95961 Expr *pNext = p->pRight;
95962 /* The Expr.x union is never used at the same time as Expr.pRight */
95963 assert( p->x.pList==0 || p->pRight==0 );
95964 /* p->flags holds EP_Collate and p->pLeft->flags does not. And
95965 ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at
95966 ** least one EP_Collate. Thus the following two ALWAYS. */
95967 if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){
95968 int i;
95969 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
95970 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
95971 pNext = p->x.pList->a[i].pExpr;
95972 break;
95973 }
95974 }
95975 }
95976 p = pNext;
95977 }
95978 }else{
95979 break;
95980 }
95981 }
95982 if( sqlite3CheckCollSeq(pParse, pColl) ){
95983 pColl = 0;
95984 }
95985 return pColl;
95986}
95987
95988/*
95989** Return the collation sequence for the expression pExpr. If
95990** there is no defined collating sequence, return a pointer to the
95991** defautl collation sequence.
95992**
95993** See also: sqlite3ExprCollSeq()
95994**
95995** The sqlite3ExprCollSeq() routine works the same except that it
95996** returns NULL if there is no defined collation.
95997*/
95998SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr){
95999 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
96000 if( p==0 ) p = pParse->db->pDfltColl;
96001 assert( p!=0 );
96002 return p;
96003}
96004
96005/*
96006** Return TRUE if the two expressions have equivalent collating sequences.
96007*/
96008SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse *pParse, Expr *pE1, Expr *pE2){
96009 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
96010 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
96011 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
96012}
96013
96014/*
96015** pExpr is an operand of a comparison operator. aff2 is the
96016** type affinity of the other operand. This routine returns the
96017** type affinity that should be used for the comparison operator.
96018*/
96019SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
96020 char aff1 = sqlite3ExprAffinity(pExpr);
96021 if( aff1 && aff2 ){
96022 /* Both sides of the comparison are columns. If one has numeric
96023 ** affinity, use that. Otherwise use no affinity.
96024 */
96025 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
96026 return SQLITE_AFF_NUMERIC;
96027 }else{
96028 return SQLITE_AFF_BLOB;
96029 }
96030 }else if( !aff1 && !aff2 ){
96031 /* Neither side of the comparison is a column. Compare the
96032 ** results directly.
96033 */
96034 return SQLITE_AFF_BLOB;
96035 }else{
96036 /* One side is a column, the other is not. Use the columns affinity. */
96037 assert( aff1==0 || aff2==0 );
96038 return (aff1 + aff2);
96039 }
96040}
96041
96042/*
96043** pExpr is a comparison operator. Return the type affinity that should
96044** be applied to both operands prior to doing the comparison.
96045*/
96046static char comparisonAffinity(Expr *pExpr){
96047 char aff;
96048 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
96049 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
96050 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
96051 assert( pExpr->pLeft );
96052 aff = sqlite3ExprAffinity(pExpr->pLeft);
96053 if( pExpr->pRight ){
96054 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
96055 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
96056 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
96057 }else if( aff==0 ){
96058 aff = SQLITE_AFF_BLOB;
96059 }
96060 return aff;
96061}
96062
96063/*
96064** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
96065** idx_affinity is the affinity of an indexed column. Return true
96066** if the index with affinity idx_affinity may be used to implement
96067** the comparison in pExpr.
96068*/
96069SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
96070 char aff = comparisonAffinity(pExpr);
96071 switch( aff ){
96072 case SQLITE_AFF_BLOB:
96073 return 1;
96074 case SQLITE_AFF_TEXT:
96075 return idx_affinity==SQLITE_AFF_TEXT;
96076 default:
96077 return sqlite3IsNumericAffinity(idx_affinity);
96078 }
96079}
96080
96081/*
96082** Return the P5 value that should be used for a binary comparison
96083** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
96084*/
96085static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
96086 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
96087 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
96088 return aff;
96089}
96090
96091/*
96092** Return a pointer to the collation sequence that should be used by
96093** a binary comparison operator comparing pLeft and pRight.
96094**
96095** If the left hand expression has a collating sequence type, then it is
96096** used. Otherwise the collation sequence for the right hand expression
96097** is used, or the default (BINARY) if neither expression has a collating
96098** type.
96099**
96100** Argument pRight (but not pLeft) may be a null pointer. In this case,
96101** it is not considered.
96102*/
96103SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
96104 Parse *pParse,
96105 Expr *pLeft,
96106 Expr *pRight
96107){
96108 CollSeq *pColl;
96109 assert( pLeft );
96110 if( pLeft->flags & EP_Collate ){
96111 pColl = sqlite3ExprCollSeq(pParse, pLeft);
96112 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
96113 pColl = sqlite3ExprCollSeq(pParse, pRight);
96114 }else{
96115 pColl = sqlite3ExprCollSeq(pParse, pLeft);
96116 if( !pColl ){
96117 pColl = sqlite3ExprCollSeq(pParse, pRight);
96118 }
96119 }
96120 return pColl;
96121}
96122
96123/*
96124** Generate code for a comparison operator.
96125*/
96126static int codeCompare(
96127 Parse *pParse, /* The parsing (and code generating) context */
96128 Expr *pLeft, /* The left operand */
96129 Expr *pRight, /* The right operand */
96130 int opcode, /* The comparison opcode */
96131 int in1, int in2, /* Register holding operands */
96132 int dest, /* Jump here if true. */
96133 int jumpIfNull /* If true, jump if either operand is NULL */
96134){
96135 int p5;
96136 int addr;
96137 CollSeq *p4;
96138
96139 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
96140 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
96141 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
96142 (void*)p4, P4_COLLSEQ);
96143 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
96144 return addr;
96145}
96146
96147/*
96148** Return true if expression pExpr is a vector, or false otherwise.
96149**
96150** A vector is defined as any expression that results in two or more
96151** columns of result. Every TK_VECTOR node is an vector because the
96152** parser will not generate a TK_VECTOR with fewer than two entries.
96153** But a TK_SELECT might be either a vector or a scalar. It is only
96154** considered a vector if it has two or more result columns.
96155*/
96156SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr){
96157 return sqlite3ExprVectorSize(pExpr)>1;
96158}
96159
96160/*
96161** If the expression passed as the only argument is of type TK_VECTOR
96162** return the number of expressions in the vector. Or, if the expression
96163** is a sub-select, return the number of columns in the sub-select. For
96164** any other type of expression, return 1.
96165*/
96166SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr){
96167 u8 op = pExpr->op;
96168 if( op==TK_REGISTER ) op = pExpr->op2;
96169 if( op==TK_VECTOR ){
96170 return pExpr->x.pList->nExpr;
96171 }else if( op==TK_SELECT ){
96172 return pExpr->x.pSelect->pEList->nExpr;
96173 }else{
96174 return 1;
96175 }
96176}
96177
96178/*
96179** Return a pointer to a subexpression of pVector that is the i-th
96180** column of the vector (numbered starting with 0). The caller must
96181** ensure that i is within range.
96182**
96183** If pVector is really a scalar (and "scalar" here includes subqueries
96184** that return a single column!) then return pVector unmodified.
96185**
96186** pVector retains ownership of the returned subexpression.
96187**
96188** If the vector is a (SELECT ...) then the expression returned is
96189** just the expression for the i-th term of the result set, and may
96190** not be ready for evaluation because the table cursor has not yet
96191** been positioned.
96192*/
96193SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
96194 assert( i<sqlite3ExprVectorSize(pVector) );
96195 if( sqlite3ExprIsVector(pVector) ){
96196 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
96197 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
96198 return pVector->x.pSelect->pEList->a[i].pExpr;
96199 }else{
96200 return pVector->x.pList->a[i].pExpr;
96201 }
96202 }
96203 return pVector;
96204}
96205
96206/*
96207** Compute and return a new Expr object which when passed to
96208** sqlite3ExprCode() will generate all necessary code to compute
96209** the iField-th column of the vector expression pVector.
96210**
96211** It is ok for pVector to be a scalar (as long as iField==0).
96212** In that case, this routine works like sqlite3ExprDup().
96213**
96214** The caller owns the returned Expr object and is responsible for
96215** ensuring that the returned value eventually gets freed.
96216**
96217** The caller retains ownership of pVector. If pVector is a TK_SELECT,
96218** then the returned object will reference pVector and so pVector must remain
96219** valid for the life of the returned object. If pVector is a TK_VECTOR
96220** or a scalar expression, then it can be deleted as soon as this routine
96221** returns.
96222**
96223** A trick to cause a TK_SELECT pVector to be deleted together with
96224** the returned Expr object is to attach the pVector to the pRight field
96225** of the returned TK_SELECT_COLUMN Expr object.
96226*/
96227SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(
96228 Parse *pParse, /* Parsing context */
96229 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
96230 int iField /* Which column of the vector to return */
96231){
96232 Expr *pRet;
96233 if( pVector->op==TK_SELECT ){
96234 assert( pVector->flags & EP_xIsSelect );
96235 /* The TK_SELECT_COLUMN Expr node:
96236 **
96237 ** pLeft: pVector containing TK_SELECT. Not deleted.
96238 ** pRight: not used. But recursively deleted.
96239 ** iColumn: Index of a column in pVector
96240 ** iTable: 0 or the number of columns on the LHS of an assignment
96241 ** pLeft->iTable: First in an array of register holding result, or 0
96242 ** if the result is not yet computed.
96243 **
96244 ** sqlite3ExprDelete() specifically skips the recursive delete of
96245 ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector
96246 ** can be attached to pRight to cause this node to take ownership of
96247 ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes
96248 ** with the same pLeft pointer to the pVector, but only one of them
96249 ** will own the pVector.
96250 */
96251 pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0);
96252 if( pRet ){
96253 pRet->iColumn = iField;
96254 pRet->pLeft = pVector;
96255 }
96256 assert( pRet==0 || pRet->iTable==0 );
96257 }else{
96258 if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr;
96259 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
96260 }
96261 return pRet;
96262}
96263
96264/*
96265** If expression pExpr is of type TK_SELECT, generate code to evaluate
96266** it. Return the register in which the result is stored (or, if the
96267** sub-select returns more than one column, the first in an array
96268** of registers in which the result is stored).
96269**
96270** If pExpr is not a TK_SELECT expression, return 0.
96271*/
96272static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
96273 int reg = 0;
96274#ifndef SQLITE_OMIT_SUBQUERY
96275 if( pExpr->op==TK_SELECT ){
96276 reg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
96277 }
96278#endif
96279 return reg;
96280}
96281
96282/*
96283** Argument pVector points to a vector expression - either a TK_VECTOR
96284** or TK_SELECT that returns more than one column. This function returns
96285** the register number of a register that contains the value of
96286** element iField of the vector.
96287**
96288** If pVector is a TK_SELECT expression, then code for it must have
96289** already been generated using the exprCodeSubselect() routine. In this
96290** case parameter regSelect should be the first in an array of registers
96291** containing the results of the sub-select.
96292**
96293** If pVector is of type TK_VECTOR, then code for the requested field
96294** is generated. In this case (*pRegFree) may be set to the number of
96295** a temporary register to be freed by the caller before returning.
96296**
96297** Before returning, output parameter (*ppExpr) is set to point to the
96298** Expr object corresponding to element iElem of the vector.
96299*/
96300static int exprVectorRegister(
96301 Parse *pParse, /* Parse context */
96302 Expr *pVector, /* Vector to extract element from */
96303 int iField, /* Field to extract from pVector */
96304 int regSelect, /* First in array of registers */
96305 Expr **ppExpr, /* OUT: Expression element */
96306 int *pRegFree /* OUT: Temp register to free */
96307){
96308 u8 op = pVector->op;
96309 assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT );
96310 if( op==TK_REGISTER ){
96311 *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField);
96312 return pVector->iTable+iField;
96313 }
96314 if( op==TK_SELECT ){
96315 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
96316 return regSelect+iField;
96317 }
96318 *ppExpr = pVector->x.pList->a[iField].pExpr;
96319 return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
96320}
96321
96322/*
96323** Expression pExpr is a comparison between two vector values. Compute
96324** the result of the comparison (1, 0, or NULL) and write that
96325** result into register dest.
96326**
96327** The caller must satisfy the following preconditions:
96328**
96329** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
96330** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
96331** otherwise: op==pExpr->op and p5==0
96332*/
96333static void codeVectorCompare(
96334 Parse *pParse, /* Code generator context */
96335 Expr *pExpr, /* The comparison operation */
96336 int dest, /* Write results into this register */
96337 u8 op, /* Comparison operator */
96338 u8 p5 /* SQLITE_NULLEQ or zero */
96339){
96340 Vdbe *v = pParse->pVdbe;
96341 Expr *pLeft = pExpr->pLeft;
96342 Expr *pRight = pExpr->pRight;
96343 int nLeft = sqlite3ExprVectorSize(pLeft);
96344 int i;
96345 int regLeft = 0;
96346 int regRight = 0;
96347 u8 opx = op;
96348 int addrDone = sqlite3VdbeMakeLabel(v);
96349
96350 if( nLeft!=sqlite3ExprVectorSize(pRight) ){
96351 sqlite3ErrorMsg(pParse, "row value misused");
96352 return;
96353 }
96354 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
96355 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
96356 || pExpr->op==TK_LT || pExpr->op==TK_GT
96357 || pExpr->op==TK_LE || pExpr->op==TK_GE
96358 );
96359 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
96360 || (pExpr->op==TK_ISNOT && op==TK_NE) );
96361 assert( p5==0 || pExpr->op!=op );
96362 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
96363
96364 p5 |= SQLITE_STOREP2;
96365 if( opx==TK_LE ) opx = TK_LT;
96366 if( opx==TK_GE ) opx = TK_GT;
96367
96368 regLeft = exprCodeSubselect(pParse, pLeft);
96369 regRight = exprCodeSubselect(pParse, pRight);
96370
96371 for(i=0; 1 /*Loop exits by "break"*/; i++){
96372 int regFree1 = 0, regFree2 = 0;
96373 Expr *pL, *pR;
96374 int r1, r2;
96375 assert( i>=0 && i<nLeft );
96376 r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
96377 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
96378 codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5);
96379 testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
96380 testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
96381 testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
96382 testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
96383 testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
96384 testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
96385 sqlite3ReleaseTempReg(pParse, regFree1);
96386 sqlite3ReleaseTempReg(pParse, regFree2);
96387 if( i==nLeft-1 ){
96388 break;
96389 }
96390 if( opx==TK_EQ ){
96391 sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v);
96392 p5 |= SQLITE_KEEPNULL;
96393 }else if( opx==TK_NE ){
96394 sqlite3VdbeAddOp2(v, OP_If, dest, addrDone); VdbeCoverage(v);
96395 p5 |= SQLITE_KEEPNULL;
96396 }else{
96397 assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE );
96398 sqlite3VdbeAddOp2(v, OP_ElseNotEq, 0, addrDone);
96399 VdbeCoverageIf(v, op==TK_LT);
96400 VdbeCoverageIf(v, op==TK_GT);
96401 VdbeCoverageIf(v, op==TK_LE);
96402 VdbeCoverageIf(v, op==TK_GE);
96403 if( i==nLeft-2 ) opx = op;
96404 }
96405 }
96406 sqlite3VdbeResolveLabel(v, addrDone);
96407}
96408
96409#if SQLITE_MAX_EXPR_DEPTH>0
96410/*
96411** Check that argument nHeight is less than or equal to the maximum
96412** expression depth allowed. If it is not, leave an error message in
96413** pParse.
96414*/
96415SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
96416 int rc = SQLITE_OK;
96417 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
96418 if( nHeight>mxHeight ){
96419 sqlite3ErrorMsg(pParse,
96420 "Expression tree is too large (maximum depth %d)", mxHeight
96421 );
96422 rc = SQLITE_ERROR;
96423 }
96424 return rc;
96425}
96426
96427/* The following three functions, heightOfExpr(), heightOfExprList()
96428** and heightOfSelect(), are used to determine the maximum height
96429** of any expression tree referenced by the structure passed as the
96430** first argument.
96431**
96432** If this maximum height is greater than the current value pointed
96433** to by pnHeight, the second parameter, then set *pnHeight to that
96434** value.
96435*/
96436static void heightOfExpr(Expr *p, int *pnHeight){
96437 if( p ){
96438 if( p->nHeight>*pnHeight ){
96439 *pnHeight = p->nHeight;
96440 }
96441 }
96442}
96443static void heightOfExprList(ExprList *p, int *pnHeight){
96444 if( p ){
96445 int i;
96446 for(i=0; i<p->nExpr; i++){
96447 heightOfExpr(p->a[i].pExpr, pnHeight);
96448 }
96449 }
96450}
96451static void heightOfSelect(Select *pSelect, int *pnHeight){
96452 Select *p;
96453 for(p=pSelect; p; p=p->pPrior){
96454 heightOfExpr(p->pWhere, pnHeight);
96455 heightOfExpr(p->pHaving, pnHeight);
96456 heightOfExpr(p->pLimit, pnHeight);
96457 heightOfExprList(p->pEList, pnHeight);
96458 heightOfExprList(p->pGroupBy, pnHeight);
96459 heightOfExprList(p->pOrderBy, pnHeight);
96460 }
96461}
96462
96463/*
96464** Set the Expr.nHeight variable in the structure passed as an
96465** argument. An expression with no children, Expr.pList or
96466** Expr.pSelect member has a height of 1. Any other expression
96467** has a height equal to the maximum height of any other
96468** referenced Expr plus one.
96469**
96470** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
96471** if appropriate.
96472*/
96473static void exprSetHeight(Expr *p){
96474 int nHeight = 0;
96475 heightOfExpr(p->pLeft, &nHeight);
96476 heightOfExpr(p->pRight, &nHeight);
96477 if( ExprHasProperty(p, EP_xIsSelect) ){
96478 heightOfSelect(p->x.pSelect, &nHeight);
96479 }else if( p->x.pList ){
96480 heightOfExprList(p->x.pList, &nHeight);
96481 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
96482 }
96483 p->nHeight = nHeight + 1;
96484}
96485
96486/*
96487** Set the Expr.nHeight variable using the exprSetHeight() function. If
96488** the height is greater than the maximum allowed expression depth,
96489** leave an error in pParse.
96490**
96491** Also propagate all EP_Propagate flags from the Expr.x.pList into
96492** Expr.flags.
96493*/
96494SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
96495 if( pParse->nErr ) return;
96496 exprSetHeight(p);
96497 sqlite3ExprCheckHeight(pParse, p->nHeight);
96498}
96499
96500/*
96501** Return the maximum height of any expression tree referenced
96502** by the select statement passed as an argument.
96503*/
96504SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
96505 int nHeight = 0;
96506 heightOfSelect(p, &nHeight);
96507 return nHeight;
96508}
96509#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
96510/*
96511** Propagate all EP_Propagate flags from the Expr.x.pList into
96512** Expr.flags.
96513*/
96514SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
96515 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
96516 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
96517 }
96518}
96519#define exprSetHeight(y)
96520#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
96521
96522/*
96523** This routine is the core allocator for Expr nodes.
96524**
96525** Construct a new expression node and return a pointer to it. Memory
96526** for this node and for the pToken argument is a single allocation
96527** obtained from sqlite3DbMalloc(). The calling function
96528** is responsible for making sure the node eventually gets freed.
96529**
96530** If dequote is true, then the token (if it exists) is dequoted.
96531** If dequote is false, no dequoting is performed. The deQuote
96532** parameter is ignored if pToken is NULL or if the token does not
96533** appear to be quoted. If the quotes were of the form "..." (double-quotes)
96534** then the EP_DblQuoted flag is set on the expression node.
96535**
96536** Special case: If op==TK_INTEGER and pToken points to a string that
96537** can be translated into a 32-bit integer, then the token is not
96538** stored in u.zToken. Instead, the integer values is written
96539** into u.iValue and the EP_IntValue flag is set. No extra storage
96540** is allocated to hold the integer text and the dequote flag is ignored.
96541*/
96542SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
96543 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */
96544 int op, /* Expression opcode */
96545 const Token *pToken, /* Token argument. Might be NULL */
96546 int dequote /* True to dequote */
96547){
96548 Expr *pNew;
96549 int nExtra = 0;
96550 int iValue = 0;
96551
96552 assert( db!=0 );
96553 if( pToken ){
96554 if( op!=TK_INTEGER || pToken->z==0
96555 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
96556 nExtra = pToken->n+1;
96557 assert( iValue>=0 );
96558 }
96559 }
96560 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
96561 if( pNew ){
96562 memset(pNew, 0, sizeof(Expr));
96563 pNew->op = (u8)op;
96564 pNew->iAgg = -1;
96565 if( pToken ){
96566 if( nExtra==0 ){
96567 pNew->flags |= EP_IntValue|EP_Leaf;
96568 pNew->u.iValue = iValue;
96569 }else{
96570 pNew->u.zToken = (char*)&pNew[1];
96571 assert( pToken->z!=0 || pToken->n==0 );
96572 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
96573 pNew->u.zToken[pToken->n] = 0;
96574 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
96575 if( pNew->u.zToken[0]=='"' ) pNew->flags |= EP_DblQuoted;
96576 sqlite3Dequote(pNew->u.zToken);
96577 }
96578 }
96579 }
96580#if SQLITE_MAX_EXPR_DEPTH>0
96581 pNew->nHeight = 1;
96582#endif
96583 }
96584 return pNew;
96585}
96586
96587/*
96588** Allocate a new expression node from a zero-terminated token that has
96589** already been dequoted.
96590*/
96591SQLITE_PRIVATE Expr *sqlite3Expr(
96592 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
96593 int op, /* Expression opcode */
96594 const char *zToken /* Token argument. Might be NULL */
96595){
96596 Token x;
96597 x.z = zToken;
96598 x.n = sqlite3Strlen30(zToken);
96599 return sqlite3ExprAlloc(db, op, &x, 0);
96600}
96601
96602/*
96603** Attach subtrees pLeft and pRight to the Expr node pRoot.
96604**
96605** If pRoot==NULL that means that a memory allocation error has occurred.
96606** In that case, delete the subtrees pLeft and pRight.
96607*/
96608SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
96609 sqlite3 *db,
96610 Expr *pRoot,
96611 Expr *pLeft,
96612 Expr *pRight
96613){
96614 if( pRoot==0 ){
96615 assert( db->mallocFailed );
96616 sqlite3ExprDelete(db, pLeft);
96617 sqlite3ExprDelete(db, pRight);
96618 }else{
96619 if( pRight ){
96620 pRoot->pRight = pRight;
96621 pRoot->flags |= EP_Propagate & pRight->flags;
96622 }
96623 if( pLeft ){
96624 pRoot->pLeft = pLeft;
96625 pRoot->flags |= EP_Propagate & pLeft->flags;
96626 }
96627 exprSetHeight(pRoot);
96628 }
96629}
96630
96631/*
96632** Allocate an Expr node which joins as many as two subtrees.
96633**
96634** One or both of the subtrees can be NULL. Return a pointer to the new
96635** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
96636** free the subtrees and return NULL.
96637*/
96638SQLITE_PRIVATE Expr *sqlite3PExpr(
96639 Parse *pParse, /* Parsing context */
96640 int op, /* Expression opcode */
96641 Expr *pLeft, /* Left operand */
96642 Expr *pRight /* Right operand */
96643){
96644 Expr *p;
96645 if( op==TK_AND && pParse->nErr==0 ){
96646 /* Take advantage of short-circuit false optimization for AND */
96647 p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
96648 }else{
96649 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
96650 if( p ){
96651 memset(p, 0, sizeof(Expr));
96652 p->op = op & TKFLG_MASK;
96653 p->iAgg = -1;
96654 }
96655 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
96656 }
96657 if( p ) {
96658 sqlite3ExprCheckHeight(pParse, p->nHeight);
96659 }
96660 return p;
96661}
96662
96663/*
96664** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due
96665** do a memory allocation failure) then delete the pSelect object.
96666*/
96667SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
96668 if( pExpr ){
96669 pExpr->x.pSelect = pSelect;
96670 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery);
96671 sqlite3ExprSetHeightAndFlags(pParse, pExpr);
96672 }else{
96673 assert( pParse->db->mallocFailed );
96674 sqlite3SelectDelete(pParse->db, pSelect);
96675 }
96676}
96677
96678
96679/*
96680** If the expression is always either TRUE or FALSE (respectively),
96681** then return 1. If one cannot determine the truth value of the
96682** expression at compile-time return 0.
96683**
96684** This is an optimization. If is OK to return 0 here even if
96685** the expression really is always false or false (a false negative).
96686** But it is a bug to return 1 if the expression might have different
96687** boolean values in different circumstances (a false positive.)
96688**
96689** Note that if the expression is part of conditional for a
96690** LEFT JOIN, then we cannot determine at compile-time whether or not
96691** is it true or false, so always return 0.
96692*/
96693static int exprAlwaysTrue(Expr *p){
96694 int v = 0;
96695 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
96696 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
96697 return v!=0;
96698}
96699static int exprAlwaysFalse(Expr *p){
96700 int v = 0;
96701 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
96702 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
96703 return v==0;
96704}
96705
96706/*
96707** Join two expressions using an AND operator. If either expression is
96708** NULL, then just return the other expression.
96709**
96710** If one side or the other of the AND is known to be false, then instead
96711** of returning an AND expression, just return a constant expression with
96712** a value of false.
96713*/
96714SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
96715 if( pLeft==0 ){
96716 return pRight;
96717 }else if( pRight==0 ){
96718 return pLeft;
96719 }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
96720 sqlite3ExprDelete(db, pLeft);
96721 sqlite3ExprDelete(db, pRight);
96722 return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
96723 }else{
96724 Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
96725 sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
96726 return pNew;
96727 }
96728}
96729
96730/*
96731** Construct a new expression node for a function with multiple
96732** arguments.
96733*/
96734SQLITE_PRIVATE Expr *sqlite3ExprFunction(
96735 Parse *pParse, /* Parsing context */
96736 ExprList *pList, /* Argument list */
96737 Token *pToken, /* Name of the function */
96738 int eDistinct /* SF_Distinct or SF_ALL or 0 */
96739){
96740 Expr *pNew;
96741 sqlite3 *db = pParse->db;
96742 assert( pToken );
96743 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
96744 if( pNew==0 ){
96745 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
96746 return 0;
96747 }
96748 if( pList && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
96749 sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken);
96750 }
96751 pNew->x.pList = pList;
96752 ExprSetProperty(pNew, EP_HasFunc);
96753 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
96754 sqlite3ExprSetHeightAndFlags(pParse, pNew);
96755 if( eDistinct==SF_Distinct ) ExprSetProperty(pNew, EP_Distinct);
96756 return pNew;
96757}
96758
96759/*
96760** Assign a variable number to an expression that encodes a wildcard
96761** in the original SQL statement.
96762**
96763** Wildcards consisting of a single "?" are assigned the next sequential
96764** variable number.
96765**
96766** Wildcards of the form "?nnn" are assigned the number "nnn". We make
96767** sure "nnn" is not too big to avoid a denial of service attack when
96768** the SQL statement comes from an external source.
96769**
96770** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
96771** as the previous instance of the same wildcard. Or if this is the first
96772** instance of the wildcard, the next sequential variable number is
96773** assigned.
96774*/
96775SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){
96776 sqlite3 *db = pParse->db;
96777 const char *z;
96778 ynVar x;
96779
96780 if( pExpr==0 ) return;
96781 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
96782 z = pExpr->u.zToken;
96783 assert( z!=0 );
96784 assert( z[0]!=0 );
96785 assert( n==(u32)sqlite3Strlen30(z) );
96786 if( z[1]==0 ){
96787 /* Wildcard of the form "?". Assign the next variable number */
96788 assert( z[0]=='?' );
96789 x = (ynVar)(++pParse->nVar);
96790 }else{
96791 int doAdd = 0;
96792 if( z[0]=='?' ){
96793 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
96794 ** use it as the variable number */
96795 i64 i;
96796 int bOk;
96797 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
96798 i = z[1]-'0'; /* The common case of ?N for a single digit N */
96799 bOk = 1;
96800 }else{
96801 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
96802 }
96803 testcase( i==0 );
96804 testcase( i==1 );
96805 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
96806 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
96807 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
96808 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
96809 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
96810 return;
96811 }
96812 x = (ynVar)i;
96813 if( x>pParse->nVar ){
96814 pParse->nVar = (int)x;
96815 doAdd = 1;
96816 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
96817 doAdd = 1;
96818 }
96819 }else{
96820 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
96821 ** number as the prior appearance of the same name, or if the name
96822 ** has never appeared before, reuse the same variable number
96823 */
96824 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
96825 if( x==0 ){
96826 x = (ynVar)(++pParse->nVar);
96827 doAdd = 1;
96828 }
96829 }
96830 if( doAdd ){
96831 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
96832 }
96833 }
96834 pExpr->iColumn = x;
96835 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
96836 sqlite3ErrorMsg(pParse, "too many SQL variables");
96837 }
96838}
96839
96840/*
96841** Recursively delete an expression tree.
96842*/
96843static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
96844 assert( p!=0 );
96845 /* Sanity check: Assert that the IntValue is non-negative if it exists */
96846 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
96847#ifdef SQLITE_DEBUG
96848 if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
96849 assert( p->pLeft==0 );
96850 assert( p->pRight==0 );
96851 assert( p->x.pSelect==0 );
96852 }
96853#endif
96854 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
96855 /* The Expr.x union is never used at the same time as Expr.pRight */
96856 assert( p->x.pList==0 || p->pRight==0 );
96857 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
96858 if( p->pRight ){
96859 sqlite3ExprDeleteNN(db, p->pRight);
96860 }else if( ExprHasProperty(p, EP_xIsSelect) ){
96861 sqlite3SelectDelete(db, p->x.pSelect);
96862 }else{
96863 sqlite3ExprListDelete(db, p->x.pList);
96864 }
96865 if( !ExprHasProperty(p, EP_Reduced) ){
96866 sqlite3WindowDelete(db, p->pWin);
96867 }
96868 }
96869 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
96870 if( !ExprHasProperty(p, EP_Static) ){
96871 sqlite3DbFreeNN(db, p);
96872 }
96873}
96874SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
96875 if( p ) sqlite3ExprDeleteNN(db, p);
96876}
96877
96878/*
96879** Return the number of bytes allocated for the expression structure
96880** passed as the first argument. This is always one of EXPR_FULLSIZE,
96881** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
96882*/
96883static int exprStructSize(Expr *p){
96884 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
96885 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
96886 return EXPR_FULLSIZE;
96887}
96888
96889/*
96890** The dupedExpr*Size() routines each return the number of bytes required
96891** to store a copy of an expression or expression tree. They differ in
96892** how much of the tree is measured.
96893**
96894** dupedExprStructSize() Size of only the Expr structure
96895** dupedExprNodeSize() Size of Expr + space for token
96896** dupedExprSize() Expr + token + subtree components
96897**
96898***************************************************************************
96899**
96900** The dupedExprStructSize() function returns two values OR-ed together:
96901** (1) the space required for a copy of the Expr structure only and
96902** (2) the EP_xxx flags that indicate what the structure size should be.
96903** The return values is always one of:
96904**
96905** EXPR_FULLSIZE
96906** EXPR_REDUCEDSIZE | EP_Reduced
96907** EXPR_TOKENONLYSIZE | EP_TokenOnly
96908**
96909** The size of the structure can be found by masking the return value
96910** of this routine with 0xfff. The flags can be found by masking the
96911** return value with EP_Reduced|EP_TokenOnly.
96912**
96913** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
96914** (unreduced) Expr objects as they or originally constructed by the parser.
96915** During expression analysis, extra information is computed and moved into
96916** later parts of the Expr object and that extra information might get chopped
96917** off if the expression is reduced. Note also that it does not work to
96918** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
96919** to reduce a pristine expression tree from the parser. The implementation
96920** of dupedExprStructSize() contain multiple assert() statements that attempt
96921** to enforce this constraint.
96922*/
96923static int dupedExprStructSize(Expr *p, int flags){
96924 int nSize;
96925 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
96926 assert( EXPR_FULLSIZE<=0xfff );
96927 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
96928 if( 0==flags || p->op==TK_SELECT_COLUMN
96929#ifndef SQLITE_OMIT_WINDOWFUNC
96930 || p->pWin
96931#endif
96932 ){
96933 nSize = EXPR_FULLSIZE;
96934 }else{
96935 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
96936 assert( !ExprHasProperty(p, EP_FromJoin) );
96937 assert( !ExprHasProperty(p, EP_MemToken) );
96938 assert( !ExprHasProperty(p, EP_NoReduce) );
96939 if( p->pLeft || p->x.pList ){
96940 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
96941 }else{
96942 assert( p->pRight==0 );
96943 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
96944 }
96945 }
96946 return nSize;
96947}
96948
96949/*
96950** This function returns the space in bytes required to store the copy
96951** of the Expr structure and a copy of the Expr.u.zToken string (if that
96952** string is defined.)
96953*/
96954static int dupedExprNodeSize(Expr *p, int flags){
96955 int nByte = dupedExprStructSize(p, flags) & 0xfff;
96956 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
96957 nByte += sqlite3Strlen30(p->u.zToken)+1;
96958 }
96959 return ROUND8(nByte);
96960}
96961
96962/*
96963** Return the number of bytes required to create a duplicate of the
96964** expression passed as the first argument. The second argument is a
96965** mask containing EXPRDUP_XXX flags.
96966**
96967** The value returned includes space to create a copy of the Expr struct
96968** itself and the buffer referred to by Expr.u.zToken, if any.
96969**
96970** If the EXPRDUP_REDUCE flag is set, then the return value includes
96971** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
96972** and Expr.pRight variables (but not for any structures pointed to or
96973** descended from the Expr.x.pList or Expr.x.pSelect variables).
96974*/
96975static int dupedExprSize(Expr *p, int flags){
96976 int nByte = 0;
96977 if( p ){
96978 nByte = dupedExprNodeSize(p, flags);
96979 if( flags&EXPRDUP_REDUCE ){
96980 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
96981 }
96982 }
96983 return nByte;
96984}
96985
96986/*
96987** This function is similar to sqlite3ExprDup(), except that if pzBuffer
96988** is not NULL then *pzBuffer is assumed to point to a buffer large enough
96989** to store the copy of expression p, the copies of p->u.zToken
96990** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
96991** if any. Before returning, *pzBuffer is set to the first byte past the
96992** portion of the buffer copied into by this function.
96993*/
96994static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){
96995 Expr *pNew; /* Value to return */
96996 u8 *zAlloc; /* Memory space from which to build Expr object */
96997 u32 staticFlag; /* EP_Static if space not obtained from malloc */
96998
96999 assert( db!=0 );
97000 assert( p );
97001 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE );
97002 assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE );
97003
97004 /* Figure out where to write the new Expr structure. */
97005 if( pzBuffer ){
97006 zAlloc = *pzBuffer;
97007 staticFlag = EP_Static;
97008 }else{
97009 zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags));
97010 staticFlag = 0;
97011 }
97012 pNew = (Expr *)zAlloc;
97013
97014 if( pNew ){
97015 /* Set nNewSize to the size allocated for the structure pointed to
97016 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
97017 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
97018 ** by the copy of the p->u.zToken string (if any).
97019 */
97020 const unsigned nStructSize = dupedExprStructSize(p, dupFlags);
97021 const int nNewSize = nStructSize & 0xfff;
97022 int nToken;
97023 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
97024 nToken = sqlite3Strlen30(p->u.zToken) + 1;
97025 }else{
97026 nToken = 0;
97027 }
97028 if( dupFlags ){
97029 assert( ExprHasProperty(p, EP_Reduced)==0 );
97030 memcpy(zAlloc, p, nNewSize);
97031 }else{
97032 u32 nSize = (u32)exprStructSize(p);
97033 memcpy(zAlloc, p, nSize);
97034 if( nSize<EXPR_FULLSIZE ){
97035 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
97036 }
97037 }
97038
97039 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
97040 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
97041 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
97042 pNew->flags |= staticFlag;
97043
97044 /* Copy the p->u.zToken string, if any. */
97045 if( nToken ){
97046 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
97047 memcpy(zToken, p->u.zToken, nToken);
97048 }
97049
97050 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
97051 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
97052 if( ExprHasProperty(p, EP_xIsSelect) ){
97053 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
97054 }else{
97055 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
97056 }
97057 }
97058
97059 /* Fill in pNew->pLeft and pNew->pRight. */
97060 zAlloc += dupedExprNodeSize(p, dupFlags);
97061 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
97062 if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
97063 pNew->pLeft = p->pLeft ?
97064 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
97065 pNew->pRight = p->pRight ?
97066 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
97067 }
97068 }else{
97069#ifndef SQLITE_OMIT_WINDOWFUNC
97070 if( ExprHasProperty(p, EP_Reduced|EP_TokenOnly) ){
97071 pNew->pWin = 0;
97072 }else{
97073 pNew->pWin = sqlite3WindowDup(db, pNew, p->pWin);
97074 }
97075#endif /* SQLITE_OMIT_WINDOWFUNC */
97076 if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
97077 if( pNew->op==TK_SELECT_COLUMN ){
97078 pNew->pLeft = p->pLeft;
97079 assert( p->iColumn==0 || p->pRight==0 );
97080 assert( p->pRight==0 || p->pRight==p->pLeft );
97081 }else{
97082 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
97083 }
97084 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
97085 }
97086 }
97087 if( pzBuffer ){
97088 *pzBuffer = zAlloc;
97089 }
97090 }
97091 return pNew;
97092}
97093
97094/*
97095** Create and return a deep copy of the object passed as the second
97096** argument. If an OOM condition is encountered, NULL is returned
97097** and the db->mallocFailed flag set.
97098*/
97099#ifndef SQLITE_OMIT_CTE
97100static With *withDup(sqlite3 *db, With *p){
97101 With *pRet = 0;
97102 if( p ){
97103 int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
97104 pRet = sqlite3DbMallocZero(db, nByte);
97105 if( pRet ){
97106 int i;
97107 pRet->nCte = p->nCte;
97108 for(i=0; i<p->nCte; i++){
97109 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
97110 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
97111 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
97112 }
97113 }
97114 }
97115 return pRet;
97116}
97117#else
97118# define withDup(x,y) 0
97119#endif
97120
97121/*
97122** The following group of routines make deep copies of expressions,
97123** expression lists, ID lists, and select statements. The copies can
97124** be deleted (by being passed to their respective ...Delete() routines)
97125** without effecting the originals.
97126**
97127** The expression list, ID, and source lists return by sqlite3ExprListDup(),
97128** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
97129** by subsequent calls to sqlite*ListAppend() routines.
97130**
97131** Any tables that the SrcList might point to are not duplicated.
97132**
97133** The flags parameter contains a combination of the EXPRDUP_XXX flags.
97134** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
97135** truncated version of the usual Expr structure that will be stored as
97136** part of the in-memory representation of the database schema.
97137*/
97138SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
97139 assert( flags==0 || flags==EXPRDUP_REDUCE );
97140 return p ? exprDup(db, p, flags, 0) : 0;
97141}
97142SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
97143 ExprList *pNew;
97144 struct ExprList_item *pItem, *pOldItem;
97145 int i;
97146 Expr *pPriorSelectCol = 0;
97147 assert( db!=0 );
97148 if( p==0 ) return 0;
97149 pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p));
97150 if( pNew==0 ) return 0;
97151 pNew->nExpr = p->nExpr;
97152 pItem = pNew->a;
97153 pOldItem = p->a;
97154 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
97155 Expr *pOldExpr = pOldItem->pExpr;
97156 Expr *pNewExpr;
97157 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
97158 if( pOldExpr
97159 && pOldExpr->op==TK_SELECT_COLUMN
97160 && (pNewExpr = pItem->pExpr)!=0
97161 ){
97162 assert( pNewExpr->iColumn==0 || i>0 );
97163 if( pNewExpr->iColumn==0 ){
97164 assert( pOldExpr->pLeft==pOldExpr->pRight );
97165 pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight;
97166 }else{
97167 assert( i>0 );
97168 assert( pItem[-1].pExpr!=0 );
97169 assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 );
97170 assert( pPriorSelectCol==pItem[-1].pExpr->pLeft );
97171 pNewExpr->pLeft = pPriorSelectCol;
97172 }
97173 }
97174 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
97175 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
97176 pItem->sortOrder = pOldItem->sortOrder;
97177 pItem->done = 0;
97178 pItem->bSpanIsTab = pOldItem->bSpanIsTab;
97179 pItem->bSorterRef = pOldItem->bSorterRef;
97180 pItem->u = pOldItem->u;
97181 }
97182 return pNew;
97183}
97184
97185/*
97186** If cursors, triggers, views and subqueries are all omitted from
97187** the build, then none of the following routines, except for
97188** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
97189** called with a NULL argument.
97190*/
97191#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
97192 || !defined(SQLITE_OMIT_SUBQUERY)
97193SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
97194 SrcList *pNew;
97195 int i;
97196 int nByte;
97197 assert( db!=0 );
97198 if( p==0 ) return 0;
97199 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
97200 pNew = sqlite3DbMallocRawNN(db, nByte );
97201 if( pNew==0 ) return 0;
97202 pNew->nSrc = pNew->nAlloc = p->nSrc;
97203 for(i=0; i<p->nSrc; i++){
97204 struct SrcList_item *pNewItem = &pNew->a[i];
97205 struct SrcList_item *pOldItem = &p->a[i];
97206 Table *pTab;
97207 pNewItem->pSchema = pOldItem->pSchema;
97208 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
97209 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
97210 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
97211 pNewItem->fg = pOldItem->fg;
97212 pNewItem->iCursor = pOldItem->iCursor;
97213 pNewItem->addrFillSub = pOldItem->addrFillSub;
97214 pNewItem->regReturn = pOldItem->regReturn;
97215 if( pNewItem->fg.isIndexedBy ){
97216 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
97217 }
97218 pNewItem->pIBIndex = pOldItem->pIBIndex;
97219 if( pNewItem->fg.isTabFunc ){
97220 pNewItem->u1.pFuncArg =
97221 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
97222 }
97223 pTab = pNewItem->pTab = pOldItem->pTab;
97224 if( pTab ){
97225 pTab->nTabRef++;
97226 }
97227 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
97228 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
97229 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
97230 pNewItem->colUsed = pOldItem->colUsed;
97231 }
97232 return pNew;
97233}
97234SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
97235 IdList *pNew;
97236 int i;
97237 assert( db!=0 );
97238 if( p==0 ) return 0;
97239 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
97240 if( pNew==0 ) return 0;
97241 pNew->nId = p->nId;
97242 pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
97243 if( pNew->a==0 ){
97244 sqlite3DbFreeNN(db, pNew);
97245 return 0;
97246 }
97247 /* Note that because the size of the allocation for p->a[] is not
97248 ** necessarily a power of two, sqlite3IdListAppend() may not be called
97249 ** on the duplicate created by this function. */
97250 for(i=0; i<p->nId; i++){
97251 struct IdList_item *pNewItem = &pNew->a[i];
97252 struct IdList_item *pOldItem = &p->a[i];
97253 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
97254 pNewItem->idx = pOldItem->idx;
97255 }
97256 return pNew;
97257}
97258SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
97259 Select *pRet = 0;
97260 Select *pNext = 0;
97261 Select **pp = &pRet;
97262 Select *p;
97263
97264 assert( db!=0 );
97265 for(p=pDup; p; p=p->pPrior){
97266 Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
97267 if( pNew==0 ) break;
97268 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
97269 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
97270 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
97271 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
97272 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
97273 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
97274 pNew->op = p->op;
97275 pNew->pNext = pNext;
97276 pNew->pPrior = 0;
97277 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
97278 pNew->iLimit = 0;
97279 pNew->iOffset = 0;
97280 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
97281 pNew->addrOpenEphm[0] = -1;
97282 pNew->addrOpenEphm[1] = -1;
97283 pNew->nSelectRow = p->nSelectRow;
97284 pNew->pWith = withDup(db, p->pWith);
97285#ifndef SQLITE_OMIT_WINDOWFUNC
97286 pNew->pWin = 0;
97287 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
97288#endif
97289 pNew->selId = p->selId;
97290 *pp = pNew;
97291 pp = &pNew->pPrior;
97292 pNext = pNew;
97293 }
97294
97295 return pRet;
97296}
97297#else
97298SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
97299 assert( p==0 );
97300 return 0;
97301}
97302#endif
97303
97304
97305/*
97306** Add a new element to the end of an expression list. If pList is
97307** initially NULL, then create a new expression list.
97308**
97309** The pList argument must be either NULL or a pointer to an ExprList
97310** obtained from a prior call to sqlite3ExprListAppend(). This routine
97311** may not be used with an ExprList obtained from sqlite3ExprListDup().
97312** Reason: This routine assumes that the number of slots in pList->a[]
97313** is a power of two. That is true for sqlite3ExprListAppend() returns
97314** but is not necessarily true from the return value of sqlite3ExprListDup().
97315**
97316** If a memory allocation error occurs, the entire list is freed and
97317** NULL is returned. If non-NULL is returned, then it is guaranteed
97318** that the new entry was successfully appended.
97319*/
97320SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
97321 Parse *pParse, /* Parsing context */
97322 ExprList *pList, /* List to which to append. Might be NULL */
97323 Expr *pExpr /* Expression to be appended. Might be NULL */
97324){
97325 struct ExprList_item *pItem;
97326 sqlite3 *db = pParse->db;
97327 assert( db!=0 );
97328 if( pList==0 ){
97329 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
97330 if( pList==0 ){
97331 goto no_mem;
97332 }
97333 pList->nExpr = 0;
97334 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
97335 ExprList *pNew;
97336 pNew = sqlite3DbRealloc(db, pList,
97337 sizeof(*pList)+(2*pList->nExpr - 1)*sizeof(pList->a[0]));
97338 if( pNew==0 ){
97339 goto no_mem;
97340 }
97341 pList = pNew;
97342 }
97343 pItem = &pList->a[pList->nExpr++];
97344 assert( offsetof(struct ExprList_item,zName)==sizeof(pItem->pExpr) );
97345 assert( offsetof(struct ExprList_item,pExpr)==0 );
97346 memset(&pItem->zName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zName));
97347 pItem->pExpr = pExpr;
97348 return pList;
97349
97350no_mem:
97351 /* Avoid leaking memory if malloc has failed. */
97352 sqlite3ExprDelete(db, pExpr);
97353 sqlite3ExprListDelete(db, pList);
97354 return 0;
97355}
97356
97357/*
97358** pColumns and pExpr form a vector assignment which is part of the SET
97359** clause of an UPDATE statement. Like this:
97360**
97361** (a,b,c) = (expr1,expr2,expr3)
97362** Or: (a,b,c) = (SELECT x,y,z FROM ....)
97363**
97364** For each term of the vector assignment, append new entries to the
97365** expression list pList. In the case of a subquery on the RHS, append
97366** TK_SELECT_COLUMN expressions.
97367*/
97368SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
97369 Parse *pParse, /* Parsing context */
97370 ExprList *pList, /* List to which to append. Might be NULL */
97371 IdList *pColumns, /* List of names of LHS of the assignment */
97372 Expr *pExpr /* Vector expression to be appended. Might be NULL */
97373){
97374 sqlite3 *db = pParse->db;
97375 int n;
97376 int i;
97377 int iFirst = pList ? pList->nExpr : 0;
97378 /* pColumns can only be NULL due to an OOM but an OOM will cause an
97379 ** exit prior to this routine being invoked */
97380 if( NEVER(pColumns==0) ) goto vector_append_error;
97381 if( pExpr==0 ) goto vector_append_error;
97382
97383 /* If the RHS is a vector, then we can immediately check to see that
97384 ** the size of the RHS and LHS match. But if the RHS is a SELECT,
97385 ** wildcards ("*") in the result set of the SELECT must be expanded before
97386 ** we can do the size check, so defer the size check until code generation.
97387 */
97388 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
97389 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
97390 pColumns->nId, n);
97391 goto vector_append_error;
97392 }
97393
97394 for(i=0; i<pColumns->nId; i++){
97395 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i);
97396 pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
97397 if( pList ){
97398 assert( pList->nExpr==iFirst+i+1 );
97399 pList->a[pList->nExpr-1].zName = pColumns->a[i].zName;
97400 pColumns->a[i].zName = 0;
97401 }
97402 }
97403
97404 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
97405 Expr *pFirst = pList->a[iFirst].pExpr;
97406 assert( pFirst!=0 );
97407 assert( pFirst->op==TK_SELECT_COLUMN );
97408
97409 /* Store the SELECT statement in pRight so it will be deleted when
97410 ** sqlite3ExprListDelete() is called */
97411 pFirst->pRight = pExpr;
97412 pExpr = 0;
97413
97414 /* Remember the size of the LHS in iTable so that we can check that
97415 ** the RHS and LHS sizes match during code generation. */
97416 pFirst->iTable = pColumns->nId;
97417 }
97418
97419vector_append_error:
97420 sqlite3ExprDelete(db, pExpr);
97421 sqlite3IdListDelete(db, pColumns);
97422 return pList;
97423}
97424
97425/*
97426** Set the sort order for the last element on the given ExprList.
97427*/
97428SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){
97429 if( p==0 ) return;
97430 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 );
97431 assert( p->nExpr>0 );
97432 if( iSortOrder<0 ){
97433 assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC );
97434 return;
97435 }
97436 p->a[p->nExpr-1].sortOrder = (u8)iSortOrder;
97437}
97438
97439/*
97440** Set the ExprList.a[].zName element of the most recently added item
97441** on the expression list.
97442**
97443** pList might be NULL following an OOM error. But pName should never be
97444** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
97445** is set.
97446*/
97447SQLITE_PRIVATE void sqlite3ExprListSetName(
97448 Parse *pParse, /* Parsing context */
97449 ExprList *pList, /* List to which to add the span. */
97450 Token *pName, /* Name to be added */
97451 int dequote /* True to cause the name to be dequoted */
97452){
97453 assert( pList!=0 || pParse->db->mallocFailed!=0 );
97454 if( pList ){
97455 struct ExprList_item *pItem;
97456 assert( pList->nExpr>0 );
97457 pItem = &pList->a[pList->nExpr-1];
97458 assert( pItem->zName==0 );
97459 pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
97460 if( dequote ) sqlite3Dequote(pItem->zName);
97461 if( IN_RENAME_OBJECT ){
97462 sqlite3RenameTokenMap(pParse, (void*)pItem->zName, pName);
97463 }
97464 }
97465}
97466
97467/*
97468** Set the ExprList.a[].zSpan element of the most recently added item
97469** on the expression list.
97470**
97471** pList might be NULL following an OOM error. But pSpan should never be
97472** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
97473** is set.
97474*/
97475SQLITE_PRIVATE void sqlite3ExprListSetSpan(
97476 Parse *pParse, /* Parsing context */
97477 ExprList *pList, /* List to which to add the span. */
97478 const char *zStart, /* Start of the span */
97479 const char *zEnd /* End of the span */
97480){
97481 sqlite3 *db = pParse->db;
97482 assert( pList!=0 || db->mallocFailed!=0 );
97483 if( pList ){
97484 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
97485 assert( pList->nExpr>0 );
97486 sqlite3DbFree(db, pItem->zSpan);
97487 pItem->zSpan = sqlite3DbSpanDup(db, zStart, zEnd);
97488 }
97489}
97490
97491/*
97492** If the expression list pEList contains more than iLimit elements,
97493** leave an error message in pParse.
97494*/
97495SQLITE_PRIVATE void sqlite3ExprListCheckLength(
97496 Parse *pParse,
97497 ExprList *pEList,
97498 const char *zObject
97499){
97500 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
97501 testcase( pEList && pEList->nExpr==mx );
97502 testcase( pEList && pEList->nExpr==mx+1 );
97503 if( pEList && pEList->nExpr>mx ){
97504 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
97505 }
97506}
97507
97508/*
97509** Delete an entire expression list.
97510*/
97511static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
97512 int i = pList->nExpr;
97513 struct ExprList_item *pItem = pList->a;
97514 assert( pList->nExpr>0 );
97515 do{
97516 sqlite3ExprDelete(db, pItem->pExpr);
97517 sqlite3DbFree(db, pItem->zName);
97518 sqlite3DbFree(db, pItem->zSpan);
97519 pItem++;
97520 }while( --i>0 );
97521 sqlite3DbFreeNN(db, pList);
97522}
97523SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
97524 if( pList ) exprListDeleteNN(db, pList);
97525}
97526
97527/*
97528** Return the bitwise-OR of all Expr.flags fields in the given
97529** ExprList.
97530*/
97531SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){
97532 int i;
97533 u32 m = 0;
97534 assert( pList!=0 );
97535 for(i=0; i<pList->nExpr; i++){
97536 Expr *pExpr = pList->a[i].pExpr;
97537 assert( pExpr!=0 );
97538 m |= pExpr->flags;
97539 }
97540 return m;
97541}
97542
97543/*
97544** This is a SELECT-node callback for the expression walker that
97545** always "fails". By "fail" in this case, we mean set
97546** pWalker->eCode to zero and abort.
97547**
97548** This callback is used by multiple expression walkers.
97549*/
97550SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
97551 UNUSED_PARAMETER(NotUsed);
97552 pWalker->eCode = 0;
97553 return WRC_Abort;
97554}
97555
97556/*
97557** If the input expression is an ID with the name "true" or "false"
97558** then convert it into an TK_TRUEFALSE term. Return non-zero if
97559** the conversion happened, and zero if the expression is unaltered.
97560*/
97561SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr *pExpr){
97562 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
97563 if( sqlite3StrICmp(pExpr->u.zToken, "true")==0
97564 || sqlite3StrICmp(pExpr->u.zToken, "false")==0
97565 ){
97566 pExpr->op = TK_TRUEFALSE;
97567 return 1;
97568 }
97569 return 0;
97570}
97571
97572/*
97573** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE
97574** and 0 if it is FALSE.
97575*/
97576SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr *pExpr){
97577 assert( pExpr->op==TK_TRUEFALSE );
97578 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
97579 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
97580 return pExpr->u.zToken[4]==0;
97581}
97582
97583
97584/*
97585** These routines are Walker callbacks used to check expressions to
97586** see if they are "constant" for some definition of constant. The
97587** Walker.eCode value determines the type of "constant" we are looking
97588** for.
97589**
97590** These callback routines are used to implement the following:
97591**
97592** sqlite3ExprIsConstant() pWalker->eCode==1
97593** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
97594** sqlite3ExprIsTableConstant() pWalker->eCode==3
97595** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
97596**
97597** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
97598** is found to not be a constant.
97599**
97600** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
97601** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
97602** an existing schema and 4 when processing a new statement. A bound
97603** parameter raises an error for new statements, but is silently converted
97604** to NULL for existing schemas. This allows sqlite_master tables that
97605** contain a bound parameter because they were generated by older versions
97606** of SQLite to be parsed by newer versions of SQLite without raising a
97607** malformed schema error.
97608*/
97609static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
97610
97611 /* If pWalker->eCode is 2 then any term of the expression that comes from
97612 ** the ON or USING clauses of a left join disqualifies the expression
97613 ** from being considered constant. */
97614 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
97615 pWalker->eCode = 0;
97616 return WRC_Abort;
97617 }
97618
97619 switch( pExpr->op ){
97620 /* Consider functions to be constant if all their arguments are constant
97621 ** and either pWalker->eCode==4 or 5 or the function has the
97622 ** SQLITE_FUNC_CONST flag. */
97623 case TK_FUNCTION:
97624 if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
97625 return WRC_Continue;
97626 }else{
97627 pWalker->eCode = 0;
97628 return WRC_Abort;
97629 }
97630 case TK_ID:
97631 /* Convert "true" or "false" in a DEFAULT clause into the
97632 ** appropriate TK_TRUEFALSE operator */
97633 if( sqlite3ExprIdToTrueFalse(pExpr) ){
97634 return WRC_Prune;
97635 }
97636 /* Fall thru */
97637 case TK_COLUMN:
97638 case TK_AGG_FUNCTION:
97639 case TK_AGG_COLUMN:
97640 testcase( pExpr->op==TK_ID );
97641 testcase( pExpr->op==TK_COLUMN );
97642 testcase( pExpr->op==TK_AGG_FUNCTION );
97643 testcase( pExpr->op==TK_AGG_COLUMN );
97644 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
97645 return WRC_Continue;
97646 }
97647 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
97648 return WRC_Continue;
97649 }
97650 /* Fall through */
97651 case TK_IF_NULL_ROW:
97652 case TK_REGISTER:
97653 testcase( pExpr->op==TK_REGISTER );
97654 testcase( pExpr->op==TK_IF_NULL_ROW );
97655 pWalker->eCode = 0;
97656 return WRC_Abort;
97657 case TK_VARIABLE:
97658 if( pWalker->eCode==5 ){
97659 /* Silently convert bound parameters that appear inside of CREATE
97660 ** statements into a NULL when parsing the CREATE statement text out
97661 ** of the sqlite_master table */
97662 pExpr->op = TK_NULL;
97663 }else if( pWalker->eCode==4 ){
97664 /* A bound parameter in a CREATE statement that originates from
97665 ** sqlite3_prepare() causes an error */
97666 pWalker->eCode = 0;
97667 return WRC_Abort;
97668 }
97669 /* Fall through */
97670 default:
97671 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
97672 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
97673 return WRC_Continue;
97674 }
97675}
97676static int exprIsConst(Expr *p, int initFlag, int iCur){
97677 Walker w;
97678 w.eCode = initFlag;
97679 w.xExprCallback = exprNodeIsConstant;
97680 w.xSelectCallback = sqlite3SelectWalkFail;
97681#ifdef SQLITE_DEBUG
97682 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
97683#endif
97684 w.u.iCur = iCur;
97685 sqlite3WalkExpr(&w, p);
97686 return w.eCode;
97687}
97688
97689/*
97690** Walk an expression tree. Return non-zero if the expression is constant
97691** and 0 if it involves variables or function calls.
97692**
97693** For the purposes of this function, a double-quoted string (ex: "abc")
97694** is considered a variable but a single-quoted string (ex: 'abc') is
97695** a constant.
97696*/
97697SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
97698 return exprIsConst(p, 1, 0);
97699}
97700
97701/*
97702** Walk an expression tree. Return non-zero if
97703**
97704** (1) the expression is constant, and
97705** (2) the expression does originate in the ON or USING clause
97706** of a LEFT JOIN, and
97707** (3) the expression does not contain any EP_FixedCol TK_COLUMN
97708** operands created by the constant propagation optimization.
97709**
97710** When this routine returns true, it indicates that the expression
97711** can be added to the pParse->pConstExpr list and evaluated once when
97712** the prepared statement starts up. See sqlite3ExprCodeAtInit().
97713*/
97714SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
97715 return exprIsConst(p, 2, 0);
97716}
97717
97718/*
97719** Walk an expression tree. Return non-zero if the expression is constant
97720** for any single row of the table with cursor iCur. In other words, the
97721** expression must not refer to any non-deterministic function nor any
97722** table other than iCur.
97723*/
97724SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
97725 return exprIsConst(p, 3, iCur);
97726}
97727
97728
97729/*
97730** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy().
97731*/
97732static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){
97733 ExprList *pGroupBy = pWalker->u.pGroupBy;
97734 int i;
97735
97736 /* Check if pExpr is identical to any GROUP BY term. If so, consider
97737 ** it constant. */
97738 for(i=0; i<pGroupBy->nExpr; i++){
97739 Expr *p = pGroupBy->a[i].pExpr;
97740 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
97741 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
97742 if( sqlite3IsBinary(pColl) ){
97743 return WRC_Prune;
97744 }
97745 }
97746 }
97747
97748 /* Check if pExpr is a sub-select. If so, consider it variable. */
97749 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
97750 pWalker->eCode = 0;
97751 return WRC_Abort;
97752 }
97753
97754 return exprNodeIsConstant(pWalker, pExpr);
97755}
97756
97757/*
97758** Walk the expression tree passed as the first argument. Return non-zero
97759** if the expression consists entirely of constants or copies of terms
97760** in pGroupBy that sort with the BINARY collation sequence.
97761**
97762** This routine is used to determine if a term of the HAVING clause can
97763** be promoted into the WHERE clause. In order for such a promotion to work,
97764** the value of the HAVING clause term must be the same for all members of
97765** a "group". The requirement that the GROUP BY term must be BINARY
97766** assumes that no other collating sequence will have a finer-grained
97767** grouping than binary. In other words (A=B COLLATE binary) implies
97768** A=B in every other collating sequence. The requirement that the
97769** GROUP BY be BINARY is stricter than necessary. It would also work
97770** to promote HAVING clauses that use the same alternative collating
97771** sequence as the GROUP BY term, but that is much harder to check,
97772** alternative collating sequences are uncommon, and this is only an
97773** optimization, so we take the easy way out and simply require the
97774** GROUP BY to use the BINARY collating sequence.
97775*/
97776SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){
97777 Walker w;
97778 w.eCode = 1;
97779 w.xExprCallback = exprNodeIsConstantOrGroupBy;
97780 w.xSelectCallback = 0;
97781 w.u.pGroupBy = pGroupBy;
97782 w.pParse = pParse;
97783 sqlite3WalkExpr(&w, p);
97784 return w.eCode;
97785}
97786
97787/*
97788** Walk an expression tree. Return non-zero if the expression is constant
97789** or a function call with constant arguments. Return and 0 if there
97790** are any variables.
97791**
97792** For the purposes of this function, a double-quoted string (ex: "abc")
97793** is considered a variable but a single-quoted string (ex: 'abc') is
97794** a constant.
97795*/
97796SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
97797 assert( isInit==0 || isInit==1 );
97798 return exprIsConst(p, 4+isInit, 0);
97799}
97800
97801#ifdef SQLITE_ENABLE_CURSOR_HINTS
97802/*
97803** Walk an expression tree. Return 1 if the expression contains a
97804** subquery of some kind. Return 0 if there are no subqueries.
97805*/
97806SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr *p){
97807 Walker w;
97808 w.eCode = 1;
97809 w.xExprCallback = sqlite3ExprWalkNoop;
97810 w.xSelectCallback = sqlite3SelectWalkFail;
97811#ifdef SQLITE_DEBUG
97812 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
97813#endif
97814 sqlite3WalkExpr(&w, p);
97815 return w.eCode==0;
97816}
97817#endif
97818
97819/*
97820** If the expression p codes a constant integer that is small enough
97821** to fit in a 32-bit integer, return 1 and put the value of the integer
97822** in *pValue. If the expression is not an integer or if it is too big
97823** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
97824*/
97825SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
97826 int rc = 0;
97827 if( p==0 ) return 0; /* Can only happen following on OOM */
97828
97829 /* If an expression is an integer literal that fits in a signed 32-bit
97830 ** integer, then the EP_IntValue flag will have already been set */
97831 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
97832 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
97833
97834 if( p->flags & EP_IntValue ){
97835 *pValue = p->u.iValue;
97836 return 1;
97837 }
97838 switch( p->op ){
97839 case TK_UPLUS: {
97840 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
97841 break;
97842 }
97843 case TK_UMINUS: {
97844 int v;
97845 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
97846 assert( v!=(-2147483647-1) );
97847 *pValue = -v;
97848 rc = 1;
97849 }
97850 break;
97851 }
97852 default: break;
97853 }
97854 return rc;
97855}
97856
97857/*
97858** Return FALSE if there is no chance that the expression can be NULL.
97859**
97860** If the expression might be NULL or if the expression is too complex
97861** to tell return TRUE.
97862**
97863** This routine is used as an optimization, to skip OP_IsNull opcodes
97864** when we know that a value cannot be NULL. Hence, a false positive
97865** (returning TRUE when in fact the expression can never be NULL) might
97866** be a small performance hit but is otherwise harmless. On the other
97867** hand, a false negative (returning FALSE when the result could be NULL)
97868** will likely result in an incorrect answer. So when in doubt, return
97869** TRUE.
97870*/
97871SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
97872 u8 op;
97873 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
97874 op = p->op;
97875 if( op==TK_REGISTER ) op = p->op2;
97876 switch( op ){
97877 case TK_INTEGER:
97878 case TK_STRING:
97879 case TK_FLOAT:
97880 case TK_BLOB:
97881 return 0;
97882 case TK_COLUMN:
97883 return ExprHasProperty(p, EP_CanBeNull) ||
97884 p->pTab==0 || /* Reference to column of index on expression */
97885 (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
97886 default:
97887 return 1;
97888 }
97889}
97890
97891/*
97892** Return TRUE if the given expression is a constant which would be
97893** unchanged by OP_Affinity with the affinity given in the second
97894** argument.
97895**
97896** This routine is used to determine if the OP_Affinity operation
97897** can be omitted. When in doubt return FALSE. A false negative
97898** is harmless. A false positive, however, can result in the wrong
97899** answer.
97900*/
97901SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
97902 u8 op;
97903 if( aff==SQLITE_AFF_BLOB ) return 1;
97904 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
97905 op = p->op;
97906 if( op==TK_REGISTER ) op = p->op2;
97907 switch( op ){
97908 case TK_INTEGER: {
97909 return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
97910 }
97911 case TK_FLOAT: {
97912 return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
97913 }
97914 case TK_STRING: {
97915 return aff==SQLITE_AFF_TEXT;
97916 }
97917 case TK_BLOB: {
97918 return 1;
97919 }
97920 case TK_COLUMN: {
97921 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
97922 return p->iColumn<0
97923 && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
97924 }
97925 default: {
97926 return 0;
97927 }
97928 }
97929}
97930
97931/*
97932** Return TRUE if the given string is a row-id column name.
97933*/
97934SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
97935 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
97936 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
97937 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
97938 return 0;
97939}
97940
97941/*
97942** pX is the RHS of an IN operator. If pX is a SELECT statement
97943** that can be simplified to a direct table access, then return
97944** a pointer to the SELECT statement. If pX is not a SELECT statement,
97945** or if the SELECT statement needs to be manifested into a transient
97946** table, then return NULL.
97947*/
97948#ifndef SQLITE_OMIT_SUBQUERY
97949static Select *isCandidateForInOpt(Expr *pX){
97950 Select *p;
97951 SrcList *pSrc;
97952 ExprList *pEList;
97953 Table *pTab;
97954 int i;
97955 if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */
97956 if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */
97957 p = pX->x.pSelect;
97958 if( p->pPrior ) return 0; /* Not a compound SELECT */
97959 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
97960 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
97961 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
97962 return 0; /* No DISTINCT keyword and no aggregate functions */
97963 }
97964 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
97965 if( p->pLimit ) return 0; /* Has no LIMIT clause */
97966 if( p->pWhere ) return 0; /* Has no WHERE clause */
97967 pSrc = p->pSrc;
97968 assert( pSrc!=0 );
97969 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
97970 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
97971 pTab = pSrc->a[0].pTab;
97972 assert( pTab!=0 );
97973 assert( pTab->pSelect==0 ); /* FROM clause is not a view */
97974 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
97975 pEList = p->pEList;
97976 assert( pEList!=0 );
97977 /* All SELECT results must be columns. */
97978 for(i=0; i<pEList->nExpr; i++){
97979 Expr *pRes = pEList->a[i].pExpr;
97980 if( pRes->op!=TK_COLUMN ) return 0;
97981 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
97982 }
97983 return p;
97984}
97985#endif /* SQLITE_OMIT_SUBQUERY */
97986
97987#ifndef SQLITE_OMIT_SUBQUERY
97988/*
97989** Generate code that checks the left-most column of index table iCur to see if
97990** it contains any NULL entries. Cause the register at regHasNull to be set
97991** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
97992** to be set to NULL if iCur contains one or more NULL values.
97993*/
97994static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
97995 int addr1;
97996 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
97997 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
97998 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
97999 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
98000 VdbeComment((v, "first_entry_in(%d)", iCur));
98001 sqlite3VdbeJumpHere(v, addr1);
98002}
98003#endif
98004
98005
98006#ifndef SQLITE_OMIT_SUBQUERY
98007/*
98008** The argument is an IN operator with a list (not a subquery) on the
98009** right-hand side. Return TRUE if that list is constant.
98010*/
98011static int sqlite3InRhsIsConstant(Expr *pIn){
98012 Expr *pLHS;
98013 int res;
98014 assert( !ExprHasProperty(pIn, EP_xIsSelect) );
98015 pLHS = pIn->pLeft;
98016 pIn->pLeft = 0;
98017 res = sqlite3ExprIsConstant(pIn);
98018 pIn->pLeft = pLHS;
98019 return res;
98020}
98021#endif
98022
98023/*
98024** This function is used by the implementation of the IN (...) operator.
98025** The pX parameter is the expression on the RHS of the IN operator, which
98026** might be either a list of expressions or a subquery.
98027**
98028** The job of this routine is to find or create a b-tree object that can
98029** be used either to test for membership in the RHS set or to iterate through
98030** all members of the RHS set, skipping duplicates.
98031**
98032** A cursor is opened on the b-tree object that is the RHS of the IN operator
98033** and pX->iTable is set to the index of that cursor.
98034**
98035** The returned value of this function indicates the b-tree type, as follows:
98036**
98037** IN_INDEX_ROWID - The cursor was opened on a database table.
98038** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
98039** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
98040** IN_INDEX_EPH - The cursor was opened on a specially created and
98041** populated epheremal table.
98042** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
98043** implemented as a sequence of comparisons.
98044**
98045** An existing b-tree might be used if the RHS expression pX is a simple
98046** subquery such as:
98047**
98048** SELECT <column1>, <column2>... FROM <table>
98049**
98050** If the RHS of the IN operator is a list or a more complex subquery, then
98051** an ephemeral table might need to be generated from the RHS and then
98052** pX->iTable made to point to the ephemeral table instead of an
98053** existing table.
98054**
98055** The inFlags parameter must contain, at a minimum, one of the bits
98056** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains
98057** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
98058** membership test. When the IN_INDEX_LOOP bit is set, the IN index will
98059** be used to loop over all values of the RHS of the IN operator.
98060**
98061** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
98062** through the set members) then the b-tree must not contain duplicates.
98063** An epheremal table will be created unless the selected columns are guaranteed
98064** to be unique - either because it is an INTEGER PRIMARY KEY or due to
98065** a UNIQUE constraint or index.
98066**
98067** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
98068** for fast set membership tests) then an epheremal table must
98069** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
98070** index can be found with the specified <columns> as its left-most.
98071**
98072** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
98073** if the RHS of the IN operator is a list (not a subquery) then this
98074** routine might decide that creating an ephemeral b-tree for membership
98075** testing is too expensive and return IN_INDEX_NOOP. In that case, the
98076** calling routine should implement the IN operator using a sequence
98077** of Eq or Ne comparison operations.
98078**
98079** When the b-tree is being used for membership tests, the calling function
98080** might need to know whether or not the RHS side of the IN operator
98081** contains a NULL. If prRhsHasNull is not a NULL pointer and
98082** if there is any chance that the (...) might contain a NULL value at
98083** runtime, then a register is allocated and the register number written
98084** to *prRhsHasNull. If there is no chance that the (...) contains a
98085** NULL value, then *prRhsHasNull is left unchanged.
98086**
98087** If a register is allocated and its location stored in *prRhsHasNull, then
98088** the value in that register will be NULL if the b-tree contains one or more
98089** NULL values, and it will be some non-NULL value if the b-tree contains no
98090** NULL values.
98091**
98092** If the aiMap parameter is not NULL, it must point to an array containing
98093** one element for each column returned by the SELECT statement on the RHS
98094** of the IN(...) operator. The i'th entry of the array is populated with the
98095** offset of the index column that matches the i'th column returned by the
98096** SELECT. For example, if the expression and selected index are:
98097**
98098** (?,?,?) IN (SELECT a, b, c FROM t1)
98099** CREATE INDEX i1 ON t1(b, c, a);
98100**
98101** then aiMap[] is populated with {2, 0, 1}.
98102*/
98103#ifndef SQLITE_OMIT_SUBQUERY
98104SQLITE_PRIVATE int sqlite3FindInIndex(
98105 Parse *pParse, /* Parsing context */
98106 Expr *pX, /* The right-hand side (RHS) of the IN operator */
98107 u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
98108 int *prRhsHasNull, /* Register holding NULL status. See notes */
98109 int *aiMap /* Mapping from Index fields to RHS fields */
98110){
98111 Select *p; /* SELECT to the right of IN operator */
98112 int eType = 0; /* Type of RHS table. IN_INDEX_* */
98113 int iTab = pParse->nTab++; /* Cursor of the RHS table */
98114 int mustBeUnique; /* True if RHS must be unique */
98115 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
98116
98117 assert( pX->op==TK_IN );
98118 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
98119
98120 /* If the RHS of this IN(...) operator is a SELECT, and if it matters
98121 ** whether or not the SELECT result contains NULL values, check whether
98122 ** or not NULL is actually possible (it may not be, for example, due
98123 ** to NOT NULL constraints in the schema). If no NULL values are possible,
98124 ** set prRhsHasNull to 0 before continuing. */
98125 if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){
98126 int i;
98127 ExprList *pEList = pX->x.pSelect->pEList;
98128 for(i=0; i<pEList->nExpr; i++){
98129 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
98130 }
98131 if( i==pEList->nExpr ){
98132 prRhsHasNull = 0;
98133 }
98134 }
98135
98136 /* Check to see if an existing table or index can be used to
98137 ** satisfy the query. This is preferable to generating a new
98138 ** ephemeral table. */
98139 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
98140 sqlite3 *db = pParse->db; /* Database connection */
98141 Table *pTab; /* Table <table>. */
98142 i16 iDb; /* Database idx for pTab */
98143 ExprList *pEList = p->pEList;
98144 int nExpr = pEList->nExpr;
98145
98146 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
98147 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
98148 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
98149 pTab = p->pSrc->a[0].pTab;
98150
98151 /* Code an OP_Transaction and OP_TableLock for <table>. */
98152 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
98153 sqlite3CodeVerifySchema(pParse, iDb);
98154 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
98155
98156 assert(v); /* sqlite3GetVdbe() has always been previously called */
98157 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
98158 /* The "x IN (SELECT rowid FROM table)" case */
98159 int iAddr = sqlite3VdbeAddOp0(v, OP_Once);
98160 VdbeCoverage(v);
98161
98162 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
98163 eType = IN_INDEX_ROWID;
98164 ExplainQueryPlan((pParse, 0,
98165 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
98166 sqlite3VdbeJumpHere(v, iAddr);
98167 }else{
98168 Index *pIdx; /* Iterator variable */
98169 int affinity_ok = 1;
98170 int i;
98171
98172 /* Check that the affinity that will be used to perform each
98173 ** comparison is the same as the affinity of each column in table
98174 ** on the RHS of the IN operator. If it not, it is not possible to
98175 ** use any index of the RHS table. */
98176 for(i=0; i<nExpr && affinity_ok; i++){
98177 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
98178 int iCol = pEList->a[i].pExpr->iColumn;
98179 char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
98180 char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
98181 testcase( cmpaff==SQLITE_AFF_BLOB );
98182 testcase( cmpaff==SQLITE_AFF_TEXT );
98183 switch( cmpaff ){
98184 case SQLITE_AFF_BLOB:
98185 break;
98186 case SQLITE_AFF_TEXT:
98187 /* sqlite3CompareAffinity() only returns TEXT if one side or the
98188 ** other has no affinity and the other side is TEXT. Hence,
98189 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
98190 ** and for the term on the LHS of the IN to have no affinity. */
98191 assert( idxaff==SQLITE_AFF_TEXT );
98192 break;
98193 default:
98194 affinity_ok = sqlite3IsNumericAffinity(idxaff);
98195 }
98196 }
98197
98198 if( affinity_ok ){
98199 /* Search for an existing index that will work for this IN operator */
98200 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
98201 Bitmask colUsed; /* Columns of the index used */
98202 Bitmask mCol; /* Mask for the current column */
98203 if( pIdx->nColumn<nExpr ) continue;
98204 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
98205 ** BITMASK(nExpr) without overflowing */
98206 testcase( pIdx->nColumn==BMS-2 );
98207 testcase( pIdx->nColumn==BMS-1 );
98208 if( pIdx->nColumn>=BMS-1 ) continue;
98209 if( mustBeUnique ){
98210 if( pIdx->nKeyCol>nExpr
98211 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
98212 ){
98213 continue; /* This index is not unique over the IN RHS columns */
98214 }
98215 }
98216
98217 colUsed = 0; /* Columns of index used so far */
98218 for(i=0; i<nExpr; i++){
98219 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
98220 Expr *pRhs = pEList->a[i].pExpr;
98221 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
98222 int j;
98223
98224 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
98225 for(j=0; j<nExpr; j++){
98226 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
98227 assert( pIdx->azColl[j] );
98228 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
98229 continue;
98230 }
98231 break;
98232 }
98233 if( j==nExpr ) break;
98234 mCol = MASKBIT(j);
98235 if( mCol & colUsed ) break; /* Each column used only once */
98236 colUsed |= mCol;
98237 if( aiMap ) aiMap[i] = j;
98238 }
98239
98240 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
98241 if( colUsed==(MASKBIT(nExpr)-1) ){
98242 /* If we reach this point, that means the index pIdx is usable */
98243 int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
98244 ExplainQueryPlan((pParse, 0,
98245 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
98246 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
98247 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
98248 VdbeComment((v, "%s", pIdx->zName));
98249 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
98250 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
98251
98252 if( prRhsHasNull ){
98253#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
98254 i64 mask = (1<<nExpr)-1;
98255 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed,
98256 iTab, 0, 0, (u8*)&mask, P4_INT64);
98257#endif
98258 *prRhsHasNull = ++pParse->nMem;
98259 if( nExpr==1 ){
98260 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
98261 }
98262 }
98263 sqlite3VdbeJumpHere(v, iAddr);
98264 }
98265 } /* End loop over indexes */
98266 } /* End if( affinity_ok ) */
98267 } /* End if not an rowid index */
98268 } /* End attempt to optimize using an index */
98269
98270 /* If no preexisting index is available for the IN clause
98271 ** and IN_INDEX_NOOP is an allowed reply
98272 ** and the RHS of the IN operator is a list, not a subquery
98273 ** and the RHS is not constant or has two or fewer terms,
98274 ** then it is not worth creating an ephemeral table to evaluate
98275 ** the IN operator so return IN_INDEX_NOOP.
98276 */
98277 if( eType==0
98278 && (inFlags & IN_INDEX_NOOP_OK)
98279 && !ExprHasProperty(pX, EP_xIsSelect)
98280 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
98281 ){
98282 eType = IN_INDEX_NOOP;
98283 }
98284
98285 if( eType==0 ){
98286 /* Could not find an existing table or index to use as the RHS b-tree.
98287 ** We will have to generate an ephemeral table to do the job.
98288 */
98289 u32 savedNQueryLoop = pParse->nQueryLoop;
98290 int rMayHaveNull = 0;
98291 eType = IN_INDEX_EPH;
98292 if( inFlags & IN_INDEX_LOOP ){
98293 pParse->nQueryLoop = 0;
98294 if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
98295 eType = IN_INDEX_ROWID;
98296 }
98297 }else if( prRhsHasNull ){
98298 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
98299 }
98300 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
98301 pParse->nQueryLoop = savedNQueryLoop;
98302 }else{
98303 pX->iTable = iTab;
98304 }
98305
98306 if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
98307 int i, n;
98308 n = sqlite3ExprVectorSize(pX->pLeft);
98309 for(i=0; i<n; i++) aiMap[i] = i;
98310 }
98311 return eType;
98312}
98313#endif
98314
98315#ifndef SQLITE_OMIT_SUBQUERY
98316/*
98317** Argument pExpr is an (?, ?...) IN(...) expression. This
98318** function allocates and returns a nul-terminated string containing
98319** the affinities to be used for each column of the comparison.
98320**
98321** It is the responsibility of the caller to ensure that the returned
98322** string is eventually freed using sqlite3DbFree().
98323*/
98324static char *exprINAffinity(Parse *pParse, Expr *pExpr){
98325 Expr *pLeft = pExpr->pLeft;
98326 int nVal = sqlite3ExprVectorSize(pLeft);
98327 Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
98328 char *zRet;
98329
98330 assert( pExpr->op==TK_IN );
98331 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
98332 if( zRet ){
98333 int i;
98334 for(i=0; i<nVal; i++){
98335 Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
98336 char a = sqlite3ExprAffinity(pA);
98337 if( pSelect ){
98338 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
98339 }else{
98340 zRet[i] = a;
98341 }
98342 }
98343 zRet[nVal] = '\0';
98344 }
98345 return zRet;
98346}
98347#endif
98348
98349#ifndef SQLITE_OMIT_SUBQUERY
98350/*
98351** Load the Parse object passed as the first argument with an error
98352** message of the form:
98353**
98354** "sub-select returns N columns - expected M"
98355*/
98356SQLITE_PRIVATE void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
98357 const char *zFmt = "sub-select returns %d columns - expected %d";
98358 sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
98359}
98360#endif
98361
98362/*
98363** Expression pExpr is a vector that has been used in a context where
98364** it is not permitted. If pExpr is a sub-select vector, this routine
98365** loads the Parse object with a message of the form:
98366**
98367** "sub-select returns N columns - expected 1"
98368**
98369** Or, if it is a regular scalar vector:
98370**
98371** "row value misused"
98372*/
98373SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
98374#ifndef SQLITE_OMIT_SUBQUERY
98375 if( pExpr->flags & EP_xIsSelect ){
98376 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
98377 }else
98378#endif
98379 {
98380 sqlite3ErrorMsg(pParse, "row value misused");
98381 }
98382}
98383
98384/*
98385** Generate code for scalar subqueries used as a subquery expression, EXISTS,
98386** or IN operators. Examples:
98387**
98388** (SELECT a FROM b) -- subquery
98389** EXISTS (SELECT a FROM b) -- EXISTS subquery
98390** x IN (4,5,11) -- IN operator with list on right-hand side
98391** x IN (SELECT a FROM b) -- IN operator with subquery on the right
98392**
98393** The pExpr parameter describes the expression that contains the IN
98394** operator or subquery.
98395**
98396** If parameter isRowid is non-zero, then expression pExpr is guaranteed
98397** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
98398** to some integer key column of a table B-Tree. In this case, use an
98399** intkey B-Tree to store the set of IN(...) values instead of the usual
98400** (slower) variable length keys B-Tree.
98401**
98402** If rMayHaveNull is non-zero, that means that the operation is an IN
98403** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
98404** All this routine does is initialize the register given by rMayHaveNull
98405** to NULL. Calling routines will take care of changing this register
98406** value to non-NULL if the RHS is NULL-free.
98407**
98408** For a SELECT or EXISTS operator, return the register that holds the
98409** result. For a multi-column SELECT, the result is stored in a contiguous
98410** array of registers and the return value is the register of the left-most
98411** result column. Return 0 for IN operators or if an error occurs.
98412*/
98413#ifndef SQLITE_OMIT_SUBQUERY
98414SQLITE_PRIVATE int sqlite3CodeSubselect(
98415 Parse *pParse, /* Parsing context */
98416 Expr *pExpr, /* The IN, SELECT, or EXISTS operator */
98417 int rHasNullFlag, /* Register that records whether NULLs exist in RHS */
98418 int isRowid /* If true, LHS of IN operator is a rowid */
98419){
98420 int jmpIfDynamic = -1; /* One-time test address */
98421 int rReg = 0; /* Register storing resulting */
98422 Vdbe *v = sqlite3GetVdbe(pParse);
98423 if( NEVER(v==0) ) return 0;
98424
98425 /* The evaluation of the IN/EXISTS/SELECT must be repeated every time it
98426 ** is encountered if any of the following is true:
98427 **
98428 ** * The right-hand side is a correlated subquery
98429 ** * The right-hand side is an expression list containing variables
98430 ** * We are inside a trigger
98431 **
98432 ** If all of the above are false, then we can run this code just once
98433 ** save the results, and reuse the same result on subsequent invocations.
98434 */
98435 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
98436 jmpIfDynamic = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
98437 }
98438
98439 switch( pExpr->op ){
98440 case TK_IN: {
98441 int addr; /* Address of OP_OpenEphemeral instruction */
98442 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
98443 KeyInfo *pKeyInfo = 0; /* Key information */
98444 int nVal; /* Size of vector pLeft */
98445
98446 nVal = sqlite3ExprVectorSize(pLeft);
98447 assert( !isRowid || nVal==1 );
98448
98449 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
98450 ** expression it is handled the same way. An ephemeral table is
98451 ** filled with index keys representing the results from the
98452 ** SELECT or the <exprlist>.
98453 **
98454 ** If the 'x' expression is a column value, or the SELECT...
98455 ** statement returns a column value, then the affinity of that
98456 ** column is used to build the index keys. If both 'x' and the
98457 ** SELECT... statement are columns, then numeric affinity is used
98458 ** if either column has NUMERIC or INTEGER affinity. If neither
98459 ** 'x' nor the SELECT... statement are columns, then numeric affinity
98460 ** is used.
98461 */
98462 pExpr->iTable = pParse->nTab++;
98463 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral,
98464 pExpr->iTable, (isRowid?0:nVal));
98465 pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
98466
98467 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
98468 /* Case 1: expr IN (SELECT ...)
98469 **
98470 ** Generate code to write the results of the select into the temporary
98471 ** table allocated and opened above.
98472 */
98473 Select *pSelect = pExpr->x.pSelect;
98474 ExprList *pEList = pSelect->pEList;
98475
98476 ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY",
98477 jmpIfDynamic>=0?"":"CORRELATED "
98478 ));
98479 assert( !isRowid );
98480 /* If the LHS and RHS of the IN operator do not match, that
98481 ** error will have been caught long before we reach this point. */
98482 if( ALWAYS(pEList->nExpr==nVal) ){
98483 SelectDest dest;
98484 int i;
98485 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
98486 dest.zAffSdst = exprINAffinity(pParse, pExpr);
98487 pSelect->iLimit = 0;
98488 testcase( pSelect->selFlags & SF_Distinct );
98489 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
98490 if( sqlite3Select(pParse, pSelect, &dest) ){
98491 sqlite3DbFree(pParse->db, dest.zAffSdst);
98492 sqlite3KeyInfoUnref(pKeyInfo);
98493 return 0;
98494 }
98495 sqlite3DbFree(pParse->db, dest.zAffSdst);
98496 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
98497 assert( pEList!=0 );
98498 assert( pEList->nExpr>0 );
98499 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
98500 for(i=0; i<nVal; i++){
98501 Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
98502 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
98503 pParse, p, pEList->a[i].pExpr
98504 );
98505 }
98506 }
98507 }else if( ALWAYS(pExpr->x.pList!=0) ){
98508 /* Case 2: expr IN (exprlist)
98509 **
98510 ** For each expression, build an index key from the evaluation and
98511 ** store it in the temporary table. If <expr> is a column, then use
98512 ** that columns affinity when building index keys. If <expr> is not
98513 ** a column, use numeric affinity.
98514 */
98515 char affinity; /* Affinity of the LHS of the IN */
98516 int i;
98517 ExprList *pList = pExpr->x.pList;
98518 struct ExprList_item *pItem;
98519 int r1, r2, r3;
98520 affinity = sqlite3ExprAffinity(pLeft);
98521 if( !affinity ){
98522 affinity = SQLITE_AFF_BLOB;
98523 }
98524 if( pKeyInfo ){
98525 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
98526 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
98527 }
98528
98529 /* Loop through each expression in <exprlist>. */
98530 r1 = sqlite3GetTempReg(pParse);
98531 r2 = sqlite3GetTempReg(pParse);
98532 if( isRowid ) sqlite3VdbeAddOp4(v, OP_Blob, 0, r2, 0, "", P4_STATIC);
98533 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
98534 Expr *pE2 = pItem->pExpr;
98535 int iValToIns;
98536
98537 /* If the expression is not constant then we will need to
98538 ** disable the test that was generated above that makes sure
98539 ** this code only executes once. Because for a non-constant
98540 ** expression we need to rerun this code each time.
98541 */
98542 if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){
98543 sqlite3VdbeChangeToNoop(v, jmpIfDynamic);
98544 jmpIfDynamic = -1;
98545 }
98546
98547 /* Evaluate the expression and insert it into the temp table */
98548 if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
98549 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
98550 }else{
98551 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
98552 if( isRowid ){
98553 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
98554 sqlite3VdbeCurrentAddr(v)+2);
98555 VdbeCoverage(v);
98556 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
98557 }else{
98558 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
98559 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pExpr->iTable, r2, r3, 1);
98560 }
98561 }
98562 }
98563 sqlite3ReleaseTempReg(pParse, r1);
98564 sqlite3ReleaseTempReg(pParse, r2);
98565 }
98566 if( pKeyInfo ){
98567 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
98568 }
98569 break;
98570 }
98571
98572 case TK_EXISTS:
98573 case TK_SELECT:
98574 default: {
98575 /* Case 3: (SELECT ... FROM ...)
98576 ** or: EXISTS(SELECT ... FROM ...)
98577 **
98578 ** For a SELECT, generate code to put the values for all columns of
98579 ** the first row into an array of registers and return the index of
98580 ** the first register.
98581 **
98582 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
98583 ** into a register and return that register number.
98584 **
98585 ** In both cases, the query is augmented with "LIMIT 1". Any
98586 ** preexisting limit is discarded in place of the new LIMIT 1.
98587 */
98588 Select *pSel; /* SELECT statement to encode */
98589 SelectDest dest; /* How to deal with SELECT result */
98590 int nReg; /* Registers to allocate */
98591 Expr *pLimit; /* New limit expression */
98592
98593 testcase( pExpr->op==TK_EXISTS );
98594 testcase( pExpr->op==TK_SELECT );
98595 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
98596 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
98597
98598 pSel = pExpr->x.pSelect;
98599 ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY",
98600 jmpIfDynamic>=0?"":"CORRELATED "));
98601 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
98602 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
98603 pParse->nMem += nReg;
98604 if( pExpr->op==TK_SELECT ){
98605 dest.eDest = SRT_Mem;
98606 dest.iSdst = dest.iSDParm;
98607 dest.nSdst = nReg;
98608 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
98609 VdbeComment((v, "Init subquery result"));
98610 }else{
98611 dest.eDest = SRT_Exists;
98612 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
98613 VdbeComment((v, "Init EXISTS result"));
98614 }
98615 pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[1], 0);
98616 if( pSel->pLimit ){
98617 sqlite3ExprDelete(pParse->db, pSel->pLimit->pLeft);
98618 pSel->pLimit->pLeft = pLimit;
98619 }else{
98620 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
98621 }
98622 pSel->iLimit = 0;
98623 if( sqlite3Select(pParse, pSel, &dest) ){
98624 return 0;
98625 }
98626 rReg = dest.iSDParm;
98627 ExprSetVVAProperty(pExpr, EP_NoReduce);
98628 break;
98629 }
98630 }
98631
98632 if( rHasNullFlag ){
98633 sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
98634 }
98635
98636 if( jmpIfDynamic>=0 ){
98637 sqlite3VdbeJumpHere(v, jmpIfDynamic);
98638 }
98639
98640 return rReg;
98641}
98642#endif /* SQLITE_OMIT_SUBQUERY */
98643
98644#ifndef SQLITE_OMIT_SUBQUERY
98645/*
98646** Expr pIn is an IN(...) expression. This function checks that the
98647** sub-select on the RHS of the IN() operator has the same number of
98648** columns as the vector on the LHS. Or, if the RHS of the IN() is not
98649** a sub-query, that the LHS is a vector of size 1.
98650*/
98651SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
98652 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
98653 if( (pIn->flags & EP_xIsSelect) ){
98654 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
98655 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
98656 return 1;
98657 }
98658 }else if( nVector!=1 ){
98659 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
98660 return 1;
98661 }
98662 return 0;
98663}
98664#endif
98665
98666#ifndef SQLITE_OMIT_SUBQUERY
98667/*
98668** Generate code for an IN expression.
98669**
98670** x IN (SELECT ...)
98671** x IN (value, value, ...)
98672**
98673** The left-hand side (LHS) is a scalar or vector expression. The
98674** right-hand side (RHS) is an array of zero or more scalar values, or a
98675** subquery. If the RHS is a subquery, the number of result columns must
98676** match the number of columns in the vector on the LHS. If the RHS is
98677** a list of values, the LHS must be a scalar.
98678**
98679** The IN operator is true if the LHS value is contained within the RHS.
98680** The result is false if the LHS is definitely not in the RHS. The
98681** result is NULL if the presence of the LHS in the RHS cannot be
98682** determined due to NULLs.
98683**
98684** This routine generates code that jumps to destIfFalse if the LHS is not
98685** contained within the RHS. If due to NULLs we cannot determine if the LHS
98686** is contained in the RHS then jump to destIfNull. If the LHS is contained
98687** within the RHS then fall through.
98688**
98689** See the separate in-operator.md documentation file in the canonical
98690** SQLite source tree for additional information.
98691*/
98692static void sqlite3ExprCodeIN(
98693 Parse *pParse, /* Parsing and code generating context */
98694 Expr *pExpr, /* The IN expression */
98695 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
98696 int destIfNull /* Jump here if the results are unknown due to NULLs */
98697){
98698 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
98699 int eType; /* Type of the RHS */
98700 int rLhs; /* Register(s) holding the LHS values */
98701 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */
98702 Vdbe *v; /* Statement under construction */
98703 int *aiMap = 0; /* Map from vector field to index column */
98704 char *zAff = 0; /* Affinity string for comparisons */
98705 int nVector; /* Size of vectors for this IN operator */
98706 int iDummy; /* Dummy parameter to exprCodeVector() */
98707 Expr *pLeft; /* The LHS of the IN operator */
98708 int i; /* loop counter */
98709 int destStep2; /* Where to jump when NULLs seen in step 2 */
98710 int destStep6 = 0; /* Start of code for Step 6 */
98711 int addrTruthOp; /* Address of opcode that determines the IN is true */
98712 int destNotNull; /* Jump here if a comparison is not true in step 6 */
98713 int addrTop; /* Top of the step-6 loop */
98714
98715 pLeft = pExpr->pLeft;
98716 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
98717 zAff = exprINAffinity(pParse, pExpr);
98718 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
98719 aiMap = (int*)sqlite3DbMallocZero(
98720 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
98721 );
98722 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
98723
98724 /* Attempt to compute the RHS. After this step, if anything other than
98725 ** IN_INDEX_NOOP is returned, the table opened ith cursor pExpr->iTable
98726 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
98727 ** the RHS has not yet been coded. */
98728 v = pParse->pVdbe;
98729 assert( v!=0 ); /* OOM detected prior to this routine */
98730 VdbeNoopComment((v, "begin IN expr"));
98731 eType = sqlite3FindInIndex(pParse, pExpr,
98732 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
98733 destIfFalse==destIfNull ? 0 : &rRhsHasNull, aiMap);
98734
98735 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
98736 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC
98737 );
98738#ifdef SQLITE_DEBUG
98739 /* Confirm that aiMap[] contains nVector integer values between 0 and
98740 ** nVector-1. */
98741 for(i=0; i<nVector; i++){
98742 int j, cnt;
98743 for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
98744 assert( cnt==1 );
98745 }
98746#endif
98747
98748 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
98749 ** vector, then it is stored in an array of nVector registers starting
98750 ** at r1.
98751 **
98752 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
98753 ** so that the fields are in the same order as an existing index. The
98754 ** aiMap[] array contains a mapping from the original LHS field order to
98755 ** the field order that matches the RHS index.
98756 */
98757 rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
98758 for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
98759 if( i==nVector ){
98760 /* LHS fields are not reordered */
98761 rLhs = rLhsOrig;
98762 }else{
98763 /* Need to reorder the LHS fields according to aiMap */
98764 rLhs = sqlite3GetTempRange(pParse, nVector);
98765 for(i=0; i<nVector; i++){
98766 sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0);
98767 }
98768 }
98769
98770 /* If sqlite3FindInIndex() did not find or create an index that is
98771 ** suitable for evaluating the IN operator, then evaluate using a
98772 ** sequence of comparisons.
98773 **
98774 ** This is step (1) in the in-operator.md optimized algorithm.
98775 */
98776 if( eType==IN_INDEX_NOOP ){
98777 ExprList *pList = pExpr->x.pList;
98778 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
98779 int labelOk = sqlite3VdbeMakeLabel(v);
98780 int r2, regToFree;
98781 int regCkNull = 0;
98782 int ii;
98783 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
98784 if( destIfNull!=destIfFalse ){
98785 regCkNull = sqlite3GetTempReg(pParse);
98786 sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull);
98787 }
98788 for(ii=0; ii<pList->nExpr; ii++){
98789 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
98790 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
98791 sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
98792 }
98793 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
98794 sqlite3VdbeAddOp4(v, OP_Eq, rLhs, labelOk, r2,
98795 (void*)pColl, P4_COLLSEQ);
98796 VdbeCoverageIf(v, ii<pList->nExpr-1);
98797 VdbeCoverageIf(v, ii==pList->nExpr-1);
98798 sqlite3VdbeChangeP5(v, zAff[0]);
98799 }else{
98800 assert( destIfNull==destIfFalse );
98801 sqlite3VdbeAddOp4(v, OP_Ne, rLhs, destIfFalse, r2,
98802 (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
98803 sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL);
98804 }
98805 sqlite3ReleaseTempReg(pParse, regToFree);
98806 }
98807 if( regCkNull ){
98808 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
98809 sqlite3VdbeGoto(v, destIfFalse);
98810 }
98811 sqlite3VdbeResolveLabel(v, labelOk);
98812 sqlite3ReleaseTempReg(pParse, regCkNull);
98813 goto sqlite3ExprCodeIN_finished;
98814 }
98815
98816 /* Step 2: Check to see if the LHS contains any NULL columns. If the
98817 ** LHS does contain NULLs then the result must be either FALSE or NULL.
98818 ** We will then skip the binary search of the RHS.
98819 */
98820 if( destIfNull==destIfFalse ){
98821 destStep2 = destIfFalse;
98822 }else{
98823 destStep2 = destStep6 = sqlite3VdbeMakeLabel(v);
98824 }
98825 for(i=0; i<nVector; i++){
98826 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
98827 if( sqlite3ExprCanBeNull(p) ){
98828 sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2);
98829 VdbeCoverage(v);
98830 }
98831 }
98832
98833 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
98834 ** of the RHS using the LHS as a probe. If found, the result is
98835 ** true.
98836 */
98837 if( eType==IN_INDEX_ROWID ){
98838 /* In this case, the RHS is the ROWID of table b-tree and so we also
98839 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
98840 ** into a single opcode. */
98841 sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, rLhs);
98842 VdbeCoverage(v);
98843 addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto); /* Return True */
98844 }else{
98845 sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector);
98846 if( destIfFalse==destIfNull ){
98847 /* Combine Step 3 and Step 5 into a single opcode */
98848 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse,
98849 rLhs, nVector); VdbeCoverage(v);
98850 goto sqlite3ExprCodeIN_finished;
98851 }
98852 /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
98853 addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0,
98854 rLhs, nVector); VdbeCoverage(v);
98855 }
98856
98857 /* Step 4. If the RHS is known to be non-NULL and we did not find
98858 ** an match on the search above, then the result must be FALSE.
98859 */
98860 if( rRhsHasNull && nVector==1 ){
98861 sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse);
98862 VdbeCoverage(v);
98863 }
98864
98865 /* Step 5. If we do not care about the difference between NULL and
98866 ** FALSE, then just return false.
98867 */
98868 if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse);
98869
98870 /* Step 6: Loop through rows of the RHS. Compare each row to the LHS.
98871 ** If any comparison is NULL, then the result is NULL. If all
98872 ** comparisons are FALSE then the final result is FALSE.
98873 **
98874 ** For a scalar LHS, it is sufficient to check just the first row
98875 ** of the RHS.
98876 */
98877 if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6);
98878 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
98879 VdbeCoverage(v);
98880 if( nVector>1 ){
98881 destNotNull = sqlite3VdbeMakeLabel(v);
98882 }else{
98883 /* For nVector==1, combine steps 6 and 7 by immediately returning
98884 ** FALSE if the first comparison is not NULL */
98885 destNotNull = destIfFalse;
98886 }
98887 for(i=0; i<nVector; i++){
98888 Expr *p;
98889 CollSeq *pColl;
98890 int r3 = sqlite3GetTempReg(pParse);
98891 p = sqlite3VectorFieldSubexpr(pLeft, i);
98892 pColl = sqlite3ExprCollSeq(pParse, p);
98893 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, i, r3);
98894 sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3,
98895 (void*)pColl, P4_COLLSEQ);
98896 VdbeCoverage(v);
98897 sqlite3ReleaseTempReg(pParse, r3);
98898 }
98899 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
98900 if( nVector>1 ){
98901 sqlite3VdbeResolveLabel(v, destNotNull);
98902 sqlite3VdbeAddOp2(v, OP_Next, pExpr->iTable, addrTop+1);
98903 VdbeCoverage(v);
98904
98905 /* Step 7: If we reach this point, we know that the result must
98906 ** be false. */
98907 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
98908 }
98909
98910 /* Jumps here in order to return true. */
98911 sqlite3VdbeJumpHere(v, addrTruthOp);
98912
98913sqlite3ExprCodeIN_finished:
98914 if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
98915 VdbeComment((v, "end IN expr"));
98916sqlite3ExprCodeIN_oom_error:
98917 sqlite3DbFree(pParse->db, aiMap);
98918 sqlite3DbFree(pParse->db, zAff);
98919}
98920#endif /* SQLITE_OMIT_SUBQUERY */
98921
98922#ifndef SQLITE_OMIT_FLOATING_POINT
98923/*
98924** Generate an instruction that will put the floating point
98925** value described by z[0..n-1] into register iMem.
98926**
98927** The z[] string will probably not be zero-terminated. But the
98928** z[n] character is guaranteed to be something that does not look
98929** like the continuation of the number.
98930*/
98931static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
98932 if( ALWAYS(z!=0) ){
98933 double value;
98934 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
98935 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
98936 if( negateFlag ) value = -value;
98937 sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
98938 }
98939}
98940#endif
98941
98942
98943/*
98944** Generate an instruction that will put the integer describe by
98945** text z[0..n-1] into register iMem.
98946**
98947** Expr.u.zToken is always UTF8 and zero-terminated.
98948*/
98949static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
98950 Vdbe *v = pParse->pVdbe;
98951 if( pExpr->flags & EP_IntValue ){
98952 int i = pExpr->u.iValue;
98953 assert( i>=0 );
98954 if( negFlag ) i = -i;
98955 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
98956 }else{
98957 int c;
98958 i64 value;
98959 const char *z = pExpr->u.zToken;
98960 assert( z!=0 );
98961 c = sqlite3DecOrHexToI64(z, &value);
98962 if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){
98963#ifdef SQLITE_OMIT_FLOATING_POINT
98964 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
98965#else
98966#ifndef SQLITE_OMIT_HEX_INTEGER
98967 if( sqlite3_strnicmp(z,"0x",2)==0 ){
98968 sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
98969 }else
98970#endif
98971 {
98972 codeReal(v, z, negFlag, iMem);
98973 }
98974#endif
98975 }else{
98976 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
98977 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
98978 }
98979 }
98980}
98981
98982
98983/* Generate code that will load into register regOut a value that is
98984** appropriate for the iIdxCol-th column of index pIdx.
98985*/
98986SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(
98987 Parse *pParse, /* The parsing context */
98988 Index *pIdx, /* The index whose column is to be loaded */
98989 int iTabCur, /* Cursor pointing to a table row */
98990 int iIdxCol, /* The column of the index to be loaded */
98991 int regOut /* Store the index column value in this register */
98992){
98993 i16 iTabCol = pIdx->aiColumn[iIdxCol];
98994 if( iTabCol==XN_EXPR ){
98995 assert( pIdx->aColExpr );
98996 assert( pIdx->aColExpr->nExpr>iIdxCol );
98997 pParse->iSelfTab = iTabCur + 1;
98998 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
98999 pParse->iSelfTab = 0;
99000 }else{
99001 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
99002 iTabCol, regOut);
99003 }
99004}
99005
99006/*
99007** Generate code to extract the value of the iCol-th column of a table.
99008*/
99009SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
99010 Vdbe *v, /* The VDBE under construction */
99011 Table *pTab, /* The table containing the value */
99012 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
99013 int iCol, /* Index of the column to extract */
99014 int regOut /* Extract the value into this register */
99015){
99016 if( pTab==0 ){
99017 sqlite3VdbeAddOp3(v, OP_Column, iTabCur, iCol, regOut);
99018 return;
99019 }
99020 if( iCol<0 || iCol==pTab->iPKey ){
99021 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
99022 }else{
99023 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
99024 int x = iCol;
99025 if( !HasRowid(pTab) && !IsVirtual(pTab) ){
99026 x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
99027 }
99028 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
99029 }
99030 if( iCol>=0 ){
99031 sqlite3ColumnDefault(v, pTab, iCol, regOut);
99032 }
99033}
99034
99035/*
99036** Generate code that will extract the iColumn-th column from
99037** table pTab and store the column value in register iReg.
99038**
99039** There must be an open cursor to pTab in iTable when this routine
99040** is called. If iColumn<0 then code is generated that extracts the rowid.
99041*/
99042SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
99043 Parse *pParse, /* Parsing and code generating context */
99044 Table *pTab, /* Description of the table we are reading from */
99045 int iColumn, /* Index of the table column */
99046 int iTable, /* The cursor pointing to the table */
99047 int iReg, /* Store results here */
99048 u8 p5 /* P5 value for OP_Column + FLAGS */
99049){
99050 Vdbe *v = pParse->pVdbe;
99051 assert( v!=0 );
99052 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
99053 if( p5 ){
99054 sqlite3VdbeChangeP5(v, p5);
99055 }
99056 return iReg;
99057}
99058
99059/*
99060** Generate code to move content from registers iFrom...iFrom+nReg-1
99061** over to iTo..iTo+nReg-1.
99062*/
99063SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
99064 assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
99065 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
99066}
99067
99068/*
99069** Convert a scalar expression node to a TK_REGISTER referencing
99070** register iReg. The caller must ensure that iReg already contains
99071** the correct value for the expression.
99072*/
99073static void exprToRegister(Expr *p, int iReg){
99074 p->op2 = p->op;
99075 p->op = TK_REGISTER;
99076 p->iTable = iReg;
99077 ExprClearProperty(p, EP_Skip);
99078}
99079
99080/*
99081** Evaluate an expression (either a vector or a scalar expression) and store
99082** the result in continguous temporary registers. Return the index of
99083** the first register used to store the result.
99084**
99085** If the returned result register is a temporary scalar, then also write
99086** that register number into *piFreeable. If the returned result register
99087** is not a temporary or if the expression is a vector set *piFreeable
99088** to 0.
99089*/
99090static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
99091 int iResult;
99092 int nResult = sqlite3ExprVectorSize(p);
99093 if( nResult==1 ){
99094 iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
99095 }else{
99096 *piFreeable = 0;
99097 if( p->op==TK_SELECT ){
99098#if SQLITE_OMIT_SUBQUERY
99099 iResult = 0;
99100#else
99101 iResult = sqlite3CodeSubselect(pParse, p, 0, 0);
99102#endif
99103 }else{
99104 int i;
99105 iResult = pParse->nMem+1;
99106 pParse->nMem += nResult;
99107 for(i=0; i<nResult; i++){
99108 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
99109 }
99110 }
99111 }
99112 return iResult;
99113}
99114
99115
99116/*
99117** Generate code into the current Vdbe to evaluate the given
99118** expression. Attempt to store the results in register "target".
99119** Return the register where results are stored.
99120**
99121** With this routine, there is no guarantee that results will
99122** be stored in target. The result might be stored in some other
99123** register if it is convenient to do so. The calling function
99124** must check the return code and move the results to the desired
99125** register.
99126*/
99127SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
99128 Vdbe *v = pParse->pVdbe; /* The VM under construction */
99129 int op; /* The opcode being coded */
99130 int inReg = target; /* Results stored in register inReg */
99131 int regFree1 = 0; /* If non-zero free this temporary register */
99132 int regFree2 = 0; /* If non-zero free this temporary register */
99133 int r1, r2; /* Various register numbers */
99134 Expr tempX; /* Temporary expression node */
99135 int p5 = 0;
99136
99137 assert( target>0 && target<=pParse->nMem );
99138 if( v==0 ){
99139 assert( pParse->db->mallocFailed );
99140 return 0;
99141 }
99142
99143expr_code_doover:
99144 if( pExpr==0 ){
99145 op = TK_NULL;
99146 }else{
99147 op = pExpr->op;
99148 }
99149 switch( op ){
99150 case TK_AGG_COLUMN: {
99151 AggInfo *pAggInfo = pExpr->pAggInfo;
99152 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
99153 if( !pAggInfo->directMode ){
99154 assert( pCol->iMem>0 );
99155 return pCol->iMem;
99156 }else if( pAggInfo->useSortingIdx ){
99157 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
99158 pCol->iSorterColumn, target);
99159 return target;
99160 }
99161 /* Otherwise, fall thru into the TK_COLUMN case */
99162 }
99163 case TK_COLUMN: {
99164 int iTab = pExpr->iTable;
99165 if( ExprHasProperty(pExpr, EP_FixedCol) ){
99166 /* This COLUMN expression is really a constant due to WHERE clause
99167 ** constraints, and that constant is coded by the pExpr->pLeft
99168 ** expresssion. However, make sure the constant has the correct
99169 ** datatype by applying the Affinity of the table column to the
99170 ** constant.
99171 */
99172 int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
99173 int aff = sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
99174 if( aff!=SQLITE_AFF_BLOB ){
99175 static const char zAff[] = "B\000C\000D\000E";
99176 assert( SQLITE_AFF_BLOB=='A' );
99177 assert( SQLITE_AFF_TEXT=='B' );
99178 if( iReg!=target ){
99179 sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target);
99180 iReg = target;
99181 }
99182 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0,
99183 &zAff[(aff-'B')*2], P4_STATIC);
99184 }
99185 return iReg;
99186 }
99187 if( iTab<0 ){
99188 if( pParse->iSelfTab<0 ){
99189 /* Generating CHECK constraints or inserting into partial index */
99190 return pExpr->iColumn - pParse->iSelfTab;
99191 }else{
99192 /* Coding an expression that is part of an index where column names
99193 ** in the index refer to the table to which the index belongs */
99194 iTab = pParse->iSelfTab - 1;
99195 }
99196 }
99197 return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
99198 pExpr->iColumn, iTab, target,
99199 pExpr->op2);
99200 }
99201 case TK_INTEGER: {
99202 codeInteger(pParse, pExpr, 0, target);
99203 return target;
99204 }
99205 case TK_TRUEFALSE: {
99206 sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target);
99207 return target;
99208 }
99209#ifndef SQLITE_OMIT_FLOATING_POINT
99210 case TK_FLOAT: {
99211 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99212 codeReal(v, pExpr->u.zToken, 0, target);
99213 return target;
99214 }
99215#endif
99216 case TK_STRING: {
99217 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99218 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
99219 return target;
99220 }
99221 case TK_NULL: {
99222 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
99223 return target;
99224 }
99225#ifndef SQLITE_OMIT_BLOB_LITERAL
99226 case TK_BLOB: {
99227 int n;
99228 const char *z;
99229 char *zBlob;
99230 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99231 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
99232 assert( pExpr->u.zToken[1]=='\'' );
99233 z = &pExpr->u.zToken[2];
99234 n = sqlite3Strlen30(z) - 1;
99235 assert( z[n]=='\'' );
99236 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
99237 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
99238 return target;
99239 }
99240#endif
99241 case TK_VARIABLE: {
99242 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99243 assert( pExpr->u.zToken!=0 );
99244 assert( pExpr->u.zToken[0]!=0 );
99245 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
99246 if( pExpr->u.zToken[1]!=0 ){
99247 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
99248 assert( pExpr->u.zToken[0]=='?' || strcmp(pExpr->u.zToken, z)==0 );
99249 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
99250 sqlite3VdbeAppendP4(v, (char*)z, P4_STATIC);
99251 }
99252 return target;
99253 }
99254 case TK_REGISTER: {
99255 return pExpr->iTable;
99256 }
99257#ifndef SQLITE_OMIT_CAST
99258 case TK_CAST: {
99259 /* Expressions of the form: CAST(pLeft AS token) */
99260 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
99261 if( inReg!=target ){
99262 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
99263 inReg = target;
99264 }
99265 sqlite3VdbeAddOp2(v, OP_Cast, target,
99266 sqlite3AffinityType(pExpr->u.zToken, 0));
99267 return inReg;
99268 }
99269#endif /* SQLITE_OMIT_CAST */
99270 case TK_IS:
99271 case TK_ISNOT:
99272 op = (op==TK_IS) ? TK_EQ : TK_NE;
99273 p5 = SQLITE_NULLEQ;
99274 /* fall-through */
99275 case TK_LT:
99276 case TK_LE:
99277 case TK_GT:
99278 case TK_GE:
99279 case TK_NE:
99280 case TK_EQ: {
99281 Expr *pLeft = pExpr->pLeft;
99282 if( sqlite3ExprIsVector(pLeft) ){
99283 codeVectorCompare(pParse, pExpr, target, op, p5);
99284 }else{
99285 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
99286 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
99287 codeCompare(pParse, pLeft, pExpr->pRight, op,
99288 r1, r2, inReg, SQLITE_STOREP2 | p5);
99289 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
99290 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
99291 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
99292 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
99293 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
99294 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
99295 testcase( regFree1==0 );
99296 testcase( regFree2==0 );
99297 }
99298 break;
99299 }
99300 case TK_AND:
99301 case TK_OR:
99302 case TK_PLUS:
99303 case TK_STAR:
99304 case TK_MINUS:
99305 case TK_REM:
99306 case TK_BITAND:
99307 case TK_BITOR:
99308 case TK_SLASH:
99309 case TK_LSHIFT:
99310 case TK_RSHIFT:
99311 case TK_CONCAT: {
99312 assert( TK_AND==OP_And ); testcase( op==TK_AND );
99313 assert( TK_OR==OP_Or ); testcase( op==TK_OR );
99314 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS );
99315 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS );
99316 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM );
99317 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND );
99318 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR );
99319 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH );
99320 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT );
99321 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT );
99322 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT );
99323 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
99324 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
99325 sqlite3VdbeAddOp3(v, op, r2, r1, target);
99326 testcase( regFree1==0 );
99327 testcase( regFree2==0 );
99328 break;
99329 }
99330 case TK_UMINUS: {
99331 Expr *pLeft = pExpr->pLeft;
99332 assert( pLeft );
99333 if( pLeft->op==TK_INTEGER ){
99334 codeInteger(pParse, pLeft, 1, target);
99335 return target;
99336#ifndef SQLITE_OMIT_FLOATING_POINT
99337 }else if( pLeft->op==TK_FLOAT ){
99338 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99339 codeReal(v, pLeft->u.zToken, 1, target);
99340 return target;
99341#endif
99342 }else{
99343 tempX.op = TK_INTEGER;
99344 tempX.flags = EP_IntValue|EP_TokenOnly;
99345 tempX.u.iValue = 0;
99346 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
99347 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
99348 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
99349 testcase( regFree2==0 );
99350 }
99351 break;
99352 }
99353 case TK_BITNOT:
99354 case TK_NOT: {
99355 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT );
99356 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
99357 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
99358 testcase( regFree1==0 );
99359 sqlite3VdbeAddOp2(v, op, r1, inReg);
99360 break;
99361 }
99362 case TK_TRUTH: {
99363 int isTrue; /* IS TRUE or IS NOT TRUE */
99364 int bNormal; /* IS TRUE or IS FALSE */
99365 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
99366 testcase( regFree1==0 );
99367 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
99368 bNormal = pExpr->op2==TK_IS;
99369 testcase( isTrue && bNormal);
99370 testcase( !isTrue && bNormal);
99371 sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
99372 break;
99373 }
99374 case TK_ISNULL:
99375 case TK_NOTNULL: {
99376 int addr;
99377 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
99378 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
99379 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
99380 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
99381 testcase( regFree1==0 );
99382 addr = sqlite3VdbeAddOp1(v, op, r1);
99383 VdbeCoverageIf(v, op==TK_ISNULL);
99384 VdbeCoverageIf(v, op==TK_NOTNULL);
99385 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
99386 sqlite3VdbeJumpHere(v, addr);
99387 break;
99388 }
99389 case TK_AGG_FUNCTION: {
99390 AggInfo *pInfo = pExpr->pAggInfo;
99391 if( pInfo==0 ){
99392 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99393 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
99394 }else{
99395 return pInfo->aFunc[pExpr->iAgg].iMem;
99396 }
99397 break;
99398 }
99399 case TK_FUNCTION: {
99400 ExprList *pFarg; /* List of function arguments */
99401 int nFarg; /* Number of function arguments */
99402 FuncDef *pDef; /* The function definition object */
99403 const char *zId; /* The function name */
99404 u32 constMask = 0; /* Mask of function arguments that are constant */
99405 int i; /* Loop counter */
99406 sqlite3 *db = pParse->db; /* The database connection */
99407 u8 enc = ENC(db); /* The text encoding used by this database */
99408 CollSeq *pColl = 0; /* A collating sequence */
99409
99410#ifndef SQLITE_OMIT_WINDOWFUNC
99411 if( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) && pExpr->pWin ){
99412 return pExpr->pWin->regResult;
99413 }
99414#endif
99415
99416 if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
99417 /* SQL functions can be expensive. So try to move constant functions
99418 ** out of the inner loop, even if that means an extra OP_Copy. */
99419 return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
99420 }
99421 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
99422 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
99423 pFarg = 0;
99424 }else{
99425 pFarg = pExpr->x.pList;
99426 }
99427 nFarg = pFarg ? pFarg->nExpr : 0;
99428 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99429 zId = pExpr->u.zToken;
99430 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
99431#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
99432 if( pDef==0 && pParse->explain ){
99433 pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
99434 }
99435#endif
99436 if( pDef==0 || pDef->xFinalize!=0 ){
99437 sqlite3ErrorMsg(pParse, "unknown function: %s()", zId);
99438 break;
99439 }
99440
99441 /* Attempt a direct implementation of the built-in COALESCE() and
99442 ** IFNULL() functions. This avoids unnecessary evaluation of
99443 ** arguments past the first non-NULL argument.
99444 */
99445 if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
99446 int endCoalesce = sqlite3VdbeMakeLabel(v);
99447 assert( nFarg>=2 );
99448 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
99449 for(i=1; i<nFarg; i++){
99450 sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
99451 VdbeCoverage(v);
99452 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
99453 }
99454 sqlite3VdbeResolveLabel(v, endCoalesce);
99455 break;
99456 }
99457
99458 /* The UNLIKELY() function is a no-op. The result is the value
99459 ** of the first argument.
99460 */
99461 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
99462 assert( nFarg>=1 );
99463 return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
99464 }
99465
99466#ifdef SQLITE_DEBUG
99467 /* The AFFINITY() function evaluates to a string that describes
99468 ** the type affinity of the argument. This is used for testing of
99469 ** the SQLite type logic.
99470 */
99471 if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){
99472 const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
99473 char aff;
99474 assert( nFarg==1 );
99475 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
99476 sqlite3VdbeLoadString(v, target,
99477 aff ? azAff[aff-SQLITE_AFF_BLOB] : "none");
99478 return target;
99479 }
99480#endif
99481
99482 for(i=0; i<nFarg; i++){
99483 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
99484 testcase( i==31 );
99485 constMask |= MASKBIT32(i);
99486 }
99487 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
99488 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
99489 }
99490 }
99491 if( pFarg ){
99492 if( constMask ){
99493 r1 = pParse->nMem+1;
99494 pParse->nMem += nFarg;
99495 }else{
99496 r1 = sqlite3GetTempRange(pParse, nFarg);
99497 }
99498
99499 /* For length() and typeof() functions with a column argument,
99500 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
99501 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
99502 ** loading.
99503 */
99504 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
99505 u8 exprOp;
99506 assert( nFarg==1 );
99507 assert( pFarg->a[0].pExpr!=0 );
99508 exprOp = pFarg->a[0].pExpr->op;
99509 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
99510 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
99511 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
99512 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
99513 pFarg->a[0].pExpr->op2 =
99514 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
99515 }
99516 }
99517
99518 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
99519 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
99520 }else{
99521 r1 = 0;
99522 }
99523#ifndef SQLITE_OMIT_VIRTUALTABLE
99524 /* Possibly overload the function if the first argument is
99525 ** a virtual table column.
99526 **
99527 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
99528 ** second argument, not the first, as the argument to test to
99529 ** see if it is a column in a virtual table. This is done because
99530 ** the left operand of infix functions (the operand we want to
99531 ** control overloading) ends up as the second argument to the
99532 ** function. The expression "A glob B" is equivalent to
99533 ** "glob(B,A). We want to use the A in "A glob B" to test
99534 ** for function overloading. But we use the B term in "glob(B,A)".
99535 */
99536 if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc) ){
99537 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
99538 }else if( nFarg>0 ){
99539 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
99540 }
99541#endif
99542 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
99543 if( !pColl ) pColl = db->pDfltColl;
99544 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
99545 }
99546#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
99547 if( pDef->funcFlags & SQLITE_FUNC_OFFSET ){
99548 Expr *pArg = pFarg->a[0].pExpr;
99549 if( pArg->op==TK_COLUMN ){
99550 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
99551 }else{
99552 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
99553 }
99554 }else
99555#endif
99556 {
99557 sqlite3VdbeAddOp4(v, pParse->iSelfTab ? OP_PureFunc0 : OP_Function0,
99558 constMask, r1, target, (char*)pDef, P4_FUNCDEF);
99559 sqlite3VdbeChangeP5(v, (u8)nFarg);
99560 }
99561 if( nFarg && constMask==0 ){
99562 sqlite3ReleaseTempRange(pParse, r1, nFarg);
99563 }
99564 return target;
99565 }
99566#ifndef SQLITE_OMIT_SUBQUERY
99567 case TK_EXISTS:
99568 case TK_SELECT: {
99569 int nCol;
99570 testcase( op==TK_EXISTS );
99571 testcase( op==TK_SELECT );
99572 if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){
99573 sqlite3SubselectError(pParse, nCol, 1);
99574 }else{
99575 return sqlite3CodeSubselect(pParse, pExpr, 0, 0);
99576 }
99577 break;
99578 }
99579 case TK_SELECT_COLUMN: {
99580 int n;
99581 if( pExpr->pLeft->iTable==0 ){
99582 pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft, 0, 0);
99583 }
99584 assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT );
99585 if( pExpr->iTable
99586 && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft))
99587 ){
99588 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
99589 pExpr->iTable, n);
99590 }
99591 return pExpr->pLeft->iTable + pExpr->iColumn;
99592 }
99593 case TK_IN: {
99594 int destIfFalse = sqlite3VdbeMakeLabel(v);
99595 int destIfNull = sqlite3VdbeMakeLabel(v);
99596 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
99597 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
99598 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
99599 sqlite3VdbeResolveLabel(v, destIfFalse);
99600 sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
99601 sqlite3VdbeResolveLabel(v, destIfNull);
99602 return target;
99603 }
99604#endif /* SQLITE_OMIT_SUBQUERY */
99605
99606
99607 /*
99608 ** x BETWEEN y AND z
99609 **
99610 ** This is equivalent to
99611 **
99612 ** x>=y AND x<=z
99613 **
99614 ** X is stored in pExpr->pLeft.
99615 ** Y is stored in pExpr->pList->a[0].pExpr.
99616 ** Z is stored in pExpr->pList->a[1].pExpr.
99617 */
99618 case TK_BETWEEN: {
99619 exprCodeBetween(pParse, pExpr, target, 0, 0);
99620 return target;
99621 }
99622 case TK_SPAN:
99623 case TK_COLLATE:
99624 case TK_UPLUS: {
99625 pExpr = pExpr->pLeft;
99626 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
99627 }
99628
99629 case TK_TRIGGER: {
99630 /* If the opcode is TK_TRIGGER, then the expression is a reference
99631 ** to a column in the new.* or old.* pseudo-tables available to
99632 ** trigger programs. In this case Expr.iTable is set to 1 for the
99633 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
99634 ** is set to the column of the pseudo-table to read, or to -1 to
99635 ** read the rowid field.
99636 **
99637 ** The expression is implemented using an OP_Param opcode. The p1
99638 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
99639 ** to reference another column of the old.* pseudo-table, where
99640 ** i is the index of the column. For a new.rowid reference, p1 is
99641 ** set to (n+1), where n is the number of columns in each pseudo-table.
99642 ** For a reference to any other column in the new.* pseudo-table, p1
99643 ** is set to (n+2+i), where n and i are as defined previously. For
99644 ** example, if the table on which triggers are being fired is
99645 ** declared as:
99646 **
99647 ** CREATE TABLE t1(a, b);
99648 **
99649 ** Then p1 is interpreted as follows:
99650 **
99651 ** p1==0 -> old.rowid p1==3 -> new.rowid
99652 ** p1==1 -> old.a p1==4 -> new.a
99653 ** p1==2 -> old.b p1==5 -> new.b
99654 */
99655 Table *pTab = pExpr->pTab;
99656 int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
99657
99658 assert( pExpr->iTable==0 || pExpr->iTable==1 );
99659 assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
99660 assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
99661 assert( p1>=0 && p1<(pTab->nCol*2+2) );
99662
99663 sqlite3VdbeAddOp2(v, OP_Param, p1, target);
99664 VdbeComment((v, "r[%d]=%s.%s", target,
99665 (pExpr->iTable ? "new" : "old"),
99666 (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName)
99667 ));
99668
99669#ifndef SQLITE_OMIT_FLOATING_POINT
99670 /* If the column has REAL affinity, it may currently be stored as an
99671 ** integer. Use OP_RealAffinity to make sure it is really real.
99672 **
99673 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
99674 ** floating point when extracting it from the record. */
99675 if( pExpr->iColumn>=0
99676 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
99677 ){
99678 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
99679 }
99680#endif
99681 break;
99682 }
99683
99684 case TK_VECTOR: {
99685 sqlite3ErrorMsg(pParse, "row value misused");
99686 break;
99687 }
99688
99689 case TK_IF_NULL_ROW: {
99690 int addrINR;
99691 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
99692 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
99693 sqlite3VdbeJumpHere(v, addrINR);
99694 sqlite3VdbeChangeP3(v, addrINR, inReg);
99695 break;
99696 }
99697
99698 /*
99699 ** Form A:
99700 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
99701 **
99702 ** Form B:
99703 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
99704 **
99705 ** Form A is can be transformed into the equivalent form B as follows:
99706 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
99707 ** WHEN x=eN THEN rN ELSE y END
99708 **
99709 ** X (if it exists) is in pExpr->pLeft.
99710 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
99711 ** odd. The Y is also optional. If the number of elements in x.pList
99712 ** is even, then Y is omitted and the "otherwise" result is NULL.
99713 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
99714 **
99715 ** The result of the expression is the Ri for the first matching Ei,
99716 ** or if there is no matching Ei, the ELSE term Y, or if there is
99717 ** no ELSE term, NULL.
99718 */
99719 default: assert( op==TK_CASE ); {
99720 int endLabel; /* GOTO label for end of CASE stmt */
99721 int nextCase; /* GOTO label for next WHEN clause */
99722 int nExpr; /* 2x number of WHEN terms */
99723 int i; /* Loop counter */
99724 ExprList *pEList; /* List of WHEN terms */
99725 struct ExprList_item *aListelem; /* Array of WHEN terms */
99726 Expr opCompare; /* The X==Ei expression */
99727 Expr *pX; /* The X expression */
99728 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
99729
99730 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
99731 assert(pExpr->x.pList->nExpr > 0);
99732 pEList = pExpr->x.pList;
99733 aListelem = pEList->a;
99734 nExpr = pEList->nExpr;
99735 endLabel = sqlite3VdbeMakeLabel(v);
99736 if( (pX = pExpr->pLeft)!=0 ){
99737 tempX = *pX;
99738 testcase( pX->op==TK_COLUMN );
99739 exprToRegister(&tempX, exprCodeVector(pParse, &tempX, &regFree1));
99740 testcase( regFree1==0 );
99741 memset(&opCompare, 0, sizeof(opCompare));
99742 opCompare.op = TK_EQ;
99743 opCompare.pLeft = &tempX;
99744 pTest = &opCompare;
99745 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
99746 ** The value in regFree1 might get SCopy-ed into the file result.
99747 ** So make sure that the regFree1 register is not reused for other
99748 ** purposes and possibly overwritten. */
99749 regFree1 = 0;
99750 }
99751 for(i=0; i<nExpr-1; i=i+2){
99752 if( pX ){
99753 assert( pTest!=0 );
99754 opCompare.pRight = aListelem[i].pExpr;
99755 }else{
99756 pTest = aListelem[i].pExpr;
99757 }
99758 nextCase = sqlite3VdbeMakeLabel(v);
99759 testcase( pTest->op==TK_COLUMN );
99760 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
99761 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
99762 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
99763 sqlite3VdbeGoto(v, endLabel);
99764 sqlite3VdbeResolveLabel(v, nextCase);
99765 }
99766 if( (nExpr&1)!=0 ){
99767 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
99768 }else{
99769 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
99770 }
99771 sqlite3VdbeResolveLabel(v, endLabel);
99772 break;
99773 }
99774#ifndef SQLITE_OMIT_TRIGGER
99775 case TK_RAISE: {
99776 assert( pExpr->affinity==OE_Rollback
99777 || pExpr->affinity==OE_Abort
99778 || pExpr->affinity==OE_Fail
99779 || pExpr->affinity==OE_Ignore
99780 );
99781 if( !pParse->pTriggerTab ){
99782 sqlite3ErrorMsg(pParse,
99783 "RAISE() may only be used within a trigger-program");
99784 return 0;
99785 }
99786 if( pExpr->affinity==OE_Abort ){
99787 sqlite3MayAbort(pParse);
99788 }
99789 assert( !ExprHasProperty(pExpr, EP_IntValue) );
99790 if( pExpr->affinity==OE_Ignore ){
99791 sqlite3VdbeAddOp4(
99792 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
99793 VdbeCoverage(v);
99794 }else{
99795 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
99796 pExpr->affinity, pExpr->u.zToken, 0, 0);
99797 }
99798
99799 break;
99800 }
99801#endif
99802 }
99803 sqlite3ReleaseTempReg(pParse, regFree1);
99804 sqlite3ReleaseTempReg(pParse, regFree2);
99805 return inReg;
99806}
99807
99808/*
99809** Factor out the code of the given expression to initialization time.
99810**
99811** If regDest>=0 then the result is always stored in that register and the
99812** result is not reusable. If regDest<0 then this routine is free to
99813** store the value whereever it wants. The register where the expression
99814** is stored is returned. When regDest<0, two identical expressions will
99815** code to the same register.
99816*/
99817SQLITE_PRIVATE int sqlite3ExprCodeAtInit(
99818 Parse *pParse, /* Parsing context */
99819 Expr *pExpr, /* The expression to code when the VDBE initializes */
99820 int regDest /* Store the value in this register */
99821){
99822 ExprList *p;
99823 assert( ConstFactorOk(pParse) );
99824 p = pParse->pConstExpr;
99825 if( regDest<0 && p ){
99826 struct ExprList_item *pItem;
99827 int i;
99828 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
99829 if( pItem->reusable && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 ){
99830 return pItem->u.iConstExprReg;
99831 }
99832 }
99833 }
99834 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
99835 p = sqlite3ExprListAppend(pParse, p, pExpr);
99836 if( p ){
99837 struct ExprList_item *pItem = &p->a[p->nExpr-1];
99838 pItem->reusable = regDest<0;
99839 if( regDest<0 ) regDest = ++pParse->nMem;
99840 pItem->u.iConstExprReg = regDest;
99841 }
99842 pParse->pConstExpr = p;
99843 return regDest;
99844}
99845
99846/*
99847** Generate code to evaluate an expression and store the results
99848** into a register. Return the register number where the results
99849** are stored.
99850**
99851** If the register is a temporary register that can be deallocated,
99852** then write its number into *pReg. If the result register is not
99853** a temporary, then set *pReg to zero.
99854**
99855** If pExpr is a constant, then this routine might generate this
99856** code to fill the register in the initialization section of the
99857** VDBE program, in order to factor it out of the evaluation loop.
99858*/
99859SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
99860 int r2;
99861 pExpr = sqlite3ExprSkipCollate(pExpr);
99862 if( ConstFactorOk(pParse)
99863 && pExpr->op!=TK_REGISTER
99864 && sqlite3ExprIsConstantNotJoin(pExpr)
99865 ){
99866 *pReg = 0;
99867 r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
99868 }else{
99869 int r1 = sqlite3GetTempReg(pParse);
99870 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
99871 if( r2==r1 ){
99872 *pReg = r1;
99873 }else{
99874 sqlite3ReleaseTempReg(pParse, r1);
99875 *pReg = 0;
99876 }
99877 }
99878 return r2;
99879}
99880
99881/*
99882** Generate code that will evaluate expression pExpr and store the
99883** results in register target. The results are guaranteed to appear
99884** in register target.
99885*/
99886SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
99887 int inReg;
99888
99889 assert( target>0 && target<=pParse->nMem );
99890 if( pExpr && pExpr->op==TK_REGISTER ){
99891 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
99892 }else{
99893 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
99894 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
99895 if( inReg!=target && pParse->pVdbe ){
99896 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
99897 }
99898 }
99899}
99900
99901/*
99902** Make a transient copy of expression pExpr and then code it using
99903** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
99904** except that the input expression is guaranteed to be unchanged.
99905*/
99906SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
99907 sqlite3 *db = pParse->db;
99908 pExpr = sqlite3ExprDup(db, pExpr, 0);
99909 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
99910 sqlite3ExprDelete(db, pExpr);
99911}
99912
99913/*
99914** Generate code that will evaluate expression pExpr and store the
99915** results in register target. The results are guaranteed to appear
99916** in register target. If the expression is constant, then this routine
99917** might choose to code the expression at initialization time.
99918*/
99919SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
99920 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
99921 sqlite3ExprCodeAtInit(pParse, pExpr, target);
99922 }else{
99923 sqlite3ExprCode(pParse, pExpr, target);
99924 }
99925}
99926
99927/*
99928** Generate code that evaluates the given expression and puts the result
99929** in register target.
99930**
99931** Also make a copy of the expression results into another "cache" register
99932** and modify the expression so that the next time it is evaluated,
99933** the result is a copy of the cache register.
99934**
99935** This routine is used for expressions that are used multiple
99936** times. They are evaluated once and the results of the expression
99937** are reused.
99938*/
99939SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
99940 Vdbe *v = pParse->pVdbe;
99941 int iMem;
99942
99943 assert( target>0 );
99944 assert( pExpr->op!=TK_REGISTER );
99945 sqlite3ExprCode(pParse, pExpr, target);
99946 iMem = ++pParse->nMem;
99947 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
99948 exprToRegister(pExpr, iMem);
99949}
99950
99951/*
99952** Generate code that pushes the value of every element of the given
99953** expression list into a sequence of registers beginning at target.
99954**
99955** Return the number of elements evaluated. The number returned will
99956** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
99957** is defined.
99958**
99959** The SQLITE_ECEL_DUP flag prevents the arguments from being
99960** filled using OP_SCopy. OP_Copy must be used instead.
99961**
99962** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
99963** factored out into initialization code.
99964**
99965** The SQLITE_ECEL_REF flag means that expressions in the list with
99966** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
99967** in registers at srcReg, and so the value can be copied from there.
99968** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
99969** are simply omitted rather than being copied from srcReg.
99970*/
99971SQLITE_PRIVATE int sqlite3ExprCodeExprList(
99972 Parse *pParse, /* Parsing context */
99973 ExprList *pList, /* The expression list to be coded */
99974 int target, /* Where to write results */
99975 int srcReg, /* Source registers if SQLITE_ECEL_REF */
99976 u8 flags /* SQLITE_ECEL_* flags */
99977){
99978 struct ExprList_item *pItem;
99979 int i, j, n;
99980 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
99981 Vdbe *v = pParse->pVdbe;
99982 assert( pList!=0 );
99983 assert( target>0 );
99984 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
99985 n = pList->nExpr;
99986 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
99987 for(pItem=pList->a, i=0; i<n; i++, pItem++){
99988 Expr *pExpr = pItem->pExpr;
99989#ifdef SQLITE_ENABLE_SORTER_REFERENCES
99990 if( pItem->bSorterRef ){
99991 i--;
99992 n--;
99993 }else
99994#endif
99995 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
99996 if( flags & SQLITE_ECEL_OMITREF ){
99997 i--;
99998 n--;
99999 }else{
100000 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
100001 }
100002 }else if( (flags & SQLITE_ECEL_FACTOR)!=0
100003 && sqlite3ExprIsConstantNotJoin(pExpr)
100004 ){
100005 sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
100006 }else{
100007 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
100008 if( inReg!=target+i ){
100009 VdbeOp *pOp;
100010 if( copyOp==OP_Copy
100011 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
100012 && pOp->p1+pOp->p3+1==inReg
100013 && pOp->p2+pOp->p3+1==target+i
100014 ){
100015 pOp->p3++;
100016 }else{
100017 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
100018 }
100019 }
100020 }
100021 }
100022 return n;
100023}
100024
100025/*
100026** Generate code for a BETWEEN operator.
100027**
100028** x BETWEEN y AND z
100029**
100030** The above is equivalent to
100031**
100032** x>=y AND x<=z
100033**
100034** Code it as such, taking care to do the common subexpression
100035** elimination of x.
100036**
100037** The xJumpIf parameter determines details:
100038**
100039** NULL: Store the boolean result in reg[dest]
100040** sqlite3ExprIfTrue: Jump to dest if true
100041** sqlite3ExprIfFalse: Jump to dest if false
100042**
100043** The jumpIfNull parameter is ignored if xJumpIf is NULL.
100044*/
100045static void exprCodeBetween(
100046 Parse *pParse, /* Parsing and code generating context */
100047 Expr *pExpr, /* The BETWEEN expression */
100048 int dest, /* Jump destination or storage location */
100049 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
100050 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
100051){
100052 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
100053 Expr compLeft; /* The x>=y term */
100054 Expr compRight; /* The x<=z term */
100055 Expr exprX; /* The x subexpression */
100056 int regFree1 = 0; /* Temporary use register */
100057
100058
100059 memset(&compLeft, 0, sizeof(Expr));
100060 memset(&compRight, 0, sizeof(Expr));
100061 memset(&exprAnd, 0, sizeof(Expr));
100062
100063 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
100064 exprX = *pExpr->pLeft;
100065 exprAnd.op = TK_AND;
100066 exprAnd.pLeft = &compLeft;
100067 exprAnd.pRight = &compRight;
100068 compLeft.op = TK_GE;
100069 compLeft.pLeft = &exprX;
100070 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
100071 compRight.op = TK_LE;
100072 compRight.pLeft = &exprX;
100073 compRight.pRight = pExpr->x.pList->a[1].pExpr;
100074 exprToRegister(&exprX, exprCodeVector(pParse, &exprX, &regFree1));
100075 if( xJump ){
100076 xJump(pParse, &exprAnd, dest, jumpIfNull);
100077 }else{
100078 /* Mark the expression is being from the ON or USING clause of a join
100079 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
100080 ** it into the Parse.pConstExpr list. We should use a new bit for this,
100081 ** for clarity, but we are out of bits in the Expr.flags field so we
100082 ** have to reuse the EP_FromJoin bit. Bummer. */
100083 exprX.flags |= EP_FromJoin;
100084 sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
100085 }
100086 sqlite3ReleaseTempReg(pParse, regFree1);
100087
100088 /* Ensure adequate test coverage */
100089 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 );
100090 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 );
100091 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 );
100092 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 );
100093 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
100094 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
100095 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
100096 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
100097 testcase( xJump==0 );
100098}
100099
100100/*
100101** Generate code for a boolean expression such that a jump is made
100102** to the label "dest" if the expression is true but execution
100103** continues straight thru if the expression is false.
100104**
100105** If the expression evaluates to NULL (neither true nor false), then
100106** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
100107**
100108** This code depends on the fact that certain token values (ex: TK_EQ)
100109** are the same as opcode values (ex: OP_Eq) that implement the corresponding
100110** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
100111** the make process cause these values to align. Assert()s in the code
100112** below verify that the numbers are aligned correctly.
100113*/
100114SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
100115 Vdbe *v = pParse->pVdbe;
100116 int op = 0;
100117 int regFree1 = 0;
100118 int regFree2 = 0;
100119 int r1, r2;
100120
100121 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
100122 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
100123 if( NEVER(pExpr==0) ) return; /* No way this can happen */
100124 op = pExpr->op;
100125 switch( op ){
100126 case TK_AND: {
100127 int d2 = sqlite3VdbeMakeLabel(v);
100128 testcase( jumpIfNull==0 );
100129 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
100130 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
100131 sqlite3VdbeResolveLabel(v, d2);
100132 break;
100133 }
100134 case TK_OR: {
100135 testcase( jumpIfNull==0 );
100136 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
100137 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
100138 break;
100139 }
100140 case TK_NOT: {
100141 testcase( jumpIfNull==0 );
100142 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
100143 break;
100144 }
100145 case TK_TRUTH: {
100146 int isNot; /* IS NOT TRUE or IS NOT FALSE */
100147 int isTrue; /* IS TRUE or IS NOT TRUE */
100148 testcase( jumpIfNull==0 );
100149 isNot = pExpr->op2==TK_ISNOT;
100150 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
100151 testcase( isTrue && isNot );
100152 testcase( !isTrue && isNot );
100153 if( isTrue ^ isNot ){
100154 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
100155 isNot ? SQLITE_JUMPIFNULL : 0);
100156 }else{
100157 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
100158 isNot ? SQLITE_JUMPIFNULL : 0);
100159 }
100160 break;
100161 }
100162 case TK_IS:
100163 case TK_ISNOT:
100164 testcase( op==TK_IS );
100165 testcase( op==TK_ISNOT );
100166 op = (op==TK_IS) ? TK_EQ : TK_NE;
100167 jumpIfNull = SQLITE_NULLEQ;
100168 /* Fall thru */
100169 case TK_LT:
100170 case TK_LE:
100171 case TK_GT:
100172 case TK_GE:
100173 case TK_NE:
100174 case TK_EQ: {
100175 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
100176 testcase( jumpIfNull==0 );
100177 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
100178 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
100179 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
100180 r1, r2, dest, jumpIfNull);
100181 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
100182 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
100183 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
100184 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
100185 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
100186 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
100187 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
100188 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
100189 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
100190 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
100191 testcase( regFree1==0 );
100192 testcase( regFree2==0 );
100193 break;
100194 }
100195 case TK_ISNULL:
100196 case TK_NOTNULL: {
100197 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
100198 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
100199 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
100200 sqlite3VdbeAddOp2(v, op, r1, dest);
100201 VdbeCoverageIf(v, op==TK_ISNULL);
100202 VdbeCoverageIf(v, op==TK_NOTNULL);
100203 testcase( regFree1==0 );
100204 break;
100205 }
100206 case TK_BETWEEN: {
100207 testcase( jumpIfNull==0 );
100208 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
100209 break;
100210 }
100211#ifndef SQLITE_OMIT_SUBQUERY
100212 case TK_IN: {
100213 int destIfFalse = sqlite3VdbeMakeLabel(v);
100214 int destIfNull = jumpIfNull ? dest : destIfFalse;
100215 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
100216 sqlite3VdbeGoto(v, dest);
100217 sqlite3VdbeResolveLabel(v, destIfFalse);
100218 break;
100219 }
100220#endif
100221 default: {
100222 default_expr:
100223 if( exprAlwaysTrue(pExpr) ){
100224 sqlite3VdbeGoto(v, dest);
100225 }else if( exprAlwaysFalse(pExpr) ){
100226 /* No-op */
100227 }else{
100228 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
100229 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
100230 VdbeCoverage(v);
100231 testcase( regFree1==0 );
100232 testcase( jumpIfNull==0 );
100233 }
100234 break;
100235 }
100236 }
100237 sqlite3ReleaseTempReg(pParse, regFree1);
100238 sqlite3ReleaseTempReg(pParse, regFree2);
100239}
100240
100241/*
100242** Generate code for a boolean expression such that a jump is made
100243** to the label "dest" if the expression is false but execution
100244** continues straight thru if the expression is true.
100245**
100246** If the expression evaluates to NULL (neither true nor false) then
100247** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
100248** is 0.
100249*/
100250SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
100251 Vdbe *v = pParse->pVdbe;
100252 int op = 0;
100253 int regFree1 = 0;
100254 int regFree2 = 0;
100255 int r1, r2;
100256
100257 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
100258 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
100259 if( pExpr==0 ) return;
100260
100261 /* The value of pExpr->op and op are related as follows:
100262 **
100263 ** pExpr->op op
100264 ** --------- ----------
100265 ** TK_ISNULL OP_NotNull
100266 ** TK_NOTNULL OP_IsNull
100267 ** TK_NE OP_Eq
100268 ** TK_EQ OP_Ne
100269 ** TK_GT OP_Le
100270 ** TK_LE OP_Gt
100271 ** TK_GE OP_Lt
100272 ** TK_LT OP_Ge
100273 **
100274 ** For other values of pExpr->op, op is undefined and unused.
100275 ** The value of TK_ and OP_ constants are arranged such that we
100276 ** can compute the mapping above using the following expression.
100277 ** Assert()s verify that the computation is correct.
100278 */
100279 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
100280
100281 /* Verify correct alignment of TK_ and OP_ constants
100282 */
100283 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
100284 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
100285 assert( pExpr->op!=TK_NE || op==OP_Eq );
100286 assert( pExpr->op!=TK_EQ || op==OP_Ne );
100287 assert( pExpr->op!=TK_LT || op==OP_Ge );
100288 assert( pExpr->op!=TK_LE || op==OP_Gt );
100289 assert( pExpr->op!=TK_GT || op==OP_Le );
100290 assert( pExpr->op!=TK_GE || op==OP_Lt );
100291
100292 switch( pExpr->op ){
100293 case TK_AND: {
100294 testcase( jumpIfNull==0 );
100295 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
100296 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
100297 break;
100298 }
100299 case TK_OR: {
100300 int d2 = sqlite3VdbeMakeLabel(v);
100301 testcase( jumpIfNull==0 );
100302 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
100303 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
100304 sqlite3VdbeResolveLabel(v, d2);
100305 break;
100306 }
100307 case TK_NOT: {
100308 testcase( jumpIfNull==0 );
100309 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
100310 break;
100311 }
100312 case TK_TRUTH: {
100313 int isNot; /* IS NOT TRUE or IS NOT FALSE */
100314 int isTrue; /* IS TRUE or IS NOT TRUE */
100315 testcase( jumpIfNull==0 );
100316 isNot = pExpr->op2==TK_ISNOT;
100317 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
100318 testcase( isTrue && isNot );
100319 testcase( !isTrue && isNot );
100320 if( isTrue ^ isNot ){
100321 /* IS TRUE and IS NOT FALSE */
100322 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
100323 isNot ? 0 : SQLITE_JUMPIFNULL);
100324
100325 }else{
100326 /* IS FALSE and IS NOT TRUE */
100327 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
100328 isNot ? 0 : SQLITE_JUMPIFNULL);
100329 }
100330 break;
100331 }
100332 case TK_IS:
100333 case TK_ISNOT:
100334 testcase( pExpr->op==TK_IS );
100335 testcase( pExpr->op==TK_ISNOT );
100336 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
100337 jumpIfNull = SQLITE_NULLEQ;
100338 /* Fall thru */
100339 case TK_LT:
100340 case TK_LE:
100341 case TK_GT:
100342 case TK_GE:
100343 case TK_NE:
100344 case TK_EQ: {
100345 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
100346 testcase( jumpIfNull==0 );
100347 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
100348 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
100349 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
100350 r1, r2, dest, jumpIfNull);
100351 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
100352 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
100353 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
100354 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
100355 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
100356 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
100357 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
100358 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
100359 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
100360 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
100361 testcase( regFree1==0 );
100362 testcase( regFree2==0 );
100363 break;
100364 }
100365 case TK_ISNULL:
100366 case TK_NOTNULL: {
100367 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
100368 sqlite3VdbeAddOp2(v, op, r1, dest);
100369 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
100370 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
100371 testcase( regFree1==0 );
100372 break;
100373 }
100374 case TK_BETWEEN: {
100375 testcase( jumpIfNull==0 );
100376 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
100377 break;
100378 }
100379#ifndef SQLITE_OMIT_SUBQUERY
100380 case TK_IN: {
100381 if( jumpIfNull ){
100382 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
100383 }else{
100384 int destIfNull = sqlite3VdbeMakeLabel(v);
100385 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
100386 sqlite3VdbeResolveLabel(v, destIfNull);
100387 }
100388 break;
100389 }
100390#endif
100391 default: {
100392 default_expr:
100393 if( exprAlwaysFalse(pExpr) ){
100394 sqlite3VdbeGoto(v, dest);
100395 }else if( exprAlwaysTrue(pExpr) ){
100396 /* no-op */
100397 }else{
100398 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
100399 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
100400 VdbeCoverage(v);
100401 testcase( regFree1==0 );
100402 testcase( jumpIfNull==0 );
100403 }
100404 break;
100405 }
100406 }
100407 sqlite3ReleaseTempReg(pParse, regFree1);
100408 sqlite3ReleaseTempReg(pParse, regFree2);
100409}
100410
100411/*
100412** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
100413** code generation, and that copy is deleted after code generation. This
100414** ensures that the original pExpr is unchanged.
100415*/
100416SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
100417 sqlite3 *db = pParse->db;
100418 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
100419 if( db->mallocFailed==0 ){
100420 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
100421 }
100422 sqlite3ExprDelete(db, pCopy);
100423}
100424
100425/*
100426** Expression pVar is guaranteed to be an SQL variable. pExpr may be any
100427** type of expression.
100428**
100429** If pExpr is a simple SQL value - an integer, real, string, blob
100430** or NULL value - then the VDBE currently being prepared is configured
100431** to re-prepare each time a new value is bound to variable pVar.
100432**
100433** Additionally, if pExpr is a simple SQL value and the value is the
100434** same as that currently bound to variable pVar, non-zero is returned.
100435** Otherwise, if the values are not the same or if pExpr is not a simple
100436** SQL value, zero is returned.
100437*/
100438static int exprCompareVariable(Parse *pParse, Expr *pVar, Expr *pExpr){
100439 int res = 0;
100440 int iVar;
100441 sqlite3_value *pL, *pR = 0;
100442
100443 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
100444 if( pR ){
100445 iVar = pVar->iColumn;
100446 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
100447 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
100448 if( pL ){
100449 if( sqlite3_value_type(pL)==SQLITE_TEXT ){
100450 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
100451 }
100452 res = 0==sqlite3MemCompare(pL, pR, 0);
100453 }
100454 sqlite3ValueFree(pR);
100455 sqlite3ValueFree(pL);
100456 }
100457
100458 return res;
100459}
100460
100461/*
100462** Do a deep comparison of two expression trees. Return 0 if the two
100463** expressions are completely identical. Return 1 if they differ only
100464** by a COLLATE operator at the top level. Return 2 if there are differences
100465** other than the top-level COLLATE operator.
100466**
100467** If any subelement of pB has Expr.iTable==(-1) then it is allowed
100468** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
100469**
100470** The pA side might be using TK_REGISTER. If that is the case and pB is
100471** not using TK_REGISTER but is otherwise equivalent, then still return 0.
100472**
100473** Sometimes this routine will return 2 even if the two expressions
100474** really are equivalent. If we cannot prove that the expressions are
100475** identical, we return 2 just to be safe. So if this routine
100476** returns 2, then you do not really know for certain if the two
100477** expressions are the same. But if you get a 0 or 1 return, then you
100478** can be sure the expressions are the same. In the places where
100479** this routine is used, it does not hurt to get an extra 2 - that
100480** just might result in some slightly slower code. But returning
100481** an incorrect 0 or 1 could lead to a malfunction.
100482**
100483** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in
100484** pParse->pReprepare can be matched against literals in pB. The
100485** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
100486** If pParse is NULL (the normal case) then any TK_VARIABLE term in
100487** Argument pParse should normally be NULL. If it is not NULL and pA or
100488** pB causes a return value of 2.
100489*/
100490SQLITE_PRIVATE int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){
100491 u32 combinedFlags;
100492 if( pA==0 || pB==0 ){
100493 return pB==pA ? 0 : 2;
100494 }
100495 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
100496 return 0;
100497 }
100498 combinedFlags = pA->flags | pB->flags;
100499 if( combinedFlags & EP_IntValue ){
100500 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
100501 return 0;
100502 }
100503 return 2;
100504 }
100505 if( pA->op!=pB->op ){
100506 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
100507 return 1;
100508 }
100509 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
100510 return 1;
100511 }
100512 return 2;
100513 }
100514 if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
100515 if( pA->op==TK_FUNCTION ){
100516 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
100517 }else if( pA->op==TK_COLLATE ){
100518 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
100519 }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
100520 return 2;
100521 }
100522 }
100523 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
100524 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
100525 if( combinedFlags & EP_xIsSelect ) return 2;
100526 if( (combinedFlags & EP_FixedCol)==0
100527 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
100528 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
100529 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
100530 assert( (combinedFlags & EP_Reduced)==0 );
100531 if( pA->op!=TK_STRING && pA->op!=TK_TRUEFALSE ){
100532 if( pA->iColumn!=pB->iColumn ) return 2;
100533 if( pA->iTable!=pB->iTable
100534 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
100535 }
100536#ifndef SQLITE_OMIT_WINDOWFUNC
100537 /* Justification for the assert():
100538 ** window functions have p->op==TK_FUNCTION but aggregate functions
100539 ** have p->op==TK_AGG_FUNCTION. So any comparison between an aggregate
100540 ** function and a window function should have failed before reaching
100541 ** this point. And, it is not possible to have a window function and
100542 ** a scalar function with the same name and number of arguments. So
100543 ** if we reach this point, either A and B both window functions or
100544 ** neither are a window functions. */
100545 assert( (pA->pWin==0)==(pB->pWin==0) );
100546
100547 if( pA->pWin!=0 ){
100548 if( sqlite3WindowCompare(pParse,pA->pWin,pB->pWin)!=0 ) return 2;
100549 }
100550#endif
100551 }
100552 return 0;
100553}
100554
100555/*
100556** Compare two ExprList objects. Return 0 if they are identical and
100557** non-zero if they differ in any way.
100558**
100559** If any subelement of pB has Expr.iTable==(-1) then it is allowed
100560** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
100561**
100562** This routine might return non-zero for equivalent ExprLists. The
100563** only consequence will be disabled optimizations. But this routine
100564** must never return 0 if the two ExprList objects are different, or
100565** a malfunction will result.
100566**
100567** Two NULL pointers are considered to be the same. But a NULL pointer
100568** always differs from a non-NULL pointer.
100569*/
100570SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
100571 int i;
100572 if( pA==0 && pB==0 ) return 0;
100573 if( pA==0 || pB==0 ) return 1;
100574 if( pA->nExpr!=pB->nExpr ) return 1;
100575 for(i=0; i<pA->nExpr; i++){
100576 Expr *pExprA = pA->a[i].pExpr;
100577 Expr *pExprB = pB->a[i].pExpr;
100578 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
100579 if( sqlite3ExprCompare(0, pExprA, pExprB, iTab) ) return 1;
100580 }
100581 return 0;
100582}
100583
100584/*
100585** Like sqlite3ExprCompare() except COLLATE operators at the top-level
100586** are ignored.
100587*/
100588SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA, Expr *pB, int iTab){
100589 return sqlite3ExprCompare(0,
100590 sqlite3ExprSkipCollate(pA),
100591 sqlite3ExprSkipCollate(pB),
100592 iTab);
100593}
100594
100595/*
100596** Return true if we can prove the pE2 will always be true if pE1 is
100597** true. Return false if we cannot complete the proof or if pE2 might
100598** be false. Examples:
100599**
100600** pE1: x==5 pE2: x==5 Result: true
100601** pE1: x>0 pE2: x==5 Result: false
100602** pE1: x=21 pE2: x=21 OR y=43 Result: true
100603** pE1: x!=123 pE2: x IS NOT NULL Result: true
100604** pE1: x!=?1 pE2: x IS NOT NULL Result: true
100605** pE1: x IS NULL pE2: x IS NOT NULL Result: false
100606** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false
100607**
100608** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
100609** Expr.iTable<0 then assume a table number given by iTab.
100610**
100611** If pParse is not NULL, then the values of bound variables in pE1 are
100612** compared against literal values in pE2 and pParse->pVdbe->expmask is
100613** modified to record which bound variables are referenced. If pParse
100614** is NULL, then false will be returned if pE1 contains any bound variables.
100615**
100616** When in doubt, return false. Returning true might give a performance
100617** improvement. Returning false might cause a performance reduction, but
100618** it will always give the correct answer and is hence always safe.
100619*/
100620SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse *pParse, Expr *pE1, Expr *pE2, int iTab){
100621 if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){
100622 return 1;
100623 }
100624 if( pE2->op==TK_OR
100625 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
100626 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
100627 ){
100628 return 1;
100629 }
100630 if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){
100631 Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft);
100632 testcase( pX!=pE1->pLeft );
100633 if( sqlite3ExprCompare(pParse, pX, pE2->pLeft, iTab)==0 ) return 1;
100634 }
100635 return 0;
100636}
100637
100638/*
100639** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
100640** If the expression node requires that the table at pWalker->iCur
100641** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
100642**
100643** This routine controls an optimization. False positives (setting
100644** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
100645** (never setting pWalker->eCode) is a harmless missed optimization.
100646*/
100647static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
100648 testcase( pExpr->op==TK_AGG_COLUMN );
100649 testcase( pExpr->op==TK_AGG_FUNCTION );
100650 if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
100651 switch( pExpr->op ){
100652 case TK_ISNOT:
100653 case TK_NOT:
100654 case TK_ISNULL:
100655 case TK_IS:
100656 case TK_OR:
100657 case TK_CASE:
100658 case TK_IN:
100659 case TK_FUNCTION:
100660 testcase( pExpr->op==TK_ISNOT );
100661 testcase( pExpr->op==TK_NOT );
100662 testcase( pExpr->op==TK_ISNULL );
100663 testcase( pExpr->op==TK_IS );
100664 testcase( pExpr->op==TK_OR );
100665 testcase( pExpr->op==TK_CASE );
100666 testcase( pExpr->op==TK_IN );
100667 testcase( pExpr->op==TK_FUNCTION );
100668 return WRC_Prune;
100669 case TK_COLUMN:
100670 if( pWalker->u.iCur==pExpr->iTable ){
100671 pWalker->eCode = 1;
100672 return WRC_Abort;
100673 }
100674 return WRC_Prune;
100675
100676 /* Virtual tables are allowed to use constraints like x=NULL. So
100677 ** a term of the form x=y does not prove that y is not null if x
100678 ** is the column of a virtual table */
100679 case TK_EQ:
100680 case TK_NE:
100681 case TK_LT:
100682 case TK_LE:
100683 case TK_GT:
100684 case TK_GE:
100685 testcase( pExpr->op==TK_EQ );
100686 testcase( pExpr->op==TK_NE );
100687 testcase( pExpr->op==TK_LT );
100688 testcase( pExpr->op==TK_LE );
100689 testcase( pExpr->op==TK_GT );
100690 testcase( pExpr->op==TK_GE );
100691 if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->pTab))
100692 || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->pTab))
100693 ){
100694 return WRC_Prune;
100695 }
100696 default:
100697 return WRC_Continue;
100698 }
100699}
100700
100701/*
100702** Return true (non-zero) if expression p can only be true if at least
100703** one column of table iTab is non-null. In other words, return true
100704** if expression p will always be NULL or false if every column of iTab
100705** is NULL.
100706**
100707** False negatives are acceptable. In other words, it is ok to return
100708** zero even if expression p will never be true of every column of iTab
100709** is NULL. A false negative is merely a missed optimization opportunity.
100710**
100711** False positives are not allowed, however. A false positive may result
100712** in an incorrect answer.
100713**
100714** Terms of p that are marked with EP_FromJoin (and hence that come from
100715** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
100716**
100717** This routine is used to check if a LEFT JOIN can be converted into
100718** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE
100719** clause requires that some column of the right table of the LEFT JOIN
100720** be non-NULL, then the LEFT JOIN can be safely converted into an
100721** ordinary join.
100722*/
100723SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
100724 Walker w;
100725 w.xExprCallback = impliesNotNullRow;
100726 w.xSelectCallback = 0;
100727 w.xSelectCallback2 = 0;
100728 w.eCode = 0;
100729 w.u.iCur = iTab;
100730 sqlite3WalkExpr(&w, p);
100731 return w.eCode;
100732}
100733
100734/*
100735** An instance of the following structure is used by the tree walker
100736** to determine if an expression can be evaluated by reference to the
100737** index only, without having to do a search for the corresponding
100738** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur
100739** is the cursor for the table.
100740*/
100741struct IdxCover {
100742 Index *pIdx; /* The index to be tested for coverage */
100743 int iCur; /* Cursor number for the table corresponding to the index */
100744};
100745
100746/*
100747** Check to see if there are references to columns in table
100748** pWalker->u.pIdxCover->iCur can be satisfied using the index
100749** pWalker->u.pIdxCover->pIdx.
100750*/
100751static int exprIdxCover(Walker *pWalker, Expr *pExpr){
100752 if( pExpr->op==TK_COLUMN
100753 && pExpr->iTable==pWalker->u.pIdxCover->iCur
100754 && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
100755 ){
100756 pWalker->eCode = 1;
100757 return WRC_Abort;
100758 }
100759 return WRC_Continue;
100760}
100761
100762/*
100763** Determine if an index pIdx on table with cursor iCur contains will
100764** the expression pExpr. Return true if the index does cover the
100765** expression and false if the pExpr expression references table columns
100766** that are not found in the index pIdx.
100767**
100768** An index covering an expression means that the expression can be
100769** evaluated using only the index and without having to lookup the
100770** corresponding table entry.
100771*/
100772SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(
100773 Expr *pExpr, /* The index to be tested */
100774 int iCur, /* The cursor number for the corresponding table */
100775 Index *pIdx /* The index that might be used for coverage */
100776){
100777 Walker w;
100778 struct IdxCover xcov;
100779 memset(&w, 0, sizeof(w));
100780 xcov.iCur = iCur;
100781 xcov.pIdx = pIdx;
100782 w.xExprCallback = exprIdxCover;
100783 w.u.pIdxCover = &xcov;
100784 sqlite3WalkExpr(&w, pExpr);
100785 return !w.eCode;
100786}
100787
100788
100789/*
100790** An instance of the following structure is used by the tree walker
100791** to count references to table columns in the arguments of an
100792** aggregate function, in order to implement the
100793** sqlite3FunctionThisSrc() routine.
100794*/
100795struct SrcCount {
100796 SrcList *pSrc; /* One particular FROM clause in a nested query */
100797 int nThis; /* Number of references to columns in pSrcList */
100798 int nOther; /* Number of references to columns in other FROM clauses */
100799};
100800
100801/*
100802** Count the number of references to columns.
100803*/
100804static int exprSrcCount(Walker *pWalker, Expr *pExpr){
100805 /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
100806 ** is always called before sqlite3ExprAnalyzeAggregates() and so the
100807 ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If
100808 ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
100809 ** NEVER() will need to be removed. */
100810 if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
100811 int i;
100812 struct SrcCount *p = pWalker->u.pSrcCount;
100813 SrcList *pSrc = p->pSrc;
100814 int nSrc = pSrc ? pSrc->nSrc : 0;
100815 for(i=0; i<nSrc; i++){
100816 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
100817 }
100818 if( i<nSrc ){
100819 p->nThis++;
100820 }else{
100821 p->nOther++;
100822 }
100823 }
100824 return WRC_Continue;
100825}
100826
100827/*
100828** Determine if any of the arguments to the pExpr Function reference
100829** pSrcList. Return true if they do. Also return true if the function
100830** has no arguments or has only constant arguments. Return false if pExpr
100831** references columns but not columns of tables found in pSrcList.
100832*/
100833SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
100834 Walker w;
100835 struct SrcCount cnt;
100836 assert( pExpr->op==TK_AGG_FUNCTION );
100837 w.xExprCallback = exprSrcCount;
100838 w.xSelectCallback = 0;
100839 w.u.pSrcCount = &cnt;
100840 cnt.pSrc = pSrcList;
100841 cnt.nThis = 0;
100842 cnt.nOther = 0;
100843 sqlite3WalkExprList(&w, pExpr->x.pList);
100844 return cnt.nThis>0 || cnt.nOther==0;
100845}
100846
100847/*
100848** Add a new element to the pAggInfo->aCol[] array. Return the index of
100849** the new element. Return a negative number if malloc fails.
100850*/
100851static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
100852 int i;
100853 pInfo->aCol = sqlite3ArrayAllocate(
100854 db,
100855 pInfo->aCol,
100856 sizeof(pInfo->aCol[0]),
100857 &pInfo->nColumn,
100858 &i
100859 );
100860 return i;
100861}
100862
100863/*
100864** Add a new element to the pAggInfo->aFunc[] array. Return the index of
100865** the new element. Return a negative number if malloc fails.
100866*/
100867static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
100868 int i;
100869 pInfo->aFunc = sqlite3ArrayAllocate(
100870 db,
100871 pInfo->aFunc,
100872 sizeof(pInfo->aFunc[0]),
100873 &pInfo->nFunc,
100874 &i
100875 );
100876 return i;
100877}
100878
100879/*
100880** This is the xExprCallback for a tree walker. It is used to
100881** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
100882** for additional information.
100883*/
100884static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
100885 int i;
100886 NameContext *pNC = pWalker->u.pNC;
100887 Parse *pParse = pNC->pParse;
100888 SrcList *pSrcList = pNC->pSrcList;
100889 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
100890
100891 assert( pNC->ncFlags & NC_UAggInfo );
100892 switch( pExpr->op ){
100893 case TK_AGG_COLUMN:
100894 case TK_COLUMN: {
100895 testcase( pExpr->op==TK_AGG_COLUMN );
100896 testcase( pExpr->op==TK_COLUMN );
100897 /* Check to see if the column is in one of the tables in the FROM
100898 ** clause of the aggregate query */
100899 if( ALWAYS(pSrcList!=0) ){
100900 struct SrcList_item *pItem = pSrcList->a;
100901 for(i=0; i<pSrcList->nSrc; i++, pItem++){
100902 struct AggInfo_col *pCol;
100903 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
100904 if( pExpr->iTable==pItem->iCursor ){
100905 /* If we reach this point, it means that pExpr refers to a table
100906 ** that is in the FROM clause of the aggregate query.
100907 **
100908 ** Make an entry for the column in pAggInfo->aCol[] if there
100909 ** is not an entry there already.
100910 */
100911 int k;
100912 pCol = pAggInfo->aCol;
100913 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
100914 if( pCol->iTable==pExpr->iTable &&
100915 pCol->iColumn==pExpr->iColumn ){
100916 break;
100917 }
100918 }
100919 if( (k>=pAggInfo->nColumn)
100920 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
100921 ){
100922 pCol = &pAggInfo->aCol[k];
100923 pCol->pTab = pExpr->pTab;
100924 pCol->iTable = pExpr->iTable;
100925 pCol->iColumn = pExpr->iColumn;
100926 pCol->iMem = ++pParse->nMem;
100927 pCol->iSorterColumn = -1;
100928 pCol->pExpr = pExpr;
100929 if( pAggInfo->pGroupBy ){
100930 int j, n;
100931 ExprList *pGB = pAggInfo->pGroupBy;
100932 struct ExprList_item *pTerm = pGB->a;
100933 n = pGB->nExpr;
100934 for(j=0; j<n; j++, pTerm++){
100935 Expr *pE = pTerm->pExpr;
100936 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
100937 pE->iColumn==pExpr->iColumn ){
100938 pCol->iSorterColumn = j;
100939 break;
100940 }
100941 }
100942 }
100943 if( pCol->iSorterColumn<0 ){
100944 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
100945 }
100946 }
100947 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
100948 ** because it was there before or because we just created it).
100949 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
100950 ** pAggInfo->aCol[] entry.
100951 */
100952 ExprSetVVAProperty(pExpr, EP_NoReduce);
100953 pExpr->pAggInfo = pAggInfo;
100954 pExpr->op = TK_AGG_COLUMN;
100955 pExpr->iAgg = (i16)k;
100956 break;
100957 } /* endif pExpr->iTable==pItem->iCursor */
100958 } /* end loop over pSrcList */
100959 }
100960 return WRC_Prune;
100961 }
100962 case TK_AGG_FUNCTION: {
100963 if( (pNC->ncFlags & NC_InAggFunc)==0
100964 && pWalker->walkerDepth==pExpr->op2
100965 ){
100966 /* Check to see if pExpr is a duplicate of another aggregate
100967 ** function that is already in the pAggInfo structure
100968 */
100969 struct AggInfo_func *pItem = pAggInfo->aFunc;
100970 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
100971 if( sqlite3ExprCompare(0, pItem->pExpr, pExpr, -1)==0 ){
100972 break;
100973 }
100974 }
100975 if( i>=pAggInfo->nFunc ){
100976 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
100977 */
100978 u8 enc = ENC(pParse->db);
100979 i = addAggInfoFunc(pParse->db, pAggInfo);
100980 if( i>=0 ){
100981 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
100982 pItem = &pAggInfo->aFunc[i];
100983 pItem->pExpr = pExpr;
100984 pItem->iMem = ++pParse->nMem;
100985 assert( !ExprHasProperty(pExpr, EP_IntValue) );
100986 pItem->pFunc = sqlite3FindFunction(pParse->db,
100987 pExpr->u.zToken,
100988 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
100989 if( pExpr->flags & EP_Distinct ){
100990 pItem->iDistinct = pParse->nTab++;
100991 }else{
100992 pItem->iDistinct = -1;
100993 }
100994 }
100995 }
100996 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
100997 */
100998 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
100999 ExprSetVVAProperty(pExpr, EP_NoReduce);
101000 pExpr->iAgg = (i16)i;
101001 pExpr->pAggInfo = pAggInfo;
101002 return WRC_Prune;
101003 }else{
101004 return WRC_Continue;
101005 }
101006 }
101007 }
101008 return WRC_Continue;
101009}
101010static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
101011 UNUSED_PARAMETER(pSelect);
101012 pWalker->walkerDepth++;
101013 return WRC_Continue;
101014}
101015static void analyzeAggregatesInSelectEnd(Walker *pWalker, Select *pSelect){
101016 UNUSED_PARAMETER(pSelect);
101017 pWalker->walkerDepth--;
101018}
101019
101020/*
101021** Analyze the pExpr expression looking for aggregate functions and
101022** for variables that need to be added to AggInfo object that pNC->pAggInfo
101023** points to. Additional entries are made on the AggInfo object as
101024** necessary.
101025**
101026** This routine should only be called after the expression has been
101027** analyzed by sqlite3ResolveExprNames().
101028*/
101029SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
101030 Walker w;
101031 w.xExprCallback = analyzeAggregate;
101032 w.xSelectCallback = analyzeAggregatesInSelect;
101033 w.xSelectCallback2 = analyzeAggregatesInSelectEnd;
101034 w.walkerDepth = 0;
101035 w.u.pNC = pNC;
101036 assert( pNC->pSrcList!=0 );
101037 sqlite3WalkExpr(&w, pExpr);
101038}
101039
101040/*
101041** Call sqlite3ExprAnalyzeAggregates() for every expression in an
101042** expression list. Return the number of errors.
101043**
101044** If an error is found, the analysis is cut short.
101045*/
101046SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
101047 struct ExprList_item *pItem;
101048 int i;
101049 if( pList ){
101050 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
101051 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
101052 }
101053 }
101054}
101055
101056/*
101057** Allocate a single new register for use to hold some intermediate result.
101058*/
101059SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
101060 if( pParse->nTempReg==0 ){
101061 return ++pParse->nMem;
101062 }
101063 return pParse->aTempReg[--pParse->nTempReg];
101064}
101065
101066/*
101067** Deallocate a register, making available for reuse for some other
101068** purpose.
101069*/
101070SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
101071 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
101072 pParse->aTempReg[pParse->nTempReg++] = iReg;
101073 }
101074}
101075
101076/*
101077** Allocate or deallocate a block of nReg consecutive registers.
101078*/
101079SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
101080 int i, n;
101081 if( nReg==1 ) return sqlite3GetTempReg(pParse);
101082 i = pParse->iRangeReg;
101083 n = pParse->nRangeReg;
101084 if( nReg<=n ){
101085 pParse->iRangeReg += nReg;
101086 pParse->nRangeReg -= nReg;
101087 }else{
101088 i = pParse->nMem+1;
101089 pParse->nMem += nReg;
101090 }
101091 return i;
101092}
101093SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
101094 if( nReg==1 ){
101095 sqlite3ReleaseTempReg(pParse, iReg);
101096 return;
101097 }
101098 if( nReg>pParse->nRangeReg ){
101099 pParse->nRangeReg = nReg;
101100 pParse->iRangeReg = iReg;
101101 }
101102}
101103
101104/*
101105** Mark all temporary registers as being unavailable for reuse.
101106*/
101107SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
101108 pParse->nTempReg = 0;
101109 pParse->nRangeReg = 0;
101110}
101111
101112/*
101113** Validate that no temporary register falls within the range of
101114** iFirst..iLast, inclusive. This routine is only call from within assert()
101115** statements.
101116*/
101117#ifdef SQLITE_DEBUG
101118SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
101119 int i;
101120 if( pParse->nRangeReg>0
101121 && pParse->iRangeReg+pParse->nRangeReg > iFirst
101122 && pParse->iRangeReg <= iLast
101123 ){
101124 return 0;
101125 }
101126 for(i=0; i<pParse->nTempReg; i++){
101127 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
101128 return 0;
101129 }
101130 }
101131 return 1;
101132}
101133#endif /* SQLITE_DEBUG */
101134
101135/************** End of expr.c ************************************************/
101136/************** Begin file alter.c *******************************************/
101137/*
101138** 2005 February 15
101139**
101140** The author disclaims copyright to this source code. In place of
101141** a legal notice, here is a blessing:
101142**
101143** May you do good and not evil.
101144** May you find forgiveness for yourself and forgive others.
101145** May you share freely, never taking more than you give.
101146**
101147*************************************************************************
101148** This file contains C code routines that used to generate VDBE code
101149** that implements the ALTER TABLE command.
101150*/
101151/* #include "sqliteInt.h" */
101152
101153/*
101154** The code in this file only exists if we are not omitting the
101155** ALTER TABLE logic from the build.
101156*/
101157#ifndef SQLITE_OMIT_ALTERTABLE
101158
101159/*
101160** Parameter zName is the name of a table that is about to be altered
101161** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
101162** If the table is a system table, this function leaves an error message
101163** in pParse->zErr (system tables may not be altered) and returns non-zero.
101164**
101165** Or, if zName is not a system table, zero is returned.
101166*/
101167static int isSystemTable(Parse *pParse, const char *zName){
101168 if( 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
101169 sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
101170 return 1;
101171 }
101172 return 0;
101173}
101174
101175/*
101176** Generate code to verify that the schemas of database zDb and, if
101177** bTemp is not true, database "temp", can still be parsed. This is
101178** called at the end of the generation of an ALTER TABLE ... RENAME ...
101179** statement to ensure that the operation has not rendered any schema
101180** objects unusable.
101181*/
101182static void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){
101183 sqlite3NestedParse(pParse,
101184 "SELECT 1 "
101185 "FROM \"%w\".%s "
101186 "WHERE name NOT LIKE 'sqlite_%%'"
101187 " AND sql NOT LIKE 'create virtual%%'"
101188 " AND sqlite_rename_test(%Q, sql, type, name, %d)=NULL ",
101189 zDb, MASTER_NAME,
101190 zDb, bTemp
101191 );
101192
101193 if( bTemp==0 ){
101194 sqlite3NestedParse(pParse,
101195 "SELECT 1 "
101196 "FROM temp.%s "
101197 "WHERE name NOT LIKE 'sqlite_%%'"
101198 " AND sql NOT LIKE 'create virtual%%'"
101199 " AND sqlite_rename_test(%Q, sql, type, name, 1)=NULL ",
101200 MASTER_NAME, zDb
101201 );
101202 }
101203}
101204
101205/*
101206** Generate code to reload the schema for database iDb. And, if iDb!=1, for
101207** the temp database as well.
101208*/
101209static void renameReloadSchema(Parse *pParse, int iDb){
101210 Vdbe *v = pParse->pVdbe;
101211 if( v ){
101212 sqlite3ChangeCookie(pParse, iDb);
101213 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0);
101214 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0);
101215 }
101216}
101217
101218/*
101219** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
101220** command.
101221*/
101222SQLITE_PRIVATE void sqlite3AlterRenameTable(
101223 Parse *pParse, /* Parser context. */
101224 SrcList *pSrc, /* The table to rename. */
101225 Token *pName /* The new table name. */
101226){
101227 int iDb; /* Database that contains the table */
101228 char *zDb; /* Name of database iDb */
101229 Table *pTab; /* Table being renamed */
101230 char *zName = 0; /* NULL-terminated version of pName */
101231 sqlite3 *db = pParse->db; /* Database connection */
101232 int nTabName; /* Number of UTF-8 characters in zTabName */
101233 const char *zTabName; /* Original name of the table */
101234 Vdbe *v;
101235 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
101236 u32 savedDbFlags; /* Saved value of db->mDbFlags */
101237
101238 savedDbFlags = db->mDbFlags;
101239 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
101240 assert( pSrc->nSrc==1 );
101241 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
101242
101243 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
101244 if( !pTab ) goto exit_rename_table;
101245 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
101246 zDb = db->aDb[iDb].zDbSName;
101247 db->mDbFlags |= DBFLAG_PreferBuiltin;
101248
101249 /* Get a NULL terminated version of the new table name. */
101250 zName = sqlite3NameFromToken(db, pName);
101251 if( !zName ) goto exit_rename_table;
101252
101253 /* Check that a table or index named 'zName' does not already exist
101254 ** in database iDb. If so, this is an error.
101255 */
101256 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
101257 sqlite3ErrorMsg(pParse,
101258 "there is already another table or index with this name: %s", zName);
101259 goto exit_rename_table;
101260 }
101261
101262 /* Make sure it is not a system table being altered, or a reserved name
101263 ** that the table is being renamed to.
101264 */
101265 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
101266 goto exit_rename_table;
101267 }
101268 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
101269 exit_rename_table;
101270 }
101271
101272#ifndef SQLITE_OMIT_VIEW
101273 if( pTab->pSelect ){
101274 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
101275 goto exit_rename_table;
101276 }
101277#endif
101278
101279#ifndef SQLITE_OMIT_AUTHORIZATION
101280 /* Invoke the authorization callback. */
101281 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
101282 goto exit_rename_table;
101283 }
101284#endif
101285
101286#ifndef SQLITE_OMIT_VIRTUALTABLE
101287 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
101288 goto exit_rename_table;
101289 }
101290 if( IsVirtual(pTab) ){
101291 pVTab = sqlite3GetVTable(db, pTab);
101292 if( pVTab->pVtab->pModule->xRename==0 ){
101293 pVTab = 0;
101294 }
101295 }
101296#endif
101297
101298 /* Begin a transaction for database iDb.
101299 ** Then modify the schema cookie (since the ALTER TABLE modifies the
101300 ** schema). Open a statement transaction if the table is a virtual
101301 ** table.
101302 */
101303 v = sqlite3GetVdbe(pParse);
101304 if( v==0 ){
101305 goto exit_rename_table;
101306 }
101307
101308 /* figure out how many UTF-8 characters are in zName */
101309 zTabName = pTab->zName;
101310 nTabName = sqlite3Utf8CharLen(zTabName, -1);
101311
101312 /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
101313 ** the schema to use the new table name. */
101314 sqlite3NestedParse(pParse,
101315 "UPDATE \"%w\".%s SET "
101316 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
101317 "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
101318 "AND name NOT LIKE 'sqlite_%%'"
101319 , zDb, MASTER_NAME, zDb, zTabName, zName, (iDb==1), zTabName
101320 );
101321
101322 /* Update the tbl_name and name columns of the sqlite_master table
101323 ** as required. */
101324 sqlite3NestedParse(pParse,
101325 "UPDATE %Q.%s SET "
101326 "tbl_name = %Q, "
101327 "name = CASE "
101328 "WHEN type='table' THEN %Q "
101329 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
101330 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
101331 "ELSE name END "
101332 "WHERE tbl_name=%Q COLLATE nocase AND "
101333 "(type='table' OR type='index' OR type='trigger');",
101334 zDb, MASTER_NAME,
101335 zName, zName, zName,
101336 nTabName, zTabName
101337 );
101338
101339#ifndef SQLITE_OMIT_AUTOINCREMENT
101340 /* If the sqlite_sequence table exists in this database, then update
101341 ** it with the new table name.
101342 */
101343 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
101344 sqlite3NestedParse(pParse,
101345 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
101346 zDb, zName, pTab->zName);
101347 }
101348#endif
101349
101350 /* If the table being renamed is not itself part of the temp database,
101351 ** edit view and trigger definitions within the temp database
101352 ** as required. */
101353 if( iDb!=1 ){
101354 sqlite3NestedParse(pParse,
101355 "UPDATE sqlite_temp_master SET "
101356 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
101357 "tbl_name = "
101358 "CASE WHEN tbl_name=%Q COLLATE nocase AND "
101359 " sqlite_rename_test(%Q, sql, type, name, 1) "
101360 "THEN %Q ELSE tbl_name END "
101361 "WHERE type IN ('view', 'trigger')"
101362 , zDb, zTabName, zName, zTabName, zDb, zName);
101363 }
101364
101365 /* If this is a virtual table, invoke the xRename() function if
101366 ** one is defined. The xRename() callback will modify the names
101367 ** of any resources used by the v-table implementation (including other
101368 ** SQLite tables) that are identified by the name of the virtual table.
101369 */
101370#ifndef SQLITE_OMIT_VIRTUALTABLE
101371 if( pVTab ){
101372 int i = ++pParse->nMem;
101373 sqlite3VdbeLoadString(v, i, zName);
101374 sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
101375 sqlite3MayAbort(pParse);
101376 }
101377#endif
101378
101379 renameReloadSchema(pParse, iDb);
101380 renameTestSchema(pParse, zDb, iDb==1);
101381
101382exit_rename_table:
101383 sqlite3SrcListDelete(db, pSrc);
101384 sqlite3DbFree(db, zName);
101385 db->mDbFlags = savedDbFlags;
101386}
101387
101388/*
101389** This function is called after an "ALTER TABLE ... ADD" statement
101390** has been parsed. Argument pColDef contains the text of the new
101391** column definition.
101392**
101393** The Table structure pParse->pNewTable was extended to include
101394** the new column during parsing.
101395*/
101396SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
101397 Table *pNew; /* Copy of pParse->pNewTable */
101398 Table *pTab; /* Table being altered */
101399 int iDb; /* Database number */
101400 const char *zDb; /* Database name */
101401 const char *zTab; /* Table name */
101402 char *zCol; /* Null-terminated column definition */
101403 Column *pCol; /* The new column */
101404 Expr *pDflt; /* Default value for the new column */
101405 sqlite3 *db; /* The database connection; */
101406 Vdbe *v; /* The prepared statement under construction */
101407 int r1; /* Temporary registers */
101408
101409 db = pParse->db;
101410 if( pParse->nErr || db->mallocFailed ) return;
101411 pNew = pParse->pNewTable;
101412 assert( pNew );
101413
101414 assert( sqlite3BtreeHoldsAllMutexes(db) );
101415 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
101416 zDb = db->aDb[iDb].zDbSName;
101417 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
101418 pCol = &pNew->aCol[pNew->nCol-1];
101419 pDflt = pCol->pDflt;
101420 pTab = sqlite3FindTable(db, zTab, zDb);
101421 assert( pTab );
101422
101423#ifndef SQLITE_OMIT_AUTHORIZATION
101424 /* Invoke the authorization callback. */
101425 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
101426 return;
101427 }
101428#endif
101429
101430 /* If the default value for the new column was specified with a
101431 ** literal NULL, then set pDflt to 0. This simplifies checking
101432 ** for an SQL NULL default below.
101433 */
101434 assert( pDflt==0 || pDflt->op==TK_SPAN );
101435 if( pDflt && pDflt->pLeft->op==TK_NULL ){
101436 pDflt = 0;
101437 }
101438
101439 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
101440 ** If there is a NOT NULL constraint, then the default value for the
101441 ** column must not be NULL.
101442 */
101443 if( pCol->colFlags & COLFLAG_PRIMKEY ){
101444 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
101445 return;
101446 }
101447 if( pNew->pIndex ){
101448 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
101449 return;
101450 }
101451 if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
101452 sqlite3ErrorMsg(pParse,
101453 "Cannot add a REFERENCES column with non-NULL default value");
101454 return;
101455 }
101456 if( pCol->notNull && !pDflt ){
101457 sqlite3ErrorMsg(pParse,
101458 "Cannot add a NOT NULL column with default value NULL");
101459 return;
101460 }
101461
101462 /* Ensure the default expression is something that sqlite3ValueFromExpr()
101463 ** can handle (i.e. not CURRENT_TIME etc.)
101464 */
101465 if( pDflt ){
101466 sqlite3_value *pVal = 0;
101467 int rc;
101468 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
101469 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
101470 if( rc!=SQLITE_OK ){
101471 assert( db->mallocFailed == 1 );
101472 return;
101473 }
101474 if( !pVal ){
101475 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
101476 return;
101477 }
101478 sqlite3ValueFree(pVal);
101479 }
101480
101481 /* Modify the CREATE TABLE statement. */
101482 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
101483 if( zCol ){
101484 char *zEnd = &zCol[pColDef->n-1];
101485 u32 savedDbFlags = db->mDbFlags;
101486 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
101487 *zEnd-- = '\0';
101488 }
101489 db->mDbFlags |= DBFLAG_PreferBuiltin;
101490 sqlite3NestedParse(pParse,
101491 "UPDATE \"%w\".%s SET "
101492 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
101493 "WHERE type = 'table' AND name = %Q",
101494 zDb, MASTER_NAME, pNew->addColOffset, zCol, pNew->addColOffset+1,
101495 zTab
101496 );
101497 sqlite3DbFree(db, zCol);
101498 db->mDbFlags = savedDbFlags;
101499 }
101500
101501 /* Make sure the schema version is at least 3. But do not upgrade
101502 ** from less than 3 to 4, as that will corrupt any preexisting DESC
101503 ** index.
101504 */
101505 v = sqlite3GetVdbe(pParse);
101506 if( v ){
101507 r1 = sqlite3GetTempReg(pParse);
101508 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
101509 sqlite3VdbeUsesBtree(v, iDb);
101510 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
101511 sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
101512 VdbeCoverage(v);
101513 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
101514 sqlite3ReleaseTempReg(pParse, r1);
101515 }
101516
101517 /* Reload the table definition */
101518 renameReloadSchema(pParse, iDb);
101519}
101520
101521/*
101522** This function is called by the parser after the table-name in
101523** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
101524** pSrc is the full-name of the table being altered.
101525**
101526** This routine makes a (partial) copy of the Table structure
101527** for the table being altered and sets Parse.pNewTable to point
101528** to it. Routines called by the parser as the column definition
101529** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
101530** the copy. The copy of the Table structure is deleted by tokenize.c
101531** after parsing is finished.
101532**
101533** Routine sqlite3AlterFinishAddColumn() will be called to complete
101534** coding the "ALTER TABLE ... ADD" statement.
101535*/
101536SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
101537 Table *pNew;
101538 Table *pTab;
101539 int iDb;
101540 int i;
101541 int nAlloc;
101542 sqlite3 *db = pParse->db;
101543
101544 /* Look up the table being altered. */
101545 assert( pParse->pNewTable==0 );
101546 assert( sqlite3BtreeHoldsAllMutexes(db) );
101547 if( db->mallocFailed ) goto exit_begin_add_column;
101548 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
101549 if( !pTab ) goto exit_begin_add_column;
101550
101551#ifndef SQLITE_OMIT_VIRTUALTABLE
101552 if( IsVirtual(pTab) ){
101553 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
101554 goto exit_begin_add_column;
101555 }
101556#endif
101557
101558 /* Make sure this is not an attempt to ALTER a view. */
101559 if( pTab->pSelect ){
101560 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
101561 goto exit_begin_add_column;
101562 }
101563 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
101564 goto exit_begin_add_column;
101565 }
101566
101567 assert( pTab->addColOffset>0 );
101568 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
101569
101570 /* Put a copy of the Table struct in Parse.pNewTable for the
101571 ** sqlite3AddColumn() function and friends to modify. But modify
101572 ** the name by adding an "sqlite_altertab_" prefix. By adding this
101573 ** prefix, we insure that the name will not collide with an existing
101574 ** table because user table are not allowed to have the "sqlite_"
101575 ** prefix on their name.
101576 */
101577 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
101578 if( !pNew ) goto exit_begin_add_column;
101579 pParse->pNewTable = pNew;
101580 pNew->nTabRef = 1;
101581 pNew->nCol = pTab->nCol;
101582 assert( pNew->nCol>0 );
101583 nAlloc = (((pNew->nCol-1)/8)*8)+8;
101584 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
101585 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
101586 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
101587 if( !pNew->aCol || !pNew->zName ){
101588 assert( db->mallocFailed );
101589 goto exit_begin_add_column;
101590 }
101591 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
101592 for(i=0; i<pNew->nCol; i++){
101593 Column *pCol = &pNew->aCol[i];
101594 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
101595 pCol->zColl = 0;
101596 pCol->pDflt = 0;
101597 }
101598 pNew->pSchema = db->aDb[iDb].pSchema;
101599 pNew->addColOffset = pTab->addColOffset;
101600 pNew->nTabRef = 1;
101601
101602exit_begin_add_column:
101603 sqlite3SrcListDelete(db, pSrc);
101604 return;
101605}
101606
101607/*
101608** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
101609** command. This function checks if the table is a view or virtual
101610** table (columns of views or virtual tables may not be renamed). If so,
101611** it loads an error message into pParse and returns non-zero.
101612**
101613** Or, if pTab is not a view or virtual table, zero is returned.
101614*/
101615#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
101616static int isRealTable(Parse *pParse, Table *pTab){
101617 const char *zType = 0;
101618#ifndef SQLITE_OMIT_VIEW
101619 if( pTab->pSelect ){
101620 zType = "view";
101621 }
101622#endif
101623#ifndef SQLITE_OMIT_VIRTUALTABLE
101624 if( IsVirtual(pTab) ){
101625 zType = "virtual table";
101626 }
101627#endif
101628 if( zType ){
101629 sqlite3ErrorMsg(
101630 pParse, "cannot rename columns of %s \"%s\"", zType, pTab->zName
101631 );
101632 return 1;
101633 }
101634 return 0;
101635}
101636#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
101637# define isRealTable(x,y) (0)
101638#endif
101639
101640/*
101641** Handles the following parser reduction:
101642**
101643** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
101644*/
101645SQLITE_PRIVATE void sqlite3AlterRenameColumn(
101646 Parse *pParse, /* Parsing context */
101647 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
101648 Token *pOld, /* Name of column being changed */
101649 Token *pNew /* New column name */
101650){
101651 sqlite3 *db = pParse->db; /* Database connection */
101652 Table *pTab; /* Table being updated */
101653 int iCol; /* Index of column being renamed */
101654 char *zOld = 0; /* Old column name */
101655 char *zNew = 0; /* New column name */
101656 const char *zDb; /* Name of schema containing the table */
101657 int iSchema; /* Index of the schema */
101658 int bQuote; /* True to quote the new name */
101659
101660 /* Locate the table to be altered */
101661 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
101662 if( !pTab ) goto exit_rename_column;
101663
101664 /* Cannot alter a system table */
101665 if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ) goto exit_rename_column;
101666 if( SQLITE_OK!=isRealTable(pParse, pTab) ) goto exit_rename_column;
101667
101668 /* Which schema holds the table to be altered */
101669 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
101670 assert( iSchema>=0 );
101671 zDb = db->aDb[iSchema].zDbSName;
101672
101673#ifndef SQLITE_OMIT_AUTHORIZATION
101674 /* Invoke the authorization callback. */
101675 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
101676 goto exit_rename_column;
101677 }
101678#endif
101679
101680 /* Make sure the old name really is a column name in the table to be
101681 ** altered. Set iCol to be the index of the column being renamed */
101682 zOld = sqlite3NameFromToken(db, pOld);
101683 if( !zOld ) goto exit_rename_column;
101684 for(iCol=0; iCol<pTab->nCol; iCol++){
101685 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break;
101686 }
101687 if( iCol==pTab->nCol ){
101688 sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld);
101689 goto exit_rename_column;
101690 }
101691
101692 /* Do the rename operation using a recursive UPDATE statement that
101693 ** uses the sqlite_rename_column() SQL function to compute the new
101694 ** CREATE statement text for the sqlite_master table.
101695 */
101696 zNew = sqlite3NameFromToken(db, pNew);
101697 if( !zNew ) goto exit_rename_column;
101698 assert( pNew->n>0 );
101699 bQuote = sqlite3Isquote(pNew->z[0]);
101700 sqlite3NestedParse(pParse,
101701 "UPDATE \"%w\".%s SET "
101702 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
101703 "WHERE name NOT LIKE 'sqlite_%%' AND (type != 'index' OR tbl_name = %Q)"
101704 " AND sql NOT LIKE 'create virtual%%'",
101705 zDb, MASTER_NAME,
101706 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
101707 pTab->zName
101708 );
101709
101710 sqlite3NestedParse(pParse,
101711 "UPDATE temp.%s SET "
101712 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
101713 "WHERE type IN ('trigger', 'view')",
101714 MASTER_NAME,
101715 zDb, pTab->zName, iCol, zNew, bQuote
101716 );
101717
101718 /* Drop and reload the database schema. */
101719 renameReloadSchema(pParse, iSchema);
101720 renameTestSchema(pParse, zDb, iSchema==1);
101721
101722 exit_rename_column:
101723 sqlite3SrcListDelete(db, pSrc);
101724 sqlite3DbFree(db, zOld);
101725 sqlite3DbFree(db, zNew);
101726 return;
101727}
101728
101729/*
101730** Each RenameToken object maps an element of the parse tree into
101731** the token that generated that element. The parse tree element
101732** might be one of:
101733**
101734** * A pointer to an Expr that represents an ID
101735** * The name of a table column in Column.zName
101736**
101737** A list of RenameToken objects can be constructed during parsing.
101738** Each new object is created by sqlite3RenameTokenMap().
101739** As the parse tree is transformed, the sqlite3RenameTokenRemap()
101740** routine is used to keep the mapping current.
101741**
101742** After the parse finishes, renameTokenFind() routine can be used
101743** to look up the actual token value that created some element in
101744** the parse tree.
101745*/
101746struct RenameToken {
101747 void *p; /* Parse tree element created by token t */
101748 Token t; /* The token that created parse tree element p */
101749 RenameToken *pNext; /* Next is a list of all RenameToken objects */
101750};
101751
101752/*
101753** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
101754** down into the Walker.
101755*/
101756typedef struct RenameCtx RenameCtx;
101757struct RenameCtx {
101758 RenameToken *pList; /* List of tokens to overwrite */
101759 int nList; /* Number of tokens in pList */
101760 int iCol; /* Index of column being renamed */
101761 Table *pTab; /* Table being ALTERed */
101762 const char *zOld; /* Old column name */
101763};
101764
101765#ifdef SQLITE_DEBUG
101766/*
101767** This function is only for debugging. It performs two tasks:
101768**
101769** 1. Checks that pointer pPtr does not already appear in the
101770** rename-token list.
101771**
101772** 2. Dereferences each pointer in the rename-token list.
101773**
101774** The second is most effective when debugging under valgrind or
101775** address-sanitizer or similar. If any of these pointers no longer
101776** point to valid objects, an exception is raised by the memory-checking
101777** tool.
101778**
101779** The point of this is to prevent comparisons of invalid pointer values.
101780** Even though this always seems to work, it is undefined according to the
101781** C standard. Example of undefined comparison:
101782**
101783** sqlite3_free(x);
101784** if( x==y ) ...
101785**
101786** Technically, as x no longer points into a valid object or to the byte
101787** following a valid object, it may not be used in comparison operations.
101788*/
101789static void renameTokenCheckAll(Parse *pParse, void *pPtr){
101790 if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
101791 RenameToken *p;
101792 u8 i = 0;
101793 for(p=pParse->pRename; p; p=p->pNext){
101794 if( p->p ){
101795 assert( p->p!=pPtr );
101796 i += *(u8*)(p->p);
101797 }
101798 }
101799 }
101800}
101801#else
101802# define renameTokenCheckAll(x,y)
101803#endif
101804
101805/*
101806** Add a new RenameToken object mapping parse tree element pPtr into
101807** token *pToken to the Parse object currently under construction.
101808**
101809** Return a copy of pPtr.
101810*/
101811SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
101812 RenameToken *pNew;
101813 assert( pPtr || pParse->db->mallocFailed );
101814 renameTokenCheckAll(pParse, pPtr);
101815 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
101816 if( pNew ){
101817 pNew->p = pPtr;
101818 pNew->t = *pToken;
101819 pNew->pNext = pParse->pRename;
101820 pParse->pRename = pNew;
101821 }
101822
101823 return pPtr;
101824}
101825
101826/*
101827** It is assumed that there is already a RenameToken object associated
101828** with parse tree element pFrom. This function remaps the associated token
101829** to parse tree element pTo.
101830*/
101831SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse *pParse, void *pTo, void *pFrom){
101832 RenameToken *p;
101833 renameTokenCheckAll(pParse, pTo);
101834 for(p=pParse->pRename; p; p=p->pNext){
101835 if( p->p==pFrom ){
101836 p->p = pTo;
101837 break;
101838 }
101839 }
101840}
101841
101842/*
101843** Walker callback used by sqlite3RenameExprUnmap().
101844*/
101845static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
101846 Parse *pParse = pWalker->pParse;
101847 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
101848 return WRC_Continue;
101849}
101850
101851/*
101852** Remove all nodes that are part of expression pExpr from the rename list.
101853*/
101854SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
101855 Walker sWalker;
101856 memset(&sWalker, 0, sizeof(Walker));
101857 sWalker.pParse = pParse;
101858 sWalker.xExprCallback = renameUnmapExprCb;
101859 sqlite3WalkExpr(&sWalker, pExpr);
101860}
101861
101862/*
101863** Remove all nodes that are part of expression-list pEList from the
101864** rename list.
101865*/
101866SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
101867 if( pEList ){
101868 int i;
101869 Walker sWalker;
101870 memset(&sWalker, 0, sizeof(Walker));
101871 sWalker.pParse = pParse;
101872 sWalker.xExprCallback = renameUnmapExprCb;
101873 sqlite3WalkExprList(&sWalker, pEList);
101874 for(i=0; i<pEList->nExpr; i++){
101875 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zName);
101876 }
101877 }
101878}
101879
101880/*
101881** Free the list of RenameToken objects given in the second argument
101882*/
101883static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
101884 RenameToken *pNext;
101885 RenameToken *p;
101886 for(p=pToken; p; p=pNext){
101887 pNext = p->pNext;
101888 sqlite3DbFree(db, p);
101889 }
101890}
101891
101892/*
101893** Search the Parse object passed as the first argument for a RenameToken
101894** object associated with parse tree element pPtr. If found, remove it
101895** from the Parse object and add it to the list maintained by the
101896** RenameCtx object passed as the second argument.
101897*/
101898static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){
101899 RenameToken **pp;
101900 assert( pPtr!=0 );
101901 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
101902 if( (*pp)->p==pPtr ){
101903 RenameToken *pToken = *pp;
101904 *pp = pToken->pNext;
101905 pToken->pNext = pCtx->pList;
101906 pCtx->pList = pToken;
101907 pCtx->nList++;
101908 break;
101909 }
101910 }
101911}
101912
101913/*
101914** This is a Walker select callback. It does nothing. It is only required
101915** because without a dummy callback, sqlite3WalkExpr() and similar do not
101916** descend into sub-select statements.
101917*/
101918static int renameColumnSelectCb(Walker *pWalker, Select *p){
101919 UNUSED_PARAMETER(pWalker);
101920 UNUSED_PARAMETER(p);
101921 return WRC_Continue;
101922}
101923
101924/*
101925** This is a Walker expression callback.
101926**
101927** For every TK_COLUMN node in the expression tree, search to see
101928** if the column being references is the column being renamed by an
101929** ALTER TABLE statement. If it is, then attach its associated
101930** RenameToken object to the list of RenameToken objects being
101931** constructed in RenameCtx object at pWalker->u.pRename.
101932*/
101933static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
101934 RenameCtx *p = pWalker->u.pRename;
101935 if( pExpr->op==TK_TRIGGER
101936 && pExpr->iColumn==p->iCol
101937 && pWalker->pParse->pTriggerTab==p->pTab
101938 ){
101939 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
101940 }else if( pExpr->op==TK_COLUMN
101941 && pExpr->iColumn==p->iCol
101942 && p->pTab==pExpr->pTab
101943 ){
101944 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
101945 }
101946 return WRC_Continue;
101947}
101948
101949/*
101950** The RenameCtx contains a list of tokens that reference a column that
101951** is being renamed by an ALTER TABLE statement. Return the "last"
101952** RenameToken in the RenameCtx and remove that RenameToken from the
101953** RenameContext. "Last" means the last RenameToken encountered when
101954** the input SQL is parsed from left to right. Repeated calls to this routine
101955** return all column name tokens in the order that they are encountered
101956** in the SQL statement.
101957*/
101958static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
101959 RenameToken *pBest = pCtx->pList;
101960 RenameToken *pToken;
101961 RenameToken **pp;
101962
101963 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
101964 if( pToken->t.z>pBest->t.z ) pBest = pToken;
101965 }
101966 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
101967 *pp = pBest->pNext;
101968
101969 return pBest;
101970}
101971
101972/*
101973** An error occured while parsing or otherwise processing a database
101974** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
101975** ALTER TABLE RENAME COLUMN program. The error message emitted by the
101976** sub-routine is currently stored in pParse->zErrMsg. This function
101977** adds context to the error message and then stores it in pCtx.
101978*/
101979static void renameColumnParseError(
101980 sqlite3_context *pCtx,
101981 int bPost,
101982 sqlite3_value *pType,
101983 sqlite3_value *pObject,
101984 Parse *pParse
101985){
101986 const char *zT = (const char*)sqlite3_value_text(pType);
101987 const char *zN = (const char*)sqlite3_value_text(pObject);
101988 char *zErr;
101989
101990 zErr = sqlite3_mprintf("error in %s %s%s: %s",
101991 zT, zN, (bPost ? " after rename" : ""),
101992 pParse->zErrMsg
101993 );
101994 sqlite3_result_error(pCtx, zErr, -1);
101995 sqlite3_free(zErr);
101996}
101997
101998/*
101999** For each name in the the expression-list pEList (i.e. each
102000** pEList->a[i].zName) that matches the string in zOld, extract the
102001** corresponding rename-token from Parse object pParse and add it
102002** to the RenameCtx pCtx.
102003*/
102004static void renameColumnElistNames(
102005 Parse *pParse,
102006 RenameCtx *pCtx,
102007 ExprList *pEList,
102008 const char *zOld
102009){
102010 if( pEList ){
102011 int i;
102012 for(i=0; i<pEList->nExpr; i++){
102013 char *zName = pEList->a[i].zName;
102014 if( 0==sqlite3_stricmp(zName, zOld) ){
102015 renameTokenFind(pParse, pCtx, (void*)zName);
102016 }
102017 }
102018 }
102019}
102020
102021/*
102022** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
102023** that matches the string in zOld, extract the corresponding rename-token
102024** from Parse object pParse and add it to the RenameCtx pCtx.
102025*/
102026static void renameColumnIdlistNames(
102027 Parse *pParse,
102028 RenameCtx *pCtx,
102029 IdList *pIdList,
102030 const char *zOld
102031){
102032 if( pIdList ){
102033 int i;
102034 for(i=0; i<pIdList->nId; i++){
102035 char *zName = pIdList->a[i].zName;
102036 if( 0==sqlite3_stricmp(zName, zOld) ){
102037 renameTokenFind(pParse, pCtx, (void*)zName);
102038 }
102039 }
102040 }
102041}
102042
102043/*
102044** Parse the SQL statement zSql using Parse object (*p). The Parse object
102045** is initialized by this function before it is used.
102046*/
102047static int renameParseSql(
102048 Parse *p, /* Memory to use for Parse object */
102049 const char *zDb, /* Name of schema SQL belongs to */
102050 int bTable, /* 1 -> RENAME TABLE, 0 -> RENAME COLUMN */
102051 sqlite3 *db, /* Database handle */
102052 const char *zSql, /* SQL to parse */
102053 int bTemp /* True if SQL is from temp schema */
102054){
102055 int rc;
102056 char *zErr = 0;
102057
102058 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
102059
102060 /* Parse the SQL statement passed as the first argument. If no error
102061 ** occurs and the parse does not result in a new table, index or
102062 ** trigger object, the database must be corrupt. */
102063 memset(p, 0, sizeof(Parse));
102064 p->eParseMode = (bTable ? PARSE_MODE_RENAME_TABLE : PARSE_MODE_RENAME_COLUMN);
102065 p->db = db;
102066 p->nQueryLoop = 1;
102067 rc = sqlite3RunParser(p, zSql, &zErr);
102068 assert( p->zErrMsg==0 );
102069 assert( rc!=SQLITE_OK || zErr==0 );
102070 assert( (0!=p->pNewTable) + (0!=p->pNewIndex) + (0!=p->pNewTrigger)<2 );
102071 p->zErrMsg = zErr;
102072 if( db->mallocFailed ) rc = SQLITE_NOMEM;
102073 if( rc==SQLITE_OK
102074 && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
102075 ){
102076 rc = SQLITE_CORRUPT_BKPT;
102077 }
102078
102079#ifdef SQLITE_DEBUG
102080 /* Ensure that all mappings in the Parse.pRename list really do map to
102081 ** a part of the input string. */
102082 if( rc==SQLITE_OK ){
102083 int nSql = sqlite3Strlen30(zSql);
102084 RenameToken *pToken;
102085 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
102086 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
102087 }
102088 }
102089#endif
102090
102091 db->init.iDb = 0;
102092 return rc;
102093}
102094
102095/*
102096** This function edits SQL statement zSql, replacing each token identified
102097** by the linked list pRename with the text of zNew. If argument bQuote is
102098** true, then zNew is always quoted first. If no error occurs, the result
102099** is loaded into context object pCtx as the result.
102100**
102101** Or, if an error occurs (i.e. an OOM condition), an error is left in
102102** pCtx and an SQLite error code returned.
102103*/
102104static int renameEditSql(
102105 sqlite3_context *pCtx, /* Return result here */
102106 RenameCtx *pRename, /* Rename context */
102107 const char *zSql, /* SQL statement to edit */
102108 const char *zNew, /* New token text */
102109 int bQuote /* True to always quote token */
102110){
102111 int nNew = sqlite3Strlen30(zNew);
102112 int nSql = sqlite3Strlen30(zSql);
102113 sqlite3 *db = sqlite3_context_db_handle(pCtx);
102114 int rc = SQLITE_OK;
102115 char *zQuot;
102116 char *zOut;
102117 int nQuot;
102118
102119 /* Set zQuot to point to a buffer containing a quoted copy of the
102120 ** identifier zNew. If the corresponding identifier in the original
102121 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
102122 ** point to zQuot so that all substitutions are made using the
102123 ** quoted version of the new column name. */
102124 zQuot = sqlite3MPrintf(db, "\"%w\"", zNew);
102125 if( zQuot==0 ){
102126 return SQLITE_NOMEM;
102127 }else{
102128 nQuot = sqlite3Strlen30(zQuot);
102129 }
102130 if( bQuote ){
102131 zNew = zQuot;
102132 nNew = nQuot;
102133 }
102134
102135 /* At this point pRename->pList contains a list of RenameToken objects
102136 ** corresponding to all tokens in the input SQL that must be replaced
102137 ** with the new column name. All that remains is to construct and
102138 ** return the edited SQL string. */
102139 assert( nQuot>=nNew );
102140 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
102141 if( zOut ){
102142 int nOut = nSql;
102143 memcpy(zOut, zSql, nSql);
102144 while( pRename->pList ){
102145 int iOff; /* Offset of token to replace in zOut */
102146 RenameToken *pBest = renameColumnTokenNext(pRename);
102147
102148 u32 nReplace;
102149 const char *zReplace;
102150 if( sqlite3IsIdChar(*pBest->t.z) ){
102151 nReplace = nNew;
102152 zReplace = zNew;
102153 }else{
102154 nReplace = nQuot;
102155 zReplace = zQuot;
102156 }
102157
102158 iOff = pBest->t.z - zSql;
102159 if( pBest->t.n!=nReplace ){
102160 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
102161 nOut - (iOff + pBest->t.n)
102162 );
102163 nOut += nReplace - pBest->t.n;
102164 zOut[nOut] = '\0';
102165 }
102166 memcpy(&zOut[iOff], zReplace, nReplace);
102167 sqlite3DbFree(db, pBest);
102168 }
102169
102170 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
102171 sqlite3DbFree(db, zOut);
102172 }else{
102173 rc = SQLITE_NOMEM;
102174 }
102175
102176 sqlite3_free(zQuot);
102177 return rc;
102178}
102179
102180/*
102181** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
102182** it was read from the schema of database zDb. Return SQLITE_OK if
102183** successful. Otherwise, return an SQLite error code and leave an error
102184** message in the Parse object.
102185*/
102186static int renameResolveTrigger(Parse *pParse, const char *zDb){
102187 sqlite3 *db = pParse->db;
102188 Trigger *pNew = pParse->pNewTrigger;
102189 TriggerStep *pStep;
102190 NameContext sNC;
102191 int rc = SQLITE_OK;
102192
102193 memset(&sNC, 0, sizeof(sNC));
102194 sNC.pParse = pParse;
102195 assert( pNew->pTabSchema );
102196 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
102197 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
102198 );
102199 pParse->eTriggerOp = pNew->op;
102200
102201 /* Resolve symbols in WHEN clause */
102202 if( pNew->pWhen ){
102203 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
102204 }
102205
102206 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
102207 if( pStep->pSelect ){
102208 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
102209 if( pParse->nErr ) rc = pParse->rc;
102210 }
102211 if( rc==SQLITE_OK && pStep->zTarget ){
102212 Table *pTarget = sqlite3LocateTable(pParse, 0, pStep->zTarget, zDb);
102213 if( pTarget==0 ){
102214 rc = SQLITE_ERROR;
102215 }else if( SQLITE_OK==(rc = sqlite3ViewGetColumnNames(pParse, pTarget)) ){
102216 SrcList sSrc;
102217 memset(&sSrc, 0, sizeof(sSrc));
102218 sSrc.nSrc = 1;
102219 sSrc.a[0].zName = pStep->zTarget;
102220 sSrc.a[0].pTab = pTarget;
102221 sNC.pSrcList = &sSrc;
102222 if( pStep->pWhere ){
102223 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
102224 }
102225 if( rc==SQLITE_OK ){
102226 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
102227 }
102228 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
102229 if( pStep->pUpsert ){
102230 Upsert *pUpsert = pStep->pUpsert;
102231 assert( rc==SQLITE_OK );
102232 pUpsert->pUpsertSrc = &sSrc;
102233 sNC.uNC.pUpsert = pUpsert;
102234 sNC.ncFlags = NC_UUpsert;
102235 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
102236 if( rc==SQLITE_OK ){
102237 ExprList *pUpsertSet = pUpsert->pUpsertSet;
102238 rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
102239 }
102240 if( rc==SQLITE_OK ){
102241 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
102242 }
102243 if( rc==SQLITE_OK ){
102244 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
102245 }
102246 sNC.ncFlags = 0;
102247 }
102248 }
102249 }
102250 }
102251 return rc;
102252}
102253
102254/*
102255** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
102256** objects that are part of the trigger passed as the second argument.
102257*/
102258static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
102259 TriggerStep *pStep;
102260
102261 /* Find tokens to edit in WHEN clause */
102262 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
102263
102264 /* Find tokens to edit in trigger steps */
102265 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
102266 sqlite3WalkSelect(pWalker, pStep->pSelect);
102267 sqlite3WalkExpr(pWalker, pStep->pWhere);
102268 sqlite3WalkExprList(pWalker, pStep->pExprList);
102269 if( pStep->pUpsert ){
102270 Upsert *pUpsert = pStep->pUpsert;
102271 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
102272 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
102273 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
102274 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
102275 }
102276 }
102277}
102278
102279/*
102280** Free the contents of Parse object (*pParse). Do not free the memory
102281** occupied by the Parse object itself.
102282*/
102283static void renameParseCleanup(Parse *pParse){
102284 sqlite3 *db = pParse->db;
102285 if( pParse->pVdbe ){
102286 sqlite3VdbeFinalize(pParse->pVdbe);
102287 }
102288 sqlite3DeleteTable(db, pParse->pNewTable);
102289 if( pParse->pNewIndex ) sqlite3FreeIndex(db, pParse->pNewIndex);
102290 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
102291 sqlite3DbFree(db, pParse->zErrMsg);
102292 renameTokenFree(db, pParse->pRename);
102293 sqlite3ParserReset(pParse);
102294}
102295
102296/*
102297** SQL function:
102298**
102299** sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
102300**
102301** 0. zSql: SQL statement to rewrite
102302** 1. type: Type of object ("table", "view" etc.)
102303** 2. object: Name of object
102304** 3. Database: Database name (e.g. "main")
102305** 4. Table: Table name
102306** 5. iCol: Index of column to rename
102307** 6. zNew: New column name
102308** 7. bQuote: Non-zero if the new column name should be quoted.
102309** 8. bTemp: True if zSql comes from temp schema
102310**
102311** Do a column rename operation on the CREATE statement given in zSql.
102312** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
102313** into zNew. The name should be quoted if bQuote is true.
102314**
102315** This function is used internally by the ALTER TABLE RENAME COLUMN command.
102316** Though accessible to application code, it is not intended for use by
102317** applications. The existance of this function, and the way it works,
102318** is subject to change without notice.
102319**
102320** If any of the parameters are out-of-bounds, then simply return NULL.
102321** An out-of-bounds parameter can only occur when the application calls
102322** this function directly. The parameters will always be well-formed when
102323** this routine is invoked by the bytecode for a legitimate ALTER TABLE
102324** statement.
102325*/
102326static void renameColumnFunc(
102327 sqlite3_context *context,
102328 int NotUsed,
102329 sqlite3_value **argv
102330){
102331 sqlite3 *db = sqlite3_context_db_handle(context);
102332 RenameCtx sCtx;
102333 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
102334 const char *zDb = (const char*)sqlite3_value_text(argv[3]);
102335 const char *zTable = (const char*)sqlite3_value_text(argv[4]);
102336 int iCol = sqlite3_value_int(argv[5]);
102337 const char *zNew = (const char*)sqlite3_value_text(argv[6]);
102338 int bQuote = sqlite3_value_int(argv[7]);
102339 int bTemp = sqlite3_value_int(argv[8]);
102340 const char *zOld;
102341 int rc;
102342 Parse sParse;
102343 Walker sWalker;
102344 Index *pIdx;
102345 int i;
102346 Table *pTab;
102347#ifndef SQLITE_OMIT_AUTHORIZATION
102348 sqlite3_xauth xAuth = db->xAuth;
102349#endif
102350
102351 UNUSED_PARAMETER(NotUsed);
102352 if( zSql==0 ) return;
102353 if( zTable==0 ) return;
102354 if( zNew==0 ) return;
102355 if( iCol<0 ) return;
102356 sqlite3BtreeEnterAll(db);
102357 pTab = sqlite3FindTable(db, zTable, zDb);
102358 if( pTab==0 || iCol>=pTab->nCol ){
102359 sqlite3BtreeLeaveAll(db);
102360 return;
102361 }
102362 zOld = pTab->aCol[iCol].zName;
102363 memset(&sCtx, 0, sizeof(sCtx));
102364 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
102365
102366#ifndef SQLITE_OMIT_AUTHORIZATION
102367 db->xAuth = 0;
102368#endif
102369 rc = renameParseSql(&sParse, zDb, 0, db, zSql, bTemp);
102370
102371 /* Find tokens that need to be replaced. */
102372 memset(&sWalker, 0, sizeof(Walker));
102373 sWalker.pParse = &sParse;
102374 sWalker.xExprCallback = renameColumnExprCb;
102375 sWalker.xSelectCallback = renameColumnSelectCb;
102376 sWalker.u.pRename = &sCtx;
102377
102378 sCtx.pTab = pTab;
102379 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
102380 if( sParse.pNewTable ){
102381 Select *pSelect = sParse.pNewTable->pSelect;
102382 if( pSelect ){
102383 sParse.rc = SQLITE_OK;
102384 sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, 0);
102385 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
102386 if( rc==SQLITE_OK ){
102387 sqlite3WalkSelect(&sWalker, pSelect);
102388 }
102389 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
102390 }else{
102391 /* A regular table */
102392 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
102393 FKey *pFKey;
102394 assert( sParse.pNewTable->pSelect==0 );
102395 sCtx.pTab = sParse.pNewTable;
102396 if( bFKOnly==0 ){
102397 renameTokenFind(
102398 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName
102399 );
102400 if( sCtx.iCol<0 ){
102401 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
102402 }
102403 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
102404 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
102405 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
102406 }
102407 }
102408
102409 for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
102410 for(i=0; i<pFKey->nCol; i++){
102411 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
102412 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
102413 }
102414 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
102415 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
102416 ){
102417 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
102418 }
102419 }
102420 }
102421 }
102422 }else if( sParse.pNewIndex ){
102423 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
102424 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
102425 }else{
102426 /* A trigger */
102427 TriggerStep *pStep;
102428 rc = renameResolveTrigger(&sParse, (bTemp ? 0 : zDb));
102429 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
102430
102431 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
102432 if( pStep->zTarget ){
102433 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
102434 if( pTarget==pTab ){
102435 if( pStep->pUpsert ){
102436 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
102437 renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
102438 }
102439 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
102440 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
102441 }
102442 }
102443 }
102444
102445
102446 /* Find tokens to edit in UPDATE OF clause */
102447 if( sParse.pTriggerTab==pTab ){
102448 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
102449 }
102450
102451 /* Find tokens to edit in various expressions and selects */
102452 renameWalkTrigger(&sWalker, sParse.pNewTrigger);
102453 }
102454
102455 assert( rc==SQLITE_OK );
102456 rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
102457
102458renameColumnFunc_done:
102459 if( rc!=SQLITE_OK ){
102460 if( sParse.zErrMsg ){
102461 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
102462 }else{
102463 sqlite3_result_error_code(context, rc);
102464 }
102465 }
102466
102467 renameParseCleanup(&sParse);
102468 renameTokenFree(db, sCtx.pList);
102469#ifndef SQLITE_OMIT_AUTHORIZATION
102470 db->xAuth = xAuth;
102471#endif
102472 sqlite3BtreeLeaveAll(db);
102473}
102474
102475/*
102476** Walker expression callback used by "RENAME TABLE".
102477*/
102478static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
102479 RenameCtx *p = pWalker->u.pRename;
102480 if( pExpr->op==TK_COLUMN && p->pTab==pExpr->pTab ){
102481 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->pTab);
102482 }
102483 return WRC_Continue;
102484}
102485
102486/*
102487** Walker select callback used by "RENAME TABLE".
102488*/
102489static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
102490 int i;
102491 RenameCtx *p = pWalker->u.pRename;
102492 SrcList *pSrc = pSelect->pSrc;
102493 for(i=0; i<pSrc->nSrc; i++){
102494 struct SrcList_item *pItem = &pSrc->a[i];
102495 if( pItem->pTab==p->pTab ){
102496 renameTokenFind(pWalker->pParse, p, pItem->zName);
102497 }
102498 }
102499
102500 return WRC_Continue;
102501}
102502
102503
102504/*
102505** This C function implements an SQL user function that is used by SQL code
102506** generated by the ALTER TABLE ... RENAME command to modify the definition
102507** of any foreign key constraints that use the table being renamed as the
102508** parent table. It is passed three arguments:
102509**
102510** 0: The database containing the table being renamed.
102511** 1. type: Type of object ("table", "view" etc.)
102512** 2. object: Name of object
102513** 3: The complete text of the schema statement being modified,
102514** 4: The old name of the table being renamed, and
102515** 5: The new name of the table being renamed.
102516** 6: True if the schema statement comes from the temp db.
102517**
102518** It returns the new schema statement. For example:
102519**
102520** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
102521** -> 'CREATE TABLE t1(a REFERENCES t3)'
102522*/
102523static void renameTableFunc(
102524 sqlite3_context *context,
102525 int NotUsed,
102526 sqlite3_value **argv
102527){
102528 sqlite3 *db = sqlite3_context_db_handle(context);
102529 const char *zDb = (const char*)sqlite3_value_text(argv[0]);
102530 const char *zInput = (const char*)sqlite3_value_text(argv[3]);
102531 const char *zOld = (const char*)sqlite3_value_text(argv[4]);
102532 const char *zNew = (const char*)sqlite3_value_text(argv[5]);
102533 int bTemp = sqlite3_value_int(argv[6]);
102534 UNUSED_PARAMETER(NotUsed);
102535
102536 if( zInput && zOld && zNew ){
102537 Parse sParse;
102538 int rc;
102539 int bQuote = 1;
102540 RenameCtx sCtx;
102541 Walker sWalker;
102542
102543#ifndef SQLITE_OMIT_AUTHORIZATION
102544 sqlite3_xauth xAuth = db->xAuth;
102545 db->xAuth = 0;
102546#endif
102547
102548 sqlite3BtreeEnterAll(db);
102549
102550 memset(&sCtx, 0, sizeof(RenameCtx));
102551 sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
102552 memset(&sWalker, 0, sizeof(Walker));
102553 sWalker.pParse = &sParse;
102554 sWalker.xExprCallback = renameTableExprCb;
102555 sWalker.xSelectCallback = renameTableSelectCb;
102556 sWalker.u.pRename = &sCtx;
102557
102558 rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
102559
102560 if( rc==SQLITE_OK ){
102561 int isLegacy = (db->flags & SQLITE_LegacyAlter);
102562 if( sParse.pNewTable ){
102563 Table *pTab = sParse.pNewTable;
102564
102565 if( pTab->pSelect ){
102566 if( isLegacy==0 ){
102567 NameContext sNC;
102568 memset(&sNC, 0, sizeof(sNC));
102569 sNC.pParse = &sParse;
102570
102571 sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
102572 if( sParse.nErr ) rc = sParse.rc;
102573 sqlite3WalkSelect(&sWalker, pTab->pSelect);
102574 }
102575 }else{
102576 /* Modify any FK definitions to point to the new table. */
102577#ifndef SQLITE_OMIT_FOREIGN_KEY
102578 if( db->flags & SQLITE_ForeignKeys ){
102579 FKey *pFKey;
102580 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
102581 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
102582 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
102583 }
102584 }
102585 }
102586#endif
102587
102588 /* If this is the table being altered, fix any table refs in CHECK
102589 ** expressions. Also update the name that appears right after the
102590 ** "CREATE [VIRTUAL] TABLE" bit. */
102591 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
102592 sCtx.pTab = pTab;
102593 if( isLegacy==0 ){
102594 sqlite3WalkExprList(&sWalker, pTab->pCheck);
102595 }
102596 renameTokenFind(&sParse, &sCtx, pTab->zName);
102597 }
102598 }
102599 }
102600
102601 else if( sParse.pNewIndex ){
102602 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
102603 if( isLegacy==0 ){
102604 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
102605 }
102606 }
102607
102608#ifndef SQLITE_OMIT_TRIGGER
102609 else{
102610 Trigger *pTrigger = sParse.pNewTrigger;
102611 TriggerStep *pStep;
102612 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
102613 && sCtx.pTab->pSchema==pTrigger->pTabSchema
102614 ){
102615 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
102616 }
102617
102618 if( isLegacy==0 ){
102619 rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
102620 if( rc==SQLITE_OK ){
102621 renameWalkTrigger(&sWalker, pTrigger);
102622 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
102623 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
102624 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
102625 }
102626 }
102627 }
102628 }
102629 }
102630#endif
102631 }
102632
102633 if( rc==SQLITE_OK ){
102634 rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
102635 }
102636 if( rc!=SQLITE_OK ){
102637 if( sParse.zErrMsg ){
102638 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
102639 }else{
102640 sqlite3_result_error_code(context, rc);
102641 }
102642 }
102643
102644 renameParseCleanup(&sParse);
102645 renameTokenFree(db, sCtx.pList);
102646 sqlite3BtreeLeaveAll(db);
102647#ifndef SQLITE_OMIT_AUTHORIZATION
102648 db->xAuth = xAuth;
102649#endif
102650 }
102651
102652 return;
102653}
102654
102655/*
102656** An SQL user function that checks that there are no parse or symbol
102657** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
102658** After an ALTER TABLE .. RENAME operation is performed and the schema
102659** reloaded, this function is called on each SQL statement in the schema
102660** to ensure that it is still usable.
102661**
102662** 0: Database name ("main", "temp" etc.).
102663** 1: SQL statement.
102664** 2: Object type ("view", "table", "trigger" or "index").
102665** 3: Object name.
102666** 4: True if object is from temp schema.
102667**
102668** Unless it finds an error, this function normally returns NULL. However, it
102669** returns integer value 1 if:
102670**
102671** * the SQL argument creates a trigger, and
102672** * the table that the trigger is attached to is in database zDb.
102673*/
102674static void renameTableTest(
102675 sqlite3_context *context,
102676 int NotUsed,
102677 sqlite3_value **argv
102678){
102679 sqlite3 *db = sqlite3_context_db_handle(context);
102680 char const *zDb = (const char*)sqlite3_value_text(argv[0]);
102681 char const *zInput = (const char*)sqlite3_value_text(argv[1]);
102682 int bTemp = sqlite3_value_int(argv[4]);
102683 int isLegacy = (db->flags & SQLITE_LegacyAlter);
102684
102685#ifndef SQLITE_OMIT_AUTHORIZATION
102686 sqlite3_xauth xAuth = db->xAuth;
102687 db->xAuth = 0;
102688#endif
102689
102690 UNUSED_PARAMETER(NotUsed);
102691 if( zDb && zInput ){
102692 int rc;
102693 Parse sParse;
102694 rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
102695 if( rc==SQLITE_OK ){
102696 if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
102697 NameContext sNC;
102698 memset(&sNC, 0, sizeof(sNC));
102699 sNC.pParse = &sParse;
102700 sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
102701 if( sParse.nErr ) rc = sParse.rc;
102702 }
102703
102704 else if( sParse.pNewTrigger ){
102705 if( isLegacy==0 ){
102706 rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
102707 }
102708 if( rc==SQLITE_OK ){
102709 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
102710 int i2 = sqlite3FindDbName(db, zDb);
102711 if( i1==i2 ) sqlite3_result_int(context, 1);
102712 }
102713 }
102714 }
102715
102716 if( rc!=SQLITE_OK ){
102717 renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
102718 }
102719 renameParseCleanup(&sParse);
102720 }
102721
102722#ifndef SQLITE_OMIT_AUTHORIZATION
102723 db->xAuth = xAuth;
102724#endif
102725}
102726
102727/*
102728** Register built-in functions used to help implement ALTER TABLE
102729*/
102730SQLITE_PRIVATE void sqlite3AlterFunctions(void){
102731 static FuncDef aAlterTableFuncs[] = {
102732 FUNCTION(sqlite_rename_column, 9, 0, 0, renameColumnFunc),
102733 FUNCTION(sqlite_rename_table, 7, 0, 0, renameTableFunc),
102734 FUNCTION(sqlite_rename_test, 5, 0, 0, renameTableTest),
102735 };
102736 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
102737}
102738#endif /* SQLITE_ALTER_TABLE */
102739
102740/************** End of alter.c ***********************************************/
102741/************** Begin file analyze.c *****************************************/
102742/*
102743** 2005-07-08
102744**
102745** The author disclaims copyright to this source code. In place of
102746** a legal notice, here is a blessing:
102747**
102748** May you do good and not evil.
102749** May you find forgiveness for yourself and forgive others.
102750** May you share freely, never taking more than you give.
102751**
102752*************************************************************************
102753** This file contains code associated with the ANALYZE command.
102754**
102755** The ANALYZE command gather statistics about the content of tables
102756** and indices. These statistics are made available to the query planner
102757** to help it make better decisions about how to perform queries.
102758**
102759** The following system tables are or have been supported:
102760**
102761** CREATE TABLE sqlite_stat1(tbl, idx, stat);
102762** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
102763** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
102764** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
102765**
102766** Additional tables might be added in future releases of SQLite.
102767** The sqlite_stat2 table is not created or used unless the SQLite version
102768** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
102769** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
102770** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
102771** created and used by SQLite versions 3.7.9 and later and with
102772** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
102773** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced
102774** version of sqlite_stat3 and is only available when compiled with
102775** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later. It is
102776** not possible to enable both STAT3 and STAT4 at the same time. If they
102777** are both enabled, then STAT4 takes precedence.
102778**
102779** For most applications, sqlite_stat1 provides all the statistics required
102780** for the query planner to make good choices.
102781**
102782** Format of sqlite_stat1:
102783**
102784** There is normally one row per index, with the index identified by the
102785** name in the idx column. The tbl column is the name of the table to
102786** which the index belongs. In each such row, the stat column will be
102787** a string consisting of a list of integers. The first integer in this
102788** list is the number of rows in the index. (This is the same as the
102789** number of rows in the table, except for partial indices.) The second
102790** integer is the average number of rows in the index that have the same
102791** value in the first column of the index. The third integer is the average
102792** number of rows in the index that have the same value for the first two
102793** columns. The N-th integer (for N>1) is the average number of rows in
102794** the index which have the same value for the first N-1 columns. For
102795** a K-column index, there will be K+1 integers in the stat column. If
102796** the index is unique, then the last integer will be 1.
102797**
102798** The list of integers in the stat column can optionally be followed
102799** by the keyword "unordered". The "unordered" keyword, if it is present,
102800** must be separated from the last integer by a single space. If the
102801** "unordered" keyword is present, then the query planner assumes that
102802** the index is unordered and will not use the index for a range query.
102803**
102804** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
102805** column contains a single integer which is the (estimated) number of
102806** rows in the table identified by sqlite_stat1.tbl.
102807**
102808** Format of sqlite_stat2:
102809**
102810** The sqlite_stat2 is only created and is only used if SQLite is compiled
102811** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
102812** 3.6.18 and 3.7.8. The "stat2" table contains additional information
102813** about the distribution of keys within an index. The index is identified by
102814** the "idx" column and the "tbl" column is the name of the table to which
102815** the index belongs. There are usually 10 rows in the sqlite_stat2
102816** table for each index.
102817**
102818** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
102819** inclusive are samples of the left-most key value in the index taken at
102820** evenly spaced points along the index. Let the number of samples be S
102821** (10 in the standard build) and let C be the number of rows in the index.
102822** Then the sampled rows are given by:
102823**
102824** rownumber = (i*C*2 + C)/(S*2)
102825**
102826** For i between 0 and S-1. Conceptually, the index space is divided into
102827** S uniform buckets and the samples are the middle row from each bucket.
102828**
102829** The format for sqlite_stat2 is recorded here for legacy reference. This
102830** version of SQLite does not support sqlite_stat2. It neither reads nor
102831** writes the sqlite_stat2 table. This version of SQLite only supports
102832** sqlite_stat3.
102833**
102834** Format for sqlite_stat3:
102835**
102836** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
102837** sqlite_stat4 format will be described first. Further information
102838** about sqlite_stat3 follows the sqlite_stat4 description.
102839**
102840** Format for sqlite_stat4:
102841**
102842** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
102843** to aid the query planner in choosing good indices based on the values
102844** that indexed columns are compared against in the WHERE clauses of
102845** queries.
102846**
102847** The sqlite_stat4 table contains multiple entries for each index.
102848** The idx column names the index and the tbl column is the table of the
102849** index. If the idx and tbl columns are the same, then the sample is
102850** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
102851** binary encoding of a key from the index. The nEq column is a
102852** list of integers. The first integer is the approximate number
102853** of entries in the index whose left-most column exactly matches
102854** the left-most column of the sample. The second integer in nEq
102855** is the approximate number of entries in the index where the
102856** first two columns match the first two columns of the sample.
102857** And so forth. nLt is another list of integers that show the approximate
102858** number of entries that are strictly less than the sample. The first
102859** integer in nLt contains the number of entries in the index where the
102860** left-most column is less than the left-most column of the sample.
102861** The K-th integer in the nLt entry is the number of index entries
102862** where the first K columns are less than the first K columns of the
102863** sample. The nDLt column is like nLt except that it contains the
102864** number of distinct entries in the index that are less than the
102865** sample.
102866**
102867** There can be an arbitrary number of sqlite_stat4 entries per index.
102868** The ANALYZE command will typically generate sqlite_stat4 tables
102869** that contain between 10 and 40 samples which are distributed across
102870** the key space, though not uniformly, and which include samples with
102871** large nEq values.
102872**
102873** Format for sqlite_stat3 redux:
102874**
102875** The sqlite_stat3 table is like sqlite_stat4 except that it only
102876** looks at the left-most column of the index. The sqlite_stat3.sample
102877** column contains the actual value of the left-most column instead
102878** of a blob encoding of the complete index key as is found in
102879** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
102880** all contain just a single integer which is the same as the first
102881** integer in the equivalent columns in sqlite_stat4.
102882*/
102883#ifndef SQLITE_OMIT_ANALYZE
102884/* #include "sqliteInt.h" */
102885
102886#if defined(SQLITE_ENABLE_STAT4)
102887# define IsStat4 1
102888# define IsStat3 0
102889#elif defined(SQLITE_ENABLE_STAT3)
102890# define IsStat4 0
102891# define IsStat3 1
102892#else
102893# define IsStat4 0
102894# define IsStat3 0
102895# undef SQLITE_STAT4_SAMPLES
102896# define SQLITE_STAT4_SAMPLES 1
102897#endif
102898#define IsStat34 (IsStat3+IsStat4) /* 1 for STAT3 or STAT4. 0 otherwise */
102899
102900/*
102901** This routine generates code that opens the sqlite_statN tables.
102902** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
102903** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
102904** appropriate compile-time options are provided.
102905**
102906** If the sqlite_statN tables do not previously exist, it is created.
102907**
102908** Argument zWhere may be a pointer to a buffer containing a table name,
102909** or it may be a NULL pointer. If it is not NULL, then all entries in
102910** the sqlite_statN tables associated with the named table are deleted.
102911** If zWhere==0, then code is generated to delete all stat table entries.
102912*/
102913static void openStatTable(
102914 Parse *pParse, /* Parsing context */
102915 int iDb, /* The database we are looking in */
102916 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
102917 const char *zWhere, /* Delete entries for this table or index */
102918 const char *zWhereType /* Either "tbl" or "idx" */
102919){
102920 static const struct {
102921 const char *zName;
102922 const char *zCols;
102923 } aTable[] = {
102924 { "sqlite_stat1", "tbl,idx,stat" },
102925#if defined(SQLITE_ENABLE_STAT4)
102926 { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
102927 { "sqlite_stat3", 0 },
102928#elif defined(SQLITE_ENABLE_STAT3)
102929 { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
102930 { "sqlite_stat4", 0 },
102931#else
102932 { "sqlite_stat3", 0 },
102933 { "sqlite_stat4", 0 },
102934#endif
102935 };
102936 int i;
102937 sqlite3 *db = pParse->db;
102938 Db *pDb;
102939 Vdbe *v = sqlite3GetVdbe(pParse);
102940 int aRoot[ArraySize(aTable)];
102941 u8 aCreateTbl[ArraySize(aTable)];
102942
102943 if( v==0 ) return;
102944 assert( sqlite3BtreeHoldsAllMutexes(db) );
102945 assert( sqlite3VdbeDb(v)==db );
102946 pDb = &db->aDb[iDb];
102947
102948 /* Create new statistic tables if they do not exist, or clear them
102949 ** if they do already exist.
102950 */
102951 for(i=0; i<ArraySize(aTable); i++){
102952 const char *zTab = aTable[i].zName;
102953 Table *pStat;
102954 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
102955 if( aTable[i].zCols ){
102956 /* The sqlite_statN table does not exist. Create it. Note that a
102957 ** side-effect of the CREATE TABLE statement is to leave the rootpage
102958 ** of the new table in register pParse->regRoot. This is important
102959 ** because the OpenWrite opcode below will be needing it. */
102960 sqlite3NestedParse(pParse,
102961 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
102962 );
102963 aRoot[i] = pParse->regRoot;
102964 aCreateTbl[i] = OPFLAG_P2ISREG;
102965 }
102966 }else{
102967 /* The table already exists. If zWhere is not NULL, delete all entries
102968 ** associated with the table zWhere. If zWhere is NULL, delete the
102969 ** entire contents of the table. */
102970 aRoot[i] = pStat->tnum;
102971 aCreateTbl[i] = 0;
102972 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
102973 if( zWhere ){
102974 sqlite3NestedParse(pParse,
102975 "DELETE FROM %Q.%s WHERE %s=%Q",
102976 pDb->zDbSName, zTab, zWhereType, zWhere
102977 );
102978#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
102979 }else if( db->xPreUpdateCallback ){
102980 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
102981#endif
102982 }else{
102983 /* The sqlite_stat[134] table already exists. Delete all rows. */
102984 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
102985 }
102986 }
102987 }
102988
102989 /* Open the sqlite_stat[134] tables for writing. */
102990 for(i=0; aTable[i].zCols; i++){
102991 assert( i<ArraySize(aTable) );
102992 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3);
102993 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
102994 VdbeComment((v, aTable[i].zName));
102995 }
102996}
102997
102998/*
102999** Recommended number of samples for sqlite_stat4
103000*/
103001#ifndef SQLITE_STAT4_SAMPLES
103002# define SQLITE_STAT4_SAMPLES 24
103003#endif
103004
103005/*
103006** Three SQL functions - stat_init(), stat_push(), and stat_get() -
103007** share an instance of the following structure to hold their state
103008** information.
103009*/
103010typedef struct Stat4Accum Stat4Accum;
103011typedef struct Stat4Sample Stat4Sample;
103012struct Stat4Sample {
103013 tRowcnt *anEq; /* sqlite_stat4.nEq */
103014 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
103015#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103016 tRowcnt *anLt; /* sqlite_stat4.nLt */
103017 union {
103018 i64 iRowid; /* Rowid in main table of the key */
103019 u8 *aRowid; /* Key for WITHOUT ROWID tables */
103020 } u;
103021 u32 nRowid; /* Sizeof aRowid[] */
103022 u8 isPSample; /* True if a periodic sample */
103023 int iCol; /* If !isPSample, the reason for inclusion */
103024 u32 iHash; /* Tiebreaker hash */
103025#endif
103026};
103027struct Stat4Accum {
103028 tRowcnt nRow; /* Number of rows in the entire table */
103029 tRowcnt nPSample; /* How often to do a periodic sample */
103030 int nCol; /* Number of columns in index + pk/rowid */
103031 int nKeyCol; /* Number of index columns w/o the pk/rowid */
103032 int mxSample; /* Maximum number of samples to accumulate */
103033 Stat4Sample current; /* Current row as a Stat4Sample */
103034 u32 iPrn; /* Pseudo-random number used for sampling */
103035 Stat4Sample *aBest; /* Array of nCol best samples */
103036 int iMin; /* Index in a[] of entry with minimum score */
103037 int nSample; /* Current number of samples */
103038 int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
103039 int iGet; /* Index of current sample accessed by stat_get() */
103040 Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
103041 sqlite3 *db; /* Database connection, for malloc() */
103042};
103043
103044/* Reclaim memory used by a Stat4Sample
103045*/
103046#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103047static void sampleClear(sqlite3 *db, Stat4Sample *p){
103048 assert( db!=0 );
103049 if( p->nRowid ){
103050 sqlite3DbFree(db, p->u.aRowid);
103051 p->nRowid = 0;
103052 }
103053}
103054#endif
103055
103056/* Initialize the BLOB value of a ROWID
103057*/
103058#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103059static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
103060 assert( db!=0 );
103061 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
103062 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
103063 if( p->u.aRowid ){
103064 p->nRowid = n;
103065 memcpy(p->u.aRowid, pData, n);
103066 }else{
103067 p->nRowid = 0;
103068 }
103069}
103070#endif
103071
103072/* Initialize the INTEGER value of a ROWID.
103073*/
103074#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103075static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
103076 assert( db!=0 );
103077 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
103078 p->nRowid = 0;
103079 p->u.iRowid = iRowid;
103080}
103081#endif
103082
103083
103084/*
103085** Copy the contents of object (*pFrom) into (*pTo).
103086*/
103087#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103088static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
103089 pTo->isPSample = pFrom->isPSample;
103090 pTo->iCol = pFrom->iCol;
103091 pTo->iHash = pFrom->iHash;
103092 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
103093 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
103094 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
103095 if( pFrom->nRowid ){
103096 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
103097 }else{
103098 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
103099 }
103100}
103101#endif
103102
103103/*
103104** Reclaim all memory of a Stat4Accum structure.
103105*/
103106static void stat4Destructor(void *pOld){
103107 Stat4Accum *p = (Stat4Accum*)pOld;
103108#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103109 int i;
103110 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
103111 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
103112 sampleClear(p->db, &p->current);
103113#endif
103114 sqlite3DbFree(p->db, p);
103115}
103116
103117/*
103118** Implementation of the stat_init(N,K,C) SQL function. The three parameters
103119** are:
103120** N: The number of columns in the index including the rowid/pk (note 1)
103121** K: The number of columns in the index excluding the rowid/pk.
103122** C: The number of rows in the index (note 2)
103123**
103124** Note 1: In the special case of the covering index that implements a
103125** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
103126** total number of columns in the table.
103127**
103128** Note 2: C is only used for STAT3 and STAT4.
103129**
103130** For indexes on ordinary rowid tables, N==K+1. But for indexes on
103131** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
103132** PRIMARY KEY of the table. The covering index that implements the
103133** original WITHOUT ROWID table as N==K as a special case.
103134**
103135** This routine allocates the Stat4Accum object in heap memory. The return
103136** value is a pointer to the Stat4Accum object. The datatype of the
103137** return value is BLOB, but it is really just a pointer to the Stat4Accum
103138** object.
103139*/
103140static void statInit(
103141 sqlite3_context *context,
103142 int argc,
103143 sqlite3_value **argv
103144){
103145 Stat4Accum *p;
103146 int nCol; /* Number of columns in index being sampled */
103147 int nKeyCol; /* Number of key columns */
103148 int nColUp; /* nCol rounded up for alignment */
103149 int n; /* Bytes of space to allocate */
103150 sqlite3 *db; /* Database connection */
103151#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103152 int mxSample = SQLITE_STAT4_SAMPLES;
103153#endif
103154
103155 /* Decode the three function arguments */
103156 UNUSED_PARAMETER(argc);
103157 nCol = sqlite3_value_int(argv[0]);
103158 assert( nCol>0 );
103159 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
103160 nKeyCol = sqlite3_value_int(argv[1]);
103161 assert( nKeyCol<=nCol );
103162 assert( nKeyCol>0 );
103163
103164 /* Allocate the space required for the Stat4Accum object */
103165 n = sizeof(*p)
103166 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
103167 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
103168#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103169 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */
103170 + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */
103171 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
103172#endif
103173 ;
103174 db = sqlite3_context_db_handle(context);
103175 p = sqlite3DbMallocZero(db, n);
103176 if( p==0 ){
103177 sqlite3_result_error_nomem(context);
103178 return;
103179 }
103180
103181 p->db = db;
103182 p->nRow = 0;
103183 p->nCol = nCol;
103184 p->nKeyCol = nKeyCol;
103185 p->current.anDLt = (tRowcnt*)&p[1];
103186 p->current.anEq = &p->current.anDLt[nColUp];
103187
103188#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103189 {
103190 u8 *pSpace; /* Allocated space not yet assigned */
103191 int i; /* Used to iterate through p->aSample[] */
103192
103193 p->iGet = -1;
103194 p->mxSample = mxSample;
103195 p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
103196 p->current.anLt = &p->current.anEq[nColUp];
103197 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
103198
103199 /* Set up the Stat4Accum.a[] and aBest[] arrays */
103200 p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
103201 p->aBest = &p->a[mxSample];
103202 pSpace = (u8*)(&p->a[mxSample+nCol]);
103203 for(i=0; i<(mxSample+nCol); i++){
103204 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
103205 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
103206 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
103207 }
103208 assert( (pSpace - (u8*)p)==n );
103209
103210 for(i=0; i<nCol; i++){
103211 p->aBest[i].iCol = i;
103212 }
103213 }
103214#endif
103215
103216 /* Return a pointer to the allocated object to the caller. Note that
103217 ** only the pointer (the 2nd parameter) matters. The size of the object
103218 ** (given by the 3rd parameter) is never used and can be any positive
103219 ** value. */
103220 sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor);
103221}
103222static const FuncDef statInitFuncdef = {
103223 2+IsStat34, /* nArg */
103224 SQLITE_UTF8, /* funcFlags */
103225 0, /* pUserData */
103226 0, /* pNext */
103227 statInit, /* xSFunc */
103228 0, /* xFinalize */
103229 0, 0, /* xValue, xInverse */
103230 "stat_init", /* zName */
103231 {0}
103232};
103233
103234#ifdef SQLITE_ENABLE_STAT4
103235/*
103236** pNew and pOld are both candidate non-periodic samples selected for
103237** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
103238** considering only any trailing columns and the sample hash value, this
103239** function returns true if sample pNew is to be preferred over pOld.
103240** In other words, if we assume that the cardinalities of the selected
103241** column for pNew and pOld are equal, is pNew to be preferred over pOld.
103242**
103243** This function assumes that for each argument sample, the contents of
103244** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
103245*/
103246static int sampleIsBetterPost(
103247 Stat4Accum *pAccum,
103248 Stat4Sample *pNew,
103249 Stat4Sample *pOld
103250){
103251 int nCol = pAccum->nCol;
103252 int i;
103253 assert( pNew->iCol==pOld->iCol );
103254 for(i=pNew->iCol+1; i<nCol; i++){
103255 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
103256 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
103257 }
103258 if( pNew->iHash>pOld->iHash ) return 1;
103259 return 0;
103260}
103261#endif
103262
103263#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103264/*
103265** Return true if pNew is to be preferred over pOld.
103266**
103267** This function assumes that for each argument sample, the contents of
103268** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
103269*/
103270static int sampleIsBetter(
103271 Stat4Accum *pAccum,
103272 Stat4Sample *pNew,
103273 Stat4Sample *pOld
103274){
103275 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
103276 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
103277
103278 assert( pOld->isPSample==0 && pNew->isPSample==0 );
103279 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
103280
103281 if( (nEqNew>nEqOld) ) return 1;
103282#ifdef SQLITE_ENABLE_STAT4
103283 if( nEqNew==nEqOld ){
103284 if( pNew->iCol<pOld->iCol ) return 1;
103285 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
103286 }
103287 return 0;
103288#else
103289 return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
103290#endif
103291}
103292
103293/*
103294** Copy the contents of sample *pNew into the p->a[] array. If necessary,
103295** remove the least desirable sample from p->a[] to make room.
103296*/
103297static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
103298 Stat4Sample *pSample = 0;
103299 int i;
103300
103301 assert( IsStat4 || nEqZero==0 );
103302
103303#ifdef SQLITE_ENABLE_STAT4
103304 /* Stat4Accum.nMaxEqZero is set to the maximum number of leading 0
103305 ** values in the anEq[] array of any sample in Stat4Accum.a[]. In
103306 ** other words, if nMaxEqZero is n, then it is guaranteed that there
103307 ** are no samples with Stat4Sample.anEq[m]==0 for (m>=n). */
103308 if( nEqZero>p->nMaxEqZero ){
103309 p->nMaxEqZero = nEqZero;
103310 }
103311 if( pNew->isPSample==0 ){
103312 Stat4Sample *pUpgrade = 0;
103313 assert( pNew->anEq[pNew->iCol]>0 );
103314
103315 /* This sample is being added because the prefix that ends in column
103316 ** iCol occurs many times in the table. However, if we have already
103317 ** added a sample that shares this prefix, there is no need to add
103318 ** this one. Instead, upgrade the priority of the highest priority
103319 ** existing sample that shares this prefix. */
103320 for(i=p->nSample-1; i>=0; i--){
103321 Stat4Sample *pOld = &p->a[i];
103322 if( pOld->anEq[pNew->iCol]==0 ){
103323 if( pOld->isPSample ) return;
103324 assert( pOld->iCol>pNew->iCol );
103325 assert( sampleIsBetter(p, pNew, pOld) );
103326 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
103327 pUpgrade = pOld;
103328 }
103329 }
103330 }
103331 if( pUpgrade ){
103332 pUpgrade->iCol = pNew->iCol;
103333 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
103334 goto find_new_min;
103335 }
103336 }
103337#endif
103338
103339 /* If necessary, remove sample iMin to make room for the new sample. */
103340 if( p->nSample>=p->mxSample ){
103341 Stat4Sample *pMin = &p->a[p->iMin];
103342 tRowcnt *anEq = pMin->anEq;
103343 tRowcnt *anLt = pMin->anLt;
103344 tRowcnt *anDLt = pMin->anDLt;
103345 sampleClear(p->db, pMin);
103346 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
103347 pSample = &p->a[p->nSample-1];
103348 pSample->nRowid = 0;
103349 pSample->anEq = anEq;
103350 pSample->anDLt = anDLt;
103351 pSample->anLt = anLt;
103352 p->nSample = p->mxSample-1;
103353 }
103354
103355 /* The "rows less-than" for the rowid column must be greater than that
103356 ** for the last sample in the p->a[] array. Otherwise, the samples would
103357 ** be out of order. */
103358#ifdef SQLITE_ENABLE_STAT4
103359 assert( p->nSample==0
103360 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
103361#endif
103362
103363 /* Insert the new sample */
103364 pSample = &p->a[p->nSample];
103365 sampleCopy(p, pSample, pNew);
103366 p->nSample++;
103367
103368 /* Zero the first nEqZero entries in the anEq[] array. */
103369 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
103370
103371#ifdef SQLITE_ENABLE_STAT4
103372 find_new_min:
103373#endif
103374 if( p->nSample>=p->mxSample ){
103375 int iMin = -1;
103376 for(i=0; i<p->mxSample; i++){
103377 if( p->a[i].isPSample ) continue;
103378 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
103379 iMin = i;
103380 }
103381 }
103382 assert( iMin>=0 );
103383 p->iMin = iMin;
103384 }
103385}
103386#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
103387
103388/*
103389** Field iChng of the index being scanned has changed. So at this point
103390** p->current contains a sample that reflects the previous row of the
103391** index. The value of anEq[iChng] and subsequent anEq[] elements are
103392** correct at this point.
103393*/
103394static void samplePushPrevious(Stat4Accum *p, int iChng){
103395#ifdef SQLITE_ENABLE_STAT4
103396 int i;
103397
103398 /* Check if any samples from the aBest[] array should be pushed
103399 ** into IndexSample.a[] at this point. */
103400 for(i=(p->nCol-2); i>=iChng; i--){
103401 Stat4Sample *pBest = &p->aBest[i];
103402 pBest->anEq[i] = p->current.anEq[i];
103403 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
103404 sampleInsert(p, pBest, i);
103405 }
103406 }
103407
103408 /* Check that no sample contains an anEq[] entry with an index of
103409 ** p->nMaxEqZero or greater set to zero. */
103410 for(i=p->nSample-1; i>=0; i--){
103411 int j;
103412 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
103413 }
103414
103415 /* Update the anEq[] fields of any samples already collected. */
103416 if( iChng<p->nMaxEqZero ){
103417 for(i=p->nSample-1; i>=0; i--){
103418 int j;
103419 for(j=iChng; j<p->nCol; j++){
103420 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
103421 }
103422 }
103423 p->nMaxEqZero = iChng;
103424 }
103425#endif
103426
103427#if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
103428 if( iChng==0 ){
103429 tRowcnt nLt = p->current.anLt[0];
103430 tRowcnt nEq = p->current.anEq[0];
103431
103432 /* Check if this is to be a periodic sample. If so, add it. */
103433 if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
103434 p->current.isPSample = 1;
103435 sampleInsert(p, &p->current, 0);
103436 p->current.isPSample = 0;
103437 }else
103438
103439 /* Or if it is a non-periodic sample. Add it in this case too. */
103440 if( p->nSample<p->mxSample
103441 || sampleIsBetter(p, &p->current, &p->a[p->iMin])
103442 ){
103443 sampleInsert(p, &p->current, 0);
103444 }
103445 }
103446#endif
103447
103448#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
103449 UNUSED_PARAMETER( p );
103450 UNUSED_PARAMETER( iChng );
103451#endif
103452}
103453
103454/*
103455** Implementation of the stat_push SQL function: stat_push(P,C,R)
103456** Arguments:
103457**
103458** P Pointer to the Stat4Accum object created by stat_init()
103459** C Index of left-most column to differ from previous row
103460** R Rowid for the current row. Might be a key record for
103461** WITHOUT ROWID tables.
103462**
103463** This SQL function always returns NULL. It's purpose it to accumulate
103464** statistical data and/or samples in the Stat4Accum object about the
103465** index being analyzed. The stat_get() SQL function will later be used to
103466** extract relevant information for constructing the sqlite_statN tables.
103467**
103468** The R parameter is only used for STAT3 and STAT4
103469*/
103470static void statPush(
103471 sqlite3_context *context,
103472 int argc,
103473 sqlite3_value **argv
103474){
103475 int i;
103476
103477 /* The three function arguments */
103478 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
103479 int iChng = sqlite3_value_int(argv[1]);
103480
103481 UNUSED_PARAMETER( argc );
103482 UNUSED_PARAMETER( context );
103483 assert( p->nCol>0 );
103484 assert( iChng<p->nCol );
103485
103486 if( p->nRow==0 ){
103487 /* This is the first call to this function. Do initialization. */
103488 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
103489 }else{
103490 /* Second and subsequent calls get processed here */
103491 samplePushPrevious(p, iChng);
103492
103493 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
103494 ** to the current row of the index. */
103495 for(i=0; i<iChng; i++){
103496 p->current.anEq[i]++;
103497 }
103498 for(i=iChng; i<p->nCol; i++){
103499 p->current.anDLt[i]++;
103500#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103501 p->current.anLt[i] += p->current.anEq[i];
103502#endif
103503 p->current.anEq[i] = 1;
103504 }
103505 }
103506 p->nRow++;
103507#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103508 if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
103509 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
103510 }else{
103511 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
103512 sqlite3_value_blob(argv[2]));
103513 }
103514 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
103515#endif
103516
103517#ifdef SQLITE_ENABLE_STAT4
103518 {
103519 tRowcnt nLt = p->current.anLt[p->nCol-1];
103520
103521 /* Check if this is to be a periodic sample. If so, add it. */
103522 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
103523 p->current.isPSample = 1;
103524 p->current.iCol = 0;
103525 sampleInsert(p, &p->current, p->nCol-1);
103526 p->current.isPSample = 0;
103527 }
103528
103529 /* Update the aBest[] array. */
103530 for(i=0; i<(p->nCol-1); i++){
103531 p->current.iCol = i;
103532 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
103533 sampleCopy(p, &p->aBest[i], &p->current);
103534 }
103535 }
103536 }
103537#endif
103538}
103539static const FuncDef statPushFuncdef = {
103540 2+IsStat34, /* nArg */
103541 SQLITE_UTF8, /* funcFlags */
103542 0, /* pUserData */
103543 0, /* pNext */
103544 statPush, /* xSFunc */
103545 0, /* xFinalize */
103546 0, 0, /* xValue, xInverse */
103547 "stat_push", /* zName */
103548 {0}
103549};
103550
103551#define STAT_GET_STAT1 0 /* "stat" column of stat1 table */
103552#define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */
103553#define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */
103554#define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
103555#define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
103556
103557/*
103558** Implementation of the stat_get(P,J) SQL function. This routine is
103559** used to query statistical information that has been gathered into
103560** the Stat4Accum object by prior calls to stat_push(). The P parameter
103561** has type BLOB but it is really just a pointer to the Stat4Accum object.
103562** The content to returned is determined by the parameter J
103563** which is one of the STAT_GET_xxxx values defined above.
103564**
103565** The stat_get(P,J) function is not available to generic SQL. It is
103566** inserted as part of a manually constructed bytecode program. (See
103567** the callStatGet() routine below.) It is guaranteed that the P
103568** parameter will always be a poiner to a Stat4Accum object, never a
103569** NULL.
103570**
103571** If neither STAT3 nor STAT4 are enabled, then J is always
103572** STAT_GET_STAT1 and is hence omitted and this routine becomes
103573** a one-parameter function, stat_get(P), that always returns the
103574** stat1 table entry information.
103575*/
103576static void statGet(
103577 sqlite3_context *context,
103578 int argc,
103579 sqlite3_value **argv
103580){
103581 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
103582#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103583 /* STAT3 and STAT4 have a parameter on this routine. */
103584 int eCall = sqlite3_value_int(argv[1]);
103585 assert( argc==2 );
103586 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
103587 || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
103588 || eCall==STAT_GET_NDLT
103589 );
103590 if( eCall==STAT_GET_STAT1 )
103591#else
103592 assert( argc==1 );
103593#endif
103594 {
103595 /* Return the value to store in the "stat" column of the sqlite_stat1
103596 ** table for this index.
103597 **
103598 ** The value is a string composed of a list of integers describing
103599 ** the index. The first integer in the list is the total number of
103600 ** entries in the index. There is one additional integer in the list
103601 ** for each indexed column. This additional integer is an estimate of
103602 ** the number of rows matched by a stabbing query on the index using
103603 ** a key with the corresponding number of fields. In other words,
103604 ** if the index is on columns (a,b) and the sqlite_stat1 value is
103605 ** "100 10 2", then SQLite estimates that:
103606 **
103607 ** * the index contains 100 rows,
103608 ** * "WHERE a=?" matches 10 rows, and
103609 ** * "WHERE a=? AND b=?" matches 2 rows.
103610 **
103611 ** If D is the count of distinct values and K is the total number of
103612 ** rows, then each estimate is computed as:
103613 **
103614 ** I = (K+D-1)/D
103615 */
103616 char *z;
103617 int i;
103618
103619 char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 );
103620 if( zRet==0 ){
103621 sqlite3_result_error_nomem(context);
103622 return;
103623 }
103624
103625 sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
103626 z = zRet + sqlite3Strlen30(zRet);
103627 for(i=0; i<p->nKeyCol; i++){
103628 u64 nDistinct = p->current.anDLt[i] + 1;
103629 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
103630 sqlite3_snprintf(24, z, " %llu", iVal);
103631 z += sqlite3Strlen30(z);
103632 assert( p->current.anEq[i] );
103633 }
103634 assert( z[0]=='\0' && z>zRet );
103635
103636 sqlite3_result_text(context, zRet, -1, sqlite3_free);
103637 }
103638#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103639 else if( eCall==STAT_GET_ROWID ){
103640 if( p->iGet<0 ){
103641 samplePushPrevious(p, 0);
103642 p->iGet = 0;
103643 }
103644 if( p->iGet<p->nSample ){
103645 Stat4Sample *pS = p->a + p->iGet;
103646 if( pS->nRowid==0 ){
103647 sqlite3_result_int64(context, pS->u.iRowid);
103648 }else{
103649 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
103650 SQLITE_TRANSIENT);
103651 }
103652 }
103653 }else{
103654 tRowcnt *aCnt = 0;
103655
103656 assert( p->iGet<p->nSample );
103657 switch( eCall ){
103658 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
103659 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
103660 default: {
103661 aCnt = p->a[p->iGet].anDLt;
103662 p->iGet++;
103663 break;
103664 }
103665 }
103666
103667 if( IsStat3 ){
103668 sqlite3_result_int64(context, (i64)aCnt[0]);
103669 }else{
103670 char *zRet = sqlite3MallocZero(p->nCol * 25);
103671 if( zRet==0 ){
103672 sqlite3_result_error_nomem(context);
103673 }else{
103674 int i;
103675 char *z = zRet;
103676 for(i=0; i<p->nCol; i++){
103677 sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
103678 z += sqlite3Strlen30(z);
103679 }
103680 assert( z[0]=='\0' && z>zRet );
103681 z[-1] = '\0';
103682 sqlite3_result_text(context, zRet, -1, sqlite3_free);
103683 }
103684 }
103685 }
103686#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
103687#ifndef SQLITE_DEBUG
103688 UNUSED_PARAMETER( argc );
103689#endif
103690}
103691static const FuncDef statGetFuncdef = {
103692 1+IsStat34, /* nArg */
103693 SQLITE_UTF8, /* funcFlags */
103694 0, /* pUserData */
103695 0, /* pNext */
103696 statGet, /* xSFunc */
103697 0, /* xFinalize */
103698 0, 0, /* xValue, xInverse */
103699 "stat_get", /* zName */
103700 {0}
103701};
103702
103703static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
103704 assert( regOut!=regStat4 && regOut!=regStat4+1 );
103705#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103706 sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
103707#elif SQLITE_DEBUG
103708 assert( iParam==STAT_GET_STAT1 );
103709#else
103710 UNUSED_PARAMETER( iParam );
103711#endif
103712 sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4, regOut,
103713 (char*)&statGetFuncdef, P4_FUNCDEF);
103714 sqlite3VdbeChangeP5(v, 1 + IsStat34);
103715}
103716
103717/*
103718** Generate code to do an analysis of all indices associated with
103719** a single table.
103720*/
103721static void analyzeOneTable(
103722 Parse *pParse, /* Parser context */
103723 Table *pTab, /* Table whose indices are to be analyzed */
103724 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
103725 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
103726 int iMem, /* Available memory locations begin here */
103727 int iTab /* Next available cursor */
103728){
103729 sqlite3 *db = pParse->db; /* Database handle */
103730 Index *pIdx; /* An index to being analyzed */
103731 int iIdxCur; /* Cursor open on index being analyzed */
103732 int iTabCur; /* Table cursor */
103733 Vdbe *v; /* The virtual machine being built up */
103734 int i; /* Loop counter */
103735 int jZeroRows = -1; /* Jump from here if number of rows is zero */
103736 int iDb; /* Index of database containing pTab */
103737 u8 needTableCnt = 1; /* True to count the table */
103738 int regNewRowid = iMem++; /* Rowid for the inserted record */
103739 int regStat4 = iMem++; /* Register to hold Stat4Accum object */
103740 int regChng = iMem++; /* Index of changed index field */
103741#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103742 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
103743#endif
103744 int regTemp = iMem++; /* Temporary use register */
103745 int regTabname = iMem++; /* Register containing table name */
103746 int regIdxname = iMem++; /* Register containing index name */
103747 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
103748 int regPrev = iMem; /* MUST BE LAST (see below) */
103749#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
103750 Table *pStat1 = 0;
103751#endif
103752
103753 pParse->nMem = MAX(pParse->nMem, iMem);
103754 v = sqlite3GetVdbe(pParse);
103755 if( v==0 || NEVER(pTab==0) ){
103756 return;
103757 }
103758 if( pTab->tnum==0 ){
103759 /* Do not gather statistics on views or virtual tables */
103760 return;
103761 }
103762 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
103763 /* Do not gather statistics on system tables */
103764 return;
103765 }
103766 assert( sqlite3BtreeHoldsAllMutexes(db) );
103767 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
103768 assert( iDb>=0 );
103769 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
103770#ifndef SQLITE_OMIT_AUTHORIZATION
103771 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
103772 db->aDb[iDb].zDbSName ) ){
103773 return;
103774 }
103775#endif
103776
103777#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
103778 if( db->xPreUpdateCallback ){
103779 pStat1 = (Table*)sqlite3DbMallocZero(db, sizeof(Table) + 13);
103780 if( pStat1==0 ) return;
103781 pStat1->zName = (char*)&pStat1[1];
103782 memcpy(pStat1->zName, "sqlite_stat1", 13);
103783 pStat1->nCol = 3;
103784 pStat1->iPKey = -1;
103785 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNBLOB);
103786 }
103787#endif
103788
103789 /* Establish a read-lock on the table at the shared-cache level.
103790 ** Open a read-only cursor on the table. Also allocate a cursor number
103791 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
103792 ** this time though. */
103793 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
103794 iTabCur = iTab++;
103795 iIdxCur = iTab++;
103796 pParse->nTab = MAX(pParse->nTab, iTab);
103797 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
103798 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
103799
103800 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
103801 int nCol; /* Number of columns in pIdx. "N" */
103802 int addrRewind; /* Address of "OP_Rewind iIdxCur" */
103803 int addrNextRow; /* Address of "next_row:" */
103804 const char *zIdxName; /* Name of the index */
103805 int nColTest; /* Number of columns to test for changes */
103806
103807 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
103808 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
103809 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIdx) ){
103810 nCol = pIdx->nKeyCol;
103811 zIdxName = pTab->zName;
103812 nColTest = nCol - 1;
103813 }else{
103814 nCol = pIdx->nColumn;
103815 zIdxName = pIdx->zName;
103816 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
103817 }
103818
103819 /* Populate the register containing the index name. */
103820 sqlite3VdbeLoadString(v, regIdxname, zIdxName);
103821 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
103822
103823 /*
103824 ** Pseudo-code for loop that calls stat_push():
103825 **
103826 ** Rewind csr
103827 ** if eof(csr) goto end_of_scan;
103828 ** regChng = 0
103829 ** goto chng_addr_0;
103830 **
103831 ** next_row:
103832 ** regChng = 0
103833 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
103834 ** regChng = 1
103835 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
103836 ** ...
103837 ** regChng = N
103838 ** goto chng_addr_N
103839 **
103840 ** chng_addr_0:
103841 ** regPrev(0) = idx(0)
103842 ** chng_addr_1:
103843 ** regPrev(1) = idx(1)
103844 ** ...
103845 **
103846 ** endDistinctTest:
103847 ** regRowid = idx(rowid)
103848 ** stat_push(P, regChng, regRowid)
103849 ** Next csr
103850 ** if !eof(csr) goto next_row;
103851 **
103852 ** end_of_scan:
103853 */
103854
103855 /* Make sure there are enough memory cells allocated to accommodate
103856 ** the regPrev array and a trailing rowid (the rowid slot is required
103857 ** when building a record to insert into the sample column of
103858 ** the sqlite_stat4 table. */
103859 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
103860
103861 /* Open a read-only cursor on the index being analyzed. */
103862 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
103863 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
103864 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
103865 VdbeComment((v, "%s", pIdx->zName));
103866
103867 /* Invoke the stat_init() function. The arguments are:
103868 **
103869 ** (1) the number of columns in the index including the rowid
103870 ** (or for a WITHOUT ROWID table, the number of PK columns),
103871 ** (2) the number of columns in the key without the rowid/pk
103872 ** (3) the number of rows in the index,
103873 **
103874 **
103875 ** The third argument is only used for STAT3 and STAT4
103876 */
103877#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103878 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
103879#endif
103880 sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
103881 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
103882 sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4+1, regStat4,
103883 (char*)&statInitFuncdef, P4_FUNCDEF);
103884 sqlite3VdbeChangeP5(v, 2+IsStat34);
103885
103886 /* Implementation of the following:
103887 **
103888 ** Rewind csr
103889 ** if eof(csr) goto end_of_scan;
103890 ** regChng = 0
103891 ** goto next_push_0;
103892 **
103893 */
103894 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
103895 VdbeCoverage(v);
103896 sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
103897 addrNextRow = sqlite3VdbeCurrentAddr(v);
103898
103899 if( nColTest>0 ){
103900 int endDistinctTest = sqlite3VdbeMakeLabel(v);
103901 int *aGotoChng; /* Array of jump instruction addresses */
103902 aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
103903 if( aGotoChng==0 ) continue;
103904
103905 /*
103906 ** next_row:
103907 ** regChng = 0
103908 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
103909 ** regChng = 1
103910 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
103911 ** ...
103912 ** regChng = N
103913 ** goto endDistinctTest
103914 */
103915 sqlite3VdbeAddOp0(v, OP_Goto);
103916 addrNextRow = sqlite3VdbeCurrentAddr(v);
103917 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
103918 /* For a single-column UNIQUE index, once we have found a non-NULL
103919 ** row, we know that all the rest will be distinct, so skip
103920 ** subsequent distinctness tests. */
103921 sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest);
103922 VdbeCoverage(v);
103923 }
103924 for(i=0; i<nColTest; i++){
103925 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
103926 sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
103927 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
103928 aGotoChng[i] =
103929 sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
103930 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
103931 VdbeCoverage(v);
103932 }
103933 sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng);
103934 sqlite3VdbeGoto(v, endDistinctTest);
103935
103936
103937 /*
103938 ** chng_addr_0:
103939 ** regPrev(0) = idx(0)
103940 ** chng_addr_1:
103941 ** regPrev(1) = idx(1)
103942 ** ...
103943 */
103944 sqlite3VdbeJumpHere(v, addrNextRow-1);
103945 for(i=0; i<nColTest; i++){
103946 sqlite3VdbeJumpHere(v, aGotoChng[i]);
103947 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
103948 }
103949 sqlite3VdbeResolveLabel(v, endDistinctTest);
103950 sqlite3DbFree(db, aGotoChng);
103951 }
103952
103953 /*
103954 ** chng_addr_N:
103955 ** regRowid = idx(rowid) // STAT34 only
103956 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only
103957 ** Next csr
103958 ** if !eof(csr) goto next_row;
103959 */
103960#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103961 assert( regRowid==(regStat4+2) );
103962 if( HasRowid(pTab) ){
103963 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
103964 }else{
103965 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
103966 int j, k, regKey;
103967 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
103968 for(j=0; j<pPk->nKeyCol; j++){
103969 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
103970 assert( k>=0 && k<pIdx->nColumn );
103971 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
103972 VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
103973 }
103974 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
103975 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
103976 }
103977#endif
103978 assert( regChng==(regStat4+1) );
103979 sqlite3VdbeAddOp4(v, OP_Function0, 1, regStat4, regTemp,
103980 (char*)&statPushFuncdef, P4_FUNCDEF);
103981 sqlite3VdbeChangeP5(v, 2+IsStat34);
103982 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
103983
103984 /* Add the entry to the stat1 table. */
103985 callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
103986 assert( "BBB"[0]==SQLITE_AFF_TEXT );
103987 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
103988 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
103989 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
103990#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
103991 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
103992#endif
103993 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
103994
103995 /* Add the entries to the stat3 or stat4 table. */
103996#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
103997 {
103998 int regEq = regStat1;
103999 int regLt = regStat1+1;
104000 int regDLt = regStat1+2;
104001 int regSample = regStat1+3;
104002 int regCol = regStat1+4;
104003 int regSampleRowid = regCol + nCol;
104004 int addrNext;
104005 int addrIsNull;
104006 u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
104007
104008 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
104009
104010 addrNext = sqlite3VdbeCurrentAddr(v);
104011 callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
104012 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
104013 VdbeCoverage(v);
104014 callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
104015 callStatGet(v, regStat4, STAT_GET_NLT, regLt);
104016 callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
104017 sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
104018 VdbeCoverage(v);
104019#ifdef SQLITE_ENABLE_STAT3
104020 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, 0, regSample);
104021#else
104022 for(i=0; i<nCol; i++){
104023 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
104024 }
104025 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
104026#endif
104027 sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
104028 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
104029 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
104030 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
104031 sqlite3VdbeJumpHere(v, addrIsNull);
104032 }
104033#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
104034
104035 /* End of analysis */
104036 sqlite3VdbeJumpHere(v, addrRewind);
104037 }
104038
104039
104040 /* Create a single sqlite_stat1 entry containing NULL as the index
104041 ** name and the row count as the content.
104042 */
104043 if( pOnlyIdx==0 && needTableCnt ){
104044 VdbeComment((v, "%s", pTab->zName));
104045 sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
104046 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
104047 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
104048 assert( "BBB"[0]==SQLITE_AFF_TEXT );
104049 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
104050 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
104051 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
104052 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
104053#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
104054 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
104055#endif
104056 sqlite3VdbeJumpHere(v, jZeroRows);
104057 }
104058}
104059
104060
104061/*
104062** Generate code that will cause the most recent index analysis to
104063** be loaded into internal hash tables where is can be used.
104064*/
104065static void loadAnalysis(Parse *pParse, int iDb){
104066 Vdbe *v = sqlite3GetVdbe(pParse);
104067 if( v ){
104068 sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
104069 }
104070}
104071
104072/*
104073** Generate code that will do an analysis of an entire database
104074*/
104075static void analyzeDatabase(Parse *pParse, int iDb){
104076 sqlite3 *db = pParse->db;
104077 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
104078 HashElem *k;
104079 int iStatCur;
104080 int iMem;
104081 int iTab;
104082
104083 sqlite3BeginWriteOperation(pParse, 0, iDb);
104084 iStatCur = pParse->nTab;
104085 pParse->nTab += 3;
104086 openStatTable(pParse, iDb, iStatCur, 0, 0);
104087 iMem = pParse->nMem+1;
104088 iTab = pParse->nTab;
104089 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
104090 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
104091 Table *pTab = (Table*)sqliteHashData(k);
104092 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
104093 }
104094 loadAnalysis(pParse, iDb);
104095}
104096
104097/*
104098** Generate code that will do an analysis of a single table in
104099** a database. If pOnlyIdx is not NULL then it is a single index
104100** in pTab that should be analyzed.
104101*/
104102static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
104103 int iDb;
104104 int iStatCur;
104105
104106 assert( pTab!=0 );
104107 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
104108 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
104109 sqlite3BeginWriteOperation(pParse, 0, iDb);
104110 iStatCur = pParse->nTab;
104111 pParse->nTab += 3;
104112 if( pOnlyIdx ){
104113 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
104114 }else{
104115 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
104116 }
104117 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
104118 loadAnalysis(pParse, iDb);
104119}
104120
104121/*
104122** Generate code for the ANALYZE command. The parser calls this routine
104123** when it recognizes an ANALYZE command.
104124**
104125** ANALYZE -- 1
104126** ANALYZE <database> -- 2
104127** ANALYZE ?<database>.?<tablename> -- 3
104128**
104129** Form 1 causes all indices in all attached databases to be analyzed.
104130** Form 2 analyzes all indices the single database named.
104131** Form 3 analyzes all indices associated with the named table.
104132*/
104133SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
104134 sqlite3 *db = pParse->db;
104135 int iDb;
104136 int i;
104137 char *z, *zDb;
104138 Table *pTab;
104139 Index *pIdx;
104140 Token *pTableName;
104141 Vdbe *v;
104142
104143 /* Read the database schema. If an error occurs, leave an error message
104144 ** and code in pParse and return NULL. */
104145 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
104146 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
104147 return;
104148 }
104149
104150 assert( pName2!=0 || pName1==0 );
104151 if( pName1==0 ){
104152 /* Form 1: Analyze everything */
104153 for(i=0; i<db->nDb; i++){
104154 if( i==1 ) continue; /* Do not analyze the TEMP database */
104155 analyzeDatabase(pParse, i);
104156 }
104157 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
104158 /* Analyze the schema named as the argument */
104159 analyzeDatabase(pParse, iDb);
104160 }else{
104161 /* Form 3: Analyze the table or index named as an argument */
104162 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
104163 if( iDb>=0 ){
104164 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
104165 z = sqlite3NameFromToken(db, pTableName);
104166 if( z ){
104167 if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
104168 analyzeTable(pParse, pIdx->pTable, pIdx);
104169 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
104170 analyzeTable(pParse, pTab, 0);
104171 }
104172 sqlite3DbFree(db, z);
104173 }
104174 }
104175 }
104176 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
104177 sqlite3VdbeAddOp0(v, OP_Expire);
104178 }
104179}
104180
104181/*
104182** Used to pass information from the analyzer reader through to the
104183** callback routine.
104184*/
104185typedef struct analysisInfo analysisInfo;
104186struct analysisInfo {
104187 sqlite3 *db;
104188 const char *zDatabase;
104189};
104190
104191/*
104192** The first argument points to a nul-terminated string containing a
104193** list of space separated integers. Read the first nOut of these into
104194** the array aOut[].
104195*/
104196static void decodeIntArray(
104197 char *zIntArray, /* String containing int array to decode */
104198 int nOut, /* Number of slots in aOut[] */
104199 tRowcnt *aOut, /* Store integers here */
104200 LogEst *aLog, /* Or, if aOut==0, here */
104201 Index *pIndex /* Handle extra flags for this index, if not NULL */
104202){
104203 char *z = zIntArray;
104204 int c;
104205 int i;
104206 tRowcnt v;
104207
104208#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104209 if( z==0 ) z = "";
104210#else
104211 assert( z!=0 );
104212#endif
104213 for(i=0; *z && i<nOut; i++){
104214 v = 0;
104215 while( (c=z[0])>='0' && c<='9' ){
104216 v = v*10 + c - '0';
104217 z++;
104218 }
104219#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104220 if( aOut ) aOut[i] = v;
104221 if( aLog ) aLog[i] = sqlite3LogEst(v);
104222#else
104223 assert( aOut==0 );
104224 UNUSED_PARAMETER(aOut);
104225 assert( aLog!=0 );
104226 aLog[i] = sqlite3LogEst(v);
104227#endif
104228 if( *z==' ' ) z++;
104229 }
104230#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
104231 assert( pIndex!=0 ); {
104232#else
104233 if( pIndex ){
104234#endif
104235 pIndex->bUnordered = 0;
104236 pIndex->noSkipScan = 0;
104237 while( z[0] ){
104238 if( sqlite3_strglob("unordered*", z)==0 ){
104239 pIndex->bUnordered = 1;
104240 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
104241 pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
104242 }else if( sqlite3_strglob("noskipscan*", z)==0 ){
104243 pIndex->noSkipScan = 1;
104244 }
104245#ifdef SQLITE_ENABLE_COSTMULT
104246 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
104247 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
104248 }
104249#endif
104250 while( z[0]!=0 && z[0]!=' ' ) z++;
104251 while( z[0]==' ' ) z++;
104252 }
104253 }
104254}
104255
104256/*
104257** This callback is invoked once for each index when reading the
104258** sqlite_stat1 table.
104259**
104260** argv[0] = name of the table
104261** argv[1] = name of the index (might be NULL)
104262** argv[2] = results of analysis - on integer for each column
104263**
104264** Entries for which argv[1]==NULL simply record the number of rows in
104265** the table.
104266*/
104267static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
104268 analysisInfo *pInfo = (analysisInfo*)pData;
104269 Index *pIndex;
104270 Table *pTable;
104271 const char *z;
104272
104273 assert( argc==3 );
104274 UNUSED_PARAMETER2(NotUsed, argc);
104275
104276 if( argv==0 || argv[0]==0 || argv[2]==0 ){
104277 return 0;
104278 }
104279 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
104280 if( pTable==0 ){
104281 return 0;
104282 }
104283 if( argv[1]==0 ){
104284 pIndex = 0;
104285 }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
104286 pIndex = sqlite3PrimaryKeyIndex(pTable);
104287 }else{
104288 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
104289 }
104290 z = argv[2];
104291
104292 if( pIndex ){
104293 tRowcnt *aiRowEst = 0;
104294 int nCol = pIndex->nKeyCol+1;
104295#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104296 /* Index.aiRowEst may already be set here if there are duplicate
104297 ** sqlite_stat1 entries for this index. In that case just clobber
104298 ** the old data with the new instead of allocating a new array. */
104299 if( pIndex->aiRowEst==0 ){
104300 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
104301 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
104302 }
104303 aiRowEst = pIndex->aiRowEst;
104304#endif
104305 pIndex->bUnordered = 0;
104306 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
104307 pIndex->hasStat1 = 1;
104308 if( pIndex->pPartIdxWhere==0 ){
104309 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
104310 pTable->tabFlags |= TF_HasStat1;
104311 }
104312 }else{
104313 Index fakeIdx;
104314 fakeIdx.szIdxRow = pTable->szTabRow;
104315#ifdef SQLITE_ENABLE_COSTMULT
104316 fakeIdx.pTable = pTable;
104317#endif
104318 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
104319 pTable->szTabRow = fakeIdx.szIdxRow;
104320 pTable->tabFlags |= TF_HasStat1;
104321 }
104322
104323 return 0;
104324}
104325
104326/*
104327** If the Index.aSample variable is not NULL, delete the aSample[] array
104328** and its contents.
104329*/
104330SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
104331#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104332 if( pIdx->aSample ){
104333 int j;
104334 for(j=0; j<pIdx->nSample; j++){
104335 IndexSample *p = &pIdx->aSample[j];
104336 sqlite3DbFree(db, p->p);
104337 }
104338 sqlite3DbFree(db, pIdx->aSample);
104339 }
104340 if( db && db->pnBytesFreed==0 ){
104341 pIdx->nSample = 0;
104342 pIdx->aSample = 0;
104343 }
104344#else
104345 UNUSED_PARAMETER(db);
104346 UNUSED_PARAMETER(pIdx);
104347#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
104348}
104349
104350#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104351/*
104352** Populate the pIdx->aAvgEq[] array based on the samples currently
104353** stored in pIdx->aSample[].
104354*/
104355static void initAvgEq(Index *pIdx){
104356 if( pIdx ){
104357 IndexSample *aSample = pIdx->aSample;
104358 IndexSample *pFinal = &aSample[pIdx->nSample-1];
104359 int iCol;
104360 int nCol = 1;
104361 if( pIdx->nSampleCol>1 ){
104362 /* If this is stat4 data, then calculate aAvgEq[] values for all
104363 ** sample columns except the last. The last is always set to 1, as
104364 ** once the trailing PK fields are considered all index keys are
104365 ** unique. */
104366 nCol = pIdx->nSampleCol-1;
104367 pIdx->aAvgEq[nCol] = 1;
104368 }
104369 for(iCol=0; iCol<nCol; iCol++){
104370 int nSample = pIdx->nSample;
104371 int i; /* Used to iterate through samples */
104372 tRowcnt sumEq = 0; /* Sum of the nEq values */
104373 tRowcnt avgEq = 0;
104374 tRowcnt nRow; /* Number of rows in index */
104375 i64 nSum100 = 0; /* Number of terms contributing to sumEq */
104376 i64 nDist100; /* Number of distinct values in index */
104377
104378 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
104379 nRow = pFinal->anLt[iCol];
104380 nDist100 = (i64)100 * pFinal->anDLt[iCol];
104381 nSample--;
104382 }else{
104383 nRow = pIdx->aiRowEst[0];
104384 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
104385 }
104386 pIdx->nRowEst0 = nRow;
104387
104388 /* Set nSum to the number of distinct (iCol+1) field prefixes that
104389 ** occur in the stat4 table for this index. Set sumEq to the sum of
104390 ** the nEq values for column iCol for the same set (adding the value
104391 ** only once where there exist duplicate prefixes). */
104392 for(i=0; i<nSample; i++){
104393 if( i==(pIdx->nSample-1)
104394 || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol]
104395 ){
104396 sumEq += aSample[i].anEq[iCol];
104397 nSum100 += 100;
104398 }
104399 }
104400
104401 if( nDist100>nSum100 && sumEq<nRow ){
104402 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
104403 }
104404 if( avgEq==0 ) avgEq = 1;
104405 pIdx->aAvgEq[iCol] = avgEq;
104406 }
104407 }
104408}
104409
104410/*
104411** Look up an index by name. Or, if the name of a WITHOUT ROWID table
104412** is supplied instead, find the PRIMARY KEY index for that table.
104413*/
104414static Index *findIndexOrPrimaryKey(
104415 sqlite3 *db,
104416 const char *zName,
104417 const char *zDb
104418){
104419 Index *pIdx = sqlite3FindIndex(db, zName, zDb);
104420 if( pIdx==0 ){
104421 Table *pTab = sqlite3FindTable(db, zName, zDb);
104422 if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
104423 }
104424 return pIdx;
104425}
104426
104427/*
104428** Load the content from either the sqlite_stat4 or sqlite_stat3 table
104429** into the relevant Index.aSample[] arrays.
104430**
104431** Arguments zSql1 and zSql2 must point to SQL statements that return
104432** data equivalent to the following (statements are different for stat3,
104433** see the caller of this function for details):
104434**
104435** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
104436** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
104437**
104438** where %Q is replaced with the database name before the SQL is executed.
104439*/
104440static int loadStatTbl(
104441 sqlite3 *db, /* Database handle */
104442 int bStat3, /* Assume single column records only */
104443 const char *zSql1, /* SQL statement 1 (see above) */
104444 const char *zSql2, /* SQL statement 2 (see above) */
104445 const char *zDb /* Database name (e.g. "main") */
104446){
104447 int rc; /* Result codes from subroutines */
104448 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
104449 char *zSql; /* Text of the SQL statement */
104450 Index *pPrevIdx = 0; /* Previous index in the loop */
104451 IndexSample *pSample; /* A slot in pIdx->aSample[] */
104452
104453 assert( db->lookaside.bDisable );
104454 zSql = sqlite3MPrintf(db, zSql1, zDb);
104455 if( !zSql ){
104456 return SQLITE_NOMEM_BKPT;
104457 }
104458 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
104459 sqlite3DbFree(db, zSql);
104460 if( rc ) return rc;
104461
104462 while( sqlite3_step(pStmt)==SQLITE_ROW ){
104463 int nIdxCol = 1; /* Number of columns in stat4 records */
104464
104465 char *zIndex; /* Index name */
104466 Index *pIdx; /* Pointer to the index object */
104467 int nSample; /* Number of samples */
104468 int nByte; /* Bytes of space required */
104469 int i; /* Bytes of space required */
104470 tRowcnt *pSpace;
104471
104472 zIndex = (char *)sqlite3_column_text(pStmt, 0);
104473 if( zIndex==0 ) continue;
104474 nSample = sqlite3_column_int(pStmt, 1);
104475 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
104476 assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
104477 /* Index.nSample is non-zero at this point if data has already been
104478 ** loaded from the stat4 table. In this case ignore stat3 data. */
104479 if( pIdx==0 || pIdx->nSample ) continue;
104480 if( bStat3==0 ){
104481 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
104482 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
104483 nIdxCol = pIdx->nKeyCol;
104484 }else{
104485 nIdxCol = pIdx->nColumn;
104486 }
104487 }
104488 pIdx->nSampleCol = nIdxCol;
104489 nByte = sizeof(IndexSample) * nSample;
104490 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
104491 nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
104492
104493 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
104494 if( pIdx->aSample==0 ){
104495 sqlite3_finalize(pStmt);
104496 return SQLITE_NOMEM_BKPT;
104497 }
104498 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
104499 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
104500 for(i=0; i<nSample; i++){
104501 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
104502 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
104503 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
104504 }
104505 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
104506 }
104507 rc = sqlite3_finalize(pStmt);
104508 if( rc ) return rc;
104509
104510 zSql = sqlite3MPrintf(db, zSql2, zDb);
104511 if( !zSql ){
104512 return SQLITE_NOMEM_BKPT;
104513 }
104514 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
104515 sqlite3DbFree(db, zSql);
104516 if( rc ) return rc;
104517
104518 while( sqlite3_step(pStmt)==SQLITE_ROW ){
104519 char *zIndex; /* Index name */
104520 Index *pIdx; /* Pointer to the index object */
104521 int nCol = 1; /* Number of columns in index */
104522
104523 zIndex = (char *)sqlite3_column_text(pStmt, 0);
104524 if( zIndex==0 ) continue;
104525 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
104526 if( pIdx==0 ) continue;
104527 /* This next condition is true if data has already been loaded from
104528 ** the sqlite_stat4 table. In this case ignore stat3 data. */
104529 nCol = pIdx->nSampleCol;
104530 if( bStat3 && nCol>1 ) continue;
104531 if( pIdx!=pPrevIdx ){
104532 initAvgEq(pPrevIdx);
104533 pPrevIdx = pIdx;
104534 }
104535 pSample = &pIdx->aSample[pIdx->nSample];
104536 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
104537 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
104538 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
104539
104540 /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
104541 ** This is in case the sample record is corrupted. In that case, the
104542 ** sqlite3VdbeRecordCompare() may read up to two varints past the
104543 ** end of the allocated buffer before it realizes it is dealing with
104544 ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
104545 ** a buffer overread. */
104546 pSample->n = sqlite3_column_bytes(pStmt, 4);
104547 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
104548 if( pSample->p==0 ){
104549 sqlite3_finalize(pStmt);
104550 return SQLITE_NOMEM_BKPT;
104551 }
104552 if( pSample->n ){
104553 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
104554 }
104555 pIdx->nSample++;
104556 }
104557 rc = sqlite3_finalize(pStmt);
104558 if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
104559 return rc;
104560}
104561
104562/*
104563** Load content from the sqlite_stat4 and sqlite_stat3 tables into
104564** the Index.aSample[] arrays of all indices.
104565*/
104566static int loadStat4(sqlite3 *db, const char *zDb){
104567 int rc = SQLITE_OK; /* Result codes from subroutines */
104568
104569 assert( db->lookaside.bDisable );
104570 if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
104571 rc = loadStatTbl(db, 0,
104572 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
104573 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
104574 zDb
104575 );
104576 }
104577
104578 if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
104579 rc = loadStatTbl(db, 1,
104580 "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx",
104581 "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
104582 zDb
104583 );
104584 }
104585
104586 return rc;
104587}
104588#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
104589
104590/*
104591** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
104592** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
104593** arrays. The contents of sqlite_stat3/4 are used to populate the
104594** Index.aSample[] arrays.
104595**
104596** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
104597** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined
104598** during compilation and the sqlite_stat3/4 table is present, no data is
104599** read from it.
104600**
104601** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the
104602** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
104603** returned. However, in this case, data is read from the sqlite_stat1
104604** table (if it is present) before returning.
104605**
104606** If an OOM error occurs, this function always sets db->mallocFailed.
104607** This means if the caller does not care about other errors, the return
104608** code may be ignored.
104609*/
104610SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
104611 analysisInfo sInfo;
104612 HashElem *i;
104613 char *zSql;
104614 int rc = SQLITE_OK;
104615 Schema *pSchema = db->aDb[iDb].pSchema;
104616
104617 assert( iDb>=0 && iDb<db->nDb );
104618 assert( db->aDb[iDb].pBt!=0 );
104619
104620 /* Clear any prior statistics */
104621 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
104622 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
104623 Table *pTab = sqliteHashData(i);
104624 pTab->tabFlags &= ~TF_HasStat1;
104625 }
104626 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
104627 Index *pIdx = sqliteHashData(i);
104628 pIdx->hasStat1 = 0;
104629#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104630 sqlite3DeleteIndexSamples(db, pIdx);
104631 pIdx->aSample = 0;
104632#endif
104633 }
104634
104635 /* Load new statistics out of the sqlite_stat1 table */
104636 sInfo.db = db;
104637 sInfo.zDatabase = db->aDb[iDb].zDbSName;
104638 if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)!=0 ){
104639 zSql = sqlite3MPrintf(db,
104640 "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
104641 if( zSql==0 ){
104642 rc = SQLITE_NOMEM_BKPT;
104643 }else{
104644 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
104645 sqlite3DbFree(db, zSql);
104646 }
104647 }
104648
104649 /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
104650 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
104651 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
104652 Index *pIdx = sqliteHashData(i);
104653 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
104654 }
104655
104656 /* Load the statistics from the sqlite_stat4 table. */
104657#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104658 if( rc==SQLITE_OK ){
104659 db->lookaside.bDisable++;
104660 rc = loadStat4(db, sInfo.zDatabase);
104661 db->lookaside.bDisable--;
104662 }
104663 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
104664 Index *pIdx = sqliteHashData(i);
104665 sqlite3_free(pIdx->aiRowEst);
104666 pIdx->aiRowEst = 0;
104667 }
104668#endif
104669
104670 if( rc==SQLITE_NOMEM ){
104671 sqlite3OomFault(db);
104672 }
104673 return rc;
104674}
104675
104676
104677#endif /* SQLITE_OMIT_ANALYZE */
104678
104679/************** End of analyze.c *********************************************/
104680/************** Begin file attach.c ******************************************/
104681/*
104682** 2003 April 6
104683**
104684** The author disclaims copyright to this source code. In place of
104685** a legal notice, here is a blessing:
104686**
104687** May you do good and not evil.
104688** May you find forgiveness for yourself and forgive others.
104689** May you share freely, never taking more than you give.
104690**
104691*************************************************************************
104692** This file contains code used to implement the ATTACH and DETACH commands.
104693*/
104694/* #include "sqliteInt.h" */
104695
104696#ifndef SQLITE_OMIT_ATTACH
104697/*
104698** Resolve an expression that was part of an ATTACH or DETACH statement. This
104699** is slightly different from resolving a normal SQL expression, because simple
104700** identifiers are treated as strings, not possible column names or aliases.
104701**
104702** i.e. if the parser sees:
104703**
104704** ATTACH DATABASE abc AS def
104705**
104706** it treats the two expressions as literal strings 'abc' and 'def' instead of
104707** looking for columns of the same name.
104708**
104709** This only applies to the root node of pExpr, so the statement:
104710**
104711** ATTACH DATABASE abc||def AS 'db2'
104712**
104713** will fail because neither abc or def can be resolved.
104714*/
104715static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
104716{
104717 int rc = SQLITE_OK;
104718 if( pExpr ){
104719 if( pExpr->op!=TK_ID ){
104720 rc = sqlite3ResolveExprNames(pName, pExpr);
104721 }else{
104722 pExpr->op = TK_STRING;
104723 }
104724 }
104725 return rc;
104726}
104727
104728/*
104729** An SQL user-function registered to do the work of an ATTACH statement. The
104730** three arguments to the function come directly from an attach statement:
104731**
104732** ATTACH DATABASE x AS y KEY z
104733**
104734** SELECT sqlite_attach(x, y, z)
104735**
104736** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
104737** third argument.
104738**
104739** If the db->init.reopenMemdb flags is set, then instead of attaching a
104740** new database, close the database on db->init.iDb and reopen it as an
104741** empty MemDB.
104742*/
104743static void attachFunc(
104744 sqlite3_context *context,
104745 int NotUsed,
104746 sqlite3_value **argv
104747){
104748 int i;
104749 int rc = 0;
104750 sqlite3 *db = sqlite3_context_db_handle(context);
104751 const char *zName;
104752 const char *zFile;
104753 char *zPath = 0;
104754 char *zErr = 0;
104755 unsigned int flags;
104756 Db *aNew; /* New array of Db pointers */
104757 Db *pNew; /* Db object for the newly attached database */
104758 char *zErrDyn = 0;
104759 sqlite3_vfs *pVfs;
104760
104761 UNUSED_PARAMETER(NotUsed);
104762 zFile = (const char *)sqlite3_value_text(argv[0]);
104763 zName = (const char *)sqlite3_value_text(argv[1]);
104764 if( zFile==0 ) zFile = "";
104765 if( zName==0 ) zName = "";
104766
104767#ifdef SQLITE_ENABLE_DESERIALIZE
104768# define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
104769#else
104770# define REOPEN_AS_MEMDB(db) (0)
104771#endif
104772
104773 if( REOPEN_AS_MEMDB(db) ){
104774 /* This is not a real ATTACH. Instead, this routine is being called
104775 ** from sqlite3_deserialize() to close database db->init.iDb and
104776 ** reopen it as a MemDB */
104777 pVfs = sqlite3_vfs_find("memdb");
104778 if( pVfs==0 ) return;
104779 pNew = &db->aDb[db->init.iDb];
104780 if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
104781 pNew->pBt = 0;
104782 pNew->pSchema = 0;
104783 rc = sqlite3BtreeOpen(pVfs, "x", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
104784 }else{
104785 /* This is a real ATTACH
104786 **
104787 ** Check for the following errors:
104788 **
104789 ** * Too many attached databases,
104790 ** * Transaction currently open
104791 ** * Specified database name already being used.
104792 */
104793 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
104794 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
104795 db->aLimit[SQLITE_LIMIT_ATTACHED]
104796 );
104797 goto attach_error;
104798 }
104799 for(i=0; i<db->nDb; i++){
104800 char *z = db->aDb[i].zDbSName;
104801 assert( z && zName );
104802 if( sqlite3StrICmp(z, zName)==0 ){
104803 zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
104804 goto attach_error;
104805 }
104806 }
104807
104808 /* Allocate the new entry in the db->aDb[] array and initialize the schema
104809 ** hash tables.
104810 */
104811 if( db->aDb==db->aDbStatic ){
104812 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
104813 if( aNew==0 ) return;
104814 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
104815 }else{
104816 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
104817 if( aNew==0 ) return;
104818 }
104819 db->aDb = aNew;
104820 pNew = &db->aDb[db->nDb];
104821 memset(pNew, 0, sizeof(*pNew));
104822
104823 /* Open the database file. If the btree is successfully opened, use
104824 ** it to obtain the database schema. At this point the schema may
104825 ** or may not be initialized.
104826 */
104827 flags = db->openFlags;
104828 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
104829 if( rc!=SQLITE_OK ){
104830 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
104831 sqlite3_result_error(context, zErr, -1);
104832 sqlite3_free(zErr);
104833 return;
104834 }
104835 assert( pVfs );
104836 flags |= SQLITE_OPEN_MAIN_DB;
104837 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
104838 sqlite3_free( zPath );
104839 db->nDb++;
104840 }
104841 db->noSharedCache = 0;
104842 if( rc==SQLITE_CONSTRAINT ){
104843 rc = SQLITE_ERROR;
104844 zErrDyn = sqlite3MPrintf(db, "database is already attached");
104845 }else if( rc==SQLITE_OK ){
104846 Pager *pPager;
104847 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
104848 if( !pNew->pSchema ){
104849 rc = SQLITE_NOMEM_BKPT;
104850 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
104851 zErrDyn = sqlite3MPrintf(db,
104852 "attached databases must use the same text encoding as main database");
104853 rc = SQLITE_ERROR;
104854 }
104855 sqlite3BtreeEnter(pNew->pBt);
104856 pPager = sqlite3BtreePager(pNew->pBt);
104857 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
104858 sqlite3BtreeSecureDelete(pNew->pBt,
104859 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
104860#ifndef SQLITE_OMIT_PAGER_PRAGMAS
104861 sqlite3BtreeSetPagerFlags(pNew->pBt,
104862 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
104863#endif
104864 sqlite3BtreeLeave(pNew->pBt);
104865 }
104866 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
104867 if( !REOPEN_AS_MEMDB(db) ) pNew->zDbSName = sqlite3DbStrDup(db, zName);
104868 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
104869 rc = SQLITE_NOMEM_BKPT;
104870 }
104871
104872
104873#ifdef SQLITE_HAS_CODEC
104874 if( rc==SQLITE_OK ){
104875 extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
104876 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
104877 int nKey;
104878 char *zKey;
104879 int t = sqlite3_value_type(argv[2]);
104880 switch( t ){
104881 case SQLITE_INTEGER:
104882 case SQLITE_FLOAT:
104883 zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
104884 rc = SQLITE_ERROR;
104885 break;
104886
104887 case SQLITE_TEXT:
104888 case SQLITE_BLOB:
104889 nKey = sqlite3_value_bytes(argv[2]);
104890 zKey = (char *)sqlite3_value_blob(argv[2]);
104891 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
104892 break;
104893
104894 case SQLITE_NULL:
104895 /* No key specified. Use the key from the main database */
104896 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
104897 if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
104898 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
104899 }
104900 break;
104901 }
104902 }
104903#endif
104904
104905 /* If the file was opened successfully, read the schema for the new database.
104906 ** If this fails, or if opening the file failed, then close the file and
104907 ** remove the entry from the db->aDb[] array. i.e. put everything back the
104908 ** way we found it.
104909 */
104910 if( rc==SQLITE_OK ){
104911 sqlite3BtreeEnterAll(db);
104912 db->init.iDb = 0;
104913 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
104914 rc = sqlite3Init(db, &zErrDyn);
104915 sqlite3BtreeLeaveAll(db);
104916 assert( zErrDyn==0 || rc!=SQLITE_OK );
104917 }
104918#ifdef SQLITE_USER_AUTHENTICATION
104919 if( rc==SQLITE_OK ){
104920 u8 newAuth = 0;
104921 rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
104922 if( newAuth<db->auth.authLevel ){
104923 rc = SQLITE_AUTH_USER;
104924 }
104925 }
104926#endif
104927 if( rc ){
104928 if( !REOPEN_AS_MEMDB(db) ){
104929 int iDb = db->nDb - 1;
104930 assert( iDb>=2 );
104931 if( db->aDb[iDb].pBt ){
104932 sqlite3BtreeClose(db->aDb[iDb].pBt);
104933 db->aDb[iDb].pBt = 0;
104934 db->aDb[iDb].pSchema = 0;
104935 }
104936 sqlite3ResetAllSchemasOfConnection(db);
104937 db->nDb = iDb;
104938 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
104939 sqlite3OomFault(db);
104940 sqlite3DbFree(db, zErrDyn);
104941 zErrDyn = sqlite3MPrintf(db, "out of memory");
104942 }else if( zErrDyn==0 ){
104943 zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
104944 }
104945 }
104946 goto attach_error;
104947 }
104948
104949 return;
104950
104951attach_error:
104952 /* Return an error if we get here */
104953 if( zErrDyn ){
104954 sqlite3_result_error(context, zErrDyn, -1);
104955 sqlite3DbFree(db, zErrDyn);
104956 }
104957 if( rc ) sqlite3_result_error_code(context, rc);
104958}
104959
104960/*
104961** An SQL user-function registered to do the work of an DETACH statement. The
104962** three arguments to the function come directly from a detach statement:
104963**
104964** DETACH DATABASE x
104965**
104966** SELECT sqlite_detach(x)
104967*/
104968static void detachFunc(
104969 sqlite3_context *context,
104970 int NotUsed,
104971 sqlite3_value **argv
104972){
104973 const char *zName = (const char *)sqlite3_value_text(argv[0]);
104974 sqlite3 *db = sqlite3_context_db_handle(context);
104975 int i;
104976 Db *pDb = 0;
104977 char zErr[128];
104978
104979 UNUSED_PARAMETER(NotUsed);
104980
104981 if( zName==0 ) zName = "";
104982 for(i=0; i<db->nDb; i++){
104983 pDb = &db->aDb[i];
104984 if( pDb->pBt==0 ) continue;
104985 if( sqlite3StrICmp(pDb->zDbSName, zName)==0 ) break;
104986 }
104987
104988 if( i>=db->nDb ){
104989 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
104990 goto detach_error;
104991 }
104992 if( i<2 ){
104993 sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
104994 goto detach_error;
104995 }
104996 if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
104997 sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
104998 goto detach_error;
104999 }
105000
105001 sqlite3BtreeClose(pDb->pBt);
105002 pDb->pBt = 0;
105003 pDb->pSchema = 0;
105004 sqlite3CollapseDatabaseArray(db);
105005 return;
105006
105007detach_error:
105008 sqlite3_result_error(context, zErr, -1);
105009}
105010
105011/*
105012** This procedure generates VDBE code for a single invocation of either the
105013** sqlite_detach() or sqlite_attach() SQL user functions.
105014*/
105015static void codeAttach(
105016 Parse *pParse, /* The parser context */
105017 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
105018 FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
105019 Expr *pAuthArg, /* Expression to pass to authorization callback */
105020 Expr *pFilename, /* Name of database file */
105021 Expr *pDbname, /* Name of the database to use internally */
105022 Expr *pKey /* Database key for encryption extension */
105023){
105024 int rc;
105025 NameContext sName;
105026 Vdbe *v;
105027 sqlite3* db = pParse->db;
105028 int regArgs;
105029
105030 if( pParse->nErr ) goto attach_end;
105031 memset(&sName, 0, sizeof(NameContext));
105032 sName.pParse = pParse;
105033
105034 if(
105035 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
105036 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
105037 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
105038 ){
105039 goto attach_end;
105040 }
105041
105042#ifndef SQLITE_OMIT_AUTHORIZATION
105043 if( pAuthArg ){
105044 char *zAuthArg;
105045 if( pAuthArg->op==TK_STRING ){
105046 zAuthArg = pAuthArg->u.zToken;
105047 }else{
105048 zAuthArg = 0;
105049 }
105050 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
105051 if(rc!=SQLITE_OK ){
105052 goto attach_end;
105053 }
105054 }
105055#endif /* SQLITE_OMIT_AUTHORIZATION */
105056
105057
105058 v = sqlite3GetVdbe(pParse);
105059 regArgs = sqlite3GetTempRange(pParse, 4);
105060 sqlite3ExprCode(pParse, pFilename, regArgs);
105061 sqlite3ExprCode(pParse, pDbname, regArgs+1);
105062 sqlite3ExprCode(pParse, pKey, regArgs+2);
105063
105064 assert( v || db->mallocFailed );
105065 if( v ){
105066 sqlite3VdbeAddOp4(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3,
105067 (char *)pFunc, P4_FUNCDEF);
105068 assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
105069 sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
105070
105071 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
105072 ** statement only). For DETACH, set it to false (expire all existing
105073 ** statements).
105074 */
105075 sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
105076 }
105077
105078attach_end:
105079 sqlite3ExprDelete(db, pFilename);
105080 sqlite3ExprDelete(db, pDbname);
105081 sqlite3ExprDelete(db, pKey);
105082}
105083
105084/*
105085** Called by the parser to compile a DETACH statement.
105086**
105087** DETACH pDbname
105088*/
105089SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
105090 static const FuncDef detach_func = {
105091 1, /* nArg */
105092 SQLITE_UTF8, /* funcFlags */
105093 0, /* pUserData */
105094 0, /* pNext */
105095 detachFunc, /* xSFunc */
105096 0, /* xFinalize */
105097 0, 0, /* xValue, xInverse */
105098 "sqlite_detach", /* zName */
105099 {0}
105100 };
105101 codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
105102}
105103
105104/*
105105** Called by the parser to compile an ATTACH statement.
105106**
105107** ATTACH p AS pDbname KEY pKey
105108*/
105109SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
105110 static const FuncDef attach_func = {
105111 3, /* nArg */
105112 SQLITE_UTF8, /* funcFlags */
105113 0, /* pUserData */
105114 0, /* pNext */
105115 attachFunc, /* xSFunc */
105116 0, /* xFinalize */
105117 0, 0, /* xValue, xInverse */
105118 "sqlite_attach", /* zName */
105119 {0}
105120 };
105121 codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
105122}
105123#endif /* SQLITE_OMIT_ATTACH */
105124
105125/*
105126** Initialize a DbFixer structure. This routine must be called prior
105127** to passing the structure to one of the sqliteFixAAAA() routines below.
105128*/
105129SQLITE_PRIVATE void sqlite3FixInit(
105130 DbFixer *pFix, /* The fixer to be initialized */
105131 Parse *pParse, /* Error messages will be written here */
105132 int iDb, /* This is the database that must be used */
105133 const char *zType, /* "view", "trigger", or "index" */
105134 const Token *pName /* Name of the view, trigger, or index */
105135){
105136 sqlite3 *db;
105137
105138 db = pParse->db;
105139 assert( db->nDb>iDb );
105140 pFix->pParse = pParse;
105141 pFix->zDb = db->aDb[iDb].zDbSName;
105142 pFix->pSchema = db->aDb[iDb].pSchema;
105143 pFix->zType = zType;
105144 pFix->pName = pName;
105145 pFix->bVarOnly = (iDb==1);
105146}
105147
105148/*
105149** The following set of routines walk through the parse tree and assign
105150** a specific database to all table references where the database name
105151** was left unspecified in the original SQL statement. The pFix structure
105152** must have been initialized by a prior call to sqlite3FixInit().
105153**
105154** These routines are used to make sure that an index, trigger, or
105155** view in one database does not refer to objects in a different database.
105156** (Exception: indices, triggers, and views in the TEMP database are
105157** allowed to refer to anything.) If a reference is explicitly made
105158** to an object in a different database, an error message is added to
105159** pParse->zErrMsg and these routines return non-zero. If everything
105160** checks out, these routines return 0.
105161*/
105162SQLITE_PRIVATE int sqlite3FixSrcList(
105163 DbFixer *pFix, /* Context of the fixation */
105164 SrcList *pList /* The Source list to check and modify */
105165){
105166 int i;
105167 const char *zDb;
105168 struct SrcList_item *pItem;
105169
105170 if( NEVER(pList==0) ) return 0;
105171 zDb = pFix->zDb;
105172 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
105173 if( pFix->bVarOnly==0 ){
105174 if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
105175 sqlite3ErrorMsg(pFix->pParse,
105176 "%s %T cannot reference objects in database %s",
105177 pFix->zType, pFix->pName, pItem->zDatabase);
105178 return 1;
105179 }
105180 sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
105181 pItem->zDatabase = 0;
105182 pItem->pSchema = pFix->pSchema;
105183 }
105184#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
105185 if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
105186 if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
105187#endif
105188 if( pItem->fg.isTabFunc && sqlite3FixExprList(pFix, pItem->u1.pFuncArg) ){
105189 return 1;
105190 }
105191 }
105192 return 0;
105193}
105194#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
105195SQLITE_PRIVATE int sqlite3FixSelect(
105196 DbFixer *pFix, /* Context of the fixation */
105197 Select *pSelect /* The SELECT statement to be fixed to one database */
105198){
105199 while( pSelect ){
105200 if( sqlite3FixExprList(pFix, pSelect->pEList) ){
105201 return 1;
105202 }
105203 if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
105204 return 1;
105205 }
105206 if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
105207 return 1;
105208 }
105209 if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){
105210 return 1;
105211 }
105212 if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
105213 return 1;
105214 }
105215 if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
105216 return 1;
105217 }
105218 if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
105219 return 1;
105220 }
105221 if( pSelect->pWith ){
105222 int i;
105223 for(i=0; i<pSelect->pWith->nCte; i++){
105224 if( sqlite3FixSelect(pFix, pSelect->pWith->a[i].pSelect) ){
105225 return 1;
105226 }
105227 }
105228 }
105229 pSelect = pSelect->pPrior;
105230 }
105231 return 0;
105232}
105233SQLITE_PRIVATE int sqlite3FixExpr(
105234 DbFixer *pFix, /* Context of the fixation */
105235 Expr *pExpr /* The expression to be fixed to one database */
105236){
105237 while( pExpr ){
105238 if( pExpr->op==TK_VARIABLE ){
105239 if( pFix->pParse->db->init.busy ){
105240 pExpr->op = TK_NULL;
105241 }else{
105242 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
105243 return 1;
105244 }
105245 }
105246 if( ExprHasProperty(pExpr, EP_TokenOnly|EP_Leaf) ) break;
105247 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
105248 if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
105249 }else{
105250 if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
105251 }
105252 if( sqlite3FixExpr(pFix, pExpr->pRight) ){
105253 return 1;
105254 }
105255 pExpr = pExpr->pLeft;
105256 }
105257 return 0;
105258}
105259SQLITE_PRIVATE int sqlite3FixExprList(
105260 DbFixer *pFix, /* Context of the fixation */
105261 ExprList *pList /* The expression to be fixed to one database */
105262){
105263 int i;
105264 struct ExprList_item *pItem;
105265 if( pList==0 ) return 0;
105266 for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
105267 if( sqlite3FixExpr(pFix, pItem->pExpr) ){
105268 return 1;
105269 }
105270 }
105271 return 0;
105272}
105273#endif
105274
105275#ifndef SQLITE_OMIT_TRIGGER
105276SQLITE_PRIVATE int sqlite3FixTriggerStep(
105277 DbFixer *pFix, /* Context of the fixation */
105278 TriggerStep *pStep /* The trigger step be fixed to one database */
105279){
105280 while( pStep ){
105281 if( sqlite3FixSelect(pFix, pStep->pSelect) ){
105282 return 1;
105283 }
105284 if( sqlite3FixExpr(pFix, pStep->pWhere) ){
105285 return 1;
105286 }
105287 if( sqlite3FixExprList(pFix, pStep->pExprList) ){
105288 return 1;
105289 }
105290#ifndef SQLITE_OMIT_UPSERT
105291 if( pStep->pUpsert ){
105292 Upsert *pUp = pStep->pUpsert;
105293 if( sqlite3FixExprList(pFix, pUp->pUpsertTarget)
105294 || sqlite3FixExpr(pFix, pUp->pUpsertTargetWhere)
105295 || sqlite3FixExprList(pFix, pUp->pUpsertSet)
105296 || sqlite3FixExpr(pFix, pUp->pUpsertWhere)
105297 ){
105298 return 1;
105299 }
105300 }
105301#endif
105302 pStep = pStep->pNext;
105303 }
105304 return 0;
105305}
105306#endif
105307
105308/************** End of attach.c **********************************************/
105309/************** Begin file auth.c ********************************************/
105310/*
105311** 2003 January 11
105312**
105313** The author disclaims copyright to this source code. In place of
105314** a legal notice, here is a blessing:
105315**
105316** May you do good and not evil.
105317** May you find forgiveness for yourself and forgive others.
105318** May you share freely, never taking more than you give.
105319**
105320*************************************************************************
105321** This file contains code used to implement the sqlite3_set_authorizer()
105322** API. This facility is an optional feature of the library. Embedded
105323** systems that do not need this facility may omit it by recompiling
105324** the library with -DSQLITE_OMIT_AUTHORIZATION=1
105325*/
105326/* #include "sqliteInt.h" */
105327
105328/*
105329** All of the code in this file may be omitted by defining a single
105330** macro.
105331*/
105332#ifndef SQLITE_OMIT_AUTHORIZATION
105333
105334/*
105335** Set or clear the access authorization function.
105336**
105337** The access authorization function is be called during the compilation
105338** phase to verify that the user has read and/or write access permission on
105339** various fields of the database. The first argument to the auth function
105340** is a copy of the 3rd argument to this routine. The second argument
105341** to the auth function is one of these constants:
105342**
105343** SQLITE_CREATE_INDEX
105344** SQLITE_CREATE_TABLE
105345** SQLITE_CREATE_TEMP_INDEX
105346** SQLITE_CREATE_TEMP_TABLE
105347** SQLITE_CREATE_TEMP_TRIGGER
105348** SQLITE_CREATE_TEMP_VIEW
105349** SQLITE_CREATE_TRIGGER
105350** SQLITE_CREATE_VIEW
105351** SQLITE_DELETE
105352** SQLITE_DROP_INDEX
105353** SQLITE_DROP_TABLE
105354** SQLITE_DROP_TEMP_INDEX
105355** SQLITE_DROP_TEMP_TABLE
105356** SQLITE_DROP_TEMP_TRIGGER
105357** SQLITE_DROP_TEMP_VIEW
105358** SQLITE_DROP_TRIGGER
105359** SQLITE_DROP_VIEW
105360** SQLITE_INSERT
105361** SQLITE_PRAGMA
105362** SQLITE_READ
105363** SQLITE_SELECT
105364** SQLITE_TRANSACTION
105365** SQLITE_UPDATE
105366**
105367** The third and fourth arguments to the auth function are the name of
105368** the table and the column that are being accessed. The auth function
105369** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
105370** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
105371** means that the SQL statement will never-run - the sqlite3_exec() call
105372** will return with an error. SQLITE_IGNORE means that the SQL statement
105373** should run but attempts to read the specified column will return NULL
105374** and attempts to write the column will be ignored.
105375**
105376** Setting the auth function to NULL disables this hook. The default
105377** setting of the auth function is NULL.
105378*/
105379SQLITE_API int sqlite3_set_authorizer(
105380 sqlite3 *db,
105381 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
105382 void *pArg
105383){
105384#ifdef SQLITE_ENABLE_API_ARMOR
105385 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
105386#endif
105387 sqlite3_mutex_enter(db->mutex);
105388 db->xAuth = (sqlite3_xauth)xAuth;
105389 db->pAuthArg = pArg;
105390 sqlite3ExpirePreparedStatements(db, 0);
105391 sqlite3_mutex_leave(db->mutex);
105392 return SQLITE_OK;
105393}
105394
105395/*
105396** Write an error message into pParse->zErrMsg that explains that the
105397** user-supplied authorization function returned an illegal value.
105398*/
105399static void sqliteAuthBadReturnCode(Parse *pParse){
105400 sqlite3ErrorMsg(pParse, "authorizer malfunction");
105401 pParse->rc = SQLITE_ERROR;
105402}
105403
105404/*
105405** Invoke the authorization callback for permission to read column zCol from
105406** table zTab in database zDb. This function assumes that an authorization
105407** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
105408**
105409** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
105410** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
105411** is treated as SQLITE_DENY. In this case an error is left in pParse.
105412*/
105413SQLITE_PRIVATE int sqlite3AuthReadCol(
105414 Parse *pParse, /* The parser context */
105415 const char *zTab, /* Table name */
105416 const char *zCol, /* Column name */
105417 int iDb /* Index of containing database. */
105418){
105419 sqlite3 *db = pParse->db; /* Database handle */
105420 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
105421 int rc; /* Auth callback return code */
105422
105423 if( db->init.busy ) return SQLITE_OK;
105424 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
105425#ifdef SQLITE_USER_AUTHENTICATION
105426 ,db->auth.zAuthUser
105427#endif
105428 );
105429 if( rc==SQLITE_DENY ){
105430 char *z = sqlite3_mprintf("%s.%s", zTab, zCol);
105431 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
105432 sqlite3ErrorMsg(pParse, "access to %z is prohibited", z);
105433 pParse->rc = SQLITE_AUTH;
105434 }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
105435 sqliteAuthBadReturnCode(pParse);
105436 }
105437 return rc;
105438}
105439
105440/*
105441** The pExpr should be a TK_COLUMN expression. The table referred to
105442** is in pTabList or else it is the NEW or OLD table of a trigger.
105443** Check to see if it is OK to read this particular column.
105444**
105445** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
105446** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
105447** then generate an error.
105448*/
105449SQLITE_PRIVATE void sqlite3AuthRead(
105450 Parse *pParse, /* The parser context */
105451 Expr *pExpr, /* The expression to check authorization on */
105452 Schema *pSchema, /* The schema of the expression */
105453 SrcList *pTabList /* All table that pExpr might refer to */
105454){
105455 sqlite3 *db = pParse->db;
105456 Table *pTab = 0; /* The table being read */
105457 const char *zCol; /* Name of the column of the table */
105458 int iSrc; /* Index in pTabList->a[] of table being read */
105459 int iDb; /* The index of the database the expression refers to */
105460 int iCol; /* Index of column in table */
105461
105462 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
105463 if( db->xAuth==0 ) return;
105464 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
105465 if( iDb<0 ){
105466 /* An attempt to read a column out of a subquery or other
105467 ** temporary table. */
105468 return;
105469 }
105470
105471 if( pExpr->op==TK_TRIGGER ){
105472 pTab = pParse->pTriggerTab;
105473 }else{
105474 assert( pTabList );
105475 for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
105476 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
105477 pTab = pTabList->a[iSrc].pTab;
105478 break;
105479 }
105480 }
105481 }
105482 iCol = pExpr->iColumn;
105483 if( NEVER(pTab==0) ) return;
105484
105485 if( iCol>=0 ){
105486 assert( iCol<pTab->nCol );
105487 zCol = pTab->aCol[iCol].zName;
105488 }else if( pTab->iPKey>=0 ){
105489 assert( pTab->iPKey<pTab->nCol );
105490 zCol = pTab->aCol[pTab->iPKey].zName;
105491 }else{
105492 zCol = "ROWID";
105493 }
105494 assert( iDb>=0 && iDb<db->nDb );
105495 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
105496 pExpr->op = TK_NULL;
105497 }
105498}
105499
105500/*
105501** Do an authorization check using the code and arguments given. Return
105502** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
105503** is returned, then the error count and error message in pParse are
105504** modified appropriately.
105505*/
105506SQLITE_PRIVATE int sqlite3AuthCheck(
105507 Parse *pParse,
105508 int code,
105509 const char *zArg1,
105510 const char *zArg2,
105511 const char *zArg3
105512){
105513 sqlite3 *db = pParse->db;
105514 int rc;
105515
105516 /* Don't do any authorization checks if the database is initialising
105517 ** or if the parser is being invoked from within sqlite3_declare_vtab.
105518 */
105519 if( db->init.busy || IN_SPECIAL_PARSE ){
105520 return SQLITE_OK;
105521 }
105522
105523 if( db->xAuth==0 ){
105524 return SQLITE_OK;
105525 }
105526
105527 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
105528 ** callback are either NULL pointers or zero-terminated strings that
105529 ** contain additional details about the action to be authorized.
105530 **
105531 ** The following testcase() macros show that any of the 3rd through 6th
105532 ** parameters can be either NULL or a string. */
105533 testcase( zArg1==0 );
105534 testcase( zArg2==0 );
105535 testcase( zArg3==0 );
105536 testcase( pParse->zAuthContext==0 );
105537
105538 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
105539#ifdef SQLITE_USER_AUTHENTICATION
105540 ,db->auth.zAuthUser
105541#endif
105542 );
105543 if( rc==SQLITE_DENY ){
105544 sqlite3ErrorMsg(pParse, "not authorized");
105545 pParse->rc = SQLITE_AUTH;
105546 }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
105547 rc = SQLITE_DENY;
105548 sqliteAuthBadReturnCode(pParse);
105549 }
105550 return rc;
105551}
105552
105553/*
105554** Push an authorization context. After this routine is called, the
105555** zArg3 argument to authorization callbacks will be zContext until
105556** popped. Or if pParse==0, this routine is a no-op.
105557*/
105558SQLITE_PRIVATE void sqlite3AuthContextPush(
105559 Parse *pParse,
105560 AuthContext *pContext,
105561 const char *zContext
105562){
105563 assert( pParse );
105564 pContext->pParse = pParse;
105565 pContext->zAuthContext = pParse->zAuthContext;
105566 pParse->zAuthContext = zContext;
105567}
105568
105569/*
105570** Pop an authorization context that was previously pushed
105571** by sqlite3AuthContextPush
105572*/
105573SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
105574 if( pContext->pParse ){
105575 pContext->pParse->zAuthContext = pContext->zAuthContext;
105576 pContext->pParse = 0;
105577 }
105578}
105579
105580#endif /* SQLITE_OMIT_AUTHORIZATION */
105581
105582/************** End of auth.c ************************************************/
105583/************** Begin file build.c *******************************************/
105584/*
105585** 2001 September 15
105586**
105587** The author disclaims copyright to this source code. In place of
105588** a legal notice, here is a blessing:
105589**
105590** May you do good and not evil.
105591** May you find forgiveness for yourself and forgive others.
105592** May you share freely, never taking more than you give.
105593**
105594*************************************************************************
105595** This file contains C code routines that are called by the SQLite parser
105596** when syntax rules are reduced. The routines in this file handle the
105597** following kinds of SQL syntax:
105598**
105599** CREATE TABLE
105600** DROP TABLE
105601** CREATE INDEX
105602** DROP INDEX
105603** creating ID lists
105604** BEGIN TRANSACTION
105605** COMMIT
105606** ROLLBACK
105607*/
105608/* #include "sqliteInt.h" */
105609
105610#ifndef SQLITE_OMIT_SHARED_CACHE
105611/*
105612** The TableLock structure is only used by the sqlite3TableLock() and
105613** codeTableLocks() functions.
105614*/
105615struct TableLock {
105616 int iDb; /* The database containing the table to be locked */
105617 int iTab; /* The root page of the table to be locked */
105618 u8 isWriteLock; /* True for write lock. False for a read lock */
105619 const char *zLockName; /* Name of the table */
105620};
105621
105622/*
105623** Record the fact that we want to lock a table at run-time.
105624**
105625** The table to be locked has root page iTab and is found in database iDb.
105626** A read or a write lock can be taken depending on isWritelock.
105627**
105628** This routine just records the fact that the lock is desired. The
105629** code to make the lock occur is generated by a later call to
105630** codeTableLocks() which occurs during sqlite3FinishCoding().
105631*/
105632SQLITE_PRIVATE void sqlite3TableLock(
105633 Parse *pParse, /* Parsing context */
105634 int iDb, /* Index of the database containing the table to lock */
105635 int iTab, /* Root page number of the table to be locked */
105636 u8 isWriteLock, /* True for a write lock */
105637 const char *zName /* Name of the table to be locked */
105638){
105639 Parse *pToplevel = sqlite3ParseToplevel(pParse);
105640 int i;
105641 int nBytes;
105642 TableLock *p;
105643 assert( iDb>=0 );
105644
105645 if( iDb==1 ) return;
105646 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
105647 for(i=0; i<pToplevel->nTableLock; i++){
105648 p = &pToplevel->aTableLock[i];
105649 if( p->iDb==iDb && p->iTab==iTab ){
105650 p->isWriteLock = (p->isWriteLock || isWriteLock);
105651 return;
105652 }
105653 }
105654
105655 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
105656 pToplevel->aTableLock =
105657 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
105658 if( pToplevel->aTableLock ){
105659 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
105660 p->iDb = iDb;
105661 p->iTab = iTab;
105662 p->isWriteLock = isWriteLock;
105663 p->zLockName = zName;
105664 }else{
105665 pToplevel->nTableLock = 0;
105666 sqlite3OomFault(pToplevel->db);
105667 }
105668}
105669
105670/*
105671** Code an OP_TableLock instruction for each table locked by the
105672** statement (configured by calls to sqlite3TableLock()).
105673*/
105674static void codeTableLocks(Parse *pParse){
105675 int i;
105676 Vdbe *pVdbe;
105677
105678 pVdbe = sqlite3GetVdbe(pParse);
105679 assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
105680
105681 for(i=0; i<pParse->nTableLock; i++){
105682 TableLock *p = &pParse->aTableLock[i];
105683 int p1 = p->iDb;
105684 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
105685 p->zLockName, P4_STATIC);
105686 }
105687}
105688#else
105689 #define codeTableLocks(x)
105690#endif
105691
105692/*
105693** Return TRUE if the given yDbMask object is empty - if it contains no
105694** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero()
105695** macros when SQLITE_MAX_ATTACHED is greater than 30.
105696*/
105697#if SQLITE_MAX_ATTACHED>30
105698SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask m){
105699 int i;
105700 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
105701 return 1;
105702}
105703#endif
105704
105705/*
105706** This routine is called after a single SQL statement has been
105707** parsed and a VDBE program to execute that statement has been
105708** prepared. This routine puts the finishing touches on the
105709** VDBE program and resets the pParse structure for the next
105710** parse.
105711**
105712** Note that if an error occurred, it might be the case that
105713** no VDBE code was generated.
105714*/
105715SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
105716 sqlite3 *db;
105717 Vdbe *v;
105718
105719 assert( pParse->pToplevel==0 );
105720 db = pParse->db;
105721 if( pParse->nested ) return;
105722 if( db->mallocFailed || pParse->nErr ){
105723 if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR;
105724 return;
105725 }
105726
105727 /* Begin by generating some termination code at the end of the
105728 ** vdbe program
105729 */
105730 v = sqlite3GetVdbe(pParse);
105731 assert( !pParse->isMultiWrite
105732 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
105733 if( v ){
105734 sqlite3VdbeAddOp0(v, OP_Halt);
105735
105736#if SQLITE_USER_AUTHENTICATION
105737 if( pParse->nTableLock>0 && db->init.busy==0 ){
105738 sqlite3UserAuthInit(db);
105739 if( db->auth.authLevel<UAUTH_User ){
105740 sqlite3ErrorMsg(pParse, "user not authenticated");
105741 pParse->rc = SQLITE_AUTH_USER;
105742 return;
105743 }
105744 }
105745#endif
105746
105747 /* The cookie mask contains one bit for each database file open.
105748 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
105749 ** set for each database that is used. Generate code to start a
105750 ** transaction on each used database and to verify the schema cookie
105751 ** on each used database.
105752 */
105753 if( db->mallocFailed==0
105754 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
105755 ){
105756 int iDb, i;
105757 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
105758 sqlite3VdbeJumpHere(v, 0);
105759 for(iDb=0; iDb<db->nDb; iDb++){
105760 Schema *pSchema;
105761 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
105762 sqlite3VdbeUsesBtree(v, iDb);
105763 pSchema = db->aDb[iDb].pSchema;
105764 sqlite3VdbeAddOp4Int(v,
105765 OP_Transaction, /* Opcode */
105766 iDb, /* P1 */
105767 DbMaskTest(pParse->writeMask,iDb), /* P2 */
105768 pSchema->schema_cookie, /* P3 */
105769 pSchema->iGeneration /* P4 */
105770 );
105771 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
105772 VdbeComment((v,
105773 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
105774 }
105775#ifndef SQLITE_OMIT_VIRTUALTABLE
105776 for(i=0; i<pParse->nVtabLock; i++){
105777 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
105778 sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
105779 }
105780 pParse->nVtabLock = 0;
105781#endif
105782
105783 /* Once all the cookies have been verified and transactions opened,
105784 ** obtain the required table-locks. This is a no-op unless the
105785 ** shared-cache feature is enabled.
105786 */
105787 codeTableLocks(pParse);
105788
105789 /* Initialize any AUTOINCREMENT data structures required.
105790 */
105791 sqlite3AutoincrementBegin(pParse);
105792
105793 /* Code constant expressions that where factored out of inner loops */
105794 if( pParse->pConstExpr ){
105795 ExprList *pEL = pParse->pConstExpr;
105796 pParse->okConstFactor = 0;
105797 for(i=0; i<pEL->nExpr; i++){
105798 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
105799 }
105800 }
105801
105802 /* Finally, jump back to the beginning of the executable code. */
105803 sqlite3VdbeGoto(v, 1);
105804 }
105805 }
105806
105807
105808 /* Get the VDBE program ready for execution
105809 */
105810 if( v && pParse->nErr==0 && !db->mallocFailed ){
105811 /* A minimum of one cursor is required if autoincrement is used
105812 * See ticket [a696379c1f08866] */
105813 if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
105814 sqlite3VdbeMakeReady(v, pParse);
105815 pParse->rc = SQLITE_DONE;
105816 }else{
105817 pParse->rc = SQLITE_ERROR;
105818 }
105819}
105820
105821/*
105822** Run the parser and code generator recursively in order to generate
105823** code for the SQL statement given onto the end of the pParse context
105824** currently under construction. When the parser is run recursively
105825** this way, the final OP_Halt is not appended and other initialization
105826** and finalization steps are omitted because those are handling by the
105827** outermost parser.
105828**
105829** Not everything is nestable. This facility is designed to permit
105830** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use
105831** care if you decide to try to use this routine for some other purposes.
105832*/
105833SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
105834 va_list ap;
105835 char *zSql;
105836 char *zErrMsg = 0;
105837 sqlite3 *db = pParse->db;
105838 char saveBuf[PARSE_TAIL_SZ];
105839
105840 if( pParse->nErr ) return;
105841 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
105842 va_start(ap, zFormat);
105843 zSql = sqlite3VMPrintf(db, zFormat, ap);
105844 va_end(ap);
105845 if( zSql==0 ){
105846 return; /* A malloc must have failed */
105847 }
105848 pParse->nested++;
105849 memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
105850 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
105851 sqlite3RunParser(pParse, zSql, &zErrMsg);
105852 sqlite3DbFree(db, zErrMsg);
105853 sqlite3DbFree(db, zSql);
105854 memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
105855 pParse->nested--;
105856}
105857
105858#if SQLITE_USER_AUTHENTICATION
105859/*
105860** Return TRUE if zTable is the name of the system table that stores the
105861** list of users and their access credentials.
105862*/
105863SQLITE_PRIVATE int sqlite3UserAuthTable(const char *zTable){
105864 return sqlite3_stricmp(zTable, "sqlite_user")==0;
105865}
105866#endif
105867
105868/*
105869** Locate the in-memory structure that describes a particular database
105870** table given the name of that table and (optionally) the name of the
105871** database containing the table. Return NULL if not found.
105872**
105873** If zDatabase is 0, all databases are searched for the table and the
105874** first matching table is returned. (No checking for duplicate table
105875** names is done.) The search order is TEMP first, then MAIN, then any
105876** auxiliary databases added using the ATTACH command.
105877**
105878** See also sqlite3LocateTable().
105879*/
105880SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
105881 Table *p = 0;
105882 int i;
105883
105884 /* All mutexes are required for schema access. Make sure we hold them. */
105885 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
105886#if SQLITE_USER_AUTHENTICATION
105887 /* Only the admin user is allowed to know that the sqlite_user table
105888 ** exists */
105889 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
105890 return 0;
105891 }
105892#endif
105893 while(1){
105894 for(i=OMIT_TEMPDB; i<db->nDb; i++){
105895 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
105896 if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){
105897 assert( sqlite3SchemaMutexHeld(db, j, 0) );
105898 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
105899 if( p ) return p;
105900 }
105901 }
105902 /* Not found. If the name we were looking for was temp.sqlite_master
105903 ** then change the name to sqlite_temp_master and try again. */
105904 if( sqlite3StrICmp(zName, MASTER_NAME)!=0 ) break;
105905 if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break;
105906 zName = TEMP_MASTER_NAME;
105907 }
105908 return 0;
105909}
105910
105911/*
105912** Locate the in-memory structure that describes a particular database
105913** table given the name of that table and (optionally) the name of the
105914** database containing the table. Return NULL if not found. Also leave an
105915** error message in pParse->zErrMsg.
105916**
105917** The difference between this routine and sqlite3FindTable() is that this
105918** routine leaves an error message in pParse->zErrMsg where
105919** sqlite3FindTable() does not.
105920*/
105921SQLITE_PRIVATE Table *sqlite3LocateTable(
105922 Parse *pParse, /* context in which to report errors */
105923 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */
105924 const char *zName, /* Name of the table we are looking for */
105925 const char *zDbase /* Name of the database. Might be NULL */
105926){
105927 Table *p;
105928 sqlite3 *db = pParse->db;
105929
105930 /* Read the database schema. If an error occurs, leave an error message
105931 ** and code in pParse and return NULL. */
105932 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
105933 && SQLITE_OK!=sqlite3ReadSchema(pParse)
105934 ){
105935 return 0;
105936 }
105937
105938 p = sqlite3FindTable(db, zName, zDbase);
105939 if( p==0 ){
105940 const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
105941#ifndef SQLITE_OMIT_VIRTUALTABLE
105942 if( sqlite3FindDbName(db, zDbase)<1 ){
105943 /* If zName is the not the name of a table in the schema created using
105944 ** CREATE, then check to see if it is the name of an virtual table that
105945 ** can be an eponymous virtual table. */
105946 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
105947 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
105948 pMod = sqlite3PragmaVtabRegister(db, zName);
105949 }
105950 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
105951 return pMod->pEpoTab;
105952 }
105953 }
105954#endif
105955 if( (flags & LOCATE_NOERR)==0 ){
105956 if( zDbase ){
105957 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
105958 }else{
105959 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
105960 }
105961 pParse->checkSchema = 1;
105962 }
105963 }
105964
105965 return p;
105966}
105967
105968/*
105969** Locate the table identified by *p.
105970**
105971** This is a wrapper around sqlite3LocateTable(). The difference between
105972** sqlite3LocateTable() and this function is that this function restricts
105973** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
105974** non-NULL if it is part of a view or trigger program definition. See
105975** sqlite3FixSrcList() for details.
105976*/
105977SQLITE_PRIVATE Table *sqlite3LocateTableItem(
105978 Parse *pParse,
105979 u32 flags,
105980 struct SrcList_item *p
105981){
105982 const char *zDb;
105983 assert( p->pSchema==0 || p->zDatabase==0 );
105984 if( p->pSchema ){
105985 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
105986 zDb = pParse->db->aDb[iDb].zDbSName;
105987 }else{
105988 zDb = p->zDatabase;
105989 }
105990 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
105991}
105992
105993/*
105994** Locate the in-memory structure that describes
105995** a particular index given the name of that index
105996** and the name of the database that contains the index.
105997** Return NULL if not found.
105998**
105999** If zDatabase is 0, all databases are searched for the
106000** table and the first matching index is returned. (No checking
106001** for duplicate index names is done.) The search order is
106002** TEMP first, then MAIN, then any auxiliary databases added
106003** using the ATTACH command.
106004*/
106005SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
106006 Index *p = 0;
106007 int i;
106008 /* All mutexes are required for schema access. Make sure we hold them. */
106009 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
106010 for(i=OMIT_TEMPDB; i<db->nDb; i++){
106011 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
106012 Schema *pSchema = db->aDb[j].pSchema;
106013 assert( pSchema );
106014 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue;
106015 assert( sqlite3SchemaMutexHeld(db, j, 0) );
106016 p = sqlite3HashFind(&pSchema->idxHash, zName);
106017 if( p ) break;
106018 }
106019 return p;
106020}
106021
106022/*
106023** Reclaim the memory used by an index
106024*/
106025SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3 *db, Index *p){
106026#ifndef SQLITE_OMIT_ANALYZE
106027 sqlite3DeleteIndexSamples(db, p);
106028#endif
106029 sqlite3ExprDelete(db, p->pPartIdxWhere);
106030 sqlite3ExprListDelete(db, p->aColExpr);
106031 sqlite3DbFree(db, p->zColAff);
106032 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
106033#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
106034 sqlite3_free(p->aiRowEst);
106035#endif
106036 sqlite3DbFree(db, p);
106037}
106038
106039/*
106040** For the index called zIdxName which is found in the database iDb,
106041** unlike that index from its Table then remove the index from
106042** the index hash table and free all memory structures associated
106043** with the index.
106044*/
106045SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
106046 Index *pIndex;
106047 Hash *pHash;
106048
106049 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
106050 pHash = &db->aDb[iDb].pSchema->idxHash;
106051 pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
106052 if( ALWAYS(pIndex) ){
106053 if( pIndex->pTable->pIndex==pIndex ){
106054 pIndex->pTable->pIndex = pIndex->pNext;
106055 }else{
106056 Index *p;
106057 /* Justification of ALWAYS(); The index must be on the list of
106058 ** indices. */
106059 p = pIndex->pTable->pIndex;
106060 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
106061 if( ALWAYS(p && p->pNext==pIndex) ){
106062 p->pNext = pIndex->pNext;
106063 }
106064 }
106065 sqlite3FreeIndex(db, pIndex);
106066 }
106067 db->mDbFlags |= DBFLAG_SchemaChange;
106068}
106069
106070/*
106071** Look through the list of open database files in db->aDb[] and if
106072** any have been closed, remove them from the list. Reallocate the
106073** db->aDb[] structure to a smaller size, if possible.
106074**
106075** Entry 0 (the "main" database) and entry 1 (the "temp" database)
106076** are never candidates for being collapsed.
106077*/
106078SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
106079 int i, j;
106080 for(i=j=2; i<db->nDb; i++){
106081 struct Db *pDb = &db->aDb[i];
106082 if( pDb->pBt==0 ){
106083 sqlite3DbFree(db, pDb->zDbSName);
106084 pDb->zDbSName = 0;
106085 continue;
106086 }
106087 if( j<i ){
106088 db->aDb[j] = db->aDb[i];
106089 }
106090 j++;
106091 }
106092 db->nDb = j;
106093 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
106094 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
106095 sqlite3DbFree(db, db->aDb);
106096 db->aDb = db->aDbStatic;
106097 }
106098}
106099
106100/*
106101** Reset the schema for the database at index iDb. Also reset the
106102** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
106103** Deferred resets may be run by calling with iDb<0.
106104*/
106105SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
106106 int i;
106107 assert( iDb<db->nDb );
106108
106109 if( iDb>=0 ){
106110 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
106111 DbSetProperty(db, iDb, DB_ResetWanted);
106112 DbSetProperty(db, 1, DB_ResetWanted);
106113 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
106114 }
106115
106116 if( db->nSchemaLock==0 ){
106117 for(i=0; i<db->nDb; i++){
106118 if( DbHasProperty(db, i, DB_ResetWanted) ){
106119 sqlite3SchemaClear(db->aDb[i].pSchema);
106120 }
106121 }
106122 }
106123}
106124
106125/*
106126** Erase all schema information from all attached databases (including
106127** "main" and "temp") for a single database connection.
106128*/
106129SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
106130 int i;
106131 sqlite3BtreeEnterAll(db);
106132 assert( db->nSchemaLock==0 );
106133 for(i=0; i<db->nDb; i++){
106134 Db *pDb = &db->aDb[i];
106135 if( pDb->pSchema ){
106136 sqlite3SchemaClear(pDb->pSchema);
106137 }
106138 }
106139 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
106140 sqlite3VtabUnlockList(db);
106141 sqlite3BtreeLeaveAll(db);
106142 sqlite3CollapseDatabaseArray(db);
106143}
106144
106145/*
106146** This routine is called when a commit occurs.
106147*/
106148SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
106149 db->mDbFlags &= ~DBFLAG_SchemaChange;
106150}
106151
106152/*
106153** Delete memory allocated for the column names of a table or view (the
106154** Table.aCol[] array).
106155*/
106156SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
106157 int i;
106158 Column *pCol;
106159 assert( pTable!=0 );
106160 if( (pCol = pTable->aCol)!=0 ){
106161 for(i=0; i<pTable->nCol; i++, pCol++){
106162 sqlite3DbFree(db, pCol->zName);
106163 sqlite3ExprDelete(db, pCol->pDflt);
106164 sqlite3DbFree(db, pCol->zColl);
106165 }
106166 sqlite3DbFree(db, pTable->aCol);
106167 }
106168}
106169
106170/*
106171** Remove the memory data structures associated with the given
106172** Table. No changes are made to disk by this routine.
106173**
106174** This routine just deletes the data structure. It does not unlink
106175** the table data structure from the hash table. But it does destroy
106176** memory structures of the indices and foreign keys associated with
106177** the table.
106178**
106179** The db parameter is optional. It is needed if the Table object
106180** contains lookaside memory. (Table objects in the schema do not use
106181** lookaside memory, but some ephemeral Table objects do.) Or the
106182** db parameter can be used with db->pnBytesFreed to measure the memory
106183** used by the Table object.
106184*/
106185static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
106186 Index *pIndex, *pNext;
106187
106188#ifdef SQLITE_DEBUG
106189 /* Record the number of outstanding lookaside allocations in schema Tables
106190 ** prior to doing any free() operations. Since schema Tables do not use
106191 ** lookaside, this number should not change. */
106192 int nLookaside = 0;
106193 if( db && (pTable->tabFlags & TF_Ephemeral)==0 ){
106194 nLookaside = sqlite3LookasideUsed(db, 0);
106195 }
106196#endif
106197
106198 /* Delete all indices associated with this table. */
106199 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
106200 pNext = pIndex->pNext;
106201 assert( pIndex->pSchema==pTable->pSchema
106202 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
106203 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
106204 char *zName = pIndex->zName;
106205 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
106206 &pIndex->pSchema->idxHash, zName, 0
106207 );
106208 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
106209 assert( pOld==pIndex || pOld==0 );
106210 }
106211 sqlite3FreeIndex(db, pIndex);
106212 }
106213
106214 /* Delete any foreign keys attached to this table. */
106215 sqlite3FkDelete(db, pTable);
106216
106217 /* Delete the Table structure itself.
106218 */
106219 sqlite3DeleteColumnNames(db, pTable);
106220 sqlite3DbFree(db, pTable->zName);
106221 sqlite3DbFree(db, pTable->zColAff);
106222 sqlite3SelectDelete(db, pTable->pSelect);
106223 sqlite3ExprListDelete(db, pTable->pCheck);
106224#ifndef SQLITE_OMIT_VIRTUALTABLE
106225 sqlite3VtabClear(db, pTable);
106226#endif
106227 sqlite3DbFree(db, pTable);
106228
106229 /* Verify that no lookaside memory was used by schema tables */
106230 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
106231}
106232SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
106233 /* Do not delete the table until the reference count reaches zero. */
106234 if( !pTable ) return;
106235 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
106236 deleteTable(db, pTable);
106237}
106238
106239
106240/*
106241** Unlink the given table from the hash tables and the delete the
106242** table structure with all its indices and foreign keys.
106243*/
106244SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
106245 Table *p;
106246 Db *pDb;
106247
106248 assert( db!=0 );
106249 assert( iDb>=0 && iDb<db->nDb );
106250 assert( zTabName );
106251 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
106252 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
106253 pDb = &db->aDb[iDb];
106254 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
106255 sqlite3DeleteTable(db, p);
106256 db->mDbFlags |= DBFLAG_SchemaChange;
106257}
106258
106259/*
106260** Given a token, return a string that consists of the text of that
106261** token. Space to hold the returned string
106262** is obtained from sqliteMalloc() and must be freed by the calling
106263** function.
106264**
106265** Any quotation marks (ex: "name", 'name', [name], or `name`) that
106266** surround the body of the token are removed.
106267**
106268** Tokens are often just pointers into the original SQL text and so
106269** are not \000 terminated and are not persistent. The returned string
106270** is \000 terminated and is persistent.
106271*/
106272SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
106273 char *zName;
106274 if( pName ){
106275 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
106276 sqlite3Dequote(zName);
106277 }else{
106278 zName = 0;
106279 }
106280 return zName;
106281}
106282
106283/*
106284** Open the sqlite_master table stored in database number iDb for
106285** writing. The table is opened using cursor 0.
106286*/
106287SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
106288 Vdbe *v = sqlite3GetVdbe(p);
106289 sqlite3TableLock(p, iDb, MASTER_ROOT, 1, MASTER_NAME);
106290 sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5);
106291 if( p->nTab==0 ){
106292 p->nTab = 1;
106293 }
106294}
106295
106296/*
106297** Parameter zName points to a nul-terminated buffer containing the name
106298** of a database ("main", "temp" or the name of an attached db). This
106299** function returns the index of the named database in db->aDb[], or
106300** -1 if the named db cannot be found.
106301*/
106302SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
106303 int i = -1; /* Database number */
106304 if( zName ){
106305 Db *pDb;
106306 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
106307 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
106308 /* "main" is always an acceptable alias for the primary database
106309 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
106310 if( i==0 && 0==sqlite3_stricmp("main", zName) ) break;
106311 }
106312 }
106313 return i;
106314}
106315
106316/*
106317** The token *pName contains the name of a database (either "main" or
106318** "temp" or the name of an attached db). This routine returns the
106319** index of the named database in db->aDb[], or -1 if the named db
106320** does not exist.
106321*/
106322SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
106323 int i; /* Database number */
106324 char *zName; /* Name we are searching for */
106325 zName = sqlite3NameFromToken(db, pName);
106326 i = sqlite3FindDbName(db, zName);
106327 sqlite3DbFree(db, zName);
106328 return i;
106329}
106330
106331/* The table or view or trigger name is passed to this routine via tokens
106332** pName1 and pName2. If the table name was fully qualified, for example:
106333**
106334** CREATE TABLE xxx.yyy (...);
106335**
106336** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
106337** the table name is not fully qualified, i.e.:
106338**
106339** CREATE TABLE yyy(...);
106340**
106341** Then pName1 is set to "yyy" and pName2 is "".
106342**
106343** This routine sets the *ppUnqual pointer to point at the token (pName1 or
106344** pName2) that stores the unqualified table name. The index of the
106345** database "xxx" is returned.
106346*/
106347SQLITE_PRIVATE int sqlite3TwoPartName(
106348 Parse *pParse, /* Parsing and code generating context */
106349 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
106350 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
106351 Token **pUnqual /* Write the unqualified object name here */
106352){
106353 int iDb; /* Database holding the object */
106354 sqlite3 *db = pParse->db;
106355
106356 assert( pName2!=0 );
106357 if( pName2->n>0 ){
106358 if( db->init.busy ) {
106359 sqlite3ErrorMsg(pParse, "corrupt database");
106360 return -1;
106361 }
106362 *pUnqual = pName2;
106363 iDb = sqlite3FindDb(db, pName1);
106364 if( iDb<0 ){
106365 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
106366 return -1;
106367 }
106368 }else{
106369 assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT
106370 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
106371 iDb = db->init.iDb;
106372 *pUnqual = pName1;
106373 }
106374 return iDb;
106375}
106376
106377/*
106378** This routine is used to check if the UTF-8 string zName is a legal
106379** unqualified name for a new schema object (table, index, view or
106380** trigger). All names are legal except those that begin with the string
106381** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
106382** is reserved for internal use.
106383*/
106384SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
106385 if( !pParse->db->init.busy && pParse->nested==0
106386 && (pParse->db->flags & SQLITE_WriteSchema)==0
106387 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
106388 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
106389 return SQLITE_ERROR;
106390 }
106391 return SQLITE_OK;
106392}
106393
106394/*
106395** Return the PRIMARY KEY index of a table
106396*/
106397SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
106398 Index *p;
106399 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
106400 return p;
106401}
106402
106403/*
106404** Return the column of index pIdx that corresponds to table
106405** column iCol. Return -1 if not found.
106406*/
106407SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){
106408 int i;
106409 for(i=0; i<pIdx->nColumn; i++){
106410 if( iCol==pIdx->aiColumn[i] ) return i;
106411 }
106412 return -1;
106413}
106414
106415/*
106416** Begin constructing a new table representation in memory. This is
106417** the first of several action routines that get called in response
106418** to a CREATE TABLE statement. In particular, this routine is called
106419** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
106420** flag is true if the table should be stored in the auxiliary database
106421** file instead of in the main database file. This is normally the case
106422** when the "TEMP" or "TEMPORARY" keyword occurs in between
106423** CREATE and TABLE.
106424**
106425** The new table record is initialized and put in pParse->pNewTable.
106426** As more of the CREATE TABLE statement is parsed, additional action
106427** routines will be called to add more information to this record.
106428** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
106429** is called to complete the construction of the new table record.
106430*/
106431SQLITE_PRIVATE void sqlite3StartTable(
106432 Parse *pParse, /* Parser context */
106433 Token *pName1, /* First part of the name of the table or view */
106434 Token *pName2, /* Second part of the name of the table or view */
106435 int isTemp, /* True if this is a TEMP table */
106436 int isView, /* True if this is a VIEW */
106437 int isVirtual, /* True if this is a VIRTUAL table */
106438 int noErr /* Do nothing if table already exists */
106439){
106440 Table *pTable;
106441 char *zName = 0; /* The name of the new table */
106442 sqlite3 *db = pParse->db;
106443 Vdbe *v;
106444 int iDb; /* Database number to create the table in */
106445 Token *pName; /* Unqualified name of the table to create */
106446
106447 if( db->init.busy && db->init.newTnum==1 ){
106448 /* Special case: Parsing the sqlite_master or sqlite_temp_master schema */
106449 iDb = db->init.iDb;
106450 zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb));
106451 pName = pName1;
106452 }else{
106453 /* The common case */
106454 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
106455 if( iDb<0 ) return;
106456 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
106457 /* If creating a temp table, the name may not be qualified. Unless
106458 ** the database name is "temp" anyway. */
106459 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
106460 return;
106461 }
106462 if( !OMIT_TEMPDB && isTemp ) iDb = 1;
106463 zName = sqlite3NameFromToken(db, pName);
106464 if( IN_RENAME_OBJECT ){
106465 sqlite3RenameTokenMap(pParse, (void*)zName, pName);
106466 }
106467 }
106468 pParse->sNameToken = *pName;
106469 if( zName==0 ) return;
106470 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
106471 goto begin_table_error;
106472 }
106473 if( db->init.iDb==1 ) isTemp = 1;
106474#ifndef SQLITE_OMIT_AUTHORIZATION
106475 assert( isTemp==0 || isTemp==1 );
106476 assert( isView==0 || isView==1 );
106477 {
106478 static const u8 aCode[] = {
106479 SQLITE_CREATE_TABLE,
106480 SQLITE_CREATE_TEMP_TABLE,
106481 SQLITE_CREATE_VIEW,
106482 SQLITE_CREATE_TEMP_VIEW
106483 };
106484 char *zDb = db->aDb[iDb].zDbSName;
106485 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
106486 goto begin_table_error;
106487 }
106488 if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
106489 zName, 0, zDb) ){
106490 goto begin_table_error;
106491 }
106492 }
106493#endif
106494
106495 /* Make sure the new table name does not collide with an existing
106496 ** index or table name in the same database. Issue an error message if
106497 ** it does. The exception is if the statement being parsed was passed
106498 ** to an sqlite3_declare_vtab() call. In that case only the column names
106499 ** and types will be used, so there is no need to test for namespace
106500 ** collisions.
106501 */
106502 if( !IN_SPECIAL_PARSE ){
106503 char *zDb = db->aDb[iDb].zDbSName;
106504 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
106505 goto begin_table_error;
106506 }
106507 pTable = sqlite3FindTable(db, zName, zDb);
106508 if( pTable ){
106509 if( !noErr ){
106510 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
106511 }else{
106512 assert( !db->init.busy || CORRUPT_DB );
106513 sqlite3CodeVerifySchema(pParse, iDb);
106514 }
106515 goto begin_table_error;
106516 }
106517 if( sqlite3FindIndex(db, zName, zDb)!=0 ){
106518 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
106519 goto begin_table_error;
106520 }
106521 }
106522
106523 pTable = sqlite3DbMallocZero(db, sizeof(Table));
106524 if( pTable==0 ){
106525 assert( db->mallocFailed );
106526 pParse->rc = SQLITE_NOMEM_BKPT;
106527 pParse->nErr++;
106528 goto begin_table_error;
106529 }
106530 pTable->zName = zName;
106531 pTable->iPKey = -1;
106532 pTable->pSchema = db->aDb[iDb].pSchema;
106533 pTable->nTabRef = 1;
106534#ifdef SQLITE_DEFAULT_ROWEST
106535 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
106536#else
106537 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
106538#endif
106539 assert( pParse->pNewTable==0 );
106540 pParse->pNewTable = pTable;
106541
106542 /* If this is the magic sqlite_sequence table used by autoincrement,
106543 ** then record a pointer to this table in the main database structure
106544 ** so that INSERT can find the table easily.
106545 */
106546#ifndef SQLITE_OMIT_AUTOINCREMENT
106547 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
106548 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
106549 pTable->pSchema->pSeqTab = pTable;
106550 }
106551#endif
106552
106553 /* Begin generating the code that will insert the table record into
106554 ** the SQLITE_MASTER table. Note in particular that we must go ahead
106555 ** and allocate the record number for the table entry now. Before any
106556 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
106557 ** indices to be created and the table record must come before the
106558 ** indices. Hence, the record number for the table must be allocated
106559 ** now.
106560 */
106561 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
106562 int addr1;
106563 int fileFormat;
106564 int reg1, reg2, reg3;
106565 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
106566 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
106567 sqlite3BeginWriteOperation(pParse, 1, iDb);
106568
106569#ifndef SQLITE_OMIT_VIRTUALTABLE
106570 if( isVirtual ){
106571 sqlite3VdbeAddOp0(v, OP_VBegin);
106572 }
106573#endif
106574
106575 /* If the file format and encoding in the database have not been set,
106576 ** set them now.
106577 */
106578 reg1 = pParse->regRowid = ++pParse->nMem;
106579 reg2 = pParse->regRoot = ++pParse->nMem;
106580 reg3 = ++pParse->nMem;
106581 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
106582 sqlite3VdbeUsesBtree(v, iDb);
106583 addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
106584 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
106585 1 : SQLITE_MAX_FILE_FORMAT;
106586 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat);
106587 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db));
106588 sqlite3VdbeJumpHere(v, addr1);
106589
106590 /* This just creates a place-holder record in the sqlite_master table.
106591 ** The record created does not contain anything yet. It will be replaced
106592 ** by the real entry in code generated at sqlite3EndTable().
106593 **
106594 ** The rowid for the new entry is left in register pParse->regRowid.
106595 ** The root page number of the new table is left in reg pParse->regRoot.
106596 ** The rowid and root page number values are needed by the code that
106597 ** sqlite3EndTable will generate.
106598 */
106599#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
106600 if( isView || isVirtual ){
106601 sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
106602 }else
106603#endif
106604 {
106605 pParse->addrCrTab =
106606 sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY);
106607 }
106608 sqlite3OpenMasterTable(pParse, iDb);
106609 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
106610 sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC);
106611 sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
106612 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
106613 sqlite3VdbeAddOp0(v, OP_Close);
106614 }
106615
106616 /* Normal (non-error) return. */
106617 return;
106618
106619 /* If an error occurs, we jump here */
106620begin_table_error:
106621 sqlite3DbFree(db, zName);
106622 return;
106623}
106624
106625/* Set properties of a table column based on the (magical)
106626** name of the column.
106627*/
106628#if SQLITE_ENABLE_HIDDEN_COLUMNS
106629SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
106630 if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){
106631 pCol->colFlags |= COLFLAG_HIDDEN;
106632 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
106633 pTab->tabFlags |= TF_OOOHidden;
106634 }
106635}
106636#endif
106637
106638
106639/*
106640** Add a new column to the table currently being constructed.
106641**
106642** The parser calls this routine once for each column declaration
106643** in a CREATE TABLE statement. sqlite3StartTable() gets called
106644** first to get things going. Then this routine is called for each
106645** column.
106646*/
106647SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){
106648 Table *p;
106649 int i;
106650 char *z;
106651 char *zType;
106652 Column *pCol;
106653 sqlite3 *db = pParse->db;
106654 if( (p = pParse->pNewTable)==0 ) return;
106655 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
106656 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
106657 return;
106658 }
106659 z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
106660 if( z==0 ) return;
106661 if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, pName);
106662 memcpy(z, pName->z, pName->n);
106663 z[pName->n] = 0;
106664 sqlite3Dequote(z);
106665 for(i=0; i<p->nCol; i++){
106666 if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
106667 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
106668 sqlite3DbFree(db, z);
106669 return;
106670 }
106671 }
106672 if( (p->nCol & 0x7)==0 ){
106673 Column *aNew;
106674 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
106675 if( aNew==0 ){
106676 sqlite3DbFree(db, z);
106677 return;
106678 }
106679 p->aCol = aNew;
106680 }
106681 pCol = &p->aCol[p->nCol];
106682 memset(pCol, 0, sizeof(p->aCol[0]));
106683 pCol->zName = z;
106684 sqlite3ColumnPropertiesFromName(p, pCol);
106685
106686 if( pType->n==0 ){
106687 /* If there is no type specified, columns have the default affinity
106688 ** 'BLOB' with a default size of 4 bytes. */
106689 pCol->affinity = SQLITE_AFF_BLOB;
106690 pCol->szEst = 1;
106691#ifdef SQLITE_ENABLE_SORTER_REFERENCES
106692 if( 4>=sqlite3GlobalConfig.szSorterRef ){
106693 pCol->colFlags |= COLFLAG_SORTERREF;
106694 }
106695#endif
106696 }else{
106697 zType = z + sqlite3Strlen30(z) + 1;
106698 memcpy(zType, pType->z, pType->n);
106699 zType[pType->n] = 0;
106700 sqlite3Dequote(zType);
106701 pCol->affinity = sqlite3AffinityType(zType, pCol);
106702 pCol->colFlags |= COLFLAG_HASTYPE;
106703 }
106704 p->nCol++;
106705 pParse->constraintName.n = 0;
106706}
106707
106708/*
106709** This routine is called by the parser while in the middle of
106710** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
106711** been seen on a column. This routine sets the notNull flag on
106712** the column currently under construction.
106713*/
106714SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
106715 Table *p;
106716 Column *pCol;
106717 p = pParse->pNewTable;
106718 if( p==0 || NEVER(p->nCol<1) ) return;
106719 pCol = &p->aCol[p->nCol-1];
106720 pCol->notNull = (u8)onError;
106721 p->tabFlags |= TF_HasNotNull;
106722
106723 /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
106724 ** on this column. */
106725 if( pCol->colFlags & COLFLAG_UNIQUE ){
106726 Index *pIdx;
106727 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
106728 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
106729 if( pIdx->aiColumn[0]==p->nCol-1 ){
106730 pIdx->uniqNotNull = 1;
106731 }
106732 }
106733 }
106734}
106735
106736/*
106737** Scan the column type name zType (length nType) and return the
106738** associated affinity type.
106739**
106740** This routine does a case-independent search of zType for the
106741** substrings in the following table. If one of the substrings is
106742** found, the corresponding affinity is returned. If zType contains
106743** more than one of the substrings, entries toward the top of
106744** the table take priority. For example, if zType is 'BLOBINT',
106745** SQLITE_AFF_INTEGER is returned.
106746**
106747** Substring | Affinity
106748** --------------------------------
106749** 'INT' | SQLITE_AFF_INTEGER
106750** 'CHAR' | SQLITE_AFF_TEXT
106751** 'CLOB' | SQLITE_AFF_TEXT
106752** 'TEXT' | SQLITE_AFF_TEXT
106753** 'BLOB' | SQLITE_AFF_BLOB
106754** 'REAL' | SQLITE_AFF_REAL
106755** 'FLOA' | SQLITE_AFF_REAL
106756** 'DOUB' | SQLITE_AFF_REAL
106757**
106758** If none of the substrings in the above table are found,
106759** SQLITE_AFF_NUMERIC is returned.
106760*/
106761SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, Column *pCol){
106762 u32 h = 0;
106763 char aff = SQLITE_AFF_NUMERIC;
106764 const char *zChar = 0;
106765
106766 assert( zIn!=0 );
106767 while( zIn[0] ){
106768 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
106769 zIn++;
106770 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
106771 aff = SQLITE_AFF_TEXT;
106772 zChar = zIn;
106773 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
106774 aff = SQLITE_AFF_TEXT;
106775 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
106776 aff = SQLITE_AFF_TEXT;
106777 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
106778 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
106779 aff = SQLITE_AFF_BLOB;
106780 if( zIn[0]=='(' ) zChar = zIn;
106781#ifndef SQLITE_OMIT_FLOATING_POINT
106782 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
106783 && aff==SQLITE_AFF_NUMERIC ){
106784 aff = SQLITE_AFF_REAL;
106785 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
106786 && aff==SQLITE_AFF_NUMERIC ){
106787 aff = SQLITE_AFF_REAL;
106788 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
106789 && aff==SQLITE_AFF_NUMERIC ){
106790 aff = SQLITE_AFF_REAL;
106791#endif
106792 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
106793 aff = SQLITE_AFF_INTEGER;
106794 break;
106795 }
106796 }
106797
106798 /* If pCol is not NULL, store an estimate of the field size. The
106799 ** estimate is scaled so that the size of an integer is 1. */
106800 if( pCol ){
106801 int v = 0; /* default size is approx 4 bytes */
106802 if( aff<SQLITE_AFF_NUMERIC ){
106803 if( zChar ){
106804 while( zChar[0] ){
106805 if( sqlite3Isdigit(zChar[0]) ){
106806 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
106807 sqlite3GetInt32(zChar, &v);
106808 break;
106809 }
106810 zChar++;
106811 }
106812 }else{
106813 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
106814 }
106815 }
106816#ifdef SQLITE_ENABLE_SORTER_REFERENCES
106817 if( v>=sqlite3GlobalConfig.szSorterRef ){
106818 pCol->colFlags |= COLFLAG_SORTERREF;
106819 }
106820#endif
106821 v = v/4 + 1;
106822 if( v>255 ) v = 255;
106823 pCol->szEst = v;
106824 }
106825 return aff;
106826}
106827
106828/*
106829** The expression is the default value for the most recently added column
106830** of the table currently under construction.
106831**
106832** Default value expressions must be constant. Raise an exception if this
106833** is not the case.
106834**
106835** This routine is called by the parser while in the middle of
106836** parsing a CREATE TABLE statement.
106837*/
106838SQLITE_PRIVATE void sqlite3AddDefaultValue(
106839 Parse *pParse, /* Parsing context */
106840 Expr *pExpr, /* The parsed expression of the default value */
106841 const char *zStart, /* Start of the default value text */
106842 const char *zEnd /* First character past end of defaut value text */
106843){
106844 Table *p;
106845 Column *pCol;
106846 sqlite3 *db = pParse->db;
106847 p = pParse->pNewTable;
106848 if( p!=0 ){
106849 pCol = &(p->aCol[p->nCol-1]);
106850 if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
106851 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
106852 pCol->zName);
106853 }else{
106854 /* A copy of pExpr is used instead of the original, as pExpr contains
106855 ** tokens that point to volatile memory.
106856 */
106857 Expr x;
106858 sqlite3ExprDelete(db, pCol->pDflt);
106859 memset(&x, 0, sizeof(x));
106860 x.op = TK_SPAN;
106861 x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
106862 x.pLeft = pExpr;
106863 x.flags = EP_Skip;
106864 pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
106865 sqlite3DbFree(db, x.u.zToken);
106866 }
106867 }
106868 if( IN_RENAME_OBJECT ){
106869 sqlite3RenameExprUnmap(pParse, pExpr);
106870 }
106871 sqlite3ExprDelete(db, pExpr);
106872}
106873
106874/*
106875** Backwards Compatibility Hack:
106876**
106877** Historical versions of SQLite accepted strings as column names in
106878** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example:
106879**
106880** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
106881** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
106882**
106883** This is goofy. But to preserve backwards compatibility we continue to
106884** accept it. This routine does the necessary conversion. It converts
106885** the expression given in its argument from a TK_STRING into a TK_ID
106886** if the expression is just a TK_STRING with an optional COLLATE clause.
106887** If the epxression is anything other than TK_STRING, the expression is
106888** unchanged.
106889*/
106890static void sqlite3StringToId(Expr *p){
106891 if( p->op==TK_STRING ){
106892 p->op = TK_ID;
106893 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
106894 p->pLeft->op = TK_ID;
106895 }
106896}
106897
106898/*
106899** Designate the PRIMARY KEY for the table. pList is a list of names
106900** of columns that form the primary key. If pList is NULL, then the
106901** most recently added column of the table is the primary key.
106902**
106903** A table can have at most one primary key. If the table already has
106904** a primary key (and this is the second primary key) then create an
106905** error.
106906**
106907** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
106908** then we will try to use that column as the rowid. Set the Table.iPKey
106909** field of the table under construction to be the index of the
106910** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
106911** no INTEGER PRIMARY KEY.
106912**
106913** If the key is not an INTEGER PRIMARY KEY, then create a unique
106914** index for the key. No index is created for INTEGER PRIMARY KEYs.
106915*/
106916SQLITE_PRIVATE void sqlite3AddPrimaryKey(
106917 Parse *pParse, /* Parsing context */
106918 ExprList *pList, /* List of field names to be indexed */
106919 int onError, /* What to do with a uniqueness conflict */
106920 int autoInc, /* True if the AUTOINCREMENT keyword is present */
106921 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
106922){
106923 Table *pTab = pParse->pNewTable;
106924 Column *pCol = 0;
106925 int iCol = -1, i;
106926 int nTerm;
106927 if( pTab==0 ) goto primary_key_exit;
106928 if( pTab->tabFlags & TF_HasPrimaryKey ){
106929 sqlite3ErrorMsg(pParse,
106930 "table \"%s\" has more than one primary key", pTab->zName);
106931 goto primary_key_exit;
106932 }
106933 pTab->tabFlags |= TF_HasPrimaryKey;
106934 if( pList==0 ){
106935 iCol = pTab->nCol - 1;
106936 pCol = &pTab->aCol[iCol];
106937 pCol->colFlags |= COLFLAG_PRIMKEY;
106938 nTerm = 1;
106939 }else{
106940 nTerm = pList->nExpr;
106941 for(i=0; i<nTerm; i++){
106942 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
106943 assert( pCExpr!=0 );
106944 sqlite3StringToId(pCExpr);
106945 if( pCExpr->op==TK_ID ){
106946 const char *zCName = pCExpr->u.zToken;
106947 for(iCol=0; iCol<pTab->nCol; iCol++){
106948 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){
106949 pCol = &pTab->aCol[iCol];
106950 pCol->colFlags |= COLFLAG_PRIMKEY;
106951 break;
106952 }
106953 }
106954 }
106955 }
106956 }
106957 if( nTerm==1
106958 && pCol
106959 && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
106960 && sortOrder!=SQLITE_SO_DESC
106961 ){
106962 if( IN_RENAME_OBJECT && pList ){
106963 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pList->a[0].pExpr);
106964 }
106965 pTab->iPKey = iCol;
106966 pTab->keyConf = (u8)onError;
106967 assert( autoInc==0 || autoInc==1 );
106968 pTab->tabFlags |= autoInc*TF_Autoincrement;
106969 if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
106970 }else if( autoInc ){
106971#ifndef SQLITE_OMIT_AUTOINCREMENT
106972 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
106973 "INTEGER PRIMARY KEY");
106974#endif
106975 }else{
106976 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
106977 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY);
106978 pList = 0;
106979 }
106980
106981primary_key_exit:
106982 sqlite3ExprListDelete(pParse->db, pList);
106983 return;
106984}
106985
106986/*
106987** Add a new CHECK constraint to the table currently under construction.
106988*/
106989SQLITE_PRIVATE void sqlite3AddCheckConstraint(
106990 Parse *pParse, /* Parsing context */
106991 Expr *pCheckExpr /* The check expression */
106992){
106993#ifndef SQLITE_OMIT_CHECK
106994 Table *pTab = pParse->pNewTable;
106995 sqlite3 *db = pParse->db;
106996 if( pTab && !IN_DECLARE_VTAB
106997 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
106998 ){
106999 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
107000 if( pParse->constraintName.n ){
107001 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
107002 }
107003 }else
107004#endif
107005 {
107006 sqlite3ExprDelete(pParse->db, pCheckExpr);
107007 }
107008}
107009
107010/*
107011** Set the collation function of the most recently parsed table column
107012** to the CollSeq given.
107013*/
107014SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
107015 Table *p;
107016 int i;
107017 char *zColl; /* Dequoted name of collation sequence */
107018 sqlite3 *db;
107019
107020 if( (p = pParse->pNewTable)==0 ) return;
107021 i = p->nCol-1;
107022 db = pParse->db;
107023 zColl = sqlite3NameFromToken(db, pToken);
107024 if( !zColl ) return;
107025
107026 if( sqlite3LocateCollSeq(pParse, zColl) ){
107027 Index *pIdx;
107028 sqlite3DbFree(db, p->aCol[i].zColl);
107029 p->aCol[i].zColl = zColl;
107030
107031 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
107032 ** then an index may have been created on this column before the
107033 ** collation type was added. Correct this if it is the case.
107034 */
107035 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
107036 assert( pIdx->nKeyCol==1 );
107037 if( pIdx->aiColumn[0]==i ){
107038 pIdx->azColl[0] = p->aCol[i].zColl;
107039 }
107040 }
107041 }else{
107042 sqlite3DbFree(db, zColl);
107043 }
107044}
107045
107046/*
107047** This function returns the collation sequence for database native text
107048** encoding identified by the string zName, length nName.
107049**
107050** If the requested collation sequence is not available, or not available
107051** in the database native encoding, the collation factory is invoked to
107052** request it. If the collation factory does not supply such a sequence,
107053** and the sequence is available in another text encoding, then that is
107054** returned instead.
107055**
107056** If no versions of the requested collations sequence are available, or
107057** another error occurs, NULL is returned and an error message written into
107058** pParse.
107059**
107060** This routine is a wrapper around sqlite3FindCollSeq(). This routine
107061** invokes the collation factory if the named collation cannot be found
107062** and generates an error message.
107063**
107064** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
107065*/
107066SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
107067 sqlite3 *db = pParse->db;
107068 u8 enc = ENC(db);
107069 u8 initbusy = db->init.busy;
107070 CollSeq *pColl;
107071
107072 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
107073 if( !initbusy && (!pColl || !pColl->xCmp) ){
107074 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
107075 }
107076
107077 return pColl;
107078}
107079
107080
107081/*
107082** Generate code that will increment the schema cookie.
107083**
107084** The schema cookie is used to determine when the schema for the
107085** database changes. After each schema change, the cookie value
107086** changes. When a process first reads the schema it records the
107087** cookie. Thereafter, whenever it goes to access the database,
107088** it checks the cookie to make sure the schema has not changed
107089** since it was last read.
107090**
107091** This plan is not completely bullet-proof. It is possible for
107092** the schema to change multiple times and for the cookie to be
107093** set back to prior value. But schema changes are infrequent
107094** and the probability of hitting the same cookie value is only
107095** 1 chance in 2^32. So we're safe enough.
107096**
107097** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
107098** the schema-version whenever the schema changes.
107099*/
107100SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
107101 sqlite3 *db = pParse->db;
107102 Vdbe *v = pParse->pVdbe;
107103 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
107104 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION,
107105 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
107106}
107107
107108/*
107109** Measure the number of characters needed to output the given
107110** identifier. The number returned includes any quotes used
107111** but does not include the null terminator.
107112**
107113** The estimate is conservative. It might be larger that what is
107114** really needed.
107115*/
107116static int identLength(const char *z){
107117 int n;
107118 for(n=0; *z; n++, z++){
107119 if( *z=='"' ){ n++; }
107120 }
107121 return n + 2;
107122}
107123
107124/*
107125** The first parameter is a pointer to an output buffer. The second
107126** parameter is a pointer to an integer that contains the offset at
107127** which to write into the output buffer. This function copies the
107128** nul-terminated string pointed to by the third parameter, zSignedIdent,
107129** to the specified offset in the buffer and updates *pIdx to refer
107130** to the first byte after the last byte written before returning.
107131**
107132** If the string zSignedIdent consists entirely of alpha-numeric
107133** characters, does not begin with a digit and is not an SQL keyword,
107134** then it is copied to the output buffer exactly as it is. Otherwise,
107135** it is quoted using double-quotes.
107136*/
107137static void identPut(char *z, int *pIdx, char *zSignedIdent){
107138 unsigned char *zIdent = (unsigned char*)zSignedIdent;
107139 int i, j, needQuote;
107140 i = *pIdx;
107141
107142 for(j=0; zIdent[j]; j++){
107143 if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
107144 }
107145 needQuote = sqlite3Isdigit(zIdent[0])
107146 || sqlite3KeywordCode(zIdent, j)!=TK_ID
107147 || zIdent[j]!=0
107148 || j==0;
107149
107150 if( needQuote ) z[i++] = '"';
107151 for(j=0; zIdent[j]; j++){
107152 z[i++] = zIdent[j];
107153 if( zIdent[j]=='"' ) z[i++] = '"';
107154 }
107155 if( needQuote ) z[i++] = '"';
107156 z[i] = 0;
107157 *pIdx = i;
107158}
107159
107160/*
107161** Generate a CREATE TABLE statement appropriate for the given
107162** table. Memory to hold the text of the statement is obtained
107163** from sqliteMalloc() and must be freed by the calling function.
107164*/
107165static char *createTableStmt(sqlite3 *db, Table *p){
107166 int i, k, n;
107167 char *zStmt;
107168 char *zSep, *zSep2, *zEnd;
107169 Column *pCol;
107170 n = 0;
107171 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
107172 n += identLength(pCol->zName) + 5;
107173 }
107174 n += identLength(p->zName);
107175 if( n<50 ){
107176 zSep = "";
107177 zSep2 = ",";
107178 zEnd = ")";
107179 }else{
107180 zSep = "\n ";
107181 zSep2 = ",\n ";
107182 zEnd = "\n)";
107183 }
107184 n += 35 + 6*p->nCol;
107185 zStmt = sqlite3DbMallocRaw(0, n);
107186 if( zStmt==0 ){
107187 sqlite3OomFault(db);
107188 return 0;
107189 }
107190 sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
107191 k = sqlite3Strlen30(zStmt);
107192 identPut(zStmt, &k, p->zName);
107193 zStmt[k++] = '(';
107194 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
107195 static const char * const azType[] = {
107196 /* SQLITE_AFF_BLOB */ "",
107197 /* SQLITE_AFF_TEXT */ " TEXT",
107198 /* SQLITE_AFF_NUMERIC */ " NUM",
107199 /* SQLITE_AFF_INTEGER */ " INT",
107200 /* SQLITE_AFF_REAL */ " REAL"
107201 };
107202 int len;
107203 const char *zType;
107204
107205 sqlite3_snprintf(n-k, &zStmt[k], zSep);
107206 k += sqlite3Strlen30(&zStmt[k]);
107207 zSep = zSep2;
107208 identPut(zStmt, &k, pCol->zName);
107209 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
107210 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
107211 testcase( pCol->affinity==SQLITE_AFF_BLOB );
107212 testcase( pCol->affinity==SQLITE_AFF_TEXT );
107213 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
107214 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
107215 testcase( pCol->affinity==SQLITE_AFF_REAL );
107216
107217 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
107218 len = sqlite3Strlen30(zType);
107219 assert( pCol->affinity==SQLITE_AFF_BLOB
107220 || pCol->affinity==sqlite3AffinityType(zType, 0) );
107221 memcpy(&zStmt[k], zType, len);
107222 k += len;
107223 assert( k<=n );
107224 }
107225 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
107226 return zStmt;
107227}
107228
107229/*
107230** Resize an Index object to hold N columns total. Return SQLITE_OK
107231** on success and SQLITE_NOMEM on an OOM error.
107232*/
107233static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
107234 char *zExtra;
107235 int nByte;
107236 if( pIdx->nColumn>=N ) return SQLITE_OK;
107237 assert( pIdx->isResized==0 );
107238 nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
107239 zExtra = sqlite3DbMallocZero(db, nByte);
107240 if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
107241 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
107242 pIdx->azColl = (const char**)zExtra;
107243 zExtra += sizeof(char*)*N;
107244 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
107245 pIdx->aiColumn = (i16*)zExtra;
107246 zExtra += sizeof(i16)*N;
107247 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
107248 pIdx->aSortOrder = (u8*)zExtra;
107249 pIdx->nColumn = N;
107250 pIdx->isResized = 1;
107251 return SQLITE_OK;
107252}
107253
107254/*
107255** Estimate the total row width for a table.
107256*/
107257static void estimateTableWidth(Table *pTab){
107258 unsigned wTable = 0;
107259 const Column *pTabCol;
107260 int i;
107261 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
107262 wTable += pTabCol->szEst;
107263 }
107264 if( pTab->iPKey<0 ) wTable++;
107265 pTab->szTabRow = sqlite3LogEst(wTable*4);
107266}
107267
107268/*
107269** Estimate the average size of a row for an index.
107270*/
107271static void estimateIndexWidth(Index *pIdx){
107272 unsigned wIndex = 0;
107273 int i;
107274 const Column *aCol = pIdx->pTable->aCol;
107275 for(i=0; i<pIdx->nColumn; i++){
107276 i16 x = pIdx->aiColumn[i];
107277 assert( x<pIdx->pTable->nCol );
107278 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
107279 }
107280 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
107281}
107282
107283/* Return true if value x is found any of the first nCol entries of aiCol[]
107284*/
107285static int hasColumn(const i16 *aiCol, int nCol, int x){
107286 while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
107287 return 0;
107288}
107289
107290/* Recompute the colNotIdxed field of the Index.
107291**
107292** colNotIdxed is a bitmask that has a 0 bit representing each indexed
107293** columns that are within the first 63 columns of the table. The
107294** high-order bit of colNotIdxed is always 1. All unindexed columns
107295** of the table have a 1.
107296**
107297** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
107298** to determine if the index is covering index.
107299*/
107300static void recomputeColumnsNotIndexed(Index *pIdx){
107301 Bitmask m = 0;
107302 int j;
107303 for(j=pIdx->nColumn-1; j>=0; j--){
107304 int x = pIdx->aiColumn[j];
107305 if( x>=0 ){
107306 testcase( x==BMS-1 );
107307 testcase( x==BMS-2 );
107308 if( x<BMS-1 ) m |= MASKBIT(x);
107309 }
107310 }
107311 pIdx->colNotIdxed = ~m;
107312 assert( (pIdx->colNotIdxed>>63)==1 );
107313}
107314
107315/*
107316** This routine runs at the end of parsing a CREATE TABLE statement that
107317** has a WITHOUT ROWID clause. The job of this routine is to convert both
107318** internal schema data structures and the generated VDBE code so that they
107319** are appropriate for a WITHOUT ROWID table instead of a rowid table.
107320** Changes include:
107321**
107322** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL.
107323** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY
107324** into BTREE_BLOBKEY.
107325** (3) Bypass the creation of the sqlite_master table entry
107326** for the PRIMARY KEY as the primary key index is now
107327** identified by the sqlite_master table entry of the table itself.
107328** (4) Set the Index.tnum of the PRIMARY KEY Index object in the
107329** schema to the rootpage from the main table.
107330** (5) Add all table columns to the PRIMARY KEY Index object
107331** so that the PRIMARY KEY is a covering index. The surplus
107332** columns are part of KeyInfo.nAllField and are not used for
107333** sorting or lookup or uniqueness checks.
107334** (6) Replace the rowid tail on all automatically generated UNIQUE
107335** indices with the PRIMARY KEY columns.
107336**
107337** For virtual tables, only (1) is performed.
107338*/
107339static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
107340 Index *pIdx;
107341 Index *pPk;
107342 int nPk;
107343 int i, j;
107344 sqlite3 *db = pParse->db;
107345 Vdbe *v = pParse->pVdbe;
107346
107347 /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
107348 */
107349 if( !db->init.imposterTable ){
107350 for(i=0; i<pTab->nCol; i++){
107351 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
107352 pTab->aCol[i].notNull = OE_Abort;
107353 }
107354 }
107355 }
107356
107357 /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
107358 ** into BTREE_BLOBKEY.
107359 */
107360 if( pParse->addrCrTab ){
107361 assert( v );
107362 sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY);
107363 }
107364
107365 /* Locate the PRIMARY KEY index. Or, if this table was originally
107366 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
107367 */
107368 if( pTab->iPKey>=0 ){
107369 ExprList *pList;
107370 Token ipkToken;
107371 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
107372 pList = sqlite3ExprListAppend(pParse, 0,
107373 sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
107374 if( pList==0 ) return;
107375 pList->a[0].sortOrder = pParse->iPkSortOrder;
107376 assert( pParse->pNewTable==pTab );
107377 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
107378 SQLITE_IDXTYPE_PRIMARYKEY);
107379 if( db->mallocFailed || pParse->nErr ) return;
107380 pPk = sqlite3PrimaryKeyIndex(pTab);
107381 pTab->iPKey = -1;
107382 }else{
107383 pPk = sqlite3PrimaryKeyIndex(pTab);
107384
107385 /*
107386 ** Remove all redundant columns from the PRIMARY KEY. For example, change
107387 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
107388 ** code assumes the PRIMARY KEY contains no repeated columns.
107389 */
107390 for(i=j=1; i<pPk->nKeyCol; i++){
107391 if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){
107392 pPk->nColumn--;
107393 }else{
107394 pPk->aiColumn[j++] = pPk->aiColumn[i];
107395 }
107396 }
107397 pPk->nKeyCol = j;
107398 }
107399 assert( pPk!=0 );
107400 pPk->isCovering = 1;
107401 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
107402 nPk = pPk->nKeyCol;
107403
107404 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
107405 ** table entry. This is only required if currently generating VDBE
107406 ** code for a CREATE TABLE (not when parsing one as part of reading
107407 ** a database schema). */
107408 if( v && pPk->tnum>0 ){
107409 assert( db->init.busy==0 );
107410 sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto);
107411 }
107412
107413 /* The root page of the PRIMARY KEY is the table root page */
107414 pPk->tnum = pTab->tnum;
107415
107416 /* Update the in-memory representation of all UNIQUE indices by converting
107417 ** the final rowid column into one or more columns of the PRIMARY KEY.
107418 */
107419 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
107420 int n;
107421 if( IsPrimaryKeyIndex(pIdx) ) continue;
107422 for(i=n=0; i<nPk; i++){
107423 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
107424 }
107425 if( n==0 ){
107426 /* This index is a superset of the primary key */
107427 pIdx->nColumn = pIdx->nKeyCol;
107428 continue;
107429 }
107430 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
107431 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
107432 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
107433 pIdx->aiColumn[j] = pPk->aiColumn[i];
107434 pIdx->azColl[j] = pPk->azColl[i];
107435 j++;
107436 }
107437 }
107438 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
107439 assert( pIdx->nColumn>=j );
107440 }
107441
107442 /* Add all table columns to the PRIMARY KEY index
107443 */
107444 if( nPk<pTab->nCol ){
107445 if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
107446 for(i=0, j=nPk; i<pTab->nCol; i++){
107447 if( !hasColumn(pPk->aiColumn, j, i) ){
107448 assert( j<pPk->nColumn );
107449 pPk->aiColumn[j] = i;
107450 pPk->azColl[j] = sqlite3StrBINARY;
107451 j++;
107452 }
107453 }
107454 assert( pPk->nColumn==j );
107455 assert( pTab->nCol==j );
107456 }else{
107457 pPk->nColumn = pTab->nCol;
107458 }
107459 recomputeColumnsNotIndexed(pPk);
107460}
107461
107462/*
107463** This routine is called to report the final ")" that terminates
107464** a CREATE TABLE statement.
107465**
107466** The table structure that other action routines have been building
107467** is added to the internal hash tables, assuming no errors have
107468** occurred.
107469**
107470** An entry for the table is made in the master table on disk, unless
107471** this is a temporary table or db->init.busy==1. When db->init.busy==1
107472** it means we are reading the sqlite_master table because we just
107473** connected to the database or because the sqlite_master table has
107474** recently changed, so the entry for this table already exists in
107475** the sqlite_master table. We do not want to create it again.
107476**
107477** If the pSelect argument is not NULL, it means that this routine
107478** was called to create a table generated from a
107479** "CREATE TABLE ... AS SELECT ..." statement. The column names of
107480** the new table will match the result set of the SELECT.
107481*/
107482SQLITE_PRIVATE void sqlite3EndTable(
107483 Parse *pParse, /* Parse context */
107484 Token *pCons, /* The ',' token after the last column defn. */
107485 Token *pEnd, /* The ')' before options in the CREATE TABLE */
107486 u8 tabOpts, /* Extra table options. Usually 0. */
107487 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
107488){
107489 Table *p; /* The new table */
107490 sqlite3 *db = pParse->db; /* The database connection */
107491 int iDb; /* Database in which the table lives */
107492 Index *pIdx; /* An implied index of the table */
107493
107494 if( pEnd==0 && pSelect==0 ){
107495 return;
107496 }
107497 assert( !db->mallocFailed );
107498 p = pParse->pNewTable;
107499 if( p==0 ) return;
107500
107501 /* If the db->init.busy is 1 it means we are reading the SQL off the
107502 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
107503 ** So do not write to the disk again. Extract the root page number
107504 ** for the table from the db->init.newTnum field. (The page number
107505 ** should have been put there by the sqliteOpenCb routine.)
107506 **
107507 ** If the root page number is 1, that means this is the sqlite_master
107508 ** table itself. So mark it read-only.
107509 */
107510 if( db->init.busy ){
107511 if( pSelect ){
107512 sqlite3ErrorMsg(pParse, "");
107513 return;
107514 }
107515 p->tnum = db->init.newTnum;
107516 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
107517 }
107518
107519 /* Special processing for WITHOUT ROWID Tables */
107520 if( tabOpts & TF_WithoutRowid ){
107521 if( (p->tabFlags & TF_Autoincrement) ){
107522 sqlite3ErrorMsg(pParse,
107523 "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
107524 return;
107525 }
107526 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
107527 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
107528 }else{
107529 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
107530 convertToWithoutRowidTable(pParse, p);
107531 }
107532 }
107533
107534 iDb = sqlite3SchemaToIndex(db, p->pSchema);
107535
107536#ifndef SQLITE_OMIT_CHECK
107537 /* Resolve names in all CHECK constraint expressions.
107538 */
107539 if( p->pCheck ){
107540 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
107541 }
107542#endif /* !defined(SQLITE_OMIT_CHECK) */
107543
107544 /* Estimate the average row size for the table and for all implied indices */
107545 estimateTableWidth(p);
107546 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
107547 estimateIndexWidth(pIdx);
107548 }
107549
107550 /* If not initializing, then create a record for the new table
107551 ** in the SQLITE_MASTER table of the database.
107552 **
107553 ** If this is a TEMPORARY table, write the entry into the auxiliary
107554 ** file instead of into the main database file.
107555 */
107556 if( !db->init.busy ){
107557 int n;
107558 Vdbe *v;
107559 char *zType; /* "view" or "table" */
107560 char *zType2; /* "VIEW" or "TABLE" */
107561 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
107562
107563 v = sqlite3GetVdbe(pParse);
107564 if( NEVER(v==0) ) return;
107565
107566 sqlite3VdbeAddOp1(v, OP_Close, 0);
107567
107568 /*
107569 ** Initialize zType for the new view or table.
107570 */
107571 if( p->pSelect==0 ){
107572 /* A regular table */
107573 zType = "table";
107574 zType2 = "TABLE";
107575#ifndef SQLITE_OMIT_VIEW
107576 }else{
107577 /* A view */
107578 zType = "view";
107579 zType2 = "VIEW";
107580#endif
107581 }
107582
107583 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
107584 ** statement to populate the new table. The root-page number for the
107585 ** new table is in register pParse->regRoot.
107586 **
107587 ** Once the SELECT has been coded by sqlite3Select(), it is in a
107588 ** suitable state to query for the column names and types to be used
107589 ** by the new table.
107590 **
107591 ** A shared-cache write-lock is not required to write to the new table,
107592 ** as a schema-lock must have already been obtained to create it. Since
107593 ** a schema-lock excludes all other database users, the write-lock would
107594 ** be redundant.
107595 */
107596 if( pSelect ){
107597 SelectDest dest; /* Where the SELECT should store results */
107598 int regYield; /* Register holding co-routine entry-point */
107599 int addrTop; /* Top of the co-routine */
107600 int regRec; /* A record to be insert into the new table */
107601 int regRowid; /* Rowid of the next row to insert */
107602 int addrInsLoop; /* Top of the loop for inserting rows */
107603 Table *pSelTab; /* A table that describes the SELECT results */
107604
107605 regYield = ++pParse->nMem;
107606 regRec = ++pParse->nMem;
107607 regRowid = ++pParse->nMem;
107608 assert(pParse->nTab==1);
107609 sqlite3MayAbort(pParse);
107610 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
107611 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
107612 pParse->nTab = 2;
107613 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
107614 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
107615 if( pParse->nErr ) return;
107616 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
107617 if( pSelTab==0 ) return;
107618 assert( p->aCol==0 );
107619 p->nCol = pSelTab->nCol;
107620 p->aCol = pSelTab->aCol;
107621 pSelTab->nCol = 0;
107622 pSelTab->aCol = 0;
107623 sqlite3DeleteTable(db, pSelTab);
107624 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
107625 sqlite3Select(pParse, pSelect, &dest);
107626 if( pParse->nErr ) return;
107627 sqlite3VdbeEndCoroutine(v, regYield);
107628 sqlite3VdbeJumpHere(v, addrTop - 1);
107629 addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
107630 VdbeCoverage(v);
107631 sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
107632 sqlite3TableAffinity(v, p, 0);
107633 sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
107634 sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
107635 sqlite3VdbeGoto(v, addrInsLoop);
107636 sqlite3VdbeJumpHere(v, addrInsLoop);
107637 sqlite3VdbeAddOp1(v, OP_Close, 1);
107638 }
107639
107640 /* Compute the complete text of the CREATE statement */
107641 if( pSelect ){
107642 zStmt = createTableStmt(db, p);
107643 }else{
107644 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
107645 n = (int)(pEnd2->z - pParse->sNameToken.z);
107646 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
107647 zStmt = sqlite3MPrintf(db,
107648 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
107649 );
107650 }
107651
107652 /* A slot for the record has already been allocated in the
107653 ** SQLITE_MASTER table. We just need to update that slot with all
107654 ** the information we've collected.
107655 */
107656 sqlite3NestedParse(pParse,
107657 "UPDATE %Q.%s "
107658 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
107659 "WHERE rowid=#%d",
107660 db->aDb[iDb].zDbSName, MASTER_NAME,
107661 zType,
107662 p->zName,
107663 p->zName,
107664 pParse->regRoot,
107665 zStmt,
107666 pParse->regRowid
107667 );
107668 sqlite3DbFree(db, zStmt);
107669 sqlite3ChangeCookie(pParse, iDb);
107670
107671#ifndef SQLITE_OMIT_AUTOINCREMENT
107672 /* Check to see if we need to create an sqlite_sequence table for
107673 ** keeping track of autoincrement keys.
107674 */
107675 if( (p->tabFlags & TF_Autoincrement)!=0 ){
107676 Db *pDb = &db->aDb[iDb];
107677 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
107678 if( pDb->pSchema->pSeqTab==0 ){
107679 sqlite3NestedParse(pParse,
107680 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
107681 pDb->zDbSName
107682 );
107683 }
107684 }
107685#endif
107686
107687 /* Reparse everything to update our internal data structures */
107688 sqlite3VdbeAddParseSchemaOp(v, iDb,
107689 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
107690 }
107691
107692
107693 /* Add the table to the in-memory representation of the database.
107694 */
107695 if( db->init.busy ){
107696 Table *pOld;
107697 Schema *pSchema = p->pSchema;
107698 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
107699 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
107700 if( pOld ){
107701 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
107702 sqlite3OomFault(db);
107703 return;
107704 }
107705 pParse->pNewTable = 0;
107706 db->mDbFlags |= DBFLAG_SchemaChange;
107707
107708#ifndef SQLITE_OMIT_ALTERTABLE
107709 if( !p->pSelect ){
107710 const char *zName = (const char *)pParse->sNameToken.z;
107711 int nName;
107712 assert( !pSelect && pCons && pEnd );
107713 if( pCons->z==0 ){
107714 pCons = pEnd;
107715 }
107716 nName = (int)((const char *)pCons->z - zName);
107717 p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
107718 }
107719#endif
107720 }
107721}
107722
107723#ifndef SQLITE_OMIT_VIEW
107724/*
107725** The parser calls this routine in order to create a new VIEW
107726*/
107727SQLITE_PRIVATE void sqlite3CreateView(
107728 Parse *pParse, /* The parsing context */
107729 Token *pBegin, /* The CREATE token that begins the statement */
107730 Token *pName1, /* The token that holds the name of the view */
107731 Token *pName2, /* The token that holds the name of the view */
107732 ExprList *pCNames, /* Optional list of view column names */
107733 Select *pSelect, /* A SELECT statement that will become the new view */
107734 int isTemp, /* TRUE for a TEMPORARY view */
107735 int noErr /* Suppress error messages if VIEW already exists */
107736){
107737 Table *p;
107738 int n;
107739 const char *z;
107740 Token sEnd;
107741 DbFixer sFix;
107742 Token *pName = 0;
107743 int iDb;
107744 sqlite3 *db = pParse->db;
107745
107746 if( pParse->nVar>0 ){
107747 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
107748 goto create_view_fail;
107749 }
107750 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
107751 p = pParse->pNewTable;
107752 if( p==0 || pParse->nErr ) goto create_view_fail;
107753 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
107754 iDb = sqlite3SchemaToIndex(db, p->pSchema);
107755 sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
107756 if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
107757
107758 /* Make a copy of the entire SELECT statement that defines the view.
107759 ** This will force all the Expr.token.z values to be dynamically
107760 ** allocated rather than point to the input string - which means that
107761 ** they will persist after the current sqlite3_exec() call returns.
107762 */
107763 if( IN_RENAME_OBJECT ){
107764 p->pSelect = pSelect;
107765 pSelect = 0;
107766 }else{
107767 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
107768 }
107769 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
107770 if( db->mallocFailed ) goto create_view_fail;
107771
107772 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
107773 ** the end.
107774 */
107775 sEnd = pParse->sLastToken;
107776 assert( sEnd.z[0]!=0 || sEnd.n==0 );
107777 if( sEnd.z[0]!=';' ){
107778 sEnd.z += sEnd.n;
107779 }
107780 sEnd.n = 0;
107781 n = (int)(sEnd.z - pBegin->z);
107782 assert( n>0 );
107783 z = pBegin->z;
107784 while( sqlite3Isspace(z[n-1]) ){ n--; }
107785 sEnd.z = &z[n-1];
107786 sEnd.n = 1;
107787
107788 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
107789 sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
107790
107791create_view_fail:
107792 sqlite3SelectDelete(db, pSelect);
107793 if( IN_RENAME_OBJECT ){
107794 sqlite3RenameExprlistUnmap(pParse, pCNames);
107795 }
107796 sqlite3ExprListDelete(db, pCNames);
107797 return;
107798}
107799#endif /* SQLITE_OMIT_VIEW */
107800
107801#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
107802/*
107803** The Table structure pTable is really a VIEW. Fill in the names of
107804** the columns of the view in the pTable structure. Return the number
107805** of errors. If an error is seen leave an error message in pParse->zErrMsg.
107806*/
107807SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
107808 Table *pSelTab; /* A fake table from which we get the result set */
107809 Select *pSel; /* Copy of the SELECT that implements the view */
107810 int nErr = 0; /* Number of errors encountered */
107811 int n; /* Temporarily holds the number of cursors assigned */
107812 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
107813#ifndef SQLITE_OMIT_VIRTUALTABLE
107814 int rc;
107815#endif
107816#ifndef SQLITE_OMIT_AUTHORIZATION
107817 sqlite3_xauth xAuth; /* Saved xAuth pointer */
107818#endif
107819
107820 assert( pTable );
107821
107822#ifndef SQLITE_OMIT_VIRTUALTABLE
107823 db->nSchemaLock++;
107824 rc = sqlite3VtabCallConnect(pParse, pTable);
107825 db->nSchemaLock--;
107826 if( rc ){
107827 return 1;
107828 }
107829 if( IsVirtual(pTable) ) return 0;
107830#endif
107831
107832#ifndef SQLITE_OMIT_VIEW
107833 /* A positive nCol means the columns names for this view are
107834 ** already known.
107835 */
107836 if( pTable->nCol>0 ) return 0;
107837
107838 /* A negative nCol is a special marker meaning that we are currently
107839 ** trying to compute the column names. If we enter this routine with
107840 ** a negative nCol, it means two or more views form a loop, like this:
107841 **
107842 ** CREATE VIEW one AS SELECT * FROM two;
107843 ** CREATE VIEW two AS SELECT * FROM one;
107844 **
107845 ** Actually, the error above is now caught prior to reaching this point.
107846 ** But the following test is still important as it does come up
107847 ** in the following:
107848 **
107849 ** CREATE TABLE main.ex1(a);
107850 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
107851 ** SELECT * FROM temp.ex1;
107852 */
107853 if( pTable->nCol<0 ){
107854 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
107855 return 1;
107856 }
107857 assert( pTable->nCol>=0 );
107858
107859 /* If we get this far, it means we need to compute the table names.
107860 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
107861 ** "*" elements in the results set of the view and will assign cursors
107862 ** to the elements of the FROM clause. But we do not want these changes
107863 ** to be permanent. So the computation is done on a copy of the SELECT
107864 ** statement that defines the view.
107865 */
107866 assert( pTable->pSelect );
107867 pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
107868 if( pSel ){
107869#ifndef SQLITE_OMIT_ALTERTABLE
107870 u8 eParseMode = pParse->eParseMode;
107871 pParse->eParseMode = PARSE_MODE_NORMAL;
107872#endif
107873 n = pParse->nTab;
107874 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
107875 pTable->nCol = -1;
107876 db->lookaside.bDisable++;
107877#ifndef SQLITE_OMIT_AUTHORIZATION
107878 xAuth = db->xAuth;
107879 db->xAuth = 0;
107880 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
107881 db->xAuth = xAuth;
107882#else
107883 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
107884#endif
107885 pParse->nTab = n;
107886 if( pTable->pCheck ){
107887 /* CREATE VIEW name(arglist) AS ...
107888 ** The names of the columns in the table are taken from
107889 ** arglist which is stored in pTable->pCheck. The pCheck field
107890 ** normally holds CHECK constraints on an ordinary table, but for
107891 ** a VIEW it holds the list of column names.
107892 */
107893 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
107894 &pTable->nCol, &pTable->aCol);
107895 if( db->mallocFailed==0
107896 && pParse->nErr==0
107897 && pTable->nCol==pSel->pEList->nExpr
107898 ){
107899 sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel);
107900 }
107901 }else if( pSelTab ){
107902 /* CREATE VIEW name AS... without an argument list. Construct
107903 ** the column names from the SELECT statement that defines the view.
107904 */
107905 assert( pTable->aCol==0 );
107906 pTable->nCol = pSelTab->nCol;
107907 pTable->aCol = pSelTab->aCol;
107908 pSelTab->nCol = 0;
107909 pSelTab->aCol = 0;
107910 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
107911 }else{
107912 pTable->nCol = 0;
107913 nErr++;
107914 }
107915 sqlite3DeleteTable(db, pSelTab);
107916 sqlite3SelectDelete(db, pSel);
107917 db->lookaside.bDisable--;
107918#ifndef SQLITE_OMIT_ALTERTABLE
107919 pParse->eParseMode = eParseMode;
107920#endif
107921 } else {
107922 nErr++;
107923 }
107924 pTable->pSchema->schemaFlags |= DB_UnresetViews;
107925 if( db->mallocFailed ){
107926 sqlite3DeleteColumnNames(db, pTable);
107927 pTable->aCol = 0;
107928 pTable->nCol = 0;
107929 }
107930#endif /* SQLITE_OMIT_VIEW */
107931 return nErr;
107932}
107933#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
107934
107935#ifndef SQLITE_OMIT_VIEW
107936/*
107937** Clear the column names from every VIEW in database idx.
107938*/
107939static void sqliteViewResetAll(sqlite3 *db, int idx){
107940 HashElem *i;
107941 assert( sqlite3SchemaMutexHeld(db, idx, 0) );
107942 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
107943 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
107944 Table *pTab = sqliteHashData(i);
107945 if( pTab->pSelect ){
107946 sqlite3DeleteColumnNames(db, pTab);
107947 pTab->aCol = 0;
107948 pTab->nCol = 0;
107949 }
107950 }
107951 DbClearProperty(db, idx, DB_UnresetViews);
107952}
107953#else
107954# define sqliteViewResetAll(A,B)
107955#endif /* SQLITE_OMIT_VIEW */
107956
107957/*
107958** This function is called by the VDBE to adjust the internal schema
107959** used by SQLite when the btree layer moves a table root page. The
107960** root-page of a table or index in database iDb has changed from iFrom
107961** to iTo.
107962**
107963** Ticket #1728: The symbol table might still contain information
107964** on tables and/or indices that are the process of being deleted.
107965** If you are unlucky, one of those deleted indices or tables might
107966** have the same rootpage number as the real table or index that is
107967** being moved. So we cannot stop searching after the first match
107968** because the first match might be for one of the deleted indices
107969** or tables and not the table/index that is actually being moved.
107970** We must continue looping until all tables and indices with
107971** rootpage==iFrom have been converted to have a rootpage of iTo
107972** in order to be certain that we got the right one.
107973*/
107974#ifndef SQLITE_OMIT_AUTOVACUUM
107975SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
107976 HashElem *pElem;
107977 Hash *pHash;
107978 Db *pDb;
107979
107980 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
107981 pDb = &db->aDb[iDb];
107982 pHash = &pDb->pSchema->tblHash;
107983 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
107984 Table *pTab = sqliteHashData(pElem);
107985 if( pTab->tnum==iFrom ){
107986 pTab->tnum = iTo;
107987 }
107988 }
107989 pHash = &pDb->pSchema->idxHash;
107990 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
107991 Index *pIdx = sqliteHashData(pElem);
107992 if( pIdx->tnum==iFrom ){
107993 pIdx->tnum = iTo;
107994 }
107995 }
107996}
107997#endif
107998
107999/*
108000** Write code to erase the table with root-page iTable from database iDb.
108001** Also write code to modify the sqlite_master table and internal schema
108002** if a root-page of another table is moved by the btree-layer whilst
108003** erasing iTable (this can happen with an auto-vacuum database).
108004*/
108005static void destroyRootPage(Parse *pParse, int iTable, int iDb){
108006 Vdbe *v = sqlite3GetVdbe(pParse);
108007 int r1 = sqlite3GetTempReg(pParse);
108008 assert( iTable>1 );
108009 sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
108010 sqlite3MayAbort(pParse);
108011#ifndef SQLITE_OMIT_AUTOVACUUM
108012 /* OP_Destroy stores an in integer r1. If this integer
108013 ** is non-zero, then it is the root page number of a table moved to
108014 ** location iTable. The following code modifies the sqlite_master table to
108015 ** reflect this.
108016 **
108017 ** The "#NNN" in the SQL is a special constant that means whatever value
108018 ** is in register NNN. See grammar rules associated with the TK_REGISTER
108019 ** token for additional information.
108020 */
108021 sqlite3NestedParse(pParse,
108022 "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
108023 pParse->db->aDb[iDb].zDbSName, MASTER_NAME, iTable, r1, r1);
108024#endif
108025 sqlite3ReleaseTempReg(pParse, r1);
108026}
108027
108028/*
108029** Write VDBE code to erase table pTab and all associated indices on disk.
108030** Code to update the sqlite_master tables and internal schema definitions
108031** in case a root-page belonging to another table is moved by the btree layer
108032** is also added (this can happen with an auto-vacuum database).
108033*/
108034static void destroyTable(Parse *pParse, Table *pTab){
108035 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
108036 ** is not defined), then it is important to call OP_Destroy on the
108037 ** table and index root-pages in order, starting with the numerically
108038 ** largest root-page number. This guarantees that none of the root-pages
108039 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
108040 ** following were coded:
108041 **
108042 ** OP_Destroy 4 0
108043 ** ...
108044 ** OP_Destroy 5 0
108045 **
108046 ** and root page 5 happened to be the largest root-page number in the
108047 ** database, then root page 5 would be moved to page 4 by the
108048 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
108049 ** a free-list page.
108050 */
108051 int iTab = pTab->tnum;
108052 int iDestroyed = 0;
108053
108054 while( 1 ){
108055 Index *pIdx;
108056 int iLargest = 0;
108057
108058 if( iDestroyed==0 || iTab<iDestroyed ){
108059 iLargest = iTab;
108060 }
108061 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108062 int iIdx = pIdx->tnum;
108063 assert( pIdx->pSchema==pTab->pSchema );
108064 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
108065 iLargest = iIdx;
108066 }
108067 }
108068 if( iLargest==0 ){
108069 return;
108070 }else{
108071 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
108072 assert( iDb>=0 && iDb<pParse->db->nDb );
108073 destroyRootPage(pParse, iLargest, iDb);
108074 iDestroyed = iLargest;
108075 }
108076 }
108077}
108078
108079/*
108080** Remove entries from the sqlite_statN tables (for N in (1,2,3))
108081** after a DROP INDEX or DROP TABLE command.
108082*/
108083static void sqlite3ClearStatTables(
108084 Parse *pParse, /* The parsing context */
108085 int iDb, /* The database number */
108086 const char *zType, /* "idx" or "tbl" */
108087 const char *zName /* Name of index or table */
108088){
108089 int i;
108090 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
108091 for(i=1; i<=4; i++){
108092 char zTab[24];
108093 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
108094 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
108095 sqlite3NestedParse(pParse,
108096 "DELETE FROM %Q.%s WHERE %s=%Q",
108097 zDbName, zTab, zType, zName
108098 );
108099 }
108100 }
108101}
108102
108103/*
108104** Generate code to drop a table.
108105*/
108106SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
108107 Vdbe *v;
108108 sqlite3 *db = pParse->db;
108109 Trigger *pTrigger;
108110 Db *pDb = &db->aDb[iDb];
108111
108112 v = sqlite3GetVdbe(pParse);
108113 assert( v!=0 );
108114 sqlite3BeginWriteOperation(pParse, 1, iDb);
108115
108116#ifndef SQLITE_OMIT_VIRTUALTABLE
108117 if( IsVirtual(pTab) ){
108118 sqlite3VdbeAddOp0(v, OP_VBegin);
108119 }
108120#endif
108121
108122 /* Drop all triggers associated with the table being dropped. Code
108123 ** is generated to remove entries from sqlite_master and/or
108124 ** sqlite_temp_master if required.
108125 */
108126 pTrigger = sqlite3TriggerList(pParse, pTab);
108127 while( pTrigger ){
108128 assert( pTrigger->pSchema==pTab->pSchema ||
108129 pTrigger->pSchema==db->aDb[1].pSchema );
108130 sqlite3DropTriggerPtr(pParse, pTrigger);
108131 pTrigger = pTrigger->pNext;
108132 }
108133
108134#ifndef SQLITE_OMIT_AUTOINCREMENT
108135 /* Remove any entries of the sqlite_sequence table associated with
108136 ** the table being dropped. This is done before the table is dropped
108137 ** at the btree level, in case the sqlite_sequence table needs to
108138 ** move as a result of the drop (can happen in auto-vacuum mode).
108139 */
108140 if( pTab->tabFlags & TF_Autoincrement ){
108141 sqlite3NestedParse(pParse,
108142 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
108143 pDb->zDbSName, pTab->zName
108144 );
108145 }
108146#endif
108147
108148 /* Drop all SQLITE_MASTER table and index entries that refer to the
108149 ** table. The program name loops through the master table and deletes
108150 ** every row that refers to a table of the same name as the one being
108151 ** dropped. Triggers are handled separately because a trigger can be
108152 ** created in the temp database that refers to a table in another
108153 ** database.
108154 */
108155 sqlite3NestedParse(pParse,
108156 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
108157 pDb->zDbSName, MASTER_NAME, pTab->zName);
108158 if( !isView && !IsVirtual(pTab) ){
108159 destroyTable(pParse, pTab);
108160 }
108161
108162 /* Remove the table entry from SQLite's internal schema and modify
108163 ** the schema cookie.
108164 */
108165 if( IsVirtual(pTab) ){
108166 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
108167 }
108168 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
108169 sqlite3ChangeCookie(pParse, iDb);
108170 sqliteViewResetAll(db, iDb);
108171}
108172
108173/*
108174** This routine is called to do the work of a DROP TABLE statement.
108175** pName is the name of the table to be dropped.
108176*/
108177SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
108178 Table *pTab;
108179 Vdbe *v;
108180 sqlite3 *db = pParse->db;
108181 int iDb;
108182
108183 if( db->mallocFailed ){
108184 goto exit_drop_table;
108185 }
108186 assert( pParse->nErr==0 );
108187 assert( pName->nSrc==1 );
108188 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
108189 if( noErr ) db->suppressErr++;
108190 assert( isView==0 || isView==LOCATE_VIEW );
108191 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
108192 if( noErr ) db->suppressErr--;
108193
108194 if( pTab==0 ){
108195 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
108196 goto exit_drop_table;
108197 }
108198 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108199 assert( iDb>=0 && iDb<db->nDb );
108200
108201 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
108202 ** it is initialized.
108203 */
108204 if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
108205 goto exit_drop_table;
108206 }
108207#ifndef SQLITE_OMIT_AUTHORIZATION
108208 {
108209 int code;
108210 const char *zTab = SCHEMA_TABLE(iDb);
108211 const char *zDb = db->aDb[iDb].zDbSName;
108212 const char *zArg2 = 0;
108213 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
108214 goto exit_drop_table;
108215 }
108216 if( isView ){
108217 if( !OMIT_TEMPDB && iDb==1 ){
108218 code = SQLITE_DROP_TEMP_VIEW;
108219 }else{
108220 code = SQLITE_DROP_VIEW;
108221 }
108222#ifndef SQLITE_OMIT_VIRTUALTABLE
108223 }else if( IsVirtual(pTab) ){
108224 code = SQLITE_DROP_VTABLE;
108225 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
108226#endif
108227 }else{
108228 if( !OMIT_TEMPDB && iDb==1 ){
108229 code = SQLITE_DROP_TEMP_TABLE;
108230 }else{
108231 code = SQLITE_DROP_TABLE;
108232 }
108233 }
108234 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
108235 goto exit_drop_table;
108236 }
108237 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
108238 goto exit_drop_table;
108239 }
108240 }
108241#endif
108242 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
108243 && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
108244 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
108245 goto exit_drop_table;
108246 }
108247
108248#ifndef SQLITE_OMIT_VIEW
108249 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
108250 ** on a table.
108251 */
108252 if( isView && pTab->pSelect==0 ){
108253 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
108254 goto exit_drop_table;
108255 }
108256 if( !isView && pTab->pSelect ){
108257 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
108258 goto exit_drop_table;
108259 }
108260#endif
108261
108262 /* Generate code to remove the table from the master table
108263 ** on disk.
108264 */
108265 v = sqlite3GetVdbe(pParse);
108266 if( v ){
108267 sqlite3BeginWriteOperation(pParse, 1, iDb);
108268 if( !isView ){
108269 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
108270 sqlite3FkDropTable(pParse, pName, pTab);
108271 }
108272 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
108273 }
108274
108275exit_drop_table:
108276 sqlite3SrcListDelete(db, pName);
108277}
108278
108279/*
108280** This routine is called to create a new foreign key on the table
108281** currently under construction. pFromCol determines which columns
108282** in the current table point to the foreign key. If pFromCol==0 then
108283** connect the key to the last column inserted. pTo is the name of
108284** the table referred to (a.k.a the "parent" table). pToCol is a list
108285** of tables in the parent pTo table. flags contains all
108286** information about the conflict resolution algorithms specified
108287** in the ON DELETE, ON UPDATE and ON INSERT clauses.
108288**
108289** An FKey structure is created and added to the table currently
108290** under construction in the pParse->pNewTable field.
108291**
108292** The foreign key is set for IMMEDIATE processing. A subsequent call
108293** to sqlite3DeferForeignKey() might change this to DEFERRED.
108294*/
108295SQLITE_PRIVATE void sqlite3CreateForeignKey(
108296 Parse *pParse, /* Parsing context */
108297 ExprList *pFromCol, /* Columns in this table that point to other table */
108298 Token *pTo, /* Name of the other table */
108299 ExprList *pToCol, /* Columns in the other table */
108300 int flags /* Conflict resolution algorithms. */
108301){
108302 sqlite3 *db = pParse->db;
108303#ifndef SQLITE_OMIT_FOREIGN_KEY
108304 FKey *pFKey = 0;
108305 FKey *pNextTo;
108306 Table *p = pParse->pNewTable;
108307 int nByte;
108308 int i;
108309 int nCol;
108310 char *z;
108311
108312 assert( pTo!=0 );
108313 if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
108314 if( pFromCol==0 ){
108315 int iCol = p->nCol-1;
108316 if( NEVER(iCol<0) ) goto fk_end;
108317 if( pToCol && pToCol->nExpr!=1 ){
108318 sqlite3ErrorMsg(pParse, "foreign key on %s"
108319 " should reference only one column of table %T",
108320 p->aCol[iCol].zName, pTo);
108321 goto fk_end;
108322 }
108323 nCol = 1;
108324 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
108325 sqlite3ErrorMsg(pParse,
108326 "number of columns in foreign key does not match the number of "
108327 "columns in the referenced table");
108328 goto fk_end;
108329 }else{
108330 nCol = pFromCol->nExpr;
108331 }
108332 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
108333 if( pToCol ){
108334 for(i=0; i<pToCol->nExpr; i++){
108335 nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
108336 }
108337 }
108338 pFKey = sqlite3DbMallocZero(db, nByte );
108339 if( pFKey==0 ){
108340 goto fk_end;
108341 }
108342 pFKey->pFrom = p;
108343 pFKey->pNextFrom = p->pFKey;
108344 z = (char*)&pFKey->aCol[nCol];
108345 pFKey->zTo = z;
108346 if( IN_RENAME_OBJECT ){
108347 sqlite3RenameTokenMap(pParse, (void*)z, pTo);
108348 }
108349 memcpy(z, pTo->z, pTo->n);
108350 z[pTo->n] = 0;
108351 sqlite3Dequote(z);
108352 z += pTo->n+1;
108353 pFKey->nCol = nCol;
108354 if( pFromCol==0 ){
108355 pFKey->aCol[0].iFrom = p->nCol-1;
108356 }else{
108357 for(i=0; i<nCol; i++){
108358 int j;
108359 for(j=0; j<p->nCol; j++){
108360 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
108361 pFKey->aCol[i].iFrom = j;
108362 break;
108363 }
108364 }
108365 if( j>=p->nCol ){
108366 sqlite3ErrorMsg(pParse,
108367 "unknown column \"%s\" in foreign key definition",
108368 pFromCol->a[i].zName);
108369 goto fk_end;
108370 }
108371 if( IN_RENAME_OBJECT ){
108372 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zName);
108373 }
108374 }
108375 }
108376 if( pToCol ){
108377 for(i=0; i<nCol; i++){
108378 int n = sqlite3Strlen30(pToCol->a[i].zName);
108379 pFKey->aCol[i].zCol = z;
108380 if( IN_RENAME_OBJECT ){
108381 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zName);
108382 }
108383 memcpy(z, pToCol->a[i].zName, n);
108384 z[n] = 0;
108385 z += n+1;
108386 }
108387 }
108388 pFKey->isDeferred = 0;
108389 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
108390 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
108391
108392 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
108393 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
108394 pFKey->zTo, (void *)pFKey
108395 );
108396 if( pNextTo==pFKey ){
108397 sqlite3OomFault(db);
108398 goto fk_end;
108399 }
108400 if( pNextTo ){
108401 assert( pNextTo->pPrevTo==0 );
108402 pFKey->pNextTo = pNextTo;
108403 pNextTo->pPrevTo = pFKey;
108404 }
108405
108406 /* Link the foreign key to the table as the last step.
108407 */
108408 p->pFKey = pFKey;
108409 pFKey = 0;
108410
108411fk_end:
108412 sqlite3DbFree(db, pFKey);
108413#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
108414 sqlite3ExprListDelete(db, pFromCol);
108415 sqlite3ExprListDelete(db, pToCol);
108416}
108417
108418/*
108419** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
108420** clause is seen as part of a foreign key definition. The isDeferred
108421** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
108422** The behavior of the most recently created foreign key is adjusted
108423** accordingly.
108424*/
108425SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
108426#ifndef SQLITE_OMIT_FOREIGN_KEY
108427 Table *pTab;
108428 FKey *pFKey;
108429 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
108430 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
108431 pFKey->isDeferred = (u8)isDeferred;
108432#endif
108433}
108434
108435/*
108436** Generate code that will erase and refill index *pIdx. This is
108437** used to initialize a newly created index or to recompute the
108438** content of an index in response to a REINDEX command.
108439**
108440** if memRootPage is not negative, it means that the index is newly
108441** created. The register specified by memRootPage contains the
108442** root page number of the index. If memRootPage is negative, then
108443** the index already exists and must be cleared before being refilled and
108444** the root page number of the index is taken from pIndex->tnum.
108445*/
108446static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
108447 Table *pTab = pIndex->pTable; /* The table that is indexed */
108448 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
108449 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
108450 int iSorter; /* Cursor opened by OpenSorter (if in use) */
108451 int addr1; /* Address of top of loop */
108452 int addr2; /* Address to jump to for next iteration */
108453 int tnum; /* Root page of index */
108454 int iPartIdxLabel; /* Jump to this label to skip a row */
108455 Vdbe *v; /* Generate code into this virtual machine */
108456 KeyInfo *pKey; /* KeyInfo for index */
108457 int regRecord; /* Register holding assembled index record */
108458 sqlite3 *db = pParse->db; /* The database connection */
108459 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
108460
108461#ifndef SQLITE_OMIT_AUTHORIZATION
108462 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
108463 db->aDb[iDb].zDbSName ) ){
108464 return;
108465 }
108466#endif
108467
108468 /* Require a write-lock on the table to perform this operation */
108469 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
108470
108471 v = sqlite3GetVdbe(pParse);
108472 if( v==0 ) return;
108473 if( memRootPage>=0 ){
108474 tnum = memRootPage;
108475 }else{
108476 tnum = pIndex->tnum;
108477 }
108478 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
108479 assert( pKey!=0 || db->mallocFailed || pParse->nErr );
108480
108481 /* Open the sorter cursor if we are to use one. */
108482 iSorter = pParse->nTab++;
108483 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
108484 sqlite3KeyInfoRef(pKey), P4_KEYINFO);
108485
108486 /* Open the table. Loop through all rows of the table, inserting index
108487 ** records into the sorter. */
108488 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
108489 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
108490 regRecord = sqlite3GetTempReg(pParse);
108491 sqlite3MultiWrite(pParse);
108492
108493 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
108494 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
108495 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
108496 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
108497 sqlite3VdbeJumpHere(v, addr1);
108498 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
108499 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
108500 (char *)pKey, P4_KEYINFO);
108501 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
108502
108503 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
108504 if( IsUniqueIndex(pIndex) ){
108505 int j2 = sqlite3VdbeGoto(v, 1);
108506 addr2 = sqlite3VdbeCurrentAddr(v);
108507 sqlite3VdbeVerifyAbortable(v, OE_Abort);
108508 sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
108509 pIndex->nKeyCol); VdbeCoverage(v);
108510 sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
108511 sqlite3VdbeJumpHere(v, j2);
108512 }else{
108513 addr2 = sqlite3VdbeCurrentAddr(v);
108514 }
108515 sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
108516 sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
108517 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
108518 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
108519 sqlite3ReleaseTempReg(pParse, regRecord);
108520 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
108521 sqlite3VdbeJumpHere(v, addr1);
108522
108523 sqlite3VdbeAddOp1(v, OP_Close, iTab);
108524 sqlite3VdbeAddOp1(v, OP_Close, iIdx);
108525 sqlite3VdbeAddOp1(v, OP_Close, iSorter);
108526}
108527
108528/*
108529** Allocate heap space to hold an Index object with nCol columns.
108530**
108531** Increase the allocation size to provide an extra nExtra bytes
108532** of 8-byte aligned space after the Index object and return a
108533** pointer to this extra space in *ppExtra.
108534*/
108535SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
108536 sqlite3 *db, /* Database connection */
108537 i16 nCol, /* Total number of columns in the index */
108538 int nExtra, /* Number of bytes of extra space to alloc */
108539 char **ppExtra /* Pointer to the "extra" space */
108540){
108541 Index *p; /* Allocated index object */
108542 int nByte; /* Bytes of space for Index object + arrays */
108543
108544 nByte = ROUND8(sizeof(Index)) + /* Index structure */
108545 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
108546 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */
108547 sizeof(i16)*nCol + /* Index.aiColumn */
108548 sizeof(u8)*nCol); /* Index.aSortOrder */
108549 p = sqlite3DbMallocZero(db, nByte + nExtra);
108550 if( p ){
108551 char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
108552 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
108553 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
108554 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
108555 p->aSortOrder = (u8*)pExtra;
108556 p->nColumn = nCol;
108557 p->nKeyCol = nCol - 1;
108558 *ppExtra = ((char*)p) + nByte;
108559 }
108560 return p;
108561}
108562
108563/*
108564** Create a new index for an SQL table. pName1.pName2 is the name of the index
108565** and pTblList is the name of the table that is to be indexed. Both will
108566** be NULL for a primary key or an index that is created to satisfy a
108567** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
108568** as the table to be indexed. pParse->pNewTable is a table that is
108569** currently being constructed by a CREATE TABLE statement.
108570**
108571** pList is a list of columns to be indexed. pList will be NULL if this
108572** is a primary key or unique-constraint on the most recent column added
108573** to the table currently under construction.
108574*/
108575SQLITE_PRIVATE void sqlite3CreateIndex(
108576 Parse *pParse, /* All information about this parse */
108577 Token *pName1, /* First part of index name. May be NULL */
108578 Token *pName2, /* Second part of index name. May be NULL */
108579 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
108580 ExprList *pList, /* A list of columns to be indexed */
108581 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
108582 Token *pStart, /* The CREATE token that begins this statement */
108583 Expr *pPIWhere, /* WHERE clause for partial indices */
108584 int sortOrder, /* Sort order of primary key when pList==NULL */
108585 int ifNotExist, /* Omit error if index already exists */
108586 u8 idxType /* The index type */
108587){
108588 Table *pTab = 0; /* Table to be indexed */
108589 Index *pIndex = 0; /* The index to be created */
108590 char *zName = 0; /* Name of the index */
108591 int nName; /* Number of characters in zName */
108592 int i, j;
108593 DbFixer sFix; /* For assigning database names to pTable */
108594 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
108595 sqlite3 *db = pParse->db;
108596 Db *pDb; /* The specific table containing the indexed database */
108597 int iDb; /* Index of the database that is being written */
108598 Token *pName = 0; /* Unqualified name of the index to create */
108599 struct ExprList_item *pListItem; /* For looping over pList */
108600 int nExtra = 0; /* Space allocated for zExtra[] */
108601 int nExtraCol; /* Number of extra columns needed */
108602 char *zExtra = 0; /* Extra space after the Index object */
108603 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */
108604
108605 if( db->mallocFailed || pParse->nErr>0 ){
108606 goto exit_create_index;
108607 }
108608 if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
108609 goto exit_create_index;
108610 }
108611 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
108612 goto exit_create_index;
108613 }
108614
108615 /*
108616 ** Find the table that is to be indexed. Return early if not found.
108617 */
108618 if( pTblName!=0 ){
108619
108620 /* Use the two-part index name to determine the database
108621 ** to search for the table. 'Fix' the table name to this db
108622 ** before looking up the table.
108623 */
108624 assert( pName1 && pName2 );
108625 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
108626 if( iDb<0 ) goto exit_create_index;
108627 assert( pName && pName->z );
108628
108629#ifndef SQLITE_OMIT_TEMPDB
108630 /* If the index name was unqualified, check if the table
108631 ** is a temp table. If so, set the database to 1. Do not do this
108632 ** if initialising a database schema.
108633 */
108634 if( !db->init.busy ){
108635 pTab = sqlite3SrcListLookup(pParse, pTblName);
108636 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
108637 iDb = 1;
108638 }
108639 }
108640#endif
108641
108642 sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
108643 if( sqlite3FixSrcList(&sFix, pTblName) ){
108644 /* Because the parser constructs pTblName from a single identifier,
108645 ** sqlite3FixSrcList can never fail. */
108646 assert(0);
108647 }
108648 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
108649 assert( db->mallocFailed==0 || pTab==0 );
108650 if( pTab==0 ) goto exit_create_index;
108651 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
108652 sqlite3ErrorMsg(pParse,
108653 "cannot create a TEMP index on non-TEMP table \"%s\"",
108654 pTab->zName);
108655 goto exit_create_index;
108656 }
108657 if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
108658 }else{
108659 assert( pName==0 );
108660 assert( pStart==0 );
108661 pTab = pParse->pNewTable;
108662 if( !pTab ) goto exit_create_index;
108663 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108664 }
108665 pDb = &db->aDb[iDb];
108666
108667 assert( pTab!=0 );
108668 assert( pParse->nErr==0 );
108669 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
108670 && db->init.busy==0
108671#if SQLITE_USER_AUTHENTICATION
108672 && sqlite3UserAuthTable(pTab->zName)==0
108673#endif
108674#ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
108675 && sqlite3StrICmp(&pTab->zName[7],"master")!=0
108676#endif
108677 && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0
108678 ){
108679 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
108680 goto exit_create_index;
108681 }
108682#ifndef SQLITE_OMIT_VIEW
108683 if( pTab->pSelect ){
108684 sqlite3ErrorMsg(pParse, "views may not be indexed");
108685 goto exit_create_index;
108686 }
108687#endif
108688#ifndef SQLITE_OMIT_VIRTUALTABLE
108689 if( IsVirtual(pTab) ){
108690 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
108691 goto exit_create_index;
108692 }
108693#endif
108694
108695 /*
108696 ** Find the name of the index. Make sure there is not already another
108697 ** index or table with the same name.
108698 **
108699 ** Exception: If we are reading the names of permanent indices from the
108700 ** sqlite_master table (because some other process changed the schema) and
108701 ** one of the index names collides with the name of a temporary table or
108702 ** index, then we will continue to process this index.
108703 **
108704 ** If pName==0 it means that we are
108705 ** dealing with a primary key or UNIQUE constraint. We have to invent our
108706 ** own name.
108707 */
108708 if( pName ){
108709 zName = sqlite3NameFromToken(db, pName);
108710 if( zName==0 ) goto exit_create_index;
108711 assert( pName->z!=0 );
108712 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
108713 goto exit_create_index;
108714 }
108715 if( !IN_RENAME_OBJECT ){
108716 if( !db->init.busy ){
108717 if( sqlite3FindTable(db, zName, 0)!=0 ){
108718 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
108719 goto exit_create_index;
108720 }
108721 }
108722 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
108723 if( !ifNotExist ){
108724 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
108725 }else{
108726 assert( !db->init.busy );
108727 sqlite3CodeVerifySchema(pParse, iDb);
108728 }
108729 goto exit_create_index;
108730 }
108731 }
108732 }else{
108733 int n;
108734 Index *pLoop;
108735 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
108736 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
108737 if( zName==0 ){
108738 goto exit_create_index;
108739 }
108740
108741 /* Automatic index names generated from within sqlite3_declare_vtab()
108742 ** must have names that are distinct from normal automatic index names.
108743 ** The following statement converts "sqlite3_autoindex..." into
108744 ** "sqlite3_butoindex..." in order to make the names distinct.
108745 ** The "vtab_err.test" test demonstrates the need of this statement. */
108746 if( IN_SPECIAL_PARSE ) zName[7]++;
108747 }
108748
108749 /* Check for authorization to create an index.
108750 */
108751#ifndef SQLITE_OMIT_AUTHORIZATION
108752 if( !IN_RENAME_OBJECT ){
108753 const char *zDb = pDb->zDbSName;
108754 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
108755 goto exit_create_index;
108756 }
108757 i = SQLITE_CREATE_INDEX;
108758 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
108759 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
108760 goto exit_create_index;
108761 }
108762 }
108763#endif
108764
108765 /* If pList==0, it means this routine was called to make a primary
108766 ** key out of the last column added to the table under construction.
108767 ** So create a fake list to simulate this.
108768 */
108769 if( pList==0 ){
108770 Token prevCol;
108771 Column *pCol = &pTab->aCol[pTab->nCol-1];
108772 pCol->colFlags |= COLFLAG_UNIQUE;
108773 sqlite3TokenInit(&prevCol, pCol->zName);
108774 pList = sqlite3ExprListAppend(pParse, 0,
108775 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
108776 if( pList==0 ) goto exit_create_index;
108777 assert( pList->nExpr==1 );
108778 sqlite3ExprListSetSortOrder(pList, sortOrder);
108779 }else{
108780 sqlite3ExprListCheckLength(pParse, pList, "index");
108781 }
108782
108783 /* Figure out how many bytes of space are required to store explicitly
108784 ** specified collation sequence names.
108785 */
108786 for(i=0; i<pList->nExpr; i++){
108787 Expr *pExpr = pList->a[i].pExpr;
108788 assert( pExpr!=0 );
108789 if( pExpr->op==TK_COLLATE ){
108790 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
108791 }
108792 }
108793
108794 /*
108795 ** Allocate the index structure.
108796 */
108797 nName = sqlite3Strlen30(zName);
108798 nExtraCol = pPk ? pPk->nKeyCol : 1;
108799 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
108800 nName + nExtra + 1, &zExtra);
108801 if( db->mallocFailed ){
108802 goto exit_create_index;
108803 }
108804 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
108805 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
108806 pIndex->zName = zExtra;
108807 zExtra += nName + 1;
108808 memcpy(pIndex->zName, zName, nName+1);
108809 pIndex->pTable = pTab;
108810 pIndex->onError = (u8)onError;
108811 pIndex->uniqNotNull = onError!=OE_None;
108812 pIndex->idxType = idxType;
108813 pIndex->pSchema = db->aDb[iDb].pSchema;
108814 pIndex->nKeyCol = pList->nExpr;
108815 if( pPIWhere ){
108816 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
108817 pIndex->pPartIdxWhere = pPIWhere;
108818 pPIWhere = 0;
108819 }
108820 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108821
108822 /* Check to see if we should honor DESC requests on index columns
108823 */
108824 if( pDb->pSchema->file_format>=4 ){
108825 sortOrderMask = -1; /* Honor DESC */
108826 }else{
108827 sortOrderMask = 0; /* Ignore DESC */
108828 }
108829
108830 /* Analyze the list of expressions that form the terms of the index and
108831 ** report any errors. In the common case where the expression is exactly
108832 ** a table column, store that column in aiColumn[]. For general expressions,
108833 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
108834 **
108835 ** TODO: Issue a warning if two or more columns of the index are identical.
108836 ** TODO: Issue a warning if the table primary key is used as part of the
108837 ** index key.
108838 */
108839 pListItem = pList->a;
108840 if( IN_RENAME_OBJECT ){
108841 pIndex->aColExpr = pList;
108842 pList = 0;
108843 }
108844 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
108845 Expr *pCExpr; /* The i-th index expression */
108846 int requestedSortOrder; /* ASC or DESC on the i-th expression */
108847 const char *zColl; /* Collation sequence name */
108848
108849 sqlite3StringToId(pListItem->pExpr);
108850 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
108851 if( pParse->nErr ) goto exit_create_index;
108852 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
108853 if( pCExpr->op!=TK_COLUMN ){
108854 if( pTab==pParse->pNewTable ){
108855 sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
108856 "UNIQUE constraints");
108857 goto exit_create_index;
108858 }
108859 if( pIndex->aColExpr==0 ){
108860 pIndex->aColExpr = pList;
108861 pList = 0;
108862 }
108863 j = XN_EXPR;
108864 pIndex->aiColumn[i] = XN_EXPR;
108865 pIndex->uniqNotNull = 0;
108866 }else{
108867 j = pCExpr->iColumn;
108868 assert( j<=0x7fff );
108869 if( j<0 ){
108870 j = pTab->iPKey;
108871 }else if( pTab->aCol[j].notNull==0 ){
108872 pIndex->uniqNotNull = 0;
108873 }
108874 pIndex->aiColumn[i] = (i16)j;
108875 }
108876 zColl = 0;
108877 if( pListItem->pExpr->op==TK_COLLATE ){
108878 int nColl;
108879 zColl = pListItem->pExpr->u.zToken;
108880 nColl = sqlite3Strlen30(zColl) + 1;
108881 assert( nExtra>=nColl );
108882 memcpy(zExtra, zColl, nColl);
108883 zColl = zExtra;
108884 zExtra += nColl;
108885 nExtra -= nColl;
108886 }else if( j>=0 ){
108887 zColl = pTab->aCol[j].zColl;
108888 }
108889 if( !zColl ) zColl = sqlite3StrBINARY;
108890 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
108891 goto exit_create_index;
108892 }
108893 pIndex->azColl[i] = zColl;
108894 requestedSortOrder = pListItem->sortOrder & sortOrderMask;
108895 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
108896 }
108897
108898 /* Append the table key to the end of the index. For WITHOUT ROWID
108899 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For
108900 ** normal tables (when pPk==0) this will be the rowid.
108901 */
108902 if( pPk ){
108903 for(j=0; j<pPk->nKeyCol; j++){
108904 int x = pPk->aiColumn[j];
108905 assert( x>=0 );
108906 if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
108907 pIndex->nColumn--;
108908 }else{
108909 pIndex->aiColumn[i] = x;
108910 pIndex->azColl[i] = pPk->azColl[j];
108911 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
108912 i++;
108913 }
108914 }
108915 assert( i==pIndex->nColumn );
108916 }else{
108917 pIndex->aiColumn[i] = XN_ROWID;
108918 pIndex->azColl[i] = sqlite3StrBINARY;
108919 }
108920 sqlite3DefaultRowEst(pIndex);
108921 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
108922
108923 /* If this index contains every column of its table, then mark
108924 ** it as a covering index */
108925 assert( HasRowid(pTab)
108926 || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 );
108927 recomputeColumnsNotIndexed(pIndex);
108928 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
108929 pIndex->isCovering = 1;
108930 for(j=0; j<pTab->nCol; j++){
108931 if( j==pTab->iPKey ) continue;
108932 if( sqlite3ColumnOfIndex(pIndex,j)>=0 ) continue;
108933 pIndex->isCovering = 0;
108934 break;
108935 }
108936 }
108937
108938 if( pTab==pParse->pNewTable ){
108939 /* This routine has been called to create an automatic index as a
108940 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
108941 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
108942 ** i.e. one of:
108943 **
108944 ** CREATE TABLE t(x PRIMARY KEY, y);
108945 ** CREATE TABLE t(x, y, UNIQUE(x, y));
108946 **
108947 ** Either way, check to see if the table already has such an index. If
108948 ** so, don't bother creating this one. This only applies to
108949 ** automatically created indices. Users can do as they wish with
108950 ** explicit indices.
108951 **
108952 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
108953 ** (and thus suppressing the second one) even if they have different
108954 ** sort orders.
108955 **
108956 ** If there are different collating sequences or if the columns of
108957 ** the constraint occur in different orders, then the constraints are
108958 ** considered distinct and both result in separate indices.
108959 */
108960 Index *pIdx;
108961 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108962 int k;
108963 assert( IsUniqueIndex(pIdx) );
108964 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
108965 assert( IsUniqueIndex(pIndex) );
108966
108967 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
108968 for(k=0; k<pIdx->nKeyCol; k++){
108969 const char *z1;
108970 const char *z2;
108971 assert( pIdx->aiColumn[k]>=0 );
108972 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
108973 z1 = pIdx->azColl[k];
108974 z2 = pIndex->azColl[k];
108975 if( sqlite3StrICmp(z1, z2) ) break;
108976 }
108977 if( k==pIdx->nKeyCol ){
108978 if( pIdx->onError!=pIndex->onError ){
108979 /* This constraint creates the same index as a previous
108980 ** constraint specified somewhere in the CREATE TABLE statement.
108981 ** However the ON CONFLICT clauses are different. If both this
108982 ** constraint and the previous equivalent constraint have explicit
108983 ** ON CONFLICT clauses this is an error. Otherwise, use the
108984 ** explicitly specified behavior for the index.
108985 */
108986 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
108987 sqlite3ErrorMsg(pParse,
108988 "conflicting ON CONFLICT clauses specified", 0);
108989 }
108990 if( pIdx->onError==OE_Default ){
108991 pIdx->onError = pIndex->onError;
108992 }
108993 }
108994 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
108995 goto exit_create_index;
108996 }
108997 }
108998 }
108999
109000 if( !IN_RENAME_OBJECT ){
109001
109002 /* Link the new Index structure to its table and to the other
109003 ** in-memory database structures.
109004 */
109005 assert( pParse->nErr==0 );
109006 if( db->init.busy ){
109007 Index *p;
109008 assert( !IN_SPECIAL_PARSE );
109009 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
109010 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
109011 pIndex->zName, pIndex);
109012 if( p ){
109013 assert( p==pIndex ); /* Malloc must have failed */
109014 sqlite3OomFault(db);
109015 goto exit_create_index;
109016 }
109017 db->mDbFlags |= DBFLAG_SchemaChange;
109018 if( pTblName!=0 ){
109019 pIndex->tnum = db->init.newTnum;
109020 }
109021 }
109022
109023 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
109024 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
109025 ** emit code to allocate the index rootpage on disk and make an entry for
109026 ** the index in the sqlite_master table and populate the index with
109027 ** content. But, do not do this if we are simply reading the sqlite_master
109028 ** table to parse the schema, or if this index is the PRIMARY KEY index
109029 ** of a WITHOUT ROWID table.
109030 **
109031 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
109032 ** or UNIQUE index in a CREATE TABLE statement. Since the table
109033 ** has just been created, it contains no data and the index initialization
109034 ** step can be skipped.
109035 */
109036 else if( HasRowid(pTab) || pTblName!=0 ){
109037 Vdbe *v;
109038 char *zStmt;
109039 int iMem = ++pParse->nMem;
109040
109041 v = sqlite3GetVdbe(pParse);
109042 if( v==0 ) goto exit_create_index;
109043
109044 sqlite3BeginWriteOperation(pParse, 1, iDb);
109045
109046 /* Create the rootpage for the index using CreateIndex. But before
109047 ** doing so, code a Noop instruction and store its address in
109048 ** Index.tnum. This is required in case this index is actually a
109049 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
109050 ** that case the convertToWithoutRowidTable() routine will replace
109051 ** the Noop with a Goto to jump over the VDBE code generated below. */
109052 pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
109053 sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
109054
109055 /* Gather the complete text of the CREATE INDEX statement into
109056 ** the zStmt variable
109057 */
109058 if( pStart ){
109059 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
109060 if( pName->z[n-1]==';' ) n--;
109061 /* A named index with an explicit CREATE INDEX statement */
109062 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
109063 onError==OE_None ? "" : " UNIQUE", n, pName->z);
109064 }else{
109065 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
109066 /* zStmt = sqlite3MPrintf(""); */
109067 zStmt = 0;
109068 }
109069
109070 /* Add an entry in sqlite_master for this index
109071 */
109072 sqlite3NestedParse(pParse,
109073 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
109074 db->aDb[iDb].zDbSName, MASTER_NAME,
109075 pIndex->zName,
109076 pTab->zName,
109077 iMem,
109078 zStmt
109079 );
109080 sqlite3DbFree(db, zStmt);
109081
109082 /* Fill the index with data and reparse the schema. Code an OP_Expire
109083 ** to invalidate all pre-compiled statements.
109084 */
109085 if( pTblName ){
109086 sqlite3RefillIndex(pParse, pIndex, iMem);
109087 sqlite3ChangeCookie(pParse, iDb);
109088 sqlite3VdbeAddParseSchemaOp(v, iDb,
109089 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
109090 sqlite3VdbeAddOp2(v, OP_Expire, 0, 1);
109091 }
109092
109093 sqlite3VdbeJumpHere(v, pIndex->tnum);
109094 }
109095 }
109096
109097 /* When adding an index to the list of indices for a table, make
109098 ** sure all indices labeled OE_Replace come after all those labeled
109099 ** OE_Ignore. This is necessary for the correct constraint check
109100 ** processing (in sqlite3GenerateConstraintChecks()) as part of
109101 ** UPDATE and INSERT statements.
109102 */
109103 if( db->init.busy || pTblName==0 ){
109104 if( onError!=OE_Replace || pTab->pIndex==0
109105 || pTab->pIndex->onError==OE_Replace){
109106 pIndex->pNext = pTab->pIndex;
109107 pTab->pIndex = pIndex;
109108 }else{
109109 Index *pOther = pTab->pIndex;
109110 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
109111 pOther = pOther->pNext;
109112 }
109113 pIndex->pNext = pOther->pNext;
109114 pOther->pNext = pIndex;
109115 }
109116 pIndex = 0;
109117 }
109118 else if( IN_RENAME_OBJECT ){
109119 assert( pParse->pNewIndex==0 );
109120 pParse->pNewIndex = pIndex;
109121 pIndex = 0;
109122 }
109123
109124 /* Clean up before exiting */
109125exit_create_index:
109126 if( pIndex ) sqlite3FreeIndex(db, pIndex);
109127 sqlite3ExprDelete(db, pPIWhere);
109128 sqlite3ExprListDelete(db, pList);
109129 sqlite3SrcListDelete(db, pTblName);
109130 sqlite3DbFree(db, zName);
109131}
109132
109133/*
109134** Fill the Index.aiRowEst[] array with default information - information
109135** to be used when we have not run the ANALYZE command.
109136**
109137** aiRowEst[0] is supposed to contain the number of elements in the index.
109138** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
109139** number of rows in the table that match any particular value of the
109140** first column of the index. aiRowEst[2] is an estimate of the number
109141** of rows that match any particular combination of the first 2 columns
109142** of the index. And so forth. It must always be the case that
109143*
109144** aiRowEst[N]<=aiRowEst[N-1]
109145** aiRowEst[N]>=1
109146**
109147** Apart from that, we have little to go on besides intuition as to
109148** how aiRowEst[] should be initialized. The numbers generated here
109149** are based on typical values found in actual indices.
109150*/
109151SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
109152 /* 10, 9, 8, 7, 6 */
109153 LogEst aVal[] = { 33, 32, 30, 28, 26 };
109154 LogEst *a = pIdx->aiRowLogEst;
109155 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
109156 int i;
109157
109158 /* Indexes with default row estimates should not have stat1 data */
109159 assert( !pIdx->hasStat1 );
109160
109161 /* Set the first entry (number of rows in the index) to the estimated
109162 ** number of rows in the table, or half the number of rows in the table
109163 ** for a partial index. But do not let the estimate drop below 10. */
109164 a[0] = pIdx->pTable->nRowLogEst;
109165 if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10; assert( 10==sqlite3LogEst(2) );
109166 if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) );
109167
109168 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
109169 ** 6 and each subsequent value (if any) is 5. */
109170 memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
109171 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
109172 a[i] = 23; assert( 23==sqlite3LogEst(5) );
109173 }
109174
109175 assert( 0==sqlite3LogEst(1) );
109176 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
109177}
109178
109179/*
109180** This routine will drop an existing named index. This routine
109181** implements the DROP INDEX statement.
109182*/
109183SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
109184 Index *pIndex;
109185 Vdbe *v;
109186 sqlite3 *db = pParse->db;
109187 int iDb;
109188
109189 assert( pParse->nErr==0 ); /* Never called with prior errors */
109190 if( db->mallocFailed ){
109191 goto exit_drop_index;
109192 }
109193 assert( pName->nSrc==1 );
109194 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
109195 goto exit_drop_index;
109196 }
109197 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
109198 if( pIndex==0 ){
109199 if( !ifExists ){
109200 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
109201 }else{
109202 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
109203 }
109204 pParse->checkSchema = 1;
109205 goto exit_drop_index;
109206 }
109207 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
109208 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
109209 "or PRIMARY KEY constraint cannot be dropped", 0);
109210 goto exit_drop_index;
109211 }
109212 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
109213#ifndef SQLITE_OMIT_AUTHORIZATION
109214 {
109215 int code = SQLITE_DROP_INDEX;
109216 Table *pTab = pIndex->pTable;
109217 const char *zDb = db->aDb[iDb].zDbSName;
109218 const char *zTab = SCHEMA_TABLE(iDb);
109219 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
109220 goto exit_drop_index;
109221 }
109222 if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
109223 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
109224 goto exit_drop_index;
109225 }
109226 }
109227#endif
109228
109229 /* Generate code to remove the index and from the master table */
109230 v = sqlite3GetVdbe(pParse);
109231 if( v ){
109232 sqlite3BeginWriteOperation(pParse, 1, iDb);
109233 sqlite3NestedParse(pParse,
109234 "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
109235 db->aDb[iDb].zDbSName, MASTER_NAME, pIndex->zName
109236 );
109237 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
109238 sqlite3ChangeCookie(pParse, iDb);
109239 destroyRootPage(pParse, pIndex->tnum, iDb);
109240 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
109241 }
109242
109243exit_drop_index:
109244 sqlite3SrcListDelete(db, pName);
109245}
109246
109247/*
109248** pArray is a pointer to an array of objects. Each object in the
109249** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
109250** to extend the array so that there is space for a new object at the end.
109251**
109252** When this function is called, *pnEntry contains the current size of
109253** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
109254** in total).
109255**
109256** If the realloc() is successful (i.e. if no OOM condition occurs), the
109257** space allocated for the new object is zeroed, *pnEntry updated to
109258** reflect the new size of the array and a pointer to the new allocation
109259** returned. *pIdx is set to the index of the new array entry in this case.
109260**
109261** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
109262** unchanged and a copy of pArray returned.
109263*/
109264SQLITE_PRIVATE void *sqlite3ArrayAllocate(
109265 sqlite3 *db, /* Connection to notify of malloc failures */
109266 void *pArray, /* Array of objects. Might be reallocated */
109267 int szEntry, /* Size of each object in the array */
109268 int *pnEntry, /* Number of objects currently in use */
109269 int *pIdx /* Write the index of a new slot here */
109270){
109271 char *z;
109272 int n = *pnEntry;
109273 if( (n & (n-1))==0 ){
109274 int sz = (n==0) ? 1 : 2*n;
109275 void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
109276 if( pNew==0 ){
109277 *pIdx = -1;
109278 return pArray;
109279 }
109280 pArray = pNew;
109281 }
109282 z = (char*)pArray;
109283 memset(&z[n * szEntry], 0, szEntry);
109284 *pIdx = n;
109285 ++*pnEntry;
109286 return pArray;
109287}
109288
109289/*
109290** Append a new element to the given IdList. Create a new IdList if
109291** need be.
109292**
109293** A new IdList is returned, or NULL if malloc() fails.
109294*/
109295SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
109296 sqlite3 *db = pParse->db;
109297 int i;
109298 if( pList==0 ){
109299 pList = sqlite3DbMallocZero(db, sizeof(IdList) );
109300 if( pList==0 ) return 0;
109301 }
109302 pList->a = sqlite3ArrayAllocate(
109303 db,
109304 pList->a,
109305 sizeof(pList->a[0]),
109306 &pList->nId,
109307 &i
109308 );
109309 if( i<0 ){
109310 sqlite3IdListDelete(db, pList);
109311 return 0;
109312 }
109313 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
109314 if( IN_RENAME_OBJECT && pList->a[i].zName ){
109315 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
109316 }
109317 return pList;
109318}
109319
109320/*
109321** Delete an IdList.
109322*/
109323SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
109324 int i;
109325 if( pList==0 ) return;
109326 for(i=0; i<pList->nId; i++){
109327 sqlite3DbFree(db, pList->a[i].zName);
109328 }
109329 sqlite3DbFree(db, pList->a);
109330 sqlite3DbFreeNN(db, pList);
109331}
109332
109333/*
109334** Return the index in pList of the identifier named zId. Return -1
109335** if not found.
109336*/
109337SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
109338 int i;
109339 if( pList==0 ) return -1;
109340 for(i=0; i<pList->nId; i++){
109341 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
109342 }
109343 return -1;
109344}
109345
109346/*
109347** Expand the space allocated for the given SrcList object by
109348** creating nExtra new slots beginning at iStart. iStart is zero based.
109349** New slots are zeroed.
109350**
109351** For example, suppose a SrcList initially contains two entries: A,B.
109352** To append 3 new entries onto the end, do this:
109353**
109354** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
109355**
109356** After the call above it would contain: A, B, nil, nil, nil.
109357** If the iStart argument had been 1 instead of 2, then the result
109358** would have been: A, nil, nil, nil, B. To prepend the new slots,
109359** the iStart value would be 0. The result then would
109360** be: nil, nil, nil, A, B.
109361**
109362** If a memory allocation fails the SrcList is unchanged. The
109363** db->mallocFailed flag will be set to true.
109364*/
109365SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
109366 sqlite3 *db, /* Database connection to notify of OOM errors */
109367 SrcList *pSrc, /* The SrcList to be enlarged */
109368 int nExtra, /* Number of new slots to add to pSrc->a[] */
109369 int iStart /* Index in pSrc->a[] of first new slot */
109370){
109371 int i;
109372
109373 /* Sanity checking on calling parameters */
109374 assert( iStart>=0 );
109375 assert( nExtra>=1 );
109376 assert( pSrc!=0 );
109377 assert( iStart<=pSrc->nSrc );
109378
109379 /* Allocate additional space if needed */
109380 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
109381 SrcList *pNew;
109382 int nAlloc = pSrc->nSrc*2+nExtra;
109383 int nGot;
109384 pNew = sqlite3DbRealloc(db, pSrc,
109385 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
109386 if( pNew==0 ){
109387 assert( db->mallocFailed );
109388 return pSrc;
109389 }
109390 pSrc = pNew;
109391 nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
109392 pSrc->nAlloc = nGot;
109393 }
109394
109395 /* Move existing slots that come after the newly inserted slots
109396 ** out of the way */
109397 for(i=pSrc->nSrc-1; i>=iStart; i--){
109398 pSrc->a[i+nExtra] = pSrc->a[i];
109399 }
109400 pSrc->nSrc += nExtra;
109401
109402 /* Zero the newly allocated slots */
109403 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
109404 for(i=iStart; i<iStart+nExtra; i++){
109405 pSrc->a[i].iCursor = -1;
109406 }
109407
109408 /* Return a pointer to the enlarged SrcList */
109409 return pSrc;
109410}
109411
109412
109413/*
109414** Append a new table name to the given SrcList. Create a new SrcList if
109415** need be. A new entry is created in the SrcList even if pTable is NULL.
109416**
109417** A SrcList is returned, or NULL if there is an OOM error. The returned
109418** SrcList might be the same as the SrcList that was input or it might be
109419** a new one. If an OOM error does occurs, then the prior value of pList
109420** that is input to this routine is automatically freed.
109421**
109422** If pDatabase is not null, it means that the table has an optional
109423** database name prefix. Like this: "database.table". The pDatabase
109424** points to the table name and the pTable points to the database name.
109425** The SrcList.a[].zName field is filled with the table name which might
109426** come from pTable (if pDatabase is NULL) or from pDatabase.
109427** SrcList.a[].zDatabase is filled with the database name from pTable,
109428** or with NULL if no database is specified.
109429**
109430** In other words, if call like this:
109431**
109432** sqlite3SrcListAppend(D,A,B,0);
109433**
109434** Then B is a table name and the database name is unspecified. If called
109435** like this:
109436**
109437** sqlite3SrcListAppend(D,A,B,C);
109438**
109439** Then C is the table name and B is the database name. If C is defined
109440** then so is B. In other words, we never have a case where:
109441**
109442** sqlite3SrcListAppend(D,A,0,C);
109443**
109444** Both pTable and pDatabase are assumed to be quoted. They are dequoted
109445** before being added to the SrcList.
109446*/
109447SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
109448 sqlite3 *db, /* Connection to notify of malloc failures */
109449 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
109450 Token *pTable, /* Table to append */
109451 Token *pDatabase /* Database of the table */
109452){
109453 struct SrcList_item *pItem;
109454 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */
109455 assert( db!=0 );
109456 if( pList==0 ){
109457 pList = sqlite3DbMallocRawNN(db, sizeof(SrcList) );
109458 if( pList==0 ) return 0;
109459 pList->nAlloc = 1;
109460 pList->nSrc = 1;
109461 memset(&pList->a[0], 0, sizeof(pList->a[0]));
109462 pList->a[0].iCursor = -1;
109463 }else{
109464 pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
109465 }
109466 if( db->mallocFailed ){
109467 sqlite3SrcListDelete(db, pList);
109468 return 0;
109469 }
109470 pItem = &pList->a[pList->nSrc-1];
109471 if( pDatabase && pDatabase->z==0 ){
109472 pDatabase = 0;
109473 }
109474 if( pDatabase ){
109475 pItem->zName = sqlite3NameFromToken(db, pDatabase);
109476 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
109477 }else{
109478 pItem->zName = sqlite3NameFromToken(db, pTable);
109479 pItem->zDatabase = 0;
109480 }
109481 return pList;
109482}
109483
109484/*
109485** Assign VdbeCursor index numbers to all tables in a SrcList
109486*/
109487SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
109488 int i;
109489 struct SrcList_item *pItem;
109490 assert(pList || pParse->db->mallocFailed );
109491 if( pList ){
109492 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
109493 if( pItem->iCursor>=0 ) break;
109494 pItem->iCursor = pParse->nTab++;
109495 if( pItem->pSelect ){
109496 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
109497 }
109498 }
109499 }
109500}
109501
109502/*
109503** Delete an entire SrcList including all its substructure.
109504*/
109505SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
109506 int i;
109507 struct SrcList_item *pItem;
109508 if( pList==0 ) return;
109509 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
109510 sqlite3DbFree(db, pItem->zDatabase);
109511 sqlite3DbFree(db, pItem->zName);
109512 sqlite3DbFree(db, pItem->zAlias);
109513 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
109514 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
109515 sqlite3DeleteTable(db, pItem->pTab);
109516 sqlite3SelectDelete(db, pItem->pSelect);
109517 sqlite3ExprDelete(db, pItem->pOn);
109518 sqlite3IdListDelete(db, pItem->pUsing);
109519 }
109520 sqlite3DbFreeNN(db, pList);
109521}
109522
109523/*
109524** This routine is called by the parser to add a new term to the
109525** end of a growing FROM clause. The "p" parameter is the part of
109526** the FROM clause that has already been constructed. "p" is NULL
109527** if this is the first term of the FROM clause. pTable and pDatabase
109528** are the name of the table and database named in the FROM clause term.
109529** pDatabase is NULL if the database name qualifier is missing - the
109530** usual case. If the term has an alias, then pAlias points to the
109531** alias token. If the term is a subquery, then pSubquery is the
109532** SELECT statement that the subquery encodes. The pTable and
109533** pDatabase parameters are NULL for subqueries. The pOn and pUsing
109534** parameters are the content of the ON and USING clauses.
109535**
109536** Return a new SrcList which encodes is the FROM with the new
109537** term added.
109538*/
109539SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
109540 Parse *pParse, /* Parsing context */
109541 SrcList *p, /* The left part of the FROM clause already seen */
109542 Token *pTable, /* Name of the table to add to the FROM clause */
109543 Token *pDatabase, /* Name of the database containing pTable */
109544 Token *pAlias, /* The right-hand side of the AS subexpression */
109545 Select *pSubquery, /* A subquery used in place of a table name */
109546 Expr *pOn, /* The ON clause of a join */
109547 IdList *pUsing /* The USING clause of a join */
109548){
109549 struct SrcList_item *pItem;
109550 sqlite3 *db = pParse->db;
109551 if( !p && (pOn || pUsing) ){
109552 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
109553 (pOn ? "ON" : "USING")
109554 );
109555 goto append_from_error;
109556 }
109557 p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
109558 if( p==0 ){
109559 goto append_from_error;
109560 }
109561 assert( p->nSrc>0 );
109562 pItem = &p->a[p->nSrc-1];
109563 assert( (pTable==0)==(pDatabase==0) );
109564 assert( pItem->zName==0 || pDatabase!=0 );
109565 if( IN_RENAME_OBJECT && pItem->zName ){
109566 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
109567 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
109568 }
109569 assert( pAlias!=0 );
109570 if( pAlias->n ){
109571 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
109572 }
109573 pItem->pSelect = pSubquery;
109574 pItem->pOn = pOn;
109575 pItem->pUsing = pUsing;
109576 return p;
109577
109578 append_from_error:
109579 assert( p==0 );
109580 sqlite3ExprDelete(db, pOn);
109581 sqlite3IdListDelete(db, pUsing);
109582 sqlite3SelectDelete(db, pSubquery);
109583 return 0;
109584}
109585
109586/*
109587** Add an INDEXED BY or NOT INDEXED clause to the most recently added
109588** element of the source-list passed as the second argument.
109589*/
109590SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
109591 assert( pIndexedBy!=0 );
109592 if( p && pIndexedBy->n>0 ){
109593 struct SrcList_item *pItem;
109594 assert( p->nSrc>0 );
109595 pItem = &p->a[p->nSrc-1];
109596 assert( pItem->fg.notIndexed==0 );
109597 assert( pItem->fg.isIndexedBy==0 );
109598 assert( pItem->fg.isTabFunc==0 );
109599 if( pIndexedBy->n==1 && !pIndexedBy->z ){
109600 /* A "NOT INDEXED" clause was supplied. See parse.y
109601 ** construct "indexed_opt" for details. */
109602 pItem->fg.notIndexed = 1;
109603 }else{
109604 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
109605 pItem->fg.isIndexedBy = 1;
109606 }
109607 }
109608}
109609
109610/*
109611** Add the list of function arguments to the SrcList entry for a
109612** table-valued-function.
109613*/
109614SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
109615 if( p ){
109616 struct SrcList_item *pItem = &p->a[p->nSrc-1];
109617 assert( pItem->fg.notIndexed==0 );
109618 assert( pItem->fg.isIndexedBy==0 );
109619 assert( pItem->fg.isTabFunc==0 );
109620 pItem->u1.pFuncArg = pList;
109621 pItem->fg.isTabFunc = 1;
109622 }else{
109623 sqlite3ExprListDelete(pParse->db, pList);
109624 }
109625}
109626
109627/*
109628** When building up a FROM clause in the parser, the join operator
109629** is initially attached to the left operand. But the code generator
109630** expects the join operator to be on the right operand. This routine
109631** Shifts all join operators from left to right for an entire FROM
109632** clause.
109633**
109634** Example: Suppose the join is like this:
109635**
109636** A natural cross join B
109637**
109638** The operator is "natural cross join". The A and B operands are stored
109639** in p->a[0] and p->a[1], respectively. The parser initially stores the
109640** operator with A. This routine shifts that operator over to B.
109641*/
109642SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
109643 if( p ){
109644 int i;
109645 for(i=p->nSrc-1; i>0; i--){
109646 p->a[i].fg.jointype = p->a[i-1].fg.jointype;
109647 }
109648 p->a[0].fg.jointype = 0;
109649 }
109650}
109651
109652/*
109653** Generate VDBE code for a BEGIN statement.
109654*/
109655SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
109656 sqlite3 *db;
109657 Vdbe *v;
109658 int i;
109659
109660 assert( pParse!=0 );
109661 db = pParse->db;
109662 assert( db!=0 );
109663 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
109664 return;
109665 }
109666 v = sqlite3GetVdbe(pParse);
109667 if( !v ) return;
109668 if( type!=TK_DEFERRED ){
109669 for(i=0; i<db->nDb; i++){
109670 sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
109671 sqlite3VdbeUsesBtree(v, i);
109672 }
109673 }
109674 sqlite3VdbeAddOp0(v, OP_AutoCommit);
109675}
109676
109677/*
109678** Generate VDBE code for a COMMIT or ROLLBACK statement.
109679** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise
109680** code is generated for a COMMIT.
109681*/
109682SQLITE_PRIVATE void sqlite3EndTransaction(Parse *pParse, int eType){
109683 Vdbe *v;
109684 int isRollback;
109685
109686 assert( pParse!=0 );
109687 assert( pParse->db!=0 );
109688 assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK );
109689 isRollback = eType==TK_ROLLBACK;
109690 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION,
109691 isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){
109692 return;
109693 }
109694 v = sqlite3GetVdbe(pParse);
109695 if( v ){
109696 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback);
109697 }
109698}
109699
109700/*
109701** This function is called by the parser when it parses a command to create,
109702** release or rollback an SQL savepoint.
109703*/
109704SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
109705 char *zName = sqlite3NameFromToken(pParse->db, pName);
109706 if( zName ){
109707 Vdbe *v = sqlite3GetVdbe(pParse);
109708#ifndef SQLITE_OMIT_AUTHORIZATION
109709 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
109710 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
109711#endif
109712 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
109713 sqlite3DbFree(pParse->db, zName);
109714 return;
109715 }
109716 sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
109717 }
109718}
109719
109720/*
109721** Make sure the TEMP database is open and available for use. Return
109722** the number of errors. Leave any error messages in the pParse structure.
109723*/
109724SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
109725 sqlite3 *db = pParse->db;
109726 if( db->aDb[1].pBt==0 && !pParse->explain ){
109727 int rc;
109728 Btree *pBt;
109729 static const int flags =
109730 SQLITE_OPEN_READWRITE |
109731 SQLITE_OPEN_CREATE |
109732 SQLITE_OPEN_EXCLUSIVE |
109733 SQLITE_OPEN_DELETEONCLOSE |
109734 SQLITE_OPEN_TEMP_DB;
109735
109736 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
109737 if( rc!=SQLITE_OK ){
109738 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
109739 "file for storing temporary tables");
109740 pParse->rc = rc;
109741 return 1;
109742 }
109743 db->aDb[1].pBt = pBt;
109744 assert( db->aDb[1].pSchema );
109745 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
109746 sqlite3OomFault(db);
109747 return 1;
109748 }
109749 }
109750 return 0;
109751}
109752
109753/*
109754** Record the fact that the schema cookie will need to be verified
109755** for database iDb. The code to actually verify the schema cookie
109756** will occur at the end of the top-level VDBE and will be generated
109757** later, by sqlite3FinishCoding().
109758*/
109759SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
109760 Parse *pToplevel = sqlite3ParseToplevel(pParse);
109761
109762 assert( iDb>=0 && iDb<pParse->db->nDb );
109763 assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 );
109764 assert( iDb<SQLITE_MAX_ATTACHED+2 );
109765 assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) );
109766 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
109767 DbMaskSet(pToplevel->cookieMask, iDb);
109768 if( !OMIT_TEMPDB && iDb==1 ){
109769 sqlite3OpenTempDatabase(pToplevel);
109770 }
109771 }
109772}
109773
109774/*
109775** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
109776** attached database. Otherwise, invoke it for the database named zDb only.
109777*/
109778SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
109779 sqlite3 *db = pParse->db;
109780 int i;
109781 for(i=0; i<db->nDb; i++){
109782 Db *pDb = &db->aDb[i];
109783 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
109784 sqlite3CodeVerifySchema(pParse, i);
109785 }
109786 }
109787}
109788
109789/*
109790** Generate VDBE code that prepares for doing an operation that
109791** might change the database.
109792**
109793** This routine starts a new transaction if we are not already within
109794** a transaction. If we are already within a transaction, then a checkpoint
109795** is set if the setStatement parameter is true. A checkpoint should
109796** be set for operations that might fail (due to a constraint) part of
109797** the way through and which will need to undo some writes without having to
109798** rollback the whole transaction. For operations where all constraints
109799** can be checked before any changes are made to the database, it is never
109800** necessary to undo a write and the checkpoint should not be set.
109801*/
109802SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
109803 Parse *pToplevel = sqlite3ParseToplevel(pParse);
109804 sqlite3CodeVerifySchema(pParse, iDb);
109805 DbMaskSet(pToplevel->writeMask, iDb);
109806 pToplevel->isMultiWrite |= setStatement;
109807}
109808
109809/*
109810** Indicate that the statement currently under construction might write
109811** more than one entry (example: deleting one row then inserting another,
109812** inserting multiple rows in a table, or inserting a row and index entries.)
109813** If an abort occurs after some of these writes have completed, then it will
109814** be necessary to undo the completed writes.
109815*/
109816SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
109817 Parse *pToplevel = sqlite3ParseToplevel(pParse);
109818 pToplevel->isMultiWrite = 1;
109819}
109820
109821/*
109822** The code generator calls this routine if is discovers that it is
109823** possible to abort a statement prior to completion. In order to
109824** perform this abort without corrupting the database, we need to make
109825** sure that the statement is protected by a statement transaction.
109826**
109827** Technically, we only need to set the mayAbort flag if the
109828** isMultiWrite flag was previously set. There is a time dependency
109829** such that the abort must occur after the multiwrite. This makes
109830** some statements involving the REPLACE conflict resolution algorithm
109831** go a little faster. But taking advantage of this time dependency
109832** makes it more difficult to prove that the code is correct (in
109833** particular, it prevents us from writing an effective
109834** implementation of sqlite3AssertMayAbort()) and so we have chosen
109835** to take the safe route and skip the optimization.
109836*/
109837SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
109838 Parse *pToplevel = sqlite3ParseToplevel(pParse);
109839 pToplevel->mayAbort = 1;
109840}
109841
109842/*
109843** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
109844** error. The onError parameter determines which (if any) of the statement
109845** and/or current transaction is rolled back.
109846*/
109847SQLITE_PRIVATE void sqlite3HaltConstraint(
109848 Parse *pParse, /* Parsing context */
109849 int errCode, /* extended error code */
109850 int onError, /* Constraint type */
109851 char *p4, /* Error message */
109852 i8 p4type, /* P4_STATIC or P4_TRANSIENT */
109853 u8 p5Errmsg /* P5_ErrMsg type */
109854){
109855 Vdbe *v = sqlite3GetVdbe(pParse);
109856 assert( (errCode&0xff)==SQLITE_CONSTRAINT );
109857 if( onError==OE_Abort ){
109858 sqlite3MayAbort(pParse);
109859 }
109860 sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
109861 sqlite3VdbeChangeP5(v, p5Errmsg);
109862}
109863
109864/*
109865** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
109866*/
109867SQLITE_PRIVATE void sqlite3UniqueConstraint(
109868 Parse *pParse, /* Parsing context */
109869 int onError, /* Constraint type */
109870 Index *pIdx /* The index that triggers the constraint */
109871){
109872 char *zErr;
109873 int j;
109874 StrAccum errMsg;
109875 Table *pTab = pIdx->pTable;
109876
109877 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
109878 if( pIdx->aColExpr ){
109879 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
109880 }else{
109881 for(j=0; j<pIdx->nKeyCol; j++){
109882 char *zCol;
109883 assert( pIdx->aiColumn[j]>=0 );
109884 zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
109885 if( j ) sqlite3_str_append(&errMsg, ", ", 2);
109886 sqlite3_str_appendall(&errMsg, pTab->zName);
109887 sqlite3_str_append(&errMsg, ".", 1);
109888 sqlite3_str_appendall(&errMsg, zCol);
109889 }
109890 }
109891 zErr = sqlite3StrAccumFinish(&errMsg);
109892 sqlite3HaltConstraint(pParse,
109893 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
109894 : SQLITE_CONSTRAINT_UNIQUE,
109895 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
109896}
109897
109898
109899/*
109900** Code an OP_Halt due to non-unique rowid.
109901*/
109902SQLITE_PRIVATE void sqlite3RowidConstraint(
109903 Parse *pParse, /* Parsing context */
109904 int onError, /* Conflict resolution algorithm */
109905 Table *pTab /* The table with the non-unique rowid */
109906){
109907 char *zMsg;
109908 int rc;
109909 if( pTab->iPKey>=0 ){
109910 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
109911 pTab->aCol[pTab->iPKey].zName);
109912 rc = SQLITE_CONSTRAINT_PRIMARYKEY;
109913 }else{
109914 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
109915 rc = SQLITE_CONSTRAINT_ROWID;
109916 }
109917 sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC,
109918 P5_ConstraintUnique);
109919}
109920
109921/*
109922** Check to see if pIndex uses the collating sequence pColl. Return
109923** true if it does and false if it does not.
109924*/
109925#ifndef SQLITE_OMIT_REINDEX
109926static int collationMatch(const char *zColl, Index *pIndex){
109927 int i;
109928 assert( zColl!=0 );
109929 for(i=0; i<pIndex->nColumn; i++){
109930 const char *z = pIndex->azColl[i];
109931 assert( z!=0 || pIndex->aiColumn[i]<0 );
109932 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
109933 return 1;
109934 }
109935 }
109936 return 0;
109937}
109938#endif
109939
109940/*
109941** Recompute all indices of pTab that use the collating sequence pColl.
109942** If pColl==0 then recompute all indices of pTab.
109943*/
109944#ifndef SQLITE_OMIT_REINDEX
109945static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
109946 Index *pIndex; /* An index associated with pTab */
109947
109948 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
109949 if( zColl==0 || collationMatch(zColl, pIndex) ){
109950 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
109951 sqlite3BeginWriteOperation(pParse, 0, iDb);
109952 sqlite3RefillIndex(pParse, pIndex, -1);
109953 }
109954 }
109955}
109956#endif
109957
109958/*
109959** Recompute all indices of all tables in all databases where the
109960** indices use the collating sequence pColl. If pColl==0 then recompute
109961** all indices everywhere.
109962*/
109963#ifndef SQLITE_OMIT_REINDEX
109964static void reindexDatabases(Parse *pParse, char const *zColl){
109965 Db *pDb; /* A single database */
109966 int iDb; /* The database index number */
109967 sqlite3 *db = pParse->db; /* The database connection */
109968 HashElem *k; /* For looping over tables in pDb */
109969 Table *pTab; /* A table in the database */
109970
109971 assert( sqlite3BtreeHoldsAllMutexes(db) ); /* Needed for schema access */
109972 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
109973 assert( pDb!=0 );
109974 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
109975 pTab = (Table*)sqliteHashData(k);
109976 reindexTable(pParse, pTab, zColl);
109977 }
109978 }
109979}
109980#endif
109981
109982/*
109983** Generate code for the REINDEX command.
109984**
109985** REINDEX -- 1
109986** REINDEX <collation> -- 2
109987** REINDEX ?<database>.?<tablename> -- 3
109988** REINDEX ?<database>.?<indexname> -- 4
109989**
109990** Form 1 causes all indices in all attached databases to be rebuilt.
109991** Form 2 rebuilds all indices in all databases that use the named
109992** collating function. Forms 3 and 4 rebuild the named index or all
109993** indices associated with the named table.
109994*/
109995#ifndef SQLITE_OMIT_REINDEX
109996SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
109997 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
109998 char *z; /* Name of a table or index */
109999 const char *zDb; /* Name of the database */
110000 Table *pTab; /* A table in the database */
110001 Index *pIndex; /* An index associated with pTab */
110002 int iDb; /* The database index number */
110003 sqlite3 *db = pParse->db; /* The database connection */
110004 Token *pObjName; /* Name of the table or index to be reindexed */
110005
110006 /* Read the database schema. If an error occurs, leave an error message
110007 ** and code in pParse and return NULL. */
110008 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
110009 return;
110010 }
110011
110012 if( pName1==0 ){
110013 reindexDatabases(pParse, 0);
110014 return;
110015 }else if( NEVER(pName2==0) || pName2->z==0 ){
110016 char *zColl;
110017 assert( pName1->z );
110018 zColl = sqlite3NameFromToken(pParse->db, pName1);
110019 if( !zColl ) return;
110020 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
110021 if( pColl ){
110022 reindexDatabases(pParse, zColl);
110023 sqlite3DbFree(db, zColl);
110024 return;
110025 }
110026 sqlite3DbFree(db, zColl);
110027 }
110028 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
110029 if( iDb<0 ) return;
110030 z = sqlite3NameFromToken(db, pObjName);
110031 if( z==0 ) return;
110032 zDb = db->aDb[iDb].zDbSName;
110033 pTab = sqlite3FindTable(db, z, zDb);
110034 if( pTab ){
110035 reindexTable(pParse, pTab, 0);
110036 sqlite3DbFree(db, z);
110037 return;
110038 }
110039 pIndex = sqlite3FindIndex(db, z, zDb);
110040 sqlite3DbFree(db, z);
110041 if( pIndex ){
110042 sqlite3BeginWriteOperation(pParse, 0, iDb);
110043 sqlite3RefillIndex(pParse, pIndex, -1);
110044 return;
110045 }
110046 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
110047}
110048#endif
110049
110050/*
110051** Return a KeyInfo structure that is appropriate for the given Index.
110052**
110053** The caller should invoke sqlite3KeyInfoUnref() on the returned object
110054** when it has finished using it.
110055*/
110056SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
110057 int i;
110058 int nCol = pIdx->nColumn;
110059 int nKey = pIdx->nKeyCol;
110060 KeyInfo *pKey;
110061 if( pParse->nErr ) return 0;
110062 if( pIdx->uniqNotNull ){
110063 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
110064 }else{
110065 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
110066 }
110067 if( pKey ){
110068 assert( sqlite3KeyInfoIsWriteable(pKey) );
110069 for(i=0; i<nCol; i++){
110070 const char *zColl = pIdx->azColl[i];
110071 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
110072 sqlite3LocateCollSeq(pParse, zColl);
110073 pKey->aSortOrder[i] = pIdx->aSortOrder[i];
110074 }
110075 if( pParse->nErr ){
110076 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
110077 if( pIdx->bNoQuery==0 ){
110078 /* Deactivate the index because it contains an unknown collating
110079 ** sequence. The only way to reactive the index is to reload the
110080 ** schema. Adding the missing collating sequence later does not
110081 ** reactive the index. The application had the chance to register
110082 ** the missing index using the collation-needed callback. For
110083 ** simplicity, SQLite will not give the application a second chance.
110084 */
110085 pIdx->bNoQuery = 1;
110086 pParse->rc = SQLITE_ERROR_RETRY;
110087 }
110088 sqlite3KeyInfoUnref(pKey);
110089 pKey = 0;
110090 }
110091 }
110092 return pKey;
110093}
110094
110095#ifndef SQLITE_OMIT_CTE
110096/*
110097** This routine is invoked once per CTE by the parser while parsing a
110098** WITH clause.
110099*/
110100SQLITE_PRIVATE With *sqlite3WithAdd(
110101 Parse *pParse, /* Parsing context */
110102 With *pWith, /* Existing WITH clause, or NULL */
110103 Token *pName, /* Name of the common-table */
110104 ExprList *pArglist, /* Optional column name list for the table */
110105 Select *pQuery /* Query used to initialize the table */
110106){
110107 sqlite3 *db = pParse->db;
110108 With *pNew;
110109 char *zName;
110110
110111 /* Check that the CTE name is unique within this WITH clause. If
110112 ** not, store an error in the Parse structure. */
110113 zName = sqlite3NameFromToken(pParse->db, pName);
110114 if( zName && pWith ){
110115 int i;
110116 for(i=0; i<pWith->nCte; i++){
110117 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
110118 sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
110119 }
110120 }
110121 }
110122
110123 if( pWith ){
110124 int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
110125 pNew = sqlite3DbRealloc(db, pWith, nByte);
110126 }else{
110127 pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
110128 }
110129 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
110130
110131 if( db->mallocFailed ){
110132 sqlite3ExprListDelete(db, pArglist);
110133 sqlite3SelectDelete(db, pQuery);
110134 sqlite3DbFree(db, zName);
110135 pNew = pWith;
110136 }else{
110137 pNew->a[pNew->nCte].pSelect = pQuery;
110138 pNew->a[pNew->nCte].pCols = pArglist;
110139 pNew->a[pNew->nCte].zName = zName;
110140 pNew->a[pNew->nCte].zCteErr = 0;
110141 pNew->nCte++;
110142 }
110143
110144 return pNew;
110145}
110146
110147/*
110148** Free the contents of the With object passed as the second argument.
110149*/
110150SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){
110151 if( pWith ){
110152 int i;
110153 for(i=0; i<pWith->nCte; i++){
110154 struct Cte *pCte = &pWith->a[i];
110155 sqlite3ExprListDelete(db, pCte->pCols);
110156 sqlite3SelectDelete(db, pCte->pSelect);
110157 sqlite3DbFree(db, pCte->zName);
110158 }
110159 sqlite3DbFree(db, pWith);
110160 }
110161}
110162#endif /* !defined(SQLITE_OMIT_CTE) */
110163
110164/************** End of build.c ***********************************************/
110165/************** Begin file callback.c ****************************************/
110166/*
110167** 2005 May 23
110168**
110169** The author disclaims copyright to this source code. In place of
110170** a legal notice, here is a blessing:
110171**
110172** May you do good and not evil.
110173** May you find forgiveness for yourself and forgive others.
110174** May you share freely, never taking more than you give.
110175**
110176*************************************************************************
110177**
110178** This file contains functions used to access the internal hash tables
110179** of user defined functions and collation sequences.
110180*/
110181
110182/* #include "sqliteInt.h" */
110183
110184/*
110185** Invoke the 'collation needed' callback to request a collation sequence
110186** in the encoding enc of name zName, length nName.
110187*/
110188static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
110189 assert( !db->xCollNeeded || !db->xCollNeeded16 );
110190 if( db->xCollNeeded ){
110191 char *zExternal = sqlite3DbStrDup(db, zName);
110192 if( !zExternal ) return;
110193 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
110194 sqlite3DbFree(db, zExternal);
110195 }
110196#ifndef SQLITE_OMIT_UTF16
110197 if( db->xCollNeeded16 ){
110198 char const *zExternal;
110199 sqlite3_value *pTmp = sqlite3ValueNew(db);
110200 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
110201 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
110202 if( zExternal ){
110203 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
110204 }
110205 sqlite3ValueFree(pTmp);
110206 }
110207#endif
110208}
110209
110210/*
110211** This routine is called if the collation factory fails to deliver a
110212** collation function in the best encoding but there may be other versions
110213** of this collation function (for other text encodings) available. Use one
110214** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
110215** possible.
110216*/
110217static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
110218 CollSeq *pColl2;
110219 char *z = pColl->zName;
110220 int i;
110221 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
110222 for(i=0; i<3; i++){
110223 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
110224 if( pColl2->xCmp!=0 ){
110225 memcpy(pColl, pColl2, sizeof(CollSeq));
110226 pColl->xDel = 0; /* Do not copy the destructor */
110227 return SQLITE_OK;
110228 }
110229 }
110230 return SQLITE_ERROR;
110231}
110232
110233/*
110234** This function is responsible for invoking the collation factory callback
110235** or substituting a collation sequence of a different encoding when the
110236** requested collation sequence is not available in the desired encoding.
110237**
110238** If it is not NULL, then pColl must point to the database native encoding
110239** collation sequence with name zName, length nName.
110240**
110241** The return value is either the collation sequence to be used in database
110242** db for collation type name zName, length nName, or NULL, if no collation
110243** sequence can be found. If no collation is found, leave an error message.
110244**
110245** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
110246*/
110247SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
110248 Parse *pParse, /* Parsing context */
110249 u8 enc, /* The desired encoding for the collating sequence */
110250 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
110251 const char *zName /* Collating sequence name */
110252){
110253 CollSeq *p;
110254 sqlite3 *db = pParse->db;
110255
110256 p = pColl;
110257 if( !p ){
110258 p = sqlite3FindCollSeq(db, enc, zName, 0);
110259 }
110260 if( !p || !p->xCmp ){
110261 /* No collation sequence of this type for this encoding is registered.
110262 ** Call the collation factory to see if it can supply us with one.
110263 */
110264 callCollNeeded(db, enc, zName);
110265 p = sqlite3FindCollSeq(db, enc, zName, 0);
110266 }
110267 if( p && !p->xCmp && synthCollSeq(db, p) ){
110268 p = 0;
110269 }
110270 assert( !p || p->xCmp );
110271 if( p==0 ){
110272 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
110273 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
110274 }
110275 return p;
110276}
110277
110278/*
110279** This routine is called on a collation sequence before it is used to
110280** check that it is defined. An undefined collation sequence exists when
110281** a database is loaded that contains references to collation sequences
110282** that have not been defined by sqlite3_create_collation() etc.
110283**
110284** If required, this routine calls the 'collation needed' callback to
110285** request a definition of the collating sequence. If this doesn't work,
110286** an equivalent collating sequence that uses a text encoding different
110287** from the main database is substituted, if one is available.
110288*/
110289SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
110290 if( pColl && pColl->xCmp==0 ){
110291 const char *zName = pColl->zName;
110292 sqlite3 *db = pParse->db;
110293 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
110294 if( !p ){
110295 return SQLITE_ERROR;
110296 }
110297 assert( p==pColl );
110298 }
110299 return SQLITE_OK;
110300}
110301
110302
110303
110304/*
110305** Locate and return an entry from the db.aCollSeq hash table. If the entry
110306** specified by zName and nName is not found and parameter 'create' is
110307** true, then create a new entry. Otherwise return NULL.
110308**
110309** Each pointer stored in the sqlite3.aCollSeq hash table contains an
110310** array of three CollSeq structures. The first is the collation sequence
110311** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
110312**
110313** Stored immediately after the three collation sequences is a copy of
110314** the collation sequence name. A pointer to this string is stored in
110315** each collation sequence structure.
110316*/
110317static CollSeq *findCollSeqEntry(
110318 sqlite3 *db, /* Database connection */
110319 const char *zName, /* Name of the collating sequence */
110320 int create /* Create a new entry if true */
110321){
110322 CollSeq *pColl;
110323 pColl = sqlite3HashFind(&db->aCollSeq, zName);
110324
110325 if( 0==pColl && create ){
110326 int nName = sqlite3Strlen30(zName) + 1;
110327 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
110328 if( pColl ){
110329 CollSeq *pDel = 0;
110330 pColl[0].zName = (char*)&pColl[3];
110331 pColl[0].enc = SQLITE_UTF8;
110332 pColl[1].zName = (char*)&pColl[3];
110333 pColl[1].enc = SQLITE_UTF16LE;
110334 pColl[2].zName = (char*)&pColl[3];
110335 pColl[2].enc = SQLITE_UTF16BE;
110336 memcpy(pColl[0].zName, zName, nName);
110337 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
110338
110339 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
110340 ** return the pColl pointer to be deleted (because it wasn't added
110341 ** to the hash table).
110342 */
110343 assert( pDel==0 || pDel==pColl );
110344 if( pDel!=0 ){
110345 sqlite3OomFault(db);
110346 sqlite3DbFree(db, pDel);
110347 pColl = 0;
110348 }
110349 }
110350 }
110351 return pColl;
110352}
110353
110354/*
110355** Parameter zName points to a UTF-8 encoded string nName bytes long.
110356** Return the CollSeq* pointer for the collation sequence named zName
110357** for the encoding 'enc' from the database 'db'.
110358**
110359** If the entry specified is not found and 'create' is true, then create a
110360** new entry. Otherwise return NULL.
110361**
110362** A separate function sqlite3LocateCollSeq() is a wrapper around
110363** this routine. sqlite3LocateCollSeq() invokes the collation factory
110364** if necessary and generates an error message if the collating sequence
110365** cannot be found.
110366**
110367** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
110368*/
110369SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
110370 sqlite3 *db,
110371 u8 enc,
110372 const char *zName,
110373 int create
110374){
110375 CollSeq *pColl;
110376 if( zName ){
110377 pColl = findCollSeqEntry(db, zName, create);
110378 }else{
110379 pColl = db->pDfltColl;
110380 }
110381 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
110382 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
110383 if( pColl ) pColl += enc-1;
110384 return pColl;
110385}
110386
110387/* During the search for the best function definition, this procedure
110388** is called to test how well the function passed as the first argument
110389** matches the request for a function with nArg arguments in a system
110390** that uses encoding enc. The value returned indicates how well the
110391** request is matched. A higher value indicates a better match.
110392**
110393** If nArg is -1 that means to only return a match (non-zero) if p->nArg
110394** is also -1. In other words, we are searching for a function that
110395** takes a variable number of arguments.
110396**
110397** If nArg is -2 that means that we are searching for any function
110398** regardless of the number of arguments it uses, so return a positive
110399** match score for any
110400**
110401** The returned value is always between 0 and 6, as follows:
110402**
110403** 0: Not a match.
110404** 1: UTF8/16 conversion required and function takes any number of arguments.
110405** 2: UTF16 byte order change required and function takes any number of args.
110406** 3: encoding matches and function takes any number of arguments
110407** 4: UTF8/16 conversion required - argument count matches exactly
110408** 5: UTF16 byte order conversion required - argument count matches exactly
110409** 6: Perfect match: encoding and argument count match exactly.
110410**
110411** If nArg==(-2) then any function with a non-null xSFunc is
110412** a perfect match and any function with xSFunc NULL is
110413** a non-match.
110414*/
110415#define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */
110416static int matchQuality(
110417 FuncDef *p, /* The function we are evaluating for match quality */
110418 int nArg, /* Desired number of arguments. (-1)==any */
110419 u8 enc /* Desired text encoding */
110420){
110421 int match;
110422
110423 /* nArg of -2 is a special case */
110424 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
110425
110426 /* Wrong number of arguments means "no match" */
110427 if( p->nArg!=nArg && p->nArg>=0 ) return 0;
110428
110429 /* Give a better score to a function with a specific number of arguments
110430 ** than to function that accepts any number of arguments. */
110431 if( p->nArg==nArg ){
110432 match = 4;
110433 }else{
110434 match = 1;
110435 }
110436
110437 /* Bonus points if the text encoding matches */
110438 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
110439 match += 2; /* Exact encoding match */
110440 }else if( (enc & p->funcFlags & 2)!=0 ){
110441 match += 1; /* Both are UTF16, but with different byte orders */
110442 }
110443
110444 return match;
110445}
110446
110447/*
110448** Search a FuncDefHash for a function with the given name. Return
110449** a pointer to the matching FuncDef if found, or 0 if there is no match.
110450*/
110451static FuncDef *functionSearch(
110452 int h, /* Hash of the name */
110453 const char *zFunc /* Name of function */
110454){
110455 FuncDef *p;
110456 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
110457 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
110458 return p;
110459 }
110460 }
110461 return 0;
110462}
110463
110464/*
110465** Insert a new FuncDef into a FuncDefHash hash table.
110466*/
110467SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(
110468 FuncDef *aDef, /* List of global functions to be inserted */
110469 int nDef /* Length of the apDef[] list */
110470){
110471 int i;
110472 for(i=0; i<nDef; i++){
110473 FuncDef *pOther;
110474 const char *zName = aDef[i].zName;
110475 int nName = sqlite3Strlen30(zName);
110476 int h = (zName[0] + nName) % SQLITE_FUNC_HASH_SZ;
110477 assert( zName[0]>='a' && zName[0]<='z' );
110478 pOther = functionSearch(h, zName);
110479 if( pOther ){
110480 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
110481 aDef[i].pNext = pOther->pNext;
110482 pOther->pNext = &aDef[i];
110483 }else{
110484 aDef[i].pNext = 0;
110485 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
110486 sqlite3BuiltinFunctions.a[h] = &aDef[i];
110487 }
110488 }
110489}
110490
110491
110492
110493/*
110494** Locate a user function given a name, a number of arguments and a flag
110495** indicating whether the function prefers UTF-16 over UTF-8. Return a
110496** pointer to the FuncDef structure that defines that function, or return
110497** NULL if the function does not exist.
110498**
110499** If the createFlag argument is true, then a new (blank) FuncDef
110500** structure is created and liked into the "db" structure if a
110501** no matching function previously existed.
110502**
110503** If nArg is -2, then the first valid function found is returned. A
110504** function is valid if xSFunc is non-zero. The nArg==(-2)
110505** case is used to see if zName is a valid function name for some number
110506** of arguments. If nArg is -2, then createFlag must be 0.
110507**
110508** If createFlag is false, then a function with the required name and
110509** number of arguments may be returned even if the eTextRep flag does not
110510** match that requested.
110511*/
110512SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
110513 sqlite3 *db, /* An open database */
110514 const char *zName, /* Name of the function. zero-terminated */
110515 int nArg, /* Number of arguments. -1 means any number */
110516 u8 enc, /* Preferred text encoding */
110517 u8 createFlag /* Create new entry if true and does not otherwise exist */
110518){
110519 FuncDef *p; /* Iterator variable */
110520 FuncDef *pBest = 0; /* Best match found so far */
110521 int bestScore = 0; /* Score of best match */
110522 int h; /* Hash value */
110523 int nName; /* Length of the name */
110524
110525 assert( nArg>=(-2) );
110526 assert( nArg>=(-1) || createFlag==0 );
110527 nName = sqlite3Strlen30(zName);
110528
110529 /* First search for a match amongst the application-defined functions.
110530 */
110531 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
110532 while( p ){
110533 int score = matchQuality(p, nArg, enc);
110534 if( score>bestScore ){
110535 pBest = p;
110536 bestScore = score;
110537 }
110538 p = p->pNext;
110539 }
110540
110541 /* If no match is found, search the built-in functions.
110542 **
110543 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
110544 ** functions even if a prior app-defined function was found. And give
110545 ** priority to built-in functions.
110546 **
110547 ** Except, if createFlag is true, that means that we are trying to
110548 ** install a new function. Whatever FuncDef structure is returned it will
110549 ** have fields overwritten with new information appropriate for the
110550 ** new function. But the FuncDefs for built-in functions are read-only.
110551 ** So we must not search for built-ins when creating a new function.
110552 */
110553 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
110554 bestScore = 0;
110555 h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % SQLITE_FUNC_HASH_SZ;
110556 p = functionSearch(h, zName);
110557 while( p ){
110558 int score = matchQuality(p, nArg, enc);
110559 if( score>bestScore ){
110560 pBest = p;
110561 bestScore = score;
110562 }
110563 p = p->pNext;
110564 }
110565 }
110566
110567 /* If the createFlag parameter is true and the search did not reveal an
110568 ** exact match for the name, number of arguments and encoding, then add a
110569 ** new entry to the hash table and return it.
110570 */
110571 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
110572 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
110573 FuncDef *pOther;
110574 u8 *z;
110575 pBest->zName = (const char*)&pBest[1];
110576 pBest->nArg = (u16)nArg;
110577 pBest->funcFlags = enc;
110578 memcpy((char*)&pBest[1], zName, nName+1);
110579 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
110580 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
110581 if( pOther==pBest ){
110582 sqlite3DbFree(db, pBest);
110583 sqlite3OomFault(db);
110584 return 0;
110585 }else{
110586 pBest->pNext = pOther;
110587 }
110588 }
110589
110590 if( pBest && (pBest->xSFunc || createFlag) ){
110591 return pBest;
110592 }
110593 return 0;
110594}
110595
110596/*
110597** Free all resources held by the schema structure. The void* argument points
110598** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
110599** pointer itself, it just cleans up subsidiary resources (i.e. the contents
110600** of the schema hash tables).
110601**
110602** The Schema.cache_size variable is not cleared.
110603*/
110604SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
110605 Hash temp1;
110606 Hash temp2;
110607 HashElem *pElem;
110608 Schema *pSchema = (Schema *)p;
110609
110610 temp1 = pSchema->tblHash;
110611 temp2 = pSchema->trigHash;
110612 sqlite3HashInit(&pSchema->trigHash);
110613 sqlite3HashClear(&pSchema->idxHash);
110614 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
110615 sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
110616 }
110617 sqlite3HashClear(&temp2);
110618 sqlite3HashInit(&pSchema->tblHash);
110619 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
110620 Table *pTab = sqliteHashData(pElem);
110621 sqlite3DeleteTable(0, pTab);
110622 }
110623 sqlite3HashClear(&temp1);
110624 sqlite3HashClear(&pSchema->fkeyHash);
110625 pSchema->pSeqTab = 0;
110626 if( pSchema->schemaFlags & DB_SchemaLoaded ){
110627 pSchema->iGeneration++;
110628 }
110629 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
110630}
110631
110632/*
110633** Find and return the schema associated with a BTree. Create
110634** a new one if necessary.
110635*/
110636SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
110637 Schema * p;
110638 if( pBt ){
110639 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
110640 }else{
110641 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
110642 }
110643 if( !p ){
110644 sqlite3OomFault(db);
110645 }else if ( 0==p->file_format ){
110646 sqlite3HashInit(&p->tblHash);
110647 sqlite3HashInit(&p->idxHash);
110648 sqlite3HashInit(&p->trigHash);
110649 sqlite3HashInit(&p->fkeyHash);
110650 p->enc = SQLITE_UTF8;
110651 }
110652 return p;
110653}
110654
110655/************** End of callback.c ********************************************/
110656/************** Begin file delete.c ******************************************/
110657/*
110658** 2001 September 15
110659**
110660** The author disclaims copyright to this source code. In place of
110661** a legal notice, here is a blessing:
110662**
110663** May you do good and not evil.
110664** May you find forgiveness for yourself and forgive others.
110665** May you share freely, never taking more than you give.
110666**
110667*************************************************************************
110668** This file contains C code routines that are called by the parser
110669** in order to generate code for DELETE FROM statements.
110670*/
110671/* #include "sqliteInt.h" */
110672
110673/*
110674** While a SrcList can in general represent multiple tables and subqueries
110675** (as in the FROM clause of a SELECT statement) in this case it contains
110676** the name of a single table, as one might find in an INSERT, DELETE,
110677** or UPDATE statement. Look up that table in the symbol table and
110678** return a pointer. Set an error message and return NULL if the table
110679** name is not found or if any other error occurs.
110680**
110681** The following fields are initialized appropriate in pSrc:
110682**
110683** pSrc->a[0].pTab Pointer to the Table object
110684** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
110685**
110686*/
110687SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
110688 struct SrcList_item *pItem = pSrc->a;
110689 Table *pTab;
110690 assert( pItem && pSrc->nSrc==1 );
110691 pTab = sqlite3LocateTableItem(pParse, 0, pItem);
110692 sqlite3DeleteTable(pParse->db, pItem->pTab);
110693 pItem->pTab = pTab;
110694 if( pTab ){
110695 pTab->nTabRef++;
110696 }
110697 if( sqlite3IndexedByLookup(pParse, pItem) ){
110698 pTab = 0;
110699 }
110700 return pTab;
110701}
110702
110703/*
110704** Check to make sure the given table is writable. If it is not
110705** writable, generate an error message and return 1. If it is
110706** writable return 0;
110707*/
110708SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
110709 /* A table is not writable under the following circumstances:
110710 **
110711 ** 1) It is a virtual table and no implementation of the xUpdate method
110712 ** has been provided, or
110713 ** 2) It is a system table (i.e. sqlite_master), this call is not
110714 ** part of a nested parse and writable_schema pragma has not
110715 ** been specified.
110716 **
110717 ** In either case leave an error message in pParse and return non-zero.
110718 */
110719 if( ( IsVirtual(pTab)
110720 && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
110721 || ( (pTab->tabFlags & TF_Readonly)!=0
110722 && (pParse->db->flags & SQLITE_WriteSchema)==0
110723 && pParse->nested==0 )
110724 ){
110725 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
110726 return 1;
110727 }
110728
110729#ifndef SQLITE_OMIT_VIEW
110730 if( !viewOk && pTab->pSelect ){
110731 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
110732 return 1;
110733 }
110734#endif
110735 return 0;
110736}
110737
110738
110739#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
110740/*
110741** Evaluate a view and store its result in an ephemeral table. The
110742** pWhere argument is an optional WHERE clause that restricts the
110743** set of rows in the view that are to be added to the ephemeral table.
110744*/
110745SQLITE_PRIVATE void sqlite3MaterializeView(
110746 Parse *pParse, /* Parsing context */
110747 Table *pView, /* View definition */
110748 Expr *pWhere, /* Optional WHERE clause to be added */
110749 ExprList *pOrderBy, /* Optional ORDER BY clause */
110750 Expr *pLimit, /* Optional LIMIT clause */
110751 int iCur /* Cursor number for ephemeral table */
110752){
110753 SelectDest dest;
110754 Select *pSel;
110755 SrcList *pFrom;
110756 sqlite3 *db = pParse->db;
110757 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
110758 pWhere = sqlite3ExprDup(db, pWhere, 0);
110759 pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
110760 if( pFrom ){
110761 assert( pFrom->nSrc==1 );
110762 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
110763 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
110764 assert( pFrom->a[0].pOn==0 );
110765 assert( pFrom->a[0].pUsing==0 );
110766 }
110767 pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy,
110768 SF_IncludeHidden, pLimit);
110769 sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
110770 sqlite3Select(pParse, pSel, &dest);
110771 sqlite3SelectDelete(db, pSel);
110772}
110773#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
110774
110775#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
110776/*
110777** Generate an expression tree to implement the WHERE, ORDER BY,
110778** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
110779**
110780** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
110781** \__________________________/
110782** pLimitWhere (pInClause)
110783*/
110784SQLITE_PRIVATE Expr *sqlite3LimitWhere(
110785 Parse *pParse, /* The parser context */
110786 SrcList *pSrc, /* the FROM clause -- which tables to scan */
110787 Expr *pWhere, /* The WHERE clause. May be null */
110788 ExprList *pOrderBy, /* The ORDER BY clause. May be null */
110789 Expr *pLimit, /* The LIMIT clause. May be null */
110790 char *zStmtType /* Either DELETE or UPDATE. For err msgs. */
110791){
110792 sqlite3 *db = pParse->db;
110793 Expr *pLhs = NULL; /* LHS of IN(SELECT...) operator */
110794 Expr *pInClause = NULL; /* WHERE rowid IN ( select ) */
110795 ExprList *pEList = NULL; /* Expression list contaning only pSelectRowid */
110796 SrcList *pSelectSrc = NULL; /* SELECT rowid FROM x ... (dup of pSrc) */
110797 Select *pSelect = NULL; /* Complete SELECT tree */
110798 Table *pTab;
110799
110800 /* Check that there isn't an ORDER BY without a LIMIT clause.
110801 */
110802 if( pOrderBy && pLimit==0 ) {
110803 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
110804 sqlite3ExprDelete(pParse->db, pWhere);
110805 sqlite3ExprListDelete(pParse->db, pOrderBy);
110806 return 0;
110807 }
110808
110809 /* We only need to generate a select expression if there
110810 ** is a limit/offset term to enforce.
110811 */
110812 if( pLimit == 0 ) {
110813 return pWhere;
110814 }
110815
110816 /* Generate a select expression tree to enforce the limit/offset
110817 ** term for the DELETE or UPDATE statement. For example:
110818 ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
110819 ** becomes:
110820 ** DELETE FROM table_a WHERE rowid IN (
110821 ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
110822 ** );
110823 */
110824
110825 pTab = pSrc->a[0].pTab;
110826 if( HasRowid(pTab) ){
110827 pLhs = sqlite3PExpr(pParse, TK_ROW, 0, 0);
110828 pEList = sqlite3ExprListAppend(
110829 pParse, 0, sqlite3PExpr(pParse, TK_ROW, 0, 0)
110830 );
110831 }else{
110832 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
110833 if( pPk->nKeyCol==1 ){
110834 const char *zName = pTab->aCol[pPk->aiColumn[0]].zName;
110835 pLhs = sqlite3Expr(db, TK_ID, zName);
110836 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, zName));
110837 }else{
110838 int i;
110839 for(i=0; i<pPk->nKeyCol; i++){
110840 Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zName);
110841 pEList = sqlite3ExprListAppend(pParse, pEList, p);
110842 }
110843 pLhs = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
110844 if( pLhs ){
110845 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
110846 }
110847 }
110848 }
110849
110850 /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
110851 ** and the SELECT subtree. */
110852 pSrc->a[0].pTab = 0;
110853 pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
110854 pSrc->a[0].pTab = pTab;
110855 pSrc->a[0].pIBIndex = 0;
110856
110857 /* generate the SELECT expression tree. */
110858 pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0,
110859 pOrderBy,0,pLimit
110860 );
110861
110862 /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
110863 pInClause = sqlite3PExpr(pParse, TK_IN, pLhs, 0);
110864 sqlite3PExprAddSelect(pParse, pInClause, pSelect);
110865 return pInClause;
110866}
110867#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
110868 /* && !defined(SQLITE_OMIT_SUBQUERY) */
110869
110870/*
110871** Generate code for a DELETE FROM statement.
110872**
110873** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
110874** \________/ \________________/
110875** pTabList pWhere
110876*/
110877SQLITE_PRIVATE void sqlite3DeleteFrom(
110878 Parse *pParse, /* The parser context */
110879 SrcList *pTabList, /* The table from which we should delete things */
110880 Expr *pWhere, /* The WHERE clause. May be null */
110881 ExprList *pOrderBy, /* ORDER BY clause. May be null */
110882 Expr *pLimit /* LIMIT clause. May be null */
110883){
110884 Vdbe *v; /* The virtual database engine */
110885 Table *pTab; /* The table from which records will be deleted */
110886 int i; /* Loop counter */
110887 WhereInfo *pWInfo; /* Information about the WHERE clause */
110888 Index *pIdx; /* For looping over indices of the table */
110889 int iTabCur; /* Cursor number for the table */
110890 int iDataCur = 0; /* VDBE cursor for the canonical data source */
110891 int iIdxCur = 0; /* Cursor number of the first index */
110892 int nIdx; /* Number of indices */
110893 sqlite3 *db; /* Main database structure */
110894 AuthContext sContext; /* Authorization context */
110895 NameContext sNC; /* Name context to resolve expressions in */
110896 int iDb; /* Database number */
110897 int memCnt = 0; /* Memory cell used for change counting */
110898 int rcauth; /* Value returned by authorization callback */
110899 int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */
110900 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
110901 u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
110902 Index *pPk; /* The PRIMARY KEY index on the table */
110903 int iPk = 0; /* First of nPk registers holding PRIMARY KEY value */
110904 i16 nPk = 1; /* Number of columns in the PRIMARY KEY */
110905 int iKey; /* Memory cell holding key of row to be deleted */
110906 i16 nKey; /* Number of memory cells in the row key */
110907 int iEphCur = 0; /* Ephemeral table holding all primary key values */
110908 int iRowSet = 0; /* Register for rowset of rows to delete */
110909 int addrBypass = 0; /* Address of jump over the delete logic */
110910 int addrLoop = 0; /* Top of the delete loop */
110911 int addrEphOpen = 0; /* Instruction to open the Ephemeral table */
110912 int bComplex; /* True if there are triggers or FKs or
110913 ** subqueries in the WHERE clause */
110914
110915#ifndef SQLITE_OMIT_TRIGGER
110916 int isView; /* True if attempting to delete from a view */
110917 Trigger *pTrigger; /* List of table triggers, if required */
110918#endif
110919
110920 memset(&sContext, 0, sizeof(sContext));
110921 db = pParse->db;
110922 if( pParse->nErr || db->mallocFailed ){
110923 goto delete_from_cleanup;
110924 }
110925 assert( pTabList->nSrc==1 );
110926
110927
110928 /* Locate the table which we want to delete. This table has to be
110929 ** put in an SrcList structure because some of the subroutines we
110930 ** will be calling are designed to work with multiple tables and expect
110931 ** an SrcList* parameter instead of just a Table* parameter.
110932 */
110933 pTab = sqlite3SrcListLookup(pParse, pTabList);
110934 if( pTab==0 ) goto delete_from_cleanup;
110935
110936 /* Figure out if we have any triggers and if the table being
110937 ** deleted from is a view
110938 */
110939#ifndef SQLITE_OMIT_TRIGGER
110940 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
110941 isView = pTab->pSelect!=0;
110942#else
110943# define pTrigger 0
110944# define isView 0
110945#endif
110946 bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
110947#ifdef SQLITE_OMIT_VIEW
110948# undef isView
110949# define isView 0
110950#endif
110951
110952#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
110953 if( !isView ){
110954 pWhere = sqlite3LimitWhere(
110955 pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
110956 );
110957 pOrderBy = 0;
110958 pLimit = 0;
110959 }
110960#endif
110961
110962 /* If pTab is really a view, make sure it has been initialized.
110963 */
110964 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
110965 goto delete_from_cleanup;
110966 }
110967
110968 if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
110969 goto delete_from_cleanup;
110970 }
110971 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
110972 assert( iDb<db->nDb );
110973 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
110974 db->aDb[iDb].zDbSName);
110975 assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
110976 if( rcauth==SQLITE_DENY ){
110977 goto delete_from_cleanup;
110978 }
110979 assert(!isView || pTrigger);
110980
110981 /* Assign cursor numbers to the table and all its indices.
110982 */
110983 assert( pTabList->nSrc==1 );
110984 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
110985 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
110986 pParse->nTab++;
110987 }
110988
110989 /* Start the view context
110990 */
110991 if( isView ){
110992 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
110993 }
110994
110995 /* Begin generating code.
110996 */
110997 v = sqlite3GetVdbe(pParse);
110998 if( v==0 ){
110999 goto delete_from_cleanup;
111000 }
111001 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
111002 sqlite3BeginWriteOperation(pParse, bComplex, iDb);
111003
111004 /* If we are trying to delete from a view, realize that view into
111005 ** an ephemeral table.
111006 */
111007#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
111008 if( isView ){
111009 sqlite3MaterializeView(pParse, pTab,
111010 pWhere, pOrderBy, pLimit, iTabCur
111011 );
111012 iDataCur = iIdxCur = iTabCur;
111013 pOrderBy = 0;
111014 pLimit = 0;
111015 }
111016#endif
111017
111018 /* Resolve the column names in the WHERE clause.
111019 */
111020 memset(&sNC, 0, sizeof(sNC));
111021 sNC.pParse = pParse;
111022 sNC.pSrcList = pTabList;
111023 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
111024 goto delete_from_cleanup;
111025 }
111026
111027 /* Initialize the counter of the number of rows deleted, if
111028 ** we are counting rows.
111029 */
111030 if( (db->flags & SQLITE_CountRows)!=0
111031 && !pParse->nested
111032 && !pParse->pTriggerTab
111033 ){
111034 memCnt = ++pParse->nMem;
111035 sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
111036 }
111037
111038#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
111039 /* Special case: A DELETE without a WHERE clause deletes everything.
111040 ** It is easier just to erase the whole table. Prior to version 3.6.5,
111041 ** this optimization caused the row change count (the value returned by
111042 ** API function sqlite3_count_changes) to be set incorrectly.
111043 **
111044 ** The "rcauth==SQLITE_OK" terms is the
111045 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
111046 ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
111047 ** the truncate optimization is disabled and all rows are deleted
111048 ** individually.
111049 */
111050 if( rcauth==SQLITE_OK
111051 && pWhere==0
111052 && !bComplex
111053 && !IsVirtual(pTab)
111054#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
111055 && db->xPreUpdateCallback==0
111056#endif
111057 ){
111058 assert( !isView );
111059 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
111060 if( HasRowid(pTab) ){
111061 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
111062 pTab->zName, P4_STATIC);
111063 }
111064 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
111065 assert( pIdx->pSchema==pTab->pSchema );
111066 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
111067 }
111068 }else
111069#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
111070 {
111071 u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK|WHERE_SEEK_TABLE;
111072 if( sNC.ncFlags & NC_VarSelect ) bComplex = 1;
111073 wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW);
111074 if( HasRowid(pTab) ){
111075 /* For a rowid table, initialize the RowSet to an empty set */
111076 pPk = 0;
111077 nPk = 1;
111078 iRowSet = ++pParse->nMem;
111079 sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
111080 }else{
111081 /* For a WITHOUT ROWID table, create an ephemeral table used to
111082 ** hold all primary keys for rows to be deleted. */
111083 pPk = sqlite3PrimaryKeyIndex(pTab);
111084 assert( pPk!=0 );
111085 nPk = pPk->nKeyCol;
111086 iPk = pParse->nMem+1;
111087 pParse->nMem += nPk;
111088 iEphCur = pParse->nTab++;
111089 addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
111090 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
111091 }
111092
111093 /* Construct a query to find the rowid or primary key for every row
111094 ** to be deleted, based on the WHERE clause. Set variable eOnePass
111095 ** to indicate the strategy used to implement this delete:
111096 **
111097 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
111098 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
111099 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
111100 */
111101 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
111102 if( pWInfo==0 ) goto delete_from_cleanup;
111103 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
111104 assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
111105 assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
111106 if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
111107
111108 /* Keep track of the number of rows to be deleted */
111109 if( memCnt ){
111110 sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
111111 }
111112
111113 /* Extract the rowid or primary key for the current row */
111114 if( pPk ){
111115 for(i=0; i<nPk; i++){
111116 assert( pPk->aiColumn[i]>=0 );
111117 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
111118 pPk->aiColumn[i], iPk+i);
111119 }
111120 iKey = iPk;
111121 }else{
111122 iKey = ++pParse->nMem;
111123 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
111124 }
111125
111126 if( eOnePass!=ONEPASS_OFF ){
111127 /* For ONEPASS, no need to store the rowid/primary-key. There is only
111128 ** one, so just keep it in its register(s) and fall through to the
111129 ** delete code. */
111130 nKey = nPk; /* OP_Found will use an unpacked key */
111131 aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
111132 if( aToOpen==0 ){
111133 sqlite3WhereEnd(pWInfo);
111134 goto delete_from_cleanup;
111135 }
111136 memset(aToOpen, 1, nIdx+1);
111137 aToOpen[nIdx+1] = 0;
111138 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
111139 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
111140 if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
111141 }else{
111142 if( pPk ){
111143 /* Add the PK key for this row to the temporary table */
111144 iKey = ++pParse->nMem;
111145 nKey = 0; /* Zero tells OP_Found to use a composite key */
111146 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
111147 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
111148 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEphCur, iKey, iPk, nPk);
111149 }else{
111150 /* Add the rowid of the row to be deleted to the RowSet */
111151 nKey = 1; /* OP_DeferredSeek always uses a single rowid */
111152 sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
111153 }
111154 }
111155
111156 /* If this DELETE cannot use the ONEPASS strategy, this is the
111157 ** end of the WHERE loop */
111158 if( eOnePass!=ONEPASS_OFF ){
111159 addrBypass = sqlite3VdbeMakeLabel(v);
111160 }else{
111161 sqlite3WhereEnd(pWInfo);
111162 }
111163
111164 /* Unless this is a view, open cursors for the table we are
111165 ** deleting from and all its indices. If this is a view, then the
111166 ** only effect this statement has is to fire the INSTEAD OF
111167 ** triggers.
111168 */
111169 if( !isView ){
111170 int iAddrOnce = 0;
111171 if( eOnePass==ONEPASS_MULTI ){
111172 iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
111173 }
111174 testcase( IsVirtual(pTab) );
111175 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE,
111176 iTabCur, aToOpen, &iDataCur, &iIdxCur);
111177 assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
111178 assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
111179 if( eOnePass==ONEPASS_MULTI ) sqlite3VdbeJumpHere(v, iAddrOnce);
111180 }
111181
111182 /* Set up a loop over the rowids/primary-keys that were found in the
111183 ** where-clause loop above.
111184 */
111185 if( eOnePass!=ONEPASS_OFF ){
111186 assert( nKey==nPk ); /* OP_Found will use an unpacked key */
111187 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
111188 assert( pPk!=0 || pTab->pSelect!=0 );
111189 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
111190 VdbeCoverage(v);
111191 }
111192 }else if( pPk ){
111193 addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
111194 if( IsVirtual(pTab) ){
111195 sqlite3VdbeAddOp3(v, OP_Column, iEphCur, 0, iKey);
111196 }else{
111197 sqlite3VdbeAddOp2(v, OP_RowData, iEphCur, iKey);
111198 }
111199 assert( nKey==0 ); /* OP_Found will use a composite key */
111200 }else{
111201 addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
111202 VdbeCoverage(v);
111203 assert( nKey==1 );
111204 }
111205
111206 /* Delete the row */
111207#ifndef SQLITE_OMIT_VIRTUALTABLE
111208 if( IsVirtual(pTab) ){
111209 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
111210 sqlite3VtabMakeWritable(pParse, pTab);
111211 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
111212 sqlite3MayAbort(pParse);
111213 if( eOnePass==ONEPASS_SINGLE ){
111214 sqlite3VdbeAddOp1(v, OP_Close, iTabCur);
111215 if( sqlite3IsToplevel(pParse) ){
111216 pParse->isMultiWrite = 0;
111217 }
111218 }
111219 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
111220 sqlite3VdbeChangeP5(v, OE_Abort);
111221 }else
111222#endif
111223 {
111224 int count = (pParse->nested==0); /* True to count changes */
111225 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
111226 iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
111227 }
111228
111229 /* End of the loop over all rowids/primary-keys. */
111230 if( eOnePass!=ONEPASS_OFF ){
111231 sqlite3VdbeResolveLabel(v, addrBypass);
111232 sqlite3WhereEnd(pWInfo);
111233 }else if( pPk ){
111234 sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
111235 sqlite3VdbeJumpHere(v, addrLoop);
111236 }else{
111237 sqlite3VdbeGoto(v, addrLoop);
111238 sqlite3VdbeJumpHere(v, addrLoop);
111239 }
111240 } /* End non-truncate path */
111241
111242 /* Update the sqlite_sequence table by storing the content of the
111243 ** maximum rowid counter values recorded while inserting into
111244 ** autoincrement tables.
111245 */
111246 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
111247 sqlite3AutoincrementEnd(pParse);
111248 }
111249
111250 /* Return the number of rows that were deleted. If this routine is
111251 ** generating code because of a call to sqlite3NestedParse(), do not
111252 ** invoke the callback function.
111253 */
111254 if( memCnt ){
111255 sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
111256 sqlite3VdbeSetNumCols(v, 1);
111257 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
111258 }
111259
111260delete_from_cleanup:
111261 sqlite3AuthContextPop(&sContext);
111262 sqlite3SrcListDelete(db, pTabList);
111263 sqlite3ExprDelete(db, pWhere);
111264#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
111265 sqlite3ExprListDelete(db, pOrderBy);
111266 sqlite3ExprDelete(db, pLimit);
111267#endif
111268 sqlite3DbFree(db, aToOpen);
111269 return;
111270}
111271/* Make sure "isView" and other macros defined above are undefined. Otherwise
111272** they may interfere with compilation of other functions in this file
111273** (or in another file, if this file becomes part of the amalgamation). */
111274#ifdef isView
111275 #undef isView
111276#endif
111277#ifdef pTrigger
111278 #undef pTrigger
111279#endif
111280
111281/*
111282** This routine generates VDBE code that causes a single row of a
111283** single table to be deleted. Both the original table entry and
111284** all indices are removed.
111285**
111286** Preconditions:
111287**
111288** 1. iDataCur is an open cursor on the btree that is the canonical data
111289** store for the table. (This will be either the table itself,
111290** in the case of a rowid table, or the PRIMARY KEY index in the case
111291** of a WITHOUT ROWID table.)
111292**
111293** 2. Read/write cursors for all indices of pTab must be open as
111294** cursor number iIdxCur+i for the i-th index.
111295**
111296** 3. The primary key for the row to be deleted must be stored in a
111297** sequence of nPk memory cells starting at iPk. If nPk==0 that means
111298** that a search record formed from OP_MakeRecord is contained in the
111299** single memory location iPk.
111300**
111301** eMode:
111302** Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
111303** ONEPASS_MULTI. If eMode is not ONEPASS_OFF, then the cursor
111304** iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
111305** then this function must seek iDataCur to the entry identified by iPk
111306** and nPk before reading from it.
111307**
111308** If eMode is ONEPASS_MULTI, then this call is being made as part
111309** of a ONEPASS delete that affects multiple rows. In this case, if
111310** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
111311** iDataCur, then its position should be preserved following the delete
111312** operation. Or, if iIdxNoSeek is not a valid cursor number, the
111313** position of iDataCur should be preserved instead.
111314**
111315** iIdxNoSeek:
111316** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
111317** then it identifies an index cursor (from within array of cursors
111318** starting at iIdxCur) that already points to the index entry to be deleted.
111319** Except, this optimization is disabled if there are BEFORE triggers since
111320** the trigger body might have moved the cursor.
111321*/
111322SQLITE_PRIVATE void sqlite3GenerateRowDelete(
111323 Parse *pParse, /* Parsing context */
111324 Table *pTab, /* Table containing the row to be deleted */
111325 Trigger *pTrigger, /* List of triggers to (potentially) fire */
111326 int iDataCur, /* Cursor from which column data is extracted */
111327 int iIdxCur, /* First index cursor */
111328 int iPk, /* First memory cell containing the PRIMARY KEY */
111329 i16 nPk, /* Number of PRIMARY KEY memory cells */
111330 u8 count, /* If non-zero, increment the row change counter */
111331 u8 onconf, /* Default ON CONFLICT policy for triggers */
111332 u8 eMode, /* ONEPASS_OFF, _SINGLE, or _MULTI. See above */
111333 int iIdxNoSeek /* Cursor number of cursor that does not need seeking */
111334){
111335 Vdbe *v = pParse->pVdbe; /* Vdbe */
111336 int iOld = 0; /* First register in OLD.* array */
111337 int iLabel; /* Label resolved to end of generated code */
111338 u8 opSeek; /* Seek opcode */
111339
111340 /* Vdbe is guaranteed to have been allocated by this stage. */
111341 assert( v );
111342 VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
111343 iDataCur, iIdxCur, iPk, (int)nPk));
111344
111345 /* Seek cursor iCur to the row to delete. If this row no longer exists
111346 ** (this can happen if a trigger program has already deleted it), do
111347 ** not attempt to delete it or fire any DELETE triggers. */
111348 iLabel = sqlite3VdbeMakeLabel(v);
111349 opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
111350 if( eMode==ONEPASS_OFF ){
111351 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
111352 VdbeCoverageIf(v, opSeek==OP_NotExists);
111353 VdbeCoverageIf(v, opSeek==OP_NotFound);
111354 }
111355
111356 /* If there are any triggers to fire, allocate a range of registers to
111357 ** use for the old.* references in the triggers. */
111358 if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
111359 u32 mask; /* Mask of OLD.* columns in use */
111360 int iCol; /* Iterator used while populating OLD.* */
111361 int addrStart; /* Start of BEFORE trigger programs */
111362
111363 /* TODO: Could use temporary registers here. Also could attempt to
111364 ** avoid copying the contents of the rowid register. */
111365 mask = sqlite3TriggerColmask(
111366 pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
111367 );
111368 mask |= sqlite3FkOldmask(pParse, pTab);
111369 iOld = pParse->nMem+1;
111370 pParse->nMem += (1 + pTab->nCol);
111371
111372 /* Populate the OLD.* pseudo-table register array. These values will be
111373 ** used by any BEFORE and AFTER triggers that exist. */
111374 sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld);
111375 for(iCol=0; iCol<pTab->nCol; iCol++){
111376 testcase( mask!=0xffffffff && iCol==31 );
111377 testcase( mask!=0xffffffff && iCol==32 );
111378 if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
111379 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
111380 }
111381 }
111382
111383 /* Invoke BEFORE DELETE trigger programs. */
111384 addrStart = sqlite3VdbeCurrentAddr(v);
111385 sqlite3CodeRowTrigger(pParse, pTrigger,
111386 TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
111387 );
111388
111389 /* If any BEFORE triggers were coded, then seek the cursor to the
111390 ** row to be deleted again. It may be that the BEFORE triggers moved
111391 ** the cursor or already deleted the row that the cursor was
111392 ** pointing to.
111393 **
111394 ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
111395 ** may have moved that cursor.
111396 */
111397 if( addrStart<sqlite3VdbeCurrentAddr(v) ){
111398 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
111399 VdbeCoverageIf(v, opSeek==OP_NotExists);
111400 VdbeCoverageIf(v, opSeek==OP_NotFound);
111401 testcase( iIdxNoSeek>=0 );
111402 iIdxNoSeek = -1;
111403 }
111404
111405 /* Do FK processing. This call checks that any FK constraints that
111406 ** refer to this table (i.e. constraints attached to other tables)
111407 ** are not violated by deleting this row. */
111408 sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
111409 }
111410
111411 /* Delete the index and table entries. Skip this step if pTab is really
111412 ** a view (in which case the only effect of the DELETE statement is to
111413 ** fire the INSTEAD OF triggers).
111414 **
111415 ** If variable 'count' is non-zero, then this OP_Delete instruction should
111416 ** invoke the update-hook. The pre-update-hook, on the other hand should
111417 ** be invoked unless table pTab is a system table. The difference is that
111418 ** the update-hook is not invoked for rows removed by REPLACE, but the
111419 ** pre-update-hook is.
111420 */
111421 if( pTab->pSelect==0 ){
111422 u8 p5 = 0;
111423 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
111424 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
111425 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
111426 sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
111427 }
111428 if( eMode!=ONEPASS_OFF ){
111429 sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
111430 }
111431 if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
111432 sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
111433 }
111434 if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
111435 sqlite3VdbeChangeP5(v, p5);
111436 }
111437
111438 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
111439 ** handle rows (possibly in other tables) that refer via a foreign key
111440 ** to the row just deleted. */
111441 sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
111442
111443 /* Invoke AFTER DELETE trigger programs. */
111444 sqlite3CodeRowTrigger(pParse, pTrigger,
111445 TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
111446 );
111447
111448 /* Jump here if the row had already been deleted before any BEFORE
111449 ** trigger programs were invoked. Or if a trigger program throws a
111450 ** RAISE(IGNORE) exception. */
111451 sqlite3VdbeResolveLabel(v, iLabel);
111452 VdbeModuleComment((v, "END: GenRowDel()"));
111453}
111454
111455/*
111456** This routine generates VDBE code that causes the deletion of all
111457** index entries associated with a single row of a single table, pTab
111458**
111459** Preconditions:
111460**
111461** 1. A read/write cursor "iDataCur" must be open on the canonical storage
111462** btree for the table pTab. (This will be either the table itself
111463** for rowid tables or to the primary key index for WITHOUT ROWID
111464** tables.)
111465**
111466** 2. Read/write cursors for all indices of pTab must be open as
111467** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
111468** index is the 0-th index.)
111469**
111470** 3. The "iDataCur" cursor must be already be positioned on the row
111471** that is to be deleted.
111472*/
111473SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
111474 Parse *pParse, /* Parsing and code generating context */
111475 Table *pTab, /* Table containing the row to be deleted */
111476 int iDataCur, /* Cursor of table holding data. */
111477 int iIdxCur, /* First index cursor */
111478 int *aRegIdx, /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
111479 int iIdxNoSeek /* Do not delete from this cursor */
111480){
111481 int i; /* Index loop counter */
111482 int r1 = -1; /* Register holding an index key */
111483 int iPartIdxLabel; /* Jump destination for skipping partial index entries */
111484 Index *pIdx; /* Current index */
111485 Index *pPrior = 0; /* Prior index */
111486 Vdbe *v; /* The prepared statement under construction */
111487 Index *pPk; /* PRIMARY KEY index, or NULL for rowid tables */
111488
111489 v = pParse->pVdbe;
111490 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
111491 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
111492 assert( iIdxCur+i!=iDataCur || pPk==pIdx );
111493 if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
111494 if( pIdx==pPk ) continue;
111495 if( iIdxCur+i==iIdxNoSeek ) continue;
111496 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
111497 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
111498 &iPartIdxLabel, pPrior, r1);
111499 sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
111500 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
111501 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
111502 pPrior = pIdx;
111503 }
111504}
111505
111506/*
111507** Generate code that will assemble an index key and stores it in register
111508** regOut. The key with be for index pIdx which is an index on pTab.
111509** iCur is the index of a cursor open on the pTab table and pointing to
111510** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then
111511** iCur must be the cursor of the PRIMARY KEY index.
111512**
111513** Return a register number which is the first in a block of
111514** registers that holds the elements of the index key. The
111515** block of registers has already been deallocated by the time
111516** this routine returns.
111517**
111518** If *piPartIdxLabel is not NULL, fill it in with a label and jump
111519** to that label if pIdx is a partial index that should be skipped.
111520** The label should be resolved using sqlite3ResolvePartIdxLabel().
111521** A partial index should be skipped if its WHERE clause evaluates
111522** to false or null. If pIdx is not a partial index, *piPartIdxLabel
111523** will be set to zero which is an empty label that is ignored by
111524** sqlite3ResolvePartIdxLabel().
111525**
111526** The pPrior and regPrior parameters are used to implement a cache to
111527** avoid unnecessary register loads. If pPrior is not NULL, then it is
111528** a pointer to a different index for which an index key has just been
111529** computed into register regPrior. If the current pIdx index is generating
111530** its key into the same sequence of registers and if pPrior and pIdx share
111531** a column in common, then the register corresponding to that column already
111532** holds the correct value and the loading of that register is skipped.
111533** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
111534** on a table with multiple indices, and especially with the ROWID or
111535** PRIMARY KEY columns of the index.
111536*/
111537SQLITE_PRIVATE int sqlite3GenerateIndexKey(
111538 Parse *pParse, /* Parsing context */
111539 Index *pIdx, /* The index for which to generate a key */
111540 int iDataCur, /* Cursor number from which to take column data */
111541 int regOut, /* Put the new key into this register if not 0 */
111542 int prefixOnly, /* Compute only a unique prefix of the key */
111543 int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
111544 Index *pPrior, /* Previously generated index key */
111545 int regPrior /* Register holding previous generated key */
111546){
111547 Vdbe *v = pParse->pVdbe;
111548 int j;
111549 int regBase;
111550 int nCol;
111551
111552 if( piPartIdxLabel ){
111553 if( pIdx->pPartIdxWhere ){
111554 *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
111555 pParse->iSelfTab = iDataCur + 1;
111556 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
111557 SQLITE_JUMPIFNULL);
111558 pParse->iSelfTab = 0;
111559 }else{
111560 *piPartIdxLabel = 0;
111561 }
111562 }
111563 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
111564 regBase = sqlite3GetTempRange(pParse, nCol);
111565 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
111566 for(j=0; j<nCol; j++){
111567 if( pPrior
111568 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
111569 && pPrior->aiColumn[j]!=XN_EXPR
111570 ){
111571 /* This column was already computed by the previous index */
111572 continue;
111573 }
111574 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
111575 /* If the column affinity is REAL but the number is an integer, then it
111576 ** might be stored in the table as an integer (using a compact
111577 ** representation) then converted to REAL by an OP_RealAffinity opcode.
111578 ** But we are getting ready to store this value back into an index, where
111579 ** it should be converted by to INTEGER again. So omit the OP_RealAffinity
111580 ** opcode if it is present */
111581 sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
111582 }
111583 if( regOut ){
111584 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
111585 if( pIdx->pTable->pSelect ){
111586 const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
111587 sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
111588 }
111589 }
111590 sqlite3ReleaseTempRange(pParse, regBase, nCol);
111591 return regBase;
111592}
111593
111594/*
111595** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
111596** because it was a partial index, then this routine should be called to
111597** resolve that label.
111598*/
111599SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
111600 if( iLabel ){
111601 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
111602 }
111603}
111604
111605/************** End of delete.c **********************************************/
111606/************** Begin file func.c ********************************************/
111607/*
111608** 2002 February 23
111609**
111610** The author disclaims copyright to this source code. In place of
111611** a legal notice, here is a blessing:
111612**
111613** May you do good and not evil.
111614** May you find forgiveness for yourself and forgive others.
111615** May you share freely, never taking more than you give.
111616**
111617*************************************************************************
111618** This file contains the C-language implementations for many of the SQL
111619** functions of SQLite. (Some function, and in particular the date and
111620** time functions, are implemented separately.)
111621*/
111622/* #include "sqliteInt.h" */
111623/* #include <stdlib.h> */
111624/* #include <assert.h> */
111625/* #include "vdbeInt.h" */
111626
111627/*
111628** Return the collating function associated with a function.
111629*/
111630static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
111631 VdbeOp *pOp;
111632 assert( context->pVdbe!=0 );
111633 pOp = &context->pVdbe->aOp[context->iOp-1];
111634 assert( pOp->opcode==OP_CollSeq );
111635 assert( pOp->p4type==P4_COLLSEQ );
111636 return pOp->p4.pColl;
111637}
111638
111639/*
111640** Indicate that the accumulator load should be skipped on this
111641** iteration of the aggregate loop.
111642*/
111643static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
111644 assert( context->isError<=0 );
111645 context->isError = -1;
111646 context->skipFlag = 1;
111647}
111648
111649/*
111650** Implementation of the non-aggregate min() and max() functions
111651*/
111652static void minmaxFunc(
111653 sqlite3_context *context,
111654 int argc,
111655 sqlite3_value **argv
111656){
111657 int i;
111658 int mask; /* 0 for min() or 0xffffffff for max() */
111659 int iBest;
111660 CollSeq *pColl;
111661
111662 assert( argc>1 );
111663 mask = sqlite3_user_data(context)==0 ? 0 : -1;
111664 pColl = sqlite3GetFuncCollSeq(context);
111665 assert( pColl );
111666 assert( mask==-1 || mask==0 );
111667 iBest = 0;
111668 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
111669 for(i=1; i<argc; i++){
111670 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
111671 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
111672 testcase( mask==0 );
111673 iBest = i;
111674 }
111675 }
111676 sqlite3_result_value(context, argv[iBest]);
111677}
111678
111679/*
111680** Return the type of the argument.
111681*/
111682static void typeofFunc(
111683 sqlite3_context *context,
111684 int NotUsed,
111685 sqlite3_value **argv
111686){
111687 static const char *azType[] = { "integer", "real", "text", "blob", "null" };
111688 int i = sqlite3_value_type(argv[0]) - 1;
111689 UNUSED_PARAMETER(NotUsed);
111690 assert( i>=0 && i<ArraySize(azType) );
111691 assert( SQLITE_INTEGER==1 );
111692 assert( SQLITE_FLOAT==2 );
111693 assert( SQLITE_TEXT==3 );
111694 assert( SQLITE_BLOB==4 );
111695 assert( SQLITE_NULL==5 );
111696 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
111697 ** the datatype code for the initial datatype of the sqlite3_value object
111698 ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT,
111699 ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */
111700 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
111701}
111702
111703
111704/*
111705** Implementation of the length() function
111706*/
111707static void lengthFunc(
111708 sqlite3_context *context,
111709 int argc,
111710 sqlite3_value **argv
111711){
111712 assert( argc==1 );
111713 UNUSED_PARAMETER(argc);
111714 switch( sqlite3_value_type(argv[0]) ){
111715 case SQLITE_BLOB:
111716 case SQLITE_INTEGER:
111717 case SQLITE_FLOAT: {
111718 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
111719 break;
111720 }
111721 case SQLITE_TEXT: {
111722 const unsigned char *z = sqlite3_value_text(argv[0]);
111723 const unsigned char *z0;
111724 unsigned char c;
111725 if( z==0 ) return;
111726 z0 = z;
111727 while( (c = *z)!=0 ){
111728 z++;
111729 if( c>=0xc0 ){
111730 while( (*z & 0xc0)==0x80 ){ z++; z0++; }
111731 }
111732 }
111733 sqlite3_result_int(context, (int)(z-z0));
111734 break;
111735 }
111736 default: {
111737 sqlite3_result_null(context);
111738 break;
111739 }
111740 }
111741}
111742
111743/*
111744** Implementation of the abs() function.
111745**
111746** IMP: R-23979-26855 The abs(X) function returns the absolute value of
111747** the numeric argument X.
111748*/
111749static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
111750 assert( argc==1 );
111751 UNUSED_PARAMETER(argc);
111752 switch( sqlite3_value_type(argv[0]) ){
111753 case SQLITE_INTEGER: {
111754 i64 iVal = sqlite3_value_int64(argv[0]);
111755 if( iVal<0 ){
111756 if( iVal==SMALLEST_INT64 ){
111757 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
111758 ** then abs(X) throws an integer overflow error since there is no
111759 ** equivalent positive 64-bit two complement value. */
111760 sqlite3_result_error(context, "integer overflow", -1);
111761 return;
111762 }
111763 iVal = -iVal;
111764 }
111765 sqlite3_result_int64(context, iVal);
111766 break;
111767 }
111768 case SQLITE_NULL: {
111769 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
111770 sqlite3_result_null(context);
111771 break;
111772 }
111773 default: {
111774 /* Because sqlite3_value_double() returns 0.0 if the argument is not
111775 ** something that can be converted into a number, we have:
111776 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
111777 ** that cannot be converted to a numeric value.
111778 */
111779 double rVal = sqlite3_value_double(argv[0]);
111780 if( rVal<0 ) rVal = -rVal;
111781 sqlite3_result_double(context, rVal);
111782 break;
111783 }
111784 }
111785}
111786
111787/*
111788** Implementation of the instr() function.
111789**
111790** instr(haystack,needle) finds the first occurrence of needle
111791** in haystack and returns the number of previous characters plus 1,
111792** or 0 if needle does not occur within haystack.
111793**
111794** If both haystack and needle are BLOBs, then the result is one more than
111795** the number of bytes in haystack prior to the first occurrence of needle,
111796** or 0 if needle never occurs in haystack.
111797*/
111798static void instrFunc(
111799 sqlite3_context *context,
111800 int argc,
111801 sqlite3_value **argv
111802){
111803 const unsigned char *zHaystack;
111804 const unsigned char *zNeedle;
111805 int nHaystack;
111806 int nNeedle;
111807 int typeHaystack, typeNeedle;
111808 int N = 1;
111809 int isText;
111810
111811 UNUSED_PARAMETER(argc);
111812 typeHaystack = sqlite3_value_type(argv[0]);
111813 typeNeedle = sqlite3_value_type(argv[1]);
111814 if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
111815 nHaystack = sqlite3_value_bytes(argv[0]);
111816 nNeedle = sqlite3_value_bytes(argv[1]);
111817 if( nNeedle>0 ){
111818 if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
111819 zHaystack = sqlite3_value_blob(argv[0]);
111820 zNeedle = sqlite3_value_blob(argv[1]);
111821 isText = 0;
111822 }else{
111823 zHaystack = sqlite3_value_text(argv[0]);
111824 zNeedle = sqlite3_value_text(argv[1]);
111825 isText = 1;
111826 }
111827 if( zNeedle==0 || (nHaystack && zHaystack==0) ) return;
111828 while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
111829 N++;
111830 do{
111831 nHaystack--;
111832 zHaystack++;
111833 }while( isText && (zHaystack[0]&0xc0)==0x80 );
111834 }
111835 if( nNeedle>nHaystack ) N = 0;
111836 }
111837 sqlite3_result_int(context, N);
111838}
111839
111840/*
111841** Implementation of the printf() function.
111842*/
111843static void printfFunc(
111844 sqlite3_context *context,
111845 int argc,
111846 sqlite3_value **argv
111847){
111848 PrintfArguments x;
111849 StrAccum str;
111850 const char *zFormat;
111851 int n;
111852 sqlite3 *db = sqlite3_context_db_handle(context);
111853
111854 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
111855 x.nArg = argc-1;
111856 x.nUsed = 0;
111857 x.apArg = argv+1;
111858 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
111859 str.printfFlags = SQLITE_PRINTF_SQLFUNC;
111860 sqlite3_str_appendf(&str, zFormat, &x);
111861 n = str.nChar;
111862 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
111863 SQLITE_DYNAMIC);
111864 }
111865}
111866
111867/*
111868** Implementation of the substr() function.
111869**
111870** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
111871** p1 is 1-indexed. So substr(x,1,1) returns the first character
111872** of x. If x is text, then we actually count UTF-8 characters.
111873** If x is a blob, then we count bytes.
111874**
111875** If p1 is negative, then we begin abs(p1) from the end of x[].
111876**
111877** If p2 is negative, return the p2 characters preceding p1.
111878*/
111879static void substrFunc(
111880 sqlite3_context *context,
111881 int argc,
111882 sqlite3_value **argv
111883){
111884 const unsigned char *z;
111885 const unsigned char *z2;
111886 int len;
111887 int p0type;
111888 i64 p1, p2;
111889 int negP2 = 0;
111890
111891 assert( argc==3 || argc==2 );
111892 if( sqlite3_value_type(argv[1])==SQLITE_NULL
111893 || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
111894 ){
111895 return;
111896 }
111897 p0type = sqlite3_value_type(argv[0]);
111898 p1 = sqlite3_value_int(argv[1]);
111899 if( p0type==SQLITE_BLOB ){
111900 len = sqlite3_value_bytes(argv[0]);
111901 z = sqlite3_value_blob(argv[0]);
111902 if( z==0 ) return;
111903 assert( len==sqlite3_value_bytes(argv[0]) );
111904 }else{
111905 z = sqlite3_value_text(argv[0]);
111906 if( z==0 ) return;
111907 len = 0;
111908 if( p1<0 ){
111909 for(z2=z; *z2; len++){
111910 SQLITE_SKIP_UTF8(z2);
111911 }
111912 }
111913 }
111914#ifdef SQLITE_SUBSTR_COMPATIBILITY
111915 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
111916 ** as substr(X,1,N) - it returns the first N characters of X. This
111917 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
111918 ** from 2009-02-02 for compatibility of applications that exploited the
111919 ** old buggy behavior. */
111920 if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
111921#endif
111922 if( argc==3 ){
111923 p2 = sqlite3_value_int(argv[2]);
111924 if( p2<0 ){
111925 p2 = -p2;
111926 negP2 = 1;
111927 }
111928 }else{
111929 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
111930 }
111931 if( p1<0 ){
111932 p1 += len;
111933 if( p1<0 ){
111934 p2 += p1;
111935 if( p2<0 ) p2 = 0;
111936 p1 = 0;
111937 }
111938 }else if( p1>0 ){
111939 p1--;
111940 }else if( p2>0 ){
111941 p2--;
111942 }
111943 if( negP2 ){
111944 p1 -= p2;
111945 if( p1<0 ){
111946 p2 += p1;
111947 p1 = 0;
111948 }
111949 }
111950 assert( p1>=0 && p2>=0 );
111951 if( p0type!=SQLITE_BLOB ){
111952 while( *z && p1 ){
111953 SQLITE_SKIP_UTF8(z);
111954 p1--;
111955 }
111956 for(z2=z; *z2 && p2; p2--){
111957 SQLITE_SKIP_UTF8(z2);
111958 }
111959 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
111960 SQLITE_UTF8);
111961 }else{
111962 if( p1+p2>len ){
111963 p2 = len-p1;
111964 if( p2<0 ) p2 = 0;
111965 }
111966 sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT);
111967 }
111968}
111969
111970/*
111971** Implementation of the round() function
111972*/
111973#ifndef SQLITE_OMIT_FLOATING_POINT
111974static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
111975 int n = 0;
111976 double r;
111977 char *zBuf;
111978 assert( argc==1 || argc==2 );
111979 if( argc==2 ){
111980 if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
111981 n = sqlite3_value_int(argv[1]);
111982 if( n>30 ) n = 30;
111983 if( n<0 ) n = 0;
111984 }
111985 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
111986 r = sqlite3_value_double(argv[0]);
111987 /* If Y==0 and X will fit in a 64-bit int,
111988 ** handle the rounding directly,
111989 ** otherwise use printf.
111990 */
111991 if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
111992 r = (double)((sqlite_int64)(r+0.5));
111993 }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
111994 r = -(double)((sqlite_int64)((-r)+0.5));
111995 }else{
111996 zBuf = sqlite3_mprintf("%.*f",n,r);
111997 if( zBuf==0 ){
111998 sqlite3_result_error_nomem(context);
111999 return;
112000 }
112001 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
112002 sqlite3_free(zBuf);
112003 }
112004 sqlite3_result_double(context, r);
112005}
112006#endif
112007
112008/*
112009** Allocate nByte bytes of space using sqlite3Malloc(). If the
112010** allocation fails, call sqlite3_result_error_nomem() to notify
112011** the database handle that malloc() has failed and return NULL.
112012** If nByte is larger than the maximum string or blob length, then
112013** raise an SQLITE_TOOBIG exception and return NULL.
112014*/
112015static void *contextMalloc(sqlite3_context *context, i64 nByte){
112016 char *z;
112017 sqlite3 *db = sqlite3_context_db_handle(context);
112018 assert( nByte>0 );
112019 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
112020 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
112021 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
112022 sqlite3_result_error_toobig(context);
112023 z = 0;
112024 }else{
112025 z = sqlite3Malloc(nByte);
112026 if( !z ){
112027 sqlite3_result_error_nomem(context);
112028 }
112029 }
112030 return z;
112031}
112032
112033/*
112034** Implementation of the upper() and lower() SQL functions.
112035*/
112036static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
112037 char *z1;
112038 const char *z2;
112039 int i, n;
112040 UNUSED_PARAMETER(argc);
112041 z2 = (char*)sqlite3_value_text(argv[0]);
112042 n = sqlite3_value_bytes(argv[0]);
112043 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
112044 assert( z2==(char*)sqlite3_value_text(argv[0]) );
112045 if( z2 ){
112046 z1 = contextMalloc(context, ((i64)n)+1);
112047 if( z1 ){
112048 for(i=0; i<n; i++){
112049 z1[i] = (char)sqlite3Toupper(z2[i]);
112050 }
112051 sqlite3_result_text(context, z1, n, sqlite3_free);
112052 }
112053 }
112054}
112055static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
112056 char *z1;
112057 const char *z2;
112058 int i, n;
112059 UNUSED_PARAMETER(argc);
112060 z2 = (char*)sqlite3_value_text(argv[0]);
112061 n = sqlite3_value_bytes(argv[0]);
112062 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
112063 assert( z2==(char*)sqlite3_value_text(argv[0]) );
112064 if( z2 ){
112065 z1 = contextMalloc(context, ((i64)n)+1);
112066 if( z1 ){
112067 for(i=0; i<n; i++){
112068 z1[i] = sqlite3Tolower(z2[i]);
112069 }
112070 sqlite3_result_text(context, z1, n, sqlite3_free);
112071 }
112072 }
112073}
112074
112075/*
112076** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
112077** as VDBE code so that unused argument values do not have to be computed.
112078** However, we still need some kind of function implementation for this
112079** routines in the function table. The noopFunc macro provides this.
112080** noopFunc will never be called so it doesn't matter what the implementation
112081** is. We might as well use the "version()" function as a substitute.
112082*/
112083#define noopFunc versionFunc /* Substitute function - never called */
112084
112085/*
112086** Implementation of random(). Return a random integer.
112087*/
112088static void randomFunc(
112089 sqlite3_context *context,
112090 int NotUsed,
112091 sqlite3_value **NotUsed2
112092){
112093 sqlite_int64 r;
112094 UNUSED_PARAMETER2(NotUsed, NotUsed2);
112095 sqlite3_randomness(sizeof(r), &r);
112096 if( r<0 ){
112097 /* We need to prevent a random number of 0x8000000000000000
112098 ** (or -9223372036854775808) since when you do abs() of that
112099 ** number of you get the same value back again. To do this
112100 ** in a way that is testable, mask the sign bit off of negative
112101 ** values, resulting in a positive value. Then take the
112102 ** 2s complement of that positive value. The end result can
112103 ** therefore be no less than -9223372036854775807.
112104 */
112105 r = -(r & LARGEST_INT64);
112106 }
112107 sqlite3_result_int64(context, r);
112108}
112109
112110/*
112111** Implementation of randomblob(N). Return a random blob
112112** that is N bytes long.
112113*/
112114static void randomBlob(
112115 sqlite3_context *context,
112116 int argc,
112117 sqlite3_value **argv
112118){
112119 int n;
112120 unsigned char *p;
112121 assert( argc==1 );
112122 UNUSED_PARAMETER(argc);
112123 n = sqlite3_value_int(argv[0]);
112124 if( n<1 ){
112125 n = 1;
112126 }
112127 p = contextMalloc(context, n);
112128 if( p ){
112129 sqlite3_randomness(n, p);
112130 sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
112131 }
112132}
112133
112134/*
112135** Implementation of the last_insert_rowid() SQL function. The return
112136** value is the same as the sqlite3_last_insert_rowid() API function.
112137*/
112138static void last_insert_rowid(
112139 sqlite3_context *context,
112140 int NotUsed,
112141 sqlite3_value **NotUsed2
112142){
112143 sqlite3 *db = sqlite3_context_db_handle(context);
112144 UNUSED_PARAMETER2(NotUsed, NotUsed2);
112145 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
112146 ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
112147 ** function. */
112148 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
112149}
112150
112151/*
112152** Implementation of the changes() SQL function.
112153**
112154** IMP: R-62073-11209 The changes() SQL function is a wrapper
112155** around the sqlite3_changes() C/C++ function and hence follows the same
112156** rules for counting changes.
112157*/
112158static void changes(
112159 sqlite3_context *context,
112160 int NotUsed,
112161 sqlite3_value **NotUsed2
112162){
112163 sqlite3 *db = sqlite3_context_db_handle(context);
112164 UNUSED_PARAMETER2(NotUsed, NotUsed2);
112165 sqlite3_result_int(context, sqlite3_changes(db));
112166}
112167
112168/*
112169** Implementation of the total_changes() SQL function. The return value is
112170** the same as the sqlite3_total_changes() API function.
112171*/
112172static void total_changes(
112173 sqlite3_context *context,
112174 int NotUsed,
112175 sqlite3_value **NotUsed2
112176){
112177 sqlite3 *db = sqlite3_context_db_handle(context);
112178 UNUSED_PARAMETER2(NotUsed, NotUsed2);
112179 /* IMP: R-52756-41993 This function is a wrapper around the
112180 ** sqlite3_total_changes() C/C++ interface. */
112181 sqlite3_result_int(context, sqlite3_total_changes(db));
112182}
112183
112184/*
112185** A structure defining how to do GLOB-style comparisons.
112186*/
112187struct compareInfo {
112188 u8 matchAll; /* "*" or "%" */
112189 u8 matchOne; /* "?" or "_" */
112190 u8 matchSet; /* "[" or 0 */
112191 u8 noCase; /* true to ignore case differences */
112192};
112193
112194/*
112195** For LIKE and GLOB matching on EBCDIC machines, assume that every
112196** character is exactly one byte in size. Also, provde the Utf8Read()
112197** macro for fast reading of the next character in the common case where
112198** the next character is ASCII.
112199*/
112200#if defined(SQLITE_EBCDIC)
112201# define sqlite3Utf8Read(A) (*((*A)++))
112202# define Utf8Read(A) (*(A++))
112203#else
112204# define Utf8Read(A) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
112205#endif
112206
112207static const struct compareInfo globInfo = { '*', '?', '[', 0 };
112208/* The correct SQL-92 behavior is for the LIKE operator to ignore
112209** case. Thus 'a' LIKE 'A' would be true. */
112210static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
112211/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
112212** is case sensitive causing 'a' LIKE 'A' to be false */
112213static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
112214
112215/*
112216** Possible error returns from patternMatch()
112217*/
112218#define SQLITE_MATCH 0
112219#define SQLITE_NOMATCH 1
112220#define SQLITE_NOWILDCARDMATCH 2
112221
112222/*
112223** Compare two UTF-8 strings for equality where the first string is
112224** a GLOB or LIKE expression. Return values:
112225**
112226** SQLITE_MATCH: Match
112227** SQLITE_NOMATCH: No match
112228** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards.
112229**
112230** Globbing rules:
112231**
112232** '*' Matches any sequence of zero or more characters.
112233**
112234** '?' Matches exactly one character.
112235**
112236** [...] Matches one character from the enclosed list of
112237** characters.
112238**
112239** [^...] Matches one character not in the enclosed list.
112240**
112241** With the [...] and [^...] matching, a ']' character can be included
112242** in the list by making it the first character after '[' or '^'. A
112243** range of characters can be specified using '-'. Example:
112244** "[a-z]" matches any single lower-case letter. To match a '-', make
112245** it the last character in the list.
112246**
112247** Like matching rules:
112248**
112249** '%' Matches any sequence of zero or more characters
112250**
112251*** '_' Matches any one character
112252**
112253** Ec Where E is the "esc" character and c is any other
112254** character, including '%', '_', and esc, match exactly c.
112255**
112256** The comments within this routine usually assume glob matching.
112257**
112258** This routine is usually quick, but can be N**2 in the worst case.
112259*/
112260static int patternCompare(
112261 const u8 *zPattern, /* The glob pattern */
112262 const u8 *zString, /* The string to compare against the glob */
112263 const struct compareInfo *pInfo, /* Information about how to do the compare */
112264 u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */
112265){
112266 u32 c, c2; /* Next pattern and input string chars */
112267 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
112268 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
112269 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
112270 const u8 *zEscaped = 0; /* One past the last escaped input char */
112271
112272 while( (c = Utf8Read(zPattern))!=0 ){
112273 if( c==matchAll ){ /* Match "*" */
112274 /* Skip over multiple "*" characters in the pattern. If there
112275 ** are also "?" characters, skip those as well, but consume a
112276 ** single character of the input string for each "?" skipped */
112277 while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){
112278 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
112279 return SQLITE_NOWILDCARDMATCH;
112280 }
112281 }
112282 if( c==0 ){
112283 return SQLITE_MATCH; /* "*" at the end of the pattern matches */
112284 }else if( c==matchOther ){
112285 if( pInfo->matchSet==0 ){
112286 c = sqlite3Utf8Read(&zPattern);
112287 if( c==0 ) return SQLITE_NOWILDCARDMATCH;
112288 }else{
112289 /* "[...]" immediately follows the "*". We have to do a slow
112290 ** recursive search in this case, but it is an unusual case. */
112291 assert( matchOther<0x80 ); /* '[' is a single-byte character */
112292 while( *zString ){
112293 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
112294 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
112295 SQLITE_SKIP_UTF8(zString);
112296 }
112297 return SQLITE_NOWILDCARDMATCH;
112298 }
112299 }
112300
112301 /* At this point variable c contains the first character of the
112302 ** pattern string past the "*". Search in the input string for the
112303 ** first matching character and recursively continue the match from
112304 ** that point.
112305 **
112306 ** For a case-insensitive search, set variable cx to be the same as
112307 ** c but in the other case and search the input string for either
112308 ** c or cx.
112309 */
112310 if( c<=0x80 ){
112311 char zStop[3];
112312 int bMatch;
112313 if( noCase ){
112314 zStop[0] = sqlite3Toupper(c);
112315 zStop[1] = sqlite3Tolower(c);
112316 zStop[2] = 0;
112317 }else{
112318 zStop[0] = c;
112319 zStop[1] = 0;
112320 }
112321 while(1){
112322 zString += strcspn((const char*)zString, zStop);
112323 if( zString[0]==0 ) break;
112324 zString++;
112325 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
112326 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
112327 }
112328 }else{
112329 int bMatch;
112330 while( (c2 = Utf8Read(zString))!=0 ){
112331 if( c2!=c ) continue;
112332 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
112333 if( bMatch!=SQLITE_NOMATCH ) return bMatch;
112334 }
112335 }
112336 return SQLITE_NOWILDCARDMATCH;
112337 }
112338 if( c==matchOther ){
112339 if( pInfo->matchSet==0 ){
112340 c = sqlite3Utf8Read(&zPattern);
112341 if( c==0 ) return SQLITE_NOMATCH;
112342 zEscaped = zPattern;
112343 }else{
112344 u32 prior_c = 0;
112345 int seen = 0;
112346 int invert = 0;
112347 c = sqlite3Utf8Read(&zString);
112348 if( c==0 ) return SQLITE_NOMATCH;
112349 c2 = sqlite3Utf8Read(&zPattern);
112350 if( c2=='^' ){
112351 invert = 1;
112352 c2 = sqlite3Utf8Read(&zPattern);
112353 }
112354 if( c2==']' ){
112355 if( c==']' ) seen = 1;
112356 c2 = sqlite3Utf8Read(&zPattern);
112357 }
112358 while( c2 && c2!=']' ){
112359 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
112360 c2 = sqlite3Utf8Read(&zPattern);
112361 if( c>=prior_c && c<=c2 ) seen = 1;
112362 prior_c = 0;
112363 }else{
112364 if( c==c2 ){
112365 seen = 1;
112366 }
112367 prior_c = c2;
112368 }
112369 c2 = sqlite3Utf8Read(&zPattern);
112370 }
112371 if( c2==0 || (seen ^ invert)==0 ){
112372 return SQLITE_NOMATCH;
112373 }
112374 continue;
112375 }
112376 }
112377 c2 = Utf8Read(zString);
112378 if( c==c2 ) continue;
112379 if( noCase && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){
112380 continue;
112381 }
112382 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
112383 return SQLITE_NOMATCH;
112384 }
112385 return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH;
112386}
112387
112388/*
112389** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
112390** non-zero if there is no match.
112391*/
112392SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
112393 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
112394}
112395
112396/*
112397** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
112398** a miss - like strcmp().
112399*/
112400SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
112401 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
112402}
112403
112404/*
112405** Count the number of times that the LIKE operator (or GLOB which is
112406** just a variation of LIKE) gets called. This is used for testing
112407** only.
112408*/
112409#ifdef SQLITE_TEST
112410SQLITE_API int sqlite3_like_count = 0;
112411#endif
112412
112413
112414/*
112415** Implementation of the like() SQL function. This function implements
112416** the build-in LIKE operator. The first argument to the function is the
112417** pattern and the second argument is the string. So, the SQL statements:
112418**
112419** A LIKE B
112420**
112421** is implemented as like(B,A).
112422**
112423** This same function (with a different compareInfo structure) computes
112424** the GLOB operator.
112425*/
112426static void likeFunc(
112427 sqlite3_context *context,
112428 int argc,
112429 sqlite3_value **argv
112430){
112431 const unsigned char *zA, *zB;
112432 u32 escape;
112433 int nPat;
112434 sqlite3 *db = sqlite3_context_db_handle(context);
112435 struct compareInfo *pInfo = sqlite3_user_data(context);
112436
112437#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
112438 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
112439 || sqlite3_value_type(argv[1])==SQLITE_BLOB
112440 ){
112441#ifdef SQLITE_TEST
112442 sqlite3_like_count++;
112443#endif
112444 sqlite3_result_int(context, 0);
112445 return;
112446 }
112447#endif
112448 zB = sqlite3_value_text(argv[0]);
112449 zA = sqlite3_value_text(argv[1]);
112450
112451 /* Limit the length of the LIKE or GLOB pattern to avoid problems
112452 ** of deep recursion and N*N behavior in patternCompare().
112453 */
112454 nPat = sqlite3_value_bytes(argv[0]);
112455 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
112456 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
112457 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
112458 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
112459 return;
112460 }
112461 assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */
112462
112463 if( argc==3 ){
112464 /* The escape character string must consist of a single UTF-8 character.
112465 ** Otherwise, return an error.
112466 */
112467 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
112468 if( zEsc==0 ) return;
112469 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
112470 sqlite3_result_error(context,
112471 "ESCAPE expression must be a single character", -1);
112472 return;
112473 }
112474 escape = sqlite3Utf8Read(&zEsc);
112475 }else{
112476 escape = pInfo->matchSet;
112477 }
112478 if( zA && zB ){
112479#ifdef SQLITE_TEST
112480 sqlite3_like_count++;
112481#endif
112482 sqlite3_result_int(context,
112483 patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH);
112484 }
112485}
112486
112487/*
112488** Implementation of the NULLIF(x,y) function. The result is the first
112489** argument if the arguments are different. The result is NULL if the
112490** arguments are equal to each other.
112491*/
112492static void nullifFunc(
112493 sqlite3_context *context,
112494 int NotUsed,
112495 sqlite3_value **argv
112496){
112497 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
112498 UNUSED_PARAMETER(NotUsed);
112499 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
112500 sqlite3_result_value(context, argv[0]);
112501 }
112502}
112503
112504/*
112505** Implementation of the sqlite_version() function. The result is the version
112506** of the SQLite library that is running.
112507*/
112508static void versionFunc(
112509 sqlite3_context *context,
112510 int NotUsed,
112511 sqlite3_value **NotUsed2
112512){
112513 UNUSED_PARAMETER2(NotUsed, NotUsed2);
112514 /* IMP: R-48699-48617 This function is an SQL wrapper around the
112515 ** sqlite3_libversion() C-interface. */
112516 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
112517}
112518
112519/*
112520** Implementation of the sqlite_source_id() function. The result is a string
112521** that identifies the particular version of the source code used to build
112522** SQLite.
112523*/
112524static void sourceidFunc(
112525 sqlite3_context *context,
112526 int NotUsed,
112527 sqlite3_value **NotUsed2
112528){
112529 UNUSED_PARAMETER2(NotUsed, NotUsed2);
112530 /* IMP: R-24470-31136 This function is an SQL wrapper around the
112531 ** sqlite3_sourceid() C interface. */
112532 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
112533}
112534
112535/*
112536** Implementation of the sqlite_log() function. This is a wrapper around
112537** sqlite3_log(). The return value is NULL. The function exists purely for
112538** its side-effects.
112539*/
112540static void errlogFunc(
112541 sqlite3_context *context,
112542 int argc,
112543 sqlite3_value **argv
112544){
112545 UNUSED_PARAMETER(argc);
112546 UNUSED_PARAMETER(context);
112547 sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
112548}
112549
112550/*
112551** Implementation of the sqlite_compileoption_used() function.
112552** The result is an integer that identifies if the compiler option
112553** was used to build SQLite.
112554*/
112555#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
112556static void compileoptionusedFunc(
112557 sqlite3_context *context,
112558 int argc,
112559 sqlite3_value **argv
112560){
112561 const char *zOptName;
112562 assert( argc==1 );
112563 UNUSED_PARAMETER(argc);
112564 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
112565 ** function is a wrapper around the sqlite3_compileoption_used() C/C++
112566 ** function.
112567 */
112568 if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
112569 sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
112570 }
112571}
112572#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
112573
112574/*
112575** Implementation of the sqlite_compileoption_get() function.
112576** The result is a string that identifies the compiler options
112577** used to build SQLite.
112578*/
112579#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
112580static void compileoptiongetFunc(
112581 sqlite3_context *context,
112582 int argc,
112583 sqlite3_value **argv
112584){
112585 int n;
112586 assert( argc==1 );
112587 UNUSED_PARAMETER(argc);
112588 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
112589 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
112590 */
112591 n = sqlite3_value_int(argv[0]);
112592 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
112593}
112594#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
112595
112596/* Array for converting from half-bytes (nybbles) into ASCII hex
112597** digits. */
112598static const char hexdigits[] = {
112599 '0', '1', '2', '3', '4', '5', '6', '7',
112600 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
112601};
112602
112603/*
112604** Implementation of the QUOTE() function. This function takes a single
112605** argument. If the argument is numeric, the return value is the same as
112606** the argument. If the argument is NULL, the return value is the string
112607** "NULL". Otherwise, the argument is enclosed in single quotes with
112608** single-quote escapes.
112609*/
112610static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
112611 assert( argc==1 );
112612 UNUSED_PARAMETER(argc);
112613 switch( sqlite3_value_type(argv[0]) ){
112614 case SQLITE_FLOAT: {
112615 double r1, r2;
112616 char zBuf[50];
112617 r1 = sqlite3_value_double(argv[0]);
112618 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
112619 sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
112620 if( r1!=r2 ){
112621 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
112622 }
112623 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
112624 break;
112625 }
112626 case SQLITE_INTEGER: {
112627 sqlite3_result_value(context, argv[0]);
112628 break;
112629 }
112630 case SQLITE_BLOB: {
112631 char *zText = 0;
112632 char const *zBlob = sqlite3_value_blob(argv[0]);
112633 int nBlob = sqlite3_value_bytes(argv[0]);
112634 assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
112635 zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
112636 if( zText ){
112637 int i;
112638 for(i=0; i<nBlob; i++){
112639 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
112640 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
112641 }
112642 zText[(nBlob*2)+2] = '\'';
112643 zText[(nBlob*2)+3] = '\0';
112644 zText[0] = 'X';
112645 zText[1] = '\'';
112646 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
112647 sqlite3_free(zText);
112648 }
112649 break;
112650 }
112651 case SQLITE_TEXT: {
112652 int i,j;
112653 u64 n;
112654 const unsigned char *zArg = sqlite3_value_text(argv[0]);
112655 char *z;
112656
112657 if( zArg==0 ) return;
112658 for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
112659 z = contextMalloc(context, ((i64)i)+((i64)n)+3);
112660 if( z ){
112661 z[0] = '\'';
112662 for(i=0, j=1; zArg[i]; i++){
112663 z[j++] = zArg[i];
112664 if( zArg[i]=='\'' ){
112665 z[j++] = '\'';
112666 }
112667 }
112668 z[j++] = '\'';
112669 z[j] = 0;
112670 sqlite3_result_text(context, z, j, sqlite3_free);
112671 }
112672 break;
112673 }
112674 default: {
112675 assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
112676 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
112677 break;
112678 }
112679 }
112680}
112681
112682/*
112683** The unicode() function. Return the integer unicode code-point value
112684** for the first character of the input string.
112685*/
112686static void unicodeFunc(
112687 sqlite3_context *context,
112688 int argc,
112689 sqlite3_value **argv
112690){
112691 const unsigned char *z = sqlite3_value_text(argv[0]);
112692 (void)argc;
112693 if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
112694}
112695
112696/*
112697** The char() function takes zero or more arguments, each of which is
112698** an integer. It constructs a string where each character of the string
112699** is the unicode character for the corresponding integer argument.
112700*/
112701static void charFunc(
112702 sqlite3_context *context,
112703 int argc,
112704 sqlite3_value **argv
112705){
112706 unsigned char *z, *zOut;
112707 int i;
112708 zOut = z = sqlite3_malloc64( argc*4+1 );
112709 if( z==0 ){
112710 sqlite3_result_error_nomem(context);
112711 return;
112712 }
112713 for(i=0; i<argc; i++){
112714 sqlite3_int64 x;
112715 unsigned c;
112716 x = sqlite3_value_int64(argv[i]);
112717 if( x<0 || x>0x10ffff ) x = 0xfffd;
112718 c = (unsigned)(x & 0x1fffff);
112719 if( c<0x00080 ){
112720 *zOut++ = (u8)(c&0xFF);
112721 }else if( c<0x00800 ){
112722 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
112723 *zOut++ = 0x80 + (u8)(c & 0x3F);
112724 }else if( c<0x10000 ){
112725 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
112726 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
112727 *zOut++ = 0x80 + (u8)(c & 0x3F);
112728 }else{
112729 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
112730 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
112731 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
112732 *zOut++ = 0x80 + (u8)(c & 0x3F);
112733 } \
112734 }
112735 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
112736}
112737
112738/*
112739** The hex() function. Interpret the argument as a blob. Return
112740** a hexadecimal rendering as text.
112741*/
112742static void hexFunc(
112743 sqlite3_context *context,
112744 int argc,
112745 sqlite3_value **argv
112746){
112747 int i, n;
112748 const unsigned char *pBlob;
112749 char *zHex, *z;
112750 assert( argc==1 );
112751 UNUSED_PARAMETER(argc);
112752 pBlob = sqlite3_value_blob(argv[0]);
112753 n = sqlite3_value_bytes(argv[0]);
112754 assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
112755 z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
112756 if( zHex ){
112757 for(i=0; i<n; i++, pBlob++){
112758 unsigned char c = *pBlob;
112759 *(z++) = hexdigits[(c>>4)&0xf];
112760 *(z++) = hexdigits[c&0xf];
112761 }
112762 *z = 0;
112763 sqlite3_result_text(context, zHex, n*2, sqlite3_free);
112764 }
112765}
112766
112767/*
112768** The zeroblob(N) function returns a zero-filled blob of size N bytes.
112769*/
112770static void zeroblobFunc(
112771 sqlite3_context *context,
112772 int argc,
112773 sqlite3_value **argv
112774){
112775 i64 n;
112776 int rc;
112777 assert( argc==1 );
112778 UNUSED_PARAMETER(argc);
112779 n = sqlite3_value_int64(argv[0]);
112780 if( n<0 ) n = 0;
112781 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
112782 if( rc ){
112783 sqlite3_result_error_code(context, rc);
112784 }
112785}
112786
112787/*
112788** The replace() function. Three arguments are all strings: call
112789** them A, B, and C. The result is also a string which is derived
112790** from A by replacing every occurrence of B with C. The match
112791** must be exact. Collating sequences are not used.
112792*/
112793static void replaceFunc(
112794 sqlite3_context *context,
112795 int argc,
112796 sqlite3_value **argv
112797){
112798 const unsigned char *zStr; /* The input string A */
112799 const unsigned char *zPattern; /* The pattern string B */
112800 const unsigned char *zRep; /* The replacement string C */
112801 unsigned char *zOut; /* The output */
112802 int nStr; /* Size of zStr */
112803 int nPattern; /* Size of zPattern */
112804 int nRep; /* Size of zRep */
112805 i64 nOut; /* Maximum size of zOut */
112806 int loopLimit; /* Last zStr[] that might match zPattern[] */
112807 int i, j; /* Loop counters */
112808 unsigned cntExpand; /* Number zOut expansions */
112809 sqlite3 *db = sqlite3_context_db_handle(context);
112810
112811 assert( argc==3 );
112812 UNUSED_PARAMETER(argc);
112813 zStr = sqlite3_value_text(argv[0]);
112814 if( zStr==0 ) return;
112815 nStr = sqlite3_value_bytes(argv[0]);
112816 assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */
112817 zPattern = sqlite3_value_text(argv[1]);
112818 if( zPattern==0 ){
112819 assert( sqlite3_value_type(argv[1])==SQLITE_NULL
112820 || sqlite3_context_db_handle(context)->mallocFailed );
112821 return;
112822 }
112823 if( zPattern[0]==0 ){
112824 assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
112825 sqlite3_result_value(context, argv[0]);
112826 return;
112827 }
112828 nPattern = sqlite3_value_bytes(argv[1]);
112829 assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */
112830 zRep = sqlite3_value_text(argv[2]);
112831 if( zRep==0 ) return;
112832 nRep = sqlite3_value_bytes(argv[2]);
112833 assert( zRep==sqlite3_value_text(argv[2]) );
112834 nOut = nStr + 1;
112835 assert( nOut<SQLITE_MAX_LENGTH );
112836 zOut = contextMalloc(context, (i64)nOut);
112837 if( zOut==0 ){
112838 return;
112839 }
112840 loopLimit = nStr - nPattern;
112841 cntExpand = 0;
112842 for(i=j=0; i<=loopLimit; i++){
112843 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
112844 zOut[j++] = zStr[i];
112845 }else{
112846 if( nRep>nPattern ){
112847 nOut += nRep - nPattern;
112848 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
112849 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
112850 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
112851 sqlite3_result_error_toobig(context);
112852 sqlite3_free(zOut);
112853 return;
112854 }
112855 cntExpand++;
112856 if( (cntExpand&(cntExpand-1))==0 ){
112857 /* Grow the size of the output buffer only on substitutions
112858 ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
112859 u8 *zOld;
112860 zOld = zOut;
112861 zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1));
112862 if( zOut==0 ){
112863 sqlite3_result_error_nomem(context);
112864 sqlite3_free(zOld);
112865 return;
112866 }
112867 }
112868 }
112869 memcpy(&zOut[j], zRep, nRep);
112870 j += nRep;
112871 i += nPattern-1;
112872 }
112873 }
112874 assert( j+nStr-i+1<=nOut );
112875 memcpy(&zOut[j], &zStr[i], nStr-i);
112876 j += nStr - i;
112877 assert( j<=nOut );
112878 zOut[j] = 0;
112879 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
112880}
112881
112882/*
112883** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
112884** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
112885*/
112886static void trimFunc(
112887 sqlite3_context *context,
112888 int argc,
112889 sqlite3_value **argv
112890){
112891 const unsigned char *zIn; /* Input string */
112892 const unsigned char *zCharSet; /* Set of characters to trim */
112893 int nIn; /* Number of bytes in input */
112894 int flags; /* 1: trimleft 2: trimright 3: trim */
112895 int i; /* Loop counter */
112896 unsigned char *aLen = 0; /* Length of each character in zCharSet */
112897 unsigned char **azChar = 0; /* Individual characters in zCharSet */
112898 int nChar; /* Number of characters in zCharSet */
112899
112900 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
112901 return;
112902 }
112903 zIn = sqlite3_value_text(argv[0]);
112904 if( zIn==0 ) return;
112905 nIn = sqlite3_value_bytes(argv[0]);
112906 assert( zIn==sqlite3_value_text(argv[0]) );
112907 if( argc==1 ){
112908 static const unsigned char lenOne[] = { 1 };
112909 static unsigned char * const azOne[] = { (u8*)" " };
112910 nChar = 1;
112911 aLen = (u8*)lenOne;
112912 azChar = (unsigned char **)azOne;
112913 zCharSet = 0;
112914 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
112915 return;
112916 }else{
112917 const unsigned char *z;
112918 for(z=zCharSet, nChar=0; *z; nChar++){
112919 SQLITE_SKIP_UTF8(z);
112920 }
112921 if( nChar>0 ){
112922 azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
112923 if( azChar==0 ){
112924 return;
112925 }
112926 aLen = (unsigned char*)&azChar[nChar];
112927 for(z=zCharSet, nChar=0; *z; nChar++){
112928 azChar[nChar] = (unsigned char *)z;
112929 SQLITE_SKIP_UTF8(z);
112930 aLen[nChar] = (u8)(z - azChar[nChar]);
112931 }
112932 }
112933 }
112934 if( nChar>0 ){
112935 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
112936 if( flags & 1 ){
112937 while( nIn>0 ){
112938 int len = 0;
112939 for(i=0; i<nChar; i++){
112940 len = aLen[i];
112941 if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
112942 }
112943 if( i>=nChar ) break;
112944 zIn += len;
112945 nIn -= len;
112946 }
112947 }
112948 if( flags & 2 ){
112949 while( nIn>0 ){
112950 int len = 0;
112951 for(i=0; i<nChar; i++){
112952 len = aLen[i];
112953 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
112954 }
112955 if( i>=nChar ) break;
112956 nIn -= len;
112957 }
112958 }
112959 if( zCharSet ){
112960 sqlite3_free(azChar);
112961 }
112962 }
112963 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
112964}
112965
112966
112967#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
112968/*
112969** The "unknown" function is automatically substituted in place of
112970** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
112971** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
112972** When the "sqlite3" command-line shell is built using this functionality,
112973** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
112974** involving application-defined functions to be examined in a generic
112975** sqlite3 shell.
112976*/
112977static void unknownFunc(
112978 sqlite3_context *context,
112979 int argc,
112980 sqlite3_value **argv
112981){
112982 /* no-op */
112983}
112984#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
112985
112986
112987/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
112988** is only available if the SQLITE_SOUNDEX compile-time option is used
112989** when SQLite is built.
112990*/
112991#ifdef SQLITE_SOUNDEX
112992/*
112993** Compute the soundex encoding of a word.
112994**
112995** IMP: R-59782-00072 The soundex(X) function returns a string that is the
112996** soundex encoding of the string X.
112997*/
112998static void soundexFunc(
112999 sqlite3_context *context,
113000 int argc,
113001 sqlite3_value **argv
113002){
113003 char zResult[8];
113004 const u8 *zIn;
113005 int i, j;
113006 static const unsigned char iCode[] = {
113007 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113008 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113009 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113010 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113011 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
113012 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
113013 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
113014 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
113015 };
113016 assert( argc==1 );
113017 zIn = (u8*)sqlite3_value_text(argv[0]);
113018 if( zIn==0 ) zIn = (u8*)"";
113019 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
113020 if( zIn[i] ){
113021 u8 prevcode = iCode[zIn[i]&0x7f];
113022 zResult[0] = sqlite3Toupper(zIn[i]);
113023 for(j=1; j<4 && zIn[i]; i++){
113024 int code = iCode[zIn[i]&0x7f];
113025 if( code>0 ){
113026 if( code!=prevcode ){
113027 prevcode = code;
113028 zResult[j++] = code + '0';
113029 }
113030 }else{
113031 prevcode = 0;
113032 }
113033 }
113034 while( j<4 ){
113035 zResult[j++] = '0';
113036 }
113037 zResult[j] = 0;
113038 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
113039 }else{
113040 /* IMP: R-64894-50321 The string "?000" is returned if the argument
113041 ** is NULL or contains no ASCII alphabetic characters. */
113042 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
113043 }
113044}
113045#endif /* SQLITE_SOUNDEX */
113046
113047#ifndef SQLITE_OMIT_LOAD_EXTENSION
113048/*
113049** A function that loads a shared-library extension then returns NULL.
113050*/
113051static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
113052 const char *zFile = (const char *)sqlite3_value_text(argv[0]);
113053 const char *zProc;
113054 sqlite3 *db = sqlite3_context_db_handle(context);
113055 char *zErrMsg = 0;
113056
113057 /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
113058 ** flag is set. See the sqlite3_enable_load_extension() API.
113059 */
113060 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
113061 sqlite3_result_error(context, "not authorized", -1);
113062 return;
113063 }
113064
113065 if( argc==2 ){
113066 zProc = (const char *)sqlite3_value_text(argv[1]);
113067 }else{
113068 zProc = 0;
113069 }
113070 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
113071 sqlite3_result_error(context, zErrMsg, -1);
113072 sqlite3_free(zErrMsg);
113073 }
113074}
113075#endif
113076
113077
113078/*
113079** An instance of the following structure holds the context of a
113080** sum() or avg() aggregate computation.
113081*/
113082typedef struct SumCtx SumCtx;
113083struct SumCtx {
113084 double rSum; /* Floating point sum */
113085 i64 iSum; /* Integer sum */
113086 i64 cnt; /* Number of elements summed */
113087 u8 overflow; /* True if integer overflow seen */
113088 u8 approx; /* True if non-integer value was input to the sum */
113089};
113090
113091/*
113092** Routines used to compute the sum, average, and total.
113093**
113094** The SUM() function follows the (broken) SQL standard which means
113095** that it returns NULL if it sums over no inputs. TOTAL returns
113096** 0.0 in that case. In addition, TOTAL always returns a float where
113097** SUM might return an integer if it never encounters a floating point
113098** value. TOTAL never fails, but SUM might through an exception if
113099** it overflows an integer.
113100*/
113101static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
113102 SumCtx *p;
113103 int type;
113104 assert( argc==1 );
113105 UNUSED_PARAMETER(argc);
113106 p = sqlite3_aggregate_context(context, sizeof(*p));
113107 type = sqlite3_value_numeric_type(argv[0]);
113108 if( p && type!=SQLITE_NULL ){
113109 p->cnt++;
113110 if( type==SQLITE_INTEGER ){
113111 i64 v = sqlite3_value_int64(argv[0]);
113112 p->rSum += v;
113113 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
113114 p->approx = p->overflow = 1;
113115 }
113116 }else{
113117 p->rSum += sqlite3_value_double(argv[0]);
113118 p->approx = 1;
113119 }
113120 }
113121}
113122#ifndef SQLITE_OMIT_WINDOWFUNC
113123static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
113124 SumCtx *p;
113125 int type;
113126 assert( argc==1 );
113127 UNUSED_PARAMETER(argc);
113128 p = sqlite3_aggregate_context(context, sizeof(*p));
113129 type = sqlite3_value_numeric_type(argv[0]);
113130 /* p is always non-NULL because sumStep() will have been called first
113131 ** to initialize it */
113132 if( ALWAYS(p) && type!=SQLITE_NULL ){
113133 assert( p->cnt>0 );
113134 p->cnt--;
113135 assert( type==SQLITE_INTEGER || p->approx );
113136 if( type==SQLITE_INTEGER && p->approx==0 ){
113137 i64 v = sqlite3_value_int64(argv[0]);
113138 p->rSum -= v;
113139 p->iSum -= v;
113140 }else{
113141 p->rSum -= sqlite3_value_double(argv[0]);
113142 }
113143 }
113144}
113145#else
113146# define sumInverse 0
113147#endif /* SQLITE_OMIT_WINDOWFUNC */
113148static void sumFinalize(sqlite3_context *context){
113149 SumCtx *p;
113150 p = sqlite3_aggregate_context(context, 0);
113151 if( p && p->cnt>0 ){
113152 if( p->overflow ){
113153 sqlite3_result_error(context,"integer overflow",-1);
113154 }else if( p->approx ){
113155 sqlite3_result_double(context, p->rSum);
113156 }else{
113157 sqlite3_result_int64(context, p->iSum);
113158 }
113159 }
113160}
113161static void avgFinalize(sqlite3_context *context){
113162 SumCtx *p;
113163 p = sqlite3_aggregate_context(context, 0);
113164 if( p && p->cnt>0 ){
113165 sqlite3_result_double(context, p->rSum/(double)p->cnt);
113166 }
113167}
113168static void totalFinalize(sqlite3_context *context){
113169 SumCtx *p;
113170 p = sqlite3_aggregate_context(context, 0);
113171 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
113172 sqlite3_result_double(context, p ? p->rSum : (double)0);
113173}
113174
113175/*
113176** The following structure keeps track of state information for the
113177** count() aggregate function.
113178*/
113179typedef struct CountCtx CountCtx;
113180struct CountCtx {
113181 i64 n;
113182#ifdef SQLITE_DEBUG
113183 int bInverse; /* True if xInverse() ever called */
113184#endif
113185};
113186
113187/*
113188** Routines to implement the count() aggregate function.
113189*/
113190static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
113191 CountCtx *p;
113192 p = sqlite3_aggregate_context(context, sizeof(*p));
113193 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
113194 p->n++;
113195 }
113196
113197#ifndef SQLITE_OMIT_DEPRECATED
113198 /* The sqlite3_aggregate_count() function is deprecated. But just to make
113199 ** sure it still operates correctly, verify that its count agrees with our
113200 ** internal count when using count(*) and when the total count can be
113201 ** expressed as a 32-bit integer. */
113202 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
113203 || p->n==sqlite3_aggregate_count(context) );
113204#endif
113205}
113206static void countFinalize(sqlite3_context *context){
113207 CountCtx *p;
113208 p = sqlite3_aggregate_context(context, 0);
113209 sqlite3_result_int64(context, p ? p->n : 0);
113210}
113211#ifndef SQLITE_OMIT_WINDOWFUNC
113212static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){
113213 CountCtx *p;
113214 p = sqlite3_aggregate_context(ctx, sizeof(*p));
113215 /* p is always non-NULL since countStep() will have been called first */
113216 if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && ALWAYS(p) ){
113217 p->n--;
113218#ifdef SQLITE_DEBUG
113219 p->bInverse = 1;
113220#endif
113221 }
113222}
113223#else
113224# define countInverse 0
113225#endif /* SQLITE_OMIT_WINDOWFUNC */
113226
113227/*
113228** Routines to implement min() and max() aggregate functions.
113229*/
113230static void minmaxStep(
113231 sqlite3_context *context,
113232 int NotUsed,
113233 sqlite3_value **argv
113234){
113235 Mem *pArg = (Mem *)argv[0];
113236 Mem *pBest;
113237 UNUSED_PARAMETER(NotUsed);
113238
113239 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
113240 if( !pBest ) return;
113241
113242 if( sqlite3_value_type(pArg)==SQLITE_NULL ){
113243 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
113244 }else if( pBest->flags ){
113245 int max;
113246 int cmp;
113247 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
113248 /* This step function is used for both the min() and max() aggregates,
113249 ** the only difference between the two being that the sense of the
113250 ** comparison is inverted. For the max() aggregate, the
113251 ** sqlite3_user_data() function returns (void *)-1. For min() it
113252 ** returns (void *)db, where db is the sqlite3* database pointer.
113253 ** Therefore the next statement sets variable 'max' to 1 for the max()
113254 ** aggregate, or 0 for min().
113255 */
113256 max = sqlite3_user_data(context)!=0;
113257 cmp = sqlite3MemCompare(pBest, pArg, pColl);
113258 if( (max && cmp<0) || (!max && cmp>0) ){
113259 sqlite3VdbeMemCopy(pBest, pArg);
113260 }else{
113261 sqlite3SkipAccumulatorLoad(context);
113262 }
113263 }else{
113264 pBest->db = sqlite3_context_db_handle(context);
113265 sqlite3VdbeMemCopy(pBest, pArg);
113266 }
113267}
113268static void minMaxValueFinalize(sqlite3_context *context, int bValue){
113269 sqlite3_value *pRes;
113270 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
113271 if( pRes ){
113272 if( pRes->flags ){
113273 sqlite3_result_value(context, pRes);
113274 }
113275 if( bValue==0 ) sqlite3VdbeMemRelease(pRes);
113276 }
113277}
113278#ifndef SQLITE_OMIT_WINDOWFUNC
113279static void minMaxValue(sqlite3_context *context){
113280 minMaxValueFinalize(context, 1);
113281}
113282#else
113283# define minMaxValue 0
113284#endif /* SQLITE_OMIT_WINDOWFUNC */
113285static void minMaxFinalize(sqlite3_context *context){
113286 minMaxValueFinalize(context, 0);
113287}
113288
113289/*
113290** group_concat(EXPR, ?SEPARATOR?)
113291*/
113292static void groupConcatStep(
113293 sqlite3_context *context,
113294 int argc,
113295 sqlite3_value **argv
113296){
113297 const char *zVal;
113298 StrAccum *pAccum;
113299 const char *zSep;
113300 int nVal, nSep;
113301 assert( argc==1 || argc==2 );
113302 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
113303 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
113304
113305 if( pAccum ){
113306 sqlite3 *db = sqlite3_context_db_handle(context);
113307 int firstTerm = pAccum->mxAlloc==0;
113308 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
113309 if( !firstTerm ){
113310 if( argc==2 ){
113311 zSep = (char*)sqlite3_value_text(argv[1]);
113312 nSep = sqlite3_value_bytes(argv[1]);
113313 }else{
113314 zSep = ",";
113315 nSep = 1;
113316 }
113317 if( zSep ) sqlite3_str_append(pAccum, zSep, nSep);
113318 }
113319 zVal = (char*)sqlite3_value_text(argv[0]);
113320 nVal = sqlite3_value_bytes(argv[0]);
113321 if( zVal ) sqlite3_str_append(pAccum, zVal, nVal);
113322 }
113323}
113324#ifndef SQLITE_OMIT_WINDOWFUNC
113325static void groupConcatInverse(
113326 sqlite3_context *context,
113327 int argc,
113328 sqlite3_value **argv
113329){
113330 int n;
113331 StrAccum *pAccum;
113332 assert( argc==1 || argc==2 );
113333 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
113334 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
113335 /* pAccum is always non-NULL since groupConcatStep() will have always
113336 ** run frist to initialize it */
113337 if( ALWAYS(pAccum) ){
113338 n = sqlite3_value_bytes(argv[0]);
113339 if( argc==2 ){
113340 n += sqlite3_value_bytes(argv[1]);
113341 }else{
113342 n++;
113343 }
113344 if( n>=(int)pAccum->nChar ){
113345 pAccum->nChar = 0;
113346 }else{
113347 pAccum->nChar -= n;
113348 memmove(pAccum->zText, &pAccum->zText[n], pAccum->nChar);
113349 }
113350 if( pAccum->nChar==0 ) pAccum->mxAlloc = 0;
113351 }
113352}
113353#else
113354# define groupConcatInverse 0
113355#endif /* SQLITE_OMIT_WINDOWFUNC */
113356static void groupConcatFinalize(sqlite3_context *context){
113357 StrAccum *pAccum;
113358 pAccum = sqlite3_aggregate_context(context, 0);
113359 if( pAccum ){
113360 if( pAccum->accError==SQLITE_TOOBIG ){
113361 sqlite3_result_error_toobig(context);
113362 }else if( pAccum->accError==SQLITE_NOMEM ){
113363 sqlite3_result_error_nomem(context);
113364 }else{
113365 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
113366 sqlite3_free);
113367 }
113368 }
113369}
113370#ifndef SQLITE_OMIT_WINDOWFUNC
113371static void groupConcatValue(sqlite3_context *context){
113372 sqlite3_str *pAccum;
113373 pAccum = (sqlite3_str*)sqlite3_aggregate_context(context, 0);
113374 if( pAccum ){
113375 if( pAccum->accError==SQLITE_TOOBIG ){
113376 sqlite3_result_error_toobig(context);
113377 }else if( pAccum->accError==SQLITE_NOMEM ){
113378 sqlite3_result_error_nomem(context);
113379 }else{
113380 const char *zText = sqlite3_str_value(pAccum);
113381 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
113382 }
113383 }
113384}
113385#else
113386# define groupConcatValue 0
113387#endif /* SQLITE_OMIT_WINDOWFUNC */
113388
113389/*
113390** This routine does per-connection function registration. Most
113391** of the built-in functions above are part of the global function set.
113392** This routine only deals with those that are not global.
113393*/
113394SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
113395 int rc = sqlite3_overload_function(db, "MATCH", 2);
113396 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
113397 if( rc==SQLITE_NOMEM ){
113398 sqlite3OomFault(db);
113399 }
113400}
113401
113402/*
113403** Set the LIKEOPT flag on the 2-argument function with the given name.
113404*/
113405static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
113406 FuncDef *pDef;
113407 pDef = sqlite3FindFunction(db, zName, 2, SQLITE_UTF8, 0);
113408 if( ALWAYS(pDef) ){
113409 pDef->funcFlags |= flagVal;
113410 }
113411}
113412
113413/*
113414** Register the built-in LIKE and GLOB functions. The caseSensitive
113415** parameter determines whether or not the LIKE operator is case
113416** sensitive. GLOB is always case sensitive.
113417*/
113418SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
113419 struct compareInfo *pInfo;
113420 if( caseSensitive ){
113421 pInfo = (struct compareInfo*)&likeInfoAlt;
113422 }else{
113423 pInfo = (struct compareInfo*)&likeInfoNorm;
113424 }
113425 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
113426 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
113427 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
113428 (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0, 0, 0);
113429 setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
113430 setLikeOptFlag(db, "like",
113431 caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
113432}
113433
113434/*
113435** pExpr points to an expression which implements a function. If
113436** it is appropriate to apply the LIKE optimization to that function
113437** then set aWc[0] through aWc[2] to the wildcard characters and the
113438** escape character and then return TRUE. If the function is not a
113439** LIKE-style function then return FALSE.
113440**
113441** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
113442** operator if c is a string literal that is exactly one byte in length.
113443** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is
113444** no ESCAPE clause.
113445**
113446** *pIsNocase is set to true if uppercase and lowercase are equivalent for
113447** the function (default for LIKE). If the function makes the distinction
113448** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
113449** false.
113450*/
113451SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
113452 FuncDef *pDef;
113453 int nExpr;
113454 if( pExpr->op!=TK_FUNCTION || !pExpr->x.pList ){
113455 return 0;
113456 }
113457 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
113458 nExpr = pExpr->x.pList->nExpr;
113459 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
113460 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
113461 return 0;
113462 }
113463 if( nExpr<3 ){
113464 aWc[3] = 0;
113465 }else{
113466 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
113467 char *zEscape;
113468 if( pEscape->op!=TK_STRING ) return 0;
113469 zEscape = pEscape->u.zToken;
113470 if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
113471 aWc[3] = zEscape[0];
113472 }
113473
113474 /* The memcpy() statement assumes that the wildcard characters are
113475 ** the first three statements in the compareInfo structure. The
113476 ** asserts() that follow verify that assumption
113477 */
113478 memcpy(aWc, pDef->pUserData, 3);
113479 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
113480 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
113481 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
113482 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
113483 return 1;
113484}
113485
113486/*
113487** All of the FuncDef structures in the aBuiltinFunc[] array above
113488** to the global function hash table. This occurs at start-time (as
113489** a consequence of calling sqlite3_initialize()).
113490**
113491** After this routine runs
113492*/
113493SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void){
113494 /*
113495 ** The following array holds FuncDef structures for all of the functions
113496 ** defined in this file.
113497 **
113498 ** The array cannot be constant since changes are made to the
113499 ** FuncDef.pHash elements at start-time. The elements of this array
113500 ** are read-only after initialization is complete.
113501 **
113502 ** For peak efficiency, put the most frequently used function last.
113503 */
113504 static FuncDef aBuiltinFunc[] = {
113505#ifdef SQLITE_SOUNDEX
113506 FUNCTION(soundex, 1, 0, 0, soundexFunc ),
113507#endif
113508#ifndef SQLITE_OMIT_LOAD_EXTENSION
113509 VFUNCTION(load_extension, 1, 0, 0, loadExt ),
113510 VFUNCTION(load_extension, 2, 0, 0, loadExt ),
113511#endif
113512#if SQLITE_USER_AUTHENTICATION
113513 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ),
113514#endif
113515#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
113516 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ),
113517 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ),
113518#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
113519 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
113520 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
113521 FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY),
113522#ifdef SQLITE_DEBUG
113523 FUNCTION2(affinity, 1, 0, 0, noopFunc, SQLITE_FUNC_AFFINITY),
113524#endif
113525#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
113526 FUNCTION2(sqlite_offset, 1, 0, 0, noopFunc, SQLITE_FUNC_OFFSET|
113527 SQLITE_FUNC_TYPEOF),
113528#endif
113529 FUNCTION(ltrim, 1, 1, 0, trimFunc ),
113530 FUNCTION(ltrim, 2, 1, 0, trimFunc ),
113531 FUNCTION(rtrim, 1, 2, 0, trimFunc ),
113532 FUNCTION(rtrim, 2, 2, 0, trimFunc ),
113533 FUNCTION(trim, 1, 3, 0, trimFunc ),
113534 FUNCTION(trim, 2, 3, 0, trimFunc ),
113535 FUNCTION(min, -1, 0, 1, minmaxFunc ),
113536 FUNCTION(min, 0, 0, 1, 0 ),
113537 WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
113538 SQLITE_FUNC_MINMAX ),
113539 FUNCTION(max, -1, 1, 1, minmaxFunc ),
113540 FUNCTION(max, 0, 1, 1, 0 ),
113541 WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
113542 SQLITE_FUNC_MINMAX ),
113543 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF),
113544 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH),
113545 FUNCTION(instr, 2, 0, 0, instrFunc ),
113546 FUNCTION(printf, -1, 0, 0, printfFunc ),
113547 FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
113548 FUNCTION(char, -1, 0, 0, charFunc ),
113549 FUNCTION(abs, 1, 0, 0, absFunc ),
113550#ifndef SQLITE_OMIT_FLOATING_POINT
113551 FUNCTION(round, 1, 0, 0, roundFunc ),
113552 FUNCTION(round, 2, 0, 0, roundFunc ),
113553#endif
113554 FUNCTION(upper, 1, 0, 0, upperFunc ),
113555 FUNCTION(lower, 1, 0, 0, lowerFunc ),
113556 FUNCTION(hex, 1, 0, 0, hexFunc ),
113557 FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
113558 VFUNCTION(random, 0, 0, 0, randomFunc ),
113559 VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
113560 FUNCTION(nullif, 2, 0, 1, nullifFunc ),
113561 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
113562 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
113563 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ),
113564 FUNCTION(quote, 1, 0, 0, quoteFunc ),
113565 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
113566 VFUNCTION(changes, 0, 0, 0, changes ),
113567 VFUNCTION(total_changes, 0, 0, 0, total_changes ),
113568 FUNCTION(replace, 3, 0, 0, replaceFunc ),
113569 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ),
113570 FUNCTION(substr, 2, 0, 0, substrFunc ),
113571 FUNCTION(substr, 3, 0, 0, substrFunc ),
113572 WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0),
113573 WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0),
113574 WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0),
113575 WAGGREGATE(count, 0,0,0, countStep,
113576 countFinalize, countFinalize, countInverse, SQLITE_FUNC_COUNT ),
113577 WAGGREGATE(count, 1,0,0, countStep,
113578 countFinalize, countFinalize, countInverse, 0 ),
113579 WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep,
113580 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
113581 WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep,
113582 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
113583
113584 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
113585#ifdef SQLITE_CASE_SENSITIVE_LIKE
113586 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
113587 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
113588#else
113589 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
113590 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
113591#endif
113592#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
113593 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
113594#endif
113595 FUNCTION(coalesce, 1, 0, 0, 0 ),
113596 FUNCTION(coalesce, 0, 0, 0, 0 ),
113597 FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE),
113598 };
113599#ifndef SQLITE_OMIT_ALTERTABLE
113600 sqlite3AlterFunctions();
113601#endif
113602 sqlite3WindowFunctions();
113603#if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
113604 sqlite3AnalyzeFunctions();
113605#endif
113606 sqlite3RegisterDateTimeFunctions();
113607 sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc));
113608
113609#if 0 /* Enable to print out how the built-in functions are hashed */
113610 {
113611 int i;
113612 FuncDef *p;
113613 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
113614 printf("FUNC-HASH %02d:", i);
113615 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
113616 int n = sqlite3Strlen30(p->zName);
113617 int h = p->zName[0] + n;
113618 printf(" %s(%d)", p->zName, h);
113619 }
113620 printf("\n");
113621 }
113622 }
113623#endif
113624}
113625
113626/************** End of func.c ************************************************/
113627/************** Begin file fkey.c ********************************************/
113628/*
113629**
113630** The author disclaims copyright to this source code. In place of
113631** a legal notice, here is a blessing:
113632**
113633** May you do good and not evil.
113634** May you find forgiveness for yourself and forgive others.
113635** May you share freely, never taking more than you give.
113636**
113637*************************************************************************
113638** This file contains code used by the compiler to add foreign key
113639** support to compiled SQL statements.
113640*/
113641/* #include "sqliteInt.h" */
113642
113643#ifndef SQLITE_OMIT_FOREIGN_KEY
113644#ifndef SQLITE_OMIT_TRIGGER
113645
113646/*
113647** Deferred and Immediate FKs
113648** --------------------------
113649**
113650** Foreign keys in SQLite come in two flavours: deferred and immediate.
113651** If an immediate foreign key constraint is violated,
113652** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
113653** statement transaction rolled back. If a
113654** deferred foreign key constraint is violated, no action is taken
113655** immediately. However if the application attempts to commit the
113656** transaction before fixing the constraint violation, the attempt fails.
113657**
113658** Deferred constraints are implemented using a simple counter associated
113659** with the database handle. The counter is set to zero each time a
113660** database transaction is opened. Each time a statement is executed
113661** that causes a foreign key violation, the counter is incremented. Each
113662** time a statement is executed that removes an existing violation from
113663** the database, the counter is decremented. When the transaction is
113664** committed, the commit fails if the current value of the counter is
113665** greater than zero. This scheme has two big drawbacks:
113666**
113667** * When a commit fails due to a deferred foreign key constraint,
113668** there is no way to tell which foreign constraint is not satisfied,
113669** or which row it is not satisfied for.
113670**
113671** * If the database contains foreign key violations when the
113672** transaction is opened, this may cause the mechanism to malfunction.
113673**
113674** Despite these problems, this approach is adopted as it seems simpler
113675** than the alternatives.
113676**
113677** INSERT operations:
113678**
113679** I.1) For each FK for which the table is the child table, search
113680** the parent table for a match. If none is found increment the
113681** constraint counter.
113682**
113683** I.2) For each FK for which the table is the parent table,
113684** search the child table for rows that correspond to the new
113685** row in the parent table. Decrement the counter for each row
113686** found (as the constraint is now satisfied).
113687**
113688** DELETE operations:
113689**
113690** D.1) For each FK for which the table is the child table,
113691** search the parent table for a row that corresponds to the
113692** deleted row in the child table. If such a row is not found,
113693** decrement the counter.
113694**
113695** D.2) For each FK for which the table is the parent table, search
113696** the child table for rows that correspond to the deleted row
113697** in the parent table. For each found increment the counter.
113698**
113699** UPDATE operations:
113700**
113701** An UPDATE command requires that all 4 steps above are taken, but only
113702** for FK constraints for which the affected columns are actually
113703** modified (values must be compared at runtime).
113704**
113705** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
113706** This simplifies the implementation a bit.
113707**
113708** For the purposes of immediate FK constraints, the OR REPLACE conflict
113709** resolution is considered to delete rows before the new row is inserted.
113710** If a delete caused by OR REPLACE violates an FK constraint, an exception
113711** is thrown, even if the FK constraint would be satisfied after the new
113712** row is inserted.
113713**
113714** Immediate constraints are usually handled similarly. The only difference
113715** is that the counter used is stored as part of each individual statement
113716** object (struct Vdbe). If, after the statement has run, its immediate
113717** constraint counter is greater than zero,
113718** it returns SQLITE_CONSTRAINT_FOREIGNKEY
113719** and the statement transaction is rolled back. An exception is an INSERT
113720** statement that inserts a single row only (no triggers). In this case,
113721** instead of using a counter, an exception is thrown immediately if the
113722** INSERT violates a foreign key constraint. This is necessary as such
113723** an INSERT does not open a statement transaction.
113724**
113725** TODO: How should dropping a table be handled? How should renaming a
113726** table be handled?
113727**
113728**
113729** Query API Notes
113730** ---------------
113731**
113732** Before coding an UPDATE or DELETE row operation, the code-generator
113733** for those two operations needs to know whether or not the operation
113734** requires any FK processing and, if so, which columns of the original
113735** row are required by the FK processing VDBE code (i.e. if FKs were
113736** implemented using triggers, which of the old.* columns would be
113737** accessed). No information is required by the code-generator before
113738** coding an INSERT operation. The functions used by the UPDATE/DELETE
113739** generation code to query for this information are:
113740**
113741** sqlite3FkRequired() - Test to see if FK processing is required.
113742** sqlite3FkOldmask() - Query for the set of required old.* columns.
113743**
113744**
113745** Externally accessible module functions
113746** --------------------------------------
113747**
113748** sqlite3FkCheck() - Check for foreign key violations.
113749** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
113750** sqlite3FkDelete() - Delete an FKey structure.
113751*/
113752
113753/*
113754** VDBE Calling Convention
113755** -----------------------
113756**
113757** Example:
113758**
113759** For the following INSERT statement:
113760**
113761** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
113762** INSERT INTO t1 VALUES(1, 2, 3.1);
113763**
113764** Register (x): 2 (type integer)
113765** Register (x+1): 1 (type integer)
113766** Register (x+2): NULL (type NULL)
113767** Register (x+3): 3.1 (type real)
113768*/
113769
113770/*
113771** A foreign key constraint requires that the key columns in the parent
113772** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
113773** Given that pParent is the parent table for foreign key constraint pFKey,
113774** search the schema for a unique index on the parent key columns.
113775**
113776** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
113777** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
113778** is set to point to the unique index.
113779**
113780** If the parent key consists of a single column (the foreign key constraint
113781** is not a composite foreign key), output variable *paiCol is set to NULL.
113782** Otherwise, it is set to point to an allocated array of size N, where
113783** N is the number of columns in the parent key. The first element of the
113784** array is the index of the child table column that is mapped by the FK
113785** constraint to the parent table column stored in the left-most column
113786** of index *ppIdx. The second element of the array is the index of the
113787** child table column that corresponds to the second left-most column of
113788** *ppIdx, and so on.
113789**
113790** If the required index cannot be found, either because:
113791**
113792** 1) The named parent key columns do not exist, or
113793**
113794** 2) The named parent key columns do exist, but are not subject to a
113795** UNIQUE or PRIMARY KEY constraint, or
113796**
113797** 3) No parent key columns were provided explicitly as part of the
113798** foreign key definition, and the parent table does not have a
113799** PRIMARY KEY, or
113800**
113801** 4) No parent key columns were provided explicitly as part of the
113802** foreign key definition, and the PRIMARY KEY of the parent table
113803** consists of a different number of columns to the child key in
113804** the child table.
113805**
113806** then non-zero is returned, and a "foreign key mismatch" error loaded
113807** into pParse. If an OOM error occurs, non-zero is returned and the
113808** pParse->db->mallocFailed flag is set.
113809*/
113810SQLITE_PRIVATE int sqlite3FkLocateIndex(
113811 Parse *pParse, /* Parse context to store any error in */
113812 Table *pParent, /* Parent table of FK constraint pFKey */
113813 FKey *pFKey, /* Foreign key to find index for */
113814 Index **ppIdx, /* OUT: Unique index on parent table */
113815 int **paiCol /* OUT: Map of index columns in pFKey */
113816){
113817 Index *pIdx = 0; /* Value to return via *ppIdx */
113818 int *aiCol = 0; /* Value to return via *paiCol */
113819 int nCol = pFKey->nCol; /* Number of columns in parent key */
113820 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
113821
113822 /* The caller is responsible for zeroing output parameters. */
113823 assert( ppIdx && *ppIdx==0 );
113824 assert( !paiCol || *paiCol==0 );
113825 assert( pParse );
113826
113827 /* If this is a non-composite (single column) foreign key, check if it
113828 ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
113829 ** and *paiCol set to zero and return early.
113830 **
113831 ** Otherwise, for a composite foreign key (more than one column), allocate
113832 ** space for the aiCol array (returned via output parameter *paiCol).
113833 ** Non-composite foreign keys do not require the aiCol array.
113834 */
113835 if( nCol==1 ){
113836 /* The FK maps to the IPK if any of the following are true:
113837 **
113838 ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
113839 ** mapped to the primary key of table pParent, or
113840 ** 2) The FK is explicitly mapped to a column declared as INTEGER
113841 ** PRIMARY KEY.
113842 */
113843 if( pParent->iPKey>=0 ){
113844 if( !zKey ) return 0;
113845 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
113846 }
113847 }else if( paiCol ){
113848 assert( nCol>1 );
113849 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
113850 if( !aiCol ) return 1;
113851 *paiCol = aiCol;
113852 }
113853
113854 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
113855 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
113856 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
113857 ** of columns. If each indexed column corresponds to a foreign key
113858 ** column of pFKey, then this index is a winner. */
113859
113860 if( zKey==0 ){
113861 /* If zKey is NULL, then this foreign key is implicitly mapped to
113862 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
113863 ** identified by the test. */
113864 if( IsPrimaryKeyIndex(pIdx) ){
113865 if( aiCol ){
113866 int i;
113867 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
113868 }
113869 break;
113870 }
113871 }else{
113872 /* If zKey is non-NULL, then this foreign key was declared to
113873 ** map to an explicit list of columns in table pParent. Check if this
113874 ** index matches those columns. Also, check that the index uses
113875 ** the default collation sequences for each column. */
113876 int i, j;
113877 for(i=0; i<nCol; i++){
113878 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
113879 const char *zDfltColl; /* Def. collation for column */
113880 char *zIdxCol; /* Name of indexed column */
113881
113882 if( iCol<0 ) break; /* No foreign keys against expression indexes */
113883
113884 /* If the index uses a collation sequence that is different from
113885 ** the default collation sequence for the column, this index is
113886 ** unusable. Bail out early in this case. */
113887 zDfltColl = pParent->aCol[iCol].zColl;
113888 if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
113889 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
113890
113891 zIdxCol = pParent->aCol[iCol].zName;
113892 for(j=0; j<nCol; j++){
113893 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
113894 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
113895 break;
113896 }
113897 }
113898 if( j==nCol ) break;
113899 }
113900 if( i==nCol ) break; /* pIdx is usable */
113901 }
113902 }
113903 }
113904
113905 if( !pIdx ){
113906 if( !pParse->disableTriggers ){
113907 sqlite3ErrorMsg(pParse,
113908 "foreign key mismatch - \"%w\" referencing \"%w\"",
113909 pFKey->pFrom->zName, pFKey->zTo);
113910 }
113911 sqlite3DbFree(pParse->db, aiCol);
113912 return 1;
113913 }
113914
113915 *ppIdx = pIdx;
113916 return 0;
113917}
113918
113919/*
113920** This function is called when a row is inserted into or deleted from the
113921** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
113922** on the child table of pFKey, this function is invoked twice for each row
113923** affected - once to "delete" the old row, and then again to "insert" the
113924** new row.
113925**
113926** Each time it is called, this function generates VDBE code to locate the
113927** row in the parent table that corresponds to the row being inserted into
113928** or deleted from the child table. If the parent row can be found, no
113929** special action is taken. Otherwise, if the parent row can *not* be
113930** found in the parent table:
113931**
113932** Operation | FK type | Action taken
113933** --------------------------------------------------------------------------
113934** INSERT immediate Increment the "immediate constraint counter".
113935**
113936** DELETE immediate Decrement the "immediate constraint counter".
113937**
113938** INSERT deferred Increment the "deferred constraint counter".
113939**
113940** DELETE deferred Decrement the "deferred constraint counter".
113941**
113942** These operations are identified in the comment at the top of this file
113943** (fkey.c) as "I.1" and "D.1".
113944*/
113945static void fkLookupParent(
113946 Parse *pParse, /* Parse context */
113947 int iDb, /* Index of database housing pTab */
113948 Table *pTab, /* Parent table of FK pFKey */
113949 Index *pIdx, /* Unique index on parent key columns in pTab */
113950 FKey *pFKey, /* Foreign key constraint */
113951 int *aiCol, /* Map from parent key columns to child table columns */
113952 int regData, /* Address of array containing child table row */
113953 int nIncr, /* Increment constraint counter by this */
113954 int isIgnore /* If true, pretend pTab contains all NULL values */
113955){
113956 int i; /* Iterator variable */
113957 Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
113958 int iCur = pParse->nTab - 1; /* Cursor number to use */
113959 int iOk = sqlite3VdbeMakeLabel(v); /* jump here if parent key found */
113960
113961 sqlite3VdbeVerifyAbortable(v,
113962 (!pFKey->isDeferred
113963 && !(pParse->db->flags & SQLITE_DeferFKs)
113964 && !pParse->pToplevel
113965 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
113966
113967 /* If nIncr is less than zero, then check at runtime if there are any
113968 ** outstanding constraints to resolve. If there are not, there is no need
113969 ** to check if deleting this row resolves any outstanding violations.
113970 **
113971 ** Check if any of the key columns in the child table row are NULL. If
113972 ** any are, then the constraint is considered satisfied. No need to
113973 ** search for a matching row in the parent table. */
113974 if( nIncr<0 ){
113975 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
113976 VdbeCoverage(v);
113977 }
113978 for(i=0; i<pFKey->nCol; i++){
113979 int iReg = aiCol[i] + regData + 1;
113980 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); VdbeCoverage(v);
113981 }
113982
113983 if( isIgnore==0 ){
113984 if( pIdx==0 ){
113985 /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
113986 ** column of the parent table (table pTab). */
113987 int iMustBeInt; /* Address of MustBeInt instruction */
113988 int regTemp = sqlite3GetTempReg(pParse);
113989
113990 /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
113991 ** apply the affinity of the parent key). If this fails, then there
113992 ** is no matching parent key. Before using MustBeInt, make a copy of
113993 ** the value. Otherwise, the value inserted into the child key column
113994 ** will have INTEGER affinity applied to it, which may not be correct. */
113995 sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
113996 iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
113997 VdbeCoverage(v);
113998
113999 /* If the parent table is the same as the child table, and we are about
114000 ** to increment the constraint-counter (i.e. this is an INSERT operation),
114001 ** then check if the row being inserted matches itself. If so, do not
114002 ** increment the constraint-counter. */
114003 if( pTab==pFKey->pFrom && nIncr==1 ){
114004 sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp); VdbeCoverage(v);
114005 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
114006 }
114007
114008 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
114009 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); VdbeCoverage(v);
114010 sqlite3VdbeGoto(v, iOk);
114011 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
114012 sqlite3VdbeJumpHere(v, iMustBeInt);
114013 sqlite3ReleaseTempReg(pParse, regTemp);
114014 }else{
114015 int nCol = pFKey->nCol;
114016 int regTemp = sqlite3GetTempRange(pParse, nCol);
114017 int regRec = sqlite3GetTempReg(pParse);
114018
114019 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
114020 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
114021 for(i=0; i<nCol; i++){
114022 sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
114023 }
114024
114025 /* If the parent table is the same as the child table, and we are about
114026 ** to increment the constraint-counter (i.e. this is an INSERT operation),
114027 ** then check if the row being inserted matches itself. If so, do not
114028 ** increment the constraint-counter.
114029 **
114030 ** If any of the parent-key values are NULL, then the row cannot match
114031 ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
114032 ** of the parent-key values are NULL (at this point it is known that
114033 ** none of the child key values are).
114034 */
114035 if( pTab==pFKey->pFrom && nIncr==1 ){
114036 int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
114037 for(i=0; i<nCol; i++){
114038 int iChild = aiCol[i]+1+regData;
114039 int iParent = pIdx->aiColumn[i]+1+regData;
114040 assert( pIdx->aiColumn[i]>=0 );
114041 assert( aiCol[i]!=pTab->iPKey );
114042 if( pIdx->aiColumn[i]==pTab->iPKey ){
114043 /* The parent key is a composite key that includes the IPK column */
114044 iParent = regData;
114045 }
114046 sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v);
114047 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
114048 }
114049 sqlite3VdbeGoto(v, iOk);
114050 }
114051
114052 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec,
114053 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
114054 sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v);
114055
114056 sqlite3ReleaseTempReg(pParse, regRec);
114057 sqlite3ReleaseTempRange(pParse, regTemp, nCol);
114058 }
114059 }
114060
114061 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
114062 && !pParse->pToplevel
114063 && !pParse->isMultiWrite
114064 ){
114065 /* Special case: If this is an INSERT statement that will insert exactly
114066 ** one row into the table, raise a constraint immediately instead of
114067 ** incrementing a counter. This is necessary as the VM code is being
114068 ** generated for will not open a statement transaction. */
114069 assert( nIncr==1 );
114070 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
114071 OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
114072 }else{
114073 if( nIncr>0 && pFKey->isDeferred==0 ){
114074 sqlite3MayAbort(pParse);
114075 }
114076 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
114077 }
114078
114079 sqlite3VdbeResolveLabel(v, iOk);
114080 sqlite3VdbeAddOp1(v, OP_Close, iCur);
114081}
114082
114083
114084/*
114085** Return an Expr object that refers to a memory register corresponding
114086** to column iCol of table pTab.
114087**
114088** regBase is the first of an array of register that contains the data
114089** for pTab. regBase itself holds the rowid. regBase+1 holds the first
114090** column. regBase+2 holds the second column, and so forth.
114091*/
114092static Expr *exprTableRegister(
114093 Parse *pParse, /* Parsing and code generating context */
114094 Table *pTab, /* The table whose content is at r[regBase]... */
114095 int regBase, /* Contents of table pTab */
114096 i16 iCol /* Which column of pTab is desired */
114097){
114098 Expr *pExpr;
114099 Column *pCol;
114100 const char *zColl;
114101 sqlite3 *db = pParse->db;
114102
114103 pExpr = sqlite3Expr(db, TK_REGISTER, 0);
114104 if( pExpr ){
114105 if( iCol>=0 && iCol!=pTab->iPKey ){
114106 pCol = &pTab->aCol[iCol];
114107 pExpr->iTable = regBase + iCol + 1;
114108 pExpr->affinity = pCol->affinity;
114109 zColl = pCol->zColl;
114110 if( zColl==0 ) zColl = db->pDfltColl->zName;
114111 pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
114112 }else{
114113 pExpr->iTable = regBase;
114114 pExpr->affinity = SQLITE_AFF_INTEGER;
114115 }
114116 }
114117 return pExpr;
114118}
114119
114120/*
114121** Return an Expr object that refers to column iCol of table pTab which
114122** has cursor iCur.
114123*/
114124static Expr *exprTableColumn(
114125 sqlite3 *db, /* The database connection */
114126 Table *pTab, /* The table whose column is desired */
114127 int iCursor, /* The open cursor on the table */
114128 i16 iCol /* The column that is wanted */
114129){
114130 Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
114131 if( pExpr ){
114132 pExpr->pTab = pTab;
114133 pExpr->iTable = iCursor;
114134 pExpr->iColumn = iCol;
114135 }
114136 return pExpr;
114137}
114138
114139/*
114140** This function is called to generate code executed when a row is deleted
114141** from the parent table of foreign key constraint pFKey and, if pFKey is
114142** deferred, when a row is inserted into the same table. When generating
114143** code for an SQL UPDATE operation, this function may be called twice -
114144** once to "delete" the old row and once to "insert" the new row.
114145**
114146** Parameter nIncr is passed -1 when inserting a row (as this may decrease
114147** the number of FK violations in the db) or +1 when deleting one (as this
114148** may increase the number of FK constraint problems).
114149**
114150** The code generated by this function scans through the rows in the child
114151** table that correspond to the parent table row being deleted or inserted.
114152** For each child row found, one of the following actions is taken:
114153**
114154** Operation | FK type | Action taken
114155** --------------------------------------------------------------------------
114156** DELETE immediate Increment the "immediate constraint counter".
114157** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
114158** throw a "FOREIGN KEY constraint failed" exception.
114159**
114160** INSERT immediate Decrement the "immediate constraint counter".
114161**
114162** DELETE deferred Increment the "deferred constraint counter".
114163** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
114164** throw a "FOREIGN KEY constraint failed" exception.
114165**
114166** INSERT deferred Decrement the "deferred constraint counter".
114167**
114168** These operations are identified in the comment at the top of this file
114169** (fkey.c) as "I.2" and "D.2".
114170*/
114171static void fkScanChildren(
114172 Parse *pParse, /* Parse context */
114173 SrcList *pSrc, /* The child table to be scanned */
114174 Table *pTab, /* The parent table */
114175 Index *pIdx, /* Index on parent covering the foreign key */
114176 FKey *pFKey, /* The foreign key linking pSrc to pTab */
114177 int *aiCol, /* Map from pIdx cols to child table cols */
114178 int regData, /* Parent row data starts here */
114179 int nIncr /* Amount to increment deferred counter by */
114180){
114181 sqlite3 *db = pParse->db; /* Database handle */
114182 int i; /* Iterator variable */
114183 Expr *pWhere = 0; /* WHERE clause to scan with */
114184 NameContext sNameContext; /* Context used to resolve WHERE clause */
114185 WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */
114186 int iFkIfZero = 0; /* Address of OP_FkIfZero */
114187 Vdbe *v = sqlite3GetVdbe(pParse);
114188
114189 assert( pIdx==0 || pIdx->pTable==pTab );
114190 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
114191 assert( pIdx!=0 || pFKey->nCol==1 );
114192 assert( pIdx!=0 || HasRowid(pTab) );
114193
114194 if( nIncr<0 ){
114195 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
114196 VdbeCoverage(v);
114197 }
114198
114199 /* Create an Expr object representing an SQL expression like:
114200 **
114201 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
114202 **
114203 ** The collation sequence used for the comparison should be that of
114204 ** the parent key columns. The affinity of the parent key column should
114205 ** be applied to each child key value before the comparison takes place.
114206 */
114207 for(i=0; i<pFKey->nCol; i++){
114208 Expr *pLeft; /* Value from parent table row */
114209 Expr *pRight; /* Column ref to child table */
114210 Expr *pEq; /* Expression (pLeft = pRight) */
114211 i16 iCol; /* Index of column in child table */
114212 const char *zCol; /* Name of column in child table */
114213
114214 iCol = pIdx ? pIdx->aiColumn[i] : -1;
114215 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
114216 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
114217 assert( iCol>=0 );
114218 zCol = pFKey->pFrom->aCol[iCol].zName;
114219 pRight = sqlite3Expr(db, TK_ID, zCol);
114220 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
114221 pWhere = sqlite3ExprAnd(db, pWhere, pEq);
114222 }
114223
114224 /* If the child table is the same as the parent table, then add terms
114225 ** to the WHERE clause that prevent this entry from being scanned.
114226 ** The added WHERE clause terms are like this:
114227 **
114228 ** $current_rowid!=rowid
114229 ** NOT( $current_a==a AND $current_b==b AND ... )
114230 **
114231 ** The first form is used for rowid tables. The second form is used
114232 ** for WITHOUT ROWID tables. In the second form, the primary key is
114233 ** (a,b,...)
114234 */
114235 if( pTab==pFKey->pFrom && nIncr>0 ){
114236 Expr *pNe; /* Expression (pLeft != pRight) */
114237 Expr *pLeft; /* Value from parent table row */
114238 Expr *pRight; /* Column ref to child table */
114239 if( HasRowid(pTab) ){
114240 pLeft = exprTableRegister(pParse, pTab, regData, -1);
114241 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
114242 pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight);
114243 }else{
114244 Expr *pEq, *pAll = 0;
114245 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
114246 assert( pIdx!=0 );
114247 for(i=0; i<pPk->nKeyCol; i++){
114248 i16 iCol = pIdx->aiColumn[i];
114249 assert( iCol>=0 );
114250 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
114251 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
114252 pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
114253 pAll = sqlite3ExprAnd(db, pAll, pEq);
114254 }
114255 pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0);
114256 }
114257 pWhere = sqlite3ExprAnd(db, pWhere, pNe);
114258 }
114259
114260 /* Resolve the references in the WHERE clause. */
114261 memset(&sNameContext, 0, sizeof(NameContext));
114262 sNameContext.pSrcList = pSrc;
114263 sNameContext.pParse = pParse;
114264 sqlite3ResolveExprNames(&sNameContext, pWhere);
114265
114266 /* Create VDBE to loop through the entries in pSrc that match the WHERE
114267 ** clause. For each row found, increment either the deferred or immediate
114268 ** foreign key constraint counter. */
114269 if( pParse->nErr==0 ){
114270 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
114271 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
114272 if( pWInfo ){
114273 sqlite3WhereEnd(pWInfo);
114274 }
114275 }
114276
114277 /* Clean up the WHERE clause constructed above. */
114278 sqlite3ExprDelete(db, pWhere);
114279 if( iFkIfZero ){
114280 sqlite3VdbeJumpHere(v, iFkIfZero);
114281 }
114282}
114283
114284/*
114285** This function returns a linked list of FKey objects (connected by
114286** FKey.pNextTo) holding all children of table pTab. For example,
114287** given the following schema:
114288**
114289** CREATE TABLE t1(a PRIMARY KEY);
114290** CREATE TABLE t2(b REFERENCES t1(a);
114291**
114292** Calling this function with table "t1" as an argument returns a pointer
114293** to the FKey structure representing the foreign key constraint on table
114294** "t2". Calling this function with "t2" as the argument would return a
114295** NULL pointer (as there are no FK constraints for which t2 is the parent
114296** table).
114297*/
114298SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
114299 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
114300}
114301
114302/*
114303** The second argument is a Trigger structure allocated by the
114304** fkActionTrigger() routine. This function deletes the Trigger structure
114305** and all of its sub-components.
114306**
114307** The Trigger structure or any of its sub-components may be allocated from
114308** the lookaside buffer belonging to database handle dbMem.
114309*/
114310static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
114311 if( p ){
114312 TriggerStep *pStep = p->step_list;
114313 sqlite3ExprDelete(dbMem, pStep->pWhere);
114314 sqlite3ExprListDelete(dbMem, pStep->pExprList);
114315 sqlite3SelectDelete(dbMem, pStep->pSelect);
114316 sqlite3ExprDelete(dbMem, p->pWhen);
114317 sqlite3DbFree(dbMem, p);
114318 }
114319}
114320
114321/*
114322** This function is called to generate code that runs when table pTab is
114323** being dropped from the database. The SrcList passed as the second argument
114324** to this function contains a single entry guaranteed to resolve to
114325** table pTab.
114326**
114327** Normally, no code is required. However, if either
114328**
114329** (a) The table is the parent table of a FK constraint, or
114330** (b) The table is the child table of a deferred FK constraint and it is
114331** determined at runtime that there are outstanding deferred FK
114332** constraint violations in the database,
114333**
114334** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
114335** the table from the database. Triggers are disabled while running this
114336** DELETE, but foreign key actions are not.
114337*/
114338SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
114339 sqlite3 *db = pParse->db;
114340 if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) ){
114341 int iSkip = 0;
114342 Vdbe *v = sqlite3GetVdbe(pParse);
114343
114344 assert( v ); /* VDBE has already been allocated */
114345 assert( pTab->pSelect==0 ); /* Not a view */
114346 if( sqlite3FkReferences(pTab)==0 ){
114347 /* Search for a deferred foreign key constraint for which this table
114348 ** is the child table. If one cannot be found, return without
114349 ** generating any VDBE code. If one can be found, then jump over
114350 ** the entire DELETE if there are no outstanding deferred constraints
114351 ** when this statement is run. */
114352 FKey *p;
114353 for(p=pTab->pFKey; p; p=p->pNextFrom){
114354 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
114355 }
114356 if( !p ) return;
114357 iSkip = sqlite3VdbeMakeLabel(v);
114358 sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
114359 }
114360
114361 pParse->disableTriggers = 1;
114362 sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0, 0, 0);
114363 pParse->disableTriggers = 0;
114364
114365 /* If the DELETE has generated immediate foreign key constraint
114366 ** violations, halt the VDBE and return an error at this point, before
114367 ** any modifications to the schema are made. This is because statement
114368 ** transactions are not able to rollback schema changes.
114369 **
114370 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
114371 ** the statement transaction will not be rolled back even if FK
114372 ** constraints are violated.
114373 */
114374 if( (db->flags & SQLITE_DeferFKs)==0 ){
114375 sqlite3VdbeVerifyAbortable(v, OE_Abort);
114376 sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
114377 VdbeCoverage(v);
114378 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
114379 OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
114380 }
114381
114382 if( iSkip ){
114383 sqlite3VdbeResolveLabel(v, iSkip);
114384 }
114385 }
114386}
114387
114388
114389/*
114390** The second argument points to an FKey object representing a foreign key
114391** for which pTab is the child table. An UPDATE statement against pTab
114392** is currently being processed. For each column of the table that is
114393** actually updated, the corresponding element in the aChange[] array
114394** is zero or greater (if a column is unmodified the corresponding element
114395** is set to -1). If the rowid column is modified by the UPDATE statement
114396** the bChngRowid argument is non-zero.
114397**
114398** This function returns true if any of the columns that are part of the
114399** child key for FK constraint *p are modified.
114400*/
114401static int fkChildIsModified(
114402 Table *pTab, /* Table being updated */
114403 FKey *p, /* Foreign key for which pTab is the child */
114404 int *aChange, /* Array indicating modified columns */
114405 int bChngRowid /* True if rowid is modified by this update */
114406){
114407 int i;
114408 for(i=0; i<p->nCol; i++){
114409 int iChildKey = p->aCol[i].iFrom;
114410 if( aChange[iChildKey]>=0 ) return 1;
114411 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
114412 }
114413 return 0;
114414}
114415
114416/*
114417** The second argument points to an FKey object representing a foreign key
114418** for which pTab is the parent table. An UPDATE statement against pTab
114419** is currently being processed. For each column of the table that is
114420** actually updated, the corresponding element in the aChange[] array
114421** is zero or greater (if a column is unmodified the corresponding element
114422** is set to -1). If the rowid column is modified by the UPDATE statement
114423** the bChngRowid argument is non-zero.
114424**
114425** This function returns true if any of the columns that are part of the
114426** parent key for FK constraint *p are modified.
114427*/
114428static int fkParentIsModified(
114429 Table *pTab,
114430 FKey *p,
114431 int *aChange,
114432 int bChngRowid
114433){
114434 int i;
114435 for(i=0; i<p->nCol; i++){
114436 char *zKey = p->aCol[i].zCol;
114437 int iKey;
114438 for(iKey=0; iKey<pTab->nCol; iKey++){
114439 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
114440 Column *pCol = &pTab->aCol[iKey];
114441 if( zKey ){
114442 if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
114443 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
114444 return 1;
114445 }
114446 }
114447 }
114448 }
114449 return 0;
114450}
114451
114452/*
114453** Return true if the parser passed as the first argument is being
114454** used to code a trigger that is really a "SET NULL" action belonging
114455** to trigger pFKey.
114456*/
114457static int isSetNullAction(Parse *pParse, FKey *pFKey){
114458 Parse *pTop = sqlite3ParseToplevel(pParse);
114459 if( pTop->pTriggerPrg ){
114460 Trigger *p = pTop->pTriggerPrg->pTrigger;
114461 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
114462 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
114463 ){
114464 return 1;
114465 }
114466 }
114467 return 0;
114468}
114469
114470/*
114471** This function is called when inserting, deleting or updating a row of
114472** table pTab to generate VDBE code to perform foreign key constraint
114473** processing for the operation.
114474**
114475** For a DELETE operation, parameter regOld is passed the index of the
114476** first register in an array of (pTab->nCol+1) registers containing the
114477** rowid of the row being deleted, followed by each of the column values
114478** of the row being deleted, from left to right. Parameter regNew is passed
114479** zero in this case.
114480**
114481** For an INSERT operation, regOld is passed zero and regNew is passed the
114482** first register of an array of (pTab->nCol+1) registers containing the new
114483** row data.
114484**
114485** For an UPDATE operation, this function is called twice. Once before
114486** the original record is deleted from the table using the calling convention
114487** described for DELETE. Then again after the original record is deleted
114488** but before the new record is inserted using the INSERT convention.
114489*/
114490SQLITE_PRIVATE void sqlite3FkCheck(
114491 Parse *pParse, /* Parse context */
114492 Table *pTab, /* Row is being deleted from this table */
114493 int regOld, /* Previous row data is stored here */
114494 int regNew, /* New row data is stored here */
114495 int *aChange, /* Array indicating UPDATEd columns (or 0) */
114496 int bChngRowid /* True if rowid is UPDATEd */
114497){
114498 sqlite3 *db = pParse->db; /* Database handle */
114499 FKey *pFKey; /* Used to iterate through FKs */
114500 int iDb; /* Index of database containing pTab */
114501 const char *zDb; /* Name of database containing pTab */
114502 int isIgnoreErrors = pParse->disableTriggers;
114503
114504 /* Exactly one of regOld and regNew should be non-zero. */
114505 assert( (regOld==0)!=(regNew==0) );
114506
114507 /* If foreign-keys are disabled, this function is a no-op. */
114508 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
114509
114510 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
114511 zDb = db->aDb[iDb].zDbSName;
114512
114513 /* Loop through all the foreign key constraints for which pTab is the
114514 ** child table (the table that the foreign key definition is part of). */
114515 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
114516 Table *pTo; /* Parent table of foreign key pFKey */
114517 Index *pIdx = 0; /* Index on key columns in pTo */
114518 int *aiFree = 0;
114519 int *aiCol;
114520 int iCol;
114521 int i;
114522 int bIgnore = 0;
114523
114524 if( aChange
114525 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
114526 && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
114527 ){
114528 continue;
114529 }
114530
114531 /* Find the parent table of this foreign key. Also find a unique index
114532 ** on the parent key columns in the parent table. If either of these
114533 ** schema items cannot be located, set an error in pParse and return
114534 ** early. */
114535 if( pParse->disableTriggers ){
114536 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
114537 }else{
114538 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
114539 }
114540 if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
114541 assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
114542 if( !isIgnoreErrors || db->mallocFailed ) return;
114543 if( pTo==0 ){
114544 /* If isIgnoreErrors is true, then a table is being dropped. In this
114545 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
114546 ** before actually dropping it in order to check FK constraints.
114547 ** If the parent table of an FK constraint on the current table is
114548 ** missing, behave as if it is empty. i.e. decrement the relevant
114549 ** FK counter for each row of the current table with non-NULL keys.
114550 */
114551 Vdbe *v = sqlite3GetVdbe(pParse);
114552 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
114553 for(i=0; i<pFKey->nCol; i++){
114554 int iReg = pFKey->aCol[i].iFrom + regOld + 1;
114555 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v);
114556 }
114557 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
114558 }
114559 continue;
114560 }
114561 assert( pFKey->nCol==1 || (aiFree && pIdx) );
114562
114563 if( aiFree ){
114564 aiCol = aiFree;
114565 }else{
114566 iCol = pFKey->aCol[0].iFrom;
114567 aiCol = &iCol;
114568 }
114569 for(i=0; i<pFKey->nCol; i++){
114570 if( aiCol[i]==pTab->iPKey ){
114571 aiCol[i] = -1;
114572 }
114573 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
114574#ifndef SQLITE_OMIT_AUTHORIZATION
114575 /* Request permission to read the parent key columns. If the
114576 ** authorization callback returns SQLITE_IGNORE, behave as if any
114577 ** values read from the parent table are NULL. */
114578 if( db->xAuth ){
114579 int rcauth;
114580 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
114581 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
114582 bIgnore = (rcauth==SQLITE_IGNORE);
114583 }
114584#endif
114585 }
114586
114587 /* Take a shared-cache advisory read-lock on the parent table. Allocate
114588 ** a cursor to use to search the unique index on the parent key columns
114589 ** in the parent table. */
114590 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
114591 pParse->nTab++;
114592
114593 if( regOld!=0 ){
114594 /* A row is being removed from the child table. Search for the parent.
114595 ** If the parent does not exist, removing the child row resolves an
114596 ** outstanding foreign key constraint violation. */
114597 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
114598 }
114599 if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
114600 /* A row is being added to the child table. If a parent row cannot
114601 ** be found, adding the child row has violated the FK constraint.
114602 **
114603 ** If this operation is being performed as part of a trigger program
114604 ** that is actually a "SET NULL" action belonging to this very
114605 ** foreign key, then omit this scan altogether. As all child key
114606 ** values are guaranteed to be NULL, it is not possible for adding
114607 ** this row to cause an FK violation. */
114608 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore);
114609 }
114610
114611 sqlite3DbFree(db, aiFree);
114612 }
114613
114614 /* Loop through all the foreign key constraints that refer to this table.
114615 ** (the "child" constraints) */
114616 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
114617 Index *pIdx = 0; /* Foreign key index for pFKey */
114618 SrcList *pSrc;
114619 int *aiCol = 0;
114620
114621 if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
114622 continue;
114623 }
114624
114625 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
114626 && !pParse->pToplevel && !pParse->isMultiWrite
114627 ){
114628 assert( regOld==0 && regNew!=0 );
114629 /* Inserting a single row into a parent table cannot cause (or fix)
114630 ** an immediate foreign key violation. So do nothing in this case. */
114631 continue;
114632 }
114633
114634 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
114635 if( !isIgnoreErrors || db->mallocFailed ) return;
114636 continue;
114637 }
114638 assert( aiCol || pFKey->nCol==1 );
114639
114640 /* Create a SrcList structure containing the child table. We need the
114641 ** child table as a SrcList for sqlite3WhereBegin() */
114642 pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
114643 if( pSrc ){
114644 struct SrcList_item *pItem = pSrc->a;
114645 pItem->pTab = pFKey->pFrom;
114646 pItem->zName = pFKey->pFrom->zName;
114647 pItem->pTab->nTabRef++;
114648 pItem->iCursor = pParse->nTab++;
114649
114650 if( regNew!=0 ){
114651 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
114652 }
114653 if( regOld!=0 ){
114654 int eAction = pFKey->aAction[aChange!=0];
114655 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
114656 /* If this is a deferred FK constraint, or a CASCADE or SET NULL
114657 ** action applies, then any foreign key violations caused by
114658 ** removing the parent key will be rectified by the action trigger.
114659 ** So do not set the "may-abort" flag in this case.
114660 **
114661 ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
114662 ** may-abort flag will eventually be set on this statement anyway
114663 ** (when this function is called as part of processing the UPDATE
114664 ** within the action trigger).
114665 **
114666 ** Note 2: At first glance it may seem like SQLite could simply omit
114667 ** all OP_FkCounter related scans when either CASCADE or SET NULL
114668 ** applies. The trouble starts if the CASCADE or SET NULL action
114669 ** trigger causes other triggers or action rules attached to the
114670 ** child table to fire. In these cases the fk constraint counters
114671 ** might be set incorrectly if any OP_FkCounter related scans are
114672 ** omitted. */
114673 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
114674 sqlite3MayAbort(pParse);
114675 }
114676 }
114677 pItem->zName = 0;
114678 sqlite3SrcListDelete(db, pSrc);
114679 }
114680 sqlite3DbFree(db, aiCol);
114681 }
114682}
114683
114684#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
114685
114686/*
114687** This function is called before generating code to update or delete a
114688** row contained in table pTab.
114689*/
114690SQLITE_PRIVATE u32 sqlite3FkOldmask(
114691 Parse *pParse, /* Parse context */
114692 Table *pTab /* Table being modified */
114693){
114694 u32 mask = 0;
114695 if( pParse->db->flags&SQLITE_ForeignKeys ){
114696 FKey *p;
114697 int i;
114698 for(p=pTab->pFKey; p; p=p->pNextFrom){
114699 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
114700 }
114701 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
114702 Index *pIdx = 0;
114703 sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
114704 if( pIdx ){
114705 for(i=0; i<pIdx->nKeyCol; i++){
114706 assert( pIdx->aiColumn[i]>=0 );
114707 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
114708 }
114709 }
114710 }
114711 }
114712 return mask;
114713}
114714
114715
114716/*
114717** This function is called before generating code to update or delete a
114718** row contained in table pTab. If the operation is a DELETE, then
114719** parameter aChange is passed a NULL value. For an UPDATE, aChange points
114720** to an array of size N, where N is the number of columns in table pTab.
114721** If the i'th column is not modified by the UPDATE, then the corresponding
114722** entry in the aChange[] array is set to -1. If the column is modified,
114723** the value is 0 or greater. Parameter chngRowid is set to true if the
114724** UPDATE statement modifies the rowid fields of the table.
114725**
114726** If any foreign key processing will be required, this function returns
114727** non-zero. If there is no foreign key related processing, this function
114728** returns zero.
114729**
114730** For an UPDATE, this function returns 2 if:
114731**
114732** * There are any FKs for which pTab is the child and the parent table, or
114733** * the UPDATE modifies one or more parent keys for which the action is
114734** not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL).
114735**
114736** Or, assuming some other foreign key processing is required, 1.
114737*/
114738SQLITE_PRIVATE int sqlite3FkRequired(
114739 Parse *pParse, /* Parse context */
114740 Table *pTab, /* Table being modified */
114741 int *aChange, /* Non-NULL for UPDATE operations */
114742 int chngRowid /* True for UPDATE that affects rowid */
114743){
114744 int eRet = 0;
114745 if( pParse->db->flags&SQLITE_ForeignKeys ){
114746 if( !aChange ){
114747 /* A DELETE operation. Foreign key processing is required if the
114748 ** table in question is either the child or parent table for any
114749 ** foreign key constraint. */
114750 eRet = (sqlite3FkReferences(pTab) || pTab->pFKey);
114751 }else{
114752 /* This is an UPDATE. Foreign key processing is only required if the
114753 ** operation modifies one or more child or parent key columns. */
114754 FKey *p;
114755
114756 /* Check if any child key columns are being modified. */
114757 for(p=pTab->pFKey; p; p=p->pNextFrom){
114758 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) return 2;
114759 if( fkChildIsModified(pTab, p, aChange, chngRowid) ){
114760 eRet = 1;
114761 }
114762 }
114763
114764 /* Check if any parent key columns are being modified. */
114765 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
114766 if( fkParentIsModified(pTab, p, aChange, chngRowid) ){
114767 if( p->aAction[1]!=OE_None ) return 2;
114768 eRet = 1;
114769 }
114770 }
114771 }
114772 }
114773 return eRet;
114774}
114775
114776/*
114777** This function is called when an UPDATE or DELETE operation is being
114778** compiled on table pTab, which is the parent table of foreign-key pFKey.
114779** If the current operation is an UPDATE, then the pChanges parameter is
114780** passed a pointer to the list of columns being modified. If it is a
114781** DELETE, pChanges is passed a NULL pointer.
114782**
114783** It returns a pointer to a Trigger structure containing a trigger
114784** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
114785** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
114786** returned (these actions require no special handling by the triggers
114787** sub-system, code for them is created by fkScanChildren()).
114788**
114789** For example, if pFKey is the foreign key and pTab is table "p" in
114790** the following schema:
114791**
114792** CREATE TABLE p(pk PRIMARY KEY);
114793** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
114794**
114795** then the returned trigger structure is equivalent to:
114796**
114797** CREATE TRIGGER ... DELETE ON p BEGIN
114798** DELETE FROM c WHERE ck = old.pk;
114799** END;
114800**
114801** The returned pointer is cached as part of the foreign key object. It
114802** is eventually freed along with the rest of the foreign key object by
114803** sqlite3FkDelete().
114804*/
114805static Trigger *fkActionTrigger(
114806 Parse *pParse, /* Parse context */
114807 Table *pTab, /* Table being updated or deleted from */
114808 FKey *pFKey, /* Foreign key to get action for */
114809 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
114810){
114811 sqlite3 *db = pParse->db; /* Database handle */
114812 int action; /* One of OE_None, OE_Cascade etc. */
114813 Trigger *pTrigger; /* Trigger definition to return */
114814 int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
114815
114816 action = pFKey->aAction[iAction];
114817 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
114818 return 0;
114819 }
114820 pTrigger = pFKey->apTrigger[iAction];
114821
114822 if( action!=OE_None && !pTrigger ){
114823 char const *zFrom; /* Name of child table */
114824 int nFrom; /* Length in bytes of zFrom */
114825 Index *pIdx = 0; /* Parent key index for this FK */
114826 int *aiCol = 0; /* child table cols -> parent key cols */
114827 TriggerStep *pStep = 0; /* First (only) step of trigger program */
114828 Expr *pWhere = 0; /* WHERE clause of trigger step */
114829 ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */
114830 Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */
114831 int i; /* Iterator variable */
114832 Expr *pWhen = 0; /* WHEN clause for the trigger */
114833
114834 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
114835 assert( aiCol || pFKey->nCol==1 );
114836
114837 for(i=0; i<pFKey->nCol; i++){
114838 Token tOld = { "old", 3 }; /* Literal "old" token */
114839 Token tNew = { "new", 3 }; /* Literal "new" token */
114840 Token tFromCol; /* Name of column in child table */
114841 Token tToCol; /* Name of column in parent table */
114842 int iFromCol; /* Idx of column in child table */
114843 Expr *pEq; /* tFromCol = OLD.tToCol */
114844
114845 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
114846 assert( iFromCol>=0 );
114847 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
114848 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
114849 sqlite3TokenInit(&tToCol,
114850 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName);
114851 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName);
114852
114853 /* Create the expression "OLD.zToCol = zFromCol". It is important
114854 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
114855 ** that the affinity and collation sequence associated with the
114856 ** parent table are used for the comparison. */
114857 pEq = sqlite3PExpr(pParse, TK_EQ,
114858 sqlite3PExpr(pParse, TK_DOT,
114859 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
114860 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
114861 sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0)
114862 );
114863 pWhere = sqlite3ExprAnd(db, pWhere, pEq);
114864
114865 /* For ON UPDATE, construct the next term of the WHEN clause.
114866 ** The final WHEN clause will be like this:
114867 **
114868 ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
114869 */
114870 if( pChanges ){
114871 pEq = sqlite3PExpr(pParse, TK_IS,
114872 sqlite3PExpr(pParse, TK_DOT,
114873 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
114874 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
114875 sqlite3PExpr(pParse, TK_DOT,
114876 sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
114877 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0))
114878 );
114879 pWhen = sqlite3ExprAnd(db, pWhen, pEq);
114880 }
114881
114882 if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
114883 Expr *pNew;
114884 if( action==OE_Cascade ){
114885 pNew = sqlite3PExpr(pParse, TK_DOT,
114886 sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
114887 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0));
114888 }else if( action==OE_SetDflt ){
114889 Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
114890 if( pDflt ){
114891 pNew = sqlite3ExprDup(db, pDflt, 0);
114892 }else{
114893 pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
114894 }
114895 }else{
114896 pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
114897 }
114898 pList = sqlite3ExprListAppend(pParse, pList, pNew);
114899 sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
114900 }
114901 }
114902 sqlite3DbFree(db, aiCol);
114903
114904 zFrom = pFKey->pFrom->zName;
114905 nFrom = sqlite3Strlen30(zFrom);
114906
114907 if( action==OE_Restrict ){
114908 Token tFrom;
114909 Expr *pRaise;
114910
114911 tFrom.z = zFrom;
114912 tFrom.n = nFrom;
114913 pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed");
114914 if( pRaise ){
114915 pRaise->affinity = OE_Abort;
114916 }
114917 pSelect = sqlite3SelectNew(pParse,
114918 sqlite3ExprListAppend(pParse, 0, pRaise),
114919 sqlite3SrcListAppend(db, 0, &tFrom, 0),
114920 pWhere,
114921 0, 0, 0, 0, 0
114922 );
114923 pWhere = 0;
114924 }
114925
114926 /* Disable lookaside memory allocation */
114927 db->lookaside.bDisable++;
114928
114929 pTrigger = (Trigger *)sqlite3DbMallocZero(db,
114930 sizeof(Trigger) + /* struct Trigger */
114931 sizeof(TriggerStep) + /* Single step in trigger program */
114932 nFrom + 1 /* Space for pStep->zTarget */
114933 );
114934 if( pTrigger ){
114935 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
114936 pStep->zTarget = (char *)&pStep[1];
114937 memcpy((char *)pStep->zTarget, zFrom, nFrom);
114938
114939 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
114940 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
114941 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
114942 if( pWhen ){
114943 pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0);
114944 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
114945 }
114946 }
114947
114948 /* Re-enable the lookaside buffer, if it was disabled earlier. */
114949 db->lookaside.bDisable--;
114950
114951 sqlite3ExprDelete(db, pWhere);
114952 sqlite3ExprDelete(db, pWhen);
114953 sqlite3ExprListDelete(db, pList);
114954 sqlite3SelectDelete(db, pSelect);
114955 if( db->mallocFailed==1 ){
114956 fkTriggerDelete(db, pTrigger);
114957 return 0;
114958 }
114959 assert( pStep!=0 );
114960
114961 switch( action ){
114962 case OE_Restrict:
114963 pStep->op = TK_SELECT;
114964 break;
114965 case OE_Cascade:
114966 if( !pChanges ){
114967 pStep->op = TK_DELETE;
114968 break;
114969 }
114970 default:
114971 pStep->op = TK_UPDATE;
114972 }
114973 pStep->pTrig = pTrigger;
114974 pTrigger->pSchema = pTab->pSchema;
114975 pTrigger->pTabSchema = pTab->pSchema;
114976 pFKey->apTrigger[iAction] = pTrigger;
114977 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
114978 }
114979
114980 return pTrigger;
114981}
114982
114983/*
114984** This function is called when deleting or updating a row to implement
114985** any required CASCADE, SET NULL or SET DEFAULT actions.
114986*/
114987SQLITE_PRIVATE void sqlite3FkActions(
114988 Parse *pParse, /* Parse context */
114989 Table *pTab, /* Table being updated or deleted from */
114990 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
114991 int regOld, /* Address of array containing old row */
114992 int *aChange, /* Array indicating UPDATEd columns (or 0) */
114993 int bChngRowid /* True if rowid is UPDATEd */
114994){
114995 /* If foreign-key support is enabled, iterate through all FKs that
114996 ** refer to table pTab. If there is an action associated with the FK
114997 ** for this operation (either update or delete), invoke the associated
114998 ** trigger sub-program. */
114999 if( pParse->db->flags&SQLITE_ForeignKeys ){
115000 FKey *pFKey; /* Iterator variable */
115001 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
115002 if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
115003 Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
115004 if( pAct ){
115005 sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort, 0);
115006 }
115007 }
115008 }
115009 }
115010}
115011
115012#endif /* ifndef SQLITE_OMIT_TRIGGER */
115013
115014/*
115015** Free all memory associated with foreign key definitions attached to
115016** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
115017** hash table.
115018*/
115019SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
115020 FKey *pFKey; /* Iterator variable */
115021 FKey *pNext; /* Copy of pFKey->pNextFrom */
115022
115023 assert( db==0 || IsVirtual(pTab)
115024 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
115025 for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
115026
115027 /* Remove the FK from the fkeyHash hash table. */
115028 if( !db || db->pnBytesFreed==0 ){
115029 if( pFKey->pPrevTo ){
115030 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
115031 }else{
115032 void *p = (void *)pFKey->pNextTo;
115033 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
115034 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
115035 }
115036 if( pFKey->pNextTo ){
115037 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
115038 }
115039 }
115040
115041 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
115042 ** classified as either immediate or deferred.
115043 */
115044 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
115045
115046 /* Delete any triggers created to implement actions for this FK. */
115047#ifndef SQLITE_OMIT_TRIGGER
115048 fkTriggerDelete(db, pFKey->apTrigger[0]);
115049 fkTriggerDelete(db, pFKey->apTrigger[1]);
115050#endif
115051
115052 pNext = pFKey->pNextFrom;
115053 sqlite3DbFree(db, pFKey);
115054 }
115055}
115056#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
115057
115058/************** End of fkey.c ************************************************/
115059/************** Begin file insert.c ******************************************/
115060/*
115061** 2001 September 15
115062**
115063** The author disclaims copyright to this source code. In place of
115064** a legal notice, here is a blessing:
115065**
115066** May you do good and not evil.
115067** May you find forgiveness for yourself and forgive others.
115068** May you share freely, never taking more than you give.
115069**
115070*************************************************************************
115071** This file contains C code routines that are called by the parser
115072** to handle INSERT statements in SQLite.
115073*/
115074/* #include "sqliteInt.h" */
115075
115076/*
115077** Generate code that will
115078**
115079** (1) acquire a lock for table pTab then
115080** (2) open pTab as cursor iCur.
115081**
115082** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
115083** for that table that is actually opened.
115084*/
115085SQLITE_PRIVATE void sqlite3OpenTable(
115086 Parse *pParse, /* Generate code into this VDBE */
115087 int iCur, /* The cursor number of the table */
115088 int iDb, /* The database index in sqlite3.aDb[] */
115089 Table *pTab, /* The table to be opened */
115090 int opcode /* OP_OpenRead or OP_OpenWrite */
115091){
115092 Vdbe *v;
115093 assert( !IsVirtual(pTab) );
115094 v = sqlite3GetVdbe(pParse);
115095 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
115096 sqlite3TableLock(pParse, iDb, pTab->tnum,
115097 (opcode==OP_OpenWrite)?1:0, pTab->zName);
115098 if( HasRowid(pTab) ){
115099 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
115100 VdbeComment((v, "%s", pTab->zName));
115101 }else{
115102 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
115103 assert( pPk!=0 );
115104 assert( pPk->tnum==pTab->tnum );
115105 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
115106 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
115107 VdbeComment((v, "%s", pTab->zName));
115108 }
115109}
115110
115111/*
115112** Return a pointer to the column affinity string associated with index
115113** pIdx. A column affinity string has one character for each column in
115114** the table, according to the affinity of the column:
115115**
115116** Character Column affinity
115117** ------------------------------
115118** 'A' BLOB
115119** 'B' TEXT
115120** 'C' NUMERIC
115121** 'D' INTEGER
115122** 'F' REAL
115123**
115124** An extra 'D' is appended to the end of the string to cover the
115125** rowid that appears as the last column in every index.
115126**
115127** Memory for the buffer containing the column index affinity string
115128** is managed along with the rest of the Index structure. It will be
115129** released when sqlite3DeleteIndex() is called.
115130*/
115131SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
115132 if( !pIdx->zColAff ){
115133 /* The first time a column affinity string for a particular index is
115134 ** required, it is allocated and populated here. It is then stored as
115135 ** a member of the Index structure for subsequent use.
115136 **
115137 ** The column affinity string will eventually be deleted by
115138 ** sqliteDeleteIndex() when the Index structure itself is cleaned
115139 ** up.
115140 */
115141 int n;
115142 Table *pTab = pIdx->pTable;
115143 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
115144 if( !pIdx->zColAff ){
115145 sqlite3OomFault(db);
115146 return 0;
115147 }
115148 for(n=0; n<pIdx->nColumn; n++){
115149 i16 x = pIdx->aiColumn[n];
115150 if( x>=0 ){
115151 pIdx->zColAff[n] = pTab->aCol[x].affinity;
115152 }else if( x==XN_ROWID ){
115153 pIdx->zColAff[n] = SQLITE_AFF_INTEGER;
115154 }else{
115155 char aff;
115156 assert( x==XN_EXPR );
115157 assert( pIdx->aColExpr!=0 );
115158 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
115159 if( aff==0 ) aff = SQLITE_AFF_BLOB;
115160 pIdx->zColAff[n] = aff;
115161 }
115162 }
115163 pIdx->zColAff[n] = 0;
115164 }
115165
115166 return pIdx->zColAff;
115167}
115168
115169/*
115170** Compute the affinity string for table pTab, if it has not already been
115171** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
115172**
115173** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
115174** if iReg>0 then code an OP_Affinity opcode that will set the affinities
115175** for register iReg and following. Or if affinities exists and iReg==0,
115176** then just set the P4 operand of the previous opcode (which should be
115177** an OP_MakeRecord) to the affinity string.
115178**
115179** A column affinity string has one character per column:
115180**
115181** Character Column affinity
115182** ------------------------------
115183** 'A' BLOB
115184** 'B' TEXT
115185** 'C' NUMERIC
115186** 'D' INTEGER
115187** 'E' REAL
115188*/
115189SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
115190 int i;
115191 char *zColAff = pTab->zColAff;
115192 if( zColAff==0 ){
115193 sqlite3 *db = sqlite3VdbeDb(v);
115194 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
115195 if( !zColAff ){
115196 sqlite3OomFault(db);
115197 return;
115198 }
115199
115200 for(i=0; i<pTab->nCol; i++){
115201 zColAff[i] = pTab->aCol[i].affinity;
115202 }
115203 do{
115204 zColAff[i--] = 0;
115205 }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
115206 pTab->zColAff = zColAff;
115207 }
115208 i = sqlite3Strlen30(zColAff);
115209 if( i ){
115210 if( iReg ){
115211 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
115212 }else{
115213 sqlite3VdbeChangeP4(v, -1, zColAff, i);
115214 }
115215 }
115216}
115217
115218/*
115219** Return non-zero if the table pTab in database iDb or any of its indices
115220** have been opened at any point in the VDBE program. This is used to see if
115221** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
115222** run without using a temporary table for the results of the SELECT.
115223*/
115224static int readsTable(Parse *p, int iDb, Table *pTab){
115225 Vdbe *v = sqlite3GetVdbe(p);
115226 int i;
115227 int iEnd = sqlite3VdbeCurrentAddr(v);
115228#ifndef SQLITE_OMIT_VIRTUALTABLE
115229 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
115230#endif
115231
115232 for(i=1; i<iEnd; i++){
115233 VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
115234 assert( pOp!=0 );
115235 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
115236 Index *pIndex;
115237 int tnum = pOp->p2;
115238 if( tnum==pTab->tnum ){
115239 return 1;
115240 }
115241 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
115242 if( tnum==pIndex->tnum ){
115243 return 1;
115244 }
115245 }
115246 }
115247#ifndef SQLITE_OMIT_VIRTUALTABLE
115248 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
115249 assert( pOp->p4.pVtab!=0 );
115250 assert( pOp->p4type==P4_VTAB );
115251 return 1;
115252 }
115253#endif
115254 }
115255 return 0;
115256}
115257
115258#ifndef SQLITE_OMIT_AUTOINCREMENT
115259/*
115260** Locate or create an AutoincInfo structure associated with table pTab
115261** which is in database iDb. Return the register number for the register
115262** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT
115263** table. (Also return zero when doing a VACUUM since we do not want to
115264** update the AUTOINCREMENT counters during a VACUUM.)
115265**
115266** There is at most one AutoincInfo structure per table even if the
115267** same table is autoincremented multiple times due to inserts within
115268** triggers. A new AutoincInfo structure is created if this is the
115269** first use of table pTab. On 2nd and subsequent uses, the original
115270** AutoincInfo structure is used.
115271**
115272** Four consecutive registers are allocated:
115273**
115274** (1) The name of the pTab table.
115275** (2) The maximum ROWID of pTab.
115276** (3) The rowid in sqlite_sequence of pTab
115277** (4) The original value of the max ROWID in pTab, or NULL if none
115278**
115279** The 2nd register is the one that is returned. That is all the
115280** insert routine needs to know about.
115281*/
115282static int autoIncBegin(
115283 Parse *pParse, /* Parsing context */
115284 int iDb, /* Index of the database holding pTab */
115285 Table *pTab /* The table we are writing to */
115286){
115287 int memId = 0; /* Register holding maximum rowid */
115288 assert( pParse->db->aDb[iDb].pSchema!=0 );
115289 if( (pTab->tabFlags & TF_Autoincrement)!=0
115290 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
115291 ){
115292 Parse *pToplevel = sqlite3ParseToplevel(pParse);
115293 AutoincInfo *pInfo;
115294 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
115295
115296 /* Verify that the sqlite_sequence table exists and is an ordinary
115297 ** rowid table with exactly two columns.
115298 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
115299 if( pSeqTab==0
115300 || !HasRowid(pSeqTab)
115301 || IsVirtual(pSeqTab)
115302 || pSeqTab->nCol!=2
115303 ){
115304 pParse->nErr++;
115305 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
115306 return 0;
115307 }
115308
115309 pInfo = pToplevel->pAinc;
115310 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
115311 if( pInfo==0 ){
115312 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
115313 if( pInfo==0 ) return 0;
115314 pInfo->pNext = pToplevel->pAinc;
115315 pToplevel->pAinc = pInfo;
115316 pInfo->pTab = pTab;
115317 pInfo->iDb = iDb;
115318 pToplevel->nMem++; /* Register to hold name of table */
115319 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
115320 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
115321 }
115322 memId = pInfo->regCtr;
115323 }
115324 return memId;
115325}
115326
115327/*
115328** This routine generates code that will initialize all of the
115329** register used by the autoincrement tracker.
115330*/
115331SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
115332 AutoincInfo *p; /* Information about an AUTOINCREMENT */
115333 sqlite3 *db = pParse->db; /* The database connection */
115334 Db *pDb; /* Database only autoinc table */
115335 int memId; /* Register holding max rowid */
115336 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
115337
115338 /* This routine is never called during trigger-generation. It is
115339 ** only called from the top-level */
115340 assert( pParse->pTriggerTab==0 );
115341 assert( sqlite3IsToplevel(pParse) );
115342
115343 assert( v ); /* We failed long ago if this is not so */
115344 for(p = pParse->pAinc; p; p = p->pNext){
115345 static const int iLn = VDBE_OFFSET_LINENO(2);
115346 static const VdbeOpList autoInc[] = {
115347 /* 0 */ {OP_Null, 0, 0, 0},
115348 /* 1 */ {OP_Rewind, 0, 10, 0},
115349 /* 2 */ {OP_Column, 0, 0, 0},
115350 /* 3 */ {OP_Ne, 0, 9, 0},
115351 /* 4 */ {OP_Rowid, 0, 0, 0},
115352 /* 5 */ {OP_Column, 0, 1, 0},
115353 /* 6 */ {OP_AddImm, 0, 0, 0},
115354 /* 7 */ {OP_Copy, 0, 0, 0},
115355 /* 8 */ {OP_Goto, 0, 11, 0},
115356 /* 9 */ {OP_Next, 0, 2, 0},
115357 /* 10 */ {OP_Integer, 0, 0, 0},
115358 /* 11 */ {OP_Close, 0, 0, 0}
115359 };
115360 VdbeOp *aOp;
115361 pDb = &db->aDb[p->iDb];
115362 memId = p->regCtr;
115363 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
115364 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
115365 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
115366 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
115367 if( aOp==0 ) break;
115368 aOp[0].p2 = memId;
115369 aOp[0].p3 = memId+2;
115370 aOp[2].p3 = memId;
115371 aOp[3].p1 = memId-1;
115372 aOp[3].p3 = memId;
115373 aOp[3].p5 = SQLITE_JUMPIFNULL;
115374 aOp[4].p2 = memId+1;
115375 aOp[5].p3 = memId;
115376 aOp[6].p1 = memId;
115377 aOp[7].p2 = memId+2;
115378 aOp[7].p1 = memId;
115379 aOp[10].p2 = memId;
115380 }
115381}
115382
115383/*
115384** Update the maximum rowid for an autoincrement calculation.
115385**
115386** This routine should be called when the regRowid register holds a
115387** new rowid that is about to be inserted. If that new rowid is
115388** larger than the maximum rowid in the memId memory cell, then the
115389** memory cell is updated.
115390*/
115391static void autoIncStep(Parse *pParse, int memId, int regRowid){
115392 if( memId>0 ){
115393 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
115394 }
115395}
115396
115397/*
115398** This routine generates the code needed to write autoincrement
115399** maximum rowid values back into the sqlite_sequence register.
115400** Every statement that might do an INSERT into an autoincrement
115401** table (either directly or through triggers) needs to call this
115402** routine just before the "exit" code.
115403*/
115404static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
115405 AutoincInfo *p;
115406 Vdbe *v = pParse->pVdbe;
115407 sqlite3 *db = pParse->db;
115408
115409 assert( v );
115410 for(p = pParse->pAinc; p; p = p->pNext){
115411 static const int iLn = VDBE_OFFSET_LINENO(2);
115412 static const VdbeOpList autoIncEnd[] = {
115413 /* 0 */ {OP_NotNull, 0, 2, 0},
115414 /* 1 */ {OP_NewRowid, 0, 0, 0},
115415 /* 2 */ {OP_MakeRecord, 0, 2, 0},
115416 /* 3 */ {OP_Insert, 0, 0, 0},
115417 /* 4 */ {OP_Close, 0, 0, 0}
115418 };
115419 VdbeOp *aOp;
115420 Db *pDb = &db->aDb[p->iDb];
115421 int iRec;
115422 int memId = p->regCtr;
115423
115424 iRec = sqlite3GetTempReg(pParse);
115425 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
115426 sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
115427 VdbeCoverage(v);
115428 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
115429 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
115430 if( aOp==0 ) break;
115431 aOp[0].p1 = memId+1;
115432 aOp[1].p2 = memId+1;
115433 aOp[2].p1 = memId-1;
115434 aOp[2].p3 = iRec;
115435 aOp[3].p2 = iRec;
115436 aOp[3].p3 = memId+1;
115437 aOp[3].p5 = OPFLAG_APPEND;
115438 sqlite3ReleaseTempReg(pParse, iRec);
115439 }
115440}
115441SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
115442 if( pParse->pAinc ) autoIncrementEnd(pParse);
115443}
115444#else
115445/*
115446** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
115447** above are all no-ops
115448*/
115449# define autoIncBegin(A,B,C) (0)
115450# define autoIncStep(A,B,C)
115451#endif /* SQLITE_OMIT_AUTOINCREMENT */
115452
115453
115454/* Forward declaration */
115455static int xferOptimization(
115456 Parse *pParse, /* Parser context */
115457 Table *pDest, /* The table we are inserting into */
115458 Select *pSelect, /* A SELECT statement to use as the data source */
115459 int onError, /* How to handle constraint errors */
115460 int iDbDest /* The database of pDest */
115461);
115462
115463/*
115464** This routine is called to handle SQL of the following forms:
115465**
115466** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
115467** insert into TABLE (IDLIST) select
115468** insert into TABLE (IDLIST) default values
115469**
115470** The IDLIST following the table name is always optional. If omitted,
115471** then a list of all (non-hidden) columns for the table is substituted.
115472** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST
115473** is omitted.
115474**
115475** For the pSelect parameter holds the values to be inserted for the
115476** first two forms shown above. A VALUES clause is really just short-hand
115477** for a SELECT statement that omits the FROM clause and everything else
115478** that follows. If the pSelect parameter is NULL, that means that the
115479** DEFAULT VALUES form of the INSERT statement is intended.
115480**
115481** The code generated follows one of four templates. For a simple
115482** insert with data coming from a single-row VALUES clause, the code executes
115483** once straight down through. Pseudo-code follows (we call this
115484** the "1st template"):
115485**
115486** open write cursor to <table> and its indices
115487** put VALUES clause expressions into registers
115488** write the resulting record into <table>
115489** cleanup
115490**
115491** The three remaining templates assume the statement is of the form
115492**
115493** INSERT INTO <table> SELECT ...
115494**
115495** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
115496** in other words if the SELECT pulls all columns from a single table
115497** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
115498** if <table2> and <table1> are distinct tables but have identical
115499** schemas, including all the same indices, then a special optimization
115500** is invoked that copies raw records from <table2> over to <table1>.
115501** See the xferOptimization() function for the implementation of this
115502** template. This is the 2nd template.
115503**
115504** open a write cursor to <table>
115505** open read cursor on <table2>
115506** transfer all records in <table2> over to <table>
115507** close cursors
115508** foreach index on <table>
115509** open a write cursor on the <table> index
115510** open a read cursor on the corresponding <table2> index
115511** transfer all records from the read to the write cursors
115512** close cursors
115513** end foreach
115514**
115515** The 3rd template is for when the second template does not apply
115516** and the SELECT clause does not read from <table> at any time.
115517** The generated code follows this template:
115518**
115519** X <- A
115520** goto B
115521** A: setup for the SELECT
115522** loop over the rows in the SELECT
115523** load values into registers R..R+n
115524** yield X
115525** end loop
115526** cleanup after the SELECT
115527** end-coroutine X
115528** B: open write cursor to <table> and its indices
115529** C: yield X, at EOF goto D
115530** insert the select result into <table> from R..R+n
115531** goto C
115532** D: cleanup
115533**
115534** The 4th template is used if the insert statement takes its
115535** values from a SELECT but the data is being inserted into a table
115536** that is also read as part of the SELECT. In the third form,
115537** we have to use an intermediate table to store the results of
115538** the select. The template is like this:
115539**
115540** X <- A
115541** goto B
115542** A: setup for the SELECT
115543** loop over the tables in the SELECT
115544** load value into register R..R+n
115545** yield X
115546** end loop
115547** cleanup after the SELECT
115548** end co-routine R
115549** B: open temp table
115550** L: yield X, at EOF goto M
115551** insert row from R..R+n into temp table
115552** goto L
115553** M: open write cursor to <table> and its indices
115554** rewind temp table
115555** C: loop over rows of intermediate table
115556** transfer values form intermediate table into <table>
115557** end loop
115558** D: cleanup
115559*/
115560SQLITE_PRIVATE void sqlite3Insert(
115561 Parse *pParse, /* Parser context */
115562 SrcList *pTabList, /* Name of table into which we are inserting */
115563 Select *pSelect, /* A SELECT statement to use as the data source */
115564 IdList *pColumn, /* Column names corresponding to IDLIST. */
115565 int onError, /* How to handle constraint errors */
115566 Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */
115567){
115568 sqlite3 *db; /* The main database structure */
115569 Table *pTab; /* The table to insert into. aka TABLE */
115570 int i, j; /* Loop counters */
115571 Vdbe *v; /* Generate code into this virtual machine */
115572 Index *pIdx; /* For looping over indices of the table */
115573 int nColumn; /* Number of columns in the data */
115574 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
115575 int iDataCur = 0; /* VDBE cursor that is the main data repository */
115576 int iIdxCur = 0; /* First index cursor */
115577 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
115578 int endOfLoop; /* Label for the end of the insertion loop */
115579 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
115580 int addrInsTop = 0; /* Jump to label "D" */
115581 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
115582 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
115583 int iDb; /* Index of database holding TABLE */
115584 u8 useTempTable = 0; /* Store SELECT results in intermediate table */
115585 u8 appendFlag = 0; /* True if the insert is likely to be an append */
115586 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */
115587 u8 bIdListInOrder; /* True if IDLIST is in table order */
115588 ExprList *pList = 0; /* List of VALUES() to be inserted */
115589
115590 /* Register allocations */
115591 int regFromSelect = 0;/* Base register for data coming from SELECT */
115592 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
115593 int regRowCount = 0; /* Memory cell used for the row counter */
115594 int regIns; /* Block of regs holding rowid+data being inserted */
115595 int regRowid; /* registers holding insert rowid */
115596 int regData; /* register holding first column to insert */
115597 int *aRegIdx = 0; /* One register allocated to each index */
115598
115599#ifndef SQLITE_OMIT_TRIGGER
115600 int isView; /* True if attempting to insert into a view */
115601 Trigger *pTrigger; /* List of triggers on pTab, if required */
115602 int tmask; /* Mask of trigger times */
115603#endif
115604
115605 db = pParse->db;
115606 if( pParse->nErr || db->mallocFailed ){
115607 goto insert_cleanup;
115608 }
115609 dest.iSDParm = 0; /* Suppress a harmless compiler warning */
115610
115611 /* If the Select object is really just a simple VALUES() list with a
115612 ** single row (the common case) then keep that one row of values
115613 ** and discard the other (unused) parts of the pSelect object
115614 */
115615 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
115616 pList = pSelect->pEList;
115617 pSelect->pEList = 0;
115618 sqlite3SelectDelete(db, pSelect);
115619 pSelect = 0;
115620 }
115621
115622 /* Locate the table into which we will be inserting new information.
115623 */
115624 assert( pTabList->nSrc==1 );
115625 pTab = sqlite3SrcListLookup(pParse, pTabList);
115626 if( pTab==0 ){
115627 goto insert_cleanup;
115628 }
115629 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
115630 assert( iDb<db->nDb );
115631 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
115632 db->aDb[iDb].zDbSName) ){
115633 goto insert_cleanup;
115634 }
115635 withoutRowid = !HasRowid(pTab);
115636
115637 /* Figure out if we have any triggers and if the table being
115638 ** inserted into is a view
115639 */
115640#ifndef SQLITE_OMIT_TRIGGER
115641 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
115642 isView = pTab->pSelect!=0;
115643#else
115644# define pTrigger 0
115645# define tmask 0
115646# define isView 0
115647#endif
115648#ifdef SQLITE_OMIT_VIEW
115649# undef isView
115650# define isView 0
115651#endif
115652 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
115653
115654 /* If pTab is really a view, make sure it has been initialized.
115655 ** ViewGetColumnNames() is a no-op if pTab is not a view.
115656 */
115657 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
115658 goto insert_cleanup;
115659 }
115660
115661 /* Cannot insert into a read-only table.
115662 */
115663 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
115664 goto insert_cleanup;
115665 }
115666
115667 /* Allocate a VDBE
115668 */
115669 v = sqlite3GetVdbe(pParse);
115670 if( v==0 ) goto insert_cleanup;
115671 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
115672 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
115673
115674#ifndef SQLITE_OMIT_XFER_OPT
115675 /* If the statement is of the form
115676 **
115677 ** INSERT INTO <table1> SELECT * FROM <table2>;
115678 **
115679 ** Then special optimizations can be applied that make the transfer
115680 ** very fast and which reduce fragmentation of indices.
115681 **
115682 ** This is the 2nd template.
115683 */
115684 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
115685 assert( !pTrigger );
115686 assert( pList==0 );
115687 goto insert_end;
115688 }
115689#endif /* SQLITE_OMIT_XFER_OPT */
115690
115691 /* If this is an AUTOINCREMENT table, look up the sequence number in the
115692 ** sqlite_sequence table and store it in memory cell regAutoinc.
115693 */
115694 regAutoinc = autoIncBegin(pParse, iDb, pTab);
115695
115696 /* Allocate registers for holding the rowid of the new row,
115697 ** the content of the new row, and the assembled row record.
115698 */
115699 regRowid = regIns = pParse->nMem+1;
115700 pParse->nMem += pTab->nCol + 1;
115701 if( IsVirtual(pTab) ){
115702 regRowid++;
115703 pParse->nMem++;
115704 }
115705 regData = regRowid+1;
115706
115707 /* If the INSERT statement included an IDLIST term, then make sure
115708 ** all elements of the IDLIST really are columns of the table and
115709 ** remember the column indices.
115710 **
115711 ** If the table has an INTEGER PRIMARY KEY column and that column
115712 ** is named in the IDLIST, then record in the ipkColumn variable
115713 ** the index into IDLIST of the primary key column. ipkColumn is
115714 ** the index of the primary key as it appears in IDLIST, not as
115715 ** is appears in the original table. (The index of the INTEGER
115716 ** PRIMARY KEY in the original table is pTab->iPKey.)
115717 */
115718 bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0;
115719 if( pColumn ){
115720 for(i=0; i<pColumn->nId; i++){
115721 pColumn->a[i].idx = -1;
115722 }
115723 for(i=0; i<pColumn->nId; i++){
115724 for(j=0; j<pTab->nCol; j++){
115725 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
115726 pColumn->a[i].idx = j;
115727 if( i!=j ) bIdListInOrder = 0;
115728 if( j==pTab->iPKey ){
115729 ipkColumn = i; assert( !withoutRowid );
115730 }
115731 break;
115732 }
115733 }
115734 if( j>=pTab->nCol ){
115735 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
115736 ipkColumn = i;
115737 bIdListInOrder = 0;
115738 }else{
115739 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
115740 pTabList, 0, pColumn->a[i].zName);
115741 pParse->checkSchema = 1;
115742 goto insert_cleanup;
115743 }
115744 }
115745 }
115746 }
115747
115748 /* Figure out how many columns of data are supplied. If the data
115749 ** is coming from a SELECT statement, then generate a co-routine that
115750 ** produces a single row of the SELECT on each invocation. The
115751 ** co-routine is the common header to the 3rd and 4th templates.
115752 */
115753 if( pSelect ){
115754 /* Data is coming from a SELECT or from a multi-row VALUES clause.
115755 ** Generate a co-routine to run the SELECT. */
115756 int regYield; /* Register holding co-routine entry-point */
115757 int addrTop; /* Top of the co-routine */
115758 int rc; /* Result code */
115759
115760 regYield = ++pParse->nMem;
115761 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
115762 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
115763 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
115764 dest.iSdst = bIdListInOrder ? regData : 0;
115765 dest.nSdst = pTab->nCol;
115766 rc = sqlite3Select(pParse, pSelect, &dest);
115767 regFromSelect = dest.iSdst;
115768 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
115769 sqlite3VdbeEndCoroutine(v, regYield);
115770 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
115771 assert( pSelect->pEList );
115772 nColumn = pSelect->pEList->nExpr;
115773
115774 /* Set useTempTable to TRUE if the result of the SELECT statement
115775 ** should be written into a temporary table (template 4). Set to
115776 ** FALSE if each output row of the SELECT can be written directly into
115777 ** the destination table (template 3).
115778 **
115779 ** A temp table must be used if the table being updated is also one
115780 ** of the tables being read by the SELECT statement. Also use a
115781 ** temp table in the case of row triggers.
115782 */
115783 if( pTrigger || readsTable(pParse, iDb, pTab) ){
115784 useTempTable = 1;
115785 }
115786
115787 if( useTempTable ){
115788 /* Invoke the coroutine to extract information from the SELECT
115789 ** and add it to a transient table srcTab. The code generated
115790 ** here is from the 4th template:
115791 **
115792 ** B: open temp table
115793 ** L: yield X, goto M at EOF
115794 ** insert row from R..R+n into temp table
115795 ** goto L
115796 ** M: ...
115797 */
115798 int regRec; /* Register to hold packed record */
115799 int regTempRowid; /* Register to hold temp table ROWID */
115800 int addrL; /* Label "L" */
115801
115802 srcTab = pParse->nTab++;
115803 regRec = sqlite3GetTempReg(pParse);
115804 regTempRowid = sqlite3GetTempReg(pParse);
115805 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
115806 addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
115807 sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
115808 sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
115809 sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
115810 sqlite3VdbeGoto(v, addrL);
115811 sqlite3VdbeJumpHere(v, addrL);
115812 sqlite3ReleaseTempReg(pParse, regRec);
115813 sqlite3ReleaseTempReg(pParse, regTempRowid);
115814 }
115815 }else{
115816 /* This is the case if the data for the INSERT is coming from a
115817 ** single-row VALUES clause
115818 */
115819 NameContext sNC;
115820 memset(&sNC, 0, sizeof(sNC));
115821 sNC.pParse = pParse;
115822 srcTab = -1;
115823 assert( useTempTable==0 );
115824 if( pList ){
115825 nColumn = pList->nExpr;
115826 if( sqlite3ResolveExprListNames(&sNC, pList) ){
115827 goto insert_cleanup;
115828 }
115829 }else{
115830 nColumn = 0;
115831 }
115832 }
115833
115834 /* If there is no IDLIST term but the table has an integer primary
115835 ** key, the set the ipkColumn variable to the integer primary key
115836 ** column index in the original table definition.
115837 */
115838 if( pColumn==0 && nColumn>0 ){
115839 ipkColumn = pTab->iPKey;
115840 }
115841
115842 /* Make sure the number of columns in the source data matches the number
115843 ** of columns to be inserted into the table.
115844 */
115845 for(i=0; i<pTab->nCol; i++){
115846 nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
115847 }
115848 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
115849 sqlite3ErrorMsg(pParse,
115850 "table %S has %d columns but %d values were supplied",
115851 pTabList, 0, pTab->nCol-nHidden, nColumn);
115852 goto insert_cleanup;
115853 }
115854 if( pColumn!=0 && nColumn!=pColumn->nId ){
115855 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
115856 goto insert_cleanup;
115857 }
115858
115859 /* Initialize the count of rows to be inserted
115860 */
115861 if( (db->flags & SQLITE_CountRows)!=0
115862 && !pParse->nested
115863 && !pParse->pTriggerTab
115864 ){
115865 regRowCount = ++pParse->nMem;
115866 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
115867 }
115868
115869 /* If this is not a view, open the table and and all indices */
115870 if( !isView ){
115871 int nIdx;
115872 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
115873 &iDataCur, &iIdxCur);
115874 aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1));
115875 if( aRegIdx==0 ){
115876 goto insert_cleanup;
115877 }
115878 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
115879 assert( pIdx );
115880 aRegIdx[i] = ++pParse->nMem;
115881 pParse->nMem += pIdx->nColumn;
115882 }
115883 }
115884#ifndef SQLITE_OMIT_UPSERT
115885 if( pUpsert ){
115886 pTabList->a[0].iCursor = iDataCur;
115887 pUpsert->pUpsertSrc = pTabList;
115888 pUpsert->regData = regData;
115889 pUpsert->iDataCur = iDataCur;
115890 pUpsert->iIdxCur = iIdxCur;
115891 if( pUpsert->pUpsertTarget ){
115892 sqlite3UpsertAnalyzeTarget(pParse, pTabList, pUpsert);
115893 }
115894 }
115895#endif
115896
115897
115898 /* This is the top of the main insertion loop */
115899 if( useTempTable ){
115900 /* This block codes the top of loop only. The complete loop is the
115901 ** following pseudocode (template 4):
115902 **
115903 ** rewind temp table, if empty goto D
115904 ** C: loop over rows of intermediate table
115905 ** transfer values form intermediate table into <table>
115906 ** end loop
115907 ** D: ...
115908 */
115909 addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
115910 addrCont = sqlite3VdbeCurrentAddr(v);
115911 }else if( pSelect ){
115912 /* This block codes the top of loop only. The complete loop is the
115913 ** following pseudocode (template 3):
115914 **
115915 ** C: yield X, at EOF goto D
115916 ** insert the select result into <table> from R..R+n
115917 ** goto C
115918 ** D: ...
115919 */
115920 addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
115921 VdbeCoverage(v);
115922 }
115923
115924 /* Run the BEFORE and INSTEAD OF triggers, if there are any
115925 */
115926 endOfLoop = sqlite3VdbeMakeLabel(v);
115927 if( tmask & TRIGGER_BEFORE ){
115928 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
115929
115930 /* build the NEW.* reference row. Note that if there is an INTEGER
115931 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
115932 ** translated into a unique ID for the row. But on a BEFORE trigger,
115933 ** we do not know what the unique ID will be (because the insert has
115934 ** not happened yet) so we substitute a rowid of -1
115935 */
115936 if( ipkColumn<0 ){
115937 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
115938 }else{
115939 int addr1;
115940 assert( !withoutRowid );
115941 if( useTempTable ){
115942 sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
115943 }else{
115944 assert( pSelect==0 ); /* Otherwise useTempTable is true */
115945 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
115946 }
115947 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
115948 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
115949 sqlite3VdbeJumpHere(v, addr1);
115950 sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
115951 }
115952
115953 /* Cannot have triggers on a virtual table. If it were possible,
115954 ** this block would have to account for hidden column.
115955 */
115956 assert( !IsVirtual(pTab) );
115957
115958 /* Create the new column data
115959 */
115960 for(i=j=0; i<pTab->nCol; i++){
115961 if( pColumn ){
115962 for(j=0; j<pColumn->nId; j++){
115963 if( pColumn->a[j].idx==i ) break;
115964 }
115965 }
115966 if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId)
115967 || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){
115968 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
115969 }else if( useTempTable ){
115970 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
115971 }else{
115972 assert( pSelect==0 ); /* Otherwise useTempTable is true */
115973 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
115974 }
115975 if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++;
115976 }
115977
115978 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
115979 ** do not attempt any conversions before assembling the record.
115980 ** If this is a real table, attempt conversions as required by the
115981 ** table column affinities.
115982 */
115983 if( !isView ){
115984 sqlite3TableAffinity(v, pTab, regCols+1);
115985 }
115986
115987 /* Fire BEFORE or INSTEAD OF triggers */
115988 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
115989 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
115990
115991 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
115992 }
115993
115994 /* Compute the content of the next row to insert into a range of
115995 ** registers beginning at regIns.
115996 */
115997 if( !isView ){
115998 if( IsVirtual(pTab) ){
115999 /* The row that the VUpdate opcode will delete: none */
116000 sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
116001 }
116002 if( ipkColumn>=0 ){
116003 if( useTempTable ){
116004 sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
116005 }else if( pSelect ){
116006 sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
116007 }else{
116008 VdbeOp *pOp;
116009 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
116010 pOp = sqlite3VdbeGetOp(v, -1);
116011 assert( pOp!=0 );
116012 if( pOp->opcode==OP_Null && !IsVirtual(pTab) ){
116013 appendFlag = 1;
116014 pOp->opcode = OP_NewRowid;
116015 pOp->p1 = iDataCur;
116016 pOp->p2 = regRowid;
116017 pOp->p3 = regAutoinc;
116018 }
116019 }
116020 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
116021 ** to generate a unique primary key value.
116022 */
116023 if( !appendFlag ){
116024 int addr1;
116025 if( !IsVirtual(pTab) ){
116026 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
116027 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
116028 sqlite3VdbeJumpHere(v, addr1);
116029 }else{
116030 addr1 = sqlite3VdbeCurrentAddr(v);
116031 sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v);
116032 }
116033 sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
116034 }
116035 }else if( IsVirtual(pTab) || withoutRowid ){
116036 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
116037 }else{
116038 sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
116039 appendFlag = 1;
116040 }
116041 autoIncStep(pParse, regAutoinc, regRowid);
116042
116043 /* Compute data for all columns of the new entry, beginning
116044 ** with the first column.
116045 */
116046 nHidden = 0;
116047 for(i=0; i<pTab->nCol; i++){
116048 int iRegStore = regRowid+1+i;
116049 if( i==pTab->iPKey ){
116050 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
116051 ** Whenever this column is read, the rowid will be substituted
116052 ** in its place. Hence, fill this column with a NULL to avoid
116053 ** taking up data space with information that will never be used.
116054 ** As there may be shallow copies of this value, make it a soft-NULL */
116055 sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
116056 continue;
116057 }
116058 if( pColumn==0 ){
116059 if( IsHiddenColumn(&pTab->aCol[i]) ){
116060 j = -1;
116061 nHidden++;
116062 }else{
116063 j = i - nHidden;
116064 }
116065 }else{
116066 for(j=0; j<pColumn->nId; j++){
116067 if( pColumn->a[j].idx==i ) break;
116068 }
116069 }
116070 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
116071 sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
116072 }else if( useTempTable ){
116073 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
116074 }else if( pSelect ){
116075 if( regFromSelect!=regData ){
116076 sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
116077 }
116078 }else{
116079 sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
116080 }
116081 }
116082
116083 /* Generate code to check constraints and generate index keys and
116084 ** do the insertion.
116085 */
116086#ifndef SQLITE_OMIT_VIRTUALTABLE
116087 if( IsVirtual(pTab) ){
116088 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
116089 sqlite3VtabMakeWritable(pParse, pTab);
116090 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
116091 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
116092 sqlite3MayAbort(pParse);
116093 }else
116094#endif
116095 {
116096 int isReplace; /* Set to true if constraints may cause a replace */
116097 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
116098 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
116099 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
116100 );
116101 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
116102
116103 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
116104 ** constraints or (b) there are no triggers and this table is not a
116105 ** parent table in a foreign key constraint. It is safe to set the
116106 ** flag in the second case as if any REPLACE constraint is hit, an
116107 ** OP_Delete or OP_IdxDelete instruction will be executed on each
116108 ** cursor that is disturbed. And these instructions both clear the
116109 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
116110 ** functionality. */
116111 bUseSeek = (isReplace==0 || (pTrigger==0 &&
116112 ((db->flags & SQLITE_ForeignKeys)==0 || sqlite3FkReferences(pTab)==0)
116113 ));
116114 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
116115 regIns, aRegIdx, 0, appendFlag, bUseSeek
116116 );
116117 }
116118 }
116119
116120 /* Update the count of rows that are inserted
116121 */
116122 if( regRowCount ){
116123 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
116124 }
116125
116126 if( pTrigger ){
116127 /* Code AFTER triggers */
116128 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
116129 pTab, regData-2-pTab->nCol, onError, endOfLoop);
116130 }
116131
116132 /* The bottom of the main insertion loop, if the data source
116133 ** is a SELECT statement.
116134 */
116135 sqlite3VdbeResolveLabel(v, endOfLoop);
116136 if( useTempTable ){
116137 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
116138 sqlite3VdbeJumpHere(v, addrInsTop);
116139 sqlite3VdbeAddOp1(v, OP_Close, srcTab);
116140 }else if( pSelect ){
116141 sqlite3VdbeGoto(v, addrCont);
116142 sqlite3VdbeJumpHere(v, addrInsTop);
116143 }
116144
116145insert_end:
116146 /* Update the sqlite_sequence table by storing the content of the
116147 ** maximum rowid counter values recorded while inserting into
116148 ** autoincrement tables.
116149 */
116150 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
116151 sqlite3AutoincrementEnd(pParse);
116152 }
116153
116154 /*
116155 ** Return the number of rows inserted. If this routine is
116156 ** generating code because of a call to sqlite3NestedParse(), do not
116157 ** invoke the callback function.
116158 */
116159 if( regRowCount ){
116160 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
116161 sqlite3VdbeSetNumCols(v, 1);
116162 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
116163 }
116164
116165insert_cleanup:
116166 sqlite3SrcListDelete(db, pTabList);
116167 sqlite3ExprListDelete(db, pList);
116168 sqlite3UpsertDelete(db, pUpsert);
116169 sqlite3SelectDelete(db, pSelect);
116170 sqlite3IdListDelete(db, pColumn);
116171 sqlite3DbFree(db, aRegIdx);
116172}
116173
116174/* Make sure "isView" and other macros defined above are undefined. Otherwise
116175** they may interfere with compilation of other functions in this file
116176** (or in another file, if this file becomes part of the amalgamation). */
116177#ifdef isView
116178 #undef isView
116179#endif
116180#ifdef pTrigger
116181 #undef pTrigger
116182#endif
116183#ifdef tmask
116184 #undef tmask
116185#endif
116186
116187/*
116188** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged()
116189*/
116190#define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */
116191#define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */
116192
116193/* This is the Walker callback from checkConstraintUnchanged(). Set
116194** bit 0x01 of pWalker->eCode if
116195** pWalker->eCode to 0 if this expression node references any of the
116196** columns that are being modifed by an UPDATE statement.
116197*/
116198static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
116199 if( pExpr->op==TK_COLUMN ){
116200 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
116201 if( pExpr->iColumn>=0 ){
116202 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
116203 pWalker->eCode |= CKCNSTRNT_COLUMN;
116204 }
116205 }else{
116206 pWalker->eCode |= CKCNSTRNT_ROWID;
116207 }
116208 }
116209 return WRC_Continue;
116210}
116211
116212/*
116213** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
116214** only columns that are modified by the UPDATE are those for which
116215** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
116216**
116217** Return true if CHECK constraint pExpr does not use any of the
116218** changing columns (or the rowid if it is changing). In other words,
116219** return true if this CHECK constraint can be skipped when validating
116220** the new row in the UPDATE statement.
116221*/
116222static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){
116223 Walker w;
116224 memset(&w, 0, sizeof(w));
116225 w.eCode = 0;
116226 w.xExprCallback = checkConstraintExprNode;
116227 w.u.aiCol = aiChng;
116228 sqlite3WalkExpr(&w, pExpr);
116229 if( !chngRowid ){
116230 testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
116231 w.eCode &= ~CKCNSTRNT_ROWID;
116232 }
116233 testcase( w.eCode==0 );
116234 testcase( w.eCode==CKCNSTRNT_COLUMN );
116235 testcase( w.eCode==CKCNSTRNT_ROWID );
116236 testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
116237 return !w.eCode;
116238}
116239
116240/*
116241** Generate code to do constraint checks prior to an INSERT or an UPDATE
116242** on table pTab.
116243**
116244** The regNewData parameter is the first register in a range that contains
116245** the data to be inserted or the data after the update. There will be
116246** pTab->nCol+1 registers in this range. The first register (the one
116247** that regNewData points to) will contain the new rowid, or NULL in the
116248** case of a WITHOUT ROWID table. The second register in the range will
116249** contain the content of the first table column. The third register will
116250** contain the content of the second table column. And so forth.
116251**
116252** The regOldData parameter is similar to regNewData except that it contains
116253** the data prior to an UPDATE rather than afterwards. regOldData is zero
116254** for an INSERT. This routine can distinguish between UPDATE and INSERT by
116255** checking regOldData for zero.
116256**
116257** For an UPDATE, the pkChng boolean is true if the true primary key (the
116258** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
116259** might be modified by the UPDATE. If pkChng is false, then the key of
116260** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
116261**
116262** For an INSERT, the pkChng boolean indicates whether or not the rowid
116263** was explicitly specified as part of the INSERT statement. If pkChng
116264** is zero, it means that the either rowid is computed automatically or
116265** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
116266** pkChng will only be true if the INSERT statement provides an integer
116267** value for either the rowid column or its INTEGER PRIMARY KEY alias.
116268**
116269** The code generated by this routine will store new index entries into
116270** registers identified by aRegIdx[]. No index entry is created for
116271** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
116272** the same as the order of indices on the linked list of indices
116273** at pTab->pIndex.
116274**
116275** The caller must have already opened writeable cursors on the main
116276** table and all applicable indices (that is to say, all indices for which
116277** aRegIdx[] is not zero). iDataCur is the cursor for the main table when
116278** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
116279** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
116280** for the first index in the pTab->pIndex list. Cursors for other indices
116281** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
116282**
116283** This routine also generates code to check constraints. NOT NULL,
116284** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
116285** then the appropriate action is performed. There are five possible
116286** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
116287**
116288** Constraint type Action What Happens
116289** --------------- ---------- ----------------------------------------
116290** any ROLLBACK The current transaction is rolled back and
116291** sqlite3_step() returns immediately with a
116292** return code of SQLITE_CONSTRAINT.
116293**
116294** any ABORT Back out changes from the current command
116295** only (do not do a complete rollback) then
116296** cause sqlite3_step() to return immediately
116297** with SQLITE_CONSTRAINT.
116298**
116299** any FAIL Sqlite3_step() returns immediately with a
116300** return code of SQLITE_CONSTRAINT. The
116301** transaction is not rolled back and any
116302** changes to prior rows are retained.
116303**
116304** any IGNORE The attempt in insert or update the current
116305** row is skipped, without throwing an error.
116306** Processing continues with the next row.
116307** (There is an immediate jump to ignoreDest.)
116308**
116309** NOT NULL REPLACE The NULL value is replace by the default
116310** value for that column. If the default value
116311** is NULL, the action is the same as ABORT.
116312**
116313** UNIQUE REPLACE The other row that conflicts with the row
116314** being inserted is removed.
116315**
116316** CHECK REPLACE Illegal. The results in an exception.
116317**
116318** Which action to take is determined by the overrideError parameter.
116319** Or if overrideError==OE_Default, then the pParse->onError parameter
116320** is used. Or if pParse->onError==OE_Default then the onError value
116321** for the constraint is used.
116322*/
116323SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
116324 Parse *pParse, /* The parser context */
116325 Table *pTab, /* The table being inserted or updated */
116326 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */
116327 int iDataCur, /* Canonical data cursor (main table or PK index) */
116328 int iIdxCur, /* First index cursor */
116329 int regNewData, /* First register in a range holding values to insert */
116330 int regOldData, /* Previous content. 0 for INSERTs */
116331 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
116332 u8 overrideError, /* Override onError to this if not OE_Default */
116333 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
116334 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
116335 int *aiChng, /* column i is unchanged if aiChng[i]<0 */
116336 Upsert *pUpsert /* ON CONFLICT clauses, if any. NULL otherwise */
116337){
116338 Vdbe *v; /* VDBE under constrution */
116339 Index *pIdx; /* Pointer to one of the indices */
116340 Index *pPk = 0; /* The PRIMARY KEY index */
116341 sqlite3 *db; /* Database connection */
116342 int i; /* loop counter */
116343 int ix; /* Index loop counter */
116344 int nCol; /* Number of columns */
116345 int onError; /* Conflict resolution strategy */
116346 int addr1; /* Address of jump instruction */
116347 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
116348 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
116349 Index *pUpIdx = 0; /* Index to which to apply the upsert */
116350 u8 isUpdate; /* True if this is an UPDATE operation */
116351 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */
116352 int upsertBypass = 0; /* Address of Goto to bypass upsert subroutine */
116353 int upsertJump = 0; /* Address of Goto that jumps into upsert subroutine */
116354 int ipkTop = 0; /* Top of the IPK uniqueness check */
116355 int ipkBottom = 0; /* OP_Goto at the end of the IPK uniqueness check */
116356
116357 isUpdate = regOldData!=0;
116358 db = pParse->db;
116359 v = sqlite3GetVdbe(pParse);
116360 assert( v!=0 );
116361 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
116362 nCol = pTab->nCol;
116363
116364 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
116365 ** normal rowid tables. nPkField is the number of key fields in the
116366 ** pPk index or 1 for a rowid table. In other words, nPkField is the
116367 ** number of fields in the true primary key of the table. */
116368 if( HasRowid(pTab) ){
116369 pPk = 0;
116370 nPkField = 1;
116371 }else{
116372 pPk = sqlite3PrimaryKeyIndex(pTab);
116373 nPkField = pPk->nKeyCol;
116374 }
116375
116376 /* Record that this module has started */
116377 VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
116378 iDataCur, iIdxCur, regNewData, regOldData, pkChng));
116379
116380 /* Test all NOT NULL constraints.
116381 */
116382 for(i=0; i<nCol; i++){
116383 if( i==pTab->iPKey ){
116384 continue; /* ROWID is never NULL */
116385 }
116386 if( aiChng && aiChng[i]<0 ){
116387 /* Don't bother checking for NOT NULL on columns that do not change */
116388 continue;
116389 }
116390 onError = pTab->aCol[i].notNull;
116391 if( onError==OE_None ) continue; /* This column is allowed to be NULL */
116392 if( overrideError!=OE_Default ){
116393 onError = overrideError;
116394 }else if( onError==OE_Default ){
116395 onError = OE_Abort;
116396 }
116397 if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
116398 onError = OE_Abort;
116399 }
116400 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
116401 || onError==OE_Ignore || onError==OE_Replace );
116402 switch( onError ){
116403 case OE_Abort:
116404 sqlite3MayAbort(pParse);
116405 /* Fall through */
116406 case OE_Rollback:
116407 case OE_Fail: {
116408 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
116409 pTab->aCol[i].zName);
116410 sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
116411 regNewData+1+i);
116412 sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
116413 sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
116414 VdbeCoverage(v);
116415 break;
116416 }
116417 case OE_Ignore: {
116418 sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
116419 VdbeCoverage(v);
116420 break;
116421 }
116422 default: {
116423 assert( onError==OE_Replace );
116424 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i);
116425 VdbeCoverage(v);
116426 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
116427 sqlite3VdbeJumpHere(v, addr1);
116428 break;
116429 }
116430 }
116431 }
116432
116433 /* Test all CHECK constraints
116434 */
116435#ifndef SQLITE_OMIT_CHECK
116436 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
116437 ExprList *pCheck = pTab->pCheck;
116438 pParse->iSelfTab = -(regNewData+1);
116439 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
116440 for(i=0; i<pCheck->nExpr; i++){
116441 int allOk;
116442 Expr *pExpr = pCheck->a[i].pExpr;
116443 if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
116444 allOk = sqlite3VdbeMakeLabel(v);
116445 sqlite3VdbeVerifyAbortable(v, onError);
116446 sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
116447 if( onError==OE_Ignore ){
116448 sqlite3VdbeGoto(v, ignoreDest);
116449 }else{
116450 char *zName = pCheck->a[i].zName;
116451 if( zName==0 ) zName = pTab->zName;
116452 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
116453 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
116454 onError, zName, P4_TRANSIENT,
116455 P5_ConstraintCheck);
116456 }
116457 sqlite3VdbeResolveLabel(v, allOk);
116458 }
116459 pParse->iSelfTab = 0;
116460 }
116461#endif /* !defined(SQLITE_OMIT_CHECK) */
116462
116463 /* UNIQUE and PRIMARY KEY constraints should be handled in the following
116464 ** order:
116465 **
116466 ** (1) OE_Update
116467 ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
116468 ** (3) OE_Replace
116469 **
116470 ** OE_Fail and OE_Ignore must happen before any changes are made.
116471 ** OE_Update guarantees that only a single row will change, so it
116472 ** must happen before OE_Replace. Technically, OE_Abort and OE_Rollback
116473 ** could happen in any order, but they are grouped up front for
116474 ** convenience.
116475 **
116476 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
116477 ** The order of constraints used to have OE_Update as (2) and OE_Abort
116478 ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
116479 ** constraint before any others, so it had to be moved.
116480 **
116481 ** Constraint checking code is generated in this order:
116482 ** (A) The rowid constraint
116483 ** (B) Unique index constraints that do not have OE_Replace as their
116484 ** default conflict resolution strategy
116485 ** (C) Unique index that do use OE_Replace by default.
116486 **
116487 ** The ordering of (2) and (3) is accomplished by making sure the linked
116488 ** list of indexes attached to a table puts all OE_Replace indexes last
116489 ** in the list. See sqlite3CreateIndex() for where that happens.
116490 */
116491
116492 if( pUpsert ){
116493 if( pUpsert->pUpsertTarget==0 ){
116494 /* An ON CONFLICT DO NOTHING clause, without a constraint-target.
116495 ** Make all unique constraint resolution be OE_Ignore */
116496 assert( pUpsert->pUpsertSet==0 );
116497 overrideError = OE_Ignore;
116498 pUpsert = 0;
116499 }else if( (pUpIdx = pUpsert->pUpsertIdx)!=0 ){
116500 /* If the constraint-target uniqueness check must be run first.
116501 ** Jump to that uniqueness check now */
116502 upsertJump = sqlite3VdbeAddOp0(v, OP_Goto);
116503 VdbeComment((v, "UPSERT constraint goes first"));
116504 }
116505 }
116506
116507 /* If rowid is changing, make sure the new rowid does not previously
116508 ** exist in the table.
116509 */
116510 if( pkChng && pPk==0 ){
116511 int addrRowidOk = sqlite3VdbeMakeLabel(v);
116512
116513 /* Figure out what action to take in case of a rowid collision */
116514 onError = pTab->keyConf;
116515 if( overrideError!=OE_Default ){
116516 onError = overrideError;
116517 }else if( onError==OE_Default ){
116518 onError = OE_Abort;
116519 }
116520
116521 /* figure out whether or not upsert applies in this case */
116522 if( pUpsert && pUpsert->pUpsertIdx==0 ){
116523 if( pUpsert->pUpsertSet==0 ){
116524 onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */
116525 }else{
116526 onError = OE_Update; /* DO UPDATE */
116527 }
116528 }
116529
116530 /* If the response to a rowid conflict is REPLACE but the response
116531 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
116532 ** to defer the running of the rowid conflict checking until after
116533 ** the UNIQUE constraints have run.
116534 */
116535 if( onError==OE_Replace /* IPK rule is REPLACE */
116536 && onError!=overrideError /* Rules for other contraints are different */
116537 && pTab->pIndex /* There exist other constraints */
116538 ){
116539 ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
116540 VdbeComment((v, "defer IPK REPLACE until last"));
116541 }
116542
116543 if( isUpdate ){
116544 /* pkChng!=0 does not mean that the rowid has changed, only that
116545 ** it might have changed. Skip the conflict logic below if the rowid
116546 ** is unchanged. */
116547 sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
116548 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
116549 VdbeCoverage(v);
116550 }
116551
116552 /* Check to see if the new rowid already exists in the table. Skip
116553 ** the following conflict logic if it does not. */
116554 VdbeNoopComment((v, "uniqueness check for ROWID"));
116555 sqlite3VdbeVerifyAbortable(v, onError);
116556 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
116557 VdbeCoverage(v);
116558
116559 switch( onError ){
116560 default: {
116561 onError = OE_Abort;
116562 /* Fall thru into the next case */
116563 }
116564 case OE_Rollback:
116565 case OE_Abort:
116566 case OE_Fail: {
116567 testcase( onError==OE_Rollback );
116568 testcase( onError==OE_Abort );
116569 testcase( onError==OE_Fail );
116570 sqlite3RowidConstraint(pParse, onError, pTab);
116571 break;
116572 }
116573 case OE_Replace: {
116574 /* If there are DELETE triggers on this table and the
116575 ** recursive-triggers flag is set, call GenerateRowDelete() to
116576 ** remove the conflicting row from the table. This will fire
116577 ** the triggers and remove both the table and index b-tree entries.
116578 **
116579 ** Otherwise, if there are no triggers or the recursive-triggers
116580 ** flag is not set, but the table has one or more indexes, call
116581 ** GenerateRowIndexDelete(). This removes the index b-tree entries
116582 ** only. The table b-tree entry will be replaced by the new entry
116583 ** when it is inserted.
116584 **
116585 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
116586 ** also invoke MultiWrite() to indicate that this VDBE may require
116587 ** statement rollback (if the statement is aborted after the delete
116588 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
116589 ** but being more selective here allows statements like:
116590 **
116591 ** REPLACE INTO t(rowid) VALUES($newrowid)
116592 **
116593 ** to run without a statement journal if there are no indexes on the
116594 ** table.
116595 */
116596 Trigger *pTrigger = 0;
116597 if( db->flags&SQLITE_RecTriggers ){
116598 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
116599 }
116600 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
116601 sqlite3MultiWrite(pParse);
116602 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
116603 regNewData, 1, 0, OE_Replace, 1, -1);
116604 }else{
116605#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
116606 assert( HasRowid(pTab) );
116607 /* This OP_Delete opcode fires the pre-update-hook only. It does
116608 ** not modify the b-tree. It is more efficient to let the coming
116609 ** OP_Insert replace the existing entry than it is to delete the
116610 ** existing entry and then insert a new one. */
116611 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
116612 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
116613#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
116614 if( pTab->pIndex ){
116615 sqlite3MultiWrite(pParse);
116616 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
116617 }
116618 }
116619 seenReplace = 1;
116620 break;
116621 }
116622#ifndef SQLITE_OMIT_UPSERT
116623 case OE_Update: {
116624 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
116625 /* Fall through */
116626 }
116627#endif
116628 case OE_Ignore: {
116629 testcase( onError==OE_Ignore );
116630 sqlite3VdbeGoto(v, ignoreDest);
116631 break;
116632 }
116633 }
116634 sqlite3VdbeResolveLabel(v, addrRowidOk);
116635 if( ipkTop ){
116636 ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
116637 sqlite3VdbeJumpHere(v, ipkTop-1);
116638 }
116639 }
116640
116641 /* Test all UNIQUE constraints by creating entries for each UNIQUE
116642 ** index and making sure that duplicate entries do not already exist.
116643 ** Compute the revised record entries for indices as we go.
116644 **
116645 ** This loop also handles the case of the PRIMARY KEY index for a
116646 ** WITHOUT ROWID table.
116647 */
116648 for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
116649 int regIdx; /* Range of registers hold conent for pIdx */
116650 int regR; /* Range of registers holding conflicting PK */
116651 int iThisCur; /* Cursor for this UNIQUE index */
116652 int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */
116653
116654 if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */
116655 if( pUpIdx==pIdx ){
116656 addrUniqueOk = upsertJump+1;
116657 upsertBypass = sqlite3VdbeGoto(v, 0);
116658 VdbeComment((v, "Skip upsert subroutine"));
116659 sqlite3VdbeJumpHere(v, upsertJump);
116660 }else{
116661 addrUniqueOk = sqlite3VdbeMakeLabel(v);
116662 }
116663 if( bAffinityDone==0 && (pUpIdx==0 || pUpIdx==pIdx) ){
116664 sqlite3TableAffinity(v, pTab, regNewData+1);
116665 bAffinityDone = 1;
116666 }
116667 VdbeNoopComment((v, "uniqueness check for %s", pIdx->zName));
116668 iThisCur = iIdxCur+ix;
116669
116670
116671 /* Skip partial indices for which the WHERE clause is not true */
116672 if( pIdx->pPartIdxWhere ){
116673 sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
116674 pParse->iSelfTab = -(regNewData+1);
116675 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
116676 SQLITE_JUMPIFNULL);
116677 pParse->iSelfTab = 0;
116678 }
116679
116680 /* Create a record for this index entry as it should appear after
116681 ** the insert or update. Store that record in the aRegIdx[ix] register
116682 */
116683 regIdx = aRegIdx[ix]+1;
116684 for(i=0; i<pIdx->nColumn; i++){
116685 int iField = pIdx->aiColumn[i];
116686 int x;
116687 if( iField==XN_EXPR ){
116688 pParse->iSelfTab = -(regNewData+1);
116689 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
116690 pParse->iSelfTab = 0;
116691 VdbeComment((v, "%s column %d", pIdx->zName, i));
116692 }else{
116693 if( iField==XN_ROWID || iField==pTab->iPKey ){
116694 x = regNewData;
116695 }else{
116696 x = iField + regNewData + 1;
116697 }
116698 sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i);
116699 VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
116700 }
116701 }
116702 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
116703 VdbeComment((v, "for %s", pIdx->zName));
116704#ifdef SQLITE_ENABLE_NULL_TRIM
116705 if( pIdx->idxType==2 ) sqlite3SetMakeRecordP5(v, pIdx->pTable);
116706#endif
116707
116708 /* In an UPDATE operation, if this index is the PRIMARY KEY index
116709 ** of a WITHOUT ROWID table and there has been no change the
116710 ** primary key, then no collision is possible. The collision detection
116711 ** logic below can all be skipped. */
116712 if( isUpdate && pPk==pIdx && pkChng==0 ){
116713 sqlite3VdbeResolveLabel(v, addrUniqueOk);
116714 continue;
116715 }
116716
116717 /* Find out what action to take in case there is a uniqueness conflict */
116718 onError = pIdx->onError;
116719 if( onError==OE_None ){
116720 sqlite3VdbeResolveLabel(v, addrUniqueOk);
116721 continue; /* pIdx is not a UNIQUE index */
116722 }
116723 if( overrideError!=OE_Default ){
116724 onError = overrideError;
116725 }else if( onError==OE_Default ){
116726 onError = OE_Abort;
116727 }
116728
116729 /* Figure out if the upsert clause applies to this index */
116730 if( pUpIdx==pIdx ){
116731 if( pUpsert->pUpsertSet==0 ){
116732 onError = OE_Ignore; /* DO NOTHING is the same as INSERT OR IGNORE */
116733 }else{
116734 onError = OE_Update; /* DO UPDATE */
116735 }
116736 }
116737
116738 /* Collision detection may be omitted if all of the following are true:
116739 ** (1) The conflict resolution algorithm is REPLACE
116740 ** (2) The table is a WITHOUT ROWID table
116741 ** (3) There are no secondary indexes on the table
116742 ** (4) No delete triggers need to be fired if there is a conflict
116743 ** (5) No FK constraint counters need to be updated if a conflict occurs.
116744 */
116745 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
116746 && pPk==pIdx /* Condition 2 */
116747 && onError==OE_Replace /* Condition 1 */
116748 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
116749 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0))
116750 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
116751 (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
116752 ){
116753 sqlite3VdbeResolveLabel(v, addrUniqueOk);
116754 continue;
116755 }
116756
116757 /* Check to see if the new index entry will be unique */
116758 sqlite3VdbeVerifyAbortable(v, onError);
116759 sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
116760 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
116761
116762 /* Generate code to handle collisions */
116763 regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
116764 if( isUpdate || onError==OE_Replace ){
116765 if( HasRowid(pTab) ){
116766 sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
116767 /* Conflict only if the rowid of the existing index entry
116768 ** is different from old-rowid */
116769 if( isUpdate ){
116770 sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
116771 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
116772 VdbeCoverage(v);
116773 }
116774 }else{
116775 int x;
116776 /* Extract the PRIMARY KEY from the end of the index entry and
116777 ** store it in registers regR..regR+nPk-1 */
116778 if( pIdx!=pPk ){
116779 for(i=0; i<pPk->nKeyCol; i++){
116780 assert( pPk->aiColumn[i]>=0 );
116781 x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
116782 sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
116783 VdbeComment((v, "%s.%s", pTab->zName,
116784 pTab->aCol[pPk->aiColumn[i]].zName));
116785 }
116786 }
116787 if( isUpdate ){
116788 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
116789 ** table, only conflict if the new PRIMARY KEY values are actually
116790 ** different from the old.
116791 **
116792 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
116793 ** of the matched index row are different from the original PRIMARY
116794 ** KEY values of this row before the update. */
116795 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
116796 int op = OP_Ne;
116797 int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
116798
116799 for(i=0; i<pPk->nKeyCol; i++){
116800 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
116801 x = pPk->aiColumn[i];
116802 assert( x>=0 );
116803 if( i==(pPk->nKeyCol-1) ){
116804 addrJump = addrUniqueOk;
116805 op = OP_Eq;
116806 }
116807 sqlite3VdbeAddOp4(v, op,
116808 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
116809 );
116810 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
116811 VdbeCoverageIf(v, op==OP_Eq);
116812 VdbeCoverageIf(v, op==OP_Ne);
116813 }
116814 }
116815 }
116816 }
116817
116818 /* Generate code that executes if the new index entry is not unique */
116819 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
116820 || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update );
116821 switch( onError ){
116822 case OE_Rollback:
116823 case OE_Abort:
116824 case OE_Fail: {
116825 testcase( onError==OE_Rollback );
116826 testcase( onError==OE_Abort );
116827 testcase( onError==OE_Fail );
116828 sqlite3UniqueConstraint(pParse, onError, pIdx);
116829 break;
116830 }
116831#ifndef SQLITE_OMIT_UPSERT
116832 case OE_Update: {
116833 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
116834 /* Fall through */
116835 }
116836#endif
116837 case OE_Ignore: {
116838 testcase( onError==OE_Ignore );
116839 sqlite3VdbeGoto(v, ignoreDest);
116840 break;
116841 }
116842 default: {
116843 Trigger *pTrigger = 0;
116844 assert( onError==OE_Replace );
116845 if( db->flags&SQLITE_RecTriggers ){
116846 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
116847 }
116848 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
116849 sqlite3MultiWrite(pParse);
116850 }
116851 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
116852 regR, nPkField, 0, OE_Replace,
116853 (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
116854 seenReplace = 1;
116855 break;
116856 }
116857 }
116858 if( pUpIdx==pIdx ){
116859 sqlite3VdbeGoto(v, upsertJump+1);
116860 sqlite3VdbeJumpHere(v, upsertBypass);
116861 }else{
116862 sqlite3VdbeResolveLabel(v, addrUniqueOk);
116863 }
116864 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
116865 }
116866
116867 /* If the IPK constraint is a REPLACE, run it last */
116868 if( ipkTop ){
116869 sqlite3VdbeGoto(v, ipkTop+1);
116870 VdbeComment((v, "Do IPK REPLACE"));
116871 sqlite3VdbeJumpHere(v, ipkBottom);
116872 }
116873
116874 *pbMayReplace = seenReplace;
116875 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
116876}
116877
116878#ifdef SQLITE_ENABLE_NULL_TRIM
116879/*
116880** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
116881** to be the number of columns in table pTab that must not be NULL-trimmed.
116882**
116883** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
116884*/
116885SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
116886 u16 i;
116887
116888 /* Records with omitted columns are only allowed for schema format
116889 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
116890 if( pTab->pSchema->file_format<2 ) return;
116891
116892 for(i=pTab->nCol-1; i>0; i--){
116893 if( pTab->aCol[i].pDflt!=0 ) break;
116894 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
116895 }
116896 sqlite3VdbeChangeP5(v, i+1);
116897}
116898#endif
116899
116900/*
116901** This routine generates code to finish the INSERT or UPDATE operation
116902** that was started by a prior call to sqlite3GenerateConstraintChecks.
116903** A consecutive range of registers starting at regNewData contains the
116904** rowid and the content to be inserted.
116905**
116906** The arguments to this routine should be the same as the first six
116907** arguments to sqlite3GenerateConstraintChecks.
116908*/
116909SQLITE_PRIVATE void sqlite3CompleteInsertion(
116910 Parse *pParse, /* The parser context */
116911 Table *pTab, /* the table into which we are inserting */
116912 int iDataCur, /* Cursor of the canonical data source */
116913 int iIdxCur, /* First index cursor */
116914 int regNewData, /* Range of content */
116915 int *aRegIdx, /* Register used by each index. 0 for unused indices */
116916 int update_flags, /* True for UPDATE, False for INSERT */
116917 int appendBias, /* True if this is likely to be an append */
116918 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
116919){
116920 Vdbe *v; /* Prepared statements under construction */
116921 Index *pIdx; /* An index being inserted or updated */
116922 u8 pik_flags; /* flag values passed to the btree insert */
116923 int regData; /* Content registers (after the rowid) */
116924 int regRec; /* Register holding assembled record for the table */
116925 int i; /* Loop counter */
116926 u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
116927
116928 assert( update_flags==0
116929 || update_flags==OPFLAG_ISUPDATE
116930 || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
116931 );
116932
116933 v = sqlite3GetVdbe(pParse);
116934 assert( v!=0 );
116935 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
116936 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
116937 if( aRegIdx[i]==0 ) continue;
116938 bAffinityDone = 1;
116939 if( pIdx->pPartIdxWhere ){
116940 sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
116941 VdbeCoverage(v);
116942 }
116943 pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
116944 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
116945 assert( pParse->nested==0 );
116946 pik_flags |= OPFLAG_NCHANGE;
116947 pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
116948#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
116949 if( update_flags==0 ){
116950 sqlite3VdbeAddOp4(v, OP_InsertInt,
116951 iIdxCur+i, aRegIdx[i], 0, (char*)pTab, P4_TABLE
116952 );
116953 sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
116954 }
116955#endif
116956 }
116957 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
116958 aRegIdx[i]+1,
116959 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
116960 sqlite3VdbeChangeP5(v, pik_flags);
116961 }
116962 if( !HasRowid(pTab) ) return;
116963 regData = regNewData + 1;
116964 regRec = sqlite3GetTempReg(pParse);
116965 sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
116966 sqlite3SetMakeRecordP5(v, pTab);
116967 if( !bAffinityDone ){
116968 sqlite3TableAffinity(v, pTab, 0);
116969 }
116970 if( pParse->nested ){
116971 pik_flags = 0;
116972 }else{
116973 pik_flags = OPFLAG_NCHANGE;
116974 pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
116975 }
116976 if( appendBias ){
116977 pik_flags |= OPFLAG_APPEND;
116978 }
116979 if( useSeekResult ){
116980 pik_flags |= OPFLAG_USESEEKRESULT;
116981 }
116982 sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData);
116983 if( !pParse->nested ){
116984 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
116985 }
116986 sqlite3VdbeChangeP5(v, pik_flags);
116987}
116988
116989/*
116990** Allocate cursors for the pTab table and all its indices and generate
116991** code to open and initialized those cursors.
116992**
116993** The cursor for the object that contains the complete data (normally
116994** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
116995** ROWID table) is returned in *piDataCur. The first index cursor is
116996** returned in *piIdxCur. The number of indices is returned.
116997**
116998** Use iBase as the first cursor (either the *piDataCur for rowid tables
116999** or the first index for WITHOUT ROWID tables) if it is non-negative.
117000** If iBase is negative, then allocate the next available cursor.
117001**
117002** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
117003** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
117004** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
117005** pTab->pIndex list.
117006**
117007** If pTab is a virtual table, then this routine is a no-op and the
117008** *piDataCur and *piIdxCur values are left uninitialized.
117009*/
117010SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
117011 Parse *pParse, /* Parsing context */
117012 Table *pTab, /* Table to be opened */
117013 int op, /* OP_OpenRead or OP_OpenWrite */
117014 u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
117015 int iBase, /* Use this for the table cursor, if there is one */
117016 u8 *aToOpen, /* If not NULL: boolean for each table and index */
117017 int *piDataCur, /* Write the database source cursor number here */
117018 int *piIdxCur /* Write the first index cursor number here */
117019){
117020 int i;
117021 int iDb;
117022 int iDataCur;
117023 Index *pIdx;
117024 Vdbe *v;
117025
117026 assert( op==OP_OpenRead || op==OP_OpenWrite );
117027 assert( op==OP_OpenWrite || p5==0 );
117028 if( IsVirtual(pTab) ){
117029 /* This routine is a no-op for virtual tables. Leave the output
117030 ** variables *piDataCur and *piIdxCur uninitialized so that valgrind
117031 ** can detect if they are used by mistake in the caller. */
117032 return 0;
117033 }
117034 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
117035 v = sqlite3GetVdbe(pParse);
117036 assert( v!=0 );
117037 if( iBase<0 ) iBase = pParse->nTab;
117038 iDataCur = iBase++;
117039 if( piDataCur ) *piDataCur = iDataCur;
117040 if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
117041 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
117042 }else{
117043 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
117044 }
117045 if( piIdxCur ) *piIdxCur = iBase;
117046 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
117047 int iIdxCur = iBase++;
117048 assert( pIdx->pSchema==pTab->pSchema );
117049 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
117050 if( piDataCur ) *piDataCur = iIdxCur;
117051 p5 = 0;
117052 }
117053 if( aToOpen==0 || aToOpen[i+1] ){
117054 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
117055 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
117056 sqlite3VdbeChangeP5(v, p5);
117057 VdbeComment((v, "%s", pIdx->zName));
117058 }
117059 }
117060 if( iBase>pParse->nTab ) pParse->nTab = iBase;
117061 return i;
117062}
117063
117064
117065#ifdef SQLITE_TEST
117066/*
117067** The following global variable is incremented whenever the
117068** transfer optimization is used. This is used for testing
117069** purposes only - to make sure the transfer optimization really
117070** is happening when it is supposed to.
117071*/
117072SQLITE_API int sqlite3_xferopt_count;
117073#endif /* SQLITE_TEST */
117074
117075
117076#ifndef SQLITE_OMIT_XFER_OPT
117077/*
117078** Check to see if index pSrc is compatible as a source of data
117079** for index pDest in an insert transfer optimization. The rules
117080** for a compatible index:
117081**
117082** * The index is over the same set of columns
117083** * The same DESC and ASC markings occurs on all columns
117084** * The same onError processing (OE_Abort, OE_Ignore, etc)
117085** * The same collating sequence on each column
117086** * The index has the exact same WHERE clause
117087*/
117088static int xferCompatibleIndex(Index *pDest, Index *pSrc){
117089 int i;
117090 assert( pDest && pSrc );
117091 assert( pDest->pTable!=pSrc->pTable );
117092 if( pDest->nKeyCol!=pSrc->nKeyCol ){
117093 return 0; /* Different number of columns */
117094 }
117095 if( pDest->onError!=pSrc->onError ){
117096 return 0; /* Different conflict resolution strategies */
117097 }
117098 for(i=0; i<pSrc->nKeyCol; i++){
117099 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
117100 return 0; /* Different columns indexed */
117101 }
117102 if( pSrc->aiColumn[i]==XN_EXPR ){
117103 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
117104 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
117105 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
117106 return 0; /* Different expressions in the index */
117107 }
117108 }
117109 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
117110 return 0; /* Different sort orders */
117111 }
117112 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
117113 return 0; /* Different collating sequences */
117114 }
117115 }
117116 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
117117 return 0; /* Different WHERE clauses */
117118 }
117119
117120 /* If no test above fails then the indices must be compatible */
117121 return 1;
117122}
117123
117124/*
117125** Attempt the transfer optimization on INSERTs of the form
117126**
117127** INSERT INTO tab1 SELECT * FROM tab2;
117128**
117129** The xfer optimization transfers raw records from tab2 over to tab1.
117130** Columns are not decoded and reassembled, which greatly improves
117131** performance. Raw index records are transferred in the same way.
117132**
117133** The xfer optimization is only attempted if tab1 and tab2 are compatible.
117134** There are lots of rules for determining compatibility - see comments
117135** embedded in the code for details.
117136**
117137** This routine returns TRUE if the optimization is guaranteed to be used.
117138** Sometimes the xfer optimization will only work if the destination table
117139** is empty - a factor that can only be determined at run-time. In that
117140** case, this routine generates code for the xfer optimization but also
117141** does a test to see if the destination table is empty and jumps over the
117142** xfer optimization code if the test fails. In that case, this routine
117143** returns FALSE so that the caller will know to go ahead and generate
117144** an unoptimized transfer. This routine also returns FALSE if there
117145** is no chance that the xfer optimization can be applied.
117146**
117147** This optimization is particularly useful at making VACUUM run faster.
117148*/
117149static int xferOptimization(
117150 Parse *pParse, /* Parser context */
117151 Table *pDest, /* The table we are inserting into */
117152 Select *pSelect, /* A SELECT statement to use as the data source */
117153 int onError, /* How to handle constraint errors */
117154 int iDbDest /* The database of pDest */
117155){
117156 sqlite3 *db = pParse->db;
117157 ExprList *pEList; /* The result set of the SELECT */
117158 Table *pSrc; /* The table in the FROM clause of SELECT */
117159 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
117160 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
117161 int i; /* Loop counter */
117162 int iDbSrc; /* The database of pSrc */
117163 int iSrc, iDest; /* Cursors from source and destination */
117164 int addr1, addr2; /* Loop addresses */
117165 int emptyDestTest = 0; /* Address of test for empty pDest */
117166 int emptySrcTest = 0; /* Address of test for empty pSrc */
117167 Vdbe *v; /* The VDBE we are building */
117168 int regAutoinc; /* Memory register used by AUTOINC */
117169 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
117170 int regData, regRowid; /* Registers holding data and rowid */
117171
117172 if( pSelect==0 ){
117173 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
117174 }
117175 if( pParse->pWith || pSelect->pWith ){
117176 /* Do not attempt to process this query if there are an WITH clauses
117177 ** attached to it. Proceeding may generate a false "no such table: xxx"
117178 ** error if pSelect reads from a CTE named "xxx". */
117179 return 0;
117180 }
117181 if( sqlite3TriggerList(pParse, pDest) ){
117182 return 0; /* tab1 must not have triggers */
117183 }
117184#ifndef SQLITE_OMIT_VIRTUALTABLE
117185 if( IsVirtual(pDest) ){
117186 return 0; /* tab1 must not be a virtual table */
117187 }
117188#endif
117189 if( onError==OE_Default ){
117190 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
117191 if( onError==OE_Default ) onError = OE_Abort;
117192 }
117193 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
117194 if( pSelect->pSrc->nSrc!=1 ){
117195 return 0; /* FROM clause must have exactly one term */
117196 }
117197 if( pSelect->pSrc->a[0].pSelect ){
117198 return 0; /* FROM clause cannot contain a subquery */
117199 }
117200 if( pSelect->pWhere ){
117201 return 0; /* SELECT may not have a WHERE clause */
117202 }
117203 if( pSelect->pOrderBy ){
117204 return 0; /* SELECT may not have an ORDER BY clause */
117205 }
117206 /* Do not need to test for a HAVING clause. If HAVING is present but
117207 ** there is no ORDER BY, we will get an error. */
117208 if( pSelect->pGroupBy ){
117209 return 0; /* SELECT may not have a GROUP BY clause */
117210 }
117211 if( pSelect->pLimit ){
117212 return 0; /* SELECT may not have a LIMIT clause */
117213 }
117214 if( pSelect->pPrior ){
117215 return 0; /* SELECT may not be a compound query */
117216 }
117217 if( pSelect->selFlags & SF_Distinct ){
117218 return 0; /* SELECT may not be DISTINCT */
117219 }
117220 pEList = pSelect->pEList;
117221 assert( pEList!=0 );
117222 if( pEList->nExpr!=1 ){
117223 return 0; /* The result set must have exactly one column */
117224 }
117225 assert( pEList->a[0].pExpr );
117226 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
117227 return 0; /* The result set must be the special operator "*" */
117228 }
117229
117230 /* At this point we have established that the statement is of the
117231 ** correct syntactic form to participate in this optimization. Now
117232 ** we have to check the semantics.
117233 */
117234 pItem = pSelect->pSrc->a;
117235 pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
117236 if( pSrc==0 ){
117237 return 0; /* FROM clause does not contain a real table */
117238 }
117239 if( pSrc==pDest ){
117240 return 0; /* tab1 and tab2 may not be the same table */
117241 }
117242 if( HasRowid(pDest)!=HasRowid(pSrc) ){
117243 return 0; /* source and destination must both be WITHOUT ROWID or not */
117244 }
117245#ifndef SQLITE_OMIT_VIRTUALTABLE
117246 if( IsVirtual(pSrc) ){
117247 return 0; /* tab2 must not be a virtual table */
117248 }
117249#endif
117250 if( pSrc->pSelect ){
117251 return 0; /* tab2 may not be a view */
117252 }
117253 if( pDest->nCol!=pSrc->nCol ){
117254 return 0; /* Number of columns must be the same in tab1 and tab2 */
117255 }
117256 if( pDest->iPKey!=pSrc->iPKey ){
117257 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
117258 }
117259 for(i=0; i<pDest->nCol; i++){
117260 Column *pDestCol = &pDest->aCol[i];
117261 Column *pSrcCol = &pSrc->aCol[i];
117262#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
117263 if( (db->mDbFlags & DBFLAG_Vacuum)==0
117264 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
117265 ){
117266 return 0; /* Neither table may have __hidden__ columns */
117267 }
117268#endif
117269 if( pDestCol->affinity!=pSrcCol->affinity ){
117270 return 0; /* Affinity must be the same on all columns */
117271 }
117272 if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){
117273 return 0; /* Collating sequence must be the same on all columns */
117274 }
117275 if( pDestCol->notNull && !pSrcCol->notNull ){
117276 return 0; /* tab2 must be NOT NULL if tab1 is */
117277 }
117278 /* Default values for second and subsequent columns need to match. */
117279 if( i>0 ){
117280 assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN );
117281 assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN );
117282 if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0)
117283 || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken,
117284 pSrcCol->pDflt->u.zToken)!=0)
117285 ){
117286 return 0; /* Default values must be the same for all columns */
117287 }
117288 }
117289 }
117290 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
117291 if( IsUniqueIndex(pDestIdx) ){
117292 destHasUniqueIdx = 1;
117293 }
117294 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
117295 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
117296 }
117297 if( pSrcIdx==0 ){
117298 return 0; /* pDestIdx has no corresponding index in pSrc */
117299 }
117300 }
117301#ifndef SQLITE_OMIT_CHECK
117302 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
117303 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
117304 }
117305#endif
117306#ifndef SQLITE_OMIT_FOREIGN_KEY
117307 /* Disallow the transfer optimization if the destination table constains
117308 ** any foreign key constraints. This is more restrictive than necessary.
117309 ** But the main beneficiary of the transfer optimization is the VACUUM
117310 ** command, and the VACUUM command disables foreign key constraints. So
117311 ** the extra complication to make this rule less restrictive is probably
117312 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
117313 */
117314 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
117315 return 0;
117316 }
117317#endif
117318 if( (db->flags & SQLITE_CountRows)!=0 ){
117319 return 0; /* xfer opt does not play well with PRAGMA count_changes */
117320 }
117321
117322 /* If we get this far, it means that the xfer optimization is at
117323 ** least a possibility, though it might only work if the destination
117324 ** table (tab1) is initially empty.
117325 */
117326#ifdef SQLITE_TEST
117327 sqlite3_xferopt_count++;
117328#endif
117329 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
117330 v = sqlite3GetVdbe(pParse);
117331 sqlite3CodeVerifySchema(pParse, iDbSrc);
117332 iSrc = pParse->nTab++;
117333 iDest = pParse->nTab++;
117334 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
117335 regData = sqlite3GetTempReg(pParse);
117336 regRowid = sqlite3GetTempReg(pParse);
117337 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
117338 assert( HasRowid(pDest) || destHasUniqueIdx );
117339 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
117340 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
117341 || destHasUniqueIdx /* (2) */
117342 || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */
117343 )){
117344 /* In some circumstances, we are able to run the xfer optimization
117345 ** only if the destination table is initially empty. Unless the
117346 ** DBFLAG_Vacuum flag is set, this block generates code to make
117347 ** that determination. If DBFLAG_Vacuum is set, then the destination
117348 ** table is always empty.
117349 **
117350 ** Conditions under which the destination must be empty:
117351 **
117352 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
117353 ** (If the destination is not initially empty, the rowid fields
117354 ** of index entries might need to change.)
117355 **
117356 ** (2) The destination has a unique index. (The xfer optimization
117357 ** is unable to test uniqueness.)
117358 **
117359 ** (3) onError is something other than OE_Abort and OE_Rollback.
117360 */
117361 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
117362 emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto);
117363 sqlite3VdbeJumpHere(v, addr1);
117364 }
117365 if( HasRowid(pSrc) ){
117366 u8 insFlags;
117367 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
117368 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
117369 if( pDest->iPKey>=0 ){
117370 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
117371 sqlite3VdbeVerifyAbortable(v, onError);
117372 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
117373 VdbeCoverage(v);
117374 sqlite3RowidConstraint(pParse, onError, pDest);
117375 sqlite3VdbeJumpHere(v, addr2);
117376 autoIncStep(pParse, regAutoinc, regRowid);
117377 }else if( pDest->pIndex==0 ){
117378 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
117379 }else{
117380 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
117381 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
117382 }
117383 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
117384 if( db->mDbFlags & DBFLAG_Vacuum ){
117385 sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
117386 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
117387 OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
117388 }else{
117389 insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND;
117390 }
117391 sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid,
117392 (char*)pDest, P4_TABLE);
117393 sqlite3VdbeChangeP5(v, insFlags);
117394 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
117395 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
117396 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
117397 }else{
117398 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
117399 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
117400 }
117401 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
117402 u8 idxInsFlags = 0;
117403 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
117404 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
117405 }
117406 assert( pSrcIdx );
117407 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
117408 sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
117409 VdbeComment((v, "%s", pSrcIdx->zName));
117410 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
117411 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
117412 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
117413 VdbeComment((v, "%s", pDestIdx->zName));
117414 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
117415 sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
117416 if( db->mDbFlags & DBFLAG_Vacuum ){
117417 /* This INSERT command is part of a VACUUM operation, which guarantees
117418 ** that the destination table is empty. If all indexed columns use
117419 ** collation sequence BINARY, then it can also be assumed that the
117420 ** index will be populated by inserting keys in strictly sorted
117421 ** order. In this case, instead of seeking within the b-tree as part
117422 ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
117423 ** OP_IdxInsert to seek to the point within the b-tree where each key
117424 ** should be inserted. This is faster.
117425 **
117426 ** If any of the indexed columns use a collation sequence other than
117427 ** BINARY, this optimization is disabled. This is because the user
117428 ** might change the definition of a collation sequence and then run
117429 ** a VACUUM command. In that case keys may not be written in strictly
117430 ** sorted order. */
117431 for(i=0; i<pSrcIdx->nColumn; i++){
117432 const char *zColl = pSrcIdx->azColl[i];
117433 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
117434 }
117435 if( i==pSrcIdx->nColumn ){
117436 idxInsFlags = OPFLAG_USESEEKRESULT;
117437 sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
117438 }
117439 }
117440 if( !HasRowid(pSrc) && pDestIdx->idxType==2 ){
117441 idxInsFlags |= OPFLAG_NCHANGE;
117442 }
117443 sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
117444 sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
117445 sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
117446 sqlite3VdbeJumpHere(v, addr1);
117447 sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
117448 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
117449 }
117450 if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
117451 sqlite3ReleaseTempReg(pParse, regRowid);
117452 sqlite3ReleaseTempReg(pParse, regData);
117453 if( emptyDestTest ){
117454 sqlite3AutoincrementEnd(pParse);
117455 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
117456 sqlite3VdbeJumpHere(v, emptyDestTest);
117457 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
117458 return 0;
117459 }else{
117460 return 1;
117461 }
117462}
117463#endif /* SQLITE_OMIT_XFER_OPT */
117464
117465/************** End of insert.c **********************************************/
117466/************** Begin file legacy.c ******************************************/
117467/*
117468** 2001 September 15
117469**
117470** The author disclaims copyright to this source code. In place of
117471** a legal notice, here is a blessing:
117472**
117473** May you do good and not evil.
117474** May you find forgiveness for yourself and forgive others.
117475** May you share freely, never taking more than you give.
117476**
117477*************************************************************************
117478** Main file for the SQLite library. The routines in this file
117479** implement the programmer interface to the library. Routines in
117480** other files are for internal use by SQLite and should not be
117481** accessed by users of the library.
117482*/
117483
117484/* #include "sqliteInt.h" */
117485
117486/*
117487** Execute SQL code. Return one of the SQLITE_ success/failure
117488** codes. Also write an error message into memory obtained from
117489** malloc() and make *pzErrMsg point to that message.
117490**
117491** If the SQL is a query, then for each row in the query result
117492** the xCallback() function is called. pArg becomes the first
117493** argument to xCallback(). If xCallback=NULL then no callback
117494** is invoked, even for queries.
117495*/
117496SQLITE_API int sqlite3_exec(
117497 sqlite3 *db, /* The database on which the SQL executes */
117498 const char *zSql, /* The SQL to be executed */
117499 sqlite3_callback xCallback, /* Invoke this callback routine */
117500 void *pArg, /* First argument to xCallback() */
117501 char **pzErrMsg /* Write error messages here */
117502){
117503 int rc = SQLITE_OK; /* Return code */
117504 const char *zLeftover; /* Tail of unprocessed SQL */
117505 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
117506 char **azCols = 0; /* Names of result columns */
117507 int callbackIsInit; /* True if callback data is initialized */
117508
117509 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
117510 if( zSql==0 ) zSql = "";
117511
117512 sqlite3_mutex_enter(db->mutex);
117513 sqlite3Error(db, SQLITE_OK);
117514 while( rc==SQLITE_OK && zSql[0] ){
117515 int nCol;
117516 char **azVals = 0;
117517
117518 pStmt = 0;
117519 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
117520 assert( rc==SQLITE_OK || pStmt==0 );
117521 if( rc!=SQLITE_OK ){
117522 continue;
117523 }
117524 if( !pStmt ){
117525 /* this happens for a comment or white-space */
117526 zSql = zLeftover;
117527 continue;
117528 }
117529
117530 callbackIsInit = 0;
117531 nCol = sqlite3_column_count(pStmt);
117532
117533 while( 1 ){
117534 int i;
117535 rc = sqlite3_step(pStmt);
117536
117537 /* Invoke the callback function if required */
117538 if( xCallback && (SQLITE_ROW==rc ||
117539 (SQLITE_DONE==rc && !callbackIsInit
117540 && db->flags&SQLITE_NullCallback)) ){
117541 if( !callbackIsInit ){
117542 azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*));
117543 if( azCols==0 ){
117544 goto exec_out;
117545 }
117546 for(i=0; i<nCol; i++){
117547 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
117548 /* sqlite3VdbeSetColName() installs column names as UTF8
117549 ** strings so there is no way for sqlite3_column_name() to fail. */
117550 assert( azCols[i]!=0 );
117551 }
117552 callbackIsInit = 1;
117553 }
117554 if( rc==SQLITE_ROW ){
117555 azVals = &azCols[nCol];
117556 for(i=0; i<nCol; i++){
117557 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
117558 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
117559 sqlite3OomFault(db);
117560 goto exec_out;
117561 }
117562 }
117563 azVals[i] = 0;
117564 }
117565 if( xCallback(pArg, nCol, azVals, azCols) ){
117566 /* EVIDENCE-OF: R-38229-40159 If the callback function to
117567 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
117568 ** return SQLITE_ABORT. */
117569 rc = SQLITE_ABORT;
117570 sqlite3VdbeFinalize((Vdbe *)pStmt);
117571 pStmt = 0;
117572 sqlite3Error(db, SQLITE_ABORT);
117573 goto exec_out;
117574 }
117575 }
117576
117577 if( rc!=SQLITE_ROW ){
117578 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
117579 pStmt = 0;
117580 zSql = zLeftover;
117581 while( sqlite3Isspace(zSql[0]) ) zSql++;
117582 break;
117583 }
117584 }
117585
117586 sqlite3DbFree(db, azCols);
117587 azCols = 0;
117588 }
117589
117590exec_out:
117591 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
117592 sqlite3DbFree(db, azCols);
117593
117594 rc = sqlite3ApiExit(db, rc);
117595 if( rc!=SQLITE_OK && pzErrMsg ){
117596 *pzErrMsg = sqlite3DbStrDup(0, sqlite3_errmsg(db));
117597 if( *pzErrMsg==0 ){
117598 rc = SQLITE_NOMEM_BKPT;
117599 sqlite3Error(db, SQLITE_NOMEM);
117600 }
117601 }else if( pzErrMsg ){
117602 *pzErrMsg = 0;
117603 }
117604
117605 assert( (rc&db->errMask)==rc );
117606 sqlite3_mutex_leave(db->mutex);
117607 return rc;
117608}
117609
117610/************** End of legacy.c **********************************************/
117611/************** Begin file loadext.c *****************************************/
117612/*
117613** 2006 June 7
117614**
117615** The author disclaims copyright to this source code. In place of
117616** a legal notice, here is a blessing:
117617**
117618** May you do good and not evil.
117619** May you find forgiveness for yourself and forgive others.
117620** May you share freely, never taking more than you give.
117621**
117622*************************************************************************
117623** This file contains code used to dynamically load extensions into
117624** the SQLite library.
117625*/
117626
117627#ifndef SQLITE_CORE
117628 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */
117629#endif
117630/************** Include sqlite3ext.h in the middle of loadext.c **************/
117631/************** Begin file sqlite3ext.h **************************************/
117632/*
117633** 2006 June 7
117634**
117635** The author disclaims copyright to this source code. In place of
117636** a legal notice, here is a blessing:
117637**
117638** May you do good and not evil.
117639** May you find forgiveness for yourself and forgive others.
117640** May you share freely, never taking more than you give.
117641**
117642*************************************************************************
117643** This header file defines the SQLite interface for use by
117644** shared libraries that want to be imported as extensions into
117645** an SQLite instance. Shared libraries that intend to be loaded
117646** as extensions by SQLite should #include this file instead of
117647** sqlite3.h.
117648*/
117649#ifndef SQLITE3EXT_H
117650#define SQLITE3EXT_H
117651/* #include "sqlite3.h" */
117652
117653/*
117654** The following structure holds pointers to all of the SQLite API
117655** routines.
117656**
117657** WARNING: In order to maintain backwards compatibility, add new
117658** interfaces to the end of this structure only. If you insert new
117659** interfaces in the middle of this structure, then older different
117660** versions of SQLite will not be able to load each other's shared
117661** libraries!
117662*/
117663struct sqlite3_api_routines {
117664 void * (*aggregate_context)(sqlite3_context*,int nBytes);
117665 int (*aggregate_count)(sqlite3_context*);
117666 int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
117667 int (*bind_double)(sqlite3_stmt*,int,double);
117668 int (*bind_int)(sqlite3_stmt*,int,int);
117669 int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
117670 int (*bind_null)(sqlite3_stmt*,int);
117671 int (*bind_parameter_count)(sqlite3_stmt*);
117672 int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
117673 const char * (*bind_parameter_name)(sqlite3_stmt*,int);
117674 int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
117675 int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
117676 int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
117677 int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
117678 int (*busy_timeout)(sqlite3*,int ms);
117679 int (*changes)(sqlite3*);
117680 int (*close)(sqlite3*);
117681 int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
117682 int eTextRep,const char*));
117683 int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
117684 int eTextRep,const void*));
117685 const void * (*column_blob)(sqlite3_stmt*,int iCol);
117686 int (*column_bytes)(sqlite3_stmt*,int iCol);
117687 int (*column_bytes16)(sqlite3_stmt*,int iCol);
117688 int (*column_count)(sqlite3_stmt*pStmt);
117689 const char * (*column_database_name)(sqlite3_stmt*,int);
117690 const void * (*column_database_name16)(sqlite3_stmt*,int);
117691 const char * (*column_decltype)(sqlite3_stmt*,int i);
117692 const void * (*column_decltype16)(sqlite3_stmt*,int);
117693 double (*column_double)(sqlite3_stmt*,int iCol);
117694 int (*column_int)(sqlite3_stmt*,int iCol);
117695 sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
117696 const char * (*column_name)(sqlite3_stmt*,int);
117697 const void * (*column_name16)(sqlite3_stmt*,int);
117698 const char * (*column_origin_name)(sqlite3_stmt*,int);
117699 const void * (*column_origin_name16)(sqlite3_stmt*,int);
117700 const char * (*column_table_name)(sqlite3_stmt*,int);
117701 const void * (*column_table_name16)(sqlite3_stmt*,int);
117702 const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
117703 const void * (*column_text16)(sqlite3_stmt*,int iCol);
117704 int (*column_type)(sqlite3_stmt*,int iCol);
117705 sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
117706 void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
117707 int (*complete)(const char*sql);
117708 int (*complete16)(const void*sql);
117709 int (*create_collation)(sqlite3*,const char*,int,void*,
117710 int(*)(void*,int,const void*,int,const void*));
117711 int (*create_collation16)(sqlite3*,const void*,int,void*,
117712 int(*)(void*,int,const void*,int,const void*));
117713 int (*create_function)(sqlite3*,const char*,int,int,void*,
117714 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
117715 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
117716 void (*xFinal)(sqlite3_context*));
117717 int (*create_function16)(sqlite3*,const void*,int,int,void*,
117718 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
117719 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
117720 void (*xFinal)(sqlite3_context*));
117721 int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
117722 int (*data_count)(sqlite3_stmt*pStmt);
117723 sqlite3 * (*db_handle)(sqlite3_stmt*);
117724 int (*declare_vtab)(sqlite3*,const char*);
117725 int (*enable_shared_cache)(int);
117726 int (*errcode)(sqlite3*db);
117727 const char * (*errmsg)(sqlite3*);
117728 const void * (*errmsg16)(sqlite3*);
117729 int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
117730 int (*expired)(sqlite3_stmt*);
117731 int (*finalize)(sqlite3_stmt*pStmt);
117732 void (*free)(void*);
117733 void (*free_table)(char**result);
117734 int (*get_autocommit)(sqlite3*);
117735 void * (*get_auxdata)(sqlite3_context*,int);
117736 int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
117737 int (*global_recover)(void);
117738 void (*interruptx)(sqlite3*);
117739 sqlite_int64 (*last_insert_rowid)(sqlite3*);
117740 const char * (*libversion)(void);
117741 int (*libversion_number)(void);
117742 void *(*malloc)(int);
117743 char * (*mprintf)(const char*,...);
117744 int (*open)(const char*,sqlite3**);
117745 int (*open16)(const void*,sqlite3**);
117746 int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
117747 int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
117748 void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
117749 void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
117750 void *(*realloc)(void*,int);
117751 int (*reset)(sqlite3_stmt*pStmt);
117752 void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
117753 void (*result_double)(sqlite3_context*,double);
117754 void (*result_error)(sqlite3_context*,const char*,int);
117755 void (*result_error16)(sqlite3_context*,const void*,int);
117756 void (*result_int)(sqlite3_context*,int);
117757 void (*result_int64)(sqlite3_context*,sqlite_int64);
117758 void (*result_null)(sqlite3_context*);
117759 void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
117760 void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
117761 void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
117762 void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
117763 void (*result_value)(sqlite3_context*,sqlite3_value*);
117764 void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
117765 int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
117766 const char*,const char*),void*);
117767 void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
117768 char * (*xsnprintf)(int,char*,const char*,...);
117769 int (*step)(sqlite3_stmt*);
117770 int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
117771 char const**,char const**,int*,int*,int*);
117772 void (*thread_cleanup)(void);
117773 int (*total_changes)(sqlite3*);
117774 void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
117775 int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
117776 void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
117777 sqlite_int64),void*);
117778 void * (*user_data)(sqlite3_context*);
117779 const void * (*value_blob)(sqlite3_value*);
117780 int (*value_bytes)(sqlite3_value*);
117781 int (*value_bytes16)(sqlite3_value*);
117782 double (*value_double)(sqlite3_value*);
117783 int (*value_int)(sqlite3_value*);
117784 sqlite_int64 (*value_int64)(sqlite3_value*);
117785 int (*value_numeric_type)(sqlite3_value*);
117786 const unsigned char * (*value_text)(sqlite3_value*);
117787 const void * (*value_text16)(sqlite3_value*);
117788 const void * (*value_text16be)(sqlite3_value*);
117789 const void * (*value_text16le)(sqlite3_value*);
117790 int (*value_type)(sqlite3_value*);
117791 char *(*vmprintf)(const char*,va_list);
117792 /* Added ??? */
117793 int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
117794 /* Added by 3.3.13 */
117795 int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
117796 int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
117797 int (*clear_bindings)(sqlite3_stmt*);
117798 /* Added by 3.4.1 */
117799 int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
117800 void (*xDestroy)(void *));
117801 /* Added by 3.5.0 */
117802 int (*bind_zeroblob)(sqlite3_stmt*,int,int);
117803 int (*blob_bytes)(sqlite3_blob*);
117804 int (*blob_close)(sqlite3_blob*);
117805 int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
117806 int,sqlite3_blob**);
117807 int (*blob_read)(sqlite3_blob*,void*,int,int);
117808 int (*blob_write)(sqlite3_blob*,const void*,int,int);
117809 int (*create_collation_v2)(sqlite3*,const char*,int,void*,
117810 int(*)(void*,int,const void*,int,const void*),
117811 void(*)(void*));
117812 int (*file_control)(sqlite3*,const char*,int,void*);
117813 sqlite3_int64 (*memory_highwater)(int);
117814 sqlite3_int64 (*memory_used)(void);
117815 sqlite3_mutex *(*mutex_alloc)(int);
117816 void (*mutex_enter)(sqlite3_mutex*);
117817 void (*mutex_free)(sqlite3_mutex*);
117818 void (*mutex_leave)(sqlite3_mutex*);
117819 int (*mutex_try)(sqlite3_mutex*);
117820 int (*open_v2)(const char*,sqlite3**,int,const char*);
117821 int (*release_memory)(int);
117822 void (*result_error_nomem)(sqlite3_context*);
117823 void (*result_error_toobig)(sqlite3_context*);
117824 int (*sleep)(int);
117825 void (*soft_heap_limit)(int);
117826 sqlite3_vfs *(*vfs_find)(const char*);
117827 int (*vfs_register)(sqlite3_vfs*,int);
117828 int (*vfs_unregister)(sqlite3_vfs*);
117829 int (*xthreadsafe)(void);
117830 void (*result_zeroblob)(sqlite3_context*,int);
117831 void (*result_error_code)(sqlite3_context*,int);
117832 int (*test_control)(int, ...);
117833 void (*randomness)(int,void*);
117834 sqlite3 *(*context_db_handle)(sqlite3_context*);
117835 int (*extended_result_codes)(sqlite3*,int);
117836 int (*limit)(sqlite3*,int,int);
117837 sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
117838 const char *(*sql)(sqlite3_stmt*);
117839 int (*status)(int,int*,int*,int);
117840 int (*backup_finish)(sqlite3_backup*);
117841 sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
117842 int (*backup_pagecount)(sqlite3_backup*);
117843 int (*backup_remaining)(sqlite3_backup*);
117844 int (*backup_step)(sqlite3_backup*,int);
117845 const char *(*compileoption_get)(int);
117846 int (*compileoption_used)(const char*);
117847 int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
117848 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
117849 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
117850 void (*xFinal)(sqlite3_context*),
117851 void(*xDestroy)(void*));
117852 int (*db_config)(sqlite3*,int,...);
117853 sqlite3_mutex *(*db_mutex)(sqlite3*);
117854 int (*db_status)(sqlite3*,int,int*,int*,int);
117855 int (*extended_errcode)(sqlite3*);
117856 void (*log)(int,const char*,...);
117857 sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
117858 const char *(*sourceid)(void);
117859 int (*stmt_status)(sqlite3_stmt*,int,int);
117860 int (*strnicmp)(const char*,const char*,int);
117861 int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
117862 int (*wal_autocheckpoint)(sqlite3*,int);
117863 int (*wal_checkpoint)(sqlite3*,const char*);
117864 void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
117865 int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
117866 int (*vtab_config)(sqlite3*,int op,...);
117867 int (*vtab_on_conflict)(sqlite3*);
117868 /* Version 3.7.16 and later */
117869 int (*close_v2)(sqlite3*);
117870 const char *(*db_filename)(sqlite3*,const char*);
117871 int (*db_readonly)(sqlite3*,const char*);
117872 int (*db_release_memory)(sqlite3*);
117873 const char *(*errstr)(int);
117874 int (*stmt_busy)(sqlite3_stmt*);
117875 int (*stmt_readonly)(sqlite3_stmt*);
117876 int (*stricmp)(const char*,const char*);
117877 int (*uri_boolean)(const char*,const char*,int);
117878 sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
117879 const char *(*uri_parameter)(const char*,const char*);
117880 char *(*xvsnprintf)(int,char*,const char*,va_list);
117881 int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
117882 /* Version 3.8.7 and later */
117883 int (*auto_extension)(void(*)(void));
117884 int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
117885 void(*)(void*));
117886 int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
117887 void(*)(void*),unsigned char);
117888 int (*cancel_auto_extension)(void(*)(void));
117889 int (*load_extension)(sqlite3*,const char*,const char*,char**);
117890 void *(*malloc64)(sqlite3_uint64);
117891 sqlite3_uint64 (*msize)(void*);
117892 void *(*realloc64)(void*,sqlite3_uint64);
117893 void (*reset_auto_extension)(void);
117894 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
117895 void(*)(void*));
117896 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
117897 void(*)(void*), unsigned char);
117898 int (*strglob)(const char*,const char*);
117899 /* Version 3.8.11 and later */
117900 sqlite3_value *(*value_dup)(const sqlite3_value*);
117901 void (*value_free)(sqlite3_value*);
117902 int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
117903 int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
117904 /* Version 3.9.0 and later */
117905 unsigned int (*value_subtype)(sqlite3_value*);
117906 void (*result_subtype)(sqlite3_context*,unsigned int);
117907 /* Version 3.10.0 and later */
117908 int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
117909 int (*strlike)(const char*,const char*,unsigned int);
117910 int (*db_cacheflush)(sqlite3*);
117911 /* Version 3.12.0 and later */
117912 int (*system_errno)(sqlite3*);
117913 /* Version 3.14.0 and later */
117914 int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
117915 char *(*expanded_sql)(sqlite3_stmt*);
117916 /* Version 3.18.0 and later */
117917 void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
117918 /* Version 3.20.0 and later */
117919 int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
117920 sqlite3_stmt**,const char**);
117921 int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
117922 sqlite3_stmt**,const void**);
117923 int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
117924 void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
117925 void *(*value_pointer)(sqlite3_value*,const char*);
117926 int (*vtab_nochange)(sqlite3_context*);
117927 int (*value_nochange)(sqlite3_value*);
117928 const char *(*vtab_collation)(sqlite3_index_info*,int);
117929 /* Version 3.24.0 and later */
117930 int (*keyword_count)(void);
117931 int (*keyword_name)(int,const char**,int*);
117932 int (*keyword_check)(const char*,int);
117933 sqlite3_str *(*str_new)(sqlite3*);
117934 char *(*str_finish)(sqlite3_str*);
117935 void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
117936 void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
117937 void (*str_append)(sqlite3_str*, const char *zIn, int N);
117938 void (*str_appendall)(sqlite3_str*, const char *zIn);
117939 void (*str_appendchar)(sqlite3_str*, int N, char C);
117940 void (*str_reset)(sqlite3_str*);
117941 int (*str_errcode)(sqlite3_str*);
117942 int (*str_length)(sqlite3_str*);
117943 char *(*str_value)(sqlite3_str*);
117944 int (*create_window_function)(sqlite3*,const char*,int,int,void*,
117945 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
117946 void (*xFinal)(sqlite3_context*),
117947 void (*xValue)(sqlite3_context*),
117948 void (*xInv)(sqlite3_context*,int,sqlite3_value**),
117949 void(*xDestroy)(void*));
117950};
117951
117952/*
117953** This is the function signature used for all extension entry points. It
117954** is also defined in the file "loadext.c".
117955*/
117956typedef int (*sqlite3_loadext_entry)(
117957 sqlite3 *db, /* Handle to the database. */
117958 char **pzErrMsg, /* Used to set error string on failure. */
117959 const sqlite3_api_routines *pThunk /* Extension API function pointers. */
117960);
117961
117962/*
117963** The following macros redefine the API routines so that they are
117964** redirected through the global sqlite3_api structure.
117965**
117966** This header file is also used by the loadext.c source file
117967** (part of the main SQLite library - not an extension) so that
117968** it can get access to the sqlite3_api_routines structure
117969** definition. But the main library does not want to redefine
117970** the API. So the redefinition macros are only valid if the
117971** SQLITE_CORE macros is undefined.
117972*/
117973#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
117974#define sqlite3_aggregate_context sqlite3_api->aggregate_context
117975#ifndef SQLITE_OMIT_DEPRECATED
117976#define sqlite3_aggregate_count sqlite3_api->aggregate_count
117977#endif
117978#define sqlite3_bind_blob sqlite3_api->bind_blob
117979#define sqlite3_bind_double sqlite3_api->bind_double
117980#define sqlite3_bind_int sqlite3_api->bind_int
117981#define sqlite3_bind_int64 sqlite3_api->bind_int64
117982#define sqlite3_bind_null sqlite3_api->bind_null
117983#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
117984#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
117985#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
117986#define sqlite3_bind_text sqlite3_api->bind_text
117987#define sqlite3_bind_text16 sqlite3_api->bind_text16
117988#define sqlite3_bind_value sqlite3_api->bind_value
117989#define sqlite3_busy_handler sqlite3_api->busy_handler
117990#define sqlite3_busy_timeout sqlite3_api->busy_timeout
117991#define sqlite3_changes sqlite3_api->changes
117992#define sqlite3_close sqlite3_api->close
117993#define sqlite3_collation_needed sqlite3_api->collation_needed
117994#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
117995#define sqlite3_column_blob sqlite3_api->column_blob
117996#define sqlite3_column_bytes sqlite3_api->column_bytes
117997#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
117998#define sqlite3_column_count sqlite3_api->column_count
117999#define sqlite3_column_database_name sqlite3_api->column_database_name
118000#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
118001#define sqlite3_column_decltype sqlite3_api->column_decltype
118002#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
118003#define sqlite3_column_double sqlite3_api->column_double
118004#define sqlite3_column_int sqlite3_api->column_int
118005#define sqlite3_column_int64 sqlite3_api->column_int64
118006#define sqlite3_column_name sqlite3_api->column_name
118007#define sqlite3_column_name16 sqlite3_api->column_name16
118008#define sqlite3_column_origin_name sqlite3_api->column_origin_name
118009#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
118010#define sqlite3_column_table_name sqlite3_api->column_table_name
118011#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
118012#define sqlite3_column_text sqlite3_api->column_text
118013#define sqlite3_column_text16 sqlite3_api->column_text16
118014#define sqlite3_column_type sqlite3_api->column_type
118015#define sqlite3_column_value sqlite3_api->column_value
118016#define sqlite3_commit_hook sqlite3_api->commit_hook
118017#define sqlite3_complete sqlite3_api->complete
118018#define sqlite3_complete16 sqlite3_api->complete16
118019#define sqlite3_create_collation sqlite3_api->create_collation
118020#define sqlite3_create_collation16 sqlite3_api->create_collation16
118021#define sqlite3_create_function sqlite3_api->create_function
118022#define sqlite3_create_function16 sqlite3_api->create_function16
118023#define sqlite3_create_module sqlite3_api->create_module
118024#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
118025#define sqlite3_data_count sqlite3_api->data_count
118026#define sqlite3_db_handle sqlite3_api->db_handle
118027#define sqlite3_declare_vtab sqlite3_api->declare_vtab
118028#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
118029#define sqlite3_errcode sqlite3_api->errcode
118030#define sqlite3_errmsg sqlite3_api->errmsg
118031#define sqlite3_errmsg16 sqlite3_api->errmsg16
118032#define sqlite3_exec sqlite3_api->exec
118033#ifndef SQLITE_OMIT_DEPRECATED
118034#define sqlite3_expired sqlite3_api->expired
118035#endif
118036#define sqlite3_finalize sqlite3_api->finalize
118037#define sqlite3_free sqlite3_api->free
118038#define sqlite3_free_table sqlite3_api->free_table
118039#define sqlite3_get_autocommit sqlite3_api->get_autocommit
118040#define sqlite3_get_auxdata sqlite3_api->get_auxdata
118041#define sqlite3_get_table sqlite3_api->get_table
118042#ifndef SQLITE_OMIT_DEPRECATED
118043#define sqlite3_global_recover sqlite3_api->global_recover
118044#endif
118045#define sqlite3_interrupt sqlite3_api->interruptx
118046#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
118047#define sqlite3_libversion sqlite3_api->libversion
118048#define sqlite3_libversion_number sqlite3_api->libversion_number
118049#define sqlite3_malloc sqlite3_api->malloc
118050#define sqlite3_mprintf sqlite3_api->mprintf
118051#define sqlite3_open sqlite3_api->open
118052#define sqlite3_open16 sqlite3_api->open16
118053#define sqlite3_prepare sqlite3_api->prepare
118054#define sqlite3_prepare16 sqlite3_api->prepare16
118055#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
118056#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
118057#define sqlite3_profile sqlite3_api->profile
118058#define sqlite3_progress_handler sqlite3_api->progress_handler
118059#define sqlite3_realloc sqlite3_api->realloc
118060#define sqlite3_reset sqlite3_api->reset
118061#define sqlite3_result_blob sqlite3_api->result_blob
118062#define sqlite3_result_double sqlite3_api->result_double
118063#define sqlite3_result_error sqlite3_api->result_error
118064#define sqlite3_result_error16 sqlite3_api->result_error16
118065#define sqlite3_result_int sqlite3_api->result_int
118066#define sqlite3_result_int64 sqlite3_api->result_int64
118067#define sqlite3_result_null sqlite3_api->result_null
118068#define sqlite3_result_text sqlite3_api->result_text
118069#define sqlite3_result_text16 sqlite3_api->result_text16
118070#define sqlite3_result_text16be sqlite3_api->result_text16be
118071#define sqlite3_result_text16le sqlite3_api->result_text16le
118072#define sqlite3_result_value sqlite3_api->result_value
118073#define sqlite3_rollback_hook sqlite3_api->rollback_hook
118074#define sqlite3_set_authorizer sqlite3_api->set_authorizer
118075#define sqlite3_set_auxdata sqlite3_api->set_auxdata
118076#define sqlite3_snprintf sqlite3_api->xsnprintf
118077#define sqlite3_step sqlite3_api->step
118078#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
118079#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
118080#define sqlite3_total_changes sqlite3_api->total_changes
118081#define sqlite3_trace sqlite3_api->trace
118082#ifndef SQLITE_OMIT_DEPRECATED
118083#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
118084#endif
118085#define sqlite3_update_hook sqlite3_api->update_hook
118086#define sqlite3_user_data sqlite3_api->user_data
118087#define sqlite3_value_blob sqlite3_api->value_blob
118088#define sqlite3_value_bytes sqlite3_api->value_bytes
118089#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
118090#define sqlite3_value_double sqlite3_api->value_double
118091#define sqlite3_value_int sqlite3_api->value_int
118092#define sqlite3_value_int64 sqlite3_api->value_int64
118093#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
118094#define sqlite3_value_text sqlite3_api->value_text
118095#define sqlite3_value_text16 sqlite3_api->value_text16
118096#define sqlite3_value_text16be sqlite3_api->value_text16be
118097#define sqlite3_value_text16le sqlite3_api->value_text16le
118098#define sqlite3_value_type sqlite3_api->value_type
118099#define sqlite3_vmprintf sqlite3_api->vmprintf
118100#define sqlite3_vsnprintf sqlite3_api->xvsnprintf
118101#define sqlite3_overload_function sqlite3_api->overload_function
118102#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
118103#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
118104#define sqlite3_clear_bindings sqlite3_api->clear_bindings
118105#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
118106#define sqlite3_blob_bytes sqlite3_api->blob_bytes
118107#define sqlite3_blob_close sqlite3_api->blob_close
118108#define sqlite3_blob_open sqlite3_api->blob_open
118109#define sqlite3_blob_read sqlite3_api->blob_read
118110#define sqlite3_blob_write sqlite3_api->blob_write
118111#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
118112#define sqlite3_file_control sqlite3_api->file_control
118113#define sqlite3_memory_highwater sqlite3_api->memory_highwater
118114#define sqlite3_memory_used sqlite3_api->memory_used
118115#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
118116#define sqlite3_mutex_enter sqlite3_api->mutex_enter
118117#define sqlite3_mutex_free sqlite3_api->mutex_free
118118#define sqlite3_mutex_leave sqlite3_api->mutex_leave
118119#define sqlite3_mutex_try sqlite3_api->mutex_try
118120#define sqlite3_open_v2 sqlite3_api->open_v2
118121#define sqlite3_release_memory sqlite3_api->release_memory
118122#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
118123#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
118124#define sqlite3_sleep sqlite3_api->sleep
118125#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
118126#define sqlite3_vfs_find sqlite3_api->vfs_find
118127#define sqlite3_vfs_register sqlite3_api->vfs_register
118128#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
118129#define sqlite3_threadsafe sqlite3_api->xthreadsafe
118130#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
118131#define sqlite3_result_error_code sqlite3_api->result_error_code
118132#define sqlite3_test_control sqlite3_api->test_control
118133#define sqlite3_randomness sqlite3_api->randomness
118134#define sqlite3_context_db_handle sqlite3_api->context_db_handle
118135#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
118136#define sqlite3_limit sqlite3_api->limit
118137#define sqlite3_next_stmt sqlite3_api->next_stmt
118138#define sqlite3_sql sqlite3_api->sql
118139#define sqlite3_status sqlite3_api->status
118140#define sqlite3_backup_finish sqlite3_api->backup_finish
118141#define sqlite3_backup_init sqlite3_api->backup_init
118142#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
118143#define sqlite3_backup_remaining sqlite3_api->backup_remaining
118144#define sqlite3_backup_step sqlite3_api->backup_step
118145#define sqlite3_compileoption_get sqlite3_api->compileoption_get
118146#define sqlite3_compileoption_used sqlite3_api->compileoption_used
118147#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
118148#define sqlite3_db_config sqlite3_api->db_config
118149#define sqlite3_db_mutex sqlite3_api->db_mutex
118150#define sqlite3_db_status sqlite3_api->db_status
118151#define sqlite3_extended_errcode sqlite3_api->extended_errcode
118152#define sqlite3_log sqlite3_api->log
118153#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
118154#define sqlite3_sourceid sqlite3_api->sourceid
118155#define sqlite3_stmt_status sqlite3_api->stmt_status
118156#define sqlite3_strnicmp sqlite3_api->strnicmp
118157#define sqlite3_unlock_notify sqlite3_api->unlock_notify
118158#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
118159#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
118160#define sqlite3_wal_hook sqlite3_api->wal_hook
118161#define sqlite3_blob_reopen sqlite3_api->blob_reopen
118162#define sqlite3_vtab_config sqlite3_api->vtab_config
118163#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
118164/* Version 3.7.16 and later */
118165#define sqlite3_close_v2 sqlite3_api->close_v2
118166#define sqlite3_db_filename sqlite3_api->db_filename
118167#define sqlite3_db_readonly sqlite3_api->db_readonly
118168#define sqlite3_db_release_memory sqlite3_api->db_release_memory
118169#define sqlite3_errstr sqlite3_api->errstr
118170#define sqlite3_stmt_busy sqlite3_api->stmt_busy
118171#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
118172#define sqlite3_stricmp sqlite3_api->stricmp
118173#define sqlite3_uri_boolean sqlite3_api->uri_boolean
118174#define sqlite3_uri_int64 sqlite3_api->uri_int64
118175#define sqlite3_uri_parameter sqlite3_api->uri_parameter
118176#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
118177#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
118178/* Version 3.8.7 and later */
118179#define sqlite3_auto_extension sqlite3_api->auto_extension
118180#define sqlite3_bind_blob64 sqlite3_api->bind_blob64
118181#define sqlite3_bind_text64 sqlite3_api->bind_text64
118182#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
118183#define sqlite3_load_extension sqlite3_api->load_extension
118184#define sqlite3_malloc64 sqlite3_api->malloc64
118185#define sqlite3_msize sqlite3_api->msize
118186#define sqlite3_realloc64 sqlite3_api->realloc64
118187#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
118188#define sqlite3_result_blob64 sqlite3_api->result_blob64
118189#define sqlite3_result_text64 sqlite3_api->result_text64
118190#define sqlite3_strglob sqlite3_api->strglob
118191/* Version 3.8.11 and later */
118192#define sqlite3_value_dup sqlite3_api->value_dup
118193#define sqlite3_value_free sqlite3_api->value_free
118194#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
118195#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
118196/* Version 3.9.0 and later */
118197#define sqlite3_value_subtype sqlite3_api->value_subtype
118198#define sqlite3_result_subtype sqlite3_api->result_subtype
118199/* Version 3.10.0 and later */
118200#define sqlite3_status64 sqlite3_api->status64
118201#define sqlite3_strlike sqlite3_api->strlike
118202#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
118203/* Version 3.12.0 and later */
118204#define sqlite3_system_errno sqlite3_api->system_errno
118205/* Version 3.14.0 and later */
118206#define sqlite3_trace_v2 sqlite3_api->trace_v2
118207#define sqlite3_expanded_sql sqlite3_api->expanded_sql
118208/* Version 3.18.0 and later */
118209#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
118210/* Version 3.20.0 and later */
118211#define sqlite3_prepare_v3 sqlite3_api->prepare_v3
118212#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
118213#define sqlite3_bind_pointer sqlite3_api->bind_pointer
118214#define sqlite3_result_pointer sqlite3_api->result_pointer
118215#define sqlite3_value_pointer sqlite3_api->value_pointer
118216/* Version 3.22.0 and later */
118217#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
118218#define sqlite3_value_nochange sqlite3_api->value_nochange
118219#define sqlite3_vtab_collation sqlite3_api->vtab_collation
118220/* Version 3.24.0 and later */
118221#define sqlite3_keyword_count sqlite3_api->keyword_count
118222#define sqlite3_keyword_name sqlite3_api->keyword_name
118223#define sqlite3_keyword_check sqlite3_api->keyword_check
118224#define sqlite3_str_new sqlite3_api->str_new
118225#define sqlite3_str_finish sqlite3_api->str_finish
118226#define sqlite3_str_appendf sqlite3_api->str_appendf
118227#define sqlite3_str_vappendf sqlite3_api->str_vappendf
118228#define sqlite3_str_append sqlite3_api->str_append
118229#define sqlite3_str_appendall sqlite3_api->str_appendall
118230#define sqlite3_str_appendchar sqlite3_api->str_appendchar
118231#define sqlite3_str_reset sqlite3_api->str_reset
118232#define sqlite3_str_errcode sqlite3_api->str_errcode
118233#define sqlite3_str_length sqlite3_api->str_length
118234#define sqlite3_str_value sqlite3_api->str_value
118235/* Version 3.25.0 and later */
118236#define sqlite3_create_window_function sqlite3_api->create_window_function
118237#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
118238
118239#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
118240 /* This case when the file really is being compiled as a loadable
118241 ** extension */
118242# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
118243# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
118244# define SQLITE_EXTENSION_INIT3 \
118245 extern const sqlite3_api_routines *sqlite3_api;
118246#else
118247 /* This case when the file is being statically linked into the
118248 ** application */
118249# define SQLITE_EXTENSION_INIT1 /*no-op*/
118250# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
118251# define SQLITE_EXTENSION_INIT3 /*no-op*/
118252#endif
118253
118254#endif /* SQLITE3EXT_H */
118255
118256/************** End of sqlite3ext.h ******************************************/
118257/************** Continuing where we left off in loadext.c ********************/
118258/* #include "sqliteInt.h" */
118259
118260#ifndef SQLITE_OMIT_LOAD_EXTENSION
118261/*
118262** Some API routines are omitted when various features are
118263** excluded from a build of SQLite. Substitute a NULL pointer
118264** for any missing APIs.
118265*/
118266#ifndef SQLITE_ENABLE_COLUMN_METADATA
118267# define sqlite3_column_database_name 0
118268# define sqlite3_column_database_name16 0
118269# define sqlite3_column_table_name 0
118270# define sqlite3_column_table_name16 0
118271# define sqlite3_column_origin_name 0
118272# define sqlite3_column_origin_name16 0
118273#endif
118274
118275#ifdef SQLITE_OMIT_AUTHORIZATION
118276# define sqlite3_set_authorizer 0
118277#endif
118278
118279#ifdef SQLITE_OMIT_UTF16
118280# define sqlite3_bind_text16 0
118281# define sqlite3_collation_needed16 0
118282# define sqlite3_column_decltype16 0
118283# define sqlite3_column_name16 0
118284# define sqlite3_column_text16 0
118285# define sqlite3_complete16 0
118286# define sqlite3_create_collation16 0
118287# define sqlite3_create_function16 0
118288# define sqlite3_errmsg16 0
118289# define sqlite3_open16 0
118290# define sqlite3_prepare16 0
118291# define sqlite3_prepare16_v2 0
118292# define sqlite3_prepare16_v3 0
118293# define sqlite3_result_error16 0
118294# define sqlite3_result_text16 0
118295# define sqlite3_result_text16be 0
118296# define sqlite3_result_text16le 0
118297# define sqlite3_value_text16 0
118298# define sqlite3_value_text16be 0
118299# define sqlite3_value_text16le 0
118300# define sqlite3_column_database_name16 0
118301# define sqlite3_column_table_name16 0
118302# define sqlite3_column_origin_name16 0
118303#endif
118304
118305#ifdef SQLITE_OMIT_COMPLETE
118306# define sqlite3_complete 0
118307# define sqlite3_complete16 0
118308#endif
118309
118310#ifdef SQLITE_OMIT_DECLTYPE
118311# define sqlite3_column_decltype16 0
118312# define sqlite3_column_decltype 0
118313#endif
118314
118315#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
118316# define sqlite3_progress_handler 0
118317#endif
118318
118319#ifdef SQLITE_OMIT_VIRTUALTABLE
118320# define sqlite3_create_module 0
118321# define sqlite3_create_module_v2 0
118322# define sqlite3_declare_vtab 0
118323# define sqlite3_vtab_config 0
118324# define sqlite3_vtab_on_conflict 0
118325#endif
118326
118327#ifdef SQLITE_OMIT_SHARED_CACHE
118328# define sqlite3_enable_shared_cache 0
118329#endif
118330
118331#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
118332# define sqlite3_profile 0
118333# define sqlite3_trace 0
118334#endif
118335
118336#ifdef SQLITE_OMIT_GET_TABLE
118337# define sqlite3_free_table 0
118338# define sqlite3_get_table 0
118339#endif
118340
118341#ifdef SQLITE_OMIT_INCRBLOB
118342#define sqlite3_bind_zeroblob 0
118343#define sqlite3_blob_bytes 0
118344#define sqlite3_blob_close 0
118345#define sqlite3_blob_open 0
118346#define sqlite3_blob_read 0
118347#define sqlite3_blob_write 0
118348#define sqlite3_blob_reopen 0
118349#endif
118350
118351#if defined(SQLITE_OMIT_TRACE)
118352# define sqlite3_trace_v2 0
118353#endif
118354
118355/*
118356** The following structure contains pointers to all SQLite API routines.
118357** A pointer to this structure is passed into extensions when they are
118358** loaded so that the extension can make calls back into the SQLite
118359** library.
118360**
118361** When adding new APIs, add them to the bottom of this structure
118362** in order to preserve backwards compatibility.
118363**
118364** Extensions that use newer APIs should first call the
118365** sqlite3_libversion_number() to make sure that the API they
118366** intend to use is supported by the library. Extensions should
118367** also check to make sure that the pointer to the function is
118368** not NULL before calling it.
118369*/
118370static const sqlite3_api_routines sqlite3Apis = {
118371 sqlite3_aggregate_context,
118372#ifndef SQLITE_OMIT_DEPRECATED
118373 sqlite3_aggregate_count,
118374#else
118375 0,
118376#endif
118377 sqlite3_bind_blob,
118378 sqlite3_bind_double,
118379 sqlite3_bind_int,
118380 sqlite3_bind_int64,
118381 sqlite3_bind_null,
118382 sqlite3_bind_parameter_count,
118383 sqlite3_bind_parameter_index,
118384 sqlite3_bind_parameter_name,
118385 sqlite3_bind_text,
118386 sqlite3_bind_text16,
118387 sqlite3_bind_value,
118388 sqlite3_busy_handler,
118389 sqlite3_busy_timeout,
118390 sqlite3_changes,
118391 sqlite3_close,
118392 sqlite3_collation_needed,
118393 sqlite3_collation_needed16,
118394 sqlite3_column_blob,
118395 sqlite3_column_bytes,
118396 sqlite3_column_bytes16,
118397 sqlite3_column_count,
118398 sqlite3_column_database_name,
118399 sqlite3_column_database_name16,
118400 sqlite3_column_decltype,
118401 sqlite3_column_decltype16,
118402 sqlite3_column_double,
118403 sqlite3_column_int,
118404 sqlite3_column_int64,
118405 sqlite3_column_name,
118406 sqlite3_column_name16,
118407 sqlite3_column_origin_name,
118408 sqlite3_column_origin_name16,
118409 sqlite3_column_table_name,
118410 sqlite3_column_table_name16,
118411 sqlite3_column_text,
118412 sqlite3_column_text16,
118413 sqlite3_column_type,
118414 sqlite3_column_value,
118415 sqlite3_commit_hook,
118416 sqlite3_complete,
118417 sqlite3_complete16,
118418 sqlite3_create_collation,
118419 sqlite3_create_collation16,
118420 sqlite3_create_function,
118421 sqlite3_create_function16,
118422 sqlite3_create_module,
118423 sqlite3_data_count,
118424 sqlite3_db_handle,
118425 sqlite3_declare_vtab,
118426 sqlite3_enable_shared_cache,
118427 sqlite3_errcode,
118428 sqlite3_errmsg,
118429 sqlite3_errmsg16,
118430 sqlite3_exec,
118431#ifndef SQLITE_OMIT_DEPRECATED
118432 sqlite3_expired,
118433#else
118434 0,
118435#endif
118436 sqlite3_finalize,
118437 sqlite3_free,
118438 sqlite3_free_table,
118439 sqlite3_get_autocommit,
118440 sqlite3_get_auxdata,
118441 sqlite3_get_table,
118442 0, /* Was sqlite3_global_recover(), but that function is deprecated */
118443 sqlite3_interrupt,
118444 sqlite3_last_insert_rowid,
118445 sqlite3_libversion,
118446 sqlite3_libversion_number,
118447 sqlite3_malloc,
118448 sqlite3_mprintf,
118449 sqlite3_open,
118450 sqlite3_open16,
118451 sqlite3_prepare,
118452 sqlite3_prepare16,
118453 sqlite3_profile,
118454 sqlite3_progress_handler,
118455 sqlite3_realloc,
118456 sqlite3_reset,
118457 sqlite3_result_blob,
118458 sqlite3_result_double,
118459 sqlite3_result_error,
118460 sqlite3_result_error16,
118461 sqlite3_result_int,
118462 sqlite3_result_int64,
118463 sqlite3_result_null,
118464 sqlite3_result_text,
118465 sqlite3_result_text16,
118466 sqlite3_result_text16be,
118467 sqlite3_result_text16le,
118468 sqlite3_result_value,
118469 sqlite3_rollback_hook,
118470 sqlite3_set_authorizer,
118471 sqlite3_set_auxdata,
118472 sqlite3_snprintf,
118473 sqlite3_step,
118474 sqlite3_table_column_metadata,
118475#ifndef SQLITE_OMIT_DEPRECATED
118476 sqlite3_thread_cleanup,
118477#else
118478 0,
118479#endif
118480 sqlite3_total_changes,
118481 sqlite3_trace,
118482#ifndef SQLITE_OMIT_DEPRECATED
118483 sqlite3_transfer_bindings,
118484#else
118485 0,
118486#endif
118487 sqlite3_update_hook,
118488 sqlite3_user_data,
118489 sqlite3_value_blob,
118490 sqlite3_value_bytes,
118491 sqlite3_value_bytes16,
118492 sqlite3_value_double,
118493 sqlite3_value_int,
118494 sqlite3_value_int64,
118495 sqlite3_value_numeric_type,
118496 sqlite3_value_text,
118497 sqlite3_value_text16,
118498 sqlite3_value_text16be,
118499 sqlite3_value_text16le,
118500 sqlite3_value_type,
118501 sqlite3_vmprintf,
118502 /*
118503 ** The original API set ends here. All extensions can call any
118504 ** of the APIs above provided that the pointer is not NULL. But
118505 ** before calling APIs that follow, extension should check the
118506 ** sqlite3_libversion_number() to make sure they are dealing with
118507 ** a library that is new enough to support that API.
118508 *************************************************************************
118509 */
118510 sqlite3_overload_function,
118511
118512 /*
118513 ** Added after 3.3.13
118514 */
118515 sqlite3_prepare_v2,
118516 sqlite3_prepare16_v2,
118517 sqlite3_clear_bindings,
118518
118519 /*
118520 ** Added for 3.4.1
118521 */
118522 sqlite3_create_module_v2,
118523
118524 /*
118525 ** Added for 3.5.0
118526 */
118527 sqlite3_bind_zeroblob,
118528 sqlite3_blob_bytes,
118529 sqlite3_blob_close,
118530 sqlite3_blob_open,
118531 sqlite3_blob_read,
118532 sqlite3_blob_write,
118533 sqlite3_create_collation_v2,
118534 sqlite3_file_control,
118535 sqlite3_memory_highwater,
118536 sqlite3_memory_used,
118537#ifdef SQLITE_MUTEX_OMIT
118538 0,
118539 0,
118540 0,
118541 0,
118542 0,
118543#else
118544 sqlite3_mutex_alloc,
118545 sqlite3_mutex_enter,
118546 sqlite3_mutex_free,
118547 sqlite3_mutex_leave,
118548 sqlite3_mutex_try,
118549#endif
118550 sqlite3_open_v2,
118551 sqlite3_release_memory,
118552 sqlite3_result_error_nomem,
118553 sqlite3_result_error_toobig,
118554 sqlite3_sleep,
118555 sqlite3_soft_heap_limit,
118556 sqlite3_vfs_find,
118557 sqlite3_vfs_register,
118558 sqlite3_vfs_unregister,
118559
118560 /*
118561 ** Added for 3.5.8
118562 */
118563 sqlite3_threadsafe,
118564 sqlite3_result_zeroblob,
118565 sqlite3_result_error_code,
118566 sqlite3_test_control,
118567 sqlite3_randomness,
118568 sqlite3_context_db_handle,
118569
118570 /*
118571 ** Added for 3.6.0
118572 */
118573 sqlite3_extended_result_codes,
118574 sqlite3_limit,
118575 sqlite3_next_stmt,
118576 sqlite3_sql,
118577 sqlite3_status,
118578
118579 /*
118580 ** Added for 3.7.4
118581 */
118582 sqlite3_backup_finish,
118583 sqlite3_backup_init,
118584 sqlite3_backup_pagecount,
118585 sqlite3_backup_remaining,
118586 sqlite3_backup_step,
118587#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
118588 sqlite3_compileoption_get,
118589 sqlite3_compileoption_used,
118590#else
118591 0,
118592 0,
118593#endif
118594 sqlite3_create_function_v2,
118595 sqlite3_db_config,
118596 sqlite3_db_mutex,
118597 sqlite3_db_status,
118598 sqlite3_extended_errcode,
118599 sqlite3_log,
118600 sqlite3_soft_heap_limit64,
118601 sqlite3_sourceid,
118602 sqlite3_stmt_status,
118603 sqlite3_strnicmp,
118604#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
118605 sqlite3_unlock_notify,
118606#else
118607 0,
118608#endif
118609#ifndef SQLITE_OMIT_WAL
118610 sqlite3_wal_autocheckpoint,
118611 sqlite3_wal_checkpoint,
118612 sqlite3_wal_hook,
118613#else
118614 0,
118615 0,
118616 0,
118617#endif
118618 sqlite3_blob_reopen,
118619 sqlite3_vtab_config,
118620 sqlite3_vtab_on_conflict,
118621 sqlite3_close_v2,
118622 sqlite3_db_filename,
118623 sqlite3_db_readonly,
118624 sqlite3_db_release_memory,
118625 sqlite3_errstr,
118626 sqlite3_stmt_busy,
118627 sqlite3_stmt_readonly,
118628 sqlite3_stricmp,
118629 sqlite3_uri_boolean,
118630 sqlite3_uri_int64,
118631 sqlite3_uri_parameter,
118632 sqlite3_vsnprintf,
118633 sqlite3_wal_checkpoint_v2,
118634 /* Version 3.8.7 and later */
118635 sqlite3_auto_extension,
118636 sqlite3_bind_blob64,
118637 sqlite3_bind_text64,
118638 sqlite3_cancel_auto_extension,
118639 sqlite3_load_extension,
118640 sqlite3_malloc64,
118641 sqlite3_msize,
118642 sqlite3_realloc64,
118643 sqlite3_reset_auto_extension,
118644 sqlite3_result_blob64,
118645 sqlite3_result_text64,
118646 sqlite3_strglob,
118647 /* Version 3.8.11 and later */
118648 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
118649 sqlite3_value_free,
118650 sqlite3_result_zeroblob64,
118651 sqlite3_bind_zeroblob64,
118652 /* Version 3.9.0 and later */
118653 sqlite3_value_subtype,
118654 sqlite3_result_subtype,
118655 /* Version 3.10.0 and later */
118656 sqlite3_status64,
118657 sqlite3_strlike,
118658 sqlite3_db_cacheflush,
118659 /* Version 3.12.0 and later */
118660 sqlite3_system_errno,
118661 /* Version 3.14.0 and later */
118662 sqlite3_trace_v2,
118663 sqlite3_expanded_sql,
118664 /* Version 3.18.0 and later */
118665 sqlite3_set_last_insert_rowid,
118666 /* Version 3.20.0 and later */
118667 sqlite3_prepare_v3,
118668 sqlite3_prepare16_v3,
118669 sqlite3_bind_pointer,
118670 sqlite3_result_pointer,
118671 sqlite3_value_pointer,
118672 /* Version 3.22.0 and later */
118673 sqlite3_vtab_nochange,
118674 sqlite3_value_nochange,
118675 sqlite3_vtab_collation,
118676 /* Version 3.24.0 and later */
118677 sqlite3_keyword_count,
118678 sqlite3_keyword_name,
118679 sqlite3_keyword_check,
118680 sqlite3_str_new,
118681 sqlite3_str_finish,
118682 sqlite3_str_appendf,
118683 sqlite3_str_vappendf,
118684 sqlite3_str_append,
118685 sqlite3_str_appendall,
118686 sqlite3_str_appendchar,
118687 sqlite3_str_reset,
118688 sqlite3_str_errcode,
118689 sqlite3_str_length,
118690 sqlite3_str_value,
118691 /* Version 3.25.0 and later */
118692 sqlite3_create_window_function
118693};
118694
118695/*
118696** Attempt to load an SQLite extension library contained in the file
118697** zFile. The entry point is zProc. zProc may be 0 in which case a
118698** default entry point name (sqlite3_extension_init) is used. Use
118699** of the default name is recommended.
118700**
118701** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
118702**
118703** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
118704** error message text. The calling function should free this memory
118705** by calling sqlite3DbFree(db, ).
118706*/
118707static int sqlite3LoadExtension(
118708 sqlite3 *db, /* Load the extension into this database connection */
118709 const char *zFile, /* Name of the shared library containing extension */
118710 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
118711 char **pzErrMsg /* Put error message here if not 0 */
118712){
118713 sqlite3_vfs *pVfs = db->pVfs;
118714 void *handle;
118715 sqlite3_loadext_entry xInit;
118716 char *zErrmsg = 0;
118717 const char *zEntry;
118718 char *zAltEntry = 0;
118719 void **aHandle;
118720 u64 nMsg = 300 + sqlite3Strlen30(zFile);
118721 int ii;
118722 int rc;
118723
118724 /* Shared library endings to try if zFile cannot be loaded as written */
118725 static const char *azEndings[] = {
118726#if SQLITE_OS_WIN
118727 "dll"
118728#elif defined(__APPLE__)
118729 "dylib"
118730#else
118731 "so"
118732#endif
118733 };
118734
118735
118736 if( pzErrMsg ) *pzErrMsg = 0;
118737
118738 /* Ticket #1863. To avoid a creating security problems for older
118739 ** applications that relink against newer versions of SQLite, the
118740 ** ability to run load_extension is turned off by default. One
118741 ** must call either sqlite3_enable_load_extension(db) or
118742 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
118743 ** to turn on extension loading.
118744 */
118745 if( (db->flags & SQLITE_LoadExtension)==0 ){
118746 if( pzErrMsg ){
118747 *pzErrMsg = sqlite3_mprintf("not authorized");
118748 }
118749 return SQLITE_ERROR;
118750 }
118751
118752 zEntry = zProc ? zProc : "sqlite3_extension_init";
118753
118754 handle = sqlite3OsDlOpen(pVfs, zFile);
118755#if SQLITE_OS_UNIX || SQLITE_OS_WIN
118756 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
118757 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
118758 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
118759 handle = sqlite3OsDlOpen(pVfs, zAltFile);
118760 sqlite3_free(zAltFile);
118761 }
118762#endif
118763 if( handle==0 ){
118764 if( pzErrMsg ){
118765 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
118766 if( zErrmsg ){
118767 sqlite3_snprintf(nMsg, zErrmsg,
118768 "unable to open shared library [%s]", zFile);
118769 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
118770 }
118771 }
118772 return SQLITE_ERROR;
118773 }
118774 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
118775
118776 /* If no entry point was specified and the default legacy
118777 ** entry point name "sqlite3_extension_init" was not found, then
118778 ** construct an entry point name "sqlite3_X_init" where the X is
118779 ** replaced by the lowercase value of every ASCII alphabetic
118780 ** character in the filename after the last "/" upto the first ".",
118781 ** and eliding the first three characters if they are "lib".
118782 ** Examples:
118783 **
118784 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
118785 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
118786 */
118787 if( xInit==0 && zProc==0 ){
118788 int iFile, iEntry, c;
118789 int ncFile = sqlite3Strlen30(zFile);
118790 zAltEntry = sqlite3_malloc64(ncFile+30);
118791 if( zAltEntry==0 ){
118792 sqlite3OsDlClose(pVfs, handle);
118793 return SQLITE_NOMEM_BKPT;
118794 }
118795 memcpy(zAltEntry, "sqlite3_", 8);
118796 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
118797 iFile++;
118798 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
118799 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
118800 if( sqlite3Isalpha(c) ){
118801 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
118802 }
118803 }
118804 memcpy(zAltEntry+iEntry, "_init", 6);
118805 zEntry = zAltEntry;
118806 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
118807 }
118808 if( xInit==0 ){
118809 if( pzErrMsg ){
118810 nMsg += sqlite3Strlen30(zEntry);
118811 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
118812 if( zErrmsg ){
118813 sqlite3_snprintf(nMsg, zErrmsg,
118814 "no entry point [%s] in shared library [%s]", zEntry, zFile);
118815 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
118816 }
118817 }
118818 sqlite3OsDlClose(pVfs, handle);
118819 sqlite3_free(zAltEntry);
118820 return SQLITE_ERROR;
118821 }
118822 sqlite3_free(zAltEntry);
118823 rc = xInit(db, &zErrmsg, &sqlite3Apis);
118824 if( rc ){
118825 if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK;
118826 if( pzErrMsg ){
118827 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
118828 }
118829 sqlite3_free(zErrmsg);
118830 sqlite3OsDlClose(pVfs, handle);
118831 return SQLITE_ERROR;
118832 }
118833
118834 /* Append the new shared library handle to the db->aExtension array. */
118835 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
118836 if( aHandle==0 ){
118837 return SQLITE_NOMEM_BKPT;
118838 }
118839 if( db->nExtension>0 ){
118840 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
118841 }
118842 sqlite3DbFree(db, db->aExtension);
118843 db->aExtension = aHandle;
118844
118845 db->aExtension[db->nExtension++] = handle;
118846 return SQLITE_OK;
118847}
118848SQLITE_API int sqlite3_load_extension(
118849 sqlite3 *db, /* Load the extension into this database connection */
118850 const char *zFile, /* Name of the shared library containing extension */
118851 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
118852 char **pzErrMsg /* Put error message here if not 0 */
118853){
118854 int rc;
118855 sqlite3_mutex_enter(db->mutex);
118856 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
118857 rc = sqlite3ApiExit(db, rc);
118858 sqlite3_mutex_leave(db->mutex);
118859 return rc;
118860}
118861
118862/*
118863** Call this routine when the database connection is closing in order
118864** to clean up loaded extensions
118865*/
118866SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
118867 int i;
118868 assert( sqlite3_mutex_held(db->mutex) );
118869 for(i=0; i<db->nExtension; i++){
118870 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
118871 }
118872 sqlite3DbFree(db, db->aExtension);
118873}
118874
118875/*
118876** Enable or disable extension loading. Extension loading is disabled by
118877** default so as not to open security holes in older applications.
118878*/
118879SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
118880 sqlite3_mutex_enter(db->mutex);
118881 if( onoff ){
118882 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
118883 }else{
118884 db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
118885 }
118886 sqlite3_mutex_leave(db->mutex);
118887 return SQLITE_OK;
118888}
118889
118890#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
118891
118892/*
118893** The following object holds the list of automatically loaded
118894** extensions.
118895**
118896** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER
118897** mutex must be held while accessing this list.
118898*/
118899typedef struct sqlite3AutoExtList sqlite3AutoExtList;
118900static SQLITE_WSD struct sqlite3AutoExtList {
118901 u32 nExt; /* Number of entries in aExt[] */
118902 void (**aExt)(void); /* Pointers to the extension init functions */
118903} sqlite3Autoext = { 0, 0 };
118904
118905/* The "wsdAutoext" macro will resolve to the autoextension
118906** state vector. If writable static data is unsupported on the target,
118907** we have to locate the state vector at run-time. In the more common
118908** case where writable static data is supported, wsdStat can refer directly
118909** to the "sqlite3Autoext" state vector declared above.
118910*/
118911#ifdef SQLITE_OMIT_WSD
118912# define wsdAutoextInit \
118913 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
118914# define wsdAutoext x[0]
118915#else
118916# define wsdAutoextInit
118917# define wsdAutoext sqlite3Autoext
118918#endif
118919
118920
118921/*
118922** Register a statically linked extension that is automatically
118923** loaded by every new database connection.
118924*/
118925SQLITE_API int sqlite3_auto_extension(
118926 void (*xInit)(void)
118927){
118928 int rc = SQLITE_OK;
118929#ifndef SQLITE_OMIT_AUTOINIT
118930 rc = sqlite3_initialize();
118931 if( rc ){
118932 return rc;
118933 }else
118934#endif
118935 {
118936 u32 i;
118937#if SQLITE_THREADSAFE
118938 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
118939#endif
118940 wsdAutoextInit;
118941 sqlite3_mutex_enter(mutex);
118942 for(i=0; i<wsdAutoext.nExt; i++){
118943 if( wsdAutoext.aExt[i]==xInit ) break;
118944 }
118945 if( i==wsdAutoext.nExt ){
118946 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
118947 void (**aNew)(void);
118948 aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte);
118949 if( aNew==0 ){
118950 rc = SQLITE_NOMEM_BKPT;
118951 }else{
118952 wsdAutoext.aExt = aNew;
118953 wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
118954 wsdAutoext.nExt++;
118955 }
118956 }
118957 sqlite3_mutex_leave(mutex);
118958 assert( (rc&0xff)==rc );
118959 return rc;
118960 }
118961}
118962
118963/*
118964** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
118965** set of routines that is invoked for each new database connection, if it
118966** is currently on the list. If xInit is not on the list, then this
118967** routine is a no-op.
118968**
118969** Return 1 if xInit was found on the list and removed. Return 0 if xInit
118970** was not on the list.
118971*/
118972SQLITE_API int sqlite3_cancel_auto_extension(
118973 void (*xInit)(void)
118974){
118975#if SQLITE_THREADSAFE
118976 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
118977#endif
118978 int i;
118979 int n = 0;
118980 wsdAutoextInit;
118981 sqlite3_mutex_enter(mutex);
118982 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
118983 if( wsdAutoext.aExt[i]==xInit ){
118984 wsdAutoext.nExt--;
118985 wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
118986 n++;
118987 break;
118988 }
118989 }
118990 sqlite3_mutex_leave(mutex);
118991 return n;
118992}
118993
118994/*
118995** Reset the automatic extension loading mechanism.
118996*/
118997SQLITE_API void sqlite3_reset_auto_extension(void){
118998#ifndef SQLITE_OMIT_AUTOINIT
118999 if( sqlite3_initialize()==SQLITE_OK )
119000#endif
119001 {
119002#if SQLITE_THREADSAFE
119003 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
119004#endif
119005 wsdAutoextInit;
119006 sqlite3_mutex_enter(mutex);
119007 sqlite3_free(wsdAutoext.aExt);
119008 wsdAutoext.aExt = 0;
119009 wsdAutoext.nExt = 0;
119010 sqlite3_mutex_leave(mutex);
119011 }
119012}
119013
119014/*
119015** Load all automatic extensions.
119016**
119017** If anything goes wrong, set an error in the database connection.
119018*/
119019SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
119020 u32 i;
119021 int go = 1;
119022 int rc;
119023 sqlite3_loadext_entry xInit;
119024
119025 wsdAutoextInit;
119026 if( wsdAutoext.nExt==0 ){
119027 /* Common case: early out without every having to acquire a mutex */
119028 return;
119029 }
119030 for(i=0; go; i++){
119031 char *zErrmsg;
119032#if SQLITE_THREADSAFE
119033 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
119034#endif
119035#ifdef SQLITE_OMIT_LOAD_EXTENSION
119036 const sqlite3_api_routines *pThunk = 0;
119037#else
119038 const sqlite3_api_routines *pThunk = &sqlite3Apis;
119039#endif
119040 sqlite3_mutex_enter(mutex);
119041 if( i>=wsdAutoext.nExt ){
119042 xInit = 0;
119043 go = 0;
119044 }else{
119045 xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i];
119046 }
119047 sqlite3_mutex_leave(mutex);
119048 zErrmsg = 0;
119049 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
119050 sqlite3ErrorWithMsg(db, rc,
119051 "automatic extension loading failed: %s", zErrmsg);
119052 go = 0;
119053 }
119054 sqlite3_free(zErrmsg);
119055 }
119056}
119057
119058/************** End of loadext.c *********************************************/
119059/************** Begin file pragma.c ******************************************/
119060/*
119061** 2003 April 6
119062**
119063** The author disclaims copyright to this source code. In place of
119064** a legal notice, here is a blessing:
119065**
119066** May you do good and not evil.
119067** May you find forgiveness for yourself and forgive others.
119068** May you share freely, never taking more than you give.
119069**
119070*************************************************************************
119071** This file contains code used to implement the PRAGMA command.
119072*/
119073/* #include "sqliteInt.h" */
119074
119075#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
119076# if defined(__APPLE__)
119077# define SQLITE_ENABLE_LOCKING_STYLE 1
119078# else
119079# define SQLITE_ENABLE_LOCKING_STYLE 0
119080# endif
119081#endif
119082
119083/***************************************************************************
119084** The "pragma.h" include file is an automatically generated file that
119085** that includes the PragType_XXXX macro definitions and the aPragmaName[]
119086** object. This ensures that the aPragmaName[] table is arranged in
119087** lexicographical order to facility a binary search of the pragma name.
119088** Do not edit pragma.h directly. Edit and rerun the script in at
119089** ../tool/mkpragmatab.tcl. */
119090/************** Include pragma.h in the middle of pragma.c *******************/
119091/************** Begin file pragma.h ******************************************/
119092/* DO NOT EDIT!
119093** This file is automatically generated by the script at
119094** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit
119095** that script and rerun it.
119096*/
119097
119098/* The various pragma types */
119099#define PragTyp_HEADER_VALUE 0
119100#define PragTyp_AUTO_VACUUM 1
119101#define PragTyp_FLAG 2
119102#define PragTyp_BUSY_TIMEOUT 3
119103#define PragTyp_CACHE_SIZE 4
119104#define PragTyp_CACHE_SPILL 5
119105#define PragTyp_CASE_SENSITIVE_LIKE 6
119106#define PragTyp_COLLATION_LIST 7
119107#define PragTyp_COMPILE_OPTIONS 8
119108#define PragTyp_DATA_STORE_DIRECTORY 9
119109#define PragTyp_DATABASE_LIST 10
119110#define PragTyp_DEFAULT_CACHE_SIZE 11
119111#define PragTyp_ENCODING 12
119112#define PragTyp_FOREIGN_KEY_CHECK 13
119113#define PragTyp_FOREIGN_KEY_LIST 14
119114#define PragTyp_FUNCTION_LIST 15
119115#define PragTyp_INCREMENTAL_VACUUM 16
119116#define PragTyp_INDEX_INFO 17
119117#define PragTyp_INDEX_LIST 18
119118#define PragTyp_INTEGRITY_CHECK 19
119119#define PragTyp_JOURNAL_MODE 20
119120#define PragTyp_JOURNAL_SIZE_LIMIT 21
119121#define PragTyp_LOCK_PROXY_FILE 22
119122#define PragTyp_LOCKING_MODE 23
119123#define PragTyp_PAGE_COUNT 24
119124#define PragTyp_MMAP_SIZE 25
119125#define PragTyp_MODULE_LIST 26
119126#define PragTyp_OPTIMIZE 27
119127#define PragTyp_PAGE_SIZE 28
119128#define PragTyp_PRAGMA_LIST 29
119129#define PragTyp_SECURE_DELETE 30
119130#define PragTyp_SHRINK_MEMORY 31
119131#define PragTyp_SOFT_HEAP_LIMIT 32
119132#define PragTyp_SYNCHRONOUS 33
119133#define PragTyp_TABLE_INFO 34
119134#define PragTyp_TEMP_STORE 35
119135#define PragTyp_TEMP_STORE_DIRECTORY 36
119136#define PragTyp_THREADS 37
119137#define PragTyp_WAL_AUTOCHECKPOINT 38
119138#define PragTyp_WAL_CHECKPOINT 39
119139#define PragTyp_ACTIVATE_EXTENSIONS 40
119140#define PragTyp_HEXKEY 41
119141#define PragTyp_KEY 42
119142#define PragTyp_REKEY 43
119143#define PragTyp_LOCK_STATUS 44
119144#define PragTyp_PARSER_TRACE 45
119145#define PragTyp_STATS 46
119146
119147/* Property flags associated with various pragma. */
119148#define PragFlg_NeedSchema 0x01 /* Force schema load before running */
119149#define PragFlg_NoColumns 0x02 /* OP_ResultRow called with zero columns */
119150#define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
119151#define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
119152#define PragFlg_Result0 0x10 /* Acts as query when no argument */
119153#define PragFlg_Result1 0x20 /* Acts as query when has one argument */
119154#define PragFlg_SchemaOpt 0x40 /* Schema restricts name search if present */
119155#define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
119156
119157/* Names of columns for pragmas that return multi-column result
119158** or that return single-column results where the name of the
119159** result column is different from the name of the pragma
119160*/
119161static const char *const pragCName[] = {
119162 /* 0 */ "cache_size", /* Used by: default_cache_size */
119163 /* 1 */ "cid", /* Used by: table_info */
119164 /* 2 */ "name",
119165 /* 3 */ "type",
119166 /* 4 */ "notnull",
119167 /* 5 */ "dflt_value",
119168 /* 6 */ "pk",
119169 /* 7 */ "tbl", /* Used by: stats */
119170 /* 8 */ "idx",
119171 /* 9 */ "wdth",
119172 /* 10 */ "hght",
119173 /* 11 */ "flgs",
119174 /* 12 */ "seqno", /* Used by: index_info */
119175 /* 13 */ "cid",
119176 /* 14 */ "name",
119177 /* 15 */ "seqno", /* Used by: index_xinfo */
119178 /* 16 */ "cid",
119179 /* 17 */ "name",
119180 /* 18 */ "desc",
119181 /* 19 */ "coll",
119182 /* 20 */ "key",
119183 /* 21 */ "seq", /* Used by: index_list */
119184 /* 22 */ "name",
119185 /* 23 */ "unique",
119186 /* 24 */ "origin",
119187 /* 25 */ "partial",
119188 /* 26 */ "seq", /* Used by: database_list */
119189 /* 27 */ "name",
119190 /* 28 */ "file",
119191 /* 29 */ "name", /* Used by: function_list */
119192 /* 30 */ "builtin",
119193 /* 31 */ "name", /* Used by: module_list pragma_list */
119194 /* 32 */ "seq", /* Used by: collation_list */
119195 /* 33 */ "name",
119196 /* 34 */ "id", /* Used by: foreign_key_list */
119197 /* 35 */ "seq",
119198 /* 36 */ "table",
119199 /* 37 */ "from",
119200 /* 38 */ "to",
119201 /* 39 */ "on_update",
119202 /* 40 */ "on_delete",
119203 /* 41 */ "match",
119204 /* 42 */ "table", /* Used by: foreign_key_check */
119205 /* 43 */ "rowid",
119206 /* 44 */ "parent",
119207 /* 45 */ "fkid",
119208 /* 46 */ "busy", /* Used by: wal_checkpoint */
119209 /* 47 */ "log",
119210 /* 48 */ "checkpointed",
119211 /* 49 */ "timeout", /* Used by: busy_timeout */
119212 /* 50 */ "database", /* Used by: lock_status */
119213 /* 51 */ "status",
119214};
119215
119216/* Definitions of all built-in pragmas */
119217typedef struct PragmaName {
119218 const char *const zName; /* Name of pragma */
119219 u8 ePragTyp; /* PragTyp_XXX value */
119220 u8 mPragFlg; /* Zero or more PragFlg_XXX values */
119221 u8 iPragCName; /* Start of column names in pragCName[] */
119222 u8 nPragCName; /* Num of col names. 0 means use pragma name */
119223 u32 iArg; /* Extra argument */
119224} PragmaName;
119225static const PragmaName aPragmaName[] = {
119226#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
119227 {/* zName: */ "activate_extensions",
119228 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS,
119229 /* ePragFlg: */ 0,
119230 /* ColNames: */ 0, 0,
119231 /* iArg: */ 0 },
119232#endif
119233#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
119234 {/* zName: */ "application_id",
119235 /* ePragTyp: */ PragTyp_HEADER_VALUE,
119236 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
119237 /* ColNames: */ 0, 0,
119238 /* iArg: */ BTREE_APPLICATION_ID },
119239#endif
119240#if !defined(SQLITE_OMIT_AUTOVACUUM)
119241 {/* zName: */ "auto_vacuum",
119242 /* ePragTyp: */ PragTyp_AUTO_VACUUM,
119243 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119244 /* ColNames: */ 0, 0,
119245 /* iArg: */ 0 },
119246#endif
119247#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119248#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
119249 {/* zName: */ "automatic_index",
119250 /* ePragTyp: */ PragTyp_FLAG,
119251 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119252 /* ColNames: */ 0, 0,
119253 /* iArg: */ SQLITE_AutoIndex },
119254#endif
119255#endif
119256 {/* zName: */ "busy_timeout",
119257 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT,
119258 /* ePragFlg: */ PragFlg_Result0,
119259 /* ColNames: */ 49, 1,
119260 /* iArg: */ 0 },
119261#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119262 {/* zName: */ "cache_size",
119263 /* ePragTyp: */ PragTyp_CACHE_SIZE,
119264 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119265 /* ColNames: */ 0, 0,
119266 /* iArg: */ 0 },
119267#endif
119268#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119269 {/* zName: */ "cache_spill",
119270 /* ePragTyp: */ PragTyp_CACHE_SPILL,
119271 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119272 /* ColNames: */ 0, 0,
119273 /* iArg: */ 0 },
119274#endif
119275 {/* zName: */ "case_sensitive_like",
119276 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE,
119277 /* ePragFlg: */ PragFlg_NoColumns,
119278 /* ColNames: */ 0, 0,
119279 /* iArg: */ 0 },
119280 {/* zName: */ "cell_size_check",
119281 /* ePragTyp: */ PragTyp_FLAG,
119282 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119283 /* ColNames: */ 0, 0,
119284 /* iArg: */ SQLITE_CellSizeCk },
119285#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119286 {/* zName: */ "checkpoint_fullfsync",
119287 /* ePragTyp: */ PragTyp_FLAG,
119288 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119289 /* ColNames: */ 0, 0,
119290 /* iArg: */ SQLITE_CkptFullFSync },
119291#endif
119292#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119293 {/* zName: */ "collation_list",
119294 /* ePragTyp: */ PragTyp_COLLATION_LIST,
119295 /* ePragFlg: */ PragFlg_Result0,
119296 /* ColNames: */ 32, 2,
119297 /* iArg: */ 0 },
119298#endif
119299#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
119300 {/* zName: */ "compile_options",
119301 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS,
119302 /* ePragFlg: */ PragFlg_Result0,
119303 /* ColNames: */ 0, 0,
119304 /* iArg: */ 0 },
119305#endif
119306#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119307 {/* zName: */ "count_changes",
119308 /* ePragTyp: */ PragTyp_FLAG,
119309 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119310 /* ColNames: */ 0, 0,
119311 /* iArg: */ SQLITE_CountRows },
119312#endif
119313#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
119314 {/* zName: */ "data_store_directory",
119315 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY,
119316 /* ePragFlg: */ PragFlg_NoColumns1,
119317 /* ColNames: */ 0, 0,
119318 /* iArg: */ 0 },
119319#endif
119320#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
119321 {/* zName: */ "data_version",
119322 /* ePragTyp: */ PragTyp_HEADER_VALUE,
119323 /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0,
119324 /* ColNames: */ 0, 0,
119325 /* iArg: */ BTREE_DATA_VERSION },
119326#endif
119327#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119328 {/* zName: */ "database_list",
119329 /* ePragTyp: */ PragTyp_DATABASE_LIST,
119330 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0,
119331 /* ColNames: */ 26, 3,
119332 /* iArg: */ 0 },
119333#endif
119334#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
119335 {/* zName: */ "default_cache_size",
119336 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE,
119337 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119338 /* ColNames: */ 0, 1,
119339 /* iArg: */ 0 },
119340#endif
119341#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119342#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119343 {/* zName: */ "defer_foreign_keys",
119344 /* ePragTyp: */ PragTyp_FLAG,
119345 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119346 /* ColNames: */ 0, 0,
119347 /* iArg: */ SQLITE_DeferFKs },
119348#endif
119349#endif
119350#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119351 {/* zName: */ "empty_result_callbacks",
119352 /* ePragTyp: */ PragTyp_FLAG,
119353 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119354 /* ColNames: */ 0, 0,
119355 /* iArg: */ SQLITE_NullCallback },
119356#endif
119357#if !defined(SQLITE_OMIT_UTF16)
119358 {/* zName: */ "encoding",
119359 /* ePragTyp: */ PragTyp_ENCODING,
119360 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119361 /* ColNames: */ 0, 0,
119362 /* iArg: */ 0 },
119363#endif
119364#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119365 {/* zName: */ "foreign_key_check",
119366 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK,
119367 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0,
119368 /* ColNames: */ 42, 4,
119369 /* iArg: */ 0 },
119370#endif
119371#if !defined(SQLITE_OMIT_FOREIGN_KEY)
119372 {/* zName: */ "foreign_key_list",
119373 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST,
119374 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119375 /* ColNames: */ 34, 8,
119376 /* iArg: */ 0 },
119377#endif
119378#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119379#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
119380 {/* zName: */ "foreign_keys",
119381 /* ePragTyp: */ PragTyp_FLAG,
119382 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119383 /* ColNames: */ 0, 0,
119384 /* iArg: */ SQLITE_ForeignKeys },
119385#endif
119386#endif
119387#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
119388 {/* zName: */ "freelist_count",
119389 /* ePragTyp: */ PragTyp_HEADER_VALUE,
119390 /* ePragFlg: */ PragFlg_ReadOnly|PragFlg_Result0,
119391 /* ColNames: */ 0, 0,
119392 /* iArg: */ BTREE_FREE_PAGE_COUNT },
119393#endif
119394#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119395 {/* zName: */ "full_column_names",
119396 /* ePragTyp: */ PragTyp_FLAG,
119397 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119398 /* ColNames: */ 0, 0,
119399 /* iArg: */ SQLITE_FullColNames },
119400 {/* zName: */ "fullfsync",
119401 /* ePragTyp: */ PragTyp_FLAG,
119402 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119403 /* ColNames: */ 0, 0,
119404 /* iArg: */ SQLITE_FullFSync },
119405#endif
119406#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119407#if defined(SQLITE_INTROSPECTION_PRAGMAS)
119408 {/* zName: */ "function_list",
119409 /* ePragTyp: */ PragTyp_FUNCTION_LIST,
119410 /* ePragFlg: */ PragFlg_Result0,
119411 /* ColNames: */ 29, 2,
119412 /* iArg: */ 0 },
119413#endif
119414#endif
119415#if defined(SQLITE_HAS_CODEC)
119416 {/* zName: */ "hexkey",
119417 /* ePragTyp: */ PragTyp_HEXKEY,
119418 /* ePragFlg: */ 0,
119419 /* ColNames: */ 0, 0,
119420 /* iArg: */ 0 },
119421 {/* zName: */ "hexrekey",
119422 /* ePragTyp: */ PragTyp_HEXKEY,
119423 /* ePragFlg: */ 0,
119424 /* ColNames: */ 0, 0,
119425 /* iArg: */ 0 },
119426#endif
119427#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119428#if !defined(SQLITE_OMIT_CHECK)
119429 {/* zName: */ "ignore_check_constraints",
119430 /* ePragTyp: */ PragTyp_FLAG,
119431 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119432 /* ColNames: */ 0, 0,
119433 /* iArg: */ SQLITE_IgnoreChecks },
119434#endif
119435#endif
119436#if !defined(SQLITE_OMIT_AUTOVACUUM)
119437 {/* zName: */ "incremental_vacuum",
119438 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM,
119439 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_NoColumns,
119440 /* ColNames: */ 0, 0,
119441 /* iArg: */ 0 },
119442#endif
119443#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119444 {/* zName: */ "index_info",
119445 /* ePragTyp: */ PragTyp_INDEX_INFO,
119446 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119447 /* ColNames: */ 12, 3,
119448 /* iArg: */ 0 },
119449 {/* zName: */ "index_list",
119450 /* ePragTyp: */ PragTyp_INDEX_LIST,
119451 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119452 /* ColNames: */ 21, 5,
119453 /* iArg: */ 0 },
119454 {/* zName: */ "index_xinfo",
119455 /* ePragTyp: */ PragTyp_INDEX_INFO,
119456 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119457 /* ColNames: */ 15, 6,
119458 /* iArg: */ 1 },
119459#endif
119460#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
119461 {/* zName: */ "integrity_check",
119462 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
119463 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1,
119464 /* ColNames: */ 0, 0,
119465 /* iArg: */ 0 },
119466#endif
119467#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119468 {/* zName: */ "journal_mode",
119469 /* ePragTyp: */ PragTyp_JOURNAL_MODE,
119470 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
119471 /* ColNames: */ 0, 0,
119472 /* iArg: */ 0 },
119473 {/* zName: */ "journal_size_limit",
119474 /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT,
119475 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq,
119476 /* ColNames: */ 0, 0,
119477 /* iArg: */ 0 },
119478#endif
119479#if defined(SQLITE_HAS_CODEC)
119480 {/* zName: */ "key",
119481 /* ePragTyp: */ PragTyp_KEY,
119482 /* ePragFlg: */ 0,
119483 /* ColNames: */ 0, 0,
119484 /* iArg: */ 0 },
119485#endif
119486#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119487 {/* zName: */ "legacy_alter_table",
119488 /* ePragTyp: */ PragTyp_FLAG,
119489 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119490 /* ColNames: */ 0, 0,
119491 /* iArg: */ SQLITE_LegacyAlter },
119492 {/* zName: */ "legacy_file_format",
119493 /* ePragTyp: */ PragTyp_FLAG,
119494 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119495 /* ColNames: */ 0, 0,
119496 /* iArg: */ SQLITE_LegacyFileFmt },
119497#endif
119498#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
119499 {/* zName: */ "lock_proxy_file",
119500 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE,
119501 /* ePragFlg: */ PragFlg_NoColumns1,
119502 /* ColNames: */ 0, 0,
119503 /* iArg: */ 0 },
119504#endif
119505#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
119506 {/* zName: */ "lock_status",
119507 /* ePragTyp: */ PragTyp_LOCK_STATUS,
119508 /* ePragFlg: */ PragFlg_Result0,
119509 /* ColNames: */ 50, 2,
119510 /* iArg: */ 0 },
119511#endif
119512#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119513 {/* zName: */ "locking_mode",
119514 /* ePragTyp: */ PragTyp_LOCKING_MODE,
119515 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq,
119516 /* ColNames: */ 0, 0,
119517 /* iArg: */ 0 },
119518 {/* zName: */ "max_page_count",
119519 /* ePragTyp: */ PragTyp_PAGE_COUNT,
119520 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
119521 /* ColNames: */ 0, 0,
119522 /* iArg: */ 0 },
119523 {/* zName: */ "mmap_size",
119524 /* ePragTyp: */ PragTyp_MMAP_SIZE,
119525 /* ePragFlg: */ 0,
119526 /* ColNames: */ 0, 0,
119527 /* iArg: */ 0 },
119528#endif
119529#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119530#if !defined(SQLITE_OMIT_VIRTUALTABLE)
119531#if defined(SQLITE_INTROSPECTION_PRAGMAS)
119532 {/* zName: */ "module_list",
119533 /* ePragTyp: */ PragTyp_MODULE_LIST,
119534 /* ePragFlg: */ PragFlg_Result0,
119535 /* ColNames: */ 31, 1,
119536 /* iArg: */ 0 },
119537#endif
119538#endif
119539#endif
119540 {/* zName: */ "optimize",
119541 /* ePragTyp: */ PragTyp_OPTIMIZE,
119542 /* ePragFlg: */ PragFlg_Result1|PragFlg_NeedSchema,
119543 /* ColNames: */ 0, 0,
119544 /* iArg: */ 0 },
119545#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119546 {/* zName: */ "page_count",
119547 /* ePragTyp: */ PragTyp_PAGE_COUNT,
119548 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
119549 /* ColNames: */ 0, 0,
119550 /* iArg: */ 0 },
119551 {/* zName: */ "page_size",
119552 /* ePragTyp: */ PragTyp_PAGE_SIZE,
119553 /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119554 /* ColNames: */ 0, 0,
119555 /* iArg: */ 0 },
119556#endif
119557#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_PARSER_TRACE)
119558 {/* zName: */ "parser_trace",
119559 /* ePragTyp: */ PragTyp_PARSER_TRACE,
119560 /* ePragFlg: */ 0,
119561 /* ColNames: */ 0, 0,
119562 /* iArg: */ 0 },
119563#endif
119564#if defined(SQLITE_INTROSPECTION_PRAGMAS)
119565 {/* zName: */ "pragma_list",
119566 /* ePragTyp: */ PragTyp_PRAGMA_LIST,
119567 /* ePragFlg: */ PragFlg_Result0,
119568 /* ColNames: */ 31, 1,
119569 /* iArg: */ 0 },
119570#endif
119571#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119572 {/* zName: */ "query_only",
119573 /* ePragTyp: */ PragTyp_FLAG,
119574 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119575 /* ColNames: */ 0, 0,
119576 /* iArg: */ SQLITE_QueryOnly },
119577#endif
119578#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
119579 {/* zName: */ "quick_check",
119580 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK,
119581 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1,
119582 /* ColNames: */ 0, 0,
119583 /* iArg: */ 0 },
119584#endif
119585#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119586 {/* zName: */ "read_uncommitted",
119587 /* ePragTyp: */ PragTyp_FLAG,
119588 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119589 /* ColNames: */ 0, 0,
119590 /* iArg: */ SQLITE_ReadUncommit },
119591 {/* zName: */ "recursive_triggers",
119592 /* ePragTyp: */ PragTyp_FLAG,
119593 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119594 /* ColNames: */ 0, 0,
119595 /* iArg: */ SQLITE_RecTriggers },
119596#endif
119597#if defined(SQLITE_HAS_CODEC)
119598 {/* zName: */ "rekey",
119599 /* ePragTyp: */ PragTyp_REKEY,
119600 /* ePragFlg: */ 0,
119601 /* ColNames: */ 0, 0,
119602 /* iArg: */ 0 },
119603#endif
119604#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119605 {/* zName: */ "reverse_unordered_selects",
119606 /* ePragTyp: */ PragTyp_FLAG,
119607 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119608 /* ColNames: */ 0, 0,
119609 /* iArg: */ SQLITE_ReverseOrder },
119610#endif
119611#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
119612 {/* zName: */ "schema_version",
119613 /* ePragTyp: */ PragTyp_HEADER_VALUE,
119614 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
119615 /* ColNames: */ 0, 0,
119616 /* iArg: */ BTREE_SCHEMA_VERSION },
119617#endif
119618#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119619 {/* zName: */ "secure_delete",
119620 /* ePragTyp: */ PragTyp_SECURE_DELETE,
119621 /* ePragFlg: */ PragFlg_Result0,
119622 /* ColNames: */ 0, 0,
119623 /* iArg: */ 0 },
119624#endif
119625#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119626 {/* zName: */ "short_column_names",
119627 /* ePragTyp: */ PragTyp_FLAG,
119628 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119629 /* ColNames: */ 0, 0,
119630 /* iArg: */ SQLITE_ShortColNames },
119631#endif
119632 {/* zName: */ "shrink_memory",
119633 /* ePragTyp: */ PragTyp_SHRINK_MEMORY,
119634 /* ePragFlg: */ PragFlg_NoColumns,
119635 /* ColNames: */ 0, 0,
119636 /* iArg: */ 0 },
119637 {/* zName: */ "soft_heap_limit",
119638 /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT,
119639 /* ePragFlg: */ PragFlg_Result0,
119640 /* ColNames: */ 0, 0,
119641 /* iArg: */ 0 },
119642#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119643#if defined(SQLITE_DEBUG)
119644 {/* zName: */ "sql_trace",
119645 /* ePragTyp: */ PragTyp_FLAG,
119646 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119647 /* ColNames: */ 0, 0,
119648 /* iArg: */ SQLITE_SqlTrace },
119649#endif
119650#endif
119651#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
119652 {/* zName: */ "stats",
119653 /* ePragTyp: */ PragTyp_STATS,
119654 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
119655 /* ColNames: */ 7, 5,
119656 /* iArg: */ 0 },
119657#endif
119658#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119659 {/* zName: */ "synchronous",
119660 /* ePragTyp: */ PragTyp_SYNCHRONOUS,
119661 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
119662 /* ColNames: */ 0, 0,
119663 /* iArg: */ 0 },
119664#endif
119665#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
119666 {/* zName: */ "table_info",
119667 /* ePragTyp: */ PragTyp_TABLE_INFO,
119668 /* ePragFlg: */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
119669 /* ColNames: */ 1, 6,
119670 /* iArg: */ 0 },
119671#endif
119672#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
119673 {/* zName: */ "temp_store",
119674 /* ePragTyp: */ PragTyp_TEMP_STORE,
119675 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119676 /* ColNames: */ 0, 0,
119677 /* iArg: */ 0 },
119678 {/* zName: */ "temp_store_directory",
119679 /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY,
119680 /* ePragFlg: */ PragFlg_NoColumns1,
119681 /* ColNames: */ 0, 0,
119682 /* iArg: */ 0 },
119683#endif
119684 {/* zName: */ "threads",
119685 /* ePragTyp: */ PragTyp_THREADS,
119686 /* ePragFlg: */ PragFlg_Result0,
119687 /* ColNames: */ 0, 0,
119688 /* iArg: */ 0 },
119689#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
119690 {/* zName: */ "user_version",
119691 /* ePragTyp: */ PragTyp_HEADER_VALUE,
119692 /* ePragFlg: */ PragFlg_NoColumns1|PragFlg_Result0,
119693 /* ColNames: */ 0, 0,
119694 /* iArg: */ BTREE_USER_VERSION },
119695#endif
119696#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119697#if defined(SQLITE_DEBUG)
119698 {/* zName: */ "vdbe_addoptrace",
119699 /* ePragTyp: */ PragTyp_FLAG,
119700 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119701 /* ColNames: */ 0, 0,
119702 /* iArg: */ SQLITE_VdbeAddopTrace },
119703 {/* zName: */ "vdbe_debug",
119704 /* ePragTyp: */ PragTyp_FLAG,
119705 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119706 /* ColNames: */ 0, 0,
119707 /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
119708 {/* zName: */ "vdbe_eqp",
119709 /* ePragTyp: */ PragTyp_FLAG,
119710 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119711 /* ColNames: */ 0, 0,
119712 /* iArg: */ SQLITE_VdbeEQP },
119713 {/* zName: */ "vdbe_listing",
119714 /* ePragTyp: */ PragTyp_FLAG,
119715 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119716 /* ColNames: */ 0, 0,
119717 /* iArg: */ SQLITE_VdbeListing },
119718 {/* zName: */ "vdbe_trace",
119719 /* ePragTyp: */ PragTyp_FLAG,
119720 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119721 /* ColNames: */ 0, 0,
119722 /* iArg: */ SQLITE_VdbeTrace },
119723#endif
119724#endif
119725#if !defined(SQLITE_OMIT_WAL)
119726 {/* zName: */ "wal_autocheckpoint",
119727 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT,
119728 /* ePragFlg: */ 0,
119729 /* ColNames: */ 0, 0,
119730 /* iArg: */ 0 },
119731 {/* zName: */ "wal_checkpoint",
119732 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT,
119733 /* ePragFlg: */ PragFlg_NeedSchema,
119734 /* ColNames: */ 46, 3,
119735 /* iArg: */ 0 },
119736#endif
119737#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
119738 {/* zName: */ "writable_schema",
119739 /* ePragTyp: */ PragTyp_FLAG,
119740 /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1,
119741 /* ColNames: */ 0, 0,
119742 /* iArg: */ SQLITE_WriteSchema },
119743#endif
119744};
119745/* Number of pragmas: 61 on by default, 78 total. */
119746
119747/************** End of pragma.h **********************************************/
119748/************** Continuing where we left off in pragma.c *********************/
119749
119750/*
119751** Interpret the given string as a safety level. Return 0 for OFF,
119752** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
119753** unrecognized string argument. The FULL and EXTRA option is disallowed
119754** if the omitFull parameter it 1.
119755**
119756** Note that the values returned are one less that the values that
119757** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
119758** to support legacy SQL code. The safety level used to be boolean
119759** and older scripts may have used numbers 0 for OFF and 1 for ON.
119760*/
119761static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
119762 /* 123456789 123456789 123 */
119763 static const char zText[] = "onoffalseyestruextrafull";
119764 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
119765 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
119766 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
119767 /* on no off false yes true extra full */
119768 int i, n;
119769 if( sqlite3Isdigit(*z) ){
119770 return (u8)sqlite3Atoi(z);
119771 }
119772 n = sqlite3Strlen30(z);
119773 for(i=0; i<ArraySize(iLength); i++){
119774 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0
119775 && (!omitFull || iValue[i]<=1)
119776 ){
119777 return iValue[i];
119778 }
119779 }
119780 return dflt;
119781}
119782
119783/*
119784** Interpret the given string as a boolean value.
119785*/
119786SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, u8 dflt){
119787 return getSafetyLevel(z,1,dflt)!=0;
119788}
119789
119790/* The sqlite3GetBoolean() function is used by other modules but the
119791** remainder of this file is specific to PRAGMA processing. So omit
119792** the rest of the file if PRAGMAs are omitted from the build.
119793*/
119794#if !defined(SQLITE_OMIT_PRAGMA)
119795
119796/*
119797** Interpret the given string as a locking mode value.
119798*/
119799static int getLockingMode(const char *z){
119800 if( z ){
119801 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
119802 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
119803 }
119804 return PAGER_LOCKINGMODE_QUERY;
119805}
119806
119807#ifndef SQLITE_OMIT_AUTOVACUUM
119808/*
119809** Interpret the given string as an auto-vacuum mode value.
119810**
119811** The following strings, "none", "full" and "incremental" are
119812** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
119813*/
119814static int getAutoVacuum(const char *z){
119815 int i;
119816 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
119817 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
119818 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
119819 i = sqlite3Atoi(z);
119820 return (u8)((i>=0&&i<=2)?i:0);
119821}
119822#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
119823
119824#ifndef SQLITE_OMIT_PAGER_PRAGMAS
119825/*
119826** Interpret the given string as a temp db location. Return 1 for file
119827** backed temporary databases, 2 for the Red-Black tree in memory database
119828** and 0 to use the compile-time default.
119829*/
119830static int getTempStore(const char *z){
119831 if( z[0]>='0' && z[0]<='2' ){
119832 return z[0] - '0';
119833 }else if( sqlite3StrICmp(z, "file")==0 ){
119834 return 1;
119835 }else if( sqlite3StrICmp(z, "memory")==0 ){
119836 return 2;
119837 }else{
119838 return 0;
119839 }
119840}
119841#endif /* SQLITE_PAGER_PRAGMAS */
119842
119843#ifndef SQLITE_OMIT_PAGER_PRAGMAS
119844/*
119845** Invalidate temp storage, either when the temp storage is changed
119846** from default, or when 'file' and the temp_store_directory has changed
119847*/
119848static int invalidateTempStorage(Parse *pParse){
119849 sqlite3 *db = pParse->db;
119850 if( db->aDb[1].pBt!=0 ){
119851 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
119852 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
119853 "from within a transaction");
119854 return SQLITE_ERROR;
119855 }
119856 sqlite3BtreeClose(db->aDb[1].pBt);
119857 db->aDb[1].pBt = 0;
119858 sqlite3ResetAllSchemasOfConnection(db);
119859 }
119860 return SQLITE_OK;
119861}
119862#endif /* SQLITE_PAGER_PRAGMAS */
119863
119864#ifndef SQLITE_OMIT_PAGER_PRAGMAS
119865/*
119866** If the TEMP database is open, close it and mark the database schema
119867** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
119868** or DEFAULT_TEMP_STORE pragmas.
119869*/
119870static int changeTempStorage(Parse *pParse, const char *zStorageType){
119871 int ts = getTempStore(zStorageType);
119872 sqlite3 *db = pParse->db;
119873 if( db->temp_store==ts ) return SQLITE_OK;
119874 if( invalidateTempStorage( pParse ) != SQLITE_OK ){
119875 return SQLITE_ERROR;
119876 }
119877 db->temp_store = (u8)ts;
119878 return SQLITE_OK;
119879}
119880#endif /* SQLITE_PAGER_PRAGMAS */
119881
119882/*
119883** Set result column names for a pragma.
119884*/
119885static void setPragmaResultColumnNames(
119886 Vdbe *v, /* The query under construction */
119887 const PragmaName *pPragma /* The pragma */
119888){
119889 u8 n = pPragma->nPragCName;
119890 sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
119891 if( n==0 ){
119892 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
119893 }else{
119894 int i, j;
119895 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
119896 sqlite3VdbeSetColName(v, i, COLNAME_NAME, pragCName[j], SQLITE_STATIC);
119897 }
119898 }
119899}
119900
119901/*
119902** Generate code to return a single integer value.
119903*/
119904static void returnSingleInt(Vdbe *v, i64 value){
119905 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
119906 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
119907}
119908
119909/*
119910** Generate code to return a single text value.
119911*/
119912static void returnSingleText(
119913 Vdbe *v, /* Prepared statement under construction */
119914 const char *zValue /* Value to be returned */
119915){
119916 if( zValue ){
119917 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
119918 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
119919 }
119920}
119921
119922
119923/*
119924** Set the safety_level and pager flags for pager iDb. Or if iDb<0
119925** set these values for all pagers.
119926*/
119927#ifndef SQLITE_OMIT_PAGER_PRAGMAS
119928static void setAllPagerFlags(sqlite3 *db){
119929 if( db->autoCommit ){
119930 Db *pDb = db->aDb;
119931 int n = db->nDb;
119932 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
119933 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
119934 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
119935 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
119936 == PAGER_FLAGS_MASK );
119937 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
119938 while( (n--) > 0 ){
119939 if( pDb->pBt ){
119940 sqlite3BtreeSetPagerFlags(pDb->pBt,
119941 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
119942 }
119943 pDb++;
119944 }
119945 }
119946}
119947#else
119948# define setAllPagerFlags(X) /* no-op */
119949#endif
119950
119951
119952/*
119953** Return a human-readable name for a constraint resolution action.
119954*/
119955#ifndef SQLITE_OMIT_FOREIGN_KEY
119956static const char *actionName(u8 action){
119957 const char *zName;
119958 switch( action ){
119959 case OE_SetNull: zName = "SET NULL"; break;
119960 case OE_SetDflt: zName = "SET DEFAULT"; break;
119961 case OE_Cascade: zName = "CASCADE"; break;
119962 case OE_Restrict: zName = "RESTRICT"; break;
119963 default: zName = "NO ACTION";
119964 assert( action==OE_None ); break;
119965 }
119966 return zName;
119967}
119968#endif
119969
119970
119971/*
119972** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
119973** defined in pager.h. This function returns the associated lowercase
119974** journal-mode name.
119975*/
119976SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
119977 static char * const azModeName[] = {
119978 "delete", "persist", "off", "truncate", "memory"
119979#ifndef SQLITE_OMIT_WAL
119980 , "wal"
119981#endif
119982 };
119983 assert( PAGER_JOURNALMODE_DELETE==0 );
119984 assert( PAGER_JOURNALMODE_PERSIST==1 );
119985 assert( PAGER_JOURNALMODE_OFF==2 );
119986 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
119987 assert( PAGER_JOURNALMODE_MEMORY==4 );
119988 assert( PAGER_JOURNALMODE_WAL==5 );
119989 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
119990
119991 if( eMode==ArraySize(azModeName) ) return 0;
119992 return azModeName[eMode];
119993}
119994
119995/*
119996** Locate a pragma in the aPragmaName[] array.
119997*/
119998static const PragmaName *pragmaLocate(const char *zName){
119999 int upr, lwr, mid = 0, rc;
120000 lwr = 0;
120001 upr = ArraySize(aPragmaName)-1;
120002 while( lwr<=upr ){
120003 mid = (lwr+upr)/2;
120004 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
120005 if( rc==0 ) break;
120006 if( rc<0 ){
120007 upr = mid - 1;
120008 }else{
120009 lwr = mid + 1;
120010 }
120011 }
120012 return lwr>upr ? 0 : &aPragmaName[mid];
120013}
120014
120015/*
120016** Helper subroutine for PRAGMA integrity_check:
120017**
120018** Generate code to output a single-column result row with a value of the
120019** string held in register 3. Decrement the result count in register 1
120020** and halt if the maximum number of result rows have been issued.
120021*/
120022static int integrityCheckResultRow(Vdbe *v){
120023 int addr;
120024 sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
120025 addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
120026 VdbeCoverage(v);
120027 sqlite3VdbeAddOp0(v, OP_Halt);
120028 return addr;
120029}
120030
120031/*
120032** Process a pragma statement.
120033**
120034** Pragmas are of this form:
120035**
120036** PRAGMA [schema.]id [= value]
120037**
120038** The identifier might also be a string. The value is a string, and
120039** identifier, or a number. If minusFlag is true, then the value is
120040** a number that was preceded by a minus sign.
120041**
120042** If the left side is "database.id" then pId1 is the database name
120043** and pId2 is the id. If the left side is just "id" then pId1 is the
120044** id and pId2 is any empty string.
120045*/
120046SQLITE_PRIVATE void sqlite3Pragma(
120047 Parse *pParse,
120048 Token *pId1, /* First part of [schema.]id field */
120049 Token *pId2, /* Second part of [schema.]id field, or NULL */
120050 Token *pValue, /* Token for <value>, or NULL */
120051 int minusFlag /* True if a '-' sign preceded <value> */
120052){
120053 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
120054 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
120055 const char *zDb = 0; /* The database name */
120056 Token *pId; /* Pointer to <id> token */
120057 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
120058 int iDb; /* Database index for <database> */
120059 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
120060 sqlite3 *db = pParse->db; /* The database connection */
120061 Db *pDb; /* The specific database being pragmaed */
120062 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
120063 const PragmaName *pPragma; /* The pragma */
120064
120065 if( v==0 ) return;
120066 sqlite3VdbeRunOnlyOnce(v);
120067 pParse->nMem = 2;
120068
120069 /* Interpret the [schema.] part of the pragma statement. iDb is the
120070 ** index of the database this pragma is being applied to in db.aDb[]. */
120071 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
120072 if( iDb<0 ) return;
120073 pDb = &db->aDb[iDb];
120074
120075 /* If the temp database has been explicitly named as part of the
120076 ** pragma, make sure it is open.
120077 */
120078 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
120079 return;
120080 }
120081
120082 zLeft = sqlite3NameFromToken(db, pId);
120083 if( !zLeft ) return;
120084 if( minusFlag ){
120085 zRight = sqlite3MPrintf(db, "-%T", pValue);
120086 }else{
120087 zRight = sqlite3NameFromToken(db, pValue);
120088 }
120089
120090 assert( pId2 );
120091 zDb = pId2->n>0 ? pDb->zDbSName : 0;
120092 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
120093 goto pragma_out;
120094 }
120095
120096 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
120097 ** connection. If it returns SQLITE_OK, then assume that the VFS
120098 ** handled the pragma and generate a no-op prepared statement.
120099 **
120100 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
120101 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
120102 ** object corresponding to the database file to which the pragma
120103 ** statement refers.
120104 **
120105 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
120106 ** file control is an array of pointers to strings (char**) in which the
120107 ** second element of the array is the name of the pragma and the third
120108 ** element is the argument to the pragma or NULL if the pragma has no
120109 ** argument.
120110 */
120111 aFcntl[0] = 0;
120112 aFcntl[1] = zLeft;
120113 aFcntl[2] = zRight;
120114 aFcntl[3] = 0;
120115 db->busyHandler.nBusy = 0;
120116 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
120117 if( rc==SQLITE_OK ){
120118 sqlite3VdbeSetNumCols(v, 1);
120119 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT);
120120 returnSingleText(v, aFcntl[0]);
120121 sqlite3_free(aFcntl[0]);
120122 goto pragma_out;
120123 }
120124 if( rc!=SQLITE_NOTFOUND ){
120125 if( aFcntl[0] ){
120126 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
120127 sqlite3_free(aFcntl[0]);
120128 }
120129 pParse->nErr++;
120130 pParse->rc = rc;
120131 goto pragma_out;
120132 }
120133
120134 /* Locate the pragma in the lookup table */
120135 pPragma = pragmaLocate(zLeft);
120136 if( pPragma==0 ) goto pragma_out;
120137
120138 /* Make sure the database schema is loaded if the pragma requires that */
120139 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
120140 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
120141 }
120142
120143 /* Register the result column names for pragmas that return results */
120144 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
120145 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
120146 ){
120147 setPragmaResultColumnNames(v, pPragma);
120148 }
120149
120150 /* Jump to the appropriate pragma handler */
120151 switch( pPragma->ePragTyp ){
120152
120153#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
120154 /*
120155 ** PRAGMA [schema.]default_cache_size
120156 ** PRAGMA [schema.]default_cache_size=N
120157 **
120158 ** The first form reports the current persistent setting for the
120159 ** page cache size. The value returned is the maximum number of
120160 ** pages in the page cache. The second form sets both the current
120161 ** page cache size value and the persistent page cache size value
120162 ** stored in the database file.
120163 **
120164 ** Older versions of SQLite would set the default cache size to a
120165 ** negative number to indicate synchronous=OFF. These days, synchronous
120166 ** is always on by default regardless of the sign of the default cache
120167 ** size. But continue to take the absolute value of the default cache
120168 ** size of historical compatibility.
120169 */
120170 case PragTyp_DEFAULT_CACHE_SIZE: {
120171 static const int iLn = VDBE_OFFSET_LINENO(2);
120172 static const VdbeOpList getCacheSize[] = {
120173 { OP_Transaction, 0, 0, 0}, /* 0 */
120174 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
120175 { OP_IfPos, 1, 8, 0},
120176 { OP_Integer, 0, 2, 0},
120177 { OP_Subtract, 1, 2, 1},
120178 { OP_IfPos, 1, 8, 0},
120179 { OP_Integer, 0, 1, 0}, /* 6 */
120180 { OP_Noop, 0, 0, 0},
120181 { OP_ResultRow, 1, 1, 0},
120182 };
120183 VdbeOp *aOp;
120184 sqlite3VdbeUsesBtree(v, iDb);
120185 if( !zRight ){
120186 pParse->nMem += 2;
120187 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
120188 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
120189 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
120190 aOp[0].p1 = iDb;
120191 aOp[1].p1 = iDb;
120192 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
120193 }else{
120194 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
120195 sqlite3BeginWriteOperation(pParse, 0, iDb);
120196 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
120197 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
120198 pDb->pSchema->cache_size = size;
120199 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
120200 }
120201 break;
120202 }
120203#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
120204
120205#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
120206 /*
120207 ** PRAGMA [schema.]page_size
120208 ** PRAGMA [schema.]page_size=N
120209 **
120210 ** The first form reports the current setting for the
120211 ** database page size in bytes. The second form sets the
120212 ** database page size value. The value can only be set if
120213 ** the database has not yet been created.
120214 */
120215 case PragTyp_PAGE_SIZE: {
120216 Btree *pBt = pDb->pBt;
120217 assert( pBt!=0 );
120218 if( !zRight ){
120219 int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
120220 returnSingleInt(v, size);
120221 }else{
120222 /* Malloc may fail when setting the page-size, as there is an internal
120223 ** buffer that the pager module resizes using sqlite3_realloc().
120224 */
120225 db->nextPagesize = sqlite3Atoi(zRight);
120226 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
120227 sqlite3OomFault(db);
120228 }
120229 }
120230 break;
120231 }
120232
120233 /*
120234 ** PRAGMA [schema.]secure_delete
120235 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
120236 **
120237 ** The first form reports the current setting for the
120238 ** secure_delete flag. The second form changes the secure_delete
120239 ** flag setting and reports the new value.
120240 */
120241 case PragTyp_SECURE_DELETE: {
120242 Btree *pBt = pDb->pBt;
120243 int b = -1;
120244 assert( pBt!=0 );
120245 if( zRight ){
120246 if( sqlite3_stricmp(zRight, "fast")==0 ){
120247 b = 2;
120248 }else{
120249 b = sqlite3GetBoolean(zRight, 0);
120250 }
120251 }
120252 if( pId2->n==0 && b>=0 ){
120253 int ii;
120254 for(ii=0; ii<db->nDb; ii++){
120255 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
120256 }
120257 }
120258 b = sqlite3BtreeSecureDelete(pBt, b);
120259 returnSingleInt(v, b);
120260 break;
120261 }
120262
120263 /*
120264 ** PRAGMA [schema.]max_page_count
120265 ** PRAGMA [schema.]max_page_count=N
120266 **
120267 ** The first form reports the current setting for the
120268 ** maximum number of pages in the database file. The
120269 ** second form attempts to change this setting. Both
120270 ** forms return the current setting.
120271 **
120272 ** The absolute value of N is used. This is undocumented and might
120273 ** change. The only purpose is to provide an easy way to test
120274 ** the sqlite3AbsInt32() function.
120275 **
120276 ** PRAGMA [schema.]page_count
120277 **
120278 ** Return the number of pages in the specified database.
120279 */
120280 case PragTyp_PAGE_COUNT: {
120281 int iReg;
120282 sqlite3CodeVerifySchema(pParse, iDb);
120283 iReg = ++pParse->nMem;
120284 if( sqlite3Tolower(zLeft[0])=='p' ){
120285 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
120286 }else{
120287 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
120288 sqlite3AbsInt32(sqlite3Atoi(zRight)));
120289 }
120290 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
120291 break;
120292 }
120293
120294 /*
120295 ** PRAGMA [schema.]locking_mode
120296 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
120297 */
120298 case PragTyp_LOCKING_MODE: {
120299 const char *zRet = "normal";
120300 int eMode = getLockingMode(zRight);
120301
120302 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
120303 /* Simple "PRAGMA locking_mode;" statement. This is a query for
120304 ** the current default locking mode (which may be different to
120305 ** the locking-mode of the main database).
120306 */
120307 eMode = db->dfltLockMode;
120308 }else{
120309 Pager *pPager;
120310 if( pId2->n==0 ){
120311 /* This indicates that no database name was specified as part
120312 ** of the PRAGMA command. In this case the locking-mode must be
120313 ** set on all attached databases, as well as the main db file.
120314 **
120315 ** Also, the sqlite3.dfltLockMode variable is set so that
120316 ** any subsequently attached databases also use the specified
120317 ** locking mode.
120318 */
120319 int ii;
120320 assert(pDb==&db->aDb[0]);
120321 for(ii=2; ii<db->nDb; ii++){
120322 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
120323 sqlite3PagerLockingMode(pPager, eMode);
120324 }
120325 db->dfltLockMode = (u8)eMode;
120326 }
120327 pPager = sqlite3BtreePager(pDb->pBt);
120328 eMode = sqlite3PagerLockingMode(pPager, eMode);
120329 }
120330
120331 assert( eMode==PAGER_LOCKINGMODE_NORMAL
120332 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
120333 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
120334 zRet = "exclusive";
120335 }
120336 returnSingleText(v, zRet);
120337 break;
120338 }
120339
120340 /*
120341 ** PRAGMA [schema.]journal_mode
120342 ** PRAGMA [schema.]journal_mode =
120343 ** (delete|persist|off|truncate|memory|wal|off)
120344 */
120345 case PragTyp_JOURNAL_MODE: {
120346 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
120347 int ii; /* Loop counter */
120348
120349 if( zRight==0 ){
120350 /* If there is no "=MODE" part of the pragma, do a query for the
120351 ** current mode */
120352 eMode = PAGER_JOURNALMODE_QUERY;
120353 }else{
120354 const char *zMode;
120355 int n = sqlite3Strlen30(zRight);
120356 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
120357 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
120358 }
120359 if( !zMode ){
120360 /* If the "=MODE" part does not match any known journal mode,
120361 ** then do a query */
120362 eMode = PAGER_JOURNALMODE_QUERY;
120363 }
120364 }
120365 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
120366 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
120367 iDb = 0;
120368 pId2->n = 1;
120369 }
120370 for(ii=db->nDb-1; ii>=0; ii--){
120371 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
120372 sqlite3VdbeUsesBtree(v, ii);
120373 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
120374 }
120375 }
120376 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
120377 break;
120378 }
120379
120380 /*
120381 ** PRAGMA [schema.]journal_size_limit
120382 ** PRAGMA [schema.]journal_size_limit=N
120383 **
120384 ** Get or set the size limit on rollback journal files.
120385 */
120386 case PragTyp_JOURNAL_SIZE_LIMIT: {
120387 Pager *pPager = sqlite3BtreePager(pDb->pBt);
120388 i64 iLimit = -2;
120389 if( zRight ){
120390 sqlite3DecOrHexToI64(zRight, &iLimit);
120391 if( iLimit<-1 ) iLimit = -1;
120392 }
120393 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
120394 returnSingleInt(v, iLimit);
120395 break;
120396 }
120397
120398#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
120399
120400 /*
120401 ** PRAGMA [schema.]auto_vacuum
120402 ** PRAGMA [schema.]auto_vacuum=N
120403 **
120404 ** Get or set the value of the database 'auto-vacuum' parameter.
120405 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
120406 */
120407#ifndef SQLITE_OMIT_AUTOVACUUM
120408 case PragTyp_AUTO_VACUUM: {
120409 Btree *pBt = pDb->pBt;
120410 assert( pBt!=0 );
120411 if( !zRight ){
120412 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
120413 }else{
120414 int eAuto = getAutoVacuum(zRight);
120415 assert( eAuto>=0 && eAuto<=2 );
120416 db->nextAutovac = (u8)eAuto;
120417 /* Call SetAutoVacuum() to set initialize the internal auto and
120418 ** incr-vacuum flags. This is required in case this connection
120419 ** creates the database file. It is important that it is created
120420 ** as an auto-vacuum capable db.
120421 */
120422 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
120423 if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
120424 /* When setting the auto_vacuum mode to either "full" or
120425 ** "incremental", write the value of meta[6] in the database
120426 ** file. Before writing to meta[6], check that meta[3] indicates
120427 ** that this really is an auto-vacuum capable database.
120428 */
120429 static const int iLn = VDBE_OFFSET_LINENO(2);
120430 static const VdbeOpList setMeta6[] = {
120431 { OP_Transaction, 0, 1, 0}, /* 0 */
120432 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
120433 { OP_If, 1, 0, 0}, /* 2 */
120434 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
120435 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
120436 };
120437 VdbeOp *aOp;
120438 int iAddr = sqlite3VdbeCurrentAddr(v);
120439 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
120440 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
120441 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
120442 aOp[0].p1 = iDb;
120443 aOp[1].p1 = iDb;
120444 aOp[2].p2 = iAddr+4;
120445 aOp[4].p1 = iDb;
120446 aOp[4].p3 = eAuto - 1;
120447 sqlite3VdbeUsesBtree(v, iDb);
120448 }
120449 }
120450 break;
120451 }
120452#endif
120453
120454 /*
120455 ** PRAGMA [schema.]incremental_vacuum(N)
120456 **
120457 ** Do N steps of incremental vacuuming on a database.
120458 */
120459#ifndef SQLITE_OMIT_AUTOVACUUM
120460 case PragTyp_INCREMENTAL_VACUUM: {
120461 int iLimit, addr;
120462 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
120463 iLimit = 0x7fffffff;
120464 }
120465 sqlite3BeginWriteOperation(pParse, 0, iDb);
120466 sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
120467 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
120468 sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
120469 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
120470 sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
120471 sqlite3VdbeJumpHere(v, addr);
120472 break;
120473 }
120474#endif
120475
120476#ifndef SQLITE_OMIT_PAGER_PRAGMAS
120477 /*
120478 ** PRAGMA [schema.]cache_size
120479 ** PRAGMA [schema.]cache_size=N
120480 **
120481 ** The first form reports the current local setting for the
120482 ** page cache size. The second form sets the local
120483 ** page cache size value. If N is positive then that is the
120484 ** number of pages in the cache. If N is negative, then the
120485 ** number of pages is adjusted so that the cache uses -N kibibytes
120486 ** of memory.
120487 */
120488 case PragTyp_CACHE_SIZE: {
120489 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
120490 if( !zRight ){
120491 returnSingleInt(v, pDb->pSchema->cache_size);
120492 }else{
120493 int size = sqlite3Atoi(zRight);
120494 pDb->pSchema->cache_size = size;
120495 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
120496 }
120497 break;
120498 }
120499
120500 /*
120501 ** PRAGMA [schema.]cache_spill
120502 ** PRAGMA cache_spill=BOOLEAN
120503 ** PRAGMA [schema.]cache_spill=N
120504 **
120505 ** The first form reports the current local setting for the
120506 ** page cache spill size. The second form turns cache spill on
120507 ** or off. When turnning cache spill on, the size is set to the
120508 ** current cache_size. The third form sets a spill size that
120509 ** may be different form the cache size.
120510 ** If N is positive then that is the
120511 ** number of pages in the cache. If N is negative, then the
120512 ** number of pages is adjusted so that the cache uses -N kibibytes
120513 ** of memory.
120514 **
120515 ** If the number of cache_spill pages is less then the number of
120516 ** cache_size pages, no spilling occurs until the page count exceeds
120517 ** the number of cache_size pages.
120518 **
120519 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
120520 ** not just the schema specified.
120521 */
120522 case PragTyp_CACHE_SPILL: {
120523 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
120524 if( !zRight ){
120525 returnSingleInt(v,
120526 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
120527 sqlite3BtreeSetSpillSize(pDb->pBt,0));
120528 }else{
120529 int size = 1;
120530 if( sqlite3GetInt32(zRight, &size) ){
120531 sqlite3BtreeSetSpillSize(pDb->pBt, size);
120532 }
120533 if( sqlite3GetBoolean(zRight, size!=0) ){
120534 db->flags |= SQLITE_CacheSpill;
120535 }else{
120536 db->flags &= ~SQLITE_CacheSpill;
120537 }
120538 setAllPagerFlags(db);
120539 }
120540 break;
120541 }
120542
120543 /*
120544 ** PRAGMA [schema.]mmap_size(N)
120545 **
120546 ** Used to set mapping size limit. The mapping size limit is
120547 ** used to limit the aggregate size of all memory mapped regions of the
120548 ** database file. If this parameter is set to zero, then memory mapping
120549 ** is not used at all. If N is negative, then the default memory map
120550 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
120551 ** The parameter N is measured in bytes.
120552 **
120553 ** This value is advisory. The underlying VFS is free to memory map
120554 ** as little or as much as it wants. Except, if N is set to 0 then the
120555 ** upper layers will never invoke the xFetch interfaces to the VFS.
120556 */
120557 case PragTyp_MMAP_SIZE: {
120558 sqlite3_int64 sz;
120559#if SQLITE_MAX_MMAP_SIZE>0
120560 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
120561 if( zRight ){
120562 int ii;
120563 sqlite3DecOrHexToI64(zRight, &sz);
120564 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
120565 if( pId2->n==0 ) db->szMmap = sz;
120566 for(ii=db->nDb-1; ii>=0; ii--){
120567 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
120568 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
120569 }
120570 }
120571 }
120572 sz = -1;
120573 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
120574#else
120575 sz = 0;
120576 rc = SQLITE_OK;
120577#endif
120578 if( rc==SQLITE_OK ){
120579 returnSingleInt(v, sz);
120580 }else if( rc!=SQLITE_NOTFOUND ){
120581 pParse->nErr++;
120582 pParse->rc = rc;
120583 }
120584 break;
120585 }
120586
120587 /*
120588 ** PRAGMA temp_store
120589 ** PRAGMA temp_store = "default"|"memory"|"file"
120590 **
120591 ** Return or set the local value of the temp_store flag. Changing
120592 ** the local value does not make changes to the disk file and the default
120593 ** value will be restored the next time the database is opened.
120594 **
120595 ** Note that it is possible for the library compile-time options to
120596 ** override this setting
120597 */
120598 case PragTyp_TEMP_STORE: {
120599 if( !zRight ){
120600 returnSingleInt(v, db->temp_store);
120601 }else{
120602 changeTempStorage(pParse, zRight);
120603 }
120604 break;
120605 }
120606
120607 /*
120608 ** PRAGMA temp_store_directory
120609 ** PRAGMA temp_store_directory = ""|"directory_name"
120610 **
120611 ** Return or set the local value of the temp_store_directory flag. Changing
120612 ** the value sets a specific directory to be used for temporary files.
120613 ** Setting to a null string reverts to the default temporary directory search.
120614 ** If temporary directory is changed, then invalidateTempStorage.
120615 **
120616 */
120617 case PragTyp_TEMP_STORE_DIRECTORY: {
120618 if( !zRight ){
120619 returnSingleText(v, sqlite3_temp_directory);
120620 }else{
120621#ifndef SQLITE_OMIT_WSD
120622 if( zRight[0] ){
120623 int res;
120624 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
120625 if( rc!=SQLITE_OK || res==0 ){
120626 sqlite3ErrorMsg(pParse, "not a writable directory");
120627 goto pragma_out;
120628 }
120629 }
120630 if( SQLITE_TEMP_STORE==0
120631 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
120632 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
120633 ){
120634 invalidateTempStorage(pParse);
120635 }
120636 sqlite3_free(sqlite3_temp_directory);
120637 if( zRight[0] ){
120638 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
120639 }else{
120640 sqlite3_temp_directory = 0;
120641 }
120642#endif /* SQLITE_OMIT_WSD */
120643 }
120644 break;
120645 }
120646
120647#if SQLITE_OS_WIN
120648 /*
120649 ** PRAGMA data_store_directory
120650 ** PRAGMA data_store_directory = ""|"directory_name"
120651 **
120652 ** Return or set the local value of the data_store_directory flag. Changing
120653 ** the value sets a specific directory to be used for database files that
120654 ** were specified with a relative pathname. Setting to a null string reverts
120655 ** to the default database directory, which for database files specified with
120656 ** a relative path will probably be based on the current directory for the
120657 ** process. Database file specified with an absolute path are not impacted
120658 ** by this setting, regardless of its value.
120659 **
120660 */
120661 case PragTyp_DATA_STORE_DIRECTORY: {
120662 if( !zRight ){
120663 returnSingleText(v, sqlite3_data_directory);
120664 }else{
120665#ifndef SQLITE_OMIT_WSD
120666 if( zRight[0] ){
120667 int res;
120668 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
120669 if( rc!=SQLITE_OK || res==0 ){
120670 sqlite3ErrorMsg(pParse, "not a writable directory");
120671 goto pragma_out;
120672 }
120673 }
120674 sqlite3_free(sqlite3_data_directory);
120675 if( zRight[0] ){
120676 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
120677 }else{
120678 sqlite3_data_directory = 0;
120679 }
120680#endif /* SQLITE_OMIT_WSD */
120681 }
120682 break;
120683 }
120684#endif
120685
120686#if SQLITE_ENABLE_LOCKING_STYLE
120687 /*
120688 ** PRAGMA [schema.]lock_proxy_file
120689 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
120690 **
120691 ** Return or set the value of the lock_proxy_file flag. Changing
120692 ** the value sets a specific file to be used for database access locks.
120693 **
120694 */
120695 case PragTyp_LOCK_PROXY_FILE: {
120696 if( !zRight ){
120697 Pager *pPager = sqlite3BtreePager(pDb->pBt);
120698 char *proxy_file_path = NULL;
120699 sqlite3_file *pFile = sqlite3PagerFile(pPager);
120700 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
120701 &proxy_file_path);
120702 returnSingleText(v, proxy_file_path);
120703 }else{
120704 Pager *pPager = sqlite3BtreePager(pDb->pBt);
120705 sqlite3_file *pFile = sqlite3PagerFile(pPager);
120706 int res;
120707 if( zRight[0] ){
120708 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
120709 zRight);
120710 } else {
120711 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
120712 NULL);
120713 }
120714 if( res!=SQLITE_OK ){
120715 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
120716 goto pragma_out;
120717 }
120718 }
120719 break;
120720 }
120721#endif /* SQLITE_ENABLE_LOCKING_STYLE */
120722
120723 /*
120724 ** PRAGMA [schema.]synchronous
120725 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
120726 **
120727 ** Return or set the local value of the synchronous flag. Changing
120728 ** the local value does not make changes to the disk file and the
120729 ** default value will be restored the next time the database is
120730 ** opened.
120731 */
120732 case PragTyp_SYNCHRONOUS: {
120733 if( !zRight ){
120734 returnSingleInt(v, pDb->safety_level-1);
120735 }else{
120736 if( !db->autoCommit ){
120737 sqlite3ErrorMsg(pParse,
120738 "Safety level may not be changed inside a transaction");
120739 }else if( iDb!=1 ){
120740 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
120741 if( iLevel==0 ) iLevel = 1;
120742 pDb->safety_level = iLevel;
120743 pDb->bSyncSet = 1;
120744 setAllPagerFlags(db);
120745 }
120746 }
120747 break;
120748 }
120749#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
120750
120751#ifndef SQLITE_OMIT_FLAG_PRAGMAS
120752 case PragTyp_FLAG: {
120753 if( zRight==0 ){
120754 setPragmaResultColumnNames(v, pPragma);
120755 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
120756 }else{
120757 int mask = pPragma->iArg; /* Mask of bits to set or clear. */
120758 if( db->autoCommit==0 ){
120759 /* Foreign key support may not be enabled or disabled while not
120760 ** in auto-commit mode. */
120761 mask &= ~(SQLITE_ForeignKeys);
120762 }
120763#if SQLITE_USER_AUTHENTICATION
120764 if( db->auth.authLevel==UAUTH_User ){
120765 /* Do not allow non-admin users to modify the schema arbitrarily */
120766 mask &= ~(SQLITE_WriteSchema);
120767 }
120768#endif
120769
120770 if( sqlite3GetBoolean(zRight, 0) ){
120771 db->flags |= mask;
120772 }else{
120773 db->flags &= ~mask;
120774 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
120775 }
120776
120777 /* Many of the flag-pragmas modify the code generated by the SQL
120778 ** compiler (eg. count_changes). So add an opcode to expire all
120779 ** compiled SQL statements after modifying a pragma value.
120780 */
120781 sqlite3VdbeAddOp0(v, OP_Expire);
120782 setAllPagerFlags(db);
120783 }
120784 break;
120785 }
120786#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
120787
120788#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
120789 /*
120790 ** PRAGMA table_info(<table>)
120791 **
120792 ** Return a single row for each column of the named table. The columns of
120793 ** the returned data set are:
120794 **
120795 ** cid: Column id (numbered from left to right, starting at 0)
120796 ** name: Column name
120797 ** type: Column declaration type.
120798 ** notnull: True if 'NOT NULL' is part of column declaration
120799 ** dflt_value: The default value for the column, if any.
120800 ** pk: Non-zero for PK fields.
120801 */
120802 case PragTyp_TABLE_INFO: if( zRight ){
120803 Table *pTab;
120804 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
120805 if( pTab ){
120806 int i, k;
120807 int nHidden = 0;
120808 Column *pCol;
120809 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
120810 pParse->nMem = 6;
120811 sqlite3CodeVerifySchema(pParse, iDb);
120812 sqlite3ViewGetColumnNames(pParse, pTab);
120813 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
120814 if( IsHiddenColumn(pCol) ){
120815 nHidden++;
120816 continue;
120817 }
120818 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
120819 k = 0;
120820 }else if( pPk==0 ){
120821 k = 1;
120822 }else{
120823 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
120824 }
120825 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
120826 sqlite3VdbeMultiLoad(v, 1, "issisi",
120827 i-nHidden,
120828 pCol->zName,
120829 sqlite3ColumnType(pCol,""),
120830 pCol->notNull ? 1 : 0,
120831 pCol->pDflt ? pCol->pDflt->u.zToken : 0,
120832 k);
120833 }
120834 }
120835 }
120836 break;
120837
120838#ifdef SQLITE_DEBUG
120839 case PragTyp_STATS: {
120840 Index *pIdx;
120841 HashElem *i;
120842 pParse->nMem = 5;
120843 sqlite3CodeVerifySchema(pParse, iDb);
120844 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
120845 Table *pTab = sqliteHashData(i);
120846 sqlite3VdbeMultiLoad(v, 1, "ssiii",
120847 pTab->zName,
120848 0,
120849 pTab->szTabRow,
120850 pTab->nRowLogEst,
120851 pTab->tabFlags);
120852 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
120853 sqlite3VdbeMultiLoad(v, 2, "siiiX",
120854 pIdx->zName,
120855 pIdx->szIdxRow,
120856 pIdx->aiRowLogEst[0],
120857 pIdx->hasStat1);
120858 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
120859 }
120860 }
120861 }
120862 break;
120863#endif
120864
120865 case PragTyp_INDEX_INFO: if( zRight ){
120866 Index *pIdx;
120867 Table *pTab;
120868 pIdx = sqlite3FindIndex(db, zRight, zDb);
120869 if( pIdx ){
120870 int i;
120871 int mx;
120872 if( pPragma->iArg ){
120873 /* PRAGMA index_xinfo (newer version with more rows and columns) */
120874 mx = pIdx->nColumn;
120875 pParse->nMem = 6;
120876 }else{
120877 /* PRAGMA index_info (legacy version) */
120878 mx = pIdx->nKeyCol;
120879 pParse->nMem = 3;
120880 }
120881 pTab = pIdx->pTable;
120882 sqlite3CodeVerifySchema(pParse, iDb);
120883 assert( pParse->nMem<=pPragma->nPragCName );
120884 for(i=0; i<mx; i++){
120885 i16 cnum = pIdx->aiColumn[i];
120886 sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
120887 cnum<0 ? 0 : pTab->aCol[cnum].zName);
120888 if( pPragma->iArg ){
120889 sqlite3VdbeMultiLoad(v, 4, "isiX",
120890 pIdx->aSortOrder[i],
120891 pIdx->azColl[i],
120892 i<pIdx->nKeyCol);
120893 }
120894 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
120895 }
120896 }
120897 }
120898 break;
120899
120900 case PragTyp_INDEX_LIST: if( zRight ){
120901 Index *pIdx;
120902 Table *pTab;
120903 int i;
120904 pTab = sqlite3FindTable(db, zRight, zDb);
120905 if( pTab ){
120906 pParse->nMem = 5;
120907 sqlite3CodeVerifySchema(pParse, iDb);
120908 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
120909 const char *azOrigin[] = { "c", "u", "pk" };
120910 sqlite3VdbeMultiLoad(v, 1, "isisi",
120911 i,
120912 pIdx->zName,
120913 IsUniqueIndex(pIdx),
120914 azOrigin[pIdx->idxType],
120915 pIdx->pPartIdxWhere!=0);
120916 }
120917 }
120918 }
120919 break;
120920
120921 case PragTyp_DATABASE_LIST: {
120922 int i;
120923 pParse->nMem = 3;
120924 for(i=0; i<db->nDb; i++){
120925 if( db->aDb[i].pBt==0 ) continue;
120926 assert( db->aDb[i].zDbSName!=0 );
120927 sqlite3VdbeMultiLoad(v, 1, "iss",
120928 i,
120929 db->aDb[i].zDbSName,
120930 sqlite3BtreeGetFilename(db->aDb[i].pBt));
120931 }
120932 }
120933 break;
120934
120935 case PragTyp_COLLATION_LIST: {
120936 int i = 0;
120937 HashElem *p;
120938 pParse->nMem = 2;
120939 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
120940 CollSeq *pColl = (CollSeq *)sqliteHashData(p);
120941 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
120942 }
120943 }
120944 break;
120945
120946#ifdef SQLITE_INTROSPECTION_PRAGMAS
120947 case PragTyp_FUNCTION_LIST: {
120948 int i;
120949 HashElem *j;
120950 FuncDef *p;
120951 pParse->nMem = 2;
120952 for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
120953 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
120954 sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 1);
120955 }
120956 }
120957 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
120958 p = (FuncDef*)sqliteHashData(j);
120959 sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 0);
120960 }
120961 }
120962 break;
120963
120964#ifndef SQLITE_OMIT_VIRTUALTABLE
120965 case PragTyp_MODULE_LIST: {
120966 HashElem *j;
120967 pParse->nMem = 1;
120968 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
120969 Module *pMod = (Module*)sqliteHashData(j);
120970 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
120971 }
120972 }
120973 break;
120974#endif /* SQLITE_OMIT_VIRTUALTABLE */
120975
120976 case PragTyp_PRAGMA_LIST: {
120977 int i;
120978 for(i=0; i<ArraySize(aPragmaName); i++){
120979 sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
120980 }
120981 }
120982 break;
120983#endif /* SQLITE_INTROSPECTION_PRAGMAS */
120984
120985#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
120986
120987#ifndef SQLITE_OMIT_FOREIGN_KEY
120988 case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
120989 FKey *pFK;
120990 Table *pTab;
120991 pTab = sqlite3FindTable(db, zRight, zDb);
120992 if( pTab ){
120993 pFK = pTab->pFKey;
120994 if( pFK ){
120995 int i = 0;
120996 pParse->nMem = 8;
120997 sqlite3CodeVerifySchema(pParse, iDb);
120998 while(pFK){
120999 int j;
121000 for(j=0; j<pFK->nCol; j++){
121001 sqlite3VdbeMultiLoad(v, 1, "iissssss",
121002 i,
121003 j,
121004 pFK->zTo,
121005 pTab->aCol[pFK->aCol[j].iFrom].zName,
121006 pFK->aCol[j].zCol,
121007 actionName(pFK->aAction[1]), /* ON UPDATE */
121008 actionName(pFK->aAction[0]), /* ON DELETE */
121009 "NONE");
121010 }
121011 ++i;
121012 pFK = pFK->pNextFrom;
121013 }
121014 }
121015 }
121016 }
121017 break;
121018#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
121019
121020#ifndef SQLITE_OMIT_FOREIGN_KEY
121021#ifndef SQLITE_OMIT_TRIGGER
121022 case PragTyp_FOREIGN_KEY_CHECK: {
121023 FKey *pFK; /* A foreign key constraint */
121024 Table *pTab; /* Child table contain "REFERENCES" keyword */
121025 Table *pParent; /* Parent table that child points to */
121026 Index *pIdx; /* Index in the parent table */
121027 int i; /* Loop counter: Foreign key number for pTab */
121028 int j; /* Loop counter: Field of the foreign key */
121029 HashElem *k; /* Loop counter: Next table in schema */
121030 int x; /* result variable */
121031 int regResult; /* 3 registers to hold a result row */
121032 int regKey; /* Register to hold key for checking the FK */
121033 int regRow; /* Registers to hold a row from pTab */
121034 int addrTop; /* Top of a loop checking foreign keys */
121035 int addrOk; /* Jump here if the key is OK */
121036 int *aiCols; /* child to parent column mapping */
121037
121038 regResult = pParse->nMem+1;
121039 pParse->nMem += 4;
121040 regKey = ++pParse->nMem;
121041 regRow = ++pParse->nMem;
121042 sqlite3CodeVerifySchema(pParse, iDb);
121043 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
121044 while( k ){
121045 if( zRight ){
121046 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
121047 k = 0;
121048 }else{
121049 pTab = (Table*)sqliteHashData(k);
121050 k = sqliteHashNext(k);
121051 }
121052 if( pTab==0 || pTab->pFKey==0 ) continue;
121053 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
121054 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
121055 sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
121056 sqlite3VdbeLoadString(v, regResult, pTab->zName);
121057 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
121058 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
121059 if( pParent==0 ) continue;
121060 pIdx = 0;
121061 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
121062 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
121063 if( x==0 ){
121064 if( pIdx==0 ){
121065 sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
121066 }else{
121067 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
121068 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
121069 }
121070 }else{
121071 k = 0;
121072 break;
121073 }
121074 }
121075 assert( pParse->nErr>0 || pFK==0 );
121076 if( pFK ) break;
121077 if( pParse->nTab<i ) pParse->nTab = i;
121078 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
121079 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
121080 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
121081 pIdx = 0;
121082 aiCols = 0;
121083 if( pParent ){
121084 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
121085 assert( x==0 );
121086 }
121087 addrOk = sqlite3VdbeMakeLabel(v);
121088
121089 /* Generate code to read the child key values into registers
121090 ** regRow..regRow+n. If any of the child key values are NULL, this
121091 ** row cannot cause an FK violation. Jump directly to addrOk in
121092 ** this case. */
121093 for(j=0; j<pFK->nCol; j++){
121094 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
121095 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
121096 sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
121097 }
121098
121099 /* Generate code to query the parent index for a matching parent
121100 ** key. If a match is found, jump to addrOk. */
121101 if( pIdx ){
121102 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
121103 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
121104 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
121105 VdbeCoverage(v);
121106 }else if( pParent ){
121107 int jmp = sqlite3VdbeCurrentAddr(v)+2;
121108 sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v);
121109 sqlite3VdbeGoto(v, addrOk);
121110 assert( pFK->nCol==1 );
121111 }
121112
121113 /* Generate code to report an FK violation to the caller. */
121114 if( HasRowid(pTab) ){
121115 sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
121116 }else{
121117 sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1);
121118 }
121119 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
121120 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
121121 sqlite3VdbeResolveLabel(v, addrOk);
121122 sqlite3DbFree(db, aiCols);
121123 }
121124 sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
121125 sqlite3VdbeJumpHere(v, addrTop);
121126 }
121127 }
121128 break;
121129#endif /* !defined(SQLITE_OMIT_TRIGGER) */
121130#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
121131
121132#ifndef NDEBUG
121133 case PragTyp_PARSER_TRACE: {
121134 if( zRight ){
121135 if( sqlite3GetBoolean(zRight, 0) ){
121136 sqlite3ParserTrace(stdout, "parser: ");
121137 }else{
121138 sqlite3ParserTrace(0, 0);
121139 }
121140 }
121141 }
121142 break;
121143#endif
121144
121145 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
121146 ** used will be case sensitive or not depending on the RHS.
121147 */
121148 case PragTyp_CASE_SENSITIVE_LIKE: {
121149 if( zRight ){
121150 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
121151 }
121152 }
121153 break;
121154
121155#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
121156# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
121157#endif
121158
121159#ifndef SQLITE_OMIT_INTEGRITY_CHECK
121160 /* PRAGMA integrity_check
121161 ** PRAGMA integrity_check(N)
121162 ** PRAGMA quick_check
121163 ** PRAGMA quick_check(N)
121164 **
121165 ** Verify the integrity of the database.
121166 **
121167 ** The "quick_check" is reduced version of
121168 ** integrity_check designed to detect most database corruption
121169 ** without the overhead of cross-checking indexes. Quick_check
121170 ** is linear time wherease integrity_check is O(NlogN).
121171 */
121172 case PragTyp_INTEGRITY_CHECK: {
121173 int i, j, addr, mxErr;
121174
121175 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
121176
121177 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
121178 ** then iDb is set to the index of the database identified by <db>.
121179 ** In this case, the integrity of database iDb only is verified by
121180 ** the VDBE created below.
121181 **
121182 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
121183 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
121184 ** to -1 here, to indicate that the VDBE should verify the integrity
121185 ** of all attached databases. */
121186 assert( iDb>=0 );
121187 assert( iDb==0 || pId2->z );
121188 if( pId2->z==0 ) iDb = -1;
121189
121190 /* Initialize the VDBE program */
121191 pParse->nMem = 6;
121192
121193 /* Set the maximum error count */
121194 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
121195 if( zRight ){
121196 sqlite3GetInt32(zRight, &mxErr);
121197 if( mxErr<=0 ){
121198 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
121199 }
121200 }
121201 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
121202
121203 /* Do an integrity check on each database file */
121204 for(i=0; i<db->nDb; i++){
121205 HashElem *x; /* For looping over tables in the schema */
121206 Hash *pTbls; /* Set of all tables in the schema */
121207 int *aRoot; /* Array of root page numbers of all btrees */
121208 int cnt = 0; /* Number of entries in aRoot[] */
121209 int mxIdx = 0; /* Maximum number of indexes for any table */
121210
121211 if( OMIT_TEMPDB && i==1 ) continue;
121212 if( iDb>=0 && i!=iDb ) continue;
121213
121214 sqlite3CodeVerifySchema(pParse, i);
121215
121216 /* Do an integrity check of the B-Tree
121217 **
121218 ** Begin by finding the root pages numbers
121219 ** for all tables and indices in the database.
121220 */
121221 assert( sqlite3SchemaMutexHeld(db, i, 0) );
121222 pTbls = &db->aDb[i].pSchema->tblHash;
121223 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
121224 Table *pTab = sqliteHashData(x); /* Current table */
121225 Index *pIdx; /* An index on pTab */
121226 int nIdx; /* Number of indexes on pTab */
121227 if( HasRowid(pTab) ) cnt++;
121228 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
121229 if( nIdx>mxIdx ) mxIdx = nIdx;
121230 }
121231 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
121232 if( aRoot==0 ) break;
121233 for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
121234 Table *pTab = sqliteHashData(x);
121235 Index *pIdx;
121236 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
121237 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121238 aRoot[++cnt] = pIdx->tnum;
121239 }
121240 }
121241 aRoot[0] = cnt;
121242
121243 /* Make sure sufficient number of registers have been allocated */
121244 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
121245 sqlite3ClearTempRegCache(pParse);
121246
121247 /* Do the b-tree integrity checks */
121248 sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
121249 sqlite3VdbeChangeP5(v, (u8)i);
121250 addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
121251 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
121252 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
121253 P4_DYNAMIC);
121254 sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
121255 integrityCheckResultRow(v);
121256 sqlite3VdbeJumpHere(v, addr);
121257
121258 /* Make sure all the indices are constructed correctly.
121259 */
121260 for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
121261 Table *pTab = sqliteHashData(x);
121262 Index *pIdx, *pPk;
121263 Index *pPrior = 0;
121264 int loopTop;
121265 int iDataCur, iIdxCur;
121266 int r1 = -1;
121267
121268 if( pTab->tnum<1 ) continue; /* Skip VIEWs or VIRTUAL TABLEs */
121269 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
121270 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
121271 1, 0, &iDataCur, &iIdxCur);
121272 /* reg[7] counts the number of entries in the table.
121273 ** reg[8+i] counts the number of entries in the i-th index
121274 */
121275 sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
121276 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
121277 sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
121278 }
121279 assert( pParse->nMem>=8+j );
121280 assert( sqlite3NoTempsInRange(pParse,1,7+j) );
121281 sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
121282 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
121283 if( !isQuick ){
121284 /* Sanity check on record header decoding */
121285 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nCol-1, 3);
121286 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
121287 }
121288 /* Verify that all NOT NULL columns really are NOT NULL */
121289 for(j=0; j<pTab->nCol; j++){
121290 char *zErr;
121291 int jmp2;
121292 if( j==pTab->iPKey ) continue;
121293 if( pTab->aCol[j].notNull==0 ) continue;
121294 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
121295 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
121296 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
121297 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
121298 pTab->aCol[j].zName);
121299 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
121300 integrityCheckResultRow(v);
121301 sqlite3VdbeJumpHere(v, jmp2);
121302 }
121303 /* Verify CHECK constraints */
121304 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
121305 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
121306 if( db->mallocFailed==0 ){
121307 int addrCkFault = sqlite3VdbeMakeLabel(v);
121308 int addrCkOk = sqlite3VdbeMakeLabel(v);
121309 char *zErr;
121310 int k;
121311 pParse->iSelfTab = iDataCur + 1;
121312 for(k=pCheck->nExpr-1; k>0; k--){
121313 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
121314 }
121315 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
121316 SQLITE_JUMPIFNULL);
121317 sqlite3VdbeResolveLabel(v, addrCkFault);
121318 pParse->iSelfTab = 0;
121319 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
121320 pTab->zName);
121321 sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
121322 integrityCheckResultRow(v);
121323 sqlite3VdbeResolveLabel(v, addrCkOk);
121324 }
121325 sqlite3ExprListDelete(db, pCheck);
121326 }
121327 if( !isQuick ){ /* Omit the remaining tests for quick_check */
121328 /* Validate index entries for the current row */
121329 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
121330 int jmp2, jmp3, jmp4, jmp5;
121331 int ckUniq = sqlite3VdbeMakeLabel(v);
121332 if( pPk==pIdx ) continue;
121333 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
121334 pPrior, r1);
121335 pPrior = pIdx;
121336 sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
121337 /* Verify that an index entry exists for the current table row */
121338 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
121339 pIdx->nColumn); VdbeCoverage(v);
121340 sqlite3VdbeLoadString(v, 3, "row ");
121341 sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
121342 sqlite3VdbeLoadString(v, 4, " missing from index ");
121343 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
121344 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
121345 sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
121346 jmp4 = integrityCheckResultRow(v);
121347 sqlite3VdbeJumpHere(v, jmp2);
121348 /* For UNIQUE indexes, verify that only one entry exists with the
121349 ** current key. The entry is unique if (1) any column is NULL
121350 ** or (2) the next entry has a different key */
121351 if( IsUniqueIndex(pIdx) ){
121352 int uniqOk = sqlite3VdbeMakeLabel(v);
121353 int jmp6;
121354 int kk;
121355 for(kk=0; kk<pIdx->nKeyCol; kk++){
121356 int iCol = pIdx->aiColumn[kk];
121357 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
121358 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
121359 sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
121360 VdbeCoverage(v);
121361 }
121362 jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
121363 sqlite3VdbeGoto(v, uniqOk);
121364 sqlite3VdbeJumpHere(v, jmp6);
121365 sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
121366 pIdx->nKeyCol); VdbeCoverage(v);
121367 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
121368 sqlite3VdbeGoto(v, jmp5);
121369 sqlite3VdbeResolveLabel(v, uniqOk);
121370 }
121371 sqlite3VdbeJumpHere(v, jmp4);
121372 sqlite3ResolvePartIdxLabel(pParse, jmp3);
121373 }
121374 }
121375 sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
121376 sqlite3VdbeJumpHere(v, loopTop-1);
121377#ifndef SQLITE_OMIT_BTREECOUNT
121378 if( !isQuick ){
121379 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
121380 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
121381 if( pPk==pIdx ) continue;
121382 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
121383 addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
121384 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
121385 sqlite3VdbeLoadString(v, 4, pIdx->zName);
121386 sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
121387 integrityCheckResultRow(v);
121388 sqlite3VdbeJumpHere(v, addr);
121389 }
121390 }
121391#endif /* SQLITE_OMIT_BTREECOUNT */
121392 }
121393 }
121394 {
121395 static const int iLn = VDBE_OFFSET_LINENO(2);
121396 static const VdbeOpList endCode[] = {
121397 { OP_AddImm, 1, 0, 0}, /* 0 */
121398 { OP_IfNotZero, 1, 4, 0}, /* 1 */
121399 { OP_String8, 0, 3, 0}, /* 2 */
121400 { OP_ResultRow, 3, 1, 0}, /* 3 */
121401 { OP_Halt, 0, 0, 0}, /* 4 */
121402 { OP_String8, 0, 3, 0}, /* 5 */
121403 { OP_Goto, 0, 3, 0}, /* 6 */
121404 };
121405 VdbeOp *aOp;
121406
121407 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
121408 if( aOp ){
121409 aOp[0].p2 = 1-mxErr;
121410 aOp[2].p4type = P4_STATIC;
121411 aOp[2].p4.z = "ok";
121412 aOp[5].p4type = P4_STATIC;
121413 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
121414 }
121415 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
121416 }
121417 }
121418 break;
121419#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
121420
121421#ifndef SQLITE_OMIT_UTF16
121422 /*
121423 ** PRAGMA encoding
121424 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
121425 **
121426 ** In its first form, this pragma returns the encoding of the main
121427 ** database. If the database is not initialized, it is initialized now.
121428 **
121429 ** The second form of this pragma is a no-op if the main database file
121430 ** has not already been initialized. In this case it sets the default
121431 ** encoding that will be used for the main database file if a new file
121432 ** is created. If an existing main database file is opened, then the
121433 ** default text encoding for the existing database is used.
121434 **
121435 ** In all cases new databases created using the ATTACH command are
121436 ** created to use the same default text encoding as the main database. If
121437 ** the main database has not been initialized and/or created when ATTACH
121438 ** is executed, this is done before the ATTACH operation.
121439 **
121440 ** In the second form this pragma sets the text encoding to be used in
121441 ** new database files created using this database handle. It is only
121442 ** useful if invoked immediately after the main database i
121443 */
121444 case PragTyp_ENCODING: {
121445 static const struct EncName {
121446 char *zName;
121447 u8 enc;
121448 } encnames[] = {
121449 { "UTF8", SQLITE_UTF8 },
121450 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
121451 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
121452 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
121453 { "UTF16le", SQLITE_UTF16LE },
121454 { "UTF16be", SQLITE_UTF16BE },
121455 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
121456 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
121457 { 0, 0 }
121458 };
121459 const struct EncName *pEnc;
121460 if( !zRight ){ /* "PRAGMA encoding" */
121461 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
121462 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
121463 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
121464 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
121465 returnSingleText(v, encnames[ENC(pParse->db)].zName);
121466 }else{ /* "PRAGMA encoding = XXX" */
121467 /* Only change the value of sqlite.enc if the database handle is not
121468 ** initialized. If the main database exists, the new sqlite.enc value
121469 ** will be overwritten when the schema is next loaded. If it does not
121470 ** already exists, it will be created to use the new encoding value.
121471 */
121472 if(
121473 !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
121474 DbHasProperty(db, 0, DB_Empty)
121475 ){
121476 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
121477 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
121478 SCHEMA_ENC(db) = ENC(db) =
121479 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
121480 break;
121481 }
121482 }
121483 if( !pEnc->zName ){
121484 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
121485 }
121486 }
121487 }
121488 }
121489 break;
121490#endif /* SQLITE_OMIT_UTF16 */
121491
121492#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
121493 /*
121494 ** PRAGMA [schema.]schema_version
121495 ** PRAGMA [schema.]schema_version = <integer>
121496 **
121497 ** PRAGMA [schema.]user_version
121498 ** PRAGMA [schema.]user_version = <integer>
121499 **
121500 ** PRAGMA [schema.]freelist_count
121501 **
121502 ** PRAGMA [schema.]data_version
121503 **
121504 ** PRAGMA [schema.]application_id
121505 ** PRAGMA [schema.]application_id = <integer>
121506 **
121507 ** The pragma's schema_version and user_version are used to set or get
121508 ** the value of the schema-version and user-version, respectively. Both
121509 ** the schema-version and the user-version are 32-bit signed integers
121510 ** stored in the database header.
121511 **
121512 ** The schema-cookie is usually only manipulated internally by SQLite. It
121513 ** is incremented by SQLite whenever the database schema is modified (by
121514 ** creating or dropping a table or index). The schema version is used by
121515 ** SQLite each time a query is executed to ensure that the internal cache
121516 ** of the schema used when compiling the SQL query matches the schema of
121517 ** the database against which the compiled query is actually executed.
121518 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
121519 ** the schema-version is potentially dangerous and may lead to program
121520 ** crashes or database corruption. Use with caution!
121521 **
121522 ** The user-version is not used internally by SQLite. It may be used by
121523 ** applications for any purpose.
121524 */
121525 case PragTyp_HEADER_VALUE: {
121526 int iCookie = pPragma->iArg; /* Which cookie to read or write */
121527 sqlite3VdbeUsesBtree(v, iDb);
121528 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
121529 /* Write the specified cookie value */
121530 static const VdbeOpList setCookie[] = {
121531 { OP_Transaction, 0, 1, 0}, /* 0 */
121532 { OP_SetCookie, 0, 0, 0}, /* 1 */
121533 };
121534 VdbeOp *aOp;
121535 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
121536 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
121537 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
121538 aOp[0].p1 = iDb;
121539 aOp[1].p1 = iDb;
121540 aOp[1].p2 = iCookie;
121541 aOp[1].p3 = sqlite3Atoi(zRight);
121542 }else{
121543 /* Read the specified cookie value */
121544 static const VdbeOpList readCookie[] = {
121545 { OP_Transaction, 0, 0, 0}, /* 0 */
121546 { OP_ReadCookie, 0, 1, 0}, /* 1 */
121547 { OP_ResultRow, 1, 1, 0}
121548 };
121549 VdbeOp *aOp;
121550 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
121551 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
121552 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
121553 aOp[0].p1 = iDb;
121554 aOp[1].p1 = iDb;
121555 aOp[1].p3 = iCookie;
121556 sqlite3VdbeReusable(v);
121557 }
121558 }
121559 break;
121560#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
121561
121562#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
121563 /*
121564 ** PRAGMA compile_options
121565 **
121566 ** Return the names of all compile-time options used in this build,
121567 ** one option per row.
121568 */
121569 case PragTyp_COMPILE_OPTIONS: {
121570 int i = 0;
121571 const char *zOpt;
121572 pParse->nMem = 1;
121573 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
121574 sqlite3VdbeLoadString(v, 1, zOpt);
121575 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
121576 }
121577 sqlite3VdbeReusable(v);
121578 }
121579 break;
121580#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
121581
121582#ifndef SQLITE_OMIT_WAL
121583 /*
121584 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
121585 **
121586 ** Checkpoint the database.
121587 */
121588 case PragTyp_WAL_CHECKPOINT: {
121589 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
121590 int eMode = SQLITE_CHECKPOINT_PASSIVE;
121591 if( zRight ){
121592 if( sqlite3StrICmp(zRight, "full")==0 ){
121593 eMode = SQLITE_CHECKPOINT_FULL;
121594 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
121595 eMode = SQLITE_CHECKPOINT_RESTART;
121596 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
121597 eMode = SQLITE_CHECKPOINT_TRUNCATE;
121598 }
121599 }
121600 pParse->nMem = 3;
121601 sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
121602 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
121603 }
121604 break;
121605
121606 /*
121607 ** PRAGMA wal_autocheckpoint
121608 ** PRAGMA wal_autocheckpoint = N
121609 **
121610 ** Configure a database connection to automatically checkpoint a database
121611 ** after accumulating N frames in the log. Or query for the current value
121612 ** of N.
121613 */
121614 case PragTyp_WAL_AUTOCHECKPOINT: {
121615 if( zRight ){
121616 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
121617 }
121618 returnSingleInt(v,
121619 db->xWalCallback==sqlite3WalDefaultHook ?
121620 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
121621 }
121622 break;
121623#endif
121624
121625 /*
121626 ** PRAGMA shrink_memory
121627 **
121628 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
121629 ** connection on which it is invoked to free up as much memory as it
121630 ** can, by calling sqlite3_db_release_memory().
121631 */
121632 case PragTyp_SHRINK_MEMORY: {
121633 sqlite3_db_release_memory(db);
121634 break;
121635 }
121636
121637 /*
121638 ** PRAGMA optimize
121639 ** PRAGMA optimize(MASK)
121640 ** PRAGMA schema.optimize
121641 ** PRAGMA schema.optimize(MASK)
121642 **
121643 ** Attempt to optimize the database. All schemas are optimized in the first
121644 ** two forms, and only the specified schema is optimized in the latter two.
121645 **
121646 ** The details of optimizations performed by this pragma are expected
121647 ** to change and improve over time. Applications should anticipate that
121648 ** this pragma will perform new optimizations in future releases.
121649 **
121650 ** The optional argument is a bitmask of optimizations to perform:
121651 **
121652 ** 0x0001 Debugging mode. Do not actually perform any optimizations
121653 ** but instead return one line of text for each optimization
121654 ** that would have been done. Off by default.
121655 **
121656 ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
121657 ** See below for additional information.
121658 **
121659 ** 0x0004 (Not yet implemented) Record usage and performance
121660 ** information from the current session in the
121661 ** database file so that it will be available to "optimize"
121662 ** pragmas run by future database connections.
121663 **
121664 ** 0x0008 (Not yet implemented) Create indexes that might have
121665 ** been helpful to recent queries
121666 **
121667 ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all
121668 ** of the optimizations listed above except Debug Mode, including new
121669 ** optimizations that have not yet been invented. If new optimizations are
121670 ** ever added that should be off by default, those off-by-default
121671 ** optimizations will have bitmasks of 0x10000 or larger.
121672 **
121673 ** DETERMINATION OF WHEN TO RUN ANALYZE
121674 **
121675 ** In the current implementation, a table is analyzed if only if all of
121676 ** the following are true:
121677 **
121678 ** (1) MASK bit 0x02 is set.
121679 **
121680 ** (2) The query planner used sqlite_stat1-style statistics for one or
121681 ** more indexes of the table at some point during the lifetime of
121682 ** the current connection.
121683 **
121684 ** (3) One or more indexes of the table are currently unanalyzed OR
121685 ** the number of rows in the table has increased by 25 times or more
121686 ** since the last time ANALYZE was run.
121687 **
121688 ** The rules for when tables are analyzed are likely to change in
121689 ** future releases.
121690 */
121691 case PragTyp_OPTIMIZE: {
121692 int iDbLast; /* Loop termination point for the schema loop */
121693 int iTabCur; /* Cursor for a table whose size needs checking */
121694 HashElem *k; /* Loop over tables of a schema */
121695 Schema *pSchema; /* The current schema */
121696 Table *pTab; /* A table in the schema */
121697 Index *pIdx; /* An index of the table */
121698 LogEst szThreshold; /* Size threshold above which reanalysis is needd */
121699 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
121700 u32 opMask; /* Mask of operations to perform */
121701
121702 if( zRight ){
121703 opMask = (u32)sqlite3Atoi(zRight);
121704 if( (opMask & 0x02)==0 ) break;
121705 }else{
121706 opMask = 0xfffe;
121707 }
121708 iTabCur = pParse->nTab++;
121709 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
121710 if( iDb==1 ) continue;
121711 sqlite3CodeVerifySchema(pParse, iDb);
121712 pSchema = db->aDb[iDb].pSchema;
121713 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
121714 pTab = (Table*)sqliteHashData(k);
121715
121716 /* If table pTab has not been used in a way that would benefit from
121717 ** having analysis statistics during the current session, then skip it.
121718 ** This also has the effect of skipping virtual tables and views */
121719 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
121720
121721 /* Reanalyze if the table is 25 times larger than the last analysis */
121722 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
121723 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121724 if( !pIdx->hasStat1 ){
121725 szThreshold = 0; /* Always analyze if any index lacks statistics */
121726 break;
121727 }
121728 }
121729 if( szThreshold ){
121730 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
121731 sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur,
121732 sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
121733 VdbeCoverage(v);
121734 }
121735 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
121736 db->aDb[iDb].zDbSName, pTab->zName);
121737 if( opMask & 0x01 ){
121738 int r1 = sqlite3GetTempReg(pParse);
121739 sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
121740 sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
121741 }else{
121742 sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
121743 }
121744 }
121745 }
121746 sqlite3VdbeAddOp0(v, OP_Expire);
121747 break;
121748 }
121749
121750 /*
121751 ** PRAGMA busy_timeout
121752 ** PRAGMA busy_timeout = N
121753 **
121754 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
121755 ** if one is set. If no busy handler or a different busy handler is set
121756 ** then 0 is returned. Setting the busy_timeout to 0 or negative
121757 ** disables the timeout.
121758 */
121759 /*case PragTyp_BUSY_TIMEOUT*/ default: {
121760 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
121761 if( zRight ){
121762 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
121763 }
121764 returnSingleInt(v, db->busyTimeout);
121765 break;
121766 }
121767
121768 /*
121769 ** PRAGMA soft_heap_limit
121770 ** PRAGMA soft_heap_limit = N
121771 **
121772 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
121773 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
121774 ** specified and is a non-negative integer.
121775 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
121776 ** returns the same integer that would be returned by the
121777 ** sqlite3_soft_heap_limit64(-1) C-language function.
121778 */
121779 case PragTyp_SOFT_HEAP_LIMIT: {
121780 sqlite3_int64 N;
121781 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
121782 sqlite3_soft_heap_limit64(N);
121783 }
121784 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
121785 break;
121786 }
121787
121788 /*
121789 ** PRAGMA threads
121790 ** PRAGMA threads = N
121791 **
121792 ** Configure the maximum number of worker threads. Return the new
121793 ** maximum, which might be less than requested.
121794 */
121795 case PragTyp_THREADS: {
121796 sqlite3_int64 N;
121797 if( zRight
121798 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK
121799 && N>=0
121800 ){
121801 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
121802 }
121803 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
121804 break;
121805 }
121806
121807#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
121808 /*
121809 ** Report the current state of file logs for all databases
121810 */
121811 case PragTyp_LOCK_STATUS: {
121812 static const char *const azLockName[] = {
121813 "unlocked", "shared", "reserved", "pending", "exclusive"
121814 };
121815 int i;
121816 pParse->nMem = 2;
121817 for(i=0; i<db->nDb; i++){
121818 Btree *pBt;
121819 const char *zState = "unknown";
121820 int j;
121821 if( db->aDb[i].zDbSName==0 ) continue;
121822 pBt = db->aDb[i].pBt;
121823 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
121824 zState = "closed";
121825 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
121826 SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
121827 zState = azLockName[j];
121828 }
121829 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
121830 }
121831 break;
121832 }
121833#endif
121834
121835#ifdef SQLITE_HAS_CODEC
121836 case PragTyp_KEY: {
121837 if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
121838 break;
121839 }
121840 case PragTyp_REKEY: {
121841 if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
121842 break;
121843 }
121844 case PragTyp_HEXKEY: {
121845 if( zRight ){
121846 u8 iByte;
121847 int i;
121848 char zKey[40];
121849 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
121850 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
121851 if( (i&1)!=0 ) zKey[i/2] = iByte;
121852 }
121853 if( (zLeft[3] & 0xf)==0xb ){
121854 sqlite3_key_v2(db, zDb, zKey, i/2);
121855 }else{
121856 sqlite3_rekey_v2(db, zDb, zKey, i/2);
121857 }
121858 }
121859 break;
121860 }
121861#endif
121862#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
121863 case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
121864#ifdef SQLITE_HAS_CODEC
121865 if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
121866 sqlite3_activate_see(&zRight[4]);
121867 }
121868#endif
121869#ifdef SQLITE_ENABLE_CEROD
121870 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
121871 sqlite3_activate_cerod(&zRight[6]);
121872 }
121873#endif
121874 }
121875 break;
121876#endif
121877
121878 } /* End of the PRAGMA switch */
121879
121880 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
121881 ** purpose is to execute assert() statements to verify that if the
121882 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
121883 ** to the PRAGMA, the implementation has not added any OP_ResultRow
121884 ** instructions to the VM. */
121885 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
121886 sqlite3VdbeVerifyNoResultRow(v);
121887 }
121888
121889pragma_out:
121890 sqlite3DbFree(db, zLeft);
121891 sqlite3DbFree(db, zRight);
121892}
121893#ifndef SQLITE_OMIT_VIRTUALTABLE
121894/*****************************************************************************
121895** Implementation of an eponymous virtual table that runs a pragma.
121896**
121897*/
121898typedef struct PragmaVtab PragmaVtab;
121899typedef struct PragmaVtabCursor PragmaVtabCursor;
121900struct PragmaVtab {
121901 sqlite3_vtab base; /* Base class. Must be first */
121902 sqlite3 *db; /* The database connection to which it belongs */
121903 const PragmaName *pName; /* Name of the pragma */
121904 u8 nHidden; /* Number of hidden columns */
121905 u8 iHidden; /* Index of the first hidden column */
121906};
121907struct PragmaVtabCursor {
121908 sqlite3_vtab_cursor base; /* Base class. Must be first */
121909 sqlite3_stmt *pPragma; /* The pragma statement to run */
121910 sqlite_int64 iRowid; /* Current rowid */
121911 char *azArg[2]; /* Value of the argument and schema */
121912};
121913
121914/*
121915** Pragma virtual table module xConnect method.
121916*/
121917static int pragmaVtabConnect(
121918 sqlite3 *db,
121919 void *pAux,
121920 int argc, const char *const*argv,
121921 sqlite3_vtab **ppVtab,
121922 char **pzErr
121923){
121924 const PragmaName *pPragma = (const PragmaName*)pAux;
121925 PragmaVtab *pTab = 0;
121926 int rc;
121927 int i, j;
121928 char cSep = '(';
121929 StrAccum acc;
121930 char zBuf[200];
121931
121932 UNUSED_PARAMETER(argc);
121933 UNUSED_PARAMETER(argv);
121934 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
121935 sqlite3_str_appendall(&acc, "CREATE TABLE x");
121936 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
121937 sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
121938 cSep = ',';
121939 }
121940 if( i==0 ){
121941 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
121942 i++;
121943 }
121944 j = 0;
121945 if( pPragma->mPragFlg & PragFlg_Result1 ){
121946 sqlite3_str_appendall(&acc, ",arg HIDDEN");
121947 j++;
121948 }
121949 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
121950 sqlite3_str_appendall(&acc, ",schema HIDDEN");
121951 j++;
121952 }
121953 sqlite3_str_append(&acc, ")", 1);
121954 sqlite3StrAccumFinish(&acc);
121955 assert( strlen(zBuf) < sizeof(zBuf)-1 );
121956 rc = sqlite3_declare_vtab(db, zBuf);
121957 if( rc==SQLITE_OK ){
121958 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
121959 if( pTab==0 ){
121960 rc = SQLITE_NOMEM;
121961 }else{
121962 memset(pTab, 0, sizeof(PragmaVtab));
121963 pTab->pName = pPragma;
121964 pTab->db = db;
121965 pTab->iHidden = i;
121966 pTab->nHidden = j;
121967 }
121968 }else{
121969 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
121970 }
121971
121972 *ppVtab = (sqlite3_vtab*)pTab;
121973 return rc;
121974}
121975
121976/*
121977** Pragma virtual table module xDisconnect method.
121978*/
121979static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
121980 PragmaVtab *pTab = (PragmaVtab*)pVtab;
121981 sqlite3_free(pTab);
121982 return SQLITE_OK;
121983}
121984
121985/* Figure out the best index to use to search a pragma virtual table.
121986**
121987** There are not really any index choices. But we want to encourage the
121988** query planner to give == constraints on as many hidden parameters as
121989** possible, and especially on the first hidden parameter. So return a
121990** high cost if hidden parameters are unconstrained.
121991*/
121992static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
121993 PragmaVtab *pTab = (PragmaVtab*)tab;
121994 const struct sqlite3_index_constraint *pConstraint;
121995 int i, j;
121996 int seen[2];
121997
121998 pIdxInfo->estimatedCost = (double)1;
121999 if( pTab->nHidden==0 ){ return SQLITE_OK; }
122000 pConstraint = pIdxInfo->aConstraint;
122001 seen[0] = 0;
122002 seen[1] = 0;
122003 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
122004 if( pConstraint->usable==0 ) continue;
122005 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
122006 if( pConstraint->iColumn < pTab->iHidden ) continue;
122007 j = pConstraint->iColumn - pTab->iHidden;
122008 assert( j < 2 );
122009 seen[j] = i+1;
122010 }
122011 if( seen[0]==0 ){
122012 pIdxInfo->estimatedCost = (double)2147483647;
122013 pIdxInfo->estimatedRows = 2147483647;
122014 return SQLITE_OK;
122015 }
122016 j = seen[0]-1;
122017 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
122018 pIdxInfo->aConstraintUsage[j].omit = 1;
122019 if( seen[1]==0 ) return SQLITE_OK;
122020 pIdxInfo->estimatedCost = (double)20;
122021 pIdxInfo->estimatedRows = 20;
122022 j = seen[1]-1;
122023 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
122024 pIdxInfo->aConstraintUsage[j].omit = 1;
122025 return SQLITE_OK;
122026}
122027
122028/* Create a new cursor for the pragma virtual table */
122029static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
122030 PragmaVtabCursor *pCsr;
122031 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
122032 if( pCsr==0 ) return SQLITE_NOMEM;
122033 memset(pCsr, 0, sizeof(PragmaVtabCursor));
122034 pCsr->base.pVtab = pVtab;
122035 *ppCursor = &pCsr->base;
122036 return SQLITE_OK;
122037}
122038
122039/* Clear all content from pragma virtual table cursor. */
122040static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
122041 int i;
122042 sqlite3_finalize(pCsr->pPragma);
122043 pCsr->pPragma = 0;
122044 for(i=0; i<ArraySize(pCsr->azArg); i++){
122045 sqlite3_free(pCsr->azArg[i]);
122046 pCsr->azArg[i] = 0;
122047 }
122048}
122049
122050/* Close a pragma virtual table cursor */
122051static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
122052 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
122053 pragmaVtabCursorClear(pCsr);
122054 sqlite3_free(pCsr);
122055 return SQLITE_OK;
122056}
122057
122058/* Advance the pragma virtual table cursor to the next row */
122059static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
122060 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
122061 int rc = SQLITE_OK;
122062
122063 /* Increment the xRowid value */
122064 pCsr->iRowid++;
122065 assert( pCsr->pPragma );
122066 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
122067 rc = sqlite3_finalize(pCsr->pPragma);
122068 pCsr->pPragma = 0;
122069 pragmaVtabCursorClear(pCsr);
122070 }
122071 return rc;
122072}
122073
122074/*
122075** Pragma virtual table module xFilter method.
122076*/
122077static int pragmaVtabFilter(
122078 sqlite3_vtab_cursor *pVtabCursor,
122079 int idxNum, const char *idxStr,
122080 int argc, sqlite3_value **argv
122081){
122082 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
122083 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
122084 int rc;
122085 int i, j;
122086 StrAccum acc;
122087 char *zSql;
122088
122089 UNUSED_PARAMETER(idxNum);
122090 UNUSED_PARAMETER(idxStr);
122091 pragmaVtabCursorClear(pCsr);
122092 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
122093 for(i=0; i<argc; i++, j++){
122094 const char *zText = (const char*)sqlite3_value_text(argv[i]);
122095 assert( j<ArraySize(pCsr->azArg) );
122096 assert( pCsr->azArg[j]==0 );
122097 if( zText ){
122098 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
122099 if( pCsr->azArg[j]==0 ){
122100 return SQLITE_NOMEM;
122101 }
122102 }
122103 }
122104 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
122105 sqlite3_str_appendall(&acc, "PRAGMA ");
122106 if( pCsr->azArg[1] ){
122107 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
122108 }
122109 sqlite3_str_appendall(&acc, pTab->pName->zName);
122110 if( pCsr->azArg[0] ){
122111 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
122112 }
122113 zSql = sqlite3StrAccumFinish(&acc);
122114 if( zSql==0 ) return SQLITE_NOMEM;
122115 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
122116 sqlite3_free(zSql);
122117 if( rc!=SQLITE_OK ){
122118 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
122119 return rc;
122120 }
122121 return pragmaVtabNext(pVtabCursor);
122122}
122123
122124/*
122125** Pragma virtual table module xEof method.
122126*/
122127static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
122128 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
122129 return (pCsr->pPragma==0);
122130}
122131
122132/* The xColumn method simply returns the corresponding column from
122133** the PRAGMA.
122134*/
122135static int pragmaVtabColumn(
122136 sqlite3_vtab_cursor *pVtabCursor,
122137 sqlite3_context *ctx,
122138 int i
122139){
122140 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
122141 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
122142 if( i<pTab->iHidden ){
122143 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
122144 }else{
122145 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
122146 }
122147 return SQLITE_OK;
122148}
122149
122150/*
122151** Pragma virtual table module xRowid method.
122152*/
122153static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
122154 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
122155 *p = pCsr->iRowid;
122156 return SQLITE_OK;
122157}
122158
122159/* The pragma virtual table object */
122160static const sqlite3_module pragmaVtabModule = {
122161 0, /* iVersion */
122162 0, /* xCreate - create a table */
122163 pragmaVtabConnect, /* xConnect - connect to an existing table */
122164 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
122165 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
122166 0, /* xDestroy - Drop a table */
122167 pragmaVtabOpen, /* xOpen - open a cursor */
122168 pragmaVtabClose, /* xClose - close a cursor */
122169 pragmaVtabFilter, /* xFilter - configure scan constraints */
122170 pragmaVtabNext, /* xNext - advance a cursor */
122171 pragmaVtabEof, /* xEof */
122172 pragmaVtabColumn, /* xColumn - read data */
122173 pragmaVtabRowid, /* xRowid - read data */
122174 0, /* xUpdate - write data */
122175 0, /* xBegin - begin transaction */
122176 0, /* xSync - sync transaction */
122177 0, /* xCommit - commit transaction */
122178 0, /* xRollback - rollback transaction */
122179 0, /* xFindFunction - function overloading */
122180 0, /* xRename - rename the table */
122181 0, /* xSavepoint */
122182 0, /* xRelease */
122183 0 /* xRollbackTo */
122184};
122185
122186/*
122187** Check to see if zTabName is really the name of a pragma. If it is,
122188** then register an eponymous virtual table for that pragma and return
122189** a pointer to the Module object for the new virtual table.
122190*/
122191SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
122192 const PragmaName *pName;
122193 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
122194 pName = pragmaLocate(zName+7);
122195 if( pName==0 ) return 0;
122196 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
122197 assert( sqlite3HashFind(&db->aModule, zName)==0 );
122198 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
122199}
122200
122201#endif /* SQLITE_OMIT_VIRTUALTABLE */
122202
122203#endif /* SQLITE_OMIT_PRAGMA */
122204
122205/************** End of pragma.c **********************************************/
122206/************** Begin file prepare.c *****************************************/
122207/*
122208** 2005 May 25
122209**
122210** The author disclaims copyright to this source code. In place of
122211** a legal notice, here is a blessing:
122212**
122213** May you do good and not evil.
122214** May you find forgiveness for yourself and forgive others.
122215** May you share freely, never taking more than you give.
122216**
122217*************************************************************************
122218** This file contains the implementation of the sqlite3_prepare()
122219** interface, and routines that contribute to loading the database schema
122220** from disk.
122221*/
122222/* #include "sqliteInt.h" */
122223
122224/*
122225** Fill the InitData structure with an error message that indicates
122226** that the database is corrupt.
122227*/
122228static void corruptSchema(
122229 InitData *pData, /* Initialization context */
122230 const char *zObj, /* Object being parsed at the point of error */
122231 const char *zExtra /* Error information */
122232){
122233 sqlite3 *db = pData->db;
122234 if( db->mallocFailed ){
122235 pData->rc = SQLITE_NOMEM_BKPT;
122236 }else if( pData->pzErrMsg[0]!=0 ){
122237 /* A error message has already been generated. Do not overwrite it */
122238 }else if( pData->mInitFlags & INITFLAG_AlterTable ){
122239 *pData->pzErrMsg = sqlite3DbStrDup(db, zExtra);
122240 pData->rc = SQLITE_ERROR;
122241 }else if( db->flags & SQLITE_WriteSchema ){
122242 pData->rc = SQLITE_CORRUPT_BKPT;
122243 }else{
122244 char *z;
122245 if( zObj==0 ) zObj = "?";
122246 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
122247 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
122248 *pData->pzErrMsg = z;
122249 pData->rc = SQLITE_CORRUPT_BKPT;
122250 }
122251}
122252
122253/*
122254** This is the callback routine for the code that initializes the
122255** database. See sqlite3Init() below for additional information.
122256** This routine is also called from the OP_ParseSchema opcode of the VDBE.
122257**
122258** Each callback contains the following information:
122259**
122260** argv[0] = name of thing being created
122261** argv[1] = root page number for table or index. 0 for trigger or view.
122262** argv[2] = SQL text for the CREATE statement.
122263**
122264*/
122265SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
122266 InitData *pData = (InitData*)pInit;
122267 sqlite3 *db = pData->db;
122268 int iDb = pData->iDb;
122269
122270 assert( argc==3 );
122271 UNUSED_PARAMETER2(NotUsed, argc);
122272 assert( sqlite3_mutex_held(db->mutex) );
122273 DbClearProperty(db, iDb, DB_Empty);
122274 if( db->mallocFailed ){
122275 corruptSchema(pData, argv[0], 0);
122276 return 1;
122277 }
122278
122279 assert( iDb>=0 && iDb<db->nDb );
122280 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
122281 if( argv[1]==0 ){
122282 corruptSchema(pData, argv[0], 0);
122283 }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){
122284 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
122285 ** But because db->init.busy is set to 1, no VDBE code is generated
122286 ** or executed. All the parser does is build the internal data
122287 ** structures that describe the table, index, or view.
122288 */
122289 int rc;
122290 u8 saved_iDb = db->init.iDb;
122291 sqlite3_stmt *pStmt;
122292 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
122293
122294 assert( db->init.busy );
122295 db->init.iDb = iDb;
122296 db->init.newTnum = sqlite3Atoi(argv[1]);
122297 db->init.orphanTrigger = 0;
122298 TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
122299 rc = db->errCode;
122300 assert( (rc&0xFF)==(rcp&0xFF) );
122301 db->init.iDb = saved_iDb;
122302 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
122303 if( SQLITE_OK!=rc ){
122304 if( db->init.orphanTrigger ){
122305 assert( iDb==1 );
122306 }else{
122307 pData->rc = rc;
122308 if( rc==SQLITE_NOMEM ){
122309 sqlite3OomFault(db);
122310 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
122311 corruptSchema(pData, argv[0], sqlite3_errmsg(db));
122312 }
122313 }
122314 }
122315 sqlite3_finalize(pStmt);
122316 }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){
122317 corruptSchema(pData, argv[0], 0);
122318 }else{
122319 /* If the SQL column is blank it means this is an index that
122320 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
122321 ** constraint for a CREATE TABLE. The index should have already
122322 ** been created when we processed the CREATE TABLE. All we have
122323 ** to do here is record the root page number for that index.
122324 */
122325 Index *pIndex;
122326 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName);
122327 if( pIndex==0 ){
122328 /* This can occur if there exists an index on a TEMP table which
122329 ** has the same name as another index on a permanent index. Since
122330 ** the permanent table is hidden by the TEMP table, we can also
122331 ** safely ignore the index on the permanent table.
122332 */
122333 /* Do Nothing */;
122334 }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
122335 corruptSchema(pData, argv[0], "invalid rootpage");
122336 }
122337 }
122338 return 0;
122339}
122340
122341/*
122342** Attempt to read the database schema and initialize internal
122343** data structures for a single database file. The index of the
122344** database file is given by iDb. iDb==0 is used for the main
122345** database. iDb==1 should never be used. iDb>=2 is used for
122346** auxiliary databases. Return one of the SQLITE_ error codes to
122347** indicate success or failure.
122348*/
122349SQLITE_PRIVATE int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
122350 int rc;
122351 int i;
122352#ifndef SQLITE_OMIT_DEPRECATED
122353 int size;
122354#endif
122355 Db *pDb;
122356 char const *azArg[4];
122357 int meta[5];
122358 InitData initData;
122359 const char *zMasterName;
122360 int openedTransaction = 0;
122361
122362 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
122363 assert( iDb>=0 && iDb<db->nDb );
122364 assert( db->aDb[iDb].pSchema );
122365 assert( sqlite3_mutex_held(db->mutex) );
122366 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
122367
122368 db->init.busy = 1;
122369
122370 /* Construct the in-memory representation schema tables (sqlite_master or
122371 ** sqlite_temp_master) by invoking the parser directly. The appropriate
122372 ** table name will be inserted automatically by the parser so we can just
122373 ** use the abbreviation "x" here. The parser will also automatically tag
122374 ** the schema table as read-only. */
122375 azArg[0] = zMasterName = SCHEMA_TABLE(iDb);
122376 azArg[1] = "1";
122377 azArg[2] = "CREATE TABLE x(type text,name text,tbl_name text,"
122378 "rootpage int,sql text)";
122379 azArg[3] = 0;
122380 initData.db = db;
122381 initData.iDb = iDb;
122382 initData.rc = SQLITE_OK;
122383 initData.pzErrMsg = pzErrMsg;
122384 initData.mInitFlags = mFlags;
122385 sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
122386 if( initData.rc ){
122387 rc = initData.rc;
122388 goto error_out;
122389 }
122390
122391 /* Create a cursor to hold the database open
122392 */
122393 pDb = &db->aDb[iDb];
122394 if( pDb->pBt==0 ){
122395 assert( iDb==1 );
122396 DbSetProperty(db, 1, DB_SchemaLoaded);
122397 rc = SQLITE_OK;
122398 goto error_out;
122399 }
122400
122401 /* If there is not already a read-only (or read-write) transaction opened
122402 ** on the b-tree database, open one now. If a transaction is opened, it
122403 ** will be closed before this function returns. */
122404 sqlite3BtreeEnter(pDb->pBt);
122405 if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
122406 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
122407 if( rc!=SQLITE_OK ){
122408 sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
122409 goto initone_error_out;
122410 }
122411 openedTransaction = 1;
122412 }
122413
122414 /* Get the database meta information.
122415 **
122416 ** Meta values are as follows:
122417 ** meta[0] Schema cookie. Changes with each schema change.
122418 ** meta[1] File format of schema layer.
122419 ** meta[2] Size of the page cache.
122420 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
122421 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
122422 ** meta[5] User version
122423 ** meta[6] Incremental vacuum mode
122424 ** meta[7] unused
122425 ** meta[8] unused
122426 ** meta[9] unused
122427 **
122428 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
122429 ** the possible values of meta[4].
122430 */
122431 for(i=0; i<ArraySize(meta); i++){
122432 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
122433 }
122434 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
122435 memset(meta, 0, sizeof(meta));
122436 }
122437 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
122438
122439 /* If opening a non-empty database, check the text encoding. For the
122440 ** main database, set sqlite3.enc to the encoding of the main database.
122441 ** For an attached db, it is an error if the encoding is not the same
122442 ** as sqlite3.enc.
122443 */
122444 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
122445 if( iDb==0 ){
122446#ifndef SQLITE_OMIT_UTF16
122447 u8 encoding;
122448 /* If opening the main database, set ENC(db). */
122449 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
122450 if( encoding==0 ) encoding = SQLITE_UTF8;
122451 ENC(db) = encoding;
122452#else
122453 ENC(db) = SQLITE_UTF8;
122454#endif
122455 }else{
122456 /* If opening an attached database, the encoding much match ENC(db) */
122457 if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
122458 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
122459 " text encoding as main database");
122460 rc = SQLITE_ERROR;
122461 goto initone_error_out;
122462 }
122463 }
122464 }else{
122465 DbSetProperty(db, iDb, DB_Empty);
122466 }
122467 pDb->pSchema->enc = ENC(db);
122468
122469 if( pDb->pSchema->cache_size==0 ){
122470#ifndef SQLITE_OMIT_DEPRECATED
122471 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
122472 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
122473 pDb->pSchema->cache_size = size;
122474#else
122475 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
122476#endif
122477 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
122478 }
122479
122480 /*
122481 ** file_format==1 Version 3.0.0.
122482 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
122483 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
122484 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
122485 */
122486 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
122487 if( pDb->pSchema->file_format==0 ){
122488 pDb->pSchema->file_format = 1;
122489 }
122490 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
122491 sqlite3SetString(pzErrMsg, db, "unsupported file format");
122492 rc = SQLITE_ERROR;
122493 goto initone_error_out;
122494 }
122495
122496 /* Ticket #2804: When we open a database in the newer file format,
122497 ** clear the legacy_file_format pragma flag so that a VACUUM will
122498 ** not downgrade the database and thus invalidate any descending
122499 ** indices that the user might have created.
122500 */
122501 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
122502 db->flags &= ~SQLITE_LegacyFileFmt;
122503 }
122504
122505 /* Read the schema information out of the schema tables
122506 */
122507 assert( db->init.busy );
122508 {
122509 char *zSql;
122510 zSql = sqlite3MPrintf(db,
122511 "SELECT name, rootpage, sql FROM \"%w\".%s ORDER BY rowid",
122512 db->aDb[iDb].zDbSName, zMasterName);
122513#ifndef SQLITE_OMIT_AUTHORIZATION
122514 {
122515 sqlite3_xauth xAuth;
122516 xAuth = db->xAuth;
122517 db->xAuth = 0;
122518#endif
122519 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
122520#ifndef SQLITE_OMIT_AUTHORIZATION
122521 db->xAuth = xAuth;
122522 }
122523#endif
122524 if( rc==SQLITE_OK ) rc = initData.rc;
122525 sqlite3DbFree(db, zSql);
122526#ifndef SQLITE_OMIT_ANALYZE
122527 if( rc==SQLITE_OK ){
122528 sqlite3AnalysisLoad(db, iDb);
122529 }
122530#endif
122531 }
122532 if( db->mallocFailed ){
122533 rc = SQLITE_NOMEM_BKPT;
122534 sqlite3ResetAllSchemasOfConnection(db);
122535 }
122536 if( rc==SQLITE_OK || (db->flags&SQLITE_WriteSchema)){
122537 /* Black magic: If the SQLITE_WriteSchema flag is set, then consider
122538 ** the schema loaded, even if errors occurred. In this situation the
122539 ** current sqlite3_prepare() operation will fail, but the following one
122540 ** will attempt to compile the supplied statement against whatever subset
122541 ** of the schema was loaded before the error occurred. The primary
122542 ** purpose of this is to allow access to the sqlite_master table
122543 ** even when its contents have been corrupted.
122544 */
122545 DbSetProperty(db, iDb, DB_SchemaLoaded);
122546 rc = SQLITE_OK;
122547 }
122548
122549 /* Jump here for an error that occurs after successfully allocating
122550 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
122551 ** before that point, jump to error_out.
122552 */
122553initone_error_out:
122554 if( openedTransaction ){
122555 sqlite3BtreeCommit(pDb->pBt);
122556 }
122557 sqlite3BtreeLeave(pDb->pBt);
122558
122559error_out:
122560 if( rc ){
122561 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
122562 sqlite3OomFault(db);
122563 }
122564 sqlite3ResetOneSchema(db, iDb);
122565 }
122566 db->init.busy = 0;
122567 return rc;
122568}
122569
122570/*
122571** Initialize all database files - the main database file, the file
122572** used to store temporary tables, and any additional database files
122573** created using ATTACH statements. Return a success code. If an
122574** error occurs, write an error message into *pzErrMsg.
122575**
122576** After a database is initialized, the DB_SchemaLoaded bit is set
122577** bit is set in the flags field of the Db structure. If the database
122578** file was of zero-length, then the DB_Empty flag is also set.
122579*/
122580SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
122581 int i, rc;
122582 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
122583
122584 assert( sqlite3_mutex_held(db->mutex) );
122585 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
122586 assert( db->init.busy==0 );
122587 ENC(db) = SCHEMA_ENC(db);
122588 assert( db->nDb>0 );
122589 /* Do the main schema first */
122590 if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
122591 rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
122592 if( rc ) return rc;
122593 }
122594 /* All other schemas after the main schema. The "temp" schema must be last */
122595 for(i=db->nDb-1; i>0; i--){
122596 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
122597 if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
122598 rc = sqlite3InitOne(db, i, pzErrMsg, 0);
122599 if( rc ) return rc;
122600 }
122601 }
122602 if( commit_internal ){
122603 sqlite3CommitInternalChanges(db);
122604 }
122605 return SQLITE_OK;
122606}
122607
122608/*
122609** This routine is a no-op if the database schema is already initialized.
122610** Otherwise, the schema is loaded. An error code is returned.
122611*/
122612SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
122613 int rc = SQLITE_OK;
122614 sqlite3 *db = pParse->db;
122615 assert( sqlite3_mutex_held(db->mutex) );
122616 if( !db->init.busy ){
122617 rc = sqlite3Init(db, &pParse->zErrMsg);
122618 if( rc!=SQLITE_OK ){
122619 pParse->rc = rc;
122620 pParse->nErr++;
122621 }else if( db->noSharedCache ){
122622 db->mDbFlags |= DBFLAG_SchemaKnownOk;
122623 }
122624 }
122625 return rc;
122626}
122627
122628
122629/*
122630** Check schema cookies in all databases. If any cookie is out
122631** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
122632** make no changes to pParse->rc.
122633*/
122634static void schemaIsValid(Parse *pParse){
122635 sqlite3 *db = pParse->db;
122636 int iDb;
122637 int rc;
122638 int cookie;
122639
122640 assert( pParse->checkSchema );
122641 assert( sqlite3_mutex_held(db->mutex) );
122642 for(iDb=0; iDb<db->nDb; iDb++){
122643 int openedTransaction = 0; /* True if a transaction is opened */
122644 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
122645 if( pBt==0 ) continue;
122646
122647 /* If there is not already a read-only (or read-write) transaction opened
122648 ** on the b-tree database, open one now. If a transaction is opened, it
122649 ** will be closed immediately after reading the meta-value. */
122650 if( !sqlite3BtreeIsInReadTrans(pBt) ){
122651 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
122652 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
122653 sqlite3OomFault(db);
122654 }
122655 if( rc!=SQLITE_OK ) return;
122656 openedTransaction = 1;
122657 }
122658
122659 /* Read the schema cookie from the database. If it does not match the
122660 ** value stored as part of the in-memory schema representation,
122661 ** set Parse.rc to SQLITE_SCHEMA. */
122662 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
122663 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
122664 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
122665 sqlite3ResetOneSchema(db, iDb);
122666 pParse->rc = SQLITE_SCHEMA;
122667 }
122668
122669 /* Close the transaction, if one was opened. */
122670 if( openedTransaction ){
122671 sqlite3BtreeCommit(pBt);
122672 }
122673 }
122674}
122675
122676/*
122677** Convert a schema pointer into the iDb index that indicates
122678** which database file in db->aDb[] the schema refers to.
122679**
122680** If the same database is attached more than once, the first
122681** attached database is returned.
122682*/
122683SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
122684 int i = -1000000;
122685
122686 /* If pSchema is NULL, then return -1000000. This happens when code in
122687 ** expr.c is trying to resolve a reference to a transient table (i.e. one
122688 ** created by a sub-select). In this case the return value of this
122689 ** function should never be used.
122690 **
122691 ** We return -1000000 instead of the more usual -1 simply because using
122692 ** -1000000 as the incorrect index into db->aDb[] is much
122693 ** more likely to cause a segfault than -1 (of course there are assert()
122694 ** statements too, but it never hurts to play the odds).
122695 */
122696 assert( sqlite3_mutex_held(db->mutex) );
122697 if( pSchema ){
122698 for(i=0; 1; i++){
122699 assert( i<db->nDb );
122700 if( db->aDb[i].pSchema==pSchema ){
122701 break;
122702 }
122703 }
122704 assert( i>=0 && i<db->nDb );
122705 }
122706 return i;
122707}
122708
122709/*
122710** Free all memory allocations in the pParse object
122711*/
122712SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
122713 sqlite3 *db = pParse->db;
122714 sqlite3DbFree(db, pParse->aLabel);
122715 sqlite3ExprListDelete(db, pParse->pConstExpr);
122716 if( db ){
122717 assert( db->lookaside.bDisable >= pParse->disableLookaside );
122718 db->lookaside.bDisable -= pParse->disableLookaside;
122719 }
122720 pParse->disableLookaside = 0;
122721}
122722
122723/*
122724** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
122725*/
122726static int sqlite3Prepare(
122727 sqlite3 *db, /* Database handle. */
122728 const char *zSql, /* UTF-8 encoded SQL statement. */
122729 int nBytes, /* Length of zSql in bytes. */
122730 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
122731 Vdbe *pReprepare, /* VM being reprepared */
122732 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
122733 const char **pzTail /* OUT: End of parsed string */
122734){
122735 char *zErrMsg = 0; /* Error message */
122736 int rc = SQLITE_OK; /* Result code */
122737 int i; /* Loop counter */
122738 Parse sParse; /* Parsing context */
122739
122740 memset(&sParse, 0, PARSE_HDR_SZ);
122741 memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
122742 sParse.pReprepare = pReprepare;
122743 assert( ppStmt && *ppStmt==0 );
122744 /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
122745 assert( sqlite3_mutex_held(db->mutex) );
122746
122747 /* For a long-term use prepared statement avoid the use of
122748 ** lookaside memory.
122749 */
122750 if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
122751 sParse.disableLookaside++;
122752 db->lookaside.bDisable++;
122753 }
122754
122755 /* Check to verify that it is possible to get a read lock on all
122756 ** database schemas. The inability to get a read lock indicates that
122757 ** some other database connection is holding a write-lock, which in
122758 ** turn means that the other connection has made uncommitted changes
122759 ** to the schema.
122760 **
122761 ** Were we to proceed and prepare the statement against the uncommitted
122762 ** schema changes and if those schema changes are subsequently rolled
122763 ** back and different changes are made in their place, then when this
122764 ** prepared statement goes to run the schema cookie would fail to detect
122765 ** the schema change. Disaster would follow.
122766 **
122767 ** This thread is currently holding mutexes on all Btrees (because
122768 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
122769 ** is not possible for another thread to start a new schema change
122770 ** while this routine is running. Hence, we do not need to hold
122771 ** locks on the schema, we just need to make sure nobody else is
122772 ** holding them.
122773 **
122774 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
122775 ** but it does *not* override schema lock detection, so this all still
122776 ** works even if READ_UNCOMMITTED is set.
122777 */
122778 for(i=0; i<db->nDb; i++) {
122779 Btree *pBt = db->aDb[i].pBt;
122780 if( pBt ){
122781 assert( sqlite3BtreeHoldsMutex(pBt) );
122782 rc = sqlite3BtreeSchemaLocked(pBt);
122783 if( rc ){
122784 const char *zDb = db->aDb[i].zDbSName;
122785 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
122786 testcase( db->flags & SQLITE_ReadUncommit );
122787 goto end_prepare;
122788 }
122789 }
122790 }
122791
122792 sqlite3VtabUnlockList(db);
122793
122794 sParse.db = db;
122795 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
122796 char *zSqlCopy;
122797 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
122798 testcase( nBytes==mxLen );
122799 testcase( nBytes==mxLen+1 );
122800 if( nBytes>mxLen ){
122801 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
122802 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
122803 goto end_prepare;
122804 }
122805 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
122806 if( zSqlCopy ){
122807 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
122808 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
122809 sqlite3DbFree(db, zSqlCopy);
122810 }else{
122811 sParse.zTail = &zSql[nBytes];
122812 }
122813 }else{
122814 sqlite3RunParser(&sParse, zSql, &zErrMsg);
122815 }
122816 assert( 0==sParse.nQueryLoop );
122817
122818 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
122819 if( sParse.checkSchema ){
122820 schemaIsValid(&sParse);
122821 }
122822 if( db->mallocFailed ){
122823 sParse.rc = SQLITE_NOMEM_BKPT;
122824 }
122825 if( pzTail ){
122826 *pzTail = sParse.zTail;
122827 }
122828 rc = sParse.rc;
122829
122830#ifndef SQLITE_OMIT_EXPLAIN
122831 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
122832 static const char * const azColName[] = {
122833 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
122834 "id", "parent", "notused", "detail"
122835 };
122836 int iFirst, mx;
122837 if( sParse.explain==2 ){
122838 sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
122839 iFirst = 8;
122840 mx = 12;
122841 }else{
122842 sqlite3VdbeSetNumCols(sParse.pVdbe, 8);
122843 iFirst = 0;
122844 mx = 8;
122845 }
122846 for(i=iFirst; i<mx; i++){
122847 sqlite3VdbeSetColName(sParse.pVdbe, i-iFirst, COLNAME_NAME,
122848 azColName[i], SQLITE_STATIC);
122849 }
122850 }
122851#endif
122852
122853 if( db->init.busy==0 ){
122854 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
122855 }
122856 if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
122857 sqlite3VdbeFinalize(sParse.pVdbe);
122858 assert(!(*ppStmt));
122859 }else{
122860 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
122861 }
122862
122863 if( zErrMsg ){
122864 sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
122865 sqlite3DbFree(db, zErrMsg);
122866 }else{
122867 sqlite3Error(db, rc);
122868 }
122869
122870 /* Delete any TriggerPrg structures allocated while parsing this statement. */
122871 while( sParse.pTriggerPrg ){
122872 TriggerPrg *pT = sParse.pTriggerPrg;
122873 sParse.pTriggerPrg = pT->pNext;
122874 sqlite3DbFree(db, pT);
122875 }
122876
122877end_prepare:
122878
122879 sqlite3ParserReset(&sParse);
122880 return rc;
122881}
122882static int sqlite3LockAndPrepare(
122883 sqlite3 *db, /* Database handle. */
122884 const char *zSql, /* UTF-8 encoded SQL statement. */
122885 int nBytes, /* Length of zSql in bytes. */
122886 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
122887 Vdbe *pOld, /* VM being reprepared */
122888 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
122889 const char **pzTail /* OUT: End of parsed string */
122890){
122891 int rc;
122892 int cnt = 0;
122893
122894#ifdef SQLITE_ENABLE_API_ARMOR
122895 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
122896#endif
122897 *ppStmt = 0;
122898 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
122899 return SQLITE_MISUSE_BKPT;
122900 }
122901 sqlite3_mutex_enter(db->mutex);
122902 sqlite3BtreeEnterAll(db);
122903 do{
122904 /* Make multiple attempts to compile the SQL, until it either succeeds
122905 ** or encounters a permanent error. A schema problem after one schema
122906 ** reset is considered a permanent error. */
122907 rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
122908 assert( rc==SQLITE_OK || *ppStmt==0 );
122909 }while( rc==SQLITE_ERROR_RETRY
122910 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
122911 sqlite3BtreeLeaveAll(db);
122912 rc = sqlite3ApiExit(db, rc);
122913 assert( (rc&db->errMask)==rc );
122914 sqlite3_mutex_leave(db->mutex);
122915 return rc;
122916}
122917
122918/*
122919** Rerun the compilation of a statement after a schema change.
122920**
122921** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
122922** if the statement cannot be recompiled because another connection has
122923** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
122924** occurs, return SQLITE_SCHEMA.
122925*/
122926SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
122927 int rc;
122928 sqlite3_stmt *pNew;
122929 const char *zSql;
122930 sqlite3 *db;
122931 u8 prepFlags;
122932
122933 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
122934 zSql = sqlite3_sql((sqlite3_stmt *)p);
122935 assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */
122936 db = sqlite3VdbeDb(p);
122937 assert( sqlite3_mutex_held(db->mutex) );
122938 prepFlags = sqlite3VdbePrepareFlags(p);
122939 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
122940 if( rc ){
122941 if( rc==SQLITE_NOMEM ){
122942 sqlite3OomFault(db);
122943 }
122944 assert( pNew==0 );
122945 return rc;
122946 }else{
122947 assert( pNew!=0 );
122948 }
122949 sqlite3VdbeSwap((Vdbe*)pNew, p);
122950 sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
122951 sqlite3VdbeResetStepResult((Vdbe*)pNew);
122952 sqlite3VdbeFinalize((Vdbe*)pNew);
122953 return SQLITE_OK;
122954}
122955
122956
122957/*
122958** Two versions of the official API. Legacy and new use. In the legacy
122959** version, the original SQL text is not saved in the prepared statement
122960** and so if a schema change occurs, SQLITE_SCHEMA is returned by
122961** sqlite3_step(). In the new version, the original SQL text is retained
122962** and the statement is automatically recompiled if an schema change
122963** occurs.
122964*/
122965SQLITE_API int sqlite3_prepare(
122966 sqlite3 *db, /* Database handle. */
122967 const char *zSql, /* UTF-8 encoded SQL statement. */
122968 int nBytes, /* Length of zSql in bytes. */
122969 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
122970 const char **pzTail /* OUT: End of parsed string */
122971){
122972 int rc;
122973 rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
122974 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
122975 return rc;
122976}
122977SQLITE_API int sqlite3_prepare_v2(
122978 sqlite3 *db, /* Database handle. */
122979 const char *zSql, /* UTF-8 encoded SQL statement. */
122980 int nBytes, /* Length of zSql in bytes. */
122981 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
122982 const char **pzTail /* OUT: End of parsed string */
122983){
122984 int rc;
122985 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
122986 ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
122987 ** parameter.
122988 **
122989 ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
122990 rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,0,
122991 ppStmt,pzTail);
122992 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
122993 return rc;
122994}
122995SQLITE_API int sqlite3_prepare_v3(
122996 sqlite3 *db, /* Database handle. */
122997 const char *zSql, /* UTF-8 encoded SQL statement. */
122998 int nBytes, /* Length of zSql in bytes. */
122999 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
123000 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123001 const char **pzTail /* OUT: End of parsed string */
123002){
123003 int rc;
123004 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
123005 ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
123006 ** which is a bit array consisting of zero or more of the
123007 ** SQLITE_PREPARE_* flags.
123008 **
123009 ** Proof by comparison to the implementation of sqlite3_prepare_v2()
123010 ** directly above. */
123011 rc = sqlite3LockAndPrepare(db,zSql,nBytes,
123012 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
123013 0,ppStmt,pzTail);
123014 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
123015 return rc;
123016}
123017
123018
123019#ifndef SQLITE_OMIT_UTF16
123020/*
123021** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
123022*/
123023static int sqlite3Prepare16(
123024 sqlite3 *db, /* Database handle. */
123025 const void *zSql, /* UTF-16 encoded SQL statement. */
123026 int nBytes, /* Length of zSql in bytes. */
123027 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
123028 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123029 const void **pzTail /* OUT: End of parsed string */
123030){
123031 /* This function currently works by first transforming the UTF-16
123032 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
123033 ** tricky bit is figuring out the pointer to return in *pzTail.
123034 */
123035 char *zSql8;
123036 const char *zTail8 = 0;
123037 int rc = SQLITE_OK;
123038
123039#ifdef SQLITE_ENABLE_API_ARMOR
123040 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
123041#endif
123042 *ppStmt = 0;
123043 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
123044 return SQLITE_MISUSE_BKPT;
123045 }
123046 if( nBytes>=0 ){
123047 int sz;
123048 const char *z = (const char*)zSql;
123049 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
123050 nBytes = sz;
123051 }
123052 sqlite3_mutex_enter(db->mutex);
123053 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
123054 if( zSql8 ){
123055 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
123056 }
123057
123058 if( zTail8 && pzTail ){
123059 /* If sqlite3_prepare returns a tail pointer, we calculate the
123060 ** equivalent pointer into the UTF-16 string by counting the unicode
123061 ** characters between zSql8 and zTail8, and then returning a pointer
123062 ** the same number of characters into the UTF-16 string.
123063 */
123064 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
123065 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
123066 }
123067 sqlite3DbFree(db, zSql8);
123068 rc = sqlite3ApiExit(db, rc);
123069 sqlite3_mutex_leave(db->mutex);
123070 return rc;
123071}
123072
123073/*
123074** Two versions of the official API. Legacy and new use. In the legacy
123075** version, the original SQL text is not saved in the prepared statement
123076** and so if a schema change occurs, SQLITE_SCHEMA is returned by
123077** sqlite3_step(). In the new version, the original SQL text is retained
123078** and the statement is automatically recompiled if an schema change
123079** occurs.
123080*/
123081SQLITE_API int sqlite3_prepare16(
123082 sqlite3 *db, /* Database handle. */
123083 const void *zSql, /* UTF-16 encoded SQL statement. */
123084 int nBytes, /* Length of zSql in bytes. */
123085 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123086 const void **pzTail /* OUT: End of parsed string */
123087){
123088 int rc;
123089 rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
123090 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
123091 return rc;
123092}
123093SQLITE_API int sqlite3_prepare16_v2(
123094 sqlite3 *db, /* Database handle. */
123095 const void *zSql, /* UTF-16 encoded SQL statement. */
123096 int nBytes, /* Length of zSql in bytes. */
123097 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123098 const void **pzTail /* OUT: End of parsed string */
123099){
123100 int rc;
123101 rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail);
123102 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
123103 return rc;
123104}
123105SQLITE_API int sqlite3_prepare16_v3(
123106 sqlite3 *db, /* Database handle. */
123107 const void *zSql, /* UTF-16 encoded SQL statement. */
123108 int nBytes, /* Length of zSql in bytes. */
123109 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
123110 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
123111 const void **pzTail /* OUT: End of parsed string */
123112){
123113 int rc;
123114 rc = sqlite3Prepare16(db,zSql,nBytes,
123115 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
123116 ppStmt,pzTail);
123117 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
123118 return rc;
123119}
123120
123121#endif /* SQLITE_OMIT_UTF16 */
123122
123123/************** End of prepare.c *********************************************/
123124/************** Begin file select.c ******************************************/
123125/*
123126** 2001 September 15
123127**
123128** The author disclaims copyright to this source code. In place of
123129** a legal notice, here is a blessing:
123130**
123131** May you do good and not evil.
123132** May you find forgiveness for yourself and forgive others.
123133** May you share freely, never taking more than you give.
123134**
123135*************************************************************************
123136** This file contains C code routines that are called by the parser
123137** to handle SELECT statements in SQLite.
123138*/
123139/* #include "sqliteInt.h" */
123140
123141/*
123142** Trace output macros
123143*/
123144#if SELECTTRACE_ENABLED
123145/***/ int sqlite3SelectTrace = 0;
123146# define SELECTTRACE(K,P,S,X) \
123147 if(sqlite3SelectTrace&(K)) \
123148 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
123149 sqlite3DebugPrintf X
123150#else
123151# define SELECTTRACE(K,P,S,X)
123152#endif
123153
123154
123155/*
123156** An instance of the following object is used to record information about
123157** how to process the DISTINCT keyword, to simplify passing that information
123158** into the selectInnerLoop() routine.
123159*/
123160typedef struct DistinctCtx DistinctCtx;
123161struct DistinctCtx {
123162 u8 isTnct; /* True if the DISTINCT keyword is present */
123163 u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
123164 int tabTnct; /* Ephemeral table used for DISTINCT processing */
123165 int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
123166};
123167
123168/*
123169** An instance of the following object is used to record information about
123170** the ORDER BY (or GROUP BY) clause of query is being coded.
123171**
123172** The aDefer[] array is used by the sorter-references optimization. For
123173** example, assuming there is no index that can be used for the ORDER BY,
123174** for the query:
123175**
123176** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
123177**
123178** it may be more efficient to add just the "a" values to the sorter, and
123179** retrieve the associated "bigblob" values directly from table t1 as the
123180** 10 smallest "a" values are extracted from the sorter.
123181**
123182** When the sorter-reference optimization is used, there is one entry in the
123183** aDefer[] array for each database table that may be read as values are
123184** extracted from the sorter.
123185*/
123186typedef struct SortCtx SortCtx;
123187struct SortCtx {
123188 ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */
123189 int nOBSat; /* Number of ORDER BY terms satisfied by indices */
123190 int iECursor; /* Cursor number for the sorter */
123191 int regReturn; /* Register holding block-output return address */
123192 int labelBkOut; /* Start label for the block-output subroutine */
123193 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
123194 int labelDone; /* Jump here when done, ex: LIMIT reached */
123195 int labelOBLopt; /* Jump here when sorter is full */
123196 u8 sortFlags; /* Zero or more SORTFLAG_* bits */
123197#ifdef SQLITE_ENABLE_SORTER_REFERENCES
123198 u8 nDefer; /* Number of valid entries in aDefer[] */
123199 struct DeferredCsr {
123200 Table *pTab; /* Table definition */
123201 int iCsr; /* Cursor number for table */
123202 int nKey; /* Number of PK columns for table pTab (>=1) */
123203 } aDefer[4];
123204#endif
123205 struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */
123206};
123207#define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */
123208
123209/*
123210** Delete all the content of a Select structure. Deallocate the structure
123211** itself only if bFree is true.
123212*/
123213static void clearSelect(sqlite3 *db, Select *p, int bFree){
123214 while( p ){
123215 Select *pPrior = p->pPrior;
123216 sqlite3ExprListDelete(db, p->pEList);
123217 sqlite3SrcListDelete(db, p->pSrc);
123218 sqlite3ExprDelete(db, p->pWhere);
123219 sqlite3ExprListDelete(db, p->pGroupBy);
123220 sqlite3ExprDelete(db, p->pHaving);
123221 sqlite3ExprListDelete(db, p->pOrderBy);
123222 sqlite3ExprDelete(db, p->pLimit);
123223#ifndef SQLITE_OMIT_WINDOWFUNC
123224 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
123225 sqlite3WindowListDelete(db, p->pWinDefn);
123226 }
123227#endif
123228 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
123229 if( bFree ) sqlite3DbFreeNN(db, p);
123230 p = pPrior;
123231 bFree = 1;
123232 }
123233}
123234
123235/*
123236** Initialize a SelectDest structure.
123237*/
123238SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
123239 pDest->eDest = (u8)eDest;
123240 pDest->iSDParm = iParm;
123241 pDest->zAffSdst = 0;
123242 pDest->iSdst = 0;
123243 pDest->nSdst = 0;
123244}
123245
123246
123247/*
123248** Allocate a new Select structure and return a pointer to that
123249** structure.
123250*/
123251SQLITE_PRIVATE Select *sqlite3SelectNew(
123252 Parse *pParse, /* Parsing context */
123253 ExprList *pEList, /* which columns to include in the result */
123254 SrcList *pSrc, /* the FROM clause -- which tables to scan */
123255 Expr *pWhere, /* the WHERE clause */
123256 ExprList *pGroupBy, /* the GROUP BY clause */
123257 Expr *pHaving, /* the HAVING clause */
123258 ExprList *pOrderBy, /* the ORDER BY clause */
123259 u32 selFlags, /* Flag parameters, such as SF_Distinct */
123260 Expr *pLimit /* LIMIT value. NULL means not used */
123261){
123262 Select *pNew;
123263 Select standin;
123264 pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
123265 if( pNew==0 ){
123266 assert( pParse->db->mallocFailed );
123267 pNew = &standin;
123268 }
123269 if( pEList==0 ){
123270 pEList = sqlite3ExprListAppend(pParse, 0,
123271 sqlite3Expr(pParse->db,TK_ASTERISK,0));
123272 }
123273 pNew->pEList = pEList;
123274 pNew->op = TK_SELECT;
123275 pNew->selFlags = selFlags;
123276 pNew->iLimit = 0;
123277 pNew->iOffset = 0;
123278 pNew->selId = ++pParse->nSelect;
123279 pNew->addrOpenEphm[0] = -1;
123280 pNew->addrOpenEphm[1] = -1;
123281 pNew->nSelectRow = 0;
123282 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
123283 pNew->pSrc = pSrc;
123284 pNew->pWhere = pWhere;
123285 pNew->pGroupBy = pGroupBy;
123286 pNew->pHaving = pHaving;
123287 pNew->pOrderBy = pOrderBy;
123288 pNew->pPrior = 0;
123289 pNew->pNext = 0;
123290 pNew->pLimit = pLimit;
123291 pNew->pWith = 0;
123292#ifndef SQLITE_OMIT_WINDOWFUNC
123293 pNew->pWin = 0;
123294 pNew->pWinDefn = 0;
123295#endif
123296 if( pParse->db->mallocFailed ) {
123297 clearSelect(pParse->db, pNew, pNew!=&standin);
123298 pNew = 0;
123299 }else{
123300 assert( pNew->pSrc!=0 || pParse->nErr>0 );
123301 }
123302 assert( pNew!=&standin );
123303 return pNew;
123304}
123305
123306
123307/*
123308** Delete the given Select structure and all of its substructures.
123309*/
123310SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
123311 if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1);
123312}
123313
123314/*
123315** Return a pointer to the right-most SELECT statement in a compound.
123316*/
123317static Select *findRightmost(Select *p){
123318 while( p->pNext ) p = p->pNext;
123319 return p;
123320}
123321
123322/*
123323** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
123324** type of join. Return an integer constant that expresses that type
123325** in terms of the following bit values:
123326**
123327** JT_INNER
123328** JT_CROSS
123329** JT_OUTER
123330** JT_NATURAL
123331** JT_LEFT
123332** JT_RIGHT
123333**
123334** A full outer join is the combination of JT_LEFT and JT_RIGHT.
123335**
123336** If an illegal or unsupported join type is seen, then still return
123337** a join type, but put an error in the pParse structure.
123338*/
123339SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
123340 int jointype = 0;
123341 Token *apAll[3];
123342 Token *p;
123343 /* 0123456789 123456789 123456789 123 */
123344 static const char zKeyText[] = "naturaleftouterightfullinnercross";
123345 static const struct {
123346 u8 i; /* Beginning of keyword text in zKeyText[] */
123347 u8 nChar; /* Length of the keyword in characters */
123348 u8 code; /* Join type mask */
123349 } aKeyword[] = {
123350 /* natural */ { 0, 7, JT_NATURAL },
123351 /* left */ { 6, 4, JT_LEFT|JT_OUTER },
123352 /* outer */ { 10, 5, JT_OUTER },
123353 /* right */ { 14, 5, JT_RIGHT|JT_OUTER },
123354 /* full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
123355 /* inner */ { 23, 5, JT_INNER },
123356 /* cross */ { 28, 5, JT_INNER|JT_CROSS },
123357 };
123358 int i, j;
123359 apAll[0] = pA;
123360 apAll[1] = pB;
123361 apAll[2] = pC;
123362 for(i=0; i<3 && apAll[i]; i++){
123363 p = apAll[i];
123364 for(j=0; j<ArraySize(aKeyword); j++){
123365 if( p->n==aKeyword[j].nChar
123366 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
123367 jointype |= aKeyword[j].code;
123368 break;
123369 }
123370 }
123371 testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
123372 if( j>=ArraySize(aKeyword) ){
123373 jointype |= JT_ERROR;
123374 break;
123375 }
123376 }
123377 if(
123378 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
123379 (jointype & JT_ERROR)!=0
123380 ){
123381 const char *zSp = " ";
123382 assert( pB!=0 );
123383 if( pC==0 ){ zSp++; }
123384 sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
123385 "%T %T%s%T", pA, pB, zSp, pC);
123386 jointype = JT_INNER;
123387 }else if( (jointype & JT_OUTER)!=0
123388 && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
123389 sqlite3ErrorMsg(pParse,
123390 "RIGHT and FULL OUTER JOINs are not currently supported");
123391 jointype = JT_INNER;
123392 }
123393 return jointype;
123394}
123395
123396/*
123397** Return the index of a column in a table. Return -1 if the column
123398** is not contained in the table.
123399*/
123400static int columnIndex(Table *pTab, const char *zCol){
123401 int i;
123402 for(i=0; i<pTab->nCol; i++){
123403 if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
123404 }
123405 return -1;
123406}
123407
123408/*
123409** Search the first N tables in pSrc, from left to right, looking for a
123410** table that has a column named zCol.
123411**
123412** When found, set *piTab and *piCol to the table index and column index
123413** of the matching column and return TRUE.
123414**
123415** If not found, return FALSE.
123416*/
123417static int tableAndColumnIndex(
123418 SrcList *pSrc, /* Array of tables to search */
123419 int N, /* Number of tables in pSrc->a[] to search */
123420 const char *zCol, /* Name of the column we are looking for */
123421 int *piTab, /* Write index of pSrc->a[] here */
123422 int *piCol /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
123423){
123424 int i; /* For looping over tables in pSrc */
123425 int iCol; /* Index of column matching zCol */
123426
123427 assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */
123428 for(i=0; i<N; i++){
123429 iCol = columnIndex(pSrc->a[i].pTab, zCol);
123430 if( iCol>=0 ){
123431 if( piTab ){
123432 *piTab = i;
123433 *piCol = iCol;
123434 }
123435 return 1;
123436 }
123437 }
123438 return 0;
123439}
123440
123441/*
123442** This function is used to add terms implied by JOIN syntax to the
123443** WHERE clause expression of a SELECT statement. The new term, which
123444** is ANDed with the existing WHERE clause, is of the form:
123445**
123446** (tab1.col1 = tab2.col2)
123447**
123448** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
123449** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
123450** column iColRight of tab2.
123451*/
123452static void addWhereTerm(
123453 Parse *pParse, /* Parsing context */
123454 SrcList *pSrc, /* List of tables in FROM clause */
123455 int iLeft, /* Index of first table to join in pSrc */
123456 int iColLeft, /* Index of column in first table */
123457 int iRight, /* Index of second table in pSrc */
123458 int iColRight, /* Index of column in second table */
123459 int isOuterJoin, /* True if this is an OUTER join */
123460 Expr **ppWhere /* IN/OUT: The WHERE clause to add to */
123461){
123462 sqlite3 *db = pParse->db;
123463 Expr *pE1;
123464 Expr *pE2;
123465 Expr *pEq;
123466
123467 assert( iLeft<iRight );
123468 assert( pSrc->nSrc>iRight );
123469 assert( pSrc->a[iLeft].pTab );
123470 assert( pSrc->a[iRight].pTab );
123471
123472 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
123473 pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
123474
123475 pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);
123476 if( pEq && isOuterJoin ){
123477 ExprSetProperty(pEq, EP_FromJoin);
123478 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
123479 ExprSetVVAProperty(pEq, EP_NoReduce);
123480 pEq->iRightJoinTable = (i16)pE2->iTable;
123481 }
123482 *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
123483}
123484
123485/*
123486** Set the EP_FromJoin property on all terms of the given expression.
123487** And set the Expr.iRightJoinTable to iTable for every term in the
123488** expression.
123489**
123490** The EP_FromJoin property is used on terms of an expression to tell
123491** the LEFT OUTER JOIN processing logic that this term is part of the
123492** join restriction specified in the ON or USING clause and not a part
123493** of the more general WHERE clause. These terms are moved over to the
123494** WHERE clause during join processing but we need to remember that they
123495** originated in the ON or USING clause.
123496**
123497** The Expr.iRightJoinTable tells the WHERE clause processing that the
123498** expression depends on table iRightJoinTable even if that table is not
123499** explicitly mentioned in the expression. That information is needed
123500** for cases like this:
123501**
123502** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
123503**
123504** The where clause needs to defer the handling of the t1.x=5
123505** term until after the t2 loop of the join. In that way, a
123506** NULL t2 row will be inserted whenever t1.x!=5. If we do not
123507** defer the handling of t1.x=5, it will be processed immediately
123508** after the t1 loop and rows with t1.x!=5 will never appear in
123509** the output, which is incorrect.
123510*/
123511static void setJoinExpr(Expr *p, int iTable){
123512 while( p ){
123513 ExprSetProperty(p, EP_FromJoin);
123514 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
123515 ExprSetVVAProperty(p, EP_NoReduce);
123516 p->iRightJoinTable = (i16)iTable;
123517 if( p->op==TK_FUNCTION && p->x.pList ){
123518 int i;
123519 for(i=0; i<p->x.pList->nExpr; i++){
123520 setJoinExpr(p->x.pList->a[i].pExpr, iTable);
123521 }
123522 }
123523 setJoinExpr(p->pLeft, iTable);
123524 p = p->pRight;
123525 }
123526}
123527
123528/* Undo the work of setJoinExpr(). In the expression tree p, convert every
123529** term that is marked with EP_FromJoin and iRightJoinTable==iTable into
123530** an ordinary term that omits the EP_FromJoin mark.
123531**
123532** This happens when a LEFT JOIN is simplified into an ordinary JOIN.
123533*/
123534static void unsetJoinExpr(Expr *p, int iTable){
123535 while( p ){
123536 if( ExprHasProperty(p, EP_FromJoin)
123537 && (iTable<0 || p->iRightJoinTable==iTable) ){
123538 ExprClearProperty(p, EP_FromJoin);
123539 }
123540 if( p->op==TK_FUNCTION && p->x.pList ){
123541 int i;
123542 for(i=0; i<p->x.pList->nExpr; i++){
123543 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable);
123544 }
123545 }
123546 unsetJoinExpr(p->pLeft, iTable);
123547 p = p->pRight;
123548 }
123549}
123550
123551/*
123552** This routine processes the join information for a SELECT statement.
123553** ON and USING clauses are converted into extra terms of the WHERE clause.
123554** NATURAL joins also create extra WHERE clause terms.
123555**
123556** The terms of a FROM clause are contained in the Select.pSrc structure.
123557** The left most table is the first entry in Select.pSrc. The right-most
123558** table is the last entry. The join operator is held in the entry to
123559** the left. Thus entry 0 contains the join operator for the join between
123560** entries 0 and 1. Any ON or USING clauses associated with the join are
123561** also attached to the left entry.
123562**
123563** This routine returns the number of errors encountered.
123564*/
123565static int sqliteProcessJoin(Parse *pParse, Select *p){
123566 SrcList *pSrc; /* All tables in the FROM clause */
123567 int i, j; /* Loop counters */
123568 struct SrcList_item *pLeft; /* Left table being joined */
123569 struct SrcList_item *pRight; /* Right table being joined */
123570
123571 pSrc = p->pSrc;
123572 pLeft = &pSrc->a[0];
123573 pRight = &pLeft[1];
123574 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
123575 Table *pRightTab = pRight->pTab;
123576 int isOuter;
123577
123578 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
123579 isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
123580
123581 /* When the NATURAL keyword is present, add WHERE clause terms for
123582 ** every column that the two tables have in common.
123583 */
123584 if( pRight->fg.jointype & JT_NATURAL ){
123585 if( pRight->pOn || pRight->pUsing ){
123586 sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
123587 "an ON or USING clause", 0);
123588 return 1;
123589 }
123590 for(j=0; j<pRightTab->nCol; j++){
123591 char *zName; /* Name of column in the right table */
123592 int iLeft; /* Matching left table */
123593 int iLeftCol; /* Matching column in the left table */
123594
123595 zName = pRightTab->aCol[j].zName;
123596 if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
123597 addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
123598 isOuter, &p->pWhere);
123599 }
123600 }
123601 }
123602
123603 /* Disallow both ON and USING clauses in the same join
123604 */
123605 if( pRight->pOn && pRight->pUsing ){
123606 sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
123607 "clauses in the same join");
123608 return 1;
123609 }
123610
123611 /* Add the ON clause to the end of the WHERE clause, connected by
123612 ** an AND operator.
123613 */
123614 if( pRight->pOn ){
123615 if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
123616 p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
123617 pRight->pOn = 0;
123618 }
123619
123620 /* Create extra terms on the WHERE clause for each column named
123621 ** in the USING clause. Example: If the two tables to be joined are
123622 ** A and B and the USING clause names X, Y, and Z, then add this
123623 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
123624 ** Report an error if any column mentioned in the USING clause is
123625 ** not contained in both tables to be joined.
123626 */
123627 if( pRight->pUsing ){
123628 IdList *pList = pRight->pUsing;
123629 for(j=0; j<pList->nId; j++){
123630 char *zName; /* Name of the term in the USING clause */
123631 int iLeft; /* Table on the left with matching column name */
123632 int iLeftCol; /* Column number of matching column on the left */
123633 int iRightCol; /* Column number of matching column on the right */
123634
123635 zName = pList->a[j].zName;
123636 iRightCol = columnIndex(pRightTab, zName);
123637 if( iRightCol<0
123638 || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
123639 ){
123640 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
123641 "not present in both tables", zName);
123642 return 1;
123643 }
123644 addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
123645 isOuter, &p->pWhere);
123646 }
123647 }
123648 }
123649 return 0;
123650}
123651
123652/*
123653** An instance of this object holds information (beyond pParse and pSelect)
123654** needed to load the next result row that is to be added to the sorter.
123655*/
123656typedef struct RowLoadInfo RowLoadInfo;
123657struct RowLoadInfo {
123658 int regResult; /* Store results in array of registers here */
123659 u8 ecelFlags; /* Flag argument to ExprCodeExprList() */
123660#ifdef SQLITE_ENABLE_SORTER_REFERENCES
123661 ExprList *pExtra; /* Extra columns needed by sorter refs */
123662 int regExtraResult; /* Where to load the extra columns */
123663#endif
123664};
123665
123666/*
123667** This routine does the work of loading query data into an array of
123668** registers so that it can be added to the sorter.
123669*/
123670static void innerLoopLoadRow(
123671 Parse *pParse, /* Statement under construction */
123672 Select *pSelect, /* The query being coded */
123673 RowLoadInfo *pInfo /* Info needed to complete the row load */
123674){
123675 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
123676 0, pInfo->ecelFlags);
123677#ifdef SQLITE_ENABLE_SORTER_REFERENCES
123678 if( pInfo->pExtra ){
123679 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
123680 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
123681 }
123682#endif
123683}
123684
123685/*
123686** Code the OP_MakeRecord instruction that generates the entry to be
123687** added into the sorter.
123688**
123689** Return the register in which the result is stored.
123690*/
123691static int makeSorterRecord(
123692 Parse *pParse,
123693 SortCtx *pSort,
123694 Select *pSelect,
123695 int regBase,
123696 int nBase
123697){
123698 int nOBSat = pSort->nOBSat;
123699 Vdbe *v = pParse->pVdbe;
123700 int regOut = ++pParse->nMem;
123701 if( pSort->pDeferredRowLoad ){
123702 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
123703 }
123704 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
123705 return regOut;
123706}
123707
123708/*
123709** Generate code that will push the record in registers regData
123710** through regData+nData-1 onto the sorter.
123711*/
123712static void pushOntoSorter(
123713 Parse *pParse, /* Parser context */
123714 SortCtx *pSort, /* Information about the ORDER BY clause */
123715 Select *pSelect, /* The whole SELECT statement */
123716 int regData, /* First register holding data to be sorted */
123717 int regOrigData, /* First register holding data before packing */
123718 int nData, /* Number of elements in the regData data array */
123719 int nPrefixReg /* No. of reg prior to regData available for use */
123720){
123721 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
123722 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
123723 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
123724 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
123725 int regBase; /* Regs for sorter record */
123726 int regRecord = 0; /* Assembled sorter record */
123727 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
123728 int op; /* Opcode to add sorter record to sorter */
123729 int iLimit; /* LIMIT counter */
123730 int iSkip = 0; /* End of the sorter insert loop */
123731
123732 assert( bSeq==0 || bSeq==1 );
123733
123734 /* Three cases:
123735 ** (1) The data to be sorted has already been packed into a Record
123736 ** by a prior OP_MakeRecord. In this case nData==1 and regData
123737 ** will be completely unrelated to regOrigData.
123738 ** (2) All output columns are included in the sort record. In that
123739 ** case regData==regOrigData.
123740 ** (3) Some output columns are omitted from the sort record due to
123741 ** the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
123742 ** SQLITE_ECEL_OMITREF optimization, or due to the
123743 ** SortCtx.pDeferredRowLoad optimiation. In any of these cases
123744 ** regOrigData is 0 to prevent this routine from trying to copy
123745 ** values that might not yet exist.
123746 */
123747 assert( nData==1 || regData==regOrigData || regOrigData==0 );
123748
123749 if( nPrefixReg ){
123750 assert( nPrefixReg==nExpr+bSeq );
123751 regBase = regData - nPrefixReg;
123752 }else{
123753 regBase = pParse->nMem + 1;
123754 pParse->nMem += nBase;
123755 }
123756 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
123757 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
123758 pSort->labelDone = sqlite3VdbeMakeLabel(v);
123759 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
123760 SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));
123761 if( bSeq ){
123762 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
123763 }
123764 if( nPrefixReg==0 && nData>0 ){
123765 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
123766 }
123767 if( nOBSat>0 ){
123768 int regPrevKey; /* The first nOBSat columns of the previous row */
123769 int addrFirst; /* Address of the OP_IfNot opcode */
123770 int addrJmp; /* Address of the OP_Jump opcode */
123771 VdbeOp *pOp; /* Opcode that opens the sorter */
123772 int nKey; /* Number of sorting key columns, including OP_Sequence */
123773 KeyInfo *pKI; /* Original KeyInfo on the sorter table */
123774
123775 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
123776 regPrevKey = pParse->nMem+1;
123777 pParse->nMem += pSort->nOBSat;
123778 nKey = nExpr - pSort->nOBSat + bSeq;
123779 if( bSeq ){
123780 addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr);
123781 }else{
123782 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
123783 }
123784 VdbeCoverage(v);
123785 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
123786 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
123787 if( pParse->db->mallocFailed ) return;
123788 pOp->p2 = nKey + nData;
123789 pKI = pOp->p4.pKeyInfo;
123790 memset(pKI->aSortOrder, 0, pKI->nKeyField); /* Makes OP_Jump testable */
123791 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
123792 testcase( pKI->nAllField > pKI->nKeyField+2 );
123793 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
123794 pKI->nAllField-pKI->nKeyField-1);
123795 addrJmp = sqlite3VdbeCurrentAddr(v);
123796 sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
123797 pSort->labelBkOut = sqlite3VdbeMakeLabel(v);
123798 pSort->regReturn = ++pParse->nMem;
123799 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
123800 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
123801 if( iLimit ){
123802 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
123803 VdbeCoverage(v);
123804 }
123805 sqlite3VdbeJumpHere(v, addrFirst);
123806 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
123807 sqlite3VdbeJumpHere(v, addrJmp);
123808 }
123809 if( iLimit ){
123810 /* At this point the values for the new sorter entry are stored
123811 ** in an array of registers. They need to be composed into a record
123812 ** and inserted into the sorter if either (a) there are currently
123813 ** less than LIMIT+OFFSET items or (b) the new record is smaller than
123814 ** the largest record currently in the sorter. If (b) is true and there
123815 ** are already LIMIT+OFFSET items in the sorter, delete the largest
123816 ** entry before inserting the new one. This way there are never more
123817 ** than LIMIT+OFFSET items in the sorter.
123818 **
123819 ** If the new record does not need to be inserted into the sorter,
123820 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
123821 ** value is not zero, then it is a label of where to jump. Otherwise,
123822 ** just bypass the row insert logic. See the header comment on the
123823 ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
123824 */
123825 int iCsr = pSort->iECursor;
123826 sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);
123827 VdbeCoverage(v);
123828 sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);
123829 iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE,
123830 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
123831 VdbeCoverage(v);
123832 sqlite3VdbeAddOp1(v, OP_Delete, iCsr);
123833 }
123834 if( regRecord==0 ){
123835 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
123836 }
123837 if( pSort->sortFlags & SORTFLAG_UseSorter ){
123838 op = OP_SorterInsert;
123839 }else{
123840 op = OP_IdxInsert;
123841 }
123842 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
123843 regBase+nOBSat, nBase-nOBSat);
123844 if( iSkip ){
123845 sqlite3VdbeChangeP2(v, iSkip,
123846 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
123847 }
123848}
123849
123850/*
123851** Add code to implement the OFFSET
123852*/
123853static void codeOffset(
123854 Vdbe *v, /* Generate code into this VM */
123855 int iOffset, /* Register holding the offset counter */
123856 int iContinue /* Jump here to skip the current record */
123857){
123858 if( iOffset>0 ){
123859 sqlite3VdbeAddOp3(v, OP_IfPos, iOffset, iContinue, 1); VdbeCoverage(v);
123860 VdbeComment((v, "OFFSET"));
123861 }
123862}
123863
123864/*
123865** Add code that will check to make sure the N registers starting at iMem
123866** form a distinct entry. iTab is a sorting index that holds previously
123867** seen combinations of the N values. A new entry is made in iTab
123868** if the current N values are new.
123869**
123870** A jump to addrRepeat is made and the N+1 values are popped from the
123871** stack if the top N elements are not distinct.
123872*/
123873static void codeDistinct(
123874 Parse *pParse, /* Parsing and code generating context */
123875 int iTab, /* A sorting index used to test for distinctness */
123876 int addrRepeat, /* Jump to here if not distinct */
123877 int N, /* Number of elements */
123878 int iMem /* First element */
123879){
123880 Vdbe *v;
123881 int r1;
123882
123883 v = pParse->pVdbe;
123884 r1 = sqlite3GetTempReg(pParse);
123885 sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
123886 sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
123887 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
123888 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
123889 sqlite3ReleaseTempReg(pParse, r1);
123890}
123891
123892#ifdef SQLITE_ENABLE_SORTER_REFERENCES
123893/*
123894** This function is called as part of inner-loop generation for a SELECT
123895** statement with an ORDER BY that is not optimized by an index. It
123896** determines the expressions, if any, that the sorter-reference
123897** optimization should be used for. The sorter-reference optimization
123898** is used for SELECT queries like:
123899**
123900** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
123901**
123902** If the optimization is used for expression "bigblob", then instead of
123903** storing values read from that column in the sorter records, the PK of
123904** the row from table t1 is stored instead. Then, as records are extracted from
123905** the sorter to return to the user, the required value of bigblob is
123906** retrieved directly from table t1. If the values are very large, this
123907** can be more efficient than storing them directly in the sorter records.
123908**
123909** The ExprList_item.bSorterRef flag is set for each expression in pEList
123910** for which the sorter-reference optimization should be enabled.
123911** Additionally, the pSort->aDefer[] array is populated with entries
123912** for all cursors required to evaluate all selected expressions. Finally.
123913** output variable (*ppExtra) is set to an expression list containing
123914** expressions for all extra PK values that should be stored in the
123915** sorter records.
123916*/
123917static void selectExprDefer(
123918 Parse *pParse, /* Leave any error here */
123919 SortCtx *pSort, /* Sorter context */
123920 ExprList *pEList, /* Expressions destined for sorter */
123921 ExprList **ppExtra /* Expressions to append to sorter record */
123922){
123923 int i;
123924 int nDefer = 0;
123925 ExprList *pExtra = 0;
123926 for(i=0; i<pEList->nExpr; i++){
123927 struct ExprList_item *pItem = &pEList->a[i];
123928 if( pItem->u.x.iOrderByCol==0 ){
123929 Expr *pExpr = pItem->pExpr;
123930 Table *pTab = pExpr->pTab;
123931 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
123932 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
123933 ){
123934 int j;
123935 for(j=0; j<nDefer; j++){
123936 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
123937 }
123938 if( j==nDefer ){
123939 if( nDefer==ArraySize(pSort->aDefer) ){
123940 continue;
123941 }else{
123942 int nKey = 1;
123943 int k;
123944 Index *pPk = 0;
123945 if( !HasRowid(pTab) ){
123946 pPk = sqlite3PrimaryKeyIndex(pTab);
123947 nKey = pPk->nKeyCol;
123948 }
123949 for(k=0; k<nKey; k++){
123950 Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
123951 if( pNew ){
123952 pNew->iTable = pExpr->iTable;
123953 pNew->pTab = pExpr->pTab;
123954 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
123955 pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
123956 }
123957 }
123958 pSort->aDefer[nDefer].pTab = pExpr->pTab;
123959 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
123960 pSort->aDefer[nDefer].nKey = nKey;
123961 nDefer++;
123962 }
123963 }
123964 pItem->bSorterRef = 1;
123965 }
123966 }
123967 }
123968 pSort->nDefer = (u8)nDefer;
123969 *ppExtra = pExtra;
123970}
123971#endif
123972
123973/*
123974** This routine generates the code for the inside of the inner loop
123975** of a SELECT.
123976**
123977** If srcTab is negative, then the p->pEList expressions
123978** are evaluated in order to get the data for this row. If srcTab is
123979** zero or more, then data is pulled from srcTab and p->pEList is used only
123980** to get the number of columns and the collation sequence for each column.
123981*/
123982static void selectInnerLoop(
123983 Parse *pParse, /* The parser context */
123984 Select *p, /* The complete select statement being coded */
123985 int srcTab, /* Pull data from this table if non-negative */
123986 SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */
123987 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
123988 SelectDest *pDest, /* How to dispose of the results */
123989 int iContinue, /* Jump here to continue with next row */
123990 int iBreak /* Jump here to break out of the inner loop */
123991){
123992 Vdbe *v = pParse->pVdbe;
123993 int i;
123994 int hasDistinct; /* True if the DISTINCT keyword is present */
123995 int eDest = pDest->eDest; /* How to dispose of results */
123996 int iParm = pDest->iSDParm; /* First argument to disposal method */
123997 int nResultCol; /* Number of result columns */
123998 int nPrefixReg = 0; /* Number of extra registers before regResult */
123999 RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */
124000
124001 /* Usually, regResult is the first cell in an array of memory cells
124002 ** containing the current result row. In this case regOrig is set to the
124003 ** same value. However, if the results are being sent to the sorter, the
124004 ** values for any expressions that are also part of the sort-key are omitted
124005 ** from this array. In this case regOrig is set to zero. */
124006 int regResult; /* Start of memory holding current results */
124007 int regOrig; /* Start of memory holding full result (or 0) */
124008
124009 assert( v );
124010 assert( p->pEList!=0 );
124011 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
124012 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
124013 if( pSort==0 && !hasDistinct ){
124014 assert( iContinue!=0 );
124015 codeOffset(v, p->iOffset, iContinue);
124016 }
124017
124018 /* Pull the requested columns.
124019 */
124020 nResultCol = p->pEList->nExpr;
124021
124022 if( pDest->iSdst==0 ){
124023 if( pSort ){
124024 nPrefixReg = pSort->pOrderBy->nExpr;
124025 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
124026 pParse->nMem += nPrefixReg;
124027 }
124028 pDest->iSdst = pParse->nMem+1;
124029 pParse->nMem += nResultCol;
124030 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
124031 /* This is an error condition that can result, for example, when a SELECT
124032 ** on the right-hand side of an INSERT contains more result columns than
124033 ** there are columns in the table on the left. The error will be caught
124034 ** and reported later. But we need to make sure enough memory is allocated
124035 ** to avoid other spurious errors in the meantime. */
124036 pParse->nMem += nResultCol;
124037 }
124038 pDest->nSdst = nResultCol;
124039 regOrig = regResult = pDest->iSdst;
124040 if( srcTab>=0 ){
124041 for(i=0; i<nResultCol; i++){
124042 sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
124043 VdbeComment((v, "%s", p->pEList->a[i].zName));
124044 }
124045 }else if( eDest!=SRT_Exists ){
124046#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124047 ExprList *pExtra = 0;
124048#endif
124049 /* If the destination is an EXISTS(...) expression, the actual
124050 ** values returned by the SELECT are not required.
124051 */
124052 u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */
124053 ExprList *pEList;
124054 if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
124055 ecelFlags = SQLITE_ECEL_DUP;
124056 }else{
124057 ecelFlags = 0;
124058 }
124059 if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
124060 /* For each expression in p->pEList that is a copy of an expression in
124061 ** the ORDER BY clause (pSort->pOrderBy), set the associated
124062 ** iOrderByCol value to one more than the index of the ORDER BY
124063 ** expression within the sort-key that pushOntoSorter() will generate.
124064 ** This allows the p->pEList field to be omitted from the sorted record,
124065 ** saving space and CPU cycles. */
124066 ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
124067
124068 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
124069 int j;
124070 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
124071 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
124072 }
124073 }
124074#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124075 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
124076 if( pExtra && pParse->db->mallocFailed==0 ){
124077 /* If there are any extra PK columns to add to the sorter records,
124078 ** allocate extra memory cells and adjust the OpenEphemeral
124079 ** instruction to account for the larger records. This is only
124080 ** required if there are one or more WITHOUT ROWID tables with
124081 ** composite primary keys in the SortCtx.aDefer[] array. */
124082 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
124083 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
124084 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
124085 pParse->nMem += pExtra->nExpr;
124086 }
124087#endif
124088
124089 /* Adjust nResultCol to account for columns that are omitted
124090 ** from the sorter by the optimizations in this branch */
124091 pEList = p->pEList;
124092 for(i=0; i<pEList->nExpr; i++){
124093 if( pEList->a[i].u.x.iOrderByCol>0
124094#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124095 || pEList->a[i].bSorterRef
124096#endif
124097 ){
124098 nResultCol--;
124099 regOrig = 0;
124100 }
124101 }
124102
124103 testcase( regOrig );
124104 testcase( eDest==SRT_Set );
124105 testcase( eDest==SRT_Mem );
124106 testcase( eDest==SRT_Coroutine );
124107 testcase( eDest==SRT_Output );
124108 assert( eDest==SRT_Set || eDest==SRT_Mem
124109 || eDest==SRT_Coroutine || eDest==SRT_Output );
124110 }
124111 sRowLoadInfo.regResult = regResult;
124112 sRowLoadInfo.ecelFlags = ecelFlags;
124113#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124114 sRowLoadInfo.pExtra = pExtra;
124115 sRowLoadInfo.regExtraResult = regResult + nResultCol;
124116 if( pExtra ) nResultCol += pExtra->nExpr;
124117#endif
124118 if( p->iLimit
124119 && (ecelFlags & SQLITE_ECEL_OMITREF)!=0
124120 && nPrefixReg>0
124121 ){
124122 assert( pSort!=0 );
124123 assert( hasDistinct==0 );
124124 pSort->pDeferredRowLoad = &sRowLoadInfo;
124125 regOrig = 0;
124126 }else{
124127 innerLoopLoadRow(pParse, p, &sRowLoadInfo);
124128 }
124129 }
124130
124131 /* If the DISTINCT keyword was present on the SELECT statement
124132 ** and this row has been seen before, then do not make this row
124133 ** part of the result.
124134 */
124135 if( hasDistinct ){
124136 switch( pDistinct->eTnctType ){
124137 case WHERE_DISTINCT_ORDERED: {
124138 VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
124139 int iJump; /* Jump destination */
124140 int regPrev; /* Previous row content */
124141
124142 /* Allocate space for the previous row */
124143 regPrev = pParse->nMem+1;
124144 pParse->nMem += nResultCol;
124145
124146 /* Change the OP_OpenEphemeral coded earlier to an OP_Null
124147 ** sets the MEM_Cleared bit on the first register of the
124148 ** previous value. This will cause the OP_Ne below to always
124149 ** fail on the first iteration of the loop even if the first
124150 ** row is all NULLs.
124151 */
124152 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
124153 pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
124154 pOp->opcode = OP_Null;
124155 pOp->p1 = 1;
124156 pOp->p2 = regPrev;
124157
124158 iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
124159 for(i=0; i<nResultCol; i++){
124160 CollSeq *pColl = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
124161 if( i<nResultCol-1 ){
124162 sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
124163 VdbeCoverage(v);
124164 }else{
124165 sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
124166 VdbeCoverage(v);
124167 }
124168 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
124169 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
124170 }
124171 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
124172 sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
124173 break;
124174 }
124175
124176 case WHERE_DISTINCT_UNIQUE: {
124177 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
124178 break;
124179 }
124180
124181 default: {
124182 assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
124183 codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
124184 regResult);
124185 break;
124186 }
124187 }
124188 if( pSort==0 ){
124189 codeOffset(v, p->iOffset, iContinue);
124190 }
124191 }
124192
124193 switch( eDest ){
124194 /* In this mode, write each query result to the key of the temporary
124195 ** table iParm.
124196 */
124197#ifndef SQLITE_OMIT_COMPOUND_SELECT
124198 case SRT_Union: {
124199 int r1;
124200 r1 = sqlite3GetTempReg(pParse);
124201 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
124202 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
124203 sqlite3ReleaseTempReg(pParse, r1);
124204 break;
124205 }
124206
124207 /* Construct a record from the query result, but instead of
124208 ** saving that record, use it as a key to delete elements from
124209 ** the temporary table iParm.
124210 */
124211 case SRT_Except: {
124212 sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
124213 break;
124214 }
124215#endif /* SQLITE_OMIT_COMPOUND_SELECT */
124216
124217 /* Store the result as data using a unique key.
124218 */
124219 case SRT_Fifo:
124220 case SRT_DistFifo:
124221 case SRT_Table:
124222 case SRT_EphemTab: {
124223 int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
124224 testcase( eDest==SRT_Table );
124225 testcase( eDest==SRT_EphemTab );
124226 testcase( eDest==SRT_Fifo );
124227 testcase( eDest==SRT_DistFifo );
124228 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
124229#ifndef SQLITE_OMIT_CTE
124230 if( eDest==SRT_DistFifo ){
124231 /* If the destination is DistFifo, then cursor (iParm+1) is open
124232 ** on an ephemeral index. If the current row is already present
124233 ** in the index, do not write it to the output. If not, add the
124234 ** current row to the index and proceed with writing it to the
124235 ** output table as well. */
124236 int addr = sqlite3VdbeCurrentAddr(v) + 4;
124237 sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
124238 VdbeCoverage(v);
124239 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol);
124240 assert( pSort==0 );
124241 }
124242#endif
124243 if( pSort ){
124244 assert( regResult==regOrig );
124245 pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
124246 }else{
124247 int r2 = sqlite3GetTempReg(pParse);
124248 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
124249 sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
124250 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
124251 sqlite3ReleaseTempReg(pParse, r2);
124252 }
124253 sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1);
124254 break;
124255 }
124256
124257#ifndef SQLITE_OMIT_SUBQUERY
124258 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
124259 ** then there should be a single item on the stack. Write this
124260 ** item into the set table with bogus data.
124261 */
124262 case SRT_Set: {
124263 if( pSort ){
124264 /* At first glance you would think we could optimize out the
124265 ** ORDER BY in this case since the order of entries in the set
124266 ** does not matter. But there might be a LIMIT clause, in which
124267 ** case the order does matter */
124268 pushOntoSorter(
124269 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
124270 }else{
124271 int r1 = sqlite3GetTempReg(pParse);
124272 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
124273 sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol,
124274 r1, pDest->zAffSdst, nResultCol);
124275 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
124276 sqlite3ReleaseTempReg(pParse, r1);
124277 }
124278 break;
124279 }
124280
124281 /* If any row exist in the result set, record that fact and abort.
124282 */
124283 case SRT_Exists: {
124284 sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
124285 /* The LIMIT clause will terminate the loop for us */
124286 break;
124287 }
124288
124289 /* If this is a scalar select that is part of an expression, then
124290 ** store the results in the appropriate memory cell or array of
124291 ** memory cells and break out of the scan loop.
124292 */
124293 case SRT_Mem: {
124294 if( pSort ){
124295 assert( nResultCol<=pDest->nSdst );
124296 pushOntoSorter(
124297 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
124298 }else{
124299 assert( nResultCol==pDest->nSdst );
124300 assert( regResult==iParm );
124301 /* The LIMIT clause will jump out of the loop for us */
124302 }
124303 break;
124304 }
124305#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
124306
124307 case SRT_Coroutine: /* Send data to a co-routine */
124308 case SRT_Output: { /* Return the results */
124309 testcase( eDest==SRT_Coroutine );
124310 testcase( eDest==SRT_Output );
124311 if( pSort ){
124312 pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol,
124313 nPrefixReg);
124314 }else if( eDest==SRT_Coroutine ){
124315 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
124316 }else{
124317 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
124318 }
124319 break;
124320 }
124321
124322#ifndef SQLITE_OMIT_CTE
124323 /* Write the results into a priority queue that is order according to
124324 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
124325 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
124326 ** pSO->nExpr columns, then make sure all keys are unique by adding a
124327 ** final OP_Sequence column. The last column is the record as a blob.
124328 */
124329 case SRT_DistQueue:
124330 case SRT_Queue: {
124331 int nKey;
124332 int r1, r2, r3;
124333 int addrTest = 0;
124334 ExprList *pSO;
124335 pSO = pDest->pOrderBy;
124336 assert( pSO );
124337 nKey = pSO->nExpr;
124338 r1 = sqlite3GetTempReg(pParse);
124339 r2 = sqlite3GetTempRange(pParse, nKey+2);
124340 r3 = r2+nKey+1;
124341 if( eDest==SRT_DistQueue ){
124342 /* If the destination is DistQueue, then cursor (iParm+1) is open
124343 ** on a second ephemeral index that holds all values every previously
124344 ** added to the queue. */
124345 addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0,
124346 regResult, nResultCol);
124347 VdbeCoverage(v);
124348 }
124349 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
124350 if( eDest==SRT_DistQueue ){
124351 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
124352 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
124353 }
124354 for(i=0; i<nKey; i++){
124355 sqlite3VdbeAddOp2(v, OP_SCopy,
124356 regResult + pSO->a[i].u.x.iOrderByCol - 1,
124357 r2+i);
124358 }
124359 sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
124360 sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
124361 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2);
124362 if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
124363 sqlite3ReleaseTempReg(pParse, r1);
124364 sqlite3ReleaseTempRange(pParse, r2, nKey+2);
124365 break;
124366 }
124367#endif /* SQLITE_OMIT_CTE */
124368
124369
124370
124371#if !defined(SQLITE_OMIT_TRIGGER)
124372 /* Discard the results. This is used for SELECT statements inside
124373 ** the body of a TRIGGER. The purpose of such selects is to call
124374 ** user-defined functions that have side effects. We do not care
124375 ** about the actual results of the select.
124376 */
124377 default: {
124378 assert( eDest==SRT_Discard );
124379 break;
124380 }
124381#endif
124382 }
124383
124384 /* Jump to the end of the loop if the LIMIT is reached. Except, if
124385 ** there is a sorter, in which case the sorter has already limited
124386 ** the output for us.
124387 */
124388 if( pSort==0 && p->iLimit ){
124389 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
124390 }
124391}
124392
124393/*
124394** Allocate a KeyInfo object sufficient for an index of N key columns and
124395** X extra columns.
124396*/
124397SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
124398 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
124399 KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
124400 if( p ){
124401 p->aSortOrder = (u8*)&p->aColl[N+X];
124402 p->nKeyField = (u16)N;
124403 p->nAllField = (u16)(N+X);
124404 p->enc = ENC(db);
124405 p->db = db;
124406 p->nRef = 1;
124407 memset(&p[1], 0, nExtra);
124408 }else{
124409 sqlite3OomFault(db);
124410 }
124411 return p;
124412}
124413
124414/*
124415** Deallocate a KeyInfo object
124416*/
124417SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
124418 if( p ){
124419 assert( p->nRef>0 );
124420 p->nRef--;
124421 if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
124422 }
124423}
124424
124425/*
124426** Make a new pointer to a KeyInfo object
124427*/
124428SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
124429 if( p ){
124430 assert( p->nRef>0 );
124431 p->nRef++;
124432 }
124433 return p;
124434}
124435
124436#ifdef SQLITE_DEBUG
124437/*
124438** Return TRUE if a KeyInfo object can be change. The KeyInfo object
124439** can only be changed if this is just a single reference to the object.
124440**
124441** This routine is used only inside of assert() statements.
124442*/
124443SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
124444#endif /* SQLITE_DEBUG */
124445
124446/*
124447** Given an expression list, generate a KeyInfo structure that records
124448** the collating sequence for each expression in that expression list.
124449**
124450** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
124451** KeyInfo structure is appropriate for initializing a virtual index to
124452** implement that clause. If the ExprList is the result set of a SELECT
124453** then the KeyInfo structure is appropriate for initializing a virtual
124454** index to implement a DISTINCT test.
124455**
124456** Space to hold the KeyInfo structure is obtained from malloc. The calling
124457** function is responsible for seeing that this structure is eventually
124458** freed.
124459*/
124460SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(
124461 Parse *pParse, /* Parsing context */
124462 ExprList *pList, /* Form the KeyInfo object from this ExprList */
124463 int iStart, /* Begin with this column of pList */
124464 int nExtra /* Add this many extra columns to the end */
124465){
124466 int nExpr;
124467 KeyInfo *pInfo;
124468 struct ExprList_item *pItem;
124469 sqlite3 *db = pParse->db;
124470 int i;
124471
124472 nExpr = pList->nExpr;
124473 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
124474 if( pInfo ){
124475 assert( sqlite3KeyInfoIsWriteable(pInfo) );
124476 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
124477 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
124478 pInfo->aSortOrder[i-iStart] = pItem->sortOrder;
124479 }
124480 }
124481 return pInfo;
124482}
124483
124484/*
124485** Name of the connection operator, used for error messages.
124486*/
124487static const char *selectOpName(int id){
124488 char *z;
124489 switch( id ){
124490 case TK_ALL: z = "UNION ALL"; break;
124491 case TK_INTERSECT: z = "INTERSECT"; break;
124492 case TK_EXCEPT: z = "EXCEPT"; break;
124493 default: z = "UNION"; break;
124494 }
124495 return z;
124496}
124497
124498#ifndef SQLITE_OMIT_EXPLAIN
124499/*
124500** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
124501** is a no-op. Otherwise, it adds a single row of output to the EQP result,
124502** where the caption is of the form:
124503**
124504** "USE TEMP B-TREE FOR xxx"
124505**
124506** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
124507** is determined by the zUsage argument.
124508*/
124509static void explainTempTable(Parse *pParse, const char *zUsage){
124510 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
124511}
124512
124513/*
124514** Assign expression b to lvalue a. A second, no-op, version of this macro
124515** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
124516** in sqlite3Select() to assign values to structure member variables that
124517** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
124518** code with #ifndef directives.
124519*/
124520# define explainSetInteger(a, b) a = b
124521
124522#else
124523/* No-op versions of the explainXXX() functions and macros. */
124524# define explainTempTable(y,z)
124525# define explainSetInteger(y,z)
124526#endif
124527
124528
124529/*
124530** If the inner loop was generated using a non-null pOrderBy argument,
124531** then the results were placed in a sorter. After the loop is terminated
124532** we need to run the sorter and output the results. The following
124533** routine generates the code needed to do that.
124534*/
124535static void generateSortTail(
124536 Parse *pParse, /* Parsing context */
124537 Select *p, /* The SELECT statement */
124538 SortCtx *pSort, /* Information on the ORDER BY clause */
124539 int nColumn, /* Number of columns of data */
124540 SelectDest *pDest /* Write the sorted results here */
124541){
124542 Vdbe *v = pParse->pVdbe; /* The prepared statement */
124543 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
124544 int addrContinue = sqlite3VdbeMakeLabel(v); /* Jump here for next cycle */
124545 int addr; /* Top of output loop. Jump for Next. */
124546 int addrOnce = 0;
124547 int iTab;
124548 ExprList *pOrderBy = pSort->pOrderBy;
124549 int eDest = pDest->eDest;
124550 int iParm = pDest->iSDParm;
124551 int regRow;
124552 int regRowid;
124553 int iCol;
124554 int nKey; /* Number of key columns in sorter record */
124555 int iSortTab; /* Sorter cursor to read from */
124556 int i;
124557 int bSeq; /* True if sorter record includes seq. no. */
124558 int nRefKey = 0;
124559 struct ExprList_item *aOutEx = p->pEList->a;
124560
124561 assert( addrBreak<0 );
124562 if( pSort->labelBkOut ){
124563 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
124564 sqlite3VdbeGoto(v, addrBreak);
124565 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
124566 }
124567
124568#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124569 /* Open any cursors needed for sorter-reference expressions */
124570 for(i=0; i<pSort->nDefer; i++){
124571 Table *pTab = pSort->aDefer[i].pTab;
124572 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
124573 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
124574 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
124575 }
124576#endif
124577
124578 iTab = pSort->iECursor;
124579 if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
124580 regRowid = 0;
124581 regRow = pDest->iSdst;
124582 }else{
124583 regRowid = sqlite3GetTempReg(pParse);
124584 regRow = sqlite3GetTempRange(pParse, nColumn);
124585 }
124586 nKey = pOrderBy->nExpr - pSort->nOBSat;
124587 if( pSort->sortFlags & SORTFLAG_UseSorter ){
124588 int regSortOut = ++pParse->nMem;
124589 iSortTab = pParse->nTab++;
124590 if( pSort->labelBkOut ){
124591 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
124592 }
124593 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut,
124594 nKey+1+nColumn+nRefKey);
124595 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
124596 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
124597 VdbeCoverage(v);
124598 codeOffset(v, p->iOffset, addrContinue);
124599 sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
124600 bSeq = 0;
124601 }else{
124602 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
124603 codeOffset(v, p->iOffset, addrContinue);
124604 iSortTab = iTab;
124605 bSeq = 1;
124606 }
124607 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
124608#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124609 if( aOutEx[i].bSorterRef ) continue;
124610#endif
124611 if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
124612 }
124613#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124614 if( pSort->nDefer ){
124615 int iKey = iCol+1;
124616 int regKey = sqlite3GetTempRange(pParse, nRefKey);
124617
124618 for(i=0; i<pSort->nDefer; i++){
124619 int iCsr = pSort->aDefer[i].iCsr;
124620 Table *pTab = pSort->aDefer[i].pTab;
124621 int nKey = pSort->aDefer[i].nKey;
124622
124623 sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
124624 if( HasRowid(pTab) ){
124625 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey);
124626 sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr,
124627 sqlite3VdbeCurrentAddr(v)+1, regKey);
124628 }else{
124629 int k;
124630 int iJmp;
124631 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
124632 for(k=0; k<nKey; k++){
124633 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey+k);
124634 }
124635 iJmp = sqlite3VdbeCurrentAddr(v);
124636 sqlite3VdbeAddOp4Int(v, OP_SeekGE, iCsr, iJmp+2, regKey, nKey);
124637 sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp+3, regKey, nKey);
124638 sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
124639 }
124640 }
124641 sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
124642 }
124643#endif
124644 for(i=nColumn-1; i>=0; i--){
124645#ifdef SQLITE_ENABLE_SORTER_REFERENCES
124646 if( aOutEx[i].bSorterRef ){
124647 sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
124648 }else
124649#endif
124650 {
124651 int iRead;
124652 if( aOutEx[i].u.x.iOrderByCol ){
124653 iRead = aOutEx[i].u.x.iOrderByCol-1;
124654 }else{
124655 iRead = iCol--;
124656 }
124657 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
124658 VdbeComment((v, "%s", aOutEx[i].zName?aOutEx[i].zName : aOutEx[i].zSpan));
124659 }
124660 }
124661 switch( eDest ){
124662 case SRT_Table:
124663 case SRT_EphemTab: {
124664 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
124665 sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
124666 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
124667 break;
124668 }
124669#ifndef SQLITE_OMIT_SUBQUERY
124670 case SRT_Set: {
124671 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
124672 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid,
124673 pDest->zAffSdst, nColumn);
124674 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn);
124675 break;
124676 }
124677 case SRT_Mem: {
124678 /* The LIMIT clause will terminate the loop for us */
124679 break;
124680 }
124681#endif
124682 default: {
124683 assert( eDest==SRT_Output || eDest==SRT_Coroutine );
124684 testcase( eDest==SRT_Output );
124685 testcase( eDest==SRT_Coroutine );
124686 if( eDest==SRT_Output ){
124687 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
124688 }else{
124689 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
124690 }
124691 break;
124692 }
124693 }
124694 if( regRowid ){
124695 if( eDest==SRT_Set ){
124696 sqlite3ReleaseTempRange(pParse, regRow, nColumn);
124697 }else{
124698 sqlite3ReleaseTempReg(pParse, regRow);
124699 }
124700 sqlite3ReleaseTempReg(pParse, regRowid);
124701 }
124702 /* The bottom of the loop
124703 */
124704 sqlite3VdbeResolveLabel(v, addrContinue);
124705 if( pSort->sortFlags & SORTFLAG_UseSorter ){
124706 sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
124707 }else{
124708 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
124709 }
124710 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
124711 sqlite3VdbeResolveLabel(v, addrBreak);
124712}
124713
124714/*
124715** Return a pointer to a string containing the 'declaration type' of the
124716** expression pExpr. The string may be treated as static by the caller.
124717**
124718** Also try to estimate the size of the returned value and return that
124719** result in *pEstWidth.
124720**
124721** The declaration type is the exact datatype definition extracted from the
124722** original CREATE TABLE statement if the expression is a column. The
124723** declaration type for a ROWID field is INTEGER. Exactly when an expression
124724** is considered a column can be complex in the presence of subqueries. The
124725** result-set expression in all of the following SELECT statements is
124726** considered a column by this function.
124727**
124728** SELECT col FROM tbl;
124729** SELECT (SELECT col FROM tbl;
124730** SELECT (SELECT col FROM tbl);
124731** SELECT abc FROM (SELECT col AS abc FROM tbl);
124732**
124733** The declaration type for any expression other than a column is NULL.
124734**
124735** This routine has either 3 or 6 parameters depending on whether or not
124736** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
124737*/
124738#ifdef SQLITE_ENABLE_COLUMN_METADATA
124739# define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
124740#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
124741# define columnType(A,B,C,D,E) columnTypeImpl(A,B)
124742#endif
124743static const char *columnTypeImpl(
124744 NameContext *pNC,
124745#ifndef SQLITE_ENABLE_COLUMN_METADATA
124746 Expr *pExpr
124747#else
124748 Expr *pExpr,
124749 const char **pzOrigDb,
124750 const char **pzOrigTab,
124751 const char **pzOrigCol
124752#endif
124753){
124754 char const *zType = 0;
124755 int j;
124756#ifdef SQLITE_ENABLE_COLUMN_METADATA
124757 char const *zOrigDb = 0;
124758 char const *zOrigTab = 0;
124759 char const *zOrigCol = 0;
124760#endif
124761
124762 assert( pExpr!=0 );
124763 assert( pNC->pSrcList!=0 );
124764 assert( pExpr->op!=TK_AGG_COLUMN ); /* This routine runes before aggregates
124765 ** are processed */
124766 switch( pExpr->op ){
124767 case TK_COLUMN: {
124768 /* The expression is a column. Locate the table the column is being
124769 ** extracted from in NameContext.pSrcList. This table may be real
124770 ** database table or a subquery.
124771 */
124772 Table *pTab = 0; /* Table structure column is extracted from */
124773 Select *pS = 0; /* Select the column is extracted from */
124774 int iCol = pExpr->iColumn; /* Index of column in pTab */
124775 while( pNC && !pTab ){
124776 SrcList *pTabList = pNC->pSrcList;
124777 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
124778 if( j<pTabList->nSrc ){
124779 pTab = pTabList->a[j].pTab;
124780 pS = pTabList->a[j].pSelect;
124781 }else{
124782 pNC = pNC->pNext;
124783 }
124784 }
124785
124786 if( pTab==0 ){
124787 /* At one time, code such as "SELECT new.x" within a trigger would
124788 ** cause this condition to run. Since then, we have restructured how
124789 ** trigger code is generated and so this condition is no longer
124790 ** possible. However, it can still be true for statements like
124791 ** the following:
124792 **
124793 ** CREATE TABLE t1(col INTEGER);
124794 ** SELECT (SELECT t1.col) FROM FROM t1;
124795 **
124796 ** when columnType() is called on the expression "t1.col" in the
124797 ** sub-select. In this case, set the column type to NULL, even
124798 ** though it should really be "INTEGER".
124799 **
124800 ** This is not a problem, as the column type of "t1.col" is never
124801 ** used. When columnType() is called on the expression
124802 ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
124803 ** branch below. */
124804 break;
124805 }
124806
124807 assert( pTab && pExpr->pTab==pTab );
124808 if( pS ){
124809 /* The "table" is actually a sub-select or a view in the FROM clause
124810 ** of the SELECT statement. Return the declaration type and origin
124811 ** data for the result-set column of the sub-select.
124812 */
124813 if( iCol>=0 && iCol<pS->pEList->nExpr ){
124814 /* If iCol is less than zero, then the expression requests the
124815 ** rowid of the sub-select or view. This expression is legal (see
124816 ** test case misc2.2.2) - it always evaluates to NULL.
124817 */
124818 NameContext sNC;
124819 Expr *p = pS->pEList->a[iCol].pExpr;
124820 sNC.pSrcList = pS->pSrc;
124821 sNC.pNext = pNC;
124822 sNC.pParse = pNC->pParse;
124823 zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol);
124824 }
124825 }else{
124826 /* A real table or a CTE table */
124827 assert( !pS );
124828#ifdef SQLITE_ENABLE_COLUMN_METADATA
124829 if( iCol<0 ) iCol = pTab->iPKey;
124830 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
124831 if( iCol<0 ){
124832 zType = "INTEGER";
124833 zOrigCol = "rowid";
124834 }else{
124835 zOrigCol = pTab->aCol[iCol].zName;
124836 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
124837 }
124838 zOrigTab = pTab->zName;
124839 if( pNC->pParse && pTab->pSchema ){
124840 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
124841 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
124842 }
124843#else
124844 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
124845 if( iCol<0 ){
124846 zType = "INTEGER";
124847 }else{
124848 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
124849 }
124850#endif
124851 }
124852 break;
124853 }
124854#ifndef SQLITE_OMIT_SUBQUERY
124855 case TK_SELECT: {
124856 /* The expression is a sub-select. Return the declaration type and
124857 ** origin info for the single column in the result set of the SELECT
124858 ** statement.
124859 */
124860 NameContext sNC;
124861 Select *pS = pExpr->x.pSelect;
124862 Expr *p = pS->pEList->a[0].pExpr;
124863 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
124864 sNC.pSrcList = pS->pSrc;
124865 sNC.pNext = pNC;
124866 sNC.pParse = pNC->pParse;
124867 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
124868 break;
124869 }
124870#endif
124871 }
124872
124873#ifdef SQLITE_ENABLE_COLUMN_METADATA
124874 if( pzOrigDb ){
124875 assert( pzOrigTab && pzOrigCol );
124876 *pzOrigDb = zOrigDb;
124877 *pzOrigTab = zOrigTab;
124878 *pzOrigCol = zOrigCol;
124879 }
124880#endif
124881 return zType;
124882}
124883
124884/*
124885** Generate code that will tell the VDBE the declaration types of columns
124886** in the result set.
124887*/
124888static void generateColumnTypes(
124889 Parse *pParse, /* Parser context */
124890 SrcList *pTabList, /* List of tables */
124891 ExprList *pEList /* Expressions defining the result set */
124892){
124893#ifndef SQLITE_OMIT_DECLTYPE
124894 Vdbe *v = pParse->pVdbe;
124895 int i;
124896 NameContext sNC;
124897 sNC.pSrcList = pTabList;
124898 sNC.pParse = pParse;
124899 sNC.pNext = 0;
124900 for(i=0; i<pEList->nExpr; i++){
124901 Expr *p = pEList->a[i].pExpr;
124902 const char *zType;
124903#ifdef SQLITE_ENABLE_COLUMN_METADATA
124904 const char *zOrigDb = 0;
124905 const char *zOrigTab = 0;
124906 const char *zOrigCol = 0;
124907 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
124908
124909 /* The vdbe must make its own copy of the column-type and other
124910 ** column specific strings, in case the schema is reset before this
124911 ** virtual machine is deleted.
124912 */
124913 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
124914 sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
124915 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
124916#else
124917 zType = columnType(&sNC, p, 0, 0, 0);
124918#endif
124919 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
124920 }
124921#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
124922}
124923
124924
124925/*
124926** Compute the column names for a SELECT statement.
124927**
124928** The only guarantee that SQLite makes about column names is that if the
124929** column has an AS clause assigning it a name, that will be the name used.
124930** That is the only documented guarantee. However, countless applications
124931** developed over the years have made baseless assumptions about column names
124932** and will break if those assumptions changes. Hence, use extreme caution
124933** when modifying this routine to avoid breaking legacy.
124934**
124935** See Also: sqlite3ColumnsFromExprList()
124936**
124937** The PRAGMA short_column_names and PRAGMA full_column_names settings are
124938** deprecated. The default setting is short=ON, full=OFF. 99.9% of all
124939** applications should operate this way. Nevertheless, we need to support the
124940** other modes for legacy:
124941**
124942** short=OFF, full=OFF: Column name is the text of the expression has it
124943** originally appears in the SELECT statement. In
124944** other words, the zSpan of the result expression.
124945**
124946** short=ON, full=OFF: (This is the default setting). If the result
124947** refers directly to a table column, then the
124948** result column name is just the table column
124949** name: COLUMN. Otherwise use zSpan.
124950**
124951** full=ON, short=ANY: If the result refers directly to a table column,
124952** then the result column name with the table name
124953** prefix, ex: TABLE.COLUMN. Otherwise use zSpan.
124954*/
124955static void generateColumnNames(
124956 Parse *pParse, /* Parser context */
124957 Select *pSelect /* Generate column names for this SELECT statement */
124958){
124959 Vdbe *v = pParse->pVdbe;
124960 int i;
124961 Table *pTab;
124962 SrcList *pTabList;
124963 ExprList *pEList;
124964 sqlite3 *db = pParse->db;
124965 int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */
124966 int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
124967
124968#ifndef SQLITE_OMIT_EXPLAIN
124969 /* If this is an EXPLAIN, skip this step */
124970 if( pParse->explain ){
124971 return;
124972 }
124973#endif
124974
124975 if( pParse->colNamesSet ) return;
124976 /* Column names are determined by the left-most term of a compound select */
124977 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
124978 SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
124979 pTabList = pSelect->pSrc;
124980 pEList = pSelect->pEList;
124981 assert( v!=0 );
124982 assert( pTabList!=0 );
124983 pParse->colNamesSet = 1;
124984 fullName = (db->flags & SQLITE_FullColNames)!=0;
124985 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
124986 sqlite3VdbeSetNumCols(v, pEList->nExpr);
124987 for(i=0; i<pEList->nExpr; i++){
124988 Expr *p = pEList->a[i].pExpr;
124989
124990 assert( p!=0 );
124991 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
124992 assert( p->op!=TK_COLUMN || p->pTab!=0 ); /* Covering idx not yet coded */
124993 if( pEList->a[i].zName ){
124994 /* An AS clause always takes first priority */
124995 char *zName = pEList->a[i].zName;
124996 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
124997 }else if( srcName && p->op==TK_COLUMN ){
124998 char *zCol;
124999 int iCol = p->iColumn;
125000 pTab = p->pTab;
125001 assert( pTab!=0 );
125002 if( iCol<0 ) iCol = pTab->iPKey;
125003 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
125004 if( iCol<0 ){
125005 zCol = "rowid";
125006 }else{
125007 zCol = pTab->aCol[iCol].zName;
125008 }
125009 if( fullName ){
125010 char *zName = 0;
125011 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
125012 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
125013 }else{
125014 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
125015 }
125016 }else{
125017 const char *z = pEList->a[i].zSpan;
125018 z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
125019 sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
125020 }
125021 }
125022 generateColumnTypes(pParse, pTabList, pEList);
125023}
125024
125025/*
125026** Given an expression list (which is really the list of expressions
125027** that form the result set of a SELECT statement) compute appropriate
125028** column names for a table that would hold the expression list.
125029**
125030** All column names will be unique.
125031**
125032** Only the column names are computed. Column.zType, Column.zColl,
125033** and other fields of Column are zeroed.
125034**
125035** Return SQLITE_OK on success. If a memory allocation error occurs,
125036** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
125037**
125038** The only guarantee that SQLite makes about column names is that if the
125039** column has an AS clause assigning it a name, that will be the name used.
125040** That is the only documented guarantee. However, countless applications
125041** developed over the years have made baseless assumptions about column names
125042** and will break if those assumptions changes. Hence, use extreme caution
125043** when modifying this routine to avoid breaking legacy.
125044**
125045** See Also: generateColumnNames()
125046*/
125047SQLITE_PRIVATE int sqlite3ColumnsFromExprList(
125048 Parse *pParse, /* Parsing context */
125049 ExprList *pEList, /* Expr list from which to derive column names */
125050 i16 *pnCol, /* Write the number of columns here */
125051 Column **paCol /* Write the new column list here */
125052){
125053 sqlite3 *db = pParse->db; /* Database connection */
125054 int i, j; /* Loop counters */
125055 u32 cnt; /* Index added to make the name unique */
125056 Column *aCol, *pCol; /* For looping over result columns */
125057 int nCol; /* Number of columns in the result set */
125058 char *zName; /* Column name */
125059 int nName; /* Size of name in zName[] */
125060 Hash ht; /* Hash table of column names */
125061
125062 sqlite3HashInit(&ht);
125063 if( pEList ){
125064 nCol = pEList->nExpr;
125065 aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
125066 testcase( aCol==0 );
125067 if( nCol>32767 ) nCol = 32767;
125068 }else{
125069 nCol = 0;
125070 aCol = 0;
125071 }
125072 assert( nCol==(i16)nCol );
125073 *pnCol = nCol;
125074 *paCol = aCol;
125075
125076 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
125077 /* Get an appropriate name for the column
125078 */
125079 if( (zName = pEList->a[i].zName)!=0 ){
125080 /* If the column contains an "AS <name>" phrase, use <name> as the name */
125081 }else{
125082 Expr *pColExpr = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
125083 while( pColExpr->op==TK_DOT ){
125084 pColExpr = pColExpr->pRight;
125085 assert( pColExpr!=0 );
125086 }
125087 assert( pColExpr->op!=TK_AGG_COLUMN );
125088 if( pColExpr->op==TK_COLUMN ){
125089 /* For columns use the column name name */
125090 int iCol = pColExpr->iColumn;
125091 Table *pTab = pColExpr->pTab;
125092 assert( pTab!=0 );
125093 if( iCol<0 ) iCol = pTab->iPKey;
125094 zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
125095 }else if( pColExpr->op==TK_ID ){
125096 assert( !ExprHasProperty(pColExpr, EP_IntValue) );
125097 zName = pColExpr->u.zToken;
125098 }else{
125099 /* Use the original text of the column expression as its name */
125100 zName = pEList->a[i].zSpan;
125101 }
125102 }
125103 if( zName ){
125104 zName = sqlite3DbStrDup(db, zName);
125105 }else{
125106 zName = sqlite3MPrintf(db,"column%d",i+1);
125107 }
125108
125109 /* Make sure the column name is unique. If the name is not unique,
125110 ** append an integer to the name so that it becomes unique.
125111 */
125112 cnt = 0;
125113 while( zName && sqlite3HashFind(&ht, zName)!=0 ){
125114 nName = sqlite3Strlen30(zName);
125115 if( nName>0 ){
125116 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
125117 if( zName[j]==':' ) nName = j;
125118 }
125119 zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
125120 if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
125121 }
125122 pCol->zName = zName;
125123 sqlite3ColumnPropertiesFromName(0, pCol);
125124 if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
125125 sqlite3OomFault(db);
125126 }
125127 }
125128 sqlite3HashClear(&ht);
125129 if( db->mallocFailed ){
125130 for(j=0; j<i; j++){
125131 sqlite3DbFree(db, aCol[j].zName);
125132 }
125133 sqlite3DbFree(db, aCol);
125134 *paCol = 0;
125135 *pnCol = 0;
125136 return SQLITE_NOMEM_BKPT;
125137 }
125138 return SQLITE_OK;
125139}
125140
125141/*
125142** Add type and collation information to a column list based on
125143** a SELECT statement.
125144**
125145** The column list presumably came from selectColumnNamesFromExprList().
125146** The column list has only names, not types or collations. This
125147** routine goes through and adds the types and collations.
125148**
125149** This routine requires that all identifiers in the SELECT
125150** statement be resolved.
125151*/
125152SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(
125153 Parse *pParse, /* Parsing contexts */
125154 Table *pTab, /* Add column type information to this table */
125155 Select *pSelect /* SELECT used to determine types and collations */
125156){
125157 sqlite3 *db = pParse->db;
125158 NameContext sNC;
125159 Column *pCol;
125160 CollSeq *pColl;
125161 int i;
125162 Expr *p;
125163 struct ExprList_item *a;
125164
125165 assert( pSelect!=0 );
125166 assert( (pSelect->selFlags & SF_Resolved)!=0 );
125167 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
125168 if( db->mallocFailed ) return;
125169 memset(&sNC, 0, sizeof(sNC));
125170 sNC.pSrcList = pSelect->pSrc;
125171 a = pSelect->pEList->a;
125172 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
125173 const char *zType;
125174 int n, m;
125175 p = a[i].pExpr;
125176 zType = columnType(&sNC, p, 0, 0, 0);
125177 /* pCol->szEst = ... // Column size est for SELECT tables never used */
125178 pCol->affinity = sqlite3ExprAffinity(p);
125179 if( zType ){
125180 m = sqlite3Strlen30(zType);
125181 n = sqlite3Strlen30(pCol->zName);
125182 pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
125183 if( pCol->zName ){
125184 memcpy(&pCol->zName[n+1], zType, m+1);
125185 pCol->colFlags |= COLFLAG_HASTYPE;
125186 }
125187 }
125188 if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
125189 pColl = sqlite3ExprCollSeq(pParse, p);
125190 if( pColl && pCol->zColl==0 ){
125191 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
125192 }
125193 }
125194 pTab->szTabRow = 1; /* Any non-zero value works */
125195}
125196
125197/*
125198** Given a SELECT statement, generate a Table structure that describes
125199** the result set of that SELECT.
125200*/
125201SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
125202 Table *pTab;
125203 sqlite3 *db = pParse->db;
125204 int savedFlags;
125205
125206 savedFlags = db->flags;
125207 db->flags &= ~SQLITE_FullColNames;
125208 db->flags |= SQLITE_ShortColNames;
125209 sqlite3SelectPrep(pParse, pSelect, 0);
125210 if( pParse->nErr ) return 0;
125211 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
125212 db->flags = savedFlags;
125213 pTab = sqlite3DbMallocZero(db, sizeof(Table) );
125214 if( pTab==0 ){
125215 return 0;
125216 }
125217 /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
125218 ** is disabled */
125219 assert( db->lookaside.bDisable );
125220 pTab->nTabRef = 1;
125221 pTab->zName = 0;
125222 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
125223 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
125224 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect);
125225 pTab->iPKey = -1;
125226 if( db->mallocFailed ){
125227 sqlite3DeleteTable(db, pTab);
125228 return 0;
125229 }
125230 return pTab;
125231}
125232
125233/*
125234** Get a VDBE for the given parser context. Create a new one if necessary.
125235** If an error occurs, return NULL and leave a message in pParse.
125236*/
125237SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
125238 if( pParse->pVdbe ){
125239 return pParse->pVdbe;
125240 }
125241 if( pParse->pToplevel==0
125242 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
125243 ){
125244 pParse->okConstFactor = 1;
125245 }
125246 return sqlite3VdbeCreate(pParse);
125247}
125248
125249
125250/*
125251** Compute the iLimit and iOffset fields of the SELECT based on the
125252** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
125253** that appear in the original SQL statement after the LIMIT and OFFSET
125254** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
125255** are the integer memory register numbers for counters used to compute
125256** the limit and offset. If there is no limit and/or offset, then
125257** iLimit and iOffset are negative.
125258**
125259** This routine changes the values of iLimit and iOffset only if
125260** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
125261** and iOffset should have been preset to appropriate default values (zero)
125262** prior to calling this routine.
125263**
125264** The iOffset register (if it exists) is initialized to the value
125265** of the OFFSET. The iLimit register is initialized to LIMIT. Register
125266** iOffset+1 is initialized to LIMIT+OFFSET.
125267**
125268** Only if pLimit->pLeft!=0 do the limit registers get
125269** redefined. The UNION ALL operator uses this property to force
125270** the reuse of the same limit and offset registers across multiple
125271** SELECT statements.
125272*/
125273static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
125274 Vdbe *v = 0;
125275 int iLimit = 0;
125276 int iOffset;
125277 int n;
125278 Expr *pLimit = p->pLimit;
125279
125280 if( p->iLimit ) return;
125281
125282 /*
125283 ** "LIMIT -1" always shows all rows. There is some
125284 ** controversy about what the correct behavior should be.
125285 ** The current implementation interprets "LIMIT 0" to mean
125286 ** no rows.
125287 */
125288 if( pLimit ){
125289 assert( pLimit->op==TK_LIMIT );
125290 assert( pLimit->pLeft!=0 );
125291 p->iLimit = iLimit = ++pParse->nMem;
125292 v = sqlite3GetVdbe(pParse);
125293 assert( v!=0 );
125294 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
125295 sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
125296 VdbeComment((v, "LIMIT counter"));
125297 if( n==0 ){
125298 sqlite3VdbeGoto(v, iBreak);
125299 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
125300 p->nSelectRow = sqlite3LogEst((u64)n);
125301 p->selFlags |= SF_FixedLimit;
125302 }
125303 }else{
125304 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
125305 sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
125306 VdbeComment((v, "LIMIT counter"));
125307 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v);
125308 }
125309 if( pLimit->pRight ){
125310 p->iOffset = iOffset = ++pParse->nMem;
125311 pParse->nMem++; /* Allocate an extra register for limit+offset */
125312 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
125313 sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
125314 VdbeComment((v, "OFFSET counter"));
125315 sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset);
125316 VdbeComment((v, "LIMIT+OFFSET"));
125317 }
125318 }
125319}
125320
125321#ifndef SQLITE_OMIT_COMPOUND_SELECT
125322/*
125323** Return the appropriate collating sequence for the iCol-th column of
125324** the result set for the compound-select statement "p". Return NULL if
125325** the column has no default collating sequence.
125326**
125327** The collating sequence for the compound select is taken from the
125328** left-most term of the select that has a collating sequence.
125329*/
125330static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
125331 CollSeq *pRet;
125332 if( p->pPrior ){
125333 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
125334 }else{
125335 pRet = 0;
125336 }
125337 assert( iCol>=0 );
125338 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
125339 ** have been thrown during name resolution and we would not have gotten
125340 ** this far */
125341 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
125342 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
125343 }
125344 return pRet;
125345}
125346
125347/*
125348** The select statement passed as the second parameter is a compound SELECT
125349** with an ORDER BY clause. This function allocates and returns a KeyInfo
125350** structure suitable for implementing the ORDER BY.
125351**
125352** Space to hold the KeyInfo structure is obtained from malloc. The calling
125353** function is responsible for ensuring that this structure is eventually
125354** freed.
125355*/
125356static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
125357 ExprList *pOrderBy = p->pOrderBy;
125358 int nOrderBy = p->pOrderBy->nExpr;
125359 sqlite3 *db = pParse->db;
125360 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
125361 if( pRet ){
125362 int i;
125363 for(i=0; i<nOrderBy; i++){
125364 struct ExprList_item *pItem = &pOrderBy->a[i];
125365 Expr *pTerm = pItem->pExpr;
125366 CollSeq *pColl;
125367
125368 if( pTerm->flags & EP_Collate ){
125369 pColl = sqlite3ExprCollSeq(pParse, pTerm);
125370 }else{
125371 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
125372 if( pColl==0 ) pColl = db->pDfltColl;
125373 pOrderBy->a[i].pExpr =
125374 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
125375 }
125376 assert( sqlite3KeyInfoIsWriteable(pRet) );
125377 pRet->aColl[i] = pColl;
125378 pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder;
125379 }
125380 }
125381
125382 return pRet;
125383}
125384
125385#ifndef SQLITE_OMIT_CTE
125386/*
125387** This routine generates VDBE code to compute the content of a WITH RECURSIVE
125388** query of the form:
125389**
125390** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
125391** \___________/ \_______________/
125392** p->pPrior p
125393**
125394**
125395** There is exactly one reference to the recursive-table in the FROM clause
125396** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
125397**
125398** The setup-query runs once to generate an initial set of rows that go
125399** into a Queue table. Rows are extracted from the Queue table one by
125400** one. Each row extracted from Queue is output to pDest. Then the single
125401** extracted row (now in the iCurrent table) becomes the content of the
125402** recursive-table for a recursive-query run. The output of the recursive-query
125403** is added back into the Queue table. Then another row is extracted from Queue
125404** and the iteration continues until the Queue table is empty.
125405**
125406** If the compound query operator is UNION then no duplicate rows are ever
125407** inserted into the Queue table. The iDistinct table keeps a copy of all rows
125408** that have ever been inserted into Queue and causes duplicates to be
125409** discarded. If the operator is UNION ALL, then duplicates are allowed.
125410**
125411** If the query has an ORDER BY, then entries in the Queue table are kept in
125412** ORDER BY order and the first entry is extracted for each cycle. Without
125413** an ORDER BY, the Queue table is just a FIFO.
125414**
125415** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
125416** have been output to pDest. A LIMIT of zero means to output no rows and a
125417** negative LIMIT means to output all rows. If there is also an OFFSET clause
125418** with a positive value, then the first OFFSET outputs are discarded rather
125419** than being sent to pDest. The LIMIT count does not begin until after OFFSET
125420** rows have been skipped.
125421*/
125422static void generateWithRecursiveQuery(
125423 Parse *pParse, /* Parsing context */
125424 Select *p, /* The recursive SELECT to be coded */
125425 SelectDest *pDest /* What to do with query results */
125426){
125427 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
125428 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
125429 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
125430 Select *pSetup = p->pPrior; /* The setup query */
125431 int addrTop; /* Top of the loop */
125432 int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
125433 int iCurrent = 0; /* The Current table */
125434 int regCurrent; /* Register holding Current table */
125435 int iQueue; /* The Queue table */
125436 int iDistinct = 0; /* To ensure unique results if UNION */
125437 int eDest = SRT_Fifo; /* How to write to Queue */
125438 SelectDest destQueue; /* SelectDest targetting the Queue table */
125439 int i; /* Loop counter */
125440 int rc; /* Result code */
125441 ExprList *pOrderBy; /* The ORDER BY clause */
125442 Expr *pLimit; /* Saved LIMIT and OFFSET */
125443 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
125444
125445 /* Obtain authorization to do a recursive query */
125446 if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
125447
125448 /* Process the LIMIT and OFFSET clauses, if they exist */
125449 addrBreak = sqlite3VdbeMakeLabel(v);
125450 p->nSelectRow = 320; /* 4 billion rows */
125451 computeLimitRegisters(pParse, p, addrBreak);
125452 pLimit = p->pLimit;
125453 regLimit = p->iLimit;
125454 regOffset = p->iOffset;
125455 p->pLimit = 0;
125456 p->iLimit = p->iOffset = 0;
125457 pOrderBy = p->pOrderBy;
125458
125459 /* Locate the cursor number of the Current table */
125460 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
125461 if( pSrc->a[i].fg.isRecursive ){
125462 iCurrent = pSrc->a[i].iCursor;
125463 break;
125464 }
125465 }
125466
125467 /* Allocate cursors numbers for Queue and Distinct. The cursor number for
125468 ** the Distinct table must be exactly one greater than Queue in order
125469 ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
125470 iQueue = pParse->nTab++;
125471 if( p->op==TK_UNION ){
125472 eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo;
125473 iDistinct = pParse->nTab++;
125474 }else{
125475 eDest = pOrderBy ? SRT_Queue : SRT_Fifo;
125476 }
125477 sqlite3SelectDestInit(&destQueue, eDest, iQueue);
125478
125479 /* Allocate cursors for Current, Queue, and Distinct. */
125480 regCurrent = ++pParse->nMem;
125481 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
125482 if( pOrderBy ){
125483 KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
125484 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
125485 (char*)pKeyInfo, P4_KEYINFO);
125486 destQueue.pOrderBy = pOrderBy;
125487 }else{
125488 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
125489 }
125490 VdbeComment((v, "Queue table"));
125491 if( iDistinct ){
125492 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
125493 p->selFlags |= SF_UsesEphemeral;
125494 }
125495
125496 /* Detach the ORDER BY clause from the compound SELECT */
125497 p->pOrderBy = 0;
125498
125499 /* Store the results of the setup-query in Queue. */
125500 pSetup->pNext = 0;
125501 ExplainQueryPlan((pParse, 1, "SETUP"));
125502 rc = sqlite3Select(pParse, pSetup, &destQueue);
125503 pSetup->pNext = p;
125504 if( rc ) goto end_of_recursive_query;
125505
125506 /* Find the next row in the Queue and output that row */
125507 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
125508
125509 /* Transfer the next row in Queue over to Current */
125510 sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
125511 if( pOrderBy ){
125512 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
125513 }else{
125514 sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
125515 }
125516 sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
125517
125518 /* Output the single row in Current */
125519 addrCont = sqlite3VdbeMakeLabel(v);
125520 codeOffset(v, regOffset, addrCont);
125521 selectInnerLoop(pParse, p, iCurrent,
125522 0, 0, pDest, addrCont, addrBreak);
125523 if( regLimit ){
125524 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak);
125525 VdbeCoverage(v);
125526 }
125527 sqlite3VdbeResolveLabel(v, addrCont);
125528
125529 /* Execute the recursive SELECT taking the single row in Current as
125530 ** the value for the recursive-table. Store the results in the Queue.
125531 */
125532 if( p->selFlags & SF_Aggregate ){
125533 sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
125534 }else{
125535 p->pPrior = 0;
125536 ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"));
125537 sqlite3Select(pParse, p, &destQueue);
125538 assert( p->pPrior==0 );
125539 p->pPrior = pSetup;
125540 }
125541
125542 /* Keep running the loop until the Queue is empty */
125543 sqlite3VdbeGoto(v, addrTop);
125544 sqlite3VdbeResolveLabel(v, addrBreak);
125545
125546end_of_recursive_query:
125547 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
125548 p->pOrderBy = pOrderBy;
125549 p->pLimit = pLimit;
125550 return;
125551}
125552#endif /* SQLITE_OMIT_CTE */
125553
125554/* Forward references */
125555static int multiSelectOrderBy(
125556 Parse *pParse, /* Parsing context */
125557 Select *p, /* The right-most of SELECTs to be coded */
125558 SelectDest *pDest /* What to do with query results */
125559);
125560
125561/*
125562** Handle the special case of a compound-select that originates from a
125563** VALUES clause. By handling this as a special case, we avoid deep
125564** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
125565** on a VALUES clause.
125566**
125567** Because the Select object originates from a VALUES clause:
125568** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
125569** (2) All terms are UNION ALL
125570** (3) There is no ORDER BY clause
125571**
125572** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
125573** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
125574** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
125575** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
125576*/
125577static int multiSelectValues(
125578 Parse *pParse, /* Parsing context */
125579 Select *p, /* The right-most of SELECTs to be coded */
125580 SelectDest *pDest /* What to do with query results */
125581){
125582 int nRow = 1;
125583 int rc = 0;
125584 int bShowAll = p->pLimit==0;
125585 assert( p->selFlags & SF_MultiValue );
125586 do{
125587 assert( p->selFlags & SF_Values );
125588 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
125589 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
125590 if( p->pPrior==0 ) break;
125591 assert( p->pPrior->pNext==p );
125592 p = p->pPrior;
125593 nRow += bShowAll;
125594 }while(1);
125595 ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,
125596 nRow==1 ? "" : "S"));
125597 while( p ){
125598 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
125599 if( !bShowAll ) break;
125600 p->nSelectRow = nRow;
125601 p = p->pNext;
125602 }
125603 return rc;
125604}
125605
125606/*
125607** This routine is called to process a compound query form from
125608** two or more separate queries using UNION, UNION ALL, EXCEPT, or
125609** INTERSECT
125610**
125611** "p" points to the right-most of the two queries. the query on the
125612** left is p->pPrior. The left query could also be a compound query
125613** in which case this routine will be called recursively.
125614**
125615** The results of the total query are to be written into a destination
125616** of type eDest with parameter iParm.
125617**
125618** Example 1: Consider a three-way compound SQL statement.
125619**
125620** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
125621**
125622** This statement is parsed up as follows:
125623**
125624** SELECT c FROM t3
125625** |
125626** `-----> SELECT b FROM t2
125627** |
125628** `------> SELECT a FROM t1
125629**
125630** The arrows in the diagram above represent the Select.pPrior pointer.
125631** So if this routine is called with p equal to the t3 query, then
125632** pPrior will be the t2 query. p->op will be TK_UNION in this case.
125633**
125634** Notice that because of the way SQLite parses compound SELECTs, the
125635** individual selects always group from left to right.
125636*/
125637static int multiSelect(
125638 Parse *pParse, /* Parsing context */
125639 Select *p, /* The right-most of SELECTs to be coded */
125640 SelectDest *pDest /* What to do with query results */
125641){
125642 int rc = SQLITE_OK; /* Success code from a subroutine */
125643 Select *pPrior; /* Another SELECT immediately to our left */
125644 Vdbe *v; /* Generate code to this VDBE */
125645 SelectDest dest; /* Alternative data destination */
125646 Select *pDelete = 0; /* Chain of simple selects to delete */
125647 sqlite3 *db; /* Database connection */
125648
125649 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
125650 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
125651 */
125652 assert( p && p->pPrior ); /* Calling function guarantees this much */
125653 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
125654 db = pParse->db;
125655 pPrior = p->pPrior;
125656 dest = *pDest;
125657 if( pPrior->pOrderBy || pPrior->pLimit ){
125658 sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
125659 pPrior->pOrderBy!=0 ? "ORDER BY" : "LIMIT", selectOpName(p->op));
125660 rc = 1;
125661 goto multi_select_end;
125662 }
125663
125664 v = sqlite3GetVdbe(pParse);
125665 assert( v!=0 ); /* The VDBE already created by calling function */
125666
125667 /* Create the destination temporary table if necessary
125668 */
125669 if( dest.eDest==SRT_EphemTab ){
125670 assert( p->pEList );
125671 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
125672 dest.eDest = SRT_Table;
125673 }
125674
125675 /* Special handling for a compound-select that originates as a VALUES clause.
125676 */
125677 if( p->selFlags & SF_MultiValue ){
125678 rc = multiSelectValues(pParse, p, &dest);
125679 goto multi_select_end;
125680 }
125681
125682 /* Make sure all SELECTs in the statement have the same number of elements
125683 ** in their result sets.
125684 */
125685 assert( p->pEList && pPrior->pEList );
125686 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
125687
125688#ifndef SQLITE_OMIT_CTE
125689 if( p->selFlags & SF_Recursive ){
125690 generateWithRecursiveQuery(pParse, p, &dest);
125691 }else
125692#endif
125693
125694 /* Compound SELECTs that have an ORDER BY clause are handled separately.
125695 */
125696 if( p->pOrderBy ){
125697 return multiSelectOrderBy(pParse, p, pDest);
125698 }else{
125699
125700#ifndef SQLITE_OMIT_EXPLAIN
125701 if( pPrior->pPrior==0 ){
125702 ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
125703 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
125704 }
125705#endif
125706
125707 /* Generate code for the left and right SELECT statements.
125708 */
125709 switch( p->op ){
125710 case TK_ALL: {
125711 int addr = 0;
125712 int nLimit;
125713 assert( !pPrior->pLimit );
125714 pPrior->iLimit = p->iLimit;
125715 pPrior->iOffset = p->iOffset;
125716 pPrior->pLimit = p->pLimit;
125717 rc = sqlite3Select(pParse, pPrior, &dest);
125718 p->pLimit = 0;
125719 if( rc ){
125720 goto multi_select_end;
125721 }
125722 p->pPrior = 0;
125723 p->iLimit = pPrior->iLimit;
125724 p->iOffset = pPrior->iOffset;
125725 if( p->iLimit ){
125726 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
125727 VdbeComment((v, "Jump ahead if LIMIT reached"));
125728 if( p->iOffset ){
125729 sqlite3VdbeAddOp3(v, OP_OffsetLimit,
125730 p->iLimit, p->iOffset+1, p->iOffset);
125731 }
125732 }
125733 ExplainQueryPlan((pParse, 1, "UNION ALL"));
125734 rc = sqlite3Select(pParse, p, &dest);
125735 testcase( rc!=SQLITE_OK );
125736 pDelete = p->pPrior;
125737 p->pPrior = pPrior;
125738 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
125739 if( pPrior->pLimit
125740 && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
125741 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
125742 ){
125743 p->nSelectRow = sqlite3LogEst((u64)nLimit);
125744 }
125745 if( addr ){
125746 sqlite3VdbeJumpHere(v, addr);
125747 }
125748 break;
125749 }
125750 case TK_EXCEPT:
125751 case TK_UNION: {
125752 int unionTab; /* Cursor number of the temp table holding result */
125753 u8 op = 0; /* One of the SRT_ operations to apply to self */
125754 int priorOp; /* The SRT_ operation to apply to prior selects */
125755 Expr *pLimit; /* Saved values of p->nLimit */
125756 int addr;
125757 SelectDest uniondest;
125758
125759 testcase( p->op==TK_EXCEPT );
125760 testcase( p->op==TK_UNION );
125761 priorOp = SRT_Union;
125762 if( dest.eDest==priorOp ){
125763 /* We can reuse a temporary table generated by a SELECT to our
125764 ** right.
125765 */
125766 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
125767 unionTab = dest.iSDParm;
125768 }else{
125769 /* We will need to create our own temporary table to hold the
125770 ** intermediate results.
125771 */
125772 unionTab = pParse->nTab++;
125773 assert( p->pOrderBy==0 );
125774 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
125775 assert( p->addrOpenEphm[0] == -1 );
125776 p->addrOpenEphm[0] = addr;
125777 findRightmost(p)->selFlags |= SF_UsesEphemeral;
125778 assert( p->pEList );
125779 }
125780
125781 /* Code the SELECT statements to our left
125782 */
125783 assert( !pPrior->pOrderBy );
125784 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
125785 rc = sqlite3Select(pParse, pPrior, &uniondest);
125786 if( rc ){
125787 goto multi_select_end;
125788 }
125789
125790 /* Code the current SELECT statement
125791 */
125792 if( p->op==TK_EXCEPT ){
125793 op = SRT_Except;
125794 }else{
125795 assert( p->op==TK_UNION );
125796 op = SRT_Union;
125797 }
125798 p->pPrior = 0;
125799 pLimit = p->pLimit;
125800 p->pLimit = 0;
125801 uniondest.eDest = op;
125802 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
125803 selectOpName(p->op)));
125804 rc = sqlite3Select(pParse, p, &uniondest);
125805 testcase( rc!=SQLITE_OK );
125806 /* Query flattening in sqlite3Select() might refill p->pOrderBy.
125807 ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
125808 sqlite3ExprListDelete(db, p->pOrderBy);
125809 pDelete = p->pPrior;
125810 p->pPrior = pPrior;
125811 p->pOrderBy = 0;
125812 if( p->op==TK_UNION ){
125813 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
125814 }
125815 sqlite3ExprDelete(db, p->pLimit);
125816 p->pLimit = pLimit;
125817 p->iLimit = 0;
125818 p->iOffset = 0;
125819
125820 /* Convert the data in the temporary table into whatever form
125821 ** it is that we currently need.
125822 */
125823 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
125824 if( dest.eDest!=priorOp ){
125825 int iCont, iBreak, iStart;
125826 assert( p->pEList );
125827 iBreak = sqlite3VdbeMakeLabel(v);
125828 iCont = sqlite3VdbeMakeLabel(v);
125829 computeLimitRegisters(pParse, p, iBreak);
125830 sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
125831 iStart = sqlite3VdbeCurrentAddr(v);
125832 selectInnerLoop(pParse, p, unionTab,
125833 0, 0, &dest, iCont, iBreak);
125834 sqlite3VdbeResolveLabel(v, iCont);
125835 sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
125836 sqlite3VdbeResolveLabel(v, iBreak);
125837 sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
125838 }
125839 break;
125840 }
125841 default: assert( p->op==TK_INTERSECT ); {
125842 int tab1, tab2;
125843 int iCont, iBreak, iStart;
125844 Expr *pLimit;
125845 int addr;
125846 SelectDest intersectdest;
125847 int r1;
125848
125849 /* INTERSECT is different from the others since it requires
125850 ** two temporary tables. Hence it has its own case. Begin
125851 ** by allocating the tables we will need.
125852 */
125853 tab1 = pParse->nTab++;
125854 tab2 = pParse->nTab++;
125855 assert( p->pOrderBy==0 );
125856
125857 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
125858 assert( p->addrOpenEphm[0] == -1 );
125859 p->addrOpenEphm[0] = addr;
125860 findRightmost(p)->selFlags |= SF_UsesEphemeral;
125861 assert( p->pEList );
125862
125863 /* Code the SELECTs to our left into temporary table "tab1".
125864 */
125865 sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
125866 rc = sqlite3Select(pParse, pPrior, &intersectdest);
125867 if( rc ){
125868 goto multi_select_end;
125869 }
125870
125871 /* Code the current SELECT into temporary table "tab2"
125872 */
125873 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
125874 assert( p->addrOpenEphm[1] == -1 );
125875 p->addrOpenEphm[1] = addr;
125876 p->pPrior = 0;
125877 pLimit = p->pLimit;
125878 p->pLimit = 0;
125879 intersectdest.iSDParm = tab2;
125880 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
125881 selectOpName(p->op)));
125882 rc = sqlite3Select(pParse, p, &intersectdest);
125883 testcase( rc!=SQLITE_OK );
125884 pDelete = p->pPrior;
125885 p->pPrior = pPrior;
125886 if( p->nSelectRow>pPrior->nSelectRow ){
125887 p->nSelectRow = pPrior->nSelectRow;
125888 }
125889 sqlite3ExprDelete(db, p->pLimit);
125890 p->pLimit = pLimit;
125891
125892 /* Generate code to take the intersection of the two temporary
125893 ** tables.
125894 */
125895 assert( p->pEList );
125896 iBreak = sqlite3VdbeMakeLabel(v);
125897 iCont = sqlite3VdbeMakeLabel(v);
125898 computeLimitRegisters(pParse, p, iBreak);
125899 sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
125900 r1 = sqlite3GetTempReg(pParse);
125901 iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
125902 sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
125903 VdbeCoverage(v);
125904 sqlite3ReleaseTempReg(pParse, r1);
125905 selectInnerLoop(pParse, p, tab1,
125906 0, 0, &dest, iCont, iBreak);
125907 sqlite3VdbeResolveLabel(v, iCont);
125908 sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
125909 sqlite3VdbeResolveLabel(v, iBreak);
125910 sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
125911 sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
125912 break;
125913 }
125914 }
125915
125916 #ifndef SQLITE_OMIT_EXPLAIN
125917 if( p->pNext==0 ){
125918 ExplainQueryPlanPop(pParse);
125919 }
125920 #endif
125921 }
125922
125923 /* Compute collating sequences used by
125924 ** temporary tables needed to implement the compound select.
125925 ** Attach the KeyInfo structure to all temporary tables.
125926 **
125927 ** This section is run by the right-most SELECT statement only.
125928 ** SELECT statements to the left always skip this part. The right-most
125929 ** SELECT might also skip this part if it has no ORDER BY clause and
125930 ** no temp tables are required.
125931 */
125932 if( p->selFlags & SF_UsesEphemeral ){
125933 int i; /* Loop counter */
125934 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
125935 Select *pLoop; /* For looping through SELECT statements */
125936 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
125937 int nCol; /* Number of columns in result set */
125938
125939 assert( p->pNext==0 );
125940 nCol = p->pEList->nExpr;
125941 pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
125942 if( !pKeyInfo ){
125943 rc = SQLITE_NOMEM_BKPT;
125944 goto multi_select_end;
125945 }
125946 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
125947 *apColl = multiSelectCollSeq(pParse, p, i);
125948 if( 0==*apColl ){
125949 *apColl = db->pDfltColl;
125950 }
125951 }
125952
125953 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
125954 for(i=0; i<2; i++){
125955 int addr = pLoop->addrOpenEphm[i];
125956 if( addr<0 ){
125957 /* If [0] is unused then [1] is also unused. So we can
125958 ** always safely abort as soon as the first unused slot is found */
125959 assert( pLoop->addrOpenEphm[1]<0 );
125960 break;
125961 }
125962 sqlite3VdbeChangeP2(v, addr, nCol);
125963 sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
125964 P4_KEYINFO);
125965 pLoop->addrOpenEphm[i] = -1;
125966 }
125967 }
125968 sqlite3KeyInfoUnref(pKeyInfo);
125969 }
125970
125971multi_select_end:
125972 pDest->iSdst = dest.iSdst;
125973 pDest->nSdst = dest.nSdst;
125974 sqlite3SelectDelete(db, pDelete);
125975 return rc;
125976}
125977#endif /* SQLITE_OMIT_COMPOUND_SELECT */
125978
125979/*
125980** Error message for when two or more terms of a compound select have different
125981** size result sets.
125982*/
125983SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
125984 if( p->selFlags & SF_Values ){
125985 sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
125986 }else{
125987 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
125988 " do not have the same number of result columns", selectOpName(p->op));
125989 }
125990}
125991
125992/*
125993** Code an output subroutine for a coroutine implementation of a
125994** SELECT statment.
125995**
125996** The data to be output is contained in pIn->iSdst. There are
125997** pIn->nSdst columns to be output. pDest is where the output should
125998** be sent.
125999**
126000** regReturn is the number of the register holding the subroutine
126001** return address.
126002**
126003** If regPrev>0 then it is the first register in a vector that
126004** records the previous output. mem[regPrev] is a flag that is false
126005** if there has been no previous output. If regPrev>0 then code is
126006** generated to suppress duplicates. pKeyInfo is used for comparing
126007** keys.
126008**
126009** If the LIMIT found in p->iLimit is reached, jump immediately to
126010** iBreak.
126011*/
126012static int generateOutputSubroutine(
126013 Parse *pParse, /* Parsing context */
126014 Select *p, /* The SELECT statement */
126015 SelectDest *pIn, /* Coroutine supplying data */
126016 SelectDest *pDest, /* Where to send the data */
126017 int regReturn, /* The return address register */
126018 int regPrev, /* Previous result register. No uniqueness if 0 */
126019 KeyInfo *pKeyInfo, /* For comparing with previous entry */
126020 int iBreak /* Jump here if we hit the LIMIT */
126021){
126022 Vdbe *v = pParse->pVdbe;
126023 int iContinue;
126024 int addr;
126025
126026 addr = sqlite3VdbeCurrentAddr(v);
126027 iContinue = sqlite3VdbeMakeLabel(v);
126028
126029 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
126030 */
126031 if( regPrev ){
126032 int addr1, addr2;
126033 addr1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
126034 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
126035 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
126036 sqlite3VdbeAddOp3(v, OP_Jump, addr2+2, iContinue, addr2+2); VdbeCoverage(v);
126037 sqlite3VdbeJumpHere(v, addr1);
126038 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
126039 sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
126040 }
126041 if( pParse->db->mallocFailed ) return 0;
126042
126043 /* Suppress the first OFFSET entries if there is an OFFSET clause
126044 */
126045 codeOffset(v, p->iOffset, iContinue);
126046
126047 assert( pDest->eDest!=SRT_Exists );
126048 assert( pDest->eDest!=SRT_Table );
126049 switch( pDest->eDest ){
126050 /* Store the result as data using a unique key.
126051 */
126052 case SRT_EphemTab: {
126053 int r1 = sqlite3GetTempReg(pParse);
126054 int r2 = sqlite3GetTempReg(pParse);
126055 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
126056 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
126057 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
126058 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
126059 sqlite3ReleaseTempReg(pParse, r2);
126060 sqlite3ReleaseTempReg(pParse, r1);
126061 break;
126062 }
126063
126064#ifndef SQLITE_OMIT_SUBQUERY
126065 /* If we are creating a set for an "expr IN (SELECT ...)".
126066 */
126067 case SRT_Set: {
126068 int r1;
126069 testcase( pIn->nSdst>1 );
126070 r1 = sqlite3GetTempReg(pParse);
126071 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
126072 r1, pDest->zAffSdst, pIn->nSdst);
126073 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
126074 pIn->iSdst, pIn->nSdst);
126075 sqlite3ReleaseTempReg(pParse, r1);
126076 break;
126077 }
126078
126079 /* If this is a scalar select that is part of an expression, then
126080 ** store the results in the appropriate memory cell and break out
126081 ** of the scan loop.
126082 */
126083 case SRT_Mem: {
126084 assert( pIn->nSdst==1 || pParse->nErr>0 ); testcase( pIn->nSdst!=1 );
126085 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
126086 /* The LIMIT clause will jump out of the loop for us */
126087 break;
126088 }
126089#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
126090
126091 /* The results are stored in a sequence of registers
126092 ** starting at pDest->iSdst. Then the co-routine yields.
126093 */
126094 case SRT_Coroutine: {
126095 if( pDest->iSdst==0 ){
126096 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
126097 pDest->nSdst = pIn->nSdst;
126098 }
126099 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
126100 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
126101 break;
126102 }
126103
126104 /* If none of the above, then the result destination must be
126105 ** SRT_Output. This routine is never called with any other
126106 ** destination other than the ones handled above or SRT_Output.
126107 **
126108 ** For SRT_Output, results are stored in a sequence of registers.
126109 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
126110 ** return the next row of result.
126111 */
126112 default: {
126113 assert( pDest->eDest==SRT_Output );
126114 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
126115 break;
126116 }
126117 }
126118
126119 /* Jump to the end of the loop if the LIMIT is reached.
126120 */
126121 if( p->iLimit ){
126122 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
126123 }
126124
126125 /* Generate the subroutine return
126126 */
126127 sqlite3VdbeResolveLabel(v, iContinue);
126128 sqlite3VdbeAddOp1(v, OP_Return, regReturn);
126129
126130 return addr;
126131}
126132
126133/*
126134** Alternative compound select code generator for cases when there
126135** is an ORDER BY clause.
126136**
126137** We assume a query of the following form:
126138**
126139** <selectA> <operator> <selectB> ORDER BY <orderbylist>
126140**
126141** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
126142** is to code both <selectA> and <selectB> with the ORDER BY clause as
126143** co-routines. Then run the co-routines in parallel and merge the results
126144** into the output. In addition to the two coroutines (called selectA and
126145** selectB) there are 7 subroutines:
126146**
126147** outA: Move the output of the selectA coroutine into the output
126148** of the compound query.
126149**
126150** outB: Move the output of the selectB coroutine into the output
126151** of the compound query. (Only generated for UNION and
126152** UNION ALL. EXCEPT and INSERTSECT never output a row that
126153** appears only in B.)
126154**
126155** AltB: Called when there is data from both coroutines and A<B.
126156**
126157** AeqB: Called when there is data from both coroutines and A==B.
126158**
126159** AgtB: Called when there is data from both coroutines and A>B.
126160**
126161** EofA: Called when data is exhausted from selectA.
126162**
126163** EofB: Called when data is exhausted from selectB.
126164**
126165** The implementation of the latter five subroutines depend on which
126166** <operator> is used:
126167**
126168**
126169** UNION ALL UNION EXCEPT INTERSECT
126170** ------------- ----------------- -------------- -----------------
126171** AltB: outA, nextA outA, nextA outA, nextA nextA
126172**
126173** AeqB: outA, nextA nextA nextA outA, nextA
126174**
126175** AgtB: outB, nextB outB, nextB nextB nextB
126176**
126177** EofA: outB, nextB outB, nextB halt halt
126178**
126179** EofB: outA, nextA outA, nextA outA, nextA halt
126180**
126181** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
126182** causes an immediate jump to EofA and an EOF on B following nextB causes
126183** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
126184** following nextX causes a jump to the end of the select processing.
126185**
126186** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
126187** within the output subroutine. The regPrev register set holds the previously
126188** output value. A comparison is made against this value and the output
126189** is skipped if the next results would be the same as the previous.
126190**
126191** The implementation plan is to implement the two coroutines and seven
126192** subroutines first, then put the control logic at the bottom. Like this:
126193**
126194** goto Init
126195** coA: coroutine for left query (A)
126196** coB: coroutine for right query (B)
126197** outA: output one row of A
126198** outB: output one row of B (UNION and UNION ALL only)
126199** EofA: ...
126200** EofB: ...
126201** AltB: ...
126202** AeqB: ...
126203** AgtB: ...
126204** Init: initialize coroutine registers
126205** yield coA
126206** if eof(A) goto EofA
126207** yield coB
126208** if eof(B) goto EofB
126209** Cmpr: Compare A, B
126210** Jump AltB, AeqB, AgtB
126211** End: ...
126212**
126213** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
126214** actually called using Gosub and they do not Return. EofA and EofB loop
126215** until all data is exhausted then jump to the "end" labe. AltB, AeqB,
126216** and AgtB jump to either L2 or to one of EofA or EofB.
126217*/
126218#ifndef SQLITE_OMIT_COMPOUND_SELECT
126219static int multiSelectOrderBy(
126220 Parse *pParse, /* Parsing context */
126221 Select *p, /* The right-most of SELECTs to be coded */
126222 SelectDest *pDest /* What to do with query results */
126223){
126224 int i, j; /* Loop counters */
126225 Select *pPrior; /* Another SELECT immediately to our left */
126226 Vdbe *v; /* Generate code to this VDBE */
126227 SelectDest destA; /* Destination for coroutine A */
126228 SelectDest destB; /* Destination for coroutine B */
126229 int regAddrA; /* Address register for select-A coroutine */
126230 int regAddrB; /* Address register for select-B coroutine */
126231 int addrSelectA; /* Address of the select-A coroutine */
126232 int addrSelectB; /* Address of the select-B coroutine */
126233 int regOutA; /* Address register for the output-A subroutine */
126234 int regOutB; /* Address register for the output-B subroutine */
126235 int addrOutA; /* Address of the output-A subroutine */
126236 int addrOutB = 0; /* Address of the output-B subroutine */
126237 int addrEofA; /* Address of the select-A-exhausted subroutine */
126238 int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
126239 int addrEofB; /* Address of the select-B-exhausted subroutine */
126240 int addrAltB; /* Address of the A<B subroutine */
126241 int addrAeqB; /* Address of the A==B subroutine */
126242 int addrAgtB; /* Address of the A>B subroutine */
126243 int regLimitA; /* Limit register for select-A */
126244 int regLimitB; /* Limit register for select-A */
126245 int regPrev; /* A range of registers to hold previous output */
126246 int savedLimit; /* Saved value of p->iLimit */
126247 int savedOffset; /* Saved value of p->iOffset */
126248 int labelCmpr; /* Label for the start of the merge algorithm */
126249 int labelEnd; /* Label for the end of the overall SELECT stmt */
126250 int addr1; /* Jump instructions that get retargetted */
126251 int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
126252 KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
126253 KeyInfo *pKeyMerge; /* Comparison information for merging rows */
126254 sqlite3 *db; /* Database connection */
126255 ExprList *pOrderBy; /* The ORDER BY clause */
126256 int nOrderBy; /* Number of terms in the ORDER BY clause */
126257 int *aPermute; /* Mapping from ORDER BY terms to result set columns */
126258
126259 assert( p->pOrderBy!=0 );
126260 assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */
126261 db = pParse->db;
126262 v = pParse->pVdbe;
126263 assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */
126264 labelEnd = sqlite3VdbeMakeLabel(v);
126265 labelCmpr = sqlite3VdbeMakeLabel(v);
126266
126267
126268 /* Patch up the ORDER BY clause
126269 */
126270 op = p->op;
126271 pPrior = p->pPrior;
126272 assert( pPrior->pOrderBy==0 );
126273 pOrderBy = p->pOrderBy;
126274 assert( pOrderBy );
126275 nOrderBy = pOrderBy->nExpr;
126276
126277 /* For operators other than UNION ALL we have to make sure that
126278 ** the ORDER BY clause covers every term of the result set. Add
126279 ** terms to the ORDER BY clause as necessary.
126280 */
126281 if( op!=TK_ALL ){
126282 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
126283 struct ExprList_item *pItem;
126284 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
126285 assert( pItem->u.x.iOrderByCol>0 );
126286 if( pItem->u.x.iOrderByCol==i ) break;
126287 }
126288 if( j==nOrderBy ){
126289 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
126290 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
126291 pNew->flags |= EP_IntValue;
126292 pNew->u.iValue = i;
126293 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
126294 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
126295 }
126296 }
126297 }
126298
126299 /* Compute the comparison permutation and keyinfo that is used with
126300 ** the permutation used to determine if the next
126301 ** row of results comes from selectA or selectB. Also add explicit
126302 ** collations to the ORDER BY clause terms so that when the subqueries
126303 ** to the right and the left are evaluated, they use the correct
126304 ** collation.
126305 */
126306 aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1));
126307 if( aPermute ){
126308 struct ExprList_item *pItem;
126309 aPermute[0] = nOrderBy;
126310 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
126311 assert( pItem->u.x.iOrderByCol>0 );
126312 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
126313 aPermute[i] = pItem->u.x.iOrderByCol - 1;
126314 }
126315 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
126316 }else{
126317 pKeyMerge = 0;
126318 }
126319
126320 /* Reattach the ORDER BY clause to the query.
126321 */
126322 p->pOrderBy = pOrderBy;
126323 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
126324
126325 /* Allocate a range of temporary registers and the KeyInfo needed
126326 ** for the logic that removes duplicate result rows when the
126327 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
126328 */
126329 if( op==TK_ALL ){
126330 regPrev = 0;
126331 }else{
126332 int nExpr = p->pEList->nExpr;
126333 assert( nOrderBy>=nExpr || db->mallocFailed );
126334 regPrev = pParse->nMem+1;
126335 pParse->nMem += nExpr+1;
126336 sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
126337 pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
126338 if( pKeyDup ){
126339 assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
126340 for(i=0; i<nExpr; i++){
126341 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
126342 pKeyDup->aSortOrder[i] = 0;
126343 }
126344 }
126345 }
126346
126347 /* Separate the left and the right query from one another
126348 */
126349 p->pPrior = 0;
126350 pPrior->pNext = 0;
126351 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
126352 if( pPrior->pPrior==0 ){
126353 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
126354 }
126355
126356 /* Compute the limit registers */
126357 computeLimitRegisters(pParse, p, labelEnd);
126358 if( p->iLimit && op==TK_ALL ){
126359 regLimitA = ++pParse->nMem;
126360 regLimitB = ++pParse->nMem;
126361 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
126362 regLimitA);
126363 sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
126364 }else{
126365 regLimitA = regLimitB = 0;
126366 }
126367 sqlite3ExprDelete(db, p->pLimit);
126368 p->pLimit = 0;
126369
126370 regAddrA = ++pParse->nMem;
126371 regAddrB = ++pParse->nMem;
126372 regOutA = ++pParse->nMem;
126373 regOutB = ++pParse->nMem;
126374 sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
126375 sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
126376
126377 ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op)));
126378
126379 /* Generate a coroutine to evaluate the SELECT statement to the
126380 ** left of the compound operator - the "A" select.
126381 */
126382 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
126383 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
126384 VdbeComment((v, "left SELECT"));
126385 pPrior->iLimit = regLimitA;
126386 ExplainQueryPlan((pParse, 1, "LEFT"));
126387 sqlite3Select(pParse, pPrior, &destA);
126388 sqlite3VdbeEndCoroutine(v, regAddrA);
126389 sqlite3VdbeJumpHere(v, addr1);
126390
126391 /* Generate a coroutine to evaluate the SELECT statement on
126392 ** the right - the "B" select
126393 */
126394 addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
126395 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
126396 VdbeComment((v, "right SELECT"));
126397 savedLimit = p->iLimit;
126398 savedOffset = p->iOffset;
126399 p->iLimit = regLimitB;
126400 p->iOffset = 0;
126401 ExplainQueryPlan((pParse, 1, "RIGHT"));
126402 sqlite3Select(pParse, p, &destB);
126403 p->iLimit = savedLimit;
126404 p->iOffset = savedOffset;
126405 sqlite3VdbeEndCoroutine(v, regAddrB);
126406
126407 /* Generate a subroutine that outputs the current row of the A
126408 ** select as the next output row of the compound select.
126409 */
126410 VdbeNoopComment((v, "Output routine for A"));
126411 addrOutA = generateOutputSubroutine(pParse,
126412 p, &destA, pDest, regOutA,
126413 regPrev, pKeyDup, labelEnd);
126414
126415 /* Generate a subroutine that outputs the current row of the B
126416 ** select as the next output row of the compound select.
126417 */
126418 if( op==TK_ALL || op==TK_UNION ){
126419 VdbeNoopComment((v, "Output routine for B"));
126420 addrOutB = generateOutputSubroutine(pParse,
126421 p, &destB, pDest, regOutB,
126422 regPrev, pKeyDup, labelEnd);
126423 }
126424 sqlite3KeyInfoUnref(pKeyDup);
126425
126426 /* Generate a subroutine to run when the results from select A
126427 ** are exhausted and only data in select B remains.
126428 */
126429 if( op==TK_EXCEPT || op==TK_INTERSECT ){
126430 addrEofA_noB = addrEofA = labelEnd;
126431 }else{
126432 VdbeNoopComment((v, "eof-A subroutine"));
126433 addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
126434 addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
126435 VdbeCoverage(v);
126436 sqlite3VdbeGoto(v, addrEofA);
126437 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
126438 }
126439
126440 /* Generate a subroutine to run when the results from select B
126441 ** are exhausted and only data in select A remains.
126442 */
126443 if( op==TK_INTERSECT ){
126444 addrEofB = addrEofA;
126445 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
126446 }else{
126447 VdbeNoopComment((v, "eof-B subroutine"));
126448 addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
126449 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
126450 sqlite3VdbeGoto(v, addrEofB);
126451 }
126452
126453 /* Generate code to handle the case of A<B
126454 */
126455 VdbeNoopComment((v, "A-lt-B subroutine"));
126456 addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
126457 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
126458 sqlite3VdbeGoto(v, labelCmpr);
126459
126460 /* Generate code to handle the case of A==B
126461 */
126462 if( op==TK_ALL ){
126463 addrAeqB = addrAltB;
126464 }else if( op==TK_INTERSECT ){
126465 addrAeqB = addrAltB;
126466 addrAltB++;
126467 }else{
126468 VdbeNoopComment((v, "A-eq-B subroutine"));
126469 addrAeqB =
126470 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
126471 sqlite3VdbeGoto(v, labelCmpr);
126472 }
126473
126474 /* Generate code to handle the case of A>B
126475 */
126476 VdbeNoopComment((v, "A-gt-B subroutine"));
126477 addrAgtB = sqlite3VdbeCurrentAddr(v);
126478 if( op==TK_ALL || op==TK_UNION ){
126479 sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
126480 }
126481 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
126482 sqlite3VdbeGoto(v, labelCmpr);
126483
126484 /* This code runs once to initialize everything.
126485 */
126486 sqlite3VdbeJumpHere(v, addr1);
126487 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
126488 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
126489
126490 /* Implement the main merge loop
126491 */
126492 sqlite3VdbeResolveLabel(v, labelCmpr);
126493 sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
126494 sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
126495 (char*)pKeyMerge, P4_KEYINFO);
126496 sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
126497 sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
126498
126499 /* Jump to the this point in order to terminate the query.
126500 */
126501 sqlite3VdbeResolveLabel(v, labelEnd);
126502
126503 /* Reassembly the compound query so that it will be freed correctly
126504 ** by the calling function */
126505 if( p->pPrior ){
126506 sqlite3SelectDelete(db, p->pPrior);
126507 }
126508 p->pPrior = pPrior;
126509 pPrior->pNext = p;
126510
126511 /*** TBD: Insert subroutine calls to close cursors on incomplete
126512 **** subqueries ****/
126513 ExplainQueryPlanPop(pParse);
126514 return pParse->nErr!=0;
126515}
126516#endif
126517
126518#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
126519
126520/* An instance of the SubstContext object describes an substitution edit
126521** to be performed on a parse tree.
126522**
126523** All references to columns in table iTable are to be replaced by corresponding
126524** expressions in pEList.
126525*/
126526typedef struct SubstContext {
126527 Parse *pParse; /* The parsing context */
126528 int iTable; /* Replace references to this table */
126529 int iNewTable; /* New table number */
126530 int isLeftJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
126531 ExprList *pEList; /* Replacement expressions */
126532} SubstContext;
126533
126534/* Forward Declarations */
126535static void substExprList(SubstContext*, ExprList*);
126536static void substSelect(SubstContext*, Select*, int);
126537
126538/*
126539** Scan through the expression pExpr. Replace every reference to
126540** a column in table number iTable with a copy of the iColumn-th
126541** entry in pEList. (But leave references to the ROWID column
126542** unchanged.)
126543**
126544** This routine is part of the flattening procedure. A subquery
126545** whose result set is defined by pEList appears as entry in the
126546** FROM clause of a SELECT such that the VDBE cursor assigned to that
126547** FORM clause entry is iTable. This routine makes the necessary
126548** changes to pExpr so that it refers directly to the source table
126549** of the subquery rather the result set of the subquery.
126550*/
126551static Expr *substExpr(
126552 SubstContext *pSubst, /* Description of the substitution */
126553 Expr *pExpr /* Expr in which substitution occurs */
126554){
126555 if( pExpr==0 ) return 0;
126556 if( ExprHasProperty(pExpr, EP_FromJoin)
126557 && pExpr->iRightJoinTable==pSubst->iTable
126558 ){
126559 pExpr->iRightJoinTable = pSubst->iNewTable;
126560 }
126561 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSubst->iTable ){
126562 if( pExpr->iColumn<0 ){
126563 pExpr->op = TK_NULL;
126564 }else{
126565 Expr *pNew;
126566 Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
126567 Expr ifNullRow;
126568 assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
126569 assert( pExpr->pRight==0 );
126570 if( sqlite3ExprIsVector(pCopy) ){
126571 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
126572 }else{
126573 sqlite3 *db = pSubst->pParse->db;
126574 if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){
126575 memset(&ifNullRow, 0, sizeof(ifNullRow));
126576 ifNullRow.op = TK_IF_NULL_ROW;
126577 ifNullRow.pLeft = pCopy;
126578 ifNullRow.iTable = pSubst->iNewTable;
126579 pCopy = &ifNullRow;
126580 }
126581 pNew = sqlite3ExprDup(db, pCopy, 0);
126582 if( pNew && pSubst->isLeftJoin ){
126583 ExprSetProperty(pNew, EP_CanBeNull);
126584 }
126585 if( pNew && ExprHasProperty(pExpr,EP_FromJoin) ){
126586 pNew->iRightJoinTable = pExpr->iRightJoinTable;
126587 ExprSetProperty(pNew, EP_FromJoin);
126588 }
126589 sqlite3ExprDelete(db, pExpr);
126590 pExpr = pNew;
126591 }
126592 }
126593 }else{
126594 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
126595 pExpr->iTable = pSubst->iNewTable;
126596 }
126597 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
126598 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
126599 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
126600 substSelect(pSubst, pExpr->x.pSelect, 1);
126601 }else{
126602 substExprList(pSubst, pExpr->x.pList);
126603 }
126604 }
126605 return pExpr;
126606}
126607static void substExprList(
126608 SubstContext *pSubst, /* Description of the substitution */
126609 ExprList *pList /* List to scan and in which to make substitutes */
126610){
126611 int i;
126612 if( pList==0 ) return;
126613 for(i=0; i<pList->nExpr; i++){
126614 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
126615 }
126616}
126617static void substSelect(
126618 SubstContext *pSubst, /* Description of the substitution */
126619 Select *p, /* SELECT statement in which to make substitutions */
126620 int doPrior /* Do substitutes on p->pPrior too */
126621){
126622 SrcList *pSrc;
126623 struct SrcList_item *pItem;
126624 int i;
126625 if( !p ) return;
126626 do{
126627 substExprList(pSubst, p->pEList);
126628 substExprList(pSubst, p->pGroupBy);
126629 substExprList(pSubst, p->pOrderBy);
126630 p->pHaving = substExpr(pSubst, p->pHaving);
126631 p->pWhere = substExpr(pSubst, p->pWhere);
126632 pSrc = p->pSrc;
126633 assert( pSrc!=0 );
126634 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
126635 substSelect(pSubst, pItem->pSelect, 1);
126636 if( pItem->fg.isTabFunc ){
126637 substExprList(pSubst, pItem->u1.pFuncArg);
126638 }
126639 }
126640 }while( doPrior && (p = p->pPrior)!=0 );
126641}
126642#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
126643
126644#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
126645/*
126646** This routine attempts to flatten subqueries as a performance optimization.
126647** This routine returns 1 if it makes changes and 0 if no flattening occurs.
126648**
126649** To understand the concept of flattening, consider the following
126650** query:
126651**
126652** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
126653**
126654** The default way of implementing this query is to execute the
126655** subquery first and store the results in a temporary table, then
126656** run the outer query on that temporary table. This requires two
126657** passes over the data. Furthermore, because the temporary table
126658** has no indices, the WHERE clause on the outer query cannot be
126659** optimized.
126660**
126661** This routine attempts to rewrite queries such as the above into
126662** a single flat select, like this:
126663**
126664** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
126665**
126666** The code generated for this simplification gives the same result
126667** but only has to scan the data once. And because indices might
126668** exist on the table t1, a complete scan of the data might be
126669** avoided.
126670**
126671** Flattening is subject to the following constraints:
126672**
126673** (**) We no longer attempt to flatten aggregate subqueries. Was:
126674** The subquery and the outer query cannot both be aggregates.
126675**
126676** (**) We no longer attempt to flatten aggregate subqueries. Was:
126677** (2) If the subquery is an aggregate then
126678** (2a) the outer query must not be a join and
126679** (2b) the outer query must not use subqueries
126680** other than the one FROM-clause subquery that is a candidate
126681** for flattening. (This is due to ticket [2f7170d73bf9abf80]
126682** from 2015-02-09.)
126683**
126684** (3) If the subquery is the right operand of a LEFT JOIN then
126685** (3a) the subquery may not be a join and
126686** (3b) the FROM clause of the subquery may not contain a virtual
126687** table and
126688** (3c) the outer query may not be an aggregate.
126689**
126690** (4) The subquery can not be DISTINCT.
126691**
126692** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
126693** sub-queries that were excluded from this optimization. Restriction
126694** (4) has since been expanded to exclude all DISTINCT subqueries.
126695**
126696** (**) We no longer attempt to flatten aggregate subqueries. Was:
126697** If the subquery is aggregate, the outer query may not be DISTINCT.
126698**
126699** (7) The subquery must have a FROM clause. TODO: For subqueries without
126700** A FROM clause, consider adding a FROM clause with the special
126701** table sqlite_once that consists of a single row containing a
126702** single NULL.
126703**
126704** (8) If the subquery uses LIMIT then the outer query may not be a join.
126705**
126706** (9) If the subquery uses LIMIT then the outer query may not be aggregate.
126707**
126708** (**) Restriction (10) was removed from the code on 2005-02-05 but we
126709** accidently carried the comment forward until 2014-09-15. Original
126710** constraint: "If the subquery is aggregate then the outer query
126711** may not use LIMIT."
126712**
126713** (11) The subquery and the outer query may not both have ORDER BY clauses.
126714**
126715** (**) Not implemented. Subsumed into restriction (3). Was previously
126716** a separate restriction deriving from ticket #350.
126717**
126718** (13) The subquery and outer query may not both use LIMIT.
126719**
126720** (14) The subquery may not use OFFSET.
126721**
126722** (15) If the outer query is part of a compound select, then the
126723** subquery may not use LIMIT.
126724** (See ticket #2339 and ticket [02a8e81d44]).
126725**
126726** (16) If the outer query is aggregate, then the subquery may not
126727** use ORDER BY. (Ticket #2942) This used to not matter
126728** until we introduced the group_concat() function.
126729**
126730** (17) If the subquery is a compound select, then
126731** (17a) all compound operators must be a UNION ALL, and
126732** (17b) no terms within the subquery compound may be aggregate
126733** or DISTINCT, and
126734** (17c) every term within the subquery compound must have a FROM clause
126735** (17d) the outer query may not be
126736** (17d1) aggregate, or
126737** (17d2) DISTINCT, or
126738** (17d3) a join.
126739**
126740** The parent and sub-query may contain WHERE clauses. Subject to
126741** rules (11), (13) and (14), they may also contain ORDER BY,
126742** LIMIT and OFFSET clauses. The subquery cannot use any compound
126743** operator other than UNION ALL because all the other compound
126744** operators have an implied DISTINCT which is disallowed by
126745** restriction (4).
126746**
126747** Also, each component of the sub-query must return the same number
126748** of result columns. This is actually a requirement for any compound
126749** SELECT statement, but all the code here does is make sure that no
126750** such (illegal) sub-query is flattened. The caller will detect the
126751** syntax error and return a detailed message.
126752**
126753** (18) If the sub-query is a compound select, then all terms of the
126754** ORDER BY clause of the parent must be simple references to
126755** columns of the sub-query.
126756**
126757** (19) If the subquery uses LIMIT then the outer query may not
126758** have a WHERE clause.
126759**
126760** (20) If the sub-query is a compound select, then it must not use
126761** an ORDER BY clause. Ticket #3773. We could relax this constraint
126762** somewhat by saying that the terms of the ORDER BY clause must
126763** appear as unmodified result columns in the outer query. But we
126764** have other optimizations in mind to deal with that case.
126765**
126766** (21) If the subquery uses LIMIT then the outer query may not be
126767** DISTINCT. (See ticket [752e1646fc]).
126768**
126769** (22) The subquery may not be a recursive CTE.
126770**
126771** (**) Subsumed into restriction (17d3). Was: If the outer query is
126772** a recursive CTE, then the sub-query may not be a compound query.
126773** This restriction is because transforming the
126774** parent to a compound query confuses the code that handles
126775** recursive queries in multiSelect().
126776**
126777** (**) We no longer attempt to flatten aggregate subqueries. Was:
126778** The subquery may not be an aggregate that uses the built-in min() or
126779** or max() functions. (Without this restriction, a query like:
126780** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
126781** return the value X for which Y was maximal.)
126782**
126783** (25) If either the subquery or the parent query contains a window
126784** function in the select list or ORDER BY clause, flattening
126785** is not attempted.
126786**
126787**
126788** In this routine, the "p" parameter is a pointer to the outer query.
126789** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
126790** uses aggregates.
126791**
126792** If flattening is not attempted, this routine is a no-op and returns 0.
126793** If flattening is attempted this routine returns 1.
126794**
126795** All of the expression analysis must occur on both the outer query and
126796** the subquery before this routine runs.
126797*/
126798static int flattenSubquery(
126799 Parse *pParse, /* Parsing context */
126800 Select *p, /* The parent or outer SELECT statement */
126801 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
126802 int isAgg /* True if outer SELECT uses aggregate functions */
126803){
126804 const char *zSavedAuthContext = pParse->zAuthContext;
126805 Select *pParent; /* Current UNION ALL term of the other query */
126806 Select *pSub; /* The inner query or "subquery" */
126807 Select *pSub1; /* Pointer to the rightmost select in sub-query */
126808 SrcList *pSrc; /* The FROM clause of the outer query */
126809 SrcList *pSubSrc; /* The FROM clause of the subquery */
126810 int iParent; /* VDBE cursor number of the pSub result set temp table */
126811 int iNewParent = -1;/* Replacement table for iParent */
126812 int isLeftJoin = 0; /* True if pSub is the right side of a LEFT JOIN */
126813 int i; /* Loop counter */
126814 Expr *pWhere; /* The WHERE clause */
126815 struct SrcList_item *pSubitem; /* The subquery */
126816 sqlite3 *db = pParse->db;
126817
126818 /* Check to see if flattening is permitted. Return 0 if not.
126819 */
126820 assert( p!=0 );
126821 assert( p->pPrior==0 );
126822 if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
126823 pSrc = p->pSrc;
126824 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
126825 pSubitem = &pSrc->a[iFrom];
126826 iParent = pSubitem->iCursor;
126827 pSub = pSubitem->pSelect;
126828 assert( pSub!=0 );
126829
126830#ifndef SQLITE_OMIT_WINDOWFUNC
126831 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
126832#endif
126833
126834 pSubSrc = pSub->pSrc;
126835 assert( pSubSrc );
126836 /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
126837 ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
126838 ** because they could be computed at compile-time. But when LIMIT and OFFSET
126839 ** became arbitrary expressions, we were forced to add restrictions (13)
126840 ** and (14). */
126841 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
126842 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
126843 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
126844 return 0; /* Restriction (15) */
126845 }
126846 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
126847 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
126848 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
126849 return 0; /* Restrictions (8)(9) */
126850 }
126851 if( p->pOrderBy && pSub->pOrderBy ){
126852 return 0; /* Restriction (11) */
126853 }
126854 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
126855 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
126856 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
126857 return 0; /* Restriction (21) */
126858 }
126859 if( pSub->selFlags & (SF_Recursive) ){
126860 return 0; /* Restrictions (22) */
126861 }
126862
126863 /*
126864 ** If the subquery is the right operand of a LEFT JOIN, then the
126865 ** subquery may not be a join itself (3a). Example of why this is not
126866 ** allowed:
126867 **
126868 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
126869 **
126870 ** If we flatten the above, we would get
126871 **
126872 ** (t1 LEFT OUTER JOIN t2) JOIN t3
126873 **
126874 ** which is not at all the same thing.
126875 **
126876 ** If the subquery is the right operand of a LEFT JOIN, then the outer
126877 ** query cannot be an aggregate. (3c) This is an artifact of the way
126878 ** aggregates are processed - there is no mechanism to determine if
126879 ** the LEFT JOIN table should be all-NULL.
126880 **
126881 ** See also tickets #306, #350, and #3300.
126882 */
126883 if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
126884 isLeftJoin = 1;
126885 if( pSubSrc->nSrc>1 || isAgg || IsVirtual(pSubSrc->a[0].pTab) ){
126886 /* (3a) (3c) (3b) */
126887 return 0;
126888 }
126889 }
126890#ifdef SQLITE_EXTRA_IFNULLROW
126891 else if( iFrom>0 && !isAgg ){
126892 /* Setting isLeftJoin to -1 causes OP_IfNullRow opcodes to be generated for
126893 ** every reference to any result column from subquery in a join, even
126894 ** though they are not necessary. This will stress-test the OP_IfNullRow
126895 ** opcode. */
126896 isLeftJoin = -1;
126897 }
126898#endif
126899
126900 /* Restriction (17): If the sub-query is a compound SELECT, then it must
126901 ** use only the UNION ALL operator. And none of the simple select queries
126902 ** that make up the compound SELECT are allowed to be aggregate or distinct
126903 ** queries.
126904 */
126905 if( pSub->pPrior ){
126906 if( pSub->pOrderBy ){
126907 return 0; /* Restriction (20) */
126908 }
126909 if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
126910 return 0; /* (17d1), (17d2), or (17d3) */
126911 }
126912 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
126913 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
126914 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
126915 assert( pSub->pSrc!=0 );
126916 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
126917 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
126918 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
126919 || pSub1->pSrc->nSrc<1 /* (17c) */
126920 ){
126921 return 0;
126922 }
126923 testcase( pSub1->pSrc->nSrc>1 );
126924 }
126925
126926 /* Restriction (18). */
126927 if( p->pOrderBy ){
126928 int ii;
126929 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
126930 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
126931 }
126932 }
126933 }
126934
126935 /* Ex-restriction (23):
126936 ** The only way that the recursive part of a CTE can contain a compound
126937 ** subquery is for the subquery to be one term of a join. But if the
126938 ** subquery is a join, then the flattening has already been stopped by
126939 ** restriction (17d3)
126940 */
126941 assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 );
126942
126943 /***** If we reach this point, flattening is permitted. *****/
126944 SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
126945 pSub->selId, pSub, iFrom));
126946
126947 /* Authorize the subquery */
126948 pParse->zAuthContext = pSubitem->zName;
126949 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
126950 testcase( i==SQLITE_DENY );
126951 pParse->zAuthContext = zSavedAuthContext;
126952
126953 /* If the sub-query is a compound SELECT statement, then (by restrictions
126954 ** 17 and 18 above) it must be a UNION ALL and the parent query must
126955 ** be of the form:
126956 **
126957 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
126958 **
126959 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
126960 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
126961 ** OFFSET clauses and joins them to the left-hand-side of the original
126962 ** using UNION ALL operators. In this case N is the number of simple
126963 ** select statements in the compound sub-query.
126964 **
126965 ** Example:
126966 **
126967 ** SELECT a+1 FROM (
126968 ** SELECT x FROM tab
126969 ** UNION ALL
126970 ** SELECT y FROM tab
126971 ** UNION ALL
126972 ** SELECT abs(z*2) FROM tab2
126973 ** ) WHERE a!=5 ORDER BY 1
126974 **
126975 ** Transformed into:
126976 **
126977 ** SELECT x+1 FROM tab WHERE x+1!=5
126978 ** UNION ALL
126979 ** SELECT y+1 FROM tab WHERE y+1!=5
126980 ** UNION ALL
126981 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
126982 ** ORDER BY 1
126983 **
126984 ** We call this the "compound-subquery flattening".
126985 */
126986 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
126987 Select *pNew;
126988 ExprList *pOrderBy = p->pOrderBy;
126989 Expr *pLimit = p->pLimit;
126990 Select *pPrior = p->pPrior;
126991 p->pOrderBy = 0;
126992 p->pSrc = 0;
126993 p->pPrior = 0;
126994 p->pLimit = 0;
126995 pNew = sqlite3SelectDup(db, p, 0);
126996 p->pLimit = pLimit;
126997 p->pOrderBy = pOrderBy;
126998 p->pSrc = pSrc;
126999 p->op = TK_ALL;
127000 if( pNew==0 ){
127001 p->pPrior = pPrior;
127002 }else{
127003 pNew->pPrior = pPrior;
127004 if( pPrior ) pPrior->pNext = pNew;
127005 pNew->pNext = p;
127006 p->pPrior = pNew;
127007 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
127008 " creates %u as peer\n",pNew->selId));
127009 }
127010 if( db->mallocFailed ) return 1;
127011 }
127012
127013 /* Begin flattening the iFrom-th entry of the FROM clause
127014 ** in the outer query.
127015 */
127016 pSub = pSub1 = pSubitem->pSelect;
127017
127018 /* Delete the transient table structure associated with the
127019 ** subquery
127020 */
127021 sqlite3DbFree(db, pSubitem->zDatabase);
127022 sqlite3DbFree(db, pSubitem->zName);
127023 sqlite3DbFree(db, pSubitem->zAlias);
127024 pSubitem->zDatabase = 0;
127025 pSubitem->zName = 0;
127026 pSubitem->zAlias = 0;
127027 pSubitem->pSelect = 0;
127028
127029 /* Defer deleting the Table object associated with the
127030 ** subquery until code generation is
127031 ** complete, since there may still exist Expr.pTab entries that
127032 ** refer to the subquery even after flattening. Ticket #3346.
127033 **
127034 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
127035 */
127036 if( ALWAYS(pSubitem->pTab!=0) ){
127037 Table *pTabToDel = pSubitem->pTab;
127038 if( pTabToDel->nTabRef==1 ){
127039 Parse *pToplevel = sqlite3ParseToplevel(pParse);
127040 pTabToDel->pNextZombie = pToplevel->pZombieTab;
127041 pToplevel->pZombieTab = pTabToDel;
127042 }else{
127043 pTabToDel->nTabRef--;
127044 }
127045 pSubitem->pTab = 0;
127046 }
127047
127048 /* The following loop runs once for each term in a compound-subquery
127049 ** flattening (as described above). If we are doing a different kind
127050 ** of flattening - a flattening other than a compound-subquery flattening -
127051 ** then this loop only runs once.
127052 **
127053 ** This loop moves all of the FROM elements of the subquery into the
127054 ** the FROM clause of the outer query. Before doing this, remember
127055 ** the cursor number for the original outer query FROM element in
127056 ** iParent. The iParent cursor will never be used. Subsequent code
127057 ** will scan expressions looking for iParent references and replace
127058 ** those references with expressions that resolve to the subquery FROM
127059 ** elements we are now copying in.
127060 */
127061 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
127062 int nSubSrc;
127063 u8 jointype = 0;
127064 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
127065 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
127066 pSrc = pParent->pSrc; /* FROM clause of the outer query */
127067
127068 if( pSrc ){
127069 assert( pParent==p ); /* First time through the loop */
127070 jointype = pSubitem->fg.jointype;
127071 }else{
127072 assert( pParent!=p ); /* 2nd and subsequent times through the loop */
127073 pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
127074 if( pSrc==0 ){
127075 assert( db->mallocFailed );
127076 break;
127077 }
127078 }
127079
127080 /* The subquery uses a single slot of the FROM clause of the outer
127081 ** query. If the subquery has more than one element in its FROM clause,
127082 ** then expand the outer query to make space for it to hold all elements
127083 ** of the subquery.
127084 **
127085 ** Example:
127086 **
127087 ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
127088 **
127089 ** The outer query has 3 slots in its FROM clause. One slot of the
127090 ** outer query (the middle slot) is used by the subquery. The next
127091 ** block of code will expand the outer query FROM clause to 4 slots.
127092 ** The middle slot is expanded to two slots in order to make space
127093 ** for the two elements in the FROM clause of the subquery.
127094 */
127095 if( nSubSrc>1 ){
127096 pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
127097 if( db->mallocFailed ){
127098 break;
127099 }
127100 }
127101
127102 /* Transfer the FROM clause terms from the subquery into the
127103 ** outer query.
127104 */
127105 for(i=0; i<nSubSrc; i++){
127106 sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
127107 assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
127108 pSrc->a[i+iFrom] = pSubSrc->a[i];
127109 iNewParent = pSubSrc->a[i].iCursor;
127110 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
127111 }
127112 pSrc->a[iFrom].fg.jointype = jointype;
127113
127114 /* Now begin substituting subquery result set expressions for
127115 ** references to the iParent in the outer query.
127116 **
127117 ** Example:
127118 **
127119 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
127120 ** \ \_____________ subquery __________/ /
127121 ** \_____________________ outer query ______________________________/
127122 **
127123 ** We look at every expression in the outer query and every place we see
127124 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
127125 */
127126 if( pSub->pOrderBy ){
127127 /* At this point, any non-zero iOrderByCol values indicate that the
127128 ** ORDER BY column expression is identical to the iOrderByCol'th
127129 ** expression returned by SELECT statement pSub. Since these values
127130 ** do not necessarily correspond to columns in SELECT statement pParent,
127131 ** zero them before transfering the ORDER BY clause.
127132 **
127133 ** Not doing this may cause an error if a subsequent call to this
127134 ** function attempts to flatten a compound sub-query into pParent
127135 ** (the only way this can happen is if the compound sub-query is
127136 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
127137 ExprList *pOrderBy = pSub->pOrderBy;
127138 for(i=0; i<pOrderBy->nExpr; i++){
127139 pOrderBy->a[i].u.x.iOrderByCol = 0;
127140 }
127141 assert( pParent->pOrderBy==0 );
127142 pParent->pOrderBy = pOrderBy;
127143 pSub->pOrderBy = 0;
127144 }
127145 pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
127146 if( isLeftJoin>0 ){
127147 setJoinExpr(pWhere, iNewParent);
127148 }
127149 pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere);
127150 if( db->mallocFailed==0 ){
127151 SubstContext x;
127152 x.pParse = pParse;
127153 x.iTable = iParent;
127154 x.iNewTable = iNewParent;
127155 x.isLeftJoin = isLeftJoin;
127156 x.pEList = pSub->pEList;
127157 substSelect(&x, pParent, 0);
127158 }
127159
127160 /* The flattened query is distinct if either the inner or the
127161 ** outer query is distinct.
127162 */
127163 pParent->selFlags |= pSub->selFlags & SF_Distinct;
127164
127165 /*
127166 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
127167 **
127168 ** One is tempted to try to add a and b to combine the limits. But this
127169 ** does not work if either limit is negative.
127170 */
127171 if( pSub->pLimit ){
127172 pParent->pLimit = pSub->pLimit;
127173 pSub->pLimit = 0;
127174 }
127175 }
127176
127177 /* Finially, delete what is left of the subquery and return
127178 ** success.
127179 */
127180 sqlite3SelectDelete(db, pSub1);
127181
127182#if SELECTTRACE_ENABLED
127183 if( sqlite3SelectTrace & 0x100 ){
127184 SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
127185 sqlite3TreeViewSelect(0, p, 0);
127186 }
127187#endif
127188
127189 return 1;
127190}
127191#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
127192
127193/*
127194** A structure to keep track of all of the column values that fixed to
127195** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
127196*/
127197typedef struct WhereConst WhereConst;
127198struct WhereConst {
127199 Parse *pParse; /* Parsing context */
127200 int nConst; /* Number for COLUMN=CONSTANT terms */
127201 int nChng; /* Number of times a constant is propagated */
127202 Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */
127203};
127204
127205/*
127206** Add a new entry to the pConst object
127207*/
127208static void constInsert(
127209 WhereConst *pConst,
127210 Expr *pColumn,
127211 Expr *pValue
127212){
127213
127214 pConst->nConst++;
127215 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
127216 pConst->nConst*2*sizeof(Expr*));
127217 if( pConst->apExpr==0 ){
127218 pConst->nConst = 0;
127219 }else{
127220 if( ExprHasProperty(pValue, EP_FixedCol) ) pValue = pValue->pLeft;
127221 pConst->apExpr[pConst->nConst*2-2] = pColumn;
127222 pConst->apExpr[pConst->nConst*2-1] = pValue;
127223 }
127224}
127225
127226/*
127227** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
127228** is a constant expression and where the term must be true because it
127229** is part of the AND-connected terms of the expression. For each term
127230** found, add it to the pConst structure.
127231*/
127232static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
127233 Expr *pRight, *pLeft;
127234 if( pExpr==0 ) return;
127235 if( ExprHasProperty(pExpr, EP_FromJoin) ) return;
127236 if( pExpr->op==TK_AND ){
127237 findConstInWhere(pConst, pExpr->pRight);
127238 findConstInWhere(pConst, pExpr->pLeft);
127239 return;
127240 }
127241 if( pExpr->op!=TK_EQ ) return;
127242 pRight = pExpr->pRight;
127243 pLeft = pExpr->pLeft;
127244 assert( pRight!=0 );
127245 assert( pLeft!=0 );
127246 if( pRight->op==TK_COLUMN
127247 && !ExprHasProperty(pRight, EP_FixedCol)
127248 && sqlite3ExprIsConstant(pLeft)
127249 && sqlite3IsBinary(sqlite3BinaryCompareCollSeq(pConst->pParse,pLeft,pRight))
127250 ){
127251 constInsert(pConst, pRight, pLeft);
127252 }else
127253 if( pLeft->op==TK_COLUMN
127254 && !ExprHasProperty(pLeft, EP_FixedCol)
127255 && sqlite3ExprIsConstant(pRight)
127256 && sqlite3IsBinary(sqlite3BinaryCompareCollSeq(pConst->pParse,pLeft,pRight))
127257 ){
127258 constInsert(pConst, pLeft, pRight);
127259 }
127260}
127261
127262/*
127263** This is a Walker expression callback. pExpr is a candidate expression
127264** to be replaced by a value. If pExpr is equivalent to one of the
127265** columns named in pWalker->u.pConst, then overwrite it with its
127266** corresponding value.
127267*/
127268static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){
127269 int i;
127270 WhereConst *pConst;
127271 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
127272 if( ExprHasProperty(pExpr, EP_FixedCol) ) return WRC_Continue;
127273 pConst = pWalker->u.pConst;
127274 for(i=0; i<pConst->nConst; i++){
127275 Expr *pColumn = pConst->apExpr[i*2];
127276 if( pColumn==pExpr ) continue;
127277 if( pColumn->iTable!=pExpr->iTable ) continue;
127278 if( pColumn->iColumn!=pExpr->iColumn ) continue;
127279 /* A match is found. Add the EP_FixedCol property */
127280 pConst->nChng++;
127281 ExprClearProperty(pExpr, EP_Leaf);
127282 ExprSetProperty(pExpr, EP_FixedCol);
127283 assert( pExpr->pLeft==0 );
127284 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
127285 break;
127286 }
127287 return WRC_Prune;
127288}
127289
127290/*
127291** The WHERE-clause constant propagation optimization.
127292**
127293** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
127294** CONSTANT=COLUMN that must be tree (in other words, if the terms top-level
127295** AND-connected terms that are not part of a ON clause from a LEFT JOIN)
127296** then throughout the query replace all other occurrences of COLUMN
127297** with CONSTANT within the WHERE clause.
127298**
127299** For example, the query:
127300**
127301** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
127302**
127303** Is transformed into
127304**
127305** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
127306**
127307** Return true if any transformations where made and false if not.
127308**
127309** Implementation note: Constant propagation is tricky due to affinity
127310** and collating sequence interactions. Consider this example:
127311**
127312** CREATE TABLE t1(a INT,b TEXT);
127313** INSERT INTO t1 VALUES(123,'0123');
127314** SELECT * FROM t1 WHERE a=123 AND b=a;
127315** SELECT * FROM t1 WHERE a=123 AND b=123;
127316**
127317** The two SELECT statements above should return different answers. b=a
127318** is alway true because the comparison uses numeric affinity, but b=123
127319** is false because it uses text affinity and '0123' is not the same as '123'.
127320** To work around this, the expression tree is not actually changed from
127321** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
127322** and the "123" value is hung off of the pLeft pointer. Code generator
127323** routines know to generate the constant "123" instead of looking up the
127324** column value. Also, to avoid collation problems, this optimization is
127325** only attempted if the "a=123" term uses the default BINARY collation.
127326*/
127327static int propagateConstants(
127328 Parse *pParse, /* The parsing context */
127329 Select *p /* The query in which to propagate constants */
127330){
127331 WhereConst x;
127332 Walker w;
127333 int nChng = 0;
127334 x.pParse = pParse;
127335 do{
127336 x.nConst = 0;
127337 x.nChng = 0;
127338 x.apExpr = 0;
127339 findConstInWhere(&x, p->pWhere);
127340 if( x.nConst ){
127341 memset(&w, 0, sizeof(w));
127342 w.pParse = pParse;
127343 w.xExprCallback = propagateConstantExprRewrite;
127344 w.xSelectCallback = sqlite3SelectWalkNoop;
127345 w.xSelectCallback2 = 0;
127346 w.walkerDepth = 0;
127347 w.u.pConst = &x;
127348 sqlite3WalkExpr(&w, p->pWhere);
127349 sqlite3DbFree(x.pParse->db, x.apExpr);
127350 nChng += x.nChng;
127351 }
127352 }while( x.nChng );
127353 return nChng;
127354}
127355
127356#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
127357/*
127358** Make copies of relevant WHERE clause terms of the outer query into
127359** the WHERE clause of subquery. Example:
127360**
127361** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
127362**
127363** Transformed into:
127364**
127365** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
127366** WHERE x=5 AND y=10;
127367**
127368** The hope is that the terms added to the inner query will make it more
127369** efficient.
127370**
127371** Do not attempt this optimization if:
127372**
127373** (1) (** This restriction was removed on 2017-09-29. We used to
127374** disallow this optimization for aggregate subqueries, but now
127375** it is allowed by putting the extra terms on the HAVING clause.
127376** The added HAVING clause is pointless if the subquery lacks
127377** a GROUP BY clause. But such a HAVING clause is also harmless
127378** so there does not appear to be any reason to add extra logic
127379** to suppress it. **)
127380**
127381** (2) The inner query is the recursive part of a common table expression.
127382**
127383** (3) The inner query has a LIMIT clause (since the changes to the WHERE
127384** clause would change the meaning of the LIMIT).
127385**
127386** (4) The inner query is the right operand of a LEFT JOIN and the
127387** expression to be pushed down does not come from the ON clause
127388** on that LEFT JOIN.
127389**
127390** (5) The WHERE clause expression originates in the ON or USING clause
127391** of a LEFT JOIN where iCursor is not the right-hand table of that
127392** left join. An example:
127393**
127394** SELECT *
127395** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
127396** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
127397** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
127398**
127399** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9).
127400** But if the (b2=2) term were to be pushed down into the bb subquery,
127401** then the (1,1,NULL) row would be suppressed.
127402**
127403** (6) The inner query features one or more window-functions (since
127404** changes to the WHERE clause of the inner query could change the
127405** window over which window functions are calculated).
127406**
127407** Return 0 if no changes are made and non-zero if one or more WHERE clause
127408** terms are duplicated into the subquery.
127409*/
127410static int pushDownWhereTerms(
127411 Parse *pParse, /* Parse context (for malloc() and error reporting) */
127412 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
127413 Expr *pWhere, /* The WHERE clause of the outer query */
127414 int iCursor, /* Cursor number of the subquery */
127415 int isLeftJoin /* True if pSubq is the right term of a LEFT JOIN */
127416){
127417 Expr *pNew;
127418 int nChng = 0;
127419 if( pWhere==0 ) return 0;
127420 if( pSubq->selFlags & SF_Recursive ) return 0; /* restriction (2) */
127421
127422#ifndef SQLITE_OMIT_WINDOWFUNC
127423 if( pSubq->pWin ) return 0; /* restriction (6) */
127424#endif
127425
127426#ifdef SQLITE_DEBUG
127427 /* Only the first term of a compound can have a WITH clause. But make
127428 ** sure no other terms are marked SF_Recursive in case something changes
127429 ** in the future.
127430 */
127431 {
127432 Select *pX;
127433 for(pX=pSubq; pX; pX=pX->pPrior){
127434 assert( (pX->selFlags & (SF_Recursive))==0 );
127435 }
127436 }
127437#endif
127438
127439 if( pSubq->pLimit!=0 ){
127440 return 0; /* restriction (3) */
127441 }
127442 while( pWhere->op==TK_AND ){
127443 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
127444 iCursor, isLeftJoin);
127445 pWhere = pWhere->pLeft;
127446 }
127447 if( isLeftJoin
127448 && (ExprHasProperty(pWhere,EP_FromJoin)==0
127449 || pWhere->iRightJoinTable!=iCursor)
127450 ){
127451 return 0; /* restriction (4) */
127452 }
127453 if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
127454 return 0; /* restriction (5) */
127455 }
127456 if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
127457 nChng++;
127458 while( pSubq ){
127459 SubstContext x;
127460 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
127461 unsetJoinExpr(pNew, -1);
127462 x.pParse = pParse;
127463 x.iTable = iCursor;
127464 x.iNewTable = iCursor;
127465 x.isLeftJoin = 0;
127466 x.pEList = pSubq->pEList;
127467 pNew = substExpr(&x, pNew);
127468 if( pSubq->selFlags & SF_Aggregate ){
127469 pSubq->pHaving = sqlite3ExprAnd(pParse->db, pSubq->pHaving, pNew);
127470 }else{
127471 pSubq->pWhere = sqlite3ExprAnd(pParse->db, pSubq->pWhere, pNew);
127472 }
127473 pSubq = pSubq->pPrior;
127474 }
127475 }
127476 return nChng;
127477}
127478#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
127479
127480/*
127481** The pFunc is the only aggregate function in the query. Check to see
127482** if the query is a candidate for the min/max optimization.
127483**
127484** If the query is a candidate for the min/max optimization, then set
127485** *ppMinMax to be an ORDER BY clause to be used for the optimization
127486** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
127487** whether pFunc is a min() or max() function.
127488**
127489** If the query is not a candidate for the min/max optimization, return
127490** WHERE_ORDERBY_NORMAL (which must be zero).
127491**
127492** This routine must be called after aggregate functions have been
127493** located but before their arguments have been subjected to aggregate
127494** analysis.
127495*/
127496static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
127497 int eRet = WHERE_ORDERBY_NORMAL; /* Return value */
127498 ExprList *pEList = pFunc->x.pList; /* Arguments to agg function */
127499 const char *zFunc; /* Name of aggregate function pFunc */
127500 ExprList *pOrderBy;
127501 u8 sortOrder;
127502
127503 assert( *ppMinMax==0 );
127504 assert( pFunc->op==TK_AGG_FUNCTION );
127505 if( pEList==0 || pEList->nExpr!=1 ) return eRet;
127506 zFunc = pFunc->u.zToken;
127507 if( sqlite3StrICmp(zFunc, "min")==0 ){
127508 eRet = WHERE_ORDERBY_MIN;
127509 sortOrder = SQLITE_SO_ASC;
127510 }else if( sqlite3StrICmp(zFunc, "max")==0 ){
127511 eRet = WHERE_ORDERBY_MAX;
127512 sortOrder = SQLITE_SO_DESC;
127513 }else{
127514 return eRet;
127515 }
127516 *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
127517 assert( pOrderBy!=0 || db->mallocFailed );
127518 if( pOrderBy ) pOrderBy->a[0].sortOrder = sortOrder;
127519 return eRet;
127520}
127521
127522/*
127523** The select statement passed as the first argument is an aggregate query.
127524** The second argument is the associated aggregate-info object. This
127525** function tests if the SELECT is of the form:
127526**
127527** SELECT count(*) FROM <tbl>
127528**
127529** where table is a database table, not a sub-select or view. If the query
127530** does match this pattern, then a pointer to the Table object representing
127531** <tbl> is returned. Otherwise, 0 is returned.
127532*/
127533static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
127534 Table *pTab;
127535 Expr *pExpr;
127536
127537 assert( !p->pGroupBy );
127538
127539 if( p->pWhere || p->pEList->nExpr!=1
127540 || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
127541 ){
127542 return 0;
127543 }
127544 pTab = p->pSrc->a[0].pTab;
127545 pExpr = p->pEList->a[0].pExpr;
127546 assert( pTab && !pTab->pSelect && pExpr );
127547
127548 if( IsVirtual(pTab) ) return 0;
127549 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
127550 if( NEVER(pAggInfo->nFunc==0) ) return 0;
127551 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
127552 if( pExpr->flags&EP_Distinct ) return 0;
127553
127554 return pTab;
127555}
127556
127557/*
127558** If the source-list item passed as an argument was augmented with an
127559** INDEXED BY clause, then try to locate the specified index. If there
127560** was such a clause and the named index cannot be found, return
127561** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
127562** pFrom->pIndex and return SQLITE_OK.
127563*/
127564SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
127565 if( pFrom->pTab && pFrom->fg.isIndexedBy ){
127566 Table *pTab = pFrom->pTab;
127567 char *zIndexedBy = pFrom->u1.zIndexedBy;
127568 Index *pIdx;
127569 for(pIdx=pTab->pIndex;
127570 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
127571 pIdx=pIdx->pNext
127572 );
127573 if( !pIdx ){
127574 sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
127575 pParse->checkSchema = 1;
127576 return SQLITE_ERROR;
127577 }
127578 pFrom->pIBIndex = pIdx;
127579 }
127580 return SQLITE_OK;
127581}
127582/*
127583** Detect compound SELECT statements that use an ORDER BY clause with
127584** an alternative collating sequence.
127585**
127586** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
127587**
127588** These are rewritten as a subquery:
127589**
127590** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
127591** ORDER BY ... COLLATE ...
127592**
127593** This transformation is necessary because the multiSelectOrderBy() routine
127594** above that generates the code for a compound SELECT with an ORDER BY clause
127595** uses a merge algorithm that requires the same collating sequence on the
127596** result columns as on the ORDER BY clause. See ticket
127597** http://www.sqlite.org/src/info/6709574d2a
127598**
127599** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
127600** The UNION ALL operator works fine with multiSelectOrderBy() even when
127601** there are COLLATE terms in the ORDER BY.
127602*/
127603static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
127604 int i;
127605 Select *pNew;
127606 Select *pX;
127607 sqlite3 *db;
127608 struct ExprList_item *a;
127609 SrcList *pNewSrc;
127610 Parse *pParse;
127611 Token dummy;
127612
127613 if( p->pPrior==0 ) return WRC_Continue;
127614 if( p->pOrderBy==0 ) return WRC_Continue;
127615 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
127616 if( pX==0 ) return WRC_Continue;
127617 a = p->pOrderBy->a;
127618 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
127619 if( a[i].pExpr->flags & EP_Collate ) break;
127620 }
127621 if( i<0 ) return WRC_Continue;
127622
127623 /* If we reach this point, that means the transformation is required. */
127624
127625 pParse = pWalker->pParse;
127626 db = pParse->db;
127627 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
127628 if( pNew==0 ) return WRC_Abort;
127629 memset(&dummy, 0, sizeof(dummy));
127630 pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
127631 if( pNewSrc==0 ) return WRC_Abort;
127632 *pNew = *p;
127633 p->pSrc = pNewSrc;
127634 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
127635 p->op = TK_SELECT;
127636 p->pWhere = 0;
127637 pNew->pGroupBy = 0;
127638 pNew->pHaving = 0;
127639 pNew->pOrderBy = 0;
127640 p->pPrior = 0;
127641 p->pNext = 0;
127642 p->pWith = 0;
127643 p->selFlags &= ~SF_Compound;
127644 assert( (p->selFlags & SF_Converted)==0 );
127645 p->selFlags |= SF_Converted;
127646 assert( pNew->pPrior!=0 );
127647 pNew->pPrior->pNext = pNew;
127648 pNew->pLimit = 0;
127649 return WRC_Continue;
127650}
127651
127652/*
127653** Check to see if the FROM clause term pFrom has table-valued function
127654** arguments. If it does, leave an error message in pParse and return
127655** non-zero, since pFrom is not allowed to be a table-valued function.
127656*/
127657static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){
127658 if( pFrom->fg.isTabFunc ){
127659 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
127660 return 1;
127661 }
127662 return 0;
127663}
127664
127665#ifndef SQLITE_OMIT_CTE
127666/*
127667** Argument pWith (which may be NULL) points to a linked list of nested
127668** WITH contexts, from inner to outermost. If the table identified by
127669** FROM clause element pItem is really a common-table-expression (CTE)
127670** then return a pointer to the CTE definition for that table. Otherwise
127671** return NULL.
127672**
127673** If a non-NULL value is returned, set *ppContext to point to the With
127674** object that the returned CTE belongs to.
127675*/
127676static struct Cte *searchWith(
127677 With *pWith, /* Current innermost WITH clause */
127678 struct SrcList_item *pItem, /* FROM clause element to resolve */
127679 With **ppContext /* OUT: WITH clause return value belongs to */
127680){
127681 const char *zName;
127682 if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
127683 With *p;
127684 for(p=pWith; p; p=p->pOuter){
127685 int i;
127686 for(i=0; i<p->nCte; i++){
127687 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
127688 *ppContext = p;
127689 return &p->a[i];
127690 }
127691 }
127692 }
127693 }
127694 return 0;
127695}
127696
127697/* The code generator maintains a stack of active WITH clauses
127698** with the inner-most WITH clause being at the top of the stack.
127699**
127700** This routine pushes the WITH clause passed as the second argument
127701** onto the top of the stack. If argument bFree is true, then this
127702** WITH clause will never be popped from the stack. In this case it
127703** should be freed along with the Parse object. In other cases, when
127704** bFree==0, the With object will be freed along with the SELECT
127705** statement with which it is associated.
127706*/
127707SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
127708 assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) );
127709 if( pWith ){
127710 assert( pParse->pWith!=pWith );
127711 pWith->pOuter = pParse->pWith;
127712 pParse->pWith = pWith;
127713 if( bFree ) pParse->pWithToFree = pWith;
127714 }
127715}
127716
127717/*
127718** This function checks if argument pFrom refers to a CTE declared by
127719** a WITH clause on the stack currently maintained by the parser. And,
127720** if currently processing a CTE expression, if it is a recursive
127721** reference to the current CTE.
127722**
127723** If pFrom falls into either of the two categories above, pFrom->pTab
127724** and other fields are populated accordingly. The caller should check
127725** (pFrom->pTab!=0) to determine whether or not a successful match
127726** was found.
127727**
127728** Whether or not a match is found, SQLITE_OK is returned if no error
127729** occurs. If an error does occur, an error message is stored in the
127730** parser and some error code other than SQLITE_OK returned.
127731*/
127732static int withExpand(
127733 Walker *pWalker,
127734 struct SrcList_item *pFrom
127735){
127736 Parse *pParse = pWalker->pParse;
127737 sqlite3 *db = pParse->db;
127738 struct Cte *pCte; /* Matched CTE (or NULL if no match) */
127739 With *pWith; /* WITH clause that pCte belongs to */
127740
127741 assert( pFrom->pTab==0 );
127742
127743 pCte = searchWith(pParse->pWith, pFrom, &pWith);
127744 if( pCte ){
127745 Table *pTab;
127746 ExprList *pEList;
127747 Select *pSel;
127748 Select *pLeft; /* Left-most SELECT statement */
127749 int bMayRecursive; /* True if compound joined by UNION [ALL] */
127750 With *pSavedWith; /* Initial value of pParse->pWith */
127751
127752 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
127753 ** recursive reference to CTE pCte. Leave an error in pParse and return
127754 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
127755 ** In this case, proceed. */
127756 if( pCte->zCteErr ){
127757 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
127758 return SQLITE_ERROR;
127759 }
127760 if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR;
127761
127762 assert( pFrom->pTab==0 );
127763 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
127764 if( pTab==0 ) return WRC_Abort;
127765 pTab->nTabRef = 1;
127766 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
127767 pTab->iPKey = -1;
127768 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
127769 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
127770 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
127771 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
127772 assert( pFrom->pSelect );
127773
127774 /* Check if this is a recursive CTE. */
127775 pSel = pFrom->pSelect;
127776 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
127777 if( bMayRecursive ){
127778 int i;
127779 SrcList *pSrc = pFrom->pSelect->pSrc;
127780 for(i=0; i<pSrc->nSrc; i++){
127781 struct SrcList_item *pItem = &pSrc->a[i];
127782 if( pItem->zDatabase==0
127783 && pItem->zName!=0
127784 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
127785 ){
127786 pItem->pTab = pTab;
127787 pItem->fg.isRecursive = 1;
127788 pTab->nTabRef++;
127789 pSel->selFlags |= SF_Recursive;
127790 }
127791 }
127792 }
127793
127794 /* Only one recursive reference is permitted. */
127795 if( pTab->nTabRef>2 ){
127796 sqlite3ErrorMsg(
127797 pParse, "multiple references to recursive table: %s", pCte->zName
127798 );
127799 return SQLITE_ERROR;
127800 }
127801 assert( pTab->nTabRef==1 ||
127802 ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
127803
127804 pCte->zCteErr = "circular reference: %s";
127805 pSavedWith = pParse->pWith;
127806 pParse->pWith = pWith;
127807 if( bMayRecursive ){
127808 Select *pPrior = pSel->pPrior;
127809 assert( pPrior->pWith==0 );
127810 pPrior->pWith = pSel->pWith;
127811 sqlite3WalkSelect(pWalker, pPrior);
127812 pPrior->pWith = 0;
127813 }else{
127814 sqlite3WalkSelect(pWalker, pSel);
127815 }
127816 pParse->pWith = pWith;
127817
127818 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
127819 pEList = pLeft->pEList;
127820 if( pCte->pCols ){
127821 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
127822 sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
127823 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
127824 );
127825 pParse->pWith = pSavedWith;
127826 return SQLITE_ERROR;
127827 }
127828 pEList = pCte->pCols;
127829 }
127830
127831 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
127832 if( bMayRecursive ){
127833 if( pSel->selFlags & SF_Recursive ){
127834 pCte->zCteErr = "multiple recursive references: %s";
127835 }else{
127836 pCte->zCteErr = "recursive reference in a subquery: %s";
127837 }
127838 sqlite3WalkSelect(pWalker, pSel);
127839 }
127840 pCte->zCteErr = 0;
127841 pParse->pWith = pSavedWith;
127842 }
127843
127844 return SQLITE_OK;
127845}
127846#endif
127847
127848#ifndef SQLITE_OMIT_CTE
127849/*
127850** If the SELECT passed as the second argument has an associated WITH
127851** clause, pop it from the stack stored as part of the Parse object.
127852**
127853** This function is used as the xSelectCallback2() callback by
127854** sqlite3SelectExpand() when walking a SELECT tree to resolve table
127855** names and other FROM clause elements.
127856*/
127857static void selectPopWith(Walker *pWalker, Select *p){
127858 Parse *pParse = pWalker->pParse;
127859 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
127860 With *pWith = findRightmost(p)->pWith;
127861 if( pWith!=0 ){
127862 assert( pParse->pWith==pWith );
127863 pParse->pWith = pWith->pOuter;
127864 }
127865 }
127866}
127867#else
127868#define selectPopWith 0
127869#endif
127870
127871/*
127872** The SrcList_item structure passed as the second argument represents a
127873** sub-query in the FROM clause of a SELECT statement. This function
127874** allocates and populates the SrcList_item.pTab object. If successful,
127875** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
127876** SQLITE_NOMEM.
127877*/
127878SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse *pParse, struct SrcList_item *pFrom){
127879 Select *pSel = pFrom->pSelect;
127880 Table *pTab;
127881
127882 assert( pSel );
127883 pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
127884 if( pTab==0 ) return SQLITE_NOMEM;
127885 pTab->nTabRef = 1;
127886 if( pFrom->zAlias ){
127887 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
127888 }else{
127889 pTab->zName = sqlite3MPrintf(pParse->db, "subquery_%u", pSel->selId);
127890 }
127891 while( pSel->pPrior ){ pSel = pSel->pPrior; }
127892 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
127893 pTab->iPKey = -1;
127894 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
127895 pTab->tabFlags |= TF_Ephemeral;
127896
127897 return SQLITE_OK;
127898}
127899
127900/*
127901** This routine is a Walker callback for "expanding" a SELECT statement.
127902** "Expanding" means to do the following:
127903**
127904** (1) Make sure VDBE cursor numbers have been assigned to every
127905** element of the FROM clause.
127906**
127907** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
127908** defines FROM clause. When views appear in the FROM clause,
127909** fill pTabList->a[].pSelect with a copy of the SELECT statement
127910** that implements the view. A copy is made of the view's SELECT
127911** statement so that we can freely modify or delete that statement
127912** without worrying about messing up the persistent representation
127913** of the view.
127914**
127915** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
127916** on joins and the ON and USING clause of joins.
127917**
127918** (4) Scan the list of columns in the result set (pEList) looking
127919** for instances of the "*" operator or the TABLE.* operator.
127920** If found, expand each "*" to be every column in every table
127921** and TABLE.* to be every column in TABLE.
127922**
127923*/
127924static int selectExpander(Walker *pWalker, Select *p){
127925 Parse *pParse = pWalker->pParse;
127926 int i, j, k;
127927 SrcList *pTabList;
127928 ExprList *pEList;
127929 struct SrcList_item *pFrom;
127930 sqlite3 *db = pParse->db;
127931 Expr *pE, *pRight, *pExpr;
127932 u16 selFlags = p->selFlags;
127933 u32 elistFlags = 0;
127934
127935 p->selFlags |= SF_Expanded;
127936 if( db->mallocFailed ){
127937 return WRC_Abort;
127938 }
127939 assert( p->pSrc!=0 );
127940 if( (selFlags & SF_Expanded)!=0 ){
127941 return WRC_Prune;
127942 }
127943 pTabList = p->pSrc;
127944 pEList = p->pEList;
127945 sqlite3WithPush(pParse, p->pWith, 0);
127946
127947 /* Make sure cursor numbers have been assigned to all entries in
127948 ** the FROM clause of the SELECT statement.
127949 */
127950 sqlite3SrcListAssignCursors(pParse, pTabList);
127951
127952 /* Look up every table named in the FROM clause of the select. If
127953 ** an entry of the FROM clause is a subquery instead of a table or view,
127954 ** then create a transient table structure to describe the subquery.
127955 */
127956 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
127957 Table *pTab;
127958 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
127959 if( pFrom->fg.isRecursive ) continue;
127960 assert( pFrom->pTab==0 );
127961#ifndef SQLITE_OMIT_CTE
127962 if( withExpand(pWalker, pFrom) ) return WRC_Abort;
127963 if( pFrom->pTab ) {} else
127964#endif
127965 if( pFrom->zName==0 ){
127966#ifndef SQLITE_OMIT_SUBQUERY
127967 Select *pSel = pFrom->pSelect;
127968 /* A sub-query in the FROM clause of a SELECT */
127969 assert( pSel!=0 );
127970 assert( pFrom->pTab==0 );
127971 if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort;
127972 if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort;
127973#endif
127974 }else{
127975 /* An ordinary table or view name in the FROM clause */
127976 assert( pFrom->pTab==0 );
127977 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
127978 if( pTab==0 ) return WRC_Abort;
127979 if( pTab->nTabRef>=0xffff ){
127980 sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
127981 pTab->zName);
127982 pFrom->pTab = 0;
127983 return WRC_Abort;
127984 }
127985 pTab->nTabRef++;
127986 if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){
127987 return WRC_Abort;
127988 }
127989#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
127990 if( IsVirtual(pTab) || pTab->pSelect ){
127991 i16 nCol;
127992 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
127993 assert( pFrom->pSelect==0 );
127994 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
127995 nCol = pTab->nCol;
127996 pTab->nCol = -1;
127997 sqlite3WalkSelect(pWalker, pFrom->pSelect);
127998 pTab->nCol = nCol;
127999 }
128000#endif
128001 }
128002
128003 /* Locate the index named by the INDEXED BY clause, if any. */
128004 if( sqlite3IndexedByLookup(pParse, pFrom) ){
128005 return WRC_Abort;
128006 }
128007 }
128008
128009 /* Process NATURAL keywords, and ON and USING clauses of joins.
128010 */
128011 if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
128012 return WRC_Abort;
128013 }
128014
128015 /* For every "*" that occurs in the column list, insert the names of
128016 ** all columns in all tables. And for every TABLE.* insert the names
128017 ** of all columns in TABLE. The parser inserted a special expression
128018 ** with the TK_ASTERISK operator for each "*" that it found in the column
128019 ** list. The following code just has to locate the TK_ASTERISK
128020 ** expressions and expand each one to the list of all columns in
128021 ** all tables.
128022 **
128023 ** The first loop just checks to see if there are any "*" operators
128024 ** that need expanding.
128025 */
128026 for(k=0; k<pEList->nExpr; k++){
128027 pE = pEList->a[k].pExpr;
128028 if( pE->op==TK_ASTERISK ) break;
128029 assert( pE->op!=TK_DOT || pE->pRight!=0 );
128030 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
128031 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
128032 elistFlags |= pE->flags;
128033 }
128034 if( k<pEList->nExpr ){
128035 /*
128036 ** If we get here it means the result set contains one or more "*"
128037 ** operators that need to be expanded. Loop through each expression
128038 ** in the result set and expand them one by one.
128039 */
128040 struct ExprList_item *a = pEList->a;
128041 ExprList *pNew = 0;
128042 int flags = pParse->db->flags;
128043 int longNames = (flags & SQLITE_FullColNames)!=0
128044 && (flags & SQLITE_ShortColNames)==0;
128045
128046 for(k=0; k<pEList->nExpr; k++){
128047 pE = a[k].pExpr;
128048 elistFlags |= pE->flags;
128049 pRight = pE->pRight;
128050 assert( pE->op!=TK_DOT || pRight!=0 );
128051 if( pE->op!=TK_ASTERISK
128052 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
128053 ){
128054 /* This particular expression does not need to be expanded.
128055 */
128056 pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
128057 if( pNew ){
128058 pNew->a[pNew->nExpr-1].zName = a[k].zName;
128059 pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
128060 a[k].zName = 0;
128061 a[k].zSpan = 0;
128062 }
128063 a[k].pExpr = 0;
128064 }else{
128065 /* This expression is a "*" or a "TABLE.*" and needs to be
128066 ** expanded. */
128067 int tableSeen = 0; /* Set to 1 when TABLE matches */
128068 char *zTName = 0; /* text of name of TABLE */
128069 if( pE->op==TK_DOT ){
128070 assert( pE->pLeft!=0 );
128071 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
128072 zTName = pE->pLeft->u.zToken;
128073 }
128074 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
128075 Table *pTab = pFrom->pTab;
128076 Select *pSub = pFrom->pSelect;
128077 char *zTabName = pFrom->zAlias;
128078 const char *zSchemaName = 0;
128079 int iDb;
128080 if( zTabName==0 ){
128081 zTabName = pTab->zName;
128082 }
128083 if( db->mallocFailed ) break;
128084 if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
128085 pSub = 0;
128086 if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
128087 continue;
128088 }
128089 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
128090 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
128091 }
128092 for(j=0; j<pTab->nCol; j++){
128093 char *zName = pTab->aCol[j].zName;
128094 char *zColname; /* The computed column name */
128095 char *zToFree; /* Malloced string that needs to be freed */
128096 Token sColname; /* Computed column name as a token */
128097
128098 assert( zName );
128099 if( zTName && pSub
128100 && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
128101 ){
128102 continue;
128103 }
128104
128105 /* If a column is marked as 'hidden', omit it from the expanded
128106 ** result-set list unless the SELECT has the SF_IncludeHidden
128107 ** bit set.
128108 */
128109 if( (p->selFlags & SF_IncludeHidden)==0
128110 && IsHiddenColumn(&pTab->aCol[j])
128111 ){
128112 continue;
128113 }
128114 tableSeen = 1;
128115
128116 if( i>0 && zTName==0 ){
128117 if( (pFrom->fg.jointype & JT_NATURAL)!=0
128118 && tableAndColumnIndex(pTabList, i, zName, 0, 0)
128119 ){
128120 /* In a NATURAL join, omit the join columns from the
128121 ** table to the right of the join */
128122 continue;
128123 }
128124 if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
128125 /* In a join with a USING clause, omit columns in the
128126 ** using clause from the table on the right. */
128127 continue;
128128 }
128129 }
128130 pRight = sqlite3Expr(db, TK_ID, zName);
128131 zColname = zName;
128132 zToFree = 0;
128133 if( longNames || pTabList->nSrc>1 ){
128134 Expr *pLeft;
128135 pLeft = sqlite3Expr(db, TK_ID, zTabName);
128136 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
128137 if( zSchemaName ){
128138 pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
128139 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr);
128140 }
128141 if( longNames ){
128142 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
128143 zToFree = zColname;
128144 }
128145 }else{
128146 pExpr = pRight;
128147 }
128148 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
128149 sqlite3TokenInit(&sColname, zColname);
128150 sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
128151 if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
128152 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
128153 if( pSub ){
128154 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
128155 testcase( pX->zSpan==0 );
128156 }else{
128157 pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
128158 zSchemaName, zTabName, zColname);
128159 testcase( pX->zSpan==0 );
128160 }
128161 pX->bSpanIsTab = 1;
128162 }
128163 sqlite3DbFree(db, zToFree);
128164 }
128165 }
128166 if( !tableSeen ){
128167 if( zTName ){
128168 sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
128169 }else{
128170 sqlite3ErrorMsg(pParse, "no tables specified");
128171 }
128172 }
128173 }
128174 }
128175 sqlite3ExprListDelete(db, pEList);
128176 p->pEList = pNew;
128177 }
128178 if( p->pEList ){
128179 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
128180 sqlite3ErrorMsg(pParse, "too many columns in result set");
128181 return WRC_Abort;
128182 }
128183 if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
128184 p->selFlags |= SF_ComplexResult;
128185 }
128186 }
128187 return WRC_Continue;
128188}
128189
128190/*
128191** No-op routine for the parse-tree walker.
128192**
128193** When this routine is the Walker.xExprCallback then expression trees
128194** are walked without any actions being taken at each node. Presumably,
128195** when this routine is used for Walker.xExprCallback then
128196** Walker.xSelectCallback is set to do something useful for every
128197** subquery in the parser tree.
128198*/
128199SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
128200 UNUSED_PARAMETER2(NotUsed, NotUsed2);
128201 return WRC_Continue;
128202}
128203
128204/*
128205** No-op routine for the parse-tree walker for SELECT statements.
128206** subquery in the parser tree.
128207*/
128208SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
128209 UNUSED_PARAMETER2(NotUsed, NotUsed2);
128210 return WRC_Continue;
128211}
128212
128213#if SQLITE_DEBUG
128214/*
128215** Always assert. This xSelectCallback2 implementation proves that the
128216** xSelectCallback2 is never invoked.
128217*/
128218SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
128219 UNUSED_PARAMETER2(NotUsed, NotUsed2);
128220 assert( 0 );
128221}
128222#endif
128223/*
128224** This routine "expands" a SELECT statement and all of its subqueries.
128225** For additional information on what it means to "expand" a SELECT
128226** statement, see the comment on the selectExpand worker callback above.
128227**
128228** Expanding a SELECT statement is the first step in processing a
128229** SELECT statement. The SELECT statement must be expanded before
128230** name resolution is performed.
128231**
128232** If anything goes wrong, an error message is written into pParse.
128233** The calling function can detect the problem by looking at pParse->nErr
128234** and/or pParse->db->mallocFailed.
128235*/
128236static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
128237 Walker w;
128238 w.xExprCallback = sqlite3ExprWalkNoop;
128239 w.pParse = pParse;
128240 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
128241 w.xSelectCallback = convertCompoundSelectToSubquery;
128242 w.xSelectCallback2 = 0;
128243 sqlite3WalkSelect(&w, pSelect);
128244 }
128245 w.xSelectCallback = selectExpander;
128246 w.xSelectCallback2 = selectPopWith;
128247 sqlite3WalkSelect(&w, pSelect);
128248}
128249
128250
128251#ifndef SQLITE_OMIT_SUBQUERY
128252/*
128253** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
128254** interface.
128255**
128256** For each FROM-clause subquery, add Column.zType and Column.zColl
128257** information to the Table structure that represents the result set
128258** of that subquery.
128259**
128260** The Table structure that represents the result set was constructed
128261** by selectExpander() but the type and collation information was omitted
128262** at that point because identifiers had not yet been resolved. This
128263** routine is called after identifier resolution.
128264*/
128265static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
128266 Parse *pParse;
128267 int i;
128268 SrcList *pTabList;
128269 struct SrcList_item *pFrom;
128270
128271 assert( p->selFlags & SF_Resolved );
128272 if( p->selFlags & SF_HasTypeInfo ) return;
128273 p->selFlags |= SF_HasTypeInfo;
128274 pParse = pWalker->pParse;
128275 pTabList = p->pSrc;
128276 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
128277 Table *pTab = pFrom->pTab;
128278 assert( pTab!=0 );
128279 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
128280 /* A sub-query in the FROM clause of a SELECT */
128281 Select *pSel = pFrom->pSelect;
128282 if( pSel ){
128283 while( pSel->pPrior ) pSel = pSel->pPrior;
128284 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel);
128285 }
128286 }
128287 }
128288}
128289#endif
128290
128291
128292/*
128293** This routine adds datatype and collating sequence information to
128294** the Table structures of all FROM-clause subqueries in a
128295** SELECT statement.
128296**
128297** Use this routine after name resolution.
128298*/
128299static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
128300#ifndef SQLITE_OMIT_SUBQUERY
128301 Walker w;
128302 w.xSelectCallback = sqlite3SelectWalkNoop;
128303 w.xSelectCallback2 = selectAddSubqueryTypeInfo;
128304 w.xExprCallback = sqlite3ExprWalkNoop;
128305 w.pParse = pParse;
128306 sqlite3WalkSelect(&w, pSelect);
128307#endif
128308}
128309
128310
128311/*
128312** This routine sets up a SELECT statement for processing. The
128313** following is accomplished:
128314**
128315** * VDBE Cursor numbers are assigned to all FROM-clause terms.
128316** * Ephemeral Table objects are created for all FROM-clause subqueries.
128317** * ON and USING clauses are shifted into WHERE statements
128318** * Wildcards "*" and "TABLE.*" in result sets are expanded.
128319** * Identifiers in expression are matched to tables.
128320**
128321** This routine acts recursively on all subqueries within the SELECT.
128322*/
128323SQLITE_PRIVATE void sqlite3SelectPrep(
128324 Parse *pParse, /* The parser context */
128325 Select *p, /* The SELECT statement being coded. */
128326 NameContext *pOuterNC /* Name context for container */
128327){
128328 assert( p!=0 || pParse->db->mallocFailed );
128329 if( pParse->db->mallocFailed ) return;
128330 if( p->selFlags & SF_HasTypeInfo ) return;
128331 sqlite3SelectExpand(pParse, p);
128332 if( pParse->nErr || pParse->db->mallocFailed ) return;
128333 sqlite3ResolveSelectNames(pParse, p, pOuterNC);
128334 if( pParse->nErr || pParse->db->mallocFailed ) return;
128335 sqlite3SelectAddTypeInfo(pParse, p);
128336}
128337
128338/*
128339** Reset the aggregate accumulator.
128340**
128341** The aggregate accumulator is a set of memory cells that hold
128342** intermediate results while calculating an aggregate. This
128343** routine generates code that stores NULLs in all of those memory
128344** cells.
128345*/
128346static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
128347 Vdbe *v = pParse->pVdbe;
128348 int i;
128349 struct AggInfo_func *pFunc;
128350 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
128351 if( nReg==0 ) return;
128352#ifdef SQLITE_DEBUG
128353 /* Verify that all AggInfo registers are within the range specified by
128354 ** AggInfo.mnReg..AggInfo.mxReg */
128355 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
128356 for(i=0; i<pAggInfo->nColumn; i++){
128357 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
128358 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
128359 }
128360 for(i=0; i<pAggInfo->nFunc; i++){
128361 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
128362 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
128363 }
128364#endif
128365 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
128366 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
128367 if( pFunc->iDistinct>=0 ){
128368 Expr *pE = pFunc->pExpr;
128369 assert( !ExprHasProperty(pE, EP_xIsSelect) );
128370 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
128371 sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
128372 "argument");
128373 pFunc->iDistinct = -1;
128374 }else{
128375 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
128376 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
128377 (char*)pKeyInfo, P4_KEYINFO);
128378 }
128379 }
128380 }
128381}
128382
128383/*
128384** Invoke the OP_AggFinalize opcode for every aggregate function
128385** in the AggInfo structure.
128386*/
128387static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
128388 Vdbe *v = pParse->pVdbe;
128389 int i;
128390 struct AggInfo_func *pF;
128391 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
128392 ExprList *pList = pF->pExpr->x.pList;
128393 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
128394 sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
128395 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
128396 }
128397}
128398
128399
128400/*
128401** Update the accumulator memory cells for an aggregate based on
128402** the current cursor position.
128403**
128404** If regAcc is non-zero and there are no min() or max() aggregates
128405** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
128406** registers i register regAcc contains 0. The caller will take care
128407** of setting and clearing regAcc.
128408*/
128409static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){
128410 Vdbe *v = pParse->pVdbe;
128411 int i;
128412 int regHit = 0;
128413 int addrHitTest = 0;
128414 struct AggInfo_func *pF;
128415 struct AggInfo_col *pC;
128416
128417 pAggInfo->directMode = 1;
128418 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
128419 int nArg;
128420 int addrNext = 0;
128421 int regAgg;
128422 ExprList *pList = pF->pExpr->x.pList;
128423 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
128424 if( pList ){
128425 nArg = pList->nExpr;
128426 regAgg = sqlite3GetTempRange(pParse, nArg);
128427 sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP);
128428 }else{
128429 nArg = 0;
128430 regAgg = 0;
128431 }
128432 if( pF->iDistinct>=0 ){
128433 addrNext = sqlite3VdbeMakeLabel(v);
128434 testcase( nArg==0 ); /* Error condition */
128435 testcase( nArg>1 ); /* Also an error */
128436 codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
128437 }
128438 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
128439 CollSeq *pColl = 0;
128440 struct ExprList_item *pItem;
128441 int j;
128442 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
128443 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
128444 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
128445 }
128446 if( !pColl ){
128447 pColl = pParse->db->pDfltColl;
128448 }
128449 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
128450 sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
128451 }
128452 sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
128453 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
128454 sqlite3VdbeChangeP5(v, (u8)nArg);
128455 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
128456 if( addrNext ){
128457 sqlite3VdbeResolveLabel(v, addrNext);
128458 }
128459 }
128460 if( regHit==0 && pAggInfo->nAccumulator ){
128461 regHit = regAcc;
128462 }
128463 if( regHit ){
128464 addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
128465 }
128466 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
128467 sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
128468 }
128469 pAggInfo->directMode = 0;
128470 if( addrHitTest ){
128471 sqlite3VdbeJumpHere(v, addrHitTest);
128472 }
128473}
128474
128475/*
128476** Add a single OP_Explain instruction to the VDBE to explain a simple
128477** count(*) query ("SELECT count(*) FROM pTab").
128478*/
128479#ifndef SQLITE_OMIT_EXPLAIN
128480static void explainSimpleCount(
128481 Parse *pParse, /* Parse context */
128482 Table *pTab, /* Table being queried */
128483 Index *pIdx /* Index used to optimize scan, or NULL */
128484){
128485 if( pParse->explain==2 ){
128486 int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
128487 sqlite3VdbeExplain(pParse, 0, "SCAN TABLE %s%s%s",
128488 pTab->zName,
128489 bCover ? " USING COVERING INDEX " : "",
128490 bCover ? pIdx->zName : ""
128491 );
128492 }
128493}
128494#else
128495# define explainSimpleCount(a,b,c)
128496#endif
128497
128498/*
128499** sqlite3WalkExpr() callback used by havingToWhere().
128500**
128501** If the node passed to the callback is a TK_AND node, return
128502** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
128503**
128504** Otherwise, return WRC_Prune. In this case, also check if the
128505** sub-expression matches the criteria for being moved to the WHERE
128506** clause. If so, add it to the WHERE clause and replace the sub-expression
128507** within the HAVING expression with a constant "1".
128508*/
128509static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
128510 if( pExpr->op!=TK_AND ){
128511 Select *pS = pWalker->u.pSelect;
128512 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
128513 sqlite3 *db = pWalker->pParse->db;
128514 Expr *pNew = sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[1], 0);
128515 if( pNew ){
128516 Expr *pWhere = pS->pWhere;
128517 SWAP(Expr, *pNew, *pExpr);
128518 pNew = sqlite3ExprAnd(db, pWhere, pNew);
128519 pS->pWhere = pNew;
128520 pWalker->eCode = 1;
128521 }
128522 }
128523 return WRC_Prune;
128524 }
128525 return WRC_Continue;
128526}
128527
128528/*
128529** Transfer eligible terms from the HAVING clause of a query, which is
128530** processed after grouping, to the WHERE clause, which is processed before
128531** grouping. For example, the query:
128532**
128533** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
128534**
128535** can be rewritten as:
128536**
128537** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
128538**
128539** A term of the HAVING expression is eligible for transfer if it consists
128540** entirely of constants and expressions that are also GROUP BY terms that
128541** use the "BINARY" collation sequence.
128542*/
128543static void havingToWhere(Parse *pParse, Select *p){
128544 Walker sWalker;
128545 memset(&sWalker, 0, sizeof(sWalker));
128546 sWalker.pParse = pParse;
128547 sWalker.xExprCallback = havingToWhereExprCb;
128548 sWalker.u.pSelect = p;
128549 sqlite3WalkExpr(&sWalker, p->pHaving);
128550#if SELECTTRACE_ENABLED
128551 if( sWalker.eCode && (sqlite3SelectTrace & 0x100)!=0 ){
128552 SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
128553 sqlite3TreeViewSelect(0, p, 0);
128554 }
128555#endif
128556}
128557
128558/*
128559** Check to see if the pThis entry of pTabList is a self-join of a prior view.
128560** If it is, then return the SrcList_item for the prior view. If it is not,
128561** then return 0.
128562*/
128563static struct SrcList_item *isSelfJoinView(
128564 SrcList *pTabList, /* Search for self-joins in this FROM clause */
128565 struct SrcList_item *pThis /* Search for prior reference to this subquery */
128566){
128567 struct SrcList_item *pItem;
128568 for(pItem = pTabList->a; pItem<pThis; pItem++){
128569 if( pItem->pSelect==0 ) continue;
128570 if( pItem->fg.viaCoroutine ) continue;
128571 if( pItem->zName==0 ) continue;
128572 if( sqlite3_stricmp(pItem->zDatabase, pThis->zDatabase)!=0 ) continue;
128573 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
128574 if( sqlite3ExprCompare(0,
128575 pThis->pSelect->pWhere, pItem->pSelect->pWhere, -1)
128576 ){
128577 /* The view was modified by some other optimization such as
128578 ** pushDownWhereTerms() */
128579 continue;
128580 }
128581 return pItem;
128582 }
128583 return 0;
128584}
128585
128586#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
128587/*
128588** Attempt to transform a query of the form
128589**
128590** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
128591**
128592** Into this:
128593**
128594** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
128595**
128596** The transformation only works if all of the following are true:
128597**
128598** * The subquery is a UNION ALL of two or more terms
128599** * The subquery does not have a LIMIT clause
128600** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries
128601** * The outer query is a simple count(*)
128602**
128603** Return TRUE if the optimization is undertaken.
128604*/
128605static int countOfViewOptimization(Parse *pParse, Select *p){
128606 Select *pSub, *pPrior;
128607 Expr *pExpr;
128608 Expr *pCount;
128609 sqlite3 *db;
128610 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
128611 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
128612 pExpr = p->pEList->a[0].pExpr;
128613 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
128614 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
128615 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
128616 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
128617 pSub = p->pSrc->a[0].pSelect;
128618 if( pSub==0 ) return 0; /* The FROM is a subquery */
128619 if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
128620 do{
128621 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
128622 if( pSub->pWhere ) return 0; /* No WHERE clause */
128623 if( pSub->pLimit ) return 0; /* No LIMIT clause */
128624 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
128625 pSub = pSub->pPrior; /* Repeat over compound */
128626 }while( pSub );
128627
128628 /* If we reach this point then it is OK to perform the transformation */
128629
128630 db = pParse->db;
128631 pCount = pExpr;
128632 pExpr = 0;
128633 pSub = p->pSrc->a[0].pSelect;
128634 p->pSrc->a[0].pSelect = 0;
128635 sqlite3SrcListDelete(db, p->pSrc);
128636 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
128637 while( pSub ){
128638 Expr *pTerm;
128639 pPrior = pSub->pPrior;
128640 pSub->pPrior = 0;
128641 pSub->pNext = 0;
128642 pSub->selFlags |= SF_Aggregate;
128643 pSub->selFlags &= ~SF_Compound;
128644 pSub->nSelectRow = 0;
128645 sqlite3ExprListDelete(db, pSub->pEList);
128646 pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
128647 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
128648 pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
128649 sqlite3PExprAddSelect(pParse, pTerm, pSub);
128650 if( pExpr==0 ){
128651 pExpr = pTerm;
128652 }else{
128653 pExpr = sqlite3PExpr(pParse, TK_PLUS, pTerm, pExpr);
128654 }
128655 pSub = pPrior;
128656 }
128657 p->pEList->a[0].pExpr = pExpr;
128658 p->selFlags &= ~SF_Aggregate;
128659
128660#if SELECTTRACE_ENABLED
128661 if( sqlite3SelectTrace & 0x400 ){
128662 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
128663 sqlite3TreeViewSelect(0, p, 0);
128664 }
128665#endif
128666 return 1;
128667}
128668#endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
128669
128670/*
128671** Generate code for the SELECT statement given in the p argument.
128672**
128673** The results are returned according to the SelectDest structure.
128674** See comments in sqliteInt.h for further information.
128675**
128676** This routine returns the number of errors. If any errors are
128677** encountered, then an appropriate error message is left in
128678** pParse->zErrMsg.
128679**
128680** This routine does NOT free the Select structure passed in. The
128681** calling function needs to do that.
128682*/
128683SQLITE_PRIVATE int sqlite3Select(
128684 Parse *pParse, /* The parser context */
128685 Select *p, /* The SELECT statement being coded. */
128686 SelectDest *pDest /* What to do with the query results */
128687){
128688 int i, j; /* Loop counters */
128689 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
128690 Vdbe *v; /* The virtual machine under construction */
128691 int isAgg; /* True for select lists like "count(*)" */
128692 ExprList *pEList = 0; /* List of columns to extract. */
128693 SrcList *pTabList; /* List of tables to select from */
128694 Expr *pWhere; /* The WHERE clause. May be NULL */
128695 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
128696 Expr *pHaving; /* The HAVING clause. May be NULL */
128697 int rc = 1; /* Value to return from this function */
128698 DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
128699 SortCtx sSort; /* Info on how to code the ORDER BY clause */
128700 AggInfo sAggInfo; /* Information used by aggregate queries */
128701 int iEnd; /* Address of the end of the query */
128702 sqlite3 *db; /* The database connection */
128703 ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
128704 u8 minMaxFlag; /* Flag for min/max queries */
128705
128706 db = pParse->db;
128707 v = sqlite3GetVdbe(pParse);
128708 if( p==0 || db->mallocFailed || pParse->nErr ){
128709 return 1;
128710 }
128711 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
128712 memset(&sAggInfo, 0, sizeof(sAggInfo));
128713#if SELECTTRACE_ENABLED
128714 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
128715 if( sqlite3SelectTrace & 0x100 ){
128716 sqlite3TreeViewSelect(0, p, 0);
128717 }
128718#endif
128719
128720 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
128721 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
128722 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
128723 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
128724 if( IgnorableOrderby(pDest) ){
128725 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
128726 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
128727 pDest->eDest==SRT_Queue || pDest->eDest==SRT_DistFifo ||
128728 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo);
128729 /* If ORDER BY makes no difference in the output then neither does
128730 ** DISTINCT so it can be removed too. */
128731 sqlite3ExprListDelete(db, p->pOrderBy);
128732 p->pOrderBy = 0;
128733 p->selFlags &= ~SF_Distinct;
128734 }
128735 sqlite3SelectPrep(pParse, p, 0);
128736 if( pParse->nErr || db->mallocFailed ){
128737 goto select_end;
128738 }
128739 assert( p->pEList!=0 );
128740#if SELECTTRACE_ENABLED
128741 if( sqlite3SelectTrace & 0x104 ){
128742 SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
128743 sqlite3TreeViewSelect(0, p, 0);
128744 }
128745#endif
128746
128747 if( pDest->eDest==SRT_Output ){
128748 generateColumnNames(pParse, p);
128749 }
128750
128751#ifndef SQLITE_OMIT_WINDOWFUNC
128752 if( sqlite3WindowRewrite(pParse, p) ){
128753 goto select_end;
128754 }
128755#if SELECTTRACE_ENABLED
128756 if( sqlite3SelectTrace & 0x108 ){
128757 SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
128758 sqlite3TreeViewSelect(0, p, 0);
128759 }
128760#endif
128761#endif /* SQLITE_OMIT_WINDOWFUNC */
128762 pTabList = p->pSrc;
128763 isAgg = (p->selFlags & SF_Aggregate)!=0;
128764 memset(&sSort, 0, sizeof(sSort));
128765 sSort.pOrderBy = p->pOrderBy;
128766
128767 /* Try to various optimizations (flattening subqueries, and strength
128768 ** reduction of join operators) in the FROM clause up into the main query
128769 */
128770#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
128771 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
128772 struct SrcList_item *pItem = &pTabList->a[i];
128773 Select *pSub = pItem->pSelect;
128774 Table *pTab = pItem->pTab;
128775
128776 /* Convert LEFT JOIN into JOIN if there are terms of the right table
128777 ** of the LEFT JOIN used in the WHERE clause.
128778 */
128779 if( (pItem->fg.jointype & JT_LEFT)!=0
128780 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
128781 && OptimizationEnabled(db, SQLITE_SimplifyJoin)
128782 ){
128783 SELECTTRACE(0x100,pParse,p,
128784 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
128785 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
128786 unsetJoinExpr(p->pWhere, pItem->iCursor);
128787 }
128788
128789 /* No futher action if this term of the FROM clause is no a subquery */
128790 if( pSub==0 ) continue;
128791
128792 /* Catch mismatch in the declared columns of a view and the number of
128793 ** columns in the SELECT on the RHS */
128794 if( pTab->nCol!=pSub->pEList->nExpr ){
128795 sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
128796 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
128797 goto select_end;
128798 }
128799
128800 /* Do not try to flatten an aggregate subquery.
128801 **
128802 ** Flattening an aggregate subquery is only possible if the outer query
128803 ** is not a join. But if the outer query is not a join, then the subquery
128804 ** will be implemented as a co-routine and there is no advantage to
128805 ** flattening in that case.
128806 */
128807 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
128808 assert( pSub->pGroupBy==0 );
128809
128810 /* If the outer query contains a "complex" result set (that is,
128811 ** if the result set of the outer query uses functions or subqueries)
128812 ** and if the subquery contains an ORDER BY clause and if
128813 ** it will be implemented as a co-routine, then do not flatten. This
128814 ** restriction allows SQL constructs like this:
128815 **
128816 ** SELECT expensive_function(x)
128817 ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
128818 **
128819 ** The expensive_function() is only computed on the 10 rows that
128820 ** are output, rather than every row of the table.
128821 **
128822 ** The requirement that the outer query have a complex result set
128823 ** means that flattening does occur on simpler SQL constraints without
128824 ** the expensive_function() like:
128825 **
128826 ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
128827 */
128828 if( pSub->pOrderBy!=0
128829 && i==0
128830 && (p->selFlags & SF_ComplexResult)!=0
128831 && (pTabList->nSrc==1
128832 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
128833 ){
128834 continue;
128835 }
128836
128837 if( flattenSubquery(pParse, p, i, isAgg) ){
128838 /* This subquery can be absorbed into its parent. */
128839 i = -1;
128840 }
128841 pTabList = p->pSrc;
128842 if( db->mallocFailed ) goto select_end;
128843 if( !IgnorableOrderby(pDest) ){
128844 sSort.pOrderBy = p->pOrderBy;
128845 }
128846 }
128847#endif
128848
128849#ifndef SQLITE_OMIT_COMPOUND_SELECT
128850 /* Handle compound SELECT statements using the separate multiSelect()
128851 ** procedure.
128852 */
128853 if( p->pPrior ){
128854 rc = multiSelect(pParse, p, pDest);
128855#if SELECTTRACE_ENABLED
128856 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
128857 if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
128858 sqlite3TreeViewSelect(0, p, 0);
128859 }
128860#endif
128861 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
128862 return rc;
128863 }
128864#endif
128865
128866 /* Do the WHERE-clause constant propagation optimization if this is
128867 ** a join. No need to speed time on this operation for non-join queries
128868 ** as the equivalent optimization will be handled by query planner in
128869 ** sqlite3WhereBegin().
128870 */
128871 if( pTabList->nSrc>1
128872 && OptimizationEnabled(db, SQLITE_PropagateConst)
128873 && propagateConstants(pParse, p)
128874 ){
128875#if SELECTTRACE_ENABLED
128876 if( sqlite3SelectTrace & 0x100 ){
128877 SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
128878 sqlite3TreeViewSelect(0, p, 0);
128879 }
128880#endif
128881 }else{
128882 SELECTTRACE(0x100,pParse,p,("Constant propagation not helpful\n"));
128883 }
128884
128885#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
128886 if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
128887 && countOfViewOptimization(pParse, p)
128888 ){
128889 if( db->mallocFailed ) goto select_end;
128890 pEList = p->pEList;
128891 pTabList = p->pSrc;
128892 }
128893#endif
128894
128895 /* For each term in the FROM clause, do two things:
128896 ** (1) Authorized unreferenced tables
128897 ** (2) Generate code for all sub-queries
128898 */
128899 for(i=0; i<pTabList->nSrc; i++){
128900 struct SrcList_item *pItem = &pTabList->a[i];
128901 SelectDest dest;
128902 Select *pSub;
128903#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
128904 const char *zSavedAuthContext;
128905#endif
128906
128907 /* Issue SQLITE_READ authorizations with a fake column name for any
128908 ** tables that are referenced but from which no values are extracted.
128909 ** Examples of where these kinds of null SQLITE_READ authorizations
128910 ** would occur:
128911 **
128912 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
128913 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
128914 **
128915 ** The fake column name is an empty string. It is possible for a table to
128916 ** have a column named by the empty string, in which case there is no way to
128917 ** distinguish between an unreferenced table and an actual reference to the
128918 ** "" column. The original design was for the fake column name to be a NULL,
128919 ** which would be unambiguous. But legacy authorization callbacks might
128920 ** assume the column name is non-NULL and segfault. The use of an empty
128921 ** string for the fake column name seems safer.
128922 */
128923 if( pItem->colUsed==0 ){
128924 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
128925 }
128926
128927#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
128928 /* Generate code for all sub-queries in the FROM clause
128929 */
128930 pSub = pItem->pSelect;
128931 if( pSub==0 ) continue;
128932
128933 /* Sometimes the code for a subquery will be generated more than
128934 ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
128935 ** for example. In that case, do not regenerate the code to manifest
128936 ** a view or the co-routine to implement a view. The first instance
128937 ** is sufficient, though the subroutine to manifest the view does need
128938 ** to be invoked again. */
128939 if( pItem->addrFillSub ){
128940 if( pItem->fg.viaCoroutine==0 ){
128941 /* The subroutine that manifests the view might be a one-time routine,
128942 ** or it might need to be rerun on each iteration because it
128943 ** encodes a correlated subquery. */
128944 testcase( sqlite3VdbeGetOp(v, pItem->addrFillSub)->opcode==OP_Once );
128945 sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
128946 }
128947 continue;
128948 }
128949
128950 /* Increment Parse.nHeight by the height of the largest expression
128951 ** tree referred to by this, the parent select. The child select
128952 ** may contain expression trees of at most
128953 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
128954 ** more conservative than necessary, but much easier than enforcing
128955 ** an exact limit.
128956 */
128957 pParse->nHeight += sqlite3SelectExprHeight(p);
128958
128959 /* Make copies of constant WHERE-clause terms in the outer query down
128960 ** inside the subquery. This can help the subquery to run more efficiently.
128961 */
128962 if( OptimizationEnabled(db, SQLITE_PushDown)
128963 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
128964 (pItem->fg.jointype & JT_OUTER)!=0)
128965 ){
128966#if SELECTTRACE_ENABLED
128967 if( sqlite3SelectTrace & 0x100 ){
128968 SELECTTRACE(0x100,pParse,p,
128969 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
128970 sqlite3TreeViewSelect(0, p, 0);
128971 }
128972#endif
128973 }else{
128974 SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
128975 }
128976
128977 zSavedAuthContext = pParse->zAuthContext;
128978 pParse->zAuthContext = pItem->zName;
128979
128980 /* Generate code to implement the subquery
128981 **
128982 ** The subquery is implemented as a co-routine if the subquery is
128983 ** guaranteed to be the outer loop (so that it does not need to be
128984 ** computed more than once)
128985 **
128986 ** TODO: Are there other reasons beside (1) to use a co-routine
128987 ** implementation?
128988 */
128989 if( i==0
128990 && (pTabList->nSrc==1
128991 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0) /* (1) */
128992 ){
128993 /* Implement a co-routine that will return a single row of the result
128994 ** set on each invocation.
128995 */
128996 int addrTop = sqlite3VdbeCurrentAddr(v)+1;
128997
128998 pItem->regReturn = ++pParse->nMem;
128999 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
129000 VdbeComment((v, "%s", pItem->pTab->zName));
129001 pItem->addrFillSub = addrTop;
129002 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
129003 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %u", pSub->selId));
129004 sqlite3Select(pParse, pSub, &dest);
129005 pItem->pTab->nRowLogEst = pSub->nSelectRow;
129006 pItem->fg.viaCoroutine = 1;
129007 pItem->regResult = dest.iSdst;
129008 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
129009 sqlite3VdbeJumpHere(v, addrTop-1);
129010 sqlite3ClearTempRegCache(pParse);
129011 }else{
129012 /* Generate a subroutine that will fill an ephemeral table with
129013 ** the content of this subquery. pItem->addrFillSub will point
129014 ** to the address of the generated subroutine. pItem->regReturn
129015 ** is a register allocated to hold the subroutine return address
129016 */
129017 int topAddr;
129018 int onceAddr = 0;
129019 int retAddr;
129020 struct SrcList_item *pPrior;
129021
129022 assert( pItem->addrFillSub==0 );
129023 pItem->regReturn = ++pParse->nMem;
129024 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
129025 pItem->addrFillSub = topAddr+1;
129026 if( pItem->fg.isCorrelated==0 ){
129027 /* If the subquery is not correlated and if we are not inside of
129028 ** a trigger, then we only need to compute the value of the subquery
129029 ** once. */
129030 onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
129031 VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
129032 }else{
129033 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
129034 }
129035 pPrior = isSelfJoinView(pTabList, pItem);
129036 if( pPrior ){
129037 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
129038 assert( pPrior->pSelect!=0 );
129039 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
129040 }else{
129041 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
129042 ExplainQueryPlan((pParse, 1, "MATERIALIZE %u", pSub->selId));
129043 sqlite3Select(pParse, pSub, &dest);
129044 }
129045 pItem->pTab->nRowLogEst = pSub->nSelectRow;
129046 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
129047 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
129048 VdbeComment((v, "end %s", pItem->pTab->zName));
129049 sqlite3VdbeChangeP1(v, topAddr, retAddr);
129050 sqlite3ClearTempRegCache(pParse);
129051 }
129052 if( db->mallocFailed ) goto select_end;
129053 pParse->nHeight -= sqlite3SelectExprHeight(p);
129054 pParse->zAuthContext = zSavedAuthContext;
129055#endif
129056 }
129057
129058 /* Various elements of the SELECT copied into local variables for
129059 ** convenience */
129060 pEList = p->pEList;
129061 pWhere = p->pWhere;
129062 pGroupBy = p->pGroupBy;
129063 pHaving = p->pHaving;
129064 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
129065
129066#if SELECTTRACE_ENABLED
129067 if( sqlite3SelectTrace & 0x400 ){
129068 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
129069 sqlite3TreeViewSelect(0, p, 0);
129070 }
129071#endif
129072
129073 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
129074 ** if the select-list is the same as the ORDER BY list, then this query
129075 ** can be rewritten as a GROUP BY. In other words, this:
129076 **
129077 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
129078 **
129079 ** is transformed to:
129080 **
129081 ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
129082 **
129083 ** The second form is preferred as a single index (or temp-table) may be
129084 ** used for both the ORDER BY and DISTINCT processing. As originally
129085 ** written the query must use a temp-table for at least one of the ORDER
129086 ** BY and DISTINCT, and an index or separate temp-table for the other.
129087 */
129088 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
129089 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
129090 ){
129091 p->selFlags &= ~SF_Distinct;
129092 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
129093 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
129094 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
129095 ** original setting of the SF_Distinct flag, not the current setting */
129096 assert( sDistinct.isTnct );
129097
129098#if SELECTTRACE_ENABLED
129099 if( sqlite3SelectTrace & 0x400 ){
129100 SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
129101 sqlite3TreeViewSelect(0, p, 0);
129102 }
129103#endif
129104 }
129105
129106 /* If there is an ORDER BY clause, then create an ephemeral index to
129107 ** do the sorting. But this sorting ephemeral index might end up
129108 ** being unused if the data can be extracted in pre-sorted order.
129109 ** If that is the case, then the OP_OpenEphemeral instruction will be
129110 ** changed to an OP_Noop once we figure out that the sorting index is
129111 ** not needed. The sSort.addrSortIndex variable is used to facilitate
129112 ** that change.
129113 */
129114 if( sSort.pOrderBy ){
129115 KeyInfo *pKeyInfo;
129116 pKeyInfo = sqlite3KeyInfoFromExprList(
129117 pParse, sSort.pOrderBy, 0, pEList->nExpr);
129118 sSort.iECursor = pParse->nTab++;
129119 sSort.addrSortIndex =
129120 sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
129121 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
129122 (char*)pKeyInfo, P4_KEYINFO
129123 );
129124 }else{
129125 sSort.addrSortIndex = -1;
129126 }
129127
129128 /* If the output is destined for a temporary table, open that table.
129129 */
129130 if( pDest->eDest==SRT_EphemTab ){
129131 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
129132 }
129133
129134 /* Set the limiter.
129135 */
129136 iEnd = sqlite3VdbeMakeLabel(v);
129137 if( (p->selFlags & SF_FixedLimit)==0 ){
129138 p->nSelectRow = 320; /* 4 billion rows */
129139 }
129140 computeLimitRegisters(pParse, p, iEnd);
129141 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
129142 sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen);
129143 sSort.sortFlags |= SORTFLAG_UseSorter;
129144 }
129145
129146 /* Open an ephemeral index to use for the distinct set.
129147 */
129148 if( p->selFlags & SF_Distinct ){
129149 sDistinct.tabTnct = pParse->nTab++;
129150 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
129151 sDistinct.tabTnct, 0, 0,
129152 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
129153 P4_KEYINFO);
129154 sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
129155 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
129156 }else{
129157 sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
129158 }
129159
129160 if( !isAgg && pGroupBy==0 ){
129161 /* No aggregate functions and no GROUP BY clause */
129162 u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0)
129163 | (p->selFlags & SF_FixedLimit);
129164#ifndef SQLITE_OMIT_WINDOWFUNC
129165 Window *pWin = p->pWin; /* Master window object (or NULL) */
129166 if( pWin ){
129167 sqlite3WindowCodeInit(pParse, pWin);
129168 }
129169#endif
129170 assert( WHERE_USE_LIMIT==SF_FixedLimit );
129171
129172
129173 /* Begin the database scan. */
129174 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
129175 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
129176 p->pEList, wctrlFlags, p->nSelectRow);
129177 if( pWInfo==0 ) goto select_end;
129178 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
129179 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
129180 }
129181 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
129182 sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
129183 }
129184 if( sSort.pOrderBy ){
129185 sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
129186 sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
129187 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
129188 sSort.pOrderBy = 0;
129189 }
129190 }
129191
129192 /* If sorting index that was created by a prior OP_OpenEphemeral
129193 ** instruction ended up not being needed, then change the OP_OpenEphemeral
129194 ** into an OP_Noop.
129195 */
129196 if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
129197 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
129198 }
129199
129200 assert( p->pEList==pEList );
129201#ifndef SQLITE_OMIT_WINDOWFUNC
129202 if( pWin ){
129203 int addrGosub = sqlite3VdbeMakeLabel(v);
129204 int iCont = sqlite3VdbeMakeLabel(v);
129205 int iBreak = sqlite3VdbeMakeLabel(v);
129206 int regGosub = ++pParse->nMem;
129207
129208 sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
129209
129210 sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
129211 sqlite3VdbeResolveLabel(v, addrGosub);
129212 VdbeNoopComment((v, "inner-loop subroutine"));
129213 sSort.labelOBLopt = 0;
129214 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
129215 sqlite3VdbeResolveLabel(v, iCont);
129216 sqlite3VdbeAddOp1(v, OP_Return, regGosub);
129217 VdbeComment((v, "end inner-loop subroutine"));
129218 sqlite3VdbeResolveLabel(v, iBreak);
129219 }else
129220#endif /* SQLITE_OMIT_WINDOWFUNC */
129221 {
129222 /* Use the standard inner loop. */
129223 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
129224 sqlite3WhereContinueLabel(pWInfo),
129225 sqlite3WhereBreakLabel(pWInfo));
129226
129227 /* End the database scan loop.
129228 */
129229 sqlite3WhereEnd(pWInfo);
129230 }
129231 }else{
129232 /* This case when there exist aggregate functions or a GROUP BY clause
129233 ** or both */
129234 NameContext sNC; /* Name context for processing aggregate information */
129235 int iAMem; /* First Mem address for storing current GROUP BY */
129236 int iBMem; /* First Mem address for previous GROUP BY */
129237 int iUseFlag; /* Mem address holding flag indicating that at least
129238 ** one row of the input to the aggregator has been
129239 ** processed */
129240 int iAbortFlag; /* Mem address which causes query abort if positive */
129241 int groupBySort; /* Rows come from source in GROUP BY order */
129242 int addrEnd; /* End of processing for this SELECT */
129243 int sortPTab = 0; /* Pseudotable used to decode sorting results */
129244 int sortOut = 0; /* Output register from the sorter */
129245 int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
129246
129247 /* Remove any and all aliases between the result set and the
129248 ** GROUP BY clause.
129249 */
129250 if( pGroupBy ){
129251 int k; /* Loop counter */
129252 struct ExprList_item *pItem; /* For looping over expression in a list */
129253
129254 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
129255 pItem->u.x.iAlias = 0;
129256 }
129257 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
129258 pItem->u.x.iAlias = 0;
129259 }
129260 assert( 66==sqlite3LogEst(100) );
129261 if( p->nSelectRow>66 ) p->nSelectRow = 66;
129262 }else{
129263 assert( 0==sqlite3LogEst(1) );
129264 p->nSelectRow = 0;
129265 }
129266
129267 /* If there is both a GROUP BY and an ORDER BY clause and they are
129268 ** identical, then it may be possible to disable the ORDER BY clause
129269 ** on the grounds that the GROUP BY will cause elements to come out
129270 ** in the correct order. It also may not - the GROUP BY might use a
129271 ** database index that causes rows to be grouped together as required
129272 ** but not actually sorted. Either way, record the fact that the
129273 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
129274 ** variable. */
129275 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
129276 orderByGrp = 1;
129277 }
129278
129279 /* Create a label to jump to when we want to abort the query */
129280 addrEnd = sqlite3VdbeMakeLabel(v);
129281
129282 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
129283 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
129284 ** SELECT statement.
129285 */
129286 memset(&sNC, 0, sizeof(sNC));
129287 sNC.pParse = pParse;
129288 sNC.pSrcList = pTabList;
129289 sNC.uNC.pAggInfo = &sAggInfo;
129290 VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
129291 sAggInfo.mnReg = pParse->nMem+1;
129292 sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
129293 sAggInfo.pGroupBy = pGroupBy;
129294 sqlite3ExprAnalyzeAggList(&sNC, pEList);
129295 sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
129296 if( pHaving ){
129297 if( pGroupBy ){
129298 assert( pWhere==p->pWhere );
129299 assert( pHaving==p->pHaving );
129300 assert( pGroupBy==p->pGroupBy );
129301 havingToWhere(pParse, p);
129302 pWhere = p->pWhere;
129303 }
129304 sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
129305 }
129306 sAggInfo.nAccumulator = sAggInfo.nColumn;
129307 if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
129308 minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
129309 }else{
129310 minMaxFlag = WHERE_ORDERBY_NORMAL;
129311 }
129312 for(i=0; i<sAggInfo.nFunc; i++){
129313 assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
129314 sNC.ncFlags |= NC_InAggFunc;
129315 sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
129316 sNC.ncFlags &= ~NC_InAggFunc;
129317 }
129318 sAggInfo.mxReg = pParse->nMem;
129319 if( db->mallocFailed ) goto select_end;
129320#if SELECTTRACE_ENABLED
129321 if( sqlite3SelectTrace & 0x400 ){
129322 int ii;
129323 SELECTTRACE(0x400,pParse,p,("After aggregate analysis:\n"));
129324 sqlite3TreeViewSelect(0, p, 0);
129325 for(ii=0; ii<sAggInfo.nColumn; ii++){
129326 sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
129327 ii, sAggInfo.aCol[ii].iMem);
129328 sqlite3TreeViewExpr(0, sAggInfo.aCol[ii].pExpr, 0);
129329 }
129330 for(ii=0; ii<sAggInfo.nFunc; ii++){
129331 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
129332 ii, sAggInfo.aFunc[ii].iMem);
129333 sqlite3TreeViewExpr(0, sAggInfo.aFunc[ii].pExpr, 0);
129334 }
129335 }
129336#endif
129337
129338
129339 /* Processing for aggregates with GROUP BY is very different and
129340 ** much more complex than aggregates without a GROUP BY.
129341 */
129342 if( pGroupBy ){
129343 KeyInfo *pKeyInfo; /* Keying information for the group by clause */
129344 int addr1; /* A-vs-B comparision jump */
129345 int addrOutputRow; /* Start of subroutine that outputs a result row */
129346 int regOutputRow; /* Return address register for output subroutine */
129347 int addrSetAbort; /* Set the abort flag and return */
129348 int addrTopOfLoop; /* Top of the input loop */
129349 int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
129350 int addrReset; /* Subroutine for resetting the accumulator */
129351 int regReset; /* Return address register for reset subroutine */
129352
129353 /* If there is a GROUP BY clause we might need a sorting index to
129354 ** implement it. Allocate that sorting index now. If it turns out
129355 ** that we do not need it after all, the OP_SorterOpen instruction
129356 ** will be converted into a Noop.
129357 */
129358 sAggInfo.sortingIdx = pParse->nTab++;
129359 pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pGroupBy,0,sAggInfo.nColumn);
129360 addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
129361 sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
129362 0, (char*)pKeyInfo, P4_KEYINFO);
129363
129364 /* Initialize memory locations used by GROUP BY aggregate processing
129365 */
129366 iUseFlag = ++pParse->nMem;
129367 iAbortFlag = ++pParse->nMem;
129368 regOutputRow = ++pParse->nMem;
129369 addrOutputRow = sqlite3VdbeMakeLabel(v);
129370 regReset = ++pParse->nMem;
129371 addrReset = sqlite3VdbeMakeLabel(v);
129372 iAMem = pParse->nMem + 1;
129373 pParse->nMem += pGroupBy->nExpr;
129374 iBMem = pParse->nMem + 1;
129375 pParse->nMem += pGroupBy->nExpr;
129376 sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
129377 VdbeComment((v, "clear abort flag"));
129378 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
129379
129380 /* Begin a loop that will extract all source rows in GROUP BY order.
129381 ** This might involve two separate loops with an OP_Sort in between, or
129382 ** it might be a single loop that uses an index to extract information
129383 ** in the right order to begin with.
129384 */
129385 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
129386 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
129387 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
129388 WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
129389 );
129390 if( pWInfo==0 ) goto select_end;
129391 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
129392 /* The optimizer is able to deliver rows in group by order so
129393 ** we do not have to sort. The OP_OpenEphemeral table will be
129394 ** cancelled later because we still need to use the pKeyInfo
129395 */
129396 groupBySort = 0;
129397 }else{
129398 /* Rows are coming out in undetermined order. We have to push
129399 ** each row into a sorting index, terminate the first loop,
129400 ** then loop over the sorting index in order to get the output
129401 ** in sorted order
129402 */
129403 int regBase;
129404 int regRecord;
129405 int nCol;
129406 int nGroupBy;
129407
129408 explainTempTable(pParse,
129409 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
129410 "DISTINCT" : "GROUP BY");
129411
129412 groupBySort = 1;
129413 nGroupBy = pGroupBy->nExpr;
129414 nCol = nGroupBy;
129415 j = nGroupBy;
129416 for(i=0; i<sAggInfo.nColumn; i++){
129417 if( sAggInfo.aCol[i].iSorterColumn>=j ){
129418 nCol++;
129419 j++;
129420 }
129421 }
129422 regBase = sqlite3GetTempRange(pParse, nCol);
129423 sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
129424 j = nGroupBy;
129425 for(i=0; i<sAggInfo.nColumn; i++){
129426 struct AggInfo_col *pCol = &sAggInfo.aCol[i];
129427 if( pCol->iSorterColumn>=j ){
129428 int r1 = j + regBase;
129429 sqlite3ExprCodeGetColumnOfTable(v,
129430 pCol->pTab, pCol->iTable, pCol->iColumn, r1);
129431 j++;
129432 }
129433 }
129434 regRecord = sqlite3GetTempReg(pParse);
129435 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
129436 sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
129437 sqlite3ReleaseTempReg(pParse, regRecord);
129438 sqlite3ReleaseTempRange(pParse, regBase, nCol);
129439 sqlite3WhereEnd(pWInfo);
129440 sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
129441 sortOut = sqlite3GetTempReg(pParse);
129442 sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
129443 sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
129444 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
129445 sAggInfo.useSortingIdx = 1;
129446 }
129447
129448 /* If the index or temporary table used by the GROUP BY sort
129449 ** will naturally deliver rows in the order required by the ORDER BY
129450 ** clause, cancel the ephemeral table open coded earlier.
129451 **
129452 ** This is an optimization - the correct answer should result regardless.
129453 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
129454 ** disable this optimization for testing purposes. */
129455 if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)
129456 && (groupBySort || sqlite3WhereIsSorted(pWInfo))
129457 ){
129458 sSort.pOrderBy = 0;
129459 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
129460 }
129461
129462 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
129463 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
129464 ** Then compare the current GROUP BY terms against the GROUP BY terms
129465 ** from the previous row currently stored in a0, a1, a2...
129466 */
129467 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
129468 if( groupBySort ){
129469 sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
129470 sortOut, sortPTab);
129471 }
129472 for(j=0; j<pGroupBy->nExpr; j++){
129473 if( groupBySort ){
129474 sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
129475 }else{
129476 sAggInfo.directMode = 1;
129477 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
129478 }
129479 }
129480 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
129481 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
129482 addr1 = sqlite3VdbeCurrentAddr(v);
129483 sqlite3VdbeAddOp3(v, OP_Jump, addr1+1, 0, addr1+1); VdbeCoverage(v);
129484
129485 /* Generate code that runs whenever the GROUP BY changes.
129486 ** Changes in the GROUP BY are detected by the previous code
129487 ** block. If there were no changes, this block is skipped.
129488 **
129489 ** This code copies current group by terms in b0,b1,b2,...
129490 ** over to a0,a1,a2. It then calls the output subroutine
129491 ** and resets the aggregate accumulator registers in preparation
129492 ** for the next GROUP BY batch.
129493 */
129494 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
129495 sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
129496 VdbeComment((v, "output one row"));
129497 sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v);
129498 VdbeComment((v, "check abort flag"));
129499 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
129500 VdbeComment((v, "reset accumulator"));
129501
129502 /* Update the aggregate accumulators based on the content of
129503 ** the current row
129504 */
129505 sqlite3VdbeJumpHere(v, addr1);
129506 updateAccumulator(pParse, iUseFlag, &sAggInfo);
129507 sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
129508 VdbeComment((v, "indicate data in accumulator"));
129509
129510 /* End of the loop
129511 */
129512 if( groupBySort ){
129513 sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
129514 VdbeCoverage(v);
129515 }else{
129516 sqlite3WhereEnd(pWInfo);
129517 sqlite3VdbeChangeToNoop(v, addrSortingIdx);
129518 }
129519
129520 /* Output the final row of result
129521 */
129522 sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
129523 VdbeComment((v, "output final row"));
129524
129525 /* Jump over the subroutines
129526 */
129527 sqlite3VdbeGoto(v, addrEnd);
129528
129529 /* Generate a subroutine that outputs a single row of the result
129530 ** set. This subroutine first looks at the iUseFlag. If iUseFlag
129531 ** is less than or equal to zero, the subroutine is a no-op. If
129532 ** the processing calls for the query to abort, this subroutine
129533 ** increments the iAbortFlag memory location before returning in
129534 ** order to signal the caller to abort.
129535 */
129536 addrSetAbort = sqlite3VdbeCurrentAddr(v);
129537 sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
129538 VdbeComment((v, "set abort flag"));
129539 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
129540 sqlite3VdbeResolveLabel(v, addrOutputRow);
129541 addrOutputRow = sqlite3VdbeCurrentAddr(v);
129542 sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
129543 VdbeCoverage(v);
129544 VdbeComment((v, "Groupby result generator entry point"));
129545 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
129546 finalizeAggFunctions(pParse, &sAggInfo);
129547 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
129548 selectInnerLoop(pParse, p, -1, &sSort,
129549 &sDistinct, pDest,
129550 addrOutputRow+1, addrSetAbort);
129551 sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
129552 VdbeComment((v, "end groupby result generator"));
129553
129554 /* Generate a subroutine that will reset the group-by accumulator
129555 */
129556 sqlite3VdbeResolveLabel(v, addrReset);
129557 resetAccumulator(pParse, &sAggInfo);
129558 sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
129559 VdbeComment((v, "indicate accumulator empty"));
129560 sqlite3VdbeAddOp1(v, OP_Return, regReset);
129561
129562 } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
129563 else {
129564#ifndef SQLITE_OMIT_BTREECOUNT
129565 Table *pTab;
129566 if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
129567 /* If isSimpleCount() returns a pointer to a Table structure, then
129568 ** the SQL statement is of the form:
129569 **
129570 ** SELECT count(*) FROM <tbl>
129571 **
129572 ** where the Table structure returned represents table <tbl>.
129573 **
129574 ** This statement is so common that it is optimized specially. The
129575 ** OP_Count instruction is executed either on the intkey table that
129576 ** contains the data for table <tbl> or on one of its indexes. It
129577 ** is better to execute the op on an index, as indexes are almost
129578 ** always spread across less pages than their corresponding tables.
129579 */
129580 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
129581 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
129582 Index *pIdx; /* Iterator variable */
129583 KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
129584 Index *pBest = 0; /* Best index found so far */
129585 int iRoot = pTab->tnum; /* Root page of scanned b-tree */
129586
129587 sqlite3CodeVerifySchema(pParse, iDb);
129588 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
129589
129590 /* Search for the index that has the lowest scan cost.
129591 **
129592 ** (2011-04-15) Do not do a full scan of an unordered index.
129593 **
129594 ** (2013-10-03) Do not count the entries in a partial index.
129595 **
129596 ** In practice the KeyInfo structure will not be used. It is only
129597 ** passed to keep OP_OpenRead happy.
129598 */
129599 if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
129600 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
129601 if( pIdx->bUnordered==0
129602 && pIdx->szIdxRow<pTab->szTabRow
129603 && pIdx->pPartIdxWhere==0
129604 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
129605 ){
129606 pBest = pIdx;
129607 }
129608 }
129609 if( pBest ){
129610 iRoot = pBest->tnum;
129611 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
129612 }
129613
129614 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
129615 sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1);
129616 if( pKeyInfo ){
129617 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
129618 }
129619 sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
129620 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
129621 explainSimpleCount(pParse, pTab, pBest);
129622 }else
129623#endif /* SQLITE_OMIT_BTREECOUNT */
129624 {
129625 int regAcc = 0; /* "populate accumulators" flag */
129626
129627 /* If there are accumulator registers but no min() or max() functions,
129628 ** allocate register regAcc. Register regAcc will contain 0 the first
129629 ** time the inner loop runs, and 1 thereafter. The code generated
129630 ** by updateAccumulator() only updates the accumulator registers if
129631 ** regAcc contains 0. */
129632 if( sAggInfo.nAccumulator ){
129633 for(i=0; i<sAggInfo.nFunc; i++){
129634 if( sAggInfo.aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ) break;
129635 }
129636 if( i==sAggInfo.nFunc ){
129637 regAcc = ++pParse->nMem;
129638 sqlite3VdbeAddOp2(v, OP_Integer, 0, regAcc);
129639 }
129640 }
129641
129642 /* This case runs if the aggregate has no GROUP BY clause. The
129643 ** processing is much simpler since there is only a single row
129644 ** of output.
129645 */
129646 assert( p->pGroupBy==0 );
129647 resetAccumulator(pParse, &sAggInfo);
129648
129649 /* If this query is a candidate for the min/max optimization, then
129650 ** minMaxFlag will have been previously set to either
129651 ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
129652 ** be an appropriate ORDER BY expression for the optimization.
129653 */
129654 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
129655 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
129656
129657 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
129658 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
129659 0, minMaxFlag, 0);
129660 if( pWInfo==0 ){
129661 goto select_end;
129662 }
129663 updateAccumulator(pParse, regAcc, &sAggInfo);
129664 if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc);
129665 if( sqlite3WhereIsOrdered(pWInfo)>0 ){
129666 sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo));
129667 VdbeComment((v, "%s() by index",
129668 (minMaxFlag==WHERE_ORDERBY_MIN?"min":"max")));
129669 }
129670 sqlite3WhereEnd(pWInfo);
129671 finalizeAggFunctions(pParse, &sAggInfo);
129672 }
129673
129674 sSort.pOrderBy = 0;
129675 sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
129676 selectInnerLoop(pParse, p, -1, 0, 0,
129677 pDest, addrEnd, addrEnd);
129678 }
129679 sqlite3VdbeResolveLabel(v, addrEnd);
129680
129681 } /* endif aggregate query */
129682
129683 if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
129684 explainTempTable(pParse, "DISTINCT");
129685 }
129686
129687 /* If there is an ORDER BY clause, then we need to sort the results
129688 ** and send them to the callback one by one.
129689 */
129690 if( sSort.pOrderBy ){
129691 explainTempTable(pParse,
129692 sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
129693 assert( p->pEList==pEList );
129694 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
129695 }
129696
129697 /* Jump here to skip this query
129698 */
129699 sqlite3VdbeResolveLabel(v, iEnd);
129700
129701 /* The SELECT has been coded. If there is an error in the Parse structure,
129702 ** set the return code to 1. Otherwise 0. */
129703 rc = (pParse->nErr>0);
129704
129705 /* Control jumps to here if an error is encountered above, or upon
129706 ** successful coding of the SELECT.
129707 */
129708select_end:
129709 sqlite3ExprListDelete(db, pMinMaxOrderBy);
129710 sqlite3DbFree(db, sAggInfo.aCol);
129711 sqlite3DbFree(db, sAggInfo.aFunc);
129712#if SELECTTRACE_ENABLED
129713 SELECTTRACE(0x1,pParse,p,("end processing\n"));
129714 if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
129715 sqlite3TreeViewSelect(0, p, 0);
129716 }
129717#endif
129718 ExplainQueryPlanPop(pParse);
129719 return rc;
129720}
129721
129722/************** End of select.c **********************************************/
129723/************** Begin file table.c *******************************************/
129724/*
129725** 2001 September 15
129726**
129727** The author disclaims copyright to this source code. In place of
129728** a legal notice, here is a blessing:
129729**
129730** May you do good and not evil.
129731** May you find forgiveness for yourself and forgive others.
129732** May you share freely, never taking more than you give.
129733**
129734*************************************************************************
129735** This file contains the sqlite3_get_table() and sqlite3_free_table()
129736** interface routines. These are just wrappers around the main
129737** interface routine of sqlite3_exec().
129738**
129739** These routines are in a separate files so that they will not be linked
129740** if they are not used.
129741*/
129742/* #include "sqliteInt.h" */
129743
129744#ifndef SQLITE_OMIT_GET_TABLE
129745
129746/*
129747** This structure is used to pass data from sqlite3_get_table() through
129748** to the callback function is uses to build the result.
129749*/
129750typedef struct TabResult {
129751 char **azResult; /* Accumulated output */
129752 char *zErrMsg; /* Error message text, if an error occurs */
129753 u32 nAlloc; /* Slots allocated for azResult[] */
129754 u32 nRow; /* Number of rows in the result */
129755 u32 nColumn; /* Number of columns in the result */
129756 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
129757 int rc; /* Return code from sqlite3_exec() */
129758} TabResult;
129759
129760/*
129761** This routine is called once for each row in the result table. Its job
129762** is to fill in the TabResult structure appropriately, allocating new
129763** memory as necessary.
129764*/
129765static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
129766 TabResult *p = (TabResult*)pArg; /* Result accumulator */
129767 int need; /* Slots needed in p->azResult[] */
129768 int i; /* Loop counter */
129769 char *z; /* A single column of result */
129770
129771 /* Make sure there is enough space in p->azResult to hold everything
129772 ** we need to remember from this invocation of the callback.
129773 */
129774 if( p->nRow==0 && argv!=0 ){
129775 need = nCol*2;
129776 }else{
129777 need = nCol;
129778 }
129779 if( p->nData + need > p->nAlloc ){
129780 char **azNew;
129781 p->nAlloc = p->nAlloc*2 + need;
129782 azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc );
129783 if( azNew==0 ) goto malloc_failed;
129784 p->azResult = azNew;
129785 }
129786
129787 /* If this is the first row, then generate an extra row containing
129788 ** the names of all columns.
129789 */
129790 if( p->nRow==0 ){
129791 p->nColumn = nCol;
129792 for(i=0; i<nCol; i++){
129793 z = sqlite3_mprintf("%s", colv[i]);
129794 if( z==0 ) goto malloc_failed;
129795 p->azResult[p->nData++] = z;
129796 }
129797 }else if( (int)p->nColumn!=nCol ){
129798 sqlite3_free(p->zErrMsg);
129799 p->zErrMsg = sqlite3_mprintf(
129800 "sqlite3_get_table() called with two or more incompatible queries"
129801 );
129802 p->rc = SQLITE_ERROR;
129803 return 1;
129804 }
129805
129806 /* Copy over the row data
129807 */
129808 if( argv!=0 ){
129809 for(i=0; i<nCol; i++){
129810 if( argv[i]==0 ){
129811 z = 0;
129812 }else{
129813 int n = sqlite3Strlen30(argv[i])+1;
129814 z = sqlite3_malloc64( n );
129815 if( z==0 ) goto malloc_failed;
129816 memcpy(z, argv[i], n);
129817 }
129818 p->azResult[p->nData++] = z;
129819 }
129820 p->nRow++;
129821 }
129822 return 0;
129823
129824malloc_failed:
129825 p->rc = SQLITE_NOMEM_BKPT;
129826 return 1;
129827}
129828
129829/*
129830** Query the database. But instead of invoking a callback for each row,
129831** malloc() for space to hold the result and return the entire results
129832** at the conclusion of the call.
129833**
129834** The result that is written to ***pazResult is held in memory obtained
129835** from malloc(). But the caller cannot free this memory directly.
129836** Instead, the entire table should be passed to sqlite3_free_table() when
129837** the calling procedure is finished using it.
129838*/
129839SQLITE_API int sqlite3_get_table(
129840 sqlite3 *db, /* The database on which the SQL executes */
129841 const char *zSql, /* The SQL to be executed */
129842 char ***pazResult, /* Write the result table here */
129843 int *pnRow, /* Write the number of rows in the result here */
129844 int *pnColumn, /* Write the number of columns of result here */
129845 char **pzErrMsg /* Write error messages here */
129846){
129847 int rc;
129848 TabResult res;
129849
129850#ifdef SQLITE_ENABLE_API_ARMOR
129851 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT;
129852#endif
129853 *pazResult = 0;
129854 if( pnColumn ) *pnColumn = 0;
129855 if( pnRow ) *pnRow = 0;
129856 if( pzErrMsg ) *pzErrMsg = 0;
129857 res.zErrMsg = 0;
129858 res.nRow = 0;
129859 res.nColumn = 0;
129860 res.nData = 1;
129861 res.nAlloc = 20;
129862 res.rc = SQLITE_OK;
129863 res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
129864 if( res.azResult==0 ){
129865 db->errCode = SQLITE_NOMEM;
129866 return SQLITE_NOMEM_BKPT;
129867 }
129868 res.azResult[0] = 0;
129869 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
129870 assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
129871 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
129872 if( (rc&0xff)==SQLITE_ABORT ){
129873 sqlite3_free_table(&res.azResult[1]);
129874 if( res.zErrMsg ){
129875 if( pzErrMsg ){
129876 sqlite3_free(*pzErrMsg);
129877 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
129878 }
129879 sqlite3_free(res.zErrMsg);
129880 }
129881 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
129882 return res.rc;
129883 }
129884 sqlite3_free(res.zErrMsg);
129885 if( rc!=SQLITE_OK ){
129886 sqlite3_free_table(&res.azResult[1]);
129887 return rc;
129888 }
129889 if( res.nAlloc>res.nData ){
129890 char **azNew;
129891 azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData );
129892 if( azNew==0 ){
129893 sqlite3_free_table(&res.azResult[1]);
129894 db->errCode = SQLITE_NOMEM;
129895 return SQLITE_NOMEM_BKPT;
129896 }
129897 res.azResult = azNew;
129898 }
129899 *pazResult = &res.azResult[1];
129900 if( pnColumn ) *pnColumn = res.nColumn;
129901 if( pnRow ) *pnRow = res.nRow;
129902 return rc;
129903}
129904
129905/*
129906** This routine frees the space the sqlite3_get_table() malloced.
129907*/
129908SQLITE_API void sqlite3_free_table(
129909 char **azResult /* Result returned from sqlite3_get_table() */
129910){
129911 if( azResult ){
129912 int i, n;
129913 azResult--;
129914 assert( azResult!=0 );
129915 n = SQLITE_PTR_TO_INT(azResult[0]);
129916 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
129917 sqlite3_free(azResult);
129918 }
129919}
129920
129921#endif /* SQLITE_OMIT_GET_TABLE */
129922
129923/************** End of table.c ***********************************************/
129924/************** Begin file trigger.c *****************************************/
129925/*
129926**
129927** The author disclaims copyright to this source code. In place of
129928** a legal notice, here is a blessing:
129929**
129930** May you do good and not evil.
129931** May you find forgiveness for yourself and forgive others.
129932** May you share freely, never taking more than you give.
129933**
129934*************************************************************************
129935** This file contains the implementation for TRIGGERs
129936*/
129937/* #include "sqliteInt.h" */
129938
129939#ifndef SQLITE_OMIT_TRIGGER
129940/*
129941** Delete a linked list of TriggerStep structures.
129942*/
129943SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
129944 while( pTriggerStep ){
129945 TriggerStep * pTmp = pTriggerStep;
129946 pTriggerStep = pTriggerStep->pNext;
129947
129948 sqlite3ExprDelete(db, pTmp->pWhere);
129949 sqlite3ExprListDelete(db, pTmp->pExprList);
129950 sqlite3SelectDelete(db, pTmp->pSelect);
129951 sqlite3IdListDelete(db, pTmp->pIdList);
129952 sqlite3UpsertDelete(db, pTmp->pUpsert);
129953 sqlite3DbFree(db, pTmp->zSpan);
129954
129955 sqlite3DbFree(db, pTmp);
129956 }
129957}
129958
129959/*
129960** Given table pTab, return a list of all the triggers attached to
129961** the table. The list is connected by Trigger.pNext pointers.
129962**
129963** All of the triggers on pTab that are in the same database as pTab
129964** are already attached to pTab->pTrigger. But there might be additional
129965** triggers on pTab in the TEMP schema. This routine prepends all
129966** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
129967** and returns the combined list.
129968**
129969** To state it another way: This routine returns a list of all triggers
129970** that fire off of pTab. The list will include any TEMP triggers on
129971** pTab as well as the triggers lised in pTab->pTrigger.
129972*/
129973SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
129974 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
129975 Trigger *pList = 0; /* List of triggers to return */
129976
129977 if( pParse->disableTriggers ){
129978 return 0;
129979 }
129980
129981 if( pTmpSchema!=pTab->pSchema ){
129982 HashElem *p;
129983 assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
129984 for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
129985 Trigger *pTrig = (Trigger *)sqliteHashData(p);
129986 if( pTrig->pTabSchema==pTab->pSchema
129987 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
129988 ){
129989 pTrig->pNext = (pList ? pList : pTab->pTrigger);
129990 pList = pTrig;
129991 }
129992 }
129993 }
129994
129995 return (pList ? pList : pTab->pTrigger);
129996}
129997
129998/*
129999** This is called by the parser when it sees a CREATE TRIGGER statement
130000** up to the point of the BEGIN before the trigger actions. A Trigger
130001** structure is generated based on the information available and stored
130002** in pParse->pNewTrigger. After the trigger actions have been parsed, the
130003** sqlite3FinishTrigger() function is called to complete the trigger
130004** construction process.
130005*/
130006SQLITE_PRIVATE void sqlite3BeginTrigger(
130007 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
130008 Token *pName1, /* The name of the trigger */
130009 Token *pName2, /* The name of the trigger */
130010 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
130011 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
130012 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
130013 SrcList *pTableName,/* The name of the table/view the trigger applies to */
130014 Expr *pWhen, /* WHEN clause */
130015 int isTemp, /* True if the TEMPORARY keyword is present */
130016 int noErr /* Suppress errors if the trigger already exists */
130017){
130018 Trigger *pTrigger = 0; /* The new trigger */
130019 Table *pTab; /* Table that the trigger fires off of */
130020 char *zName = 0; /* Name of the trigger */
130021 sqlite3 *db = pParse->db; /* The database connection */
130022 int iDb; /* The database to store the trigger in */
130023 Token *pName; /* The unqualified db name */
130024 DbFixer sFix; /* State vector for the DB fixer */
130025
130026 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
130027 assert( pName2!=0 );
130028 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
130029 assert( op>0 && op<0xff );
130030 if( isTemp ){
130031 /* If TEMP was specified, then the trigger name may not be qualified. */
130032 if( pName2->n>0 ){
130033 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
130034 goto trigger_cleanup;
130035 }
130036 iDb = 1;
130037 pName = pName1;
130038 }else{
130039 /* Figure out the db that the trigger will be created in */
130040 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
130041 if( iDb<0 ){
130042 goto trigger_cleanup;
130043 }
130044 }
130045 if( !pTableName || db->mallocFailed ){
130046 goto trigger_cleanup;
130047 }
130048
130049 /* A long-standing parser bug is that this syntax was allowed:
130050 **
130051 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
130052 ** ^^^^^^^^
130053 **
130054 ** To maintain backwards compatibility, ignore the database
130055 ** name on pTableName if we are reparsing out of SQLITE_MASTER.
130056 */
130057 if( db->init.busy && iDb!=1 ){
130058 sqlite3DbFree(db, pTableName->a[0].zDatabase);
130059 pTableName->a[0].zDatabase = 0;
130060 }
130061
130062 /* If the trigger name was unqualified, and the table is a temp table,
130063 ** then set iDb to 1 to create the trigger in the temporary database.
130064 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
130065 ** exist, the error is caught by the block below.
130066 */
130067 pTab = sqlite3SrcListLookup(pParse, pTableName);
130068 if( db->init.busy==0 && pName2->n==0 && pTab
130069 && pTab->pSchema==db->aDb[1].pSchema ){
130070 iDb = 1;
130071 }
130072
130073 /* Ensure the table name matches database name and that the table exists */
130074 if( db->mallocFailed ) goto trigger_cleanup;
130075 assert( pTableName->nSrc==1 );
130076 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
130077 if( sqlite3FixSrcList(&sFix, pTableName) ){
130078 goto trigger_cleanup;
130079 }
130080 pTab = sqlite3SrcListLookup(pParse, pTableName);
130081 if( !pTab ){
130082 /* The table does not exist. */
130083 if( db->init.iDb==1 ){
130084 /* Ticket #3810.
130085 ** Normally, whenever a table is dropped, all associated triggers are
130086 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
130087 ** and the table is dropped by a different database connection, the
130088 ** trigger is not visible to the database connection that does the
130089 ** drop so the trigger cannot be dropped. This results in an
130090 ** "orphaned trigger" - a trigger whose associated table is missing.
130091 */
130092 db->init.orphanTrigger = 1;
130093 }
130094 goto trigger_cleanup;
130095 }
130096 if( IsVirtual(pTab) ){
130097 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
130098 goto trigger_cleanup;
130099 }
130100
130101 /* Check that the trigger name is not reserved and that no trigger of the
130102 ** specified name exists */
130103 zName = sqlite3NameFromToken(db, pName);
130104 if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
130105 goto trigger_cleanup;
130106 }
130107 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
130108 if( !IN_RENAME_OBJECT ){
130109 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
130110 if( !noErr ){
130111 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
130112 }else{
130113 assert( !db->init.busy );
130114 sqlite3CodeVerifySchema(pParse, iDb);
130115 }
130116 goto trigger_cleanup;
130117 }
130118 }
130119
130120 /* Do not create a trigger on a system table */
130121 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
130122 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
130123 goto trigger_cleanup;
130124 }
130125
130126 /* INSTEAD of triggers are only for views and views only support INSTEAD
130127 ** of triggers.
130128 */
130129 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
130130 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
130131 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
130132 goto trigger_cleanup;
130133 }
130134 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
130135 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
130136 " trigger on table: %S", pTableName, 0);
130137 goto trigger_cleanup;
130138 }
130139
130140#ifndef SQLITE_OMIT_AUTHORIZATION
130141 if( !IN_RENAME_OBJECT ){
130142 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
130143 int code = SQLITE_CREATE_TRIGGER;
130144 const char *zDb = db->aDb[iTabDb].zDbSName;
130145 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
130146 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
130147 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
130148 goto trigger_cleanup;
130149 }
130150 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
130151 goto trigger_cleanup;
130152 }
130153 }
130154#endif
130155
130156 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
130157 ** cannot appear on views. So we might as well translate every
130158 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
130159 ** elsewhere.
130160 */
130161 if (tr_tm == TK_INSTEAD){
130162 tr_tm = TK_BEFORE;
130163 }
130164
130165 /* Build the Trigger object */
130166 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
130167 if( pTrigger==0 ) goto trigger_cleanup;
130168 pTrigger->zName = zName;
130169 zName = 0;
130170 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
130171 pTrigger->pSchema = db->aDb[iDb].pSchema;
130172 pTrigger->pTabSchema = pTab->pSchema;
130173 pTrigger->op = (u8)op;
130174 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
130175 if( IN_RENAME_OBJECT ){
130176 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
130177 pTrigger->pWhen = pWhen;
130178 pWhen = 0;
130179 }else{
130180 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
130181 }
130182 pTrigger->pColumns = pColumns;
130183 pColumns = 0;
130184 assert( pParse->pNewTrigger==0 );
130185 pParse->pNewTrigger = pTrigger;
130186
130187trigger_cleanup:
130188 sqlite3DbFree(db, zName);
130189 sqlite3SrcListDelete(db, pTableName);
130190 sqlite3IdListDelete(db, pColumns);
130191 sqlite3ExprDelete(db, pWhen);
130192 if( !pParse->pNewTrigger ){
130193 sqlite3DeleteTrigger(db, pTrigger);
130194 }else{
130195 assert( pParse->pNewTrigger==pTrigger );
130196 }
130197}
130198
130199/*
130200** This routine is called after all of the trigger actions have been parsed
130201** in order to complete the process of building the trigger.
130202*/
130203SQLITE_PRIVATE void sqlite3FinishTrigger(
130204 Parse *pParse, /* Parser context */
130205 TriggerStep *pStepList, /* The triggered program */
130206 Token *pAll /* Token that describes the complete CREATE TRIGGER */
130207){
130208 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
130209 char *zName; /* Name of trigger */
130210 sqlite3 *db = pParse->db; /* The database */
130211 DbFixer sFix; /* Fixer object */
130212 int iDb; /* Database containing the trigger */
130213 Token nameToken; /* Trigger name for error reporting */
130214
130215 pParse->pNewTrigger = 0;
130216 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
130217 zName = pTrig->zName;
130218 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
130219 pTrig->step_list = pStepList;
130220 while( pStepList ){
130221 pStepList->pTrig = pTrig;
130222 pStepList = pStepList->pNext;
130223 }
130224 sqlite3TokenInit(&nameToken, pTrig->zName);
130225 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
130226 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
130227 || sqlite3FixExpr(&sFix, pTrig->pWhen)
130228 ){
130229 goto triggerfinish_cleanup;
130230 }
130231
130232#ifndef SQLITE_OMIT_ALTERTABLE
130233 if( IN_RENAME_OBJECT ){
130234 assert( !db->init.busy );
130235 pParse->pNewTrigger = pTrig;
130236 pTrig = 0;
130237 }else
130238#endif
130239
130240 /* if we are not initializing,
130241 ** build the sqlite_master entry
130242 */
130243 if( !db->init.busy ){
130244 Vdbe *v;
130245 char *z;
130246
130247 /* Make an entry in the sqlite_master table */
130248 v = sqlite3GetVdbe(pParse);
130249 if( v==0 ) goto triggerfinish_cleanup;
130250 sqlite3BeginWriteOperation(pParse, 0, iDb);
130251 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
130252 testcase( z==0 );
130253 sqlite3NestedParse(pParse,
130254 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
130255 db->aDb[iDb].zDbSName, MASTER_NAME, zName,
130256 pTrig->table, z);
130257 sqlite3DbFree(db, z);
130258 sqlite3ChangeCookie(pParse, iDb);
130259 sqlite3VdbeAddParseSchemaOp(v, iDb,
130260 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
130261 }
130262
130263 if( db->init.busy ){
130264 Trigger *pLink = pTrig;
130265 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
130266 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
130267 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
130268 if( pTrig ){
130269 sqlite3OomFault(db);
130270 }else if( pLink->pSchema==pLink->pTabSchema ){
130271 Table *pTab;
130272 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
130273 assert( pTab!=0 );
130274 pLink->pNext = pTab->pTrigger;
130275 pTab->pTrigger = pLink;
130276 }
130277 }
130278
130279triggerfinish_cleanup:
130280 sqlite3DeleteTrigger(db, pTrig);
130281 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
130282 sqlite3DeleteTriggerStep(db, pStepList);
130283}
130284
130285/*
130286** Duplicate a range of text from an SQL statement, then convert all
130287** whitespace characters into ordinary space characters.
130288*/
130289static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
130290 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
130291 int i;
130292 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
130293 return z;
130294}
130295
130296/*
130297** Turn a SELECT statement (that the pSelect parameter points to) into
130298** a trigger step. Return a pointer to a TriggerStep structure.
130299**
130300** The parser calls this routine when it finds a SELECT statement in
130301** body of a TRIGGER.
130302*/
130303SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(
130304 sqlite3 *db, /* Database connection */
130305 Select *pSelect, /* The SELECT statement */
130306 const char *zStart, /* Start of SQL text */
130307 const char *zEnd /* End of SQL text */
130308){
130309 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
130310 if( pTriggerStep==0 ) {
130311 sqlite3SelectDelete(db, pSelect);
130312 return 0;
130313 }
130314 pTriggerStep->op = TK_SELECT;
130315 pTriggerStep->pSelect = pSelect;
130316 pTriggerStep->orconf = OE_Default;
130317 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
130318 return pTriggerStep;
130319}
130320
130321/*
130322** Allocate space to hold a new trigger step. The allocated space
130323** holds both the TriggerStep object and the TriggerStep.target.z string.
130324**
130325** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
130326*/
130327static TriggerStep *triggerStepAllocate(
130328 Parse *pParse, /* Parser context */
130329 u8 op, /* Trigger opcode */
130330 Token *pName, /* The target name */
130331 const char *zStart, /* Start of SQL text */
130332 const char *zEnd /* End of SQL text */
130333){
130334 sqlite3 *db = pParse->db;
130335 TriggerStep *pTriggerStep;
130336
130337 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
130338 if( pTriggerStep ){
130339 char *z = (char*)&pTriggerStep[1];
130340 memcpy(z, pName->z, pName->n);
130341 sqlite3Dequote(z);
130342 pTriggerStep->zTarget = z;
130343 pTriggerStep->op = op;
130344 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
130345 if( IN_RENAME_OBJECT ){
130346 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
130347 }
130348 }
130349 return pTriggerStep;
130350}
130351
130352/*
130353** Build a trigger step out of an INSERT statement. Return a pointer
130354** to the new trigger step.
130355**
130356** The parser calls this routine when it sees an INSERT inside the
130357** body of a trigger.
130358*/
130359SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
130360 Parse *pParse, /* Parser */
130361 Token *pTableName, /* Name of the table into which we insert */
130362 IdList *pColumn, /* List of columns in pTableName to insert into */
130363 Select *pSelect, /* A SELECT statement that supplies values */
130364 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
130365 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
130366 const char *zStart, /* Start of SQL text */
130367 const char *zEnd /* End of SQL text */
130368){
130369 sqlite3 *db = pParse->db;
130370 TriggerStep *pTriggerStep;
130371
130372 assert(pSelect != 0 || db->mallocFailed);
130373
130374 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
130375 if( pTriggerStep ){
130376 if( IN_RENAME_OBJECT ){
130377 pTriggerStep->pSelect = pSelect;
130378 pSelect = 0;
130379 }else{
130380 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
130381 }
130382 pTriggerStep->pIdList = pColumn;
130383 pTriggerStep->pUpsert = pUpsert;
130384 pTriggerStep->orconf = orconf;
130385 }else{
130386 testcase( pColumn );
130387 sqlite3IdListDelete(db, pColumn);
130388 testcase( pUpsert );
130389 sqlite3UpsertDelete(db, pUpsert);
130390 }
130391 sqlite3SelectDelete(db, pSelect);
130392
130393 return pTriggerStep;
130394}
130395
130396/*
130397** Construct a trigger step that implements an UPDATE statement and return
130398** a pointer to that trigger step. The parser calls this routine when it
130399** sees an UPDATE statement inside the body of a CREATE TRIGGER.
130400*/
130401SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
130402 Parse *pParse, /* Parser */
130403 Token *pTableName, /* Name of the table to be updated */
130404 ExprList *pEList, /* The SET clause: list of column and new values */
130405 Expr *pWhere, /* The WHERE clause */
130406 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
130407 const char *zStart, /* Start of SQL text */
130408 const char *zEnd /* End of SQL text */
130409){
130410 sqlite3 *db = pParse->db;
130411 TriggerStep *pTriggerStep;
130412
130413 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
130414 if( pTriggerStep ){
130415 if( IN_RENAME_OBJECT ){
130416 pTriggerStep->pExprList = pEList;
130417 pTriggerStep->pWhere = pWhere;
130418 pEList = 0;
130419 pWhere = 0;
130420 }else{
130421 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
130422 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
130423 }
130424 pTriggerStep->orconf = orconf;
130425 }
130426 sqlite3ExprListDelete(db, pEList);
130427 sqlite3ExprDelete(db, pWhere);
130428 return pTriggerStep;
130429}
130430
130431/*
130432** Construct a trigger step that implements a DELETE statement and return
130433** a pointer to that trigger step. The parser calls this routine when it
130434** sees a DELETE statement inside the body of a CREATE TRIGGER.
130435*/
130436SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
130437 Parse *pParse, /* Parser */
130438 Token *pTableName, /* The table from which rows are deleted */
130439 Expr *pWhere, /* The WHERE clause */
130440 const char *zStart, /* Start of SQL text */
130441 const char *zEnd /* End of SQL text */
130442){
130443 sqlite3 *db = pParse->db;
130444 TriggerStep *pTriggerStep;
130445
130446 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
130447 if( pTriggerStep ){
130448 if( IN_RENAME_OBJECT ){
130449 pTriggerStep->pWhere = pWhere;
130450 pWhere = 0;
130451 }else{
130452 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
130453 }
130454 pTriggerStep->orconf = OE_Default;
130455 }
130456 sqlite3ExprDelete(db, pWhere);
130457 return pTriggerStep;
130458}
130459
130460/*
130461** Recursively delete a Trigger structure
130462*/
130463SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
130464 if( pTrigger==0 ) return;
130465 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
130466 sqlite3DbFree(db, pTrigger->zName);
130467 sqlite3DbFree(db, pTrigger->table);
130468 sqlite3ExprDelete(db, pTrigger->pWhen);
130469 sqlite3IdListDelete(db, pTrigger->pColumns);
130470 sqlite3DbFree(db, pTrigger);
130471}
130472
130473/*
130474** This function is called to drop a trigger from the database schema.
130475**
130476** This may be called directly from the parser and therefore identifies
130477** the trigger by name. The sqlite3DropTriggerPtr() routine does the
130478** same job as this routine except it takes a pointer to the trigger
130479** instead of the trigger name.
130480**/
130481SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
130482 Trigger *pTrigger = 0;
130483 int i;
130484 const char *zDb;
130485 const char *zName;
130486 sqlite3 *db = pParse->db;
130487
130488 if( db->mallocFailed ) goto drop_trigger_cleanup;
130489 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
130490 goto drop_trigger_cleanup;
130491 }
130492
130493 assert( pName->nSrc==1 );
130494 zDb = pName->a[0].zDatabase;
130495 zName = pName->a[0].zName;
130496 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
130497 for(i=OMIT_TEMPDB; i<db->nDb; i++){
130498 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
130499 if( zDb && sqlite3StrICmp(db->aDb[j].zDbSName, zDb) ) continue;
130500 assert( sqlite3SchemaMutexHeld(db, j, 0) );
130501 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
130502 if( pTrigger ) break;
130503 }
130504 if( !pTrigger ){
130505 if( !noErr ){
130506 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
130507 }else{
130508 sqlite3CodeVerifyNamedSchema(pParse, zDb);
130509 }
130510 pParse->checkSchema = 1;
130511 goto drop_trigger_cleanup;
130512 }
130513 sqlite3DropTriggerPtr(pParse, pTrigger);
130514
130515drop_trigger_cleanup:
130516 sqlite3SrcListDelete(db, pName);
130517}
130518
130519/*
130520** Return a pointer to the Table structure for the table that a trigger
130521** is set on.
130522*/
130523static Table *tableOfTrigger(Trigger *pTrigger){
130524 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
130525}
130526
130527
130528/*
130529** Drop a trigger given a pointer to that trigger.
130530*/
130531SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
130532 Table *pTable;
130533 Vdbe *v;
130534 sqlite3 *db = pParse->db;
130535 int iDb;
130536
130537 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
130538 assert( iDb>=0 && iDb<db->nDb );
130539 pTable = tableOfTrigger(pTrigger);
130540 assert( pTable );
130541 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
130542#ifndef SQLITE_OMIT_AUTHORIZATION
130543 {
130544 int code = SQLITE_DROP_TRIGGER;
130545 const char *zDb = db->aDb[iDb].zDbSName;
130546 const char *zTab = SCHEMA_TABLE(iDb);
130547 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
130548 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
130549 sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
130550 return;
130551 }
130552 }
130553#endif
130554
130555 /* Generate code to destroy the database record of the trigger.
130556 */
130557 assert( pTable!=0 );
130558 if( (v = sqlite3GetVdbe(pParse))!=0 ){
130559 sqlite3NestedParse(pParse,
130560 "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'",
130561 db->aDb[iDb].zDbSName, MASTER_NAME, pTrigger->zName
130562 );
130563 sqlite3ChangeCookie(pParse, iDb);
130564 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
130565 }
130566}
130567
130568/*
130569** Remove a trigger from the hash tables of the sqlite* pointer.
130570*/
130571SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
130572 Trigger *pTrigger;
130573 Hash *pHash;
130574
130575 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
130576 pHash = &(db->aDb[iDb].pSchema->trigHash);
130577 pTrigger = sqlite3HashInsert(pHash, zName, 0);
130578 if( ALWAYS(pTrigger) ){
130579 if( pTrigger->pSchema==pTrigger->pTabSchema ){
130580 Table *pTab = tableOfTrigger(pTrigger);
130581 Trigger **pp;
130582 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
130583 *pp = (*pp)->pNext;
130584 }
130585 sqlite3DeleteTrigger(db, pTrigger);
130586 db->mDbFlags |= DBFLAG_SchemaChange;
130587 }
130588}
130589
130590/*
130591** pEList is the SET clause of an UPDATE statement. Each entry
130592** in pEList is of the format <id>=<expr>. If any of the entries
130593** in pEList have an <id> which matches an identifier in pIdList,
130594** then return TRUE. If pIdList==NULL, then it is considered a
130595** wildcard that matches anything. Likewise if pEList==NULL then
130596** it matches anything so always return true. Return false only
130597** if there is no match.
130598*/
130599static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
130600 int e;
130601 if( pIdList==0 || NEVER(pEList==0) ) return 1;
130602 for(e=0; e<pEList->nExpr; e++){
130603 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
130604 }
130605 return 0;
130606}
130607
130608/*
130609** Return a list of all triggers on table pTab if there exists at least
130610** one trigger that must be fired when an operation of type 'op' is
130611** performed on the table, and, if that operation is an UPDATE, if at
130612** least one of the columns in pChanges is being modified.
130613*/
130614SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
130615 Parse *pParse, /* Parse context */
130616 Table *pTab, /* The table the contains the triggers */
130617 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
130618 ExprList *pChanges, /* Columns that change in an UPDATE statement */
130619 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
130620){
130621 int mask = 0;
130622 Trigger *pList = 0;
130623 Trigger *p;
130624
130625 if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
130626 pList = sqlite3TriggerList(pParse, pTab);
130627 }
130628 assert( pList==0 || IsVirtual(pTab)==0 );
130629 for(p=pList; p; p=p->pNext){
130630 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
130631 mask |= p->tr_tm;
130632 }
130633 }
130634 if( pMask ){
130635 *pMask = mask;
130636 }
130637 return (mask ? pList : 0);
130638}
130639
130640/*
130641** Convert the pStep->zTarget string into a SrcList and return a pointer
130642** to that SrcList.
130643**
130644** This routine adds a specific database name, if needed, to the target when
130645** forming the SrcList. This prevents a trigger in one database from
130646** referring to a target in another database. An exception is when the
130647** trigger is in TEMP in which case it can refer to any other database it
130648** wants.
130649*/
130650static SrcList *targetSrcList(
130651 Parse *pParse, /* The parsing context */
130652 TriggerStep *pStep /* The trigger containing the target token */
130653){
130654 sqlite3 *db = pParse->db;
130655 int iDb; /* Index of the database to use */
130656 SrcList *pSrc; /* SrcList to be returned */
130657
130658 pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
130659 if( pSrc ){
130660 assert( pSrc->nSrc>0 );
130661 pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
130662 iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
130663 if( iDb==0 || iDb>=2 ){
130664 const char *zDb;
130665 assert( iDb<db->nDb );
130666 zDb = db->aDb[iDb].zDbSName;
130667 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, zDb);
130668 }
130669 }
130670 return pSrc;
130671}
130672
130673/*
130674** Generate VDBE code for the statements inside the body of a single
130675** trigger.
130676*/
130677static int codeTriggerProgram(
130678 Parse *pParse, /* The parser context */
130679 TriggerStep *pStepList, /* List of statements inside the trigger body */
130680 int orconf /* Conflict algorithm. (OE_Abort, etc) */
130681){
130682 TriggerStep *pStep;
130683 Vdbe *v = pParse->pVdbe;
130684 sqlite3 *db = pParse->db;
130685
130686 assert( pParse->pTriggerTab && pParse->pToplevel );
130687 assert( pStepList );
130688 assert( v!=0 );
130689 for(pStep=pStepList; pStep; pStep=pStep->pNext){
130690 /* Figure out the ON CONFLICT policy that will be used for this step
130691 ** of the trigger program. If the statement that caused this trigger
130692 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
130693 ** the ON CONFLICT policy that was specified as part of the trigger
130694 ** step statement. Example:
130695 **
130696 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
130697 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
130698 ** END;
130699 **
130700 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
130701 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
130702 */
130703 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
130704 assert( pParse->okConstFactor==0 );
130705
130706#ifndef SQLITE_OMIT_TRACE
130707 if( pStep->zSpan ){
130708 sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
130709 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
130710 P4_DYNAMIC);
130711 }
130712#endif
130713
130714 switch( pStep->op ){
130715 case TK_UPDATE: {
130716 sqlite3Update(pParse,
130717 targetSrcList(pParse, pStep),
130718 sqlite3ExprListDup(db, pStep->pExprList, 0),
130719 sqlite3ExprDup(db, pStep->pWhere, 0),
130720 pParse->eOrconf, 0, 0, 0
130721 );
130722 break;
130723 }
130724 case TK_INSERT: {
130725 sqlite3Insert(pParse,
130726 targetSrcList(pParse, pStep),
130727 sqlite3SelectDup(db, pStep->pSelect, 0),
130728 sqlite3IdListDup(db, pStep->pIdList),
130729 pParse->eOrconf,
130730 sqlite3UpsertDup(db, pStep->pUpsert)
130731 );
130732 break;
130733 }
130734 case TK_DELETE: {
130735 sqlite3DeleteFrom(pParse,
130736 targetSrcList(pParse, pStep),
130737 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
130738 );
130739 break;
130740 }
130741 default: assert( pStep->op==TK_SELECT ); {
130742 SelectDest sDest;
130743 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
130744 sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
130745 sqlite3Select(pParse, pSelect, &sDest);
130746 sqlite3SelectDelete(db, pSelect);
130747 break;
130748 }
130749 }
130750 if( pStep->op!=TK_SELECT ){
130751 sqlite3VdbeAddOp0(v, OP_ResetCount);
130752 }
130753 }
130754
130755 return 0;
130756}
130757
130758#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
130759/*
130760** This function is used to add VdbeComment() annotations to a VDBE
130761** program. It is not used in production code, only for debugging.
130762*/
130763static const char *onErrorText(int onError){
130764 switch( onError ){
130765 case OE_Abort: return "abort";
130766 case OE_Rollback: return "rollback";
130767 case OE_Fail: return "fail";
130768 case OE_Replace: return "replace";
130769 case OE_Ignore: return "ignore";
130770 case OE_Default: return "default";
130771 }
130772 return "n/a";
130773}
130774#endif
130775
130776/*
130777** Parse context structure pFrom has just been used to create a sub-vdbe
130778** (trigger program). If an error has occurred, transfer error information
130779** from pFrom to pTo.
130780*/
130781static void transferParseError(Parse *pTo, Parse *pFrom){
130782 assert( pFrom->zErrMsg==0 || pFrom->nErr );
130783 assert( pTo->zErrMsg==0 || pTo->nErr );
130784 if( pTo->nErr==0 ){
130785 pTo->zErrMsg = pFrom->zErrMsg;
130786 pTo->nErr = pFrom->nErr;
130787 pTo->rc = pFrom->rc;
130788 }else{
130789 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
130790 }
130791}
130792
130793/*
130794** Create and populate a new TriggerPrg object with a sub-program
130795** implementing trigger pTrigger with ON CONFLICT policy orconf.
130796*/
130797static TriggerPrg *codeRowTrigger(
130798 Parse *pParse, /* Current parse context */
130799 Trigger *pTrigger, /* Trigger to code */
130800 Table *pTab, /* The table pTrigger is attached to */
130801 int orconf /* ON CONFLICT policy to code trigger program with */
130802){
130803 Parse *pTop = sqlite3ParseToplevel(pParse);
130804 sqlite3 *db = pParse->db; /* Database handle */
130805 TriggerPrg *pPrg; /* Value to return */
130806 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
130807 Vdbe *v; /* Temporary VM */
130808 NameContext sNC; /* Name context for sub-vdbe */
130809 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
130810 Parse *pSubParse; /* Parse context for sub-vdbe */
130811 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
130812
130813 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
130814 assert( pTop->pVdbe );
130815
130816 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
130817 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
130818 ** list of the top-level Parse object sooner rather than later. */
130819 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
130820 if( !pPrg ) return 0;
130821 pPrg->pNext = pTop->pTriggerPrg;
130822 pTop->pTriggerPrg = pPrg;
130823 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
130824 if( !pProgram ) return 0;
130825 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
130826 pPrg->pTrigger = pTrigger;
130827 pPrg->orconf = orconf;
130828 pPrg->aColmask[0] = 0xffffffff;
130829 pPrg->aColmask[1] = 0xffffffff;
130830
130831 /* Allocate and populate a new Parse context to use for coding the
130832 ** trigger sub-program. */
130833 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
130834 if( !pSubParse ) return 0;
130835 memset(&sNC, 0, sizeof(sNC));
130836 sNC.pParse = pSubParse;
130837 pSubParse->db = db;
130838 pSubParse->pTriggerTab = pTab;
130839 pSubParse->pToplevel = pTop;
130840 pSubParse->zAuthContext = pTrigger->zName;
130841 pSubParse->eTriggerOp = pTrigger->op;
130842 pSubParse->nQueryLoop = pParse->nQueryLoop;
130843
130844 v = sqlite3GetVdbe(pSubParse);
130845 if( v ){
130846 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
130847 pTrigger->zName, onErrorText(orconf),
130848 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
130849 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
130850 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
130851 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
130852 pTab->zName
130853 ));
130854#ifndef SQLITE_OMIT_TRACE
130855 if( pTrigger->zName ){
130856 sqlite3VdbeChangeP4(v, -1,
130857 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
130858 );
130859 }
130860#endif
130861
130862 /* If one was specified, code the WHEN clause. If it evaluates to false
130863 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
130864 ** OP_Halt inserted at the end of the program. */
130865 if( pTrigger->pWhen ){
130866 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
130867 if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
130868 && db->mallocFailed==0
130869 ){
130870 iEndTrigger = sqlite3VdbeMakeLabel(v);
130871 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
130872 }
130873 sqlite3ExprDelete(db, pWhen);
130874 }
130875
130876 /* Code the trigger program into the sub-vdbe. */
130877 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
130878
130879 /* Insert an OP_Halt at the end of the sub-program. */
130880 if( iEndTrigger ){
130881 sqlite3VdbeResolveLabel(v, iEndTrigger);
130882 }
130883 sqlite3VdbeAddOp0(v, OP_Halt);
130884 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
130885
130886 transferParseError(pParse, pSubParse);
130887 if( db->mallocFailed==0 && pParse->nErr==0 ){
130888 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
130889 }
130890 pProgram->nMem = pSubParse->nMem;
130891 pProgram->nCsr = pSubParse->nTab;
130892 pProgram->token = (void *)pTrigger;
130893 pPrg->aColmask[0] = pSubParse->oldmask;
130894 pPrg->aColmask[1] = pSubParse->newmask;
130895 sqlite3VdbeDelete(v);
130896 }
130897
130898 assert( !pSubParse->pAinc && !pSubParse->pZombieTab );
130899 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
130900 sqlite3ParserReset(pSubParse);
130901 sqlite3StackFree(db, pSubParse);
130902
130903 return pPrg;
130904}
130905
130906/*
130907** Return a pointer to a TriggerPrg object containing the sub-program for
130908** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
130909** TriggerPrg object exists, a new object is allocated and populated before
130910** being returned.
130911*/
130912static TriggerPrg *getRowTrigger(
130913 Parse *pParse, /* Current parse context */
130914 Trigger *pTrigger, /* Trigger to code */
130915 Table *pTab, /* The table trigger pTrigger is attached to */
130916 int orconf /* ON CONFLICT algorithm. */
130917){
130918 Parse *pRoot = sqlite3ParseToplevel(pParse);
130919 TriggerPrg *pPrg;
130920
130921 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
130922
130923 /* It may be that this trigger has already been coded (or is in the
130924 ** process of being coded). If this is the case, then an entry with
130925 ** a matching TriggerPrg.pTrigger field will be present somewhere
130926 ** in the Parse.pTriggerPrg list. Search for such an entry. */
130927 for(pPrg=pRoot->pTriggerPrg;
130928 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
130929 pPrg=pPrg->pNext
130930 );
130931
130932 /* If an existing TriggerPrg could not be located, create a new one. */
130933 if( !pPrg ){
130934 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
130935 }
130936
130937 return pPrg;
130938}
130939
130940/*
130941** Generate code for the trigger program associated with trigger p on
130942** table pTab. The reg, orconf and ignoreJump parameters passed to this
130943** function are the same as those described in the header function for
130944** sqlite3CodeRowTrigger()
130945*/
130946SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
130947 Parse *pParse, /* Parse context */
130948 Trigger *p, /* Trigger to code */
130949 Table *pTab, /* The table to code triggers from */
130950 int reg, /* Reg array containing OLD.* and NEW.* values */
130951 int orconf, /* ON CONFLICT policy */
130952 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
130953){
130954 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
130955 TriggerPrg *pPrg;
130956 pPrg = getRowTrigger(pParse, p, pTab, orconf);
130957 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
130958
130959 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
130960 ** is a pointer to the sub-vdbe containing the trigger program. */
130961 if( pPrg ){
130962 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
130963
130964 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
130965 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
130966 VdbeComment(
130967 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
130968
130969 /* Set the P5 operand of the OP_Program instruction to non-zero if
130970 ** recursive invocation of this trigger program is disallowed. Recursive
130971 ** invocation is disallowed if (a) the sub-program is really a trigger,
130972 ** not a foreign key action, and (b) the flag to enable recursive triggers
130973 ** is clear. */
130974 sqlite3VdbeChangeP5(v, (u8)bRecursive);
130975 }
130976}
130977
130978/*
130979** This is called to code the required FOR EACH ROW triggers for an operation
130980** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
130981** is given by the op parameter. The tr_tm parameter determines whether the
130982** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
130983** parameter pChanges is passed the list of columns being modified.
130984**
130985** If there are no triggers that fire at the specified time for the specified
130986** operation on pTab, this function is a no-op.
130987**
130988** The reg argument is the address of the first in an array of registers
130989** that contain the values substituted for the new.* and old.* references
130990** in the trigger program. If N is the number of columns in table pTab
130991** (a copy of pTab->nCol), then registers are populated as follows:
130992**
130993** Register Contains
130994** ------------------------------------------------------
130995** reg+0 OLD.rowid
130996** reg+1 OLD.* value of left-most column of pTab
130997** ... ...
130998** reg+N OLD.* value of right-most column of pTab
130999** reg+N+1 NEW.rowid
131000** reg+N+2 OLD.* value of left-most column of pTab
131001** ... ...
131002** reg+N+N+1 NEW.* value of right-most column of pTab
131003**
131004** For ON DELETE triggers, the registers containing the NEW.* values will
131005** never be accessed by the trigger program, so they are not allocated or
131006** populated by the caller (there is no data to populate them with anyway).
131007** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
131008** are never accessed, and so are not allocated by the caller. So, for an
131009** ON INSERT trigger, the value passed to this function as parameter reg
131010** is not a readable register, although registers (reg+N) through
131011** (reg+N+N+1) are.
131012**
131013** Parameter orconf is the default conflict resolution algorithm for the
131014** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
131015** is the instruction that control should jump to if a trigger program
131016** raises an IGNORE exception.
131017*/
131018SQLITE_PRIVATE void sqlite3CodeRowTrigger(
131019 Parse *pParse, /* Parse context */
131020 Trigger *pTrigger, /* List of triggers on table pTab */
131021 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
131022 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
131023 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
131024 Table *pTab, /* The table to code triggers from */
131025 int reg, /* The first in an array of registers (see above) */
131026 int orconf, /* ON CONFLICT policy */
131027 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
131028){
131029 Trigger *p; /* Used to iterate through pTrigger list */
131030
131031 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
131032 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
131033 assert( (op==TK_UPDATE)==(pChanges!=0) );
131034
131035 for(p=pTrigger; p; p=p->pNext){
131036
131037 /* Sanity checking: The schema for the trigger and for the table are
131038 ** always defined. The trigger must be in the same schema as the table
131039 ** or else it must be a TEMP trigger. */
131040 assert( p->pSchema!=0 );
131041 assert( p->pTabSchema!=0 );
131042 assert( p->pSchema==p->pTabSchema
131043 || p->pSchema==pParse->db->aDb[1].pSchema );
131044
131045 /* Determine whether we should code this trigger */
131046 if( p->op==op
131047 && p->tr_tm==tr_tm
131048 && checkColumnOverlap(p->pColumns, pChanges)
131049 ){
131050 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
131051 }
131052 }
131053}
131054
131055/*
131056** Triggers may access values stored in the old.* or new.* pseudo-table.
131057** This function returns a 32-bit bitmask indicating which columns of the
131058** old.* or new.* tables actually are used by triggers. This information
131059** may be used by the caller, for example, to avoid having to load the entire
131060** old.* record into memory when executing an UPDATE or DELETE command.
131061**
131062** Bit 0 of the returned mask is set if the left-most column of the
131063** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
131064** the second leftmost column value is required, and so on. If there
131065** are more than 32 columns in the table, and at least one of the columns
131066** with an index greater than 32 may be accessed, 0xffffffff is returned.
131067**
131068** It is not possible to determine if the old.rowid or new.rowid column is
131069** accessed by triggers. The caller must always assume that it is.
131070**
131071** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
131072** applies to the old.* table. If 1, the new.* table.
131073**
131074** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
131075** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
131076** included in the returned mask if the TRIGGER_BEFORE bit is set in the
131077** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
131078** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
131079*/
131080SQLITE_PRIVATE u32 sqlite3TriggerColmask(
131081 Parse *pParse, /* Parse context */
131082 Trigger *pTrigger, /* List of triggers on table pTab */
131083 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
131084 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
131085 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
131086 Table *pTab, /* The table to code triggers from */
131087 int orconf /* Default ON CONFLICT policy for trigger steps */
131088){
131089 const int op = pChanges ? TK_UPDATE : TK_DELETE;
131090 u32 mask = 0;
131091 Trigger *p;
131092
131093 assert( isNew==1 || isNew==0 );
131094 for(p=pTrigger; p; p=p->pNext){
131095 if( p->op==op && (tr_tm&p->tr_tm)
131096 && checkColumnOverlap(p->pColumns,pChanges)
131097 ){
131098 TriggerPrg *pPrg;
131099 pPrg = getRowTrigger(pParse, p, pTab, orconf);
131100 if( pPrg ){
131101 mask |= pPrg->aColmask[isNew];
131102 }
131103 }
131104 }
131105
131106 return mask;
131107}
131108
131109#endif /* !defined(SQLITE_OMIT_TRIGGER) */
131110
131111/************** End of trigger.c *********************************************/
131112/************** Begin file update.c ******************************************/
131113/*
131114** 2001 September 15
131115**
131116** The author disclaims copyright to this source code. In place of
131117** a legal notice, here is a blessing:
131118**
131119** May you do good and not evil.
131120** May you find forgiveness for yourself and forgive others.
131121** May you share freely, never taking more than you give.
131122**
131123*************************************************************************
131124** This file contains C code routines that are called by the parser
131125** to handle UPDATE statements.
131126*/
131127/* #include "sqliteInt.h" */
131128
131129#ifndef SQLITE_OMIT_VIRTUALTABLE
131130/* Forward declaration */
131131static void updateVirtualTable(
131132 Parse *pParse, /* The parsing context */
131133 SrcList *pSrc, /* The virtual table to be modified */
131134 Table *pTab, /* The virtual table */
131135 ExprList *pChanges, /* The columns to change in the UPDATE statement */
131136 Expr *pRowidExpr, /* Expression used to recompute the rowid */
131137 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
131138 Expr *pWhere, /* WHERE clause of the UPDATE statement */
131139 int onError /* ON CONFLICT strategy */
131140);
131141#endif /* SQLITE_OMIT_VIRTUALTABLE */
131142
131143/*
131144** The most recently coded instruction was an OP_Column to retrieve the
131145** i-th column of table pTab. This routine sets the P4 parameter of the
131146** OP_Column to the default value, if any.
131147**
131148** The default value of a column is specified by a DEFAULT clause in the
131149** column definition. This was either supplied by the user when the table
131150** was created, or added later to the table definition by an ALTER TABLE
131151** command. If the latter, then the row-records in the table btree on disk
131152** may not contain a value for the column and the default value, taken
131153** from the P4 parameter of the OP_Column instruction, is returned instead.
131154** If the former, then all row-records are guaranteed to include a value
131155** for the column and the P4 value is not required.
131156**
131157** Column definitions created by an ALTER TABLE command may only have
131158** literal default values specified: a number, null or a string. (If a more
131159** complicated default expression value was provided, it is evaluated
131160** when the ALTER TABLE is executed and one of the literal values written
131161** into the sqlite_master table.)
131162**
131163** Therefore, the P4 parameter is only required if the default value for
131164** the column is a literal number, string or null. The sqlite3ValueFromExpr()
131165** function is capable of transforming these types of expressions into
131166** sqlite3_value objects.
131167**
131168** If parameter iReg is not negative, code an OP_RealAffinity instruction
131169** on register iReg. This is used when an equivalent integer value is
131170** stored in place of an 8-byte floating point value in order to save
131171** space.
131172*/
131173SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
131174 assert( pTab!=0 );
131175 if( !pTab->pSelect ){
131176 sqlite3_value *pValue = 0;
131177 u8 enc = ENC(sqlite3VdbeDb(v));
131178 Column *pCol = &pTab->aCol[i];
131179 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
131180 assert( i<pTab->nCol );
131181 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
131182 pCol->affinity, &pValue);
131183 if( pValue ){
131184 sqlite3VdbeAppendP4(v, pValue, P4_MEM);
131185 }
131186 }
131187#ifndef SQLITE_OMIT_FLOATING_POINT
131188 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
131189 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
131190 }
131191#endif
131192}
131193
131194/*
131195** Process an UPDATE statement.
131196**
131197** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
131198** \_______/ \________/ \______/ \________________/
131199* onError pTabList pChanges pWhere
131200*/
131201SQLITE_PRIVATE void sqlite3Update(
131202 Parse *pParse, /* The parser context */
131203 SrcList *pTabList, /* The table in which we should change things */
131204 ExprList *pChanges, /* Things to be changed */
131205 Expr *pWhere, /* The WHERE clause. May be null */
131206 int onError, /* How to handle constraint errors */
131207 ExprList *pOrderBy, /* ORDER BY clause. May be null */
131208 Expr *pLimit, /* LIMIT clause. May be null */
131209 Upsert *pUpsert /* ON CONFLICT clause, or null */
131210){
131211 int i, j; /* Loop counters */
131212 Table *pTab; /* The table to be updated */
131213 int addrTop = 0; /* VDBE instruction address of the start of the loop */
131214 WhereInfo *pWInfo; /* Information about the WHERE clause */
131215 Vdbe *v; /* The virtual database engine */
131216 Index *pIdx; /* For looping over indices */
131217 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
131218 int nIdx; /* Number of indices that need updating */
131219 int iBaseCur; /* Base cursor number */
131220 int iDataCur; /* Cursor for the canonical data btree */
131221 int iIdxCur; /* Cursor for the first index */
131222 sqlite3 *db; /* The database structure */
131223 int *aRegIdx = 0; /* First register in array assigned to each index */
131224 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
131225 ** an expression for the i-th column of the table.
131226 ** aXRef[i]==-1 if the i-th column is not changed. */
131227 u8 *aToOpen; /* 1 for tables and indices to be opened */
131228 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
131229 u8 chngRowid; /* Rowid changed in a normal table */
131230 u8 chngKey; /* Either chngPk or chngRowid */
131231 Expr *pRowidExpr = 0; /* Expression defining the new record number */
131232 AuthContext sContext; /* The authorization context */
131233 NameContext sNC; /* The name-context to resolve expressions in */
131234 int iDb; /* Database containing the table being updated */
131235 int eOnePass; /* ONEPASS_XXX value from where.c */
131236 int hasFK; /* True if foreign key processing is required */
131237 int labelBreak; /* Jump here to break out of UPDATE loop */
131238 int labelContinue; /* Jump here to continue next step of UPDATE loop */
131239 int flags; /* Flags for sqlite3WhereBegin() */
131240
131241#ifndef SQLITE_OMIT_TRIGGER
131242 int isView; /* True when updating a view (INSTEAD OF trigger) */
131243 Trigger *pTrigger; /* List of triggers on pTab, if required */
131244 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
131245#endif
131246 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
131247 int iEph = 0; /* Ephemeral table holding all primary key values */
131248 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
131249 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
131250 int addrOpen = 0; /* Address of OP_OpenEphemeral */
131251 int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
131252 i16 nPk = 0; /* Number of components of the PRIMARY KEY */
131253 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
131254
131255 /* Register Allocations */
131256 int regRowCount = 0; /* A count of rows changed */
131257 int regOldRowid = 0; /* The old rowid */
131258 int regNewRowid = 0; /* The new rowid */
131259 int regNew = 0; /* Content of the NEW.* table in triggers */
131260 int regOld = 0; /* Content of OLD.* table in triggers */
131261 int regRowSet = 0; /* Rowset of rows to be updated */
131262 int regKey = 0; /* composite PRIMARY KEY value */
131263
131264 memset(&sContext, 0, sizeof(sContext));
131265 db = pParse->db;
131266 if( pParse->nErr || db->mallocFailed ){
131267 goto update_cleanup;
131268 }
131269 assert( pTabList->nSrc==1 );
131270
131271 /* Locate the table which we want to update.
131272 */
131273 pTab = sqlite3SrcListLookup(pParse, pTabList);
131274 if( pTab==0 ) goto update_cleanup;
131275 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
131276
131277 /* Figure out if we have any triggers and if the table being
131278 ** updated is a view.
131279 */
131280#ifndef SQLITE_OMIT_TRIGGER
131281 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
131282 isView = pTab->pSelect!=0;
131283 assert( pTrigger || tmask==0 );
131284#else
131285# define pTrigger 0
131286# define isView 0
131287# define tmask 0
131288#endif
131289#ifdef SQLITE_OMIT_VIEW
131290# undef isView
131291# define isView 0
131292#endif
131293
131294#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
131295 if( !isView ){
131296 pWhere = sqlite3LimitWhere(
131297 pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"
131298 );
131299 pOrderBy = 0;
131300 pLimit = 0;
131301 }
131302#endif
131303
131304 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
131305 goto update_cleanup;
131306 }
131307 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
131308 goto update_cleanup;
131309 }
131310
131311 /* Allocate a cursors for the main database table and for all indices.
131312 ** The index cursors might not be used, but if they are used they
131313 ** need to occur right after the database cursor. So go ahead and
131314 ** allocate enough space, just in case.
131315 */
131316 iBaseCur = iDataCur = pParse->nTab++;
131317 iIdxCur = iDataCur+1;
131318 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
131319 testcase( pPk!=0 && pPk!=pTab->pIndex );
131320 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
131321 if( pPk==pIdx ){
131322 iDataCur = pParse->nTab;
131323 }
131324 pParse->nTab++;
131325 }
131326 if( pUpsert ){
131327 /* On an UPSERT, reuse the same cursors already opened by INSERT */
131328 iDataCur = pUpsert->iDataCur;
131329 iIdxCur = pUpsert->iIdxCur;
131330 pParse->nTab = iBaseCur;
131331 }
131332 pTabList->a[0].iCursor = iDataCur;
131333
131334 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
131335 ** Initialize aXRef[] and aToOpen[] to their default values.
131336 */
131337 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
131338 if( aXRef==0 ) goto update_cleanup;
131339 aRegIdx = aXRef+pTab->nCol;
131340 aToOpen = (u8*)(aRegIdx+nIdx);
131341 memset(aToOpen, 1, nIdx+1);
131342 aToOpen[nIdx+1] = 0;
131343 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
131344
131345 /* Initialize the name-context */
131346 memset(&sNC, 0, sizeof(sNC));
131347 sNC.pParse = pParse;
131348 sNC.pSrcList = pTabList;
131349 sNC.uNC.pUpsert = pUpsert;
131350 sNC.ncFlags = NC_UUpsert;
131351
131352 /* Resolve the column names in all the expressions of the
131353 ** of the UPDATE statement. Also find the column index
131354 ** for each column to be updated in the pChanges array. For each
131355 ** column to be updated, make sure we have authorization to change
131356 ** that column.
131357 */
131358 chngRowid = chngPk = 0;
131359 for(i=0; i<pChanges->nExpr; i++){
131360 if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
131361 goto update_cleanup;
131362 }
131363 for(j=0; j<pTab->nCol; j++){
131364 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
131365 if( j==pTab->iPKey ){
131366 chngRowid = 1;
131367 pRowidExpr = pChanges->a[i].pExpr;
131368 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
131369 chngPk = 1;
131370 }
131371 aXRef[j] = i;
131372 break;
131373 }
131374 }
131375 if( j>=pTab->nCol ){
131376 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){
131377 j = -1;
131378 chngRowid = 1;
131379 pRowidExpr = pChanges->a[i].pExpr;
131380 }else{
131381 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
131382 pParse->checkSchema = 1;
131383 goto update_cleanup;
131384 }
131385 }
131386#ifndef SQLITE_OMIT_AUTHORIZATION
131387 {
131388 int rc;
131389 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
131390 j<0 ? "ROWID" : pTab->aCol[j].zName,
131391 db->aDb[iDb].zDbSName);
131392 if( rc==SQLITE_DENY ){
131393 goto update_cleanup;
131394 }else if( rc==SQLITE_IGNORE ){
131395 aXRef[j] = -1;
131396 }
131397 }
131398#endif
131399 }
131400 assert( (chngRowid & chngPk)==0 );
131401 assert( chngRowid==0 || chngRowid==1 );
131402 assert( chngPk==0 || chngPk==1 );
131403 chngKey = chngRowid + chngPk;
131404
131405 /* The SET expressions are not actually used inside the WHERE loop.
131406 ** So reset the colUsed mask. Unless this is a virtual table. In that
131407 ** case, set all bits of the colUsed mask (to ensure that the virtual
131408 ** table implementation makes all columns available).
131409 */
131410 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
131411
131412 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
131413
131414 /* There is one entry in the aRegIdx[] array for each index on the table
131415 ** being updated. Fill in aRegIdx[] with a register number that will hold
131416 ** the key for accessing each index.
131417 **
131418 ** FIXME: Be smarter about omitting indexes that use expressions.
131419 */
131420 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
131421 int reg;
131422 if( chngKey || hasFK>1 || pIdx->pPartIdxWhere || pIdx==pPk ){
131423 reg = ++pParse->nMem;
131424 pParse->nMem += pIdx->nColumn;
131425 }else{
131426 reg = 0;
131427 for(i=0; i<pIdx->nKeyCol; i++){
131428 i16 iIdxCol = pIdx->aiColumn[i];
131429 if( iIdxCol<0 || aXRef[iIdxCol]>=0 ){
131430 reg = ++pParse->nMem;
131431 pParse->nMem += pIdx->nColumn;
131432 if( (onError==OE_Replace)
131433 || (onError==OE_Default && pIdx->onError==OE_Replace)
131434 ){
131435 bReplace = 1;
131436 }
131437 break;
131438 }
131439 }
131440 }
131441 if( reg==0 ) aToOpen[j+1] = 0;
131442 aRegIdx[j] = reg;
131443 }
131444 if( bReplace ){
131445 /* If REPLACE conflict resolution might be invoked, open cursors on all
131446 ** indexes in case they are needed to delete records. */
131447 memset(aToOpen, 1, nIdx+1);
131448 }
131449
131450 /* Begin generating code. */
131451 v = sqlite3GetVdbe(pParse);
131452 if( v==0 ) goto update_cleanup;
131453 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
131454 sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);
131455
131456 /* Allocate required registers. */
131457 if( !IsVirtual(pTab) ){
131458 regRowSet = ++pParse->nMem;
131459 regOldRowid = regNewRowid = ++pParse->nMem;
131460 if( chngPk || pTrigger || hasFK ){
131461 regOld = pParse->nMem + 1;
131462 pParse->nMem += pTab->nCol;
131463 }
131464 if( chngKey || pTrigger || hasFK ){
131465 regNewRowid = ++pParse->nMem;
131466 }
131467 regNew = pParse->nMem + 1;
131468 pParse->nMem += pTab->nCol;
131469 }
131470
131471 /* Start the view context. */
131472 if( isView ){
131473 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
131474 }
131475
131476 /* If we are trying to update a view, realize that view into
131477 ** an ephemeral table.
131478 */
131479#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
131480 if( isView ){
131481 sqlite3MaterializeView(pParse, pTab,
131482 pWhere, pOrderBy, pLimit, iDataCur
131483 );
131484 pOrderBy = 0;
131485 pLimit = 0;
131486 }
131487#endif
131488
131489 /* Resolve the column names in all the expressions in the
131490 ** WHERE clause.
131491 */
131492 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
131493 goto update_cleanup;
131494 }
131495
131496#ifndef SQLITE_OMIT_VIRTUALTABLE
131497 /* Virtual tables must be handled separately */
131498 if( IsVirtual(pTab) ){
131499 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
131500 pWhere, onError);
131501 goto update_cleanup;
131502 }
131503#endif
131504
131505 /* Jump to labelBreak to abandon further processing of this UPDATE */
131506 labelContinue = labelBreak = sqlite3VdbeMakeLabel(v);
131507
131508 /* Not an UPSERT. Normal processing. Begin by
131509 ** initialize the count of updated rows */
131510 if( (db->flags&SQLITE_CountRows)!=0
131511 && !pParse->pTriggerTab
131512 && !pParse->nested
131513 && pUpsert==0
131514 ){
131515 regRowCount = ++pParse->nMem;
131516 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
131517 }
131518
131519 if( HasRowid(pTab) ){
131520 sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
131521 }else{
131522 assert( pPk!=0 );
131523 nPk = pPk->nKeyCol;
131524 iPk = pParse->nMem+1;
131525 pParse->nMem += nPk;
131526 regKey = ++pParse->nMem;
131527 if( pUpsert==0 ){
131528 iEph = pParse->nTab++;
131529 sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
131530 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
131531 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
131532 }
131533 }
131534
131535 if( pUpsert ){
131536 /* If this is an UPSERT, then all cursors have already been opened by
131537 ** the outer INSERT and the data cursor should be pointing at the row
131538 ** that is to be updated. So bypass the code that searches for the
131539 ** row(s) to be updated.
131540 */
131541 pWInfo = 0;
131542 eOnePass = ONEPASS_SINGLE;
131543 sqlite3ExprIfFalse(pParse, pWhere, labelBreak, SQLITE_JUMPIFNULL);
131544 }else{
131545 /* Begin the database scan.
131546 **
131547 ** Do not consider a single-pass strategy for a multi-row update if
131548 ** there are any triggers or foreign keys to process, or rows may
131549 ** be deleted as a result of REPLACE conflict handling. Any of these
131550 ** things might disturb a cursor being used to scan through the table
131551 ** or index, causing a single-pass approach to malfunction. */
131552 flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
131553 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
131554 flags |= WHERE_ONEPASS_MULTIROW;
131555 }
131556 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
131557 if( pWInfo==0 ) goto update_cleanup;
131558
131559 /* A one-pass strategy that might update more than one row may not
131560 ** be used if any column of the index used for the scan is being
131561 ** updated. Otherwise, if there is an index on "b", statements like
131562 ** the following could create an infinite loop:
131563 **
131564 ** UPDATE t1 SET b=b+1 WHERE b>?
131565 **
131566 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
131567 ** strategy that uses an index for which one or more columns are being
131568 ** updated. */
131569 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
131570 if( eOnePass!=ONEPASS_SINGLE ){
131571 sqlite3MultiWrite(pParse);
131572 if( eOnePass==ONEPASS_MULTI ){
131573 int iCur = aiCurOnePass[1];
131574 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
131575 eOnePass = ONEPASS_OFF;
131576 }
131577 assert( iCur!=iDataCur || !HasRowid(pTab) );
131578 }
131579 }
131580 }
131581
131582 if( HasRowid(pTab) ){
131583 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
131584 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
131585 ** leave it in register regOldRowid. */
131586 sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
131587 if( eOnePass==ONEPASS_OFF ){
131588 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
131589 }
131590 }else{
131591 /* Read the PK of the current row into an array of registers. In
131592 ** ONEPASS_OFF mode, serialize the array into a record and store it in
131593 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
131594 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
131595 ** is not required) and leave the PK fields in the array of registers. */
131596 for(i=0; i<nPk; i++){
131597 assert( pPk->aiColumn[i]>=0 );
131598 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
131599 }
131600 if( eOnePass ){
131601 if( addrOpen ) sqlite3VdbeChangeToNoop(v, addrOpen);
131602 nKey = nPk;
131603 regKey = iPk;
131604 }else{
131605 sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
131606 sqlite3IndexAffinityStr(db, pPk), nPk);
131607 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
131608 }
131609 }
131610
131611 if( pUpsert==0 ){
131612 if( eOnePass!=ONEPASS_MULTI ){
131613 sqlite3WhereEnd(pWInfo);
131614 }
131615
131616 if( !isView ){
131617 int addrOnce = 0;
131618
131619 /* Open every index that needs updating. */
131620 if( eOnePass!=ONEPASS_OFF ){
131621 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
131622 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
131623 }
131624
131625 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
131626 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
131627 }
131628 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur,
131629 aToOpen, 0, 0);
131630 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
131631 }
131632
131633 /* Top of the update loop */
131634 if( eOnePass!=ONEPASS_OFF ){
131635 if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
131636 assert( pPk );
131637 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey,nKey);
131638 VdbeCoverage(v);
131639 }
131640 if( eOnePass!=ONEPASS_SINGLE ){
131641 labelContinue = sqlite3VdbeMakeLabel(v);
131642 }
131643 sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
131644 VdbeCoverageIf(v, pPk==0);
131645 VdbeCoverageIf(v, pPk!=0);
131646 }else if( pPk ){
131647 labelContinue = sqlite3VdbeMakeLabel(v);
131648 sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
131649 addrTop = sqlite3VdbeAddOp2(v, OP_RowData, iEph, regKey);
131650 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
131651 VdbeCoverage(v);
131652 }else{
131653 labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet,labelBreak,
131654 regOldRowid);
131655 VdbeCoverage(v);
131656 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
131657 VdbeCoverage(v);
131658 }
131659 }
131660
131661 /* If the rowid value will change, set register regNewRowid to
131662 ** contain the new value. If the rowid is not being modified,
131663 ** then regNewRowid is the same register as regOldRowid, which is
131664 ** already populated. */
131665 assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
131666 if( chngRowid ){
131667 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
131668 sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v);
131669 }
131670
131671 /* Compute the old pre-UPDATE content of the row being changed, if that
131672 ** information is needed */
131673 if( chngPk || hasFK || pTrigger ){
131674 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
131675 oldmask |= sqlite3TriggerColmask(pParse,
131676 pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
131677 );
131678 for(i=0; i<pTab->nCol; i++){
131679 if( oldmask==0xffffffff
131680 || (i<32 && (oldmask & MASKBIT32(i))!=0)
131681 || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
131682 ){
131683 testcase( oldmask!=0xffffffff && i==31 );
131684 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i);
131685 }else{
131686 sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
131687 }
131688 }
131689 if( chngRowid==0 && pPk==0 ){
131690 sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
131691 }
131692 }
131693
131694 /* Populate the array of registers beginning at regNew with the new
131695 ** row data. This array is used to check constants, create the new
131696 ** table and index records, and as the values for any new.* references
131697 ** made by triggers.
131698 **
131699 ** If there are one or more BEFORE triggers, then do not populate the
131700 ** registers associated with columns that are (a) not modified by
131701 ** this UPDATE statement and (b) not accessed by new.* references. The
131702 ** values for registers not modified by the UPDATE must be reloaded from
131703 ** the database after the BEFORE triggers are fired anyway (as the trigger
131704 ** may have modified them). So not loading those that are not going to
131705 ** be used eliminates some redundant opcodes.
131706 */
131707 newmask = sqlite3TriggerColmask(
131708 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
131709 );
131710 for(i=0; i<pTab->nCol; i++){
131711 if( i==pTab->iPKey ){
131712 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
131713 }else{
131714 j = aXRef[i];
131715 if( j>=0 ){
131716 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
131717 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){
131718 /* This branch loads the value of a column that will not be changed
131719 ** into a register. This is done if there are no BEFORE triggers, or
131720 ** if there are one or more BEFORE triggers that use this value via
131721 ** a new.* reference in a trigger program.
131722 */
131723 testcase( i==31 );
131724 testcase( i==32 );
131725 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
131726 }else{
131727 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
131728 }
131729 }
131730 }
131731
131732 /* Fire any BEFORE UPDATE triggers. This happens before constraints are
131733 ** verified. One could argue that this is wrong.
131734 */
131735 if( tmask&TRIGGER_BEFORE ){
131736 sqlite3TableAffinity(v, pTab, regNew);
131737 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
131738 TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue);
131739
131740 /* The row-trigger may have deleted the row being updated. In this
131741 ** case, jump to the next row. No updates or AFTER triggers are
131742 ** required. This behavior - what happens when the row being updated
131743 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
131744 ** documentation.
131745 */
131746 if( pPk ){
131747 sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey);
131748 VdbeCoverage(v);
131749 }else{
131750 sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
131751 VdbeCoverage(v);
131752 }
131753
131754 /* After-BEFORE-trigger-reload-loop:
131755 ** If it did not delete it, the BEFORE trigger may still have modified
131756 ** some of the columns of the row being updated. Load the values for
131757 ** all columns not modified by the update statement into their registers
131758 ** in case this has happened. Only unmodified columns are reloaded.
131759 ** The values computed for modified columns use the values before the
131760 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
131761 ** for an example.
131762 */
131763 for(i=0; i<pTab->nCol; i++){
131764 if( aXRef[i]<0 && i!=pTab->iPKey ){
131765 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
131766 }
131767 }
131768 }
131769
131770 if( !isView ){
131771 int addr1 = 0; /* Address of jump instruction */
131772
131773 /* Do constraint checks. */
131774 assert( regOldRowid>0 );
131775 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
131776 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
131777 aXRef, 0);
131778
131779 /* Do FK constraint checks. */
131780 if( hasFK ){
131781 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
131782 }
131783
131784 /* Delete the index entries associated with the current record. */
131785 if( bReplace || chngKey ){
131786 if( pPk ){
131787 addr1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey);
131788 }else{
131789 addr1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid);
131790 }
131791 VdbeCoverageNeverTaken(v);
131792 }
131793 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
131794
131795 /* If changing the rowid value, or if there are foreign key constraints
131796 ** to process, delete the old record. Otherwise, add a noop OP_Delete
131797 ** to invoke the pre-update hook.
131798 **
131799 ** That (regNew==regnewRowid+1) is true is also important for the
131800 ** pre-update hook. If the caller invokes preupdate_new(), the returned
131801 ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
131802 ** is the column index supplied by the user.
131803 */
131804 assert( regNew==regNewRowid+1 );
131805#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
131806 sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
131807 OPFLAG_ISUPDATE | ((hasFK>1 || chngKey) ? 0 : OPFLAG_ISNOOP),
131808 regNewRowid
131809 );
131810 if( eOnePass==ONEPASS_MULTI ){
131811 assert( hasFK==0 && chngKey==0 );
131812 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
131813 }
131814 if( !pParse->nested ){
131815 sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
131816 }
131817#else
131818 if( hasFK>1 || chngKey ){
131819 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
131820 }
131821#endif
131822 if( bReplace || chngKey ){
131823 sqlite3VdbeJumpHere(v, addr1);
131824 }
131825
131826 if( hasFK ){
131827 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
131828 }
131829
131830 /* Insert the new index entries and the new record. */
131831 sqlite3CompleteInsertion(
131832 pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,
131833 OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0),
131834 0, 0
131835 );
131836
131837 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
131838 ** handle rows (possibly in other tables) that refer via a foreign key
131839 ** to the row just updated. */
131840 if( hasFK ){
131841 sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
131842 }
131843 }
131844
131845 /* Increment the row counter
131846 */
131847 if( regRowCount ){
131848 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
131849 }
131850
131851 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
131852 TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
131853
131854 /* Repeat the above with the next record to be updated, until
131855 ** all record selected by the WHERE clause have been updated.
131856 */
131857 if( eOnePass==ONEPASS_SINGLE ){
131858 /* Nothing to do at end-of-loop for a single-pass */
131859 }else if( eOnePass==ONEPASS_MULTI ){
131860 sqlite3VdbeResolveLabel(v, labelContinue);
131861 sqlite3WhereEnd(pWInfo);
131862 }else if( pPk ){
131863 sqlite3VdbeResolveLabel(v, labelContinue);
131864 sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
131865 }else{
131866 sqlite3VdbeGoto(v, labelContinue);
131867 }
131868 sqlite3VdbeResolveLabel(v, labelBreak);
131869
131870 /* Update the sqlite_sequence table by storing the content of the
131871 ** maximum rowid counter values recorded while inserting into
131872 ** autoincrement tables.
131873 */
131874 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
131875 sqlite3AutoincrementEnd(pParse);
131876 }
131877
131878 /*
131879 ** Return the number of rows that were changed, if we are tracking
131880 ** that information.
131881 */
131882 if( regRowCount ){
131883 sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
131884 sqlite3VdbeSetNumCols(v, 1);
131885 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
131886 }
131887
131888update_cleanup:
131889 sqlite3AuthContextPop(&sContext);
131890 sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
131891 sqlite3SrcListDelete(db, pTabList);
131892 sqlite3ExprListDelete(db, pChanges);
131893 sqlite3ExprDelete(db, pWhere);
131894#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
131895 sqlite3ExprListDelete(db, pOrderBy);
131896 sqlite3ExprDelete(db, pLimit);
131897#endif
131898 return;
131899}
131900/* Make sure "isView" and other macros defined above are undefined. Otherwise
131901** they may interfere with compilation of other functions in this file
131902** (or in another file, if this file becomes part of the amalgamation). */
131903#ifdef isView
131904 #undef isView
131905#endif
131906#ifdef pTrigger
131907 #undef pTrigger
131908#endif
131909
131910#ifndef SQLITE_OMIT_VIRTUALTABLE
131911/*
131912** Generate code for an UPDATE of a virtual table.
131913**
131914** There are two possible strategies - the default and the special
131915** "onepass" strategy. Onepass is only used if the virtual table
131916** implementation indicates that pWhere may match at most one row.
131917**
131918** The default strategy is to create an ephemeral table that contains
131919** for each row to be changed:
131920**
131921** (A) The original rowid of that row.
131922** (B) The revised rowid for the row.
131923** (C) The content of every column in the row.
131924**
131925** Then loop through the contents of this ephemeral table executing a
131926** VUpdate for each row. When finished, drop the ephemeral table.
131927**
131928** The "onepass" strategy does not use an ephemeral table. Instead, it
131929** stores the same values (A, B and C above) in a register array and
131930** makes a single invocation of VUpdate.
131931*/
131932static void updateVirtualTable(
131933 Parse *pParse, /* The parsing context */
131934 SrcList *pSrc, /* The virtual table to be modified */
131935 Table *pTab, /* The virtual table */
131936 ExprList *pChanges, /* The columns to change in the UPDATE statement */
131937 Expr *pRowid, /* Expression used to recompute the rowid */
131938 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
131939 Expr *pWhere, /* WHERE clause of the UPDATE statement */
131940 int onError /* ON CONFLICT strategy */
131941){
131942 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
131943 int ephemTab; /* Table holding the result of the SELECT */
131944 int i; /* Loop counter */
131945 sqlite3 *db = pParse->db; /* Database connection */
131946 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
131947 WhereInfo *pWInfo;
131948 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
131949 int regArg; /* First register in VUpdate arg array */
131950 int regRec; /* Register in which to assemble record */
131951 int regRowid; /* Register for ephem table rowid */
131952 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
131953 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
131954 int eOnePass; /* True to use onepass strategy */
131955 int addr; /* Address of OP_OpenEphemeral */
131956
131957 /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
131958 ** create and open the ephemeral table in which the records created from
131959 ** these arguments will be temporarily stored. */
131960 assert( v );
131961 ephemTab = pParse->nTab++;
131962 addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, nArg);
131963 regArg = pParse->nMem + 1;
131964 pParse->nMem += nArg;
131965 regRec = ++pParse->nMem;
131966 regRowid = ++pParse->nMem;
131967
131968 /* Start scanning the virtual table */
131969 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0,0,WHERE_ONEPASS_DESIRED,0);
131970 if( pWInfo==0 ) return;
131971
131972 /* Populate the argument registers. */
131973 for(i=0; i<pTab->nCol; i++){
131974 if( aXRef[i]>=0 ){
131975 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
131976 }else{
131977 sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i);
131978 sqlite3VdbeChangeP5(v, 1); /* Enable sqlite3_vtab_nochange() */
131979 }
131980 }
131981 if( HasRowid(pTab) ){
131982 sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg);
131983 if( pRowid ){
131984 sqlite3ExprCode(pParse, pRowid, regArg+1);
131985 }else{
131986 sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg+1);
131987 }
131988 }else{
131989 Index *pPk; /* PRIMARY KEY index */
131990 i16 iPk; /* PRIMARY KEY column */
131991 pPk = sqlite3PrimaryKeyIndex(pTab);
131992 assert( pPk!=0 );
131993 assert( pPk->nKeyCol==1 );
131994 iPk = pPk->aiColumn[0];
131995 sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, iPk, regArg);
131996 sqlite3VdbeAddOp2(v, OP_SCopy, regArg+2+iPk, regArg+1);
131997 }
131998
131999 eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
132000
132001 /* There is no ONEPASS_MULTI on virtual tables */
132002 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
132003
132004 if( eOnePass ){
132005 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
132006 ** above. */
132007 sqlite3VdbeChangeToNoop(v, addr);
132008 sqlite3VdbeAddOp1(v, OP_Close, iCsr);
132009 }else{
132010 /* Create a record from the argument register contents and insert it into
132011 ** the ephemeral table. */
132012 sqlite3MultiWrite(pParse);
132013 sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
132014#ifdef SQLITE_DEBUG
132015 /* Signal an assert() within OP_MakeRecord that it is allowed to
132016 ** accept no-change records with serial_type 10 */
132017 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC);
132018#endif
132019 sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
132020 sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
132021 }
132022
132023
132024 if( eOnePass==ONEPASS_OFF ){
132025 /* End the virtual table scan */
132026 sqlite3WhereEnd(pWInfo);
132027
132028 /* Begin scannning through the ephemeral table. */
132029 addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v);
132030
132031 /* Extract arguments from the current row of the ephemeral table and
132032 ** invoke the VUpdate method. */
132033 for(i=0; i<nArg; i++){
132034 sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i, regArg+i);
132035 }
132036 }
132037 sqlite3VtabMakeWritable(pParse, pTab);
132038 sqlite3VdbeAddOp4(v, OP_VUpdate, 0, nArg, regArg, pVTab, P4_VTAB);
132039 sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
132040 sqlite3MayAbort(pParse);
132041
132042 /* End of the ephemeral table scan. Or, if using the onepass strategy,
132043 ** jump to here if the scan visited zero rows. */
132044 if( eOnePass==ONEPASS_OFF ){
132045 sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
132046 sqlite3VdbeJumpHere(v, addr);
132047 sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
132048 }else{
132049 sqlite3WhereEnd(pWInfo);
132050 }
132051}
132052#endif /* SQLITE_OMIT_VIRTUALTABLE */
132053
132054/************** End of update.c **********************************************/
132055/************** Begin file upsert.c ******************************************/
132056/*
132057** 2018-04-12
132058**
132059** The author disclaims copyright to this source code. In place of
132060** a legal notice, here is a blessing:
132061**
132062** May you do good and not evil.
132063** May you find forgiveness for yourself and forgive others.
132064** May you share freely, never taking more than you give.
132065**
132066*************************************************************************
132067** This file contains code to implement various aspects of UPSERT
132068** processing and handling of the Upsert object.
132069*/
132070/* #include "sqliteInt.h" */
132071
132072#ifndef SQLITE_OMIT_UPSERT
132073/*
132074** Free a list of Upsert objects
132075*/
132076SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
132077 if( p ){
132078 sqlite3ExprListDelete(db, p->pUpsertTarget);
132079 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
132080 sqlite3ExprListDelete(db, p->pUpsertSet);
132081 sqlite3ExprDelete(db, p->pUpsertWhere);
132082 sqlite3DbFree(db, p);
132083 }
132084}
132085
132086/*
132087** Duplicate an Upsert object.
132088*/
132089SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
132090 if( p==0 ) return 0;
132091 return sqlite3UpsertNew(db,
132092 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
132093 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
132094 sqlite3ExprListDup(db, p->pUpsertSet, 0),
132095 sqlite3ExprDup(db, p->pUpsertWhere, 0)
132096 );
132097}
132098
132099/*
132100** Create a new Upsert object.
132101*/
132102SQLITE_PRIVATE Upsert *sqlite3UpsertNew(
132103 sqlite3 *db, /* Determines which memory allocator to use */
132104 ExprList *pTarget, /* Target argument to ON CONFLICT, or NULL */
132105 Expr *pTargetWhere, /* Optional WHERE clause on the target */
132106 ExprList *pSet, /* UPDATE columns, or NULL for a DO NOTHING */
132107 Expr *pWhere /* WHERE clause for the ON CONFLICT UPDATE */
132108){
132109 Upsert *pNew;
132110 pNew = sqlite3DbMallocRaw(db, sizeof(Upsert));
132111 if( pNew==0 ){
132112 sqlite3ExprListDelete(db, pTarget);
132113 sqlite3ExprDelete(db, pTargetWhere);
132114 sqlite3ExprListDelete(db, pSet);
132115 sqlite3ExprDelete(db, pWhere);
132116 return 0;
132117 }else{
132118 pNew->pUpsertTarget = pTarget;
132119 pNew->pUpsertTargetWhere = pTargetWhere;
132120 pNew->pUpsertSet = pSet;
132121 pNew->pUpsertWhere = pWhere;
132122 pNew->pUpsertIdx = 0;
132123 }
132124 return pNew;
132125}
132126
132127/*
132128** Analyze the ON CONFLICT clause described by pUpsert. Resolve all
132129** symbols in the conflict-target.
132130**
132131** Return SQLITE_OK if everything works, or an error code is something
132132** is wrong.
132133*/
132134SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(
132135 Parse *pParse, /* The parsing context */
132136 SrcList *pTabList, /* Table into which we are inserting */
132137 Upsert *pUpsert /* The ON CONFLICT clauses */
132138){
132139 Table *pTab; /* That table into which we are inserting */
132140 int rc; /* Result code */
132141 int iCursor; /* Cursor used by pTab */
132142 Index *pIdx; /* One of the indexes of pTab */
132143 ExprList *pTarget; /* The conflict-target clause */
132144 Expr *pTerm; /* One term of the conflict-target clause */
132145 NameContext sNC; /* Context for resolving symbolic names */
132146 Expr sCol[2]; /* Index column converted into an Expr */
132147
132148 assert( pTabList->nSrc==1 );
132149 assert( pTabList->a[0].pTab!=0 );
132150 assert( pUpsert!=0 );
132151 assert( pUpsert->pUpsertTarget!=0 );
132152
132153 /* Resolve all symbolic names in the conflict-target clause, which
132154 ** includes both the list of columns and the optional partial-index
132155 ** WHERE clause.
132156 */
132157 memset(&sNC, 0, sizeof(sNC));
132158 sNC.pParse = pParse;
132159 sNC.pSrcList = pTabList;
132160 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
132161 if( rc ) return rc;
132162 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
132163 if( rc ) return rc;
132164
132165 /* Check to see if the conflict target matches the rowid. */
132166 pTab = pTabList->a[0].pTab;
132167 pTarget = pUpsert->pUpsertTarget;
132168 iCursor = pTabList->a[0].iCursor;
132169 if( HasRowid(pTab)
132170 && pTarget->nExpr==1
132171 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
132172 && pTerm->iColumn==XN_ROWID
132173 ){
132174 /* The conflict-target is the rowid of the primary table */
132175 assert( pUpsert->pUpsertIdx==0 );
132176 return SQLITE_OK;
132177 }
132178
132179 /* Initialize sCol[0..1] to be an expression parse tree for a
132180 ** single column of an index. The sCol[0] node will be the TK_COLLATE
132181 ** operator and sCol[1] will be the TK_COLUMN operator. Code below
132182 ** will populate the specific collation and column number values
132183 ** prior to comparing against the conflict-target expression.
132184 */
132185 memset(sCol, 0, sizeof(sCol));
132186 sCol[0].op = TK_COLLATE;
132187 sCol[0].pLeft = &sCol[1];
132188 sCol[1].op = TK_COLUMN;
132189 sCol[1].iTable = pTabList->a[0].iCursor;
132190
132191 /* Check for matches against other indexes */
132192 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
132193 int ii, jj, nn;
132194 if( !IsUniqueIndex(pIdx) ) continue;
132195 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
132196 if( pIdx->pPartIdxWhere ){
132197 if( pUpsert->pUpsertTargetWhere==0 ) continue;
132198 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
132199 pIdx->pPartIdxWhere, iCursor)!=0 ){
132200 continue;
132201 }
132202 }
132203 nn = pIdx->nKeyCol;
132204 for(ii=0; ii<nn; ii++){
132205 Expr *pExpr;
132206 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
132207 if( pIdx->aiColumn[ii]==XN_EXPR ){
132208 assert( pIdx->aColExpr!=0 );
132209 assert( pIdx->aColExpr->nExpr>ii );
132210 pExpr = pIdx->aColExpr->a[ii].pExpr;
132211 if( pExpr->op!=TK_COLLATE ){
132212 sCol[0].pLeft = pExpr;
132213 pExpr = &sCol[0];
132214 }
132215 }else{
132216 sCol[0].pLeft = &sCol[1];
132217 sCol[1].iColumn = pIdx->aiColumn[ii];
132218 pExpr = &sCol[0];
132219 }
132220 for(jj=0; jj<nn; jj++){
132221 if( sqlite3ExprCompare(pParse, pTarget->a[jj].pExpr, pExpr,iCursor)<2 ){
132222 break; /* Column ii of the index matches column jj of target */
132223 }
132224 }
132225 if( jj>=nn ){
132226 /* The target contains no match for column jj of the index */
132227 break;
132228 }
132229 }
132230 if( ii<nn ){
132231 /* Column ii of the index did not match any term of the conflict target.
132232 ** Continue the search with the next index. */
132233 continue;
132234 }
132235 pUpsert->pUpsertIdx = pIdx;
132236 return SQLITE_OK;
132237 }
132238 sqlite3ErrorMsg(pParse, "ON CONFLICT clause does not match any "
132239 "PRIMARY KEY or UNIQUE constraint");
132240 return SQLITE_ERROR;
132241}
132242
132243/*
132244** Generate bytecode that does an UPDATE as part of an upsert.
132245**
132246** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
132247** In this case parameter iCur is a cursor open on the table b-tree that
132248** currently points to the conflicting table row. Otherwise, if pIdx
132249** is not NULL, then pIdx is the constraint that failed and iCur is a
132250** cursor points to the conflicting row.
132251*/
132252SQLITE_PRIVATE void sqlite3UpsertDoUpdate(
132253 Parse *pParse, /* The parsing and code-generating context */
132254 Upsert *pUpsert, /* The ON CONFLICT clause for the upsert */
132255 Table *pTab, /* The table being updated */
132256 Index *pIdx, /* The UNIQUE constraint that failed */
132257 int iCur /* Cursor for pIdx (or pTab if pIdx==NULL) */
132258){
132259 Vdbe *v = pParse->pVdbe;
132260 sqlite3 *db = pParse->db;
132261 SrcList *pSrc; /* FROM clause for the UPDATE */
132262 int iDataCur;
132263
132264 assert( v!=0 );
132265 assert( pUpsert!=0 );
132266 VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
132267 iDataCur = pUpsert->iDataCur;
132268 if( pIdx && iCur!=iDataCur ){
132269 if( HasRowid(pTab) ){
132270 int regRowid = sqlite3GetTempReg(pParse);
132271 sqlite3VdbeAddOp2(v, OP_IdxRowid, iCur, regRowid);
132272 sqlite3VdbeAddOp3(v, OP_SeekRowid, iDataCur, 0, regRowid);
132273 VdbeCoverage(v);
132274 sqlite3ReleaseTempReg(pParse, regRowid);
132275 }else{
132276 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
132277 int nPk = pPk->nKeyCol;
132278 int iPk = pParse->nMem+1;
132279 int i;
132280 pParse->nMem += nPk;
132281 for(i=0; i<nPk; i++){
132282 int k;
132283 assert( pPk->aiColumn[i]>=0 );
132284 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
132285 sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i);
132286 VdbeComment((v, "%s.%s", pIdx->zName,
132287 pTab->aCol[pPk->aiColumn[i]].zName));
132288 }
132289 sqlite3VdbeVerifyAbortable(v, OE_Abort);
132290 i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk);
132291 VdbeCoverage(v);
132292 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0,
132293 "corrupt database", P4_STATIC);
132294 sqlite3VdbeJumpHere(v, i);
132295 }
132296 }
132297 /* pUpsert does not own pUpsertSrc - the outer INSERT statement does. So
132298 ** we have to make a copy before passing it down into sqlite3Update() */
132299 pSrc = sqlite3SrcListDup(db, pUpsert->pUpsertSrc, 0);
132300 sqlite3Update(pParse, pSrc, pUpsert->pUpsertSet,
132301 pUpsert->pUpsertWhere, OE_Abort, 0, 0, pUpsert);
132302 pUpsert->pUpsertSet = 0; /* Will have been deleted by sqlite3Update() */
132303 pUpsert->pUpsertWhere = 0; /* Will have been deleted by sqlite3Update() */
132304 VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
132305}
132306
132307#endif /* SQLITE_OMIT_UPSERT */
132308
132309/************** End of upsert.c **********************************************/
132310/************** Begin file vacuum.c ******************************************/
132311/*
132312** 2003 April 6
132313**
132314** The author disclaims copyright to this source code. In place of
132315** a legal notice, here is a blessing:
132316**
132317** May you do good and not evil.
132318** May you find forgiveness for yourself and forgive others.
132319** May you share freely, never taking more than you give.
132320**
132321*************************************************************************
132322** This file contains code used to implement the VACUUM command.
132323**
132324** Most of the code in this file may be omitted by defining the
132325** SQLITE_OMIT_VACUUM macro.
132326*/
132327/* #include "sqliteInt.h" */
132328/* #include "vdbeInt.h" */
132329
132330#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
132331
132332/*
132333** Execute zSql on database db.
132334**
132335** If zSql returns rows, then each row will have exactly one
132336** column. (This will only happen if zSql begins with "SELECT".)
132337** Take each row of result and call execSql() again recursively.
132338**
132339** The execSqlF() routine does the same thing, except it accepts
132340** a format string as its third argument
132341*/
132342static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
132343 sqlite3_stmt *pStmt;
132344 int rc;
132345
132346 /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
132347 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
132348 if( rc!=SQLITE_OK ) return rc;
132349 while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
132350 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
132351 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
132352 /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
132353 ** or INSERT. Historically there have been attacks that first
132354 ** corrupt the sqlite_master.sql field with other kinds of statements
132355 ** then run VACUUM to get those statements to execute at inappropriate
132356 ** times. */
132357 if( zSubSql
132358 && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
132359 ){
132360 rc = execSql(db, pzErrMsg, zSubSql);
132361 if( rc!=SQLITE_OK ) break;
132362 }
132363 }
132364 assert( rc!=SQLITE_ROW );
132365 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
132366 if( rc ){
132367 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
132368 }
132369 (void)sqlite3_finalize(pStmt);
132370 return rc;
132371}
132372static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
132373 char *z;
132374 va_list ap;
132375 int rc;
132376 va_start(ap, zSql);
132377 z = sqlite3VMPrintf(db, zSql, ap);
132378 va_end(ap);
132379 if( z==0 ) return SQLITE_NOMEM;
132380 rc = execSql(db, pzErrMsg, z);
132381 sqlite3DbFree(db, z);
132382 return rc;
132383}
132384
132385/*
132386** The VACUUM command is used to clean up the database,
132387** collapse free space, etc. It is modelled after the VACUUM command
132388** in PostgreSQL. The VACUUM command works as follows:
132389**
132390** (1) Create a new transient database file
132391** (2) Copy all content from the database being vacuumed into
132392** the new transient database file
132393** (3) Copy content from the transient database back into the
132394** original database.
132395**
132396** The transient database requires temporary disk space approximately
132397** equal to the size of the original database. The copy operation of
132398** step (3) requires additional temporary disk space approximately equal
132399** to the size of the original database for the rollback journal.
132400** Hence, temporary disk space that is approximately 2x the size of the
132401** original database is required. Every page of the database is written
132402** approximately 3 times: Once for step (2) and twice for step (3).
132403** Two writes per page are required in step (3) because the original
132404** database content must be written into the rollback journal prior to
132405** overwriting the database with the vacuumed content.
132406**
132407** Only 1x temporary space and only 1x writes would be required if
132408** the copy of step (3) were replaced by deleting the original database
132409** and renaming the transient database as the original. But that will
132410** not work if other processes are attached to the original database.
132411** And a power loss in between deleting the original and renaming the
132412** transient would cause the database file to appear to be deleted
132413** following reboot.
132414*/
132415SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse, Token *pNm){
132416 Vdbe *v = sqlite3GetVdbe(pParse);
132417 int iDb = 0;
132418 if( v==0 ) return;
132419 if( pNm ){
132420#ifndef SQLITE_BUG_COMPATIBLE_20160819
132421 /* Default behavior: Report an error if the argument to VACUUM is
132422 ** not recognized */
132423 iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm);
132424 if( iDb<0 ) return;
132425#else
132426 /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
132427 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
132428 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
132429 ** The buggy behavior is required for binary compatibility with some
132430 ** legacy applications. */
132431 iDb = sqlite3FindDb(pParse->db, pNm);
132432 if( iDb<0 ) iDb = 0;
132433#endif
132434 }
132435 if( iDb!=1 ){
132436 sqlite3VdbeAddOp1(v, OP_Vacuum, iDb);
132437 sqlite3VdbeUsesBtree(v, iDb);
132438 }
132439 return;
132440}
132441
132442/*
132443** This routine implements the OP_Vacuum opcode of the VDBE.
132444*/
132445SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db, int iDb){
132446 int rc = SQLITE_OK; /* Return code from service routines */
132447 Btree *pMain; /* The database being vacuumed */
132448 Btree *pTemp; /* The temporary database we vacuum into */
132449 u16 saved_mDbFlags; /* Saved value of db->mDbFlags */
132450 u32 saved_flags; /* Saved value of db->flags */
132451 int saved_nChange; /* Saved value of db->nChange */
132452 int saved_nTotalChange; /* Saved value of db->nTotalChange */
132453 u8 saved_mTrace; /* Saved trace settings */
132454 Db *pDb = 0; /* Database to detach at end of vacuum */
132455 int isMemDb; /* True if vacuuming a :memory: database */
132456 int nRes; /* Bytes of reserved space at the end of each page */
132457 int nDb; /* Number of attached databases */
132458 const char *zDbMain; /* Schema name of database to vacuum */
132459
132460 if( !db->autoCommit ){
132461 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
132462 return SQLITE_ERROR;
132463 }
132464 if( db->nVdbeActive>1 ){
132465 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
132466 return SQLITE_ERROR;
132467 }
132468
132469 /* Save the current value of the database flags so that it can be
132470 ** restored before returning. Then set the writable-schema flag, and
132471 ** disable CHECK and foreign key constraints. */
132472 saved_flags = db->flags;
132473 saved_mDbFlags = db->mDbFlags;
132474 saved_nChange = db->nChange;
132475 saved_nTotalChange = db->nTotalChange;
132476 saved_mTrace = db->mTrace;
132477 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
132478 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
132479 db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_CountRows);
132480 db->mTrace = 0;
132481
132482 zDbMain = db->aDb[iDb].zDbSName;
132483 pMain = db->aDb[iDb].pBt;
132484 isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
132485
132486 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
132487 ** can be set to 'off' for this file, as it is not recovered if a crash
132488 ** occurs anyway. The integrity of the database is maintained by a
132489 ** (possibly synchronous) transaction opened on the main database before
132490 ** sqlite3BtreeCopyFile() is called.
132491 **
132492 ** An optimisation would be to use a non-journaled pager.
132493 ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
132494 ** that actually made the VACUUM run slower. Very little journalling
132495 ** actually occurs when doing a vacuum since the vacuum_db is initially
132496 ** empty. Only the journal header is written. Apparently it takes more
132497 ** time to parse and run the PRAGMA to turn journalling off than it does
132498 ** to write the journal header file.
132499 */
132500 nDb = db->nDb;
132501 rc = execSql(db, pzErrMsg, "ATTACH''AS vacuum_db");
132502 if( rc!=SQLITE_OK ) goto end_of_vacuum;
132503 assert( (db->nDb-1)==nDb );
132504 pDb = &db->aDb[nDb];
132505 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
132506 pTemp = pDb->pBt;
132507
132508 /* The call to execSql() to attach the temp database has left the file
132509 ** locked (as there was more than one active statement when the transaction
132510 ** to read the schema was concluded. Unlock it here so that this doesn't
132511 ** cause problems for the call to BtreeSetPageSize() below. */
132512 sqlite3BtreeCommit(pTemp);
132513
132514 nRes = sqlite3BtreeGetOptimalReserve(pMain);
132515
132516 /* A VACUUM cannot change the pagesize of an encrypted database. */
132517#ifdef SQLITE_HAS_CODEC
132518 if( db->nextPagesize ){
132519 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
132520 int nKey;
132521 char *zKey;
132522 sqlite3CodecGetKey(db, iDb, (void**)&zKey, &nKey);
132523 if( nKey ) db->nextPagesize = 0;
132524 }
132525#endif
132526
132527 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
132528 sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
132529 sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF|PAGER_CACHESPILL);
132530
132531 /* Begin a transaction and take an exclusive lock on the main database
132532 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
132533 ** to ensure that we do not try to change the page-size on a WAL database.
132534 */
132535 rc = execSql(db, pzErrMsg, "BEGIN");
132536 if( rc!=SQLITE_OK ) goto end_of_vacuum;
132537 rc = sqlite3BtreeBeginTrans(pMain, 2, 0);
132538 if( rc!=SQLITE_OK ) goto end_of_vacuum;
132539
132540 /* Do not attempt to change the page size for a WAL database */
132541 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
132542 ==PAGER_JOURNALMODE_WAL ){
132543 db->nextPagesize = 0;
132544 }
132545
132546 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
132547 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
132548 || NEVER(db->mallocFailed)
132549 ){
132550 rc = SQLITE_NOMEM_BKPT;
132551 goto end_of_vacuum;
132552 }
132553
132554#ifndef SQLITE_OMIT_AUTOVACUUM
132555 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
132556 sqlite3BtreeGetAutoVacuum(pMain));
132557#endif
132558
132559 /* Query the schema of the main database. Create a mirror schema
132560 ** in the temporary database.
132561 */
132562 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
132563 rc = execSqlF(db, pzErrMsg,
132564 "SELECT sql FROM \"%w\".sqlite_master"
132565 " WHERE type='table'AND name<>'sqlite_sequence'"
132566 " AND coalesce(rootpage,1)>0",
132567 zDbMain
132568 );
132569 if( rc!=SQLITE_OK ) goto end_of_vacuum;
132570 rc = execSqlF(db, pzErrMsg,
132571 "SELECT sql FROM \"%w\".sqlite_master"
132572 " WHERE type='index'",
132573 zDbMain
132574 );
132575 if( rc!=SQLITE_OK ) goto end_of_vacuum;
132576 db->init.iDb = 0;
132577
132578 /* Loop through the tables in the main database. For each, do
132579 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
132580 ** the contents to the temporary database.
132581 */
132582 rc = execSqlF(db, pzErrMsg,
132583 "SELECT'INSERT INTO vacuum_db.'||quote(name)"
132584 "||' SELECT*FROM\"%w\".'||quote(name)"
132585 "FROM vacuum_db.sqlite_master "
132586 "WHERE type='table'AND coalesce(rootpage,1)>0",
132587 zDbMain
132588 );
132589 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
132590 db->mDbFlags &= ~DBFLAG_Vacuum;
132591 if( rc!=SQLITE_OK ) goto end_of_vacuum;
132592
132593 /* Copy the triggers, views, and virtual tables from the main database
132594 ** over to the temporary database. None of these objects has any
132595 ** associated storage, so all we have to do is copy their entries
132596 ** from the SQLITE_MASTER table.
132597 */
132598 rc = execSqlF(db, pzErrMsg,
132599 "INSERT INTO vacuum_db.sqlite_master"
132600 " SELECT*FROM \"%w\".sqlite_master"
132601 " WHERE type IN('view','trigger')"
132602 " OR(type='table'AND rootpage=0)",
132603 zDbMain
132604 );
132605 if( rc ) goto end_of_vacuum;
132606
132607 /* At this point, there is a write transaction open on both the
132608 ** vacuum database and the main database. Assuming no error occurs,
132609 ** both transactions are closed by this block - the main database
132610 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
132611 ** call to sqlite3BtreeCommit().
132612 */
132613 {
132614 u32 meta;
132615 int i;
132616
132617 /* This array determines which meta meta values are preserved in the
132618 ** vacuum. Even entries are the meta value number and odd entries
132619 ** are an increment to apply to the meta value after the vacuum.
132620 ** The increment is used to increase the schema cookie so that other
132621 ** connections to the same database will know to reread the schema.
132622 */
132623 static const unsigned char aCopy[] = {
132624 BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */
132625 BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
132626 BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */
132627 BTREE_USER_VERSION, 0, /* Preserve the user version */
132628 BTREE_APPLICATION_ID, 0, /* Preserve the application id */
132629 };
132630
132631 assert( 1==sqlite3BtreeIsInTrans(pTemp) );
132632 assert( 1==sqlite3BtreeIsInTrans(pMain) );
132633
132634 /* Copy Btree meta values */
132635 for(i=0; i<ArraySize(aCopy); i+=2){
132636 /* GetMeta() and UpdateMeta() cannot fail in this context because
132637 ** we already have page 1 loaded into cache and marked dirty. */
132638 sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
132639 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
132640 if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
132641 }
132642
132643 rc = sqlite3BtreeCopyFile(pMain, pTemp);
132644 if( rc!=SQLITE_OK ) goto end_of_vacuum;
132645 rc = sqlite3BtreeCommit(pTemp);
132646 if( rc!=SQLITE_OK ) goto end_of_vacuum;
132647#ifndef SQLITE_OMIT_AUTOVACUUM
132648 sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
132649#endif
132650 }
132651
132652 assert( rc==SQLITE_OK );
132653 rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
132654
132655end_of_vacuum:
132656 /* Restore the original value of db->flags */
132657 db->init.iDb = 0;
132658 db->mDbFlags = saved_mDbFlags;
132659 db->flags = saved_flags;
132660 db->nChange = saved_nChange;
132661 db->nTotalChange = saved_nTotalChange;
132662 db->mTrace = saved_mTrace;
132663 sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
132664
132665 /* Currently there is an SQL level transaction open on the vacuum
132666 ** database. No locks are held on any other files (since the main file
132667 ** was committed at the btree level). So it safe to end the transaction
132668 ** by manually setting the autoCommit flag to true and detaching the
132669 ** vacuum database. The vacuum_db journal file is deleted when the pager
132670 ** is closed by the DETACH.
132671 */
132672 db->autoCommit = 1;
132673
132674 if( pDb ){
132675 sqlite3BtreeClose(pDb->pBt);
132676 pDb->pBt = 0;
132677 pDb->pSchema = 0;
132678 }
132679
132680 /* This both clears the schemas and reduces the size of the db->aDb[]
132681 ** array. */
132682 sqlite3ResetAllSchemasOfConnection(db);
132683
132684 return rc;
132685}
132686
132687#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
132688
132689/************** End of vacuum.c **********************************************/
132690/************** Begin file vtab.c ********************************************/
132691/*
132692** 2006 June 10
132693**
132694** The author disclaims copyright to this source code. In place of
132695** a legal notice, here is a blessing:
132696**
132697** May you do good and not evil.
132698** May you find forgiveness for yourself and forgive others.
132699** May you share freely, never taking more than you give.
132700**
132701*************************************************************************
132702** This file contains code used to help implement virtual tables.
132703*/
132704#ifndef SQLITE_OMIT_VIRTUALTABLE
132705/* #include "sqliteInt.h" */
132706
132707/*
132708** Before a virtual table xCreate() or xConnect() method is invoked, the
132709** sqlite3.pVtabCtx member variable is set to point to an instance of
132710** this struct allocated on the stack. It is used by the implementation of
132711** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
132712** are invoked only from within xCreate and xConnect methods.
132713*/
132714struct VtabCtx {
132715 VTable *pVTable; /* The virtual table being constructed */
132716 Table *pTab; /* The Table object to which the virtual table belongs */
132717 VtabCtx *pPrior; /* Parent context (if any) */
132718 int bDeclared; /* True after sqlite3_declare_vtab() is called */
132719};
132720
132721/*
132722** Construct and install a Module object for a virtual table. When this
132723** routine is called, it is guaranteed that all appropriate locks are held
132724** and the module is not already part of the connection.
132725*/
132726SQLITE_PRIVATE Module *sqlite3VtabCreateModule(
132727 sqlite3 *db, /* Database in which module is registered */
132728 const char *zName, /* Name assigned to this module */
132729 const sqlite3_module *pModule, /* The definition of the module */
132730 void *pAux, /* Context pointer for xCreate/xConnect */
132731 void (*xDestroy)(void *) /* Module destructor function */
132732){
132733 Module *pMod;
132734 int nName = sqlite3Strlen30(zName);
132735 pMod = (Module *)sqlite3Malloc(sizeof(Module) + nName + 1);
132736 if( pMod==0 ){
132737 sqlite3OomFault(db);
132738 }else{
132739 Module *pDel;
132740 char *zCopy = (char *)(&pMod[1]);
132741 memcpy(zCopy, zName, nName+1);
132742 pMod->zName = zCopy;
132743 pMod->pModule = pModule;
132744 pMod->pAux = pAux;
132745 pMod->xDestroy = xDestroy;
132746 pMod->pEpoTab = 0;
132747 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
132748 assert( pDel==0 || pDel==pMod );
132749 if( pDel ){
132750 sqlite3OomFault(db);
132751 sqlite3DbFree(db, pDel);
132752 pMod = 0;
132753 }
132754 }
132755 return pMod;
132756}
132757
132758/*
132759** The actual function that does the work of creating a new module.
132760** This function implements the sqlite3_create_module() and
132761** sqlite3_create_module_v2() interfaces.
132762*/
132763static int createModule(
132764 sqlite3 *db, /* Database in which module is registered */
132765 const char *zName, /* Name assigned to this module */
132766 const sqlite3_module *pModule, /* The definition of the module */
132767 void *pAux, /* Context pointer for xCreate/xConnect */
132768 void (*xDestroy)(void *) /* Module destructor function */
132769){
132770 int rc = SQLITE_OK;
132771
132772 sqlite3_mutex_enter(db->mutex);
132773 if( sqlite3HashFind(&db->aModule, zName) ){
132774 rc = SQLITE_MISUSE_BKPT;
132775 }else{
132776 (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy);
132777 }
132778 rc = sqlite3ApiExit(db, rc);
132779 if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
132780 sqlite3_mutex_leave(db->mutex);
132781 return rc;
132782}
132783
132784
132785/*
132786** External API function used to create a new virtual-table module.
132787*/
132788SQLITE_API int sqlite3_create_module(
132789 sqlite3 *db, /* Database in which module is registered */
132790 const char *zName, /* Name assigned to this module */
132791 const sqlite3_module *pModule, /* The definition of the module */
132792 void *pAux /* Context pointer for xCreate/xConnect */
132793){
132794#ifdef SQLITE_ENABLE_API_ARMOR
132795 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
132796#endif
132797 return createModule(db, zName, pModule, pAux, 0);
132798}
132799
132800/*
132801** External API function used to create a new virtual-table module.
132802*/
132803SQLITE_API int sqlite3_create_module_v2(
132804 sqlite3 *db, /* Database in which module is registered */
132805 const char *zName, /* Name assigned to this module */
132806 const sqlite3_module *pModule, /* The definition of the module */
132807 void *pAux, /* Context pointer for xCreate/xConnect */
132808 void (*xDestroy)(void *) /* Module destructor function */
132809){
132810#ifdef SQLITE_ENABLE_API_ARMOR
132811 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
132812#endif
132813 return createModule(db, zName, pModule, pAux, xDestroy);
132814}
132815
132816/*
132817** Lock the virtual table so that it cannot be disconnected.
132818** Locks nest. Every lock should have a corresponding unlock.
132819** If an unlock is omitted, resources leaks will occur.
132820**
132821** If a disconnect is attempted while a virtual table is locked,
132822** the disconnect is deferred until all locks have been removed.
132823*/
132824SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
132825 pVTab->nRef++;
132826}
132827
132828
132829/*
132830** pTab is a pointer to a Table structure representing a virtual-table.
132831** Return a pointer to the VTable object used by connection db to access
132832** this virtual-table, if one has been created, or NULL otherwise.
132833*/
132834SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
132835 VTable *pVtab;
132836 assert( IsVirtual(pTab) );
132837 for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
132838 return pVtab;
132839}
132840
132841/*
132842** Decrement the ref-count on a virtual table object. When the ref-count
132843** reaches zero, call the xDisconnect() method to delete the object.
132844*/
132845SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
132846 sqlite3 *db = pVTab->db;
132847
132848 assert( db );
132849 assert( pVTab->nRef>0 );
132850 assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
132851
132852 pVTab->nRef--;
132853 if( pVTab->nRef==0 ){
132854 sqlite3_vtab *p = pVTab->pVtab;
132855 if( p ){
132856 p->pModule->xDisconnect(p);
132857 }
132858 sqlite3DbFree(db, pVTab);
132859 }
132860}
132861
132862/*
132863** Table p is a virtual table. This function moves all elements in the
132864** p->pVTable list to the sqlite3.pDisconnect lists of their associated
132865** database connections to be disconnected at the next opportunity.
132866** Except, if argument db is not NULL, then the entry associated with
132867** connection db is left in the p->pVTable list.
132868*/
132869static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
132870 VTable *pRet = 0;
132871 VTable *pVTable = p->pVTable;
132872 p->pVTable = 0;
132873
132874 /* Assert that the mutex (if any) associated with the BtShared database
132875 ** that contains table p is held by the caller. See header comments
132876 ** above function sqlite3VtabUnlockList() for an explanation of why
132877 ** this makes it safe to access the sqlite3.pDisconnect list of any
132878 ** database connection that may have an entry in the p->pVTable list.
132879 */
132880 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
132881
132882 while( pVTable ){
132883 sqlite3 *db2 = pVTable->db;
132884 VTable *pNext = pVTable->pNext;
132885 assert( db2 );
132886 if( db2==db ){
132887 pRet = pVTable;
132888 p->pVTable = pRet;
132889 pRet->pNext = 0;
132890 }else{
132891 pVTable->pNext = db2->pDisconnect;
132892 db2->pDisconnect = pVTable;
132893 }
132894 pVTable = pNext;
132895 }
132896
132897 assert( !db || pRet );
132898 return pRet;
132899}
132900
132901/*
132902** Table *p is a virtual table. This function removes the VTable object
132903** for table *p associated with database connection db from the linked
132904** list in p->pVTab. It also decrements the VTable ref count. This is
132905** used when closing database connection db to free all of its VTable
132906** objects without disturbing the rest of the Schema object (which may
132907** be being used by other shared-cache connections).
132908*/
132909SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
132910 VTable **ppVTab;
132911
132912 assert( IsVirtual(p) );
132913 assert( sqlite3BtreeHoldsAllMutexes(db) );
132914 assert( sqlite3_mutex_held(db->mutex) );
132915
132916 for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
132917 if( (*ppVTab)->db==db ){
132918 VTable *pVTab = *ppVTab;
132919 *ppVTab = pVTab->pNext;
132920 sqlite3VtabUnlock(pVTab);
132921 break;
132922 }
132923 }
132924}
132925
132926
132927/*
132928** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
132929**
132930** This function may only be called when the mutexes associated with all
132931** shared b-tree databases opened using connection db are held by the
132932** caller. This is done to protect the sqlite3.pDisconnect list. The
132933** sqlite3.pDisconnect list is accessed only as follows:
132934**
132935** 1) By this function. In this case, all BtShared mutexes and the mutex
132936** associated with the database handle itself must be held.
132937**
132938** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
132939** the sqlite3.pDisconnect list. In this case either the BtShared mutex
132940** associated with the database the virtual table is stored in is held
132941** or, if the virtual table is stored in a non-sharable database, then
132942** the database handle mutex is held.
132943**
132944** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
132945** by multiple threads. It is thread-safe.
132946*/
132947SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
132948 VTable *p = db->pDisconnect;
132949 db->pDisconnect = 0;
132950
132951 assert( sqlite3BtreeHoldsAllMutexes(db) );
132952 assert( sqlite3_mutex_held(db->mutex) );
132953
132954 if( p ){
132955 sqlite3ExpirePreparedStatements(db, 0);
132956 do {
132957 VTable *pNext = p->pNext;
132958 sqlite3VtabUnlock(p);
132959 p = pNext;
132960 }while( p );
132961 }
132962}
132963
132964/*
132965** Clear any and all virtual-table information from the Table record.
132966** This routine is called, for example, just before deleting the Table
132967** record.
132968**
132969** Since it is a virtual-table, the Table structure contains a pointer
132970** to the head of a linked list of VTable structures. Each VTable
132971** structure is associated with a single sqlite3* user of the schema.
132972** The reference count of the VTable structure associated with database
132973** connection db is decremented immediately (which may lead to the
132974** structure being xDisconnected and free). Any other VTable structures
132975** in the list are moved to the sqlite3.pDisconnect list of the associated
132976** database connection.
132977*/
132978SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
132979 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
132980 if( p->azModuleArg ){
132981 int i;
132982 for(i=0; i<p->nModuleArg; i++){
132983 if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
132984 }
132985 sqlite3DbFree(db, p->azModuleArg);
132986 }
132987}
132988
132989/*
132990** Add a new module argument to pTable->azModuleArg[].
132991** The string is not copied - the pointer is stored. The
132992** string will be freed automatically when the table is
132993** deleted.
132994*/
132995static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
132996 int nBytes = sizeof(char *)*(2+pTable->nModuleArg);
132997 char **azModuleArg;
132998 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
132999 if( azModuleArg==0 ){
133000 sqlite3DbFree(db, zArg);
133001 }else{
133002 int i = pTable->nModuleArg++;
133003 azModuleArg[i] = zArg;
133004 azModuleArg[i+1] = 0;
133005 pTable->azModuleArg = azModuleArg;
133006 }
133007}
133008
133009/*
133010** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
133011** statement. The module name has been parsed, but the optional list
133012** of parameters that follow the module name are still pending.
133013*/
133014SQLITE_PRIVATE void sqlite3VtabBeginParse(
133015 Parse *pParse, /* Parsing context */
133016 Token *pName1, /* Name of new table, or database name */
133017 Token *pName2, /* Name of new table or NULL */
133018 Token *pModuleName, /* Name of the module for the virtual table */
133019 int ifNotExists /* No error if the table already exists */
133020){
133021 int iDb; /* The database the table is being created in */
133022 Table *pTable; /* The new virtual table */
133023 sqlite3 *db; /* Database connection */
133024
133025 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
133026 pTable = pParse->pNewTable;
133027 if( pTable==0 ) return;
133028 assert( 0==pTable->pIndex );
133029
133030 db = pParse->db;
133031 iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
133032 assert( iDb>=0 );
133033
133034 assert( pTable->nModuleArg==0 );
133035 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
133036 addModuleArgument(db, pTable, 0);
133037 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
133038 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
133039 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
133040 );
133041 pParse->sNameToken.n = (int)(
133042 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
133043 );
133044
133045#ifndef SQLITE_OMIT_AUTHORIZATION
133046 /* Creating a virtual table invokes the authorization callback twice.
133047 ** The first invocation, to obtain permission to INSERT a row into the
133048 ** sqlite_master table, has already been made by sqlite3StartTable().
133049 ** The second call, to obtain permission to create the table, is made now.
133050 */
133051 if( pTable->azModuleArg ){
133052 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
133053 pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
133054 }
133055#endif
133056}
133057
133058/*
133059** This routine takes the module argument that has been accumulating
133060** in pParse->zArg[] and appends it to the list of arguments on the
133061** virtual table currently under construction in pParse->pTable.
133062*/
133063static void addArgumentToVtab(Parse *pParse){
133064 if( pParse->sArg.z && pParse->pNewTable ){
133065 const char *z = (const char*)pParse->sArg.z;
133066 int n = pParse->sArg.n;
133067 sqlite3 *db = pParse->db;
133068 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
133069 }
133070}
133071
133072/*
133073** The parser calls this routine after the CREATE VIRTUAL TABLE statement
133074** has been completely parsed.
133075*/
133076SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
133077 Table *pTab = pParse->pNewTable; /* The table being constructed */
133078 sqlite3 *db = pParse->db; /* The database connection */
133079
133080 if( pTab==0 ) return;
133081 addArgumentToVtab(pParse);
133082 pParse->sArg.z = 0;
133083 if( pTab->nModuleArg<1 ) return;
133084
133085 /* If the CREATE VIRTUAL TABLE statement is being entered for the
133086 ** first time (in other words if the virtual table is actually being
133087 ** created now instead of just being read out of sqlite_master) then
133088 ** do additional initialization work and store the statement text
133089 ** in the sqlite_master table.
133090 */
133091 if( !db->init.busy ){
133092 char *zStmt;
133093 char *zWhere;
133094 int iDb;
133095 int iReg;
133096 Vdbe *v;
133097
133098 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
133099 if( pEnd ){
133100 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
133101 }
133102 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
133103
133104 /* A slot for the record has already been allocated in the
133105 ** SQLITE_MASTER table. We just need to update that slot with all
133106 ** the information we've collected.
133107 **
133108 ** The VM register number pParse->regRowid holds the rowid of an
133109 ** entry in the sqlite_master table tht was created for this vtab
133110 ** by sqlite3StartTable().
133111 */
133112 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133113 sqlite3NestedParse(pParse,
133114 "UPDATE %Q.%s "
133115 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
133116 "WHERE rowid=#%d",
133117 db->aDb[iDb].zDbSName, MASTER_NAME,
133118 pTab->zName,
133119 pTab->zName,
133120 zStmt,
133121 pParse->regRowid
133122 );
133123 sqlite3DbFree(db, zStmt);
133124 v = sqlite3GetVdbe(pParse);
133125 sqlite3ChangeCookie(pParse, iDb);
133126
133127 sqlite3VdbeAddOp0(v, OP_Expire);
133128 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
133129 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
133130
133131 iReg = ++pParse->nMem;
133132 sqlite3VdbeLoadString(v, iReg, pTab->zName);
133133 sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg);
133134 }
133135
133136 /* If we are rereading the sqlite_master table create the in-memory
133137 ** record of the table. The xConnect() method is not called until
133138 ** the first time the virtual table is used in an SQL statement. This
133139 ** allows a schema that contains virtual tables to be loaded before
133140 ** the required virtual table implementations are registered. */
133141 else {
133142 Table *pOld;
133143 Schema *pSchema = pTab->pSchema;
133144 const char *zName = pTab->zName;
133145 assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
133146 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
133147 if( pOld ){
133148 sqlite3OomFault(db);
133149 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
133150 return;
133151 }
133152 pParse->pNewTable = 0;
133153 }
133154}
133155
133156/*
133157** The parser calls this routine when it sees the first token
133158** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
133159*/
133160SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
133161 addArgumentToVtab(pParse);
133162 pParse->sArg.z = 0;
133163 pParse->sArg.n = 0;
133164}
133165
133166/*
133167** The parser calls this routine for each token after the first token
133168** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
133169*/
133170SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
133171 Token *pArg = &pParse->sArg;
133172 if( pArg->z==0 ){
133173 pArg->z = p->z;
133174 pArg->n = p->n;
133175 }else{
133176 assert(pArg->z <= p->z);
133177 pArg->n = (int)(&p->z[p->n] - pArg->z);
133178 }
133179}
133180
133181/*
133182** Invoke a virtual table constructor (either xCreate or xConnect). The
133183** pointer to the function to invoke is passed as the fourth parameter
133184** to this procedure.
133185*/
133186static int vtabCallConstructor(
133187 sqlite3 *db,
133188 Table *pTab,
133189 Module *pMod,
133190 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
133191 char **pzErr
133192){
133193 VtabCtx sCtx;
133194 VTable *pVTable;
133195 int rc;
133196 const char *const*azArg = (const char *const*)pTab->azModuleArg;
133197 int nArg = pTab->nModuleArg;
133198 char *zErr = 0;
133199 char *zModuleName;
133200 int iDb;
133201 VtabCtx *pCtx;
133202
133203 /* Check that the virtual-table is not already being initialized */
133204 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
133205 if( pCtx->pTab==pTab ){
133206 *pzErr = sqlite3MPrintf(db,
133207 "vtable constructor called recursively: %s", pTab->zName
133208 );
133209 return SQLITE_LOCKED;
133210 }
133211 }
133212
133213 zModuleName = sqlite3DbStrDup(db, pTab->zName);
133214 if( !zModuleName ){
133215 return SQLITE_NOMEM_BKPT;
133216 }
133217
133218 pVTable = sqlite3MallocZero(sizeof(VTable));
133219 if( !pVTable ){
133220 sqlite3OomFault(db);
133221 sqlite3DbFree(db, zModuleName);
133222 return SQLITE_NOMEM_BKPT;
133223 }
133224 pVTable->db = db;
133225 pVTable->pMod = pMod;
133226
133227 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133228 pTab->azModuleArg[1] = db->aDb[iDb].zDbSName;
133229
133230 /* Invoke the virtual table constructor */
133231 assert( &db->pVtabCtx );
133232 assert( xConstruct );
133233 sCtx.pTab = pTab;
133234 sCtx.pVTable = pVTable;
133235 sCtx.pPrior = db->pVtabCtx;
133236 sCtx.bDeclared = 0;
133237 db->pVtabCtx = &sCtx;
133238 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
133239 db->pVtabCtx = sCtx.pPrior;
133240 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
133241 assert( sCtx.pTab==pTab );
133242
133243 if( SQLITE_OK!=rc ){
133244 if( zErr==0 ){
133245 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
133246 }else {
133247 *pzErr = sqlite3MPrintf(db, "%s", zErr);
133248 sqlite3_free(zErr);
133249 }
133250 sqlite3DbFree(db, pVTable);
133251 }else if( ALWAYS(pVTable->pVtab) ){
133252 /* Justification of ALWAYS(): A correct vtab constructor must allocate
133253 ** the sqlite3_vtab object if successful. */
133254 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
133255 pVTable->pVtab->pModule = pMod->pModule;
133256 pVTable->nRef = 1;
133257 if( sCtx.bDeclared==0 ){
133258 const char *zFormat = "vtable constructor did not declare schema: %s";
133259 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
133260 sqlite3VtabUnlock(pVTable);
133261 rc = SQLITE_ERROR;
133262 }else{
133263 int iCol;
133264 u8 oooHidden = 0;
133265 /* If everything went according to plan, link the new VTable structure
133266 ** into the linked list headed by pTab->pVTable. Then loop through the
133267 ** columns of the table to see if any of them contain the token "hidden".
133268 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
133269 ** the type string. */
133270 pVTable->pNext = pTab->pVTable;
133271 pTab->pVTable = pVTable;
133272
133273 for(iCol=0; iCol<pTab->nCol; iCol++){
133274 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
133275 int nType;
133276 int i = 0;
133277 nType = sqlite3Strlen30(zType);
133278 for(i=0; i<nType; i++){
133279 if( 0==sqlite3StrNICmp("hidden", &zType[i], 6)
133280 && (i==0 || zType[i-1]==' ')
133281 && (zType[i+6]=='\0' || zType[i+6]==' ')
133282 ){
133283 break;
133284 }
133285 }
133286 if( i<nType ){
133287 int j;
133288 int nDel = 6 + (zType[i+6] ? 1 : 0);
133289 for(j=i; (j+nDel)<=nType; j++){
133290 zType[j] = zType[j+nDel];
133291 }
133292 if( zType[i]=='\0' && i>0 ){
133293 assert(zType[i-1]==' ');
133294 zType[i-1] = '\0';
133295 }
133296 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
133297 oooHidden = TF_OOOHidden;
133298 }else{
133299 pTab->tabFlags |= oooHidden;
133300 }
133301 }
133302 }
133303 }
133304
133305 sqlite3DbFree(db, zModuleName);
133306 return rc;
133307}
133308
133309/*
133310** This function is invoked by the parser to call the xConnect() method
133311** of the virtual table pTab. If an error occurs, an error code is returned
133312** and an error left in pParse.
133313**
133314** This call is a no-op if table pTab is not a virtual table.
133315*/
133316SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
133317 sqlite3 *db = pParse->db;
133318 const char *zMod;
133319 Module *pMod;
133320 int rc;
133321
133322 assert( pTab );
133323 if( !IsVirtual(pTab) || sqlite3GetVTable(db, pTab) ){
133324 return SQLITE_OK;
133325 }
133326
133327 /* Locate the required virtual table module */
133328 zMod = pTab->azModuleArg[0];
133329 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
133330
133331 if( !pMod ){
133332 const char *zModule = pTab->azModuleArg[0];
133333 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
133334 rc = SQLITE_ERROR;
133335 }else{
133336 char *zErr = 0;
133337 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
133338 if( rc!=SQLITE_OK ){
133339 sqlite3ErrorMsg(pParse, "%s", zErr);
133340 pParse->rc = rc;
133341 }
133342 sqlite3DbFree(db, zErr);
133343 }
133344
133345 return rc;
133346}
133347/*
133348** Grow the db->aVTrans[] array so that there is room for at least one
133349** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
133350*/
133351static int growVTrans(sqlite3 *db){
133352 const int ARRAY_INCR = 5;
133353
133354 /* Grow the sqlite3.aVTrans array if required */
133355 if( (db->nVTrans%ARRAY_INCR)==0 ){
133356 VTable **aVTrans;
133357 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
133358 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
133359 if( !aVTrans ){
133360 return SQLITE_NOMEM_BKPT;
133361 }
133362 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
133363 db->aVTrans = aVTrans;
133364 }
133365
133366 return SQLITE_OK;
133367}
133368
133369/*
133370** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
133371** have already been reserved using growVTrans().
133372*/
133373static void addToVTrans(sqlite3 *db, VTable *pVTab){
133374 /* Add pVtab to the end of sqlite3.aVTrans */
133375 db->aVTrans[db->nVTrans++] = pVTab;
133376 sqlite3VtabLock(pVTab);
133377}
133378
133379/*
133380** This function is invoked by the vdbe to call the xCreate method
133381** of the virtual table named zTab in database iDb.
133382**
133383** If an error occurs, *pzErr is set to point to an English language
133384** description of the error and an SQLITE_XXX error code is returned.
133385** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
133386*/
133387SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
133388 int rc = SQLITE_OK;
133389 Table *pTab;
133390 Module *pMod;
133391 const char *zMod;
133392
133393 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
133394 assert( pTab && IsVirtual(pTab) && !pTab->pVTable );
133395
133396 /* Locate the required virtual table module */
133397 zMod = pTab->azModuleArg[0];
133398 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
133399
133400 /* If the module has been registered and includes a Create method,
133401 ** invoke it now. If the module has not been registered, return an
133402 ** error. Otherwise, do nothing.
133403 */
133404 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
133405 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
133406 rc = SQLITE_ERROR;
133407 }else{
133408 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
133409 }
133410
133411 /* Justification of ALWAYS(): The xConstructor method is required to
133412 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
133413 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
133414 rc = growVTrans(db);
133415 if( rc==SQLITE_OK ){
133416 addToVTrans(db, sqlite3GetVTable(db, pTab));
133417 }
133418 }
133419
133420 return rc;
133421}
133422
133423/*
133424** This function is used to set the schema of a virtual table. It is only
133425** valid to call this function from within the xCreate() or xConnect() of a
133426** virtual table module.
133427*/
133428SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
133429 VtabCtx *pCtx;
133430 int rc = SQLITE_OK;
133431 Table *pTab;
133432 char *zErr = 0;
133433 Parse sParse;
133434
133435#ifdef SQLITE_ENABLE_API_ARMOR
133436 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
133437 return SQLITE_MISUSE_BKPT;
133438 }
133439#endif
133440 sqlite3_mutex_enter(db->mutex);
133441 pCtx = db->pVtabCtx;
133442 if( !pCtx || pCtx->bDeclared ){
133443 sqlite3Error(db, SQLITE_MISUSE);
133444 sqlite3_mutex_leave(db->mutex);
133445 return SQLITE_MISUSE_BKPT;
133446 }
133447 pTab = pCtx->pTab;
133448 assert( IsVirtual(pTab) );
133449
133450 memset(&sParse, 0, sizeof(sParse));
133451 sParse.eParseMode = PARSE_MODE_DECLARE_VTAB;
133452 sParse.db = db;
133453 sParse.nQueryLoop = 1;
133454 if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable, &zErr)
133455 && sParse.pNewTable
133456 && !db->mallocFailed
133457 && !sParse.pNewTable->pSelect
133458 && !IsVirtual(sParse.pNewTable)
133459 ){
133460 if( !pTab->aCol ){
133461 Table *pNew = sParse.pNewTable;
133462 Index *pIdx;
133463 pTab->aCol = pNew->aCol;
133464 pTab->nCol = pNew->nCol;
133465 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
133466 pNew->nCol = 0;
133467 pNew->aCol = 0;
133468 assert( pTab->pIndex==0 );
133469 assert( HasRowid(pNew) || sqlite3PrimaryKeyIndex(pNew)!=0 );
133470 if( !HasRowid(pNew)
133471 && pCtx->pVTable->pMod->pModule->xUpdate!=0
133472 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
133473 ){
133474 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
133475 ** or else must have a single-column PRIMARY KEY */
133476 rc = SQLITE_ERROR;
133477 }
133478 pIdx = pNew->pIndex;
133479 if( pIdx ){
133480 assert( pIdx->pNext==0 );
133481 pTab->pIndex = pIdx;
133482 pNew->pIndex = 0;
133483 pIdx->pTable = pTab;
133484 }
133485 }
133486 pCtx->bDeclared = 1;
133487 }else{
133488 sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
133489 sqlite3DbFree(db, zErr);
133490 rc = SQLITE_ERROR;
133491 }
133492 sParse.eParseMode = PARSE_MODE_NORMAL;
133493
133494 if( sParse.pVdbe ){
133495 sqlite3VdbeFinalize(sParse.pVdbe);
133496 }
133497 sqlite3DeleteTable(db, sParse.pNewTable);
133498 sqlite3ParserReset(&sParse);
133499
133500 assert( (rc&0xff)==rc );
133501 rc = sqlite3ApiExit(db, rc);
133502 sqlite3_mutex_leave(db->mutex);
133503 return rc;
133504}
133505
133506/*
133507** This function is invoked by the vdbe to call the xDestroy method
133508** of the virtual table named zTab in database iDb. This occurs
133509** when a DROP TABLE is mentioned.
133510**
133511** This call is a no-op if zTab is not a virtual table.
133512*/
133513SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
133514 int rc = SQLITE_OK;
133515 Table *pTab;
133516
133517 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
133518 if( pTab!=0 && ALWAYS(pTab->pVTable!=0) ){
133519 VTable *p;
133520 int (*xDestroy)(sqlite3_vtab *);
133521 for(p=pTab->pVTable; p; p=p->pNext){
133522 assert( p->pVtab );
133523 if( p->pVtab->nRef>0 ){
133524 return SQLITE_LOCKED;
133525 }
133526 }
133527 p = vtabDisconnectAll(db, pTab);
133528 xDestroy = p->pMod->pModule->xDestroy;
133529 assert( xDestroy!=0 ); /* Checked before the virtual table is created */
133530 rc = xDestroy(p->pVtab);
133531 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
133532 if( rc==SQLITE_OK ){
133533 assert( pTab->pVTable==p && p->pNext==0 );
133534 p->pVtab = 0;
133535 pTab->pVTable = 0;
133536 sqlite3VtabUnlock(p);
133537 }
133538 }
133539
133540 return rc;
133541}
133542
133543/*
133544** This function invokes either the xRollback or xCommit method
133545** of each of the virtual tables in the sqlite3.aVTrans array. The method
133546** called is identified by the second argument, "offset", which is
133547** the offset of the method to call in the sqlite3_module structure.
133548**
133549** The array is cleared after invoking the callbacks.
133550*/
133551static void callFinaliser(sqlite3 *db, int offset){
133552 int i;
133553 if( db->aVTrans ){
133554 VTable **aVTrans = db->aVTrans;
133555 db->aVTrans = 0;
133556 for(i=0; i<db->nVTrans; i++){
133557 VTable *pVTab = aVTrans[i];
133558 sqlite3_vtab *p = pVTab->pVtab;
133559 if( p ){
133560 int (*x)(sqlite3_vtab *);
133561 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
133562 if( x ) x(p);
133563 }
133564 pVTab->iSavepoint = 0;
133565 sqlite3VtabUnlock(pVTab);
133566 }
133567 sqlite3DbFree(db, aVTrans);
133568 db->nVTrans = 0;
133569 }
133570}
133571
133572/*
133573** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
133574** array. Return the error code for the first error that occurs, or
133575** SQLITE_OK if all xSync operations are successful.
133576**
133577** If an error message is available, leave it in p->zErrMsg.
133578*/
133579SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
133580 int i;
133581 int rc = SQLITE_OK;
133582 VTable **aVTrans = db->aVTrans;
133583
133584 db->aVTrans = 0;
133585 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
133586 int (*x)(sqlite3_vtab *);
133587 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
133588 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
133589 rc = x(pVtab);
133590 sqlite3VtabImportErrmsg(p, pVtab);
133591 }
133592 }
133593 db->aVTrans = aVTrans;
133594 return rc;
133595}
133596
133597/*
133598** Invoke the xRollback method of all virtual tables in the
133599** sqlite3.aVTrans array. Then clear the array itself.
133600*/
133601SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
133602 callFinaliser(db, offsetof(sqlite3_module,xRollback));
133603 return SQLITE_OK;
133604}
133605
133606/*
133607** Invoke the xCommit method of all virtual tables in the
133608** sqlite3.aVTrans array. Then clear the array itself.
133609*/
133610SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
133611 callFinaliser(db, offsetof(sqlite3_module,xCommit));
133612 return SQLITE_OK;
133613}
133614
133615/*
133616** If the virtual table pVtab supports the transaction interface
133617** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
133618** not currently open, invoke the xBegin method now.
133619**
133620** If the xBegin call is successful, place the sqlite3_vtab pointer
133621** in the sqlite3.aVTrans array.
133622*/
133623SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
133624 int rc = SQLITE_OK;
133625 const sqlite3_module *pModule;
133626
133627 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
133628 ** than zero, then this function is being called from within a
133629 ** virtual module xSync() callback. It is illegal to write to
133630 ** virtual module tables in this case, so return SQLITE_LOCKED.
133631 */
133632 if( sqlite3VtabInSync(db) ){
133633 return SQLITE_LOCKED;
133634 }
133635 if( !pVTab ){
133636 return SQLITE_OK;
133637 }
133638 pModule = pVTab->pVtab->pModule;
133639
133640 if( pModule->xBegin ){
133641 int i;
133642
133643 /* If pVtab is already in the aVTrans array, return early */
133644 for(i=0; i<db->nVTrans; i++){
133645 if( db->aVTrans[i]==pVTab ){
133646 return SQLITE_OK;
133647 }
133648 }
133649
133650 /* Invoke the xBegin method. If successful, add the vtab to the
133651 ** sqlite3.aVTrans[] array. */
133652 rc = growVTrans(db);
133653 if( rc==SQLITE_OK ){
133654 rc = pModule->xBegin(pVTab->pVtab);
133655 if( rc==SQLITE_OK ){
133656 int iSvpt = db->nStatement + db->nSavepoint;
133657 addToVTrans(db, pVTab);
133658 if( iSvpt && pModule->xSavepoint ){
133659 pVTab->iSavepoint = iSvpt;
133660 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
133661 }
133662 }
133663 }
133664 }
133665 return rc;
133666}
133667
133668/*
133669** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
133670** virtual tables that currently have an open transaction. Pass iSavepoint
133671** as the second argument to the virtual table method invoked.
133672**
133673** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
133674** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
133675** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
133676** an open transaction is invoked.
133677**
133678** If any virtual table method returns an error code other than SQLITE_OK,
133679** processing is abandoned and the error returned to the caller of this
133680** function immediately. If all calls to virtual table methods are successful,
133681** SQLITE_OK is returned.
133682*/
133683SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
133684 int rc = SQLITE_OK;
133685
133686 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
133687 assert( iSavepoint>=-1 );
133688 if( db->aVTrans ){
133689 int i;
133690 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
133691 VTable *pVTab = db->aVTrans[i];
133692 const sqlite3_module *pMod = pVTab->pMod->pModule;
133693 if( pVTab->pVtab && pMod->iVersion>=2 ){
133694 int (*xMethod)(sqlite3_vtab *, int);
133695 switch( op ){
133696 case SAVEPOINT_BEGIN:
133697 xMethod = pMod->xSavepoint;
133698 pVTab->iSavepoint = iSavepoint+1;
133699 break;
133700 case SAVEPOINT_ROLLBACK:
133701 xMethod = pMod->xRollbackTo;
133702 break;
133703 default:
133704 xMethod = pMod->xRelease;
133705 break;
133706 }
133707 if( xMethod && pVTab->iSavepoint>iSavepoint ){
133708 rc = xMethod(pVTab->pVtab, iSavepoint);
133709 }
133710 }
133711 }
133712 }
133713 return rc;
133714}
133715
133716/*
133717** The first parameter (pDef) is a function implementation. The
133718** second parameter (pExpr) is the first argument to this function.
133719** If pExpr is a column in a virtual table, then let the virtual
133720** table implementation have an opportunity to overload the function.
133721**
133722** This routine is used to allow virtual table implementations to
133723** overload MATCH, LIKE, GLOB, and REGEXP operators.
133724**
133725** Return either the pDef argument (indicating no change) or a
133726** new FuncDef structure that is marked as ephemeral using the
133727** SQLITE_FUNC_EPHEM flag.
133728*/
133729SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
133730 sqlite3 *db, /* Database connection for reporting malloc problems */
133731 FuncDef *pDef, /* Function to possibly overload */
133732 int nArg, /* Number of arguments to the function */
133733 Expr *pExpr /* First argument to the function */
133734){
133735 Table *pTab;
133736 sqlite3_vtab *pVtab;
133737 sqlite3_module *pMod;
133738 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
133739 void *pArg = 0;
133740 FuncDef *pNew;
133741 int rc = 0;
133742
133743 /* Check to see the left operand is a column in a virtual table */
133744 if( NEVER(pExpr==0) ) return pDef;
133745 if( pExpr->op!=TK_COLUMN ) return pDef;
133746 pTab = pExpr->pTab;
133747 if( pTab==0 ) return pDef;
133748 if( !IsVirtual(pTab) ) return pDef;
133749 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
133750 assert( pVtab!=0 );
133751 assert( pVtab->pModule!=0 );
133752 pMod = (sqlite3_module *)pVtab->pModule;
133753 if( pMod->xFindFunction==0 ) return pDef;
133754
133755 /* Call the xFindFunction method on the virtual table implementation
133756 ** to see if the implementation wants to overload this function.
133757 **
133758 ** Though undocumented, we have historically always invoked xFindFunction
133759 ** with an all lower-case function name. Continue in this tradition to
133760 ** avoid any chance of an incompatibility.
133761 */
133762#ifdef SQLITE_DEBUG
133763 {
133764 int i;
133765 for(i=0; pDef->zName[i]; i++){
133766 unsigned char x = (unsigned char)pDef->zName[i];
133767 assert( x==sqlite3UpperToLower[x] );
133768 }
133769 }
133770#endif
133771 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
133772 if( rc==0 ){
133773 return pDef;
133774 }
133775
133776 /* Create a new ephemeral function definition for the overloaded
133777 ** function */
133778 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
133779 + sqlite3Strlen30(pDef->zName) + 1);
133780 if( pNew==0 ){
133781 return pDef;
133782 }
133783 *pNew = *pDef;
133784 pNew->zName = (const char*)&pNew[1];
133785 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
133786 pNew->xSFunc = xSFunc;
133787 pNew->pUserData = pArg;
133788 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
133789 return pNew;
133790}
133791
133792/*
133793** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
133794** array so that an OP_VBegin will get generated for it. Add pTab to the
133795** array if it is missing. If pTab is already in the array, this routine
133796** is a no-op.
133797*/
133798SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
133799 Parse *pToplevel = sqlite3ParseToplevel(pParse);
133800 int i, n;
133801 Table **apVtabLock;
133802
133803 assert( IsVirtual(pTab) );
133804 for(i=0; i<pToplevel->nVtabLock; i++){
133805 if( pTab==pToplevel->apVtabLock[i] ) return;
133806 }
133807 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
133808 apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
133809 if( apVtabLock ){
133810 pToplevel->apVtabLock = apVtabLock;
133811 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
133812 }else{
133813 sqlite3OomFault(pToplevel->db);
133814 }
133815}
133816
133817/*
133818** Check to see if virtual table module pMod can be have an eponymous
133819** virtual table instance. If it can, create one if one does not already
133820** exist. Return non-zero if the eponymous virtual table instance exists
133821** when this routine returns, and return zero if it does not exist.
133822**
133823** An eponymous virtual table instance is one that is named after its
133824** module, and more importantly, does not require a CREATE VIRTUAL TABLE
133825** statement in order to come into existance. Eponymous virtual table
133826** instances always exist. They cannot be DROP-ed.
133827**
133828** Any virtual table module for which xConnect and xCreate are the same
133829** method can have an eponymous virtual table instance.
133830*/
133831SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){
133832 const sqlite3_module *pModule = pMod->pModule;
133833 Table *pTab;
133834 char *zErr = 0;
133835 int rc;
133836 sqlite3 *db = pParse->db;
133837 if( pMod->pEpoTab ) return 1;
133838 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
133839 pTab = sqlite3DbMallocZero(db, sizeof(Table));
133840 if( pTab==0 ) return 0;
133841 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
133842 if( pTab->zName==0 ){
133843 sqlite3DbFree(db, pTab);
133844 return 0;
133845 }
133846 pMod->pEpoTab = pTab;
133847 pTab->nTabRef = 1;
133848 pTab->pSchema = db->aDb[0].pSchema;
133849 assert( pTab->nModuleArg==0 );
133850 pTab->iPKey = -1;
133851 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
133852 addModuleArgument(db, pTab, 0);
133853 addModuleArgument(db, pTab, sqlite3DbStrDup(db, pTab->zName));
133854 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
133855 if( rc ){
133856 sqlite3ErrorMsg(pParse, "%s", zErr);
133857 sqlite3DbFree(db, zErr);
133858 sqlite3VtabEponymousTableClear(db, pMod);
133859 return 0;
133860 }
133861 return 1;
133862}
133863
133864/*
133865** Erase the eponymous virtual table instance associated with
133866** virtual table module pMod, if it exists.
133867*/
133868SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){
133869 Table *pTab = pMod->pEpoTab;
133870 if( pTab!=0 ){
133871 /* Mark the table as Ephemeral prior to deleting it, so that the
133872 ** sqlite3DeleteTable() routine will know that it is not stored in
133873 ** the schema. */
133874 pTab->tabFlags |= TF_Ephemeral;
133875 sqlite3DeleteTable(db, pTab);
133876 pMod->pEpoTab = 0;
133877 }
133878}
133879
133880/*
133881** Return the ON CONFLICT resolution mode in effect for the virtual
133882** table update operation currently in progress.
133883**
133884** The results of this routine are undefined unless it is called from
133885** within an xUpdate method.
133886*/
133887SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
133888 static const unsigned char aMap[] = {
133889 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
133890 };
133891#ifdef SQLITE_ENABLE_API_ARMOR
133892 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
133893#endif
133894 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
133895 assert( OE_Ignore==4 && OE_Replace==5 );
133896 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
133897 return (int)aMap[db->vtabOnConflict-1];
133898}
133899
133900/*
133901** Call from within the xCreate() or xConnect() methods to provide
133902** the SQLite core with additional information about the behavior
133903** of the virtual table being implemented.
133904*/
133905SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
133906 va_list ap;
133907 int rc = SQLITE_OK;
133908
133909#ifdef SQLITE_ENABLE_API_ARMOR
133910 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
133911#endif
133912 sqlite3_mutex_enter(db->mutex);
133913 va_start(ap, op);
133914 switch( op ){
133915 case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
133916 VtabCtx *p = db->pVtabCtx;
133917 if( !p ){
133918 rc = SQLITE_MISUSE_BKPT;
133919 }else{
133920 assert( p->pTab==0 || IsVirtual(p->pTab) );
133921 p->pVTable->bConstraint = (u8)va_arg(ap, int);
133922 }
133923 break;
133924 }
133925 default:
133926 rc = SQLITE_MISUSE_BKPT;
133927 break;
133928 }
133929 va_end(ap);
133930
133931 if( rc!=SQLITE_OK ) sqlite3Error(db, rc);
133932 sqlite3_mutex_leave(db->mutex);
133933 return rc;
133934}
133935
133936#endif /* SQLITE_OMIT_VIRTUALTABLE */
133937
133938/************** End of vtab.c ************************************************/
133939/************** Begin file wherecode.c ***************************************/
133940/*
133941** 2015-06-06
133942**
133943** The author disclaims copyright to this source code. In place of
133944** a legal notice, here is a blessing:
133945**
133946** May you do good and not evil.
133947** May you find forgiveness for yourself and forgive others.
133948** May you share freely, never taking more than you give.
133949**
133950*************************************************************************
133951** This module contains C code that generates VDBE code used to process
133952** the WHERE clause of SQL statements.
133953**
133954** This file was split off from where.c on 2015-06-06 in order to reduce the
133955** size of where.c and make it easier to edit. This file contains the routines
133956** that actually generate the bulk of the WHERE loop code. The original where.c
133957** file retains the code that does query planning and analysis.
133958*/
133959/* #include "sqliteInt.h" */
133960/************** Include whereInt.h in the middle of wherecode.c **************/
133961/************** Begin file whereInt.h ****************************************/
133962/*
133963** 2013-11-12
133964**
133965** The author disclaims copyright to this source code. In place of
133966** a legal notice, here is a blessing:
133967**
133968** May you do good and not evil.
133969** May you find forgiveness for yourself and forgive others.
133970** May you share freely, never taking more than you give.
133971**
133972*************************************************************************
133973**
133974** This file contains structure and macro definitions for the query
133975** planner logic in "where.c". These definitions are broken out into
133976** a separate source file for easier editing.
133977*/
133978
133979/*
133980** Trace output macros
133981*/
133982#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
133983/***/ extern int sqlite3WhereTrace;
133984#endif
133985#if defined(SQLITE_DEBUG) \
133986 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
133987# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
133988# define WHERETRACE_ENABLED 1
133989#else
133990# define WHERETRACE(K,X)
133991#endif
133992
133993/* Forward references
133994*/
133995typedef struct WhereClause WhereClause;
133996typedef struct WhereMaskSet WhereMaskSet;
133997typedef struct WhereOrInfo WhereOrInfo;
133998typedef struct WhereAndInfo WhereAndInfo;
133999typedef struct WhereLevel WhereLevel;
134000typedef struct WhereLoop WhereLoop;
134001typedef struct WherePath WherePath;
134002typedef struct WhereTerm WhereTerm;
134003typedef struct WhereLoopBuilder WhereLoopBuilder;
134004typedef struct WhereScan WhereScan;
134005typedef struct WhereOrCost WhereOrCost;
134006typedef struct WhereOrSet WhereOrSet;
134007
134008/*
134009** This object contains information needed to implement a single nested
134010** loop in WHERE clause.
134011**
134012** Contrast this object with WhereLoop. This object describes the
134013** implementation of the loop. WhereLoop describes the algorithm.
134014** This object contains a pointer to the WhereLoop algorithm as one of
134015** its elements.
134016**
134017** The WhereInfo object contains a single instance of this object for
134018** each term in the FROM clause (which is to say, for each of the
134019** nested loops as implemented). The order of WhereLevel objects determines
134020** the loop nested order, with WhereInfo.a[0] being the outer loop and
134021** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
134022*/
134023struct WhereLevel {
134024 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
134025 int iTabCur; /* The VDBE cursor used to access the table */
134026 int iIdxCur; /* The VDBE cursor used to access pIdx */
134027 int addrBrk; /* Jump here to break out of the loop */
134028 int addrNxt; /* Jump here to start the next IN combination */
134029 int addrSkip; /* Jump here for next iteration of skip-scan */
134030 int addrCont; /* Jump here to continue with the next loop cycle */
134031 int addrFirst; /* First instruction of interior of the loop */
134032 int addrBody; /* Beginning of the body of this loop */
134033#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
134034 u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */
134035 int addrLikeRep; /* LIKE range processing address */
134036#endif
134037 u8 iFrom; /* Which entry in the FROM clause */
134038 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */
134039 int p1, p2; /* Operands of the opcode used to ends the loop */
134040 union { /* Information that depends on pWLoop->wsFlags */
134041 struct {
134042 int nIn; /* Number of entries in aInLoop[] */
134043 struct InLoop {
134044 int iCur; /* The VDBE cursor used by this IN operator */
134045 int addrInTop; /* Top of the IN loop */
134046 int iBase; /* Base register of multi-key index record */
134047 int nPrefix; /* Number of prior entires in the key */
134048 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
134049 } *aInLoop; /* Information about each nested IN operator */
134050 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
134051 Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */
134052 } u;
134053 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
134054 Bitmask notReady; /* FROM entries not usable at this level */
134055#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
134056 int addrVisit; /* Address at which row is visited */
134057#endif
134058};
134059
134060/*
134061** Each instance of this object represents an algorithm for evaluating one
134062** term of a join. Every term of the FROM clause will have at least
134063** one corresponding WhereLoop object (unless INDEXED BY constraints
134064** prevent a query solution - which is an error) and many terms of the
134065** FROM clause will have multiple WhereLoop objects, each describing a
134066** potential way of implementing that FROM-clause term, together with
134067** dependencies and cost estimates for using the chosen algorithm.
134068**
134069** Query planning consists of building up a collection of these WhereLoop
134070** objects, then computing a particular sequence of WhereLoop objects, with
134071** one WhereLoop object per FROM clause term, that satisfy all dependencies
134072** and that minimize the overall cost.
134073*/
134074struct WhereLoop {
134075 Bitmask prereq; /* Bitmask of other loops that must run first */
134076 Bitmask maskSelf; /* Bitmask identifying table iTab */
134077#ifdef SQLITE_DEBUG
134078 char cId; /* Symbolic ID of this loop for debugging use */
134079#endif
134080 u8 iTab; /* Position in FROM clause of table for this loop */
134081 u8 iSortIdx; /* Sorting index number. 0==None */
134082 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
134083 LogEst rRun; /* Cost of running each loop */
134084 LogEst nOut; /* Estimated number of output rows */
134085 union {
134086 struct { /* Information for internal btree tables */
134087 u16 nEq; /* Number of equality constraints */
134088 u16 nBtm; /* Size of BTM vector */
134089 u16 nTop; /* Size of TOP vector */
134090 u16 nIdxCol; /* Index column used for ORDER BY */
134091 Index *pIndex; /* Index used, or NULL */
134092 } btree;
134093 struct { /* Information for virtual tables */
134094 int idxNum; /* Index number */
134095 u8 needFree; /* True if sqlite3_free(idxStr) is needed */
134096 i8 isOrdered; /* True if satisfies ORDER BY */
134097 u16 omitMask; /* Terms that may be omitted */
134098 char *idxStr; /* Index identifier string */
134099 } vtab;
134100 } u;
134101 u32 wsFlags; /* WHERE_* flags describing the plan */
134102 u16 nLTerm; /* Number of entries in aLTerm[] */
134103 u16 nSkip; /* Number of NULL aLTerm[] entries */
134104 /**** whereLoopXfer() copies fields above ***********************/
134105# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
134106 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
134107 WhereTerm **aLTerm; /* WhereTerms used */
134108 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
134109 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
134110};
134111
134112/* This object holds the prerequisites and the cost of running a
134113** subquery on one operand of an OR operator in the WHERE clause.
134114** See WhereOrSet for additional information
134115*/
134116struct WhereOrCost {
134117 Bitmask prereq; /* Prerequisites */
134118 LogEst rRun; /* Cost of running this subquery */
134119 LogEst nOut; /* Number of outputs for this subquery */
134120};
134121
134122/* The WhereOrSet object holds a set of possible WhereOrCosts that
134123** correspond to the subquery(s) of OR-clause processing. Only the
134124** best N_OR_COST elements are retained.
134125*/
134126#define N_OR_COST 3
134127struct WhereOrSet {
134128 u16 n; /* Number of valid a[] entries */
134129 WhereOrCost a[N_OR_COST]; /* Set of best costs */
134130};
134131
134132/*
134133** Each instance of this object holds a sequence of WhereLoop objects
134134** that implement some or all of a query plan.
134135**
134136** Think of each WhereLoop object as a node in a graph with arcs
134137** showing dependencies and costs for travelling between nodes. (That is
134138** not a completely accurate description because WhereLoop costs are a
134139** vector, not a scalar, and because dependencies are many-to-one, not
134140** one-to-one as are graph nodes. But it is a useful visualization aid.)
134141** Then a WherePath object is a path through the graph that visits some
134142** or all of the WhereLoop objects once.
134143**
134144** The "solver" works by creating the N best WherePath objects of length
134145** 1. Then using those as a basis to compute the N best WherePath objects
134146** of length 2. And so forth until the length of WherePaths equals the
134147** number of nodes in the FROM clause. The best (lowest cost) WherePath
134148** at the end is the chosen query plan.
134149*/
134150struct WherePath {
134151 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
134152 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
134153 LogEst nRow; /* Estimated number of rows generated by this path */
134154 LogEst rCost; /* Total cost of this path */
134155 LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */
134156 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
134157 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
134158};
134159
134160/*
134161** The query generator uses an array of instances of this structure to
134162** help it analyze the subexpressions of the WHERE clause. Each WHERE
134163** clause subexpression is separated from the others by AND operators,
134164** usually, or sometimes subexpressions separated by OR.
134165**
134166** All WhereTerms are collected into a single WhereClause structure.
134167** The following identity holds:
134168**
134169** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
134170**
134171** When a term is of the form:
134172**
134173** X <op> <expr>
134174**
134175** where X is a column name and <op> is one of certain operators,
134176** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
134177** cursor number and column number for X. WhereTerm.eOperator records
134178** the <op> using a bitmask encoding defined by WO_xxx below. The
134179** use of a bitmask encoding for the operator allows us to search
134180** quickly for terms that match any of several different operators.
134181**
134182** A WhereTerm might also be two or more subterms connected by OR:
134183**
134184** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
134185**
134186** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
134187** and the WhereTerm.u.pOrInfo field points to auxiliary information that
134188** is collected about the OR clause.
134189**
134190** If a term in the WHERE clause does not match either of the two previous
134191** categories, then eOperator==0. The WhereTerm.pExpr field is still set
134192** to the original subexpression content and wtFlags is set up appropriately
134193** but no other fields in the WhereTerm object are meaningful.
134194**
134195** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
134196** but they do so indirectly. A single WhereMaskSet structure translates
134197** cursor number into bits and the translated bit is stored in the prereq
134198** fields. The translation is used in order to maximize the number of
134199** bits that will fit in a Bitmask. The VDBE cursor numbers might be
134200** spread out over the non-negative integers. For example, the cursor
134201** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
134202** translates these sparse cursor numbers into consecutive integers
134203** beginning with 0 in order to make the best possible use of the available
134204** bits in the Bitmask. So, in the example above, the cursor numbers
134205** would be mapped into integers 0 through 7.
134206**
134207** The number of terms in a join is limited by the number of bits
134208** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
134209** is only able to process joins with 64 or fewer tables.
134210*/
134211struct WhereTerm {
134212 Expr *pExpr; /* Pointer to the subexpression that is this term */
134213 WhereClause *pWC; /* The clause this term is part of */
134214 LogEst truthProb; /* Probability of truth for this expression */
134215 u16 wtFlags; /* TERM_xxx bit flags. See below */
134216 u16 eOperator; /* A WO_xx value describing <op> */
134217 u8 nChild; /* Number of children that must disable us */
134218 u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
134219 int iParent; /* Disable pWC->a[iParent] when this term disabled */
134220 int leftCursor; /* Cursor number of X in "X <op> <expr>" */
134221 int iField; /* Field in (?,?,?) IN (SELECT...) vector */
134222 union {
134223 int leftColumn; /* Column number of X in "X <op> <expr>" */
134224 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
134225 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
134226 } u;
134227 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
134228 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
134229};
134230
134231/*
134232** Allowed values of WhereTerm.wtFlags
134233*/
134234#define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */
134235#define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */
134236#define TERM_CODED 0x04 /* This term is already coded */
134237#define TERM_COPIED 0x08 /* Has a child */
134238#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
134239#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
134240#define TERM_OR_OK 0x40 /* Used during OR-clause processing */
134241#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
134242# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
134243#else
134244# define TERM_VNULL 0x00 /* Disabled if not using stat3 */
134245#endif
134246#define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */
134247#define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */
134248#define TERM_LIKE 0x400 /* The original LIKE operator */
134249#define TERM_IS 0x800 /* Term.pExpr is an IS operator */
134250#define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
134251
134252/*
134253** An instance of the WhereScan object is used as an iterator for locating
134254** terms in the WHERE clause that are useful to the query planner.
134255*/
134256struct WhereScan {
134257 WhereClause *pOrigWC; /* Original, innermost WhereClause */
134258 WhereClause *pWC; /* WhereClause currently being scanned */
134259 const char *zCollName; /* Required collating sequence, if not NULL */
134260 Expr *pIdxExpr; /* Search for this index expression */
134261 char idxaff; /* Must match this affinity, if zCollName!=NULL */
134262 unsigned char nEquiv; /* Number of entries in aEquiv[] */
134263 unsigned char iEquiv; /* Next unused slot in aEquiv[] */
134264 u32 opMask; /* Acceptable operators */
134265 int k; /* Resume scanning at this->pWC->a[this->k] */
134266 int aiCur[11]; /* Cursors in the equivalence class */
134267 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
134268};
134269
134270/*
134271** An instance of the following structure holds all information about a
134272** WHERE clause. Mostly this is a container for one or more WhereTerms.
134273**
134274** Explanation of pOuter: For a WHERE clause of the form
134275**
134276** a AND ((b AND c) OR (d AND e)) AND f
134277**
134278** There are separate WhereClause objects for the whole clause and for
134279** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
134280** subclauses points to the WhereClause object for the whole clause.
134281*/
134282struct WhereClause {
134283 WhereInfo *pWInfo; /* WHERE clause processing context */
134284 WhereClause *pOuter; /* Outer conjunction */
134285 u8 op; /* Split operator. TK_AND or TK_OR */
134286 u8 hasOr; /* True if any a[].eOperator is WO_OR */
134287 int nTerm; /* Number of terms */
134288 int nSlot; /* Number of entries in a[] */
134289 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
134290#if defined(SQLITE_SMALL_STACK)
134291 WhereTerm aStatic[1]; /* Initial static space for a[] */
134292#else
134293 WhereTerm aStatic[8]; /* Initial static space for a[] */
134294#endif
134295};
134296
134297/*
134298** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
134299** a dynamically allocated instance of the following structure.
134300*/
134301struct WhereOrInfo {
134302 WhereClause wc; /* Decomposition into subterms */
134303 Bitmask indexable; /* Bitmask of all indexable tables in the clause */
134304};
134305
134306/*
134307** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
134308** a dynamically allocated instance of the following structure.
134309*/
134310struct WhereAndInfo {
134311 WhereClause wc; /* The subexpression broken out */
134312};
134313
134314/*
134315** An instance of the following structure keeps track of a mapping
134316** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
134317**
134318** The VDBE cursor numbers are small integers contained in
134319** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
134320** clause, the cursor numbers might not begin with 0 and they might
134321** contain gaps in the numbering sequence. But we want to make maximum
134322** use of the bits in our bitmasks. This structure provides a mapping
134323** from the sparse cursor numbers into consecutive integers beginning
134324** with 0.
134325**
134326** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
134327** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
134328**
134329** For example, if the WHERE clause expression used these VDBE
134330** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
134331** would map those cursor numbers into bits 0 through 5.
134332**
134333** Note that the mapping is not necessarily ordered. In the example
134334** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
134335** 57->5, 73->4. Or one of 719 other combinations might be used. It
134336** does not really matter. What is important is that sparse cursor
134337** numbers all get mapped into bit numbers that begin with 0 and contain
134338** no gaps.
134339*/
134340struct WhereMaskSet {
134341 int bVarSelect; /* Used by sqlite3WhereExprUsage() */
134342 int n; /* Number of assigned cursor values */
134343 int ix[BMS]; /* Cursor assigned to each bit */
134344};
134345
134346/*
134347** Initialize a WhereMaskSet object
134348*/
134349#define initMaskSet(P) (P)->n=0
134350
134351/*
134352** This object is a convenience wrapper holding all information needed
134353** to construct WhereLoop objects for a particular query.
134354*/
134355struct WhereLoopBuilder {
134356 WhereInfo *pWInfo; /* Information about this WHERE */
134357 WhereClause *pWC; /* WHERE clause terms */
134358 ExprList *pOrderBy; /* ORDER BY clause */
134359 WhereLoop *pNew; /* Template WhereLoop */
134360 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
134361#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
134362 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
134363 int nRecValid; /* Number of valid fields currently in pRec */
134364#endif
134365 unsigned int bldFlags; /* SQLITE_BLDF_* flags */
134366};
134367
134368/* Allowed values for WhereLoopBuider.bldFlags */
134369#define SQLITE_BLDF_INDEXED 0x0001 /* An index is used */
134370#define SQLITE_BLDF_UNIQUE 0x0002 /* All keys of a UNIQUE index used */
134371
134372/*
134373** The WHERE clause processing routine has two halves. The
134374** first part does the start of the WHERE loop and the second
134375** half does the tail of the WHERE loop. An instance of
134376** this structure is returned by the first half and passed
134377** into the second half to give some continuity.
134378**
134379** An instance of this object holds the complete state of the query
134380** planner.
134381*/
134382struct WhereInfo {
134383 Parse *pParse; /* Parsing and code generating context */
134384 SrcList *pTabList; /* List of tables in the join */
134385 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
134386 ExprList *pResultSet; /* Result set of the query */
134387 Expr *pWhere; /* The complete WHERE clause */
134388 LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
134389 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
134390 int iContinue; /* Jump here to continue with next record */
134391 int iBreak; /* Jump here to break out of the loop */
134392 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
134393 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
134394 u8 nLevel; /* Number of nested loop */
134395 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
134396 u8 sorted; /* True if really sorted (not just grouped) */
134397 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */
134398 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
134399 u8 eDistinct; /* One of the WHERE_DISTINCT_* values */
134400 u8 bOrderedInnerLoop; /* True if only the inner-most loop is ordered */
134401 int iTop; /* The very beginning of the WHERE loop */
134402 WhereLoop *pLoops; /* List of all WhereLoop objects */
134403 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
134404 LogEst nRowOut; /* Estimated number of output rows */
134405 WhereClause sWC; /* Decomposition of the WHERE clause */
134406 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
134407 WhereLevel a[1]; /* Information about each nest loop in WHERE */
134408};
134409
134410/*
134411** Private interfaces - callable only by other where.c routines.
134412**
134413** where.c:
134414*/
134415SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
134416#ifdef WHERETRACE_ENABLED
134417SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC);
134418#endif
134419SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
134420 WhereClause *pWC, /* The WHERE clause to be searched */
134421 int iCur, /* Cursor number of LHS */
134422 int iColumn, /* Column number of LHS */
134423 Bitmask notReady, /* RHS must not overlap with this mask */
134424 u32 op, /* Mask of WO_xx values describing operator */
134425 Index *pIdx /* Must be compatible with this index, if not NULL */
134426);
134427
134428/* wherecode.c: */
134429#ifndef SQLITE_OMIT_EXPLAIN
134430SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
134431 Parse *pParse, /* Parse context */
134432 SrcList *pTabList, /* Table list this loop refers to */
134433 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
134434 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
134435);
134436#else
134437# define sqlite3WhereExplainOneScan(u,v,w,x) 0
134438#endif /* SQLITE_OMIT_EXPLAIN */
134439#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
134440SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
134441 Vdbe *v, /* Vdbe to add scanstatus entry to */
134442 SrcList *pSrclist, /* FROM clause pLvl reads data from */
134443 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
134444 int addrExplain /* Address of OP_Explain (or 0) */
134445);
134446#else
134447# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
134448#endif
134449SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
134450 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
134451 int iLevel, /* Which level of pWInfo->a[] should be coded */
134452 Bitmask notReady /* Which tables are currently available */
134453);
134454
134455/* whereexpr.c: */
134456SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
134457SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
134458SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
134459SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
134460SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
134461SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
134462SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
134463SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*);
134464
134465
134466
134467
134468
134469/*
134470** Bitmasks for the operators on WhereTerm objects. These are all
134471** operators that are of interest to the query planner. An
134472** OR-ed combination of these values can be used when searching for
134473** particular WhereTerms within a WhereClause.
134474**
134475** Value constraints:
134476** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ
134477** WO_LT == SQLITE_INDEX_CONSTRAINT_LT
134478** WO_LE == SQLITE_INDEX_CONSTRAINT_LE
134479** WO_GT == SQLITE_INDEX_CONSTRAINT_GT
134480** WO_GE == SQLITE_INDEX_CONSTRAINT_GE
134481*/
134482#define WO_IN 0x0001
134483#define WO_EQ 0x0002
134484#define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
134485#define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
134486#define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
134487#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
134488#define WO_AUX 0x0040 /* Op useful to virtual tables only */
134489#define WO_IS 0x0080
134490#define WO_ISNULL 0x0100
134491#define WO_OR 0x0200 /* Two or more OR-connected terms */
134492#define WO_AND 0x0400 /* Two or more AND-connected terms */
134493#define WO_EQUIV 0x0800 /* Of the form A==B, both columns */
134494#define WO_NOOP 0x1000 /* This term does not restrict search space */
134495
134496#define WO_ALL 0x1fff /* Mask of all possible WO_* values */
134497#define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
134498
134499/*
134500** These are definitions of bits in the WhereLoop.wsFlags field.
134501** The particular combination of bits in each WhereLoop help to
134502** determine the algorithm that WhereLoop represents.
134503*/
134504#define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */
134505#define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */
134506#define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */
134507#define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */
134508#define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */
134509#define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */
134510#define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */
134511#define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */
134512#define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
134513#define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */
134514#define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */
134515#define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */
134516#define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */
134517#define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
134518#define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
134519#define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
134520#define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
134521#define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
134522#define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */
134523#define WHERE_IN_EARLYOUT 0x00040000 /* Perhaps quit IN loops early */
134524
134525/************** End of whereInt.h ********************************************/
134526/************** Continuing where we left off in wherecode.c ******************/
134527
134528#ifndef SQLITE_OMIT_EXPLAIN
134529
134530/*
134531** Return the name of the i-th column of the pIdx index.
134532*/
134533static const char *explainIndexColumnName(Index *pIdx, int i){
134534 i = pIdx->aiColumn[i];
134535 if( i==XN_EXPR ) return "<expr>";
134536 if( i==XN_ROWID ) return "rowid";
134537 return pIdx->pTable->aCol[i].zName;
134538}
134539
134540/*
134541** This routine is a helper for explainIndexRange() below
134542**
134543** pStr holds the text of an expression that we are building up one term
134544** at a time. This routine adds a new term to the end of the expression.
134545** Terms are separated by AND so add the "AND" text for second and subsequent
134546** terms only.
134547*/
134548static void explainAppendTerm(
134549 StrAccum *pStr, /* The text expression being built */
134550 Index *pIdx, /* Index to read column names from */
134551 int nTerm, /* Number of terms */
134552 int iTerm, /* Zero-based index of first term. */
134553 int bAnd, /* Non-zero to append " AND " */
134554 const char *zOp /* Name of the operator */
134555){
134556 int i;
134557
134558 assert( nTerm>=1 );
134559 if( bAnd ) sqlite3_str_append(pStr, " AND ", 5);
134560
134561 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
134562 for(i=0; i<nTerm; i++){
134563 if( i ) sqlite3_str_append(pStr, ",", 1);
134564 sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i));
134565 }
134566 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
134567
134568 sqlite3_str_append(pStr, zOp, 1);
134569
134570 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
134571 for(i=0; i<nTerm; i++){
134572 if( i ) sqlite3_str_append(pStr, ",", 1);
134573 sqlite3_str_append(pStr, "?", 1);
134574 }
134575 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
134576}
134577
134578/*
134579** Argument pLevel describes a strategy for scanning table pTab. This
134580** function appends text to pStr that describes the subset of table
134581** rows scanned by the strategy in the form of an SQL expression.
134582**
134583** For example, if the query:
134584**
134585** SELECT * FROM t1 WHERE a=1 AND b>2;
134586**
134587** is run and there is an index on (a, b), then this function returns a
134588** string similar to:
134589**
134590** "a=? AND b>?"
134591*/
134592static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
134593 Index *pIndex = pLoop->u.btree.pIndex;
134594 u16 nEq = pLoop->u.btree.nEq;
134595 u16 nSkip = pLoop->nSkip;
134596 int i, j;
134597
134598 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
134599 sqlite3_str_append(pStr, " (", 2);
134600 for(i=0; i<nEq; i++){
134601 const char *z = explainIndexColumnName(pIndex, i);
134602 if( i ) sqlite3_str_append(pStr, " AND ", 5);
134603 sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
134604 }
134605
134606 j = i;
134607 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
134608 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
134609 i = 1;
134610 }
134611 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
134612 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
134613 }
134614 sqlite3_str_append(pStr, ")", 1);
134615}
134616
134617/*
134618** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
134619** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
134620** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
134621** is added to the output to describe the table scan strategy in pLevel.
134622**
134623** If an OP_Explain opcode is added to the VM, its address is returned.
134624** Otherwise, if no OP_Explain is coded, zero is returned.
134625*/
134626SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
134627 Parse *pParse, /* Parse context */
134628 SrcList *pTabList, /* Table list this loop refers to */
134629 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
134630 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
134631){
134632 int ret = 0;
134633#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
134634 if( sqlite3ParseToplevel(pParse)->explain==2 )
134635#endif
134636 {
134637 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
134638 Vdbe *v = pParse->pVdbe; /* VM being constructed */
134639 sqlite3 *db = pParse->db; /* Database handle */
134640 int isSearch; /* True for a SEARCH. False for SCAN. */
134641 WhereLoop *pLoop; /* The controlling WhereLoop object */
134642 u32 flags; /* Flags that describe this loop */
134643 char *zMsg; /* Text to add to EQP output */
134644 StrAccum str; /* EQP output string */
134645 char zBuf[100]; /* Initial space for EQP output string */
134646
134647 pLoop = pLevel->pWLoop;
134648 flags = pLoop->wsFlags;
134649 if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_OR_SUBCLAUSE) ) return 0;
134650
134651 isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
134652 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
134653 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
134654
134655 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
134656 sqlite3_str_appendall(&str, isSearch ? "SEARCH" : "SCAN");
134657 if( pItem->pSelect ){
134658 sqlite3_str_appendf(&str, " SUBQUERY %u", pItem->pSelect->selId);
134659 }else{
134660 sqlite3_str_appendf(&str, " TABLE %s", pItem->zName);
134661 }
134662
134663 if( pItem->zAlias ){
134664 sqlite3_str_appendf(&str, " AS %s", pItem->zAlias);
134665 }
134666 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
134667 const char *zFmt = 0;
134668 Index *pIdx;
134669
134670 assert( pLoop->u.btree.pIndex!=0 );
134671 pIdx = pLoop->u.btree.pIndex;
134672 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
134673 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
134674 if( isSearch ){
134675 zFmt = "PRIMARY KEY";
134676 }
134677 }else if( flags & WHERE_PARTIALIDX ){
134678 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
134679 }else if( flags & WHERE_AUTO_INDEX ){
134680 zFmt = "AUTOMATIC COVERING INDEX";
134681 }else if( flags & WHERE_IDX_ONLY ){
134682 zFmt = "COVERING INDEX %s";
134683 }else{
134684 zFmt = "INDEX %s";
134685 }
134686 if( zFmt ){
134687 sqlite3_str_append(&str, " USING ", 7);
134688 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
134689 explainIndexRange(&str, pLoop);
134690 }
134691 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
134692 const char *zRangeOp;
134693 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
134694 zRangeOp = "=";
134695 }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
134696 zRangeOp = ">? AND rowid<";
134697 }else if( flags&WHERE_BTM_LIMIT ){
134698 zRangeOp = ">";
134699 }else{
134700 assert( flags&WHERE_TOP_LIMIT);
134701 zRangeOp = "<";
134702 }
134703 sqlite3_str_appendf(&str,
134704 " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
134705 }
134706#ifndef SQLITE_OMIT_VIRTUALTABLE
134707 else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
134708 sqlite3_str_appendf(&str, " VIRTUAL TABLE INDEX %d:%s",
134709 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
134710 }
134711#endif
134712#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
134713 if( pLoop->nOut>=10 ){
134714 sqlite3_str_appendf(&str, " (~%llu rows)",
134715 sqlite3LogEstToInt(pLoop->nOut));
134716 }else{
134717 sqlite3_str_append(&str, " (~1 row)", 9);
134718 }
134719#endif
134720 zMsg = sqlite3StrAccumFinish(&str);
134721 ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v),
134722 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
134723 }
134724 return ret;
134725}
134726#endif /* SQLITE_OMIT_EXPLAIN */
134727
134728#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
134729/*
134730** Configure the VM passed as the first argument with an
134731** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
134732** implement level pLvl. Argument pSrclist is a pointer to the FROM
134733** clause that the scan reads data from.
134734**
134735** If argument addrExplain is not 0, it must be the address of an
134736** OP_Explain instruction that describes the same loop.
134737*/
134738SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
134739 Vdbe *v, /* Vdbe to add scanstatus entry to */
134740 SrcList *pSrclist, /* FROM clause pLvl reads data from */
134741 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
134742 int addrExplain /* Address of OP_Explain (or 0) */
134743){
134744 const char *zObj = 0;
134745 WhereLoop *pLoop = pLvl->pWLoop;
134746 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
134747 zObj = pLoop->u.btree.pIndex->zName;
134748 }else{
134749 zObj = pSrclist->a[pLvl->iFrom].zName;
134750 }
134751 sqlite3VdbeScanStatus(
134752 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
134753 );
134754}
134755#endif
134756
134757
134758/*
134759** Disable a term in the WHERE clause. Except, do not disable the term
134760** if it controls a LEFT OUTER JOIN and it did not originate in the ON
134761** or USING clause of that join.
134762**
134763** Consider the term t2.z='ok' in the following queries:
134764**
134765** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
134766** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
134767** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
134768**
134769** The t2.z='ok' is disabled in the in (2) because it originates
134770** in the ON clause. The term is disabled in (3) because it is not part
134771** of a LEFT OUTER JOIN. In (1), the term is not disabled.
134772**
134773** Disabling a term causes that term to not be tested in the inner loop
134774** of the join. Disabling is an optimization. When terms are satisfied
134775** by indices, we disable them to prevent redundant tests in the inner
134776** loop. We would get the correct results if nothing were ever disabled,
134777** but joins might run a little slower. The trick is to disable as much
134778** as we can without disabling too much. If we disabled in (1), we'd get
134779** the wrong answer. See ticket #813.
134780**
134781** If all the children of a term are disabled, then that term is also
134782** automatically disabled. In this way, terms get disabled if derived
134783** virtual terms are tested first. For example:
134784**
134785** x GLOB 'abc*' AND x>='abc' AND x<'acd'
134786** \___________/ \______/ \_____/
134787** parent child1 child2
134788**
134789** Only the parent term was in the original WHERE clause. The child1
134790** and child2 terms were added by the LIKE optimization. If both of
134791** the virtual child terms are valid, then testing of the parent can be
134792** skipped.
134793**
134794** Usually the parent term is marked as TERM_CODED. But if the parent
134795** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
134796** The TERM_LIKECOND marking indicates that the term should be coded inside
134797** a conditional such that is only evaluated on the second pass of a
134798** LIKE-optimization loop, when scanning BLOBs instead of strings.
134799*/
134800static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
134801 int nLoop = 0;
134802 assert( pTerm!=0 );
134803 while( (pTerm->wtFlags & TERM_CODED)==0
134804 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
134805 && (pLevel->notReady & pTerm->prereqAll)==0
134806 ){
134807 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
134808 pTerm->wtFlags |= TERM_LIKECOND;
134809 }else{
134810 pTerm->wtFlags |= TERM_CODED;
134811 }
134812 if( pTerm->iParent<0 ) break;
134813 pTerm = &pTerm->pWC->a[pTerm->iParent];
134814 assert( pTerm!=0 );
134815 pTerm->nChild--;
134816 if( pTerm->nChild!=0 ) break;
134817 nLoop++;
134818 }
134819}
134820
134821/*
134822** Code an OP_Affinity opcode to apply the column affinity string zAff
134823** to the n registers starting at base.
134824**
134825** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
134826** beginning and end of zAff are ignored. If all entries in zAff are
134827** SQLITE_AFF_BLOB, then no code gets generated.
134828**
134829** This routine makes its own copy of zAff so that the caller is free
134830** to modify zAff after this routine returns.
134831*/
134832static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
134833 Vdbe *v = pParse->pVdbe;
134834 if( zAff==0 ){
134835 assert( pParse->db->mallocFailed );
134836 return;
134837 }
134838 assert( v!=0 );
134839
134840 /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning
134841 ** and end of the affinity string.
134842 */
134843 while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){
134844 n--;
134845 base++;
134846 zAff++;
134847 }
134848 while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
134849 n--;
134850 }
134851
134852 /* Code the OP_Affinity opcode if there is anything left to do. */
134853 if( n>0 ){
134854 sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n);
134855 }
134856}
134857
134858/*
134859** Expression pRight, which is the RHS of a comparison operation, is
134860** either a vector of n elements or, if n==1, a scalar expression.
134861** Before the comparison operation, affinity zAff is to be applied
134862** to the pRight values. This function modifies characters within the
134863** affinity string to SQLITE_AFF_BLOB if either:
134864**
134865** * the comparison will be performed with no affinity, or
134866** * the affinity change in zAff is guaranteed not to change the value.
134867*/
134868static void updateRangeAffinityStr(
134869 Expr *pRight, /* RHS of comparison */
134870 int n, /* Number of vector elements in comparison */
134871 char *zAff /* Affinity string to modify */
134872){
134873 int i;
134874 for(i=0; i<n; i++){
134875 Expr *p = sqlite3VectorFieldSubexpr(pRight, i);
134876 if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB
134877 || sqlite3ExprNeedsNoAffinityChange(p, zAff[i])
134878 ){
134879 zAff[i] = SQLITE_AFF_BLOB;
134880 }
134881 }
134882}
134883
134884
134885/*
134886** pX is an expression of the form: (vector) IN (SELECT ...)
134887** In other words, it is a vector IN operator with a SELECT clause on the
134888** LHS. But not all terms in the vector are indexable and the terms might
134889** not be in the correct order for indexing.
134890**
134891** This routine makes a copy of the input pX expression and then adjusts
134892** the vector on the LHS with corresponding changes to the SELECT so that
134893** the vector contains only index terms and those terms are in the correct
134894** order. The modified IN expression is returned. The caller is responsible
134895** for deleting the returned expression.
134896**
134897** Example:
134898**
134899** CREATE TABLE t1(a,b,c,d,e,f);
134900** CREATE INDEX t1x1 ON t1(e,c);
134901** SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
134902** \_______________________________________/
134903** The pX expression
134904**
134905** Since only columns e and c can be used with the index, in that order,
134906** the modified IN expression that is returned will be:
134907**
134908** (e,c) IN (SELECT z,x FROM t2)
134909**
134910** The reduced pX is different from the original (obviously) and thus is
134911** only used for indexing, to improve performance. The original unaltered
134912** IN expression must also be run on each output row for correctness.
134913*/
134914static Expr *removeUnindexableInClauseTerms(
134915 Parse *pParse, /* The parsing context */
134916 int iEq, /* Look at loop terms starting here */
134917 WhereLoop *pLoop, /* The current loop */
134918 Expr *pX /* The IN expression to be reduced */
134919){
134920 sqlite3 *db = pParse->db;
134921 Expr *pNew = sqlite3ExprDup(db, pX, 0);
134922 if( db->mallocFailed==0 ){
134923 ExprList *pOrigRhs = pNew->x.pSelect->pEList; /* Original unmodified RHS */
134924 ExprList *pOrigLhs = pNew->pLeft->x.pList; /* Original unmodified LHS */
134925 ExprList *pRhs = 0; /* New RHS after modifications */
134926 ExprList *pLhs = 0; /* New LHS after mods */
134927 int i; /* Loop counter */
134928 Select *pSelect; /* Pointer to the SELECT on the RHS */
134929
134930 for(i=iEq; i<pLoop->nLTerm; i++){
134931 if( pLoop->aLTerm[i]->pExpr==pX ){
134932 int iField = pLoop->aLTerm[i]->iField - 1;
134933 assert( pOrigRhs->a[iField].pExpr!=0 );
134934 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
134935 pOrigRhs->a[iField].pExpr = 0;
134936 assert( pOrigLhs->a[iField].pExpr!=0 );
134937 pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
134938 pOrigLhs->a[iField].pExpr = 0;
134939 }
134940 }
134941 sqlite3ExprListDelete(db, pOrigRhs);
134942 sqlite3ExprListDelete(db, pOrigLhs);
134943 pNew->pLeft->x.pList = pLhs;
134944 pNew->x.pSelect->pEList = pRhs;
134945 if( pLhs && pLhs->nExpr==1 ){
134946 /* Take care here not to generate a TK_VECTOR containing only a
134947 ** single value. Since the parser never creates such a vector, some
134948 ** of the subroutines do not handle this case. */
134949 Expr *p = pLhs->a[0].pExpr;
134950 pLhs->a[0].pExpr = 0;
134951 sqlite3ExprDelete(db, pNew->pLeft);
134952 pNew->pLeft = p;
134953 }
134954 pSelect = pNew->x.pSelect;
134955 if( pSelect->pOrderBy ){
134956 /* If the SELECT statement has an ORDER BY clause, zero the
134957 ** iOrderByCol variables. These are set to non-zero when an
134958 ** ORDER BY term exactly matches one of the terms of the
134959 ** result-set. Since the result-set of the SELECT statement may
134960 ** have been modified or reordered, these variables are no longer
134961 ** set correctly. Since setting them is just an optimization,
134962 ** it's easiest just to zero them here. */
134963 ExprList *pOrderBy = pSelect->pOrderBy;
134964 for(i=0; i<pOrderBy->nExpr; i++){
134965 pOrderBy->a[i].u.x.iOrderByCol = 0;
134966 }
134967 }
134968
134969#if 0
134970 printf("For indexing, change the IN expr:\n");
134971 sqlite3TreeViewExpr(0, pX, 0);
134972 printf("Into:\n");
134973 sqlite3TreeViewExpr(0, pNew, 0);
134974#endif
134975 }
134976 return pNew;
134977}
134978
134979
134980/*
134981** Generate code for a single equality term of the WHERE clause. An equality
134982** term can be either X=expr or X IN (...). pTerm is the term to be
134983** coded.
134984**
134985** The current value for the constraint is left in a register, the index
134986** of which is returned. An attempt is made store the result in iTarget but
134987** this is only guaranteed for TK_ISNULL and TK_IN constraints. If the
134988** constraint is a TK_EQ or TK_IS, then the current value might be left in
134989** some other register and it is the caller's responsibility to compensate.
134990**
134991** For a constraint of the form X=expr, the expression is evaluated in
134992** straight-line code. For constraints of the form X IN (...)
134993** this routine sets up a loop that will iterate over all values of X.
134994*/
134995static int codeEqualityTerm(
134996 Parse *pParse, /* The parsing context */
134997 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
134998 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
134999 int iEq, /* Index of the equality term within this level */
135000 int bRev, /* True for reverse-order IN operations */
135001 int iTarget /* Attempt to leave results in this register */
135002){
135003 Expr *pX = pTerm->pExpr;
135004 Vdbe *v = pParse->pVdbe;
135005 int iReg; /* Register holding results */
135006
135007 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
135008 assert( iTarget>0 );
135009 if( pX->op==TK_EQ || pX->op==TK_IS ){
135010 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
135011 }else if( pX->op==TK_ISNULL ){
135012 iReg = iTarget;
135013 sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
135014#ifndef SQLITE_OMIT_SUBQUERY
135015 }else{
135016 int eType = IN_INDEX_NOOP;
135017 int iTab;
135018 struct InLoop *pIn;
135019 WhereLoop *pLoop = pLevel->pWLoop;
135020 int i;
135021 int nEq = 0;
135022 int *aiMap = 0;
135023
135024 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
135025 && pLoop->u.btree.pIndex!=0
135026 && pLoop->u.btree.pIndex->aSortOrder[iEq]
135027 ){
135028 testcase( iEq==0 );
135029 testcase( bRev );
135030 bRev = !bRev;
135031 }
135032 assert( pX->op==TK_IN );
135033 iReg = iTarget;
135034
135035 for(i=0; i<iEq; i++){
135036 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
135037 disableTerm(pLevel, pTerm);
135038 return iTarget;
135039 }
135040 }
135041 for(i=iEq;i<pLoop->nLTerm; i++){
135042 assert( pLoop->aLTerm[i]!=0 );
135043 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
135044 }
135045
135046 if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){
135047 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, 0);
135048 }else{
135049 sqlite3 *db = pParse->db;
135050 pX = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
135051
135052 if( !db->mallocFailed ){
135053 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
135054 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap);
135055 pTerm->pExpr->iTable = pX->iTable;
135056 }
135057 sqlite3ExprDelete(db, pX);
135058 pX = pTerm->pExpr;
135059 }
135060
135061 if( eType==IN_INDEX_INDEX_DESC ){
135062 testcase( bRev );
135063 bRev = !bRev;
135064 }
135065 iTab = pX->iTable;
135066 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
135067 VdbeCoverageIf(v, bRev);
135068 VdbeCoverageIf(v, !bRev);
135069 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
135070
135071 pLoop->wsFlags |= WHERE_IN_ABLE;
135072 if( pLevel->u.in.nIn==0 ){
135073 pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
135074 }
135075
135076 i = pLevel->u.in.nIn;
135077 pLevel->u.in.nIn += nEq;
135078 pLevel->u.in.aInLoop =
135079 sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
135080 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
135081 pIn = pLevel->u.in.aInLoop;
135082 if( pIn ){
135083 int iMap = 0; /* Index in aiMap[] */
135084 pIn += i;
135085 for(i=iEq;i<pLoop->nLTerm; i++){
135086 if( pLoop->aLTerm[i]->pExpr==pX ){
135087 int iOut = iReg + i - iEq;
135088 if( eType==IN_INDEX_ROWID ){
135089 testcase( nEq>1 ); /* Happens with a UNIQUE index on ROWID */
135090 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
135091 }else{
135092 int iCol = aiMap ? aiMap[iMap++] : 0;
135093 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
135094 }
135095 sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v);
135096 if( i==iEq ){
135097 pIn->iCur = iTab;
135098 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
135099 if( iEq>0 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ){
135100 pIn->iBase = iReg - i;
135101 pIn->nPrefix = i;
135102 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
135103 }else{
135104 pIn->nPrefix = 0;
135105 }
135106 }else{
135107 pIn->eEndLoopOp = OP_Noop;
135108 }
135109 pIn++;
135110 }
135111 }
135112 }else{
135113 pLevel->u.in.nIn = 0;
135114 }
135115 sqlite3DbFree(pParse->db, aiMap);
135116#endif
135117 }
135118 disableTerm(pLevel, pTerm);
135119 return iReg;
135120}
135121
135122/*
135123** Generate code that will evaluate all == and IN constraints for an
135124** index scan.
135125**
135126** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
135127** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
135128** The index has as many as three equality constraints, but in this
135129** example, the third "c" value is an inequality. So only two
135130** constraints are coded. This routine will generate code to evaluate
135131** a==5 and b IN (1,2,3). The current values for a and b will be stored
135132** in consecutive registers and the index of the first register is returned.
135133**
135134** In the example above nEq==2. But this subroutine works for any value
135135** of nEq including 0. If nEq==0, this routine is nearly a no-op.
135136** The only thing it does is allocate the pLevel->iMem memory cell and
135137** compute the affinity string.
135138**
135139** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
135140** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
135141** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
135142** occurs after the nEq quality constraints.
135143**
135144** This routine allocates a range of nEq+nExtraReg memory cells and returns
135145** the index of the first memory cell in that range. The code that
135146** calls this routine will use that memory range to store keys for
135147** start and termination conditions of the loop.
135148** key value of the loop. If one or more IN operators appear, then
135149** this routine allocates an additional nEq memory cells for internal
135150** use.
135151**
135152** Before returning, *pzAff is set to point to a buffer containing a
135153** copy of the column affinity string of the index allocated using
135154** sqlite3DbMalloc(). Except, entries in the copy of the string associated
135155** with equality constraints that use BLOB or NONE affinity are set to
135156** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
135157**
135158** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
135159** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
135160**
135161** In the example above, the index on t1(a) has TEXT affinity. But since
135162** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
135163** no conversion should be attempted before using a t2.b value as part of
135164** a key to search the index. Hence the first byte in the returned affinity
135165** string in this example would be set to SQLITE_AFF_BLOB.
135166*/
135167static int codeAllEqualityTerms(
135168 Parse *pParse, /* Parsing context */
135169 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
135170 int bRev, /* Reverse the order of IN operators */
135171 int nExtraReg, /* Number of extra registers to allocate */
135172 char **pzAff /* OUT: Set to point to affinity string */
135173){
135174 u16 nEq; /* The number of == or IN constraints to code */
135175 u16 nSkip; /* Number of left-most columns to skip */
135176 Vdbe *v = pParse->pVdbe; /* The vm under construction */
135177 Index *pIdx; /* The index being used for this loop */
135178 WhereTerm *pTerm; /* A single constraint term */
135179 WhereLoop *pLoop; /* The WhereLoop object */
135180 int j; /* Loop counter */
135181 int regBase; /* Base register */
135182 int nReg; /* Number of registers to allocate */
135183 char *zAff; /* Affinity string to return */
135184
135185 /* This module is only called on query plans that use an index. */
135186 pLoop = pLevel->pWLoop;
135187 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
135188 nEq = pLoop->u.btree.nEq;
135189 nSkip = pLoop->nSkip;
135190 pIdx = pLoop->u.btree.pIndex;
135191 assert( pIdx!=0 );
135192
135193 /* Figure out how many memory cells we will need then allocate them.
135194 */
135195 regBase = pParse->nMem + 1;
135196 nReg = pLoop->u.btree.nEq + nExtraReg;
135197 pParse->nMem += nReg;
135198
135199 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
135200 assert( zAff!=0 || pParse->db->mallocFailed );
135201
135202 if( nSkip ){
135203 int iIdxCur = pLevel->iIdxCur;
135204 sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
135205 VdbeCoverageIf(v, bRev==0);
135206 VdbeCoverageIf(v, bRev!=0);
135207 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
135208 j = sqlite3VdbeAddOp0(v, OP_Goto);
135209 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
135210 iIdxCur, 0, regBase, nSkip);
135211 VdbeCoverageIf(v, bRev==0);
135212 VdbeCoverageIf(v, bRev!=0);
135213 sqlite3VdbeJumpHere(v, j);
135214 for(j=0; j<nSkip; j++){
135215 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
135216 testcase( pIdx->aiColumn[j]==XN_EXPR );
135217 VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
135218 }
135219 }
135220
135221 /* Evaluate the equality constraints
135222 */
135223 assert( zAff==0 || (int)strlen(zAff)>=nEq );
135224 for(j=nSkip; j<nEq; j++){
135225 int r1;
135226 pTerm = pLoop->aLTerm[j];
135227 assert( pTerm!=0 );
135228 /* The following testcase is true for indices with redundant columns.
135229 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
135230 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
135231 testcase( pTerm->wtFlags & TERM_VIRTUAL );
135232 r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
135233 if( r1!=regBase+j ){
135234 if( nReg==1 ){
135235 sqlite3ReleaseTempReg(pParse, regBase);
135236 regBase = r1;
135237 }else{
135238 sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
135239 }
135240 }
135241 if( pTerm->eOperator & WO_IN ){
135242 if( pTerm->pExpr->flags & EP_xIsSelect ){
135243 /* No affinity ever needs to be (or should be) applied to a value
135244 ** from the RHS of an "? IN (SELECT ...)" expression. The
135245 ** sqlite3FindInIndex() routine has already ensured that the
135246 ** affinity of the comparison has been applied to the value. */
135247 if( zAff ) zAff[j] = SQLITE_AFF_BLOB;
135248 }
135249 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
135250 Expr *pRight = pTerm->pExpr->pRight;
135251 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
135252 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
135253 VdbeCoverage(v);
135254 }
135255 if( zAff ){
135256 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
135257 zAff[j] = SQLITE_AFF_BLOB;
135258 }
135259 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
135260 zAff[j] = SQLITE_AFF_BLOB;
135261 }
135262 }
135263 }
135264 }
135265 *pzAff = zAff;
135266 return regBase;
135267}
135268
135269#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
135270/*
135271** If the most recently coded instruction is a constant range constraint
135272** (a string literal) that originated from the LIKE optimization, then
135273** set P3 and P5 on the OP_String opcode so that the string will be cast
135274** to a BLOB at appropriate times.
135275**
135276** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
135277** expression: "x>='ABC' AND x<'abd'". But this requires that the range
135278** scan loop run twice, once for strings and a second time for BLOBs.
135279** The OP_String opcodes on the second pass convert the upper and lower
135280** bound string constants to blobs. This routine makes the necessary changes
135281** to the OP_String opcodes for that to happen.
135282**
135283** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then
135284** only the one pass through the string space is required, so this routine
135285** becomes a no-op.
135286*/
135287static void whereLikeOptimizationStringFixup(
135288 Vdbe *v, /* prepared statement under construction */
135289 WhereLevel *pLevel, /* The loop that contains the LIKE operator */
135290 WhereTerm *pTerm /* The upper or lower bound just coded */
135291){
135292 if( pTerm->wtFlags & TERM_LIKEOPT ){
135293 VdbeOp *pOp;
135294 assert( pLevel->iLikeRepCntr>0 );
135295 pOp = sqlite3VdbeGetOp(v, -1);
135296 assert( pOp!=0 );
135297 assert( pOp->opcode==OP_String8
135298 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
135299 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
135300 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
135301 }
135302}
135303#else
135304# define whereLikeOptimizationStringFixup(A,B,C)
135305#endif
135306
135307#ifdef SQLITE_ENABLE_CURSOR_HINTS
135308/*
135309** Information is passed from codeCursorHint() down to individual nodes of
135310** the expression tree (by sqlite3WalkExpr()) using an instance of this
135311** structure.
135312*/
135313struct CCurHint {
135314 int iTabCur; /* Cursor for the main table */
135315 int iIdxCur; /* Cursor for the index, if pIdx!=0. Unused otherwise */
135316 Index *pIdx; /* The index used to access the table */
135317};
135318
135319/*
135320** This function is called for every node of an expression that is a candidate
135321** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference
135322** the table CCurHint.iTabCur, verify that the same column can be
135323** accessed through the index. If it cannot, then set pWalker->eCode to 1.
135324*/
135325static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){
135326 struct CCurHint *pHint = pWalker->u.pCCurHint;
135327 assert( pHint->pIdx!=0 );
135328 if( pExpr->op==TK_COLUMN
135329 && pExpr->iTable==pHint->iTabCur
135330 && sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn)<0
135331 ){
135332 pWalker->eCode = 1;
135333 }
135334 return WRC_Continue;
135335}
135336
135337/*
135338** Test whether or not expression pExpr, which was part of a WHERE clause,
135339** should be included in the cursor-hint for a table that is on the rhs
135340** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
135341** expression is not suitable.
135342**
135343** An expression is unsuitable if it might evaluate to non NULL even if
135344** a TK_COLUMN node that does affect the value of the expression is set
135345** to NULL. For example:
135346**
135347** col IS NULL
135348** col IS NOT NULL
135349** coalesce(col, 1)
135350** CASE WHEN col THEN 0 ELSE 1 END
135351*/
135352static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){
135353 if( pExpr->op==TK_IS
135354 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
135355 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
135356 ){
135357 pWalker->eCode = 1;
135358 }else if( pExpr->op==TK_FUNCTION ){
135359 int d1;
135360 char d2[4];
135361 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
135362 pWalker->eCode = 1;
135363 }
135364 }
135365
135366 return WRC_Continue;
135367}
135368
135369
135370/*
135371** This function is called on every node of an expression tree used as an
135372** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN
135373** that accesses any table other than the one identified by
135374** CCurHint.iTabCur, then do the following:
135375**
135376** 1) allocate a register and code an OP_Column instruction to read
135377** the specified column into the new register, and
135378**
135379** 2) transform the expression node to a TK_REGISTER node that reads
135380** from the newly populated register.
135381**
135382** Also, if the node is a TK_COLUMN that does access the table idenified
135383** by pCCurHint.iTabCur, and an index is being used (which we will
135384** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into
135385** an access of the index rather than the original table.
135386*/
135387static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){
135388 int rc = WRC_Continue;
135389 struct CCurHint *pHint = pWalker->u.pCCurHint;
135390 if( pExpr->op==TK_COLUMN ){
135391 if( pExpr->iTable!=pHint->iTabCur ){
135392 int reg = ++pWalker->pParse->nMem; /* Register for column value */
135393 sqlite3ExprCode(pWalker->pParse, pExpr, reg);
135394 pExpr->op = TK_REGISTER;
135395 pExpr->iTable = reg;
135396 }else if( pHint->pIdx!=0 ){
135397 pExpr->iTable = pHint->iIdxCur;
135398 pExpr->iColumn = sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn);
135399 assert( pExpr->iColumn>=0 );
135400 }
135401 }else if( pExpr->op==TK_AGG_FUNCTION ){
135402 /* An aggregate function in the WHERE clause of a query means this must
135403 ** be a correlated sub-query, and expression pExpr is an aggregate from
135404 ** the parent context. Do not walk the function arguments in this case.
135405 **
135406 ** todo: It should be possible to replace this node with a TK_REGISTER
135407 ** expression, as the result of the expression must be stored in a
135408 ** register at this point. The same holds for TK_AGG_COLUMN nodes. */
135409 rc = WRC_Prune;
135410 }
135411 return rc;
135412}
135413
135414/*
135415** Insert an OP_CursorHint instruction if it is appropriate to do so.
135416*/
135417static void codeCursorHint(
135418 struct SrcList_item *pTabItem, /* FROM clause item */
135419 WhereInfo *pWInfo, /* The where clause */
135420 WhereLevel *pLevel, /* Which loop to provide hints for */
135421 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
135422){
135423 Parse *pParse = pWInfo->pParse;
135424 sqlite3 *db = pParse->db;
135425 Vdbe *v = pParse->pVdbe;
135426 Expr *pExpr = 0;
135427 WhereLoop *pLoop = pLevel->pWLoop;
135428 int iCur;
135429 WhereClause *pWC;
135430 WhereTerm *pTerm;
135431 int i, j;
135432 struct CCurHint sHint;
135433 Walker sWalker;
135434
135435 if( OptimizationDisabled(db, SQLITE_CursorHints) ) return;
135436 iCur = pLevel->iTabCur;
135437 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
135438 sHint.iTabCur = iCur;
135439 sHint.iIdxCur = pLevel->iIdxCur;
135440 sHint.pIdx = pLoop->u.btree.pIndex;
135441 memset(&sWalker, 0, sizeof(sWalker));
135442 sWalker.pParse = pParse;
135443 sWalker.u.pCCurHint = &sHint;
135444 pWC = &pWInfo->sWC;
135445 for(i=0; i<pWC->nTerm; i++){
135446 pTerm = &pWC->a[i];
135447 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
135448 if( pTerm->prereqAll & pLevel->notReady ) continue;
135449
135450 /* Any terms specified as part of the ON(...) clause for any LEFT
135451 ** JOIN for which the current table is not the rhs are omitted
135452 ** from the cursor-hint.
135453 **
135454 ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms
135455 ** that were specified as part of the WHERE clause must be excluded.
135456 ** This is to address the following:
135457 **
135458 ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
135459 **
135460 ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
135461 ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is
135462 ** pushed down to the cursor, this row is filtered out, causing
135463 ** SQLite to synthesize a row of NULL values. Which does match the
135464 ** WHERE clause, and so the query returns a row. Which is incorrect.
135465 **
135466 ** For the same reason, WHERE terms such as:
135467 **
135468 ** WHERE 1 = (t2.c IS NULL)
135469 **
135470 ** are also excluded. See codeCursorHintIsOrFunction() for details.
135471 */
135472 if( pTabItem->fg.jointype & JT_LEFT ){
135473 Expr *pExpr = pTerm->pExpr;
135474 if( !ExprHasProperty(pExpr, EP_FromJoin)
135475 || pExpr->iRightJoinTable!=pTabItem->iCursor
135476 ){
135477 sWalker.eCode = 0;
135478 sWalker.xExprCallback = codeCursorHintIsOrFunction;
135479 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
135480 if( sWalker.eCode ) continue;
135481 }
135482 }else{
135483 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue;
135484 }
135485
135486 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
135487 ** the cursor. These terms are not needed as hints for a pure range
135488 ** scan (that has no == terms) so omit them. */
135489 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
135490 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
135491 if( j<pLoop->nLTerm ) continue;
135492 }
135493
135494 /* No subqueries or non-deterministic functions allowed */
135495 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
135496
135497 /* For an index scan, make sure referenced columns are actually in
135498 ** the index. */
135499 if( sHint.pIdx!=0 ){
135500 sWalker.eCode = 0;
135501 sWalker.xExprCallback = codeCursorHintCheckExpr;
135502 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
135503 if( sWalker.eCode ) continue;
135504 }
135505
135506 /* If we survive all prior tests, that means this term is worth hinting */
135507 pExpr = sqlite3ExprAnd(db, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
135508 }
135509 if( pExpr!=0 ){
135510 sWalker.xExprCallback = codeCursorHintFixExpr;
135511 sqlite3WalkExpr(&sWalker, pExpr);
135512 sqlite3VdbeAddOp4(v, OP_CursorHint,
135513 (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
135514 (const char*)pExpr, P4_EXPR);
135515 }
135516}
135517#else
135518# define codeCursorHint(A,B,C,D) /* No-op */
135519#endif /* SQLITE_ENABLE_CURSOR_HINTS */
135520
135521/*
135522** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
135523** a rowid value just read from cursor iIdxCur, open on index pIdx. This
135524** function generates code to do a deferred seek of cursor iCur to the
135525** rowid stored in register iRowid.
135526**
135527** Normally, this is just:
135528**
135529** OP_DeferredSeek $iCur $iRowid
135530**
135531** However, if the scan currently being coded is a branch of an OR-loop and
135532** the statement currently being coded is a SELECT, then P3 of OP_DeferredSeek
135533** is set to iIdxCur and P4 is set to point to an array of integers
135534** containing one entry for each column of the table cursor iCur is open
135535** on. For each table column, if the column is the i'th column of the
135536** index, then the corresponding array entry is set to (i+1). If the column
135537** does not appear in the index at all, the array entry is set to 0.
135538*/
135539static void codeDeferredSeek(
135540 WhereInfo *pWInfo, /* Where clause context */
135541 Index *pIdx, /* Index scan is using */
135542 int iCur, /* Cursor for IPK b-tree */
135543 int iIdxCur /* Index cursor */
135544){
135545 Parse *pParse = pWInfo->pParse; /* Parse context */
135546 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
135547
135548 assert( iIdxCur>0 );
135549 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
135550
135551 sqlite3VdbeAddOp3(v, OP_DeferredSeek, iIdxCur, 0, iCur);
135552 if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
135553 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
135554 ){
135555 int i;
135556 Table *pTab = pIdx->pTable;
135557 int *ai = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*(pTab->nCol+1));
135558 if( ai ){
135559 ai[0] = pTab->nCol;
135560 for(i=0; i<pIdx->nColumn-1; i++){
135561 assert( pIdx->aiColumn[i]<pTab->nCol );
135562 if( pIdx->aiColumn[i]>=0 ) ai[pIdx->aiColumn[i]+1] = i+1;
135563 }
135564 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
135565 }
135566 }
135567}
135568
135569/*
135570** If the expression passed as the second argument is a vector, generate
135571** code to write the first nReg elements of the vector into an array
135572** of registers starting with iReg.
135573**
135574** If the expression is not a vector, then nReg must be passed 1. In
135575** this case, generate code to evaluate the expression and leave the
135576** result in register iReg.
135577*/
135578static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
135579 assert( nReg>0 );
135580 if( p && sqlite3ExprIsVector(p) ){
135581#ifndef SQLITE_OMIT_SUBQUERY
135582 if( (p->flags & EP_xIsSelect) ){
135583 Vdbe *v = pParse->pVdbe;
135584 int iSelect = sqlite3CodeSubselect(pParse, p, 0, 0);
135585 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
135586 }else
135587#endif
135588 {
135589 int i;
135590 ExprList *pList = p->x.pList;
135591 assert( nReg<=pList->nExpr );
135592 for(i=0; i<nReg; i++){
135593 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
135594 }
135595 }
135596 }else{
135597 assert( nReg==1 );
135598 sqlite3ExprCode(pParse, p, iReg);
135599 }
135600}
135601
135602/* An instance of the IdxExprTrans object carries information about a
135603** mapping from an expression on table columns into a column in an index
135604** down through the Walker.
135605*/
135606typedef struct IdxExprTrans {
135607 Expr *pIdxExpr; /* The index expression */
135608 int iTabCur; /* The cursor of the corresponding table */
135609 int iIdxCur; /* The cursor for the index */
135610 int iIdxCol; /* The column for the index */
135611} IdxExprTrans;
135612
135613/* The walker node callback used to transform matching expressions into
135614** a reference to an index column for an index on an expression.
135615**
135616** If pExpr matches, then transform it into a reference to the index column
135617** that contains the value of pExpr.
135618*/
135619static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
135620 IdxExprTrans *pX = p->u.pIdxTrans;
135621 if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
135622 pExpr->op = TK_COLUMN;
135623 pExpr->iTable = pX->iIdxCur;
135624 pExpr->iColumn = pX->iIdxCol;
135625 pExpr->pTab = 0;
135626 return WRC_Prune;
135627 }else{
135628 return WRC_Continue;
135629 }
135630}
135631
135632/*
135633** For an indexes on expression X, locate every instance of expression X
135634** in pExpr and change that subexpression into a reference to the appropriate
135635** column of the index.
135636*/
135637static void whereIndexExprTrans(
135638 Index *pIdx, /* The Index */
135639 int iTabCur, /* Cursor of the table that is being indexed */
135640 int iIdxCur, /* Cursor of the index itself */
135641 WhereInfo *pWInfo /* Transform expressions in this WHERE clause */
135642){
135643 int iIdxCol; /* Column number of the index */
135644 ExprList *aColExpr; /* Expressions that are indexed */
135645 Walker w;
135646 IdxExprTrans x;
135647 aColExpr = pIdx->aColExpr;
135648 if( aColExpr==0 ) return; /* Not an index on expressions */
135649 memset(&w, 0, sizeof(w));
135650 w.xExprCallback = whereIndexExprTransNode;
135651 w.u.pIdxTrans = &x;
135652 x.iTabCur = iTabCur;
135653 x.iIdxCur = iIdxCur;
135654 for(iIdxCol=0; iIdxCol<aColExpr->nExpr; iIdxCol++){
135655 if( pIdx->aiColumn[iIdxCol]!=XN_EXPR ) continue;
135656 assert( aColExpr->a[iIdxCol].pExpr!=0 );
135657 x.iIdxCol = iIdxCol;
135658 x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
135659 sqlite3WalkExpr(&w, pWInfo->pWhere);
135660 sqlite3WalkExprList(&w, pWInfo->pOrderBy);
135661 sqlite3WalkExprList(&w, pWInfo->pResultSet);
135662 }
135663}
135664
135665/*
135666** Generate code for the start of the iLevel-th loop in the WHERE clause
135667** implementation described by pWInfo.
135668*/
135669SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
135670 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
135671 int iLevel, /* Which level of pWInfo->a[] should be coded */
135672 Bitmask notReady /* Which tables are currently available */
135673){
135674 int j, k; /* Loop counters */
135675 int iCur; /* The VDBE cursor for the table */
135676 int addrNxt; /* Where to jump to continue with the next IN case */
135677 int omitTable; /* True if we use the index only */
135678 int bRev; /* True if we need to scan in reverse order */
135679 WhereLevel *pLevel; /* The where level to be coded */
135680 WhereLoop *pLoop; /* The WhereLoop object being coded */
135681 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
135682 WhereTerm *pTerm; /* A WHERE clause term */
135683 Parse *pParse; /* Parsing context */
135684 sqlite3 *db; /* Database connection */
135685 Vdbe *v; /* The prepared stmt under constructions */
135686 struct SrcList_item *pTabItem; /* FROM clause term being coded */
135687 int addrBrk; /* Jump here to break out of the loop */
135688 int addrHalt; /* addrBrk for the outermost loop */
135689 int addrCont; /* Jump here to continue with next cycle */
135690 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
135691 int iReleaseReg = 0; /* Temp register to free before returning */
135692 Index *pIdx = 0; /* Index used by loop (if any) */
135693 int iLoop; /* Iteration of constraint generator loop */
135694
135695 pParse = pWInfo->pParse;
135696 v = pParse->pVdbe;
135697 pWC = &pWInfo->sWC;
135698 db = pParse->db;
135699 pLevel = &pWInfo->a[iLevel];
135700 pLoop = pLevel->pWLoop;
135701 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
135702 iCur = pTabItem->iCursor;
135703 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
135704 bRev = (pWInfo->revMask>>iLevel)&1;
135705 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
135706 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0;
135707 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
135708
135709 /* Create labels for the "break" and "continue" instructions
135710 ** for the current loop. Jump to addrBrk to break out of a loop.
135711 ** Jump to cont to go immediately to the next iteration of the
135712 ** loop.
135713 **
135714 ** When there is an IN operator, we also have a "addrNxt" label that
135715 ** means to continue with the next IN value combination. When
135716 ** there are no IN operators in the constraints, the "addrNxt" label
135717 ** is the same as "addrBrk".
135718 */
135719 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
135720 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
135721
135722 /* If this is the right table of a LEFT OUTER JOIN, allocate and
135723 ** initialize a memory cell that records if this table matches any
135724 ** row of the left table of the join.
135725 */
135726 assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
135727 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
135728 );
135729 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
135730 pLevel->iLeftJoin = ++pParse->nMem;
135731 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
135732 VdbeComment((v, "init LEFT JOIN no-match flag"));
135733 }
135734
135735 /* Compute a safe address to jump to if we discover that the table for
135736 ** this loop is empty and can never contribute content. */
135737 for(j=iLevel; j>0 && pWInfo->a[j].iLeftJoin==0; j--){}
135738 addrHalt = pWInfo->a[j].addrBrk;
135739
135740 /* Special case of a FROM clause subquery implemented as a co-routine */
135741 if( pTabItem->fg.viaCoroutine ){
135742 int regYield = pTabItem->regReturn;
135743 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
135744 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
135745 VdbeCoverage(v);
135746 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
135747 pLevel->op = OP_Goto;
135748 }else
135749
135750#ifndef SQLITE_OMIT_VIRTUALTABLE
135751 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
135752 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
135753 ** to access the data.
135754 */
135755 int iReg; /* P3 Value for OP_VFilter */
135756 int addrNotFound;
135757 int nConstraint = pLoop->nLTerm;
135758 int iIn; /* Counter for IN constraints */
135759
135760 iReg = sqlite3GetTempRange(pParse, nConstraint+2);
135761 addrNotFound = pLevel->addrBrk;
135762 for(j=0; j<nConstraint; j++){
135763 int iTarget = iReg+j+2;
135764 pTerm = pLoop->aLTerm[j];
135765 if( NEVER(pTerm==0) ) continue;
135766 if( pTerm->eOperator & WO_IN ){
135767 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
135768 addrNotFound = pLevel->addrNxt;
135769 }else{
135770 Expr *pRight = pTerm->pExpr->pRight;
135771 codeExprOrVector(pParse, pRight, iTarget, 1);
135772 }
135773 }
135774 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
135775 sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
135776 sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
135777 pLoop->u.vtab.idxStr,
135778 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
135779 VdbeCoverage(v);
135780 pLoop->u.vtab.needFree = 0;
135781 pLevel->p1 = iCur;
135782 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
135783 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
135784 iIn = pLevel->u.in.nIn;
135785 for(j=nConstraint-1; j>=0; j--){
135786 pTerm = pLoop->aLTerm[j];
135787 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
135788 disableTerm(pLevel, pTerm);
135789 }else if( (pTerm->eOperator & WO_IN)!=0 ){
135790 Expr *pCompare; /* The comparison operator */
135791 Expr *pRight; /* RHS of the comparison */
135792 VdbeOp *pOp; /* Opcode to access the value of the IN constraint */
135793
135794 /* Reload the constraint value into reg[iReg+j+2]. The same value
135795 ** was loaded into the same register prior to the OP_VFilter, but
135796 ** the xFilter implementation might have changed the datatype or
135797 ** encoding of the value in the register, so it *must* be reloaded. */
135798 assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed );
135799 if( !db->mallocFailed ){
135800 assert( iIn>0 );
135801 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[--iIn].addrInTop);
135802 assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid );
135803 assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 );
135804 assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 );
135805 testcase( pOp->opcode==OP_Rowid );
135806 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
135807 }
135808
135809 /* Generate code that will continue to the next row if
135810 ** the IN constraint is not satisfied */
135811 pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0);
135812 assert( pCompare!=0 || db->mallocFailed );
135813 if( pCompare ){
135814 pCompare->pLeft = pTerm->pExpr->pLeft;
135815 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
135816 if( pRight ){
135817 pRight->iTable = iReg+j+2;
135818 sqlite3ExprIfFalse(pParse, pCompare, pLevel->addrCont, 0);
135819 }
135820 pCompare->pLeft = 0;
135821 sqlite3ExprDelete(db, pCompare);
135822 }
135823 }
135824 }
135825 /* These registers need to be preserved in case there is an IN operator
135826 ** loop. So we could deallocate the registers here (and potentially
135827 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
135828 ** simpler and safer to simply not reuse the registers.
135829 **
135830 ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
135831 */
135832 }else
135833#endif /* SQLITE_OMIT_VIRTUALTABLE */
135834
135835 if( (pLoop->wsFlags & WHERE_IPK)!=0
135836 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
135837 ){
135838 /* Case 2: We can directly reference a single row using an
135839 ** equality comparison against the ROWID field. Or
135840 ** we reference multiple rows using a "rowid IN (...)"
135841 ** construct.
135842 */
135843 assert( pLoop->u.btree.nEq==1 );
135844 pTerm = pLoop->aLTerm[0];
135845 assert( pTerm!=0 );
135846 assert( pTerm->pExpr!=0 );
135847 assert( omitTable==0 );
135848 testcase( pTerm->wtFlags & TERM_VIRTUAL );
135849 iReleaseReg = ++pParse->nMem;
135850 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
135851 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
135852 addrNxt = pLevel->addrNxt;
135853 sqlite3VdbeAddOp3(v, OP_SeekRowid, iCur, addrNxt, iRowidReg);
135854 VdbeCoverage(v);
135855 pLevel->op = OP_Noop;
135856 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
135857 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
135858 ){
135859 /* Case 3: We have an inequality comparison against the ROWID field.
135860 */
135861 int testOp = OP_Noop;
135862 int start;
135863 int memEndValue = 0;
135864 WhereTerm *pStart, *pEnd;
135865
135866 assert( omitTable==0 );
135867 j = 0;
135868 pStart = pEnd = 0;
135869 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
135870 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
135871 assert( pStart!=0 || pEnd!=0 );
135872 if( bRev ){
135873 pTerm = pStart;
135874 pStart = pEnd;
135875 pEnd = pTerm;
135876 }
135877 codeCursorHint(pTabItem, pWInfo, pLevel, pEnd);
135878 if( pStart ){
135879 Expr *pX; /* The expression that defines the start bound */
135880 int r1, rTemp; /* Registers for holding the start boundary */
135881 int op; /* Cursor seek operation */
135882
135883 /* The following constant maps TK_xx codes into corresponding
135884 ** seek opcodes. It depends on a particular ordering of TK_xx
135885 */
135886 const u8 aMoveOp[] = {
135887 /* TK_GT */ OP_SeekGT,
135888 /* TK_LE */ OP_SeekLE,
135889 /* TK_LT */ OP_SeekLT,
135890 /* TK_GE */ OP_SeekGE
135891 };
135892 assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */
135893 assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */
135894 assert( TK_GE==TK_GT+3 ); /* ... is correcct. */
135895
135896 assert( (pStart->wtFlags & TERM_VNULL)==0 );
135897 testcase( pStart->wtFlags & TERM_VIRTUAL );
135898 pX = pStart->pExpr;
135899 assert( pX!=0 );
135900 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
135901 if( sqlite3ExprIsVector(pX->pRight) ){
135902 r1 = rTemp = sqlite3GetTempReg(pParse);
135903 codeExprOrVector(pParse, pX->pRight, r1, 1);
135904 testcase( pX->op==TK_GT );
135905 testcase( pX->op==TK_GE );
135906 testcase( pX->op==TK_LT );
135907 testcase( pX->op==TK_LE );
135908 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
135909 assert( pX->op!=TK_GT || op==OP_SeekGE );
135910 assert( pX->op!=TK_GE || op==OP_SeekGE );
135911 assert( pX->op!=TK_LT || op==OP_SeekLE );
135912 assert( pX->op!=TK_LE || op==OP_SeekLE );
135913 }else{
135914 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
135915 disableTerm(pLevel, pStart);
135916 op = aMoveOp[(pX->op - TK_GT)];
135917 }
135918 sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
135919 VdbeComment((v, "pk"));
135920 VdbeCoverageIf(v, pX->op==TK_GT);
135921 VdbeCoverageIf(v, pX->op==TK_LE);
135922 VdbeCoverageIf(v, pX->op==TK_LT);
135923 VdbeCoverageIf(v, pX->op==TK_GE);
135924 sqlite3ReleaseTempReg(pParse, rTemp);
135925 }else{
135926 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrHalt);
135927 VdbeCoverageIf(v, bRev==0);
135928 VdbeCoverageIf(v, bRev!=0);
135929 }
135930 if( pEnd ){
135931 Expr *pX;
135932 pX = pEnd->pExpr;
135933 assert( pX!=0 );
135934 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
135935 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
135936 testcase( pEnd->wtFlags & TERM_VIRTUAL );
135937 memEndValue = ++pParse->nMem;
135938 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
135939 if( 0==sqlite3ExprIsVector(pX->pRight)
135940 && (pX->op==TK_LT || pX->op==TK_GT)
135941 ){
135942 testOp = bRev ? OP_Le : OP_Ge;
135943 }else{
135944 testOp = bRev ? OP_Lt : OP_Gt;
135945 }
135946 if( 0==sqlite3ExprIsVector(pX->pRight) ){
135947 disableTerm(pLevel, pEnd);
135948 }
135949 }
135950 start = sqlite3VdbeCurrentAddr(v);
135951 pLevel->op = bRev ? OP_Prev : OP_Next;
135952 pLevel->p1 = iCur;
135953 pLevel->p2 = start;
135954 assert( pLevel->p5==0 );
135955 if( testOp!=OP_Noop ){
135956 iRowidReg = ++pParse->nMem;
135957 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
135958 sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
135959 VdbeCoverageIf(v, testOp==OP_Le);
135960 VdbeCoverageIf(v, testOp==OP_Lt);
135961 VdbeCoverageIf(v, testOp==OP_Ge);
135962 VdbeCoverageIf(v, testOp==OP_Gt);
135963 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
135964 }
135965 }else if( pLoop->wsFlags & WHERE_INDEXED ){
135966 /* Case 4: A scan using an index.
135967 **
135968 ** The WHERE clause may contain zero or more equality
135969 ** terms ("==" or "IN" operators) that refer to the N
135970 ** left-most columns of the index. It may also contain
135971 ** inequality constraints (>, <, >= or <=) on the indexed
135972 ** column that immediately follows the N equalities. Only
135973 ** the right-most column can be an inequality - the rest must
135974 ** use the "==" and "IN" operators. For example, if the
135975 ** index is on (x,y,z), then the following clauses are all
135976 ** optimized:
135977 **
135978 ** x=5
135979 ** x=5 AND y=10
135980 ** x=5 AND y<10
135981 ** x=5 AND y>5 AND y<10
135982 ** x=5 AND y=5 AND z<=10
135983 **
135984 ** The z<10 term of the following cannot be used, only
135985 ** the x=5 term:
135986 **
135987 ** x=5 AND z<10
135988 **
135989 ** N may be zero if there are inequality constraints.
135990 ** If there are no inequality constraints, then N is at
135991 ** least one.
135992 **
135993 ** This case is also used when there are no WHERE clause
135994 ** constraints but an index is selected anyway, in order
135995 ** to force the output order to conform to an ORDER BY.
135996 */
135997 static const u8 aStartOp[] = {
135998 0,
135999 0,
136000 OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
136001 OP_Last, /* 3: (!start_constraints && startEq && bRev) */
136002 OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */
136003 OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */
136004 OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */
136005 OP_SeekLE /* 7: (start_constraints && startEq && bRev) */
136006 };
136007 static const u8 aEndOp[] = {
136008 OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */
136009 OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */
136010 OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */
136011 OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */
136012 };
136013 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
136014 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
136015 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
136016 int regBase; /* Base register holding constraint values */
136017 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
136018 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
136019 int startEq; /* True if range start uses ==, >= or <= */
136020 int endEq; /* True if range end uses ==, >= or <= */
136021 int start_constraints; /* Start of range is constrained */
136022 int nConstraint; /* Number of constraint terms */
136023 int iIdxCur; /* The VDBE cursor for the index */
136024 int nExtraReg = 0; /* Number of extra registers needed */
136025 int op; /* Instruction opcode */
136026 char *zStartAff; /* Affinity for start of range constraint */
136027 char *zEndAff = 0; /* Affinity for end of range constraint */
136028 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
136029 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
136030
136031 pIdx = pLoop->u.btree.pIndex;
136032 iIdxCur = pLevel->iIdxCur;
136033 assert( nEq>=pLoop->nSkip );
136034
136035 /* If this loop satisfies a sort order (pOrderBy) request that
136036 ** was passed to this function to implement a "SELECT min(x) ..."
136037 ** query, then the caller will only allow the loop to run for
136038 ** a single iteration. This means that the first row returned
136039 ** should not have a NULL value stored in 'x'. If column 'x' is
136040 ** the first one after the nEq equality constraints in the index,
136041 ** this requires some special handling.
136042 */
136043 assert( pWInfo->pOrderBy==0
136044 || pWInfo->pOrderBy->nExpr==1
136045 || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
136046 if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
136047 && pWInfo->nOBSat>0
136048 && (pIdx->nKeyCol>nEq)
136049 ){
136050 assert( pLoop->nSkip==0 );
136051 bSeekPastNull = 1;
136052 nExtraReg = 1;
136053 }
136054
136055 /* Find any inequality constraint terms for the start and end
136056 ** of the range.
136057 */
136058 j = nEq;
136059 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
136060 pRangeStart = pLoop->aLTerm[j++];
136061 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
136062 /* Like optimization range constraints always occur in pairs */
136063 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
136064 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
136065 }
136066 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
136067 pRangeEnd = pLoop->aLTerm[j++];
136068 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
136069#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
136070 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
136071 assert( pRangeStart!=0 ); /* LIKE opt constraints */
136072 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
136073 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
136074 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
136075 VdbeComment((v, "LIKE loop counter"));
136076 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
136077 /* iLikeRepCntr actually stores 2x the counter register number. The
136078 ** bottom bit indicates whether the search order is ASC or DESC. */
136079 testcase( bRev );
136080 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
136081 assert( (bRev & ~1)==0 );
136082 pLevel->iLikeRepCntr <<=1;
136083 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
136084 }
136085#endif
136086 if( pRangeStart==0 ){
136087 j = pIdx->aiColumn[nEq];
136088 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
136089 bSeekPastNull = 1;
136090 }
136091 }
136092 }
136093 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
136094
136095 /* If we are doing a reverse order scan on an ascending index, or
136096 ** a forward order scan on a descending index, interchange the
136097 ** start and end terms (pRangeStart and pRangeEnd).
136098 */
136099 if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
136100 || (bRev && pIdx->nKeyCol==nEq)
136101 ){
136102 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
136103 SWAP(u8, bSeekPastNull, bStopAtNull);
136104 SWAP(u8, nBtm, nTop);
136105 }
136106
136107 /* Generate code to evaluate all constraint terms using == or IN
136108 ** and store the values of those terms in an array of registers
136109 ** starting at regBase.
136110 */
136111 codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd);
136112 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
136113 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
136114 if( zStartAff && nTop ){
136115 zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]);
136116 }
136117 addrNxt = pLevel->addrNxt;
136118
136119 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
136120 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
136121 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
136122 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
136123 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
136124 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
136125 start_constraints = pRangeStart || nEq>0;
136126
136127 /* Seek the index cursor to the start of the range. */
136128 nConstraint = nEq;
136129 if( pRangeStart ){
136130 Expr *pRight = pRangeStart->pExpr->pRight;
136131 codeExprOrVector(pParse, pRight, regBase+nEq, nBtm);
136132 whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
136133 if( (pRangeStart->wtFlags & TERM_VNULL)==0
136134 && sqlite3ExprCanBeNull(pRight)
136135 ){
136136 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
136137 VdbeCoverage(v);
136138 }
136139 if( zStartAff ){
136140 updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]);
136141 }
136142 nConstraint += nBtm;
136143 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
136144 if( sqlite3ExprIsVector(pRight)==0 ){
136145 disableTerm(pLevel, pRangeStart);
136146 }else{
136147 startEq = 1;
136148 }
136149 bSeekPastNull = 0;
136150 }else if( bSeekPastNull ){
136151 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
136152 nConstraint++;
136153 startEq = 0;
136154 start_constraints = 1;
136155 }
136156 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
136157 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
136158 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
136159 ** above has already left the cursor sitting on the correct row,
136160 ** so no further seeking is needed */
136161 }else{
136162 if( pLoop->wsFlags & WHERE_IN_EARLYOUT ){
136163 sqlite3VdbeAddOp1(v, OP_SeekHit, iIdxCur);
136164 }
136165 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
136166 assert( op!=0 );
136167 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
136168 VdbeCoverage(v);
136169 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
136170 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
136171 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
136172 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
136173 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
136174 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
136175 }
136176
136177 /* Load the value for the inequality constraint at the end of the
136178 ** range (if any).
136179 */
136180 nConstraint = nEq;
136181 if( pRangeEnd ){
136182 Expr *pRight = pRangeEnd->pExpr->pRight;
136183 codeExprOrVector(pParse, pRight, regBase+nEq, nTop);
136184 whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
136185 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
136186 && sqlite3ExprCanBeNull(pRight)
136187 ){
136188 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
136189 VdbeCoverage(v);
136190 }
136191 if( zEndAff ){
136192 updateRangeAffinityStr(pRight, nTop, zEndAff);
136193 codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff);
136194 }else{
136195 assert( pParse->db->mallocFailed );
136196 }
136197 nConstraint += nTop;
136198 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
136199
136200 if( sqlite3ExprIsVector(pRight)==0 ){
136201 disableTerm(pLevel, pRangeEnd);
136202 }else{
136203 endEq = 1;
136204 }
136205 }else if( bStopAtNull ){
136206 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
136207 endEq = 0;
136208 nConstraint++;
136209 }
136210 sqlite3DbFree(db, zStartAff);
136211 sqlite3DbFree(db, zEndAff);
136212
136213 /* Top of the loop body */
136214 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
136215
136216 /* Check if the index cursor is past the end of the range. */
136217 if( nConstraint ){
136218 op = aEndOp[bRev*2 + endEq];
136219 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
136220 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
136221 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
136222 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
136223 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
136224 }
136225
136226 if( pLoop->wsFlags & WHERE_IN_EARLYOUT ){
136227 sqlite3VdbeAddOp2(v, OP_SeekHit, iIdxCur, 1);
136228 }
136229
136230 /* Seek the table cursor, if required */
136231 if( omitTable ){
136232 /* pIdx is a covering index. No need to access the main table. */
136233 }else if( HasRowid(pIdx->pTable) ){
136234 if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || (
136235 (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE)
136236 && (pWInfo->eOnePass==ONEPASS_SINGLE)
136237 )){
136238 iRowidReg = ++pParse->nMem;
136239 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
136240 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
136241 VdbeCoverage(v);
136242 }else{
136243 codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
136244 }
136245 }else if( iCur!=iIdxCur ){
136246 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
136247 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
136248 for(j=0; j<pPk->nKeyCol; j++){
136249 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
136250 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
136251 }
136252 sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
136253 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
136254 }
136255
136256 /* If pIdx is an index on one or more expressions, then look through
136257 ** all the expressions in pWInfo and try to transform matching expressions
136258 ** into reference to index columns.
136259 **
136260 ** Do not do this for the RHS of a LEFT JOIN. This is because the
136261 ** expression may be evaluated after OP_NullRow has been executed on
136262 ** the cursor. In this case it is important to do the full evaluation,
136263 ** as the result of the expression may not be NULL, even if all table
136264 ** column values are. https://www.sqlite.org/src/info/7fa8049685b50b5a
136265 */
136266 if( pLevel->iLeftJoin==0 ){
136267 whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
136268 }
136269
136270 /* Record the instruction used to terminate the loop. */
136271 if( pLoop->wsFlags & WHERE_ONEROW ){
136272 pLevel->op = OP_Noop;
136273 }else if( bRev ){
136274 pLevel->op = OP_Prev;
136275 }else{
136276 pLevel->op = OP_Next;
136277 }
136278 pLevel->p1 = iIdxCur;
136279 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
136280 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
136281 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
136282 }else{
136283 assert( pLevel->p5==0 );
136284 }
136285 if( omitTable ) pIdx = 0;
136286 }else
136287
136288#ifndef SQLITE_OMIT_OR_OPTIMIZATION
136289 if( pLoop->wsFlags & WHERE_MULTI_OR ){
136290 /* Case 5: Two or more separately indexed terms connected by OR
136291 **
136292 ** Example:
136293 **
136294 ** CREATE TABLE t1(a,b,c,d);
136295 ** CREATE INDEX i1 ON t1(a);
136296 ** CREATE INDEX i2 ON t1(b);
136297 ** CREATE INDEX i3 ON t1(c);
136298 **
136299 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
136300 **
136301 ** In the example, there are three indexed terms connected by OR.
136302 ** The top of the loop looks like this:
136303 **
136304 ** Null 1 # Zero the rowset in reg 1
136305 **
136306 ** Then, for each indexed term, the following. The arguments to
136307 ** RowSetTest are such that the rowid of the current row is inserted
136308 ** into the RowSet. If it is already present, control skips the
136309 ** Gosub opcode and jumps straight to the code generated by WhereEnd().
136310 **
136311 ** sqlite3WhereBegin(<term>)
136312 ** RowSetTest # Insert rowid into rowset
136313 ** Gosub 2 A
136314 ** sqlite3WhereEnd()
136315 **
136316 ** Following the above, code to terminate the loop. Label A, the target
136317 ** of the Gosub above, jumps to the instruction right after the Goto.
136318 **
136319 ** Null 1 # Zero the rowset in reg 1
136320 ** Goto B # The loop is finished.
136321 **
136322 ** A: <loop body> # Return data, whatever.
136323 **
136324 ** Return 2 # Jump back to the Gosub
136325 **
136326 ** B: <after the loop>
136327 **
136328 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
136329 ** use an ephemeral index instead of a RowSet to record the primary
136330 ** keys of the rows we have already seen.
136331 **
136332 */
136333 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
136334 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
136335 Index *pCov = 0; /* Potential covering index (or NULL) */
136336 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
136337
136338 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
136339 int regRowset = 0; /* Register for RowSet object */
136340 int regRowid = 0; /* Register holding rowid */
136341 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
136342 int iRetInit; /* Address of regReturn init */
136343 int untestedTerms = 0; /* Some terms not completely tested */
136344 int ii; /* Loop counter */
136345 u16 wctrlFlags; /* Flags for sub-WHERE clause */
136346 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
136347 Table *pTab = pTabItem->pTab;
136348
136349 pTerm = pLoop->aLTerm[0];
136350 assert( pTerm!=0 );
136351 assert( pTerm->eOperator & WO_OR );
136352 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
136353 pOrWc = &pTerm->u.pOrInfo->wc;
136354 pLevel->op = OP_Return;
136355 pLevel->p1 = regReturn;
136356
136357 /* Set up a new SrcList in pOrTab containing the table being scanned
136358 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
136359 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
136360 */
136361 if( pWInfo->nLevel>1 ){
136362 int nNotReady; /* The number of notReady tables */
136363 struct SrcList_item *origSrc; /* Original list of tables */
136364 nNotReady = pWInfo->nLevel - iLevel - 1;
136365 pOrTab = sqlite3StackAllocRaw(db,
136366 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
136367 if( pOrTab==0 ) return notReady;
136368 pOrTab->nAlloc = (u8)(nNotReady + 1);
136369 pOrTab->nSrc = pOrTab->nAlloc;
136370 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
136371 origSrc = pWInfo->pTabList->a;
136372 for(k=1; k<=nNotReady; k++){
136373 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
136374 }
136375 }else{
136376 pOrTab = pWInfo->pTabList;
136377 }
136378
136379 /* Initialize the rowset register to contain NULL. An SQL NULL is
136380 ** equivalent to an empty rowset. Or, create an ephemeral index
136381 ** capable of holding primary keys in the case of a WITHOUT ROWID.
136382 **
136383 ** Also initialize regReturn to contain the address of the instruction
136384 ** immediately following the OP_Return at the bottom of the loop. This
136385 ** is required in a few obscure LEFT JOIN cases where control jumps
136386 ** over the top of the loop into the body of it. In this case the
136387 ** correct response for the end-of-loop code (the OP_Return) is to
136388 ** fall through to the next instruction, just as an OP_Next does if
136389 ** called on an uninitialized cursor.
136390 */
136391 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
136392 if( HasRowid(pTab) ){
136393 regRowset = ++pParse->nMem;
136394 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
136395 }else{
136396 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
136397 regRowset = pParse->nTab++;
136398 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
136399 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
136400 }
136401 regRowid = ++pParse->nMem;
136402 }
136403 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
136404
136405 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
136406 ** Then for every term xN, evaluate as the subexpression: xN AND z
136407 ** That way, terms in y that are factored into the disjunction will
136408 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
136409 **
136410 ** Actually, each subexpression is converted to "xN AND w" where w is
136411 ** the "interesting" terms of z - terms that did not originate in the
136412 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
136413 ** indices.
136414 **
136415 ** This optimization also only applies if the (x1 OR x2 OR ...) term
136416 ** is not contained in the ON clause of a LEFT JOIN.
136417 ** See ticket http://www.sqlite.org/src/info/f2369304e4
136418 */
136419 if( pWC->nTerm>1 ){
136420 int iTerm;
136421 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
136422 Expr *pExpr = pWC->a[iTerm].pExpr;
136423 if( &pWC->a[iTerm] == pTerm ) continue;
136424 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
136425 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
136426 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
136427 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
136428 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
136429 pExpr = sqlite3ExprDup(db, pExpr, 0);
136430 pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
136431 }
136432 if( pAndExpr ){
136433 pAndExpr = sqlite3PExpr(pParse, TK_AND|TKFLG_DONTFOLD, 0, pAndExpr);
136434 }
136435 }
136436
136437 /* Run a separate WHERE clause for each term of the OR clause. After
136438 ** eliminating duplicates from other WHERE clauses, the action for each
136439 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
136440 */
136441 wctrlFlags = WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE);
136442 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
136443 for(ii=0; ii<pOrWc->nTerm; ii++){
136444 WhereTerm *pOrTerm = &pOrWc->a[ii];
136445 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
136446 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
136447 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
136448 int jmp1 = 0; /* Address of jump operation */
136449 assert( (pTabItem[0].fg.jointype & JT_LEFT)==0
136450 || ExprHasProperty(pOrExpr, EP_FromJoin)
136451 );
136452 if( pAndExpr ){
136453 pAndExpr->pLeft = pOrExpr;
136454 pOrExpr = pAndExpr;
136455 }
136456 /* Loop through table entries that match term pOrTerm. */
136457 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
136458 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
136459 wctrlFlags, iCovCur);
136460 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
136461 if( pSubWInfo ){
136462 WhereLoop *pSubLoop;
136463 int addrExplain = sqlite3WhereExplainOneScan(
136464 pParse, pOrTab, &pSubWInfo->a[0], 0
136465 );
136466 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
136467
136468 /* This is the sub-WHERE clause body. First skip over
136469 ** duplicate rows from prior sub-WHERE clauses, and record the
136470 ** rowid (or PRIMARY KEY) for the current row so that the same
136471 ** row will be skipped in subsequent sub-WHERE clauses.
136472 */
136473 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
136474 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
136475 if( HasRowid(pTab) ){
136476 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
136477 jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0,
136478 regRowid, iSet);
136479 VdbeCoverage(v);
136480 }else{
136481 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
136482 int nPk = pPk->nKeyCol;
136483 int iPk;
136484 int r;
136485
136486 /* Read the PK into an array of temp registers. */
136487 r = sqlite3GetTempRange(pParse, nPk);
136488 for(iPk=0; iPk<nPk; iPk++){
136489 int iCol = pPk->aiColumn[iPk];
136490 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, r+iPk);
136491 }
136492
136493 /* Check if the temp table already contains this key. If so,
136494 ** the row has already been included in the result set and
136495 ** can be ignored (by jumping past the Gosub below). Otherwise,
136496 ** insert the key into the temp table and proceed with processing
136497 ** the row.
136498 **
136499 ** Use some of the same optimizations as OP_RowSetTest: If iSet
136500 ** is zero, assume that the key cannot already be present in
136501 ** the temp table. And if iSet is -1, assume that there is no
136502 ** need to insert the key into the temp table, as it will never
136503 ** be tested for. */
136504 if( iSet ){
136505 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
136506 VdbeCoverage(v);
136507 }
136508 if( iSet>=0 ){
136509 sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
136510 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, regRowset, regRowid,
136511 r, nPk);
136512 if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
136513 }
136514
136515 /* Release the array of temp registers */
136516 sqlite3ReleaseTempRange(pParse, r, nPk);
136517 }
136518 }
136519
136520 /* Invoke the main loop body as a subroutine */
136521 sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
136522
136523 /* Jump here (skipping the main loop body subroutine) if the
136524 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
136525 if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1);
136526
136527 /* The pSubWInfo->untestedTerms flag means that this OR term
136528 ** contained one or more AND term from a notReady table. The
136529 ** terms from the notReady table could not be tested and will
136530 ** need to be tested later.
136531 */
136532 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
136533
136534 /* If all of the OR-connected terms are optimized using the same
136535 ** index, and the index is opened using the same cursor number
136536 ** by each call to sqlite3WhereBegin() made by this loop, it may
136537 ** be possible to use that index as a covering index.
136538 **
136539 ** If the call to sqlite3WhereBegin() above resulted in a scan that
136540 ** uses an index, and this is either the first OR-connected term
136541 ** processed or the index is the same as that used by all previous
136542 ** terms, set pCov to the candidate covering index. Otherwise, set
136543 ** pCov to NULL to indicate that no candidate covering index will
136544 ** be available.
136545 */
136546 pSubLoop = pSubWInfo->a[0].pWLoop;
136547 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
136548 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
136549 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
136550 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
136551 ){
136552 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
136553 pCov = pSubLoop->u.btree.pIndex;
136554 }else{
136555 pCov = 0;
136556 }
136557
136558 /* Finish the loop through table entries that match term pOrTerm. */
136559 sqlite3WhereEnd(pSubWInfo);
136560 }
136561 }
136562 }
136563 ExplainQueryPlanPop(pParse);
136564 pLevel->u.pCovidx = pCov;
136565 if( pCov ) pLevel->iIdxCur = iCovCur;
136566 if( pAndExpr ){
136567 pAndExpr->pLeft = 0;
136568 sqlite3ExprDelete(db, pAndExpr);
136569 }
136570 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
136571 sqlite3VdbeGoto(v, pLevel->addrBrk);
136572 sqlite3VdbeResolveLabel(v, iLoopBody);
136573
136574 if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
136575 if( !untestedTerms ) disableTerm(pLevel, pTerm);
136576 }else
136577#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
136578
136579 {
136580 /* Case 6: There is no usable index. We must do a complete
136581 ** scan of the entire table.
136582 */
136583 static const u8 aStep[] = { OP_Next, OP_Prev };
136584 static const u8 aStart[] = { OP_Rewind, OP_Last };
136585 assert( bRev==0 || bRev==1 );
136586 if( pTabItem->fg.isRecursive ){
136587 /* Tables marked isRecursive have only a single row that is stored in
136588 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
136589 pLevel->op = OP_Noop;
136590 }else{
136591 codeCursorHint(pTabItem, pWInfo, pLevel, 0);
136592 pLevel->op = aStep[bRev];
136593 pLevel->p1 = iCur;
136594 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
136595 VdbeCoverageIf(v, bRev==0);
136596 VdbeCoverageIf(v, bRev!=0);
136597 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
136598 }
136599 }
136600
136601#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
136602 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
136603#endif
136604
136605 /* Insert code to test every subexpression that can be completely
136606 ** computed using the current set of tables.
136607 **
136608 ** This loop may run between one and three times, depending on the
136609 ** constraints to be generated. The value of stack variable iLoop
136610 ** determines the constraints coded by each iteration, as follows:
136611 **
136612 ** iLoop==1: Code only expressions that are entirely covered by pIdx.
136613 ** iLoop==2: Code remaining expressions that do not contain correlated
136614 ** sub-queries.
136615 ** iLoop==3: Code all remaining expressions.
136616 **
136617 ** An effort is made to skip unnecessary iterations of the loop.
136618 */
136619 iLoop = (pIdx ? 1 : 2);
136620 do{
136621 int iNext = 0; /* Next value for iLoop */
136622 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
136623 Expr *pE;
136624 int skipLikeAddr = 0;
136625 testcase( pTerm->wtFlags & TERM_VIRTUAL );
136626 testcase( pTerm->wtFlags & TERM_CODED );
136627 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
136628 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
136629 testcase( pWInfo->untestedTerms==0
136630 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
136631 pWInfo->untestedTerms = 1;
136632 continue;
136633 }
136634 pE = pTerm->pExpr;
136635 assert( pE!=0 );
136636 if( (pTabItem->fg.jointype&JT_LEFT) && !ExprHasProperty(pE,EP_FromJoin) ){
136637 continue;
136638 }
136639
136640 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
136641 iNext = 2;
136642 continue;
136643 }
136644 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
136645 if( iNext==0 ) iNext = 3;
136646 continue;
136647 }
136648
136649 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
136650 /* If the TERM_LIKECOND flag is set, that means that the range search
136651 ** is sufficient to guarantee that the LIKE operator is true, so we
136652 ** can skip the call to the like(A,B) function. But this only works
136653 ** for strings. So do not skip the call to the function on the pass
136654 ** that compares BLOBs. */
136655#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
136656 continue;
136657#else
136658 u32 x = pLevel->iLikeRepCntr;
136659 if( x>0 ){
136660 skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1));
136661 }
136662 VdbeCoverage(v);
136663#endif
136664 }
136665#ifdef WHERETRACE_ENABLED /* 0xffff */
136666 if( sqlite3WhereTrace ){
136667 VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
136668 pWC->nTerm-j, pTerm, iLoop));
136669 }
136670#endif
136671 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
136672 if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
136673 pTerm->wtFlags |= TERM_CODED;
136674 }
136675 iLoop = iNext;
136676 }while( iLoop>0 );
136677
136678 /* Insert code to test for implied constraints based on transitivity
136679 ** of the "==" operator.
136680 **
136681 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
136682 ** and we are coding the t1 loop and the t2 loop has not yet coded,
136683 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
136684 ** the implied "t1.a=123" constraint.
136685 */
136686 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
136687 Expr *pE, sEAlt;
136688 WhereTerm *pAlt;
136689 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
136690 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
136691 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
136692 if( pTerm->leftCursor!=iCur ) continue;
136693 if( pLevel->iLeftJoin ) continue;
136694 pE = pTerm->pExpr;
136695 assert( !ExprHasProperty(pE, EP_FromJoin) );
136696 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
136697 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
136698 WO_EQ|WO_IN|WO_IS, 0);
136699 if( pAlt==0 ) continue;
136700 if( pAlt->wtFlags & (TERM_CODED) ) continue;
136701 if( (pAlt->eOperator & WO_IN)
136702 && (pAlt->pExpr->flags & EP_xIsSelect)
136703 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
136704 ){
136705 continue;
136706 }
136707 testcase( pAlt->eOperator & WO_EQ );
136708 testcase( pAlt->eOperator & WO_IS );
136709 testcase( pAlt->eOperator & WO_IN );
136710 VdbeModuleComment((v, "begin transitive constraint"));
136711 sEAlt = *pAlt->pExpr;
136712 sEAlt.pLeft = pE->pLeft;
136713 sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL);
136714 }
136715
136716 /* For a LEFT OUTER JOIN, generate code that will record the fact that
136717 ** at least one row of the right table has matched the left table.
136718 */
136719 if( pLevel->iLeftJoin ){
136720 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
136721 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
136722 VdbeComment((v, "record LEFT JOIN hit"));
136723 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
136724 testcase( pTerm->wtFlags & TERM_VIRTUAL );
136725 testcase( pTerm->wtFlags & TERM_CODED );
136726 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
136727 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
136728 assert( pWInfo->untestedTerms );
136729 continue;
136730 }
136731 assert( pTerm->pExpr );
136732 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
136733 pTerm->wtFlags |= TERM_CODED;
136734 }
136735 }
136736
136737 return pLevel->notReady;
136738}
136739
136740/************** End of wherecode.c *******************************************/
136741/************** Begin file whereexpr.c ***************************************/
136742/*
136743** 2015-06-08
136744**
136745** The author disclaims copyright to this source code. In place of
136746** a legal notice, here is a blessing:
136747**
136748** May you do good and not evil.
136749** May you find forgiveness for yourself and forgive others.
136750** May you share freely, never taking more than you give.
136751**
136752*************************************************************************
136753** This module contains C code that generates VDBE code used to process
136754** the WHERE clause of SQL statements.
136755**
136756** This file was originally part of where.c but was split out to improve
136757** readability and editabiliity. This file contains utility routines for
136758** analyzing Expr objects in the WHERE clause.
136759*/
136760/* #include "sqliteInt.h" */
136761/* #include "whereInt.h" */
136762
136763/* Forward declarations */
136764static void exprAnalyze(SrcList*, WhereClause*, int);
136765
136766/*
136767** Deallocate all memory associated with a WhereOrInfo object.
136768*/
136769static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
136770 sqlite3WhereClauseClear(&p->wc);
136771 sqlite3DbFree(db, p);
136772}
136773
136774/*
136775** Deallocate all memory associated with a WhereAndInfo object.
136776*/
136777static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
136778 sqlite3WhereClauseClear(&p->wc);
136779 sqlite3DbFree(db, p);
136780}
136781
136782/*
136783** Add a single new WhereTerm entry to the WhereClause object pWC.
136784** The new WhereTerm object is constructed from Expr p and with wtFlags.
136785** The index in pWC->a[] of the new WhereTerm is returned on success.
136786** 0 is returned if the new WhereTerm could not be added due to a memory
136787** allocation error. The memory allocation failure will be recorded in
136788** the db->mallocFailed flag so that higher-level functions can detect it.
136789**
136790** This routine will increase the size of the pWC->a[] array as necessary.
136791**
136792** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
136793** for freeing the expression p is assumed by the WhereClause object pWC.
136794** This is true even if this routine fails to allocate a new WhereTerm.
136795**
136796** WARNING: This routine might reallocate the space used to store
136797** WhereTerms. All pointers to WhereTerms should be invalidated after
136798** calling this routine. Such pointers may be reinitialized by referencing
136799** the pWC->a[] array.
136800*/
136801static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
136802 WhereTerm *pTerm;
136803 int idx;
136804 testcase( wtFlags & TERM_VIRTUAL );
136805 if( pWC->nTerm>=pWC->nSlot ){
136806 WhereTerm *pOld = pWC->a;
136807 sqlite3 *db = pWC->pWInfo->pParse->db;
136808 pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
136809 if( pWC->a==0 ){
136810 if( wtFlags & TERM_DYNAMIC ){
136811 sqlite3ExprDelete(db, p);
136812 }
136813 pWC->a = pOld;
136814 return 0;
136815 }
136816 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
136817 if( pOld!=pWC->aStatic ){
136818 sqlite3DbFree(db, pOld);
136819 }
136820 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
136821 }
136822 pTerm = &pWC->a[idx = pWC->nTerm++];
136823 if( p && ExprHasProperty(p, EP_Unlikely) ){
136824 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
136825 }else{
136826 pTerm->truthProb = 1;
136827 }
136828 pTerm->pExpr = sqlite3ExprSkipCollate(p);
136829 pTerm->wtFlags = wtFlags;
136830 pTerm->pWC = pWC;
136831 pTerm->iParent = -1;
136832 memset(&pTerm->eOperator, 0,
136833 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
136834 return idx;
136835}
136836
136837/*
136838** Return TRUE if the given operator is one of the operators that is
136839** allowed for an indexable WHERE clause term. The allowed operators are
136840** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
136841*/
136842static int allowedOp(int op){
136843 assert( TK_GT>TK_EQ && TK_GT<TK_GE );
136844 assert( TK_LT>TK_EQ && TK_LT<TK_GE );
136845 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
136846 assert( TK_GE==TK_EQ+4 );
136847 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
136848}
136849
136850/*
136851** Commute a comparison operator. Expressions of the form "X op Y"
136852** are converted into "Y op X".
136853**
136854** If left/right precedence rules come into play when determining the
136855** collating sequence, then COLLATE operators are adjusted to ensure
136856** that the collating sequence does not change. For example:
136857** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
136858** the left hand side of a comparison overrides any collation sequence
136859** attached to the right. For the same reason the EP_Collate flag
136860** is not commuted.
136861*/
136862static void exprCommute(Parse *pParse, Expr *pExpr){
136863 u16 expRight = (pExpr->pRight->flags & EP_Collate);
136864 u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
136865 assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
136866 if( expRight==expLeft ){
136867 /* Either X and Y both have COLLATE operator or neither do */
136868 if( expRight ){
136869 /* Both X and Y have COLLATE operators. Make sure X is always
136870 ** used by clearing the EP_Collate flag from Y. */
136871 pExpr->pRight->flags &= ~EP_Collate;
136872 }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
136873 /* Neither X nor Y have COLLATE operators, but X has a non-default
136874 ** collating sequence. So add the EP_Collate marker on X to cause
136875 ** it to be searched first. */
136876 pExpr->pLeft->flags |= EP_Collate;
136877 }
136878 }
136879 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
136880 if( pExpr->op>=TK_GT ){
136881 assert( TK_LT==TK_GT+2 );
136882 assert( TK_GE==TK_LE+2 );
136883 assert( TK_GT>TK_EQ );
136884 assert( TK_GT<TK_LE );
136885 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
136886 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
136887 }
136888}
136889
136890/*
136891** Translate from TK_xx operator to WO_xx bitmask.
136892*/
136893static u16 operatorMask(int op){
136894 u16 c;
136895 assert( allowedOp(op) );
136896 if( op==TK_IN ){
136897 c = WO_IN;
136898 }else if( op==TK_ISNULL ){
136899 c = WO_ISNULL;
136900 }else if( op==TK_IS ){
136901 c = WO_IS;
136902 }else{
136903 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
136904 c = (u16)(WO_EQ<<(op-TK_EQ));
136905 }
136906 assert( op!=TK_ISNULL || c==WO_ISNULL );
136907 assert( op!=TK_IN || c==WO_IN );
136908 assert( op!=TK_EQ || c==WO_EQ );
136909 assert( op!=TK_LT || c==WO_LT );
136910 assert( op!=TK_LE || c==WO_LE );
136911 assert( op!=TK_GT || c==WO_GT );
136912 assert( op!=TK_GE || c==WO_GE );
136913 assert( op!=TK_IS || c==WO_IS );
136914 return c;
136915}
136916
136917
136918#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
136919/*
136920** Check to see if the given expression is a LIKE or GLOB operator that
136921** can be optimized using inequality constraints. Return TRUE if it is
136922** so and false if not.
136923**
136924** In order for the operator to be optimizible, the RHS must be a string
136925** literal that does not begin with a wildcard. The LHS must be a column
136926** that may only be NULL, a string, or a BLOB, never a number. (This means
136927** that virtual tables cannot participate in the LIKE optimization.) The
136928** collating sequence for the column on the LHS must be appropriate for
136929** the operator.
136930*/
136931static int isLikeOrGlob(
136932 Parse *pParse, /* Parsing and code generating context */
136933 Expr *pExpr, /* Test this expression */
136934 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
136935 int *pisComplete, /* True if the only wildcard is % in the last character */
136936 int *pnoCase /* True if uppercase is equivalent to lowercase */
136937){
136938 const u8 *z = 0; /* String on RHS of LIKE operator */
136939 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
136940 ExprList *pList; /* List of operands to the LIKE operator */
136941 u8 c; /* One character in z[] */
136942 int cnt; /* Number of non-wildcard prefix characters */
136943 u8 wc[4]; /* Wildcard characters */
136944 sqlite3 *db = pParse->db; /* Database connection */
136945 sqlite3_value *pVal = 0;
136946 int op; /* Opcode of pRight */
136947 int rc; /* Result code to return */
136948
136949 if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, (char*)wc) ){
136950 return 0;
136951 }
136952#ifdef SQLITE_EBCDIC
136953 if( *pnoCase ) return 0;
136954#endif
136955 pList = pExpr->x.pList;
136956 pLeft = pList->a[1].pExpr;
136957
136958 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
136959 op = pRight->op;
136960 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
136961 Vdbe *pReprepare = pParse->pReprepare;
136962 int iCol = pRight->iColumn;
136963 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
136964 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
136965 z = sqlite3_value_text(pVal);
136966 }
136967 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
136968 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
136969 }else if( op==TK_STRING ){
136970 z = (u8*)pRight->u.zToken;
136971 }
136972 if( z ){
136973
136974 /* Count the number of prefix characters prior to the first wildcard */
136975 cnt = 0;
136976 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
136977 cnt++;
136978 if( c==wc[3] && z[cnt]!=0 ) cnt++;
136979 }
136980
136981 /* The optimization is possible only if (1) the pattern does not begin
136982 ** with a wildcard and if (2) the non-wildcard prefix does not end with
136983 ** an (illegal 0xff) character, or (3) the pattern does not consist of
136984 ** a single escape character. The second condition is necessary so
136985 ** that we can increment the prefix key to find an upper bound for the
136986 ** range search. The third is because the caller assumes that the pattern
136987 ** consists of at least one character after all escapes have been
136988 ** removed. */
136989 if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
136990 Expr *pPrefix;
136991
136992 /* A "complete" match if the pattern ends with "*" or "%" */
136993 *pisComplete = c==wc[0] && z[cnt+1]==0;
136994
136995 /* Get the pattern prefix. Remove all escapes from the prefix. */
136996 pPrefix = sqlite3Expr(db, TK_STRING, (char*)z);
136997 if( pPrefix ){
136998 int iFrom, iTo;
136999 char *zNew = pPrefix->u.zToken;
137000 zNew[cnt] = 0;
137001 for(iFrom=iTo=0; iFrom<cnt; iFrom++){
137002 if( zNew[iFrom]==wc[3] ) iFrom++;
137003 zNew[iTo++] = zNew[iFrom];
137004 }
137005 zNew[iTo] = 0;
137006
137007 /* If the RHS begins with a digit or a minus sign, then the LHS must be
137008 ** an ordinary column (not a virtual table column) with TEXT affinity.
137009 ** Otherwise the LHS might be numeric and "lhs >= rhs" would be false
137010 ** even though "lhs LIKE rhs" is true. But if the RHS does not start
137011 ** with a digit or '-', then "lhs LIKE rhs" will always be false if
137012 ** the LHS is numeric and so the optimization still works.
137013 **
137014 ** 2018-09-10 ticket c94369cae9b561b1f996d0054bfab11389f9d033
137015 ** The RHS pattern must not be '/%' because the termination condition
137016 ** will then become "x<'0'" and if the affinity is numeric, will then
137017 ** be converted into "x<0", which is incorrect.
137018 */
137019 if( sqlite3Isdigit(zNew[0])
137020 || zNew[0]=='-'
137021 || (zNew[0]+1=='0' && iTo==1)
137022 ){
137023 if( pLeft->op!=TK_COLUMN
137024 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
137025 || IsVirtual(pLeft->pTab) /* Value might be numeric */
137026 ){
137027 sqlite3ExprDelete(db, pPrefix);
137028 sqlite3ValueFree(pVal);
137029 return 0;
137030 }
137031 }
137032 }
137033 *ppPrefix = pPrefix;
137034
137035 /* If the RHS pattern is a bound parameter, make arrangements to
137036 ** reprepare the statement when that parameter is rebound */
137037 if( op==TK_VARIABLE ){
137038 Vdbe *v = pParse->pVdbe;
137039 sqlite3VdbeSetVarmask(v, pRight->iColumn);
137040 if( *pisComplete && pRight->u.zToken[1] ){
137041 /* If the rhs of the LIKE expression is a variable, and the current
137042 ** value of the variable means there is no need to invoke the LIKE
137043 ** function, then no OP_Variable will be added to the program.
137044 ** This causes problems for the sqlite3_bind_parameter_name()
137045 ** API. To work around them, add a dummy OP_Variable here.
137046 */
137047 int r1 = sqlite3GetTempReg(pParse);
137048 sqlite3ExprCodeTarget(pParse, pRight, r1);
137049 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
137050 sqlite3ReleaseTempReg(pParse, r1);
137051 }
137052 }
137053 }else{
137054 z = 0;
137055 }
137056 }
137057
137058 rc = (z!=0);
137059 sqlite3ValueFree(pVal);
137060 return rc;
137061}
137062#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
137063
137064
137065#ifndef SQLITE_OMIT_VIRTUALTABLE
137066/*
137067** Check to see if the pExpr expression is a form that needs to be passed
137068** to the xBestIndex method of virtual tables. Forms of interest include:
137069**
137070** Expression Virtual Table Operator
137071** ----------------------- ---------------------------------
137072** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH
137073** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB
137074** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE
137075** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP
137076** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE
137077** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE
137078** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT
137079** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT
137080** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL
137081**
137082** In every case, "column" must be a column of a virtual table. If there
137083** is a match, set *ppLeft to the "column" expression, set *ppRight to the
137084** "expr" expression (even though in forms (6) and (8) the column is on the
137085** right and the expression is on the left). Also set *peOp2 to the
137086** appropriate virtual table operator. The return value is 1 or 2 if there
137087** is a match. The usual return is 1, but if the RHS is also a column
137088** of virtual table in forms (5) or (7) then return 2.
137089**
137090** If the expression matches none of the patterns above, return 0.
137091*/
137092static int isAuxiliaryVtabOperator(
137093 sqlite3 *db, /* Parsing context */
137094 Expr *pExpr, /* Test this expression */
137095 unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
137096 Expr **ppLeft, /* Column expression to left of MATCH/op2 */
137097 Expr **ppRight /* Expression to left of MATCH/op2 */
137098){
137099 if( pExpr->op==TK_FUNCTION ){
137100 static const struct Op2 {
137101 const char *zOp;
137102 unsigned char eOp2;
137103 } aOp[] = {
137104 { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
137105 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
137106 { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
137107 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
137108 };
137109 ExprList *pList;
137110 Expr *pCol; /* Column reference */
137111 int i;
137112
137113 pList = pExpr->x.pList;
137114 if( pList==0 || pList->nExpr!=2 ){
137115 return 0;
137116 }
137117
137118 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
137119 ** virtual table on their second argument, which is the same as
137120 ** the left-hand side operand in their in-fix form.
137121 **
137122 ** vtab_column MATCH expression
137123 ** MATCH(expression,vtab_column)
137124 */
137125 pCol = pList->a[1].pExpr;
137126 if( pCol->op==TK_COLUMN && IsVirtual(pCol->pTab) ){
137127 for(i=0; i<ArraySize(aOp); i++){
137128 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
137129 *peOp2 = aOp[i].eOp2;
137130 *ppRight = pList->a[0].pExpr;
137131 *ppLeft = pCol;
137132 return 1;
137133 }
137134 }
137135 }
137136
137137 /* We can also match against the first column of overloaded
137138 ** functions where xFindFunction returns a value of at least
137139 ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
137140 **
137141 ** OVERLOADED(vtab_column,expression)
137142 **
137143 ** Historically, xFindFunction expected to see lower-case function
137144 ** names. But for this use case, xFindFunction is expected to deal
137145 ** with function names in an arbitrary case.
137146 */
137147 pCol = pList->a[0].pExpr;
137148 if( pCol->op==TK_COLUMN && IsVirtual(pCol->pTab) ){
137149 sqlite3_vtab *pVtab;
137150 sqlite3_module *pMod;
137151 void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
137152 void *pNotUsed;
137153 pVtab = sqlite3GetVTable(db, pCol->pTab)->pVtab;
137154 assert( pVtab!=0 );
137155 assert( pVtab->pModule!=0 );
137156 pMod = (sqlite3_module *)pVtab->pModule;
137157 if( pMod->xFindFunction!=0 ){
137158 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
137159 if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
137160 *peOp2 = i;
137161 *ppRight = pList->a[1].pExpr;
137162 *ppLeft = pCol;
137163 return 1;
137164 }
137165 }
137166 }
137167 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
137168 int res = 0;
137169 Expr *pLeft = pExpr->pLeft;
137170 Expr *pRight = pExpr->pRight;
137171 if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->pTab) ){
137172 res++;
137173 }
137174 if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->pTab) ){
137175 res++;
137176 SWAP(Expr*, pLeft, pRight);
137177 }
137178 *ppLeft = pLeft;
137179 *ppRight = pRight;
137180 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
137181 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
137182 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
137183 return res;
137184 }
137185 return 0;
137186}
137187#endif /* SQLITE_OMIT_VIRTUALTABLE */
137188
137189/*
137190** If the pBase expression originated in the ON or USING clause of
137191** a join, then transfer the appropriate markings over to derived.
137192*/
137193static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
137194 if( pDerived ){
137195 pDerived->flags |= pBase->flags & EP_FromJoin;
137196 pDerived->iRightJoinTable = pBase->iRightJoinTable;
137197 }
137198}
137199
137200/*
137201** Mark term iChild as being a child of term iParent
137202*/
137203static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
137204 pWC->a[iChild].iParent = iParent;
137205 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
137206 pWC->a[iParent].nChild++;
137207}
137208
137209/*
137210** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
137211** a conjunction, then return just pTerm when N==0. If N is exceeds
137212** the number of available subterms, return NULL.
137213*/
137214static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
137215 if( pTerm->eOperator!=WO_AND ){
137216 return N==0 ? pTerm : 0;
137217 }
137218 if( N<pTerm->u.pAndInfo->wc.nTerm ){
137219 return &pTerm->u.pAndInfo->wc.a[N];
137220 }
137221 return 0;
137222}
137223
137224/*
137225** Subterms pOne and pTwo are contained within WHERE clause pWC. The
137226** two subterms are in disjunction - they are OR-ed together.
137227**
137228** If these two terms are both of the form: "A op B" with the same
137229** A and B values but different operators and if the operators are
137230** compatible (if one is = and the other is <, for example) then
137231** add a new virtual AND term to pWC that is the combination of the
137232** two.
137233**
137234** Some examples:
137235**
137236** x<y OR x=y --> x<=y
137237** x=y OR x=y --> x=y
137238** x<=y OR x<y --> x<=y
137239**
137240** The following is NOT generated:
137241**
137242** x<y OR x>y --> x!=y
137243*/
137244static void whereCombineDisjuncts(
137245 SrcList *pSrc, /* the FROM clause */
137246 WhereClause *pWC, /* The complete WHERE clause */
137247 WhereTerm *pOne, /* First disjunct */
137248 WhereTerm *pTwo /* Second disjunct */
137249){
137250 u16 eOp = pOne->eOperator | pTwo->eOperator;
137251 sqlite3 *db; /* Database connection (for malloc) */
137252 Expr *pNew; /* New virtual expression */
137253 int op; /* Operator for the combined expression */
137254 int idxNew; /* Index in pWC of the next virtual term */
137255
137256 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
137257 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
137258 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
137259 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
137260 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
137261 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
137262 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
137263 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
137264 /* If we reach this point, it means the two subterms can be combined */
137265 if( (eOp & (eOp-1))!=0 ){
137266 if( eOp & (WO_LT|WO_LE) ){
137267 eOp = WO_LE;
137268 }else{
137269 assert( eOp & (WO_GT|WO_GE) );
137270 eOp = WO_GE;
137271 }
137272 }
137273 db = pWC->pWInfo->pParse->db;
137274 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
137275 if( pNew==0 ) return;
137276 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
137277 pNew->op = op;
137278 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
137279 exprAnalyze(pSrc, pWC, idxNew);
137280}
137281
137282#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
137283/*
137284** Analyze a term that consists of two or more OR-connected
137285** subterms. So in:
137286**
137287** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
137288** ^^^^^^^^^^^^^^^^^^^^
137289**
137290** This routine analyzes terms such as the middle term in the above example.
137291** A WhereOrTerm object is computed and attached to the term under
137292** analysis, regardless of the outcome of the analysis. Hence:
137293**
137294** WhereTerm.wtFlags |= TERM_ORINFO
137295** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
137296**
137297** The term being analyzed must have two or more of OR-connected subterms.
137298** A single subterm might be a set of AND-connected sub-subterms.
137299** Examples of terms under analysis:
137300**
137301** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
137302** (B) x=expr1 OR expr2=x OR x=expr3
137303** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
137304** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
137305** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
137306** (F) x>A OR (x=A AND y>=B)
137307**
137308** CASE 1:
137309**
137310** If all subterms are of the form T.C=expr for some single column of C and
137311** a single table T (as shown in example B above) then create a new virtual
137312** term that is an equivalent IN expression. In other words, if the term
137313** being analyzed is:
137314**
137315** x = expr1 OR expr2 = x OR x = expr3
137316**
137317** then create a new virtual term like this:
137318**
137319** x IN (expr1,expr2,expr3)
137320**
137321** CASE 2:
137322**
137323** If there are exactly two disjuncts and one side has x>A and the other side
137324** has x=A (for the same x and A) then add a new virtual conjunct term to the
137325** WHERE clause of the form "x>=A". Example:
137326**
137327** x>A OR (x=A AND y>B) adds: x>=A
137328**
137329** The added conjunct can sometimes be helpful in query planning.
137330**
137331** CASE 3:
137332**
137333** If all subterms are indexable by a single table T, then set
137334**
137335** WhereTerm.eOperator = WO_OR
137336** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
137337**
137338** A subterm is "indexable" if it is of the form
137339** "T.C <op> <expr>" where C is any column of table T and
137340** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
137341** A subterm is also indexable if it is an AND of two or more
137342** subsubterms at least one of which is indexable. Indexable AND
137343** subterms have their eOperator set to WO_AND and they have
137344** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
137345**
137346** From another point of view, "indexable" means that the subterm could
137347** potentially be used with an index if an appropriate index exists.
137348** This analysis does not consider whether or not the index exists; that
137349** is decided elsewhere. This analysis only looks at whether subterms
137350** appropriate for indexing exist.
137351**
137352** All examples A through E above satisfy case 3. But if a term
137353** also satisfies case 1 (such as B) we know that the optimizer will
137354** always prefer case 1, so in that case we pretend that case 3 is not
137355** satisfied.
137356**
137357** It might be the case that multiple tables are indexable. For example,
137358** (E) above is indexable on tables P, Q, and R.
137359**
137360** Terms that satisfy case 3 are candidates for lookup by using
137361** separate indices to find rowids for each subterm and composing
137362** the union of all rowids using a RowSet object. This is similar
137363** to "bitmap indices" in other database engines.
137364**
137365** OTHERWISE:
137366**
137367** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
137368** zero. This term is not useful for search.
137369*/
137370static void exprAnalyzeOrTerm(
137371 SrcList *pSrc, /* the FROM clause */
137372 WhereClause *pWC, /* the complete WHERE clause */
137373 int idxTerm /* Index of the OR-term to be analyzed */
137374){
137375 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
137376 Parse *pParse = pWInfo->pParse; /* Parser context */
137377 sqlite3 *db = pParse->db; /* Database connection */
137378 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
137379 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
137380 int i; /* Loop counters */
137381 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
137382 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
137383 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
137384 Bitmask chngToIN; /* Tables that might satisfy case 1 */
137385 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
137386
137387 /*
137388 ** Break the OR clause into its separate subterms. The subterms are
137389 ** stored in a WhereClause structure containing within the WhereOrInfo
137390 ** object that is attached to the original OR clause term.
137391 */
137392 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
137393 assert( pExpr->op==TK_OR );
137394 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
137395 if( pOrInfo==0 ) return;
137396 pTerm->wtFlags |= TERM_ORINFO;
137397 pOrWc = &pOrInfo->wc;
137398 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
137399 sqlite3WhereClauseInit(pOrWc, pWInfo);
137400 sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
137401 sqlite3WhereExprAnalyze(pSrc, pOrWc);
137402 if( db->mallocFailed ) return;
137403 assert( pOrWc->nTerm>=2 );
137404
137405 /*
137406 ** Compute the set of tables that might satisfy cases 1 or 3.
137407 */
137408 indexable = ~(Bitmask)0;
137409 chngToIN = ~(Bitmask)0;
137410 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
137411 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
137412 WhereAndInfo *pAndInfo;
137413 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
137414 chngToIN = 0;
137415 pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
137416 if( pAndInfo ){
137417 WhereClause *pAndWC;
137418 WhereTerm *pAndTerm;
137419 int j;
137420 Bitmask b = 0;
137421 pOrTerm->u.pAndInfo = pAndInfo;
137422 pOrTerm->wtFlags |= TERM_ANDINFO;
137423 pOrTerm->eOperator = WO_AND;
137424 pAndWC = &pAndInfo->wc;
137425 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
137426 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
137427 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
137428 sqlite3WhereExprAnalyze(pSrc, pAndWC);
137429 pAndWC->pOuter = pWC;
137430 if( !db->mallocFailed ){
137431 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
137432 assert( pAndTerm->pExpr );
137433 if( allowedOp(pAndTerm->pExpr->op)
137434 || pAndTerm->eOperator==WO_AUX
137435 ){
137436 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
137437 }
137438 }
137439 }
137440 indexable &= b;
137441 }
137442 }else if( pOrTerm->wtFlags & TERM_COPIED ){
137443 /* Skip this term for now. We revisit it when we process the
137444 ** corresponding TERM_VIRTUAL term */
137445 }else{
137446 Bitmask b;
137447 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
137448 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
137449 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
137450 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
137451 }
137452 indexable &= b;
137453 if( (pOrTerm->eOperator & WO_EQ)==0 ){
137454 chngToIN = 0;
137455 }else{
137456 chngToIN &= b;
137457 }
137458 }
137459 }
137460
137461 /*
137462 ** Record the set of tables that satisfy case 3. The set might be
137463 ** empty.
137464 */
137465 pOrInfo->indexable = indexable;
137466 if( indexable ){
137467 pTerm->eOperator = WO_OR;
137468 pWC->hasOr = 1;
137469 }else{
137470 pTerm->eOperator = WO_OR;
137471 }
137472
137473 /* For a two-way OR, attempt to implementation case 2.
137474 */
137475 if( indexable && pOrWc->nTerm==2 ){
137476 int iOne = 0;
137477 WhereTerm *pOne;
137478 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
137479 int iTwo = 0;
137480 WhereTerm *pTwo;
137481 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
137482 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
137483 }
137484 }
137485 }
137486
137487 /*
137488 ** chngToIN holds a set of tables that *might* satisfy case 1. But
137489 ** we have to do some additional checking to see if case 1 really
137490 ** is satisfied.
137491 **
137492 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
137493 ** that there is no possibility of transforming the OR clause into an
137494 ** IN operator because one or more terms in the OR clause contain
137495 ** something other than == on a column in the single table. The 1-bit
137496 ** case means that every term of the OR clause is of the form
137497 ** "table.column=expr" for some single table. The one bit that is set
137498 ** will correspond to the common table. We still need to check to make
137499 ** sure the same column is used on all terms. The 2-bit case is when
137500 ** the all terms are of the form "table1.column=table2.column". It
137501 ** might be possible to form an IN operator with either table1.column
137502 ** or table2.column as the LHS if either is common to every term of
137503 ** the OR clause.
137504 **
137505 ** Note that terms of the form "table.column1=table.column2" (the
137506 ** same table on both sizes of the ==) cannot be optimized.
137507 */
137508 if( chngToIN ){
137509 int okToChngToIN = 0; /* True if the conversion to IN is valid */
137510 int iColumn = -1; /* Column index on lhs of IN operator */
137511 int iCursor = -1; /* Table cursor common to all terms */
137512 int j = 0; /* Loop counter */
137513
137514 /* Search for a table and column that appears on one side or the
137515 ** other of the == operator in every subterm. That table and column
137516 ** will be recorded in iCursor and iColumn. There might not be any
137517 ** such table and column. Set okToChngToIN if an appropriate table
137518 ** and column is found but leave okToChngToIN false if not found.
137519 */
137520 for(j=0; j<2 && !okToChngToIN; j++){
137521 pOrTerm = pOrWc->a;
137522 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
137523 assert( pOrTerm->eOperator & WO_EQ );
137524 pOrTerm->wtFlags &= ~TERM_OR_OK;
137525 if( pOrTerm->leftCursor==iCursor ){
137526 /* This is the 2-bit case and we are on the second iteration and
137527 ** current term is from the first iteration. So skip this term. */
137528 assert( j==1 );
137529 continue;
137530 }
137531 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
137532 pOrTerm->leftCursor))==0 ){
137533 /* This term must be of the form t1.a==t2.b where t2 is in the
137534 ** chngToIN set but t1 is not. This term will be either preceded
137535 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
137536 ** and use its inversion. */
137537 testcase( pOrTerm->wtFlags & TERM_COPIED );
137538 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
137539 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
137540 continue;
137541 }
137542 iColumn = pOrTerm->u.leftColumn;
137543 iCursor = pOrTerm->leftCursor;
137544 break;
137545 }
137546 if( i<0 ){
137547 /* No candidate table+column was found. This can only occur
137548 ** on the second iteration */
137549 assert( j==1 );
137550 assert( IsPowerOfTwo(chngToIN) );
137551 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
137552 break;
137553 }
137554 testcase( j==1 );
137555
137556 /* We have found a candidate table and column. Check to see if that
137557 ** table and column is common to every term in the OR clause */
137558 okToChngToIN = 1;
137559 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
137560 assert( pOrTerm->eOperator & WO_EQ );
137561 if( pOrTerm->leftCursor!=iCursor ){
137562 pOrTerm->wtFlags &= ~TERM_OR_OK;
137563 }else if( pOrTerm->u.leftColumn!=iColumn ){
137564 okToChngToIN = 0;
137565 }else{
137566 int affLeft, affRight;
137567 /* If the right-hand side is also a column, then the affinities
137568 ** of both right and left sides must be such that no type
137569 ** conversions are required on the right. (Ticket #2249)
137570 */
137571 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
137572 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
137573 if( affRight!=0 && affRight!=affLeft ){
137574 okToChngToIN = 0;
137575 }else{
137576 pOrTerm->wtFlags |= TERM_OR_OK;
137577 }
137578 }
137579 }
137580 }
137581
137582 /* At this point, okToChngToIN is true if original pTerm satisfies
137583 ** case 1. In that case, construct a new virtual term that is
137584 ** pTerm converted into an IN operator.
137585 */
137586 if( okToChngToIN ){
137587 Expr *pDup; /* A transient duplicate expression */
137588 ExprList *pList = 0; /* The RHS of the IN operator */
137589 Expr *pLeft = 0; /* The LHS of the IN operator */
137590 Expr *pNew; /* The complete IN operator */
137591
137592 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
137593 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
137594 assert( pOrTerm->eOperator & WO_EQ );
137595 assert( pOrTerm->leftCursor==iCursor );
137596 assert( pOrTerm->u.leftColumn==iColumn );
137597 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
137598 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
137599 pLeft = pOrTerm->pExpr->pLeft;
137600 }
137601 assert( pLeft!=0 );
137602 pDup = sqlite3ExprDup(db, pLeft, 0);
137603 pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0);
137604 if( pNew ){
137605 int idxNew;
137606 transferJoinMarkings(pNew, pExpr);
137607 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
137608 pNew->x.pList = pList;
137609 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
137610 testcase( idxNew==0 );
137611 exprAnalyze(pSrc, pWC, idxNew);
137612 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where used again */
137613 markTermAsChild(pWC, idxNew, idxTerm);
137614 }else{
137615 sqlite3ExprListDelete(db, pList);
137616 }
137617 }
137618 }
137619}
137620#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
137621
137622/*
137623** We already know that pExpr is a binary operator where both operands are
137624** column references. This routine checks to see if pExpr is an equivalence
137625** relation:
137626** 1. The SQLITE_Transitive optimization must be enabled
137627** 2. Must be either an == or an IS operator
137628** 3. Not originating in the ON clause of an OUTER JOIN
137629** 4. The affinities of A and B must be compatible
137630** 5a. Both operands use the same collating sequence OR
137631** 5b. The overall collating sequence is BINARY
137632** If this routine returns TRUE, that means that the RHS can be substituted
137633** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
137634** This is an optimization. No harm comes from returning 0. But if 1 is
137635** returned when it should not be, then incorrect answers might result.
137636*/
137637static int termIsEquivalence(Parse *pParse, Expr *pExpr){
137638 char aff1, aff2;
137639 CollSeq *pColl;
137640 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
137641 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
137642 if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
137643 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
137644 aff2 = sqlite3ExprAffinity(pExpr->pRight);
137645 if( aff1!=aff2
137646 && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
137647 ){
137648 return 0;
137649 }
137650 pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
137651 if( sqlite3IsBinary(pColl) ) return 1;
137652 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
137653}
137654
137655/*
137656** Recursively walk the expressions of a SELECT statement and generate
137657** a bitmask indicating which tables are used in that expression
137658** tree.
137659*/
137660static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
137661 Bitmask mask = 0;
137662 while( pS ){
137663 SrcList *pSrc = pS->pSrc;
137664 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
137665 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
137666 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
137667 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
137668 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
137669 if( ALWAYS(pSrc!=0) ){
137670 int i;
137671 for(i=0; i<pSrc->nSrc; i++){
137672 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
137673 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
137674 if( pSrc->a[i].fg.isTabFunc ){
137675 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
137676 }
137677 }
137678 }
137679 pS = pS->pPrior;
137680 }
137681 return mask;
137682}
137683
137684/*
137685** Expression pExpr is one operand of a comparison operator that might
137686** be useful for indexing. This routine checks to see if pExpr appears
137687** in any index. Return TRUE (1) if pExpr is an indexed term and return
137688** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor
137689** number of the table that is indexed and aiCurCol[1] to the column number
137690** of the column that is indexed, or XN_EXPR (-2) if an expression is being
137691** indexed.
137692**
137693** If pExpr is a TK_COLUMN column reference, then this routine always returns
137694** true even if that particular column is not indexed, because the column
137695** might be added to an automatic index later.
137696*/
137697static SQLITE_NOINLINE int exprMightBeIndexed2(
137698 SrcList *pFrom, /* The FROM clause */
137699 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
137700 int *aiCurCol, /* Write the referenced table cursor and column here */
137701 Expr *pExpr /* An operand of a comparison operator */
137702){
137703 Index *pIdx;
137704 int i;
137705 int iCur;
137706 for(i=0; mPrereq>1; i++, mPrereq>>=1){}
137707 iCur = pFrom->a[i].iCursor;
137708 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
137709 if( pIdx->aColExpr==0 ) continue;
137710 for(i=0; i<pIdx->nKeyCol; i++){
137711 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
137712 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
137713 aiCurCol[0] = iCur;
137714 aiCurCol[1] = XN_EXPR;
137715 return 1;
137716 }
137717 }
137718 }
137719 return 0;
137720}
137721static int exprMightBeIndexed(
137722 SrcList *pFrom, /* The FROM clause */
137723 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
137724 int *aiCurCol, /* Write the referenced table cursor & column here */
137725 Expr *pExpr, /* An operand of a comparison operator */
137726 int op /* The specific comparison operator */
137727){
137728 /* If this expression is a vector to the left or right of a
137729 ** inequality constraint (>, <, >= or <=), perform the processing
137730 ** on the first element of the vector. */
137731 assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE );
137732 assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE );
137733 assert( op<=TK_GE );
137734 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
137735 pExpr = pExpr->x.pList->a[0].pExpr;
137736 }
137737
137738 if( pExpr->op==TK_COLUMN ){
137739 aiCurCol[0] = pExpr->iTable;
137740 aiCurCol[1] = pExpr->iColumn;
137741 return 1;
137742 }
137743 if( mPrereq==0 ) return 0; /* No table references */
137744 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
137745 return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr);
137746}
137747
137748/*
137749** The input to this routine is an WhereTerm structure with only the
137750** "pExpr" field filled in. The job of this routine is to analyze the
137751** subexpression and populate all the other fields of the WhereTerm
137752** structure.
137753**
137754** If the expression is of the form "<expr> <op> X" it gets commuted
137755** to the standard form of "X <op> <expr>".
137756**
137757** If the expression is of the form "X <op> Y" where both X and Y are
137758** columns, then the original expression is unchanged and a new virtual
137759** term of the form "Y <op> X" is added to the WHERE clause and
137760** analyzed separately. The original term is marked with TERM_COPIED
137761** and the new term is marked with TERM_DYNAMIC (because it's pExpr
137762** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
137763** is a commuted copy of a prior term.) The original term has nChild=1
137764** and the copy has idxParent set to the index of the original term.
137765*/
137766static void exprAnalyze(
137767 SrcList *pSrc, /* the FROM clause */
137768 WhereClause *pWC, /* the WHERE clause */
137769 int idxTerm /* Index of the term to be analyzed */
137770){
137771 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
137772 WhereTerm *pTerm; /* The term to be analyzed */
137773 WhereMaskSet *pMaskSet; /* Set of table index masks */
137774 Expr *pExpr; /* The expression to be analyzed */
137775 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
137776 Bitmask prereqAll; /* Prerequesites of pExpr */
137777 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
137778 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
137779 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
137780 int noCase = 0; /* uppercase equivalent to lowercase */
137781 int op; /* Top-level operator. pExpr->op */
137782 Parse *pParse = pWInfo->pParse; /* Parsing context */
137783 sqlite3 *db = pParse->db; /* Database connection */
137784 unsigned char eOp2 = 0; /* op2 value for LIKE/REGEXP/GLOB */
137785 int nLeft; /* Number of elements on left side vector */
137786
137787 if( db->mallocFailed ){
137788 return;
137789 }
137790 pTerm = &pWC->a[idxTerm];
137791 pMaskSet = &pWInfo->sMaskSet;
137792 pExpr = pTerm->pExpr;
137793 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
137794 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
137795 op = pExpr->op;
137796 if( op==TK_IN ){
137797 assert( pExpr->pRight==0 );
137798 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
137799 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
137800 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
137801 }else{
137802 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
137803 }
137804 }else if( op==TK_ISNULL ){
137805 pTerm->prereqRight = 0;
137806 }else{
137807 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
137808 }
137809 pMaskSet->bVarSelect = 0;
137810 prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
137811 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
137812 if( ExprHasProperty(pExpr, EP_FromJoin) ){
137813 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
137814 prereqAll |= x;
137815 extraRight = x-1; /* ON clause terms may not be used with an index
137816 ** on left table of a LEFT JOIN. Ticket #3015 */
137817 if( (prereqAll>>1)>=x ){
137818 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
137819 return;
137820 }
137821 }
137822 pTerm->prereqAll = prereqAll;
137823 pTerm->leftCursor = -1;
137824 pTerm->iParent = -1;
137825 pTerm->eOperator = 0;
137826 if( allowedOp(op) ){
137827 int aiCurCol[2];
137828 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
137829 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
137830 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
137831
137832 if( pTerm->iField>0 ){
137833 assert( op==TK_IN );
137834 assert( pLeft->op==TK_VECTOR );
137835 pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr;
137836 }
137837
137838 if( exprMightBeIndexed(pSrc, prereqLeft, aiCurCol, pLeft, op) ){
137839 pTerm->leftCursor = aiCurCol[0];
137840 pTerm->u.leftColumn = aiCurCol[1];
137841 pTerm->eOperator = operatorMask(op) & opMask;
137842 }
137843 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
137844 if( pRight
137845 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
137846 ){
137847 WhereTerm *pNew;
137848 Expr *pDup;
137849 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
137850 assert( pTerm->iField==0 );
137851 if( pTerm->leftCursor>=0 ){
137852 int idxNew;
137853 pDup = sqlite3ExprDup(db, pExpr, 0);
137854 if( db->mallocFailed ){
137855 sqlite3ExprDelete(db, pDup);
137856 return;
137857 }
137858 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
137859 if( idxNew==0 ) return;
137860 pNew = &pWC->a[idxNew];
137861 markTermAsChild(pWC, idxNew, idxTerm);
137862 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
137863 pTerm = &pWC->a[idxTerm];
137864 pTerm->wtFlags |= TERM_COPIED;
137865
137866 if( termIsEquivalence(pParse, pDup) ){
137867 pTerm->eOperator |= WO_EQUIV;
137868 eExtraOp = WO_EQUIV;
137869 }
137870 }else{
137871 pDup = pExpr;
137872 pNew = pTerm;
137873 }
137874 exprCommute(pParse, pDup);
137875 pNew->leftCursor = aiCurCol[0];
137876 pNew->u.leftColumn = aiCurCol[1];
137877 testcase( (prereqLeft | extraRight) != prereqLeft );
137878 pNew->prereqRight = prereqLeft | extraRight;
137879 pNew->prereqAll = prereqAll;
137880 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
137881 }
137882 }
137883
137884#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
137885 /* If a term is the BETWEEN operator, create two new virtual terms
137886 ** that define the range that the BETWEEN implements. For example:
137887 **
137888 ** a BETWEEN b AND c
137889 **
137890 ** is converted into:
137891 **
137892 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
137893 **
137894 ** The two new terms are added onto the end of the WhereClause object.
137895 ** The new terms are "dynamic" and are children of the original BETWEEN
137896 ** term. That means that if the BETWEEN term is coded, the children are
137897 ** skipped. Or, if the children are satisfied by an index, the original
137898 ** BETWEEN term is skipped.
137899 */
137900 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
137901 ExprList *pList = pExpr->x.pList;
137902 int i;
137903 static const u8 ops[] = {TK_GE, TK_LE};
137904 assert( pList!=0 );
137905 assert( pList->nExpr==2 );
137906 for(i=0; i<2; i++){
137907 Expr *pNewExpr;
137908 int idxNew;
137909 pNewExpr = sqlite3PExpr(pParse, ops[i],
137910 sqlite3ExprDup(db, pExpr->pLeft, 0),
137911 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
137912 transferJoinMarkings(pNewExpr, pExpr);
137913 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
137914 testcase( idxNew==0 );
137915 exprAnalyze(pSrc, pWC, idxNew);
137916 pTerm = &pWC->a[idxTerm];
137917 markTermAsChild(pWC, idxNew, idxTerm);
137918 }
137919 }
137920#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
137921
137922#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
137923 /* Analyze a term that is composed of two or more subterms connected by
137924 ** an OR operator.
137925 */
137926 else if( pExpr->op==TK_OR ){
137927 assert( pWC->op==TK_AND );
137928 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
137929 pTerm = &pWC->a[idxTerm];
137930 }
137931#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
137932
137933#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
137934 /* Add constraints to reduce the search space on a LIKE or GLOB
137935 ** operator.
137936 **
137937 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
137938 **
137939 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
137940 **
137941 ** The last character of the prefix "abc" is incremented to form the
137942 ** termination condition "abd". If case is not significant (the default
137943 ** for LIKE) then the lower-bound is made all uppercase and the upper-
137944 ** bound is made all lowercase so that the bounds also work when comparing
137945 ** BLOBs.
137946 */
137947 if( pWC->op==TK_AND
137948 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
137949 ){
137950 Expr *pLeft; /* LHS of LIKE/GLOB operator */
137951 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
137952 Expr *pNewExpr1;
137953 Expr *pNewExpr2;
137954 int idxNew1;
137955 int idxNew2;
137956 const char *zCollSeqName; /* Name of collating sequence */
137957 const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
137958
137959 pLeft = pExpr->x.pList->a[1].pExpr;
137960 pStr2 = sqlite3ExprDup(db, pStr1, 0);
137961
137962 /* Convert the lower bound to upper-case and the upper bound to
137963 ** lower-case (upper-case is less than lower-case in ASCII) so that
137964 ** the range constraints also work for BLOBs
137965 */
137966 if( noCase && !pParse->db->mallocFailed ){
137967 int i;
137968 char c;
137969 pTerm->wtFlags |= TERM_LIKE;
137970 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
137971 pStr1->u.zToken[i] = sqlite3Toupper(c);
137972 pStr2->u.zToken[i] = sqlite3Tolower(c);
137973 }
137974 }
137975
137976 if( !db->mallocFailed ){
137977 u8 c, *pC; /* Last character before the first wildcard */
137978 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
137979 c = *pC;
137980 if( noCase ){
137981 /* The point is to increment the last character before the first
137982 ** wildcard. But if we increment '@', that will push it into the
137983 ** alphabetic range where case conversions will mess up the
137984 ** inequality. To avoid this, make sure to also run the full
137985 ** LIKE on all candidate expressions by clearing the isComplete flag
137986 */
137987 if( c=='A'-1 ) isComplete = 0;
137988 c = sqlite3UpperToLower[c];
137989 }
137990 *pC = c + 1;
137991 }
137992 zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY;
137993 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
137994 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
137995 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
137996 pStr1);
137997 transferJoinMarkings(pNewExpr1, pExpr);
137998 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
137999 testcase( idxNew1==0 );
138000 exprAnalyze(pSrc, pWC, idxNew1);
138001 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
138002 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
138003 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
138004 pStr2);
138005 transferJoinMarkings(pNewExpr2, pExpr);
138006 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
138007 testcase( idxNew2==0 );
138008 exprAnalyze(pSrc, pWC, idxNew2);
138009 pTerm = &pWC->a[idxTerm];
138010 if( isComplete ){
138011 markTermAsChild(pWC, idxNew1, idxTerm);
138012 markTermAsChild(pWC, idxNew2, idxTerm);
138013 }
138014 }
138015#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
138016
138017#ifndef SQLITE_OMIT_VIRTUALTABLE
138018 /* Add a WO_AUX auxiliary term to the constraint set if the
138019 ** current expression is of the form "column OP expr" where OP
138020 ** is an operator that gets passed into virtual tables but which is
138021 ** not normally optimized for ordinary tables. In other words, OP
138022 ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
138023 ** This information is used by the xBestIndex methods of
138024 ** virtual tables. The native query optimizer does not attempt
138025 ** to do anything with MATCH functions.
138026 */
138027 if( pWC->op==TK_AND ){
138028 Expr *pRight = 0, *pLeft = 0;
138029 int res = isAuxiliaryVtabOperator(db, pExpr, &eOp2, &pLeft, &pRight);
138030 while( res-- > 0 ){
138031 int idxNew;
138032 WhereTerm *pNewTerm;
138033 Bitmask prereqColumn, prereqExpr;
138034
138035 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
138036 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
138037 if( (prereqExpr & prereqColumn)==0 ){
138038 Expr *pNewExpr;
138039 pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
138040 0, sqlite3ExprDup(db, pRight, 0));
138041 if( ExprHasProperty(pExpr, EP_FromJoin) && pNewExpr ){
138042 ExprSetProperty(pNewExpr, EP_FromJoin);
138043 }
138044 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
138045 testcase( idxNew==0 );
138046 pNewTerm = &pWC->a[idxNew];
138047 pNewTerm->prereqRight = prereqExpr;
138048 pNewTerm->leftCursor = pLeft->iTable;
138049 pNewTerm->u.leftColumn = pLeft->iColumn;
138050 pNewTerm->eOperator = WO_AUX;
138051 pNewTerm->eMatchOp = eOp2;
138052 markTermAsChild(pWC, idxNew, idxTerm);
138053 pTerm = &pWC->a[idxTerm];
138054 pTerm->wtFlags |= TERM_COPIED;
138055 pNewTerm->prereqAll = pTerm->prereqAll;
138056 }
138057 SWAP(Expr*, pLeft, pRight);
138058 }
138059 }
138060#endif /* SQLITE_OMIT_VIRTUALTABLE */
138061
138062 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
138063 ** new terms for each component comparison - "a = ?" and "b = ?". The
138064 ** new terms completely replace the original vector comparison, which is
138065 ** no longer used.
138066 **
138067 ** This is only required if at least one side of the comparison operation
138068 ** is not a sub-select. */
138069 if( pWC->op==TK_AND
138070 && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
138071 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
138072 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
138073 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
138074 || (pExpr->pRight->flags & EP_xIsSelect)==0)
138075 ){
138076 int i;
138077 for(i=0; i<nLeft; i++){
138078 int idxNew;
138079 Expr *pNew;
138080 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
138081 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
138082
138083 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
138084 transferJoinMarkings(pNew, pExpr);
138085 idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
138086 exprAnalyze(pSrc, pWC, idxNew);
138087 }
138088 pTerm = &pWC->a[idxTerm];
138089 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
138090 pTerm->eOperator = 0;
138091 }
138092
138093 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
138094 ** a virtual term for each vector component. The expression object
138095 ** used by each such virtual term is pExpr (the full vector IN(...)
138096 ** expression). The WhereTerm.iField variable identifies the index within
138097 ** the vector on the LHS that the virtual term represents.
138098 **
138099 ** This only works if the RHS is a simple SELECT, not a compound
138100 */
138101 if( pWC->op==TK_AND && pExpr->op==TK_IN && pTerm->iField==0
138102 && pExpr->pLeft->op==TK_VECTOR
138103 && pExpr->x.pSelect->pPrior==0
138104 ){
138105 int i;
138106 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
138107 int idxNew;
138108 idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL);
138109 pWC->a[idxNew].iField = i+1;
138110 exprAnalyze(pSrc, pWC, idxNew);
138111 markTermAsChild(pWC, idxNew, idxTerm);
138112 }
138113 }
138114
138115#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
138116 /* When sqlite_stat3 histogram data is available an operator of the
138117 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
138118 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
138119 ** virtual term of that form.
138120 **
138121 ** Note that the virtual term must be tagged with TERM_VNULL.
138122 */
138123 if( pExpr->op==TK_NOTNULL
138124 && pExpr->pLeft->op==TK_COLUMN
138125 && pExpr->pLeft->iColumn>=0
138126 && OptimizationEnabled(db, SQLITE_Stat34)
138127 ){
138128 Expr *pNewExpr;
138129 Expr *pLeft = pExpr->pLeft;
138130 int idxNew;
138131 WhereTerm *pNewTerm;
138132
138133 pNewExpr = sqlite3PExpr(pParse, TK_GT,
138134 sqlite3ExprDup(db, pLeft, 0),
138135 sqlite3ExprAlloc(db, TK_NULL, 0, 0));
138136
138137 idxNew = whereClauseInsert(pWC, pNewExpr,
138138 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
138139 if( idxNew ){
138140 pNewTerm = &pWC->a[idxNew];
138141 pNewTerm->prereqRight = 0;
138142 pNewTerm->leftCursor = pLeft->iTable;
138143 pNewTerm->u.leftColumn = pLeft->iColumn;
138144 pNewTerm->eOperator = WO_GT;
138145 markTermAsChild(pWC, idxNew, idxTerm);
138146 pTerm = &pWC->a[idxTerm];
138147 pTerm->wtFlags |= TERM_COPIED;
138148 pNewTerm->prereqAll = pTerm->prereqAll;
138149 }
138150 }
138151#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
138152
138153 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
138154 ** an index for tables to the left of the join.
138155 */
138156 testcase( pTerm!=&pWC->a[idxTerm] );
138157 pTerm = &pWC->a[idxTerm];
138158 pTerm->prereqRight |= extraRight;
138159}
138160
138161/***************************************************************************
138162** Routines with file scope above. Interface to the rest of the where.c
138163** subsystem follows.
138164***************************************************************************/
138165
138166/*
138167** This routine identifies subexpressions in the WHERE clause where
138168** each subexpression is separated by the AND operator or some other
138169** operator specified in the op parameter. The WhereClause structure
138170** is filled with pointers to subexpressions. For example:
138171**
138172** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
138173** \________/ \_______________/ \________________/
138174** slot[0] slot[1] slot[2]
138175**
138176** The original WHERE clause in pExpr is unaltered. All this routine
138177** does is make slot[] entries point to substructure within pExpr.
138178**
138179** In the previous sentence and in the diagram, "slot[]" refers to
138180** the WhereClause.a[] array. The slot[] array grows as needed to contain
138181** all terms of the WHERE clause.
138182*/
138183SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
138184 Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
138185 pWC->op = op;
138186 if( pE2==0 ) return;
138187 if( pE2->op!=op ){
138188 whereClauseInsert(pWC, pExpr, 0);
138189 }else{
138190 sqlite3WhereSplit(pWC, pE2->pLeft, op);
138191 sqlite3WhereSplit(pWC, pE2->pRight, op);
138192 }
138193}
138194
138195/*
138196** Initialize a preallocated WhereClause structure.
138197*/
138198SQLITE_PRIVATE void sqlite3WhereClauseInit(
138199 WhereClause *pWC, /* The WhereClause to be initialized */
138200 WhereInfo *pWInfo /* The WHERE processing context */
138201){
138202 pWC->pWInfo = pWInfo;
138203 pWC->hasOr = 0;
138204 pWC->pOuter = 0;
138205 pWC->nTerm = 0;
138206 pWC->nSlot = ArraySize(pWC->aStatic);
138207 pWC->a = pWC->aStatic;
138208}
138209
138210/*
138211** Deallocate a WhereClause structure. The WhereClause structure
138212** itself is not freed. This routine is the inverse of
138213** sqlite3WhereClauseInit().
138214*/
138215SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){
138216 int i;
138217 WhereTerm *a;
138218 sqlite3 *db = pWC->pWInfo->pParse->db;
138219 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
138220 if( a->wtFlags & TERM_DYNAMIC ){
138221 sqlite3ExprDelete(db, a->pExpr);
138222 }
138223 if( a->wtFlags & TERM_ORINFO ){
138224 whereOrInfoDelete(db, a->u.pOrInfo);
138225 }else if( a->wtFlags & TERM_ANDINFO ){
138226 whereAndInfoDelete(db, a->u.pAndInfo);
138227 }
138228 }
138229 if( pWC->a!=pWC->aStatic ){
138230 sqlite3DbFree(db, pWC->a);
138231 }
138232}
138233
138234
138235/*
138236** These routines walk (recursively) an expression tree and generate
138237** a bitmask indicating which tables are used in that expression
138238** tree.
138239*/
138240SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){
138241 Bitmask mask;
138242 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
138243 return sqlite3WhereGetMask(pMaskSet, p->iTable);
138244 }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
138245 assert( p->op!=TK_IF_NULL_ROW );
138246 return 0;
138247 }
138248 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
138249 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
138250 if( p->pRight ){
138251 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
138252 assert( p->x.pList==0 );
138253 }else if( ExprHasProperty(p, EP_xIsSelect) ){
138254 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
138255 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
138256 }else if( p->x.pList ){
138257 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
138258 }
138259 return mask;
138260}
138261SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
138262 return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
138263}
138264SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
138265 int i;
138266 Bitmask mask = 0;
138267 if( pList ){
138268 for(i=0; i<pList->nExpr; i++){
138269 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
138270 }
138271 }
138272 return mask;
138273}
138274
138275
138276/*
138277** Call exprAnalyze on all terms in a WHERE clause.
138278**
138279** Note that exprAnalyze() might add new virtual terms onto the
138280** end of the WHERE clause. We do not want to analyze these new
138281** virtual terms, so start analyzing at the end and work forward
138282** so that the added virtual terms are never processed.
138283*/
138284SQLITE_PRIVATE void sqlite3WhereExprAnalyze(
138285 SrcList *pTabList, /* the FROM clause */
138286 WhereClause *pWC /* the WHERE clause to be analyzed */
138287){
138288 int i;
138289 for(i=pWC->nTerm-1; i>=0; i--){
138290 exprAnalyze(pTabList, pWC, i);
138291 }
138292}
138293
138294/*
138295** For table-valued-functions, transform the function arguments into
138296** new WHERE clause terms.
138297**
138298** Each function argument translates into an equality constraint against
138299** a HIDDEN column in the table.
138300*/
138301SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(
138302 Parse *pParse, /* Parsing context */
138303 struct SrcList_item *pItem, /* The FROM clause term to process */
138304 WhereClause *pWC /* Xfer function arguments to here */
138305){
138306 Table *pTab;
138307 int j, k;
138308 ExprList *pArgs;
138309 Expr *pColRef;
138310 Expr *pTerm;
138311 if( pItem->fg.isTabFunc==0 ) return;
138312 pTab = pItem->pTab;
138313 assert( pTab!=0 );
138314 pArgs = pItem->u1.pFuncArg;
138315 if( pArgs==0 ) return;
138316 for(j=k=0; j<pArgs->nExpr; j++){
138317 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
138318 if( k>=pTab->nCol ){
138319 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
138320 pTab->zName, j);
138321 return;
138322 }
138323 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
138324 if( pColRef==0 ) return;
138325 pColRef->iTable = pItem->iCursor;
138326 pColRef->iColumn = k++;
138327 pColRef->pTab = pTab;
138328 pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef,
138329 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0));
138330 whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
138331 }
138332}
138333
138334/************** End of whereexpr.c *******************************************/
138335/************** Begin file where.c *******************************************/
138336/*
138337** 2001 September 15
138338**
138339** The author disclaims copyright to this source code. In place of
138340** a legal notice, here is a blessing:
138341**
138342** May you do good and not evil.
138343** May you find forgiveness for yourself and forgive others.
138344** May you share freely, never taking more than you give.
138345**
138346*************************************************************************
138347** This module contains C code that generates VDBE code used to process
138348** the WHERE clause of SQL statements. This module is responsible for
138349** generating the code that loops through a table looking for applicable
138350** rows. Indices are selected and used to speed the search when doing
138351** so is applicable. Because this module is responsible for selecting
138352** indices, you might also think of this module as the "query optimizer".
138353*/
138354/* #include "sqliteInt.h" */
138355/* #include "whereInt.h" */
138356
138357/*
138358** Extra information appended to the end of sqlite3_index_info but not
138359** visible to the xBestIndex function, at least not directly. The
138360** sqlite3_vtab_collation() interface knows how to reach it, however.
138361**
138362** This object is not an API and can be changed from one release to the
138363** next. As long as allocateIndexInfo() and sqlite3_vtab_collation()
138364** agree on the structure, all will be well.
138365*/
138366typedef struct HiddenIndexInfo HiddenIndexInfo;
138367struct HiddenIndexInfo {
138368 WhereClause *pWC; /* The Where clause being analyzed */
138369 Parse *pParse; /* The parsing context */
138370};
138371
138372/* Forward declaration of methods */
138373static int whereLoopResize(sqlite3*, WhereLoop*, int);
138374
138375/* Test variable that can be set to enable WHERE tracing */
138376#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
138377/***/ int sqlite3WhereTrace = 0;
138378#endif
138379
138380
138381/*
138382** Return the estimated number of output rows from a WHERE clause
138383*/
138384SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
138385 return pWInfo->nRowOut;
138386}
138387
138388/*
138389** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
138390** WHERE clause returns outputs for DISTINCT processing.
138391*/
138392SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
138393 return pWInfo->eDistinct;
138394}
138395
138396/*
138397** Return TRUE if the WHERE clause returns rows in ORDER BY order.
138398** Return FALSE if the output needs to be sorted.
138399*/
138400SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
138401 return pWInfo->nOBSat;
138402}
138403
138404/*
138405** In the ORDER BY LIMIT optimization, if the inner-most loop is known
138406** to emit rows in increasing order, and if the last row emitted by the
138407** inner-most loop did not fit within the sorter, then we can skip all
138408** subsequent rows for the current iteration of the inner loop (because they
138409** will not fit in the sorter either) and continue with the second inner
138410** loop - the loop immediately outside the inner-most.
138411**
138412** When a row does not fit in the sorter (because the sorter already
138413** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the
138414** label returned by this function.
138415**
138416** If the ORDER BY LIMIT optimization applies, the jump destination should
138417** be the continuation for the second-inner-most loop. If the ORDER BY
138418** LIMIT optimization does not apply, then the jump destination should
138419** be the continuation for the inner-most loop.
138420**
138421** It is always safe for this routine to return the continuation of the
138422** inner-most loop, in the sense that a correct answer will result.
138423** Returning the continuation the second inner loop is an optimization
138424** that might make the code run a little faster, but should not change
138425** the final answer.
138426*/
138427SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo *pWInfo){
138428 WhereLevel *pInner;
138429 if( !pWInfo->bOrderedInnerLoop ){
138430 /* The ORDER BY LIMIT optimization does not apply. Jump to the
138431 ** continuation of the inner-most loop. */
138432 return pWInfo->iContinue;
138433 }
138434 pInner = &pWInfo->a[pWInfo->nLevel-1];
138435 assert( pInner->addrNxt!=0 );
138436 return pInner->addrNxt;
138437}
138438
138439/*
138440** Return the VDBE address or label to jump to in order to continue
138441** immediately with the next row of a WHERE clause.
138442*/
138443SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
138444 assert( pWInfo->iContinue!=0 );
138445 return pWInfo->iContinue;
138446}
138447
138448/*
138449** Return the VDBE address or label to jump to in order to break
138450** out of a WHERE loop.
138451*/
138452SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
138453 return pWInfo->iBreak;
138454}
138455
138456/*
138457** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to
138458** operate directly on the rowis returned by a WHERE clause. Return
138459** ONEPASS_SINGLE (1) if the statement can operation directly because only
138460** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
138461** optimization can be used on multiple
138462**
138463** If the ONEPASS optimization is used (if this routine returns true)
138464** then also write the indices of open cursors used by ONEPASS
138465** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
138466** table and iaCur[1] gets the cursor used by an auxiliary index.
138467** Either value may be -1, indicating that cursor is not used.
138468** Any cursors returned will have been opened for writing.
138469**
138470** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
138471** unable to use the ONEPASS optimization.
138472*/
138473SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
138474 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
138475#ifdef WHERETRACE_ENABLED
138476 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
138477 sqlite3DebugPrintf("%s cursors: %d %d\n",
138478 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
138479 aiCur[0], aiCur[1]);
138480 }
138481#endif
138482 return pWInfo->eOnePass;
138483}
138484
138485/*
138486** Move the content of pSrc into pDest
138487*/
138488static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
138489 pDest->n = pSrc->n;
138490 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
138491}
138492
138493/*
138494** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
138495**
138496** The new entry might overwrite an existing entry, or it might be
138497** appended, or it might be discarded. Do whatever is the right thing
138498** so that pSet keeps the N_OR_COST best entries seen so far.
138499*/
138500static int whereOrInsert(
138501 WhereOrSet *pSet, /* The WhereOrSet to be updated */
138502 Bitmask prereq, /* Prerequisites of the new entry */
138503 LogEst rRun, /* Run-cost of the new entry */
138504 LogEst nOut /* Number of outputs for the new entry */
138505){
138506 u16 i;
138507 WhereOrCost *p;
138508 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
138509 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
138510 goto whereOrInsert_done;
138511 }
138512 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
138513 return 0;
138514 }
138515 }
138516 if( pSet->n<N_OR_COST ){
138517 p = &pSet->a[pSet->n++];
138518 p->nOut = nOut;
138519 }else{
138520 p = pSet->a;
138521 for(i=1; i<pSet->n; i++){
138522 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
138523 }
138524 if( p->rRun<=rRun ) return 0;
138525 }
138526whereOrInsert_done:
138527 p->prereq = prereq;
138528 p->rRun = rRun;
138529 if( p->nOut>nOut ) p->nOut = nOut;
138530 return 1;
138531}
138532
138533/*
138534** Return the bitmask for the given cursor number. Return 0 if
138535** iCursor is not in the set.
138536*/
138537SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
138538 int i;
138539 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
138540 for(i=0; i<pMaskSet->n; i++){
138541 if( pMaskSet->ix[i]==iCursor ){
138542 return MASKBIT(i);
138543 }
138544 }
138545 return 0;
138546}
138547
138548/*
138549** Create a new mask for cursor iCursor.
138550**
138551** There is one cursor per table in the FROM clause. The number of
138552** tables in the FROM clause is limited by a test early in the
138553** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
138554** array will never overflow.
138555*/
138556static void createMask(WhereMaskSet *pMaskSet, int iCursor){
138557 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
138558 pMaskSet->ix[pMaskSet->n++] = iCursor;
138559}
138560
138561/*
138562** Advance to the next WhereTerm that matches according to the criteria
138563** established when the pScan object was initialized by whereScanInit().
138564** Return NULL if there are no more matching WhereTerms.
138565*/
138566static WhereTerm *whereScanNext(WhereScan *pScan){
138567 int iCur; /* The cursor on the LHS of the term */
138568 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
138569 Expr *pX; /* An expression being tested */
138570 WhereClause *pWC; /* Shorthand for pScan->pWC */
138571 WhereTerm *pTerm; /* The term being tested */
138572 int k = pScan->k; /* Where to start scanning */
138573
138574 assert( pScan->iEquiv<=pScan->nEquiv );
138575 pWC = pScan->pWC;
138576 while(1){
138577 iColumn = pScan->aiColumn[pScan->iEquiv-1];
138578 iCur = pScan->aiCur[pScan->iEquiv-1];
138579 assert( pWC!=0 );
138580 do{
138581 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
138582 if( pTerm->leftCursor==iCur
138583 && pTerm->u.leftColumn==iColumn
138584 && (iColumn!=XN_EXPR
138585 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
138586 pScan->pIdxExpr,iCur)==0)
138587 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
138588 ){
138589 if( (pTerm->eOperator & WO_EQUIV)!=0
138590 && pScan->nEquiv<ArraySize(pScan->aiCur)
138591 && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN
138592 ){
138593 int j;
138594 for(j=0; j<pScan->nEquiv; j++){
138595 if( pScan->aiCur[j]==pX->iTable
138596 && pScan->aiColumn[j]==pX->iColumn ){
138597 break;
138598 }
138599 }
138600 if( j==pScan->nEquiv ){
138601 pScan->aiCur[j] = pX->iTable;
138602 pScan->aiColumn[j] = pX->iColumn;
138603 pScan->nEquiv++;
138604 }
138605 }
138606 if( (pTerm->eOperator & pScan->opMask)!=0 ){
138607 /* Verify the affinity and collating sequence match */
138608 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
138609 CollSeq *pColl;
138610 Parse *pParse = pWC->pWInfo->pParse;
138611 pX = pTerm->pExpr;
138612 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
138613 continue;
138614 }
138615 assert(pX->pLeft);
138616 pColl = sqlite3BinaryCompareCollSeq(pParse,
138617 pX->pLeft, pX->pRight);
138618 if( pColl==0 ) pColl = pParse->db->pDfltColl;
138619 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
138620 continue;
138621 }
138622 }
138623 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
138624 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
138625 && pX->iTable==pScan->aiCur[0]
138626 && pX->iColumn==pScan->aiColumn[0]
138627 ){
138628 testcase( pTerm->eOperator & WO_IS );
138629 continue;
138630 }
138631 pScan->pWC = pWC;
138632 pScan->k = k+1;
138633 return pTerm;
138634 }
138635 }
138636 }
138637 pWC = pWC->pOuter;
138638 k = 0;
138639 }while( pWC!=0 );
138640 if( pScan->iEquiv>=pScan->nEquiv ) break;
138641 pWC = pScan->pOrigWC;
138642 k = 0;
138643 pScan->iEquiv++;
138644 }
138645 return 0;
138646}
138647
138648/*
138649** Initialize a WHERE clause scanner object. Return a pointer to the
138650** first match. Return NULL if there are no matches.
138651**
138652** The scanner will be searching the WHERE clause pWC. It will look
138653** for terms of the form "X <op> <expr>" where X is column iColumn of table
138654** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx
138655** must be one of the indexes of table iCur.
138656**
138657** The <op> must be one of the operators described by opMask.
138658**
138659** If the search is for X and the WHERE clause contains terms of the
138660** form X=Y then this routine might also return terms of the form
138661** "Y <op> <expr>". The number of levels of transitivity is limited,
138662** but is enough to handle most commonly occurring SQL statements.
138663**
138664** If X is not the INTEGER PRIMARY KEY then X must be compatible with
138665** index pIdx.
138666*/
138667static WhereTerm *whereScanInit(
138668 WhereScan *pScan, /* The WhereScan object being initialized */
138669 WhereClause *pWC, /* The WHERE clause to be scanned */
138670 int iCur, /* Cursor to scan for */
138671 int iColumn, /* Column to scan for */
138672 u32 opMask, /* Operator(s) to scan for */
138673 Index *pIdx /* Must be compatible with this index */
138674){
138675 pScan->pOrigWC = pWC;
138676 pScan->pWC = pWC;
138677 pScan->pIdxExpr = 0;
138678 pScan->idxaff = 0;
138679 pScan->zCollName = 0;
138680 if( pIdx ){
138681 int j = iColumn;
138682 iColumn = pIdx->aiColumn[j];
138683 if( iColumn==XN_EXPR ){
138684 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
138685 pScan->zCollName = pIdx->azColl[j];
138686 }else if( iColumn==pIdx->pTable->iPKey ){
138687 iColumn = XN_ROWID;
138688 }else if( iColumn>=0 ){
138689 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
138690 pScan->zCollName = pIdx->azColl[j];
138691 }
138692 }else if( iColumn==XN_EXPR ){
138693 return 0;
138694 }
138695 pScan->opMask = opMask;
138696 pScan->k = 0;
138697 pScan->aiCur[0] = iCur;
138698 pScan->aiColumn[0] = iColumn;
138699 pScan->nEquiv = 1;
138700 pScan->iEquiv = 1;
138701 return whereScanNext(pScan);
138702}
138703
138704/*
138705** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
138706** where X is a reference to the iColumn of table iCur or of index pIdx
138707** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by
138708** the op parameter. Return a pointer to the term. Return 0 if not found.
138709**
138710** If pIdx!=0 then it must be one of the indexes of table iCur.
138711** Search for terms matching the iColumn-th column of pIdx
138712** rather than the iColumn-th column of table iCur.
138713**
138714** The term returned might by Y=<expr> if there is another constraint in
138715** the WHERE clause that specifies that X=Y. Any such constraints will be
138716** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
138717** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11
138718** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10
138719** other equivalent values. Hence a search for X will return <expr> if X=A1
138720** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>.
138721**
138722** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
138723** then try for the one with no dependencies on <expr> - in other words where
138724** <expr> is a constant expression of some kind. Only return entries of
138725** the form "X <op> Y" where Y is a column in another table if no terms of
138726** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
138727** exist, try to return a term that does not use WO_EQUIV.
138728*/
138729SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
138730 WhereClause *pWC, /* The WHERE clause to be searched */
138731 int iCur, /* Cursor number of LHS */
138732 int iColumn, /* Column number of LHS */
138733 Bitmask notReady, /* RHS must not overlap with this mask */
138734 u32 op, /* Mask of WO_xx values describing operator */
138735 Index *pIdx /* Must be compatible with this index, if not NULL */
138736){
138737 WhereTerm *pResult = 0;
138738 WhereTerm *p;
138739 WhereScan scan;
138740
138741 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
138742 op &= WO_EQ|WO_IS;
138743 while( p ){
138744 if( (p->prereqRight & notReady)==0 ){
138745 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
138746 testcase( p->eOperator & WO_IS );
138747 return p;
138748 }
138749 if( pResult==0 ) pResult = p;
138750 }
138751 p = whereScanNext(&scan);
138752 }
138753 return pResult;
138754}
138755
138756/*
138757** This function searches pList for an entry that matches the iCol-th column
138758** of index pIdx.
138759**
138760** If such an expression is found, its index in pList->a[] is returned. If
138761** no expression is found, -1 is returned.
138762*/
138763static int findIndexCol(
138764 Parse *pParse, /* Parse context */
138765 ExprList *pList, /* Expression list to search */
138766 int iBase, /* Cursor for table associated with pIdx */
138767 Index *pIdx, /* Index to match column of */
138768 int iCol /* Column of index to match */
138769){
138770 int i;
138771 const char *zColl = pIdx->azColl[iCol];
138772
138773 for(i=0; i<pList->nExpr; i++){
138774 Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
138775 if( p->op==TK_COLUMN
138776 && p->iColumn==pIdx->aiColumn[iCol]
138777 && p->iTable==iBase
138778 ){
138779 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
138780 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
138781 return i;
138782 }
138783 }
138784 }
138785
138786 return -1;
138787}
138788
138789/*
138790** Return TRUE if the iCol-th column of index pIdx is NOT NULL
138791*/
138792static int indexColumnNotNull(Index *pIdx, int iCol){
138793 int j;
138794 assert( pIdx!=0 );
138795 assert( iCol>=0 && iCol<pIdx->nColumn );
138796 j = pIdx->aiColumn[iCol];
138797 if( j>=0 ){
138798 return pIdx->pTable->aCol[j].notNull;
138799 }else if( j==(-1) ){
138800 return 1;
138801 }else{
138802 assert( j==(-2) );
138803 return 0; /* Assume an indexed expression can always yield a NULL */
138804
138805 }
138806}
138807
138808/*
138809** Return true if the DISTINCT expression-list passed as the third argument
138810** is redundant.
138811**
138812** A DISTINCT list is redundant if any subset of the columns in the
138813** DISTINCT list are collectively unique and individually non-null.
138814*/
138815static int isDistinctRedundant(
138816 Parse *pParse, /* Parsing context */
138817 SrcList *pTabList, /* The FROM clause */
138818 WhereClause *pWC, /* The WHERE clause */
138819 ExprList *pDistinct /* The result set that needs to be DISTINCT */
138820){
138821 Table *pTab;
138822 Index *pIdx;
138823 int i;
138824 int iBase;
138825
138826 /* If there is more than one table or sub-select in the FROM clause of
138827 ** this query, then it will not be possible to show that the DISTINCT
138828 ** clause is redundant. */
138829 if( pTabList->nSrc!=1 ) return 0;
138830 iBase = pTabList->a[0].iCursor;
138831 pTab = pTabList->a[0].pTab;
138832
138833 /* If any of the expressions is an IPK column on table iBase, then return
138834 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
138835 ** current SELECT is a correlated sub-query.
138836 */
138837 for(i=0; i<pDistinct->nExpr; i++){
138838 Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
138839 if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
138840 }
138841
138842 /* Loop through all indices on the table, checking each to see if it makes
138843 ** the DISTINCT qualifier redundant. It does so if:
138844 **
138845 ** 1. The index is itself UNIQUE, and
138846 **
138847 ** 2. All of the columns in the index are either part of the pDistinct
138848 ** list, or else the WHERE clause contains a term of the form "col=X",
138849 ** where X is a constant value. The collation sequences of the
138850 ** comparison and select-list expressions must match those of the index.
138851 **
138852 ** 3. All of those index columns for which the WHERE clause does not
138853 ** contain a "col=X" term are subject to a NOT NULL constraint.
138854 */
138855 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
138856 if( !IsUniqueIndex(pIdx) ) continue;
138857 for(i=0; i<pIdx->nKeyCol; i++){
138858 if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ, pIdx) ){
138859 if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break;
138860 if( indexColumnNotNull(pIdx, i)==0 ) break;
138861 }
138862 }
138863 if( i==pIdx->nKeyCol ){
138864 /* This index implies that the DISTINCT qualifier is redundant. */
138865 return 1;
138866 }
138867 }
138868
138869 return 0;
138870}
138871
138872
138873/*
138874** Estimate the logarithm of the input value to base 2.
138875*/
138876static LogEst estLog(LogEst N){
138877 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
138878}
138879
138880/*
138881** Convert OP_Column opcodes to OP_Copy in previously generated code.
138882**
138883** This routine runs over generated VDBE code and translates OP_Column
138884** opcodes into OP_Copy when the table is being accessed via co-routine
138885** instead of via table lookup.
138886**
138887** If the bIncrRowid parameter is 0, then any OP_Rowid instructions on
138888** cursor iTabCur are transformed into OP_Null. Or, if bIncrRowid is non-zero,
138889** then each OP_Rowid is transformed into an instruction to increment the
138890** value stored in its output register.
138891*/
138892static void translateColumnToCopy(
138893 Parse *pParse, /* Parsing context */
138894 int iStart, /* Translate from this opcode to the end */
138895 int iTabCur, /* OP_Column/OP_Rowid references to this table */
138896 int iRegister, /* The first column is in this register */
138897 int bIncrRowid /* If non-zero, transform OP_rowid to OP_AddImm(1) */
138898){
138899 Vdbe *v = pParse->pVdbe;
138900 VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
138901 int iEnd = sqlite3VdbeCurrentAddr(v);
138902 if( pParse->db->mallocFailed ) return;
138903 for(; iStart<iEnd; iStart++, pOp++){
138904 if( pOp->p1!=iTabCur ) continue;
138905 if( pOp->opcode==OP_Column ){
138906 pOp->opcode = OP_Copy;
138907 pOp->p1 = pOp->p2 + iRegister;
138908 pOp->p2 = pOp->p3;
138909 pOp->p3 = 0;
138910 }else if( pOp->opcode==OP_Rowid ){
138911 if( bIncrRowid ){
138912 /* Increment the value stored in the P2 operand of the OP_Rowid. */
138913 pOp->opcode = OP_AddImm;
138914 pOp->p1 = pOp->p2;
138915 pOp->p2 = 1;
138916 }else{
138917 pOp->opcode = OP_Null;
138918 pOp->p1 = 0;
138919 pOp->p3 = 0;
138920 }
138921 }
138922 }
138923}
138924
138925/*
138926** Two routines for printing the content of an sqlite3_index_info
138927** structure. Used for testing and debugging only. If neither
138928** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
138929** are no-ops.
138930*/
138931#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
138932static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
138933 int i;
138934 if( !sqlite3WhereTrace ) return;
138935 for(i=0; i<p->nConstraint; i++){
138936 sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
138937 i,
138938 p->aConstraint[i].iColumn,
138939 p->aConstraint[i].iTermOffset,
138940 p->aConstraint[i].op,
138941 p->aConstraint[i].usable);
138942 }
138943 for(i=0; i<p->nOrderBy; i++){
138944 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
138945 i,
138946 p->aOrderBy[i].iColumn,
138947 p->aOrderBy[i].desc);
138948 }
138949}
138950static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
138951 int i;
138952 if( !sqlite3WhereTrace ) return;
138953 for(i=0; i<p->nConstraint; i++){
138954 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
138955 i,
138956 p->aConstraintUsage[i].argvIndex,
138957 p->aConstraintUsage[i].omit);
138958 }
138959 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
138960 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
138961 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
138962 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
138963 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
138964}
138965#else
138966#define TRACE_IDX_INPUTS(A)
138967#define TRACE_IDX_OUTPUTS(A)
138968#endif
138969
138970#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
138971/*
138972** Return TRUE if the WHERE clause term pTerm is of a form where it
138973** could be used with an index to access pSrc, assuming an appropriate
138974** index existed.
138975*/
138976static int termCanDriveIndex(
138977 WhereTerm *pTerm, /* WHERE clause term to check */
138978 struct SrcList_item *pSrc, /* Table we are trying to access */
138979 Bitmask notReady /* Tables in outer loops of the join */
138980){
138981 char aff;
138982 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
138983 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
138984 if( (pSrc->fg.jointype & JT_LEFT)
138985 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
138986 && (pTerm->eOperator & WO_IS)
138987 ){
138988 /* Cannot use an IS term from the WHERE clause as an index driver for
138989 ** the RHS of a LEFT JOIN. Such a term can only be used if it is from
138990 ** the ON clause. */
138991 return 0;
138992 }
138993 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
138994 if( pTerm->u.leftColumn<0 ) return 0;
138995 aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
138996 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
138997 testcase( pTerm->pExpr->op==TK_IS );
138998 return 1;
138999}
139000#endif
139001
139002
139003#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
139004/*
139005** Generate code to construct the Index object for an automatic index
139006** and to set up the WhereLevel object pLevel so that the code generator
139007** makes use of the automatic index.
139008*/
139009static void constructAutomaticIndex(
139010 Parse *pParse, /* The parsing context */
139011 WhereClause *pWC, /* The WHERE clause */
139012 struct SrcList_item *pSrc, /* The FROM clause term to get the next index */
139013 Bitmask notReady, /* Mask of cursors that are not available */
139014 WhereLevel *pLevel /* Write new index here */
139015){
139016 int nKeyCol; /* Number of columns in the constructed index */
139017 WhereTerm *pTerm; /* A single term of the WHERE clause */
139018 WhereTerm *pWCEnd; /* End of pWC->a[] */
139019 Index *pIdx; /* Object describing the transient index */
139020 Vdbe *v; /* Prepared statement under construction */
139021 int addrInit; /* Address of the initialization bypass jump */
139022 Table *pTable; /* The table being indexed */
139023 int addrTop; /* Top of the index fill loop */
139024 int regRecord; /* Register holding an index record */
139025 int n; /* Column counter */
139026 int i; /* Loop counter */
139027 int mxBitCol; /* Maximum column in pSrc->colUsed */
139028 CollSeq *pColl; /* Collating sequence to on a column */
139029 WhereLoop *pLoop; /* The Loop object */
139030 char *zNotUsed; /* Extra space on the end of pIdx */
139031 Bitmask idxCols; /* Bitmap of columns used for indexing */
139032 Bitmask extraCols; /* Bitmap of additional columns */
139033 u8 sentWarning = 0; /* True if a warnning has been issued */
139034 Expr *pPartial = 0; /* Partial Index Expression */
139035 int iContinue = 0; /* Jump here to skip excluded rows */
139036 struct SrcList_item *pTabItem; /* FROM clause term being indexed */
139037 int addrCounter = 0; /* Address where integer counter is initialized */
139038 int regBase; /* Array of registers where record is assembled */
139039
139040 /* Generate code to skip over the creation and initialization of the
139041 ** transient index on 2nd and subsequent iterations of the loop. */
139042 v = pParse->pVdbe;
139043 assert( v!=0 );
139044 addrInit = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
139045
139046 /* Count the number of columns that will be added to the index
139047 ** and used to match WHERE clause constraints */
139048 nKeyCol = 0;
139049 pTable = pSrc->pTab;
139050 pWCEnd = &pWC->a[pWC->nTerm];
139051 pLoop = pLevel->pWLoop;
139052 idxCols = 0;
139053 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
139054 Expr *pExpr = pTerm->pExpr;
139055 assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */
139056 || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */
139057 || pLoop->prereq!=0 ); /* table of a LEFT JOIN */
139058 if( pLoop->prereq==0
139059 && (pTerm->wtFlags & TERM_VIRTUAL)==0
139060 && !ExprHasProperty(pExpr, EP_FromJoin)
139061 && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){
139062 pPartial = sqlite3ExprAnd(pParse->db, pPartial,
139063 sqlite3ExprDup(pParse->db, pExpr, 0));
139064 }
139065 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
139066 int iCol = pTerm->u.leftColumn;
139067 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
139068 testcase( iCol==BMS );
139069 testcase( iCol==BMS-1 );
139070 if( !sentWarning ){
139071 sqlite3_log(SQLITE_WARNING_AUTOINDEX,
139072 "automatic index on %s(%s)", pTable->zName,
139073 pTable->aCol[iCol].zName);
139074 sentWarning = 1;
139075 }
139076 if( (idxCols & cMask)==0 ){
139077 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
139078 goto end_auto_index_create;
139079 }
139080 pLoop->aLTerm[nKeyCol++] = pTerm;
139081 idxCols |= cMask;
139082 }
139083 }
139084 }
139085 assert( nKeyCol>0 );
139086 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
139087 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
139088 | WHERE_AUTO_INDEX;
139089
139090 /* Count the number of additional columns needed to create a
139091 ** covering index. A "covering index" is an index that contains all
139092 ** columns that are needed by the query. With a covering index, the
139093 ** original table never needs to be accessed. Automatic indices must
139094 ** be a covering index because the index will not be updated if the
139095 ** original table changes and the index and table cannot both be used
139096 ** if they go out of sync.
139097 */
139098 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
139099 mxBitCol = MIN(BMS-1,pTable->nCol);
139100 testcase( pTable->nCol==BMS-1 );
139101 testcase( pTable->nCol==BMS-2 );
139102 for(i=0; i<mxBitCol; i++){
139103 if( extraCols & MASKBIT(i) ) nKeyCol++;
139104 }
139105 if( pSrc->colUsed & MASKBIT(BMS-1) ){
139106 nKeyCol += pTable->nCol - BMS + 1;
139107 }
139108
139109 /* Construct the Index object to describe this index */
139110 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
139111 if( pIdx==0 ) goto end_auto_index_create;
139112 pLoop->u.btree.pIndex = pIdx;
139113 pIdx->zName = "auto-index";
139114 pIdx->pTable = pTable;
139115 n = 0;
139116 idxCols = 0;
139117 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
139118 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
139119 int iCol = pTerm->u.leftColumn;
139120 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
139121 testcase( iCol==BMS-1 );
139122 testcase( iCol==BMS );
139123 if( (idxCols & cMask)==0 ){
139124 Expr *pX = pTerm->pExpr;
139125 idxCols |= cMask;
139126 pIdx->aiColumn[n] = pTerm->u.leftColumn;
139127 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
139128 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
139129 n++;
139130 }
139131 }
139132 }
139133 assert( (u32)n==pLoop->u.btree.nEq );
139134
139135 /* Add additional columns needed to make the automatic index into
139136 ** a covering index */
139137 for(i=0; i<mxBitCol; i++){
139138 if( extraCols & MASKBIT(i) ){
139139 pIdx->aiColumn[n] = i;
139140 pIdx->azColl[n] = sqlite3StrBINARY;
139141 n++;
139142 }
139143 }
139144 if( pSrc->colUsed & MASKBIT(BMS-1) ){
139145 for(i=BMS-1; i<pTable->nCol; i++){
139146 pIdx->aiColumn[n] = i;
139147 pIdx->azColl[n] = sqlite3StrBINARY;
139148 n++;
139149 }
139150 }
139151 assert( n==nKeyCol );
139152 pIdx->aiColumn[n] = XN_ROWID;
139153 pIdx->azColl[n] = sqlite3StrBINARY;
139154
139155 /* Create the automatic index */
139156 assert( pLevel->iIdxCur>=0 );
139157 pLevel->iIdxCur = pParse->nTab++;
139158 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
139159 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
139160 VdbeComment((v, "for %s", pTable->zName));
139161
139162 /* Fill the automatic index with content */
139163 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
139164 if( pTabItem->fg.viaCoroutine ){
139165 int regYield = pTabItem->regReturn;
139166 addrCounter = sqlite3VdbeAddOp2(v, OP_Integer, 0, 0);
139167 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
139168 addrTop = sqlite3VdbeAddOp1(v, OP_Yield, regYield);
139169 VdbeCoverage(v);
139170 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
139171 }else{
139172 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
139173 }
139174 if( pPartial ){
139175 iContinue = sqlite3VdbeMakeLabel(v);
139176 sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL);
139177 pLoop->wsFlags |= WHERE_PARTIALIDX;
139178 }
139179 regRecord = sqlite3GetTempReg(pParse);
139180 regBase = sqlite3GenerateIndexKey(
139181 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
139182 );
139183 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
139184 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
139185 if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
139186 if( pTabItem->fg.viaCoroutine ){
139187 sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
139188 testcase( pParse->db->mallocFailed );
139189 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
139190 pTabItem->regResult, 1);
139191 sqlite3VdbeGoto(v, addrTop);
139192 pTabItem->fg.viaCoroutine = 0;
139193 }else{
139194 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
139195 }
139196 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
139197 sqlite3VdbeJumpHere(v, addrTop);
139198 sqlite3ReleaseTempReg(pParse, regRecord);
139199
139200 /* Jump here when skipping the initialization */
139201 sqlite3VdbeJumpHere(v, addrInit);
139202
139203end_auto_index_create:
139204 sqlite3ExprDelete(pParse->db, pPartial);
139205}
139206#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
139207
139208#ifndef SQLITE_OMIT_VIRTUALTABLE
139209/*
139210** Allocate and populate an sqlite3_index_info structure. It is the
139211** responsibility of the caller to eventually release the structure
139212** by passing the pointer returned by this function to sqlite3_free().
139213*/
139214static sqlite3_index_info *allocateIndexInfo(
139215 Parse *pParse, /* The parsing context */
139216 WhereClause *pWC, /* The WHERE clause being analyzed */
139217 Bitmask mUnusable, /* Ignore terms with these prereqs */
139218 struct SrcList_item *pSrc, /* The FROM clause term that is the vtab */
139219 ExprList *pOrderBy, /* The ORDER BY clause */
139220 u16 *pmNoOmit /* Mask of terms not to omit */
139221){
139222 int i, j;
139223 int nTerm;
139224 struct sqlite3_index_constraint *pIdxCons;
139225 struct sqlite3_index_orderby *pIdxOrderBy;
139226 struct sqlite3_index_constraint_usage *pUsage;
139227 struct HiddenIndexInfo *pHidden;
139228 WhereTerm *pTerm;
139229 int nOrderBy;
139230 sqlite3_index_info *pIdxInfo;
139231 u16 mNoOmit = 0;
139232
139233 /* Count the number of possible WHERE clause constraints referring
139234 ** to this virtual table */
139235 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
139236 if( pTerm->leftCursor != pSrc->iCursor ) continue;
139237 if( pTerm->prereqRight & mUnusable ) continue;
139238 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
139239 testcase( pTerm->eOperator & WO_IN );
139240 testcase( pTerm->eOperator & WO_ISNULL );
139241 testcase( pTerm->eOperator & WO_IS );
139242 testcase( pTerm->eOperator & WO_ALL );
139243 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
139244 if( pTerm->wtFlags & TERM_VNULL ) continue;
139245 assert( pTerm->u.leftColumn>=(-1) );
139246 nTerm++;
139247 }
139248
139249 /* If the ORDER BY clause contains only columns in the current
139250 ** virtual table then allocate space for the aOrderBy part of
139251 ** the sqlite3_index_info structure.
139252 */
139253 nOrderBy = 0;
139254 if( pOrderBy ){
139255 int n = pOrderBy->nExpr;
139256 for(i=0; i<n; i++){
139257 Expr *pExpr = pOrderBy->a[i].pExpr;
139258 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
139259 }
139260 if( i==n){
139261 nOrderBy = n;
139262 }
139263 }
139264
139265 /* Allocate the sqlite3_index_info structure
139266 */
139267 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
139268 + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
139269 + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden) );
139270 if( pIdxInfo==0 ){
139271 sqlite3ErrorMsg(pParse, "out of memory");
139272 return 0;
139273 }
139274
139275 /* Initialize the structure. The sqlite3_index_info structure contains
139276 ** many fields that are declared "const" to prevent xBestIndex from
139277 ** changing them. We have to do some funky casting in order to
139278 ** initialize those fields.
139279 */
139280 pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
139281 pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1];
139282 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
139283 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
139284 *(int*)&pIdxInfo->nConstraint = nTerm;
139285 *(int*)&pIdxInfo->nOrderBy = nOrderBy;
139286 *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
139287 *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
139288 *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
139289 pUsage;
139290
139291 pHidden->pWC = pWC;
139292 pHidden->pParse = pParse;
139293 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
139294 u16 op;
139295 if( pTerm->leftCursor != pSrc->iCursor ) continue;
139296 if( pTerm->prereqRight & mUnusable ) continue;
139297 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
139298 testcase( pTerm->eOperator & WO_IN );
139299 testcase( pTerm->eOperator & WO_IS );
139300 testcase( pTerm->eOperator & WO_ISNULL );
139301 testcase( pTerm->eOperator & WO_ALL );
139302 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
139303 if( pTerm->wtFlags & TERM_VNULL ) continue;
139304 if( (pSrc->fg.jointype & JT_LEFT)!=0
139305 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
139306 && (pTerm->eOperator & (WO_IS|WO_ISNULL))
139307 ){
139308 /* An "IS" term in the WHERE clause where the virtual table is the rhs
139309 ** of a LEFT JOIN. Do not pass this term to the virtual table
139310 ** implementation, as this can lead to incorrect results from SQL such
139311 ** as:
139312 **
139313 ** "LEFT JOIN vtab WHERE vtab.col IS NULL" */
139314 testcase( pTerm->eOperator & WO_ISNULL );
139315 testcase( pTerm->eOperator & WO_IS );
139316 continue;
139317 }
139318 assert( pTerm->u.leftColumn>=(-1) );
139319 pIdxCons[j].iColumn = pTerm->u.leftColumn;
139320 pIdxCons[j].iTermOffset = i;
139321 op = pTerm->eOperator & WO_ALL;
139322 if( op==WO_IN ) op = WO_EQ;
139323 if( op==WO_AUX ){
139324 pIdxCons[j].op = pTerm->eMatchOp;
139325 }else if( op & (WO_ISNULL|WO_IS) ){
139326 if( op==WO_ISNULL ){
139327 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL;
139328 }else{
139329 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS;
139330 }
139331 }else{
139332 pIdxCons[j].op = (u8)op;
139333 /* The direct assignment in the previous line is possible only because
139334 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
139335 ** following asserts verify this fact. */
139336 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
139337 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
139338 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
139339 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
139340 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
139341 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
139342
139343 if( op & (WO_LT|WO_LE|WO_GT|WO_GE)
139344 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
139345 ){
139346 if( i<16 ) mNoOmit |= (1 << i);
139347 if( op==WO_LT ) pIdxCons[j].op = WO_LE;
139348 if( op==WO_GT ) pIdxCons[j].op = WO_GE;
139349 }
139350 }
139351
139352 j++;
139353 }
139354 for(i=0; i<nOrderBy; i++){
139355 Expr *pExpr = pOrderBy->a[i].pExpr;
139356 pIdxOrderBy[i].iColumn = pExpr->iColumn;
139357 pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
139358 }
139359
139360 *pmNoOmit = mNoOmit;
139361 return pIdxInfo;
139362}
139363
139364/*
139365** The table object reference passed as the second argument to this function
139366** must represent a virtual table. This function invokes the xBestIndex()
139367** method of the virtual table with the sqlite3_index_info object that
139368** comes in as the 3rd argument to this function.
139369**
139370** If an error occurs, pParse is populated with an error message and a
139371** non-zero value is returned. Otherwise, 0 is returned and the output
139372** part of the sqlite3_index_info structure is left populated.
139373**
139374** Whether or not an error is returned, it is the responsibility of the
139375** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
139376** that this is required.
139377*/
139378static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
139379 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
139380 int rc;
139381
139382 TRACE_IDX_INPUTS(p);
139383 rc = pVtab->pModule->xBestIndex(pVtab, p);
139384 TRACE_IDX_OUTPUTS(p);
139385
139386 if( rc!=SQLITE_OK ){
139387 if( rc==SQLITE_NOMEM ){
139388 sqlite3OomFault(pParse->db);
139389 }else if( !pVtab->zErrMsg ){
139390 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
139391 }else{
139392 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
139393 }
139394 }
139395 sqlite3_free(pVtab->zErrMsg);
139396 pVtab->zErrMsg = 0;
139397
139398#if 0
139399 /* This error is now caught by the caller.
139400 ** Search for "xBestIndex malfunction" below */
139401 for(i=0; i<p->nConstraint; i++){
139402 if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
139403 sqlite3ErrorMsg(pParse,
139404 "table %s: xBestIndex returned an invalid plan", pTab->zName);
139405 }
139406 }
139407#endif
139408
139409 return pParse->nErr;
139410}
139411#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
139412
139413#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
139414/*
139415** Estimate the location of a particular key among all keys in an
139416** index. Store the results in aStat as follows:
139417**
139418** aStat[0] Est. number of rows less than pRec
139419** aStat[1] Est. number of rows equal to pRec
139420**
139421** Return the index of the sample that is the smallest sample that
139422** is greater than or equal to pRec. Note that this index is not an index
139423** into the aSample[] array - it is an index into a virtual set of samples
139424** based on the contents of aSample[] and the number of fields in record
139425** pRec.
139426*/
139427static int whereKeyStats(
139428 Parse *pParse, /* Database connection */
139429 Index *pIdx, /* Index to consider domain of */
139430 UnpackedRecord *pRec, /* Vector of values to consider */
139431 int roundUp, /* Round up if true. Round down if false */
139432 tRowcnt *aStat /* OUT: stats written here */
139433){
139434 IndexSample *aSample = pIdx->aSample;
139435 int iCol; /* Index of required stats in anEq[] etc. */
139436 int i; /* Index of first sample >= pRec */
139437 int iSample; /* Smallest sample larger than or equal to pRec */
139438 int iMin = 0; /* Smallest sample not yet tested */
139439 int iTest; /* Next sample to test */
139440 int res; /* Result of comparison operation */
139441 int nField; /* Number of fields in pRec */
139442 tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */
139443
139444#ifndef SQLITE_DEBUG
139445 UNUSED_PARAMETER( pParse );
139446#endif
139447 assert( pRec!=0 );
139448 assert( pIdx->nSample>0 );
139449 assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol );
139450
139451 /* Do a binary search to find the first sample greater than or equal
139452 ** to pRec. If pRec contains a single field, the set of samples to search
139453 ** is simply the aSample[] array. If the samples in aSample[] contain more
139454 ** than one fields, all fields following the first are ignored.
139455 **
139456 ** If pRec contains N fields, where N is more than one, then as well as the
139457 ** samples in aSample[] (truncated to N fields), the search also has to
139458 ** consider prefixes of those samples. For example, if the set of samples
139459 ** in aSample is:
139460 **
139461 ** aSample[0] = (a, 5)
139462 ** aSample[1] = (a, 10)
139463 ** aSample[2] = (b, 5)
139464 ** aSample[3] = (c, 100)
139465 ** aSample[4] = (c, 105)
139466 **
139467 ** Then the search space should ideally be the samples above and the
139468 ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
139469 ** the code actually searches this set:
139470 **
139471 ** 0: (a)
139472 ** 1: (a, 5)
139473 ** 2: (a, 10)
139474 ** 3: (a, 10)
139475 ** 4: (b)
139476 ** 5: (b, 5)
139477 ** 6: (c)
139478 ** 7: (c, 100)
139479 ** 8: (c, 105)
139480 ** 9: (c, 105)
139481 **
139482 ** For each sample in the aSample[] array, N samples are present in the
139483 ** effective sample array. In the above, samples 0 and 1 are based on
139484 ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
139485 **
139486 ** Often, sample i of each block of N effective samples has (i+1) fields.
139487 ** Except, each sample may be extended to ensure that it is greater than or
139488 ** equal to the previous sample in the array. For example, in the above,
139489 ** sample 2 is the first sample of a block of N samples, so at first it
139490 ** appears that it should be 1 field in size. However, that would make it
139491 ** smaller than sample 1, so the binary search would not work. As a result,
139492 ** it is extended to two fields. The duplicates that this creates do not
139493 ** cause any problems.
139494 */
139495 nField = pRec->nField;
139496 iCol = 0;
139497 iSample = pIdx->nSample * nField;
139498 do{
139499 int iSamp; /* Index in aSample[] of test sample */
139500 int n; /* Number of fields in test sample */
139501
139502 iTest = (iMin+iSample)/2;
139503 iSamp = iTest / nField;
139504 if( iSamp>0 ){
139505 /* The proposed effective sample is a prefix of sample aSample[iSamp].
139506 ** Specifically, the shortest prefix of at least (1 + iTest%nField)
139507 ** fields that is greater than the previous effective sample. */
139508 for(n=(iTest % nField) + 1; n<nField; n++){
139509 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
139510 }
139511 }else{
139512 n = iTest + 1;
139513 }
139514
139515 pRec->nField = n;
139516 res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
139517 if( res<0 ){
139518 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
139519 iMin = iTest+1;
139520 }else if( res==0 && n<nField ){
139521 iLower = aSample[iSamp].anLt[n-1];
139522 iMin = iTest+1;
139523 res = -1;
139524 }else{
139525 iSample = iTest;
139526 iCol = n-1;
139527 }
139528 }while( res && iMin<iSample );
139529 i = iSample / nField;
139530
139531#ifdef SQLITE_DEBUG
139532 /* The following assert statements check that the binary search code
139533 ** above found the right answer. This block serves no purpose other
139534 ** than to invoke the asserts. */
139535 if( pParse->db->mallocFailed==0 ){
139536 if( res==0 ){
139537 /* If (res==0) is true, then pRec must be equal to sample i. */
139538 assert( i<pIdx->nSample );
139539 assert( iCol==nField-1 );
139540 pRec->nField = nField;
139541 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
139542 || pParse->db->mallocFailed
139543 );
139544 }else{
139545 /* Unless i==pIdx->nSample, indicating that pRec is larger than
139546 ** all samples in the aSample[] array, pRec must be smaller than the
139547 ** (iCol+1) field prefix of sample i. */
139548 assert( i<=pIdx->nSample && i>=0 );
139549 pRec->nField = iCol+1;
139550 assert( i==pIdx->nSample
139551 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
139552 || pParse->db->mallocFailed );
139553
139554 /* if i==0 and iCol==0, then record pRec is smaller than all samples
139555 ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
139556 ** be greater than or equal to the (iCol) field prefix of sample i.
139557 ** If (i>0), then pRec must also be greater than sample (i-1). */
139558 if( iCol>0 ){
139559 pRec->nField = iCol;
139560 assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0
139561 || pParse->db->mallocFailed );
139562 }
139563 if( i>0 ){
139564 pRec->nField = nField;
139565 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
139566 || pParse->db->mallocFailed );
139567 }
139568 }
139569 }
139570#endif /* ifdef SQLITE_DEBUG */
139571
139572 if( res==0 ){
139573 /* Record pRec is equal to sample i */
139574 assert( iCol==nField-1 );
139575 aStat[0] = aSample[i].anLt[iCol];
139576 aStat[1] = aSample[i].anEq[iCol];
139577 }else{
139578 /* At this point, the (iCol+1) field prefix of aSample[i] is the first
139579 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
139580 ** is larger than all samples in the array. */
139581 tRowcnt iUpper, iGap;
139582 if( i>=pIdx->nSample ){
139583 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
139584 }else{
139585 iUpper = aSample[i].anLt[iCol];
139586 }
139587
139588 if( iLower>=iUpper ){
139589 iGap = 0;
139590 }else{
139591 iGap = iUpper - iLower;
139592 }
139593 if( roundUp ){
139594 iGap = (iGap*2)/3;
139595 }else{
139596 iGap = iGap/3;
139597 }
139598 aStat[0] = iLower + iGap;
139599 aStat[1] = pIdx->aAvgEq[nField-1];
139600 }
139601
139602 /* Restore the pRec->nField value before returning. */
139603 pRec->nField = nField;
139604 return i;
139605}
139606#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
139607
139608/*
139609** If it is not NULL, pTerm is a term that provides an upper or lower
139610** bound on a range scan. Without considering pTerm, it is estimated
139611** that the scan will visit nNew rows. This function returns the number
139612** estimated to be visited after taking pTerm into account.
139613**
139614** If the user explicitly specified a likelihood() value for this term,
139615** then the return value is the likelihood multiplied by the number of
139616** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
139617** has a likelihood of 0.50, and any other term a likelihood of 0.25.
139618*/
139619static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
139620 LogEst nRet = nNew;
139621 if( pTerm ){
139622 if( pTerm->truthProb<=0 ){
139623 nRet += pTerm->truthProb;
139624 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
139625 nRet -= 20; assert( 20==sqlite3LogEst(4) );
139626 }
139627 }
139628 return nRet;
139629}
139630
139631
139632#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
139633/*
139634** Return the affinity for a single column of an index.
139635*/
139636SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){
139637 assert( iCol>=0 && iCol<pIdx->nColumn );
139638 if( !pIdx->zColAff ){
139639 if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB;
139640 }
139641 return pIdx->zColAff[iCol];
139642}
139643#endif
139644
139645
139646#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
139647/*
139648** This function is called to estimate the number of rows visited by a
139649** range-scan on a skip-scan index. For example:
139650**
139651** CREATE INDEX i1 ON t1(a, b, c);
139652** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
139653**
139654** Value pLoop->nOut is currently set to the estimated number of rows
139655** visited for scanning (a=? AND b=?). This function reduces that estimate
139656** by some factor to account for the (c BETWEEN ? AND ?) expression based
139657** on the stat4 data for the index. this scan will be peformed multiple
139658** times (once for each (a,b) combination that matches a=?) is dealt with
139659** by the caller.
139660**
139661** It does this by scanning through all stat4 samples, comparing values
139662** extracted from pLower and pUpper with the corresponding column in each
139663** sample. If L and U are the number of samples found to be less than or
139664** equal to the values extracted from pLower and pUpper respectively, and
139665** N is the total number of samples, the pLoop->nOut value is adjusted
139666** as follows:
139667**
139668** nOut = nOut * ( min(U - L, 1) / N )
139669**
139670** If pLower is NULL, or a value cannot be extracted from the term, L is
139671** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
139672** U is set to N.
139673**
139674** Normally, this function sets *pbDone to 1 before returning. However,
139675** if no value can be extracted from either pLower or pUpper (and so the
139676** estimate of the number of rows delivered remains unchanged), *pbDone
139677** is left as is.
139678**
139679** If an error occurs, an SQLite error code is returned. Otherwise,
139680** SQLITE_OK.
139681*/
139682static int whereRangeSkipScanEst(
139683 Parse *pParse, /* Parsing & code generating context */
139684 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
139685 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
139686 WhereLoop *pLoop, /* Update the .nOut value of this loop */
139687 int *pbDone /* Set to true if at least one expr. value extracted */
139688){
139689 Index *p = pLoop->u.btree.pIndex;
139690 int nEq = pLoop->u.btree.nEq;
139691 sqlite3 *db = pParse->db;
139692 int nLower = -1;
139693 int nUpper = p->nSample+1;
139694 int rc = SQLITE_OK;
139695 u8 aff = sqlite3IndexColumnAffinity(db, p, nEq);
139696 CollSeq *pColl;
139697
139698 sqlite3_value *p1 = 0; /* Value extracted from pLower */
139699 sqlite3_value *p2 = 0; /* Value extracted from pUpper */
139700 sqlite3_value *pVal = 0; /* Value extracted from record */
139701
139702 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
139703 if( pLower ){
139704 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
139705 nLower = 0;
139706 }
139707 if( pUpper && rc==SQLITE_OK ){
139708 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
139709 nUpper = p2 ? 0 : p->nSample;
139710 }
139711
139712 if( p1 || p2 ){
139713 int i;
139714 int nDiff;
139715 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
139716 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
139717 if( rc==SQLITE_OK && p1 ){
139718 int res = sqlite3MemCompare(p1, pVal, pColl);
139719 if( res>=0 ) nLower++;
139720 }
139721 if( rc==SQLITE_OK && p2 ){
139722 int res = sqlite3MemCompare(p2, pVal, pColl);
139723 if( res>=0 ) nUpper++;
139724 }
139725 }
139726 nDiff = (nUpper - nLower);
139727 if( nDiff<=0 ) nDiff = 1;
139728
139729 /* If there is both an upper and lower bound specified, and the
139730 ** comparisons indicate that they are close together, use the fallback
139731 ** method (assume that the scan visits 1/64 of the rows) for estimating
139732 ** the number of rows visited. Otherwise, estimate the number of rows
139733 ** using the method described in the header comment for this function. */
139734 if( nDiff!=1 || pUpper==0 || pLower==0 ){
139735 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
139736 pLoop->nOut -= nAdjust;
139737 *pbDone = 1;
139738 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
139739 nLower, nUpper, nAdjust*-1, pLoop->nOut));
139740 }
139741
139742 }else{
139743 assert( *pbDone==0 );
139744 }
139745
139746 sqlite3ValueFree(p1);
139747 sqlite3ValueFree(p2);
139748 sqlite3ValueFree(pVal);
139749
139750 return rc;
139751}
139752#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
139753
139754/*
139755** This function is used to estimate the number of rows that will be visited
139756** by scanning an index for a range of values. The range may have an upper
139757** bound, a lower bound, or both. The WHERE clause terms that set the upper
139758** and lower bounds are represented by pLower and pUpper respectively. For
139759** example, assuming that index p is on t1(a):
139760**
139761** ... FROM t1 WHERE a > ? AND a < ? ...
139762** |_____| |_____|
139763** | |
139764** pLower pUpper
139765**
139766** If either of the upper or lower bound is not present, then NULL is passed in
139767** place of the corresponding WhereTerm.
139768**
139769** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
139770** column subject to the range constraint. Or, equivalently, the number of
139771** equality constraints optimized by the proposed index scan. For example,
139772** assuming index p is on t1(a, b), and the SQL query is:
139773**
139774** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
139775**
139776** then nEq is set to 1 (as the range restricted column, b, is the second
139777** left-most column of the index). Or, if the query is:
139778**
139779** ... FROM t1 WHERE a > ? AND a < ? ...
139780**
139781** then nEq is set to 0.
139782**
139783** When this function is called, *pnOut is set to the sqlite3LogEst() of the
139784** number of rows that the index scan is expected to visit without
139785** considering the range constraints. If nEq is 0, then *pnOut is the number of
139786** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
139787** to account for the range constraints pLower and pUpper.
139788**
139789** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
139790** used, a single range inequality reduces the search space by a factor of 4.
139791** and a pair of constraints (x>? AND x<?) reduces the expected number of
139792** rows visited by a factor of 64.
139793*/
139794static int whereRangeScanEst(
139795 Parse *pParse, /* Parsing & code generating context */
139796 WhereLoopBuilder *pBuilder,
139797 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
139798 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
139799 WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */
139800){
139801 int rc = SQLITE_OK;
139802 int nOut = pLoop->nOut;
139803 LogEst nNew;
139804
139805#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
139806 Index *p = pLoop->u.btree.pIndex;
139807 int nEq = pLoop->u.btree.nEq;
139808
139809 if( p->nSample>0 && nEq<p->nSampleCol
139810 && OptimizationEnabled(pParse->db, SQLITE_Stat34)
139811 ){
139812 if( nEq==pBuilder->nRecValid ){
139813 UnpackedRecord *pRec = pBuilder->pRec;
139814 tRowcnt a[2];
139815 int nBtm = pLoop->u.btree.nBtm;
139816 int nTop = pLoop->u.btree.nTop;
139817
139818 /* Variable iLower will be set to the estimate of the number of rows in
139819 ** the index that are less than the lower bound of the range query. The
139820 ** lower bound being the concatenation of $P and $L, where $P is the
139821 ** key-prefix formed by the nEq values matched against the nEq left-most
139822 ** columns of the index, and $L is the value in pLower.
139823 **
139824 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
139825 ** is not a simple variable or literal value), the lower bound of the
139826 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
139827 ** if $L is available, whereKeyStats() is called for both ($P) and
139828 ** ($P:$L) and the larger of the two returned values is used.
139829 **
139830 ** Similarly, iUpper is to be set to the estimate of the number of rows
139831 ** less than the upper bound of the range query. Where the upper bound
139832 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
139833 ** of iUpper are requested of whereKeyStats() and the smaller used.
139834 **
139835 ** The number of rows between the two bounds is then just iUpper-iLower.
139836 */
139837 tRowcnt iLower; /* Rows less than the lower bound */
139838 tRowcnt iUpper; /* Rows less than the upper bound */
139839 int iLwrIdx = -2; /* aSample[] for the lower bound */
139840 int iUprIdx = -1; /* aSample[] for the upper bound */
139841
139842 if( pRec ){
139843 testcase( pRec->nField!=pBuilder->nRecValid );
139844 pRec->nField = pBuilder->nRecValid;
139845 }
139846 /* Determine iLower and iUpper using ($P) only. */
139847 if( nEq==0 ){
139848 iLower = 0;
139849 iUpper = p->nRowEst0;
139850 }else{
139851 /* Note: this call could be optimized away - since the same values must
139852 ** have been requested when testing key $P in whereEqualScanEst(). */
139853 whereKeyStats(pParse, p, pRec, 0, a);
139854 iLower = a[0];
139855 iUpper = a[0] + a[1];
139856 }
139857
139858 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
139859 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
139860 assert( p->aSortOrder!=0 );
139861 if( p->aSortOrder[nEq] ){
139862 /* The roles of pLower and pUpper are swapped for a DESC index */
139863 SWAP(WhereTerm*, pLower, pUpper);
139864 SWAP(int, nBtm, nTop);
139865 }
139866
139867 /* If possible, improve on the iLower estimate using ($P:$L). */
139868 if( pLower ){
139869 int n; /* Values extracted from pExpr */
139870 Expr *pExpr = pLower->pExpr->pRight;
139871 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n);
139872 if( rc==SQLITE_OK && n ){
139873 tRowcnt iNew;
139874 u16 mask = WO_GT|WO_LE;
139875 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
139876 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
139877 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
139878 if( iNew>iLower ) iLower = iNew;
139879 nOut--;
139880 pLower = 0;
139881 }
139882 }
139883
139884 /* If possible, improve on the iUpper estimate using ($P:$U). */
139885 if( pUpper ){
139886 int n; /* Values extracted from pExpr */
139887 Expr *pExpr = pUpper->pExpr->pRight;
139888 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n);
139889 if( rc==SQLITE_OK && n ){
139890 tRowcnt iNew;
139891 u16 mask = WO_GT|WO_LE;
139892 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
139893 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
139894 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
139895 if( iNew<iUpper ) iUpper = iNew;
139896 nOut--;
139897 pUpper = 0;
139898 }
139899 }
139900
139901 pBuilder->pRec = pRec;
139902 if( rc==SQLITE_OK ){
139903 if( iUpper>iLower ){
139904 nNew = sqlite3LogEst(iUpper - iLower);
139905 /* TUNING: If both iUpper and iLower are derived from the same
139906 ** sample, then assume they are 4x more selective. This brings
139907 ** the estimated selectivity more in line with what it would be
139908 ** if estimated without the use of STAT3/4 tables. */
139909 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
139910 }else{
139911 nNew = 10; assert( 10==sqlite3LogEst(2) );
139912 }
139913 if( nNew<nOut ){
139914 nOut = nNew;
139915 }
139916 WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n",
139917 (u32)iLower, (u32)iUpper, nOut));
139918 }
139919 }else{
139920 int bDone = 0;
139921 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
139922 if( bDone ) return rc;
139923 }
139924 }
139925#else
139926 UNUSED_PARAMETER(pParse);
139927 UNUSED_PARAMETER(pBuilder);
139928 assert( pLower || pUpper );
139929#endif
139930 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
139931 nNew = whereRangeAdjust(pLower, nOut);
139932 nNew = whereRangeAdjust(pUpper, nNew);
139933
139934 /* TUNING: If there is both an upper and lower limit and neither limit
139935 ** has an application-defined likelihood(), assume the range is
139936 ** reduced by an additional 75%. This means that, by default, an open-ended
139937 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
139938 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
139939 ** match 1/64 of the index. */
139940 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
139941 nNew -= 20;
139942 }
139943
139944 nOut -= (pLower!=0) + (pUpper!=0);
139945 if( nNew<10 ) nNew = 10;
139946 if( nNew<nOut ) nOut = nNew;
139947#if defined(WHERETRACE_ENABLED)
139948 if( pLoop->nOut>nOut ){
139949 WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n",
139950 pLoop->nOut, nOut));
139951 }
139952#endif
139953 pLoop->nOut = (LogEst)nOut;
139954 return rc;
139955}
139956
139957#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
139958/*
139959** Estimate the number of rows that will be returned based on
139960** an equality constraint x=VALUE and where that VALUE occurs in
139961** the histogram data. This only works when x is the left-most
139962** column of an index and sqlite_stat3 histogram data is available
139963** for that index. When pExpr==NULL that means the constraint is
139964** "x IS NULL" instead of "x=VALUE".
139965**
139966** Write the estimated row count into *pnRow and return SQLITE_OK.
139967** If unable to make an estimate, leave *pnRow unchanged and return
139968** non-zero.
139969**
139970** This routine can fail if it is unable to load a collating sequence
139971** required for string comparison, or if unable to allocate memory
139972** for a UTF conversion required for comparison. The error is stored
139973** in the pParse structure.
139974*/
139975static int whereEqualScanEst(
139976 Parse *pParse, /* Parsing & code generating context */
139977 WhereLoopBuilder *pBuilder,
139978 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
139979 tRowcnt *pnRow /* Write the revised row estimate here */
139980){
139981 Index *p = pBuilder->pNew->u.btree.pIndex;
139982 int nEq = pBuilder->pNew->u.btree.nEq;
139983 UnpackedRecord *pRec = pBuilder->pRec;
139984 int rc; /* Subfunction return code */
139985 tRowcnt a[2]; /* Statistics */
139986 int bOk;
139987
139988 assert( nEq>=1 );
139989 assert( nEq<=p->nColumn );
139990 assert( p->aSample!=0 );
139991 assert( p->nSample>0 );
139992 assert( pBuilder->nRecValid<nEq );
139993
139994 /* If values are not available for all fields of the index to the left
139995 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
139996 if( pBuilder->nRecValid<(nEq-1) ){
139997 return SQLITE_NOTFOUND;
139998 }
139999
140000 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
140001 ** below would return the same value. */
140002 if( nEq>=p->nColumn ){
140003 *pnRow = 1;
140004 return SQLITE_OK;
140005 }
140006
140007 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
140008 pBuilder->pRec = pRec;
140009 if( rc!=SQLITE_OK ) return rc;
140010 if( bOk==0 ) return SQLITE_NOTFOUND;
140011 pBuilder->nRecValid = nEq;
140012
140013 whereKeyStats(pParse, p, pRec, 0, a);
140014 WHERETRACE(0x10,("equality scan regions %s(%d): %d\n",
140015 p->zName, nEq-1, (int)a[1]));
140016 *pnRow = a[1];
140017
140018 return rc;
140019}
140020#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
140021
140022#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
140023/*
140024** Estimate the number of rows that will be returned based on
140025** an IN constraint where the right-hand side of the IN operator
140026** is a list of values. Example:
140027**
140028** WHERE x IN (1,2,3,4)
140029**
140030** Write the estimated row count into *pnRow and return SQLITE_OK.
140031** If unable to make an estimate, leave *pnRow unchanged and return
140032** non-zero.
140033**
140034** This routine can fail if it is unable to load a collating sequence
140035** required for string comparison, or if unable to allocate memory
140036** for a UTF conversion required for comparison. The error is stored
140037** in the pParse structure.
140038*/
140039static int whereInScanEst(
140040 Parse *pParse, /* Parsing & code generating context */
140041 WhereLoopBuilder *pBuilder,
140042 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
140043 tRowcnt *pnRow /* Write the revised row estimate here */
140044){
140045 Index *p = pBuilder->pNew->u.btree.pIndex;
140046 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
140047 int nRecValid = pBuilder->nRecValid;
140048 int rc = SQLITE_OK; /* Subfunction return code */
140049 tRowcnt nEst; /* Number of rows for a single term */
140050 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
140051 int i; /* Loop counter */
140052
140053 assert( p->aSample!=0 );
140054 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
140055 nEst = nRow0;
140056 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
140057 nRowEst += nEst;
140058 pBuilder->nRecValid = nRecValid;
140059 }
140060
140061 if( rc==SQLITE_OK ){
140062 if( nRowEst > nRow0 ) nRowEst = nRow0;
140063 *pnRow = nRowEst;
140064 WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
140065 }
140066 assert( pBuilder->nRecValid==nRecValid );
140067 return rc;
140068}
140069#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
140070
140071
140072#ifdef WHERETRACE_ENABLED
140073/*
140074** Print the content of a WhereTerm object
140075*/
140076static void whereTermPrint(WhereTerm *pTerm, int iTerm){
140077 if( pTerm==0 ){
140078 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
140079 }else{
140080 char zType[4];
140081 char zLeft[50];
140082 memcpy(zType, "...", 4);
140083 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
140084 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
140085 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
140086 if( pTerm->eOperator & WO_SINGLE ){
140087 sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}",
140088 pTerm->leftCursor, pTerm->u.leftColumn);
140089 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
140090 sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%lld",
140091 pTerm->u.pOrInfo->indexable);
140092 }else{
140093 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
140094 }
140095 sqlite3DebugPrintf(
140096 "TERM-%-3d %p %s %-12s prob=%-3d op=0x%03x wtFlags=0x%04x",
140097 iTerm, pTerm, zType, zLeft, pTerm->truthProb,
140098 pTerm->eOperator, pTerm->wtFlags);
140099 if( pTerm->iField ){
140100 sqlite3DebugPrintf(" iField=%d\n", pTerm->iField);
140101 }else{
140102 sqlite3DebugPrintf("\n");
140103 }
140104 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
140105 }
140106}
140107#endif
140108
140109#ifdef WHERETRACE_ENABLED
140110/*
140111** Show the complete content of a WhereClause
140112*/
140113SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC){
140114 int i;
140115 for(i=0; i<pWC->nTerm; i++){
140116 whereTermPrint(&pWC->a[i], i);
140117 }
140118}
140119#endif
140120
140121#ifdef WHERETRACE_ENABLED
140122/*
140123** Print a WhereLoop object for debugging purposes
140124*/
140125static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){
140126 WhereInfo *pWInfo = pWC->pWInfo;
140127 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
140128 struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
140129 Table *pTab = pItem->pTab;
140130 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
140131 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
140132 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
140133 sqlite3DebugPrintf(" %12s",
140134 pItem->zAlias ? pItem->zAlias : pTab->zName);
140135 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
140136 const char *zName;
140137 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
140138 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
140139 int i = sqlite3Strlen30(zName) - 1;
140140 while( zName[i]!='_' ) i--;
140141 zName += i;
140142 }
140143 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
140144 }else{
140145 sqlite3DebugPrintf("%20s","");
140146 }
140147 }else{
140148 char *z;
140149 if( p->u.vtab.idxStr ){
140150 z = sqlite3_mprintf("(%d,\"%s\",%x)",
140151 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
140152 }else{
140153 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
140154 }
140155 sqlite3DebugPrintf(" %-19s", z);
140156 sqlite3_free(z);
140157 }
140158 if( p->wsFlags & WHERE_SKIPSCAN ){
140159 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
140160 }else{
140161 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
140162 }
140163 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
140164 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
140165 int i;
140166 for(i=0; i<p->nLTerm; i++){
140167 whereTermPrint(p->aLTerm[i], i);
140168 }
140169 }
140170}
140171#endif
140172
140173/*
140174** Convert bulk memory into a valid WhereLoop that can be passed
140175** to whereLoopClear harmlessly.
140176*/
140177static void whereLoopInit(WhereLoop *p){
140178 p->aLTerm = p->aLTermSpace;
140179 p->nLTerm = 0;
140180 p->nLSlot = ArraySize(p->aLTermSpace);
140181 p->wsFlags = 0;
140182}
140183
140184/*
140185** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact.
140186*/
140187static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
140188 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
140189 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
140190 sqlite3_free(p->u.vtab.idxStr);
140191 p->u.vtab.needFree = 0;
140192 p->u.vtab.idxStr = 0;
140193 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
140194 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
140195 sqlite3DbFreeNN(db, p->u.btree.pIndex);
140196 p->u.btree.pIndex = 0;
140197 }
140198 }
140199}
140200
140201/*
140202** Deallocate internal memory used by a WhereLoop object
140203*/
140204static void whereLoopClear(sqlite3 *db, WhereLoop *p){
140205 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
140206 whereLoopClearUnion(db, p);
140207 whereLoopInit(p);
140208}
140209
140210/*
140211** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
140212*/
140213static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
140214 WhereTerm **paNew;
140215 if( p->nLSlot>=n ) return SQLITE_OK;
140216 n = (n+7)&~7;
140217 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
140218 if( paNew==0 ) return SQLITE_NOMEM_BKPT;
140219 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
140220 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
140221 p->aLTerm = paNew;
140222 p->nLSlot = n;
140223 return SQLITE_OK;
140224}
140225
140226/*
140227** Transfer content from the second pLoop into the first.
140228*/
140229static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
140230 whereLoopClearUnion(db, pTo);
140231 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
140232 memset(&pTo->u, 0, sizeof(pTo->u));
140233 return SQLITE_NOMEM_BKPT;
140234 }
140235 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
140236 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
140237 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
140238 pFrom->u.vtab.needFree = 0;
140239 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
140240 pFrom->u.btree.pIndex = 0;
140241 }
140242 return SQLITE_OK;
140243}
140244
140245/*
140246** Delete a WhereLoop object
140247*/
140248static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
140249 whereLoopClear(db, p);
140250 sqlite3DbFreeNN(db, p);
140251}
140252
140253/*
140254** Free a WhereInfo structure
140255*/
140256static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
140257 int i;
140258 assert( pWInfo!=0 );
140259 for(i=0; i<pWInfo->nLevel; i++){
140260 WhereLevel *pLevel = &pWInfo->a[i];
140261 if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
140262 sqlite3DbFree(db, pLevel->u.in.aInLoop);
140263 }
140264 }
140265 sqlite3WhereClauseClear(&pWInfo->sWC);
140266 while( pWInfo->pLoops ){
140267 WhereLoop *p = pWInfo->pLoops;
140268 pWInfo->pLoops = p->pNextLoop;
140269 whereLoopDelete(db, p);
140270 }
140271 sqlite3DbFreeNN(db, pWInfo);
140272}
140273
140274/*
140275** Return TRUE if all of the following are true:
140276**
140277** (1) X has the same or lower cost that Y
140278** (2) X uses fewer WHERE clause terms than Y
140279** (3) Every WHERE clause term used by X is also used by Y
140280** (4) X skips at least as many columns as Y
140281** (5) If X is a covering index, than Y is too
140282**
140283** Conditions (2) and (3) mean that X is a "proper subset" of Y.
140284** If X is a proper subset of Y then Y is a better choice and ought
140285** to have a lower cost. This routine returns TRUE when that cost
140286** relationship is inverted and needs to be adjusted. Constraint (4)
140287** was added because if X uses skip-scan less than Y it still might
140288** deserve a lower cost even if it is a proper subset of Y. Constraint (5)
140289** was added because a covering index probably deserves to have a lower cost
140290** than a non-covering index even if it is a proper subset.
140291*/
140292static int whereLoopCheaperProperSubset(
140293 const WhereLoop *pX, /* First WhereLoop to compare */
140294 const WhereLoop *pY /* Compare against this WhereLoop */
140295){
140296 int i, j;
140297 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
140298 return 0; /* X is not a subset of Y */
140299 }
140300 if( pY->nSkip > pX->nSkip ) return 0;
140301 if( pX->rRun >= pY->rRun ){
140302 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
140303 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
140304 }
140305 for(i=pX->nLTerm-1; i>=0; i--){
140306 if( pX->aLTerm[i]==0 ) continue;
140307 for(j=pY->nLTerm-1; j>=0; j--){
140308 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
140309 }
140310 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
140311 }
140312 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
140313 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
140314 return 0; /* Constraint (5) */
140315 }
140316 return 1; /* All conditions meet */
140317}
140318
140319/*
140320** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so
140321** that:
140322**
140323** (1) pTemplate costs less than any other WhereLoops that are a proper
140324** subset of pTemplate
140325**
140326** (2) pTemplate costs more than any other WhereLoops for which pTemplate
140327** is a proper subset.
140328**
140329** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
140330** WHERE clause terms than Y and that every WHERE clause term used by X is
140331** also used by Y.
140332*/
140333static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
140334 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
140335 for(; p; p=p->pNextLoop){
140336 if( p->iTab!=pTemplate->iTab ) continue;
140337 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
140338 if( whereLoopCheaperProperSubset(p, pTemplate) ){
140339 /* Adjust pTemplate cost downward so that it is cheaper than its
140340 ** subset p. */
140341 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
140342 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1));
140343 pTemplate->rRun = p->rRun;
140344 pTemplate->nOut = p->nOut - 1;
140345 }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
140346 /* Adjust pTemplate cost upward so that it is costlier than p since
140347 ** pTemplate is a proper subset of p */
140348 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
140349 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1));
140350 pTemplate->rRun = p->rRun;
140351 pTemplate->nOut = p->nOut + 1;
140352 }
140353 }
140354}
140355
140356/*
140357** Search the list of WhereLoops in *ppPrev looking for one that can be
140358** replaced by pTemplate.
140359**
140360** Return NULL if pTemplate does not belong on the WhereLoop list.
140361** In other words if pTemplate ought to be dropped from further consideration.
140362**
140363** If pX is a WhereLoop that pTemplate can replace, then return the
140364** link that points to pX.
140365**
140366** If pTemplate cannot replace any existing element of the list but needs
140367** to be added to the list as a new entry, then return a pointer to the
140368** tail of the list.
140369*/
140370static WhereLoop **whereLoopFindLesser(
140371 WhereLoop **ppPrev,
140372 const WhereLoop *pTemplate
140373){
140374 WhereLoop *p;
140375 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
140376 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
140377 /* If either the iTab or iSortIdx values for two WhereLoop are different
140378 ** then those WhereLoops need to be considered separately. Neither is
140379 ** a candidate to replace the other. */
140380 continue;
140381 }
140382 /* In the current implementation, the rSetup value is either zero
140383 ** or the cost of building an automatic index (NlogN) and the NlogN
140384 ** is the same for compatible WhereLoops. */
140385 assert( p->rSetup==0 || pTemplate->rSetup==0
140386 || p->rSetup==pTemplate->rSetup );
140387
140388 /* whereLoopAddBtree() always generates and inserts the automatic index
140389 ** case first. Hence compatible candidate WhereLoops never have a larger
140390 ** rSetup. Call this SETUP-INVARIANT */
140391 assert( p->rSetup>=pTemplate->rSetup );
140392
140393 /* Any loop using an appliation-defined index (or PRIMARY KEY or
140394 ** UNIQUE constraint) with one or more == constraints is better
140395 ** than an automatic index. Unless it is a skip-scan. */
140396 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
140397 && (pTemplate->nSkip)==0
140398 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
140399 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
140400 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
140401 ){
140402 break;
140403 }
140404
140405 /* If existing WhereLoop p is better than pTemplate, pTemplate can be
140406 ** discarded. WhereLoop p is better if:
140407 ** (1) p has no more dependencies than pTemplate, and
140408 ** (2) p has an equal or lower cost than pTemplate
140409 */
140410 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
140411 && p->rSetup<=pTemplate->rSetup /* (2a) */
140412 && p->rRun<=pTemplate->rRun /* (2b) */
140413 && p->nOut<=pTemplate->nOut /* (2c) */
140414 ){
140415 return 0; /* Discard pTemplate */
140416 }
140417
140418 /* If pTemplate is always better than p, then cause p to be overwritten
140419 ** with pTemplate. pTemplate is better than p if:
140420 ** (1) pTemplate has no more dependences than p, and
140421 ** (2) pTemplate has an equal or lower cost than p.
140422 */
140423 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
140424 && p->rRun>=pTemplate->rRun /* (2a) */
140425 && p->nOut>=pTemplate->nOut /* (2b) */
140426 ){
140427 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
140428 break; /* Cause p to be overwritten by pTemplate */
140429 }
140430 }
140431 return ppPrev;
140432}
140433
140434/*
140435** Insert or replace a WhereLoop entry using the template supplied.
140436**
140437** An existing WhereLoop entry might be overwritten if the new template
140438** is better and has fewer dependencies. Or the template will be ignored
140439** and no insert will occur if an existing WhereLoop is faster and has
140440** fewer dependencies than the template. Otherwise a new WhereLoop is
140441** added based on the template.
140442**
140443** If pBuilder->pOrSet is not NULL then we care about only the
140444** prerequisites and rRun and nOut costs of the N best loops. That
140445** information is gathered in the pBuilder->pOrSet object. This special
140446** processing mode is used only for OR clause processing.
140447**
140448** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
140449** still might overwrite similar loops with the new template if the
140450** new template is better. Loops may be overwritten if the following
140451** conditions are met:
140452**
140453** (1) They have the same iTab.
140454** (2) They have the same iSortIdx.
140455** (3) The template has same or fewer dependencies than the current loop
140456** (4) The template has the same or lower cost than the current loop
140457*/
140458static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
140459 WhereLoop **ppPrev, *p;
140460 WhereInfo *pWInfo = pBuilder->pWInfo;
140461 sqlite3 *db = pWInfo->pParse->db;
140462 int rc;
140463
140464 /* If pBuilder->pOrSet is defined, then only keep track of the costs
140465 ** and prereqs.
140466 */
140467 if( pBuilder->pOrSet!=0 ){
140468 if( pTemplate->nLTerm ){
140469#if WHERETRACE_ENABLED
140470 u16 n = pBuilder->pOrSet->n;
140471 int x =
140472#endif
140473 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
140474 pTemplate->nOut);
140475#if WHERETRACE_ENABLED /* 0x8 */
140476 if( sqlite3WhereTrace & 0x8 ){
140477 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
140478 whereLoopPrint(pTemplate, pBuilder->pWC);
140479 }
140480#endif
140481 }
140482 return SQLITE_OK;
140483 }
140484
140485 /* Look for an existing WhereLoop to replace with pTemplate
140486 */
140487 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
140488 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
140489
140490 if( ppPrev==0 ){
140491 /* There already exists a WhereLoop on the list that is better
140492 ** than pTemplate, so just ignore pTemplate */
140493#if WHERETRACE_ENABLED /* 0x8 */
140494 if( sqlite3WhereTrace & 0x8 ){
140495 sqlite3DebugPrintf(" skip: ");
140496 whereLoopPrint(pTemplate, pBuilder->pWC);
140497 }
140498#endif
140499 return SQLITE_OK;
140500 }else{
140501 p = *ppPrev;
140502 }
140503
140504 /* If we reach this point it means that either p[] should be overwritten
140505 ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
140506 ** WhereLoop and insert it.
140507 */
140508#if WHERETRACE_ENABLED /* 0x8 */
140509 if( sqlite3WhereTrace & 0x8 ){
140510 if( p!=0 ){
140511 sqlite3DebugPrintf("replace: ");
140512 whereLoopPrint(p, pBuilder->pWC);
140513 sqlite3DebugPrintf(" with: ");
140514 }else{
140515 sqlite3DebugPrintf(" add: ");
140516 }
140517 whereLoopPrint(pTemplate, pBuilder->pWC);
140518 }
140519#endif
140520 if( p==0 ){
140521 /* Allocate a new WhereLoop to add to the end of the list */
140522 *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop));
140523 if( p==0 ) return SQLITE_NOMEM_BKPT;
140524 whereLoopInit(p);
140525 p->pNextLoop = 0;
140526 }else{
140527 /* We will be overwriting WhereLoop p[]. But before we do, first
140528 ** go through the rest of the list and delete any other entries besides
140529 ** p[] that are also supplated by pTemplate */
140530 WhereLoop **ppTail = &p->pNextLoop;
140531 WhereLoop *pToDel;
140532 while( *ppTail ){
140533 ppTail = whereLoopFindLesser(ppTail, pTemplate);
140534 if( ppTail==0 ) break;
140535 pToDel = *ppTail;
140536 if( pToDel==0 ) break;
140537 *ppTail = pToDel->pNextLoop;
140538#if WHERETRACE_ENABLED /* 0x8 */
140539 if( sqlite3WhereTrace & 0x8 ){
140540 sqlite3DebugPrintf(" delete: ");
140541 whereLoopPrint(pToDel, pBuilder->pWC);
140542 }
140543#endif
140544 whereLoopDelete(db, pToDel);
140545 }
140546 }
140547 rc = whereLoopXfer(db, p, pTemplate);
140548 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
140549 Index *pIndex = p->u.btree.pIndex;
140550 if( pIndex && pIndex->tnum==0 ){
140551 p->u.btree.pIndex = 0;
140552 }
140553 }
140554 return rc;
140555}
140556
140557/*
140558** Adjust the WhereLoop.nOut value downward to account for terms of the
140559** WHERE clause that reference the loop but which are not used by an
140560** index.
140561*
140562** For every WHERE clause term that is not used by the index
140563** and which has a truth probability assigned by one of the likelihood(),
140564** likely(), or unlikely() SQL functions, reduce the estimated number
140565** of output rows by the probability specified.
140566**
140567** TUNING: For every WHERE clause term that is not used by the index
140568** and which does not have an assigned truth probability, heuristics
140569** described below are used to try to estimate the truth probability.
140570** TODO --> Perhaps this is something that could be improved by better
140571** table statistics.
140572**
140573** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75%
140574** value corresponds to -1 in LogEst notation, so this means decrement
140575** the WhereLoop.nOut field for every such WHERE clause term.
140576**
140577** Heuristic 2: If there exists one or more WHERE clause terms of the
140578** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
140579** final output row estimate is no greater than 1/4 of the total number
140580** of rows in the table. In other words, assume that x==EXPR will filter
140581** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
140582** "x" column is boolean or else -1 or 0 or 1 is a common default value
140583** on the "x" column and so in that case only cap the output row estimate
140584** at 1/2 instead of 1/4.
140585*/
140586static void whereLoopOutputAdjust(
140587 WhereClause *pWC, /* The WHERE clause */
140588 WhereLoop *pLoop, /* The loop to adjust downward */
140589 LogEst nRow /* Number of rows in the entire table */
140590){
140591 WhereTerm *pTerm, *pX;
140592 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
140593 int i, j, k;
140594 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
140595
140596 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
140597 for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
140598 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
140599 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
140600 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
140601 for(j=pLoop->nLTerm-1; j>=0; j--){
140602 pX = pLoop->aLTerm[j];
140603 if( pX==0 ) continue;
140604 if( pX==pTerm ) break;
140605 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
140606 }
140607 if( j<0 ){
140608 if( pTerm->truthProb<=0 ){
140609 /* If a truth probability is specified using the likelihood() hints,
140610 ** then use the probability provided by the application. */
140611 pLoop->nOut += pTerm->truthProb;
140612 }else{
140613 /* In the absence of explicit truth probabilities, use heuristics to
140614 ** guess a reasonable truth probability. */
140615 pLoop->nOut--;
140616 if( pTerm->eOperator&(WO_EQ|WO_IS) ){
140617 Expr *pRight = pTerm->pExpr->pRight;
140618 testcase( pTerm->pExpr->op==TK_IS );
140619 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
140620 k = 10;
140621 }else{
140622 k = 20;
140623 }
140624 if( iReduce<k ) iReduce = k;
140625 }
140626 }
140627 }
140628 }
140629 if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce;
140630}
140631
140632/*
140633** Term pTerm is a vector range comparison operation. The first comparison
140634** in the vector can be optimized using column nEq of the index. This
140635** function returns the total number of vector elements that can be used
140636** as part of the range comparison.
140637**
140638** For example, if the query is:
140639**
140640** WHERE a = ? AND (b, c, d) > (?, ?, ?)
140641**
140642** and the index:
140643**
140644** CREATE INDEX ... ON (a, b, c, d, e)
140645**
140646** then this function would be invoked with nEq=1. The value returned in
140647** this case is 3.
140648*/
140649static int whereRangeVectorLen(
140650 Parse *pParse, /* Parsing context */
140651 int iCur, /* Cursor open on pIdx */
140652 Index *pIdx, /* The index to be used for a inequality constraint */
140653 int nEq, /* Number of prior equality constraints on same index */
140654 WhereTerm *pTerm /* The vector inequality constraint */
140655){
140656 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
140657 int i;
140658
140659 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
140660 for(i=1; i<nCmp; i++){
140661 /* Test if comparison i of pTerm is compatible with column (i+nEq)
140662 ** of the index. If not, exit the loop. */
140663 char aff; /* Comparison affinity */
140664 char idxaff = 0; /* Indexed columns affinity */
140665 CollSeq *pColl; /* Comparison collation sequence */
140666 Expr *pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
140667 Expr *pRhs = pTerm->pExpr->pRight;
140668 if( pRhs->flags & EP_xIsSelect ){
140669 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
140670 }else{
140671 pRhs = pRhs->x.pList->a[i].pExpr;
140672 }
140673
140674 /* Check that the LHS of the comparison is a column reference to
140675 ** the right column of the right source table. And that the sort
140676 ** order of the index column is the same as the sort order of the
140677 ** leftmost index column. */
140678 if( pLhs->op!=TK_COLUMN
140679 || pLhs->iTable!=iCur
140680 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
140681 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
140682 ){
140683 break;
140684 }
140685
140686 testcase( pLhs->iColumn==XN_ROWID );
140687 aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs));
140688 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
140689 if( aff!=idxaff ) break;
140690
140691 pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
140692 if( pColl==0 ) break;
140693 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
140694 }
140695 return i;
140696}
140697
140698/*
140699** Adjust the cost C by the costMult facter T. This only occurs if
140700** compiled with -DSQLITE_ENABLE_COSTMULT
140701*/
140702#ifdef SQLITE_ENABLE_COSTMULT
140703# define ApplyCostMultiplier(C,T) C += T
140704#else
140705# define ApplyCostMultiplier(C,T)
140706#endif
140707
140708/*
140709** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
140710** index pIndex. Try to match one more.
140711**
140712** When this function is called, pBuilder->pNew->nOut contains the
140713** number of rows expected to be visited by filtering using the nEq
140714** terms only. If it is modified, this value is restored before this
140715** function returns.
140716**
140717** If pProbe->tnum==0, that means pIndex is a fake index used for the
140718** INTEGER PRIMARY KEY.
140719*/
140720static int whereLoopAddBtreeIndex(
140721 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
140722 struct SrcList_item *pSrc, /* FROM clause term being analyzed */
140723 Index *pProbe, /* An index on pSrc */
140724 LogEst nInMul /* log(Number of iterations due to IN) */
140725){
140726 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
140727 Parse *pParse = pWInfo->pParse; /* Parsing context */
140728 sqlite3 *db = pParse->db; /* Database connection malloc context */
140729 WhereLoop *pNew; /* Template WhereLoop under construction */
140730 WhereTerm *pTerm; /* A WhereTerm under consideration */
140731 int opMask; /* Valid operators for constraints */
140732 WhereScan scan; /* Iterator for WHERE terms */
140733 Bitmask saved_prereq; /* Original value of pNew->prereq */
140734 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
140735 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
140736 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
140737 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
140738 u16 saved_nSkip; /* Original value of pNew->nSkip */
140739 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
140740 LogEst saved_nOut; /* Original value of pNew->nOut */
140741 int rc = SQLITE_OK; /* Return code */
140742 LogEst rSize; /* Number of rows in the table */
140743 LogEst rLogSize; /* Logarithm of table size */
140744 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
140745
140746 pNew = pBuilder->pNew;
140747 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
140748 WHERETRACE(0x800, ("BEGIN %s.addBtreeIdx(%s), nEq=%d\n",
140749 pProbe->pTable->zName,pProbe->zName, pNew->u.btree.nEq));
140750
140751 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
140752 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
140753 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
140754 opMask = WO_LT|WO_LE;
140755 }else{
140756 assert( pNew->u.btree.nBtm==0 );
140757 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
140758 }
140759 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
140760
140761 assert( pNew->u.btree.nEq<pProbe->nColumn );
140762
140763 saved_nEq = pNew->u.btree.nEq;
140764 saved_nBtm = pNew->u.btree.nBtm;
140765 saved_nTop = pNew->u.btree.nTop;
140766 saved_nSkip = pNew->nSkip;
140767 saved_nLTerm = pNew->nLTerm;
140768 saved_wsFlags = pNew->wsFlags;
140769 saved_prereq = pNew->prereq;
140770 saved_nOut = pNew->nOut;
140771 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
140772 opMask, pProbe);
140773 pNew->rSetup = 0;
140774 rSize = pProbe->aiRowLogEst[0];
140775 rLogSize = estLog(rSize);
140776 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
140777 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
140778 LogEst rCostIdx;
140779 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
140780 int nIn = 0;
140781#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
140782 int nRecValid = pBuilder->nRecValid;
140783#endif
140784 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
140785 && indexColumnNotNull(pProbe, saved_nEq)
140786 ){
140787 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
140788 }
140789 if( pTerm->prereqRight & pNew->maskSelf ) continue;
140790
140791 /* Do not allow the upper bound of a LIKE optimization range constraint
140792 ** to mix with a lower range bound from some other source */
140793 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
140794
140795 /* Do not allow constraints from the WHERE clause to be used by the
140796 ** right table of a LEFT JOIN. Only constraints in the ON clause are
140797 ** allowed */
140798 if( (pSrc->fg.jointype & JT_LEFT)!=0
140799 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
140800 ){
140801 continue;
140802 }
140803
140804 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
140805 pBuilder->bldFlags |= SQLITE_BLDF_UNIQUE;
140806 }else{
140807 pBuilder->bldFlags |= SQLITE_BLDF_INDEXED;
140808 }
140809 pNew->wsFlags = saved_wsFlags;
140810 pNew->u.btree.nEq = saved_nEq;
140811 pNew->u.btree.nBtm = saved_nBtm;
140812 pNew->u.btree.nTop = saved_nTop;
140813 pNew->nLTerm = saved_nLTerm;
140814 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
140815 pNew->aLTerm[pNew->nLTerm++] = pTerm;
140816 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
140817
140818 assert( nInMul==0
140819 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
140820 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
140821 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
140822 );
140823
140824 if( eOp & WO_IN ){
140825 Expr *pExpr = pTerm->pExpr;
140826 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
140827 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
140828 int i;
140829 nIn = 46; assert( 46==sqlite3LogEst(25) );
140830
140831 /* The expression may actually be of the form (x, y) IN (SELECT...).
140832 ** In this case there is a separate term for each of (x) and (y).
140833 ** However, the nIn multiplier should only be applied once, not once
140834 ** for each such term. The following loop checks that pTerm is the
140835 ** first such term in use, and sets nIn back to 0 if it is not. */
140836 for(i=0; i<pNew->nLTerm-1; i++){
140837 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
140838 }
140839 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
140840 /* "x IN (value, value, ...)" */
140841 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
140842 assert( nIn>0 ); /* RHS always has 2 or more terms... The parser
140843 ** changes "x IN (?)" into "x=?". */
140844 }
140845 if( pProbe->hasStat1 ){
140846 LogEst M, logK, safetyMargin;
140847 /* Let:
140848 ** N = the total number of rows in the table
140849 ** K = the number of entries on the RHS of the IN operator
140850 ** M = the number of rows in the table that match terms to the
140851 ** to the left in the same index. If the IN operator is on
140852 ** the left-most index column, M==N.
140853 **
140854 ** Given the definitions above, it is better to omit the IN operator
140855 ** from the index lookup and instead do a scan of the M elements,
140856 ** testing each scanned row against the IN operator separately, if:
140857 **
140858 ** M*log(K) < K*log(N)
140859 **
140860 ** Our estimates for M, K, and N might be inaccurate, so we build in
140861 ** a safety margin of 2 (LogEst: 10) that favors using the IN operator
140862 ** with the index, as using an index has better worst-case behavior.
140863 ** If we do not have real sqlite_stat1 data, always prefer to use
140864 ** the index.
140865 */
140866 M = pProbe->aiRowLogEst[saved_nEq];
140867 logK = estLog(nIn);
140868 safetyMargin = 10; /* TUNING: extra weight for indexed IN */
140869 if( M + logK + safetyMargin < nIn + rLogSize ){
140870 WHERETRACE(0x40,
140871 ("Scan preferred over IN operator on column %d of \"%s\" (%d<%d)\n",
140872 saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
140873 continue;
140874 }else{
140875 WHERETRACE(0x40,
140876 ("IN operator preferred on column %d of \"%s\" (%d>=%d)\n",
140877 saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
140878 }
140879 }
140880 pNew->wsFlags |= WHERE_COLUMN_IN;
140881 }else if( eOp & (WO_EQ|WO_IS) ){
140882 int iCol = pProbe->aiColumn[saved_nEq];
140883 pNew->wsFlags |= WHERE_COLUMN_EQ;
140884 assert( saved_nEq==pNew->u.btree.nEq );
140885 if( iCol==XN_ROWID
140886 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
140887 ){
140888 if( iCol==XN_ROWID || pProbe->uniqNotNull
140889 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
140890 ){
140891 pNew->wsFlags |= WHERE_ONEROW;
140892 }else{
140893 pNew->wsFlags |= WHERE_UNQ_WANTED;
140894 }
140895 }
140896 }else if( eOp & WO_ISNULL ){
140897 pNew->wsFlags |= WHERE_COLUMN_NULL;
140898 }else if( eOp & (WO_GT|WO_GE) ){
140899 testcase( eOp & WO_GT );
140900 testcase( eOp & WO_GE );
140901 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
140902 pNew->u.btree.nBtm = whereRangeVectorLen(
140903 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
140904 );
140905 pBtm = pTerm;
140906 pTop = 0;
140907 if( pTerm->wtFlags & TERM_LIKEOPT ){
140908 /* Range contraints that come from the LIKE optimization are
140909 ** always used in pairs. */
140910 pTop = &pTerm[1];
140911 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
140912 assert( pTop->wtFlags & TERM_LIKEOPT );
140913 assert( pTop->eOperator==WO_LT );
140914 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
140915 pNew->aLTerm[pNew->nLTerm++] = pTop;
140916 pNew->wsFlags |= WHERE_TOP_LIMIT;
140917 pNew->u.btree.nTop = 1;
140918 }
140919 }else{
140920 assert( eOp & (WO_LT|WO_LE) );
140921 testcase( eOp & WO_LT );
140922 testcase( eOp & WO_LE );
140923 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
140924 pNew->u.btree.nTop = whereRangeVectorLen(
140925 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
140926 );
140927 pTop = pTerm;
140928 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
140929 pNew->aLTerm[pNew->nLTerm-2] : 0;
140930 }
140931
140932 /* At this point pNew->nOut is set to the number of rows expected to
140933 ** be visited by the index scan before considering term pTerm, or the
140934 ** values of nIn and nInMul. In other words, assuming that all
140935 ** "x IN(...)" terms are replaced with "x = ?". This block updates
140936 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
140937 assert( pNew->nOut==saved_nOut );
140938 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
140939 /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4
140940 ** data, using some other estimate. */
140941 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
140942 }else{
140943 int nEq = ++pNew->u.btree.nEq;
140944 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) );
140945
140946 assert( pNew->nOut==saved_nOut );
140947 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
140948 assert( (eOp & WO_IN) || nIn==0 );
140949 testcase( eOp & WO_IN );
140950 pNew->nOut += pTerm->truthProb;
140951 pNew->nOut -= nIn;
140952 }else{
140953#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
140954 tRowcnt nOut = 0;
140955 if( nInMul==0
140956 && pProbe->nSample
140957 && pNew->u.btree.nEq<=pProbe->nSampleCol
140958 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
140959 && OptimizationEnabled(db, SQLITE_Stat34)
140960 ){
140961 Expr *pExpr = pTerm->pExpr;
140962 if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){
140963 testcase( eOp & WO_EQ );
140964 testcase( eOp & WO_IS );
140965 testcase( eOp & WO_ISNULL );
140966 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
140967 }else{
140968 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
140969 }
140970 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
140971 if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */
140972 if( nOut ){
140973 pNew->nOut = sqlite3LogEst(nOut);
140974 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
140975 pNew->nOut -= nIn;
140976 }
140977 }
140978 if( nOut==0 )
140979#endif
140980 {
140981 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
140982 if( eOp & WO_ISNULL ){
140983 /* TUNING: If there is no likelihood() value, assume that a
140984 ** "col IS NULL" expression matches twice as many rows
140985 ** as (col=?). */
140986 pNew->nOut += 10;
140987 }
140988 }
140989 }
140990 }
140991
140992 /* Set rCostIdx to the cost of visiting selected rows in index. Add
140993 ** it to pNew->rRun, which is currently set to the cost of the index
140994 ** seek only. Then, if this is a non-covering index, add the cost of
140995 ** visiting the rows in the main table. */
140996 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
140997 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
140998 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
140999 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
141000 }
141001 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
141002
141003 nOutUnadjusted = pNew->nOut;
141004 pNew->rRun += nInMul + nIn;
141005 pNew->nOut += nInMul + nIn;
141006 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
141007 rc = whereLoopInsert(pBuilder, pNew);
141008
141009 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
141010 pNew->nOut = saved_nOut;
141011 }else{
141012 pNew->nOut = nOutUnadjusted;
141013 }
141014
141015 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
141016 && pNew->u.btree.nEq<pProbe->nColumn
141017 ){
141018 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
141019 }
141020 pNew->nOut = saved_nOut;
141021#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
141022 pBuilder->nRecValid = nRecValid;
141023#endif
141024 }
141025 pNew->prereq = saved_prereq;
141026 pNew->u.btree.nEq = saved_nEq;
141027 pNew->u.btree.nBtm = saved_nBtm;
141028 pNew->u.btree.nTop = saved_nTop;
141029 pNew->nSkip = saved_nSkip;
141030 pNew->wsFlags = saved_wsFlags;
141031 pNew->nOut = saved_nOut;
141032 pNew->nLTerm = saved_nLTerm;
141033
141034 /* Consider using a skip-scan if there are no WHERE clause constraints
141035 ** available for the left-most terms of the index, and if the average
141036 ** number of repeats in the left-most terms is at least 18.
141037 **
141038 ** The magic number 18 is selected on the basis that scanning 17 rows
141039 ** is almost always quicker than an index seek (even though if the index
141040 ** contains fewer than 2^17 rows we assume otherwise in other parts of
141041 ** the code). And, even if it is not, it should not be too much slower.
141042 ** On the other hand, the extra seeks could end up being significantly
141043 ** more expensive. */
141044 assert( 42==sqlite3LogEst(18) );
141045 if( saved_nEq==saved_nSkip
141046 && saved_nEq+1<pProbe->nKeyCol
141047 && pProbe->noSkipScan==0
141048 && OptimizationEnabled(db, SQLITE_SkipScan)
141049 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
141050 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
141051 ){
141052 LogEst nIter;
141053 pNew->u.btree.nEq++;
141054 pNew->nSkip++;
141055 pNew->aLTerm[pNew->nLTerm++] = 0;
141056 pNew->wsFlags |= WHERE_SKIPSCAN;
141057 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
141058 pNew->nOut -= nIter;
141059 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
141060 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
141061 nIter += 5;
141062 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
141063 pNew->nOut = saved_nOut;
141064 pNew->u.btree.nEq = saved_nEq;
141065 pNew->nSkip = saved_nSkip;
141066 pNew->wsFlags = saved_wsFlags;
141067 }
141068
141069 WHERETRACE(0x800, ("END %s.addBtreeIdx(%s), nEq=%d, rc=%d\n",
141070 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
141071 return rc;
141072}
141073
141074/*
141075** Return True if it is possible that pIndex might be useful in
141076** implementing the ORDER BY clause in pBuilder.
141077**
141078** Return False if pBuilder does not contain an ORDER BY clause or
141079** if there is no way for pIndex to be useful in implementing that
141080** ORDER BY clause.
141081*/
141082static int indexMightHelpWithOrderBy(
141083 WhereLoopBuilder *pBuilder,
141084 Index *pIndex,
141085 int iCursor
141086){
141087 ExprList *pOB;
141088 ExprList *aColExpr;
141089 int ii, jj;
141090
141091 if( pIndex->bUnordered ) return 0;
141092 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
141093 for(ii=0; ii<pOB->nExpr; ii++){
141094 Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
141095 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
141096 if( pExpr->iColumn<0 ) return 1;
141097 for(jj=0; jj<pIndex->nKeyCol; jj++){
141098 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
141099 }
141100 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
141101 for(jj=0; jj<pIndex->nKeyCol; jj++){
141102 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
141103 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
141104 return 1;
141105 }
141106 }
141107 }
141108 }
141109 return 0;
141110}
141111
141112/* Check to see if a partial index with pPartIndexWhere can be used
141113** in the current query. Return true if it can be and false if not.
141114*/
141115static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
141116 int i;
141117 WhereTerm *pTerm;
141118 Parse *pParse = pWC->pWInfo->pParse;
141119 while( pWhere->op==TK_AND ){
141120 if( !whereUsablePartialIndex(iTab,pWC,pWhere->pLeft) ) return 0;
141121 pWhere = pWhere->pRight;
141122 }
141123 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
141124 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
141125 Expr *pExpr = pTerm->pExpr;
141126 if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
141127 && sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab)
141128 ){
141129 return 1;
141130 }
141131 }
141132 return 0;
141133}
141134
141135/*
141136** Add all WhereLoop objects for a single table of the join where the table
141137** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
141138** a b-tree table, not a virtual table.
141139**
141140** The costs (WhereLoop.rRun) of the b-tree loops added by this function
141141** are calculated as follows:
141142**
141143** For a full scan, assuming the table (or index) contains nRow rows:
141144**
141145** cost = nRow * 3.0 // full-table scan
141146** cost = nRow * K // scan of covering index
141147** cost = nRow * (K+3.0) // scan of non-covering index
141148**
141149** where K is a value between 1.1 and 3.0 set based on the relative
141150** estimated average size of the index and table records.
141151**
141152** For an index scan, where nVisit is the number of index rows visited
141153** by the scan, and nSeek is the number of seek operations required on
141154** the index b-tree:
141155**
141156** cost = nSeek * (log(nRow) + K * nVisit) // covering index
141157** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
141158**
141159** Normally, nSeek is 1. nSeek values greater than 1 come about if the
141160** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
141161** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
141162**
141163** The estimated values (nRow, nVisit, nSeek) often contain a large amount
141164** of uncertainty. For this reason, scoring is designed to pick plans that
141165** "do the least harm" if the estimates are inaccurate. For example, a
141166** log(nRow) factor is omitted from a non-covering index scan in order to
141167** bias the scoring in favor of using an index, since the worst-case
141168** performance of using an index is far better than the worst-case performance
141169** of a full table scan.
141170*/
141171static int whereLoopAddBtree(
141172 WhereLoopBuilder *pBuilder, /* WHERE clause information */
141173 Bitmask mPrereq /* Extra prerequesites for using this table */
141174){
141175 WhereInfo *pWInfo; /* WHERE analysis context */
141176 Index *pProbe; /* An index we are evaluating */
141177 Index sPk; /* A fake index object for the primary key */
141178 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */
141179 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
141180 SrcList *pTabList; /* The FROM clause */
141181 struct SrcList_item *pSrc; /* The FROM clause btree term to add */
141182 WhereLoop *pNew; /* Template WhereLoop object */
141183 int rc = SQLITE_OK; /* Return code */
141184 int iSortIdx = 1; /* Index number */
141185 int b; /* A boolean value */
141186 LogEst rSize; /* number of rows in the table */
141187 LogEst rLogSize; /* Logarithm of the number of rows in the table */
141188 WhereClause *pWC; /* The parsed WHERE clause */
141189 Table *pTab; /* Table being queried */
141190
141191 pNew = pBuilder->pNew;
141192 pWInfo = pBuilder->pWInfo;
141193 pTabList = pWInfo->pTabList;
141194 pSrc = pTabList->a + pNew->iTab;
141195 pTab = pSrc->pTab;
141196 pWC = pBuilder->pWC;
141197 assert( !IsVirtual(pSrc->pTab) );
141198
141199 if( pSrc->pIBIndex ){
141200 /* An INDEXED BY clause specifies a particular index to use */
141201 pProbe = pSrc->pIBIndex;
141202 }else if( !HasRowid(pTab) ){
141203 pProbe = pTab->pIndex;
141204 }else{
141205 /* There is no INDEXED BY clause. Create a fake Index object in local
141206 ** variable sPk to represent the rowid primary key index. Make this
141207 ** fake index the first in a chain of Index objects with all of the real
141208 ** indices to follow */
141209 Index *pFirst; /* First of real indices on the table */
141210 memset(&sPk, 0, sizeof(Index));
141211 sPk.nKeyCol = 1;
141212 sPk.nColumn = 1;
141213 sPk.aiColumn = &aiColumnPk;
141214 sPk.aiRowLogEst = aiRowEstPk;
141215 sPk.onError = OE_Replace;
141216 sPk.pTable = pTab;
141217 sPk.szIdxRow = pTab->szTabRow;
141218 aiRowEstPk[0] = pTab->nRowLogEst;
141219 aiRowEstPk[1] = 0;
141220 pFirst = pSrc->pTab->pIndex;
141221 if( pSrc->fg.notIndexed==0 ){
141222 /* The real indices of the table are only considered if the
141223 ** NOT INDEXED qualifier is omitted from the FROM clause */
141224 sPk.pNext = pFirst;
141225 }
141226 pProbe = &sPk;
141227 }
141228 rSize = pTab->nRowLogEst;
141229 rLogSize = estLog(rSize);
141230
141231#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
141232 /* Automatic indexes */
141233 if( !pBuilder->pOrSet /* Not part of an OR optimization */
141234 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
141235 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
141236 && pSrc->pIBIndex==0 /* Has no INDEXED BY clause */
141237 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
141238 && HasRowid(pTab) /* Not WITHOUT ROWID table. (FIXME: Why not?) */
141239 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
141240 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
141241 ){
141242 /* Generate auto-index WhereLoops */
141243 WhereTerm *pTerm;
141244 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
141245 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
141246 if( pTerm->prereqRight & pNew->maskSelf ) continue;
141247 if( termCanDriveIndex(pTerm, pSrc, 0) ){
141248 pNew->u.btree.nEq = 1;
141249 pNew->nSkip = 0;
141250 pNew->u.btree.pIndex = 0;
141251 pNew->nLTerm = 1;
141252 pNew->aLTerm[0] = pTerm;
141253 /* TUNING: One-time cost for computing the automatic index is
141254 ** estimated to be X*N*log2(N) where N is the number of rows in
141255 ** the table being indexed and where X is 7 (LogEst=28) for normal
141256 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
141257 ** of X is smaller for views and subqueries so that the query planner
141258 ** will be more aggressive about generating automatic indexes for
141259 ** those objects, since there is no opportunity to add schema
141260 ** indexes on subqueries and views. */
141261 pNew->rSetup = rLogSize + rSize;
141262 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
141263 pNew->rSetup += 28;
141264 }else{
141265 pNew->rSetup -= 10;
141266 }
141267 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
141268 if( pNew->rSetup<0 ) pNew->rSetup = 0;
141269 /* TUNING: Each index lookup yields 20 rows in the table. This
141270 ** is more than the usual guess of 10 rows, since we have no way
141271 ** of knowing how selective the index will ultimately be. It would
141272 ** not be unreasonable to make this value much larger. */
141273 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
141274 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
141275 pNew->wsFlags = WHERE_AUTO_INDEX;
141276 pNew->prereq = mPrereq | pTerm->prereqRight;
141277 rc = whereLoopInsert(pBuilder, pNew);
141278 }
141279 }
141280 }
141281#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
141282
141283 /* Loop over all indices. If there was an INDEXED BY clause, then only
141284 ** consider index pProbe. */
141285 for(; rc==SQLITE_OK && pProbe;
141286 pProbe=(pSrc->pIBIndex ? 0 : pProbe->pNext), iSortIdx++
141287 ){
141288 if( pProbe->pPartIdxWhere!=0
141289 && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
141290 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
141291 continue; /* Partial index inappropriate for this query */
141292 }
141293 if( pProbe->bNoQuery ) continue;
141294 rSize = pProbe->aiRowLogEst[0];
141295 pNew->u.btree.nEq = 0;
141296 pNew->u.btree.nBtm = 0;
141297 pNew->u.btree.nTop = 0;
141298 pNew->nSkip = 0;
141299 pNew->nLTerm = 0;
141300 pNew->iSortIdx = 0;
141301 pNew->rSetup = 0;
141302 pNew->prereq = mPrereq;
141303 pNew->nOut = rSize;
141304 pNew->u.btree.pIndex = pProbe;
141305 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
141306 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
141307 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
141308 if( pProbe->tnum<=0 ){
141309 /* Integer primary key index */
141310 pNew->wsFlags = WHERE_IPK;
141311
141312 /* Full table scan */
141313 pNew->iSortIdx = b ? iSortIdx : 0;
141314 /* TUNING: Cost of full table scan is (N*3.0). */
141315 pNew->rRun = rSize + 16;
141316 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
141317 whereLoopOutputAdjust(pWC, pNew, rSize);
141318 rc = whereLoopInsert(pBuilder, pNew);
141319 pNew->nOut = rSize;
141320 if( rc ) break;
141321 }else{
141322 Bitmask m;
141323 if( pProbe->isCovering ){
141324 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
141325 m = 0;
141326 }else{
141327 m = pSrc->colUsed & pProbe->colNotIdxed;
141328 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
141329 }
141330
141331 /* Full scan via index */
141332 if( b
141333 || !HasRowid(pTab)
141334 || pProbe->pPartIdxWhere!=0
141335 || ( m==0
141336 && pProbe->bUnordered==0
141337 && (pProbe->szIdxRow<pTab->szTabRow)
141338 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
141339 && sqlite3GlobalConfig.bUseCis
141340 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
141341 )
141342 ){
141343 pNew->iSortIdx = b ? iSortIdx : 0;
141344
141345 /* The cost of visiting the index rows is N*K, where K is
141346 ** between 1.1 and 3.0, depending on the relative sizes of the
141347 ** index and table rows. */
141348 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
141349 if( m!=0 ){
141350 /* If this is a non-covering index scan, add in the cost of
141351 ** doing table lookups. The cost will be 3x the number of
141352 ** lookups. Take into account WHERE clause terms that can be
141353 ** satisfied using just the index, and that do not require a
141354 ** table lookup. */
141355 LogEst nLookup = rSize + 16; /* Base cost: N*3 */
141356 int ii;
141357 int iCur = pSrc->iCursor;
141358 WhereClause *pWC2 = &pWInfo->sWC;
141359 for(ii=0; ii<pWC2->nTerm; ii++){
141360 WhereTerm *pTerm = &pWC2->a[ii];
141361 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
141362 break;
141363 }
141364 /* pTerm can be evaluated using just the index. So reduce
141365 ** the expected number of table lookups accordingly */
141366 if( pTerm->truthProb<=0 ){
141367 nLookup += pTerm->truthProb;
141368 }else{
141369 nLookup--;
141370 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
141371 }
141372 }
141373
141374 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
141375 }
141376 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
141377 whereLoopOutputAdjust(pWC, pNew, rSize);
141378 rc = whereLoopInsert(pBuilder, pNew);
141379 pNew->nOut = rSize;
141380 if( rc ) break;
141381 }
141382 }
141383
141384 pBuilder->bldFlags = 0;
141385 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
141386 if( pBuilder->bldFlags==SQLITE_BLDF_INDEXED ){
141387 /* If a non-unique index is used, or if a prefix of the key for
141388 ** unique index is used (making the index functionally non-unique)
141389 ** then the sqlite_stat1 data becomes important for scoring the
141390 ** plan */
141391 pTab->tabFlags |= TF_StatsUsed;
141392 }
141393#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
141394 sqlite3Stat4ProbeFree(pBuilder->pRec);
141395 pBuilder->nRecValid = 0;
141396 pBuilder->pRec = 0;
141397#endif
141398 }
141399 return rc;
141400}
141401
141402#ifndef SQLITE_OMIT_VIRTUALTABLE
141403
141404/*
141405** Argument pIdxInfo is already populated with all constraints that may
141406** be used by the virtual table identified by pBuilder->pNew->iTab. This
141407** function marks a subset of those constraints usable, invokes the
141408** xBestIndex method and adds the returned plan to pBuilder.
141409**
141410** A constraint is marked usable if:
141411**
141412** * Argument mUsable indicates that its prerequisites are available, and
141413**
141414** * It is not one of the operators specified in the mExclude mask passed
141415** as the fourth argument (which in practice is either WO_IN or 0).
141416**
141417** Argument mPrereq is a mask of tables that must be scanned before the
141418** virtual table in question. These are added to the plans prerequisites
141419** before it is added to pBuilder.
141420**
141421** Output parameter *pbIn is set to true if the plan added to pBuilder
141422** uses one or more WO_IN terms, or false otherwise.
141423*/
141424static int whereLoopAddVirtualOne(
141425 WhereLoopBuilder *pBuilder,
141426 Bitmask mPrereq, /* Mask of tables that must be used. */
141427 Bitmask mUsable, /* Mask of usable tables */
141428 u16 mExclude, /* Exclude terms using these operators */
141429 sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */
141430 u16 mNoOmit, /* Do not omit these constraints */
141431 int *pbIn /* OUT: True if plan uses an IN(...) op */
141432){
141433 WhereClause *pWC = pBuilder->pWC;
141434 struct sqlite3_index_constraint *pIdxCons;
141435 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
141436 int i;
141437 int mxTerm;
141438 int rc = SQLITE_OK;
141439 WhereLoop *pNew = pBuilder->pNew;
141440 Parse *pParse = pBuilder->pWInfo->pParse;
141441 struct SrcList_item *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
141442 int nConstraint = pIdxInfo->nConstraint;
141443
141444 assert( (mUsable & mPrereq)==mPrereq );
141445 *pbIn = 0;
141446 pNew->prereq = mPrereq;
141447
141448 /* Set the usable flag on the subset of constraints identified by
141449 ** arguments mUsable and mExclude. */
141450 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
141451 for(i=0; i<nConstraint; i++, pIdxCons++){
141452 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
141453 pIdxCons->usable = 0;
141454 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
141455 && (pTerm->eOperator & mExclude)==0
141456 ){
141457 pIdxCons->usable = 1;
141458 }
141459 }
141460
141461 /* Initialize the output fields of the sqlite3_index_info structure */
141462 memset(pUsage, 0, sizeof(pUsage[0])*nConstraint);
141463 assert( pIdxInfo->needToFreeIdxStr==0 );
141464 pIdxInfo->idxStr = 0;
141465 pIdxInfo->idxNum = 0;
141466 pIdxInfo->orderByConsumed = 0;
141467 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
141468 pIdxInfo->estimatedRows = 25;
141469 pIdxInfo->idxFlags = 0;
141470 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
141471
141472 /* Invoke the virtual table xBestIndex() method */
141473 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
141474 if( rc ) return rc;
141475
141476 mxTerm = -1;
141477 assert( pNew->nLSlot>=nConstraint );
141478 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
141479 pNew->u.vtab.omitMask = 0;
141480 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
141481 for(i=0; i<nConstraint; i++, pIdxCons++){
141482 int iTerm;
141483 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
141484 WhereTerm *pTerm;
141485 int j = pIdxCons->iTermOffset;
141486 if( iTerm>=nConstraint
141487 || j<0
141488 || j>=pWC->nTerm
141489 || pNew->aLTerm[iTerm]!=0
141490 || pIdxCons->usable==0
141491 ){
141492 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
141493 testcase( pIdxInfo->needToFreeIdxStr );
141494 return SQLITE_ERROR;
141495 }
141496 testcase( iTerm==nConstraint-1 );
141497 testcase( j==0 );
141498 testcase( j==pWC->nTerm-1 );
141499 pTerm = &pWC->a[j];
141500 pNew->prereq |= pTerm->prereqRight;
141501 assert( iTerm<pNew->nLSlot );
141502 pNew->aLTerm[iTerm] = pTerm;
141503 if( iTerm>mxTerm ) mxTerm = iTerm;
141504 testcase( iTerm==15 );
141505 testcase( iTerm==16 );
141506 if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
141507 if( (pTerm->eOperator & WO_IN)!=0 ){
141508 /* A virtual table that is constrained by an IN clause may not
141509 ** consume the ORDER BY clause because (1) the order of IN terms
141510 ** is not necessarily related to the order of output terms and
141511 ** (2) Multiple outputs from a single IN value will not merge
141512 ** together. */
141513 pIdxInfo->orderByConsumed = 0;
141514 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
141515 *pbIn = 1; assert( (mExclude & WO_IN)==0 );
141516 }
141517 }
141518 }
141519 pNew->u.vtab.omitMask &= ~mNoOmit;
141520
141521 pNew->nLTerm = mxTerm+1;
141522 for(i=0; i<=mxTerm; i++){
141523 if( pNew->aLTerm[i]==0 ){
141524 /* The non-zero argvIdx values must be contiguous. Raise an
141525 ** error if they are not */
141526 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
141527 testcase( pIdxInfo->needToFreeIdxStr );
141528 return SQLITE_ERROR;
141529 }
141530 }
141531 assert( pNew->nLTerm<=pNew->nLSlot );
141532 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
141533 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
141534 pIdxInfo->needToFreeIdxStr = 0;
141535 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
141536 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
141537 pIdxInfo->nOrderBy : 0);
141538 pNew->rSetup = 0;
141539 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
141540 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
141541
141542 /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
141543 ** that the scan will visit at most one row. Clear it otherwise. */
141544 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
141545 pNew->wsFlags |= WHERE_ONEROW;
141546 }else{
141547 pNew->wsFlags &= ~WHERE_ONEROW;
141548 }
141549 rc = whereLoopInsert(pBuilder, pNew);
141550 if( pNew->u.vtab.needFree ){
141551 sqlite3_free(pNew->u.vtab.idxStr);
141552 pNew->u.vtab.needFree = 0;
141553 }
141554 WHERETRACE(0xffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
141555 *pbIn, (sqlite3_uint64)mPrereq,
141556 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
141557
141558 return rc;
141559}
141560
141561/*
141562** If this function is invoked from within an xBestIndex() callback, it
141563** returns a pointer to a buffer containing the name of the collation
141564** sequence associated with element iCons of the sqlite3_index_info.aConstraint
141565** array. Or, if iCons is out of range or there is no active xBestIndex
141566** call, return NULL.
141567*/
141568SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
141569 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
141570 const char *zRet = 0;
141571 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
141572 CollSeq *pC = 0;
141573 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
141574 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
141575 if( pX->pLeft ){
141576 pC = sqlite3BinaryCompareCollSeq(pHidden->pParse, pX->pLeft, pX->pRight);
141577 }
141578 zRet = (pC ? pC->zName : sqlite3StrBINARY);
141579 }
141580 return zRet;
141581}
141582
141583/*
141584** Add all WhereLoop objects for a table of the join identified by
141585** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
141586**
141587** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
141588** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
141589** entries that occur before the virtual table in the FROM clause and are
141590** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
141591** mUnusable mask contains all FROM clause entries that occur after the
141592** virtual table and are separated from it by at least one LEFT or
141593** CROSS JOIN.
141594**
141595** For example, if the query were:
141596**
141597** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
141598**
141599** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6).
141600**
141601** All the tables in mPrereq must be scanned before the current virtual
141602** table. So any terms for which all prerequisites are satisfied by
141603** mPrereq may be specified as "usable" in all calls to xBestIndex.
141604** Conversely, all tables in mUnusable must be scanned after the current
141605** virtual table, so any terms for which the prerequisites overlap with
141606** mUnusable should always be configured as "not-usable" for xBestIndex.
141607*/
141608static int whereLoopAddVirtual(
141609 WhereLoopBuilder *pBuilder, /* WHERE clause information */
141610 Bitmask mPrereq, /* Tables that must be scanned before this one */
141611 Bitmask mUnusable /* Tables that must be scanned after this one */
141612){
141613 int rc = SQLITE_OK; /* Return code */
141614 WhereInfo *pWInfo; /* WHERE analysis context */
141615 Parse *pParse; /* The parsing context */
141616 WhereClause *pWC; /* The WHERE clause */
141617 struct SrcList_item *pSrc; /* The FROM clause term to search */
141618 sqlite3_index_info *p; /* Object to pass to xBestIndex() */
141619 int nConstraint; /* Number of constraints in p */
141620 int bIn; /* True if plan uses IN(...) operator */
141621 WhereLoop *pNew;
141622 Bitmask mBest; /* Tables used by best possible plan */
141623 u16 mNoOmit;
141624
141625 assert( (mPrereq & mUnusable)==0 );
141626 pWInfo = pBuilder->pWInfo;
141627 pParse = pWInfo->pParse;
141628 pWC = pBuilder->pWC;
141629 pNew = pBuilder->pNew;
141630 pSrc = &pWInfo->pTabList->a[pNew->iTab];
141631 assert( IsVirtual(pSrc->pTab) );
141632 p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy,
141633 &mNoOmit);
141634 if( p==0 ) return SQLITE_NOMEM_BKPT;
141635 pNew->rSetup = 0;
141636 pNew->wsFlags = WHERE_VIRTUALTABLE;
141637 pNew->nLTerm = 0;
141638 pNew->u.vtab.needFree = 0;
141639 nConstraint = p->nConstraint;
141640 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
141641 sqlite3DbFree(pParse->db, p);
141642 return SQLITE_NOMEM_BKPT;
141643 }
141644
141645 /* First call xBestIndex() with all constraints usable. */
141646 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
141647 WHERETRACE(0x40, (" VirtualOne: all usable\n"));
141648 rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn);
141649
141650 /* If the call to xBestIndex() with all terms enabled produced a plan
141651 ** that does not require any source tables (IOW: a plan with mBest==0),
141652 ** then there is no point in making any further calls to xBestIndex()
141653 ** since they will all return the same result (if the xBestIndex()
141654 ** implementation is sane). */
141655 if( rc==SQLITE_OK && (mBest = (pNew->prereq & ~mPrereq))!=0 ){
141656 int seenZero = 0; /* True if a plan with no prereqs seen */
141657 int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */
141658 Bitmask mPrev = 0;
141659 Bitmask mBestNoIn = 0;
141660
141661 /* If the plan produced by the earlier call uses an IN(...) term, call
141662 ** xBestIndex again, this time with IN(...) terms disabled. */
141663 if( bIn ){
141664 WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n"));
141665 rc = whereLoopAddVirtualOne(
141666 pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn);
141667 assert( bIn==0 );
141668 mBestNoIn = pNew->prereq & ~mPrereq;
141669 if( mBestNoIn==0 ){
141670 seenZero = 1;
141671 seenZeroNoIN = 1;
141672 }
141673 }
141674
141675 /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq)
141676 ** in the set of terms that apply to the current virtual table. */
141677 while( rc==SQLITE_OK ){
141678 int i;
141679 Bitmask mNext = ALLBITS;
141680 assert( mNext>0 );
141681 for(i=0; i<nConstraint; i++){
141682 Bitmask mThis = (
141683 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
141684 );
141685 if( mThis>mPrev && mThis<mNext ) mNext = mThis;
141686 }
141687 mPrev = mNext;
141688 if( mNext==ALLBITS ) break;
141689 if( mNext==mBest || mNext==mBestNoIn ) continue;
141690 WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
141691 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
141692 rc = whereLoopAddVirtualOne(
141693 pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn);
141694 if( pNew->prereq==mPrereq ){
141695 seenZero = 1;
141696 if( bIn==0 ) seenZeroNoIN = 1;
141697 }
141698 }
141699
141700 /* If the calls to xBestIndex() in the above loop did not find a plan
141701 ** that requires no source tables at all (i.e. one guaranteed to be
141702 ** usable), make a call here with all source tables disabled */
141703 if( rc==SQLITE_OK && seenZero==0 ){
141704 WHERETRACE(0x40, (" VirtualOne: all disabled\n"));
141705 rc = whereLoopAddVirtualOne(
141706 pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn);
141707 if( bIn==0 ) seenZeroNoIN = 1;
141708 }
141709
141710 /* If the calls to xBestIndex() have so far failed to find a plan
141711 ** that requires no source tables at all and does not use an IN(...)
141712 ** operator, make a final call to obtain one here. */
141713 if( rc==SQLITE_OK && seenZeroNoIN==0 ){
141714 WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n"));
141715 rc = whereLoopAddVirtualOne(
141716 pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn);
141717 }
141718 }
141719
141720 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
141721 sqlite3DbFreeNN(pParse->db, p);
141722 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
141723 return rc;
141724}
141725#endif /* SQLITE_OMIT_VIRTUALTABLE */
141726
141727/*
141728** Add WhereLoop entries to handle OR terms. This works for either
141729** btrees or virtual tables.
141730*/
141731static int whereLoopAddOr(
141732 WhereLoopBuilder *pBuilder,
141733 Bitmask mPrereq,
141734 Bitmask mUnusable
141735){
141736 WhereInfo *pWInfo = pBuilder->pWInfo;
141737 WhereClause *pWC;
141738 WhereLoop *pNew;
141739 WhereTerm *pTerm, *pWCEnd;
141740 int rc = SQLITE_OK;
141741 int iCur;
141742 WhereClause tempWC;
141743 WhereLoopBuilder sSubBuild;
141744 WhereOrSet sSum, sCur;
141745 struct SrcList_item *pItem;
141746
141747 pWC = pBuilder->pWC;
141748 pWCEnd = pWC->a + pWC->nTerm;
141749 pNew = pBuilder->pNew;
141750 memset(&sSum, 0, sizeof(sSum));
141751 pItem = pWInfo->pTabList->a + pNew->iTab;
141752 iCur = pItem->iCursor;
141753
141754 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
141755 if( (pTerm->eOperator & WO_OR)!=0
141756 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
141757 ){
141758 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
141759 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
141760 WhereTerm *pOrTerm;
141761 int once = 1;
141762 int i, j;
141763
141764 sSubBuild = *pBuilder;
141765 sSubBuild.pOrderBy = 0;
141766 sSubBuild.pOrSet = &sCur;
141767
141768 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
141769 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
141770 if( (pOrTerm->eOperator & WO_AND)!=0 ){
141771 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
141772 }else if( pOrTerm->leftCursor==iCur ){
141773 tempWC.pWInfo = pWC->pWInfo;
141774 tempWC.pOuter = pWC;
141775 tempWC.op = TK_AND;
141776 tempWC.nTerm = 1;
141777 tempWC.a = pOrTerm;
141778 sSubBuild.pWC = &tempWC;
141779 }else{
141780 continue;
141781 }
141782 sCur.n = 0;
141783#ifdef WHERETRACE_ENABLED
141784 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
141785 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
141786 if( sqlite3WhereTrace & 0x400 ){
141787 sqlite3WhereClausePrint(sSubBuild.pWC);
141788 }
141789#endif
141790#ifndef SQLITE_OMIT_VIRTUALTABLE
141791 if( IsVirtual(pItem->pTab) ){
141792 rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable);
141793 }else
141794#endif
141795 {
141796 rc = whereLoopAddBtree(&sSubBuild, mPrereq);
141797 }
141798 if( rc==SQLITE_OK ){
141799 rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable);
141800 }
141801 assert( rc==SQLITE_OK || sCur.n==0 );
141802 if( sCur.n==0 ){
141803 sSum.n = 0;
141804 break;
141805 }else if( once ){
141806 whereOrMove(&sSum, &sCur);
141807 once = 0;
141808 }else{
141809 WhereOrSet sPrev;
141810 whereOrMove(&sPrev, &sSum);
141811 sSum.n = 0;
141812 for(i=0; i<sPrev.n; i++){
141813 for(j=0; j<sCur.n; j++){
141814 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
141815 sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
141816 sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
141817 }
141818 }
141819 }
141820 }
141821 pNew->nLTerm = 1;
141822 pNew->aLTerm[0] = pTerm;
141823 pNew->wsFlags = WHERE_MULTI_OR;
141824 pNew->rSetup = 0;
141825 pNew->iSortIdx = 0;
141826 memset(&pNew->u, 0, sizeof(pNew->u));
141827 for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
141828 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
141829 ** of all sub-scans required by the OR-scan. However, due to rounding
141830 ** errors, it may be that the cost of the OR-scan is equal to its
141831 ** most expensive sub-scan. Add the smallest possible penalty
141832 ** (equivalent to multiplying the cost by 1.07) to ensure that
141833 ** this does not happen. Otherwise, for WHERE clauses such as the
141834 ** following where there is an index on "y":
141835 **
141836 ** WHERE likelihood(x=?, 0.99) OR y=?
141837 **
141838 ** the planner may elect to "OR" together a full-table scan and an
141839 ** index lookup. And other similarly odd results. */
141840 pNew->rRun = sSum.a[i].rRun + 1;
141841 pNew->nOut = sSum.a[i].nOut;
141842 pNew->prereq = sSum.a[i].prereq;
141843 rc = whereLoopInsert(pBuilder, pNew);
141844 }
141845 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
141846 }
141847 }
141848 return rc;
141849}
141850
141851/*
141852** Add all WhereLoop objects for all tables
141853*/
141854static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
141855 WhereInfo *pWInfo = pBuilder->pWInfo;
141856 Bitmask mPrereq = 0;
141857 Bitmask mPrior = 0;
141858 int iTab;
141859 SrcList *pTabList = pWInfo->pTabList;
141860 struct SrcList_item *pItem;
141861 struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
141862 sqlite3 *db = pWInfo->pParse->db;
141863 int rc = SQLITE_OK;
141864 WhereLoop *pNew;
141865 u8 priorJointype = 0;
141866
141867 /* Loop over the tables in the join, from left to right */
141868 pNew = pBuilder->pNew;
141869 whereLoopInit(pNew);
141870 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
141871 Bitmask mUnusable = 0;
141872 pNew->iTab = iTab;
141873 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
141874 if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
141875 /* This condition is true when pItem is the FROM clause term on the
141876 ** right-hand-side of a LEFT or CROSS JOIN. */
141877 mPrereq = mPrior;
141878 }
141879 priorJointype = pItem->fg.jointype;
141880#ifndef SQLITE_OMIT_VIRTUALTABLE
141881 if( IsVirtual(pItem->pTab) ){
141882 struct SrcList_item *p;
141883 for(p=&pItem[1]; p<pEnd; p++){
141884 if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){
141885 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
141886 }
141887 }
141888 rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable);
141889 }else
141890#endif /* SQLITE_OMIT_VIRTUALTABLE */
141891 {
141892 rc = whereLoopAddBtree(pBuilder, mPrereq);
141893 }
141894 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
141895 rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
141896 }
141897 mPrior |= pNew->maskSelf;
141898 if( rc || db->mallocFailed ) break;
141899 }
141900
141901 whereLoopClear(db, pNew);
141902 return rc;
141903}
141904
141905/*
141906** Examine a WherePath (with the addition of the extra WhereLoop of the 6th
141907** parameters) to see if it outputs rows in the requested ORDER BY
141908** (or GROUP BY) without requiring a separate sort operation. Return N:
141909**
141910** N>0: N terms of the ORDER BY clause are satisfied
141911** N==0: No terms of the ORDER BY clause are satisfied
141912** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
141913**
141914** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
141915** strict. With GROUP BY and DISTINCT the only requirement is that
141916** equivalent rows appear immediately adjacent to one another. GROUP BY
141917** and DISTINCT do not require rows to appear in any particular order as long
141918** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT
141919** the pOrderBy terms can be matched in any order. With ORDER BY, the
141920** pOrderBy terms must be matched in strict left-to-right order.
141921*/
141922static i8 wherePathSatisfiesOrderBy(
141923 WhereInfo *pWInfo, /* The WHERE clause */
141924 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */
141925 WherePath *pPath, /* The WherePath to check */
141926 u16 wctrlFlags, /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */
141927 u16 nLoop, /* Number of entries in pPath->aLoop[] */
141928 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
141929 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */
141930){
141931 u8 revSet; /* True if rev is known */
141932 u8 rev; /* Composite sort order */
141933 u8 revIdx; /* Index sort order */
141934 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
141935 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */
141936 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */
141937 u16 eqOpMask; /* Allowed equality operators */
141938 u16 nKeyCol; /* Number of key columns in pIndex */
141939 u16 nColumn; /* Total number of ordered columns in the index */
141940 u16 nOrderBy; /* Number terms in the ORDER BY clause */
141941 int iLoop; /* Index of WhereLoop in pPath being processed */
141942 int i, j; /* Loop counters */
141943 int iCur; /* Cursor number for current WhereLoop */
141944 int iColumn; /* A column number within table iCur */
141945 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
141946 WhereTerm *pTerm; /* A single term of the WHERE clause */
141947 Expr *pOBExpr; /* An expression from the ORDER BY clause */
141948 CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */
141949 Index *pIndex; /* The index associated with pLoop */
141950 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
141951 Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */
141952 Bitmask obDone; /* Mask of all ORDER BY terms */
141953 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
141954 Bitmask ready; /* Mask of inner loops */
141955
141956 /*
141957 ** We say the WhereLoop is "one-row" if it generates no more than one
141958 ** row of output. A WhereLoop is one-row if all of the following are true:
141959 ** (a) All index columns match with WHERE_COLUMN_EQ.
141960 ** (b) The index is unique
141961 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
141962 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
141963 **
141964 ** We say the WhereLoop is "order-distinct" if the set of columns from
141965 ** that WhereLoop that are in the ORDER BY clause are different for every
141966 ** row of the WhereLoop. Every one-row WhereLoop is automatically
141967 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
141968 ** is not order-distinct. To be order-distinct is not quite the same as being
141969 ** UNIQUE since a UNIQUE column or index can have multiple rows that
141970 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
141971 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
141972 **
141973 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
141974 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
141975 ** automatically order-distinct.
141976 */
141977
141978 assert( pOrderBy!=0 );
141979 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
141980
141981 nOrderBy = pOrderBy->nExpr;
141982 testcase( nOrderBy==BMS-1 );
141983 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
141984 isOrderDistinct = 1;
141985 obDone = MASKBIT(nOrderBy)-1;
141986 orderDistinctMask = 0;
141987 ready = 0;
141988 eqOpMask = WO_EQ | WO_IS | WO_ISNULL;
141989 if( wctrlFlags & WHERE_ORDERBY_LIMIT ) eqOpMask |= WO_IN;
141990 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
141991 if( iLoop>0 ) ready |= pLoop->maskSelf;
141992 if( iLoop<nLoop ){
141993 pLoop = pPath->aLoop[iLoop];
141994 if( wctrlFlags & WHERE_ORDERBY_LIMIT ) continue;
141995 }else{
141996 pLoop = pLast;
141997 }
141998 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
141999 if( pLoop->u.vtab.isOrdered ) obSat = obDone;
142000 break;
142001 }else{
142002 pLoop->u.btree.nIdxCol = 0;
142003 }
142004 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
142005
142006 /* Mark off any ORDER BY term X that is a column in the table of
142007 ** the current loop for which there is term in the WHERE
142008 ** clause of the form X IS NULL or X=? that reference only outer
142009 ** loops.
142010 */
142011 for(i=0; i<nOrderBy; i++){
142012 if( MASKBIT(i) & obSat ) continue;
142013 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
142014 if( pOBExpr->op!=TK_COLUMN ) continue;
142015 if( pOBExpr->iTable!=iCur ) continue;
142016 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
142017 ~ready, eqOpMask, 0);
142018 if( pTerm==0 ) continue;
142019 if( pTerm->eOperator==WO_IN ){
142020 /* IN terms are only valid for sorting in the ORDER BY LIMIT
142021 ** optimization, and then only if they are actually used
142022 ** by the query plan */
142023 assert( wctrlFlags & WHERE_ORDERBY_LIMIT );
142024 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
142025 if( j>=pLoop->nLTerm ) continue;
142026 }
142027 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
142028 if( sqlite3ExprCollSeqMatch(pWInfo->pParse,
142029 pOrderBy->a[i].pExpr, pTerm->pExpr)==0 ){
142030 continue;
142031 }
142032 testcase( pTerm->pExpr->op==TK_IS );
142033 }
142034 obSat |= MASKBIT(i);
142035 }
142036
142037 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
142038 if( pLoop->wsFlags & WHERE_IPK ){
142039 pIndex = 0;
142040 nKeyCol = 0;
142041 nColumn = 1;
142042 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
142043 return 0;
142044 }else{
142045 nKeyCol = pIndex->nKeyCol;
142046 nColumn = pIndex->nColumn;
142047 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
142048 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
142049 || !HasRowid(pIndex->pTable));
142050 isOrderDistinct = IsUniqueIndex(pIndex);
142051 }
142052
142053 /* Loop through all columns of the index and deal with the ones
142054 ** that are not constrained by == or IN.
142055 */
142056 rev = revSet = 0;
142057 distinctColumns = 0;
142058 for(j=0; j<nColumn; j++){
142059 u8 bOnce = 1; /* True to run the ORDER BY search loop */
142060
142061 assert( j>=pLoop->u.btree.nEq
142062 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
142063 );
142064 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
142065 u16 eOp = pLoop->aLTerm[j]->eOperator;
142066
142067 /* Skip over == and IS and ISNULL terms. (Also skip IN terms when
142068 ** doing WHERE_ORDERBY_LIMIT processing).
142069 **
142070 ** If the current term is a column of an ((?,?) IN (SELECT...))
142071 ** expression for which the SELECT returns more than one column,
142072 ** check that it is the only column used by this loop. Otherwise,
142073 ** if it is one of two or more, none of the columns can be
142074 ** considered to match an ORDER BY term. */
142075 if( (eOp & eqOpMask)!=0 ){
142076 if( eOp & WO_ISNULL ){
142077 testcase( isOrderDistinct );
142078 isOrderDistinct = 0;
142079 }
142080 continue;
142081 }else if( ALWAYS(eOp & WO_IN) ){
142082 /* ALWAYS() justification: eOp is an equality operator due to the
142083 ** j<pLoop->u.btree.nEq constraint above. Any equality other
142084 ** than WO_IN is captured by the previous "if". So this one
142085 ** always has to be WO_IN. */
142086 Expr *pX = pLoop->aLTerm[j]->pExpr;
142087 for(i=j+1; i<pLoop->u.btree.nEq; i++){
142088 if( pLoop->aLTerm[i]->pExpr==pX ){
142089 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
142090 bOnce = 0;
142091 break;
142092 }
142093 }
142094 }
142095 }
142096
142097 /* Get the column number in the table (iColumn) and sort order
142098 ** (revIdx) for the j-th column of the index.
142099 */
142100 if( pIndex ){
142101 iColumn = pIndex->aiColumn[j];
142102 revIdx = pIndex->aSortOrder[j];
142103 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
142104 }else{
142105 iColumn = XN_ROWID;
142106 revIdx = 0;
142107 }
142108
142109 /* An unconstrained column that might be NULL means that this
142110 ** WhereLoop is not well-ordered
142111 */
142112 if( isOrderDistinct
142113 && iColumn>=0
142114 && j>=pLoop->u.btree.nEq
142115 && pIndex->pTable->aCol[iColumn].notNull==0
142116 ){
142117 isOrderDistinct = 0;
142118 }
142119
142120 /* Find the ORDER BY term that corresponds to the j-th column
142121 ** of the index and mark that ORDER BY term off
142122 */
142123 isMatch = 0;
142124 for(i=0; bOnce && i<nOrderBy; i++){
142125 if( MASKBIT(i) & obSat ) continue;
142126 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
142127 testcase( wctrlFlags & WHERE_GROUPBY );
142128 testcase( wctrlFlags & WHERE_DISTINCTBY );
142129 if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
142130 if( iColumn>=XN_ROWID ){
142131 if( pOBExpr->op!=TK_COLUMN ) continue;
142132 if( pOBExpr->iTable!=iCur ) continue;
142133 if( pOBExpr->iColumn!=iColumn ) continue;
142134 }else{
142135 Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
142136 if( sqlite3ExprCompareSkip(pOBExpr, pIdxExpr, iCur) ){
142137 continue;
142138 }
142139 }
142140 if( iColumn!=XN_ROWID ){
142141 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
142142 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
142143 }
142144 pLoop->u.btree.nIdxCol = j+1;
142145 isMatch = 1;
142146 break;
142147 }
142148 if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){
142149 /* Make sure the sort order is compatible in an ORDER BY clause.
142150 ** Sort order is irrelevant for a GROUP BY clause. */
142151 if( revSet ){
142152 if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0;
142153 }else{
142154 rev = revIdx ^ pOrderBy->a[i].sortOrder;
142155 if( rev ) *pRevMask |= MASKBIT(iLoop);
142156 revSet = 1;
142157 }
142158 }
142159 if( isMatch ){
142160 if( iColumn==XN_ROWID ){
142161 testcase( distinctColumns==0 );
142162 distinctColumns = 1;
142163 }
142164 obSat |= MASKBIT(i);
142165 }else{
142166 /* No match found */
142167 if( j==0 || j<nKeyCol ){
142168 testcase( isOrderDistinct!=0 );
142169 isOrderDistinct = 0;
142170 }
142171 break;
142172 }
142173 } /* end Loop over all index columns */
142174 if( distinctColumns ){
142175 testcase( isOrderDistinct==0 );
142176 isOrderDistinct = 1;
142177 }
142178 } /* end-if not one-row */
142179
142180 /* Mark off any other ORDER BY terms that reference pLoop */
142181 if( isOrderDistinct ){
142182 orderDistinctMask |= pLoop->maskSelf;
142183 for(i=0; i<nOrderBy; i++){
142184 Expr *p;
142185 Bitmask mTerm;
142186 if( MASKBIT(i) & obSat ) continue;
142187 p = pOrderBy->a[i].pExpr;
142188 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
142189 if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
142190 if( (mTerm&~orderDistinctMask)==0 ){
142191 obSat |= MASKBIT(i);
142192 }
142193 }
142194 }
142195 } /* End the loop over all WhereLoops from outer-most down to inner-most */
142196 if( obSat==obDone ) return (i8)nOrderBy;
142197 if( !isOrderDistinct ){
142198 for(i=nOrderBy-1; i>0; i--){
142199 Bitmask m = MASKBIT(i) - 1;
142200 if( (obSat&m)==m ) return i;
142201 }
142202 return 0;
142203 }
142204 return -1;
142205}
142206
142207
142208/*
142209** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
142210** the planner assumes that the specified pOrderBy list is actually a GROUP
142211** BY clause - and so any order that groups rows as required satisfies the
142212** request.
142213**
142214** Normally, in this case it is not possible for the caller to determine
142215** whether or not the rows are really being delivered in sorted order, or
142216** just in some other order that provides the required grouping. However,
142217** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
142218** this function may be called on the returned WhereInfo object. It returns
142219** true if the rows really will be sorted in the specified order, or false
142220** otherwise.
142221**
142222** For example, assuming:
142223**
142224** CREATE INDEX i1 ON t1(x, Y);
142225**
142226** then
142227**
142228** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
142229** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
142230*/
142231SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){
142232 assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
142233 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
142234 return pWInfo->sorted;
142235}
142236
142237#ifdef WHERETRACE_ENABLED
142238/* For debugging use only: */
142239static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
142240 static char zName[65];
142241 int i;
142242 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
142243 if( pLast ) zName[i++] = pLast->cId;
142244 zName[i] = 0;
142245 return zName;
142246}
142247#endif
142248
142249/*
142250** Return the cost of sorting nRow rows, assuming that the keys have
142251** nOrderby columns and that the first nSorted columns are already in
142252** order.
142253*/
142254static LogEst whereSortingCost(
142255 WhereInfo *pWInfo,
142256 LogEst nRow,
142257 int nOrderBy,
142258 int nSorted
142259){
142260 /* TUNING: Estimated cost of a full external sort, where N is
142261 ** the number of rows to sort is:
142262 **
142263 ** cost = (3.0 * N * log(N)).
142264 **
142265 ** Or, if the order-by clause has X terms but only the last Y
142266 ** terms are out of order, then block-sorting will reduce the
142267 ** sorting cost to:
142268 **
142269 ** cost = (3.0 * N * log(N)) * (Y/X)
142270 **
142271 ** The (Y/X) term is implemented using stack variable rScale
142272 ** below. */
142273 LogEst rScale, rSortCost;
142274 assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
142275 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
142276 rSortCost = nRow + rScale + 16;
142277
142278 /* Multiple by log(M) where M is the number of output rows.
142279 ** Use the LIMIT for M if it is smaller */
142280 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
142281 nRow = pWInfo->iLimit;
142282 }
142283 rSortCost += estLog(nRow);
142284 return rSortCost;
142285}
142286
142287/*
142288** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
142289** attempts to find the lowest cost path that visits each WhereLoop
142290** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
142291**
142292** Assume that the total number of output rows that will need to be sorted
142293** will be nRowEst (in the 10*log2 representation). Or, ignore sorting
142294** costs if nRowEst==0.
142295**
142296** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
142297** error occurs.
142298*/
142299static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
142300 int mxChoice; /* Maximum number of simultaneous paths tracked */
142301 int nLoop; /* Number of terms in the join */
142302 Parse *pParse; /* Parsing context */
142303 sqlite3 *db; /* The database connection */
142304 int iLoop; /* Loop counter over the terms of the join */
142305 int ii, jj; /* Loop counters */
142306 int mxI = 0; /* Index of next entry to replace */
142307 int nOrderBy; /* Number of ORDER BY clause terms */
142308 LogEst mxCost = 0; /* Maximum cost of a set of paths */
142309 LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */
142310 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
142311 WherePath *aFrom; /* All nFrom paths at the previous level */
142312 WherePath *aTo; /* The nTo best paths at the current level */
142313 WherePath *pFrom; /* An element of aFrom[] that we are working on */
142314 WherePath *pTo; /* An element of aTo[] that we are working on */
142315 WhereLoop *pWLoop; /* One of the WhereLoop objects */
142316 WhereLoop **pX; /* Used to divy up the pSpace memory */
142317 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
142318 char *pSpace; /* Temporary memory used by this routine */
142319 int nSpace; /* Bytes of space allocated at pSpace */
142320
142321 pParse = pWInfo->pParse;
142322 db = pParse->db;
142323 nLoop = pWInfo->nLevel;
142324 /* TUNING: For simple queries, only the best path is tracked.
142325 ** For 2-way joins, the 5 best paths are followed.
142326 ** For joins of 3 or more tables, track the 10 best paths */
142327 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
142328 assert( nLoop<=pWInfo->pTabList->nSrc );
142329 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
142330
142331 /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
142332 ** case the purpose of this call is to estimate the number of rows returned
142333 ** by the overall query. Once this estimate has been obtained, the caller
142334 ** will invoke this function a second time, passing the estimate as the
142335 ** nRowEst parameter. */
142336 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
142337 nOrderBy = 0;
142338 }else{
142339 nOrderBy = pWInfo->pOrderBy->nExpr;
142340 }
142341
142342 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
142343 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
142344 nSpace += sizeof(LogEst) * nOrderBy;
142345 pSpace = sqlite3DbMallocRawNN(db, nSpace);
142346 if( pSpace==0 ) return SQLITE_NOMEM_BKPT;
142347 aTo = (WherePath*)pSpace;
142348 aFrom = aTo+mxChoice;
142349 memset(aFrom, 0, sizeof(aFrom[0]));
142350 pX = (WhereLoop**)(aFrom+mxChoice);
142351 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
142352 pFrom->aLoop = pX;
142353 }
142354 if( nOrderBy ){
142355 /* If there is an ORDER BY clause and it is not being ignored, set up
142356 ** space for the aSortCost[] array. Each element of the aSortCost array
142357 ** is either zero - meaning it has not yet been initialized - or the
142358 ** cost of sorting nRowEst rows of data where the first X terms of
142359 ** the ORDER BY clause are already in order, where X is the array
142360 ** index. */
142361 aSortCost = (LogEst*)pX;
142362 memset(aSortCost, 0, sizeof(LogEst) * nOrderBy);
142363 }
142364 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
142365 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
142366
142367 /* Seed the search with a single WherePath containing zero WhereLoops.
142368 **
142369 ** TUNING: Do not let the number of iterations go above 28. If the cost
142370 ** of computing an automatic index is not paid back within the first 28
142371 ** rows, then do not use the automatic index. */
142372 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
142373 nFrom = 1;
142374 assert( aFrom[0].isOrdered==0 );
142375 if( nOrderBy ){
142376 /* If nLoop is zero, then there are no FROM terms in the query. Since
142377 ** in this case the query may return a maximum of one row, the results
142378 ** are already in the requested order. Set isOrdered to nOrderBy to
142379 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
142380 ** -1, indicating that the result set may or may not be ordered,
142381 ** depending on the loops added to the current plan. */
142382 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
142383 }
142384
142385 /* Compute successively longer WherePaths using the previous generation
142386 ** of WherePaths as the basis for the next. Keep track of the mxChoice
142387 ** best paths at each generation */
142388 for(iLoop=0; iLoop<nLoop; iLoop++){
142389 nTo = 0;
142390 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
142391 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
142392 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
142393 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
142394 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
142395 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
142396 Bitmask maskNew; /* Mask of src visited by (..) */
142397 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
142398
142399 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
142400 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
142401 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
142402 /* Do not use an automatic index if the this loop is expected
142403 ** to run less than 1.25 times. It is tempting to also exclude
142404 ** automatic index usage on an outer loop, but sometimes an automatic
142405 ** index is useful in the outer loop of a correlated subquery. */
142406 assert( 10==sqlite3LogEst(2) );
142407 continue;
142408 }
142409
142410 /* At this point, pWLoop is a candidate to be the next loop.
142411 ** Compute its cost */
142412 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
142413 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
142414 nOut = pFrom->nRow + pWLoop->nOut;
142415 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
142416 if( isOrdered<0 ){
142417 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
142418 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
142419 iLoop, pWLoop, &revMask);
142420 }else{
142421 revMask = pFrom->revLoop;
142422 }
142423 if( isOrdered>=0 && isOrdered<nOrderBy ){
142424 if( aSortCost[isOrdered]==0 ){
142425 aSortCost[isOrdered] = whereSortingCost(
142426 pWInfo, nRowEst, nOrderBy, isOrdered
142427 );
142428 }
142429 /* TUNING: Add a small extra penalty (5) to sorting as an
142430 ** extra encouragment to the query planner to select a plan
142431 ** where the rows emerge in the correct order without any sorting
142432 ** required. */
142433 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]) + 5;
142434
142435 WHERETRACE(0x002,
142436 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
142437 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
142438 rUnsorted, rCost));
142439 }else{
142440 rCost = rUnsorted;
142441 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
142442 }
142443
142444 /* Check to see if pWLoop should be added to the set of
142445 ** mxChoice best-so-far paths.
142446 **
142447 ** First look for an existing path among best-so-far paths
142448 ** that covers the same set of loops and has the same isOrdered
142449 ** setting as the current path candidate.
142450 **
142451 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
142452 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
142453 ** of legal values for isOrdered, -1..64.
142454 */
142455 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
142456 if( pTo->maskLoop==maskNew
142457 && ((pTo->isOrdered^isOrdered)&0x80)==0
142458 ){
142459 testcase( jj==nTo-1 );
142460 break;
142461 }
142462 }
142463 if( jj>=nTo ){
142464 /* None of the existing best-so-far paths match the candidate. */
142465 if( nTo>=mxChoice
142466 && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
142467 ){
142468 /* The current candidate is no better than any of the mxChoice
142469 ** paths currently in the best-so-far buffer. So discard
142470 ** this candidate as not viable. */
142471#ifdef WHERETRACE_ENABLED /* 0x4 */
142472 if( sqlite3WhereTrace&0x4 ){
142473 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
142474 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
142475 isOrdered>=0 ? isOrdered+'0' : '?');
142476 }
142477#endif
142478 continue;
142479 }
142480 /* If we reach this points it means that the new candidate path
142481 ** needs to be added to the set of best-so-far paths. */
142482 if( nTo<mxChoice ){
142483 /* Increase the size of the aTo set by one */
142484 jj = nTo++;
142485 }else{
142486 /* New path replaces the prior worst to keep count below mxChoice */
142487 jj = mxI;
142488 }
142489 pTo = &aTo[jj];
142490#ifdef WHERETRACE_ENABLED /* 0x4 */
142491 if( sqlite3WhereTrace&0x4 ){
142492 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
142493 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
142494 isOrdered>=0 ? isOrdered+'0' : '?');
142495 }
142496#endif
142497 }else{
142498 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
142499 ** same set of loops and has the same isOrdered setting as the
142500 ** candidate path. Check to see if the candidate should replace
142501 ** pTo or if the candidate should be skipped.
142502 **
142503 ** The conditional is an expanded vector comparison equivalent to:
142504 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
142505 */
142506 if( pTo->rCost<rCost
142507 || (pTo->rCost==rCost
142508 && (pTo->nRow<nOut
142509 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
142510 )
142511 )
142512 ){
142513#ifdef WHERETRACE_ENABLED /* 0x4 */
142514 if( sqlite3WhereTrace&0x4 ){
142515 sqlite3DebugPrintf(
142516 "Skip %s cost=%-3d,%3d,%3d order=%c",
142517 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
142518 isOrdered>=0 ? isOrdered+'0' : '?');
142519 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
142520 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
142521 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
142522 }
142523#endif
142524 /* Discard the candidate path from further consideration */
142525 testcase( pTo->rCost==rCost );
142526 continue;
142527 }
142528 testcase( pTo->rCost==rCost+1 );
142529 /* Control reaches here if the candidate path is better than the
142530 ** pTo path. Replace pTo with the candidate. */
142531#ifdef WHERETRACE_ENABLED /* 0x4 */
142532 if( sqlite3WhereTrace&0x4 ){
142533 sqlite3DebugPrintf(
142534 "Update %s cost=%-3d,%3d,%3d order=%c",
142535 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
142536 isOrdered>=0 ? isOrdered+'0' : '?');
142537 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
142538 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
142539 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
142540 }
142541#endif
142542 }
142543 /* pWLoop is a winner. Add it to the set of best so far */
142544 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
142545 pTo->revLoop = revMask;
142546 pTo->nRow = nOut;
142547 pTo->rCost = rCost;
142548 pTo->rUnsorted = rUnsorted;
142549 pTo->isOrdered = isOrdered;
142550 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
142551 pTo->aLoop[iLoop] = pWLoop;
142552 if( nTo>=mxChoice ){
142553 mxI = 0;
142554 mxCost = aTo[0].rCost;
142555 mxUnsorted = aTo[0].nRow;
142556 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
142557 if( pTo->rCost>mxCost
142558 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
142559 ){
142560 mxCost = pTo->rCost;
142561 mxUnsorted = pTo->rUnsorted;
142562 mxI = jj;
142563 }
142564 }
142565 }
142566 }
142567 }
142568
142569#ifdef WHERETRACE_ENABLED /* >=2 */
142570 if( sqlite3WhereTrace & 0x02 ){
142571 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
142572 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
142573 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
142574 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
142575 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
142576 if( pTo->isOrdered>0 ){
142577 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
142578 }else{
142579 sqlite3DebugPrintf("\n");
142580 }
142581 }
142582 }
142583#endif
142584
142585 /* Swap the roles of aFrom and aTo for the next generation */
142586 pFrom = aTo;
142587 aTo = aFrom;
142588 aFrom = pFrom;
142589 nFrom = nTo;
142590 }
142591
142592 if( nFrom==0 ){
142593 sqlite3ErrorMsg(pParse, "no query solution");
142594 sqlite3DbFreeNN(db, pSpace);
142595 return SQLITE_ERROR;
142596 }
142597
142598 /* Find the lowest cost path. pFrom will be left pointing to that path */
142599 pFrom = aFrom;
142600 for(ii=1; ii<nFrom; ii++){
142601 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
142602 }
142603 assert( pWInfo->nLevel==nLoop );
142604 /* Load the lowest cost path into pWInfo */
142605 for(iLoop=0; iLoop<nLoop; iLoop++){
142606 WhereLevel *pLevel = pWInfo->a + iLoop;
142607 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
142608 pLevel->iFrom = pWLoop->iTab;
142609 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
142610 }
142611 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
142612 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
142613 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
142614 && nRowEst
142615 ){
142616 Bitmask notUsed;
142617 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
142618 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
142619 if( rc==pWInfo->pResultSet->nExpr ){
142620 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
142621 }
142622 }
142623 pWInfo->bOrderedInnerLoop = 0;
142624 if( pWInfo->pOrderBy ){
142625 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
142626 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
142627 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
142628 }
142629 }else{
142630 pWInfo->nOBSat = pFrom->isOrdered;
142631 pWInfo->revMask = pFrom->revLoop;
142632 if( pWInfo->nOBSat<=0 ){
142633 pWInfo->nOBSat = 0;
142634 if( nLoop>0 ){
142635 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
142636 if( (wsFlags & WHERE_ONEROW)==0
142637 && (wsFlags&(WHERE_IPK|WHERE_COLUMN_IN))!=(WHERE_IPK|WHERE_COLUMN_IN)
142638 ){
142639 Bitmask m = 0;
142640 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
142641 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
142642 testcase( wsFlags & WHERE_IPK );
142643 testcase( wsFlags & WHERE_COLUMN_IN );
142644 if( rc==pWInfo->pOrderBy->nExpr ){
142645 pWInfo->bOrderedInnerLoop = 1;
142646 pWInfo->revMask = m;
142647 }
142648 }
142649 }
142650 }
142651 }
142652 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
142653 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
142654 ){
142655 Bitmask revMask = 0;
142656 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
142657 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
142658 );
142659 assert( pWInfo->sorted==0 );
142660 if( nOrder==pWInfo->pOrderBy->nExpr ){
142661 pWInfo->sorted = 1;
142662 pWInfo->revMask = revMask;
142663 }
142664 }
142665 }
142666
142667
142668 pWInfo->nRowOut = pFrom->nRow;
142669
142670 /* Free temporary memory and return success */
142671 sqlite3DbFreeNN(db, pSpace);
142672 return SQLITE_OK;
142673}
142674
142675/*
142676** Most queries use only a single table (they are not joins) and have
142677** simple == constraints against indexed fields. This routine attempts
142678** to plan those simple cases using much less ceremony than the
142679** general-purpose query planner, and thereby yield faster sqlite3_prepare()
142680** times for the common case.
142681**
142682** Return non-zero on success, if this query can be handled by this
142683** no-frills query planner. Return zero if this query needs the
142684** general-purpose query planner.
142685*/
142686static int whereShortCut(WhereLoopBuilder *pBuilder){
142687 WhereInfo *pWInfo;
142688 struct SrcList_item *pItem;
142689 WhereClause *pWC;
142690 WhereTerm *pTerm;
142691 WhereLoop *pLoop;
142692 int iCur;
142693 int j;
142694 Table *pTab;
142695 Index *pIdx;
142696
142697 pWInfo = pBuilder->pWInfo;
142698 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
142699 assert( pWInfo->pTabList->nSrc>=1 );
142700 pItem = pWInfo->pTabList->a;
142701 pTab = pItem->pTab;
142702 if( IsVirtual(pTab) ) return 0;
142703 if( pItem->fg.isIndexedBy ) return 0;
142704 iCur = pItem->iCursor;
142705 pWC = &pWInfo->sWC;
142706 pLoop = pBuilder->pNew;
142707 pLoop->wsFlags = 0;
142708 pLoop->nSkip = 0;
142709 pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
142710 if( pTerm ){
142711 testcase( pTerm->eOperator & WO_IS );
142712 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
142713 pLoop->aLTerm[0] = pTerm;
142714 pLoop->nLTerm = 1;
142715 pLoop->u.btree.nEq = 1;
142716 /* TUNING: Cost of a rowid lookup is 10 */
142717 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
142718 }else{
142719 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
142720 int opMask;
142721 assert( pLoop->aLTermSpace==pLoop->aLTerm );
142722 if( !IsUniqueIndex(pIdx)
142723 || pIdx->pPartIdxWhere!=0
142724 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
142725 ) continue;
142726 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
142727 for(j=0; j<pIdx->nKeyCol; j++){
142728 pTerm = sqlite3WhereFindTerm(pWC, iCur, j, 0, opMask, pIdx);
142729 if( pTerm==0 ) break;
142730 testcase( pTerm->eOperator & WO_IS );
142731 pLoop->aLTerm[j] = pTerm;
142732 }
142733 if( j!=pIdx->nKeyCol ) continue;
142734 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
142735 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
142736 pLoop->wsFlags |= WHERE_IDX_ONLY;
142737 }
142738 pLoop->nLTerm = j;
142739 pLoop->u.btree.nEq = j;
142740 pLoop->u.btree.pIndex = pIdx;
142741 /* TUNING: Cost of a unique index lookup is 15 */
142742 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
142743 break;
142744 }
142745 }
142746 if( pLoop->wsFlags ){
142747 pLoop->nOut = (LogEst)1;
142748 pWInfo->a[0].pWLoop = pLoop;
142749 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
142750 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
142751 pWInfo->a[0].iTabCur = iCur;
142752 pWInfo->nRowOut = 1;
142753 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
142754 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
142755 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
142756 }
142757#ifdef SQLITE_DEBUG
142758 pLoop->cId = '0';
142759#endif
142760 return 1;
142761 }
142762 return 0;
142763}
142764
142765/*
142766** Helper function for exprIsDeterministic().
142767*/
142768static int exprNodeIsDeterministic(Walker *pWalker, Expr *pExpr){
142769 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
142770 pWalker->eCode = 0;
142771 return WRC_Abort;
142772 }
142773 return WRC_Continue;
142774}
142775
142776/*
142777** Return true if the expression contains no non-deterministic SQL
142778** functions. Do not consider non-deterministic SQL functions that are
142779** part of sub-select statements.
142780*/
142781static int exprIsDeterministic(Expr *p){
142782 Walker w;
142783 memset(&w, 0, sizeof(w));
142784 w.eCode = 1;
142785 w.xExprCallback = exprNodeIsDeterministic;
142786 w.xSelectCallback = sqlite3SelectWalkFail;
142787 sqlite3WalkExpr(&w, p);
142788 return w.eCode;
142789}
142790
142791/*
142792** Generate the beginning of the loop used for WHERE clause processing.
142793** The return value is a pointer to an opaque structure that contains
142794** information needed to terminate the loop. Later, the calling routine
142795** should invoke sqlite3WhereEnd() with the return value of this function
142796** in order to complete the WHERE clause processing.
142797**
142798** If an error occurs, this routine returns NULL.
142799**
142800** The basic idea is to do a nested loop, one loop for each table in
142801** the FROM clause of a select. (INSERT and UPDATE statements are the
142802** same as a SELECT with only a single table in the FROM clause.) For
142803** example, if the SQL is this:
142804**
142805** SELECT * FROM t1, t2, t3 WHERE ...;
142806**
142807** Then the code generated is conceptually like the following:
142808**
142809** foreach row1 in t1 do \ Code generated
142810** foreach row2 in t2 do |-- by sqlite3WhereBegin()
142811** foreach row3 in t3 do /
142812** ...
142813** end \ Code generated
142814** end |-- by sqlite3WhereEnd()
142815** end /
142816**
142817** Note that the loops might not be nested in the order in which they
142818** appear in the FROM clause if a different order is better able to make
142819** use of indices. Note also that when the IN operator appears in
142820** the WHERE clause, it might result in additional nested loops for
142821** scanning through all values on the right-hand side of the IN.
142822**
142823** There are Btree cursors associated with each table. t1 uses cursor
142824** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
142825** And so forth. This routine generates code to open those VDBE cursors
142826** and sqlite3WhereEnd() generates the code to close them.
142827**
142828** The code that sqlite3WhereBegin() generates leaves the cursors named
142829** in pTabList pointing at their appropriate entries. The [...] code
142830** can use OP_Column and OP_Rowid opcodes on these cursors to extract
142831** data from the various tables of the loop.
142832**
142833** If the WHERE clause is empty, the foreach loops must each scan their
142834** entire tables. Thus a three-way join is an O(N^3) operation. But if
142835** the tables have indices and there are terms in the WHERE clause that
142836** refer to those indices, a complete table scan can be avoided and the
142837** code will run much faster. Most of the work of this routine is checking
142838** to see if there are indices that can be used to speed up the loop.
142839**
142840** Terms of the WHERE clause are also used to limit which rows actually
142841** make it to the "..." in the middle of the loop. After each "foreach",
142842** terms of the WHERE clause that use only terms in that loop and outer
142843** loops are evaluated and if false a jump is made around all subsequent
142844** inner loops (or around the "..." if the test occurs within the inner-
142845** most loop)
142846**
142847** OUTER JOINS
142848**
142849** An outer join of tables t1 and t2 is conceptally coded as follows:
142850**
142851** foreach row1 in t1 do
142852** flag = 0
142853** foreach row2 in t2 do
142854** start:
142855** ...
142856** flag = 1
142857** end
142858** if flag==0 then
142859** move the row2 cursor to a null row
142860** goto start
142861** fi
142862** end
142863**
142864** ORDER BY CLAUSE PROCESSING
142865**
142866** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
142867** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
142868** if there is one. If there is no ORDER BY clause or if this routine
142869** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
142870**
142871** The iIdxCur parameter is the cursor number of an index. If
142872** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index
142873** to use for OR clause processing. The WHERE clause should use this
142874** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
142875** the first cursor in an array of cursors for all indices. iIdxCur should
142876** be used to compute the appropriate cursor depending on which index is
142877** used.
142878*/
142879SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
142880 Parse *pParse, /* The parser context */
142881 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
142882 Expr *pWhere, /* The WHERE clause */
142883 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
142884 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
142885 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
142886 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
142887 ** If WHERE_USE_LIMIT, then the limit amount */
142888){
142889 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
142890 int nTabList; /* Number of elements in pTabList */
142891 WhereInfo *pWInfo; /* Will become the return value of this function */
142892 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
142893 Bitmask notReady; /* Cursors that are not yet positioned */
142894 WhereLoopBuilder sWLB; /* The WhereLoop builder */
142895 WhereMaskSet *pMaskSet; /* The expression mask set */
142896 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
142897 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
142898 int ii; /* Loop counter */
142899 sqlite3 *db; /* Database connection */
142900 int rc; /* Return code */
142901 u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */
142902
142903 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (
142904 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
142905 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
142906 ));
142907
142908 /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */
142909 assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
142910 || (wctrlFlags & WHERE_USE_LIMIT)==0 );
142911
142912 /* Variable initialization */
142913 db = pParse->db;
142914 memset(&sWLB, 0, sizeof(sWLB));
142915
142916 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
142917 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
142918 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
142919 sWLB.pOrderBy = pOrderBy;
142920
142921 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
142922 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
142923 if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
142924 wctrlFlags &= ~WHERE_WANT_DISTINCT;
142925 }
142926
142927 /* The number of tables in the FROM clause is limited by the number of
142928 ** bits in a Bitmask
142929 */
142930 testcase( pTabList->nSrc==BMS );
142931 if( pTabList->nSrc>BMS ){
142932 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
142933 return 0;
142934 }
142935
142936 /* This function normally generates a nested loop for all tables in
142937 ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should
142938 ** only generate code for the first table in pTabList and assume that
142939 ** any cursors associated with subsequent tables are uninitialized.
142940 */
142941 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
142942
142943 /* Allocate and initialize the WhereInfo structure that will become the
142944 ** return value. A single allocation is used to store the WhereInfo
142945 ** struct, the contents of WhereInfo.a[], the WhereClause structure
142946 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
142947 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
142948 ** some architectures. Hence the ROUND8() below.
142949 */
142950 nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
142951 pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop));
142952 if( db->mallocFailed ){
142953 sqlite3DbFree(db, pWInfo);
142954 pWInfo = 0;
142955 goto whereBeginError;
142956 }
142957 pWInfo->pParse = pParse;
142958 pWInfo->pTabList = pTabList;
142959 pWInfo->pOrderBy = pOrderBy;
142960 pWInfo->pWhere = pWhere;
142961 pWInfo->pResultSet = pResultSet;
142962 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
142963 pWInfo->nLevel = nTabList;
142964 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v);
142965 pWInfo->wctrlFlags = wctrlFlags;
142966 pWInfo->iLimit = iAuxArg;
142967 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
142968 memset(&pWInfo->nOBSat, 0,
142969 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
142970 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
142971 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
142972 pMaskSet = &pWInfo->sMaskSet;
142973 sWLB.pWInfo = pWInfo;
142974 sWLB.pWC = &pWInfo->sWC;
142975 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
142976 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
142977 whereLoopInit(sWLB.pNew);
142978#ifdef SQLITE_DEBUG
142979 sWLB.pNew->cId = '*';
142980#endif
142981
142982 /* Split the WHERE clause into separate subexpressions where each
142983 ** subexpression is separated by an AND operator.
142984 */
142985 initMaskSet(pMaskSet);
142986 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
142987 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
142988
142989 /* Special case: No FROM clause
142990 */
142991 if( nTabList==0 ){
142992 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
142993 if( wctrlFlags & WHERE_WANT_DISTINCT ){
142994 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
142995 }
142996 ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"));
142997 }else{
142998 /* Assign a bit from the bitmask to every term in the FROM clause.
142999 **
143000 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
143001 **
143002 ** The rule of the previous sentence ensures thta if X is the bitmask for
143003 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
143004 ** Knowing the bitmask for all tables to the left of a left join is
143005 ** important. Ticket #3015.
143006 **
143007 ** Note that bitmasks are created for all pTabList->nSrc tables in
143008 ** pTabList, not just the first nTabList tables. nTabList is normally
143009 ** equal to pTabList->nSrc but might be shortened to 1 if the
143010 ** WHERE_OR_SUBCLAUSE flag is set.
143011 */
143012 ii = 0;
143013 do{
143014 createMask(pMaskSet, pTabList->a[ii].iCursor);
143015 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
143016 }while( (++ii)<pTabList->nSrc );
143017 #ifdef SQLITE_DEBUG
143018 {
143019 Bitmask mx = 0;
143020 for(ii=0; ii<pTabList->nSrc; ii++){
143021 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
143022 assert( m>=mx );
143023 mx = m;
143024 }
143025 }
143026 #endif
143027 }
143028
143029 /* Analyze all of the subexpressions. */
143030 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
143031 if( db->mallocFailed ) goto whereBeginError;
143032
143033 /* Special case: WHERE terms that do not refer to any tables in the join
143034 ** (constant expressions). Evaluate each such term, and jump over all the
143035 ** generated code if the result is not true.
143036 **
143037 ** Do not do this if the expression contains non-deterministic functions
143038 ** that are not within a sub-select. This is not strictly required, but
143039 ** preserves SQLite's legacy behaviour in the following two cases:
143040 **
143041 ** FROM ... WHERE random()>0; -- eval random() once per row
143042 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
143043 */
143044 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
143045 WhereTerm *pT = &sWLB.pWC->a[ii];
143046 if( pT->wtFlags & TERM_VIRTUAL ) continue;
143047 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
143048 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
143049 pT->wtFlags |= TERM_CODED;
143050 }
143051 }
143052
143053 if( wctrlFlags & WHERE_WANT_DISTINCT ){
143054 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
143055 /* The DISTINCT marking is pointless. Ignore it. */
143056 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
143057 }else if( pOrderBy==0 ){
143058 /* Try to ORDER BY the result set to make distinct processing easier */
143059 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
143060 pWInfo->pOrderBy = pResultSet;
143061 }
143062 }
143063
143064 /* Construct the WhereLoop objects */
143065#if defined(WHERETRACE_ENABLED)
143066 if( sqlite3WhereTrace & 0xffff ){
143067 sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
143068 if( wctrlFlags & WHERE_USE_LIMIT ){
143069 sqlite3DebugPrintf(", limit: %d", iAuxArg);
143070 }
143071 sqlite3DebugPrintf(")\n");
143072 }
143073 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
143074 sqlite3WhereClausePrint(sWLB.pWC);
143075 }
143076#endif
143077
143078 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
143079 rc = whereLoopAddAll(&sWLB);
143080 if( rc ) goto whereBeginError;
143081
143082#ifdef WHERETRACE_ENABLED
143083 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */
143084 WhereLoop *p;
143085 int i;
143086 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
143087 "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
143088 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
143089 p->cId = zLabel[i%(sizeof(zLabel)-1)];
143090 whereLoopPrint(p, sWLB.pWC);
143091 }
143092 }
143093#endif
143094
143095 wherePathSolver(pWInfo, 0);
143096 if( db->mallocFailed ) goto whereBeginError;
143097 if( pWInfo->pOrderBy ){
143098 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
143099 if( db->mallocFailed ) goto whereBeginError;
143100 }
143101 }
143102 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
143103 pWInfo->revMask = ALLBITS;
143104 }
143105 if( pParse->nErr || NEVER(db->mallocFailed) ){
143106 goto whereBeginError;
143107 }
143108#ifdef WHERETRACE_ENABLED
143109 if( sqlite3WhereTrace ){
143110 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
143111 if( pWInfo->nOBSat>0 ){
143112 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
143113 }
143114 switch( pWInfo->eDistinct ){
143115 case WHERE_DISTINCT_UNIQUE: {
143116 sqlite3DebugPrintf(" DISTINCT=unique");
143117 break;
143118 }
143119 case WHERE_DISTINCT_ORDERED: {
143120 sqlite3DebugPrintf(" DISTINCT=ordered");
143121 break;
143122 }
143123 case WHERE_DISTINCT_UNORDERED: {
143124 sqlite3DebugPrintf(" DISTINCT=unordered");
143125 break;
143126 }
143127 }
143128 sqlite3DebugPrintf("\n");
143129 for(ii=0; ii<pWInfo->nLevel; ii++){
143130 whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
143131 }
143132 }
143133#endif
143134
143135 /* Attempt to omit tables from the join that do not affect the result.
143136 ** For a table to not affect the result, the following must be true:
143137 **
143138 ** 1) The query must not be an aggregate.
143139 ** 2) The table must be the RHS of a LEFT JOIN.
143140 ** 3) Either the query must be DISTINCT, or else the ON or USING clause
143141 ** must contain a constraint that limits the scan of the table to
143142 ** at most a single row.
143143 ** 4) The table must not be referenced by any part of the query apart
143144 ** from its own USING or ON clause.
143145 **
143146 ** For example, given:
143147 **
143148 ** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
143149 ** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
143150 ** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
143151 **
143152 ** then table t2 can be omitted from the following:
143153 **
143154 ** SELECT v1, v3 FROM t1
143155 ** LEFT JOIN t2 USING (t1.ipk=t2.ipk)
143156 ** LEFT JOIN t3 USING (t1.ipk=t3.ipk)
143157 **
143158 ** or from:
143159 **
143160 ** SELECT DISTINCT v1, v3 FROM t1
143161 ** LEFT JOIN t2
143162 ** LEFT JOIN t3 USING (t1.ipk=t3.ipk)
143163 */
143164 notReady = ~(Bitmask)0;
143165 if( pWInfo->nLevel>=2
143166 && pResultSet!=0 /* guarantees condition (1) above */
143167 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
143168 ){
143169 int i;
143170 Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
143171 if( sWLB.pOrderBy ){
143172 tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
143173 }
143174 for(i=pWInfo->nLevel-1; i>=1; i--){
143175 WhereTerm *pTerm, *pEnd;
143176 struct SrcList_item *pItem;
143177 pLoop = pWInfo->a[i].pWLoop;
143178 pItem = &pWInfo->pTabList->a[pLoop->iTab];
143179 if( (pItem->fg.jointype & JT_LEFT)==0 ) continue;
143180 if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
143181 && (pLoop->wsFlags & WHERE_ONEROW)==0
143182 ){
143183 continue;
143184 }
143185 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
143186 pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
143187 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
143188 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
143189 if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
143190 || pTerm->pExpr->iRightJoinTable!=pItem->iCursor
143191 ){
143192 break;
143193 }
143194 }
143195 }
143196 if( pTerm<pEnd ) continue;
143197 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
143198 notReady &= ~pLoop->maskSelf;
143199 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
143200 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
143201 pTerm->wtFlags |= TERM_CODED;
143202 }
143203 }
143204 if( i!=pWInfo->nLevel-1 ){
143205 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
143206 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
143207 }
143208 pWInfo->nLevel--;
143209 nTabList--;
143210 }
143211 }
143212 WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
143213 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
143214
143215 /* If the caller is an UPDATE or DELETE statement that is requesting
143216 ** to use a one-pass algorithm, determine if this is appropriate.
143217 **
143218 ** A one-pass approach can be used if the caller has requested one
143219 ** and either (a) the scan visits at most one row or (b) each
143220 ** of the following are true:
143221 **
143222 ** * the caller has indicated that a one-pass approach can be used
143223 ** with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
143224 ** * the table is not a virtual table, and
143225 ** * either the scan does not use the OR optimization or the caller
143226 ** is a DELETE operation (WHERE_DUPLICATES_OK is only specified
143227 ** for DELETE).
143228 **
143229 ** The last qualification is because an UPDATE statement uses
143230 ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
143231 ** use a one-pass approach, and this is not set accurately for scans
143232 ** that use the OR optimization.
143233 */
143234 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
143235 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
143236 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
143237 int bOnerow = (wsFlags & WHERE_ONEROW)!=0;
143238 if( bOnerow || (
143239 0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW)
143240 && 0==(wsFlags & WHERE_VIRTUALTABLE)
143241 && (0==(wsFlags & WHERE_MULTI_OR) || (wctrlFlags & WHERE_DUPLICATES_OK))
143242 )){
143243 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
143244 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
143245 if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
143246 bFordelete = OPFLAG_FORDELETE;
143247 }
143248 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
143249 }
143250 }
143251 }
143252
143253 /* Open all tables in the pTabList and any indices selected for
143254 ** searching those tables.
143255 */
143256 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
143257 Table *pTab; /* Table to open */
143258 int iDb; /* Index of database containing table/index */
143259 struct SrcList_item *pTabItem;
143260
143261 pTabItem = &pTabList->a[pLevel->iFrom];
143262 pTab = pTabItem->pTab;
143263 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
143264 pLoop = pLevel->pWLoop;
143265 if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
143266 /* Do nothing */
143267 }else
143268#ifndef SQLITE_OMIT_VIRTUALTABLE
143269 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
143270 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
143271 int iCur = pTabItem->iCursor;
143272 sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
143273 }else if( IsVirtual(pTab) ){
143274 /* noop */
143275 }else
143276#endif
143277 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
143278 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
143279 int op = OP_OpenRead;
143280 if( pWInfo->eOnePass!=ONEPASS_OFF ){
143281 op = OP_OpenWrite;
143282 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
143283 };
143284 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
143285 assert( pTabItem->iCursor==pLevel->iTabCur );
143286 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
143287 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
143288 if( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol<BMS && HasRowid(pTab) ){
143289 Bitmask b = pTabItem->colUsed;
143290 int n = 0;
143291 for(; b; b=b>>1, n++){}
143292 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
143293 assert( n<=pTab->nCol );
143294 }
143295#ifdef SQLITE_ENABLE_CURSOR_HINTS
143296 if( pLoop->u.btree.pIndex!=0 ){
143297 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete);
143298 }else
143299#endif
143300 {
143301 sqlite3VdbeChangeP5(v, bFordelete);
143302 }
143303#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
143304 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
143305 (const u8*)&pTabItem->colUsed, P4_INT64);
143306#endif
143307 }else{
143308 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
143309 }
143310 if( pLoop->wsFlags & WHERE_INDEXED ){
143311 Index *pIx = pLoop->u.btree.pIndex;
143312 int iIndexCur;
143313 int op = OP_OpenRead;
143314 /* iAuxArg is always set to a positive value if ONEPASS is possible */
143315 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
143316 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
143317 && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0
143318 ){
143319 /* This is one term of an OR-optimization using the PRIMARY KEY of a
143320 ** WITHOUT ROWID table. No need for a separate index */
143321 iIndexCur = pLevel->iTabCur;
143322 op = 0;
143323 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
143324 Index *pJ = pTabItem->pTab->pIndex;
143325 iIndexCur = iAuxArg;
143326 assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
143327 while( ALWAYS(pJ) && pJ!=pIx ){
143328 iIndexCur++;
143329 pJ = pJ->pNext;
143330 }
143331 op = OP_OpenWrite;
143332 pWInfo->aiCurOnePass[1] = iIndexCur;
143333 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){
143334 iIndexCur = iAuxArg;
143335 op = OP_ReopenIdx;
143336 }else{
143337 iIndexCur = pParse->nTab++;
143338 }
143339 pLevel->iIdxCur = iIndexCur;
143340 assert( pIx->pSchema==pTab->pSchema );
143341 assert( iIndexCur>=0 );
143342 if( op ){
143343 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
143344 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
143345 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
143346 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
143347 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
143348 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
143349 ){
143350 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
143351 }
143352 VdbeComment((v, "%s", pIx->zName));
143353#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
143354 {
143355 u64 colUsed = 0;
143356 int ii, jj;
143357 for(ii=0; ii<pIx->nColumn; ii++){
143358 jj = pIx->aiColumn[ii];
143359 if( jj<0 ) continue;
143360 if( jj>63 ) jj = 63;
143361 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
143362 colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
143363 }
143364 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0,
143365 (u8*)&colUsed, P4_INT64);
143366 }
143367#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
143368 }
143369 }
143370 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
143371 }
143372 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
143373 if( db->mallocFailed ) goto whereBeginError;
143374
143375 /* Generate the code to do the search. Each iteration of the for
143376 ** loop below generates code for a single nested loop of the VM
143377 ** program.
143378 */
143379 for(ii=0; ii<nTabList; ii++){
143380 int addrExplain;
143381 int wsFlags;
143382 pLevel = &pWInfo->a[ii];
143383 wsFlags = pLevel->pWLoop->wsFlags;
143384#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
143385 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
143386 constructAutomaticIndex(pParse, &pWInfo->sWC,
143387 &pTabList->a[pLevel->iFrom], notReady, pLevel);
143388 if( db->mallocFailed ) goto whereBeginError;
143389 }
143390#endif
143391 addrExplain = sqlite3WhereExplainOneScan(
143392 pParse, pTabList, pLevel, wctrlFlags
143393 );
143394 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
143395 notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady);
143396 pWInfo->iContinue = pLevel->addrCont;
143397 if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE)==0 ){
143398 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
143399 }
143400 }
143401
143402 /* Done. */
143403 VdbeModuleComment((v, "Begin WHERE-core"));
143404 return pWInfo;
143405
143406 /* Jump here if malloc fails */
143407whereBeginError:
143408 if( pWInfo ){
143409 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
143410 whereInfoFree(db, pWInfo);
143411 }
143412 return 0;
143413}
143414
143415/*
143416** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
143417** index rather than the main table. In SQLITE_DEBUG mode, we want
143418** to trace those changes if PRAGMA vdbe_addoptrace=on. This routine
143419** does that.
143420*/
143421#ifndef SQLITE_DEBUG
143422# define OpcodeRewriteTrace(D,K,P) /* no-op */
143423#else
143424# define OpcodeRewriteTrace(D,K,P) sqlite3WhereOpcodeRewriteTrace(D,K,P)
143425 static void sqlite3WhereOpcodeRewriteTrace(
143426 sqlite3 *db,
143427 int pc,
143428 VdbeOp *pOp
143429 ){
143430 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
143431 sqlite3VdbePrintOp(0, pc, pOp);
143432 }
143433#endif
143434
143435/*
143436** Generate the end of the WHERE loop. See comments on
143437** sqlite3WhereBegin() for additional information.
143438*/
143439SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
143440 Parse *pParse = pWInfo->pParse;
143441 Vdbe *v = pParse->pVdbe;
143442 int i;
143443 WhereLevel *pLevel;
143444 WhereLoop *pLoop;
143445 SrcList *pTabList = pWInfo->pTabList;
143446 sqlite3 *db = pParse->db;
143447
143448 /* Generate loop termination code.
143449 */
143450 VdbeModuleComment((v, "End WHERE-core"));
143451 for(i=pWInfo->nLevel-1; i>=0; i--){
143452 int addr;
143453 pLevel = &pWInfo->a[i];
143454 pLoop = pLevel->pWLoop;
143455 if( pLevel->op!=OP_Noop ){
143456#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
143457 int addrSeek = 0;
143458 Index *pIdx;
143459 int n;
143460 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
143461 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
143462 && (pLoop->wsFlags & WHERE_INDEXED)!=0
143463 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
143464 && (n = pLoop->u.btree.nIdxCol)>0
143465 && pIdx->aiRowLogEst[n]>=36
143466 ){
143467 int r1 = pParse->nMem+1;
143468 int j, op;
143469 for(j=0; j<n; j++){
143470 sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
143471 }
143472 pParse->nMem += n+1;
143473 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
143474 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
143475 VdbeCoverageIf(v, op==OP_SeekLT);
143476 VdbeCoverageIf(v, op==OP_SeekGT);
143477 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
143478 }
143479#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
143480 /* The common case: Advance to the next row */
143481 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
143482 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
143483 sqlite3VdbeChangeP5(v, pLevel->p5);
143484 VdbeCoverage(v);
143485 VdbeCoverageIf(v, pLevel->op==OP_Next);
143486 VdbeCoverageIf(v, pLevel->op==OP_Prev);
143487 VdbeCoverageIf(v, pLevel->op==OP_VNext);
143488#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
143489 if( addrSeek ) sqlite3VdbeJumpHere(v, addrSeek);
143490#endif
143491 }else{
143492 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
143493 }
143494 if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
143495 struct InLoop *pIn;
143496 int j;
143497 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
143498 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
143499 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
143500 if( pIn->eEndLoopOp!=OP_Noop ){
143501 if( pIn->nPrefix ){
143502 assert( pLoop->wsFlags & WHERE_IN_EARLYOUT );
143503 sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
143504 sqlite3VdbeCurrentAddr(v)+2,
143505 pIn->iBase, pIn->nPrefix);
143506 VdbeCoverage(v);
143507 }
143508 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
143509 VdbeCoverage(v);
143510 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
143511 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
143512 }
143513 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
143514 }
143515 }
143516 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
143517 if( pLevel->addrSkip ){
143518 sqlite3VdbeGoto(v, pLevel->addrSkip);
143519 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
143520 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
143521 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
143522 }
143523#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
143524 if( pLevel->addrLikeRep ){
143525 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
143526 pLevel->addrLikeRep);
143527 VdbeCoverage(v);
143528 }
143529#endif
143530 if( pLevel->iLeftJoin ){
143531 int ws = pLoop->wsFlags;
143532 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
143533 assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 );
143534 if( (ws & WHERE_IDX_ONLY)==0 ){
143535 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
143536 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
143537 }
143538 if( (ws & WHERE_INDEXED)
143539 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCovidx)
143540 ){
143541 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
143542 }
143543 if( pLevel->op==OP_Return ){
143544 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
143545 }else{
143546 sqlite3VdbeGoto(v, pLevel->addrFirst);
143547 }
143548 sqlite3VdbeJumpHere(v, addr);
143549 }
143550 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
143551 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
143552 }
143553
143554 /* The "break" point is here, just past the end of the outer loop.
143555 ** Set it.
143556 */
143557 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
143558
143559 assert( pWInfo->nLevel<=pTabList->nSrc );
143560 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
143561 int k, last;
143562 VdbeOp *pOp;
143563 Index *pIdx = 0;
143564 struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
143565 Table *pTab = pTabItem->pTab;
143566 assert( pTab!=0 );
143567 pLoop = pLevel->pWLoop;
143568
143569 /* For a co-routine, change all OP_Column references to the table of
143570 ** the co-routine into OP_Copy of result contained in a register.
143571 ** OP_Rowid becomes OP_Null.
143572 */
143573 if( pTabItem->fg.viaCoroutine ){
143574 testcase( pParse->db->mallocFailed );
143575 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
143576 pTabItem->regResult, 0);
143577 continue;
143578 }
143579
143580 /* If this scan uses an index, make VDBE code substitutions to read data
143581 ** from the index instead of from the table where possible. In some cases
143582 ** this optimization prevents the table from ever being read, which can
143583 ** yield a significant performance boost.
143584 **
143585 ** Calls to the code generator in between sqlite3WhereBegin and
143586 ** sqlite3WhereEnd will have created code that references the table
143587 ** directly. This loop scans all that code looking for opcodes
143588 ** that reference the table and converts them into opcodes that
143589 ** reference the index.
143590 */
143591 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
143592 pIdx = pLoop->u.btree.pIndex;
143593 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
143594 pIdx = pLevel->u.pCovidx;
143595 }
143596 if( pIdx
143597 && (pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable))
143598 && !db->mallocFailed
143599 ){
143600 last = sqlite3VdbeCurrentAddr(v);
143601 k = pLevel->addrBody;
143602#ifdef SQLITE_DEBUG
143603 if( db->flags & SQLITE_VdbeAddopTrace ){
143604 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
143605 }
143606#endif
143607 pOp = sqlite3VdbeGetOp(v, k);
143608 for(; k<last; k++, pOp++){
143609 if( pOp->p1!=pLevel->iTabCur ) continue;
143610 if( pOp->opcode==OP_Column
143611#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
143612 || pOp->opcode==OP_Offset
143613#endif
143614 ){
143615 int x = pOp->p2;
143616 assert( pIdx->pTable==pTab );
143617 if( !HasRowid(pTab) ){
143618 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
143619 x = pPk->aiColumn[x];
143620 assert( x>=0 );
143621 }
143622 x = sqlite3ColumnOfIndex(pIdx, x);
143623 if( x>=0 ){
143624 pOp->p2 = x;
143625 pOp->p1 = pLevel->iIdxCur;
143626 OpcodeRewriteTrace(db, k, pOp);
143627 }
143628 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
143629 || pWInfo->eOnePass );
143630 }else if( pOp->opcode==OP_Rowid ){
143631 pOp->p1 = pLevel->iIdxCur;
143632 pOp->opcode = OP_IdxRowid;
143633 OpcodeRewriteTrace(db, k, pOp);
143634 }else if( pOp->opcode==OP_IfNullRow ){
143635 pOp->p1 = pLevel->iIdxCur;
143636 OpcodeRewriteTrace(db, k, pOp);
143637 }
143638 }
143639#ifdef SQLITE_DEBUG
143640 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
143641#endif
143642 }
143643 }
143644
143645 /* Final cleanup
143646 */
143647 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
143648 whereInfoFree(db, pWInfo);
143649 return;
143650}
143651
143652/************** End of where.c ***********************************************/
143653/************** Begin file window.c ******************************************/
143654/*
143655** 2018 May 08
143656**
143657** The author disclaims copyright to this source code. In place of
143658** a legal notice, here is a blessing:
143659**
143660** May you do good and not evil.
143661** May you find forgiveness for yourself and forgive others.
143662** May you share freely, never taking more than you give.
143663**
143664*************************************************************************
143665*/
143666/* #include "sqliteInt.h" */
143667
143668#ifndef SQLITE_OMIT_WINDOWFUNC
143669
143670/*
143671** SELECT REWRITING
143672**
143673** Any SELECT statement that contains one or more window functions in
143674** either the select list or ORDER BY clause (the only two places window
143675** functions may be used) is transformed by function sqlite3WindowRewrite()
143676** in order to support window function processing. For example, with the
143677** schema:
143678**
143679** CREATE TABLE t1(a, b, c, d, e, f, g);
143680**
143681** the statement:
143682**
143683** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
143684**
143685** is transformed to:
143686**
143687** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
143688** SELECT a, e, c, d, b FROM t1 ORDER BY c, d
143689** ) ORDER BY e;
143690**
143691** The flattening optimization is disabled when processing this transformed
143692** SELECT statement. This allows the implementation of the window function
143693** (in this case max()) to process rows sorted in order of (c, d), which
143694** makes things easier for obvious reasons. More generally:
143695**
143696** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to
143697** the sub-query.
143698**
143699** * ORDER BY, LIMIT and OFFSET remain part of the parent query.
143700**
143701** * Terminals from each of the expression trees that make up the
143702** select-list and ORDER BY expressions in the parent query are
143703** selected by the sub-query. For the purposes of the transformation,
143704** terminals are column references and aggregate functions.
143705**
143706** If there is more than one window function in the SELECT that uses
143707** the same window declaration (the OVER bit), then a single scan may
143708** be used to process more than one window function. For example:
143709**
143710** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
143711** min(e) OVER (PARTITION BY c ORDER BY d)
143712** FROM t1;
143713**
143714** is transformed in the same way as the example above. However:
143715**
143716** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
143717** min(e) OVER (PARTITION BY a ORDER BY b)
143718** FROM t1;
143719**
143720** Must be transformed to:
143721**
143722** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
143723** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
143724** SELECT a, e, c, d, b FROM t1 ORDER BY a, b
143725** ) ORDER BY c, d
143726** ) ORDER BY e;
143727**
143728** so that both min() and max() may process rows in the order defined by
143729** their respective window declarations.
143730**
143731** INTERFACE WITH SELECT.C
143732**
143733** When processing the rewritten SELECT statement, code in select.c calls
143734** sqlite3WhereBegin() to begin iterating through the results of the
143735** sub-query, which is always implemented as a co-routine. It then calls
143736** sqlite3WindowCodeStep() to process rows and finish the scan by calling
143737** sqlite3WhereEnd().
143738**
143739** sqlite3WindowCodeStep() generates VM code so that, for each row returned
143740** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
143741** When the sub-routine is invoked:
143742**
143743** * The results of all window-functions for the row are stored
143744** in the associated Window.regResult registers.
143745**
143746** * The required terminal values are stored in the current row of
143747** temp table Window.iEphCsr.
143748**
143749** In some cases, depending on the window frame and the specific window
143750** functions invoked, sqlite3WindowCodeStep() caches each entire partition
143751** in a temp table before returning any rows. In other cases it does not.
143752** This detail is encapsulated within this file, the code generated by
143753** select.c is the same in either case.
143754**
143755** BUILT-IN WINDOW FUNCTIONS
143756**
143757** This implementation features the following built-in window functions:
143758**
143759** row_number()
143760** rank()
143761** dense_rank()
143762** percent_rank()
143763** cume_dist()
143764** ntile(N)
143765** lead(expr [, offset [, default]])
143766** lag(expr [, offset [, default]])
143767** first_value(expr)
143768** last_value(expr)
143769** nth_value(expr, N)
143770**
143771** These are the same built-in window functions supported by Postgres.
143772** Although the behaviour of aggregate window functions (functions that
143773** can be used as either aggregates or window funtions) allows them to
143774** be implemented using an API, built-in window functions are much more
143775** esoteric. Additionally, some window functions (e.g. nth_value())
143776** may only be implemented by caching the entire partition in memory.
143777** As such, some built-in window functions use the same API as aggregate
143778** window functions and some are implemented directly using VDBE
143779** instructions. Additionally, for those functions that use the API, the
143780** window frame is sometimes modified before the SELECT statement is
143781** rewritten. For example, regardless of the specified window frame, the
143782** row_number() function always uses:
143783**
143784** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
143785**
143786** See sqlite3WindowUpdate() for details.
143787**
143788** As well as some of the built-in window functions, aggregate window
143789** functions min() and max() are implemented using VDBE instructions if
143790** the start of the window frame is declared as anything other than
143791** UNBOUNDED PRECEDING.
143792*/
143793
143794/*
143795** Implementation of built-in window function row_number(). Assumes that the
143796** window frame has been coerced to:
143797**
143798** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
143799*/
143800static void row_numberStepFunc(
143801 sqlite3_context *pCtx,
143802 int nArg,
143803 sqlite3_value **apArg
143804){
143805 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
143806 if( p ) (*p)++;
143807 UNUSED_PARAMETER(nArg);
143808 UNUSED_PARAMETER(apArg);
143809}
143810static void row_numberValueFunc(sqlite3_context *pCtx){
143811 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
143812 sqlite3_result_int64(pCtx, (p ? *p : 0));
143813}
143814
143815/*
143816** Context object type used by rank(), dense_rank(), percent_rank() and
143817** cume_dist().
143818*/
143819struct CallCount {
143820 i64 nValue;
143821 i64 nStep;
143822 i64 nTotal;
143823};
143824
143825/*
143826** Implementation of built-in window function dense_rank(). Assumes that
143827** the window frame has been set to:
143828**
143829** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
143830*/
143831static void dense_rankStepFunc(
143832 sqlite3_context *pCtx,
143833 int nArg,
143834 sqlite3_value **apArg
143835){
143836 struct CallCount *p;
143837 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
143838 if( p ) p->nStep = 1;
143839 UNUSED_PARAMETER(nArg);
143840 UNUSED_PARAMETER(apArg);
143841}
143842static void dense_rankValueFunc(sqlite3_context *pCtx){
143843 struct CallCount *p;
143844 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
143845 if( p ){
143846 if( p->nStep ){
143847 p->nValue++;
143848 p->nStep = 0;
143849 }
143850 sqlite3_result_int64(pCtx, p->nValue);
143851 }
143852}
143853
143854/*
143855** Implementation of built-in window function rank(). Assumes that
143856** the window frame has been set to:
143857**
143858** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
143859*/
143860static void rankStepFunc(
143861 sqlite3_context *pCtx,
143862 int nArg,
143863 sqlite3_value **apArg
143864){
143865 struct CallCount *p;
143866 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
143867 if( p ){
143868 p->nStep++;
143869 if( p->nValue==0 ){
143870 p->nValue = p->nStep;
143871 }
143872 }
143873 UNUSED_PARAMETER(nArg);
143874 UNUSED_PARAMETER(apArg);
143875}
143876static void rankValueFunc(sqlite3_context *pCtx){
143877 struct CallCount *p;
143878 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
143879 if( p ){
143880 sqlite3_result_int64(pCtx, p->nValue);
143881 p->nValue = 0;
143882 }
143883}
143884
143885/*
143886** Implementation of built-in window function percent_rank(). Assumes that
143887** the window frame has been set to:
143888**
143889** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
143890*/
143891static void percent_rankStepFunc(
143892 sqlite3_context *pCtx,
143893 int nArg,
143894 sqlite3_value **apArg
143895){
143896 struct CallCount *p;
143897 UNUSED_PARAMETER(nArg); assert( nArg==1 );
143898
143899 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
143900 if( p ){
143901 if( p->nTotal==0 ){
143902 p->nTotal = sqlite3_value_int64(apArg[0]);
143903 }
143904 p->nStep++;
143905 if( p->nValue==0 ){
143906 p->nValue = p->nStep;
143907 }
143908 }
143909}
143910static void percent_rankValueFunc(sqlite3_context *pCtx){
143911 struct CallCount *p;
143912 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
143913 if( p ){
143914 if( p->nTotal>1 ){
143915 double r = (double)(p->nValue-1) / (double)(p->nTotal-1);
143916 sqlite3_result_double(pCtx, r);
143917 }else{
143918 sqlite3_result_double(pCtx, 0.0);
143919 }
143920 p->nValue = 0;
143921 }
143922}
143923
143924/*
143925** Implementation of built-in window function cume_dist(). Assumes that
143926** the window frame has been set to:
143927**
143928** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
143929*/
143930static void cume_distStepFunc(
143931 sqlite3_context *pCtx,
143932 int nArg,
143933 sqlite3_value **apArg
143934){
143935 struct CallCount *p;
143936 assert( nArg==1 ); UNUSED_PARAMETER(nArg);
143937
143938 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
143939 if( p ){
143940 if( p->nTotal==0 ){
143941 p->nTotal = sqlite3_value_int64(apArg[0]);
143942 }
143943 p->nStep++;
143944 }
143945}
143946static void cume_distValueFunc(sqlite3_context *pCtx){
143947 struct CallCount *p;
143948 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
143949 if( p && p->nTotal ){
143950 double r = (double)(p->nStep) / (double)(p->nTotal);
143951 sqlite3_result_double(pCtx, r);
143952 }
143953}
143954
143955/*
143956** Context object for ntile() window function.
143957*/
143958struct NtileCtx {
143959 i64 nTotal; /* Total rows in partition */
143960 i64 nParam; /* Parameter passed to ntile(N) */
143961 i64 iRow; /* Current row */
143962};
143963
143964/*
143965** Implementation of ntile(). This assumes that the window frame has
143966** been coerced to:
143967**
143968** ROWS UNBOUNDED PRECEDING AND CURRENT ROW
143969*/
143970static void ntileStepFunc(
143971 sqlite3_context *pCtx,
143972 int nArg,
143973 sqlite3_value **apArg
143974){
143975 struct NtileCtx *p;
143976 assert( nArg==2 ); UNUSED_PARAMETER(nArg);
143977 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
143978 if( p ){
143979 if( p->nTotal==0 ){
143980 p->nParam = sqlite3_value_int64(apArg[0]);
143981 p->nTotal = sqlite3_value_int64(apArg[1]);
143982 if( p->nParam<=0 ){
143983 sqlite3_result_error(
143984 pCtx, "argument of ntile must be a positive integer", -1
143985 );
143986 }
143987 }
143988 p->iRow++;
143989 }
143990}
143991static void ntileValueFunc(sqlite3_context *pCtx){
143992 struct NtileCtx *p;
143993 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
143994 if( p && p->nParam>0 ){
143995 int nSize = (p->nTotal / p->nParam);
143996 if( nSize==0 ){
143997 sqlite3_result_int64(pCtx, p->iRow);
143998 }else{
143999 i64 nLarge = p->nTotal - p->nParam*nSize;
144000 i64 iSmall = nLarge*(nSize+1);
144001 i64 iRow = p->iRow-1;
144002
144003 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
144004
144005 if( iRow<iSmall ){
144006 sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
144007 }else{
144008 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
144009 }
144010 }
144011 }
144012}
144013
144014/*
144015** Context object for last_value() window function.
144016*/
144017struct LastValueCtx {
144018 sqlite3_value *pVal;
144019 int nVal;
144020};
144021
144022/*
144023** Implementation of last_value().
144024*/
144025static void last_valueStepFunc(
144026 sqlite3_context *pCtx,
144027 int nArg,
144028 sqlite3_value **apArg
144029){
144030 struct LastValueCtx *p;
144031 UNUSED_PARAMETER(nArg);
144032 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144033 if( p ){
144034 sqlite3_value_free(p->pVal);
144035 p->pVal = sqlite3_value_dup(apArg[0]);
144036 if( p->pVal==0 ){
144037 sqlite3_result_error_nomem(pCtx);
144038 }else{
144039 p->nVal++;
144040 }
144041 }
144042}
144043static void last_valueInvFunc(
144044 sqlite3_context *pCtx,
144045 int nArg,
144046 sqlite3_value **apArg
144047){
144048 struct LastValueCtx *p;
144049 UNUSED_PARAMETER(nArg);
144050 UNUSED_PARAMETER(apArg);
144051 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144052 if( ALWAYS(p) ){
144053 p->nVal--;
144054 if( p->nVal==0 ){
144055 sqlite3_value_free(p->pVal);
144056 p->pVal = 0;
144057 }
144058 }
144059}
144060static void last_valueValueFunc(sqlite3_context *pCtx){
144061 struct LastValueCtx *p;
144062 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144063 if( p && p->pVal ){
144064 sqlite3_result_value(pCtx, p->pVal);
144065 }
144066}
144067static void last_valueFinalizeFunc(sqlite3_context *pCtx){
144068 struct LastValueCtx *p;
144069 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
144070 if( p && p->pVal ){
144071 sqlite3_result_value(pCtx, p->pVal);
144072 sqlite3_value_free(p->pVal);
144073 p->pVal = 0;
144074 }
144075}
144076
144077/*
144078** Static names for the built-in window function names. These static
144079** names are used, rather than string literals, so that FuncDef objects
144080** can be associated with a particular window function by direct
144081** comparison of the zName pointer. Example:
144082**
144083** if( pFuncDef->zName==row_valueName ){ ... }
144084*/
144085static const char row_numberName[] = "row_number";
144086static const char dense_rankName[] = "dense_rank";
144087static const char rankName[] = "rank";
144088static const char percent_rankName[] = "percent_rank";
144089static const char cume_distName[] = "cume_dist";
144090static const char ntileName[] = "ntile";
144091static const char last_valueName[] = "last_value";
144092static const char nth_valueName[] = "nth_value";
144093static const char first_valueName[] = "first_value";
144094static const char leadName[] = "lead";
144095static const char lagName[] = "lag";
144096
144097/*
144098** No-op implementations of xStep() and xFinalize(). Used as place-holders
144099** for built-in window functions that never call those interfaces.
144100**
144101** The noopValueFunc() is called but is expected to do nothing. The
144102** noopStepFunc() is never called, and so it is marked with NO_TEST to
144103** let the test coverage routine know not to expect this function to be
144104** invoked.
144105*/
144106static void noopStepFunc( /*NO_TEST*/
144107 sqlite3_context *p, /*NO_TEST*/
144108 int n, /*NO_TEST*/
144109 sqlite3_value **a /*NO_TEST*/
144110){ /*NO_TEST*/
144111 UNUSED_PARAMETER(p); /*NO_TEST*/
144112 UNUSED_PARAMETER(n); /*NO_TEST*/
144113 UNUSED_PARAMETER(a); /*NO_TEST*/
144114 assert(0); /*NO_TEST*/
144115} /*NO_TEST*/
144116static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
144117
144118/* Window functions that use all window interfaces: xStep, xFinal,
144119** xValue, and xInverse */
144120#define WINDOWFUNCALL(name,nArg,extra) { \
144121 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
144122 name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \
144123 name ## InvFunc, name ## Name, {0} \
144124}
144125
144126/* Window functions that are implemented using bytecode and thus have
144127** no-op routines for their methods */
144128#define WINDOWFUNCNOOP(name,nArg,extra) { \
144129 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
144130 noopStepFunc, noopValueFunc, noopValueFunc, \
144131 noopStepFunc, name ## Name, {0} \
144132}
144133
144134/* Window functions that use all window interfaces: xStep, the
144135** same routine for xFinalize and xValue and which never call
144136** xInverse. */
144137#define WINDOWFUNCX(name,nArg,extra) { \
144138 nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0, \
144139 name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \
144140 noopStepFunc, name ## Name, {0} \
144141}
144142
144143
144144/*
144145** Register those built-in window functions that are not also aggregates.
144146*/
144147SQLITE_PRIVATE void sqlite3WindowFunctions(void){
144148 static FuncDef aWindowFuncs[] = {
144149 WINDOWFUNCX(row_number, 0, 0),
144150 WINDOWFUNCX(dense_rank, 0, 0),
144151 WINDOWFUNCX(rank, 0, 0),
144152 WINDOWFUNCX(percent_rank, 0, SQLITE_FUNC_WINDOW_SIZE),
144153 WINDOWFUNCX(cume_dist, 0, SQLITE_FUNC_WINDOW_SIZE),
144154 WINDOWFUNCX(ntile, 1, SQLITE_FUNC_WINDOW_SIZE),
144155 WINDOWFUNCALL(last_value, 1, 0),
144156 WINDOWFUNCNOOP(nth_value, 2, 0),
144157 WINDOWFUNCNOOP(first_value, 1, 0),
144158 WINDOWFUNCNOOP(lead, 1, 0),
144159 WINDOWFUNCNOOP(lead, 2, 0),
144160 WINDOWFUNCNOOP(lead, 3, 0),
144161 WINDOWFUNCNOOP(lag, 1, 0),
144162 WINDOWFUNCNOOP(lag, 2, 0),
144163 WINDOWFUNCNOOP(lag, 3, 0),
144164 };
144165 sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs));
144166}
144167
144168/*
144169** This function is called immediately after resolving the function name
144170** for a window function within a SELECT statement. Argument pList is a
144171** linked list of WINDOW definitions for the current SELECT statement.
144172** Argument pFunc is the function definition just resolved and pWin
144173** is the Window object representing the associated OVER clause. This
144174** function updates the contents of pWin as follows:
144175**
144176** * If the OVER clause refered to a named window (as in "max(x) OVER win"),
144177** search list pList for a matching WINDOW definition, and update pWin
144178** accordingly. If no such WINDOW clause can be found, leave an error
144179** in pParse.
144180**
144181** * If the function is a built-in window function that requires the
144182** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
144183** of this file), pWin is updated here.
144184*/
144185SQLITE_PRIVATE void sqlite3WindowUpdate(
144186 Parse *pParse,
144187 Window *pList, /* List of named windows for this SELECT */
144188 Window *pWin, /* Window frame to update */
144189 FuncDef *pFunc /* Window function definition */
144190){
144191 if( pWin->zName && pWin->eType==0 ){
144192 Window *p;
144193 for(p=pList; p; p=p->pNextWin){
144194 if( sqlite3StrICmp(p->zName, pWin->zName)==0 ) break;
144195 }
144196 if( p==0 ){
144197 sqlite3ErrorMsg(pParse, "no such window: %s", pWin->zName);
144198 return;
144199 }
144200 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
144201 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
144202 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
144203 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
144204 pWin->eStart = p->eStart;
144205 pWin->eEnd = p->eEnd;
144206 pWin->eType = p->eType;
144207 }
144208 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
144209 sqlite3 *db = pParse->db;
144210 if( pWin->pFilter ){
144211 sqlite3ErrorMsg(pParse,
144212 "FILTER clause may only be used with aggregate window functions"
144213 );
144214 }else
144215 if( pFunc->zName==row_numberName || pFunc->zName==ntileName ){
144216 sqlite3ExprDelete(db, pWin->pStart);
144217 sqlite3ExprDelete(db, pWin->pEnd);
144218 pWin->pStart = pWin->pEnd = 0;
144219 pWin->eType = TK_ROWS;
144220 pWin->eStart = TK_UNBOUNDED;
144221 pWin->eEnd = TK_CURRENT;
144222 }else
144223
144224 if( pFunc->zName==dense_rankName || pFunc->zName==rankName
144225 || pFunc->zName==percent_rankName || pFunc->zName==cume_distName
144226 ){
144227 sqlite3ExprDelete(db, pWin->pStart);
144228 sqlite3ExprDelete(db, pWin->pEnd);
144229 pWin->pStart = pWin->pEnd = 0;
144230 pWin->eType = TK_RANGE;
144231 pWin->eStart = TK_UNBOUNDED;
144232 pWin->eEnd = TK_CURRENT;
144233 }
144234 }
144235 pWin->pFunc = pFunc;
144236}
144237
144238/*
144239** Context object passed through sqlite3WalkExprList() to
144240** selectWindowRewriteExprCb() by selectWindowRewriteEList().
144241*/
144242typedef struct WindowRewrite WindowRewrite;
144243struct WindowRewrite {
144244 Window *pWin;
144245 SrcList *pSrc;
144246 ExprList *pSub;
144247 Select *pSubSelect; /* Current sub-select, if any */
144248};
144249
144250/*
144251** Callback function used by selectWindowRewriteEList(). If necessary,
144252** this function appends to the output expression-list and updates
144253** expression (*ppExpr) in place.
144254*/
144255static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
144256 struct WindowRewrite *p = pWalker->u.pRewrite;
144257 Parse *pParse = pWalker->pParse;
144258
144259 /* If this function is being called from within a scalar sub-select
144260 ** that used by the SELECT statement being processed, only process
144261 ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
144262 ** not process aggregates or window functions at all, as they belong
144263 ** to the scalar sub-select. */
144264 if( p->pSubSelect ){
144265 if( pExpr->op!=TK_COLUMN ){
144266 return WRC_Continue;
144267 }else{
144268 int nSrc = p->pSrc->nSrc;
144269 int i;
144270 for(i=0; i<nSrc; i++){
144271 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
144272 }
144273 if( i==nSrc ) return WRC_Continue;
144274 }
144275 }
144276
144277 switch( pExpr->op ){
144278
144279 case TK_FUNCTION:
144280 if( pExpr->pWin==0 ){
144281 break;
144282 }else{
144283 Window *pWin;
144284 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
144285 if( pExpr->pWin==pWin ){
144286 assert( pWin->pOwner==pExpr );
144287 return WRC_Prune;
144288 }
144289 }
144290 }
144291 /* Fall through. */
144292
144293 case TK_AGG_FUNCTION:
144294 case TK_COLUMN: {
144295 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
144296 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
144297 if( p->pSub ){
144298 assert( ExprHasProperty(pExpr, EP_Static)==0 );
144299 ExprSetProperty(pExpr, EP_Static);
144300 sqlite3ExprDelete(pParse->db, pExpr);
144301 ExprClearProperty(pExpr, EP_Static);
144302 memset(pExpr, 0, sizeof(Expr));
144303
144304 pExpr->op = TK_COLUMN;
144305 pExpr->iColumn = p->pSub->nExpr-1;
144306 pExpr->iTable = p->pWin->iEphCsr;
144307 }
144308
144309 break;
144310 }
144311
144312 default: /* no-op */
144313 break;
144314 }
144315
144316 return WRC_Continue;
144317}
144318static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
144319 struct WindowRewrite *p = pWalker->u.pRewrite;
144320 Select *pSave = p->pSubSelect;
144321 if( pSave==pSelect ){
144322 return WRC_Continue;
144323 }else{
144324 p->pSubSelect = pSelect;
144325 sqlite3WalkSelect(pWalker, pSelect);
144326 p->pSubSelect = pSave;
144327 }
144328 return WRC_Prune;
144329}
144330
144331
144332/*
144333** Iterate through each expression in expression-list pEList. For each:
144334**
144335** * TK_COLUMN,
144336** * aggregate function, or
144337** * window function with a Window object that is not a member of the
144338** Window list passed as the second argument (pWin).
144339**
144340** Append the node to output expression-list (*ppSub). And replace it
144341** with a TK_COLUMN that reads the (N-1)th element of table
144342** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
144343** appending the new one.
144344*/
144345static void selectWindowRewriteEList(
144346 Parse *pParse,
144347 Window *pWin,
144348 SrcList *pSrc,
144349 ExprList *pEList, /* Rewrite expressions in this list */
144350 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
144351){
144352 Walker sWalker;
144353 WindowRewrite sRewrite;
144354
144355 memset(&sWalker, 0, sizeof(Walker));
144356 memset(&sRewrite, 0, sizeof(WindowRewrite));
144357
144358 sRewrite.pSub = *ppSub;
144359 sRewrite.pWin = pWin;
144360 sRewrite.pSrc = pSrc;
144361
144362 sWalker.pParse = pParse;
144363 sWalker.xExprCallback = selectWindowRewriteExprCb;
144364 sWalker.xSelectCallback = selectWindowRewriteSelectCb;
144365 sWalker.u.pRewrite = &sRewrite;
144366
144367 (void)sqlite3WalkExprList(&sWalker, pEList);
144368
144369 *ppSub = sRewrite.pSub;
144370}
144371
144372/*
144373** Append a copy of each expression in expression-list pAppend to
144374** expression list pList. Return a pointer to the result list.
144375*/
144376static ExprList *exprListAppendList(
144377 Parse *pParse, /* Parsing context */
144378 ExprList *pList, /* List to which to append. Might be NULL */
144379 ExprList *pAppend /* List of values to append. Might be NULL */
144380){
144381 if( pAppend ){
144382 int i;
144383 int nInit = pList ? pList->nExpr : 0;
144384 for(i=0; i<pAppend->nExpr; i++){
144385 Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
144386 pList = sqlite3ExprListAppend(pParse, pList, pDup);
144387 if( pList ) pList->a[nInit+i].sortOrder = pAppend->a[i].sortOrder;
144388 }
144389 }
144390 return pList;
144391}
144392
144393/*
144394** If the SELECT statement passed as the second argument does not invoke
144395** any SQL window functions, this function is a no-op. Otherwise, it
144396** rewrites the SELECT statement so that window function xStep functions
144397** are invoked in the correct order as described under "SELECT REWRITING"
144398** at the top of this file.
144399*/
144400SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
144401 int rc = SQLITE_OK;
144402 if( p->pWin ){
144403 Vdbe *v = sqlite3GetVdbe(pParse);
144404 sqlite3 *db = pParse->db;
144405 Select *pSub = 0; /* The subquery */
144406 SrcList *pSrc = p->pSrc;
144407 Expr *pWhere = p->pWhere;
144408 ExprList *pGroupBy = p->pGroupBy;
144409 Expr *pHaving = p->pHaving;
144410 ExprList *pSort = 0;
144411
144412 ExprList *pSublist = 0; /* Expression list for sub-query */
144413 Window *pMWin = p->pWin; /* Master window object */
144414 Window *pWin; /* Window object iterator */
144415
144416 p->pSrc = 0;
144417 p->pWhere = 0;
144418 p->pGroupBy = 0;
144419 p->pHaving = 0;
144420
144421 /* Create the ORDER BY clause for the sub-select. This is the concatenation
144422 ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
144423 ** redundant, remove the ORDER BY from the parent SELECT. */
144424 pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0);
144425 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy);
144426 if( pSort && p->pOrderBy ){
144427 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
144428 sqlite3ExprListDelete(db, p->pOrderBy);
144429 p->pOrderBy = 0;
144430 }
144431 }
144432
144433 /* Assign a cursor number for the ephemeral table used to buffer rows.
144434 ** The OpenEphemeral instruction is coded later, after it is known how
144435 ** many columns the table will have. */
144436 pMWin->iEphCsr = pParse->nTab++;
144437
144438 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, &pSublist);
144439 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, &pSublist);
144440 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
144441
144442 /* Append the PARTITION BY and ORDER BY expressions to the to the
144443 ** sub-select expression list. They are required to figure out where
144444 ** boundaries for partitions and sets of peer rows lie. */
144445 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition);
144446 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy);
144447
144448 /* Append the arguments passed to each window function to the
144449 ** sub-select expression list. Also allocate two registers for each
144450 ** window function - one for the accumulator, another for interim
144451 ** results. */
144452 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
144453 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
144454 pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList);
144455 if( pWin->pFilter ){
144456 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
144457 pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
144458 }
144459 pWin->regAccum = ++pParse->nMem;
144460 pWin->regResult = ++pParse->nMem;
144461 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
144462 }
144463
144464 /* If there is no ORDER BY or PARTITION BY clause, and the window
144465 ** function accepts zero arguments, and there are no other columns
144466 ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
144467 ** that pSublist is still NULL here. Add a constant expression here to
144468 ** keep everything legal in this case.
144469 */
144470 if( pSublist==0 ){
144471 pSublist = sqlite3ExprListAppend(pParse, 0,
144472 sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0)
144473 );
144474 }
144475
144476 pSub = sqlite3SelectNew(
144477 pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
144478 );
144479 p->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
144480 assert( p->pSrc || db->mallocFailed );
144481 if( p->pSrc ){
144482 p->pSrc->a[0].pSelect = pSub;
144483 sqlite3SrcListAssignCursors(pParse, p->pSrc);
144484 if( sqlite3ExpandSubquery(pParse, &p->pSrc->a[0]) ){
144485 rc = SQLITE_NOMEM;
144486 }else{
144487 pSub->selFlags |= SF_Expanded;
144488 p->selFlags &= ~SF_Aggregate;
144489 sqlite3SelectPrep(pParse, pSub, 0);
144490 }
144491
144492 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
144493 }else{
144494 sqlite3SelectDelete(db, pSub);
144495 }
144496 if( db->mallocFailed ) rc = SQLITE_NOMEM;
144497 }
144498
144499 return rc;
144500}
144501
144502/*
144503** Free the Window object passed as the second argument.
144504*/
144505SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3 *db, Window *p){
144506 if( p ){
144507 sqlite3ExprDelete(db, p->pFilter);
144508 sqlite3ExprListDelete(db, p->pPartition);
144509 sqlite3ExprListDelete(db, p->pOrderBy);
144510 sqlite3ExprDelete(db, p->pEnd);
144511 sqlite3ExprDelete(db, p->pStart);
144512 sqlite3DbFree(db, p->zName);
144513 sqlite3DbFree(db, p);
144514 }
144515}
144516
144517/*
144518** Free the linked list of Window objects starting at the second argument.
144519*/
144520SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p){
144521 while( p ){
144522 Window *pNext = p->pNextWin;
144523 sqlite3WindowDelete(db, p);
144524 p = pNext;
144525 }
144526}
144527
144528/*
144529** The argument expression is an PRECEDING or FOLLOWING offset. The
144530** value should be a non-negative integer. If the value is not a
144531** constant, change it to NULL. The fact that it is then a non-negative
144532** integer will be caught later. But it is important not to leave
144533** variable values in the expression tree.
144534*/
144535static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
144536 if( 0==sqlite3ExprIsConstant(pExpr) ){
144537 sqlite3ExprDelete(pParse->db, pExpr);
144538 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
144539 }
144540 return pExpr;
144541}
144542
144543/*
144544** Allocate and return a new Window object describing a Window Definition.
144545*/
144546SQLITE_PRIVATE Window *sqlite3WindowAlloc(
144547 Parse *pParse, /* Parsing context */
144548 int eType, /* Frame type. TK_RANGE or TK_ROWS */
144549 int eStart, /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
144550 Expr *pStart, /* Start window size if TK_PRECEDING or FOLLOWING */
144551 int eEnd, /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
144552 Expr *pEnd /* End window size if TK_FOLLOWING or PRECEDING */
144553){
144554 Window *pWin = 0;
144555
144556 /* Parser assures the following: */
144557 assert( eType==TK_RANGE || eType==TK_ROWS );
144558 assert( eStart==TK_CURRENT || eStart==TK_PRECEDING
144559 || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING );
144560 assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING
144561 || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING );
144562 assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) );
144563 assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) );
144564
144565
144566 /* If a frame is declared "RANGE" (not "ROWS"), then it may not use
144567 ** either "<expr> PRECEDING" or "<expr> FOLLOWING".
144568 */
144569 if( eType==TK_RANGE && (pStart!=0 || pEnd!=0) ){
144570 sqlite3ErrorMsg(pParse, "RANGE must use only UNBOUNDED or CURRENT ROW");
144571 goto windowAllocErr;
144572 }
144573
144574 /* Additionally, the
144575 ** starting boundary type may not occur earlier in the following list than
144576 ** the ending boundary type:
144577 **
144578 ** UNBOUNDED PRECEDING
144579 ** <expr> PRECEDING
144580 ** CURRENT ROW
144581 ** <expr> FOLLOWING
144582 ** UNBOUNDED FOLLOWING
144583 **
144584 ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
144585 ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
144586 ** frame boundary.
144587 */
144588 if( (eStart==TK_CURRENT && eEnd==TK_PRECEDING)
144589 || (eStart==TK_FOLLOWING && (eEnd==TK_PRECEDING || eEnd==TK_CURRENT))
144590 ){
144591 sqlite3ErrorMsg(pParse, "unsupported frame delimiter for ROWS");
144592 goto windowAllocErr;
144593 }
144594
144595 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
144596 if( pWin==0 ) goto windowAllocErr;
144597 pWin->eType = eType;
144598 pWin->eStart = eStart;
144599 pWin->eEnd = eEnd;
144600 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
144601 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
144602 return pWin;
144603
144604windowAllocErr:
144605 sqlite3ExprDelete(pParse->db, pEnd);
144606 sqlite3ExprDelete(pParse->db, pStart);
144607 return 0;
144608}
144609
144610/*
144611** Attach window object pWin to expression p.
144612*/
144613SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
144614 if( p ){
144615 /* This routine is only called for the parser. If pWin was not
144616 ** allocated due to an OOM, then the parser would fail before ever
144617 ** invoking this routine */
144618 if( ALWAYS(pWin) ){
144619 p->pWin = pWin;
144620 pWin->pOwner = p;
144621 if( p->flags & EP_Distinct ){
144622 sqlite3ErrorMsg(pParse,
144623 "DISTINCT is not supported for window functions");
144624 }
144625 }
144626 }else{
144627 sqlite3WindowDelete(pParse->db, pWin);
144628 }
144629}
144630
144631/*
144632** Return 0 if the two window objects are identical, or non-zero otherwise.
144633** Identical window objects can be processed in a single scan.
144634*/
144635SQLITE_PRIVATE int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){
144636 if( p1->eType!=p2->eType ) return 1;
144637 if( p1->eStart!=p2->eStart ) return 1;
144638 if( p1->eEnd!=p2->eEnd ) return 1;
144639 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
144640 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
144641 if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1;
144642 if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1;
144643 return 0;
144644}
144645
144646
144647/*
144648** This is called by code in select.c before it calls sqlite3WhereBegin()
144649** to begin iterating through the sub-query results. It is used to allocate
144650** and initialize registers and cursors used by sqlite3WindowCodeStep().
144651*/
144652SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){
144653 Window *pWin;
144654 Vdbe *v = sqlite3GetVdbe(pParse);
144655 int nPart = (pMWin->pPartition ? pMWin->pPartition->nExpr : 0);
144656 nPart += (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
144657 if( nPart ){
144658 pMWin->regPart = pParse->nMem+1;
144659 pParse->nMem += nPart;
144660 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nPart-1);
144661 }
144662
144663 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
144664 FuncDef *p = pWin->pFunc;
144665 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
144666 /* The inline versions of min() and max() require a single ephemeral
144667 ** table and 3 registers. The registers are used as follows:
144668 **
144669 ** regApp+0: slot to copy min()/max() argument to for MakeRecord
144670 ** regApp+1: integer value used to ensure keys are unique
144671 ** regApp+2: output of MakeRecord
144672 */
144673 ExprList *pList = pWin->pOwner->x.pList;
144674 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0);
144675 pWin->csrApp = pParse->nTab++;
144676 pWin->regApp = pParse->nMem+1;
144677 pParse->nMem += 3;
144678 if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){
144679 assert( pKeyInfo->aSortOrder[0]==0 );
144680 pKeyInfo->aSortOrder[0] = 1;
144681 }
144682 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
144683 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
144684 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
144685 }
144686 else if( p->zName==nth_valueName || p->zName==first_valueName ){
144687 /* Allocate two registers at pWin->regApp. These will be used to
144688 ** store the start and end index of the current frame. */
144689 assert( pMWin->iEphCsr );
144690 pWin->regApp = pParse->nMem+1;
144691 pWin->csrApp = pParse->nTab++;
144692 pParse->nMem += 2;
144693 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
144694 }
144695 else if( p->zName==leadName || p->zName==lagName ){
144696 assert( pMWin->iEphCsr );
144697 pWin->csrApp = pParse->nTab++;
144698 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
144699 }
144700 }
144701}
144702
144703/*
144704** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
144705** value of the second argument to nth_value() (eCond==2) has just been
144706** evaluated and the result left in register reg. This function generates VM
144707** code to check that the value is a non-negative integer and throws an
144708** exception if it is not.
144709*/
144710static void windowCheckIntValue(Parse *pParse, int reg, int eCond){
144711 static const char *azErr[] = {
144712 "frame starting offset must be a non-negative integer",
144713 "frame ending offset must be a non-negative integer",
144714 "second argument to nth_value must be a positive integer"
144715 };
144716 static int aOp[] = { OP_Ge, OP_Ge, OP_Gt };
144717 Vdbe *v = sqlite3GetVdbe(pParse);
144718 int regZero = sqlite3GetTempReg(pParse);
144719 assert( eCond==0 || eCond==1 || eCond==2 );
144720 sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
144721 sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
144722 VdbeCoverageIf(v, eCond==0);
144723 VdbeCoverageIf(v, eCond==1);
144724 VdbeCoverageIf(v, eCond==2);
144725 sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
144726 VdbeCoverageNeverNullIf(v, eCond==0);
144727 VdbeCoverageNeverNullIf(v, eCond==1);
144728 VdbeCoverageNeverNullIf(v, eCond==2);
144729 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
144730 sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC);
144731 sqlite3ReleaseTempReg(pParse, regZero);
144732}
144733
144734/*
144735** Return the number of arguments passed to the window-function associated
144736** with the object passed as the only argument to this function.
144737*/
144738static int windowArgCount(Window *pWin){
144739 ExprList *pList = pWin->pOwner->x.pList;
144740 return (pList ? pList->nExpr : 0);
144741}
144742
144743/*
144744** Generate VM code to invoke either xStep() (if bInverse is 0) or
144745** xInverse (if bInverse is non-zero) for each window function in the
144746** linked list starting at pMWin. Or, for built-in window functions
144747** that do not use the standard function API, generate the required
144748** inline VM code.
144749**
144750** If argument csr is greater than or equal to 0, then argument reg is
144751** the first register in an array of registers guaranteed to be large
144752** enough to hold the array of arguments for each function. In this case
144753** the arguments are extracted from the current row of csr into the
144754** array of registers before invoking OP_AggStep or OP_AggInverse
144755**
144756** Or, if csr is less than zero, then the array of registers at reg is
144757** already populated with all columns from the current row of the sub-query.
144758**
144759** If argument regPartSize is non-zero, then it is a register containing the
144760** number of rows in the current partition.
144761*/
144762static void windowAggStep(
144763 Parse *pParse,
144764 Window *pMWin, /* Linked list of window functions */
144765 int csr, /* Read arguments from this cursor */
144766 int bInverse, /* True to invoke xInverse instead of xStep */
144767 int reg, /* Array of registers */
144768 int regPartSize /* Register containing size of partition */
144769){
144770 Vdbe *v = sqlite3GetVdbe(pParse);
144771 Window *pWin;
144772 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
144773 int flags = pWin->pFunc->funcFlags;
144774 int regArg;
144775 int nArg = windowArgCount(pWin);
144776
144777 if( csr>=0 ){
144778 int i;
144779 for(i=0; i<nArg; i++){
144780 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
144781 }
144782 regArg = reg;
144783 if( flags & SQLITE_FUNC_WINDOW_SIZE ){
144784 if( nArg==0 ){
144785 regArg = regPartSize;
144786 }else{
144787 sqlite3VdbeAddOp2(v, OP_SCopy, regPartSize, reg+nArg);
144788 }
144789 nArg++;
144790 }
144791 }else{
144792 assert( !(flags & SQLITE_FUNC_WINDOW_SIZE) );
144793 regArg = reg + pWin->iArgCol;
144794 }
144795
144796 if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
144797 && pWin->eStart!=TK_UNBOUNDED
144798 ){
144799 int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regArg);
144800 VdbeCoverage(v);
144801 if( bInverse==0 ){
144802 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
144803 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
144804 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
144805 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
144806 }else{
144807 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
144808 VdbeCoverageNeverTaken(v);
144809 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
144810 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
144811 }
144812 sqlite3VdbeJumpHere(v, addrIsNull);
144813 }else if( pWin->regApp ){
144814 assert( pWin->pFunc->zName==nth_valueName
144815 || pWin->pFunc->zName==first_valueName
144816 );
144817 assert( bInverse==0 || bInverse==1 );
144818 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
144819 }else if( pWin->pFunc->zName==leadName
144820 || pWin->pFunc->zName==lagName
144821 ){
144822 /* no-op */
144823 }else{
144824 int addrIf = 0;
144825 if( pWin->pFilter ){
144826 int regTmp;
144827 assert( nArg==0 || nArg==pWin->pOwner->x.pList->nExpr );
144828 assert( nArg || pWin->pOwner->x.pList==0 );
144829 if( csr>0 ){
144830 regTmp = sqlite3GetTempReg(pParse);
144831 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
144832 }else{
144833 regTmp = regArg + nArg;
144834 }
144835 addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
144836 VdbeCoverage(v);
144837 if( csr>0 ){
144838 sqlite3ReleaseTempReg(pParse, regTmp);
144839 }
144840 }
144841 if( pWin->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
144842 CollSeq *pColl;
144843 assert( nArg>0 );
144844 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
144845 sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
144846 }
144847 sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep,
144848 bInverse, regArg, pWin->regAccum);
144849 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
144850 sqlite3VdbeChangeP5(v, (u8)nArg);
144851 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
144852 }
144853 }
144854}
144855
144856/*
144857** Generate VM code to invoke either xValue() (bFinal==0) or xFinalize()
144858** (bFinal==1) for each window function in the linked list starting at
144859** pMWin. Or, for built-in window-functions that do not use the standard
144860** API, generate the equivalent VM code.
144861*/
144862static void windowAggFinal(Parse *pParse, Window *pMWin, int bFinal){
144863 Vdbe *v = sqlite3GetVdbe(pParse);
144864 Window *pWin;
144865
144866 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
144867 if( (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
144868 && pWin->eStart!=TK_UNBOUNDED
144869 ){
144870 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
144871 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
144872 VdbeCoverage(v);
144873 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
144874 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
144875 if( bFinal ){
144876 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
144877 }
144878 }else if( pWin->regApp ){
144879 }else{
144880 if( bFinal ){
144881 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, windowArgCount(pWin));
144882 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
144883 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
144884 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
144885 }else{
144886 sqlite3VdbeAddOp3(v, OP_AggValue, pWin->regAccum, windowArgCount(pWin),
144887 pWin->regResult);
144888 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
144889 }
144890 }
144891 }
144892}
144893
144894/*
144895** This function generates VM code to invoke the sub-routine at address
144896** lblFlushPart once for each partition with the entire partition cached in
144897** the Window.iEphCsr temp table.
144898*/
144899static void windowPartitionCache(
144900 Parse *pParse,
144901 Select *p, /* The rewritten SELECT statement */
144902 WhereInfo *pWInfo, /* WhereInfo to call WhereEnd() on */
144903 int regFlushPart, /* Register to use with Gosub lblFlushPart */
144904 int lblFlushPart, /* Subroutine to Gosub to */
144905 int *pRegSize /* OUT: Register containing partition size */
144906){
144907 Window *pMWin = p->pWin;
144908 Vdbe *v = sqlite3GetVdbe(pParse);
144909 int iSubCsr = p->pSrc->a[0].iCursor;
144910 int nSub = p->pSrc->a[0].pTab->nCol;
144911 int k;
144912
144913 int reg = pParse->nMem+1;
144914 int regRecord = reg+nSub;
144915 int regRowid = regRecord+1;
144916
144917 *pRegSize = regRowid;
144918 pParse->nMem += nSub + 2;
144919
144920 /* Load the column values for the row returned by the sub-select
144921 ** into an array of registers starting at reg. */
144922 for(k=0; k<nSub; k++){
144923 sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
144924 }
144925 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, nSub, regRecord);
144926
144927 /* Check if this is the start of a new partition. If so, call the
144928 ** flush_partition sub-routine. */
144929 if( pMWin->pPartition ){
144930 int addr;
144931 ExprList *pPart = pMWin->pPartition;
144932 int nPart = pPart->nExpr;
144933 int regNewPart = reg + pMWin->nBufferCol;
144934 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
144935
144936 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
144937 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
144938 sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
144939 VdbeCoverageEqNe(v);
144940 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
144941 sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
144942 VdbeComment((v, "call flush_partition"));
144943 }
144944
144945 /* Buffer the current row in the ephemeral table. */
144946 sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
144947 sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
144948
144949 /* End of the input loop */
144950 sqlite3WhereEnd(pWInfo);
144951
144952 /* Invoke "flush_partition" to deal with the final (or only) partition */
144953 sqlite3VdbeAddOp2(v, OP_Gosub, regFlushPart, lblFlushPart);
144954 VdbeComment((v, "call flush_partition"));
144955}
144956
144957/*
144958** Invoke the sub-routine at regGosub (generated by code in select.c) to
144959** return the current row of Window.iEphCsr. If all window functions are
144960** aggregate window functions that use the standard API, a single
144961** OP_Gosub instruction is all that this routine generates. Extra VM code
144962** for per-row processing is only generated for the following built-in window
144963** functions:
144964**
144965** nth_value()
144966** first_value()
144967** lag()
144968** lead()
144969*/
144970static void windowReturnOneRow(
144971 Parse *pParse,
144972 Window *pMWin,
144973 int regGosub,
144974 int addrGosub
144975){
144976 Vdbe *v = sqlite3GetVdbe(pParse);
144977 Window *pWin;
144978 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
144979 FuncDef *pFunc = pWin->pFunc;
144980 if( pFunc->zName==nth_valueName
144981 || pFunc->zName==first_valueName
144982 ){
144983 int csr = pWin->csrApp;
144984 int lbl = sqlite3VdbeMakeLabel(v);
144985 int tmpReg = sqlite3GetTempReg(pParse);
144986 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
144987
144988 if( pFunc->zName==nth_valueName ){
144989 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+1,tmpReg);
144990 windowCheckIntValue(pParse, tmpReg, 2);
144991 }else{
144992 sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
144993 }
144994 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
144995 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
144996 VdbeCoverageNeverNull(v);
144997 sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg);
144998 VdbeCoverageNeverTaken(v);
144999 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
145000 sqlite3VdbeResolveLabel(v, lbl);
145001 sqlite3ReleaseTempReg(pParse, tmpReg);
145002 }
145003 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
145004 int nArg = pWin->pOwner->x.pList->nExpr;
145005 int iEph = pMWin->iEphCsr;
145006 int csr = pWin->csrApp;
145007 int lbl = sqlite3VdbeMakeLabel(v);
145008 int tmpReg = sqlite3GetTempReg(pParse);
145009
145010 if( nArg<3 ){
145011 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
145012 }else{
145013 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+2, pWin->regResult);
145014 }
145015 sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
145016 if( nArg<2 ){
145017 int val = (pFunc->zName==leadName ? 1 : -1);
145018 sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
145019 }else{
145020 int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
145021 int tmpReg2 = sqlite3GetTempReg(pParse);
145022 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
145023 sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
145024 sqlite3ReleaseTempReg(pParse, tmpReg2);
145025 }
145026
145027 sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
145028 VdbeCoverage(v);
145029 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
145030 sqlite3VdbeResolveLabel(v, lbl);
145031 sqlite3ReleaseTempReg(pParse, tmpReg);
145032 }
145033 }
145034 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
145035}
145036
145037/*
145038** Invoke the code generated by windowReturnOneRow() and, optionally, the
145039** xInverse() function for each window function, for one or more rows
145040** from the Window.iEphCsr temp table. This routine generates VM code
145041** similar to:
145042**
145043** while( regCtr>0 ){
145044** regCtr--;
145045** windowReturnOneRow()
145046** if( bInverse ){
145047** AggInverse
145048** }
145049** Next (Window.iEphCsr)
145050** }
145051*/
145052static void windowReturnRows(
145053 Parse *pParse,
145054 Window *pMWin, /* List of window functions */
145055 int regCtr, /* Register containing number of rows */
145056 int regGosub, /* Register for Gosub addrGosub */
145057 int addrGosub, /* Address of sub-routine for ReturnOneRow */
145058 int regInvArg, /* Array of registers for xInverse args */
145059 int regInvSize /* Register containing size of partition */
145060){
145061 int addr;
145062 Vdbe *v = sqlite3GetVdbe(pParse);
145063 windowAggFinal(pParse, pMWin, 0);
145064 addr = sqlite3VdbeAddOp3(v, OP_IfPos, regCtr, sqlite3VdbeCurrentAddr(v)+2 ,1);
145065 VdbeCoverage(v);
145066 sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
145067 windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
145068 if( regInvArg ){
145069 windowAggStep(pParse, pMWin, pMWin->iEphCsr, 1, regInvArg, regInvSize);
145070 }
145071 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, addr);
145072 VdbeCoverage(v);
145073 sqlite3VdbeJumpHere(v, addr+1); /* The OP_Goto */
145074}
145075
145076/*
145077** Generate code to set the accumulator register for each window function
145078** in the linked list passed as the second argument to NULL. And perform
145079** any equivalent initialization required by any built-in window functions
145080** in the list.
145081*/
145082static int windowInitAccum(Parse *pParse, Window *pMWin){
145083 Vdbe *v = sqlite3GetVdbe(pParse);
145084 int regArg;
145085 int nArg = 0;
145086 Window *pWin;
145087 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
145088 FuncDef *pFunc = pWin->pFunc;
145089 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
145090 nArg = MAX(nArg, windowArgCount(pWin));
145091 if( pFunc->zName==nth_valueName
145092 || pFunc->zName==first_valueName
145093 ){
145094 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
145095 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
145096 }
145097
145098 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
145099 assert( pWin->eStart!=TK_UNBOUNDED );
145100 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
145101 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
145102 }
145103 }
145104 regArg = pParse->nMem+1;
145105 pParse->nMem += nArg;
145106 return regArg;
145107}
145108
145109
145110/*
145111** This function does the work of sqlite3WindowCodeStep() for all "ROWS"
145112** window frame types except for "BETWEEN UNBOUNDED PRECEDING AND CURRENT
145113** ROW". Pseudo-code for each follows.
145114**
145115** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
145116**
145117** ...
145118** if( new partition ){
145119** Gosub flush_partition
145120** }
145121** Insert (record in eph-table)
145122** sqlite3WhereEnd()
145123** Gosub flush_partition
145124**
145125** flush_partition:
145126** Once {
145127** OpenDup (iEphCsr -> csrStart)
145128** OpenDup (iEphCsr -> csrEnd)
145129** }
145130** regStart = <expr1> // PRECEDING expression
145131** regEnd = <expr2> // FOLLOWING expression
145132** if( regStart<0 || regEnd<0 ){ error! }
145133** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
145134** Next(csrEnd) // if EOF skip Aggstep
145135** Aggstep (csrEnd)
145136** if( (regEnd--)<=0 ){
145137** AggFinal (xValue)
145138** Gosub addrGosub
145139** Next(csr) // if EOF goto flush_partition_done
145140** if( (regStart--)<=0 ){
145141** AggInverse (csrStart)
145142** Next(csrStart)
145143** }
145144** }
145145** flush_partition_done:
145146** ResetSorter (csr)
145147** Return
145148**
145149** ROWS BETWEEN <expr> PRECEDING AND CURRENT ROW
145150** ROWS BETWEEN CURRENT ROW AND <expr> FOLLOWING
145151** ROWS BETWEEN UNBOUNDED PRECEDING AND <expr> FOLLOWING
145152**
145153** These are similar to the above. For "CURRENT ROW", intialize the
145154** register to 0. For "UNBOUNDED PRECEDING" to infinity.
145155**
145156** ROWS BETWEEN <expr> PRECEDING AND UNBOUNDED FOLLOWING
145157** ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
145158**
145159** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
145160** while( 1 ){
145161** Next(csrEnd) // Exit while(1) at EOF
145162** Aggstep (csrEnd)
145163** }
145164** while( 1 ){
145165** AggFinal (xValue)
145166** Gosub addrGosub
145167** Next(csr) // if EOF goto flush_partition_done
145168** if( (regStart--)<=0 ){
145169** AggInverse (csrStart)
145170** Next(csrStart)
145171** }
145172** }
145173**
145174** For the "CURRENT ROW AND UNBOUNDED FOLLOWING" case, the final if()
145175** condition is always true (as if regStart were initialized to 0).
145176**
145177** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
145178**
145179** This is the only RANGE case handled by this routine. It modifies the
145180** second while( 1 ) loop in "ROWS BETWEEN CURRENT ... UNBOUNDED..." to
145181** be:
145182**
145183** while( 1 ){
145184** AggFinal (xValue)
145185** while( 1 ){
145186** regPeer++
145187** Gosub addrGosub
145188** Next(csr) // if EOF goto flush_partition_done
145189** if( new peer ) break;
145190** }
145191** while( (regPeer--)>0 ){
145192** AggInverse (csrStart)
145193** Next(csrStart)
145194** }
145195** }
145196**
145197** ROWS BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING
145198**
145199** regEnd = regEnd - regStart
145200** Rewind (csr,csrStart,csrEnd) // if EOF goto flush_partition_done
145201** Aggstep (csrEnd)
145202** Next(csrEnd) // if EOF fall-through
145203** if( (regEnd--)<=0 ){
145204** if( (regStart--)<=0 ){
145205** AggFinal (xValue)
145206** Gosub addrGosub
145207** Next(csr) // if EOF goto flush_partition_done
145208** }
145209** AggInverse (csrStart)
145210** Next (csrStart)
145211** }
145212**
145213** ROWS BETWEEN <expr> PRECEDING AND <expr> PRECEDING
145214**
145215** Replace the bit after "Rewind" in the above with:
145216**
145217** if( (regEnd--)<=0 ){
145218** AggStep (csrEnd)
145219** Next (csrEnd)
145220** }
145221** AggFinal (xValue)
145222** Gosub addrGosub
145223** Next(csr) // if EOF goto flush_partition_done
145224** if( (regStart--)<=0 ){
145225** AggInverse (csr2)
145226** Next (csr2)
145227** }
145228**
145229*/
145230static void windowCodeRowExprStep(
145231 Parse *pParse,
145232 Select *p,
145233 WhereInfo *pWInfo,
145234 int regGosub,
145235 int addrGosub
145236){
145237 Window *pMWin = p->pWin;
145238 Vdbe *v = sqlite3GetVdbe(pParse);
145239 int regFlushPart; /* Register for "Gosub flush_partition" */
145240 int lblFlushPart; /* Label for "Gosub flush_partition" */
145241 int lblFlushDone; /* Label for "Gosub flush_partition_done" */
145242
145243 int regArg;
145244 int addr;
145245 int csrStart = pParse->nTab++;
145246 int csrEnd = pParse->nTab++;
145247 int regStart; /* Value of <expr> PRECEDING */
145248 int regEnd; /* Value of <expr> FOLLOWING */
145249 int addrGoto;
145250 int addrTop;
145251 int addrIfPos1 = 0;
145252 int addrIfPos2 = 0;
145253 int regSize = 0;
145254
145255 assert( pMWin->eStart==TK_PRECEDING
145256 || pMWin->eStart==TK_CURRENT
145257 || pMWin->eStart==TK_FOLLOWING
145258 || pMWin->eStart==TK_UNBOUNDED
145259 );
145260 assert( pMWin->eEnd==TK_FOLLOWING
145261 || pMWin->eEnd==TK_CURRENT
145262 || pMWin->eEnd==TK_UNBOUNDED
145263 || pMWin->eEnd==TK_PRECEDING
145264 );
145265
145266 /* Allocate register and label for the "flush_partition" sub-routine. */
145267 regFlushPart = ++pParse->nMem;
145268 lblFlushPart = sqlite3VdbeMakeLabel(v);
145269 lblFlushDone = sqlite3VdbeMakeLabel(v);
145270
145271 regStart = ++pParse->nMem;
145272 regEnd = ++pParse->nMem;
145273
145274 windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
145275
145276 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
145277
145278 /* Start of "flush_partition" */
145279 sqlite3VdbeResolveLabel(v, lblFlushPart);
145280 sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+3);
145281 VdbeCoverage(v);
145282 VdbeComment((v, "Flush_partition subroutine"));
145283 sqlite3VdbeAddOp2(v, OP_OpenDup, csrStart, pMWin->iEphCsr);
145284 sqlite3VdbeAddOp2(v, OP_OpenDup, csrEnd, pMWin->iEphCsr);
145285
145286 /* If either regStart or regEnd are not non-negative integers, throw
145287 ** an exception. */
145288 if( pMWin->pStart ){
145289 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
145290 windowCheckIntValue(pParse, regStart, 0);
145291 }
145292 if( pMWin->pEnd ){
145293 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
145294 windowCheckIntValue(pParse, regEnd, 1);
145295 }
145296
145297 /* If this is "ROWS <expr1> FOLLOWING AND ROWS <expr2> FOLLOWING", do:
145298 **
145299 ** if( regEnd<regStart ){
145300 ** // The frame always consists of 0 rows
145301 ** regStart = regSize;
145302 ** }
145303 ** regEnd = regEnd - regStart;
145304 */
145305 if( pMWin->pEnd && pMWin->eStart==TK_FOLLOWING ){
145306 assert( pMWin->pStart!=0 );
145307 assert( pMWin->eEnd==TK_FOLLOWING );
145308 sqlite3VdbeAddOp3(v, OP_Ge, regStart, sqlite3VdbeCurrentAddr(v)+2, regEnd);
145309 VdbeCoverageNeverNull(v);
145310 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
145311 sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regEnd);
145312 }
145313
145314 if( pMWin->pStart && pMWin->eEnd==TK_PRECEDING ){
145315 assert( pMWin->pEnd!=0 );
145316 assert( pMWin->eStart==TK_PRECEDING );
145317 sqlite3VdbeAddOp3(v, OP_Le, regStart, sqlite3VdbeCurrentAddr(v)+3, regEnd);
145318 VdbeCoverageNeverNull(v);
145319 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regStart);
145320 sqlite3VdbeAddOp2(v, OP_Copy, regSize, regEnd);
145321 }
145322
145323 /* Initialize the accumulator register for each window function to NULL */
145324 regArg = windowInitAccum(pParse, pMWin);
145325
145326 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblFlushDone);
145327 VdbeCoverage(v);
145328 sqlite3VdbeAddOp2(v, OP_Rewind, csrStart, lblFlushDone);
145329 VdbeCoverageNeverTaken(v);
145330 sqlite3VdbeChangeP5(v, 1);
145331 sqlite3VdbeAddOp2(v, OP_Rewind, csrEnd, lblFlushDone);
145332 VdbeCoverageNeverTaken(v);
145333 sqlite3VdbeChangeP5(v, 1);
145334
145335 /* Invoke AggStep function for each window function using the row that
145336 ** csrEnd currently points to. Or, if csrEnd is already at EOF,
145337 ** do nothing. */
145338 addrTop = sqlite3VdbeCurrentAddr(v);
145339 if( pMWin->eEnd==TK_PRECEDING ){
145340 addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
145341 VdbeCoverage(v);
145342 }
145343 sqlite3VdbeAddOp2(v, OP_Next, csrEnd, sqlite3VdbeCurrentAddr(v)+2);
145344 VdbeCoverage(v);
145345 addr = sqlite3VdbeAddOp0(v, OP_Goto);
145346 windowAggStep(pParse, pMWin, csrEnd, 0, regArg, regSize);
145347 if( pMWin->eEnd==TK_UNBOUNDED ){
145348 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
145349 sqlite3VdbeJumpHere(v, addr);
145350 addrTop = sqlite3VdbeCurrentAddr(v);
145351 }else{
145352 sqlite3VdbeJumpHere(v, addr);
145353 if( pMWin->eEnd==TK_PRECEDING ){
145354 sqlite3VdbeJumpHere(v, addrIfPos1);
145355 }
145356 }
145357
145358 if( pMWin->eEnd==TK_FOLLOWING ){
145359 addrIfPos1 = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0 , 1);
145360 VdbeCoverage(v);
145361 }
145362 if( pMWin->eStart==TK_FOLLOWING ){
145363 addrIfPos2 = sqlite3VdbeAddOp3(v, OP_IfPos, regStart, 0 , 1);
145364 VdbeCoverage(v);
145365 }
145366 windowAggFinal(pParse, pMWin, 0);
145367 windowReturnOneRow(pParse, pMWin, regGosub, addrGosub);
145368 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)+2);
145369 VdbeCoverage(v);
145370 sqlite3VdbeAddOp2(v, OP_Goto, 0, lblFlushDone);
145371 if( pMWin->eStart==TK_FOLLOWING ){
145372 sqlite3VdbeJumpHere(v, addrIfPos2);
145373 }
145374
145375 if( pMWin->eStart==TK_CURRENT
145376 || pMWin->eStart==TK_PRECEDING
145377 || pMWin->eStart==TK_FOLLOWING
145378 ){
145379 int lblSkipInverse = sqlite3VdbeMakeLabel(v);;
145380 if( pMWin->eStart==TK_PRECEDING ){
145381 sqlite3VdbeAddOp3(v, OP_IfPos, regStart, lblSkipInverse, 1);
145382 VdbeCoverage(v);
145383 }
145384 if( pMWin->eStart==TK_FOLLOWING ){
145385 sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+2);
145386 VdbeCoverage(v);
145387 sqlite3VdbeAddOp2(v, OP_Goto, 0, lblSkipInverse);
145388 }else{
145389 sqlite3VdbeAddOp2(v, OP_Next, csrStart, sqlite3VdbeCurrentAddr(v)+1);
145390 VdbeCoverageAlwaysTaken(v);
145391 }
145392 windowAggStep(pParse, pMWin, csrStart, 1, regArg, regSize);
145393 sqlite3VdbeResolveLabel(v, lblSkipInverse);
145394 }
145395 if( pMWin->eEnd==TK_FOLLOWING ){
145396 sqlite3VdbeJumpHere(v, addrIfPos1);
145397 }
145398 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
145399
145400 /* flush_partition_done: */
145401 sqlite3VdbeResolveLabel(v, lblFlushDone);
145402 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
145403 sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
145404 VdbeComment((v, "end flush_partition subroutine"));
145405
145406 /* Jump to here to skip over flush_partition */
145407 sqlite3VdbeJumpHere(v, addrGoto);
145408}
145409
145410/*
145411** This function does the work of sqlite3WindowCodeStep() for cases that
145412** would normally be handled by windowCodeDefaultStep() when there are
145413** one or more built-in window-functions that require the entire partition
145414** to be cached in a temp table before any rows can be returned. Additionally.
145415** "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" is always handled by
145416** this function.
145417**
145418** Pseudo-code corresponding to the VM code generated by this function
145419** for each type of window follows.
145420**
145421** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
145422**
145423** flush_partition:
145424** Once {
145425** OpenDup (iEphCsr -> csrLead)
145426** }
145427** Integer ctr 0
145428** foreach row (csrLead){
145429** if( new peer ){
145430** AggFinal (xValue)
145431** for(i=0; i<ctr; i++){
145432** Gosub addrGosub
145433** Next iEphCsr
145434** }
145435** Integer ctr 0
145436** }
145437** AggStep (csrLead)
145438** Incr ctr
145439** }
145440**
145441** AggFinal (xFinalize)
145442** for(i=0; i<ctr; i++){
145443** Gosub addrGosub
145444** Next iEphCsr
145445** }
145446**
145447** ResetSorter (csr)
145448** Return
145449**
145450** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
145451**
145452** As above, except that the "if( new peer )" branch is always taken.
145453**
145454** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
145455**
145456** As above, except that each of the for() loops becomes:
145457**
145458** for(i=0; i<ctr; i++){
145459** Gosub addrGosub
145460** AggInverse (iEphCsr)
145461** Next iEphCsr
145462** }
145463**
145464** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
145465**
145466** flush_partition:
145467** Once {
145468** OpenDup (iEphCsr -> csrLead)
145469** }
145470** foreach row (csrLead) {
145471** AggStep (csrLead)
145472** }
145473** foreach row (iEphCsr) {
145474** Gosub addrGosub
145475** }
145476**
145477** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
145478**
145479** flush_partition:
145480** Once {
145481** OpenDup (iEphCsr -> csrLead)
145482** }
145483** foreach row (csrLead){
145484** AggStep (csrLead)
145485** }
145486** Rewind (csrLead)
145487** Integer ctr 0
145488** foreach row (csrLead){
145489** if( new peer ){
145490** AggFinal (xValue)
145491** for(i=0; i<ctr; i++){
145492** Gosub addrGosub
145493** AggInverse (iEphCsr)
145494** Next iEphCsr
145495** }
145496** Integer ctr 0
145497** }
145498** Incr ctr
145499** }
145500**
145501** AggFinal (xFinalize)
145502** for(i=0; i<ctr; i++){
145503** Gosub addrGosub
145504** Next iEphCsr
145505** }
145506**
145507** ResetSorter (csr)
145508** Return
145509*/
145510static void windowCodeCacheStep(
145511 Parse *pParse,
145512 Select *p,
145513 WhereInfo *pWInfo,
145514 int regGosub,
145515 int addrGosub
145516){
145517 Window *pMWin = p->pWin;
145518 Vdbe *v = sqlite3GetVdbe(pParse);
145519 int k;
145520 int addr;
145521 ExprList *pPart = pMWin->pPartition;
145522 ExprList *pOrderBy = pMWin->pOrderBy;
145523 int nPeer = pOrderBy ? pOrderBy->nExpr : 0;
145524 int regNewPeer;
145525
145526 int addrGoto; /* Address of Goto used to jump flush_par.. */
145527 int addrNext; /* Jump here for next iteration of loop */
145528 int regFlushPart;
145529 int lblFlushPart;
145530 int csrLead;
145531 int regCtr;
145532 int regArg; /* Register array to martial function args */
145533 int regSize;
145534 int lblEmpty;
145535 int bReverse = pMWin->pOrderBy && pMWin->eStart==TK_CURRENT
145536 && pMWin->eEnd==TK_UNBOUNDED;
145537
145538 assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
145539 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
145540 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
145541 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED)
145542 );
145543
145544 lblEmpty = sqlite3VdbeMakeLabel(v);
145545 regNewPeer = pParse->nMem+1;
145546 pParse->nMem += nPeer;
145547
145548 /* Allocate register and label for the "flush_partition" sub-routine. */
145549 regFlushPart = ++pParse->nMem;
145550 lblFlushPart = sqlite3VdbeMakeLabel(v);
145551
145552 csrLead = pParse->nTab++;
145553 regCtr = ++pParse->nMem;
145554
145555 windowPartitionCache(pParse, p, pWInfo, regFlushPart, lblFlushPart, &regSize);
145556 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
145557
145558 /* Start of "flush_partition" */
145559 sqlite3VdbeResolveLabel(v, lblFlushPart);
145560 sqlite3VdbeAddOp2(v, OP_Once, 0, sqlite3VdbeCurrentAddr(v)+2);
145561 VdbeCoverage(v);
145562 sqlite3VdbeAddOp2(v, OP_OpenDup, csrLead, pMWin->iEphCsr);
145563
145564 /* Initialize the accumulator register for each window function to NULL */
145565 regArg = windowInitAccum(pParse, pMWin);
145566
145567 sqlite3VdbeAddOp2(v, OP_Integer, 0, regCtr);
145568 sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
145569 VdbeCoverage(v);
145570 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr, lblEmpty);
145571 VdbeCoverageNeverTaken(v);
145572
145573 if( bReverse ){
145574 int addr2 = sqlite3VdbeCurrentAddr(v);
145575 windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
145576 sqlite3VdbeAddOp2(v, OP_Next, csrLead, addr2);
145577 VdbeCoverage(v);
145578 sqlite3VdbeAddOp2(v, OP_Rewind, csrLead, lblEmpty);
145579 VdbeCoverageNeverTaken(v);
145580 }
145581 addrNext = sqlite3VdbeCurrentAddr(v);
145582
145583 if( pOrderBy && (pMWin->eEnd==TK_CURRENT || pMWin->eStart==TK_CURRENT) ){
145584 int bCurrent = (pMWin->eStart==TK_CURRENT);
145585 int addrJump = 0; /* Address of OP_Jump below */
145586 if( pMWin->eType==TK_RANGE ){
145587 int iOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
145588 int regPeer = pMWin->regPart + (pPart ? pPart->nExpr : 0);
145589 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
145590 for(k=0; k<nPeer; k++){
145591 sqlite3VdbeAddOp3(v, OP_Column, csrLead, iOff+k, regNewPeer+k);
145592 }
145593 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
145594 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
145595 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
145596 VdbeCoverage(v);
145597 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, nPeer-1);
145598 }
145599
145600 windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub,
145601 (bCurrent ? regArg : 0), (bCurrent ? regSize : 0)
145602 );
145603 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
145604 }
145605
145606 if( bReverse==0 ){
145607 windowAggStep(pParse, pMWin, csrLead, 0, regArg, regSize);
145608 }
145609 sqlite3VdbeAddOp2(v, OP_AddImm, regCtr, 1);
145610 sqlite3VdbeAddOp2(v, OP_Next, csrLead, addrNext);
145611 VdbeCoverage(v);
145612
145613 windowReturnRows(pParse, pMWin, regCtr, regGosub, addrGosub, 0, 0);
145614
145615 sqlite3VdbeResolveLabel(v, lblEmpty);
145616 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
145617 sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
145618
145619 /* Jump to here to skip over flush_partition */
145620 sqlite3VdbeJumpHere(v, addrGoto);
145621}
145622
145623
145624/*
145625** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
145626**
145627** ...
145628** if( new partition ){
145629** AggFinal (xFinalize)
145630** Gosub addrGosub
145631** ResetSorter eph-table
145632** }
145633** else if( new peer ){
145634** AggFinal (xValue)
145635** Gosub addrGosub
145636** ResetSorter eph-table
145637** }
145638** AggStep
145639** Insert (record into eph-table)
145640** sqlite3WhereEnd()
145641** AggFinal (xFinalize)
145642** Gosub addrGosub
145643**
145644** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
145645**
145646** As above, except take no action for a "new peer". Invoke
145647** the sub-routine once only for each partition.
145648**
145649** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
145650**
145651** As above, except that the "new peer" condition is handled in the
145652** same way as "new partition" (so there is no "else if" block).
145653**
145654** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
145655**
145656** As above, except assume every row is a "new peer".
145657*/
145658static void windowCodeDefaultStep(
145659 Parse *pParse,
145660 Select *p,
145661 WhereInfo *pWInfo,
145662 int regGosub,
145663 int addrGosub
145664){
145665 Window *pMWin = p->pWin;
145666 Vdbe *v = sqlite3GetVdbe(pParse);
145667 int k;
145668 int iSubCsr = p->pSrc->a[0].iCursor;
145669 int nSub = p->pSrc->a[0].pTab->nCol;
145670 int reg = pParse->nMem+1;
145671 int regRecord = reg+nSub;
145672 int regRowid = regRecord+1;
145673 int addr;
145674 ExprList *pPart = pMWin->pPartition;
145675 ExprList *pOrderBy = pMWin->pOrderBy;
145676
145677 assert( pMWin->eType==TK_RANGE
145678 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
145679 );
145680
145681 assert( (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_CURRENT)
145682 || (pMWin->eStart==TK_UNBOUNDED && pMWin->eEnd==TK_UNBOUNDED)
145683 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_CURRENT)
145684 || (pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED && !pOrderBy)
145685 );
145686
145687 if( pMWin->eEnd==TK_UNBOUNDED ){
145688 pOrderBy = 0;
145689 }
145690
145691 pParse->nMem += nSub + 2;
145692
145693 /* Load the individual column values of the row returned by
145694 ** the sub-select into an array of registers. */
145695 for(k=0; k<nSub; k++){
145696 sqlite3VdbeAddOp3(v, OP_Column, iSubCsr, k, reg+k);
145697 }
145698
145699 /* Check if this is the start of a new partition or peer group. */
145700 if( pPart || pOrderBy ){
145701 int nPart = (pPart ? pPart->nExpr : 0);
145702 int addrGoto = 0;
145703 int addrJump = 0;
145704 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
145705
145706 if( pPart ){
145707 int regNewPart = reg + pMWin->nBufferCol;
145708 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
145709 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart,nPart);
145710 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
145711 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
145712 VdbeCoverageEqNe(v);
145713 windowAggFinal(pParse, pMWin, 1);
145714 if( pOrderBy ){
145715 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
145716 }
145717 }
145718
145719 if( pOrderBy ){
145720 int regNewPeer = reg + pMWin->nBufferCol + nPart;
145721 int regPeer = pMWin->regPart + nPart;
145722
145723 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
145724 if( pMWin->eType==TK_RANGE ){
145725 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
145726 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPeer, regPeer, nPeer);
145727 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
145728 addrJump = sqlite3VdbeAddOp3(v, OP_Jump, addr+2, 0, addr+2);
145729 VdbeCoverage(v);
145730 }else{
145731 addrJump = 0;
145732 }
145733 windowAggFinal(pParse, pMWin, pMWin->eStart==TK_CURRENT);
145734 if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
145735 }
145736
145737 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
145738 VdbeCoverage(v);
145739 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
145740 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
145741 VdbeCoverage(v);
145742
145743 sqlite3VdbeAddOp1(v, OP_ResetSorter, pMWin->iEphCsr);
145744 sqlite3VdbeAddOp3(
145745 v, OP_Copy, reg+pMWin->nBufferCol, pMWin->regPart, nPart+nPeer-1
145746 );
145747
145748 if( addrJump ) sqlite3VdbeJumpHere(v, addrJump);
145749 }
145750
145751 /* Invoke step function for window functions */
145752 windowAggStep(pParse, pMWin, -1, 0, reg, 0);
145753
145754 /* Buffer the current row in the ephemeral table. */
145755 if( pMWin->nBufferCol>0 ){
145756 sqlite3VdbeAddOp3(v, OP_MakeRecord, reg, pMWin->nBufferCol, regRecord);
145757 }else{
145758 sqlite3VdbeAddOp2(v, OP_Blob, 0, regRecord);
145759 sqlite3VdbeAppendP4(v, (void*)"", 0);
145760 }
145761 sqlite3VdbeAddOp2(v, OP_NewRowid, pMWin->iEphCsr, regRowid);
145762 sqlite3VdbeAddOp3(v, OP_Insert, pMWin->iEphCsr, regRecord, regRowid);
145763
145764 /* End the database scan loop. */
145765 sqlite3WhereEnd(pWInfo);
145766
145767 windowAggFinal(pParse, pMWin, 1);
145768 sqlite3VdbeAddOp2(v, OP_Rewind, pMWin->iEphCsr,sqlite3VdbeCurrentAddr(v)+3);
145769 VdbeCoverage(v);
145770 sqlite3VdbeAddOp2(v, OP_Gosub, regGosub, addrGosub);
145771 sqlite3VdbeAddOp2(v, OP_Next, pMWin->iEphCsr, sqlite3VdbeCurrentAddr(v)-1);
145772 VdbeCoverage(v);
145773}
145774
145775/*
145776** Allocate and return a duplicate of the Window object indicated by the
145777** third argument. Set the Window.pOwner field of the new object to
145778** pOwner.
145779*/
145780SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
145781 Window *pNew = 0;
145782 if( p ){
145783 pNew = sqlite3DbMallocZero(db, sizeof(Window));
145784 if( pNew ){
145785 pNew->zName = sqlite3DbStrDup(db, p->zName);
145786 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
145787 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
145788 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
145789 pNew->eType = p->eType;
145790 pNew->eEnd = p->eEnd;
145791 pNew->eStart = p->eStart;
145792 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
145793 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
145794 pNew->pOwner = pOwner;
145795 }
145796 }
145797 return pNew;
145798}
145799
145800/*
145801** Return a copy of the linked list of Window objects passed as the
145802** second argument.
145803*/
145804SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p){
145805 Window *pWin;
145806 Window *pRet = 0;
145807 Window **pp = &pRet;
145808
145809 for(pWin=p; pWin; pWin=pWin->pNextWin){
145810 *pp = sqlite3WindowDup(db, 0, pWin);
145811 if( *pp==0 ) break;
145812 pp = &((*pp)->pNextWin);
145813 }
145814
145815 return pRet;
145816}
145817
145818/*
145819** sqlite3WhereBegin() has already been called for the SELECT statement
145820** passed as the second argument when this function is invoked. It generates
145821** code to populate the Window.regResult register for each window function and
145822** invoke the sub-routine at instruction addrGosub once for each row.
145823** This function calls sqlite3WhereEnd() before returning.
145824*/
145825SQLITE_PRIVATE void sqlite3WindowCodeStep(
145826 Parse *pParse, /* Parse context */
145827 Select *p, /* Rewritten SELECT statement */
145828 WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */
145829 int regGosub, /* Register for OP_Gosub */
145830 int addrGosub /* OP_Gosub here to return each row */
145831){
145832 Window *pMWin = p->pWin;
145833
145834 /* There are three different functions that may be used to do the work
145835 ** of this one, depending on the window frame and the specific built-in
145836 ** window functions used (if any).
145837 **
145838 ** windowCodeRowExprStep() handles all "ROWS" window frames, except for:
145839 **
145840 ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
145841 **
145842 ** The exception is because windowCodeRowExprStep() implements all window
145843 ** frame types by caching the entire partition in a temp table, and
145844 ** "ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" is easy enough to
145845 ** implement without such a cache.
145846 **
145847 ** windowCodeCacheStep() is used for:
145848 **
145849 ** RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
145850 **
145851 ** It is also used for anything not handled by windowCodeRowExprStep()
145852 ** that invokes a built-in window function that requires the entire
145853 ** partition to be cached in a temp table before any rows are returned
145854 ** (e.g. nth_value() or percent_rank()).
145855 **
145856 ** Finally, assuming there is no built-in window function that requires
145857 ** the partition to be cached, windowCodeDefaultStep() is used for:
145858 **
145859 ** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
145860 ** RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
145861 ** RANGE BETWEEN CURRENT ROW AND CURRENT ROW
145862 ** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
145863 **
145864 ** windowCodeDefaultStep() is the only one of the three functions that
145865 ** does not cache each partition in a temp table before beginning to
145866 ** return rows.
145867 */
145868 if( pMWin->eType==TK_ROWS
145869 && (pMWin->eStart!=TK_UNBOUNDED||pMWin->eEnd!=TK_CURRENT||!pMWin->pOrderBy)
145870 ){
145871 VdbeModuleComment((pParse->pVdbe, "Begin RowExprStep()"));
145872 windowCodeRowExprStep(pParse, p, pWInfo, regGosub, addrGosub);
145873 }else{
145874 Window *pWin;
145875 int bCache = 0; /* True to use CacheStep() */
145876
145877 if( pMWin->eStart==TK_CURRENT && pMWin->eEnd==TK_UNBOUNDED ){
145878 bCache = 1;
145879 }else{
145880 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
145881 FuncDef *pFunc = pWin->pFunc;
145882 if( (pFunc->funcFlags & SQLITE_FUNC_WINDOW_SIZE)
145883 || (pFunc->zName==nth_valueName)
145884 || (pFunc->zName==first_valueName)
145885 || (pFunc->zName==leadName)
145886 || (pFunc->zName==lagName)
145887 ){
145888 bCache = 1;
145889 break;
145890 }
145891 }
145892 }
145893
145894 /* Otherwise, call windowCodeDefaultStep(). */
145895 if( bCache ){
145896 VdbeModuleComment((pParse->pVdbe, "Begin CacheStep()"));
145897 windowCodeCacheStep(pParse, p, pWInfo, regGosub, addrGosub);
145898 }else{
145899 VdbeModuleComment((pParse->pVdbe, "Begin DefaultStep()"));
145900 windowCodeDefaultStep(pParse, p, pWInfo, regGosub, addrGosub);
145901 }
145902 }
145903}
145904
145905#endif /* SQLITE_OMIT_WINDOWFUNC */
145906
145907/************** End of window.c **********************************************/
145908/************** Begin file parse.c *******************************************/
145909/*
145910** 2000-05-29
145911**
145912** The author disclaims copyright to this source code. In place of
145913** a legal notice, here is a blessing:
145914**
145915** May you do good and not evil.
145916** May you find forgiveness for yourself and forgive others.
145917** May you share freely, never taking more than you give.
145918**
145919*************************************************************************
145920** Driver template for the LEMON parser generator.
145921**
145922** The "lemon" program processes an LALR(1) input grammar file, then uses
145923** this template to construct a parser. The "lemon" program inserts text
145924** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
145925** interstitial "-" characters) contained in this template is changed into
145926** the value of the %name directive from the grammar. Otherwise, the content
145927** of this template is copied straight through into the generate parser
145928** source file.
145929**
145930** The following is the concatenation of all %include directives from the
145931** input grammar file:
145932*/
145933/* #include <stdio.h> */
145934/************ Begin %include sections from the grammar ************************/
145935
145936/* #include "sqliteInt.h" */
145937
145938/*
145939** Disable all error recovery processing in the parser push-down
145940** automaton.
145941*/
145942#define YYNOERRORRECOVERY 1
145943
145944/*
145945** Make yytestcase() the same as testcase()
145946*/
145947#define yytestcase(X) testcase(X)
145948
145949/*
145950** Indicate that sqlite3ParserFree() will never be called with a null
145951** pointer.
145952*/
145953#define YYPARSEFREENEVERNULL 1
145954
145955/*
145956** In the amalgamation, the parse.c file generated by lemon and the
145957** tokenize.c file are concatenated. In that case, sqlite3RunParser()
145958** has access to the the size of the yyParser object and so the parser
145959** engine can be allocated from stack. In that case, only the
145960** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
145961** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
145962** omitted.
145963*/
145964#ifdef SQLITE_AMALGAMATION
145965# define sqlite3Parser_ENGINEALWAYSONSTACK 1
145966#endif
145967
145968/*
145969** Alternative datatype for the argument to the malloc() routine passed
145970** into sqlite3ParserAlloc(). The default is size_t.
145971*/
145972#define YYMALLOCARGTYPE u64
145973
145974/*
145975** An instance of the following structure describes the event of a
145976** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
145977** TK_DELETE, or TK_INSTEAD. If the event is of the form
145978**
145979** UPDATE ON (a,b,c)
145980**
145981** Then the "b" IdList records the list "a,b,c".
145982*/
145983struct TrigEvent { int a; IdList * b; };
145984
145985struct FrameBound { int eType; Expr *pExpr; };
145986
145987/*
145988** Disable lookaside memory allocation for objects that might be
145989** shared across database connections.
145990*/
145991static void disableLookaside(Parse *pParse){
145992 pParse->disableLookaside++;
145993 pParse->db->lookaside.bDisable++;
145994}
145995
145996
145997 /*
145998 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
145999 ** all elements in the list. And make sure list length does not exceed
146000 ** SQLITE_LIMIT_COMPOUND_SELECT.
146001 */
146002 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
146003 if( p->pPrior ){
146004 Select *pNext = 0, *pLoop;
146005 int mxSelect, cnt = 0;
146006 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
146007 pLoop->pNext = pNext;
146008 pLoop->selFlags |= SF_Compound;
146009 }
146010 if( (p->selFlags & SF_MultiValue)==0 &&
146011 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
146012 cnt>mxSelect
146013 ){
146014 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
146015 }
146016 }
146017 }
146018
146019
146020 /* Construct a new Expr object from a single identifier. Use the
146021 ** new Expr to populate pOut. Set the span of pOut to be the identifier
146022 ** that created the expression.
146023 */
146024 static Expr *tokenExpr(Parse *pParse, int op, Token t){
146025 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
146026 if( p ){
146027 /* memset(p, 0, sizeof(Expr)); */
146028 p->op = (u8)op;
146029 p->affinity = 0;
146030 p->flags = EP_Leaf;
146031 p->iAgg = -1;
146032 p->pLeft = p->pRight = 0;
146033 p->x.pList = 0;
146034 p->pAggInfo = 0;
146035 p->pTab = 0;
146036 p->op2 = 0;
146037 p->iTable = 0;
146038 p->iColumn = 0;
146039#ifndef SQLITE_OMIT_WINDOWFUNC
146040 p->pWin = 0;
146041#endif
146042 p->u.zToken = (char*)&p[1];
146043 memcpy(p->u.zToken, t.z, t.n);
146044 p->u.zToken[t.n] = 0;
146045 if( sqlite3Isquote(p->u.zToken[0]) ){
146046 if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted;
146047 sqlite3Dequote(p->u.zToken);
146048 }
146049#if SQLITE_MAX_EXPR_DEPTH>0
146050 p->nHeight = 1;
146051#endif
146052 if( IN_RENAME_OBJECT ){
146053 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
146054 }
146055 }
146056 return p;
146057 }
146058
146059
146060 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
146061 ** unary TK_ISNULL or TK_NOTNULL expression. */
146062 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
146063 sqlite3 *db = pParse->db;
146064 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
146065 pA->op = (u8)op;
146066 sqlite3ExprDelete(db, pA->pRight);
146067 pA->pRight = 0;
146068 }
146069 }
146070
146071 /* Add a single new term to an ExprList that is used to store a
146072 ** list of identifiers. Report an error if the ID list contains
146073 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
146074 ** error while parsing a legacy schema.
146075 */
146076 static ExprList *parserAddExprIdListTerm(
146077 Parse *pParse,
146078 ExprList *pPrior,
146079 Token *pIdToken,
146080 int hasCollate,
146081 int sortOrder
146082 ){
146083 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
146084 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
146085 && pParse->db->init.busy==0
146086 ){
146087 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
146088 pIdToken->n, pIdToken->z);
146089 }
146090 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
146091 return p;
146092 }
146093/**************** End of %include directives **********************************/
146094/* These constants specify the various numeric values for terminal symbols
146095** in a format understandable to "makeheaders". This section is blank unless
146096** "lemon" is run with the "-m" command-line option.
146097***************** Begin makeheaders token definitions *************************/
146098/**************** End makeheaders token definitions ***************************/
146099
146100/* The next sections is a series of control #defines.
146101** various aspects of the generated parser.
146102** YYCODETYPE is the data type used to store the integer codes
146103** that represent terminal and non-terminal symbols.
146104** "unsigned char" is used if there are fewer than
146105** 256 symbols. Larger types otherwise.
146106** YYNOCODE is a number of type YYCODETYPE that is not used for
146107** any terminal or nonterminal symbol.
146108** YYFALLBACK If defined, this indicates that one or more tokens
146109** (also known as: "terminal symbols") have fall-back
146110** values which should be used if the original symbol
146111** would not parse. This permits keywords to sometimes
146112** be used as identifiers, for example.
146113** YYACTIONTYPE is the data type used for "action codes" - numbers
146114** that indicate what to do in response to the next
146115** token.
146116** sqlite3ParserTOKENTYPE is the data type used for minor type for terminal
146117** symbols. Background: A "minor type" is a semantic
146118** value associated with a terminal or non-terminal
146119** symbols. For example, for an "ID" terminal symbol,
146120** the minor type might be the name of the identifier.
146121** Each non-terminal can have a different minor type.
146122** Terminal symbols all have the same minor type, though.
146123** This macros defines the minor type for terminal
146124** symbols.
146125** YYMINORTYPE is the data type used for all minor types.
146126** This is typically a union of many types, one of
146127** which is sqlite3ParserTOKENTYPE. The entry in the union
146128** for terminal symbols is called "yy0".
146129** YYSTACKDEPTH is the maximum depth of the parser's stack. If
146130** zero the stack is dynamically sized using realloc()
146131** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument
146132** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument
146133** sqlite3ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
146134** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser
146135** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser
146136** sqlite3ParserCTX_* As sqlite3ParserARG_ except for %extra_context
146137** YYERRORSYMBOL is the code number of the error symbol. If not
146138** defined, then do no error processing.
146139** YYNSTATE the combined number of states.
146140** YYNRULE the number of rules in the grammar
146141** YYNTOKEN Number of terminal symbols
146142** YY_MAX_SHIFT Maximum value for shift actions
146143** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
146144** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
146145** YY_ERROR_ACTION The yy_action[] code for syntax error
146146** YY_ACCEPT_ACTION The yy_action[] code for accept
146147** YY_NO_ACTION The yy_action[] code for no-op
146148** YY_MIN_REDUCE Minimum value for reduce actions
146149** YY_MAX_REDUCE Maximum value for reduce actions
146150*/
146151#ifndef INTERFACE
146152# define INTERFACE 1
146153#endif
146154/************* Begin control #defines *****************************************/
146155#define YYCODETYPE unsigned short int
146156#define YYNOCODE 277
146157#define YYACTIONTYPE unsigned short int
146158#define YYWILDCARD 91
146159#define sqlite3ParserTOKENTYPE Token
146160typedef union {
146161 int yyinit;
146162 sqlite3ParserTOKENTYPE yy0;
146163 Expr* yy18;
146164 struct TrigEvent yy34;
146165 IdList* yy48;
146166 int yy70;
146167 struct {int value; int mask;} yy111;
146168 struct FrameBound yy119;
146169 SrcList* yy135;
146170 TriggerStep* yy207;
146171 Window* yy327;
146172 Upsert* yy340;
146173 const char* yy392;
146174 ExprList* yy420;
146175 With* yy449;
146176 Select* yy489;
146177} YYMINORTYPE;
146178#ifndef YYSTACKDEPTH
146179#define YYSTACKDEPTH 100
146180#endif
146181#define sqlite3ParserARG_SDECL
146182#define sqlite3ParserARG_PDECL
146183#define sqlite3ParserARG_PARAM
146184#define sqlite3ParserARG_FETCH
146185#define sqlite3ParserARG_STORE
146186#define sqlite3ParserCTX_SDECL Parse *pParse;
146187#define sqlite3ParserCTX_PDECL ,Parse *pParse
146188#define sqlite3ParserCTX_PARAM ,pParse
146189#define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
146190#define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
146191#define YYFALLBACK 1
146192#define YYNSTATE 521
146193#define YYNRULE 367
146194#define YYNTOKEN 155
146195#define YY_MAX_SHIFT 520
146196#define YY_MIN_SHIFTREDUCE 756
146197#define YY_MAX_SHIFTREDUCE 1122
146198#define YY_ERROR_ACTION 1123
146199#define YY_ACCEPT_ACTION 1124
146200#define YY_NO_ACTION 1125
146201#define YY_MIN_REDUCE 1126
146202#define YY_MAX_REDUCE 1492
146203/************* End control #defines *******************************************/
146204#define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
146205
146206/* Define the yytestcase() macro to be a no-op if is not already defined
146207** otherwise.
146208**
146209** Applications can choose to define yytestcase() in the %include section
146210** to a macro that can assist in verifying code coverage. For production
146211** code the yytestcase() macro should be turned off. But it is useful
146212** for testing.
146213*/
146214#ifndef yytestcase
146215# define yytestcase(X)
146216#endif
146217
146218
146219/* Next are the tables used to determine what action to take based on the
146220** current state and lookahead token. These tables are used to implement
146221** functions that take a state number and lookahead value and return an
146222** action integer.
146223**
146224** Suppose the action integer is N. Then the action is determined as
146225** follows
146226**
146227** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
146228** token onto the stack and goto state N.
146229**
146230** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
146231** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
146232**
146233** N == YY_ERROR_ACTION A syntax error has occurred.
146234**
146235** N == YY_ACCEPT_ACTION The parser accepts its input.
146236**
146237** N == YY_NO_ACTION No such action. Denotes unused
146238** slots in the yy_action[] table.
146239**
146240** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
146241** and YY_MAX_REDUCE
146242**
146243** The action table is constructed as a single large table named yy_action[].
146244** Given state S and lookahead X, the action is computed as either:
146245**
146246** (A) N = yy_action[ yy_shift_ofst[S] + X ]
146247** (B) N = yy_default[S]
146248**
146249** The (A) formula is preferred. The B formula is used instead if
146250** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
146251**
146252** The formulas above are for computing the action when the lookahead is
146253** a terminal symbol. If the lookahead is a non-terminal (as occurs after
146254** a reduce action) then the yy_reduce_ofst[] array is used in place of
146255** the yy_shift_ofst[] array.
146256**
146257** The following are the tables generated in this section:
146258**
146259** yy_action[] A single table containing all actions.
146260** yy_lookahead[] A table containing the lookahead for each entry in
146261** yy_action. Used to detect hash collisions.
146262** yy_shift_ofst[] For each state, the offset into yy_action for
146263** shifting terminals.
146264** yy_reduce_ofst[] For each state, the offset into yy_action for
146265** shifting non-terminals after a reduce.
146266** yy_default[] Default action for each state.
146267**
146268*********** Begin parsing tables **********************************************/
146269#define YY_ACTTAB_COUNT (2009)
146270static const YYACTIONTYPE yy_action[] = {
146271 /* 0 */ 368, 105, 102, 197, 105, 102, 197, 515, 1124, 1,
146272 /* 10 */ 1, 520, 2, 1128, 515, 1192, 1171, 1456, 275, 370,
146273 /* 20 */ 127, 1389, 1197, 1197, 1192, 1166, 178, 1205, 64, 64,
146274 /* 30 */ 477, 887, 322, 428, 348, 37, 37, 808, 362, 888,
146275 /* 40 */ 509, 509, 509, 112, 113, 103, 1100, 1100, 953, 956,
146276 /* 50 */ 946, 946, 110, 110, 111, 111, 111, 111, 365, 252,
146277 /* 60 */ 252, 515, 252, 252, 497, 515, 309, 515, 459, 515,
146278 /* 70 */ 1079, 491, 512, 478, 6, 512, 809, 134, 498, 228,
146279 /* 80 */ 194, 428, 37, 37, 515, 208, 64, 64, 64, 64,
146280 /* 90 */ 13, 13, 109, 109, 109, 109, 108, 108, 107, 107,
146281 /* 100 */ 107, 106, 401, 258, 381, 13, 13, 398, 397, 428,
146282 /* 110 */ 252, 252, 370, 476, 405, 1104, 1079, 1080, 1081, 386,
146283 /* 120 */ 1106, 390, 497, 512, 497, 1423, 1419, 304, 1105, 307,
146284 /* 130 */ 1256, 496, 370, 499, 16, 16, 112, 113, 103, 1100,
146285 /* 140 */ 1100, 953, 956, 946, 946, 110, 110, 111, 111, 111,
146286 /* 150 */ 111, 262, 1107, 495, 1107, 401, 112, 113, 103, 1100,
146287 /* 160 */ 1100, 953, 956, 946, 946, 110, 110, 111, 111, 111,
146288 /* 170 */ 111, 129, 1425, 343, 1420, 339, 1059, 492, 1057, 263,
146289 /* 180 */ 73, 105, 102, 197, 994, 109, 109, 109, 109, 108,
146290 /* 190 */ 108, 107, 107, 107, 106, 401, 370, 111, 111, 111,
146291 /* 200 */ 111, 104, 492, 89, 1432, 109, 109, 109, 109, 108,
146292 /* 210 */ 108, 107, 107, 107, 106, 401, 111, 111, 111, 111,
146293 /* 220 */ 112, 113, 103, 1100, 1100, 953, 956, 946, 946, 110,
146294 /* 230 */ 110, 111, 111, 111, 111, 109, 109, 109, 109, 108,
146295 /* 240 */ 108, 107, 107, 107, 106, 401, 114, 108, 108, 107,
146296 /* 250 */ 107, 107, 106, 401, 109, 109, 109, 109, 108, 108,
146297 /* 260 */ 107, 107, 107, 106, 401, 152, 399, 399, 399, 109,
146298 /* 270 */ 109, 109, 109, 108, 108, 107, 107, 107, 106, 401,
146299 /* 280 */ 178, 493, 1412, 434, 1037, 1486, 1079, 515, 1486, 370,
146300 /* 290 */ 421, 297, 357, 412, 74, 1079, 109, 109, 109, 109,
146301 /* 300 */ 108, 108, 107, 107, 107, 106, 401, 1413, 37, 37,
146302 /* 310 */ 1431, 274, 506, 112, 113, 103, 1100, 1100, 953, 956,
146303 /* 320 */ 946, 946, 110, 110, 111, 111, 111, 111, 1436, 520,
146304 /* 330 */ 2, 1128, 1079, 1080, 1081, 430, 275, 1079, 127, 366,
146305 /* 340 */ 933, 1079, 1080, 1081, 220, 1205, 913, 458, 455, 454,
146306 /* 350 */ 392, 167, 515, 1035, 152, 445, 924, 453, 152, 874,
146307 /* 360 */ 923, 289, 109, 109, 109, 109, 108, 108, 107, 107,
146308 /* 370 */ 107, 106, 401, 13, 13, 261, 853, 252, 252, 227,
146309 /* 380 */ 106, 401, 370, 1079, 1080, 1081, 311, 388, 1079, 296,
146310 /* 390 */ 512, 923, 923, 925, 231, 323, 1255, 1388, 1423, 490,
146311 /* 400 */ 274, 506, 12, 208, 274, 506, 112, 113, 103, 1100,
146312 /* 410 */ 1100, 953, 956, 946, 946, 110, 110, 111, 111, 111,
146313 /* 420 */ 111, 1440, 286, 1128, 288, 1079, 1097, 247, 275, 1098,
146314 /* 430 */ 127, 387, 405, 389, 1079, 1080, 1081, 1205, 159, 238,
146315 /* 440 */ 255, 321, 461, 316, 460, 225, 790, 105, 102, 197,
146316 /* 450 */ 513, 314, 842, 842, 445, 109, 109, 109, 109, 108,
146317 /* 460 */ 108, 107, 107, 107, 106, 401, 515, 514, 515, 252,
146318 /* 470 */ 252, 1079, 1080, 1081, 435, 370, 1098, 933, 1460, 794,
146319 /* 480 */ 274, 506, 512, 105, 102, 197, 336, 63, 63, 64,
146320 /* 490 */ 64, 27, 790, 924, 287, 208, 1354, 923, 515, 112,
146321 /* 500 */ 113, 103, 1100, 1100, 953, 956, 946, 946, 110, 110,
146322 /* 510 */ 111, 111, 111, 111, 107, 107, 107, 106, 401, 49,
146323 /* 520 */ 49, 515, 28, 1079, 405, 497, 421, 297, 923, 923,
146324 /* 530 */ 925, 186, 468, 1079, 467, 999, 999, 442, 515, 1079,
146325 /* 540 */ 334, 515, 45, 45, 1083, 342, 173, 168, 109, 109,
146326 /* 550 */ 109, 109, 108, 108, 107, 107, 107, 106, 401, 13,
146327 /* 560 */ 13, 205, 13, 13, 252, 252, 1195, 1195, 370, 1079,
146328 /* 570 */ 1080, 1081, 787, 265, 5, 359, 494, 512, 469, 1079,
146329 /* 580 */ 1080, 1081, 398, 397, 1079, 1079, 1080, 1081, 3, 282,
146330 /* 590 */ 1079, 1083, 112, 113, 103, 1100, 1100, 953, 956, 946,
146331 /* 600 */ 946, 110, 110, 111, 111, 111, 111, 252, 252, 1015,
146332 /* 610 */ 220, 1079, 873, 458, 455, 454, 943, 943, 954, 957,
146333 /* 620 */ 512, 252, 252, 453, 1016, 1079, 445, 1107, 1209, 1107,
146334 /* 630 */ 1079, 1080, 1081, 515, 512, 426, 1079, 1080, 1081, 1017,
146335 /* 640 */ 512, 109, 109, 109, 109, 108, 108, 107, 107, 107,
146336 /* 650 */ 106, 401, 1052, 515, 50, 50, 515, 1079, 1080, 1081,
146337 /* 660 */ 828, 370, 1051, 379, 411, 1064, 1358, 207, 408, 773,
146338 /* 670 */ 829, 1079, 1080, 1081, 64, 64, 322, 64, 64, 1302,
146339 /* 680 */ 947, 411, 410, 1358, 1360, 112, 113, 103, 1100, 1100,
146340 /* 690 */ 953, 956, 946, 946, 110, 110, 111, 111, 111, 111,
146341 /* 700 */ 294, 482, 515, 1037, 1487, 515, 434, 1487, 354, 1120,
146342 /* 710 */ 483, 996, 913, 485, 466, 996, 132, 178, 33, 450,
146343 /* 720 */ 1203, 136, 406, 64, 64, 479, 64, 64, 419, 369,
146344 /* 730 */ 283, 1146, 252, 252, 109, 109, 109, 109, 108, 108,
146345 /* 740 */ 107, 107, 107, 106, 401, 512, 224, 440, 411, 266,
146346 /* 750 */ 1358, 266, 252, 252, 370, 296, 416, 284, 934, 396,
146347 /* 760 */ 976, 470, 400, 252, 252, 512, 9, 473, 231, 500,
146348 /* 770 */ 354, 1036, 1035, 1488, 355, 374, 512, 1121, 112, 113,
146349 /* 780 */ 103, 1100, 1100, 953, 956, 946, 946, 110, 110, 111,
146350 /* 790 */ 111, 111, 111, 252, 252, 1015, 515, 1347, 295, 252,
146351 /* 800 */ 252, 252, 252, 1098, 375, 249, 512, 445, 872, 322,
146352 /* 810 */ 1016, 480, 512, 195, 512, 434, 273, 15, 15, 515,
146353 /* 820 */ 314, 515, 95, 515, 93, 1017, 367, 109, 109, 109,
146354 /* 830 */ 109, 108, 108, 107, 107, 107, 106, 401, 515, 1121,
146355 /* 840 */ 39, 39, 51, 51, 52, 52, 503, 370, 515, 1204,
146356 /* 850 */ 1098, 918, 439, 341, 133, 436, 223, 222, 221, 53,
146357 /* 860 */ 53, 322, 1400, 761, 762, 763, 515, 370, 88, 54,
146358 /* 870 */ 54, 112, 113, 103, 1100, 1100, 953, 956, 946, 946,
146359 /* 880 */ 110, 110, 111, 111, 111, 111, 407, 55, 55, 196,
146360 /* 890 */ 515, 112, 113, 103, 1100, 1100, 953, 956, 946, 946,
146361 /* 900 */ 110, 110, 111, 111, 111, 111, 135, 264, 1149, 376,
146362 /* 910 */ 515, 40, 40, 515, 872, 515, 993, 515, 993, 116,
146363 /* 920 */ 109, 109, 109, 109, 108, 108, 107, 107, 107, 106,
146364 /* 930 */ 401, 41, 41, 515, 43, 43, 44, 44, 56, 56,
146365 /* 940 */ 109, 109, 109, 109, 108, 108, 107, 107, 107, 106,
146366 /* 950 */ 401, 515, 379, 515, 57, 57, 515, 799, 515, 379,
146367 /* 960 */ 515, 445, 200, 515, 323, 515, 1397, 515, 1459, 515,
146368 /* 970 */ 1287, 817, 58, 58, 14, 14, 515, 59, 59, 118,
146369 /* 980 */ 118, 60, 60, 515, 46, 46, 61, 61, 62, 62,
146370 /* 990 */ 47, 47, 515, 190, 189, 91, 515, 140, 140, 515,
146371 /* 1000 */ 394, 515, 277, 1200, 141, 141, 515, 1115, 515, 992,
146372 /* 1010 */ 515, 992, 515, 69, 69, 370, 278, 48, 48, 259,
146373 /* 1020 */ 65, 65, 119, 119, 246, 246, 260, 66, 66, 120,
146374 /* 1030 */ 120, 121, 121, 117, 117, 370, 515, 512, 383, 112,
146375 /* 1040 */ 113, 103, 1100, 1100, 953, 956, 946, 946, 110, 110,
146376 /* 1050 */ 111, 111, 111, 111, 515, 872, 515, 139, 139, 112,
146377 /* 1060 */ 113, 103, 1100, 1100, 953, 956, 946, 946, 110, 110,
146378 /* 1070 */ 111, 111, 111, 111, 1287, 138, 138, 125, 125, 515,
146379 /* 1080 */ 12, 515, 281, 1287, 515, 445, 131, 1287, 109, 109,
146380 /* 1090 */ 109, 109, 108, 108, 107, 107, 107, 106, 401, 515,
146381 /* 1100 */ 124, 124, 122, 122, 515, 123, 123, 515, 109, 109,
146382 /* 1110 */ 109, 109, 108, 108, 107, 107, 107, 106, 401, 515,
146383 /* 1120 */ 68, 68, 463, 783, 515, 70, 70, 302, 67, 67,
146384 /* 1130 */ 1032, 253, 253, 356, 1287, 191, 196, 1433, 465, 1301,
146385 /* 1140 */ 38, 38, 384, 94, 512, 42, 42, 177, 848, 274,
146386 /* 1150 */ 506, 385, 420, 847, 1356, 441, 508, 376, 377, 153,
146387 /* 1160 */ 423, 872, 432, 370, 224, 251, 194, 887, 182, 293,
146388 /* 1170 */ 783, 848, 88, 254, 466, 888, 847, 915, 807, 806,
146389 /* 1180 */ 230, 1241, 910, 370, 17, 413, 797, 112, 113, 103,
146390 /* 1190 */ 1100, 1100, 953, 956, 946, 946, 110, 110, 111, 111,
146391 /* 1200 */ 111, 111, 395, 814, 815, 1175, 983, 112, 101, 103,
146392 /* 1210 */ 1100, 1100, 953, 956, 946, 946, 110, 110, 111, 111,
146393 /* 1220 */ 111, 111, 375, 422, 427, 429, 298, 230, 230, 88,
146394 /* 1230 */ 1240, 451, 312, 797, 226, 88, 109, 109, 109, 109,
146395 /* 1240 */ 108, 108, 107, 107, 107, 106, 401, 86, 433, 979,
146396 /* 1250 */ 927, 881, 226, 983, 230, 415, 109, 109, 109, 109,
146397 /* 1260 */ 108, 108, 107, 107, 107, 106, 401, 320, 845, 781,
146398 /* 1270 */ 846, 100, 130, 100, 1403, 290, 370, 319, 1377, 1376,
146399 /* 1280 */ 437, 1449, 299, 1237, 303, 306, 308, 310, 1188, 1174,
146400 /* 1290 */ 1173, 1172, 315, 324, 325, 1228, 370, 927, 1249, 271,
146401 /* 1300 */ 1286, 113, 103, 1100, 1100, 953, 956, 946, 946, 110,
146402 /* 1310 */ 110, 111, 111, 111, 111, 1224, 1235, 502, 501, 1292,
146403 /* 1320 */ 1221, 1155, 103, 1100, 1100, 953, 956, 946, 946, 110,
146404 /* 1330 */ 110, 111, 111, 111, 111, 1148, 1137, 1136, 1138, 1443,
146405 /* 1340 */ 446, 244, 184, 98, 507, 188, 4, 353, 327, 109,
146406 /* 1350 */ 109, 109, 109, 108, 108, 107, 107, 107, 106, 401,
146407 /* 1360 */ 510, 329, 331, 199, 414, 456, 292, 285, 318, 109,
146408 /* 1370 */ 109, 109, 109, 108, 108, 107, 107, 107, 106, 401,
146409 /* 1380 */ 11, 1271, 1279, 402, 361, 192, 1171, 1351, 431, 505,
146410 /* 1390 */ 346, 1350, 333, 98, 507, 504, 4, 187, 1446, 1115,
146411 /* 1400 */ 233, 1396, 155, 1394, 1112, 152, 72, 75, 378, 425,
146412 /* 1410 */ 510, 165, 149, 157, 933, 1276, 86, 30, 1268, 417,
146413 /* 1420 */ 96, 96, 8, 160, 161, 162, 163, 97, 418, 402,
146414 /* 1430 */ 517, 516, 449, 402, 923, 210, 358, 424, 1282, 438,
146415 /* 1440 */ 169, 214, 360, 1345, 80, 504, 31, 444, 1365, 301,
146416 /* 1450 */ 245, 274, 506, 216, 174, 305, 488, 447, 217, 462,
146417 /* 1460 */ 1139, 487, 218, 363, 933, 923, 923, 925, 926, 24,
146418 /* 1470 */ 96, 96, 1191, 1190, 1189, 391, 1182, 97, 1163, 402,
146419 /* 1480 */ 517, 516, 799, 364, 923, 1162, 317, 1161, 98, 507,
146420 /* 1490 */ 1181, 4, 1458, 472, 393, 269, 270, 475, 481, 1232,
146421 /* 1500 */ 85, 1233, 326, 328, 232, 510, 495, 1231, 330, 98,
146422 /* 1510 */ 507, 1230, 4, 486, 335, 923, 923, 925, 926, 24,
146423 /* 1520 */ 1435, 1068, 404, 181, 336, 256, 510, 115, 402, 332,
146424 /* 1530 */ 352, 352, 351, 241, 349, 1214, 1414, 770, 338, 10,
146425 /* 1540 */ 504, 340, 272, 92, 1331, 1213, 87, 183, 484, 402,
146426 /* 1550 */ 201, 488, 280, 239, 344, 345, 489, 1145, 29, 933,
146427 /* 1560 */ 279, 504, 1074, 518, 240, 96, 96, 242, 243, 519,
146428 /* 1570 */ 1134, 1129, 97, 154, 402, 517, 516, 372, 373, 923,
146429 /* 1580 */ 933, 142, 143, 128, 1381, 267, 96, 96, 852, 757,
146430 /* 1590 */ 203, 144, 403, 97, 1382, 402, 517, 516, 204, 1380,
146431 /* 1600 */ 923, 146, 1379, 1159, 1158, 71, 1156, 276, 202, 185,
146432 /* 1610 */ 923, 923, 925, 926, 24, 198, 257, 126, 991, 989,
146433 /* 1620 */ 907, 98, 507, 156, 4, 145, 158, 206, 831, 209,
146434 /* 1630 */ 291, 923, 923, 925, 926, 24, 1005, 911, 510, 164,
146435 /* 1640 */ 147, 380, 371, 382, 166, 76, 77, 274, 506, 148,
146436 /* 1650 */ 78, 79, 1008, 211, 212, 1004, 137, 213, 18, 300,
146437 /* 1660 */ 230, 402, 997, 1109, 443, 215, 32, 170, 171, 772,
146438 /* 1670 */ 409, 448, 319, 504, 219, 172, 452, 81, 19, 457,
146439 /* 1680 */ 313, 20, 82, 268, 488, 150, 810, 179, 83, 487,
146440 /* 1690 */ 464, 151, 933, 180, 959, 84, 1040, 34, 96, 96,
146441 /* 1700 */ 471, 1041, 35, 474, 193, 97, 248, 402, 517, 516,
146442 /* 1710 */ 1068, 404, 923, 250, 256, 880, 229, 175, 875, 352,
146443 /* 1720 */ 352, 351, 241, 349, 100, 21, 770, 22, 1054, 1056,
146444 /* 1730 */ 7, 98, 507, 1045, 4, 337, 1058, 23, 974, 201,
146445 /* 1740 */ 176, 280, 88, 923, 923, 925, 926, 24, 510, 279,
146446 /* 1750 */ 960, 958, 962, 1014, 963, 1013, 235, 234, 25, 36,
146447 /* 1760 */ 99, 90, 507, 928, 4, 511, 350, 782, 26, 841,
146448 /* 1770 */ 236, 402, 347, 1069, 237, 1125, 1125, 1451, 510, 203,
146449 /* 1780 */ 1450, 1125, 1125, 504, 1125, 1125, 1125, 204, 1125, 1125,
146450 /* 1790 */ 146, 1125, 1125, 1125, 1125, 1125, 1125, 202, 1125, 1125,
146451 /* 1800 */ 1125, 402, 933, 1125, 1125, 1125, 1125, 1125, 96, 96,
146452 /* 1810 */ 1125, 1125, 1125, 504, 1125, 97, 1125, 402, 517, 516,
146453 /* 1820 */ 1125, 1125, 923, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
146454 /* 1830 */ 1125, 371, 933, 1125, 1125, 1125, 274, 506, 96, 96,
146455 /* 1840 */ 1125, 1125, 1125, 1125, 1125, 97, 1125, 402, 517, 516,
146456 /* 1850 */ 1125, 1125, 923, 923, 923, 925, 926, 24, 1125, 409,
146457 /* 1860 */ 1125, 1125, 1125, 256, 1125, 1125, 1125, 1125, 352, 352,
146458 /* 1870 */ 351, 241, 349, 1125, 1125, 770, 1125, 1125, 1125, 1125,
146459 /* 1880 */ 1125, 1125, 1125, 923, 923, 925, 926, 24, 201, 1125,
146460 /* 1890 */ 280, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 279, 1125,
146461 /* 1900 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
146462 /* 1910 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
146463 /* 1920 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 203, 1125,
146464 /* 1930 */ 1125, 1125, 1125, 1125, 1125, 1125, 204, 1125, 1125, 146,
146465 /* 1940 */ 1125, 1125, 1125, 1125, 1125, 1125, 202, 1125, 1125, 1125,
146466 /* 1950 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
146467 /* 1960 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
146468 /* 1970 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
146469 /* 1980 */ 371, 1125, 1125, 1125, 1125, 274, 506, 1125, 1125, 1125,
146470 /* 1990 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125,
146471 /* 2000 */ 1125, 1125, 1125, 1125, 1125, 1125, 1125, 1125, 409,
146472};
146473static const YYCODETYPE yy_lookahead[] = {
146474 /* 0 */ 184, 238, 239, 240, 238, 239, 240, 163, 155, 156,
146475 /* 10 */ 157, 158, 159, 160, 163, 191, 192, 183, 165, 19,
146476 /* 20 */ 167, 258, 202, 203, 200, 191, 163, 174, 184, 185,
146477 /* 30 */ 174, 31, 163, 163, 171, 184, 185, 35, 175, 39,
146478 /* 40 */ 179, 180, 181, 43, 44, 45, 46, 47, 48, 49,
146479 /* 50 */ 50, 51, 52, 53, 54, 55, 56, 57, 184, 206,
146480 /* 60 */ 207, 163, 206, 207, 220, 163, 16, 163, 66, 163,
146481 /* 70 */ 59, 270, 219, 229, 273, 219, 74, 208, 174, 223,
146482 /* 80 */ 224, 163, 184, 185, 163, 232, 184, 185, 184, 185,
146483 /* 90 */ 184, 185, 92, 93, 94, 95, 96, 97, 98, 99,
146484 /* 100 */ 100, 101, 102, 233, 198, 184, 185, 96, 97, 163,
146485 /* 110 */ 206, 207, 19, 163, 261, 104, 105, 106, 107, 198,
146486 /* 120 */ 109, 119, 220, 219, 220, 274, 275, 77, 117, 79,
146487 /* 130 */ 187, 229, 19, 229, 184, 185, 43, 44, 45, 46,
146488 /* 140 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
146489 /* 150 */ 57, 233, 141, 134, 143, 102, 43, 44, 45, 46,
146490 /* 160 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
146491 /* 170 */ 57, 152, 274, 216, 276, 218, 83, 163, 85, 233,
146492 /* 180 */ 67, 238, 239, 240, 11, 92, 93, 94, 95, 96,
146493 /* 190 */ 97, 98, 99, 100, 101, 102, 19, 54, 55, 56,
146494 /* 200 */ 57, 58, 163, 26, 163, 92, 93, 94, 95, 96,
146495 /* 210 */ 97, 98, 99, 100, 101, 102, 54, 55, 56, 57,
146496 /* 220 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
146497 /* 230 */ 53, 54, 55, 56, 57, 92, 93, 94, 95, 96,
146498 /* 240 */ 97, 98, 99, 100, 101, 102, 69, 96, 97, 98,
146499 /* 250 */ 99, 100, 101, 102, 92, 93, 94, 95, 96, 97,
146500 /* 260 */ 98, 99, 100, 101, 102, 81, 179, 180, 181, 92,
146501 /* 270 */ 93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
146502 /* 280 */ 163, 267, 268, 163, 22, 23, 59, 163, 26, 19,
146503 /* 290 */ 117, 118, 175, 109, 24, 59, 92, 93, 94, 95,
146504 /* 300 */ 96, 97, 98, 99, 100, 101, 102, 268, 184, 185,
146505 /* 310 */ 269, 127, 128, 43, 44, 45, 46, 47, 48, 49,
146506 /* 320 */ 50, 51, 52, 53, 54, 55, 56, 57, 157, 158,
146507 /* 330 */ 159, 160, 105, 106, 107, 163, 165, 59, 167, 184,
146508 /* 340 */ 90, 105, 106, 107, 108, 174, 73, 111, 112, 113,
146509 /* 350 */ 19, 22, 163, 91, 81, 163, 106, 121, 81, 132,
146510 /* 360 */ 110, 16, 92, 93, 94, 95, 96, 97, 98, 99,
146511 /* 370 */ 100, 101, 102, 184, 185, 255, 98, 206, 207, 26,
146512 /* 380 */ 101, 102, 19, 105, 106, 107, 23, 198, 59, 116,
146513 /* 390 */ 219, 141, 142, 143, 24, 163, 187, 205, 274, 275,
146514 /* 400 */ 127, 128, 182, 232, 127, 128, 43, 44, 45, 46,
146515 /* 410 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
146516 /* 420 */ 57, 158, 77, 160, 79, 59, 26, 182, 165, 59,
146517 /* 430 */ 167, 199, 261, 102, 105, 106, 107, 174, 72, 108,
146518 /* 440 */ 109, 110, 111, 112, 113, 114, 59, 238, 239, 240,
146519 /* 450 */ 123, 120, 125, 126, 163, 92, 93, 94, 95, 96,
146520 /* 460 */ 97, 98, 99, 100, 101, 102, 163, 163, 163, 206,
146521 /* 470 */ 207, 105, 106, 107, 254, 19, 106, 90, 197, 23,
146522 /* 480 */ 127, 128, 219, 238, 239, 240, 22, 184, 185, 184,
146523 /* 490 */ 185, 22, 105, 106, 149, 232, 205, 110, 163, 43,
146524 /* 500 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
146525 /* 510 */ 54, 55, 56, 57, 98, 99, 100, 101, 102, 184,
146526 /* 520 */ 185, 163, 53, 59, 261, 220, 117, 118, 141, 142,
146527 /* 530 */ 143, 131, 174, 59, 229, 116, 117, 118, 163, 59,
146528 /* 540 */ 163, 163, 184, 185, 59, 242, 72, 22, 92, 93,
146529 /* 550 */ 94, 95, 96, 97, 98, 99, 100, 101, 102, 184,
146530 /* 560 */ 185, 24, 184, 185, 206, 207, 202, 203, 19, 105,
146531 /* 570 */ 106, 107, 23, 198, 22, 174, 198, 219, 220, 105,
146532 /* 580 */ 106, 107, 96, 97, 59, 105, 106, 107, 22, 174,
146533 /* 590 */ 59, 106, 43, 44, 45, 46, 47, 48, 49, 50,
146534 /* 600 */ 51, 52, 53, 54, 55, 56, 57, 206, 207, 12,
146535 /* 610 */ 108, 59, 132, 111, 112, 113, 46, 47, 48, 49,
146536 /* 620 */ 219, 206, 207, 121, 27, 59, 163, 141, 207, 143,
146537 /* 630 */ 105, 106, 107, 163, 219, 234, 105, 106, 107, 42,
146538 /* 640 */ 219, 92, 93, 94, 95, 96, 97, 98, 99, 100,
146539 /* 650 */ 101, 102, 76, 163, 184, 185, 163, 105, 106, 107,
146540 /* 660 */ 63, 19, 86, 163, 163, 23, 163, 130, 205, 21,
146541 /* 670 */ 73, 105, 106, 107, 184, 185, 163, 184, 185, 237,
146542 /* 680 */ 110, 180, 181, 180, 181, 43, 44, 45, 46, 47,
146543 /* 690 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
146544 /* 700 */ 174, 163, 163, 22, 23, 163, 163, 26, 22, 23,
146545 /* 710 */ 220, 29, 73, 220, 272, 33, 22, 163, 24, 19,
146546 /* 720 */ 174, 208, 259, 184, 185, 19, 184, 185, 80, 175,
146547 /* 730 */ 230, 174, 206, 207, 92, 93, 94, 95, 96, 97,
146548 /* 740 */ 98, 99, 100, 101, 102, 219, 46, 65, 247, 195,
146549 /* 750 */ 247, 197, 206, 207, 19, 116, 117, 118, 23, 220,
146550 /* 760 */ 112, 174, 220, 206, 207, 219, 22, 174, 24, 174,
146551 /* 770 */ 22, 23, 91, 264, 265, 168, 219, 91, 43, 44,
146552 /* 780 */ 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
146553 /* 790 */ 55, 56, 57, 206, 207, 12, 163, 149, 255, 206,
146554 /* 800 */ 207, 206, 207, 59, 104, 23, 219, 163, 26, 163,
146555 /* 810 */ 27, 105, 219, 163, 219, 163, 211, 184, 185, 163,
146556 /* 820 */ 120, 163, 146, 163, 148, 42, 221, 92, 93, 94,
146557 /* 830 */ 95, 96, 97, 98, 99, 100, 101, 102, 163, 91,
146558 /* 840 */ 184, 185, 184, 185, 184, 185, 63, 19, 163, 205,
146559 /* 850 */ 106, 23, 245, 163, 208, 248, 116, 117, 118, 184,
146560 /* 860 */ 185, 163, 163, 7, 8, 9, 163, 19, 26, 184,
146561 /* 870 */ 185, 43, 44, 45, 46, 47, 48, 49, 50, 51,
146562 /* 880 */ 52, 53, 54, 55, 56, 57, 163, 184, 185, 107,
146563 /* 890 */ 163, 43, 44, 45, 46, 47, 48, 49, 50, 51,
146564 /* 900 */ 52, 53, 54, 55, 56, 57, 208, 255, 177, 178,
146565 /* 910 */ 163, 184, 185, 163, 132, 163, 141, 163, 143, 22,
146566 /* 920 */ 92, 93, 94, 95, 96, 97, 98, 99, 100, 101,
146567 /* 930 */ 102, 184, 185, 163, 184, 185, 184, 185, 184, 185,
146568 /* 940 */ 92, 93, 94, 95, 96, 97, 98, 99, 100, 101,
146569 /* 950 */ 102, 163, 163, 163, 184, 185, 163, 115, 163, 163,
146570 /* 960 */ 163, 163, 15, 163, 163, 163, 163, 163, 23, 163,
146571 /* 970 */ 163, 26, 184, 185, 184, 185, 163, 184, 185, 184,
146572 /* 980 */ 185, 184, 185, 163, 184, 185, 184, 185, 184, 185,
146573 /* 990 */ 184, 185, 163, 96, 97, 147, 163, 184, 185, 163,
146574 /* 1000 */ 199, 163, 163, 205, 184, 185, 163, 60, 163, 141,
146575 /* 1010 */ 163, 143, 163, 184, 185, 19, 163, 184, 185, 230,
146576 /* 1020 */ 184, 185, 184, 185, 206, 207, 230, 184, 185, 184,
146577 /* 1030 */ 185, 184, 185, 184, 185, 19, 163, 219, 231, 43,
146578 /* 1040 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
146579 /* 1050 */ 54, 55, 56, 57, 163, 26, 163, 184, 185, 43,
146580 /* 1060 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
146581 /* 1070 */ 54, 55, 56, 57, 163, 184, 185, 184, 185, 163,
146582 /* 1080 */ 182, 163, 163, 163, 163, 163, 22, 163, 92, 93,
146583 /* 1090 */ 94, 95, 96, 97, 98, 99, 100, 101, 102, 163,
146584 /* 1100 */ 184, 185, 184, 185, 163, 184, 185, 163, 92, 93,
146585 /* 1110 */ 94, 95, 96, 97, 98, 99, 100, 101, 102, 163,
146586 /* 1120 */ 184, 185, 98, 59, 163, 184, 185, 205, 184, 185,
146587 /* 1130 */ 23, 206, 207, 26, 163, 26, 107, 153, 154, 237,
146588 /* 1140 */ 184, 185, 231, 147, 219, 184, 185, 249, 124, 127,
146589 /* 1150 */ 128, 231, 254, 129, 163, 231, 177, 178, 262, 263,
146590 /* 1160 */ 118, 132, 19, 19, 46, 223, 224, 31, 24, 23,
146591 /* 1170 */ 106, 124, 26, 22, 272, 39, 129, 23, 109, 110,
146592 /* 1180 */ 26, 163, 140, 19, 22, 234, 59, 43, 44, 45,
146593 /* 1190 */ 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
146594 /* 1200 */ 56, 57, 231, 7, 8, 193, 59, 43, 44, 45,
146595 /* 1210 */ 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
146596 /* 1220 */ 56, 57, 104, 61, 23, 23, 23, 26, 26, 26,
146597 /* 1230 */ 163, 23, 23, 106, 26, 26, 92, 93, 94, 95,
146598 /* 1240 */ 96, 97, 98, 99, 100, 101, 102, 138, 105, 23,
146599 /* 1250 */ 59, 23, 26, 106, 26, 163, 92, 93, 94, 95,
146600 /* 1260 */ 96, 97, 98, 99, 100, 101, 102, 110, 23, 23,
146601 /* 1270 */ 23, 26, 26, 26, 163, 163, 19, 120, 163, 163,
146602 /* 1280 */ 163, 130, 163, 163, 163, 163, 163, 163, 163, 193,
146603 /* 1290 */ 193, 163, 163, 163, 163, 225, 19, 106, 163, 222,
146604 /* 1300 */ 163, 44, 45, 46, 47, 48, 49, 50, 51, 52,
146605 /* 1310 */ 53, 54, 55, 56, 57, 163, 163, 203, 163, 163,
146606 /* 1320 */ 222, 163, 45, 46, 47, 48, 49, 50, 51, 52,
146607 /* 1330 */ 53, 54, 55, 56, 57, 163, 163, 163, 163, 163,
146608 /* 1340 */ 251, 250, 209, 19, 20, 182, 22, 161, 222, 92,
146609 /* 1350 */ 93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
146610 /* 1360 */ 36, 222, 222, 260, 226, 188, 256, 226, 187, 92,
146611 /* 1370 */ 93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
146612 /* 1380 */ 210, 213, 213, 59, 213, 196, 192, 187, 256, 244,
146613 /* 1390 */ 212, 187, 226, 19, 20, 71, 22, 210, 166, 60,
146614 /* 1400 */ 130, 170, 260, 170, 38, 81, 257, 257, 170, 104,
146615 /* 1410 */ 36, 22, 43, 201, 90, 236, 138, 235, 213, 18,
146616 /* 1420 */ 96, 97, 48, 204, 204, 204, 204, 103, 170, 105,
146617 /* 1430 */ 106, 107, 18, 59, 110, 169, 213, 213, 201, 170,
146618 /* 1440 */ 201, 169, 236, 213, 146, 71, 235, 62, 253, 252,
146619 /* 1450 */ 170, 127, 128, 169, 22, 170, 82, 189, 169, 104,
146620 /* 1460 */ 170, 87, 169, 189, 90, 141, 142, 143, 144, 145,
146621 /* 1470 */ 96, 97, 186, 186, 186, 64, 194, 103, 186, 105,
146622 /* 1480 */ 106, 107, 115, 189, 110, 188, 186, 186, 19, 20,
146623 /* 1490 */ 194, 22, 186, 189, 102, 246, 246, 189, 133, 228,
146624 /* 1500 */ 104, 228, 227, 227, 170, 36, 134, 228, 227, 19,
146625 /* 1510 */ 20, 228, 22, 84, 271, 141, 142, 143, 144, 145,
146626 /* 1520 */ 0, 1, 2, 216, 22, 5, 36, 137, 59, 227,
146627 /* 1530 */ 10, 11, 12, 13, 14, 217, 269, 17, 216, 22,
146628 /* 1540 */ 71, 170, 243, 146, 241, 217, 136, 215, 135, 59,
146629 /* 1550 */ 30, 82, 32, 25, 214, 213, 87, 173, 26, 90,
146630 /* 1560 */ 40, 71, 13, 172, 164, 96, 97, 164, 6, 162,
146631 /* 1570 */ 162, 162, 103, 263, 105, 106, 107, 266, 266, 110,
146632 /* 1580 */ 90, 176, 176, 190, 182, 190, 96, 97, 98, 4,
146633 /* 1590 */ 70, 176, 3, 103, 182, 105, 106, 107, 78, 182,
146634 /* 1600 */ 110, 81, 182, 182, 182, 182, 182, 151, 88, 22,
146635 /* 1610 */ 141, 142, 143, 144, 145, 15, 89, 16, 23, 23,
146636 /* 1620 */ 128, 19, 20, 139, 22, 119, 131, 24, 20, 133,
146637 /* 1630 */ 16, 141, 142, 143, 144, 145, 1, 140, 36, 131,
146638 /* 1640 */ 119, 61, 122, 37, 139, 53, 53, 127, 128, 119,
146639 /* 1650 */ 53, 53, 105, 34, 130, 1, 5, 104, 22, 149,
146640 /* 1660 */ 26, 59, 68, 75, 41, 130, 24, 68, 104, 20,
146641 /* 1670 */ 150, 19, 120, 71, 114, 22, 67, 22, 22, 67,
146642 /* 1680 */ 23, 22, 22, 67, 82, 37, 28, 23, 138, 87,
146643 /* 1690 */ 22, 153, 90, 23, 23, 26, 23, 22, 96, 97,
146644 /* 1700 */ 24, 23, 22, 24, 130, 103, 23, 105, 106, 107,
146645 /* 1710 */ 1, 2, 110, 23, 5, 105, 34, 22, 132, 10,
146646 /* 1720 */ 11, 12, 13, 14, 26, 34, 17, 34, 85, 83,
146647 /* 1730 */ 44, 19, 20, 23, 22, 24, 75, 34, 23, 30,
146648 /* 1740 */ 26, 32, 26, 141, 142, 143, 144, 145, 36, 40,
146649 /* 1750 */ 23, 23, 23, 23, 11, 23, 22, 26, 22, 22,
146650 /* 1760 */ 22, 19, 20, 23, 22, 26, 15, 23, 22, 124,
146651 /* 1770 */ 130, 59, 23, 1, 130, 277, 277, 130, 36, 70,
146652 /* 1780 */ 130, 277, 277, 71, 277, 277, 277, 78, 277, 277,
146653 /* 1790 */ 81, 277, 277, 277, 277, 277, 277, 88, 277, 277,
146654 /* 1800 */ 277, 59, 90, 277, 277, 277, 277, 277, 96, 97,
146655 /* 1810 */ 277, 277, 277, 71, 277, 103, 277, 105, 106, 107,
146656 /* 1820 */ 277, 277, 110, 277, 277, 277, 277, 277, 277, 277,
146657 /* 1830 */ 277, 122, 90, 277, 277, 277, 127, 128, 96, 97,
146658 /* 1840 */ 277, 277, 277, 277, 277, 103, 277, 105, 106, 107,
146659 /* 1850 */ 277, 277, 110, 141, 142, 143, 144, 145, 277, 150,
146660 /* 1860 */ 277, 277, 277, 5, 277, 277, 277, 277, 10, 11,
146661 /* 1870 */ 12, 13, 14, 277, 277, 17, 277, 277, 277, 277,
146662 /* 1880 */ 277, 277, 277, 141, 142, 143, 144, 145, 30, 277,
146663 /* 1890 */ 32, 277, 277, 277, 277, 277, 277, 277, 40, 277,
146664 /* 1900 */ 277, 277, 277, 277, 277, 277, 277, 277, 277, 277,
146665 /* 1910 */ 277, 277, 277, 277, 277, 277, 277, 277, 277, 277,
146666 /* 1920 */ 277, 277, 277, 277, 277, 277, 277, 277, 70, 277,
146667 /* 1930 */ 277, 277, 277, 277, 277, 277, 78, 277, 277, 81,
146668 /* 1940 */ 277, 277, 277, 277, 277, 277, 88, 277, 277, 277,
146669 /* 1950 */ 277, 277, 277, 277, 277, 277, 277, 277, 277, 277,
146670 /* 1960 */ 277, 277, 277, 277, 277, 277, 277, 277, 277, 277,
146671 /* 1970 */ 277, 277, 277, 277, 277, 277, 277, 277, 277, 277,
146672 /* 1980 */ 122, 277, 277, 277, 277, 127, 128, 277, 277, 277,
146673 /* 1990 */ 277, 277, 277, 277, 277, 277, 277, 277, 277, 277,
146674 /* 2000 */ 277, 277, 277, 277, 277, 277, 277, 277, 150, 277,
146675 /* 2010 */ 277, 277, 277, 277, 277, 277, 277, 277, 277,
146676};
146677#define YY_SHIFT_COUNT (520)
146678#define YY_SHIFT_MIN (0)
146679#define YY_SHIFT_MAX (1858)
146680static const unsigned short int yy_shift_ofst[] = {
146681 /* 0 */ 1709, 1520, 1858, 1324, 1324, 277, 1374, 1469, 1602, 1712,
146682 /* 10 */ 1712, 1712, 273, 0, 0, 113, 1016, 1712, 1712, 1712,
146683 /* 20 */ 1712, 1712, 1712, 1712, 1712, 1712, 1712, 11, 11, 236,
146684 /* 30 */ 184, 277, 277, 277, 277, 277, 277, 93, 177, 270,
146685 /* 40 */ 363, 456, 549, 642, 735, 828, 848, 996, 1144, 1016,
146686 /* 50 */ 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016, 1016,
146687 /* 60 */ 1016, 1016, 1016, 1016, 1016, 1016, 1164, 1016, 1257, 1277,
146688 /* 70 */ 1277, 1490, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
146689 /* 80 */ 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
146690 /* 90 */ 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712, 1712,
146691 /* 100 */ 1712, 1712, 1712, 1742, 1712, 1712, 1712, 1712, 1712, 1712,
146692 /* 110 */ 1712, 1712, 1712, 1712, 1712, 1712, 1712, 143, 162, 162,
146693 /* 120 */ 162, 162, 162, 204, 151, 416, 531, 648, 700, 531,
146694 /* 130 */ 486, 486, 531, 353, 353, 353, 353, 409, 279, 53,
146695 /* 140 */ 2009, 2009, 331, 331, 331, 329, 366, 329, 329, 597,
146696 /* 150 */ 597, 464, 474, 262, 681, 531, 531, 531, 531, 531,
146697 /* 160 */ 531, 531, 531, 531, 531, 531, 531, 531, 531, 531,
146698 /* 170 */ 531, 531, 531, 531, 531, 531, 531, 173, 485, 984,
146699 /* 180 */ 984, 576, 485, 19, 1022, 2009, 2009, 2009, 387, 250,
146700 /* 190 */ 250, 525, 502, 278, 552, 227, 480, 566, 531, 531,
146701 /* 200 */ 531, 531, 531, 531, 531, 531, 531, 531, 639, 531,
146702 /* 210 */ 531, 531, 531, 531, 531, 531, 531, 531, 531, 531,
146703 /* 220 */ 531, 2, 2, 2, 531, 531, 531, 531, 782, 531,
146704 /* 230 */ 531, 531, 744, 531, 531, 783, 531, 531, 531, 531,
146705 /* 240 */ 531, 531, 531, 531, 419, 682, 327, 370, 370, 370,
146706 /* 250 */ 370, 1029, 327, 327, 1024, 897, 856, 947, 1109, 706,
146707 /* 260 */ 706, 1143, 1109, 1109, 1143, 842, 945, 1118, 1136, 1136,
146708 /* 270 */ 1136, 706, 676, 400, 1047, 694, 1339, 1270, 1270, 1366,
146709 /* 280 */ 1366, 1270, 1305, 1389, 1369, 1278, 1401, 1401, 1401, 1401,
146710 /* 290 */ 1270, 1414, 1278, 1278, 1305, 1389, 1369, 1369, 1278, 1270,
146711 /* 300 */ 1414, 1298, 1385, 1270, 1414, 1432, 1270, 1414, 1270, 1414,
146712 /* 310 */ 1432, 1355, 1355, 1355, 1411, 1432, 1355, 1367, 1355, 1411,
146713 /* 320 */ 1355, 1355, 1432, 1392, 1392, 1432, 1365, 1396, 1365, 1396,
146714 /* 330 */ 1365, 1396, 1365, 1396, 1270, 1372, 1429, 1502, 1390, 1372,
146715 /* 340 */ 1517, 1270, 1397, 1390, 1410, 1413, 1278, 1528, 1532, 1549,
146716 /* 350 */ 1549, 1562, 1562, 1562, 2009, 2009, 2009, 2009, 2009, 2009,
146717 /* 360 */ 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009,
146718 /* 370 */ 570, 345, 686, 748, 50, 740, 1064, 1107, 469, 537,
146719 /* 380 */ 1042, 1146, 1162, 1154, 1201, 1202, 1203, 1208, 1209, 1127,
146720 /* 390 */ 1069, 1196, 1157, 1147, 1226, 1228, 1245, 775, 868, 1246,
146721 /* 400 */ 1247, 1191, 1151, 1585, 1589, 1587, 1456, 1600, 1527, 1601,
146722 /* 410 */ 1595, 1596, 1492, 1484, 1506, 1603, 1495, 1608, 1496, 1614,
146723 /* 420 */ 1635, 1508, 1497, 1521, 1580, 1606, 1505, 1592, 1593, 1597,
146724 /* 430 */ 1598, 1530, 1547, 1619, 1524, 1654, 1651, 1636, 1553, 1510,
146725 /* 440 */ 1594, 1634, 1599, 1588, 1623, 1535, 1564, 1642, 1649, 1652,
146726 /* 450 */ 1552, 1560, 1653, 1609, 1655, 1656, 1657, 1659, 1612, 1658,
146727 /* 460 */ 1660, 1616, 1648, 1664, 1550, 1668, 1538, 1670, 1671, 1669,
146728 /* 470 */ 1673, 1675, 1676, 1678, 1680, 1679, 1574, 1683, 1690, 1610,
146729 /* 480 */ 1682, 1695, 1586, 1698, 1691, 1698, 1693, 1643, 1661, 1646,
146730 /* 490 */ 1686, 1710, 1711, 1714, 1716, 1703, 1715, 1698, 1727, 1728,
146731 /* 500 */ 1729, 1730, 1731, 1732, 1734, 1743, 1736, 1737, 1740, 1744,
146732 /* 510 */ 1738, 1746, 1739, 1645, 1640, 1644, 1647, 1650, 1749, 1751,
146733 /* 520 */ 1772,
146734};
146735#define YY_REDUCE_COUNT (369)
146736#define YY_REDUCE_MIN (-237)
146737#define YY_REDUCE_MAX (1424)
146738static const short yy_reduce_ofst[] = {
146739 /* 0 */ -147, 171, 263, -96, 358, -144, -149, -102, 124, -156,
146740 /* 10 */ -98, 305, 401, -57, 209, -237, 245, -94, -79, 189,
146741 /* 20 */ 375, 490, 493, 378, 303, 539, 542, 501, 503, 554,
146742 /* 30 */ 415, 526, 546, 557, 587, 593, 595, -234, -234, -234,
146743 /* 40 */ -234, -234, -234, -234, -234, -234, -234, -234, -234, -234,
146744 /* 50 */ -234, -234, -234, -234, -234, -234, -234, -234, -234, -234,
146745 /* 60 */ -234, -234, -234, -234, -234, -234, -234, -234, -234, -234,
146746 /* 70 */ -234, -50, 335, 470, 633, 656, 658, 660, 675, 685,
146747 /* 80 */ 703, 727, 747, 750, 752, 754, 770, 788, 790, 793,
146748 /* 90 */ 795, 797, 800, 802, 804, 806, 813, 820, 829, 833,
146749 /* 100 */ 836, 838, 843, 845, 847, 849, 873, 891, 893, 916,
146750 /* 110 */ 918, 921, 936, 941, 944, 956, 961, -234, -234, -234,
146751 /* 120 */ -234, -234, -234, -234, -234, -234, 463, 607, -176, 14,
146752 /* 130 */ -139, 87, -137, 818, 925, 818, 925, 898, -234, -234,
146753 /* 140 */ -234, -234, -166, -166, -166, -130, -131, -82, -54, -180,
146754 /* 150 */ 364, 41, 513, 509, 509, 117, 500, 789, 796, 646,
146755 /* 160 */ 192, 291, 644, 798, 120, 807, 543, 911, 920, 652,
146756 /* 170 */ 924, 922, 232, 698, 801, 971, 39, 220, 731, 442,
146757 /* 180 */ 902, -199, 979, -43, 421, 896, 942, 605, -184, -126,
146758 /* 190 */ 155, 172, 281, 304, 377, 538, 650, 690, 699, 723,
146759 /* 200 */ 803, 839, 853, 919, 991, 1018, 1067, 1092, 951, 1111,
146760 /* 210 */ 1112, 1115, 1116, 1117, 1119, 1120, 1121, 1122, 1123, 1124,
146761 /* 220 */ 1125, 1012, 1096, 1097, 1128, 1129, 1130, 1131, 1070, 1135,
146762 /* 230 */ 1137, 1152, 1077, 1153, 1155, 1114, 1156, 304, 1158, 1172,
146763 /* 240 */ 1173, 1174, 1175, 1176, 1089, 1091, 1133, 1098, 1126, 1139,
146764 /* 250 */ 1140, 1070, 1133, 1133, 1170, 1163, 1186, 1103, 1168, 1138,
146765 /* 260 */ 1141, 1110, 1169, 1171, 1132, 1177, 1189, 1194, 1181, 1200,
146766 /* 270 */ 1204, 1166, 1145, 1178, 1187, 1232, 1142, 1231, 1233, 1149,
146767 /* 280 */ 1150, 1238, 1179, 1182, 1212, 1205, 1219, 1220, 1221, 1222,
146768 /* 290 */ 1258, 1266, 1223, 1224, 1206, 1211, 1237, 1239, 1230, 1269,
146769 /* 300 */ 1272, 1195, 1197, 1280, 1284, 1268, 1285, 1289, 1290, 1293,
146770 /* 310 */ 1274, 1286, 1287, 1288, 1282, 1294, 1292, 1297, 1300, 1296,
146771 /* 320 */ 1301, 1306, 1304, 1249, 1250, 1308, 1271, 1275, 1273, 1276,
146772 /* 330 */ 1279, 1281, 1283, 1302, 1334, 1307, 1243, 1267, 1318, 1322,
146773 /* 340 */ 1303, 1371, 1299, 1328, 1332, 1340, 1342, 1384, 1391, 1400,
146774 /* 350 */ 1403, 1407, 1408, 1409, 1311, 1312, 1310, 1405, 1402, 1412,
146775 /* 360 */ 1417, 1420, 1406, 1393, 1395, 1421, 1422, 1423, 1424, 1415,
146776};
146777static const YYACTIONTYPE yy_default[] = {
146778 /* 0 */ 1492, 1492, 1492, 1340, 1123, 1229, 1123, 1123, 1123, 1340,
146779 /* 10 */ 1340, 1340, 1123, 1259, 1259, 1391, 1154, 1123, 1123, 1123,
146780 /* 20 */ 1123, 1123, 1123, 1123, 1339, 1123, 1123, 1123, 1123, 1123,
146781 /* 30 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1265, 1123,
146782 /* 40 */ 1123, 1123, 1123, 1123, 1341, 1342, 1123, 1123, 1123, 1390,
146783 /* 50 */ 1392, 1275, 1274, 1273, 1272, 1373, 1246, 1270, 1263, 1267,
146784 /* 60 */ 1335, 1336, 1334, 1338, 1342, 1341, 1123, 1266, 1306, 1320,
146785 /* 70 */ 1305, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146786 /* 80 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146787 /* 90 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146788 /* 100 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146789 /* 110 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1314, 1319, 1325,
146790 /* 120 */ 1318, 1315, 1308, 1307, 1309, 1310, 1123, 1144, 1193, 1123,
146791 /* 130 */ 1123, 1123, 1123, 1409, 1408, 1123, 1123, 1154, 1311, 1312,
146792 /* 140 */ 1322, 1321, 1398, 1448, 1447, 1123, 1123, 1123, 1123, 1123,
146793 /* 150 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146794 /* 160 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146795 /* 170 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1154, 1150, 1300,
146796 /* 180 */ 1299, 1418, 1150, 1253, 1123, 1404, 1229, 1220, 1123, 1123,
146797 /* 190 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146798 /* 200 */ 1123, 1395, 1393, 1123, 1355, 1123, 1123, 1123, 1123, 1123,
146799 /* 210 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146800 /* 220 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146801 /* 230 */ 1123, 1123, 1225, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146802 /* 240 */ 1123, 1123, 1123, 1442, 1123, 1368, 1207, 1225, 1225, 1225,
146803 /* 250 */ 1225, 1227, 1208, 1206, 1219, 1154, 1130, 1484, 1269, 1248,
146804 /* 260 */ 1248, 1481, 1269, 1269, 1481, 1168, 1462, 1165, 1259, 1259,
146805 /* 270 */ 1259, 1248, 1337, 1226, 1219, 1123, 1484, 1234, 1234, 1483,
146806 /* 280 */ 1483, 1234, 1278, 1284, 1196, 1269, 1202, 1202, 1202, 1202,
146807 /* 290 */ 1234, 1141, 1269, 1269, 1278, 1284, 1196, 1196, 1269, 1234,
146808 /* 300 */ 1141, 1372, 1478, 1234, 1141, 1348, 1234, 1141, 1234, 1141,
146809 /* 310 */ 1348, 1194, 1194, 1194, 1183, 1348, 1194, 1168, 1194, 1183,
146810 /* 320 */ 1194, 1194, 1348, 1352, 1352, 1348, 1252, 1247, 1252, 1247,
146811 /* 330 */ 1252, 1247, 1252, 1247, 1234, 1253, 1417, 1123, 1264, 1253,
146812 /* 340 */ 1343, 1234, 1123, 1264, 1262, 1260, 1269, 1147, 1186, 1445,
146813 /* 350 */ 1445, 1441, 1441, 1441, 1489, 1489, 1404, 1457, 1154, 1154,
146814 /* 360 */ 1154, 1154, 1457, 1170, 1170, 1154, 1154, 1154, 1154, 1457,
146815 /* 370 */ 1123, 1123, 1123, 1123, 1123, 1123, 1452, 1123, 1357, 1238,
146816 /* 380 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146817 /* 390 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146818 /* 400 */ 1123, 1123, 1289, 1123, 1126, 1401, 1123, 1123, 1399, 1123,
146819 /* 410 */ 1123, 1123, 1123, 1123, 1123, 1239, 1123, 1123, 1123, 1123,
146820 /* 420 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146821 /* 430 */ 1123, 1123, 1123, 1123, 1480, 1123, 1123, 1123, 1123, 1123,
146822 /* 440 */ 1123, 1371, 1370, 1123, 1123, 1236, 1123, 1123, 1123, 1123,
146823 /* 450 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146824 /* 460 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146825 /* 470 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146826 /* 480 */ 1123, 1123, 1123, 1261, 1123, 1416, 1123, 1123, 1123, 1123,
146827 /* 490 */ 1123, 1123, 1123, 1430, 1254, 1123, 1123, 1471, 1123, 1123,
146828 /* 500 */ 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123, 1123,
146829 /* 510 */ 1123, 1123, 1466, 1210, 1291, 1123, 1290, 1294, 1123, 1135,
146830 /* 520 */ 1123,
146831};
146832/********** End of lemon-generated parsing tables *****************************/
146833
146834/* The next table maps tokens (terminal symbols) into fallback tokens.
146835** If a construct like the following:
146836**
146837** %fallback ID X Y Z.
146838**
146839** appears in the grammar, then ID becomes a fallback token for X, Y,
146840** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
146841** but it does not parse, the type of the token is changed to ID and
146842** the parse is retried before an error is thrown.
146843**
146844** This feature can be used, for example, to cause some keywords in a language
146845** to revert to identifiers if they keyword does not apply in the context where
146846** it appears.
146847*/
146848#ifdef YYFALLBACK
146849static const YYCODETYPE yyFallback[] = {
146850 0, /* $ => nothing */
146851 0, /* SEMI => nothing */
146852 59, /* EXPLAIN => ID */
146853 59, /* QUERY => ID */
146854 59, /* PLAN => ID */
146855 59, /* BEGIN => ID */
146856 0, /* TRANSACTION => nothing */
146857 59, /* DEFERRED => ID */
146858 59, /* IMMEDIATE => ID */
146859 59, /* EXCLUSIVE => ID */
146860 0, /* COMMIT => nothing */
146861 59, /* END => ID */
146862 59, /* ROLLBACK => ID */
146863 59, /* SAVEPOINT => ID */
146864 59, /* RELEASE => ID */
146865 0, /* TO => nothing */
146866 0, /* TABLE => nothing */
146867 0, /* CREATE => nothing */
146868 59, /* IF => ID */
146869 0, /* NOT => nothing */
146870 0, /* EXISTS => nothing */
146871 59, /* TEMP => ID */
146872 0, /* LP => nothing */
146873 0, /* RP => nothing */
146874 0, /* AS => nothing */
146875 59, /* WITHOUT => ID */
146876 0, /* COMMA => nothing */
146877 59, /* ABORT => ID */
146878 59, /* ACTION => ID */
146879 59, /* AFTER => ID */
146880 59, /* ANALYZE => ID */
146881 59, /* ASC => ID */
146882 59, /* ATTACH => ID */
146883 59, /* BEFORE => ID */
146884 59, /* BY => ID */
146885 59, /* CASCADE => ID */
146886 59, /* CAST => ID */
146887 59, /* CONFLICT => ID */
146888 59, /* DATABASE => ID */
146889 59, /* DESC => ID */
146890 59, /* DETACH => ID */
146891 59, /* EACH => ID */
146892 59, /* FAIL => ID */
146893 0, /* OR => nothing */
146894 0, /* AND => nothing */
146895 0, /* IS => nothing */
146896 59, /* MATCH => ID */
146897 59, /* LIKE_KW => ID */
146898 0, /* BETWEEN => nothing */
146899 0, /* IN => nothing */
146900 0, /* ISNULL => nothing */
146901 0, /* NOTNULL => nothing */
146902 0, /* NE => nothing */
146903 0, /* EQ => nothing */
146904 0, /* GT => nothing */
146905 0, /* LE => nothing */
146906 0, /* LT => nothing */
146907 0, /* GE => nothing */
146908 0, /* ESCAPE => nothing */
146909 0, /* ID => nothing */
146910 59, /* COLUMNKW => ID */
146911 59, /* DO => ID */
146912 59, /* FOR => ID */
146913 59, /* IGNORE => ID */
146914 59, /* INITIALLY => ID */
146915 59, /* INSTEAD => ID */
146916 59, /* NO => ID */
146917 59, /* KEY => ID */
146918 59, /* OF => ID */
146919 59, /* OFFSET => ID */
146920 59, /* PRAGMA => ID */
146921 59, /* RAISE => ID */
146922 59, /* RECURSIVE => ID */
146923 59, /* REPLACE => ID */
146924 59, /* RESTRICT => ID */
146925 59, /* ROW => ID */
146926 59, /* ROWS => ID */
146927 59, /* TRIGGER => ID */
146928 59, /* VACUUM => ID */
146929 59, /* VIEW => ID */
146930 59, /* VIRTUAL => ID */
146931 59, /* WITH => ID */
146932 59, /* CURRENT => ID */
146933 59, /* FOLLOWING => ID */
146934 59, /* PARTITION => ID */
146935 59, /* PRECEDING => ID */
146936 59, /* RANGE => ID */
146937 59, /* UNBOUNDED => ID */
146938 59, /* REINDEX => ID */
146939 59, /* RENAME => ID */
146940 59, /* CTIME_KW => ID */
146941};
146942#endif /* YYFALLBACK */
146943
146944/* The following structure represents a single element of the
146945** parser's stack. Information stored includes:
146946**
146947** + The state number for the parser at this level of the stack.
146948**
146949** + The value of the token stored at this level of the stack.
146950** (In other words, the "major" token.)
146951**
146952** + The semantic value stored at this level of the stack. This is
146953** the information used by the action routines in the grammar.
146954** It is sometimes called the "minor" token.
146955**
146956** After the "shift" half of a SHIFTREDUCE action, the stateno field
146957** actually contains the reduce action for the second half of the
146958** SHIFTREDUCE.
146959*/
146960struct yyStackEntry {
146961 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
146962 YYCODETYPE major; /* The major token value. This is the code
146963 ** number for the token at this stack level */
146964 YYMINORTYPE minor; /* The user-supplied minor token value. This
146965 ** is the value of the token */
146966};
146967typedef struct yyStackEntry yyStackEntry;
146968
146969/* The state of the parser is completely contained in an instance of
146970** the following structure */
146971struct yyParser {
146972 yyStackEntry *yytos; /* Pointer to top element of the stack */
146973#ifdef YYTRACKMAXSTACKDEPTH
146974 int yyhwm; /* High-water mark of the stack */
146975#endif
146976#ifndef YYNOERRORRECOVERY
146977 int yyerrcnt; /* Shifts left before out of the error */
146978#endif
146979 sqlite3ParserARG_SDECL /* A place to hold %extra_argument */
146980 sqlite3ParserCTX_SDECL /* A place to hold %extra_context */
146981#if YYSTACKDEPTH<=0
146982 int yystksz; /* Current side of the stack */
146983 yyStackEntry *yystack; /* The parser's stack */
146984 yyStackEntry yystk0; /* First stack entry */
146985#else
146986 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
146987 yyStackEntry *yystackEnd; /* Last entry in the stack */
146988#endif
146989};
146990typedef struct yyParser yyParser;
146991
146992#ifndef NDEBUG
146993/* #include <stdio.h> */
146994static FILE *yyTraceFILE = 0;
146995static char *yyTracePrompt = 0;
146996#endif /* NDEBUG */
146997
146998#ifndef NDEBUG
146999/*
147000** Turn parser tracing on by giving a stream to which to write the trace
147001** and a prompt to preface each trace message. Tracing is turned off
147002** by making either argument NULL
147003**
147004** Inputs:
147005** <ul>
147006** <li> A FILE* to which trace output should be written.
147007** If NULL, then tracing is turned off.
147008** <li> A prefix string written at the beginning of every
147009** line of trace output. If NULL, then tracing is
147010** turned off.
147011** </ul>
147012**
147013** Outputs:
147014** None.
147015*/
147016SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
147017 yyTraceFILE = TraceFILE;
147018 yyTracePrompt = zTracePrompt;
147019 if( yyTraceFILE==0 ) yyTracePrompt = 0;
147020 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
147021}
147022#endif /* NDEBUG */
147023
147024#if defined(YYCOVERAGE) || !defined(NDEBUG)
147025/* For tracing shifts, the names of all terminals and nonterminals
147026** are required. The following table supplies these names */
147027static const char *const yyTokenName[] = {
147028 /* 0 */ "$",
147029 /* 1 */ "SEMI",
147030 /* 2 */ "EXPLAIN",
147031 /* 3 */ "QUERY",
147032 /* 4 */ "PLAN",
147033 /* 5 */ "BEGIN",
147034 /* 6 */ "TRANSACTION",
147035 /* 7 */ "DEFERRED",
147036 /* 8 */ "IMMEDIATE",
147037 /* 9 */ "EXCLUSIVE",
147038 /* 10 */ "COMMIT",
147039 /* 11 */ "END",
147040 /* 12 */ "ROLLBACK",
147041 /* 13 */ "SAVEPOINT",
147042 /* 14 */ "RELEASE",
147043 /* 15 */ "TO",
147044 /* 16 */ "TABLE",
147045 /* 17 */ "CREATE",
147046 /* 18 */ "IF",
147047 /* 19 */ "NOT",
147048 /* 20 */ "EXISTS",
147049 /* 21 */ "TEMP",
147050 /* 22 */ "LP",
147051 /* 23 */ "RP",
147052 /* 24 */ "AS",
147053 /* 25 */ "WITHOUT",
147054 /* 26 */ "COMMA",
147055 /* 27 */ "ABORT",
147056 /* 28 */ "ACTION",
147057 /* 29 */ "AFTER",
147058 /* 30 */ "ANALYZE",
147059 /* 31 */ "ASC",
147060 /* 32 */ "ATTACH",
147061 /* 33 */ "BEFORE",
147062 /* 34 */ "BY",
147063 /* 35 */ "CASCADE",
147064 /* 36 */ "CAST",
147065 /* 37 */ "CONFLICT",
147066 /* 38 */ "DATABASE",
147067 /* 39 */ "DESC",
147068 /* 40 */ "DETACH",
147069 /* 41 */ "EACH",
147070 /* 42 */ "FAIL",
147071 /* 43 */ "OR",
147072 /* 44 */ "AND",
147073 /* 45 */ "IS",
147074 /* 46 */ "MATCH",
147075 /* 47 */ "LIKE_KW",
147076 /* 48 */ "BETWEEN",
147077 /* 49 */ "IN",
147078 /* 50 */ "ISNULL",
147079 /* 51 */ "NOTNULL",
147080 /* 52 */ "NE",
147081 /* 53 */ "EQ",
147082 /* 54 */ "GT",
147083 /* 55 */ "LE",
147084 /* 56 */ "LT",
147085 /* 57 */ "GE",
147086 /* 58 */ "ESCAPE",
147087 /* 59 */ "ID",
147088 /* 60 */ "COLUMNKW",
147089 /* 61 */ "DO",
147090 /* 62 */ "FOR",
147091 /* 63 */ "IGNORE",
147092 /* 64 */ "INITIALLY",
147093 /* 65 */ "INSTEAD",
147094 /* 66 */ "NO",
147095 /* 67 */ "KEY",
147096 /* 68 */ "OF",
147097 /* 69 */ "OFFSET",
147098 /* 70 */ "PRAGMA",
147099 /* 71 */ "RAISE",
147100 /* 72 */ "RECURSIVE",
147101 /* 73 */ "REPLACE",
147102 /* 74 */ "RESTRICT",
147103 /* 75 */ "ROW",
147104 /* 76 */ "ROWS",
147105 /* 77 */ "TRIGGER",
147106 /* 78 */ "VACUUM",
147107 /* 79 */ "VIEW",
147108 /* 80 */ "VIRTUAL",
147109 /* 81 */ "WITH",
147110 /* 82 */ "CURRENT",
147111 /* 83 */ "FOLLOWING",
147112 /* 84 */ "PARTITION",
147113 /* 85 */ "PRECEDING",
147114 /* 86 */ "RANGE",
147115 /* 87 */ "UNBOUNDED",
147116 /* 88 */ "REINDEX",
147117 /* 89 */ "RENAME",
147118 /* 90 */ "CTIME_KW",
147119 /* 91 */ "ANY",
147120 /* 92 */ "BITAND",
147121 /* 93 */ "BITOR",
147122 /* 94 */ "LSHIFT",
147123 /* 95 */ "RSHIFT",
147124 /* 96 */ "PLUS",
147125 /* 97 */ "MINUS",
147126 /* 98 */ "STAR",
147127 /* 99 */ "SLASH",
147128 /* 100 */ "REM",
147129 /* 101 */ "CONCAT",
147130 /* 102 */ "COLLATE",
147131 /* 103 */ "BITNOT",
147132 /* 104 */ "ON",
147133 /* 105 */ "INDEXED",
147134 /* 106 */ "STRING",
147135 /* 107 */ "JOIN_KW",
147136 /* 108 */ "CONSTRAINT",
147137 /* 109 */ "DEFAULT",
147138 /* 110 */ "NULL",
147139 /* 111 */ "PRIMARY",
147140 /* 112 */ "UNIQUE",
147141 /* 113 */ "CHECK",
147142 /* 114 */ "REFERENCES",
147143 /* 115 */ "AUTOINCR",
147144 /* 116 */ "INSERT",
147145 /* 117 */ "DELETE",
147146 /* 118 */ "UPDATE",
147147 /* 119 */ "SET",
147148 /* 120 */ "DEFERRABLE",
147149 /* 121 */ "FOREIGN",
147150 /* 122 */ "DROP",
147151 /* 123 */ "UNION",
147152 /* 124 */ "ALL",
147153 /* 125 */ "EXCEPT",
147154 /* 126 */ "INTERSECT",
147155 /* 127 */ "SELECT",
147156 /* 128 */ "VALUES",
147157 /* 129 */ "DISTINCT",
147158 /* 130 */ "DOT",
147159 /* 131 */ "FROM",
147160 /* 132 */ "JOIN",
147161 /* 133 */ "USING",
147162 /* 134 */ "ORDER",
147163 /* 135 */ "GROUP",
147164 /* 136 */ "HAVING",
147165 /* 137 */ "LIMIT",
147166 /* 138 */ "WHERE",
147167 /* 139 */ "INTO",
147168 /* 140 */ "NOTHING",
147169 /* 141 */ "FLOAT",
147170 /* 142 */ "BLOB",
147171 /* 143 */ "INTEGER",
147172 /* 144 */ "VARIABLE",
147173 /* 145 */ "CASE",
147174 /* 146 */ "WHEN",
147175 /* 147 */ "THEN",
147176 /* 148 */ "ELSE",
147177 /* 149 */ "INDEX",
147178 /* 150 */ "ALTER",
147179 /* 151 */ "ADD",
147180 /* 152 */ "WINDOW",
147181 /* 153 */ "OVER",
147182 /* 154 */ "FILTER",
147183 /* 155 */ "input",
147184 /* 156 */ "cmdlist",
147185 /* 157 */ "ecmd",
147186 /* 158 */ "cmdx",
147187 /* 159 */ "explain",
147188 /* 160 */ "cmd",
147189 /* 161 */ "transtype",
147190 /* 162 */ "trans_opt",
147191 /* 163 */ "nm",
147192 /* 164 */ "savepoint_opt",
147193 /* 165 */ "create_table",
147194 /* 166 */ "create_table_args",
147195 /* 167 */ "createkw",
147196 /* 168 */ "temp",
147197 /* 169 */ "ifnotexists",
147198 /* 170 */ "dbnm",
147199 /* 171 */ "columnlist",
147200 /* 172 */ "conslist_opt",
147201 /* 173 */ "table_options",
147202 /* 174 */ "select",
147203 /* 175 */ "columnname",
147204 /* 176 */ "carglist",
147205 /* 177 */ "typetoken",
147206 /* 178 */ "typename",
147207 /* 179 */ "signed",
147208 /* 180 */ "plus_num",
147209 /* 181 */ "minus_num",
147210 /* 182 */ "scanpt",
147211 /* 183 */ "ccons",
147212 /* 184 */ "term",
147213 /* 185 */ "expr",
147214 /* 186 */ "onconf",
147215 /* 187 */ "sortorder",
147216 /* 188 */ "autoinc",
147217 /* 189 */ "eidlist_opt",
147218 /* 190 */ "refargs",
147219 /* 191 */ "defer_subclause",
147220 /* 192 */ "refarg",
147221 /* 193 */ "refact",
147222 /* 194 */ "init_deferred_pred_opt",
147223 /* 195 */ "conslist",
147224 /* 196 */ "tconscomma",
147225 /* 197 */ "tcons",
147226 /* 198 */ "sortlist",
147227 /* 199 */ "eidlist",
147228 /* 200 */ "defer_subclause_opt",
147229 /* 201 */ "orconf",
147230 /* 202 */ "resolvetype",
147231 /* 203 */ "raisetype",
147232 /* 204 */ "ifexists",
147233 /* 205 */ "fullname",
147234 /* 206 */ "selectnowith",
147235 /* 207 */ "oneselect",
147236 /* 208 */ "wqlist",
147237 /* 209 */ "multiselect_op",
147238 /* 210 */ "distinct",
147239 /* 211 */ "selcollist",
147240 /* 212 */ "from",
147241 /* 213 */ "where_opt",
147242 /* 214 */ "groupby_opt",
147243 /* 215 */ "having_opt",
147244 /* 216 */ "orderby_opt",
147245 /* 217 */ "limit_opt",
147246 /* 218 */ "window_clause",
147247 /* 219 */ "values",
147248 /* 220 */ "nexprlist",
147249 /* 221 */ "sclp",
147250 /* 222 */ "as",
147251 /* 223 */ "seltablist",
147252 /* 224 */ "stl_prefix",
147253 /* 225 */ "joinop",
147254 /* 226 */ "indexed_opt",
147255 /* 227 */ "on_opt",
147256 /* 228 */ "using_opt",
147257 /* 229 */ "exprlist",
147258 /* 230 */ "xfullname",
147259 /* 231 */ "idlist",
147260 /* 232 */ "with",
147261 /* 233 */ "setlist",
147262 /* 234 */ "insert_cmd",
147263 /* 235 */ "idlist_opt",
147264 /* 236 */ "upsert",
147265 /* 237 */ "over_clause",
147266 /* 238 */ "likeop",
147267 /* 239 */ "between_op",
147268 /* 240 */ "in_op",
147269 /* 241 */ "paren_exprlist",
147270 /* 242 */ "case_operand",
147271 /* 243 */ "case_exprlist",
147272 /* 244 */ "case_else",
147273 /* 245 */ "uniqueflag",
147274 /* 246 */ "collate",
147275 /* 247 */ "nmnum",
147276 /* 248 */ "trigger_decl",
147277 /* 249 */ "trigger_cmd_list",
147278 /* 250 */ "trigger_time",
147279 /* 251 */ "trigger_event",
147280 /* 252 */ "foreach_clause",
147281 /* 253 */ "when_clause",
147282 /* 254 */ "trigger_cmd",
147283 /* 255 */ "trnm",
147284 /* 256 */ "tridxby",
147285 /* 257 */ "database_kw_opt",
147286 /* 258 */ "key_opt",
147287 /* 259 */ "add_column_fullname",
147288 /* 260 */ "kwcolumn_opt",
147289 /* 261 */ "create_vtab",
147290 /* 262 */ "vtabarglist",
147291 /* 263 */ "vtabarg",
147292 /* 264 */ "vtabargtoken",
147293 /* 265 */ "lp",
147294 /* 266 */ "anylist",
147295 /* 267 */ "windowdefn_list",
147296 /* 268 */ "windowdefn",
147297 /* 269 */ "window",
147298 /* 270 */ "frame_opt",
147299 /* 271 */ "part_opt",
147300 /* 272 */ "filter_opt",
147301 /* 273 */ "range_or_rows",
147302 /* 274 */ "frame_bound",
147303 /* 275 */ "frame_bound_s",
147304 /* 276 */ "frame_bound_e",
147305};
147306#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
147307
147308#ifndef NDEBUG
147309/* For tracing reduce actions, the names of all rules are required.
147310*/
147311static const char *const yyRuleName[] = {
147312 /* 0 */ "explain ::= EXPLAIN",
147313 /* 1 */ "explain ::= EXPLAIN QUERY PLAN",
147314 /* 2 */ "cmdx ::= cmd",
147315 /* 3 */ "cmd ::= BEGIN transtype trans_opt",
147316 /* 4 */ "transtype ::=",
147317 /* 5 */ "transtype ::= DEFERRED",
147318 /* 6 */ "transtype ::= IMMEDIATE",
147319 /* 7 */ "transtype ::= EXCLUSIVE",
147320 /* 8 */ "cmd ::= COMMIT|END trans_opt",
147321 /* 9 */ "cmd ::= ROLLBACK trans_opt",
147322 /* 10 */ "cmd ::= SAVEPOINT nm",
147323 /* 11 */ "cmd ::= RELEASE savepoint_opt nm",
147324 /* 12 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
147325 /* 13 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
147326 /* 14 */ "createkw ::= CREATE",
147327 /* 15 */ "ifnotexists ::=",
147328 /* 16 */ "ifnotexists ::= IF NOT EXISTS",
147329 /* 17 */ "temp ::= TEMP",
147330 /* 18 */ "temp ::=",
147331 /* 19 */ "create_table_args ::= LP columnlist conslist_opt RP table_options",
147332 /* 20 */ "create_table_args ::= AS select",
147333 /* 21 */ "table_options ::=",
147334 /* 22 */ "table_options ::= WITHOUT nm",
147335 /* 23 */ "columnname ::= nm typetoken",
147336 /* 24 */ "typetoken ::=",
147337 /* 25 */ "typetoken ::= typename LP signed RP",
147338 /* 26 */ "typetoken ::= typename LP signed COMMA signed RP",
147339 /* 27 */ "typename ::= typename ID|STRING",
147340 /* 28 */ "scanpt ::=",
147341 /* 29 */ "ccons ::= CONSTRAINT nm",
147342 /* 30 */ "ccons ::= DEFAULT scanpt term scanpt",
147343 /* 31 */ "ccons ::= DEFAULT LP expr RP",
147344 /* 32 */ "ccons ::= DEFAULT PLUS term scanpt",
147345 /* 33 */ "ccons ::= DEFAULT MINUS term scanpt",
147346 /* 34 */ "ccons ::= DEFAULT scanpt ID|INDEXED",
147347 /* 35 */ "ccons ::= NOT NULL onconf",
147348 /* 36 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
147349 /* 37 */ "ccons ::= UNIQUE onconf",
147350 /* 38 */ "ccons ::= CHECK LP expr RP",
147351 /* 39 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
147352 /* 40 */ "ccons ::= defer_subclause",
147353 /* 41 */ "ccons ::= COLLATE ID|STRING",
147354 /* 42 */ "autoinc ::=",
147355 /* 43 */ "autoinc ::= AUTOINCR",
147356 /* 44 */ "refargs ::=",
147357 /* 45 */ "refargs ::= refargs refarg",
147358 /* 46 */ "refarg ::= MATCH nm",
147359 /* 47 */ "refarg ::= ON INSERT refact",
147360 /* 48 */ "refarg ::= ON DELETE refact",
147361 /* 49 */ "refarg ::= ON UPDATE refact",
147362 /* 50 */ "refact ::= SET NULL",
147363 /* 51 */ "refact ::= SET DEFAULT",
147364 /* 52 */ "refact ::= CASCADE",
147365 /* 53 */ "refact ::= RESTRICT",
147366 /* 54 */ "refact ::= NO ACTION",
147367 /* 55 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
147368 /* 56 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
147369 /* 57 */ "init_deferred_pred_opt ::=",
147370 /* 58 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
147371 /* 59 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
147372 /* 60 */ "conslist_opt ::=",
147373 /* 61 */ "tconscomma ::= COMMA",
147374 /* 62 */ "tcons ::= CONSTRAINT nm",
147375 /* 63 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
147376 /* 64 */ "tcons ::= UNIQUE LP sortlist RP onconf",
147377 /* 65 */ "tcons ::= CHECK LP expr RP onconf",
147378 /* 66 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt",
147379 /* 67 */ "defer_subclause_opt ::=",
147380 /* 68 */ "onconf ::=",
147381 /* 69 */ "onconf ::= ON CONFLICT resolvetype",
147382 /* 70 */ "orconf ::=",
147383 /* 71 */ "orconf ::= OR resolvetype",
147384 /* 72 */ "resolvetype ::= IGNORE",
147385 /* 73 */ "resolvetype ::= REPLACE",
147386 /* 74 */ "cmd ::= DROP TABLE ifexists fullname",
147387 /* 75 */ "ifexists ::= IF EXISTS",
147388 /* 76 */ "ifexists ::=",
147389 /* 77 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select",
147390 /* 78 */ "cmd ::= DROP VIEW ifexists fullname",
147391 /* 79 */ "cmd ::= select",
147392 /* 80 */ "select ::= WITH wqlist selectnowith",
147393 /* 81 */ "select ::= WITH RECURSIVE wqlist selectnowith",
147394 /* 82 */ "select ::= selectnowith",
147395 /* 83 */ "selectnowith ::= selectnowith multiselect_op oneselect",
147396 /* 84 */ "multiselect_op ::= UNION",
147397 /* 85 */ "multiselect_op ::= UNION ALL",
147398 /* 86 */ "multiselect_op ::= EXCEPT|INTERSECT",
147399 /* 87 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
147400 /* 88 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt",
147401 /* 89 */ "values ::= VALUES LP nexprlist RP",
147402 /* 90 */ "values ::= values COMMA LP nexprlist RP",
147403 /* 91 */ "distinct ::= DISTINCT",
147404 /* 92 */ "distinct ::= ALL",
147405 /* 93 */ "distinct ::=",
147406 /* 94 */ "sclp ::=",
147407 /* 95 */ "selcollist ::= sclp scanpt expr scanpt as",
147408 /* 96 */ "selcollist ::= sclp scanpt STAR",
147409 /* 97 */ "selcollist ::= sclp scanpt nm DOT STAR",
147410 /* 98 */ "as ::= AS nm",
147411 /* 99 */ "as ::=",
147412 /* 100 */ "from ::=",
147413 /* 101 */ "from ::= FROM seltablist",
147414 /* 102 */ "stl_prefix ::= seltablist joinop",
147415 /* 103 */ "stl_prefix ::=",
147416 /* 104 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
147417 /* 105 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt",
147418 /* 106 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
147419 /* 107 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
147420 /* 108 */ "dbnm ::=",
147421 /* 109 */ "dbnm ::= DOT nm",
147422 /* 110 */ "fullname ::= nm",
147423 /* 111 */ "fullname ::= nm DOT nm",
147424 /* 112 */ "xfullname ::= nm",
147425 /* 113 */ "xfullname ::= nm DOT nm",
147426 /* 114 */ "xfullname ::= nm DOT nm AS nm",
147427 /* 115 */ "xfullname ::= nm AS nm",
147428 /* 116 */ "joinop ::= COMMA|JOIN",
147429 /* 117 */ "joinop ::= JOIN_KW JOIN",
147430 /* 118 */ "joinop ::= JOIN_KW nm JOIN",
147431 /* 119 */ "joinop ::= JOIN_KW nm nm JOIN",
147432 /* 120 */ "on_opt ::= ON expr",
147433 /* 121 */ "on_opt ::=",
147434 /* 122 */ "indexed_opt ::=",
147435 /* 123 */ "indexed_opt ::= INDEXED BY nm",
147436 /* 124 */ "indexed_opt ::= NOT INDEXED",
147437 /* 125 */ "using_opt ::= USING LP idlist RP",
147438 /* 126 */ "using_opt ::=",
147439 /* 127 */ "orderby_opt ::=",
147440 /* 128 */ "orderby_opt ::= ORDER BY sortlist",
147441 /* 129 */ "sortlist ::= sortlist COMMA expr sortorder",
147442 /* 130 */ "sortlist ::= expr sortorder",
147443 /* 131 */ "sortorder ::= ASC",
147444 /* 132 */ "sortorder ::= DESC",
147445 /* 133 */ "sortorder ::=",
147446 /* 134 */ "groupby_opt ::=",
147447 /* 135 */ "groupby_opt ::= GROUP BY nexprlist",
147448 /* 136 */ "having_opt ::=",
147449 /* 137 */ "having_opt ::= HAVING expr",
147450 /* 138 */ "limit_opt ::=",
147451 /* 139 */ "limit_opt ::= LIMIT expr",
147452 /* 140 */ "limit_opt ::= LIMIT expr OFFSET expr",
147453 /* 141 */ "limit_opt ::= LIMIT expr COMMA expr",
147454 /* 142 */ "cmd ::= with DELETE FROM xfullname indexed_opt where_opt",
147455 /* 143 */ "where_opt ::=",
147456 /* 144 */ "where_opt ::= WHERE expr",
147457 /* 145 */ "cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt",
147458 /* 146 */ "setlist ::= setlist COMMA nm EQ expr",
147459 /* 147 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
147460 /* 148 */ "setlist ::= nm EQ expr",
147461 /* 149 */ "setlist ::= LP idlist RP EQ expr",
147462 /* 150 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert",
147463 /* 151 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES",
147464 /* 152 */ "upsert ::=",
147465 /* 153 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt",
147466 /* 154 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING",
147467 /* 155 */ "upsert ::= ON CONFLICT DO NOTHING",
147468 /* 156 */ "insert_cmd ::= INSERT orconf",
147469 /* 157 */ "insert_cmd ::= REPLACE",
147470 /* 158 */ "idlist_opt ::=",
147471 /* 159 */ "idlist_opt ::= LP idlist RP",
147472 /* 160 */ "idlist ::= idlist COMMA nm",
147473 /* 161 */ "idlist ::= nm",
147474 /* 162 */ "expr ::= LP expr RP",
147475 /* 163 */ "expr ::= ID|INDEXED",
147476 /* 164 */ "expr ::= JOIN_KW",
147477 /* 165 */ "expr ::= nm DOT nm",
147478 /* 166 */ "expr ::= nm DOT nm DOT nm",
147479 /* 167 */ "term ::= NULL|FLOAT|BLOB",
147480 /* 168 */ "term ::= STRING",
147481 /* 169 */ "term ::= INTEGER",
147482 /* 170 */ "expr ::= VARIABLE",
147483 /* 171 */ "expr ::= expr COLLATE ID|STRING",
147484 /* 172 */ "expr ::= CAST LP expr AS typetoken RP",
147485 /* 173 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
147486 /* 174 */ "expr ::= ID|INDEXED LP STAR RP",
147487 /* 175 */ "expr ::= ID|INDEXED LP distinct exprlist RP over_clause",
147488 /* 176 */ "expr ::= ID|INDEXED LP STAR RP over_clause",
147489 /* 177 */ "term ::= CTIME_KW",
147490 /* 178 */ "expr ::= LP nexprlist COMMA expr RP",
147491 /* 179 */ "expr ::= expr AND expr",
147492 /* 180 */ "expr ::= expr OR expr",
147493 /* 181 */ "expr ::= expr LT|GT|GE|LE expr",
147494 /* 182 */ "expr ::= expr EQ|NE expr",
147495 /* 183 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
147496 /* 184 */ "expr ::= expr PLUS|MINUS expr",
147497 /* 185 */ "expr ::= expr STAR|SLASH|REM expr",
147498 /* 186 */ "expr ::= expr CONCAT expr",
147499 /* 187 */ "likeop ::= NOT LIKE_KW|MATCH",
147500 /* 188 */ "expr ::= expr likeop expr",
147501 /* 189 */ "expr ::= expr likeop expr ESCAPE expr",
147502 /* 190 */ "expr ::= expr ISNULL|NOTNULL",
147503 /* 191 */ "expr ::= expr NOT NULL",
147504 /* 192 */ "expr ::= expr IS expr",
147505 /* 193 */ "expr ::= expr IS NOT expr",
147506 /* 194 */ "expr ::= NOT expr",
147507 /* 195 */ "expr ::= BITNOT expr",
147508 /* 196 */ "expr ::= PLUS|MINUS expr",
147509 /* 197 */ "between_op ::= BETWEEN",
147510 /* 198 */ "between_op ::= NOT BETWEEN",
147511 /* 199 */ "expr ::= expr between_op expr AND expr",
147512 /* 200 */ "in_op ::= IN",
147513 /* 201 */ "in_op ::= NOT IN",
147514 /* 202 */ "expr ::= expr in_op LP exprlist RP",
147515 /* 203 */ "expr ::= LP select RP",
147516 /* 204 */ "expr ::= expr in_op LP select RP",
147517 /* 205 */ "expr ::= expr in_op nm dbnm paren_exprlist",
147518 /* 206 */ "expr ::= EXISTS LP select RP",
147519 /* 207 */ "expr ::= CASE case_operand case_exprlist case_else END",
147520 /* 208 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
147521 /* 209 */ "case_exprlist ::= WHEN expr THEN expr",
147522 /* 210 */ "case_else ::= ELSE expr",
147523 /* 211 */ "case_else ::=",
147524 /* 212 */ "case_operand ::= expr",
147525 /* 213 */ "case_operand ::=",
147526 /* 214 */ "exprlist ::=",
147527 /* 215 */ "nexprlist ::= nexprlist COMMA expr",
147528 /* 216 */ "nexprlist ::= expr",
147529 /* 217 */ "paren_exprlist ::=",
147530 /* 218 */ "paren_exprlist ::= LP exprlist RP",
147531 /* 219 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
147532 /* 220 */ "uniqueflag ::= UNIQUE",
147533 /* 221 */ "uniqueflag ::=",
147534 /* 222 */ "eidlist_opt ::=",
147535 /* 223 */ "eidlist_opt ::= LP eidlist RP",
147536 /* 224 */ "eidlist ::= eidlist COMMA nm collate sortorder",
147537 /* 225 */ "eidlist ::= nm collate sortorder",
147538 /* 226 */ "collate ::=",
147539 /* 227 */ "collate ::= COLLATE ID|STRING",
147540 /* 228 */ "cmd ::= DROP INDEX ifexists fullname",
147541 /* 229 */ "cmd ::= VACUUM",
147542 /* 230 */ "cmd ::= VACUUM nm",
147543 /* 231 */ "cmd ::= PRAGMA nm dbnm",
147544 /* 232 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
147545 /* 233 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
147546 /* 234 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
147547 /* 235 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
147548 /* 236 */ "plus_num ::= PLUS INTEGER|FLOAT",
147549 /* 237 */ "minus_num ::= MINUS INTEGER|FLOAT",
147550 /* 238 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
147551 /* 239 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
147552 /* 240 */ "trigger_time ::= BEFORE|AFTER",
147553 /* 241 */ "trigger_time ::= INSTEAD OF",
147554 /* 242 */ "trigger_time ::=",
147555 /* 243 */ "trigger_event ::= DELETE|INSERT",
147556 /* 244 */ "trigger_event ::= UPDATE",
147557 /* 245 */ "trigger_event ::= UPDATE OF idlist",
147558 /* 246 */ "when_clause ::=",
147559 /* 247 */ "when_clause ::= WHEN expr",
147560 /* 248 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
147561 /* 249 */ "trigger_cmd_list ::= trigger_cmd SEMI",
147562 /* 250 */ "trnm ::= nm DOT nm",
147563 /* 251 */ "tridxby ::= INDEXED BY nm",
147564 /* 252 */ "tridxby ::= NOT INDEXED",
147565 /* 253 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt",
147566 /* 254 */ "trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt",
147567 /* 255 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt",
147568 /* 256 */ "trigger_cmd ::= scanpt select scanpt",
147569 /* 257 */ "expr ::= RAISE LP IGNORE RP",
147570 /* 258 */ "expr ::= RAISE LP raisetype COMMA nm RP",
147571 /* 259 */ "raisetype ::= ROLLBACK",
147572 /* 260 */ "raisetype ::= ABORT",
147573 /* 261 */ "raisetype ::= FAIL",
147574 /* 262 */ "cmd ::= DROP TRIGGER ifexists fullname",
147575 /* 263 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
147576 /* 264 */ "cmd ::= DETACH database_kw_opt expr",
147577 /* 265 */ "key_opt ::=",
147578 /* 266 */ "key_opt ::= KEY expr",
147579 /* 267 */ "cmd ::= REINDEX",
147580 /* 268 */ "cmd ::= REINDEX nm dbnm",
147581 /* 269 */ "cmd ::= ANALYZE",
147582 /* 270 */ "cmd ::= ANALYZE nm dbnm",
147583 /* 271 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
147584 /* 272 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
147585 /* 273 */ "add_column_fullname ::= fullname",
147586 /* 274 */ "cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm",
147587 /* 275 */ "cmd ::= create_vtab",
147588 /* 276 */ "cmd ::= create_vtab LP vtabarglist RP",
147589 /* 277 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
147590 /* 278 */ "vtabarg ::=",
147591 /* 279 */ "vtabargtoken ::= ANY",
147592 /* 280 */ "vtabargtoken ::= lp anylist RP",
147593 /* 281 */ "lp ::= LP",
147594 /* 282 */ "with ::= WITH wqlist",
147595 /* 283 */ "with ::= WITH RECURSIVE wqlist",
147596 /* 284 */ "wqlist ::= nm eidlist_opt AS LP select RP",
147597 /* 285 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
147598 /* 286 */ "windowdefn_list ::= windowdefn",
147599 /* 287 */ "windowdefn_list ::= windowdefn_list COMMA windowdefn",
147600 /* 288 */ "windowdefn ::= nm AS window",
147601 /* 289 */ "window ::= LP part_opt orderby_opt frame_opt RP",
147602 /* 290 */ "part_opt ::= PARTITION BY nexprlist",
147603 /* 291 */ "part_opt ::=",
147604 /* 292 */ "frame_opt ::=",
147605 /* 293 */ "frame_opt ::= range_or_rows frame_bound_s",
147606 /* 294 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e",
147607 /* 295 */ "range_or_rows ::= RANGE",
147608 /* 296 */ "range_or_rows ::= ROWS",
147609 /* 297 */ "frame_bound_s ::= frame_bound",
147610 /* 298 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
147611 /* 299 */ "frame_bound_e ::= frame_bound",
147612 /* 300 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
147613 /* 301 */ "frame_bound ::= expr PRECEDING",
147614 /* 302 */ "frame_bound ::= CURRENT ROW",
147615 /* 303 */ "frame_bound ::= expr FOLLOWING",
147616 /* 304 */ "window_clause ::= WINDOW windowdefn_list",
147617 /* 305 */ "over_clause ::= filter_opt OVER window",
147618 /* 306 */ "over_clause ::= filter_opt OVER nm",
147619 /* 307 */ "filter_opt ::=",
147620 /* 308 */ "filter_opt ::= FILTER LP WHERE expr RP",
147621 /* 309 */ "input ::= cmdlist",
147622 /* 310 */ "cmdlist ::= cmdlist ecmd",
147623 /* 311 */ "cmdlist ::= ecmd",
147624 /* 312 */ "ecmd ::= SEMI",
147625 /* 313 */ "ecmd ::= cmdx SEMI",
147626 /* 314 */ "ecmd ::= explain cmdx",
147627 /* 315 */ "trans_opt ::=",
147628 /* 316 */ "trans_opt ::= TRANSACTION",
147629 /* 317 */ "trans_opt ::= TRANSACTION nm",
147630 /* 318 */ "savepoint_opt ::= SAVEPOINT",
147631 /* 319 */ "savepoint_opt ::=",
147632 /* 320 */ "cmd ::= create_table create_table_args",
147633 /* 321 */ "columnlist ::= columnlist COMMA columnname carglist",
147634 /* 322 */ "columnlist ::= columnname carglist",
147635 /* 323 */ "nm ::= ID|INDEXED",
147636 /* 324 */ "nm ::= STRING",
147637 /* 325 */ "nm ::= JOIN_KW",
147638 /* 326 */ "typetoken ::= typename",
147639 /* 327 */ "typename ::= ID|STRING",
147640 /* 328 */ "signed ::= plus_num",
147641 /* 329 */ "signed ::= minus_num",
147642 /* 330 */ "carglist ::= carglist ccons",
147643 /* 331 */ "carglist ::=",
147644 /* 332 */ "ccons ::= NULL onconf",
147645 /* 333 */ "conslist_opt ::= COMMA conslist",
147646 /* 334 */ "conslist ::= conslist tconscomma tcons",
147647 /* 335 */ "conslist ::= tcons",
147648 /* 336 */ "tconscomma ::=",
147649 /* 337 */ "defer_subclause_opt ::= defer_subclause",
147650 /* 338 */ "resolvetype ::= raisetype",
147651 /* 339 */ "selectnowith ::= oneselect",
147652 /* 340 */ "oneselect ::= values",
147653 /* 341 */ "sclp ::= selcollist COMMA",
147654 /* 342 */ "as ::= ID|STRING",
147655 /* 343 */ "expr ::= term",
147656 /* 344 */ "likeop ::= LIKE_KW|MATCH",
147657 /* 345 */ "exprlist ::= nexprlist",
147658 /* 346 */ "nmnum ::= plus_num",
147659 /* 347 */ "nmnum ::= nm",
147660 /* 348 */ "nmnum ::= ON",
147661 /* 349 */ "nmnum ::= DELETE",
147662 /* 350 */ "nmnum ::= DEFAULT",
147663 /* 351 */ "plus_num ::= INTEGER|FLOAT",
147664 /* 352 */ "foreach_clause ::=",
147665 /* 353 */ "foreach_clause ::= FOR EACH ROW",
147666 /* 354 */ "trnm ::= nm",
147667 /* 355 */ "tridxby ::=",
147668 /* 356 */ "database_kw_opt ::= DATABASE",
147669 /* 357 */ "database_kw_opt ::=",
147670 /* 358 */ "kwcolumn_opt ::=",
147671 /* 359 */ "kwcolumn_opt ::= COLUMNKW",
147672 /* 360 */ "vtabarglist ::= vtabarg",
147673 /* 361 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
147674 /* 362 */ "vtabarg ::= vtabarg vtabargtoken",
147675 /* 363 */ "anylist ::=",
147676 /* 364 */ "anylist ::= anylist LP anylist RP",
147677 /* 365 */ "anylist ::= anylist ANY",
147678 /* 366 */ "with ::=",
147679};
147680#endif /* NDEBUG */
147681
147682
147683#if YYSTACKDEPTH<=0
147684/*
147685** Try to increase the size of the parser stack. Return the number
147686** of errors. Return 0 on success.
147687*/
147688static int yyGrowStack(yyParser *p){
147689 int newSize;
147690 int idx;
147691 yyStackEntry *pNew;
147692
147693 newSize = p->yystksz*2 + 100;
147694 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
147695 if( p->yystack==&p->yystk0 ){
147696 pNew = malloc(newSize*sizeof(pNew[0]));
147697 if( pNew ) pNew[0] = p->yystk0;
147698 }else{
147699 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
147700 }
147701 if( pNew ){
147702 p->yystack = pNew;
147703 p->yytos = &p->yystack[idx];
147704#ifndef NDEBUG
147705 if( yyTraceFILE ){
147706 fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
147707 yyTracePrompt, p->yystksz, newSize);
147708 }
147709#endif
147710 p->yystksz = newSize;
147711 }
147712 return pNew==0;
147713}
147714#endif
147715
147716/* Datatype of the argument to the memory allocated passed as the
147717** second argument to sqlite3ParserAlloc() below. This can be changed by
147718** putting an appropriate #define in the %include section of the input
147719** grammar.
147720*/
147721#ifndef YYMALLOCARGTYPE
147722# define YYMALLOCARGTYPE size_t
147723#endif
147724
147725/* Initialize a new parser that has already been allocated.
147726*/
147727SQLITE_PRIVATE void sqlite3ParserInit(void *yypRawParser sqlite3ParserCTX_PDECL){
147728 yyParser *yypParser = (yyParser*)yypRawParser;
147729 sqlite3ParserCTX_STORE
147730#ifdef YYTRACKMAXSTACKDEPTH
147731 yypParser->yyhwm = 0;
147732#endif
147733#if YYSTACKDEPTH<=0
147734 yypParser->yytos = NULL;
147735 yypParser->yystack = NULL;
147736 yypParser->yystksz = 0;
147737 if( yyGrowStack(yypParser) ){
147738 yypParser->yystack = &yypParser->yystk0;
147739 yypParser->yystksz = 1;
147740 }
147741#endif
147742#ifndef YYNOERRORRECOVERY
147743 yypParser->yyerrcnt = -1;
147744#endif
147745 yypParser->yytos = yypParser->yystack;
147746 yypParser->yystack[0].stateno = 0;
147747 yypParser->yystack[0].major = 0;
147748#if YYSTACKDEPTH>0
147749 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
147750#endif
147751}
147752
147753#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
147754/*
147755** This function allocates a new parser.
147756** The only argument is a pointer to a function which works like
147757** malloc.
147758**
147759** Inputs:
147760** A pointer to the function used to allocate memory.
147761**
147762** Outputs:
147763** A pointer to a parser. This pointer is used in subsequent calls
147764** to sqlite3Parser and sqlite3ParserFree.
147765*/
147766SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) sqlite3ParserCTX_PDECL){
147767 yyParser *yypParser;
147768 yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
147769 if( yypParser ){
147770 sqlite3ParserCTX_STORE
147771 sqlite3ParserInit(yypParser sqlite3ParserCTX_PARAM);
147772 }
147773 return (void*)yypParser;
147774}
147775#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
147776
147777
147778/* The following function deletes the "minor type" or semantic value
147779** associated with a symbol. The symbol can be either a terminal
147780** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
147781** a pointer to the value to be deleted. The code used to do the
147782** deletions is derived from the %destructor and/or %token_destructor
147783** directives of the input grammar.
147784*/
147785static void yy_destructor(
147786 yyParser *yypParser, /* The parser */
147787 YYCODETYPE yymajor, /* Type code for object to destroy */
147788 YYMINORTYPE *yypminor /* The object to be destroyed */
147789){
147790 sqlite3ParserARG_FETCH
147791 sqlite3ParserCTX_FETCH
147792 switch( yymajor ){
147793 /* Here is inserted the actions which take place when a
147794 ** terminal or non-terminal is destroyed. This can happen
147795 ** when the symbol is popped from the stack during a
147796 ** reduce or during error processing or when a parser is
147797 ** being destroyed before it is finished parsing.
147798 **
147799 ** Note: during a reduce, the only symbols destroyed are those
147800 ** which appear on the RHS of the rule, but which are *not* used
147801 ** inside the C code.
147802 */
147803/********* Begin destructor definitions ***************************************/
147804 case 174: /* select */
147805 case 206: /* selectnowith */
147806 case 207: /* oneselect */
147807 case 219: /* values */
147808{
147809sqlite3SelectDelete(pParse->db, (yypminor->yy489));
147810}
147811 break;
147812 case 184: /* term */
147813 case 185: /* expr */
147814 case 213: /* where_opt */
147815 case 215: /* having_opt */
147816 case 227: /* on_opt */
147817 case 242: /* case_operand */
147818 case 244: /* case_else */
147819 case 253: /* when_clause */
147820 case 258: /* key_opt */
147821 case 272: /* filter_opt */
147822{
147823sqlite3ExprDelete(pParse->db, (yypminor->yy18));
147824}
147825 break;
147826 case 189: /* eidlist_opt */
147827 case 198: /* sortlist */
147828 case 199: /* eidlist */
147829 case 211: /* selcollist */
147830 case 214: /* groupby_opt */
147831 case 216: /* orderby_opt */
147832 case 220: /* nexprlist */
147833 case 221: /* sclp */
147834 case 229: /* exprlist */
147835 case 233: /* setlist */
147836 case 241: /* paren_exprlist */
147837 case 243: /* case_exprlist */
147838 case 271: /* part_opt */
147839{
147840sqlite3ExprListDelete(pParse->db, (yypminor->yy420));
147841}
147842 break;
147843 case 205: /* fullname */
147844 case 212: /* from */
147845 case 223: /* seltablist */
147846 case 224: /* stl_prefix */
147847 case 230: /* xfullname */
147848{
147849sqlite3SrcListDelete(pParse->db, (yypminor->yy135));
147850}
147851 break;
147852 case 208: /* wqlist */
147853{
147854sqlite3WithDelete(pParse->db, (yypminor->yy449));
147855}
147856 break;
147857 case 218: /* window_clause */
147858 case 267: /* windowdefn_list */
147859{
147860sqlite3WindowListDelete(pParse->db, (yypminor->yy327));
147861}
147862 break;
147863 case 228: /* using_opt */
147864 case 231: /* idlist */
147865 case 235: /* idlist_opt */
147866{
147867sqlite3IdListDelete(pParse->db, (yypminor->yy48));
147868}
147869 break;
147870 case 237: /* over_clause */
147871 case 268: /* windowdefn */
147872 case 269: /* window */
147873 case 270: /* frame_opt */
147874{
147875sqlite3WindowDelete(pParse->db, (yypminor->yy327));
147876}
147877 break;
147878 case 249: /* trigger_cmd_list */
147879 case 254: /* trigger_cmd */
147880{
147881sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy207));
147882}
147883 break;
147884 case 251: /* trigger_event */
147885{
147886sqlite3IdListDelete(pParse->db, (yypminor->yy34).b);
147887}
147888 break;
147889 case 274: /* frame_bound */
147890 case 275: /* frame_bound_s */
147891 case 276: /* frame_bound_e */
147892{
147893sqlite3ExprDelete(pParse->db, (yypminor->yy119).pExpr);
147894}
147895 break;
147896/********* End destructor definitions *****************************************/
147897 default: break; /* If no destructor action specified: do nothing */
147898 }
147899}
147900
147901/*
147902** Pop the parser's stack once.
147903**
147904** If there is a destructor routine associated with the token which
147905** is popped from the stack, then call it.
147906*/
147907static void yy_pop_parser_stack(yyParser *pParser){
147908 yyStackEntry *yytos;
147909 assert( pParser->yytos!=0 );
147910 assert( pParser->yytos > pParser->yystack );
147911 yytos = pParser->yytos--;
147912#ifndef NDEBUG
147913 if( yyTraceFILE ){
147914 fprintf(yyTraceFILE,"%sPopping %s\n",
147915 yyTracePrompt,
147916 yyTokenName[yytos->major]);
147917 }
147918#endif
147919 yy_destructor(pParser, yytos->major, &yytos->minor);
147920}
147921
147922/*
147923** Clear all secondary memory allocations from the parser
147924*/
147925SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){
147926 yyParser *pParser = (yyParser*)p;
147927 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
147928#if YYSTACKDEPTH<=0
147929 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
147930#endif
147931}
147932
147933#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
147934/*
147935** Deallocate and destroy a parser. Destructors are called for
147936** all stack elements before shutting the parser down.
147937**
147938** If the YYPARSEFREENEVERNULL macro exists (for example because it
147939** is defined in a %include section of the input grammar) then it is
147940** assumed that the input pointer is never NULL.
147941*/
147942SQLITE_PRIVATE void sqlite3ParserFree(
147943 void *p, /* The parser to be deleted */
147944 void (*freeProc)(void*) /* Function used to reclaim memory */
147945){
147946#ifndef YYPARSEFREENEVERNULL
147947 if( p==0 ) return;
147948#endif
147949 sqlite3ParserFinalize(p);
147950 (*freeProc)(p);
147951}
147952#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
147953
147954/*
147955** Return the peak depth of the stack for a parser.
147956*/
147957#ifdef YYTRACKMAXSTACKDEPTH
147958SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
147959 yyParser *pParser = (yyParser*)p;
147960 return pParser->yyhwm;
147961}
147962#endif
147963
147964/* This array of booleans keeps track of the parser statement
147965** coverage. The element yycoverage[X][Y] is set when the parser
147966** is in state X and has a lookahead token Y. In a well-tested
147967** systems, every element of this matrix should end up being set.
147968*/
147969#if defined(YYCOVERAGE)
147970static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
147971#endif
147972
147973/*
147974** Write into out a description of every state/lookahead combination that
147975**
147976** (1) has not been used by the parser, and
147977** (2) is not a syntax error.
147978**
147979** Return the number of missed state/lookahead combinations.
147980*/
147981#if defined(YYCOVERAGE)
147982SQLITE_PRIVATE int sqlite3ParserCoverage(FILE *out){
147983 int stateno, iLookAhead, i;
147984 int nMissed = 0;
147985 for(stateno=0; stateno<YYNSTATE; stateno++){
147986 i = yy_shift_ofst[stateno];
147987 for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
147988 if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
147989 if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
147990 if( out ){
147991 fprintf(out,"State %d lookahead %s %s\n", stateno,
147992 yyTokenName[iLookAhead],
147993 yycoverage[stateno][iLookAhead] ? "ok" : "missed");
147994 }
147995 }
147996 }
147997 return nMissed;
147998}
147999#endif
148000
148001/*
148002** Find the appropriate action for a parser given the terminal
148003** look-ahead token iLookAhead.
148004*/
148005static YYACTIONTYPE yy_find_shift_action(
148006 YYCODETYPE iLookAhead, /* The look-ahead token */
148007 YYACTIONTYPE stateno /* Current state number */
148008){
148009 int i;
148010
148011 if( stateno>YY_MAX_SHIFT ) return stateno;
148012 assert( stateno <= YY_SHIFT_COUNT );
148013#if defined(YYCOVERAGE)
148014 yycoverage[stateno][iLookAhead] = 1;
148015#endif
148016 do{
148017 i = yy_shift_ofst[stateno];
148018 assert( i>=0 );
148019 /* assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD ); */
148020 assert( iLookAhead!=YYNOCODE );
148021 assert( iLookAhead < YYNTOKEN );
148022 i += iLookAhead;
148023 if( i>=YY_NLOOKAHEAD || yy_lookahead[i]!=iLookAhead ){
148024#ifdef YYFALLBACK
148025 YYCODETYPE iFallback; /* Fallback token */
148026 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
148027 && (iFallback = yyFallback[iLookAhead])!=0 ){
148028#ifndef NDEBUG
148029 if( yyTraceFILE ){
148030 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
148031 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
148032 }
148033#endif
148034 assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
148035 iLookAhead = iFallback;
148036 continue;
148037 }
148038#endif
148039#ifdef YYWILDCARD
148040 {
148041 int j = i - iLookAhead + YYWILDCARD;
148042 if(
148043#if YY_SHIFT_MIN+YYWILDCARD<0
148044 j>=0 &&
148045#endif
148046#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
148047 j<YY_ACTTAB_COUNT &&
148048#endif
148049 j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) &&
148050 yy_lookahead[j]==YYWILDCARD && iLookAhead>0
148051 ){
148052#ifndef NDEBUG
148053 if( yyTraceFILE ){
148054 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
148055 yyTracePrompt, yyTokenName[iLookAhead],
148056 yyTokenName[YYWILDCARD]);
148057 }
148058#endif /* NDEBUG */
148059 return yy_action[j];
148060 }
148061 }
148062#endif /* YYWILDCARD */
148063 return yy_default[stateno];
148064 }else{
148065 return yy_action[i];
148066 }
148067 }while(1);
148068}
148069
148070/*
148071** Find the appropriate action for a parser given the non-terminal
148072** look-ahead token iLookAhead.
148073*/
148074static YYACTIONTYPE yy_find_reduce_action(
148075 YYACTIONTYPE stateno, /* Current state number */
148076 YYCODETYPE iLookAhead /* The look-ahead token */
148077){
148078 int i;
148079#ifdef YYERRORSYMBOL
148080 if( stateno>YY_REDUCE_COUNT ){
148081 return yy_default[stateno];
148082 }
148083#else
148084 assert( stateno<=YY_REDUCE_COUNT );
148085#endif
148086 i = yy_reduce_ofst[stateno];
148087 assert( iLookAhead!=YYNOCODE );
148088 i += iLookAhead;
148089#ifdef YYERRORSYMBOL
148090 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
148091 return yy_default[stateno];
148092 }
148093#else
148094 assert( i>=0 && i<YY_ACTTAB_COUNT );
148095 assert( yy_lookahead[i]==iLookAhead );
148096#endif
148097 return yy_action[i];
148098}
148099
148100/*
148101** The following routine is called if the stack overflows.
148102*/
148103static void yyStackOverflow(yyParser *yypParser){
148104 sqlite3ParserARG_FETCH
148105 sqlite3ParserCTX_FETCH
148106#ifndef NDEBUG
148107 if( yyTraceFILE ){
148108 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
148109 }
148110#endif
148111 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
148112 /* Here code is inserted which will execute if the parser
148113 ** stack every overflows */
148114/******** Begin %stack_overflow code ******************************************/
148115
148116 sqlite3ErrorMsg(pParse, "parser stack overflow");
148117/******** End %stack_overflow code ********************************************/
148118 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument var */
148119 sqlite3ParserCTX_STORE
148120}
148121
148122/*
148123** Print tracing information for a SHIFT action
148124*/
148125#ifndef NDEBUG
148126static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
148127 if( yyTraceFILE ){
148128 if( yyNewState<YYNSTATE ){
148129 fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
148130 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
148131 yyNewState);
148132 }else{
148133 fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
148134 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
148135 yyNewState - YY_MIN_REDUCE);
148136 }
148137 }
148138}
148139#else
148140# define yyTraceShift(X,Y,Z)
148141#endif
148142
148143/*
148144** Perform a shift action.
148145*/
148146static void yy_shift(
148147 yyParser *yypParser, /* The parser to be shifted */
148148 YYACTIONTYPE yyNewState, /* The new state to shift in */
148149 YYCODETYPE yyMajor, /* The major token to shift in */
148150 sqlite3ParserTOKENTYPE yyMinor /* The minor token to shift in */
148151){
148152 yyStackEntry *yytos;
148153 yypParser->yytos++;
148154#ifdef YYTRACKMAXSTACKDEPTH
148155 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
148156 yypParser->yyhwm++;
148157 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
148158 }
148159#endif
148160#if YYSTACKDEPTH>0
148161 if( yypParser->yytos>yypParser->yystackEnd ){
148162 yypParser->yytos--;
148163 yyStackOverflow(yypParser);
148164 return;
148165 }
148166#else
148167 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
148168 if( yyGrowStack(yypParser) ){
148169 yypParser->yytos--;
148170 yyStackOverflow(yypParser);
148171 return;
148172 }
148173 }
148174#endif
148175 if( yyNewState > YY_MAX_SHIFT ){
148176 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
148177 }
148178 yytos = yypParser->yytos;
148179 yytos->stateno = yyNewState;
148180 yytos->major = yyMajor;
148181 yytos->minor.yy0 = yyMinor;
148182 yyTraceShift(yypParser, yyNewState, "Shift");
148183}
148184
148185/* The following table contains information about every rule that
148186** is used during the reduce.
148187*/
148188static const struct {
148189 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
148190 signed char nrhs; /* Negative of the number of RHS symbols in the rule */
148191} yyRuleInfo[] = {
148192 { 159, -1 }, /* (0) explain ::= EXPLAIN */
148193 { 159, -3 }, /* (1) explain ::= EXPLAIN QUERY PLAN */
148194 { 158, -1 }, /* (2) cmdx ::= cmd */
148195 { 160, -3 }, /* (3) cmd ::= BEGIN transtype trans_opt */
148196 { 161, 0 }, /* (4) transtype ::= */
148197 { 161, -1 }, /* (5) transtype ::= DEFERRED */
148198 { 161, -1 }, /* (6) transtype ::= IMMEDIATE */
148199 { 161, -1 }, /* (7) transtype ::= EXCLUSIVE */
148200 { 160, -2 }, /* (8) cmd ::= COMMIT|END trans_opt */
148201 { 160, -2 }, /* (9) cmd ::= ROLLBACK trans_opt */
148202 { 160, -2 }, /* (10) cmd ::= SAVEPOINT nm */
148203 { 160, -3 }, /* (11) cmd ::= RELEASE savepoint_opt nm */
148204 { 160, -5 }, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
148205 { 165, -6 }, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
148206 { 167, -1 }, /* (14) createkw ::= CREATE */
148207 { 169, 0 }, /* (15) ifnotexists ::= */
148208 { 169, -3 }, /* (16) ifnotexists ::= IF NOT EXISTS */
148209 { 168, -1 }, /* (17) temp ::= TEMP */
148210 { 168, 0 }, /* (18) temp ::= */
148211 { 166, -5 }, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_options */
148212 { 166, -2 }, /* (20) create_table_args ::= AS select */
148213 { 173, 0 }, /* (21) table_options ::= */
148214 { 173, -2 }, /* (22) table_options ::= WITHOUT nm */
148215 { 175, -2 }, /* (23) columnname ::= nm typetoken */
148216 { 177, 0 }, /* (24) typetoken ::= */
148217 { 177, -4 }, /* (25) typetoken ::= typename LP signed RP */
148218 { 177, -6 }, /* (26) typetoken ::= typename LP signed COMMA signed RP */
148219 { 178, -2 }, /* (27) typename ::= typename ID|STRING */
148220 { 182, 0 }, /* (28) scanpt ::= */
148221 { 183, -2 }, /* (29) ccons ::= CONSTRAINT nm */
148222 { 183, -4 }, /* (30) ccons ::= DEFAULT scanpt term scanpt */
148223 { 183, -4 }, /* (31) ccons ::= DEFAULT LP expr RP */
148224 { 183, -4 }, /* (32) ccons ::= DEFAULT PLUS term scanpt */
148225 { 183, -4 }, /* (33) ccons ::= DEFAULT MINUS term scanpt */
148226 { 183, -3 }, /* (34) ccons ::= DEFAULT scanpt ID|INDEXED */
148227 { 183, -3 }, /* (35) ccons ::= NOT NULL onconf */
148228 { 183, -5 }, /* (36) ccons ::= PRIMARY KEY sortorder onconf autoinc */
148229 { 183, -2 }, /* (37) ccons ::= UNIQUE onconf */
148230 { 183, -4 }, /* (38) ccons ::= CHECK LP expr RP */
148231 { 183, -4 }, /* (39) ccons ::= REFERENCES nm eidlist_opt refargs */
148232 { 183, -1 }, /* (40) ccons ::= defer_subclause */
148233 { 183, -2 }, /* (41) ccons ::= COLLATE ID|STRING */
148234 { 188, 0 }, /* (42) autoinc ::= */
148235 { 188, -1 }, /* (43) autoinc ::= AUTOINCR */
148236 { 190, 0 }, /* (44) refargs ::= */
148237 { 190, -2 }, /* (45) refargs ::= refargs refarg */
148238 { 192, -2 }, /* (46) refarg ::= MATCH nm */
148239 { 192, -3 }, /* (47) refarg ::= ON INSERT refact */
148240 { 192, -3 }, /* (48) refarg ::= ON DELETE refact */
148241 { 192, -3 }, /* (49) refarg ::= ON UPDATE refact */
148242 { 193, -2 }, /* (50) refact ::= SET NULL */
148243 { 193, -2 }, /* (51) refact ::= SET DEFAULT */
148244 { 193, -1 }, /* (52) refact ::= CASCADE */
148245 { 193, -1 }, /* (53) refact ::= RESTRICT */
148246 { 193, -2 }, /* (54) refact ::= NO ACTION */
148247 { 191, -3 }, /* (55) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
148248 { 191, -2 }, /* (56) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
148249 { 194, 0 }, /* (57) init_deferred_pred_opt ::= */
148250 { 194, -2 }, /* (58) init_deferred_pred_opt ::= INITIALLY DEFERRED */
148251 { 194, -2 }, /* (59) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
148252 { 172, 0 }, /* (60) conslist_opt ::= */
148253 { 196, -1 }, /* (61) tconscomma ::= COMMA */
148254 { 197, -2 }, /* (62) tcons ::= CONSTRAINT nm */
148255 { 197, -7 }, /* (63) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
148256 { 197, -5 }, /* (64) tcons ::= UNIQUE LP sortlist RP onconf */
148257 { 197, -5 }, /* (65) tcons ::= CHECK LP expr RP onconf */
148258 { 197, -10 }, /* (66) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
148259 { 200, 0 }, /* (67) defer_subclause_opt ::= */
148260 { 186, 0 }, /* (68) onconf ::= */
148261 { 186, -3 }, /* (69) onconf ::= ON CONFLICT resolvetype */
148262 { 201, 0 }, /* (70) orconf ::= */
148263 { 201, -2 }, /* (71) orconf ::= OR resolvetype */
148264 { 202, -1 }, /* (72) resolvetype ::= IGNORE */
148265 { 202, -1 }, /* (73) resolvetype ::= REPLACE */
148266 { 160, -4 }, /* (74) cmd ::= DROP TABLE ifexists fullname */
148267 { 204, -2 }, /* (75) ifexists ::= IF EXISTS */
148268 { 204, 0 }, /* (76) ifexists ::= */
148269 { 160, -9 }, /* (77) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
148270 { 160, -4 }, /* (78) cmd ::= DROP VIEW ifexists fullname */
148271 { 160, -1 }, /* (79) cmd ::= select */
148272 { 174, -3 }, /* (80) select ::= WITH wqlist selectnowith */
148273 { 174, -4 }, /* (81) select ::= WITH RECURSIVE wqlist selectnowith */
148274 { 174, -1 }, /* (82) select ::= selectnowith */
148275 { 206, -3 }, /* (83) selectnowith ::= selectnowith multiselect_op oneselect */
148276 { 209, -1 }, /* (84) multiselect_op ::= UNION */
148277 { 209, -2 }, /* (85) multiselect_op ::= UNION ALL */
148278 { 209, -1 }, /* (86) multiselect_op ::= EXCEPT|INTERSECT */
148279 { 207, -9 }, /* (87) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
148280 { 207, -10 }, /* (88) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
148281 { 219, -4 }, /* (89) values ::= VALUES LP nexprlist RP */
148282 { 219, -5 }, /* (90) values ::= values COMMA LP nexprlist RP */
148283 { 210, -1 }, /* (91) distinct ::= DISTINCT */
148284 { 210, -1 }, /* (92) distinct ::= ALL */
148285 { 210, 0 }, /* (93) distinct ::= */
148286 { 221, 0 }, /* (94) sclp ::= */
148287 { 211, -5 }, /* (95) selcollist ::= sclp scanpt expr scanpt as */
148288 { 211, -3 }, /* (96) selcollist ::= sclp scanpt STAR */
148289 { 211, -5 }, /* (97) selcollist ::= sclp scanpt nm DOT STAR */
148290 { 222, -2 }, /* (98) as ::= AS nm */
148291 { 222, 0 }, /* (99) as ::= */
148292 { 212, 0 }, /* (100) from ::= */
148293 { 212, -2 }, /* (101) from ::= FROM seltablist */
148294 { 224, -2 }, /* (102) stl_prefix ::= seltablist joinop */
148295 { 224, 0 }, /* (103) stl_prefix ::= */
148296 { 223, -7 }, /* (104) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
148297 { 223, -9 }, /* (105) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
148298 { 223, -7 }, /* (106) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
148299 { 223, -7 }, /* (107) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
148300 { 170, 0 }, /* (108) dbnm ::= */
148301 { 170, -2 }, /* (109) dbnm ::= DOT nm */
148302 { 205, -1 }, /* (110) fullname ::= nm */
148303 { 205, -3 }, /* (111) fullname ::= nm DOT nm */
148304 { 230, -1 }, /* (112) xfullname ::= nm */
148305 { 230, -3 }, /* (113) xfullname ::= nm DOT nm */
148306 { 230, -5 }, /* (114) xfullname ::= nm DOT nm AS nm */
148307 { 230, -3 }, /* (115) xfullname ::= nm AS nm */
148308 { 225, -1 }, /* (116) joinop ::= COMMA|JOIN */
148309 { 225, -2 }, /* (117) joinop ::= JOIN_KW JOIN */
148310 { 225, -3 }, /* (118) joinop ::= JOIN_KW nm JOIN */
148311 { 225, -4 }, /* (119) joinop ::= JOIN_KW nm nm JOIN */
148312 { 227, -2 }, /* (120) on_opt ::= ON expr */
148313 { 227, 0 }, /* (121) on_opt ::= */
148314 { 226, 0 }, /* (122) indexed_opt ::= */
148315 { 226, -3 }, /* (123) indexed_opt ::= INDEXED BY nm */
148316 { 226, -2 }, /* (124) indexed_opt ::= NOT INDEXED */
148317 { 228, -4 }, /* (125) using_opt ::= USING LP idlist RP */
148318 { 228, 0 }, /* (126) using_opt ::= */
148319 { 216, 0 }, /* (127) orderby_opt ::= */
148320 { 216, -3 }, /* (128) orderby_opt ::= ORDER BY sortlist */
148321 { 198, -4 }, /* (129) sortlist ::= sortlist COMMA expr sortorder */
148322 { 198, -2 }, /* (130) sortlist ::= expr sortorder */
148323 { 187, -1 }, /* (131) sortorder ::= ASC */
148324 { 187, -1 }, /* (132) sortorder ::= DESC */
148325 { 187, 0 }, /* (133) sortorder ::= */
148326 { 214, 0 }, /* (134) groupby_opt ::= */
148327 { 214, -3 }, /* (135) groupby_opt ::= GROUP BY nexprlist */
148328 { 215, 0 }, /* (136) having_opt ::= */
148329 { 215, -2 }, /* (137) having_opt ::= HAVING expr */
148330 { 217, 0 }, /* (138) limit_opt ::= */
148331 { 217, -2 }, /* (139) limit_opt ::= LIMIT expr */
148332 { 217, -4 }, /* (140) limit_opt ::= LIMIT expr OFFSET expr */
148333 { 217, -4 }, /* (141) limit_opt ::= LIMIT expr COMMA expr */
148334 { 160, -6 }, /* (142) cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
148335 { 213, 0 }, /* (143) where_opt ::= */
148336 { 213, -2 }, /* (144) where_opt ::= WHERE expr */
148337 { 160, -8 }, /* (145) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt */
148338 { 233, -5 }, /* (146) setlist ::= setlist COMMA nm EQ expr */
148339 { 233, -7 }, /* (147) setlist ::= setlist COMMA LP idlist RP EQ expr */
148340 { 233, -3 }, /* (148) setlist ::= nm EQ expr */
148341 { 233, -5 }, /* (149) setlist ::= LP idlist RP EQ expr */
148342 { 160, -7 }, /* (150) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
148343 { 160, -7 }, /* (151) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
148344 { 236, 0 }, /* (152) upsert ::= */
148345 { 236, -11 }, /* (153) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
148346 { 236, -8 }, /* (154) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
148347 { 236, -4 }, /* (155) upsert ::= ON CONFLICT DO NOTHING */
148348 { 234, -2 }, /* (156) insert_cmd ::= INSERT orconf */
148349 { 234, -1 }, /* (157) insert_cmd ::= REPLACE */
148350 { 235, 0 }, /* (158) idlist_opt ::= */
148351 { 235, -3 }, /* (159) idlist_opt ::= LP idlist RP */
148352 { 231, -3 }, /* (160) idlist ::= idlist COMMA nm */
148353 { 231, -1 }, /* (161) idlist ::= nm */
148354 { 185, -3 }, /* (162) expr ::= LP expr RP */
148355 { 185, -1 }, /* (163) expr ::= ID|INDEXED */
148356 { 185, -1 }, /* (164) expr ::= JOIN_KW */
148357 { 185, -3 }, /* (165) expr ::= nm DOT nm */
148358 { 185, -5 }, /* (166) expr ::= nm DOT nm DOT nm */
148359 { 184, -1 }, /* (167) term ::= NULL|FLOAT|BLOB */
148360 { 184, -1 }, /* (168) term ::= STRING */
148361 { 184, -1 }, /* (169) term ::= INTEGER */
148362 { 185, -1 }, /* (170) expr ::= VARIABLE */
148363 { 185, -3 }, /* (171) expr ::= expr COLLATE ID|STRING */
148364 { 185, -6 }, /* (172) expr ::= CAST LP expr AS typetoken RP */
148365 { 185, -5 }, /* (173) expr ::= ID|INDEXED LP distinct exprlist RP */
148366 { 185, -4 }, /* (174) expr ::= ID|INDEXED LP STAR RP */
148367 { 185, -6 }, /* (175) expr ::= ID|INDEXED LP distinct exprlist RP over_clause */
148368 { 185, -5 }, /* (176) expr ::= ID|INDEXED LP STAR RP over_clause */
148369 { 184, -1 }, /* (177) term ::= CTIME_KW */
148370 { 185, -5 }, /* (178) expr ::= LP nexprlist COMMA expr RP */
148371 { 185, -3 }, /* (179) expr ::= expr AND expr */
148372 { 185, -3 }, /* (180) expr ::= expr OR expr */
148373 { 185, -3 }, /* (181) expr ::= expr LT|GT|GE|LE expr */
148374 { 185, -3 }, /* (182) expr ::= expr EQ|NE expr */
148375 { 185, -3 }, /* (183) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
148376 { 185, -3 }, /* (184) expr ::= expr PLUS|MINUS expr */
148377 { 185, -3 }, /* (185) expr ::= expr STAR|SLASH|REM expr */
148378 { 185, -3 }, /* (186) expr ::= expr CONCAT expr */
148379 { 238, -2 }, /* (187) likeop ::= NOT LIKE_KW|MATCH */
148380 { 185, -3 }, /* (188) expr ::= expr likeop expr */
148381 { 185, -5 }, /* (189) expr ::= expr likeop expr ESCAPE expr */
148382 { 185, -2 }, /* (190) expr ::= expr ISNULL|NOTNULL */
148383 { 185, -3 }, /* (191) expr ::= expr NOT NULL */
148384 { 185, -3 }, /* (192) expr ::= expr IS expr */
148385 { 185, -4 }, /* (193) expr ::= expr IS NOT expr */
148386 { 185, -2 }, /* (194) expr ::= NOT expr */
148387 { 185, -2 }, /* (195) expr ::= BITNOT expr */
148388 { 185, -2 }, /* (196) expr ::= PLUS|MINUS expr */
148389 { 239, -1 }, /* (197) between_op ::= BETWEEN */
148390 { 239, -2 }, /* (198) between_op ::= NOT BETWEEN */
148391 { 185, -5 }, /* (199) expr ::= expr between_op expr AND expr */
148392 { 240, -1 }, /* (200) in_op ::= IN */
148393 { 240, -2 }, /* (201) in_op ::= NOT IN */
148394 { 185, -5 }, /* (202) expr ::= expr in_op LP exprlist RP */
148395 { 185, -3 }, /* (203) expr ::= LP select RP */
148396 { 185, -5 }, /* (204) expr ::= expr in_op LP select RP */
148397 { 185, -5 }, /* (205) expr ::= expr in_op nm dbnm paren_exprlist */
148398 { 185, -4 }, /* (206) expr ::= EXISTS LP select RP */
148399 { 185, -5 }, /* (207) expr ::= CASE case_operand case_exprlist case_else END */
148400 { 243, -5 }, /* (208) case_exprlist ::= case_exprlist WHEN expr THEN expr */
148401 { 243, -4 }, /* (209) case_exprlist ::= WHEN expr THEN expr */
148402 { 244, -2 }, /* (210) case_else ::= ELSE expr */
148403 { 244, 0 }, /* (211) case_else ::= */
148404 { 242, -1 }, /* (212) case_operand ::= expr */
148405 { 242, 0 }, /* (213) case_operand ::= */
148406 { 229, 0 }, /* (214) exprlist ::= */
148407 { 220, -3 }, /* (215) nexprlist ::= nexprlist COMMA expr */
148408 { 220, -1 }, /* (216) nexprlist ::= expr */
148409 { 241, 0 }, /* (217) paren_exprlist ::= */
148410 { 241, -3 }, /* (218) paren_exprlist ::= LP exprlist RP */
148411 { 160, -12 }, /* (219) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
148412 { 245, -1 }, /* (220) uniqueflag ::= UNIQUE */
148413 { 245, 0 }, /* (221) uniqueflag ::= */
148414 { 189, 0 }, /* (222) eidlist_opt ::= */
148415 { 189, -3 }, /* (223) eidlist_opt ::= LP eidlist RP */
148416 { 199, -5 }, /* (224) eidlist ::= eidlist COMMA nm collate sortorder */
148417 { 199, -3 }, /* (225) eidlist ::= nm collate sortorder */
148418 { 246, 0 }, /* (226) collate ::= */
148419 { 246, -2 }, /* (227) collate ::= COLLATE ID|STRING */
148420 { 160, -4 }, /* (228) cmd ::= DROP INDEX ifexists fullname */
148421 { 160, -1 }, /* (229) cmd ::= VACUUM */
148422 { 160, -2 }, /* (230) cmd ::= VACUUM nm */
148423 { 160, -3 }, /* (231) cmd ::= PRAGMA nm dbnm */
148424 { 160, -5 }, /* (232) cmd ::= PRAGMA nm dbnm EQ nmnum */
148425 { 160, -6 }, /* (233) cmd ::= PRAGMA nm dbnm LP nmnum RP */
148426 { 160, -5 }, /* (234) cmd ::= PRAGMA nm dbnm EQ minus_num */
148427 { 160, -6 }, /* (235) cmd ::= PRAGMA nm dbnm LP minus_num RP */
148428 { 180, -2 }, /* (236) plus_num ::= PLUS INTEGER|FLOAT */
148429 { 181, -2 }, /* (237) minus_num ::= MINUS INTEGER|FLOAT */
148430 { 160, -5 }, /* (238) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
148431 { 248, -11 }, /* (239) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
148432 { 250, -1 }, /* (240) trigger_time ::= BEFORE|AFTER */
148433 { 250, -2 }, /* (241) trigger_time ::= INSTEAD OF */
148434 { 250, 0 }, /* (242) trigger_time ::= */
148435 { 251, -1 }, /* (243) trigger_event ::= DELETE|INSERT */
148436 { 251, -1 }, /* (244) trigger_event ::= UPDATE */
148437 { 251, -3 }, /* (245) trigger_event ::= UPDATE OF idlist */
148438 { 253, 0 }, /* (246) when_clause ::= */
148439 { 253, -2 }, /* (247) when_clause ::= WHEN expr */
148440 { 249, -3 }, /* (248) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
148441 { 249, -2 }, /* (249) trigger_cmd_list ::= trigger_cmd SEMI */
148442 { 255, -3 }, /* (250) trnm ::= nm DOT nm */
148443 { 256, -3 }, /* (251) tridxby ::= INDEXED BY nm */
148444 { 256, -2 }, /* (252) tridxby ::= NOT INDEXED */
148445 { 254, -8 }, /* (253) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
148446 { 254, -8 }, /* (254) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
148447 { 254, -6 }, /* (255) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
148448 { 254, -3 }, /* (256) trigger_cmd ::= scanpt select scanpt */
148449 { 185, -4 }, /* (257) expr ::= RAISE LP IGNORE RP */
148450 { 185, -6 }, /* (258) expr ::= RAISE LP raisetype COMMA nm RP */
148451 { 203, -1 }, /* (259) raisetype ::= ROLLBACK */
148452 { 203, -1 }, /* (260) raisetype ::= ABORT */
148453 { 203, -1 }, /* (261) raisetype ::= FAIL */
148454 { 160, -4 }, /* (262) cmd ::= DROP TRIGGER ifexists fullname */
148455 { 160, -6 }, /* (263) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
148456 { 160, -3 }, /* (264) cmd ::= DETACH database_kw_opt expr */
148457 { 258, 0 }, /* (265) key_opt ::= */
148458 { 258, -2 }, /* (266) key_opt ::= KEY expr */
148459 { 160, -1 }, /* (267) cmd ::= REINDEX */
148460 { 160, -3 }, /* (268) cmd ::= REINDEX nm dbnm */
148461 { 160, -1 }, /* (269) cmd ::= ANALYZE */
148462 { 160, -3 }, /* (270) cmd ::= ANALYZE nm dbnm */
148463 { 160, -6 }, /* (271) cmd ::= ALTER TABLE fullname RENAME TO nm */
148464 { 160, -7 }, /* (272) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
148465 { 259, -1 }, /* (273) add_column_fullname ::= fullname */
148466 { 160, -8 }, /* (274) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
148467 { 160, -1 }, /* (275) cmd ::= create_vtab */
148468 { 160, -4 }, /* (276) cmd ::= create_vtab LP vtabarglist RP */
148469 { 261, -8 }, /* (277) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
148470 { 263, 0 }, /* (278) vtabarg ::= */
148471 { 264, -1 }, /* (279) vtabargtoken ::= ANY */
148472 { 264, -3 }, /* (280) vtabargtoken ::= lp anylist RP */
148473 { 265, -1 }, /* (281) lp ::= LP */
148474 { 232, -2 }, /* (282) with ::= WITH wqlist */
148475 { 232, -3 }, /* (283) with ::= WITH RECURSIVE wqlist */
148476 { 208, -6 }, /* (284) wqlist ::= nm eidlist_opt AS LP select RP */
148477 { 208, -8 }, /* (285) wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
148478 { 267, -1 }, /* (286) windowdefn_list ::= windowdefn */
148479 { 267, -3 }, /* (287) windowdefn_list ::= windowdefn_list COMMA windowdefn */
148480 { 268, -3 }, /* (288) windowdefn ::= nm AS window */
148481 { 269, -5 }, /* (289) window ::= LP part_opt orderby_opt frame_opt RP */
148482 { 271, -3 }, /* (290) part_opt ::= PARTITION BY nexprlist */
148483 { 271, 0 }, /* (291) part_opt ::= */
148484 { 270, 0 }, /* (292) frame_opt ::= */
148485 { 270, -2 }, /* (293) frame_opt ::= range_or_rows frame_bound_s */
148486 { 270, -5 }, /* (294) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e */
148487 { 273, -1 }, /* (295) range_or_rows ::= RANGE */
148488 { 273, -1 }, /* (296) range_or_rows ::= ROWS */
148489 { 275, -1 }, /* (297) frame_bound_s ::= frame_bound */
148490 { 275, -2 }, /* (298) frame_bound_s ::= UNBOUNDED PRECEDING */
148491 { 276, -1 }, /* (299) frame_bound_e ::= frame_bound */
148492 { 276, -2 }, /* (300) frame_bound_e ::= UNBOUNDED FOLLOWING */
148493 { 274, -2 }, /* (301) frame_bound ::= expr PRECEDING */
148494 { 274, -2 }, /* (302) frame_bound ::= CURRENT ROW */
148495 { 274, -2 }, /* (303) frame_bound ::= expr FOLLOWING */
148496 { 218, -2 }, /* (304) window_clause ::= WINDOW windowdefn_list */
148497 { 237, -3 }, /* (305) over_clause ::= filter_opt OVER window */
148498 { 237, -3 }, /* (306) over_clause ::= filter_opt OVER nm */
148499 { 272, 0 }, /* (307) filter_opt ::= */
148500 { 272, -5 }, /* (308) filter_opt ::= FILTER LP WHERE expr RP */
148501 { 155, -1 }, /* (309) input ::= cmdlist */
148502 { 156, -2 }, /* (310) cmdlist ::= cmdlist ecmd */
148503 { 156, -1 }, /* (311) cmdlist ::= ecmd */
148504 { 157, -1 }, /* (312) ecmd ::= SEMI */
148505 { 157, -2 }, /* (313) ecmd ::= cmdx SEMI */
148506 { 157, -2 }, /* (314) ecmd ::= explain cmdx */
148507 { 162, 0 }, /* (315) trans_opt ::= */
148508 { 162, -1 }, /* (316) trans_opt ::= TRANSACTION */
148509 { 162, -2 }, /* (317) trans_opt ::= TRANSACTION nm */
148510 { 164, -1 }, /* (318) savepoint_opt ::= SAVEPOINT */
148511 { 164, 0 }, /* (319) savepoint_opt ::= */
148512 { 160, -2 }, /* (320) cmd ::= create_table create_table_args */
148513 { 171, -4 }, /* (321) columnlist ::= columnlist COMMA columnname carglist */
148514 { 171, -2 }, /* (322) columnlist ::= columnname carglist */
148515 { 163, -1 }, /* (323) nm ::= ID|INDEXED */
148516 { 163, -1 }, /* (324) nm ::= STRING */
148517 { 163, -1 }, /* (325) nm ::= JOIN_KW */
148518 { 177, -1 }, /* (326) typetoken ::= typename */
148519 { 178, -1 }, /* (327) typename ::= ID|STRING */
148520 { 179, -1 }, /* (328) signed ::= plus_num */
148521 { 179, -1 }, /* (329) signed ::= minus_num */
148522 { 176, -2 }, /* (330) carglist ::= carglist ccons */
148523 { 176, 0 }, /* (331) carglist ::= */
148524 { 183, -2 }, /* (332) ccons ::= NULL onconf */
148525 { 172, -2 }, /* (333) conslist_opt ::= COMMA conslist */
148526 { 195, -3 }, /* (334) conslist ::= conslist tconscomma tcons */
148527 { 195, -1 }, /* (335) conslist ::= tcons */
148528 { 196, 0 }, /* (336) tconscomma ::= */
148529 { 200, -1 }, /* (337) defer_subclause_opt ::= defer_subclause */
148530 { 202, -1 }, /* (338) resolvetype ::= raisetype */
148531 { 206, -1 }, /* (339) selectnowith ::= oneselect */
148532 { 207, -1 }, /* (340) oneselect ::= values */
148533 { 221, -2 }, /* (341) sclp ::= selcollist COMMA */
148534 { 222, -1 }, /* (342) as ::= ID|STRING */
148535 { 185, -1 }, /* (343) expr ::= term */
148536 { 238, -1 }, /* (344) likeop ::= LIKE_KW|MATCH */
148537 { 229, -1 }, /* (345) exprlist ::= nexprlist */
148538 { 247, -1 }, /* (346) nmnum ::= plus_num */
148539 { 247, -1 }, /* (347) nmnum ::= nm */
148540 { 247, -1 }, /* (348) nmnum ::= ON */
148541 { 247, -1 }, /* (349) nmnum ::= DELETE */
148542 { 247, -1 }, /* (350) nmnum ::= DEFAULT */
148543 { 180, -1 }, /* (351) plus_num ::= INTEGER|FLOAT */
148544 { 252, 0 }, /* (352) foreach_clause ::= */
148545 { 252, -3 }, /* (353) foreach_clause ::= FOR EACH ROW */
148546 { 255, -1 }, /* (354) trnm ::= nm */
148547 { 256, 0 }, /* (355) tridxby ::= */
148548 { 257, -1 }, /* (356) database_kw_opt ::= DATABASE */
148549 { 257, 0 }, /* (357) database_kw_opt ::= */
148550 { 260, 0 }, /* (358) kwcolumn_opt ::= */
148551 { 260, -1 }, /* (359) kwcolumn_opt ::= COLUMNKW */
148552 { 262, -1 }, /* (360) vtabarglist ::= vtabarg */
148553 { 262, -3 }, /* (361) vtabarglist ::= vtabarglist COMMA vtabarg */
148554 { 263, -2 }, /* (362) vtabarg ::= vtabarg vtabargtoken */
148555 { 266, 0 }, /* (363) anylist ::= */
148556 { 266, -4 }, /* (364) anylist ::= anylist LP anylist RP */
148557 { 266, -2 }, /* (365) anylist ::= anylist ANY */
148558 { 232, 0 }, /* (366) with ::= */
148559};
148560
148561static void yy_accept(yyParser*); /* Forward Declaration */
148562
148563/*
148564** Perform a reduce action and the shift that must immediately
148565** follow the reduce.
148566**
148567** The yyLookahead and yyLookaheadToken parameters provide reduce actions
148568** access to the lookahead token (if any). The yyLookahead will be YYNOCODE
148569** if the lookahead token has already been consumed. As this procedure is
148570** only called from one place, optimizing compilers will in-line it, which
148571** means that the extra parameters have no performance impact.
148572*/
148573static YYACTIONTYPE yy_reduce(
148574 yyParser *yypParser, /* The parser */
148575 unsigned int yyruleno, /* Number of the rule by which to reduce */
148576 int yyLookahead, /* Lookahead token, or YYNOCODE if none */
148577 sqlite3ParserTOKENTYPE yyLookaheadToken /* Value of the lookahead token */
148578 sqlite3ParserCTX_PDECL /* %extra_context */
148579){
148580 int yygoto; /* The next state */
148581 YYACTIONTYPE yyact; /* The next action */
148582 yyStackEntry *yymsp; /* The top of the parser's stack */
148583 int yysize; /* Amount to pop the stack */
148584 sqlite3ParserARG_FETCH
148585 (void)yyLookahead;
148586 (void)yyLookaheadToken;
148587 yymsp = yypParser->yytos;
148588#ifndef NDEBUG
148589 if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
148590 yysize = yyRuleInfo[yyruleno].nrhs;
148591 if( yysize ){
148592 fprintf(yyTraceFILE, "%sReduce %d [%s], go to state %d.\n",
148593 yyTracePrompt,
148594 yyruleno, yyRuleName[yyruleno], yymsp[yysize].stateno);
148595 }else{
148596 fprintf(yyTraceFILE, "%sReduce %d [%s].\n",
148597 yyTracePrompt, yyruleno, yyRuleName[yyruleno]);
148598 }
148599 }
148600#endif /* NDEBUG */
148601
148602 /* Check that the stack is large enough to grow by a single entry
148603 ** if the RHS of the rule is empty. This ensures that there is room
148604 ** enough on the stack to push the LHS value */
148605 if( yyRuleInfo[yyruleno].nrhs==0 ){
148606#ifdef YYTRACKMAXSTACKDEPTH
148607 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
148608 yypParser->yyhwm++;
148609 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack));
148610 }
148611#endif
148612#if YYSTACKDEPTH>0
148613 if( yypParser->yytos>=yypParser->yystackEnd ){
148614 yyStackOverflow(yypParser);
148615 /* The call to yyStackOverflow() above pops the stack until it is
148616 ** empty, causing the main parser loop to exit. So the return value
148617 ** is never used and does not matter. */
148618 return 0;
148619 }
148620#else
148621 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
148622 if( yyGrowStack(yypParser) ){
148623 yyStackOverflow(yypParser);
148624 /* The call to yyStackOverflow() above pops the stack until it is
148625 ** empty, causing the main parser loop to exit. So the return value
148626 ** is never used and does not matter. */
148627 return 0;
148628 }
148629 yymsp = yypParser->yytos;
148630 }
148631#endif
148632 }
148633
148634 switch( yyruleno ){
148635 /* Beginning here are the reduction cases. A typical example
148636 ** follows:
148637 ** case 0:
148638 ** #line <lineno> <grammarfile>
148639 ** { ... } // User supplied code
148640 ** #line <lineno> <thisfile>
148641 ** break;
148642 */
148643/********** Begin reduce actions **********************************************/
148644 YYMINORTYPE yylhsminor;
148645 case 0: /* explain ::= EXPLAIN */
148646{ pParse->explain = 1; }
148647 break;
148648 case 1: /* explain ::= EXPLAIN QUERY PLAN */
148649{ pParse->explain = 2; }
148650 break;
148651 case 2: /* cmdx ::= cmd */
148652{ sqlite3FinishCoding(pParse); }
148653 break;
148654 case 3: /* cmd ::= BEGIN transtype trans_opt */
148655{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy70);}
148656 break;
148657 case 4: /* transtype ::= */
148658{yymsp[1].minor.yy70 = TK_DEFERRED;}
148659 break;
148660 case 5: /* transtype ::= DEFERRED */
148661 case 6: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==6);
148662 case 7: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==7);
148663{yymsp[0].minor.yy70 = yymsp[0].major; /*A-overwrites-X*/}
148664 break;
148665 case 8: /* cmd ::= COMMIT|END trans_opt */
148666 case 9: /* cmd ::= ROLLBACK trans_opt */ yytestcase(yyruleno==9);
148667{sqlite3EndTransaction(pParse,yymsp[-1].major);}
148668 break;
148669 case 10: /* cmd ::= SAVEPOINT nm */
148670{
148671 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
148672}
148673 break;
148674 case 11: /* cmd ::= RELEASE savepoint_opt nm */
148675{
148676 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
148677}
148678 break;
148679 case 12: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
148680{
148681 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
148682}
148683 break;
148684 case 13: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
148685{
148686 sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy70,0,0,yymsp[-2].minor.yy70);
148687}
148688 break;
148689 case 14: /* createkw ::= CREATE */
148690{disableLookaside(pParse);}
148691 break;
148692 case 15: /* ifnotexists ::= */
148693 case 18: /* temp ::= */ yytestcase(yyruleno==18);
148694 case 21: /* table_options ::= */ yytestcase(yyruleno==21);
148695 case 42: /* autoinc ::= */ yytestcase(yyruleno==42);
148696 case 57: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==57);
148697 case 67: /* defer_subclause_opt ::= */ yytestcase(yyruleno==67);
148698 case 76: /* ifexists ::= */ yytestcase(yyruleno==76);
148699 case 93: /* distinct ::= */ yytestcase(yyruleno==93);
148700 case 226: /* collate ::= */ yytestcase(yyruleno==226);
148701{yymsp[1].minor.yy70 = 0;}
148702 break;
148703 case 16: /* ifnotexists ::= IF NOT EXISTS */
148704{yymsp[-2].minor.yy70 = 1;}
148705 break;
148706 case 17: /* temp ::= TEMP */
148707 case 43: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==43);
148708{yymsp[0].minor.yy70 = 1;}
148709 break;
148710 case 19: /* create_table_args ::= LP columnlist conslist_opt RP table_options */
148711{
148712 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy70,0);
148713}
148714 break;
148715 case 20: /* create_table_args ::= AS select */
148716{
148717 sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy489);
148718 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy489);
148719}
148720 break;
148721 case 22: /* table_options ::= WITHOUT nm */
148722{
148723 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
148724 yymsp[-1].minor.yy70 = TF_WithoutRowid | TF_NoVisibleRowid;
148725 }else{
148726 yymsp[-1].minor.yy70 = 0;
148727 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
148728 }
148729}
148730 break;
148731 case 23: /* columnname ::= nm typetoken */
148732{sqlite3AddColumn(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
148733 break;
148734 case 24: /* typetoken ::= */
148735 case 60: /* conslist_opt ::= */ yytestcase(yyruleno==60);
148736 case 99: /* as ::= */ yytestcase(yyruleno==99);
148737{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = 0;}
148738 break;
148739 case 25: /* typetoken ::= typename LP signed RP */
148740{
148741 yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
148742}
148743 break;
148744 case 26: /* typetoken ::= typename LP signed COMMA signed RP */
148745{
148746 yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
148747}
148748 break;
148749 case 27: /* typename ::= typename ID|STRING */
148750{yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
148751 break;
148752 case 28: /* scanpt ::= */
148753{
148754 assert( yyLookahead!=YYNOCODE );
148755 yymsp[1].minor.yy392 = yyLookaheadToken.z;
148756}
148757 break;
148758 case 29: /* ccons ::= CONSTRAINT nm */
148759 case 62: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==62);
148760{pParse->constraintName = yymsp[0].minor.yy0;}
148761 break;
148762 case 30: /* ccons ::= DEFAULT scanpt term scanpt */
148763{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy18,yymsp[-2].minor.yy392,yymsp[0].minor.yy392);}
148764 break;
148765 case 31: /* ccons ::= DEFAULT LP expr RP */
148766{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy18,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
148767 break;
148768 case 32: /* ccons ::= DEFAULT PLUS term scanpt */
148769{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy18,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy392);}
148770 break;
148771 case 33: /* ccons ::= DEFAULT MINUS term scanpt */
148772{
148773 Expr *p = sqlite3PExpr(pParse, TK_UMINUS, yymsp[-1].minor.yy18, 0);
148774 sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy392);
148775}
148776 break;
148777 case 34: /* ccons ::= DEFAULT scanpt ID|INDEXED */
148778{
148779 Expr *p = tokenExpr(pParse, TK_STRING, yymsp[0].minor.yy0);
148780 if( p ){
148781 sqlite3ExprIdToTrueFalse(p);
148782 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
148783 }
148784 sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
148785}
148786 break;
148787 case 35: /* ccons ::= NOT NULL onconf */
148788{sqlite3AddNotNull(pParse, yymsp[0].minor.yy70);}
148789 break;
148790 case 36: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
148791{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy70,yymsp[0].minor.yy70,yymsp[-2].minor.yy70);}
148792 break;
148793 case 37: /* ccons ::= UNIQUE onconf */
148794{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy70,0,0,0,0,
148795 SQLITE_IDXTYPE_UNIQUE);}
148796 break;
148797 case 38: /* ccons ::= CHECK LP expr RP */
148798{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy18);}
148799 break;
148800 case 39: /* ccons ::= REFERENCES nm eidlist_opt refargs */
148801{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy420,yymsp[0].minor.yy70);}
148802 break;
148803 case 40: /* ccons ::= defer_subclause */
148804{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy70);}
148805 break;
148806 case 41: /* ccons ::= COLLATE ID|STRING */
148807{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
148808 break;
148809 case 44: /* refargs ::= */
148810{ yymsp[1].minor.yy70 = OE_None*0x0101; /* EV: R-19803-45884 */}
148811 break;
148812 case 45: /* refargs ::= refargs refarg */
148813{ yymsp[-1].minor.yy70 = (yymsp[-1].minor.yy70 & ~yymsp[0].minor.yy111.mask) | yymsp[0].minor.yy111.value; }
148814 break;
148815 case 46: /* refarg ::= MATCH nm */
148816{ yymsp[-1].minor.yy111.value = 0; yymsp[-1].minor.yy111.mask = 0x000000; }
148817 break;
148818 case 47: /* refarg ::= ON INSERT refact */
148819{ yymsp[-2].minor.yy111.value = 0; yymsp[-2].minor.yy111.mask = 0x000000; }
148820 break;
148821 case 48: /* refarg ::= ON DELETE refact */
148822{ yymsp[-2].minor.yy111.value = yymsp[0].minor.yy70; yymsp[-2].minor.yy111.mask = 0x0000ff; }
148823 break;
148824 case 49: /* refarg ::= ON UPDATE refact */
148825{ yymsp[-2].minor.yy111.value = yymsp[0].minor.yy70<<8; yymsp[-2].minor.yy111.mask = 0x00ff00; }
148826 break;
148827 case 50: /* refact ::= SET NULL */
148828{ yymsp[-1].minor.yy70 = OE_SetNull; /* EV: R-33326-45252 */}
148829 break;
148830 case 51: /* refact ::= SET DEFAULT */
148831{ yymsp[-1].minor.yy70 = OE_SetDflt; /* EV: R-33326-45252 */}
148832 break;
148833 case 52: /* refact ::= CASCADE */
148834{ yymsp[0].minor.yy70 = OE_Cascade; /* EV: R-33326-45252 */}
148835 break;
148836 case 53: /* refact ::= RESTRICT */
148837{ yymsp[0].minor.yy70 = OE_Restrict; /* EV: R-33326-45252 */}
148838 break;
148839 case 54: /* refact ::= NO ACTION */
148840{ yymsp[-1].minor.yy70 = OE_None; /* EV: R-33326-45252 */}
148841 break;
148842 case 55: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
148843{yymsp[-2].minor.yy70 = 0;}
148844 break;
148845 case 56: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
148846 case 71: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==71);
148847 case 156: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==156);
148848{yymsp[-1].minor.yy70 = yymsp[0].minor.yy70;}
148849 break;
148850 case 58: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
148851 case 75: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==75);
148852 case 198: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==198);
148853 case 201: /* in_op ::= NOT IN */ yytestcase(yyruleno==201);
148854 case 227: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==227);
148855{yymsp[-1].minor.yy70 = 1;}
148856 break;
148857 case 59: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
148858{yymsp[-1].minor.yy70 = 0;}
148859 break;
148860 case 61: /* tconscomma ::= COMMA */
148861{pParse->constraintName.n = 0;}
148862 break;
148863 case 63: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
148864{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy420,yymsp[0].minor.yy70,yymsp[-2].minor.yy70,0);}
148865 break;
148866 case 64: /* tcons ::= UNIQUE LP sortlist RP onconf */
148867{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy420,yymsp[0].minor.yy70,0,0,0,0,
148868 SQLITE_IDXTYPE_UNIQUE);}
148869 break;
148870 case 65: /* tcons ::= CHECK LP expr RP onconf */
148871{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy18);}
148872 break;
148873 case 66: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
148874{
148875 sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy420, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy420, yymsp[-1].minor.yy70);
148876 sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy70);
148877}
148878 break;
148879 case 68: /* onconf ::= */
148880 case 70: /* orconf ::= */ yytestcase(yyruleno==70);
148881{yymsp[1].minor.yy70 = OE_Default;}
148882 break;
148883 case 69: /* onconf ::= ON CONFLICT resolvetype */
148884{yymsp[-2].minor.yy70 = yymsp[0].minor.yy70;}
148885 break;
148886 case 72: /* resolvetype ::= IGNORE */
148887{yymsp[0].minor.yy70 = OE_Ignore;}
148888 break;
148889 case 73: /* resolvetype ::= REPLACE */
148890 case 157: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==157);
148891{yymsp[0].minor.yy70 = OE_Replace;}
148892 break;
148893 case 74: /* cmd ::= DROP TABLE ifexists fullname */
148894{
148895 sqlite3DropTable(pParse, yymsp[0].minor.yy135, 0, yymsp[-1].minor.yy70);
148896}
148897 break;
148898 case 77: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
148899{
148900 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);
148901}
148902 break;
148903 case 78: /* cmd ::= DROP VIEW ifexists fullname */
148904{
148905 sqlite3DropTable(pParse, yymsp[0].minor.yy135, 1, yymsp[-1].minor.yy70);
148906}
148907 break;
148908 case 79: /* cmd ::= select */
148909{
148910 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
148911 sqlite3Select(pParse, yymsp[0].minor.yy489, &dest);
148912 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy489);
148913}
148914 break;
148915 case 80: /* select ::= WITH wqlist selectnowith */
148916{
148917 Select *p = yymsp[0].minor.yy489;
148918 if( p ){
148919 p->pWith = yymsp[-1].minor.yy449;
148920 parserDoubleLinkSelect(pParse, p);
148921 }else{
148922 sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy449);
148923 }
148924 yymsp[-2].minor.yy489 = p;
148925}
148926 break;
148927 case 81: /* select ::= WITH RECURSIVE wqlist selectnowith */
148928{
148929 Select *p = yymsp[0].minor.yy489;
148930 if( p ){
148931 p->pWith = yymsp[-1].minor.yy449;
148932 parserDoubleLinkSelect(pParse, p);
148933 }else{
148934 sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy449);
148935 }
148936 yymsp[-3].minor.yy489 = p;
148937}
148938 break;
148939 case 82: /* select ::= selectnowith */
148940{
148941 Select *p = yymsp[0].minor.yy489;
148942 if( p ){
148943 parserDoubleLinkSelect(pParse, p);
148944 }
148945 yymsp[0].minor.yy489 = p; /*A-overwrites-X*/
148946}
148947 break;
148948 case 83: /* selectnowith ::= selectnowith multiselect_op oneselect */
148949{
148950 Select *pRhs = yymsp[0].minor.yy489;
148951 Select *pLhs = yymsp[-2].minor.yy489;
148952 if( pRhs && pRhs->pPrior ){
148953 SrcList *pFrom;
148954 Token x;
148955 x.n = 0;
148956 parserDoubleLinkSelect(pParse, pRhs);
148957 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
148958 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
148959 }
148960 if( pRhs ){
148961 pRhs->op = (u8)yymsp[-1].minor.yy70;
148962 pRhs->pPrior = pLhs;
148963 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
148964 pRhs->selFlags &= ~SF_MultiValue;
148965 if( yymsp[-1].minor.yy70!=TK_ALL ) pParse->hasCompound = 1;
148966 }else{
148967 sqlite3SelectDelete(pParse->db, pLhs);
148968 }
148969 yymsp[-2].minor.yy489 = pRhs;
148970}
148971 break;
148972 case 84: /* multiselect_op ::= UNION */
148973 case 86: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==86);
148974{yymsp[0].minor.yy70 = yymsp[0].major; /*A-overwrites-OP*/}
148975 break;
148976 case 85: /* multiselect_op ::= UNION ALL */
148977{yymsp[-1].minor.yy70 = TK_ALL;}
148978 break;
148979 case 87: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
148980{
148981 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);
148982}
148983 break;
148984 case 88: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
148985{
148986 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);
148987 if( yymsp[-9].minor.yy489 ){
148988 yymsp[-9].minor.yy489->pWinDefn = yymsp[-2].minor.yy327;
148989 }else{
148990 sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy327);
148991 }
148992}
148993 break;
148994 case 89: /* values ::= VALUES LP nexprlist RP */
148995{
148996 yymsp[-3].minor.yy489 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy420,0,0,0,0,0,SF_Values,0);
148997}
148998 break;
148999 case 90: /* values ::= values COMMA LP nexprlist RP */
149000{
149001 Select *pRight, *pLeft = yymsp[-4].minor.yy489;
149002 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy420,0,0,0,0,0,SF_Values|SF_MultiValue,0);
149003 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
149004 if( pRight ){
149005 pRight->op = TK_ALL;
149006 pRight->pPrior = pLeft;
149007 yymsp[-4].minor.yy489 = pRight;
149008 }else{
149009 yymsp[-4].minor.yy489 = pLeft;
149010 }
149011}
149012 break;
149013 case 91: /* distinct ::= DISTINCT */
149014{yymsp[0].minor.yy70 = SF_Distinct;}
149015 break;
149016 case 92: /* distinct ::= ALL */
149017{yymsp[0].minor.yy70 = SF_All;}
149018 break;
149019 case 94: /* sclp ::= */
149020 case 127: /* orderby_opt ::= */ yytestcase(yyruleno==127);
149021 case 134: /* groupby_opt ::= */ yytestcase(yyruleno==134);
149022 case 214: /* exprlist ::= */ yytestcase(yyruleno==214);
149023 case 217: /* paren_exprlist ::= */ yytestcase(yyruleno==217);
149024 case 222: /* eidlist_opt ::= */ yytestcase(yyruleno==222);
149025{yymsp[1].minor.yy420 = 0;}
149026 break;
149027 case 95: /* selcollist ::= sclp scanpt expr scanpt as */
149028{
149029 yymsp[-4].minor.yy420 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy420, yymsp[-2].minor.yy18);
149030 if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy420, &yymsp[0].minor.yy0, 1);
149031 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy420,yymsp[-3].minor.yy392,yymsp[-1].minor.yy392);
149032}
149033 break;
149034 case 96: /* selcollist ::= sclp scanpt STAR */
149035{
149036 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
149037 yymsp[-2].minor.yy420 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy420, p);
149038}
149039 break;
149040 case 97: /* selcollist ::= sclp scanpt nm DOT STAR */
149041{
149042 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
149043 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
149044 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
149045 yymsp[-4].minor.yy420 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy420, pDot);
149046}
149047 break;
149048 case 98: /* as ::= AS nm */
149049 case 109: /* dbnm ::= DOT nm */ yytestcase(yyruleno==109);
149050 case 236: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==236);
149051 case 237: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==237);
149052{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
149053 break;
149054 case 100: /* from ::= */
149055{yymsp[1].minor.yy135 = sqlite3DbMallocZero(pParse->db, sizeof(*yymsp[1].minor.yy135));}
149056 break;
149057 case 101: /* from ::= FROM seltablist */
149058{
149059 yymsp[-1].minor.yy135 = yymsp[0].minor.yy135;
149060 sqlite3SrcListShiftJoinType(yymsp[-1].minor.yy135);
149061}
149062 break;
149063 case 102: /* stl_prefix ::= seltablist joinop */
149064{
149065 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;
149066}
149067 break;
149068 case 103: /* stl_prefix ::= */
149069{yymsp[1].minor.yy135 = 0;}
149070 break;
149071 case 104: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
149072{
149073 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);
149074 sqlite3SrcListIndexedBy(pParse, yymsp[-6].minor.yy135, &yymsp[-2].minor.yy0);
149075}
149076 break;
149077 case 105: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
149078{
149079 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);
149080 sqlite3SrcListFuncArgs(pParse, yymsp[-8].minor.yy135, yymsp[-4].minor.yy420);
149081}
149082 break;
149083 case 106: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
149084{
149085 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);
149086 }
149087 break;
149088 case 107: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
149089{
149090 if( yymsp[-6].minor.yy135==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy18==0 && yymsp[0].minor.yy48==0 ){
149091 yymsp[-6].minor.yy135 = yymsp[-4].minor.yy135;
149092 }else if( yymsp[-4].minor.yy135->nSrc==1 ){
149093 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);
149094 if( yymsp[-6].minor.yy135 ){
149095 struct SrcList_item *pNew = &yymsp[-6].minor.yy135->a[yymsp[-6].minor.yy135->nSrc-1];
149096 struct SrcList_item *pOld = yymsp[-4].minor.yy135->a;
149097 pNew->zName = pOld->zName;
149098 pNew->zDatabase = pOld->zDatabase;
149099 pNew->pSelect = pOld->pSelect;
149100 pOld->zName = pOld->zDatabase = 0;
149101 pOld->pSelect = 0;
149102 }
149103 sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy135);
149104 }else{
149105 Select *pSubquery;
149106 sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy135);
149107 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy135,0,0,0,0,SF_NestedFrom,0);
149108 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);
149109 }
149110 }
149111 break;
149112 case 108: /* dbnm ::= */
149113 case 122: /* indexed_opt ::= */ yytestcase(yyruleno==122);
149114{yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;}
149115 break;
149116 case 110: /* fullname ::= nm */
149117{
149118 yylhsminor.yy135 = sqlite3SrcListAppend(pParse->db,0,&yymsp[0].minor.yy0,0);
149119 if( IN_RENAME_OBJECT && yylhsminor.yy135 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy135->a[0].zName, &yymsp[0].minor.yy0);
149120}
149121 yymsp[0].minor.yy135 = yylhsminor.yy135;
149122 break;
149123 case 111: /* fullname ::= nm DOT nm */
149124{
149125 yylhsminor.yy135 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
149126 if( IN_RENAME_OBJECT && yylhsminor.yy135 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy135->a[0].zName, &yymsp[0].minor.yy0);
149127}
149128 yymsp[-2].minor.yy135 = yylhsminor.yy135;
149129 break;
149130 case 112: /* xfullname ::= nm */
149131{yymsp[0].minor.yy135 = sqlite3SrcListAppend(pParse->db,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
149132 break;
149133 case 113: /* xfullname ::= nm DOT nm */
149134{yymsp[-2].minor.yy135 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/}
149135 break;
149136 case 114: /* xfullname ::= nm DOT nm AS nm */
149137{
149138 yymsp[-4].minor.yy135 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); /*A-overwrites-X*/
149139 if( yymsp[-4].minor.yy135 ) yymsp[-4].minor.yy135->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
149140}
149141 break;
149142 case 115: /* xfullname ::= nm AS nm */
149143{
149144 yymsp[-2].minor.yy135 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
149145 if( yymsp[-2].minor.yy135 ) yymsp[-2].minor.yy135->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
149146}
149147 break;
149148 case 116: /* joinop ::= COMMA|JOIN */
149149{ yymsp[0].minor.yy70 = JT_INNER; }
149150 break;
149151 case 117: /* joinop ::= JOIN_KW JOIN */
149152{yymsp[-1].minor.yy70 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
149153 break;
149154 case 118: /* joinop ::= JOIN_KW nm JOIN */
149155{yymsp[-2].minor.yy70 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-overwrites-A*/}
149156 break;
149157 case 119: /* joinop ::= JOIN_KW nm nm JOIN */
149158{yymsp[-3].minor.yy70 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);/*X-overwrites-A*/}
149159 break;
149160 case 120: /* on_opt ::= ON expr */
149161 case 137: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==137);
149162 case 144: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==144);
149163 case 210: /* case_else ::= ELSE expr */ yytestcase(yyruleno==210);
149164{yymsp[-1].minor.yy18 = yymsp[0].minor.yy18;}
149165 break;
149166 case 121: /* on_opt ::= */
149167 case 136: /* having_opt ::= */ yytestcase(yyruleno==136);
149168 case 138: /* limit_opt ::= */ yytestcase(yyruleno==138);
149169 case 143: /* where_opt ::= */ yytestcase(yyruleno==143);
149170 case 211: /* case_else ::= */ yytestcase(yyruleno==211);
149171 case 213: /* case_operand ::= */ yytestcase(yyruleno==213);
149172{yymsp[1].minor.yy18 = 0;}
149173 break;
149174 case 123: /* indexed_opt ::= INDEXED BY nm */
149175{yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
149176 break;
149177 case 124: /* indexed_opt ::= NOT INDEXED */
149178{yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
149179 break;
149180 case 125: /* using_opt ::= USING LP idlist RP */
149181{yymsp[-3].minor.yy48 = yymsp[-1].minor.yy48;}
149182 break;
149183 case 126: /* using_opt ::= */
149184 case 158: /* idlist_opt ::= */ yytestcase(yyruleno==158);
149185{yymsp[1].minor.yy48 = 0;}
149186 break;
149187 case 128: /* orderby_opt ::= ORDER BY sortlist */
149188 case 135: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==135);
149189{yymsp[-2].minor.yy420 = yymsp[0].minor.yy420;}
149190 break;
149191 case 129: /* sortlist ::= sortlist COMMA expr sortorder */
149192{
149193 yymsp[-3].minor.yy420 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy420,yymsp[-1].minor.yy18);
149194 sqlite3ExprListSetSortOrder(yymsp[-3].minor.yy420,yymsp[0].minor.yy70);
149195}
149196 break;
149197 case 130: /* sortlist ::= expr sortorder */
149198{
149199 yymsp[-1].minor.yy420 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy18); /*A-overwrites-Y*/
149200 sqlite3ExprListSetSortOrder(yymsp[-1].minor.yy420,yymsp[0].minor.yy70);
149201}
149202 break;
149203 case 131: /* sortorder ::= ASC */
149204{yymsp[0].minor.yy70 = SQLITE_SO_ASC;}
149205 break;
149206 case 132: /* sortorder ::= DESC */
149207{yymsp[0].minor.yy70 = SQLITE_SO_DESC;}
149208 break;
149209 case 133: /* sortorder ::= */
149210{yymsp[1].minor.yy70 = SQLITE_SO_UNDEFINED;}
149211 break;
149212 case 139: /* limit_opt ::= LIMIT expr */
149213{yymsp[-1].minor.yy18 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy18,0);}
149214 break;
149215 case 140: /* limit_opt ::= LIMIT expr OFFSET expr */
149216{yymsp[-3].minor.yy18 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[-2].minor.yy18,yymsp[0].minor.yy18);}
149217 break;
149218 case 141: /* limit_opt ::= LIMIT expr COMMA expr */
149219{yymsp[-3].minor.yy18 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy18,yymsp[-2].minor.yy18);}
149220 break;
149221 case 142: /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
149222{
149223 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy135, &yymsp[-1].minor.yy0);
149224 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy135,yymsp[0].minor.yy18,0,0);
149225}
149226 break;
149227 case 145: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt */
149228{
149229 sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy135, &yymsp[-3].minor.yy0);
149230 sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy420,"set list");
149231 sqlite3Update(pParse,yymsp[-4].minor.yy135,yymsp[-1].minor.yy420,yymsp[0].minor.yy18,yymsp[-5].minor.yy70,0,0,0);
149232}
149233 break;
149234 case 146: /* setlist ::= setlist COMMA nm EQ expr */
149235{
149236 yymsp[-4].minor.yy420 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy420, yymsp[0].minor.yy18);
149237 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy420, &yymsp[-2].minor.yy0, 1);
149238}
149239 break;
149240 case 147: /* setlist ::= setlist COMMA LP idlist RP EQ expr */
149241{
149242 yymsp[-6].minor.yy420 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy420, yymsp[-3].minor.yy48, yymsp[0].minor.yy18);
149243}
149244 break;
149245 case 148: /* setlist ::= nm EQ expr */
149246{
149247 yylhsminor.yy420 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy18);
149248 sqlite3ExprListSetName(pParse, yylhsminor.yy420, &yymsp[-2].minor.yy0, 1);
149249}
149250 yymsp[-2].minor.yy420 = yylhsminor.yy420;
149251 break;
149252 case 149: /* setlist ::= LP idlist RP EQ expr */
149253{
149254 yymsp[-4].minor.yy420 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy48, yymsp[0].minor.yy18);
149255}
149256 break;
149257 case 150: /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
149258{
149259 sqlite3Insert(pParse, yymsp[-3].minor.yy135, yymsp[-1].minor.yy489, yymsp[-2].minor.yy48, yymsp[-5].minor.yy70, yymsp[0].minor.yy340);
149260}
149261 break;
149262 case 151: /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
149263{
149264 sqlite3Insert(pParse, yymsp[-3].minor.yy135, 0, yymsp[-2].minor.yy48, yymsp[-5].minor.yy70, 0);
149265}
149266 break;
149267 case 152: /* upsert ::= */
149268{ yymsp[1].minor.yy340 = 0; }
149269 break;
149270 case 153: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
149271{ yymsp[-10].minor.yy340 = sqlite3UpsertNew(pParse->db,yymsp[-7].minor.yy420,yymsp[-5].minor.yy18,yymsp[-1].minor.yy420,yymsp[0].minor.yy18);}
149272 break;
149273 case 154: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
149274{ yymsp[-7].minor.yy340 = sqlite3UpsertNew(pParse->db,yymsp[-4].minor.yy420,yymsp[-2].minor.yy18,0,0); }
149275 break;
149276 case 155: /* upsert ::= ON CONFLICT DO NOTHING */
149277{ yymsp[-3].minor.yy340 = sqlite3UpsertNew(pParse->db,0,0,0,0); }
149278 break;
149279 case 159: /* idlist_opt ::= LP idlist RP */
149280{yymsp[-2].minor.yy48 = yymsp[-1].minor.yy48;}
149281 break;
149282 case 160: /* idlist ::= idlist COMMA nm */
149283{yymsp[-2].minor.yy48 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy48,&yymsp[0].minor.yy0);}
149284 break;
149285 case 161: /* idlist ::= nm */
149286{yymsp[0].minor.yy48 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
149287 break;
149288 case 162: /* expr ::= LP expr RP */
149289{yymsp[-2].minor.yy18 = yymsp[-1].minor.yy18;}
149290 break;
149291 case 163: /* expr ::= ID|INDEXED */
149292 case 164: /* expr ::= JOIN_KW */ yytestcase(yyruleno==164);
149293{yymsp[0].minor.yy18=tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/}
149294 break;
149295 case 165: /* expr ::= nm DOT nm */
149296{
149297 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
149298 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
149299 if( IN_RENAME_OBJECT ){
149300 sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[0].minor.yy0);
149301 sqlite3RenameTokenMap(pParse, (void*)temp1, &yymsp[-2].minor.yy0);
149302 }
149303 yylhsminor.yy18 = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
149304}
149305 yymsp[-2].minor.yy18 = yylhsminor.yy18;
149306 break;
149307 case 166: /* expr ::= nm DOT nm DOT nm */
149308{
149309 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-4].minor.yy0, 1);
149310 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
149311 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
149312 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
149313 if( IN_RENAME_OBJECT ){
149314 sqlite3RenameTokenMap(pParse, (void*)temp3, &yymsp[0].minor.yy0);
149315 sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[-2].minor.yy0);
149316 }
149317 yylhsminor.yy18 = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
149318}
149319 yymsp[-4].minor.yy18 = yylhsminor.yy18;
149320 break;
149321 case 167: /* term ::= NULL|FLOAT|BLOB */
149322 case 168: /* term ::= STRING */ yytestcase(yyruleno==168);
149323{yymsp[0].minor.yy18=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
149324 break;
149325 case 169: /* term ::= INTEGER */
149326{
149327 yylhsminor.yy18 = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
149328}
149329 yymsp[0].minor.yy18 = yylhsminor.yy18;
149330 break;
149331 case 170: /* expr ::= VARIABLE */
149332{
149333 if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1])) ){
149334 u32 n = yymsp[0].minor.yy0.n;
149335 yymsp[0].minor.yy18 = tokenExpr(pParse, TK_VARIABLE, yymsp[0].minor.yy0);
149336 sqlite3ExprAssignVarNumber(pParse, yymsp[0].minor.yy18, n);
149337 }else{
149338 /* When doing a nested parse, one can include terms in an expression
149339 ** that look like this: #1 #2 ... These terms refer to registers
149340 ** in the virtual machine. #N is the N-th register. */
149341 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
149342 assert( t.n>=2 );
149343 if( pParse->nested==0 ){
149344 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
149345 yymsp[0].minor.yy18 = 0;
149346 }else{
149347 yymsp[0].minor.yy18 = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
149348 if( yymsp[0].minor.yy18 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy18->iTable);
149349 }
149350 }
149351}
149352 break;
149353 case 171: /* expr ::= expr COLLATE ID|STRING */
149354{
149355 yymsp[-2].minor.yy18 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy18, &yymsp[0].minor.yy0, 1);
149356}
149357 break;
149358 case 172: /* expr ::= CAST LP expr AS typetoken RP */
149359{
149360 yymsp[-5].minor.yy18 = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1].minor.yy0, 1);
149361 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy18, yymsp[-3].minor.yy18, 0);
149362}
149363 break;
149364 case 173: /* expr ::= ID|INDEXED LP distinct exprlist RP */
149365{
149366 yylhsminor.yy18 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy420, &yymsp[-4].minor.yy0, yymsp[-2].minor.yy70);
149367}
149368 yymsp[-4].minor.yy18 = yylhsminor.yy18;
149369 break;
149370 case 174: /* expr ::= ID|INDEXED LP STAR RP */
149371{
149372 yylhsminor.yy18 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
149373}
149374 yymsp[-3].minor.yy18 = yylhsminor.yy18;
149375 break;
149376 case 175: /* expr ::= ID|INDEXED LP distinct exprlist RP over_clause */
149377{
149378 yylhsminor.yy18 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy420, &yymsp[-5].minor.yy0, yymsp[-3].minor.yy70);
149379 sqlite3WindowAttach(pParse, yylhsminor.yy18, yymsp[0].minor.yy327);
149380}
149381 yymsp[-5].minor.yy18 = yylhsminor.yy18;
149382 break;
149383 case 176: /* expr ::= ID|INDEXED LP STAR RP over_clause */
149384{
149385 yylhsminor.yy18 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
149386 sqlite3WindowAttach(pParse, yylhsminor.yy18, yymsp[0].minor.yy327);
149387}
149388 yymsp[-4].minor.yy18 = yylhsminor.yy18;
149389 break;
149390 case 177: /* term ::= CTIME_KW */
149391{
149392 yylhsminor.yy18 = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0, 0);
149393}
149394 yymsp[0].minor.yy18 = yylhsminor.yy18;
149395 break;
149396 case 178: /* expr ::= LP nexprlist COMMA expr RP */
149397{
149398 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy420, yymsp[-1].minor.yy18);
149399 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
149400 if( yymsp[-4].minor.yy18 ){
149401 yymsp[-4].minor.yy18->x.pList = pList;
149402 }else{
149403 sqlite3ExprListDelete(pParse->db, pList);
149404 }
149405}
149406 break;
149407 case 179: /* expr ::= expr AND expr */
149408 case 180: /* expr ::= expr OR expr */ yytestcase(yyruleno==180);
149409 case 181: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==181);
149410 case 182: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==182);
149411 case 183: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==183);
149412 case 184: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==184);
149413 case 185: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==185);
149414 case 186: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==186);
149415{yymsp[-2].minor.yy18=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy18,yymsp[0].minor.yy18);}
149416 break;
149417 case 187: /* likeop ::= NOT LIKE_KW|MATCH */
149418{yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/}
149419 break;
149420 case 188: /* expr ::= expr likeop expr */
149421{
149422 ExprList *pList;
149423 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
149424 yymsp[-1].minor.yy0.n &= 0x7fffffff;
149425 pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy18);
149426 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy18);
149427 yymsp[-2].minor.yy18 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
149428 if( bNot ) yymsp[-2].minor.yy18 = sqlite3PExpr(pParse, TK_NOT, yymsp[-2].minor.yy18, 0);
149429 if( yymsp[-2].minor.yy18 ) yymsp[-2].minor.yy18->flags |= EP_InfixFunc;
149430}
149431 break;
149432 case 189: /* expr ::= expr likeop expr ESCAPE expr */
149433{
149434 ExprList *pList;
149435 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
149436 yymsp[-3].minor.yy0.n &= 0x7fffffff;
149437 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy18);
149438 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy18);
149439 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy18);
149440 yymsp[-4].minor.yy18 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
149441 if( bNot ) yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy18, 0);
149442 if( yymsp[-4].minor.yy18 ) yymsp[-4].minor.yy18->flags |= EP_InfixFunc;
149443}
149444 break;
149445 case 190: /* expr ::= expr ISNULL|NOTNULL */
149446{yymsp[-1].minor.yy18 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy18,0);}
149447 break;
149448 case 191: /* expr ::= expr NOT NULL */
149449{yymsp[-2].minor.yy18 = sqlite3PExpr(pParse,TK_NOTNULL,yymsp[-2].minor.yy18,0);}
149450 break;
149451 case 192: /* expr ::= expr IS expr */
149452{
149453 yymsp[-2].minor.yy18 = sqlite3PExpr(pParse,TK_IS,yymsp[-2].minor.yy18,yymsp[0].minor.yy18);
149454 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy18, yymsp[-2].minor.yy18, TK_ISNULL);
149455}
149456 break;
149457 case 193: /* expr ::= expr IS NOT expr */
149458{
149459 yymsp[-3].minor.yy18 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-3].minor.yy18,yymsp[0].minor.yy18);
149460 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy18, yymsp[-3].minor.yy18, TK_NOTNULL);
149461}
149462 break;
149463 case 194: /* expr ::= NOT expr */
149464 case 195: /* expr ::= BITNOT expr */ yytestcase(yyruleno==195);
149465{yymsp[-1].minor.yy18 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy18, 0);/*A-overwrites-B*/}
149466 break;
149467 case 196: /* expr ::= PLUS|MINUS expr */
149468{
149469 yymsp[-1].minor.yy18 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, yymsp[0].minor.yy18, 0);
149470 /*A-overwrites-B*/
149471}
149472 break;
149473 case 197: /* between_op ::= BETWEEN */
149474 case 200: /* in_op ::= IN */ yytestcase(yyruleno==200);
149475{yymsp[0].minor.yy70 = 0;}
149476 break;
149477 case 199: /* expr ::= expr between_op expr AND expr */
149478{
149479 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy18);
149480 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy18);
149481 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy18, 0);
149482 if( yymsp[-4].minor.yy18 ){
149483 yymsp[-4].minor.yy18->x.pList = pList;
149484 }else{
149485 sqlite3ExprListDelete(pParse->db, pList);
149486 }
149487 if( yymsp[-3].minor.yy70 ) yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy18, 0);
149488}
149489 break;
149490 case 202: /* expr ::= expr in_op LP exprlist RP */
149491{
149492 if( yymsp[-1].minor.yy420==0 ){
149493 /* Expressions of the form
149494 **
149495 ** expr1 IN ()
149496 ** expr1 NOT IN ()
149497 **
149498 ** simplify to constants 0 (false) and 1 (true), respectively,
149499 ** regardless of the value of expr1.
149500 */
149501 sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy18);
149502 yymsp[-4].minor.yy18 = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[yymsp[-3].minor.yy70],1);
149503 }else if( yymsp[-1].minor.yy420->nExpr==1 ){
149504 /* Expressions of the form:
149505 **
149506 ** expr1 IN (?1)
149507 ** expr1 NOT IN (?2)
149508 **
149509 ** with exactly one value on the RHS can be simplified to something
149510 ** like this:
149511 **
149512 ** expr1 == ?1
149513 ** expr1 <> ?2
149514 **
149515 ** But, the RHS of the == or <> is marked with the EP_Generic flag
149516 ** so that it may not contribute to the computation of comparison
149517 ** affinity or the collating sequence to use for comparison. Otherwise,
149518 ** the semantics would be subtly different from IN or NOT IN.
149519 */
149520 Expr *pRHS = yymsp[-1].minor.yy420->a[0].pExpr;
149521 yymsp[-1].minor.yy420->a[0].pExpr = 0;
149522 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy420);
149523 /* pRHS cannot be NULL because a malloc error would have been detected
149524 ** before now and control would have never reached this point */
149525 if( ALWAYS(pRHS) ){
149526 pRHS->flags &= ~EP_Collate;
149527 pRHS->flags |= EP_Generic;
149528 }
149529 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, yymsp[-3].minor.yy70 ? TK_NE : TK_EQ, yymsp[-4].minor.yy18, pRHS);
149530 }else{
149531 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy18, 0);
149532 if( yymsp[-4].minor.yy18 ){
149533 yymsp[-4].minor.yy18->x.pList = yymsp[-1].minor.yy420;
149534 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy18);
149535 }else{
149536 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy420);
149537 }
149538 if( yymsp[-3].minor.yy70 ) yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy18, 0);
149539 }
149540 }
149541 break;
149542 case 203: /* expr ::= LP select RP */
149543{
149544 yymsp[-2].minor.yy18 = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
149545 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy18, yymsp[-1].minor.yy489);
149546 }
149547 break;
149548 case 204: /* expr ::= expr in_op LP select RP */
149549{
149550 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy18, 0);
149551 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy18, yymsp[-1].minor.yy489);
149552 if( yymsp[-3].minor.yy70 ) yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy18, 0);
149553 }
149554 break;
149555 case 205: /* expr ::= expr in_op nm dbnm paren_exprlist */
149556{
149557 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
149558 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
149559 if( yymsp[0].minor.yy420 ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, yymsp[0].minor.yy420);
149560 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy18, 0);
149561 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy18, pSelect);
149562 if( yymsp[-3].minor.yy70 ) yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy18, 0);
149563 }
149564 break;
149565 case 206: /* expr ::= EXISTS LP select RP */
149566{
149567 Expr *p;
149568 p = yymsp[-3].minor.yy18 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
149569 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy489);
149570 }
149571 break;
149572 case 207: /* expr ::= CASE case_operand case_exprlist case_else END */
149573{
149574 yymsp[-4].minor.yy18 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy18, 0);
149575 if( yymsp[-4].minor.yy18 ){
149576 yymsp[-4].minor.yy18->x.pList = yymsp[-1].minor.yy18 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy420,yymsp[-1].minor.yy18) : yymsp[-2].minor.yy420;
149577 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy18);
149578 }else{
149579 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy420);
149580 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy18);
149581 }
149582}
149583 break;
149584 case 208: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
149585{
149586 yymsp[-4].minor.yy420 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy420, yymsp[-2].minor.yy18);
149587 yymsp[-4].minor.yy420 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy420, yymsp[0].minor.yy18);
149588}
149589 break;
149590 case 209: /* case_exprlist ::= WHEN expr THEN expr */
149591{
149592 yymsp[-3].minor.yy420 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy18);
149593 yymsp[-3].minor.yy420 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy420, yymsp[0].minor.yy18);
149594}
149595 break;
149596 case 212: /* case_operand ::= expr */
149597{yymsp[0].minor.yy18 = yymsp[0].minor.yy18; /*A-overwrites-X*/}
149598 break;
149599 case 215: /* nexprlist ::= nexprlist COMMA expr */
149600{yymsp[-2].minor.yy420 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy420,yymsp[0].minor.yy18);}
149601 break;
149602 case 216: /* nexprlist ::= expr */
149603{yymsp[0].minor.yy420 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy18); /*A-overwrites-Y*/}
149604 break;
149605 case 218: /* paren_exprlist ::= LP exprlist RP */
149606 case 223: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==223);
149607{yymsp[-2].minor.yy420 = yymsp[-1].minor.yy420;}
149608 break;
149609 case 219: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
149610{
149611 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
149612 sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy420, yymsp[-10].minor.yy70,
149613 &yymsp[-11].minor.yy0, yymsp[0].minor.yy18, SQLITE_SO_ASC, yymsp[-8].minor.yy70, SQLITE_IDXTYPE_APPDEF);
149614 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
149615 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
149616 }
149617}
149618 break;
149619 case 220: /* uniqueflag ::= UNIQUE */
149620 case 260: /* raisetype ::= ABORT */ yytestcase(yyruleno==260);
149621{yymsp[0].minor.yy70 = OE_Abort;}
149622 break;
149623 case 221: /* uniqueflag ::= */
149624{yymsp[1].minor.yy70 = OE_None;}
149625 break;
149626 case 224: /* eidlist ::= eidlist COMMA nm collate sortorder */
149627{
149628 yymsp[-4].minor.yy420 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy420, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy70, yymsp[0].minor.yy70);
149629}
149630 break;
149631 case 225: /* eidlist ::= nm collate sortorder */
149632{
149633 yymsp[-2].minor.yy420 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy70, yymsp[0].minor.yy70); /*A-overwrites-Y*/
149634}
149635 break;
149636 case 228: /* cmd ::= DROP INDEX ifexists fullname */
149637{sqlite3DropIndex(pParse, yymsp[0].minor.yy135, yymsp[-1].minor.yy70);}
149638 break;
149639 case 229: /* cmd ::= VACUUM */
149640{sqlite3Vacuum(pParse,0);}
149641 break;
149642 case 230: /* cmd ::= VACUUM nm */
149643{sqlite3Vacuum(pParse,&yymsp[0].minor.yy0);}
149644 break;
149645 case 231: /* cmd ::= PRAGMA nm dbnm */
149646{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
149647 break;
149648 case 232: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
149649{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
149650 break;
149651 case 233: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
149652{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
149653 break;
149654 case 234: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
149655{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
149656 break;
149657 case 235: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
149658{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
149659 break;
149660 case 238: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
149661{
149662 Token all;
149663 all.z = yymsp[-3].minor.yy0.z;
149664 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
149665 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy207, &all);
149666}
149667 break;
149668 case 239: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
149669{
149670 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);
149671 yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-overwrites-T*/
149672}
149673 break;
149674 case 240: /* trigger_time ::= BEFORE|AFTER */
149675{ yymsp[0].minor.yy70 = yymsp[0].major; /*A-overwrites-X*/ }
149676 break;
149677 case 241: /* trigger_time ::= INSTEAD OF */
149678{ yymsp[-1].minor.yy70 = TK_INSTEAD;}
149679 break;
149680 case 242: /* trigger_time ::= */
149681{ yymsp[1].minor.yy70 = TK_BEFORE; }
149682 break;
149683 case 243: /* trigger_event ::= DELETE|INSERT */
149684 case 244: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==244);
149685{yymsp[0].minor.yy34.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy34.b = 0;}
149686 break;
149687 case 245: /* trigger_event ::= UPDATE OF idlist */
149688{yymsp[-2].minor.yy34.a = TK_UPDATE; yymsp[-2].minor.yy34.b = yymsp[0].minor.yy48;}
149689 break;
149690 case 246: /* when_clause ::= */
149691 case 265: /* key_opt ::= */ yytestcase(yyruleno==265);
149692 case 307: /* filter_opt ::= */ yytestcase(yyruleno==307);
149693{ yymsp[1].minor.yy18 = 0; }
149694 break;
149695 case 247: /* when_clause ::= WHEN expr */
149696 case 266: /* key_opt ::= KEY expr */ yytestcase(yyruleno==266);
149697{ yymsp[-1].minor.yy18 = yymsp[0].minor.yy18; }
149698 break;
149699 case 248: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
149700{
149701 assert( yymsp[-2].minor.yy207!=0 );
149702 yymsp[-2].minor.yy207->pLast->pNext = yymsp[-1].minor.yy207;
149703 yymsp[-2].minor.yy207->pLast = yymsp[-1].minor.yy207;
149704}
149705 break;
149706 case 249: /* trigger_cmd_list ::= trigger_cmd SEMI */
149707{
149708 assert( yymsp[-1].minor.yy207!=0 );
149709 yymsp[-1].minor.yy207->pLast = yymsp[-1].minor.yy207;
149710}
149711 break;
149712 case 250: /* trnm ::= nm DOT nm */
149713{
149714 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
149715 sqlite3ErrorMsg(pParse,
149716 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
149717 "statements within triggers");
149718}
149719 break;
149720 case 251: /* tridxby ::= INDEXED BY nm */
149721{
149722 sqlite3ErrorMsg(pParse,
149723 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
149724 "within triggers");
149725}
149726 break;
149727 case 252: /* tridxby ::= NOT INDEXED */
149728{
149729 sqlite3ErrorMsg(pParse,
149730 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
149731 "within triggers");
149732}
149733 break;
149734 case 253: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
149735{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);}
149736 yymsp[-7].minor.yy207 = yylhsminor.yy207;
149737 break;
149738 case 254: /* trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
149739{
149740 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*/
149741}
149742 yymsp[-7].minor.yy207 = yylhsminor.yy207;
149743 break;
149744 case 255: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
149745{yylhsminor.yy207 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy18, yymsp[-5].minor.yy0.z, yymsp[0].minor.yy392);}
149746 yymsp[-5].minor.yy207 = yylhsminor.yy207;
149747 break;
149748 case 256: /* trigger_cmd ::= scanpt select scanpt */
149749{yylhsminor.yy207 = sqlite3TriggerSelectStep(pParse->db, yymsp[-1].minor.yy489, yymsp[-2].minor.yy392, yymsp[0].minor.yy392); /*yylhsminor.yy207-overwrites-yymsp[-1].minor.yy489*/}
149750 yymsp[-2].minor.yy207 = yylhsminor.yy207;
149751 break;
149752 case 257: /* expr ::= RAISE LP IGNORE RP */
149753{
149754 yymsp[-3].minor.yy18 = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
149755 if( yymsp[-3].minor.yy18 ){
149756 yymsp[-3].minor.yy18->affinity = OE_Ignore;
149757 }
149758}
149759 break;
149760 case 258: /* expr ::= RAISE LP raisetype COMMA nm RP */
149761{
149762 yymsp[-5].minor.yy18 = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1);
149763 if( yymsp[-5].minor.yy18 ) {
149764 yymsp[-5].minor.yy18->affinity = (char)yymsp[-3].minor.yy70;
149765 }
149766}
149767 break;
149768 case 259: /* raisetype ::= ROLLBACK */
149769{yymsp[0].minor.yy70 = OE_Rollback;}
149770 break;
149771 case 261: /* raisetype ::= FAIL */
149772{yymsp[0].minor.yy70 = OE_Fail;}
149773 break;
149774 case 262: /* cmd ::= DROP TRIGGER ifexists fullname */
149775{
149776 sqlite3DropTrigger(pParse,yymsp[0].minor.yy135,yymsp[-1].minor.yy70);
149777}
149778 break;
149779 case 263: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
149780{
149781 sqlite3Attach(pParse, yymsp[-3].minor.yy18, yymsp[-1].minor.yy18, yymsp[0].minor.yy18);
149782}
149783 break;
149784 case 264: /* cmd ::= DETACH database_kw_opt expr */
149785{
149786 sqlite3Detach(pParse, yymsp[0].minor.yy18);
149787}
149788 break;
149789 case 267: /* cmd ::= REINDEX */
149790{sqlite3Reindex(pParse, 0, 0);}
149791 break;
149792 case 268: /* cmd ::= REINDEX nm dbnm */
149793{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
149794 break;
149795 case 269: /* cmd ::= ANALYZE */
149796{sqlite3Analyze(pParse, 0, 0);}
149797 break;
149798 case 270: /* cmd ::= ANALYZE nm dbnm */
149799{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
149800 break;
149801 case 271: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
149802{
149803 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy135,&yymsp[0].minor.yy0);
149804}
149805 break;
149806 case 272: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
149807{
149808 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
149809 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
149810}
149811 break;
149812 case 273: /* add_column_fullname ::= fullname */
149813{
149814 disableLookaside(pParse);
149815 sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy135);
149816}
149817 break;
149818 case 274: /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
149819{
149820 sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy135, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
149821}
149822 break;
149823 case 275: /* cmd ::= create_vtab */
149824{sqlite3VtabFinishParse(pParse,0);}
149825 break;
149826 case 276: /* cmd ::= create_vtab LP vtabarglist RP */
149827{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
149828 break;
149829 case 277: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
149830{
149831 sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy70);
149832}
149833 break;
149834 case 278: /* vtabarg ::= */
149835{sqlite3VtabArgInit(pParse);}
149836 break;
149837 case 279: /* vtabargtoken ::= ANY */
149838 case 280: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==280);
149839 case 281: /* lp ::= LP */ yytestcase(yyruleno==281);
149840{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
149841 break;
149842 case 282: /* with ::= WITH wqlist */
149843 case 283: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==283);
149844{ sqlite3WithPush(pParse, yymsp[0].minor.yy449, 1); }
149845 break;
149846 case 284: /* wqlist ::= nm eidlist_opt AS LP select RP */
149847{
149848 yymsp[-5].minor.yy449 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy420, yymsp[-1].minor.yy489); /*A-overwrites-X*/
149849}
149850 break;
149851 case 285: /* wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
149852{
149853 yymsp[-7].minor.yy449 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy449, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy420, yymsp[-1].minor.yy489);
149854}
149855 break;
149856 case 286: /* windowdefn_list ::= windowdefn */
149857{ yylhsminor.yy327 = yymsp[0].minor.yy327; }
149858 yymsp[0].minor.yy327 = yylhsminor.yy327;
149859 break;
149860 case 287: /* windowdefn_list ::= windowdefn_list COMMA windowdefn */
149861{
149862 assert( yymsp[0].minor.yy327!=0 );
149863 yymsp[0].minor.yy327->pNextWin = yymsp[-2].minor.yy327;
149864 yylhsminor.yy327 = yymsp[0].minor.yy327;
149865}
149866 yymsp[-2].minor.yy327 = yylhsminor.yy327;
149867 break;
149868 case 288: /* windowdefn ::= nm AS window */
149869{
149870 if( ALWAYS(yymsp[0].minor.yy327) ){
149871 yymsp[0].minor.yy327->zName = sqlite3DbStrNDup(pParse->db, yymsp[-2].minor.yy0.z, yymsp[-2].minor.yy0.n);
149872 }
149873 yylhsminor.yy327 = yymsp[0].minor.yy327;
149874}
149875 yymsp[-2].minor.yy327 = yylhsminor.yy327;
149876 break;
149877 case 289: /* window ::= LP part_opt orderby_opt frame_opt RP */
149878{
149879 yymsp[-4].minor.yy327 = yymsp[-1].minor.yy327;
149880 if( ALWAYS(yymsp[-4].minor.yy327) ){
149881 yymsp[-4].minor.yy327->pPartition = yymsp[-3].minor.yy420;
149882 yymsp[-4].minor.yy327->pOrderBy = yymsp[-2].minor.yy420;
149883 }
149884}
149885 break;
149886 case 290: /* part_opt ::= PARTITION BY nexprlist */
149887{ yymsp[-2].minor.yy420 = yymsp[0].minor.yy420; }
149888 break;
149889 case 291: /* part_opt ::= */
149890{ yymsp[1].minor.yy420 = 0; }
149891 break;
149892 case 292: /* frame_opt ::= */
149893{
149894 yymsp[1].minor.yy327 = sqlite3WindowAlloc(pParse, TK_RANGE, TK_UNBOUNDED, 0, TK_CURRENT, 0);
149895}
149896 break;
149897 case 293: /* frame_opt ::= range_or_rows frame_bound_s */
149898{
149899 yylhsminor.yy327 = sqlite3WindowAlloc(pParse, yymsp[-1].minor.yy70, yymsp[0].minor.yy119.eType, yymsp[0].minor.yy119.pExpr, TK_CURRENT, 0);
149900}
149901 yymsp[-1].minor.yy327 = yylhsminor.yy327;
149902 break;
149903 case 294: /* frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e */
149904{
149905 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);
149906}
149907 yymsp[-4].minor.yy327 = yylhsminor.yy327;
149908 break;
149909 case 295: /* range_or_rows ::= RANGE */
149910{ yymsp[0].minor.yy70 = TK_RANGE; }
149911 break;
149912 case 296: /* range_or_rows ::= ROWS */
149913{ yymsp[0].minor.yy70 = TK_ROWS; }
149914 break;
149915 case 297: /* frame_bound_s ::= frame_bound */
149916 case 299: /* frame_bound_e ::= frame_bound */ yytestcase(yyruleno==299);
149917{ yylhsminor.yy119 = yymsp[0].minor.yy119; }
149918 yymsp[0].minor.yy119 = yylhsminor.yy119;
149919 break;
149920 case 298: /* frame_bound_s ::= UNBOUNDED PRECEDING */
149921 case 300: /* frame_bound_e ::= UNBOUNDED FOLLOWING */ yytestcase(yyruleno==300);
149922{yymsp[-1].minor.yy119.eType = TK_UNBOUNDED; yymsp[-1].minor.yy119.pExpr = 0;}
149923 break;
149924 case 301: /* frame_bound ::= expr PRECEDING */
149925{ yylhsminor.yy119.eType = TK_PRECEDING; yylhsminor.yy119.pExpr = yymsp[-1].minor.yy18; }
149926 yymsp[-1].minor.yy119 = yylhsminor.yy119;
149927 break;
149928 case 302: /* frame_bound ::= CURRENT ROW */
149929{ yymsp[-1].minor.yy119.eType = TK_CURRENT ; yymsp[-1].minor.yy119.pExpr = 0; }
149930 break;
149931 case 303: /* frame_bound ::= expr FOLLOWING */
149932{ yylhsminor.yy119.eType = TK_FOLLOWING; yylhsminor.yy119.pExpr = yymsp[-1].minor.yy18; }
149933 yymsp[-1].minor.yy119 = yylhsminor.yy119;
149934 break;
149935 case 304: /* window_clause ::= WINDOW windowdefn_list */
149936{ yymsp[-1].minor.yy327 = yymsp[0].minor.yy327; }
149937 break;
149938 case 305: /* over_clause ::= filter_opt OVER window */
149939{
149940 yylhsminor.yy327 = yymsp[0].minor.yy327;
149941 assert( yylhsminor.yy327!=0 );
149942 yylhsminor.yy327->pFilter = yymsp[-2].minor.yy18;
149943}
149944 yymsp[-2].minor.yy327 = yylhsminor.yy327;
149945 break;
149946 case 306: /* over_clause ::= filter_opt OVER nm */
149947{
149948 yylhsminor.yy327 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
149949 if( yylhsminor.yy327 ){
149950 yylhsminor.yy327->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n);
149951 yylhsminor.yy327->pFilter = yymsp[-2].minor.yy18;
149952 }else{
149953 sqlite3ExprDelete(pParse->db, yymsp[-2].minor.yy18);
149954 }
149955}
149956 yymsp[-2].minor.yy327 = yylhsminor.yy327;
149957 break;
149958 case 308: /* filter_opt ::= FILTER LP WHERE expr RP */
149959{ yymsp[-4].minor.yy18 = yymsp[-1].minor.yy18; }
149960 break;
149961 default:
149962 /* (309) input ::= cmdlist */ yytestcase(yyruleno==309);
149963 /* (310) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==310);
149964 /* (311) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=311);
149965 /* (312) ecmd ::= SEMI */ yytestcase(yyruleno==312);
149966 /* (313) ecmd ::= cmdx SEMI */ yytestcase(yyruleno==313);
149967 /* (314) ecmd ::= explain cmdx */ yytestcase(yyruleno==314);
149968 /* (315) trans_opt ::= */ yytestcase(yyruleno==315);
149969 /* (316) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==316);
149970 /* (317) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==317);
149971 /* (318) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==318);
149972 /* (319) savepoint_opt ::= */ yytestcase(yyruleno==319);
149973 /* (320) cmd ::= create_table create_table_args */ yytestcase(yyruleno==320);
149974 /* (321) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==321);
149975 /* (322) columnlist ::= columnname carglist */ yytestcase(yyruleno==322);
149976 /* (323) nm ::= ID|INDEXED */ yytestcase(yyruleno==323);
149977 /* (324) nm ::= STRING */ yytestcase(yyruleno==324);
149978 /* (325) nm ::= JOIN_KW */ yytestcase(yyruleno==325);
149979 /* (326) typetoken ::= typename */ yytestcase(yyruleno==326);
149980 /* (327) typename ::= ID|STRING */ yytestcase(yyruleno==327);
149981 /* (328) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=328);
149982 /* (329) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=329);
149983 /* (330) carglist ::= carglist ccons */ yytestcase(yyruleno==330);
149984 /* (331) carglist ::= */ yytestcase(yyruleno==331);
149985 /* (332) ccons ::= NULL onconf */ yytestcase(yyruleno==332);
149986 /* (333) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==333);
149987 /* (334) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==334);
149988 /* (335) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=335);
149989 /* (336) tconscomma ::= */ yytestcase(yyruleno==336);
149990 /* (337) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert(yyruleno!=337);
149991 /* (338) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=338);
149992 /* (339) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=339);
149993 /* (340) oneselect ::= values */ yytestcase(yyruleno==340);
149994 /* (341) sclp ::= selcollist COMMA */ yytestcase(yyruleno==341);
149995 /* (342) as ::= ID|STRING */ yytestcase(yyruleno==342);
149996 /* (343) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=343);
149997 /* (344) likeop ::= LIKE_KW|MATCH */ yytestcase(yyruleno==344);
149998 /* (345) exprlist ::= nexprlist */ yytestcase(yyruleno==345);
149999 /* (346) nmnum ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=346);
150000 /* (347) nmnum ::= nm (OPTIMIZED OUT) */ assert(yyruleno!=347);
150001 /* (348) nmnum ::= ON */ yytestcase(yyruleno==348);
150002 /* (349) nmnum ::= DELETE */ yytestcase(yyruleno==349);
150003 /* (350) nmnum ::= DEFAULT */ yytestcase(yyruleno==350);
150004 /* (351) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==351);
150005 /* (352) foreach_clause ::= */ yytestcase(yyruleno==352);
150006 /* (353) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==353);
150007 /* (354) trnm ::= nm */ yytestcase(yyruleno==354);
150008 /* (355) tridxby ::= */ yytestcase(yyruleno==355);
150009 /* (356) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==356);
150010 /* (357) database_kw_opt ::= */ yytestcase(yyruleno==357);
150011 /* (358) kwcolumn_opt ::= */ yytestcase(yyruleno==358);
150012 /* (359) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==359);
150013 /* (360) vtabarglist ::= vtabarg */ yytestcase(yyruleno==360);
150014 /* (361) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==361);
150015 /* (362) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==362);
150016 /* (363) anylist ::= */ yytestcase(yyruleno==363);
150017 /* (364) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==364);
150018 /* (365) anylist ::= anylist ANY */ yytestcase(yyruleno==365);
150019 /* (366) with ::= */ yytestcase(yyruleno==366);
150020 break;
150021/********** End reduce actions ************************************************/
150022 };
150023 assert( yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
150024 yygoto = yyRuleInfo[yyruleno].lhs;
150025 yysize = yyRuleInfo[yyruleno].nrhs;
150026 yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPE)yygoto);
150027
150028 /* There are no SHIFTREDUCE actions on nonterminals because the table
150029 ** generator has simplified them to pure REDUCE actions. */
150030 assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) );
150031
150032 /* It is not possible for a REDUCE to be followed by an error */
150033 assert( yyact!=YY_ERROR_ACTION );
150034
150035 yymsp += yysize+1;
150036 yypParser->yytos = yymsp;
150037 yymsp->stateno = (YYACTIONTYPE)yyact;
150038 yymsp->major = (YYCODETYPE)yygoto;
150039 yyTraceShift(yypParser, yyact, "... then shift");
150040 return yyact;
150041}
150042
150043/*
150044** The following code executes when the parse fails
150045*/
150046#ifndef YYNOERRORRECOVERY
150047static void yy_parse_failed(
150048 yyParser *yypParser /* The parser */
150049){
150050 sqlite3ParserARG_FETCH
150051 sqlite3ParserCTX_FETCH
150052#ifndef NDEBUG
150053 if( yyTraceFILE ){
150054 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
150055 }
150056#endif
150057 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
150058 /* Here code is inserted which will be executed whenever the
150059 ** parser fails */
150060/************ Begin %parse_failure code ***************************************/
150061/************ End %parse_failure code *****************************************/
150062 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
150063 sqlite3ParserCTX_STORE
150064}
150065#endif /* YYNOERRORRECOVERY */
150066
150067/*
150068** The following code executes when a syntax error first occurs.
150069*/
150070static void yy_syntax_error(
150071 yyParser *yypParser, /* The parser */
150072 int yymajor, /* The major type of the error token */
150073 sqlite3ParserTOKENTYPE yyminor /* The minor type of the error token */
150074){
150075 sqlite3ParserARG_FETCH
150076 sqlite3ParserCTX_FETCH
150077#define TOKEN yyminor
150078/************ Begin %syntax_error code ****************************************/
150079
150080 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
150081 if( TOKEN.z[0] ){
150082 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
150083 }else{
150084 sqlite3ErrorMsg(pParse, "incomplete input");
150085 }
150086/************ End %syntax_error code ******************************************/
150087 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
150088 sqlite3ParserCTX_STORE
150089}
150090
150091/*
150092** The following is executed when the parser accepts
150093*/
150094static void yy_accept(
150095 yyParser *yypParser /* The parser */
150096){
150097 sqlite3ParserARG_FETCH
150098 sqlite3ParserCTX_FETCH
150099#ifndef NDEBUG
150100 if( yyTraceFILE ){
150101 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
150102 }
150103#endif
150104#ifndef YYNOERRORRECOVERY
150105 yypParser->yyerrcnt = -1;
150106#endif
150107 assert( yypParser->yytos==yypParser->yystack );
150108 /* Here code is inserted which will be executed whenever the
150109 ** parser accepts */
150110/*********** Begin %parse_accept code *****************************************/
150111/*********** End %parse_accept code *******************************************/
150112 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
150113 sqlite3ParserCTX_STORE
150114}
150115
150116/* The main parser program.
150117** The first argument is a pointer to a structure obtained from
150118** "sqlite3ParserAlloc" which describes the current state of the parser.
150119** The second argument is the major token number. The third is
150120** the minor token. The fourth optional argument is whatever the
150121** user wants (and specified in the grammar) and is available for
150122** use by the action routines.
150123**
150124** Inputs:
150125** <ul>
150126** <li> A pointer to the parser (an opaque structure.)
150127** <li> The major token number.
150128** <li> The minor token number.
150129** <li> An option argument of a grammar-specified type.
150130** </ul>
150131**
150132** Outputs:
150133** None.
150134*/
150135SQLITE_PRIVATE void sqlite3Parser(
150136 void *yyp, /* The parser */
150137 int yymajor, /* The major token code number */
150138 sqlite3ParserTOKENTYPE yyminor /* The value for the token */
150139 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
150140){
150141 YYMINORTYPE yyminorunion;
150142 YYACTIONTYPE yyact; /* The parser action. */
150143#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
150144 int yyendofinput; /* True if we are at the end of input */
150145#endif
150146#ifdef YYERRORSYMBOL
150147 int yyerrorhit = 0; /* True if yymajor has invoked an error */
150148#endif
150149 yyParser *yypParser = (yyParser*)yyp; /* The parser */
150150 sqlite3ParserCTX_FETCH
150151 sqlite3ParserARG_STORE
150152
150153 assert( yypParser->yytos!=0 );
150154#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
150155 yyendofinput = (yymajor==0);
150156#endif
150157
150158 yyact = yypParser->yytos->stateno;
150159#ifndef NDEBUG
150160 if( yyTraceFILE ){
150161 if( yyact < YY_MIN_REDUCE ){
150162 fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
150163 yyTracePrompt,yyTokenName[yymajor],yyact);
150164 }else{
150165 fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
150166 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
150167 }
150168 }
150169#endif
150170
150171 do{
150172 assert( yyact==yypParser->yytos->stateno );
150173 yyact = yy_find_shift_action((YYCODETYPE)yymajor,yyact);
150174 if( yyact >= YY_MIN_REDUCE ){
150175 yyact = yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor,
150176 yyminor sqlite3ParserCTX_PARAM);
150177 }else if( yyact <= YY_MAX_SHIFTREDUCE ){
150178 yy_shift(yypParser,yyact,(YYCODETYPE)yymajor,yyminor);
150179#ifndef YYNOERRORRECOVERY
150180 yypParser->yyerrcnt--;
150181#endif
150182 break;
150183 }else if( yyact==YY_ACCEPT_ACTION ){
150184 yypParser->yytos--;
150185 yy_accept(yypParser);
150186 return;
150187 }else{
150188 assert( yyact == YY_ERROR_ACTION );
150189 yyminorunion.yy0 = yyminor;
150190#ifdef YYERRORSYMBOL
150191 int yymx;
150192#endif
150193#ifndef NDEBUG
150194 if( yyTraceFILE ){
150195 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
150196 }
150197#endif
150198#ifdef YYERRORSYMBOL
150199 /* A syntax error has occurred.
150200 ** The response to an error depends upon whether or not the
150201 ** grammar defines an error token "ERROR".
150202 **
150203 ** This is what we do if the grammar does define ERROR:
150204 **
150205 ** * Call the %syntax_error function.
150206 **
150207 ** * Begin popping the stack until we enter a state where
150208 ** it is legal to shift the error symbol, then shift
150209 ** the error symbol.
150210 **
150211 ** * Set the error count to three.
150212 **
150213 ** * Begin accepting and shifting new tokens. No new error
150214 ** processing will occur until three tokens have been
150215 ** shifted successfully.
150216 **
150217 */
150218 if( yypParser->yyerrcnt<0 ){
150219 yy_syntax_error(yypParser,yymajor,yyminor);
150220 }
150221 yymx = yypParser->yytos->major;
150222 if( yymx==YYERRORSYMBOL || yyerrorhit ){
150223#ifndef NDEBUG
150224 if( yyTraceFILE ){
150225 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
150226 yyTracePrompt,yyTokenName[yymajor]);
150227 }
150228#endif
150229 yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
150230 yymajor = YYNOCODE;
150231 }else{
150232 while( yypParser->yytos >= yypParser->yystack
150233 && yymx != YYERRORSYMBOL
150234 && (yyact = yy_find_reduce_action(
150235 yypParser->yytos->stateno,
150236 YYERRORSYMBOL)) >= YY_MIN_REDUCE
150237 ){
150238 yy_pop_parser_stack(yypParser);
150239 }
150240 if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
150241 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
150242 yy_parse_failed(yypParser);
150243#ifndef YYNOERRORRECOVERY
150244 yypParser->yyerrcnt = -1;
150245#endif
150246 yymajor = YYNOCODE;
150247 }else if( yymx!=YYERRORSYMBOL ){
150248 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
150249 }
150250 }
150251 yypParser->yyerrcnt = 3;
150252 yyerrorhit = 1;
150253 if( yymajor==YYNOCODE ) break;
150254 yyact = yypParser->yytos->stateno;
150255#elif defined(YYNOERRORRECOVERY)
150256 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
150257 ** do any kind of error recovery. Instead, simply invoke the syntax
150258 ** error routine and continue going as if nothing had happened.
150259 **
150260 ** Applications can set this macro (for example inside %include) if
150261 ** they intend to abandon the parse upon the first syntax error seen.
150262 */
150263 yy_syntax_error(yypParser,yymajor, yyminor);
150264 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
150265 break;
150266#else /* YYERRORSYMBOL is not defined */
150267 /* This is what we do if the grammar does not define ERROR:
150268 **
150269 ** * Report an error message, and throw away the input token.
150270 **
150271 ** * If the input token is $, then fail the parse.
150272 **
150273 ** As before, subsequent error messages are suppressed until
150274 ** three input tokens have been successfully shifted.
150275 */
150276 if( yypParser->yyerrcnt<=0 ){
150277 yy_syntax_error(yypParser,yymajor, yyminor);
150278 }
150279 yypParser->yyerrcnt = 3;
150280 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
150281 if( yyendofinput ){
150282 yy_parse_failed(yypParser);
150283#ifndef YYNOERRORRECOVERY
150284 yypParser->yyerrcnt = -1;
150285#endif
150286 }
150287 break;
150288#endif
150289 }
150290 }while( yypParser->yytos>yypParser->yystack );
150291#ifndef NDEBUG
150292 if( yyTraceFILE ){
150293 yyStackEntry *i;
150294 char cDiv = '[';
150295 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
150296 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
150297 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
150298 cDiv = ' ';
150299 }
150300 fprintf(yyTraceFILE,"]\n");
150301 }
150302#endif
150303 return;
150304}
150305
150306/*
150307** Return the fallback token corresponding to canonical token iToken, or
150308** 0 if iToken has no fallback.
150309*/
150310SQLITE_PRIVATE int sqlite3ParserFallback(int iToken){
150311#ifdef YYFALLBACK
150312 if( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) ){
150313 return yyFallback[iToken];
150314 }
150315#else
150316 (void)iToken;
150317#endif
150318 return 0;
150319}
150320
150321/************** End of parse.c ***********************************************/
150322/************** Begin file tokenize.c ****************************************/
150323/*
150324** 2001 September 15
150325**
150326** The author disclaims copyright to this source code. In place of
150327** a legal notice, here is a blessing:
150328**
150329** May you do good and not evil.
150330** May you find forgiveness for yourself and forgive others.
150331** May you share freely, never taking more than you give.
150332**
150333*************************************************************************
150334** An tokenizer for SQL
150335**
150336** This file contains C code that splits an SQL input string up into
150337** individual tokens and sends those tokens one-by-one over to the
150338** parser for analysis.
150339*/
150340/* #include "sqliteInt.h" */
150341/* #include <stdlib.h> */
150342
150343/* Character classes for tokenizing
150344**
150345** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
150346** using a lookup table, whereas a switch() directly on c uses a binary search.
150347** The lookup table is much faster. To maximize speed, and to ensure that
150348** a lookup table is used, all of the classes need to be small integers and
150349** all of them need to be used within the switch.
150350*/
150351#define CC_X 0 /* The letter 'x', or start of BLOB literal */
150352#define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */
150353#define CC_ID 2 /* unicode characters usable in IDs */
150354#define CC_DIGIT 3 /* Digits */
150355#define CC_DOLLAR 4 /* '$' */
150356#define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */
150357#define CC_VARNUM 6 /* '?'. Numeric SQL variables */
150358#define CC_SPACE 7 /* Space characters */
150359#define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */
150360#define CC_QUOTE2 9 /* '['. [...] style quoted ids */
150361#define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */
150362#define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
150363#define CC_LT 12 /* '<'. Part of < or <= or <> */
150364#define CC_GT 13 /* '>'. Part of > or >= */
150365#define CC_EQ 14 /* '='. Part of = or == */
150366#define CC_BANG 15 /* '!'. Part of != */
150367#define CC_SLASH 16 /* '/'. / or c-style comment */
150368#define CC_LP 17 /* '(' */
150369#define CC_RP 18 /* ')' */
150370#define CC_SEMI 19 /* ';' */
150371#define CC_PLUS 20 /* '+' */
150372#define CC_STAR 21 /* '*' */
150373#define CC_PERCENT 22 /* '%' */
150374#define CC_COMMA 23 /* ',' */
150375#define CC_AND 24 /* '&' */
150376#define CC_TILDA 25 /* '~' */
150377#define CC_DOT 26 /* '.' */
150378#define CC_ILLEGAL 27 /* Illegal character */
150379#define CC_NUL 28 /* 0x00 */
150380
150381static const unsigned char aiClass[] = {
150382#ifdef SQLITE_ASCII
150383/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
150384/* 0x */ 28, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27,
150385/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
150386/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
150387/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
150388/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
150389/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1,
150390/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
150391/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27,
150392/* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
150393/* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
150394/* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
150395/* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
150396/* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
150397/* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
150398/* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
150399/* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
150400#endif
150401#ifdef SQLITE_EBCDIC
150402/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
150403/* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27,
150404/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
150405/* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
150406/* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
150407/* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10,
150408/* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27,
150409/* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 6,
150410/* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8,
150411/* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
150412/* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
150413/* Ax */ 27, 25, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
150414/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27,
150415/* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
150416/* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
150417/* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
150418/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27,
150419#endif
150420};
150421
150422/*
150423** The charMap() macro maps alphabetic characters (only) into their
150424** lower-case ASCII equivalent. On ASCII machines, this is just
150425** an upper-to-lower case map. On EBCDIC machines we also need
150426** to adjust the encoding. The mapping is only valid for alphabetics
150427** which are the only characters for which this feature is used.
150428**
150429** Used by keywordhash.h
150430*/
150431#ifdef SQLITE_ASCII
150432# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
150433#endif
150434#ifdef SQLITE_EBCDIC
150435# define charMap(X) ebcdicToAscii[(unsigned char)X]
150436const unsigned char ebcdicToAscii[] = {
150437/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
150438 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
150439 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
150440 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
150441 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
150442 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
150443 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
150444 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
150445 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
150446 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
150447 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
150448 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
150449 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
150450 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
150451 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
150452 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
150453 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
150454};
150455#endif
150456
150457/*
150458** The sqlite3KeywordCode function looks up an identifier to determine if
150459** it is a keyword. If it is a keyword, the token code of that keyword is
150460** returned. If the input is not a keyword, TK_ID is returned.
150461**
150462** The implementation of this routine was generated by a program,
150463** mkkeywordhash.c, located in the tool subdirectory of the distribution.
150464** The output of the mkkeywordhash.c program is written into a file
150465** named keywordhash.h and then included into this source file by
150466** the #include below.
150467*/
150468/************** Include keywordhash.h in the middle of tokenize.c ************/
150469/************** Begin file keywordhash.h *************************************/
150470/***** This file contains automatically generated code ******
150471**
150472** The code in this file has been automatically generated by
150473**
150474** sqlite/tool/mkkeywordhash.c
150475**
150476** The code in this file implements a function that determines whether
150477** or not a given identifier is really an SQL keyword. The same thing
150478** might be implemented more directly using a hand-written hash table.
150479** But by using this automatically generated code, the size of the code
150480** is substantially reduced. This is important for embedded applications
150481** on platforms with limited memory.
150482*/
150483/* Hash score: 208 */
150484/* zKWText[] encodes 923 bytes of keyword text in 614 bytes */
150485/* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */
150486/* ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE */
150487/* XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY */
150488/* UNIQUERYWITHOUTERELEASEATTACHAVINGROUPDATEBEGINNERANGEBETWEEN */
150489/* OTHINGLOBYCASCADELETECASECOLLATECREATECURRENT_DATEDETACH */
150490/* IMMEDIATEJOINSERTLIKEMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMIT */
150491/* WHENOTNULLWHERECURSIVEAFTERENAMEANDEFAULTAUTOINCREMENTCAST */
150492/* COLUMNCOMMITCONFLICTCROSSCURRENT_TIMESTAMPARTITIONDEFERRED */
150493/* ISTINCTDROPRECEDINGFAILFILTEREPLACEFOLLOWINGFROMFULLIFISNULL */
150494/* ORDERESTRICTOVERIGHTROLLBACKROWSUNBOUNDEDUNIONUSINGVACUUMVIEW */
150495/* INDOWINITIALLYPRIMARY */
150496static const char zKWText[613] = {
150497 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
150498 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
150499 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
150500 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
150501 'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
150502 'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
150503 'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
150504 'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
150505 'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
150506 'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
150507 'U','E','R','Y','W','I','T','H','O','U','T','E','R','E','L','E','A','S',
150508 'E','A','T','T','A','C','H','A','V','I','N','G','R','O','U','P','D','A',
150509 'T','E','B','E','G','I','N','N','E','R','A','N','G','E','B','E','T','W',
150510 'E','E','N','O','T','H','I','N','G','L','O','B','Y','C','A','S','C','A',
150511 'D','E','L','E','T','E','C','A','S','E','C','O','L','L','A','T','E','C',
150512 'R','E','A','T','E','C','U','R','R','E','N','T','_','D','A','T','E','D',
150513 'E','T','A','C','H','I','M','M','E','D','I','A','T','E','J','O','I','N',
150514 'S','E','R','T','L','I','K','E','M','A','T','C','H','P','L','A','N','A',
150515 'L','Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U',
150516 'E','S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','O',
150517 'T','N','U','L','L','W','H','E','R','E','C','U','R','S','I','V','E','A',
150518 'F','T','E','R','E','N','A','M','E','A','N','D','E','F','A','U','L','T',
150519 'A','U','T','O','I','N','C','R','E','M','E','N','T','C','A','S','T','C',
150520 'O','L','U','M','N','C','O','M','M','I','T','C','O','N','F','L','I','C',
150521 'T','C','R','O','S','S','C','U','R','R','E','N','T','_','T','I','M','E',
150522 'S','T','A','M','P','A','R','T','I','T','I','O','N','D','E','F','E','R',
150523 'R','E','D','I','S','T','I','N','C','T','D','R','O','P','R','E','C','E',
150524 'D','I','N','G','F','A','I','L','F','I','L','T','E','R','E','P','L','A',
150525 'C','E','F','O','L','L','O','W','I','N','G','F','R','O','M','F','U','L',
150526 'L','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S','T','R',
150527 'I','C','T','O','V','E','R','I','G','H','T','R','O','L','L','B','A','C',
150528 'K','R','O','W','S','U','N','B','O','U','N','D','E','D','U','N','I','O',
150529 'N','U','S','I','N','G','V','A','C','U','U','M','V','I','E','W','I','N',
150530 'D','O','W','I','N','I','T','I','A','L','L','Y','P','R','I','M','A','R',
150531 'Y',
150532};
150533/* aKWHash[i] is the hash value for the i-th keyword */
150534static const unsigned char aKWHash[127] = {
150535 74, 109, 124, 72, 106, 45, 0, 0, 81, 0, 76, 61, 0,
150536 42, 12, 77, 15, 0, 123, 84, 54, 118, 125, 19, 0, 0,
150537 130, 0, 128, 121, 0, 22, 96, 0, 9, 0, 0, 115, 69,
150538 0, 67, 6, 0, 48, 93, 136, 0, 126, 104, 0, 0, 44,
150539 0, 107, 24, 0, 17, 0, 131, 53, 23, 0, 5, 62, 132,
150540 99, 0, 0, 135, 110, 60, 134, 57, 113, 55, 0, 94, 0,
150541 103, 26, 0, 102, 0, 0, 0, 98, 95, 100, 105, 117, 14,
150542 39, 116, 0, 80, 0, 133, 114, 92, 59, 0, 129, 79, 119,
150543 86, 46, 83, 0, 0, 97, 40, 122, 120, 0, 127, 0, 0,
150544 29, 0, 89, 87, 88, 0, 20, 85, 111, 56,
150545};
150546/* aKWNext[] forms the hash collision chain. If aKWHash[i]==0
150547** then the i-th keyword has no more hash collisions. Otherwise,
150548** the next keyword with the same hash is aKWHash[i]-1. */
150549static const unsigned char aKWNext[136] = {
150550 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0,
150551 0, 2, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0,
150552 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
150553 0, 0, 0, 0, 33, 0, 21, 0, 0, 0, 0, 0, 50,
150554 0, 43, 3, 47, 0, 0, 32, 0, 0, 0, 0, 0, 0,
150555 0, 1, 64, 0, 0, 65, 0, 41, 0, 38, 0, 0, 0,
150556 0, 0, 49, 75, 0, 0, 30, 0, 58, 0, 0, 0, 31,
150557 63, 16, 34, 10, 0, 0, 0, 0, 0, 0, 0, 11, 70,
150558 91, 0, 0, 8, 0, 108, 0, 101, 28, 52, 68, 0, 112,
150559 0, 73, 51, 0, 90, 27, 37, 0, 71, 36, 82, 0, 35,
150560 66, 25, 18, 0, 0, 78,
150561};
150562/* aKWLen[i] is the length (in bytes) of the i-th keyword */
150563static const unsigned char aKWLen[136] = {
150564 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6,
150565 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 6,
150566 11, 6, 2, 7, 5, 5, 9, 6, 9, 9, 7, 10, 10,
150567 4, 6, 2, 3, 9, 4, 2, 6, 5, 7, 4, 5, 7,
150568 6, 6, 5, 6, 5, 5, 5, 7, 7, 4, 2, 7, 3,
150569 6, 4, 7, 6, 12, 6, 9, 4, 6, 4, 5, 4, 7,
150570 6, 5, 6, 7, 5, 4, 7, 3, 2, 4, 5, 9, 5,
150571 6, 3, 7, 13, 2, 2, 4, 6, 6, 8, 5, 17, 12,
150572 7, 9, 8, 8, 2, 4, 9, 4, 6, 7, 9, 4, 4,
150573 2, 6, 5, 8, 4, 5, 8, 4, 3, 9, 5, 5, 6,
150574 4, 6, 2, 9, 3, 7,
150575};
150576/* aKWOffset[i] is the index into zKWText[] of the start of
150577** the text for the i-th keyword. */
150578static const unsigned short int aKWOffset[136] = {
150579 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33,
150580 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81,
150581 86, 91, 95, 96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
150582 159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 184, 188, 192,
150583 199, 204, 209, 212, 218, 221, 225, 230, 236, 242, 245, 247, 248,
150584 252, 258, 262, 269, 275, 287, 293, 302, 304, 310, 314, 319, 321,
150585 328, 333, 338, 344, 350, 355, 358, 358, 358, 361, 365, 368, 377,
150586 381, 387, 389, 396, 398, 400, 409, 413, 419, 425, 433, 438, 438,
150587 438, 454, 463, 470, 471, 478, 481, 490, 494, 499, 506, 515, 519,
150588 523, 525, 531, 535, 543, 546, 551, 559, 559, 563, 572, 577, 582,
150589 588, 591, 594, 597, 602, 606,
150590};
150591/* aKWCode[i] is the parser symbol code for the i-th keyword */
150592static const unsigned char aKWCode[136] = {
150593 TK_REINDEX, TK_INDEXED, TK_INDEX, TK_DESC, TK_ESCAPE,
150594 TK_EACH, TK_CHECK, TK_KEY, TK_BEFORE, TK_FOREIGN,
150595 TK_FOR, TK_IGNORE, TK_LIKE_KW, TK_EXPLAIN, TK_INSTEAD,
150596 TK_ADD, TK_DATABASE, TK_AS, TK_SELECT, TK_TABLE,
150597 TK_JOIN_KW, TK_THEN, TK_END, TK_DEFERRABLE, TK_ELSE,
150598 TK_EXCEPT, TK_TRANSACTION,TK_ACTION, TK_ON, TK_JOIN_KW,
150599 TK_ALTER, TK_RAISE, TK_EXCLUSIVE, TK_EXISTS, TK_SAVEPOINT,
150600 TK_INTERSECT, TK_TRIGGER, TK_REFERENCES, TK_CONSTRAINT, TK_INTO,
150601 TK_OFFSET, TK_OF, TK_SET, TK_TEMP, TK_TEMP,
150602 TK_OR, TK_UNIQUE, TK_QUERY, TK_WITHOUT, TK_WITH,
150603 TK_JOIN_KW, TK_RELEASE, TK_ATTACH, TK_HAVING, TK_GROUP,
150604 TK_UPDATE, TK_BEGIN, TK_JOIN_KW, TK_RANGE, TK_BETWEEN,
150605 TK_NOTHING, TK_LIKE_KW, TK_BY, TK_CASCADE, TK_ASC,
150606 TK_DELETE, TK_CASE, TK_COLLATE, TK_CREATE, TK_CTIME_KW,
150607 TK_DETACH, TK_IMMEDIATE, TK_JOIN, TK_INSERT, TK_LIKE_KW,
150608 TK_MATCH, TK_PLAN, TK_ANALYZE, TK_PRAGMA, TK_ABORT,
150609 TK_VALUES, TK_VIRTUAL, TK_LIMIT, TK_WHEN, TK_NOTNULL,
150610 TK_NOT, TK_NO, TK_NULL, TK_WHERE, TK_RECURSIVE,
150611 TK_AFTER, TK_RENAME, TK_AND, TK_DEFAULT, TK_AUTOINCR,
150612 TK_TO, TK_IN, TK_CAST, TK_COLUMNKW, TK_COMMIT,
150613 TK_CONFLICT, TK_JOIN_KW, TK_CTIME_KW, TK_CTIME_KW, TK_CURRENT,
150614 TK_PARTITION, TK_DEFERRED, TK_DISTINCT, TK_IS, TK_DROP,
150615 TK_PRECEDING, TK_FAIL, TK_FILTER, TK_REPLACE, TK_FOLLOWING,
150616 TK_FROM, TK_JOIN_KW, TK_IF, TK_ISNULL, TK_ORDER,
150617 TK_RESTRICT, TK_OVER, TK_JOIN_KW, TK_ROLLBACK, TK_ROWS,
150618 TK_ROW, TK_UNBOUNDED, TK_UNION, TK_USING, TK_VACUUM,
150619 TK_VIEW, TK_WINDOW, TK_DO, TK_INITIALLY, TK_ALL,
150620 TK_PRIMARY,
150621};
150622/* Check to see if z[0..n-1] is a keyword. If it is, write the
150623** parser symbol code for that keyword into *pType. Always
150624** return the integer n (the length of the token). */
150625static int keywordCode(const char *z, int n, int *pType){
150626 int i, j;
150627 const char *zKW;
150628 if( n>=2 ){
150629 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127;
150630 for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
150631 if( aKWLen[i]!=n ) continue;
150632 j = 0;
150633 zKW = &zKWText[aKWOffset[i]];
150634#ifdef SQLITE_ASCII
150635 while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }
150636#endif
150637#ifdef SQLITE_EBCDIC
150638 while( j<n && toupper(z[j])==zKW[j] ){ j++; }
150639#endif
150640 if( j<n ) continue;
150641 testcase( i==0 ); /* REINDEX */
150642 testcase( i==1 ); /* INDEXED */
150643 testcase( i==2 ); /* INDEX */
150644 testcase( i==3 ); /* DESC */
150645 testcase( i==4 ); /* ESCAPE */
150646 testcase( i==5 ); /* EACH */
150647 testcase( i==6 ); /* CHECK */
150648 testcase( i==7 ); /* KEY */
150649 testcase( i==8 ); /* BEFORE */
150650 testcase( i==9 ); /* FOREIGN */
150651 testcase( i==10 ); /* FOR */
150652 testcase( i==11 ); /* IGNORE */
150653 testcase( i==12 ); /* REGEXP */
150654 testcase( i==13 ); /* EXPLAIN */
150655 testcase( i==14 ); /* INSTEAD */
150656 testcase( i==15 ); /* ADD */
150657 testcase( i==16 ); /* DATABASE */
150658 testcase( i==17 ); /* AS */
150659 testcase( i==18 ); /* SELECT */
150660 testcase( i==19 ); /* TABLE */
150661 testcase( i==20 ); /* LEFT */
150662 testcase( i==21 ); /* THEN */
150663 testcase( i==22 ); /* END */
150664 testcase( i==23 ); /* DEFERRABLE */
150665 testcase( i==24 ); /* ELSE */
150666 testcase( i==25 ); /* EXCEPT */
150667 testcase( i==26 ); /* TRANSACTION */
150668 testcase( i==27 ); /* ACTION */
150669 testcase( i==28 ); /* ON */
150670 testcase( i==29 ); /* NATURAL */
150671 testcase( i==30 ); /* ALTER */
150672 testcase( i==31 ); /* RAISE */
150673 testcase( i==32 ); /* EXCLUSIVE */
150674 testcase( i==33 ); /* EXISTS */
150675 testcase( i==34 ); /* SAVEPOINT */
150676 testcase( i==35 ); /* INTERSECT */
150677 testcase( i==36 ); /* TRIGGER */
150678 testcase( i==37 ); /* REFERENCES */
150679 testcase( i==38 ); /* CONSTRAINT */
150680 testcase( i==39 ); /* INTO */
150681 testcase( i==40 ); /* OFFSET */
150682 testcase( i==41 ); /* OF */
150683 testcase( i==42 ); /* SET */
150684 testcase( i==43 ); /* TEMPORARY */
150685 testcase( i==44 ); /* TEMP */
150686 testcase( i==45 ); /* OR */
150687 testcase( i==46 ); /* UNIQUE */
150688 testcase( i==47 ); /* QUERY */
150689 testcase( i==48 ); /* WITHOUT */
150690 testcase( i==49 ); /* WITH */
150691 testcase( i==50 ); /* OUTER */
150692 testcase( i==51 ); /* RELEASE */
150693 testcase( i==52 ); /* ATTACH */
150694 testcase( i==53 ); /* HAVING */
150695 testcase( i==54 ); /* GROUP */
150696 testcase( i==55 ); /* UPDATE */
150697 testcase( i==56 ); /* BEGIN */
150698 testcase( i==57 ); /* INNER */
150699 testcase( i==58 ); /* RANGE */
150700 testcase( i==59 ); /* BETWEEN */
150701 testcase( i==60 ); /* NOTHING */
150702 testcase( i==61 ); /* GLOB */
150703 testcase( i==62 ); /* BY */
150704 testcase( i==63 ); /* CASCADE */
150705 testcase( i==64 ); /* ASC */
150706 testcase( i==65 ); /* DELETE */
150707 testcase( i==66 ); /* CASE */
150708 testcase( i==67 ); /* COLLATE */
150709 testcase( i==68 ); /* CREATE */
150710 testcase( i==69 ); /* CURRENT_DATE */
150711 testcase( i==70 ); /* DETACH */
150712 testcase( i==71 ); /* IMMEDIATE */
150713 testcase( i==72 ); /* JOIN */
150714 testcase( i==73 ); /* INSERT */
150715 testcase( i==74 ); /* LIKE */
150716 testcase( i==75 ); /* MATCH */
150717 testcase( i==76 ); /* PLAN */
150718 testcase( i==77 ); /* ANALYZE */
150719 testcase( i==78 ); /* PRAGMA */
150720 testcase( i==79 ); /* ABORT */
150721 testcase( i==80 ); /* VALUES */
150722 testcase( i==81 ); /* VIRTUAL */
150723 testcase( i==82 ); /* LIMIT */
150724 testcase( i==83 ); /* WHEN */
150725 testcase( i==84 ); /* NOTNULL */
150726 testcase( i==85 ); /* NOT */
150727 testcase( i==86 ); /* NO */
150728 testcase( i==87 ); /* NULL */
150729 testcase( i==88 ); /* WHERE */
150730 testcase( i==89 ); /* RECURSIVE */
150731 testcase( i==90 ); /* AFTER */
150732 testcase( i==91 ); /* RENAME */
150733 testcase( i==92 ); /* AND */
150734 testcase( i==93 ); /* DEFAULT */
150735 testcase( i==94 ); /* AUTOINCREMENT */
150736 testcase( i==95 ); /* TO */
150737 testcase( i==96 ); /* IN */
150738 testcase( i==97 ); /* CAST */
150739 testcase( i==98 ); /* COLUMN */
150740 testcase( i==99 ); /* COMMIT */
150741 testcase( i==100 ); /* CONFLICT */
150742 testcase( i==101 ); /* CROSS */
150743 testcase( i==102 ); /* CURRENT_TIMESTAMP */
150744 testcase( i==103 ); /* CURRENT_TIME */
150745 testcase( i==104 ); /* CURRENT */
150746 testcase( i==105 ); /* PARTITION */
150747 testcase( i==106 ); /* DEFERRED */
150748 testcase( i==107 ); /* DISTINCT */
150749 testcase( i==108 ); /* IS */
150750 testcase( i==109 ); /* DROP */
150751 testcase( i==110 ); /* PRECEDING */
150752 testcase( i==111 ); /* FAIL */
150753 testcase( i==112 ); /* FILTER */
150754 testcase( i==113 ); /* REPLACE */
150755 testcase( i==114 ); /* FOLLOWING */
150756 testcase( i==115 ); /* FROM */
150757 testcase( i==116 ); /* FULL */
150758 testcase( i==117 ); /* IF */
150759 testcase( i==118 ); /* ISNULL */
150760 testcase( i==119 ); /* ORDER */
150761 testcase( i==120 ); /* RESTRICT */
150762 testcase( i==121 ); /* OVER */
150763 testcase( i==122 ); /* RIGHT */
150764 testcase( i==123 ); /* ROLLBACK */
150765 testcase( i==124 ); /* ROWS */
150766 testcase( i==125 ); /* ROW */
150767 testcase( i==126 ); /* UNBOUNDED */
150768 testcase( i==127 ); /* UNION */
150769 testcase( i==128 ); /* USING */
150770 testcase( i==129 ); /* VACUUM */
150771 testcase( i==130 ); /* VIEW */
150772 testcase( i==131 ); /* WINDOW */
150773 testcase( i==132 ); /* DO */
150774 testcase( i==133 ); /* INITIALLY */
150775 testcase( i==134 ); /* ALL */
150776 testcase( i==135 ); /* PRIMARY */
150777 *pType = aKWCode[i];
150778 break;
150779 }
150780 }
150781 return n;
150782}
150783SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
150784 int id = TK_ID;
150785 keywordCode((char*)z, n, &id);
150786 return id;
150787}
150788#define SQLITE_N_KEYWORD 136
150789SQLITE_API int sqlite3_keyword_name(int i,const char **pzName,int *pnName){
150790 if( i<0 || i>=SQLITE_N_KEYWORD ) return SQLITE_ERROR;
150791 *pzName = zKWText + aKWOffset[i];
150792 *pnName = aKWLen[i];
150793 return SQLITE_OK;
150794}
150795SQLITE_API int sqlite3_keyword_count(void){ return SQLITE_N_KEYWORD; }
150796SQLITE_API int sqlite3_keyword_check(const char *zName, int nName){
150797 return TK_ID!=sqlite3KeywordCode((const u8*)zName, nName);
150798}
150799
150800/************** End of keywordhash.h *****************************************/
150801/************** Continuing where we left off in tokenize.c *******************/
150802
150803
150804/*
150805** If X is a character that can be used in an identifier then
150806** IdChar(X) will be true. Otherwise it is false.
150807**
150808** For ASCII, any character with the high-order bit set is
150809** allowed in an identifier. For 7-bit characters,
150810** sqlite3IsIdChar[X] must be 1.
150811**
150812** For EBCDIC, the rules are more complex but have the same
150813** end result.
150814**
150815** Ticket #1066. the SQL standard does not allow '$' in the
150816** middle of identifiers. But many SQL implementations do.
150817** SQLite will allow '$' in identifiers for compatibility.
150818** But the feature is undocumented.
150819*/
150820#ifdef SQLITE_ASCII
150821#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
150822#endif
150823#ifdef SQLITE_EBCDIC
150824SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
150825/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
150826 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
150827 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
150828 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
150829 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
150830 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
150831 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
150832 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
150833 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
150834 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
150835 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
150836 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
150837 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
150838};
150839#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
150840#endif
150841
150842/* Make the IdChar function accessible from ctime.c and alter.c */
150843SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
150844
150845#ifndef SQLITE_OMIT_WINDOWFUNC
150846/*
150847** Return the id of the next token in string (*pz). Before returning, set
150848** (*pz) to point to the byte following the parsed token.
150849*/
150850static int getToken(const unsigned char **pz){
150851 const unsigned char *z = *pz;
150852 int t; /* Token type to return */
150853 do {
150854 z += sqlite3GetToken(z, &t);
150855 }while( t==TK_SPACE );
150856 if( t==TK_ID
150857 || t==TK_STRING
150858 || t==TK_JOIN_KW
150859 || t==TK_WINDOW
150860 || t==TK_OVER
150861 || sqlite3ParserFallback(t)==TK_ID
150862 ){
150863 t = TK_ID;
150864 }
150865 *pz = z;
150866 return t;
150867}
150868
150869/*
150870** The following three functions are called immediately after the tokenizer
150871** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
150872** whether the token should be treated as a keyword or an SQL identifier.
150873** This cannot be handled by the usual lemon %fallback method, due to
150874** the ambiguity in some constructions. e.g.
150875**
150876** SELECT sum(x) OVER ...
150877**
150878** In the above, "OVER" might be a keyword, or it might be an alias for the
150879** sum(x) expression. If a "%fallback ID OVER" directive were added to
150880** grammar, then SQLite would always treat "OVER" as an alias, making it
150881** impossible to call a window-function without a FILTER clause.
150882**
150883** WINDOW is treated as a keyword if:
150884**
150885** * the following token is an identifier, or a keyword that can fallback
150886** to being an identifier, and
150887** * the token after than one is TK_AS.
150888**
150889** OVER is a keyword if:
150890**
150891** * the previous token was TK_RP, and
150892** * the next token is either TK_LP or an identifier.
150893**
150894** FILTER is a keyword if:
150895**
150896** * the previous token was TK_RP, and
150897** * the next token is TK_LP.
150898*/
150899static int analyzeWindowKeyword(const unsigned char *z){
150900 int t;
150901 t = getToken(&z);
150902 if( t!=TK_ID ) return TK_ID;
150903 t = getToken(&z);
150904 if( t!=TK_AS ) return TK_ID;
150905 return TK_WINDOW;
150906}
150907static int analyzeOverKeyword(const unsigned char *z, int lastToken){
150908 if( lastToken==TK_RP ){
150909 int t = getToken(&z);
150910 if( t==TK_LP || t==TK_ID ) return TK_OVER;
150911 }
150912 return TK_ID;
150913}
150914static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
150915 if( lastToken==TK_RP && getToken(&z)==TK_LP ){
150916 return TK_FILTER;
150917 }
150918 return TK_ID;
150919}
150920#endif /* SQLITE_OMIT_WINDOWFUNC */
150921
150922/*
150923** Return the length (in bytes) of the token that begins at z[0].
150924** Store the token type in *tokenType before returning.
150925*/
150926SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
150927 int i, c;
150928 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
150929 ** of the token. See the comment on the CC_ defines
150930 ** above. */
150931 case CC_SPACE: {
150932 testcase( z[0]==' ' );
150933 testcase( z[0]=='\t' );
150934 testcase( z[0]=='\n' );
150935 testcase( z[0]=='\f' );
150936 testcase( z[0]=='\r' );
150937 for(i=1; sqlite3Isspace(z[i]); i++){}
150938 *tokenType = TK_SPACE;
150939 return i;
150940 }
150941 case CC_MINUS: {
150942 if( z[1]=='-' ){
150943 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
150944 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
150945 return i;
150946 }
150947 *tokenType = TK_MINUS;
150948 return 1;
150949 }
150950 case CC_LP: {
150951 *tokenType = TK_LP;
150952 return 1;
150953 }
150954 case CC_RP: {
150955 *tokenType = TK_RP;
150956 return 1;
150957 }
150958 case CC_SEMI: {
150959 *tokenType = TK_SEMI;
150960 return 1;
150961 }
150962 case CC_PLUS: {
150963 *tokenType = TK_PLUS;
150964 return 1;
150965 }
150966 case CC_STAR: {
150967 *tokenType = TK_STAR;
150968 return 1;
150969 }
150970 case CC_SLASH: {
150971 if( z[1]!='*' || z[2]==0 ){
150972 *tokenType = TK_SLASH;
150973 return 1;
150974 }
150975 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
150976 if( c ) i++;
150977 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
150978 return i;
150979 }
150980 case CC_PERCENT: {
150981 *tokenType = TK_REM;
150982 return 1;
150983 }
150984 case CC_EQ: {
150985 *tokenType = TK_EQ;
150986 return 1 + (z[1]=='=');
150987 }
150988 case CC_LT: {
150989 if( (c=z[1])=='=' ){
150990 *tokenType = TK_LE;
150991 return 2;
150992 }else if( c=='>' ){
150993 *tokenType = TK_NE;
150994 return 2;
150995 }else if( c=='<' ){
150996 *tokenType = TK_LSHIFT;
150997 return 2;
150998 }else{
150999 *tokenType = TK_LT;
151000 return 1;
151001 }
151002 }
151003 case CC_GT: {
151004 if( (c=z[1])=='=' ){
151005 *tokenType = TK_GE;
151006 return 2;
151007 }else if( c=='>' ){
151008 *tokenType = TK_RSHIFT;
151009 return 2;
151010 }else{
151011 *tokenType = TK_GT;
151012 return 1;
151013 }
151014 }
151015 case CC_BANG: {
151016 if( z[1]!='=' ){
151017 *tokenType = TK_ILLEGAL;
151018 return 1;
151019 }else{
151020 *tokenType = TK_NE;
151021 return 2;
151022 }
151023 }
151024 case CC_PIPE: {
151025 if( z[1]!='|' ){
151026 *tokenType = TK_BITOR;
151027 return 1;
151028 }else{
151029 *tokenType = TK_CONCAT;
151030 return 2;
151031 }
151032 }
151033 case CC_COMMA: {
151034 *tokenType = TK_COMMA;
151035 return 1;
151036 }
151037 case CC_AND: {
151038 *tokenType = TK_BITAND;
151039 return 1;
151040 }
151041 case CC_TILDA: {
151042 *tokenType = TK_BITNOT;
151043 return 1;
151044 }
151045 case CC_QUOTE: {
151046 int delim = z[0];
151047 testcase( delim=='`' );
151048 testcase( delim=='\'' );
151049 testcase( delim=='"' );
151050 for(i=1; (c=z[i])!=0; i++){
151051 if( c==delim ){
151052 if( z[i+1]==delim ){
151053 i++;
151054 }else{
151055 break;
151056 }
151057 }
151058 }
151059 if( c=='\'' ){
151060 *tokenType = TK_STRING;
151061 return i+1;
151062 }else if( c!=0 ){
151063 *tokenType = TK_ID;
151064 return i+1;
151065 }else{
151066 *tokenType = TK_ILLEGAL;
151067 return i;
151068 }
151069 }
151070 case CC_DOT: {
151071#ifndef SQLITE_OMIT_FLOATING_POINT
151072 if( !sqlite3Isdigit(z[1]) )
151073#endif
151074 {
151075 *tokenType = TK_DOT;
151076 return 1;
151077 }
151078 /* If the next character is a digit, this is a floating point
151079 ** number that begins with ".". Fall thru into the next case */
151080 }
151081 case CC_DIGIT: {
151082 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
151083 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
151084 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
151085 testcase( z[0]=='9' );
151086 *tokenType = TK_INTEGER;
151087#ifndef SQLITE_OMIT_HEX_INTEGER
151088 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
151089 for(i=3; sqlite3Isxdigit(z[i]); i++){}
151090 return i;
151091 }
151092#endif
151093 for(i=0; sqlite3Isdigit(z[i]); i++){}
151094#ifndef SQLITE_OMIT_FLOATING_POINT
151095 if( z[i]=='.' ){
151096 i++;
151097 while( sqlite3Isdigit(z[i]) ){ i++; }
151098 *tokenType = TK_FLOAT;
151099 }
151100 if( (z[i]=='e' || z[i]=='E') &&
151101 ( sqlite3Isdigit(z[i+1])
151102 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
151103 )
151104 ){
151105 i += 2;
151106 while( sqlite3Isdigit(z[i]) ){ i++; }
151107 *tokenType = TK_FLOAT;
151108 }
151109#endif
151110 while( IdChar(z[i]) ){
151111 *tokenType = TK_ILLEGAL;
151112 i++;
151113 }
151114 return i;
151115 }
151116 case CC_QUOTE2: {
151117 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
151118 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
151119 return i;
151120 }
151121 case CC_VARNUM: {
151122 *tokenType = TK_VARIABLE;
151123 for(i=1; sqlite3Isdigit(z[i]); i++){}
151124 return i;
151125 }
151126 case CC_DOLLAR:
151127 case CC_VARALPHA: {
151128 int n = 0;
151129 testcase( z[0]=='$' ); testcase( z[0]=='@' );
151130 testcase( z[0]==':' ); testcase( z[0]=='#' );
151131 *tokenType = TK_VARIABLE;
151132 for(i=1; (c=z[i])!=0; i++){
151133 if( IdChar(c) ){
151134 n++;
151135#ifndef SQLITE_OMIT_TCL_VARIABLE
151136 }else if( c=='(' && n>0 ){
151137 do{
151138 i++;
151139 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
151140 if( c==')' ){
151141 i++;
151142 }else{
151143 *tokenType = TK_ILLEGAL;
151144 }
151145 break;
151146 }else if( c==':' && z[i+1]==':' ){
151147 i++;
151148#endif
151149 }else{
151150 break;
151151 }
151152 }
151153 if( n==0 ) *tokenType = TK_ILLEGAL;
151154 return i;
151155 }
151156 case CC_KYWD: {
151157 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
151158 if( IdChar(z[i]) ){
151159 /* This token started out using characters that can appear in keywords,
151160 ** but z[i] is a character not allowed within keywords, so this must
151161 ** be an identifier instead */
151162 i++;
151163 break;
151164 }
151165 *tokenType = TK_ID;
151166 return keywordCode((char*)z, i, tokenType);
151167 }
151168 case CC_X: {
151169#ifndef SQLITE_OMIT_BLOB_LITERAL
151170 testcase( z[0]=='x' ); testcase( z[0]=='X' );
151171 if( z[1]=='\'' ){
151172 *tokenType = TK_BLOB;
151173 for(i=2; sqlite3Isxdigit(z[i]); i++){}
151174 if( z[i]!='\'' || i%2 ){
151175 *tokenType = TK_ILLEGAL;
151176 while( z[i] && z[i]!='\'' ){ i++; }
151177 }
151178 if( z[i] ) i++;
151179 return i;
151180 }
151181#endif
151182 /* If it is not a BLOB literal, then it must be an ID, since no
151183 ** SQL keywords start with the letter 'x'. Fall through */
151184 }
151185 case CC_ID: {
151186 i = 1;
151187 break;
151188 }
151189 case CC_NUL: {
151190 *tokenType = TK_ILLEGAL;
151191 return 0;
151192 }
151193 default: {
151194 *tokenType = TK_ILLEGAL;
151195 return 1;
151196 }
151197 }
151198 while( IdChar(z[i]) ){ i++; }
151199 *tokenType = TK_ID;
151200 return i;
151201}
151202
151203/*
151204** Run the parser on the given SQL string. The parser structure is
151205** passed in. An SQLITE_ status code is returned. If an error occurs
151206** then an and attempt is made to write an error message into
151207** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
151208** error message.
151209*/
151210SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
151211 int nErr = 0; /* Number of errors encountered */
151212 void *pEngine; /* The LEMON-generated LALR(1) parser */
151213 int n = 0; /* Length of the next token token */
151214 int tokenType; /* type of the next token */
151215 int lastTokenParsed = -1; /* type of the previous token */
151216 sqlite3 *db = pParse->db; /* The database connection */
151217 int mxSqlLen; /* Max length of an SQL string */
151218#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
151219 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
151220#endif
151221
151222 assert( zSql!=0 );
151223 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
151224 if( db->nVdbeActive==0 ){
151225 db->u1.isInterrupted = 0;
151226 }
151227 pParse->rc = SQLITE_OK;
151228 pParse->zTail = zSql;
151229 assert( pzErrMsg!=0 );
151230 /* sqlite3ParserTrace(stdout, "parser: "); */
151231#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
151232 pEngine = &sEngine;
151233 sqlite3ParserInit(pEngine, pParse);
151234#else
151235 pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
151236 if( pEngine==0 ){
151237 sqlite3OomFault(db);
151238 return SQLITE_NOMEM_BKPT;
151239 }
151240#endif
151241 assert( pParse->pNewTable==0 );
151242 assert( pParse->pNewTrigger==0 );
151243 assert( pParse->nVar==0 );
151244 assert( pParse->pVList==0 );
151245 while( 1 ){
151246 n = sqlite3GetToken((u8*)zSql, &tokenType);
151247 mxSqlLen -= n;
151248 if( mxSqlLen<0 ){
151249 pParse->rc = SQLITE_TOOBIG;
151250 break;
151251 }
151252#ifndef SQLITE_OMIT_WINDOWFUNC
151253 if( tokenType>=TK_WINDOW ){
151254 assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER
151255 || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW
151256 );
151257#else
151258 if( tokenType>=TK_SPACE ){
151259 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
151260#endif /* SQLITE_OMIT_WINDOWFUNC */
151261 if( db->u1.isInterrupted ){
151262 pParse->rc = SQLITE_INTERRUPT;
151263 break;
151264 }
151265 if( tokenType==TK_SPACE ){
151266 zSql += n;
151267 continue;
151268 }
151269 if( zSql[0]==0 ){
151270 /* Upon reaching the end of input, call the parser two more times
151271 ** with tokens TK_SEMI and 0, in that order. */
151272 if( lastTokenParsed==TK_SEMI ){
151273 tokenType = 0;
151274 }else if( lastTokenParsed==0 ){
151275 break;
151276 }else{
151277 tokenType = TK_SEMI;
151278 }
151279 n = 0;
151280#ifndef SQLITE_OMIT_WINDOWFUNC
151281 }else if( tokenType==TK_WINDOW ){
151282 assert( n==6 );
151283 tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
151284 }else if( tokenType==TK_OVER ){
151285 assert( n==4 );
151286 tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed);
151287 }else if( tokenType==TK_FILTER ){
151288 assert( n==6 );
151289 tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed);
151290#endif /* SQLITE_OMIT_WINDOWFUNC */
151291 }else{
151292 sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql);
151293 break;
151294 }
151295 }
151296 pParse->sLastToken.z = zSql;
151297 pParse->sLastToken.n = n;
151298 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
151299 lastTokenParsed = tokenType;
151300 zSql += n;
151301 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
151302 }
151303 assert( nErr==0 );
151304#ifdef YYTRACKMAXSTACKDEPTH
151305 sqlite3_mutex_enter(sqlite3MallocMutex());
151306 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
151307 sqlite3ParserStackPeak(pEngine)
151308 );
151309 sqlite3_mutex_leave(sqlite3MallocMutex());
151310#endif /* YYDEBUG */
151311#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
151312 sqlite3ParserFinalize(pEngine);
151313#else
151314 sqlite3ParserFree(pEngine, sqlite3_free);
151315#endif
151316 if( db->mallocFailed ){
151317 pParse->rc = SQLITE_NOMEM_BKPT;
151318 }
151319 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
151320 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
151321 }
151322 assert( pzErrMsg!=0 );
151323 if( pParse->zErrMsg ){
151324 *pzErrMsg = pParse->zErrMsg;
151325 sqlite3_log(pParse->rc, "%s in \"%s\"",
151326 *pzErrMsg, pParse->zTail);
151327 pParse->zErrMsg = 0;
151328 nErr++;
151329 }
151330 pParse->zTail = zSql;
151331 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
151332 sqlite3VdbeDelete(pParse->pVdbe);
151333 pParse->pVdbe = 0;
151334 }
151335#ifndef SQLITE_OMIT_SHARED_CACHE
151336 if( pParse->nested==0 ){
151337 sqlite3DbFree(db, pParse->aTableLock);
151338 pParse->aTableLock = 0;
151339 pParse->nTableLock = 0;
151340 }
151341#endif
151342#ifndef SQLITE_OMIT_VIRTUALTABLE
151343 sqlite3_free(pParse->apVtabLock);
151344#endif
151345
151346 if( !IN_SPECIAL_PARSE ){
151347 /* If the pParse->declareVtab flag is set, do not delete any table
151348 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
151349 ** will take responsibility for freeing the Table structure.
151350 */
151351 sqlite3DeleteTable(db, pParse->pNewTable);
151352 }
151353 if( !IN_RENAME_OBJECT ){
151354 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
151355 }
151356
151357 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
151358 sqlite3DbFree(db, pParse->pVList);
151359 while( pParse->pAinc ){
151360 AutoincInfo *p = pParse->pAinc;
151361 pParse->pAinc = p->pNext;
151362 sqlite3DbFreeNN(db, p);
151363 }
151364 while( pParse->pZombieTab ){
151365 Table *p = pParse->pZombieTab;
151366 pParse->pZombieTab = p->pNextZombie;
151367 sqlite3DeleteTable(db, p);
151368 }
151369 assert( nErr==0 || pParse->rc!=SQLITE_OK );
151370 return nErr;
151371}
151372
151373/************** End of tokenize.c ********************************************/
151374/************** Begin file complete.c ****************************************/
151375/*
151376** 2001 September 15
151377**
151378** The author disclaims copyright to this source code. In place of
151379** a legal notice, here is a blessing:
151380**
151381** May you do good and not evil.
151382** May you find forgiveness for yourself and forgive others.
151383** May you share freely, never taking more than you give.
151384**
151385*************************************************************************
151386** An tokenizer for SQL
151387**
151388** This file contains C code that implements the sqlite3_complete() API.
151389** This code used to be part of the tokenizer.c source file. But by
151390** separating it out, the code will be automatically omitted from
151391** static links that do not use it.
151392*/
151393/* #include "sqliteInt.h" */
151394#ifndef SQLITE_OMIT_COMPLETE
151395
151396/*
151397** This is defined in tokenize.c. We just have to import the definition.
151398*/
151399#ifndef SQLITE_AMALGAMATION
151400#ifdef SQLITE_ASCII
151401#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
151402#endif
151403#ifdef SQLITE_EBCDIC
151404SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
151405#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
151406#endif
151407#endif /* SQLITE_AMALGAMATION */
151408
151409
151410/*
151411** Token types used by the sqlite3_complete() routine. See the header
151412** comments on that procedure for additional information.
151413*/
151414#define tkSEMI 0
151415#define tkWS 1
151416#define tkOTHER 2
151417#ifndef SQLITE_OMIT_TRIGGER
151418#define tkEXPLAIN 3
151419#define tkCREATE 4
151420#define tkTEMP 5
151421#define tkTRIGGER 6
151422#define tkEND 7
151423#endif
151424
151425/*
151426** Return TRUE if the given SQL string ends in a semicolon.
151427**
151428** Special handling is require for CREATE TRIGGER statements.
151429** Whenever the CREATE TRIGGER keywords are seen, the statement
151430** must end with ";END;".
151431**
151432** This implementation uses a state machine with 8 states:
151433**
151434** (0) INVALID We have not yet seen a non-whitespace character.
151435**
151436** (1) START At the beginning or end of an SQL statement. This routine
151437** returns 1 if it ends in the START state and 0 if it ends
151438** in any other state.
151439**
151440** (2) NORMAL We are in the middle of statement which ends with a single
151441** semicolon.
151442**
151443** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
151444** a statement.
151445**
151446** (4) CREATE The keyword CREATE has been seen at the beginning of a
151447** statement, possibly preceded by EXPLAIN and/or followed by
151448** TEMP or TEMPORARY
151449**
151450** (5) TRIGGER We are in the middle of a trigger definition that must be
151451** ended by a semicolon, the keyword END, and another semicolon.
151452**
151453** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at
151454** the end of a trigger definition.
151455**
151456** (7) END We've seen the ";END" of the ";END;" that occurs at the end
151457** of a trigger definition.
151458**
151459** Transitions between states above are determined by tokens extracted
151460** from the input. The following tokens are significant:
151461**
151462** (0) tkSEMI A semicolon.
151463** (1) tkWS Whitespace.
151464** (2) tkOTHER Any other SQL token.
151465** (3) tkEXPLAIN The "explain" keyword.
151466** (4) tkCREATE The "create" keyword.
151467** (5) tkTEMP The "temp" or "temporary" keyword.
151468** (6) tkTRIGGER The "trigger" keyword.
151469** (7) tkEND The "end" keyword.
151470**
151471** Whitespace never causes a state transition and is always ignored.
151472** This means that a SQL string of all whitespace is invalid.
151473**
151474** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
151475** to recognize the end of a trigger can be omitted. All we have to do
151476** is look for a semicolon that is not part of an string or comment.
151477*/
151478SQLITE_API int sqlite3_complete(const char *zSql){
151479 u8 state = 0; /* Current state, using numbers defined in header comment */
151480 u8 token; /* Value of the next token */
151481
151482#ifndef SQLITE_OMIT_TRIGGER
151483 /* A complex statement machine used to detect the end of a CREATE TRIGGER
151484 ** statement. This is the normal case.
151485 */
151486 static const u8 trans[8][8] = {
151487 /* Token: */
151488 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
151489 /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, },
151490 /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, },
151491 /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, },
151492 /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, },
151493 /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
151494 /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
151495 /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
151496 /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
151497 };
151498#else
151499 /* If triggers are not supported by this compile then the statement machine
151500 ** used to detect the end of a statement is much simpler
151501 */
151502 static const u8 trans[3][3] = {
151503 /* Token: */
151504 /* State: ** SEMI WS OTHER */
151505 /* 0 INVALID: */ { 1, 0, 2, },
151506 /* 1 START: */ { 1, 1, 2, },
151507 /* 2 NORMAL: */ { 1, 2, 2, },
151508 };
151509#endif /* SQLITE_OMIT_TRIGGER */
151510
151511#ifdef SQLITE_ENABLE_API_ARMOR
151512 if( zSql==0 ){
151513 (void)SQLITE_MISUSE_BKPT;
151514 return 0;
151515 }
151516#endif
151517
151518 while( *zSql ){
151519 switch( *zSql ){
151520 case ';': { /* A semicolon */
151521 token = tkSEMI;
151522 break;
151523 }
151524 case ' ':
151525 case '\r':
151526 case '\t':
151527 case '\n':
151528 case '\f': { /* White space is ignored */
151529 token = tkWS;
151530 break;
151531 }
151532 case '/': { /* C-style comments */
151533 if( zSql[1]!='*' ){
151534 token = tkOTHER;
151535 break;
151536 }
151537 zSql += 2;
151538 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
151539 if( zSql[0]==0 ) return 0;
151540 zSql++;
151541 token = tkWS;
151542 break;
151543 }
151544 case '-': { /* SQL-style comments from "--" to end of line */
151545 if( zSql[1]!='-' ){
151546 token = tkOTHER;
151547 break;
151548 }
151549 while( *zSql && *zSql!='\n' ){ zSql++; }
151550 if( *zSql==0 ) return state==1;
151551 token = tkWS;
151552 break;
151553 }
151554 case '[': { /* Microsoft-style identifiers in [...] */
151555 zSql++;
151556 while( *zSql && *zSql!=']' ){ zSql++; }
151557 if( *zSql==0 ) return 0;
151558 token = tkOTHER;
151559 break;
151560 }
151561 case '`': /* Grave-accent quoted symbols used by MySQL */
151562 case '"': /* single- and double-quoted strings */
151563 case '\'': {
151564 int c = *zSql;
151565 zSql++;
151566 while( *zSql && *zSql!=c ){ zSql++; }
151567 if( *zSql==0 ) return 0;
151568 token = tkOTHER;
151569 break;
151570 }
151571 default: {
151572#ifdef SQLITE_EBCDIC
151573 unsigned char c;
151574#endif
151575 if( IdChar((u8)*zSql) ){
151576 /* Keywords and unquoted identifiers */
151577 int nId;
151578 for(nId=1; IdChar(zSql[nId]); nId++){}
151579#ifdef SQLITE_OMIT_TRIGGER
151580 token = tkOTHER;
151581#else
151582 switch( *zSql ){
151583 case 'c': case 'C': {
151584 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
151585 token = tkCREATE;
151586 }else{
151587 token = tkOTHER;
151588 }
151589 break;
151590 }
151591 case 't': case 'T': {
151592 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
151593 token = tkTRIGGER;
151594 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
151595 token = tkTEMP;
151596 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
151597 token = tkTEMP;
151598 }else{
151599 token = tkOTHER;
151600 }
151601 break;
151602 }
151603 case 'e': case 'E': {
151604 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
151605 token = tkEND;
151606 }else
151607#ifndef SQLITE_OMIT_EXPLAIN
151608 if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
151609 token = tkEXPLAIN;
151610 }else
151611#endif
151612 {
151613 token = tkOTHER;
151614 }
151615 break;
151616 }
151617 default: {
151618 token = tkOTHER;
151619 break;
151620 }
151621 }
151622#endif /* SQLITE_OMIT_TRIGGER */
151623 zSql += nId-1;
151624 }else{
151625 /* Operators and special symbols */
151626 token = tkOTHER;
151627 }
151628 break;
151629 }
151630 }
151631 state = trans[state][token];
151632 zSql++;
151633 }
151634 return state==1;
151635}
151636
151637#ifndef SQLITE_OMIT_UTF16
151638/*
151639** This routine is the same as the sqlite3_complete() routine described
151640** above, except that the parameter is required to be UTF-16 encoded, not
151641** UTF-8.
151642*/
151643SQLITE_API int sqlite3_complete16(const void *zSql){
151644 sqlite3_value *pVal;
151645 char const *zSql8;
151646 int rc;
151647
151648#ifndef SQLITE_OMIT_AUTOINIT
151649 rc = sqlite3_initialize();
151650 if( rc ) return rc;
151651#endif
151652 pVal = sqlite3ValueNew(0);
151653 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
151654 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
151655 if( zSql8 ){
151656 rc = sqlite3_complete(zSql8);
151657 }else{
151658 rc = SQLITE_NOMEM_BKPT;
151659 }
151660 sqlite3ValueFree(pVal);
151661 return rc & 0xff;
151662}
151663#endif /* SQLITE_OMIT_UTF16 */
151664#endif /* SQLITE_OMIT_COMPLETE */
151665
151666/************** End of complete.c ********************************************/
151667/************** Begin file main.c ********************************************/
151668/*
151669** 2001 September 15
151670**
151671** The author disclaims copyright to this source code. In place of
151672** a legal notice, here is a blessing:
151673**
151674** May you do good and not evil.
151675** May you find forgiveness for yourself and forgive others.
151676** May you share freely, never taking more than you give.
151677**
151678*************************************************************************
151679** Main file for the SQLite library. The routines in this file
151680** implement the programmer interface to the library. Routines in
151681** other files are for internal use by SQLite and should not be
151682** accessed by users of the library.
151683*/
151684/* #include "sqliteInt.h" */
151685
151686#ifdef SQLITE_ENABLE_FTS3
151687/************** Include fts3.h in the middle of main.c ***********************/
151688/************** Begin file fts3.h ********************************************/
151689/*
151690** 2006 Oct 10
151691**
151692** The author disclaims copyright to this source code. In place of
151693** a legal notice, here is a blessing:
151694**
151695** May you do good and not evil.
151696** May you find forgiveness for yourself and forgive others.
151697** May you share freely, never taking more than you give.
151698**
151699******************************************************************************
151700**
151701** This header file is used by programs that want to link against the
151702** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
151703*/
151704/* #include "sqlite3.h" */
151705
151706#if 0
151707extern "C" {
151708#endif /* __cplusplus */
151709
151710SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
151711
151712#if 0
151713} /* extern "C" */
151714#endif /* __cplusplus */
151715
151716/************** End of fts3.h ************************************************/
151717/************** Continuing where we left off in main.c ***********************/
151718#endif
151719#ifdef SQLITE_ENABLE_RTREE
151720/************** Include rtree.h in the middle of main.c **********************/
151721/************** Begin file rtree.h *******************************************/
151722/*
151723** 2008 May 26
151724**
151725** The author disclaims copyright to this source code. In place of
151726** a legal notice, here is a blessing:
151727**
151728** May you do good and not evil.
151729** May you find forgiveness for yourself and forgive others.
151730** May you share freely, never taking more than you give.
151731**
151732******************************************************************************
151733**
151734** This header file is used by programs that want to link against the
151735** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
151736*/
151737/* #include "sqlite3.h" */
151738
151739#ifdef SQLITE_OMIT_VIRTUALTABLE
151740# undef SQLITE_ENABLE_RTREE
151741#endif
151742
151743#if 0
151744extern "C" {
151745#endif /* __cplusplus */
151746
151747SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
151748
151749#if 0
151750} /* extern "C" */
151751#endif /* __cplusplus */
151752
151753/************** End of rtree.h ***********************************************/
151754/************** Continuing where we left off in main.c ***********************/
151755#endif
151756#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
151757/************** Include sqliteicu.h in the middle of main.c ******************/
151758/************** Begin file sqliteicu.h ***************************************/
151759/*
151760** 2008 May 26
151761**
151762** The author disclaims copyright to this source code. In place of
151763** a legal notice, here is a blessing:
151764**
151765** May you do good and not evil.
151766** May you find forgiveness for yourself and forgive others.
151767** May you share freely, never taking more than you give.
151768**
151769******************************************************************************
151770**
151771** This header file is used by programs that want to link against the
151772** ICU extension. All it does is declare the sqlite3IcuInit() interface.
151773*/
151774/* #include "sqlite3.h" */
151775
151776#if 0
151777extern "C" {
151778#endif /* __cplusplus */
151779
151780SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
151781
151782#if 0
151783} /* extern "C" */
151784#endif /* __cplusplus */
151785
151786
151787/************** End of sqliteicu.h *******************************************/
151788/************** Continuing where we left off in main.c ***********************/
151789#endif
151790#ifdef SQLITE_ENABLE_JSON1
151791SQLITE_PRIVATE int sqlite3Json1Init(sqlite3*);
151792#endif
151793#ifdef SQLITE_ENABLE_STMTVTAB
151794SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3*);
151795#endif
151796#ifdef SQLITE_ENABLE_FTS5
151797SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*);
151798#endif
151799
151800#ifndef SQLITE_AMALGAMATION
151801/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
151802** contains the text of SQLITE_VERSION macro.
151803*/
151804SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
151805#endif
151806
151807/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
151808** a pointer to the to the sqlite3_version[] string constant.
151809*/
151810SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
151811
151812/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
151813** pointer to a string constant whose value is the same as the
151814** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using
151815** an edited copy of the amalgamation, then the last four characters of
151816** the hash might be different from SQLITE_SOURCE_ID.
151817*/
151818/* SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } */
151819
151820/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
151821** returns an integer equal to SQLITE_VERSION_NUMBER.
151822*/
151823SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
151824
151825/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
151826** zero if and only if SQLite was compiled with mutexing code omitted due to
151827** the SQLITE_THREADSAFE compile-time option being set to 0.
151828*/
151829SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
151830
151831/*
151832** When compiling the test fixture or with debugging enabled (on Win32),
151833** this variable being set to non-zero will cause OSTRACE macros to emit
151834** extra diagnostic information.
151835*/
151836#ifdef SQLITE_HAVE_OS_TRACE
151837# ifndef SQLITE_DEBUG_OS_TRACE
151838# define SQLITE_DEBUG_OS_TRACE 0
151839# endif
151840 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
151841#endif
151842
151843#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
151844/*
151845** If the following function pointer is not NULL and if
151846** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
151847** I/O active are written using this function. These messages
151848** are intended for debugging activity only.
151849*/
151850SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
151851#endif
151852
151853/*
151854** If the following global variable points to a string which is the
151855** name of a directory, then that directory will be used to store
151856** temporary files.
151857**
151858** See also the "PRAGMA temp_store_directory" SQL command.
151859*/
151860SQLITE_API char *sqlite3_temp_directory = 0;
151861
151862/*
151863** If the following global variable points to a string which is the
151864** name of a directory, then that directory will be used to store
151865** all database files specified with a relative pathname.
151866**
151867** See also the "PRAGMA data_store_directory" SQL command.
151868*/
151869SQLITE_API char *sqlite3_data_directory = 0;
151870
151871/*
151872** Initialize SQLite.
151873**
151874** This routine must be called to initialize the memory allocation,
151875** VFS, and mutex subsystems prior to doing any serious work with
151876** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
151877** this routine will be called automatically by key routines such as
151878** sqlite3_open().
151879**
151880** This routine is a no-op except on its very first call for the process,
151881** or for the first call after a call to sqlite3_shutdown.
151882**
151883** The first thread to call this routine runs the initialization to
151884** completion. If subsequent threads call this routine before the first
151885** thread has finished the initialization process, then the subsequent
151886** threads must block until the first thread finishes with the initialization.
151887**
151888** The first thread might call this routine recursively. Recursive
151889** calls to this routine should not block, of course. Otherwise the
151890** initialization process would never complete.
151891**
151892** Let X be the first thread to enter this routine. Let Y be some other
151893** thread. Then while the initial invocation of this routine by X is
151894** incomplete, it is required that:
151895**
151896** * Calls to this routine from Y must block until the outer-most
151897** call by X completes.
151898**
151899** * Recursive calls to this routine from thread X return immediately
151900** without blocking.
151901*/
151902SQLITE_API int sqlite3_initialize(void){
151903 MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
151904 int rc; /* Result code */
151905#ifdef SQLITE_EXTRA_INIT
151906 int bRunExtraInit = 0; /* Extra initialization needed */
151907#endif
151908
151909#ifdef SQLITE_OMIT_WSD
151910 rc = sqlite3_wsd_init(4096, 24);
151911 if( rc!=SQLITE_OK ){
151912 return rc;
151913 }
151914#endif
151915
151916 /* If the following assert() fails on some obscure processor/compiler
151917 ** combination, the work-around is to set the correct pointer
151918 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
151919 assert( SQLITE_PTRSIZE==sizeof(char*) );
151920
151921 /* If SQLite is already completely initialized, then this call
151922 ** to sqlite3_initialize() should be a no-op. But the initialization
151923 ** must be complete. So isInit must not be set until the very end
151924 ** of this routine.
151925 */
151926 if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
151927
151928 /* Make sure the mutex subsystem is initialized. If unable to
151929 ** initialize the mutex subsystem, return early with the error.
151930 ** If the system is so sick that we are unable to allocate a mutex,
151931 ** there is not much SQLite is going to be able to do.
151932 **
151933 ** The mutex subsystem must take care of serializing its own
151934 ** initialization.
151935 */
151936 rc = sqlite3MutexInit();
151937 if( rc ) return rc;
151938
151939 /* Initialize the malloc() system and the recursive pInitMutex mutex.
151940 ** This operation is protected by the STATIC_MASTER mutex. Note that
151941 ** MutexAlloc() is called for a static mutex prior to initializing the
151942 ** malloc subsystem - this implies that the allocation of a static
151943 ** mutex must not require support from the malloc subsystem.
151944 */
151945 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
151946 sqlite3_mutex_enter(pMaster);
151947 sqlite3GlobalConfig.isMutexInit = 1;
151948 if( !sqlite3GlobalConfig.isMallocInit ){
151949 rc = sqlite3MallocInit();
151950 }
151951 if( rc==SQLITE_OK ){
151952 sqlite3GlobalConfig.isMallocInit = 1;
151953 if( !sqlite3GlobalConfig.pInitMutex ){
151954 sqlite3GlobalConfig.pInitMutex =
151955 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
151956 if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
151957 rc = SQLITE_NOMEM_BKPT;
151958 }
151959 }
151960 }
151961 if( rc==SQLITE_OK ){
151962 sqlite3GlobalConfig.nRefInitMutex++;
151963 }
151964 sqlite3_mutex_leave(pMaster);
151965
151966 /* If rc is not SQLITE_OK at this point, then either the malloc
151967 ** subsystem could not be initialized or the system failed to allocate
151968 ** the pInitMutex mutex. Return an error in either case. */
151969 if( rc!=SQLITE_OK ){
151970 return rc;
151971 }
151972
151973 /* Do the rest of the initialization under the recursive mutex so
151974 ** that we will be able to handle recursive calls into
151975 ** sqlite3_initialize(). The recursive calls normally come through
151976 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
151977 ** recursive calls might also be possible.
151978 **
151979 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
151980 ** to the xInit method, so the xInit method need not be threadsafe.
151981 **
151982 ** The following mutex is what serializes access to the appdef pcache xInit
151983 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
151984 ** call to sqlite3PcacheInitialize().
151985 */
151986 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
151987 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
151988 sqlite3GlobalConfig.inProgress = 1;
151989#ifdef SQLITE_ENABLE_SQLLOG
151990 {
151991 extern void sqlite3_init_sqllog(void);
151992 sqlite3_init_sqllog();
151993 }
151994#endif
151995 memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions));
151996 sqlite3RegisterBuiltinFunctions();
151997 if( sqlite3GlobalConfig.isPCacheInit==0 ){
151998 rc = sqlite3PcacheInitialize();
151999 }
152000 if( rc==SQLITE_OK ){
152001 sqlite3GlobalConfig.isPCacheInit = 1;
152002 rc = sqlite3OsInit();
152003 }
152004#ifdef SQLITE_ENABLE_DESERIALIZE
152005 if( rc==SQLITE_OK ){
152006 rc = sqlite3MemdbInit();
152007 }
152008#endif
152009 if( rc==SQLITE_OK ){
152010 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
152011 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
152012 sqlite3GlobalConfig.isInit = 1;
152013#ifdef SQLITE_EXTRA_INIT
152014 bRunExtraInit = 1;
152015#endif
152016 }
152017 sqlite3GlobalConfig.inProgress = 0;
152018 }
152019 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
152020
152021 /* Go back under the static mutex and clean up the recursive
152022 ** mutex to prevent a resource leak.
152023 */
152024 sqlite3_mutex_enter(pMaster);
152025 sqlite3GlobalConfig.nRefInitMutex--;
152026 if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
152027 assert( sqlite3GlobalConfig.nRefInitMutex==0 );
152028 sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
152029 sqlite3GlobalConfig.pInitMutex = 0;
152030 }
152031 sqlite3_mutex_leave(pMaster);
152032
152033 /* The following is just a sanity check to make sure SQLite has
152034 ** been compiled correctly. It is important to run this code, but
152035 ** we don't want to run it too often and soak up CPU cycles for no
152036 ** reason. So we run it once during initialization.
152037 */
152038#ifndef NDEBUG
152039#ifndef SQLITE_OMIT_FLOATING_POINT
152040 /* This section of code's only "output" is via assert() statements. */
152041 if( rc==SQLITE_OK ){
152042 u64 x = (((u64)1)<<63)-1;
152043 double y;
152044 assert(sizeof(x)==8);
152045 assert(sizeof(x)==sizeof(y));
152046 memcpy(&y, &x, 8);
152047 assert( sqlite3IsNaN(y) );
152048 }
152049#endif
152050#endif
152051
152052 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
152053 ** compile-time option.
152054 */
152055#ifdef SQLITE_EXTRA_INIT
152056 if( bRunExtraInit ){
152057 int SQLITE_EXTRA_INIT(const char*);
152058 rc = SQLITE_EXTRA_INIT(0);
152059 }
152060#endif
152061
152062 return rc;
152063}
152064
152065/*
152066** Undo the effects of sqlite3_initialize(). Must not be called while
152067** there are outstanding database connections or memory allocations or
152068** while any part of SQLite is otherwise in use in any thread. This
152069** routine is not threadsafe. But it is safe to invoke this routine
152070** on when SQLite is already shut down. If SQLite is already shut down
152071** when this routine is invoked, then this routine is a harmless no-op.
152072*/
152073SQLITE_API int sqlite3_shutdown(void){
152074#ifdef SQLITE_OMIT_WSD
152075 int rc = sqlite3_wsd_init(4096, 24);
152076 if( rc!=SQLITE_OK ){
152077 return rc;
152078 }
152079#endif
152080
152081 if( sqlite3GlobalConfig.isInit ){
152082#ifdef SQLITE_EXTRA_SHUTDOWN
152083 void SQLITE_EXTRA_SHUTDOWN(void);
152084 SQLITE_EXTRA_SHUTDOWN();
152085#endif
152086 sqlite3_os_end();
152087 sqlite3_reset_auto_extension();
152088 sqlite3GlobalConfig.isInit = 0;
152089 }
152090 if( sqlite3GlobalConfig.isPCacheInit ){
152091 sqlite3PcacheShutdown();
152092 sqlite3GlobalConfig.isPCacheInit = 0;
152093 }
152094 if( sqlite3GlobalConfig.isMallocInit ){
152095 sqlite3MallocEnd();
152096 sqlite3GlobalConfig.isMallocInit = 0;
152097
152098#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
152099 /* The heap subsystem has now been shutdown and these values are supposed
152100 ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
152101 ** which would rely on that heap subsystem; therefore, make sure these
152102 ** values cannot refer to heap memory that was just invalidated when the
152103 ** heap subsystem was shutdown. This is only done if the current call to
152104 ** this function resulted in the heap subsystem actually being shutdown.
152105 */
152106 sqlite3_data_directory = 0;
152107 sqlite3_temp_directory = 0;
152108#endif
152109 }
152110 if( sqlite3GlobalConfig.isMutexInit ){
152111 sqlite3MutexEnd();
152112 sqlite3GlobalConfig.isMutexInit = 0;
152113 }
152114
152115 return SQLITE_OK;
152116}
152117
152118/*
152119** This API allows applications to modify the global configuration of
152120** the SQLite library at run-time.
152121**
152122** This routine should only be called when there are no outstanding
152123** database connections or memory allocations. This routine is not
152124** threadsafe. Failure to heed these warnings can lead to unpredictable
152125** behavior.
152126*/
152127SQLITE_API int sqlite3_config(int op, ...){
152128 va_list ap;
152129 int rc = SQLITE_OK;
152130
152131 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
152132 ** the SQLite library is in use. */
152133 if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
152134
152135 va_start(ap, op);
152136 switch( op ){
152137
152138 /* Mutex configuration options are only available in a threadsafe
152139 ** compile.
152140 */
152141#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
152142 case SQLITE_CONFIG_SINGLETHREAD: {
152143 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
152144 ** Single-thread. */
152145 sqlite3GlobalConfig.bCoreMutex = 0; /* Disable mutex on core */
152146 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
152147 break;
152148 }
152149#endif
152150#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
152151 case SQLITE_CONFIG_MULTITHREAD: {
152152 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
152153 ** Multi-thread. */
152154 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
152155 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
152156 break;
152157 }
152158#endif
152159#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
152160 case SQLITE_CONFIG_SERIALIZED: {
152161 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
152162 ** Serialized. */
152163 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
152164 sqlite3GlobalConfig.bFullMutex = 1; /* Enable mutex on connections */
152165 break;
152166 }
152167#endif
152168#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
152169 case SQLITE_CONFIG_MUTEX: {
152170 /* Specify an alternative mutex implementation */
152171 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
152172 break;
152173 }
152174#endif
152175#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
152176 case SQLITE_CONFIG_GETMUTEX: {
152177 /* Retrieve the current mutex implementation */
152178 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
152179 break;
152180 }
152181#endif
152182
152183 case SQLITE_CONFIG_MALLOC: {
152184 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
152185 ** single argument which is a pointer to an instance of the
152186 ** sqlite3_mem_methods structure. The argument specifies alternative
152187 ** low-level memory allocation routines to be used in place of the memory
152188 ** allocation routines built into SQLite. */
152189 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
152190 break;
152191 }
152192 case SQLITE_CONFIG_GETMALLOC: {
152193 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
152194 ** single argument which is a pointer to an instance of the
152195 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
152196 ** filled with the currently defined memory allocation routines. */
152197 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
152198 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
152199 break;
152200 }
152201 case SQLITE_CONFIG_MEMSTATUS: {
152202 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
152203 ** single argument of type int, interpreted as a boolean, which enables
152204 ** or disables the collection of memory allocation statistics. */
152205 sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
152206 break;
152207 }
152208 case SQLITE_CONFIG_SMALL_MALLOC: {
152209 sqlite3GlobalConfig.bSmallMalloc = va_arg(ap, int);
152210 break;
152211 }
152212 case SQLITE_CONFIG_PAGECACHE: {
152213 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
152214 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
152215 ** the size of each page cache line (sz), and the number of cache lines
152216 ** (N). */
152217 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
152218 sqlite3GlobalConfig.szPage = va_arg(ap, int);
152219 sqlite3GlobalConfig.nPage = va_arg(ap, int);
152220 break;
152221 }
152222 case SQLITE_CONFIG_PCACHE_HDRSZ: {
152223 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
152224 ** a single parameter which is a pointer to an integer and writes into
152225 ** that integer the number of extra bytes per page required for each page
152226 ** in SQLITE_CONFIG_PAGECACHE. */
152227 *va_arg(ap, int*) =
152228 sqlite3HeaderSizeBtree() +
152229 sqlite3HeaderSizePcache() +
152230 sqlite3HeaderSizePcache1();
152231 break;
152232 }
152233
152234 case SQLITE_CONFIG_PCACHE: {
152235 /* no-op */
152236 break;
152237 }
152238 case SQLITE_CONFIG_GETPCACHE: {
152239 /* now an error */
152240 rc = SQLITE_ERROR;
152241 break;
152242 }
152243
152244 case SQLITE_CONFIG_PCACHE2: {
152245 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
152246 ** single argument which is a pointer to an sqlite3_pcache_methods2
152247 ** object. This object specifies the interface to a custom page cache
152248 ** implementation. */
152249 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
152250 break;
152251 }
152252 case SQLITE_CONFIG_GETPCACHE2: {
152253 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
152254 ** single argument which is a pointer to an sqlite3_pcache_methods2
152255 ** object. SQLite copies of the current page cache implementation into
152256 ** that object. */
152257 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
152258 sqlite3PCacheSetDefault();
152259 }
152260 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
152261 break;
152262 }
152263
152264/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
152265** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
152266** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
152267#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
152268 case SQLITE_CONFIG_HEAP: {
152269 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
152270 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
152271 ** number of bytes in the memory buffer, and the minimum allocation size.
152272 */
152273 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
152274 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
152275 sqlite3GlobalConfig.mnReq = va_arg(ap, int);
152276
152277 if( sqlite3GlobalConfig.mnReq<1 ){
152278 sqlite3GlobalConfig.mnReq = 1;
152279 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
152280 /* cap min request size at 2^12 */
152281 sqlite3GlobalConfig.mnReq = (1<<12);
152282 }
152283
152284 if( sqlite3GlobalConfig.pHeap==0 ){
152285 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
152286 ** is NULL, then SQLite reverts to using its default memory allocator
152287 ** (the system malloc() implementation), undoing any prior invocation of
152288 ** SQLITE_CONFIG_MALLOC.
152289 **
152290 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
152291 ** revert to its default implementation when sqlite3_initialize() is run
152292 */
152293 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
152294 }else{
152295 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
152296 ** alternative memory allocator is engaged to handle all of SQLites
152297 ** memory allocation needs. */
152298#ifdef SQLITE_ENABLE_MEMSYS3
152299 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
152300#endif
152301#ifdef SQLITE_ENABLE_MEMSYS5
152302 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
152303#endif
152304 }
152305 break;
152306 }
152307#endif
152308
152309 case SQLITE_CONFIG_LOOKASIDE: {
152310 sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
152311 sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
152312 break;
152313 }
152314
152315 /* Record a pointer to the logger function and its first argument.
152316 ** The default is NULL. Logging is disabled if the function pointer is
152317 ** NULL.
152318 */
152319 case SQLITE_CONFIG_LOG: {
152320 /* MSVC is picky about pulling func ptrs from va lists.
152321 ** http://support.microsoft.com/kb/47961
152322 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
152323 */
152324 typedef void(*LOGFUNC_t)(void*,int,const char*);
152325 sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
152326 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
152327 break;
152328 }
152329
152330 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
152331 ** can be changed at start-time using the
152332 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
152333 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
152334 */
152335 case SQLITE_CONFIG_URI: {
152336 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
152337 ** argument of type int. If non-zero, then URI handling is globally
152338 ** enabled. If the parameter is zero, then URI handling is globally
152339 ** disabled. */
152340 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
152341 break;
152342 }
152343
152344 case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
152345 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
152346 ** option takes a single integer argument which is interpreted as a
152347 ** boolean in order to enable or disable the use of covering indices for
152348 ** full table scans in the query optimizer. */
152349 sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
152350 break;
152351 }
152352
152353#ifdef SQLITE_ENABLE_SQLLOG
152354 case SQLITE_CONFIG_SQLLOG: {
152355 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
152356 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
152357 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
152358 break;
152359 }
152360#endif
152361
152362 case SQLITE_CONFIG_MMAP_SIZE: {
152363 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
152364 ** integer (sqlite3_int64) values that are the default mmap size limit
152365 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
152366 ** mmap size limit. */
152367 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
152368 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
152369 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
152370 ** negative, then that argument is changed to its compile-time default.
152371 **
152372 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
152373 ** silently truncated if necessary so that it does not exceed the
152374 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
152375 ** compile-time option.
152376 */
152377 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
152378 mxMmap = SQLITE_MAX_MMAP_SIZE;
152379 }
152380 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
152381 if( szMmap>mxMmap) szMmap = mxMmap;
152382 sqlite3GlobalConfig.mxMmap = mxMmap;
152383 sqlite3GlobalConfig.szMmap = szMmap;
152384 break;
152385 }
152386
152387#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
152388 case SQLITE_CONFIG_WIN32_HEAPSIZE: {
152389 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
152390 ** unsigned integer value that specifies the maximum size of the created
152391 ** heap. */
152392 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
152393 break;
152394 }
152395#endif
152396
152397 case SQLITE_CONFIG_PMASZ: {
152398 sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int);
152399 break;
152400 }
152401
152402 case SQLITE_CONFIG_STMTJRNL_SPILL: {
152403 sqlite3GlobalConfig.nStmtSpill = va_arg(ap, int);
152404 break;
152405 }
152406
152407#ifdef SQLITE_ENABLE_SORTER_REFERENCES
152408 case SQLITE_CONFIG_SORTERREF_SIZE: {
152409 int iVal = va_arg(ap, int);
152410 if( iVal<0 ){
152411 iVal = SQLITE_DEFAULT_SORTERREF_SIZE;
152412 }
152413 sqlite3GlobalConfig.szSorterRef = (u32)iVal;
152414 break;
152415 }
152416#endif /* SQLITE_ENABLE_SORTER_REFERENCES */
152417
152418 default: {
152419 rc = SQLITE_ERROR;
152420 break;
152421 }
152422 }
152423 va_end(ap);
152424 return rc;
152425}
152426
152427/*
152428** Set up the lookaside buffers for a database connection.
152429** Return SQLITE_OK on success.
152430** If lookaside is already active, return SQLITE_BUSY.
152431**
152432** The sz parameter is the number of bytes in each lookaside slot.
152433** The cnt parameter is the number of slots. If pStart is NULL the
152434** space for the lookaside memory is obtained from sqlite3_malloc().
152435** If pStart is not NULL then it is sz*cnt bytes of memory to use for
152436** the lookaside memory.
152437*/
152438static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
152439#ifndef SQLITE_OMIT_LOOKASIDE
152440 void *pStart;
152441
152442 if( sqlite3LookasideUsed(db,0)>0 ){
152443 return SQLITE_BUSY;
152444 }
152445 /* Free any existing lookaside buffer for this handle before
152446 ** allocating a new one so we don't have to have space for
152447 ** both at the same time.
152448 */
152449 if( db->lookaside.bMalloced ){
152450 sqlite3_free(db->lookaside.pStart);
152451 }
152452 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
152453 ** than a pointer to be useful.
152454 */
152455 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
152456 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
152457 if( cnt<0 ) cnt = 0;
152458 if( sz==0 || cnt==0 ){
152459 sz = 0;
152460 pStart = 0;
152461 }else if( pBuf==0 ){
152462 sqlite3BeginBenignMalloc();
152463 pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */
152464 sqlite3EndBenignMalloc();
152465 if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
152466 }else{
152467 pStart = pBuf;
152468 }
152469 db->lookaside.pStart = pStart;
152470 db->lookaside.pInit = 0;
152471 db->lookaside.pFree = 0;
152472 db->lookaside.sz = (u16)sz;
152473 if( pStart ){
152474 int i;
152475 LookasideSlot *p;
152476 assert( sz > (int)sizeof(LookasideSlot*) );
152477 db->lookaside.nSlot = cnt;
152478 p = (LookasideSlot*)pStart;
152479 for(i=cnt-1; i>=0; i--){
152480 p->pNext = db->lookaside.pInit;
152481 db->lookaside.pInit = p;
152482 p = (LookasideSlot*)&((u8*)p)[sz];
152483 }
152484 db->lookaside.pEnd = p;
152485 db->lookaside.bDisable = 0;
152486 db->lookaside.bMalloced = pBuf==0 ?1:0;
152487 }else{
152488 db->lookaside.pStart = db;
152489 db->lookaside.pEnd = db;
152490 db->lookaside.bDisable = 1;
152491 db->lookaside.bMalloced = 0;
152492 db->lookaside.nSlot = 0;
152493 }
152494#endif /* SQLITE_OMIT_LOOKASIDE */
152495 return SQLITE_OK;
152496}
152497
152498/*
152499** Return the mutex associated with a database connection.
152500*/
152501SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
152502#ifdef SQLITE_ENABLE_API_ARMOR
152503 if( !sqlite3SafetyCheckOk(db) ){
152504 (void)SQLITE_MISUSE_BKPT;
152505 return 0;
152506 }
152507#endif
152508 return db->mutex;
152509}
152510
152511/*
152512** Free up as much memory as we can from the given database
152513** connection.
152514*/
152515SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
152516 int i;
152517
152518#ifdef SQLITE_ENABLE_API_ARMOR
152519 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
152520#endif
152521 sqlite3_mutex_enter(db->mutex);
152522 sqlite3BtreeEnterAll(db);
152523 for(i=0; i<db->nDb; i++){
152524 Btree *pBt = db->aDb[i].pBt;
152525 if( pBt ){
152526 Pager *pPager = sqlite3BtreePager(pBt);
152527 sqlite3PagerShrink(pPager);
152528 }
152529 }
152530 sqlite3BtreeLeaveAll(db);
152531 sqlite3_mutex_leave(db->mutex);
152532 return SQLITE_OK;
152533}
152534
152535/*
152536** Flush any dirty pages in the pager-cache for any attached database
152537** to disk.
152538*/
152539SQLITE_API int sqlite3_db_cacheflush(sqlite3 *db){
152540 int i;
152541 int rc = SQLITE_OK;
152542 int bSeenBusy = 0;
152543
152544#ifdef SQLITE_ENABLE_API_ARMOR
152545 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
152546#endif
152547 sqlite3_mutex_enter(db->mutex);
152548 sqlite3BtreeEnterAll(db);
152549 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
152550 Btree *pBt = db->aDb[i].pBt;
152551 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
152552 Pager *pPager = sqlite3BtreePager(pBt);
152553 rc = sqlite3PagerFlush(pPager);
152554 if( rc==SQLITE_BUSY ){
152555 bSeenBusy = 1;
152556 rc = SQLITE_OK;
152557 }
152558 }
152559 }
152560 sqlite3BtreeLeaveAll(db);
152561 sqlite3_mutex_leave(db->mutex);
152562 return ((rc==SQLITE_OK && bSeenBusy) ? SQLITE_BUSY : rc);
152563}
152564
152565/*
152566** Configuration settings for an individual database connection
152567*/
152568SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
152569 va_list ap;
152570 int rc;
152571 va_start(ap, op);
152572 switch( op ){
152573 case SQLITE_DBCONFIG_MAINDBNAME: {
152574 /* IMP: R-06824-28531 */
152575 /* IMP: R-36257-52125 */
152576 db->aDb[0].zDbSName = va_arg(ap,char*);
152577 rc = SQLITE_OK;
152578 break;
152579 }
152580 case SQLITE_DBCONFIG_LOOKASIDE: {
152581 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
152582 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
152583 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
152584 rc = setupLookaside(db, pBuf, sz, cnt);
152585 break;
152586 }
152587 default: {
152588 static const struct {
152589 int op; /* The opcode */
152590 u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */
152591 } aFlagOp[] = {
152592 { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys },
152593 { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger },
152594 { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer },
152595 { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension },
152596 { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose },
152597 { SQLITE_DBCONFIG_ENABLE_QPSG, SQLITE_EnableQPSG },
152598 { SQLITE_DBCONFIG_TRIGGER_EQP, SQLITE_TriggerEQP },
152599 { SQLITE_DBCONFIG_RESET_DATABASE, SQLITE_ResetDatabase },
152600 };
152601 unsigned int i;
152602 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
152603 for(i=0; i<ArraySize(aFlagOp); i++){
152604 if( aFlagOp[i].op==op ){
152605 int onoff = va_arg(ap, int);
152606 int *pRes = va_arg(ap, int*);
152607 u32 oldFlags = db->flags;
152608 if( onoff>0 ){
152609 db->flags |= aFlagOp[i].mask;
152610 }else if( onoff==0 ){
152611 db->flags &= ~aFlagOp[i].mask;
152612 }
152613 if( oldFlags!=db->flags ){
152614 sqlite3ExpirePreparedStatements(db, 0);
152615 }
152616 if( pRes ){
152617 *pRes = (db->flags & aFlagOp[i].mask)!=0;
152618 }
152619 rc = SQLITE_OK;
152620 break;
152621 }
152622 }
152623 break;
152624 }
152625 }
152626 va_end(ap);
152627 return rc;
152628}
152629
152630
152631/*
152632** Return true if the buffer z[0..n-1] contains all spaces.
152633*/
152634static int allSpaces(const char *z, int n){
152635 while( n>0 && z[n-1]==' ' ){ n--; }
152636 return n==0;
152637}
152638
152639/*
152640** This is the default collating function named "BINARY" which is always
152641** available.
152642**
152643** If the padFlag argument is not NULL then space padding at the end
152644** of strings is ignored. This implements the RTRIM collation.
152645*/
152646static int binCollFunc(
152647 void *padFlag,
152648 int nKey1, const void *pKey1,
152649 int nKey2, const void *pKey2
152650){
152651 int rc, n;
152652 n = nKey1<nKey2 ? nKey1 : nKey2;
152653 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
152654 ** strings byte by byte using the memcmp() function from the standard C
152655 ** library. */
152656 assert( pKey1 && pKey2 );
152657 rc = memcmp(pKey1, pKey2, n);
152658 if( rc==0 ){
152659 if( padFlag
152660 && allSpaces(((char*)pKey1)+n, nKey1-n)
152661 && allSpaces(((char*)pKey2)+n, nKey2-n)
152662 ){
152663 /* EVIDENCE-OF: R-31624-24737 RTRIM is like BINARY except that extra
152664 ** spaces at the end of either string do not change the result. In other
152665 ** words, strings will compare equal to one another as long as they
152666 ** differ only in the number of spaces at the end.
152667 */
152668 }else{
152669 rc = nKey1 - nKey2;
152670 }
152671 }
152672 return rc;
152673}
152674
152675/*
152676** Return true if CollSeq is the default built-in BINARY.
152677*/
152678SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq *p){
152679 assert( p==0 || p->xCmp!=binCollFunc || p->pUser!=0
152680 || strcmp(p->zName,"BINARY")==0 );
152681 return p==0 || (p->xCmp==binCollFunc && p->pUser==0);
152682}
152683
152684/*
152685** Another built-in collating sequence: NOCASE.
152686**
152687** This collating sequence is intended to be used for "case independent
152688** comparison". SQLite's knowledge of upper and lower case equivalents
152689** extends only to the 26 characters used in the English language.
152690**
152691** At the moment there is only a UTF-8 implementation.
152692*/
152693static int nocaseCollatingFunc(
152694 void *NotUsed,
152695 int nKey1, const void *pKey1,
152696 int nKey2, const void *pKey2
152697){
152698 int r = sqlite3StrNICmp(
152699 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
152700 UNUSED_PARAMETER(NotUsed);
152701 if( 0==r ){
152702 r = nKey1-nKey2;
152703 }
152704 return r;
152705}
152706
152707/*
152708** Return the ROWID of the most recent insert
152709*/
152710SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
152711#ifdef SQLITE_ENABLE_API_ARMOR
152712 if( !sqlite3SafetyCheckOk(db) ){
152713 (void)SQLITE_MISUSE_BKPT;
152714 return 0;
152715 }
152716#endif
152717 return db->lastRowid;
152718}
152719
152720/*
152721** Set the value returned by the sqlite3_last_insert_rowid() API function.
152722*/
152723SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
152724#ifdef SQLITE_ENABLE_API_ARMOR
152725 if( !sqlite3SafetyCheckOk(db) ){
152726 (void)SQLITE_MISUSE_BKPT;
152727 return;
152728 }
152729#endif
152730 sqlite3_mutex_enter(db->mutex);
152731 db->lastRowid = iRowid;
152732 sqlite3_mutex_leave(db->mutex);
152733}
152734
152735/*
152736** Return the number of changes in the most recent call to sqlite3_exec().
152737*/
152738SQLITE_API int sqlite3_changes(sqlite3 *db){
152739#ifdef SQLITE_ENABLE_API_ARMOR
152740 if( !sqlite3SafetyCheckOk(db) ){
152741 (void)SQLITE_MISUSE_BKPT;
152742 return 0;
152743 }
152744#endif
152745 return db->nChange;
152746}
152747
152748/*
152749** Return the number of changes since the database handle was opened.
152750*/
152751SQLITE_API int sqlite3_total_changes(sqlite3 *db){
152752#ifdef SQLITE_ENABLE_API_ARMOR
152753 if( !sqlite3SafetyCheckOk(db) ){
152754 (void)SQLITE_MISUSE_BKPT;
152755 return 0;
152756 }
152757#endif
152758 return db->nTotalChange;
152759}
152760
152761/*
152762** Close all open savepoints. This function only manipulates fields of the
152763** database handle object, it does not close any savepoints that may be open
152764** at the b-tree/pager level.
152765*/
152766SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
152767 while( db->pSavepoint ){
152768 Savepoint *pTmp = db->pSavepoint;
152769 db->pSavepoint = pTmp->pNext;
152770 sqlite3DbFree(db, pTmp);
152771 }
152772 db->nSavepoint = 0;
152773 db->nStatement = 0;
152774 db->isTransactionSavepoint = 0;
152775}
152776
152777/*
152778** Invoke the destructor function associated with FuncDef p, if any. Except,
152779** if this is not the last copy of the function, do not invoke it. Multiple
152780** copies of a single function are created when create_function() is called
152781** with SQLITE_ANY as the encoding.
152782*/
152783static void functionDestroy(sqlite3 *db, FuncDef *p){
152784 FuncDestructor *pDestructor = p->u.pDestructor;
152785 if( pDestructor ){
152786 pDestructor->nRef--;
152787 if( pDestructor->nRef==0 ){
152788 pDestructor->xDestroy(pDestructor->pUserData);
152789 sqlite3DbFree(db, pDestructor);
152790 }
152791 }
152792}
152793
152794/*
152795** Disconnect all sqlite3_vtab objects that belong to database connection
152796** db. This is called when db is being closed.
152797*/
152798static void disconnectAllVtab(sqlite3 *db){
152799#ifndef SQLITE_OMIT_VIRTUALTABLE
152800 int i;
152801 HashElem *p;
152802 sqlite3BtreeEnterAll(db);
152803 for(i=0; i<db->nDb; i++){
152804 Schema *pSchema = db->aDb[i].pSchema;
152805 if( pSchema ){
152806 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
152807 Table *pTab = (Table *)sqliteHashData(p);
152808 if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
152809 }
152810 }
152811 }
152812 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
152813 Module *pMod = (Module *)sqliteHashData(p);
152814 if( pMod->pEpoTab ){
152815 sqlite3VtabDisconnect(db, pMod->pEpoTab);
152816 }
152817 }
152818 sqlite3VtabUnlockList(db);
152819 sqlite3BtreeLeaveAll(db);
152820#else
152821 UNUSED_PARAMETER(db);
152822#endif
152823}
152824
152825/*
152826** Return TRUE if database connection db has unfinalized prepared
152827** statements or unfinished sqlite3_backup objects.
152828*/
152829static int connectionIsBusy(sqlite3 *db){
152830 int j;
152831 assert( sqlite3_mutex_held(db->mutex) );
152832 if( db->pVdbe ) return 1;
152833 for(j=0; j<db->nDb; j++){
152834 Btree *pBt = db->aDb[j].pBt;
152835 if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
152836 }
152837 return 0;
152838}
152839
152840/*
152841** Close an existing SQLite database
152842*/
152843static int sqlite3Close(sqlite3 *db, int forceZombie){
152844 if( !db ){
152845 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
152846 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
152847 return SQLITE_OK;
152848 }
152849 if( !sqlite3SafetyCheckSickOrOk(db) ){
152850 return SQLITE_MISUSE_BKPT;
152851 }
152852 sqlite3_mutex_enter(db->mutex);
152853 if( db->mTrace & SQLITE_TRACE_CLOSE ){
152854 db->xTrace(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
152855 }
152856
152857 /* Force xDisconnect calls on all virtual tables */
152858 disconnectAllVtab(db);
152859
152860 /* If a transaction is open, the disconnectAllVtab() call above
152861 ** will not have called the xDisconnect() method on any virtual
152862 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
152863 ** call will do so. We need to do this before the check for active
152864 ** SQL statements below, as the v-table implementation may be storing
152865 ** some prepared statements internally.
152866 */
152867 sqlite3VtabRollback(db);
152868
152869 /* Legacy behavior (sqlite3_close() behavior) is to return
152870 ** SQLITE_BUSY if the connection can not be closed immediately.
152871 */
152872 if( !forceZombie && connectionIsBusy(db) ){
152873 sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized "
152874 "statements or unfinished backups");
152875 sqlite3_mutex_leave(db->mutex);
152876 return SQLITE_BUSY;
152877 }
152878
152879#ifdef SQLITE_ENABLE_SQLLOG
152880 if( sqlite3GlobalConfig.xSqllog ){
152881 /* Closing the handle. Fourth parameter is passed the value 2. */
152882 sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
152883 }
152884#endif
152885
152886 /* Convert the connection into a zombie and then close it.
152887 */
152888 db->magic = SQLITE_MAGIC_ZOMBIE;
152889 sqlite3LeaveMutexAndCloseZombie(db);
152890 return SQLITE_OK;
152891}
152892
152893/*
152894** Two variations on the public interface for closing a database
152895** connection. The sqlite3_close() version returns SQLITE_BUSY and
152896** leaves the connection option if there are unfinalized prepared
152897** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
152898** version forces the connection to become a zombie if there are
152899** unclosed resources, and arranges for deallocation when the last
152900** prepare statement or sqlite3_backup closes.
152901*/
152902SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
152903SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
152904
152905
152906/*
152907** Close the mutex on database connection db.
152908**
152909** Furthermore, if database connection db is a zombie (meaning that there
152910** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
152911** every sqlite3_stmt has now been finalized and every sqlite3_backup has
152912** finished, then free all resources.
152913*/
152914SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
152915 HashElem *i; /* Hash table iterator */
152916 int j;
152917
152918 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
152919 ** or if the connection has not yet been closed by sqlite3_close_v2(),
152920 ** then just leave the mutex and return.
152921 */
152922 if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
152923 sqlite3_mutex_leave(db->mutex);
152924 return;
152925 }
152926
152927 /* If we reach this point, it means that the database connection has
152928 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
152929 ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
152930 ** go ahead and free all resources.
152931 */
152932
152933 /* If a transaction is open, roll it back. This also ensures that if
152934 ** any database schemas have been modified by an uncommitted transaction
152935 ** they are reset. And that the required b-tree mutex is held to make
152936 ** the pager rollback and schema reset an atomic operation. */
152937 sqlite3RollbackAll(db, SQLITE_OK);
152938
152939 /* Free any outstanding Savepoint structures. */
152940 sqlite3CloseSavepoints(db);
152941
152942 /* Close all database connections */
152943 for(j=0; j<db->nDb; j++){
152944 struct Db *pDb = &db->aDb[j];
152945 if( pDb->pBt ){
152946 sqlite3BtreeClose(pDb->pBt);
152947 pDb->pBt = 0;
152948 if( j!=1 ){
152949 pDb->pSchema = 0;
152950 }
152951 }
152952 }
152953 /* Clear the TEMP schema separately and last */
152954 if( db->aDb[1].pSchema ){
152955 sqlite3SchemaClear(db->aDb[1].pSchema);
152956 }
152957 sqlite3VtabUnlockList(db);
152958
152959 /* Free up the array of auxiliary databases */
152960 sqlite3CollapseDatabaseArray(db);
152961 assert( db->nDb<=2 );
152962 assert( db->aDb==db->aDbStatic );
152963
152964 /* Tell the code in notify.c that the connection no longer holds any
152965 ** locks and does not require any further unlock-notify callbacks.
152966 */
152967 sqlite3ConnectionClosed(db);
152968
152969 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
152970 FuncDef *pNext, *p;
152971 p = sqliteHashData(i);
152972 do{
152973 functionDestroy(db, p);
152974 pNext = p->pNext;
152975 sqlite3DbFree(db, p);
152976 p = pNext;
152977 }while( p );
152978 }
152979 sqlite3HashClear(&db->aFunc);
152980 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
152981 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
152982 /* Invoke any destructors registered for collation sequence user data. */
152983 for(j=0; j<3; j++){
152984 if( pColl[j].xDel ){
152985 pColl[j].xDel(pColl[j].pUser);
152986 }
152987 }
152988 sqlite3DbFree(db, pColl);
152989 }
152990 sqlite3HashClear(&db->aCollSeq);
152991#ifndef SQLITE_OMIT_VIRTUALTABLE
152992 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
152993 Module *pMod = (Module *)sqliteHashData(i);
152994 if( pMod->xDestroy ){
152995 pMod->xDestroy(pMod->pAux);
152996 }
152997 sqlite3VtabEponymousTableClear(db, pMod);
152998 sqlite3DbFree(db, pMod);
152999 }
153000 sqlite3HashClear(&db->aModule);
153001#endif
153002
153003 sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
153004 sqlite3ValueFree(db->pErr);
153005 sqlite3CloseExtensions(db);
153006#if SQLITE_USER_AUTHENTICATION
153007 sqlite3_free(db->auth.zAuthUser);
153008 sqlite3_free(db->auth.zAuthPW);
153009#endif
153010
153011 db->magic = SQLITE_MAGIC_ERROR;
153012
153013 /* The temp-database schema is allocated differently from the other schema
153014 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
153015 ** So it needs to be freed here. Todo: Why not roll the temp schema into
153016 ** the same sqliteMalloc() as the one that allocates the database
153017 ** structure?
153018 */
153019 sqlite3DbFree(db, db->aDb[1].pSchema);
153020 sqlite3_mutex_leave(db->mutex);
153021 db->magic = SQLITE_MAGIC_CLOSED;
153022 sqlite3_mutex_free(db->mutex);
153023 assert( sqlite3LookasideUsed(db,0)==0 );
153024 if( db->lookaside.bMalloced ){
153025 sqlite3_free(db->lookaside.pStart);
153026 }
153027 sqlite3_free(db);
153028}
153029
153030/*
153031** Rollback all database files. If tripCode is not SQLITE_OK, then
153032** any write cursors are invalidated ("tripped" - as in "tripping a circuit
153033** breaker") and made to return tripCode if there are any further
153034** attempts to use that cursor. Read cursors remain open and valid
153035** but are "saved" in case the table pages are moved around.
153036*/
153037SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
153038 int i;
153039 int inTrans = 0;
153040 int schemaChange;
153041 assert( sqlite3_mutex_held(db->mutex) );
153042 sqlite3BeginBenignMalloc();
153043
153044 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
153045 ** This is important in case the transaction being rolled back has
153046 ** modified the database schema. If the b-tree mutexes are not taken
153047 ** here, then another shared-cache connection might sneak in between
153048 ** the database rollback and schema reset, which can cause false
153049 ** corruption reports in some cases. */
153050 sqlite3BtreeEnterAll(db);
153051 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
153052
153053 for(i=0; i<db->nDb; i++){
153054 Btree *p = db->aDb[i].pBt;
153055 if( p ){
153056 if( sqlite3BtreeIsInTrans(p) ){
153057 inTrans = 1;
153058 }
153059 sqlite3BtreeRollback(p, tripCode, !schemaChange);
153060 }
153061 }
153062 sqlite3VtabRollback(db);
153063 sqlite3EndBenignMalloc();
153064
153065 if( schemaChange ){
153066 sqlite3ExpirePreparedStatements(db, 0);
153067 sqlite3ResetAllSchemasOfConnection(db);
153068 }
153069 sqlite3BtreeLeaveAll(db);
153070
153071 /* Any deferred constraint violations have now been resolved. */
153072 db->nDeferredCons = 0;
153073 db->nDeferredImmCons = 0;
153074 db->flags &= ~SQLITE_DeferFKs;
153075
153076 /* If one has been configured, invoke the rollback-hook callback */
153077 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
153078 db->xRollbackCallback(db->pRollbackArg);
153079 }
153080}
153081
153082/*
153083** Return a static string containing the name corresponding to the error code
153084** specified in the argument.
153085*/
153086#if defined(SQLITE_NEED_ERR_NAME)
153087SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
153088 const char *zName = 0;
153089 int i, origRc = rc;
153090 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
153091 switch( rc ){
153092 case SQLITE_OK: zName = "SQLITE_OK"; break;
153093 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
153094 case SQLITE_ERROR_SNAPSHOT: zName = "SQLITE_ERROR_SNAPSHOT"; break;
153095 case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break;
153096 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
153097 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
153098 case SQLITE_ABORT_ROLLBACK: zName = "SQLITE_ABORT_ROLLBACK"; break;
153099 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
153100 case SQLITE_BUSY_RECOVERY: zName = "SQLITE_BUSY_RECOVERY"; break;
153101 case SQLITE_BUSY_SNAPSHOT: zName = "SQLITE_BUSY_SNAPSHOT"; break;
153102 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
153103 case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
153104 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
153105 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
153106 case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break;
153107 case SQLITE_READONLY_CANTINIT: zName = "SQLITE_READONLY_CANTINIT"; break;
153108 case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break;
153109 case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break;
153110 case SQLITE_READONLY_DIRECTORY: zName = "SQLITE_READONLY_DIRECTORY";break;
153111 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
153112 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
153113 case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
153114 case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
153115 case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
153116 case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break;
153117 case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break;
153118 case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
153119 case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
153120 case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
153121 case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
153122 case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
153123 case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
153124 case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
153125 case SQLITE_IOERR_CHECKRESERVEDLOCK:
153126 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
153127 case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
153128 case SQLITE_IOERR_CLOSE: zName = "SQLITE_IOERR_CLOSE"; break;
153129 case SQLITE_IOERR_DIR_CLOSE: zName = "SQLITE_IOERR_DIR_CLOSE"; break;
153130 case SQLITE_IOERR_SHMOPEN: zName = "SQLITE_IOERR_SHMOPEN"; break;
153131 case SQLITE_IOERR_SHMSIZE: zName = "SQLITE_IOERR_SHMSIZE"; break;
153132 case SQLITE_IOERR_SHMLOCK: zName = "SQLITE_IOERR_SHMLOCK"; break;
153133 case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break;
153134 case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break;
153135 case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
153136 case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break;
153137 case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break;
153138 case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break;
153139 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
153140 case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break;
153141 case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
153142 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
153143 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
153144 case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
153145 case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break;
153146 case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break;
153147 case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break;
153148 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
153149 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
153150 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
153151 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
153152 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
153153 case SQLITE_CONSTRAINT_UNIQUE: zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
153154 case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
153155 case SQLITE_CONSTRAINT_FOREIGNKEY:
153156 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break;
153157 case SQLITE_CONSTRAINT_CHECK: zName = "SQLITE_CONSTRAINT_CHECK"; break;
153158 case SQLITE_CONSTRAINT_PRIMARYKEY:
153159 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break;
153160 case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
153161 case SQLITE_CONSTRAINT_COMMITHOOK:
153162 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break;
153163 case SQLITE_CONSTRAINT_VTAB: zName = "SQLITE_CONSTRAINT_VTAB"; break;
153164 case SQLITE_CONSTRAINT_FUNCTION:
153165 zName = "SQLITE_CONSTRAINT_FUNCTION"; break;
153166 case SQLITE_CONSTRAINT_ROWID: zName = "SQLITE_CONSTRAINT_ROWID"; break;
153167 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
153168 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
153169 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
153170 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
153171 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
153172 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
153173 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
153174 case SQLITE_ROW: zName = "SQLITE_ROW"; break;
153175 case SQLITE_NOTICE: zName = "SQLITE_NOTICE"; break;
153176 case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
153177 case SQLITE_NOTICE_RECOVER_ROLLBACK:
153178 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
153179 case SQLITE_WARNING: zName = "SQLITE_WARNING"; break;
153180 case SQLITE_WARNING_AUTOINDEX: zName = "SQLITE_WARNING_AUTOINDEX"; break;
153181 case SQLITE_DONE: zName = "SQLITE_DONE"; break;
153182 }
153183 }
153184 if( zName==0 ){
153185 static char zBuf[50];
153186 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
153187 zName = zBuf;
153188 }
153189 return zName;
153190}
153191#endif
153192
153193/*
153194** Return a static string that describes the kind of error specified in the
153195** argument.
153196*/
153197SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
153198 static const char* const aMsg[] = {
153199 /* SQLITE_OK */ "not an error",
153200 /* SQLITE_ERROR */ "SQL logic error",
153201 /* SQLITE_INTERNAL */ 0,
153202 /* SQLITE_PERM */ "access permission denied",
153203 /* SQLITE_ABORT */ "query aborted",
153204 /* SQLITE_BUSY */ "database is locked",
153205 /* SQLITE_LOCKED */ "database table is locked",
153206 /* SQLITE_NOMEM */ "out of memory",
153207 /* SQLITE_READONLY */ "attempt to write a readonly database",
153208 /* SQLITE_INTERRUPT */ "interrupted",
153209 /* SQLITE_IOERR */ "disk I/O error",
153210 /* SQLITE_CORRUPT */ "database disk image is malformed",
153211 /* SQLITE_NOTFOUND */ "unknown operation",
153212 /* SQLITE_FULL */ "database or disk is full",
153213 /* SQLITE_CANTOPEN */ "unable to open database file",
153214 /* SQLITE_PROTOCOL */ "locking protocol",
153215 /* SQLITE_EMPTY */ 0,
153216 /* SQLITE_SCHEMA */ "database schema has changed",
153217 /* SQLITE_TOOBIG */ "string or blob too big",
153218 /* SQLITE_CONSTRAINT */ "constraint failed",
153219 /* SQLITE_MISMATCH */ "datatype mismatch",
153220 /* SQLITE_MISUSE */ "bad parameter or other API misuse",
153221#ifdef SQLITE_DISABLE_LFS
153222 /* SQLITE_NOLFS */ "large file support is disabled",
153223#else
153224 /* SQLITE_NOLFS */ 0,
153225#endif
153226 /* SQLITE_AUTH */ "authorization denied",
153227 /* SQLITE_FORMAT */ 0,
153228 /* SQLITE_RANGE */ "column index out of range",
153229 /* SQLITE_NOTADB */ "file is not a database",
153230 /* SQLITE_NOTICE */ "notification message",
153231 /* SQLITE_WARNING */ "warning message",
153232 };
153233 const char *zErr = "unknown error";
153234 switch( rc ){
153235 case SQLITE_ABORT_ROLLBACK: {
153236 zErr = "abort due to ROLLBACK";
153237 break;
153238 }
153239 case SQLITE_ROW: {
153240 zErr = "another row available";
153241 break;
153242 }
153243 case SQLITE_DONE: {
153244 zErr = "no more rows available";
153245 break;
153246 }
153247 default: {
153248 rc &= 0xff;
153249 if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
153250 zErr = aMsg[rc];
153251 }
153252 break;
153253 }
153254 }
153255 return zErr;
153256}
153257
153258/*
153259** This routine implements a busy callback that sleeps and tries
153260** again until a timeout value is reached. The timeout value is
153261** an integer number of milliseconds passed in as the first
153262** argument.
153263**
153264** Return non-zero to retry the lock. Return zero to stop trying
153265** and cause SQLite to return SQLITE_BUSY.
153266*/
153267static int sqliteDefaultBusyCallback(
153268 void *ptr, /* Database connection */
153269 int count, /* Number of times table has been busy */
153270 sqlite3_file *pFile /* The file on which the lock occurred */
153271){
153272#if SQLITE_OS_WIN || HAVE_USLEEP
153273 /* This case is for systems that have support for sleeping for fractions of
153274 ** a second. Examples: All windows systems, unix systems with usleep() */
153275 static const u8 delays[] =
153276 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
153277 static const u8 totals[] =
153278 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
153279# define NDELAY ArraySize(delays)
153280 sqlite3 *db = (sqlite3 *)ptr;
153281 int tmout = db->busyTimeout;
153282 int delay, prior;
153283
153284#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
153285 if( sqlite3OsFileControl(pFile,SQLITE_FCNTL_LOCK_TIMEOUT,&tmout)==SQLITE_OK ){
153286 if( count ){
153287 tmout = 0;
153288 sqlite3OsFileControl(pFile, SQLITE_FCNTL_LOCK_TIMEOUT, &tmout);
153289 return 0;
153290 }else{
153291 return 1;
153292 }
153293 }
153294#else
153295 UNUSED_PARAMETER(pFile);
153296#endif
153297 assert( count>=0 );
153298 if( count < NDELAY ){
153299 delay = delays[count];
153300 prior = totals[count];
153301 }else{
153302 delay = delays[NDELAY-1];
153303 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
153304 }
153305 if( prior + delay > tmout ){
153306 delay = tmout - prior;
153307 if( delay<=0 ) return 0;
153308 }
153309 sqlite3OsSleep(db->pVfs, delay*1000);
153310 return 1;
153311#else
153312 /* This case for unix systems that lack usleep() support. Sleeping
153313 ** must be done in increments of whole seconds */
153314 sqlite3 *db = (sqlite3 *)ptr;
153315 int tmout = ((sqlite3 *)ptr)->busyTimeout;
153316 UNUSED_PARAMETER(pFile);
153317 if( (count+1)*1000 > tmout ){
153318 return 0;
153319 }
153320 sqlite3OsSleep(db->pVfs, 1000000);
153321 return 1;
153322#endif
153323}
153324
153325/*
153326** Invoke the given busy handler.
153327**
153328** This routine is called when an operation failed to acquire a
153329** lock on VFS file pFile.
153330**
153331** If this routine returns non-zero, the lock is retried. If it
153332** returns 0, the operation aborts with an SQLITE_BUSY error.
153333*/
153334SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p, sqlite3_file *pFile){
153335 int rc;
153336 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
153337 if( p->bExtraFileArg ){
153338 /* Add an extra parameter with the pFile pointer to the end of the
153339 ** callback argument list */
153340 int (*xTra)(void*,int,sqlite3_file*);
153341 xTra = (int(*)(void*,int,sqlite3_file*))p->xBusyHandler;
153342 rc = xTra(p->pBusyArg, p->nBusy, pFile);
153343 }else{
153344 /* Legacy style busy handler callback */
153345 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
153346 }
153347 if( rc==0 ){
153348 p->nBusy = -1;
153349 }else{
153350 p->nBusy++;
153351 }
153352 return rc;
153353}
153354
153355/*
153356** This routine sets the busy callback for an Sqlite database to the
153357** given callback function with the given argument.
153358*/
153359SQLITE_API int sqlite3_busy_handler(
153360 sqlite3 *db,
153361 int (*xBusy)(void*,int),
153362 void *pArg
153363){
153364#ifdef SQLITE_ENABLE_API_ARMOR
153365 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
153366#endif
153367 sqlite3_mutex_enter(db->mutex);
153368 db->busyHandler.xBusyHandler = xBusy;
153369 db->busyHandler.pBusyArg = pArg;
153370 db->busyHandler.nBusy = 0;
153371 db->busyHandler.bExtraFileArg = 0;
153372 db->busyTimeout = 0;
153373 sqlite3_mutex_leave(db->mutex);
153374 return SQLITE_OK;
153375}
153376
153377#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
153378/*
153379** This routine sets the progress callback for an Sqlite database to the
153380** given callback function with the given argument. The progress callback will
153381** be invoked every nOps opcodes.
153382*/
153383SQLITE_API void sqlite3_progress_handler(
153384 sqlite3 *db,
153385 int nOps,
153386 int (*xProgress)(void*),
153387 void *pArg
153388){
153389#ifdef SQLITE_ENABLE_API_ARMOR
153390 if( !sqlite3SafetyCheckOk(db) ){
153391 (void)SQLITE_MISUSE_BKPT;
153392 return;
153393 }
153394#endif
153395 sqlite3_mutex_enter(db->mutex);
153396 if( nOps>0 ){
153397 db->xProgress = xProgress;
153398 db->nProgressOps = (unsigned)nOps;
153399 db->pProgressArg = pArg;
153400 }else{
153401 db->xProgress = 0;
153402 db->nProgressOps = 0;
153403 db->pProgressArg = 0;
153404 }
153405 sqlite3_mutex_leave(db->mutex);
153406}
153407#endif
153408
153409
153410/*
153411** This routine installs a default busy handler that waits for the
153412** specified number of milliseconds before returning 0.
153413*/
153414SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
153415#ifdef SQLITE_ENABLE_API_ARMOR
153416 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
153417#endif
153418 if( ms>0 ){
153419 sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
153420 (void*)db);
153421 db->busyTimeout = ms;
153422 db->busyHandler.bExtraFileArg = 1;
153423 }else{
153424 sqlite3_busy_handler(db, 0, 0);
153425 }
153426 return SQLITE_OK;
153427}
153428
153429/*
153430** Cause any pending operation to stop at its earliest opportunity.
153431*/
153432SQLITE_API void sqlite3_interrupt(sqlite3 *db){
153433#ifdef SQLITE_ENABLE_API_ARMOR
153434 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->magic!=SQLITE_MAGIC_ZOMBIE) ){
153435 (void)SQLITE_MISUSE_BKPT;
153436 return;
153437 }
153438#endif
153439 db->u1.isInterrupted = 1;
153440}
153441
153442
153443/*
153444** This function is exactly the same as sqlite3_create_function(), except
153445** that it is designed to be called by internal code. The difference is
153446** that if a malloc() fails in sqlite3_create_function(), an error code
153447** is returned and the mallocFailed flag cleared.
153448*/
153449SQLITE_PRIVATE int sqlite3CreateFunc(
153450 sqlite3 *db,
153451 const char *zFunctionName,
153452 int nArg,
153453 int enc,
153454 void *pUserData,
153455 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
153456 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
153457 void (*xFinal)(sqlite3_context*),
153458 void (*xValue)(sqlite3_context*),
153459 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
153460 FuncDestructor *pDestructor
153461){
153462 FuncDef *p;
153463 int nName;
153464 int extraFlags;
153465
153466 assert( sqlite3_mutex_held(db->mutex) );
153467 assert( xValue==0 || xSFunc==0 );
153468 if( zFunctionName==0 /* Must have a valid name */
153469 || (xSFunc!=0 && xFinal!=0) /* Not both xSFunc and xFinal */
153470 || ((xFinal==0)!=(xStep==0)) /* Both or neither of xFinal and xStep */
153471 || ((xValue==0)!=(xInverse==0)) /* Both or neither of xValue, xInverse */
153472 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG)
153473 || (255<(nName = sqlite3Strlen30( zFunctionName)))
153474 ){
153475 return SQLITE_MISUSE_BKPT;
153476 }
153477
153478 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
153479 extraFlags = enc & SQLITE_DETERMINISTIC;
153480 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
153481
153482#ifndef SQLITE_OMIT_UTF16
153483 /* If SQLITE_UTF16 is specified as the encoding type, transform this
153484 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
153485 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
153486 **
153487 ** If SQLITE_ANY is specified, add three versions of the function
153488 ** to the hash table.
153489 */
153490 if( enc==SQLITE_UTF16 ){
153491 enc = SQLITE_UTF16NATIVE;
153492 }else if( enc==SQLITE_ANY ){
153493 int rc;
153494 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags,
153495 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
153496 if( rc==SQLITE_OK ){
153497 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags,
153498 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
153499 }
153500 if( rc!=SQLITE_OK ){
153501 return rc;
153502 }
153503 enc = SQLITE_UTF16BE;
153504 }
153505#else
153506 enc = SQLITE_UTF8;
153507#endif
153508
153509 /* Check if an existing function is being overridden or deleted. If so,
153510 ** and there are active VMs, then return SQLITE_BUSY. If a function
153511 ** is being overridden/deleted but there are no active VMs, allow the
153512 ** operation to continue but invalidate all precompiled statements.
153513 */
153514 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0);
153515 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){
153516 if( db->nVdbeActive ){
153517 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
153518 "unable to delete/modify user-function due to active statements");
153519 assert( !db->mallocFailed );
153520 return SQLITE_BUSY;
153521 }else{
153522 sqlite3ExpirePreparedStatements(db, 0);
153523 }
153524 }
153525
153526 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1);
153527 assert(p || db->mallocFailed);
153528 if( !p ){
153529 return SQLITE_NOMEM_BKPT;
153530 }
153531
153532 /* If an older version of the function with a configured destructor is
153533 ** being replaced invoke the destructor function here. */
153534 functionDestroy(db, p);
153535
153536 if( pDestructor ){
153537 pDestructor->nRef++;
153538 }
153539 p->u.pDestructor = pDestructor;
153540 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
153541 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
153542 p->xSFunc = xSFunc ? xSFunc : xStep;
153543 p->xFinalize = xFinal;
153544 p->xValue = xValue;
153545 p->xInverse = xInverse;
153546 p->pUserData = pUserData;
153547 p->nArg = (u16)nArg;
153548 return SQLITE_OK;
153549}
153550
153551/*
153552** Worker function used by utf-8 APIs that create new functions:
153553**
153554** sqlite3_create_function()
153555** sqlite3_create_function_v2()
153556** sqlite3_create_window_function()
153557*/
153558static int createFunctionApi(
153559 sqlite3 *db,
153560 const char *zFunc,
153561 int nArg,
153562 int enc,
153563 void *p,
153564 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
153565 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
153566 void (*xFinal)(sqlite3_context*),
153567 void (*xValue)(sqlite3_context*),
153568 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
153569 void(*xDestroy)(void*)
153570){
153571 int rc = SQLITE_ERROR;
153572 FuncDestructor *pArg = 0;
153573
153574#ifdef SQLITE_ENABLE_API_ARMOR
153575 if( !sqlite3SafetyCheckOk(db) ){
153576 return SQLITE_MISUSE_BKPT;
153577 }
153578#endif
153579 sqlite3_mutex_enter(db->mutex);
153580 if( xDestroy ){
153581 pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor));
153582 if( !pArg ){
153583 sqlite3OomFault(db);
153584 xDestroy(p);
153585 goto out;
153586 }
153587 pArg->nRef = 0;
153588 pArg->xDestroy = xDestroy;
153589 pArg->pUserData = p;
153590 }
153591 rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p,
153592 xSFunc, xStep, xFinal, xValue, xInverse, pArg
153593 );
153594 if( pArg && pArg->nRef==0 ){
153595 assert( rc!=SQLITE_OK );
153596 xDestroy(p);
153597 sqlite3_free(pArg);
153598 }
153599
153600 out:
153601 rc = sqlite3ApiExit(db, rc);
153602 sqlite3_mutex_leave(db->mutex);
153603 return rc;
153604}
153605
153606/*
153607** Create new user functions.
153608*/
153609SQLITE_API int sqlite3_create_function(
153610 sqlite3 *db,
153611 const char *zFunc,
153612 int nArg,
153613 int enc,
153614 void *p,
153615 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
153616 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
153617 void (*xFinal)(sqlite3_context*)
153618){
153619 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
153620 xFinal, 0, 0, 0);
153621}
153622SQLITE_API int sqlite3_create_function_v2(
153623 sqlite3 *db,
153624 const char *zFunc,
153625 int nArg,
153626 int enc,
153627 void *p,
153628 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
153629 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
153630 void (*xFinal)(sqlite3_context*),
153631 void (*xDestroy)(void *)
153632){
153633 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
153634 xFinal, 0, 0, xDestroy);
153635}
153636SQLITE_API int sqlite3_create_window_function(
153637 sqlite3 *db,
153638 const char *zFunc,
153639 int nArg,
153640 int enc,
153641 void *p,
153642 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
153643 void (*xFinal)(sqlite3_context*),
153644 void (*xValue)(sqlite3_context*),
153645 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
153646 void (*xDestroy)(void *)
153647){
153648 return createFunctionApi(db, zFunc, nArg, enc, p, 0, xStep,
153649 xFinal, xValue, xInverse, xDestroy);
153650}
153651
153652#ifndef SQLITE_OMIT_UTF16
153653SQLITE_API int sqlite3_create_function16(
153654 sqlite3 *db,
153655 const void *zFunctionName,
153656 int nArg,
153657 int eTextRep,
153658 void *p,
153659 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
153660 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
153661 void (*xFinal)(sqlite3_context*)
153662){
153663 int rc;
153664 char *zFunc8;
153665
153666#ifdef SQLITE_ENABLE_API_ARMOR
153667 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
153668#endif
153669 sqlite3_mutex_enter(db->mutex);
153670 assert( !db->mallocFailed );
153671 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
153672 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0,0,0);
153673 sqlite3DbFree(db, zFunc8);
153674 rc = sqlite3ApiExit(db, rc);
153675 sqlite3_mutex_leave(db->mutex);
153676 return rc;
153677}
153678#endif
153679
153680
153681/*
153682** The following is the implementation of an SQL function that always
153683** fails with an error message stating that the function is used in the
153684** wrong context. The sqlite3_overload_function() API might construct
153685** SQL function that use this routine so that the functions will exist
153686** for name resolution but are actually overloaded by the xFindFunction
153687** method of virtual tables.
153688*/
153689static void sqlite3InvalidFunction(
153690 sqlite3_context *context, /* The function calling context */
153691 int NotUsed, /* Number of arguments to the function */
153692 sqlite3_value **NotUsed2 /* Value of each argument */
153693){
153694 const char *zName = (const char*)sqlite3_user_data(context);
153695 char *zErr;
153696 UNUSED_PARAMETER2(NotUsed, NotUsed2);
153697 zErr = sqlite3_mprintf(
153698 "unable to use function %s in the requested context", zName);
153699 sqlite3_result_error(context, zErr, -1);
153700 sqlite3_free(zErr);
153701}
153702
153703/*
153704** Declare that a function has been overloaded by a virtual table.
153705**
153706** If the function already exists as a regular global function, then
153707** this routine is a no-op. If the function does not exist, then create
153708** a new one that always throws a run-time error.
153709**
153710** When virtual tables intend to provide an overloaded function, they
153711** should call this routine to make sure the global function exists.
153712** A global function must exist in order for name resolution to work
153713** properly.
153714*/
153715SQLITE_API int sqlite3_overload_function(
153716 sqlite3 *db,
153717 const char *zName,
153718 int nArg
153719){
153720 int rc;
153721 char *zCopy;
153722
153723#ifdef SQLITE_ENABLE_API_ARMOR
153724 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
153725 return SQLITE_MISUSE_BKPT;
153726 }
153727#endif
153728 sqlite3_mutex_enter(db->mutex);
153729 rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)!=0;
153730 sqlite3_mutex_leave(db->mutex);
153731 if( rc ) return SQLITE_OK;
153732 zCopy = sqlite3_mprintf(zName);
153733 if( zCopy==0 ) return SQLITE_NOMEM;
153734 return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF8,
153735 zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
153736}
153737
153738#ifndef SQLITE_OMIT_TRACE
153739/*
153740** Register a trace function. The pArg from the previously registered trace
153741** is returned.
153742**
153743** A NULL trace function means that no tracing is executes. A non-NULL
153744** trace is a pointer to a function that is invoked at the start of each
153745** SQL statement.
153746*/
153747#ifndef SQLITE_OMIT_DEPRECATED
153748SQLITE_API void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){
153749 void *pOld;
153750
153751#ifdef SQLITE_ENABLE_API_ARMOR
153752 if( !sqlite3SafetyCheckOk(db) ){
153753 (void)SQLITE_MISUSE_BKPT;
153754 return 0;
153755 }
153756#endif
153757 sqlite3_mutex_enter(db->mutex);
153758 pOld = db->pTraceArg;
153759 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
153760 db->xTrace = (int(*)(u32,void*,void*,void*))xTrace;
153761 db->pTraceArg = pArg;
153762 sqlite3_mutex_leave(db->mutex);
153763 return pOld;
153764}
153765#endif /* SQLITE_OMIT_DEPRECATED */
153766
153767/* Register a trace callback using the version-2 interface.
153768*/
153769SQLITE_API int sqlite3_trace_v2(
153770 sqlite3 *db, /* Trace this connection */
153771 unsigned mTrace, /* Mask of events to be traced */
153772 int(*xTrace)(unsigned,void*,void*,void*), /* Callback to invoke */
153773 void *pArg /* Context */
153774){
153775#ifdef SQLITE_ENABLE_API_ARMOR
153776 if( !sqlite3SafetyCheckOk(db) ){
153777 return SQLITE_MISUSE_BKPT;
153778 }
153779#endif
153780 sqlite3_mutex_enter(db->mutex);
153781 if( mTrace==0 ) xTrace = 0;
153782 if( xTrace==0 ) mTrace = 0;
153783 db->mTrace = mTrace;
153784 db->xTrace = xTrace;
153785 db->pTraceArg = pArg;
153786 sqlite3_mutex_leave(db->mutex);
153787 return SQLITE_OK;
153788}
153789
153790#ifndef SQLITE_OMIT_DEPRECATED
153791/*
153792** Register a profile function. The pArg from the previously registered
153793** profile function is returned.
153794**
153795** A NULL profile function means that no profiling is executes. A non-NULL
153796** profile is a pointer to a function that is invoked at the conclusion of
153797** each SQL statement that is run.
153798*/
153799SQLITE_API void *sqlite3_profile(
153800 sqlite3 *db,
153801 void (*xProfile)(void*,const char*,sqlite_uint64),
153802 void *pArg
153803){
153804 void *pOld;
153805
153806#ifdef SQLITE_ENABLE_API_ARMOR
153807 if( !sqlite3SafetyCheckOk(db) ){
153808 (void)SQLITE_MISUSE_BKPT;
153809 return 0;
153810 }
153811#endif
153812 sqlite3_mutex_enter(db->mutex);
153813 pOld = db->pProfileArg;
153814 db->xProfile = xProfile;
153815 db->pProfileArg = pArg;
153816 sqlite3_mutex_leave(db->mutex);
153817 return pOld;
153818}
153819#endif /* SQLITE_OMIT_DEPRECATED */
153820#endif /* SQLITE_OMIT_TRACE */
153821
153822/*
153823** Register a function to be invoked when a transaction commits.
153824** If the invoked function returns non-zero, then the commit becomes a
153825** rollback.
153826*/
153827SQLITE_API void *sqlite3_commit_hook(
153828 sqlite3 *db, /* Attach the hook to this database */
153829 int (*xCallback)(void*), /* Function to invoke on each commit */
153830 void *pArg /* Argument to the function */
153831){
153832 void *pOld;
153833
153834#ifdef SQLITE_ENABLE_API_ARMOR
153835 if( !sqlite3SafetyCheckOk(db) ){
153836 (void)SQLITE_MISUSE_BKPT;
153837 return 0;
153838 }
153839#endif
153840 sqlite3_mutex_enter(db->mutex);
153841 pOld = db->pCommitArg;
153842 db->xCommitCallback = xCallback;
153843 db->pCommitArg = pArg;
153844 sqlite3_mutex_leave(db->mutex);
153845 return pOld;
153846}
153847
153848/*
153849** Register a callback to be invoked each time a row is updated,
153850** inserted or deleted using this database connection.
153851*/
153852SQLITE_API void *sqlite3_update_hook(
153853 sqlite3 *db, /* Attach the hook to this database */
153854 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
153855 void *pArg /* Argument to the function */
153856){
153857 void *pRet;
153858
153859#ifdef SQLITE_ENABLE_API_ARMOR
153860 if( !sqlite3SafetyCheckOk(db) ){
153861 (void)SQLITE_MISUSE_BKPT;
153862 return 0;
153863 }
153864#endif
153865 sqlite3_mutex_enter(db->mutex);
153866 pRet = db->pUpdateArg;
153867 db->xUpdateCallback = xCallback;
153868 db->pUpdateArg = pArg;
153869 sqlite3_mutex_leave(db->mutex);
153870 return pRet;
153871}
153872
153873/*
153874** Register a callback to be invoked each time a transaction is rolled
153875** back by this database connection.
153876*/
153877SQLITE_API void *sqlite3_rollback_hook(
153878 sqlite3 *db, /* Attach the hook to this database */
153879 void (*xCallback)(void*), /* Callback function */
153880 void *pArg /* Argument to the function */
153881){
153882 void *pRet;
153883
153884#ifdef SQLITE_ENABLE_API_ARMOR
153885 if( !sqlite3SafetyCheckOk(db) ){
153886 (void)SQLITE_MISUSE_BKPT;
153887 return 0;
153888 }
153889#endif
153890 sqlite3_mutex_enter(db->mutex);
153891 pRet = db->pRollbackArg;
153892 db->xRollbackCallback = xCallback;
153893 db->pRollbackArg = pArg;
153894 sqlite3_mutex_leave(db->mutex);
153895 return pRet;
153896}
153897
153898#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
153899/*
153900** Register a callback to be invoked each time a row is updated,
153901** inserted or deleted using this database connection.
153902*/
153903SQLITE_API void *sqlite3_preupdate_hook(
153904 sqlite3 *db, /* Attach the hook to this database */
153905 void(*xCallback)( /* Callback function */
153906 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64),
153907 void *pArg /* First callback argument */
153908){
153909 void *pRet;
153910 sqlite3_mutex_enter(db->mutex);
153911 pRet = db->pPreUpdateArg;
153912 db->xPreUpdateCallback = xCallback;
153913 db->pPreUpdateArg = pArg;
153914 sqlite3_mutex_leave(db->mutex);
153915 return pRet;
153916}
153917#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
153918
153919#ifndef SQLITE_OMIT_WAL
153920/*
153921** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
153922** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
153923** is greater than sqlite3.pWalArg cast to an integer (the value configured by
153924** wal_autocheckpoint()).
153925*/
153926SQLITE_PRIVATE int sqlite3WalDefaultHook(
153927 void *pClientData, /* Argument */
153928 sqlite3 *db, /* Connection */
153929 const char *zDb, /* Database */
153930 int nFrame /* Size of WAL */
153931){
153932 if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
153933 sqlite3BeginBenignMalloc();
153934 sqlite3_wal_checkpoint(db, zDb);
153935 sqlite3EndBenignMalloc();
153936 }
153937 return SQLITE_OK;
153938}
153939#endif /* SQLITE_OMIT_WAL */
153940
153941/*
153942** Configure an sqlite3_wal_hook() callback to automatically checkpoint
153943** a database after committing a transaction if there are nFrame or
153944** more frames in the log file. Passing zero or a negative value as the
153945** nFrame parameter disables automatic checkpoints entirely.
153946**
153947** The callback registered by this function replaces any existing callback
153948** registered using sqlite3_wal_hook(). Likewise, registering a callback
153949** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
153950** configured by this function.
153951*/
153952SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
153953#ifdef SQLITE_OMIT_WAL
153954 UNUSED_PARAMETER(db);
153955 UNUSED_PARAMETER(nFrame);
153956#else
153957#ifdef SQLITE_ENABLE_API_ARMOR
153958 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
153959#endif
153960 if( nFrame>0 ){
153961 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
153962 }else{
153963 sqlite3_wal_hook(db, 0, 0);
153964 }
153965#endif
153966 return SQLITE_OK;
153967}
153968
153969/*
153970** Register a callback to be invoked each time a transaction is written
153971** into the write-ahead-log by this database connection.
153972*/
153973SQLITE_API void *sqlite3_wal_hook(
153974 sqlite3 *db, /* Attach the hook to this db handle */
153975 int(*xCallback)(void *, sqlite3*, const char*, int),
153976 void *pArg /* First argument passed to xCallback() */
153977){
153978#ifndef SQLITE_OMIT_WAL
153979 void *pRet;
153980#ifdef SQLITE_ENABLE_API_ARMOR
153981 if( !sqlite3SafetyCheckOk(db) ){
153982 (void)SQLITE_MISUSE_BKPT;
153983 return 0;
153984 }
153985#endif
153986 sqlite3_mutex_enter(db->mutex);
153987 pRet = db->pWalArg;
153988 db->xWalCallback = xCallback;
153989 db->pWalArg = pArg;
153990 sqlite3_mutex_leave(db->mutex);
153991 return pRet;
153992#else
153993 return 0;
153994#endif
153995}
153996
153997/*
153998** Checkpoint database zDb.
153999*/
154000SQLITE_API int sqlite3_wal_checkpoint_v2(
154001 sqlite3 *db, /* Database handle */
154002 const char *zDb, /* Name of attached database (or NULL) */
154003 int eMode, /* SQLITE_CHECKPOINT_* value */
154004 int *pnLog, /* OUT: Size of WAL log in frames */
154005 int *pnCkpt /* OUT: Total number of frames checkpointed */
154006){
154007#ifdef SQLITE_OMIT_WAL
154008 return SQLITE_OK;
154009#else
154010 int rc; /* Return code */
154011 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
154012
154013#ifdef SQLITE_ENABLE_API_ARMOR
154014 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
154015#endif
154016
154017 /* Initialize the output variables to -1 in case an error occurs. */
154018 if( pnLog ) *pnLog = -1;
154019 if( pnCkpt ) *pnCkpt = -1;
154020
154021 assert( SQLITE_CHECKPOINT_PASSIVE==0 );
154022 assert( SQLITE_CHECKPOINT_FULL==1 );
154023 assert( SQLITE_CHECKPOINT_RESTART==2 );
154024 assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
154025 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){
154026 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
154027 ** mode: */
154028 return SQLITE_MISUSE;
154029 }
154030
154031 sqlite3_mutex_enter(db->mutex);
154032 if( zDb && zDb[0] ){
154033 iDb = sqlite3FindDbName(db, zDb);
154034 }
154035 if( iDb<0 ){
154036 rc = SQLITE_ERROR;
154037 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb);
154038 }else{
154039 db->busyHandler.nBusy = 0;
154040 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
154041 sqlite3Error(db, rc);
154042 }
154043 rc = sqlite3ApiExit(db, rc);
154044
154045 /* If there are no active statements, clear the interrupt flag at this
154046 ** point. */
154047 if( db->nVdbeActive==0 ){
154048 db->u1.isInterrupted = 0;
154049 }
154050
154051 sqlite3_mutex_leave(db->mutex);
154052 return rc;
154053#endif
154054}
154055
154056
154057/*
154058** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
154059** to contains a zero-length string, all attached databases are
154060** checkpointed.
154061*/
154062SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
154063 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
154064 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
154065 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0);
154066}
154067
154068#ifndef SQLITE_OMIT_WAL
154069/*
154070** Run a checkpoint on database iDb. This is a no-op if database iDb is
154071** not currently open in WAL mode.
154072**
154073** If a transaction is open on the database being checkpointed, this
154074** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
154075** an error occurs while running the checkpoint, an SQLite error code is
154076** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
154077**
154078** The mutex on database handle db should be held by the caller. The mutex
154079** associated with the specific b-tree being checkpointed is taken by
154080** this function while the checkpoint is running.
154081**
154082** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
154083** checkpointed. If an error is encountered it is returned immediately -
154084** no attempt is made to checkpoint any remaining databases.
154085**
154086** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART
154087** or TRUNCATE.
154088*/
154089SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
154090 int rc = SQLITE_OK; /* Return code */
154091 int i; /* Used to iterate through attached dbs */
154092 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
154093
154094 assert( sqlite3_mutex_held(db->mutex) );
154095 assert( !pnLog || *pnLog==-1 );
154096 assert( !pnCkpt || *pnCkpt==-1 );
154097
154098 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
154099 if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
154100 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
154101 pnLog = 0;
154102 pnCkpt = 0;
154103 if( rc==SQLITE_BUSY ){
154104 bBusy = 1;
154105 rc = SQLITE_OK;
154106 }
154107 }
154108 }
154109
154110 return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
154111}
154112#endif /* SQLITE_OMIT_WAL */
154113
154114/*
154115** This function returns true if main-memory should be used instead of
154116** a temporary file for transient pager files and statement journals.
154117** The value returned depends on the value of db->temp_store (runtime
154118** parameter) and the compile time value of SQLITE_TEMP_STORE. The
154119** following table describes the relationship between these two values
154120** and this functions return value.
154121**
154122** SQLITE_TEMP_STORE db->temp_store Location of temporary database
154123** ----------------- -------------- ------------------------------
154124** 0 any file (return 0)
154125** 1 1 file (return 0)
154126** 1 2 memory (return 1)
154127** 1 0 file (return 0)
154128** 2 1 file (return 0)
154129** 2 2 memory (return 1)
154130** 2 0 memory (return 1)
154131** 3 any memory (return 1)
154132*/
154133SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
154134#if SQLITE_TEMP_STORE==1
154135 return ( db->temp_store==2 );
154136#endif
154137#if SQLITE_TEMP_STORE==2
154138 return ( db->temp_store!=1 );
154139#endif
154140#if SQLITE_TEMP_STORE==3
154141 UNUSED_PARAMETER(db);
154142 return 1;
154143#endif
154144#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
154145 UNUSED_PARAMETER(db);
154146 return 0;
154147#endif
154148}
154149
154150/*
154151** Return UTF-8 encoded English language explanation of the most recent
154152** error.
154153*/
154154SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
154155 const char *z;
154156 if( !db ){
154157 return sqlite3ErrStr(SQLITE_NOMEM_BKPT);
154158 }
154159 if( !sqlite3SafetyCheckSickOrOk(db) ){
154160 return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
154161 }
154162 sqlite3_mutex_enter(db->mutex);
154163 if( db->mallocFailed ){
154164 z = sqlite3ErrStr(SQLITE_NOMEM_BKPT);
154165 }else{
154166 testcase( db->pErr==0 );
154167 z = (char*)sqlite3_value_text(db->pErr);
154168 assert( !db->mallocFailed );
154169 if( z==0 ){
154170 z = sqlite3ErrStr(db->errCode);
154171 }
154172 }
154173 sqlite3_mutex_leave(db->mutex);
154174 return z;
154175}
154176
154177#ifndef SQLITE_OMIT_UTF16
154178/*
154179** Return UTF-16 encoded English language explanation of the most recent
154180** error.
154181*/
154182SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
154183 static const u16 outOfMem[] = {
154184 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
154185 };
154186 static const u16 misuse[] = {
154187 'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ',
154188 'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ',
154189 'm', 'i', 's', 'u', 's', 'e', 0
154190 };
154191
154192 const void *z;
154193 if( !db ){
154194 return (void *)outOfMem;
154195 }
154196 if( !sqlite3SafetyCheckSickOrOk(db) ){
154197 return (void *)misuse;
154198 }
154199 sqlite3_mutex_enter(db->mutex);
154200 if( db->mallocFailed ){
154201 z = (void *)outOfMem;
154202 }else{
154203 z = sqlite3_value_text16(db->pErr);
154204 if( z==0 ){
154205 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
154206 z = sqlite3_value_text16(db->pErr);
154207 }
154208 /* A malloc() may have failed within the call to sqlite3_value_text16()
154209 ** above. If this is the case, then the db->mallocFailed flag needs to
154210 ** be cleared before returning. Do this directly, instead of via
154211 ** sqlite3ApiExit(), to avoid setting the database handle error message.
154212 */
154213 sqlite3OomClear(db);
154214 }
154215 sqlite3_mutex_leave(db->mutex);
154216 return z;
154217}
154218#endif /* SQLITE_OMIT_UTF16 */
154219
154220/*
154221** Return the most recent error code generated by an SQLite routine. If NULL is
154222** passed to this function, we assume a malloc() failed during sqlite3_open().
154223*/
154224SQLITE_API int sqlite3_errcode(sqlite3 *db){
154225 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
154226 return SQLITE_MISUSE_BKPT;
154227 }
154228 if( !db || db->mallocFailed ){
154229 return SQLITE_NOMEM_BKPT;
154230 }
154231 return db->errCode & db->errMask;
154232}
154233SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
154234 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
154235 return SQLITE_MISUSE_BKPT;
154236 }
154237 if( !db || db->mallocFailed ){
154238 return SQLITE_NOMEM_BKPT;
154239 }
154240 return db->errCode;
154241}
154242SQLITE_API int sqlite3_system_errno(sqlite3 *db){
154243 return db ? db->iSysErrno : 0;
154244}
154245
154246/*
154247** Return a string that describes the kind of error specified in the
154248** argument. For now, this simply calls the internal sqlite3ErrStr()
154249** function.
154250*/
154251SQLITE_API const char *sqlite3_errstr(int rc){
154252 return sqlite3ErrStr(rc);
154253}
154254
154255/*
154256** Create a new collating function for database "db". The name is zName
154257** and the encoding is enc.
154258*/
154259static int createCollation(
154260 sqlite3* db,
154261 const char *zName,
154262 u8 enc,
154263 void* pCtx,
154264 int(*xCompare)(void*,int,const void*,int,const void*),
154265 void(*xDel)(void*)
154266){
154267 CollSeq *pColl;
154268 int enc2;
154269
154270 assert( sqlite3_mutex_held(db->mutex) );
154271
154272 /* If SQLITE_UTF16 is specified as the encoding type, transform this
154273 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
154274 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
154275 */
154276 enc2 = enc;
154277 testcase( enc2==SQLITE_UTF16 );
154278 testcase( enc2==SQLITE_UTF16_ALIGNED );
154279 if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
154280 enc2 = SQLITE_UTF16NATIVE;
154281 }
154282 if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
154283 return SQLITE_MISUSE_BKPT;
154284 }
154285
154286 /* Check if this call is removing or replacing an existing collation
154287 ** sequence. If so, and there are active VMs, return busy. If there
154288 ** are no active VMs, invalidate any pre-compiled statements.
154289 */
154290 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
154291 if( pColl && pColl->xCmp ){
154292 if( db->nVdbeActive ){
154293 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
154294 "unable to delete/modify collation sequence due to active statements");
154295 return SQLITE_BUSY;
154296 }
154297 sqlite3ExpirePreparedStatements(db, 0);
154298
154299 /* If collation sequence pColl was created directly by a call to
154300 ** sqlite3_create_collation, and not generated by synthCollSeq(),
154301 ** then any copies made by synthCollSeq() need to be invalidated.
154302 ** Also, collation destructor - CollSeq.xDel() - function may need
154303 ** to be called.
154304 */
154305 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
154306 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
154307 int j;
154308 for(j=0; j<3; j++){
154309 CollSeq *p = &aColl[j];
154310 if( p->enc==pColl->enc ){
154311 if( p->xDel ){
154312 p->xDel(p->pUser);
154313 }
154314 p->xCmp = 0;
154315 }
154316 }
154317 }
154318 }
154319
154320 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
154321 if( pColl==0 ) return SQLITE_NOMEM_BKPT;
154322 pColl->xCmp = xCompare;
154323 pColl->pUser = pCtx;
154324 pColl->xDel = xDel;
154325 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
154326 sqlite3Error(db, SQLITE_OK);
154327 return SQLITE_OK;
154328}
154329
154330
154331/*
154332** This array defines hard upper bounds on limit values. The
154333** initializer must be kept in sync with the SQLITE_LIMIT_*
154334** #defines in sqlite3.h.
154335*/
154336static const int aHardLimit[] = {
154337 SQLITE_MAX_LENGTH,
154338 SQLITE_MAX_SQL_LENGTH,
154339 SQLITE_MAX_COLUMN,
154340 SQLITE_MAX_EXPR_DEPTH,
154341 SQLITE_MAX_COMPOUND_SELECT,
154342 SQLITE_MAX_VDBE_OP,
154343 SQLITE_MAX_FUNCTION_ARG,
154344 SQLITE_MAX_ATTACHED,
154345 SQLITE_MAX_LIKE_PATTERN_LENGTH,
154346 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
154347 SQLITE_MAX_TRIGGER_DEPTH,
154348 SQLITE_MAX_WORKER_THREADS,
154349};
154350
154351/*
154352** Make sure the hard limits are set to reasonable values
154353*/
154354#if SQLITE_MAX_LENGTH<100
154355# error SQLITE_MAX_LENGTH must be at least 100
154356#endif
154357#if SQLITE_MAX_SQL_LENGTH<100
154358# error SQLITE_MAX_SQL_LENGTH must be at least 100
154359#endif
154360#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
154361# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
154362#endif
154363#if SQLITE_MAX_COMPOUND_SELECT<2
154364# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
154365#endif
154366#if SQLITE_MAX_VDBE_OP<40
154367# error SQLITE_MAX_VDBE_OP must be at least 40
154368#endif
154369#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>127
154370# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 127
154371#endif
154372#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
154373# error SQLITE_MAX_ATTACHED must be between 0 and 125
154374#endif
154375#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
154376# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
154377#endif
154378#if SQLITE_MAX_COLUMN>32767
154379# error SQLITE_MAX_COLUMN must not exceed 32767
154380#endif
154381#if SQLITE_MAX_TRIGGER_DEPTH<1
154382# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
154383#endif
154384#if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50
154385# error SQLITE_MAX_WORKER_THREADS must be between 0 and 50
154386#endif
154387
154388
154389/*
154390** Change the value of a limit. Report the old value.
154391** If an invalid limit index is supplied, report -1.
154392** Make no changes but still report the old value if the
154393** new limit is negative.
154394**
154395** A new lower limit does not shrink existing constructs.
154396** It merely prevents new constructs that exceed the limit
154397** from forming.
154398*/
154399SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
154400 int oldLimit;
154401
154402#ifdef SQLITE_ENABLE_API_ARMOR
154403 if( !sqlite3SafetyCheckOk(db) ){
154404 (void)SQLITE_MISUSE_BKPT;
154405 return -1;
154406 }
154407#endif
154408
154409 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
154410 ** there is a hard upper bound set at compile-time by a C preprocessor
154411 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
154412 ** "_MAX_".)
154413 */
154414 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
154415 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
154416 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
154417 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
154418 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
154419 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
154420 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
154421 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
154422 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
154423 SQLITE_MAX_LIKE_PATTERN_LENGTH );
154424 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
154425 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
154426 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS );
154427 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
154428
154429
154430 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
154431 return -1;
154432 }
154433 oldLimit = db->aLimit[limitId];
154434 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
154435 if( newLimit>aHardLimit[limitId] ){
154436 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
154437 }
154438 db->aLimit[limitId] = newLimit;
154439 }
154440 return oldLimit; /* IMP: R-53341-35419 */
154441}
154442
154443/*
154444** This function is used to parse both URIs and non-URI filenames passed by the
154445** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
154446** URIs specified as part of ATTACH statements.
154447**
154448** The first argument to this function is the name of the VFS to use (or
154449** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
154450** query parameter. The second argument contains the URI (or non-URI filename)
154451** itself. When this function is called the *pFlags variable should contain
154452** the default flags to open the database handle with. The value stored in
154453** *pFlags may be updated before returning if the URI filename contains
154454** "cache=xxx" or "mode=xxx" query parameters.
154455**
154456** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
154457** the VFS that should be used to open the database file. *pzFile is set to
154458** point to a buffer containing the name of the file to open. It is the
154459** responsibility of the caller to eventually call sqlite3_free() to release
154460** this buffer.
154461**
154462** If an error occurs, then an SQLite error code is returned and *pzErrMsg
154463** may be set to point to a buffer containing an English language error
154464** message. It is the responsibility of the caller to eventually release
154465** this buffer by calling sqlite3_free().
154466*/
154467SQLITE_PRIVATE int sqlite3ParseUri(
154468 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
154469 const char *zUri, /* Nul-terminated URI to parse */
154470 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
154471 sqlite3_vfs **ppVfs, /* OUT: VFS to use */
154472 char **pzFile, /* OUT: Filename component of URI */
154473 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
154474){
154475 int rc = SQLITE_OK;
154476 unsigned int flags = *pFlags;
154477 const char *zVfs = zDefaultVfs;
154478 char *zFile;
154479 char c;
154480 int nUri = sqlite3Strlen30(zUri);
154481
154482 assert( *pzErrMsg==0 );
154483
154484 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
154485 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
154486 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
154487 ){
154488 char *zOpt;
154489 int eState; /* Parser state when parsing URI */
154490 int iIn; /* Input character index */
154491 int iOut = 0; /* Output character index */
154492 u64 nByte = nUri+2; /* Bytes of space to allocate */
154493
154494 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
154495 ** method that there may be extra parameters following the file-name. */
154496 flags |= SQLITE_OPEN_URI;
154497
154498 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
154499 zFile = sqlite3_malloc64(nByte);
154500 if( !zFile ) return SQLITE_NOMEM_BKPT;
154501
154502 iIn = 5;
154503#ifdef SQLITE_ALLOW_URI_AUTHORITY
154504 if( strncmp(zUri+5, "///", 3)==0 ){
154505 iIn = 7;
154506 /* The following condition causes URIs with five leading / characters
154507 ** like file://///host/path to be converted into UNCs like //host/path.
154508 ** The correct URI for that UNC has only two or four leading / characters
154509 ** file://host/path or file:////host/path. But 5 leading slashes is a
154510 ** common error, we are told, so we handle it as a special case. */
154511 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
154512 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
154513 iIn = 16;
154514 }
154515#else
154516 /* Discard the scheme and authority segments of the URI. */
154517 if( zUri[5]=='/' && zUri[6]=='/' ){
154518 iIn = 7;
154519 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
154520 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
154521 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
154522 iIn-7, &zUri[7]);
154523 rc = SQLITE_ERROR;
154524 goto parse_uri_out;
154525 }
154526 }
154527#endif
154528
154529 /* Copy the filename and any query parameters into the zFile buffer.
154530 ** Decode %HH escape codes along the way.
154531 **
154532 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
154533 ** on the parsing context. As follows:
154534 **
154535 ** 0: Parsing file-name.
154536 ** 1: Parsing name section of a name=value query parameter.
154537 ** 2: Parsing value section of a name=value query parameter.
154538 */
154539 eState = 0;
154540 while( (c = zUri[iIn])!=0 && c!='#' ){
154541 iIn++;
154542 if( c=='%'
154543 && sqlite3Isxdigit(zUri[iIn])
154544 && sqlite3Isxdigit(zUri[iIn+1])
154545 ){
154546 int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
154547 octet += sqlite3HexToInt(zUri[iIn++]);
154548
154549 assert( octet>=0 && octet<256 );
154550 if( octet==0 ){
154551#ifndef SQLITE_ENABLE_URI_00_ERROR
154552 /* This branch is taken when "%00" appears within the URI. In this
154553 ** case we ignore all text in the remainder of the path, name or
154554 ** value currently being parsed. So ignore the current character
154555 ** and skip to the next "?", "=" or "&", as appropriate. */
154556 while( (c = zUri[iIn])!=0 && c!='#'
154557 && (eState!=0 || c!='?')
154558 && (eState!=1 || (c!='=' && c!='&'))
154559 && (eState!=2 || c!='&')
154560 ){
154561 iIn++;
154562 }
154563 continue;
154564#else
154565 /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */
154566 *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri");
154567 rc = SQLITE_ERROR;
154568 goto parse_uri_out;
154569#endif
154570 }
154571 c = octet;
154572 }else if( eState==1 && (c=='&' || c=='=') ){
154573 if( zFile[iOut-1]==0 ){
154574 /* An empty option name. Ignore this option altogether. */
154575 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
154576 continue;
154577 }
154578 if( c=='&' ){
154579 zFile[iOut++] = '\0';
154580 }else{
154581 eState = 2;
154582 }
154583 c = 0;
154584 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
154585 c = 0;
154586 eState = 1;
154587 }
154588 zFile[iOut++] = c;
154589 }
154590 if( eState==1 ) zFile[iOut++] = '\0';
154591 zFile[iOut++] = '\0';
154592 zFile[iOut++] = '\0';
154593
154594 /* Check if there were any options specified that should be interpreted
154595 ** here. Options that are interpreted here include "vfs" and those that
154596 ** correspond to flags that may be passed to the sqlite3_open_v2()
154597 ** method. */
154598 zOpt = &zFile[sqlite3Strlen30(zFile)+1];
154599 while( zOpt[0] ){
154600 int nOpt = sqlite3Strlen30(zOpt);
154601 char *zVal = &zOpt[nOpt+1];
154602 int nVal = sqlite3Strlen30(zVal);
154603
154604 if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
154605 zVfs = zVal;
154606 }else{
154607 struct OpenMode {
154608 const char *z;
154609 int mode;
154610 } *aMode = 0;
154611 char *zModeType = 0;
154612 int mask = 0;
154613 int limit = 0;
154614
154615 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
154616 static struct OpenMode aCacheMode[] = {
154617 { "shared", SQLITE_OPEN_SHAREDCACHE },
154618 { "private", SQLITE_OPEN_PRIVATECACHE },
154619 { 0, 0 }
154620 };
154621
154622 mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
154623 aMode = aCacheMode;
154624 limit = mask;
154625 zModeType = "cache";
154626 }
154627 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
154628 static struct OpenMode aOpenMode[] = {
154629 { "ro", SQLITE_OPEN_READONLY },
154630 { "rw", SQLITE_OPEN_READWRITE },
154631 { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
154632 { "memory", SQLITE_OPEN_MEMORY },
154633 { 0, 0 }
154634 };
154635
154636 mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
154637 | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
154638 aMode = aOpenMode;
154639 limit = mask & flags;
154640 zModeType = "access";
154641 }
154642
154643 if( aMode ){
154644 int i;
154645 int mode = 0;
154646 for(i=0; aMode[i].z; i++){
154647 const char *z = aMode[i].z;
154648 if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
154649 mode = aMode[i].mode;
154650 break;
154651 }
154652 }
154653 if( mode==0 ){
154654 *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
154655 rc = SQLITE_ERROR;
154656 goto parse_uri_out;
154657 }
154658 if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
154659 *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
154660 zModeType, zVal);
154661 rc = SQLITE_PERM;
154662 goto parse_uri_out;
154663 }
154664 flags = (flags & ~mask) | mode;
154665 }
154666 }
154667
154668 zOpt = &zVal[nVal+1];
154669 }
154670
154671 }else{
154672 zFile = sqlite3_malloc64(nUri+2);
154673 if( !zFile ) return SQLITE_NOMEM_BKPT;
154674 if( nUri ){
154675 memcpy(zFile, zUri, nUri);
154676 }
154677 zFile[nUri] = '\0';
154678 zFile[nUri+1] = '\0';
154679 flags &= ~SQLITE_OPEN_URI;
154680 }
154681
154682 *ppVfs = sqlite3_vfs_find(zVfs);
154683 if( *ppVfs==0 ){
154684 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
154685 rc = SQLITE_ERROR;
154686 }
154687 parse_uri_out:
154688 if( rc!=SQLITE_OK ){
154689 sqlite3_free(zFile);
154690 zFile = 0;
154691 }
154692 *pFlags = flags;
154693 *pzFile = zFile;
154694 return rc;
154695}
154696
154697
154698/*
154699** This routine does the work of opening a database on behalf of
154700** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
154701** is UTF-8 encoded.
154702*/
154703static int openDatabase(
154704 const char *zFilename, /* Database filename UTF-8 encoded */
154705 sqlite3 **ppDb, /* OUT: Returned database handle */
154706 unsigned int flags, /* Operational flags */
154707 const char *zVfs /* Name of the VFS to use */
154708){
154709 sqlite3 *db; /* Store allocated handle here */
154710 int rc; /* Return code */
154711 int isThreadsafe; /* True for threadsafe connections */
154712 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
154713 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
154714
154715#ifdef SQLITE_ENABLE_API_ARMOR
154716 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
154717#endif
154718 *ppDb = 0;
154719#ifndef SQLITE_OMIT_AUTOINIT
154720 rc = sqlite3_initialize();
154721 if( rc ) return rc;
154722#endif
154723
154724 if( sqlite3GlobalConfig.bCoreMutex==0 ){
154725 isThreadsafe = 0;
154726 }else if( flags & SQLITE_OPEN_NOMUTEX ){
154727 isThreadsafe = 0;
154728 }else if( flags & SQLITE_OPEN_FULLMUTEX ){
154729 isThreadsafe = 1;
154730 }else{
154731 isThreadsafe = sqlite3GlobalConfig.bFullMutex;
154732 }
154733
154734 if( flags & SQLITE_OPEN_PRIVATECACHE ){
154735 flags &= ~SQLITE_OPEN_SHAREDCACHE;
154736 }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
154737 flags |= SQLITE_OPEN_SHAREDCACHE;
154738 }
154739
154740 /* Remove harmful bits from the flags parameter
154741 **
154742 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
154743 ** dealt with in the previous code block. Besides these, the only
154744 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
154745 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
154746 ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits. Silently mask
154747 ** off all other flags.
154748 */
154749 flags &= ~( SQLITE_OPEN_DELETEONCLOSE |
154750 SQLITE_OPEN_EXCLUSIVE |
154751 SQLITE_OPEN_MAIN_DB |
154752 SQLITE_OPEN_TEMP_DB |
154753 SQLITE_OPEN_TRANSIENT_DB |
154754 SQLITE_OPEN_MAIN_JOURNAL |
154755 SQLITE_OPEN_TEMP_JOURNAL |
154756 SQLITE_OPEN_SUBJOURNAL |
154757 SQLITE_OPEN_MASTER_JOURNAL |
154758 SQLITE_OPEN_NOMUTEX |
154759 SQLITE_OPEN_FULLMUTEX |
154760 SQLITE_OPEN_WAL
154761 );
154762
154763 /* Allocate the sqlite data structure */
154764 db = sqlite3MallocZero( sizeof(sqlite3) );
154765 if( db==0 ) goto opendb_out;
154766 if( isThreadsafe
154767#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
154768 || sqlite3GlobalConfig.bCoreMutex
154769#endif
154770 ){
154771 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
154772 if( db->mutex==0 ){
154773 sqlite3_free(db);
154774 db = 0;
154775 goto opendb_out;
154776 }
154777 if( isThreadsafe==0 ){
154778 sqlite3MutexWarnOnContention(db->mutex);
154779 }
154780 }
154781 sqlite3_mutex_enter(db->mutex);
154782 db->errMask = 0xff;
154783 db->nDb = 2;
154784 db->magic = SQLITE_MAGIC_BUSY;
154785 db->aDb = db->aDbStatic;
154786 db->lookaside.bDisable = 1;
154787
154788 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
154789 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
154790 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
154791 db->autoCommit = 1;
154792 db->nextAutovac = -1;
154793 db->szMmap = sqlite3GlobalConfig.szMmap;
154794 db->nextPagesize = 0;
154795 db->nMaxSorterMmap = 0x7FFFFFFF;
154796 db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill
154797#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
154798 | SQLITE_AutoIndex
154799#endif
154800#if SQLITE_DEFAULT_CKPTFULLFSYNC
154801 | SQLITE_CkptFullFSync
154802#endif
154803#if SQLITE_DEFAULT_FILE_FORMAT<4
154804 | SQLITE_LegacyFileFmt
154805#endif
154806#ifdef SQLITE_ENABLE_LOAD_EXTENSION
154807 | SQLITE_LoadExtension
154808#endif
154809#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
154810 | SQLITE_RecTriggers
154811#endif
154812#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
154813 | SQLITE_ForeignKeys
154814#endif
154815#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
154816 | SQLITE_ReverseOrder
154817#endif
154818#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
154819 | SQLITE_CellSizeCk
154820#endif
154821#if defined(SQLITE_ENABLE_FTS3_TOKENIZER)
154822 | SQLITE_Fts3Tokenizer
154823#endif
154824#if defined(SQLITE_ENABLE_QPSG)
154825 | SQLITE_EnableQPSG
154826#endif
154827 ;
154828 sqlite3HashInit(&db->aCollSeq);
154829#ifndef SQLITE_OMIT_VIRTUALTABLE
154830 sqlite3HashInit(&db->aModule);
154831#endif
154832
154833 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
154834 ** and UTF-16, so add a version for each to avoid any unnecessary
154835 ** conversions. The only error that can occur here is a malloc() failure.
154836 **
154837 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
154838 ** functions:
154839 */
154840 createCollation(db, sqlite3StrBINARY, SQLITE_UTF8, 0, binCollFunc, 0);
154841 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE, 0, binCollFunc, 0);
154842 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE, 0, binCollFunc, 0);
154843 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
154844 createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
154845 if( db->mallocFailed ){
154846 goto opendb_out;
154847 }
154848 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
154849 ** strings is BINARY.
154850 */
154851 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, sqlite3StrBINARY, 0);
154852 assert( db->pDfltColl!=0 );
154853
154854 /* Parse the filename/URI argument
154855 **
154856 ** Only allow sensible combinations of bits in the flags argument.
154857 ** Throw an error if any non-sense combination is used. If we
154858 ** do not block illegal combinations here, it could trigger
154859 ** assert() statements in deeper layers. Sensible combinations
154860 ** are:
154861 **
154862 ** 1: SQLITE_OPEN_READONLY
154863 ** 2: SQLITE_OPEN_READWRITE
154864 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
154865 */
154866 db->openFlags = flags;
154867 assert( SQLITE_OPEN_READONLY == 0x01 );
154868 assert( SQLITE_OPEN_READWRITE == 0x02 );
154869 assert( SQLITE_OPEN_CREATE == 0x04 );
154870 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
154871 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
154872 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
154873 if( ((1<<(flags&7)) & 0x46)==0 ){
154874 rc = SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */
154875 }else{
154876 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
154877 }
154878 if( rc!=SQLITE_OK ){
154879 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
154880 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
154881 sqlite3_free(zErrMsg);
154882 goto opendb_out;
154883 }
154884
154885 /* Open the backend database driver */
154886 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
154887 flags | SQLITE_OPEN_MAIN_DB);
154888 if( rc!=SQLITE_OK ){
154889 if( rc==SQLITE_IOERR_NOMEM ){
154890 rc = SQLITE_NOMEM_BKPT;
154891 }
154892 sqlite3Error(db, rc);
154893 goto opendb_out;
154894 }
154895 sqlite3BtreeEnter(db->aDb[0].pBt);
154896 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
154897 if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db);
154898 sqlite3BtreeLeave(db->aDb[0].pBt);
154899 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
154900
154901 /* The default safety_level for the main database is FULL; for the temp
154902 ** database it is OFF. This matches the pager layer defaults.
154903 */
154904 db->aDb[0].zDbSName = "main";
154905 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
154906 db->aDb[1].zDbSName = "temp";
154907 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF;
154908
154909 db->magic = SQLITE_MAGIC_OPEN;
154910 if( db->mallocFailed ){
154911 goto opendb_out;
154912 }
154913
154914 /* Register all built-in functions, but do not attempt to read the
154915 ** database schema yet. This is delayed until the first time the database
154916 ** is accessed.
154917 */
154918 sqlite3Error(db, SQLITE_OK);
154919 sqlite3RegisterPerConnectionBuiltinFunctions(db);
154920 rc = sqlite3_errcode(db);
154921
154922#ifdef SQLITE_ENABLE_FTS5
154923 /* Register any built-in FTS5 module before loading the automatic
154924 ** extensions. This allows automatic extensions to register FTS5
154925 ** tokenizers and auxiliary functions. */
154926 if( !db->mallocFailed && rc==SQLITE_OK ){
154927 rc = sqlite3Fts5Init(db);
154928 }
154929#endif
154930
154931 /* Load automatic extensions - extensions that have been registered
154932 ** using the sqlite3_automatic_extension() API.
154933 */
154934 if( rc==SQLITE_OK ){
154935 sqlite3AutoLoadExtensions(db);
154936 rc = sqlite3_errcode(db);
154937 if( rc!=SQLITE_OK ){
154938 goto opendb_out;
154939 }
154940 }
154941
154942#ifdef SQLITE_ENABLE_FTS1
154943 if( !db->mallocFailed ){
154944 extern int sqlite3Fts1Init(sqlite3*);
154945 rc = sqlite3Fts1Init(db);
154946 }
154947#endif
154948
154949#ifdef SQLITE_ENABLE_FTS2
154950 if( !db->mallocFailed && rc==SQLITE_OK ){
154951 extern int sqlite3Fts2Init(sqlite3*);
154952 rc = sqlite3Fts2Init(db);
154953 }
154954#endif
154955
154956#ifdef SQLITE_ENABLE_FTS3 /* automatically defined by SQLITE_ENABLE_FTS4 */
154957 if( !db->mallocFailed && rc==SQLITE_OK ){
154958 rc = sqlite3Fts3Init(db);
154959 }
154960#endif
154961
154962#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
154963 if( !db->mallocFailed && rc==SQLITE_OK ){
154964 rc = sqlite3IcuInit(db);
154965 }
154966#endif
154967
154968#ifdef SQLITE_ENABLE_RTREE
154969 if( !db->mallocFailed && rc==SQLITE_OK){
154970 rc = sqlite3RtreeInit(db);
154971 }
154972#endif
154973
154974#ifdef SQLITE_ENABLE_DBPAGE_VTAB
154975 if( !db->mallocFailed && rc==SQLITE_OK){
154976 rc = sqlite3DbpageRegister(db);
154977 }
154978#endif
154979
154980#ifdef SQLITE_ENABLE_DBSTAT_VTAB
154981 if( !db->mallocFailed && rc==SQLITE_OK){
154982 rc = sqlite3DbstatRegister(db);
154983 }
154984#endif
154985
154986#ifdef SQLITE_ENABLE_JSON1
154987 if( !db->mallocFailed && rc==SQLITE_OK){
154988 rc = sqlite3Json1Init(db);
154989 }
154990#endif
154991
154992#ifdef SQLITE_ENABLE_STMTVTAB
154993 if( !db->mallocFailed && rc==SQLITE_OK){
154994 rc = sqlite3StmtVtabInit(db);
154995 }
154996#endif
154997
154998 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
154999 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
155000 ** mode. Doing nothing at all also makes NORMAL the default.
155001 */
155002#ifdef SQLITE_DEFAULT_LOCKING_MODE
155003 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
155004 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
155005 SQLITE_DEFAULT_LOCKING_MODE);
155006#endif
155007
155008 if( rc ) sqlite3Error(db, rc);
155009
155010 /* Enable the lookaside-malloc subsystem */
155011 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
155012 sqlite3GlobalConfig.nLookaside);
155013
155014 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
155015
155016opendb_out:
155017 if( db ){
155018 assert( db->mutex!=0 || isThreadsafe==0
155019 || sqlite3GlobalConfig.bFullMutex==0 );
155020 sqlite3_mutex_leave(db->mutex);
155021 }
155022 rc = sqlite3_errcode(db);
155023 assert( db!=0 || rc==SQLITE_NOMEM );
155024 if( rc==SQLITE_NOMEM ){
155025 sqlite3_close(db);
155026 db = 0;
155027 }else if( rc!=SQLITE_OK ){
155028 db->magic = SQLITE_MAGIC_SICK;
155029 }
155030 *ppDb = db;
155031#ifdef SQLITE_ENABLE_SQLLOG
155032 if( sqlite3GlobalConfig.xSqllog ){
155033 /* Opening a db handle. Fourth parameter is passed 0. */
155034 void *pArg = sqlite3GlobalConfig.pSqllogArg;
155035 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
155036 }
155037#endif
155038#if defined(SQLITE_HAS_CODEC)
155039 if( rc==SQLITE_OK ){
155040 const char *zKey;
155041 if( (zKey = sqlite3_uri_parameter(zOpen, "hexkey"))!=0 && zKey[0] ){
155042 u8 iByte;
155043 int i;
155044 char zDecoded[40];
155045 for(i=0, iByte=0; i<sizeof(zDecoded)*2 && sqlite3Isxdigit(zKey[i]); i++){
155046 iByte = (iByte<<4) + sqlite3HexToInt(zKey[i]);
155047 if( (i&1)!=0 ) zDecoded[i/2] = iByte;
155048 }
155049 sqlite3_key_v2(db, 0, zDecoded, i/2);
155050 }else if( (zKey = sqlite3_uri_parameter(zOpen, "key"))!=0 ){
155051 sqlite3_key_v2(db, 0, zKey, sqlite3Strlen30(zKey));
155052 }
155053 }
155054#endif
155055 sqlite3_free(zOpen);
155056 return rc & 0xff;
155057}
155058
155059/*
155060** Open a new database handle.
155061*/
155062SQLITE_API int sqlite3_open(
155063 const char *zFilename,
155064 sqlite3 **ppDb
155065){
155066 return openDatabase(zFilename, ppDb,
155067 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
155068}
155069SQLITE_API int sqlite3_open_v2(
155070 const char *filename, /* Database filename (UTF-8) */
155071 sqlite3 **ppDb, /* OUT: SQLite db handle */
155072 int flags, /* Flags */
155073 const char *zVfs /* Name of VFS module to use */
155074){
155075 return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
155076}
155077
155078#ifndef SQLITE_OMIT_UTF16
155079/*
155080** Open a new database handle.
155081*/
155082SQLITE_API int sqlite3_open16(
155083 const void *zFilename,
155084 sqlite3 **ppDb
155085){
155086 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
155087 sqlite3_value *pVal;
155088 int rc;
155089
155090#ifdef SQLITE_ENABLE_API_ARMOR
155091 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
155092#endif
155093 *ppDb = 0;
155094#ifndef SQLITE_OMIT_AUTOINIT
155095 rc = sqlite3_initialize();
155096 if( rc ) return rc;
155097#endif
155098 if( zFilename==0 ) zFilename = "\000\000";
155099 pVal = sqlite3ValueNew(0);
155100 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
155101 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
155102 if( zFilename8 ){
155103 rc = openDatabase(zFilename8, ppDb,
155104 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
155105 assert( *ppDb || rc==SQLITE_NOMEM );
155106 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
155107 SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE;
155108 }
155109 }else{
155110 rc = SQLITE_NOMEM_BKPT;
155111 }
155112 sqlite3ValueFree(pVal);
155113
155114 return rc & 0xff;
155115}
155116#endif /* SQLITE_OMIT_UTF16 */
155117
155118/*
155119** Register a new collation sequence with the database handle db.
155120*/
155121SQLITE_API int sqlite3_create_collation(
155122 sqlite3* db,
155123 const char *zName,
155124 int enc,
155125 void* pCtx,
155126 int(*xCompare)(void*,int,const void*,int,const void*)
155127){
155128 return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
155129}
155130
155131/*
155132** Register a new collation sequence with the database handle db.
155133*/
155134SQLITE_API int sqlite3_create_collation_v2(
155135 sqlite3* db,
155136 const char *zName,
155137 int enc,
155138 void* pCtx,
155139 int(*xCompare)(void*,int,const void*,int,const void*),
155140 void(*xDel)(void*)
155141){
155142 int rc;
155143
155144#ifdef SQLITE_ENABLE_API_ARMOR
155145 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
155146#endif
155147 sqlite3_mutex_enter(db->mutex);
155148 assert( !db->mallocFailed );
155149 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
155150 rc = sqlite3ApiExit(db, rc);
155151 sqlite3_mutex_leave(db->mutex);
155152 return rc;
155153}
155154
155155#ifndef SQLITE_OMIT_UTF16
155156/*
155157** Register a new collation sequence with the database handle db.
155158*/
155159SQLITE_API int sqlite3_create_collation16(
155160 sqlite3* db,
155161 const void *zName,
155162 int enc,
155163 void* pCtx,
155164 int(*xCompare)(void*,int,const void*,int,const void*)
155165){
155166 int rc = SQLITE_OK;
155167 char *zName8;
155168
155169#ifdef SQLITE_ENABLE_API_ARMOR
155170 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
155171#endif
155172 sqlite3_mutex_enter(db->mutex);
155173 assert( !db->mallocFailed );
155174 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
155175 if( zName8 ){
155176 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
155177 sqlite3DbFree(db, zName8);
155178 }
155179 rc = sqlite3ApiExit(db, rc);
155180 sqlite3_mutex_leave(db->mutex);
155181 return rc;
155182}
155183#endif /* SQLITE_OMIT_UTF16 */
155184
155185/*
155186** Register a collation sequence factory callback with the database handle
155187** db. Replace any previously installed collation sequence factory.
155188*/
155189SQLITE_API int sqlite3_collation_needed(
155190 sqlite3 *db,
155191 void *pCollNeededArg,
155192 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
155193){
155194#ifdef SQLITE_ENABLE_API_ARMOR
155195 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
155196#endif
155197 sqlite3_mutex_enter(db->mutex);
155198 db->xCollNeeded = xCollNeeded;
155199 db->xCollNeeded16 = 0;
155200 db->pCollNeededArg = pCollNeededArg;
155201 sqlite3_mutex_leave(db->mutex);
155202 return SQLITE_OK;
155203}
155204
155205#ifndef SQLITE_OMIT_UTF16
155206/*
155207** Register a collation sequence factory callback with the database handle
155208** db. Replace any previously installed collation sequence factory.
155209*/
155210SQLITE_API int sqlite3_collation_needed16(
155211 sqlite3 *db,
155212 void *pCollNeededArg,
155213 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
155214){
155215#ifdef SQLITE_ENABLE_API_ARMOR
155216 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
155217#endif
155218 sqlite3_mutex_enter(db->mutex);
155219 db->xCollNeeded = 0;
155220 db->xCollNeeded16 = xCollNeeded16;
155221 db->pCollNeededArg = pCollNeededArg;
155222 sqlite3_mutex_leave(db->mutex);
155223 return SQLITE_OK;
155224}
155225#endif /* SQLITE_OMIT_UTF16 */
155226
155227#ifndef SQLITE_OMIT_DEPRECATED
155228/*
155229** This function is now an anachronism. It used to be used to recover from a
155230** malloc() failure, but SQLite now does this automatically.
155231*/
155232SQLITE_API int sqlite3_global_recover(void){
155233 return SQLITE_OK;
155234}
155235#endif
155236
155237/*
155238** Test to see whether or not the database connection is in autocommit
155239** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
155240** by default. Autocommit is disabled by a BEGIN statement and reenabled
155241** by the next COMMIT or ROLLBACK.
155242*/
155243SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
155244#ifdef SQLITE_ENABLE_API_ARMOR
155245 if( !sqlite3SafetyCheckOk(db) ){
155246 (void)SQLITE_MISUSE_BKPT;
155247 return 0;
155248 }
155249#endif
155250 return db->autoCommit;
155251}
155252
155253/*
155254** The following routines are substitutes for constants SQLITE_CORRUPT,
155255** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error
155256** constants. They serve two purposes:
155257**
155258** 1. Serve as a convenient place to set a breakpoint in a debugger
155259** to detect when version error conditions occurs.
155260**
155261** 2. Invoke sqlite3_log() to provide the source code location where
155262** a low-level error is first detected.
155263*/
155264SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType){
155265 sqlite3_log(iErr, "%s at line %d of [%.10s]",
155266 zType, lineno, 20+sqlite3_sourceid());
155267 return iErr;
155268}
155269SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
155270 testcase( sqlite3GlobalConfig.xLog!=0 );
155271 return sqlite3ReportError(SQLITE_CORRUPT, lineno, "database corruption");
155272}
155273SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
155274 testcase( sqlite3GlobalConfig.xLog!=0 );
155275 return sqlite3ReportError(SQLITE_MISUSE, lineno, "misuse");
155276}
155277SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
155278 testcase( sqlite3GlobalConfig.xLog!=0 );
155279 return sqlite3ReportError(SQLITE_CANTOPEN, lineno, "cannot open file");
155280}
155281#ifdef SQLITE_DEBUG
155282SQLITE_PRIVATE int sqlite3CorruptPgnoError(int lineno, Pgno pgno){
155283 char zMsg[100];
155284 sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
155285 testcase( sqlite3GlobalConfig.xLog!=0 );
155286 return sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
155287}
155288SQLITE_PRIVATE int sqlite3NomemError(int lineno){
155289 testcase( sqlite3GlobalConfig.xLog!=0 );
155290 return sqlite3ReportError(SQLITE_NOMEM, lineno, "OOM");
155291}
155292SQLITE_PRIVATE int sqlite3IoerrnomemError(int lineno){
155293 testcase( sqlite3GlobalConfig.xLog!=0 );
155294 return sqlite3ReportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error");
155295}
155296#endif
155297
155298#ifndef SQLITE_OMIT_DEPRECATED
155299/*
155300** This is a convenience routine that makes sure that all thread-specific
155301** data for this thread has been deallocated.
155302**
155303** SQLite no longer uses thread-specific data so this routine is now a
155304** no-op. It is retained for historical compatibility.
155305*/
155306SQLITE_API void sqlite3_thread_cleanup(void){
155307}
155308#endif
155309
155310/*
155311** Return meta information about a specific column of a database table.
155312** See comment in sqlite3.h (sqlite.h.in) for details.
155313*/
155314SQLITE_API int sqlite3_table_column_metadata(
155315 sqlite3 *db, /* Connection handle */
155316 const char *zDbName, /* Database name or NULL */
155317 const char *zTableName, /* Table name */
155318 const char *zColumnName, /* Column name */
155319 char const **pzDataType, /* OUTPUT: Declared data type */
155320 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
155321 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
155322 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
155323 int *pAutoinc /* OUTPUT: True if column is auto-increment */
155324){
155325 int rc;
155326 char *zErrMsg = 0;
155327 Table *pTab = 0;
155328 Column *pCol = 0;
155329 int iCol = 0;
155330 char const *zDataType = 0;
155331 char const *zCollSeq = 0;
155332 int notnull = 0;
155333 int primarykey = 0;
155334 int autoinc = 0;
155335
155336
155337#ifdef SQLITE_ENABLE_API_ARMOR
155338 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
155339 return SQLITE_MISUSE_BKPT;
155340 }
155341#endif
155342
155343 /* Ensure the database schema has been loaded */
155344 sqlite3_mutex_enter(db->mutex);
155345 sqlite3BtreeEnterAll(db);
155346 rc = sqlite3Init(db, &zErrMsg);
155347 if( SQLITE_OK!=rc ){
155348 goto error_out;
155349 }
155350
155351 /* Locate the table in question */
155352 pTab = sqlite3FindTable(db, zTableName, zDbName);
155353 if( !pTab || pTab->pSelect ){
155354 pTab = 0;
155355 goto error_out;
155356 }
155357
155358 /* Find the column for which info is requested */
155359 if( zColumnName==0 ){
155360 /* Query for existance of table only */
155361 }else{
155362 for(iCol=0; iCol<pTab->nCol; iCol++){
155363 pCol = &pTab->aCol[iCol];
155364 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
155365 break;
155366 }
155367 }
155368 if( iCol==pTab->nCol ){
155369 if( HasRowid(pTab) && sqlite3IsRowid(zColumnName) ){
155370 iCol = pTab->iPKey;
155371 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
155372 }else{
155373 pTab = 0;
155374 goto error_out;
155375 }
155376 }
155377 }
155378
155379 /* The following block stores the meta information that will be returned
155380 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
155381 ** and autoinc. At this point there are two possibilities:
155382 **
155383 ** 1. The specified column name was rowid", "oid" or "_rowid_"
155384 ** and there is no explicitly declared IPK column.
155385 **
155386 ** 2. The table is not a view and the column name identified an
155387 ** explicitly declared column. Copy meta information from *pCol.
155388 */
155389 if( pCol ){
155390 zDataType = sqlite3ColumnType(pCol,0);
155391 zCollSeq = pCol->zColl;
155392 notnull = pCol->notNull!=0;
155393 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
155394 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
155395 }else{
155396 zDataType = "INTEGER";
155397 primarykey = 1;
155398 }
155399 if( !zCollSeq ){
155400 zCollSeq = sqlite3StrBINARY;
155401 }
155402
155403error_out:
155404 sqlite3BtreeLeaveAll(db);
155405
155406 /* Whether the function call succeeded or failed, set the output parameters
155407 ** to whatever their local counterparts contain. If an error did occur,
155408 ** this has the effect of zeroing all output parameters.
155409 */
155410 if( pzDataType ) *pzDataType = zDataType;
155411 if( pzCollSeq ) *pzCollSeq = zCollSeq;
155412 if( pNotNull ) *pNotNull = notnull;
155413 if( pPrimaryKey ) *pPrimaryKey = primarykey;
155414 if( pAutoinc ) *pAutoinc = autoinc;
155415
155416 if( SQLITE_OK==rc && !pTab ){
155417 sqlite3DbFree(db, zErrMsg);
155418 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
155419 zColumnName);
155420 rc = SQLITE_ERROR;
155421 }
155422 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
155423 sqlite3DbFree(db, zErrMsg);
155424 rc = sqlite3ApiExit(db, rc);
155425 sqlite3_mutex_leave(db->mutex);
155426 return rc;
155427}
155428
155429/*
155430** Sleep for a little while. Return the amount of time slept.
155431*/
155432SQLITE_API int sqlite3_sleep(int ms){
155433 sqlite3_vfs *pVfs;
155434 int rc;
155435 pVfs = sqlite3_vfs_find(0);
155436 if( pVfs==0 ) return 0;
155437
155438 /* This function works in milliseconds, but the underlying OsSleep()
155439 ** API uses microseconds. Hence the 1000's.
155440 */
155441 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
155442 return rc;
155443}
155444
155445/*
155446** Enable or disable the extended result codes.
155447*/
155448SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
155449#ifdef SQLITE_ENABLE_API_ARMOR
155450 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
155451#endif
155452 sqlite3_mutex_enter(db->mutex);
155453 db->errMask = onoff ? 0xffffffff : 0xff;
155454 sqlite3_mutex_leave(db->mutex);
155455 return SQLITE_OK;
155456}
155457
155458/*
155459** Invoke the xFileControl method on a particular database.
155460*/
155461SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
155462 int rc = SQLITE_ERROR;
155463 Btree *pBtree;
155464
155465#ifdef SQLITE_ENABLE_API_ARMOR
155466 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
155467#endif
155468 sqlite3_mutex_enter(db->mutex);
155469 pBtree = sqlite3DbNameToBtree(db, zDbName);
155470 if( pBtree ){
155471 Pager *pPager;
155472 sqlite3_file *fd;
155473 sqlite3BtreeEnter(pBtree);
155474 pPager = sqlite3BtreePager(pBtree);
155475 assert( pPager!=0 );
155476 fd = sqlite3PagerFile(pPager);
155477 assert( fd!=0 );
155478 if( op==SQLITE_FCNTL_FILE_POINTER ){
155479 *(sqlite3_file**)pArg = fd;
155480 rc = SQLITE_OK;
155481 }else if( op==SQLITE_FCNTL_VFS_POINTER ){
155482 *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
155483 rc = SQLITE_OK;
155484 }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){
155485 *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
155486 rc = SQLITE_OK;
155487 }else if( op==SQLITE_FCNTL_DATA_VERSION ){
155488 *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager);
155489 rc = SQLITE_OK;
155490 }else{
155491 rc = sqlite3OsFileControl(fd, op, pArg);
155492 }
155493 sqlite3BtreeLeave(pBtree);
155494 }
155495 sqlite3_mutex_leave(db->mutex);
155496 return rc;
155497}
155498
155499/*
155500** Interface to the testing logic.
155501*/
155502SQLITE_API int sqlite3_test_control(int op, ...){
155503 int rc = 0;
155504#ifdef SQLITE_UNTESTABLE
155505 UNUSED_PARAMETER(op);
155506#else
155507 va_list ap;
155508 va_start(ap, op);
155509 switch( op ){
155510
155511 /*
155512 ** Save the current state of the PRNG.
155513 */
155514 case SQLITE_TESTCTRL_PRNG_SAVE: {
155515 sqlite3PrngSaveState();
155516 break;
155517 }
155518
155519 /*
155520 ** Restore the state of the PRNG to the last state saved using
155521 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
155522 ** this verb acts like PRNG_RESET.
155523 */
155524 case SQLITE_TESTCTRL_PRNG_RESTORE: {
155525 sqlite3PrngRestoreState();
155526 break;
155527 }
155528
155529 /*
155530 ** Reset the PRNG back to its uninitialized state. The next call
155531 ** to sqlite3_randomness() will reseed the PRNG using a single call
155532 ** to the xRandomness method of the default VFS.
155533 */
155534 case SQLITE_TESTCTRL_PRNG_RESET: {
155535 sqlite3_randomness(0,0);
155536 break;
155537 }
155538
155539 /*
155540 ** sqlite3_test_control(BITVEC_TEST, size, program)
155541 **
155542 ** Run a test against a Bitvec object of size. The program argument
155543 ** is an array of integers that defines the test. Return -1 on a
155544 ** memory allocation error, 0 on success, or non-zero for an error.
155545 ** See the sqlite3BitvecBuiltinTest() for additional information.
155546 */
155547 case SQLITE_TESTCTRL_BITVEC_TEST: {
155548 int sz = va_arg(ap, int);
155549 int *aProg = va_arg(ap, int*);
155550 rc = sqlite3BitvecBuiltinTest(sz, aProg);
155551 break;
155552 }
155553
155554 /*
155555 ** sqlite3_test_control(FAULT_INSTALL, xCallback)
155556 **
155557 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
155558 ** if xCallback is not NULL.
155559 **
155560 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
155561 ** is called immediately after installing the new callback and the return
155562 ** value from sqlite3FaultSim(0) becomes the return from
155563 ** sqlite3_test_control().
155564 */
155565 case SQLITE_TESTCTRL_FAULT_INSTALL: {
155566 /* MSVC is picky about pulling func ptrs from va lists.
155567 ** http://support.microsoft.com/kb/47961
155568 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
155569 */
155570 typedef int(*TESTCALLBACKFUNC_t)(int);
155571 sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
155572 rc = sqlite3FaultSim(0);
155573 break;
155574 }
155575
155576 /*
155577 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
155578 **
155579 ** Register hooks to call to indicate which malloc() failures
155580 ** are benign.
155581 */
155582 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
155583 typedef void (*void_function)(void);
155584 void_function xBenignBegin;
155585 void_function xBenignEnd;
155586 xBenignBegin = va_arg(ap, void_function);
155587 xBenignEnd = va_arg(ap, void_function);
155588 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
155589 break;
155590 }
155591
155592 /*
155593 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
155594 **
155595 ** Set the PENDING byte to the value in the argument, if X>0.
155596 ** Make no changes if X==0. Return the value of the pending byte
155597 ** as it existing before this routine was called.
155598 **
155599 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
155600 ** an incompatible database file format. Changing the PENDING byte
155601 ** while any database connection is open results in undefined and
155602 ** deleterious behavior.
155603 */
155604 case SQLITE_TESTCTRL_PENDING_BYTE: {
155605 rc = PENDING_BYTE;
155606#ifndef SQLITE_OMIT_WSD
155607 {
155608 unsigned int newVal = va_arg(ap, unsigned int);
155609 if( newVal ) sqlite3PendingByte = newVal;
155610 }
155611#endif
155612 break;
155613 }
155614
155615 /*
155616 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
155617 **
155618 ** This action provides a run-time test to see whether or not
155619 ** assert() was enabled at compile-time. If X is true and assert()
155620 ** is enabled, then the return value is true. If X is true and
155621 ** assert() is disabled, then the return value is zero. If X is
155622 ** false and assert() is enabled, then the assertion fires and the
155623 ** process aborts. If X is false and assert() is disabled, then the
155624 ** return value is zero.
155625 */
155626 case SQLITE_TESTCTRL_ASSERT: {
155627 volatile int x = 0;
155628 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
155629 rc = x;
155630 break;
155631 }
155632
155633
155634 /*
155635 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
155636 **
155637 ** This action provides a run-time test to see how the ALWAYS and
155638 ** NEVER macros were defined at compile-time.
155639 **
155640 ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
155641 **
155642 ** The recommended test is X==2. If the return value is 2, that means
155643 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
155644 ** default setting. If the return value is 1, then ALWAYS() is either
155645 ** hard-coded to true or else it asserts if its argument is false.
155646 ** The first behavior (hard-coded to true) is the case if
155647 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
155648 ** behavior (assert if the argument to ALWAYS() is false) is the case if
155649 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
155650 **
155651 ** The run-time test procedure might look something like this:
155652 **
155653 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
155654 ** // ALWAYS() and NEVER() are no-op pass-through macros
155655 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
155656 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
155657 ** }else{
155658 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
155659 ** }
155660 */
155661 case SQLITE_TESTCTRL_ALWAYS: {
155662 int x = va_arg(ap,int);
155663 rc = x ? ALWAYS(x) : 0;
155664 break;
155665 }
155666
155667 /*
155668 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
155669 **
155670 ** The integer returned reveals the byte-order of the computer on which
155671 ** SQLite is running:
155672 **
155673 ** 1 big-endian, determined at run-time
155674 ** 10 little-endian, determined at run-time
155675 ** 432101 big-endian, determined at compile-time
155676 ** 123410 little-endian, determined at compile-time
155677 */
155678 case SQLITE_TESTCTRL_BYTEORDER: {
155679 rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN;
155680 break;
155681 }
155682
155683 /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
155684 **
155685 ** Set the nReserve size to N for the main database on the database
155686 ** connection db.
155687 */
155688 case SQLITE_TESTCTRL_RESERVE: {
155689 sqlite3 *db = va_arg(ap, sqlite3*);
155690 int x = va_arg(ap,int);
155691 sqlite3_mutex_enter(db->mutex);
155692 sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
155693 sqlite3_mutex_leave(db->mutex);
155694 break;
155695 }
155696
155697 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
155698 **
155699 ** Enable or disable various optimizations for testing purposes. The
155700 ** argument N is a bitmask of optimizations to be disabled. For normal
155701 ** operation N should be 0. The idea is that a test program (like the
155702 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
155703 ** with various optimizations disabled to verify that the same answer
155704 ** is obtained in every case.
155705 */
155706 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
155707 sqlite3 *db = va_arg(ap, sqlite3*);
155708 db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
155709 break;
155710 }
155711
155712 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
155713 **
155714 ** If parameter onoff is non-zero, configure the wrappers so that all
155715 ** subsequent calls to localtime() and variants fail. If onoff is zero,
155716 ** undo this setting.
155717 */
155718 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
155719 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
155720 break;
155721 }
155722
155723 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
155724 **
155725 ** Set or clear a flag that indicates that the database file is always well-
155726 ** formed and never corrupt. This flag is clear by default, indicating that
155727 ** database files might have arbitrary corruption. Setting the flag during
155728 ** testing causes certain assert() statements in the code to be activated
155729 ** that demonstrat invariants on well-formed database files.
155730 */
155731 case SQLITE_TESTCTRL_NEVER_CORRUPT: {
155732 sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
155733 break;
155734 }
155735
155736 /* Set the threshold at which OP_Once counters reset back to zero.
155737 ** By default this is 0x7ffffffe (over 2 billion), but that value is
155738 ** too big to test in a reasonable amount of time, so this control is
155739 ** provided to set a small and easily reachable reset value.
155740 */
155741 case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD: {
155742 sqlite3GlobalConfig.iOnceResetThreshold = va_arg(ap, int);
155743 break;
155744 }
155745
155746 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
155747 **
155748 ** Set the VDBE coverage callback function to xCallback with context
155749 ** pointer ptr.
155750 */
155751 case SQLITE_TESTCTRL_VDBE_COVERAGE: {
155752#ifdef SQLITE_VDBE_COVERAGE
155753 typedef void (*branch_callback)(void*,unsigned int,
155754 unsigned char,unsigned char);
155755 sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
155756 sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
155757#endif
155758 break;
155759 }
155760
155761 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
155762 case SQLITE_TESTCTRL_SORTER_MMAP: {
155763 sqlite3 *db = va_arg(ap, sqlite3*);
155764 db->nMaxSorterMmap = va_arg(ap, int);
155765 break;
155766 }
155767
155768 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
155769 **
155770 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
155771 ** not.
155772 */
155773 case SQLITE_TESTCTRL_ISINIT: {
155774 if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
155775 break;
155776 }
155777
155778 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
155779 **
155780 ** This test control is used to create imposter tables. "db" is a pointer
155781 ** to the database connection. dbName is the database name (ex: "main" or
155782 ** "temp") which will receive the imposter. "onOff" turns imposter mode on
155783 ** or off. "tnum" is the root page of the b-tree to which the imposter
155784 ** table should connect.
155785 **
155786 ** Enable imposter mode only when the schema has already been parsed. Then
155787 ** run a single CREATE TABLE statement to construct the imposter table in
155788 ** the parsed schema. Then turn imposter mode back off again.
155789 **
155790 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
155791 ** the schema to be reparsed the next time it is needed. This has the
155792 ** effect of erasing all imposter tables.
155793 */
155794 case SQLITE_TESTCTRL_IMPOSTER: {
155795 sqlite3 *db = va_arg(ap, sqlite3*);
155796 sqlite3_mutex_enter(db->mutex);
155797 db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
155798 db->init.busy = db->init.imposterTable = va_arg(ap,int);
155799 db->init.newTnum = va_arg(ap,int);
155800 if( db->init.busy==0 && db->init.newTnum>0 ){
155801 sqlite3ResetAllSchemasOfConnection(db);
155802 }
155803 sqlite3_mutex_leave(db->mutex);
155804 break;
155805 }
155806
155807#if defined(YYCOVERAGE)
155808 /* sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out)
155809 **
155810 ** This test control (only available when SQLite is compiled with
155811 ** -DYYCOVERAGE) writes a report onto "out" that shows all
155812 ** state/lookahead combinations in the parser state machine
155813 ** which are never exercised. If any state is missed, make the
155814 ** return code SQLITE_ERROR.
155815 */
155816 case SQLITE_TESTCTRL_PARSER_COVERAGE: {
155817 FILE *out = va_arg(ap, FILE*);
155818 if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR;
155819 break;
155820 }
155821#endif /* defined(YYCOVERAGE) */
155822 }
155823 va_end(ap);
155824#endif /* SQLITE_UNTESTABLE */
155825 return rc;
155826}
155827
155828/*
155829** This is a utility routine, useful to VFS implementations, that checks
155830** to see if a database file was a URI that contained a specific query
155831** parameter, and if so obtains the value of the query parameter.
155832**
155833** The zFilename argument is the filename pointer passed into the xOpen()
155834** method of a VFS implementation. The zParam argument is the name of the
155835** query parameter we seek. This routine returns the value of the zParam
155836** parameter if it exists. If the parameter does not exist, this routine
155837** returns a NULL pointer.
155838*/
155839SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
155840 if( zFilename==0 || zParam==0 ) return 0;
155841 zFilename += sqlite3Strlen30(zFilename) + 1;
155842 while( zFilename[0] ){
155843 int x = strcmp(zFilename, zParam);
155844 zFilename += sqlite3Strlen30(zFilename) + 1;
155845 if( x==0 ) return zFilename;
155846 zFilename += sqlite3Strlen30(zFilename) + 1;
155847 }
155848 return 0;
155849}
155850
155851/*
155852** Return a boolean value for a query parameter.
155853*/
155854SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
155855 const char *z = sqlite3_uri_parameter(zFilename, zParam);
155856 bDflt = bDflt!=0;
155857 return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
155858}
155859
155860/*
155861** Return a 64-bit integer value for a query parameter.
155862*/
155863SQLITE_API sqlite3_int64 sqlite3_uri_int64(
155864 const char *zFilename, /* Filename as passed to xOpen */
155865 const char *zParam, /* URI parameter sought */
155866 sqlite3_int64 bDflt /* return if parameter is missing */
155867){
155868 const char *z = sqlite3_uri_parameter(zFilename, zParam);
155869 sqlite3_int64 v;
155870 if( z && sqlite3DecOrHexToI64(z, &v)==0 ){
155871 bDflt = v;
155872 }
155873 return bDflt;
155874}
155875
155876/*
155877** Return the Btree pointer identified by zDbName. Return NULL if not found.
155878*/
155879SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
155880 int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0;
155881 return iDb<0 ? 0 : db->aDb[iDb].pBt;
155882}
155883
155884/*
155885** Return the filename of the database associated with a database
155886** connection.
155887*/
155888SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
155889 Btree *pBt;
155890#ifdef SQLITE_ENABLE_API_ARMOR
155891 if( !sqlite3SafetyCheckOk(db) ){
155892 (void)SQLITE_MISUSE_BKPT;
155893 return 0;
155894 }
155895#endif
155896 pBt = sqlite3DbNameToBtree(db, zDbName);
155897 return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
155898}
155899
155900/*
155901** Return 1 if database is read-only or 0 if read/write. Return -1 if
155902** no such database exists.
155903*/
155904SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
155905 Btree *pBt;
155906#ifdef SQLITE_ENABLE_API_ARMOR
155907 if( !sqlite3SafetyCheckOk(db) ){
155908 (void)SQLITE_MISUSE_BKPT;
155909 return -1;
155910 }
155911#endif
155912 pBt = sqlite3DbNameToBtree(db, zDbName);
155913 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
155914}
155915
155916#ifdef SQLITE_ENABLE_SNAPSHOT
155917/*
155918** Obtain a snapshot handle for the snapshot of database zDb currently
155919** being read by handle db.
155920*/
155921SQLITE_API int sqlite3_snapshot_get(
155922 sqlite3 *db,
155923 const char *zDb,
155924 sqlite3_snapshot **ppSnapshot
155925){
155926 int rc = SQLITE_ERROR;
155927#ifndef SQLITE_OMIT_WAL
155928
155929#ifdef SQLITE_ENABLE_API_ARMOR
155930 if( !sqlite3SafetyCheckOk(db) ){
155931 return SQLITE_MISUSE_BKPT;
155932 }
155933#endif
155934 sqlite3_mutex_enter(db->mutex);
155935
155936 if( db->autoCommit==0 ){
155937 int iDb = sqlite3FindDbName(db, zDb);
155938 if( iDb==0 || iDb>1 ){
155939 Btree *pBt = db->aDb[iDb].pBt;
155940 if( 0==sqlite3BtreeIsInTrans(pBt) ){
155941 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
155942 if( rc==SQLITE_OK ){
155943 rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
155944 }
155945 }
155946 }
155947 }
155948
155949 sqlite3_mutex_leave(db->mutex);
155950#endif /* SQLITE_OMIT_WAL */
155951 return rc;
155952}
155953
155954/*
155955** Open a read-transaction on the snapshot idendified by pSnapshot.
155956*/
155957SQLITE_API int sqlite3_snapshot_open(
155958 sqlite3 *db,
155959 const char *zDb,
155960 sqlite3_snapshot *pSnapshot
155961){
155962 int rc = SQLITE_ERROR;
155963#ifndef SQLITE_OMIT_WAL
155964
155965#ifdef SQLITE_ENABLE_API_ARMOR
155966 if( !sqlite3SafetyCheckOk(db) ){
155967 return SQLITE_MISUSE_BKPT;
155968 }
155969#endif
155970 sqlite3_mutex_enter(db->mutex);
155971 if( db->autoCommit==0 ){
155972 int iDb;
155973 iDb = sqlite3FindDbName(db, zDb);
155974 if( iDb==0 || iDb>1 ){
155975 Btree *pBt = db->aDb[iDb].pBt;
155976 if( sqlite3BtreeIsInTrans(pBt)==0 ){
155977 Pager *pPager = sqlite3BtreePager(pBt);
155978 int bUnlock = 0;
155979 if( sqlite3BtreeIsInReadTrans(pBt) ){
155980 if( db->nVdbeActive==0 ){
155981 rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot);
155982 if( rc==SQLITE_OK ){
155983 bUnlock = 1;
155984 rc = sqlite3BtreeCommit(pBt);
155985 }
155986 }
155987 }else{
155988 rc = SQLITE_OK;
155989 }
155990 if( rc==SQLITE_OK ){
155991 rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot);
155992 }
155993 if( rc==SQLITE_OK ){
155994 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
155995 sqlite3PagerSnapshotOpen(pPager, 0);
155996 }
155997 if( bUnlock ){
155998 sqlite3PagerSnapshotUnlock(pPager);
155999 }
156000 }
156001 }
156002 }
156003
156004 sqlite3_mutex_leave(db->mutex);
156005#endif /* SQLITE_OMIT_WAL */
156006 return rc;
156007}
156008
156009/*
156010** Recover as many snapshots as possible from the wal file associated with
156011** schema zDb of database db.
156012*/
156013SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
156014 int rc = SQLITE_ERROR;
156015 int iDb;
156016#ifndef SQLITE_OMIT_WAL
156017
156018#ifdef SQLITE_ENABLE_API_ARMOR
156019 if( !sqlite3SafetyCheckOk(db) ){
156020 return SQLITE_MISUSE_BKPT;
156021 }
156022#endif
156023
156024 sqlite3_mutex_enter(db->mutex);
156025 iDb = sqlite3FindDbName(db, zDb);
156026 if( iDb==0 || iDb>1 ){
156027 Btree *pBt = db->aDb[iDb].pBt;
156028 if( 0==sqlite3BtreeIsInReadTrans(pBt) ){
156029 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
156030 if( rc==SQLITE_OK ){
156031 rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt));
156032 sqlite3BtreeCommit(pBt);
156033 }
156034 }
156035 }
156036 sqlite3_mutex_leave(db->mutex);
156037#endif /* SQLITE_OMIT_WAL */
156038 return rc;
156039}
156040
156041/*
156042** Free a snapshot handle obtained from sqlite3_snapshot_get().
156043*/
156044SQLITE_API void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
156045 sqlite3_free(pSnapshot);
156046}
156047#endif /* SQLITE_ENABLE_SNAPSHOT */
156048
156049#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
156050/*
156051** Given the name of a compile-time option, return true if that option
156052** was used and false if not.
156053**
156054** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
156055** is not required for a match.
156056*/
156057SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
156058 int i, n;
156059 int nOpt;
156060 const char **azCompileOpt;
156061
156062#if SQLITE_ENABLE_API_ARMOR
156063 if( zOptName==0 ){
156064 (void)SQLITE_MISUSE_BKPT;
156065 return 0;
156066 }
156067#endif
156068
156069 azCompileOpt = sqlite3CompileOptions(&nOpt);
156070
156071 if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
156072 n = sqlite3Strlen30(zOptName);
156073
156074 /* Since nOpt is normally in single digits, a linear search is
156075 ** adequate. No need for a binary search. */
156076 for(i=0; i<nOpt; i++){
156077 if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
156078 && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
156079 ){
156080 return 1;
156081 }
156082 }
156083 return 0;
156084}
156085
156086/*
156087** Return the N-th compile-time option string. If N is out of range,
156088** return a NULL pointer.
156089*/
156090SQLITE_API const char *sqlite3_compileoption_get(int N){
156091 int nOpt;
156092 const char **azCompileOpt;
156093 azCompileOpt = sqlite3CompileOptions(&nOpt);
156094 if( N>=0 && N<nOpt ){
156095 return azCompileOpt[N];
156096 }
156097 return 0;
156098}
156099#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
156100
156101/************** End of main.c ************************************************/
156102/************** Begin file notify.c ******************************************/
156103/*
156104** 2009 March 3
156105**
156106** The author disclaims copyright to this source code. In place of
156107** a legal notice, here is a blessing:
156108**
156109** May you do good and not evil.
156110** May you find forgiveness for yourself and forgive others.
156111** May you share freely, never taking more than you give.
156112**
156113*************************************************************************
156114**
156115** This file contains the implementation of the sqlite3_unlock_notify()
156116** API method and its associated functionality.
156117*/
156118/* #include "sqliteInt.h" */
156119/* #include "btreeInt.h" */
156120
156121/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
156122#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
156123
156124/*
156125** Public interfaces:
156126**
156127** sqlite3ConnectionBlocked()
156128** sqlite3ConnectionUnlocked()
156129** sqlite3ConnectionClosed()
156130** sqlite3_unlock_notify()
156131*/
156132
156133#define assertMutexHeld() \
156134 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
156135
156136/*
156137** Head of a linked list of all sqlite3 objects created by this process
156138** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
156139** is not NULL. This variable may only accessed while the STATIC_MASTER
156140** mutex is held.
156141*/
156142static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
156143
156144#ifndef NDEBUG
156145/*
156146** This function is a complex assert() that verifies the following
156147** properties of the blocked connections list:
156148**
156149** 1) Each entry in the list has a non-NULL value for either
156150** pUnlockConnection or pBlockingConnection, or both.
156151**
156152** 2) All entries in the list that share a common value for
156153** xUnlockNotify are grouped together.
156154**
156155** 3) If the argument db is not NULL, then none of the entries in the
156156** blocked connections list have pUnlockConnection or pBlockingConnection
156157** set to db. This is used when closing connection db.
156158*/
156159static void checkListProperties(sqlite3 *db){
156160 sqlite3 *p;
156161 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
156162 int seen = 0;
156163 sqlite3 *p2;
156164
156165 /* Verify property (1) */
156166 assert( p->pUnlockConnection || p->pBlockingConnection );
156167
156168 /* Verify property (2) */
156169 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
156170 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
156171 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
156172 assert( db==0 || p->pUnlockConnection!=db );
156173 assert( db==0 || p->pBlockingConnection!=db );
156174 }
156175 }
156176}
156177#else
156178# define checkListProperties(x)
156179#endif
156180
156181/*
156182** Remove connection db from the blocked connections list. If connection
156183** db is not currently a part of the list, this function is a no-op.
156184*/
156185static void removeFromBlockedList(sqlite3 *db){
156186 sqlite3 **pp;
156187 assertMutexHeld();
156188 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
156189 if( *pp==db ){
156190 *pp = (*pp)->pNextBlocked;
156191 break;
156192 }
156193 }
156194}
156195
156196/*
156197** Add connection db to the blocked connections list. It is assumed
156198** that it is not already a part of the list.
156199*/
156200static void addToBlockedList(sqlite3 *db){
156201 sqlite3 **pp;
156202 assertMutexHeld();
156203 for(
156204 pp=&sqlite3BlockedList;
156205 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
156206 pp=&(*pp)->pNextBlocked
156207 );
156208 db->pNextBlocked = *pp;
156209 *pp = db;
156210}
156211
156212/*
156213** Obtain the STATIC_MASTER mutex.
156214*/
156215static void enterMutex(void){
156216 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
156217 checkListProperties(0);
156218}
156219
156220/*
156221** Release the STATIC_MASTER mutex.
156222*/
156223static void leaveMutex(void){
156224 assertMutexHeld();
156225 checkListProperties(0);
156226 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
156227}
156228
156229/*
156230** Register an unlock-notify callback.
156231**
156232** This is called after connection "db" has attempted some operation
156233** but has received an SQLITE_LOCKED error because another connection
156234** (call it pOther) in the same process was busy using the same shared
156235** cache. pOther is found by looking at db->pBlockingConnection.
156236**
156237** If there is no blocking connection, the callback is invoked immediately,
156238** before this routine returns.
156239**
156240** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
156241** a deadlock.
156242**
156243** Otherwise, make arrangements to invoke xNotify when pOther drops
156244** its locks.
156245**
156246** Each call to this routine overrides any prior callbacks registered
156247** on the same "db". If xNotify==0 then any prior callbacks are immediately
156248** cancelled.
156249*/
156250SQLITE_API int sqlite3_unlock_notify(
156251 sqlite3 *db,
156252 void (*xNotify)(void **, int),
156253 void *pArg
156254){
156255 int rc = SQLITE_OK;
156256
156257 sqlite3_mutex_enter(db->mutex);
156258 enterMutex();
156259
156260 if( xNotify==0 ){
156261 removeFromBlockedList(db);
156262 db->pBlockingConnection = 0;
156263 db->pUnlockConnection = 0;
156264 db->xUnlockNotify = 0;
156265 db->pUnlockArg = 0;
156266 }else if( 0==db->pBlockingConnection ){
156267 /* The blocking transaction has been concluded. Or there never was a
156268 ** blocking transaction. In either case, invoke the notify callback
156269 ** immediately.
156270 */
156271 xNotify(&pArg, 1);
156272 }else{
156273 sqlite3 *p;
156274
156275 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
156276 if( p ){
156277 rc = SQLITE_LOCKED; /* Deadlock detected. */
156278 }else{
156279 db->pUnlockConnection = db->pBlockingConnection;
156280 db->xUnlockNotify = xNotify;
156281 db->pUnlockArg = pArg;
156282 removeFromBlockedList(db);
156283 addToBlockedList(db);
156284 }
156285 }
156286
156287 leaveMutex();
156288 assert( !db->mallocFailed );
156289 sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0));
156290 sqlite3_mutex_leave(db->mutex);
156291 return rc;
156292}
156293
156294/*
156295** This function is called while stepping or preparing a statement
156296** associated with connection db. The operation will return SQLITE_LOCKED
156297** to the user because it requires a lock that will not be available
156298** until connection pBlocker concludes its current transaction.
156299*/
156300SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
156301 enterMutex();
156302 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
156303 addToBlockedList(db);
156304 }
156305 db->pBlockingConnection = pBlocker;
156306 leaveMutex();
156307}
156308
156309/*
156310** This function is called when
156311** the transaction opened by database db has just finished. Locks held
156312** by database connection db have been released.
156313**
156314** This function loops through each entry in the blocked connections
156315** list and does the following:
156316**
156317** 1) If the sqlite3.pBlockingConnection member of a list entry is
156318** set to db, then set pBlockingConnection=0.
156319**
156320** 2) If the sqlite3.pUnlockConnection member of a list entry is
156321** set to db, then invoke the configured unlock-notify callback and
156322** set pUnlockConnection=0.
156323**
156324** 3) If the two steps above mean that pBlockingConnection==0 and
156325** pUnlockConnection==0, remove the entry from the blocked connections
156326** list.
156327*/
156328SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
156329 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
156330 int nArg = 0; /* Number of entries in aArg[] */
156331 sqlite3 **pp; /* Iterator variable */
156332 void **aArg; /* Arguments to the unlock callback */
156333 void **aDyn = 0; /* Dynamically allocated space for aArg[] */
156334 void *aStatic[16]; /* Starter space for aArg[]. No malloc required */
156335
156336 aArg = aStatic;
156337 enterMutex(); /* Enter STATIC_MASTER mutex */
156338
156339 /* This loop runs once for each entry in the blocked-connections list. */
156340 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
156341 sqlite3 *p = *pp;
156342
156343 /* Step 1. */
156344 if( p->pBlockingConnection==db ){
156345 p->pBlockingConnection = 0;
156346 }
156347
156348 /* Step 2. */
156349 if( p->pUnlockConnection==db ){
156350 assert( p->xUnlockNotify );
156351 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
156352 xUnlockNotify(aArg, nArg);
156353 nArg = 0;
156354 }
156355
156356 sqlite3BeginBenignMalloc();
156357 assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
156358 assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
156359 if( (!aDyn && nArg==(int)ArraySize(aStatic))
156360 || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
156361 ){
156362 /* The aArg[] array needs to grow. */
156363 void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
156364 if( pNew ){
156365 memcpy(pNew, aArg, nArg*sizeof(void *));
156366 sqlite3_free(aDyn);
156367 aDyn = aArg = pNew;
156368 }else{
156369 /* This occurs when the array of context pointers that need to
156370 ** be passed to the unlock-notify callback is larger than the
156371 ** aStatic[] array allocated on the stack and the attempt to
156372 ** allocate a larger array from the heap has failed.
156373 **
156374 ** This is a difficult situation to handle. Returning an error
156375 ** code to the caller is insufficient, as even if an error code
156376 ** is returned the transaction on connection db will still be
156377 ** closed and the unlock-notify callbacks on blocked connections
156378 ** will go unissued. This might cause the application to wait
156379 ** indefinitely for an unlock-notify callback that will never
156380 ** arrive.
156381 **
156382 ** Instead, invoke the unlock-notify callback with the context
156383 ** array already accumulated. We can then clear the array and
156384 ** begin accumulating any further context pointers without
156385 ** requiring any dynamic allocation. This is sub-optimal because
156386 ** it means that instead of one callback with a large array of
156387 ** context pointers the application will receive two or more
156388 ** callbacks with smaller arrays of context pointers, which will
156389 ** reduce the applications ability to prioritize multiple
156390 ** connections. But it is the best that can be done under the
156391 ** circumstances.
156392 */
156393 xUnlockNotify(aArg, nArg);
156394 nArg = 0;
156395 }
156396 }
156397 sqlite3EndBenignMalloc();
156398
156399 aArg[nArg++] = p->pUnlockArg;
156400 xUnlockNotify = p->xUnlockNotify;
156401 p->pUnlockConnection = 0;
156402 p->xUnlockNotify = 0;
156403 p->pUnlockArg = 0;
156404 }
156405
156406 /* Step 3. */
156407 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
156408 /* Remove connection p from the blocked connections list. */
156409 *pp = p->pNextBlocked;
156410 p->pNextBlocked = 0;
156411 }else{
156412 pp = &p->pNextBlocked;
156413 }
156414 }
156415
156416 if( nArg!=0 ){
156417 xUnlockNotify(aArg, nArg);
156418 }
156419 sqlite3_free(aDyn);
156420 leaveMutex(); /* Leave STATIC_MASTER mutex */
156421}
156422
156423/*
156424** This is called when the database connection passed as an argument is
156425** being closed. The connection is removed from the blocked list.
156426*/
156427SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
156428 sqlite3ConnectionUnlocked(db);
156429 enterMutex();
156430 removeFromBlockedList(db);
156431 checkListProperties(db);
156432 leaveMutex();
156433}
156434#endif
156435
156436/************** End of notify.c **********************************************/
156437/************** Begin file fts3.c ********************************************/
156438/*
156439** 2006 Oct 10
156440**
156441** The author disclaims copyright to this source code. In place of
156442** a legal notice, here is a blessing:
156443**
156444** May you do good and not evil.
156445** May you find forgiveness for yourself and forgive others.
156446** May you share freely, never taking more than you give.
156447**
156448******************************************************************************
156449**
156450** This is an SQLite module implementing full-text search.
156451*/
156452
156453/*
156454** The code in this file is only compiled if:
156455**
156456** * The FTS3 module is being built as an extension
156457** (in which case SQLITE_CORE is not defined), or
156458**
156459** * The FTS3 module is being built into the core of
156460** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
156461*/
156462
156463/* The full-text index is stored in a series of b+tree (-like)
156464** structures called segments which map terms to doclists. The
156465** structures are like b+trees in layout, but are constructed from the
156466** bottom up in optimal fashion and are not updatable. Since trees
156467** are built from the bottom up, things will be described from the
156468** bottom up.
156469**
156470**
156471**** Varints ****
156472** The basic unit of encoding is a variable-length integer called a
156473** varint. We encode variable-length integers in little-endian order
156474** using seven bits * per byte as follows:
156475**
156476** KEY:
156477** A = 0xxxxxxx 7 bits of data and one flag bit
156478** B = 1xxxxxxx 7 bits of data and one flag bit
156479**
156480** 7 bits - A
156481** 14 bits - BA
156482** 21 bits - BBA
156483** and so on.
156484**
156485** This is similar in concept to how sqlite encodes "varints" but
156486** the encoding is not the same. SQLite varints are big-endian
156487** are are limited to 9 bytes in length whereas FTS3 varints are
156488** little-endian and can be up to 10 bytes in length (in theory).
156489**
156490** Example encodings:
156491**
156492** 1: 0x01
156493** 127: 0x7f
156494** 128: 0x81 0x00
156495**
156496**
156497**** Document lists ****
156498** A doclist (document list) holds a docid-sorted list of hits for a
156499** given term. Doclists hold docids and associated token positions.
156500** A docid is the unique integer identifier for a single document.
156501** A position is the index of a word within the document. The first
156502** word of the document has a position of 0.
156503**
156504** FTS3 used to optionally store character offsets using a compile-time
156505** option. But that functionality is no longer supported.
156506**
156507** A doclist is stored like this:
156508**
156509** array {
156510** varint docid; (delta from previous doclist)
156511** array { (position list for column 0)
156512** varint position; (2 more than the delta from previous position)
156513** }
156514** array {
156515** varint POS_COLUMN; (marks start of position list for new column)
156516** varint column; (index of new column)
156517** array {
156518** varint position; (2 more than the delta from previous position)
156519** }
156520** }
156521** varint POS_END; (marks end of positions for this document.
156522** }
156523**
156524** Here, array { X } means zero or more occurrences of X, adjacent in
156525** memory. A "position" is an index of a token in the token stream
156526** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
156527** in the same logical place as the position element, and act as sentinals
156528** ending a position list array. POS_END is 0. POS_COLUMN is 1.
156529** The positions numbers are not stored literally but rather as two more
156530** than the difference from the prior position, or the just the position plus
156531** 2 for the first position. Example:
156532**
156533** label: A B C D E F G H I J K
156534** value: 123 5 9 1 1 14 35 0 234 72 0
156535**
156536** The 123 value is the first docid. For column zero in this document
156537** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
156538** at D signals the start of a new column; the 1 at E indicates that the
156539** new column is column number 1. There are two positions at 12 and 45
156540** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
156541** 234 at I is the delta to next docid (357). It has one position 70
156542** (72-2) and then terminates with the 0 at K.
156543**
156544** A "position-list" is the list of positions for multiple columns for
156545** a single docid. A "column-list" is the set of positions for a single
156546** column. Hence, a position-list consists of one or more column-lists,
156547** a document record consists of a docid followed by a position-list and
156548** a doclist consists of one or more document records.
156549**
156550** A bare doclist omits the position information, becoming an
156551** array of varint-encoded docids.
156552**
156553**** Segment leaf nodes ****
156554** Segment leaf nodes store terms and doclists, ordered by term. Leaf
156555** nodes are written using LeafWriter, and read using LeafReader (to
156556** iterate through a single leaf node's data) and LeavesReader (to
156557** iterate through a segment's entire leaf layer). Leaf nodes have
156558** the format:
156559**
156560** varint iHeight; (height from leaf level, always 0)
156561** varint nTerm; (length of first term)
156562** char pTerm[nTerm]; (content of first term)
156563** varint nDoclist; (length of term's associated doclist)
156564** char pDoclist[nDoclist]; (content of doclist)
156565** array {
156566** (further terms are delta-encoded)
156567** varint nPrefix; (length of prefix shared with previous term)
156568** varint nSuffix; (length of unshared suffix)
156569** char pTermSuffix[nSuffix];(unshared suffix of next term)
156570** varint nDoclist; (length of term's associated doclist)
156571** char pDoclist[nDoclist]; (content of doclist)
156572** }
156573**
156574** Here, array { X } means zero or more occurrences of X, adjacent in
156575** memory.
156576**
156577** Leaf nodes are broken into blocks which are stored contiguously in
156578** the %_segments table in sorted order. This means that when the end
156579** of a node is reached, the next term is in the node with the next
156580** greater node id.
156581**
156582** New data is spilled to a new leaf node when the current node
156583** exceeds LEAF_MAX bytes (default 2048). New data which itself is
156584** larger than STANDALONE_MIN (default 1024) is placed in a standalone
156585** node (a leaf node with a single term and doclist). The goal of
156586** these settings is to pack together groups of small doclists while
156587** making it efficient to directly access large doclists. The
156588** assumption is that large doclists represent terms which are more
156589** likely to be query targets.
156590**
156591** TODO(shess) It may be useful for blocking decisions to be more
156592** dynamic. For instance, it may make more sense to have a 2.5k leaf
156593** node rather than splitting into 2k and .5k nodes. My intuition is
156594** that this might extend through 2x or 4x the pagesize.
156595**
156596**
156597**** Segment interior nodes ****
156598** Segment interior nodes store blockids for subtree nodes and terms
156599** to describe what data is stored by the each subtree. Interior
156600** nodes are written using InteriorWriter, and read using
156601** InteriorReader. InteriorWriters are created as needed when
156602** SegmentWriter creates new leaf nodes, or when an interior node
156603** itself grows too big and must be split. The format of interior
156604** nodes:
156605**
156606** varint iHeight; (height from leaf level, always >0)
156607** varint iBlockid; (block id of node's leftmost subtree)
156608** optional {
156609** varint nTerm; (length of first term)
156610** char pTerm[nTerm]; (content of first term)
156611** array {
156612** (further terms are delta-encoded)
156613** varint nPrefix; (length of shared prefix with previous term)
156614** varint nSuffix; (length of unshared suffix)
156615** char pTermSuffix[nSuffix]; (unshared suffix of next term)
156616** }
156617** }
156618**
156619** Here, optional { X } means an optional element, while array { X }
156620** means zero or more occurrences of X, adjacent in memory.
156621**
156622** An interior node encodes n terms separating n+1 subtrees. The
156623** subtree blocks are contiguous, so only the first subtree's blockid
156624** is encoded. The subtree at iBlockid will contain all terms less
156625** than the first term encoded (or all terms if no term is encoded).
156626** Otherwise, for terms greater than or equal to pTerm[i] but less
156627** than pTerm[i+1], the subtree for that term will be rooted at
156628** iBlockid+i. Interior nodes only store enough term data to
156629** distinguish adjacent children (if the rightmost term of the left
156630** child is "something", and the leftmost term of the right child is
156631** "wicked", only "w" is stored).
156632**
156633** New data is spilled to a new interior node at the same height when
156634** the current node exceeds INTERIOR_MAX bytes (default 2048).
156635** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
156636** interior nodes and making the tree too skinny. The interior nodes
156637** at a given height are naturally tracked by interior nodes at
156638** height+1, and so on.
156639**
156640**
156641**** Segment directory ****
156642** The segment directory in table %_segdir stores meta-information for
156643** merging and deleting segments, and also the root node of the
156644** segment's tree.
156645**
156646** The root node is the top node of the segment's tree after encoding
156647** the entire segment, restricted to ROOT_MAX bytes (default 1024).
156648** This could be either a leaf node or an interior node. If the top
156649** node requires more than ROOT_MAX bytes, it is flushed to %_segments
156650** and a new root interior node is generated (which should always fit
156651** within ROOT_MAX because it only needs space for 2 varints, the
156652** height and the blockid of the previous root).
156653**
156654** The meta-information in the segment directory is:
156655** level - segment level (see below)
156656** idx - index within level
156657** - (level,idx uniquely identify a segment)
156658** start_block - first leaf node
156659** leaves_end_block - last leaf node
156660** end_block - last block (including interior nodes)
156661** root - contents of root node
156662**
156663** If the root node is a leaf node, then start_block,
156664** leaves_end_block, and end_block are all 0.
156665**
156666**
156667**** Segment merging ****
156668** To amortize update costs, segments are grouped into levels and
156669** merged in batches. Each increase in level represents exponentially
156670** more documents.
156671**
156672** New documents (actually, document updates) are tokenized and
156673** written individually (using LeafWriter) to a level 0 segment, with
156674** incrementing idx. When idx reaches MERGE_COUNT (default 16), all
156675** level 0 segments are merged into a single level 1 segment. Level 1
156676** is populated like level 0, and eventually MERGE_COUNT level 1
156677** segments are merged to a single level 2 segment (representing
156678** MERGE_COUNT^2 updates), and so on.
156679**
156680** A segment merge traverses all segments at a given level in
156681** parallel, performing a straightforward sorted merge. Since segment
156682** leaf nodes are written in to the %_segments table in order, this
156683** merge traverses the underlying sqlite disk structures efficiently.
156684** After the merge, all segment blocks from the merged level are
156685** deleted.
156686**
156687** MERGE_COUNT controls how often we merge segments. 16 seems to be
156688** somewhat of a sweet spot for insertion performance. 32 and 64 show
156689** very similar performance numbers to 16 on insertion, though they're
156690** a tiny bit slower (perhaps due to more overhead in merge-time
156691** sorting). 8 is about 20% slower than 16, 4 about 50% slower than
156692** 16, 2 about 66% slower than 16.
156693**
156694** At query time, high MERGE_COUNT increases the number of segments
156695** which need to be scanned and merged. For instance, with 100k docs
156696** inserted:
156697**
156698** MERGE_COUNT segments
156699** 16 25
156700** 8 12
156701** 4 10
156702** 2 6
156703**
156704** This appears to have only a moderate impact on queries for very
156705** frequent terms (which are somewhat dominated by segment merge
156706** costs), and infrequent and non-existent terms still seem to be fast
156707** even with many segments.
156708**
156709** TODO(shess) That said, it would be nice to have a better query-side
156710** argument for MERGE_COUNT of 16. Also, it is possible/likely that
156711** optimizations to things like doclist merging will swing the sweet
156712** spot around.
156713**
156714**
156715**
156716**** Handling of deletions and updates ****
156717** Since we're using a segmented structure, with no docid-oriented
156718** index into the term index, we clearly cannot simply update the term
156719** index when a document is deleted or updated. For deletions, we
156720** write an empty doclist (varint(docid) varint(POS_END)), for updates
156721** we simply write the new doclist. Segment merges overwrite older
156722** data for a particular docid with newer data, so deletes or updates
156723** will eventually overtake the earlier data and knock it out. The
156724** query logic likewise merges doclists so that newer data knocks out
156725** older data.
156726*/
156727
156728/************** Include fts3Int.h in the middle of fts3.c ********************/
156729/************** Begin file fts3Int.h *****************************************/
156730/*
156731** 2009 Nov 12
156732**
156733** The author disclaims copyright to this source code. In place of
156734** a legal notice, here is a blessing:
156735**
156736** May you do good and not evil.
156737** May you find forgiveness for yourself and forgive others.
156738** May you share freely, never taking more than you give.
156739**
156740******************************************************************************
156741**
156742*/
156743#ifndef _FTSINT_H
156744#define _FTSINT_H
156745
156746#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
156747# define NDEBUG 1
156748#endif
156749
156750/* FTS3/FTS4 require virtual tables */
156751#ifdef SQLITE_OMIT_VIRTUALTABLE
156752# undef SQLITE_ENABLE_FTS3
156753# undef SQLITE_ENABLE_FTS4
156754#endif
156755
156756/*
156757** FTS4 is really an extension for FTS3. It is enabled using the
156758** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
156759** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
156760*/
156761#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
156762# define SQLITE_ENABLE_FTS3
156763#endif
156764
156765#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
156766
156767/* If not building as part of the core, include sqlite3ext.h. */
156768#ifndef SQLITE_CORE
156769/* # include "sqlite3ext.h" */
156770SQLITE_EXTENSION_INIT3
156771#endif
156772
156773/* #include "sqlite3.h" */
156774/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
156775/************** Begin file fts3_tokenizer.h **********************************/
156776/*
156777** 2006 July 10
156778**
156779** The author disclaims copyright to this source code.
156780**
156781*************************************************************************
156782** Defines the interface to tokenizers used by fulltext-search. There
156783** are three basic components:
156784**
156785** sqlite3_tokenizer_module is a singleton defining the tokenizer
156786** interface functions. This is essentially the class structure for
156787** tokenizers.
156788**
156789** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
156790** including customization information defined at creation time.
156791**
156792** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
156793** tokens from a particular input.
156794*/
156795#ifndef _FTS3_TOKENIZER_H_
156796#define _FTS3_TOKENIZER_H_
156797
156798/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
156799** If tokenizers are to be allowed to call sqlite3_*() functions, then
156800** we will need a way to register the API consistently.
156801*/
156802/* #include "sqlite3.h" */
156803
156804/*
156805** Structures used by the tokenizer interface. When a new tokenizer
156806** implementation is registered, the caller provides a pointer to
156807** an sqlite3_tokenizer_module containing pointers to the callback
156808** functions that make up an implementation.
156809**
156810** When an fts3 table is created, it passes any arguments passed to
156811** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
156812** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
156813** implementation. The xCreate() function in turn returns an
156814** sqlite3_tokenizer structure representing the specific tokenizer to
156815** be used for the fts3 table (customized by the tokenizer clause arguments).
156816**
156817** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
156818** method is called. It returns an sqlite3_tokenizer_cursor object
156819** that may be used to tokenize a specific input buffer based on
156820** the tokenization rules supplied by a specific sqlite3_tokenizer
156821** object.
156822*/
156823typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
156824typedef struct sqlite3_tokenizer sqlite3_tokenizer;
156825typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
156826
156827struct sqlite3_tokenizer_module {
156828
156829 /*
156830 ** Structure version. Should always be set to 0 or 1.
156831 */
156832 int iVersion;
156833
156834 /*
156835 ** Create a new tokenizer. The values in the argv[] array are the
156836 ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
156837 ** TABLE statement that created the fts3 table. For example, if
156838 ** the following SQL is executed:
156839 **
156840 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
156841 **
156842 ** then argc is set to 2, and the argv[] array contains pointers
156843 ** to the strings "arg1" and "arg2".
156844 **
156845 ** This method should return either SQLITE_OK (0), or an SQLite error
156846 ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
156847 ** to point at the newly created tokenizer structure. The generic
156848 ** sqlite3_tokenizer.pModule variable should not be initialized by
156849 ** this callback. The caller will do so.
156850 */
156851 int (*xCreate)(
156852 int argc, /* Size of argv array */
156853 const char *const*argv, /* Tokenizer argument strings */
156854 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
156855 );
156856
156857 /*
156858 ** Destroy an existing tokenizer. The fts3 module calls this method
156859 ** exactly once for each successful call to xCreate().
156860 */
156861 int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
156862
156863 /*
156864 ** Create a tokenizer cursor to tokenize an input buffer. The caller
156865 ** is responsible for ensuring that the input buffer remains valid
156866 ** until the cursor is closed (using the xClose() method).
156867 */
156868 int (*xOpen)(
156869 sqlite3_tokenizer *pTokenizer, /* Tokenizer object */
156870 const char *pInput, int nBytes, /* Input buffer */
156871 sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */
156872 );
156873
156874 /*
156875 ** Destroy an existing tokenizer cursor. The fts3 module calls this
156876 ** method exactly once for each successful call to xOpen().
156877 */
156878 int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
156879
156880 /*
156881 ** Retrieve the next token from the tokenizer cursor pCursor. This
156882 ** method should either return SQLITE_OK and set the values of the
156883 ** "OUT" variables identified below, or SQLITE_DONE to indicate that
156884 ** the end of the buffer has been reached, or an SQLite error code.
156885 **
156886 ** *ppToken should be set to point at a buffer containing the
156887 ** normalized version of the token (i.e. after any case-folding and/or
156888 ** stemming has been performed). *pnBytes should be set to the length
156889 ** of this buffer in bytes. The input text that generated the token is
156890 ** identified by the byte offsets returned in *piStartOffset and
156891 ** *piEndOffset. *piStartOffset should be set to the index of the first
156892 ** byte of the token in the input buffer. *piEndOffset should be set
156893 ** to the index of the first byte just past the end of the token in
156894 ** the input buffer.
156895 **
156896 ** The buffer *ppToken is set to point at is managed by the tokenizer
156897 ** implementation. It is only required to be valid until the next call
156898 ** to xNext() or xClose().
156899 */
156900 /* TODO(shess) current implementation requires pInput to be
156901 ** nul-terminated. This should either be fixed, or pInput/nBytes
156902 ** should be converted to zInput.
156903 */
156904 int (*xNext)(
156905 sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */
156906 const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */
156907 int *piStartOffset, /* OUT: Byte offset of token in input buffer */
156908 int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */
156909 int *piPosition /* OUT: Number of tokens returned before this one */
156910 );
156911
156912 /***********************************************************************
156913 ** Methods below this point are only available if iVersion>=1.
156914 */
156915
156916 /*
156917 ** Configure the language id of a tokenizer cursor.
156918 */
156919 int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
156920};
156921
156922struct sqlite3_tokenizer {
156923 const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */
156924 /* Tokenizer implementations will typically add additional fields */
156925};
156926
156927struct sqlite3_tokenizer_cursor {
156928 sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */
156929 /* Tokenizer implementations will typically add additional fields */
156930};
156931
156932int fts3_global_term_cnt(int iTerm, int iCol);
156933int fts3_term_cnt(int iTerm, int iCol);
156934
156935
156936#endif /* _FTS3_TOKENIZER_H_ */
156937
156938/************** End of fts3_tokenizer.h **************************************/
156939/************** Continuing where we left off in fts3Int.h ********************/
156940/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
156941/************** Begin file fts3_hash.h ***************************************/
156942/*
156943** 2001 September 22
156944**
156945** The author disclaims copyright to this source code. In place of
156946** a legal notice, here is a blessing:
156947**
156948** May you do good and not evil.
156949** May you find forgiveness for yourself and forgive others.
156950** May you share freely, never taking more than you give.
156951**
156952*************************************************************************
156953** This is the header file for the generic hash-table implementation
156954** used in SQLite. We've modified it slightly to serve as a standalone
156955** hash table implementation for the full-text indexing module.
156956**
156957*/
156958#ifndef _FTS3_HASH_H_
156959#define _FTS3_HASH_H_
156960
156961/* Forward declarations of structures. */
156962typedef struct Fts3Hash Fts3Hash;
156963typedef struct Fts3HashElem Fts3HashElem;
156964
156965/* A complete hash table is an instance of the following structure.
156966** The internals of this structure are intended to be opaque -- client
156967** code should not attempt to access or modify the fields of this structure
156968** directly. Change this structure only by using the routines below.
156969** However, many of the "procedures" and "functions" for modifying and
156970** accessing this structure are really macros, so we can't really make
156971** this structure opaque.
156972*/
156973struct Fts3Hash {
156974 char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */
156975 char copyKey; /* True if copy of key made on insert */
156976 int count; /* Number of entries in this table */
156977 Fts3HashElem *first; /* The first element of the array */
156978 int htsize; /* Number of buckets in the hash table */
156979 struct _fts3ht { /* the hash table */
156980 int count; /* Number of entries with this hash */
156981 Fts3HashElem *chain; /* Pointer to first entry with this hash */
156982 } *ht;
156983};
156984
156985/* Each element in the hash table is an instance of the following
156986** structure. All elements are stored on a single doubly-linked list.
156987**
156988** Again, this structure is intended to be opaque, but it can't really
156989** be opaque because it is used by macros.
156990*/
156991struct Fts3HashElem {
156992 Fts3HashElem *next, *prev; /* Next and previous elements in the table */
156993 void *data; /* Data associated with this element */
156994 void *pKey; int nKey; /* Key associated with this element */
156995};
156996
156997/*
156998** There are 2 different modes of operation for a hash table:
156999**
157000** FTS3_HASH_STRING pKey points to a string that is nKey bytes long
157001** (including the null-terminator, if any). Case
157002** is respected in comparisons.
157003**
157004** FTS3_HASH_BINARY pKey points to binary data nKey bytes long.
157005** memcmp() is used to compare keys.
157006**
157007** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
157008*/
157009#define FTS3_HASH_STRING 1
157010#define FTS3_HASH_BINARY 2
157011
157012/*
157013** Access routines. To delete, insert a NULL pointer.
157014*/
157015SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
157016SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
157017SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
157018SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
157019SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
157020
157021/*
157022** Shorthand for the functions above
157023*/
157024#define fts3HashInit sqlite3Fts3HashInit
157025#define fts3HashInsert sqlite3Fts3HashInsert
157026#define fts3HashFind sqlite3Fts3HashFind
157027#define fts3HashClear sqlite3Fts3HashClear
157028#define fts3HashFindElem sqlite3Fts3HashFindElem
157029
157030/*
157031** Macros for looping over all elements of a hash table. The idiom is
157032** like this:
157033**
157034** Fts3Hash h;
157035** Fts3HashElem *p;
157036** ...
157037** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
157038** SomeStructure *pData = fts3HashData(p);
157039** // do something with pData
157040** }
157041*/
157042#define fts3HashFirst(H) ((H)->first)
157043#define fts3HashNext(E) ((E)->next)
157044#define fts3HashData(E) ((E)->data)
157045#define fts3HashKey(E) ((E)->pKey)
157046#define fts3HashKeysize(E) ((E)->nKey)
157047
157048/*
157049** Number of entries in a hash table
157050*/
157051#define fts3HashCount(H) ((H)->count)
157052
157053#endif /* _FTS3_HASH_H_ */
157054
157055/************** End of fts3_hash.h *******************************************/
157056/************** Continuing where we left off in fts3Int.h ********************/
157057
157058/*
157059** This constant determines the maximum depth of an FTS expression tree
157060** that the library will create and use. FTS uses recursion to perform
157061** various operations on the query tree, so the disadvantage of a large
157062** limit is that it may allow very large queries to use large amounts
157063** of stack space (perhaps causing a stack overflow).
157064*/
157065#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH
157066# define SQLITE_FTS3_MAX_EXPR_DEPTH 12
157067#endif
157068
157069
157070/*
157071** This constant controls how often segments are merged. Once there are
157072** FTS3_MERGE_COUNT segments of level N, they are merged into a single
157073** segment of level N+1.
157074*/
157075#define FTS3_MERGE_COUNT 16
157076
157077/*
157078** This is the maximum amount of data (in bytes) to store in the
157079** Fts3Table.pendingTerms hash table. Normally, the hash table is
157080** populated as documents are inserted/updated/deleted in a transaction
157081** and used to create a new segment when the transaction is committed.
157082** However if this limit is reached midway through a transaction, a new
157083** segment is created and the hash table cleared immediately.
157084*/
157085#define FTS3_MAX_PENDING_DATA (1*1024*1024)
157086
157087/*
157088** Macro to return the number of elements in an array. SQLite has a
157089** similar macro called ArraySize(). Use a different name to avoid
157090** a collision when building an amalgamation with built-in FTS3.
157091*/
157092#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
157093
157094
157095#ifndef MIN
157096# define MIN(x,y) ((x)<(y)?(x):(y))
157097#endif
157098#ifndef MAX
157099# define MAX(x,y) ((x)>(y)?(x):(y))
157100#endif
157101
157102/*
157103** Maximum length of a varint encoded integer. The varint format is different
157104** from that used by SQLite, so the maximum length is 10, not 9.
157105*/
157106#define FTS3_VARINT_MAX 10
157107
157108/*
157109** FTS4 virtual tables may maintain multiple indexes - one index of all terms
157110** in the document set and zero or more prefix indexes. All indexes are stored
157111** as one or more b+-trees in the %_segments and %_segdir tables.
157112**
157113** It is possible to determine which index a b+-tree belongs to based on the
157114** value stored in the "%_segdir.level" column. Given this value L, the index
157115** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
157116** level values between 0 and 1023 (inclusive) belong to index 0, all levels
157117** between 1024 and 2047 to index 1, and so on.
157118**
157119** It is considered impossible for an index to use more than 1024 levels. In
157120** theory though this may happen, but only after at least
157121** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
157122*/
157123#define FTS3_SEGDIR_MAXLEVEL 1024
157124#define FTS3_SEGDIR_MAXLEVEL_STR "1024"
157125
157126/*
157127** The testcase() macro is only used by the amalgamation. If undefined,
157128** make it a no-op.
157129*/
157130#ifndef testcase
157131# define testcase(X)
157132#endif
157133
157134/*
157135** Terminator values for position-lists and column-lists.
157136*/
157137#define POS_COLUMN (1) /* Column-list terminator */
157138#define POS_END (0) /* Position-list terminator */
157139
157140/*
157141** This section provides definitions to allow the
157142** FTS3 extension to be compiled outside of the
157143** amalgamation.
157144*/
157145#ifndef SQLITE_AMALGAMATION
157146/*
157147** Macros indicating that conditional expressions are always true or
157148** false.
157149*/
157150#ifdef SQLITE_COVERAGE_TEST
157151# define ALWAYS(x) (1)
157152# define NEVER(X) (0)
157153#elif defined(SQLITE_DEBUG)
157154# define ALWAYS(x) sqlite3Fts3Always((x)!=0)
157155# define NEVER(x) sqlite3Fts3Never((x)!=0)
157156SQLITE_PRIVATE int sqlite3Fts3Always(int b);
157157SQLITE_PRIVATE int sqlite3Fts3Never(int b);
157158#else
157159# define ALWAYS(x) (x)
157160# define NEVER(x) (x)
157161#endif
157162
157163/*
157164** Internal types used by SQLite.
157165*/
157166typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
157167typedef short int i16; /* 2-byte (or larger) signed integer */
157168typedef unsigned int u32; /* 4-byte unsigned integer */
157169typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
157170typedef sqlite3_int64 i64; /* 8-byte signed integer */
157171
157172/*
157173** Macro used to suppress compiler warnings for unused parameters.
157174*/
157175#define UNUSED_PARAMETER(x) (void)(x)
157176
157177/*
157178** Activate assert() only if SQLITE_TEST is enabled.
157179*/
157180#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
157181# define NDEBUG 1
157182#endif
157183
157184/*
157185** The TESTONLY macro is used to enclose variable declarations or
157186** other bits of code that are needed to support the arguments
157187** within testcase() and assert() macros.
157188*/
157189#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
157190# define TESTONLY(X) X
157191#else
157192# define TESTONLY(X)
157193#endif
157194
157195#endif /* SQLITE_AMALGAMATION */
157196
157197#ifdef SQLITE_DEBUG
157198SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
157199# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
157200#else
157201# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
157202#endif
157203
157204typedef struct Fts3Table Fts3Table;
157205typedef struct Fts3Cursor Fts3Cursor;
157206typedef struct Fts3Expr Fts3Expr;
157207typedef struct Fts3Phrase Fts3Phrase;
157208typedef struct Fts3PhraseToken Fts3PhraseToken;
157209
157210typedef struct Fts3Doclist Fts3Doclist;
157211typedef struct Fts3SegFilter Fts3SegFilter;
157212typedef struct Fts3DeferredToken Fts3DeferredToken;
157213typedef struct Fts3SegReader Fts3SegReader;
157214typedef struct Fts3MultiSegReader Fts3MultiSegReader;
157215
157216typedef struct MatchinfoBuffer MatchinfoBuffer;
157217
157218/*
157219** A connection to a fulltext index is an instance of the following
157220** structure. The xCreate and xConnect methods create an instance
157221** of this structure and xDestroy and xDisconnect free that instance.
157222** All other methods receive a pointer to the structure as one of their
157223** arguments.
157224*/
157225struct Fts3Table {
157226 sqlite3_vtab base; /* Base class used by SQLite core */
157227 sqlite3 *db; /* The database connection */
157228 const char *zDb; /* logical database name */
157229 const char *zName; /* virtual table name */
157230 int nColumn; /* number of named columns in virtual table */
157231 char **azColumn; /* column names. malloced */
157232 u8 *abNotindexed; /* True for 'notindexed' columns */
157233 sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
157234 char *zContentTbl; /* content=xxx option, or NULL */
157235 char *zLanguageid; /* languageid=xxx option, or NULL */
157236 int nAutoincrmerge; /* Value configured by 'automerge' */
157237 u32 nLeafAdd; /* Number of leaf blocks added this trans */
157238
157239 /* Precompiled statements used by the implementation. Each of these
157240 ** statements is run and reset within a single virtual table API call.
157241 */
157242 sqlite3_stmt *aStmt[40];
157243 sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */
157244
157245 char *zReadExprlist;
157246 char *zWriteExprlist;
157247
157248 int nNodeSize; /* Soft limit for node size */
157249 u8 bFts4; /* True for FTS4, false for FTS3 */
157250 u8 bHasStat; /* True if %_stat table exists (2==unknown) */
157251 u8 bHasDocsize; /* True if %_docsize table exists */
157252 u8 bDescIdx; /* True if doclists are in reverse order */
157253 u8 bIgnoreSavepoint; /* True to ignore xSavepoint invocations */
157254 int nPgsz; /* Page size for host database */
157255 char *zSegmentsTbl; /* Name of %_segments table */
157256 sqlite3_blob *pSegments; /* Blob handle open on %_segments table */
157257
157258 /*
157259 ** The following array of hash tables is used to buffer pending index
157260 ** updates during transactions. All pending updates buffered at any one
157261 ** time must share a common language-id (see the FTS4 langid= feature).
157262 ** The current language id is stored in variable iPrevLangid.
157263 **
157264 ** A single FTS4 table may have multiple full-text indexes. For each index
157265 ** there is an entry in the aIndex[] array. Index 0 is an index of all the
157266 ** terms that appear in the document set. Each subsequent index in aIndex[]
157267 ** is an index of prefixes of a specific length.
157268 **
157269 ** Variable nPendingData contains an estimate the memory consumed by the
157270 ** pending data structures, including hash table overhead, but not including
157271 ** malloc overhead. When nPendingData exceeds nMaxPendingData, all hash
157272 ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
157273 ** recently inserted record.
157274 */
157275 int nIndex; /* Size of aIndex[] */
157276 struct Fts3Index {
157277 int nPrefix; /* Prefix length (0 for main terms index) */
157278 Fts3Hash hPending; /* Pending terms table for this index */
157279 } *aIndex;
157280 int nMaxPendingData; /* Max pending data before flush to disk */
157281 int nPendingData; /* Current bytes of pending data */
157282 sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */
157283 int iPrevLangid; /* Langid of recently inserted document */
157284 int bPrevDelete; /* True if last operation was a delete */
157285
157286#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
157287 /* State variables used for validating that the transaction control
157288 ** methods of the virtual table are called at appropriate times. These
157289 ** values do not contribute to FTS functionality; they are used for
157290 ** verifying the operation of the SQLite core.
157291 */
157292 int inTransaction; /* True after xBegin but before xCommit/xRollback */
157293 int mxSavepoint; /* Largest valid xSavepoint integer */
157294#endif
157295
157296#ifdef SQLITE_TEST
157297 /* True to disable the incremental doclist optimization. This is controled
157298 ** by special insert command 'test-no-incr-doclist'. */
157299 int bNoIncrDoclist;
157300#endif
157301};
157302
157303/*
157304** When the core wants to read from the virtual table, it creates a
157305** virtual table cursor (an instance of the following structure) using
157306** the xOpen method. Cursors are destroyed using the xClose method.
157307*/
157308struct Fts3Cursor {
157309 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
157310 i16 eSearch; /* Search strategy (see below) */
157311 u8 isEof; /* True if at End Of Results */
157312 u8 isRequireSeek; /* True if must seek pStmt to %_content row */
157313 u8 bSeekStmt; /* True if pStmt is a seek */
157314 sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
157315 Fts3Expr *pExpr; /* Parsed MATCH query string */
157316 int iLangid; /* Language being queried for */
157317 int nPhrase; /* Number of matchable phrases in query */
157318 Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
157319 sqlite3_int64 iPrevId; /* Previous id read from aDoclist */
157320 char *pNextId; /* Pointer into the body of aDoclist */
157321 char *aDoclist; /* List of docids for full-text queries */
157322 int nDoclist; /* Size of buffer at aDoclist */
157323 u8 bDesc; /* True to sort in descending order */
157324 int eEvalmode; /* An FTS3_EVAL_XX constant */
157325 int nRowAvg; /* Average size of database rows, in pages */
157326 sqlite3_int64 nDoc; /* Documents in table */
157327 i64 iMinDocid; /* Minimum docid to return */
157328 i64 iMaxDocid; /* Maximum docid to return */
157329 int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
157330 MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */
157331};
157332
157333#define FTS3_EVAL_FILTER 0
157334#define FTS3_EVAL_NEXT 1
157335#define FTS3_EVAL_MATCHINFO 2
157336
157337/*
157338** The Fts3Cursor.eSearch member is always set to one of the following.
157339** Actualy, Fts3Cursor.eSearch can be greater than or equal to
157340** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
157341** of the column to be searched. For example, in
157342**
157343** CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
157344** SELECT docid FROM ex1 WHERE b MATCH 'one two three';
157345**
157346** Because the LHS of the MATCH operator is 2nd column "b",
157347** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1. (+0 for a,
157348** +1 for b, +2 for c, +3 for d.) If the LHS of MATCH were "ex1"
157349** indicating that all columns should be searched,
157350** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
157351*/
157352#define FTS3_FULLSCAN_SEARCH 0 /* Linear scan of %_content table */
157353#define FTS3_DOCID_SEARCH 1 /* Lookup by rowid on %_content table */
157354#define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */
157355
157356/*
157357** The lower 16-bits of the sqlite3_index_info.idxNum value set by
157358** the xBestIndex() method contains the Fts3Cursor.eSearch value described
157359** above. The upper 16-bits contain a combination of the following
157360** bits, used to describe extra constraints on full-text searches.
157361*/
157362#define FTS3_HAVE_LANGID 0x00010000 /* languageid=? */
157363#define FTS3_HAVE_DOCID_GE 0x00020000 /* docid>=? */
157364#define FTS3_HAVE_DOCID_LE 0x00040000 /* docid<=? */
157365
157366struct Fts3Doclist {
157367 char *aAll; /* Array containing doclist (or NULL) */
157368 int nAll; /* Size of a[] in bytes */
157369 char *pNextDocid; /* Pointer to next docid */
157370
157371 sqlite3_int64 iDocid; /* Current docid (if pList!=0) */
157372 int bFreeList; /* True if pList should be sqlite3_free()d */
157373 char *pList; /* Pointer to position list following iDocid */
157374 int nList; /* Length of position list */
157375};
157376
157377/*
157378** A "phrase" is a sequence of one or more tokens that must match in
157379** sequence. A single token is the base case and the most common case.
157380** For a sequence of tokens contained in double-quotes (i.e. "one two three")
157381** nToken will be the number of tokens in the string.
157382*/
157383struct Fts3PhraseToken {
157384 char *z; /* Text of the token */
157385 int n; /* Number of bytes in buffer z */
157386 int isPrefix; /* True if token ends with a "*" character */
157387 int bFirst; /* True if token must appear at position 0 */
157388
157389 /* Variables above this point are populated when the expression is
157390 ** parsed (by code in fts3_expr.c). Below this point the variables are
157391 ** used when evaluating the expression. */
157392 Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
157393 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
157394};
157395
157396struct Fts3Phrase {
157397 /* Cache of doclist for this phrase. */
157398 Fts3Doclist doclist;
157399 int bIncr; /* True if doclist is loaded incrementally */
157400 int iDoclistToken;
157401
157402 /* Used by sqlite3Fts3EvalPhrasePoslist() if this is a descendent of an
157403 ** OR condition. */
157404 char *pOrPoslist;
157405 i64 iOrDocid;
157406
157407 /* Variables below this point are populated by fts3_expr.c when parsing
157408 ** a MATCH expression. Everything above is part of the evaluation phase.
157409 */
157410 int nToken; /* Number of tokens in the phrase */
157411 int iColumn; /* Index of column this phrase must match */
157412 Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
157413};
157414
157415/*
157416** A tree of these objects forms the RHS of a MATCH operator.
157417**
157418** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
157419** points to a malloced buffer, size nDoclist bytes, containing the results
157420** of this phrase query in FTS3 doclist format. As usual, the initial
157421** "Length" field found in doclists stored on disk is omitted from this
157422** buffer.
157423**
157424** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
157425** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
157426** where nCol is the number of columns in the queried FTS table. The array
157427** is populated as follows:
157428**
157429** aMI[iCol*3 + 0] = Undefined
157430** aMI[iCol*3 + 1] = Number of occurrences
157431** aMI[iCol*3 + 2] = Number of rows containing at least one instance
157432**
157433** The aMI array is allocated using sqlite3_malloc(). It should be freed
157434** when the expression node is.
157435*/
157436struct Fts3Expr {
157437 int eType; /* One of the FTSQUERY_XXX values defined below */
157438 int nNear; /* Valid if eType==FTSQUERY_NEAR */
157439 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
157440 Fts3Expr *pLeft; /* Left operand */
157441 Fts3Expr *pRight; /* Right operand */
157442 Fts3Phrase *pPhrase; /* Valid if eType==FTSQUERY_PHRASE */
157443
157444 /* The following are used by the fts3_eval.c module. */
157445 sqlite3_int64 iDocid; /* Current docid */
157446 u8 bEof; /* True this expression is at EOF already */
157447 u8 bStart; /* True if iDocid is valid */
157448 u8 bDeferred; /* True if this expression is entirely deferred */
157449
157450 /* The following are used by the fts3_snippet.c module. */
157451 int iPhrase; /* Index of this phrase in matchinfo() results */
157452 u32 *aMI; /* See above */
157453};
157454
157455/*
157456** Candidate values for Fts3Query.eType. Note that the order of the first
157457** four values is in order of precedence when parsing expressions. For
157458** example, the following:
157459**
157460** "a OR b AND c NOT d NEAR e"
157461**
157462** is equivalent to:
157463**
157464** "a OR (b AND (c NOT (d NEAR e)))"
157465*/
157466#define FTSQUERY_NEAR 1
157467#define FTSQUERY_NOT 2
157468#define FTSQUERY_AND 3
157469#define FTSQUERY_OR 4
157470#define FTSQUERY_PHRASE 5
157471
157472
157473/* fts3_write.c */
157474SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
157475SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
157476SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
157477SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
157478SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
157479 sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
157480SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
157481 Fts3Table*,int,const char*,int,int,Fts3SegReader**);
157482SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
157483SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
157484SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
157485
157486SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
157487SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
157488
157489#ifndef SQLITE_DISABLE_FTS4_DEFERRED
157490SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
157491SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
157492SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
157493SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
157494SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
157495#else
157496# define sqlite3Fts3FreeDeferredTokens(x)
157497# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
157498# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
157499# define sqlite3Fts3FreeDeferredDoclists(x)
157500# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
157501#endif
157502
157503SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
157504SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
157505
157506/* Special values interpreted by sqlite3SegReaderCursor() */
157507#define FTS3_SEGCURSOR_PENDING -1
157508#define FTS3_SEGCURSOR_ALL -2
157509
157510SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
157511SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
157512SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
157513
157514SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(Fts3Table *,
157515 int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
157516
157517/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
157518#define FTS3_SEGMENT_REQUIRE_POS 0x00000001
157519#define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002
157520#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
157521#define FTS3_SEGMENT_PREFIX 0x00000008
157522#define FTS3_SEGMENT_SCAN 0x00000010
157523#define FTS3_SEGMENT_FIRST 0x00000020
157524
157525/* Type passed as 4th argument to SegmentReaderIterate() */
157526struct Fts3SegFilter {
157527 const char *zTerm;
157528 int nTerm;
157529 int iCol;
157530 int flags;
157531};
157532
157533struct Fts3MultiSegReader {
157534 /* Used internally by sqlite3Fts3SegReaderXXX() calls */
157535 Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */
157536 int nSegment; /* Size of apSegment array */
157537 int nAdvance; /* How many seg-readers to advance */
157538 Fts3SegFilter *pFilter; /* Pointer to filter object */
157539 char *aBuffer; /* Buffer to merge doclists in */
157540 int nBuffer; /* Allocated size of aBuffer[] in bytes */
157541
157542 int iColFilter; /* If >=0, filter for this column */
157543 int bRestart;
157544
157545 /* Used by fts3.c only. */
157546 int nCost; /* Cost of running iterator */
157547 int bLookup; /* True if a lookup of a single entry. */
157548
157549 /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
157550 char *zTerm; /* Pointer to term buffer */
157551 int nTerm; /* Size of zTerm in bytes */
157552 char *aDoclist; /* Pointer to doclist buffer */
157553 int nDoclist; /* Size of aDoclist[] in bytes */
157554};
157555
157556SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
157557
157558#define fts3GetVarint32(p, piVal) ( \
157559 (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
157560)
157561
157562/* fts3.c */
157563SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char**,const char*,...);
157564SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
157565SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
157566SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
157567SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
157568SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
157569SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
157570SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
157571SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
157572SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
157573SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
157574
157575/* fts3_tokenizer.c */
157576SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
157577SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
157578SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
157579 sqlite3_tokenizer **, char **
157580);
157581SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
157582
157583/* fts3_snippet.c */
157584SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
157585SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
157586 const char *, const char *, int, int
157587);
157588SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
157589SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
157590
157591/* fts3_expr.c */
157592SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
157593 char **, int, int, int, const char *, int, Fts3Expr **, char **
157594);
157595SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
157596#ifdef SQLITE_TEST
157597SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
157598SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
157599#endif
157600
157601SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
157602 sqlite3_tokenizer_cursor **
157603);
157604
157605/* fts3_aux.c */
157606SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
157607
157608SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
157609
157610SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
157611 Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
157612SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
157613 Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
157614SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
157615SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
157616SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
157617
157618/* fts3_tokenize_vtab.c */
157619SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
157620
157621/* fts3_unicode2.c (functions generated by parsing unicode text files) */
157622#ifndef SQLITE_DISABLE_FTS3_UNICODE
157623SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
157624SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
157625SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
157626#endif
157627
157628#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
157629#endif /* _FTSINT_H */
157630
157631/************** End of fts3Int.h *********************************************/
157632/************** Continuing where we left off in fts3.c ***********************/
157633#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
157634
157635#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
157636# define SQLITE_CORE 1
157637#endif
157638
157639/* #include <assert.h> */
157640/* #include <stdlib.h> */
157641/* #include <stddef.h> */
157642/* #include <stdio.h> */
157643/* #include <string.h> */
157644/* #include <stdarg.h> */
157645
157646/* #include "fts3.h" */
157647#ifndef SQLITE_CORE
157648/* # include "sqlite3ext.h" */
157649 SQLITE_EXTENSION_INIT1
157650#endif
157651
157652static int fts3EvalNext(Fts3Cursor *pCsr);
157653static int fts3EvalStart(Fts3Cursor *pCsr);
157654static int fts3TermSegReaderCursor(
157655 Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
157656
157657#ifndef SQLITE_AMALGAMATION
157658# if defined(SQLITE_DEBUG)
157659SQLITE_PRIVATE int sqlite3Fts3Always(int b) { assert( b ); return b; }
157660SQLITE_PRIVATE int sqlite3Fts3Never(int b) { assert( !b ); return b; }
157661# endif
157662#endif
157663
157664/*
157665** Write a 64-bit variable-length integer to memory starting at p[0].
157666** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
157667** The number of bytes written is returned.
157668*/
157669SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
157670 unsigned char *q = (unsigned char *) p;
157671 sqlite_uint64 vu = v;
157672 do{
157673 *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
157674 vu >>= 7;
157675 }while( vu!=0 );
157676 q[-1] &= 0x7f; /* turn off high bit in final byte */
157677 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
157678 return (int) (q - (unsigned char *)p);
157679}
157680
157681#define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
157682 v = (v & mask1) | ( (*ptr++) << shift ); \
157683 if( (v & mask2)==0 ){ var = v; return ret; }
157684#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
157685 v = (*ptr++); \
157686 if( (v & mask2)==0 ){ var = v; return ret; }
157687
157688/*
157689** Read a 64-bit variable-length integer from memory starting at p[0].
157690** Return the number of bytes read, or 0 on error.
157691** The value is stored in *v.
157692*/
157693SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
157694 const unsigned char *p = (const unsigned char*)pBuf;
157695 const unsigned char *pStart = p;
157696 u32 a;
157697 u64 b;
157698 int shift;
157699
157700 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1);
157701 GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2);
157702 GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3);
157703 GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
157704 b = (a & 0x0FFFFFFF );
157705
157706 for(shift=28; shift<=63; shift+=7){
157707 u64 c = *p++;
157708 b += (c&0x7F) << shift;
157709 if( (c & 0x80)==0 ) break;
157710 }
157711 *v = b;
157712 return (int)(p - pStart);
157713}
157714
157715/*
157716** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to
157717** a non-negative 32-bit integer before it is returned.
157718*/
157719SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
157720 u32 a;
157721
157722#ifndef fts3GetVarint32
157723 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *pi, 1);
157724#else
157725 a = (*p++);
157726 assert( a & 0x80 );
157727#endif
157728
157729 GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *pi, 2);
157730 GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *pi, 3);
157731 GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *pi, 4);
157732 a = (a & 0x0FFFFFFF );
157733 *pi = (int)(a | ((u32)(*p & 0x07) << 28));
157734 assert( 0==(a & 0x80000000) );
157735 assert( *pi>=0 );
157736 return 5;
157737}
157738
157739/*
157740** Return the number of bytes required to encode v as a varint
157741*/
157742SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
157743 int i = 0;
157744 do{
157745 i++;
157746 v >>= 7;
157747 }while( v!=0 );
157748 return i;
157749}
157750
157751/*
157752** Convert an SQL-style quoted string into a normal string by removing
157753** the quote characters. The conversion is done in-place. If the
157754** input does not begin with a quote character, then this routine
157755** is a no-op.
157756**
157757** Examples:
157758**
157759** "abc" becomes abc
157760** 'xyz' becomes xyz
157761** [pqr] becomes pqr
157762** `mno` becomes mno
157763**
157764*/
157765SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
157766 char quote; /* Quote character (if any ) */
157767
157768 quote = z[0];
157769 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
157770 int iIn = 1; /* Index of next byte to read from input */
157771 int iOut = 0; /* Index of next byte to write to output */
157772
157773 /* If the first byte was a '[', then the close-quote character is a ']' */
157774 if( quote=='[' ) quote = ']';
157775
157776 while( z[iIn] ){
157777 if( z[iIn]==quote ){
157778 if( z[iIn+1]!=quote ) break;
157779 z[iOut++] = quote;
157780 iIn += 2;
157781 }else{
157782 z[iOut++] = z[iIn++];
157783 }
157784 }
157785 z[iOut] = '\0';
157786 }
157787}
157788
157789/*
157790** Read a single varint from the doclist at *pp and advance *pp to point
157791** to the first byte past the end of the varint. Add the value of the varint
157792** to *pVal.
157793*/
157794static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
157795 sqlite3_int64 iVal;
157796 *pp += sqlite3Fts3GetVarint(*pp, &iVal);
157797 *pVal += iVal;
157798}
157799
157800/*
157801** When this function is called, *pp points to the first byte following a
157802** varint that is part of a doclist (or position-list, or any other list
157803** of varints). This function moves *pp to point to the start of that varint,
157804** and sets *pVal by the varint value.
157805**
157806** Argument pStart points to the first byte of the doclist that the
157807** varint is part of.
157808*/
157809static void fts3GetReverseVarint(
157810 char **pp,
157811 char *pStart,
157812 sqlite3_int64 *pVal
157813){
157814 sqlite3_int64 iVal;
157815 char *p;
157816
157817 /* Pointer p now points at the first byte past the varint we are
157818 ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
157819 ** clear on character p[-1]. */
157820 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
157821 p++;
157822 *pp = p;
157823
157824 sqlite3Fts3GetVarint(p, &iVal);
157825 *pVal = iVal;
157826}
157827
157828/*
157829** The xDisconnect() virtual table method.
157830*/
157831static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
157832 Fts3Table *p = (Fts3Table *)pVtab;
157833 int i;
157834
157835 assert( p->nPendingData==0 );
157836 assert( p->pSegments==0 );
157837
157838 /* Free any prepared statements held */
157839 sqlite3_finalize(p->pSeekStmt);
157840 for(i=0; i<SizeofArray(p->aStmt); i++){
157841 sqlite3_finalize(p->aStmt[i]);
157842 }
157843 sqlite3_free(p->zSegmentsTbl);
157844 sqlite3_free(p->zReadExprlist);
157845 sqlite3_free(p->zWriteExprlist);
157846 sqlite3_free(p->zContentTbl);
157847 sqlite3_free(p->zLanguageid);
157848
157849 /* Invoke the tokenizer destructor to free the tokenizer. */
157850 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
157851
157852 sqlite3_free(p);
157853 return SQLITE_OK;
157854}
157855
157856/*
157857** Write an error message into *pzErr
157858*/
157859SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
157860 va_list ap;
157861 sqlite3_free(*pzErr);
157862 va_start(ap, zFormat);
157863 *pzErr = sqlite3_vmprintf(zFormat, ap);
157864 va_end(ap);
157865}
157866
157867/*
157868** Construct one or more SQL statements from the format string given
157869** and then evaluate those statements. The success code is written
157870** into *pRc.
157871**
157872** If *pRc is initially non-zero then this routine is a no-op.
157873*/
157874static void fts3DbExec(
157875 int *pRc, /* Success code */
157876 sqlite3 *db, /* Database in which to run SQL */
157877 const char *zFormat, /* Format string for SQL */
157878 ... /* Arguments to the format string */
157879){
157880 va_list ap;
157881 char *zSql;
157882 if( *pRc ) return;
157883 va_start(ap, zFormat);
157884 zSql = sqlite3_vmprintf(zFormat, ap);
157885 va_end(ap);
157886 if( zSql==0 ){
157887 *pRc = SQLITE_NOMEM;
157888 }else{
157889 *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
157890 sqlite3_free(zSql);
157891 }
157892}
157893
157894/*
157895** The xDestroy() virtual table method.
157896*/
157897static int fts3DestroyMethod(sqlite3_vtab *pVtab){
157898 Fts3Table *p = (Fts3Table *)pVtab;
157899 int rc = SQLITE_OK; /* Return code */
157900 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
157901 sqlite3 *db = p->db; /* Database handle */
157902
157903 /* Drop the shadow tables */
157904 if( p->zContentTbl==0 ){
157905 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
157906 }
157907 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
157908 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
157909 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
157910 fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
157911
157912 /* If everything has worked, invoke fts3DisconnectMethod() to free the
157913 ** memory associated with the Fts3Table structure and return SQLITE_OK.
157914 ** Otherwise, return an SQLite error code.
157915 */
157916 return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
157917}
157918
157919
157920/*
157921** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
157922** passed as the first argument. This is done as part of the xConnect()
157923** and xCreate() methods.
157924**
157925** If *pRc is non-zero when this function is called, it is a no-op.
157926** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
157927** before returning.
157928*/
157929static void fts3DeclareVtab(int *pRc, Fts3Table *p){
157930 if( *pRc==SQLITE_OK ){
157931 int i; /* Iterator variable */
157932 int rc; /* Return code */
157933 char *zSql; /* SQL statement passed to declare_vtab() */
157934 char *zCols; /* List of user defined columns */
157935 const char *zLanguageid;
157936
157937 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
157938 sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
157939
157940 /* Create a list of user columns for the virtual table */
157941 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
157942 for(i=1; zCols && i<p->nColumn; i++){
157943 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
157944 }
157945
157946 /* Create the whole "CREATE TABLE" statement to pass to SQLite */
157947 zSql = sqlite3_mprintf(
157948 "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
157949 zCols, p->zName, zLanguageid
157950 );
157951 if( !zCols || !zSql ){
157952 rc = SQLITE_NOMEM;
157953 }else{
157954 rc = sqlite3_declare_vtab(p->db, zSql);
157955 }
157956
157957 sqlite3_free(zSql);
157958 sqlite3_free(zCols);
157959 *pRc = rc;
157960 }
157961}
157962
157963/*
157964** Create the %_stat table if it does not already exist.
157965*/
157966SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
157967 fts3DbExec(pRc, p->db,
157968 "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
157969 "(id INTEGER PRIMARY KEY, value BLOB);",
157970 p->zDb, p->zName
157971 );
157972 if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
157973}
157974
157975/*
157976** Create the backing store tables (%_content, %_segments and %_segdir)
157977** required by the FTS3 table passed as the only argument. This is done
157978** as part of the vtab xCreate() method.
157979**
157980** If the p->bHasDocsize boolean is true (indicating that this is an
157981** FTS4 table, not an FTS3 table) then also create the %_docsize and
157982** %_stat tables required by FTS4.
157983*/
157984static int fts3CreateTables(Fts3Table *p){
157985 int rc = SQLITE_OK; /* Return code */
157986 int i; /* Iterator variable */
157987 sqlite3 *db = p->db; /* The database connection */
157988
157989 if( p->zContentTbl==0 ){
157990 const char *zLanguageid = p->zLanguageid;
157991 char *zContentCols; /* Columns of %_content table */
157992
157993 /* Create a list of user columns for the content table */
157994 zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
157995 for(i=0; zContentCols && i<p->nColumn; i++){
157996 char *z = p->azColumn[i];
157997 zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
157998 }
157999 if( zLanguageid && zContentCols ){
158000 zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
158001 }
158002 if( zContentCols==0 ) rc = SQLITE_NOMEM;
158003
158004 /* Create the content table */
158005 fts3DbExec(&rc, db,
158006 "CREATE TABLE %Q.'%q_content'(%s)",
158007 p->zDb, p->zName, zContentCols
158008 );
158009 sqlite3_free(zContentCols);
158010 }
158011
158012 /* Create other tables */
158013 fts3DbExec(&rc, db,
158014 "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
158015 p->zDb, p->zName
158016 );
158017 fts3DbExec(&rc, db,
158018 "CREATE TABLE %Q.'%q_segdir'("
158019 "level INTEGER,"
158020 "idx INTEGER,"
158021 "start_block INTEGER,"
158022 "leaves_end_block INTEGER,"
158023 "end_block INTEGER,"
158024 "root BLOB,"
158025 "PRIMARY KEY(level, idx)"
158026 ");",
158027 p->zDb, p->zName
158028 );
158029 if( p->bHasDocsize ){
158030 fts3DbExec(&rc, db,
158031 "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
158032 p->zDb, p->zName
158033 );
158034 }
158035 assert( p->bHasStat==p->bFts4 );
158036 if( p->bHasStat ){
158037 sqlite3Fts3CreateStatTable(&rc, p);
158038 }
158039 return rc;
158040}
158041
158042/*
158043** Store the current database page-size in bytes in p->nPgsz.
158044**
158045** If *pRc is non-zero when this function is called, it is a no-op.
158046** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
158047** before returning.
158048*/
158049static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
158050 if( *pRc==SQLITE_OK ){
158051 int rc; /* Return code */
158052 char *zSql; /* SQL text "PRAGMA %Q.page_size" */
158053 sqlite3_stmt *pStmt; /* Compiled "PRAGMA %Q.page_size" statement */
158054
158055 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
158056 if( !zSql ){
158057 rc = SQLITE_NOMEM;
158058 }else{
158059 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
158060 if( rc==SQLITE_OK ){
158061 sqlite3_step(pStmt);
158062 p->nPgsz = sqlite3_column_int(pStmt, 0);
158063 rc = sqlite3_finalize(pStmt);
158064 }else if( rc==SQLITE_AUTH ){
158065 p->nPgsz = 1024;
158066 rc = SQLITE_OK;
158067 }
158068 }
158069 assert( p->nPgsz>0 || rc!=SQLITE_OK );
158070 sqlite3_free(zSql);
158071 *pRc = rc;
158072 }
158073}
158074
158075/*
158076** "Special" FTS4 arguments are column specifications of the following form:
158077**
158078** <key> = <value>
158079**
158080** There may not be whitespace surrounding the "=" character. The <value>
158081** term may be quoted, but the <key> may not.
158082*/
158083static int fts3IsSpecialColumn(
158084 const char *z,
158085 int *pnKey,
158086 char **pzValue
158087){
158088 char *zValue;
158089 const char *zCsr = z;
158090
158091 while( *zCsr!='=' ){
158092 if( *zCsr=='\0' ) return 0;
158093 zCsr++;
158094 }
158095
158096 *pnKey = (int)(zCsr-z);
158097 zValue = sqlite3_mprintf("%s", &zCsr[1]);
158098 if( zValue ){
158099 sqlite3Fts3Dequote(zValue);
158100 }
158101 *pzValue = zValue;
158102 return 1;
158103}
158104
158105/*
158106** Append the output of a printf() style formatting to an existing string.
158107*/
158108static void fts3Appendf(
158109 int *pRc, /* IN/OUT: Error code */
158110 char **pz, /* IN/OUT: Pointer to string buffer */
158111 const char *zFormat, /* Printf format string to append */
158112 ... /* Arguments for printf format string */
158113){
158114 if( *pRc==SQLITE_OK ){
158115 va_list ap;
158116 char *z;
158117 va_start(ap, zFormat);
158118 z = sqlite3_vmprintf(zFormat, ap);
158119 va_end(ap);
158120 if( z && *pz ){
158121 char *z2 = sqlite3_mprintf("%s%s", *pz, z);
158122 sqlite3_free(z);
158123 z = z2;
158124 }
158125 if( z==0 ) *pRc = SQLITE_NOMEM;
158126 sqlite3_free(*pz);
158127 *pz = z;
158128 }
158129}
158130
158131/*
158132** Return a copy of input string zInput enclosed in double-quotes (") and
158133** with all double quote characters escaped. For example:
158134**
158135** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
158136**
158137** The pointer returned points to memory obtained from sqlite3_malloc(). It
158138** is the callers responsibility to call sqlite3_free() to release this
158139** memory.
158140*/
158141static char *fts3QuoteId(char const *zInput){
158142 int nRet;
158143 char *zRet;
158144 nRet = 2 + (int)strlen(zInput)*2 + 1;
158145 zRet = sqlite3_malloc(nRet);
158146 if( zRet ){
158147 int i;
158148 char *z = zRet;
158149 *(z++) = '"';
158150 for(i=0; zInput[i]; i++){
158151 if( zInput[i]=='"' ) *(z++) = '"';
158152 *(z++) = zInput[i];
158153 }
158154 *(z++) = '"';
158155 *(z++) = '\0';
158156 }
158157 return zRet;
158158}
158159
158160/*
158161** Return a list of comma separated SQL expressions and a FROM clause that
158162** could be used in a SELECT statement such as the following:
158163**
158164** SELECT <list of expressions> FROM %_content AS x ...
158165**
158166** to return the docid, followed by each column of text data in order
158167** from left to write. If parameter zFunc is not NULL, then instead of
158168** being returned directly each column of text data is passed to an SQL
158169** function named zFunc first. For example, if zFunc is "unzip" and the
158170** table has the three user-defined columns "a", "b", and "c", the following
158171** string is returned:
158172**
158173** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
158174**
158175** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
158176** is the responsibility of the caller to eventually free it.
158177**
158178** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
158179** a NULL pointer is returned). Otherwise, if an OOM error is encountered
158180** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
158181** no error occurs, *pRc is left unmodified.
158182*/
158183static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
158184 char *zRet = 0;
158185 char *zFree = 0;
158186 char *zFunction;
158187 int i;
158188
158189 if( p->zContentTbl==0 ){
158190 if( !zFunc ){
158191 zFunction = "";
158192 }else{
158193 zFree = zFunction = fts3QuoteId(zFunc);
158194 }
158195 fts3Appendf(pRc, &zRet, "docid");
158196 for(i=0; i<p->nColumn; i++){
158197 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
158198 }
158199 if( p->zLanguageid ){
158200 fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
158201 }
158202 sqlite3_free(zFree);
158203 }else{
158204 fts3Appendf(pRc, &zRet, "rowid");
158205 for(i=0; i<p->nColumn; i++){
158206 fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
158207 }
158208 if( p->zLanguageid ){
158209 fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
158210 }
158211 }
158212 fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x",
158213 p->zDb,
158214 (p->zContentTbl ? p->zContentTbl : p->zName),
158215 (p->zContentTbl ? "" : "_content")
158216 );
158217 return zRet;
158218}
158219
158220/*
158221** Return a list of N comma separated question marks, where N is the number
158222** of columns in the %_content table (one for the docid plus one for each
158223** user-defined text column).
158224**
158225** If argument zFunc is not NULL, then all but the first question mark
158226** is preceded by zFunc and an open bracket, and followed by a closed
158227** bracket. For example, if zFunc is "zip" and the FTS3 table has three
158228** user-defined text columns, the following string is returned:
158229**
158230** "?, zip(?), zip(?), zip(?)"
158231**
158232** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
158233** is the responsibility of the caller to eventually free it.
158234**
158235** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
158236** a NULL pointer is returned). Otherwise, if an OOM error is encountered
158237** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
158238** no error occurs, *pRc is left unmodified.
158239*/
158240static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
158241 char *zRet = 0;
158242 char *zFree = 0;
158243 char *zFunction;
158244 int i;
158245
158246 if( !zFunc ){
158247 zFunction = "";
158248 }else{
158249 zFree = zFunction = fts3QuoteId(zFunc);
158250 }
158251 fts3Appendf(pRc, &zRet, "?");
158252 for(i=0; i<p->nColumn; i++){
158253 fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
158254 }
158255 if( p->zLanguageid ){
158256 fts3Appendf(pRc, &zRet, ", ?");
158257 }
158258 sqlite3_free(zFree);
158259 return zRet;
158260}
158261
158262/*
158263** This function interprets the string at (*pp) as a non-negative integer
158264** value. It reads the integer and sets *pnOut to the value read, then
158265** sets *pp to point to the byte immediately following the last byte of
158266** the integer value.
158267**
158268** Only decimal digits ('0'..'9') may be part of an integer value.
158269**
158270** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
158271** the output value undefined. Otherwise SQLITE_OK is returned.
158272**
158273** This function is used when parsing the "prefix=" FTS4 parameter.
158274*/
158275static int fts3GobbleInt(const char **pp, int *pnOut){
158276 const int MAX_NPREFIX = 10000000;
158277 const char *p; /* Iterator pointer */
158278 int nInt = 0; /* Output value */
158279
158280 for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
158281 nInt = nInt * 10 + (p[0] - '0');
158282 if( nInt>MAX_NPREFIX ){
158283 nInt = 0;
158284 break;
158285 }
158286 }
158287 if( p==*pp ) return SQLITE_ERROR;
158288 *pnOut = nInt;
158289 *pp = p;
158290 return SQLITE_OK;
158291}
158292
158293/*
158294** This function is called to allocate an array of Fts3Index structures
158295** representing the indexes maintained by the current FTS table. FTS tables
158296** always maintain the main "terms" index, but may also maintain one or
158297** more "prefix" indexes, depending on the value of the "prefix=" parameter
158298** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
158299**
158300** Argument zParam is passed the value of the "prefix=" option if one was
158301** specified, or NULL otherwise.
158302**
158303** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
158304** the allocated array. *pnIndex is set to the number of elements in the
158305** array. If an error does occur, an SQLite error code is returned.
158306**
158307** Regardless of whether or not an error is returned, it is the responsibility
158308** of the caller to call sqlite3_free() on the output array to free it.
158309*/
158310static int fts3PrefixParameter(
158311 const char *zParam, /* ABC in prefix=ABC parameter to parse */
158312 int *pnIndex, /* OUT: size of *apIndex[] array */
158313 struct Fts3Index **apIndex /* OUT: Array of indexes for this table */
158314){
158315 struct Fts3Index *aIndex; /* Allocated array */
158316 int nIndex = 1; /* Number of entries in array */
158317
158318 if( zParam && zParam[0] ){
158319 const char *p;
158320 nIndex++;
158321 for(p=zParam; *p; p++){
158322 if( *p==',' ) nIndex++;
158323 }
158324 }
158325
158326 aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
158327 *apIndex = aIndex;
158328 if( !aIndex ){
158329 return SQLITE_NOMEM;
158330 }
158331
158332 memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
158333 if( zParam ){
158334 const char *p = zParam;
158335 int i;
158336 for(i=1; i<nIndex; i++){
158337 int nPrefix = 0;
158338 if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
158339 assert( nPrefix>=0 );
158340 if( nPrefix==0 ){
158341 nIndex--;
158342 i--;
158343 }else{
158344 aIndex[i].nPrefix = nPrefix;
158345 }
158346 p++;
158347 }
158348 }
158349
158350 *pnIndex = nIndex;
158351 return SQLITE_OK;
158352}
158353
158354/*
158355** This function is called when initializing an FTS4 table that uses the
158356** content=xxx option. It determines the number of and names of the columns
158357** of the new FTS4 table.
158358**
158359** The third argument passed to this function is the value passed to the
158360** config=xxx option (i.e. "xxx"). This function queries the database for
158361** a table of that name. If found, the output variables are populated
158362** as follows:
158363**
158364** *pnCol: Set to the number of columns table xxx has,
158365**
158366** *pnStr: Set to the total amount of space required to store a copy
158367** of each columns name, including the nul-terminator.
158368**
158369** *pazCol: Set to point to an array of *pnCol strings. Each string is
158370** the name of the corresponding column in table xxx. The array
158371** and its contents are allocated using a single allocation. It
158372** is the responsibility of the caller to free this allocation
158373** by eventually passing the *pazCol value to sqlite3_free().
158374**
158375** If the table cannot be found, an error code is returned and the output
158376** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
158377** returned (and the output variables are undefined).
158378*/
158379static int fts3ContentColumns(
158380 sqlite3 *db, /* Database handle */
158381 const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */
158382 const char *zTbl, /* Name of content table */
158383 const char ***pazCol, /* OUT: Malloc'd array of column names */
158384 int *pnCol, /* OUT: Size of array *pazCol */
158385 int *pnStr, /* OUT: Bytes of string content */
158386 char **pzErr /* OUT: error message */
158387){
158388 int rc = SQLITE_OK; /* Return code */
158389 char *zSql; /* "SELECT *" statement on zTbl */
158390 sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */
158391
158392 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
158393 if( !zSql ){
158394 rc = SQLITE_NOMEM;
158395 }else{
158396 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
158397 if( rc!=SQLITE_OK ){
158398 sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db));
158399 }
158400 }
158401 sqlite3_free(zSql);
158402
158403 if( rc==SQLITE_OK ){
158404 const char **azCol; /* Output array */
158405 int nStr = 0; /* Size of all column names (incl. 0x00) */
158406 int nCol; /* Number of table columns */
158407 int i; /* Used to iterate through columns */
158408
158409 /* Loop through the returned columns. Set nStr to the number of bytes of
158410 ** space required to store a copy of each column name, including the
158411 ** nul-terminator byte. */
158412 nCol = sqlite3_column_count(pStmt);
158413 for(i=0; i<nCol; i++){
158414 const char *zCol = sqlite3_column_name(pStmt, i);
158415 nStr += (int)strlen(zCol) + 1;
158416 }
158417
158418 /* Allocate and populate the array to return. */
158419 azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr);
158420 if( azCol==0 ){
158421 rc = SQLITE_NOMEM;
158422 }else{
158423 char *p = (char *)&azCol[nCol];
158424 for(i=0; i<nCol; i++){
158425 const char *zCol = sqlite3_column_name(pStmt, i);
158426 int n = (int)strlen(zCol)+1;
158427 memcpy(p, zCol, n);
158428 azCol[i] = p;
158429 p += n;
158430 }
158431 }
158432 sqlite3_finalize(pStmt);
158433
158434 /* Set the output variables. */
158435 *pnCol = nCol;
158436 *pnStr = nStr;
158437 *pazCol = azCol;
158438 }
158439
158440 return rc;
158441}
158442
158443/*
158444** This function is the implementation of both the xConnect and xCreate
158445** methods of the FTS3 virtual table.
158446**
158447** The argv[] array contains the following:
158448**
158449** argv[0] -> module name ("fts3" or "fts4")
158450** argv[1] -> database name
158451** argv[2] -> table name
158452** argv[...] -> "column name" and other module argument fields.
158453*/
158454static int fts3InitVtab(
158455 int isCreate, /* True for xCreate, false for xConnect */
158456 sqlite3 *db, /* The SQLite database connection */
158457 void *pAux, /* Hash table containing tokenizers */
158458 int argc, /* Number of elements in argv array */
158459 const char * const *argv, /* xCreate/xConnect argument array */
158460 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
158461 char **pzErr /* Write any error message here */
158462){
158463 Fts3Hash *pHash = (Fts3Hash *)pAux;
158464 Fts3Table *p = 0; /* Pointer to allocated vtab */
158465 int rc = SQLITE_OK; /* Return code */
158466 int i; /* Iterator variable */
158467 int nByte; /* Size of allocation used for *p */
158468 int iCol; /* Column index */
158469 int nString = 0; /* Bytes required to hold all column names */
158470 int nCol = 0; /* Number of columns in the FTS table */
158471 char *zCsr; /* Space for holding column names */
158472 int nDb; /* Bytes required to hold database name */
158473 int nName; /* Bytes required to hold table name */
158474 int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
158475 const char **aCol; /* Array of column names */
158476 sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
158477
158478 int nIndex = 0; /* Size of aIndex[] array */
158479 struct Fts3Index *aIndex = 0; /* Array of indexes for this table */
158480
158481 /* The results of parsing supported FTS4 key=value options: */
158482 int bNoDocsize = 0; /* True to omit %_docsize table */
158483 int bDescIdx = 0; /* True to store descending indexes */
158484 char *zPrefix = 0; /* Prefix parameter value (or NULL) */
158485 char *zCompress = 0; /* compress=? parameter (or NULL) */
158486 char *zUncompress = 0; /* uncompress=? parameter (or NULL) */
158487 char *zContent = 0; /* content=? parameter (or NULL) */
158488 char *zLanguageid = 0; /* languageid=? parameter (or NULL) */
158489 char **azNotindexed = 0; /* The set of notindexed= columns */
158490 int nNotindexed = 0; /* Size of azNotindexed[] array */
158491
158492 assert( strlen(argv[0])==4 );
158493 assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
158494 || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
158495 );
158496
158497 nDb = (int)strlen(argv[1]) + 1;
158498 nName = (int)strlen(argv[2]) + 1;
158499
158500 nByte = sizeof(const char *) * (argc-2);
158501 aCol = (const char **)sqlite3_malloc(nByte);
158502 if( aCol ){
158503 memset((void*)aCol, 0, nByte);
158504 azNotindexed = (char **)sqlite3_malloc(nByte);
158505 }
158506 if( azNotindexed ){
158507 memset(azNotindexed, 0, nByte);
158508 }
158509 if( !aCol || !azNotindexed ){
158510 rc = SQLITE_NOMEM;
158511 goto fts3_init_out;
158512 }
158513
158514 /* Loop through all of the arguments passed by the user to the FTS3/4
158515 ** module (i.e. all the column names and special arguments). This loop
158516 ** does the following:
158517 **
158518 ** + Figures out the number of columns the FTSX table will have, and
158519 ** the number of bytes of space that must be allocated to store copies
158520 ** of the column names.
158521 **
158522 ** + If there is a tokenizer specification included in the arguments,
158523 ** initializes the tokenizer pTokenizer.
158524 */
158525 for(i=3; rc==SQLITE_OK && i<argc; i++){
158526 char const *z = argv[i];
158527 int nKey;
158528 char *zVal;
158529
158530 /* Check if this is a tokenizer specification */
158531 if( !pTokenizer
158532 && strlen(z)>8
158533 && 0==sqlite3_strnicmp(z, "tokenize", 8)
158534 && 0==sqlite3Fts3IsIdChar(z[8])
158535 ){
158536 rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
158537 }
158538
158539 /* Check if it is an FTS4 special argument. */
158540 else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
158541 struct Fts4Option {
158542 const char *zOpt;
158543 int nOpt;
158544 } aFts4Opt[] = {
158545 { "matchinfo", 9 }, /* 0 -> MATCHINFO */
158546 { "prefix", 6 }, /* 1 -> PREFIX */
158547 { "compress", 8 }, /* 2 -> COMPRESS */
158548 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
158549 { "order", 5 }, /* 4 -> ORDER */
158550 { "content", 7 }, /* 5 -> CONTENT */
158551 { "languageid", 10 }, /* 6 -> LANGUAGEID */
158552 { "notindexed", 10 } /* 7 -> NOTINDEXED */
158553 };
158554
158555 int iOpt;
158556 if( !zVal ){
158557 rc = SQLITE_NOMEM;
158558 }else{
158559 for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
158560 struct Fts4Option *pOp = &aFts4Opt[iOpt];
158561 if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
158562 break;
158563 }
158564 }
158565 switch( iOpt ){
158566 case 0: /* MATCHINFO */
158567 if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
158568 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
158569 rc = SQLITE_ERROR;
158570 }
158571 bNoDocsize = 1;
158572 break;
158573
158574 case 1: /* PREFIX */
158575 sqlite3_free(zPrefix);
158576 zPrefix = zVal;
158577 zVal = 0;
158578 break;
158579
158580 case 2: /* COMPRESS */
158581 sqlite3_free(zCompress);
158582 zCompress = zVal;
158583 zVal = 0;
158584 break;
158585
158586 case 3: /* UNCOMPRESS */
158587 sqlite3_free(zUncompress);
158588 zUncompress = zVal;
158589 zVal = 0;
158590 break;
158591
158592 case 4: /* ORDER */
158593 if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
158594 && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4))
158595 ){
158596 sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
158597 rc = SQLITE_ERROR;
158598 }
158599 bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
158600 break;
158601
158602 case 5: /* CONTENT */
158603 sqlite3_free(zContent);
158604 zContent = zVal;
158605 zVal = 0;
158606 break;
158607
158608 case 6: /* LANGUAGEID */
158609 assert( iOpt==6 );
158610 sqlite3_free(zLanguageid);
158611 zLanguageid = zVal;
158612 zVal = 0;
158613 break;
158614
158615 case 7: /* NOTINDEXED */
158616 azNotindexed[nNotindexed++] = zVal;
158617 zVal = 0;
158618 break;
158619
158620 default:
158621 assert( iOpt==SizeofArray(aFts4Opt) );
158622 sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
158623 rc = SQLITE_ERROR;
158624 break;
158625 }
158626 sqlite3_free(zVal);
158627 }
158628 }
158629
158630 /* Otherwise, the argument is a column name. */
158631 else {
158632 nString += (int)(strlen(z) + 1);
158633 aCol[nCol++] = z;
158634 }
158635 }
158636
158637 /* If a content=xxx option was specified, the following:
158638 **
158639 ** 1. Ignore any compress= and uncompress= options.
158640 **
158641 ** 2. If no column names were specified as part of the CREATE VIRTUAL
158642 ** TABLE statement, use all columns from the content table.
158643 */
158644 if( rc==SQLITE_OK && zContent ){
158645 sqlite3_free(zCompress);
158646 sqlite3_free(zUncompress);
158647 zCompress = 0;
158648 zUncompress = 0;
158649 if( nCol==0 ){
158650 sqlite3_free((void*)aCol);
158651 aCol = 0;
158652 rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
158653
158654 /* If a languageid= option was specified, remove the language id
158655 ** column from the aCol[] array. */
158656 if( rc==SQLITE_OK && zLanguageid ){
158657 int j;
158658 for(j=0; j<nCol; j++){
158659 if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
158660 int k;
158661 for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
158662 nCol--;
158663 break;
158664 }
158665 }
158666 }
158667 }
158668 }
158669 if( rc!=SQLITE_OK ) goto fts3_init_out;
158670
158671 if( nCol==0 ){
158672 assert( nString==0 );
158673 aCol[0] = "content";
158674 nString = 8;
158675 nCol = 1;
158676 }
158677
158678 if( pTokenizer==0 ){
158679 rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
158680 if( rc!=SQLITE_OK ) goto fts3_init_out;
158681 }
158682 assert( pTokenizer );
158683
158684 rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
158685 if( rc==SQLITE_ERROR ){
158686 assert( zPrefix );
158687 sqlite3Fts3ErrMsg(pzErr, "error parsing prefix parameter: %s", zPrefix);
158688 }
158689 if( rc!=SQLITE_OK ) goto fts3_init_out;
158690
158691 /* Allocate and populate the Fts3Table structure. */
158692 nByte = sizeof(Fts3Table) + /* Fts3Table */
158693 nCol * sizeof(char *) + /* azColumn */
158694 nIndex * sizeof(struct Fts3Index) + /* aIndex */
158695 nCol * sizeof(u8) + /* abNotindexed */
158696 nName + /* zName */
158697 nDb + /* zDb */
158698 nString; /* Space for azColumn strings */
158699 p = (Fts3Table*)sqlite3_malloc(nByte);
158700 if( p==0 ){
158701 rc = SQLITE_NOMEM;
158702 goto fts3_init_out;
158703 }
158704 memset(p, 0, nByte);
158705 p->db = db;
158706 p->nColumn = nCol;
158707 p->nPendingData = 0;
158708 p->azColumn = (char **)&p[1];
158709 p->pTokenizer = pTokenizer;
158710 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
158711 p->bHasDocsize = (isFts4 && bNoDocsize==0);
158712 p->bHasStat = (u8)isFts4;
158713 p->bFts4 = (u8)isFts4;
158714 p->bDescIdx = (u8)bDescIdx;
158715 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
158716 p->zContentTbl = zContent;
158717 p->zLanguageid = zLanguageid;
158718 zContent = 0;
158719 zLanguageid = 0;
158720 TESTONLY( p->inTransaction = -1 );
158721 TESTONLY( p->mxSavepoint = -1 );
158722
158723 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
158724 memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
158725 p->nIndex = nIndex;
158726 for(i=0; i<nIndex; i++){
158727 fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
158728 }
158729 p->abNotindexed = (u8 *)&p->aIndex[nIndex];
158730
158731 /* Fill in the zName and zDb fields of the vtab structure. */
158732 zCsr = (char *)&p->abNotindexed[nCol];
158733 p->zName = zCsr;
158734 memcpy(zCsr, argv[2], nName);
158735 zCsr += nName;
158736 p->zDb = zCsr;
158737 memcpy(zCsr, argv[1], nDb);
158738 zCsr += nDb;
158739
158740 /* Fill in the azColumn array */
158741 for(iCol=0; iCol<nCol; iCol++){
158742 char *z;
158743 int n = 0;
158744 z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
158745 if( n>0 ){
158746 memcpy(zCsr, z, n);
158747 }
158748 zCsr[n] = '\0';
158749 sqlite3Fts3Dequote(zCsr);
158750 p->azColumn[iCol] = zCsr;
158751 zCsr += n+1;
158752 assert( zCsr <= &((char *)p)[nByte] );
158753 }
158754
158755 /* Fill in the abNotindexed array */
158756 for(iCol=0; iCol<nCol; iCol++){
158757 int n = (int)strlen(p->azColumn[iCol]);
158758 for(i=0; i<nNotindexed; i++){
158759 char *zNot = azNotindexed[i];
158760 if( zNot && n==(int)strlen(zNot)
158761 && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
158762 ){
158763 p->abNotindexed[iCol] = 1;
158764 sqlite3_free(zNot);
158765 azNotindexed[i] = 0;
158766 }
158767 }
158768 }
158769 for(i=0; i<nNotindexed; i++){
158770 if( azNotindexed[i] ){
158771 sqlite3Fts3ErrMsg(pzErr, "no such column: %s", azNotindexed[i]);
158772 rc = SQLITE_ERROR;
158773 }
158774 }
158775
158776 if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){
158777 char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
158778 rc = SQLITE_ERROR;
158779 sqlite3Fts3ErrMsg(pzErr, "missing %s parameter in fts4 constructor", zMiss);
158780 }
158781 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
158782 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
158783 if( rc!=SQLITE_OK ) goto fts3_init_out;
158784
158785 /* If this is an xCreate call, create the underlying tables in the
158786 ** database. TODO: For xConnect(), it could verify that said tables exist.
158787 */
158788 if( isCreate ){
158789 rc = fts3CreateTables(p);
158790 }
158791
158792 /* Check to see if a legacy fts3 table has been "upgraded" by the
158793 ** addition of a %_stat table so that it can use incremental merge.
158794 */
158795 if( !isFts4 && !isCreate ){
158796 p->bHasStat = 2;
158797 }
158798
158799 /* Figure out the page-size for the database. This is required in order to
158800 ** estimate the cost of loading large doclists from the database. */
158801 fts3DatabasePageSize(&rc, p);
158802 p->nNodeSize = p->nPgsz-35;
158803
158804 /* Declare the table schema to SQLite. */
158805 fts3DeclareVtab(&rc, p);
158806
158807fts3_init_out:
158808 sqlite3_free(zPrefix);
158809 sqlite3_free(aIndex);
158810 sqlite3_free(zCompress);
158811 sqlite3_free(zUncompress);
158812 sqlite3_free(zContent);
158813 sqlite3_free(zLanguageid);
158814 for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]);
158815 sqlite3_free((void *)aCol);
158816 sqlite3_free((void *)azNotindexed);
158817 if( rc!=SQLITE_OK ){
158818 if( p ){
158819 fts3DisconnectMethod((sqlite3_vtab *)p);
158820 }else if( pTokenizer ){
158821 pTokenizer->pModule->xDestroy(pTokenizer);
158822 }
158823 }else{
158824 assert( p->pSegments==0 );
158825 *ppVTab = &p->base;
158826 }
158827 return rc;
158828}
158829
158830/*
158831** The xConnect() and xCreate() methods for the virtual table. All the
158832** work is done in function fts3InitVtab().
158833*/
158834static int fts3ConnectMethod(
158835 sqlite3 *db, /* Database connection */
158836 void *pAux, /* Pointer to tokenizer hash table */
158837 int argc, /* Number of elements in argv array */
158838 const char * const *argv, /* xCreate/xConnect argument array */
158839 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
158840 char **pzErr /* OUT: sqlite3_malloc'd error message */
158841){
158842 return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
158843}
158844static int fts3CreateMethod(
158845 sqlite3 *db, /* Database connection */
158846 void *pAux, /* Pointer to tokenizer hash table */
158847 int argc, /* Number of elements in argv array */
158848 const char * const *argv, /* xCreate/xConnect argument array */
158849 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
158850 char **pzErr /* OUT: sqlite3_malloc'd error message */
158851){
158852 return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
158853}
158854
158855/*
158856** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
158857** extension is currently being used by a version of SQLite too old to
158858** support estimatedRows. In that case this function is a no-op.
158859*/
158860static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
158861#if SQLITE_VERSION_NUMBER>=3008002
158862 if( sqlite3_libversion_number()>=3008002 ){
158863 pIdxInfo->estimatedRows = nRow;
158864 }
158865#endif
158866}
158867
158868/*
158869** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
158870** extension is currently being used by a version of SQLite too old to
158871** support index-info flags. In that case this function is a no-op.
158872*/
158873static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){
158874#if SQLITE_VERSION_NUMBER>=3008012
158875 if( sqlite3_libversion_number()>=3008012 ){
158876 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
158877 }
158878#endif
158879}
158880
158881/*
158882** Implementation of the xBestIndex method for FTS3 tables. There
158883** are three possible strategies, in order of preference:
158884**
158885** 1. Direct lookup by rowid or docid.
158886** 2. Full-text search using a MATCH operator on a non-docid column.
158887** 3. Linear scan of %_content table.
158888*/
158889static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
158890 Fts3Table *p = (Fts3Table *)pVTab;
158891 int i; /* Iterator variable */
158892 int iCons = -1; /* Index of constraint to use */
158893
158894 int iLangidCons = -1; /* Index of langid=x constraint, if present */
158895 int iDocidGe = -1; /* Index of docid>=x constraint, if present */
158896 int iDocidLe = -1; /* Index of docid<=x constraint, if present */
158897 int iIdx;
158898
158899 /* By default use a full table scan. This is an expensive option,
158900 ** so search through the constraints to see if a more efficient
158901 ** strategy is possible.
158902 */
158903 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
158904 pInfo->estimatedCost = 5000000;
158905 for(i=0; i<pInfo->nConstraint; i++){
158906 int bDocid; /* True if this constraint is on docid */
158907 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
158908 if( pCons->usable==0 ){
158909 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
158910 /* There exists an unusable MATCH constraint. This means that if
158911 ** the planner does elect to use the results of this call as part
158912 ** of the overall query plan the user will see an "unable to use
158913 ** function MATCH in the requested context" error. To discourage
158914 ** this, return a very high cost here. */
158915 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
158916 pInfo->estimatedCost = 1e50;
158917 fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
158918 return SQLITE_OK;
158919 }
158920 continue;
158921 }
158922
158923 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
158924
158925 /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
158926 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
158927 pInfo->idxNum = FTS3_DOCID_SEARCH;
158928 pInfo->estimatedCost = 1.0;
158929 iCons = i;
158930 }
158931
158932 /* A MATCH constraint. Use a full-text search.
158933 **
158934 ** If there is more than one MATCH constraint available, use the first
158935 ** one encountered. If there is both a MATCH constraint and a direct
158936 ** rowid/docid lookup, prefer the MATCH strategy. This is done even
158937 ** though the rowid/docid lookup is faster than a MATCH query, selecting
158938 ** it would lead to an "unable to use function MATCH in the requested
158939 ** context" error.
158940 */
158941 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
158942 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
158943 ){
158944 pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
158945 pInfo->estimatedCost = 2.0;
158946 iCons = i;
158947 }
158948
158949 /* Equality constraint on the langid column */
158950 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
158951 && pCons->iColumn==p->nColumn + 2
158952 ){
158953 iLangidCons = i;
158954 }
158955
158956 if( bDocid ){
158957 switch( pCons->op ){
158958 case SQLITE_INDEX_CONSTRAINT_GE:
158959 case SQLITE_INDEX_CONSTRAINT_GT:
158960 iDocidGe = i;
158961 break;
158962
158963 case SQLITE_INDEX_CONSTRAINT_LE:
158964 case SQLITE_INDEX_CONSTRAINT_LT:
158965 iDocidLe = i;
158966 break;
158967 }
158968 }
158969 }
158970
158971 /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */
158972 if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pInfo);
158973
158974 iIdx = 1;
158975 if( iCons>=0 ){
158976 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
158977 pInfo->aConstraintUsage[iCons].omit = 1;
158978 }
158979 if( iLangidCons>=0 ){
158980 pInfo->idxNum |= FTS3_HAVE_LANGID;
158981 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
158982 }
158983 if( iDocidGe>=0 ){
158984 pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
158985 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
158986 }
158987 if( iDocidLe>=0 ){
158988 pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
158989 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
158990 }
158991
158992 /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
158993 ** docid) order. Both ascending and descending are possible.
158994 */
158995 if( pInfo->nOrderBy==1 ){
158996 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
158997 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
158998 if( pOrder->desc ){
158999 pInfo->idxStr = "DESC";
159000 }else{
159001 pInfo->idxStr = "ASC";
159002 }
159003 pInfo->orderByConsumed = 1;
159004 }
159005 }
159006
159007 assert( p->pSegments==0 );
159008 return SQLITE_OK;
159009}
159010
159011/*
159012** Implementation of xOpen method.
159013*/
159014static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
159015 sqlite3_vtab_cursor *pCsr; /* Allocated cursor */
159016
159017 UNUSED_PARAMETER(pVTab);
159018
159019 /* Allocate a buffer large enough for an Fts3Cursor structure. If the
159020 ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
159021 ** if the allocation fails, return SQLITE_NOMEM.
159022 */
159023 *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
159024 if( !pCsr ){
159025 return SQLITE_NOMEM;
159026 }
159027 memset(pCsr, 0, sizeof(Fts3Cursor));
159028 return SQLITE_OK;
159029}
159030
159031/*
159032** Finalize the statement handle at pCsr->pStmt.
159033**
159034** Or, if that statement handle is one created by fts3CursorSeekStmt(),
159035** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
159036** pointer there instead of finalizing it.
159037*/
159038static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
159039 if( pCsr->bSeekStmt ){
159040 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
159041 if( p->pSeekStmt==0 ){
159042 p->pSeekStmt = pCsr->pStmt;
159043 sqlite3_reset(pCsr->pStmt);
159044 pCsr->pStmt = 0;
159045 }
159046 pCsr->bSeekStmt = 0;
159047 }
159048 sqlite3_finalize(pCsr->pStmt);
159049}
159050
159051/*
159052** Free all resources currently held by the cursor passed as the only
159053** argument.
159054*/
159055static void fts3ClearCursor(Fts3Cursor *pCsr){
159056 fts3CursorFinalizeStmt(pCsr);
159057 sqlite3Fts3FreeDeferredTokens(pCsr);
159058 sqlite3_free(pCsr->aDoclist);
159059 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
159060 sqlite3Fts3ExprFree(pCsr->pExpr);
159061 memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
159062}
159063
159064/*
159065** Close the cursor. For additional information see the documentation
159066** on the xClose method of the virtual table interface.
159067*/
159068static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
159069 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
159070 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
159071 fts3ClearCursor(pCsr);
159072 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
159073 sqlite3_free(pCsr);
159074 return SQLITE_OK;
159075}
159076
159077/*
159078** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
159079** compose and prepare an SQL statement of the form:
159080**
159081** "SELECT <columns> FROM %_content WHERE rowid = ?"
159082**
159083** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
159084** it. If an error occurs, return an SQLite error code.
159085*/
159086static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
159087 int rc = SQLITE_OK;
159088 if( pCsr->pStmt==0 ){
159089 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
159090 char *zSql;
159091 if( p->pSeekStmt ){
159092 pCsr->pStmt = p->pSeekStmt;
159093 p->pSeekStmt = 0;
159094 }else{
159095 zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
159096 if( !zSql ) return SQLITE_NOMEM;
159097 rc = sqlite3_prepare_v3(p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0);
159098 sqlite3_free(zSql);
159099 }
159100 if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
159101 }
159102 return rc;
159103}
159104
159105/*
159106** Position the pCsr->pStmt statement so that it is on the row
159107** of the %_content table that contains the last match. Return
159108** SQLITE_OK on success.
159109*/
159110static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
159111 int rc = SQLITE_OK;
159112 if( pCsr->isRequireSeek ){
159113 rc = fts3CursorSeekStmt(pCsr);
159114 if( rc==SQLITE_OK ){
159115 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
159116 pCsr->isRequireSeek = 0;
159117 if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
159118 return SQLITE_OK;
159119 }else{
159120 rc = sqlite3_reset(pCsr->pStmt);
159121 if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
159122 /* If no row was found and no error has occurred, then the %_content
159123 ** table is missing a row that is present in the full-text index.
159124 ** The data structures are corrupt. */
159125 rc = FTS_CORRUPT_VTAB;
159126 pCsr->isEof = 1;
159127 }
159128 }
159129 }
159130 }
159131
159132 if( rc!=SQLITE_OK && pContext ){
159133 sqlite3_result_error_code(pContext, rc);
159134 }
159135 return rc;
159136}
159137
159138/*
159139** This function is used to process a single interior node when searching
159140** a b-tree for a term or term prefix. The node data is passed to this
159141** function via the zNode/nNode parameters. The term to search for is
159142** passed in zTerm/nTerm.
159143**
159144** If piFirst is not NULL, then this function sets *piFirst to the blockid
159145** of the child node that heads the sub-tree that may contain the term.
159146**
159147** If piLast is not NULL, then *piLast is set to the right-most child node
159148** that heads a sub-tree that may contain a term for which zTerm/nTerm is
159149** a prefix.
159150**
159151** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
159152*/
159153static int fts3ScanInteriorNode(
159154 const char *zTerm, /* Term to select leaves for */
159155 int nTerm, /* Size of term zTerm in bytes */
159156 const char *zNode, /* Buffer containing segment interior node */
159157 int nNode, /* Size of buffer at zNode */
159158 sqlite3_int64 *piFirst, /* OUT: Selected child node */
159159 sqlite3_int64 *piLast /* OUT: Selected child node */
159160){
159161 int rc = SQLITE_OK; /* Return code */
159162 const char *zCsr = zNode; /* Cursor to iterate through node */
159163 const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
159164 char *zBuffer = 0; /* Buffer to load terms into */
159165 int nAlloc = 0; /* Size of allocated buffer */
159166 int isFirstTerm = 1; /* True when processing first term on page */
159167 sqlite3_int64 iChild; /* Block id of child node to descend to */
159168
159169 /* Skip over the 'height' varint that occurs at the start of every
159170 ** interior node. Then load the blockid of the left-child of the b-tree
159171 ** node into variable iChild.
159172 **
159173 ** Even if the data structure on disk is corrupted, this (reading two
159174 ** varints from the buffer) does not risk an overread. If zNode is a
159175 ** root node, then the buffer comes from a SELECT statement. SQLite does
159176 ** not make this guarantee explicitly, but in practice there are always
159177 ** either more than 20 bytes of allocated space following the nNode bytes of
159178 ** contents, or two zero bytes. Or, if the node is read from the %_segments
159179 ** table, then there are always 20 bytes of zeroed padding following the
159180 ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
159181 */
159182 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
159183 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
159184 if( zCsr>zEnd ){
159185 return FTS_CORRUPT_VTAB;
159186 }
159187
159188 while( zCsr<zEnd && (piFirst || piLast) ){
159189 int cmp; /* memcmp() result */
159190 int nSuffix; /* Size of term suffix */
159191 int nPrefix = 0; /* Size of term prefix */
159192 int nBuffer; /* Total term size */
159193
159194 /* Load the next term on the node into zBuffer. Use realloc() to expand
159195 ** the size of zBuffer if required. */
159196 if( !isFirstTerm ){
159197 zCsr += fts3GetVarint32(zCsr, &nPrefix);
159198 }
159199 isFirstTerm = 0;
159200 zCsr += fts3GetVarint32(zCsr, &nSuffix);
159201
159202 assert( nPrefix>=0 && nSuffix>=0 );
159203 if( &zCsr[nSuffix]>zEnd ){
159204 rc = FTS_CORRUPT_VTAB;
159205 goto finish_scan;
159206 }
159207 if( nPrefix+nSuffix>nAlloc ){
159208 char *zNew;
159209 nAlloc = (nPrefix+nSuffix) * 2;
159210 zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
159211 if( !zNew ){
159212 rc = SQLITE_NOMEM;
159213 goto finish_scan;
159214 }
159215 zBuffer = zNew;
159216 }
159217 assert( zBuffer );
159218 memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
159219 nBuffer = nPrefix + nSuffix;
159220 zCsr += nSuffix;
159221
159222 /* Compare the term we are searching for with the term just loaded from
159223 ** the interior node. If the specified term is greater than or equal
159224 ** to the term from the interior node, then all terms on the sub-tree
159225 ** headed by node iChild are smaller than zTerm. No need to search
159226 ** iChild.
159227 **
159228 ** If the interior node term is larger than the specified term, then
159229 ** the tree headed by iChild may contain the specified term.
159230 */
159231 cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
159232 if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
159233 *piFirst = iChild;
159234 piFirst = 0;
159235 }
159236
159237 if( piLast && cmp<0 ){
159238 *piLast = iChild;
159239 piLast = 0;
159240 }
159241
159242 iChild++;
159243 };
159244
159245 if( piFirst ) *piFirst = iChild;
159246 if( piLast ) *piLast = iChild;
159247
159248 finish_scan:
159249 sqlite3_free(zBuffer);
159250 return rc;
159251}
159252
159253
159254/*
159255** The buffer pointed to by argument zNode (size nNode bytes) contains an
159256** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
159257** contains a term. This function searches the sub-tree headed by the zNode
159258** node for the range of leaf nodes that may contain the specified term
159259** or terms for which the specified term is a prefix.
159260**
159261** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
159262** left-most leaf node in the tree that may contain the specified term.
159263** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
159264** right-most leaf node that may contain a term for which the specified
159265** term is a prefix.
159266**
159267** It is possible that the range of returned leaf nodes does not contain
159268** the specified term or any terms for which it is a prefix. However, if the
159269** segment does contain any such terms, they are stored within the identified
159270** range. Because this function only inspects interior segment nodes (and
159271** never loads leaf nodes into memory), it is not possible to be sure.
159272**
159273** If an error occurs, an error code other than SQLITE_OK is returned.
159274*/
159275static int fts3SelectLeaf(
159276 Fts3Table *p, /* Virtual table handle */
159277 const char *zTerm, /* Term to select leaves for */
159278 int nTerm, /* Size of term zTerm in bytes */
159279 const char *zNode, /* Buffer containing segment interior node */
159280 int nNode, /* Size of buffer at zNode */
159281 sqlite3_int64 *piLeaf, /* Selected leaf node */
159282 sqlite3_int64 *piLeaf2 /* Selected leaf node */
159283){
159284 int rc = SQLITE_OK; /* Return code */
159285 int iHeight; /* Height of this node in tree */
159286
159287 assert( piLeaf || piLeaf2 );
159288
159289 fts3GetVarint32(zNode, &iHeight);
159290 rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
159291 assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
159292
159293 if( rc==SQLITE_OK && iHeight>1 ){
159294 char *zBlob = 0; /* Blob read from %_segments table */
159295 int nBlob = 0; /* Size of zBlob in bytes */
159296
159297 if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
159298 rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
159299 if( rc==SQLITE_OK ){
159300 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
159301 }
159302 sqlite3_free(zBlob);
159303 piLeaf = 0;
159304 zBlob = 0;
159305 }
159306
159307 if( rc==SQLITE_OK ){
159308 rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
159309 }
159310 if( rc==SQLITE_OK ){
159311 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
159312 }
159313 sqlite3_free(zBlob);
159314 }
159315
159316 return rc;
159317}
159318
159319/*
159320** This function is used to create delta-encoded serialized lists of FTS3
159321** varints. Each call to this function appends a single varint to a list.
159322*/
159323static void fts3PutDeltaVarint(
159324 char **pp, /* IN/OUT: Output pointer */
159325 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
159326 sqlite3_int64 iVal /* Write this value to the list */
159327){
159328 assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
159329 *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
159330 *piPrev = iVal;
159331}
159332
159333/*
159334** When this function is called, *ppPoslist is assumed to point to the
159335** start of a position-list. After it returns, *ppPoslist points to the
159336** first byte after the position-list.
159337**
159338** A position list is list of positions (delta encoded) and columns for
159339** a single document record of a doclist. So, in other words, this
159340** routine advances *ppPoslist so that it points to the next docid in
159341** the doclist, or to the first byte past the end of the doclist.
159342**
159343** If pp is not NULL, then the contents of the position list are copied
159344** to *pp. *pp is set to point to the first byte past the last byte copied
159345** before this function returns.
159346*/
159347static void fts3PoslistCopy(char **pp, char **ppPoslist){
159348 char *pEnd = *ppPoslist;
159349 char c = 0;
159350
159351 /* The end of a position list is marked by a zero encoded as an FTS3
159352 ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
159353 ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
159354 ** of some other, multi-byte, value.
159355 **
159356 ** The following while-loop moves pEnd to point to the first byte that is not
159357 ** immediately preceded by a byte with the 0x80 bit set. Then increments
159358 ** pEnd once more so that it points to the byte immediately following the
159359 ** last byte in the position-list.
159360 */
159361 while( *pEnd | c ){
159362 c = *pEnd++ & 0x80;
159363 testcase( c!=0 && (*pEnd)==0 );
159364 }
159365 pEnd++; /* Advance past the POS_END terminator byte */
159366
159367 if( pp ){
159368 int n = (int)(pEnd - *ppPoslist);
159369 char *p = *pp;
159370 memcpy(p, *ppPoslist, n);
159371 p += n;
159372 *pp = p;
159373 }
159374 *ppPoslist = pEnd;
159375}
159376
159377/*
159378** When this function is called, *ppPoslist is assumed to point to the
159379** start of a column-list. After it returns, *ppPoslist points to the
159380** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
159381**
159382** A column-list is list of delta-encoded positions for a single column
159383** within a single document within a doclist.
159384**
159385** The column-list is terminated either by a POS_COLUMN varint (1) or
159386** a POS_END varint (0). This routine leaves *ppPoslist pointing to
159387** the POS_COLUMN or POS_END that terminates the column-list.
159388**
159389** If pp is not NULL, then the contents of the column-list are copied
159390** to *pp. *pp is set to point to the first byte past the last byte copied
159391** before this function returns. The POS_COLUMN or POS_END terminator
159392** is not copied into *pp.
159393*/
159394static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
159395 char *pEnd = *ppPoslist;
159396 char c = 0;
159397
159398 /* A column-list is terminated by either a 0x01 or 0x00 byte that is
159399 ** not part of a multi-byte varint.
159400 */
159401 while( 0xFE & (*pEnd | c) ){
159402 c = *pEnd++ & 0x80;
159403 testcase( c!=0 && ((*pEnd)&0xfe)==0 );
159404 }
159405 if( pp ){
159406 int n = (int)(pEnd - *ppPoslist);
159407 char *p = *pp;
159408 memcpy(p, *ppPoslist, n);
159409 p += n;
159410 *pp = p;
159411 }
159412 *ppPoslist = pEnd;
159413}
159414
159415/*
159416** Value used to signify the end of an position-list. This is safe because
159417** it is not possible to have a document with 2^31 terms.
159418*/
159419#define POSITION_LIST_END 0x7fffffff
159420
159421/*
159422** This function is used to help parse position-lists. When this function is
159423** called, *pp may point to the start of the next varint in the position-list
159424** being parsed, or it may point to 1 byte past the end of the position-list
159425** (in which case **pp will be a terminator bytes POS_END (0) or
159426** (1)).
159427**
159428** If *pp points past the end of the current position-list, set *pi to
159429** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
159430** increment the current value of *pi by the value read, and set *pp to
159431** point to the next value before returning.
159432**
159433** Before calling this routine *pi must be initialized to the value of
159434** the previous position, or zero if we are reading the first position
159435** in the position-list. Because positions are delta-encoded, the value
159436** of the previous position is needed in order to compute the value of
159437** the next position.
159438*/
159439static void fts3ReadNextPos(
159440 char **pp, /* IN/OUT: Pointer into position-list buffer */
159441 sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
159442){
159443 if( (**pp)&0xFE ){
159444 fts3GetDeltaVarint(pp, pi);
159445 *pi -= 2;
159446 }else{
159447 *pi = POSITION_LIST_END;
159448 }
159449}
159450
159451/*
159452** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
159453** the value of iCol encoded as a varint to *pp. This will start a new
159454** column list.
159455**
159456** Set *pp to point to the byte just after the last byte written before
159457** returning (do not modify it if iCol==0). Return the total number of bytes
159458** written (0 if iCol==0).
159459*/
159460static int fts3PutColNumber(char **pp, int iCol){
159461 int n = 0; /* Number of bytes written */
159462 if( iCol ){
159463 char *p = *pp; /* Output pointer */
159464 n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
159465 *p = 0x01;
159466 *pp = &p[n];
159467 }
159468 return n;
159469}
159470
159471/*
159472** Compute the union of two position lists. The output written
159473** into *pp contains all positions of both *pp1 and *pp2 in sorted
159474** order and with any duplicates removed. All pointers are
159475** updated appropriately. The caller is responsible for insuring
159476** that there is enough space in *pp to hold the complete output.
159477*/
159478static void fts3PoslistMerge(
159479 char **pp, /* Output buffer */
159480 char **pp1, /* Left input list */
159481 char **pp2 /* Right input list */
159482){
159483 char *p = *pp;
159484 char *p1 = *pp1;
159485 char *p2 = *pp2;
159486
159487 while( *p1 || *p2 ){
159488 int iCol1; /* The current column index in pp1 */
159489 int iCol2; /* The current column index in pp2 */
159490
159491 if( *p1==POS_COLUMN ) fts3GetVarint32(&p1[1], &iCol1);
159492 else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
159493 else iCol1 = 0;
159494
159495 if( *p2==POS_COLUMN ) fts3GetVarint32(&p2[1], &iCol2);
159496 else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
159497 else iCol2 = 0;
159498
159499 if( iCol1==iCol2 ){
159500 sqlite3_int64 i1 = 0; /* Last position from pp1 */
159501 sqlite3_int64 i2 = 0; /* Last position from pp2 */
159502 sqlite3_int64 iPrev = 0;
159503 int n = fts3PutColNumber(&p, iCol1);
159504 p1 += n;
159505 p2 += n;
159506
159507 /* At this point, both p1 and p2 point to the start of column-lists
159508 ** for the same column (the column with index iCol1 and iCol2).
159509 ** A column-list is a list of non-negative delta-encoded varints, each
159510 ** incremented by 2 before being stored. Each list is terminated by a
159511 ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
159512 ** and writes the results to buffer p. p is left pointing to the byte
159513 ** after the list written. No terminator (POS_END or POS_COLUMN) is
159514 ** written to the output.
159515 */
159516 fts3GetDeltaVarint(&p1, &i1);
159517 fts3GetDeltaVarint(&p2, &i2);
159518 do {
159519 fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
159520 iPrev -= 2;
159521 if( i1==i2 ){
159522 fts3ReadNextPos(&p1, &i1);
159523 fts3ReadNextPos(&p2, &i2);
159524 }else if( i1<i2 ){
159525 fts3ReadNextPos(&p1, &i1);
159526 }else{
159527 fts3ReadNextPos(&p2, &i2);
159528 }
159529 }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
159530 }else if( iCol1<iCol2 ){
159531 p1 += fts3PutColNumber(&p, iCol1);
159532 fts3ColumnlistCopy(&p, &p1);
159533 }else{
159534 p2 += fts3PutColNumber(&p, iCol2);
159535 fts3ColumnlistCopy(&p, &p2);
159536 }
159537 }
159538
159539 *p++ = POS_END;
159540 *pp = p;
159541 *pp1 = p1 + 1;
159542 *pp2 = p2 + 1;
159543}
159544
159545/*
159546** This function is used to merge two position lists into one. When it is
159547** called, *pp1 and *pp2 must both point to position lists. A position-list is
159548** the part of a doclist that follows each document id. For example, if a row
159549** contains:
159550**
159551** 'a b c'|'x y z'|'a b b a'
159552**
159553** Then the position list for this row for token 'b' would consist of:
159554**
159555** 0x02 0x01 0x02 0x03 0x03 0x00
159556**
159557** When this function returns, both *pp1 and *pp2 are left pointing to the
159558** byte following the 0x00 terminator of their respective position lists.
159559**
159560** If isSaveLeft is 0, an entry is added to the output position list for
159561** each position in *pp2 for which there exists one or more positions in
159562** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
159563** when the *pp1 token appears before the *pp2 token, but not more than nToken
159564** slots before it.
159565**
159566** e.g. nToken==1 searches for adjacent positions.
159567*/
159568static int fts3PoslistPhraseMerge(
159569 char **pp, /* IN/OUT: Preallocated output buffer */
159570 int nToken, /* Maximum difference in token positions */
159571 int isSaveLeft, /* Save the left position */
159572 int isExact, /* If *pp1 is exactly nTokens before *pp2 */
159573 char **pp1, /* IN/OUT: Left input list */
159574 char **pp2 /* IN/OUT: Right input list */
159575){
159576 char *p = *pp;
159577 char *p1 = *pp1;
159578 char *p2 = *pp2;
159579 int iCol1 = 0;
159580 int iCol2 = 0;
159581
159582 /* Never set both isSaveLeft and isExact for the same invocation. */
159583 assert( isSaveLeft==0 || isExact==0 );
159584
159585 assert( p!=0 && *p1!=0 && *p2!=0 );
159586 if( *p1==POS_COLUMN ){
159587 p1++;
159588 p1 += fts3GetVarint32(p1, &iCol1);
159589 }
159590 if( *p2==POS_COLUMN ){
159591 p2++;
159592 p2 += fts3GetVarint32(p2, &iCol2);
159593 }
159594
159595 while( 1 ){
159596 if( iCol1==iCol2 ){
159597 char *pSave = p;
159598 sqlite3_int64 iPrev = 0;
159599 sqlite3_int64 iPos1 = 0;
159600 sqlite3_int64 iPos2 = 0;
159601
159602 if( iCol1 ){
159603 *p++ = POS_COLUMN;
159604 p += sqlite3Fts3PutVarint(p, iCol1);
159605 }
159606
159607 assert( *p1!=POS_END && *p1!=POS_COLUMN );
159608 assert( *p2!=POS_END && *p2!=POS_COLUMN );
159609 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
159610 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
159611
159612 while( 1 ){
159613 if( iPos2==iPos1+nToken
159614 || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
159615 ){
159616 sqlite3_int64 iSave;
159617 iSave = isSaveLeft ? iPos1 : iPos2;
159618 fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
159619 pSave = 0;
159620 assert( p );
159621 }
159622 if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
159623 if( (*p2&0xFE)==0 ) break;
159624 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
159625 }else{
159626 if( (*p1&0xFE)==0 ) break;
159627 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
159628 }
159629 }
159630
159631 if( pSave ){
159632 assert( pp && p );
159633 p = pSave;
159634 }
159635
159636 fts3ColumnlistCopy(0, &p1);
159637 fts3ColumnlistCopy(0, &p2);
159638 assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
159639 if( 0==*p1 || 0==*p2 ) break;
159640
159641 p1++;
159642 p1 += fts3GetVarint32(p1, &iCol1);
159643 p2++;
159644 p2 += fts3GetVarint32(p2, &iCol2);
159645 }
159646
159647 /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
159648 ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
159649 ** end of the position list, or the 0x01 that precedes the next
159650 ** column-number in the position list.
159651 */
159652 else if( iCol1<iCol2 ){
159653 fts3ColumnlistCopy(0, &p1);
159654 if( 0==*p1 ) break;
159655 p1++;
159656 p1 += fts3GetVarint32(p1, &iCol1);
159657 }else{
159658 fts3ColumnlistCopy(0, &p2);
159659 if( 0==*p2 ) break;
159660 p2++;
159661 p2 += fts3GetVarint32(p2, &iCol2);
159662 }
159663 }
159664
159665 fts3PoslistCopy(0, &p2);
159666 fts3PoslistCopy(0, &p1);
159667 *pp1 = p1;
159668 *pp2 = p2;
159669 if( *pp==p ){
159670 return 0;
159671 }
159672 *p++ = 0x00;
159673 *pp = p;
159674 return 1;
159675}
159676
159677/*
159678** Merge two position-lists as required by the NEAR operator. The argument
159679** position lists correspond to the left and right phrases of an expression
159680** like:
159681**
159682** "phrase 1" NEAR "phrase number 2"
159683**
159684** Position list *pp1 corresponds to the left-hand side of the NEAR
159685** expression and *pp2 to the right. As usual, the indexes in the position
159686** lists are the offsets of the last token in each phrase (tokens "1" and "2"
159687** in the example above).
159688**
159689** The output position list - written to *pp - is a copy of *pp2 with those
159690** entries that are not sufficiently NEAR entries in *pp1 removed.
159691*/
159692static int fts3PoslistNearMerge(
159693 char **pp, /* Output buffer */
159694 char *aTmp, /* Temporary buffer space */
159695 int nRight, /* Maximum difference in token positions */
159696 int nLeft, /* Maximum difference in token positions */
159697 char **pp1, /* IN/OUT: Left input list */
159698 char **pp2 /* IN/OUT: Right input list */
159699){
159700 char *p1 = *pp1;
159701 char *p2 = *pp2;
159702
159703 char *pTmp1 = aTmp;
159704 char *pTmp2;
159705 char *aTmp2;
159706 int res = 1;
159707
159708 fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
159709 aTmp2 = pTmp2 = pTmp1;
159710 *pp1 = p1;
159711 *pp2 = p2;
159712 fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
159713 if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
159714 fts3PoslistMerge(pp, &aTmp, &aTmp2);
159715 }else if( pTmp1!=aTmp ){
159716 fts3PoslistCopy(pp, &aTmp);
159717 }else if( pTmp2!=aTmp2 ){
159718 fts3PoslistCopy(pp, &aTmp2);
159719 }else{
159720 res = 0;
159721 }
159722
159723 return res;
159724}
159725
159726/*
159727** An instance of this function is used to merge together the (potentially
159728** large number of) doclists for each term that matches a prefix query.
159729** See function fts3TermSelectMerge() for details.
159730*/
159731typedef struct TermSelect TermSelect;
159732struct TermSelect {
159733 char *aaOutput[16]; /* Malloc'd output buffers */
159734 int anOutput[16]; /* Size each output buffer in bytes */
159735};
159736
159737/*
159738** This function is used to read a single varint from a buffer. Parameter
159739** pEnd points 1 byte past the end of the buffer. When this function is
159740** called, if *pp points to pEnd or greater, then the end of the buffer
159741** has been reached. In this case *pp is set to 0 and the function returns.
159742**
159743** If *pp does not point to or past pEnd, then a single varint is read
159744** from *pp. *pp is then set to point 1 byte past the end of the read varint.
159745**
159746** If bDescIdx is false, the value read is added to *pVal before returning.
159747** If it is true, the value read is subtracted from *pVal before this
159748** function returns.
159749*/
159750static void fts3GetDeltaVarint3(
159751 char **pp, /* IN/OUT: Point to read varint from */
159752 char *pEnd, /* End of buffer */
159753 int bDescIdx, /* True if docids are descending */
159754 sqlite3_int64 *pVal /* IN/OUT: Integer value */
159755){
159756 if( *pp>=pEnd ){
159757 *pp = 0;
159758 }else{
159759 sqlite3_int64 iVal;
159760 *pp += sqlite3Fts3GetVarint(*pp, &iVal);
159761 if( bDescIdx ){
159762 *pVal -= iVal;
159763 }else{
159764 *pVal += iVal;
159765 }
159766 }
159767}
159768
159769/*
159770** This function is used to write a single varint to a buffer. The varint
159771** is written to *pp. Before returning, *pp is set to point 1 byte past the
159772** end of the value written.
159773**
159774** If *pbFirst is zero when this function is called, the value written to
159775** the buffer is that of parameter iVal.
159776**
159777** If *pbFirst is non-zero when this function is called, then the value
159778** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
159779** (if bDescIdx is non-zero).
159780**
159781** Before returning, this function always sets *pbFirst to 1 and *piPrev
159782** to the value of parameter iVal.
159783*/
159784static void fts3PutDeltaVarint3(
159785 char **pp, /* IN/OUT: Output pointer */
159786 int bDescIdx, /* True for descending docids */
159787 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
159788 int *pbFirst, /* IN/OUT: True after first int written */
159789 sqlite3_int64 iVal /* Write this value to the list */
159790){
159791 sqlite3_int64 iWrite;
159792 if( bDescIdx==0 || *pbFirst==0 ){
159793 iWrite = iVal - *piPrev;
159794 }else{
159795 iWrite = *piPrev - iVal;
159796 }
159797 assert( *pbFirst || *piPrev==0 );
159798 assert( *pbFirst==0 || iWrite>0 );
159799 *pp += sqlite3Fts3PutVarint(*pp, iWrite);
159800 *piPrev = iVal;
159801 *pbFirst = 1;
159802}
159803
159804
159805/*
159806** This macro is used by various functions that merge doclists. The two
159807** arguments are 64-bit docid values. If the value of the stack variable
159808** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
159809** Otherwise, (i2-i1).
159810**
159811** Using this makes it easier to write code that can merge doclists that are
159812** sorted in either ascending or descending order.
159813*/
159814#define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
159815
159816/*
159817** This function does an "OR" merge of two doclists (output contains all
159818** positions contained in either argument doclist). If the docids in the
159819** input doclists are sorted in ascending order, parameter bDescDoclist
159820** should be false. If they are sorted in ascending order, it should be
159821** passed a non-zero value.
159822**
159823** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
159824** containing the output doclist and SQLITE_OK is returned. In this case
159825** *pnOut is set to the number of bytes in the output doclist.
159826**
159827** If an error occurs, an SQLite error code is returned. The output values
159828** are undefined in this case.
159829*/
159830static int fts3DoclistOrMerge(
159831 int bDescDoclist, /* True if arguments are desc */
159832 char *a1, int n1, /* First doclist */
159833 char *a2, int n2, /* Second doclist */
159834 char **paOut, int *pnOut /* OUT: Malloc'd doclist */
159835){
159836 sqlite3_int64 i1 = 0;
159837 sqlite3_int64 i2 = 0;
159838 sqlite3_int64 iPrev = 0;
159839 char *pEnd1 = &a1[n1];
159840 char *pEnd2 = &a2[n2];
159841 char *p1 = a1;
159842 char *p2 = a2;
159843 char *p;
159844 char *aOut;
159845 int bFirstOut = 0;
159846
159847 *paOut = 0;
159848 *pnOut = 0;
159849
159850 /* Allocate space for the output. Both the input and output doclists
159851 ** are delta encoded. If they are in ascending order (bDescDoclist==0),
159852 ** then the first docid in each list is simply encoded as a varint. For
159853 ** each subsequent docid, the varint stored is the difference between the
159854 ** current and previous docid (a positive number - since the list is in
159855 ** ascending order).
159856 **
159857 ** The first docid written to the output is therefore encoded using the
159858 ** same number of bytes as it is in whichever of the input lists it is
159859 ** read from. And each subsequent docid read from the same input list
159860 ** consumes either the same or less bytes as it did in the input (since
159861 ** the difference between it and the previous value in the output must
159862 ** be a positive value less than or equal to the delta value read from
159863 ** the input list). The same argument applies to all but the first docid
159864 ** read from the 'other' list. And to the contents of all position lists
159865 ** that will be copied and merged from the input to the output.
159866 **
159867 ** However, if the first docid copied to the output is a negative number,
159868 ** then the encoding of the first docid from the 'other' input list may
159869 ** be larger in the output than it was in the input (since the delta value
159870 ** may be a larger positive integer than the actual docid).
159871 **
159872 ** The space required to store the output is therefore the sum of the
159873 ** sizes of the two inputs, plus enough space for exactly one of the input
159874 ** docids to grow.
159875 **
159876 ** A symetric argument may be made if the doclists are in descending
159877 ** order.
159878 */
159879 aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
159880 if( !aOut ) return SQLITE_NOMEM;
159881
159882 p = aOut;
159883 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
159884 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
159885 while( p1 || p2 ){
159886 sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
159887
159888 if( p2 && p1 && iDiff==0 ){
159889 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
159890 fts3PoslistMerge(&p, &p1, &p2);
159891 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
159892 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
159893 }else if( !p2 || (p1 && iDiff<0) ){
159894 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
159895 fts3PoslistCopy(&p, &p1);
159896 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
159897 }else{
159898 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
159899 fts3PoslistCopy(&p, &p2);
159900 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
159901 }
159902 }
159903
159904 *paOut = aOut;
159905 *pnOut = (int)(p-aOut);
159906 assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
159907 return SQLITE_OK;
159908}
159909
159910/*
159911** This function does a "phrase" merge of two doclists. In a phrase merge,
159912** the output contains a copy of each position from the right-hand input
159913** doclist for which there is a position in the left-hand input doclist
159914** exactly nDist tokens before it.
159915**
159916** If the docids in the input doclists are sorted in ascending order,
159917** parameter bDescDoclist should be false. If they are sorted in ascending
159918** order, it should be passed a non-zero value.
159919**
159920** The right-hand input doclist is overwritten by this function.
159921*/
159922static int fts3DoclistPhraseMerge(
159923 int bDescDoclist, /* True if arguments are desc */
159924 int nDist, /* Distance from left to right (1=adjacent) */
159925 char *aLeft, int nLeft, /* Left doclist */
159926 char **paRight, int *pnRight /* IN/OUT: Right/output doclist */
159927){
159928 sqlite3_int64 i1 = 0;
159929 sqlite3_int64 i2 = 0;
159930 sqlite3_int64 iPrev = 0;
159931 char *aRight = *paRight;
159932 char *pEnd1 = &aLeft[nLeft];
159933 char *pEnd2 = &aRight[*pnRight];
159934 char *p1 = aLeft;
159935 char *p2 = aRight;
159936 char *p;
159937 int bFirstOut = 0;
159938 char *aOut;
159939
159940 assert( nDist>0 );
159941 if( bDescDoclist ){
159942 aOut = sqlite3_malloc(*pnRight + FTS3_VARINT_MAX);
159943 if( aOut==0 ) return SQLITE_NOMEM;
159944 }else{
159945 aOut = aRight;
159946 }
159947 p = aOut;
159948
159949 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
159950 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
159951
159952 while( p1 && p2 ){
159953 sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
159954 if( iDiff==0 ){
159955 char *pSave = p;
159956 sqlite3_int64 iPrevSave = iPrev;
159957 int bFirstOutSave = bFirstOut;
159958
159959 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
159960 if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
159961 p = pSave;
159962 iPrev = iPrevSave;
159963 bFirstOut = bFirstOutSave;
159964 }
159965 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
159966 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
159967 }else if( iDiff<0 ){
159968 fts3PoslistCopy(0, &p1);
159969 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
159970 }else{
159971 fts3PoslistCopy(0, &p2);
159972 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
159973 }
159974 }
159975
159976 *pnRight = (int)(p - aOut);
159977 if( bDescDoclist ){
159978 sqlite3_free(aRight);
159979 *paRight = aOut;
159980 }
159981
159982 return SQLITE_OK;
159983}
159984
159985/*
159986** Argument pList points to a position list nList bytes in size. This
159987** function checks to see if the position list contains any entries for
159988** a token in position 0 (of any column). If so, it writes argument iDelta
159989** to the output buffer pOut, followed by a position list consisting only
159990** of the entries from pList at position 0, and terminated by an 0x00 byte.
159991** The value returned is the number of bytes written to pOut (if any).
159992*/
159993SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
159994 sqlite3_int64 iDelta, /* Varint that may be written to pOut */
159995 char *pList, /* Position list (no 0x00 term) */
159996 int nList, /* Size of pList in bytes */
159997 char *pOut /* Write output here */
159998){
159999 int nOut = 0;
160000 int bWritten = 0; /* True once iDelta has been written */
160001 char *p = pList;
160002 char *pEnd = &pList[nList];
160003
160004 if( *p!=0x01 ){
160005 if( *p==0x02 ){
160006 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
160007 pOut[nOut++] = 0x02;
160008 bWritten = 1;
160009 }
160010 fts3ColumnlistCopy(0, &p);
160011 }
160012
160013 while( p<pEnd ){
160014 sqlite3_int64 iCol;
160015 p++;
160016 p += sqlite3Fts3GetVarint(p, &iCol);
160017 if( *p==0x02 ){
160018 if( bWritten==0 ){
160019 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
160020 bWritten = 1;
160021 }
160022 pOut[nOut++] = 0x01;
160023 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
160024 pOut[nOut++] = 0x02;
160025 }
160026 fts3ColumnlistCopy(0, &p);
160027 }
160028 if( bWritten ){
160029 pOut[nOut++] = 0x00;
160030 }
160031
160032 return nOut;
160033}
160034
160035
160036/*
160037** Merge all doclists in the TermSelect.aaOutput[] array into a single
160038** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
160039** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
160040**
160041** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
160042** the responsibility of the caller to free any doclists left in the
160043** TermSelect.aaOutput[] array.
160044*/
160045static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
160046 char *aOut = 0;
160047 int nOut = 0;
160048 int i;
160049
160050 /* Loop through the doclists in the aaOutput[] array. Merge them all
160051 ** into a single doclist.
160052 */
160053 for(i=0; i<SizeofArray(pTS->aaOutput); i++){
160054 if( pTS->aaOutput[i] ){
160055 if( !aOut ){
160056 aOut = pTS->aaOutput[i];
160057 nOut = pTS->anOutput[i];
160058 pTS->aaOutput[i] = 0;
160059 }else{
160060 int nNew;
160061 char *aNew;
160062
160063 int rc = fts3DoclistOrMerge(p->bDescIdx,
160064 pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
160065 );
160066 if( rc!=SQLITE_OK ){
160067 sqlite3_free(aOut);
160068 return rc;
160069 }
160070
160071 sqlite3_free(pTS->aaOutput[i]);
160072 sqlite3_free(aOut);
160073 pTS->aaOutput[i] = 0;
160074 aOut = aNew;
160075 nOut = nNew;
160076 }
160077 }
160078 }
160079
160080 pTS->aaOutput[0] = aOut;
160081 pTS->anOutput[0] = nOut;
160082 return SQLITE_OK;
160083}
160084
160085/*
160086** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
160087** as the first argument. The merge is an "OR" merge (see function
160088** fts3DoclistOrMerge() for details).
160089**
160090** This function is called with the doclist for each term that matches
160091** a queried prefix. It merges all these doclists into one, the doclist
160092** for the specified prefix. Since there can be a very large number of
160093** doclists to merge, the merging is done pair-wise using the TermSelect
160094** object.
160095**
160096** This function returns SQLITE_OK if the merge is successful, or an
160097** SQLite error code (SQLITE_NOMEM) if an error occurs.
160098*/
160099static int fts3TermSelectMerge(
160100 Fts3Table *p, /* FTS table handle */
160101 TermSelect *pTS, /* TermSelect object to merge into */
160102 char *aDoclist, /* Pointer to doclist */
160103 int nDoclist /* Size of aDoclist in bytes */
160104){
160105 if( pTS->aaOutput[0]==0 ){
160106 /* If this is the first term selected, copy the doclist to the output
160107 ** buffer using memcpy().
160108 **
160109 ** Add FTS3_VARINT_MAX bytes of unused space to the end of the
160110 ** allocation. This is so as to ensure that the buffer is big enough
160111 ** to hold the current doclist AND'd with any other doclist. If the
160112 ** doclists are stored in order=ASC order, this padding would not be
160113 ** required (since the size of [doclistA AND doclistB] is always less
160114 ** than or equal to the size of [doclistA] in that case). But this is
160115 ** not true for order=DESC. For example, a doclist containing (1, -1)
160116 ** may be smaller than (-1), as in the first example the -1 may be stored
160117 ** as a single-byte delta, whereas in the second it must be stored as a
160118 ** FTS3_VARINT_MAX byte varint.
160119 **
160120 ** Similar padding is added in the fts3DoclistOrMerge() function.
160121 */
160122 pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
160123 pTS->anOutput[0] = nDoclist;
160124 if( pTS->aaOutput[0] ){
160125 memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
160126 }else{
160127 return SQLITE_NOMEM;
160128 }
160129 }else{
160130 char *aMerge = aDoclist;
160131 int nMerge = nDoclist;
160132 int iOut;
160133
160134 for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
160135 if( pTS->aaOutput[iOut]==0 ){
160136 assert( iOut>0 );
160137 pTS->aaOutput[iOut] = aMerge;
160138 pTS->anOutput[iOut] = nMerge;
160139 break;
160140 }else{
160141 char *aNew;
160142 int nNew;
160143
160144 int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
160145 pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
160146 );
160147 if( rc!=SQLITE_OK ){
160148 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
160149 return rc;
160150 }
160151
160152 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
160153 sqlite3_free(pTS->aaOutput[iOut]);
160154 pTS->aaOutput[iOut] = 0;
160155
160156 aMerge = aNew;
160157 nMerge = nNew;
160158 if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
160159 pTS->aaOutput[iOut] = aMerge;
160160 pTS->anOutput[iOut] = nMerge;
160161 }
160162 }
160163 }
160164 }
160165 return SQLITE_OK;
160166}
160167
160168/*
160169** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
160170*/
160171static int fts3SegReaderCursorAppend(
160172 Fts3MultiSegReader *pCsr,
160173 Fts3SegReader *pNew
160174){
160175 if( (pCsr->nSegment%16)==0 ){
160176 Fts3SegReader **apNew;
160177 int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
160178 apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
160179 if( !apNew ){
160180 sqlite3Fts3SegReaderFree(pNew);
160181 return SQLITE_NOMEM;
160182 }
160183 pCsr->apSegment = apNew;
160184 }
160185 pCsr->apSegment[pCsr->nSegment++] = pNew;
160186 return SQLITE_OK;
160187}
160188
160189/*
160190** Add seg-reader objects to the Fts3MultiSegReader object passed as the
160191** 8th argument.
160192**
160193** This function returns SQLITE_OK if successful, or an SQLite error code
160194** otherwise.
160195*/
160196static int fts3SegReaderCursor(
160197 Fts3Table *p, /* FTS3 table handle */
160198 int iLangid, /* Language id */
160199 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
160200 int iLevel, /* Level of segments to scan */
160201 const char *zTerm, /* Term to query for */
160202 int nTerm, /* Size of zTerm in bytes */
160203 int isPrefix, /* True for a prefix search */
160204 int isScan, /* True to scan from zTerm to EOF */
160205 Fts3MultiSegReader *pCsr /* Cursor object to populate */
160206){
160207 int rc = SQLITE_OK; /* Error code */
160208 sqlite3_stmt *pStmt = 0; /* Statement to iterate through segments */
160209 int rc2; /* Result of sqlite3_reset() */
160210
160211 /* If iLevel is less than 0 and this is not a scan, include a seg-reader
160212 ** for the pending-terms. If this is a scan, then this call must be being
160213 ** made by an fts4aux module, not an FTS table. In this case calling
160214 ** Fts3SegReaderPending might segfault, as the data structures used by
160215 ** fts4aux are not completely populated. So it's easiest to filter these
160216 ** calls out here. */
160217 if( iLevel<0 && p->aIndex ){
160218 Fts3SegReader *pSeg = 0;
160219 rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg);
160220 if( rc==SQLITE_OK && pSeg ){
160221 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
160222 }
160223 }
160224
160225 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
160226 if( rc==SQLITE_OK ){
160227 rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
160228 }
160229
160230 while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
160231 Fts3SegReader *pSeg = 0;
160232
160233 /* Read the values returned by the SELECT into local variables. */
160234 sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
160235 sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
160236 sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
160237 int nRoot = sqlite3_column_bytes(pStmt, 4);
160238 char const *zRoot = sqlite3_column_blob(pStmt, 4);
160239
160240 /* If zTerm is not NULL, and this segment is not stored entirely on its
160241 ** root node, the range of leaves scanned can be reduced. Do this. */
160242 if( iStartBlock && zTerm ){
160243 sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
160244 rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
160245 if( rc!=SQLITE_OK ) goto finished;
160246 if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
160247 }
160248
160249 rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
160250 (isPrefix==0 && isScan==0),
160251 iStartBlock, iLeavesEndBlock,
160252 iEndBlock, zRoot, nRoot, &pSeg
160253 );
160254 if( rc!=SQLITE_OK ) goto finished;
160255 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
160256 }
160257 }
160258
160259 finished:
160260 rc2 = sqlite3_reset(pStmt);
160261 if( rc==SQLITE_DONE ) rc = rc2;
160262
160263 return rc;
160264}
160265
160266/*
160267** Set up a cursor object for iterating through a full-text index or a
160268** single level therein.
160269*/
160270SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
160271 Fts3Table *p, /* FTS3 table handle */
160272 int iLangid, /* Language-id to search */
160273 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
160274 int iLevel, /* Level of segments to scan */
160275 const char *zTerm, /* Term to query for */
160276 int nTerm, /* Size of zTerm in bytes */
160277 int isPrefix, /* True for a prefix search */
160278 int isScan, /* True to scan from zTerm to EOF */
160279 Fts3MultiSegReader *pCsr /* Cursor object to populate */
160280){
160281 assert( iIndex>=0 && iIndex<p->nIndex );
160282 assert( iLevel==FTS3_SEGCURSOR_ALL
160283 || iLevel==FTS3_SEGCURSOR_PENDING
160284 || iLevel>=0
160285 );
160286 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
160287 assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
160288 assert( isPrefix==0 || isScan==0 );
160289
160290 memset(pCsr, 0, sizeof(Fts3MultiSegReader));
160291 return fts3SegReaderCursor(
160292 p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
160293 );
160294}
160295
160296/*
160297** In addition to its current configuration, have the Fts3MultiSegReader
160298** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
160299**
160300** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
160301*/
160302static int fts3SegReaderCursorAddZero(
160303 Fts3Table *p, /* FTS virtual table handle */
160304 int iLangid,
160305 const char *zTerm, /* Term to scan doclist of */
160306 int nTerm, /* Number of bytes in zTerm */
160307 Fts3MultiSegReader *pCsr /* Fts3MultiSegReader to modify */
160308){
160309 return fts3SegReaderCursor(p,
160310 iLangid, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr
160311 );
160312}
160313
160314/*
160315** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
160316** if isPrefix is true, to scan the doclist for all terms for which
160317** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
160318** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
160319** an SQLite error code.
160320**
160321** It is the responsibility of the caller to free this object by eventually
160322** passing it to fts3SegReaderCursorFree()
160323**
160324** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
160325** Output parameter *ppSegcsr is set to 0 if an error occurs.
160326*/
160327static int fts3TermSegReaderCursor(
160328 Fts3Cursor *pCsr, /* Virtual table cursor handle */
160329 const char *zTerm, /* Term to query for */
160330 int nTerm, /* Size of zTerm in bytes */
160331 int isPrefix, /* True for a prefix search */
160332 Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
160333){
160334 Fts3MultiSegReader *pSegcsr; /* Object to allocate and return */
160335 int rc = SQLITE_NOMEM; /* Return code */
160336
160337 pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
160338 if( pSegcsr ){
160339 int i;
160340 int bFound = 0; /* True once an index has been found */
160341 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
160342
160343 if( isPrefix ){
160344 for(i=1; bFound==0 && i<p->nIndex; i++){
160345 if( p->aIndex[i].nPrefix==nTerm ){
160346 bFound = 1;
160347 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
160348 i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr
160349 );
160350 pSegcsr->bLookup = 1;
160351 }
160352 }
160353
160354 for(i=1; bFound==0 && i<p->nIndex; i++){
160355 if( p->aIndex[i].nPrefix==nTerm+1 ){
160356 bFound = 1;
160357 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
160358 i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
160359 );
160360 if( rc==SQLITE_OK ){
160361 rc = fts3SegReaderCursorAddZero(
160362 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
160363 );
160364 }
160365 }
160366 }
160367 }
160368
160369 if( bFound==0 ){
160370 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
160371 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
160372 );
160373 pSegcsr->bLookup = !isPrefix;
160374 }
160375 }
160376
160377 *ppSegcsr = pSegcsr;
160378 return rc;
160379}
160380
160381/*
160382** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
160383*/
160384static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
160385 sqlite3Fts3SegReaderFinish(pSegcsr);
160386 sqlite3_free(pSegcsr);
160387}
160388
160389/*
160390** This function retrieves the doclist for the specified term (or term
160391** prefix) from the database.
160392*/
160393static int fts3TermSelect(
160394 Fts3Table *p, /* Virtual table handle */
160395 Fts3PhraseToken *pTok, /* Token to query for */
160396 int iColumn, /* Column to query (or -ve for all columns) */
160397 int *pnOut, /* OUT: Size of buffer at *ppOut */
160398 char **ppOut /* OUT: Malloced result buffer */
160399){
160400 int rc; /* Return code */
160401 Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
160402 TermSelect tsc; /* Object for pair-wise doclist merging */
160403 Fts3SegFilter filter; /* Segment term filter configuration */
160404
160405 pSegcsr = pTok->pSegcsr;
160406 memset(&tsc, 0, sizeof(TermSelect));
160407
160408 filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
160409 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
160410 | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
160411 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
160412 filter.iCol = iColumn;
160413 filter.zTerm = pTok->z;
160414 filter.nTerm = pTok->n;
160415
160416 rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
160417 while( SQLITE_OK==rc
160418 && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
160419 ){
160420 rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
160421 }
160422
160423 if( rc==SQLITE_OK ){
160424 rc = fts3TermSelectFinishMerge(p, &tsc);
160425 }
160426 if( rc==SQLITE_OK ){
160427 *ppOut = tsc.aaOutput[0];
160428 *pnOut = tsc.anOutput[0];
160429 }else{
160430 int i;
160431 for(i=0; i<SizeofArray(tsc.aaOutput); i++){
160432 sqlite3_free(tsc.aaOutput[i]);
160433 }
160434 }
160435
160436 fts3SegReaderCursorFree(pSegcsr);
160437 pTok->pSegcsr = 0;
160438 return rc;
160439}
160440
160441/*
160442** This function counts the total number of docids in the doclist stored
160443** in buffer aList[], size nList bytes.
160444**
160445** If the isPoslist argument is true, then it is assumed that the doclist
160446** contains a position-list following each docid. Otherwise, it is assumed
160447** that the doclist is simply a list of docids stored as delta encoded
160448** varints.
160449*/
160450static int fts3DoclistCountDocids(char *aList, int nList){
160451 int nDoc = 0; /* Return value */
160452 if( aList ){
160453 char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */
160454 char *p = aList; /* Cursor */
160455 while( p<aEnd ){
160456 nDoc++;
160457 while( (*p++)&0x80 ); /* Skip docid varint */
160458 fts3PoslistCopy(0, &p); /* Skip over position list */
160459 }
160460 }
160461
160462 return nDoc;
160463}
160464
160465/*
160466** Advance the cursor to the next row in the %_content table that
160467** matches the search criteria. For a MATCH search, this will be
160468** the next row that matches. For a full-table scan, this will be
160469** simply the next row in the %_content table. For a docid lookup,
160470** this routine simply sets the EOF flag.
160471**
160472** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
160473** even if we reach end-of-file. The fts3EofMethod() will be called
160474** subsequently to determine whether or not an EOF was hit.
160475*/
160476static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
160477 int rc;
160478 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
160479 if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
160480 if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
160481 pCsr->isEof = 1;
160482 rc = sqlite3_reset(pCsr->pStmt);
160483 }else{
160484 pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
160485 rc = SQLITE_OK;
160486 }
160487 }else{
160488 rc = fts3EvalNext((Fts3Cursor *)pCursor);
160489 }
160490 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
160491 return rc;
160492}
160493
160494/*
160495** The following are copied from sqliteInt.h.
160496**
160497** Constants for the largest and smallest possible 64-bit signed integers.
160498** These macros are designed to work correctly on both 32-bit and 64-bit
160499** compilers.
160500*/
160501#ifndef SQLITE_AMALGAMATION
160502# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
160503# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
160504#endif
160505
160506/*
160507** If the numeric type of argument pVal is "integer", then return it
160508** converted to a 64-bit signed integer. Otherwise, return a copy of
160509** the second parameter, iDefault.
160510*/
160511static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
160512 if( pVal ){
160513 int eType = sqlite3_value_numeric_type(pVal);
160514 if( eType==SQLITE_INTEGER ){
160515 return sqlite3_value_int64(pVal);
160516 }
160517 }
160518 return iDefault;
160519}
160520
160521/*
160522** This is the xFilter interface for the virtual table. See
160523** the virtual table xFilter method documentation for additional
160524** information.
160525**
160526** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
160527** the %_content table.
160528**
160529** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
160530** in the %_content table.
160531**
160532** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The
160533** column on the left-hand side of the MATCH operator is column
160534** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
160535** side of the MATCH operator.
160536*/
160537static int fts3FilterMethod(
160538 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
160539 int idxNum, /* Strategy index */
160540 const char *idxStr, /* Unused */
160541 int nVal, /* Number of elements in apVal */
160542 sqlite3_value **apVal /* Arguments for the indexing scheme */
160543){
160544 int rc = SQLITE_OK;
160545 char *zSql; /* SQL statement used to access %_content */
160546 int eSearch;
160547 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
160548 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
160549
160550 sqlite3_value *pCons = 0; /* The MATCH or rowid constraint, if any */
160551 sqlite3_value *pLangid = 0; /* The "langid = ?" constraint, if any */
160552 sqlite3_value *pDocidGe = 0; /* The "docid >= ?" constraint, if any */
160553 sqlite3_value *pDocidLe = 0; /* The "docid <= ?" constraint, if any */
160554 int iIdx;
160555
160556 UNUSED_PARAMETER(idxStr);
160557 UNUSED_PARAMETER(nVal);
160558
160559 eSearch = (idxNum & 0x0000FFFF);
160560 assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
160561 assert( p->pSegments==0 );
160562
160563 /* Collect arguments into local variables */
160564 iIdx = 0;
160565 if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
160566 if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
160567 if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
160568 if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
160569 assert( iIdx==nVal );
160570
160571 /* In case the cursor has been used before, clear it now. */
160572 fts3ClearCursor(pCsr);
160573
160574 /* Set the lower and upper bounds on docids to return */
160575 pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
160576 pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64);
160577
160578 if( idxStr ){
160579 pCsr->bDesc = (idxStr[0]=='D');
160580 }else{
160581 pCsr->bDesc = p->bDescIdx;
160582 }
160583 pCsr->eSearch = (i16)eSearch;
160584
160585 if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){
160586 int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
160587 const char *zQuery = (const char *)sqlite3_value_text(pCons);
160588
160589 if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL ){
160590 return SQLITE_NOMEM;
160591 }
160592
160593 pCsr->iLangid = 0;
160594 if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
160595
160596 assert( p->base.zErrMsg==0 );
160597 rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
160598 p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
160599 &p->base.zErrMsg
160600 );
160601 if( rc!=SQLITE_OK ){
160602 return rc;
160603 }
160604
160605 rc = fts3EvalStart(pCsr);
160606 sqlite3Fts3SegmentsClose(p);
160607 if( rc!=SQLITE_OK ) return rc;
160608 pCsr->pNextId = pCsr->aDoclist;
160609 pCsr->iPrevId = 0;
160610 }
160611
160612 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
160613 ** statement loops through all rows of the %_content table. For a
160614 ** full-text query or docid lookup, the statement retrieves a single
160615 ** row by docid.
160616 */
160617 if( eSearch==FTS3_FULLSCAN_SEARCH ){
160618 if( pDocidGe || pDocidLe ){
160619 zSql = sqlite3_mprintf(
160620 "SELECT %s WHERE rowid BETWEEN %lld AND %lld ORDER BY rowid %s",
160621 p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
160622 (pCsr->bDesc ? "DESC" : "ASC")
160623 );
160624 }else{
160625 zSql = sqlite3_mprintf("SELECT %s ORDER BY rowid %s",
160626 p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
160627 );
160628 }
160629 if( zSql ){
160630 rc = sqlite3_prepare_v3(p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0);
160631 sqlite3_free(zSql);
160632 }else{
160633 rc = SQLITE_NOMEM;
160634 }
160635 }else if( eSearch==FTS3_DOCID_SEARCH ){
160636 rc = fts3CursorSeekStmt(pCsr);
160637 if( rc==SQLITE_OK ){
160638 rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
160639 }
160640 }
160641 if( rc!=SQLITE_OK ) return rc;
160642
160643 return fts3NextMethod(pCursor);
160644}
160645
160646/*
160647** This is the xEof method of the virtual table. SQLite calls this
160648** routine to find out if it has reached the end of a result set.
160649*/
160650static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
160651 Fts3Cursor *pCsr = (Fts3Cursor*)pCursor;
160652 if( pCsr->isEof ){
160653 fts3ClearCursor(pCsr);
160654 pCsr->isEof = 1;
160655 }
160656 return pCsr->isEof;
160657}
160658
160659/*
160660** This is the xRowid method. The SQLite core calls this routine to
160661** retrieve the rowid for the current row of the result set. fts3
160662** exposes %_content.docid as the rowid for the virtual table. The
160663** rowid should be written to *pRowid.
160664*/
160665static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
160666 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
160667 *pRowid = pCsr->iPrevId;
160668 return SQLITE_OK;
160669}
160670
160671/*
160672** This is the xColumn method, called by SQLite to request a value from
160673** the row that the supplied cursor currently points to.
160674**
160675** If:
160676**
160677** (iCol < p->nColumn) -> The value of the iCol'th user column.
160678** (iCol == p->nColumn) -> Magic column with the same name as the table.
160679** (iCol == p->nColumn+1) -> Docid column
160680** (iCol == p->nColumn+2) -> Langid column
160681*/
160682static int fts3ColumnMethod(
160683 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
160684 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
160685 int iCol /* Index of column to read value from */
160686){
160687 int rc = SQLITE_OK; /* Return Code */
160688 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
160689 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
160690
160691 /* The column value supplied by SQLite must be in range. */
160692 assert( iCol>=0 && iCol<=p->nColumn+2 );
160693
160694 switch( iCol-p->nColumn ){
160695 case 0:
160696 /* The special 'table-name' column */
160697 sqlite3_result_pointer(pCtx, pCsr, "fts3cursor", 0);
160698 break;
160699
160700 case 1:
160701 /* The docid column */
160702 sqlite3_result_int64(pCtx, pCsr->iPrevId);
160703 break;
160704
160705 case 2:
160706 if( pCsr->pExpr ){
160707 sqlite3_result_int64(pCtx, pCsr->iLangid);
160708 break;
160709 }else if( p->zLanguageid==0 ){
160710 sqlite3_result_int(pCtx, 0);
160711 break;
160712 }else{
160713 iCol = p->nColumn;
160714 /* fall-through */
160715 }
160716
160717 default:
160718 /* A user column. Or, if this is a full-table scan, possibly the
160719 ** language-id column. Seek the cursor. */
160720 rc = fts3CursorSeek(0, pCsr);
160721 if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)-1>iCol ){
160722 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
160723 }
160724 break;
160725 }
160726
160727 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
160728 return rc;
160729}
160730
160731/*
160732** This function is the implementation of the xUpdate callback used by
160733** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
160734** inserted, updated or deleted.
160735*/
160736static int fts3UpdateMethod(
160737 sqlite3_vtab *pVtab, /* Virtual table handle */
160738 int nArg, /* Size of argument array */
160739 sqlite3_value **apVal, /* Array of arguments */
160740 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
160741){
160742 return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
160743}
160744
160745/*
160746** Implementation of xSync() method. Flush the contents of the pending-terms
160747** hash-table to the database.
160748*/
160749static int fts3SyncMethod(sqlite3_vtab *pVtab){
160750
160751 /* Following an incremental-merge operation, assuming that the input
160752 ** segments are not completely consumed (the usual case), they are updated
160753 ** in place to remove the entries that have already been merged. This
160754 ** involves updating the leaf block that contains the smallest unmerged
160755 ** entry and each block (if any) between the leaf and the root node. So
160756 ** if the height of the input segment b-trees is N, and input segments
160757 ** are merged eight at a time, updating the input segments at the end
160758 ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
160759 ** small - often between 0 and 2. So the overhead of the incremental
160760 ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
160761 ** dwarfing the actual productive work accomplished, the incremental merge
160762 ** is only attempted if it will write at least 64 leaf blocks. Hence
160763 ** nMinMerge.
160764 **
160765 ** Of course, updating the input segments also involves deleting a bunch
160766 ** of blocks from the segments table. But this is not considered overhead
160767 ** as it would also be required by a crisis-merge that used the same input
160768 ** segments.
160769 */
160770 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
160771
160772 Fts3Table *p = (Fts3Table*)pVtab;
160773 int rc;
160774 i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
160775
160776 rc = sqlite3Fts3PendingTermsFlush(p);
160777 if( rc==SQLITE_OK
160778 && p->nLeafAdd>(nMinMerge/16)
160779 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
160780 ){
160781 int mxLevel = 0; /* Maximum relative level value in db */
160782 int A; /* Incr-merge parameter A */
160783
160784 rc = sqlite3Fts3MaxLevel(p, &mxLevel);
160785 assert( rc==SQLITE_OK || mxLevel==0 );
160786 A = p->nLeafAdd * mxLevel;
160787 A += (A/2);
160788 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
160789 }
160790 sqlite3Fts3SegmentsClose(p);
160791 sqlite3_set_last_insert_rowid(p->db, iLastRowid);
160792 return rc;
160793}
160794
160795/*
160796** If it is currently unknown whether or not the FTS table has an %_stat
160797** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
160798** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
160799** if an error occurs.
160800*/
160801static int fts3SetHasStat(Fts3Table *p){
160802 int rc = SQLITE_OK;
160803 if( p->bHasStat==2 ){
160804 char *zTbl = sqlite3_mprintf("%s_stat", p->zName);
160805 if( zTbl ){
160806 int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0);
160807 sqlite3_free(zTbl);
160808 p->bHasStat = (res==SQLITE_OK);
160809 }else{
160810 rc = SQLITE_NOMEM;
160811 }
160812 }
160813 return rc;
160814}
160815
160816/*
160817** Implementation of xBegin() method.
160818*/
160819static int fts3BeginMethod(sqlite3_vtab *pVtab){
160820 Fts3Table *p = (Fts3Table*)pVtab;
160821 UNUSED_PARAMETER(pVtab);
160822 assert( p->pSegments==0 );
160823 assert( p->nPendingData==0 );
160824 assert( p->inTransaction!=1 );
160825 TESTONLY( p->inTransaction = 1 );
160826 TESTONLY( p->mxSavepoint = -1; );
160827 p->nLeafAdd = 0;
160828 return fts3SetHasStat(p);
160829}
160830
160831/*
160832** Implementation of xCommit() method. This is a no-op. The contents of
160833** the pending-terms hash-table have already been flushed into the database
160834** by fts3SyncMethod().
160835*/
160836static int fts3CommitMethod(sqlite3_vtab *pVtab){
160837 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
160838 UNUSED_PARAMETER(pVtab);
160839 assert( p->nPendingData==0 );
160840 assert( p->inTransaction!=0 );
160841 assert( p->pSegments==0 );
160842 TESTONLY( p->inTransaction = 0 );
160843 TESTONLY( p->mxSavepoint = -1; );
160844 return SQLITE_OK;
160845}
160846
160847/*
160848** Implementation of xRollback(). Discard the contents of the pending-terms
160849** hash-table. Any changes made to the database are reverted by SQLite.
160850*/
160851static int fts3RollbackMethod(sqlite3_vtab *pVtab){
160852 Fts3Table *p = (Fts3Table*)pVtab;
160853 sqlite3Fts3PendingTermsClear(p);
160854 assert( p->inTransaction!=0 );
160855 TESTONLY( p->inTransaction = 0 );
160856 TESTONLY( p->mxSavepoint = -1; );
160857 return SQLITE_OK;
160858}
160859
160860/*
160861** When called, *ppPoslist must point to the byte immediately following the
160862** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
160863** moves *ppPoslist so that it instead points to the first byte of the
160864** same position list.
160865*/
160866static void fts3ReversePoslist(char *pStart, char **ppPoslist){
160867 char *p = &(*ppPoslist)[-2];
160868 char c = 0;
160869
160870 /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */
160871 while( p>pStart && (c=*p--)==0 );
160872
160873 /* Search backwards for a varint with value zero (the end of the previous
160874 ** poslist). This is an 0x00 byte preceded by some byte that does not
160875 ** have the 0x80 bit set. */
160876 while( p>pStart && (*p & 0x80) | c ){
160877 c = *p--;
160878 }
160879 assert( p==pStart || c==0 );
160880
160881 /* At this point p points to that preceding byte without the 0x80 bit
160882 ** set. So to find the start of the poslist, skip forward 2 bytes then
160883 ** over a varint.
160884 **
160885 ** Normally. The other case is that p==pStart and the poslist to return
160886 ** is the first in the doclist. In this case do not skip forward 2 bytes.
160887 ** The second part of the if condition (c==0 && *ppPoslist>&p[2])
160888 ** is required for cases where the first byte of a doclist and the
160889 ** doclist is empty. For example, if the first docid is 10, a doclist
160890 ** that begins with:
160891 **
160892 ** 0x0A 0x00 <next docid delta varint>
160893 */
160894 if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; }
160895 while( *p++&0x80 );
160896 *ppPoslist = p;
160897}
160898
160899/*
160900** Helper function used by the implementation of the overloaded snippet(),
160901** offsets() and optimize() SQL functions.
160902**
160903** If the value passed as the third argument is a blob of size
160904** sizeof(Fts3Cursor*), then the blob contents are copied to the
160905** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
160906** message is written to context pContext and SQLITE_ERROR returned. The
160907** string passed via zFunc is used as part of the error message.
160908*/
160909static int fts3FunctionArg(
160910 sqlite3_context *pContext, /* SQL function call context */
160911 const char *zFunc, /* Function name */
160912 sqlite3_value *pVal, /* argv[0] passed to function */
160913 Fts3Cursor **ppCsr /* OUT: Store cursor handle here */
160914){
160915 int rc;
160916 *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal, "fts3cursor");
160917 if( (*ppCsr)!=0 ){
160918 rc = SQLITE_OK;
160919 }else{
160920 char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
160921 sqlite3_result_error(pContext, zErr, -1);
160922 sqlite3_free(zErr);
160923 rc = SQLITE_ERROR;
160924 }
160925 return rc;
160926}
160927
160928/*
160929** Implementation of the snippet() function for FTS3
160930*/
160931static void fts3SnippetFunc(
160932 sqlite3_context *pContext, /* SQLite function call context */
160933 int nVal, /* Size of apVal[] array */
160934 sqlite3_value **apVal /* Array of arguments */
160935){
160936 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
160937 const char *zStart = "<b>";
160938 const char *zEnd = "</b>";
160939 const char *zEllipsis = "<b>...</b>";
160940 int iCol = -1;
160941 int nToken = 15; /* Default number of tokens in snippet */
160942
160943 /* There must be at least one argument passed to this function (otherwise
160944 ** the non-overloaded version would have been called instead of this one).
160945 */
160946 assert( nVal>=1 );
160947
160948 if( nVal>6 ){
160949 sqlite3_result_error(pContext,
160950 "wrong number of arguments to function snippet()", -1);
160951 return;
160952 }
160953 if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
160954
160955 switch( nVal ){
160956 case 6: nToken = sqlite3_value_int(apVal[5]);
160957 case 5: iCol = sqlite3_value_int(apVal[4]);
160958 case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
160959 case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
160960 case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
160961 }
160962 if( !zEllipsis || !zEnd || !zStart ){
160963 sqlite3_result_error_nomem(pContext);
160964 }else if( nToken==0 ){
160965 sqlite3_result_text(pContext, "", -1, SQLITE_STATIC);
160966 }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
160967 sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
160968 }
160969}
160970
160971/*
160972** Implementation of the offsets() function for FTS3
160973*/
160974static void fts3OffsetsFunc(
160975 sqlite3_context *pContext, /* SQLite function call context */
160976 int nVal, /* Size of argument array */
160977 sqlite3_value **apVal /* Array of arguments */
160978){
160979 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
160980
160981 UNUSED_PARAMETER(nVal);
160982
160983 assert( nVal==1 );
160984 if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
160985 assert( pCsr );
160986 if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
160987 sqlite3Fts3Offsets(pContext, pCsr);
160988 }
160989}
160990
160991/*
160992** Implementation of the special optimize() function for FTS3. This
160993** function merges all segments in the database to a single segment.
160994** Example usage is:
160995**
160996** SELECT optimize(t) FROM t LIMIT 1;
160997**
160998** where 't' is the name of an FTS3 table.
160999*/
161000static void fts3OptimizeFunc(
161001 sqlite3_context *pContext, /* SQLite function call context */
161002 int nVal, /* Size of argument array */
161003 sqlite3_value **apVal /* Array of arguments */
161004){
161005 int rc; /* Return code */
161006 Fts3Table *p; /* Virtual table handle */
161007 Fts3Cursor *pCursor; /* Cursor handle passed through apVal[0] */
161008
161009 UNUSED_PARAMETER(nVal);
161010
161011 assert( nVal==1 );
161012 if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
161013 p = (Fts3Table *)pCursor->base.pVtab;
161014 assert( p );
161015
161016 rc = sqlite3Fts3Optimize(p);
161017
161018 switch( rc ){
161019 case SQLITE_OK:
161020 sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
161021 break;
161022 case SQLITE_DONE:
161023 sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
161024 break;
161025 default:
161026 sqlite3_result_error_code(pContext, rc);
161027 break;
161028 }
161029}
161030
161031/*
161032** Implementation of the matchinfo() function for FTS3
161033*/
161034static void fts3MatchinfoFunc(
161035 sqlite3_context *pContext, /* SQLite function call context */
161036 int nVal, /* Size of argument array */
161037 sqlite3_value **apVal /* Array of arguments */
161038){
161039 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
161040 assert( nVal==1 || nVal==2 );
161041 if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
161042 const char *zArg = 0;
161043 if( nVal>1 ){
161044 zArg = (const char *)sqlite3_value_text(apVal[1]);
161045 }
161046 sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
161047 }
161048}
161049
161050/*
161051** This routine implements the xFindFunction method for the FTS3
161052** virtual table.
161053*/
161054static int fts3FindFunctionMethod(
161055 sqlite3_vtab *pVtab, /* Virtual table handle */
161056 int nArg, /* Number of SQL function arguments */
161057 const char *zName, /* Name of SQL function */
161058 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
161059 void **ppArg /* Unused */
161060){
161061 struct Overloaded {
161062 const char *zName;
161063 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
161064 } aOverload[] = {
161065 { "snippet", fts3SnippetFunc },
161066 { "offsets", fts3OffsetsFunc },
161067 { "optimize", fts3OptimizeFunc },
161068 { "matchinfo", fts3MatchinfoFunc },
161069 };
161070 int i; /* Iterator variable */
161071
161072 UNUSED_PARAMETER(pVtab);
161073 UNUSED_PARAMETER(nArg);
161074 UNUSED_PARAMETER(ppArg);
161075
161076 for(i=0; i<SizeofArray(aOverload); i++){
161077 if( strcmp(zName, aOverload[i].zName)==0 ){
161078 *pxFunc = aOverload[i].xFunc;
161079 return 1;
161080 }
161081 }
161082
161083 /* No function of the specified name was found. Return 0. */
161084 return 0;
161085}
161086
161087/*
161088** Implementation of FTS3 xRename method. Rename an fts3 table.
161089*/
161090static int fts3RenameMethod(
161091 sqlite3_vtab *pVtab, /* Virtual table handle */
161092 const char *zName /* New name of table */
161093){
161094 Fts3Table *p = (Fts3Table *)pVtab;
161095 sqlite3 *db = p->db; /* Database connection */
161096 int rc; /* Return Code */
161097
161098 /* At this point it must be known if the %_stat table exists or not.
161099 ** So bHasStat may not be 2. */
161100 rc = fts3SetHasStat(p);
161101
161102 /* As it happens, the pending terms table is always empty here. This is
161103 ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
161104 ** always opens a savepoint transaction. And the xSavepoint() method
161105 ** flushes the pending terms table. But leave the (no-op) call to
161106 ** PendingTermsFlush() in in case that changes.
161107 */
161108 assert( p->nPendingData==0 );
161109 if( rc==SQLITE_OK ){
161110 rc = sqlite3Fts3PendingTermsFlush(p);
161111 }
161112
161113 if( p->zContentTbl==0 ){
161114 fts3DbExec(&rc, db,
161115 "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';",
161116 p->zDb, p->zName, zName
161117 );
161118 }
161119
161120 if( p->bHasDocsize ){
161121 fts3DbExec(&rc, db,
161122 "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';",
161123 p->zDb, p->zName, zName
161124 );
161125 }
161126 if( p->bHasStat ){
161127 fts3DbExec(&rc, db,
161128 "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';",
161129 p->zDb, p->zName, zName
161130 );
161131 }
161132 fts3DbExec(&rc, db,
161133 "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
161134 p->zDb, p->zName, zName
161135 );
161136 fts3DbExec(&rc, db,
161137 "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';",
161138 p->zDb, p->zName, zName
161139 );
161140 return rc;
161141}
161142
161143/*
161144** The xSavepoint() method.
161145**
161146** Flush the contents of the pending-terms table to disk.
161147*/
161148static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
161149 int rc = SQLITE_OK;
161150 UNUSED_PARAMETER(iSavepoint);
161151 assert( ((Fts3Table *)pVtab)->inTransaction );
161152 assert( ((Fts3Table *)pVtab)->mxSavepoint <= iSavepoint );
161153 TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
161154 if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
161155 rc = fts3SyncMethod(pVtab);
161156 }
161157 return rc;
161158}
161159
161160/*
161161** The xRelease() method.
161162**
161163** This is a no-op.
161164*/
161165static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
161166 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
161167 UNUSED_PARAMETER(iSavepoint);
161168 UNUSED_PARAMETER(pVtab);
161169 assert( p->inTransaction );
161170 assert( p->mxSavepoint >= iSavepoint );
161171 TESTONLY( p->mxSavepoint = iSavepoint-1 );
161172 return SQLITE_OK;
161173}
161174
161175/*
161176** The xRollbackTo() method.
161177**
161178** Discard the contents of the pending terms table.
161179*/
161180static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
161181 Fts3Table *p = (Fts3Table*)pVtab;
161182 UNUSED_PARAMETER(iSavepoint);
161183 assert( p->inTransaction );
161184 assert( p->mxSavepoint >= iSavepoint );
161185 TESTONLY( p->mxSavepoint = iSavepoint );
161186 sqlite3Fts3PendingTermsClear(p);
161187 return SQLITE_OK;
161188}
161189
161190static const sqlite3_module fts3Module = {
161191 /* iVersion */ 2,
161192 /* xCreate */ fts3CreateMethod,
161193 /* xConnect */ fts3ConnectMethod,
161194 /* xBestIndex */ fts3BestIndexMethod,
161195 /* xDisconnect */ fts3DisconnectMethod,
161196 /* xDestroy */ fts3DestroyMethod,
161197 /* xOpen */ fts3OpenMethod,
161198 /* xClose */ fts3CloseMethod,
161199 /* xFilter */ fts3FilterMethod,
161200 /* xNext */ fts3NextMethod,
161201 /* xEof */ fts3EofMethod,
161202 /* xColumn */ fts3ColumnMethod,
161203 /* xRowid */ fts3RowidMethod,
161204 /* xUpdate */ fts3UpdateMethod,
161205 /* xBegin */ fts3BeginMethod,
161206 /* xSync */ fts3SyncMethod,
161207 /* xCommit */ fts3CommitMethod,
161208 /* xRollback */ fts3RollbackMethod,
161209 /* xFindFunction */ fts3FindFunctionMethod,
161210 /* xRename */ fts3RenameMethod,
161211 /* xSavepoint */ fts3SavepointMethod,
161212 /* xRelease */ fts3ReleaseMethod,
161213 /* xRollbackTo */ fts3RollbackToMethod,
161214};
161215
161216/*
161217** This function is registered as the module destructor (called when an
161218** FTS3 enabled database connection is closed). It frees the memory
161219** allocated for the tokenizer hash table.
161220*/
161221static void hashDestroy(void *p){
161222 Fts3Hash *pHash = (Fts3Hash *)p;
161223 sqlite3Fts3HashClear(pHash);
161224 sqlite3_free(pHash);
161225}
161226
161227/*
161228** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
161229** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
161230** respectively. The following three forward declarations are for functions
161231** declared in these files used to retrieve the respective implementations.
161232**
161233** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
161234** to by the argument to point to the "simple" tokenizer implementation.
161235** And so on.
161236*/
161237SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
161238SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
161239#ifndef SQLITE_DISABLE_FTS3_UNICODE
161240SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
161241#endif
161242#ifdef SQLITE_ENABLE_ICU
161243SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
161244#endif
161245
161246/*
161247** Initialize the fts3 extension. If this extension is built as part
161248** of the sqlite library, then this function is called directly by
161249** SQLite. If fts3 is built as a dynamically loadable extension, this
161250** function is called by the sqlite3_extension_init() entry point.
161251*/
161252SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
161253 int rc = SQLITE_OK;
161254 Fts3Hash *pHash = 0;
161255 const sqlite3_tokenizer_module *pSimple = 0;
161256 const sqlite3_tokenizer_module *pPorter = 0;
161257#ifndef SQLITE_DISABLE_FTS3_UNICODE
161258 const sqlite3_tokenizer_module *pUnicode = 0;
161259#endif
161260
161261#ifdef SQLITE_ENABLE_ICU
161262 const sqlite3_tokenizer_module *pIcu = 0;
161263 sqlite3Fts3IcuTokenizerModule(&pIcu);
161264#endif
161265
161266#ifndef SQLITE_DISABLE_FTS3_UNICODE
161267 sqlite3Fts3UnicodeTokenizer(&pUnicode);
161268#endif
161269
161270#ifdef SQLITE_TEST
161271 rc = sqlite3Fts3InitTerm(db);
161272 if( rc!=SQLITE_OK ) return rc;
161273#endif
161274
161275 rc = sqlite3Fts3InitAux(db);
161276 if( rc!=SQLITE_OK ) return rc;
161277
161278 sqlite3Fts3SimpleTokenizerModule(&pSimple);
161279 sqlite3Fts3PorterTokenizerModule(&pPorter);
161280
161281 /* Allocate and initialize the hash-table used to store tokenizers. */
161282 pHash = sqlite3_malloc(sizeof(Fts3Hash));
161283 if( !pHash ){
161284 rc = SQLITE_NOMEM;
161285 }else{
161286 sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
161287 }
161288
161289 /* Load the built-in tokenizers into the hash table */
161290 if( rc==SQLITE_OK ){
161291 if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
161292 || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
161293
161294#ifndef SQLITE_DISABLE_FTS3_UNICODE
161295 || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode)
161296#endif
161297#ifdef SQLITE_ENABLE_ICU
161298 || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
161299#endif
161300 ){
161301 rc = SQLITE_NOMEM;
161302 }
161303 }
161304
161305#ifdef SQLITE_TEST
161306 if( rc==SQLITE_OK ){
161307 rc = sqlite3Fts3ExprInitTestInterface(db, pHash);
161308 }
161309#endif
161310
161311 /* Create the virtual table wrapper around the hash-table and overload
161312 ** the four scalar functions. If this is successful, register the
161313 ** module with sqlite.
161314 */
161315 if( SQLITE_OK==rc
161316 && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
161317 && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
161318 && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
161319 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
161320 && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
161321 && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
161322 ){
161323 rc = sqlite3_create_module_v2(
161324 db, "fts3", &fts3Module, (void *)pHash, hashDestroy
161325 );
161326 if( rc==SQLITE_OK ){
161327 rc = sqlite3_create_module_v2(
161328 db, "fts4", &fts3Module, (void *)pHash, 0
161329 );
161330 }
161331 if( rc==SQLITE_OK ){
161332 rc = sqlite3Fts3InitTok(db, (void *)pHash);
161333 }
161334 return rc;
161335 }
161336
161337
161338 /* An error has occurred. Delete the hash table and return the error code. */
161339 assert( rc!=SQLITE_OK );
161340 if( pHash ){
161341 sqlite3Fts3HashClear(pHash);
161342 sqlite3_free(pHash);
161343 }
161344 return rc;
161345}
161346
161347/*
161348** Allocate an Fts3MultiSegReader for each token in the expression headed
161349** by pExpr.
161350**
161351** An Fts3SegReader object is a cursor that can seek or scan a range of
161352** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
161353** Fts3SegReader objects internally to provide an interface to seek or scan
161354** within the union of all segments of a b-tree. Hence the name.
161355**
161356** If the allocated Fts3MultiSegReader just seeks to a single entry in a
161357** segment b-tree (if the term is not a prefix or it is a prefix for which
161358** there exists prefix b-tree of the right length) then it may be traversed
161359** and merged incrementally. Otherwise, it has to be merged into an in-memory
161360** doclist and then traversed.
161361*/
161362static void fts3EvalAllocateReaders(
161363 Fts3Cursor *pCsr, /* FTS cursor handle */
161364 Fts3Expr *pExpr, /* Allocate readers for this expression */
161365 int *pnToken, /* OUT: Total number of tokens in phrase. */
161366 int *pnOr, /* OUT: Total number of OR nodes in expr. */
161367 int *pRc /* IN/OUT: Error code */
161368){
161369 if( pExpr && SQLITE_OK==*pRc ){
161370 if( pExpr->eType==FTSQUERY_PHRASE ){
161371 int i;
161372 int nToken = pExpr->pPhrase->nToken;
161373 *pnToken += nToken;
161374 for(i=0; i<nToken; i++){
161375 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
161376 int rc = fts3TermSegReaderCursor(pCsr,
161377 pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
161378 );
161379 if( rc!=SQLITE_OK ){
161380 *pRc = rc;
161381 return;
161382 }
161383 }
161384 assert( pExpr->pPhrase->iDoclistToken==0 );
161385 pExpr->pPhrase->iDoclistToken = -1;
161386 }else{
161387 *pnOr += (pExpr->eType==FTSQUERY_OR);
161388 fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
161389 fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
161390 }
161391 }
161392}
161393
161394/*
161395** Arguments pList/nList contain the doclist for token iToken of phrase p.
161396** It is merged into the main doclist stored in p->doclist.aAll/nAll.
161397**
161398** This function assumes that pList points to a buffer allocated using
161399** sqlite3_malloc(). This function takes responsibility for eventually
161400** freeing the buffer.
161401**
161402** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
161403*/
161404static int fts3EvalPhraseMergeToken(
161405 Fts3Table *pTab, /* FTS Table pointer */
161406 Fts3Phrase *p, /* Phrase to merge pList/nList into */
161407 int iToken, /* Token pList/nList corresponds to */
161408 char *pList, /* Pointer to doclist */
161409 int nList /* Number of bytes in pList */
161410){
161411 int rc = SQLITE_OK;
161412 assert( iToken!=p->iDoclistToken );
161413
161414 if( pList==0 ){
161415 sqlite3_free(p->doclist.aAll);
161416 p->doclist.aAll = 0;
161417 p->doclist.nAll = 0;
161418 }
161419
161420 else if( p->iDoclistToken<0 ){
161421 p->doclist.aAll = pList;
161422 p->doclist.nAll = nList;
161423 }
161424
161425 else if( p->doclist.aAll==0 ){
161426 sqlite3_free(pList);
161427 }
161428
161429 else {
161430 char *pLeft;
161431 char *pRight;
161432 int nLeft;
161433 int nRight;
161434 int nDiff;
161435
161436 if( p->iDoclistToken<iToken ){
161437 pLeft = p->doclist.aAll;
161438 nLeft = p->doclist.nAll;
161439 pRight = pList;
161440 nRight = nList;
161441 nDiff = iToken - p->iDoclistToken;
161442 }else{
161443 pRight = p->doclist.aAll;
161444 nRight = p->doclist.nAll;
161445 pLeft = pList;
161446 nLeft = nList;
161447 nDiff = p->iDoclistToken - iToken;
161448 }
161449
161450 rc = fts3DoclistPhraseMerge(
161451 pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
161452 );
161453 sqlite3_free(pLeft);
161454 p->doclist.aAll = pRight;
161455 p->doclist.nAll = nRight;
161456 }
161457
161458 if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
161459 return rc;
161460}
161461
161462/*
161463** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
161464** does not take deferred tokens into account.
161465**
161466** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
161467*/
161468static int fts3EvalPhraseLoad(
161469 Fts3Cursor *pCsr, /* FTS Cursor handle */
161470 Fts3Phrase *p /* Phrase object */
161471){
161472 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
161473 int iToken;
161474 int rc = SQLITE_OK;
161475
161476 for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
161477 Fts3PhraseToken *pToken = &p->aToken[iToken];
161478 assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
161479
161480 if( pToken->pSegcsr ){
161481 int nThis = 0;
161482 char *pThis = 0;
161483 rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
161484 if( rc==SQLITE_OK ){
161485 rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
161486 }
161487 }
161488 assert( pToken->pSegcsr==0 );
161489 }
161490
161491 return rc;
161492}
161493
161494/*
161495** This function is called on each phrase after the position lists for
161496** any deferred tokens have been loaded into memory. It updates the phrases
161497** current position list to include only those positions that are really
161498** instances of the phrase (after considering deferred tokens). If this
161499** means that the phrase does not appear in the current row, doclist.pList
161500** and doclist.nList are both zeroed.
161501**
161502** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
161503*/
161504static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
161505 int iToken; /* Used to iterate through phrase tokens */
161506 char *aPoslist = 0; /* Position list for deferred tokens */
161507 int nPoslist = 0; /* Number of bytes in aPoslist */
161508 int iPrev = -1; /* Token number of previous deferred token */
161509
161510 assert( pPhrase->doclist.bFreeList==0 );
161511
161512 for(iToken=0; iToken<pPhrase->nToken; iToken++){
161513 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
161514 Fts3DeferredToken *pDeferred = pToken->pDeferred;
161515
161516 if( pDeferred ){
161517 char *pList;
161518 int nList;
161519 int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
161520 if( rc!=SQLITE_OK ) return rc;
161521
161522 if( pList==0 ){
161523 sqlite3_free(aPoslist);
161524 pPhrase->doclist.pList = 0;
161525 pPhrase->doclist.nList = 0;
161526 return SQLITE_OK;
161527
161528 }else if( aPoslist==0 ){
161529 aPoslist = pList;
161530 nPoslist = nList;
161531
161532 }else{
161533 char *aOut = pList;
161534 char *p1 = aPoslist;
161535 char *p2 = aOut;
161536
161537 assert( iPrev>=0 );
161538 fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
161539 sqlite3_free(aPoslist);
161540 aPoslist = pList;
161541 nPoslist = (int)(aOut - aPoslist);
161542 if( nPoslist==0 ){
161543 sqlite3_free(aPoslist);
161544 pPhrase->doclist.pList = 0;
161545 pPhrase->doclist.nList = 0;
161546 return SQLITE_OK;
161547 }
161548 }
161549 iPrev = iToken;
161550 }
161551 }
161552
161553 if( iPrev>=0 ){
161554 int nMaxUndeferred = pPhrase->iDoclistToken;
161555 if( nMaxUndeferred<0 ){
161556 pPhrase->doclist.pList = aPoslist;
161557 pPhrase->doclist.nList = nPoslist;
161558 pPhrase->doclist.iDocid = pCsr->iPrevId;
161559 pPhrase->doclist.bFreeList = 1;
161560 }else{
161561 int nDistance;
161562 char *p1;
161563 char *p2;
161564 char *aOut;
161565
161566 if( nMaxUndeferred>iPrev ){
161567 p1 = aPoslist;
161568 p2 = pPhrase->doclist.pList;
161569 nDistance = nMaxUndeferred - iPrev;
161570 }else{
161571 p1 = pPhrase->doclist.pList;
161572 p2 = aPoslist;
161573 nDistance = iPrev - nMaxUndeferred;
161574 }
161575
161576 aOut = (char *)sqlite3_malloc(nPoslist+8);
161577 if( !aOut ){
161578 sqlite3_free(aPoslist);
161579 return SQLITE_NOMEM;
161580 }
161581
161582 pPhrase->doclist.pList = aOut;
161583 if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
161584 pPhrase->doclist.bFreeList = 1;
161585 pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
161586 }else{
161587 sqlite3_free(aOut);
161588 pPhrase->doclist.pList = 0;
161589 pPhrase->doclist.nList = 0;
161590 }
161591 sqlite3_free(aPoslist);
161592 }
161593 }
161594
161595 return SQLITE_OK;
161596}
161597
161598/*
161599** Maximum number of tokens a phrase may have to be considered for the
161600** incremental doclists strategy.
161601*/
161602#define MAX_INCR_PHRASE_TOKENS 4
161603
161604/*
161605** This function is called for each Fts3Phrase in a full-text query
161606** expression to initialize the mechanism for returning rows. Once this
161607** function has been called successfully on an Fts3Phrase, it may be
161608** used with fts3EvalPhraseNext() to iterate through the matching docids.
161609**
161610** If parameter bOptOk is true, then the phrase may (or may not) use the
161611** incremental loading strategy. Otherwise, the entire doclist is loaded into
161612** memory within this call.
161613**
161614** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
161615*/
161616static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
161617 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
161618 int rc = SQLITE_OK; /* Error code */
161619 int i;
161620
161621 /* Determine if doclists may be loaded from disk incrementally. This is
161622 ** possible if the bOptOk argument is true, the FTS doclists will be
161623 ** scanned in forward order, and the phrase consists of
161624 ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
161625 ** tokens or prefix tokens that cannot use a prefix-index. */
161626 int bHaveIncr = 0;
161627 int bIncrOk = (bOptOk
161628 && pCsr->bDesc==pTab->bDescIdx
161629 && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
161630#ifdef SQLITE_TEST
161631 && pTab->bNoIncrDoclist==0
161632#endif
161633 );
161634 for(i=0; bIncrOk==1 && i<p->nToken; i++){
161635 Fts3PhraseToken *pToken = &p->aToken[i];
161636 if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
161637 bIncrOk = 0;
161638 }
161639 if( pToken->pSegcsr ) bHaveIncr = 1;
161640 }
161641
161642 if( bIncrOk && bHaveIncr ){
161643 /* Use the incremental approach. */
161644 int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
161645 for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
161646 Fts3PhraseToken *pToken = &p->aToken[i];
161647 Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
161648 if( pSegcsr ){
161649 rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
161650 }
161651 }
161652 p->bIncr = 1;
161653 }else{
161654 /* Load the full doclist for the phrase into memory. */
161655 rc = fts3EvalPhraseLoad(pCsr, p);
161656 p->bIncr = 0;
161657 }
161658
161659 assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
161660 return rc;
161661}
161662
161663/*
161664** This function is used to iterate backwards (from the end to start)
161665** through doclists. It is used by this module to iterate through phrase
161666** doclists in reverse and by the fts3_write.c module to iterate through
161667** pending-terms lists when writing to databases with "order=desc".
161668**
161669** The doclist may be sorted in ascending (parameter bDescIdx==0) or
161670** descending (parameter bDescIdx==1) order of docid. Regardless, this
161671** function iterates from the end of the doclist to the beginning.
161672*/
161673SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
161674 int bDescIdx, /* True if the doclist is desc */
161675 char *aDoclist, /* Pointer to entire doclist */
161676 int nDoclist, /* Length of aDoclist in bytes */
161677 char **ppIter, /* IN/OUT: Iterator pointer */
161678 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
161679 int *pnList, /* OUT: List length pointer */
161680 u8 *pbEof /* OUT: End-of-file flag */
161681){
161682 char *p = *ppIter;
161683
161684 assert( nDoclist>0 );
161685 assert( *pbEof==0 );
161686 assert( p || *piDocid==0 );
161687 assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
161688
161689 if( p==0 ){
161690 sqlite3_int64 iDocid = 0;
161691 char *pNext = 0;
161692 char *pDocid = aDoclist;
161693 char *pEnd = &aDoclist[nDoclist];
161694 int iMul = 1;
161695
161696 while( pDocid<pEnd ){
161697 sqlite3_int64 iDelta;
161698 pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
161699 iDocid += (iMul * iDelta);
161700 pNext = pDocid;
161701 fts3PoslistCopy(0, &pDocid);
161702 while( pDocid<pEnd && *pDocid==0 ) pDocid++;
161703 iMul = (bDescIdx ? -1 : 1);
161704 }
161705
161706 *pnList = (int)(pEnd - pNext);
161707 *ppIter = pNext;
161708 *piDocid = iDocid;
161709 }else{
161710 int iMul = (bDescIdx ? -1 : 1);
161711 sqlite3_int64 iDelta;
161712 fts3GetReverseVarint(&p, aDoclist, &iDelta);
161713 *piDocid -= (iMul * iDelta);
161714
161715 if( p==aDoclist ){
161716 *pbEof = 1;
161717 }else{
161718 char *pSave = p;
161719 fts3ReversePoslist(aDoclist, &p);
161720 *pnList = (int)(pSave - p);
161721 }
161722 *ppIter = p;
161723 }
161724}
161725
161726/*
161727** Iterate forwards through a doclist.
161728*/
161729SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
161730 int bDescIdx, /* True if the doclist is desc */
161731 char *aDoclist, /* Pointer to entire doclist */
161732 int nDoclist, /* Length of aDoclist in bytes */
161733 char **ppIter, /* IN/OUT: Iterator pointer */
161734 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
161735 u8 *pbEof /* OUT: End-of-file flag */
161736){
161737 char *p = *ppIter;
161738
161739 assert( nDoclist>0 );
161740 assert( *pbEof==0 );
161741 assert( p || *piDocid==0 );
161742 assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
161743
161744 if( p==0 ){
161745 p = aDoclist;
161746 p += sqlite3Fts3GetVarint(p, piDocid);
161747 }else{
161748 fts3PoslistCopy(0, &p);
161749 while( p<&aDoclist[nDoclist] && *p==0 ) p++;
161750 if( p>=&aDoclist[nDoclist] ){
161751 *pbEof = 1;
161752 }else{
161753 sqlite3_int64 iVar;
161754 p += sqlite3Fts3GetVarint(p, &iVar);
161755 *piDocid += ((bDescIdx ? -1 : 1) * iVar);
161756 }
161757 }
161758
161759 *ppIter = p;
161760}
161761
161762/*
161763** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
161764** to true if EOF is reached.
161765*/
161766static void fts3EvalDlPhraseNext(
161767 Fts3Table *pTab,
161768 Fts3Doclist *pDL,
161769 u8 *pbEof
161770){
161771 char *pIter; /* Used to iterate through aAll */
161772 char *pEnd = &pDL->aAll[pDL->nAll]; /* 1 byte past end of aAll */
161773
161774 if( pDL->pNextDocid ){
161775 pIter = pDL->pNextDocid;
161776 }else{
161777 pIter = pDL->aAll;
161778 }
161779
161780 if( pIter>=pEnd ){
161781 /* We have already reached the end of this doclist. EOF. */
161782 *pbEof = 1;
161783 }else{
161784 sqlite3_int64 iDelta;
161785 pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
161786 if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
161787 pDL->iDocid += iDelta;
161788 }else{
161789 pDL->iDocid -= iDelta;
161790 }
161791 pDL->pList = pIter;
161792 fts3PoslistCopy(0, &pIter);
161793 pDL->nList = (int)(pIter - pDL->pList);
161794
161795 /* pIter now points just past the 0x00 that terminates the position-
161796 ** list for document pDL->iDocid. However, if this position-list was
161797 ** edited in place by fts3EvalNearTrim(), then pIter may not actually
161798 ** point to the start of the next docid value. The following line deals
161799 ** with this case by advancing pIter past the zero-padding added by
161800 ** fts3EvalNearTrim(). */
161801 while( pIter<pEnd && *pIter==0 ) pIter++;
161802
161803 pDL->pNextDocid = pIter;
161804 assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
161805 *pbEof = 0;
161806 }
161807}
161808
161809/*
161810** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
161811*/
161812typedef struct TokenDoclist TokenDoclist;
161813struct TokenDoclist {
161814 int bIgnore;
161815 sqlite3_int64 iDocid;
161816 char *pList;
161817 int nList;
161818};
161819
161820/*
161821** Token pToken is an incrementally loaded token that is part of a
161822** multi-token phrase. Advance it to the next matching document in the
161823** database and populate output variable *p with the details of the new
161824** entry. Or, if the iterator has reached EOF, set *pbEof to true.
161825**
161826** If an error occurs, return an SQLite error code. Otherwise, return
161827** SQLITE_OK.
161828*/
161829static int incrPhraseTokenNext(
161830 Fts3Table *pTab, /* Virtual table handle */
161831 Fts3Phrase *pPhrase, /* Phrase to advance token of */
161832 int iToken, /* Specific token to advance */
161833 TokenDoclist *p, /* OUT: Docid and doclist for new entry */
161834 u8 *pbEof /* OUT: True if iterator is at EOF */
161835){
161836 int rc = SQLITE_OK;
161837
161838 if( pPhrase->iDoclistToken==iToken ){
161839 assert( p->bIgnore==0 );
161840 assert( pPhrase->aToken[iToken].pSegcsr==0 );
161841 fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
161842 p->pList = pPhrase->doclist.pList;
161843 p->nList = pPhrase->doclist.nList;
161844 p->iDocid = pPhrase->doclist.iDocid;
161845 }else{
161846 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
161847 assert( pToken->pDeferred==0 );
161848 assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
161849 if( pToken->pSegcsr ){
161850 assert( p->bIgnore==0 );
161851 rc = sqlite3Fts3MsrIncrNext(
161852 pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
161853 );
161854 if( p->pList==0 ) *pbEof = 1;
161855 }else{
161856 p->bIgnore = 1;
161857 }
161858 }
161859
161860 return rc;
161861}
161862
161863
161864/*
161865** The phrase iterator passed as the second argument:
161866**
161867** * features at least one token that uses an incremental doclist, and
161868**
161869** * does not contain any deferred tokens.
161870**
161871** Advance it to the next matching documnent in the database and populate
161872** the Fts3Doclist.pList and nList fields.
161873**
161874** If there is no "next" entry and no error occurs, then *pbEof is set to
161875** 1 before returning. Otherwise, if no error occurs and the iterator is
161876** successfully advanced, *pbEof is set to 0.
161877**
161878** If an error occurs, return an SQLite error code. Otherwise, return
161879** SQLITE_OK.
161880*/
161881static int fts3EvalIncrPhraseNext(
161882 Fts3Cursor *pCsr, /* FTS Cursor handle */
161883 Fts3Phrase *p, /* Phrase object to advance to next docid */
161884 u8 *pbEof /* OUT: Set to 1 if EOF */
161885){
161886 int rc = SQLITE_OK;
161887 Fts3Doclist *pDL = &p->doclist;
161888 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
161889 u8 bEof = 0;
161890
161891 /* This is only called if it is guaranteed that the phrase has at least
161892 ** one incremental token. In which case the bIncr flag is set. */
161893 assert( p->bIncr==1 );
161894
161895 if( p->nToken==1 ){
161896 rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
161897 &pDL->iDocid, &pDL->pList, &pDL->nList
161898 );
161899 if( pDL->pList==0 ) bEof = 1;
161900 }else{
161901 int bDescDoclist = pCsr->bDesc;
161902 struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS];
161903
161904 memset(a, 0, sizeof(a));
161905 assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
161906 assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
161907
161908 while( bEof==0 ){
161909 int bMaxSet = 0;
161910 sqlite3_int64 iMax = 0; /* Largest docid for all iterators */
161911 int i; /* Used to iterate through tokens */
161912
161913 /* Advance the iterator for each token in the phrase once. */
161914 for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
161915 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
161916 if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){
161917 iMax = a[i].iDocid;
161918 bMaxSet = 1;
161919 }
161920 }
161921 assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) );
161922 assert( rc!=SQLITE_OK || bMaxSet );
161923
161924 /* Keep advancing iterators until they all point to the same document */
161925 for(i=0; i<p->nToken; i++){
161926 while( rc==SQLITE_OK && bEof==0
161927 && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0
161928 ){
161929 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
161930 if( DOCID_CMP(a[i].iDocid, iMax)>0 ){
161931 iMax = a[i].iDocid;
161932 i = 0;
161933 }
161934 }
161935 }
161936
161937 /* Check if the current entries really are a phrase match */
161938 if( bEof==0 ){
161939 int nList = 0;
161940 int nByte = a[p->nToken-1].nList;
161941 char *aDoclist = sqlite3_malloc(nByte+1);
161942 if( !aDoclist ) return SQLITE_NOMEM;
161943 memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
161944
161945 for(i=0; i<(p->nToken-1); i++){
161946 if( a[i].bIgnore==0 ){
161947 char *pL = a[i].pList;
161948 char *pR = aDoclist;
161949 char *pOut = aDoclist;
161950 int nDist = p->nToken-1-i;
161951 int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
161952 if( res==0 ) break;
161953 nList = (int)(pOut - aDoclist);
161954 }
161955 }
161956 if( i==(p->nToken-1) ){
161957 pDL->iDocid = iMax;
161958 pDL->pList = aDoclist;
161959 pDL->nList = nList;
161960 pDL->bFreeList = 1;
161961 break;
161962 }
161963 sqlite3_free(aDoclist);
161964 }
161965 }
161966 }
161967
161968 *pbEof = bEof;
161969 return rc;
161970}
161971
161972/*
161973** Attempt to move the phrase iterator to point to the next matching docid.
161974** If an error occurs, return an SQLite error code. Otherwise, return
161975** SQLITE_OK.
161976**
161977** If there is no "next" entry and no error occurs, then *pbEof is set to
161978** 1 before returning. Otherwise, if no error occurs and the iterator is
161979** successfully advanced, *pbEof is set to 0.
161980*/
161981static int fts3EvalPhraseNext(
161982 Fts3Cursor *pCsr, /* FTS Cursor handle */
161983 Fts3Phrase *p, /* Phrase object to advance to next docid */
161984 u8 *pbEof /* OUT: Set to 1 if EOF */
161985){
161986 int rc = SQLITE_OK;
161987 Fts3Doclist *pDL = &p->doclist;
161988 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
161989
161990 if( p->bIncr ){
161991 rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
161992 }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
161993 sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
161994 &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
161995 );
161996 pDL->pList = pDL->pNextDocid;
161997 }else{
161998 fts3EvalDlPhraseNext(pTab, pDL, pbEof);
161999 }
162000
162001 return rc;
162002}
162003
162004/*
162005**
162006** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
162007** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
162008** expression. Also the Fts3Expr.bDeferred variable is set to true for any
162009** expressions for which all descendent tokens are deferred.
162010**
162011** If parameter bOptOk is zero, then it is guaranteed that the
162012** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
162013** each phrase in the expression (subject to deferred token processing).
162014** Or, if bOptOk is non-zero, then one or more tokens within the expression
162015** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
162016**
162017** If an error occurs within this function, *pRc is set to an SQLite error
162018** code before returning.
162019*/
162020static void fts3EvalStartReaders(
162021 Fts3Cursor *pCsr, /* FTS Cursor handle */
162022 Fts3Expr *pExpr, /* Expression to initialize phrases in */
162023 int *pRc /* IN/OUT: Error code */
162024){
162025 if( pExpr && SQLITE_OK==*pRc ){
162026 if( pExpr->eType==FTSQUERY_PHRASE ){
162027 int nToken = pExpr->pPhrase->nToken;
162028 if( nToken ){
162029 int i;
162030 for(i=0; i<nToken; i++){
162031 if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
162032 }
162033 pExpr->bDeferred = (i==nToken);
162034 }
162035 *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
162036 }else{
162037 fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
162038 fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
162039 pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
162040 }
162041 }
162042}
162043
162044/*
162045** An array of the following structures is assembled as part of the process
162046** of selecting tokens to defer before the query starts executing (as part
162047** of the xFilter() method). There is one element in the array for each
162048** token in the FTS expression.
162049**
162050** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
162051** to phrases that are connected only by AND and NEAR operators (not OR or
162052** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
162053** separately. The root of a tokens AND/NEAR cluster is stored in
162054** Fts3TokenAndCost.pRoot.
162055*/
162056typedef struct Fts3TokenAndCost Fts3TokenAndCost;
162057struct Fts3TokenAndCost {
162058 Fts3Phrase *pPhrase; /* The phrase the token belongs to */
162059 int iToken; /* Position of token in phrase */
162060 Fts3PhraseToken *pToken; /* The token itself */
162061 Fts3Expr *pRoot; /* Root of NEAR/AND cluster */
162062 int nOvfl; /* Number of overflow pages to load doclist */
162063 int iCol; /* The column the token must match */
162064};
162065
162066/*
162067** This function is used to populate an allocated Fts3TokenAndCost array.
162068**
162069** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
162070** Otherwise, if an error occurs during execution, *pRc is set to an
162071** SQLite error code.
162072*/
162073static void fts3EvalTokenCosts(
162074 Fts3Cursor *pCsr, /* FTS Cursor handle */
162075 Fts3Expr *pRoot, /* Root of current AND/NEAR cluster */
162076 Fts3Expr *pExpr, /* Expression to consider */
162077 Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */
162078 Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */
162079 int *pRc /* IN/OUT: Error code */
162080){
162081 if( *pRc==SQLITE_OK ){
162082 if( pExpr->eType==FTSQUERY_PHRASE ){
162083 Fts3Phrase *pPhrase = pExpr->pPhrase;
162084 int i;
162085 for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
162086 Fts3TokenAndCost *pTC = (*ppTC)++;
162087 pTC->pPhrase = pPhrase;
162088 pTC->iToken = i;
162089 pTC->pRoot = pRoot;
162090 pTC->pToken = &pPhrase->aToken[i];
162091 pTC->iCol = pPhrase->iColumn;
162092 *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
162093 }
162094 }else if( pExpr->eType!=FTSQUERY_NOT ){
162095 assert( pExpr->eType==FTSQUERY_OR
162096 || pExpr->eType==FTSQUERY_AND
162097 || pExpr->eType==FTSQUERY_NEAR
162098 );
162099 assert( pExpr->pLeft && pExpr->pRight );
162100 if( pExpr->eType==FTSQUERY_OR ){
162101 pRoot = pExpr->pLeft;
162102 **ppOr = pRoot;
162103 (*ppOr)++;
162104 }
162105 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
162106 if( pExpr->eType==FTSQUERY_OR ){
162107 pRoot = pExpr->pRight;
162108 **ppOr = pRoot;
162109 (*ppOr)++;
162110 }
162111 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
162112 }
162113 }
162114}
162115
162116/*
162117** Determine the average document (row) size in pages. If successful,
162118** write this value to *pnPage and return SQLITE_OK. Otherwise, return
162119** an SQLite error code.
162120**
162121** The average document size in pages is calculated by first calculating
162122** determining the average size in bytes, B. If B is less than the amount
162123** of data that will fit on a single leaf page of an intkey table in
162124** this database, then the average docsize is 1. Otherwise, it is 1 plus
162125** the number of overflow pages consumed by a record B bytes in size.
162126*/
162127static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
162128 int rc = SQLITE_OK;
162129 if( pCsr->nRowAvg==0 ){
162130 /* The average document size, which is required to calculate the cost
162131 ** of each doclist, has not yet been determined. Read the required
162132 ** data from the %_stat table to calculate it.
162133 **
162134 ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
162135 ** varints, where nCol is the number of columns in the FTS3 table.
162136 ** The first varint is the number of documents currently stored in
162137 ** the table. The following nCol varints contain the total amount of
162138 ** data stored in all rows of each column of the table, from left
162139 ** to right.
162140 */
162141 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
162142 sqlite3_stmt *pStmt;
162143 sqlite3_int64 nDoc = 0;
162144 sqlite3_int64 nByte = 0;
162145 const char *pEnd;
162146 const char *a;
162147
162148 rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
162149 if( rc!=SQLITE_OK ) return rc;
162150 a = sqlite3_column_blob(pStmt, 0);
162151 assert( a );
162152
162153 pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
162154 a += sqlite3Fts3GetVarint(a, &nDoc);
162155 while( a<pEnd ){
162156 a += sqlite3Fts3GetVarint(a, &nByte);
162157 }
162158 if( nDoc==0 || nByte==0 ){
162159 sqlite3_reset(pStmt);
162160 return FTS_CORRUPT_VTAB;
162161 }
162162
162163 pCsr->nDoc = nDoc;
162164 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
162165 assert( pCsr->nRowAvg>0 );
162166 rc = sqlite3_reset(pStmt);
162167 }
162168
162169 *pnPage = pCsr->nRowAvg;
162170 return rc;
162171}
162172
162173/*
162174** This function is called to select the tokens (if any) that will be
162175** deferred. The array aTC[] has already been populated when this is
162176** called.
162177**
162178** This function is called once for each AND/NEAR cluster in the
162179** expression. Each invocation determines which tokens to defer within
162180** the cluster with root node pRoot. See comments above the definition
162181** of struct Fts3TokenAndCost for more details.
162182**
162183** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
162184** called on each token to defer. Otherwise, an SQLite error code is
162185** returned.
162186*/
162187static int fts3EvalSelectDeferred(
162188 Fts3Cursor *pCsr, /* FTS Cursor handle */
162189 Fts3Expr *pRoot, /* Consider tokens with this root node */
162190 Fts3TokenAndCost *aTC, /* Array of expression tokens and costs */
162191 int nTC /* Number of entries in aTC[] */
162192){
162193 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
162194 int nDocSize = 0; /* Number of pages per doc loaded */
162195 int rc = SQLITE_OK; /* Return code */
162196 int ii; /* Iterator variable for various purposes */
162197 int nOvfl = 0; /* Total overflow pages used by doclists */
162198 int nToken = 0; /* Total number of tokens in cluster */
162199
162200 int nMinEst = 0; /* The minimum count for any phrase so far. */
162201 int nLoad4 = 1; /* (Phrases that will be loaded)^4. */
162202
162203 /* Tokens are never deferred for FTS tables created using the content=xxx
162204 ** option. The reason being that it is not guaranteed that the content
162205 ** table actually contains the same data as the index. To prevent this from
162206 ** causing any problems, the deferred token optimization is completely
162207 ** disabled for content=xxx tables. */
162208 if( pTab->zContentTbl ){
162209 return SQLITE_OK;
162210 }
162211
162212 /* Count the tokens in this AND/NEAR cluster. If none of the doclists
162213 ** associated with the tokens spill onto overflow pages, or if there is
162214 ** only 1 token, exit early. No tokens to defer in this case. */
162215 for(ii=0; ii<nTC; ii++){
162216 if( aTC[ii].pRoot==pRoot ){
162217 nOvfl += aTC[ii].nOvfl;
162218 nToken++;
162219 }
162220 }
162221 if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
162222
162223 /* Obtain the average docsize (in pages). */
162224 rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
162225 assert( rc!=SQLITE_OK || nDocSize>0 );
162226
162227
162228 /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
162229 ** of the number of overflow pages that will be loaded by the pager layer
162230 ** to retrieve the entire doclist for the token from the full-text index.
162231 ** Load the doclists for tokens that are either:
162232 **
162233 ** a. The cheapest token in the entire query (i.e. the one visited by the
162234 ** first iteration of this loop), or
162235 **
162236 ** b. Part of a multi-token phrase.
162237 **
162238 ** After each token doclist is loaded, merge it with the others from the
162239 ** same phrase and count the number of documents that the merged doclist
162240 ** contains. Set variable "nMinEst" to the smallest number of documents in
162241 ** any phrase doclist for which 1 or more token doclists have been loaded.
162242 ** Let nOther be the number of other phrases for which it is certain that
162243 ** one or more tokens will not be deferred.
162244 **
162245 ** Then, for each token, defer it if loading the doclist would result in
162246 ** loading N or more overflow pages into memory, where N is computed as:
162247 **
162248 ** (nMinEst + 4^nOther - 1) / (4^nOther)
162249 */
162250 for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
162251 int iTC; /* Used to iterate through aTC[] array. */
162252 Fts3TokenAndCost *pTC = 0; /* Set to cheapest remaining token. */
162253
162254 /* Set pTC to point to the cheapest remaining token. */
162255 for(iTC=0; iTC<nTC; iTC++){
162256 if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
162257 && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
162258 ){
162259 pTC = &aTC[iTC];
162260 }
162261 }
162262 assert( pTC );
162263
162264 if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
162265 /* The number of overflow pages to load for this (and therefore all
162266 ** subsequent) tokens is greater than the estimated number of pages
162267 ** that will be loaded if all subsequent tokens are deferred.
162268 */
162269 Fts3PhraseToken *pToken = pTC->pToken;
162270 rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
162271 fts3SegReaderCursorFree(pToken->pSegcsr);
162272 pToken->pSegcsr = 0;
162273 }else{
162274 /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
162275 ** for-loop. Except, limit the value to 2^24 to prevent it from
162276 ** overflowing the 32-bit integer it is stored in. */
162277 if( ii<12 ) nLoad4 = nLoad4*4;
162278
162279 if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
162280 /* Either this is the cheapest token in the entire query, or it is
162281 ** part of a multi-token phrase. Either way, the entire doclist will
162282 ** (eventually) be loaded into memory. It may as well be now. */
162283 Fts3PhraseToken *pToken = pTC->pToken;
162284 int nList = 0;
162285 char *pList = 0;
162286 rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
162287 assert( rc==SQLITE_OK || pList==0 );
162288 if( rc==SQLITE_OK ){
162289 rc = fts3EvalPhraseMergeToken(
162290 pTab, pTC->pPhrase, pTC->iToken,pList,nList
162291 );
162292 }
162293 if( rc==SQLITE_OK ){
162294 int nCount;
162295 nCount = fts3DoclistCountDocids(
162296 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
162297 );
162298 if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
162299 }
162300 }
162301 }
162302 pTC->pToken = 0;
162303 }
162304
162305 return rc;
162306}
162307
162308/*
162309** This function is called from within the xFilter method. It initializes
162310** the full-text query currently stored in pCsr->pExpr. To iterate through
162311** the results of a query, the caller does:
162312**
162313** fts3EvalStart(pCsr);
162314** while( 1 ){
162315** fts3EvalNext(pCsr);
162316** if( pCsr->bEof ) break;
162317** ... return row pCsr->iPrevId to the caller ...
162318** }
162319*/
162320static int fts3EvalStart(Fts3Cursor *pCsr){
162321 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
162322 int rc = SQLITE_OK;
162323 int nToken = 0;
162324 int nOr = 0;
162325
162326 /* Allocate a MultiSegReader for each token in the expression. */
162327 fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
162328
162329 /* Determine which, if any, tokens in the expression should be deferred. */
162330#ifndef SQLITE_DISABLE_FTS4_DEFERRED
162331 if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
162332 Fts3TokenAndCost *aTC;
162333 Fts3Expr **apOr;
162334 aTC = (Fts3TokenAndCost *)sqlite3_malloc(
162335 sizeof(Fts3TokenAndCost) * nToken
162336 + sizeof(Fts3Expr *) * nOr * 2
162337 );
162338 apOr = (Fts3Expr **)&aTC[nToken];
162339
162340 if( !aTC ){
162341 rc = SQLITE_NOMEM;
162342 }else{
162343 int ii;
162344 Fts3TokenAndCost *pTC = aTC;
162345 Fts3Expr **ppOr = apOr;
162346
162347 fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
162348 nToken = (int)(pTC-aTC);
162349 nOr = (int)(ppOr-apOr);
162350
162351 if( rc==SQLITE_OK ){
162352 rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
162353 for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
162354 rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
162355 }
162356 }
162357
162358 sqlite3_free(aTC);
162359 }
162360 }
162361#endif
162362
162363 fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
162364 return rc;
162365}
162366
162367/*
162368** Invalidate the current position list for phrase pPhrase.
162369*/
162370static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
162371 if( pPhrase->doclist.bFreeList ){
162372 sqlite3_free(pPhrase->doclist.pList);
162373 }
162374 pPhrase->doclist.pList = 0;
162375 pPhrase->doclist.nList = 0;
162376 pPhrase->doclist.bFreeList = 0;
162377}
162378
162379/*
162380** This function is called to edit the position list associated with
162381** the phrase object passed as the fifth argument according to a NEAR
162382** condition. For example:
162383**
162384** abc NEAR/5 "def ghi"
162385**
162386** Parameter nNear is passed the NEAR distance of the expression (5 in
162387** the example above). When this function is called, *paPoslist points to
162388** the position list, and *pnToken is the number of phrase tokens in, the
162389** phrase on the other side of the NEAR operator to pPhrase. For example,
162390** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
162391** the position list associated with phrase "abc".
162392**
162393** All positions in the pPhrase position list that are not sufficiently
162394** close to a position in the *paPoslist position list are removed. If this
162395** leaves 0 positions, zero is returned. Otherwise, non-zero.
162396**
162397** Before returning, *paPoslist is set to point to the position lsit
162398** associated with pPhrase. And *pnToken is set to the number of tokens in
162399** pPhrase.
162400*/
162401static int fts3EvalNearTrim(
162402 int nNear, /* NEAR distance. As in "NEAR/nNear". */
162403 char *aTmp, /* Temporary space to use */
162404 char **paPoslist, /* IN/OUT: Position list */
162405 int *pnToken, /* IN/OUT: Tokens in phrase of *paPoslist */
162406 Fts3Phrase *pPhrase /* The phrase object to trim the doclist of */
162407){
162408 int nParam1 = nNear + pPhrase->nToken;
162409 int nParam2 = nNear + *pnToken;
162410 int nNew;
162411 char *p2;
162412 char *pOut;
162413 int res;
162414
162415 assert( pPhrase->doclist.pList );
162416
162417 p2 = pOut = pPhrase->doclist.pList;
162418 res = fts3PoslistNearMerge(
162419 &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
162420 );
162421 if( res ){
162422 nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
162423 assert( pPhrase->doclist.pList[nNew]=='\0' );
162424 assert( nNew<=pPhrase->doclist.nList && nNew>0 );
162425 memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
162426 pPhrase->doclist.nList = nNew;
162427 *paPoslist = pPhrase->doclist.pList;
162428 *pnToken = pPhrase->nToken;
162429 }
162430
162431 return res;
162432}
162433
162434/*
162435** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
162436** Otherwise, it advances the expression passed as the second argument to
162437** point to the next matching row in the database. Expressions iterate through
162438** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
162439** or descending if it is non-zero.
162440**
162441** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
162442** successful, the following variables in pExpr are set:
162443**
162444** Fts3Expr.bEof (non-zero if EOF - there is no next row)
162445** Fts3Expr.iDocid (valid if bEof==0. The docid of the next row)
162446**
162447** If the expression is of type FTSQUERY_PHRASE, and the expression is not
162448** at EOF, then the following variables are populated with the position list
162449** for the phrase for the visited row:
162450**
162451** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
162452** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
162453**
162454** It says above that this function advances the expression to the next
162455** matching row. This is usually true, but there are the following exceptions:
162456**
162457** 1. Deferred tokens are not taken into account. If a phrase consists
162458** entirely of deferred tokens, it is assumed to match every row in
162459** the db. In this case the position-list is not populated at all.
162460**
162461** Or, if a phrase contains one or more deferred tokens and one or
162462** more non-deferred tokens, then the expression is advanced to the
162463** next possible match, considering only non-deferred tokens. In other
162464** words, if the phrase is "A B C", and "B" is deferred, the expression
162465** is advanced to the next row that contains an instance of "A * C",
162466** where "*" may match any single token. The position list in this case
162467** is populated as for "A * C" before returning.
162468**
162469** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
162470** advanced to point to the next row that matches "x AND y".
162471**
162472** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
162473** really a match, taking into account deferred tokens and NEAR operators.
162474*/
162475static void fts3EvalNextRow(
162476 Fts3Cursor *pCsr, /* FTS Cursor handle */
162477 Fts3Expr *pExpr, /* Expr. to advance to next matching row */
162478 int *pRc /* IN/OUT: Error code */
162479){
162480 if( *pRc==SQLITE_OK ){
162481 int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
162482 assert( pExpr->bEof==0 );
162483 pExpr->bStart = 1;
162484
162485 switch( pExpr->eType ){
162486 case FTSQUERY_NEAR:
162487 case FTSQUERY_AND: {
162488 Fts3Expr *pLeft = pExpr->pLeft;
162489 Fts3Expr *pRight = pExpr->pRight;
162490 assert( !pLeft->bDeferred || !pRight->bDeferred );
162491
162492 if( pLeft->bDeferred ){
162493 /* LHS is entirely deferred. So we assume it matches every row.
162494 ** Advance the RHS iterator to find the next row visited. */
162495 fts3EvalNextRow(pCsr, pRight, pRc);
162496 pExpr->iDocid = pRight->iDocid;
162497 pExpr->bEof = pRight->bEof;
162498 }else if( pRight->bDeferred ){
162499 /* RHS is entirely deferred. So we assume it matches every row.
162500 ** Advance the LHS iterator to find the next row visited. */
162501 fts3EvalNextRow(pCsr, pLeft, pRc);
162502 pExpr->iDocid = pLeft->iDocid;
162503 pExpr->bEof = pLeft->bEof;
162504 }else{
162505 /* Neither the RHS or LHS are deferred. */
162506 fts3EvalNextRow(pCsr, pLeft, pRc);
162507 fts3EvalNextRow(pCsr, pRight, pRc);
162508 while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
162509 sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
162510 if( iDiff==0 ) break;
162511 if( iDiff<0 ){
162512 fts3EvalNextRow(pCsr, pLeft, pRc);
162513 }else{
162514 fts3EvalNextRow(pCsr, pRight, pRc);
162515 }
162516 }
162517 pExpr->iDocid = pLeft->iDocid;
162518 pExpr->bEof = (pLeft->bEof || pRight->bEof);
162519 if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){
162520 assert( pRight->eType==FTSQUERY_PHRASE );
162521 if( pRight->pPhrase->doclist.aAll ){
162522 Fts3Doclist *pDl = &pRight->pPhrase->doclist;
162523 while( *pRc==SQLITE_OK && pRight->bEof==0 ){
162524 memset(pDl->pList, 0, pDl->nList);
162525 fts3EvalNextRow(pCsr, pRight, pRc);
162526 }
162527 }
162528 if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
162529 Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
162530 while( *pRc==SQLITE_OK && pLeft->bEof==0 ){
162531 memset(pDl->pList, 0, pDl->nList);
162532 fts3EvalNextRow(pCsr, pLeft, pRc);
162533 }
162534 }
162535 }
162536 }
162537 break;
162538 }
162539
162540 case FTSQUERY_OR: {
162541 Fts3Expr *pLeft = pExpr->pLeft;
162542 Fts3Expr *pRight = pExpr->pRight;
162543 sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
162544
162545 assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
162546 assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
162547
162548 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
162549 fts3EvalNextRow(pCsr, pLeft, pRc);
162550 }else if( pLeft->bEof || iCmp>0 ){
162551 fts3EvalNextRow(pCsr, pRight, pRc);
162552 }else{
162553 fts3EvalNextRow(pCsr, pLeft, pRc);
162554 fts3EvalNextRow(pCsr, pRight, pRc);
162555 }
162556
162557 pExpr->bEof = (pLeft->bEof && pRight->bEof);
162558 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
162559 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
162560 pExpr->iDocid = pLeft->iDocid;
162561 }else{
162562 pExpr->iDocid = pRight->iDocid;
162563 }
162564
162565 break;
162566 }
162567
162568 case FTSQUERY_NOT: {
162569 Fts3Expr *pLeft = pExpr->pLeft;
162570 Fts3Expr *pRight = pExpr->pRight;
162571
162572 if( pRight->bStart==0 ){
162573 fts3EvalNextRow(pCsr, pRight, pRc);
162574 assert( *pRc!=SQLITE_OK || pRight->bStart );
162575 }
162576
162577 fts3EvalNextRow(pCsr, pLeft, pRc);
162578 if( pLeft->bEof==0 ){
162579 while( !*pRc
162580 && !pRight->bEof
162581 && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
162582 ){
162583 fts3EvalNextRow(pCsr, pRight, pRc);
162584 }
162585 }
162586 pExpr->iDocid = pLeft->iDocid;
162587 pExpr->bEof = pLeft->bEof;
162588 break;
162589 }
162590
162591 default: {
162592 Fts3Phrase *pPhrase = pExpr->pPhrase;
162593 fts3EvalInvalidatePoslist(pPhrase);
162594 *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
162595 pExpr->iDocid = pPhrase->doclist.iDocid;
162596 break;
162597 }
162598 }
162599 }
162600}
162601
162602/*
162603** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
162604** cluster, then this function returns 1 immediately.
162605**
162606** Otherwise, it checks if the current row really does match the NEAR
162607** expression, using the data currently stored in the position lists
162608** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
162609**
162610** If the current row is a match, the position list associated with each
162611** phrase in the NEAR expression is edited in place to contain only those
162612** phrase instances sufficiently close to their peers to satisfy all NEAR
162613** constraints. In this case it returns 1. If the NEAR expression does not
162614** match the current row, 0 is returned. The position lists may or may not
162615** be edited if 0 is returned.
162616*/
162617static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
162618 int res = 1;
162619
162620 /* The following block runs if pExpr is the root of a NEAR query.
162621 ** For example, the query:
162622 **
162623 ** "w" NEAR "x" NEAR "y" NEAR "z"
162624 **
162625 ** which is represented in tree form as:
162626 **
162627 ** |
162628 ** +--NEAR--+ <-- root of NEAR query
162629 ** | |
162630 ** +--NEAR--+ "z"
162631 ** | |
162632 ** +--NEAR--+ "y"
162633 ** | |
162634 ** "w" "x"
162635 **
162636 ** The right-hand child of a NEAR node is always a phrase. The
162637 ** left-hand child may be either a phrase or a NEAR node. There are
162638 ** no exceptions to this - it's the way the parser in fts3_expr.c works.
162639 */
162640 if( *pRc==SQLITE_OK
162641 && pExpr->eType==FTSQUERY_NEAR
162642 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
162643 ){
162644 Fts3Expr *p;
162645 int nTmp = 0; /* Bytes of temp space */
162646 char *aTmp; /* Temp space for PoslistNearMerge() */
162647
162648 /* Allocate temporary working space. */
162649 for(p=pExpr; p->pLeft; p=p->pLeft){
162650 assert( p->pRight->pPhrase->doclist.nList>0 );
162651 nTmp += p->pRight->pPhrase->doclist.nList;
162652 }
162653 nTmp += p->pPhrase->doclist.nList;
162654 aTmp = sqlite3_malloc(nTmp*2);
162655 if( !aTmp ){
162656 *pRc = SQLITE_NOMEM;
162657 res = 0;
162658 }else{
162659 char *aPoslist = p->pPhrase->doclist.pList;
162660 int nToken = p->pPhrase->nToken;
162661
162662 for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
162663 Fts3Phrase *pPhrase = p->pRight->pPhrase;
162664 int nNear = p->nNear;
162665 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
162666 }
162667
162668 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
162669 nToken = pExpr->pRight->pPhrase->nToken;
162670 for(p=pExpr->pLeft; p && res; p=p->pLeft){
162671 int nNear;
162672 Fts3Phrase *pPhrase;
162673 assert( p->pParent && p->pParent->pLeft==p );
162674 nNear = p->pParent->nNear;
162675 pPhrase = (
162676 p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
162677 );
162678 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
162679 }
162680 }
162681
162682 sqlite3_free(aTmp);
162683 }
162684
162685 return res;
162686}
162687
162688/*
162689** This function is a helper function for sqlite3Fts3EvalTestDeferred().
162690** Assuming no error occurs or has occurred, It returns non-zero if the
162691** expression passed as the second argument matches the row that pCsr
162692** currently points to, or zero if it does not.
162693**
162694** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
162695** If an error occurs during execution of this function, *pRc is set to
162696** the appropriate SQLite error code. In this case the returned value is
162697** undefined.
162698*/
162699static int fts3EvalTestExpr(
162700 Fts3Cursor *pCsr, /* FTS cursor handle */
162701 Fts3Expr *pExpr, /* Expr to test. May or may not be root. */
162702 int *pRc /* IN/OUT: Error code */
162703){
162704 int bHit = 1; /* Return value */
162705 if( *pRc==SQLITE_OK ){
162706 switch( pExpr->eType ){
162707 case FTSQUERY_NEAR:
162708 case FTSQUERY_AND:
162709 bHit = (
162710 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
162711 && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
162712 && fts3EvalNearTest(pExpr, pRc)
162713 );
162714
162715 /* If the NEAR expression does not match any rows, zero the doclist for
162716 ** all phrases involved in the NEAR. This is because the snippet(),
162717 ** offsets() and matchinfo() functions are not supposed to recognize
162718 ** any instances of phrases that are part of unmatched NEAR queries.
162719 ** For example if this expression:
162720 **
162721 ** ... MATCH 'a OR (b NEAR c)'
162722 **
162723 ** is matched against a row containing:
162724 **
162725 ** 'a b d e'
162726 **
162727 ** then any snippet() should ony highlight the "a" term, not the "b"
162728 ** (as "b" is part of a non-matching NEAR clause).
162729 */
162730 if( bHit==0
162731 && pExpr->eType==FTSQUERY_NEAR
162732 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
162733 ){
162734 Fts3Expr *p;
162735 for(p=pExpr; p->pPhrase==0; p=p->pLeft){
162736 if( p->pRight->iDocid==pCsr->iPrevId ){
162737 fts3EvalInvalidatePoslist(p->pRight->pPhrase);
162738 }
162739 }
162740 if( p->iDocid==pCsr->iPrevId ){
162741 fts3EvalInvalidatePoslist(p->pPhrase);
162742 }
162743 }
162744
162745 break;
162746
162747 case FTSQUERY_OR: {
162748 int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
162749 int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
162750 bHit = bHit1 || bHit2;
162751 break;
162752 }
162753
162754 case FTSQUERY_NOT:
162755 bHit = (
162756 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
162757 && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
162758 );
162759 break;
162760
162761 default: {
162762#ifndef SQLITE_DISABLE_FTS4_DEFERRED
162763 if( pCsr->pDeferred
162764 && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
162765 ){
162766 Fts3Phrase *pPhrase = pExpr->pPhrase;
162767 assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
162768 if( pExpr->bDeferred ){
162769 fts3EvalInvalidatePoslist(pPhrase);
162770 }
162771 *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
162772 bHit = (pPhrase->doclist.pList!=0);
162773 pExpr->iDocid = pCsr->iPrevId;
162774 }else
162775#endif
162776 {
162777 bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
162778 }
162779 break;
162780 }
162781 }
162782 }
162783 return bHit;
162784}
162785
162786/*
162787** This function is called as the second part of each xNext operation when
162788** iterating through the results of a full-text query. At this point the
162789** cursor points to a row that matches the query expression, with the
162790** following caveats:
162791**
162792** * Up until this point, "NEAR" operators in the expression have been
162793** treated as "AND".
162794**
162795** * Deferred tokens have not yet been considered.
162796**
162797** If *pRc is not SQLITE_OK when this function is called, it immediately
162798** returns 0. Otherwise, it tests whether or not after considering NEAR
162799** operators and deferred tokens the current row is still a match for the
162800** expression. It returns 1 if both of the following are true:
162801**
162802** 1. *pRc is SQLITE_OK when this function returns, and
162803**
162804** 2. After scanning the current FTS table row for the deferred tokens,
162805** it is determined that the row does *not* match the query.
162806**
162807** Or, if no error occurs and it seems the current row does match the FTS
162808** query, return 0.
162809*/
162810SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
162811 int rc = *pRc;
162812 int bMiss = 0;
162813 if( rc==SQLITE_OK ){
162814
162815 /* If there are one or more deferred tokens, load the current row into
162816 ** memory and scan it to determine the position list for each deferred
162817 ** token. Then, see if this row is really a match, considering deferred
162818 ** tokens and NEAR operators (neither of which were taken into account
162819 ** earlier, by fts3EvalNextRow()).
162820 */
162821 if( pCsr->pDeferred ){
162822 rc = fts3CursorSeek(0, pCsr);
162823 if( rc==SQLITE_OK ){
162824 rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
162825 }
162826 }
162827 bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
162828
162829 /* Free the position-lists accumulated for each deferred token above. */
162830 sqlite3Fts3FreeDeferredDoclists(pCsr);
162831 *pRc = rc;
162832 }
162833 return (rc==SQLITE_OK && bMiss);
162834}
162835
162836/*
162837** Advance to the next document that matches the FTS expression in
162838** Fts3Cursor.pExpr.
162839*/
162840static int fts3EvalNext(Fts3Cursor *pCsr){
162841 int rc = SQLITE_OK; /* Return Code */
162842 Fts3Expr *pExpr = pCsr->pExpr;
162843 assert( pCsr->isEof==0 );
162844 if( pExpr==0 ){
162845 pCsr->isEof = 1;
162846 }else{
162847 do {
162848 if( pCsr->isRequireSeek==0 ){
162849 sqlite3_reset(pCsr->pStmt);
162850 }
162851 assert( sqlite3_data_count(pCsr->pStmt)==0 );
162852 fts3EvalNextRow(pCsr, pExpr, &rc);
162853 pCsr->isEof = pExpr->bEof;
162854 pCsr->isRequireSeek = 1;
162855 pCsr->isMatchinfoNeeded = 1;
162856 pCsr->iPrevId = pExpr->iDocid;
162857 }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
162858 }
162859
162860 /* Check if the cursor is past the end of the docid range specified
162861 ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */
162862 if( rc==SQLITE_OK && (
162863 (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
162864 || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
162865 )){
162866 pCsr->isEof = 1;
162867 }
162868
162869 return rc;
162870}
162871
162872/*
162873** Restart interation for expression pExpr so that the next call to
162874** fts3EvalNext() visits the first row. Do not allow incremental
162875** loading or merging of phrase doclists for this iteration.
162876**
162877** If *pRc is other than SQLITE_OK when this function is called, it is
162878** a no-op. If an error occurs within this function, *pRc is set to an
162879** SQLite error code before returning.
162880*/
162881static void fts3EvalRestart(
162882 Fts3Cursor *pCsr,
162883 Fts3Expr *pExpr,
162884 int *pRc
162885){
162886 if( pExpr && *pRc==SQLITE_OK ){
162887 Fts3Phrase *pPhrase = pExpr->pPhrase;
162888
162889 if( pPhrase ){
162890 fts3EvalInvalidatePoslist(pPhrase);
162891 if( pPhrase->bIncr ){
162892 int i;
162893 for(i=0; i<pPhrase->nToken; i++){
162894 Fts3PhraseToken *pToken = &pPhrase->aToken[i];
162895 assert( pToken->pDeferred==0 );
162896 if( pToken->pSegcsr ){
162897 sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
162898 }
162899 }
162900 *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
162901 }
162902 pPhrase->doclist.pNextDocid = 0;
162903 pPhrase->doclist.iDocid = 0;
162904 pPhrase->pOrPoslist = 0;
162905 }
162906
162907 pExpr->iDocid = 0;
162908 pExpr->bEof = 0;
162909 pExpr->bStart = 0;
162910
162911 fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
162912 fts3EvalRestart(pCsr, pExpr->pRight, pRc);
162913 }
162914}
162915
162916/*
162917** After allocating the Fts3Expr.aMI[] array for each phrase in the
162918** expression rooted at pExpr, the cursor iterates through all rows matched
162919** by pExpr, calling this function for each row. This function increments
162920** the values in Fts3Expr.aMI[] according to the position-list currently
162921** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
162922** expression nodes.
162923*/
162924static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
162925 if( pExpr ){
162926 Fts3Phrase *pPhrase = pExpr->pPhrase;
162927 if( pPhrase && pPhrase->doclist.pList ){
162928 int iCol = 0;
162929 char *p = pPhrase->doclist.pList;
162930
162931 assert( *p );
162932 while( 1 ){
162933 u8 c = 0;
162934 int iCnt = 0;
162935 while( 0xFE & (*p | c) ){
162936 if( (c&0x80)==0 ) iCnt++;
162937 c = *p++ & 0x80;
162938 }
162939
162940 /* aMI[iCol*3 + 1] = Number of occurrences
162941 ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
162942 */
162943 pExpr->aMI[iCol*3 + 1] += iCnt;
162944 pExpr->aMI[iCol*3 + 2] += (iCnt>0);
162945 if( *p==0x00 ) break;
162946 p++;
162947 p += fts3GetVarint32(p, &iCol);
162948 }
162949 }
162950
162951 fts3EvalUpdateCounts(pExpr->pLeft);
162952 fts3EvalUpdateCounts(pExpr->pRight);
162953 }
162954}
162955
162956/*
162957** Expression pExpr must be of type FTSQUERY_PHRASE.
162958**
162959** If it is not already allocated and populated, this function allocates and
162960** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
162961** of a NEAR expression, then it also allocates and populates the same array
162962** for all other phrases that are part of the NEAR expression.
162963**
162964** SQLITE_OK is returned if the aMI[] array is successfully allocated and
162965** populated. Otherwise, if an error occurs, an SQLite error code is returned.
162966*/
162967static int fts3EvalGatherStats(
162968 Fts3Cursor *pCsr, /* Cursor object */
162969 Fts3Expr *pExpr /* FTSQUERY_PHRASE expression */
162970){
162971 int rc = SQLITE_OK; /* Return code */
162972
162973 assert( pExpr->eType==FTSQUERY_PHRASE );
162974 if( pExpr->aMI==0 ){
162975 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
162976 Fts3Expr *pRoot; /* Root of NEAR expression */
162977 Fts3Expr *p; /* Iterator used for several purposes */
162978
162979 sqlite3_int64 iPrevId = pCsr->iPrevId;
162980 sqlite3_int64 iDocid;
162981 u8 bEof;
162982
162983 /* Find the root of the NEAR expression */
162984 pRoot = pExpr;
162985 while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
162986 pRoot = pRoot->pParent;
162987 }
162988 iDocid = pRoot->iDocid;
162989 bEof = pRoot->bEof;
162990 assert( pRoot->bStart );
162991
162992 /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
162993 for(p=pRoot; p; p=p->pLeft){
162994 Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
162995 assert( pE->aMI==0 );
162996 pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
162997 if( !pE->aMI ) return SQLITE_NOMEM;
162998 memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
162999 }
163000
163001 fts3EvalRestart(pCsr, pRoot, &rc);
163002
163003 while( pCsr->isEof==0 && rc==SQLITE_OK ){
163004
163005 do {
163006 /* Ensure the %_content statement is reset. */
163007 if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
163008 assert( sqlite3_data_count(pCsr->pStmt)==0 );
163009
163010 /* Advance to the next document */
163011 fts3EvalNextRow(pCsr, pRoot, &rc);
163012 pCsr->isEof = pRoot->bEof;
163013 pCsr->isRequireSeek = 1;
163014 pCsr->isMatchinfoNeeded = 1;
163015 pCsr->iPrevId = pRoot->iDocid;
163016 }while( pCsr->isEof==0
163017 && pRoot->eType==FTSQUERY_NEAR
163018 && sqlite3Fts3EvalTestDeferred(pCsr, &rc)
163019 );
163020
163021 if( rc==SQLITE_OK && pCsr->isEof==0 ){
163022 fts3EvalUpdateCounts(pRoot);
163023 }
163024 }
163025
163026 pCsr->isEof = 0;
163027 pCsr->iPrevId = iPrevId;
163028
163029 if( bEof ){
163030 pRoot->bEof = bEof;
163031 }else{
163032 /* Caution: pRoot may iterate through docids in ascending or descending
163033 ** order. For this reason, even though it seems more defensive, the
163034 ** do loop can not be written:
163035 **
163036 ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
163037 */
163038 fts3EvalRestart(pCsr, pRoot, &rc);
163039 do {
163040 fts3EvalNextRow(pCsr, pRoot, &rc);
163041 assert( pRoot->bEof==0 );
163042 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
163043 }
163044 }
163045 return rc;
163046}
163047
163048/*
163049** This function is used by the matchinfo() module to query a phrase
163050** expression node for the following information:
163051**
163052** 1. The total number of occurrences of the phrase in each column of
163053** the FTS table (considering all rows), and
163054**
163055** 2. For each column, the number of rows in the table for which the
163056** column contains at least one instance of the phrase.
163057**
163058** If no error occurs, SQLITE_OK is returned and the values for each column
163059** written into the array aiOut as follows:
163060**
163061** aiOut[iCol*3 + 1] = Number of occurrences
163062** aiOut[iCol*3 + 2] = Number of rows containing at least one instance
163063**
163064** Caveats:
163065**
163066** * If a phrase consists entirely of deferred tokens, then all output
163067** values are set to the number of documents in the table. In other
163068** words we assume that very common tokens occur exactly once in each
163069** column of each row of the table.
163070**
163071** * If a phrase contains some deferred tokens (and some non-deferred
163072** tokens), count the potential occurrence identified by considering
163073** the non-deferred tokens instead of actual phrase occurrences.
163074**
163075** * If the phrase is part of a NEAR expression, then only phrase instances
163076** that meet the NEAR constraint are included in the counts.
163077*/
163078SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
163079 Fts3Cursor *pCsr, /* FTS cursor handle */
163080 Fts3Expr *pExpr, /* Phrase expression */
163081 u32 *aiOut /* Array to write results into (see above) */
163082){
163083 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
163084 int rc = SQLITE_OK;
163085 int iCol;
163086
163087 if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
163088 assert( pCsr->nDoc>0 );
163089 for(iCol=0; iCol<pTab->nColumn; iCol++){
163090 aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
163091 aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
163092 }
163093 }else{
163094 rc = fts3EvalGatherStats(pCsr, pExpr);
163095 if( rc==SQLITE_OK ){
163096 assert( pExpr->aMI );
163097 for(iCol=0; iCol<pTab->nColumn; iCol++){
163098 aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
163099 aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
163100 }
163101 }
163102 }
163103
163104 return rc;
163105}
163106
163107/*
163108** The expression pExpr passed as the second argument to this function
163109** must be of type FTSQUERY_PHRASE.
163110**
163111** The returned value is either NULL or a pointer to a buffer containing
163112** a position-list indicating the occurrences of the phrase in column iCol
163113** of the current row.
163114**
163115** More specifically, the returned buffer contains 1 varint for each
163116** occurrence of the phrase in the column, stored using the normal (delta+2)
163117** compression and is terminated by either an 0x01 or 0x00 byte. For example,
163118** if the requested column contains "a b X c d X X" and the position-list
163119** for 'X' is requested, the buffer returned may contain:
163120**
163121** 0x04 0x05 0x03 0x01 or 0x04 0x05 0x03 0x00
163122**
163123** This function works regardless of whether or not the phrase is deferred,
163124** incremental, or neither.
163125*/
163126SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
163127 Fts3Cursor *pCsr, /* FTS3 cursor object */
163128 Fts3Expr *pExpr, /* Phrase to return doclist for */
163129 int iCol, /* Column to return position list for */
163130 char **ppOut /* OUT: Pointer to position list */
163131){
163132 Fts3Phrase *pPhrase = pExpr->pPhrase;
163133 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
163134 char *pIter;
163135 int iThis;
163136 sqlite3_int64 iDocid;
163137
163138 /* If this phrase is applies specifically to some column other than
163139 ** column iCol, return a NULL pointer. */
163140 *ppOut = 0;
163141 assert( iCol>=0 && iCol<pTab->nColumn );
163142 if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
163143 return SQLITE_OK;
163144 }
163145
163146 iDocid = pExpr->iDocid;
163147 pIter = pPhrase->doclist.pList;
163148 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
163149 int rc = SQLITE_OK;
163150 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
163151 int bOr = 0;
163152 u8 bTreeEof = 0;
163153 Fts3Expr *p; /* Used to iterate from pExpr to root */
163154 Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */
163155 int bMatch;
163156
163157 /* Check if this phrase descends from an OR expression node. If not,
163158 ** return NULL. Otherwise, the entry that corresponds to docid
163159 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
163160 ** tree that the node is part of has been marked as EOF, but the node
163161 ** itself is not EOF, then it may point to an earlier entry. */
163162 pNear = pExpr;
163163 for(p=pExpr->pParent; p; p=p->pParent){
163164 if( p->eType==FTSQUERY_OR ) bOr = 1;
163165 if( p->eType==FTSQUERY_NEAR ) pNear = p;
163166 if( p->bEof ) bTreeEof = 1;
163167 }
163168 if( bOr==0 ) return SQLITE_OK;
163169
163170 /* This is the descendent of an OR node. In this case we cannot use
163171 ** an incremental phrase. Load the entire doclist for the phrase
163172 ** into memory in this case. */
163173 if( pPhrase->bIncr ){
163174 int bEofSave = pNear->bEof;
163175 fts3EvalRestart(pCsr, pNear, &rc);
163176 while( rc==SQLITE_OK && !pNear->bEof ){
163177 fts3EvalNextRow(pCsr, pNear, &rc);
163178 if( bEofSave==0 && pNear->iDocid==iDocid ) break;
163179 }
163180 assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
163181 }
163182 if( bTreeEof ){
163183 while( rc==SQLITE_OK && !pNear->bEof ){
163184 fts3EvalNextRow(pCsr, pNear, &rc);
163185 }
163186 }
163187 if( rc!=SQLITE_OK ) return rc;
163188
163189 bMatch = 1;
163190 for(p=pNear; p; p=p->pLeft){
163191 u8 bEof = 0;
163192 Fts3Expr *pTest = p;
163193 Fts3Phrase *pPh;
163194 assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
163195 if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
163196 assert( pTest->eType==FTSQUERY_PHRASE );
163197 pPh = pTest->pPhrase;
163198
163199 pIter = pPh->pOrPoslist;
163200 iDocid = pPh->iOrDocid;
163201 if( pCsr->bDesc==bDescDoclist ){
163202 bEof = !pPh->doclist.nAll ||
163203 (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
163204 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
163205 sqlite3Fts3DoclistNext(
163206 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
163207 &pIter, &iDocid, &bEof
163208 );
163209 }
163210 }else{
163211 bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
163212 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
163213 int dummy;
163214 sqlite3Fts3DoclistPrev(
163215 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
163216 &pIter, &iDocid, &dummy, &bEof
163217 );
163218 }
163219 }
163220 pPh->pOrPoslist = pIter;
163221 pPh->iOrDocid = iDocid;
163222 if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
163223 }
163224
163225 if( bMatch ){
163226 pIter = pPhrase->pOrPoslist;
163227 }else{
163228 pIter = 0;
163229 }
163230 }
163231 if( pIter==0 ) return SQLITE_OK;
163232
163233 if( *pIter==0x01 ){
163234 pIter++;
163235 pIter += fts3GetVarint32(pIter, &iThis);
163236 }else{
163237 iThis = 0;
163238 }
163239 while( iThis<iCol ){
163240 fts3ColumnlistCopy(0, &pIter);
163241 if( *pIter==0x00 ) return SQLITE_OK;
163242 pIter++;
163243 pIter += fts3GetVarint32(pIter, &iThis);
163244 }
163245 if( *pIter==0x00 ){
163246 pIter = 0;
163247 }
163248
163249 *ppOut = ((iCol==iThis)?pIter:0);
163250 return SQLITE_OK;
163251}
163252
163253/*
163254** Free all components of the Fts3Phrase structure that were allocated by
163255** the eval module. Specifically, this means to free:
163256**
163257** * the contents of pPhrase->doclist, and
163258** * any Fts3MultiSegReader objects held by phrase tokens.
163259*/
163260SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
163261 if( pPhrase ){
163262 int i;
163263 sqlite3_free(pPhrase->doclist.aAll);
163264 fts3EvalInvalidatePoslist(pPhrase);
163265 memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
163266 for(i=0; i<pPhrase->nToken; i++){
163267 fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
163268 pPhrase->aToken[i].pSegcsr = 0;
163269 }
163270 }
163271}
163272
163273
163274/*
163275** Return SQLITE_CORRUPT_VTAB.
163276*/
163277#ifdef SQLITE_DEBUG
163278SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
163279 return SQLITE_CORRUPT_VTAB;
163280}
163281#endif
163282
163283#if !SQLITE_CORE
163284/*
163285** Initialize API pointer table, if required.
163286*/
163287#ifdef _WIN32
163288__declspec(dllexport)
163289#endif
163290SQLITE_API int sqlite3_fts3_init(
163291 sqlite3 *db,
163292 char **pzErrMsg,
163293 const sqlite3_api_routines *pApi
163294){
163295 SQLITE_EXTENSION_INIT2(pApi)
163296 return sqlite3Fts3Init(db);
163297}
163298#endif
163299
163300#endif
163301
163302/************** End of fts3.c ************************************************/
163303/************** Begin file fts3_aux.c ****************************************/
163304/*
163305** 2011 Jan 27
163306**
163307** The author disclaims copyright to this source code. In place of
163308** a legal notice, here is a blessing:
163309**
163310** May you do good and not evil.
163311** May you find forgiveness for yourself and forgive others.
163312** May you share freely, never taking more than you give.
163313**
163314******************************************************************************
163315**
163316*/
163317/* #include "fts3Int.h" */
163318#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
163319
163320/* #include <string.h> */
163321/* #include <assert.h> */
163322
163323typedef struct Fts3auxTable Fts3auxTable;
163324typedef struct Fts3auxCursor Fts3auxCursor;
163325
163326struct Fts3auxTable {
163327 sqlite3_vtab base; /* Base class used by SQLite core */
163328 Fts3Table *pFts3Tab;
163329};
163330
163331struct Fts3auxCursor {
163332 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
163333 Fts3MultiSegReader csr; /* Must be right after "base" */
163334 Fts3SegFilter filter;
163335 char *zStop;
163336 int nStop; /* Byte-length of string zStop */
163337 int iLangid; /* Language id to query */
163338 int isEof; /* True if cursor is at EOF */
163339 sqlite3_int64 iRowid; /* Current rowid */
163340
163341 int iCol; /* Current value of 'col' column */
163342 int nStat; /* Size of aStat[] array */
163343 struct Fts3auxColstats {
163344 sqlite3_int64 nDoc; /* 'documents' values for current csr row */
163345 sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */
163346 } *aStat;
163347};
163348
163349/*
163350** Schema of the terms table.
163351*/
163352#define FTS3_AUX_SCHEMA \
163353 "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
163354
163355/*
163356** This function does all the work for both the xConnect and xCreate methods.
163357** These tables have no persistent representation of their own, so xConnect
163358** and xCreate are identical operations.
163359*/
163360static int fts3auxConnectMethod(
163361 sqlite3 *db, /* Database connection */
163362 void *pUnused, /* Unused */
163363 int argc, /* Number of elements in argv array */
163364 const char * const *argv, /* xCreate/xConnect argument array */
163365 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
163366 char **pzErr /* OUT: sqlite3_malloc'd error message */
163367){
163368 char const *zDb; /* Name of database (e.g. "main") */
163369 char const *zFts3; /* Name of fts3 table */
163370 int nDb; /* Result of strlen(zDb) */
163371 int nFts3; /* Result of strlen(zFts3) */
163372 int nByte; /* Bytes of space to allocate here */
163373 int rc; /* value returned by declare_vtab() */
163374 Fts3auxTable *p; /* Virtual table object to return */
163375
163376 UNUSED_PARAMETER(pUnused);
163377
163378 /* The user should invoke this in one of two forms:
163379 **
163380 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
163381 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
163382 */
163383 if( argc!=4 && argc!=5 ) goto bad_args;
163384
163385 zDb = argv[1];
163386 nDb = (int)strlen(zDb);
163387 if( argc==5 ){
163388 if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
163389 zDb = argv[3];
163390 nDb = (int)strlen(zDb);
163391 zFts3 = argv[4];
163392 }else{
163393 goto bad_args;
163394 }
163395 }else{
163396 zFts3 = argv[3];
163397 }
163398 nFts3 = (int)strlen(zFts3);
163399
163400 rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA);
163401 if( rc!=SQLITE_OK ) return rc;
163402
163403 nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
163404 p = (Fts3auxTable *)sqlite3_malloc(nByte);
163405 if( !p ) return SQLITE_NOMEM;
163406 memset(p, 0, nByte);
163407
163408 p->pFts3Tab = (Fts3Table *)&p[1];
163409 p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
163410 p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
163411 p->pFts3Tab->db = db;
163412 p->pFts3Tab->nIndex = 1;
163413
163414 memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
163415 memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
163416 sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
163417
163418 *ppVtab = (sqlite3_vtab *)p;
163419 return SQLITE_OK;
163420
163421 bad_args:
163422 sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor");
163423 return SQLITE_ERROR;
163424}
163425
163426/*
163427** This function does the work for both the xDisconnect and xDestroy methods.
163428** These tables have no persistent representation of their own, so xDisconnect
163429** and xDestroy are identical operations.
163430*/
163431static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
163432 Fts3auxTable *p = (Fts3auxTable *)pVtab;
163433 Fts3Table *pFts3 = p->pFts3Tab;
163434 int i;
163435
163436 /* Free any prepared statements held */
163437 for(i=0; i<SizeofArray(pFts3->aStmt); i++){
163438 sqlite3_finalize(pFts3->aStmt[i]);
163439 }
163440 sqlite3_free(pFts3->zSegmentsTbl);
163441 sqlite3_free(p);
163442 return SQLITE_OK;
163443}
163444
163445#define FTS4AUX_EQ_CONSTRAINT 1
163446#define FTS4AUX_GE_CONSTRAINT 2
163447#define FTS4AUX_LE_CONSTRAINT 4
163448
163449/*
163450** xBestIndex - Analyze a WHERE and ORDER BY clause.
163451*/
163452static int fts3auxBestIndexMethod(
163453 sqlite3_vtab *pVTab,
163454 sqlite3_index_info *pInfo
163455){
163456 int i;
163457 int iEq = -1;
163458 int iGe = -1;
163459 int iLe = -1;
163460 int iLangid = -1;
163461 int iNext = 1; /* Next free argvIndex value */
163462
163463 UNUSED_PARAMETER(pVTab);
163464
163465 /* This vtab delivers always results in "ORDER BY term ASC" order. */
163466 if( pInfo->nOrderBy==1
163467 && pInfo->aOrderBy[0].iColumn==0
163468 && pInfo->aOrderBy[0].desc==0
163469 ){
163470 pInfo->orderByConsumed = 1;
163471 }
163472
163473 /* Search for equality and range constraints on the "term" column.
163474 ** And equality constraints on the hidden "languageid" column. */
163475 for(i=0; i<pInfo->nConstraint; i++){
163476 if( pInfo->aConstraint[i].usable ){
163477 int op = pInfo->aConstraint[i].op;
163478 int iCol = pInfo->aConstraint[i].iColumn;
163479
163480 if( iCol==0 ){
163481 if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
163482 if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
163483 if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
163484 if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
163485 if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
163486 }
163487 if( iCol==4 ){
163488 if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iLangid = i;
163489 }
163490 }
163491 }
163492
163493 if( iEq>=0 ){
163494 pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
163495 pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
163496 pInfo->estimatedCost = 5;
163497 }else{
163498 pInfo->idxNum = 0;
163499 pInfo->estimatedCost = 20000;
163500 if( iGe>=0 ){
163501 pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
163502 pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
163503 pInfo->estimatedCost /= 2;
163504 }
163505 if( iLe>=0 ){
163506 pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
163507 pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
163508 pInfo->estimatedCost /= 2;
163509 }
163510 }
163511 if( iLangid>=0 ){
163512 pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
163513 pInfo->estimatedCost--;
163514 }
163515
163516 return SQLITE_OK;
163517}
163518
163519/*
163520** xOpen - Open a cursor.
163521*/
163522static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
163523 Fts3auxCursor *pCsr; /* Pointer to cursor object to return */
163524
163525 UNUSED_PARAMETER(pVTab);
163526
163527 pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
163528 if( !pCsr ) return SQLITE_NOMEM;
163529 memset(pCsr, 0, sizeof(Fts3auxCursor));
163530
163531 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
163532 return SQLITE_OK;
163533}
163534
163535/*
163536** xClose - Close a cursor.
163537*/
163538static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
163539 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
163540 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
163541
163542 sqlite3Fts3SegmentsClose(pFts3);
163543 sqlite3Fts3SegReaderFinish(&pCsr->csr);
163544 sqlite3_free((void *)pCsr->filter.zTerm);
163545 sqlite3_free(pCsr->zStop);
163546 sqlite3_free(pCsr->aStat);
163547 sqlite3_free(pCsr);
163548 return SQLITE_OK;
163549}
163550
163551static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
163552 if( nSize>pCsr->nStat ){
163553 struct Fts3auxColstats *aNew;
163554 aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat,
163555 sizeof(struct Fts3auxColstats) * nSize
163556 );
163557 if( aNew==0 ) return SQLITE_NOMEM;
163558 memset(&aNew[pCsr->nStat], 0,
163559 sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
163560 );
163561 pCsr->aStat = aNew;
163562 pCsr->nStat = nSize;
163563 }
163564 return SQLITE_OK;
163565}
163566
163567/*
163568** xNext - Advance the cursor to the next row, if any.
163569*/
163570static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
163571 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
163572 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
163573 int rc;
163574
163575 /* Increment our pretend rowid value. */
163576 pCsr->iRowid++;
163577
163578 for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
163579 if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
163580 }
163581
163582 rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
163583 if( rc==SQLITE_ROW ){
163584 int i = 0;
163585 int nDoclist = pCsr->csr.nDoclist;
163586 char *aDoclist = pCsr->csr.aDoclist;
163587 int iCol;
163588
163589 int eState = 0;
163590
163591 if( pCsr->zStop ){
163592 int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
163593 int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
163594 if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
163595 pCsr->isEof = 1;
163596 return SQLITE_OK;
163597 }
163598 }
163599
163600 if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
163601 memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
163602 iCol = 0;
163603
163604 while( i<nDoclist ){
163605 sqlite3_int64 v = 0;
163606
163607 i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
163608 switch( eState ){
163609 /* State 0. In this state the integer just read was a docid. */
163610 case 0:
163611 pCsr->aStat[0].nDoc++;
163612 eState = 1;
163613 iCol = 0;
163614 break;
163615
163616 /* State 1. In this state we are expecting either a 1, indicating
163617 ** that the following integer will be a column number, or the
163618 ** start of a position list for column 0.
163619 **
163620 ** The only difference between state 1 and state 2 is that if the
163621 ** integer encountered in state 1 is not 0 or 1, then we need to
163622 ** increment the column 0 "nDoc" count for this term.
163623 */
163624 case 1:
163625 assert( iCol==0 );
163626 if( v>1 ){
163627 pCsr->aStat[1].nDoc++;
163628 }
163629 eState = 2;
163630 /* fall through */
163631
163632 case 2:
163633 if( v==0 ){ /* 0x00. Next integer will be a docid. */
163634 eState = 0;
163635 }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
163636 eState = 3;
163637 }else{ /* 2 or greater. A position. */
163638 pCsr->aStat[iCol+1].nOcc++;
163639 pCsr->aStat[0].nOcc++;
163640 }
163641 break;
163642
163643 /* State 3. The integer just read is a column number. */
163644 default: assert( eState==3 );
163645 iCol = (int)v;
163646 if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
163647 pCsr->aStat[iCol+1].nDoc++;
163648 eState = 2;
163649 break;
163650 }
163651 }
163652
163653 pCsr->iCol = 0;
163654 rc = SQLITE_OK;
163655 }else{
163656 pCsr->isEof = 1;
163657 }
163658 return rc;
163659}
163660
163661/*
163662** xFilter - Initialize a cursor to point at the start of its data.
163663*/
163664static int fts3auxFilterMethod(
163665 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
163666 int idxNum, /* Strategy index */
163667 const char *idxStr, /* Unused */
163668 int nVal, /* Number of elements in apVal */
163669 sqlite3_value **apVal /* Arguments for the indexing scheme */
163670){
163671 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
163672 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
163673 int rc;
163674 int isScan = 0;
163675 int iLangVal = 0; /* Language id to query */
163676
163677 int iEq = -1; /* Index of term=? value in apVal */
163678 int iGe = -1; /* Index of term>=? value in apVal */
163679 int iLe = -1; /* Index of term<=? value in apVal */
163680 int iLangid = -1; /* Index of languageid=? value in apVal */
163681 int iNext = 0;
163682
163683 UNUSED_PARAMETER(nVal);
163684 UNUSED_PARAMETER(idxStr);
163685
163686 assert( idxStr==0 );
163687 assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
163688 || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
163689 || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
163690 );
163691
163692 if( idxNum==FTS4AUX_EQ_CONSTRAINT ){
163693 iEq = iNext++;
163694 }else{
163695 isScan = 1;
163696 if( idxNum & FTS4AUX_GE_CONSTRAINT ){
163697 iGe = iNext++;
163698 }
163699 if( idxNum & FTS4AUX_LE_CONSTRAINT ){
163700 iLe = iNext++;
163701 }
163702 }
163703 if( iNext<nVal ){
163704 iLangid = iNext++;
163705 }
163706
163707 /* In case this cursor is being reused, close and zero it. */
163708 testcase(pCsr->filter.zTerm);
163709 sqlite3Fts3SegReaderFinish(&pCsr->csr);
163710 sqlite3_free((void *)pCsr->filter.zTerm);
163711 sqlite3_free(pCsr->aStat);
163712 memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
163713
163714 pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
163715 if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
163716
163717 if( iEq>=0 || iGe>=0 ){
163718 const unsigned char *zStr = sqlite3_value_text(apVal[0]);
163719 assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
163720 if( zStr ){
163721 pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
163722 pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
163723 if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
163724 }
163725 }
163726
163727 if( iLe>=0 ){
163728 pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
163729 pCsr->nStop = sqlite3_value_bytes(apVal[iLe]);
163730 if( pCsr->zStop==0 ) return SQLITE_NOMEM;
163731 }
163732
163733 if( iLangid>=0 ){
163734 iLangVal = sqlite3_value_int(apVal[iLangid]);
163735
163736 /* If the user specified a negative value for the languageid, use zero
163737 ** instead. This works, as the "languageid=?" constraint will also
163738 ** be tested by the VDBE layer. The test will always be false (since
163739 ** this module will not return a row with a negative languageid), and
163740 ** so the overall query will return zero rows. */
163741 if( iLangVal<0 ) iLangVal = 0;
163742 }
163743 pCsr->iLangid = iLangVal;
163744
163745 rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL,
163746 pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
163747 );
163748 if( rc==SQLITE_OK ){
163749 rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
163750 }
163751
163752 if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
163753 return rc;
163754}
163755
163756/*
163757** xEof - Return true if the cursor is at EOF, or false otherwise.
163758*/
163759static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
163760 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
163761 return pCsr->isEof;
163762}
163763
163764/*
163765** xColumn - Return a column value.
163766*/
163767static int fts3auxColumnMethod(
163768 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
163769 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
163770 int iCol /* Index of column to read value from */
163771){
163772 Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
163773
163774 assert( p->isEof==0 );
163775 switch( iCol ){
163776 case 0: /* term */
163777 sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
163778 break;
163779
163780 case 1: /* col */
163781 if( p->iCol ){
163782 sqlite3_result_int(pCtx, p->iCol-1);
163783 }else{
163784 sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC);
163785 }
163786 break;
163787
163788 case 2: /* documents */
163789 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
163790 break;
163791
163792 case 3: /* occurrences */
163793 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
163794 break;
163795
163796 default: /* languageid */
163797 assert( iCol==4 );
163798 sqlite3_result_int(pCtx, p->iLangid);
163799 break;
163800 }
163801
163802 return SQLITE_OK;
163803}
163804
163805/*
163806** xRowid - Return the current rowid for the cursor.
163807*/
163808static int fts3auxRowidMethod(
163809 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
163810 sqlite_int64 *pRowid /* OUT: Rowid value */
163811){
163812 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
163813 *pRowid = pCsr->iRowid;
163814 return SQLITE_OK;
163815}
163816
163817/*
163818** Register the fts3aux module with database connection db. Return SQLITE_OK
163819** if successful or an error code if sqlite3_create_module() fails.
163820*/
163821SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
163822 static const sqlite3_module fts3aux_module = {
163823 0, /* iVersion */
163824 fts3auxConnectMethod, /* xCreate */
163825 fts3auxConnectMethod, /* xConnect */
163826 fts3auxBestIndexMethod, /* xBestIndex */
163827 fts3auxDisconnectMethod, /* xDisconnect */
163828 fts3auxDisconnectMethod, /* xDestroy */
163829 fts3auxOpenMethod, /* xOpen */
163830 fts3auxCloseMethod, /* xClose */
163831 fts3auxFilterMethod, /* xFilter */
163832 fts3auxNextMethod, /* xNext */
163833 fts3auxEofMethod, /* xEof */
163834 fts3auxColumnMethod, /* xColumn */
163835 fts3auxRowidMethod, /* xRowid */
163836 0, /* xUpdate */
163837 0, /* xBegin */
163838 0, /* xSync */
163839 0, /* xCommit */
163840 0, /* xRollback */
163841 0, /* xFindFunction */
163842 0, /* xRename */
163843 0, /* xSavepoint */
163844 0, /* xRelease */
163845 0 /* xRollbackTo */
163846 };
163847 int rc; /* Return code */
163848
163849 rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
163850 return rc;
163851}
163852
163853#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
163854
163855/************** End of fts3_aux.c ********************************************/
163856/************** Begin file fts3_expr.c ***************************************/
163857/*
163858** 2008 Nov 28
163859**
163860** The author disclaims copyright to this source code. In place of
163861** a legal notice, here is a blessing:
163862**
163863** May you do good and not evil.
163864** May you find forgiveness for yourself and forgive others.
163865** May you share freely, never taking more than you give.
163866**
163867******************************************************************************
163868**
163869** This module contains code that implements a parser for fts3 query strings
163870** (the right-hand argument to the MATCH operator). Because the supported
163871** syntax is relatively simple, the whole tokenizer/parser system is
163872** hand-coded.
163873*/
163874/* #include "fts3Int.h" */
163875#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
163876
163877/*
163878** By default, this module parses the legacy syntax that has been
163879** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
163880** is defined, then it uses the new syntax. The differences between
163881** the new and the old syntaxes are:
163882**
163883** a) The new syntax supports parenthesis. The old does not.
163884**
163885** b) The new syntax supports the AND and NOT operators. The old does not.
163886**
163887** c) The old syntax supports the "-" token qualifier. This is not
163888** supported by the new syntax (it is replaced by the NOT operator).
163889**
163890** d) When using the old syntax, the OR operator has a greater precedence
163891** than an implicit AND. When using the new, both implicity and explicit
163892** AND operators have a higher precedence than OR.
163893**
163894** If compiled with SQLITE_TEST defined, then this module exports the
163895** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
163896** to zero causes the module to use the old syntax. If it is set to
163897** non-zero the new syntax is activated. This is so both syntaxes can
163898** be tested using a single build of testfixture.
163899**
163900** The following describes the syntax supported by the fts3 MATCH
163901** operator in a similar format to that used by the lemon parser
163902** generator. This module does not use actually lemon, it uses a
163903** custom parser.
163904**
163905** query ::= andexpr (OR andexpr)*.
163906**
163907** andexpr ::= notexpr (AND? notexpr)*.
163908**
163909** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
163910** notexpr ::= LP query RP.
163911**
163912** nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
163913**
163914** distance_opt ::= .
163915** distance_opt ::= / INTEGER.
163916**
163917** phrase ::= TOKEN.
163918** phrase ::= COLUMN:TOKEN.
163919** phrase ::= "TOKEN TOKEN TOKEN...".
163920*/
163921
163922#ifdef SQLITE_TEST
163923SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
163924#else
163925# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
163926# define sqlite3_fts3_enable_parentheses 1
163927# else
163928# define sqlite3_fts3_enable_parentheses 0
163929# endif
163930#endif
163931
163932/*
163933** Default span for NEAR operators.
163934*/
163935#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
163936
163937/* #include <string.h> */
163938/* #include <assert.h> */
163939
163940/*
163941** isNot:
163942** This variable is used by function getNextNode(). When getNextNode() is
163943** called, it sets ParseContext.isNot to true if the 'next node' is a
163944** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
163945** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
163946** zero.
163947*/
163948typedef struct ParseContext ParseContext;
163949struct ParseContext {
163950 sqlite3_tokenizer *pTokenizer; /* Tokenizer module */
163951 int iLangid; /* Language id used with tokenizer */
163952 const char **azCol; /* Array of column names for fts3 table */
163953 int bFts4; /* True to allow FTS4-only syntax */
163954 int nCol; /* Number of entries in azCol[] */
163955 int iDefaultCol; /* Default column to query */
163956 int isNot; /* True if getNextNode() sees a unary - */
163957 sqlite3_context *pCtx; /* Write error message here */
163958 int nNest; /* Number of nested brackets */
163959};
163960
163961/*
163962** This function is equivalent to the standard isspace() function.
163963**
163964** The standard isspace() can be awkward to use safely, because although it
163965** is defined to accept an argument of type int, its behavior when passed
163966** an integer that falls outside of the range of the unsigned char type
163967** is undefined (and sometimes, "undefined" means segfault). This wrapper
163968** is defined to accept an argument of type char, and always returns 0 for
163969** any values that fall outside of the range of the unsigned char type (i.e.
163970** negative values).
163971*/
163972static int fts3isspace(char c){
163973 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
163974}
163975
163976/*
163977** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
163978** zero the memory before returning a pointer to it. If unsuccessful,
163979** return NULL.
163980*/
163981static void *fts3MallocZero(int nByte){
163982 void *pRet = sqlite3_malloc(nByte);
163983 if( pRet ) memset(pRet, 0, nByte);
163984 return pRet;
163985}
163986
163987SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(
163988 sqlite3_tokenizer *pTokenizer,
163989 int iLangid,
163990 const char *z,
163991 int n,
163992 sqlite3_tokenizer_cursor **ppCsr
163993){
163994 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
163995 sqlite3_tokenizer_cursor *pCsr = 0;
163996 int rc;
163997
163998 rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
163999 assert( rc==SQLITE_OK || pCsr==0 );
164000 if( rc==SQLITE_OK ){
164001 pCsr->pTokenizer = pTokenizer;
164002 if( pModule->iVersion>=1 ){
164003 rc = pModule->xLanguageid(pCsr, iLangid);
164004 if( rc!=SQLITE_OK ){
164005 pModule->xClose(pCsr);
164006 pCsr = 0;
164007 }
164008 }
164009 }
164010 *ppCsr = pCsr;
164011 return rc;
164012}
164013
164014/*
164015** Function getNextNode(), which is called by fts3ExprParse(), may itself
164016** call fts3ExprParse(). So this forward declaration is required.
164017*/
164018static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
164019
164020/*
164021** Extract the next token from buffer z (length n) using the tokenizer
164022** and other information (column names etc.) in pParse. Create an Fts3Expr
164023** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
164024** single token and set *ppExpr to point to it. If the end of the buffer is
164025** reached before a token is found, set *ppExpr to zero. It is the
164026** responsibility of the caller to eventually deallocate the allocated
164027** Fts3Expr structure (if any) by passing it to sqlite3_free().
164028**
164029** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
164030** fails.
164031*/
164032static int getNextToken(
164033 ParseContext *pParse, /* fts3 query parse context */
164034 int iCol, /* Value for Fts3Phrase.iColumn */
164035 const char *z, int n, /* Input string */
164036 Fts3Expr **ppExpr, /* OUT: expression */
164037 int *pnConsumed /* OUT: Number of bytes consumed */
164038){
164039 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
164040 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
164041 int rc;
164042 sqlite3_tokenizer_cursor *pCursor;
164043 Fts3Expr *pRet = 0;
164044 int i = 0;
164045
164046 /* Set variable i to the maximum number of bytes of input to tokenize. */
164047 for(i=0; i<n; i++){
164048 if( sqlite3_fts3_enable_parentheses && (z[i]=='(' || z[i]==')') ) break;
164049 if( z[i]=='"' ) break;
164050 }
164051
164052 *pnConsumed = i;
164053 rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
164054 if( rc==SQLITE_OK ){
164055 const char *zToken;
164056 int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
164057 int nByte; /* total space to allocate */
164058
164059 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
164060 if( rc==SQLITE_OK ){
164061 nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
164062 pRet = (Fts3Expr *)fts3MallocZero(nByte);
164063 if( !pRet ){
164064 rc = SQLITE_NOMEM;
164065 }else{
164066 pRet->eType = FTSQUERY_PHRASE;
164067 pRet->pPhrase = (Fts3Phrase *)&pRet[1];
164068 pRet->pPhrase->nToken = 1;
164069 pRet->pPhrase->iColumn = iCol;
164070 pRet->pPhrase->aToken[0].n = nToken;
164071 pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
164072 memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
164073
164074 if( iEnd<n && z[iEnd]=='*' ){
164075 pRet->pPhrase->aToken[0].isPrefix = 1;
164076 iEnd++;
164077 }
164078
164079 while( 1 ){
164080 if( !sqlite3_fts3_enable_parentheses
164081 && iStart>0 && z[iStart-1]=='-'
164082 ){
164083 pParse->isNot = 1;
164084 iStart--;
164085 }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
164086 pRet->pPhrase->aToken[0].bFirst = 1;
164087 iStart--;
164088 }else{
164089 break;
164090 }
164091 }
164092
164093 }
164094 *pnConsumed = iEnd;
164095 }else if( i && rc==SQLITE_DONE ){
164096 rc = SQLITE_OK;
164097 }
164098
164099 pModule->xClose(pCursor);
164100 }
164101
164102 *ppExpr = pRet;
164103 return rc;
164104}
164105
164106
164107/*
164108** Enlarge a memory allocation. If an out-of-memory allocation occurs,
164109** then free the old allocation.
164110*/
164111static void *fts3ReallocOrFree(void *pOrig, int nNew){
164112 void *pRet = sqlite3_realloc(pOrig, nNew);
164113 if( !pRet ){
164114 sqlite3_free(pOrig);
164115 }
164116 return pRet;
164117}
164118
164119/*
164120** Buffer zInput, length nInput, contains the contents of a quoted string
164121** that appeared as part of an fts3 query expression. Neither quote character
164122** is included in the buffer. This function attempts to tokenize the entire
164123** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
164124** containing the results.
164125**
164126** If successful, SQLITE_OK is returned and *ppExpr set to point at the
164127** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
164128** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
164129** to 0.
164130*/
164131static int getNextString(
164132 ParseContext *pParse, /* fts3 query parse context */
164133 const char *zInput, int nInput, /* Input string */
164134 Fts3Expr **ppExpr /* OUT: expression */
164135){
164136 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
164137 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
164138 int rc;
164139 Fts3Expr *p = 0;
164140 sqlite3_tokenizer_cursor *pCursor = 0;
164141 char *zTemp = 0;
164142 int nTemp = 0;
164143
164144 const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
164145 int nToken = 0;
164146
164147 /* The final Fts3Expr data structure, including the Fts3Phrase,
164148 ** Fts3PhraseToken structures token buffers are all stored as a single
164149 ** allocation so that the expression can be freed with a single call to
164150 ** sqlite3_free(). Setting this up requires a two pass approach.
164151 **
164152 ** The first pass, in the block below, uses a tokenizer cursor to iterate
164153 ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
164154 ** to assemble data in two dynamic buffers:
164155 **
164156 ** Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
164157 ** structure, followed by the array of Fts3PhraseToken
164158 ** structures. This pass only populates the Fts3PhraseToken array.
164159 **
164160 ** Buffer zTemp: Contains copies of all tokens.
164161 **
164162 ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
164163 ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
164164 ** structures.
164165 */
164166 rc = sqlite3Fts3OpenTokenizer(
164167 pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
164168 if( rc==SQLITE_OK ){
164169 int ii;
164170 for(ii=0; rc==SQLITE_OK; ii++){
164171 const char *zByte;
164172 int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
164173 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
164174 if( rc==SQLITE_OK ){
164175 Fts3PhraseToken *pToken;
164176
164177 p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
164178 if( !p ) goto no_mem;
164179
164180 zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
164181 if( !zTemp ) goto no_mem;
164182
164183 assert( nToken==ii );
164184 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
164185 memset(pToken, 0, sizeof(Fts3PhraseToken));
164186
164187 memcpy(&zTemp[nTemp], zByte, nByte);
164188 nTemp += nByte;
164189
164190 pToken->n = nByte;
164191 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
164192 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
164193 nToken = ii+1;
164194 }
164195 }
164196
164197 pModule->xClose(pCursor);
164198 pCursor = 0;
164199 }
164200
164201 if( rc==SQLITE_DONE ){
164202 int jj;
164203 char *zBuf = 0;
164204
164205 p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
164206 if( !p ) goto no_mem;
164207 memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
164208 p->eType = FTSQUERY_PHRASE;
164209 p->pPhrase = (Fts3Phrase *)&p[1];
164210 p->pPhrase->iColumn = pParse->iDefaultCol;
164211 p->pPhrase->nToken = nToken;
164212
164213 zBuf = (char *)&p->pPhrase->aToken[nToken];
164214 if( zTemp ){
164215 memcpy(zBuf, zTemp, nTemp);
164216 sqlite3_free(zTemp);
164217 }else{
164218 assert( nTemp==0 );
164219 }
164220
164221 for(jj=0; jj<p->pPhrase->nToken; jj++){
164222 p->pPhrase->aToken[jj].z = zBuf;
164223 zBuf += p->pPhrase->aToken[jj].n;
164224 }
164225 rc = SQLITE_OK;
164226 }
164227
164228 *ppExpr = p;
164229 return rc;
164230no_mem:
164231
164232 if( pCursor ){
164233 pModule->xClose(pCursor);
164234 }
164235 sqlite3_free(zTemp);
164236 sqlite3_free(p);
164237 *ppExpr = 0;
164238 return SQLITE_NOMEM;
164239}
164240
164241/*
164242** The output variable *ppExpr is populated with an allocated Fts3Expr
164243** structure, or set to 0 if the end of the input buffer is reached.
164244**
164245** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
164246** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
164247** If SQLITE_ERROR is returned, pContext is populated with an error message.
164248*/
164249static int getNextNode(
164250 ParseContext *pParse, /* fts3 query parse context */
164251 const char *z, int n, /* Input string */
164252 Fts3Expr **ppExpr, /* OUT: expression */
164253 int *pnConsumed /* OUT: Number of bytes consumed */
164254){
164255 static const struct Fts3Keyword {
164256 char *z; /* Keyword text */
164257 unsigned char n; /* Length of the keyword */
164258 unsigned char parenOnly; /* Only valid in paren mode */
164259 unsigned char eType; /* Keyword code */
164260 } aKeyword[] = {
164261 { "OR" , 2, 0, FTSQUERY_OR },
164262 { "AND", 3, 1, FTSQUERY_AND },
164263 { "NOT", 3, 1, FTSQUERY_NOT },
164264 { "NEAR", 4, 0, FTSQUERY_NEAR }
164265 };
164266 int ii;
164267 int iCol;
164268 int iColLen;
164269 int rc;
164270 Fts3Expr *pRet = 0;
164271
164272 const char *zInput = z;
164273 int nInput = n;
164274
164275 pParse->isNot = 0;
164276
164277 /* Skip over any whitespace before checking for a keyword, an open or
164278 ** close bracket, or a quoted string.
164279 */
164280 while( nInput>0 && fts3isspace(*zInput) ){
164281 nInput--;
164282 zInput++;
164283 }
164284 if( nInput==0 ){
164285 return SQLITE_DONE;
164286 }
164287
164288 /* See if we are dealing with a keyword. */
164289 for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
164290 const struct Fts3Keyword *pKey = &aKeyword[ii];
164291
164292 if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
164293 continue;
164294 }
164295
164296 if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
164297 int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
164298 int nKey = pKey->n;
164299 char cNext;
164300
164301 /* If this is a "NEAR" keyword, check for an explicit nearness. */
164302 if( pKey->eType==FTSQUERY_NEAR ){
164303 assert( nKey==4 );
164304 if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
164305 nNear = 0;
164306 for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
164307 nNear = nNear * 10 + (zInput[nKey] - '0');
164308 }
164309 }
164310 }
164311
164312 /* At this point this is probably a keyword. But for that to be true,
164313 ** the next byte must contain either whitespace, an open or close
164314 ** parenthesis, a quote character, or EOF.
164315 */
164316 cNext = zInput[nKey];
164317 if( fts3isspace(cNext)
164318 || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
164319 ){
164320 pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
164321 if( !pRet ){
164322 return SQLITE_NOMEM;
164323 }
164324 pRet->eType = pKey->eType;
164325 pRet->nNear = nNear;
164326 *ppExpr = pRet;
164327 *pnConsumed = (int)((zInput - z) + nKey);
164328 return SQLITE_OK;
164329 }
164330
164331 /* Turns out that wasn't a keyword after all. This happens if the
164332 ** user has supplied a token such as "ORacle". Continue.
164333 */
164334 }
164335 }
164336
164337 /* See if we are dealing with a quoted phrase. If this is the case, then
164338 ** search for the closing quote and pass the whole string to getNextString()
164339 ** for processing. This is easy to do, as fts3 has no syntax for escaping
164340 ** a quote character embedded in a string.
164341 */
164342 if( *zInput=='"' ){
164343 for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
164344 *pnConsumed = (int)((zInput - z) + ii + 1);
164345 if( ii==nInput ){
164346 return SQLITE_ERROR;
164347 }
164348 return getNextString(pParse, &zInput[1], ii-1, ppExpr);
164349 }
164350
164351 if( sqlite3_fts3_enable_parentheses ){
164352 if( *zInput=='(' ){
164353 int nConsumed = 0;
164354 pParse->nNest++;
164355 rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
164356 if( rc==SQLITE_OK && !*ppExpr ){ rc = SQLITE_DONE; }
164357 *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
164358 return rc;
164359 }else if( *zInput==')' ){
164360 pParse->nNest--;
164361 *pnConsumed = (int)((zInput - z) + 1);
164362 *ppExpr = 0;
164363 return SQLITE_DONE;
164364 }
164365 }
164366
164367 /* If control flows to this point, this must be a regular token, or
164368 ** the end of the input. Read a regular token using the sqlite3_tokenizer
164369 ** interface. Before doing so, figure out if there is an explicit
164370 ** column specifier for the token.
164371 **
164372 ** TODO: Strangely, it is not possible to associate a column specifier
164373 ** with a quoted phrase, only with a single token. Not sure if this was
164374 ** an implementation artifact or an intentional decision when fts3 was
164375 ** first implemented. Whichever it was, this module duplicates the
164376 ** limitation.
164377 */
164378 iCol = pParse->iDefaultCol;
164379 iColLen = 0;
164380 for(ii=0; ii<pParse->nCol; ii++){
164381 const char *zStr = pParse->azCol[ii];
164382 int nStr = (int)strlen(zStr);
164383 if( nInput>nStr && zInput[nStr]==':'
164384 && sqlite3_strnicmp(zStr, zInput, nStr)==0
164385 ){
164386 iCol = ii;
164387 iColLen = (int)((zInput - z) + nStr + 1);
164388 break;
164389 }
164390 }
164391 rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
164392 *pnConsumed += iColLen;
164393 return rc;
164394}
164395
164396/*
164397** The argument is an Fts3Expr structure for a binary operator (any type
164398** except an FTSQUERY_PHRASE). Return an integer value representing the
164399** precedence of the operator. Lower values have a higher precedence (i.e.
164400** group more tightly). For example, in the C language, the == operator
164401** groups more tightly than ||, and would therefore have a higher precedence.
164402**
164403** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
164404** is defined), the order of the operators in precedence from highest to
164405** lowest is:
164406**
164407** NEAR
164408** NOT
164409** AND (including implicit ANDs)
164410** OR
164411**
164412** Note that when using the old query syntax, the OR operator has a higher
164413** precedence than the AND operator.
164414*/
164415static int opPrecedence(Fts3Expr *p){
164416 assert( p->eType!=FTSQUERY_PHRASE );
164417 if( sqlite3_fts3_enable_parentheses ){
164418 return p->eType;
164419 }else if( p->eType==FTSQUERY_NEAR ){
164420 return 1;
164421 }else if( p->eType==FTSQUERY_OR ){
164422 return 2;
164423 }
164424 assert( p->eType==FTSQUERY_AND );
164425 return 3;
164426}
164427
164428/*
164429** Argument ppHead contains a pointer to the current head of a query
164430** expression tree being parsed. pPrev is the expression node most recently
164431** inserted into the tree. This function adds pNew, which is always a binary
164432** operator node, into the expression tree based on the relative precedence
164433** of pNew and the existing nodes of the tree. This may result in the head
164434** of the tree changing, in which case *ppHead is set to the new root node.
164435*/
164436static void insertBinaryOperator(
164437 Fts3Expr **ppHead, /* Pointer to the root node of a tree */
164438 Fts3Expr *pPrev, /* Node most recently inserted into the tree */
164439 Fts3Expr *pNew /* New binary node to insert into expression tree */
164440){
164441 Fts3Expr *pSplit = pPrev;
164442 while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
164443 pSplit = pSplit->pParent;
164444 }
164445
164446 if( pSplit->pParent ){
164447 assert( pSplit->pParent->pRight==pSplit );
164448 pSplit->pParent->pRight = pNew;
164449 pNew->pParent = pSplit->pParent;
164450 }else{
164451 *ppHead = pNew;
164452 }
164453 pNew->pLeft = pSplit;
164454 pSplit->pParent = pNew;
164455}
164456
164457/*
164458** Parse the fts3 query expression found in buffer z, length n. This function
164459** returns either when the end of the buffer is reached or an unmatched
164460** closing bracket - ')' - is encountered.
164461**
164462** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
164463** parsed form of the expression and *pnConsumed is set to the number of
164464** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
164465** (out of memory error) or SQLITE_ERROR (parse error) is returned.
164466*/
164467static int fts3ExprParse(
164468 ParseContext *pParse, /* fts3 query parse context */
164469 const char *z, int n, /* Text of MATCH query */
164470 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
164471 int *pnConsumed /* OUT: Number of bytes consumed */
164472){
164473 Fts3Expr *pRet = 0;
164474 Fts3Expr *pPrev = 0;
164475 Fts3Expr *pNotBranch = 0; /* Only used in legacy parse mode */
164476 int nIn = n;
164477 const char *zIn = z;
164478 int rc = SQLITE_OK;
164479 int isRequirePhrase = 1;
164480
164481 while( rc==SQLITE_OK ){
164482 Fts3Expr *p = 0;
164483 int nByte = 0;
164484
164485 rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
164486 assert( nByte>0 || (rc!=SQLITE_OK && p==0) );
164487 if( rc==SQLITE_OK ){
164488 if( p ){
164489 int isPhrase;
164490
164491 if( !sqlite3_fts3_enable_parentheses
164492 && p->eType==FTSQUERY_PHRASE && pParse->isNot
164493 ){
164494 /* Create an implicit NOT operator. */
164495 Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
164496 if( !pNot ){
164497 sqlite3Fts3ExprFree(p);
164498 rc = SQLITE_NOMEM;
164499 goto exprparse_out;
164500 }
164501 pNot->eType = FTSQUERY_NOT;
164502 pNot->pRight = p;
164503 p->pParent = pNot;
164504 if( pNotBranch ){
164505 pNot->pLeft = pNotBranch;
164506 pNotBranch->pParent = pNot;
164507 }
164508 pNotBranch = pNot;
164509 p = pPrev;
164510 }else{
164511 int eType = p->eType;
164512 isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
164513
164514 /* The isRequirePhrase variable is set to true if a phrase or
164515 ** an expression contained in parenthesis is required. If a
164516 ** binary operator (AND, OR, NOT or NEAR) is encounted when
164517 ** isRequirePhrase is set, this is a syntax error.
164518 */
164519 if( !isPhrase && isRequirePhrase ){
164520 sqlite3Fts3ExprFree(p);
164521 rc = SQLITE_ERROR;
164522 goto exprparse_out;
164523 }
164524
164525 if( isPhrase && !isRequirePhrase ){
164526 /* Insert an implicit AND operator. */
164527 Fts3Expr *pAnd;
164528 assert( pRet && pPrev );
164529 pAnd = fts3MallocZero(sizeof(Fts3Expr));
164530 if( !pAnd ){
164531 sqlite3Fts3ExprFree(p);
164532 rc = SQLITE_NOMEM;
164533 goto exprparse_out;
164534 }
164535 pAnd->eType = FTSQUERY_AND;
164536 insertBinaryOperator(&pRet, pPrev, pAnd);
164537 pPrev = pAnd;
164538 }
164539
164540 /* This test catches attempts to make either operand of a NEAR
164541 ** operator something other than a phrase. For example, either of
164542 ** the following:
164543 **
164544 ** (bracketed expression) NEAR phrase
164545 ** phrase NEAR (bracketed expression)
164546 **
164547 ** Return an error in either case.
164548 */
164549 if( pPrev && (
164550 (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
164551 || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
164552 )){
164553 sqlite3Fts3ExprFree(p);
164554 rc = SQLITE_ERROR;
164555 goto exprparse_out;
164556 }
164557
164558 if( isPhrase ){
164559 if( pRet ){
164560 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
164561 pPrev->pRight = p;
164562 p->pParent = pPrev;
164563 }else{
164564 pRet = p;
164565 }
164566 }else{
164567 insertBinaryOperator(&pRet, pPrev, p);
164568 }
164569 isRequirePhrase = !isPhrase;
164570 }
164571 pPrev = p;
164572 }
164573 assert( nByte>0 );
164574 }
164575 assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
164576 nIn -= nByte;
164577 zIn += nByte;
164578 }
164579
164580 if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
164581 rc = SQLITE_ERROR;
164582 }
164583
164584 if( rc==SQLITE_DONE ){
164585 rc = SQLITE_OK;
164586 if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
164587 if( !pRet ){
164588 rc = SQLITE_ERROR;
164589 }else{
164590 Fts3Expr *pIter = pNotBranch;
164591 while( pIter->pLeft ){
164592 pIter = pIter->pLeft;
164593 }
164594 pIter->pLeft = pRet;
164595 pRet->pParent = pIter;
164596 pRet = pNotBranch;
164597 }
164598 }
164599 }
164600 *pnConsumed = n - nIn;
164601
164602exprparse_out:
164603 if( rc!=SQLITE_OK ){
164604 sqlite3Fts3ExprFree(pRet);
164605 sqlite3Fts3ExprFree(pNotBranch);
164606 pRet = 0;
164607 }
164608 *ppExpr = pRet;
164609 return rc;
164610}
164611
164612/*
164613** Return SQLITE_ERROR if the maximum depth of the expression tree passed
164614** as the only argument is more than nMaxDepth.
164615*/
164616static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
164617 int rc = SQLITE_OK;
164618 if( p ){
164619 if( nMaxDepth<0 ){
164620 rc = SQLITE_TOOBIG;
164621 }else{
164622 rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
164623 if( rc==SQLITE_OK ){
164624 rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
164625 }
164626 }
164627 }
164628 return rc;
164629}
164630
164631/*
164632** This function attempts to transform the expression tree at (*pp) to
164633** an equivalent but more balanced form. The tree is modified in place.
164634** If successful, SQLITE_OK is returned and (*pp) set to point to the
164635** new root expression node.
164636**
164637** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
164638**
164639** Otherwise, if an error occurs, an SQLite error code is returned and
164640** expression (*pp) freed.
164641*/
164642static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
164643 int rc = SQLITE_OK; /* Return code */
164644 Fts3Expr *pRoot = *pp; /* Initial root node */
164645 Fts3Expr *pFree = 0; /* List of free nodes. Linked by pParent. */
164646 int eType = pRoot->eType; /* Type of node in this tree */
164647
164648 if( nMaxDepth==0 ){
164649 rc = SQLITE_ERROR;
164650 }
164651
164652 if( rc==SQLITE_OK ){
164653 if( (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
164654 Fts3Expr **apLeaf;
164655 apLeaf = (Fts3Expr **)sqlite3_malloc(sizeof(Fts3Expr *) * nMaxDepth);
164656 if( 0==apLeaf ){
164657 rc = SQLITE_NOMEM;
164658 }else{
164659 memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
164660 }
164661
164662 if( rc==SQLITE_OK ){
164663 int i;
164664 Fts3Expr *p;
164665
164666 /* Set $p to point to the left-most leaf in the tree of eType nodes. */
164667 for(p=pRoot; p->eType==eType; p=p->pLeft){
164668 assert( p->pParent==0 || p->pParent->pLeft==p );
164669 assert( p->pLeft && p->pRight );
164670 }
164671
164672 /* This loop runs once for each leaf in the tree of eType nodes. */
164673 while( 1 ){
164674 int iLvl;
164675 Fts3Expr *pParent = p->pParent; /* Current parent of p */
164676
164677 assert( pParent==0 || pParent->pLeft==p );
164678 p->pParent = 0;
164679 if( pParent ){
164680 pParent->pLeft = 0;
164681 }else{
164682 pRoot = 0;
164683 }
164684 rc = fts3ExprBalance(&p, nMaxDepth-1);
164685 if( rc!=SQLITE_OK ) break;
164686
164687 for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
164688 if( apLeaf[iLvl]==0 ){
164689 apLeaf[iLvl] = p;
164690 p = 0;
164691 }else{
164692 assert( pFree );
164693 pFree->pLeft = apLeaf[iLvl];
164694 pFree->pRight = p;
164695 pFree->pLeft->pParent = pFree;
164696 pFree->pRight->pParent = pFree;
164697
164698 p = pFree;
164699 pFree = pFree->pParent;
164700 p->pParent = 0;
164701 apLeaf[iLvl] = 0;
164702 }
164703 }
164704 if( p ){
164705 sqlite3Fts3ExprFree(p);
164706 rc = SQLITE_TOOBIG;
164707 break;
164708 }
164709
164710 /* If that was the last leaf node, break out of the loop */
164711 if( pParent==0 ) break;
164712
164713 /* Set $p to point to the next leaf in the tree of eType nodes */
164714 for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
164715
164716 /* Remove pParent from the original tree. */
164717 assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
164718 pParent->pRight->pParent = pParent->pParent;
164719 if( pParent->pParent ){
164720 pParent->pParent->pLeft = pParent->pRight;
164721 }else{
164722 assert( pParent==pRoot );
164723 pRoot = pParent->pRight;
164724 }
164725
164726 /* Link pParent into the free node list. It will be used as an
164727 ** internal node of the new tree. */
164728 pParent->pParent = pFree;
164729 pFree = pParent;
164730 }
164731
164732 if( rc==SQLITE_OK ){
164733 p = 0;
164734 for(i=0; i<nMaxDepth; i++){
164735 if( apLeaf[i] ){
164736 if( p==0 ){
164737 p = apLeaf[i];
164738 p->pParent = 0;
164739 }else{
164740 assert( pFree!=0 );
164741 pFree->pRight = p;
164742 pFree->pLeft = apLeaf[i];
164743 pFree->pLeft->pParent = pFree;
164744 pFree->pRight->pParent = pFree;
164745
164746 p = pFree;
164747 pFree = pFree->pParent;
164748 p->pParent = 0;
164749 }
164750 }
164751 }
164752 pRoot = p;
164753 }else{
164754 /* An error occurred. Delete the contents of the apLeaf[] array
164755 ** and pFree list. Everything else is cleaned up by the call to
164756 ** sqlite3Fts3ExprFree(pRoot) below. */
164757 Fts3Expr *pDel;
164758 for(i=0; i<nMaxDepth; i++){
164759 sqlite3Fts3ExprFree(apLeaf[i]);
164760 }
164761 while( (pDel=pFree)!=0 ){
164762 pFree = pDel->pParent;
164763 sqlite3_free(pDel);
164764 }
164765 }
164766
164767 assert( pFree==0 );
164768 sqlite3_free( apLeaf );
164769 }
164770 }else if( eType==FTSQUERY_NOT ){
164771 Fts3Expr *pLeft = pRoot->pLeft;
164772 Fts3Expr *pRight = pRoot->pRight;
164773
164774 pRoot->pLeft = 0;
164775 pRoot->pRight = 0;
164776 pLeft->pParent = 0;
164777 pRight->pParent = 0;
164778
164779 rc = fts3ExprBalance(&pLeft, nMaxDepth-1);
164780 if( rc==SQLITE_OK ){
164781 rc = fts3ExprBalance(&pRight, nMaxDepth-1);
164782 }
164783
164784 if( rc!=SQLITE_OK ){
164785 sqlite3Fts3ExprFree(pRight);
164786 sqlite3Fts3ExprFree(pLeft);
164787 }else{
164788 assert( pLeft && pRight );
164789 pRoot->pLeft = pLeft;
164790 pLeft->pParent = pRoot;
164791 pRoot->pRight = pRight;
164792 pRight->pParent = pRoot;
164793 }
164794 }
164795 }
164796
164797 if( rc!=SQLITE_OK ){
164798 sqlite3Fts3ExprFree(pRoot);
164799 pRoot = 0;
164800 }
164801 *pp = pRoot;
164802 return rc;
164803}
164804
164805/*
164806** This function is similar to sqlite3Fts3ExprParse(), with the following
164807** differences:
164808**
164809** 1. It does not do expression rebalancing.
164810** 2. It does not check that the expression does not exceed the
164811** maximum allowable depth.
164812** 3. Even if it fails, *ppExpr may still be set to point to an
164813** expression tree. It should be deleted using sqlite3Fts3ExprFree()
164814** in this case.
164815*/
164816static int fts3ExprParseUnbalanced(
164817 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
164818 int iLangid, /* Language id for tokenizer */
164819 char **azCol, /* Array of column names for fts3 table */
164820 int bFts4, /* True to allow FTS4-only syntax */
164821 int nCol, /* Number of entries in azCol[] */
164822 int iDefaultCol, /* Default column to query */
164823 const char *z, int n, /* Text of MATCH query */
164824 Fts3Expr **ppExpr /* OUT: Parsed query structure */
164825){
164826 int nParsed;
164827 int rc;
164828 ParseContext sParse;
164829
164830 memset(&sParse, 0, sizeof(ParseContext));
164831 sParse.pTokenizer = pTokenizer;
164832 sParse.iLangid = iLangid;
164833 sParse.azCol = (const char **)azCol;
164834 sParse.nCol = nCol;
164835 sParse.iDefaultCol = iDefaultCol;
164836 sParse.bFts4 = bFts4;
164837 if( z==0 ){
164838 *ppExpr = 0;
164839 return SQLITE_OK;
164840 }
164841 if( n<0 ){
164842 n = (int)strlen(z);
164843 }
164844 rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
164845 assert( rc==SQLITE_OK || *ppExpr==0 );
164846
164847 /* Check for mismatched parenthesis */
164848 if( rc==SQLITE_OK && sParse.nNest ){
164849 rc = SQLITE_ERROR;
164850 }
164851
164852 return rc;
164853}
164854
164855/*
164856** Parameters z and n contain a pointer to and length of a buffer containing
164857** an fts3 query expression, respectively. This function attempts to parse the
164858** query expression and create a tree of Fts3Expr structures representing the
164859** parsed expression. If successful, *ppExpr is set to point to the head
164860** of the parsed expression tree and SQLITE_OK is returned. If an error
164861** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
164862** error) is returned and *ppExpr is set to 0.
164863**
164864** If parameter n is a negative number, then z is assumed to point to a
164865** nul-terminated string and the length is determined using strlen().
164866**
164867** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
164868** use to normalize query tokens while parsing the expression. The azCol[]
164869** array, which is assumed to contain nCol entries, should contain the names
164870** of each column in the target fts3 table, in order from left to right.
164871** Column names must be nul-terminated strings.
164872**
164873** The iDefaultCol parameter should be passed the index of the table column
164874** that appears on the left-hand-side of the MATCH operator (the default
164875** column to match against for tokens for which a column name is not explicitly
164876** specified as part of the query string), or -1 if tokens may by default
164877** match any table column.
164878*/
164879SQLITE_PRIVATE int sqlite3Fts3ExprParse(
164880 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
164881 int iLangid, /* Language id for tokenizer */
164882 char **azCol, /* Array of column names for fts3 table */
164883 int bFts4, /* True to allow FTS4-only syntax */
164884 int nCol, /* Number of entries in azCol[] */
164885 int iDefaultCol, /* Default column to query */
164886 const char *z, int n, /* Text of MATCH query */
164887 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
164888 char **pzErr /* OUT: Error message (sqlite3_malloc) */
164889){
164890 int rc = fts3ExprParseUnbalanced(
164891 pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
164892 );
164893
164894 /* Rebalance the expression. And check that its depth does not exceed
164895 ** SQLITE_FTS3_MAX_EXPR_DEPTH. */
164896 if( rc==SQLITE_OK && *ppExpr ){
164897 rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
164898 if( rc==SQLITE_OK ){
164899 rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
164900 }
164901 }
164902
164903 if( rc!=SQLITE_OK ){
164904 sqlite3Fts3ExprFree(*ppExpr);
164905 *ppExpr = 0;
164906 if( rc==SQLITE_TOOBIG ){
164907 sqlite3Fts3ErrMsg(pzErr,
164908 "FTS expression tree is too large (maximum depth %d)",
164909 SQLITE_FTS3_MAX_EXPR_DEPTH
164910 );
164911 rc = SQLITE_ERROR;
164912 }else if( rc==SQLITE_ERROR ){
164913 sqlite3Fts3ErrMsg(pzErr, "malformed MATCH expression: [%s]", z);
164914 }
164915 }
164916
164917 return rc;
164918}
164919
164920/*
164921** Free a single node of an expression tree.
164922*/
164923static void fts3FreeExprNode(Fts3Expr *p){
164924 assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
164925 sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
164926 sqlite3_free(p->aMI);
164927 sqlite3_free(p);
164928}
164929
164930/*
164931** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
164932**
164933** This function would be simpler if it recursively called itself. But
164934** that would mean passing a sufficiently large expression to ExprParse()
164935** could cause a stack overflow.
164936*/
164937SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *pDel){
164938 Fts3Expr *p;
164939 assert( pDel==0 || pDel->pParent==0 );
164940 for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
164941 assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
164942 }
164943 while( p ){
164944 Fts3Expr *pParent = p->pParent;
164945 fts3FreeExprNode(p);
164946 if( pParent && p==pParent->pLeft && pParent->pRight ){
164947 p = pParent->pRight;
164948 while( p && (p->pLeft || p->pRight) ){
164949 assert( p==p->pParent->pRight || p==p->pParent->pLeft );
164950 p = (p->pLeft ? p->pLeft : p->pRight);
164951 }
164952 }else{
164953 p = pParent;
164954 }
164955 }
164956}
164957
164958/****************************************************************************
164959*****************************************************************************
164960** Everything after this point is just test code.
164961*/
164962
164963#ifdef SQLITE_TEST
164964
164965/* #include <stdio.h> */
164966
164967/*
164968** Return a pointer to a buffer containing a text representation of the
164969** expression passed as the first argument. The buffer is obtained from
164970** sqlite3_malloc(). It is the responsibility of the caller to use
164971** sqlite3_free() to release the memory. If an OOM condition is encountered,
164972** NULL is returned.
164973**
164974** If the second argument is not NULL, then its contents are prepended to
164975** the returned expression text and then freed using sqlite3_free().
164976*/
164977static char *exprToString(Fts3Expr *pExpr, char *zBuf){
164978 if( pExpr==0 ){
164979 return sqlite3_mprintf("");
164980 }
164981 switch( pExpr->eType ){
164982 case FTSQUERY_PHRASE: {
164983 Fts3Phrase *pPhrase = pExpr->pPhrase;
164984 int i;
164985 zBuf = sqlite3_mprintf(
164986 "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
164987 for(i=0; zBuf && i<pPhrase->nToken; i++){
164988 zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
164989 pPhrase->aToken[i].n, pPhrase->aToken[i].z,
164990 (pPhrase->aToken[i].isPrefix?"+":"")
164991 );
164992 }
164993 return zBuf;
164994 }
164995
164996 case FTSQUERY_NEAR:
164997 zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
164998 break;
164999 case FTSQUERY_NOT:
165000 zBuf = sqlite3_mprintf("%zNOT ", zBuf);
165001 break;
165002 case FTSQUERY_AND:
165003 zBuf = sqlite3_mprintf("%zAND ", zBuf);
165004 break;
165005 case FTSQUERY_OR:
165006 zBuf = sqlite3_mprintf("%zOR ", zBuf);
165007 break;
165008 }
165009
165010 if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
165011 if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
165012 if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
165013
165014 if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
165015 if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
165016
165017 return zBuf;
165018}
165019
165020/*
165021** This is the implementation of a scalar SQL function used to test the
165022** expression parser. It should be called as follows:
165023**
165024** fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
165025**
165026** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
165027** to parse the query expression (see README.tokenizers). The second argument
165028** is the query expression to parse. Each subsequent argument is the name
165029** of a column of the fts3 table that the query expression may refer to.
165030** For example:
165031**
165032** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
165033*/
165034static void fts3ExprTestCommon(
165035 int bRebalance,
165036 sqlite3_context *context,
165037 int argc,
165038 sqlite3_value **argv
165039){
165040 sqlite3_tokenizer *pTokenizer = 0;
165041 int rc;
165042 char **azCol = 0;
165043 const char *zExpr;
165044 int nExpr;
165045 int nCol;
165046 int ii;
165047 Fts3Expr *pExpr;
165048 char *zBuf = 0;
165049 Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context);
165050 const char *zTokenizer = 0;
165051 char *zErr = 0;
165052
165053 if( argc<3 ){
165054 sqlite3_result_error(context,
165055 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
165056 );
165057 return;
165058 }
165059
165060 zTokenizer = (const char*)sqlite3_value_text(argv[0]);
165061 rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr);
165062 if( rc!=SQLITE_OK ){
165063 if( rc==SQLITE_NOMEM ){
165064 sqlite3_result_error_nomem(context);
165065 }else{
165066 sqlite3_result_error(context, zErr, -1);
165067 }
165068 sqlite3_free(zErr);
165069 return;
165070 }
165071
165072 zExpr = (const char *)sqlite3_value_text(argv[1]);
165073 nExpr = sqlite3_value_bytes(argv[1]);
165074 nCol = argc-2;
165075 azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
165076 if( !azCol ){
165077 sqlite3_result_error_nomem(context);
165078 goto exprtest_out;
165079 }
165080 for(ii=0; ii<nCol; ii++){
165081 azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
165082 }
165083
165084 if( bRebalance ){
165085 char *zDummy = 0;
165086 rc = sqlite3Fts3ExprParse(
165087 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
165088 );
165089 assert( rc==SQLITE_OK || pExpr==0 );
165090 sqlite3_free(zDummy);
165091 }else{
165092 rc = fts3ExprParseUnbalanced(
165093 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
165094 );
165095 }
165096
165097 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
165098 sqlite3Fts3ExprFree(pExpr);
165099 sqlite3_result_error(context, "Error parsing expression", -1);
165100 }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
165101 sqlite3_result_error_nomem(context);
165102 }else{
165103 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
165104 sqlite3_free(zBuf);
165105 }
165106
165107 sqlite3Fts3ExprFree(pExpr);
165108
165109exprtest_out:
165110 if( pTokenizer ){
165111 rc = pTokenizer->pModule->xDestroy(pTokenizer);
165112 }
165113 sqlite3_free(azCol);
165114}
165115
165116static void fts3ExprTest(
165117 sqlite3_context *context,
165118 int argc,
165119 sqlite3_value **argv
165120){
165121 fts3ExprTestCommon(0, context, argc, argv);
165122}
165123static void fts3ExprTestRebalance(
165124 sqlite3_context *context,
165125 int argc,
165126 sqlite3_value **argv
165127){
165128 fts3ExprTestCommon(1, context, argc, argv);
165129}
165130
165131/*
165132** Register the query expression parser test function fts3_exprtest()
165133** with database connection db.
165134*/
165135SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){
165136 int rc = sqlite3_create_function(
165137 db, "fts3_exprtest", -1, SQLITE_UTF8, (void*)pHash, fts3ExprTest, 0, 0
165138 );
165139 if( rc==SQLITE_OK ){
165140 rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
165141 -1, SQLITE_UTF8, (void*)pHash, fts3ExprTestRebalance, 0, 0
165142 );
165143 }
165144 return rc;
165145}
165146
165147#endif
165148#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
165149
165150/************** End of fts3_expr.c *******************************************/
165151/************** Begin file fts3_hash.c ***************************************/
165152/*
165153** 2001 September 22
165154**
165155** The author disclaims copyright to this source code. In place of
165156** a legal notice, here is a blessing:
165157**
165158** May you do good and not evil.
165159** May you find forgiveness for yourself and forgive others.
165160** May you share freely, never taking more than you give.
165161**
165162*************************************************************************
165163** This is the implementation of generic hash-tables used in SQLite.
165164** We've modified it slightly to serve as a standalone hash table
165165** implementation for the full-text indexing module.
165166*/
165167
165168/*
165169** The code in this file is only compiled if:
165170**
165171** * The FTS3 module is being built as an extension
165172** (in which case SQLITE_CORE is not defined), or
165173**
165174** * The FTS3 module is being built into the core of
165175** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
165176*/
165177/* #include "fts3Int.h" */
165178#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
165179
165180/* #include <assert.h> */
165181/* #include <stdlib.h> */
165182/* #include <string.h> */
165183
165184/* #include "fts3_hash.h" */
165185
165186/*
165187** Malloc and Free functions
165188*/
165189static void *fts3HashMalloc(int n){
165190 void *p = sqlite3_malloc(n);
165191 if( p ){
165192 memset(p, 0, n);
165193 }
165194 return p;
165195}
165196static void fts3HashFree(void *p){
165197 sqlite3_free(p);
165198}
165199
165200/* Turn bulk memory into a hash table object by initializing the
165201** fields of the Hash structure.
165202**
165203** "pNew" is a pointer to the hash table that is to be initialized.
165204** keyClass is one of the constants
165205** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass
165206** determines what kind of key the hash table will use. "copyKey" is
165207** true if the hash table should make its own private copy of keys and
165208** false if it should just use the supplied pointer.
165209*/
165210SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
165211 assert( pNew!=0 );
165212 assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
165213 pNew->keyClass = keyClass;
165214 pNew->copyKey = copyKey;
165215 pNew->first = 0;
165216 pNew->count = 0;
165217 pNew->htsize = 0;
165218 pNew->ht = 0;
165219}
165220
165221/* Remove all entries from a hash table. Reclaim all memory.
165222** Call this routine to delete a hash table or to reset a hash table
165223** to the empty state.
165224*/
165225SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
165226 Fts3HashElem *elem; /* For looping over all elements of the table */
165227
165228 assert( pH!=0 );
165229 elem = pH->first;
165230 pH->first = 0;
165231 fts3HashFree(pH->ht);
165232 pH->ht = 0;
165233 pH->htsize = 0;
165234 while( elem ){
165235 Fts3HashElem *next_elem = elem->next;
165236 if( pH->copyKey && elem->pKey ){
165237 fts3HashFree(elem->pKey);
165238 }
165239 fts3HashFree(elem);
165240 elem = next_elem;
165241 }
165242 pH->count = 0;
165243}
165244
165245/*
165246** Hash and comparison functions when the mode is FTS3_HASH_STRING
165247*/
165248static int fts3StrHash(const void *pKey, int nKey){
165249 const char *z = (const char *)pKey;
165250 unsigned h = 0;
165251 if( nKey<=0 ) nKey = (int) strlen(z);
165252 while( nKey > 0 ){
165253 h = (h<<3) ^ h ^ *z++;
165254 nKey--;
165255 }
165256 return (int)(h & 0x7fffffff);
165257}
165258static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
165259 if( n1!=n2 ) return 1;
165260 return strncmp((const char*)pKey1,(const char*)pKey2,n1);
165261}
165262
165263/*
165264** Hash and comparison functions when the mode is FTS3_HASH_BINARY
165265*/
165266static int fts3BinHash(const void *pKey, int nKey){
165267 int h = 0;
165268 const char *z = (const char *)pKey;
165269 while( nKey-- > 0 ){
165270 h = (h<<3) ^ h ^ *(z++);
165271 }
165272 return h & 0x7fffffff;
165273}
165274static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
165275 if( n1!=n2 ) return 1;
165276 return memcmp(pKey1,pKey2,n1);
165277}
165278
165279/*
165280** Return a pointer to the appropriate hash function given the key class.
165281**
165282** The C syntax in this function definition may be unfamilar to some
165283** programmers, so we provide the following additional explanation:
165284**
165285** The name of the function is "ftsHashFunction". The function takes a
165286** single parameter "keyClass". The return value of ftsHashFunction()
165287** is a pointer to another function. Specifically, the return value
165288** of ftsHashFunction() is a pointer to a function that takes two parameters
165289** with types "const void*" and "int" and returns an "int".
165290*/
165291static int (*ftsHashFunction(int keyClass))(const void*,int){
165292 if( keyClass==FTS3_HASH_STRING ){
165293 return &fts3StrHash;
165294 }else{
165295 assert( keyClass==FTS3_HASH_BINARY );
165296 return &fts3BinHash;
165297 }
165298}
165299
165300/*
165301** Return a pointer to the appropriate hash function given the key class.
165302**
165303** For help in interpreted the obscure C code in the function definition,
165304** see the header comment on the previous function.
165305*/
165306static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
165307 if( keyClass==FTS3_HASH_STRING ){
165308 return &fts3StrCompare;
165309 }else{
165310 assert( keyClass==FTS3_HASH_BINARY );
165311 return &fts3BinCompare;
165312 }
165313}
165314
165315/* Link an element into the hash table
165316*/
165317static void fts3HashInsertElement(
165318 Fts3Hash *pH, /* The complete hash table */
165319 struct _fts3ht *pEntry, /* The entry into which pNew is inserted */
165320 Fts3HashElem *pNew /* The element to be inserted */
165321){
165322 Fts3HashElem *pHead; /* First element already in pEntry */
165323 pHead = pEntry->chain;
165324 if( pHead ){
165325 pNew->next = pHead;
165326 pNew->prev = pHead->prev;
165327 if( pHead->prev ){ pHead->prev->next = pNew; }
165328 else { pH->first = pNew; }
165329 pHead->prev = pNew;
165330 }else{
165331 pNew->next = pH->first;
165332 if( pH->first ){ pH->first->prev = pNew; }
165333 pNew->prev = 0;
165334 pH->first = pNew;
165335 }
165336 pEntry->count++;
165337 pEntry->chain = pNew;
165338}
165339
165340
165341/* Resize the hash table so that it cantains "new_size" buckets.
165342** "new_size" must be a power of 2. The hash table might fail
165343** to resize if sqliteMalloc() fails.
165344**
165345** Return non-zero if a memory allocation error occurs.
165346*/
165347static int fts3Rehash(Fts3Hash *pH, int new_size){
165348 struct _fts3ht *new_ht; /* The new hash table */
165349 Fts3HashElem *elem, *next_elem; /* For looping over existing elements */
165350 int (*xHash)(const void*,int); /* The hash function */
165351
165352 assert( (new_size & (new_size-1))==0 );
165353 new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
165354 if( new_ht==0 ) return 1;
165355 fts3HashFree(pH->ht);
165356 pH->ht = new_ht;
165357 pH->htsize = new_size;
165358 xHash = ftsHashFunction(pH->keyClass);
165359 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
165360 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
165361 next_elem = elem->next;
165362 fts3HashInsertElement(pH, &new_ht[h], elem);
165363 }
165364 return 0;
165365}
165366
165367/* This function (for internal use only) locates an element in an
165368** hash table that matches the given key. The hash for this key has
165369** already been computed and is passed as the 4th parameter.
165370*/
165371static Fts3HashElem *fts3FindElementByHash(
165372 const Fts3Hash *pH, /* The pH to be searched */
165373 const void *pKey, /* The key we are searching for */
165374 int nKey,
165375 int h /* The hash for this key. */
165376){
165377 Fts3HashElem *elem; /* Used to loop thru the element list */
165378 int count; /* Number of elements left to test */
165379 int (*xCompare)(const void*,int,const void*,int); /* comparison function */
165380
165381 if( pH->ht ){
165382 struct _fts3ht *pEntry = &pH->ht[h];
165383 elem = pEntry->chain;
165384 count = pEntry->count;
165385 xCompare = ftsCompareFunction(pH->keyClass);
165386 while( count-- && elem ){
165387 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
165388 return elem;
165389 }
165390 elem = elem->next;
165391 }
165392 }
165393 return 0;
165394}
165395
165396/* Remove a single entry from the hash table given a pointer to that
165397** element and a hash on the element's key.
165398*/
165399static void fts3RemoveElementByHash(
165400 Fts3Hash *pH, /* The pH containing "elem" */
165401 Fts3HashElem* elem, /* The element to be removed from the pH */
165402 int h /* Hash value for the element */
165403){
165404 struct _fts3ht *pEntry;
165405 if( elem->prev ){
165406 elem->prev->next = elem->next;
165407 }else{
165408 pH->first = elem->next;
165409 }
165410 if( elem->next ){
165411 elem->next->prev = elem->prev;
165412 }
165413 pEntry = &pH->ht[h];
165414 if( pEntry->chain==elem ){
165415 pEntry->chain = elem->next;
165416 }
165417 pEntry->count--;
165418 if( pEntry->count<=0 ){
165419 pEntry->chain = 0;
165420 }
165421 if( pH->copyKey && elem->pKey ){
165422 fts3HashFree(elem->pKey);
165423 }
165424 fts3HashFree( elem );
165425 pH->count--;
165426 if( pH->count<=0 ){
165427 assert( pH->first==0 );
165428 assert( pH->count==0 );
165429 fts3HashClear(pH);
165430 }
165431}
165432
165433SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
165434 const Fts3Hash *pH,
165435 const void *pKey,
165436 int nKey
165437){
165438 int h; /* A hash on key */
165439 int (*xHash)(const void*,int); /* The hash function */
165440
165441 if( pH==0 || pH->ht==0 ) return 0;
165442 xHash = ftsHashFunction(pH->keyClass);
165443 assert( xHash!=0 );
165444 h = (*xHash)(pKey,nKey);
165445 assert( (pH->htsize & (pH->htsize-1))==0 );
165446 return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
165447}
165448
165449/*
165450** Attempt to locate an element of the hash table pH with a key
165451** that matches pKey,nKey. Return the data for this element if it is
165452** found, or NULL if there is no match.
165453*/
165454SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
165455 Fts3HashElem *pElem; /* The element that matches key (if any) */
165456
165457 pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
165458 return pElem ? pElem->data : 0;
165459}
165460
165461/* Insert an element into the hash table pH. The key is pKey,nKey
165462** and the data is "data".
165463**
165464** If no element exists with a matching key, then a new
165465** element is created. A copy of the key is made if the copyKey
165466** flag is set. NULL is returned.
165467**
165468** If another element already exists with the same key, then the
165469** new data replaces the old data and the old data is returned.
165470** The key is not copied in this instance. If a malloc fails, then
165471** the new data is returned and the hash table is unchanged.
165472**
165473** If the "data" parameter to this function is NULL, then the
165474** element corresponding to "key" is removed from the hash table.
165475*/
165476SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
165477 Fts3Hash *pH, /* The hash table to insert into */
165478 const void *pKey, /* The key */
165479 int nKey, /* Number of bytes in the key */
165480 void *data /* The data */
165481){
165482 int hraw; /* Raw hash value of the key */
165483 int h; /* the hash of the key modulo hash table size */
165484 Fts3HashElem *elem; /* Used to loop thru the element list */
165485 Fts3HashElem *new_elem; /* New element added to the pH */
165486 int (*xHash)(const void*,int); /* The hash function */
165487
165488 assert( pH!=0 );
165489 xHash = ftsHashFunction(pH->keyClass);
165490 assert( xHash!=0 );
165491 hraw = (*xHash)(pKey, nKey);
165492 assert( (pH->htsize & (pH->htsize-1))==0 );
165493 h = hraw & (pH->htsize-1);
165494 elem = fts3FindElementByHash(pH,pKey,nKey,h);
165495 if( elem ){
165496 void *old_data = elem->data;
165497 if( data==0 ){
165498 fts3RemoveElementByHash(pH,elem,h);
165499 }else{
165500 elem->data = data;
165501 }
165502 return old_data;
165503 }
165504 if( data==0 ) return 0;
165505 if( (pH->htsize==0 && fts3Rehash(pH,8))
165506 || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
165507 ){
165508 pH->count = 0;
165509 return data;
165510 }
165511 assert( pH->htsize>0 );
165512 new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
165513 if( new_elem==0 ) return data;
165514 if( pH->copyKey && pKey!=0 ){
165515 new_elem->pKey = fts3HashMalloc( nKey );
165516 if( new_elem->pKey==0 ){
165517 fts3HashFree(new_elem);
165518 return data;
165519 }
165520 memcpy((void*)new_elem->pKey, pKey, nKey);
165521 }else{
165522 new_elem->pKey = (void*)pKey;
165523 }
165524 new_elem->nKey = nKey;
165525 pH->count++;
165526 assert( pH->htsize>0 );
165527 assert( (pH->htsize & (pH->htsize-1))==0 );
165528 h = hraw & (pH->htsize-1);
165529 fts3HashInsertElement(pH, &pH->ht[h], new_elem);
165530 new_elem->data = data;
165531 return 0;
165532}
165533
165534#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
165535
165536/************** End of fts3_hash.c *******************************************/
165537/************** Begin file fts3_porter.c *************************************/
165538/*
165539** 2006 September 30
165540**
165541** The author disclaims copyright to this source code. In place of
165542** a legal notice, here is a blessing:
165543**
165544** May you do good and not evil.
165545** May you find forgiveness for yourself and forgive others.
165546** May you share freely, never taking more than you give.
165547**
165548*************************************************************************
165549** Implementation of the full-text-search tokenizer that implements
165550** a Porter stemmer.
165551*/
165552
165553/*
165554** The code in this file is only compiled if:
165555**
165556** * The FTS3 module is being built as an extension
165557** (in which case SQLITE_CORE is not defined), or
165558**
165559** * The FTS3 module is being built into the core of
165560** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
165561*/
165562/* #include "fts3Int.h" */
165563#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
165564
165565/* #include <assert.h> */
165566/* #include <stdlib.h> */
165567/* #include <stdio.h> */
165568/* #include <string.h> */
165569
165570/* #include "fts3_tokenizer.h" */
165571
165572/*
165573** Class derived from sqlite3_tokenizer
165574*/
165575typedef struct porter_tokenizer {
165576 sqlite3_tokenizer base; /* Base class */
165577} porter_tokenizer;
165578
165579/*
165580** Class derived from sqlite3_tokenizer_cursor
165581*/
165582typedef struct porter_tokenizer_cursor {
165583 sqlite3_tokenizer_cursor base;
165584 const char *zInput; /* input we are tokenizing */
165585 int nInput; /* size of the input */
165586 int iOffset; /* current position in zInput */
165587 int iToken; /* index of next token to be returned */
165588 char *zToken; /* storage for current token */
165589 int nAllocated; /* space allocated to zToken buffer */
165590} porter_tokenizer_cursor;
165591
165592
165593/*
165594** Create a new tokenizer instance.
165595*/
165596static int porterCreate(
165597 int argc, const char * const *argv,
165598 sqlite3_tokenizer **ppTokenizer
165599){
165600 porter_tokenizer *t;
165601
165602 UNUSED_PARAMETER(argc);
165603 UNUSED_PARAMETER(argv);
165604
165605 t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
165606 if( t==NULL ) return SQLITE_NOMEM;
165607 memset(t, 0, sizeof(*t));
165608 *ppTokenizer = &t->base;
165609 return SQLITE_OK;
165610}
165611
165612/*
165613** Destroy a tokenizer
165614*/
165615static int porterDestroy(sqlite3_tokenizer *pTokenizer){
165616 sqlite3_free(pTokenizer);
165617 return SQLITE_OK;
165618}
165619
165620/*
165621** Prepare to begin tokenizing a particular string. The input
165622** string to be tokenized is zInput[0..nInput-1]. A cursor
165623** used to incrementally tokenize this string is returned in
165624** *ppCursor.
165625*/
165626static int porterOpen(
165627 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
165628 const char *zInput, int nInput, /* String to be tokenized */
165629 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
165630){
165631 porter_tokenizer_cursor *c;
165632
165633 UNUSED_PARAMETER(pTokenizer);
165634
165635 c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
165636 if( c==NULL ) return SQLITE_NOMEM;
165637
165638 c->zInput = zInput;
165639 if( zInput==0 ){
165640 c->nInput = 0;
165641 }else if( nInput<0 ){
165642 c->nInput = (int)strlen(zInput);
165643 }else{
165644 c->nInput = nInput;
165645 }
165646 c->iOffset = 0; /* start tokenizing at the beginning */
165647 c->iToken = 0;
165648 c->zToken = NULL; /* no space allocated, yet. */
165649 c->nAllocated = 0;
165650
165651 *ppCursor = &c->base;
165652 return SQLITE_OK;
165653}
165654
165655/*
165656** Close a tokenization cursor previously opened by a call to
165657** porterOpen() above.
165658*/
165659static int porterClose(sqlite3_tokenizer_cursor *pCursor){
165660 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
165661 sqlite3_free(c->zToken);
165662 sqlite3_free(c);
165663 return SQLITE_OK;
165664}
165665/*
165666** Vowel or consonant
165667*/
165668static const char cType[] = {
165669 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
165670 1, 1, 1, 2, 1
165671};
165672
165673/*
165674** isConsonant() and isVowel() determine if their first character in
165675** the string they point to is a consonant or a vowel, according
165676** to Porter ruls.
165677**
165678** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
165679** 'Y' is a consonant unless it follows another consonant,
165680** in which case it is a vowel.
165681**
165682** In these routine, the letters are in reverse order. So the 'y' rule
165683** is that 'y' is a consonant unless it is followed by another
165684** consonent.
165685*/
165686static int isVowel(const char*);
165687static int isConsonant(const char *z){
165688 int j;
165689 char x = *z;
165690 if( x==0 ) return 0;
165691 assert( x>='a' && x<='z' );
165692 j = cType[x-'a'];
165693 if( j<2 ) return j;
165694 return z[1]==0 || isVowel(z + 1);
165695}
165696static int isVowel(const char *z){
165697 int j;
165698 char x = *z;
165699 if( x==0 ) return 0;
165700 assert( x>='a' && x<='z' );
165701 j = cType[x-'a'];
165702 if( j<2 ) return 1-j;
165703 return isConsonant(z + 1);
165704}
165705
165706/*
165707** Let any sequence of one or more vowels be represented by V and let
165708** C be sequence of one or more consonants. Then every word can be
165709** represented as:
165710**
165711** [C] (VC){m} [V]
165712**
165713** In prose: A word is an optional consonant followed by zero or
165714** vowel-consonant pairs followed by an optional vowel. "m" is the
165715** number of vowel consonant pairs. This routine computes the value
165716** of m for the first i bytes of a word.
165717**
165718** Return true if the m-value for z is 1 or more. In other words,
165719** return true if z contains at least one vowel that is followed
165720** by a consonant.
165721**
165722** In this routine z[] is in reverse order. So we are really looking
165723** for an instance of a consonant followed by a vowel.
165724*/
165725static int m_gt_0(const char *z){
165726 while( isVowel(z) ){ z++; }
165727 if( *z==0 ) return 0;
165728 while( isConsonant(z) ){ z++; }
165729 return *z!=0;
165730}
165731
165732/* Like mgt0 above except we are looking for a value of m which is
165733** exactly 1
165734*/
165735static int m_eq_1(const char *z){
165736 while( isVowel(z) ){ z++; }
165737 if( *z==0 ) return 0;
165738 while( isConsonant(z) ){ z++; }
165739 if( *z==0 ) return 0;
165740 while( isVowel(z) ){ z++; }
165741 if( *z==0 ) return 1;
165742 while( isConsonant(z) ){ z++; }
165743 return *z==0;
165744}
165745
165746/* Like mgt0 above except we are looking for a value of m>1 instead
165747** or m>0
165748*/
165749static int m_gt_1(const char *z){
165750 while( isVowel(z) ){ z++; }
165751 if( *z==0 ) return 0;
165752 while( isConsonant(z) ){ z++; }
165753 if( *z==0 ) return 0;
165754 while( isVowel(z) ){ z++; }
165755 if( *z==0 ) return 0;
165756 while( isConsonant(z) ){ z++; }
165757 return *z!=0;
165758}
165759
165760/*
165761** Return TRUE if there is a vowel anywhere within z[0..n-1]
165762*/
165763static int hasVowel(const char *z){
165764 while( isConsonant(z) ){ z++; }
165765 return *z!=0;
165766}
165767
165768/*
165769** Return TRUE if the word ends in a double consonant.
165770**
165771** The text is reversed here. So we are really looking at
165772** the first two characters of z[].
165773*/
165774static int doubleConsonant(const char *z){
165775 return isConsonant(z) && z[0]==z[1];
165776}
165777
165778/*
165779** Return TRUE if the word ends with three letters which
165780** are consonant-vowel-consonent and where the final consonant
165781** is not 'w', 'x', or 'y'.
165782**
165783** The word is reversed here. So we are really checking the
165784** first three letters and the first one cannot be in [wxy].
165785*/
165786static int star_oh(const char *z){
165787 return
165788 isConsonant(z) &&
165789 z[0]!='w' && z[0]!='x' && z[0]!='y' &&
165790 isVowel(z+1) &&
165791 isConsonant(z+2);
165792}
165793
165794/*
165795** If the word ends with zFrom and xCond() is true for the stem
165796** of the word that preceeds the zFrom ending, then change the
165797** ending to zTo.
165798**
165799** The input word *pz and zFrom are both in reverse order. zTo
165800** is in normal order.
165801**
165802** Return TRUE if zFrom matches. Return FALSE if zFrom does not
165803** match. Not that TRUE is returned even if xCond() fails and
165804** no substitution occurs.
165805*/
165806static int stem(
165807 char **pz, /* The word being stemmed (Reversed) */
165808 const char *zFrom, /* If the ending matches this... (Reversed) */
165809 const char *zTo, /* ... change the ending to this (not reversed) */
165810 int (*xCond)(const char*) /* Condition that must be true */
165811){
165812 char *z = *pz;
165813 while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
165814 if( *zFrom!=0 ) return 0;
165815 if( xCond && !xCond(z) ) return 1;
165816 while( *zTo ){
165817 *(--z) = *(zTo++);
165818 }
165819 *pz = z;
165820 return 1;
165821}
165822
165823/*
165824** This is the fallback stemmer used when the porter stemmer is
165825** inappropriate. The input word is copied into the output with
165826** US-ASCII case folding. If the input word is too long (more
165827** than 20 bytes if it contains no digits or more than 6 bytes if
165828** it contains digits) then word is truncated to 20 or 6 bytes
165829** by taking 10 or 3 bytes from the beginning and end.
165830*/
165831static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
165832 int i, mx, j;
165833 int hasDigit = 0;
165834 for(i=0; i<nIn; i++){
165835 char c = zIn[i];
165836 if( c>='A' && c<='Z' ){
165837 zOut[i] = c - 'A' + 'a';
165838 }else{
165839 if( c>='0' && c<='9' ) hasDigit = 1;
165840 zOut[i] = c;
165841 }
165842 }
165843 mx = hasDigit ? 3 : 10;
165844 if( nIn>mx*2 ){
165845 for(j=mx, i=nIn-mx; i<nIn; i++, j++){
165846 zOut[j] = zOut[i];
165847 }
165848 i = j;
165849 }
165850 zOut[i] = 0;
165851 *pnOut = i;
165852}
165853
165854
165855/*
165856** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
165857** zOut is at least big enough to hold nIn bytes. Write the actual
165858** size of the output word (exclusive of the '\0' terminator) into *pnOut.
165859**
165860** Any upper-case characters in the US-ASCII character set ([A-Z])
165861** are converted to lower case. Upper-case UTF characters are
165862** unchanged.
165863**
165864** Words that are longer than about 20 bytes are stemmed by retaining
165865** a few bytes from the beginning and the end of the word. If the
165866** word contains digits, 3 bytes are taken from the beginning and
165867** 3 bytes from the end. For long words without digits, 10 bytes
165868** are taken from each end. US-ASCII case folding still applies.
165869**
165870** If the input word contains not digits but does characters not
165871** in [a-zA-Z] then no stemming is attempted and this routine just
165872** copies the input into the input into the output with US-ASCII
165873** case folding.
165874**
165875** Stemming never increases the length of the word. So there is
165876** no chance of overflowing the zOut buffer.
165877*/
165878static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
165879 int i, j;
165880 char zReverse[28];
165881 char *z, *z2;
165882 if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
165883 /* The word is too big or too small for the porter stemmer.
165884 ** Fallback to the copy stemmer */
165885 copy_stemmer(zIn, nIn, zOut, pnOut);
165886 return;
165887 }
165888 for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
165889 char c = zIn[i];
165890 if( c>='A' && c<='Z' ){
165891 zReverse[j] = c + 'a' - 'A';
165892 }else if( c>='a' && c<='z' ){
165893 zReverse[j] = c;
165894 }else{
165895 /* The use of a character not in [a-zA-Z] means that we fallback
165896 ** to the copy stemmer */
165897 copy_stemmer(zIn, nIn, zOut, pnOut);
165898 return;
165899 }
165900 }
165901 memset(&zReverse[sizeof(zReverse)-5], 0, 5);
165902 z = &zReverse[j+1];
165903
165904
165905 /* Step 1a */
165906 if( z[0]=='s' ){
165907 if(
165908 !stem(&z, "sess", "ss", 0) &&
165909 !stem(&z, "sei", "i", 0) &&
165910 !stem(&z, "ss", "ss", 0)
165911 ){
165912 z++;
165913 }
165914 }
165915
165916 /* Step 1b */
165917 z2 = z;
165918 if( stem(&z, "dee", "ee", m_gt_0) ){
165919 /* Do nothing. The work was all in the test */
165920 }else if(
165921 (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
165922 && z!=z2
165923 ){
165924 if( stem(&z, "ta", "ate", 0) ||
165925 stem(&z, "lb", "ble", 0) ||
165926 stem(&z, "zi", "ize", 0) ){
165927 /* Do nothing. The work was all in the test */
165928 }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
165929 z++;
165930 }else if( m_eq_1(z) && star_oh(z) ){
165931 *(--z) = 'e';
165932 }
165933 }
165934
165935 /* Step 1c */
165936 if( z[0]=='y' && hasVowel(z+1) ){
165937 z[0] = 'i';
165938 }
165939
165940 /* Step 2 */
165941 switch( z[1] ){
165942 case 'a':
165943 if( !stem(&z, "lanoita", "ate", m_gt_0) ){
165944 stem(&z, "lanoit", "tion", m_gt_0);
165945 }
165946 break;
165947 case 'c':
165948 if( !stem(&z, "icne", "ence", m_gt_0) ){
165949 stem(&z, "icna", "ance", m_gt_0);
165950 }
165951 break;
165952 case 'e':
165953 stem(&z, "rezi", "ize", m_gt_0);
165954 break;
165955 case 'g':
165956 stem(&z, "igol", "log", m_gt_0);
165957 break;
165958 case 'l':
165959 if( !stem(&z, "ilb", "ble", m_gt_0)
165960 && !stem(&z, "illa", "al", m_gt_0)
165961 && !stem(&z, "iltne", "ent", m_gt_0)
165962 && !stem(&z, "ile", "e", m_gt_0)
165963 ){
165964 stem(&z, "ilsuo", "ous", m_gt_0);
165965 }
165966 break;
165967 case 'o':
165968 if( !stem(&z, "noitazi", "ize", m_gt_0)
165969 && !stem(&z, "noita", "ate", m_gt_0)
165970 ){
165971 stem(&z, "rota", "ate", m_gt_0);
165972 }
165973 break;
165974 case 's':
165975 if( !stem(&z, "msila", "al", m_gt_0)
165976 && !stem(&z, "ssenevi", "ive", m_gt_0)
165977 && !stem(&z, "ssenluf", "ful", m_gt_0)
165978 ){
165979 stem(&z, "ssensuo", "ous", m_gt_0);
165980 }
165981 break;
165982 case 't':
165983 if( !stem(&z, "itila", "al", m_gt_0)
165984 && !stem(&z, "itivi", "ive", m_gt_0)
165985 ){
165986 stem(&z, "itilib", "ble", m_gt_0);
165987 }
165988 break;
165989 }
165990
165991 /* Step 3 */
165992 switch( z[0] ){
165993 case 'e':
165994 if( !stem(&z, "etaci", "ic", m_gt_0)
165995 && !stem(&z, "evita", "", m_gt_0)
165996 ){
165997 stem(&z, "ezila", "al", m_gt_0);
165998 }
165999 break;
166000 case 'i':
166001 stem(&z, "itici", "ic", m_gt_0);
166002 break;
166003 case 'l':
166004 if( !stem(&z, "laci", "ic", m_gt_0) ){
166005 stem(&z, "luf", "", m_gt_0);
166006 }
166007 break;
166008 case 's':
166009 stem(&z, "ssen", "", m_gt_0);
166010 break;
166011 }
166012
166013 /* Step 4 */
166014 switch( z[1] ){
166015 case 'a':
166016 if( z[0]=='l' && m_gt_1(z+2) ){
166017 z += 2;
166018 }
166019 break;
166020 case 'c':
166021 if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){
166022 z += 4;
166023 }
166024 break;
166025 case 'e':
166026 if( z[0]=='r' && m_gt_1(z+2) ){
166027 z += 2;
166028 }
166029 break;
166030 case 'i':
166031 if( z[0]=='c' && m_gt_1(z+2) ){
166032 z += 2;
166033 }
166034 break;
166035 case 'l':
166036 if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
166037 z += 4;
166038 }
166039 break;
166040 case 'n':
166041 if( z[0]=='t' ){
166042 if( z[2]=='a' ){
166043 if( m_gt_1(z+3) ){
166044 z += 3;
166045 }
166046 }else if( z[2]=='e' ){
166047 if( !stem(&z, "tneme", "", m_gt_1)
166048 && !stem(&z, "tnem", "", m_gt_1)
166049 ){
166050 stem(&z, "tne", "", m_gt_1);
166051 }
166052 }
166053 }
166054 break;
166055 case 'o':
166056 if( z[0]=='u' ){
166057 if( m_gt_1(z+2) ){
166058 z += 2;
166059 }
166060 }else if( z[3]=='s' || z[3]=='t' ){
166061 stem(&z, "noi", "", m_gt_1);
166062 }
166063 break;
166064 case 's':
166065 if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
166066 z += 3;
166067 }
166068 break;
166069 case 't':
166070 if( !stem(&z, "eta", "", m_gt_1) ){
166071 stem(&z, "iti", "", m_gt_1);
166072 }
166073 break;
166074 case 'u':
166075 if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
166076 z += 3;
166077 }
166078 break;
166079 case 'v':
166080 case 'z':
166081 if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
166082 z += 3;
166083 }
166084 break;
166085 }
166086
166087 /* Step 5a */
166088 if( z[0]=='e' ){
166089 if( m_gt_1(z+1) ){
166090 z++;
166091 }else if( m_eq_1(z+1) && !star_oh(z+1) ){
166092 z++;
166093 }
166094 }
166095
166096 /* Step 5b */
166097 if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
166098 z++;
166099 }
166100
166101 /* z[] is now the stemmed word in reverse order. Flip it back
166102 ** around into forward order and return.
166103 */
166104 *pnOut = i = (int)strlen(z);
166105 zOut[i] = 0;
166106 while( *z ){
166107 zOut[--i] = *(z++);
166108 }
166109}
166110
166111/*
166112** Characters that can be part of a token. We assume any character
166113** whose value is greater than 0x80 (any UTF character) can be
166114** part of a token. In other words, delimiters all must have
166115** values of 0x7f or lower.
166116*/
166117static const char porterIdChar[] = {
166118/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
166119 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
166120 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
166121 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
166122 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
166123 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
166124};
166125#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
166126
166127/*
166128** Extract the next token from a tokenization cursor. The cursor must
166129** have been opened by a prior call to porterOpen().
166130*/
166131static int porterNext(
166132 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */
166133 const char **pzToken, /* OUT: *pzToken is the token text */
166134 int *pnBytes, /* OUT: Number of bytes in token */
166135 int *piStartOffset, /* OUT: Starting offset of token */
166136 int *piEndOffset, /* OUT: Ending offset of token */
166137 int *piPosition /* OUT: Position integer of token */
166138){
166139 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
166140 const char *z = c->zInput;
166141
166142 while( c->iOffset<c->nInput ){
166143 int iStartOffset, ch;
166144
166145 /* Scan past delimiter characters */
166146 while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
166147 c->iOffset++;
166148 }
166149
166150 /* Count non-delimiter characters. */
166151 iStartOffset = c->iOffset;
166152 while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
166153 c->iOffset++;
166154 }
166155
166156 if( c->iOffset>iStartOffset ){
166157 int n = c->iOffset-iStartOffset;
166158 if( n>c->nAllocated ){
166159 char *pNew;
166160 c->nAllocated = n+20;
166161 pNew = sqlite3_realloc(c->zToken, c->nAllocated);
166162 if( !pNew ) return SQLITE_NOMEM;
166163 c->zToken = pNew;
166164 }
166165 porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
166166 *pzToken = c->zToken;
166167 *piStartOffset = iStartOffset;
166168 *piEndOffset = c->iOffset;
166169 *piPosition = c->iToken++;
166170 return SQLITE_OK;
166171 }
166172 }
166173 return SQLITE_DONE;
166174}
166175
166176/*
166177** The set of routines that implement the porter-stemmer tokenizer
166178*/
166179static const sqlite3_tokenizer_module porterTokenizerModule = {
166180 0,
166181 porterCreate,
166182 porterDestroy,
166183 porterOpen,
166184 porterClose,
166185 porterNext,
166186 0
166187};
166188
166189/*
166190** Allocate a new porter tokenizer. Return a pointer to the new
166191** tokenizer in *ppModule
166192*/
166193SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
166194 sqlite3_tokenizer_module const**ppModule
166195){
166196 *ppModule = &porterTokenizerModule;
166197}
166198
166199#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
166200
166201/************** End of fts3_porter.c *****************************************/
166202/************** Begin file fts3_tokenizer.c **********************************/
166203/*
166204** 2007 June 22
166205**
166206** The author disclaims copyright to this source code. In place of
166207** a legal notice, here is a blessing:
166208**
166209** May you do good and not evil.
166210** May you find forgiveness for yourself and forgive others.
166211** May you share freely, never taking more than you give.
166212**
166213******************************************************************************
166214**
166215** This is part of an SQLite module implementing full-text search.
166216** This particular file implements the generic tokenizer interface.
166217*/
166218
166219/*
166220** The code in this file is only compiled if:
166221**
166222** * The FTS3 module is being built as an extension
166223** (in which case SQLITE_CORE is not defined), or
166224**
166225** * The FTS3 module is being built into the core of
166226** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
166227*/
166228/* #include "fts3Int.h" */
166229#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
166230
166231/* #include <assert.h> */
166232/* #include <string.h> */
166233
166234/*
166235** Return true if the two-argument version of fts3_tokenizer()
166236** has been activated via a prior call to sqlite3_db_config(db,
166237** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
166238*/
166239static int fts3TokenizerEnabled(sqlite3_context *context){
166240 sqlite3 *db = sqlite3_context_db_handle(context);
166241 int isEnabled = 0;
166242 sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,-1,&isEnabled);
166243 return isEnabled;
166244}
166245
166246/*
166247** Implementation of the SQL scalar function for accessing the underlying
166248** hash table. This function may be called as follows:
166249**
166250** SELECT <function-name>(<key-name>);
166251** SELECT <function-name>(<key-name>, <pointer>);
166252**
166253** where <function-name> is the name passed as the second argument
166254** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
166255**
166256** If the <pointer> argument is specified, it must be a blob value
166257** containing a pointer to be stored as the hash data corresponding
166258** to the string <key-name>. If <pointer> is not specified, then
166259** the string <key-name> must already exist in the has table. Otherwise,
166260** an error is returned.
166261**
166262** Whether or not the <pointer> argument is specified, the value returned
166263** is a blob containing the pointer stored as the hash data corresponding
166264** to string <key-name> (after the hash-table is updated, if applicable).
166265*/
166266static void fts3TokenizerFunc(
166267 sqlite3_context *context,
166268 int argc,
166269 sqlite3_value **argv
166270){
166271 Fts3Hash *pHash;
166272 void *pPtr = 0;
166273 const unsigned char *zName;
166274 int nName;
166275
166276 assert( argc==1 || argc==2 );
166277
166278 pHash = (Fts3Hash *)sqlite3_user_data(context);
166279
166280 zName = sqlite3_value_text(argv[0]);
166281 nName = sqlite3_value_bytes(argv[0])+1;
166282
166283 if( argc==2 ){
166284 if( fts3TokenizerEnabled(context) ){
166285 void *pOld;
166286 int n = sqlite3_value_bytes(argv[1]);
166287 if( zName==0 || n!=sizeof(pPtr) ){
166288 sqlite3_result_error(context, "argument type mismatch", -1);
166289 return;
166290 }
166291 pPtr = *(void **)sqlite3_value_blob(argv[1]);
166292 pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
166293 if( pOld==pPtr ){
166294 sqlite3_result_error(context, "out of memory", -1);
166295 }
166296 }else{
166297 sqlite3_result_error(context, "fts3tokenize disabled", -1);
166298 return;
166299 }
166300 }else{
166301 if( zName ){
166302 pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
166303 }
166304 if( !pPtr ){
166305 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
166306 sqlite3_result_error(context, zErr, -1);
166307 sqlite3_free(zErr);
166308 return;
166309 }
166310 }
166311 sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
166312}
166313
166314SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
166315 static const char isFtsIdChar[] = {
166316 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
166317 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
166318 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
166319 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
166320 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
166321 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
166322 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
166323 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
166324 };
166325 return (c&0x80 || isFtsIdChar[(int)(c)]);
166326}
166327
166328SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
166329 const char *z1;
166330 const char *z2 = 0;
166331
166332 /* Find the start of the next token. */
166333 z1 = zStr;
166334 while( z2==0 ){
166335 char c = *z1;
166336 switch( c ){
166337 case '\0': return 0; /* No more tokens here */
166338 case '\'':
166339 case '"':
166340 case '`': {
166341 z2 = z1;
166342 while( *++z2 && (*z2!=c || *++z2==c) );
166343 break;
166344 }
166345 case '[':
166346 z2 = &z1[1];
166347 while( *z2 && z2[0]!=']' ) z2++;
166348 if( *z2 ) z2++;
166349 break;
166350
166351 default:
166352 if( sqlite3Fts3IsIdChar(*z1) ){
166353 z2 = &z1[1];
166354 while( sqlite3Fts3IsIdChar(*z2) ) z2++;
166355 }else{
166356 z1++;
166357 }
166358 }
166359 }
166360
166361 *pn = (int)(z2-z1);
166362 return z1;
166363}
166364
166365SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
166366 Fts3Hash *pHash, /* Tokenizer hash table */
166367 const char *zArg, /* Tokenizer name */
166368 sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */
166369 char **pzErr /* OUT: Set to malloced error message */
166370){
166371 int rc;
166372 char *z = (char *)zArg;
166373 int n = 0;
166374 char *zCopy;
166375 char *zEnd; /* Pointer to nul-term of zCopy */
166376 sqlite3_tokenizer_module *m;
166377
166378 zCopy = sqlite3_mprintf("%s", zArg);
166379 if( !zCopy ) return SQLITE_NOMEM;
166380 zEnd = &zCopy[strlen(zCopy)];
166381
166382 z = (char *)sqlite3Fts3NextToken(zCopy, &n);
166383 if( z==0 ){
166384 assert( n==0 );
166385 z = zCopy;
166386 }
166387 z[n] = '\0';
166388 sqlite3Fts3Dequote(z);
166389
166390 m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
166391 if( !m ){
166392 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z);
166393 rc = SQLITE_ERROR;
166394 }else{
166395 char const **aArg = 0;
166396 int iArg = 0;
166397 z = &z[n+1];
166398 while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
166399 int nNew = sizeof(char *)*(iArg+1);
166400 char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
166401 if( !aNew ){
166402 sqlite3_free(zCopy);
166403 sqlite3_free((void *)aArg);
166404 return SQLITE_NOMEM;
166405 }
166406 aArg = aNew;
166407 aArg[iArg++] = z;
166408 z[n] = '\0';
166409 sqlite3Fts3Dequote(z);
166410 z = &z[n+1];
166411 }
166412 rc = m->xCreate(iArg, aArg, ppTok);
166413 assert( rc!=SQLITE_OK || *ppTok );
166414 if( rc!=SQLITE_OK ){
166415 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer");
166416 }else{
166417 (*ppTok)->pModule = m;
166418 }
166419 sqlite3_free((void *)aArg);
166420 }
166421
166422 sqlite3_free(zCopy);
166423 return rc;
166424}
166425
166426
166427#ifdef SQLITE_TEST
166428
166429#if defined(INCLUDE_SQLITE_TCL_H)
166430# include "sqlite_tcl.h"
166431#else
166432# include "tcl.h"
166433#endif
166434/* #include <string.h> */
166435
166436/*
166437** Implementation of a special SQL scalar function for testing tokenizers
166438** designed to be used in concert with the Tcl testing framework. This
166439** function must be called with two or more arguments:
166440**
166441** SELECT <function-name>(<key-name>, ..., <input-string>);
166442**
166443** where <function-name> is the name passed as the second argument
166444** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
166445** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
166446**
166447** The return value is a string that may be interpreted as a Tcl
166448** list. For each token in the <input-string>, three elements are
166449** added to the returned list. The first is the token position, the
166450** second is the token text (folded, stemmed, etc.) and the third is the
166451** substring of <input-string> associated with the token. For example,
166452** using the built-in "simple" tokenizer:
166453**
166454** SELECT fts_tokenizer_test('simple', 'I don't see how');
166455**
166456** will return the string:
166457**
166458** "{0 i I 1 dont don't 2 see see 3 how how}"
166459**
166460*/
166461static void testFunc(
166462 sqlite3_context *context,
166463 int argc,
166464 sqlite3_value **argv
166465){
166466 Fts3Hash *pHash;
166467 sqlite3_tokenizer_module *p;
166468 sqlite3_tokenizer *pTokenizer = 0;
166469 sqlite3_tokenizer_cursor *pCsr = 0;
166470
166471 const char *zErr = 0;
166472
166473 const char *zName;
166474 int nName;
166475 const char *zInput;
166476 int nInput;
166477
166478 const char *azArg[64];
166479
166480 const char *zToken;
166481 int nToken = 0;
166482 int iStart = 0;
166483 int iEnd = 0;
166484 int iPos = 0;
166485 int i;
166486
166487 Tcl_Obj *pRet;
166488
166489 if( argc<2 ){
166490 sqlite3_result_error(context, "insufficient arguments", -1);
166491 return;
166492 }
166493
166494 nName = sqlite3_value_bytes(argv[0]);
166495 zName = (const char *)sqlite3_value_text(argv[0]);
166496 nInput = sqlite3_value_bytes(argv[argc-1]);
166497 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
166498
166499 pHash = (Fts3Hash *)sqlite3_user_data(context);
166500 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
166501
166502 if( !p ){
166503 char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName);
166504 sqlite3_result_error(context, zErr2, -1);
166505 sqlite3_free(zErr2);
166506 return;
166507 }
166508
166509 pRet = Tcl_NewObj();
166510 Tcl_IncrRefCount(pRet);
166511
166512 for(i=1; i<argc-1; i++){
166513 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
166514 }
166515
166516 if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
166517 zErr = "error in xCreate()";
166518 goto finish;
166519 }
166520 pTokenizer->pModule = p;
166521 if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
166522 zErr = "error in xOpen()";
166523 goto finish;
166524 }
166525
166526 while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
166527 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
166528 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
166529 zToken = &zInput[iStart];
166530 nToken = iEnd-iStart;
166531 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
166532 }
166533
166534 if( SQLITE_OK!=p->xClose(pCsr) ){
166535 zErr = "error in xClose()";
166536 goto finish;
166537 }
166538 if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
166539 zErr = "error in xDestroy()";
166540 goto finish;
166541 }
166542
166543finish:
166544 if( zErr ){
166545 sqlite3_result_error(context, zErr, -1);
166546 }else{
166547 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
166548 }
166549 Tcl_DecrRefCount(pRet);
166550}
166551
166552static
166553int registerTokenizer(
166554 sqlite3 *db,
166555 char *zName,
166556 const sqlite3_tokenizer_module *p
166557){
166558 int rc;
166559 sqlite3_stmt *pStmt;
166560 const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
166561
166562 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
166563 if( rc!=SQLITE_OK ){
166564 return rc;
166565 }
166566
166567 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
166568 sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
166569 sqlite3_step(pStmt);
166570
166571 return sqlite3_finalize(pStmt);
166572}
166573
166574
166575static
166576int queryTokenizer(
166577 sqlite3 *db,
166578 char *zName,
166579 const sqlite3_tokenizer_module **pp
166580){
166581 int rc;
166582 sqlite3_stmt *pStmt;
166583 const char zSql[] = "SELECT fts3_tokenizer(?)";
166584
166585 *pp = 0;
166586 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
166587 if( rc!=SQLITE_OK ){
166588 return rc;
166589 }
166590
166591 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
166592 if( SQLITE_ROW==sqlite3_step(pStmt) ){
166593 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
166594 memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
166595 }
166596 }
166597
166598 return sqlite3_finalize(pStmt);
166599}
166600
166601SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
166602
166603/*
166604** Implementation of the scalar function fts3_tokenizer_internal_test().
166605** This function is used for testing only, it is not included in the
166606** build unless SQLITE_TEST is defined.
166607**
166608** The purpose of this is to test that the fts3_tokenizer() function
166609** can be used as designed by the C-code in the queryTokenizer and
166610** registerTokenizer() functions above. These two functions are repeated
166611** in the README.tokenizer file as an example, so it is important to
166612** test them.
166613**
166614** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
166615** function with no arguments. An assert() will fail if a problem is
166616** detected. i.e.:
166617**
166618** SELECT fts3_tokenizer_internal_test();
166619**
166620*/
166621static void intTestFunc(
166622 sqlite3_context *context,
166623 int argc,
166624 sqlite3_value **argv
166625){
166626 int rc;
166627 const sqlite3_tokenizer_module *p1;
166628 const sqlite3_tokenizer_module *p2;
166629 sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
166630
166631 UNUSED_PARAMETER(argc);
166632 UNUSED_PARAMETER(argv);
166633
166634 /* Test the query function */
166635 sqlite3Fts3SimpleTokenizerModule(&p1);
166636 rc = queryTokenizer(db, "simple", &p2);
166637 assert( rc==SQLITE_OK );
166638 assert( p1==p2 );
166639 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
166640 assert( rc==SQLITE_ERROR );
166641 assert( p2==0 );
166642 assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
166643
166644 /* Test the storage function */
166645 if( fts3TokenizerEnabled(context) ){
166646 rc = registerTokenizer(db, "nosuchtokenizer", p1);
166647 assert( rc==SQLITE_OK );
166648 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
166649 assert( rc==SQLITE_OK );
166650 assert( p2==p1 );
166651 }
166652
166653 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
166654}
166655
166656#endif
166657
166658/*
166659** Set up SQL objects in database db used to access the contents of
166660** the hash table pointed to by argument pHash. The hash table must
166661** been initialized to use string keys, and to take a private copy
166662** of the key when a value is inserted. i.e. by a call similar to:
166663**
166664** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
166665**
166666** This function adds a scalar function (see header comment above
166667** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is
166668** defined at compilation time, a temporary virtual table (see header
166669** comment above struct HashTableVtab) to the database schema. Both
166670** provide read/write access to the contents of *pHash.
166671**
166672** The third argument to this function, zName, is used as the name
166673** of both the scalar and, if created, the virtual table.
166674*/
166675SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
166676 sqlite3 *db,
166677 Fts3Hash *pHash,
166678 const char *zName
166679){
166680 int rc = SQLITE_OK;
166681 void *p = (void *)pHash;
166682 const int any = SQLITE_ANY;
166683
166684#ifdef SQLITE_TEST
166685 char *zTest = 0;
166686 char *zTest2 = 0;
166687 void *pdb = (void *)db;
166688 zTest = sqlite3_mprintf("%s_test", zName);
166689 zTest2 = sqlite3_mprintf("%s_internal_test", zName);
166690 if( !zTest || !zTest2 ){
166691 rc = SQLITE_NOMEM;
166692 }
166693#endif
166694
166695 if( SQLITE_OK==rc ){
166696 rc = sqlite3_create_function(db, zName, 1, any, p, fts3TokenizerFunc, 0, 0);
166697 }
166698 if( SQLITE_OK==rc ){
166699 rc = sqlite3_create_function(db, zName, 2, any, p, fts3TokenizerFunc, 0, 0);
166700 }
166701#ifdef SQLITE_TEST
166702 if( SQLITE_OK==rc ){
166703 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
166704 }
166705 if( SQLITE_OK==rc ){
166706 rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
166707 }
166708#endif
166709
166710#ifdef SQLITE_TEST
166711 sqlite3_free(zTest);
166712 sqlite3_free(zTest2);
166713#endif
166714
166715 return rc;
166716}
166717
166718#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
166719
166720/************** End of fts3_tokenizer.c **************************************/
166721/************** Begin file fts3_tokenizer1.c *********************************/
166722/*
166723** 2006 Oct 10
166724**
166725** The author disclaims copyright to this source code. In place of
166726** a legal notice, here is a blessing:
166727**
166728** May you do good and not evil.
166729** May you find forgiveness for yourself and forgive others.
166730** May you share freely, never taking more than you give.
166731**
166732******************************************************************************
166733**
166734** Implementation of the "simple" full-text-search tokenizer.
166735*/
166736
166737/*
166738** The code in this file is only compiled if:
166739**
166740** * The FTS3 module is being built as an extension
166741** (in which case SQLITE_CORE is not defined), or
166742**
166743** * The FTS3 module is being built into the core of
166744** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
166745*/
166746/* #include "fts3Int.h" */
166747#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
166748
166749/* #include <assert.h> */
166750/* #include <stdlib.h> */
166751/* #include <stdio.h> */
166752/* #include <string.h> */
166753
166754/* #include "fts3_tokenizer.h" */
166755
166756typedef struct simple_tokenizer {
166757 sqlite3_tokenizer base;
166758 char delim[128]; /* flag ASCII delimiters */
166759} simple_tokenizer;
166760
166761typedef struct simple_tokenizer_cursor {
166762 sqlite3_tokenizer_cursor base;
166763 const char *pInput; /* input we are tokenizing */
166764 int nBytes; /* size of the input */
166765 int iOffset; /* current position in pInput */
166766 int iToken; /* index of next token to be returned */
166767 char *pToken; /* storage for current token */
166768 int nTokenAllocated; /* space allocated to zToken buffer */
166769} simple_tokenizer_cursor;
166770
166771
166772static int simpleDelim(simple_tokenizer *t, unsigned char c){
166773 return c<0x80 && t->delim[c];
166774}
166775static int fts3_isalnum(int x){
166776 return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
166777}
166778
166779/*
166780** Create a new tokenizer instance.
166781*/
166782static int simpleCreate(
166783 int argc, const char * const *argv,
166784 sqlite3_tokenizer **ppTokenizer
166785){
166786 simple_tokenizer *t;
166787
166788 t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
166789 if( t==NULL ) return SQLITE_NOMEM;
166790 memset(t, 0, sizeof(*t));
166791
166792 /* TODO(shess) Delimiters need to remain the same from run to run,
166793 ** else we need to reindex. One solution would be a meta-table to
166794 ** track such information in the database, then we'd only want this
166795 ** information on the initial create.
166796 */
166797 if( argc>1 ){
166798 int i, n = (int)strlen(argv[1]);
166799 for(i=0; i<n; i++){
166800 unsigned char ch = argv[1][i];
166801 /* We explicitly don't support UTF-8 delimiters for now. */
166802 if( ch>=0x80 ){
166803 sqlite3_free(t);
166804 return SQLITE_ERROR;
166805 }
166806 t->delim[ch] = 1;
166807 }
166808 } else {
166809 /* Mark non-alphanumeric ASCII characters as delimiters */
166810 int i;
166811 for(i=1; i<0x80; i++){
166812 t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
166813 }
166814 }
166815
166816 *ppTokenizer = &t->base;
166817 return SQLITE_OK;
166818}
166819
166820/*
166821** Destroy a tokenizer
166822*/
166823static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
166824 sqlite3_free(pTokenizer);
166825 return SQLITE_OK;
166826}
166827
166828/*
166829** Prepare to begin tokenizing a particular string. The input
166830** string to be tokenized is pInput[0..nBytes-1]. A cursor
166831** used to incrementally tokenize this string is returned in
166832** *ppCursor.
166833*/
166834static int simpleOpen(
166835 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
166836 const char *pInput, int nBytes, /* String to be tokenized */
166837 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
166838){
166839 simple_tokenizer_cursor *c;
166840
166841 UNUSED_PARAMETER(pTokenizer);
166842
166843 c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
166844 if( c==NULL ) return SQLITE_NOMEM;
166845
166846 c->pInput = pInput;
166847 if( pInput==0 ){
166848 c->nBytes = 0;
166849 }else if( nBytes<0 ){
166850 c->nBytes = (int)strlen(pInput);
166851 }else{
166852 c->nBytes = nBytes;
166853 }
166854 c->iOffset = 0; /* start tokenizing at the beginning */
166855 c->iToken = 0;
166856 c->pToken = NULL; /* no space allocated, yet. */
166857 c->nTokenAllocated = 0;
166858
166859 *ppCursor = &c->base;
166860 return SQLITE_OK;
166861}
166862
166863/*
166864** Close a tokenization cursor previously opened by a call to
166865** simpleOpen() above.
166866*/
166867static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
166868 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
166869 sqlite3_free(c->pToken);
166870 sqlite3_free(c);
166871 return SQLITE_OK;
166872}
166873
166874/*
166875** Extract the next token from a tokenization cursor. The cursor must
166876** have been opened by a prior call to simpleOpen().
166877*/
166878static int simpleNext(
166879 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
166880 const char **ppToken, /* OUT: *ppToken is the token text */
166881 int *pnBytes, /* OUT: Number of bytes in token */
166882 int *piStartOffset, /* OUT: Starting offset of token */
166883 int *piEndOffset, /* OUT: Ending offset of token */
166884 int *piPosition /* OUT: Position integer of token */
166885){
166886 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
166887 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
166888 unsigned char *p = (unsigned char *)c->pInput;
166889
166890 while( c->iOffset<c->nBytes ){
166891 int iStartOffset;
166892
166893 /* Scan past delimiter characters */
166894 while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
166895 c->iOffset++;
166896 }
166897
166898 /* Count non-delimiter characters. */
166899 iStartOffset = c->iOffset;
166900 while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
166901 c->iOffset++;
166902 }
166903
166904 if( c->iOffset>iStartOffset ){
166905 int i, n = c->iOffset-iStartOffset;
166906 if( n>c->nTokenAllocated ){
166907 char *pNew;
166908 c->nTokenAllocated = n+20;
166909 pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
166910 if( !pNew ) return SQLITE_NOMEM;
166911 c->pToken = pNew;
166912 }
166913 for(i=0; i<n; i++){
166914 /* TODO(shess) This needs expansion to handle UTF-8
166915 ** case-insensitivity.
166916 */
166917 unsigned char ch = p[iStartOffset+i];
166918 c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
166919 }
166920 *ppToken = c->pToken;
166921 *pnBytes = n;
166922 *piStartOffset = iStartOffset;
166923 *piEndOffset = c->iOffset;
166924 *piPosition = c->iToken++;
166925
166926 return SQLITE_OK;
166927 }
166928 }
166929 return SQLITE_DONE;
166930}
166931
166932/*
166933** The set of routines that implement the simple tokenizer
166934*/
166935static const sqlite3_tokenizer_module simpleTokenizerModule = {
166936 0,
166937 simpleCreate,
166938 simpleDestroy,
166939 simpleOpen,
166940 simpleClose,
166941 simpleNext,
166942 0,
166943};
166944
166945/*
166946** Allocate a new simple tokenizer. Return a pointer to the new
166947** tokenizer in *ppModule
166948*/
166949SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
166950 sqlite3_tokenizer_module const**ppModule
166951){
166952 *ppModule = &simpleTokenizerModule;
166953}
166954
166955#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
166956
166957/************** End of fts3_tokenizer1.c *************************************/
166958/************** Begin file fts3_tokenize_vtab.c ******************************/
166959/*
166960** 2013 Apr 22
166961**
166962** The author disclaims copyright to this source code. In place of
166963** a legal notice, here is a blessing:
166964**
166965** May you do good and not evil.
166966** May you find forgiveness for yourself and forgive others.
166967** May you share freely, never taking more than you give.
166968**
166969******************************************************************************
166970**
166971** This file contains code for the "fts3tokenize" virtual table module.
166972** An fts3tokenize virtual table is created as follows:
166973**
166974** CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
166975** <tokenizer-name>, <arg-1>, ...
166976** );
166977**
166978** The table created has the following schema:
166979**
166980** CREATE TABLE <tbl>(input, token, start, end, position)
166981**
166982** When queried, the query must include a WHERE clause of type:
166983**
166984** input = <string>
166985**
166986** The virtual table module tokenizes this <string>, using the FTS3
166987** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
166988** statement and returns one row for each token in the result. With
166989** fields set as follows:
166990**
166991** input: Always set to a copy of <string>
166992** token: A token from the input.
166993** start: Byte offset of the token within the input <string>.
166994** end: Byte offset of the byte immediately following the end of the
166995** token within the input string.
166996** pos: Token offset of token within input.
166997**
166998*/
166999/* #include "fts3Int.h" */
167000#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
167001
167002/* #include <string.h> */
167003/* #include <assert.h> */
167004
167005typedef struct Fts3tokTable Fts3tokTable;
167006typedef struct Fts3tokCursor Fts3tokCursor;
167007
167008/*
167009** Virtual table structure.
167010*/
167011struct Fts3tokTable {
167012 sqlite3_vtab base; /* Base class used by SQLite core */
167013 const sqlite3_tokenizer_module *pMod;
167014 sqlite3_tokenizer *pTok;
167015};
167016
167017/*
167018** Virtual table cursor structure.
167019*/
167020struct Fts3tokCursor {
167021 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
167022 char *zInput; /* Input string */
167023 sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
167024 int iRowid; /* Current 'rowid' value */
167025 const char *zToken; /* Current 'token' value */
167026 int nToken; /* Size of zToken in bytes */
167027 int iStart; /* Current 'start' value */
167028 int iEnd; /* Current 'end' value */
167029 int iPos; /* Current 'pos' value */
167030};
167031
167032/*
167033** Query FTS for the tokenizer implementation named zName.
167034*/
167035static int fts3tokQueryTokenizer(
167036 Fts3Hash *pHash,
167037 const char *zName,
167038 const sqlite3_tokenizer_module **pp,
167039 char **pzErr
167040){
167041 sqlite3_tokenizer_module *p;
167042 int nName = (int)strlen(zName);
167043
167044 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
167045 if( !p ){
167046 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName);
167047 return SQLITE_ERROR;
167048 }
167049
167050 *pp = p;
167051 return SQLITE_OK;
167052}
167053
167054/*
167055** The second argument, argv[], is an array of pointers to nul-terminated
167056** strings. This function makes a copy of the array and strings into a
167057** single block of memory. It then dequotes any of the strings that appear
167058** to be quoted.
167059**
167060** If successful, output parameter *pazDequote is set to point at the
167061** array of dequoted strings and SQLITE_OK is returned. The caller is
167062** responsible for eventually calling sqlite3_free() to free the array
167063** in this case. Or, if an error occurs, an SQLite error code is returned.
167064** The final value of *pazDequote is undefined in this case.
167065*/
167066static int fts3tokDequoteArray(
167067 int argc, /* Number of elements in argv[] */
167068 const char * const *argv, /* Input array */
167069 char ***pazDequote /* Output array */
167070){
167071 int rc = SQLITE_OK; /* Return code */
167072 if( argc==0 ){
167073 *pazDequote = 0;
167074 }else{
167075 int i;
167076 int nByte = 0;
167077 char **azDequote;
167078
167079 for(i=0; i<argc; i++){
167080 nByte += (int)(strlen(argv[i]) + 1);
167081 }
167082
167083 *pazDequote = azDequote = sqlite3_malloc(sizeof(char *)*argc + nByte);
167084 if( azDequote==0 ){
167085 rc = SQLITE_NOMEM;
167086 }else{
167087 char *pSpace = (char *)&azDequote[argc];
167088 for(i=0; i<argc; i++){
167089 int n = (int)strlen(argv[i]);
167090 azDequote[i] = pSpace;
167091 memcpy(pSpace, argv[i], n+1);
167092 sqlite3Fts3Dequote(pSpace);
167093 pSpace += (n+1);
167094 }
167095 }
167096 }
167097
167098 return rc;
167099}
167100
167101/*
167102** Schema of the tokenizer table.
167103*/
167104#define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
167105
167106/*
167107** This function does all the work for both the xConnect and xCreate methods.
167108** These tables have no persistent representation of their own, so xConnect
167109** and xCreate are identical operations.
167110**
167111** argv[0]: module name
167112** argv[1]: database name
167113** argv[2]: table name
167114** argv[3]: first argument (tokenizer name)
167115*/
167116static int fts3tokConnectMethod(
167117 sqlite3 *db, /* Database connection */
167118 void *pHash, /* Hash table of tokenizers */
167119 int argc, /* Number of elements in argv array */
167120 const char * const *argv, /* xCreate/xConnect argument array */
167121 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
167122 char **pzErr /* OUT: sqlite3_malloc'd error message */
167123){
167124 Fts3tokTable *pTab = 0;
167125 const sqlite3_tokenizer_module *pMod = 0;
167126 sqlite3_tokenizer *pTok = 0;
167127 int rc;
167128 char **azDequote = 0;
167129 int nDequote;
167130
167131 rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
167132 if( rc!=SQLITE_OK ) return rc;
167133
167134 nDequote = argc-3;
167135 rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
167136
167137 if( rc==SQLITE_OK ){
167138 const char *zModule;
167139 if( nDequote<1 ){
167140 zModule = "simple";
167141 }else{
167142 zModule = azDequote[0];
167143 }
167144 rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
167145 }
167146
167147 assert( (rc==SQLITE_OK)==(pMod!=0) );
167148 if( rc==SQLITE_OK ){
167149 const char * const *azArg = (const char * const *)&azDequote[1];
167150 rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
167151 }
167152
167153 if( rc==SQLITE_OK ){
167154 pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
167155 if( pTab==0 ){
167156 rc = SQLITE_NOMEM;
167157 }
167158 }
167159
167160 if( rc==SQLITE_OK ){
167161 memset(pTab, 0, sizeof(Fts3tokTable));
167162 pTab->pMod = pMod;
167163 pTab->pTok = pTok;
167164 *ppVtab = &pTab->base;
167165 }else{
167166 if( pTok ){
167167 pMod->xDestroy(pTok);
167168 }
167169 }
167170
167171 sqlite3_free(azDequote);
167172 return rc;
167173}
167174
167175/*
167176** This function does the work for both the xDisconnect and xDestroy methods.
167177** These tables have no persistent representation of their own, so xDisconnect
167178** and xDestroy are identical operations.
167179*/
167180static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
167181 Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
167182
167183 pTab->pMod->xDestroy(pTab->pTok);
167184 sqlite3_free(pTab);
167185 return SQLITE_OK;
167186}
167187
167188/*
167189** xBestIndex - Analyze a WHERE and ORDER BY clause.
167190*/
167191static int fts3tokBestIndexMethod(
167192 sqlite3_vtab *pVTab,
167193 sqlite3_index_info *pInfo
167194){
167195 int i;
167196 UNUSED_PARAMETER(pVTab);
167197
167198 for(i=0; i<pInfo->nConstraint; i++){
167199 if( pInfo->aConstraint[i].usable
167200 && pInfo->aConstraint[i].iColumn==0
167201 && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
167202 ){
167203 pInfo->idxNum = 1;
167204 pInfo->aConstraintUsage[i].argvIndex = 1;
167205 pInfo->aConstraintUsage[i].omit = 1;
167206 pInfo->estimatedCost = 1;
167207 return SQLITE_OK;
167208 }
167209 }
167210
167211 pInfo->idxNum = 0;
167212 assert( pInfo->estimatedCost>1000000.0 );
167213
167214 return SQLITE_OK;
167215}
167216
167217/*
167218** xOpen - Open a cursor.
167219*/
167220static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
167221 Fts3tokCursor *pCsr;
167222 UNUSED_PARAMETER(pVTab);
167223
167224 pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
167225 if( pCsr==0 ){
167226 return SQLITE_NOMEM;
167227 }
167228 memset(pCsr, 0, sizeof(Fts3tokCursor));
167229
167230 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
167231 return SQLITE_OK;
167232}
167233
167234/*
167235** Reset the tokenizer cursor passed as the only argument. As if it had
167236** just been returned by fts3tokOpenMethod().
167237*/
167238static void fts3tokResetCursor(Fts3tokCursor *pCsr){
167239 if( pCsr->pCsr ){
167240 Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
167241 pTab->pMod->xClose(pCsr->pCsr);
167242 pCsr->pCsr = 0;
167243 }
167244 sqlite3_free(pCsr->zInput);
167245 pCsr->zInput = 0;
167246 pCsr->zToken = 0;
167247 pCsr->nToken = 0;
167248 pCsr->iStart = 0;
167249 pCsr->iEnd = 0;
167250 pCsr->iPos = 0;
167251 pCsr->iRowid = 0;
167252}
167253
167254/*
167255** xClose - Close a cursor.
167256*/
167257static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
167258 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
167259
167260 fts3tokResetCursor(pCsr);
167261 sqlite3_free(pCsr);
167262 return SQLITE_OK;
167263}
167264
167265/*
167266** xNext - Advance the cursor to the next row, if any.
167267*/
167268static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
167269 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
167270 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
167271 int rc; /* Return code */
167272
167273 pCsr->iRowid++;
167274 rc = pTab->pMod->xNext(pCsr->pCsr,
167275 &pCsr->zToken, &pCsr->nToken,
167276 &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
167277 );
167278
167279 if( rc!=SQLITE_OK ){
167280 fts3tokResetCursor(pCsr);
167281 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
167282 }
167283
167284 return rc;
167285}
167286
167287/*
167288** xFilter - Initialize a cursor to point at the start of its data.
167289*/
167290static int fts3tokFilterMethod(
167291 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
167292 int idxNum, /* Strategy index */
167293 const char *idxStr, /* Unused */
167294 int nVal, /* Number of elements in apVal */
167295 sqlite3_value **apVal /* Arguments for the indexing scheme */
167296){
167297 int rc = SQLITE_ERROR;
167298 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
167299 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
167300 UNUSED_PARAMETER(idxStr);
167301 UNUSED_PARAMETER(nVal);
167302
167303 fts3tokResetCursor(pCsr);
167304 if( idxNum==1 ){
167305 const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
167306 int nByte = sqlite3_value_bytes(apVal[0]);
167307 pCsr->zInput = sqlite3_malloc(nByte+1);
167308 if( pCsr->zInput==0 ){
167309 rc = SQLITE_NOMEM;
167310 }else{
167311 memcpy(pCsr->zInput, zByte, nByte);
167312 pCsr->zInput[nByte] = 0;
167313 rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
167314 if( rc==SQLITE_OK ){
167315 pCsr->pCsr->pTokenizer = pTab->pTok;
167316 }
167317 }
167318 }
167319
167320 if( rc!=SQLITE_OK ) return rc;
167321 return fts3tokNextMethod(pCursor);
167322}
167323
167324/*
167325** xEof - Return true if the cursor is at EOF, or false otherwise.
167326*/
167327static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
167328 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
167329 return (pCsr->zToken==0);
167330}
167331
167332/*
167333** xColumn - Return a column value.
167334*/
167335static int fts3tokColumnMethod(
167336 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
167337 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
167338 int iCol /* Index of column to read value from */
167339){
167340 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
167341
167342 /* CREATE TABLE x(input, token, start, end, position) */
167343 switch( iCol ){
167344 case 0:
167345 sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
167346 break;
167347 case 1:
167348 sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
167349 break;
167350 case 2:
167351 sqlite3_result_int(pCtx, pCsr->iStart);
167352 break;
167353 case 3:
167354 sqlite3_result_int(pCtx, pCsr->iEnd);
167355 break;
167356 default:
167357 assert( iCol==4 );
167358 sqlite3_result_int(pCtx, pCsr->iPos);
167359 break;
167360 }
167361 return SQLITE_OK;
167362}
167363
167364/*
167365** xRowid - Return the current rowid for the cursor.
167366*/
167367static int fts3tokRowidMethod(
167368 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
167369 sqlite_int64 *pRowid /* OUT: Rowid value */
167370){
167371 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
167372 *pRowid = (sqlite3_int64)pCsr->iRowid;
167373 return SQLITE_OK;
167374}
167375
167376/*
167377** Register the fts3tok module with database connection db. Return SQLITE_OK
167378** if successful or an error code if sqlite3_create_module() fails.
167379*/
167380SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
167381 static const sqlite3_module fts3tok_module = {
167382 0, /* iVersion */
167383 fts3tokConnectMethod, /* xCreate */
167384 fts3tokConnectMethod, /* xConnect */
167385 fts3tokBestIndexMethod, /* xBestIndex */
167386 fts3tokDisconnectMethod, /* xDisconnect */
167387 fts3tokDisconnectMethod, /* xDestroy */
167388 fts3tokOpenMethod, /* xOpen */
167389 fts3tokCloseMethod, /* xClose */
167390 fts3tokFilterMethod, /* xFilter */
167391 fts3tokNextMethod, /* xNext */
167392 fts3tokEofMethod, /* xEof */
167393 fts3tokColumnMethod, /* xColumn */
167394 fts3tokRowidMethod, /* xRowid */
167395 0, /* xUpdate */
167396 0, /* xBegin */
167397 0, /* xSync */
167398 0, /* xCommit */
167399 0, /* xRollback */
167400 0, /* xFindFunction */
167401 0, /* xRename */
167402 0, /* xSavepoint */
167403 0, /* xRelease */
167404 0 /* xRollbackTo */
167405 };
167406 int rc; /* Return code */
167407
167408 rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
167409 return rc;
167410}
167411
167412#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
167413
167414/************** End of fts3_tokenize_vtab.c **********************************/
167415/************** Begin file fts3_write.c **************************************/
167416/*
167417** 2009 Oct 23
167418**
167419** The author disclaims copyright to this source code. In place of
167420** a legal notice, here is a blessing:
167421**
167422** May you do good and not evil.
167423** May you find forgiveness for yourself and forgive others.
167424** May you share freely, never taking more than you give.
167425**
167426******************************************************************************
167427**
167428** This file is part of the SQLite FTS3 extension module. Specifically,
167429** this file contains code to insert, update and delete rows from FTS3
167430** tables. It also contains code to merge FTS3 b-tree segments. Some
167431** of the sub-routines used to merge segments are also used by the query
167432** code in fts3.c.
167433*/
167434
167435/* #include "fts3Int.h" */
167436#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
167437
167438/* #include <string.h> */
167439/* #include <assert.h> */
167440/* #include <stdlib.h> */
167441
167442
167443#define FTS_MAX_APPENDABLE_HEIGHT 16
167444
167445/*
167446** When full-text index nodes are loaded from disk, the buffer that they
167447** are loaded into has the following number of bytes of padding at the end
167448** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
167449** of 920 bytes is allocated for it.
167450**
167451** This means that if we have a pointer into a buffer containing node data,
167452** it is always safe to read up to two varints from it without risking an
167453** overread, even if the node data is corrupted.
167454*/
167455#define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
167456
167457/*
167458** Under certain circumstances, b-tree nodes (doclists) can be loaded into
167459** memory incrementally instead of all at once. This can be a big performance
167460** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
167461** method before retrieving all query results (as may happen, for example,
167462** if a query has a LIMIT clause).
167463**
167464** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
167465** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
167466** The code is written so that the hard lower-limit for each of these values
167467** is 1. Clearly such small values would be inefficient, but can be useful
167468** for testing purposes.
167469**
167470** If this module is built with SQLITE_TEST defined, these constants may
167471** be overridden at runtime for testing purposes. File fts3_test.c contains
167472** a Tcl interface to read and write the values.
167473*/
167474#ifdef SQLITE_TEST
167475int test_fts3_node_chunksize = (4*1024);
167476int test_fts3_node_chunk_threshold = (4*1024)*4;
167477# define FTS3_NODE_CHUNKSIZE test_fts3_node_chunksize
167478# define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
167479#else
167480# define FTS3_NODE_CHUNKSIZE (4*1024)
167481# define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
167482#endif
167483
167484/*
167485** The two values that may be meaningfully bound to the :1 parameter in
167486** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
167487*/
167488#define FTS_STAT_DOCTOTAL 0
167489#define FTS_STAT_INCRMERGEHINT 1
167490#define FTS_STAT_AUTOINCRMERGE 2
167491
167492/*
167493** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
167494** and incremental merge operation that takes place. This is used for
167495** debugging FTS only, it should not usually be turned on in production
167496** systems.
167497*/
167498#ifdef FTS3_LOG_MERGES
167499static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
167500 sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
167501}
167502#else
167503#define fts3LogMerge(x, y)
167504#endif
167505
167506
167507typedef struct PendingList PendingList;
167508typedef struct SegmentNode SegmentNode;
167509typedef struct SegmentWriter SegmentWriter;
167510
167511/*
167512** An instance of the following data structure is used to build doclists
167513** incrementally. See function fts3PendingListAppend() for details.
167514*/
167515struct PendingList {
167516 int nData;
167517 char *aData;
167518 int nSpace;
167519 sqlite3_int64 iLastDocid;
167520 sqlite3_int64 iLastCol;
167521 sqlite3_int64 iLastPos;
167522};
167523
167524
167525/*
167526** Each cursor has a (possibly empty) linked list of the following objects.
167527*/
167528struct Fts3DeferredToken {
167529 Fts3PhraseToken *pToken; /* Pointer to corresponding expr token */
167530 int iCol; /* Column token must occur in */
167531 Fts3DeferredToken *pNext; /* Next in list of deferred tokens */
167532 PendingList *pList; /* Doclist is assembled here */
167533};
167534
167535/*
167536** An instance of this structure is used to iterate through the terms on
167537** a contiguous set of segment b-tree leaf nodes. Although the details of
167538** this structure are only manipulated by code in this file, opaque handles
167539** of type Fts3SegReader* are also used by code in fts3.c to iterate through
167540** terms when querying the full-text index. See functions:
167541**
167542** sqlite3Fts3SegReaderNew()
167543** sqlite3Fts3SegReaderFree()
167544** sqlite3Fts3SegReaderIterate()
167545**
167546** Methods used to manipulate Fts3SegReader structures:
167547**
167548** fts3SegReaderNext()
167549** fts3SegReaderFirstDocid()
167550** fts3SegReaderNextDocid()
167551*/
167552struct Fts3SegReader {
167553 int iIdx; /* Index within level, or 0x7FFFFFFF for PT */
167554 u8 bLookup; /* True for a lookup only */
167555 u8 rootOnly; /* True for a root-only reader */
167556
167557 sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */
167558 sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */
167559 sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */
167560 sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */
167561
167562 char *aNode; /* Pointer to node data (or NULL) */
167563 int nNode; /* Size of buffer at aNode (or 0) */
167564 int nPopulate; /* If >0, bytes of buffer aNode[] loaded */
167565 sqlite3_blob *pBlob; /* If not NULL, blob handle to read node */
167566
167567 Fts3HashElem **ppNextElem;
167568
167569 /* Variables set by fts3SegReaderNext(). These may be read directly
167570 ** by the caller. They are valid from the time SegmentReaderNew() returns
167571 ** until SegmentReaderNext() returns something other than SQLITE_OK
167572 ** (i.e. SQLITE_DONE).
167573 */
167574 int nTerm; /* Number of bytes in current term */
167575 char *zTerm; /* Pointer to current term */
167576 int nTermAlloc; /* Allocated size of zTerm buffer */
167577 char *aDoclist; /* Pointer to doclist of current entry */
167578 int nDoclist; /* Size of doclist in current entry */
167579
167580 /* The following variables are used by fts3SegReaderNextDocid() to iterate
167581 ** through the current doclist (aDoclist/nDoclist).
167582 */
167583 char *pOffsetList;
167584 int nOffsetList; /* For descending pending seg-readers only */
167585 sqlite3_int64 iDocid;
167586};
167587
167588#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
167589#define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
167590
167591/*
167592** An instance of this structure is used to create a segment b-tree in the
167593** database. The internal details of this type are only accessed by the
167594** following functions:
167595**
167596** fts3SegWriterAdd()
167597** fts3SegWriterFlush()
167598** fts3SegWriterFree()
167599*/
167600struct SegmentWriter {
167601 SegmentNode *pTree; /* Pointer to interior tree structure */
167602 sqlite3_int64 iFirst; /* First slot in %_segments written */
167603 sqlite3_int64 iFree; /* Next free slot in %_segments */
167604 char *zTerm; /* Pointer to previous term buffer */
167605 int nTerm; /* Number of bytes in zTerm */
167606 int nMalloc; /* Size of malloc'd buffer at zMalloc */
167607 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
167608 int nSize; /* Size of allocation at aData */
167609 int nData; /* Bytes of data in aData */
167610 char *aData; /* Pointer to block from malloc() */
167611 i64 nLeafData; /* Number of bytes of leaf data written */
167612};
167613
167614/*
167615** Type SegmentNode is used by the following three functions to create
167616** the interior part of the segment b+-tree structures (everything except
167617** the leaf nodes). These functions and type are only ever used by code
167618** within the fts3SegWriterXXX() family of functions described above.
167619**
167620** fts3NodeAddTerm()
167621** fts3NodeWrite()
167622** fts3NodeFree()
167623**
167624** When a b+tree is written to the database (either as a result of a merge
167625** or the pending-terms table being flushed), leaves are written into the
167626** database file as soon as they are completely populated. The interior of
167627** the tree is assembled in memory and written out only once all leaves have
167628** been populated and stored. This is Ok, as the b+-tree fanout is usually
167629** very large, meaning that the interior of the tree consumes relatively
167630** little memory.
167631*/
167632struct SegmentNode {
167633 SegmentNode *pParent; /* Parent node (or NULL for root node) */
167634 SegmentNode *pRight; /* Pointer to right-sibling */
167635 SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */
167636 int nEntry; /* Number of terms written to node so far */
167637 char *zTerm; /* Pointer to previous term buffer */
167638 int nTerm; /* Number of bytes in zTerm */
167639 int nMalloc; /* Size of malloc'd buffer at zMalloc */
167640 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
167641 int nData; /* Bytes of valid data so far */
167642 char *aData; /* Node data */
167643};
167644
167645/*
167646** Valid values for the second argument to fts3SqlStmt().
167647*/
167648#define SQL_DELETE_CONTENT 0
167649#define SQL_IS_EMPTY 1
167650#define SQL_DELETE_ALL_CONTENT 2
167651#define SQL_DELETE_ALL_SEGMENTS 3
167652#define SQL_DELETE_ALL_SEGDIR 4
167653#define SQL_DELETE_ALL_DOCSIZE 5
167654#define SQL_DELETE_ALL_STAT 6
167655#define SQL_SELECT_CONTENT_BY_ROWID 7
167656#define SQL_NEXT_SEGMENT_INDEX 8
167657#define SQL_INSERT_SEGMENTS 9
167658#define SQL_NEXT_SEGMENTS_ID 10
167659#define SQL_INSERT_SEGDIR 11
167660#define SQL_SELECT_LEVEL 12
167661#define SQL_SELECT_LEVEL_RANGE 13
167662#define SQL_SELECT_LEVEL_COUNT 14
167663#define SQL_SELECT_SEGDIR_MAX_LEVEL 15
167664#define SQL_DELETE_SEGDIR_LEVEL 16
167665#define SQL_DELETE_SEGMENTS_RANGE 17
167666#define SQL_CONTENT_INSERT 18
167667#define SQL_DELETE_DOCSIZE 19
167668#define SQL_REPLACE_DOCSIZE 20
167669#define SQL_SELECT_DOCSIZE 21
167670#define SQL_SELECT_STAT 22
167671#define SQL_REPLACE_STAT 23
167672
167673#define SQL_SELECT_ALL_PREFIX_LEVEL 24
167674#define SQL_DELETE_ALL_TERMS_SEGDIR 25
167675#define SQL_DELETE_SEGDIR_RANGE 26
167676#define SQL_SELECT_ALL_LANGID 27
167677#define SQL_FIND_MERGE_LEVEL 28
167678#define SQL_MAX_LEAF_NODE_ESTIMATE 29
167679#define SQL_DELETE_SEGDIR_ENTRY 30
167680#define SQL_SHIFT_SEGDIR_ENTRY 31
167681#define SQL_SELECT_SEGDIR 32
167682#define SQL_CHOMP_SEGDIR 33
167683#define SQL_SEGMENT_IS_APPENDABLE 34
167684#define SQL_SELECT_INDEXES 35
167685#define SQL_SELECT_MXLEVEL 36
167686
167687#define SQL_SELECT_LEVEL_RANGE2 37
167688#define SQL_UPDATE_LEVEL_IDX 38
167689#define SQL_UPDATE_LEVEL 39
167690
167691/*
167692** This function is used to obtain an SQLite prepared statement handle
167693** for the statement identified by the second argument. If successful,
167694** *pp is set to the requested statement handle and SQLITE_OK returned.
167695** Otherwise, an SQLite error code is returned and *pp is set to 0.
167696**
167697** If argument apVal is not NULL, then it must point to an array with
167698** at least as many entries as the requested statement has bound
167699** parameters. The values are bound to the statements parameters before
167700** returning.
167701*/
167702static int fts3SqlStmt(
167703 Fts3Table *p, /* Virtual table handle */
167704 int eStmt, /* One of the SQL_XXX constants above */
167705 sqlite3_stmt **pp, /* OUT: Statement handle */
167706 sqlite3_value **apVal /* Values to bind to statement */
167707){
167708 const char *azSql[] = {
167709/* 0 */ "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
167710/* 1 */ "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
167711/* 2 */ "DELETE FROM %Q.'%q_content'",
167712/* 3 */ "DELETE FROM %Q.'%q_segments'",
167713/* 4 */ "DELETE FROM %Q.'%q_segdir'",
167714/* 5 */ "DELETE FROM %Q.'%q_docsize'",
167715/* 6 */ "DELETE FROM %Q.'%q_stat'",
167716/* 7 */ "SELECT %s WHERE rowid=?",
167717/* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
167718/* 9 */ "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
167719/* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
167720/* 11 */ "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
167721
167722 /* Return segments in order from oldest to newest.*/
167723/* 12 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
167724 "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
167725/* 13 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
167726 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
167727 "ORDER BY level DESC, idx ASC",
167728
167729/* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
167730/* 15 */ "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
167731
167732/* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
167733/* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
167734/* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)",
167735/* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
167736/* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
167737/* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
167738/* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=?",
167739/* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
167740/* 24 */ "",
167741/* 25 */ "",
167742
167743/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
167744/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
167745
167746/* This statement is used to determine which level to read the input from
167747** when performing an incremental merge. It returns the absolute level number
167748** of the oldest level in the db that contains at least ? segments. Or,
167749** if no level in the FTS index contains more than ? segments, the statement
167750** returns zero rows. */
167751/* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' "
167752 " GROUP BY level HAVING cnt>=?"
167753 " ORDER BY (level %% 1024) ASC LIMIT 1",
167754
167755/* Estimate the upper limit on the number of leaf nodes in a new segment
167756** created by merging the oldest :2 segments from absolute level :1. See
167757** function sqlite3Fts3Incrmerge() for details. */
167758/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
167759 " FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
167760
167761/* SQL_DELETE_SEGDIR_ENTRY
167762** Delete the %_segdir entry on absolute level :1 with index :2. */
167763/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
167764
167765/* SQL_SHIFT_SEGDIR_ENTRY
167766** Modify the idx value for the segment with idx=:3 on absolute level :2
167767** to :1. */
167768/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
167769
167770/* SQL_SELECT_SEGDIR
167771** Read a single entry from the %_segdir table. The entry from absolute
167772** level :1 with index value :2. */
167773/* 32 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
167774 "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
167775
167776/* SQL_CHOMP_SEGDIR
167777** Update the start_block (:1) and root (:2) fields of the %_segdir
167778** entry located on absolute level :3 with index :4. */
167779/* 33 */ "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
167780 "WHERE level = ? AND idx = ?",
167781
167782/* SQL_SEGMENT_IS_APPENDABLE
167783** Return a single row if the segment with end_block=? is appendable. Or
167784** no rows otherwise. */
167785/* 34 */ "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
167786
167787/* SQL_SELECT_INDEXES
167788** Return the list of valid segment indexes for absolute level ? */
167789/* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
167790
167791/* SQL_SELECT_MXLEVEL
167792** Return the largest relative level in the FTS index or indexes. */
167793/* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
167794
167795 /* Return segments in order from oldest to newest.*/
167796/* 37 */ "SELECT level, idx, end_block "
167797 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
167798 "ORDER BY level DESC, idx ASC",
167799
167800 /* Update statements used while promoting segments */
167801/* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
167802 "WHERE level=? AND idx=?",
167803/* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
167804
167805 };
167806 int rc = SQLITE_OK;
167807 sqlite3_stmt *pStmt;
167808
167809 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
167810 assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
167811
167812 pStmt = p->aStmt[eStmt];
167813 if( !pStmt ){
167814 char *zSql;
167815 if( eStmt==SQL_CONTENT_INSERT ){
167816 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
167817 }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
167818 zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
167819 }else{
167820 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
167821 }
167822 if( !zSql ){
167823 rc = SQLITE_NOMEM;
167824 }else{
167825 rc = sqlite3_prepare_v3(p->db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
167826 &pStmt, NULL);
167827 sqlite3_free(zSql);
167828 assert( rc==SQLITE_OK || pStmt==0 );
167829 p->aStmt[eStmt] = pStmt;
167830 }
167831 }
167832 if( apVal ){
167833 int i;
167834 int nParam = sqlite3_bind_parameter_count(pStmt);
167835 for(i=0; rc==SQLITE_OK && i<nParam; i++){
167836 rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
167837 }
167838 }
167839 *pp = pStmt;
167840 return rc;
167841}
167842
167843
167844static int fts3SelectDocsize(
167845 Fts3Table *pTab, /* FTS3 table handle */
167846 sqlite3_int64 iDocid, /* Docid to bind for SQL_SELECT_DOCSIZE */
167847 sqlite3_stmt **ppStmt /* OUT: Statement handle */
167848){
167849 sqlite3_stmt *pStmt = 0; /* Statement requested from fts3SqlStmt() */
167850 int rc; /* Return code */
167851
167852 rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
167853 if( rc==SQLITE_OK ){
167854 sqlite3_bind_int64(pStmt, 1, iDocid);
167855 rc = sqlite3_step(pStmt);
167856 if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
167857 rc = sqlite3_reset(pStmt);
167858 if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
167859 pStmt = 0;
167860 }else{
167861 rc = SQLITE_OK;
167862 }
167863 }
167864
167865 *ppStmt = pStmt;
167866 return rc;
167867}
167868
167869SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
167870 Fts3Table *pTab, /* Fts3 table handle */
167871 sqlite3_stmt **ppStmt /* OUT: Statement handle */
167872){
167873 sqlite3_stmt *pStmt = 0;
167874 int rc;
167875 rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
167876 if( rc==SQLITE_OK ){
167877 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
167878 if( sqlite3_step(pStmt)!=SQLITE_ROW
167879 || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
167880 ){
167881 rc = sqlite3_reset(pStmt);
167882 if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
167883 pStmt = 0;
167884 }
167885 }
167886 *ppStmt = pStmt;
167887 return rc;
167888}
167889
167890SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
167891 Fts3Table *pTab, /* Fts3 table handle */
167892 sqlite3_int64 iDocid, /* Docid to read size data for */
167893 sqlite3_stmt **ppStmt /* OUT: Statement handle */
167894){
167895 return fts3SelectDocsize(pTab, iDocid, ppStmt);
167896}
167897
167898/*
167899** Similar to fts3SqlStmt(). Except, after binding the parameters in
167900** array apVal[] to the SQL statement identified by eStmt, the statement
167901** is executed.
167902**
167903** Returns SQLITE_OK if the statement is successfully executed, or an
167904** SQLite error code otherwise.
167905*/
167906static void fts3SqlExec(
167907 int *pRC, /* Result code */
167908 Fts3Table *p, /* The FTS3 table */
167909 int eStmt, /* Index of statement to evaluate */
167910 sqlite3_value **apVal /* Parameters to bind */
167911){
167912 sqlite3_stmt *pStmt;
167913 int rc;
167914 if( *pRC ) return;
167915 rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
167916 if( rc==SQLITE_OK ){
167917 sqlite3_step(pStmt);
167918 rc = sqlite3_reset(pStmt);
167919 }
167920 *pRC = rc;
167921}
167922
167923
167924/*
167925** This function ensures that the caller has obtained an exclusive
167926** shared-cache table-lock on the %_segdir table. This is required before
167927** writing data to the fts3 table. If this lock is not acquired first, then
167928** the caller may end up attempting to take this lock as part of committing
167929** a transaction, causing SQLite to return SQLITE_LOCKED or
167930** LOCKED_SHAREDCACHEto a COMMIT command.
167931**
167932** It is best to avoid this because if FTS3 returns any error when
167933** committing a transaction, the whole transaction will be rolled back.
167934** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
167935** It can still happen if the user locks the underlying tables directly
167936** instead of accessing them via FTS.
167937*/
167938static int fts3Writelock(Fts3Table *p){
167939 int rc = SQLITE_OK;
167940
167941 if( p->nPendingData==0 ){
167942 sqlite3_stmt *pStmt;
167943 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0);
167944 if( rc==SQLITE_OK ){
167945 sqlite3_bind_null(pStmt, 1);
167946 sqlite3_step(pStmt);
167947 rc = sqlite3_reset(pStmt);
167948 }
167949 }
167950
167951 return rc;
167952}
167953
167954/*
167955** FTS maintains a separate indexes for each language-id (a 32-bit integer).
167956** Within each language id, a separate index is maintained to store the
167957** document terms, and each configured prefix size (configured the FTS
167958** "prefix=" option). And each index consists of multiple levels ("relative
167959** levels").
167960**
167961** All three of these values (the language id, the specific index and the
167962** level within the index) are encoded in 64-bit integer values stored
167963** in the %_segdir table on disk. This function is used to convert three
167964** separate component values into the single 64-bit integer value that
167965** can be used to query the %_segdir table.
167966**
167967** Specifically, each language-id/index combination is allocated 1024
167968** 64-bit integer level values ("absolute levels"). The main terms index
167969** for language-id 0 is allocate values 0-1023. The first prefix index
167970** (if any) for language-id 0 is allocated values 1024-2047. And so on.
167971** Language 1 indexes are allocated immediately following language 0.
167972**
167973** So, for a system with nPrefix prefix indexes configured, the block of
167974** absolute levels that corresponds to language-id iLangid and index
167975** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
167976*/
167977static sqlite3_int64 getAbsoluteLevel(
167978 Fts3Table *p, /* FTS3 table handle */
167979 int iLangid, /* Language id */
167980 int iIndex, /* Index in p->aIndex[] */
167981 int iLevel /* Level of segments */
167982){
167983 sqlite3_int64 iBase; /* First absolute level for iLangid/iIndex */
167984 assert( iLangid>=0 );
167985 assert( p->nIndex>0 );
167986 assert( iIndex>=0 && iIndex<p->nIndex );
167987
167988 iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
167989 return iBase + iLevel;
167990}
167991
167992/*
167993** Set *ppStmt to a statement handle that may be used to iterate through
167994** all rows in the %_segdir table, from oldest to newest. If successful,
167995** return SQLITE_OK. If an error occurs while preparing the statement,
167996** return an SQLite error code.
167997**
167998** There is only ever one instance of this SQL statement compiled for
167999** each FTS3 table.
168000**
168001** The statement returns the following columns from the %_segdir table:
168002**
168003** 0: idx
168004** 1: start_block
168005** 2: leaves_end_block
168006** 3: end_block
168007** 4: root
168008*/
168009SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
168010 Fts3Table *p, /* FTS3 table */
168011 int iLangid, /* Language being queried */
168012 int iIndex, /* Index for p->aIndex[] */
168013 int iLevel, /* Level to select (relative level) */
168014 sqlite3_stmt **ppStmt /* OUT: Compiled statement */
168015){
168016 int rc;
168017 sqlite3_stmt *pStmt = 0;
168018
168019 assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
168020 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
168021 assert( iIndex>=0 && iIndex<p->nIndex );
168022
168023 if( iLevel<0 ){
168024 /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
168025 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
168026 if( rc==SQLITE_OK ){
168027 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
168028 sqlite3_bind_int64(pStmt, 2,
168029 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
168030 );
168031 }
168032 }else{
168033 /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
168034 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
168035 if( rc==SQLITE_OK ){
168036 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
168037 }
168038 }
168039 *ppStmt = pStmt;
168040 return rc;
168041}
168042
168043
168044/*
168045** Append a single varint to a PendingList buffer. SQLITE_OK is returned
168046** if successful, or an SQLite error code otherwise.
168047**
168048** This function also serves to allocate the PendingList structure itself.
168049** For example, to create a new PendingList structure containing two
168050** varints:
168051**
168052** PendingList *p = 0;
168053** fts3PendingListAppendVarint(&p, 1);
168054** fts3PendingListAppendVarint(&p, 2);
168055*/
168056static int fts3PendingListAppendVarint(
168057 PendingList **pp, /* IN/OUT: Pointer to PendingList struct */
168058 sqlite3_int64 i /* Value to append to data */
168059){
168060 PendingList *p = *pp;
168061
168062 /* Allocate or grow the PendingList as required. */
168063 if( !p ){
168064 p = sqlite3_malloc(sizeof(*p) + 100);
168065 if( !p ){
168066 return SQLITE_NOMEM;
168067 }
168068 p->nSpace = 100;
168069 p->aData = (char *)&p[1];
168070 p->nData = 0;
168071 }
168072 else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
168073 int nNew = p->nSpace * 2;
168074 p = sqlite3_realloc(p, sizeof(*p) + nNew);
168075 if( !p ){
168076 sqlite3_free(*pp);
168077 *pp = 0;
168078 return SQLITE_NOMEM;
168079 }
168080 p->nSpace = nNew;
168081 p->aData = (char *)&p[1];
168082 }
168083
168084 /* Append the new serialized varint to the end of the list. */
168085 p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
168086 p->aData[p->nData] = '\0';
168087 *pp = p;
168088 return SQLITE_OK;
168089}
168090
168091/*
168092** Add a docid/column/position entry to a PendingList structure. Non-zero
168093** is returned if the structure is sqlite3_realloced as part of adding
168094** the entry. Otherwise, zero.
168095**
168096** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
168097** Zero is always returned in this case. Otherwise, if no OOM error occurs,
168098** it is set to SQLITE_OK.
168099*/
168100static int fts3PendingListAppend(
168101 PendingList **pp, /* IN/OUT: PendingList structure */
168102 sqlite3_int64 iDocid, /* Docid for entry to add */
168103 sqlite3_int64 iCol, /* Column for entry to add */
168104 sqlite3_int64 iPos, /* Position of term for entry to add */
168105 int *pRc /* OUT: Return code */
168106){
168107 PendingList *p = *pp;
168108 int rc = SQLITE_OK;
168109
168110 assert( !p || p->iLastDocid<=iDocid );
168111
168112 if( !p || p->iLastDocid!=iDocid ){
168113 sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
168114 if( p ){
168115 assert( p->nData<p->nSpace );
168116 assert( p->aData[p->nData]==0 );
168117 p->nData++;
168118 }
168119 if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
168120 goto pendinglistappend_out;
168121 }
168122 p->iLastCol = -1;
168123 p->iLastPos = 0;
168124 p->iLastDocid = iDocid;
168125 }
168126 if( iCol>0 && p->iLastCol!=iCol ){
168127 if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
168128 || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
168129 ){
168130 goto pendinglistappend_out;
168131 }
168132 p->iLastCol = iCol;
168133 p->iLastPos = 0;
168134 }
168135 if( iCol>=0 ){
168136 assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
168137 rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
168138 if( rc==SQLITE_OK ){
168139 p->iLastPos = iPos;
168140 }
168141 }
168142
168143 pendinglistappend_out:
168144 *pRc = rc;
168145 if( p!=*pp ){
168146 *pp = p;
168147 return 1;
168148 }
168149 return 0;
168150}
168151
168152/*
168153** Free a PendingList object allocated by fts3PendingListAppend().
168154*/
168155static void fts3PendingListDelete(PendingList *pList){
168156 sqlite3_free(pList);
168157}
168158
168159/*
168160** Add an entry to one of the pending-terms hash tables.
168161*/
168162static int fts3PendingTermsAddOne(
168163 Fts3Table *p,
168164 int iCol,
168165 int iPos,
168166 Fts3Hash *pHash, /* Pending terms hash table to add entry to */
168167 const char *zToken,
168168 int nToken
168169){
168170 PendingList *pList;
168171 int rc = SQLITE_OK;
168172
168173 pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
168174 if( pList ){
168175 p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
168176 }
168177 if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
168178 if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
168179 /* Malloc failed while inserting the new entry. This can only
168180 ** happen if there was no previous entry for this token.
168181 */
168182 assert( 0==fts3HashFind(pHash, zToken, nToken) );
168183 sqlite3_free(pList);
168184 rc = SQLITE_NOMEM;
168185 }
168186 }
168187 if( rc==SQLITE_OK ){
168188 p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
168189 }
168190 return rc;
168191}
168192
168193/*
168194** Tokenize the nul-terminated string zText and add all tokens to the
168195** pending-terms hash-table. The docid used is that currently stored in
168196** p->iPrevDocid, and the column is specified by argument iCol.
168197**
168198** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
168199*/
168200static int fts3PendingTermsAdd(
168201 Fts3Table *p, /* Table into which text will be inserted */
168202 int iLangid, /* Language id to use */
168203 const char *zText, /* Text of document to be inserted */
168204 int iCol, /* Column into which text is being inserted */
168205 u32 *pnWord /* IN/OUT: Incr. by number tokens inserted */
168206){
168207 int rc;
168208 int iStart = 0;
168209 int iEnd = 0;
168210 int iPos = 0;
168211 int nWord = 0;
168212
168213 char const *zToken;
168214 int nToken = 0;
168215
168216 sqlite3_tokenizer *pTokenizer = p->pTokenizer;
168217 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
168218 sqlite3_tokenizer_cursor *pCsr;
168219 int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
168220 const char**,int*,int*,int*,int*);
168221
168222 assert( pTokenizer && pModule );
168223
168224 /* If the user has inserted a NULL value, this function may be called with
168225 ** zText==0. In this case, add zero token entries to the hash table and
168226 ** return early. */
168227 if( zText==0 ){
168228 *pnWord = 0;
168229 return SQLITE_OK;
168230 }
168231
168232 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
168233 if( rc!=SQLITE_OK ){
168234 return rc;
168235 }
168236
168237 xNext = pModule->xNext;
168238 while( SQLITE_OK==rc
168239 && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
168240 ){
168241 int i;
168242 if( iPos>=nWord ) nWord = iPos+1;
168243
168244 /* Positions cannot be negative; we use -1 as a terminator internally.
168245 ** Tokens must have a non-zero length.
168246 */
168247 if( iPos<0 || !zToken || nToken<=0 ){
168248 rc = SQLITE_ERROR;
168249 break;
168250 }
168251
168252 /* Add the term to the terms index */
168253 rc = fts3PendingTermsAddOne(
168254 p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
168255 );
168256
168257 /* Add the term to each of the prefix indexes that it is not too
168258 ** short for. */
168259 for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
168260 struct Fts3Index *pIndex = &p->aIndex[i];
168261 if( nToken<pIndex->nPrefix ) continue;
168262 rc = fts3PendingTermsAddOne(
168263 p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
168264 );
168265 }
168266 }
168267
168268 pModule->xClose(pCsr);
168269 *pnWord += nWord;
168270 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
168271}
168272
168273/*
168274** Calling this function indicates that subsequent calls to
168275** fts3PendingTermsAdd() are to add term/position-list pairs for the
168276** contents of the document with docid iDocid.
168277*/
168278static int fts3PendingTermsDocid(
168279 Fts3Table *p, /* Full-text table handle */
168280 int bDelete, /* True if this op is a delete */
168281 int iLangid, /* Language id of row being written */
168282 sqlite_int64 iDocid /* Docid of row being written */
168283){
168284 assert( iLangid>=0 );
168285 assert( bDelete==1 || bDelete==0 );
168286
168287 /* TODO(shess) Explore whether partially flushing the buffer on
168288 ** forced-flush would provide better performance. I suspect that if
168289 ** we ordered the doclists by size and flushed the largest until the
168290 ** buffer was half empty, that would let the less frequent terms
168291 ** generate longer doclists.
168292 */
168293 if( iDocid<p->iPrevDocid
168294 || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
168295 || p->iPrevLangid!=iLangid
168296 || p->nPendingData>p->nMaxPendingData
168297 ){
168298 int rc = sqlite3Fts3PendingTermsFlush(p);
168299 if( rc!=SQLITE_OK ) return rc;
168300 }
168301 p->iPrevDocid = iDocid;
168302 p->iPrevLangid = iLangid;
168303 p->bPrevDelete = bDelete;
168304 return SQLITE_OK;
168305}
168306
168307/*
168308** Discard the contents of the pending-terms hash tables.
168309*/
168310SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
168311 int i;
168312 for(i=0; i<p->nIndex; i++){
168313 Fts3HashElem *pElem;
168314 Fts3Hash *pHash = &p->aIndex[i].hPending;
168315 for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
168316 PendingList *pList = (PendingList *)fts3HashData(pElem);
168317 fts3PendingListDelete(pList);
168318 }
168319 fts3HashClear(pHash);
168320 }
168321 p->nPendingData = 0;
168322}
168323
168324/*
168325** This function is called by the xUpdate() method as part of an INSERT
168326** operation. It adds entries for each term in the new record to the
168327** pendingTerms hash table.
168328**
168329** Argument apVal is the same as the similarly named argument passed to
168330** fts3InsertData(). Parameter iDocid is the docid of the new row.
168331*/
168332static int fts3InsertTerms(
168333 Fts3Table *p,
168334 int iLangid,
168335 sqlite3_value **apVal,
168336 u32 *aSz
168337){
168338 int i; /* Iterator variable */
168339 for(i=2; i<p->nColumn+2; i++){
168340 int iCol = i-2;
168341 if( p->abNotindexed[iCol]==0 ){
168342 const char *zText = (const char *)sqlite3_value_text(apVal[i]);
168343 int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
168344 if( rc!=SQLITE_OK ){
168345 return rc;
168346 }
168347 aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
168348 }
168349 }
168350 return SQLITE_OK;
168351}
168352
168353/*
168354** This function is called by the xUpdate() method for an INSERT operation.
168355** The apVal parameter is passed a copy of the apVal argument passed by
168356** SQLite to the xUpdate() method. i.e:
168357**
168358** apVal[0] Not used for INSERT.
168359** apVal[1] rowid
168360** apVal[2] Left-most user-defined column
168361** ...
168362** apVal[p->nColumn+1] Right-most user-defined column
168363** apVal[p->nColumn+2] Hidden column with same name as table
168364** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
168365** apVal[p->nColumn+4] Hidden languageid column
168366*/
168367static int fts3InsertData(
168368 Fts3Table *p, /* Full-text table */
168369 sqlite3_value **apVal, /* Array of values to insert */
168370 sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */
168371){
168372 int rc; /* Return code */
168373 sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */
168374
168375 if( p->zContentTbl ){
168376 sqlite3_value *pRowid = apVal[p->nColumn+3];
168377 if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
168378 pRowid = apVal[1];
168379 }
168380 if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
168381 return SQLITE_CONSTRAINT;
168382 }
168383 *piDocid = sqlite3_value_int64(pRowid);
168384 return SQLITE_OK;
168385 }
168386
168387 /* Locate the statement handle used to insert data into the %_content
168388 ** table. The SQL for this statement is:
168389 **
168390 ** INSERT INTO %_content VALUES(?, ?, ?, ...)
168391 **
168392 ** The statement features N '?' variables, where N is the number of user
168393 ** defined columns in the FTS3 table, plus one for the docid field.
168394 */
168395 rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
168396 if( rc==SQLITE_OK && p->zLanguageid ){
168397 rc = sqlite3_bind_int(
168398 pContentInsert, p->nColumn+2,
168399 sqlite3_value_int(apVal[p->nColumn+4])
168400 );
168401 }
168402 if( rc!=SQLITE_OK ) return rc;
168403
168404 /* There is a quirk here. The users INSERT statement may have specified
168405 ** a value for the "rowid" field, for the "docid" field, or for both.
168406 ** Which is a problem, since "rowid" and "docid" are aliases for the
168407 ** same value. For example:
168408 **
168409 ** INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
168410 **
168411 ** In FTS3, this is an error. It is an error to specify non-NULL values
168412 ** for both docid and some other rowid alias.
168413 */
168414 if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
168415 if( SQLITE_NULL==sqlite3_value_type(apVal[0])
168416 && SQLITE_NULL!=sqlite3_value_type(apVal[1])
168417 ){
168418 /* A rowid/docid conflict. */
168419 return SQLITE_ERROR;
168420 }
168421 rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
168422 if( rc!=SQLITE_OK ) return rc;
168423 }
168424
168425 /* Execute the statement to insert the record. Set *piDocid to the
168426 ** new docid value.
168427 */
168428 sqlite3_step(pContentInsert);
168429 rc = sqlite3_reset(pContentInsert);
168430
168431 *piDocid = sqlite3_last_insert_rowid(p->db);
168432 return rc;
168433}
168434
168435
168436
168437/*
168438** Remove all data from the FTS3 table. Clear the hash table containing
168439** pending terms.
168440*/
168441static int fts3DeleteAll(Fts3Table *p, int bContent){
168442 int rc = SQLITE_OK; /* Return code */
168443
168444 /* Discard the contents of the pending-terms hash table. */
168445 sqlite3Fts3PendingTermsClear(p);
168446
168447 /* Delete everything from the shadow tables. Except, leave %_content as
168448 ** is if bContent is false. */
168449 assert( p->zContentTbl==0 || bContent==0 );
168450 if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
168451 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
168452 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
168453 if( p->bHasDocsize ){
168454 fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
168455 }
168456 if( p->bHasStat ){
168457 fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
168458 }
168459 return rc;
168460}
168461
168462/*
168463**
168464*/
168465static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
168466 int iLangid = 0;
168467 if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
168468 return iLangid;
168469}
168470
168471/*
168472** The first element in the apVal[] array is assumed to contain the docid
168473** (an integer) of a row about to be deleted. Remove all terms from the
168474** full-text index.
168475*/
168476static void fts3DeleteTerms(
168477 int *pRC, /* Result code */
168478 Fts3Table *p, /* The FTS table to delete from */
168479 sqlite3_value *pRowid, /* The docid to be deleted */
168480 u32 *aSz, /* Sizes of deleted document written here */
168481 int *pbFound /* OUT: Set to true if row really does exist */
168482){
168483 int rc;
168484 sqlite3_stmt *pSelect;
168485
168486 assert( *pbFound==0 );
168487 if( *pRC ) return;
168488 rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
168489 if( rc==SQLITE_OK ){
168490 if( SQLITE_ROW==sqlite3_step(pSelect) ){
168491 int i;
168492 int iLangid = langidFromSelect(p, pSelect);
168493 i64 iDocid = sqlite3_column_int64(pSelect, 0);
168494 rc = fts3PendingTermsDocid(p, 1, iLangid, iDocid);
168495 for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
168496 int iCol = i-1;
168497 if( p->abNotindexed[iCol]==0 ){
168498 const char *zText = (const char *)sqlite3_column_text(pSelect, i);
168499 rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
168500 aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
168501 }
168502 }
168503 if( rc!=SQLITE_OK ){
168504 sqlite3_reset(pSelect);
168505 *pRC = rc;
168506 return;
168507 }
168508 *pbFound = 1;
168509 }
168510 rc = sqlite3_reset(pSelect);
168511 }else{
168512 sqlite3_reset(pSelect);
168513 }
168514 *pRC = rc;
168515}
168516
168517/*
168518** Forward declaration to account for the circular dependency between
168519** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
168520*/
168521static int fts3SegmentMerge(Fts3Table *, int, int, int);
168522
168523/*
168524** This function allocates a new level iLevel index in the segdir table.
168525** Usually, indexes are allocated within a level sequentially starting
168526** with 0, so the allocated index is one greater than the value returned
168527** by:
168528**
168529** SELECT max(idx) FROM %_segdir WHERE level = :iLevel
168530**
168531** However, if there are already FTS3_MERGE_COUNT indexes at the requested
168532** level, they are merged into a single level (iLevel+1) segment and the
168533** allocated index is 0.
168534**
168535** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
168536** returned. Otherwise, an SQLite error code is returned.
168537*/
168538static int fts3AllocateSegdirIdx(
168539 Fts3Table *p,
168540 int iLangid, /* Language id */
168541 int iIndex, /* Index for p->aIndex */
168542 int iLevel,
168543 int *piIdx
168544){
168545 int rc; /* Return Code */
168546 sqlite3_stmt *pNextIdx; /* Query for next idx at level iLevel */
168547 int iNext = 0; /* Result of query pNextIdx */
168548
168549 assert( iLangid>=0 );
168550 assert( p->nIndex>=1 );
168551
168552 /* Set variable iNext to the next available segdir index at level iLevel. */
168553 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
168554 if( rc==SQLITE_OK ){
168555 sqlite3_bind_int64(
168556 pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
168557 );
168558 if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
168559 iNext = sqlite3_column_int(pNextIdx, 0);
168560 }
168561 rc = sqlite3_reset(pNextIdx);
168562 }
168563
168564 if( rc==SQLITE_OK ){
168565 /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
168566 ** full, merge all segments in level iLevel into a single iLevel+1
168567 ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
168568 ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
168569 */
168570 if( iNext>=FTS3_MERGE_COUNT ){
168571 fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
168572 rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
168573 *piIdx = 0;
168574 }else{
168575 *piIdx = iNext;
168576 }
168577 }
168578
168579 return rc;
168580}
168581
168582/*
168583** The %_segments table is declared as follows:
168584**
168585** CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
168586**
168587** This function reads data from a single row of the %_segments table. The
168588** specific row is identified by the iBlockid parameter. If paBlob is not
168589** NULL, then a buffer is allocated using sqlite3_malloc() and populated
168590** with the contents of the blob stored in the "block" column of the
168591** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
168592** to the size of the blob in bytes before returning.
168593**
168594** If an error occurs, or the table does not contain the specified row,
168595** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
168596** paBlob is non-NULL, then it is the responsibility of the caller to
168597** eventually free the returned buffer.
168598**
168599** This function may leave an open sqlite3_blob* handle in the
168600** Fts3Table.pSegments variable. This handle is reused by subsequent calls
168601** to this function. The handle may be closed by calling the
168602** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
168603** performance improvement, but the blob handle should always be closed
168604** before control is returned to the user (to prevent a lock being held
168605** on the database file for longer than necessary). Thus, any virtual table
168606** method (xFilter etc.) that may directly or indirectly call this function
168607** must call sqlite3Fts3SegmentsClose() before returning.
168608*/
168609SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
168610 Fts3Table *p, /* FTS3 table handle */
168611 sqlite3_int64 iBlockid, /* Access the row with blockid=$iBlockid */
168612 char **paBlob, /* OUT: Blob data in malloc'd buffer */
168613 int *pnBlob, /* OUT: Size of blob data */
168614 int *pnLoad /* OUT: Bytes actually loaded */
168615){
168616 int rc; /* Return code */
168617
168618 /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
168619 assert( pnBlob );
168620
168621 if( p->pSegments ){
168622 rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
168623 }else{
168624 if( 0==p->zSegmentsTbl ){
168625 p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
168626 if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
168627 }
168628 rc = sqlite3_blob_open(
168629 p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
168630 );
168631 }
168632
168633 if( rc==SQLITE_OK ){
168634 int nByte = sqlite3_blob_bytes(p->pSegments);
168635 *pnBlob = nByte;
168636 if( paBlob ){
168637 char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
168638 if( !aByte ){
168639 rc = SQLITE_NOMEM;
168640 }else{
168641 if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
168642 nByte = FTS3_NODE_CHUNKSIZE;
168643 *pnLoad = nByte;
168644 }
168645 rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
168646 memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
168647 if( rc!=SQLITE_OK ){
168648 sqlite3_free(aByte);
168649 aByte = 0;
168650 }
168651 }
168652 *paBlob = aByte;
168653 }
168654 }
168655
168656 return rc;
168657}
168658
168659/*
168660** Close the blob handle at p->pSegments, if it is open. See comments above
168661** the sqlite3Fts3ReadBlock() function for details.
168662*/
168663SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
168664 sqlite3_blob_close(p->pSegments);
168665 p->pSegments = 0;
168666}
168667
168668static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
168669 int nRead; /* Number of bytes to read */
168670 int rc; /* Return code */
168671
168672 nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
168673 rc = sqlite3_blob_read(
168674 pReader->pBlob,
168675 &pReader->aNode[pReader->nPopulate],
168676 nRead,
168677 pReader->nPopulate
168678 );
168679
168680 if( rc==SQLITE_OK ){
168681 pReader->nPopulate += nRead;
168682 memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
168683 if( pReader->nPopulate==pReader->nNode ){
168684 sqlite3_blob_close(pReader->pBlob);
168685 pReader->pBlob = 0;
168686 pReader->nPopulate = 0;
168687 }
168688 }
168689 return rc;
168690}
168691
168692static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
168693 int rc = SQLITE_OK;
168694 assert( !pReader->pBlob
168695 || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
168696 );
168697 while( pReader->pBlob && rc==SQLITE_OK
168698 && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
168699 ){
168700 rc = fts3SegReaderIncrRead(pReader);
168701 }
168702 return rc;
168703}
168704
168705/*
168706** Set an Fts3SegReader cursor to point at EOF.
168707*/
168708static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
168709 if( !fts3SegReaderIsRootOnly(pSeg) ){
168710 sqlite3_free(pSeg->aNode);
168711 sqlite3_blob_close(pSeg->pBlob);
168712 pSeg->pBlob = 0;
168713 }
168714 pSeg->aNode = 0;
168715}
168716
168717/*
168718** Move the iterator passed as the first argument to the next term in the
168719** segment. If successful, SQLITE_OK is returned. If there is no next term,
168720** SQLITE_DONE. Otherwise, an SQLite error code.
168721*/
168722static int fts3SegReaderNext(
168723 Fts3Table *p,
168724 Fts3SegReader *pReader,
168725 int bIncr
168726){
168727 int rc; /* Return code of various sub-routines */
168728 char *pNext; /* Cursor variable */
168729 int nPrefix; /* Number of bytes in term prefix */
168730 int nSuffix; /* Number of bytes in term suffix */
168731
168732 if( !pReader->aDoclist ){
168733 pNext = pReader->aNode;
168734 }else{
168735 pNext = &pReader->aDoclist[pReader->nDoclist];
168736 }
168737
168738 if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
168739
168740 if( fts3SegReaderIsPending(pReader) ){
168741 Fts3HashElem *pElem = *(pReader->ppNextElem);
168742 sqlite3_free(pReader->aNode);
168743 pReader->aNode = 0;
168744 if( pElem ){
168745 char *aCopy;
168746 PendingList *pList = (PendingList *)fts3HashData(pElem);
168747 int nCopy = pList->nData+1;
168748 pReader->zTerm = (char *)fts3HashKey(pElem);
168749 pReader->nTerm = fts3HashKeysize(pElem);
168750 aCopy = (char*)sqlite3_malloc(nCopy);
168751 if( !aCopy ) return SQLITE_NOMEM;
168752 memcpy(aCopy, pList->aData, nCopy);
168753 pReader->nNode = pReader->nDoclist = nCopy;
168754 pReader->aNode = pReader->aDoclist = aCopy;
168755 pReader->ppNextElem++;
168756 assert( pReader->aNode );
168757 }
168758 return SQLITE_OK;
168759 }
168760
168761 fts3SegReaderSetEof(pReader);
168762
168763 /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
168764 ** blocks have already been traversed. */
168765 assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
168766 if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
168767 return SQLITE_OK;
168768 }
168769
168770 rc = sqlite3Fts3ReadBlock(
168771 p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
168772 (bIncr ? &pReader->nPopulate : 0)
168773 );
168774 if( rc!=SQLITE_OK ) return rc;
168775 assert( pReader->pBlob==0 );
168776 if( bIncr && pReader->nPopulate<pReader->nNode ){
168777 pReader->pBlob = p->pSegments;
168778 p->pSegments = 0;
168779 }
168780 pNext = pReader->aNode;
168781 }
168782
168783 assert( !fts3SegReaderIsPending(pReader) );
168784
168785 rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
168786 if( rc!=SQLITE_OK ) return rc;
168787
168788 /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
168789 ** safe (no risk of overread) even if the node data is corrupted. */
168790 pNext += fts3GetVarint32(pNext, &nPrefix);
168791 pNext += fts3GetVarint32(pNext, &nSuffix);
168792 if( nPrefix<0 || nSuffix<=0
168793 || &pNext[nSuffix]>&pReader->aNode[pReader->nNode]
168794 ){
168795 return FTS_CORRUPT_VTAB;
168796 }
168797
168798 if( nPrefix+nSuffix>pReader->nTermAlloc ){
168799 int nNew = (nPrefix+nSuffix)*2;
168800 char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
168801 if( !zNew ){
168802 return SQLITE_NOMEM;
168803 }
168804 pReader->zTerm = zNew;
168805 pReader->nTermAlloc = nNew;
168806 }
168807
168808 rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
168809 if( rc!=SQLITE_OK ) return rc;
168810
168811 memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
168812 pReader->nTerm = nPrefix+nSuffix;
168813 pNext += nSuffix;
168814 pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
168815 pReader->aDoclist = pNext;
168816 pReader->pOffsetList = 0;
168817
168818 /* Check that the doclist does not appear to extend past the end of the
168819 ** b-tree node. And that the final byte of the doclist is 0x00. If either
168820 ** of these statements is untrue, then the data structure is corrupt.
168821 */
168822 if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode]
168823 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
168824 ){
168825 return FTS_CORRUPT_VTAB;
168826 }
168827 return SQLITE_OK;
168828}
168829
168830/*
168831** Set the SegReader to point to the first docid in the doclist associated
168832** with the current term.
168833*/
168834static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
168835 int rc = SQLITE_OK;
168836 assert( pReader->aDoclist );
168837 assert( !pReader->pOffsetList );
168838 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
168839 u8 bEof = 0;
168840 pReader->iDocid = 0;
168841 pReader->nOffsetList = 0;
168842 sqlite3Fts3DoclistPrev(0,
168843 pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
168844 &pReader->iDocid, &pReader->nOffsetList, &bEof
168845 );
168846 }else{
168847 rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
168848 if( rc==SQLITE_OK ){
168849 int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
168850 pReader->pOffsetList = &pReader->aDoclist[n];
168851 }
168852 }
168853 return rc;
168854}
168855
168856/*
168857** Advance the SegReader to point to the next docid in the doclist
168858** associated with the current term.
168859**
168860** If arguments ppOffsetList and pnOffsetList are not NULL, then
168861** *ppOffsetList is set to point to the first column-offset list
168862** in the doclist entry (i.e. immediately past the docid varint).
168863** *pnOffsetList is set to the length of the set of column-offset
168864** lists, not including the nul-terminator byte. For example:
168865*/
168866static int fts3SegReaderNextDocid(
168867 Fts3Table *pTab,
168868 Fts3SegReader *pReader, /* Reader to advance to next docid */
168869 char **ppOffsetList, /* OUT: Pointer to current position-list */
168870 int *pnOffsetList /* OUT: Length of *ppOffsetList in bytes */
168871){
168872 int rc = SQLITE_OK;
168873 char *p = pReader->pOffsetList;
168874 char c = 0;
168875
168876 assert( p );
168877
168878 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
168879 /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
168880 ** Pending-terms doclists are always built up in ascending order, so
168881 ** we have to iterate through them backwards here. */
168882 u8 bEof = 0;
168883 if( ppOffsetList ){
168884 *ppOffsetList = pReader->pOffsetList;
168885 *pnOffsetList = pReader->nOffsetList - 1;
168886 }
168887 sqlite3Fts3DoclistPrev(0,
168888 pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
168889 &pReader->nOffsetList, &bEof
168890 );
168891 if( bEof ){
168892 pReader->pOffsetList = 0;
168893 }else{
168894 pReader->pOffsetList = p;
168895 }
168896 }else{
168897 char *pEnd = &pReader->aDoclist[pReader->nDoclist];
168898
168899 /* Pointer p currently points at the first byte of an offset list. The
168900 ** following block advances it to point one byte past the end of
168901 ** the same offset list. */
168902 while( 1 ){
168903
168904 /* The following line of code (and the "p++" below the while() loop) is
168905 ** normally all that is required to move pointer p to the desired
168906 ** position. The exception is if this node is being loaded from disk
168907 ** incrementally and pointer "p" now points to the first byte past
168908 ** the populated part of pReader->aNode[].
168909 */
168910 while( *p | c ) c = *p++ & 0x80;
168911 assert( *p==0 );
168912
168913 if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
168914 rc = fts3SegReaderIncrRead(pReader);
168915 if( rc!=SQLITE_OK ) return rc;
168916 }
168917 p++;
168918
168919 /* If required, populate the output variables with a pointer to and the
168920 ** size of the previous offset-list.
168921 */
168922 if( ppOffsetList ){
168923 *ppOffsetList = pReader->pOffsetList;
168924 *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
168925 }
168926
168927 /* List may have been edited in place by fts3EvalNearTrim() */
168928 while( p<pEnd && *p==0 ) p++;
168929
168930 /* If there are no more entries in the doclist, set pOffsetList to
168931 ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
168932 ** Fts3SegReader.pOffsetList to point to the next offset list before
168933 ** returning.
168934 */
168935 if( p>=pEnd ){
168936 pReader->pOffsetList = 0;
168937 }else{
168938 rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
168939 if( rc==SQLITE_OK ){
168940 sqlite3_int64 iDelta;
168941 pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
168942 if( pTab->bDescIdx ){
168943 pReader->iDocid -= iDelta;
168944 }else{
168945 pReader->iDocid += iDelta;
168946 }
168947 }
168948 }
168949 }
168950
168951 return SQLITE_OK;
168952}
168953
168954
168955SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
168956 Fts3Cursor *pCsr,
168957 Fts3MultiSegReader *pMsr,
168958 int *pnOvfl
168959){
168960 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
168961 int nOvfl = 0;
168962 int ii;
168963 int rc = SQLITE_OK;
168964 int pgsz = p->nPgsz;
168965
168966 assert( p->bFts4 );
168967 assert( pgsz>0 );
168968
168969 for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
168970 Fts3SegReader *pReader = pMsr->apSegment[ii];
168971 if( !fts3SegReaderIsPending(pReader)
168972 && !fts3SegReaderIsRootOnly(pReader)
168973 ){
168974 sqlite3_int64 jj;
168975 for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
168976 int nBlob;
168977 rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
168978 if( rc!=SQLITE_OK ) break;
168979 if( (nBlob+35)>pgsz ){
168980 nOvfl += (nBlob + 34)/pgsz;
168981 }
168982 }
168983 }
168984 }
168985 *pnOvfl = nOvfl;
168986 return rc;
168987}
168988
168989/*
168990** Free all allocations associated with the iterator passed as the
168991** second argument.
168992*/
168993SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
168994 if( pReader ){
168995 if( !fts3SegReaderIsPending(pReader) ){
168996 sqlite3_free(pReader->zTerm);
168997 }
168998 if( !fts3SegReaderIsRootOnly(pReader) ){
168999 sqlite3_free(pReader->aNode);
169000 }
169001 sqlite3_blob_close(pReader->pBlob);
169002 }
169003 sqlite3_free(pReader);
169004}
169005
169006/*
169007** Allocate a new SegReader object.
169008*/
169009SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
169010 int iAge, /* Segment "age". */
169011 int bLookup, /* True for a lookup only */
169012 sqlite3_int64 iStartLeaf, /* First leaf to traverse */
169013 sqlite3_int64 iEndLeaf, /* Final leaf to traverse */
169014 sqlite3_int64 iEndBlock, /* Final block of segment */
169015 const char *zRoot, /* Buffer containing root node */
169016 int nRoot, /* Size of buffer containing root node */
169017 Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
169018){
169019 Fts3SegReader *pReader; /* Newly allocated SegReader object */
169020 int nExtra = 0; /* Bytes to allocate segment root node */
169021
169022 assert( iStartLeaf<=iEndLeaf );
169023 if( iStartLeaf==0 ){
169024 nExtra = nRoot + FTS3_NODE_PADDING;
169025 }
169026
169027 pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
169028 if( !pReader ){
169029 return SQLITE_NOMEM;
169030 }
169031 memset(pReader, 0, sizeof(Fts3SegReader));
169032 pReader->iIdx = iAge;
169033 pReader->bLookup = bLookup!=0;
169034 pReader->iStartBlock = iStartLeaf;
169035 pReader->iLeafEndBlock = iEndLeaf;
169036 pReader->iEndBlock = iEndBlock;
169037
169038 if( nExtra ){
169039 /* The entire segment is stored in the root node. */
169040 pReader->aNode = (char *)&pReader[1];
169041 pReader->rootOnly = 1;
169042 pReader->nNode = nRoot;
169043 memcpy(pReader->aNode, zRoot, nRoot);
169044 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
169045 }else{
169046 pReader->iCurrentBlock = iStartLeaf-1;
169047 }
169048 *ppReader = pReader;
169049 return SQLITE_OK;
169050}
169051
169052/*
169053** This is a comparison function used as a qsort() callback when sorting
169054** an array of pending terms by term. This occurs as part of flushing
169055** the contents of the pending-terms hash table to the database.
169056*/
169057static int SQLITE_CDECL fts3CompareElemByTerm(
169058 const void *lhs,
169059 const void *rhs
169060){
169061 char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
169062 char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
169063 int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
169064 int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
169065
169066 int n = (n1<n2 ? n1 : n2);
169067 int c = memcmp(z1, z2, n);
169068 if( c==0 ){
169069 c = n1 - n2;
169070 }
169071 return c;
169072}
169073
169074/*
169075** This function is used to allocate an Fts3SegReader that iterates through
169076** a subset of the terms stored in the Fts3Table.pendingTerms array.
169077**
169078** If the isPrefixIter parameter is zero, then the returned SegReader iterates
169079** through each term in the pending-terms table. Or, if isPrefixIter is
169080** non-zero, it iterates through each term and its prefixes. For example, if
169081** the pending terms hash table contains the terms "sqlite", "mysql" and
169082** "firebird", then the iterator visits the following 'terms' (in the order
169083** shown):
169084**
169085** f fi fir fire fireb firebi firebir firebird
169086** m my mys mysq mysql
169087** s sq sql sqli sqlit sqlite
169088**
169089** Whereas if isPrefixIter is zero, the terms visited are:
169090**
169091** firebird mysql sqlite
169092*/
169093SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
169094 Fts3Table *p, /* Virtual table handle */
169095 int iIndex, /* Index for p->aIndex */
169096 const char *zTerm, /* Term to search for */
169097 int nTerm, /* Size of buffer zTerm */
169098 int bPrefix, /* True for a prefix iterator */
169099 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
169100){
169101 Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
169102 Fts3HashElem *pE; /* Iterator variable */
169103 Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
169104 int nElem = 0; /* Size of array at aElem */
169105 int rc = SQLITE_OK; /* Return Code */
169106 Fts3Hash *pHash;
169107
169108 pHash = &p->aIndex[iIndex].hPending;
169109 if( bPrefix ){
169110 int nAlloc = 0; /* Size of allocated array at aElem */
169111
169112 for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
169113 char *zKey = (char *)fts3HashKey(pE);
169114 int nKey = fts3HashKeysize(pE);
169115 if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
169116 if( nElem==nAlloc ){
169117 Fts3HashElem **aElem2;
169118 nAlloc += 16;
169119 aElem2 = (Fts3HashElem **)sqlite3_realloc(
169120 aElem, nAlloc*sizeof(Fts3HashElem *)
169121 );
169122 if( !aElem2 ){
169123 rc = SQLITE_NOMEM;
169124 nElem = 0;
169125 break;
169126 }
169127 aElem = aElem2;
169128 }
169129
169130 aElem[nElem++] = pE;
169131 }
169132 }
169133
169134 /* If more than one term matches the prefix, sort the Fts3HashElem
169135 ** objects in term order using qsort(). This uses the same comparison
169136 ** callback as is used when flushing terms to disk.
169137 */
169138 if( nElem>1 ){
169139 qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
169140 }
169141
169142 }else{
169143 /* The query is a simple term lookup that matches at most one term in
169144 ** the index. All that is required is a straight hash-lookup.
169145 **
169146 ** Because the stack address of pE may be accessed via the aElem pointer
169147 ** below, the "Fts3HashElem *pE" must be declared so that it is valid
169148 ** within this entire function, not just this "else{...}" block.
169149 */
169150 pE = fts3HashFindElem(pHash, zTerm, nTerm);
169151 if( pE ){
169152 aElem = &pE;
169153 nElem = 1;
169154 }
169155 }
169156
169157 if( nElem>0 ){
169158 int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
169159 pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
169160 if( !pReader ){
169161 rc = SQLITE_NOMEM;
169162 }else{
169163 memset(pReader, 0, nByte);
169164 pReader->iIdx = 0x7FFFFFFF;
169165 pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
169166 memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
169167 }
169168 }
169169
169170 if( bPrefix ){
169171 sqlite3_free(aElem);
169172 }
169173 *ppReader = pReader;
169174 return rc;
169175}
169176
169177/*
169178** Compare the entries pointed to by two Fts3SegReader structures.
169179** Comparison is as follows:
169180**
169181** 1) EOF is greater than not EOF.
169182**
169183** 2) The current terms (if any) are compared using memcmp(). If one
169184** term is a prefix of another, the longer term is considered the
169185** larger.
169186**
169187** 3) By segment age. An older segment is considered larger.
169188*/
169189static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
169190 int rc;
169191 if( pLhs->aNode && pRhs->aNode ){
169192 int rc2 = pLhs->nTerm - pRhs->nTerm;
169193 if( rc2<0 ){
169194 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
169195 }else{
169196 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
169197 }
169198 if( rc==0 ){
169199 rc = rc2;
169200 }
169201 }else{
169202 rc = (pLhs->aNode==0) - (pRhs->aNode==0);
169203 }
169204 if( rc==0 ){
169205 rc = pRhs->iIdx - pLhs->iIdx;
169206 }
169207 assert( rc!=0 );
169208 return rc;
169209}
169210
169211/*
169212** A different comparison function for SegReader structures. In this
169213** version, it is assumed that each SegReader points to an entry in
169214** a doclist for identical terms. Comparison is made as follows:
169215**
169216** 1) EOF (end of doclist in this case) is greater than not EOF.
169217**
169218** 2) By current docid.
169219**
169220** 3) By segment age. An older segment is considered larger.
169221*/
169222static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
169223 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
169224 if( rc==0 ){
169225 if( pLhs->iDocid==pRhs->iDocid ){
169226 rc = pRhs->iIdx - pLhs->iIdx;
169227 }else{
169228 rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
169229 }
169230 }
169231 assert( pLhs->aNode && pRhs->aNode );
169232 return rc;
169233}
169234static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
169235 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
169236 if( rc==0 ){
169237 if( pLhs->iDocid==pRhs->iDocid ){
169238 rc = pRhs->iIdx - pLhs->iIdx;
169239 }else{
169240 rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
169241 }
169242 }
169243 assert( pLhs->aNode && pRhs->aNode );
169244 return rc;
169245}
169246
169247/*
169248** Compare the term that the Fts3SegReader object passed as the first argument
169249** points to with the term specified by arguments zTerm and nTerm.
169250**
169251** If the pSeg iterator is already at EOF, return 0. Otherwise, return
169252** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
169253** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
169254*/
169255static int fts3SegReaderTermCmp(
169256 Fts3SegReader *pSeg, /* Segment reader object */
169257 const char *zTerm, /* Term to compare to */
169258 int nTerm /* Size of term zTerm in bytes */
169259){
169260 int res = 0;
169261 if( pSeg->aNode ){
169262 if( pSeg->nTerm>nTerm ){
169263 res = memcmp(pSeg->zTerm, zTerm, nTerm);
169264 }else{
169265 res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
169266 }
169267 if( res==0 ){
169268 res = pSeg->nTerm-nTerm;
169269 }
169270 }
169271 return res;
169272}
169273
169274/*
169275** Argument apSegment is an array of nSegment elements. It is known that
169276** the final (nSegment-nSuspect) members are already in sorted order
169277** (according to the comparison function provided). This function shuffles
169278** the array around until all entries are in sorted order.
169279*/
169280static void fts3SegReaderSort(
169281 Fts3SegReader **apSegment, /* Array to sort entries of */
169282 int nSegment, /* Size of apSegment array */
169283 int nSuspect, /* Unsorted entry count */
169284 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) /* Comparison function */
169285){
169286 int i; /* Iterator variable */
169287
169288 assert( nSuspect<=nSegment );
169289
169290 if( nSuspect==nSegment ) nSuspect--;
169291 for(i=nSuspect-1; i>=0; i--){
169292 int j;
169293 for(j=i; j<(nSegment-1); j++){
169294 Fts3SegReader *pTmp;
169295 if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
169296 pTmp = apSegment[j+1];
169297 apSegment[j+1] = apSegment[j];
169298 apSegment[j] = pTmp;
169299 }
169300 }
169301
169302#ifndef NDEBUG
169303 /* Check that the list really is sorted now. */
169304 for(i=0; i<(nSuspect-1); i++){
169305 assert( xCmp(apSegment[i], apSegment[i+1])<0 );
169306 }
169307#endif
169308}
169309
169310/*
169311** Insert a record into the %_segments table.
169312*/
169313static int fts3WriteSegment(
169314 Fts3Table *p, /* Virtual table handle */
169315 sqlite3_int64 iBlock, /* Block id for new block */
169316 char *z, /* Pointer to buffer containing block data */
169317 int n /* Size of buffer z in bytes */
169318){
169319 sqlite3_stmt *pStmt;
169320 int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
169321 if( rc==SQLITE_OK ){
169322 sqlite3_bind_int64(pStmt, 1, iBlock);
169323 sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
169324 sqlite3_step(pStmt);
169325 rc = sqlite3_reset(pStmt);
169326 sqlite3_bind_null(pStmt, 2);
169327 }
169328 return rc;
169329}
169330
169331/*
169332** Find the largest relative level number in the table. If successful, set
169333** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
169334** set *pnMax to zero and return an SQLite error code.
169335*/
169336SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
169337 int rc;
169338 int mxLevel = 0;
169339 sqlite3_stmt *pStmt = 0;
169340
169341 rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
169342 if( rc==SQLITE_OK ){
169343 if( SQLITE_ROW==sqlite3_step(pStmt) ){
169344 mxLevel = sqlite3_column_int(pStmt, 0);
169345 }
169346 rc = sqlite3_reset(pStmt);
169347 }
169348 *pnMax = mxLevel;
169349 return rc;
169350}
169351
169352/*
169353** Insert a record into the %_segdir table.
169354*/
169355static int fts3WriteSegdir(
169356 Fts3Table *p, /* Virtual table handle */
169357 sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */
169358 int iIdx, /* Value for "idx" field */
169359 sqlite3_int64 iStartBlock, /* Value for "start_block" field */
169360 sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
169361 sqlite3_int64 iEndBlock, /* Value for "end_block" field */
169362 sqlite3_int64 nLeafData, /* Bytes of leaf data in segment */
169363 char *zRoot, /* Blob value for "root" field */
169364 int nRoot /* Number of bytes in buffer zRoot */
169365){
169366 sqlite3_stmt *pStmt;
169367 int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
169368 if( rc==SQLITE_OK ){
169369 sqlite3_bind_int64(pStmt, 1, iLevel);
169370 sqlite3_bind_int(pStmt, 2, iIdx);
169371 sqlite3_bind_int64(pStmt, 3, iStartBlock);
169372 sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
169373 if( nLeafData==0 ){
169374 sqlite3_bind_int64(pStmt, 5, iEndBlock);
169375 }else{
169376 char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
169377 if( !zEnd ) return SQLITE_NOMEM;
169378 sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
169379 }
169380 sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
169381 sqlite3_step(pStmt);
169382 rc = sqlite3_reset(pStmt);
169383 sqlite3_bind_null(pStmt, 6);
169384 }
169385 return rc;
169386}
169387
169388/*
169389** Return the size of the common prefix (if any) shared by zPrev and
169390** zNext, in bytes. For example,
169391**
169392** fts3PrefixCompress("abc", 3, "abcdef", 6) // returns 3
169393** fts3PrefixCompress("abX", 3, "abcdef", 6) // returns 2
169394** fts3PrefixCompress("abX", 3, "Xbcdef", 6) // returns 0
169395*/
169396static int fts3PrefixCompress(
169397 const char *zPrev, /* Buffer containing previous term */
169398 int nPrev, /* Size of buffer zPrev in bytes */
169399 const char *zNext, /* Buffer containing next term */
169400 int nNext /* Size of buffer zNext in bytes */
169401){
169402 int n;
169403 UNUSED_PARAMETER(nNext);
169404 for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
169405 return n;
169406}
169407
169408/*
169409** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
169410** (according to memcmp) than the previous term.
169411*/
169412static int fts3NodeAddTerm(
169413 Fts3Table *p, /* Virtual table handle */
169414 SegmentNode **ppTree, /* IN/OUT: SegmentNode handle */
169415 int isCopyTerm, /* True if zTerm/nTerm is transient */
169416 const char *zTerm, /* Pointer to buffer containing term */
169417 int nTerm /* Size of term in bytes */
169418){
169419 SegmentNode *pTree = *ppTree;
169420 int rc;
169421 SegmentNode *pNew;
169422
169423 /* First try to append the term to the current node. Return early if
169424 ** this is possible.
169425 */
169426 if( pTree ){
169427 int nData = pTree->nData; /* Current size of node in bytes */
169428 int nReq = nData; /* Required space after adding zTerm */
169429 int nPrefix; /* Number of bytes of prefix compression */
169430 int nSuffix; /* Suffix length */
169431
169432 nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
169433 nSuffix = nTerm-nPrefix;
169434
169435 nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
169436 if( nReq<=p->nNodeSize || !pTree->zTerm ){
169437
169438 if( nReq>p->nNodeSize ){
169439 /* An unusual case: this is the first term to be added to the node
169440 ** and the static node buffer (p->nNodeSize bytes) is not large
169441 ** enough. Use a separately malloced buffer instead This wastes
169442 ** p->nNodeSize bytes, but since this scenario only comes about when
169443 ** the database contain two terms that share a prefix of almost 2KB,
169444 ** this is not expected to be a serious problem.
169445 */
169446 assert( pTree->aData==(char *)&pTree[1] );
169447 pTree->aData = (char *)sqlite3_malloc(nReq);
169448 if( !pTree->aData ){
169449 return SQLITE_NOMEM;
169450 }
169451 }
169452
169453 if( pTree->zTerm ){
169454 /* There is no prefix-length field for first term in a node */
169455 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
169456 }
169457
169458 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
169459 memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
169460 pTree->nData = nData + nSuffix;
169461 pTree->nEntry++;
169462
169463 if( isCopyTerm ){
169464 if( pTree->nMalloc<nTerm ){
169465 char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
169466 if( !zNew ){
169467 return SQLITE_NOMEM;
169468 }
169469 pTree->nMalloc = nTerm*2;
169470 pTree->zMalloc = zNew;
169471 }
169472 pTree->zTerm = pTree->zMalloc;
169473 memcpy(pTree->zTerm, zTerm, nTerm);
169474 pTree->nTerm = nTerm;
169475 }else{
169476 pTree->zTerm = (char *)zTerm;
169477 pTree->nTerm = nTerm;
169478 }
169479 return SQLITE_OK;
169480 }
169481 }
169482
169483 /* If control flows to here, it was not possible to append zTerm to the
169484 ** current node. Create a new node (a right-sibling of the current node).
169485 ** If this is the first node in the tree, the term is added to it.
169486 **
169487 ** Otherwise, the term is not added to the new node, it is left empty for
169488 ** now. Instead, the term is inserted into the parent of pTree. If pTree
169489 ** has no parent, one is created here.
169490 */
169491 pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
169492 if( !pNew ){
169493 return SQLITE_NOMEM;
169494 }
169495 memset(pNew, 0, sizeof(SegmentNode));
169496 pNew->nData = 1 + FTS3_VARINT_MAX;
169497 pNew->aData = (char *)&pNew[1];
169498
169499 if( pTree ){
169500 SegmentNode *pParent = pTree->pParent;
169501 rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
169502 if( pTree->pParent==0 ){
169503 pTree->pParent = pParent;
169504 }
169505 pTree->pRight = pNew;
169506 pNew->pLeftmost = pTree->pLeftmost;
169507 pNew->pParent = pParent;
169508 pNew->zMalloc = pTree->zMalloc;
169509 pNew->nMalloc = pTree->nMalloc;
169510 pTree->zMalloc = 0;
169511 }else{
169512 pNew->pLeftmost = pNew;
169513 rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
169514 }
169515
169516 *ppTree = pNew;
169517 return rc;
169518}
169519
169520/*
169521** Helper function for fts3NodeWrite().
169522*/
169523static int fts3TreeFinishNode(
169524 SegmentNode *pTree,
169525 int iHeight,
169526 sqlite3_int64 iLeftChild
169527){
169528 int nStart;
169529 assert( iHeight>=1 && iHeight<128 );
169530 nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
169531 pTree->aData[nStart] = (char)iHeight;
169532 sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
169533 return nStart;
169534}
169535
169536/*
169537** Write the buffer for the segment node pTree and all of its peers to the
169538** database. Then call this function recursively to write the parent of
169539** pTree and its peers to the database.
169540**
169541** Except, if pTree is a root node, do not write it to the database. Instead,
169542** set output variables *paRoot and *pnRoot to contain the root node.
169543**
169544** If successful, SQLITE_OK is returned and output variable *piLast is
169545** set to the largest blockid written to the database (or zero if no
169546** blocks were written to the db). Otherwise, an SQLite error code is
169547** returned.
169548*/
169549static int fts3NodeWrite(
169550 Fts3Table *p, /* Virtual table handle */
169551 SegmentNode *pTree, /* SegmentNode handle */
169552 int iHeight, /* Height of this node in tree */
169553 sqlite3_int64 iLeaf, /* Block id of first leaf node */
169554 sqlite3_int64 iFree, /* Block id of next free slot in %_segments */
169555 sqlite3_int64 *piLast, /* OUT: Block id of last entry written */
169556 char **paRoot, /* OUT: Data for root node */
169557 int *pnRoot /* OUT: Size of root node in bytes */
169558){
169559 int rc = SQLITE_OK;
169560
169561 if( !pTree->pParent ){
169562 /* Root node of the tree. */
169563 int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
169564 *piLast = iFree-1;
169565 *pnRoot = pTree->nData - nStart;
169566 *paRoot = &pTree->aData[nStart];
169567 }else{
169568 SegmentNode *pIter;
169569 sqlite3_int64 iNextFree = iFree;
169570 sqlite3_int64 iNextLeaf = iLeaf;
169571 for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
169572 int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
169573 int nWrite = pIter->nData - nStart;
169574
169575 rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
169576 iNextFree++;
169577 iNextLeaf += (pIter->nEntry+1);
169578 }
169579 if( rc==SQLITE_OK ){
169580 assert( iNextLeaf==iFree );
169581 rc = fts3NodeWrite(
169582 p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
169583 );
169584 }
169585 }
169586
169587 return rc;
169588}
169589
169590/*
169591** Free all memory allocations associated with the tree pTree.
169592*/
169593static void fts3NodeFree(SegmentNode *pTree){
169594 if( pTree ){
169595 SegmentNode *p = pTree->pLeftmost;
169596 fts3NodeFree(p->pParent);
169597 while( p ){
169598 SegmentNode *pRight = p->pRight;
169599 if( p->aData!=(char *)&p[1] ){
169600 sqlite3_free(p->aData);
169601 }
169602 assert( pRight==0 || p->zMalloc==0 );
169603 sqlite3_free(p->zMalloc);
169604 sqlite3_free(p);
169605 p = pRight;
169606 }
169607 }
169608}
169609
169610/*
169611** Add a term to the segment being constructed by the SegmentWriter object
169612** *ppWriter. When adding the first term to a segment, *ppWriter should
169613** be passed NULL. This function will allocate a new SegmentWriter object
169614** and return it via the input/output variable *ppWriter in this case.
169615**
169616** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
169617*/
169618static int fts3SegWriterAdd(
169619 Fts3Table *p, /* Virtual table handle */
169620 SegmentWriter **ppWriter, /* IN/OUT: SegmentWriter handle */
169621 int isCopyTerm, /* True if buffer zTerm must be copied */
169622 const char *zTerm, /* Pointer to buffer containing term */
169623 int nTerm, /* Size of term in bytes */
169624 const char *aDoclist, /* Pointer to buffer containing doclist */
169625 int nDoclist /* Size of doclist in bytes */
169626){
169627 int nPrefix; /* Size of term prefix in bytes */
169628 int nSuffix; /* Size of term suffix in bytes */
169629 int nReq; /* Number of bytes required on leaf page */
169630 int nData;
169631 SegmentWriter *pWriter = *ppWriter;
169632
169633 if( !pWriter ){
169634 int rc;
169635 sqlite3_stmt *pStmt;
169636
169637 /* Allocate the SegmentWriter structure */
169638 pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
169639 if( !pWriter ) return SQLITE_NOMEM;
169640 memset(pWriter, 0, sizeof(SegmentWriter));
169641 *ppWriter = pWriter;
169642
169643 /* Allocate a buffer in which to accumulate data */
169644 pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
169645 if( !pWriter->aData ) return SQLITE_NOMEM;
169646 pWriter->nSize = p->nNodeSize;
169647
169648 /* Find the next free blockid in the %_segments table */
169649 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
169650 if( rc!=SQLITE_OK ) return rc;
169651 if( SQLITE_ROW==sqlite3_step(pStmt) ){
169652 pWriter->iFree = sqlite3_column_int64(pStmt, 0);
169653 pWriter->iFirst = pWriter->iFree;
169654 }
169655 rc = sqlite3_reset(pStmt);
169656 if( rc!=SQLITE_OK ) return rc;
169657 }
169658 nData = pWriter->nData;
169659
169660 nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
169661 nSuffix = nTerm-nPrefix;
169662
169663 /* Figure out how many bytes are required by this new entry */
169664 nReq = sqlite3Fts3VarintLen(nPrefix) + /* varint containing prefix size */
169665 sqlite3Fts3VarintLen(nSuffix) + /* varint containing suffix size */
169666 nSuffix + /* Term suffix */
169667 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
169668 nDoclist; /* Doclist data */
169669
169670 if( nData>0 && nData+nReq>p->nNodeSize ){
169671 int rc;
169672
169673 /* The current leaf node is full. Write it out to the database. */
169674 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
169675 if( rc!=SQLITE_OK ) return rc;
169676 p->nLeafAdd++;
169677
169678 /* Add the current term to the interior node tree. The term added to
169679 ** the interior tree must:
169680 **
169681 ** a) be greater than the largest term on the leaf node just written
169682 ** to the database (still available in pWriter->zTerm), and
169683 **
169684 ** b) be less than or equal to the term about to be added to the new
169685 ** leaf node (zTerm/nTerm).
169686 **
169687 ** In other words, it must be the prefix of zTerm 1 byte longer than
169688 ** the common prefix (if any) of zTerm and pWriter->zTerm.
169689 */
169690 assert( nPrefix<nTerm );
169691 rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
169692 if( rc!=SQLITE_OK ) return rc;
169693
169694 nData = 0;
169695 pWriter->nTerm = 0;
169696
169697 nPrefix = 0;
169698 nSuffix = nTerm;
169699 nReq = 1 + /* varint containing prefix size */
169700 sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */
169701 nTerm + /* Term suffix */
169702 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
169703 nDoclist; /* Doclist data */
169704 }
169705
169706 /* Increase the total number of bytes written to account for the new entry. */
169707 pWriter->nLeafData += nReq;
169708
169709 /* If the buffer currently allocated is too small for this entry, realloc
169710 ** the buffer to make it large enough.
169711 */
169712 if( nReq>pWriter->nSize ){
169713 char *aNew = sqlite3_realloc(pWriter->aData, nReq);
169714 if( !aNew ) return SQLITE_NOMEM;
169715 pWriter->aData = aNew;
169716 pWriter->nSize = nReq;
169717 }
169718 assert( nData+nReq<=pWriter->nSize );
169719
169720 /* Append the prefix-compressed term and doclist to the buffer. */
169721 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
169722 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
169723 memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
169724 nData += nSuffix;
169725 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
169726 memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
169727 pWriter->nData = nData + nDoclist;
169728
169729 /* Save the current term so that it can be used to prefix-compress the next.
169730 ** If the isCopyTerm parameter is true, then the buffer pointed to by
169731 ** zTerm is transient, so take a copy of the term data. Otherwise, just
169732 ** store a copy of the pointer.
169733 */
169734 if( isCopyTerm ){
169735 if( nTerm>pWriter->nMalloc ){
169736 char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
169737 if( !zNew ){
169738 return SQLITE_NOMEM;
169739 }
169740 pWriter->nMalloc = nTerm*2;
169741 pWriter->zMalloc = zNew;
169742 pWriter->zTerm = zNew;
169743 }
169744 assert( pWriter->zTerm==pWriter->zMalloc );
169745 memcpy(pWriter->zTerm, zTerm, nTerm);
169746 }else{
169747 pWriter->zTerm = (char *)zTerm;
169748 }
169749 pWriter->nTerm = nTerm;
169750
169751 return SQLITE_OK;
169752}
169753
169754/*
169755** Flush all data associated with the SegmentWriter object pWriter to the
169756** database. This function must be called after all terms have been added
169757** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
169758** returned. Otherwise, an SQLite error code.
169759*/
169760static int fts3SegWriterFlush(
169761 Fts3Table *p, /* Virtual table handle */
169762 SegmentWriter *pWriter, /* SegmentWriter to flush to the db */
169763 sqlite3_int64 iLevel, /* Value for 'level' column of %_segdir */
169764 int iIdx /* Value for 'idx' column of %_segdir */
169765){
169766 int rc; /* Return code */
169767 if( pWriter->pTree ){
169768 sqlite3_int64 iLast = 0; /* Largest block id written to database */
169769 sqlite3_int64 iLastLeaf; /* Largest leaf block id written to db */
169770 char *zRoot = NULL; /* Pointer to buffer containing root node */
169771 int nRoot = 0; /* Size of buffer zRoot */
169772
169773 iLastLeaf = pWriter->iFree;
169774 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
169775 if( rc==SQLITE_OK ){
169776 rc = fts3NodeWrite(p, pWriter->pTree, 1,
169777 pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
169778 }
169779 if( rc==SQLITE_OK ){
169780 rc = fts3WriteSegdir(p, iLevel, iIdx,
169781 pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
169782 }
169783 }else{
169784 /* The entire tree fits on the root node. Write it to the segdir table. */
169785 rc = fts3WriteSegdir(p, iLevel, iIdx,
169786 0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
169787 }
169788 p->nLeafAdd++;
169789 return rc;
169790}
169791
169792/*
169793** Release all memory held by the SegmentWriter object passed as the
169794** first argument.
169795*/
169796static void fts3SegWriterFree(SegmentWriter *pWriter){
169797 if( pWriter ){
169798 sqlite3_free(pWriter->aData);
169799 sqlite3_free(pWriter->zMalloc);
169800 fts3NodeFree(pWriter->pTree);
169801 sqlite3_free(pWriter);
169802 }
169803}
169804
169805/*
169806** The first value in the apVal[] array is assumed to contain an integer.
169807** This function tests if there exist any documents with docid values that
169808** are different from that integer. i.e. if deleting the document with docid
169809** pRowid would mean the FTS3 table were empty.
169810**
169811** If successful, *pisEmpty is set to true if the table is empty except for
169812** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
169813** error occurs, an SQLite error code is returned.
169814*/
169815static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
169816 sqlite3_stmt *pStmt;
169817 int rc;
169818 if( p->zContentTbl ){
169819 /* If using the content=xxx option, assume the table is never empty */
169820 *pisEmpty = 0;
169821 rc = SQLITE_OK;
169822 }else{
169823 rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
169824 if( rc==SQLITE_OK ){
169825 if( SQLITE_ROW==sqlite3_step(pStmt) ){
169826 *pisEmpty = sqlite3_column_int(pStmt, 0);
169827 }
169828 rc = sqlite3_reset(pStmt);
169829 }
169830 }
169831 return rc;
169832}
169833
169834/*
169835** Set *pnMax to the largest segment level in the database for the index
169836** iIndex.
169837**
169838** Segment levels are stored in the 'level' column of the %_segdir table.
169839**
169840** Return SQLITE_OK if successful, or an SQLite error code if not.
169841*/
169842static int fts3SegmentMaxLevel(
169843 Fts3Table *p,
169844 int iLangid,
169845 int iIndex,
169846 sqlite3_int64 *pnMax
169847){
169848 sqlite3_stmt *pStmt;
169849 int rc;
169850 assert( iIndex>=0 && iIndex<p->nIndex );
169851
169852 /* Set pStmt to the compiled version of:
169853 **
169854 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
169855 **
169856 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
169857 */
169858 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
169859 if( rc!=SQLITE_OK ) return rc;
169860 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
169861 sqlite3_bind_int64(pStmt, 2,
169862 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
169863 );
169864 if( SQLITE_ROW==sqlite3_step(pStmt) ){
169865 *pnMax = sqlite3_column_int64(pStmt, 0);
169866 }
169867 return sqlite3_reset(pStmt);
169868}
169869
169870/*
169871** iAbsLevel is an absolute level that may be assumed to exist within
169872** the database. This function checks if it is the largest level number
169873** within its index. Assuming no error occurs, *pbMax is set to 1 if
169874** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
169875** is returned. If an error occurs, an error code is returned and the
169876** final value of *pbMax is undefined.
169877*/
169878static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
169879
169880 /* Set pStmt to the compiled version of:
169881 **
169882 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
169883 **
169884 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
169885 */
169886 sqlite3_stmt *pStmt;
169887 int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
169888 if( rc!=SQLITE_OK ) return rc;
169889 sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
169890 sqlite3_bind_int64(pStmt, 2,
169891 ((iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL
169892 );
169893
169894 *pbMax = 0;
169895 if( SQLITE_ROW==sqlite3_step(pStmt) ){
169896 *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL;
169897 }
169898 return sqlite3_reset(pStmt);
169899}
169900
169901/*
169902** Delete all entries in the %_segments table associated with the segment
169903** opened with seg-reader pSeg. This function does not affect the contents
169904** of the %_segdir table.
169905*/
169906static int fts3DeleteSegment(
169907 Fts3Table *p, /* FTS table handle */
169908 Fts3SegReader *pSeg /* Segment to delete */
169909){
169910 int rc = SQLITE_OK; /* Return code */
169911 if( pSeg->iStartBlock ){
169912 sqlite3_stmt *pDelete; /* SQL statement to delete rows */
169913 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
169914 if( rc==SQLITE_OK ){
169915 sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
169916 sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
169917 sqlite3_step(pDelete);
169918 rc = sqlite3_reset(pDelete);
169919 }
169920 }
169921 return rc;
169922}
169923
169924/*
169925** This function is used after merging multiple segments into a single large
169926** segment to delete the old, now redundant, segment b-trees. Specifically,
169927** it:
169928**
169929** 1) Deletes all %_segments entries for the segments associated with
169930** each of the SegReader objects in the array passed as the third
169931** argument, and
169932**
169933** 2) deletes all %_segdir entries with level iLevel, or all %_segdir
169934** entries regardless of level if (iLevel<0).
169935**
169936** SQLITE_OK is returned if successful, otherwise an SQLite error code.
169937*/
169938static int fts3DeleteSegdir(
169939 Fts3Table *p, /* Virtual table handle */
169940 int iLangid, /* Language id */
169941 int iIndex, /* Index for p->aIndex */
169942 int iLevel, /* Level of %_segdir entries to delete */
169943 Fts3SegReader **apSegment, /* Array of SegReader objects */
169944 int nReader /* Size of array apSegment */
169945){
169946 int rc = SQLITE_OK; /* Return Code */
169947 int i; /* Iterator variable */
169948 sqlite3_stmt *pDelete = 0; /* SQL statement to delete rows */
169949
169950 for(i=0; rc==SQLITE_OK && i<nReader; i++){
169951 rc = fts3DeleteSegment(p, apSegment[i]);
169952 }
169953 if( rc!=SQLITE_OK ){
169954 return rc;
169955 }
169956
169957 assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
169958 if( iLevel==FTS3_SEGCURSOR_ALL ){
169959 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
169960 if( rc==SQLITE_OK ){
169961 sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
169962 sqlite3_bind_int64(pDelete, 2,
169963 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
169964 );
169965 }
169966 }else{
169967 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
169968 if( rc==SQLITE_OK ){
169969 sqlite3_bind_int64(
169970 pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
169971 );
169972 }
169973 }
169974
169975 if( rc==SQLITE_OK ){
169976 sqlite3_step(pDelete);
169977 rc = sqlite3_reset(pDelete);
169978 }
169979
169980 return rc;
169981}
169982
169983/*
169984** When this function is called, buffer *ppList (size *pnList bytes) contains
169985** a position list that may (or may not) feature multiple columns. This
169986** function adjusts the pointer *ppList and the length *pnList so that they
169987** identify the subset of the position list that corresponds to column iCol.
169988**
169989** If there are no entries in the input position list for column iCol, then
169990** *pnList is set to zero before returning.
169991**
169992** If parameter bZero is non-zero, then any part of the input list following
169993** the end of the output list is zeroed before returning.
169994*/
169995static void fts3ColumnFilter(
169996 int iCol, /* Column to filter on */
169997 int bZero, /* Zero out anything following *ppList */
169998 char **ppList, /* IN/OUT: Pointer to position list */
169999 int *pnList /* IN/OUT: Size of buffer *ppList in bytes */
170000){
170001 char *pList = *ppList;
170002 int nList = *pnList;
170003 char *pEnd = &pList[nList];
170004 int iCurrent = 0;
170005 char *p = pList;
170006
170007 assert( iCol>=0 );
170008 while( 1 ){
170009 char c = 0;
170010 while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
170011
170012 if( iCol==iCurrent ){
170013 nList = (int)(p - pList);
170014 break;
170015 }
170016
170017 nList -= (int)(p - pList);
170018 pList = p;
170019 if( nList==0 ){
170020 break;
170021 }
170022 p = &pList[1];
170023 p += fts3GetVarint32(p, &iCurrent);
170024 }
170025
170026 if( bZero && &pList[nList]!=pEnd ){
170027 memset(&pList[nList], 0, pEnd - &pList[nList]);
170028 }
170029 *ppList = pList;
170030 *pnList = nList;
170031}
170032
170033/*
170034** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
170035** existing data). Grow the buffer if required.
170036**
170037** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
170038** trying to resize the buffer, return SQLITE_NOMEM.
170039*/
170040static int fts3MsrBufferData(
170041 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
170042 char *pList,
170043 int nList
170044){
170045 if( nList>pMsr->nBuffer ){
170046 char *pNew;
170047 pMsr->nBuffer = nList*2;
170048 pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
170049 if( !pNew ) return SQLITE_NOMEM;
170050 pMsr->aBuffer = pNew;
170051 }
170052
170053 memcpy(pMsr->aBuffer, pList, nList);
170054 return SQLITE_OK;
170055}
170056
170057SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
170058 Fts3Table *p, /* Virtual table handle */
170059 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
170060 sqlite3_int64 *piDocid, /* OUT: Docid value */
170061 char **paPoslist, /* OUT: Pointer to position list */
170062 int *pnPoslist /* OUT: Size of position list in bytes */
170063){
170064 int nMerge = pMsr->nAdvance;
170065 Fts3SegReader **apSegment = pMsr->apSegment;
170066 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
170067 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
170068 );
170069
170070 if( nMerge==0 ){
170071 *paPoslist = 0;
170072 return SQLITE_OK;
170073 }
170074
170075 while( 1 ){
170076 Fts3SegReader *pSeg;
170077 pSeg = pMsr->apSegment[0];
170078
170079 if( pSeg->pOffsetList==0 ){
170080 *paPoslist = 0;
170081 break;
170082 }else{
170083 int rc;
170084 char *pList;
170085 int nList;
170086 int j;
170087 sqlite3_int64 iDocid = apSegment[0]->iDocid;
170088
170089 rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
170090 j = 1;
170091 while( rc==SQLITE_OK
170092 && j<nMerge
170093 && apSegment[j]->pOffsetList
170094 && apSegment[j]->iDocid==iDocid
170095 ){
170096 rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
170097 j++;
170098 }
170099 if( rc!=SQLITE_OK ) return rc;
170100 fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
170101
170102 if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
170103 rc = fts3MsrBufferData(pMsr, pList, nList+1);
170104 if( rc!=SQLITE_OK ) return rc;
170105 assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
170106 pList = pMsr->aBuffer;
170107 }
170108
170109 if( pMsr->iColFilter>=0 ){
170110 fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
170111 }
170112
170113 if( nList>0 ){
170114 *paPoslist = pList;
170115 *piDocid = iDocid;
170116 *pnPoslist = nList;
170117 break;
170118 }
170119 }
170120 }
170121
170122 return SQLITE_OK;
170123}
170124
170125static int fts3SegReaderStart(
170126 Fts3Table *p, /* Virtual table handle */
170127 Fts3MultiSegReader *pCsr, /* Cursor object */
170128 const char *zTerm, /* Term searched for (or NULL) */
170129 int nTerm /* Length of zTerm in bytes */
170130){
170131 int i;
170132 int nSeg = pCsr->nSegment;
170133
170134 /* If the Fts3SegFilter defines a specific term (or term prefix) to search
170135 ** for, then advance each segment iterator until it points to a term of
170136 ** equal or greater value than the specified term. This prevents many
170137 ** unnecessary merge/sort operations for the case where single segment
170138 ** b-tree leaf nodes contain more than one term.
170139 */
170140 for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
170141 int res = 0;
170142 Fts3SegReader *pSeg = pCsr->apSegment[i];
170143 do {
170144 int rc = fts3SegReaderNext(p, pSeg, 0);
170145 if( rc!=SQLITE_OK ) return rc;
170146 }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
170147
170148 if( pSeg->bLookup && res!=0 ){
170149 fts3SegReaderSetEof(pSeg);
170150 }
170151 }
170152 fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
170153
170154 return SQLITE_OK;
170155}
170156
170157SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
170158 Fts3Table *p, /* Virtual table handle */
170159 Fts3MultiSegReader *pCsr, /* Cursor object */
170160 Fts3SegFilter *pFilter /* Restrictions on range of iteration */
170161){
170162 pCsr->pFilter = pFilter;
170163 return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
170164}
170165
170166SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
170167 Fts3Table *p, /* Virtual table handle */
170168 Fts3MultiSegReader *pCsr, /* Cursor object */
170169 int iCol, /* Column to match on. */
170170 const char *zTerm, /* Term to iterate through a doclist for */
170171 int nTerm /* Number of bytes in zTerm */
170172){
170173 int i;
170174 int rc;
170175 int nSegment = pCsr->nSegment;
170176 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
170177 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
170178 );
170179
170180 assert( pCsr->pFilter==0 );
170181 assert( zTerm && nTerm>0 );
170182
170183 /* Advance each segment iterator until it points to the term zTerm/nTerm. */
170184 rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
170185 if( rc!=SQLITE_OK ) return rc;
170186
170187 /* Determine how many of the segments actually point to zTerm/nTerm. */
170188 for(i=0; i<nSegment; i++){
170189 Fts3SegReader *pSeg = pCsr->apSegment[i];
170190 if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
170191 break;
170192 }
170193 }
170194 pCsr->nAdvance = i;
170195
170196 /* Advance each of the segments to point to the first docid. */
170197 for(i=0; i<pCsr->nAdvance; i++){
170198 rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
170199 if( rc!=SQLITE_OK ) return rc;
170200 }
170201 fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
170202
170203 assert( iCol<0 || iCol<p->nColumn );
170204 pCsr->iColFilter = iCol;
170205
170206 return SQLITE_OK;
170207}
170208
170209/*
170210** This function is called on a MultiSegReader that has been started using
170211** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
170212** have been made. Calling this function puts the MultiSegReader in such
170213** a state that if the next two calls are:
170214**
170215** sqlite3Fts3SegReaderStart()
170216** sqlite3Fts3SegReaderStep()
170217**
170218** then the entire doclist for the term is available in
170219** MultiSegReader.aDoclist/nDoclist.
170220*/
170221SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
170222 int i; /* Used to iterate through segment-readers */
170223
170224 assert( pCsr->zTerm==0 );
170225 assert( pCsr->nTerm==0 );
170226 assert( pCsr->aDoclist==0 );
170227 assert( pCsr->nDoclist==0 );
170228
170229 pCsr->nAdvance = 0;
170230 pCsr->bRestart = 1;
170231 for(i=0; i<pCsr->nSegment; i++){
170232 pCsr->apSegment[i]->pOffsetList = 0;
170233 pCsr->apSegment[i]->nOffsetList = 0;
170234 pCsr->apSegment[i]->iDocid = 0;
170235 }
170236
170237 return SQLITE_OK;
170238}
170239
170240
170241SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
170242 Fts3Table *p, /* Virtual table handle */
170243 Fts3MultiSegReader *pCsr /* Cursor object */
170244){
170245 int rc = SQLITE_OK;
170246
170247 int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
170248 int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
170249 int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
170250 int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
170251 int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
170252 int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
170253
170254 Fts3SegReader **apSegment = pCsr->apSegment;
170255 int nSegment = pCsr->nSegment;
170256 Fts3SegFilter *pFilter = pCsr->pFilter;
170257 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
170258 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
170259 );
170260
170261 if( pCsr->nSegment==0 ) return SQLITE_OK;
170262
170263 do {
170264 int nMerge;
170265 int i;
170266
170267 /* Advance the first pCsr->nAdvance entries in the apSegment[] array
170268 ** forward. Then sort the list in order of current term again.
170269 */
170270 for(i=0; i<pCsr->nAdvance; i++){
170271 Fts3SegReader *pSeg = apSegment[i];
170272 if( pSeg->bLookup ){
170273 fts3SegReaderSetEof(pSeg);
170274 }else{
170275 rc = fts3SegReaderNext(p, pSeg, 0);
170276 }
170277 if( rc!=SQLITE_OK ) return rc;
170278 }
170279 fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
170280 pCsr->nAdvance = 0;
170281
170282 /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
170283 assert( rc==SQLITE_OK );
170284 if( apSegment[0]->aNode==0 ) break;
170285
170286 pCsr->nTerm = apSegment[0]->nTerm;
170287 pCsr->zTerm = apSegment[0]->zTerm;
170288
170289 /* If this is a prefix-search, and if the term that apSegment[0] points
170290 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
170291 ** required callbacks have been made. In this case exit early.
170292 **
170293 ** Similarly, if this is a search for an exact match, and the first term
170294 ** of segment apSegment[0] is not a match, exit early.
170295 */
170296 if( pFilter->zTerm && !isScan ){
170297 if( pCsr->nTerm<pFilter->nTerm
170298 || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
170299 || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
170300 ){
170301 break;
170302 }
170303 }
170304
170305 nMerge = 1;
170306 while( nMerge<nSegment
170307 && apSegment[nMerge]->aNode
170308 && apSegment[nMerge]->nTerm==pCsr->nTerm
170309 && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
170310 ){
170311 nMerge++;
170312 }
170313
170314 assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
170315 if( nMerge==1
170316 && !isIgnoreEmpty
170317 && !isFirst
170318 && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
170319 ){
170320 pCsr->nDoclist = apSegment[0]->nDoclist;
170321 if( fts3SegReaderIsPending(apSegment[0]) ){
170322 rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
170323 pCsr->aDoclist = pCsr->aBuffer;
170324 }else{
170325 pCsr->aDoclist = apSegment[0]->aDoclist;
170326 }
170327 if( rc==SQLITE_OK ) rc = SQLITE_ROW;
170328 }else{
170329 int nDoclist = 0; /* Size of doclist */
170330 sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
170331
170332 /* The current term of the first nMerge entries in the array
170333 ** of Fts3SegReader objects is the same. The doclists must be merged
170334 ** and a single term returned with the merged doclist.
170335 */
170336 for(i=0; i<nMerge; i++){
170337 fts3SegReaderFirstDocid(p, apSegment[i]);
170338 }
170339 fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
170340 while( apSegment[0]->pOffsetList ){
170341 int j; /* Number of segments that share a docid */
170342 char *pList = 0;
170343 int nList = 0;
170344 int nByte;
170345 sqlite3_int64 iDocid = apSegment[0]->iDocid;
170346 fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
170347 j = 1;
170348 while( j<nMerge
170349 && apSegment[j]->pOffsetList
170350 && apSegment[j]->iDocid==iDocid
170351 ){
170352 fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
170353 j++;
170354 }
170355
170356 if( isColFilter ){
170357 fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
170358 }
170359
170360 if( !isIgnoreEmpty || nList>0 ){
170361
170362 /* Calculate the 'docid' delta value to write into the merged
170363 ** doclist. */
170364 sqlite3_int64 iDelta;
170365 if( p->bDescIdx && nDoclist>0 ){
170366 iDelta = iPrev - iDocid;
170367 }else{
170368 iDelta = iDocid - iPrev;
170369 }
170370 assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
170371 assert( nDoclist>0 || iDelta==iDocid );
170372
170373 nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
170374 if( nDoclist+nByte>pCsr->nBuffer ){
170375 char *aNew;
170376 pCsr->nBuffer = (nDoclist+nByte)*2;
170377 aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
170378 if( !aNew ){
170379 return SQLITE_NOMEM;
170380 }
170381 pCsr->aBuffer = aNew;
170382 }
170383
170384 if( isFirst ){
170385 char *a = &pCsr->aBuffer[nDoclist];
170386 int nWrite;
170387
170388 nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
170389 if( nWrite ){
170390 iPrev = iDocid;
170391 nDoclist += nWrite;
170392 }
170393 }else{
170394 nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
170395 iPrev = iDocid;
170396 if( isRequirePos ){
170397 memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
170398 nDoclist += nList;
170399 pCsr->aBuffer[nDoclist++] = '\0';
170400 }
170401 }
170402 }
170403
170404 fts3SegReaderSort(apSegment, nMerge, j, xCmp);
170405 }
170406 if( nDoclist>0 ){
170407 pCsr->aDoclist = pCsr->aBuffer;
170408 pCsr->nDoclist = nDoclist;
170409 rc = SQLITE_ROW;
170410 }
170411 }
170412 pCsr->nAdvance = nMerge;
170413 }while( rc==SQLITE_OK );
170414
170415 return rc;
170416}
170417
170418
170419SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
170420 Fts3MultiSegReader *pCsr /* Cursor object */
170421){
170422 if( pCsr ){
170423 int i;
170424 for(i=0; i<pCsr->nSegment; i++){
170425 sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
170426 }
170427 sqlite3_free(pCsr->apSegment);
170428 sqlite3_free(pCsr->aBuffer);
170429
170430 pCsr->nSegment = 0;
170431 pCsr->apSegment = 0;
170432 pCsr->aBuffer = 0;
170433 }
170434}
170435
170436/*
170437** Decode the "end_block" field, selected by column iCol of the SELECT
170438** statement passed as the first argument.
170439**
170440** The "end_block" field may contain either an integer, or a text field
170441** containing the text representation of two non-negative integers separated
170442** by one or more space (0x20) characters. In the first case, set *piEndBlock
170443** to the integer value and *pnByte to zero before returning. In the second,
170444** set *piEndBlock to the first value and *pnByte to the second.
170445*/
170446static void fts3ReadEndBlockField(
170447 sqlite3_stmt *pStmt,
170448 int iCol,
170449 i64 *piEndBlock,
170450 i64 *pnByte
170451){
170452 const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
170453 if( zText ){
170454 int i;
170455 int iMul = 1;
170456 i64 iVal = 0;
170457 for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
170458 iVal = iVal*10 + (zText[i] - '0');
170459 }
170460 *piEndBlock = iVal;
170461 while( zText[i]==' ' ) i++;
170462 iVal = 0;
170463 if( zText[i]=='-' ){
170464 i++;
170465 iMul = -1;
170466 }
170467 for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
170468 iVal = iVal*10 + (zText[i] - '0');
170469 }
170470 *pnByte = (iVal * (i64)iMul);
170471 }
170472}
170473
170474
170475/*
170476** A segment of size nByte bytes has just been written to absolute level
170477** iAbsLevel. Promote any segments that should be promoted as a result.
170478*/
170479static int fts3PromoteSegments(
170480 Fts3Table *p, /* FTS table handle */
170481 sqlite3_int64 iAbsLevel, /* Absolute level just updated */
170482 sqlite3_int64 nByte /* Size of new segment at iAbsLevel */
170483){
170484 int rc = SQLITE_OK;
170485 sqlite3_stmt *pRange;
170486
170487 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, &pRange, 0);
170488
170489 if( rc==SQLITE_OK ){
170490 int bOk = 0;
170491 i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
170492 i64 nLimit = (nByte*3)/2;
170493
170494 /* Loop through all entries in the %_segdir table corresponding to
170495 ** segments in this index on levels greater than iAbsLevel. If there is
170496 ** at least one such segment, and it is possible to determine that all
170497 ** such segments are smaller than nLimit bytes in size, they will be
170498 ** promoted to level iAbsLevel. */
170499 sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
170500 sqlite3_bind_int64(pRange, 2, iLast);
170501 while( SQLITE_ROW==sqlite3_step(pRange) ){
170502 i64 nSize = 0, dummy;
170503 fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
170504 if( nSize<=0 || nSize>nLimit ){
170505 /* If nSize==0, then the %_segdir.end_block field does not not
170506 ** contain a size value. This happens if it was written by an
170507 ** old version of FTS. In this case it is not possible to determine
170508 ** the size of the segment, and so segment promotion does not
170509 ** take place. */
170510 bOk = 0;
170511 break;
170512 }
170513 bOk = 1;
170514 }
170515 rc = sqlite3_reset(pRange);
170516
170517 if( bOk ){
170518 int iIdx = 0;
170519 sqlite3_stmt *pUpdate1 = 0;
170520 sqlite3_stmt *pUpdate2 = 0;
170521
170522 if( rc==SQLITE_OK ){
170523 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, &pUpdate1, 0);
170524 }
170525 if( rc==SQLITE_OK ){
170526 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, &pUpdate2, 0);
170527 }
170528
170529 if( rc==SQLITE_OK ){
170530
170531 /* Loop through all %_segdir entries for segments in this index with
170532 ** levels equal to or greater than iAbsLevel. As each entry is visited,
170533 ** updated it to set (level = -1) and (idx = N), where N is 0 for the
170534 ** oldest segment in the range, 1 for the next oldest, and so on.
170535 **
170536 ** In other words, move all segments being promoted to level -1,
170537 ** setting the "idx" fields as appropriate to keep them in the same
170538 ** order. The contents of level -1 (which is never used, except
170539 ** transiently here), will be moved back to level iAbsLevel below. */
170540 sqlite3_bind_int64(pRange, 1, iAbsLevel);
170541 while( SQLITE_ROW==sqlite3_step(pRange) ){
170542 sqlite3_bind_int(pUpdate1, 1, iIdx++);
170543 sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
170544 sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
170545 sqlite3_step(pUpdate1);
170546 rc = sqlite3_reset(pUpdate1);
170547 if( rc!=SQLITE_OK ){
170548 sqlite3_reset(pRange);
170549 break;
170550 }
170551 }
170552 }
170553 if( rc==SQLITE_OK ){
170554 rc = sqlite3_reset(pRange);
170555 }
170556
170557 /* Move level -1 to level iAbsLevel */
170558 if( rc==SQLITE_OK ){
170559 sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
170560 sqlite3_step(pUpdate2);
170561 rc = sqlite3_reset(pUpdate2);
170562 }
170563 }
170564 }
170565
170566
170567 return rc;
170568}
170569
170570/*
170571** Merge all level iLevel segments in the database into a single
170572** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
170573** single segment with a level equal to the numerically largest level
170574** currently present in the database.
170575**
170576** If this function is called with iLevel<0, but there is only one
170577** segment in the database, SQLITE_DONE is returned immediately.
170578** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
170579** an SQLite error code is returned.
170580*/
170581static int fts3SegmentMerge(
170582 Fts3Table *p,
170583 int iLangid, /* Language id to merge */
170584 int iIndex, /* Index in p->aIndex[] to merge */
170585 int iLevel /* Level to merge */
170586){
170587 int rc; /* Return code */
170588 int iIdx = 0; /* Index of new segment */
170589 sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */
170590 SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
170591 Fts3SegFilter filter; /* Segment term filter condition */
170592 Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
170593 int bIgnoreEmpty = 0; /* True to ignore empty segments */
170594 i64 iMaxLevel = 0; /* Max level number for this index/langid */
170595
170596 assert( iLevel==FTS3_SEGCURSOR_ALL
170597 || iLevel==FTS3_SEGCURSOR_PENDING
170598 || iLevel>=0
170599 );
170600 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
170601 assert( iIndex>=0 && iIndex<p->nIndex );
170602
170603 rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
170604 if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
170605
170606 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
170607 rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
170608 if( rc!=SQLITE_OK ) goto finished;
170609 }
170610
170611 if( iLevel==FTS3_SEGCURSOR_ALL ){
170612 /* This call is to merge all segments in the database to a single
170613 ** segment. The level of the new segment is equal to the numerically
170614 ** greatest segment level currently present in the database for this
170615 ** index. The idx of the new segment is always 0. */
170616 if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0]) ){
170617 rc = SQLITE_DONE;
170618 goto finished;
170619 }
170620 iNewLevel = iMaxLevel;
170621 bIgnoreEmpty = 1;
170622
170623 }else{
170624 /* This call is to merge all segments at level iLevel. find the next
170625 ** available segment index at level iLevel+1. The call to
170626 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
170627 ** a single iLevel+2 segment if necessary. */
170628 assert( FTS3_SEGCURSOR_PENDING==-1 );
170629 iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
170630 rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
170631 bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel);
170632 }
170633 if( rc!=SQLITE_OK ) goto finished;
170634
170635 assert( csr.nSegment>0 );
170636 assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
170637 assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
170638
170639 memset(&filter, 0, sizeof(Fts3SegFilter));
170640 filter.flags = FTS3_SEGMENT_REQUIRE_POS;
170641 filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
170642
170643 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
170644 while( SQLITE_OK==rc ){
170645 rc = sqlite3Fts3SegReaderStep(p, &csr);
170646 if( rc!=SQLITE_ROW ) break;
170647 rc = fts3SegWriterAdd(p, &pWriter, 1,
170648 csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
170649 }
170650 if( rc!=SQLITE_OK ) goto finished;
170651 assert( pWriter || bIgnoreEmpty );
170652
170653 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
170654 rc = fts3DeleteSegdir(
170655 p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
170656 );
170657 if( rc!=SQLITE_OK ) goto finished;
170658 }
170659 if( pWriter ){
170660 rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
170661 if( rc==SQLITE_OK ){
170662 if( iLevel==FTS3_SEGCURSOR_PENDING || iNewLevel<iMaxLevel ){
170663 rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
170664 }
170665 }
170666 }
170667
170668 finished:
170669 fts3SegWriterFree(pWriter);
170670 sqlite3Fts3SegReaderFinish(&csr);
170671 return rc;
170672}
170673
170674
170675/*
170676** Flush the contents of pendingTerms to level 0 segments.
170677*/
170678SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
170679 int rc = SQLITE_OK;
170680 int i;
170681
170682 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
170683 rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
170684 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
170685 }
170686 sqlite3Fts3PendingTermsClear(p);
170687
170688 /* Determine the auto-incr-merge setting if unknown. If enabled,
170689 ** estimate the number of leaf blocks of content to be written
170690 */
170691 if( rc==SQLITE_OK && p->bHasStat
170692 && p->nAutoincrmerge==0xff && p->nLeafAdd>0
170693 ){
170694 sqlite3_stmt *pStmt = 0;
170695 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
170696 if( rc==SQLITE_OK ){
170697 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
170698 rc = sqlite3_step(pStmt);
170699 if( rc==SQLITE_ROW ){
170700 p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
170701 if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
170702 }else if( rc==SQLITE_DONE ){
170703 p->nAutoincrmerge = 0;
170704 }
170705 rc = sqlite3_reset(pStmt);
170706 }
170707 }
170708 return rc;
170709}
170710
170711/*
170712** Encode N integers as varints into a blob.
170713*/
170714static void fts3EncodeIntArray(
170715 int N, /* The number of integers to encode */
170716 u32 *a, /* The integer values */
170717 char *zBuf, /* Write the BLOB here */
170718 int *pNBuf /* Write number of bytes if zBuf[] used here */
170719){
170720 int i, j;
170721 for(i=j=0; i<N; i++){
170722 j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
170723 }
170724 *pNBuf = j;
170725}
170726
170727/*
170728** Decode a blob of varints into N integers
170729*/
170730static void fts3DecodeIntArray(
170731 int N, /* The number of integers to decode */
170732 u32 *a, /* Write the integer values */
170733 const char *zBuf, /* The BLOB containing the varints */
170734 int nBuf /* size of the BLOB */
170735){
170736 int i, j;
170737 UNUSED_PARAMETER(nBuf);
170738 for(i=j=0; i<N; i++){
170739 sqlite3_int64 x;
170740 j += sqlite3Fts3GetVarint(&zBuf[j], &x);
170741 assert(j<=nBuf);
170742 a[i] = (u32)(x & 0xffffffff);
170743 }
170744}
170745
170746/*
170747** Insert the sizes (in tokens) for each column of the document
170748** with docid equal to p->iPrevDocid. The sizes are encoded as
170749** a blob of varints.
170750*/
170751static void fts3InsertDocsize(
170752 int *pRC, /* Result code */
170753 Fts3Table *p, /* Table into which to insert */
170754 u32 *aSz /* Sizes of each column, in tokens */
170755){
170756 char *pBlob; /* The BLOB encoding of the document size */
170757 int nBlob; /* Number of bytes in the BLOB */
170758 sqlite3_stmt *pStmt; /* Statement used to insert the encoding */
170759 int rc; /* Result code from subfunctions */
170760
170761 if( *pRC ) return;
170762 pBlob = sqlite3_malloc( 10*p->nColumn );
170763 if( pBlob==0 ){
170764 *pRC = SQLITE_NOMEM;
170765 return;
170766 }
170767 fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
170768 rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
170769 if( rc ){
170770 sqlite3_free(pBlob);
170771 *pRC = rc;
170772 return;
170773 }
170774 sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
170775 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
170776 sqlite3_step(pStmt);
170777 *pRC = sqlite3_reset(pStmt);
170778}
170779
170780/*
170781** Record 0 of the %_stat table contains a blob consisting of N varints,
170782** where N is the number of user defined columns in the fts3 table plus
170783** two. If nCol is the number of user defined columns, then values of the
170784** varints are set as follows:
170785**
170786** Varint 0: Total number of rows in the table.
170787**
170788** Varint 1..nCol: For each column, the total number of tokens stored in
170789** the column for all rows of the table.
170790**
170791** Varint 1+nCol: The total size, in bytes, of all text values in all
170792** columns of all rows of the table.
170793**
170794*/
170795static void fts3UpdateDocTotals(
170796 int *pRC, /* The result code */
170797 Fts3Table *p, /* Table being updated */
170798 u32 *aSzIns, /* Size increases */
170799 u32 *aSzDel, /* Size decreases */
170800 int nChng /* Change in the number of documents */
170801){
170802 char *pBlob; /* Storage for BLOB written into %_stat */
170803 int nBlob; /* Size of BLOB written into %_stat */
170804 u32 *a; /* Array of integers that becomes the BLOB */
170805 sqlite3_stmt *pStmt; /* Statement for reading and writing */
170806 int i; /* Loop counter */
170807 int rc; /* Result code from subfunctions */
170808
170809 const int nStat = p->nColumn+2;
170810
170811 if( *pRC ) return;
170812 a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
170813 if( a==0 ){
170814 *pRC = SQLITE_NOMEM;
170815 return;
170816 }
170817 pBlob = (char*)&a[nStat];
170818 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
170819 if( rc ){
170820 sqlite3_free(a);
170821 *pRC = rc;
170822 return;
170823 }
170824 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
170825 if( sqlite3_step(pStmt)==SQLITE_ROW ){
170826 fts3DecodeIntArray(nStat, a,
170827 sqlite3_column_blob(pStmt, 0),
170828 sqlite3_column_bytes(pStmt, 0));
170829 }else{
170830 memset(a, 0, sizeof(u32)*(nStat) );
170831 }
170832 rc = sqlite3_reset(pStmt);
170833 if( rc!=SQLITE_OK ){
170834 sqlite3_free(a);
170835 *pRC = rc;
170836 return;
170837 }
170838 if( nChng<0 && a[0]<(u32)(-nChng) ){
170839 a[0] = 0;
170840 }else{
170841 a[0] += nChng;
170842 }
170843 for(i=0; i<p->nColumn+1; i++){
170844 u32 x = a[i+1];
170845 if( x+aSzIns[i] < aSzDel[i] ){
170846 x = 0;
170847 }else{
170848 x = x + aSzIns[i] - aSzDel[i];
170849 }
170850 a[i+1] = x;
170851 }
170852 fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
170853 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
170854 if( rc ){
170855 sqlite3_free(a);
170856 *pRC = rc;
170857 return;
170858 }
170859 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
170860 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
170861 sqlite3_step(pStmt);
170862 *pRC = sqlite3_reset(pStmt);
170863 sqlite3_bind_null(pStmt, 2);
170864 sqlite3_free(a);
170865}
170866
170867/*
170868** Merge the entire database so that there is one segment for each
170869** iIndex/iLangid combination.
170870*/
170871static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
170872 int bSeenDone = 0;
170873 int rc;
170874 sqlite3_stmt *pAllLangid = 0;
170875
170876 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
170877 if( rc==SQLITE_OK ){
170878 int rc2;
170879 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
170880 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
170881 while( sqlite3_step(pAllLangid)==SQLITE_ROW ){
170882 int i;
170883 int iLangid = sqlite3_column_int(pAllLangid, 0);
170884 for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
170885 rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
170886 if( rc==SQLITE_DONE ){
170887 bSeenDone = 1;
170888 rc = SQLITE_OK;
170889 }
170890 }
170891 }
170892 rc2 = sqlite3_reset(pAllLangid);
170893 if( rc==SQLITE_OK ) rc = rc2;
170894 }
170895
170896 sqlite3Fts3SegmentsClose(p);
170897 sqlite3Fts3PendingTermsClear(p);
170898
170899 return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
170900}
170901
170902/*
170903** This function is called when the user executes the following statement:
170904**
170905** INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
170906**
170907** The entire FTS index is discarded and rebuilt. If the table is one
170908** created using the content=xxx option, then the new index is based on
170909** the current contents of the xxx table. Otherwise, it is rebuilt based
170910** on the contents of the %_content table.
170911*/
170912static int fts3DoRebuild(Fts3Table *p){
170913 int rc; /* Return Code */
170914
170915 rc = fts3DeleteAll(p, 0);
170916 if( rc==SQLITE_OK ){
170917 u32 *aSz = 0;
170918 u32 *aSzIns = 0;
170919 u32 *aSzDel = 0;
170920 sqlite3_stmt *pStmt = 0;
170921 int nEntry = 0;
170922
170923 /* Compose and prepare an SQL statement to loop through the content table */
170924 char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
170925 if( !zSql ){
170926 rc = SQLITE_NOMEM;
170927 }else{
170928 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
170929 sqlite3_free(zSql);
170930 }
170931
170932 if( rc==SQLITE_OK ){
170933 int nByte = sizeof(u32) * (p->nColumn+1)*3;
170934 aSz = (u32 *)sqlite3_malloc(nByte);
170935 if( aSz==0 ){
170936 rc = SQLITE_NOMEM;
170937 }else{
170938 memset(aSz, 0, nByte);
170939 aSzIns = &aSz[p->nColumn+1];
170940 aSzDel = &aSzIns[p->nColumn+1];
170941 }
170942 }
170943
170944 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
170945 int iCol;
170946 int iLangid = langidFromSelect(p, pStmt);
170947 rc = fts3PendingTermsDocid(p, 0, iLangid, sqlite3_column_int64(pStmt, 0));
170948 memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
170949 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
170950 if( p->abNotindexed[iCol]==0 ){
170951 const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
170952 rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
170953 aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
170954 }
170955 }
170956 if( p->bHasDocsize ){
170957 fts3InsertDocsize(&rc, p, aSz);
170958 }
170959 if( rc!=SQLITE_OK ){
170960 sqlite3_finalize(pStmt);
170961 pStmt = 0;
170962 }else{
170963 nEntry++;
170964 for(iCol=0; iCol<=p->nColumn; iCol++){
170965 aSzIns[iCol] += aSz[iCol];
170966 }
170967 }
170968 }
170969 if( p->bFts4 ){
170970 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
170971 }
170972 sqlite3_free(aSz);
170973
170974 if( pStmt ){
170975 int rc2 = sqlite3_finalize(pStmt);
170976 if( rc==SQLITE_OK ){
170977 rc = rc2;
170978 }
170979 }
170980 }
170981
170982 return rc;
170983}
170984
170985
170986/*
170987** This function opens a cursor used to read the input data for an
170988** incremental merge operation. Specifically, it opens a cursor to scan
170989** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
170990** level iAbsLevel.
170991*/
170992static int fts3IncrmergeCsr(
170993 Fts3Table *p, /* FTS3 table handle */
170994 sqlite3_int64 iAbsLevel, /* Absolute level to open */
170995 int nSeg, /* Number of segments to merge */
170996 Fts3MultiSegReader *pCsr /* Cursor object to populate */
170997){
170998 int rc; /* Return Code */
170999 sqlite3_stmt *pStmt = 0; /* Statement used to read %_segdir entry */
171000 int nByte; /* Bytes allocated at pCsr->apSegment[] */
171001
171002 /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
171003 memset(pCsr, 0, sizeof(*pCsr));
171004 nByte = sizeof(Fts3SegReader *) * nSeg;
171005 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
171006
171007 if( pCsr->apSegment==0 ){
171008 rc = SQLITE_NOMEM;
171009 }else{
171010 memset(pCsr->apSegment, 0, nByte);
171011 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
171012 }
171013 if( rc==SQLITE_OK ){
171014 int i;
171015 int rc2;
171016 sqlite3_bind_int64(pStmt, 1, iAbsLevel);
171017 assert( pCsr->nSegment==0 );
171018 for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
171019 rc = sqlite3Fts3SegReaderNew(i, 0,
171020 sqlite3_column_int64(pStmt, 1), /* segdir.start_block */
171021 sqlite3_column_int64(pStmt, 2), /* segdir.leaves_end_block */
171022 sqlite3_column_int64(pStmt, 3), /* segdir.end_block */
171023 sqlite3_column_blob(pStmt, 4), /* segdir.root */
171024 sqlite3_column_bytes(pStmt, 4), /* segdir.root */
171025 &pCsr->apSegment[i]
171026 );
171027 pCsr->nSegment++;
171028 }
171029 rc2 = sqlite3_reset(pStmt);
171030 if( rc==SQLITE_OK ) rc = rc2;
171031 }
171032
171033 return rc;
171034}
171035
171036typedef struct IncrmergeWriter IncrmergeWriter;
171037typedef struct NodeWriter NodeWriter;
171038typedef struct Blob Blob;
171039typedef struct NodeReader NodeReader;
171040
171041/*
171042** An instance of the following structure is used as a dynamic buffer
171043** to build up nodes or other blobs of data in.
171044**
171045** The function blobGrowBuffer() is used to extend the allocation.
171046*/
171047struct Blob {
171048 char *a; /* Pointer to allocation */
171049 int n; /* Number of valid bytes of data in a[] */
171050 int nAlloc; /* Allocated size of a[] (nAlloc>=n) */
171051};
171052
171053/*
171054** This structure is used to build up buffers containing segment b-tree
171055** nodes (blocks).
171056*/
171057struct NodeWriter {
171058 sqlite3_int64 iBlock; /* Current block id */
171059 Blob key; /* Last key written to the current block */
171060 Blob block; /* Current block image */
171061};
171062
171063/*
171064** An object of this type contains the state required to create or append
171065** to an appendable b-tree segment.
171066*/
171067struct IncrmergeWriter {
171068 int nLeafEst; /* Space allocated for leaf blocks */
171069 int nWork; /* Number of leaf pages flushed */
171070 sqlite3_int64 iAbsLevel; /* Absolute level of input segments */
171071 int iIdx; /* Index of *output* segment in iAbsLevel+1 */
171072 sqlite3_int64 iStart; /* Block number of first allocated block */
171073 sqlite3_int64 iEnd; /* Block number of last allocated block */
171074 sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */
171075 u8 bNoLeafData; /* If true, store 0 for segment size */
171076 NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
171077};
171078
171079/*
171080** An object of the following type is used to read data from a single
171081** FTS segment node. See the following functions:
171082**
171083** nodeReaderInit()
171084** nodeReaderNext()
171085** nodeReaderRelease()
171086*/
171087struct NodeReader {
171088 const char *aNode;
171089 int nNode;
171090 int iOff; /* Current offset within aNode[] */
171091
171092 /* Output variables. Containing the current node entry. */
171093 sqlite3_int64 iChild; /* Pointer to child node */
171094 Blob term; /* Current term */
171095 const char *aDoclist; /* Pointer to doclist */
171096 int nDoclist; /* Size of doclist in bytes */
171097};
171098
171099/*
171100** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
171101** Otherwise, if the allocation at pBlob->a is not already at least nMin
171102** bytes in size, extend (realloc) it to be so.
171103**
171104** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
171105** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
171106** to reflect the new size of the pBlob->a[] buffer.
171107*/
171108static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
171109 if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
171110 int nAlloc = nMin;
171111 char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
171112 if( a ){
171113 pBlob->nAlloc = nAlloc;
171114 pBlob->a = a;
171115 }else{
171116 *pRc = SQLITE_NOMEM;
171117 }
171118 }
171119}
171120
171121/*
171122** Attempt to advance the node-reader object passed as the first argument to
171123** the next entry on the node.
171124**
171125** Return an error code if an error occurs (SQLITE_NOMEM is possible).
171126** Otherwise return SQLITE_OK. If there is no next entry on the node
171127** (e.g. because the current entry is the last) set NodeReader->aNode to
171128** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
171129** variables for the new entry.
171130*/
171131static int nodeReaderNext(NodeReader *p){
171132 int bFirst = (p->term.n==0); /* True for first term on the node */
171133 int nPrefix = 0; /* Bytes to copy from previous term */
171134 int nSuffix = 0; /* Bytes to append to the prefix */
171135 int rc = SQLITE_OK; /* Return code */
171136
171137 assert( p->aNode );
171138 if( p->iChild && bFirst==0 ) p->iChild++;
171139 if( p->iOff>=p->nNode ){
171140 /* EOF */
171141 p->aNode = 0;
171142 }else{
171143 if( bFirst==0 ){
171144 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
171145 }
171146 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
171147
171148 blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
171149 if( rc==SQLITE_OK ){
171150 memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
171151 p->term.n = nPrefix+nSuffix;
171152 p->iOff += nSuffix;
171153 if( p->iChild==0 ){
171154 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
171155 p->aDoclist = &p->aNode[p->iOff];
171156 p->iOff += p->nDoclist;
171157 }
171158 }
171159 }
171160
171161 assert( p->iOff<=p->nNode );
171162
171163 return rc;
171164}
171165
171166/*
171167** Release all dynamic resources held by node-reader object *p.
171168*/
171169static void nodeReaderRelease(NodeReader *p){
171170 sqlite3_free(p->term.a);
171171}
171172
171173/*
171174** Initialize a node-reader object to read the node in buffer aNode/nNode.
171175**
171176** If successful, SQLITE_OK is returned and the NodeReader object set to
171177** point to the first entry on the node (if any). Otherwise, an SQLite
171178** error code is returned.
171179*/
171180static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
171181 memset(p, 0, sizeof(NodeReader));
171182 p->aNode = aNode;
171183 p->nNode = nNode;
171184
171185 /* Figure out if this is a leaf or an internal node. */
171186 if( p->aNode[0] ){
171187 /* An internal node. */
171188 p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
171189 }else{
171190 p->iOff = 1;
171191 }
171192
171193 return nodeReaderNext(p);
171194}
171195
171196/*
171197** This function is called while writing an FTS segment each time a leaf o
171198** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
171199** to be greater than the largest key on the node just written, but smaller
171200** than or equal to the first key that will be written to the next leaf
171201** node.
171202**
171203** The block id of the leaf node just written to disk may be found in
171204** (pWriter->aNodeWriter[0].iBlock) when this function is called.
171205*/
171206static int fts3IncrmergePush(
171207 Fts3Table *p, /* Fts3 table handle */
171208 IncrmergeWriter *pWriter, /* Writer object */
171209 const char *zTerm, /* Term to write to internal node */
171210 int nTerm /* Bytes at zTerm */
171211){
171212 sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
171213 int iLayer;
171214
171215 assert( nTerm>0 );
171216 for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
171217 sqlite3_int64 iNextPtr = 0;
171218 NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
171219 int rc = SQLITE_OK;
171220 int nPrefix;
171221 int nSuffix;
171222 int nSpace;
171223
171224 /* Figure out how much space the key will consume if it is written to
171225 ** the current node of layer iLayer. Due to the prefix compression,
171226 ** the space required changes depending on which node the key is to
171227 ** be added to. */
171228 nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
171229 nSuffix = nTerm - nPrefix;
171230 nSpace = sqlite3Fts3VarintLen(nPrefix);
171231 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
171232
171233 if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
171234 /* If the current node of layer iLayer contains zero keys, or if adding
171235 ** the key to it will not cause it to grow to larger than nNodeSize
171236 ** bytes in size, write the key here. */
171237
171238 Blob *pBlk = &pNode->block;
171239 if( pBlk->n==0 ){
171240 blobGrowBuffer(pBlk, p->nNodeSize, &rc);
171241 if( rc==SQLITE_OK ){
171242 pBlk->a[0] = (char)iLayer;
171243 pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
171244 }
171245 }
171246 blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
171247 blobGrowBuffer(&pNode->key, nTerm, &rc);
171248
171249 if( rc==SQLITE_OK ){
171250 if( pNode->key.n ){
171251 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
171252 }
171253 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
171254 memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
171255 pBlk->n += nSuffix;
171256
171257 memcpy(pNode->key.a, zTerm, nTerm);
171258 pNode->key.n = nTerm;
171259 }
171260 }else{
171261 /* Otherwise, flush the current node of layer iLayer to disk.
171262 ** Then allocate a new, empty sibling node. The key will be written
171263 ** into the parent of this node. */
171264 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
171265
171266 assert( pNode->block.nAlloc>=p->nNodeSize );
171267 pNode->block.a[0] = (char)iLayer;
171268 pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
171269
171270 iNextPtr = pNode->iBlock;
171271 pNode->iBlock++;
171272 pNode->key.n = 0;
171273 }
171274
171275 if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
171276 iPtr = iNextPtr;
171277 }
171278
171279 assert( 0 );
171280 return 0;
171281}
171282
171283/*
171284** Append a term and (optionally) doclist to the FTS segment node currently
171285** stored in blob *pNode. The node need not contain any terms, but the
171286** header must be written before this function is called.
171287**
171288** A node header is a single 0x00 byte for a leaf node, or a height varint
171289** followed by the left-hand-child varint for an internal node.
171290**
171291** The term to be appended is passed via arguments zTerm/nTerm. For a
171292** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
171293** node, both aDoclist and nDoclist must be passed 0.
171294**
171295** If the size of the value in blob pPrev is zero, then this is the first
171296** term written to the node. Otherwise, pPrev contains a copy of the
171297** previous term. Before this function returns, it is updated to contain a
171298** copy of zTerm/nTerm.
171299**
171300** It is assumed that the buffer associated with pNode is already large
171301** enough to accommodate the new entry. The buffer associated with pPrev
171302** is extended by this function if requrired.
171303**
171304** If an error (i.e. OOM condition) occurs, an SQLite error code is
171305** returned. Otherwise, SQLITE_OK.
171306*/
171307static int fts3AppendToNode(
171308 Blob *pNode, /* Current node image to append to */
171309 Blob *pPrev, /* Buffer containing previous term written */
171310 const char *zTerm, /* New term to write */
171311 int nTerm, /* Size of zTerm in bytes */
171312 const char *aDoclist, /* Doclist (or NULL) to write */
171313 int nDoclist /* Size of aDoclist in bytes */
171314){
171315 int rc = SQLITE_OK; /* Return code */
171316 int bFirst = (pPrev->n==0); /* True if this is the first term written */
171317 int nPrefix; /* Size of term prefix in bytes */
171318 int nSuffix; /* Size of term suffix in bytes */
171319
171320 /* Node must have already been started. There must be a doclist for a
171321 ** leaf node, and there must not be a doclist for an internal node. */
171322 assert( pNode->n>0 );
171323 assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
171324
171325 blobGrowBuffer(pPrev, nTerm, &rc);
171326 if( rc!=SQLITE_OK ) return rc;
171327
171328 nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
171329 nSuffix = nTerm - nPrefix;
171330 memcpy(pPrev->a, zTerm, nTerm);
171331 pPrev->n = nTerm;
171332
171333 if( bFirst==0 ){
171334 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
171335 }
171336 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
171337 memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
171338 pNode->n += nSuffix;
171339
171340 if( aDoclist ){
171341 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
171342 memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
171343 pNode->n += nDoclist;
171344 }
171345
171346 assert( pNode->n<=pNode->nAlloc );
171347
171348 return SQLITE_OK;
171349}
171350
171351/*
171352** Append the current term and doclist pointed to by cursor pCsr to the
171353** appendable b-tree segment opened for writing by pWriter.
171354**
171355** Return SQLITE_OK if successful, or an SQLite error code otherwise.
171356*/
171357static int fts3IncrmergeAppend(
171358 Fts3Table *p, /* Fts3 table handle */
171359 IncrmergeWriter *pWriter, /* Writer object */
171360 Fts3MultiSegReader *pCsr /* Cursor containing term and doclist */
171361){
171362 const char *zTerm = pCsr->zTerm;
171363 int nTerm = pCsr->nTerm;
171364 const char *aDoclist = pCsr->aDoclist;
171365 int nDoclist = pCsr->nDoclist;
171366 int rc = SQLITE_OK; /* Return code */
171367 int nSpace; /* Total space in bytes required on leaf */
171368 int nPrefix; /* Size of prefix shared with previous term */
171369 int nSuffix; /* Size of suffix (nTerm - nPrefix) */
171370 NodeWriter *pLeaf; /* Object used to write leaf nodes */
171371
171372 pLeaf = &pWriter->aNodeWriter[0];
171373 nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
171374 nSuffix = nTerm - nPrefix;
171375
171376 nSpace = sqlite3Fts3VarintLen(nPrefix);
171377 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
171378 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
171379
171380 /* If the current block is not empty, and if adding this term/doclist
171381 ** to the current block would make it larger than Fts3Table.nNodeSize
171382 ** bytes, write this block out to the database. */
171383 if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
171384 rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
171385 pWriter->nWork++;
171386
171387 /* Add the current term to the parent node. The term added to the
171388 ** parent must:
171389 **
171390 ** a) be greater than the largest term on the leaf node just written
171391 ** to the database (still available in pLeaf->key), and
171392 **
171393 ** b) be less than or equal to the term about to be added to the new
171394 ** leaf node (zTerm/nTerm).
171395 **
171396 ** In other words, it must be the prefix of zTerm 1 byte longer than
171397 ** the common prefix (if any) of zTerm and pWriter->zTerm.
171398 */
171399 if( rc==SQLITE_OK ){
171400 rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
171401 }
171402
171403 /* Advance to the next output block */
171404 pLeaf->iBlock++;
171405 pLeaf->key.n = 0;
171406 pLeaf->block.n = 0;
171407
171408 nSuffix = nTerm;
171409 nSpace = 1;
171410 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
171411 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
171412 }
171413
171414 pWriter->nLeafData += nSpace;
171415 blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
171416 if( rc==SQLITE_OK ){
171417 if( pLeaf->block.n==0 ){
171418 pLeaf->block.n = 1;
171419 pLeaf->block.a[0] = '\0';
171420 }
171421 rc = fts3AppendToNode(
171422 &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
171423 );
171424 }
171425
171426 return rc;
171427}
171428
171429/*
171430** This function is called to release all dynamic resources held by the
171431** merge-writer object pWriter, and if no error has occurred, to flush
171432** all outstanding node buffers held by pWriter to disk.
171433**
171434** If *pRc is not SQLITE_OK when this function is called, then no attempt
171435** is made to write any data to disk. Instead, this function serves only
171436** to release outstanding resources.
171437**
171438** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
171439** flushing buffers to disk, *pRc is set to an SQLite error code before
171440** returning.
171441*/
171442static void fts3IncrmergeRelease(
171443 Fts3Table *p, /* FTS3 table handle */
171444 IncrmergeWriter *pWriter, /* Merge-writer object */
171445 int *pRc /* IN/OUT: Error code */
171446){
171447 int i; /* Used to iterate through non-root layers */
171448 int iRoot; /* Index of root in pWriter->aNodeWriter */
171449 NodeWriter *pRoot; /* NodeWriter for root node */
171450 int rc = *pRc; /* Error code */
171451
171452 /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
171453 ** root node. If the segment fits entirely on a single leaf node, iRoot
171454 ** will be set to 0. If the root node is the parent of the leaves, iRoot
171455 ** will be 1. And so on. */
171456 for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
171457 NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
171458 if( pNode->block.n>0 ) break;
171459 assert( *pRc || pNode->block.nAlloc==0 );
171460 assert( *pRc || pNode->key.nAlloc==0 );
171461 sqlite3_free(pNode->block.a);
171462 sqlite3_free(pNode->key.a);
171463 }
171464
171465 /* Empty output segment. This is a no-op. */
171466 if( iRoot<0 ) return;
171467
171468 /* The entire output segment fits on a single node. Normally, this means
171469 ** the node would be stored as a blob in the "root" column of the %_segdir
171470 ** table. However, this is not permitted in this case. The problem is that
171471 ** space has already been reserved in the %_segments table, and so the
171472 ** start_block and end_block fields of the %_segdir table must be populated.
171473 ** And, by design or by accident, released versions of FTS cannot handle
171474 ** segments that fit entirely on the root node with start_block!=0.
171475 **
171476 ** Instead, create a synthetic root node that contains nothing but a
171477 ** pointer to the single content node. So that the segment consists of a
171478 ** single leaf and a single interior (root) node.
171479 **
171480 ** Todo: Better might be to defer allocating space in the %_segments
171481 ** table until we are sure it is needed.
171482 */
171483 if( iRoot==0 ){
171484 Blob *pBlock = &pWriter->aNodeWriter[1].block;
171485 blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
171486 if( rc==SQLITE_OK ){
171487 pBlock->a[0] = 0x01;
171488 pBlock->n = 1 + sqlite3Fts3PutVarint(
171489 &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
171490 );
171491 }
171492 iRoot = 1;
171493 }
171494 pRoot = &pWriter->aNodeWriter[iRoot];
171495
171496 /* Flush all currently outstanding nodes to disk. */
171497 for(i=0; i<iRoot; i++){
171498 NodeWriter *pNode = &pWriter->aNodeWriter[i];
171499 if( pNode->block.n>0 && rc==SQLITE_OK ){
171500 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
171501 }
171502 sqlite3_free(pNode->block.a);
171503 sqlite3_free(pNode->key.a);
171504 }
171505
171506 /* Write the %_segdir record. */
171507 if( rc==SQLITE_OK ){
171508 rc = fts3WriteSegdir(p,
171509 pWriter->iAbsLevel+1, /* level */
171510 pWriter->iIdx, /* idx */
171511 pWriter->iStart, /* start_block */
171512 pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
171513 pWriter->iEnd, /* end_block */
171514 (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
171515 pRoot->block.a, pRoot->block.n /* root */
171516 );
171517 }
171518 sqlite3_free(pRoot->block.a);
171519 sqlite3_free(pRoot->key.a);
171520
171521 *pRc = rc;
171522}
171523
171524/*
171525** Compare the term in buffer zLhs (size in bytes nLhs) with that in
171526** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
171527** the other, it is considered to be smaller than the other.
171528**
171529** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
171530** if it is greater.
171531*/
171532static int fts3TermCmp(
171533 const char *zLhs, int nLhs, /* LHS of comparison */
171534 const char *zRhs, int nRhs /* RHS of comparison */
171535){
171536 int nCmp = MIN(nLhs, nRhs);
171537 int res;
171538
171539 res = memcmp(zLhs, zRhs, nCmp);
171540 if( res==0 ) res = nLhs - nRhs;
171541
171542 return res;
171543}
171544
171545
171546/*
171547** Query to see if the entry in the %_segments table with blockid iEnd is
171548** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
171549** returning. Otherwise, set *pbRes to 0.
171550**
171551** Or, if an error occurs while querying the database, return an SQLite
171552** error code. The final value of *pbRes is undefined in this case.
171553**
171554** This is used to test if a segment is an "appendable" segment. If it
171555** is, then a NULL entry has been inserted into the %_segments table
171556** with blockid %_segdir.end_block.
171557*/
171558static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
171559 int bRes = 0; /* Result to set *pbRes to */
171560 sqlite3_stmt *pCheck = 0; /* Statement to query database with */
171561 int rc; /* Return code */
171562
171563 rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
171564 if( rc==SQLITE_OK ){
171565 sqlite3_bind_int64(pCheck, 1, iEnd);
171566 if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
171567 rc = sqlite3_reset(pCheck);
171568 }
171569
171570 *pbRes = bRes;
171571 return rc;
171572}
171573
171574/*
171575** This function is called when initializing an incremental-merge operation.
171576** It checks if the existing segment with index value iIdx at absolute level
171577** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
171578** merge-writer object *pWriter is initialized to write to it.
171579**
171580** An existing segment can be appended to by an incremental merge if:
171581**
171582** * It was initially created as an appendable segment (with all required
171583** space pre-allocated), and
171584**
171585** * The first key read from the input (arguments zKey and nKey) is
171586** greater than the largest key currently stored in the potential
171587** output segment.
171588*/
171589static int fts3IncrmergeLoad(
171590 Fts3Table *p, /* Fts3 table handle */
171591 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
171592 int iIdx, /* Index of candidate output segment */
171593 const char *zKey, /* First key to write */
171594 int nKey, /* Number of bytes in nKey */
171595 IncrmergeWriter *pWriter /* Populate this object */
171596){
171597 int rc; /* Return code */
171598 sqlite3_stmt *pSelect = 0; /* SELECT to read %_segdir entry */
171599
171600 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
171601 if( rc==SQLITE_OK ){
171602 sqlite3_int64 iStart = 0; /* Value of %_segdir.start_block */
171603 sqlite3_int64 iLeafEnd = 0; /* Value of %_segdir.leaves_end_block */
171604 sqlite3_int64 iEnd = 0; /* Value of %_segdir.end_block */
171605 const char *aRoot = 0; /* Pointer to %_segdir.root buffer */
171606 int nRoot = 0; /* Size of aRoot[] in bytes */
171607 int rc2; /* Return code from sqlite3_reset() */
171608 int bAppendable = 0; /* Set to true if segment is appendable */
171609
171610 /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
171611 sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
171612 sqlite3_bind_int(pSelect, 2, iIdx);
171613 if( sqlite3_step(pSelect)==SQLITE_ROW ){
171614 iStart = sqlite3_column_int64(pSelect, 1);
171615 iLeafEnd = sqlite3_column_int64(pSelect, 2);
171616 fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
171617 if( pWriter->nLeafData<0 ){
171618 pWriter->nLeafData = pWriter->nLeafData * -1;
171619 }
171620 pWriter->bNoLeafData = (pWriter->nLeafData==0);
171621 nRoot = sqlite3_column_bytes(pSelect, 4);
171622 aRoot = sqlite3_column_blob(pSelect, 4);
171623 }else{
171624 return sqlite3_reset(pSelect);
171625 }
171626
171627 /* Check for the zero-length marker in the %_segments table */
171628 rc = fts3IsAppendable(p, iEnd, &bAppendable);
171629
171630 /* Check that zKey/nKey is larger than the largest key the candidate */
171631 if( rc==SQLITE_OK && bAppendable ){
171632 char *aLeaf = 0;
171633 int nLeaf = 0;
171634
171635 rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
171636 if( rc==SQLITE_OK ){
171637 NodeReader reader;
171638 for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
171639 rc==SQLITE_OK && reader.aNode;
171640 rc = nodeReaderNext(&reader)
171641 ){
171642 assert( reader.aNode );
171643 }
171644 if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
171645 bAppendable = 0;
171646 }
171647 nodeReaderRelease(&reader);
171648 }
171649 sqlite3_free(aLeaf);
171650 }
171651
171652 if( rc==SQLITE_OK && bAppendable ){
171653 /* It is possible to append to this segment. Set up the IncrmergeWriter
171654 ** object to do so. */
171655 int i;
171656 int nHeight = (int)aRoot[0];
171657 NodeWriter *pNode;
171658
171659 pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
171660 pWriter->iStart = iStart;
171661 pWriter->iEnd = iEnd;
171662 pWriter->iAbsLevel = iAbsLevel;
171663 pWriter->iIdx = iIdx;
171664
171665 for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
171666 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
171667 }
171668
171669 pNode = &pWriter->aNodeWriter[nHeight];
171670 pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
171671 blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
171672 if( rc==SQLITE_OK ){
171673 memcpy(pNode->block.a, aRoot, nRoot);
171674 pNode->block.n = nRoot;
171675 }
171676
171677 for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
171678 NodeReader reader;
171679 pNode = &pWriter->aNodeWriter[i];
171680
171681 rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
171682 while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
171683 blobGrowBuffer(&pNode->key, reader.term.n, &rc);
171684 if( rc==SQLITE_OK ){
171685 memcpy(pNode->key.a, reader.term.a, reader.term.n);
171686 pNode->key.n = reader.term.n;
171687 if( i>0 ){
171688 char *aBlock = 0;
171689 int nBlock = 0;
171690 pNode = &pWriter->aNodeWriter[i-1];
171691 pNode->iBlock = reader.iChild;
171692 rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
171693 blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
171694 if( rc==SQLITE_OK ){
171695 memcpy(pNode->block.a, aBlock, nBlock);
171696 pNode->block.n = nBlock;
171697 }
171698 sqlite3_free(aBlock);
171699 }
171700 }
171701 nodeReaderRelease(&reader);
171702 }
171703 }
171704
171705 rc2 = sqlite3_reset(pSelect);
171706 if( rc==SQLITE_OK ) rc = rc2;
171707 }
171708
171709 return rc;
171710}
171711
171712/*
171713** Determine the largest segment index value that exists within absolute
171714** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
171715** one before returning SQLITE_OK. Or, if there are no segments at all
171716** within level iAbsLevel, set *piIdx to zero.
171717**
171718** If an error occurs, return an SQLite error code. The final value of
171719** *piIdx is undefined in this case.
171720*/
171721static int fts3IncrmergeOutputIdx(
171722 Fts3Table *p, /* FTS Table handle */
171723 sqlite3_int64 iAbsLevel, /* Absolute index of input segments */
171724 int *piIdx /* OUT: Next free index at iAbsLevel+1 */
171725){
171726 int rc;
171727 sqlite3_stmt *pOutputIdx = 0; /* SQL used to find output index */
171728
171729 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
171730 if( rc==SQLITE_OK ){
171731 sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
171732 sqlite3_step(pOutputIdx);
171733 *piIdx = sqlite3_column_int(pOutputIdx, 0);
171734 rc = sqlite3_reset(pOutputIdx);
171735 }
171736
171737 return rc;
171738}
171739
171740/*
171741** Allocate an appendable output segment on absolute level iAbsLevel+1
171742** with idx value iIdx.
171743**
171744** In the %_segdir table, a segment is defined by the values in three
171745** columns:
171746**
171747** start_block
171748** leaves_end_block
171749** end_block
171750**
171751** When an appendable segment is allocated, it is estimated that the
171752** maximum number of leaf blocks that may be required is the sum of the
171753** number of leaf blocks consumed by the input segments, plus the number
171754** of input segments, multiplied by two. This value is stored in stack
171755** variable nLeafEst.
171756**
171757** A total of 16*nLeafEst blocks are allocated when an appendable segment
171758** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
171759** array of leaf nodes starts at the first block allocated. The array
171760** of interior nodes that are parents of the leaf nodes start at block
171761** (start_block + (1 + end_block - start_block) / 16). And so on.
171762**
171763** In the actual code below, the value "16" is replaced with the
171764** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
171765*/
171766static int fts3IncrmergeWriter(
171767 Fts3Table *p, /* Fts3 table handle */
171768 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
171769 int iIdx, /* Index of new output segment */
171770 Fts3MultiSegReader *pCsr, /* Cursor that data will be read from */
171771 IncrmergeWriter *pWriter /* Populate this object */
171772){
171773 int rc; /* Return Code */
171774 int i; /* Iterator variable */
171775 int nLeafEst = 0; /* Blocks allocated for leaf nodes */
171776 sqlite3_stmt *pLeafEst = 0; /* SQL used to determine nLeafEst */
171777 sqlite3_stmt *pFirstBlock = 0; /* SQL used to determine first block */
171778
171779 /* Calculate nLeafEst. */
171780 rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
171781 if( rc==SQLITE_OK ){
171782 sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
171783 sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
171784 if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
171785 nLeafEst = sqlite3_column_int(pLeafEst, 0);
171786 }
171787 rc = sqlite3_reset(pLeafEst);
171788 }
171789 if( rc!=SQLITE_OK ) return rc;
171790
171791 /* Calculate the first block to use in the output segment */
171792 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
171793 if( rc==SQLITE_OK ){
171794 if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
171795 pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
171796 pWriter->iEnd = pWriter->iStart - 1;
171797 pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
171798 }
171799 rc = sqlite3_reset(pFirstBlock);
171800 }
171801 if( rc!=SQLITE_OK ) return rc;
171802
171803 /* Insert the marker in the %_segments table to make sure nobody tries
171804 ** to steal the space just allocated. This is also used to identify
171805 ** appendable segments. */
171806 rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
171807 if( rc!=SQLITE_OK ) return rc;
171808
171809 pWriter->iAbsLevel = iAbsLevel;
171810 pWriter->nLeafEst = nLeafEst;
171811 pWriter->iIdx = iIdx;
171812
171813 /* Set up the array of NodeWriter objects */
171814 for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
171815 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
171816 }
171817 return SQLITE_OK;
171818}
171819
171820/*
171821** Remove an entry from the %_segdir table. This involves running the
171822** following two statements:
171823**
171824** DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
171825** UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
171826**
171827** The DELETE statement removes the specific %_segdir level. The UPDATE
171828** statement ensures that the remaining segments have contiguously allocated
171829** idx values.
171830*/
171831static int fts3RemoveSegdirEntry(
171832 Fts3Table *p, /* FTS3 table handle */
171833 sqlite3_int64 iAbsLevel, /* Absolute level to delete from */
171834 int iIdx /* Index of %_segdir entry to delete */
171835){
171836 int rc; /* Return code */
171837 sqlite3_stmt *pDelete = 0; /* DELETE statement */
171838
171839 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
171840 if( rc==SQLITE_OK ){
171841 sqlite3_bind_int64(pDelete, 1, iAbsLevel);
171842 sqlite3_bind_int(pDelete, 2, iIdx);
171843 sqlite3_step(pDelete);
171844 rc = sqlite3_reset(pDelete);
171845 }
171846
171847 return rc;
171848}
171849
171850/*
171851** One or more segments have just been removed from absolute level iAbsLevel.
171852** Update the 'idx' values of the remaining segments in the level so that
171853** the idx values are a contiguous sequence starting from 0.
171854*/
171855static int fts3RepackSegdirLevel(
171856 Fts3Table *p, /* FTS3 table handle */
171857 sqlite3_int64 iAbsLevel /* Absolute level to repack */
171858){
171859 int rc; /* Return code */
171860 int *aIdx = 0; /* Array of remaining idx values */
171861 int nIdx = 0; /* Valid entries in aIdx[] */
171862 int nAlloc = 0; /* Allocated size of aIdx[] */
171863 int i; /* Iterator variable */
171864 sqlite3_stmt *pSelect = 0; /* Select statement to read idx values */
171865 sqlite3_stmt *pUpdate = 0; /* Update statement to modify idx values */
171866
171867 rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
171868 if( rc==SQLITE_OK ){
171869 int rc2;
171870 sqlite3_bind_int64(pSelect, 1, iAbsLevel);
171871 while( SQLITE_ROW==sqlite3_step(pSelect) ){
171872 if( nIdx>=nAlloc ){
171873 int *aNew;
171874 nAlloc += 16;
171875 aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
171876 if( !aNew ){
171877 rc = SQLITE_NOMEM;
171878 break;
171879 }
171880 aIdx = aNew;
171881 }
171882 aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
171883 }
171884 rc2 = sqlite3_reset(pSelect);
171885 if( rc==SQLITE_OK ) rc = rc2;
171886 }
171887
171888 if( rc==SQLITE_OK ){
171889 rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
171890 }
171891 if( rc==SQLITE_OK ){
171892 sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
171893 }
171894
171895 assert( p->bIgnoreSavepoint==0 );
171896 p->bIgnoreSavepoint = 1;
171897 for(i=0; rc==SQLITE_OK && i<nIdx; i++){
171898 if( aIdx[i]!=i ){
171899 sqlite3_bind_int(pUpdate, 3, aIdx[i]);
171900 sqlite3_bind_int(pUpdate, 1, i);
171901 sqlite3_step(pUpdate);
171902 rc = sqlite3_reset(pUpdate);
171903 }
171904 }
171905 p->bIgnoreSavepoint = 0;
171906
171907 sqlite3_free(aIdx);
171908 return rc;
171909}
171910
171911static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
171912 pNode->a[0] = (char)iHeight;
171913 if( iChild ){
171914 assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
171915 pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
171916 }else{
171917 assert( pNode->nAlloc>=1 );
171918 pNode->n = 1;
171919 }
171920}
171921
171922/*
171923** The first two arguments are a pointer to and the size of a segment b-tree
171924** node. The node may be a leaf or an internal node.
171925**
171926** This function creates a new node image in blob object *pNew by copying
171927** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
171928** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
171929*/
171930static int fts3TruncateNode(
171931 const char *aNode, /* Current node image */
171932 int nNode, /* Size of aNode in bytes */
171933 Blob *pNew, /* OUT: Write new node image here */
171934 const char *zTerm, /* Omit all terms smaller than this */
171935 int nTerm, /* Size of zTerm in bytes */
171936 sqlite3_int64 *piBlock /* OUT: Block number in next layer down */
171937){
171938 NodeReader reader; /* Reader object */
171939 Blob prev = {0, 0, 0}; /* Previous term written to new node */
171940 int rc = SQLITE_OK; /* Return code */
171941 int bLeaf = aNode[0]=='\0'; /* True for a leaf node */
171942
171943 /* Allocate required output space */
171944 blobGrowBuffer(pNew, nNode, &rc);
171945 if( rc!=SQLITE_OK ) return rc;
171946 pNew->n = 0;
171947
171948 /* Populate new node buffer */
171949 for(rc = nodeReaderInit(&reader, aNode, nNode);
171950 rc==SQLITE_OK && reader.aNode;
171951 rc = nodeReaderNext(&reader)
171952 ){
171953 if( pNew->n==0 ){
171954 int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
171955 if( res<0 || (bLeaf==0 && res==0) ) continue;
171956 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
171957 *piBlock = reader.iChild;
171958 }
171959 rc = fts3AppendToNode(
171960 pNew, &prev, reader.term.a, reader.term.n,
171961 reader.aDoclist, reader.nDoclist
171962 );
171963 if( rc!=SQLITE_OK ) break;
171964 }
171965 if( pNew->n==0 ){
171966 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
171967 *piBlock = reader.iChild;
171968 }
171969 assert( pNew->n<=pNew->nAlloc );
171970
171971 nodeReaderRelease(&reader);
171972 sqlite3_free(prev.a);
171973 return rc;
171974}
171975
171976/*
171977** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
171978** level iAbsLevel. This may involve deleting entries from the %_segments
171979** table, and modifying existing entries in both the %_segments and %_segdir
171980** tables.
171981**
171982** SQLITE_OK is returned if the segment is updated successfully. Or an
171983** SQLite error code otherwise.
171984*/
171985static int fts3TruncateSegment(
171986 Fts3Table *p, /* FTS3 table handle */
171987 sqlite3_int64 iAbsLevel, /* Absolute level of segment to modify */
171988 int iIdx, /* Index within level of segment to modify */
171989 const char *zTerm, /* Remove terms smaller than this */
171990 int nTerm /* Number of bytes in buffer zTerm */
171991){
171992 int rc = SQLITE_OK; /* Return code */
171993 Blob root = {0,0,0}; /* New root page image */
171994 Blob block = {0,0,0}; /* Buffer used for any other block */
171995 sqlite3_int64 iBlock = 0; /* Block id */
171996 sqlite3_int64 iNewStart = 0; /* New value for iStartBlock */
171997 sqlite3_int64 iOldStart = 0; /* Old value for iStartBlock */
171998 sqlite3_stmt *pFetch = 0; /* Statement used to fetch segdir */
171999
172000 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
172001 if( rc==SQLITE_OK ){
172002 int rc2; /* sqlite3_reset() return code */
172003 sqlite3_bind_int64(pFetch, 1, iAbsLevel);
172004 sqlite3_bind_int(pFetch, 2, iIdx);
172005 if( SQLITE_ROW==sqlite3_step(pFetch) ){
172006 const char *aRoot = sqlite3_column_blob(pFetch, 4);
172007 int nRoot = sqlite3_column_bytes(pFetch, 4);
172008 iOldStart = sqlite3_column_int64(pFetch, 1);
172009 rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
172010 }
172011 rc2 = sqlite3_reset(pFetch);
172012 if( rc==SQLITE_OK ) rc = rc2;
172013 }
172014
172015 while( rc==SQLITE_OK && iBlock ){
172016 char *aBlock = 0;
172017 int nBlock = 0;
172018 iNewStart = iBlock;
172019
172020 rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
172021 if( rc==SQLITE_OK ){
172022 rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
172023 }
172024 if( rc==SQLITE_OK ){
172025 rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
172026 }
172027 sqlite3_free(aBlock);
172028 }
172029
172030 /* Variable iNewStart now contains the first valid leaf node. */
172031 if( rc==SQLITE_OK && iNewStart ){
172032 sqlite3_stmt *pDel = 0;
172033 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
172034 if( rc==SQLITE_OK ){
172035 sqlite3_bind_int64(pDel, 1, iOldStart);
172036 sqlite3_bind_int64(pDel, 2, iNewStart-1);
172037 sqlite3_step(pDel);
172038 rc = sqlite3_reset(pDel);
172039 }
172040 }
172041
172042 if( rc==SQLITE_OK ){
172043 sqlite3_stmt *pChomp = 0;
172044 rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
172045 if( rc==SQLITE_OK ){
172046 sqlite3_bind_int64(pChomp, 1, iNewStart);
172047 sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
172048 sqlite3_bind_int64(pChomp, 3, iAbsLevel);
172049 sqlite3_bind_int(pChomp, 4, iIdx);
172050 sqlite3_step(pChomp);
172051 rc = sqlite3_reset(pChomp);
172052 sqlite3_bind_null(pChomp, 2);
172053 }
172054 }
172055
172056 sqlite3_free(root.a);
172057 sqlite3_free(block.a);
172058 return rc;
172059}
172060
172061/*
172062** This function is called after an incrmental-merge operation has run to
172063** merge (or partially merge) two or more segments from absolute level
172064** iAbsLevel.
172065**
172066** Each input segment is either removed from the db completely (if all of
172067** its data was copied to the output segment by the incrmerge operation)
172068** or modified in place so that it no longer contains those entries that
172069** have been duplicated in the output segment.
172070*/
172071static int fts3IncrmergeChomp(
172072 Fts3Table *p, /* FTS table handle */
172073 sqlite3_int64 iAbsLevel, /* Absolute level containing segments */
172074 Fts3MultiSegReader *pCsr, /* Chomp all segments opened by this cursor */
172075 int *pnRem /* Number of segments not deleted */
172076){
172077 int i;
172078 int nRem = 0;
172079 int rc = SQLITE_OK;
172080
172081 for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
172082 Fts3SegReader *pSeg = 0;
172083 int j;
172084
172085 /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
172086 ** somewhere in the pCsr->apSegment[] array. */
172087 for(j=0; ALWAYS(j<pCsr->nSegment); j++){
172088 pSeg = pCsr->apSegment[j];
172089 if( pSeg->iIdx==i ) break;
172090 }
172091 assert( j<pCsr->nSegment && pSeg->iIdx==i );
172092
172093 if( pSeg->aNode==0 ){
172094 /* Seg-reader is at EOF. Remove the entire input segment. */
172095 rc = fts3DeleteSegment(p, pSeg);
172096 if( rc==SQLITE_OK ){
172097 rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
172098 }
172099 *pnRem = 0;
172100 }else{
172101 /* The incremental merge did not copy all the data from this
172102 ** segment to the upper level. The segment is modified in place
172103 ** so that it contains no keys smaller than zTerm/nTerm. */
172104 const char *zTerm = pSeg->zTerm;
172105 int nTerm = pSeg->nTerm;
172106 rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
172107 nRem++;
172108 }
172109 }
172110
172111 if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
172112 rc = fts3RepackSegdirLevel(p, iAbsLevel);
172113 }
172114
172115 *pnRem = nRem;
172116 return rc;
172117}
172118
172119/*
172120** Store an incr-merge hint in the database.
172121*/
172122static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
172123 sqlite3_stmt *pReplace = 0;
172124 int rc; /* Return code */
172125
172126 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
172127 if( rc==SQLITE_OK ){
172128 sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
172129 sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
172130 sqlite3_step(pReplace);
172131 rc = sqlite3_reset(pReplace);
172132 sqlite3_bind_null(pReplace, 2);
172133 }
172134
172135 return rc;
172136}
172137
172138/*
172139** Load an incr-merge hint from the database. The incr-merge hint, if one
172140** exists, is stored in the rowid==1 row of the %_stat table.
172141**
172142** If successful, populate blob *pHint with the value read from the %_stat
172143** table and return SQLITE_OK. Otherwise, if an error occurs, return an
172144** SQLite error code.
172145*/
172146static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
172147 sqlite3_stmt *pSelect = 0;
172148 int rc;
172149
172150 pHint->n = 0;
172151 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
172152 if( rc==SQLITE_OK ){
172153 int rc2;
172154 sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
172155 if( SQLITE_ROW==sqlite3_step(pSelect) ){
172156 const char *aHint = sqlite3_column_blob(pSelect, 0);
172157 int nHint = sqlite3_column_bytes(pSelect, 0);
172158 if( aHint ){
172159 blobGrowBuffer(pHint, nHint, &rc);
172160 if( rc==SQLITE_OK ){
172161 memcpy(pHint->a, aHint, nHint);
172162 pHint->n = nHint;
172163 }
172164 }
172165 }
172166 rc2 = sqlite3_reset(pSelect);
172167 if( rc==SQLITE_OK ) rc = rc2;
172168 }
172169
172170 return rc;
172171}
172172
172173/*
172174** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
172175** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
172176** consists of two varints, the absolute level number of the input segments
172177** and the number of input segments.
172178**
172179** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
172180** set *pRc to an SQLite error code before returning.
172181*/
172182static void fts3IncrmergeHintPush(
172183 Blob *pHint, /* Hint blob to append to */
172184 i64 iAbsLevel, /* First varint to store in hint */
172185 int nInput, /* Second varint to store in hint */
172186 int *pRc /* IN/OUT: Error code */
172187){
172188 blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
172189 if( *pRc==SQLITE_OK ){
172190 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
172191 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
172192 }
172193}
172194
172195/*
172196** Read the last entry (most recently pushed) from the hint blob *pHint
172197** and then remove the entry. Write the two values read to *piAbsLevel and
172198** *pnInput before returning.
172199**
172200** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
172201** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
172202*/
172203static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
172204 const int nHint = pHint->n;
172205 int i;
172206
172207 i = pHint->n-2;
172208 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
172209 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
172210
172211 pHint->n = i;
172212 i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
172213 i += fts3GetVarint32(&pHint->a[i], pnInput);
172214 if( i!=nHint ) return FTS_CORRUPT_VTAB;
172215
172216 return SQLITE_OK;
172217}
172218
172219
172220/*
172221** Attempt an incremental merge that writes nMerge leaf blocks.
172222**
172223** Incremental merges happen nMin segments at a time. The segments
172224** to be merged are the nMin oldest segments (the ones with the smallest
172225** values for the _segdir.idx field) in the highest level that contains
172226** at least nMin segments. Multiple merges might occur in an attempt to
172227** write the quota of nMerge leaf blocks.
172228*/
172229SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
172230 int rc; /* Return code */
172231 int nRem = nMerge; /* Number of leaf pages yet to be written */
172232 Fts3MultiSegReader *pCsr; /* Cursor used to read input data */
172233 Fts3SegFilter *pFilter; /* Filter used with cursor pCsr */
172234 IncrmergeWriter *pWriter; /* Writer object */
172235 int nSeg = 0; /* Number of input segments */
172236 sqlite3_int64 iAbsLevel = 0; /* Absolute level number to work on */
172237 Blob hint = {0, 0, 0}; /* Hint read from %_stat table */
172238 int bDirtyHint = 0; /* True if blob 'hint' has been modified */
172239
172240 /* Allocate space for the cursor, filter and writer objects */
172241 const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
172242 pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
172243 if( !pWriter ) return SQLITE_NOMEM;
172244 pFilter = (Fts3SegFilter *)&pWriter[1];
172245 pCsr = (Fts3MultiSegReader *)&pFilter[1];
172246
172247 rc = fts3IncrmergeHintLoad(p, &hint);
172248 while( rc==SQLITE_OK && nRem>0 ){
172249 const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
172250 sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
172251 int bUseHint = 0; /* True if attempting to append */
172252 int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
172253
172254 /* Search the %_segdir table for the absolute level with the smallest
172255 ** relative level number that contains at least nMin segments, if any.
172256 ** If one is found, set iAbsLevel to the absolute level number and
172257 ** nSeg to nMin. If no level with at least nMin segments can be found,
172258 ** set nSeg to -1.
172259 */
172260 rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
172261 sqlite3_bind_int(pFindLevel, 1, MAX(2, nMin));
172262 if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
172263 iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
172264 nSeg = sqlite3_column_int(pFindLevel, 1);
172265 assert( nSeg>=2 );
172266 }else{
172267 nSeg = -1;
172268 }
172269 rc = sqlite3_reset(pFindLevel);
172270
172271 /* If the hint read from the %_stat table is not empty, check if the
172272 ** last entry in it specifies a relative level smaller than or equal
172273 ** to the level identified by the block above (if any). If so, this
172274 ** iteration of the loop will work on merging at the hinted level.
172275 */
172276 if( rc==SQLITE_OK && hint.n ){
172277 int nHint = hint.n;
172278 sqlite3_int64 iHintAbsLevel = 0; /* Hint level */
172279 int nHintSeg = 0; /* Hint number of segments */
172280
172281 rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
172282 if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
172283 iAbsLevel = iHintAbsLevel;
172284 nSeg = nHintSeg;
172285 bUseHint = 1;
172286 bDirtyHint = 1;
172287 }else{
172288 /* This undoes the effect of the HintPop() above - so that no entry
172289 ** is removed from the hint blob. */
172290 hint.n = nHint;
172291 }
172292 }
172293
172294 /* If nSeg is less that zero, then there is no level with at least
172295 ** nMin segments and no hint in the %_stat table. No work to do.
172296 ** Exit early in this case. */
172297 if( nSeg<0 ) break;
172298
172299 /* Open a cursor to iterate through the contents of the oldest nSeg
172300 ** indexes of absolute level iAbsLevel. If this cursor is opened using
172301 ** the 'hint' parameters, it is possible that there are less than nSeg
172302 ** segments available in level iAbsLevel. In this case, no work is
172303 ** done on iAbsLevel - fall through to the next iteration of the loop
172304 ** to start work on some other level. */
172305 memset(pWriter, 0, nAlloc);
172306 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
172307
172308 if( rc==SQLITE_OK ){
172309 rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
172310 assert( bUseHint==1 || bUseHint==0 );
172311 if( iIdx==0 || (bUseHint && iIdx==1) ){
172312 int bIgnore = 0;
172313 rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
172314 if( bIgnore ){
172315 pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
172316 }
172317 }
172318 }
172319
172320 if( rc==SQLITE_OK ){
172321 rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
172322 }
172323 if( SQLITE_OK==rc && pCsr->nSegment==nSeg
172324 && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
172325 && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
172326 ){
172327 if( bUseHint && iIdx>0 ){
172328 const char *zKey = pCsr->zTerm;
172329 int nKey = pCsr->nTerm;
172330 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
172331 }else{
172332 rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
172333 }
172334
172335 if( rc==SQLITE_OK && pWriter->nLeafEst ){
172336 fts3LogMerge(nSeg, iAbsLevel);
172337 do {
172338 rc = fts3IncrmergeAppend(p, pWriter, pCsr);
172339 if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
172340 if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
172341 }while( rc==SQLITE_ROW );
172342
172343 /* Update or delete the input segments */
172344 if( rc==SQLITE_OK ){
172345 nRem -= (1 + pWriter->nWork);
172346 rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
172347 if( nSeg!=0 ){
172348 bDirtyHint = 1;
172349 fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
172350 }
172351 }
172352 }
172353
172354 if( nSeg!=0 ){
172355 pWriter->nLeafData = pWriter->nLeafData * -1;
172356 }
172357 fts3IncrmergeRelease(p, pWriter, &rc);
172358 if( nSeg==0 && pWriter->bNoLeafData==0 ){
172359 fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
172360 }
172361 }
172362
172363 sqlite3Fts3SegReaderFinish(pCsr);
172364 }
172365
172366 /* Write the hint values into the %_stat table for the next incr-merger */
172367 if( bDirtyHint && rc==SQLITE_OK ){
172368 rc = fts3IncrmergeHintStore(p, &hint);
172369 }
172370
172371 sqlite3_free(pWriter);
172372 sqlite3_free(hint.a);
172373 return rc;
172374}
172375
172376/*
172377** Convert the text beginning at *pz into an integer and return
172378** its value. Advance *pz to point to the first character past
172379** the integer.
172380**
172381** This function used for parameters to merge= and incrmerge=
172382** commands.
172383*/
172384static int fts3Getint(const char **pz){
172385 const char *z = *pz;
172386 int i = 0;
172387 while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
172388 *pz = z;
172389 return i;
172390}
172391
172392/*
172393** Process statements of the form:
172394**
172395** INSERT INTO table(table) VALUES('merge=A,B');
172396**
172397** A and B are integers that decode to be the number of leaf pages
172398** written for the merge, and the minimum number of segments on a level
172399** before it will be selected for a merge, respectively.
172400*/
172401static int fts3DoIncrmerge(
172402 Fts3Table *p, /* FTS3 table handle */
172403 const char *zParam /* Nul-terminated string containing "A,B" */
172404){
172405 int rc;
172406 int nMin = (FTS3_MERGE_COUNT / 2);
172407 int nMerge = 0;
172408 const char *z = zParam;
172409
172410 /* Read the first integer value */
172411 nMerge = fts3Getint(&z);
172412
172413 /* If the first integer value is followed by a ',', read the second
172414 ** integer value. */
172415 if( z[0]==',' && z[1]!='\0' ){
172416 z++;
172417 nMin = fts3Getint(&z);
172418 }
172419
172420 if( z[0]!='\0' || nMin<2 ){
172421 rc = SQLITE_ERROR;
172422 }else{
172423 rc = SQLITE_OK;
172424 if( !p->bHasStat ){
172425 assert( p->bFts4==0 );
172426 sqlite3Fts3CreateStatTable(&rc, p);
172427 }
172428 if( rc==SQLITE_OK ){
172429 rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
172430 }
172431 sqlite3Fts3SegmentsClose(p);
172432 }
172433 return rc;
172434}
172435
172436/*
172437** Process statements of the form:
172438**
172439** INSERT INTO table(table) VALUES('automerge=X');
172440**
172441** where X is an integer. X==0 means to turn automerge off. X!=0 means
172442** turn it on. The setting is persistent.
172443*/
172444static int fts3DoAutoincrmerge(
172445 Fts3Table *p, /* FTS3 table handle */
172446 const char *zParam /* Nul-terminated string containing boolean */
172447){
172448 int rc = SQLITE_OK;
172449 sqlite3_stmt *pStmt = 0;
172450 p->nAutoincrmerge = fts3Getint(&zParam);
172451 if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){
172452 p->nAutoincrmerge = 8;
172453 }
172454 if( !p->bHasStat ){
172455 assert( p->bFts4==0 );
172456 sqlite3Fts3CreateStatTable(&rc, p);
172457 if( rc ) return rc;
172458 }
172459 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
172460 if( rc ) return rc;
172461 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
172462 sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
172463 sqlite3_step(pStmt);
172464 rc = sqlite3_reset(pStmt);
172465 return rc;
172466}
172467
172468/*
172469** Return a 64-bit checksum for the FTS index entry specified by the
172470** arguments to this function.
172471*/
172472static u64 fts3ChecksumEntry(
172473 const char *zTerm, /* Pointer to buffer containing term */
172474 int nTerm, /* Size of zTerm in bytes */
172475 int iLangid, /* Language id for current row */
172476 int iIndex, /* Index (0..Fts3Table.nIndex-1) */
172477 i64 iDocid, /* Docid for current row. */
172478 int iCol, /* Column number */
172479 int iPos /* Position */
172480){
172481 int i;
172482 u64 ret = (u64)iDocid;
172483
172484 ret += (ret<<3) + iLangid;
172485 ret += (ret<<3) + iIndex;
172486 ret += (ret<<3) + iCol;
172487 ret += (ret<<3) + iPos;
172488 for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
172489
172490 return ret;
172491}
172492
172493/*
172494** Return a checksum of all entries in the FTS index that correspond to
172495** language id iLangid. The checksum is calculated by XORing the checksums
172496** of each individual entry (see fts3ChecksumEntry()) together.
172497**
172498** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
172499** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
172500** return value is undefined in this case.
172501*/
172502static u64 fts3ChecksumIndex(
172503 Fts3Table *p, /* FTS3 table handle */
172504 int iLangid, /* Language id to return cksum for */
172505 int iIndex, /* Index to cksum (0..p->nIndex-1) */
172506 int *pRc /* OUT: Return code */
172507){
172508 Fts3SegFilter filter;
172509 Fts3MultiSegReader csr;
172510 int rc;
172511 u64 cksum = 0;
172512
172513 assert( *pRc==SQLITE_OK );
172514
172515 memset(&filter, 0, sizeof(filter));
172516 memset(&csr, 0, sizeof(csr));
172517 filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
172518 filter.flags |= FTS3_SEGMENT_SCAN;
172519
172520 rc = sqlite3Fts3SegReaderCursor(
172521 p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
172522 );
172523 if( rc==SQLITE_OK ){
172524 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
172525 }
172526
172527 if( rc==SQLITE_OK ){
172528 while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
172529 char *pCsr = csr.aDoclist;
172530 char *pEnd = &pCsr[csr.nDoclist];
172531
172532 i64 iDocid = 0;
172533 i64 iCol = 0;
172534 i64 iPos = 0;
172535
172536 pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
172537 while( pCsr<pEnd ){
172538 i64 iVal = 0;
172539 pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
172540 if( pCsr<pEnd ){
172541 if( iVal==0 || iVal==1 ){
172542 iCol = 0;
172543 iPos = 0;
172544 if( iVal ){
172545 pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
172546 }else{
172547 pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
172548 iDocid += iVal;
172549 }
172550 }else{
172551 iPos += (iVal - 2);
172552 cksum = cksum ^ fts3ChecksumEntry(
172553 csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
172554 (int)iCol, (int)iPos
172555 );
172556 }
172557 }
172558 }
172559 }
172560 }
172561 sqlite3Fts3SegReaderFinish(&csr);
172562
172563 *pRc = rc;
172564 return cksum;
172565}
172566
172567/*
172568** Check if the contents of the FTS index match the current contents of the
172569** content table. If no error occurs and the contents do match, set *pbOk
172570** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
172571** to false before returning.
172572**
172573** If an error occurs (e.g. an OOM or IO error), return an SQLite error
172574** code. The final value of *pbOk is undefined in this case.
172575*/
172576static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
172577 int rc = SQLITE_OK; /* Return code */
172578 u64 cksum1 = 0; /* Checksum based on FTS index contents */
172579 u64 cksum2 = 0; /* Checksum based on %_content contents */
172580 sqlite3_stmt *pAllLangid = 0; /* Statement to return all language-ids */
172581
172582 /* This block calculates the checksum according to the FTS index. */
172583 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
172584 if( rc==SQLITE_OK ){
172585 int rc2;
172586 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
172587 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
172588 while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
172589 int iLangid = sqlite3_column_int(pAllLangid, 0);
172590 int i;
172591 for(i=0; i<p->nIndex; i++){
172592 cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
172593 }
172594 }
172595 rc2 = sqlite3_reset(pAllLangid);
172596 if( rc==SQLITE_OK ) rc = rc2;
172597 }
172598
172599 /* This block calculates the checksum according to the %_content table */
172600 if( rc==SQLITE_OK ){
172601 sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
172602 sqlite3_stmt *pStmt = 0;
172603 char *zSql;
172604
172605 zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
172606 if( !zSql ){
172607 rc = SQLITE_NOMEM;
172608 }else{
172609 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
172610 sqlite3_free(zSql);
172611 }
172612
172613 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
172614 i64 iDocid = sqlite3_column_int64(pStmt, 0);
172615 int iLang = langidFromSelect(p, pStmt);
172616 int iCol;
172617
172618 for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
172619 if( p->abNotindexed[iCol]==0 ){
172620 const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
172621 int nText = sqlite3_column_bytes(pStmt, iCol+1);
172622 sqlite3_tokenizer_cursor *pT = 0;
172623
172624 rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText,&pT);
172625 while( rc==SQLITE_OK ){
172626 char const *zToken; /* Buffer containing token */
172627 int nToken = 0; /* Number of bytes in token */
172628 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
172629 int iPos = 0; /* Position of token in zText */
172630
172631 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
172632 if( rc==SQLITE_OK ){
172633 int i;
172634 cksum2 = cksum2 ^ fts3ChecksumEntry(
172635 zToken, nToken, iLang, 0, iDocid, iCol, iPos
172636 );
172637 for(i=1; i<p->nIndex; i++){
172638 if( p->aIndex[i].nPrefix<=nToken ){
172639 cksum2 = cksum2 ^ fts3ChecksumEntry(
172640 zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
172641 );
172642 }
172643 }
172644 }
172645 }
172646 if( pT ) pModule->xClose(pT);
172647 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
172648 }
172649 }
172650 }
172651
172652 sqlite3_finalize(pStmt);
172653 }
172654
172655 *pbOk = (cksum1==cksum2);
172656 return rc;
172657}
172658
172659/*
172660** Run the integrity-check. If no error occurs and the current contents of
172661** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
172662** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
172663**
172664** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
172665** error code.
172666**
172667** The integrity-check works as follows. For each token and indexed token
172668** prefix in the document set, a 64-bit checksum is calculated (by code
172669** in fts3ChecksumEntry()) based on the following:
172670**
172671** + The index number (0 for the main index, 1 for the first prefix
172672** index etc.),
172673** + The token (or token prefix) text itself,
172674** + The language-id of the row it appears in,
172675** + The docid of the row it appears in,
172676** + The column it appears in, and
172677** + The tokens position within that column.
172678**
172679** The checksums for all entries in the index are XORed together to create
172680** a single checksum for the entire index.
172681**
172682** The integrity-check code calculates the same checksum in two ways:
172683**
172684** 1. By scanning the contents of the FTS index, and
172685** 2. By scanning and tokenizing the content table.
172686**
172687** If the two checksums are identical, the integrity-check is deemed to have
172688** passed.
172689*/
172690static int fts3DoIntegrityCheck(
172691 Fts3Table *p /* FTS3 table handle */
172692){
172693 int rc;
172694 int bOk = 0;
172695 rc = fts3IntegrityCheck(p, &bOk);
172696 if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB;
172697 return rc;
172698}
172699
172700/*
172701** Handle a 'special' INSERT of the form:
172702**
172703** "INSERT INTO tbl(tbl) VALUES(<expr>)"
172704**
172705** Argument pVal contains the result of <expr>. Currently the only
172706** meaningful value to insert is the text 'optimize'.
172707*/
172708static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
172709 int rc; /* Return Code */
172710 const char *zVal = (const char *)sqlite3_value_text(pVal);
172711 int nVal = sqlite3_value_bytes(pVal);
172712
172713 if( !zVal ){
172714 return SQLITE_NOMEM;
172715 }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
172716 rc = fts3DoOptimize(p, 0);
172717 }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
172718 rc = fts3DoRebuild(p);
172719 }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
172720 rc = fts3DoIntegrityCheck(p);
172721 }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
172722 rc = fts3DoIncrmerge(p, &zVal[6]);
172723 }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
172724 rc = fts3DoAutoincrmerge(p, &zVal[10]);
172725#ifdef SQLITE_TEST
172726 }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
172727 p->nNodeSize = atoi(&zVal[9]);
172728 rc = SQLITE_OK;
172729 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
172730 p->nMaxPendingData = atoi(&zVal[11]);
172731 rc = SQLITE_OK;
172732 }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){
172733 p->bNoIncrDoclist = atoi(&zVal[21]);
172734 rc = SQLITE_OK;
172735#endif
172736 }else{
172737 rc = SQLITE_ERROR;
172738 }
172739
172740 return rc;
172741}
172742
172743#ifndef SQLITE_DISABLE_FTS4_DEFERRED
172744/*
172745** Delete all cached deferred doclists. Deferred doclists are cached
172746** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
172747*/
172748SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
172749 Fts3DeferredToken *pDef;
172750 for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
172751 fts3PendingListDelete(pDef->pList);
172752 pDef->pList = 0;
172753 }
172754}
172755
172756/*
172757** Free all entries in the pCsr->pDeffered list. Entries are added to
172758** this list using sqlite3Fts3DeferToken().
172759*/
172760SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
172761 Fts3DeferredToken *pDef;
172762 Fts3DeferredToken *pNext;
172763 for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
172764 pNext = pDef->pNext;
172765 fts3PendingListDelete(pDef->pList);
172766 sqlite3_free(pDef);
172767 }
172768 pCsr->pDeferred = 0;
172769}
172770
172771/*
172772** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
172773** based on the row that pCsr currently points to.
172774**
172775** A deferred-doclist is like any other doclist with position information
172776** included, except that it only contains entries for a single row of the
172777** table, not for all rows.
172778*/
172779SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
172780 int rc = SQLITE_OK; /* Return code */
172781 if( pCsr->pDeferred ){
172782 int i; /* Used to iterate through table columns */
172783 sqlite3_int64 iDocid; /* Docid of the row pCsr points to */
172784 Fts3DeferredToken *pDef; /* Used to iterate through deferred tokens */
172785
172786 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
172787 sqlite3_tokenizer *pT = p->pTokenizer;
172788 sqlite3_tokenizer_module const *pModule = pT->pModule;
172789
172790 assert( pCsr->isRequireSeek==0 );
172791 iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
172792
172793 for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
172794 if( p->abNotindexed[i]==0 ){
172795 const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
172796 sqlite3_tokenizer_cursor *pTC = 0;
172797
172798 rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
172799 while( rc==SQLITE_OK ){
172800 char const *zToken; /* Buffer containing token */
172801 int nToken = 0; /* Number of bytes in token */
172802 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
172803 int iPos = 0; /* Position of token in zText */
172804
172805 rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
172806 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
172807 Fts3PhraseToken *pPT = pDef->pToken;
172808 if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
172809 && (pPT->bFirst==0 || iPos==0)
172810 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
172811 && (0==memcmp(zToken, pPT->z, pPT->n))
172812 ){
172813 fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
172814 }
172815 }
172816 }
172817 if( pTC ) pModule->xClose(pTC);
172818 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
172819 }
172820 }
172821
172822 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
172823 if( pDef->pList ){
172824 rc = fts3PendingListAppendVarint(&pDef->pList, 0);
172825 }
172826 }
172827 }
172828
172829 return rc;
172830}
172831
172832SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
172833 Fts3DeferredToken *p,
172834 char **ppData,
172835 int *pnData
172836){
172837 char *pRet;
172838 int nSkip;
172839 sqlite3_int64 dummy;
172840
172841 *ppData = 0;
172842 *pnData = 0;
172843
172844 if( p->pList==0 ){
172845 return SQLITE_OK;
172846 }
172847
172848 pRet = (char *)sqlite3_malloc(p->pList->nData);
172849 if( !pRet ) return SQLITE_NOMEM;
172850
172851 nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
172852 *pnData = p->pList->nData - nSkip;
172853 *ppData = pRet;
172854
172855 memcpy(pRet, &p->pList->aData[nSkip], *pnData);
172856 return SQLITE_OK;
172857}
172858
172859/*
172860** Add an entry for token pToken to the pCsr->pDeferred list.
172861*/
172862SQLITE_PRIVATE int sqlite3Fts3DeferToken(
172863 Fts3Cursor *pCsr, /* Fts3 table cursor */
172864 Fts3PhraseToken *pToken, /* Token to defer */
172865 int iCol /* Column that token must appear in (or -1) */
172866){
172867 Fts3DeferredToken *pDeferred;
172868 pDeferred = sqlite3_malloc(sizeof(*pDeferred));
172869 if( !pDeferred ){
172870 return SQLITE_NOMEM;
172871 }
172872 memset(pDeferred, 0, sizeof(*pDeferred));
172873 pDeferred->pToken = pToken;
172874 pDeferred->pNext = pCsr->pDeferred;
172875 pDeferred->iCol = iCol;
172876 pCsr->pDeferred = pDeferred;
172877
172878 assert( pToken->pDeferred==0 );
172879 pToken->pDeferred = pDeferred;
172880
172881 return SQLITE_OK;
172882}
172883#endif
172884
172885/*
172886** SQLite value pRowid contains the rowid of a row that may or may not be
172887** present in the FTS3 table. If it is, delete it and adjust the contents
172888** of subsiduary data structures accordingly.
172889*/
172890static int fts3DeleteByRowid(
172891 Fts3Table *p,
172892 sqlite3_value *pRowid,
172893 int *pnChng, /* IN/OUT: Decrement if row is deleted */
172894 u32 *aSzDel
172895){
172896 int rc = SQLITE_OK; /* Return code */
172897 int bFound = 0; /* True if *pRowid really is in the table */
172898
172899 fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
172900 if( bFound && rc==SQLITE_OK ){
172901 int isEmpty = 0; /* Deleting *pRowid leaves the table empty */
172902 rc = fts3IsEmpty(p, pRowid, &isEmpty);
172903 if( rc==SQLITE_OK ){
172904 if( isEmpty ){
172905 /* Deleting this row means the whole table is empty. In this case
172906 ** delete the contents of all three tables and throw away any
172907 ** data in the pendingTerms hash table. */
172908 rc = fts3DeleteAll(p, 1);
172909 *pnChng = 0;
172910 memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
172911 }else{
172912 *pnChng = *pnChng - 1;
172913 if( p->zContentTbl==0 ){
172914 fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
172915 }
172916 if( p->bHasDocsize ){
172917 fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
172918 }
172919 }
172920 }
172921 }
172922
172923 return rc;
172924}
172925
172926/*
172927** This function does the work for the xUpdate method of FTS3 virtual
172928** tables. The schema of the virtual table being:
172929**
172930** CREATE TABLE <table name>(
172931** <user columns>,
172932** <table name> HIDDEN,
172933** docid HIDDEN,
172934** <langid> HIDDEN
172935** );
172936**
172937**
172938*/
172939SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
172940 sqlite3_vtab *pVtab, /* FTS3 vtab object */
172941 int nArg, /* Size of argument array */
172942 sqlite3_value **apVal, /* Array of arguments */
172943 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
172944){
172945 Fts3Table *p = (Fts3Table *)pVtab;
172946 int rc = SQLITE_OK; /* Return Code */
172947 u32 *aSzIns = 0; /* Sizes of inserted documents */
172948 u32 *aSzDel = 0; /* Sizes of deleted documents */
172949 int nChng = 0; /* Net change in number of documents */
172950 int bInsertDone = 0;
172951
172952 /* At this point it must be known if the %_stat table exists or not.
172953 ** So bHasStat may not be 2. */
172954 assert( p->bHasStat==0 || p->bHasStat==1 );
172955
172956 assert( p->pSegments==0 );
172957 assert(
172958 nArg==1 /* DELETE operations */
172959 || nArg==(2 + p->nColumn + 3) /* INSERT or UPDATE operations */
172960 );
172961
172962 /* Check for a "special" INSERT operation. One of the form:
172963 **
172964 ** INSERT INTO xyz(xyz) VALUES('command');
172965 */
172966 if( nArg>1
172967 && sqlite3_value_type(apVal[0])==SQLITE_NULL
172968 && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL
172969 ){
172970 rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
172971 goto update_out;
172972 }
172973
172974 if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
172975 rc = SQLITE_CONSTRAINT;
172976 goto update_out;
172977 }
172978
172979 /* Allocate space to hold the change in document sizes */
172980 aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
172981 if( aSzDel==0 ){
172982 rc = SQLITE_NOMEM;
172983 goto update_out;
172984 }
172985 aSzIns = &aSzDel[p->nColumn+1];
172986 memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
172987
172988 rc = fts3Writelock(p);
172989 if( rc!=SQLITE_OK ) goto update_out;
172990
172991 /* If this is an INSERT operation, or an UPDATE that modifies the rowid
172992 ** value, then this operation requires constraint handling.
172993 **
172994 ** If the on-conflict mode is REPLACE, this means that the existing row
172995 ** should be deleted from the database before inserting the new row. Or,
172996 ** if the on-conflict mode is other than REPLACE, then this method must
172997 ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
172998 ** modify the database file.
172999 */
173000 if( nArg>1 && p->zContentTbl==0 ){
173001 /* Find the value object that holds the new rowid value. */
173002 sqlite3_value *pNewRowid = apVal[3+p->nColumn];
173003 if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
173004 pNewRowid = apVal[1];
173005 }
173006
173007 if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && (
173008 sqlite3_value_type(apVal[0])==SQLITE_NULL
173009 || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
173010 )){
173011 /* The new rowid is not NULL (in this case the rowid will be
173012 ** automatically assigned and there is no chance of a conflict), and
173013 ** the statement is either an INSERT or an UPDATE that modifies the
173014 ** rowid column. So if the conflict mode is REPLACE, then delete any
173015 ** existing row with rowid=pNewRowid.
173016 **
173017 ** Or, if the conflict mode is not REPLACE, insert the new record into
173018 ** the %_content table. If we hit the duplicate rowid constraint (or any
173019 ** other error) while doing so, return immediately.
173020 **
173021 ** This branch may also run if pNewRowid contains a value that cannot
173022 ** be losslessly converted to an integer. In this case, the eventual
173023 ** call to fts3InsertData() (either just below or further on in this
173024 ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
173025 ** invoked, it will delete zero rows (since no row will have
173026 ** docid=$pNewRowid if $pNewRowid is not an integer value).
173027 */
173028 if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
173029 rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
173030 }else{
173031 rc = fts3InsertData(p, apVal, pRowid);
173032 bInsertDone = 1;
173033 }
173034 }
173035 }
173036 if( rc!=SQLITE_OK ){
173037 goto update_out;
173038 }
173039
173040 /* If this is a DELETE or UPDATE operation, remove the old record. */
173041 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
173042 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
173043 rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
173044 }
173045
173046 /* If this is an INSERT or UPDATE operation, insert the new record. */
173047 if( nArg>1 && rc==SQLITE_OK ){
173048 int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
173049 if( bInsertDone==0 ){
173050 rc = fts3InsertData(p, apVal, pRowid);
173051 if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
173052 rc = FTS_CORRUPT_VTAB;
173053 }
173054 }
173055 if( rc==SQLITE_OK ){
173056 rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
173057 }
173058 if( rc==SQLITE_OK ){
173059 assert( p->iPrevDocid==*pRowid );
173060 rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
173061 }
173062 if( p->bHasDocsize ){
173063 fts3InsertDocsize(&rc, p, aSzIns);
173064 }
173065 nChng++;
173066 }
173067
173068 if( p->bFts4 ){
173069 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
173070 }
173071
173072 update_out:
173073 sqlite3_free(aSzDel);
173074 sqlite3Fts3SegmentsClose(p);
173075 return rc;
173076}
173077
173078/*
173079** Flush any data in the pending-terms hash table to disk. If successful,
173080** merge all segments in the database (including the new segment, if
173081** there was any data to flush) into a single segment.
173082*/
173083SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
173084 int rc;
173085 rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
173086 if( rc==SQLITE_OK ){
173087 rc = fts3DoOptimize(p, 1);
173088 if( rc==SQLITE_OK || rc==SQLITE_DONE ){
173089 int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
173090 if( rc2!=SQLITE_OK ) rc = rc2;
173091 }else{
173092 sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
173093 sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
173094 }
173095 }
173096 sqlite3Fts3SegmentsClose(p);
173097 return rc;
173098}
173099
173100#endif
173101
173102/************** End of fts3_write.c ******************************************/
173103/************** Begin file fts3_snippet.c ************************************/
173104/*
173105** 2009 Oct 23
173106**
173107** The author disclaims copyright to this source code. In place of
173108** a legal notice, here is a blessing:
173109**
173110** May you do good and not evil.
173111** May you find forgiveness for yourself and forgive others.
173112** May you share freely, never taking more than you give.
173113**
173114******************************************************************************
173115*/
173116
173117/* #include "fts3Int.h" */
173118#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
173119
173120/* #include <string.h> */
173121/* #include <assert.h> */
173122
173123/*
173124** Characters that may appear in the second argument to matchinfo().
173125*/
173126#define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */
173127#define FTS3_MATCHINFO_NCOL 'c' /* 1 value */
173128#define FTS3_MATCHINFO_NDOC 'n' /* 1 value */
173129#define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */
173130#define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */
173131#define FTS3_MATCHINFO_LCS 's' /* nCol values */
173132#define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */
173133#define FTS3_MATCHINFO_LHITS 'y' /* nCol*nPhrase values */
173134#define FTS3_MATCHINFO_LHITS_BM 'b' /* nCol*nPhrase values */
173135
173136/*
173137** The default value for the second argument to matchinfo().
173138*/
173139#define FTS3_MATCHINFO_DEFAULT "pcx"
173140
173141
173142/*
173143** Used as an fts3ExprIterate() context when loading phrase doclists to
173144** Fts3Expr.aDoclist[]/nDoclist.
173145*/
173146typedef struct LoadDoclistCtx LoadDoclistCtx;
173147struct LoadDoclistCtx {
173148 Fts3Cursor *pCsr; /* FTS3 Cursor */
173149 int nPhrase; /* Number of phrases seen so far */
173150 int nToken; /* Number of tokens seen so far */
173151};
173152
173153/*
173154** The following types are used as part of the implementation of the
173155** fts3BestSnippet() routine.
173156*/
173157typedef struct SnippetIter SnippetIter;
173158typedef struct SnippetPhrase SnippetPhrase;
173159typedef struct SnippetFragment SnippetFragment;
173160
173161struct SnippetIter {
173162 Fts3Cursor *pCsr; /* Cursor snippet is being generated from */
173163 int iCol; /* Extract snippet from this column */
173164 int nSnippet; /* Requested snippet length (in tokens) */
173165 int nPhrase; /* Number of phrases in query */
173166 SnippetPhrase *aPhrase; /* Array of size nPhrase */
173167 int iCurrent; /* First token of current snippet */
173168};
173169
173170struct SnippetPhrase {
173171 int nToken; /* Number of tokens in phrase */
173172 char *pList; /* Pointer to start of phrase position list */
173173 int iHead; /* Next value in position list */
173174 char *pHead; /* Position list data following iHead */
173175 int iTail; /* Next value in trailing position list */
173176 char *pTail; /* Position list data following iTail */
173177};
173178
173179struct SnippetFragment {
173180 int iCol; /* Column snippet is extracted from */
173181 int iPos; /* Index of first token in snippet */
173182 u64 covered; /* Mask of query phrases covered */
173183 u64 hlmask; /* Mask of snippet terms to highlight */
173184};
173185
173186/*
173187** This type is used as an fts3ExprIterate() context object while
173188** accumulating the data returned by the matchinfo() function.
173189*/
173190typedef struct MatchInfo MatchInfo;
173191struct MatchInfo {
173192 Fts3Cursor *pCursor; /* FTS3 Cursor */
173193 int nCol; /* Number of columns in table */
173194 int nPhrase; /* Number of matchable phrases in query */
173195 sqlite3_int64 nDoc; /* Number of docs in database */
173196 char flag;
173197 u32 *aMatchinfo; /* Pre-allocated buffer */
173198};
173199
173200/*
173201** An instance of this structure is used to manage a pair of buffers, each
173202** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
173203** for details.
173204*/
173205struct MatchinfoBuffer {
173206 u8 aRef[3];
173207 int nElem;
173208 int bGlobal; /* Set if global data is loaded */
173209 char *zMatchinfo;
173210 u32 aMatchinfo[1];
173211};
173212
173213
173214/*
173215** The snippet() and offsets() functions both return text values. An instance
173216** of the following structure is used to accumulate those values while the
173217** functions are running. See fts3StringAppend() for details.
173218*/
173219typedef struct StrBuffer StrBuffer;
173220struct StrBuffer {
173221 char *z; /* Pointer to buffer containing string */
173222 int n; /* Length of z in bytes (excl. nul-term) */
173223 int nAlloc; /* Allocated size of buffer z in bytes */
173224};
173225
173226
173227/*************************************************************************
173228** Start of MatchinfoBuffer code.
173229*/
173230
173231/*
173232** Allocate a two-slot MatchinfoBuffer object.
173233*/
173234static MatchinfoBuffer *fts3MIBufferNew(int nElem, const char *zMatchinfo){
173235 MatchinfoBuffer *pRet;
173236 int nByte = sizeof(u32) * (2*nElem + 1) + sizeof(MatchinfoBuffer);
173237 int nStr = (int)strlen(zMatchinfo);
173238
173239 pRet = sqlite3_malloc(nByte + nStr+1);
173240 if( pRet ){
173241 memset(pRet, 0, nByte);
173242 pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
173243 pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0] + sizeof(u32)*(nElem+1);
173244 pRet->nElem = nElem;
173245 pRet->zMatchinfo = ((char*)pRet) + nByte;
173246 memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
173247 pRet->aRef[0] = 1;
173248 }
173249
173250 return pRet;
173251}
173252
173253static void fts3MIBufferFree(void *p){
173254 MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
173255
173256 assert( (u32*)p==&pBuf->aMatchinfo[1]
173257 || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]
173258 );
173259 if( (u32*)p==&pBuf->aMatchinfo[1] ){
173260 pBuf->aRef[1] = 0;
173261 }else{
173262 pBuf->aRef[2] = 0;
173263 }
173264
173265 if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
173266 sqlite3_free(pBuf);
173267 }
173268}
173269
173270static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
173271 void (*xRet)(void*) = 0;
173272 u32 *aOut = 0;
173273
173274 if( p->aRef[1]==0 ){
173275 p->aRef[1] = 1;
173276 aOut = &p->aMatchinfo[1];
173277 xRet = fts3MIBufferFree;
173278 }
173279 else if( p->aRef[2]==0 ){
173280 p->aRef[2] = 1;
173281 aOut = &p->aMatchinfo[p->nElem+2];
173282 xRet = fts3MIBufferFree;
173283 }else{
173284 aOut = (u32*)sqlite3_malloc(p->nElem * sizeof(u32));
173285 if( aOut ){
173286 xRet = sqlite3_free;
173287 if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
173288 }
173289 }
173290
173291 *paOut = aOut;
173292 return xRet;
173293}
173294
173295static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
173296 p->bGlobal = 1;
173297 memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
173298}
173299
173300/*
173301** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
173302*/
173303SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
173304 if( p ){
173305 assert( p->aRef[0]==1 );
173306 p->aRef[0] = 0;
173307 if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
173308 sqlite3_free(p);
173309 }
173310 }
173311}
173312
173313/*
173314** End of MatchinfoBuffer code.
173315*************************************************************************/
173316
173317
173318/*
173319** This function is used to help iterate through a position-list. A position
173320** list is a list of unique integers, sorted from smallest to largest. Each
173321** element of the list is represented by an FTS3 varint that takes the value
173322** of the difference between the current element and the previous one plus
173323** two. For example, to store the position-list:
173324**
173325** 4 9 113
173326**
173327** the three varints:
173328**
173329** 6 7 106
173330**
173331** are encoded.
173332**
173333** When this function is called, *pp points to the start of an element of
173334** the list. *piPos contains the value of the previous entry in the list.
173335** After it returns, *piPos contains the value of the next element of the
173336** list and *pp is advanced to the following varint.
173337*/
173338static void fts3GetDeltaPosition(char **pp, int *piPos){
173339 int iVal;
173340 *pp += fts3GetVarint32(*pp, &iVal);
173341 *piPos += (iVal-2);
173342}
173343
173344/*
173345** Helper function for fts3ExprIterate() (see below).
173346*/
173347static int fts3ExprIterate2(
173348 Fts3Expr *pExpr, /* Expression to iterate phrases of */
173349 int *piPhrase, /* Pointer to phrase counter */
173350 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
173351 void *pCtx /* Second argument to pass to callback */
173352){
173353 int rc; /* Return code */
173354 int eType = pExpr->eType; /* Type of expression node pExpr */
173355
173356 if( eType!=FTSQUERY_PHRASE ){
173357 assert( pExpr->pLeft && pExpr->pRight );
173358 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
173359 if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
173360 rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
173361 }
173362 }else{
173363 rc = x(pExpr, *piPhrase, pCtx);
173364 (*piPhrase)++;
173365 }
173366 return rc;
173367}
173368
173369/*
173370** Iterate through all phrase nodes in an FTS3 query, except those that
173371** are part of a sub-tree that is the right-hand-side of a NOT operator.
173372** For each phrase node found, the supplied callback function is invoked.
173373**
173374** If the callback function returns anything other than SQLITE_OK,
173375** the iteration is abandoned and the error code returned immediately.
173376** Otherwise, SQLITE_OK is returned after a callback has been made for
173377** all eligible phrase nodes.
173378*/
173379static int fts3ExprIterate(
173380 Fts3Expr *pExpr, /* Expression to iterate phrases of */
173381 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
173382 void *pCtx /* Second argument to pass to callback */
173383){
173384 int iPhrase = 0; /* Variable used as the phrase counter */
173385 return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
173386}
173387
173388
173389/*
173390** This is an fts3ExprIterate() callback used while loading the doclists
173391** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
173392** fts3ExprLoadDoclists().
173393*/
173394static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
173395 int rc = SQLITE_OK;
173396 Fts3Phrase *pPhrase = pExpr->pPhrase;
173397 LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
173398
173399 UNUSED_PARAMETER(iPhrase);
173400
173401 p->nPhrase++;
173402 p->nToken += pPhrase->nToken;
173403
173404 return rc;
173405}
173406
173407/*
173408** Load the doclists for each phrase in the query associated with FTS3 cursor
173409** pCsr.
173410**
173411** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
173412** phrases in the expression (all phrases except those directly or
173413** indirectly descended from the right-hand-side of a NOT operator). If
173414** pnToken is not NULL, then it is set to the number of tokens in all
173415** matchable phrases of the expression.
173416*/
173417static int fts3ExprLoadDoclists(
173418 Fts3Cursor *pCsr, /* Fts3 cursor for current query */
173419 int *pnPhrase, /* OUT: Number of phrases in query */
173420 int *pnToken /* OUT: Number of tokens in query */
173421){
173422 int rc; /* Return Code */
173423 LoadDoclistCtx sCtx = {0,0,0}; /* Context for fts3ExprIterate() */
173424 sCtx.pCsr = pCsr;
173425 rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
173426 if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
173427 if( pnToken ) *pnToken = sCtx.nToken;
173428 return rc;
173429}
173430
173431static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
173432 (*(int *)ctx)++;
173433 pExpr->iPhrase = iPhrase;
173434 return SQLITE_OK;
173435}
173436static int fts3ExprPhraseCount(Fts3Expr *pExpr){
173437 int nPhrase = 0;
173438 (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
173439 return nPhrase;
173440}
173441
173442/*
173443** Advance the position list iterator specified by the first two
173444** arguments so that it points to the first element with a value greater
173445** than or equal to parameter iNext.
173446*/
173447static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
173448 char *pIter = *ppIter;
173449 if( pIter ){
173450 int iIter = *piIter;
173451
173452 while( iIter<iNext ){
173453 if( 0==(*pIter & 0xFE) ){
173454 iIter = -1;
173455 pIter = 0;
173456 break;
173457 }
173458 fts3GetDeltaPosition(&pIter, &iIter);
173459 }
173460
173461 *piIter = iIter;
173462 *ppIter = pIter;
173463 }
173464}
173465
173466/*
173467** Advance the snippet iterator to the next candidate snippet.
173468*/
173469static int fts3SnippetNextCandidate(SnippetIter *pIter){
173470 int i; /* Loop counter */
173471
173472 if( pIter->iCurrent<0 ){
173473 /* The SnippetIter object has just been initialized. The first snippet
173474 ** candidate always starts at offset 0 (even if this candidate has a
173475 ** score of 0.0).
173476 */
173477 pIter->iCurrent = 0;
173478
173479 /* Advance the 'head' iterator of each phrase to the first offset that
173480 ** is greater than or equal to (iNext+nSnippet).
173481 */
173482 for(i=0; i<pIter->nPhrase; i++){
173483 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
173484 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
173485 }
173486 }else{
173487 int iStart;
173488 int iEnd = 0x7FFFFFFF;
173489
173490 for(i=0; i<pIter->nPhrase; i++){
173491 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
173492 if( pPhrase->pHead && pPhrase->iHead<iEnd ){
173493 iEnd = pPhrase->iHead;
173494 }
173495 }
173496 if( iEnd==0x7FFFFFFF ){
173497 return 1;
173498 }
173499
173500 pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
173501 for(i=0; i<pIter->nPhrase; i++){
173502 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
173503 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
173504 fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
173505 }
173506 }
173507
173508 return 0;
173509}
173510
173511/*
173512** Retrieve information about the current candidate snippet of snippet
173513** iterator pIter.
173514*/
173515static void fts3SnippetDetails(
173516 SnippetIter *pIter, /* Snippet iterator */
173517 u64 mCovered, /* Bitmask of phrases already covered */
173518 int *piToken, /* OUT: First token of proposed snippet */
173519 int *piScore, /* OUT: "Score" for this snippet */
173520 u64 *pmCover, /* OUT: Bitmask of phrases covered */
173521 u64 *pmHighlight /* OUT: Bitmask of terms to highlight */
173522){
173523 int iStart = pIter->iCurrent; /* First token of snippet */
173524 int iScore = 0; /* Score of this snippet */
173525 int i; /* Loop counter */
173526 u64 mCover = 0; /* Mask of phrases covered by this snippet */
173527 u64 mHighlight = 0; /* Mask of tokens to highlight in snippet */
173528
173529 for(i=0; i<pIter->nPhrase; i++){
173530 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
173531 if( pPhrase->pTail ){
173532 char *pCsr = pPhrase->pTail;
173533 int iCsr = pPhrase->iTail;
173534
173535 while( iCsr<(iStart+pIter->nSnippet) ){
173536 int j;
173537 u64 mPhrase = (u64)1 << i;
173538 u64 mPos = (u64)1 << (iCsr - iStart);
173539 assert( iCsr>=iStart );
173540 if( (mCover|mCovered)&mPhrase ){
173541 iScore++;
173542 }else{
173543 iScore += 1000;
173544 }
173545 mCover |= mPhrase;
173546
173547 for(j=0; j<pPhrase->nToken; j++){
173548 mHighlight |= (mPos>>j);
173549 }
173550
173551 if( 0==(*pCsr & 0x0FE) ) break;
173552 fts3GetDeltaPosition(&pCsr, &iCsr);
173553 }
173554 }
173555 }
173556
173557 /* Set the output variables before returning. */
173558 *piToken = iStart;
173559 *piScore = iScore;
173560 *pmCover = mCover;
173561 *pmHighlight = mHighlight;
173562}
173563
173564/*
173565** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
173566** Each invocation populates an element of the SnippetIter.aPhrase[] array.
173567*/
173568static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
173569 SnippetIter *p = (SnippetIter *)ctx;
173570 SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
173571 char *pCsr;
173572 int rc;
173573
173574 pPhrase->nToken = pExpr->pPhrase->nToken;
173575 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
173576 assert( rc==SQLITE_OK || pCsr==0 );
173577 if( pCsr ){
173578 int iFirst = 0;
173579 pPhrase->pList = pCsr;
173580 fts3GetDeltaPosition(&pCsr, &iFirst);
173581 assert( iFirst>=0 );
173582 pPhrase->pHead = pCsr;
173583 pPhrase->pTail = pCsr;
173584 pPhrase->iHead = iFirst;
173585 pPhrase->iTail = iFirst;
173586 }else{
173587 assert( rc!=SQLITE_OK || (
173588 pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0
173589 ));
173590 }
173591
173592 return rc;
173593}
173594
173595/*
173596** Select the fragment of text consisting of nFragment contiguous tokens
173597** from column iCol that represent the "best" snippet. The best snippet
173598** is the snippet with the highest score, where scores are calculated
173599** by adding:
173600**
173601** (a) +1 point for each occurrence of a matchable phrase in the snippet.
173602**
173603** (b) +1000 points for the first occurrence of each matchable phrase in
173604** the snippet for which the corresponding mCovered bit is not set.
173605**
173606** The selected snippet parameters are stored in structure *pFragment before
173607** returning. The score of the selected snippet is stored in *piScore
173608** before returning.
173609*/
173610static int fts3BestSnippet(
173611 int nSnippet, /* Desired snippet length */
173612 Fts3Cursor *pCsr, /* Cursor to create snippet for */
173613 int iCol, /* Index of column to create snippet from */
173614 u64 mCovered, /* Mask of phrases already covered */
173615 u64 *pmSeen, /* IN/OUT: Mask of phrases seen */
173616 SnippetFragment *pFragment, /* OUT: Best snippet found */
173617 int *piScore /* OUT: Score of snippet pFragment */
173618){
173619 int rc; /* Return Code */
173620 int nList; /* Number of phrases in expression */
173621 SnippetIter sIter; /* Iterates through snippet candidates */
173622 int nByte; /* Number of bytes of space to allocate */
173623 int iBestScore = -1; /* Best snippet score found so far */
173624 int i; /* Loop counter */
173625
173626 memset(&sIter, 0, sizeof(sIter));
173627
173628 /* Iterate through the phrases in the expression to count them. The same
173629 ** callback makes sure the doclists are loaded for each phrase.
173630 */
173631 rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
173632 if( rc!=SQLITE_OK ){
173633 return rc;
173634 }
173635
173636 /* Now that it is known how many phrases there are, allocate and zero
173637 ** the required space using malloc().
173638 */
173639 nByte = sizeof(SnippetPhrase) * nList;
173640 sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
173641 if( !sIter.aPhrase ){
173642 return SQLITE_NOMEM;
173643 }
173644 memset(sIter.aPhrase, 0, nByte);
173645
173646 /* Initialize the contents of the SnippetIter object. Then iterate through
173647 ** the set of phrases in the expression to populate the aPhrase[] array.
173648 */
173649 sIter.pCsr = pCsr;
173650 sIter.iCol = iCol;
173651 sIter.nSnippet = nSnippet;
173652 sIter.nPhrase = nList;
173653 sIter.iCurrent = -1;
173654 rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter);
173655 if( rc==SQLITE_OK ){
173656
173657 /* Set the *pmSeen output variable. */
173658 for(i=0; i<nList; i++){
173659 if( sIter.aPhrase[i].pHead ){
173660 *pmSeen |= (u64)1 << i;
173661 }
173662 }
173663
173664 /* Loop through all candidate snippets. Store the best snippet in
173665 ** *pFragment. Store its associated 'score' in iBestScore.
173666 */
173667 pFragment->iCol = iCol;
173668 while( !fts3SnippetNextCandidate(&sIter) ){
173669 int iPos;
173670 int iScore;
173671 u64 mCover;
173672 u64 mHighlite;
173673 fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover,&mHighlite);
173674 assert( iScore>=0 );
173675 if( iScore>iBestScore ){
173676 pFragment->iPos = iPos;
173677 pFragment->hlmask = mHighlite;
173678 pFragment->covered = mCover;
173679 iBestScore = iScore;
173680 }
173681 }
173682
173683 *piScore = iBestScore;
173684 }
173685 sqlite3_free(sIter.aPhrase);
173686 return rc;
173687}
173688
173689
173690/*
173691** Append a string to the string-buffer passed as the first argument.
173692**
173693** If nAppend is negative, then the length of the string zAppend is
173694** determined using strlen().
173695*/
173696static int fts3StringAppend(
173697 StrBuffer *pStr, /* Buffer to append to */
173698 const char *zAppend, /* Pointer to data to append to buffer */
173699 int nAppend /* Size of zAppend in bytes (or -1) */
173700){
173701 if( nAppend<0 ){
173702 nAppend = (int)strlen(zAppend);
173703 }
173704
173705 /* If there is insufficient space allocated at StrBuffer.z, use realloc()
173706 ** to grow the buffer until so that it is big enough to accomadate the
173707 ** appended data.
173708 */
173709 if( pStr->n+nAppend+1>=pStr->nAlloc ){
173710 int nAlloc = pStr->nAlloc+nAppend+100;
173711 char *zNew = sqlite3_realloc(pStr->z, nAlloc);
173712 if( !zNew ){
173713 return SQLITE_NOMEM;
173714 }
173715 pStr->z = zNew;
173716 pStr->nAlloc = nAlloc;
173717 }
173718 assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
173719
173720 /* Append the data to the string buffer. */
173721 memcpy(&pStr->z[pStr->n], zAppend, nAppend);
173722 pStr->n += nAppend;
173723 pStr->z[pStr->n] = '\0';
173724
173725 return SQLITE_OK;
173726}
173727
173728/*
173729** The fts3BestSnippet() function often selects snippets that end with a
173730** query term. That is, the final term of the snippet is always a term
173731** that requires highlighting. For example, if 'X' is a highlighted term
173732** and '.' is a non-highlighted term, BestSnippet() may select:
173733**
173734** ........X.....X
173735**
173736** This function "shifts" the beginning of the snippet forward in the
173737** document so that there are approximately the same number of
173738** non-highlighted terms to the right of the final highlighted term as there
173739** are to the left of the first highlighted term. For example, to this:
173740**
173741** ....X.....X....
173742**
173743** This is done as part of extracting the snippet text, not when selecting
173744** the snippet. Snippet selection is done based on doclists only, so there
173745** is no way for fts3BestSnippet() to know whether or not the document
173746** actually contains terms that follow the final highlighted term.
173747*/
173748static int fts3SnippetShift(
173749 Fts3Table *pTab, /* FTS3 table snippet comes from */
173750 int iLangid, /* Language id to use in tokenizing */
173751 int nSnippet, /* Number of tokens desired for snippet */
173752 const char *zDoc, /* Document text to extract snippet from */
173753 int nDoc, /* Size of buffer zDoc in bytes */
173754 int *piPos, /* IN/OUT: First token of snippet */
173755 u64 *pHlmask /* IN/OUT: Mask of tokens to highlight */
173756){
173757 u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */
173758
173759 if( hlmask ){
173760 int nLeft; /* Tokens to the left of first highlight */
173761 int nRight; /* Tokens to the right of last highlight */
173762 int nDesired; /* Ideal number of tokens to shift forward */
173763
173764 for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
173765 for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
173766 nDesired = (nLeft-nRight)/2;
173767
173768 /* Ideally, the start of the snippet should be pushed forward in the
173769 ** document nDesired tokens. This block checks if there are actually
173770 ** nDesired tokens to the right of the snippet. If so, *piPos and
173771 ** *pHlMask are updated to shift the snippet nDesired tokens to the
173772 ** right. Otherwise, the snippet is shifted by the number of tokens
173773 ** available.
173774 */
173775 if( nDesired>0 ){
173776 int nShift; /* Number of tokens to shift snippet by */
173777 int iCurrent = 0; /* Token counter */
173778 int rc; /* Return Code */
173779 sqlite3_tokenizer_module *pMod;
173780 sqlite3_tokenizer_cursor *pC;
173781 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
173782
173783 /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
173784 ** or more tokens in zDoc/nDoc.
173785 */
173786 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
173787 if( rc!=SQLITE_OK ){
173788 return rc;
173789 }
173790 while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
173791 const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
173792 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
173793 }
173794 pMod->xClose(pC);
173795 if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
173796
173797 nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
173798 assert( nShift<=nDesired );
173799 if( nShift>0 ){
173800 *piPos += nShift;
173801 *pHlmask = hlmask >> nShift;
173802 }
173803 }
173804 }
173805 return SQLITE_OK;
173806}
173807
173808/*
173809** Extract the snippet text for fragment pFragment from cursor pCsr and
173810** append it to string buffer pOut.
173811*/
173812static int fts3SnippetText(
173813 Fts3Cursor *pCsr, /* FTS3 Cursor */
173814 SnippetFragment *pFragment, /* Snippet to extract */
173815 int iFragment, /* Fragment number */
173816 int isLast, /* True for final fragment in snippet */
173817 int nSnippet, /* Number of tokens in extracted snippet */
173818 const char *zOpen, /* String inserted before highlighted term */
173819 const char *zClose, /* String inserted after highlighted term */
173820 const char *zEllipsis, /* String inserted between snippets */
173821 StrBuffer *pOut /* Write output here */
173822){
173823 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
173824 int rc; /* Return code */
173825 const char *zDoc; /* Document text to extract snippet from */
173826 int nDoc; /* Size of zDoc in bytes */
173827 int iCurrent = 0; /* Current token number of document */
173828 int iEnd = 0; /* Byte offset of end of current token */
173829 int isShiftDone = 0; /* True after snippet is shifted */
173830 int iPos = pFragment->iPos; /* First token of snippet */
173831 u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
173832 int iCol = pFragment->iCol+1; /* Query column to extract text from */
173833 sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
173834 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */
173835
173836 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
173837 if( zDoc==0 ){
173838 if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
173839 return SQLITE_NOMEM;
173840 }
173841 return SQLITE_OK;
173842 }
173843 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
173844
173845 /* Open a token cursor on the document. */
173846 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
173847 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
173848 if( rc!=SQLITE_OK ){
173849 return rc;
173850 }
173851
173852 while( rc==SQLITE_OK ){
173853 const char *ZDUMMY; /* Dummy argument used with tokenizer */
173854 int DUMMY1 = -1; /* Dummy argument used with tokenizer */
173855 int iBegin = 0; /* Offset in zDoc of start of token */
173856 int iFin = 0; /* Offset in zDoc of end of token */
173857 int isHighlight = 0; /* True for highlighted terms */
173858
173859 /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
173860 ** in the FTS code the variable that the third argument to xNext points to
173861 ** is initialized to zero before the first (*but not necessarily
173862 ** subsequent*) call to xNext(). This is done for a particular application
173863 ** that needs to know whether or not the tokenizer is being used for
173864 ** snippet generation or for some other purpose.
173865 **
173866 ** Extreme care is required when writing code to depend on this
173867 ** initialization. It is not a documented part of the tokenizer interface.
173868 ** If a tokenizer is used directly by any code outside of FTS, this
173869 ** convention might not be respected. */
173870 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
173871 if( rc!=SQLITE_OK ){
173872 if( rc==SQLITE_DONE ){
173873 /* Special case - the last token of the snippet is also the last token
173874 ** of the column. Append any punctuation that occurred between the end
173875 ** of the previous token and the end of the document to the output.
173876 ** Then break out of the loop. */
173877 rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
173878 }
173879 break;
173880 }
173881 if( iCurrent<iPos ){ continue; }
173882
173883 if( !isShiftDone ){
173884 int n = nDoc - iBegin;
173885 rc = fts3SnippetShift(
173886 pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
173887 );
173888 isShiftDone = 1;
173889
173890 /* Now that the shift has been done, check if the initial "..." are
173891 ** required. They are required if (a) this is not the first fragment,
173892 ** or (b) this fragment does not begin at position 0 of its column.
173893 */
173894 if( rc==SQLITE_OK ){
173895 if( iPos>0 || iFragment>0 ){
173896 rc = fts3StringAppend(pOut, zEllipsis, -1);
173897 }else if( iBegin ){
173898 rc = fts3StringAppend(pOut, zDoc, iBegin);
173899 }
173900 }
173901 if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
173902 }
173903
173904 if( iCurrent>=(iPos+nSnippet) ){
173905 if( isLast ){
173906 rc = fts3StringAppend(pOut, zEllipsis, -1);
173907 }
173908 break;
173909 }
173910
173911 /* Set isHighlight to true if this term should be highlighted. */
173912 isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
173913
173914 if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
173915 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
173916 if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
173917 if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
173918
173919 iEnd = iFin;
173920 }
173921
173922 pMod->xClose(pC);
173923 return rc;
173924}
173925
173926
173927/*
173928** This function is used to count the entries in a column-list (a
173929** delta-encoded list of term offsets within a single column of a single
173930** row). When this function is called, *ppCollist should point to the
173931** beginning of the first varint in the column-list (the varint that
173932** contains the position of the first matching term in the column data).
173933** Before returning, *ppCollist is set to point to the first byte after
173934** the last varint in the column-list (either the 0x00 signifying the end
173935** of the position-list, or the 0x01 that precedes the column number of
173936** the next column in the position-list).
173937**
173938** The number of elements in the column-list is returned.
173939*/
173940static int fts3ColumnlistCount(char **ppCollist){
173941 char *pEnd = *ppCollist;
173942 char c = 0;
173943 int nEntry = 0;
173944
173945 /* A column-list is terminated by either a 0x01 or 0x00. */
173946 while( 0xFE & (*pEnd | c) ){
173947 c = *pEnd++ & 0x80;
173948 if( !c ) nEntry++;
173949 }
173950
173951 *ppCollist = pEnd;
173952 return nEntry;
173953}
173954
173955/*
173956** This function gathers 'y' or 'b' data for a single phrase.
173957*/
173958static void fts3ExprLHits(
173959 Fts3Expr *pExpr, /* Phrase expression node */
173960 MatchInfo *p /* Matchinfo context */
173961){
173962 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
173963 int iStart;
173964 Fts3Phrase *pPhrase = pExpr->pPhrase;
173965 char *pIter = pPhrase->doclist.pList;
173966 int iCol = 0;
173967
173968 assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS );
173969 if( p->flag==FTS3_MATCHINFO_LHITS ){
173970 iStart = pExpr->iPhrase * p->nCol;
173971 }else{
173972 iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
173973 }
173974
173975 while( 1 ){
173976 int nHit = fts3ColumnlistCount(&pIter);
173977 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
173978 if( p->flag==FTS3_MATCHINFO_LHITS ){
173979 p->aMatchinfo[iStart + iCol] = (u32)nHit;
173980 }else if( nHit ){
173981 p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
173982 }
173983 }
173984 assert( *pIter==0x00 || *pIter==0x01 );
173985 if( *pIter!=0x01 ) break;
173986 pIter++;
173987 pIter += fts3GetVarint32(pIter, &iCol);
173988 }
173989}
173990
173991/*
173992** Gather the results for matchinfo directives 'y' and 'b'.
173993*/
173994static void fts3ExprLHitGather(
173995 Fts3Expr *pExpr,
173996 MatchInfo *p
173997){
173998 assert( (pExpr->pLeft==0)==(pExpr->pRight==0) );
173999 if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
174000 if( pExpr->pLeft ){
174001 fts3ExprLHitGather(pExpr->pLeft, p);
174002 fts3ExprLHitGather(pExpr->pRight, p);
174003 }else{
174004 fts3ExprLHits(pExpr, p);
174005 }
174006 }
174007}
174008
174009/*
174010** fts3ExprIterate() callback used to collect the "global" matchinfo stats
174011** for a single query.
174012**
174013** fts3ExprIterate() callback to load the 'global' elements of a
174014** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
174015** of the matchinfo array that are constant for all rows returned by the
174016** current query.
174017**
174018** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
174019** function populates Matchinfo.aMatchinfo[] as follows:
174020**
174021** for(iCol=0; iCol<nCol; iCol++){
174022** aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
174023** aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
174024** }
174025**
174026** where X is the number of matches for phrase iPhrase is column iCol of all
174027** rows of the table. Y is the number of rows for which column iCol contains
174028** at least one instance of phrase iPhrase.
174029**
174030** If the phrase pExpr consists entirely of deferred tokens, then all X and
174031** Y values are set to nDoc, where nDoc is the number of documents in the
174032** file system. This is done because the full-text index doclist is required
174033** to calculate these values properly, and the full-text index doclist is
174034** not available for deferred tokens.
174035*/
174036static int fts3ExprGlobalHitsCb(
174037 Fts3Expr *pExpr, /* Phrase expression node */
174038 int iPhrase, /* Phrase number (numbered from zero) */
174039 void *pCtx /* Pointer to MatchInfo structure */
174040){
174041 MatchInfo *p = (MatchInfo *)pCtx;
174042 return sqlite3Fts3EvalPhraseStats(
174043 p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
174044 );
174045}
174046
174047/*
174048** fts3ExprIterate() callback used to collect the "local" part of the
174049** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
174050** array that are different for each row returned by the query.
174051*/
174052static int fts3ExprLocalHitsCb(
174053 Fts3Expr *pExpr, /* Phrase expression node */
174054 int iPhrase, /* Phrase number */
174055 void *pCtx /* Pointer to MatchInfo structure */
174056){
174057 int rc = SQLITE_OK;
174058 MatchInfo *p = (MatchInfo *)pCtx;
174059 int iStart = iPhrase * p->nCol * 3;
174060 int i;
174061
174062 for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
174063 char *pCsr;
174064 rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
174065 if( pCsr ){
174066 p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
174067 }else{
174068 p->aMatchinfo[iStart+i*3] = 0;
174069 }
174070 }
174071
174072 return rc;
174073}
174074
174075static int fts3MatchinfoCheck(
174076 Fts3Table *pTab,
174077 char cArg,
174078 char **pzErr
174079){
174080 if( (cArg==FTS3_MATCHINFO_NPHRASE)
174081 || (cArg==FTS3_MATCHINFO_NCOL)
174082 || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
174083 || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
174084 || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
174085 || (cArg==FTS3_MATCHINFO_LCS)
174086 || (cArg==FTS3_MATCHINFO_HITS)
174087 || (cArg==FTS3_MATCHINFO_LHITS)
174088 || (cArg==FTS3_MATCHINFO_LHITS_BM)
174089 ){
174090 return SQLITE_OK;
174091 }
174092 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
174093 return SQLITE_ERROR;
174094}
174095
174096static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
174097 int nVal; /* Number of integers output by cArg */
174098
174099 switch( cArg ){
174100 case FTS3_MATCHINFO_NDOC:
174101 case FTS3_MATCHINFO_NPHRASE:
174102 case FTS3_MATCHINFO_NCOL:
174103 nVal = 1;
174104 break;
174105
174106 case FTS3_MATCHINFO_AVGLENGTH:
174107 case FTS3_MATCHINFO_LENGTH:
174108 case FTS3_MATCHINFO_LCS:
174109 nVal = pInfo->nCol;
174110 break;
174111
174112 case FTS3_MATCHINFO_LHITS:
174113 nVal = pInfo->nCol * pInfo->nPhrase;
174114 break;
174115
174116 case FTS3_MATCHINFO_LHITS_BM:
174117 nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
174118 break;
174119
174120 default:
174121 assert( cArg==FTS3_MATCHINFO_HITS );
174122 nVal = pInfo->nCol * pInfo->nPhrase * 3;
174123 break;
174124 }
174125
174126 return nVal;
174127}
174128
174129static int fts3MatchinfoSelectDoctotal(
174130 Fts3Table *pTab,
174131 sqlite3_stmt **ppStmt,
174132 sqlite3_int64 *pnDoc,
174133 const char **paLen
174134){
174135 sqlite3_stmt *pStmt;
174136 const char *a;
174137 sqlite3_int64 nDoc;
174138
174139 if( !*ppStmt ){
174140 int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
174141 if( rc!=SQLITE_OK ) return rc;
174142 }
174143 pStmt = *ppStmt;
174144 assert( sqlite3_data_count(pStmt)==1 );
174145
174146 a = sqlite3_column_blob(pStmt, 0);
174147 a += sqlite3Fts3GetVarint(a, &nDoc);
174148 if( nDoc==0 ) return FTS_CORRUPT_VTAB;
174149 *pnDoc = (u32)nDoc;
174150
174151 if( paLen ) *paLen = a;
174152 return SQLITE_OK;
174153}
174154
174155/*
174156** An instance of the following structure is used to store state while
174157** iterating through a multi-column position-list corresponding to the
174158** hits for a single phrase on a single row in order to calculate the
174159** values for a matchinfo() FTS3_MATCHINFO_LCS request.
174160*/
174161typedef struct LcsIterator LcsIterator;
174162struct LcsIterator {
174163 Fts3Expr *pExpr; /* Pointer to phrase expression */
174164 int iPosOffset; /* Tokens count up to end of this phrase */
174165 char *pRead; /* Cursor used to iterate through aDoclist */
174166 int iPos; /* Current position */
174167};
174168
174169/*
174170** If LcsIterator.iCol is set to the following value, the iterator has
174171** finished iterating through all offsets for all columns.
174172*/
174173#define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
174174
174175static int fts3MatchinfoLcsCb(
174176 Fts3Expr *pExpr, /* Phrase expression node */
174177 int iPhrase, /* Phrase number (numbered from zero) */
174178 void *pCtx /* Pointer to MatchInfo structure */
174179){
174180 LcsIterator *aIter = (LcsIterator *)pCtx;
174181 aIter[iPhrase].pExpr = pExpr;
174182 return SQLITE_OK;
174183}
174184
174185/*
174186** Advance the iterator passed as an argument to the next position. Return
174187** 1 if the iterator is at EOF or if it now points to the start of the
174188** position list for the next column.
174189*/
174190static int fts3LcsIteratorAdvance(LcsIterator *pIter){
174191 char *pRead = pIter->pRead;
174192 sqlite3_int64 iRead;
174193 int rc = 0;
174194
174195 pRead += sqlite3Fts3GetVarint(pRead, &iRead);
174196 if( iRead==0 || iRead==1 ){
174197 pRead = 0;
174198 rc = 1;
174199 }else{
174200 pIter->iPos += (int)(iRead-2);
174201 }
174202
174203 pIter->pRead = pRead;
174204 return rc;
174205}
174206
174207/*
174208** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
174209**
174210** If the call is successful, the longest-common-substring lengths for each
174211** column are written into the first nCol elements of the pInfo->aMatchinfo[]
174212** array before returning. SQLITE_OK is returned in this case.
174213**
174214** Otherwise, if an error occurs, an SQLite error code is returned and the
174215** data written to the first nCol elements of pInfo->aMatchinfo[] is
174216** undefined.
174217*/
174218static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
174219 LcsIterator *aIter;
174220 int i;
174221 int iCol;
174222 int nToken = 0;
174223
174224 /* Allocate and populate the array of LcsIterator objects. The array
174225 ** contains one element for each matchable phrase in the query.
174226 **/
174227 aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
174228 if( !aIter ) return SQLITE_NOMEM;
174229 memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
174230 (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
174231
174232 for(i=0; i<pInfo->nPhrase; i++){
174233 LcsIterator *pIter = &aIter[i];
174234 nToken -= pIter->pExpr->pPhrase->nToken;
174235 pIter->iPosOffset = nToken;
174236 }
174237
174238 for(iCol=0; iCol<pInfo->nCol; iCol++){
174239 int nLcs = 0; /* LCS value for this column */
174240 int nLive = 0; /* Number of iterators in aIter not at EOF */
174241
174242 for(i=0; i<pInfo->nPhrase; i++){
174243 int rc;
174244 LcsIterator *pIt = &aIter[i];
174245 rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
174246 if( rc!=SQLITE_OK ) return rc;
174247 if( pIt->pRead ){
174248 pIt->iPos = pIt->iPosOffset;
174249 fts3LcsIteratorAdvance(&aIter[i]);
174250 nLive++;
174251 }
174252 }
174253
174254 while( nLive>0 ){
174255 LcsIterator *pAdv = 0; /* The iterator to advance by one position */
174256 int nThisLcs = 0; /* LCS for the current iterator positions */
174257
174258 for(i=0; i<pInfo->nPhrase; i++){
174259 LcsIterator *pIter = &aIter[i];
174260 if( pIter->pRead==0 ){
174261 /* This iterator is already at EOF for this column. */
174262 nThisLcs = 0;
174263 }else{
174264 if( pAdv==0 || pIter->iPos<pAdv->iPos ){
174265 pAdv = pIter;
174266 }
174267 if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
174268 nThisLcs++;
174269 }else{
174270 nThisLcs = 1;
174271 }
174272 if( nThisLcs>nLcs ) nLcs = nThisLcs;
174273 }
174274 }
174275 if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
174276 }
174277
174278 pInfo->aMatchinfo[iCol] = nLcs;
174279 }
174280
174281 sqlite3_free(aIter);
174282 return SQLITE_OK;
174283}
174284
174285/*
174286** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
174287** be returned by the matchinfo() function. Argument zArg contains the
174288** format string passed as the second argument to matchinfo (or the
174289** default value "pcx" if no second argument was specified). The format
174290** string has already been validated and the pInfo->aMatchinfo[] array
174291** is guaranteed to be large enough for the output.
174292**
174293** If bGlobal is true, then populate all fields of the matchinfo() output.
174294** If it is false, then assume that those fields that do not change between
174295** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
174296** have already been populated.
174297**
174298** Return SQLITE_OK if successful, or an SQLite error code if an error
174299** occurs. If a value other than SQLITE_OK is returned, the state the
174300** pInfo->aMatchinfo[] buffer is left in is undefined.
174301*/
174302static int fts3MatchinfoValues(
174303 Fts3Cursor *pCsr, /* FTS3 cursor object */
174304 int bGlobal, /* True to grab the global stats */
174305 MatchInfo *pInfo, /* Matchinfo context object */
174306 const char *zArg /* Matchinfo format string */
174307){
174308 int rc = SQLITE_OK;
174309 int i;
174310 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
174311 sqlite3_stmt *pSelect = 0;
174312
174313 for(i=0; rc==SQLITE_OK && zArg[i]; i++){
174314 pInfo->flag = zArg[i];
174315 switch( zArg[i] ){
174316 case FTS3_MATCHINFO_NPHRASE:
174317 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
174318 break;
174319
174320 case FTS3_MATCHINFO_NCOL:
174321 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
174322 break;
174323
174324 case FTS3_MATCHINFO_NDOC:
174325 if( bGlobal ){
174326 sqlite3_int64 nDoc = 0;
174327 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
174328 pInfo->aMatchinfo[0] = (u32)nDoc;
174329 }
174330 break;
174331
174332 case FTS3_MATCHINFO_AVGLENGTH:
174333 if( bGlobal ){
174334 sqlite3_int64 nDoc; /* Number of rows in table */
174335 const char *a; /* Aggregate column length array */
174336
174337 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
174338 if( rc==SQLITE_OK ){
174339 int iCol;
174340 for(iCol=0; iCol<pInfo->nCol; iCol++){
174341 u32 iVal;
174342 sqlite3_int64 nToken;
174343 a += sqlite3Fts3GetVarint(a, &nToken);
174344 iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
174345 pInfo->aMatchinfo[iCol] = iVal;
174346 }
174347 }
174348 }
174349 break;
174350
174351 case FTS3_MATCHINFO_LENGTH: {
174352 sqlite3_stmt *pSelectDocsize = 0;
174353 rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
174354 if( rc==SQLITE_OK ){
174355 int iCol;
174356 const char *a = sqlite3_column_blob(pSelectDocsize, 0);
174357 for(iCol=0; iCol<pInfo->nCol; iCol++){
174358 sqlite3_int64 nToken;
174359 a += sqlite3Fts3GetVarint(a, &nToken);
174360 pInfo->aMatchinfo[iCol] = (u32)nToken;
174361 }
174362 }
174363 sqlite3_reset(pSelectDocsize);
174364 break;
174365 }
174366
174367 case FTS3_MATCHINFO_LCS:
174368 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
174369 if( rc==SQLITE_OK ){
174370 rc = fts3MatchinfoLcs(pCsr, pInfo);
174371 }
174372 break;
174373
174374 case FTS3_MATCHINFO_LHITS_BM:
174375 case FTS3_MATCHINFO_LHITS: {
174376 int nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32);
174377 memset(pInfo->aMatchinfo, 0, nZero);
174378 fts3ExprLHitGather(pCsr->pExpr, pInfo);
174379 break;
174380 }
174381
174382 default: {
174383 Fts3Expr *pExpr;
174384 assert( zArg[i]==FTS3_MATCHINFO_HITS );
174385 pExpr = pCsr->pExpr;
174386 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
174387 if( rc!=SQLITE_OK ) break;
174388 if( bGlobal ){
174389 if( pCsr->pDeferred ){
174390 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
174391 if( rc!=SQLITE_OK ) break;
174392 }
174393 rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
174394 sqlite3Fts3EvalTestDeferred(pCsr, &rc);
174395 if( rc!=SQLITE_OK ) break;
174396 }
174397 (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
174398 break;
174399 }
174400 }
174401
174402 pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
174403 }
174404
174405 sqlite3_reset(pSelect);
174406 return rc;
174407}
174408
174409
174410/*
174411** Populate pCsr->aMatchinfo[] with data for the current row. The
174412** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
174413*/
174414static void fts3GetMatchinfo(
174415 sqlite3_context *pCtx, /* Return results here */
174416 Fts3Cursor *pCsr, /* FTS3 Cursor object */
174417 const char *zArg /* Second argument to matchinfo() function */
174418){
174419 MatchInfo sInfo;
174420 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
174421 int rc = SQLITE_OK;
174422 int bGlobal = 0; /* Collect 'global' stats as well as local */
174423
174424 u32 *aOut = 0;
174425 void (*xDestroyOut)(void*) = 0;
174426
174427 memset(&sInfo, 0, sizeof(MatchInfo));
174428 sInfo.pCursor = pCsr;
174429 sInfo.nCol = pTab->nColumn;
174430
174431 /* If there is cached matchinfo() data, but the format string for the
174432 ** cache does not match the format string for this request, discard
174433 ** the cached data. */
174434 if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
174435 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
174436 pCsr->pMIBuffer = 0;
174437 }
174438
174439 /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
174440 ** matchinfo function has been called for this query. In this case
174441 ** allocate the array used to accumulate the matchinfo data and
174442 ** initialize those elements that are constant for every row.
174443 */
174444 if( pCsr->pMIBuffer==0 ){
174445 int nMatchinfo = 0; /* Number of u32 elements in match-info */
174446 int i; /* Used to iterate through zArg */
174447
174448 /* Determine the number of phrases in the query */
174449 pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
174450 sInfo.nPhrase = pCsr->nPhrase;
174451
174452 /* Determine the number of integers in the buffer returned by this call. */
174453 for(i=0; zArg[i]; i++){
174454 char *zErr = 0;
174455 if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
174456 sqlite3_result_error(pCtx, zErr, -1);
174457 sqlite3_free(zErr);
174458 return;
174459 }
174460 nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
174461 }
174462
174463 /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
174464 pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
174465 if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM;
174466
174467 pCsr->isMatchinfoNeeded = 1;
174468 bGlobal = 1;
174469 }
174470
174471 if( rc==SQLITE_OK ){
174472 xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
174473 if( xDestroyOut==0 ){
174474 rc = SQLITE_NOMEM;
174475 }
174476 }
174477
174478 if( rc==SQLITE_OK ){
174479 sInfo.aMatchinfo = aOut;
174480 sInfo.nPhrase = pCsr->nPhrase;
174481 rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
174482 if( bGlobal ){
174483 fts3MIBufferSetGlobal(pCsr->pMIBuffer);
174484 }
174485 }
174486
174487 if( rc!=SQLITE_OK ){
174488 sqlite3_result_error_code(pCtx, rc);
174489 if( xDestroyOut ) xDestroyOut(aOut);
174490 }else{
174491 int n = pCsr->pMIBuffer->nElem * sizeof(u32);
174492 sqlite3_result_blob(pCtx, aOut, n, xDestroyOut);
174493 }
174494}
174495
174496/*
174497** Implementation of snippet() function.
174498*/
174499SQLITE_PRIVATE void sqlite3Fts3Snippet(
174500 sqlite3_context *pCtx, /* SQLite function call context */
174501 Fts3Cursor *pCsr, /* Cursor object */
174502 const char *zStart, /* Snippet start text - "<b>" */
174503 const char *zEnd, /* Snippet end text - "</b>" */
174504 const char *zEllipsis, /* Snippet ellipsis text - "<b>...</b>" */
174505 int iCol, /* Extract snippet from this column */
174506 int nToken /* Approximate number of tokens in snippet */
174507){
174508 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
174509 int rc = SQLITE_OK;
174510 int i;
174511 StrBuffer res = {0, 0, 0};
174512
174513 /* The returned text includes up to four fragments of text extracted from
174514 ** the data in the current row. The first iteration of the for(...) loop
174515 ** below attempts to locate a single fragment of text nToken tokens in
174516 ** size that contains at least one instance of all phrases in the query
174517 ** expression that appear in the current row. If such a fragment of text
174518 ** cannot be found, the second iteration of the loop attempts to locate
174519 ** a pair of fragments, and so on.
174520 */
174521 int nSnippet = 0; /* Number of fragments in this snippet */
174522 SnippetFragment aSnippet[4]; /* Maximum of 4 fragments per snippet */
174523 int nFToken = -1; /* Number of tokens in each fragment */
174524
174525 if( !pCsr->pExpr ){
174526 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
174527 return;
174528 }
174529
174530 for(nSnippet=1; 1; nSnippet++){
174531
174532 int iSnip; /* Loop counter 0..nSnippet-1 */
174533 u64 mCovered = 0; /* Bitmask of phrases covered by snippet */
174534 u64 mSeen = 0; /* Bitmask of phrases seen by BestSnippet() */
174535
174536 if( nToken>=0 ){
174537 nFToken = (nToken+nSnippet-1) / nSnippet;
174538 }else{
174539 nFToken = -1 * nToken;
174540 }
174541
174542 for(iSnip=0; iSnip<nSnippet; iSnip++){
174543 int iBestScore = -1; /* Best score of columns checked so far */
174544 int iRead; /* Used to iterate through columns */
174545 SnippetFragment *pFragment = &aSnippet[iSnip];
174546
174547 memset(pFragment, 0, sizeof(*pFragment));
174548
174549 /* Loop through all columns of the table being considered for snippets.
174550 ** If the iCol argument to this function was negative, this means all
174551 ** columns of the FTS3 table. Otherwise, only column iCol is considered.
174552 */
174553 for(iRead=0; iRead<pTab->nColumn; iRead++){
174554 SnippetFragment sF = {0, 0, 0, 0};
174555 int iS = 0;
174556 if( iCol>=0 && iRead!=iCol ) continue;
174557
174558 /* Find the best snippet of nFToken tokens in column iRead. */
174559 rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
174560 if( rc!=SQLITE_OK ){
174561 goto snippet_out;
174562 }
174563 if( iS>iBestScore ){
174564 *pFragment = sF;
174565 iBestScore = iS;
174566 }
174567 }
174568
174569 mCovered |= pFragment->covered;
174570 }
174571
174572 /* If all query phrases seen by fts3BestSnippet() are present in at least
174573 ** one of the nSnippet snippet fragments, break out of the loop.
174574 */
174575 assert( (mCovered&mSeen)==mCovered );
174576 if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
174577 }
174578
174579 assert( nFToken>0 );
174580
174581 for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
174582 rc = fts3SnippetText(pCsr, &aSnippet[i],
174583 i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
174584 );
174585 }
174586
174587 snippet_out:
174588 sqlite3Fts3SegmentsClose(pTab);
174589 if( rc!=SQLITE_OK ){
174590 sqlite3_result_error_code(pCtx, rc);
174591 sqlite3_free(res.z);
174592 }else{
174593 sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
174594 }
174595}
174596
174597
174598typedef struct TermOffset TermOffset;
174599typedef struct TermOffsetCtx TermOffsetCtx;
174600
174601struct TermOffset {
174602 char *pList; /* Position-list */
174603 int iPos; /* Position just read from pList */
174604 int iOff; /* Offset of this term from read positions */
174605};
174606
174607struct TermOffsetCtx {
174608 Fts3Cursor *pCsr;
174609 int iCol; /* Column of table to populate aTerm for */
174610 int iTerm;
174611 sqlite3_int64 iDocid;
174612 TermOffset *aTerm;
174613};
174614
174615/*
174616** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
174617*/
174618static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
174619 TermOffsetCtx *p = (TermOffsetCtx *)ctx;
174620 int nTerm; /* Number of tokens in phrase */
174621 int iTerm; /* For looping through nTerm phrase terms */
174622 char *pList; /* Pointer to position list for phrase */
174623 int iPos = 0; /* First position in position-list */
174624 int rc;
174625
174626 UNUSED_PARAMETER(iPhrase);
174627 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
174628 nTerm = pExpr->pPhrase->nToken;
174629 if( pList ){
174630 fts3GetDeltaPosition(&pList, &iPos);
174631 assert( iPos>=0 );
174632 }
174633
174634 for(iTerm=0; iTerm<nTerm; iTerm++){
174635 TermOffset *pT = &p->aTerm[p->iTerm++];
174636 pT->iOff = nTerm-iTerm-1;
174637 pT->pList = pList;
174638 pT->iPos = iPos;
174639 }
174640
174641 return rc;
174642}
174643
174644/*
174645** Implementation of offsets() function.
174646*/
174647SQLITE_PRIVATE void sqlite3Fts3Offsets(
174648 sqlite3_context *pCtx, /* SQLite function call context */
174649 Fts3Cursor *pCsr /* Cursor object */
174650){
174651 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
174652 sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
174653 int rc; /* Return Code */
174654 int nToken; /* Number of tokens in query */
174655 int iCol; /* Column currently being processed */
174656 StrBuffer res = {0, 0, 0}; /* Result string */
174657 TermOffsetCtx sCtx; /* Context for fts3ExprTermOffsetInit() */
174658
174659 if( !pCsr->pExpr ){
174660 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
174661 return;
174662 }
174663
174664 memset(&sCtx, 0, sizeof(sCtx));
174665 assert( pCsr->isRequireSeek==0 );
174666
174667 /* Count the number of terms in the query */
174668 rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
174669 if( rc!=SQLITE_OK ) goto offsets_out;
174670
174671 /* Allocate the array of TermOffset iterators. */
174672 sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
174673 if( 0==sCtx.aTerm ){
174674 rc = SQLITE_NOMEM;
174675 goto offsets_out;
174676 }
174677 sCtx.iDocid = pCsr->iPrevId;
174678 sCtx.pCsr = pCsr;
174679
174680 /* Loop through the table columns, appending offset information to
174681 ** string-buffer res for each column.
174682 */
174683 for(iCol=0; iCol<pTab->nColumn; iCol++){
174684 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
174685 const char *ZDUMMY; /* Dummy argument used with xNext() */
174686 int NDUMMY = 0; /* Dummy argument used with xNext() */
174687 int iStart = 0;
174688 int iEnd = 0;
174689 int iCurrent = 0;
174690 const char *zDoc;
174691 int nDoc;
174692
174693 /* Initialize the contents of sCtx.aTerm[] for column iCol. There is
174694 ** no way that this operation can fail, so the return code from
174695 ** fts3ExprIterate() can be discarded.
174696 */
174697 sCtx.iCol = iCol;
174698 sCtx.iTerm = 0;
174699 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx);
174700
174701 /* Retreive the text stored in column iCol. If an SQL NULL is stored
174702 ** in column iCol, jump immediately to the next iteration of the loop.
174703 ** If an OOM occurs while retrieving the data (this can happen if SQLite
174704 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
174705 ** to the caller.
174706 */
174707 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
174708 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
174709 if( zDoc==0 ){
174710 if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
174711 continue;
174712 }
174713 rc = SQLITE_NOMEM;
174714 goto offsets_out;
174715 }
174716
174717 /* Initialize a tokenizer iterator to iterate through column iCol. */
174718 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
174719 zDoc, nDoc, &pC
174720 );
174721 if( rc!=SQLITE_OK ) goto offsets_out;
174722
174723 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
174724 while( rc==SQLITE_OK ){
174725 int i; /* Used to loop through terms */
174726 int iMinPos = 0x7FFFFFFF; /* Position of next token */
174727 TermOffset *pTerm = 0; /* TermOffset associated with next token */
174728
174729 for(i=0; i<nToken; i++){
174730 TermOffset *pT = &sCtx.aTerm[i];
174731 if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
174732 iMinPos = pT->iPos-pT->iOff;
174733 pTerm = pT;
174734 }
174735 }
174736
174737 if( !pTerm ){
174738 /* All offsets for this column have been gathered. */
174739 rc = SQLITE_DONE;
174740 }else{
174741 assert( iCurrent<=iMinPos );
174742 if( 0==(0xFE&*pTerm->pList) ){
174743 pTerm->pList = 0;
174744 }else{
174745 fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
174746 }
174747 while( rc==SQLITE_OK && iCurrent<iMinPos ){
174748 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
174749 }
174750 if( rc==SQLITE_OK ){
174751 char aBuffer[64];
174752 sqlite3_snprintf(sizeof(aBuffer), aBuffer,
174753 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
174754 );
174755 rc = fts3StringAppend(&res, aBuffer, -1);
174756 }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
174757 rc = FTS_CORRUPT_VTAB;
174758 }
174759 }
174760 }
174761 if( rc==SQLITE_DONE ){
174762 rc = SQLITE_OK;
174763 }
174764
174765 pMod->xClose(pC);
174766 if( rc!=SQLITE_OK ) goto offsets_out;
174767 }
174768
174769 offsets_out:
174770 sqlite3_free(sCtx.aTerm);
174771 assert( rc!=SQLITE_DONE );
174772 sqlite3Fts3SegmentsClose(pTab);
174773 if( rc!=SQLITE_OK ){
174774 sqlite3_result_error_code(pCtx, rc);
174775 sqlite3_free(res.z);
174776 }else{
174777 sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
174778 }
174779 return;
174780}
174781
174782/*
174783** Implementation of matchinfo() function.
174784*/
174785SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
174786 sqlite3_context *pContext, /* Function call context */
174787 Fts3Cursor *pCsr, /* FTS3 table cursor */
174788 const char *zArg /* Second arg to matchinfo() function */
174789){
174790 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
174791 const char *zFormat;
174792
174793 if( zArg ){
174794 zFormat = zArg;
174795 }else{
174796 zFormat = FTS3_MATCHINFO_DEFAULT;
174797 }
174798
174799 if( !pCsr->pExpr ){
174800 sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
174801 return;
174802 }else{
174803 /* Retrieve matchinfo() data. */
174804 fts3GetMatchinfo(pContext, pCsr, zFormat);
174805 sqlite3Fts3SegmentsClose(pTab);
174806 }
174807}
174808
174809#endif
174810
174811/************** End of fts3_snippet.c ****************************************/
174812/************** Begin file fts3_unicode.c ************************************/
174813/*
174814** 2012 May 24
174815**
174816** The author disclaims copyright to this source code. In place of
174817** a legal notice, here is a blessing:
174818**
174819** May you do good and not evil.
174820** May you find forgiveness for yourself and forgive others.
174821** May you share freely, never taking more than you give.
174822**
174823******************************************************************************
174824**
174825** Implementation of the "unicode" full-text-search tokenizer.
174826*/
174827
174828#ifndef SQLITE_DISABLE_FTS3_UNICODE
174829
174830/* #include "fts3Int.h" */
174831#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
174832
174833/* #include <assert.h> */
174834/* #include <stdlib.h> */
174835/* #include <stdio.h> */
174836/* #include <string.h> */
174837
174838/* #include "fts3_tokenizer.h" */
174839
174840/*
174841** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
174842** from the sqlite3 source file utf.c. If this file is compiled as part
174843** of the amalgamation, they are not required.
174844*/
174845#ifndef SQLITE_AMALGAMATION
174846
174847static const unsigned char sqlite3Utf8Trans1[] = {
174848 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
174849 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
174850 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
174851 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
174852 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
174853 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
174854 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
174855 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
174856};
174857
174858#define READ_UTF8(zIn, zTerm, c) \
174859 c = *(zIn++); \
174860 if( c>=0xc0 ){ \
174861 c = sqlite3Utf8Trans1[c-0xc0]; \
174862 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
174863 c = (c<<6) + (0x3f & *(zIn++)); \
174864 } \
174865 if( c<0x80 \
174866 || (c&0xFFFFF800)==0xD800 \
174867 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
174868 }
174869
174870#define WRITE_UTF8(zOut, c) { \
174871 if( c<0x00080 ){ \
174872 *zOut++ = (u8)(c&0xFF); \
174873 } \
174874 else if( c<0x00800 ){ \
174875 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
174876 *zOut++ = 0x80 + (u8)(c & 0x3F); \
174877 } \
174878 else if( c<0x10000 ){ \
174879 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
174880 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
174881 *zOut++ = 0x80 + (u8)(c & 0x3F); \
174882 }else{ \
174883 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
174884 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
174885 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
174886 *zOut++ = 0x80 + (u8)(c & 0x3F); \
174887 } \
174888}
174889
174890#endif /* ifndef SQLITE_AMALGAMATION */
174891
174892typedef struct unicode_tokenizer unicode_tokenizer;
174893typedef struct unicode_cursor unicode_cursor;
174894
174895struct unicode_tokenizer {
174896 sqlite3_tokenizer base;
174897 int bRemoveDiacritic;
174898 int nException;
174899 int *aiException;
174900};
174901
174902struct unicode_cursor {
174903 sqlite3_tokenizer_cursor base;
174904 const unsigned char *aInput; /* Input text being tokenized */
174905 int nInput; /* Size of aInput[] in bytes */
174906 int iOff; /* Current offset within aInput[] */
174907 int iToken; /* Index of next token to be returned */
174908 char *zToken; /* storage for current token */
174909 int nAlloc; /* space allocated at zToken */
174910};
174911
174912
174913/*
174914** Destroy a tokenizer allocated by unicodeCreate().
174915*/
174916static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
174917 if( pTokenizer ){
174918 unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
174919 sqlite3_free(p->aiException);
174920 sqlite3_free(p);
174921 }
174922 return SQLITE_OK;
174923}
174924
174925/*
174926** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
174927** statement has specified that the tokenizer for this table shall consider
174928** all characters in string zIn/nIn to be separators (if bAlnum==0) or
174929** token characters (if bAlnum==1).
174930**
174931** For each codepoint in the zIn/nIn string, this function checks if the
174932** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
174933** If so, no action is taken. Otherwise, the codepoint is added to the
174934** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
174935** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
174936** codepoints in the aiException[] array.
174937**
174938** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
174939** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
174940** It is not possible to change the behavior of the tokenizer with respect
174941** to these codepoints.
174942*/
174943static int unicodeAddExceptions(
174944 unicode_tokenizer *p, /* Tokenizer to add exceptions to */
174945 int bAlnum, /* Replace Isalnum() return value with this */
174946 const char *zIn, /* Array of characters to make exceptions */
174947 int nIn /* Length of z in bytes */
174948){
174949 const unsigned char *z = (const unsigned char *)zIn;
174950 const unsigned char *zTerm = &z[nIn];
174951 unsigned int iCode;
174952 int nEntry = 0;
174953
174954 assert( bAlnum==0 || bAlnum==1 );
174955
174956 while( z<zTerm ){
174957 READ_UTF8(z, zTerm, iCode);
174958 assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 );
174959 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
174960 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
174961 ){
174962 nEntry++;
174963 }
174964 }
174965
174966 if( nEntry ){
174967 int *aNew; /* New aiException[] array */
174968 int nNew; /* Number of valid entries in array aNew[] */
174969
174970 aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
174971 if( aNew==0 ) return SQLITE_NOMEM;
174972 nNew = p->nException;
174973
174974 z = (const unsigned char *)zIn;
174975 while( z<zTerm ){
174976 READ_UTF8(z, zTerm, iCode);
174977 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
174978 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
174979 ){
174980 int i, j;
174981 for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
174982 for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
174983 aNew[i] = (int)iCode;
174984 nNew++;
174985 }
174986 }
174987 p->aiException = aNew;
174988 p->nException = nNew;
174989 }
174990
174991 return SQLITE_OK;
174992}
174993
174994/*
174995** Return true if the p->aiException[] array contains the value iCode.
174996*/
174997static int unicodeIsException(unicode_tokenizer *p, int iCode){
174998 if( p->nException>0 ){
174999 int *a = p->aiException;
175000 int iLo = 0;
175001 int iHi = p->nException-1;
175002
175003 while( iHi>=iLo ){
175004 int iTest = (iHi + iLo) / 2;
175005 if( iCode==a[iTest] ){
175006 return 1;
175007 }else if( iCode>a[iTest] ){
175008 iLo = iTest+1;
175009 }else{
175010 iHi = iTest-1;
175011 }
175012 }
175013 }
175014
175015 return 0;
175016}
175017
175018/*
175019** Return true if, for the purposes of tokenization, codepoint iCode is
175020** considered a token character (not a separator).
175021*/
175022static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
175023 assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
175024 return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
175025}
175026
175027/*
175028** Create a new tokenizer instance.
175029*/
175030static int unicodeCreate(
175031 int nArg, /* Size of array argv[] */
175032 const char * const *azArg, /* Tokenizer creation arguments */
175033 sqlite3_tokenizer **pp /* OUT: New tokenizer handle */
175034){
175035 unicode_tokenizer *pNew; /* New tokenizer object */
175036 int i;
175037 int rc = SQLITE_OK;
175038
175039 pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
175040 if( pNew==NULL ) return SQLITE_NOMEM;
175041 memset(pNew, 0, sizeof(unicode_tokenizer));
175042 pNew->bRemoveDiacritic = 1;
175043
175044 for(i=0; rc==SQLITE_OK && i<nArg; i++){
175045 const char *z = azArg[i];
175046 int n = (int)strlen(z);
175047
175048 if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
175049 pNew->bRemoveDiacritic = 1;
175050 }
175051 else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
175052 pNew->bRemoveDiacritic = 0;
175053 }
175054 else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
175055 rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
175056 }
175057 else if( n>=11 && memcmp("separators=", z, 11)==0 ){
175058 rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
175059 }
175060 else{
175061 /* Unrecognized argument */
175062 rc = SQLITE_ERROR;
175063 }
175064 }
175065
175066 if( rc!=SQLITE_OK ){
175067 unicodeDestroy((sqlite3_tokenizer *)pNew);
175068 pNew = 0;
175069 }
175070 *pp = (sqlite3_tokenizer *)pNew;
175071 return rc;
175072}
175073
175074/*
175075** Prepare to begin tokenizing a particular string. The input
175076** string to be tokenized is pInput[0..nBytes-1]. A cursor
175077** used to incrementally tokenize this string is returned in
175078** *ppCursor.
175079*/
175080static int unicodeOpen(
175081 sqlite3_tokenizer *p, /* The tokenizer */
175082 const char *aInput, /* Input string */
175083 int nInput, /* Size of string aInput in bytes */
175084 sqlite3_tokenizer_cursor **pp /* OUT: New cursor object */
175085){
175086 unicode_cursor *pCsr;
175087
175088 pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
175089 if( pCsr==0 ){
175090 return SQLITE_NOMEM;
175091 }
175092 memset(pCsr, 0, sizeof(unicode_cursor));
175093
175094 pCsr->aInput = (const unsigned char *)aInput;
175095 if( aInput==0 ){
175096 pCsr->nInput = 0;
175097 }else if( nInput<0 ){
175098 pCsr->nInput = (int)strlen(aInput);
175099 }else{
175100 pCsr->nInput = nInput;
175101 }
175102
175103 *pp = &pCsr->base;
175104 UNUSED_PARAMETER(p);
175105 return SQLITE_OK;
175106}
175107
175108/*
175109** Close a tokenization cursor previously opened by a call to
175110** simpleOpen() above.
175111*/
175112static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
175113 unicode_cursor *pCsr = (unicode_cursor *) pCursor;
175114 sqlite3_free(pCsr->zToken);
175115 sqlite3_free(pCsr);
175116 return SQLITE_OK;
175117}
175118
175119/*
175120** Extract the next token from a tokenization cursor. The cursor must
175121** have been opened by a prior call to simpleOpen().
175122*/
175123static int unicodeNext(
175124 sqlite3_tokenizer_cursor *pC, /* Cursor returned by simpleOpen */
175125 const char **paToken, /* OUT: Token text */
175126 int *pnToken, /* OUT: Number of bytes at *paToken */
175127 int *piStart, /* OUT: Starting offset of token */
175128 int *piEnd, /* OUT: Ending offset of token */
175129 int *piPos /* OUT: Position integer of token */
175130){
175131 unicode_cursor *pCsr = (unicode_cursor *)pC;
175132 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
175133 unsigned int iCode = 0;
175134 char *zOut;
175135 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
175136 const unsigned char *zStart = z;
175137 const unsigned char *zEnd;
175138 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
175139
175140 /* Scan past any delimiter characters before the start of the next token.
175141 ** Return SQLITE_DONE early if this takes us all the way to the end of
175142 ** the input. */
175143 while( z<zTerm ){
175144 READ_UTF8(z, zTerm, iCode);
175145 if( unicodeIsAlnum(p, (int)iCode) ) break;
175146 zStart = z;
175147 }
175148 if( zStart>=zTerm ) return SQLITE_DONE;
175149
175150 zOut = pCsr->zToken;
175151 do {
175152 int iOut;
175153
175154 /* Grow the output buffer if required. */
175155 if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
175156 char *zNew = sqlite3_realloc(pCsr->zToken, pCsr->nAlloc+64);
175157 if( !zNew ) return SQLITE_NOMEM;
175158 zOut = &zNew[zOut - pCsr->zToken];
175159 pCsr->zToken = zNew;
175160 pCsr->nAlloc += 64;
175161 }
175162
175163 /* Write the folded case of the last character read to the output */
175164 zEnd = z;
175165 iOut = sqlite3FtsUnicodeFold((int)iCode, p->bRemoveDiacritic);
175166 if( iOut ){
175167 WRITE_UTF8(zOut, iOut);
175168 }
175169
175170 /* If the cursor is not at EOF, read the next character */
175171 if( z>=zTerm ) break;
175172 READ_UTF8(z, zTerm, iCode);
175173 }while( unicodeIsAlnum(p, (int)iCode)
175174 || sqlite3FtsUnicodeIsdiacritic((int)iCode)
175175 );
175176
175177 /* Set the output variables and return. */
175178 pCsr->iOff = (int)(z - pCsr->aInput);
175179 *paToken = pCsr->zToken;
175180 *pnToken = (int)(zOut - pCsr->zToken);
175181 *piStart = (int)(zStart - pCsr->aInput);
175182 *piEnd = (int)(zEnd - pCsr->aInput);
175183 *piPos = pCsr->iToken++;
175184 return SQLITE_OK;
175185}
175186
175187/*
175188** Set *ppModule to a pointer to the sqlite3_tokenizer_module
175189** structure for the unicode tokenizer.
175190*/
175191SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
175192 static const sqlite3_tokenizer_module module = {
175193 0,
175194 unicodeCreate,
175195 unicodeDestroy,
175196 unicodeOpen,
175197 unicodeClose,
175198 unicodeNext,
175199 0,
175200 };
175201 *ppModule = &module;
175202}
175203
175204#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
175205#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */
175206
175207/************** End of fts3_unicode.c ****************************************/
175208/************** Begin file fts3_unicode2.c ***********************************/
175209/*
175210** 2012 May 25
175211**
175212** The author disclaims copyright to this source code. In place of
175213** a legal notice, here is a blessing:
175214**
175215** May you do good and not evil.
175216** May you find forgiveness for yourself and forgive others.
175217** May you share freely, never taking more than you give.
175218**
175219******************************************************************************
175220*/
175221
175222/*
175223** DO NOT EDIT THIS MACHINE GENERATED FILE.
175224*/
175225
175226#ifndef SQLITE_DISABLE_FTS3_UNICODE
175227#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
175228
175229/* #include <assert.h> */
175230
175231/*
175232** Return true if the argument corresponds to a unicode codepoint
175233** classified as either a letter or a number. Otherwise false.
175234**
175235** The results are undefined if the value passed to this function
175236** is less than zero.
175237*/
175238SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
175239 /* Each unsigned integer in the following array corresponds to a contiguous
175240 ** range of unicode codepoints that are not either letters or numbers (i.e.
175241 ** codepoints for which this function should return 0).
175242 **
175243 ** The most significant 22 bits in each 32-bit value contain the first
175244 ** codepoint in the range. The least significant 10 bits are used to store
175245 ** the size of the range (always at least 1). In other words, the value
175246 ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
175247 ** C. It is not possible to represent a range larger than 1023 codepoints
175248 ** using this format.
175249 */
175250 static const unsigned int aEntry[] = {
175251 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
175252 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
175253 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
175254 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
175255 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
175256 0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
175257 0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
175258 0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
175259 0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
175260 0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
175261 0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
175262 0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
175263 0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
175264 0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
175265 0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
175266 0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
175267 0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
175268 0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
175269 0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
175270 0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
175271 0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
175272 0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
175273 0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
175274 0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
175275 0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
175276 0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
175277 0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
175278 0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
175279 0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
175280 0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
175281 0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
175282 0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
175283 0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
175284 0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
175285 0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
175286 0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
175287 0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
175288 0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
175289 0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
175290 0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
175291 0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
175292 0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
175293 0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
175294 0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
175295 0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
175296 0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
175297 0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
175298 0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
175299 0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
175300 0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
175301 0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
175302 0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
175303 0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
175304 0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
175305 0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
175306 0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
175307 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
175308 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
175309 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
175310 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
175311 0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
175312 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
175313 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
175314 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
175315 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
175316 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
175317 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
175318 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
175319 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
175320 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
175321 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
175322 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
175323 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
175324 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
175325 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
175326 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
175327 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
175328 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
175329 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
175330 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
175331 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
175332 0x380400F0,
175333 };
175334 static const unsigned int aAscii[4] = {
175335 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
175336 };
175337
175338 if( (unsigned int)c<128 ){
175339 return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
175340 }else if( (unsigned int)c<(1<<22) ){
175341 unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
175342 int iRes = 0;
175343 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
175344 int iLo = 0;
175345 while( iHi>=iLo ){
175346 int iTest = (iHi + iLo) / 2;
175347 if( key >= aEntry[iTest] ){
175348 iRes = iTest;
175349 iLo = iTest+1;
175350 }else{
175351 iHi = iTest-1;
175352 }
175353 }
175354 assert( aEntry[0]<key );
175355 assert( key>=aEntry[iRes] );
175356 return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
175357 }
175358 return 1;
175359}
175360
175361
175362/*
175363** If the argument is a codepoint corresponding to a lowercase letter
175364** in the ASCII range with a diacritic added, return the codepoint
175365** of the ASCII letter only. For example, if passed 235 - "LATIN
175366** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
175367** E"). The resuls of passing a codepoint that corresponds to an
175368** uppercase letter are undefined.
175369*/
175370static int remove_diacritic(int c){
175371 unsigned short aDia[] = {
175372 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
175373 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
175374 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
175375 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
175376 3456, 3696, 3712, 3728, 3744, 3896, 3912, 3928,
175377 3968, 4008, 4040, 4106, 4138, 4170, 4202, 4234,
175378 4266, 4296, 4312, 4344, 4408, 4424, 4472, 4504,
175379 6148, 6198, 6264, 6280, 6360, 6429, 6505, 6529,
175380 61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726,
175381 61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122,
175382 62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536,
175383 62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730,
175384 62924, 63050, 63082, 63274, 63390,
175385 };
175386 char aChar[] = {
175387 '\0', 'a', 'c', 'e', 'i', 'n', 'o', 'u', 'y', 'y', 'a', 'c',
175388 'd', 'e', 'e', 'g', 'h', 'i', 'j', 'k', 'l', 'n', 'o', 'r',
175389 's', 't', 'u', 'u', 'w', 'y', 'z', 'o', 'u', 'a', 'i', 'o',
175390 'u', 'g', 'k', 'o', 'j', 'g', 'n', 'a', 'e', 'i', 'o', 'r',
175391 'u', 's', 't', 'h', 'a', 'e', 'o', 'y', '\0', '\0', '\0', '\0',
175392 '\0', '\0', '\0', '\0', 'a', 'b', 'd', 'd', 'e', 'f', 'g', 'h',
175393 'h', 'i', 'k', 'l', 'l', 'm', 'n', 'p', 'r', 'r', 's', 't',
175394 'u', 'v', 'w', 'w', 'x', 'y', 'z', 'h', 't', 'w', 'y', 'a',
175395 'e', 'i', 'o', 'u', 'y',
175396 };
175397
175398 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
175399 int iRes = 0;
175400 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
175401 int iLo = 0;
175402 while( iHi>=iLo ){
175403 int iTest = (iHi + iLo) / 2;
175404 if( key >= aDia[iTest] ){
175405 iRes = iTest;
175406 iLo = iTest+1;
175407 }else{
175408 iHi = iTest-1;
175409 }
175410 }
175411 assert( key>=aDia[iRes] );
175412 return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
175413}
175414
175415
175416/*
175417** Return true if the argument interpreted as a unicode codepoint
175418** is a diacritical modifier character.
175419*/
175420SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
175421 unsigned int mask0 = 0x08029FDF;
175422 unsigned int mask1 = 0x000361F8;
175423 if( c<768 || c>817 ) return 0;
175424 return (c < 768+32) ?
175425 (mask0 & (1 << (c-768))) :
175426 (mask1 & (1 << (c-768-32)));
175427}
175428
175429
175430/*
175431** Interpret the argument as a unicode codepoint. If the codepoint
175432** is an upper case character that has a lower case equivalent,
175433** return the codepoint corresponding to the lower case version.
175434** Otherwise, return a copy of the argument.
175435**
175436** The results are undefined if the value passed to this function
175437** is less than zero.
175438*/
175439SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
175440 /* Each entry in the following array defines a rule for folding a range
175441 ** of codepoints to lower case. The rule applies to a range of nRange
175442 ** codepoints starting at codepoint iCode.
175443 **
175444 ** If the least significant bit in flags is clear, then the rule applies
175445 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
175446 ** need to be folded). Or, if it is set, then the rule only applies to
175447 ** every second codepoint in the range, starting with codepoint C.
175448 **
175449 ** The 7 most significant bits in flags are an index into the aiOff[]
175450 ** array. If a specific codepoint C does require folding, then its lower
175451 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
175452 **
175453 ** The contents of this array are generated by parsing the CaseFolding.txt
175454 ** file distributed as part of the "Unicode Character Database". See
175455 ** http://www.unicode.org for details.
175456 */
175457 static const struct TableEntry {
175458 unsigned short iCode;
175459 unsigned char flags;
175460 unsigned char nRange;
175461 } aEntry[] = {
175462 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
175463 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
175464 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
175465 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
175466 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
175467 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
175468 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
175469 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
175470 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
175471 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
175472 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
175473 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
175474 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
175475 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
175476 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
175477 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
175478 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
175479 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
175480 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
175481 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
175482 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
175483 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
175484 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
175485 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
175486 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
175487 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
175488 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
175489 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
175490 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
175491 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
175492 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
175493 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
175494 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
175495 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
175496 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
175497 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
175498 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
175499 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
175500 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
175501 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
175502 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
175503 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
175504 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
175505 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
175506 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
175507 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
175508 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
175509 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
175510 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
175511 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
175512 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
175513 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
175514 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
175515 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
175516 {65313, 14, 26},
175517 };
175518 static const unsigned short aiOff[] = {
175519 1, 2, 8, 15, 16, 26, 28, 32,
175520 37, 38, 40, 48, 63, 64, 69, 71,
175521 79, 80, 116, 202, 203, 205, 206, 207,
175522 209, 210, 211, 213, 214, 217, 218, 219,
175523 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
175524 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
175525 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
175526 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
175527 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
175528 65514, 65521, 65527, 65528, 65529,
175529 };
175530
175531 int ret = c;
175532
175533 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
175534
175535 if( c<128 ){
175536 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
175537 }else if( c<65536 ){
175538 const struct TableEntry *p;
175539 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
175540 int iLo = 0;
175541 int iRes = -1;
175542
175543 assert( c>aEntry[0].iCode );
175544 while( iHi>=iLo ){
175545 int iTest = (iHi + iLo) / 2;
175546 int cmp = (c - aEntry[iTest].iCode);
175547 if( cmp>=0 ){
175548 iRes = iTest;
175549 iLo = iTest+1;
175550 }else{
175551 iHi = iTest-1;
175552 }
175553 }
175554
175555 assert( iRes>=0 && c>=aEntry[iRes].iCode );
175556 p = &aEntry[iRes];
175557 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
175558 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
175559 assert( ret>0 );
175560 }
175561
175562 if( bRemoveDiacritic ) ret = remove_diacritic(ret);
175563 }
175564
175565 else if( c>=66560 && c<66600 ){
175566 ret = c + 40;
175567 }
175568
175569 return ret;
175570}
175571#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
175572#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
175573
175574/************** End of fts3_unicode2.c ***************************************/
175575/************** Begin file json1.c *******************************************/
175576/*
175577** 2015-08-12
175578**
175579** The author disclaims copyright to this source code. In place of
175580** a legal notice, here is a blessing:
175581**
175582** May you do good and not evil.
175583** May you find forgiveness for yourself and forgive others.
175584** May you share freely, never taking more than you give.
175585**
175586******************************************************************************
175587**
175588** This SQLite extension implements JSON functions. The interface is
175589** modeled after MySQL JSON functions:
175590**
175591** https://dev.mysql.com/doc/refman/5.7/en/json.html
175592**
175593** For the time being, all JSON is stored as pure text. (We might add
175594** a JSONB type in the future which stores a binary encoding of JSON in
175595** a BLOB, but there is no support for JSONB in the current implementation.
175596** This implementation parses JSON text at 250 MB/s, so it is hard to see
175597** how JSONB might improve on that.)
175598*/
175599#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
175600#if !defined(SQLITEINT_H)
175601/* #include "sqlite3ext.h" */
175602#endif
175603SQLITE_EXTENSION_INIT1
175604/* #include <assert.h> */
175605/* #include <string.h> */
175606/* #include <stdlib.h> */
175607/* #include <stdarg.h> */
175608
175609/* Mark a function parameter as unused, to suppress nuisance compiler
175610** warnings. */
175611#ifndef UNUSED_PARAM
175612# define UNUSED_PARAM(X) (void)(X)
175613#endif
175614
175615#ifndef LARGEST_INT64
175616# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
175617# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
175618#endif
175619
175620/*
175621** Versions of isspace(), isalnum() and isdigit() to which it is safe
175622** to pass signed char values.
175623*/
175624#ifdef sqlite3Isdigit
175625 /* Use the SQLite core versions if this routine is part of the
175626 ** SQLite amalgamation */
175627# define safe_isdigit(x) sqlite3Isdigit(x)
175628# define safe_isalnum(x) sqlite3Isalnum(x)
175629# define safe_isxdigit(x) sqlite3Isxdigit(x)
175630#else
175631 /* Use the standard library for separate compilation */
175632#include <ctype.h> /* amalgamator: keep */
175633# define safe_isdigit(x) isdigit((unsigned char)(x))
175634# define safe_isalnum(x) isalnum((unsigned char)(x))
175635# define safe_isxdigit(x) isxdigit((unsigned char)(x))
175636#endif
175637
175638/*
175639** Growing our own isspace() routine this way is twice as fast as
175640** the library isspace() function, resulting in a 7% overall performance
175641** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
175642*/
175643static const char jsonIsSpace[] = {
175644 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
175645 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175646 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175647 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175648 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175649 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175650 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175651 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175652 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175653 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175654 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175655 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175656 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175657 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175658 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175659 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175660};
175661#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
175662
175663#ifndef SQLITE_AMALGAMATION
175664 /* Unsigned integer types. These are already defined in the sqliteInt.h,
175665 ** but the definitions need to be repeated for separate compilation. */
175666 typedef sqlite3_uint64 u64;
175667 typedef unsigned int u32;
175668 typedef unsigned short int u16;
175669 typedef unsigned char u8;
175670#endif
175671
175672/* Objects */
175673typedef struct JsonString JsonString;
175674typedef struct JsonNode JsonNode;
175675typedef struct JsonParse JsonParse;
175676
175677/* An instance of this object represents a JSON string
175678** under construction. Really, this is a generic string accumulator
175679** that can be and is used to create strings other than JSON.
175680*/
175681struct JsonString {
175682 sqlite3_context *pCtx; /* Function context - put error messages here */
175683 char *zBuf; /* Append JSON content here */
175684 u64 nAlloc; /* Bytes of storage available in zBuf[] */
175685 u64 nUsed; /* Bytes of zBuf[] currently used */
175686 u8 bStatic; /* True if zBuf is static space */
175687 u8 bErr; /* True if an error has been encountered */
175688 char zSpace[100]; /* Initial static space */
175689};
175690
175691/* JSON type values
175692*/
175693#define JSON_NULL 0
175694#define JSON_TRUE 1
175695#define JSON_FALSE 2
175696#define JSON_INT 3
175697#define JSON_REAL 4
175698#define JSON_STRING 5
175699#define JSON_ARRAY 6
175700#define JSON_OBJECT 7
175701
175702/* The "subtype" set for JSON values */
175703#define JSON_SUBTYPE 74 /* Ascii for "J" */
175704
175705/*
175706** Names of the various JSON types:
175707*/
175708static const char * const jsonType[] = {
175709 "null", "true", "false", "integer", "real", "text", "array", "object"
175710};
175711
175712/* Bit values for the JsonNode.jnFlag field
175713*/
175714#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
175715#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
175716#define JNODE_REMOVE 0x04 /* Do not output */
175717#define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
175718#define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
175719#define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
175720#define JNODE_LABEL 0x40 /* Is a label of an object */
175721
175722
175723/* A single node of parsed JSON
175724*/
175725struct JsonNode {
175726 u8 eType; /* One of the JSON_ type values */
175727 u8 jnFlags; /* JNODE flags */
175728 u32 n; /* Bytes of content, or number of sub-nodes */
175729 union {
175730 const char *zJContent; /* Content for INT, REAL, and STRING */
175731 u32 iAppend; /* More terms for ARRAY and OBJECT */
175732 u32 iKey; /* Key for ARRAY objects in json_tree() */
175733 u32 iReplace; /* Replacement content for JNODE_REPLACE */
175734 JsonNode *pPatch; /* Node chain of patch for JNODE_PATCH */
175735 } u;
175736};
175737
175738/* A completely parsed JSON string
175739*/
175740struct JsonParse {
175741 u32 nNode; /* Number of slots of aNode[] used */
175742 u32 nAlloc; /* Number of slots of aNode[] allocated */
175743 JsonNode *aNode; /* Array of nodes containing the parse */
175744 const char *zJson; /* Original JSON string */
175745 u32 *aUp; /* Index of parent of each node */
175746 u8 oom; /* Set to true if out of memory */
175747 u8 nErr; /* Number of errors seen */
175748 u16 iDepth; /* Nesting depth */
175749 int nJson; /* Length of the zJson string in bytes */
175750 u32 iHold; /* Replace cache line with the lowest iHold value */
175751};
175752
175753/*
175754** Maximum nesting depth of JSON for this implementation.
175755**
175756** This limit is needed to avoid a stack overflow in the recursive
175757** descent parser. A depth of 2000 is far deeper than any sane JSON
175758** should go.
175759*/
175760#define JSON_MAX_DEPTH 2000
175761
175762/**************************************************************************
175763** Utility routines for dealing with JsonString objects
175764**************************************************************************/
175765
175766/* Set the JsonString object to an empty string
175767*/
175768static void jsonZero(JsonString *p){
175769 p->zBuf = p->zSpace;
175770 p->nAlloc = sizeof(p->zSpace);
175771 p->nUsed = 0;
175772 p->bStatic = 1;
175773}
175774
175775/* Initialize the JsonString object
175776*/
175777static void jsonInit(JsonString *p, sqlite3_context *pCtx){
175778 p->pCtx = pCtx;
175779 p->bErr = 0;
175780 jsonZero(p);
175781}
175782
175783
175784/* Free all allocated memory and reset the JsonString object back to its
175785** initial state.
175786*/
175787static void jsonReset(JsonString *p){
175788 if( !p->bStatic ) sqlite3_free(p->zBuf);
175789 jsonZero(p);
175790}
175791
175792
175793/* Report an out-of-memory (OOM) condition
175794*/
175795static void jsonOom(JsonString *p){
175796 p->bErr = 1;
175797 sqlite3_result_error_nomem(p->pCtx);
175798 jsonReset(p);
175799}
175800
175801/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
175802** Return zero on success. Return non-zero on an OOM error
175803*/
175804static int jsonGrow(JsonString *p, u32 N){
175805 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
175806 char *zNew;
175807 if( p->bStatic ){
175808 if( p->bErr ) return 1;
175809 zNew = sqlite3_malloc64(nTotal);
175810 if( zNew==0 ){
175811 jsonOom(p);
175812 return SQLITE_NOMEM;
175813 }
175814 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
175815 p->zBuf = zNew;
175816 p->bStatic = 0;
175817 }else{
175818 zNew = sqlite3_realloc64(p->zBuf, nTotal);
175819 if( zNew==0 ){
175820 jsonOom(p);
175821 return SQLITE_NOMEM;
175822 }
175823 p->zBuf = zNew;
175824 }
175825 p->nAlloc = nTotal;
175826 return SQLITE_OK;
175827}
175828
175829/* Append N bytes from zIn onto the end of the JsonString string.
175830*/
175831static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
175832 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
175833 memcpy(p->zBuf+p->nUsed, zIn, N);
175834 p->nUsed += N;
175835}
175836
175837/* Append formatted text (not to exceed N bytes) to the JsonString.
175838*/
175839static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
175840 va_list ap;
175841 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
175842 va_start(ap, zFormat);
175843 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
175844 va_end(ap);
175845 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
175846}
175847
175848/* Append a single character
175849*/
175850static void jsonAppendChar(JsonString *p, char c){
175851 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
175852 p->zBuf[p->nUsed++] = c;
175853}
175854
175855/* Append a comma separator to the output buffer, if the previous
175856** character is not '[' or '{'.
175857*/
175858static void jsonAppendSeparator(JsonString *p){
175859 char c;
175860 if( p->nUsed==0 ) return;
175861 c = p->zBuf[p->nUsed-1];
175862 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
175863}
175864
175865/* Append the N-byte string in zIn to the end of the JsonString string
175866** under construction. Enclose the string in "..." and escape
175867** any double-quotes or backslash characters contained within the
175868** string.
175869*/
175870static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
175871 u32 i;
175872 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
175873 p->zBuf[p->nUsed++] = '"';
175874 for(i=0; i<N; i++){
175875 unsigned char c = ((unsigned const char*)zIn)[i];
175876 if( c=='"' || c=='\\' ){
175877 json_simple_escape:
175878 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
175879 p->zBuf[p->nUsed++] = '\\';
175880 }else if( c<=0x1f ){
175881 static const char aSpecial[] = {
175882 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
175883 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
175884 };
175885 assert( sizeof(aSpecial)==32 );
175886 assert( aSpecial['\b']=='b' );
175887 assert( aSpecial['\f']=='f' );
175888 assert( aSpecial['\n']=='n' );
175889 assert( aSpecial['\r']=='r' );
175890 assert( aSpecial['\t']=='t' );
175891 if( aSpecial[c] ){
175892 c = aSpecial[c];
175893 goto json_simple_escape;
175894 }
175895 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
175896 p->zBuf[p->nUsed++] = '\\';
175897 p->zBuf[p->nUsed++] = 'u';
175898 p->zBuf[p->nUsed++] = '0';
175899 p->zBuf[p->nUsed++] = '0';
175900 p->zBuf[p->nUsed++] = '0' + (c>>4);
175901 c = "0123456789abcdef"[c&0xf];
175902 }
175903 p->zBuf[p->nUsed++] = c;
175904 }
175905 p->zBuf[p->nUsed++] = '"';
175906 assert( p->nUsed<p->nAlloc );
175907}
175908
175909/*
175910** Append a function parameter value to the JSON string under
175911** construction.
175912*/
175913static void jsonAppendValue(
175914 JsonString *p, /* Append to this JSON string */
175915 sqlite3_value *pValue /* Value to append */
175916){
175917 switch( sqlite3_value_type(pValue) ){
175918 case SQLITE_NULL: {
175919 jsonAppendRaw(p, "null", 4);
175920 break;
175921 }
175922 case SQLITE_INTEGER:
175923 case SQLITE_FLOAT: {
175924 const char *z = (const char*)sqlite3_value_text(pValue);
175925 u32 n = (u32)sqlite3_value_bytes(pValue);
175926 jsonAppendRaw(p, z, n);
175927 break;
175928 }
175929 case SQLITE_TEXT: {
175930 const char *z = (const char*)sqlite3_value_text(pValue);
175931 u32 n = (u32)sqlite3_value_bytes(pValue);
175932 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
175933 jsonAppendRaw(p, z, n);
175934 }else{
175935 jsonAppendString(p, z, n);
175936 }
175937 break;
175938 }
175939 default: {
175940 if( p->bErr==0 ){
175941 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
175942 p->bErr = 2;
175943 jsonReset(p);
175944 }
175945 break;
175946 }
175947 }
175948}
175949
175950
175951/* Make the JSON in p the result of the SQL function.
175952*/
175953static void jsonResult(JsonString *p){
175954 if( p->bErr==0 ){
175955 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
175956 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
175957 SQLITE_UTF8);
175958 jsonZero(p);
175959 }
175960 assert( p->bStatic );
175961}
175962
175963/**************************************************************************
175964** Utility routines for dealing with JsonNode and JsonParse objects
175965**************************************************************************/
175966
175967/*
175968** Return the number of consecutive JsonNode slots need to represent
175969** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
175970** OBJECT types, the number might be larger.
175971**
175972** Appended elements are not counted. The value returned is the number
175973** by which the JsonNode counter should increment in order to go to the
175974** next peer value.
175975*/
175976static u32 jsonNodeSize(JsonNode *pNode){
175977 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
175978}
175979
175980/*
175981** Reclaim all memory allocated by a JsonParse object. But do not
175982** delete the JsonParse object itself.
175983*/
175984static void jsonParseReset(JsonParse *pParse){
175985 sqlite3_free(pParse->aNode);
175986 pParse->aNode = 0;
175987 pParse->nNode = 0;
175988 pParse->nAlloc = 0;
175989 sqlite3_free(pParse->aUp);
175990 pParse->aUp = 0;
175991}
175992
175993/*
175994** Free a JsonParse object that was obtained from sqlite3_malloc().
175995*/
175996static void jsonParseFree(JsonParse *pParse){
175997 jsonParseReset(pParse);
175998 sqlite3_free(pParse);
175999}
176000
176001/*
176002** Convert the JsonNode pNode into a pure JSON string and
176003** append to pOut. Subsubstructure is also included. Return
176004** the number of JsonNode objects that are encoded.
176005*/
176006static void jsonRenderNode(
176007 JsonNode *pNode, /* The node to render */
176008 JsonString *pOut, /* Write JSON here */
176009 sqlite3_value **aReplace /* Replacement values */
176010){
176011 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
176012 if( pNode->jnFlags & JNODE_REPLACE ){
176013 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
176014 return;
176015 }
176016 pNode = pNode->u.pPatch;
176017 }
176018 switch( pNode->eType ){
176019 default: {
176020 assert( pNode->eType==JSON_NULL );
176021 jsonAppendRaw(pOut, "null", 4);
176022 break;
176023 }
176024 case JSON_TRUE: {
176025 jsonAppendRaw(pOut, "true", 4);
176026 break;
176027 }
176028 case JSON_FALSE: {
176029 jsonAppendRaw(pOut, "false", 5);
176030 break;
176031 }
176032 case JSON_STRING: {
176033 if( pNode->jnFlags & JNODE_RAW ){
176034 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
176035 break;
176036 }
176037 /* Fall through into the next case */
176038 }
176039 case JSON_REAL:
176040 case JSON_INT: {
176041 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
176042 break;
176043 }
176044 case JSON_ARRAY: {
176045 u32 j = 1;
176046 jsonAppendChar(pOut, '[');
176047 for(;;){
176048 while( j<=pNode->n ){
176049 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
176050 jsonAppendSeparator(pOut);
176051 jsonRenderNode(&pNode[j], pOut, aReplace);
176052 }
176053 j += jsonNodeSize(&pNode[j]);
176054 }
176055 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
176056 pNode = &pNode[pNode->u.iAppend];
176057 j = 1;
176058 }
176059 jsonAppendChar(pOut, ']');
176060 break;
176061 }
176062 case JSON_OBJECT: {
176063 u32 j = 1;
176064 jsonAppendChar(pOut, '{');
176065 for(;;){
176066 while( j<=pNode->n ){
176067 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
176068 jsonAppendSeparator(pOut);
176069 jsonRenderNode(&pNode[j], pOut, aReplace);
176070 jsonAppendChar(pOut, ':');
176071 jsonRenderNode(&pNode[j+1], pOut, aReplace);
176072 }
176073 j += 1 + jsonNodeSize(&pNode[j+1]);
176074 }
176075 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
176076 pNode = &pNode[pNode->u.iAppend];
176077 j = 1;
176078 }
176079 jsonAppendChar(pOut, '}');
176080 break;
176081 }
176082 }
176083}
176084
176085/*
176086** Return a JsonNode and all its descendents as a JSON string.
176087*/
176088static void jsonReturnJson(
176089 JsonNode *pNode, /* Node to return */
176090 sqlite3_context *pCtx, /* Return value for this function */
176091 sqlite3_value **aReplace /* Array of replacement values */
176092){
176093 JsonString s;
176094 jsonInit(&s, pCtx);
176095 jsonRenderNode(pNode, &s, aReplace);
176096 jsonResult(&s);
176097 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
176098}
176099
176100/*
176101** Make the JsonNode the return value of the function.
176102*/
176103static void jsonReturn(
176104 JsonNode *pNode, /* Node to return */
176105 sqlite3_context *pCtx, /* Return value for this function */
176106 sqlite3_value **aReplace /* Array of replacement values */
176107){
176108 switch( pNode->eType ){
176109 default: {
176110 assert( pNode->eType==JSON_NULL );
176111 sqlite3_result_null(pCtx);
176112 break;
176113 }
176114 case JSON_TRUE: {
176115 sqlite3_result_int(pCtx, 1);
176116 break;
176117 }
176118 case JSON_FALSE: {
176119 sqlite3_result_int(pCtx, 0);
176120 break;
176121 }
176122 case JSON_INT: {
176123 sqlite3_int64 i = 0;
176124 const char *z = pNode->u.zJContent;
176125 if( z[0]=='-' ){ z++; }
176126 while( z[0]>='0' && z[0]<='9' ){
176127 unsigned v = *(z++) - '0';
176128 if( i>=LARGEST_INT64/10 ){
176129 if( i>LARGEST_INT64/10 ) goto int_as_real;
176130 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
176131 if( v==9 ) goto int_as_real;
176132 if( v==8 ){
176133 if( pNode->u.zJContent[0]=='-' ){
176134 sqlite3_result_int64(pCtx, SMALLEST_INT64);
176135 goto int_done;
176136 }else{
176137 goto int_as_real;
176138 }
176139 }
176140 }
176141 i = i*10 + v;
176142 }
176143 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
176144 sqlite3_result_int64(pCtx, i);
176145 int_done:
176146 break;
176147 int_as_real: /* fall through to real */;
176148 }
176149 case JSON_REAL: {
176150 double r;
176151#ifdef SQLITE_AMALGAMATION
176152 const char *z = pNode->u.zJContent;
176153 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
176154#else
176155 r = strtod(pNode->u.zJContent, 0);
176156#endif
176157 sqlite3_result_double(pCtx, r);
176158 break;
176159 }
176160 case JSON_STRING: {
176161#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
176162 ** json_insert() and json_replace() and those routines do not
176163 ** call jsonReturn() */
176164 if( pNode->jnFlags & JNODE_RAW ){
176165 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
176166 SQLITE_TRANSIENT);
176167 }else
176168#endif
176169 assert( (pNode->jnFlags & JNODE_RAW)==0 );
176170 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
176171 /* JSON formatted without any backslash-escapes */
176172 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
176173 SQLITE_TRANSIENT);
176174 }else{
176175 /* Translate JSON formatted string into raw text */
176176 u32 i;
176177 u32 n = pNode->n;
176178 const char *z = pNode->u.zJContent;
176179 char *zOut;
176180 u32 j;
176181 zOut = sqlite3_malloc( n+1 );
176182 if( zOut==0 ){
176183 sqlite3_result_error_nomem(pCtx);
176184 break;
176185 }
176186 for(i=1, j=0; i<n-1; i++){
176187 char c = z[i];
176188 if( c!='\\' ){
176189 zOut[j++] = c;
176190 }else{
176191 c = z[++i];
176192 if( c=='u' ){
176193 u32 v = 0, k;
176194 for(k=0; k<4; i++, k++){
176195 assert( i<n-2 );
176196 c = z[i+1];
176197 assert( safe_isxdigit(c) );
176198 if( c<='9' ) v = v*16 + c - '0';
176199 else if( c<='F' ) v = v*16 + c - 'A' + 10;
176200 else v = v*16 + c - 'a' + 10;
176201 }
176202 if( v==0 ) break;
176203 if( v<=0x7f ){
176204 zOut[j++] = (char)v;
176205 }else if( v<=0x7ff ){
176206 zOut[j++] = (char)(0xc0 | (v>>6));
176207 zOut[j++] = 0x80 | (v&0x3f);
176208 }else{
176209 zOut[j++] = (char)(0xe0 | (v>>12));
176210 zOut[j++] = 0x80 | ((v>>6)&0x3f);
176211 zOut[j++] = 0x80 | (v&0x3f);
176212 }
176213 }else{
176214 if( c=='b' ){
176215 c = '\b';
176216 }else if( c=='f' ){
176217 c = '\f';
176218 }else if( c=='n' ){
176219 c = '\n';
176220 }else if( c=='r' ){
176221 c = '\r';
176222 }else if( c=='t' ){
176223 c = '\t';
176224 }
176225 zOut[j++] = c;
176226 }
176227 }
176228 }
176229 zOut[j] = 0;
176230 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
176231 }
176232 break;
176233 }
176234 case JSON_ARRAY:
176235 case JSON_OBJECT: {
176236 jsonReturnJson(pNode, pCtx, aReplace);
176237 break;
176238 }
176239 }
176240}
176241
176242/* Forward reference */
176243static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
176244
176245/*
176246** A macro to hint to the compiler that a function should not be
176247** inlined.
176248*/
176249#if defined(__GNUC__)
176250# define JSON_NOINLINE __attribute__((noinline))
176251#elif defined(_MSC_VER) && _MSC_VER>=1310
176252# define JSON_NOINLINE __declspec(noinline)
176253#else
176254# define JSON_NOINLINE
176255#endif
176256
176257
176258static JSON_NOINLINE int jsonParseAddNodeExpand(
176259 JsonParse *pParse, /* Append the node to this object */
176260 u32 eType, /* Node type */
176261 u32 n, /* Content size or sub-node count */
176262 const char *zContent /* Content */
176263){
176264 u32 nNew;
176265 JsonNode *pNew;
176266 assert( pParse->nNode>=pParse->nAlloc );
176267 if( pParse->oom ) return -1;
176268 nNew = pParse->nAlloc*2 + 10;
176269 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
176270 if( pNew==0 ){
176271 pParse->oom = 1;
176272 return -1;
176273 }
176274 pParse->nAlloc = nNew;
176275 pParse->aNode = pNew;
176276 assert( pParse->nNode<pParse->nAlloc );
176277 return jsonParseAddNode(pParse, eType, n, zContent);
176278}
176279
176280/*
176281** Create a new JsonNode instance based on the arguments and append that
176282** instance to the JsonParse. Return the index in pParse->aNode[] of the
176283** new node, or -1 if a memory allocation fails.
176284*/
176285static int jsonParseAddNode(
176286 JsonParse *pParse, /* Append the node to this object */
176287 u32 eType, /* Node type */
176288 u32 n, /* Content size or sub-node count */
176289 const char *zContent /* Content */
176290){
176291 JsonNode *p;
176292 if( pParse->nNode>=pParse->nAlloc ){
176293 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
176294 }
176295 p = &pParse->aNode[pParse->nNode];
176296 p->eType = (u8)eType;
176297 p->jnFlags = 0;
176298 p->n = n;
176299 p->u.zJContent = zContent;
176300 return pParse->nNode++;
176301}
176302
176303/*
176304** Return true if z[] begins with 4 (or more) hexadecimal digits
176305*/
176306static int jsonIs4Hex(const char *z){
176307 int i;
176308 for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
176309 return 1;
176310}
176311
176312/*
176313** Parse a single JSON value which begins at pParse->zJson[i]. Return the
176314** index of the first character past the end of the value parsed.
176315**
176316** Return negative for a syntax error. Special cases: return -2 if the
176317** first non-whitespace character is '}' and return -3 if the first
176318** non-whitespace character is ']'.
176319*/
176320static int jsonParseValue(JsonParse *pParse, u32 i){
176321 char c;
176322 u32 j;
176323 int iThis;
176324 int x;
176325 JsonNode *pNode;
176326 const char *z = pParse->zJson;
176327 while( safe_isspace(z[i]) ){ i++; }
176328 if( (c = z[i])=='{' ){
176329 /* Parse object */
176330 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
176331 if( iThis<0 ) return -1;
176332 for(j=i+1;;j++){
176333 while( safe_isspace(z[j]) ){ j++; }
176334 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
176335 x = jsonParseValue(pParse, j);
176336 if( x<0 ){
176337 pParse->iDepth--;
176338 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
176339 return -1;
176340 }
176341 if( pParse->oom ) return -1;
176342 pNode = &pParse->aNode[pParse->nNode-1];
176343 if( pNode->eType!=JSON_STRING ) return -1;
176344 pNode->jnFlags |= JNODE_LABEL;
176345 j = x;
176346 while( safe_isspace(z[j]) ){ j++; }
176347 if( z[j]!=':' ) return -1;
176348 j++;
176349 x = jsonParseValue(pParse, j);
176350 pParse->iDepth--;
176351 if( x<0 ) return -1;
176352 j = x;
176353 while( safe_isspace(z[j]) ){ j++; }
176354 c = z[j];
176355 if( c==',' ) continue;
176356 if( c!='}' ) return -1;
176357 break;
176358 }
176359 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
176360 return j+1;
176361 }else if( c=='[' ){
176362 /* Parse array */
176363 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
176364 if( iThis<0 ) return -1;
176365 for(j=i+1;;j++){
176366 while( safe_isspace(z[j]) ){ j++; }
176367 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
176368 x = jsonParseValue(pParse, j);
176369 pParse->iDepth--;
176370 if( x<0 ){
176371 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
176372 return -1;
176373 }
176374 j = x;
176375 while( safe_isspace(z[j]) ){ j++; }
176376 c = z[j];
176377 if( c==',' ) continue;
176378 if( c!=']' ) return -1;
176379 break;
176380 }
176381 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
176382 return j+1;
176383 }else if( c=='"' ){
176384 /* Parse string */
176385 u8 jnFlags = 0;
176386 j = i+1;
176387 for(;;){
176388 c = z[j];
176389 if( (c & ~0x1f)==0 ){
176390 /* Control characters are not allowed in strings */
176391 return -1;
176392 }
176393 if( c=='\\' ){
176394 c = z[++j];
176395 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
176396 || c=='n' || c=='r' || c=='t'
176397 || (c=='u' && jsonIs4Hex(z+j+1)) ){
176398 jnFlags = JNODE_ESCAPE;
176399 }else{
176400 return -1;
176401 }
176402 }else if( c=='"' ){
176403 break;
176404 }
176405 j++;
176406 }
176407 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]);
176408 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
176409 return j+1;
176410 }else if( c=='n'
176411 && strncmp(z+i,"null",4)==0
176412 && !safe_isalnum(z[i+4]) ){
176413 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
176414 return i+4;
176415 }else if( c=='t'
176416 && strncmp(z+i,"true",4)==0
176417 && !safe_isalnum(z[i+4]) ){
176418 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
176419 return i+4;
176420 }else if( c=='f'
176421 && strncmp(z+i,"false",5)==0
176422 && !safe_isalnum(z[i+5]) ){
176423 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
176424 return i+5;
176425 }else if( c=='-' || (c>='0' && c<='9') ){
176426 /* Parse number */
176427 u8 seenDP = 0;
176428 u8 seenE = 0;
176429 assert( '-' < '0' );
176430 if( c<='0' ){
176431 j = c=='-' ? i+1 : i;
176432 if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
176433 }
176434 j = i+1;
176435 for(;; j++){
176436 c = z[j];
176437 if( c>='0' && c<='9' ) continue;
176438 if( c=='.' ){
176439 if( z[j-1]=='-' ) return -1;
176440 if( seenDP ) return -1;
176441 seenDP = 1;
176442 continue;
176443 }
176444 if( c=='e' || c=='E' ){
176445 if( z[j-1]<'0' ) return -1;
176446 if( seenE ) return -1;
176447 seenDP = seenE = 1;
176448 c = z[j+1];
176449 if( c=='+' || c=='-' ){
176450 j++;
176451 c = z[j+1];
176452 }
176453 if( c<'0' || c>'9' ) return -1;
176454 continue;
176455 }
176456 break;
176457 }
176458 if( z[j-1]<'0' ) return -1;
176459 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
176460 j - i, &z[i]);
176461 return j;
176462 }else if( c=='}' ){
176463 return -2; /* End of {...} */
176464 }else if( c==']' ){
176465 return -3; /* End of [...] */
176466 }else if( c==0 ){
176467 return 0; /* End of file */
176468 }else{
176469 return -1; /* Syntax error */
176470 }
176471}
176472
176473/*
176474** Parse a complete JSON string. Return 0 on success or non-zero if there
176475** are any errors. If an error occurs, free all memory associated with
176476** pParse.
176477**
176478** pParse is uninitialized when this routine is called.
176479*/
176480static int jsonParse(
176481 JsonParse *pParse, /* Initialize and fill this JsonParse object */
176482 sqlite3_context *pCtx, /* Report errors here */
176483 const char *zJson /* Input JSON text to be parsed */
176484){
176485 int i;
176486 memset(pParse, 0, sizeof(*pParse));
176487 if( zJson==0 ) return 1;
176488 pParse->zJson = zJson;
176489 i = jsonParseValue(pParse, 0);
176490 if( pParse->oom ) i = -1;
176491 if( i>0 ){
176492 assert( pParse->iDepth==0 );
176493 while( safe_isspace(zJson[i]) ) i++;
176494 if( zJson[i] ) i = -1;
176495 }
176496 if( i<=0 ){
176497 if( pCtx!=0 ){
176498 if( pParse->oom ){
176499 sqlite3_result_error_nomem(pCtx);
176500 }else{
176501 sqlite3_result_error(pCtx, "malformed JSON", -1);
176502 }
176503 }
176504 jsonParseReset(pParse);
176505 return 1;
176506 }
176507 return 0;
176508}
176509
176510/* Mark node i of pParse as being a child of iParent. Call recursively
176511** to fill in all the descendants of node i.
176512*/
176513static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
176514 JsonNode *pNode = &pParse->aNode[i];
176515 u32 j;
176516 pParse->aUp[i] = iParent;
176517 switch( pNode->eType ){
176518 case JSON_ARRAY: {
176519 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
176520 jsonParseFillInParentage(pParse, i+j, i);
176521 }
176522 break;
176523 }
176524 case JSON_OBJECT: {
176525 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
176526 pParse->aUp[i+j] = i;
176527 jsonParseFillInParentage(pParse, i+j+1, i);
176528 }
176529 break;
176530 }
176531 default: {
176532 break;
176533 }
176534 }
176535}
176536
176537/*
176538** Compute the parentage of all nodes in a completed parse.
176539*/
176540static int jsonParseFindParents(JsonParse *pParse){
176541 u32 *aUp;
176542 assert( pParse->aUp==0 );
176543 aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode );
176544 if( aUp==0 ){
176545 pParse->oom = 1;
176546 return SQLITE_NOMEM;
176547 }
176548 jsonParseFillInParentage(pParse, 0, 0);
176549 return SQLITE_OK;
176550}
176551
176552/*
176553** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
176554*/
176555#define JSON_CACHE_ID (-429938) /* First cache entry */
176556#define JSON_CACHE_SZ 4 /* Max number of cache entries */
176557
176558/*
176559** Obtain a complete parse of the JSON found in the first argument
176560** of the argv array. Use the sqlite3_get_auxdata() cache for this
176561** parse if it is available. If the cache is not available or if it
176562** is no longer valid, parse the JSON again and return the new parse,
176563** and also register the new parse so that it will be available for
176564** future sqlite3_get_auxdata() calls.
176565*/
176566static JsonParse *jsonParseCached(
176567 sqlite3_context *pCtx,
176568 sqlite3_value **argv,
176569 sqlite3_context *pErrCtx
176570){
176571 const char *zJson = (const char*)sqlite3_value_text(argv[0]);
176572 int nJson = sqlite3_value_bytes(argv[0]);
176573 JsonParse *p;
176574 JsonParse *pMatch = 0;
176575 int iKey;
176576 int iMinKey = 0;
176577 u32 iMinHold = 0xffffffff;
176578 u32 iMaxHold = 0;
176579 if( zJson==0 ) return 0;
176580 for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
176581 p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
176582 if( p==0 ){
176583 iMinKey = iKey;
176584 break;
176585 }
176586 if( pMatch==0
176587 && p->nJson==nJson
176588 && memcmp(p->zJson,zJson,nJson)==0
176589 ){
176590 p->nErr = 0;
176591 pMatch = p;
176592 }else if( p->iHold<iMinHold ){
176593 iMinHold = p->iHold;
176594 iMinKey = iKey;
176595 }
176596 if( p->iHold>iMaxHold ){
176597 iMaxHold = p->iHold;
176598 }
176599 }
176600 if( pMatch ){
176601 pMatch->nErr = 0;
176602 pMatch->iHold = iMaxHold+1;
176603 return pMatch;
176604 }
176605 p = sqlite3_malloc( sizeof(*p) + nJson + 1 );
176606 if( p==0 ){
176607 sqlite3_result_error_nomem(pCtx);
176608 return 0;
176609 }
176610 memset(p, 0, sizeof(*p));
176611 p->zJson = (char*)&p[1];
176612 memcpy((char*)p->zJson, zJson, nJson+1);
176613 if( jsonParse(p, pErrCtx, p->zJson) ){
176614 sqlite3_free(p);
176615 return 0;
176616 }
176617 p->nJson = nJson;
176618 p->iHold = iMaxHold+1;
176619 sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
176620 (void(*)(void*))jsonParseFree);
176621 return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
176622}
176623
176624/*
176625** Compare the OBJECT label at pNode against zKey,nKey. Return true on
176626** a match.
176627*/
176628static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
176629 if( pNode->jnFlags & JNODE_RAW ){
176630 if( pNode->n!=nKey ) return 0;
176631 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
176632 }else{
176633 if( pNode->n!=nKey+2 ) return 0;
176634 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
176635 }
176636}
176637
176638/* forward declaration */
176639static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
176640
176641/*
176642** Search along zPath to find the node specified. Return a pointer
176643** to that node, or NULL if zPath is malformed or if there is no such
176644** node.
176645**
176646** If pApnd!=0, then try to append new nodes to complete zPath if it is
176647** possible to do so and if no existing node corresponds to zPath. If
176648** new nodes are appended *pApnd is set to 1.
176649*/
176650static JsonNode *jsonLookupStep(
176651 JsonParse *pParse, /* The JSON to search */
176652 u32 iRoot, /* Begin the search at this node */
176653 const char *zPath, /* The path to search */
176654 int *pApnd, /* Append nodes to complete path if not NULL */
176655 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
176656){
176657 u32 i, j, nKey;
176658 const char *zKey;
176659 JsonNode *pRoot = &pParse->aNode[iRoot];
176660 if( zPath[0]==0 ) return pRoot;
176661 if( zPath[0]=='.' ){
176662 if( pRoot->eType!=JSON_OBJECT ) return 0;
176663 zPath++;
176664 if( zPath[0]=='"' ){
176665 zKey = zPath + 1;
176666 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
176667 nKey = i-1;
176668 if( zPath[i] ){
176669 i++;
176670 }else{
176671 *pzErr = zPath;
176672 return 0;
176673 }
176674 }else{
176675 zKey = zPath;
176676 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
176677 nKey = i;
176678 }
176679 if( nKey==0 ){
176680 *pzErr = zPath;
176681 return 0;
176682 }
176683 j = 1;
176684 for(;;){
176685 while( j<=pRoot->n ){
176686 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
176687 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
176688 }
176689 j++;
176690 j += jsonNodeSize(&pRoot[j]);
176691 }
176692 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
176693 iRoot += pRoot->u.iAppend;
176694 pRoot = &pParse->aNode[iRoot];
176695 j = 1;
176696 }
176697 if( pApnd ){
176698 u32 iStart, iLabel;
176699 JsonNode *pNode;
176700 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
176701 iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
176702 zPath += i;
176703 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
176704 if( pParse->oom ) return 0;
176705 if( pNode ){
176706 pRoot = &pParse->aNode[iRoot];
176707 pRoot->u.iAppend = iStart - iRoot;
176708 pRoot->jnFlags |= JNODE_APPEND;
176709 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
176710 }
176711 return pNode;
176712 }
176713 }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){
176714 if( pRoot->eType!=JSON_ARRAY ) return 0;
176715 i = 0;
176716 j = 1;
176717 while( safe_isdigit(zPath[j]) ){
176718 i = i*10 + zPath[j] - '0';
176719 j++;
176720 }
176721 if( zPath[j]!=']' ){
176722 *pzErr = zPath;
176723 return 0;
176724 }
176725 zPath += j + 1;
176726 j = 1;
176727 for(;;){
176728 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
176729 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
176730 j += jsonNodeSize(&pRoot[j]);
176731 }
176732 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
176733 iRoot += pRoot->u.iAppend;
176734 pRoot = &pParse->aNode[iRoot];
176735 j = 1;
176736 }
176737 if( j<=pRoot->n ){
176738 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
176739 }
176740 if( i==0 && pApnd ){
176741 u32 iStart;
176742 JsonNode *pNode;
176743 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
176744 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
176745 if( pParse->oom ) return 0;
176746 if( pNode ){
176747 pRoot = &pParse->aNode[iRoot];
176748 pRoot->u.iAppend = iStart - iRoot;
176749 pRoot->jnFlags |= JNODE_APPEND;
176750 }
176751 return pNode;
176752 }
176753 }else{
176754 *pzErr = zPath;
176755 }
176756 return 0;
176757}
176758
176759/*
176760** Append content to pParse that will complete zPath. Return a pointer
176761** to the inserted node, or return NULL if the append fails.
176762*/
176763static JsonNode *jsonLookupAppend(
176764 JsonParse *pParse, /* Append content to the JSON parse */
176765 const char *zPath, /* Description of content to append */
176766 int *pApnd, /* Set this flag to 1 */
176767 const char **pzErr /* Make this point to any syntax error */
176768){
176769 *pApnd = 1;
176770 if( zPath[0]==0 ){
176771 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
176772 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
176773 }
176774 if( zPath[0]=='.' ){
176775 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
176776 }else if( strncmp(zPath,"[0]",3)==0 ){
176777 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
176778 }else{
176779 return 0;
176780 }
176781 if( pParse->oom ) return 0;
176782 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
176783}
176784
176785/*
176786** Return the text of a syntax error message on a JSON path. Space is
176787** obtained from sqlite3_malloc().
176788*/
176789static char *jsonPathSyntaxError(const char *zErr){
176790 return sqlite3_mprintf("JSON path error near '%q'", zErr);
176791}
176792
176793/*
176794** Do a node lookup using zPath. Return a pointer to the node on success.
176795** Return NULL if not found or if there is an error.
176796**
176797** On an error, write an error message into pCtx and increment the
176798** pParse->nErr counter.
176799**
176800** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
176801** nodes are appended.
176802*/
176803static JsonNode *jsonLookup(
176804 JsonParse *pParse, /* The JSON to search */
176805 const char *zPath, /* The path to search */
176806 int *pApnd, /* Append nodes to complete path if not NULL */
176807 sqlite3_context *pCtx /* Report errors here, if not NULL */
176808){
176809 const char *zErr = 0;
176810 JsonNode *pNode = 0;
176811 char *zMsg;
176812
176813 if( zPath==0 ) return 0;
176814 if( zPath[0]!='$' ){
176815 zErr = zPath;
176816 goto lookup_err;
176817 }
176818 zPath++;
176819 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
176820 if( zErr==0 ) return pNode;
176821
176822lookup_err:
176823 pParse->nErr++;
176824 assert( zErr!=0 && pCtx!=0 );
176825 zMsg = jsonPathSyntaxError(zErr);
176826 if( zMsg ){
176827 sqlite3_result_error(pCtx, zMsg, -1);
176828 sqlite3_free(zMsg);
176829 }else{
176830 sqlite3_result_error_nomem(pCtx);
176831 }
176832 return 0;
176833}
176834
176835
176836/*
176837** Report the wrong number of arguments for json_insert(), json_replace()
176838** or json_set().
176839*/
176840static void jsonWrongNumArgs(
176841 sqlite3_context *pCtx,
176842 const char *zFuncName
176843){
176844 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
176845 zFuncName);
176846 sqlite3_result_error(pCtx, zMsg, -1);
176847 sqlite3_free(zMsg);
176848}
176849
176850/*
176851** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
176852*/
176853static void jsonRemoveAllNulls(JsonNode *pNode){
176854 int i, n;
176855 assert( pNode->eType==JSON_OBJECT );
176856 n = pNode->n;
176857 for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
176858 switch( pNode[i].eType ){
176859 case JSON_NULL:
176860 pNode[i].jnFlags |= JNODE_REMOVE;
176861 break;
176862 case JSON_OBJECT:
176863 jsonRemoveAllNulls(&pNode[i]);
176864 break;
176865 }
176866 }
176867}
176868
176869
176870/****************************************************************************
176871** SQL functions used for testing and debugging
176872****************************************************************************/
176873
176874#ifdef SQLITE_DEBUG
176875/*
176876** The json_parse(JSON) function returns a string which describes
176877** a parse of the JSON provided. Or it returns NULL if JSON is not
176878** well-formed.
176879*/
176880static void jsonParseFunc(
176881 sqlite3_context *ctx,
176882 int argc,
176883 sqlite3_value **argv
176884){
176885 JsonString s; /* Output string - not real JSON */
176886 JsonParse x; /* The parse */
176887 u32 i;
176888
176889 assert( argc==1 );
176890 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
176891 jsonParseFindParents(&x);
176892 jsonInit(&s, ctx);
176893 for(i=0; i<x.nNode; i++){
176894 const char *zType;
176895 if( x.aNode[i].jnFlags & JNODE_LABEL ){
176896 assert( x.aNode[i].eType==JSON_STRING );
176897 zType = "label";
176898 }else{
176899 zType = jsonType[x.aNode[i].eType];
176900 }
176901 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
176902 i, zType, x.aNode[i].n, x.aUp[i]);
176903 if( x.aNode[i].u.zJContent!=0 ){
176904 jsonAppendRaw(&s, " ", 1);
176905 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
176906 }
176907 jsonAppendRaw(&s, "\n", 1);
176908 }
176909 jsonParseReset(&x);
176910 jsonResult(&s);
176911}
176912
176913/*
176914** The json_test1(JSON) function return true (1) if the input is JSON
176915** text generated by another json function. It returns (0) if the input
176916** is not known to be JSON.
176917*/
176918static void jsonTest1Func(
176919 sqlite3_context *ctx,
176920 int argc,
176921 sqlite3_value **argv
176922){
176923 UNUSED_PARAM(argc);
176924 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
176925}
176926#endif /* SQLITE_DEBUG */
176927
176928/****************************************************************************
176929** Scalar SQL function implementations
176930****************************************************************************/
176931
176932/*
176933** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
176934** corresponding to the SQL value input. Mostly this means putting
176935** double-quotes around strings and returning the unquoted string "null"
176936** when given a NULL input.
176937*/
176938static void jsonQuoteFunc(
176939 sqlite3_context *ctx,
176940 int argc,
176941 sqlite3_value **argv
176942){
176943 JsonString jx;
176944 UNUSED_PARAM(argc);
176945
176946 jsonInit(&jx, ctx);
176947 jsonAppendValue(&jx, argv[0]);
176948 jsonResult(&jx);
176949 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
176950}
176951
176952/*
176953** Implementation of the json_array(VALUE,...) function. Return a JSON
176954** array that contains all values given in arguments. Or if any argument
176955** is a BLOB, throw an error.
176956*/
176957static void jsonArrayFunc(
176958 sqlite3_context *ctx,
176959 int argc,
176960 sqlite3_value **argv
176961){
176962 int i;
176963 JsonString jx;
176964
176965 jsonInit(&jx, ctx);
176966 jsonAppendChar(&jx, '[');
176967 for(i=0; i<argc; i++){
176968 jsonAppendSeparator(&jx);
176969 jsonAppendValue(&jx, argv[i]);
176970 }
176971 jsonAppendChar(&jx, ']');
176972 jsonResult(&jx);
176973 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
176974}
176975
176976
176977/*
176978** json_array_length(JSON)
176979** json_array_length(JSON, PATH)
176980**
176981** Return the number of elements in the top-level JSON array.
176982** Return 0 if the input is not a well-formed JSON array.
176983*/
176984static void jsonArrayLengthFunc(
176985 sqlite3_context *ctx,
176986 int argc,
176987 sqlite3_value **argv
176988){
176989 JsonParse *p; /* The parse */
176990 sqlite3_int64 n = 0;
176991 u32 i;
176992 JsonNode *pNode;
176993
176994 p = jsonParseCached(ctx, argv, ctx);
176995 if( p==0 ) return;
176996 assert( p->nNode );
176997 if( argc==2 ){
176998 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
176999 pNode = jsonLookup(p, zPath, 0, ctx);
177000 }else{
177001 pNode = p->aNode;
177002 }
177003 if( pNode==0 ){
177004 return;
177005 }
177006 if( pNode->eType==JSON_ARRAY ){
177007 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
177008 for(i=1; i<=pNode->n; n++){
177009 i += jsonNodeSize(&pNode[i]);
177010 }
177011 }
177012 sqlite3_result_int64(ctx, n);
177013}
177014
177015/*
177016** json_extract(JSON, PATH, ...)
177017**
177018** Return the element described by PATH. Return NULL if there is no
177019** PATH element. If there are multiple PATHs, then return a JSON array
177020** with the result from each path. Throw an error if the JSON or any PATH
177021** is malformed.
177022*/
177023static void jsonExtractFunc(
177024 sqlite3_context *ctx,
177025 int argc,
177026 sqlite3_value **argv
177027){
177028 JsonParse *p; /* The parse */
177029 JsonNode *pNode;
177030 const char *zPath;
177031 JsonString jx;
177032 int i;
177033
177034 if( argc<2 ) return;
177035 p = jsonParseCached(ctx, argv, ctx);
177036 if( p==0 ) return;
177037 jsonInit(&jx, ctx);
177038 jsonAppendChar(&jx, '[');
177039 for(i=1; i<argc; i++){
177040 zPath = (const char*)sqlite3_value_text(argv[i]);
177041 pNode = jsonLookup(p, zPath, 0, ctx);
177042 if( p->nErr ) break;
177043 if( argc>2 ){
177044 jsonAppendSeparator(&jx);
177045 if( pNode ){
177046 jsonRenderNode(pNode, &jx, 0);
177047 }else{
177048 jsonAppendRaw(&jx, "null", 4);
177049 }
177050 }else if( pNode ){
177051 jsonReturn(pNode, ctx, 0);
177052 }
177053 }
177054 if( argc>2 && i==argc ){
177055 jsonAppendChar(&jx, ']');
177056 jsonResult(&jx);
177057 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
177058 }
177059 jsonReset(&jx);
177060}
177061
177062/* This is the RFC 7396 MergePatch algorithm.
177063*/
177064static JsonNode *jsonMergePatch(
177065 JsonParse *pParse, /* The JSON parser that contains the TARGET */
177066 u32 iTarget, /* Node of the TARGET in pParse */
177067 JsonNode *pPatch /* The PATCH */
177068){
177069 u32 i, j;
177070 u32 iRoot;
177071 JsonNode *pTarget;
177072 if( pPatch->eType!=JSON_OBJECT ){
177073 return pPatch;
177074 }
177075 assert( iTarget>=0 && iTarget<pParse->nNode );
177076 pTarget = &pParse->aNode[iTarget];
177077 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
177078 if( pTarget->eType!=JSON_OBJECT ){
177079 jsonRemoveAllNulls(pPatch);
177080 return pPatch;
177081 }
177082 iRoot = iTarget;
177083 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
177084 u32 nKey;
177085 const char *zKey;
177086 assert( pPatch[i].eType==JSON_STRING );
177087 assert( pPatch[i].jnFlags & JNODE_LABEL );
177088 nKey = pPatch[i].n;
177089 zKey = pPatch[i].u.zJContent;
177090 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
177091 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
177092 assert( pTarget[j].eType==JSON_STRING );
177093 assert( pTarget[j].jnFlags & JNODE_LABEL );
177094 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
177095 if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
177096 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
177097 if( pPatch[i+1].eType==JSON_NULL ){
177098 pTarget[j+1].jnFlags |= JNODE_REMOVE;
177099 }else{
177100 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
177101 if( pNew==0 ) return 0;
177102 pTarget = &pParse->aNode[iTarget];
177103 if( pNew!=&pTarget[j+1] ){
177104 pTarget[j+1].u.pPatch = pNew;
177105 pTarget[j+1].jnFlags |= JNODE_PATCH;
177106 }
177107 }
177108 break;
177109 }
177110 }
177111 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
177112 int iStart, iPatch;
177113 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
177114 jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
177115 iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
177116 if( pParse->oom ) return 0;
177117 jsonRemoveAllNulls(pPatch);
177118 pTarget = &pParse->aNode[iTarget];
177119 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
177120 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
177121 iRoot = iStart;
177122 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
177123 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
177124 }
177125 }
177126 return pTarget;
177127}
177128
177129/*
177130** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
177131** object that is the result of running the RFC 7396 MergePatch() algorithm
177132** on the two arguments.
177133*/
177134static void jsonPatchFunc(
177135 sqlite3_context *ctx,
177136 int argc,
177137 sqlite3_value **argv
177138){
177139 JsonParse x; /* The JSON that is being patched */
177140 JsonParse y; /* The patch */
177141 JsonNode *pResult; /* The result of the merge */
177142
177143 UNUSED_PARAM(argc);
177144 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
177145 if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
177146 jsonParseReset(&x);
177147 return;
177148 }
177149 pResult = jsonMergePatch(&x, 0, y.aNode);
177150 assert( pResult!=0 || x.oom );
177151 if( pResult ){
177152 jsonReturnJson(pResult, ctx, 0);
177153 }else{
177154 sqlite3_result_error_nomem(ctx);
177155 }
177156 jsonParseReset(&x);
177157 jsonParseReset(&y);
177158}
177159
177160
177161/*
177162** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
177163** object that contains all name/value given in arguments. Or if any name
177164** is not a string or if any value is a BLOB, throw an error.
177165*/
177166static void jsonObjectFunc(
177167 sqlite3_context *ctx,
177168 int argc,
177169 sqlite3_value **argv
177170){
177171 int i;
177172 JsonString jx;
177173 const char *z;
177174 u32 n;
177175
177176 if( argc&1 ){
177177 sqlite3_result_error(ctx, "json_object() requires an even number "
177178 "of arguments", -1);
177179 return;
177180 }
177181 jsonInit(&jx, ctx);
177182 jsonAppendChar(&jx, '{');
177183 for(i=0; i<argc; i+=2){
177184 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
177185 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
177186 jsonReset(&jx);
177187 return;
177188 }
177189 jsonAppendSeparator(&jx);
177190 z = (const char*)sqlite3_value_text(argv[i]);
177191 n = (u32)sqlite3_value_bytes(argv[i]);
177192 jsonAppendString(&jx, z, n);
177193 jsonAppendChar(&jx, ':');
177194 jsonAppendValue(&jx, argv[i+1]);
177195 }
177196 jsonAppendChar(&jx, '}');
177197 jsonResult(&jx);
177198 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
177199}
177200
177201
177202/*
177203** json_remove(JSON, PATH, ...)
177204**
177205** Remove the named elements from JSON and return the result. malformed
177206** JSON or PATH arguments result in an error.
177207*/
177208static void jsonRemoveFunc(
177209 sqlite3_context *ctx,
177210 int argc,
177211 sqlite3_value **argv
177212){
177213 JsonParse x; /* The parse */
177214 JsonNode *pNode;
177215 const char *zPath;
177216 u32 i;
177217
177218 if( argc<1 ) return;
177219 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
177220 assert( x.nNode );
177221 for(i=1; i<(u32)argc; i++){
177222 zPath = (const char*)sqlite3_value_text(argv[i]);
177223 if( zPath==0 ) goto remove_done;
177224 pNode = jsonLookup(&x, zPath, 0, ctx);
177225 if( x.nErr ) goto remove_done;
177226 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
177227 }
177228 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
177229 jsonReturnJson(x.aNode, ctx, 0);
177230 }
177231remove_done:
177232 jsonParseReset(&x);
177233}
177234
177235/*
177236** json_replace(JSON, PATH, VALUE, ...)
177237**
177238** Replace the value at PATH with VALUE. If PATH does not already exist,
177239** this routine is a no-op. If JSON or PATH is malformed, throw an error.
177240*/
177241static void jsonReplaceFunc(
177242 sqlite3_context *ctx,
177243 int argc,
177244 sqlite3_value **argv
177245){
177246 JsonParse x; /* The parse */
177247 JsonNode *pNode;
177248 const char *zPath;
177249 u32 i;
177250
177251 if( argc<1 ) return;
177252 if( (argc&1)==0 ) {
177253 jsonWrongNumArgs(ctx, "replace");
177254 return;
177255 }
177256 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
177257 assert( x.nNode );
177258 for(i=1; i<(u32)argc; i+=2){
177259 zPath = (const char*)sqlite3_value_text(argv[i]);
177260 pNode = jsonLookup(&x, zPath, 0, ctx);
177261 if( x.nErr ) goto replace_err;
177262 if( pNode ){
177263 pNode->jnFlags |= (u8)JNODE_REPLACE;
177264 pNode->u.iReplace = i + 1;
177265 }
177266 }
177267 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
177268 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
177269 }else{
177270 jsonReturnJson(x.aNode, ctx, argv);
177271 }
177272replace_err:
177273 jsonParseReset(&x);
177274}
177275
177276/*
177277** json_set(JSON, PATH, VALUE, ...)
177278**
177279** Set the value at PATH to VALUE. Create the PATH if it does not already
177280** exist. Overwrite existing values that do exist.
177281** If JSON or PATH is malformed, throw an error.
177282**
177283** json_insert(JSON, PATH, VALUE, ...)
177284**
177285** Create PATH and initialize it to VALUE. If PATH already exists, this
177286** routine is a no-op. If JSON or PATH is malformed, throw an error.
177287*/
177288static void jsonSetFunc(
177289 sqlite3_context *ctx,
177290 int argc,
177291 sqlite3_value **argv
177292){
177293 JsonParse x; /* The parse */
177294 JsonNode *pNode;
177295 const char *zPath;
177296 u32 i;
177297 int bApnd;
177298 int bIsSet = *(int*)sqlite3_user_data(ctx);
177299
177300 if( argc<1 ) return;
177301 if( (argc&1)==0 ) {
177302 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
177303 return;
177304 }
177305 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
177306 assert( x.nNode );
177307 for(i=1; i<(u32)argc; i+=2){
177308 zPath = (const char*)sqlite3_value_text(argv[i]);
177309 bApnd = 0;
177310 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
177311 if( x.oom ){
177312 sqlite3_result_error_nomem(ctx);
177313 goto jsonSetDone;
177314 }else if( x.nErr ){
177315 goto jsonSetDone;
177316 }else if( pNode && (bApnd || bIsSet) ){
177317 pNode->jnFlags |= (u8)JNODE_REPLACE;
177318 pNode->u.iReplace = i + 1;
177319 }
177320 }
177321 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
177322 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
177323 }else{
177324 jsonReturnJson(x.aNode, ctx, argv);
177325 }
177326jsonSetDone:
177327 jsonParseReset(&x);
177328}
177329
177330/*
177331** json_type(JSON)
177332** json_type(JSON, PATH)
177333**
177334** Return the top-level "type" of a JSON string. Throw an error if
177335** either the JSON or PATH inputs are not well-formed.
177336*/
177337static void jsonTypeFunc(
177338 sqlite3_context *ctx,
177339 int argc,
177340 sqlite3_value **argv
177341){
177342 JsonParse *p; /* The parse */
177343 const char *zPath;
177344 JsonNode *pNode;
177345
177346 p = jsonParseCached(ctx, argv, ctx);
177347 if( p==0 ) return;
177348 if( argc==2 ){
177349 zPath = (const char*)sqlite3_value_text(argv[1]);
177350 pNode = jsonLookup(p, zPath, 0, ctx);
177351 }else{
177352 pNode = p->aNode;
177353 }
177354 if( pNode ){
177355 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
177356 }
177357}
177358
177359/*
177360** json_valid(JSON)
177361**
177362** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
177363** Return 0 otherwise.
177364*/
177365static void jsonValidFunc(
177366 sqlite3_context *ctx,
177367 int argc,
177368 sqlite3_value **argv
177369){
177370 JsonParse *p; /* The parse */
177371 UNUSED_PARAM(argc);
177372 p = jsonParseCached(ctx, argv, 0);
177373 sqlite3_result_int(ctx, p!=0);
177374}
177375
177376
177377/****************************************************************************
177378** Aggregate SQL function implementations
177379****************************************************************************/
177380/*
177381** json_group_array(VALUE)
177382**
177383** Return a JSON array composed of all values in the aggregate.
177384*/
177385static void jsonArrayStep(
177386 sqlite3_context *ctx,
177387 int argc,
177388 sqlite3_value **argv
177389){
177390 JsonString *pStr;
177391 UNUSED_PARAM(argc);
177392 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
177393 if( pStr ){
177394 if( pStr->zBuf==0 ){
177395 jsonInit(pStr, ctx);
177396 jsonAppendChar(pStr, '[');
177397 }else{
177398 jsonAppendChar(pStr, ',');
177399 pStr->pCtx = ctx;
177400 }
177401 jsonAppendValue(pStr, argv[0]);
177402 }
177403}
177404static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
177405 JsonString *pStr;
177406 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
177407 if( pStr ){
177408 pStr->pCtx = ctx;
177409 jsonAppendChar(pStr, ']');
177410 if( pStr->bErr ){
177411 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
177412 assert( pStr->bStatic );
177413 }else if( isFinal ){
177414 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
177415 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
177416 pStr->bStatic = 1;
177417 }else{
177418 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
177419 pStr->nUsed--;
177420 }
177421 }else{
177422 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
177423 }
177424 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
177425}
177426static void jsonArrayValue(sqlite3_context *ctx){
177427 jsonArrayCompute(ctx, 0);
177428}
177429static void jsonArrayFinal(sqlite3_context *ctx){
177430 jsonArrayCompute(ctx, 1);
177431}
177432
177433#ifndef SQLITE_OMIT_WINDOWFUNC
177434/*
177435** This method works for both json_group_array() and json_group_object().
177436** It works by removing the first element of the group by searching forward
177437** to the first comma (",") that is not within a string and deleting all
177438** text through that comma.
177439*/
177440static void jsonGroupInverse(
177441 sqlite3_context *ctx,
177442 int argc,
177443 sqlite3_value **argv
177444){
177445 int i;
177446 int inStr = 0;
177447 char *z;
177448 JsonString *pStr;
177449 UNUSED_PARAM(argc);
177450 UNUSED_PARAM(argv);
177451 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
177452#ifdef NEVER
177453 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
177454 ** always have been called to initalize it */
177455 if( NEVER(!pStr) ) return;
177456#endif
177457 z = pStr->zBuf;
177458 for(i=1; z[i]!=',' || inStr; i++){
177459 assert( i<pStr->nUsed );
177460 if( z[i]=='"' ){
177461 inStr = !inStr;
177462 }else if( z[i]=='\\' ){
177463 i++;
177464 }
177465 }
177466 pStr->nUsed -= i;
177467 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
177468}
177469#else
177470# define jsonGroupInverse 0
177471#endif
177472
177473
177474/*
177475** json_group_obj(NAME,VALUE)
177476**
177477** Return a JSON object composed of all names and values in the aggregate.
177478*/
177479static void jsonObjectStep(
177480 sqlite3_context *ctx,
177481 int argc,
177482 sqlite3_value **argv
177483){
177484 JsonString *pStr;
177485 const char *z;
177486 u32 n;
177487 UNUSED_PARAM(argc);
177488 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
177489 if( pStr ){
177490 if( pStr->zBuf==0 ){
177491 jsonInit(pStr, ctx);
177492 jsonAppendChar(pStr, '{');
177493 }else{
177494 jsonAppendChar(pStr, ',');
177495 pStr->pCtx = ctx;
177496 }
177497 z = (const char*)sqlite3_value_text(argv[0]);
177498 n = (u32)sqlite3_value_bytes(argv[0]);
177499 jsonAppendString(pStr, z, n);
177500 jsonAppendChar(pStr, ':');
177501 jsonAppendValue(pStr, argv[1]);
177502 }
177503}
177504static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
177505 JsonString *pStr;
177506 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
177507 if( pStr ){
177508 jsonAppendChar(pStr, '}');
177509 if( pStr->bErr ){
177510 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
177511 assert( pStr->bStatic );
177512 }else if( isFinal ){
177513 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
177514 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
177515 pStr->bStatic = 1;
177516 }else{
177517 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
177518 pStr->nUsed--;
177519 }
177520 }else{
177521 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
177522 }
177523 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
177524}
177525static void jsonObjectValue(sqlite3_context *ctx){
177526 jsonObjectCompute(ctx, 0);
177527}
177528static void jsonObjectFinal(sqlite3_context *ctx){
177529 jsonObjectCompute(ctx, 1);
177530}
177531
177532
177533
177534#ifndef SQLITE_OMIT_VIRTUALTABLE
177535/****************************************************************************
177536** The json_each virtual table
177537****************************************************************************/
177538typedef struct JsonEachCursor JsonEachCursor;
177539struct JsonEachCursor {
177540 sqlite3_vtab_cursor base; /* Base class - must be first */
177541 u32 iRowid; /* The rowid */
177542 u32 iBegin; /* The first node of the scan */
177543 u32 i; /* Index in sParse.aNode[] of current row */
177544 u32 iEnd; /* EOF when i equals or exceeds this value */
177545 u8 eType; /* Type of top-level element */
177546 u8 bRecursive; /* True for json_tree(). False for json_each() */
177547 char *zJson; /* Input JSON */
177548 char *zRoot; /* Path by which to filter zJson */
177549 JsonParse sParse; /* Parse of the input JSON */
177550};
177551
177552/* Constructor for the json_each virtual table */
177553static int jsonEachConnect(
177554 sqlite3 *db,
177555 void *pAux,
177556 int argc, const char *const*argv,
177557 sqlite3_vtab **ppVtab,
177558 char **pzErr
177559){
177560 sqlite3_vtab *pNew;
177561 int rc;
177562
177563/* Column numbers */
177564#define JEACH_KEY 0
177565#define JEACH_VALUE 1
177566#define JEACH_TYPE 2
177567#define JEACH_ATOM 3
177568#define JEACH_ID 4
177569#define JEACH_PARENT 5
177570#define JEACH_FULLKEY 6
177571#define JEACH_PATH 7
177572#define JEACH_JSON 8
177573#define JEACH_ROOT 9
177574
177575 UNUSED_PARAM(pzErr);
177576 UNUSED_PARAM(argv);
177577 UNUSED_PARAM(argc);
177578 UNUSED_PARAM(pAux);
177579 rc = sqlite3_declare_vtab(db,
177580 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
177581 "json HIDDEN,root HIDDEN)");
177582 if( rc==SQLITE_OK ){
177583 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
177584 if( pNew==0 ) return SQLITE_NOMEM;
177585 memset(pNew, 0, sizeof(*pNew));
177586 }
177587 return rc;
177588}
177589
177590/* destructor for json_each virtual table */
177591static int jsonEachDisconnect(sqlite3_vtab *pVtab){
177592 sqlite3_free(pVtab);
177593 return SQLITE_OK;
177594}
177595
177596/* constructor for a JsonEachCursor object for json_each(). */
177597static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
177598 JsonEachCursor *pCur;
177599
177600 UNUSED_PARAM(p);
177601 pCur = sqlite3_malloc( sizeof(*pCur) );
177602 if( pCur==0 ) return SQLITE_NOMEM;
177603 memset(pCur, 0, sizeof(*pCur));
177604 *ppCursor = &pCur->base;
177605 return SQLITE_OK;
177606}
177607
177608/* constructor for a JsonEachCursor object for json_tree(). */
177609static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
177610 int rc = jsonEachOpenEach(p, ppCursor);
177611 if( rc==SQLITE_OK ){
177612 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
177613 pCur->bRecursive = 1;
177614 }
177615 return rc;
177616}
177617
177618/* Reset a JsonEachCursor back to its original state. Free any memory
177619** held. */
177620static void jsonEachCursorReset(JsonEachCursor *p){
177621 sqlite3_free(p->zJson);
177622 sqlite3_free(p->zRoot);
177623 jsonParseReset(&p->sParse);
177624 p->iRowid = 0;
177625 p->i = 0;
177626 p->iEnd = 0;
177627 p->eType = 0;
177628 p->zJson = 0;
177629 p->zRoot = 0;
177630}
177631
177632/* Destructor for a jsonEachCursor object */
177633static int jsonEachClose(sqlite3_vtab_cursor *cur){
177634 JsonEachCursor *p = (JsonEachCursor*)cur;
177635 jsonEachCursorReset(p);
177636 sqlite3_free(cur);
177637 return SQLITE_OK;
177638}
177639
177640/* Return TRUE if the jsonEachCursor object has been advanced off the end
177641** of the JSON object */
177642static int jsonEachEof(sqlite3_vtab_cursor *cur){
177643 JsonEachCursor *p = (JsonEachCursor*)cur;
177644 return p->i >= p->iEnd;
177645}
177646
177647/* Advance the cursor to the next element for json_tree() */
177648static int jsonEachNext(sqlite3_vtab_cursor *cur){
177649 JsonEachCursor *p = (JsonEachCursor*)cur;
177650 if( p->bRecursive ){
177651 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
177652 p->i++;
177653 p->iRowid++;
177654 if( p->i<p->iEnd ){
177655 u32 iUp = p->sParse.aUp[p->i];
177656 JsonNode *pUp = &p->sParse.aNode[iUp];
177657 p->eType = pUp->eType;
177658 if( pUp->eType==JSON_ARRAY ){
177659 if( iUp==p->i-1 ){
177660 pUp->u.iKey = 0;
177661 }else{
177662 pUp->u.iKey++;
177663 }
177664 }
177665 }
177666 }else{
177667 switch( p->eType ){
177668 case JSON_ARRAY: {
177669 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
177670 p->iRowid++;
177671 break;
177672 }
177673 case JSON_OBJECT: {
177674 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
177675 p->iRowid++;
177676 break;
177677 }
177678 default: {
177679 p->i = p->iEnd;
177680 break;
177681 }
177682 }
177683 }
177684 return SQLITE_OK;
177685}
177686
177687/* Append the name of the path for element i to pStr
177688*/
177689static void jsonEachComputePath(
177690 JsonEachCursor *p, /* The cursor */
177691 JsonString *pStr, /* Write the path here */
177692 u32 i /* Path to this element */
177693){
177694 JsonNode *pNode, *pUp;
177695 u32 iUp;
177696 if( i==0 ){
177697 jsonAppendChar(pStr, '$');
177698 return;
177699 }
177700 iUp = p->sParse.aUp[i];
177701 jsonEachComputePath(p, pStr, iUp);
177702 pNode = &p->sParse.aNode[i];
177703 pUp = &p->sParse.aNode[iUp];
177704 if( pUp->eType==JSON_ARRAY ){
177705 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
177706 }else{
177707 assert( pUp->eType==JSON_OBJECT );
177708 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
177709 assert( pNode->eType==JSON_STRING );
177710 assert( pNode->jnFlags & JNODE_LABEL );
177711 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
177712 }
177713}
177714
177715/* Return the value of a column */
177716static int jsonEachColumn(
177717 sqlite3_vtab_cursor *cur, /* The cursor */
177718 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
177719 int i /* Which column to return */
177720){
177721 JsonEachCursor *p = (JsonEachCursor*)cur;
177722 JsonNode *pThis = &p->sParse.aNode[p->i];
177723 switch( i ){
177724 case JEACH_KEY: {
177725 if( p->i==0 ) break;
177726 if( p->eType==JSON_OBJECT ){
177727 jsonReturn(pThis, ctx, 0);
177728 }else if( p->eType==JSON_ARRAY ){
177729 u32 iKey;
177730 if( p->bRecursive ){
177731 if( p->iRowid==0 ) break;
177732 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
177733 }else{
177734 iKey = p->iRowid;
177735 }
177736 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
177737 }
177738 break;
177739 }
177740 case JEACH_VALUE: {
177741 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
177742 jsonReturn(pThis, ctx, 0);
177743 break;
177744 }
177745 case JEACH_TYPE: {
177746 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
177747 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
177748 break;
177749 }
177750 case JEACH_ATOM: {
177751 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
177752 if( pThis->eType>=JSON_ARRAY ) break;
177753 jsonReturn(pThis, ctx, 0);
177754 break;
177755 }
177756 case JEACH_ID: {
177757 sqlite3_result_int64(ctx,
177758 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
177759 break;
177760 }
177761 case JEACH_PARENT: {
177762 if( p->i>p->iBegin && p->bRecursive ){
177763 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
177764 }
177765 break;
177766 }
177767 case JEACH_FULLKEY: {
177768 JsonString x;
177769 jsonInit(&x, ctx);
177770 if( p->bRecursive ){
177771 jsonEachComputePath(p, &x, p->i);
177772 }else{
177773 if( p->zRoot ){
177774 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
177775 }else{
177776 jsonAppendChar(&x, '$');
177777 }
177778 if( p->eType==JSON_ARRAY ){
177779 jsonPrintf(30, &x, "[%d]", p->iRowid);
177780 }else if( p->eType==JSON_OBJECT ){
177781 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
177782 }
177783 }
177784 jsonResult(&x);
177785 break;
177786 }
177787 case JEACH_PATH: {
177788 if( p->bRecursive ){
177789 JsonString x;
177790 jsonInit(&x, ctx);
177791 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
177792 jsonResult(&x);
177793 break;
177794 }
177795 /* For json_each() path and root are the same so fall through
177796 ** into the root case */
177797 }
177798 default: {
177799 const char *zRoot = p->zRoot;
177800 if( zRoot==0 ) zRoot = "$";
177801 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
177802 break;
177803 }
177804 case JEACH_JSON: {
177805 assert( i==JEACH_JSON );
177806 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
177807 break;
177808 }
177809 }
177810 return SQLITE_OK;
177811}
177812
177813/* Return the current rowid value */
177814static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
177815 JsonEachCursor *p = (JsonEachCursor*)cur;
177816 *pRowid = p->iRowid;
177817 return SQLITE_OK;
177818}
177819
177820/* The query strategy is to look for an equality constraint on the json
177821** column. Without such a constraint, the table cannot operate. idxNum is
177822** 1 if the constraint is found, 3 if the constraint and zRoot are found,
177823** and 0 otherwise.
177824*/
177825static int jsonEachBestIndex(
177826 sqlite3_vtab *tab,
177827 sqlite3_index_info *pIdxInfo
177828){
177829 int i;
177830 int jsonIdx = -1;
177831 int rootIdx = -1;
177832 const struct sqlite3_index_constraint *pConstraint;
177833
177834 UNUSED_PARAM(tab);
177835 pConstraint = pIdxInfo->aConstraint;
177836 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
177837 if( pConstraint->usable==0 ) continue;
177838 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
177839 switch( pConstraint->iColumn ){
177840 case JEACH_JSON: jsonIdx = i; break;
177841 case JEACH_ROOT: rootIdx = i; break;
177842 default: /* no-op */ break;
177843 }
177844 }
177845 if( jsonIdx<0 ){
177846 pIdxInfo->idxNum = 0;
177847 pIdxInfo->estimatedCost = 1e99;
177848 }else{
177849 pIdxInfo->estimatedCost = 1.0;
177850 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
177851 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
177852 if( rootIdx<0 ){
177853 pIdxInfo->idxNum = 1;
177854 }else{
177855 pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2;
177856 pIdxInfo->aConstraintUsage[rootIdx].omit = 1;
177857 pIdxInfo->idxNum = 3;
177858 }
177859 }
177860 return SQLITE_OK;
177861}
177862
177863/* Start a search on a new JSON string */
177864static int jsonEachFilter(
177865 sqlite3_vtab_cursor *cur,
177866 int idxNum, const char *idxStr,
177867 int argc, sqlite3_value **argv
177868){
177869 JsonEachCursor *p = (JsonEachCursor*)cur;
177870 const char *z;
177871 const char *zRoot = 0;
177872 sqlite3_int64 n;
177873
177874 UNUSED_PARAM(idxStr);
177875 UNUSED_PARAM(argc);
177876 jsonEachCursorReset(p);
177877 if( idxNum==0 ) return SQLITE_OK;
177878 z = (const char*)sqlite3_value_text(argv[0]);
177879 if( z==0 ) return SQLITE_OK;
177880 n = sqlite3_value_bytes(argv[0]);
177881 p->zJson = sqlite3_malloc64( n+1 );
177882 if( p->zJson==0 ) return SQLITE_NOMEM;
177883 memcpy(p->zJson, z, (size_t)n+1);
177884 if( jsonParse(&p->sParse, 0, p->zJson) ){
177885 int rc = SQLITE_NOMEM;
177886 if( p->sParse.oom==0 ){
177887 sqlite3_free(cur->pVtab->zErrMsg);
177888 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
177889 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
177890 }
177891 jsonEachCursorReset(p);
177892 return rc;
177893 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
177894 jsonEachCursorReset(p);
177895 return SQLITE_NOMEM;
177896 }else{
177897 JsonNode *pNode = 0;
177898 if( idxNum==3 ){
177899 const char *zErr = 0;
177900 zRoot = (const char*)sqlite3_value_text(argv[1]);
177901 if( zRoot==0 ) return SQLITE_OK;
177902 n = sqlite3_value_bytes(argv[1]);
177903 p->zRoot = sqlite3_malloc64( n+1 );
177904 if( p->zRoot==0 ) return SQLITE_NOMEM;
177905 memcpy(p->zRoot, zRoot, (size_t)n+1);
177906 if( zRoot[0]!='$' ){
177907 zErr = zRoot;
177908 }else{
177909 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
177910 }
177911 if( zErr ){
177912 sqlite3_free(cur->pVtab->zErrMsg);
177913 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
177914 jsonEachCursorReset(p);
177915 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
177916 }else if( pNode==0 ){
177917 return SQLITE_OK;
177918 }
177919 }else{
177920 pNode = p->sParse.aNode;
177921 }
177922 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
177923 p->eType = pNode->eType;
177924 if( p->eType>=JSON_ARRAY ){
177925 pNode->u.iKey = 0;
177926 p->iEnd = p->i + pNode->n + 1;
177927 if( p->bRecursive ){
177928 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
177929 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
177930 p->i--;
177931 }
177932 }else{
177933 p->i++;
177934 }
177935 }else{
177936 p->iEnd = p->i+1;
177937 }
177938 }
177939 return SQLITE_OK;
177940}
177941
177942/* The methods of the json_each virtual table */
177943static sqlite3_module jsonEachModule = {
177944 0, /* iVersion */
177945 0, /* xCreate */
177946 jsonEachConnect, /* xConnect */
177947 jsonEachBestIndex, /* xBestIndex */
177948 jsonEachDisconnect, /* xDisconnect */
177949 0, /* xDestroy */
177950 jsonEachOpenEach, /* xOpen - open a cursor */
177951 jsonEachClose, /* xClose - close a cursor */
177952 jsonEachFilter, /* xFilter - configure scan constraints */
177953 jsonEachNext, /* xNext - advance a cursor */
177954 jsonEachEof, /* xEof - check for end of scan */
177955 jsonEachColumn, /* xColumn - read data */
177956 jsonEachRowid, /* xRowid - read data */
177957 0, /* xUpdate */
177958 0, /* xBegin */
177959 0, /* xSync */
177960 0, /* xCommit */
177961 0, /* xRollback */
177962 0, /* xFindMethod */
177963 0, /* xRename */
177964 0, /* xSavepoint */
177965 0, /* xRelease */
177966 0 /* xRollbackTo */
177967};
177968
177969/* The methods of the json_tree virtual table. */
177970static sqlite3_module jsonTreeModule = {
177971 0, /* iVersion */
177972 0, /* xCreate */
177973 jsonEachConnect, /* xConnect */
177974 jsonEachBestIndex, /* xBestIndex */
177975 jsonEachDisconnect, /* xDisconnect */
177976 0, /* xDestroy */
177977 jsonEachOpenTree, /* xOpen - open a cursor */
177978 jsonEachClose, /* xClose - close a cursor */
177979 jsonEachFilter, /* xFilter - configure scan constraints */
177980 jsonEachNext, /* xNext - advance a cursor */
177981 jsonEachEof, /* xEof - check for end of scan */
177982 jsonEachColumn, /* xColumn - read data */
177983 jsonEachRowid, /* xRowid - read data */
177984 0, /* xUpdate */
177985 0, /* xBegin */
177986 0, /* xSync */
177987 0, /* xCommit */
177988 0, /* xRollback */
177989 0, /* xFindMethod */
177990 0, /* xRename */
177991 0, /* xSavepoint */
177992 0, /* xRelease */
177993 0 /* xRollbackTo */
177994};
177995#endif /* SQLITE_OMIT_VIRTUALTABLE */
177996
177997/****************************************************************************
177998** The following routines are the only publically visible identifiers in this
177999** file. Call the following routines in order to register the various SQL
178000** functions and the virtual table implemented by this file.
178001****************************************************************************/
178002
178003SQLITE_PRIVATE int sqlite3Json1Init(sqlite3 *db){
178004 int rc = SQLITE_OK;
178005 unsigned int i;
178006 static const struct {
178007 const char *zName;
178008 int nArg;
178009 int flag;
178010 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
178011 } aFunc[] = {
178012 { "json", 1, 0, jsonRemoveFunc },
178013 { "json_array", -1, 0, jsonArrayFunc },
178014 { "json_array_length", 1, 0, jsonArrayLengthFunc },
178015 { "json_array_length", 2, 0, jsonArrayLengthFunc },
178016 { "json_extract", -1, 0, jsonExtractFunc },
178017 { "json_insert", -1, 0, jsonSetFunc },
178018 { "json_object", -1, 0, jsonObjectFunc },
178019 { "json_patch", 2, 0, jsonPatchFunc },
178020 { "json_quote", 1, 0, jsonQuoteFunc },
178021 { "json_remove", -1, 0, jsonRemoveFunc },
178022 { "json_replace", -1, 0, jsonReplaceFunc },
178023 { "json_set", -1, 1, jsonSetFunc },
178024 { "json_type", 1, 0, jsonTypeFunc },
178025 { "json_type", 2, 0, jsonTypeFunc },
178026 { "json_valid", 1, 0, jsonValidFunc },
178027
178028#if SQLITE_DEBUG
178029 /* DEBUG and TESTING functions */
178030 { "json_parse", 1, 0, jsonParseFunc },
178031 { "json_test1", 1, 0, jsonTest1Func },
178032#endif
178033 };
178034 static const struct {
178035 const char *zName;
178036 int nArg;
178037 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
178038 void (*xFinal)(sqlite3_context*);
178039 void (*xValue)(sqlite3_context*);
178040 } aAgg[] = {
178041 { "json_group_array", 1,
178042 jsonArrayStep, jsonArrayFinal, jsonArrayValue },
178043 { "json_group_object", 2,
178044 jsonObjectStep, jsonObjectFinal, jsonObjectValue },
178045 };
178046#ifndef SQLITE_OMIT_VIRTUALTABLE
178047 static const struct {
178048 const char *zName;
178049 sqlite3_module *pModule;
178050 } aMod[] = {
178051 { "json_each", &jsonEachModule },
178052 { "json_tree", &jsonTreeModule },
178053 };
178054#endif
178055 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
178056 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
178057 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
178058 (void*)&aFunc[i].flag,
178059 aFunc[i].xFunc, 0, 0);
178060 }
178061#ifndef SQLITE_OMIT_WINDOWFUNC
178062 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
178063 rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
178064 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
178065 aAgg[i].xStep, aAgg[i].xFinal,
178066 aAgg[i].xValue, jsonGroupInverse, 0);
178067 }
178068#endif
178069#ifndef SQLITE_OMIT_VIRTUALTABLE
178070 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
178071 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
178072 }
178073#endif
178074 return rc;
178075}
178076
178077
178078#ifndef SQLITE_CORE
178079#ifdef _WIN32
178080__declspec(dllexport)
178081#endif
178082SQLITE_API int sqlite3_json_init(
178083 sqlite3 *db,
178084 char **pzErrMsg,
178085 const sqlite3_api_routines *pApi
178086){
178087 SQLITE_EXTENSION_INIT2(pApi);
178088 (void)pzErrMsg; /* Unused parameter */
178089 return sqlite3Json1Init(db);
178090}
178091#endif
178092#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */
178093
178094/************** End of json1.c ***********************************************/
178095/************** Begin file rtree.c *******************************************/
178096/*
178097** 2001 September 15
178098**
178099** The author disclaims copyright to this source code. In place of
178100** a legal notice, here is a blessing:
178101**
178102** May you do good and not evil.
178103** May you find forgiveness for yourself and forgive others.
178104** May you share freely, never taking more than you give.
178105**
178106*************************************************************************
178107** This file contains code for implementations of the r-tree and r*-tree
178108** algorithms packaged as an SQLite virtual table module.
178109*/
178110
178111/*
178112** Database Format of R-Tree Tables
178113** --------------------------------
178114**
178115** The data structure for a single virtual r-tree table is stored in three
178116** native SQLite tables declared as follows. In each case, the '%' character
178117** in the table name is replaced with the user-supplied name of the r-tree
178118** table.
178119**
178120** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
178121** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
178122** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
178123**
178124** The data for each node of the r-tree structure is stored in the %_node
178125** table. For each node that is not the root node of the r-tree, there is
178126** an entry in the %_parent table associating the node with its parent.
178127** And for each row of data in the table, there is an entry in the %_rowid
178128** table that maps from the entries rowid to the id of the node that it
178129** is stored on. If the r-tree contains auxiliary columns, those are stored
178130** on the end of the %_rowid table.
178131**
178132** The root node of an r-tree always exists, even if the r-tree table is
178133** empty. The nodeno of the root node is always 1. All other nodes in the
178134** table must be the same size as the root node. The content of each node
178135** is formatted as follows:
178136**
178137** 1. If the node is the root node (node 1), then the first 2 bytes
178138** of the node contain the tree depth as a big-endian integer.
178139** For non-root nodes, the first 2 bytes are left unused.
178140**
178141** 2. The next 2 bytes contain the number of entries currently
178142** stored in the node.
178143**
178144** 3. The remainder of the node contains the node entries. Each entry
178145** consists of a single 8-byte integer followed by an even number
178146** of 4-byte coordinates. For leaf nodes the integer is the rowid
178147** of a record. For internal nodes it is the node number of a
178148** child page.
178149*/
178150
178151#if !defined(SQLITE_CORE) \
178152 || (defined(SQLITE_ENABLE_RTREE) && !defined(SQLITE_OMIT_VIRTUALTABLE))
178153
178154#ifndef SQLITE_CORE
178155/* #include "sqlite3ext.h" */
178156 SQLITE_EXTENSION_INIT1
178157#else
178158/* #include "sqlite3.h" */
178159#endif
178160
178161/* #include <string.h> */
178162/* #include <assert.h> */
178163/* #include <stdio.h> */
178164
178165#ifndef SQLITE_AMALGAMATION
178166#include "sqlite3rtree.h"
178167typedef sqlite3_int64 i64;
178168typedef sqlite3_uint64 u64;
178169typedef unsigned char u8;
178170typedef unsigned short u16;
178171typedef unsigned int u32;
178172#endif
178173
178174/* The following macro is used to suppress compiler warnings.
178175*/
178176#ifndef UNUSED_PARAMETER
178177# define UNUSED_PARAMETER(x) (void)(x)
178178#endif
178179
178180typedef struct Rtree Rtree;
178181typedef struct RtreeCursor RtreeCursor;
178182typedef struct RtreeNode RtreeNode;
178183typedef struct RtreeCell RtreeCell;
178184typedef struct RtreeConstraint RtreeConstraint;
178185typedef struct RtreeMatchArg RtreeMatchArg;
178186typedef struct RtreeGeomCallback RtreeGeomCallback;
178187typedef union RtreeCoord RtreeCoord;
178188typedef struct RtreeSearchPoint RtreeSearchPoint;
178189
178190/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
178191#define RTREE_MAX_DIMENSIONS 5
178192
178193/* Maximum number of auxiliary columns */
178194#define RTREE_MAX_AUX_COLUMN 100
178195
178196/* Size of hash table Rtree.aHash. This hash table is not expected to
178197** ever contain very many entries, so a fixed number of buckets is
178198** used.
178199*/
178200#define HASHSIZE 97
178201
178202/* The xBestIndex method of this virtual table requires an estimate of
178203** the number of rows in the virtual table to calculate the costs of
178204** various strategies. If possible, this estimate is loaded from the
178205** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
178206** Otherwise, if no sqlite_stat1 entry is available, use
178207** RTREE_DEFAULT_ROWEST.
178208*/
178209#define RTREE_DEFAULT_ROWEST 1048576
178210#define RTREE_MIN_ROWEST 100
178211
178212/*
178213** An rtree virtual-table object.
178214*/
178215struct Rtree {
178216 sqlite3_vtab base; /* Base class. Must be first */
178217 sqlite3 *db; /* Host database connection */
178218 int iNodeSize; /* Size in bytes of each node in the node table */
178219 u8 nDim; /* Number of dimensions */
178220 u8 nDim2; /* Twice the number of dimensions */
178221 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
178222 u8 nBytesPerCell; /* Bytes consumed per cell */
178223 u8 inWrTrans; /* True if inside write transaction */
178224 u8 nAux; /* # of auxiliary columns in %_rowid */
178225 u8 nAuxNotNull; /* Number of initial not-null aux columns */
178226 int iDepth; /* Current depth of the r-tree structure */
178227 char *zDb; /* Name of database containing r-tree table */
178228 char *zName; /* Name of r-tree table */
178229 u32 nBusy; /* Current number of users of this structure */
178230 i64 nRowEst; /* Estimated number of rows in this table */
178231 u32 nCursor; /* Number of open cursors */
178232 u32 nNodeRef; /* Number RtreeNodes with positive nRef */
178233 char *zReadAuxSql; /* SQL for statement to read aux data */
178234
178235 /* List of nodes removed during a CondenseTree operation. List is
178236 ** linked together via the pointer normally used for hash chains -
178237 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
178238 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
178239 */
178240 RtreeNode *pDeleted;
178241 int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
178242
178243 /* Blob I/O on xxx_node */
178244 sqlite3_blob *pNodeBlob;
178245
178246 /* Statements to read/write/delete a record from xxx_node */
178247 sqlite3_stmt *pWriteNode;
178248 sqlite3_stmt *pDeleteNode;
178249
178250 /* Statements to read/write/delete a record from xxx_rowid */
178251 sqlite3_stmt *pReadRowid;
178252 sqlite3_stmt *pWriteRowid;
178253 sqlite3_stmt *pDeleteRowid;
178254
178255 /* Statements to read/write/delete a record from xxx_parent */
178256 sqlite3_stmt *pReadParent;
178257 sqlite3_stmt *pWriteParent;
178258 sqlite3_stmt *pDeleteParent;
178259
178260 /* Statement for writing to the "aux:" fields, if there are any */
178261 sqlite3_stmt *pWriteAux;
178262
178263 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
178264};
178265
178266/* Possible values for Rtree.eCoordType: */
178267#define RTREE_COORD_REAL32 0
178268#define RTREE_COORD_INT32 1
178269
178270/*
178271** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
178272** only deal with integer coordinates. No floating point operations
178273** will be done.
178274*/
178275#ifdef SQLITE_RTREE_INT_ONLY
178276 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
178277 typedef int RtreeValue; /* Low accuracy coordinate */
178278# define RTREE_ZERO 0
178279#else
178280 typedef double RtreeDValue; /* High accuracy coordinate */
178281 typedef float RtreeValue; /* Low accuracy coordinate */
178282# define RTREE_ZERO 0.0
178283#endif
178284
178285/*
178286** When doing a search of an r-tree, instances of the following structure
178287** record intermediate results from the tree walk.
178288**
178289** The id is always a node-id. For iLevel>=1 the id is the node-id of
178290** the node that the RtreeSearchPoint represents. When iLevel==0, however,
178291** the id is of the parent node and the cell that RtreeSearchPoint
178292** represents is the iCell-th entry in the parent node.
178293*/
178294struct RtreeSearchPoint {
178295 RtreeDValue rScore; /* The score for this node. Smallest goes first. */
178296 sqlite3_int64 id; /* Node ID */
178297 u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */
178298 u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */
178299 u8 iCell; /* Cell index within the node */
178300};
178301
178302/*
178303** The minimum number of cells allowed for a node is a third of the
178304** maximum. In Gutman's notation:
178305**
178306** m = M/3
178307**
178308** If an R*-tree "Reinsert" operation is required, the same number of
178309** cells are removed from the overfull node and reinserted into the tree.
178310*/
178311#define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
178312#define RTREE_REINSERT(p) RTREE_MINCELLS(p)
178313#define RTREE_MAXCELLS 51
178314
178315/*
178316** The smallest possible node-size is (512-64)==448 bytes. And the largest
178317** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
178318** Therefore all non-root nodes must contain at least 3 entries. Since
178319** 3^40 is greater than 2^64, an r-tree structure always has a depth of
178320** 40 or less.
178321*/
178322#define RTREE_MAX_DEPTH 40
178323
178324
178325/*
178326** Number of entries in the cursor RtreeNode cache. The first entry is
178327** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining
178328** entries cache the RtreeNode for the first elements of the priority queue.
178329*/
178330#define RTREE_CACHE_SZ 5
178331
178332/*
178333** An rtree cursor object.
178334*/
178335struct RtreeCursor {
178336 sqlite3_vtab_cursor base; /* Base class. Must be first */
178337 u8 atEOF; /* True if at end of search */
178338 u8 bPoint; /* True if sPoint is valid */
178339 u8 bAuxValid; /* True if pReadAux is valid */
178340 int iStrategy; /* Copy of idxNum search parameter */
178341 int nConstraint; /* Number of entries in aConstraint */
178342 RtreeConstraint *aConstraint; /* Search constraints. */
178343 int nPointAlloc; /* Number of slots allocated for aPoint[] */
178344 int nPoint; /* Number of slots used in aPoint[] */
178345 int mxLevel; /* iLevel value for root of the tree */
178346 RtreeSearchPoint *aPoint; /* Priority queue for search points */
178347 sqlite3_stmt *pReadAux; /* Statement to read aux-data */
178348 RtreeSearchPoint sPoint; /* Cached next search point */
178349 RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
178350 u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */
178351};
178352
178353/* Return the Rtree of a RtreeCursor */
178354#define RTREE_OF_CURSOR(X) ((Rtree*)((X)->base.pVtab))
178355
178356/*
178357** A coordinate can be either a floating point number or a integer. All
178358** coordinates within a single R-Tree are always of the same time.
178359*/
178360union RtreeCoord {
178361 RtreeValue f; /* Floating point value */
178362 int i; /* Integer value */
178363 u32 u; /* Unsigned for byte-order conversions */
178364};
178365
178366/*
178367** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
178368** formatted as a RtreeDValue (double or int64). This macro assumes that local
178369** variable pRtree points to the Rtree structure associated with the
178370** RtreeCoord.
178371*/
178372#ifdef SQLITE_RTREE_INT_ONLY
178373# define DCOORD(coord) ((RtreeDValue)coord.i)
178374#else
178375# define DCOORD(coord) ( \
178376 (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
178377 ((double)coord.f) : \
178378 ((double)coord.i) \
178379 )
178380#endif
178381
178382/*
178383** A search constraint.
178384*/
178385struct RtreeConstraint {
178386 int iCoord; /* Index of constrained coordinate */
178387 int op; /* Constraining operation */
178388 union {
178389 RtreeDValue rValue; /* Constraint value. */
178390 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
178391 int (*xQueryFunc)(sqlite3_rtree_query_info*);
178392 } u;
178393 sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */
178394};
178395
178396/* Possible values for RtreeConstraint.op */
178397#define RTREE_EQ 0x41 /* A */
178398#define RTREE_LE 0x42 /* B */
178399#define RTREE_LT 0x43 /* C */
178400#define RTREE_GE 0x44 /* D */
178401#define RTREE_GT 0x45 /* E */
178402#define RTREE_MATCH 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
178403#define RTREE_QUERY 0x47 /* G: New-style sqlite3_rtree_query_callback() */
178404
178405
178406/*
178407** An rtree structure node.
178408*/
178409struct RtreeNode {
178410 RtreeNode *pParent; /* Parent node */
178411 i64 iNode; /* The node number */
178412 int nRef; /* Number of references to this node */
178413 int isDirty; /* True if the node needs to be written to disk */
178414 u8 *zData; /* Content of the node, as should be on disk */
178415 RtreeNode *pNext; /* Next node in this hash collision chain */
178416};
178417
178418/* Return the number of cells in a node */
178419#define NCELL(pNode) readInt16(&(pNode)->zData[2])
178420
178421/*
178422** A single cell from a node, deserialized
178423*/
178424struct RtreeCell {
178425 i64 iRowid; /* Node or entry ID */
178426 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; /* Bounding box coordinates */
178427};
178428
178429
178430/*
178431** This object becomes the sqlite3_user_data() for the SQL functions
178432** that are created by sqlite3_rtree_geometry_callback() and
178433** sqlite3_rtree_query_callback() and which appear on the right of MATCH
178434** operators in order to constrain a search.
178435**
178436** xGeom and xQueryFunc are the callback functions. Exactly one of
178437** xGeom and xQueryFunc fields is non-NULL, depending on whether the
178438** SQL function was created using sqlite3_rtree_geometry_callback() or
178439** sqlite3_rtree_query_callback().
178440**
178441** This object is deleted automatically by the destructor mechanism in
178442** sqlite3_create_function_v2().
178443*/
178444struct RtreeGeomCallback {
178445 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
178446 int (*xQueryFunc)(sqlite3_rtree_query_info*);
178447 void (*xDestructor)(void*);
178448 void *pContext;
178449};
178450
178451/*
178452** An instance of this structure (in the form of a BLOB) is returned by
178453** the SQL functions that sqlite3_rtree_geometry_callback() and
178454** sqlite3_rtree_query_callback() create, and is read as the right-hand
178455** operand to the MATCH operator of an R-Tree.
178456*/
178457struct RtreeMatchArg {
178458 u32 iSize; /* Size of this object */
178459 RtreeGeomCallback cb; /* Info about the callback functions */
178460 int nParam; /* Number of parameters to the SQL function */
178461 sqlite3_value **apSqlParam; /* Original SQL parameter values */
178462 RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
178463};
178464
178465#ifndef MAX
178466# define MAX(x,y) ((x) < (y) ? (y) : (x))
178467#endif
178468#ifndef MIN
178469# define MIN(x,y) ((x) > (y) ? (y) : (x))
178470#endif
178471
178472/* What version of GCC is being used. 0 means GCC is not being used .
178473** Note that the GCC_VERSION macro will also be set correctly when using
178474** clang, since clang works hard to be gcc compatible. So the gcc
178475** optimizations will also work when compiling with clang.
178476*/
178477#ifndef GCC_VERSION
178478#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
178479# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
178480#else
178481# define GCC_VERSION 0
178482#endif
178483#endif
178484
178485/* The testcase() macro should already be defined in the amalgamation. If
178486** it is not, make it a no-op.
178487*/
178488#ifndef SQLITE_AMALGAMATION
178489# define testcase(X)
178490#endif
178491
178492/*
178493** Macros to determine whether the machine is big or little endian,
178494** and whether or not that determination is run-time or compile-time.
178495**
178496** For best performance, an attempt is made to guess at the byte-order
178497** using C-preprocessor macros. If that is unsuccessful, or if
178498** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
178499** at run-time.
178500*/
178501#ifndef SQLITE_BYTEORDER
178502#if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
178503 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
178504 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
178505 defined(__arm__)
178506# define SQLITE_BYTEORDER 1234
178507#elif defined(sparc) || defined(__ppc__)
178508# define SQLITE_BYTEORDER 4321
178509#else
178510# define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */
178511#endif
178512#endif
178513
178514
178515/* What version of MSVC is being used. 0 means MSVC is not being used */
178516#ifndef MSVC_VERSION
178517#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
178518# define MSVC_VERSION _MSC_VER
178519#else
178520# define MSVC_VERSION 0
178521#endif
178522#endif
178523
178524/*
178525** Functions to deserialize a 16 bit integer, 32 bit real number and
178526** 64 bit integer. The deserialized value is returned.
178527*/
178528static int readInt16(u8 *p){
178529 return (p[0]<<8) + p[1];
178530}
178531static void readCoord(u8 *p, RtreeCoord *pCoord){
178532 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
178533#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
178534 pCoord->u = _byteswap_ulong(*(u32*)p);
178535#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
178536 pCoord->u = __builtin_bswap32(*(u32*)p);
178537#elif SQLITE_BYTEORDER==4321
178538 pCoord->u = *(u32*)p;
178539#else
178540 pCoord->u = (
178541 (((u32)p[0]) << 24) +
178542 (((u32)p[1]) << 16) +
178543 (((u32)p[2]) << 8) +
178544 (((u32)p[3]) << 0)
178545 );
178546#endif
178547}
178548static i64 readInt64(u8 *p){
178549#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
178550 u64 x;
178551 memcpy(&x, p, 8);
178552 return (i64)_byteswap_uint64(x);
178553#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
178554 u64 x;
178555 memcpy(&x, p, 8);
178556 return (i64)__builtin_bswap64(x);
178557#elif SQLITE_BYTEORDER==4321
178558 i64 x;
178559 memcpy(&x, p, 8);
178560 return x;
178561#else
178562 return (i64)(
178563 (((u64)p[0]) << 56) +
178564 (((u64)p[1]) << 48) +
178565 (((u64)p[2]) << 40) +
178566 (((u64)p[3]) << 32) +
178567 (((u64)p[4]) << 24) +
178568 (((u64)p[5]) << 16) +
178569 (((u64)p[6]) << 8) +
178570 (((u64)p[7]) << 0)
178571 );
178572#endif
178573}
178574
178575/*
178576** Functions to serialize a 16 bit integer, 32 bit real number and
178577** 64 bit integer. The value returned is the number of bytes written
178578** to the argument buffer (always 2, 4 and 8 respectively).
178579*/
178580static void writeInt16(u8 *p, int i){
178581 p[0] = (i>> 8)&0xFF;
178582 p[1] = (i>> 0)&0xFF;
178583}
178584static int writeCoord(u8 *p, RtreeCoord *pCoord){
178585 u32 i;
178586 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */
178587 assert( sizeof(RtreeCoord)==4 );
178588 assert( sizeof(u32)==4 );
178589#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
178590 i = __builtin_bswap32(pCoord->u);
178591 memcpy(p, &i, 4);
178592#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
178593 i = _byteswap_ulong(pCoord->u);
178594 memcpy(p, &i, 4);
178595#elif SQLITE_BYTEORDER==4321
178596 i = pCoord->u;
178597 memcpy(p, &i, 4);
178598#else
178599 i = pCoord->u;
178600 p[0] = (i>>24)&0xFF;
178601 p[1] = (i>>16)&0xFF;
178602 p[2] = (i>> 8)&0xFF;
178603 p[3] = (i>> 0)&0xFF;
178604#endif
178605 return 4;
178606}
178607static int writeInt64(u8 *p, i64 i){
178608#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
178609 i = (i64)__builtin_bswap64((u64)i);
178610 memcpy(p, &i, 8);
178611#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
178612 i = (i64)_byteswap_uint64((u64)i);
178613 memcpy(p, &i, 8);
178614#elif SQLITE_BYTEORDER==4321
178615 memcpy(p, &i, 8);
178616#else
178617 p[0] = (i>>56)&0xFF;
178618 p[1] = (i>>48)&0xFF;
178619 p[2] = (i>>40)&0xFF;
178620 p[3] = (i>>32)&0xFF;
178621 p[4] = (i>>24)&0xFF;
178622 p[5] = (i>>16)&0xFF;
178623 p[6] = (i>> 8)&0xFF;
178624 p[7] = (i>> 0)&0xFF;
178625#endif
178626 return 8;
178627}
178628
178629/*
178630** Increment the reference count of node p.
178631*/
178632static void nodeReference(RtreeNode *p){
178633 if( p ){
178634 assert( p->nRef>0 );
178635 p->nRef++;
178636 }
178637}
178638
178639/*
178640** Clear the content of node p (set all bytes to 0x00).
178641*/
178642static void nodeZero(Rtree *pRtree, RtreeNode *p){
178643 memset(&p->zData[2], 0, pRtree->iNodeSize-2);
178644 p->isDirty = 1;
178645}
178646
178647/*
178648** Given a node number iNode, return the corresponding key to use
178649** in the Rtree.aHash table.
178650*/
178651static int nodeHash(i64 iNode){
178652 return iNode % HASHSIZE;
178653}
178654
178655/*
178656** Search the node hash table for node iNode. If found, return a pointer
178657** to it. Otherwise, return 0.
178658*/
178659static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
178660 RtreeNode *p;
178661 for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
178662 return p;
178663}
178664
178665/*
178666** Add node pNode to the node hash table.
178667*/
178668static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
178669 int iHash;
178670 assert( pNode->pNext==0 );
178671 iHash = nodeHash(pNode->iNode);
178672 pNode->pNext = pRtree->aHash[iHash];
178673 pRtree->aHash[iHash] = pNode;
178674}
178675
178676/*
178677** Remove node pNode from the node hash table.
178678*/
178679static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
178680 RtreeNode **pp;
178681 if( pNode->iNode!=0 ){
178682 pp = &pRtree->aHash[nodeHash(pNode->iNode)];
178683 for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
178684 *pp = pNode->pNext;
178685 pNode->pNext = 0;
178686 }
178687}
178688
178689/*
178690** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
178691** indicating that node has not yet been assigned a node number. It is
178692** assigned a node number when nodeWrite() is called to write the
178693** node contents out to the database.
178694*/
178695static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
178696 RtreeNode *pNode;
178697 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
178698 if( pNode ){
178699 memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
178700 pNode->zData = (u8 *)&pNode[1];
178701 pNode->nRef = 1;
178702 pRtree->nNodeRef++;
178703 pNode->pParent = pParent;
178704 pNode->isDirty = 1;
178705 nodeReference(pParent);
178706 }
178707 return pNode;
178708}
178709
178710/*
178711** Clear the Rtree.pNodeBlob object
178712*/
178713static void nodeBlobReset(Rtree *pRtree){
178714 if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
178715 sqlite3_blob *pBlob = pRtree->pNodeBlob;
178716 pRtree->pNodeBlob = 0;
178717 sqlite3_blob_close(pBlob);
178718 }
178719}
178720
178721/*
178722** Obtain a reference to an r-tree node.
178723*/
178724static int nodeAcquire(
178725 Rtree *pRtree, /* R-tree structure */
178726 i64 iNode, /* Node number to load */
178727 RtreeNode *pParent, /* Either the parent node or NULL */
178728 RtreeNode **ppNode /* OUT: Acquired node */
178729){
178730 int rc = SQLITE_OK;
178731 RtreeNode *pNode = 0;
178732
178733 /* Check if the requested node is already in the hash table. If so,
178734 ** increase its reference count and return it.
178735 */
178736 if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
178737 assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
178738 if( pParent && !pNode->pParent ){
178739 pParent->nRef++;
178740 pNode->pParent = pParent;
178741 }
178742 pNode->nRef++;
178743 *ppNode = pNode;
178744 return SQLITE_OK;
178745 }
178746
178747 if( pRtree->pNodeBlob ){
178748 sqlite3_blob *pBlob = pRtree->pNodeBlob;
178749 pRtree->pNodeBlob = 0;
178750 rc = sqlite3_blob_reopen(pBlob, iNode);
178751 pRtree->pNodeBlob = pBlob;
178752 if( rc ){
178753 nodeBlobReset(pRtree);
178754 if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
178755 }
178756 }
178757 if( pRtree->pNodeBlob==0 ){
178758 char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
178759 if( zTab==0 ) return SQLITE_NOMEM;
178760 rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
178761 &pRtree->pNodeBlob);
178762 sqlite3_free(zTab);
178763 }
178764 if( rc ){
178765 nodeBlobReset(pRtree);
178766 *ppNode = 0;
178767 /* If unable to open an sqlite3_blob on the desired row, that can only
178768 ** be because the shadow tables hold erroneous data. */
178769 if( rc==SQLITE_ERROR ) rc = SQLITE_CORRUPT_VTAB;
178770 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
178771 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
178772 if( !pNode ){
178773 rc = SQLITE_NOMEM;
178774 }else{
178775 pNode->pParent = pParent;
178776 pNode->zData = (u8 *)&pNode[1];
178777 pNode->nRef = 1;
178778 pRtree->nNodeRef++;
178779 pNode->iNode = iNode;
178780 pNode->isDirty = 0;
178781 pNode->pNext = 0;
178782 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
178783 pRtree->iNodeSize, 0);
178784 nodeReference(pParent);
178785 }
178786 }
178787
178788 /* If the root node was just loaded, set pRtree->iDepth to the height
178789 ** of the r-tree structure. A height of zero means all data is stored on
178790 ** the root node. A height of one means the children of the root node
178791 ** are the leaves, and so on. If the depth as specified on the root node
178792 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
178793 */
178794 if( pNode && iNode==1 ){
178795 pRtree->iDepth = readInt16(pNode->zData);
178796 if( pRtree->iDepth>RTREE_MAX_DEPTH ){
178797 rc = SQLITE_CORRUPT_VTAB;
178798 }
178799 }
178800
178801 /* If no error has occurred so far, check if the "number of entries"
178802 ** field on the node is too large. If so, set the return code to
178803 ** SQLITE_CORRUPT_VTAB.
178804 */
178805 if( pNode && rc==SQLITE_OK ){
178806 if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
178807 rc = SQLITE_CORRUPT_VTAB;
178808 }
178809 }
178810
178811 if( rc==SQLITE_OK ){
178812 if( pNode!=0 ){
178813 nodeHashInsert(pRtree, pNode);
178814 }else{
178815 rc = SQLITE_CORRUPT_VTAB;
178816 }
178817 *ppNode = pNode;
178818 }else{
178819 if( pNode ){
178820 pRtree->nNodeRef--;
178821 sqlite3_free(pNode);
178822 }
178823 *ppNode = 0;
178824 }
178825
178826 return rc;
178827}
178828
178829/*
178830** Overwrite cell iCell of node pNode with the contents of pCell.
178831*/
178832static void nodeOverwriteCell(
178833 Rtree *pRtree, /* The overall R-Tree */
178834 RtreeNode *pNode, /* The node into which the cell is to be written */
178835 RtreeCell *pCell, /* The cell to write */
178836 int iCell /* Index into pNode into which pCell is written */
178837){
178838 int ii;
178839 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
178840 p += writeInt64(p, pCell->iRowid);
178841 for(ii=0; ii<pRtree->nDim2; ii++){
178842 p += writeCoord(p, &pCell->aCoord[ii]);
178843 }
178844 pNode->isDirty = 1;
178845}
178846
178847/*
178848** Remove the cell with index iCell from node pNode.
178849*/
178850static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
178851 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
178852 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
178853 int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
178854 memmove(pDst, pSrc, nByte);
178855 writeInt16(&pNode->zData[2], NCELL(pNode)-1);
178856 pNode->isDirty = 1;
178857}
178858
178859/*
178860** Insert the contents of cell pCell into node pNode. If the insert
178861** is successful, return SQLITE_OK.
178862**
178863** If there is not enough free space in pNode, return SQLITE_FULL.
178864*/
178865static int nodeInsertCell(
178866 Rtree *pRtree, /* The overall R-Tree */
178867 RtreeNode *pNode, /* Write new cell into this node */
178868 RtreeCell *pCell /* The cell to be inserted */
178869){
178870 int nCell; /* Current number of cells in pNode */
178871 int nMaxCell; /* Maximum number of cells for pNode */
178872
178873 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
178874 nCell = NCELL(pNode);
178875
178876 assert( nCell<=nMaxCell );
178877 if( nCell<nMaxCell ){
178878 nodeOverwriteCell(pRtree, pNode, pCell, nCell);
178879 writeInt16(&pNode->zData[2], nCell+1);
178880 pNode->isDirty = 1;
178881 }
178882
178883 return (nCell==nMaxCell);
178884}
178885
178886/*
178887** If the node is dirty, write it out to the database.
178888*/
178889static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
178890 int rc = SQLITE_OK;
178891 if( pNode->isDirty ){
178892 sqlite3_stmt *p = pRtree->pWriteNode;
178893 if( pNode->iNode ){
178894 sqlite3_bind_int64(p, 1, pNode->iNode);
178895 }else{
178896 sqlite3_bind_null(p, 1);
178897 }
178898 sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
178899 sqlite3_step(p);
178900 pNode->isDirty = 0;
178901 rc = sqlite3_reset(p);
178902 sqlite3_bind_null(p, 2);
178903 if( pNode->iNode==0 && rc==SQLITE_OK ){
178904 pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
178905 nodeHashInsert(pRtree, pNode);
178906 }
178907 }
178908 return rc;
178909}
178910
178911/*
178912** Release a reference to a node. If the node is dirty and the reference
178913** count drops to zero, the node data is written to the database.
178914*/
178915static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
178916 int rc = SQLITE_OK;
178917 if( pNode ){
178918 assert( pNode->nRef>0 );
178919 assert( pRtree->nNodeRef>0 );
178920 pNode->nRef--;
178921 if( pNode->nRef==0 ){
178922 pRtree->nNodeRef--;
178923 if( pNode->iNode==1 ){
178924 pRtree->iDepth = -1;
178925 }
178926 if( pNode->pParent ){
178927 rc = nodeRelease(pRtree, pNode->pParent);
178928 }
178929 if( rc==SQLITE_OK ){
178930 rc = nodeWrite(pRtree, pNode);
178931 }
178932 nodeHashDelete(pRtree, pNode);
178933 sqlite3_free(pNode);
178934 }
178935 }
178936 return rc;
178937}
178938
178939/*
178940** Return the 64-bit integer value associated with cell iCell of
178941** node pNode. If pNode is a leaf node, this is a rowid. If it is
178942** an internal node, then the 64-bit integer is a child page number.
178943*/
178944static i64 nodeGetRowid(
178945 Rtree *pRtree, /* The overall R-Tree */
178946 RtreeNode *pNode, /* The node from which to extract the ID */
178947 int iCell /* The cell index from which to extract the ID */
178948){
178949 assert( iCell<NCELL(pNode) );
178950 return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
178951}
178952
178953/*
178954** Return coordinate iCoord from cell iCell in node pNode.
178955*/
178956static void nodeGetCoord(
178957 Rtree *pRtree, /* The overall R-Tree */
178958 RtreeNode *pNode, /* The node from which to extract a coordinate */
178959 int iCell, /* The index of the cell within the node */
178960 int iCoord, /* Which coordinate to extract */
178961 RtreeCoord *pCoord /* OUT: Space to write result to */
178962){
178963 readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
178964}
178965
178966/*
178967** Deserialize cell iCell of node pNode. Populate the structure pointed
178968** to by pCell with the results.
178969*/
178970static void nodeGetCell(
178971 Rtree *pRtree, /* The overall R-Tree */
178972 RtreeNode *pNode, /* The node containing the cell to be read */
178973 int iCell, /* Index of the cell within the node */
178974 RtreeCell *pCell /* OUT: Write the cell contents here */
178975){
178976 u8 *pData;
178977 RtreeCoord *pCoord;
178978 int ii = 0;
178979 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
178980 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
178981 pCoord = pCell->aCoord;
178982 do{
178983 readCoord(pData, &pCoord[ii]);
178984 readCoord(pData+4, &pCoord[ii+1]);
178985 pData += 8;
178986 ii += 2;
178987 }while( ii<pRtree->nDim2 );
178988}
178989
178990
178991/* Forward declaration for the function that does the work of
178992** the virtual table module xCreate() and xConnect() methods.
178993*/
178994static int rtreeInit(
178995 sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
178996);
178997
178998/*
178999** Rtree virtual table module xCreate method.
179000*/
179001static int rtreeCreate(
179002 sqlite3 *db,
179003 void *pAux,
179004 int argc, const char *const*argv,
179005 sqlite3_vtab **ppVtab,
179006 char **pzErr
179007){
179008 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
179009}
179010
179011/*
179012** Rtree virtual table module xConnect method.
179013*/
179014static int rtreeConnect(
179015 sqlite3 *db,
179016 void *pAux,
179017 int argc, const char *const*argv,
179018 sqlite3_vtab **ppVtab,
179019 char **pzErr
179020){
179021 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
179022}
179023
179024/*
179025** Increment the r-tree reference count.
179026*/
179027static void rtreeReference(Rtree *pRtree){
179028 pRtree->nBusy++;
179029}
179030
179031/*
179032** Decrement the r-tree reference count. When the reference count reaches
179033** zero the structure is deleted.
179034*/
179035static void rtreeRelease(Rtree *pRtree){
179036 pRtree->nBusy--;
179037 if( pRtree->nBusy==0 ){
179038 pRtree->inWrTrans = 0;
179039 assert( pRtree->nCursor==0 );
179040 nodeBlobReset(pRtree);
179041 assert( pRtree->nNodeRef==0 );
179042 sqlite3_finalize(pRtree->pWriteNode);
179043 sqlite3_finalize(pRtree->pDeleteNode);
179044 sqlite3_finalize(pRtree->pReadRowid);
179045 sqlite3_finalize(pRtree->pWriteRowid);
179046 sqlite3_finalize(pRtree->pDeleteRowid);
179047 sqlite3_finalize(pRtree->pReadParent);
179048 sqlite3_finalize(pRtree->pWriteParent);
179049 sqlite3_finalize(pRtree->pDeleteParent);
179050 sqlite3_finalize(pRtree->pWriteAux);
179051 sqlite3_free(pRtree->zReadAuxSql);
179052 sqlite3_free(pRtree);
179053 }
179054}
179055
179056/*
179057** Rtree virtual table module xDisconnect method.
179058*/
179059static int rtreeDisconnect(sqlite3_vtab *pVtab){
179060 rtreeRelease((Rtree *)pVtab);
179061 return SQLITE_OK;
179062}
179063
179064/*
179065** Rtree virtual table module xDestroy method.
179066*/
179067static int rtreeDestroy(sqlite3_vtab *pVtab){
179068 Rtree *pRtree = (Rtree *)pVtab;
179069 int rc;
179070 char *zCreate = sqlite3_mprintf(
179071 "DROP TABLE '%q'.'%q_node';"
179072 "DROP TABLE '%q'.'%q_rowid';"
179073 "DROP TABLE '%q'.'%q_parent';",
179074 pRtree->zDb, pRtree->zName,
179075 pRtree->zDb, pRtree->zName,
179076 pRtree->zDb, pRtree->zName
179077 );
179078 if( !zCreate ){
179079 rc = SQLITE_NOMEM;
179080 }else{
179081 nodeBlobReset(pRtree);
179082 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
179083 sqlite3_free(zCreate);
179084 }
179085 if( rc==SQLITE_OK ){
179086 rtreeRelease(pRtree);
179087 }
179088
179089 return rc;
179090}
179091
179092/*
179093** Rtree virtual table module xOpen method.
179094*/
179095static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
179096 int rc = SQLITE_NOMEM;
179097 Rtree *pRtree = (Rtree *)pVTab;
179098 RtreeCursor *pCsr;
179099
179100 pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
179101 if( pCsr ){
179102 memset(pCsr, 0, sizeof(RtreeCursor));
179103 pCsr->base.pVtab = pVTab;
179104 rc = SQLITE_OK;
179105 pRtree->nCursor++;
179106 }
179107 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
179108
179109 return rc;
179110}
179111
179112
179113/*
179114** Free the RtreeCursor.aConstraint[] array and its contents.
179115*/
179116static void freeCursorConstraints(RtreeCursor *pCsr){
179117 if( pCsr->aConstraint ){
179118 int i; /* Used to iterate through constraint array */
179119 for(i=0; i<pCsr->nConstraint; i++){
179120 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
179121 if( pInfo ){
179122 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
179123 sqlite3_free(pInfo);
179124 }
179125 }
179126 sqlite3_free(pCsr->aConstraint);
179127 pCsr->aConstraint = 0;
179128 }
179129}
179130
179131/*
179132** Rtree virtual table module xClose method.
179133*/
179134static int rtreeClose(sqlite3_vtab_cursor *cur){
179135 Rtree *pRtree = (Rtree *)(cur->pVtab);
179136 int ii;
179137 RtreeCursor *pCsr = (RtreeCursor *)cur;
179138 assert( pRtree->nCursor>0 );
179139 freeCursorConstraints(pCsr);
179140 sqlite3_finalize(pCsr->pReadAux);
179141 sqlite3_free(pCsr->aPoint);
179142 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
179143 sqlite3_free(pCsr);
179144 pRtree->nCursor--;
179145 nodeBlobReset(pRtree);
179146 return SQLITE_OK;
179147}
179148
179149/*
179150** Rtree virtual table module xEof method.
179151**
179152** Return non-zero if the cursor does not currently point to a valid
179153** record (i.e if the scan has finished), or zero otherwise.
179154*/
179155static int rtreeEof(sqlite3_vtab_cursor *cur){
179156 RtreeCursor *pCsr = (RtreeCursor *)cur;
179157 return pCsr->atEOF;
179158}
179159
179160/*
179161** Convert raw bits from the on-disk RTree record into a coordinate value.
179162** The on-disk format is big-endian and needs to be converted for little-
179163** endian platforms. The on-disk record stores integer coordinates if
179164** eInt is true and it stores 32-bit floating point records if eInt is
179165** false. a[] is the four bytes of the on-disk record to be decoded.
179166** Store the results in "r".
179167**
179168** There are five versions of this macro. The last one is generic. The
179169** other four are various architectures-specific optimizations.
179170*/
179171#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
179172#define RTREE_DECODE_COORD(eInt, a, r) { \
179173 RtreeCoord c; /* Coordinate decoded */ \
179174 c.u = _byteswap_ulong(*(u32*)a); \
179175 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
179176}
179177#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
179178#define RTREE_DECODE_COORD(eInt, a, r) { \
179179 RtreeCoord c; /* Coordinate decoded */ \
179180 c.u = __builtin_bswap32(*(u32*)a); \
179181 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
179182}
179183#elif SQLITE_BYTEORDER==1234
179184#define RTREE_DECODE_COORD(eInt, a, r) { \
179185 RtreeCoord c; /* Coordinate decoded */ \
179186 memcpy(&c.u,a,4); \
179187 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
179188 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
179189 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
179190}
179191#elif SQLITE_BYTEORDER==4321
179192#define RTREE_DECODE_COORD(eInt, a, r) { \
179193 RtreeCoord c; /* Coordinate decoded */ \
179194 memcpy(&c.u,a,4); \
179195 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
179196}
179197#else
179198#define RTREE_DECODE_COORD(eInt, a, r) { \
179199 RtreeCoord c; /* Coordinate decoded */ \
179200 c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \
179201 +((u32)a[2]<<8) + a[3]; \
179202 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
179203}
179204#endif
179205
179206/*
179207** Check the RTree node or entry given by pCellData and p against the MATCH
179208** constraint pConstraint.
179209*/
179210static int rtreeCallbackConstraint(
179211 RtreeConstraint *pConstraint, /* The constraint to test */
179212 int eInt, /* True if RTree holding integer coordinates */
179213 u8 *pCellData, /* Raw cell content */
179214 RtreeSearchPoint *pSearch, /* Container of this cell */
179215 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
179216 int *peWithin /* OUT: visibility of the cell */
179217){
179218 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
179219 int nCoord = pInfo->nCoord; /* No. of coordinates */
179220 int rc; /* Callback return code */
179221 RtreeCoord c; /* Translator union */
179222 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
179223
179224 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
179225 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
179226
179227 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
179228 pInfo->iRowid = readInt64(pCellData);
179229 }
179230 pCellData += 8;
179231#ifndef SQLITE_RTREE_INT_ONLY
179232 if( eInt==0 ){
179233 switch( nCoord ){
179234 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f;
179235 readCoord(pCellData+32, &c); aCoord[8] = c.f;
179236 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f;
179237 readCoord(pCellData+24, &c); aCoord[6] = c.f;
179238 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f;
179239 readCoord(pCellData+16, &c); aCoord[4] = c.f;
179240 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f;
179241 readCoord(pCellData+8, &c); aCoord[2] = c.f;
179242 default: readCoord(pCellData+4, &c); aCoord[1] = c.f;
179243 readCoord(pCellData, &c); aCoord[0] = c.f;
179244 }
179245 }else
179246#endif
179247 {
179248 switch( nCoord ){
179249 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i;
179250 readCoord(pCellData+32, &c); aCoord[8] = c.i;
179251 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i;
179252 readCoord(pCellData+24, &c); aCoord[6] = c.i;
179253 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i;
179254 readCoord(pCellData+16, &c); aCoord[4] = c.i;
179255 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i;
179256 readCoord(pCellData+8, &c); aCoord[2] = c.i;
179257 default: readCoord(pCellData+4, &c); aCoord[1] = c.i;
179258 readCoord(pCellData, &c); aCoord[0] = c.i;
179259 }
179260 }
179261 if( pConstraint->op==RTREE_MATCH ){
179262 int eWithin = 0;
179263 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
179264 nCoord, aCoord, &eWithin);
179265 if( eWithin==0 ) *peWithin = NOT_WITHIN;
179266 *prScore = RTREE_ZERO;
179267 }else{
179268 pInfo->aCoord = aCoord;
179269 pInfo->iLevel = pSearch->iLevel - 1;
179270 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
179271 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
179272 rc = pConstraint->u.xQueryFunc(pInfo);
179273 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
179274 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
179275 *prScore = pInfo->rScore;
179276 }
179277 }
179278 return rc;
179279}
179280
179281/*
179282** Check the internal RTree node given by pCellData against constraint p.
179283** If this constraint cannot be satisfied by any child within the node,
179284** set *peWithin to NOT_WITHIN.
179285*/
179286static void rtreeNonleafConstraint(
179287 RtreeConstraint *p, /* The constraint to test */
179288 int eInt, /* True if RTree holds integer coordinates */
179289 u8 *pCellData, /* Raw cell content as appears on disk */
179290 int *peWithin /* Adjust downward, as appropriate */
179291){
179292 sqlite3_rtree_dbl val; /* Coordinate value convert to a double */
179293
179294 /* p->iCoord might point to either a lower or upper bound coordinate
179295 ** in a coordinate pair. But make pCellData point to the lower bound.
179296 */
179297 pCellData += 8 + 4*(p->iCoord&0xfe);
179298
179299 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
179300 || p->op==RTREE_GT || p->op==RTREE_EQ );
179301 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
179302 switch( p->op ){
179303 case RTREE_LE:
179304 case RTREE_LT:
179305 case RTREE_EQ:
179306 RTREE_DECODE_COORD(eInt, pCellData, val);
179307 /* val now holds the lower bound of the coordinate pair */
179308 if( p->u.rValue>=val ) return;
179309 if( p->op!=RTREE_EQ ) break; /* RTREE_LE and RTREE_LT end here */
179310 /* Fall through for the RTREE_EQ case */
179311
179312 default: /* RTREE_GT or RTREE_GE, or fallthrough of RTREE_EQ */
179313 pCellData += 4;
179314 RTREE_DECODE_COORD(eInt, pCellData, val);
179315 /* val now holds the upper bound of the coordinate pair */
179316 if( p->u.rValue<=val ) return;
179317 }
179318 *peWithin = NOT_WITHIN;
179319}
179320
179321/*
179322** Check the leaf RTree cell given by pCellData against constraint p.
179323** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
179324** If the constraint is satisfied, leave *peWithin unchanged.
179325**
179326** The constraint is of the form: xN op $val
179327**
179328** The op is given by p->op. The xN is p->iCoord-th coordinate in
179329** pCellData. $val is given by p->u.rValue.
179330*/
179331static void rtreeLeafConstraint(
179332 RtreeConstraint *p, /* The constraint to test */
179333 int eInt, /* True if RTree holds integer coordinates */
179334 u8 *pCellData, /* Raw cell content as appears on disk */
179335 int *peWithin /* Adjust downward, as appropriate */
179336){
179337 RtreeDValue xN; /* Coordinate value converted to a double */
179338
179339 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
179340 || p->op==RTREE_GT || p->op==RTREE_EQ );
179341 pCellData += 8 + p->iCoord*4;
179342 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */
179343 RTREE_DECODE_COORD(eInt, pCellData, xN);
179344 switch( p->op ){
179345 case RTREE_LE: if( xN <= p->u.rValue ) return; break;
179346 case RTREE_LT: if( xN < p->u.rValue ) return; break;
179347 case RTREE_GE: if( xN >= p->u.rValue ) return; break;
179348 case RTREE_GT: if( xN > p->u.rValue ) return; break;
179349 default: if( xN == p->u.rValue ) return; break;
179350 }
179351 *peWithin = NOT_WITHIN;
179352}
179353
179354/*
179355** One of the cells in node pNode is guaranteed to have a 64-bit
179356** integer value equal to iRowid. Return the index of this cell.
179357*/
179358static int nodeRowidIndex(
179359 Rtree *pRtree,
179360 RtreeNode *pNode,
179361 i64 iRowid,
179362 int *piIndex
179363){
179364 int ii;
179365 int nCell = NCELL(pNode);
179366 assert( nCell<200 );
179367 for(ii=0; ii<nCell; ii++){
179368 if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
179369 *piIndex = ii;
179370 return SQLITE_OK;
179371 }
179372 }
179373 return SQLITE_CORRUPT_VTAB;
179374}
179375
179376/*
179377** Return the index of the cell containing a pointer to node pNode
179378** in its parent. If pNode is the root node, return -1.
179379*/
179380static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
179381 RtreeNode *pParent = pNode->pParent;
179382 if( pParent ){
179383 return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
179384 }
179385 *piIndex = -1;
179386 return SQLITE_OK;
179387}
179388
179389/*
179390** Compare two search points. Return negative, zero, or positive if the first
179391** is less than, equal to, or greater than the second.
179392**
179393** The rScore is the primary key. Smaller rScore values come first.
179394** If the rScore is a tie, then use iLevel as the tie breaker with smaller
179395** iLevel values coming first. In this way, if rScore is the same for all
179396** SearchPoints, then iLevel becomes the deciding factor and the result
179397** is a depth-first search, which is the desired default behavior.
179398*/
179399static int rtreeSearchPointCompare(
179400 const RtreeSearchPoint *pA,
179401 const RtreeSearchPoint *pB
179402){
179403 if( pA->rScore<pB->rScore ) return -1;
179404 if( pA->rScore>pB->rScore ) return +1;
179405 if( pA->iLevel<pB->iLevel ) return -1;
179406 if( pA->iLevel>pB->iLevel ) return +1;
179407 return 0;
179408}
179409
179410/*
179411** Interchange two search points in a cursor.
179412*/
179413static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
179414 RtreeSearchPoint t = p->aPoint[i];
179415 assert( i<j );
179416 p->aPoint[i] = p->aPoint[j];
179417 p->aPoint[j] = t;
179418 i++; j++;
179419 if( i<RTREE_CACHE_SZ ){
179420 if( j>=RTREE_CACHE_SZ ){
179421 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
179422 p->aNode[i] = 0;
179423 }else{
179424 RtreeNode *pTemp = p->aNode[i];
179425 p->aNode[i] = p->aNode[j];
179426 p->aNode[j] = pTemp;
179427 }
179428 }
179429}
179430
179431/*
179432** Return the search point with the lowest current score.
179433*/
179434static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
179435 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
179436}
179437
179438/*
179439** Get the RtreeNode for the search point with the lowest score.
179440*/
179441static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
179442 sqlite3_int64 id;
179443 int ii = 1 - pCur->bPoint;
179444 assert( ii==0 || ii==1 );
179445 assert( pCur->bPoint || pCur->nPoint );
179446 if( pCur->aNode[ii]==0 ){
179447 assert( pRC!=0 );
179448 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
179449 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
179450 }
179451 return pCur->aNode[ii];
179452}
179453
179454/*
179455** Push a new element onto the priority queue
179456*/
179457static RtreeSearchPoint *rtreeEnqueue(
179458 RtreeCursor *pCur, /* The cursor */
179459 RtreeDValue rScore, /* Score for the new search point */
179460 u8 iLevel /* Level for the new search point */
179461){
179462 int i, j;
179463 RtreeSearchPoint *pNew;
179464 if( pCur->nPoint>=pCur->nPointAlloc ){
179465 int nNew = pCur->nPointAlloc*2 + 8;
179466 pNew = sqlite3_realloc(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
179467 if( pNew==0 ) return 0;
179468 pCur->aPoint = pNew;
179469 pCur->nPointAlloc = nNew;
179470 }
179471 i = pCur->nPoint++;
179472 pNew = pCur->aPoint + i;
179473 pNew->rScore = rScore;
179474 pNew->iLevel = iLevel;
179475 assert( iLevel<=RTREE_MAX_DEPTH );
179476 while( i>0 ){
179477 RtreeSearchPoint *pParent;
179478 j = (i-1)/2;
179479 pParent = pCur->aPoint + j;
179480 if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break;
179481 rtreeSearchPointSwap(pCur, j, i);
179482 i = j;
179483 pNew = pParent;
179484 }
179485 return pNew;
179486}
179487
179488/*
179489** Allocate a new RtreeSearchPoint and return a pointer to it. Return
179490** NULL if malloc fails.
179491*/
179492static RtreeSearchPoint *rtreeSearchPointNew(
179493 RtreeCursor *pCur, /* The cursor */
179494 RtreeDValue rScore, /* Score for the new search point */
179495 u8 iLevel /* Level for the new search point */
179496){
179497 RtreeSearchPoint *pNew, *pFirst;
179498 pFirst = rtreeSearchPointFirst(pCur);
179499 pCur->anQueue[iLevel]++;
179500 if( pFirst==0
179501 || pFirst->rScore>rScore
179502 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
179503 ){
179504 if( pCur->bPoint ){
179505 int ii;
179506 pNew = rtreeEnqueue(pCur, rScore, iLevel);
179507 if( pNew==0 ) return 0;
179508 ii = (int)(pNew - pCur->aPoint) + 1;
179509 if( ii<RTREE_CACHE_SZ ){
179510 assert( pCur->aNode[ii]==0 );
179511 pCur->aNode[ii] = pCur->aNode[0];
179512 }else{
179513 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
179514 }
179515 pCur->aNode[0] = 0;
179516 *pNew = pCur->sPoint;
179517 }
179518 pCur->sPoint.rScore = rScore;
179519 pCur->sPoint.iLevel = iLevel;
179520 pCur->bPoint = 1;
179521 return &pCur->sPoint;
179522 }else{
179523 return rtreeEnqueue(pCur, rScore, iLevel);
179524 }
179525}
179526
179527#if 0
179528/* Tracing routines for the RtreeSearchPoint queue */
179529static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
179530 if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
179531 printf(" %d.%05lld.%02d %g %d",
179532 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
179533 );
179534 idx++;
179535 if( idx<RTREE_CACHE_SZ ){
179536 printf(" %p\n", pCur->aNode[idx]);
179537 }else{
179538 printf("\n");
179539 }
179540}
179541static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
179542 int ii;
179543 printf("=== %9s ", zPrefix);
179544 if( pCur->bPoint ){
179545 tracePoint(&pCur->sPoint, -1, pCur);
179546 }
179547 for(ii=0; ii<pCur->nPoint; ii++){
179548 if( ii>0 || pCur->bPoint ) printf(" ");
179549 tracePoint(&pCur->aPoint[ii], ii, pCur);
179550 }
179551}
179552# define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
179553#else
179554# define RTREE_QUEUE_TRACE(A,B) /* no-op */
179555#endif
179556
179557/* Remove the search point with the lowest current score.
179558*/
179559static void rtreeSearchPointPop(RtreeCursor *p){
179560 int i, j, k, n;
179561 i = 1 - p->bPoint;
179562 assert( i==0 || i==1 );
179563 if( p->aNode[i] ){
179564 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
179565 p->aNode[i] = 0;
179566 }
179567 if( p->bPoint ){
179568 p->anQueue[p->sPoint.iLevel]--;
179569 p->bPoint = 0;
179570 }else if( p->nPoint ){
179571 p->anQueue[p->aPoint[0].iLevel]--;
179572 n = --p->nPoint;
179573 p->aPoint[0] = p->aPoint[n];
179574 if( n<RTREE_CACHE_SZ-1 ){
179575 p->aNode[1] = p->aNode[n+1];
179576 p->aNode[n+1] = 0;
179577 }
179578 i = 0;
179579 while( (j = i*2+1)<n ){
179580 k = j+1;
179581 if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
179582 if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
179583 rtreeSearchPointSwap(p, i, k);
179584 i = k;
179585 }else{
179586 break;
179587 }
179588 }else{
179589 if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
179590 rtreeSearchPointSwap(p, i, j);
179591 i = j;
179592 }else{
179593 break;
179594 }
179595 }
179596 }
179597 }
179598}
179599
179600
179601/*
179602** Continue the search on cursor pCur until the front of the queue
179603** contains an entry suitable for returning as a result-set row,
179604** or until the RtreeSearchPoint queue is empty, indicating that the
179605** query has completed.
179606*/
179607static int rtreeStepToLeaf(RtreeCursor *pCur){
179608 RtreeSearchPoint *p;
179609 Rtree *pRtree = RTREE_OF_CURSOR(pCur);
179610 RtreeNode *pNode;
179611 int eWithin;
179612 int rc = SQLITE_OK;
179613 int nCell;
179614 int nConstraint = pCur->nConstraint;
179615 int ii;
179616 int eInt;
179617 RtreeSearchPoint x;
179618
179619 eInt = pRtree->eCoordType==RTREE_COORD_INT32;
179620 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
179621 pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
179622 if( rc ) return rc;
179623 nCell = NCELL(pNode);
179624 assert( nCell<200 );
179625 while( p->iCell<nCell ){
179626 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
179627 u8 *pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
179628 eWithin = FULLY_WITHIN;
179629 for(ii=0; ii<nConstraint; ii++){
179630 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
179631 if( pConstraint->op>=RTREE_MATCH ){
179632 rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
179633 &rScore, &eWithin);
179634 if( rc ) return rc;
179635 }else if( p->iLevel==1 ){
179636 rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
179637 }else{
179638 rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
179639 }
179640 if( eWithin==NOT_WITHIN ) break;
179641 }
179642 p->iCell++;
179643 if( eWithin==NOT_WITHIN ) continue;
179644 x.iLevel = p->iLevel - 1;
179645 if( x.iLevel ){
179646 x.id = readInt64(pCellData);
179647 x.iCell = 0;
179648 }else{
179649 x.id = p->id;
179650 x.iCell = p->iCell - 1;
179651 }
179652 if( p->iCell>=nCell ){
179653 RTREE_QUEUE_TRACE(pCur, "POP-S:");
179654 rtreeSearchPointPop(pCur);
179655 }
179656 if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
179657 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
179658 if( p==0 ) return SQLITE_NOMEM;
179659 p->eWithin = (u8)eWithin;
179660 p->id = x.id;
179661 p->iCell = x.iCell;
179662 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
179663 break;
179664 }
179665 if( p->iCell>=nCell ){
179666 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
179667 rtreeSearchPointPop(pCur);
179668 }
179669 }
179670 pCur->atEOF = p==0;
179671 return SQLITE_OK;
179672}
179673
179674/*
179675** Rtree virtual table module xNext method.
179676*/
179677static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
179678 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
179679 int rc = SQLITE_OK;
179680
179681 /* Move to the next entry that matches the configured constraints. */
179682 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
179683 if( pCsr->bAuxValid ){
179684 pCsr->bAuxValid = 0;
179685 sqlite3_reset(pCsr->pReadAux);
179686 }
179687 rtreeSearchPointPop(pCsr);
179688 rc = rtreeStepToLeaf(pCsr);
179689 return rc;
179690}
179691
179692/*
179693** Rtree virtual table module xRowid method.
179694*/
179695static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
179696 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
179697 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
179698 int rc = SQLITE_OK;
179699 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
179700 if( rc==SQLITE_OK && p ){
179701 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, p->iCell);
179702 }
179703 return rc;
179704}
179705
179706/*
179707** Rtree virtual table module xColumn method.
179708*/
179709static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
179710 Rtree *pRtree = (Rtree *)cur->pVtab;
179711 RtreeCursor *pCsr = (RtreeCursor *)cur;
179712 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
179713 RtreeCoord c;
179714 int rc = SQLITE_OK;
179715 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
179716
179717 if( rc ) return rc;
179718 if( p==0 ) return SQLITE_OK;
179719 if( i==0 ){
179720 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
179721 }else if( i<=pRtree->nDim2 ){
179722 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
179723#ifndef SQLITE_RTREE_INT_ONLY
179724 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
179725 sqlite3_result_double(ctx, c.f);
179726 }else
179727#endif
179728 {
179729 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
179730 sqlite3_result_int(ctx, c.i);
179731 }
179732 }else{
179733 if( !pCsr->bAuxValid ){
179734 if( pCsr->pReadAux==0 ){
179735 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
179736 &pCsr->pReadAux, 0);
179737 if( rc ) return rc;
179738 }
179739 sqlite3_bind_int64(pCsr->pReadAux, 1,
179740 nodeGetRowid(pRtree, pNode, p->iCell));
179741 rc = sqlite3_step(pCsr->pReadAux);
179742 if( rc==SQLITE_ROW ){
179743 pCsr->bAuxValid = 1;
179744 }else{
179745 sqlite3_reset(pCsr->pReadAux);
179746 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
179747 return rc;
179748 }
179749 }
179750 sqlite3_result_value(ctx,
179751 sqlite3_column_value(pCsr->pReadAux, i - pRtree->nDim2 + 1));
179752 }
179753 return SQLITE_OK;
179754}
179755
179756/*
179757** Use nodeAcquire() to obtain the leaf node containing the record with
179758** rowid iRowid. If successful, set *ppLeaf to point to the node and
179759** return SQLITE_OK. If there is no such record in the table, set
179760** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
179761** to zero and return an SQLite error code.
179762*/
179763static int findLeafNode(
179764 Rtree *pRtree, /* RTree to search */
179765 i64 iRowid, /* The rowid searching for */
179766 RtreeNode **ppLeaf, /* Write the node here */
179767 sqlite3_int64 *piNode /* Write the node-id here */
179768){
179769 int rc;
179770 *ppLeaf = 0;
179771 sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
179772 if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
179773 i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
179774 if( piNode ) *piNode = iNode;
179775 rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
179776 sqlite3_reset(pRtree->pReadRowid);
179777 }else{
179778 rc = sqlite3_reset(pRtree->pReadRowid);
179779 }
179780 return rc;
179781}
179782
179783/*
179784** This function is called to configure the RtreeConstraint object passed
179785** as the second argument for a MATCH constraint. The value passed as the
179786** first argument to this function is the right-hand operand to the MATCH
179787** operator.
179788*/
179789static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
179790 RtreeMatchArg *pBlob, *pSrc; /* BLOB returned by geometry function */
179791 sqlite3_rtree_query_info *pInfo; /* Callback information */
179792
179793 pSrc = sqlite3_value_pointer(pValue, "RtreeMatchArg");
179794 if( pSrc==0 ) return SQLITE_ERROR;
179795 pInfo = (sqlite3_rtree_query_info*)
179796 sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize );
179797 if( !pInfo ) return SQLITE_NOMEM;
179798 memset(pInfo, 0, sizeof(*pInfo));
179799 pBlob = (RtreeMatchArg*)&pInfo[1];
179800 memcpy(pBlob, pSrc, pSrc->iSize);
179801 pInfo->pContext = pBlob->cb.pContext;
179802 pInfo->nParam = pBlob->nParam;
179803 pInfo->aParam = pBlob->aParam;
179804 pInfo->apSqlParam = pBlob->apSqlParam;
179805
179806 if( pBlob->cb.xGeom ){
179807 pCons->u.xGeom = pBlob->cb.xGeom;
179808 }else{
179809 pCons->op = RTREE_QUERY;
179810 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
179811 }
179812 pCons->pInfo = pInfo;
179813 return SQLITE_OK;
179814}
179815
179816/*
179817** Rtree virtual table module xFilter method.
179818*/
179819static int rtreeFilter(
179820 sqlite3_vtab_cursor *pVtabCursor,
179821 int idxNum, const char *idxStr,
179822 int argc, sqlite3_value **argv
179823){
179824 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
179825 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
179826 RtreeNode *pRoot = 0;
179827 int ii;
179828 int rc = SQLITE_OK;
179829 int iCell = 0;
179830 sqlite3_stmt *pStmt;
179831
179832 rtreeReference(pRtree);
179833
179834 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
179835 freeCursorConstraints(pCsr);
179836 sqlite3_free(pCsr->aPoint);
179837 pStmt = pCsr->pReadAux;
179838 memset(pCsr, 0, sizeof(RtreeCursor));
179839 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
179840 pCsr->pReadAux = pStmt;
179841
179842 pCsr->iStrategy = idxNum;
179843 if( idxNum==1 ){
179844 /* Special case - lookup by rowid. */
179845 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
179846 RtreeSearchPoint *p; /* Search point for the leaf */
179847 i64 iRowid = sqlite3_value_int64(argv[0]);
179848 i64 iNode = 0;
179849 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
179850 if( rc==SQLITE_OK && pLeaf!=0 ){
179851 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
179852 assert( p!=0 ); /* Always returns pCsr->sPoint */
179853 pCsr->aNode[0] = pLeaf;
179854 p->id = iNode;
179855 p->eWithin = PARTLY_WITHIN;
179856 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
179857 p->iCell = (u8)iCell;
179858 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
179859 }else{
179860 pCsr->atEOF = 1;
179861 }
179862 }else{
179863 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
179864 ** with the configured constraints.
179865 */
179866 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
179867 if( rc==SQLITE_OK && argc>0 ){
179868 pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
179869 pCsr->nConstraint = argc;
179870 if( !pCsr->aConstraint ){
179871 rc = SQLITE_NOMEM;
179872 }else{
179873 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
179874 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
179875 assert( (idxStr==0 && argc==0)
179876 || (idxStr && (int)strlen(idxStr)==argc*2) );
179877 for(ii=0; ii<argc; ii++){
179878 RtreeConstraint *p = &pCsr->aConstraint[ii];
179879 p->op = idxStr[ii*2];
179880 p->iCoord = idxStr[ii*2+1]-'0';
179881 if( p->op>=RTREE_MATCH ){
179882 /* A MATCH operator. The right-hand-side must be a blob that
179883 ** can be cast into an RtreeMatchArg object. One created using
179884 ** an sqlite3_rtree_geometry_callback() SQL user function.
179885 */
179886 rc = deserializeGeometry(argv[ii], p);
179887 if( rc!=SQLITE_OK ){
179888 break;
179889 }
179890 p->pInfo->nCoord = pRtree->nDim2;
179891 p->pInfo->anQueue = pCsr->anQueue;
179892 p->pInfo->mxLevel = pRtree->iDepth + 1;
179893 }else{
179894#ifdef SQLITE_RTREE_INT_ONLY
179895 p->u.rValue = sqlite3_value_int64(argv[ii]);
179896#else
179897 p->u.rValue = sqlite3_value_double(argv[ii]);
179898#endif
179899 }
179900 }
179901 }
179902 }
179903 if( rc==SQLITE_OK ){
179904 RtreeSearchPoint *pNew;
179905 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
179906 if( pNew==0 ) return SQLITE_NOMEM;
179907 pNew->id = 1;
179908 pNew->iCell = 0;
179909 pNew->eWithin = PARTLY_WITHIN;
179910 assert( pCsr->bPoint==1 );
179911 pCsr->aNode[0] = pRoot;
179912 pRoot = 0;
179913 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
179914 rc = rtreeStepToLeaf(pCsr);
179915 }
179916 }
179917
179918 nodeRelease(pRtree, pRoot);
179919 rtreeRelease(pRtree);
179920 return rc;
179921}
179922
179923/*
179924** Rtree virtual table module xBestIndex method. There are three
179925** table scan strategies to choose from (in order from most to
179926** least desirable):
179927**
179928** idxNum idxStr Strategy
179929** ------------------------------------------------
179930** 1 Unused Direct lookup by rowid.
179931** 2 See below R-tree query or full-table scan.
179932** ------------------------------------------------
179933**
179934** If strategy 1 is used, then idxStr is not meaningful. If strategy
179935** 2 is used, idxStr is formatted to contain 2 bytes for each
179936** constraint used. The first two bytes of idxStr correspond to
179937** the constraint in sqlite3_index_info.aConstraintUsage[] with
179938** (argvIndex==1) etc.
179939**
179940** The first of each pair of bytes in idxStr identifies the constraint
179941** operator as follows:
179942**
179943** Operator Byte Value
179944** ----------------------
179945** = 0x41 ('A')
179946** <= 0x42 ('B')
179947** < 0x43 ('C')
179948** >= 0x44 ('D')
179949** > 0x45 ('E')
179950** MATCH 0x46 ('F')
179951** ----------------------
179952**
179953** The second of each pair of bytes identifies the coordinate column
179954** to which the constraint applies. The leftmost coordinate column
179955** is 'a', the second from the left 'b' etc.
179956*/
179957static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
179958 Rtree *pRtree = (Rtree*)tab;
179959 int rc = SQLITE_OK;
179960 int ii;
179961 int bMatch = 0; /* True if there exists a MATCH constraint */
179962 i64 nRow; /* Estimated rows returned by this scan */
179963
179964 int iIdx = 0;
179965 char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
179966 memset(zIdxStr, 0, sizeof(zIdxStr));
179967
179968 /* Check if there exists a MATCH constraint - even an unusable one. If there
179969 ** is, do not consider the lookup-by-rowid plan as using such a plan would
179970 ** require the VDBE to evaluate the MATCH constraint, which is not currently
179971 ** possible. */
179972 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
179973 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){
179974 bMatch = 1;
179975 }
179976 }
179977
179978 assert( pIdxInfo->idxStr==0 );
179979 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
179980 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
179981
179982 if( bMatch==0 && p->usable
179983 && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ
179984 ){
179985 /* We have an equality constraint on the rowid. Use strategy 1. */
179986 int jj;
179987 for(jj=0; jj<ii; jj++){
179988 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
179989 pIdxInfo->aConstraintUsage[jj].omit = 0;
179990 }
179991 pIdxInfo->idxNum = 1;
179992 pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
179993 pIdxInfo->aConstraintUsage[jj].omit = 1;
179994
179995 /* This strategy involves a two rowid lookups on an B-Tree structures
179996 ** and then a linear search of an R-Tree node. This should be
179997 ** considered almost as quick as a direct rowid lookup (for which
179998 ** sqlite uses an internal cost of 0.0). It is expected to return
179999 ** a single row.
180000 */
180001 pIdxInfo->estimatedCost = 30.0;
180002 pIdxInfo->estimatedRows = 1;
180003 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
180004 return SQLITE_OK;
180005 }
180006
180007 if( p->usable
180008 && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
180009 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH)
180010 ){
180011 u8 op;
180012 switch( p->op ){
180013 case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
180014 case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
180015 case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
180016 case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
180017 case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
180018 default:
180019 assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
180020 op = RTREE_MATCH;
180021 break;
180022 }
180023 zIdxStr[iIdx++] = op;
180024 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
180025 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
180026 pIdxInfo->aConstraintUsage[ii].omit = 1;
180027 }
180028 }
180029
180030 pIdxInfo->idxNum = 2;
180031 pIdxInfo->needToFreeIdxStr = 1;
180032 if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
180033 return SQLITE_NOMEM;
180034 }
180035
180036 nRow = pRtree->nRowEst >> (iIdx/2);
180037 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
180038 pIdxInfo->estimatedRows = nRow;
180039
180040 return rc;
180041}
180042
180043/*
180044** Return the N-dimensional volumn of the cell stored in *p.
180045*/
180046static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
180047 RtreeDValue area = (RtreeDValue)1;
180048 assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
180049#ifndef SQLITE_RTREE_INT_ONLY
180050 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
180051 switch( pRtree->nDim ){
180052 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
180053 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
180054 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
180055 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
180056 default: area *= p->aCoord[1].f - p->aCoord[0].f;
180057 }
180058 }else
180059#endif
180060 {
180061 switch( pRtree->nDim ){
180062 case 5: area = p->aCoord[9].i - p->aCoord[8].i;
180063 case 4: area *= p->aCoord[7].i - p->aCoord[6].i;
180064 case 3: area *= p->aCoord[5].i - p->aCoord[4].i;
180065 case 2: area *= p->aCoord[3].i - p->aCoord[2].i;
180066 default: area *= p->aCoord[1].i - p->aCoord[0].i;
180067 }
180068 }
180069 return area;
180070}
180071
180072/*
180073** Return the margin length of cell p. The margin length is the sum
180074** of the objects size in each dimension.
180075*/
180076static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
180077 RtreeDValue margin = 0;
180078 int ii = pRtree->nDim2 - 2;
180079 do{
180080 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
180081 ii -= 2;
180082 }while( ii>=0 );
180083 return margin;
180084}
180085
180086/*
180087** Store the union of cells p1 and p2 in p1.
180088*/
180089static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
180090 int ii = 0;
180091 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
180092 do{
180093 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
180094 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
180095 ii += 2;
180096 }while( ii<pRtree->nDim2 );
180097 }else{
180098 do{
180099 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
180100 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
180101 ii += 2;
180102 }while( ii<pRtree->nDim2 );
180103 }
180104}
180105
180106/*
180107** Return true if the area covered by p2 is a subset of the area covered
180108** by p1. False otherwise.
180109*/
180110static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
180111 int ii;
180112 int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
180113 for(ii=0; ii<pRtree->nDim2; ii+=2){
180114 RtreeCoord *a1 = &p1->aCoord[ii];
180115 RtreeCoord *a2 = &p2->aCoord[ii];
180116 if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
180117 || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
180118 ){
180119 return 0;
180120 }
180121 }
180122 return 1;
180123}
180124
180125/*
180126** Return the amount cell p would grow by if it were unioned with pCell.
180127*/
180128static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
180129 RtreeDValue area;
180130 RtreeCell cell;
180131 memcpy(&cell, p, sizeof(RtreeCell));
180132 area = cellArea(pRtree, &cell);
180133 cellUnion(pRtree, &cell, pCell);
180134 return (cellArea(pRtree, &cell)-area);
180135}
180136
180137static RtreeDValue cellOverlap(
180138 Rtree *pRtree,
180139 RtreeCell *p,
180140 RtreeCell *aCell,
180141 int nCell
180142){
180143 int ii;
180144 RtreeDValue overlap = RTREE_ZERO;
180145 for(ii=0; ii<nCell; ii++){
180146 int jj;
180147 RtreeDValue o = (RtreeDValue)1;
180148 for(jj=0; jj<pRtree->nDim2; jj+=2){
180149 RtreeDValue x1, x2;
180150 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
180151 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
180152 if( x2<x1 ){
180153 o = (RtreeDValue)0;
180154 break;
180155 }else{
180156 o = o * (x2-x1);
180157 }
180158 }
180159 overlap += o;
180160 }
180161 return overlap;
180162}
180163
180164
180165/*
180166** This function implements the ChooseLeaf algorithm from Gutman[84].
180167** ChooseSubTree in r*tree terminology.
180168*/
180169static int ChooseLeaf(
180170 Rtree *pRtree, /* Rtree table */
180171 RtreeCell *pCell, /* Cell to insert into rtree */
180172 int iHeight, /* Height of sub-tree rooted at pCell */
180173 RtreeNode **ppLeaf /* OUT: Selected leaf page */
180174){
180175 int rc;
180176 int ii;
180177 RtreeNode *pNode = 0;
180178 rc = nodeAcquire(pRtree, 1, 0, &pNode);
180179
180180 for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
180181 int iCell;
180182 sqlite3_int64 iBest = 0;
180183
180184 RtreeDValue fMinGrowth = RTREE_ZERO;
180185 RtreeDValue fMinArea = RTREE_ZERO;
180186
180187 int nCell = NCELL(pNode);
180188 RtreeCell cell;
180189 RtreeNode *pChild;
180190
180191 RtreeCell *aCell = 0;
180192
180193 /* Select the child node which will be enlarged the least if pCell
180194 ** is inserted into it. Resolve ties by choosing the entry with
180195 ** the smallest area.
180196 */
180197 for(iCell=0; iCell<nCell; iCell++){
180198 int bBest = 0;
180199 RtreeDValue growth;
180200 RtreeDValue area;
180201 nodeGetCell(pRtree, pNode, iCell, &cell);
180202 growth = cellGrowth(pRtree, &cell, pCell);
180203 area = cellArea(pRtree, &cell);
180204 if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
180205 bBest = 1;
180206 }
180207 if( bBest ){
180208 fMinGrowth = growth;
180209 fMinArea = area;
180210 iBest = cell.iRowid;
180211 }
180212 }
180213
180214 sqlite3_free(aCell);
180215 rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
180216 nodeRelease(pRtree, pNode);
180217 pNode = pChild;
180218 }
180219
180220 *ppLeaf = pNode;
180221 return rc;
180222}
180223
180224/*
180225** A cell with the same content as pCell has just been inserted into
180226** the node pNode. This function updates the bounding box cells in
180227** all ancestor elements.
180228*/
180229static int AdjustTree(
180230 Rtree *pRtree, /* Rtree table */
180231 RtreeNode *pNode, /* Adjust ancestry of this node. */
180232 RtreeCell *pCell /* This cell was just inserted */
180233){
180234 RtreeNode *p = pNode;
180235 while( p->pParent ){
180236 RtreeNode *pParent = p->pParent;
180237 RtreeCell cell;
180238 int iCell;
180239
180240 if( nodeParentIndex(pRtree, p, &iCell) ){
180241 return SQLITE_CORRUPT_VTAB;
180242 }
180243
180244 nodeGetCell(pRtree, pParent, iCell, &cell);
180245 if( !cellContains(pRtree, &cell, pCell) ){
180246 cellUnion(pRtree, &cell, pCell);
180247 nodeOverwriteCell(pRtree, pParent, &cell, iCell);
180248 }
180249
180250 p = pParent;
180251 }
180252 return SQLITE_OK;
180253}
180254
180255/*
180256** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
180257*/
180258static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
180259 sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
180260 sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
180261 sqlite3_step(pRtree->pWriteRowid);
180262 return sqlite3_reset(pRtree->pWriteRowid);
180263}
180264
180265/*
180266** Write mapping (iNode->iPar) to the <rtree>_parent table.
180267*/
180268static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
180269 sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
180270 sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
180271 sqlite3_step(pRtree->pWriteParent);
180272 return sqlite3_reset(pRtree->pWriteParent);
180273}
180274
180275static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
180276
180277
180278/*
180279** Arguments aIdx, aDistance and aSpare all point to arrays of size
180280** nIdx. The aIdx array contains the set of integers from 0 to
180281** (nIdx-1) in no particular order. This function sorts the values
180282** in aIdx according to the indexed values in aDistance. For
180283** example, assuming the inputs:
180284**
180285** aIdx = { 0, 1, 2, 3 }
180286** aDistance = { 5.0, 2.0, 7.0, 6.0 }
180287**
180288** this function sets the aIdx array to contain:
180289**
180290** aIdx = { 0, 1, 2, 3 }
180291**
180292** The aSpare array is used as temporary working space by the
180293** sorting algorithm.
180294*/
180295static void SortByDistance(
180296 int *aIdx,
180297 int nIdx,
180298 RtreeDValue *aDistance,
180299 int *aSpare
180300){
180301 if( nIdx>1 ){
180302 int iLeft = 0;
180303 int iRight = 0;
180304
180305 int nLeft = nIdx/2;
180306 int nRight = nIdx-nLeft;
180307 int *aLeft = aIdx;
180308 int *aRight = &aIdx[nLeft];
180309
180310 SortByDistance(aLeft, nLeft, aDistance, aSpare);
180311 SortByDistance(aRight, nRight, aDistance, aSpare);
180312
180313 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
180314 aLeft = aSpare;
180315
180316 while( iLeft<nLeft || iRight<nRight ){
180317 if( iLeft==nLeft ){
180318 aIdx[iLeft+iRight] = aRight[iRight];
180319 iRight++;
180320 }else if( iRight==nRight ){
180321 aIdx[iLeft+iRight] = aLeft[iLeft];
180322 iLeft++;
180323 }else{
180324 RtreeDValue fLeft = aDistance[aLeft[iLeft]];
180325 RtreeDValue fRight = aDistance[aRight[iRight]];
180326 if( fLeft<fRight ){
180327 aIdx[iLeft+iRight] = aLeft[iLeft];
180328 iLeft++;
180329 }else{
180330 aIdx[iLeft+iRight] = aRight[iRight];
180331 iRight++;
180332 }
180333 }
180334 }
180335
180336#if 0
180337 /* Check that the sort worked */
180338 {
180339 int jj;
180340 for(jj=1; jj<nIdx; jj++){
180341 RtreeDValue left = aDistance[aIdx[jj-1]];
180342 RtreeDValue right = aDistance[aIdx[jj]];
180343 assert( left<=right );
180344 }
180345 }
180346#endif
180347 }
180348}
180349
180350/*
180351** Arguments aIdx, aCell and aSpare all point to arrays of size
180352** nIdx. The aIdx array contains the set of integers from 0 to
180353** (nIdx-1) in no particular order. This function sorts the values
180354** in aIdx according to dimension iDim of the cells in aCell. The
180355** minimum value of dimension iDim is considered first, the
180356** maximum used to break ties.
180357**
180358** The aSpare array is used as temporary working space by the
180359** sorting algorithm.
180360*/
180361static void SortByDimension(
180362 Rtree *pRtree,
180363 int *aIdx,
180364 int nIdx,
180365 int iDim,
180366 RtreeCell *aCell,
180367 int *aSpare
180368){
180369 if( nIdx>1 ){
180370
180371 int iLeft = 0;
180372 int iRight = 0;
180373
180374 int nLeft = nIdx/2;
180375 int nRight = nIdx-nLeft;
180376 int *aLeft = aIdx;
180377 int *aRight = &aIdx[nLeft];
180378
180379 SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
180380 SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
180381
180382 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
180383 aLeft = aSpare;
180384 while( iLeft<nLeft || iRight<nRight ){
180385 RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
180386 RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
180387 RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
180388 RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
180389 if( (iLeft!=nLeft) && ((iRight==nRight)
180390 || (xleft1<xright1)
180391 || (xleft1==xright1 && xleft2<xright2)
180392 )){
180393 aIdx[iLeft+iRight] = aLeft[iLeft];
180394 iLeft++;
180395 }else{
180396 aIdx[iLeft+iRight] = aRight[iRight];
180397 iRight++;
180398 }
180399 }
180400
180401#if 0
180402 /* Check that the sort worked */
180403 {
180404 int jj;
180405 for(jj=1; jj<nIdx; jj++){
180406 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
180407 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
180408 RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
180409 RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
180410 assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
180411 }
180412 }
180413#endif
180414 }
180415}
180416
180417/*
180418** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
180419*/
180420static int splitNodeStartree(
180421 Rtree *pRtree,
180422 RtreeCell *aCell,
180423 int nCell,
180424 RtreeNode *pLeft,
180425 RtreeNode *pRight,
180426 RtreeCell *pBboxLeft,
180427 RtreeCell *pBboxRight
180428){
180429 int **aaSorted;
180430 int *aSpare;
180431 int ii;
180432
180433 int iBestDim = 0;
180434 int iBestSplit = 0;
180435 RtreeDValue fBestMargin = RTREE_ZERO;
180436
180437 int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
180438
180439 aaSorted = (int **)sqlite3_malloc(nByte);
180440 if( !aaSorted ){
180441 return SQLITE_NOMEM;
180442 }
180443
180444 aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
180445 memset(aaSorted, 0, nByte);
180446 for(ii=0; ii<pRtree->nDim; ii++){
180447 int jj;
180448 aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
180449 for(jj=0; jj<nCell; jj++){
180450 aaSorted[ii][jj] = jj;
180451 }
180452 SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
180453 }
180454
180455 for(ii=0; ii<pRtree->nDim; ii++){
180456 RtreeDValue margin = RTREE_ZERO;
180457 RtreeDValue fBestOverlap = RTREE_ZERO;
180458 RtreeDValue fBestArea = RTREE_ZERO;
180459 int iBestLeft = 0;
180460 int nLeft;
180461
180462 for(
180463 nLeft=RTREE_MINCELLS(pRtree);
180464 nLeft<=(nCell-RTREE_MINCELLS(pRtree));
180465 nLeft++
180466 ){
180467 RtreeCell left;
180468 RtreeCell right;
180469 int kk;
180470 RtreeDValue overlap;
180471 RtreeDValue area;
180472
180473 memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
180474 memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
180475 for(kk=1; kk<(nCell-1); kk++){
180476 if( kk<nLeft ){
180477 cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
180478 }else{
180479 cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
180480 }
180481 }
180482 margin += cellMargin(pRtree, &left);
180483 margin += cellMargin(pRtree, &right);
180484 overlap = cellOverlap(pRtree, &left, &right, 1);
180485 area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
180486 if( (nLeft==RTREE_MINCELLS(pRtree))
180487 || (overlap<fBestOverlap)
180488 || (overlap==fBestOverlap && area<fBestArea)
180489 ){
180490 iBestLeft = nLeft;
180491 fBestOverlap = overlap;
180492 fBestArea = area;
180493 }
180494 }
180495
180496 if( ii==0 || margin<fBestMargin ){
180497 iBestDim = ii;
180498 fBestMargin = margin;
180499 iBestSplit = iBestLeft;
180500 }
180501 }
180502
180503 memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
180504 memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
180505 for(ii=0; ii<nCell; ii++){
180506 RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
180507 RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
180508 RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
180509 nodeInsertCell(pRtree, pTarget, pCell);
180510 cellUnion(pRtree, pBbox, pCell);
180511 }
180512
180513 sqlite3_free(aaSorted);
180514 return SQLITE_OK;
180515}
180516
180517
180518static int updateMapping(
180519 Rtree *pRtree,
180520 i64 iRowid,
180521 RtreeNode *pNode,
180522 int iHeight
180523){
180524 int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
180525 xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
180526 if( iHeight>0 ){
180527 RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
180528 if( pChild ){
180529 nodeRelease(pRtree, pChild->pParent);
180530 nodeReference(pNode);
180531 pChild->pParent = pNode;
180532 }
180533 }
180534 return xSetMapping(pRtree, iRowid, pNode->iNode);
180535}
180536
180537static int SplitNode(
180538 Rtree *pRtree,
180539 RtreeNode *pNode,
180540 RtreeCell *pCell,
180541 int iHeight
180542){
180543 int i;
180544 int newCellIsRight = 0;
180545
180546 int rc = SQLITE_OK;
180547 int nCell = NCELL(pNode);
180548 RtreeCell *aCell;
180549 int *aiUsed;
180550
180551 RtreeNode *pLeft = 0;
180552 RtreeNode *pRight = 0;
180553
180554 RtreeCell leftbbox;
180555 RtreeCell rightbbox;
180556
180557 /* Allocate an array and populate it with a copy of pCell and
180558 ** all cells from node pLeft. Then zero the original node.
180559 */
180560 aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
180561 if( !aCell ){
180562 rc = SQLITE_NOMEM;
180563 goto splitnode_out;
180564 }
180565 aiUsed = (int *)&aCell[nCell+1];
180566 memset(aiUsed, 0, sizeof(int)*(nCell+1));
180567 for(i=0; i<nCell; i++){
180568 nodeGetCell(pRtree, pNode, i, &aCell[i]);
180569 }
180570 nodeZero(pRtree, pNode);
180571 memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
180572 nCell++;
180573
180574 if( pNode->iNode==1 ){
180575 pRight = nodeNew(pRtree, pNode);
180576 pLeft = nodeNew(pRtree, pNode);
180577 pRtree->iDepth++;
180578 pNode->isDirty = 1;
180579 writeInt16(pNode->zData, pRtree->iDepth);
180580 }else{
180581 pLeft = pNode;
180582 pRight = nodeNew(pRtree, pLeft->pParent);
180583 pLeft->nRef++;
180584 }
180585
180586 if( !pLeft || !pRight ){
180587 rc = SQLITE_NOMEM;
180588 goto splitnode_out;
180589 }
180590
180591 memset(pLeft->zData, 0, pRtree->iNodeSize);
180592 memset(pRight->zData, 0, pRtree->iNodeSize);
180593
180594 rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
180595 &leftbbox, &rightbbox);
180596 if( rc!=SQLITE_OK ){
180597 goto splitnode_out;
180598 }
180599
180600 /* Ensure both child nodes have node numbers assigned to them by calling
180601 ** nodeWrite(). Node pRight always needs a node number, as it was created
180602 ** by nodeNew() above. But node pLeft sometimes already has a node number.
180603 ** In this case avoid the all to nodeWrite().
180604 */
180605 if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
180606 || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
180607 ){
180608 goto splitnode_out;
180609 }
180610
180611 rightbbox.iRowid = pRight->iNode;
180612 leftbbox.iRowid = pLeft->iNode;
180613
180614 if( pNode->iNode==1 ){
180615 rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
180616 if( rc!=SQLITE_OK ){
180617 goto splitnode_out;
180618 }
180619 }else{
180620 RtreeNode *pParent = pLeft->pParent;
180621 int iCell;
180622 rc = nodeParentIndex(pRtree, pLeft, &iCell);
180623 if( rc==SQLITE_OK ){
180624 nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
180625 rc = AdjustTree(pRtree, pParent, &leftbbox);
180626 }
180627 if( rc!=SQLITE_OK ){
180628 goto splitnode_out;
180629 }
180630 }
180631 if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
180632 goto splitnode_out;
180633 }
180634
180635 for(i=0; i<NCELL(pRight); i++){
180636 i64 iRowid = nodeGetRowid(pRtree, pRight, i);
180637 rc = updateMapping(pRtree, iRowid, pRight, iHeight);
180638 if( iRowid==pCell->iRowid ){
180639 newCellIsRight = 1;
180640 }
180641 if( rc!=SQLITE_OK ){
180642 goto splitnode_out;
180643 }
180644 }
180645 if( pNode->iNode==1 ){
180646 for(i=0; i<NCELL(pLeft); i++){
180647 i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
180648 rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
180649 if( rc!=SQLITE_OK ){
180650 goto splitnode_out;
180651 }
180652 }
180653 }else if( newCellIsRight==0 ){
180654 rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
180655 }
180656
180657 if( rc==SQLITE_OK ){
180658 rc = nodeRelease(pRtree, pRight);
180659 pRight = 0;
180660 }
180661 if( rc==SQLITE_OK ){
180662 rc = nodeRelease(pRtree, pLeft);
180663 pLeft = 0;
180664 }
180665
180666splitnode_out:
180667 nodeRelease(pRtree, pRight);
180668 nodeRelease(pRtree, pLeft);
180669 sqlite3_free(aCell);
180670 return rc;
180671}
180672
180673/*
180674** If node pLeaf is not the root of the r-tree and its pParent pointer is
180675** still NULL, load all ancestor nodes of pLeaf into memory and populate
180676** the pLeaf->pParent chain all the way up to the root node.
180677**
180678** This operation is required when a row is deleted (or updated - an update
180679** is implemented as a delete followed by an insert). SQLite provides the
180680** rowid of the row to delete, which can be used to find the leaf on which
180681** the entry resides (argument pLeaf). Once the leaf is located, this
180682** function is called to determine its ancestry.
180683*/
180684static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
180685 int rc = SQLITE_OK;
180686 RtreeNode *pChild = pLeaf;
180687 while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
180688 int rc2 = SQLITE_OK; /* sqlite3_reset() return code */
180689 sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
180690 rc = sqlite3_step(pRtree->pReadParent);
180691 if( rc==SQLITE_ROW ){
180692 RtreeNode *pTest; /* Used to test for reference loops */
180693 i64 iNode; /* Node number of parent node */
180694
180695 /* Before setting pChild->pParent, test that we are not creating a
180696 ** loop of references (as we would if, say, pChild==pParent). We don't
180697 ** want to do this as it leads to a memory leak when trying to delete
180698 ** the referenced counted node structures.
180699 */
180700 iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
180701 for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
180702 if( !pTest ){
180703 rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
180704 }
180705 }
180706 rc = sqlite3_reset(pRtree->pReadParent);
180707 if( rc==SQLITE_OK ) rc = rc2;
180708 if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
180709 pChild = pChild->pParent;
180710 }
180711 return rc;
180712}
180713
180714static int deleteCell(Rtree *, RtreeNode *, int, int);
180715
180716static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
180717 int rc;
180718 int rc2;
180719 RtreeNode *pParent = 0;
180720 int iCell;
180721
180722 assert( pNode->nRef==1 );
180723
180724 /* Remove the entry in the parent cell. */
180725 rc = nodeParentIndex(pRtree, pNode, &iCell);
180726 if( rc==SQLITE_OK ){
180727 pParent = pNode->pParent;
180728 pNode->pParent = 0;
180729 rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
180730 }
180731 rc2 = nodeRelease(pRtree, pParent);
180732 if( rc==SQLITE_OK ){
180733 rc = rc2;
180734 }
180735 if( rc!=SQLITE_OK ){
180736 return rc;
180737 }
180738
180739 /* Remove the xxx_node entry. */
180740 sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
180741 sqlite3_step(pRtree->pDeleteNode);
180742 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
180743 return rc;
180744 }
180745
180746 /* Remove the xxx_parent entry. */
180747 sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
180748 sqlite3_step(pRtree->pDeleteParent);
180749 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
180750 return rc;
180751 }
180752
180753 /* Remove the node from the in-memory hash table and link it into
180754 ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
180755 */
180756 nodeHashDelete(pRtree, pNode);
180757 pNode->iNode = iHeight;
180758 pNode->pNext = pRtree->pDeleted;
180759 pNode->nRef++;
180760 pRtree->pDeleted = pNode;
180761
180762 return SQLITE_OK;
180763}
180764
180765static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
180766 RtreeNode *pParent = pNode->pParent;
180767 int rc = SQLITE_OK;
180768 if( pParent ){
180769 int ii;
180770 int nCell = NCELL(pNode);
180771 RtreeCell box; /* Bounding box for pNode */
180772 nodeGetCell(pRtree, pNode, 0, &box);
180773 for(ii=1; ii<nCell; ii++){
180774 RtreeCell cell;
180775 nodeGetCell(pRtree, pNode, ii, &cell);
180776 cellUnion(pRtree, &box, &cell);
180777 }
180778 box.iRowid = pNode->iNode;
180779 rc = nodeParentIndex(pRtree, pNode, &ii);
180780 if( rc==SQLITE_OK ){
180781 nodeOverwriteCell(pRtree, pParent, &box, ii);
180782 rc = fixBoundingBox(pRtree, pParent);
180783 }
180784 }
180785 return rc;
180786}
180787
180788/*
180789** Delete the cell at index iCell of node pNode. After removing the
180790** cell, adjust the r-tree data structure if required.
180791*/
180792static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
180793 RtreeNode *pParent;
180794 int rc;
180795
180796 if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
180797 return rc;
180798 }
180799
180800 /* Remove the cell from the node. This call just moves bytes around
180801 ** the in-memory node image, so it cannot fail.
180802 */
180803 nodeDeleteCell(pRtree, pNode, iCell);
180804
180805 /* If the node is not the tree root and now has less than the minimum
180806 ** number of cells, remove it from the tree. Otherwise, update the
180807 ** cell in the parent node so that it tightly contains the updated
180808 ** node.
180809 */
180810 pParent = pNode->pParent;
180811 assert( pParent || pNode->iNode==1 );
180812 if( pParent ){
180813 if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
180814 rc = removeNode(pRtree, pNode, iHeight);
180815 }else{
180816 rc = fixBoundingBox(pRtree, pNode);
180817 }
180818 }
180819
180820 return rc;
180821}
180822
180823static int Reinsert(
180824 Rtree *pRtree,
180825 RtreeNode *pNode,
180826 RtreeCell *pCell,
180827 int iHeight
180828){
180829 int *aOrder;
180830 int *aSpare;
180831 RtreeCell *aCell;
180832 RtreeDValue *aDistance;
180833 int nCell;
180834 RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
180835 int iDim;
180836 int ii;
180837 int rc = SQLITE_OK;
180838 int n;
180839
180840 memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
180841
180842 nCell = NCELL(pNode)+1;
180843 n = (nCell+1)&(~1);
180844
180845 /* Allocate the buffers used by this operation. The allocation is
180846 ** relinquished before this function returns.
180847 */
180848 aCell = (RtreeCell *)sqlite3_malloc(n * (
180849 sizeof(RtreeCell) + /* aCell array */
180850 sizeof(int) + /* aOrder array */
180851 sizeof(int) + /* aSpare array */
180852 sizeof(RtreeDValue) /* aDistance array */
180853 ));
180854 if( !aCell ){
180855 return SQLITE_NOMEM;
180856 }
180857 aOrder = (int *)&aCell[n];
180858 aSpare = (int *)&aOrder[n];
180859 aDistance = (RtreeDValue *)&aSpare[n];
180860
180861 for(ii=0; ii<nCell; ii++){
180862 if( ii==(nCell-1) ){
180863 memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
180864 }else{
180865 nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
180866 }
180867 aOrder[ii] = ii;
180868 for(iDim=0; iDim<pRtree->nDim; iDim++){
180869 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
180870 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
180871 }
180872 }
180873 for(iDim=0; iDim<pRtree->nDim; iDim++){
180874 aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
180875 }
180876
180877 for(ii=0; ii<nCell; ii++){
180878 aDistance[ii] = RTREE_ZERO;
180879 for(iDim=0; iDim<pRtree->nDim; iDim++){
180880 RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
180881 DCOORD(aCell[ii].aCoord[iDim*2]));
180882 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
180883 }
180884 }
180885
180886 SortByDistance(aOrder, nCell, aDistance, aSpare);
180887 nodeZero(pRtree, pNode);
180888
180889 for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
180890 RtreeCell *p = &aCell[aOrder[ii]];
180891 nodeInsertCell(pRtree, pNode, p);
180892 if( p->iRowid==pCell->iRowid ){
180893 if( iHeight==0 ){
180894 rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
180895 }else{
180896 rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
180897 }
180898 }
180899 }
180900 if( rc==SQLITE_OK ){
180901 rc = fixBoundingBox(pRtree, pNode);
180902 }
180903 for(; rc==SQLITE_OK && ii<nCell; ii++){
180904 /* Find a node to store this cell in. pNode->iNode currently contains
180905 ** the height of the sub-tree headed by the cell.
180906 */
180907 RtreeNode *pInsert;
180908 RtreeCell *p = &aCell[aOrder[ii]];
180909 rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
180910 if( rc==SQLITE_OK ){
180911 int rc2;
180912 rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
180913 rc2 = nodeRelease(pRtree, pInsert);
180914 if( rc==SQLITE_OK ){
180915 rc = rc2;
180916 }
180917 }
180918 }
180919
180920 sqlite3_free(aCell);
180921 return rc;
180922}
180923
180924/*
180925** Insert cell pCell into node pNode. Node pNode is the head of a
180926** subtree iHeight high (leaf nodes have iHeight==0).
180927*/
180928static int rtreeInsertCell(
180929 Rtree *pRtree,
180930 RtreeNode *pNode,
180931 RtreeCell *pCell,
180932 int iHeight
180933){
180934 int rc = SQLITE_OK;
180935 if( iHeight>0 ){
180936 RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
180937 if( pChild ){
180938 nodeRelease(pRtree, pChild->pParent);
180939 nodeReference(pNode);
180940 pChild->pParent = pNode;
180941 }
180942 }
180943 if( nodeInsertCell(pRtree, pNode, pCell) ){
180944 if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
180945 rc = SplitNode(pRtree, pNode, pCell, iHeight);
180946 }else{
180947 pRtree->iReinsertHeight = iHeight;
180948 rc = Reinsert(pRtree, pNode, pCell, iHeight);
180949 }
180950 }else{
180951 rc = AdjustTree(pRtree, pNode, pCell);
180952 if( rc==SQLITE_OK ){
180953 if( iHeight==0 ){
180954 rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
180955 }else{
180956 rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
180957 }
180958 }
180959 }
180960 return rc;
180961}
180962
180963static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
180964 int ii;
180965 int rc = SQLITE_OK;
180966 int nCell = NCELL(pNode);
180967
180968 for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
180969 RtreeNode *pInsert;
180970 RtreeCell cell;
180971 nodeGetCell(pRtree, pNode, ii, &cell);
180972
180973 /* Find a node to store this cell in. pNode->iNode currently contains
180974 ** the height of the sub-tree headed by the cell.
180975 */
180976 rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
180977 if( rc==SQLITE_OK ){
180978 int rc2;
180979 rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
180980 rc2 = nodeRelease(pRtree, pInsert);
180981 if( rc==SQLITE_OK ){
180982 rc = rc2;
180983 }
180984 }
180985 }
180986 return rc;
180987}
180988
180989/*
180990** Select a currently unused rowid for a new r-tree record.
180991*/
180992static int rtreeNewRowid(Rtree *pRtree, i64 *piRowid){
180993 int rc;
180994 sqlite3_bind_null(pRtree->pWriteRowid, 1);
180995 sqlite3_bind_null(pRtree->pWriteRowid, 2);
180996 sqlite3_step(pRtree->pWriteRowid);
180997 rc = sqlite3_reset(pRtree->pWriteRowid);
180998 *piRowid = sqlite3_last_insert_rowid(pRtree->db);
180999 return rc;
181000}
181001
181002/*
181003** Remove the entry with rowid=iDelete from the r-tree structure.
181004*/
181005static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
181006 int rc; /* Return code */
181007 RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */
181008 int iCell; /* Index of iDelete cell in pLeaf */
181009 RtreeNode *pRoot = 0; /* Root node of rtree structure */
181010
181011
181012 /* Obtain a reference to the root node to initialize Rtree.iDepth */
181013 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
181014
181015 /* Obtain a reference to the leaf node that contains the entry
181016 ** about to be deleted.
181017 */
181018 if( rc==SQLITE_OK ){
181019 rc = findLeafNode(pRtree, iDelete, &pLeaf, 0);
181020 }
181021
181022 /* Delete the cell in question from the leaf node. */
181023 if( rc==SQLITE_OK ){
181024 int rc2;
181025 rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
181026 if( rc==SQLITE_OK ){
181027 rc = deleteCell(pRtree, pLeaf, iCell, 0);
181028 }
181029 rc2 = nodeRelease(pRtree, pLeaf);
181030 if( rc==SQLITE_OK ){
181031 rc = rc2;
181032 }
181033 }
181034
181035 /* Delete the corresponding entry in the <rtree>_rowid table. */
181036 if( rc==SQLITE_OK ){
181037 sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
181038 sqlite3_step(pRtree->pDeleteRowid);
181039 rc = sqlite3_reset(pRtree->pDeleteRowid);
181040 }
181041
181042 /* Check if the root node now has exactly one child. If so, remove
181043 ** it, schedule the contents of the child for reinsertion and
181044 ** reduce the tree height by one.
181045 **
181046 ** This is equivalent to copying the contents of the child into
181047 ** the root node (the operation that Gutman's paper says to perform
181048 ** in this scenario).
181049 */
181050 if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
181051 int rc2;
181052 RtreeNode *pChild = 0;
181053 i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
181054 rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
181055 if( rc==SQLITE_OK ){
181056 rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
181057 }
181058 rc2 = nodeRelease(pRtree, pChild);
181059 if( rc==SQLITE_OK ) rc = rc2;
181060 if( rc==SQLITE_OK ){
181061 pRtree->iDepth--;
181062 writeInt16(pRoot->zData, pRtree->iDepth);
181063 pRoot->isDirty = 1;
181064 }
181065 }
181066
181067 /* Re-insert the contents of any underfull nodes removed from the tree. */
181068 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
181069 if( rc==SQLITE_OK ){
181070 rc = reinsertNodeContent(pRtree, pLeaf);
181071 }
181072 pRtree->pDeleted = pLeaf->pNext;
181073 pRtree->nNodeRef--;
181074 sqlite3_free(pLeaf);
181075 }
181076
181077 /* Release the reference to the root node. */
181078 if( rc==SQLITE_OK ){
181079 rc = nodeRelease(pRtree, pRoot);
181080 }else{
181081 nodeRelease(pRtree, pRoot);
181082 }
181083
181084 return rc;
181085}
181086
181087/*
181088** Rounding constants for float->double conversion.
181089*/
181090#define RNDTOWARDS (1.0 - 1.0/8388608.0) /* Round towards zero */
181091#define RNDAWAY (1.0 + 1.0/8388608.0) /* Round away from zero */
181092
181093#if !defined(SQLITE_RTREE_INT_ONLY)
181094/*
181095** Convert an sqlite3_value into an RtreeValue (presumably a float)
181096** while taking care to round toward negative or positive, respectively.
181097*/
181098static RtreeValue rtreeValueDown(sqlite3_value *v){
181099 double d = sqlite3_value_double(v);
181100 float f = (float)d;
181101 if( f>d ){
181102 f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
181103 }
181104 return f;
181105}
181106static RtreeValue rtreeValueUp(sqlite3_value *v){
181107 double d = sqlite3_value_double(v);
181108 float f = (float)d;
181109 if( f<d ){
181110 f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
181111 }
181112 return f;
181113}
181114#endif /* !defined(SQLITE_RTREE_INT_ONLY) */
181115
181116/*
181117** A constraint has failed while inserting a row into an rtree table.
181118** Assuming no OOM error occurs, this function sets the error message
181119** (at pRtree->base.zErrMsg) to an appropriate value and returns
181120** SQLITE_CONSTRAINT.
181121**
181122** Parameter iCol is the index of the leftmost column involved in the
181123** constraint failure. If it is 0, then the constraint that failed is
181124** the unique constraint on the id column. Otherwise, it is the rtree
181125** (c1<=c2) constraint on columns iCol and iCol+1 that has failed.
181126**
181127** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT.
181128*/
181129static int rtreeConstraintError(Rtree *pRtree, int iCol){
181130 sqlite3_stmt *pStmt = 0;
181131 char *zSql;
181132 int rc;
181133
181134 assert( iCol==0 || iCol%2 );
181135 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
181136 if( zSql ){
181137 rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0);
181138 }else{
181139 rc = SQLITE_NOMEM;
181140 }
181141 sqlite3_free(zSql);
181142
181143 if( rc==SQLITE_OK ){
181144 if( iCol==0 ){
181145 const char *zCol = sqlite3_column_name(pStmt, 0);
181146 pRtree->base.zErrMsg = sqlite3_mprintf(
181147 "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
181148 );
181149 }else{
181150 const char *zCol1 = sqlite3_column_name(pStmt, iCol);
181151 const char *zCol2 = sqlite3_column_name(pStmt, iCol+1);
181152 pRtree->base.zErrMsg = sqlite3_mprintf(
181153 "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
181154 );
181155 }
181156 }
181157
181158 sqlite3_finalize(pStmt);
181159 return (rc==SQLITE_OK ? SQLITE_CONSTRAINT : rc);
181160}
181161
181162
181163
181164/*
181165** The xUpdate method for rtree module virtual tables.
181166*/
181167static int rtreeUpdate(
181168 sqlite3_vtab *pVtab,
181169 int nData,
181170 sqlite3_value **aData,
181171 sqlite_int64 *pRowid
181172){
181173 Rtree *pRtree = (Rtree *)pVtab;
181174 int rc = SQLITE_OK;
181175 RtreeCell cell; /* New cell to insert if nData>1 */
181176 int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
181177
181178 if( pRtree->nNodeRef ){
181179 /* Unable to write to the btree while another cursor is reading from it,
181180 ** since the write might do a rebalance which would disrupt the read
181181 ** cursor. */
181182 return SQLITE_LOCKED_VTAB;
181183 }
181184 rtreeReference(pRtree);
181185 assert(nData>=1);
181186
181187 cell.iRowid = 0; /* Used only to suppress a compiler warning */
181188
181189 /* Constraint handling. A write operation on an r-tree table may return
181190 ** SQLITE_CONSTRAINT for two reasons:
181191 **
181192 ** 1. A duplicate rowid value, or
181193 ** 2. The supplied data violates the "x2>=x1" constraint.
181194 **
181195 ** In the first case, if the conflict-handling mode is REPLACE, then
181196 ** the conflicting row can be removed before proceeding. In the second
181197 ** case, SQLITE_CONSTRAINT must be returned regardless of the
181198 ** conflict-handling mode specified by the user.
181199 */
181200 if( nData>1 ){
181201 int ii;
181202 int nn = nData - 4;
181203
181204 if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
181205 /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
181206 **
181207 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
181208 ** with "column" that are interpreted as table constraints.
181209 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
181210 ** This problem was discovered after years of use, so we silently ignore
181211 ** these kinds of misdeclared tables to avoid breaking any legacy.
181212 */
181213
181214#ifndef SQLITE_RTREE_INT_ONLY
181215 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
181216 for(ii=0; ii<nn; ii+=2){
181217 cell.aCoord[ii].f = rtreeValueDown(aData[ii+3]);
181218 cell.aCoord[ii+1].f = rtreeValueUp(aData[ii+4]);
181219 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
181220 rc = rtreeConstraintError(pRtree, ii+1);
181221 goto constraint;
181222 }
181223 }
181224 }else
181225#endif
181226 {
181227 for(ii=0; ii<nn; ii+=2){
181228 cell.aCoord[ii].i = sqlite3_value_int(aData[ii+3]);
181229 cell.aCoord[ii+1].i = sqlite3_value_int(aData[ii+4]);
181230 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
181231 rc = rtreeConstraintError(pRtree, ii+1);
181232 goto constraint;
181233 }
181234 }
181235 }
181236
181237 /* If a rowid value was supplied, check if it is already present in
181238 ** the table. If so, the constraint has failed. */
181239 if( sqlite3_value_type(aData[2])!=SQLITE_NULL ){
181240 cell.iRowid = sqlite3_value_int64(aData[2]);
181241 if( sqlite3_value_type(aData[0])==SQLITE_NULL
181242 || sqlite3_value_int64(aData[0])!=cell.iRowid
181243 ){
181244 int steprc;
181245 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
181246 steprc = sqlite3_step(pRtree->pReadRowid);
181247 rc = sqlite3_reset(pRtree->pReadRowid);
181248 if( SQLITE_ROW==steprc ){
181249 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
181250 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
181251 }else{
181252 rc = rtreeConstraintError(pRtree, 0);
181253 goto constraint;
181254 }
181255 }
181256 }
181257 bHaveRowid = 1;
181258 }
181259 }
181260
181261 /* If aData[0] is not an SQL NULL value, it is the rowid of a
181262 ** record to delete from the r-tree table. The following block does
181263 ** just that.
181264 */
181265 if( sqlite3_value_type(aData[0])!=SQLITE_NULL ){
181266 rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(aData[0]));
181267 }
181268
181269 /* If the aData[] array contains more than one element, elements
181270 ** (aData[2]..aData[argc-1]) contain a new record to insert into
181271 ** the r-tree structure.
181272 */
181273 if( rc==SQLITE_OK && nData>1 ){
181274 /* Insert the new record into the r-tree */
181275 RtreeNode *pLeaf = 0;
181276
181277 /* Figure out the rowid of the new row. */
181278 if( bHaveRowid==0 ){
181279 rc = rtreeNewRowid(pRtree, &cell.iRowid);
181280 }
181281 *pRowid = cell.iRowid;
181282
181283 if( rc==SQLITE_OK ){
181284 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
181285 }
181286 if( rc==SQLITE_OK ){
181287 int rc2;
181288 pRtree->iReinsertHeight = -1;
181289 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
181290 rc2 = nodeRelease(pRtree, pLeaf);
181291 if( rc==SQLITE_OK ){
181292 rc = rc2;
181293 }
181294 }
181295 if( pRtree->nAux ){
181296 sqlite3_stmt *pUp = pRtree->pWriteAux;
181297 int jj;
181298 sqlite3_bind_int64(pUp, 1, *pRowid);
181299 for(jj=0; jj<pRtree->nAux; jj++){
181300 sqlite3_bind_value(pUp, jj+2, aData[pRtree->nDim2+3+jj]);
181301 }
181302 sqlite3_step(pUp);
181303 rc = sqlite3_reset(pUp);
181304 }
181305 }
181306
181307constraint:
181308 rtreeRelease(pRtree);
181309 return rc;
181310}
181311
181312/*
181313** Called when a transaction starts.
181314*/
181315static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
181316 Rtree *pRtree = (Rtree *)pVtab;
181317 assert( pRtree->inWrTrans==0 );
181318 pRtree->inWrTrans++;
181319 return SQLITE_OK;
181320}
181321
181322/*
181323** Called when a transaction completes (either by COMMIT or ROLLBACK).
181324** The sqlite3_blob object should be released at this point.
181325*/
181326static int rtreeEndTransaction(sqlite3_vtab *pVtab){
181327 Rtree *pRtree = (Rtree *)pVtab;
181328 pRtree->inWrTrans = 0;
181329 nodeBlobReset(pRtree);
181330 return SQLITE_OK;
181331}
181332
181333/*
181334** The xRename method for rtree module virtual tables.
181335*/
181336static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
181337 Rtree *pRtree = (Rtree *)pVtab;
181338 int rc = SQLITE_NOMEM;
181339 char *zSql = sqlite3_mprintf(
181340 "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";"
181341 "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
181342 "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";"
181343 , pRtree->zDb, pRtree->zName, zNewName
181344 , pRtree->zDb, pRtree->zName, zNewName
181345 , pRtree->zDb, pRtree->zName, zNewName
181346 );
181347 if( zSql ){
181348 nodeBlobReset(pRtree);
181349 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
181350 sqlite3_free(zSql);
181351 }
181352 return rc;
181353}
181354
181355/*
181356** The xSavepoint method.
181357**
181358** This module does not need to do anything to support savepoints. However,
181359** it uses this hook to close any open blob handle. This is done because a
181360** DROP TABLE command - which fortunately always opens a savepoint - cannot
181361** succeed if there are any open blob handles. i.e. if the blob handle were
181362** not closed here, the following would fail:
181363**
181364** BEGIN;
181365** INSERT INTO rtree...
181366** DROP TABLE <tablename>; -- Would fail with SQLITE_LOCKED
181367** COMMIT;
181368*/
181369static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
181370 Rtree *pRtree = (Rtree *)pVtab;
181371 u8 iwt = pRtree->inWrTrans;
181372 UNUSED_PARAMETER(iSavepoint);
181373 pRtree->inWrTrans = 0;
181374 nodeBlobReset(pRtree);
181375 pRtree->inWrTrans = iwt;
181376 return SQLITE_OK;
181377}
181378
181379/*
181380** This function populates the pRtree->nRowEst variable with an estimate
181381** of the number of rows in the virtual table. If possible, this is based
181382** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
181383*/
181384static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
181385 const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'";
181386 char *zSql;
181387 sqlite3_stmt *p;
181388 int rc;
181389 i64 nRow = 0;
181390
181391 rc = sqlite3_table_column_metadata(
181392 db, pRtree->zDb, "sqlite_stat1",0,0,0,0,0,0
181393 );
181394 if( rc!=SQLITE_OK ){
181395 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
181396 return rc==SQLITE_ERROR ? SQLITE_OK : rc;
181397 }
181398 zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
181399 if( zSql==0 ){
181400 rc = SQLITE_NOMEM;
181401 }else{
181402 rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
181403 if( rc==SQLITE_OK ){
181404 if( sqlite3_step(p)==SQLITE_ROW ) nRow = sqlite3_column_int64(p, 0);
181405 rc = sqlite3_finalize(p);
181406 }else if( rc!=SQLITE_NOMEM ){
181407 rc = SQLITE_OK;
181408 }
181409
181410 if( rc==SQLITE_OK ){
181411 if( nRow==0 ){
181412 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
181413 }else{
181414 pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
181415 }
181416 }
181417 sqlite3_free(zSql);
181418 }
181419
181420 return rc;
181421}
181422
181423static sqlite3_module rtreeModule = {
181424 2, /* iVersion */
181425 rtreeCreate, /* xCreate - create a table */
181426 rtreeConnect, /* xConnect - connect to an existing table */
181427 rtreeBestIndex, /* xBestIndex - Determine search strategy */
181428 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
181429 rtreeDestroy, /* xDestroy - Drop a table */
181430 rtreeOpen, /* xOpen - open a cursor */
181431 rtreeClose, /* xClose - close a cursor */
181432 rtreeFilter, /* xFilter - configure scan constraints */
181433 rtreeNext, /* xNext - advance a cursor */
181434 rtreeEof, /* xEof */
181435 rtreeColumn, /* xColumn - read data */
181436 rtreeRowid, /* xRowid - read data */
181437 rtreeUpdate, /* xUpdate - write data */
181438 rtreeBeginTransaction, /* xBegin - begin transaction */
181439 rtreeEndTransaction, /* xSync - sync transaction */
181440 rtreeEndTransaction, /* xCommit - commit transaction */
181441 rtreeEndTransaction, /* xRollback - rollback transaction */
181442 0, /* xFindFunction - function overloading */
181443 rtreeRename, /* xRename - rename the table */
181444 rtreeSavepoint, /* xSavepoint */
181445 0, /* xRelease */
181446 0, /* xRollbackTo */
181447};
181448
181449static int rtreeSqlInit(
181450 Rtree *pRtree,
181451 sqlite3 *db,
181452 const char *zDb,
181453 const char *zPrefix,
181454 int isCreate
181455){
181456 int rc = SQLITE_OK;
181457
181458 #define N_STATEMENT 8
181459 static const char *azSql[N_STATEMENT] = {
181460 /* Write the xxx_node table */
181461 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
181462 "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
181463
181464 /* Read and write the xxx_rowid table */
181465 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
181466 "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
181467 "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
181468
181469 /* Read and write the xxx_parent table */
181470 "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
181471 "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
181472 "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
181473 };
181474 sqlite3_stmt **appStmt[N_STATEMENT];
181475 int i;
181476
181477 pRtree->db = db;
181478
181479 if( isCreate ){
181480 char *zCreate;
181481 sqlite3_str *p = sqlite3_str_new(db);
181482 int ii;
181483 sqlite3_str_appendf(p,
181484 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
181485 zDb, zPrefix);
181486 for(ii=0; ii<pRtree->nAux; ii++){
181487 sqlite3_str_appendf(p,",a%d",ii);
181488 }
181489 sqlite3_str_appendf(p,
181490 ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
181491 zDb, zPrefix);
181492 sqlite3_str_appendf(p,
181493 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
181494 zDb, zPrefix);
181495 sqlite3_str_appendf(p,
181496 "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
181497 zDb, zPrefix, pRtree->iNodeSize);
181498 zCreate = sqlite3_str_finish(p);
181499 if( !zCreate ){
181500 return SQLITE_NOMEM;
181501 }
181502 rc = sqlite3_exec(db, zCreate, 0, 0, 0);
181503 sqlite3_free(zCreate);
181504 if( rc!=SQLITE_OK ){
181505 return rc;
181506 }
181507 }
181508
181509 appStmt[0] = &pRtree->pWriteNode;
181510 appStmt[1] = &pRtree->pDeleteNode;
181511 appStmt[2] = &pRtree->pReadRowid;
181512 appStmt[3] = &pRtree->pWriteRowid;
181513 appStmt[4] = &pRtree->pDeleteRowid;
181514 appStmt[5] = &pRtree->pReadParent;
181515 appStmt[6] = &pRtree->pWriteParent;
181516 appStmt[7] = &pRtree->pDeleteParent;
181517
181518 rc = rtreeQueryStat1(db, pRtree);
181519 for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
181520 char *zSql;
181521 const char *zFormat;
181522 if( i!=3 || pRtree->nAux==0 ){
181523 zFormat = azSql[i];
181524 }else {
181525 /* An UPSERT is very slightly slower than REPLACE, but it is needed
181526 ** if there are auxiliary columns */
181527 zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
181528 "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
181529 }
181530 zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
181531 if( zSql ){
181532 rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
181533 appStmt[i], 0);
181534 }else{
181535 rc = SQLITE_NOMEM;
181536 }
181537 sqlite3_free(zSql);
181538 }
181539 if( pRtree->nAux ){
181540 pRtree->zReadAuxSql = sqlite3_mprintf(
181541 "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
181542 zDb, zPrefix);
181543 if( pRtree->zReadAuxSql==0 ){
181544 rc = SQLITE_NOMEM;
181545 }else{
181546 sqlite3_str *p = sqlite3_str_new(db);
181547 int ii;
181548 char *zSql;
181549 sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
181550 for(ii=0; ii<pRtree->nAux; ii++){
181551 if( ii ) sqlite3_str_append(p, ",", 1);
181552 if( ii<pRtree->nAuxNotNull ){
181553 sqlite3_str_appendf(p,"a%d=coalesce(?%d,a%d)",ii,ii+2,ii);
181554 }else{
181555 sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
181556 }
181557 }
181558 sqlite3_str_appendf(p, " WHERE rowid=?1");
181559 zSql = sqlite3_str_finish(p);
181560 if( zSql==0 ){
181561 rc = SQLITE_NOMEM;
181562 }else{
181563 rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT,
181564 &pRtree->pWriteAux, 0);
181565 sqlite3_free(zSql);
181566 }
181567 }
181568 }
181569
181570 return rc;
181571}
181572
181573/*
181574** The second argument to this function contains the text of an SQL statement
181575** that returns a single integer value. The statement is compiled and executed
181576** using database connection db. If successful, the integer value returned
181577** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
181578** code is returned and the value of *piVal after returning is not defined.
181579*/
181580static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
181581 int rc = SQLITE_NOMEM;
181582 if( zSql ){
181583 sqlite3_stmt *pStmt = 0;
181584 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
181585 if( rc==SQLITE_OK ){
181586 if( SQLITE_ROW==sqlite3_step(pStmt) ){
181587 *piVal = sqlite3_column_int(pStmt, 0);
181588 }
181589 rc = sqlite3_finalize(pStmt);
181590 }
181591 }
181592 return rc;
181593}
181594
181595/*
181596** This function is called from within the xConnect() or xCreate() method to
181597** determine the node-size used by the rtree table being created or connected
181598** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
181599** Otherwise, an SQLite error code is returned.
181600**
181601** If this function is being called as part of an xConnect(), then the rtree
181602** table already exists. In this case the node-size is determined by inspecting
181603** the root node of the tree.
181604**
181605** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
181606** This ensures that each node is stored on a single database page. If the
181607** database page-size is so large that more than RTREE_MAXCELLS entries
181608** would fit in a single node, use a smaller node-size.
181609*/
181610static int getNodeSize(
181611 sqlite3 *db, /* Database handle */
181612 Rtree *pRtree, /* Rtree handle */
181613 int isCreate, /* True for xCreate, false for xConnect */
181614 char **pzErr /* OUT: Error message, if any */
181615){
181616 int rc;
181617 char *zSql;
181618 if( isCreate ){
181619 int iPageSize = 0;
181620 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
181621 rc = getIntFromStmt(db, zSql, &iPageSize);
181622 if( rc==SQLITE_OK ){
181623 pRtree->iNodeSize = iPageSize-64;
181624 if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
181625 pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
181626 }
181627 }else{
181628 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
181629 }
181630 }else{
181631 zSql = sqlite3_mprintf(
181632 "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
181633 pRtree->zDb, pRtree->zName
181634 );
181635 rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
181636 if( rc!=SQLITE_OK ){
181637 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
181638 }else if( pRtree->iNodeSize<(512-64) ){
181639 rc = SQLITE_CORRUPT_VTAB;
181640 *pzErr = sqlite3_mprintf("undersize RTree blobs in \"%q_node\"",
181641 pRtree->zName);
181642 }
181643 }
181644
181645 sqlite3_free(zSql);
181646 return rc;
181647}
181648
181649/*
181650** This function is the implementation of both the xConnect and xCreate
181651** methods of the r-tree virtual table.
181652**
181653** argv[0] -> module name
181654** argv[1] -> database name
181655** argv[2] -> table name
181656** argv[...] -> column names...
181657*/
181658static int rtreeInit(
181659 sqlite3 *db, /* Database connection */
181660 void *pAux, /* One of the RTREE_COORD_* constants */
181661 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
181662 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
181663 char **pzErr, /* OUT: Error message, if any */
181664 int isCreate /* True for xCreate, false for xConnect */
181665){
181666 int rc = SQLITE_OK;
181667 Rtree *pRtree;
181668 int nDb; /* Length of string argv[1] */
181669 int nName; /* Length of string argv[2] */
181670 int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
181671 sqlite3_str *pSql;
181672 char *zSql;
181673 int ii = 4;
181674 int iErr;
181675
181676 const char *aErrMsg[] = {
181677 0, /* 0 */
181678 "Wrong number of columns for an rtree table", /* 1 */
181679 "Too few columns for an rtree table", /* 2 */
181680 "Too many columns for an rtree table", /* 3 */
181681 "Auxiliary rtree columns must be last" /* 4 */
181682 };
181683
181684 assert( RTREE_MAX_AUX_COLUMN<256 ); /* Aux columns counted by a u8 */
181685 if( argc>RTREE_MAX_AUX_COLUMN+3 ){
181686 *pzErr = sqlite3_mprintf("%s", aErrMsg[3]);
181687 return SQLITE_ERROR;
181688 }
181689
181690 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
181691
181692 /* Allocate the sqlite3_vtab structure */
181693 nDb = (int)strlen(argv[1]);
181694 nName = (int)strlen(argv[2]);
181695 pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
181696 if( !pRtree ){
181697 return SQLITE_NOMEM;
181698 }
181699 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
181700 pRtree->nBusy = 1;
181701 pRtree->base.pModule = &rtreeModule;
181702 pRtree->zDb = (char *)&pRtree[1];
181703 pRtree->zName = &pRtree->zDb[nDb+1];
181704 pRtree->eCoordType = (u8)eCoordType;
181705 memcpy(pRtree->zDb, argv[1], nDb);
181706 memcpy(pRtree->zName, argv[2], nName);
181707
181708
181709 /* Create/Connect to the underlying relational database schema. If
181710 ** that is successful, call sqlite3_declare_vtab() to configure
181711 ** the r-tree table schema.
181712 */
181713 pSql = sqlite3_str_new(db);
181714 sqlite3_str_appendf(pSql, "CREATE TABLE x(%s", argv[3]);
181715 for(ii=4; ii<argc; ii++){
181716 if( argv[ii][0]=='+' ){
181717 pRtree->nAux++;
181718 sqlite3_str_appendf(pSql, ",%s", argv[ii]+1);
181719 }else if( pRtree->nAux>0 ){
181720 break;
181721 }else{
181722 pRtree->nDim2++;
181723 sqlite3_str_appendf(pSql, ",%s", argv[ii]);
181724 }
181725 }
181726 sqlite3_str_appendf(pSql, ");");
181727 zSql = sqlite3_str_finish(pSql);
181728 if( !zSql ){
181729 rc = SQLITE_NOMEM;
181730 }else if( ii<argc ){
181731 *pzErr = sqlite3_mprintf("%s", aErrMsg[4]);
181732 rc = SQLITE_ERROR;
181733 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
181734 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
181735 }
181736 sqlite3_free(zSql);
181737 if( rc ) goto rtreeInit_fail;
181738 pRtree->nDim = pRtree->nDim2/2;
181739 if( pRtree->nDim<1 ){
181740 iErr = 2;
181741 }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS*2 ){
181742 iErr = 3;
181743 }else if( pRtree->nDim2 % 2 ){
181744 iErr = 1;
181745 }else{
181746 iErr = 0;
181747 }
181748 if( iErr ){
181749 *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
181750 goto rtreeInit_fail;
181751 }
181752 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
181753
181754 /* Figure out the node size to use. */
181755 rc = getNodeSize(db, pRtree, isCreate, pzErr);
181756 if( rc ) goto rtreeInit_fail;
181757 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
181758 if( rc ){
181759 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
181760 goto rtreeInit_fail;
181761 }
181762
181763 *ppVtab = (sqlite3_vtab *)pRtree;
181764 return SQLITE_OK;
181765
181766rtreeInit_fail:
181767 if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
181768 assert( *ppVtab==0 );
181769 assert( pRtree->nBusy==1 );
181770 rtreeRelease(pRtree);
181771 return rc;
181772}
181773
181774
181775/*
181776** Implementation of a scalar function that decodes r-tree nodes to
181777** human readable strings. This can be used for debugging and analysis.
181778**
181779** The scalar function takes two arguments: (1) the number of dimensions
181780** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
181781** an r-tree node. For a two-dimensional r-tree structure called "rt", to
181782** deserialize all nodes, a statement like:
181783**
181784** SELECT rtreenode(2, data) FROM rt_node;
181785**
181786** The human readable string takes the form of a Tcl list with one
181787** entry for each cell in the r-tree node. Each entry is itself a
181788** list, containing the 8-byte rowid/pageno followed by the
181789** <num-dimension>*2 coordinates.
181790*/
181791static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
181792 char *zText = 0;
181793 RtreeNode node;
181794 Rtree tree;
181795 int ii;
181796
181797 UNUSED_PARAMETER(nArg);
181798 memset(&node, 0, sizeof(RtreeNode));
181799 memset(&tree, 0, sizeof(Rtree));
181800 tree.nDim = (u8)sqlite3_value_int(apArg[0]);
181801 tree.nDim2 = tree.nDim*2;
181802 tree.nBytesPerCell = 8 + 8 * tree.nDim;
181803 node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
181804
181805 for(ii=0; ii<NCELL(&node); ii++){
181806 char zCell[512];
181807 int nCell = 0;
181808 RtreeCell cell;
181809 int jj;
181810
181811 nodeGetCell(&tree, &node, ii, &cell);
181812 sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
181813 nCell = (int)strlen(zCell);
181814 for(jj=0; jj<tree.nDim2; jj++){
181815#ifndef SQLITE_RTREE_INT_ONLY
181816 sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
181817 (double)cell.aCoord[jj].f);
181818#else
181819 sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
181820 cell.aCoord[jj].i);
181821#endif
181822 nCell = (int)strlen(zCell);
181823 }
181824
181825 if( zText ){
181826 char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
181827 sqlite3_free(zText);
181828 zText = zTextNew;
181829 }else{
181830 zText = sqlite3_mprintf("{%s}", zCell);
181831 }
181832 }
181833
181834 sqlite3_result_text(ctx, zText, -1, sqlite3_free);
181835}
181836
181837/* This routine implements an SQL function that returns the "depth" parameter
181838** from the front of a blob that is an r-tree node. For example:
181839**
181840** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
181841**
181842** The depth value is 0 for all nodes other than the root node, and the root
181843** node always has nodeno=1, so the example above is the primary use for this
181844** routine. This routine is intended for testing and analysis only.
181845*/
181846static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
181847 UNUSED_PARAMETER(nArg);
181848 if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
181849 || sqlite3_value_bytes(apArg[0])<2
181850 ){
181851 sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
181852 }else{
181853 u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
181854 sqlite3_result_int(ctx, readInt16(zBlob));
181855 }
181856}
181857
181858/*
181859** Context object passed between the various routines that make up the
181860** implementation of integrity-check function rtreecheck().
181861*/
181862typedef struct RtreeCheck RtreeCheck;
181863struct RtreeCheck {
181864 sqlite3 *db; /* Database handle */
181865 const char *zDb; /* Database containing rtree table */
181866 const char *zTab; /* Name of rtree table */
181867 int bInt; /* True for rtree_i32 table */
181868 int nDim; /* Number of dimensions for this rtree tbl */
181869 sqlite3_stmt *pGetNode; /* Statement used to retrieve nodes */
181870 sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */
181871 int nLeaf; /* Number of leaf cells in table */
181872 int nNonLeaf; /* Number of non-leaf cells in table */
181873 int rc; /* Return code */
181874 char *zReport; /* Message to report */
181875 int nErr; /* Number of lines in zReport */
181876};
181877
181878#define RTREE_CHECK_MAX_ERROR 100
181879
181880/*
181881** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error,
181882** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code.
181883*/
181884static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){
181885 int rc = sqlite3_reset(pStmt);
181886 if( pCheck->rc==SQLITE_OK ) pCheck->rc = rc;
181887}
181888
181889/*
181890** The second and subsequent arguments to this function are a format string
181891** and printf style arguments. This function formats the string and attempts
181892** to compile it as an SQL statement.
181893**
181894** If successful, a pointer to the new SQL statement is returned. Otherwise,
181895** NULL is returned and an error code left in RtreeCheck.rc.
181896*/
181897static sqlite3_stmt *rtreeCheckPrepare(
181898 RtreeCheck *pCheck, /* RtreeCheck object */
181899 const char *zFmt, ... /* Format string and trailing args */
181900){
181901 va_list ap;
181902 char *z;
181903 sqlite3_stmt *pRet = 0;
181904
181905 va_start(ap, zFmt);
181906 z = sqlite3_vmprintf(zFmt, ap);
181907
181908 if( pCheck->rc==SQLITE_OK ){
181909 if( z==0 ){
181910 pCheck->rc = SQLITE_NOMEM;
181911 }else{
181912 pCheck->rc = sqlite3_prepare_v2(pCheck->db, z, -1, &pRet, 0);
181913 }
181914 }
181915
181916 sqlite3_free(z);
181917 va_end(ap);
181918 return pRet;
181919}
181920
181921/*
181922** The second and subsequent arguments to this function are a printf()
181923** style format string and arguments. This function formats the string and
181924** appends it to the report being accumuated in pCheck.
181925*/
181926static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){
181927 va_list ap;
181928 va_start(ap, zFmt);
181929 if( pCheck->rc==SQLITE_OK && pCheck->nErr<RTREE_CHECK_MAX_ERROR ){
181930 char *z = sqlite3_vmprintf(zFmt, ap);
181931 if( z==0 ){
181932 pCheck->rc = SQLITE_NOMEM;
181933 }else{
181934 pCheck->zReport = sqlite3_mprintf("%z%s%z",
181935 pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
181936 );
181937 if( pCheck->zReport==0 ){
181938 pCheck->rc = SQLITE_NOMEM;
181939 }
181940 }
181941 pCheck->nErr++;
181942 }
181943 va_end(ap);
181944}
181945
181946/*
181947** This function is a no-op if there is already an error code stored
181948** in the RtreeCheck object indicated by the first argument. NULL is
181949** returned in this case.
181950**
181951** Otherwise, the contents of rtree table node iNode are loaded from
181952** the database and copied into a buffer obtained from sqlite3_malloc().
181953** If no error occurs, a pointer to the buffer is returned and (*pnNode)
181954** is set to the size of the buffer in bytes.
181955**
181956** Or, if an error does occur, NULL is returned and an error code left
181957** in the RtreeCheck object. The final value of *pnNode is undefined in
181958** this case.
181959*/
181960static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){
181961 u8 *pRet = 0; /* Return value */
181962
181963 assert( pCheck->rc==SQLITE_OK );
181964 if( pCheck->pGetNode==0 ){
181965 pCheck->pGetNode = rtreeCheckPrepare(pCheck,
181966 "SELECT data FROM %Q.'%q_node' WHERE nodeno=?",
181967 pCheck->zDb, pCheck->zTab
181968 );
181969 }
181970
181971 if( pCheck->rc==SQLITE_OK ){
181972 sqlite3_bind_int64(pCheck->pGetNode, 1, iNode);
181973 if( sqlite3_step(pCheck->pGetNode)==SQLITE_ROW ){
181974 int nNode = sqlite3_column_bytes(pCheck->pGetNode, 0);
181975 const u8 *pNode = (const u8*)sqlite3_column_blob(pCheck->pGetNode, 0);
181976 pRet = sqlite3_malloc(nNode);
181977 if( pRet==0 ){
181978 pCheck->rc = SQLITE_NOMEM;
181979 }else{
181980 memcpy(pRet, pNode, nNode);
181981 *pnNode = nNode;
181982 }
181983 }
181984 rtreeCheckReset(pCheck, pCheck->pGetNode);
181985 if( pCheck->rc==SQLITE_OK && pRet==0 ){
181986 rtreeCheckAppendMsg(pCheck, "Node %lld missing from database", iNode);
181987 }
181988 }
181989
181990 return pRet;
181991}
181992
181993/*
181994** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
181995** (if bLeaf==1) table contains a specified entry. The schemas of the
181996** two tables are:
181997**
181998** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
181999** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
182000**
182001** In both cases, this function checks that there exists an entry with
182002** IPK value iKey and the second column set to iVal.
182003**
182004*/
182005static void rtreeCheckMapping(
182006 RtreeCheck *pCheck, /* RtreeCheck object */
182007 int bLeaf, /* True for a leaf cell, false for interior */
182008 i64 iKey, /* Key for mapping */
182009 i64 iVal /* Expected value for mapping */
182010){
182011 int rc;
182012 sqlite3_stmt *pStmt;
182013 const char *azSql[2] = {
182014 "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
182015 "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
182016 };
182017
182018 assert( bLeaf==0 || bLeaf==1 );
182019 if( pCheck->aCheckMapping[bLeaf]==0 ){
182020 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
182021 azSql[bLeaf], pCheck->zDb, pCheck->zTab
182022 );
182023 }
182024 if( pCheck->rc!=SQLITE_OK ) return;
182025
182026 pStmt = pCheck->aCheckMapping[bLeaf];
182027 sqlite3_bind_int64(pStmt, 1, iKey);
182028 rc = sqlite3_step(pStmt);
182029 if( rc==SQLITE_DONE ){
182030 rtreeCheckAppendMsg(pCheck, "Mapping (%lld -> %lld) missing from %s table",
182031 iKey, iVal, (bLeaf ? "%_rowid" : "%_parent")
182032 );
182033 }else if( rc==SQLITE_ROW ){
182034 i64 ii = sqlite3_column_int64(pStmt, 0);
182035 if( ii!=iVal ){
182036 rtreeCheckAppendMsg(pCheck,
182037 "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
182038 iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal
182039 );
182040 }
182041 }
182042 rtreeCheckReset(pCheck, pStmt);
182043}
182044
182045/*
182046** Argument pCell points to an array of coordinates stored on an rtree page.
182047** This function checks that the coordinates are internally consistent (no
182048** x1>x2 conditions) and adds an error message to the RtreeCheck object
182049** if they are not.
182050**
182051** Additionally, if pParent is not NULL, then it is assumed to point to
182052** the array of coordinates on the parent page that bound the page
182053** containing pCell. In this case it is also verified that the two
182054** sets of coordinates are mutually consistent and an error message added
182055** to the RtreeCheck object if they are not.
182056*/
182057static void rtreeCheckCellCoord(
182058 RtreeCheck *pCheck,
182059 i64 iNode, /* Node id to use in error messages */
182060 int iCell, /* Cell number to use in error messages */
182061 u8 *pCell, /* Pointer to cell coordinates */
182062 u8 *pParent /* Pointer to parent coordinates */
182063){
182064 RtreeCoord c1, c2;
182065 RtreeCoord p1, p2;
182066 int i;
182067
182068 for(i=0; i<pCheck->nDim; i++){
182069 readCoord(&pCell[4*2*i], &c1);
182070 readCoord(&pCell[4*(2*i + 1)], &c2);
182071
182072 /* printf("%e, %e\n", c1.u.f, c2.u.f); */
182073 if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
182074 rtreeCheckAppendMsg(pCheck,
182075 "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode
182076 );
182077 }
182078
182079 if( pParent ){
182080 readCoord(&pParent[4*2*i], &p1);
182081 readCoord(&pParent[4*(2*i + 1)], &p2);
182082
182083 if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f)
182084 || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
182085 ){
182086 rtreeCheckAppendMsg(pCheck,
182087 "Dimension %d of cell %d on node %lld is corrupt relative to parent"
182088 , i, iCell, iNode
182089 );
182090 }
182091 }
182092 }
182093}
182094
182095/*
182096** Run rtreecheck() checks on node iNode, which is at depth iDepth within
182097** the r-tree structure. Argument aParent points to the array of coordinates
182098** that bound node iNode on the parent node.
182099**
182100** If any problems are discovered, an error message is appended to the
182101** report accumulated in the RtreeCheck object.
182102*/
182103static void rtreeCheckNode(
182104 RtreeCheck *pCheck,
182105 int iDepth, /* Depth of iNode (0==leaf) */
182106 u8 *aParent, /* Buffer containing parent coords */
182107 i64 iNode /* Node to check */
182108){
182109 u8 *aNode = 0;
182110 int nNode = 0;
182111
182112 assert( iNode==1 || aParent!=0 );
182113 assert( pCheck->nDim>0 );
182114
182115 aNode = rtreeCheckGetNode(pCheck, iNode, &nNode);
182116 if( aNode ){
182117 if( nNode<4 ){
182118 rtreeCheckAppendMsg(pCheck,
182119 "Node %lld is too small (%d bytes)", iNode, nNode
182120 );
182121 }else{
182122 int nCell; /* Number of cells on page */
182123 int i; /* Used to iterate through cells */
182124 if( aParent==0 ){
182125 iDepth = readInt16(aNode);
182126 if( iDepth>RTREE_MAX_DEPTH ){
182127 rtreeCheckAppendMsg(pCheck, "Rtree depth out of range (%d)", iDepth);
182128 sqlite3_free(aNode);
182129 return;
182130 }
182131 }
182132 nCell = readInt16(&aNode[2]);
182133 if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
182134 rtreeCheckAppendMsg(pCheck,
182135 "Node %lld is too small for cell count of %d (%d bytes)",
182136 iNode, nCell, nNode
182137 );
182138 }else{
182139 for(i=0; i<nCell; i++){
182140 u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
182141 i64 iVal = readInt64(pCell);
182142 rtreeCheckCellCoord(pCheck, iNode, i, &pCell[8], aParent);
182143
182144 if( iDepth>0 ){
182145 rtreeCheckMapping(pCheck, 0, iVal, iNode);
182146 rtreeCheckNode(pCheck, iDepth-1, &pCell[8], iVal);
182147 pCheck->nNonLeaf++;
182148 }else{
182149 rtreeCheckMapping(pCheck, 1, iVal, iNode);
182150 pCheck->nLeaf++;
182151 }
182152 }
182153 }
182154 }
182155 sqlite3_free(aNode);
182156 }
182157}
182158
182159/*
182160** The second argument to this function must be either "_rowid" or
182161** "_parent". This function checks that the number of entries in the
182162** %_rowid or %_parent table is exactly nExpect. If not, it adds
182163** an error message to the report in the RtreeCheck object indicated
182164** by the first argument.
182165*/
182166static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){
182167 if( pCheck->rc==SQLITE_OK ){
182168 sqlite3_stmt *pCount;
182169 pCount = rtreeCheckPrepare(pCheck, "SELECT count(*) FROM %Q.'%q%s'",
182170 pCheck->zDb, pCheck->zTab, zTbl
182171 );
182172 if( pCount ){
182173 if( sqlite3_step(pCount)==SQLITE_ROW ){
182174 i64 nActual = sqlite3_column_int64(pCount, 0);
182175 if( nActual!=nExpect ){
182176 rtreeCheckAppendMsg(pCheck, "Wrong number of entries in %%%s table"
182177 " - expected %lld, actual %lld" , zTbl, nExpect, nActual
182178 );
182179 }
182180 }
182181 pCheck->rc = sqlite3_finalize(pCount);
182182 }
182183 }
182184}
182185
182186/*
182187** This function does the bulk of the work for the rtree integrity-check.
182188** It is called by rtreecheck(), which is the SQL function implementation.
182189*/
182190static int rtreeCheckTable(
182191 sqlite3 *db, /* Database handle to access db through */
182192 const char *zDb, /* Name of db ("main", "temp" etc.) */
182193 const char *zTab, /* Name of rtree table to check */
182194 char **pzReport /* OUT: sqlite3_malloc'd report text */
182195){
182196 RtreeCheck check; /* Common context for various routines */
182197 sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
182198 int bEnd = 0; /* True if transaction should be closed */
182199 int nAux = 0; /* Number of extra columns. */
182200
182201 /* Initialize the context object */
182202 memset(&check, 0, sizeof(check));
182203 check.db = db;
182204 check.zDb = zDb;
182205 check.zTab = zTab;
182206
182207 /* If there is not already an open transaction, open one now. This is
182208 ** to ensure that the queries run as part of this integrity-check operate
182209 ** on a consistent snapshot. */
182210 if( sqlite3_get_autocommit(db) ){
182211 check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
182212 bEnd = 1;
182213 }
182214
182215 /* Find the number of auxiliary columns */
182216 if( check.rc==SQLITE_OK ){
182217 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
182218 if( pStmt ){
182219 nAux = sqlite3_column_count(pStmt) - 2;
182220 sqlite3_finalize(pStmt);
182221 }
182222 check.rc = SQLITE_OK;
182223 }
182224
182225 /* Find number of dimensions in the rtree table. */
182226 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
182227 if( pStmt ){
182228 int rc;
182229 check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
182230 if( check.nDim<1 ){
182231 rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
182232 }else if( SQLITE_ROW==sqlite3_step(pStmt) ){
182233 check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER);
182234 }
182235 rc = sqlite3_finalize(pStmt);
182236 if( rc!=SQLITE_CORRUPT ) check.rc = rc;
182237 }
182238
182239 /* Do the actual integrity-check */
182240 if( check.nDim>=1 ){
182241 if( check.rc==SQLITE_OK ){
182242 rtreeCheckNode(&check, 0, 0, 1);
182243 }
182244 rtreeCheckCount(&check, "_rowid", check.nLeaf);
182245 rtreeCheckCount(&check, "_parent", check.nNonLeaf);
182246 }
182247
182248 /* Finalize SQL statements used by the integrity-check */
182249 sqlite3_finalize(check.pGetNode);
182250 sqlite3_finalize(check.aCheckMapping[0]);
182251 sqlite3_finalize(check.aCheckMapping[1]);
182252
182253 /* If one was opened, close the transaction */
182254 if( bEnd ){
182255 int rc = sqlite3_exec(db, "END", 0, 0, 0);
182256 if( check.rc==SQLITE_OK ) check.rc = rc;
182257 }
182258 *pzReport = check.zReport;
182259 return check.rc;
182260}
182261
182262/*
182263** Usage:
182264**
182265** rtreecheck(<rtree-table>);
182266** rtreecheck(<database>, <rtree-table>);
182267**
182268** Invoking this SQL function runs an integrity-check on the named rtree
182269** table. The integrity-check verifies the following:
182270**
182271** 1. For each cell in the r-tree structure (%_node table), that:
182272**
182273** a) for each dimension, (coord1 <= coord2).
182274**
182275** b) unless the cell is on the root node, that the cell is bounded
182276** by the parent cell on the parent node.
182277**
182278** c) for leaf nodes, that there is an entry in the %_rowid
182279** table corresponding to the cell's rowid value that
182280** points to the correct node.
182281**
182282** d) for cells on non-leaf nodes, that there is an entry in the
182283** %_parent table mapping from the cell's child node to the
182284** node that it resides on.
182285**
182286** 2. That there are the same number of entries in the %_rowid table
182287** as there are leaf cells in the r-tree structure, and that there
182288** is a leaf cell that corresponds to each entry in the %_rowid table.
182289**
182290** 3. That there are the same number of entries in the %_parent table
182291** as there are non-leaf cells in the r-tree structure, and that
182292** there is a non-leaf cell that corresponds to each entry in the
182293** %_parent table.
182294*/
182295static void rtreecheck(
182296 sqlite3_context *ctx,
182297 int nArg,
182298 sqlite3_value **apArg
182299){
182300 if( nArg!=1 && nArg!=2 ){
182301 sqlite3_result_error(ctx,
182302 "wrong number of arguments to function rtreecheck()", -1
182303 );
182304 }else{
182305 int rc;
182306 char *zReport = 0;
182307 const char *zDb = (const char*)sqlite3_value_text(apArg[0]);
182308 const char *zTab;
182309 if( nArg==1 ){
182310 zTab = zDb;
182311 zDb = "main";
182312 }else{
182313 zTab = (const char*)sqlite3_value_text(apArg[1]);
182314 }
182315 rc = rtreeCheckTable(sqlite3_context_db_handle(ctx), zDb, zTab, &zReport);
182316 if( rc==SQLITE_OK ){
182317 sqlite3_result_text(ctx, zReport ? zReport : "ok", -1, SQLITE_TRANSIENT);
182318 }else{
182319 sqlite3_result_error_code(ctx, rc);
182320 }
182321 sqlite3_free(zReport);
182322 }
182323}
182324
182325/* Conditionally include the geopoly code */
182326#ifdef SQLITE_ENABLE_GEOPOLY
182327/************** Include geopoly.c in the middle of rtree.c *******************/
182328/************** Begin file geopoly.c *****************************************/
182329/*
182330** 2018-05-25
182331**
182332** The author disclaims copyright to this source code. In place of
182333** a legal notice, here is a blessing:
182334**
182335** May you do good and not evil.
182336** May you find forgiveness for yourself and forgive others.
182337** May you share freely, never taking more than you give.
182338**
182339******************************************************************************
182340**
182341** This file implements an alternative R-Tree virtual table that
182342** uses polygons to express the boundaries of 2-dimensional objects.
182343**
182344** This file is #include-ed onto the end of "rtree.c" so that it has
182345** access to all of the R-Tree internals.
182346*/
182347/* #include <stdlib.h> */
182348
182349/* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */
182350#ifdef GEOPOLY_ENABLE_DEBUG
182351 static int geo_debug = 0;
182352# define GEODEBUG(X) if(geo_debug)printf X
182353#else
182354# define GEODEBUG(X)
182355#endif
182356
182357#ifndef JSON_NULL /* The following stuff repeats things found in json1 */
182358/*
182359** Versions of isspace(), isalnum() and isdigit() to which it is safe
182360** to pass signed char values.
182361*/
182362#ifdef sqlite3Isdigit
182363 /* Use the SQLite core versions if this routine is part of the
182364 ** SQLite amalgamation */
182365# define safe_isdigit(x) sqlite3Isdigit(x)
182366# define safe_isalnum(x) sqlite3Isalnum(x)
182367# define safe_isxdigit(x) sqlite3Isxdigit(x)
182368#else
182369 /* Use the standard library for separate compilation */
182370#include <ctype.h> /* amalgamator: keep */
182371# define safe_isdigit(x) isdigit((unsigned char)(x))
182372# define safe_isalnum(x) isalnum((unsigned char)(x))
182373# define safe_isxdigit(x) isxdigit((unsigned char)(x))
182374#endif
182375
182376/*
182377** Growing our own isspace() routine this way is twice as fast as
182378** the library isspace() function.
182379*/
182380static const char geopolyIsSpace[] = {
182381 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
182382 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182383 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182384 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182385 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182386 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182387 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182388 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182389 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182390 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182391 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182392 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182393 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182394 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182395 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182396 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182397};
182398#define safe_isspace(x) (geopolyIsSpace[(unsigned char)x])
182399#endif /* JSON NULL - back to original code */
182400
182401/* Compiler and version */
182402#ifndef GCC_VERSION
182403#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
182404# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
182405#else
182406# define GCC_VERSION 0
182407#endif
182408#endif
182409#ifndef MSVC_VERSION
182410#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
182411# define MSVC_VERSION _MSC_VER
182412#else
182413# define MSVC_VERSION 0
182414#endif
182415#endif
182416
182417/* Datatype for coordinates
182418*/
182419typedef float GeoCoord;
182420
182421/*
182422** Internal representation of a polygon.
182423**
182424** The polygon consists of a sequence of vertexes. There is a line
182425** segment between each pair of vertexes, and one final segment from
182426** the last vertex back to the first. (This differs from the GeoJSON
182427** standard in which the final vertex is a repeat of the first.)
182428**
182429** The polygon follows the right-hand rule. The area to the right of
182430** each segment is "outside" and the area to the left is "inside".
182431**
182432** The on-disk representation consists of a 4-byte header followed by
182433** the values. The 4-byte header is:
182434**
182435** encoding (1 byte) 0=big-endian, 1=little-endian
182436** nvertex (3 bytes) Number of vertexes as a big-endian integer
182437*/
182438typedef struct GeoPoly GeoPoly;
182439struct GeoPoly {
182440 int nVertex; /* Number of vertexes */
182441 unsigned char hdr[4]; /* Header for on-disk representation */
182442 GeoCoord a[2]; /* 2*nVertex values. X (longitude) first, then Y */
182443};
182444
182445/*
182446** State of a parse of a GeoJSON input.
182447*/
182448typedef struct GeoParse GeoParse;
182449struct GeoParse {
182450 const unsigned char *z; /* Unparsed input */
182451 int nVertex; /* Number of vertexes in a[] */
182452 int nAlloc; /* Space allocated to a[] */
182453 int nErr; /* Number of errors encountered */
182454 GeoCoord *a; /* Array of vertexes. From sqlite3_malloc64() */
182455};
182456
182457/* Do a 4-byte byte swap */
182458static void geopolySwab32(unsigned char *a){
182459 unsigned char t = a[0];
182460 a[0] = a[3];
182461 a[3] = t;
182462 t = a[1];
182463 a[1] = a[2];
182464 a[2] = t;
182465}
182466
182467/* Skip whitespace. Return the next non-whitespace character. */
182468static char geopolySkipSpace(GeoParse *p){
182469 while( p->z[0] && safe_isspace(p->z[0]) ) p->z++;
182470 return p->z[0];
182471}
182472
182473/* Parse out a number. Write the value into *pVal if pVal!=0.
182474** return non-zero on success and zero if the next token is not a number.
182475*/
182476static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
182477 char c = geopolySkipSpace(p);
182478 const unsigned char *z = p->z;
182479 int j = 0;
182480 int seenDP = 0;
182481 int seenE = 0;
182482 if( c=='-' ){
182483 j = 1;
182484 c = z[j];
182485 }
182486 if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
182487 for(;; j++){
182488 c = z[j];
182489 if( c>='0' && c<='9' ) continue;
182490 if( c=='.' ){
182491 if( z[j-1]=='-' ) return 0;
182492 if( seenDP ) return 0;
182493 seenDP = 1;
182494 continue;
182495 }
182496 if( c=='e' || c=='E' ){
182497 if( z[j-1]<'0' ) return 0;
182498 if( seenE ) return -1;
182499 seenDP = seenE = 1;
182500 c = z[j+1];
182501 if( c=='+' || c=='-' ){
182502 j++;
182503 c = z[j+1];
182504 }
182505 if( c<'0' || c>'9' ) return 0;
182506 continue;
182507 }
182508 break;
182509 }
182510 if( z[j-1]<'0' ) return 0;
182511 if( pVal ) *pVal = (GeoCoord)atof((const char*)p->z);
182512 p->z += j;
182513 return 1;
182514}
182515
182516/*
182517** If the input is a well-formed JSON array of coordinates with at least
182518** four coordinates and where each coordinate is itself a two-value array,
182519** then convert the JSON into a GeoPoly object and return a pointer to
182520** that object.
182521**
182522** If any error occurs, return NULL.
182523*/
182524static GeoPoly *geopolyParseJson(const unsigned char *z, int *pRc){
182525 GeoParse s;
182526 int rc = SQLITE_OK;
182527 memset(&s, 0, sizeof(s));
182528 s.z = z;
182529 if( geopolySkipSpace(&s)=='[' ){
182530 s.z++;
182531 while( geopolySkipSpace(&s)=='[' ){
182532 int ii = 0;
182533 char c;
182534 s.z++;
182535 if( s.nVertex>=s.nAlloc ){
182536 GeoCoord *aNew;
182537 s.nAlloc = s.nAlloc*2 + 16;
182538 aNew = sqlite3_realloc64(s.a, s.nAlloc*sizeof(GeoCoord)*2 );
182539 if( aNew==0 ){
182540 rc = SQLITE_NOMEM;
182541 s.nErr++;
182542 break;
182543 }
182544 s.a = aNew;
182545 }
182546 while( geopolyParseNumber(&s, ii<=1 ? &s.a[s.nVertex*2+ii] : 0) ){
182547 ii++;
182548 if( ii==2 ) s.nVertex++;
182549 c = geopolySkipSpace(&s);
182550 s.z++;
182551 if( c==',' ) continue;
182552 if( c==']' && ii>=2 ) break;
182553 s.nErr++;
182554 rc = SQLITE_ERROR;
182555 goto parse_json_err;
182556 }
182557 if( geopolySkipSpace(&s)==',' ){
182558 s.z++;
182559 continue;
182560 }
182561 break;
182562 }
182563 if( geopolySkipSpace(&s)==']'
182564 && s.nVertex>=4
182565 && s.a[0]==s.a[s.nVertex*2-2]
182566 && s.a[1]==s.a[s.nVertex*2-1]
182567 && (s.z++, geopolySkipSpace(&s)==0)
182568 ){
182569 int nByte;
182570 GeoPoly *pOut;
182571 int x = 1;
182572 s.nVertex--; /* Remove the redundant vertex at the end */
182573 nByte = sizeof(GeoPoly) * s.nVertex*2*sizeof(GeoCoord);
182574 pOut = sqlite3_malloc64( nByte );
182575 x = 1;
182576 if( pOut==0 ) goto parse_json_err;
182577 pOut->nVertex = s.nVertex;
182578 memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord));
182579 pOut->hdr[0] = *(unsigned char*)&x;
182580 pOut->hdr[1] = (s.nVertex>>16)&0xff;
182581 pOut->hdr[2] = (s.nVertex>>8)&0xff;
182582 pOut->hdr[3] = s.nVertex&0xff;
182583 sqlite3_free(s.a);
182584 if( pRc ) *pRc = SQLITE_OK;
182585 return pOut;
182586 }else{
182587 s.nErr++;
182588 rc = SQLITE_ERROR;
182589 }
182590 }
182591parse_json_err:
182592 if( pRc ) *pRc = rc;
182593 sqlite3_free(s.a);
182594 return 0;
182595}
182596
182597/*
182598** Given a function parameter, try to interpret it as a polygon, either
182599** in the binary format or JSON text. Compute a GeoPoly object and
182600** return a pointer to that object. Or if the input is not a well-formed
182601** polygon, put an error message in sqlite3_context and return NULL.
182602*/
182603static GeoPoly *geopolyFuncParam(
182604 sqlite3_context *pCtx, /* Context for error messages */
182605 sqlite3_value *pVal, /* The value to decode */
182606 int *pRc /* Write error here */
182607){
182608 GeoPoly *p = 0;
182609 int nByte;
182610 if( sqlite3_value_type(pVal)==SQLITE_BLOB
182611 && (nByte = sqlite3_value_bytes(pVal))>=(4+6*sizeof(GeoCoord))
182612 ){
182613 const unsigned char *a = sqlite3_value_blob(pVal);
182614 int nVertex;
182615 nVertex = (a[1]<<16) + (a[2]<<8) + a[3];
182616 if( (a[0]==0 || a[0]==1)
182617 && (nVertex*2*sizeof(GeoCoord) + 4)==(unsigned int)nByte
182618 ){
182619 p = sqlite3_malloc64( sizeof(*p) + (nVertex-1)*2*sizeof(GeoCoord) );
182620 if( p==0 ){
182621 if( pRc ) *pRc = SQLITE_NOMEM;
182622 if( pCtx ) sqlite3_result_error_nomem(pCtx);
182623 }else{
182624 int x = 1;
182625 p->nVertex = nVertex;
182626 memcpy(p->hdr, a, nByte);
182627 if( a[0] != *(unsigned char*)&x ){
182628 int ii;
182629 for(ii=0; ii<nVertex*2; ii++){
182630 geopolySwab32((unsigned char*)&p->a[ii]);
182631 }
182632 p->hdr[0] ^= 1;
182633 }
182634 }
182635 }
182636 if( pRc ) *pRc = SQLITE_OK;
182637 return p;
182638 }else if( sqlite3_value_type(pVal)==SQLITE_TEXT ){
182639 const unsigned char *zJson = sqlite3_value_text(pVal);
182640 if( zJson==0 ){
182641 if( pRc ) *pRc = SQLITE_NOMEM;
182642 return 0;
182643 }
182644 return geopolyParseJson(zJson, pRc);
182645 }else{
182646 if( pRc ) *pRc = SQLITE_ERROR;
182647 return 0;
182648 }
182649}
182650
182651/*
182652** Implementation of the geopoly_blob(X) function.
182653**
182654** If the input is a well-formed Geopoly BLOB or JSON string
182655** then return the BLOB representation of the polygon. Otherwise
182656** return NULL.
182657*/
182658static void geopolyBlobFunc(
182659 sqlite3_context *context,
182660 int argc,
182661 sqlite3_value **argv
182662){
182663 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
182664 if( p ){
182665 sqlite3_result_blob(context, p->hdr,
182666 4+8*p->nVertex, SQLITE_TRANSIENT);
182667 sqlite3_free(p);
182668 }
182669}
182670
182671/*
182672** SQL function: geopoly_json(X)
182673**
182674** Interpret X as a polygon and render it as a JSON array
182675** of coordinates. Or, if X is not a valid polygon, return NULL.
182676*/
182677static void geopolyJsonFunc(
182678 sqlite3_context *context,
182679 int argc,
182680 sqlite3_value **argv
182681){
182682 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
182683 if( p ){
182684 sqlite3 *db = sqlite3_context_db_handle(context);
182685 sqlite3_str *x = sqlite3_str_new(db);
182686 int i;
182687 sqlite3_str_append(x, "[", 1);
182688 for(i=0; i<p->nVertex; i++){
182689 sqlite3_str_appendf(x, "[%!g,%!g],", p->a[i*2], p->a[i*2+1]);
182690 }
182691 sqlite3_str_appendf(x, "[%!g,%!g]]", p->a[0], p->a[1]);
182692 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
182693 sqlite3_free(p);
182694 }
182695}
182696
182697/*
182698** SQL function: geopoly_svg(X, ....)
182699**
182700** Interpret X as a polygon and render it as a SVG <polyline>.
182701** Additional arguments are added as attributes to the <polyline>.
182702*/
182703static void geopolySvgFunc(
182704 sqlite3_context *context,
182705 int argc,
182706 sqlite3_value **argv
182707){
182708 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
182709 if( p ){
182710 sqlite3 *db = sqlite3_context_db_handle(context);
182711 sqlite3_str *x = sqlite3_str_new(db);
182712 int i;
182713 char cSep = '\'';
182714 sqlite3_str_appendf(x, "<polyline points=");
182715 for(i=0; i<p->nVertex; i++){
182716 sqlite3_str_appendf(x, "%c%g,%g", cSep, p->a[i*2], p->a[i*2+1]);
182717 cSep = ' ';
182718 }
182719 sqlite3_str_appendf(x, " %g,%g'", p->a[0], p->a[1]);
182720 for(i=1; i<argc; i++){
182721 const char *z = (const char*)sqlite3_value_text(argv[i]);
182722 if( z && z[0] ){
182723 sqlite3_str_appendf(x, " %s", z);
182724 }
182725 }
182726 sqlite3_str_appendf(x, "></polyline>");
182727 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
182728 sqlite3_free(p);
182729 }
182730}
182731
182732/*
182733** SQL Function: geopoly_xform(poly, A, B, C, D, E, F)
182734**
182735** Transform and/or translate a polygon as follows:
182736**
182737** x1 = A*x0 + B*y0 + E
182738** y1 = C*x0 + D*y0 + F
182739**
182740** For a translation:
182741**
182742** geopoly_xform(poly, 1, 0, 0, 1, x-offset, y-offset)
182743**
182744** Rotate by R around the point (0,0):
182745**
182746** geopoly_xform(poly, cos(R), sin(R), -sin(R), cos(R), 0, 0)
182747*/
182748static void geopolyXformFunc(
182749 sqlite3_context *context,
182750 int argc,
182751 sqlite3_value **argv
182752){
182753 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
182754 double A = sqlite3_value_double(argv[1]);
182755 double B = sqlite3_value_double(argv[2]);
182756 double C = sqlite3_value_double(argv[3]);
182757 double D = sqlite3_value_double(argv[4]);
182758 double E = sqlite3_value_double(argv[5]);
182759 double F = sqlite3_value_double(argv[6]);
182760 GeoCoord x1, y1, x0, y0;
182761 int ii;
182762 if( p ){
182763 for(ii=0; ii<p->nVertex; ii++){
182764 x0 = p->a[ii*2];
182765 y0 = p->a[ii*2+1];
182766 x1 = (GeoCoord)(A*x0 + B*y0 + E);
182767 y1 = (GeoCoord)(C*x0 + D*y0 + F);
182768 p->a[ii*2] = x1;
182769 p->a[ii*2+1] = y1;
182770 }
182771 sqlite3_result_blob(context, p->hdr,
182772 4+8*p->nVertex, SQLITE_TRANSIENT);
182773 sqlite3_free(p);
182774 }
182775}
182776
182777/*
182778** Implementation of the geopoly_area(X) function.
182779**
182780** If the input is a well-formed Geopoly BLOB then return the area
182781** enclosed by the polygon. If the polygon circulates clockwise instead
182782** of counterclockwise (as it should) then return the negative of the
182783** enclosed area. Otherwise return NULL.
182784*/
182785static void geopolyAreaFunc(
182786 sqlite3_context *context,
182787 int argc,
182788 sqlite3_value **argv
182789){
182790 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
182791 if( p ){
182792 double rArea = 0.0;
182793 int ii;
182794 for(ii=0; ii<p->nVertex-1; ii++){
182795 rArea += (p->a[ii*2] - p->a[ii*2+2]) /* (x0 - x1) */
182796 * (p->a[ii*2+1] + p->a[ii*2+3]) /* (y0 + y1) */
182797 * 0.5;
182798 }
182799 rArea += (p->a[ii*2] - p->a[0]) /* (xN - x0) */
182800 * (p->a[ii*2+1] + p->a[1]) /* (yN + y0) */
182801 * 0.5;
182802 sqlite3_result_double(context, rArea);
182803 sqlite3_free(p);
182804 }
182805}
182806
182807/*
182808** If pPoly is a polygon, compute its bounding box. Then:
182809**
182810** (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
182811** (2) otherwise, compute a GeoPoly for the bounding box and return the
182812** new GeoPoly
182813**
182814** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from
182815** the bounding box in aCoord and return a pointer to that GeoPoly.
182816*/
182817static GeoPoly *geopolyBBox(
182818 sqlite3_context *context, /* For recording the error */
182819 sqlite3_value *pPoly, /* The polygon */
182820 RtreeCoord *aCoord, /* Results here */
182821 int *pRc /* Error code here */
182822){
182823 GeoPoly *pOut = 0;
182824 GeoPoly *p;
182825 float mnX, mxX, mnY, mxY;
182826 if( pPoly==0 && aCoord!=0 ){
182827 p = 0;
182828 mnX = aCoord[0].f;
182829 mxX = aCoord[1].f;
182830 mnY = aCoord[2].f;
182831 mxY = aCoord[3].f;
182832 goto geopolyBboxFill;
182833 }else{
182834 p = geopolyFuncParam(context, pPoly, pRc);
182835 }
182836 if( p ){
182837 int ii;
182838 mnX = mxX = p->a[0];
182839 mnY = mxY = p->a[1];
182840 for(ii=1; ii<p->nVertex; ii++){
182841 double r = p->a[ii*2];
182842 if( r<mnX ) mnX = (float)r;
182843 else if( r>mxX ) mxX = (float)r;
182844 r = p->a[ii*2+1];
182845 if( r<mnY ) mnY = (float)r;
182846 else if( r>mxY ) mxY = (float)r;
182847 }
182848 if( pRc ) *pRc = SQLITE_OK;
182849 if( aCoord==0 ){
182850 geopolyBboxFill:
182851 pOut = sqlite3_realloc(p, sizeof(GeoPoly)+sizeof(GeoCoord)*6);
182852 if( pOut==0 ){
182853 sqlite3_free(p);
182854 if( context ) sqlite3_result_error_nomem(context);
182855 if( pRc ) *pRc = SQLITE_NOMEM;
182856 return 0;
182857 }
182858 pOut->nVertex = 4;
182859 ii = 1;
182860 pOut->hdr[0] = *(unsigned char*)&ii;
182861 pOut->hdr[1] = 0;
182862 pOut->hdr[2] = 0;
182863 pOut->hdr[3] = 4;
182864 pOut->a[0] = mnX;
182865 pOut->a[1] = mnY;
182866 pOut->a[2] = mxX;
182867 pOut->a[3] = mnY;
182868 pOut->a[4] = mxX;
182869 pOut->a[5] = mxY;
182870 pOut->a[6] = mnX;
182871 pOut->a[7] = mxY;
182872 }else{
182873 sqlite3_free(p);
182874 aCoord[0].f = mnX;
182875 aCoord[1].f = mxX;
182876 aCoord[2].f = mnY;
182877 aCoord[3].f = mxY;
182878 }
182879 }
182880 return pOut;
182881}
182882
182883/*
182884** Implementation of the geopoly_bbox(X) SQL function.
182885*/
182886static void geopolyBBoxFunc(
182887 sqlite3_context *context,
182888 int argc,
182889 sqlite3_value **argv
182890){
182891 GeoPoly *p = geopolyBBox(context, argv[0], 0, 0);
182892 if( p ){
182893 sqlite3_result_blob(context, p->hdr,
182894 4+8*p->nVertex, SQLITE_TRANSIENT);
182895 sqlite3_free(p);
182896 }
182897}
182898
182899/*
182900** State vector for the geopoly_group_bbox() aggregate function.
182901*/
182902typedef struct GeoBBox GeoBBox;
182903struct GeoBBox {
182904 int isInit;
182905 RtreeCoord a[4];
182906};
182907
182908
182909/*
182910** Implementation of the geopoly_group_bbox(X) aggregate SQL function.
182911*/
182912static void geopolyBBoxStep(
182913 sqlite3_context *context,
182914 int argc,
182915 sqlite3_value **argv
182916){
182917 RtreeCoord a[4];
182918 int rc = SQLITE_OK;
182919 (void)geopolyBBox(context, argv[0], a, &rc);
182920 if( rc==SQLITE_OK ){
182921 GeoBBox *pBBox;
182922 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, sizeof(*pBBox));
182923 if( pBBox==0 ) return;
182924 if( pBBox->isInit==0 ){
182925 pBBox->isInit = 1;
182926 memcpy(pBBox->a, a, sizeof(RtreeCoord)*4);
182927 }else{
182928 if( a[0].f < pBBox->a[0].f ) pBBox->a[0] = a[0];
182929 if( a[1].f > pBBox->a[1].f ) pBBox->a[1] = a[1];
182930 if( a[2].f < pBBox->a[2].f ) pBBox->a[2] = a[2];
182931 if( a[3].f > pBBox->a[3].f ) pBBox->a[3] = a[3];
182932 }
182933 }
182934}
182935static void geopolyBBoxFinal(
182936 sqlite3_context *context
182937){
182938 GeoPoly *p;
182939 GeoBBox *pBBox;
182940 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, 0);
182941 if( pBBox==0 ) return;
182942 p = geopolyBBox(context, 0, pBBox->a, 0);
182943 if( p ){
182944 sqlite3_result_blob(context, p->hdr,
182945 4+8*p->nVertex, SQLITE_TRANSIENT);
182946 sqlite3_free(p);
182947 }
182948}
182949
182950
182951/*
182952** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2).
182953** Returns:
182954**
182955** +2 x0,y0 is on the line segement
182956**
182957** +1 x0,y0 is beneath line segment
182958**
182959** 0 x0,y0 is not on or beneath the line segment or the line segment
182960** is vertical and x0,y0 is not on the line segment
182961**
182962** The left-most coordinate min(x1,x2) is not considered to be part of
182963** the line segment for the purposes of this analysis.
182964*/
182965static int pointBeneathLine(
182966 double x0, double y0,
182967 double x1, double y1,
182968 double x2, double y2
182969){
182970 double y;
182971 if( x0==x1 && y0==y1 ) return 2;
182972 if( x1<x2 ){
182973 if( x0<=x1 || x0>x2 ) return 0;
182974 }else if( x1>x2 ){
182975 if( x0<=x2 || x0>x1 ) return 0;
182976 }else{
182977 /* Vertical line segment */
182978 if( x0!=x1 ) return 0;
182979 if( y0<y1 && y0<y2 ) return 0;
182980 if( y0>y1 && y0>y2 ) return 0;
182981 return 2;
182982 }
182983 y = y1 + (y2-y1)*(x0-x1)/(x2-x1);
182984 if( y0==y ) return 2;
182985 if( y0<y ) return 1;
182986 return 0;
182987}
182988
182989/*
182990** SQL function: geopoly_contains_point(P,X,Y)
182991**
182992** Return +2 if point X,Y is within polygon P.
182993** Return +1 if point X,Y is on the polygon boundary.
182994** Return 0 if point X,Y is outside the polygon
182995*/
182996static void geopolyContainsPointFunc(
182997 sqlite3_context *context,
182998 int argc,
182999 sqlite3_value **argv
183000){
183001 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
183002 double x0 = sqlite3_value_double(argv[1]);
183003 double y0 = sqlite3_value_double(argv[2]);
183004 int v = 0;
183005 int cnt = 0;
183006 int ii;
183007 if( p1==0 ) return;
183008 for(ii=0; ii<p1->nVertex-1; ii++){
183009 v = pointBeneathLine(x0,y0,p1->a[ii*2],p1->a[ii*2+1],
183010 p1->a[ii*2+2],p1->a[ii*2+3]);
183011 if( v==2 ) break;
183012 cnt += v;
183013 }
183014 if( v!=2 ){
183015 v = pointBeneathLine(x0,y0,p1->a[ii*2],p1->a[ii*2+1],
183016 p1->a[0],p1->a[1]);
183017 }
183018 if( v==2 ){
183019 sqlite3_result_int(context, 1);
183020 }else if( ((v+cnt)&1)==0 ){
183021 sqlite3_result_int(context, 0);
183022 }else{
183023 sqlite3_result_int(context, 2);
183024 }
183025 sqlite3_free(p1);
183026}
183027
183028/* Forward declaration */
183029static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2);
183030
183031/*
183032** SQL function: geopoly_within(P1,P2)
183033**
183034** Return +2 if P1 and P2 are the same polygon
183035** Return +1 if P2 is contained within P1
183036** Return 0 if any part of P2 is on the outside of P1
183037**
183038*/
183039static void geopolyWithinFunc(
183040 sqlite3_context *context,
183041 int argc,
183042 sqlite3_value **argv
183043){
183044 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
183045 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
183046 if( p1 && p2 ){
183047 int x = geopolyOverlap(p1, p2);
183048 if( x<0 ){
183049 sqlite3_result_error_nomem(context);
183050 }else{
183051 sqlite3_result_int(context, x==2 ? 1 : x==4 ? 2 : 0);
183052 }
183053 }
183054 sqlite3_free(p1);
183055 sqlite3_free(p2);
183056}
183057
183058/* Objects used by the overlap algorihm. */
183059typedef struct GeoEvent GeoEvent;
183060typedef struct GeoSegment GeoSegment;
183061typedef struct GeoOverlap GeoOverlap;
183062struct GeoEvent {
183063 double x; /* X coordinate at which event occurs */
183064 int eType; /* 0 for ADD, 1 for REMOVE */
183065 GeoSegment *pSeg; /* The segment to be added or removed */
183066 GeoEvent *pNext; /* Next event in the sorted list */
183067};
183068struct GeoSegment {
183069 double C, B; /* y = C*x + B */
183070 double y; /* Current y value */
183071 float y0; /* Initial y value */
183072 unsigned char side; /* 1 for p1, 2 for p2 */
183073 unsigned int idx; /* Which segment within the side */
183074 GeoSegment *pNext; /* Next segment in a list sorted by y */
183075};
183076struct GeoOverlap {
183077 GeoEvent *aEvent; /* Array of all events */
183078 GeoSegment *aSegment; /* Array of all segments */
183079 int nEvent; /* Number of events */
183080 int nSegment; /* Number of segments */
183081};
183082
183083/*
183084** Add a single segment and its associated events.
183085*/
183086static void geopolyAddOneSegment(
183087 GeoOverlap *p,
183088 GeoCoord x0,
183089 GeoCoord y0,
183090 GeoCoord x1,
183091 GeoCoord y1,
183092 unsigned char side,
183093 unsigned int idx
183094){
183095 GeoSegment *pSeg;
183096 GeoEvent *pEvent;
183097 if( x0==x1 ) return; /* Ignore vertical segments */
183098 if( x0>x1 ){
183099 GeoCoord t = x0;
183100 x0 = x1;
183101 x1 = t;
183102 t = y0;
183103 y0 = y1;
183104 y1 = t;
183105 }
183106 pSeg = p->aSegment + p->nSegment;
183107 p->nSegment++;
183108 pSeg->C = (y1-y0)/(x1-x0);
183109 pSeg->B = y1 - x1*pSeg->C;
183110 pSeg->y0 = y0;
183111 pSeg->side = side;
183112 pSeg->idx = idx;
183113 pEvent = p->aEvent + p->nEvent;
183114 p->nEvent++;
183115 pEvent->x = x0;
183116 pEvent->eType = 0;
183117 pEvent->pSeg = pSeg;
183118 pEvent = p->aEvent + p->nEvent;
183119 p->nEvent++;
183120 pEvent->x = x1;
183121 pEvent->eType = 1;
183122 pEvent->pSeg = pSeg;
183123}
183124
183125
183126
183127/*
183128** Insert all segments and events for polygon pPoly.
183129*/
183130static void geopolyAddSegments(
183131 GeoOverlap *p, /* Add segments to this Overlap object */
183132 GeoPoly *pPoly, /* Take all segments from this polygon */
183133 unsigned char side /* The side of pPoly */
183134){
183135 unsigned int i;
183136 GeoCoord *x;
183137 for(i=0; i<(unsigned)pPoly->nVertex-1; i++){
183138 x = pPoly->a + (i*2);
183139 geopolyAddOneSegment(p, x[0], x[1], x[2], x[3], side, i);
183140 }
183141 x = pPoly->a + (i*2);
183142 geopolyAddOneSegment(p, x[0], x[1], pPoly->a[0], pPoly->a[1], side, i);
183143}
183144
183145/*
183146** Merge two lists of sorted events by X coordinate
183147*/
183148static GeoEvent *geopolyEventMerge(GeoEvent *pLeft, GeoEvent *pRight){
183149 GeoEvent head, *pLast;
183150 head.pNext = 0;
183151 pLast = &head;
183152 while( pRight && pLeft ){
183153 if( pRight->x <= pLeft->x ){
183154 pLast->pNext = pRight;
183155 pLast = pRight;
183156 pRight = pRight->pNext;
183157 }else{
183158 pLast->pNext = pLeft;
183159 pLast = pLeft;
183160 pLeft = pLeft->pNext;
183161 }
183162 }
183163 pLast->pNext = pRight ? pRight : pLeft;
183164 return head.pNext;
183165}
183166
183167/*
183168** Sort an array of nEvent event objects into a list.
183169*/
183170static GeoEvent *geopolySortEventsByX(GeoEvent *aEvent, int nEvent){
183171 int mx = 0;
183172 int i, j;
183173 GeoEvent *p;
183174 GeoEvent *a[50];
183175 for(i=0; i<nEvent; i++){
183176 p = &aEvent[i];
183177 p->pNext = 0;
183178 for(j=0; j<mx && a[j]; j++){
183179 p = geopolyEventMerge(a[j], p);
183180 a[j] = 0;
183181 }
183182 a[j] = p;
183183 if( j>=mx ) mx = j+1;
183184 }
183185 p = 0;
183186 for(i=0; i<mx; i++){
183187 p = geopolyEventMerge(a[i], p);
183188 }
183189 return p;
183190}
183191
183192/*
183193** Merge two lists of sorted segments by Y, and then by C.
183194*/
183195static GeoSegment *geopolySegmentMerge(GeoSegment *pLeft, GeoSegment *pRight){
183196 GeoSegment head, *pLast;
183197 head.pNext = 0;
183198 pLast = &head;
183199 while( pRight && pLeft ){
183200 double r = pRight->y - pLeft->y;
183201 if( r==0.0 ) r = pRight->C - pLeft->C;
183202 if( r<0.0 ){
183203 pLast->pNext = pRight;
183204 pLast = pRight;
183205 pRight = pRight->pNext;
183206 }else{
183207 pLast->pNext = pLeft;
183208 pLast = pLeft;
183209 pLeft = pLeft->pNext;
183210 }
183211 }
183212 pLast->pNext = pRight ? pRight : pLeft;
183213 return head.pNext;
183214}
183215
183216/*
183217** Sort a list of GeoSegments in order of increasing Y and in the event of
183218** a tie, increasing C (slope).
183219*/
183220static GeoSegment *geopolySortSegmentsByYAndC(GeoSegment *pList){
183221 int mx = 0;
183222 int i;
183223 GeoSegment *p;
183224 GeoSegment *a[50];
183225 while( pList ){
183226 p = pList;
183227 pList = pList->pNext;
183228 p->pNext = 0;
183229 for(i=0; i<mx && a[i]; i++){
183230 p = geopolySegmentMerge(a[i], p);
183231 a[i] = 0;
183232 }
183233 a[i] = p;
183234 if( i>=mx ) mx = i+1;
183235 }
183236 p = 0;
183237 for(i=0; i<mx; i++){
183238 p = geopolySegmentMerge(a[i], p);
183239 }
183240 return p;
183241}
183242
183243/*
183244** Determine the overlap between two polygons
183245*/
183246static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2){
183247 int nVertex = p1->nVertex + p2->nVertex + 2;
183248 GeoOverlap *p;
183249 int nByte;
183250 GeoEvent *pThisEvent;
183251 double rX;
183252 int rc = 0;
183253 int needSort = 0;
183254 GeoSegment *pActive = 0;
183255 GeoSegment *pSeg;
183256 unsigned char aOverlap[4];
183257
183258 nByte = sizeof(GeoEvent)*nVertex*2
183259 + sizeof(GeoSegment)*nVertex
183260 + sizeof(GeoOverlap);
183261 p = sqlite3_malloc( nByte );
183262 if( p==0 ) return -1;
183263 p->aEvent = (GeoEvent*)&p[1];
183264 p->aSegment = (GeoSegment*)&p->aEvent[nVertex*2];
183265 p->nEvent = p->nSegment = 0;
183266 geopolyAddSegments(p, p1, 1);
183267 geopolyAddSegments(p, p2, 2);
183268 pThisEvent = geopolySortEventsByX(p->aEvent, p->nEvent);
183269 rX = pThisEvent->x==0.0 ? -1.0 : 0.0;
183270 memset(aOverlap, 0, sizeof(aOverlap));
183271 while( pThisEvent ){
183272 if( pThisEvent->x!=rX ){
183273 GeoSegment *pPrev = 0;
183274 int iMask = 0;
183275 GEODEBUG(("Distinct X: %g\n", pThisEvent->x));
183276 rX = pThisEvent->x;
183277 if( needSort ){
183278 GEODEBUG(("SORT\n"));
183279 pActive = geopolySortSegmentsByYAndC(pActive);
183280 needSort = 0;
183281 }
183282 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
183283 if( pPrev ){
183284 if( pPrev->y!=pSeg->y ){
183285 GEODEBUG(("MASK: %d\n", iMask));
183286 aOverlap[iMask] = 1;
183287 }
183288 }
183289 iMask ^= pSeg->side;
183290 pPrev = pSeg;
183291 }
183292 pPrev = 0;
183293 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
183294 double y = pSeg->C*rX + pSeg->B;
183295 GEODEBUG(("Segment %d.%d %g->%g\n", pSeg->side, pSeg->idx, pSeg->y, y));
183296 pSeg->y = y;
183297 if( pPrev ){
183298 if( pPrev->y>pSeg->y && pPrev->side!=pSeg->side ){
183299 rc = 1;
183300 GEODEBUG(("Crossing: %d.%d and %d.%d\n",
183301 pPrev->side, pPrev->idx,
183302 pSeg->side, pSeg->idx));
183303 goto geopolyOverlapDone;
183304 }else if( pPrev->y!=pSeg->y ){
183305 GEODEBUG(("MASK: %d\n", iMask));
183306 aOverlap[iMask] = 1;
183307 }
183308 }
183309 iMask ^= pSeg->side;
183310 pPrev = pSeg;
183311 }
183312 }
183313 GEODEBUG(("%s %d.%d C=%g B=%g\n",
183314 pThisEvent->eType ? "RM " : "ADD",
183315 pThisEvent->pSeg->side, pThisEvent->pSeg->idx,
183316 pThisEvent->pSeg->C,
183317 pThisEvent->pSeg->B));
183318 if( pThisEvent->eType==0 ){
183319 /* Add a segment */
183320 pSeg = pThisEvent->pSeg;
183321 pSeg->y = pSeg->y0;
183322 pSeg->pNext = pActive;
183323 pActive = pSeg;
183324 needSort = 1;
183325 }else{
183326 /* Remove a segment */
183327 if( pActive==pThisEvent->pSeg ){
183328 pActive = pActive->pNext;
183329 }else{
183330 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
183331 if( pSeg->pNext==pThisEvent->pSeg ){
183332 pSeg->pNext = pSeg->pNext->pNext;
183333 break;
183334 }
183335 }
183336 }
183337 }
183338 pThisEvent = pThisEvent->pNext;
183339 }
183340 if( aOverlap[3]==0 ){
183341 rc = 0;
183342 }else if( aOverlap[1]!=0 && aOverlap[2]==0 ){
183343 rc = 3;
183344 }else if( aOverlap[1]==0 && aOverlap[2]!=0 ){
183345 rc = 2;
183346 }else if( aOverlap[1]==0 && aOverlap[2]==0 ){
183347 rc = 4;
183348 }else{
183349 rc = 1;
183350 }
183351
183352geopolyOverlapDone:
183353 sqlite3_free(p);
183354 return rc;
183355}
183356
183357/*
183358** SQL function: geopoly_overlap(P1,P2)
183359**
183360** Determine whether or not P1 and P2 overlap. Return value:
183361**
183362** 0 The two polygons are disjoint
183363** 1 They overlap
183364** 2 P1 is completely contained within P2
183365** 3 P2 is completely contained within P1
183366** 4 P1 and P2 are the same polygon
183367** NULL Either P1 or P2 or both are not valid polygons
183368*/
183369static void geopolyOverlapFunc(
183370 sqlite3_context *context,
183371 int argc,
183372 sqlite3_value **argv
183373){
183374 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
183375 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
183376 if( p1 && p2 ){
183377 int x = geopolyOverlap(p1, p2);
183378 if( x<0 ){
183379 sqlite3_result_error_nomem(context);
183380 }else{
183381 sqlite3_result_int(context, x);
183382 }
183383 }
183384 sqlite3_free(p1);
183385 sqlite3_free(p2);
183386}
183387
183388/*
183389** Enable or disable debugging output
183390*/
183391static void geopolyDebugFunc(
183392 sqlite3_context *context,
183393 int argc,
183394 sqlite3_value **argv
183395){
183396#ifdef GEOPOLY_ENABLE_DEBUG
183397 geo_debug = sqlite3_value_int(argv[0]);
183398#endif
183399}
183400
183401/*
183402** This function is the implementation of both the xConnect and xCreate
183403** methods of the geopoly virtual table.
183404**
183405** argv[0] -> module name
183406** argv[1] -> database name
183407** argv[2] -> table name
183408** argv[...] -> column names...
183409*/
183410static int geopolyInit(
183411 sqlite3 *db, /* Database connection */
183412 void *pAux, /* One of the RTREE_COORD_* constants */
183413 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
183414 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
183415 char **pzErr, /* OUT: Error message, if any */
183416 int isCreate /* True for xCreate, false for xConnect */
183417){
183418 int rc = SQLITE_OK;
183419 Rtree *pRtree;
183420 int nDb; /* Length of string argv[1] */
183421 int nName; /* Length of string argv[2] */
183422 sqlite3_str *pSql;
183423 char *zSql;
183424 int ii;
183425
183426 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
183427
183428 /* Allocate the sqlite3_vtab structure */
183429 nDb = (int)strlen(argv[1]);
183430 nName = (int)strlen(argv[2]);
183431 pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
183432 if( !pRtree ){
183433 return SQLITE_NOMEM;
183434 }
183435 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
183436 pRtree->nBusy = 1;
183437 pRtree->base.pModule = &rtreeModule;
183438 pRtree->zDb = (char *)&pRtree[1];
183439 pRtree->zName = &pRtree->zDb[nDb+1];
183440 pRtree->eCoordType = RTREE_COORD_REAL32;
183441 pRtree->nDim = 2;
183442 pRtree->nDim2 = 4;
183443 memcpy(pRtree->zDb, argv[1], nDb);
183444 memcpy(pRtree->zName, argv[2], nName);
183445
183446
183447 /* Create/Connect to the underlying relational database schema. If
183448 ** that is successful, call sqlite3_declare_vtab() to configure
183449 ** the r-tree table schema.
183450 */
183451 pSql = sqlite3_str_new(db);
183452 sqlite3_str_appendf(pSql, "CREATE TABLE x(_shape");
183453 pRtree->nAux = 1; /* Add one for _shape */
183454 pRtree->nAuxNotNull = 1; /* The _shape column is always not-null */
183455 for(ii=3; ii<argc; ii++){
183456 pRtree->nAux++;
183457 sqlite3_str_appendf(pSql, ",%s", argv[ii]);
183458 }
183459 sqlite3_str_appendf(pSql, ");");
183460 zSql = sqlite3_str_finish(pSql);
183461 if( !zSql ){
183462 rc = SQLITE_NOMEM;
183463 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
183464 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
183465 }
183466 sqlite3_free(zSql);
183467 if( rc ) goto geopolyInit_fail;
183468 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
183469
183470 /* Figure out the node size to use. */
183471 rc = getNodeSize(db, pRtree, isCreate, pzErr);
183472 if( rc ) goto geopolyInit_fail;
183473 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
183474 if( rc ){
183475 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
183476 goto geopolyInit_fail;
183477 }
183478
183479 *ppVtab = (sqlite3_vtab *)pRtree;
183480 return SQLITE_OK;
183481
183482geopolyInit_fail:
183483 if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
183484 assert( *ppVtab==0 );
183485 assert( pRtree->nBusy==1 );
183486 rtreeRelease(pRtree);
183487 return rc;
183488}
183489
183490
183491/*
183492** GEOPOLY virtual table module xCreate method.
183493*/
183494static int geopolyCreate(
183495 sqlite3 *db,
183496 void *pAux,
183497 int argc, const char *const*argv,
183498 sqlite3_vtab **ppVtab,
183499 char **pzErr
183500){
183501 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
183502}
183503
183504/*
183505** GEOPOLY virtual table module xConnect method.
183506*/
183507static int geopolyConnect(
183508 sqlite3 *db,
183509 void *pAux,
183510 int argc, const char *const*argv,
183511 sqlite3_vtab **ppVtab,
183512 char **pzErr
183513){
183514 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
183515}
183516
183517
183518/*
183519** GEOPOLY virtual table module xFilter method.
183520**
183521** Query plans:
183522**
183523** 1 rowid lookup
183524** 2 search for objects overlapping the same bounding box
183525** that contains polygon argv[0]
183526** 3 search for objects overlapping the same bounding box
183527** that contains polygon argv[0]
183528** 4 full table scan
183529*/
183530static int geopolyFilter(
183531 sqlite3_vtab_cursor *pVtabCursor, /* The cursor to initialize */
183532 int idxNum, /* Query plan */
183533 const char *idxStr, /* Not Used */
183534 int argc, sqlite3_value **argv /* Parameters to the query plan */
183535){
183536 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
183537 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
183538 RtreeNode *pRoot = 0;
183539 int rc = SQLITE_OK;
183540 int iCell = 0;
183541 sqlite3_stmt *pStmt;
183542
183543 rtreeReference(pRtree);
183544
183545 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
183546 freeCursorConstraints(pCsr);
183547 sqlite3_free(pCsr->aPoint);
183548 pStmt = pCsr->pReadAux;
183549 memset(pCsr, 0, sizeof(RtreeCursor));
183550 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
183551 pCsr->pReadAux = pStmt;
183552
183553 pCsr->iStrategy = idxNum;
183554 if( idxNum==1 ){
183555 /* Special case - lookup by rowid. */
183556 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
183557 RtreeSearchPoint *p; /* Search point for the leaf */
183558 i64 iRowid = sqlite3_value_int64(argv[0]);
183559 i64 iNode = 0;
183560 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
183561 if( rc==SQLITE_OK && pLeaf!=0 ){
183562 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
183563 assert( p!=0 ); /* Always returns pCsr->sPoint */
183564 pCsr->aNode[0] = pLeaf;
183565 p->id = iNode;
183566 p->eWithin = PARTLY_WITHIN;
183567 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
183568 p->iCell = (u8)iCell;
183569 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
183570 }else{
183571 pCsr->atEOF = 1;
183572 }
183573 }else{
183574 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
183575 ** with the configured constraints.
183576 */
183577 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
183578 if( rc==SQLITE_OK && idxNum<=3 ){
183579 RtreeCoord bbox[4];
183580 RtreeConstraint *p;
183581 assert( argc==1 );
183582 geopolyBBox(0, argv[0], bbox, &rc);
183583 if( rc ){
183584 goto geopoly_filter_end;
183585 }
183586 pCsr->aConstraint = p = sqlite3_malloc(sizeof(RtreeConstraint)*4);
183587 pCsr->nConstraint = 4;
183588 if( p==0 ){
183589 rc = SQLITE_NOMEM;
183590 }else{
183591 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*4);
183592 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
183593 if( idxNum==2 ){
183594 /* Overlap query */
183595 p->op = 'B';
183596 p->iCoord = 0;
183597 p->u.rValue = bbox[1].f;
183598 p++;
183599 p->op = 'D';
183600 p->iCoord = 1;
183601 p->u.rValue = bbox[0].f;
183602 p++;
183603 p->op = 'B';
183604 p->iCoord = 2;
183605 p->u.rValue = bbox[3].f;
183606 p++;
183607 p->op = 'D';
183608 p->iCoord = 3;
183609 p->u.rValue = bbox[2].f;
183610 }else{
183611 /* Within query */
183612 p->op = 'D';
183613 p->iCoord = 0;
183614 p->u.rValue = bbox[0].f;
183615 p++;
183616 p->op = 'B';
183617 p->iCoord = 1;
183618 p->u.rValue = bbox[1].f;
183619 p++;
183620 p->op = 'D';
183621 p->iCoord = 2;
183622 p->u.rValue = bbox[2].f;
183623 p++;
183624 p->op = 'B';
183625 p->iCoord = 3;
183626 p->u.rValue = bbox[3].f;
183627 }
183628 }
183629 }
183630 if( rc==SQLITE_OK ){
183631 RtreeSearchPoint *pNew;
183632 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
183633 if( pNew==0 ){
183634 rc = SQLITE_NOMEM;
183635 goto geopoly_filter_end;
183636 }
183637 pNew->id = 1;
183638 pNew->iCell = 0;
183639 pNew->eWithin = PARTLY_WITHIN;
183640 assert( pCsr->bPoint==1 );
183641 pCsr->aNode[0] = pRoot;
183642 pRoot = 0;
183643 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
183644 rc = rtreeStepToLeaf(pCsr);
183645 }
183646 }
183647
183648geopoly_filter_end:
183649 nodeRelease(pRtree, pRoot);
183650 rtreeRelease(pRtree);
183651 return rc;
183652}
183653
183654/*
183655** Rtree virtual table module xBestIndex method. There are three
183656** table scan strategies to choose from (in order from most to
183657** least desirable):
183658**
183659** idxNum idxStr Strategy
183660** ------------------------------------------------
183661** 1 "rowid" Direct lookup by rowid.
183662** 2 "rtree" R-tree overlap query using geopoly_overlap()
183663** 3 "rtree" R-tree within query using geopoly_within()
183664** 4 "fullscan" full-table scan.
183665** ------------------------------------------------
183666*/
183667static int geopolyBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
183668 int ii;
183669 int iRowidTerm = -1;
183670 int iFuncTerm = -1;
183671 int idxNum = 0;
183672
183673 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
183674 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
183675 if( !p->usable ) continue;
183676 if( p->iColumn<0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
183677 iRowidTerm = ii;
183678 break;
183679 }
183680 if( p->iColumn==0 && p->op>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
183681 /* p->op==SQLITE_INDEX_CONSTRAINT_FUNCTION for geopoly_overlap()
183682 ** p->op==(SQLITE_INDEX_CONTRAINT_FUNCTION+1) for geopoly_within().
183683 ** See geopolyFindFunction() */
183684 iFuncTerm = ii;
183685 idxNum = p->op - SQLITE_INDEX_CONSTRAINT_FUNCTION + 2;
183686 }
183687 }
183688
183689 if( iRowidTerm>=0 ){
183690 pIdxInfo->idxNum = 1;
183691 pIdxInfo->idxStr = "rowid";
183692 pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
183693 pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
183694 pIdxInfo->estimatedCost = 30.0;
183695 pIdxInfo->estimatedRows = 1;
183696 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
183697 return SQLITE_OK;
183698 }
183699 if( iFuncTerm>=0 ){
183700 pIdxInfo->idxNum = idxNum;
183701 pIdxInfo->idxStr = "rtree";
183702 pIdxInfo->aConstraintUsage[iFuncTerm].argvIndex = 1;
183703 pIdxInfo->aConstraintUsage[iFuncTerm].omit = 0;
183704 pIdxInfo->estimatedCost = 300.0;
183705 pIdxInfo->estimatedRows = 10;
183706 return SQLITE_OK;
183707 }
183708 pIdxInfo->idxNum = 4;
183709 pIdxInfo->idxStr = "fullscan";
183710 pIdxInfo->estimatedCost = 3000000.0;
183711 pIdxInfo->estimatedRows = 100000;
183712 return SQLITE_OK;
183713}
183714
183715
183716/*
183717** GEOPOLY virtual table module xColumn method.
183718*/
183719static int geopolyColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
183720 Rtree *pRtree = (Rtree *)cur->pVtab;
183721 RtreeCursor *pCsr = (RtreeCursor *)cur;
183722 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
183723 int rc = SQLITE_OK;
183724 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
183725
183726 if( rc ) return rc;
183727 if( p==0 ) return SQLITE_OK;
183728 if( i==0 && sqlite3_vtab_nochange(ctx) ) return SQLITE_OK;
183729 if( i<=pRtree->nAux ){
183730 if( !pCsr->bAuxValid ){
183731 if( pCsr->pReadAux==0 ){
183732 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
183733 &pCsr->pReadAux, 0);
183734 if( rc ) return rc;
183735 }
183736 sqlite3_bind_int64(pCsr->pReadAux, 1,
183737 nodeGetRowid(pRtree, pNode, p->iCell));
183738 rc = sqlite3_step(pCsr->pReadAux);
183739 if( rc==SQLITE_ROW ){
183740 pCsr->bAuxValid = 1;
183741 }else{
183742 sqlite3_reset(pCsr->pReadAux);
183743 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
183744 return rc;
183745 }
183746 }
183747 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pReadAux, i+2));
183748 }
183749 return SQLITE_OK;
183750}
183751
183752
183753/*
183754** The xUpdate method for GEOPOLY module virtual tables.
183755**
183756** For DELETE:
183757**
183758** argv[0] = the rowid to be deleted
183759**
183760** For INSERT:
183761**
183762** argv[0] = SQL NULL
183763** argv[1] = rowid to insert, or an SQL NULL to select automatically
183764** argv[2] = _shape column
183765** argv[3] = first application-defined column....
183766**
183767** For UPDATE:
183768**
183769** argv[0] = rowid to modify. Never NULL
183770** argv[1] = rowid after the change. Never NULL
183771** argv[2] = new value for _shape
183772** argv[3] = new value for first application-defined column....
183773*/
183774static int geopolyUpdate(
183775 sqlite3_vtab *pVtab,
183776 int nData,
183777 sqlite3_value **aData,
183778 sqlite_int64 *pRowid
183779){
183780 Rtree *pRtree = (Rtree *)pVtab;
183781 int rc = SQLITE_OK;
183782 RtreeCell cell; /* New cell to insert if nData>1 */
183783 i64 oldRowid; /* The old rowid */
183784 int oldRowidValid; /* True if oldRowid is valid */
183785 i64 newRowid; /* The new rowid */
183786 int newRowidValid; /* True if newRowid is valid */
183787 int coordChange = 0; /* Change in coordinates */
183788
183789 if( pRtree->nNodeRef ){
183790 /* Unable to write to the btree while another cursor is reading from it,
183791 ** since the write might do a rebalance which would disrupt the read
183792 ** cursor. */
183793 return SQLITE_LOCKED_VTAB;
183794 }
183795 rtreeReference(pRtree);
183796 assert(nData>=1);
183797
183798 oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL;;
183799 oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
183800 newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL;
183801 newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
183802 cell.iRowid = newRowid;
183803
183804 if( nData>1 /* not a DELETE */
183805 && (!oldRowidValid /* INSERT */
183806 || !sqlite3_value_nochange(aData[2]) /* UPDATE _shape */
183807 || oldRowid!=newRowid) /* Rowid change */
183808 ){
183809 geopolyBBox(0, aData[2], cell.aCoord, &rc);
183810 if( rc ){
183811 if( rc==SQLITE_ERROR ){
183812 pVtab->zErrMsg =
183813 sqlite3_mprintf("_shape does not contain a valid polygon");
183814 }
183815 goto geopoly_update_end;
183816 }
183817 coordChange = 1;
183818
183819 /* If a rowid value was supplied, check if it is already present in
183820 ** the table. If so, the constraint has failed. */
183821 if( newRowidValid && (!oldRowidValid || oldRowid!=newRowid) ){
183822 int steprc;
183823 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
183824 steprc = sqlite3_step(pRtree->pReadRowid);
183825 rc = sqlite3_reset(pRtree->pReadRowid);
183826 if( SQLITE_ROW==steprc ){
183827 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
183828 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
183829 }else{
183830 rc = rtreeConstraintError(pRtree, 0);
183831 }
183832 }
183833 }
183834 }
183835
183836 /* If aData[0] is not an SQL NULL value, it is the rowid of a
183837 ** record to delete from the r-tree table. The following block does
183838 ** just that.
183839 */
183840 if( rc==SQLITE_OK && (nData==1 || (coordChange && oldRowidValid)) ){
183841 rc = rtreeDeleteRowid(pRtree, oldRowid);
183842 }
183843
183844 /* If the aData[] array contains more than one element, elements
183845 ** (aData[2]..aData[argc-1]) contain a new record to insert into
183846 ** the r-tree structure.
183847 */
183848 if( rc==SQLITE_OK && nData>1 && coordChange ){
183849 /* Insert the new record into the r-tree */
183850 RtreeNode *pLeaf = 0;
183851 if( !newRowidValid ){
183852 rc = rtreeNewRowid(pRtree, &cell.iRowid);
183853 }
183854 *pRowid = cell.iRowid;
183855 if( rc==SQLITE_OK ){
183856 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
183857 }
183858 if( rc==SQLITE_OK ){
183859 int rc2;
183860 pRtree->iReinsertHeight = -1;
183861 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
183862 rc2 = nodeRelease(pRtree, pLeaf);
183863 if( rc==SQLITE_OK ){
183864 rc = rc2;
183865 }
183866 }
183867 }
183868
183869 /* Change the data */
183870 if( rc==SQLITE_OK && nData>1 ){
183871 sqlite3_stmt *pUp = pRtree->pWriteAux;
183872 int jj;
183873 int nChange = 0;
183874 sqlite3_bind_int64(pUp, 1, cell.iRowid);
183875 assert( pRtree->nAux>=1 );
183876 if( sqlite3_value_nochange(aData[2]) ){
183877 sqlite3_bind_null(pUp, 2);
183878 }else{
183879 sqlite3_bind_value(pUp, 2, aData[2]);
183880 nChange = 1;
183881 }
183882 for(jj=1; jj<pRtree->nAux; jj++){
183883 nChange++;
183884 sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
183885 }
183886 if( nChange ){
183887 sqlite3_step(pUp);
183888 rc = sqlite3_reset(pUp);
183889 }
183890 }
183891
183892geopoly_update_end:
183893 rtreeRelease(pRtree);
183894 return rc;
183895}
183896
183897/*
183898** Report that geopoly_overlap() is an overloaded function suitable
183899** for use in xBestIndex.
183900*/
183901static int geopolyFindFunction(
183902 sqlite3_vtab *pVtab,
183903 int nArg,
183904 const char *zName,
183905 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
183906 void **ppArg
183907){
183908 if( sqlite3_stricmp(zName, "geopoly_overlap")==0 ){
183909 *pxFunc = geopolyOverlapFunc;
183910 *ppArg = 0;
183911 return SQLITE_INDEX_CONSTRAINT_FUNCTION;
183912 }
183913 if( sqlite3_stricmp(zName, "geopoly_within")==0 ){
183914 *pxFunc = geopolyWithinFunc;
183915 *ppArg = 0;
183916 return SQLITE_INDEX_CONSTRAINT_FUNCTION+1;
183917 }
183918 return 0;
183919}
183920
183921
183922static sqlite3_module geopolyModule = {
183923 2, /* iVersion */
183924 geopolyCreate, /* xCreate - create a table */
183925 geopolyConnect, /* xConnect - connect to an existing table */
183926 geopolyBestIndex, /* xBestIndex - Determine search strategy */
183927 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
183928 rtreeDestroy, /* xDestroy - Drop a table */
183929 rtreeOpen, /* xOpen - open a cursor */
183930 rtreeClose, /* xClose - close a cursor */
183931 geopolyFilter, /* xFilter - configure scan constraints */
183932 rtreeNext, /* xNext - advance a cursor */
183933 rtreeEof, /* xEof */
183934 geopolyColumn, /* xColumn - read data */
183935 rtreeRowid, /* xRowid - read data */
183936 geopolyUpdate, /* xUpdate - write data */
183937 rtreeBeginTransaction, /* xBegin - begin transaction */
183938 rtreeEndTransaction, /* xSync - sync transaction */
183939 rtreeEndTransaction, /* xCommit - commit transaction */
183940 rtreeEndTransaction, /* xRollback - rollback transaction */
183941 geopolyFindFunction, /* xFindFunction - function overloading */
183942 rtreeRename, /* xRename - rename the table */
183943 rtreeSavepoint, /* xSavepoint */
183944 0, /* xRelease */
183945 0, /* xRollbackTo */
183946};
183947
183948static int sqlite3_geopoly_init(sqlite3 *db){
183949 int rc = SQLITE_OK;
183950 static const struct {
183951 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
183952 int nArg;
183953 const char *zName;
183954 } aFunc[] = {
183955 { geopolyAreaFunc, 1, "geopoly_area" },
183956 { geopolyBlobFunc, 1, "geopoly_blob" },
183957 { geopolyJsonFunc, 1, "geopoly_json" },
183958 { geopolySvgFunc, -1, "geopoly_svg" },
183959 { geopolyWithinFunc, 2, "geopoly_within" },
183960 { geopolyContainsPointFunc, 3, "geopoly_contains_point" },
183961 { geopolyOverlapFunc, 2, "geopoly_overlap" },
183962 { geopolyDebugFunc, 1, "geopoly_debug" },
183963 { geopolyBBoxFunc, 1, "geopoly_bbox" },
183964 { geopolyXformFunc, 7, "geopoly_xform" },
183965 };
183966 static const struct {
183967 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
183968 void (*xFinal)(sqlite3_context*);
183969 const char *zName;
183970 } aAgg[] = {
183971 { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox" },
183972 };
183973 int i;
183974 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
183975 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
183976 SQLITE_UTF8, 0,
183977 aFunc[i].xFunc, 0, 0);
183978 }
183979 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
183980 rc = sqlite3_create_function(db, aAgg[i].zName, 1, SQLITE_UTF8, 0,
183981 0, aAgg[i].xStep, aAgg[i].xFinal);
183982 }
183983 if( rc==SQLITE_OK ){
183984 rc = sqlite3_create_module_v2(db, "geopoly", &geopolyModule, 0, 0);
183985 }
183986 return rc;
183987}
183988
183989/************** End of geopoly.c *********************************************/
183990/************** Continuing where we left off in rtree.c **********************/
183991#endif
183992
183993/*
183994** Register the r-tree module with database handle db. This creates the
183995** virtual table module "rtree" and the debugging/analysis scalar
183996** function "rtreenode".
183997*/
183998SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
183999 const int utf8 = SQLITE_UTF8;
184000 int rc;
184001
184002 rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
184003 if( rc==SQLITE_OK ){
184004 rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
184005 }
184006 if( rc==SQLITE_OK ){
184007 rc = sqlite3_create_function(db, "rtreecheck", -1, utf8, 0,rtreecheck, 0,0);
184008 }
184009 if( rc==SQLITE_OK ){
184010#ifdef SQLITE_RTREE_INT_ONLY
184011 void *c = (void *)RTREE_COORD_INT32;
184012#else
184013 void *c = (void *)RTREE_COORD_REAL32;
184014#endif
184015 rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
184016 }
184017 if( rc==SQLITE_OK ){
184018 void *c = (void *)RTREE_COORD_INT32;
184019 rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
184020 }
184021#ifdef SQLITE_ENABLE_GEOPOLY
184022 if( rc==SQLITE_OK ){
184023 rc = sqlite3_geopoly_init(db);
184024 }
184025#endif
184026
184027 return rc;
184028}
184029
184030/*
184031** This routine deletes the RtreeGeomCallback object that was attached
184032** one of the SQL functions create by sqlite3_rtree_geometry_callback()
184033** or sqlite3_rtree_query_callback(). In other words, this routine is the
184034** destructor for an RtreeGeomCallback objecct. This routine is called when
184035** the corresponding SQL function is deleted.
184036*/
184037static void rtreeFreeCallback(void *p){
184038 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
184039 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
184040 sqlite3_free(p);
184041}
184042
184043/*
184044** This routine frees the BLOB that is returned by geomCallback().
184045*/
184046static void rtreeMatchArgFree(void *pArg){
184047 int i;
184048 RtreeMatchArg *p = (RtreeMatchArg*)pArg;
184049 for(i=0; i<p->nParam; i++){
184050 sqlite3_value_free(p->apSqlParam[i]);
184051 }
184052 sqlite3_free(p);
184053}
184054
184055/*
184056** Each call to sqlite3_rtree_geometry_callback() or
184057** sqlite3_rtree_query_callback() creates an ordinary SQLite
184058** scalar function that is implemented by this routine.
184059**
184060** All this function does is construct an RtreeMatchArg object that
184061** contains the geometry-checking callback routines and a list of
184062** parameters to this function, then return that RtreeMatchArg object
184063** as a BLOB.
184064**
184065** The R-Tree MATCH operator will read the returned BLOB, deserialize
184066** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
184067** out which elements of the R-Tree should be returned by the query.
184068*/
184069static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
184070 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
184071 RtreeMatchArg *pBlob;
184072 int nBlob;
184073 int memErr = 0;
184074
184075 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
184076 + nArg*sizeof(sqlite3_value*);
184077 pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
184078 if( !pBlob ){
184079 sqlite3_result_error_nomem(ctx);
184080 }else{
184081 int i;
184082 pBlob->iSize = nBlob;
184083 pBlob->cb = pGeomCtx[0];
184084 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
184085 pBlob->nParam = nArg;
184086 for(i=0; i<nArg; i++){
184087 pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
184088 if( pBlob->apSqlParam[i]==0 ) memErr = 1;
184089#ifdef SQLITE_RTREE_INT_ONLY
184090 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
184091#else
184092 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
184093#endif
184094 }
184095 if( memErr ){
184096 sqlite3_result_error_nomem(ctx);
184097 rtreeMatchArgFree(pBlob);
184098 }else{
184099 sqlite3_result_pointer(ctx, pBlob, "RtreeMatchArg", rtreeMatchArgFree);
184100 }
184101 }
184102}
184103
184104/*
184105** Register a new geometry function for use with the r-tree MATCH operator.
184106*/
184107SQLITE_API int sqlite3_rtree_geometry_callback(
184108 sqlite3 *db, /* Register SQL function on this connection */
184109 const char *zGeom, /* Name of the new SQL function */
184110 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
184111 void *pContext /* Extra data associated with the callback */
184112){
184113 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
184114
184115 /* Allocate and populate the context object. */
184116 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
184117 if( !pGeomCtx ) return SQLITE_NOMEM;
184118 pGeomCtx->xGeom = xGeom;
184119 pGeomCtx->xQueryFunc = 0;
184120 pGeomCtx->xDestructor = 0;
184121 pGeomCtx->pContext = pContext;
184122 return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
184123 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
184124 );
184125}
184126
184127/*
184128** Register a new 2nd-generation geometry function for use with the
184129** r-tree MATCH operator.
184130*/
184131SQLITE_API int sqlite3_rtree_query_callback(
184132 sqlite3 *db, /* Register SQL function on this connection */
184133 const char *zQueryFunc, /* Name of new SQL function */
184134 int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
184135 void *pContext, /* Extra data passed into the callback */
184136 void (*xDestructor)(void*) /* Destructor for the extra data */
184137){
184138 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
184139
184140 /* Allocate and populate the context object. */
184141 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
184142 if( !pGeomCtx ) return SQLITE_NOMEM;
184143 pGeomCtx->xGeom = 0;
184144 pGeomCtx->xQueryFunc = xQueryFunc;
184145 pGeomCtx->xDestructor = xDestructor;
184146 pGeomCtx->pContext = pContext;
184147 return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY,
184148 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
184149 );
184150}
184151
184152#if !SQLITE_CORE
184153#ifdef _WIN32
184154__declspec(dllexport)
184155#endif
184156SQLITE_API int sqlite3_rtree_init(
184157 sqlite3 *db,
184158 char **pzErrMsg,
184159 const sqlite3_api_routines *pApi
184160){
184161 SQLITE_EXTENSION_INIT2(pApi)
184162 return sqlite3RtreeInit(db);
184163}
184164#endif
184165
184166#endif
184167
184168/************** End of rtree.c ***********************************************/
184169/************** Begin file icu.c *********************************************/
184170/*
184171** 2007 May 6
184172**
184173** The author disclaims copyright to this source code. In place of
184174** a legal notice, here is a blessing:
184175**
184176** May you do good and not evil.
184177** May you find forgiveness for yourself and forgive others.
184178** May you share freely, never taking more than you give.
184179**
184180*************************************************************************
184181** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
184182**
184183** This file implements an integration between the ICU library
184184** ("International Components for Unicode", an open-source library
184185** for handling unicode data) and SQLite. The integration uses
184186** ICU to provide the following to SQLite:
184187**
184188** * An implementation of the SQL regexp() function (and hence REGEXP
184189** operator) using the ICU uregex_XX() APIs.
184190**
184191** * Implementations of the SQL scalar upper() and lower() functions
184192** for case mapping.
184193**
184194** * Integration of ICU and SQLite collation sequences.
184195**
184196** * An implementation of the LIKE operator that uses ICU to
184197** provide case-independent matching.
184198*/
184199
184200#if !defined(SQLITE_CORE) \
184201 || defined(SQLITE_ENABLE_ICU) \
184202 || defined(SQLITE_ENABLE_ICU_COLLATIONS)
184203
184204/* Include ICU headers */
184205#include <unicode/utypes.h>
184206#include <unicode/uregex.h>
184207#include <unicode/ustring.h>
184208#include <unicode/ucol.h>
184209
184210/* #include <assert.h> */
184211
184212#ifndef SQLITE_CORE
184213/* #include "sqlite3ext.h" */
184214 SQLITE_EXTENSION_INIT1
184215#else
184216/* #include "sqlite3.h" */
184217#endif
184218
184219/*
184220** This function is called when an ICU function called from within
184221** the implementation of an SQL scalar function returns an error.
184222**
184223** The scalar function context passed as the first argument is
184224** loaded with an error message based on the following two args.
184225*/
184226static void icuFunctionError(
184227 sqlite3_context *pCtx, /* SQLite scalar function context */
184228 const char *zName, /* Name of ICU function that failed */
184229 UErrorCode e /* Error code returned by ICU function */
184230){
184231 char zBuf[128];
184232 sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
184233 zBuf[127] = '\0';
184234 sqlite3_result_error(pCtx, zBuf, -1);
184235}
184236
184237#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
184238
184239/*
184240** Maximum length (in bytes) of the pattern in a LIKE or GLOB
184241** operator.
184242*/
184243#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
184244# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
184245#endif
184246
184247/*
184248** Version of sqlite3_free() that is always a function, never a macro.
184249*/
184250static void xFree(void *p){
184251 sqlite3_free(p);
184252}
184253
184254/*
184255** This lookup table is used to help decode the first byte of
184256** a multi-byte UTF8 character. It is copied here from SQLite source
184257** code file utf8.c.
184258*/
184259static const unsigned char icuUtf8Trans1[] = {
184260 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
184261 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
184262 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
184263 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
184264 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
184265 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
184266 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
184267 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
184268};
184269
184270#define SQLITE_ICU_READ_UTF8(zIn, c) \
184271 c = *(zIn++); \
184272 if( c>=0xc0 ){ \
184273 c = icuUtf8Trans1[c-0xc0]; \
184274 while( (*zIn & 0xc0)==0x80 ){ \
184275 c = (c<<6) + (0x3f & *(zIn++)); \
184276 } \
184277 }
184278
184279#define SQLITE_ICU_SKIP_UTF8(zIn) \
184280 assert( *zIn ); \
184281 if( *(zIn++)>=0xc0 ){ \
184282 while( (*zIn & 0xc0)==0x80 ){zIn++;} \
184283 }
184284
184285
184286/*
184287** Compare two UTF-8 strings for equality where the first string is
184288** a "LIKE" expression. Return true (1) if they are the same and
184289** false (0) if they are different.
184290*/
184291static int icuLikeCompare(
184292 const uint8_t *zPattern, /* LIKE pattern */
184293 const uint8_t *zString, /* The UTF-8 string to compare against */
184294 const UChar32 uEsc /* The escape character */
184295){
184296 static const uint32_t MATCH_ONE = (uint32_t)'_';
184297 static const uint32_t MATCH_ALL = (uint32_t)'%';
184298
184299 int prevEscape = 0; /* True if the previous character was uEsc */
184300
184301 while( 1 ){
184302
184303 /* Read (and consume) the next character from the input pattern. */
184304 uint32_t uPattern;
184305 SQLITE_ICU_READ_UTF8(zPattern, uPattern);
184306 if( uPattern==0 ) break;
184307
184308 /* There are now 4 possibilities:
184309 **
184310 ** 1. uPattern is an unescaped match-all character "%",
184311 ** 2. uPattern is an unescaped match-one character "_",
184312 ** 3. uPattern is an unescaped escape character, or
184313 ** 4. uPattern is to be handled as an ordinary character
184314 */
184315 if( !prevEscape && uPattern==MATCH_ALL ){
184316 /* Case 1. */
184317 uint8_t c;
184318
184319 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
184320 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
184321 ** test string.
184322 */
184323 while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){
184324 if( c==MATCH_ONE ){
184325 if( *zString==0 ) return 0;
184326 SQLITE_ICU_SKIP_UTF8(zString);
184327 }
184328 zPattern++;
184329 }
184330
184331 if( *zPattern==0 ) return 1;
184332
184333 while( *zString ){
184334 if( icuLikeCompare(zPattern, zString, uEsc) ){
184335 return 1;
184336 }
184337 SQLITE_ICU_SKIP_UTF8(zString);
184338 }
184339 return 0;
184340
184341 }else if( !prevEscape && uPattern==MATCH_ONE ){
184342 /* Case 2. */
184343 if( *zString==0 ) return 0;
184344 SQLITE_ICU_SKIP_UTF8(zString);
184345
184346 }else if( !prevEscape && uPattern==(uint32_t)uEsc){
184347 /* Case 3. */
184348 prevEscape = 1;
184349
184350 }else{
184351 /* Case 4. */
184352 uint32_t uString;
184353 SQLITE_ICU_READ_UTF8(zString, uString);
184354 uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT);
184355 uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT);
184356 if( uString!=uPattern ){
184357 return 0;
184358 }
184359 prevEscape = 0;
184360 }
184361 }
184362
184363 return *zString==0;
184364}
184365
184366/*
184367** Implementation of the like() SQL function. This function implements
184368** the build-in LIKE operator. The first argument to the function is the
184369** pattern and the second argument is the string. So, the SQL statements:
184370**
184371** A LIKE B
184372**
184373** is implemented as like(B, A). If there is an escape character E,
184374**
184375** A LIKE B ESCAPE E
184376**
184377** is mapped to like(B, A, E).
184378*/
184379static void icuLikeFunc(
184380 sqlite3_context *context,
184381 int argc,
184382 sqlite3_value **argv
184383){
184384 const unsigned char *zA = sqlite3_value_text(argv[0]);
184385 const unsigned char *zB = sqlite3_value_text(argv[1]);
184386 UChar32 uEsc = 0;
184387
184388 /* Limit the length of the LIKE or GLOB pattern to avoid problems
184389 ** of deep recursion and N*N behavior in patternCompare().
184390 */
184391 if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
184392 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
184393 return;
184394 }
184395
184396
184397 if( argc==3 ){
184398 /* The escape character string must consist of a single UTF-8 character.
184399 ** Otherwise, return an error.
184400 */
184401 int nE= sqlite3_value_bytes(argv[2]);
184402 const unsigned char *zE = sqlite3_value_text(argv[2]);
184403 int i = 0;
184404 if( zE==0 ) return;
184405 U8_NEXT(zE, i, nE, uEsc);
184406 if( i!=nE){
184407 sqlite3_result_error(context,
184408 "ESCAPE expression must be a single character", -1);
184409 return;
184410 }
184411 }
184412
184413 if( zA && zB ){
184414 sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
184415 }
184416}
184417
184418/*
184419** Function to delete compiled regexp objects. Registered as
184420** a destructor function with sqlite3_set_auxdata().
184421*/
184422static void icuRegexpDelete(void *p){
184423 URegularExpression *pExpr = (URegularExpression *)p;
184424 uregex_close(pExpr);
184425}
184426
184427/*
184428** Implementation of SQLite REGEXP operator. This scalar function takes
184429** two arguments. The first is a regular expression pattern to compile
184430** the second is a string to match against that pattern. If either
184431** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
184432** is 1 if the string matches the pattern, or 0 otherwise.
184433**
184434** SQLite maps the regexp() function to the regexp() operator such
184435** that the following two are equivalent:
184436**
184437** zString REGEXP zPattern
184438** regexp(zPattern, zString)
184439**
184440** Uses the following ICU regexp APIs:
184441**
184442** uregex_open()
184443** uregex_matches()
184444** uregex_close()
184445*/
184446static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
184447 UErrorCode status = U_ZERO_ERROR;
184448 URegularExpression *pExpr;
184449 UBool res;
184450 const UChar *zString = sqlite3_value_text16(apArg[1]);
184451
184452 (void)nArg; /* Unused parameter */
184453
184454 /* If the left hand side of the regexp operator is NULL,
184455 ** then the result is also NULL.
184456 */
184457 if( !zString ){
184458 return;
184459 }
184460
184461 pExpr = sqlite3_get_auxdata(p, 0);
184462 if( !pExpr ){
184463 const UChar *zPattern = sqlite3_value_text16(apArg[0]);
184464 if( !zPattern ){
184465 return;
184466 }
184467 pExpr = uregex_open(zPattern, -1, 0, 0, &status);
184468
184469 if( U_SUCCESS(status) ){
184470 sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
184471 }else{
184472 assert(!pExpr);
184473 icuFunctionError(p, "uregex_open", status);
184474 return;
184475 }
184476 }
184477
184478 /* Configure the text that the regular expression operates on. */
184479 uregex_setText(pExpr, zString, -1, &status);
184480 if( !U_SUCCESS(status) ){
184481 icuFunctionError(p, "uregex_setText", status);
184482 return;
184483 }
184484
184485 /* Attempt the match */
184486 res = uregex_matches(pExpr, 0, &status);
184487 if( !U_SUCCESS(status) ){
184488 icuFunctionError(p, "uregex_matches", status);
184489 return;
184490 }
184491
184492 /* Set the text that the regular expression operates on to a NULL
184493 ** pointer. This is not really necessary, but it is tidier than
184494 ** leaving the regular expression object configured with an invalid
184495 ** pointer after this function returns.
184496 */
184497 uregex_setText(pExpr, 0, 0, &status);
184498
184499 /* Return 1 or 0. */
184500 sqlite3_result_int(p, res ? 1 : 0);
184501}
184502
184503/*
184504** Implementations of scalar functions for case mapping - upper() and
184505** lower(). Function upper() converts its input to upper-case (ABC).
184506** Function lower() converts to lower-case (abc).
184507**
184508** ICU provides two types of case mapping, "general" case mapping and
184509** "language specific". Refer to ICU documentation for the differences
184510** between the two.
184511**
184512** To utilise "general" case mapping, the upper() or lower() scalar
184513** functions are invoked with one argument:
184514**
184515** upper('ABC') -> 'abc'
184516** lower('abc') -> 'ABC'
184517**
184518** To access ICU "language specific" case mapping, upper() or lower()
184519** should be invoked with two arguments. The second argument is the name
184520** of the locale to use. Passing an empty string ("") or SQL NULL value
184521** as the second argument is the same as invoking the 1 argument version
184522** of upper() or lower().
184523**
184524** lower('I', 'en_us') -> 'i'
184525** lower('I', 'tr_tr') -> '\u131' (small dotless i)
184526**
184527** http://www.icu-project.org/userguide/posix.html#case_mappings
184528*/
184529static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
184530 const UChar *zInput; /* Pointer to input string */
184531 UChar *zOutput = 0; /* Pointer to output buffer */
184532 int nInput; /* Size of utf-16 input string in bytes */
184533 int nOut; /* Size of output buffer in bytes */
184534 int cnt;
184535 int bToUpper; /* True for toupper(), false for tolower() */
184536 UErrorCode status;
184537 const char *zLocale = 0;
184538
184539 assert(nArg==1 || nArg==2);
184540 bToUpper = (sqlite3_user_data(p)!=0);
184541 if( nArg==2 ){
184542 zLocale = (const char *)sqlite3_value_text(apArg[1]);
184543 }
184544
184545 zInput = sqlite3_value_text16(apArg[0]);
184546 if( !zInput ){
184547 return;
184548 }
184549 nOut = nInput = sqlite3_value_bytes16(apArg[0]);
184550 if( nOut==0 ){
184551 sqlite3_result_text16(p, "", 0, SQLITE_STATIC);
184552 return;
184553 }
184554
184555 for(cnt=0; cnt<2; cnt++){
184556 UChar *zNew = sqlite3_realloc(zOutput, nOut);
184557 if( zNew==0 ){
184558 sqlite3_free(zOutput);
184559 sqlite3_result_error_nomem(p);
184560 return;
184561 }
184562 zOutput = zNew;
184563 status = U_ZERO_ERROR;
184564 if( bToUpper ){
184565 nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
184566 }else{
184567 nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
184568 }
184569
184570 if( U_SUCCESS(status) ){
184571 sqlite3_result_text16(p, zOutput, nOut, xFree);
184572 }else if( status==U_BUFFER_OVERFLOW_ERROR ){
184573 assert( cnt==0 );
184574 continue;
184575 }else{
184576 icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status);
184577 }
184578 return;
184579 }
184580 assert( 0 ); /* Unreachable */
184581}
184582
184583#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
184584
184585/*
184586** Collation sequence destructor function. The pCtx argument points to
184587** a UCollator structure previously allocated using ucol_open().
184588*/
184589static void icuCollationDel(void *pCtx){
184590 UCollator *p = (UCollator *)pCtx;
184591 ucol_close(p);
184592}
184593
184594/*
184595** Collation sequence comparison function. The pCtx argument points to
184596** a UCollator structure previously allocated using ucol_open().
184597*/
184598static int icuCollationColl(
184599 void *pCtx,
184600 int nLeft,
184601 const void *zLeft,
184602 int nRight,
184603 const void *zRight
184604){
184605 UCollationResult res;
184606 UCollator *p = (UCollator *)pCtx;
184607 res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
184608 switch( res ){
184609 case UCOL_LESS: return -1;
184610 case UCOL_GREATER: return +1;
184611 case UCOL_EQUAL: return 0;
184612 }
184613 assert(!"Unexpected return value from ucol_strcoll()");
184614 return 0;
184615}
184616
184617/*
184618** Implementation of the scalar function icu_load_collation().
184619**
184620** This scalar function is used to add ICU collation based collation
184621** types to an SQLite database connection. It is intended to be called
184622** as follows:
184623**
184624** SELECT icu_load_collation(<locale>, <collation-name>);
184625**
184626** Where <locale> is a string containing an ICU locale identifier (i.e.
184627** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
184628** collation sequence to create.
184629*/
184630static void icuLoadCollation(
184631 sqlite3_context *p,
184632 int nArg,
184633 sqlite3_value **apArg
184634){
184635 sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
184636 UErrorCode status = U_ZERO_ERROR;
184637 const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
184638 const char *zName; /* SQL Collation sequence name (eg. "japanese") */
184639 UCollator *pUCollator; /* ICU library collation object */
184640 int rc; /* Return code from sqlite3_create_collation_x() */
184641
184642 assert(nArg==2);
184643 (void)nArg; /* Unused parameter */
184644 zLocale = (const char *)sqlite3_value_text(apArg[0]);
184645 zName = (const char *)sqlite3_value_text(apArg[1]);
184646
184647 if( !zLocale || !zName ){
184648 return;
184649 }
184650
184651 pUCollator = ucol_open(zLocale, &status);
184652 if( !U_SUCCESS(status) ){
184653 icuFunctionError(p, "ucol_open", status);
184654 return;
184655 }
184656 assert(p);
184657
184658 rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
184659 icuCollationColl, icuCollationDel
184660 );
184661 if( rc!=SQLITE_OK ){
184662 ucol_close(pUCollator);
184663 sqlite3_result_error(p, "Error registering collation function", -1);
184664 }
184665}
184666
184667/*
184668** Register the ICU extension functions with database db.
184669*/
184670SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
184671 static const struct IcuScalar {
184672 const char *zName; /* Function name */
184673 unsigned char nArg; /* Number of arguments */
184674 unsigned short enc; /* Optimal text encoding */
184675 unsigned char iContext; /* sqlite3_user_data() context */
184676 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
184677 } scalars[] = {
184678 {"icu_load_collation", 2, SQLITE_UTF8, 1, icuLoadCollation},
184679#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
184680 {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC, 0, icuRegexpFunc},
184681 {"lower", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
184682 {"lower", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
184683 {"upper", 1, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
184684 {"upper", 2, SQLITE_UTF16|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
184685 {"lower", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
184686 {"lower", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuCaseFunc16},
184687 {"upper", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
184688 {"upper", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 1, icuCaseFunc16},
184689 {"like", 2, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
184690 {"like", 3, SQLITE_UTF8|SQLITE_DETERMINISTIC, 0, icuLikeFunc},
184691#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
184692 };
184693 int rc = SQLITE_OK;
184694 int i;
184695
184696 for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
184697 const struct IcuScalar *p = &scalars[i];
184698 rc = sqlite3_create_function(
184699 db, p->zName, p->nArg, p->enc,
184700 p->iContext ? (void*)db : (void*)0,
184701 p->xFunc, 0, 0
184702 );
184703 }
184704
184705 return rc;
184706}
184707
184708#if !SQLITE_CORE
184709#ifdef _WIN32
184710__declspec(dllexport)
184711#endif
184712SQLITE_API int sqlite3_icu_init(
184713 sqlite3 *db,
184714 char **pzErrMsg,
184715 const sqlite3_api_routines *pApi
184716){
184717 SQLITE_EXTENSION_INIT2(pApi)
184718 return sqlite3IcuInit(db);
184719}
184720#endif
184721
184722#endif
184723
184724/************** End of icu.c *************************************************/
184725/************** Begin file fts3_icu.c ****************************************/
184726/*
184727** 2007 June 22
184728**
184729** The author disclaims copyright to this source code. In place of
184730** a legal notice, here is a blessing:
184731**
184732** May you do good and not evil.
184733** May you find forgiveness for yourself and forgive others.
184734** May you share freely, never taking more than you give.
184735**
184736*************************************************************************
184737** This file implements a tokenizer for fts3 based on the ICU library.
184738*/
184739/* #include "fts3Int.h" */
184740#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
184741#ifdef SQLITE_ENABLE_ICU
184742
184743/* #include <assert.h> */
184744/* #include <string.h> */
184745/* #include "fts3_tokenizer.h" */
184746
184747#include <unicode/ubrk.h>
184748/* #include <unicode/ucol.h> */
184749/* #include <unicode/ustring.h> */
184750#include <unicode/utf16.h>
184751
184752typedef struct IcuTokenizer IcuTokenizer;
184753typedef struct IcuCursor IcuCursor;
184754
184755struct IcuTokenizer {
184756 sqlite3_tokenizer base;
184757 char *zLocale;
184758};
184759
184760struct IcuCursor {
184761 sqlite3_tokenizer_cursor base;
184762
184763 UBreakIterator *pIter; /* ICU break-iterator object */
184764 int nChar; /* Number of UChar elements in pInput */
184765 UChar *aChar; /* Copy of input using utf-16 encoding */
184766 int *aOffset; /* Offsets of each character in utf-8 input */
184767
184768 int nBuffer;
184769 char *zBuffer;
184770
184771 int iToken;
184772};
184773
184774/*
184775** Create a new tokenizer instance.
184776*/
184777static int icuCreate(
184778 int argc, /* Number of entries in argv[] */
184779 const char * const *argv, /* Tokenizer creation arguments */
184780 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
184781){
184782 IcuTokenizer *p;
184783 int n = 0;
184784
184785 if( argc>0 ){
184786 n = strlen(argv[0])+1;
184787 }
184788 p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
184789 if( !p ){
184790 return SQLITE_NOMEM;
184791 }
184792 memset(p, 0, sizeof(IcuTokenizer));
184793
184794 if( n ){
184795 p->zLocale = (char *)&p[1];
184796 memcpy(p->zLocale, argv[0], n);
184797 }
184798
184799 *ppTokenizer = (sqlite3_tokenizer *)p;
184800
184801 return SQLITE_OK;
184802}
184803
184804/*
184805** Destroy a tokenizer
184806*/
184807static int icuDestroy(sqlite3_tokenizer *pTokenizer){
184808 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
184809 sqlite3_free(p);
184810 return SQLITE_OK;
184811}
184812
184813/*
184814** Prepare to begin tokenizing a particular string. The input
184815** string to be tokenized is pInput[0..nBytes-1]. A cursor
184816** used to incrementally tokenize this string is returned in
184817** *ppCursor.
184818*/
184819static int icuOpen(
184820 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
184821 const char *zInput, /* Input string */
184822 int nInput, /* Length of zInput in bytes */
184823 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
184824){
184825 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
184826 IcuCursor *pCsr;
184827
184828 const int32_t opt = U_FOLD_CASE_DEFAULT;
184829 UErrorCode status = U_ZERO_ERROR;
184830 int nChar;
184831
184832 UChar32 c;
184833 int iInput = 0;
184834 int iOut = 0;
184835
184836 *ppCursor = 0;
184837
184838 if( zInput==0 ){
184839 nInput = 0;
184840 zInput = "";
184841 }else if( nInput<0 ){
184842 nInput = strlen(zInput);
184843 }
184844 nChar = nInput+1;
184845 pCsr = (IcuCursor *)sqlite3_malloc(
184846 sizeof(IcuCursor) + /* IcuCursor */
184847 ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */
184848 (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */
184849 );
184850 if( !pCsr ){
184851 return SQLITE_NOMEM;
184852 }
184853 memset(pCsr, 0, sizeof(IcuCursor));
184854 pCsr->aChar = (UChar *)&pCsr[1];
184855 pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
184856
184857 pCsr->aOffset[iOut] = iInput;
184858 U8_NEXT(zInput, iInput, nInput, c);
184859 while( c>0 ){
184860 int isError = 0;
184861 c = u_foldCase(c, opt);
184862 U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
184863 if( isError ){
184864 sqlite3_free(pCsr);
184865 return SQLITE_ERROR;
184866 }
184867 pCsr->aOffset[iOut] = iInput;
184868
184869 if( iInput<nInput ){
184870 U8_NEXT(zInput, iInput, nInput, c);
184871 }else{
184872 c = 0;
184873 }
184874 }
184875
184876 pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
184877 if( !U_SUCCESS(status) ){
184878 sqlite3_free(pCsr);
184879 return SQLITE_ERROR;
184880 }
184881 pCsr->nChar = iOut;
184882
184883 ubrk_first(pCsr->pIter);
184884 *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
184885 return SQLITE_OK;
184886}
184887
184888/*
184889** Close a tokenization cursor previously opened by a call to icuOpen().
184890*/
184891static int icuClose(sqlite3_tokenizer_cursor *pCursor){
184892 IcuCursor *pCsr = (IcuCursor *)pCursor;
184893 ubrk_close(pCsr->pIter);
184894 sqlite3_free(pCsr->zBuffer);
184895 sqlite3_free(pCsr);
184896 return SQLITE_OK;
184897}
184898
184899/*
184900** Extract the next token from a tokenization cursor.
184901*/
184902static int icuNext(
184903 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
184904 const char **ppToken, /* OUT: *ppToken is the token text */
184905 int *pnBytes, /* OUT: Number of bytes in token */
184906 int *piStartOffset, /* OUT: Starting offset of token */
184907 int *piEndOffset, /* OUT: Ending offset of token */
184908 int *piPosition /* OUT: Position integer of token */
184909){
184910 IcuCursor *pCsr = (IcuCursor *)pCursor;
184911
184912 int iStart = 0;
184913 int iEnd = 0;
184914 int nByte = 0;
184915
184916 while( iStart==iEnd ){
184917 UChar32 c;
184918
184919 iStart = ubrk_current(pCsr->pIter);
184920 iEnd = ubrk_next(pCsr->pIter);
184921 if( iEnd==UBRK_DONE ){
184922 return SQLITE_DONE;
184923 }
184924
184925 while( iStart<iEnd ){
184926 int iWhite = iStart;
184927 U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
184928 if( u_isspace(c) ){
184929 iStart = iWhite;
184930 }else{
184931 break;
184932 }
184933 }
184934 assert(iStart<=iEnd);
184935 }
184936
184937 do {
184938 UErrorCode status = U_ZERO_ERROR;
184939 if( nByte ){
184940 char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
184941 if( !zNew ){
184942 return SQLITE_NOMEM;
184943 }
184944 pCsr->zBuffer = zNew;
184945 pCsr->nBuffer = nByte;
184946 }
184947
184948 u_strToUTF8(
184949 pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */
184950 &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */
184951 &status /* Output success/failure */
184952 );
184953 } while( nByte>pCsr->nBuffer );
184954
184955 *ppToken = pCsr->zBuffer;
184956 *pnBytes = nByte;
184957 *piStartOffset = pCsr->aOffset[iStart];
184958 *piEndOffset = pCsr->aOffset[iEnd];
184959 *piPosition = pCsr->iToken++;
184960
184961 return SQLITE_OK;
184962}
184963
184964/*
184965** The set of routines that implement the simple tokenizer
184966*/
184967static const sqlite3_tokenizer_module icuTokenizerModule = {
184968 0, /* iVersion */
184969 icuCreate, /* xCreate */
184970 icuDestroy, /* xCreate */
184971 icuOpen, /* xOpen */
184972 icuClose, /* xClose */
184973 icuNext, /* xNext */
184974 0, /* xLanguageid */
184975};
184976
184977/*
184978** Set *ppModule to point at the implementation of the ICU tokenizer.
184979*/
184980SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
184981 sqlite3_tokenizer_module const**ppModule
184982){
184983 *ppModule = &icuTokenizerModule;
184984}
184985
184986#endif /* defined(SQLITE_ENABLE_ICU) */
184987#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
184988
184989/************** End of fts3_icu.c ********************************************/
184990/************** Begin file sqlite3rbu.c **************************************/
184991/*
184992** 2014 August 30
184993**
184994** The author disclaims copyright to this source code. In place of
184995** a legal notice, here is a blessing:
184996**
184997** May you do good and not evil.
184998** May you find forgiveness for yourself and forgive others.
184999** May you share freely, never taking more than you give.
185000**
185001*************************************************************************
185002**
185003**
185004** OVERVIEW
185005**
185006** The RBU extension requires that the RBU update be packaged as an
185007** SQLite database. The tables it expects to find are described in
185008** sqlite3rbu.h. Essentially, for each table xyz in the target database
185009** that the user wishes to write to, a corresponding data_xyz table is
185010** created in the RBU database and populated with one row for each row to
185011** update, insert or delete from the target table.
185012**
185013** The update proceeds in three stages:
185014**
185015** 1) The database is updated. The modified database pages are written
185016** to a *-oal file. A *-oal file is just like a *-wal file, except
185017** that it is named "<database>-oal" instead of "<database>-wal".
185018** Because regular SQLite clients do not look for file named
185019** "<database>-oal", they go on using the original database in
185020** rollback mode while the *-oal file is being generated.
185021**
185022** During this stage RBU does not update the database by writing
185023** directly to the target tables. Instead it creates "imposter"
185024** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
185025** to update each b-tree individually. All updates required by each
185026** b-tree are completed before moving on to the next, and all
185027** updates are done in sorted key order.
185028**
185029** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
185030** location using a call to rename(2). Before doing this the RBU
185031** module takes an EXCLUSIVE lock on the database file, ensuring
185032** that there are no other active readers.
185033**
185034** Once the EXCLUSIVE lock is released, any other database readers
185035** detect the new *-wal file and read the database in wal mode. At
185036** this point they see the new version of the database - including
185037** the updates made as part of the RBU update.
185038**
185039** 3) The new *-wal file is checkpointed. This proceeds in the same way
185040** as a regular database checkpoint, except that a single frame is
185041** checkpointed each time sqlite3rbu_step() is called. If the RBU
185042** handle is closed before the entire *-wal file is checkpointed,
185043** the checkpoint progress is saved in the RBU database and the
185044** checkpoint can be resumed by another RBU client at some point in
185045** the future.
185046**
185047** POTENTIAL PROBLEMS
185048**
185049** The rename() call might not be portable. And RBU is not currently
185050** syncing the directory after renaming the file.
185051**
185052** When state is saved, any commit to the *-oal file and the commit to
185053** the RBU update database are not atomic. So if the power fails at the
185054** wrong moment they might get out of sync. As the main database will be
185055** committed before the RBU update database this will likely either just
185056** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
185057** constraint violations).
185058**
185059** If some client does modify the target database mid RBU update, or some
185060** other error occurs, the RBU extension will keep throwing errors. It's
185061** not really clear how to get out of this state. The system could just
185062** by delete the RBU update database and *-oal file and have the device
185063** download the update again and start over.
185064**
185065** At present, for an UPDATE, both the new.* and old.* records are
185066** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
185067** fields are collected. This means we're probably writing a lot more
185068** data to disk when saving the state of an ongoing update to the RBU
185069** update database than is strictly necessary.
185070**
185071*/
185072
185073/* #include <assert.h> */
185074/* #include <string.h> */
185075/* #include <stdio.h> */
185076
185077/* #include "sqlite3.h" */
185078
185079#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
185080/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
185081/************** Begin file sqlite3rbu.h **************************************/
185082/*
185083** 2014 August 30
185084**
185085** The author disclaims copyright to this source code. In place of
185086** a legal notice, here is a blessing:
185087**
185088** May you do good and not evil.
185089** May you find forgiveness for yourself and forgive others.
185090** May you share freely, never taking more than you give.
185091**
185092*************************************************************************
185093**
185094** This file contains the public interface for the RBU extension.
185095*/
185096
185097/*
185098** SUMMARY
185099**
185100** Writing a transaction containing a large number of operations on
185101** b-tree indexes that are collectively larger than the available cache
185102** memory can be very inefficient.
185103**
185104** The problem is that in order to update a b-tree, the leaf page (at least)
185105** containing the entry being inserted or deleted must be modified. If the
185106** working set of leaves is larger than the available cache memory, then a
185107** single leaf that is modified more than once as part of the transaction
185108** may be loaded from or written to the persistent media multiple times.
185109** Additionally, because the index updates are likely to be applied in
185110** random order, access to pages within the database is also likely to be in
185111** random order, which is itself quite inefficient.
185112**
185113** One way to improve the situation is to sort the operations on each index
185114** by index key before applying them to the b-tree. This leads to an IO
185115** pattern that resembles a single linear scan through the index b-tree,
185116** and all but guarantees each modified leaf page is loaded and stored
185117** exactly once. SQLite uses this trick to improve the performance of
185118** CREATE INDEX commands. This extension allows it to be used to improve
185119** the performance of large transactions on existing databases.
185120**
185121** Additionally, this extension allows the work involved in writing the
185122** large transaction to be broken down into sub-transactions performed
185123** sequentially by separate processes. This is useful if the system cannot
185124** guarantee that a single update process will run for long enough to apply
185125** the entire update, for example because the update is being applied on a
185126** mobile device that is frequently rebooted. Even after the writer process
185127** has committed one or more sub-transactions, other database clients continue
185128** to read from the original database snapshot. In other words, partially
185129** applied transactions are not visible to other clients.
185130**
185131** "RBU" stands for "Resumable Bulk Update". As in a large database update
185132** transmitted via a wireless network to a mobile device. A transaction
185133** applied using this extension is hence refered to as an "RBU update".
185134**
185135**
185136** LIMITATIONS
185137**
185138** An "RBU update" transaction is subject to the following limitations:
185139**
185140** * The transaction must consist of INSERT, UPDATE and DELETE operations
185141** only.
185142**
185143** * INSERT statements may not use any default values.
185144**
185145** * UPDATE and DELETE statements must identify their target rows by
185146** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
185147** KEY fields may not be updated or deleted. If the table being written
185148** has no PRIMARY KEY, affected rows must be identified by rowid.
185149**
185150** * UPDATE statements may not modify PRIMARY KEY columns.
185151**
185152** * No triggers will be fired.
185153**
185154** * No foreign key violations are detected or reported.
185155**
185156** * CHECK constraints are not enforced.
185157**
185158** * No constraint handling mode except for "OR ROLLBACK" is supported.
185159**
185160**
185161** PREPARATION
185162**
185163** An "RBU update" is stored as a separate SQLite database. A database
185164** containing an RBU update is an "RBU database". For each table in the
185165** target database to be updated, the RBU database should contain a table
185166** named "data_<target name>" containing the same set of columns as the
185167** target table, and one more - "rbu_control". The data_% table should
185168** have no PRIMARY KEY or UNIQUE constraints, but each column should have
185169** the same type as the corresponding column in the target database.
185170** The "rbu_control" column should have no type at all. For example, if
185171** the target database contains:
185172**
185173** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
185174**
185175** Then the RBU database should contain:
185176**
185177** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
185178**
185179** The order of the columns in the data_% table does not matter.
185180**
185181** Instead of a regular table, the RBU database may also contain virtual
185182** tables or view named using the data_<target> naming scheme.
185183**
185184** Instead of the plain data_<target> naming scheme, RBU database tables
185185** may also be named data<integer>_<target>, where <integer> is any sequence
185186** of zero or more numeric characters (0-9). This can be significant because
185187** tables within the RBU database are always processed in order sorted by
185188** name. By judicious selection of the <integer> portion of the names
185189** of the RBU tables the user can therefore control the order in which they
185190** are processed. This can be useful, for example, to ensure that "external
185191** content" FTS4 tables are updated before their underlying content tables.
185192**
185193** If the target database table is a virtual table or a table that has no
185194** PRIMARY KEY declaration, the data_% table must also contain a column
185195** named "rbu_rowid". This column is mapped to the tables implicit primary
185196** key column - "rowid". Virtual tables for which the "rowid" column does
185197** not function like a primary key value cannot be updated using RBU. For
185198** example, if the target db contains either of the following:
185199**
185200** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
185201** CREATE TABLE x1(a, b)
185202**
185203** then the RBU database should contain:
185204**
185205** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
185206**
185207** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
185208** target table must be present in the input table. For virtual tables,
185209** hidden columns are optional - they are updated by RBU if present in
185210** the input table, or not otherwise. For example, to write to an fts4
185211** table with a hidden languageid column such as:
185212**
185213** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
185214**
185215** Either of the following input table schemas may be used:
185216**
185217** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
185218** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
185219**
185220** For each row to INSERT into the target database as part of the RBU
185221** update, the corresponding data_% table should contain a single record
185222** with the "rbu_control" column set to contain integer value 0. The
185223** other columns should be set to the values that make up the new record
185224** to insert.
185225**
185226** If the target database table has an INTEGER PRIMARY KEY, it is not
185227** possible to insert a NULL value into the IPK column. Attempting to
185228** do so results in an SQLITE_MISMATCH error.
185229**
185230** For each row to DELETE from the target database as part of the RBU
185231** update, the corresponding data_% table should contain a single record
185232** with the "rbu_control" column set to contain integer value 1. The
185233** real primary key values of the row to delete should be stored in the
185234** corresponding columns of the data_% table. The values stored in the
185235** other columns are not used.
185236**
185237** For each row to UPDATE from the target database as part of the RBU
185238** update, the corresponding data_% table should contain a single record
185239** with the "rbu_control" column set to contain a value of type text.
185240** The real primary key values identifying the row to update should be
185241** stored in the corresponding columns of the data_% table row, as should
185242** the new values of all columns being update. The text value in the
185243** "rbu_control" column must contain the same number of characters as
185244** there are columns in the target database table, and must consist entirely
185245** of 'x' and '.' characters (or in some special cases 'd' - see below). For
185246** each column that is being updated, the corresponding character is set to
185247** 'x'. For those that remain as they are, the corresponding character of the
185248** rbu_control value should be set to '.'. For example, given the tables
185249** above, the update statement:
185250**
185251** UPDATE t1 SET c = 'usa' WHERE a = 4;
185252**
185253** is represented by the data_t1 row created by:
185254**
185255** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
185256**
185257** Instead of an 'x' character, characters of the rbu_control value specified
185258** for UPDATEs may also be set to 'd'. In this case, instead of updating the
185259** target table with the value stored in the corresponding data_% column, the
185260** user-defined SQL function "rbu_delta()" is invoked and the result stored in
185261** the target table column. rbu_delta() is invoked with two arguments - the
185262** original value currently stored in the target table column and the
185263** value specified in the data_xxx table.
185264**
185265** For example, this row:
185266**
185267** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
185268**
185269** is similar to an UPDATE statement such as:
185270**
185271** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
185272**
185273** Finally, if an 'f' character appears in place of a 'd' or 's' in an
185274** ota_control string, the contents of the data_xxx table column is assumed
185275** to be a "fossil delta" - a patch to be applied to a blob value in the
185276** format used by the fossil source-code management system. In this case
185277** the existing value within the target database table must be of type BLOB.
185278** It is replaced by the result of applying the specified fossil delta to
185279** itself.
185280**
185281** If the target database table is a virtual table or a table with no PRIMARY
185282** KEY, the rbu_control value should not include a character corresponding
185283** to the rbu_rowid value. For example, this:
185284**
185285** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
185286** VALUES(NULL, 'usa', 12, '.x');
185287**
185288** causes a result similar to:
185289**
185290** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
185291**
185292** The data_xxx tables themselves should have no PRIMARY KEY declarations.
185293** However, RBU is more efficient if reading the rows in from each data_xxx
185294** table in "rowid" order is roughly the same as reading them sorted by
185295** the PRIMARY KEY of the corresponding target database table. In other
185296** words, rows should be sorted using the destination table PRIMARY KEY
185297** fields before they are inserted into the data_xxx tables.
185298**
185299** USAGE
185300**
185301** The API declared below allows an application to apply an RBU update
185302** stored on disk to an existing target database. Essentially, the
185303** application:
185304**
185305** 1) Opens an RBU handle using the sqlite3rbu_open() function.
185306**
185307** 2) Registers any required virtual table modules with the database
185308** handle returned by sqlite3rbu_db(). Also, if required, register
185309** the rbu_delta() implementation.
185310**
185311** 3) Calls the sqlite3rbu_step() function one or more times on
185312** the new handle. Each call to sqlite3rbu_step() performs a single
185313** b-tree operation, so thousands of calls may be required to apply
185314** a complete update.
185315**
185316** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
185317** sqlite3rbu_step() has been called enough times to completely
185318** apply the update to the target database, then the RBU database
185319** is marked as fully applied. Otherwise, the state of the RBU
185320** update application is saved in the RBU database for later
185321** resumption.
185322**
185323** See comments below for more detail on APIs.
185324**
185325** If an update is only partially applied to the target database by the
185326** time sqlite3rbu_close() is called, various state information is saved
185327** within the RBU database. This allows subsequent processes to automatically
185328** resume the RBU update from where it left off.
185329**
185330** To remove all RBU extension state information, returning an RBU database
185331** to its original contents, it is sufficient to drop all tables that begin
185332** with the prefix "rbu_"
185333**
185334** DATABASE LOCKING
185335**
185336** An RBU update may not be applied to a database in WAL mode. Attempting
185337** to do so is an error (SQLITE_ERROR).
185338**
185339** While an RBU handle is open, a SHARED lock may be held on the target
185340** database file. This means it is possible for other clients to read the
185341** database, but not to write it.
185342**
185343** If an RBU update is started and then suspended before it is completed,
185344** then an external client writes to the database, then attempting to resume
185345** the suspended RBU update is also an error (SQLITE_BUSY).
185346*/
185347
185348#ifndef _SQLITE3RBU_H
185349#define _SQLITE3RBU_H
185350
185351/* #include "sqlite3.h" ** Required for error code definitions ** */
185352
185353#if 0
185354extern "C" {
185355#endif
185356
185357typedef struct sqlite3rbu sqlite3rbu;
185358
185359/*
185360** Open an RBU handle.
185361**
185362** Argument zTarget is the path to the target database. Argument zRbu is
185363** the path to the RBU database. Each call to this function must be matched
185364** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
185365** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
185366** or zRbu begin with "file:", it will be interpreted as an SQLite
185367** database URI, not a regular file name.
185368**
185369** If the zState argument is passed a NULL value, the RBU extension stores
185370** the current state of the update (how many rows have been updated, which
185371** indexes are yet to be updated etc.) within the RBU database itself. This
185372** can be convenient, as it means that the RBU application does not need to
185373** organize removing a separate state file after the update is concluded.
185374** Or, if zState is non-NULL, it must be a path to a database file in which
185375** the RBU extension can store the state of the update.
185376**
185377** When resuming an RBU update, the zState argument must be passed the same
185378** value as when the RBU update was started.
185379**
185380** Once the RBU update is finished, the RBU extension does not
185381** automatically remove any zState database file, even if it created it.
185382**
185383** By default, RBU uses the default VFS to access the files on disk. To
185384** use a VFS other than the default, an SQLite "file:" URI containing a
185385** "vfs=..." option may be passed as the zTarget option.
185386**
185387** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
185388** SQLite's built-in VFSs, including the multiplexor VFS. However it does
185389** not work out of the box with zipvfs. Refer to the comment describing
185390** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
185391*/
185392SQLITE_API sqlite3rbu *sqlite3rbu_open(
185393 const char *zTarget,
185394 const char *zRbu,
185395 const char *zState
185396);
185397
185398/*
185399** Open an RBU handle to perform an RBU vacuum on database file zTarget.
185400** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
185401** that it can be suspended and resumed like an RBU update.
185402**
185403** The second argument to this function identifies a database in which
185404** to store the state of the RBU vacuum operation if it is suspended. The
185405** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
185406** operation, the state database should either not exist or be empty
185407** (contain no tables). If an RBU vacuum is suspended by calling
185408** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
185409** returned SQLITE_DONE, the vacuum state is stored in the state database.
185410** The vacuum can be resumed by calling this function to open a new RBU
185411** handle specifying the same target and state databases.
185412**
185413** If the second argument passed to this function is NULL, then the
185414** name of the state database is "<database>-vacuum", where <database>
185415** is the name of the target database file. In this case, on UNIX, if the
185416** state database is not already present in the file-system, it is created
185417** with the same permissions as the target db is made.
185418**
185419** This function does not delete the state database after an RBU vacuum
185420** is completed, even if it created it. However, if the call to
185421** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
185422** of the state tables within the state database are zeroed. This way,
185423** the next call to sqlite3rbu_vacuum() opens a handle that starts a
185424** new RBU vacuum operation.
185425**
185426** As with sqlite3rbu_open(), Zipvfs users should rever to the comment
185427** describing the sqlite3rbu_create_vfs() API function below for
185428** a description of the complications associated with using RBU with
185429** zipvfs databases.
185430*/
185431SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
185432 const char *zTarget,
185433 const char *zState
185434);
185435
185436/*
185437** Configure a limit for the amount of temp space that may be used by
185438** the RBU handle passed as the first argument. The new limit is specified
185439** in bytes by the second parameter. If it is positive, the limit is updated.
185440** If the second parameter to this function is passed zero, then the limit
185441** is removed entirely. If the second parameter is negative, the limit is
185442** not modified (this is useful for querying the current limit).
185443**
185444** In all cases the returned value is the current limit in bytes (zero
185445** indicates unlimited).
185446**
185447** If the temp space limit is exceeded during operation, an SQLITE_FULL
185448** error is returned.
185449*/
185450SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu*, sqlite3_int64);
185451
185452/*
185453** Return the current amount of temp file space, in bytes, currently used by
185454** the RBU handle passed as the only argument.
185455*/
185456SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu*);
185457
185458/*
185459** Internally, each RBU connection uses a separate SQLite database
185460** connection to access the target and rbu update databases. This
185461** API allows the application direct access to these database handles.
185462**
185463** The first argument passed to this function must be a valid, open, RBU
185464** handle. The second argument should be passed zero to access the target
185465** database handle, or non-zero to access the rbu update database handle.
185466** Accessing the underlying database handles may be useful in the
185467** following scenarios:
185468**
185469** * If any target tables are virtual tables, it may be necessary to
185470** call sqlite3_create_module() on the target database handle to
185471** register the required virtual table implementations.
185472**
185473** * If the data_xxx tables in the RBU source database are virtual
185474** tables, the application may need to call sqlite3_create_module() on
185475** the rbu update db handle to any required virtual table
185476** implementations.
185477**
185478** * If the application uses the "rbu_delta()" feature described above,
185479** it must use sqlite3_create_function() or similar to register the
185480** rbu_delta() implementation with the target database handle.
185481**
185482** If an error has occurred, either while opening or stepping the RBU object,
185483** this function may return NULL. The error code and message may be collected
185484** when sqlite3rbu_close() is called.
185485**
185486** Database handles returned by this function remain valid until the next
185487** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db().
185488*/
185489SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu*, int bRbu);
185490
185491/*
185492** Do some work towards applying the RBU update to the target db.
185493**
185494** Return SQLITE_DONE if the update has been completely applied, or
185495** SQLITE_OK if no error occurs but there remains work to do to apply
185496** the RBU update. If an error does occur, some other error code is
185497** returned.
185498**
185499** Once a call to sqlite3rbu_step() has returned a value other than
185500** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
185501** that immediately return the same value.
185502*/
185503SQLITE_API int sqlite3rbu_step(sqlite3rbu *pRbu);
185504
185505/*
185506** Force RBU to save its state to disk.
185507**
185508** If a power failure or application crash occurs during an update, following
185509** system recovery RBU may resume the update from the point at which the state
185510** was last saved. In other words, from the most recent successful call to
185511** sqlite3rbu_close() or this function.
185512**
185513** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
185514*/
185515SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *pRbu);
185516
185517/*
185518** Close an RBU handle.
185519**
185520** If the RBU update has been completely applied, mark the RBU database
185521** as fully applied. Otherwise, assuming no error has occurred, save the
185522** current state of the RBU update appliation to the RBU database.
185523**
185524** If an error has already occurred as part of an sqlite3rbu_step()
185525** or sqlite3rbu_open() call, or if one occurs within this function, an
185526** SQLite error code is returned. Additionally, if pzErrmsg is not NULL,
185527** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
185528** English language error message. It is the responsibility of the caller to
185529** eventually free any such buffer using sqlite3_free().
185530**
185531** Otherwise, if no error occurs, this function returns SQLITE_OK if the
185532** update has been partially applied, or SQLITE_DONE if it has been
185533** completely applied.
185534*/
185535SQLITE_API int sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
185536
185537/*
185538** Return the total number of key-value operations (inserts, deletes or
185539** updates) that have been performed on the target database since the
185540** current RBU update was started.
185541*/
185542SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu);
185543
185544/*
185545** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100)
185546** progress indications for the two stages of an RBU update. This API may
185547** be useful for driving GUI progress indicators and similar.
185548**
185549** An RBU update is divided into two stages:
185550**
185551** * Stage 1, in which changes are accumulated in an oal/wal file, and
185552** * Stage 2, in which the contents of the wal file are copied into the
185553** main database.
185554**
185555** The update is visible to non-RBU clients during stage 2. During stage 1
185556** non-RBU reader clients may see the original database.
185557**
185558** If this API is called during stage 2 of the update, output variable
185559** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo)
185560** to a value between 0 and 10000 to indicate the permyriadage progress of
185561** stage 2. A value of 5000 indicates that stage 2 is half finished,
185562** 9000 indicates that it is 90% finished, and so on.
185563**
185564** If this API is called during stage 1 of the update, output variable
185565** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The
185566** value to which (*pnOne) is set depends on whether or not the RBU
185567** database contains an "rbu_count" table. The rbu_count table, if it
185568** exists, must contain the same columns as the following:
185569**
185570** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
185571**
185572** There must be one row in the table for each source (data_xxx) table within
185573** the RBU database. The 'tbl' column should contain the name of the source
185574** table. The 'cnt' column should contain the number of rows within the
185575** source table.
185576**
185577** If the rbu_count table is present and populated correctly and this
185578** API is called during stage 1, the *pnOne output variable is set to the
185579** permyriadage progress of the same stage. If the rbu_count table does
185580** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
185581** table exists but is not correctly populated, the value of the *pnOne
185582** output variable during stage 1 is undefined.
185583*/
185584SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int*pnTwo);
185585
185586/*
185587** Obtain an indication as to the current stage of an RBU update or vacuum.
185588** This function always returns one of the SQLITE_RBU_STATE_XXX constants
185589** defined in this file. Return values should be interpreted as follows:
185590**
185591** SQLITE_RBU_STATE_OAL:
185592** RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
185593** may either add further data to the *-oal file, or compute data that will
185594** be added by a subsequent call.
185595**
185596** SQLITE_RBU_STATE_MOVE:
185597** RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
185598** will move the *-oal file to the equivalent *-wal path. If the current
185599** operation is an RBU update, then the updated version of the database
185600** file will become visible to ordinary SQLite clients following the next
185601** call to sqlite3rbu_step().
185602**
185603** SQLITE_RBU_STATE_CHECKPOINT:
185604** RBU is currently performing an incremental checkpoint. The next call to
185605** sqlite3rbu_step() will copy a page of data from the *-wal file into
185606** the target database file.
185607**
185608** SQLITE_RBU_STATE_DONE:
185609** The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
185610** will immediately return SQLITE_DONE.
185611**
185612** SQLITE_RBU_STATE_ERROR:
185613** An error has occurred. Any subsequent calls to sqlite3rbu_step() will
185614** immediately return the SQLite error code associated with the error.
185615*/
185616#define SQLITE_RBU_STATE_OAL 1
185617#define SQLITE_RBU_STATE_MOVE 2
185618#define SQLITE_RBU_STATE_CHECKPOINT 3
185619#define SQLITE_RBU_STATE_DONE 4
185620#define SQLITE_RBU_STATE_ERROR 5
185621
185622SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
185623
185624/*
185625** Create an RBU VFS named zName that accesses the underlying file-system
185626** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
185627** then the new RBU VFS uses the default system VFS to access the file-system.
185628** The new object is registered as a non-default VFS with SQLite before
185629** returning.
185630**
185631** Part of the RBU implementation uses a custom VFS object. Usually, this
185632** object is created and deleted automatically by RBU.
185633**
185634** The exception is for applications that also use zipvfs. In this case,
185635** the custom VFS must be explicitly created by the user before the RBU
185636** handle is opened. The RBU VFS should be installed so that the zipvfs
185637** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
185638** (for example multiplexor) to access the file-system. For example,
185639** to assemble an RBU enabled VFS stack that uses both zipvfs and
185640** multiplexor (error checking omitted):
185641**
185642** // Create a VFS named "multiplex" (not the default).
185643** sqlite3_multiplex_initialize(0, 0);
185644**
185645** // Create an rbu VFS named "rbu" that uses multiplexor. If the
185646** // second argument were replaced with NULL, the "rbu" VFS would
185647** // access the file-system via the system default VFS, bypassing the
185648** // multiplexor.
185649** sqlite3rbu_create_vfs("rbu", "multiplex");
185650**
185651** // Create a zipvfs VFS named "zipvfs" that uses rbu.
185652** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
185653**
185654** // Make zipvfs the default VFS.
185655** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
185656**
185657** Because the default VFS created above includes a RBU functionality, it
185658** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
185659** that does not include the RBU layer results in an error.
185660**
185661** The overhead of adding the "rbu" VFS to the system is negligible for
185662** non-RBU users. There is no harm in an application accessing the
185663** file-system via "rbu" all the time, even if it only uses RBU functionality
185664** occasionally.
185665*/
185666SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent);
185667
185668/*
185669** Deregister and destroy an RBU vfs created by an earlier call to
185670** sqlite3rbu_create_vfs().
185671**
185672** VFS objects are not reference counted. If a VFS object is destroyed
185673** before all database handles that use it have been closed, the results
185674** are undefined.
185675*/
185676SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName);
185677
185678#if 0
185679} /* end of the 'extern "C"' block */
185680#endif
185681
185682#endif /* _SQLITE3RBU_H */
185683
185684/************** End of sqlite3rbu.h ******************************************/
185685/************** Continuing where we left off in sqlite3rbu.c *****************/
185686
185687#if defined(_WIN32_WCE)
185688/* #include "windows.h" */
185689#endif
185690
185691/* Maximum number of prepared UPDATE statements held by this module */
185692#define SQLITE_RBU_UPDATE_CACHESIZE 16
185693
185694/* Delta checksums disabled by default. Compile with -DRBU_ENABLE_DELTA_CKSUM
185695** to enable checksum verification.
185696*/
185697#ifndef RBU_ENABLE_DELTA_CKSUM
185698# define RBU_ENABLE_DELTA_CKSUM 0
185699#endif
185700
185701/*
185702** Swap two objects of type TYPE.
185703*/
185704#if !defined(SQLITE_AMALGAMATION)
185705# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
185706#endif
185707
185708/*
185709** The rbu_state table is used to save the state of a partially applied
185710** update so that it can be resumed later. The table consists of integer
185711** keys mapped to values as follows:
185712**
185713** RBU_STATE_STAGE:
185714** May be set to integer values 1, 2, 4 or 5. As follows:
185715** 1: the *-rbu file is currently under construction.
185716** 2: the *-rbu file has been constructed, but not yet moved
185717** to the *-wal path.
185718** 4: the checkpoint is underway.
185719** 5: the rbu update has been checkpointed.
185720**
185721** RBU_STATE_TBL:
185722** Only valid if STAGE==1. The target database name of the table
185723** currently being written.
185724**
185725** RBU_STATE_IDX:
185726** Only valid if STAGE==1. The target database name of the index
185727** currently being written, or NULL if the main table is currently being
185728** updated.
185729**
185730** RBU_STATE_ROW:
185731** Only valid if STAGE==1. Number of rows already processed for the current
185732** table/index.
185733**
185734** RBU_STATE_PROGRESS:
185735** Trbul number of sqlite3rbu_step() calls made so far as part of this
185736** rbu update.
185737**
185738** RBU_STATE_CKPT:
185739** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
185740** header created by recovering the *-wal file. This is used to detect
185741** cases when another client appends frames to the *-wal file in the
185742** middle of an incremental checkpoint (an incremental checkpoint cannot
185743** be continued if this happens).
185744**
185745** RBU_STATE_COOKIE:
185746** Valid if STAGE==1. The current change-counter cookie value in the
185747** target db file.
185748**
185749** RBU_STATE_OALSZ:
185750** Valid if STAGE==1. The size in bytes of the *-oal file.
185751**
185752** RBU_STATE_DATATBL:
185753** Only valid if STAGE==1. The RBU database name of the table
185754** currently being read.
185755*/
185756#define RBU_STATE_STAGE 1
185757#define RBU_STATE_TBL 2
185758#define RBU_STATE_IDX 3
185759#define RBU_STATE_ROW 4
185760#define RBU_STATE_PROGRESS 5
185761#define RBU_STATE_CKPT 6
185762#define RBU_STATE_COOKIE 7
185763#define RBU_STATE_OALSZ 8
185764#define RBU_STATE_PHASEONESTEP 9
185765#define RBU_STATE_DATATBL 10
185766
185767#define RBU_STAGE_OAL 1
185768#define RBU_STAGE_MOVE 2
185769#define RBU_STAGE_CAPTURE 3
185770#define RBU_STAGE_CKPT 4
185771#define RBU_STAGE_DONE 5
185772
185773
185774#define RBU_CREATE_STATE \
185775 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
185776
185777typedef struct RbuFrame RbuFrame;
185778typedef struct RbuObjIter RbuObjIter;
185779typedef struct RbuState RbuState;
185780typedef struct rbu_vfs rbu_vfs;
185781typedef struct rbu_file rbu_file;
185782typedef struct RbuUpdateStmt RbuUpdateStmt;
185783
185784#if !defined(SQLITE_AMALGAMATION)
185785typedef unsigned int u32;
185786typedef unsigned short u16;
185787typedef unsigned char u8;
185788typedef sqlite3_int64 i64;
185789#endif
185790
185791/*
185792** These values must match the values defined in wal.c for the equivalent
185793** locks. These are not magic numbers as they are part of the SQLite file
185794** format.
185795*/
185796#define WAL_LOCK_WRITE 0
185797#define WAL_LOCK_CKPT 1
185798#define WAL_LOCK_READ0 3
185799
185800#define SQLITE_FCNTL_RBUCNT 5149216
185801
185802/*
185803** A structure to store values read from the rbu_state table in memory.
185804*/
185805struct RbuState {
185806 int eStage;
185807 char *zTbl;
185808 char *zDataTbl;
185809 char *zIdx;
185810 i64 iWalCksum;
185811 int nRow;
185812 i64 nProgress;
185813 u32 iCookie;
185814 i64 iOalSz;
185815 i64 nPhaseOneStep;
185816};
185817
185818struct RbuUpdateStmt {
185819 char *zMask; /* Copy of update mask used with pUpdate */
185820 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
185821 RbuUpdateStmt *pNext;
185822};
185823
185824/*
185825** An iterator of this type is used to iterate through all objects in
185826** the target database that require updating. For each such table, the
185827** iterator visits, in order:
185828**
185829** * the table itself,
185830** * each index of the table (zero or more points to visit), and
185831** * a special "cleanup table" state.
185832**
185833** abIndexed:
185834** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
185835** it points to an array of flags nTblCol elements in size. The flag is
185836** set for each column that is either a part of the PK or a part of an
185837** index. Or clear otherwise.
185838**
185839*/
185840struct RbuObjIter {
185841 sqlite3_stmt *pTblIter; /* Iterate through tables */
185842 sqlite3_stmt *pIdxIter; /* Index iterator */
185843 int nTblCol; /* Size of azTblCol[] array */
185844 char **azTblCol; /* Array of unquoted target column names */
185845 char **azTblType; /* Array of target column types */
185846 int *aiSrcOrder; /* src table col -> target table col */
185847 u8 *abTblPk; /* Array of flags, set on target PK columns */
185848 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
185849 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
185850 int eType; /* Table type - an RBU_PK_XXX value */
185851
185852 /* Output variables. zTbl==0 implies EOF. */
185853 int bCleanup; /* True in "cleanup" state */
185854 const char *zTbl; /* Name of target db table */
185855 const char *zDataTbl; /* Name of rbu db table (or null) */
185856 const char *zIdx; /* Name of target db index (or null) */
185857 int iTnum; /* Root page of current object */
185858 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
185859 int bUnique; /* Current index is unique */
185860 int nIndex; /* Number of aux. indexes on table zTbl */
185861
185862 /* Statements created by rbuObjIterPrepareAll() */
185863 int nCol; /* Number of columns in current object */
185864 sqlite3_stmt *pSelect; /* Source data */
185865 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
185866 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
185867 sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zDataTbl */
185868
185869 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
185870 RbuUpdateStmt *pRbuUpdate;
185871};
185872
185873/*
185874** Values for RbuObjIter.eType
185875**
185876** 0: Table does not exist (error)
185877** 1: Table has an implicit rowid.
185878** 2: Table has an explicit IPK column.
185879** 3: Table has an external PK index.
185880** 4: Table is WITHOUT ROWID.
185881** 5: Table is a virtual table.
185882*/
185883#define RBU_PK_NOTABLE 0
185884#define RBU_PK_NONE 1
185885#define RBU_PK_IPK 2
185886#define RBU_PK_EXTERNAL 3
185887#define RBU_PK_WITHOUT_ROWID 4
185888#define RBU_PK_VTAB 5
185889
185890
185891/*
185892** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
185893** one of the following operations.
185894*/
185895#define RBU_INSERT 1 /* Insert on a main table b-tree */
185896#define RBU_DELETE 2 /* Delete a row from a main table b-tree */
185897#define RBU_REPLACE 3 /* Delete and then insert a row */
185898#define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
185899#define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
185900
185901#define RBU_UPDATE 6 /* Update a row in a main table b-tree */
185902
185903/*
185904** A single step of an incremental checkpoint - frame iWalFrame of the wal
185905** file should be copied to page iDbPage of the database file.
185906*/
185907struct RbuFrame {
185908 u32 iDbPage;
185909 u32 iWalFrame;
185910};
185911
185912/*
185913** RBU handle.
185914**
185915** nPhaseOneStep:
185916** If the RBU database contains an rbu_count table, this value is set to
185917** a running estimate of the number of b-tree operations required to
185918** finish populating the *-oal file. This allows the sqlite3_bp_progress()
185919** API to calculate the permyriadage progress of populating the *-oal file
185920** using the formula:
185921**
185922** permyriadage = (10000 * nProgress) / nPhaseOneStep
185923**
185924** nPhaseOneStep is initialized to the sum of:
185925**
185926** nRow * (nIndex + 1)
185927**
185928** for all source tables in the RBU database, where nRow is the number
185929** of rows in the source table and nIndex the number of indexes on the
185930** corresponding target database table.
185931**
185932** This estimate is accurate if the RBU update consists entirely of
185933** INSERT operations. However, it is inaccurate if:
185934**
185935** * the RBU update contains any UPDATE operations. If the PK specified
185936** for an UPDATE operation does not exist in the target table, then
185937** no b-tree operations are required on index b-trees. Or if the
185938** specified PK does exist, then (nIndex*2) such operations are
185939** required (one delete and one insert on each index b-tree).
185940**
185941** * the RBU update contains any DELETE operations for which the specified
185942** PK does not exist. In this case no operations are required on index
185943** b-trees.
185944**
185945** * the RBU update contains REPLACE operations. These are similar to
185946** UPDATE operations.
185947**
185948** nPhaseOneStep is updated to account for the conditions above during the
185949** first pass of each source table. The updated nPhaseOneStep value is
185950** stored in the rbu_state table if the RBU update is suspended.
185951*/
185952struct sqlite3rbu {
185953 int eStage; /* Value of RBU_STATE_STAGE field */
185954 sqlite3 *dbMain; /* target database handle */
185955 sqlite3 *dbRbu; /* rbu database handle */
185956 char *zTarget; /* Path to target db */
185957 char *zRbu; /* Path to rbu db */
185958 char *zState; /* Path to state db (or NULL if zRbu) */
185959 char zStateDb[5]; /* Db name for state ("stat" or "main") */
185960 int rc; /* Value returned by last rbu_step() call */
185961 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
185962 int nStep; /* Rows processed for current object */
185963 int nProgress; /* Rows processed for all objects */
185964 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
185965 const char *zVfsName; /* Name of automatically created rbu vfs */
185966 rbu_file *pTargetFd; /* File handle open on target db */
185967 int nPagePerSector; /* Pages per sector for pTargetFd */
185968 i64 iOalSz;
185969 i64 nPhaseOneStep;
185970
185971 /* The following state variables are used as part of the incremental
185972 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
185973 ** function rbuSetupCheckpoint() for details. */
185974 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
185975 u32 mLock;
185976 int nFrame; /* Entries in aFrame[] array */
185977 int nFrameAlloc; /* Allocated size of aFrame[] array */
185978 RbuFrame *aFrame;
185979 int pgsz;
185980 u8 *aBuf;
185981 i64 iWalCksum;
185982 i64 szTemp; /* Current size of all temp files in use */
185983 i64 szTempLimit; /* Total size limit for temp files */
185984
185985 /* Used in RBU vacuum mode only */
185986 int nRbu; /* Number of RBU VFS in the stack */
185987 rbu_file *pRbuFd; /* Fd for main db of dbRbu */
185988};
185989
185990/*
185991** An rbu VFS is implemented using an instance of this structure.
185992**
185993** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
185994** It is NULL for RBU VFS objects created explicitly using
185995** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
185996** space used by the RBU handle.
185997*/
185998struct rbu_vfs {
185999 sqlite3_vfs base; /* rbu VFS shim methods */
186000 sqlite3_vfs *pRealVfs; /* Underlying VFS */
186001 sqlite3_mutex *mutex; /* Mutex to protect pMain */
186002 sqlite3rbu *pRbu; /* Owner RBU object */
186003 rbu_file *pMain; /* Linked list of main db files */
186004};
186005
186006/*
186007** Each file opened by an rbu VFS is represented by an instance of
186008** the following structure.
186009**
186010** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
186011** "sz" is set to the current size of the database file.
186012*/
186013struct rbu_file {
186014 sqlite3_file base; /* sqlite3_file methods */
186015 sqlite3_file *pReal; /* Underlying file handle */
186016 rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
186017 sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
186018 i64 sz; /* Size of file in bytes (temp only) */
186019
186020 int openFlags; /* Flags this file was opened with */
186021 u32 iCookie; /* Cookie value for main db files */
186022 u8 iWriteVer; /* "write-version" value for main db files */
186023 u8 bNolock; /* True to fail EXCLUSIVE locks */
186024
186025 int nShm; /* Number of entries in apShm[] array */
186026 char **apShm; /* Array of mmap'd *-shm regions */
186027 char *zDel; /* Delete this when closing file */
186028
186029 const char *zWal; /* Wal filename for this main db file */
186030 rbu_file *pWalFd; /* Wal file descriptor for this main db */
186031 rbu_file *pMainNext; /* Next MAIN_DB file */
186032};
186033
186034/*
186035** True for an RBU vacuum handle, or false otherwise.
186036*/
186037#define rbuIsVacuum(p) ((p)->zTarget==0)
186038
186039
186040/*************************************************************************
186041** The following three functions, found below:
186042**
186043** rbuDeltaGetInt()
186044** rbuDeltaChecksum()
186045** rbuDeltaApply()
186046**
186047** are lifted from the fossil source code (http://fossil-scm.org). They
186048** are used to implement the scalar SQL function rbu_fossil_delta().
186049*/
186050
186051/*
186052** Read bytes from *pz and convert them into a positive integer. When
186053** finished, leave *pz pointing to the first character past the end of
186054** the integer. The *pLen parameter holds the length of the string
186055** in *pz and is decremented once for each character in the integer.
186056*/
186057static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
186058 static const signed char zValue[] = {
186059 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
186060 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
186061 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
186062 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
186063 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
186064 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
186065 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
186066 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
186067 };
186068 unsigned int v = 0;
186069 int c;
186070 unsigned char *z = (unsigned char*)*pz;
186071 unsigned char *zStart = z;
186072 while( (c = zValue[0x7f&*(z++)])>=0 ){
186073 v = (v<<6) + c;
186074 }
186075 z--;
186076 *pLen -= z - zStart;
186077 *pz = (char*)z;
186078 return v;
186079}
186080
186081#if RBU_ENABLE_DELTA_CKSUM
186082/*
186083** Compute a 32-bit checksum on the N-byte buffer. Return the result.
186084*/
186085static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
186086 const unsigned char *z = (const unsigned char *)zIn;
186087 unsigned sum0 = 0;
186088 unsigned sum1 = 0;
186089 unsigned sum2 = 0;
186090 unsigned sum3 = 0;
186091 while(N >= 16){
186092 sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
186093 sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
186094 sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
186095 sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
186096 z += 16;
186097 N -= 16;
186098 }
186099 while(N >= 4){
186100 sum0 += z[0];
186101 sum1 += z[1];
186102 sum2 += z[2];
186103 sum3 += z[3];
186104 z += 4;
186105 N -= 4;
186106 }
186107 sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
186108 switch(N){
186109 case 3: sum3 += (z[2] << 8);
186110 case 2: sum3 += (z[1] << 16);
186111 case 1: sum3 += (z[0] << 24);
186112 default: ;
186113 }
186114 return sum3;
186115}
186116#endif
186117
186118/*
186119** Apply a delta.
186120**
186121** The output buffer should be big enough to hold the whole output
186122** file and a NUL terminator at the end. The delta_output_size()
186123** routine will determine this size for you.
186124**
186125** The delta string should be null-terminated. But the delta string
186126** may contain embedded NUL characters (if the input and output are
186127** binary files) so we also have to pass in the length of the delta in
186128** the lenDelta parameter.
186129**
186130** This function returns the size of the output file in bytes (excluding
186131** the final NUL terminator character). Except, if the delta string is
186132** malformed or intended for use with a source file other than zSrc,
186133** then this routine returns -1.
186134**
186135** Refer to the delta_create() documentation above for a description
186136** of the delta file format.
186137*/
186138static int rbuDeltaApply(
186139 const char *zSrc, /* The source or pattern file */
186140 int lenSrc, /* Length of the source file */
186141 const char *zDelta, /* Delta to apply to the pattern */
186142 int lenDelta, /* Length of the delta */
186143 char *zOut /* Write the output into this preallocated buffer */
186144){
186145 unsigned int limit;
186146 unsigned int total = 0;
186147#if RBU_ENABLE_DELTA_CKSUM
186148 char *zOrigOut = zOut;
186149#endif
186150
186151 limit = rbuDeltaGetInt(&zDelta, &lenDelta);
186152 if( *zDelta!='\n' ){
186153 /* ERROR: size integer not terminated by "\n" */
186154 return -1;
186155 }
186156 zDelta++; lenDelta--;
186157 while( *zDelta && lenDelta>0 ){
186158 unsigned int cnt, ofst;
186159 cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
186160 switch( zDelta[0] ){
186161 case '@': {
186162 zDelta++; lenDelta--;
186163 ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
186164 if( lenDelta>0 && zDelta[0]!=',' ){
186165 /* ERROR: copy command not terminated by ',' */
186166 return -1;
186167 }
186168 zDelta++; lenDelta--;
186169 total += cnt;
186170 if( total>limit ){
186171 /* ERROR: copy exceeds output file size */
186172 return -1;
186173 }
186174 if( (int)(ofst+cnt) > lenSrc ){
186175 /* ERROR: copy extends past end of input */
186176 return -1;
186177 }
186178 memcpy(zOut, &zSrc[ofst], cnt);
186179 zOut += cnt;
186180 break;
186181 }
186182 case ':': {
186183 zDelta++; lenDelta--;
186184 total += cnt;
186185 if( total>limit ){
186186 /* ERROR: insert command gives an output larger than predicted */
186187 return -1;
186188 }
186189 if( (int)cnt>lenDelta ){
186190 /* ERROR: insert count exceeds size of delta */
186191 return -1;
186192 }
186193 memcpy(zOut, zDelta, cnt);
186194 zOut += cnt;
186195 zDelta += cnt;
186196 lenDelta -= cnt;
186197 break;
186198 }
186199 case ';': {
186200 zDelta++; lenDelta--;
186201 zOut[0] = 0;
186202#if RBU_ENABLE_DELTA_CKSUM
186203 if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
186204 /* ERROR: bad checksum */
186205 return -1;
186206 }
186207#endif
186208 if( total!=limit ){
186209 /* ERROR: generated size does not match predicted size */
186210 return -1;
186211 }
186212 return total;
186213 }
186214 default: {
186215 /* ERROR: unknown delta operator */
186216 return -1;
186217 }
186218 }
186219 }
186220 /* ERROR: unterminated delta */
186221 return -1;
186222}
186223
186224static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
186225 int size;
186226 size = rbuDeltaGetInt(&zDelta, &lenDelta);
186227 if( *zDelta!='\n' ){
186228 /* ERROR: size integer not terminated by "\n" */
186229 return -1;
186230 }
186231 return size;
186232}
186233
186234/*
186235** End of code taken from fossil.
186236*************************************************************************/
186237
186238/*
186239** Implementation of SQL scalar function rbu_fossil_delta().
186240**
186241** This function applies a fossil delta patch to a blob. Exactly two
186242** arguments must be passed to this function. The first is the blob to
186243** patch and the second the patch to apply. If no error occurs, this
186244** function returns the patched blob.
186245*/
186246static void rbuFossilDeltaFunc(
186247 sqlite3_context *context,
186248 int argc,
186249 sqlite3_value **argv
186250){
186251 const char *aDelta;
186252 int nDelta;
186253 const char *aOrig;
186254 int nOrig;
186255
186256 int nOut;
186257 int nOut2;
186258 char *aOut;
186259
186260 assert( argc==2 );
186261
186262 nOrig = sqlite3_value_bytes(argv[0]);
186263 aOrig = (const char*)sqlite3_value_blob(argv[0]);
186264 nDelta = sqlite3_value_bytes(argv[1]);
186265 aDelta = (const char*)sqlite3_value_blob(argv[1]);
186266
186267 /* Figure out the size of the output */
186268 nOut = rbuDeltaOutputSize(aDelta, nDelta);
186269 if( nOut<0 ){
186270 sqlite3_result_error(context, "corrupt fossil delta", -1);
186271 return;
186272 }
186273
186274 aOut = sqlite3_malloc(nOut+1);
186275 if( aOut==0 ){
186276 sqlite3_result_error_nomem(context);
186277 }else{
186278 nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
186279 if( nOut2!=nOut ){
186280 sqlite3_result_error(context, "corrupt fossil delta", -1);
186281 }else{
186282 sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
186283 }
186284 }
186285}
186286
186287
186288/*
186289** Prepare the SQL statement in buffer zSql against database handle db.
186290** If successful, set *ppStmt to point to the new statement and return
186291** SQLITE_OK.
186292**
186293** Otherwise, if an error does occur, set *ppStmt to NULL and return
186294** an SQLite error code. Additionally, set output variable *pzErrmsg to
186295** point to a buffer containing an error message. It is the responsibility
186296** of the caller to (eventually) free this buffer using sqlite3_free().
186297*/
186298static int prepareAndCollectError(
186299 sqlite3 *db,
186300 sqlite3_stmt **ppStmt,
186301 char **pzErrmsg,
186302 const char *zSql
186303){
186304 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
186305 if( rc!=SQLITE_OK ){
186306 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
186307 *ppStmt = 0;
186308 }
186309 return rc;
186310}
186311
186312/*
186313** Reset the SQL statement passed as the first argument. Return a copy
186314** of the value returned by sqlite3_reset().
186315**
186316** If an error has occurred, then set *pzErrmsg to point to a buffer
186317** containing an error message. It is the responsibility of the caller
186318** to eventually free this buffer using sqlite3_free().
186319*/
186320static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
186321 int rc = sqlite3_reset(pStmt);
186322 if( rc!=SQLITE_OK ){
186323 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
186324 }
186325 return rc;
186326}
186327
186328/*
186329** Unless it is NULL, argument zSql points to a buffer allocated using
186330** sqlite3_malloc containing an SQL statement. This function prepares the SQL
186331** statement against database db and frees the buffer. If statement
186332** compilation is successful, *ppStmt is set to point to the new statement
186333** handle and SQLITE_OK is returned.
186334**
186335** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
186336** returned. In this case, *pzErrmsg may also be set to point to an error
186337** message. It is the responsibility of the caller to free this error message
186338** buffer using sqlite3_free().
186339**
186340** If argument zSql is NULL, this function assumes that an OOM has occurred.
186341** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
186342*/
186343static int prepareFreeAndCollectError(
186344 sqlite3 *db,
186345 sqlite3_stmt **ppStmt,
186346 char **pzErrmsg,
186347 char *zSql
186348){
186349 int rc;
186350 assert( *pzErrmsg==0 );
186351 if( zSql==0 ){
186352 rc = SQLITE_NOMEM;
186353 *ppStmt = 0;
186354 }else{
186355 rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
186356 sqlite3_free(zSql);
186357 }
186358 return rc;
186359}
186360
186361/*
186362** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
186363** by an earlier call to rbuObjIterCacheTableInfo().
186364*/
186365static void rbuObjIterFreeCols(RbuObjIter *pIter){
186366 int i;
186367 for(i=0; i<pIter->nTblCol; i++){
186368 sqlite3_free(pIter->azTblCol[i]);
186369 sqlite3_free(pIter->azTblType[i]);
186370 }
186371 sqlite3_free(pIter->azTblCol);
186372 pIter->azTblCol = 0;
186373 pIter->azTblType = 0;
186374 pIter->aiSrcOrder = 0;
186375 pIter->abTblPk = 0;
186376 pIter->abNotNull = 0;
186377 pIter->nTblCol = 0;
186378 pIter->eType = 0; /* Invalid value */
186379}
186380
186381/*
186382** Finalize all statements and free all allocations that are specific to
186383** the current object (table/index pair).
186384*/
186385static void rbuObjIterClearStatements(RbuObjIter *pIter){
186386 RbuUpdateStmt *pUp;
186387
186388 sqlite3_finalize(pIter->pSelect);
186389 sqlite3_finalize(pIter->pInsert);
186390 sqlite3_finalize(pIter->pDelete);
186391 sqlite3_finalize(pIter->pTmpInsert);
186392 pUp = pIter->pRbuUpdate;
186393 while( pUp ){
186394 RbuUpdateStmt *pTmp = pUp->pNext;
186395 sqlite3_finalize(pUp->pUpdate);
186396 sqlite3_free(pUp);
186397 pUp = pTmp;
186398 }
186399
186400 pIter->pSelect = 0;
186401 pIter->pInsert = 0;
186402 pIter->pDelete = 0;
186403 pIter->pRbuUpdate = 0;
186404 pIter->pTmpInsert = 0;
186405 pIter->nCol = 0;
186406}
186407
186408/*
186409** Clean up any resources allocated as part of the iterator object passed
186410** as the only argument.
186411*/
186412static void rbuObjIterFinalize(RbuObjIter *pIter){
186413 rbuObjIterClearStatements(pIter);
186414 sqlite3_finalize(pIter->pTblIter);
186415 sqlite3_finalize(pIter->pIdxIter);
186416 rbuObjIterFreeCols(pIter);
186417 memset(pIter, 0, sizeof(RbuObjIter));
186418}
186419
186420/*
186421** Advance the iterator to the next position.
186422**
186423** If no error occurs, SQLITE_OK is returned and the iterator is left
186424** pointing to the next entry. Otherwise, an error code and message is
186425** left in the RBU handle passed as the first argument. A copy of the
186426** error code is returned.
186427*/
186428static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
186429 int rc = p->rc;
186430 if( rc==SQLITE_OK ){
186431
186432 /* Free any SQLite statements used while processing the previous object */
186433 rbuObjIterClearStatements(pIter);
186434 if( pIter->zIdx==0 ){
186435 rc = sqlite3_exec(p->dbMain,
186436 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
186437 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
186438 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
186439 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
186440 , 0, 0, &p->zErrmsg
186441 );
186442 }
186443
186444 if( rc==SQLITE_OK ){
186445 if( pIter->bCleanup ){
186446 rbuObjIterFreeCols(pIter);
186447 pIter->bCleanup = 0;
186448 rc = sqlite3_step(pIter->pTblIter);
186449 if( rc!=SQLITE_ROW ){
186450 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
186451 pIter->zTbl = 0;
186452 }else{
186453 pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
186454 pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
186455 rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM;
186456 }
186457 }else{
186458 if( pIter->zIdx==0 ){
186459 sqlite3_stmt *pIdx = pIter->pIdxIter;
186460 rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
186461 }
186462 if( rc==SQLITE_OK ){
186463 rc = sqlite3_step(pIter->pIdxIter);
186464 if( rc!=SQLITE_ROW ){
186465 rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
186466 pIter->bCleanup = 1;
186467 pIter->zIdx = 0;
186468 }else{
186469 pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
186470 pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
186471 pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
186472 rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
186473 }
186474 }
186475 }
186476 }
186477 }
186478
186479 if( rc!=SQLITE_OK ){
186480 rbuObjIterFinalize(pIter);
186481 p->rc = rc;
186482 }
186483 return rc;
186484}
186485
186486
186487/*
186488** The implementation of the rbu_target_name() SQL function. This function
186489** accepts one or two arguments. The first argument is the name of a table -
186490** the name of a table in the RBU database. The second, if it is present, is 1
186491** for a view or 0 for a table.
186492**
186493** For a non-vacuum RBU handle, if the table name matches the pattern:
186494**
186495** data[0-9]_<name>
186496**
186497** where <name> is any sequence of 1 or more characters, <name> is returned.
186498** Otherwise, if the only argument does not match the above pattern, an SQL
186499** NULL is returned.
186500**
186501** "data_t1" -> "t1"
186502** "data0123_t2" -> "t2"
186503** "dataAB_t3" -> NULL
186504**
186505** For an rbu vacuum handle, a copy of the first argument is returned if
186506** the second argument is either missing or 0 (not a view).
186507*/
186508static void rbuTargetNameFunc(
186509 sqlite3_context *pCtx,
186510 int argc,
186511 sqlite3_value **argv
186512){
186513 sqlite3rbu *p = sqlite3_user_data(pCtx);
186514 const char *zIn;
186515 assert( argc==1 || argc==2 );
186516
186517 zIn = (const char*)sqlite3_value_text(argv[0]);
186518 if( zIn ){
186519 if( rbuIsVacuum(p) ){
186520 if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
186521 sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
186522 }
186523 }else{
186524 if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
186525 int i;
186526 for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
186527 if( zIn[i]=='_' && zIn[i+1] ){
186528 sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC);
186529 }
186530 }
186531 }
186532 }
186533}
186534
186535/*
186536** Initialize the iterator structure passed as the second argument.
186537**
186538** If no error occurs, SQLITE_OK is returned and the iterator is left
186539** pointing to the first entry. Otherwise, an error code and message is
186540** left in the RBU handle passed as the first argument. A copy of the
186541** error code is returned.
186542*/
186543static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
186544 int rc;
186545 memset(pIter, 0, sizeof(RbuObjIter));
186546
186547 rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
186548 sqlite3_mprintf(
186549 "SELECT rbu_target_name(name, type='view') AS target, name "
186550 "FROM sqlite_master "
186551 "WHERE type IN ('table', 'view') AND target IS NOT NULL "
186552 " %s "
186553 "ORDER BY name"
186554 , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
186555
186556 if( rc==SQLITE_OK ){
186557 rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
186558 "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
186559 " FROM main.sqlite_master "
186560 " WHERE type='index' AND tbl_name = ?"
186561 );
186562 }
186563
186564 pIter->bCleanup = 1;
186565 p->rc = rc;
186566 return rbuObjIterNext(p, pIter);
186567}
186568
186569/*
186570** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
186571** an error code is stored in the RBU handle passed as the first argument.
186572**
186573** If an error has already occurred (p->rc is already set to something other
186574** than SQLITE_OK), then this function returns NULL without modifying the
186575** stored error code. In this case it still calls sqlite3_free() on any
186576** printf() parameters associated with %z conversions.
186577*/
186578static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
186579 char *zSql = 0;
186580 va_list ap;
186581 va_start(ap, zFmt);
186582 zSql = sqlite3_vmprintf(zFmt, ap);
186583 if( p->rc==SQLITE_OK ){
186584 if( zSql==0 ) p->rc = SQLITE_NOMEM;
186585 }else{
186586 sqlite3_free(zSql);
186587 zSql = 0;
186588 }
186589 va_end(ap);
186590 return zSql;
186591}
186592
186593/*
186594** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
186595** arguments are the usual subsitution values. This function performs
186596** the printf() style substitutions and executes the result as an SQL
186597** statement on the RBU handles database.
186598**
186599** If an error occurs, an error code and error message is stored in the
186600** RBU handle. If an error has already occurred when this function is
186601** called, it is a no-op.
186602*/
186603static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
186604 va_list ap;
186605 char *zSql;
186606 va_start(ap, zFmt);
186607 zSql = sqlite3_vmprintf(zFmt, ap);
186608 if( p->rc==SQLITE_OK ){
186609 if( zSql==0 ){
186610 p->rc = SQLITE_NOMEM;
186611 }else{
186612 p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
186613 }
186614 }
186615 sqlite3_free(zSql);
186616 va_end(ap);
186617 return p->rc;
186618}
186619
186620/*
186621** Attempt to allocate and return a pointer to a zeroed block of nByte
186622** bytes.
186623**
186624** If an error (i.e. an OOM condition) occurs, return NULL and leave an
186625** error code in the rbu handle passed as the first argument. Or, if an
186626** error has already occurred when this function is called, return NULL
186627** immediately without attempting the allocation or modifying the stored
186628** error code.
186629*/
186630static void *rbuMalloc(sqlite3rbu *p, int nByte){
186631 void *pRet = 0;
186632 if( p->rc==SQLITE_OK ){
186633 assert( nByte>0 );
186634 pRet = sqlite3_malloc64(nByte);
186635 if( pRet==0 ){
186636 p->rc = SQLITE_NOMEM;
186637 }else{
186638 memset(pRet, 0, nByte);
186639 }
186640 }
186641 return pRet;
186642}
186643
186644
186645/*
186646** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
186647** there is room for at least nCol elements. If an OOM occurs, store an
186648** error code in the RBU handle passed as the first argument.
186649*/
186650static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
186651 int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
186652 char **azNew;
186653
186654 azNew = (char**)rbuMalloc(p, nByte);
186655 if( azNew ){
186656 pIter->azTblCol = azNew;
186657 pIter->azTblType = &azNew[nCol];
186658 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
186659 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
186660 pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
186661 pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
186662 }
186663}
186664
186665/*
186666** The first argument must be a nul-terminated string. This function
186667** returns a copy of the string in memory obtained from sqlite3_malloc().
186668** It is the responsibility of the caller to eventually free this memory
186669** using sqlite3_free().
186670**
186671** If an OOM condition is encountered when attempting to allocate memory,
186672** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
186673** if the allocation succeeds, (*pRc) is left unchanged.
186674*/
186675static char *rbuStrndup(const char *zStr, int *pRc){
186676 char *zRet = 0;
186677
186678 assert( *pRc==SQLITE_OK );
186679 if( zStr ){
186680 size_t nCopy = strlen(zStr) + 1;
186681 zRet = (char*)sqlite3_malloc64(nCopy);
186682 if( zRet ){
186683 memcpy(zRet, zStr, nCopy);
186684 }else{
186685 *pRc = SQLITE_NOMEM;
186686 }
186687 }
186688
186689 return zRet;
186690}
186691
186692/*
186693** Finalize the statement passed as the second argument.
186694**
186695** If the sqlite3_finalize() call indicates that an error occurs, and the
186696** rbu handle error code is not already set, set the error code and error
186697** message accordingly.
186698*/
186699static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
186700 sqlite3 *db = sqlite3_db_handle(pStmt);
186701 int rc = sqlite3_finalize(pStmt);
186702 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
186703 p->rc = rc;
186704 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
186705 }
186706}
186707
186708/* Determine the type of a table.
186709**
186710** peType is of type (int*), a pointer to an output parameter of type
186711** (int). This call sets the output parameter as follows, depending
186712** on the type of the table specified by parameters dbName and zTbl.
186713**
186714** RBU_PK_NOTABLE: No such table.
186715** RBU_PK_NONE: Table has an implicit rowid.
186716** RBU_PK_IPK: Table has an explicit IPK column.
186717** RBU_PK_EXTERNAL: Table has an external PK index.
186718** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
186719** RBU_PK_VTAB: Table is a virtual table.
186720**
186721** Argument *piPk is also of type (int*), and also points to an output
186722** parameter. Unless the table has an external primary key index
186723** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
186724** if the table does have an external primary key index, then *piPk
186725** is set to the root page number of the primary key index before
186726** returning.
186727**
186728** ALGORITHM:
186729**
186730** if( no entry exists in sqlite_master ){
186731** return RBU_PK_NOTABLE
186732** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
186733** return RBU_PK_VTAB
186734** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
186735** if( the index that is the pk exists in sqlite_master ){
186736** *piPK = rootpage of that index.
186737** return RBU_PK_EXTERNAL
186738** }else{
186739** return RBU_PK_WITHOUT_ROWID
186740** }
186741** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
186742** return RBU_PK_IPK
186743** }else{
186744** return RBU_PK_NONE
186745** }
186746*/
186747static void rbuTableType(
186748 sqlite3rbu *p,
186749 const char *zTab,
186750 int *peType,
186751 int *piTnum,
186752 int *piPk
186753){
186754 /*
186755 ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
186756 ** 1) PRAGMA index_list = ?
186757 ** 2) SELECT count(*) FROM sqlite_master where name=%Q
186758 ** 3) PRAGMA table_info = ?
186759 */
186760 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
186761
186762 *peType = RBU_PK_NOTABLE;
186763 *piPk = 0;
186764
186765 assert( p->rc==SQLITE_OK );
186766 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
186767 sqlite3_mprintf(
186768 "SELECT (sql LIKE 'create virtual%%'), rootpage"
186769 " FROM sqlite_master"
186770 " WHERE name=%Q", zTab
186771 ));
186772 if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
186773 /* Either an error, or no such table. */
186774 goto rbuTableType_end;
186775 }
186776 if( sqlite3_column_int(aStmt[0], 0) ){
186777 *peType = RBU_PK_VTAB; /* virtual table */
186778 goto rbuTableType_end;
186779 }
186780 *piTnum = sqlite3_column_int(aStmt[0], 1);
186781
186782 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
186783 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
186784 );
186785 if( p->rc ) goto rbuTableType_end;
186786 while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
186787 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
186788 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
186789 if( zOrig && zIdx && zOrig[0]=='p' ){
186790 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
186791 sqlite3_mprintf(
186792 "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
186793 ));
186794 if( p->rc==SQLITE_OK ){
186795 if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
186796 *piPk = sqlite3_column_int(aStmt[2], 0);
186797 *peType = RBU_PK_EXTERNAL;
186798 }else{
186799 *peType = RBU_PK_WITHOUT_ROWID;
186800 }
186801 }
186802 goto rbuTableType_end;
186803 }
186804 }
186805
186806 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
186807 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
186808 );
186809 if( p->rc==SQLITE_OK ){
186810 while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
186811 if( sqlite3_column_int(aStmt[3],5)>0 ){
186812 *peType = RBU_PK_IPK; /* explicit IPK column */
186813 goto rbuTableType_end;
186814 }
186815 }
186816 *peType = RBU_PK_NONE;
186817 }
186818
186819rbuTableType_end: {
186820 unsigned int i;
186821 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
186822 rbuFinalize(p, aStmt[i]);
186823 }
186824 }
186825}
186826
186827/*
186828** This is a helper function for rbuObjIterCacheTableInfo(). It populates
186829** the pIter->abIndexed[] array.
186830*/
186831static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
186832 sqlite3_stmt *pList = 0;
186833 int bIndex = 0;
186834
186835 if( p->rc==SQLITE_OK ){
186836 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
186837 p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
186838 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
186839 );
186840 }
186841
186842 pIter->nIndex = 0;
186843 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
186844 const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
186845 sqlite3_stmt *pXInfo = 0;
186846 if( zIdx==0 ) break;
186847 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
186848 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
186849 );
186850 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
186851 int iCid = sqlite3_column_int(pXInfo, 1);
186852 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
186853 }
186854 rbuFinalize(p, pXInfo);
186855 bIndex = 1;
186856 pIter->nIndex++;
186857 }
186858
186859 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
186860 /* "PRAGMA index_list" includes the main PK b-tree */
186861 pIter->nIndex--;
186862 }
186863
186864 rbuFinalize(p, pList);
186865 if( bIndex==0 ) pIter->abIndexed = 0;
186866}
186867
186868
186869/*
186870** If they are not already populated, populate the pIter->azTblCol[],
186871** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
186872** the table (not index) that the iterator currently points to.
186873**
186874** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
186875** an error does occur, an error code and error message are also left in
186876** the RBU handle.
186877*/
186878static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
186879 if( pIter->azTblCol==0 ){
186880 sqlite3_stmt *pStmt = 0;
186881 int nCol = 0;
186882 int i; /* for() loop iterator variable */
186883 int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
186884 int iOrder = 0;
186885 int iTnum = 0;
186886
186887 /* Figure out the type of table this step will deal with. */
186888 assert( pIter->eType==0 );
186889 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
186890 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
186891 p->rc = SQLITE_ERROR;
186892 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
186893 }
186894 if( p->rc ) return p->rc;
186895 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
186896
186897 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
186898 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
186899 || pIter->eType==RBU_PK_VTAB
186900 );
186901
186902 /* Populate the azTblCol[] and nTblCol variables based on the columns
186903 ** of the input table. Ignore any input table columns that begin with
186904 ** "rbu_". */
186905 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
186906 sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
186907 );
186908 if( p->rc==SQLITE_OK ){
186909 nCol = sqlite3_column_count(pStmt);
186910 rbuAllocateIterArrays(p, pIter, nCol);
186911 }
186912 for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
186913 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
186914 if( sqlite3_strnicmp("rbu_", zName, 4) ){
186915 char *zCopy = rbuStrndup(zName, &p->rc);
186916 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
186917 pIter->azTblCol[pIter->nTblCol++] = zCopy;
186918 }
186919 else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
186920 bRbuRowid = 1;
186921 }
186922 }
186923 sqlite3_finalize(pStmt);
186924 pStmt = 0;
186925
186926 if( p->rc==SQLITE_OK
186927 && rbuIsVacuum(p)==0
186928 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
186929 ){
186930 p->rc = SQLITE_ERROR;
186931 p->zErrmsg = sqlite3_mprintf(
186932 "table %q %s rbu_rowid column", pIter->zDataTbl,
186933 (bRbuRowid ? "may not have" : "requires")
186934 );
186935 }
186936
186937 /* Check that all non-HIDDEN columns in the destination table are also
186938 ** present in the input table. Populate the abTblPk[], azTblType[] and
186939 ** aiTblOrder[] arrays at the same time. */
186940 if( p->rc==SQLITE_OK ){
186941 p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
186942 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
186943 );
186944 }
186945 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
186946 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
186947 if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
186948 for(i=iOrder; i<pIter->nTblCol; i++){
186949 if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
186950 }
186951 if( i==pIter->nTblCol ){
186952 p->rc = SQLITE_ERROR;
186953 p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
186954 pIter->zDataTbl, zName
186955 );
186956 }else{
186957 int iPk = sqlite3_column_int(pStmt, 5);
186958 int bNotNull = sqlite3_column_int(pStmt, 3);
186959 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
186960
186961 if( i!=iOrder ){
186962 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
186963 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
186964 }
186965
186966 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
186967 pIter->abTblPk[iOrder] = (iPk!=0);
186968 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
186969 iOrder++;
186970 }
186971 }
186972
186973 rbuFinalize(p, pStmt);
186974 rbuObjIterCacheIndexedCols(p, pIter);
186975 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
186976 assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 );
186977 }
186978
186979 return p->rc;
186980}
186981
186982/*
186983** This function constructs and returns a pointer to a nul-terminated
186984** string containing some SQL clause or list based on one or more of the
186985** column names currently stored in the pIter->azTblCol[] array.
186986*/
186987static char *rbuObjIterGetCollist(
186988 sqlite3rbu *p, /* RBU object */
186989 RbuObjIter *pIter /* Object iterator for column names */
186990){
186991 char *zList = 0;
186992 const char *zSep = "";
186993 int i;
186994 for(i=0; i<pIter->nTblCol; i++){
186995 const char *z = pIter->azTblCol[i];
186996 zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
186997 zSep = ", ";
186998 }
186999 return zList;
187000}
187001
187002/*
187003** This function is used to create a SELECT list (the list of SQL
187004** expressions that follows a SELECT keyword) for a SELECT statement
187005** used to read from an data_xxx or rbu_tmp_xxx table while updating the
187006** index object currently indicated by the iterator object passed as the
187007** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
187008** to obtain the required information.
187009**
187010** If the index is of the following form:
187011**
187012** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
187013**
187014** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
187015** "ipk", the returned string is:
187016**
187017** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
187018**
187019** As well as the returned string, three other malloc'd strings are
187020** returned via output parameters. As follows:
187021**
187022** pzImposterCols: ...
187023** pzImposterPk: ...
187024** pzWhere: ...
187025*/
187026static char *rbuObjIterGetIndexCols(
187027 sqlite3rbu *p, /* RBU object */
187028 RbuObjIter *pIter, /* Object iterator for column names */
187029 char **pzImposterCols, /* OUT: Columns for imposter table */
187030 char **pzImposterPk, /* OUT: Imposter PK clause */
187031 char **pzWhere, /* OUT: WHERE clause */
187032 int *pnBind /* OUT: Trbul number of columns */
187033){
187034 int rc = p->rc; /* Error code */
187035 int rc2; /* sqlite3_finalize() return code */
187036 char *zRet = 0; /* String to return */
187037 char *zImpCols = 0; /* String to return via *pzImposterCols */
187038 char *zImpPK = 0; /* String to return via *pzImposterPK */
187039 char *zWhere = 0; /* String to return via *pzWhere */
187040 int nBind = 0; /* Value to return via *pnBind */
187041 const char *zCom = ""; /* Set to ", " later on */
187042 const char *zAnd = ""; /* Set to " AND " later on */
187043 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
187044
187045 if( rc==SQLITE_OK ){
187046 assert( p->zErrmsg==0 );
187047 rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
187048 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
187049 );
187050 }
187051
187052 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
187053 int iCid = sqlite3_column_int(pXInfo, 1);
187054 int bDesc = sqlite3_column_int(pXInfo, 3);
187055 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
187056 const char *zCol;
187057 const char *zType;
187058
187059 if( iCid<0 ){
187060 /* An integer primary key. If the table has an explicit IPK, use
187061 ** its name. Otherwise, use "rbu_rowid". */
187062 if( pIter->eType==RBU_PK_IPK ){
187063 int i;
187064 for(i=0; pIter->abTblPk[i]==0; i++);
187065 assert( i<pIter->nTblCol );
187066 zCol = pIter->azTblCol[i];
187067 }else if( rbuIsVacuum(p) ){
187068 zCol = "_rowid_";
187069 }else{
187070 zCol = "rbu_rowid";
187071 }
187072 zType = "INTEGER";
187073 }else{
187074 zCol = pIter->azTblCol[iCid];
187075 zType = pIter->azTblType[iCid];
187076 }
187077
187078 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
187079 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
187080 const char *zOrder = (bDesc ? " DESC" : "");
187081 zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
187082 zImpPK, zCom, nBind, zCol, zOrder
187083 );
187084 }
187085 zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
187086 zImpCols, zCom, nBind, zCol, zType, zCollate
187087 );
187088 zWhere = sqlite3_mprintf(
187089 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
187090 );
187091 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
187092 zCom = ", ";
187093 zAnd = " AND ";
187094 nBind++;
187095 }
187096
187097 rc2 = sqlite3_finalize(pXInfo);
187098 if( rc==SQLITE_OK ) rc = rc2;
187099
187100 if( rc!=SQLITE_OK ){
187101 sqlite3_free(zRet);
187102 sqlite3_free(zImpCols);
187103 sqlite3_free(zImpPK);
187104 sqlite3_free(zWhere);
187105 zRet = 0;
187106 zImpCols = 0;
187107 zImpPK = 0;
187108 zWhere = 0;
187109 p->rc = rc;
187110 }
187111
187112 *pzImposterCols = zImpCols;
187113 *pzImposterPk = zImpPK;
187114 *pzWhere = zWhere;
187115 *pnBind = nBind;
187116 return zRet;
187117}
187118
187119/*
187120** Assuming the current table columns are "a", "b" and "c", and the zObj
187121** paramter is passed "old", return a string of the form:
187122**
187123** "old.a, old.b, old.b"
187124**
187125** With the column names escaped.
187126**
187127** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
187128** the text ", old._rowid_" to the returned value.
187129*/
187130static char *rbuObjIterGetOldlist(
187131 sqlite3rbu *p,
187132 RbuObjIter *pIter,
187133 const char *zObj
187134){
187135 char *zList = 0;
187136 if( p->rc==SQLITE_OK && pIter->abIndexed ){
187137 const char *zS = "";
187138 int i;
187139 for(i=0; i<pIter->nTblCol; i++){
187140 if( pIter->abIndexed[i] ){
187141 const char *zCol = pIter->azTblCol[i];
187142 zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
187143 }else{
187144 zList = sqlite3_mprintf("%z%sNULL", zList, zS);
187145 }
187146 zS = ", ";
187147 if( zList==0 ){
187148 p->rc = SQLITE_NOMEM;
187149 break;
187150 }
187151 }
187152
187153 /* For a table with implicit rowids, append "old._rowid_" to the list. */
187154 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
187155 zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
187156 }
187157 }
187158 return zList;
187159}
187160
187161/*
187162** Return an expression that can be used in a WHERE clause to match the
187163** primary key of the current table. For example, if the table is:
187164**
187165** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
187166**
187167** Return the string:
187168**
187169** "b = ?1 AND c = ?2"
187170*/
187171static char *rbuObjIterGetWhere(
187172 sqlite3rbu *p,
187173 RbuObjIter *pIter
187174){
187175 char *zList = 0;
187176 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
187177 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
187178 }else if( pIter->eType==RBU_PK_EXTERNAL ){
187179 const char *zSep = "";
187180 int i;
187181 for(i=0; i<pIter->nTblCol; i++){
187182 if( pIter->abTblPk[i] ){
187183 zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
187184 zSep = " AND ";
187185 }
187186 }
187187 zList = rbuMPrintf(p,
187188 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
187189 );
187190
187191 }else{
187192 const char *zSep = "";
187193 int i;
187194 for(i=0; i<pIter->nTblCol; i++){
187195 if( pIter->abTblPk[i] ){
187196 const char *zCol = pIter->azTblCol[i];
187197 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
187198 zSep = " AND ";
187199 }
187200 }
187201 }
187202 return zList;
187203}
187204
187205/*
187206** The SELECT statement iterating through the keys for the current object
187207** (p->objiter.pSelect) currently points to a valid row. However, there
187208** is something wrong with the rbu_control value in the rbu_control value
187209** stored in the (p->nCol+1)'th column. Set the error code and error message
187210** of the RBU handle to something reflecting this.
187211*/
187212static void rbuBadControlError(sqlite3rbu *p){
187213 p->rc = SQLITE_ERROR;
187214 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
187215}
187216
187217
187218/*
187219** Return a nul-terminated string containing the comma separated list of
187220** assignments that should be included following the "SET" keyword of
187221** an UPDATE statement used to update the table object that the iterator
187222** passed as the second argument currently points to if the rbu_control
187223** column of the data_xxx table entry is set to zMask.
187224**
187225** The memory for the returned string is obtained from sqlite3_malloc().
187226** It is the responsibility of the caller to eventually free it using
187227** sqlite3_free().
187228**
187229** If an OOM error is encountered when allocating space for the new
187230** string, an error code is left in the rbu handle passed as the first
187231** argument and NULL is returned. Or, if an error has already occurred
187232** when this function is called, NULL is returned immediately, without
187233** attempting the allocation or modifying the stored error code.
187234*/
187235static char *rbuObjIterGetSetlist(
187236 sqlite3rbu *p,
187237 RbuObjIter *pIter,
187238 const char *zMask
187239){
187240 char *zList = 0;
187241 if( p->rc==SQLITE_OK ){
187242 int i;
187243
187244 if( (int)strlen(zMask)!=pIter->nTblCol ){
187245 rbuBadControlError(p);
187246 }else{
187247 const char *zSep = "";
187248 for(i=0; i<pIter->nTblCol; i++){
187249 char c = zMask[pIter->aiSrcOrder[i]];
187250 if( c=='x' ){
187251 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
187252 zList, zSep, pIter->azTblCol[i], i+1
187253 );
187254 zSep = ", ";
187255 }
187256 else if( c=='d' ){
187257 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
187258 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
187259 );
187260 zSep = ", ";
187261 }
187262 else if( c=='f' ){
187263 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
187264 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
187265 );
187266 zSep = ", ";
187267 }
187268 }
187269 }
187270 }
187271 return zList;
187272}
187273
187274/*
187275** Return a nul-terminated string consisting of nByte comma separated
187276** "?" expressions. For example, if nByte is 3, return a pointer to
187277** a buffer containing the string "?,?,?".
187278**
187279** The memory for the returned string is obtained from sqlite3_malloc().
187280** It is the responsibility of the caller to eventually free it using
187281** sqlite3_free().
187282**
187283** If an OOM error is encountered when allocating space for the new
187284** string, an error code is left in the rbu handle passed as the first
187285** argument and NULL is returned. Or, if an error has already occurred
187286** when this function is called, NULL is returned immediately, without
187287** attempting the allocation or modifying the stored error code.
187288*/
187289static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
187290 char *zRet = 0;
187291 int nByte = nBind*2 + 1;
187292
187293 zRet = (char*)rbuMalloc(p, nByte);
187294 if( zRet ){
187295 int i;
187296 for(i=0; i<nBind; i++){
187297 zRet[i*2] = '?';
187298 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
187299 }
187300 }
187301 return zRet;
187302}
187303
187304/*
187305** The iterator currently points to a table (not index) of type
187306** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
187307** declaration for the corresponding imposter table. For example,
187308** if the iterator points to a table created as:
187309**
187310** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
187311**
187312** this function returns:
187313**
187314** PRIMARY KEY("b", "a" DESC)
187315*/
187316static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
187317 char *z = 0;
187318 assert( pIter->zIdx==0 );
187319 if( p->rc==SQLITE_OK ){
187320 const char *zSep = "PRIMARY KEY(";
187321 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
187322 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
187323
187324 p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
187325 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
187326 );
187327 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
187328 const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
187329 if( zOrig && strcmp(zOrig, "pk")==0 ){
187330 const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
187331 if( zIdx ){
187332 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
187333 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
187334 );
187335 }
187336 break;
187337 }
187338 }
187339 rbuFinalize(p, pXList);
187340
187341 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
187342 if( sqlite3_column_int(pXInfo, 5) ){
187343 /* int iCid = sqlite3_column_int(pXInfo, 0); */
187344 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
187345 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
187346 z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
187347 zSep = ", ";
187348 }
187349 }
187350 z = rbuMPrintf(p, "%z)", z);
187351 rbuFinalize(p, pXInfo);
187352 }
187353 return z;
187354}
187355
187356/*
187357** This function creates the second imposter table used when writing to
187358** a table b-tree where the table has an external primary key. If the
187359** iterator passed as the second argument does not currently point to
187360** a table (not index) with an external primary key, this function is a
187361** no-op.
187362**
187363** Assuming the iterator does point to a table with an external PK, this
187364** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
187365** used to access that PK index. For example, if the target table is
187366** declared as follows:
187367**
187368** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
187369**
187370** then the imposter table schema is:
187371**
187372** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
187373**
187374*/
187375static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
187376 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
187377 int tnum = pIter->iPkTnum; /* Root page of PK index */
187378 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
187379 const char *zIdx = 0; /* Name of PK index */
187380 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
187381 const char *zComma = "";
187382 char *zCols = 0; /* Used to build up list of table cols */
187383 char *zPk = 0; /* Used to build up table PK declaration */
187384
187385 /* Figure out the name of the primary key index for the current table.
187386 ** This is needed for the argument to "PRAGMA index_xinfo". Set
187387 ** zIdx to point to a nul-terminated string containing this name. */
187388 p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
187389 "SELECT name FROM sqlite_master WHERE rootpage = ?"
187390 );
187391 if( p->rc==SQLITE_OK ){
187392 sqlite3_bind_int(pQuery, 1, tnum);
187393 if( SQLITE_ROW==sqlite3_step(pQuery) ){
187394 zIdx = (const char*)sqlite3_column_text(pQuery, 0);
187395 }
187396 }
187397 if( zIdx ){
187398 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
187399 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
187400 );
187401 }
187402 rbuFinalize(p, pQuery);
187403
187404 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
187405 int bKey = sqlite3_column_int(pXInfo, 5);
187406 if( bKey ){
187407 int iCid = sqlite3_column_int(pXInfo, 1);
187408 int bDesc = sqlite3_column_int(pXInfo, 3);
187409 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
187410 zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma,
187411 iCid, pIter->azTblType[iCid], zCollate
187412 );
187413 zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
187414 zComma = ", ";
187415 }
187416 }
187417 zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
187418 rbuFinalize(p, pXInfo);
187419
187420 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
187421 rbuMPrintfExec(p, p->dbMain,
187422 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
187423 zCols, zPk
187424 );
187425 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
187426 }
187427}
187428
187429/*
187430** If an error has already occurred when this function is called, it
187431** immediately returns zero (without doing any work). Or, if an error
187432** occurs during the execution of this function, it sets the error code
187433** in the sqlite3rbu object indicated by the first argument and returns
187434** zero.
187435**
187436** The iterator passed as the second argument is guaranteed to point to
187437** a table (not an index) when this function is called. This function
187438** attempts to create any imposter table required to write to the main
187439** table b-tree of the table before returning. Non-zero is returned if
187440** an imposter table are created, or zero otherwise.
187441**
187442** An imposter table is required in all cases except RBU_PK_VTAB. Only
187443** virtual tables are written to directly. The imposter table has the
187444** same schema as the actual target table (less any UNIQUE constraints).
187445** More precisely, the "same schema" means the same columns, types,
187446** collation sequences. For tables that do not have an external PRIMARY
187447** KEY, it also means the same PRIMARY KEY declaration.
187448*/
187449static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
187450 if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
187451 int tnum = pIter->iTnum;
187452 const char *zComma = "";
187453 char *zSql = 0;
187454 int iCol;
187455 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
187456
187457 for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
187458 const char *zPk = "";
187459 const char *zCol = pIter->azTblCol[iCol];
187460 const char *zColl = 0;
187461
187462 p->rc = sqlite3_table_column_metadata(
187463 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
187464 );
187465
187466 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
187467 /* If the target table column is an "INTEGER PRIMARY KEY", add
187468 ** "PRIMARY KEY" to the imposter table column declaration. */
187469 zPk = "PRIMARY KEY ";
187470 }
187471 zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s",
187472 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
187473 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
187474 );
187475 zComma = ", ";
187476 }
187477
187478 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
187479 char *zPk = rbuWithoutRowidPK(p, pIter);
187480 if( zPk ){
187481 zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
187482 }
187483 }
187484
187485 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
187486 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
187487 pIter->zTbl, zSql,
187488 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
187489 );
187490 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
187491 }
187492}
187493
187494/*
187495** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
187496** Specifically a statement of the form:
187497**
187498** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
187499**
187500** The number of bound variables is equal to the number of columns in
187501** the target table, plus one (for the rbu_control column), plus one more
187502** (for the rbu_rowid column) if the target table is an implicit IPK or
187503** virtual table.
187504*/
187505static void rbuObjIterPrepareTmpInsert(
187506 sqlite3rbu *p,
187507 RbuObjIter *pIter,
187508 const char *zCollist,
187509 const char *zRbuRowid
187510){
187511 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
187512 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
187513 if( zBind ){
187514 assert( pIter->pTmpInsert==0 );
187515 p->rc = prepareFreeAndCollectError(
187516 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
187517 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
187518 p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
187519 ));
187520 }
187521}
187522
187523static void rbuTmpInsertFunc(
187524 sqlite3_context *pCtx,
187525 int nVal,
187526 sqlite3_value **apVal
187527){
187528 sqlite3rbu *p = sqlite3_user_data(pCtx);
187529 int rc = SQLITE_OK;
187530 int i;
187531
187532 assert( sqlite3_value_int(apVal[0])!=0
187533 || p->objiter.eType==RBU_PK_EXTERNAL
187534 || p->objiter.eType==RBU_PK_NONE
187535 );
187536 if( sqlite3_value_int(apVal[0])!=0 ){
187537 p->nPhaseOneStep += p->objiter.nIndex;
187538 }
187539
187540 for(i=0; rc==SQLITE_OK && i<nVal; i++){
187541 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
187542 }
187543 if( rc==SQLITE_OK ){
187544 sqlite3_step(p->objiter.pTmpInsert);
187545 rc = sqlite3_reset(p->objiter.pTmpInsert);
187546 }
187547
187548 if( rc!=SQLITE_OK ){
187549 sqlite3_result_error_code(pCtx, rc);
187550 }
187551}
187552
187553/*
187554** Ensure that the SQLite statement handles required to update the
187555** target database object currently indicated by the iterator passed
187556** as the second argument are available.
187557*/
187558static int rbuObjIterPrepareAll(
187559 sqlite3rbu *p,
187560 RbuObjIter *pIter,
187561 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
187562){
187563 assert( pIter->bCleanup==0 );
187564 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
187565 const int tnum = pIter->iTnum;
187566 char *zCollist = 0; /* List of indexed columns */
187567 char **pz = &p->zErrmsg;
187568 const char *zIdx = pIter->zIdx;
187569 char *zLimit = 0;
187570
187571 if( nOffset ){
187572 zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
187573 if( !zLimit ) p->rc = SQLITE_NOMEM;
187574 }
187575
187576 if( zIdx ){
187577 const char *zTbl = pIter->zTbl;
187578 char *zImposterCols = 0; /* Columns for imposter table */
187579 char *zImposterPK = 0; /* Primary key declaration for imposter */
187580 char *zWhere = 0; /* WHERE clause on PK columns */
187581 char *zBind = 0;
187582 int nBind = 0;
187583
187584 assert( pIter->eType!=RBU_PK_VTAB );
187585 zCollist = rbuObjIterGetIndexCols(
187586 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
187587 );
187588 zBind = rbuObjIterGetBindlist(p, nBind);
187589
187590 /* Create the imposter table used to write to this index. */
187591 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
187592 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
187593 rbuMPrintfExec(p, p->dbMain,
187594 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
187595 zTbl, zImposterCols, zImposterPK
187596 );
187597 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
187598
187599 /* Create the statement to insert index entries */
187600 pIter->nCol = nBind;
187601 if( p->rc==SQLITE_OK ){
187602 p->rc = prepareFreeAndCollectError(
187603 p->dbMain, &pIter->pInsert, &p->zErrmsg,
187604 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
187605 );
187606 }
187607
187608 /* And to delete index entries */
187609 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
187610 p->rc = prepareFreeAndCollectError(
187611 p->dbMain, &pIter->pDelete, &p->zErrmsg,
187612 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
187613 );
187614 }
187615
187616 /* Create the SELECT statement to read keys in sorted order */
187617 if( p->rc==SQLITE_OK ){
187618 char *zSql;
187619 if( rbuIsVacuum(p) ){
187620 zSql = sqlite3_mprintf(
187621 "SELECT %s, 0 AS rbu_control FROM '%q' ORDER BY %s%s",
187622 zCollist,
187623 pIter->zDataTbl,
187624 zCollist, zLimit
187625 );
187626 }else
187627
187628 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
187629 zSql = sqlite3_mprintf(
187630 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' ORDER BY %s%s",
187631 zCollist, p->zStateDb, pIter->zDataTbl,
187632 zCollist, zLimit
187633 );
187634 }else{
187635 zSql = sqlite3_mprintf(
187636 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' "
187637 "UNION ALL "
187638 "SELECT %s, rbu_control FROM '%q' "
187639 "WHERE typeof(rbu_control)='integer' AND rbu_control!=1 "
187640 "ORDER BY %s%s",
187641 zCollist, p->zStateDb, pIter->zDataTbl,
187642 zCollist, pIter->zDataTbl,
187643 zCollist, zLimit
187644 );
187645 }
187646 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
187647 }
187648
187649 sqlite3_free(zImposterCols);
187650 sqlite3_free(zImposterPK);
187651 sqlite3_free(zWhere);
187652 sqlite3_free(zBind);
187653 }else{
187654 int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
187655 ||(pIter->eType==RBU_PK_NONE)
187656 ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
187657 const char *zTbl = pIter->zTbl; /* Table this step applies to */
187658 const char *zWrite; /* Imposter table name */
187659
187660 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
187661 char *zWhere = rbuObjIterGetWhere(p, pIter);
187662 char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
187663 char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
187664
187665 zCollist = rbuObjIterGetCollist(p, pIter);
187666 pIter->nCol = pIter->nTblCol;
187667
187668 /* Create the imposter table or tables (if required). */
187669 rbuCreateImposterTable(p, pIter);
187670 rbuCreateImposterTable2(p, pIter);
187671 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
187672
187673 /* Create the INSERT statement to write to the target PK b-tree */
187674 if( p->rc==SQLITE_OK ){
187675 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
187676 sqlite3_mprintf(
187677 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
187678 zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
187679 )
187680 );
187681 }
187682
187683 /* Create the DELETE statement to write to the target PK b-tree.
187684 ** Because it only performs INSERT operations, this is not required for
187685 ** an rbu vacuum handle. */
187686 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
187687 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
187688 sqlite3_mprintf(
187689 "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
187690 )
187691 );
187692 }
187693
187694 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
187695 const char *zRbuRowid = "";
187696 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
187697 zRbuRowid = ", rbu_rowid";
187698 }
187699
187700 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
187701 rbuMPrintfExec(p, p->dbRbu,
187702 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
187703 "SELECT *%s FROM '%q' WHERE 0;"
187704 , p->zStateDb, pIter->zDataTbl
187705 , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
187706 , pIter->zDataTbl
187707 );
187708
187709 rbuMPrintfExec(p, p->dbMain,
187710 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
187711 "BEGIN "
187712 " SELECT rbu_tmp_insert(3, %s);"
187713 "END;"
187714
187715 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
187716 "BEGIN "
187717 " SELECT rbu_tmp_insert(3, %s);"
187718 "END;"
187719
187720 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
187721 "BEGIN "
187722 " SELECT rbu_tmp_insert(4, %s);"
187723 "END;",
187724 zWrite, zTbl, zOldlist,
187725 zWrite, zTbl, zOldlist,
187726 zWrite, zTbl, zNewlist
187727 );
187728
187729 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
187730 rbuMPrintfExec(p, p->dbMain,
187731 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
187732 "BEGIN "
187733 " SELECT rbu_tmp_insert(0, %s);"
187734 "END;",
187735 zWrite, zTbl, zNewlist
187736 );
187737 }
187738
187739 rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
187740 }
187741
187742 /* Create the SELECT statement to read keys from data_xxx */
187743 if( p->rc==SQLITE_OK ){
187744 const char *zRbuRowid = "";
187745 if( bRbuRowid ){
187746 zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
187747 }
187748 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
187749 sqlite3_mprintf(
187750 "SELECT %s,%s rbu_control%s FROM '%q'%s",
187751 zCollist,
187752 (rbuIsVacuum(p) ? "0 AS " : ""),
187753 zRbuRowid,
187754 pIter->zDataTbl, zLimit
187755 )
187756 );
187757 }
187758
187759 sqlite3_free(zWhere);
187760 sqlite3_free(zOldlist);
187761 sqlite3_free(zNewlist);
187762 sqlite3_free(zBindings);
187763 }
187764 sqlite3_free(zCollist);
187765 sqlite3_free(zLimit);
187766 }
187767
187768 return p->rc;
187769}
187770
187771/*
187772** Set output variable *ppStmt to point to an UPDATE statement that may
187773** be used to update the imposter table for the main table b-tree of the
187774** table object that pIter currently points to, assuming that the
187775** rbu_control column of the data_xyz table contains zMask.
187776**
187777** If the zMask string does not specify any columns to update, then this
187778** is not an error. Output variable *ppStmt is set to NULL in this case.
187779*/
187780static int rbuGetUpdateStmt(
187781 sqlite3rbu *p, /* RBU handle */
187782 RbuObjIter *pIter, /* Object iterator */
187783 const char *zMask, /* rbu_control value ('x.x.') */
187784 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
187785){
187786 RbuUpdateStmt **pp;
187787 RbuUpdateStmt *pUp = 0;
187788 int nUp = 0;
187789
187790 /* In case an error occurs */
187791 *ppStmt = 0;
187792
187793 /* Search for an existing statement. If one is found, shift it to the front
187794 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
187795 ** to the number of statements currently in the cache and pUp to the
187796 ** last object in the list. */
187797 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
187798 pUp = *pp;
187799 if( strcmp(pUp->zMask, zMask)==0 ){
187800 *pp = pUp->pNext;
187801 pUp->pNext = pIter->pRbuUpdate;
187802 pIter->pRbuUpdate = pUp;
187803 *ppStmt = pUp->pUpdate;
187804 return SQLITE_OK;
187805 }
187806 nUp++;
187807 }
187808 assert( pUp==0 || pUp->pNext==0 );
187809
187810 if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
187811 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
187812 *pp = 0;
187813 sqlite3_finalize(pUp->pUpdate);
187814 pUp->pUpdate = 0;
187815 }else{
187816 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
187817 }
187818
187819 if( pUp ){
187820 char *zWhere = rbuObjIterGetWhere(p, pIter);
187821 char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
187822 char *zUpdate = 0;
187823
187824 pUp->zMask = (char*)&pUp[1];
187825 memcpy(pUp->zMask, zMask, pIter->nTblCol);
187826 pUp->pNext = pIter->pRbuUpdate;
187827 pIter->pRbuUpdate = pUp;
187828
187829 if( zSet ){
187830 const char *zPrefix = "";
187831
187832 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
187833 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
187834 zPrefix, pIter->zTbl, zSet, zWhere
187835 );
187836 p->rc = prepareFreeAndCollectError(
187837 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
187838 );
187839 *ppStmt = pUp->pUpdate;
187840 }
187841 sqlite3_free(zWhere);
187842 sqlite3_free(zSet);
187843 }
187844
187845 return p->rc;
187846}
187847
187848static sqlite3 *rbuOpenDbhandle(
187849 sqlite3rbu *p,
187850 const char *zName,
187851 int bUseVfs
187852){
187853 sqlite3 *db = 0;
187854 if( p->rc==SQLITE_OK ){
187855 const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
187856 p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
187857 if( p->rc ){
187858 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
187859 sqlite3_close(db);
187860 db = 0;
187861 }
187862 }
187863 return db;
187864}
187865
187866/*
187867** Free an RbuState object allocated by rbuLoadState().
187868*/
187869static void rbuFreeState(RbuState *p){
187870 if( p ){
187871 sqlite3_free(p->zTbl);
187872 sqlite3_free(p->zDataTbl);
187873 sqlite3_free(p->zIdx);
187874 sqlite3_free(p);
187875 }
187876}
187877
187878/*
187879** Allocate an RbuState object and load the contents of the rbu_state
187880** table into it. Return a pointer to the new object. It is the
187881** responsibility of the caller to eventually free the object using
187882** sqlite3_free().
187883**
187884** If an error occurs, leave an error code and message in the rbu handle
187885** and return NULL.
187886*/
187887static RbuState *rbuLoadState(sqlite3rbu *p){
187888 RbuState *pRet = 0;
187889 sqlite3_stmt *pStmt = 0;
187890 int rc;
187891 int rc2;
187892
187893 pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
187894 if( pRet==0 ) return 0;
187895
187896 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
187897 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
187898 );
187899 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
187900 switch( sqlite3_column_int(pStmt, 0) ){
187901 case RBU_STATE_STAGE:
187902 pRet->eStage = sqlite3_column_int(pStmt, 1);
187903 if( pRet->eStage!=RBU_STAGE_OAL
187904 && pRet->eStage!=RBU_STAGE_MOVE
187905 && pRet->eStage!=RBU_STAGE_CKPT
187906 ){
187907 p->rc = SQLITE_CORRUPT;
187908 }
187909 break;
187910
187911 case RBU_STATE_TBL:
187912 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
187913 break;
187914
187915 case RBU_STATE_IDX:
187916 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
187917 break;
187918
187919 case RBU_STATE_ROW:
187920 pRet->nRow = sqlite3_column_int(pStmt, 1);
187921 break;
187922
187923 case RBU_STATE_PROGRESS:
187924 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
187925 break;
187926
187927 case RBU_STATE_CKPT:
187928 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
187929 break;
187930
187931 case RBU_STATE_COOKIE:
187932 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
187933 break;
187934
187935 case RBU_STATE_OALSZ:
187936 pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
187937 break;
187938
187939 case RBU_STATE_PHASEONESTEP:
187940 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
187941 break;
187942
187943 case RBU_STATE_DATATBL:
187944 pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
187945 break;
187946
187947 default:
187948 rc = SQLITE_CORRUPT;
187949 break;
187950 }
187951 }
187952 rc2 = sqlite3_finalize(pStmt);
187953 if( rc==SQLITE_OK ) rc = rc2;
187954
187955 p->rc = rc;
187956 return pRet;
187957}
187958
187959
187960/*
187961** Open the database handle and attach the RBU database as "rbu". If an
187962** error occurs, leave an error code and message in the RBU handle.
187963*/
187964static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){
187965 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
187966 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
187967
187968 /* Open the RBU database */
187969 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
187970
187971 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
187972 sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
187973 if( p->zState==0 ){
187974 const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
187975 p->zState = rbuMPrintf(p, "file://%s-vacuum?modeof=%s", zFile, zFile);
187976 }
187977 }
187978
187979 /* If using separate RBU and state databases, attach the state database to
187980 ** the RBU db handle now. */
187981 if( p->zState ){
187982 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
187983 memcpy(p->zStateDb, "stat", 4);
187984 }else{
187985 memcpy(p->zStateDb, "main", 4);
187986 }
187987
187988#if 0
187989 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
187990 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
187991 }
187992#endif
187993
187994 /* If it has not already been created, create the rbu_state table */
187995 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
187996
187997#if 0
187998 if( rbuIsVacuum(p) ){
187999 if( p->rc==SQLITE_OK ){
188000 int rc2;
188001 int bOk = 0;
188002 sqlite3_stmt *pCnt = 0;
188003 p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
188004 "SELECT count(*) FROM stat.sqlite_master"
188005 );
188006 if( p->rc==SQLITE_OK
188007 && sqlite3_step(pCnt)==SQLITE_ROW
188008 && 1==sqlite3_column_int(pCnt, 0)
188009 ){
188010 bOk = 1;
188011 }
188012 rc2 = sqlite3_finalize(pCnt);
188013 if( p->rc==SQLITE_OK ) p->rc = rc2;
188014
188015 if( p->rc==SQLITE_OK && bOk==0 ){
188016 p->rc = SQLITE_ERROR;
188017 p->zErrmsg = sqlite3_mprintf("invalid state database");
188018 }
188019
188020 if( p->rc==SQLITE_OK ){
188021 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
188022 }
188023 }
188024 }
188025#endif
188026
188027 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
188028 int bOpen = 0;
188029 int rc;
188030 p->nRbu = 0;
188031 p->pRbuFd = 0;
188032 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
188033 if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
188034 if( p->eStage>=RBU_STAGE_MOVE ){
188035 bOpen = 1;
188036 }else{
188037 RbuState *pState = rbuLoadState(p);
188038 if( pState ){
188039 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
188040 rbuFreeState(pState);
188041 }
188042 }
188043 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
188044 }
188045
188046 p->eStage = 0;
188047 if( p->rc==SQLITE_OK && p->dbMain==0 ){
188048 if( !rbuIsVacuum(p) ){
188049 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
188050 }else if( p->pRbuFd->pWalFd ){
188051 if( pbRetry ){
188052 p->pRbuFd->bNolock = 0;
188053 sqlite3_close(p->dbRbu);
188054 sqlite3_close(p->dbMain);
188055 p->dbMain = 0;
188056 p->dbRbu = 0;
188057 *pbRetry = 1;
188058 return;
188059 }
188060 p->rc = SQLITE_ERROR;
188061 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
188062 }else{
188063 char *zTarget;
188064 char *zExtra = 0;
188065 if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
188066 zExtra = &p->zRbu[5];
188067 while( *zExtra ){
188068 if( *zExtra++=='?' ) break;
188069 }
188070 if( *zExtra=='\0' ) zExtra = 0;
188071 }
188072
188073 zTarget = sqlite3_mprintf("file:%s-vacuum?rbu_memory=1%s%s",
188074 sqlite3_db_filename(p->dbRbu, "main"),
188075 (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra)
188076 );
188077
188078 if( zTarget==0 ){
188079 p->rc = SQLITE_NOMEM;
188080 return;
188081 }
188082 p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
188083 sqlite3_free(zTarget);
188084 }
188085 }
188086
188087 if( p->rc==SQLITE_OK ){
188088 p->rc = sqlite3_create_function(p->dbMain,
188089 "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
188090 );
188091 }
188092
188093 if( p->rc==SQLITE_OK ){
188094 p->rc = sqlite3_create_function(p->dbMain,
188095 "rbu_fossil_delta", 2, SQLITE_UTF8, 0, rbuFossilDeltaFunc, 0, 0
188096 );
188097 }
188098
188099 if( p->rc==SQLITE_OK ){
188100 p->rc = sqlite3_create_function(p->dbRbu,
188101 "rbu_target_name", -1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0
188102 );
188103 }
188104
188105 if( p->rc==SQLITE_OK ){
188106 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
188107 }
188108 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
188109
188110 /* Mark the database file just opened as an RBU target database. If
188111 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
188112 ** This is an error. */
188113 if( p->rc==SQLITE_OK ){
188114 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
188115 }
188116
188117 if( p->rc==SQLITE_NOTFOUND ){
188118 p->rc = SQLITE_ERROR;
188119 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
188120 }
188121}
188122
188123/*
188124** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
188125** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
188126**
188127** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
188128** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
188129** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
188130** three characters, then shorten the suffix on z[] to be the last three
188131** characters of the original suffix.
188132**
188133** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
188134** do the suffix shortening regardless of URI parameter.
188135**
188136** Examples:
188137**
188138** test.db-journal => test.nal
188139** test.db-wal => test.wal
188140** test.db-shm => test.shm
188141** test.db-mj7f3319fa => test.9fa
188142*/
188143static void rbuFileSuffix3(const char *zBase, char *z){
188144#ifdef SQLITE_ENABLE_8_3_NAMES
188145#if SQLITE_ENABLE_8_3_NAMES<2
188146 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
188147#endif
188148 {
188149 int i, sz;
188150 sz = (int)strlen(z)&0xffffff;
188151 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
188152 if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
188153 }
188154#endif
188155}
188156
188157/*
188158** Return the current wal-index header checksum for the target database
188159** as a 64-bit integer.
188160**
188161** The checksum is store in the first page of xShmMap memory as an 8-byte
188162** blob starting at byte offset 40.
188163*/
188164static i64 rbuShmChecksum(sqlite3rbu *p){
188165 i64 iRet = 0;
188166 if( p->rc==SQLITE_OK ){
188167 sqlite3_file *pDb = p->pTargetFd->pReal;
188168 u32 volatile *ptr;
188169 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
188170 if( p->rc==SQLITE_OK ){
188171 iRet = ((i64)ptr[10] << 32) + ptr[11];
188172 }
188173 }
188174 return iRet;
188175}
188176
188177/*
188178** This function is called as part of initializing or reinitializing an
188179** incremental checkpoint.
188180**
188181** It populates the sqlite3rbu.aFrame[] array with the set of
188182** (wal frame -> db page) copy operations required to checkpoint the
188183** current wal file, and obtains the set of shm locks required to safely
188184** perform the copy operations directly on the file-system.
188185**
188186** If argument pState is not NULL, then the incremental checkpoint is
188187** being resumed. In this case, if the checksum of the wal-index-header
188188** following recovery is not the same as the checksum saved in the RbuState
188189** object, then the rbu handle is set to DONE state. This occurs if some
188190** other client appends a transaction to the wal file in the middle of
188191** an incremental checkpoint.
188192*/
188193static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
188194
188195 /* If pState is NULL, then the wal file may not have been opened and
188196 ** recovered. Running a read-statement here to ensure that doing so
188197 ** does not interfere with the "capture" process below. */
188198 if( pState==0 ){
188199 p->eStage = 0;
188200 if( p->rc==SQLITE_OK ){
188201 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
188202 }
188203 }
188204
188205 /* Assuming no error has occurred, run a "restart" checkpoint with the
188206 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
188207 ** special behaviour in the rbu VFS:
188208 **
188209 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
188210 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
188211 ** proceed with running a passive checkpoint instead of failing).
188212 **
188213 ** * Attempts to read from the *-wal file or write to the database file
188214 ** do not perform any IO. Instead, the frame/page combinations that
188215 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
188216 ** array.
188217 **
188218 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
188219 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
188220 ** no-ops. These locks will not be released until the connection
188221 ** is closed.
188222 **
188223 ** * Attempting to xSync() the database file causes an SQLITE_INTERNAL
188224 ** error.
188225 **
188226 ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
188227 ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
188228 ** array populated with a set of (frame -> page) mappings. Because the
188229 ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
188230 ** data from the wal file into the database file according to the
188231 ** contents of aFrame[].
188232 */
188233 if( p->rc==SQLITE_OK ){
188234 int rc2;
188235 p->eStage = RBU_STAGE_CAPTURE;
188236 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
188237 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
188238 }
188239
188240 if( p->rc==SQLITE_OK && p->nFrame>0 ){
188241 p->eStage = RBU_STAGE_CKPT;
188242 p->nStep = (pState ? pState->nRow : 0);
188243 p->aBuf = rbuMalloc(p, p->pgsz);
188244 p->iWalCksum = rbuShmChecksum(p);
188245 }
188246
188247 if( p->rc==SQLITE_OK ){
188248 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
188249 p->rc = SQLITE_DONE;
188250 p->eStage = RBU_STAGE_DONE;
188251 }else{
188252 int nSectorSize;
188253 sqlite3_file *pDb = p->pTargetFd->pReal;
188254 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
188255 assert( p->nPagePerSector==0 );
188256 nSectorSize = pDb->pMethods->xSectorSize(pDb);
188257 if( nSectorSize>p->pgsz ){
188258 p->nPagePerSector = nSectorSize / p->pgsz;
188259 }else{
188260 p->nPagePerSector = 1;
188261 }
188262
188263 /* Call xSync() on the wal file. This causes SQLite to sync the
188264 ** directory in which the target database and the wal file reside, in
188265 ** case it has not been synced since the rename() call in
188266 ** rbuMoveOalFile(). */
188267 p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
188268 }
188269 }
188270}
188271
188272/*
188273** Called when iAmt bytes are read from offset iOff of the wal file while
188274** the rbu object is in capture mode. Record the frame number of the frame
188275** being read in the aFrame[] array.
188276*/
188277static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
188278 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
188279 u32 iFrame;
188280
188281 if( pRbu->mLock!=mReq ){
188282 pRbu->rc = SQLITE_BUSY;
188283 return SQLITE_INTERNAL;
188284 }
188285
188286 pRbu->pgsz = iAmt;
188287 if( pRbu->nFrame==pRbu->nFrameAlloc ){
188288 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
188289 RbuFrame *aNew;
188290 aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
188291 if( aNew==0 ) return SQLITE_NOMEM;
188292 pRbu->aFrame = aNew;
188293 pRbu->nFrameAlloc = nNew;
188294 }
188295
188296 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
188297 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
188298 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
188299 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
188300 pRbu->nFrame++;
188301 return SQLITE_OK;
188302}
188303
188304/*
188305** Called when a page of data is written to offset iOff of the database
188306** file while the rbu handle is in capture mode. Record the page number
188307** of the page being written in the aFrame[] array.
188308*/
188309static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
188310 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
188311 return SQLITE_OK;
188312}
188313
188314/*
188315** This is called as part of an incremental checkpoint operation. Copy
188316** a single frame of data from the wal file into the database file, as
188317** indicated by the RbuFrame object.
188318*/
188319static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
188320 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
188321 sqlite3_file *pDb = p->pTargetFd->pReal;
188322 i64 iOff;
188323
188324 assert( p->rc==SQLITE_OK );
188325 iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
188326 p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
188327 if( p->rc ) return;
188328
188329 iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
188330 p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
188331}
188332
188333
188334/*
188335** Take an EXCLUSIVE lock on the database file.
188336*/
188337static void rbuLockDatabase(sqlite3rbu *p){
188338 sqlite3_file *pReal = p->pTargetFd->pReal;
188339 assert( p->rc==SQLITE_OK );
188340 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
188341 if( p->rc==SQLITE_OK ){
188342 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
188343 }
188344}
188345
188346#if defined(_WIN32_WCE)
188347static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
188348 int nChar;
188349 LPWSTR zWideFilename;
188350
188351 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
188352 if( nChar==0 ){
188353 return 0;
188354 }
188355 zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
188356 if( zWideFilename==0 ){
188357 return 0;
188358 }
188359 memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
188360 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
188361 nChar);
188362 if( nChar==0 ){
188363 sqlite3_free(zWideFilename);
188364 zWideFilename = 0;
188365 }
188366 return zWideFilename;
188367}
188368#endif
188369
188370/*
188371** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
188372** on the database file. This proc moves the *-oal file to the *-wal path,
188373** then reopens the database file (this time in vanilla, non-oal, WAL mode).
188374** If an error occurs, leave an error code and error message in the rbu
188375** handle.
188376*/
188377static void rbuMoveOalFile(sqlite3rbu *p){
188378 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
188379 const char *zMove = zBase;
188380 char *zOal;
188381 char *zWal;
188382
188383 if( rbuIsVacuum(p) ){
188384 zMove = sqlite3_db_filename(p->dbRbu, "main");
188385 }
188386 zOal = sqlite3_mprintf("%s-oal", zMove);
188387 zWal = sqlite3_mprintf("%s-wal", zMove);
188388
188389 assert( p->eStage==RBU_STAGE_MOVE );
188390 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
188391 if( zWal==0 || zOal==0 ){
188392 p->rc = SQLITE_NOMEM;
188393 }else{
188394 /* Move the *-oal file to *-wal. At this point connection p->db is
188395 ** holding a SHARED lock on the target database file (because it is
188396 ** in WAL mode). So no other connection may be writing the db.
188397 **
188398 ** In order to ensure that there are no database readers, an EXCLUSIVE
188399 ** lock is obtained here before the *-oal is moved to *-wal.
188400 */
188401 rbuLockDatabase(p);
188402 if( p->rc==SQLITE_OK ){
188403 rbuFileSuffix3(zBase, zWal);
188404 rbuFileSuffix3(zBase, zOal);
188405
188406 /* Re-open the databases. */
188407 rbuObjIterFinalize(&p->objiter);
188408 sqlite3_close(p->dbRbu);
188409 sqlite3_close(p->dbMain);
188410 p->dbMain = 0;
188411 p->dbRbu = 0;
188412
188413#if defined(_WIN32_WCE)
188414 {
188415 LPWSTR zWideOal;
188416 LPWSTR zWideWal;
188417
188418 zWideOal = rbuWinUtf8ToUnicode(zOal);
188419 if( zWideOal ){
188420 zWideWal = rbuWinUtf8ToUnicode(zWal);
188421 if( zWideWal ){
188422 if( MoveFileW(zWideOal, zWideWal) ){
188423 p->rc = SQLITE_OK;
188424 }else{
188425 p->rc = SQLITE_IOERR;
188426 }
188427 sqlite3_free(zWideWal);
188428 }else{
188429 p->rc = SQLITE_IOERR_NOMEM;
188430 }
188431 sqlite3_free(zWideOal);
188432 }else{
188433 p->rc = SQLITE_IOERR_NOMEM;
188434 }
188435 }
188436#else
188437 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
188438#endif
188439
188440 if( p->rc==SQLITE_OK ){
188441 rbuOpenDatabase(p, 0);
188442 rbuSetupCheckpoint(p, 0);
188443 }
188444 }
188445 }
188446
188447 sqlite3_free(zWal);
188448 sqlite3_free(zOal);
188449}
188450
188451/*
188452** The SELECT statement iterating through the keys for the current object
188453** (p->objiter.pSelect) currently points to a valid row. This function
188454** determines the type of operation requested by this row and returns
188455** one of the following values to indicate the result:
188456**
188457** * RBU_INSERT
188458** * RBU_DELETE
188459** * RBU_IDX_DELETE
188460** * RBU_UPDATE
188461**
188462** If RBU_UPDATE is returned, then output variable *pzMask is set to
188463** point to the text value indicating the columns to update.
188464**
188465** If the rbu_control field contains an invalid value, an error code and
188466** message are left in the RBU handle and zero returned.
188467*/
188468static int rbuStepType(sqlite3rbu *p, const char **pzMask){
188469 int iCol = p->objiter.nCol; /* Index of rbu_control column */
188470 int res = 0; /* Return value */
188471
188472 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
188473 case SQLITE_INTEGER: {
188474 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
188475 switch( iVal ){
188476 case 0: res = RBU_INSERT; break;
188477 case 1: res = RBU_DELETE; break;
188478 case 2: res = RBU_REPLACE; break;
188479 case 3: res = RBU_IDX_DELETE; break;
188480 case 4: res = RBU_IDX_INSERT; break;
188481 }
188482 break;
188483 }
188484
188485 case SQLITE_TEXT: {
188486 const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
188487 if( z==0 ){
188488 p->rc = SQLITE_NOMEM;
188489 }else{
188490 *pzMask = (const char*)z;
188491 }
188492 res = RBU_UPDATE;
188493
188494 break;
188495 }
188496
188497 default:
188498 break;
188499 }
188500
188501 if( res==0 ){
188502 rbuBadControlError(p);
188503 }
188504 return res;
188505}
188506
188507#ifdef SQLITE_DEBUG
188508/*
188509** Assert that column iCol of statement pStmt is named zName.
188510*/
188511static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
188512 const char *zCol = sqlite3_column_name(pStmt, iCol);
188513 assert( 0==sqlite3_stricmp(zName, zCol) );
188514}
188515#else
188516# define assertColumnName(x,y,z)
188517#endif
188518
188519/*
188520** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
188521** RBU_IDX_DELETE. This function performs the work of a single
188522** sqlite3rbu_step() call for the type of operation specified by eType.
188523*/
188524static void rbuStepOneOp(sqlite3rbu *p, int eType){
188525 RbuObjIter *pIter = &p->objiter;
188526 sqlite3_value *pVal;
188527 sqlite3_stmt *pWriter;
188528 int i;
188529
188530 assert( p->rc==SQLITE_OK );
188531 assert( eType!=RBU_DELETE || pIter->zIdx==0 );
188532 assert( eType==RBU_DELETE || eType==RBU_IDX_DELETE
188533 || eType==RBU_INSERT || eType==RBU_IDX_INSERT
188534 );
188535
188536 /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
188537 ** statement below does actually delete a row, nPhaseOneStep will be
188538 ** incremented by the same amount when SQL function rbu_tmp_insert()
188539 ** is invoked by the trigger. */
188540 if( eType==RBU_DELETE ){
188541 p->nPhaseOneStep -= p->objiter.nIndex;
188542 }
188543
188544 if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
188545 pWriter = pIter->pDelete;
188546 }else{
188547 pWriter = pIter->pInsert;
188548 }
188549
188550 for(i=0; i<pIter->nCol; i++){
188551 /* If this is an INSERT into a table b-tree and the table has an
188552 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
188553 ** to write a NULL into the IPK column. That is not permitted. */
188554 if( eType==RBU_INSERT
188555 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
188556 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
188557 ){
188558 p->rc = SQLITE_MISMATCH;
188559 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
188560 return;
188561 }
188562
188563 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
188564 continue;
188565 }
188566
188567 pVal = sqlite3_column_value(pIter->pSelect, i);
188568 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
188569 if( p->rc ) return;
188570 }
188571 if( pIter->zIdx==0 ){
188572 if( pIter->eType==RBU_PK_VTAB
188573 || pIter->eType==RBU_PK_NONE
188574 || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
188575 ){
188576 /* For a virtual table, or a table with no primary key, the
188577 ** SELECT statement is:
188578 **
188579 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
188580 **
188581 ** Hence column_value(pIter->nCol+1).
188582 */
188583 assertColumnName(pIter->pSelect, pIter->nCol+1,
188584 rbuIsVacuum(p) ? "rowid" : "rbu_rowid"
188585 );
188586 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
188587 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
188588 }
188589 }
188590 if( p->rc==SQLITE_OK ){
188591 sqlite3_step(pWriter);
188592 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
188593 }
188594}
188595
188596/*
188597** This function does the work for an sqlite3rbu_step() call.
188598**
188599** The object-iterator (p->objiter) currently points to a valid object,
188600** and the input cursor (p->objiter.pSelect) currently points to a valid
188601** input row. Perform whatever processing is required and return.
188602**
188603** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
188604** and message is left in the RBU handle and a copy of the error code
188605** returned.
188606*/
188607static int rbuStep(sqlite3rbu *p){
188608 RbuObjIter *pIter = &p->objiter;
188609 const char *zMask = 0;
188610 int eType = rbuStepType(p, &zMask);
188611
188612 if( eType ){
188613 assert( eType==RBU_INSERT || eType==RBU_DELETE
188614 || eType==RBU_REPLACE || eType==RBU_IDX_DELETE
188615 || eType==RBU_IDX_INSERT || eType==RBU_UPDATE
188616 );
188617 assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
188618
188619 if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
188620 rbuBadControlError(p);
188621 }
188622 else if( eType==RBU_REPLACE ){
188623 if( pIter->zIdx==0 ){
188624 p->nPhaseOneStep += p->objiter.nIndex;
188625 rbuStepOneOp(p, RBU_DELETE);
188626 }
188627 if( p->rc==SQLITE_OK ) rbuStepOneOp(p, RBU_INSERT);
188628 }
188629 else if( eType!=RBU_UPDATE ){
188630 rbuStepOneOp(p, eType);
188631 }
188632 else{
188633 sqlite3_value *pVal;
188634 sqlite3_stmt *pUpdate = 0;
188635 assert( eType==RBU_UPDATE );
188636 p->nPhaseOneStep -= p->objiter.nIndex;
188637 rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
188638 if( pUpdate ){
188639 int i;
188640 for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
188641 char c = zMask[pIter->aiSrcOrder[i]];
188642 pVal = sqlite3_column_value(pIter->pSelect, i);
188643 if( pIter->abTblPk[i] || c!='.' ){
188644 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
188645 }
188646 }
188647 if( p->rc==SQLITE_OK
188648 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
188649 ){
188650 /* Bind the rbu_rowid value to column _rowid_ */
188651 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
188652 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
188653 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
188654 }
188655 if( p->rc==SQLITE_OK ){
188656 sqlite3_step(pUpdate);
188657 p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
188658 }
188659 }
188660 }
188661 }
188662 return p->rc;
188663}
188664
188665/*
188666** Increment the schema cookie of the main database opened by p->dbMain.
188667**
188668** Or, if this is an RBU vacuum, set the schema cookie of the main db
188669** opened by p->dbMain to one more than the schema cookie of the main
188670** db opened by p->dbRbu.
188671*/
188672static void rbuIncrSchemaCookie(sqlite3rbu *p){
188673 if( p->rc==SQLITE_OK ){
188674 sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
188675 int iCookie = 1000000;
188676 sqlite3_stmt *pStmt;
188677
188678 p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
188679 "PRAGMA schema_version"
188680 );
188681 if( p->rc==SQLITE_OK ){
188682 /* Coverage: it may be that this sqlite3_step() cannot fail. There
188683 ** is already a transaction open, so the prepared statement cannot
188684 ** throw an SQLITE_SCHEMA exception. The only database page the
188685 ** statement reads is page 1, which is guaranteed to be in the cache.
188686 ** And no memory allocations are required. */
188687 if( SQLITE_ROW==sqlite3_step(pStmt) ){
188688 iCookie = sqlite3_column_int(pStmt, 0);
188689 }
188690 rbuFinalize(p, pStmt);
188691 }
188692 if( p->rc==SQLITE_OK ){
188693 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
188694 }
188695 }
188696}
188697
188698/*
188699** Update the contents of the rbu_state table within the rbu database. The
188700** value stored in the RBU_STATE_STAGE column is eStage. All other values
188701** are determined by inspecting the rbu handle passed as the first argument.
188702*/
188703static void rbuSaveState(sqlite3rbu *p, int eStage){
188704 if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
188705 sqlite3_stmt *pInsert = 0;
188706 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
188707 int rc;
188708
188709 assert( p->zErrmsg==0 );
188710 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
188711 sqlite3_mprintf(
188712 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
188713 "(%d, %d), "
188714 "(%d, %Q), "
188715 "(%d, %Q), "
188716 "(%d, %d), "
188717 "(%d, %d), "
188718 "(%d, %lld), "
188719 "(%d, %lld), "
188720 "(%d, %lld), "
188721 "(%d, %lld), "
188722 "(%d, %Q) ",
188723 p->zStateDb,
188724 RBU_STATE_STAGE, eStage,
188725 RBU_STATE_TBL, p->objiter.zTbl,
188726 RBU_STATE_IDX, p->objiter.zIdx,
188727 RBU_STATE_ROW, p->nStep,
188728 RBU_STATE_PROGRESS, p->nProgress,
188729 RBU_STATE_CKPT, p->iWalCksum,
188730 RBU_STATE_COOKIE, (i64)pFd->iCookie,
188731 RBU_STATE_OALSZ, p->iOalSz,
188732 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
188733 RBU_STATE_DATATBL, p->objiter.zDataTbl
188734 )
188735 );
188736 assert( pInsert==0 || rc==SQLITE_OK );
188737
188738 if( rc==SQLITE_OK ){
188739 sqlite3_step(pInsert);
188740 rc = sqlite3_finalize(pInsert);
188741 }
188742 if( rc!=SQLITE_OK ) p->rc = rc;
188743 }
188744}
188745
188746
188747/*
188748** The second argument passed to this function is the name of a PRAGMA
188749** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
188750** This function executes the following on sqlite3rbu.dbRbu:
188751**
188752** "PRAGMA main.$zPragma"
188753**
188754** where $zPragma is the string passed as the second argument, then
188755** on sqlite3rbu.dbMain:
188756**
188757** "PRAGMA main.$zPragma = $val"
188758**
188759** where $val is the value returned by the first PRAGMA invocation.
188760**
188761** In short, it copies the value of the specified PRAGMA setting from
188762** dbRbu to dbMain.
188763*/
188764static void rbuCopyPragma(sqlite3rbu *p, const char *zPragma){
188765 if( p->rc==SQLITE_OK ){
188766 sqlite3_stmt *pPragma = 0;
188767 p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
188768 sqlite3_mprintf("PRAGMA main.%s", zPragma)
188769 );
188770 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPragma) ){
188771 p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
188772 zPragma, sqlite3_column_int(pPragma, 0)
188773 );
188774 }
188775 rbuFinalize(p, pPragma);
188776 }
188777}
188778
188779/*
188780** The RBU handle passed as the only argument has just been opened and
188781** the state database is empty. If this RBU handle was opened for an
188782** RBU vacuum operation, create the schema in the target db.
188783*/
188784static void rbuCreateTargetSchema(sqlite3rbu *p){
188785 sqlite3_stmt *pSql = 0;
188786 sqlite3_stmt *pInsert = 0;
188787
188788 assert( rbuIsVacuum(p) );
188789 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
188790 if( p->rc==SQLITE_OK ){
188791 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
188792 "SELECT sql FROM sqlite_master WHERE sql!='' AND rootpage!=0"
188793 " AND name!='sqlite_sequence' "
188794 " ORDER BY type DESC"
188795 );
188796 }
188797
188798 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
188799 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
188800 p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
188801 }
188802 rbuFinalize(p, pSql);
188803 if( p->rc!=SQLITE_OK ) return;
188804
188805 if( p->rc==SQLITE_OK ){
188806 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
188807 "SELECT * FROM sqlite_master WHERE rootpage=0 OR rootpage IS NULL"
188808 );
188809 }
188810
188811 if( p->rc==SQLITE_OK ){
188812 p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
188813 "INSERT INTO sqlite_master VALUES(?,?,?,?,?)"
188814 );
188815 }
188816
188817 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
188818 int i;
188819 for(i=0; i<5; i++){
188820 sqlite3_bind_value(pInsert, i+1, sqlite3_column_value(pSql, i));
188821 }
188822 sqlite3_step(pInsert);
188823 p->rc = sqlite3_reset(pInsert);
188824 }
188825 if( p->rc==SQLITE_OK ){
188826 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
188827 }
188828
188829 rbuFinalize(p, pSql);
188830 rbuFinalize(p, pInsert);
188831}
188832
188833/*
188834** Step the RBU object.
188835*/
188836SQLITE_API int sqlite3rbu_step(sqlite3rbu *p){
188837 if( p ){
188838 switch( p->eStage ){
188839 case RBU_STAGE_OAL: {
188840 RbuObjIter *pIter = &p->objiter;
188841
188842 /* If this is an RBU vacuum operation and the state table was empty
188843 ** when this handle was opened, create the target database schema. */
188844 if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK ){
188845 rbuCreateTargetSchema(p);
188846 rbuCopyPragma(p, "user_version");
188847 rbuCopyPragma(p, "application_id");
188848 }
188849
188850 while( p->rc==SQLITE_OK && pIter->zTbl ){
188851
188852 if( pIter->bCleanup ){
188853 /* Clean up the rbu_tmp_xxx table for the previous table. It
188854 ** cannot be dropped as there are currently active SQL statements.
188855 ** But the contents can be deleted. */
188856 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
188857 rbuMPrintfExec(p, p->dbRbu,
188858 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
188859 );
188860 }
188861 }else{
188862 rbuObjIterPrepareAll(p, pIter, 0);
188863
188864 /* Advance to the next row to process. */
188865 if( p->rc==SQLITE_OK ){
188866 int rc = sqlite3_step(pIter->pSelect);
188867 if( rc==SQLITE_ROW ){
188868 p->nProgress++;
188869 p->nStep++;
188870 return rbuStep(p);
188871 }
188872 p->rc = sqlite3_reset(pIter->pSelect);
188873 p->nStep = 0;
188874 }
188875 }
188876
188877 rbuObjIterNext(p, pIter);
188878 }
188879
188880 if( p->rc==SQLITE_OK ){
188881 assert( pIter->zTbl==0 );
188882 rbuSaveState(p, RBU_STAGE_MOVE);
188883 rbuIncrSchemaCookie(p);
188884 if( p->rc==SQLITE_OK ){
188885 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
188886 }
188887 if( p->rc==SQLITE_OK ){
188888 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
188889 }
188890 p->eStage = RBU_STAGE_MOVE;
188891 }
188892 break;
188893 }
188894
188895 case RBU_STAGE_MOVE: {
188896 if( p->rc==SQLITE_OK ){
188897 rbuMoveOalFile(p);
188898 p->nProgress++;
188899 }
188900 break;
188901 }
188902
188903 case RBU_STAGE_CKPT: {
188904 if( p->rc==SQLITE_OK ){
188905 if( p->nStep>=p->nFrame ){
188906 sqlite3_file *pDb = p->pTargetFd->pReal;
188907
188908 /* Sync the db file */
188909 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
188910
188911 /* Update nBackfill */
188912 if( p->rc==SQLITE_OK ){
188913 void volatile *ptr;
188914 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
188915 if( p->rc==SQLITE_OK ){
188916 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
188917 }
188918 }
188919
188920 if( p->rc==SQLITE_OK ){
188921 p->eStage = RBU_STAGE_DONE;
188922 p->rc = SQLITE_DONE;
188923 }
188924 }else{
188925 /* At one point the following block copied a single frame from the
188926 ** wal file to the database file. So that one call to sqlite3rbu_step()
188927 ** checkpointed a single frame.
188928 **
188929 ** However, if the sector-size is larger than the page-size, and the
188930 ** application calls sqlite3rbu_savestate() or close() immediately
188931 ** after this step, then rbu_step() again, then a power failure occurs,
188932 ** then the database page written here may be damaged. Work around
188933 ** this by checkpointing frames until the next page in the aFrame[]
188934 ** lies on a different disk sector to the current one. */
188935 u32 iSector;
188936 do{
188937 RbuFrame *pFrame = &p->aFrame[p->nStep];
188938 iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
188939 rbuCheckpointFrame(p, pFrame);
188940 p->nStep++;
188941 }while( p->nStep<p->nFrame
188942 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
188943 && p->rc==SQLITE_OK
188944 );
188945 }
188946 p->nProgress++;
188947 }
188948 break;
188949 }
188950
188951 default:
188952 break;
188953 }
188954 return p->rc;
188955 }else{
188956 return SQLITE_NOMEM;
188957 }
188958}
188959
188960/*
188961** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
188962** otherwise. Either or both argument may be NULL. Two NULL values are
188963** considered equal, and NULL is considered distinct from all other values.
188964*/
188965static int rbuStrCompare(const char *z1, const char *z2){
188966 if( z1==0 && z2==0 ) return 0;
188967 if( z1==0 || z2==0 ) return 1;
188968 return (sqlite3_stricmp(z1, z2)!=0);
188969}
188970
188971/*
188972** This function is called as part of sqlite3rbu_open() when initializing
188973** an rbu handle in OAL stage. If the rbu update has not started (i.e.
188974** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
188975** things so that the next call to sqlite3rbu_step() continues on from
188976** where the previous rbu handle left off.
188977**
188978** If an error occurs, an error code and error message are left in the
188979** rbu handle passed as the first argument.
188980*/
188981static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
188982 assert( p->rc==SQLITE_OK );
188983 if( pState->zTbl ){
188984 RbuObjIter *pIter = &p->objiter;
188985 int rc = SQLITE_OK;
188986
188987 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
188988 || rbuStrCompare(pIter->zIdx, pState->zIdx)
188989 || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
188990 || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
188991 )){
188992 rc = rbuObjIterNext(p, pIter);
188993 }
188994
188995 if( rc==SQLITE_OK && !pIter->zTbl ){
188996 rc = SQLITE_ERROR;
188997 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
188998 }
188999
189000 if( rc==SQLITE_OK ){
189001 p->nStep = pState->nRow;
189002 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
189003 }
189004
189005 p->rc = rc;
189006 }
189007}
189008
189009/*
189010** If there is a "*-oal" file in the file-system corresponding to the
189011** target database in the file-system, delete it. If an error occurs,
189012** leave an error code and error message in the rbu handle.
189013*/
189014static void rbuDeleteOalFile(sqlite3rbu *p){
189015 char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
189016 if( zOal ){
189017 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
189018 assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 );
189019 pVfs->xDelete(pVfs, zOal, 0);
189020 sqlite3_free(zOal);
189021 }
189022}
189023
189024/*
189025** Allocate a private rbu VFS for the rbu handle passed as the only
189026** argument. This VFS will be used unless the call to sqlite3rbu_open()
189027** specified a URI with a vfs=? option in place of a target database
189028** file name.
189029*/
189030static void rbuCreateVfs(sqlite3rbu *p){
189031 int rnd;
189032 char zRnd[64];
189033
189034 assert( p->rc==SQLITE_OK );
189035 sqlite3_randomness(sizeof(int), (void*)&rnd);
189036 sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
189037 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
189038 if( p->rc==SQLITE_OK ){
189039 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
189040 assert( pVfs );
189041 p->zVfsName = pVfs->zName;
189042 ((rbu_vfs*)pVfs)->pRbu = p;
189043 }
189044}
189045
189046/*
189047** Destroy the private VFS created for the rbu handle passed as the only
189048** argument by an earlier call to rbuCreateVfs().
189049*/
189050static void rbuDeleteVfs(sqlite3rbu *p){
189051 if( p->zVfsName ){
189052 sqlite3rbu_destroy_vfs(p->zVfsName);
189053 p->zVfsName = 0;
189054 }
189055}
189056
189057/*
189058** This user-defined SQL function is invoked with a single argument - the
189059** name of a table expected to appear in the target database. It returns
189060** the number of auxilliary indexes on the table.
189061*/
189062static void rbuIndexCntFunc(
189063 sqlite3_context *pCtx,
189064 int nVal,
189065 sqlite3_value **apVal
189066){
189067 sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
189068 sqlite3_stmt *pStmt = 0;
189069 char *zErrmsg = 0;
189070 int rc;
189071
189072 assert( nVal==1 );
189073
189074 rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &zErrmsg,
189075 sqlite3_mprintf("SELECT count(*) FROM sqlite_master "
189076 "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
189077 );
189078 if( rc!=SQLITE_OK ){
189079 sqlite3_result_error(pCtx, zErrmsg, -1);
189080 }else{
189081 int nIndex = 0;
189082 if( SQLITE_ROW==sqlite3_step(pStmt) ){
189083 nIndex = sqlite3_column_int(pStmt, 0);
189084 }
189085 rc = sqlite3_finalize(pStmt);
189086 if( rc==SQLITE_OK ){
189087 sqlite3_result_int(pCtx, nIndex);
189088 }else{
189089 sqlite3_result_error(pCtx, sqlite3_errmsg(p->dbMain), -1);
189090 }
189091 }
189092
189093 sqlite3_free(zErrmsg);
189094}
189095
189096/*
189097** If the RBU database contains the rbu_count table, use it to initialize
189098** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
189099** is assumed to contain the same columns as:
189100**
189101** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
189102**
189103** There should be one row in the table for each data_xxx table in the
189104** database. The 'tbl' column should contain the name of a data_xxx table,
189105** and the cnt column the number of rows it contains.
189106**
189107** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
189108** for all rows in the rbu_count table, where nIndex is the number of
189109** indexes on the corresponding target database table.
189110*/
189111static void rbuInitPhaseOneSteps(sqlite3rbu *p){
189112 if( p->rc==SQLITE_OK ){
189113 sqlite3_stmt *pStmt = 0;
189114 int bExists = 0; /* True if rbu_count exists */
189115
189116 p->nPhaseOneStep = -1;
189117
189118 p->rc = sqlite3_create_function(p->dbRbu,
189119 "rbu_index_cnt", 1, SQLITE_UTF8, (void*)p, rbuIndexCntFunc, 0, 0
189120 );
189121
189122 /* Check for the rbu_count table. If it does not exist, or if an error
189123 ** occurs, nPhaseOneStep will be left set to -1. */
189124 if( p->rc==SQLITE_OK ){
189125 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
189126 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'rbu_count'"
189127 );
189128 }
189129 if( p->rc==SQLITE_OK ){
189130 if( SQLITE_ROW==sqlite3_step(pStmt) ){
189131 bExists = 1;
189132 }
189133 p->rc = sqlite3_finalize(pStmt);
189134 }
189135
189136 if( p->rc==SQLITE_OK && bExists ){
189137 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
189138 "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
189139 "FROM rbu_count"
189140 );
189141 if( p->rc==SQLITE_OK ){
189142 if( SQLITE_ROW==sqlite3_step(pStmt) ){
189143 p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
189144 }
189145 p->rc = sqlite3_finalize(pStmt);
189146 }
189147 }
189148 }
189149}
189150
189151
189152static sqlite3rbu *openRbuHandle(
189153 const char *zTarget,
189154 const char *zRbu,
189155 const char *zState
189156){
189157 sqlite3rbu *p;
189158 size_t nTarget = zTarget ? strlen(zTarget) : 0;
189159 size_t nRbu = strlen(zRbu);
189160 size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
189161
189162 p = (sqlite3rbu*)sqlite3_malloc64(nByte);
189163 if( p ){
189164 RbuState *pState = 0;
189165
189166 /* Create the custom VFS. */
189167 memset(p, 0, sizeof(sqlite3rbu));
189168 rbuCreateVfs(p);
189169
189170 /* Open the target, RBU and state databases */
189171 if( p->rc==SQLITE_OK ){
189172 char *pCsr = (char*)&p[1];
189173 int bRetry = 0;
189174 if( zTarget ){
189175 p->zTarget = pCsr;
189176 memcpy(p->zTarget, zTarget, nTarget+1);
189177 pCsr += nTarget+1;
189178 }
189179 p->zRbu = pCsr;
189180 memcpy(p->zRbu, zRbu, nRbu+1);
189181 pCsr += nRbu+1;
189182 if( zState ){
189183 p->zState = rbuMPrintf(p, "%s", zState);
189184 }
189185
189186 /* If the first attempt to open the database file fails and the bRetry
189187 ** flag it set, this means that the db was not opened because it seemed
189188 ** to be a wal-mode db. But, this may have happened due to an earlier
189189 ** RBU vacuum operation leaving an old wal file in the directory.
189190 ** If this is the case, it will have been checkpointed and deleted
189191 ** when the handle was closed and a second attempt to open the
189192 ** database may succeed. */
189193 rbuOpenDatabase(p, &bRetry);
189194 if( bRetry ){
189195 rbuOpenDatabase(p, 0);
189196 }
189197 }
189198
189199 if( p->rc==SQLITE_OK ){
189200 pState = rbuLoadState(p);
189201 assert( pState || p->rc!=SQLITE_OK );
189202 if( p->rc==SQLITE_OK ){
189203
189204 if( pState->eStage==0 ){
189205 rbuDeleteOalFile(p);
189206 rbuInitPhaseOneSteps(p);
189207 p->eStage = RBU_STAGE_OAL;
189208 }else{
189209 p->eStage = pState->eStage;
189210 p->nPhaseOneStep = pState->nPhaseOneStep;
189211 }
189212 p->nProgress = pState->nProgress;
189213 p->iOalSz = pState->iOalSz;
189214 }
189215 }
189216 assert( p->rc!=SQLITE_OK || p->eStage!=0 );
189217
189218 if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
189219 if( p->eStage==RBU_STAGE_OAL ){
189220 p->rc = SQLITE_ERROR;
189221 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
189222 }else if( p->eStage==RBU_STAGE_MOVE ){
189223 p->eStage = RBU_STAGE_CKPT;
189224 p->nStep = 0;
189225 }
189226 }
189227
189228 if( p->rc==SQLITE_OK
189229 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
189230 && pState->eStage!=0
189231 ){
189232 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
189233 if( pFd->iCookie!=pState->iCookie ){
189234 /* At this point (pTargetFd->iCookie) contains the value of the
189235 ** change-counter cookie (the thing that gets incremented when a
189236 ** transaction is committed in rollback mode) currently stored on
189237 ** page 1 of the database file. */
189238 p->rc = SQLITE_BUSY;
189239 p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
189240 (rbuIsVacuum(p) ? "vacuum" : "update")
189241 );
189242 }
189243 }
189244
189245 if( p->rc==SQLITE_OK ){
189246 if( p->eStage==RBU_STAGE_OAL ){
189247 sqlite3 *db = p->dbMain;
189248 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
189249
189250 /* Point the object iterator at the first object */
189251 if( p->rc==SQLITE_OK ){
189252 p->rc = rbuObjIterFirst(p, &p->objiter);
189253 }
189254
189255 /* If the RBU database contains no data_xxx tables, declare the RBU
189256 ** update finished. */
189257 if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
189258 p->rc = SQLITE_DONE;
189259 p->eStage = RBU_STAGE_DONE;
189260 }else{
189261 if( p->rc==SQLITE_OK && pState->eStage==0 && rbuIsVacuum(p) ){
189262 rbuCopyPragma(p, "page_size");
189263 rbuCopyPragma(p, "auto_vacuum");
189264 }
189265
189266 /* Open transactions both databases. The *-oal file is opened or
189267 ** created at this point. */
189268 if( p->rc==SQLITE_OK ){
189269 p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
189270 }
189271
189272 /* Check if the main database is a zipvfs db. If it is, set the upper
189273 ** level pager to use "journal_mode=off". This prevents it from
189274 ** generating a large journal using a temp file. */
189275 if( p->rc==SQLITE_OK ){
189276 int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS, 0);
189277 if( frc==SQLITE_OK ){
189278 p->rc = sqlite3_exec(
189279 db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
189280 }
189281 }
189282
189283 if( p->rc==SQLITE_OK ){
189284 rbuSetupOal(p, pState);
189285 }
189286 }
189287 }else if( p->eStage==RBU_STAGE_MOVE ){
189288 /* no-op */
189289 }else if( p->eStage==RBU_STAGE_CKPT ){
189290 rbuSetupCheckpoint(p, pState);
189291 }else if( p->eStage==RBU_STAGE_DONE ){
189292 p->rc = SQLITE_DONE;
189293 }else{
189294 p->rc = SQLITE_CORRUPT;
189295 }
189296 }
189297
189298 rbuFreeState(pState);
189299 }
189300
189301 return p;
189302}
189303
189304/*
189305** Allocate and return an RBU handle with all fields zeroed except for the
189306** error code, which is set to SQLITE_MISUSE.
189307*/
189308static sqlite3rbu *rbuMisuseError(void){
189309 sqlite3rbu *pRet;
189310 pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
189311 if( pRet ){
189312 memset(pRet, 0, sizeof(sqlite3rbu));
189313 pRet->rc = SQLITE_MISUSE;
189314 }
189315 return pRet;
189316}
189317
189318/*
189319** Open and return a new RBU handle.
189320*/
189321SQLITE_API sqlite3rbu *sqlite3rbu_open(
189322 const char *zTarget,
189323 const char *zRbu,
189324 const char *zState
189325){
189326 if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
189327 /* TODO: Check that zTarget and zRbu are non-NULL */
189328 return openRbuHandle(zTarget, zRbu, zState);
189329}
189330
189331/*
189332** Open a handle to begin or resume an RBU VACUUM operation.
189333*/
189334SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
189335 const char *zTarget,
189336 const char *zState
189337){
189338 if( zTarget==0 ){ return rbuMisuseError(); }
189339 /* TODO: Check that both arguments are non-NULL */
189340 return openRbuHandle(0, zTarget, zState);
189341}
189342
189343/*
189344** Return the database handle used by pRbu.
189345*/
189346SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
189347 sqlite3 *db = 0;
189348 if( pRbu ){
189349 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
189350 }
189351 return db;
189352}
189353
189354
189355/*
189356** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
189357** then edit any error message string so as to remove all occurrences of
189358** the pattern "rbu_imp_[0-9]*".
189359*/
189360static void rbuEditErrmsg(sqlite3rbu *p){
189361 if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
189362 unsigned int i;
189363 size_t nErrmsg = strlen(p->zErrmsg);
189364 for(i=0; i<(nErrmsg-8); i++){
189365 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
189366 int nDel = 8;
189367 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
189368 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
189369 nErrmsg -= nDel;
189370 }
189371 }
189372 }
189373}
189374
189375/*
189376** Close the RBU handle.
189377*/
189378SQLITE_API int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
189379 int rc;
189380 if( p ){
189381
189382 /* Commit the transaction to the *-oal file. */
189383 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
189384 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
189385 }
189386
189387 /* Sync the db file if currently doing an incremental checkpoint */
189388 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
189389 sqlite3_file *pDb = p->pTargetFd->pReal;
189390 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
189391 }
189392
189393 rbuSaveState(p, p->eStage);
189394
189395 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
189396 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
189397 }
189398
189399 /* Close any open statement handles. */
189400 rbuObjIterFinalize(&p->objiter);
189401
189402 /* If this is an RBU vacuum handle and the vacuum has either finished
189403 ** successfully or encountered an error, delete the contents of the
189404 ** state table. This causes the next call to sqlite3rbu_vacuum()
189405 ** specifying the current target and state databases to start a new
189406 ** vacuum from scratch. */
189407 if( rbuIsVacuum(p) && p->rc!=SQLITE_OK && p->dbRbu ){
189408 int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
189409 if( p->rc==SQLITE_DONE && rc2!=SQLITE_OK ) p->rc = rc2;
189410 }
189411
189412 /* Close the open database handle and VFS object. */
189413 sqlite3_close(p->dbRbu);
189414 sqlite3_close(p->dbMain);
189415 assert( p->szTemp==0 );
189416 rbuDeleteVfs(p);
189417 sqlite3_free(p->aBuf);
189418 sqlite3_free(p->aFrame);
189419
189420 rbuEditErrmsg(p);
189421 rc = p->rc;
189422 if( pzErrmsg ){
189423 *pzErrmsg = p->zErrmsg;
189424 }else{
189425 sqlite3_free(p->zErrmsg);
189426 }
189427 sqlite3_free(p->zState);
189428 sqlite3_free(p);
189429 }else{
189430 rc = SQLITE_NOMEM;
189431 *pzErrmsg = 0;
189432 }
189433 return rc;
189434}
189435
189436/*
189437** Return the total number of key-value operations (inserts, deletes or
189438** updates) that have been performed on the target database since the
189439** current RBU update was started.
189440*/
189441SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
189442 return pRbu->nProgress;
189443}
189444
189445/*
189446** Return permyriadage progress indications for the two main stages of
189447** an RBU update.
189448*/
189449SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *p, int *pnOne, int *pnTwo){
189450 const int MAX_PROGRESS = 10000;
189451 switch( p->eStage ){
189452 case RBU_STAGE_OAL:
189453 if( p->nPhaseOneStep>0 ){
189454 *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
189455 }else{
189456 *pnOne = -1;
189457 }
189458 *pnTwo = 0;
189459 break;
189460
189461 case RBU_STAGE_MOVE:
189462 *pnOne = MAX_PROGRESS;
189463 *pnTwo = 0;
189464 break;
189465
189466 case RBU_STAGE_CKPT:
189467 *pnOne = MAX_PROGRESS;
189468 *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
189469 break;
189470
189471 case RBU_STAGE_DONE:
189472 *pnOne = MAX_PROGRESS;
189473 *pnTwo = MAX_PROGRESS;
189474 break;
189475
189476 default:
189477 assert( 0 );
189478 }
189479}
189480
189481/*
189482** Return the current state of the RBU vacuum or update operation.
189483*/
189484SQLITE_API int sqlite3rbu_state(sqlite3rbu *p){
189485 int aRes[] = {
189486 0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
189487 0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
189488 };
189489
189490 assert( RBU_STAGE_OAL==1 );
189491 assert( RBU_STAGE_MOVE==2 );
189492 assert( RBU_STAGE_CKPT==4 );
189493 assert( RBU_STAGE_DONE==5 );
189494 assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL );
189495 assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE );
189496 assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT );
189497 assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE );
189498
189499 if( p->rc!=SQLITE_OK && p->rc!=SQLITE_DONE ){
189500 return SQLITE_RBU_STATE_ERROR;
189501 }else{
189502 assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE );
189503 assert( p->eStage==RBU_STAGE_OAL
189504 || p->eStage==RBU_STAGE_MOVE
189505 || p->eStage==RBU_STAGE_CKPT
189506 || p->eStage==RBU_STAGE_DONE
189507 );
189508 return aRes[p->eStage];
189509 }
189510}
189511
189512SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *p){
189513 int rc = p->rc;
189514 if( rc==SQLITE_DONE ) return SQLITE_OK;
189515
189516 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
189517 if( p->eStage==RBU_STAGE_OAL ){
189518 assert( rc!=SQLITE_DONE );
189519 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
189520 }
189521
189522 /* Sync the db file */
189523 if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
189524 sqlite3_file *pDb = p->pTargetFd->pReal;
189525 rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
189526 }
189527
189528 p->rc = rc;
189529 rbuSaveState(p, p->eStage);
189530 rc = p->rc;
189531
189532 if( p->eStage==RBU_STAGE_OAL ){
189533 assert( rc!=SQLITE_DONE );
189534 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
189535 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, 0);
189536 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
189537 }
189538
189539 p->rc = rc;
189540 return rc;
189541}
189542
189543/**************************************************************************
189544** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
189545** of a standard VFS in the following ways:
189546**
189547** 1. Whenever the first page of a main database file is read or
189548** written, the value of the change-counter cookie is stored in
189549** rbu_file.iCookie. Similarly, the value of the "write-version"
189550** database header field is stored in rbu_file.iWriteVer. This ensures
189551** that the values are always trustworthy within an open transaction.
189552**
189553** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
189554** member variable of the associated database file descriptor is set
189555** to point to the new file. A mutex protected linked list of all main
189556** db fds opened using a particular RBU VFS is maintained at
189557** rbu_vfs.pMain to facilitate this.
189558**
189559** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
189560** object can be marked as the target database of an RBU update. This
189561** turns on the following extra special behaviour:
189562**
189563** 3a. If xAccess() is called to check if there exists a *-wal file
189564** associated with an RBU target database currently in RBU_STAGE_OAL
189565** stage (preparing the *-oal file), the following special handling
189566** applies:
189567**
189568** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
189569** target database may not be in wal mode already.
189570**
189571** * if the *-wal file does not exist, set the output parameter to
189572** non-zero (to tell SQLite that it does exist) anyway.
189573**
189574** Then, when xOpen() is called to open the *-wal file associated with
189575** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
189576** file, the rbu vfs opens the corresponding *-oal file instead.
189577**
189578** 3b. The *-shm pages returned by xShmMap() for a target db file in
189579** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
189580** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
189581** are no-ops on target database files in RBU_STAGE_OAL mode. This is
189582** because assert() statements in some VFS implementations fail if
189583** xShmLock() is called before xShmMap().
189584**
189585** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
189586** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
189587** fails with an SQLITE_BUSY error. This is to stop RBU connections
189588** from automatically checkpointing a *-wal (or *-oal) file from within
189589** sqlite3_close().
189590**
189591** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
189592** all xWrite() calls on the target database file perform no IO.
189593** Instead the frame and page numbers that would be read and written
189594** are recorded. Additionally, successful attempts to obtain exclusive
189595** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
189596** database file are recorded. xShmLock() calls to unlock the same
189597** locks are no-ops (so that once obtained, these locks are never
189598** relinquished). Finally, calls to xSync() on the target database
189599** file fail with SQLITE_INTERNAL errors.
189600*/
189601
189602static void rbuUnlockShm(rbu_file *p){
189603 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
189604 if( p->pRbu ){
189605 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
189606 int i;
189607 for(i=0; i<SQLITE_SHM_NLOCK;i++){
189608 if( (1<<i) & p->pRbu->mLock ){
189609 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
189610 }
189611 }
189612 p->pRbu->mLock = 0;
189613 }
189614}
189615
189616/*
189617*/
189618static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
189619 sqlite3rbu *pRbu = pFd->pRbu;
189620 i64 nDiff = nNew - pFd->sz;
189621 pRbu->szTemp += nDiff;
189622 pFd->sz = nNew;
189623 assert( pRbu->szTemp>=0 );
189624 if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL;
189625 return SQLITE_OK;
189626}
189627
189628/*
189629** Close an rbu file.
189630*/
189631static int rbuVfsClose(sqlite3_file *pFile){
189632 rbu_file *p = (rbu_file*)pFile;
189633 int rc;
189634 int i;
189635
189636 /* Free the contents of the apShm[] array. And the array itself. */
189637 for(i=0; i<p->nShm; i++){
189638 sqlite3_free(p->apShm[i]);
189639 }
189640 sqlite3_free(p->apShm);
189641 p->apShm = 0;
189642 sqlite3_free(p->zDel);
189643
189644 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
189645 rbu_file **pp;
189646 sqlite3_mutex_enter(p->pRbuVfs->mutex);
189647 for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
189648 *pp = p->pMainNext;
189649 sqlite3_mutex_leave(p->pRbuVfs->mutex);
189650 rbuUnlockShm(p);
189651 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
189652 }
189653 else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
189654 rbuUpdateTempSize(p, 0);
189655 }
189656
189657 /* Close the underlying file handle */
189658 rc = p->pReal->pMethods->xClose(p->pReal);
189659 return rc;
189660}
189661
189662
189663/*
189664** Read and return an unsigned 32-bit big-endian integer from the buffer
189665** passed as the only argument.
189666*/
189667static u32 rbuGetU32(u8 *aBuf){
189668 return ((u32)aBuf[0] << 24)
189669 + ((u32)aBuf[1] << 16)
189670 + ((u32)aBuf[2] << 8)
189671 + ((u32)aBuf[3]);
189672}
189673
189674/*
189675** Write an unsigned 32-bit value in big-endian format to the supplied
189676** buffer.
189677*/
189678static void rbuPutU32(u8 *aBuf, u32 iVal){
189679 aBuf[0] = (iVal >> 24) & 0xFF;
189680 aBuf[1] = (iVal >> 16) & 0xFF;
189681 aBuf[2] = (iVal >> 8) & 0xFF;
189682 aBuf[3] = (iVal >> 0) & 0xFF;
189683}
189684
189685static void rbuPutU16(u8 *aBuf, u16 iVal){
189686 aBuf[0] = (iVal >> 8) & 0xFF;
189687 aBuf[1] = (iVal >> 0) & 0xFF;
189688}
189689
189690/*
189691** Read data from an rbuVfs-file.
189692*/
189693static int rbuVfsRead(
189694 sqlite3_file *pFile,
189695 void *zBuf,
189696 int iAmt,
189697 sqlite_int64 iOfst
189698){
189699 rbu_file *p = (rbu_file*)pFile;
189700 sqlite3rbu *pRbu = p->pRbu;
189701 int rc;
189702
189703 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
189704 assert( p->openFlags & SQLITE_OPEN_WAL );
189705 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
189706 }else{
189707 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
189708 && (p->openFlags & SQLITE_OPEN_WAL)
189709 && iOfst>=pRbu->iOalSz
189710 ){
189711 rc = SQLITE_OK;
189712 memset(zBuf, 0, iAmt);
189713 }else{
189714 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
189715#if 1
189716 /* If this is being called to read the first page of the target
189717 ** database as part of an rbu vacuum operation, synthesize the
189718 ** contents of the first page if it does not yet exist. Otherwise,
189719 ** SQLite will not check for a *-wal file. */
189720 if( pRbu && rbuIsVacuum(pRbu)
189721 && rc==SQLITE_IOERR_SHORT_READ && iOfst==0
189722 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
189723 && pRbu->rc==SQLITE_OK
189724 ){
189725 sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
189726 rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
189727 if( rc==SQLITE_OK ){
189728 u8 *aBuf = (u8*)zBuf;
189729 u32 iRoot = rbuGetU32(&aBuf[52]) ? 1 : 0;
189730 rbuPutU32(&aBuf[52], iRoot); /* largest root page number */
189731 rbuPutU32(&aBuf[36], 0); /* number of free pages */
189732 rbuPutU32(&aBuf[32], 0); /* first page on free list trunk */
189733 rbuPutU32(&aBuf[28], 1); /* size of db file in pages */
189734 rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1); /* Change counter */
189735
189736 if( iAmt>100 ){
189737 memset(&aBuf[100], 0, iAmt-100);
189738 rbuPutU16(&aBuf[105], iAmt & 0xFFFF);
189739 aBuf[100] = 0x0D;
189740 }
189741 }
189742 }
189743#endif
189744 }
189745 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
189746 /* These look like magic numbers. But they are stable, as they are part
189747 ** of the definition of the SQLite file format, which may not change. */
189748 u8 *pBuf = (u8*)zBuf;
189749 p->iCookie = rbuGetU32(&pBuf[24]);
189750 p->iWriteVer = pBuf[19];
189751 }
189752 }
189753 return rc;
189754}
189755
189756/*
189757** Write data to an rbuVfs-file.
189758*/
189759static int rbuVfsWrite(
189760 sqlite3_file *pFile,
189761 const void *zBuf,
189762 int iAmt,
189763 sqlite_int64 iOfst
189764){
189765 rbu_file *p = (rbu_file*)pFile;
189766 sqlite3rbu *pRbu = p->pRbu;
189767 int rc;
189768
189769 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
189770 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
189771 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
189772 }else{
189773 if( pRbu ){
189774 if( pRbu->eStage==RBU_STAGE_OAL
189775 && (p->openFlags & SQLITE_OPEN_WAL)
189776 && iOfst>=pRbu->iOalSz
189777 ){
189778 pRbu->iOalSz = iAmt + iOfst;
189779 }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE ){
189780 i64 szNew = iAmt+iOfst;
189781 if( szNew>p->sz ){
189782 rc = rbuUpdateTempSize(p, szNew);
189783 if( rc!=SQLITE_OK ) return rc;
189784 }
189785 }
189786 }
189787 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
189788 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
189789 /* These look like magic numbers. But they are stable, as they are part
189790 ** of the definition of the SQLite file format, which may not change. */
189791 u8 *pBuf = (u8*)zBuf;
189792 p->iCookie = rbuGetU32(&pBuf[24]);
189793 p->iWriteVer = pBuf[19];
189794 }
189795 }
189796 return rc;
189797}
189798
189799/*
189800** Truncate an rbuVfs-file.
189801*/
189802static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
189803 rbu_file *p = (rbu_file*)pFile;
189804 if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
189805 int rc = rbuUpdateTempSize(p, size);
189806 if( rc!=SQLITE_OK ) return rc;
189807 }
189808 return p->pReal->pMethods->xTruncate(p->pReal, size);
189809}
189810
189811/*
189812** Sync an rbuVfs-file.
189813*/
189814static int rbuVfsSync(sqlite3_file *pFile, int flags){
189815 rbu_file *p = (rbu_file *)pFile;
189816 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
189817 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
189818 return SQLITE_INTERNAL;
189819 }
189820 return SQLITE_OK;
189821 }
189822 return p->pReal->pMethods->xSync(p->pReal, flags);
189823}
189824
189825/*
189826** Return the current file-size of an rbuVfs-file.
189827*/
189828static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
189829 rbu_file *p = (rbu_file *)pFile;
189830 int rc;
189831 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
189832
189833 /* If this is an RBU vacuum operation and this is the target database,
189834 ** pretend that it has at least one page. Otherwise, SQLite will not
189835 ** check for the existance of a *-wal file. rbuVfsRead() contains
189836 ** similar logic. */
189837 if( rc==SQLITE_OK && *pSize==0
189838 && p->pRbu && rbuIsVacuum(p->pRbu)
189839 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
189840 ){
189841 *pSize = 1024;
189842 }
189843 return rc;
189844}
189845
189846/*
189847** Lock an rbuVfs-file.
189848*/
189849static int rbuVfsLock(sqlite3_file *pFile, int eLock){
189850 rbu_file *p = (rbu_file*)pFile;
189851 sqlite3rbu *pRbu = p->pRbu;
189852 int rc = SQLITE_OK;
189853
189854 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
189855 if( eLock==SQLITE_LOCK_EXCLUSIVE
189856 && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
189857 ){
189858 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
189859 ** prevents it from checkpointing the database from sqlite3_close(). */
189860 rc = SQLITE_BUSY;
189861 }else{
189862 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
189863 }
189864
189865 return rc;
189866}
189867
189868/*
189869** Unlock an rbuVfs-file.
189870*/
189871static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
189872 rbu_file *p = (rbu_file *)pFile;
189873 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
189874}
189875
189876/*
189877** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
189878*/
189879static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
189880 rbu_file *p = (rbu_file *)pFile;
189881 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
189882}
189883
189884/*
189885** File control method. For custom operations on an rbuVfs-file.
189886*/
189887static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
189888 rbu_file *p = (rbu_file *)pFile;
189889 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
189890 int rc;
189891
189892 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
189893 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
189894 );
189895 if( op==SQLITE_FCNTL_RBU ){
189896 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
189897
189898 /* First try to find another RBU vfs lower down in the vfs stack. If
189899 ** one is found, this vfs will operate in pass-through mode. The lower
189900 ** level vfs will do the special RBU handling. */
189901 rc = xControl(p->pReal, op, pArg);
189902
189903 if( rc==SQLITE_NOTFOUND ){
189904 /* Now search for a zipvfs instance lower down in the VFS stack. If
189905 ** one is found, this is an error. */
189906 void *dummy = 0;
189907 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
189908 if( rc==SQLITE_OK ){
189909 rc = SQLITE_ERROR;
189910 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
189911 }else if( rc==SQLITE_NOTFOUND ){
189912 pRbu->pTargetFd = p;
189913 p->pRbu = pRbu;
189914 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
189915 rc = SQLITE_OK;
189916 }
189917 }
189918 return rc;
189919 }
189920 else if( op==SQLITE_FCNTL_RBUCNT ){
189921 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
189922 pRbu->nRbu++;
189923 pRbu->pRbuFd = p;
189924 p->bNolock = 1;
189925 }
189926
189927 rc = xControl(p->pReal, op, pArg);
189928 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
189929 rbu_vfs *pRbuVfs = p->pRbuVfs;
189930 char *zIn = *(char**)pArg;
189931 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
189932 *(char**)pArg = zOut;
189933 if( zOut==0 ) rc = SQLITE_NOMEM;
189934 }
189935
189936 return rc;
189937}
189938
189939/*
189940** Return the sector-size in bytes for an rbuVfs-file.
189941*/
189942static int rbuVfsSectorSize(sqlite3_file *pFile){
189943 rbu_file *p = (rbu_file *)pFile;
189944 return p->pReal->pMethods->xSectorSize(p->pReal);
189945}
189946
189947/*
189948** Return the device characteristic flags supported by an rbuVfs-file.
189949*/
189950static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
189951 rbu_file *p = (rbu_file *)pFile;
189952 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
189953}
189954
189955/*
189956** Take or release a shared-memory lock.
189957*/
189958static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
189959 rbu_file *p = (rbu_file*)pFile;
189960 sqlite3rbu *pRbu = p->pRbu;
189961 int rc = SQLITE_OK;
189962
189963#ifdef SQLITE_AMALGAMATION
189964 assert( WAL_CKPT_LOCK==1 );
189965#endif
189966
189967 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
189968 if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
189969 /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
189970 ** taking this lock also prevents any checkpoints from occurring.
189971 ** todo: really, it's not clear why this might occur, as
189972 ** wal_autocheckpoint ought to be turned off. */
189973 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
189974 }else{
189975 int bCapture = 0;
189976 if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
189977 && pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
189978 && (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
189979 ){
189980 bCapture = 1;
189981 }
189982
189983 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
189984 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
189985 if( bCapture && rc==SQLITE_OK ){
189986 pRbu->mLock |= (1 << ofst);
189987 }
189988 }
189989 }
189990
189991 return rc;
189992}
189993
189994/*
189995** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
189996*/
189997static int rbuVfsShmMap(
189998 sqlite3_file *pFile,
189999 int iRegion,
190000 int szRegion,
190001 int isWrite,
190002 void volatile **pp
190003){
190004 rbu_file *p = (rbu_file*)pFile;
190005 int rc = SQLITE_OK;
190006 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
190007
190008 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
190009 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
190010 ** instead of a file on disk. */
190011 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
190012 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
190013 if( iRegion<=p->nShm ){
190014 int nByte = (iRegion+1) * sizeof(char*);
190015 char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
190016 if( apNew==0 ){
190017 rc = SQLITE_NOMEM;
190018 }else{
190019 memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
190020 p->apShm = apNew;
190021 p->nShm = iRegion+1;
190022 }
190023 }
190024
190025 if( rc==SQLITE_OK && p->apShm[iRegion]==0 ){
190026 char *pNew = (char*)sqlite3_malloc64(szRegion);
190027 if( pNew==0 ){
190028 rc = SQLITE_NOMEM;
190029 }else{
190030 memset(pNew, 0, szRegion);
190031 p->apShm[iRegion] = pNew;
190032 }
190033 }
190034
190035 if( rc==SQLITE_OK ){
190036 *pp = p->apShm[iRegion];
190037 }else{
190038 *pp = 0;
190039 }
190040 }else{
190041 assert( p->apShm==0 );
190042 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
190043 }
190044
190045 return rc;
190046}
190047
190048/*
190049** Memory barrier.
190050*/
190051static void rbuVfsShmBarrier(sqlite3_file *pFile){
190052 rbu_file *p = (rbu_file *)pFile;
190053 p->pReal->pMethods->xShmBarrier(p->pReal);
190054}
190055
190056/*
190057** The xShmUnmap method.
190058*/
190059static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
190060 rbu_file *p = (rbu_file*)pFile;
190061 int rc = SQLITE_OK;
190062 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
190063
190064 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
190065 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
190066 /* no-op */
190067 }else{
190068 /* Release the checkpointer and writer locks */
190069 rbuUnlockShm(p);
190070 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
190071 }
190072 return rc;
190073}
190074
190075/*
190076** Given that zWal points to a buffer containing a wal file name passed to
190077** either the xOpen() or xAccess() VFS method, return a pointer to the
190078** file-handle opened by the same database connection on the corresponding
190079** database file.
190080*/
190081static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal){
190082 rbu_file *pDb;
190083 sqlite3_mutex_enter(pRbuVfs->mutex);
190084 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
190085 sqlite3_mutex_leave(pRbuVfs->mutex);
190086 return pDb;
190087}
190088
190089/*
190090** A main database named zName has just been opened. The following
190091** function returns a pointer to a buffer owned by SQLite that contains
190092** the name of the *-wal file this db connection will use. SQLite
190093** happens to pass a pointer to this buffer when using xAccess()
190094** or xOpen() to operate on the *-wal file.
190095*/
190096static const char *rbuMainToWal(const char *zName, int flags){
190097 int n = (int)strlen(zName);
190098 const char *z = &zName[n];
190099 if( flags & SQLITE_OPEN_URI ){
190100 int odd = 0;
190101 while( 1 ){
190102 if( z[0]==0 ){
190103 odd = 1 - odd;
190104 if( odd && z[1]==0 ) break;
190105 }
190106 z++;
190107 }
190108 z += 2;
190109 }else{
190110 while( *z==0 ) z++;
190111 }
190112 z += (n + 8 + 1);
190113 return z;
190114}
190115
190116/*
190117** Open an rbu file handle.
190118*/
190119static int rbuVfsOpen(
190120 sqlite3_vfs *pVfs,
190121 const char *zName,
190122 sqlite3_file *pFile,
190123 int flags,
190124 int *pOutFlags
190125){
190126 static sqlite3_io_methods rbuvfs_io_methods = {
190127 2, /* iVersion */
190128 rbuVfsClose, /* xClose */
190129 rbuVfsRead, /* xRead */
190130 rbuVfsWrite, /* xWrite */
190131 rbuVfsTruncate, /* xTruncate */
190132 rbuVfsSync, /* xSync */
190133 rbuVfsFileSize, /* xFileSize */
190134 rbuVfsLock, /* xLock */
190135 rbuVfsUnlock, /* xUnlock */
190136 rbuVfsCheckReservedLock, /* xCheckReservedLock */
190137 rbuVfsFileControl, /* xFileControl */
190138 rbuVfsSectorSize, /* xSectorSize */
190139 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
190140 rbuVfsShmMap, /* xShmMap */
190141 rbuVfsShmLock, /* xShmLock */
190142 rbuVfsShmBarrier, /* xShmBarrier */
190143 rbuVfsShmUnmap, /* xShmUnmap */
190144 0, 0 /* xFetch, xUnfetch */
190145 };
190146 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
190147 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
190148 rbu_file *pFd = (rbu_file *)pFile;
190149 int rc = SQLITE_OK;
190150 const char *zOpen = zName;
190151 int oflags = flags;
190152
190153 memset(pFd, 0, sizeof(rbu_file));
190154 pFd->pReal = (sqlite3_file*)&pFd[1];
190155 pFd->pRbuVfs = pRbuVfs;
190156 pFd->openFlags = flags;
190157 if( zName ){
190158 if( flags & SQLITE_OPEN_MAIN_DB ){
190159 /* A main database has just been opened. The following block sets
190160 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
190161 ** the name of the *-wal file this db connection will use. SQLite
190162 ** happens to pass a pointer to this buffer when using xAccess()
190163 ** or xOpen() to operate on the *-wal file. */
190164 pFd->zWal = rbuMainToWal(zName, flags);
190165 }
190166 else if( flags & SQLITE_OPEN_WAL ){
190167 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName);
190168 if( pDb ){
190169 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
190170 /* This call is to open a *-wal file. Intead, open the *-oal. This
190171 ** code ensures that the string passed to xOpen() is terminated by a
190172 ** pair of '\0' bytes in case the VFS attempts to extract a URI
190173 ** parameter from it. */
190174 const char *zBase = zName;
190175 size_t nCopy;
190176 char *zCopy;
190177 if( rbuIsVacuum(pDb->pRbu) ){
190178 zBase = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
190179 zBase = rbuMainToWal(zBase, SQLITE_OPEN_URI);
190180 }
190181 nCopy = strlen(zBase);
190182 zCopy = sqlite3_malloc64(nCopy+2);
190183 if( zCopy ){
190184 memcpy(zCopy, zBase, nCopy);
190185 zCopy[nCopy-3] = 'o';
190186 zCopy[nCopy] = '\0';
190187 zCopy[nCopy+1] = '\0';
190188 zOpen = (const char*)(pFd->zDel = zCopy);
190189 }else{
190190 rc = SQLITE_NOMEM;
190191 }
190192 pFd->pRbu = pDb->pRbu;
190193 }
190194 pDb->pWalFd = pFd;
190195 }
190196 }
190197 }else{
190198 pFd->pRbu = pRbuVfs->pRbu;
190199 }
190200
190201 if( oflags & SQLITE_OPEN_MAIN_DB
190202 && sqlite3_uri_boolean(zName, "rbu_memory", 0)
190203 ){
190204 assert( oflags & SQLITE_OPEN_MAIN_DB );
190205 oflags = SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
190206 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
190207 zOpen = 0;
190208 }
190209
190210 if( rc==SQLITE_OK ){
190211 rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
190212 }
190213 if( pFd->pReal->pMethods ){
190214 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
190215 ** pointer and, if the file is a main database file, link it into the
190216 ** mutex protected linked list of all such files. */
190217 pFile->pMethods = &rbuvfs_io_methods;
190218 if( flags & SQLITE_OPEN_MAIN_DB ){
190219 sqlite3_mutex_enter(pRbuVfs->mutex);
190220 pFd->pMainNext = pRbuVfs->pMain;
190221 pRbuVfs->pMain = pFd;
190222 sqlite3_mutex_leave(pRbuVfs->mutex);
190223 }
190224 }else{
190225 sqlite3_free(pFd->zDel);
190226 }
190227
190228 return rc;
190229}
190230
190231/*
190232** Delete the file located at zPath.
190233*/
190234static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
190235 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
190236 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
190237}
190238
190239/*
190240** Test for access permissions. Return true if the requested permission
190241** is available, or false otherwise.
190242*/
190243static int rbuVfsAccess(
190244 sqlite3_vfs *pVfs,
190245 const char *zPath,
190246 int flags,
190247 int *pResOut
190248){
190249 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
190250 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
190251 int rc;
190252
190253 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
190254
190255 /* If this call is to check if a *-wal file associated with an RBU target
190256 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
190257 ** the following special handling is activated:
190258 **
190259 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
190260 ** ensures that the RBU extension never tries to update a database
190261 ** in wal mode, even if the first page of the database file has
190262 ** been damaged.
190263 **
190264 ** b) if the *-wal file does not exist, claim that it does anyway,
190265 ** causing SQLite to call xOpen() to open it. This call will also
190266 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
190267 ** file opened instead.
190268 */
190269 if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
190270 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath);
190271 if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
190272 if( *pResOut ){
190273 rc = SQLITE_CANTOPEN;
190274 }else{
190275 sqlite3_int64 sz = 0;
190276 rc = rbuVfsFileSize(&pDb->base, &sz);
190277 *pResOut = (sz>0);
190278 }
190279 }
190280 }
190281
190282 return rc;
190283}
190284
190285/*
190286** Populate buffer zOut with the full canonical pathname corresponding
190287** to the pathname in zPath. zOut is guaranteed to point to a buffer
190288** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
190289*/
190290static int rbuVfsFullPathname(
190291 sqlite3_vfs *pVfs,
190292 const char *zPath,
190293 int nOut,
190294 char *zOut
190295){
190296 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
190297 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
190298}
190299
190300#ifndef SQLITE_OMIT_LOAD_EXTENSION
190301/*
190302** Open the dynamic library located at zPath and return a handle.
190303*/
190304static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
190305 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
190306 return pRealVfs->xDlOpen(pRealVfs, zPath);
190307}
190308
190309/*
190310** Populate the buffer zErrMsg (size nByte bytes) with a human readable
190311** utf-8 string describing the most recent error encountered associated
190312** with dynamic libraries.
190313*/
190314static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
190315 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
190316 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
190317}
190318
190319/*
190320** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
190321*/
190322static void (*rbuVfsDlSym(
190323 sqlite3_vfs *pVfs,
190324 void *pArg,
190325 const char *zSym
190326))(void){
190327 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
190328 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
190329}
190330
190331/*
190332** Close the dynamic library handle pHandle.
190333*/
190334static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
190335 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
190336 pRealVfs->xDlClose(pRealVfs, pHandle);
190337}
190338#endif /* SQLITE_OMIT_LOAD_EXTENSION */
190339
190340/*
190341** Populate the buffer pointed to by zBufOut with nByte bytes of
190342** random data.
190343*/
190344static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
190345 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
190346 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
190347}
190348
190349/*
190350** Sleep for nMicro microseconds. Return the number of microseconds
190351** actually slept.
190352*/
190353static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
190354 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
190355 return pRealVfs->xSleep(pRealVfs, nMicro);
190356}
190357
190358/*
190359** Return the current time as a Julian Day number in *pTimeOut.
190360*/
190361static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
190362 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
190363 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
190364}
190365
190366/*
190367** No-op.
190368*/
190369static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
190370 return 0;
190371}
190372
190373/*
190374** Deregister and destroy an RBU vfs created by an earlier call to
190375** sqlite3rbu_create_vfs().
190376*/
190377SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName){
190378 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
190379 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
190380 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
190381 sqlite3_vfs_unregister(pVfs);
190382 sqlite3_free(pVfs);
190383 }
190384}
190385
190386/*
190387** Create an RBU VFS named zName that accesses the underlying file-system
190388** via existing VFS zParent. The new object is registered as a non-default
190389** VFS with SQLite before returning.
190390*/
190391SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
190392
190393 /* Template for VFS */
190394 static sqlite3_vfs vfs_template = {
190395 1, /* iVersion */
190396 0, /* szOsFile */
190397 0, /* mxPathname */
190398 0, /* pNext */
190399 0, /* zName */
190400 0, /* pAppData */
190401 rbuVfsOpen, /* xOpen */
190402 rbuVfsDelete, /* xDelete */
190403 rbuVfsAccess, /* xAccess */
190404 rbuVfsFullPathname, /* xFullPathname */
190405
190406#ifndef SQLITE_OMIT_LOAD_EXTENSION
190407 rbuVfsDlOpen, /* xDlOpen */
190408 rbuVfsDlError, /* xDlError */
190409 rbuVfsDlSym, /* xDlSym */
190410 rbuVfsDlClose, /* xDlClose */
190411#else
190412 0, 0, 0, 0,
190413#endif
190414
190415 rbuVfsRandomness, /* xRandomness */
190416 rbuVfsSleep, /* xSleep */
190417 rbuVfsCurrentTime, /* xCurrentTime */
190418 rbuVfsGetLastError, /* xGetLastError */
190419 0, /* xCurrentTimeInt64 (version 2) */
190420 0, 0, 0 /* Unimplemented version 3 methods */
190421 };
190422
190423 rbu_vfs *pNew = 0; /* Newly allocated VFS */
190424 int rc = SQLITE_OK;
190425 size_t nName;
190426 size_t nByte;
190427
190428 nName = strlen(zName);
190429 nByte = sizeof(rbu_vfs) + nName + 1;
190430 pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
190431 if( pNew==0 ){
190432 rc = SQLITE_NOMEM;
190433 }else{
190434 sqlite3_vfs *pParent; /* Parent VFS */
190435 memset(pNew, 0, nByte);
190436 pParent = sqlite3_vfs_find(zParent);
190437 if( pParent==0 ){
190438 rc = SQLITE_NOTFOUND;
190439 }else{
190440 char *zSpace;
190441 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
190442 pNew->base.mxPathname = pParent->mxPathname;
190443 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
190444 pNew->pRealVfs = pParent;
190445 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
190446 memcpy(zSpace, zName, nName);
190447
190448 /* Allocate the mutex and register the new VFS (not as the default) */
190449 pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
190450 if( pNew->mutex==0 ){
190451 rc = SQLITE_NOMEM;
190452 }else{
190453 rc = sqlite3_vfs_register(&pNew->base, 0);
190454 }
190455 }
190456
190457 if( rc!=SQLITE_OK ){
190458 sqlite3_mutex_free(pNew->mutex);
190459 sqlite3_free(pNew);
190460 }
190461 }
190462
190463 return rc;
190464}
190465
190466/*
190467** Configure the aggregate temp file size limit for this RBU handle.
190468*/
190469SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu *pRbu, sqlite3_int64 n){
190470 if( n>=0 ){
190471 pRbu->szTempLimit = n;
190472 }
190473 return pRbu->szTempLimit;
190474}
190475
190476SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu){
190477 return pRbu->szTemp;
190478}
190479
190480
190481/**************************************************************************/
190482
190483#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
190484
190485/************** End of sqlite3rbu.c ******************************************/
190486/************** Begin file dbstat.c ******************************************/
190487/*
190488** 2010 July 12
190489**
190490** The author disclaims copyright to this source code. In place of
190491** a legal notice, here is a blessing:
190492**
190493** May you do good and not evil.
190494** May you find forgiveness for yourself and forgive others.
190495** May you share freely, never taking more than you give.
190496**
190497******************************************************************************
190498**
190499** This file contains an implementation of the "dbstat" virtual table.
190500**
190501** The dbstat virtual table is used to extract low-level formatting
190502** information from an SQLite database in order to implement the
190503** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
190504** for an example implementation.
190505**
190506** Additional information is available on the "dbstat.html" page of the
190507** official SQLite documentation.
190508*/
190509
190510/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
190511#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
190512 && !defined(SQLITE_OMIT_VIRTUALTABLE)
190513
190514/*
190515** Page paths:
190516**
190517** The value of the 'path' column describes the path taken from the
190518** root-node of the b-tree structure to each page. The value of the
190519** root-node path is '/'.
190520**
190521** The value of the path for the left-most child page of the root of
190522** a b-tree is '/000/'. (Btrees store content ordered from left to right
190523** so the pages to the left have smaller keys than the pages to the right.)
190524** The next to left-most child of the root page is
190525** '/001', and so on, each sibling page identified by a 3-digit hex
190526** value. The children of the 451st left-most sibling have paths such
190527** as '/1c2/000/, '/1c2/001/' etc.
190528**
190529** Overflow pages are specified by appending a '+' character and a
190530** six-digit hexadecimal value to the path to the cell they are linked
190531** from. For example, the three overflow pages in a chain linked from
190532** the left-most cell of the 450th child of the root page are identified
190533** by the paths:
190534**
190535** '/1c2/000+000000' // First page in overflow chain
190536** '/1c2/000+000001' // Second page in overflow chain
190537** '/1c2/000+000002' // Third page in overflow chain
190538**
190539** If the paths are sorted using the BINARY collation sequence, then
190540** the overflow pages associated with a cell will appear earlier in the
190541** sort-order than its child page:
190542**
190543** '/1c2/000/' // Left-most child of 451st child of root
190544*/
190545#define VTAB_SCHEMA \
190546 "CREATE TABLE xx( " \
190547 " name TEXT, /* Name of table or index */" \
190548 " path TEXT, /* Path to page from root */" \
190549 " pageno INTEGER, /* Page number */" \
190550 " pagetype TEXT, /* 'internal', 'leaf' or 'overflow' */" \
190551 " ncell INTEGER, /* Cells on page (0 for overflow) */" \
190552 " payload INTEGER, /* Bytes of payload on this page */" \
190553 " unused INTEGER, /* Bytes of unused space on this page */" \
190554 " mx_payload INTEGER, /* Largest payload size of all cells */" \
190555 " pgoffset INTEGER, /* Offset of page in file */" \
190556 " pgsize INTEGER, /* Size of the page */" \
190557 " schema TEXT HIDDEN /* Database schema being analyzed */" \
190558 ");"
190559
190560
190561typedef struct StatTable StatTable;
190562typedef struct StatCursor StatCursor;
190563typedef struct StatPage StatPage;
190564typedef struct StatCell StatCell;
190565
190566struct StatCell {
190567 int nLocal; /* Bytes of local payload */
190568 u32 iChildPg; /* Child node (or 0 if this is a leaf) */
190569 int nOvfl; /* Entries in aOvfl[] */
190570 u32 *aOvfl; /* Array of overflow page numbers */
190571 int nLastOvfl; /* Bytes of payload on final overflow page */
190572 int iOvfl; /* Iterates through aOvfl[] */
190573};
190574
190575struct StatPage {
190576 u32 iPgno;
190577 DbPage *pPg;
190578 int iCell;
190579
190580 char *zPath; /* Path to this page */
190581
190582 /* Variables populated by statDecodePage(): */
190583 u8 flags; /* Copy of flags byte */
190584 int nCell; /* Number of cells on page */
190585 int nUnused; /* Number of unused bytes on page */
190586 StatCell *aCell; /* Array of parsed cells */
190587 u32 iRightChildPg; /* Right-child page number (or 0) */
190588 int nMxPayload; /* Largest payload of any cell on this page */
190589};
190590
190591struct StatCursor {
190592 sqlite3_vtab_cursor base;
190593 sqlite3_stmt *pStmt; /* Iterates through set of root pages */
190594 int isEof; /* After pStmt has returned SQLITE_DONE */
190595 int iDb; /* Schema used for this query */
190596
190597 StatPage aPage[32];
190598 int iPage; /* Current entry in aPage[] */
190599
190600 /* Values to return. */
190601 char *zName; /* Value of 'name' column */
190602 char *zPath; /* Value of 'path' column */
190603 u32 iPageno; /* Value of 'pageno' column */
190604 char *zPagetype; /* Value of 'pagetype' column */
190605 int nCell; /* Value of 'ncell' column */
190606 int nPayload; /* Value of 'payload' column */
190607 int nUnused; /* Value of 'unused' column */
190608 int nMxPayload; /* Value of 'mx_payload' column */
190609 i64 iOffset; /* Value of 'pgOffset' column */
190610 int szPage; /* Value of 'pgSize' column */
190611};
190612
190613struct StatTable {
190614 sqlite3_vtab base;
190615 sqlite3 *db;
190616 int iDb; /* Index of database to analyze */
190617};
190618
190619#ifndef get2byte
190620# define get2byte(x) ((x)[0]<<8 | (x)[1])
190621#endif
190622
190623/*
190624** Connect to or create a statvfs virtual table.
190625*/
190626static int statConnect(
190627 sqlite3 *db,
190628 void *pAux,
190629 int argc, const char *const*argv,
190630 sqlite3_vtab **ppVtab,
190631 char **pzErr
190632){
190633 StatTable *pTab = 0;
190634 int rc = SQLITE_OK;
190635 int iDb;
190636
190637 if( argc>=4 ){
190638 Token nm;
190639 sqlite3TokenInit(&nm, (char*)argv[3]);
190640 iDb = sqlite3FindDb(db, &nm);
190641 if( iDb<0 ){
190642 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
190643 return SQLITE_ERROR;
190644 }
190645 }else{
190646 iDb = 0;
190647 }
190648 rc = sqlite3_declare_vtab(db, VTAB_SCHEMA);
190649 if( rc==SQLITE_OK ){
190650 pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
190651 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
190652 }
190653
190654 assert( rc==SQLITE_OK || pTab==0 );
190655 if( rc==SQLITE_OK ){
190656 memset(pTab, 0, sizeof(StatTable));
190657 pTab->db = db;
190658 pTab->iDb = iDb;
190659 }
190660
190661 *ppVtab = (sqlite3_vtab*)pTab;
190662 return rc;
190663}
190664
190665/*
190666** Disconnect from or destroy a statvfs virtual table.
190667*/
190668static int statDisconnect(sqlite3_vtab *pVtab){
190669 sqlite3_free(pVtab);
190670 return SQLITE_OK;
190671}
190672
190673/*
190674** There is no "best-index". This virtual table always does a linear
190675** scan. However, a schema=? constraint should cause this table to
190676** operate on a different database schema, so check for it.
190677**
190678** idxNum is normally 0, but will be 1 if a schema=? constraint exists.
190679*/
190680static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
190681 int i;
190682
190683 pIdxInfo->estimatedCost = 1.0e6; /* Initial cost estimate */
190684
190685 /* Look for a valid schema=? constraint. If found, change the idxNum to
190686 ** 1 and request the value of that constraint be sent to xFilter. And
190687 ** lower the cost estimate to encourage the constrained version to be
190688 ** used.
190689 */
190690 for(i=0; i<pIdxInfo->nConstraint; i++){
190691 if( pIdxInfo->aConstraint[i].usable==0 ) continue;
190692 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
190693 if( pIdxInfo->aConstraint[i].iColumn!=10 ) continue;
190694 pIdxInfo->idxNum = 1;
190695 pIdxInfo->estimatedCost = 1.0;
190696 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
190697 pIdxInfo->aConstraintUsage[i].omit = 1;
190698 break;
190699 }
190700
190701
190702 /* Records are always returned in ascending order of (name, path).
190703 ** If this will satisfy the client, set the orderByConsumed flag so that
190704 ** SQLite does not do an external sort.
190705 */
190706 if( ( pIdxInfo->nOrderBy==1
190707 && pIdxInfo->aOrderBy[0].iColumn==0
190708 && pIdxInfo->aOrderBy[0].desc==0
190709 ) ||
190710 ( pIdxInfo->nOrderBy==2
190711 && pIdxInfo->aOrderBy[0].iColumn==0
190712 && pIdxInfo->aOrderBy[0].desc==0
190713 && pIdxInfo->aOrderBy[1].iColumn==1
190714 && pIdxInfo->aOrderBy[1].desc==0
190715 )
190716 ){
190717 pIdxInfo->orderByConsumed = 1;
190718 }
190719
190720 return SQLITE_OK;
190721}
190722
190723/*
190724** Open a new statvfs cursor.
190725*/
190726static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
190727 StatTable *pTab = (StatTable *)pVTab;
190728 StatCursor *pCsr;
190729
190730 pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
190731 if( pCsr==0 ){
190732 return SQLITE_NOMEM_BKPT;
190733 }else{
190734 memset(pCsr, 0, sizeof(StatCursor));
190735 pCsr->base.pVtab = pVTab;
190736 pCsr->iDb = pTab->iDb;
190737 }
190738
190739 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
190740 return SQLITE_OK;
190741}
190742
190743static void statClearPage(StatPage *p){
190744 int i;
190745 if( p->aCell ){
190746 for(i=0; i<p->nCell; i++){
190747 sqlite3_free(p->aCell[i].aOvfl);
190748 }
190749 sqlite3_free(p->aCell);
190750 }
190751 sqlite3PagerUnref(p->pPg);
190752 sqlite3_free(p->zPath);
190753 memset(p, 0, sizeof(StatPage));
190754}
190755
190756static void statResetCsr(StatCursor *pCsr){
190757 int i;
190758 sqlite3_reset(pCsr->pStmt);
190759 for(i=0; i<ArraySize(pCsr->aPage); i++){
190760 statClearPage(&pCsr->aPage[i]);
190761 }
190762 pCsr->iPage = 0;
190763 sqlite3_free(pCsr->zPath);
190764 pCsr->zPath = 0;
190765 pCsr->isEof = 0;
190766}
190767
190768/*
190769** Close a statvfs cursor.
190770*/
190771static int statClose(sqlite3_vtab_cursor *pCursor){
190772 StatCursor *pCsr = (StatCursor *)pCursor;
190773 statResetCsr(pCsr);
190774 sqlite3_finalize(pCsr->pStmt);
190775 sqlite3_free(pCsr);
190776 return SQLITE_OK;
190777}
190778
190779static void getLocalPayload(
190780 int nUsable, /* Usable bytes per page */
190781 u8 flags, /* Page flags */
190782 int nTotal, /* Total record (payload) size */
190783 int *pnLocal /* OUT: Bytes stored locally */
190784){
190785 int nLocal;
190786 int nMinLocal;
190787 int nMaxLocal;
190788
190789 if( flags==0x0D ){ /* Table leaf node */
190790 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
190791 nMaxLocal = nUsable - 35;
190792 }else{ /* Index interior and leaf nodes */
190793 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
190794 nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
190795 }
190796
190797 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
190798 if( nLocal>nMaxLocal ) nLocal = nMinLocal;
190799 *pnLocal = nLocal;
190800}
190801
190802static int statDecodePage(Btree *pBt, StatPage *p){
190803 int nUnused;
190804 int iOff;
190805 int nHdr;
190806 int isLeaf;
190807 int szPage;
190808
190809 u8 *aData = sqlite3PagerGetData(p->pPg);
190810 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
190811
190812 p->flags = aHdr[0];
190813 p->nCell = get2byte(&aHdr[3]);
190814 p->nMxPayload = 0;
190815
190816 isLeaf = (p->flags==0x0A || p->flags==0x0D);
190817 nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100;
190818
190819 nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
190820 nUnused += (int)aHdr[7];
190821 iOff = get2byte(&aHdr[1]);
190822 while( iOff ){
190823 nUnused += get2byte(&aData[iOff+2]);
190824 iOff = get2byte(&aData[iOff]);
190825 }
190826 p->nUnused = nUnused;
190827 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
190828 szPage = sqlite3BtreeGetPageSize(pBt);
190829
190830 if( p->nCell ){
190831 int i; /* Used to iterate through cells */
190832 int nUsable; /* Usable bytes per page */
190833
190834 sqlite3BtreeEnter(pBt);
190835 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
190836 sqlite3BtreeLeave(pBt);
190837 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
190838 if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
190839 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
190840
190841 for(i=0; i<p->nCell; i++){
190842 StatCell *pCell = &p->aCell[i];
190843
190844 iOff = get2byte(&aData[nHdr+i*2]);
190845 if( !isLeaf ){
190846 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
190847 iOff += 4;
190848 }
190849 if( p->flags==0x05 ){
190850 /* A table interior node. nPayload==0. */
190851 }else{
190852 u32 nPayload; /* Bytes of payload total (local+overflow) */
190853 int nLocal; /* Bytes of payload stored locally */
190854 iOff += getVarint32(&aData[iOff], nPayload);
190855 if( p->flags==0x0D ){
190856 u64 dummy;
190857 iOff += sqlite3GetVarint(&aData[iOff], &dummy);
190858 }
190859 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
190860 getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
190861 pCell->nLocal = nLocal;
190862 assert( nLocal>=0 );
190863 assert( nPayload>=(u32)nLocal );
190864 assert( nLocal<=(nUsable-35) );
190865 if( nPayload>(u32)nLocal ){
190866 int j;
190867 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
190868 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
190869 pCell->nOvfl = nOvfl;
190870 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
190871 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
190872 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
190873 for(j=1; j<nOvfl; j++){
190874 int rc;
190875 u32 iPrev = pCell->aOvfl[j-1];
190876 DbPage *pPg = 0;
190877 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
190878 if( rc!=SQLITE_OK ){
190879 assert( pPg==0 );
190880 return rc;
190881 }
190882 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
190883 sqlite3PagerUnref(pPg);
190884 }
190885 }
190886 }
190887 }
190888 }
190889
190890 return SQLITE_OK;
190891}
190892
190893/*
190894** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
190895** the current value of pCsr->iPageno.
190896*/
190897static void statSizeAndOffset(StatCursor *pCsr){
190898 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
190899 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
190900 Pager *pPager = sqlite3BtreePager(pBt);
190901 sqlite3_file *fd;
190902 sqlite3_int64 x[2];
190903
190904 /* The default page size and offset */
190905 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
190906 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
190907
190908 /* If connected to a ZIPVFS backend, override the page size and
190909 ** offset with actual values obtained from ZIPVFS.
190910 */
190911 fd = sqlite3PagerFile(pPager);
190912 x[0] = pCsr->iPageno;
190913 if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
190914 pCsr->iOffset = x[0];
190915 pCsr->szPage = (int)x[1];
190916 }
190917}
190918
190919/*
190920** Move a statvfs cursor to the next entry in the file.
190921*/
190922static int statNext(sqlite3_vtab_cursor *pCursor){
190923 int rc;
190924 int nPayload;
190925 char *z;
190926 StatCursor *pCsr = (StatCursor *)pCursor;
190927 StatTable *pTab = (StatTable *)pCursor->pVtab;
190928 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
190929 Pager *pPager = sqlite3BtreePager(pBt);
190930
190931 sqlite3_free(pCsr->zPath);
190932 pCsr->zPath = 0;
190933
190934statNextRestart:
190935 if( pCsr->aPage[0].pPg==0 ){
190936 rc = sqlite3_step(pCsr->pStmt);
190937 if( rc==SQLITE_ROW ){
190938 int nPage;
190939 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
190940 sqlite3PagerPagecount(pPager, &nPage);
190941 if( nPage==0 ){
190942 pCsr->isEof = 1;
190943 return sqlite3_reset(pCsr->pStmt);
190944 }
190945 rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0);
190946 pCsr->aPage[0].iPgno = iRoot;
190947 pCsr->aPage[0].iCell = 0;
190948 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
190949 pCsr->iPage = 0;
190950 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
190951 }else{
190952 pCsr->isEof = 1;
190953 return sqlite3_reset(pCsr->pStmt);
190954 }
190955 }else{
190956
190957 /* Page p itself has already been visited. */
190958 StatPage *p = &pCsr->aPage[pCsr->iPage];
190959
190960 while( p->iCell<p->nCell ){
190961 StatCell *pCell = &p->aCell[p->iCell];
190962 if( pCell->iOvfl<pCell->nOvfl ){
190963 int nUsable;
190964 sqlite3BtreeEnter(pBt);
190965 nUsable = sqlite3BtreeGetPageSize(pBt) -
190966 sqlite3BtreeGetReserveNoMutex(pBt);
190967 sqlite3BtreeLeave(pBt);
190968 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
190969 pCsr->iPageno = pCell->aOvfl[pCell->iOvfl];
190970 pCsr->zPagetype = "overflow";
190971 pCsr->nCell = 0;
190972 pCsr->nMxPayload = 0;
190973 pCsr->zPath = z = sqlite3_mprintf(
190974 "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl
190975 );
190976 if( pCell->iOvfl<pCell->nOvfl-1 ){
190977 pCsr->nUnused = 0;
190978 pCsr->nPayload = nUsable - 4;
190979 }else{
190980 pCsr->nPayload = pCell->nLastOvfl;
190981 pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
190982 }
190983 pCell->iOvfl++;
190984 statSizeAndOffset(pCsr);
190985 return z==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
190986 }
190987 if( p->iRightChildPg ) break;
190988 p->iCell++;
190989 }
190990
190991 if( !p->iRightChildPg || p->iCell>p->nCell ){
190992 statClearPage(p);
190993 if( pCsr->iPage==0 ) return statNext(pCursor);
190994 pCsr->iPage--;
190995 goto statNextRestart; /* Tail recursion */
190996 }
190997 pCsr->iPage++;
190998 assert( p==&pCsr->aPage[pCsr->iPage-1] );
190999
191000 if( p->iCell==p->nCell ){
191001 p[1].iPgno = p->iRightChildPg;
191002 }else{
191003 p[1].iPgno = p->aCell[p->iCell].iChildPg;
191004 }
191005 rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg, 0);
191006 p[1].iCell = 0;
191007 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
191008 p->iCell++;
191009 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
191010 }
191011
191012
191013 /* Populate the StatCursor fields with the values to be returned
191014 ** by the xColumn() and xRowid() methods.
191015 */
191016 if( rc==SQLITE_OK ){
191017 int i;
191018 StatPage *p = &pCsr->aPage[pCsr->iPage];
191019 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
191020 pCsr->iPageno = p->iPgno;
191021
191022 rc = statDecodePage(pBt, p);
191023 if( rc==SQLITE_OK ){
191024 statSizeAndOffset(pCsr);
191025
191026 switch( p->flags ){
191027 case 0x05: /* table internal */
191028 case 0x02: /* index internal */
191029 pCsr->zPagetype = "internal";
191030 break;
191031 case 0x0D: /* table leaf */
191032 case 0x0A: /* index leaf */
191033 pCsr->zPagetype = "leaf";
191034 break;
191035 default:
191036 pCsr->zPagetype = "corrupted";
191037 break;
191038 }
191039 pCsr->nCell = p->nCell;
191040 pCsr->nUnused = p->nUnused;
191041 pCsr->nMxPayload = p->nMxPayload;
191042 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
191043 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
191044 nPayload = 0;
191045 for(i=0; i<p->nCell; i++){
191046 nPayload += p->aCell[i].nLocal;
191047 }
191048 pCsr->nPayload = nPayload;
191049 }
191050 }
191051
191052 return rc;
191053}
191054
191055static int statEof(sqlite3_vtab_cursor *pCursor){
191056 StatCursor *pCsr = (StatCursor *)pCursor;
191057 return pCsr->isEof;
191058}
191059
191060static int statFilter(
191061 sqlite3_vtab_cursor *pCursor,
191062 int idxNum, const char *idxStr,
191063 int argc, sqlite3_value **argv
191064){
191065 StatCursor *pCsr = (StatCursor *)pCursor;
191066 StatTable *pTab = (StatTable*)(pCursor->pVtab);
191067 char *zSql;
191068 int rc = SQLITE_OK;
191069 char *zMaster;
191070
191071 if( idxNum==1 ){
191072 const char *zDbase = (const char*)sqlite3_value_text(argv[0]);
191073 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
191074 if( pCsr->iDb<0 ){
191075 sqlite3_free(pCursor->pVtab->zErrMsg);
191076 pCursor->pVtab->zErrMsg = sqlite3_mprintf("no such schema: %s", zDbase);
191077 return pCursor->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM_BKPT;
191078 }
191079 }else{
191080 pCsr->iDb = pTab->iDb;
191081 }
191082 statResetCsr(pCsr);
191083 sqlite3_finalize(pCsr->pStmt);
191084 pCsr->pStmt = 0;
191085 zMaster = pCsr->iDb==1 ? "sqlite_temp_master" : "sqlite_master";
191086 zSql = sqlite3_mprintf(
191087 "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type"
191088 " UNION ALL "
191089 "SELECT name, rootpage, type"
191090 " FROM \"%w\".%s WHERE rootpage!=0"
191091 " ORDER BY name", pTab->db->aDb[pCsr->iDb].zDbSName, zMaster);
191092 if( zSql==0 ){
191093 return SQLITE_NOMEM_BKPT;
191094 }else{
191095 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
191096 sqlite3_free(zSql);
191097 }
191098
191099 if( rc==SQLITE_OK ){
191100 rc = statNext(pCursor);
191101 }
191102 return rc;
191103}
191104
191105static int statColumn(
191106 sqlite3_vtab_cursor *pCursor,
191107 sqlite3_context *ctx,
191108 int i
191109){
191110 StatCursor *pCsr = (StatCursor *)pCursor;
191111 switch( i ){
191112 case 0: /* name */
191113 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
191114 break;
191115 case 1: /* path */
191116 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
191117 break;
191118 case 2: /* pageno */
191119 sqlite3_result_int64(ctx, pCsr->iPageno);
191120 break;
191121 case 3: /* pagetype */
191122 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
191123 break;
191124 case 4: /* ncell */
191125 sqlite3_result_int(ctx, pCsr->nCell);
191126 break;
191127 case 5: /* payload */
191128 sqlite3_result_int(ctx, pCsr->nPayload);
191129 break;
191130 case 6: /* unused */
191131 sqlite3_result_int(ctx, pCsr->nUnused);
191132 break;
191133 case 7: /* mx_payload */
191134 sqlite3_result_int(ctx, pCsr->nMxPayload);
191135 break;
191136 case 8: /* pgoffset */
191137 sqlite3_result_int64(ctx, pCsr->iOffset);
191138 break;
191139 case 9: /* pgsize */
191140 sqlite3_result_int(ctx, pCsr->szPage);
191141 break;
191142 default: { /* schema */
191143 sqlite3 *db = sqlite3_context_db_handle(ctx);
191144 int iDb = pCsr->iDb;
191145 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
191146 break;
191147 }
191148 }
191149 return SQLITE_OK;
191150}
191151
191152static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
191153 StatCursor *pCsr = (StatCursor *)pCursor;
191154 *pRowid = pCsr->iPageno;
191155 return SQLITE_OK;
191156}
191157
191158/*
191159** Invoke this routine to register the "dbstat" virtual table module
191160*/
191161SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){
191162 static sqlite3_module dbstat_module = {
191163 0, /* iVersion */
191164 statConnect, /* xCreate */
191165 statConnect, /* xConnect */
191166 statBestIndex, /* xBestIndex */
191167 statDisconnect, /* xDisconnect */
191168 statDisconnect, /* xDestroy */
191169 statOpen, /* xOpen - open a cursor */
191170 statClose, /* xClose - close a cursor */
191171 statFilter, /* xFilter - configure scan constraints */
191172 statNext, /* xNext - advance a cursor */
191173 statEof, /* xEof - check for end of scan */
191174 statColumn, /* xColumn - read data */
191175 statRowid, /* xRowid - read data */
191176 0, /* xUpdate */
191177 0, /* xBegin */
191178 0, /* xSync */
191179 0, /* xCommit */
191180 0, /* xRollback */
191181 0, /* xFindMethod */
191182 0, /* xRename */
191183 0, /* xSavepoint */
191184 0, /* xRelease */
191185 0, /* xRollbackTo */
191186 };
191187 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
191188}
191189#elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
191190SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
191191#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
191192
191193/************** End of dbstat.c **********************************************/
191194/************** Begin file dbpage.c ******************************************/
191195/*
191196** 2017-10-11
191197**
191198** The author disclaims copyright to this source code. In place of
191199** a legal notice, here is a blessing:
191200**
191201** May you do good and not evil.
191202** May you find forgiveness for yourself and forgive others.
191203** May you share freely, never taking more than you give.
191204**
191205******************************************************************************
191206**
191207** This file contains an implementation of the "sqlite_dbpage" virtual table.
191208**
191209** The sqlite_dbpage virtual table is used to read or write whole raw
191210** pages of the database file. The pager interface is used so that
191211** uncommitted changes and changes recorded in the WAL file are correctly
191212** retrieved.
191213**
191214** Usage example:
191215**
191216** SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123;
191217**
191218** This is an eponymous virtual table so it does not need to be created before
191219** use. The optional argument to the sqlite_dbpage() table name is the
191220** schema for the database file that is to be read. The default schema is
191221** "main".
191222**
191223** The data field of sqlite_dbpage table can be updated. The new
191224** value must be a BLOB which is the correct page size, otherwise the
191225** update fails. Rows may not be deleted or inserted.
191226*/
191227
191228/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
191229#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
191230 && !defined(SQLITE_OMIT_VIRTUALTABLE)
191231
191232typedef struct DbpageTable DbpageTable;
191233typedef struct DbpageCursor DbpageCursor;
191234
191235struct DbpageCursor {
191236 sqlite3_vtab_cursor base; /* Base class. Must be first */
191237 int pgno; /* Current page number */
191238 int mxPgno; /* Last page to visit on this scan */
191239 Pager *pPager; /* Pager being read/written */
191240 DbPage *pPage1; /* Page 1 of the database */
191241 int iDb; /* Index of database to analyze */
191242 int szPage; /* Size of each page in bytes */
191243};
191244
191245struct DbpageTable {
191246 sqlite3_vtab base; /* Base class. Must be first */
191247 sqlite3 *db; /* The database */
191248};
191249
191250/* Columns */
191251#define DBPAGE_COLUMN_PGNO 0
191252#define DBPAGE_COLUMN_DATA 1
191253#define DBPAGE_COLUMN_SCHEMA 2
191254
191255
191256
191257/*
191258** Connect to or create a dbpagevfs virtual table.
191259*/
191260static int dbpageConnect(
191261 sqlite3 *db,
191262 void *pAux,
191263 int argc, const char *const*argv,
191264 sqlite3_vtab **ppVtab,
191265 char **pzErr
191266){
191267 DbpageTable *pTab = 0;
191268 int rc = SQLITE_OK;
191269
191270 rc = sqlite3_declare_vtab(db,
191271 "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)");
191272 if( rc==SQLITE_OK ){
191273 pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable));
191274 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
191275 }
191276
191277 assert( rc==SQLITE_OK || pTab==0 );
191278 if( rc==SQLITE_OK ){
191279 memset(pTab, 0, sizeof(DbpageTable));
191280 pTab->db = db;
191281 }
191282
191283 *ppVtab = (sqlite3_vtab*)pTab;
191284 return rc;
191285}
191286
191287/*
191288** Disconnect from or destroy a dbpagevfs virtual table.
191289*/
191290static int dbpageDisconnect(sqlite3_vtab *pVtab){
191291 sqlite3_free(pVtab);
191292 return SQLITE_OK;
191293}
191294
191295/*
191296** idxNum:
191297**
191298** 0 schema=main, full table scan
191299** 1 schema=main, pgno=?1
191300** 2 schema=?1, full table scan
191301** 3 schema=?1, pgno=?2
191302*/
191303static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
191304 int i;
191305 int iPlan = 0;
191306
191307 /* If there is a schema= constraint, it must be honored. Report a
191308 ** ridiculously large estimated cost if the schema= constraint is
191309 ** unavailable
191310 */
191311 for(i=0; i<pIdxInfo->nConstraint; i++){
191312 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
191313 if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
191314 if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
191315 if( !p->usable ){
191316 /* No solution. Use the default SQLITE_BIG_DBL cost */
191317 pIdxInfo->estimatedRows = 0x7fffffff;
191318 return SQLITE_OK;
191319 }
191320 iPlan = 2;
191321 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
191322 pIdxInfo->aConstraintUsage[i].omit = 1;
191323 break;
191324 }
191325
191326 /* If we reach this point, it means that either there is no schema=
191327 ** constraint (in which case we use the "main" schema) or else the
191328 ** schema constraint was accepted. Lower the estimated cost accordingly
191329 */
191330 pIdxInfo->estimatedCost = 1.0e6;
191331
191332 /* Check for constraints against pgno */
191333 for(i=0; i<pIdxInfo->nConstraint; i++){
191334 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
191335 if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
191336 pIdxInfo->estimatedRows = 1;
191337 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
191338 pIdxInfo->estimatedCost = 1.0;
191339 pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1;
191340 pIdxInfo->aConstraintUsage[i].omit = 1;
191341 iPlan |= 1;
191342 break;
191343 }
191344 }
191345 pIdxInfo->idxNum = iPlan;
191346
191347 if( pIdxInfo->nOrderBy>=1
191348 && pIdxInfo->aOrderBy[0].iColumn<=0
191349 && pIdxInfo->aOrderBy[0].desc==0
191350 ){
191351 pIdxInfo->orderByConsumed = 1;
191352 }
191353 return SQLITE_OK;
191354}
191355
191356/*
191357** Open a new dbpagevfs cursor.
191358*/
191359static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
191360 DbpageCursor *pCsr;
191361
191362 pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor));
191363 if( pCsr==0 ){
191364 return SQLITE_NOMEM_BKPT;
191365 }else{
191366 memset(pCsr, 0, sizeof(DbpageCursor));
191367 pCsr->base.pVtab = pVTab;
191368 pCsr->pgno = -1;
191369 }
191370
191371 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
191372 return SQLITE_OK;
191373}
191374
191375/*
191376** Close a dbpagevfs cursor.
191377*/
191378static int dbpageClose(sqlite3_vtab_cursor *pCursor){
191379 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
191380 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
191381 sqlite3_free(pCsr);
191382 return SQLITE_OK;
191383}
191384
191385/*
191386** Move a dbpagevfs cursor to the next entry in the file.
191387*/
191388static int dbpageNext(sqlite3_vtab_cursor *pCursor){
191389 int rc = SQLITE_OK;
191390 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
191391 pCsr->pgno++;
191392 return rc;
191393}
191394
191395static int dbpageEof(sqlite3_vtab_cursor *pCursor){
191396 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
191397 return pCsr->pgno > pCsr->mxPgno;
191398}
191399
191400/*
191401** idxNum:
191402**
191403** 0 schema=main, full table scan
191404** 1 schema=main, pgno=?1
191405** 2 schema=?1, full table scan
191406** 3 schema=?1, pgno=?2
191407**
191408** idxStr is not used
191409*/
191410static int dbpageFilter(
191411 sqlite3_vtab_cursor *pCursor,
191412 int idxNum, const char *idxStr,
191413 int argc, sqlite3_value **argv
191414){
191415 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
191416 DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
191417 int rc;
191418 sqlite3 *db = pTab->db;
191419 Btree *pBt;
191420
191421 /* Default setting is no rows of result */
191422 pCsr->pgno = 1;
191423 pCsr->mxPgno = 0;
191424
191425 if( idxNum & 2 ){
191426 const char *zSchema;
191427 assert( argc>=1 );
191428 zSchema = (const char*)sqlite3_value_text(argv[0]);
191429 pCsr->iDb = sqlite3FindDbName(db, zSchema);
191430 if( pCsr->iDb<0 ) return SQLITE_OK;
191431 }else{
191432 pCsr->iDb = 0;
191433 }
191434 pBt = db->aDb[pCsr->iDb].pBt;
191435 if( pBt==0 ) return SQLITE_OK;
191436 pCsr->pPager = sqlite3BtreePager(pBt);
191437 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
191438 pCsr->mxPgno = sqlite3BtreeLastPage(pBt);
191439 if( idxNum & 1 ){
191440 assert( argc>(idxNum>>1) );
191441 pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]);
191442 if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){
191443 pCsr->pgno = 1;
191444 pCsr->mxPgno = 0;
191445 }else{
191446 pCsr->mxPgno = pCsr->pgno;
191447 }
191448 }else{
191449 assert( pCsr->pgno==1 );
191450 }
191451 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
191452 rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
191453 return rc;
191454}
191455
191456static int dbpageColumn(
191457 sqlite3_vtab_cursor *pCursor,
191458 sqlite3_context *ctx,
191459 int i
191460){
191461 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
191462 int rc = SQLITE_OK;
191463 switch( i ){
191464 case 0: { /* pgno */
191465 sqlite3_result_int(ctx, pCsr->pgno);
191466 break;
191467 }
191468 case 1: { /* data */
191469 DbPage *pDbPage = 0;
191470 rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
191471 if( rc==SQLITE_OK ){
191472 sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
191473 SQLITE_TRANSIENT);
191474 }
191475 sqlite3PagerUnref(pDbPage);
191476 break;
191477 }
191478 default: { /* schema */
191479 sqlite3 *db = sqlite3_context_db_handle(ctx);
191480 sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC);
191481 break;
191482 }
191483 }
191484 return SQLITE_OK;
191485}
191486
191487static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
191488 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
191489 *pRowid = pCsr->pgno;
191490 return SQLITE_OK;
191491}
191492
191493static int dbpageUpdate(
191494 sqlite3_vtab *pVtab,
191495 int argc,
191496 sqlite3_value **argv,
191497 sqlite_int64 *pRowid
191498){
191499 DbpageTable *pTab = (DbpageTable *)pVtab;
191500 Pgno pgno;
191501 DbPage *pDbPage = 0;
191502 int rc = SQLITE_OK;
191503 char *zErr = 0;
191504 const char *zSchema;
191505 int iDb;
191506 Btree *pBt;
191507 Pager *pPager;
191508 int szPage;
191509
191510 if( argc==1 ){
191511 zErr = "cannot delete";
191512 goto update_fail;
191513 }
191514 pgno = sqlite3_value_int(argv[0]);
191515 if( (Pgno)sqlite3_value_int(argv[1])!=pgno ){
191516 zErr = "cannot insert";
191517 goto update_fail;
191518 }
191519 zSchema = (const char*)sqlite3_value_text(argv[4]);
191520 iDb = zSchema ? sqlite3FindDbName(pTab->db, zSchema) : -1;
191521 if( iDb<0 ){
191522 zErr = "no such schema";
191523 goto update_fail;
191524 }
191525 pBt = pTab->db->aDb[iDb].pBt;
191526 if( pgno<1 || pBt==0 || pgno>(int)sqlite3BtreeLastPage(pBt) ){
191527 zErr = "bad page number";
191528 goto update_fail;
191529 }
191530 szPage = sqlite3BtreeGetPageSize(pBt);
191531 if( sqlite3_value_type(argv[3])!=SQLITE_BLOB
191532 || sqlite3_value_bytes(argv[3])!=szPage
191533 ){
191534 zErr = "bad page value";
191535 goto update_fail;
191536 }
191537 pPager = sqlite3BtreePager(pBt);
191538 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
191539 if( rc==SQLITE_OK ){
191540 rc = sqlite3PagerWrite(pDbPage);
191541 if( rc==SQLITE_OK ){
191542 memcpy(sqlite3PagerGetData(pDbPage),
191543 sqlite3_value_blob(argv[3]),
191544 szPage);
191545 }
191546 }
191547 sqlite3PagerUnref(pDbPage);
191548 return rc;
191549
191550update_fail:
191551 sqlite3_free(pVtab->zErrMsg);
191552 pVtab->zErrMsg = sqlite3_mprintf("%s", zErr);
191553 return SQLITE_ERROR;
191554}
191555
191556/* Since we do not know in advance which database files will be
191557** written by the sqlite_dbpage virtual table, start a write transaction
191558** on them all.
191559*/
191560static int dbpageBegin(sqlite3_vtab *pVtab){
191561 DbpageTable *pTab = (DbpageTable *)pVtab;
191562 sqlite3 *db = pTab->db;
191563 int i;
191564 for(i=0; i<db->nDb; i++){
191565 Btree *pBt = db->aDb[i].pBt;
191566 if( pBt ) sqlite3BtreeBeginTrans(pBt, 1, 0);
191567 }
191568 return SQLITE_OK;
191569}
191570
191571
191572/*
191573** Invoke this routine to register the "dbpage" virtual table module
191574*/
191575SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){
191576 static sqlite3_module dbpage_module = {
191577 0, /* iVersion */
191578 dbpageConnect, /* xCreate */
191579 dbpageConnect, /* xConnect */
191580 dbpageBestIndex, /* xBestIndex */
191581 dbpageDisconnect, /* xDisconnect */
191582 dbpageDisconnect, /* xDestroy */
191583 dbpageOpen, /* xOpen - open a cursor */
191584 dbpageClose, /* xClose - close a cursor */
191585 dbpageFilter, /* xFilter - configure scan constraints */
191586 dbpageNext, /* xNext - advance a cursor */
191587 dbpageEof, /* xEof - check for end of scan */
191588 dbpageColumn, /* xColumn - read data */
191589 dbpageRowid, /* xRowid - read data */
191590 dbpageUpdate, /* xUpdate */
191591 dbpageBegin, /* xBegin */
191592 0, /* xSync */
191593 0, /* xCommit */
191594 0, /* xRollback */
191595 0, /* xFindMethod */
191596 0, /* xRename */
191597 0, /* xSavepoint */
191598 0, /* xRelease */
191599 0, /* xRollbackTo */
191600 };
191601 return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
191602}
191603#elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
191604SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; }
191605#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
191606
191607/************** End of dbpage.c **********************************************/
191608/************** Begin file sqlite3session.c **********************************/
191609
191610#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
191611/* #include "sqlite3session.h" */
191612/* #include <assert.h> */
191613/* #include <string.h> */
191614
191615#ifndef SQLITE_AMALGAMATION
191616/* # include "sqliteInt.h" */
191617/* # include "vdbeInt.h" */
191618#endif
191619
191620typedef struct SessionTable SessionTable;
191621typedef struct SessionChange SessionChange;
191622typedef struct SessionBuffer SessionBuffer;
191623typedef struct SessionInput SessionInput;
191624
191625/*
191626** Minimum chunk size used by streaming versions of functions.
191627*/
191628#ifndef SESSIONS_STRM_CHUNK_SIZE
191629# ifdef SQLITE_TEST
191630# define SESSIONS_STRM_CHUNK_SIZE 64
191631# else
191632# define SESSIONS_STRM_CHUNK_SIZE 1024
191633# endif
191634#endif
191635
191636typedef struct SessionHook SessionHook;
191637struct SessionHook {
191638 void *pCtx;
191639 int (*xOld)(void*,int,sqlite3_value**);
191640 int (*xNew)(void*,int,sqlite3_value**);
191641 int (*xCount)(void*);
191642 int (*xDepth)(void*);
191643};
191644
191645/*
191646** Session handle structure.
191647*/
191648struct sqlite3_session {
191649 sqlite3 *db; /* Database handle session is attached to */
191650 char *zDb; /* Name of database session is attached to */
191651 int bEnable; /* True if currently recording */
191652 int bIndirect; /* True if all changes are indirect */
191653 int bAutoAttach; /* True to auto-attach tables */
191654 int rc; /* Non-zero if an error has occurred */
191655 void *pFilterCtx; /* First argument to pass to xTableFilter */
191656 int (*xTableFilter)(void *pCtx, const char *zTab);
191657 sqlite3_value *pZeroBlob; /* Value containing X'' */
191658 sqlite3_session *pNext; /* Next session object on same db. */
191659 SessionTable *pTable; /* List of attached tables */
191660 SessionHook hook; /* APIs to grab new and old data with */
191661};
191662
191663/*
191664** Instances of this structure are used to build strings or binary records.
191665*/
191666struct SessionBuffer {
191667 u8 *aBuf; /* Pointer to changeset buffer */
191668 int nBuf; /* Size of buffer aBuf */
191669 int nAlloc; /* Size of allocation containing aBuf */
191670};
191671
191672/*
191673** An object of this type is used internally as an abstraction for
191674** input data. Input data may be supplied either as a single large buffer
191675** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
191676** sqlite3changeset_start_strm()).
191677*/
191678struct SessionInput {
191679 int bNoDiscard; /* If true, do not discard in InputBuffer() */
191680 int iCurrent; /* Offset in aData[] of current change */
191681 int iNext; /* Offset in aData[] of next change */
191682 u8 *aData; /* Pointer to buffer containing changeset */
191683 int nData; /* Number of bytes in aData */
191684
191685 SessionBuffer buf; /* Current read buffer */
191686 int (*xInput)(void*, void*, int*); /* Input stream call (or NULL) */
191687 void *pIn; /* First argument to xInput */
191688 int bEof; /* Set to true after xInput finished */
191689};
191690
191691/*
191692** Structure for changeset iterators.
191693*/
191694struct sqlite3_changeset_iter {
191695 SessionInput in; /* Input buffer or stream */
191696 SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */
191697 int bPatchset; /* True if this is a patchset */
191698 int rc; /* Iterator error code */
191699 sqlite3_stmt *pConflict; /* Points to conflicting row, if any */
191700 char *zTab; /* Current table */
191701 int nCol; /* Number of columns in zTab */
191702 int op; /* Current operation */
191703 int bIndirect; /* True if current change was indirect */
191704 u8 *abPK; /* Primary key array */
191705 sqlite3_value **apValue; /* old.* and new.* values */
191706};
191707
191708/*
191709** Each session object maintains a set of the following structures, one
191710** for each table the session object is monitoring. The structures are
191711** stored in a linked list starting at sqlite3_session.pTable.
191712**
191713** The keys of the SessionTable.aChange[] hash table are all rows that have
191714** been modified in any way since the session object was attached to the
191715** table.
191716**
191717** The data associated with each hash-table entry is a structure containing
191718** a subset of the initial values that the modified row contained at the
191719** start of the session. Or no initial values if the row was inserted.
191720*/
191721struct SessionTable {
191722 SessionTable *pNext;
191723 char *zName; /* Local name of table */
191724 int nCol; /* Number of columns in table zName */
191725 int bStat1; /* True if this is sqlite_stat1 */
191726 const char **azCol; /* Column names */
191727 u8 *abPK; /* Array of primary key flags */
191728 int nEntry; /* Total number of entries in hash table */
191729 int nChange; /* Size of apChange[] array */
191730 SessionChange **apChange; /* Hash table buckets */
191731};
191732
191733/*
191734** RECORD FORMAT:
191735**
191736** The following record format is similar to (but not compatible with) that
191737** used in SQLite database files. This format is used as part of the
191738** change-set binary format, and so must be architecture independent.
191739**
191740** Unlike the SQLite database record format, each field is self-contained -
191741** there is no separation of header and data. Each field begins with a
191742** single byte describing its type, as follows:
191743**
191744** 0x00: Undefined value.
191745** 0x01: Integer value.
191746** 0x02: Real value.
191747** 0x03: Text value.
191748** 0x04: Blob value.
191749** 0x05: SQL NULL value.
191750**
191751** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
191752** and so on in sqlite3.h. For undefined and NULL values, the field consists
191753** only of the single type byte. For other types of values, the type byte
191754** is followed by:
191755**
191756** Text values:
191757** A varint containing the number of bytes in the value (encoded using
191758** UTF-8). Followed by a buffer containing the UTF-8 representation
191759** of the text value. There is no nul terminator.
191760**
191761** Blob values:
191762** A varint containing the number of bytes in the value, followed by
191763** a buffer containing the value itself.
191764**
191765** Integer values:
191766** An 8-byte big-endian integer value.
191767**
191768** Real values:
191769** An 8-byte big-endian IEEE 754-2008 real value.
191770**
191771** Varint values are encoded in the same way as varints in the SQLite
191772** record format.
191773**
191774** CHANGESET FORMAT:
191775**
191776** A changeset is a collection of DELETE, UPDATE and INSERT operations on
191777** one or more tables. Operations on a single table are grouped together,
191778** but may occur in any order (i.e. deletes, updates and inserts are all
191779** mixed together).
191780**
191781** Each group of changes begins with a table header:
191782**
191783** 1 byte: Constant 0x54 (capital 'T')
191784** Varint: Number of columns in the table.
191785** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
191786** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
191787**
191788** Followed by one or more changes to the table.
191789**
191790** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
191791** 1 byte: The "indirect-change" flag.
191792** old.* record: (delete and update only)
191793** new.* record: (insert and update only)
191794**
191795** The "old.*" and "new.*" records, if present, are N field records in the
191796** format described above under "RECORD FORMAT", where N is the number of
191797** columns in the table. The i'th field of each record is associated with
191798** the i'th column of the table, counting from left to right in the order
191799** in which columns were declared in the CREATE TABLE statement.
191800**
191801** The new.* record that is part of each INSERT change contains the values
191802** that make up the new row. Similarly, the old.* record that is part of each
191803** DELETE change contains the values that made up the row that was deleted
191804** from the database. In the changeset format, the records that are part
191805** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
191806** fields.
191807**
191808** Within the old.* record associated with an UPDATE change, all fields
191809** associated with table columns that are not PRIMARY KEY columns and are
191810** not modified by the UPDATE change are set to "undefined". Other fields
191811** are set to the values that made up the row before the UPDATE that the
191812** change records took place. Within the new.* record, fields associated
191813** with table columns modified by the UPDATE change contain the new
191814** values. Fields associated with table columns that are not modified
191815** are set to "undefined".
191816**
191817** PATCHSET FORMAT:
191818**
191819** A patchset is also a collection of changes. It is similar to a changeset,
191820** but leaves undefined those fields that are not useful if no conflict
191821** resolution is required when applying the changeset.
191822**
191823** Each group of changes begins with a table header:
191824**
191825** 1 byte: Constant 0x50 (capital 'P')
191826** Varint: Number of columns in the table.
191827** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
191828** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
191829**
191830** Followed by one or more changes to the table.
191831**
191832** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
191833** 1 byte: The "indirect-change" flag.
191834** single record: (PK fields for DELETE, PK and modified fields for UPDATE,
191835** full record for INSERT).
191836**
191837** As in the changeset format, each field of the single record that is part
191838** of a patchset change is associated with the correspondingly positioned
191839** table column, counting from left to right within the CREATE TABLE
191840** statement.
191841**
191842** For a DELETE change, all fields within the record except those associated
191843** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
191844** values identifying the row to delete.
191845**
191846** For an UPDATE change, all fields except those associated with PRIMARY KEY
191847** columns and columns that are modified by the UPDATE are set to "undefined".
191848** PRIMARY KEY fields contain the values identifying the table row to update,
191849** and fields associated with modified columns contain the new column values.
191850**
191851** The records associated with INSERT changes are in the same format as for
191852** changesets. It is not possible for a record associated with an INSERT
191853** change to contain a field set to "undefined".
191854*/
191855
191856/*
191857** For each row modified during a session, there exists a single instance of
191858** this structure stored in a SessionTable.aChange[] hash table.
191859*/
191860struct SessionChange {
191861 int op; /* One of UPDATE, DELETE, INSERT */
191862 int bIndirect; /* True if this change is "indirect" */
191863 int nRecord; /* Number of bytes in buffer aRecord[] */
191864 u8 *aRecord; /* Buffer containing old.* record */
191865 SessionChange *pNext; /* For hash-table collisions */
191866};
191867
191868/*
191869** Write a varint with value iVal into the buffer at aBuf. Return the
191870** number of bytes written.
191871*/
191872static int sessionVarintPut(u8 *aBuf, int iVal){
191873 return putVarint32(aBuf, iVal);
191874}
191875
191876/*
191877** Return the number of bytes required to store value iVal as a varint.
191878*/
191879static int sessionVarintLen(int iVal){
191880 return sqlite3VarintLen(iVal);
191881}
191882
191883/*
191884** Read a varint value from aBuf[] into *piVal. Return the number of
191885** bytes read.
191886*/
191887static int sessionVarintGet(u8 *aBuf, int *piVal){
191888 return getVarint32(aBuf, *piVal);
191889}
191890
191891/* Load an unaligned and unsigned 32-bit integer */
191892#define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
191893
191894/*
191895** Read a 64-bit big-endian integer value from buffer aRec[]. Return
191896** the value read.
191897*/
191898static sqlite3_int64 sessionGetI64(u8 *aRec){
191899 u64 x = SESSION_UINT32(aRec);
191900 u32 y = SESSION_UINT32(aRec+4);
191901 x = (x<<32) + y;
191902 return (sqlite3_int64)x;
191903}
191904
191905/*
191906** Write a 64-bit big-endian integer value to the buffer aBuf[].
191907*/
191908static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
191909 aBuf[0] = (i>>56) & 0xFF;
191910 aBuf[1] = (i>>48) & 0xFF;
191911 aBuf[2] = (i>>40) & 0xFF;
191912 aBuf[3] = (i>>32) & 0xFF;
191913 aBuf[4] = (i>>24) & 0xFF;
191914 aBuf[5] = (i>>16) & 0xFF;
191915 aBuf[6] = (i>> 8) & 0xFF;
191916 aBuf[7] = (i>> 0) & 0xFF;
191917}
191918
191919/*
191920** This function is used to serialize the contents of value pValue (see
191921** comment titled "RECORD FORMAT" above).
191922**
191923** If it is non-NULL, the serialized form of the value is written to
191924** buffer aBuf. *pnWrite is set to the number of bytes written before
191925** returning. Or, if aBuf is NULL, the only thing this function does is
191926** set *pnWrite.
191927**
191928** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
191929** within a call to sqlite3_value_text() (may fail if the db is utf-16))
191930** SQLITE_NOMEM is returned.
191931*/
191932static int sessionSerializeValue(
191933 u8 *aBuf, /* If non-NULL, write serialized value here */
191934 sqlite3_value *pValue, /* Value to serialize */
191935 int *pnWrite /* IN/OUT: Increment by bytes written */
191936){
191937 int nByte; /* Size of serialized value in bytes */
191938
191939 if( pValue ){
191940 int eType; /* Value type (SQLITE_NULL, TEXT etc.) */
191941
191942 eType = sqlite3_value_type(pValue);
191943 if( aBuf ) aBuf[0] = eType;
191944
191945 switch( eType ){
191946 case SQLITE_NULL:
191947 nByte = 1;
191948 break;
191949
191950 case SQLITE_INTEGER:
191951 case SQLITE_FLOAT:
191952 if( aBuf ){
191953 /* TODO: SQLite does something special to deal with mixed-endian
191954 ** floating point values (e.g. ARM7). This code probably should
191955 ** too. */
191956 u64 i;
191957 if( eType==SQLITE_INTEGER ){
191958 i = (u64)sqlite3_value_int64(pValue);
191959 }else{
191960 double r;
191961 assert( sizeof(double)==8 && sizeof(u64)==8 );
191962 r = sqlite3_value_double(pValue);
191963 memcpy(&i, &r, 8);
191964 }
191965 sessionPutI64(&aBuf[1], i);
191966 }
191967 nByte = 9;
191968 break;
191969
191970 default: {
191971 u8 *z;
191972 int n;
191973 int nVarint;
191974
191975 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
191976 if( eType==SQLITE_TEXT ){
191977 z = (u8 *)sqlite3_value_text(pValue);
191978 }else{
191979 z = (u8 *)sqlite3_value_blob(pValue);
191980 }
191981 n = sqlite3_value_bytes(pValue);
191982 if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
191983 nVarint = sessionVarintLen(n);
191984
191985 if( aBuf ){
191986 sessionVarintPut(&aBuf[1], n);
191987 if( n ) memcpy(&aBuf[nVarint + 1], z, n);
191988 }
191989
191990 nByte = 1 + nVarint + n;
191991 break;
191992 }
191993 }
191994 }else{
191995 nByte = 1;
191996 if( aBuf ) aBuf[0] = '\0';
191997 }
191998
191999 if( pnWrite ) *pnWrite += nByte;
192000 return SQLITE_OK;
192001}
192002
192003
192004/*
192005** This macro is used to calculate hash key values for data structures. In
192006** order to use this macro, the entire data structure must be represented
192007** as a series of unsigned integers. In order to calculate a hash-key value
192008** for a data structure represented as three such integers, the macro may
192009** then be used as follows:
192010**
192011** int hash_key_value;
192012** hash_key_value = HASH_APPEND(0, <value 1>);
192013** hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
192014** hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
192015**
192016** In practice, the data structures this macro is used for are the primary
192017** key values of modified rows.
192018*/
192019#define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
192020
192021/*
192022** Append the hash of the 64-bit integer passed as the second argument to the
192023** hash-key value passed as the first. Return the new hash-key value.
192024*/
192025static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
192026 h = HASH_APPEND(h, i & 0xFFFFFFFF);
192027 return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
192028}
192029
192030/*
192031** Append the hash of the blob passed via the second and third arguments to
192032** the hash-key value passed as the first. Return the new hash-key value.
192033*/
192034static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
192035 int i;
192036 for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
192037 return h;
192038}
192039
192040/*
192041** Append the hash of the data type passed as the second argument to the
192042** hash-key value passed as the first. Return the new hash-key value.
192043*/
192044static unsigned int sessionHashAppendType(unsigned int h, int eType){
192045 return HASH_APPEND(h, eType);
192046}
192047
192048/*
192049** This function may only be called from within a pre-update callback.
192050** It calculates a hash based on the primary key values of the old.* or
192051** new.* row currently available and, assuming no error occurs, writes it to
192052** *piHash before returning. If the primary key contains one or more NULL
192053** values, *pbNullPK is set to true before returning.
192054**
192055** If an error occurs, an SQLite error code is returned and the final values
192056** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
192057** and the output variables are set as described above.
192058*/
192059static int sessionPreupdateHash(
192060 sqlite3_session *pSession, /* Session object that owns pTab */
192061 SessionTable *pTab, /* Session table handle */
192062 int bNew, /* True to hash the new.* PK */
192063 int *piHash, /* OUT: Hash value */
192064 int *pbNullPK /* OUT: True if there are NULL values in PK */
192065){
192066 unsigned int h = 0; /* Hash value to return */
192067 int i; /* Used to iterate through columns */
192068
192069 assert( *pbNullPK==0 );
192070 assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
192071 for(i=0; i<pTab->nCol; i++){
192072 if( pTab->abPK[i] ){
192073 int rc;
192074 int eType;
192075 sqlite3_value *pVal;
192076
192077 if( bNew ){
192078 rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
192079 }else{
192080 rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
192081 }
192082 if( rc!=SQLITE_OK ) return rc;
192083
192084 eType = sqlite3_value_type(pVal);
192085 h = sessionHashAppendType(h, eType);
192086 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
192087 i64 iVal;
192088 if( eType==SQLITE_INTEGER ){
192089 iVal = sqlite3_value_int64(pVal);
192090 }else{
192091 double rVal = sqlite3_value_double(pVal);
192092 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
192093 memcpy(&iVal, &rVal, 8);
192094 }
192095 h = sessionHashAppendI64(h, iVal);
192096 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
192097 const u8 *z;
192098 int n;
192099 if( eType==SQLITE_TEXT ){
192100 z = (const u8 *)sqlite3_value_text(pVal);
192101 }else{
192102 z = (const u8 *)sqlite3_value_blob(pVal);
192103 }
192104 n = sqlite3_value_bytes(pVal);
192105 if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
192106 h = sessionHashAppendBlob(h, n, z);
192107 }else{
192108 assert( eType==SQLITE_NULL );
192109 assert( pTab->bStat1==0 || i!=1 );
192110 *pbNullPK = 1;
192111 }
192112 }
192113 }
192114
192115 *piHash = (h % pTab->nChange);
192116 return SQLITE_OK;
192117}
192118
192119/*
192120** The buffer that the argument points to contains a serialized SQL value.
192121** Return the number of bytes of space occupied by the value (including
192122** the type byte).
192123*/
192124static int sessionSerialLen(u8 *a){
192125 int e = *a;
192126 int n;
192127 if( e==0 || e==0xFF ) return 1;
192128 if( e==SQLITE_NULL ) return 1;
192129 if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
192130 return sessionVarintGet(&a[1], &n) + 1 + n;
192131}
192132
192133/*
192134** Based on the primary key values stored in change aRecord, calculate a
192135** hash key. Assume the has table has nBucket buckets. The hash keys
192136** calculated by this function are compatible with those calculated by
192137** sessionPreupdateHash().
192138**
192139** The bPkOnly argument is non-zero if the record at aRecord[] is from
192140** a patchset DELETE. In this case the non-PK fields are omitted entirely.
192141*/
192142static unsigned int sessionChangeHash(
192143 SessionTable *pTab, /* Table handle */
192144 int bPkOnly, /* Record consists of PK fields only */
192145 u8 *aRecord, /* Change record */
192146 int nBucket /* Assume this many buckets in hash table */
192147){
192148 unsigned int h = 0; /* Value to return */
192149 int i; /* Used to iterate through columns */
192150 u8 *a = aRecord; /* Used to iterate through change record */
192151
192152 for(i=0; i<pTab->nCol; i++){
192153 int eType = *a;
192154 int isPK = pTab->abPK[i];
192155 if( bPkOnly && isPK==0 ) continue;
192156
192157 /* It is not possible for eType to be SQLITE_NULL here. The session
192158 ** module does not record changes for rows with NULL values stored in
192159 ** primary key columns. */
192160 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
192161 || eType==SQLITE_TEXT || eType==SQLITE_BLOB
192162 || eType==SQLITE_NULL || eType==0
192163 );
192164 assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
192165
192166 if( isPK ){
192167 a++;
192168 h = sessionHashAppendType(h, eType);
192169 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
192170 h = sessionHashAppendI64(h, sessionGetI64(a));
192171 a += 8;
192172 }else{
192173 int n;
192174 a += sessionVarintGet(a, &n);
192175 h = sessionHashAppendBlob(h, n, a);
192176 a += n;
192177 }
192178 }else{
192179 a += sessionSerialLen(a);
192180 }
192181 }
192182 return (h % nBucket);
192183}
192184
192185/*
192186** Arguments aLeft and aRight are pointers to change records for table pTab.
192187** This function returns true if the two records apply to the same row (i.e.
192188** have the same values stored in the primary key columns), or false
192189** otherwise.
192190*/
192191static int sessionChangeEqual(
192192 SessionTable *pTab, /* Table used for PK definition */
192193 int bLeftPkOnly, /* True if aLeft[] contains PK fields only */
192194 u8 *aLeft, /* Change record */
192195 int bRightPkOnly, /* True if aRight[] contains PK fields only */
192196 u8 *aRight /* Change record */
192197){
192198 u8 *a1 = aLeft; /* Cursor to iterate through aLeft */
192199 u8 *a2 = aRight; /* Cursor to iterate through aRight */
192200 int iCol; /* Used to iterate through table columns */
192201
192202 for(iCol=0; iCol<pTab->nCol; iCol++){
192203 if( pTab->abPK[iCol] ){
192204 int n1 = sessionSerialLen(a1);
192205 int n2 = sessionSerialLen(a2);
192206
192207 if( n1!=n2 || memcmp(a1, a2, n1) ){
192208 return 0;
192209 }
192210 a1 += n1;
192211 a2 += n2;
192212 }else{
192213 if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
192214 if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
192215 }
192216 }
192217
192218 return 1;
192219}
192220
192221/*
192222** Arguments aLeft and aRight both point to buffers containing change
192223** records with nCol columns. This function "merges" the two records into
192224** a single records which is written to the buffer at *paOut. *paOut is
192225** then set to point to one byte after the last byte written before
192226** returning.
192227**
192228** The merging of records is done as follows: For each column, if the
192229** aRight record contains a value for the column, copy the value from
192230** their. Otherwise, if aLeft contains a value, copy it. If neither
192231** record contains a value for a given column, then neither does the
192232** output record.
192233*/
192234static void sessionMergeRecord(
192235 u8 **paOut,
192236 int nCol,
192237 u8 *aLeft,
192238 u8 *aRight
192239){
192240 u8 *a1 = aLeft; /* Cursor used to iterate through aLeft */
192241 u8 *a2 = aRight; /* Cursor used to iterate through aRight */
192242 u8 *aOut = *paOut; /* Output cursor */
192243 int iCol; /* Used to iterate from 0 to nCol */
192244
192245 for(iCol=0; iCol<nCol; iCol++){
192246 int n1 = sessionSerialLen(a1);
192247 int n2 = sessionSerialLen(a2);
192248 if( *a2 ){
192249 memcpy(aOut, a2, n2);
192250 aOut += n2;
192251 }else{
192252 memcpy(aOut, a1, n1);
192253 aOut += n1;
192254 }
192255 a1 += n1;
192256 a2 += n2;
192257 }
192258
192259 *paOut = aOut;
192260}
192261
192262/*
192263** This is a helper function used by sessionMergeUpdate().
192264**
192265** When this function is called, both *paOne and *paTwo point to a value
192266** within a change record. Before it returns, both have been advanced so
192267** as to point to the next value in the record.
192268**
192269** If, when this function is called, *paTwo points to a valid value (i.e.
192270** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
192271** pointer is returned and *pnVal is set to the number of bytes in the
192272** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
192273** set to the number of bytes in the value at *paOne. If *paOne points
192274** to the "no value" placeholder, *pnVal is set to 1. In other words:
192275**
192276** if( *paTwo is valid ) return *paTwo;
192277** return *paOne;
192278**
192279*/
192280static u8 *sessionMergeValue(
192281 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
192282 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
192283 int *pnVal /* OUT: Bytes in returned value */
192284){
192285 u8 *a1 = *paOne;
192286 u8 *a2 = *paTwo;
192287 u8 *pRet = 0;
192288 int n1;
192289
192290 assert( a1 );
192291 if( a2 ){
192292 int n2 = sessionSerialLen(a2);
192293 if( *a2 ){
192294 *pnVal = n2;
192295 pRet = a2;
192296 }
192297 *paTwo = &a2[n2];
192298 }
192299
192300 n1 = sessionSerialLen(a1);
192301 if( pRet==0 ){
192302 *pnVal = n1;
192303 pRet = a1;
192304 }
192305 *paOne = &a1[n1];
192306
192307 return pRet;
192308}
192309
192310/*
192311** This function is used by changeset_concat() to merge two UPDATE changes
192312** on the same row.
192313*/
192314static int sessionMergeUpdate(
192315 u8 **paOut, /* IN/OUT: Pointer to output buffer */
192316 SessionTable *pTab, /* Table change pertains to */
192317 int bPatchset, /* True if records are patchset records */
192318 u8 *aOldRecord1, /* old.* record for first change */
192319 u8 *aOldRecord2, /* old.* record for second change */
192320 u8 *aNewRecord1, /* new.* record for first change */
192321 u8 *aNewRecord2 /* new.* record for second change */
192322){
192323 u8 *aOld1 = aOldRecord1;
192324 u8 *aOld2 = aOldRecord2;
192325 u8 *aNew1 = aNewRecord1;
192326 u8 *aNew2 = aNewRecord2;
192327
192328 u8 *aOut = *paOut;
192329 int i;
192330
192331 if( bPatchset==0 ){
192332 int bRequired = 0;
192333
192334 assert( aOldRecord1 && aNewRecord1 );
192335
192336 /* Write the old.* vector first. */
192337 for(i=0; i<pTab->nCol; i++){
192338 int nOld;
192339 u8 *aOld;
192340 int nNew;
192341 u8 *aNew;
192342
192343 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
192344 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
192345 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
192346 if( pTab->abPK[i]==0 ) bRequired = 1;
192347 memcpy(aOut, aOld, nOld);
192348 aOut += nOld;
192349 }else{
192350 *(aOut++) = '\0';
192351 }
192352 }
192353
192354 if( !bRequired ) return 0;
192355 }
192356
192357 /* Write the new.* vector */
192358 aOld1 = aOldRecord1;
192359 aOld2 = aOldRecord2;
192360 aNew1 = aNewRecord1;
192361 aNew2 = aNewRecord2;
192362 for(i=0; i<pTab->nCol; i++){
192363 int nOld;
192364 u8 *aOld;
192365 int nNew;
192366 u8 *aNew;
192367
192368 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
192369 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
192370 if( bPatchset==0
192371 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
192372 ){
192373 *(aOut++) = '\0';
192374 }else{
192375 memcpy(aOut, aNew, nNew);
192376 aOut += nNew;
192377 }
192378 }
192379
192380 *paOut = aOut;
192381 return 1;
192382}
192383
192384/*
192385** This function is only called from within a pre-update-hook callback.
192386** It determines if the current pre-update-hook change affects the same row
192387** as the change stored in argument pChange. If so, it returns true. Otherwise
192388** if the pre-update-hook does not affect the same row as pChange, it returns
192389** false.
192390*/
192391static int sessionPreupdateEqual(
192392 sqlite3_session *pSession, /* Session object that owns SessionTable */
192393 SessionTable *pTab, /* Table associated with change */
192394 SessionChange *pChange, /* Change to compare to */
192395 int op /* Current pre-update operation */
192396){
192397 int iCol; /* Used to iterate through columns */
192398 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
192399
192400 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
192401 for(iCol=0; iCol<pTab->nCol; iCol++){
192402 if( !pTab->abPK[iCol] ){
192403 a += sessionSerialLen(a);
192404 }else{
192405 sqlite3_value *pVal; /* Value returned by preupdate_new/old */
192406 int rc; /* Error code from preupdate_new/old */
192407 int eType = *a++; /* Type of value from change record */
192408
192409 /* The following calls to preupdate_new() and preupdate_old() can not
192410 ** fail. This is because they cache their return values, and by the
192411 ** time control flows to here they have already been called once from
192412 ** within sessionPreupdateHash(). The first two asserts below verify
192413 ** this (that the method has already been called). */
192414 if( op==SQLITE_INSERT ){
192415 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
192416 rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
192417 }else{
192418 /* assert( db->pPreUpdate->pUnpacked ); */
192419 rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
192420 }
192421 assert( rc==SQLITE_OK );
192422 if( sqlite3_value_type(pVal)!=eType ) return 0;
192423
192424 /* A SessionChange object never has a NULL value in a PK column */
192425 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
192426 || eType==SQLITE_BLOB || eType==SQLITE_TEXT
192427 );
192428
192429 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
192430 i64 iVal = sessionGetI64(a);
192431 a += 8;
192432 if( eType==SQLITE_INTEGER ){
192433 if( sqlite3_value_int64(pVal)!=iVal ) return 0;
192434 }else{
192435 double rVal;
192436 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
192437 memcpy(&rVal, &iVal, 8);
192438 if( sqlite3_value_double(pVal)!=rVal ) return 0;
192439 }
192440 }else{
192441 int n;
192442 const u8 *z;
192443 a += sessionVarintGet(a, &n);
192444 if( sqlite3_value_bytes(pVal)!=n ) return 0;
192445 if( eType==SQLITE_TEXT ){
192446 z = sqlite3_value_text(pVal);
192447 }else{
192448 z = sqlite3_value_blob(pVal);
192449 }
192450 if( n>0 && memcmp(a, z, n) ) return 0;
192451 a += n;
192452 }
192453 }
192454 }
192455
192456 return 1;
192457}
192458
192459/*
192460** If required, grow the hash table used to store changes on table pTab
192461** (part of the session pSession). If a fatal OOM error occurs, set the
192462** session object to failed and return SQLITE_ERROR. Otherwise, return
192463** SQLITE_OK.
192464**
192465** It is possible that a non-fatal OOM error occurs in this function. In
192466** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
192467** Growing the hash table in this case is a performance optimization only,
192468** it is not required for correct operation.
192469*/
192470static int sessionGrowHash(int bPatchset, SessionTable *pTab){
192471 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
192472 int i;
192473 SessionChange **apNew;
192474 int nNew = (pTab->nChange ? pTab->nChange : 128) * 2;
192475
192476 apNew = (SessionChange **)sqlite3_malloc(sizeof(SessionChange *) * nNew);
192477 if( apNew==0 ){
192478 if( pTab->nChange==0 ){
192479 return SQLITE_ERROR;
192480 }
192481 return SQLITE_OK;
192482 }
192483 memset(apNew, 0, sizeof(SessionChange *) * nNew);
192484
192485 for(i=0; i<pTab->nChange; i++){
192486 SessionChange *p;
192487 SessionChange *pNext;
192488 for(p=pTab->apChange[i]; p; p=pNext){
192489 int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
192490 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
192491 pNext = p->pNext;
192492 p->pNext = apNew[iHash];
192493 apNew[iHash] = p;
192494 }
192495 }
192496
192497 sqlite3_free(pTab->apChange);
192498 pTab->nChange = nNew;
192499 pTab->apChange = apNew;
192500 }
192501
192502 return SQLITE_OK;
192503}
192504
192505/*
192506** This function queries the database for the names of the columns of table
192507** zThis, in schema zDb.
192508**
192509** Otherwise, if they are not NULL, variable *pnCol is set to the number
192510** of columns in the database table and variable *pzTab is set to point to a
192511** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
192512** point to an array of pointers to column names. And *pabPK (again, if not
192513** NULL) is set to point to an array of booleans - true if the corresponding
192514** column is part of the primary key.
192515**
192516** For example, if the table is declared as:
192517**
192518** CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
192519**
192520** Then the four output variables are populated as follows:
192521**
192522** *pnCol = 4
192523** *pzTab = "tbl1"
192524** *pazCol = {"w", "x", "y", "z"}
192525** *pabPK = {1, 0, 0, 1}
192526**
192527** All returned buffers are part of the same single allocation, which must
192528** be freed using sqlite3_free() by the caller
192529*/
192530static int sessionTableInfo(
192531 sqlite3 *db, /* Database connection */
192532 const char *zDb, /* Name of attached database (e.g. "main") */
192533 const char *zThis, /* Table name */
192534 int *pnCol, /* OUT: number of columns */
192535 const char **pzTab, /* OUT: Copy of zThis */
192536 const char ***pazCol, /* OUT: Array of column names for table */
192537 u8 **pabPK /* OUT: Array of booleans - true for PK col */
192538){
192539 char *zPragma;
192540 sqlite3_stmt *pStmt;
192541 int rc;
192542 int nByte;
192543 int nDbCol = 0;
192544 int nThis;
192545 int i;
192546 u8 *pAlloc = 0;
192547 char **azCol = 0;
192548 u8 *abPK = 0;
192549
192550 assert( pazCol && pabPK );
192551
192552 nThis = sqlite3Strlen30(zThis);
192553 if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
192554 rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
192555 if( rc==SQLITE_OK ){
192556 /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
192557 zPragma = sqlite3_mprintf(
192558 "SELECT 0, 'tbl', '', 0, '', 1 UNION ALL "
192559 "SELECT 1, 'idx', '', 0, '', 2 UNION ALL "
192560 "SELECT 2, 'stat', '', 0, '', 0"
192561 );
192562 }else if( rc==SQLITE_ERROR ){
192563 zPragma = sqlite3_mprintf("");
192564 }else{
192565 return rc;
192566 }
192567 }else{
192568 zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
192569 }
192570 if( !zPragma ) return SQLITE_NOMEM;
192571
192572 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
192573 sqlite3_free(zPragma);
192574 if( rc!=SQLITE_OK ) return rc;
192575
192576 nByte = nThis + 1;
192577 while( SQLITE_ROW==sqlite3_step(pStmt) ){
192578 nByte += sqlite3_column_bytes(pStmt, 1);
192579 nDbCol++;
192580 }
192581 rc = sqlite3_reset(pStmt);
192582
192583 if( rc==SQLITE_OK ){
192584 nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
192585 pAlloc = sqlite3_malloc(nByte);
192586 if( pAlloc==0 ){
192587 rc = SQLITE_NOMEM;
192588 }
192589 }
192590 if( rc==SQLITE_OK ){
192591 azCol = (char **)pAlloc;
192592 pAlloc = (u8 *)&azCol[nDbCol];
192593 abPK = (u8 *)pAlloc;
192594 pAlloc = &abPK[nDbCol];
192595 if( pzTab ){
192596 memcpy(pAlloc, zThis, nThis+1);
192597 *pzTab = (char *)pAlloc;
192598 pAlloc += nThis+1;
192599 }
192600
192601 i = 0;
192602 while( SQLITE_ROW==sqlite3_step(pStmt) ){
192603 int nName = sqlite3_column_bytes(pStmt, 1);
192604 const unsigned char *zName = sqlite3_column_text(pStmt, 1);
192605 if( zName==0 ) break;
192606 memcpy(pAlloc, zName, nName+1);
192607 azCol[i] = (char *)pAlloc;
192608 pAlloc += nName+1;
192609 abPK[i] = sqlite3_column_int(pStmt, 5);
192610 i++;
192611 }
192612 rc = sqlite3_reset(pStmt);
192613
192614 }
192615
192616 /* If successful, populate the output variables. Otherwise, zero them and
192617 ** free any allocation made. An error code will be returned in this case.
192618 */
192619 if( rc==SQLITE_OK ){
192620 *pazCol = (const char **)azCol;
192621 *pabPK = abPK;
192622 *pnCol = nDbCol;
192623 }else{
192624 *pazCol = 0;
192625 *pabPK = 0;
192626 *pnCol = 0;
192627 if( pzTab ) *pzTab = 0;
192628 sqlite3_free(azCol);
192629 }
192630 sqlite3_finalize(pStmt);
192631 return rc;
192632}
192633
192634/*
192635** This function is only called from within a pre-update handler for a
192636** write to table pTab, part of session pSession. If this is the first
192637** write to this table, initalize the SessionTable.nCol, azCol[] and
192638** abPK[] arrays accordingly.
192639**
192640** If an error occurs, an error code is stored in sqlite3_session.rc and
192641** non-zero returned. Or, if no error occurs but the table has no primary
192642** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
192643** indicate that updates on this table should be ignored. SessionTable.abPK
192644** is set to NULL in this case.
192645*/
192646static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
192647 if( pTab->nCol==0 ){
192648 u8 *abPK;
192649 assert( pTab->azCol==0 || pTab->abPK==0 );
192650 pSession->rc = sessionTableInfo(pSession->db, pSession->zDb,
192651 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
192652 );
192653 if( pSession->rc==SQLITE_OK ){
192654 int i;
192655 for(i=0; i<pTab->nCol; i++){
192656 if( abPK[i] ){
192657 pTab->abPK = abPK;
192658 break;
192659 }
192660 }
192661 if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
192662 pTab->bStat1 = 1;
192663 }
192664 }
192665 }
192666 return (pSession->rc || pTab->abPK==0);
192667}
192668
192669/*
192670** Versions of the four methods in object SessionHook for use with the
192671** sqlite_stat1 table. The purpose of this is to substitute a zero-length
192672** blob each time a NULL value is read from the "idx" column of the
192673** sqlite_stat1 table.
192674*/
192675typedef struct SessionStat1Ctx SessionStat1Ctx;
192676struct SessionStat1Ctx {
192677 SessionHook hook;
192678 sqlite3_session *pSession;
192679};
192680static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
192681 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
192682 sqlite3_value *pVal = 0;
192683 int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
192684 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
192685 pVal = p->pSession->pZeroBlob;
192686 }
192687 *ppVal = pVal;
192688 return rc;
192689}
192690static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
192691 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
192692 sqlite3_value *pVal = 0;
192693 int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
192694 if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
192695 pVal = p->pSession->pZeroBlob;
192696 }
192697 *ppVal = pVal;
192698 return rc;
192699}
192700static int sessionStat1Count(void *pCtx){
192701 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
192702 return p->hook.xCount(p->hook.pCtx);
192703}
192704static int sessionStat1Depth(void *pCtx){
192705 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
192706 return p->hook.xDepth(p->hook.pCtx);
192707}
192708
192709
192710/*
192711** This function is only called from with a pre-update-hook reporting a
192712** change on table pTab (attached to session pSession). The type of change
192713** (UPDATE, INSERT, DELETE) is specified by the first argument.
192714**
192715** Unless one is already present or an error occurs, an entry is added
192716** to the changed-rows hash table associated with table pTab.
192717*/
192718static void sessionPreupdateOneChange(
192719 int op, /* One of SQLITE_UPDATE, INSERT, DELETE */
192720 sqlite3_session *pSession, /* Session object pTab is attached to */
192721 SessionTable *pTab /* Table that change applies to */
192722){
192723 int iHash;
192724 int bNull = 0;
192725 int rc = SQLITE_OK;
192726 SessionStat1Ctx stat1 = {0};
192727
192728 if( pSession->rc ) return;
192729
192730 /* Load table details if required */
192731 if( sessionInitTable(pSession, pTab) ) return;
192732
192733 /* Check the number of columns in this xPreUpdate call matches the
192734 ** number of columns in the table. */
192735 if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
192736 pSession->rc = SQLITE_SCHEMA;
192737 return;
192738 }
192739
192740 /* Grow the hash table if required */
192741 if( sessionGrowHash(0, pTab) ){
192742 pSession->rc = SQLITE_NOMEM;
192743 return;
192744 }
192745
192746 if( pTab->bStat1 ){
192747 stat1.hook = pSession->hook;
192748 stat1.pSession = pSession;
192749 pSession->hook.pCtx = (void*)&stat1;
192750 pSession->hook.xNew = sessionStat1New;
192751 pSession->hook.xOld = sessionStat1Old;
192752 pSession->hook.xCount = sessionStat1Count;
192753 pSession->hook.xDepth = sessionStat1Depth;
192754 if( pSession->pZeroBlob==0 ){
192755 sqlite3_value *p = sqlite3ValueNew(0);
192756 if( p==0 ){
192757 rc = SQLITE_NOMEM;
192758 goto error_out;
192759 }
192760 sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC);
192761 pSession->pZeroBlob = p;
192762 }
192763 }
192764
192765 /* Calculate the hash-key for this change. If the primary key of the row
192766 ** includes a NULL value, exit early. Such changes are ignored by the
192767 ** session module. */
192768 rc = sessionPreupdateHash(pSession, pTab, op==SQLITE_INSERT, &iHash, &bNull);
192769 if( rc!=SQLITE_OK ) goto error_out;
192770
192771 if( bNull==0 ){
192772 /* Search the hash table for an existing record for this row. */
192773 SessionChange *pC;
192774 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
192775 if( sessionPreupdateEqual(pSession, pTab, pC, op) ) break;
192776 }
192777
192778 if( pC==0 ){
192779 /* Create a new change object containing all the old values (if
192780 ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
192781 ** values (if this is an INSERT). */
192782 SessionChange *pChange; /* New change object */
192783 int nByte; /* Number of bytes to allocate */
192784 int i; /* Used to iterate through columns */
192785
192786 assert( rc==SQLITE_OK );
192787 pTab->nEntry++;
192788
192789 /* Figure out how large an allocation is required */
192790 nByte = sizeof(SessionChange);
192791 for(i=0; i<pTab->nCol; i++){
192792 sqlite3_value *p = 0;
192793 if( op!=SQLITE_INSERT ){
192794 TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
192795 assert( trc==SQLITE_OK );
192796 }else if( pTab->abPK[i] ){
192797 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
192798 assert( trc==SQLITE_OK );
192799 }
192800
192801 /* This may fail if SQLite value p contains a utf-16 string that must
192802 ** be converted to utf-8 and an OOM error occurs while doing so. */
192803 rc = sessionSerializeValue(0, p, &nByte);
192804 if( rc!=SQLITE_OK ) goto error_out;
192805 }
192806
192807 /* Allocate the change object */
192808 pChange = (SessionChange *)sqlite3_malloc(nByte);
192809 if( !pChange ){
192810 rc = SQLITE_NOMEM;
192811 goto error_out;
192812 }else{
192813 memset(pChange, 0, sizeof(SessionChange));
192814 pChange->aRecord = (u8 *)&pChange[1];
192815 }
192816
192817 /* Populate the change object. None of the preupdate_old(),
192818 ** preupdate_new() or SerializeValue() calls below may fail as all
192819 ** required values and encodings have already been cached in memory.
192820 ** It is not possible for an OOM to occur in this block. */
192821 nByte = 0;
192822 for(i=0; i<pTab->nCol; i++){
192823 sqlite3_value *p = 0;
192824 if( op!=SQLITE_INSERT ){
192825 pSession->hook.xOld(pSession->hook.pCtx, i, &p);
192826 }else if( pTab->abPK[i] ){
192827 pSession->hook.xNew(pSession->hook.pCtx, i, &p);
192828 }
192829 sessionSerializeValue(&pChange->aRecord[nByte], p, &nByte);
192830 }
192831
192832 /* Add the change to the hash-table */
192833 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
192834 pChange->bIndirect = 1;
192835 }
192836 pChange->nRecord = nByte;
192837 pChange->op = op;
192838 pChange->pNext = pTab->apChange[iHash];
192839 pTab->apChange[iHash] = pChange;
192840
192841 }else if( pC->bIndirect ){
192842 /* If the existing change is considered "indirect", but this current
192843 ** change is "direct", mark the change object as direct. */
192844 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
192845 && pSession->bIndirect==0
192846 ){
192847 pC->bIndirect = 0;
192848 }
192849 }
192850 }
192851
192852 /* If an error has occurred, mark the session object as failed. */
192853 error_out:
192854 if( pTab->bStat1 ){
192855 pSession->hook = stat1.hook;
192856 }
192857 if( rc!=SQLITE_OK ){
192858 pSession->rc = rc;
192859 }
192860}
192861
192862static int sessionFindTable(
192863 sqlite3_session *pSession,
192864 const char *zName,
192865 SessionTable **ppTab
192866){
192867 int rc = SQLITE_OK;
192868 int nName = sqlite3Strlen30(zName);
192869 SessionTable *pRet;
192870
192871 /* Search for an existing table */
192872 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
192873 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
192874 }
192875
192876 if( pRet==0 && pSession->bAutoAttach ){
192877 /* If there is a table-filter configured, invoke it. If it returns 0,
192878 ** do not automatically add the new table. */
192879 if( pSession->xTableFilter==0
192880 || pSession->xTableFilter(pSession->pFilterCtx, zName)
192881 ){
192882 rc = sqlite3session_attach(pSession, zName);
192883 if( rc==SQLITE_OK ){
192884 for(pRet=pSession->pTable; pRet->pNext; pRet=pRet->pNext);
192885 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
192886 }
192887 }
192888 }
192889
192890 assert( rc==SQLITE_OK || pRet==0 );
192891 *ppTab = pRet;
192892 return rc;
192893}
192894
192895/*
192896** The 'pre-update' hook registered by this module with SQLite databases.
192897*/
192898static void xPreUpdate(
192899 void *pCtx, /* Copy of third arg to preupdate_hook() */
192900 sqlite3 *db, /* Database handle */
192901 int op, /* SQLITE_UPDATE, DELETE or INSERT */
192902 char const *zDb, /* Database name */
192903 char const *zName, /* Table name */
192904 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
192905 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
192906){
192907 sqlite3_session *pSession;
192908 int nDb = sqlite3Strlen30(zDb);
192909
192910 assert( sqlite3_mutex_held(db->mutex) );
192911
192912 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
192913 SessionTable *pTab;
192914
192915 /* If this session is attached to a different database ("main", "temp"
192916 ** etc.), or if it is not currently enabled, there is nothing to do. Skip
192917 ** to the next session object attached to this database. */
192918 if( pSession->bEnable==0 ) continue;
192919 if( pSession->rc ) continue;
192920 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
192921
192922 pSession->rc = sessionFindTable(pSession, zName, &pTab);
192923 if( pTab ){
192924 assert( pSession->rc==SQLITE_OK );
192925 sessionPreupdateOneChange(op, pSession, pTab);
192926 if( op==SQLITE_UPDATE ){
192927 sessionPreupdateOneChange(SQLITE_INSERT, pSession, pTab);
192928 }
192929 }
192930 }
192931}
192932
192933/*
192934** The pre-update hook implementations.
192935*/
192936static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
192937 return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
192938}
192939static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
192940 return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
192941}
192942static int sessionPreupdateCount(void *pCtx){
192943 return sqlite3_preupdate_count((sqlite3*)pCtx);
192944}
192945static int sessionPreupdateDepth(void *pCtx){
192946 return sqlite3_preupdate_depth((sqlite3*)pCtx);
192947}
192948
192949/*
192950** Install the pre-update hooks on the session object passed as the only
192951** argument.
192952*/
192953static void sessionPreupdateHooks(
192954 sqlite3_session *pSession
192955){
192956 pSession->hook.pCtx = (void*)pSession->db;
192957 pSession->hook.xOld = sessionPreupdateOld;
192958 pSession->hook.xNew = sessionPreupdateNew;
192959 pSession->hook.xCount = sessionPreupdateCount;
192960 pSession->hook.xDepth = sessionPreupdateDepth;
192961}
192962
192963typedef struct SessionDiffCtx SessionDiffCtx;
192964struct SessionDiffCtx {
192965 sqlite3_stmt *pStmt;
192966 int nOldOff;
192967};
192968
192969/*
192970** The diff hook implementations.
192971*/
192972static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
192973 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
192974 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
192975 return SQLITE_OK;
192976}
192977static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
192978 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
192979 *ppVal = sqlite3_column_value(p->pStmt, iVal);
192980 return SQLITE_OK;
192981}
192982static int sessionDiffCount(void *pCtx){
192983 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
192984 return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
192985}
192986static int sessionDiffDepth(void *pCtx){
192987 return 0;
192988}
192989
192990/*
192991** Install the diff hooks on the session object passed as the only
192992** argument.
192993*/
192994static void sessionDiffHooks(
192995 sqlite3_session *pSession,
192996 SessionDiffCtx *pDiffCtx
192997){
192998 pSession->hook.pCtx = (void*)pDiffCtx;
192999 pSession->hook.xOld = sessionDiffOld;
193000 pSession->hook.xNew = sessionDiffNew;
193001 pSession->hook.xCount = sessionDiffCount;
193002 pSession->hook.xDepth = sessionDiffDepth;
193003}
193004
193005static char *sessionExprComparePK(
193006 int nCol,
193007 const char *zDb1, const char *zDb2,
193008 const char *zTab,
193009 const char **azCol, u8 *abPK
193010){
193011 int i;
193012 const char *zSep = "";
193013 char *zRet = 0;
193014
193015 for(i=0; i<nCol; i++){
193016 if( abPK[i] ){
193017 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
193018 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
193019 );
193020 zSep = " AND ";
193021 if( zRet==0 ) break;
193022 }
193023 }
193024
193025 return zRet;
193026}
193027
193028static char *sessionExprCompareOther(
193029 int nCol,
193030 const char *zDb1, const char *zDb2,
193031 const char *zTab,
193032 const char **azCol, u8 *abPK
193033){
193034 int i;
193035 const char *zSep = "";
193036 char *zRet = 0;
193037 int bHave = 0;
193038
193039 for(i=0; i<nCol; i++){
193040 if( abPK[i]==0 ){
193041 bHave = 1;
193042 zRet = sqlite3_mprintf(
193043 "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
193044 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
193045 );
193046 zSep = " OR ";
193047 if( zRet==0 ) break;
193048 }
193049 }
193050
193051 if( bHave==0 ){
193052 assert( zRet==0 );
193053 zRet = sqlite3_mprintf("0");
193054 }
193055
193056 return zRet;
193057}
193058
193059static char *sessionSelectFindNew(
193060 int nCol,
193061 const char *zDb1, /* Pick rows in this db only */
193062 const char *zDb2, /* But not in this one */
193063 const char *zTbl, /* Table name */
193064 const char *zExpr
193065){
193066 char *zRet = sqlite3_mprintf(
193067 "SELECT * FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
193068 " SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
193069 ")",
193070 zDb1, zTbl, zDb2, zTbl, zExpr
193071 );
193072 return zRet;
193073}
193074
193075static int sessionDiffFindNew(
193076 int op,
193077 sqlite3_session *pSession,
193078 SessionTable *pTab,
193079 const char *zDb1,
193080 const char *zDb2,
193081 char *zExpr
193082){
193083 int rc = SQLITE_OK;
193084 char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
193085
193086 if( zStmt==0 ){
193087 rc = SQLITE_NOMEM;
193088 }else{
193089 sqlite3_stmt *pStmt;
193090 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
193091 if( rc==SQLITE_OK ){
193092 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
193093 pDiffCtx->pStmt = pStmt;
193094 pDiffCtx->nOldOff = 0;
193095 while( SQLITE_ROW==sqlite3_step(pStmt) ){
193096 sessionPreupdateOneChange(op, pSession, pTab);
193097 }
193098 rc = sqlite3_finalize(pStmt);
193099 }
193100 sqlite3_free(zStmt);
193101 }
193102
193103 return rc;
193104}
193105
193106static int sessionDiffFindModified(
193107 sqlite3_session *pSession,
193108 SessionTable *pTab,
193109 const char *zFrom,
193110 const char *zExpr
193111){
193112 int rc = SQLITE_OK;
193113
193114 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
193115 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
193116 );
193117 if( zExpr2==0 ){
193118 rc = SQLITE_NOMEM;
193119 }else{
193120 char *zStmt = sqlite3_mprintf(
193121 "SELECT * FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
193122 pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
193123 );
193124 if( zStmt==0 ){
193125 rc = SQLITE_NOMEM;
193126 }else{
193127 sqlite3_stmt *pStmt;
193128 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
193129
193130 if( rc==SQLITE_OK ){
193131 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
193132 pDiffCtx->pStmt = pStmt;
193133 pDiffCtx->nOldOff = pTab->nCol;
193134 while( SQLITE_ROW==sqlite3_step(pStmt) ){
193135 sessionPreupdateOneChange(SQLITE_UPDATE, pSession, pTab);
193136 }
193137 rc = sqlite3_finalize(pStmt);
193138 }
193139 sqlite3_free(zStmt);
193140 }
193141 }
193142
193143 return rc;
193144}
193145
193146SQLITE_API int sqlite3session_diff(
193147 sqlite3_session *pSession,
193148 const char *zFrom,
193149 const char *zTbl,
193150 char **pzErrMsg
193151){
193152 const char *zDb = pSession->zDb;
193153 int rc = pSession->rc;
193154 SessionDiffCtx d;
193155
193156 memset(&d, 0, sizeof(d));
193157 sessionDiffHooks(pSession, &d);
193158
193159 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
193160 if( pzErrMsg ) *pzErrMsg = 0;
193161 if( rc==SQLITE_OK ){
193162 char *zExpr = 0;
193163 sqlite3 *db = pSession->db;
193164 SessionTable *pTo; /* Table zTbl */
193165
193166 /* Locate and if necessary initialize the target table object */
193167 rc = sessionFindTable(pSession, zTbl, &pTo);
193168 if( pTo==0 ) goto diff_out;
193169 if( sessionInitTable(pSession, pTo) ){
193170 rc = pSession->rc;
193171 goto diff_out;
193172 }
193173
193174 /* Check the table schemas match */
193175 if( rc==SQLITE_OK ){
193176 int bHasPk = 0;
193177 int bMismatch = 0;
193178 int nCol; /* Columns in zFrom.zTbl */
193179 u8 *abPK;
193180 const char **azCol = 0;
193181 rc = sessionTableInfo(db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
193182 if( rc==SQLITE_OK ){
193183 if( pTo->nCol!=nCol ){
193184 bMismatch = 1;
193185 }else{
193186 int i;
193187 for(i=0; i<nCol; i++){
193188 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
193189 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
193190 if( abPK[i] ) bHasPk = 1;
193191 }
193192 }
193193 }
193194 sqlite3_free((char*)azCol);
193195 if( bMismatch ){
193196 *pzErrMsg = sqlite3_mprintf("table schemas do not match");
193197 rc = SQLITE_SCHEMA;
193198 }
193199 if( bHasPk==0 ){
193200 /* Ignore tables with no primary keys */
193201 goto diff_out;
193202 }
193203 }
193204
193205 if( rc==SQLITE_OK ){
193206 zExpr = sessionExprComparePK(pTo->nCol,
193207 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
193208 );
193209 }
193210
193211 /* Find new rows */
193212 if( rc==SQLITE_OK ){
193213 rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr);
193214 }
193215
193216 /* Find old rows */
193217 if( rc==SQLITE_OK ){
193218 rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr);
193219 }
193220
193221 /* Find modified rows */
193222 if( rc==SQLITE_OK ){
193223 rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
193224 }
193225
193226 sqlite3_free(zExpr);
193227 }
193228
193229 diff_out:
193230 sessionPreupdateHooks(pSession);
193231 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
193232 return rc;
193233}
193234
193235/*
193236** Create a session object. This session object will record changes to
193237** database zDb attached to connection db.
193238*/
193239SQLITE_API int sqlite3session_create(
193240 sqlite3 *db, /* Database handle */
193241 const char *zDb, /* Name of db (e.g. "main") */
193242 sqlite3_session **ppSession /* OUT: New session object */
193243){
193244 sqlite3_session *pNew; /* Newly allocated session object */
193245 sqlite3_session *pOld; /* Session object already attached to db */
193246 int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
193247
193248 /* Zero the output value in case an error occurs. */
193249 *ppSession = 0;
193250
193251 /* Allocate and populate the new session object. */
193252 pNew = (sqlite3_session *)sqlite3_malloc(sizeof(sqlite3_session) + nDb + 1);
193253 if( !pNew ) return SQLITE_NOMEM;
193254 memset(pNew, 0, sizeof(sqlite3_session));
193255 pNew->db = db;
193256 pNew->zDb = (char *)&pNew[1];
193257 pNew->bEnable = 1;
193258 memcpy(pNew->zDb, zDb, nDb+1);
193259 sessionPreupdateHooks(pNew);
193260
193261 /* Add the new session object to the linked list of session objects
193262 ** attached to database handle $db. Do this under the cover of the db
193263 ** handle mutex. */
193264 sqlite3_mutex_enter(sqlite3_db_mutex(db));
193265 pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
193266 pNew->pNext = pOld;
193267 sqlite3_mutex_leave(sqlite3_db_mutex(db));
193268
193269 *ppSession = pNew;
193270 return SQLITE_OK;
193271}
193272
193273/*
193274** Free the list of table objects passed as the first argument. The contents
193275** of the changed-rows hash tables are also deleted.
193276*/
193277static void sessionDeleteTable(SessionTable *pList){
193278 SessionTable *pNext;
193279 SessionTable *pTab;
193280
193281 for(pTab=pList; pTab; pTab=pNext){
193282 int i;
193283 pNext = pTab->pNext;
193284 for(i=0; i<pTab->nChange; i++){
193285 SessionChange *p;
193286 SessionChange *pNextChange;
193287 for(p=pTab->apChange[i]; p; p=pNextChange){
193288 pNextChange = p->pNext;
193289 sqlite3_free(p);
193290 }
193291 }
193292 sqlite3_free((char*)pTab->azCol); /* cast works around VC++ bug */
193293 sqlite3_free(pTab->apChange);
193294 sqlite3_free(pTab);
193295 }
193296}
193297
193298/*
193299** Delete a session object previously allocated using sqlite3session_create().
193300*/
193301SQLITE_API void sqlite3session_delete(sqlite3_session *pSession){
193302 sqlite3 *db = pSession->db;
193303 sqlite3_session *pHead;
193304 sqlite3_session **pp;
193305
193306 /* Unlink the session from the linked list of sessions attached to the
193307 ** database handle. Hold the db mutex while doing so. */
193308 sqlite3_mutex_enter(sqlite3_db_mutex(db));
193309 pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
193310 for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
193311 if( (*pp)==pSession ){
193312 *pp = (*pp)->pNext;
193313 if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
193314 break;
193315 }
193316 }
193317 sqlite3_mutex_leave(sqlite3_db_mutex(db));
193318 sqlite3ValueFree(pSession->pZeroBlob);
193319
193320 /* Delete all attached table objects. And the contents of their
193321 ** associated hash-tables. */
193322 sessionDeleteTable(pSession->pTable);
193323
193324 /* Free the session object itself. */
193325 sqlite3_free(pSession);
193326}
193327
193328/*
193329** Set a table filter on a Session Object.
193330*/
193331SQLITE_API void sqlite3session_table_filter(
193332 sqlite3_session *pSession,
193333 int(*xFilter)(void*, const char*),
193334 void *pCtx /* First argument passed to xFilter */
193335){
193336 pSession->bAutoAttach = 1;
193337 pSession->pFilterCtx = pCtx;
193338 pSession->xTableFilter = xFilter;
193339}
193340
193341/*
193342** Attach a table to a session. All subsequent changes made to the table
193343** while the session object is enabled will be recorded.
193344**
193345** Only tables that have a PRIMARY KEY defined may be attached. It does
193346** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
193347** or not.
193348*/
193349SQLITE_API int sqlite3session_attach(
193350 sqlite3_session *pSession, /* Session object */
193351 const char *zName /* Table name */
193352){
193353 int rc = SQLITE_OK;
193354 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
193355
193356 if( !zName ){
193357 pSession->bAutoAttach = 1;
193358 }else{
193359 SessionTable *pTab; /* New table object (if required) */
193360 int nName; /* Number of bytes in string zName */
193361
193362 /* First search for an existing entry. If one is found, this call is
193363 ** a no-op. Return early. */
193364 nName = sqlite3Strlen30(zName);
193365 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
193366 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
193367 }
193368
193369 if( !pTab ){
193370 /* Allocate new SessionTable object. */
193371 pTab = (SessionTable *)sqlite3_malloc(sizeof(SessionTable) + nName + 1);
193372 if( !pTab ){
193373 rc = SQLITE_NOMEM;
193374 }else{
193375 /* Populate the new SessionTable object and link it into the list.
193376 ** The new object must be linked onto the end of the list, not
193377 ** simply added to the start of it in order to ensure that tables
193378 ** appear in the correct order when a changeset or patchset is
193379 ** eventually generated. */
193380 SessionTable **ppTab;
193381 memset(pTab, 0, sizeof(SessionTable));
193382 pTab->zName = (char *)&pTab[1];
193383 memcpy(pTab->zName, zName, nName+1);
193384 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
193385 *ppTab = pTab;
193386 }
193387 }
193388 }
193389
193390 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
193391 return rc;
193392}
193393
193394/*
193395** Ensure that there is room in the buffer to append nByte bytes of data.
193396** If not, use sqlite3_realloc() to grow the buffer so that there is.
193397**
193398** If successful, return zero. Otherwise, if an OOM condition is encountered,
193399** set *pRc to SQLITE_NOMEM and return non-zero.
193400*/
193401static int sessionBufferGrow(SessionBuffer *p, int nByte, int *pRc){
193402 if( *pRc==SQLITE_OK && p->nAlloc-p->nBuf<nByte ){
193403 u8 *aNew;
193404 int nNew = p->nAlloc ? p->nAlloc : 128;
193405 do {
193406 nNew = nNew*2;
193407 }while( nNew<(p->nBuf+nByte) );
193408
193409 aNew = (u8 *)sqlite3_realloc(p->aBuf, nNew);
193410 if( 0==aNew ){
193411 *pRc = SQLITE_NOMEM;
193412 }else{
193413 p->aBuf = aNew;
193414 p->nAlloc = nNew;
193415 }
193416 }
193417 return (*pRc!=SQLITE_OK);
193418}
193419
193420/*
193421** Append the value passed as the second argument to the buffer passed
193422** as the first.
193423**
193424** This function is a no-op if *pRc is non-zero when it is called.
193425** Otherwise, if an error occurs, *pRc is set to an SQLite error code
193426** before returning.
193427*/
193428static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
193429 int rc = *pRc;
193430 if( rc==SQLITE_OK ){
193431 int nByte = 0;
193432 rc = sessionSerializeValue(0, pVal, &nByte);
193433 sessionBufferGrow(p, nByte, &rc);
193434 if( rc==SQLITE_OK ){
193435 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
193436 p->nBuf += nByte;
193437 }else{
193438 *pRc = rc;
193439 }
193440 }
193441}
193442
193443/*
193444** This function is a no-op if *pRc is other than SQLITE_OK when it is
193445** called. Otherwise, append a single byte to the buffer.
193446**
193447** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
193448** returning.
193449*/
193450static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
193451 if( 0==sessionBufferGrow(p, 1, pRc) ){
193452 p->aBuf[p->nBuf++] = v;
193453 }
193454}
193455
193456/*
193457** This function is a no-op if *pRc is other than SQLITE_OK when it is
193458** called. Otherwise, append a single varint to the buffer.
193459**
193460** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
193461** returning.
193462*/
193463static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
193464 if( 0==sessionBufferGrow(p, 9, pRc) ){
193465 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
193466 }
193467}
193468
193469/*
193470** This function is a no-op if *pRc is other than SQLITE_OK when it is
193471** called. Otherwise, append a blob of data to the buffer.
193472**
193473** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
193474** returning.
193475*/
193476static void sessionAppendBlob(
193477 SessionBuffer *p,
193478 const u8 *aBlob,
193479 int nBlob,
193480 int *pRc
193481){
193482 if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
193483 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
193484 p->nBuf += nBlob;
193485 }
193486}
193487
193488/*
193489** This function is a no-op if *pRc is other than SQLITE_OK when it is
193490** called. Otherwise, append a string to the buffer. All bytes in the string
193491** up to (but not including) the nul-terminator are written to the buffer.
193492**
193493** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
193494** returning.
193495*/
193496static void sessionAppendStr(
193497 SessionBuffer *p,
193498 const char *zStr,
193499 int *pRc
193500){
193501 int nStr = sqlite3Strlen30(zStr);
193502 if( 0==sessionBufferGrow(p, nStr, pRc) ){
193503 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
193504 p->nBuf += nStr;
193505 }
193506}
193507
193508/*
193509** This function is a no-op if *pRc is other than SQLITE_OK when it is
193510** called. Otherwise, append the string representation of integer iVal
193511** to the buffer. No nul-terminator is written.
193512**
193513** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
193514** returning.
193515*/
193516static void sessionAppendInteger(
193517 SessionBuffer *p, /* Buffer to append to */
193518 int iVal, /* Value to write the string rep. of */
193519 int *pRc /* IN/OUT: Error code */
193520){
193521 char aBuf[24];
193522 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
193523 sessionAppendStr(p, aBuf, pRc);
193524}
193525
193526/*
193527** This function is a no-op if *pRc is other than SQLITE_OK when it is
193528** called. Otherwise, append the string zStr enclosed in quotes (") and
193529** with any embedded quote characters escaped to the buffer. No
193530** nul-terminator byte is written.
193531**
193532** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
193533** returning.
193534*/
193535static void sessionAppendIdent(
193536 SessionBuffer *p, /* Buffer to a append to */
193537 const char *zStr, /* String to quote, escape and append */
193538 int *pRc /* IN/OUT: Error code */
193539){
193540 int nStr = sqlite3Strlen30(zStr)*2 + 2 + 1;
193541 if( 0==sessionBufferGrow(p, nStr, pRc) ){
193542 char *zOut = (char *)&p->aBuf[p->nBuf];
193543 const char *zIn = zStr;
193544 *zOut++ = '"';
193545 while( *zIn ){
193546 if( *zIn=='"' ) *zOut++ = '"';
193547 *zOut++ = *(zIn++);
193548 }
193549 *zOut++ = '"';
193550 p->nBuf = (int)((u8 *)zOut - p->aBuf);
193551 }
193552}
193553
193554/*
193555** This function is a no-op if *pRc is other than SQLITE_OK when it is
193556** called. Otherwse, it appends the serialized version of the value stored
193557** in column iCol of the row that SQL statement pStmt currently points
193558** to to the buffer.
193559*/
193560static void sessionAppendCol(
193561 SessionBuffer *p, /* Buffer to append to */
193562 sqlite3_stmt *pStmt, /* Handle pointing to row containing value */
193563 int iCol, /* Column to read value from */
193564 int *pRc /* IN/OUT: Error code */
193565){
193566 if( *pRc==SQLITE_OK ){
193567 int eType = sqlite3_column_type(pStmt, iCol);
193568 sessionAppendByte(p, (u8)eType, pRc);
193569 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
193570 sqlite3_int64 i;
193571 u8 aBuf[8];
193572 if( eType==SQLITE_INTEGER ){
193573 i = sqlite3_column_int64(pStmt, iCol);
193574 }else{
193575 double r = sqlite3_column_double(pStmt, iCol);
193576 memcpy(&i, &r, 8);
193577 }
193578 sessionPutI64(aBuf, i);
193579 sessionAppendBlob(p, aBuf, 8, pRc);
193580 }
193581 if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){
193582 u8 *z;
193583 int nByte;
193584 if( eType==SQLITE_BLOB ){
193585 z = (u8 *)sqlite3_column_blob(pStmt, iCol);
193586 }else{
193587 z = (u8 *)sqlite3_column_text(pStmt, iCol);
193588 }
193589 nByte = sqlite3_column_bytes(pStmt, iCol);
193590 if( z || (eType==SQLITE_BLOB && nByte==0) ){
193591 sessionAppendVarint(p, nByte, pRc);
193592 sessionAppendBlob(p, z, nByte, pRc);
193593 }else{
193594 *pRc = SQLITE_NOMEM;
193595 }
193596 }
193597 }
193598}
193599
193600/*
193601**
193602** This function appends an update change to the buffer (see the comments
193603** under "CHANGESET FORMAT" at the top of the file). An update change
193604** consists of:
193605**
193606** 1 byte: SQLITE_UPDATE (0x17)
193607** n bytes: old.* record (see RECORD FORMAT)
193608** m bytes: new.* record (see RECORD FORMAT)
193609**
193610** The SessionChange object passed as the third argument contains the
193611** values that were stored in the row when the session began (the old.*
193612** values). The statement handle passed as the second argument points
193613** at the current version of the row (the new.* values).
193614**
193615** If all of the old.* values are equal to their corresponding new.* value
193616** (i.e. nothing has changed), then no data at all is appended to the buffer.
193617**
193618** Otherwise, the old.* record contains all primary key values and the
193619** original values of any fields that have been modified. The new.* record
193620** contains the new values of only those fields that have been modified.
193621*/
193622static int sessionAppendUpdate(
193623 SessionBuffer *pBuf, /* Buffer to append to */
193624 int bPatchset, /* True for "patchset", 0 for "changeset" */
193625 sqlite3_stmt *pStmt, /* Statement handle pointing at new row */
193626 SessionChange *p, /* Object containing old values */
193627 u8 *abPK /* Boolean array - true for PK columns */
193628){
193629 int rc = SQLITE_OK;
193630 SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
193631 int bNoop = 1; /* Set to zero if any values are modified */
193632 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
193633 int i; /* Used to iterate through columns */
193634 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
193635
193636 sessionAppendByte(pBuf, SQLITE_UPDATE, &rc);
193637 sessionAppendByte(pBuf, p->bIndirect, &rc);
193638 for(i=0; i<sqlite3_column_count(pStmt); i++){
193639 int bChanged = 0;
193640 int nAdvance;
193641 int eType = *pCsr;
193642 switch( eType ){
193643 case SQLITE_NULL:
193644 nAdvance = 1;
193645 if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
193646 bChanged = 1;
193647 }
193648 break;
193649
193650 case SQLITE_FLOAT:
193651 case SQLITE_INTEGER: {
193652 nAdvance = 9;
193653 if( eType==sqlite3_column_type(pStmt, i) ){
193654 sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
193655 if( eType==SQLITE_INTEGER ){
193656 if( iVal==sqlite3_column_int64(pStmt, i) ) break;
193657 }else{
193658 double dVal;
193659 memcpy(&dVal, &iVal, 8);
193660 if( dVal==sqlite3_column_double(pStmt, i) ) break;
193661 }
193662 }
193663 bChanged = 1;
193664 break;
193665 }
193666
193667 default: {
193668 int n;
193669 int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
193670 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
193671 nAdvance = nHdr + n;
193672 if( eType==sqlite3_column_type(pStmt, i)
193673 && n==sqlite3_column_bytes(pStmt, i)
193674 && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
193675 ){
193676 break;
193677 }
193678 bChanged = 1;
193679 }
193680 }
193681
193682 /* If at least one field has been modified, this is not a no-op. */
193683 if( bChanged ) bNoop = 0;
193684
193685 /* Add a field to the old.* record. This is omitted if this modules is
193686 ** currently generating a patchset. */
193687 if( bPatchset==0 ){
193688 if( bChanged || abPK[i] ){
193689 sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
193690 }else{
193691 sessionAppendByte(pBuf, 0, &rc);
193692 }
193693 }
193694
193695 /* Add a field to the new.* record. Or the only record if currently
193696 ** generating a patchset. */
193697 if( bChanged || (bPatchset && abPK[i]) ){
193698 sessionAppendCol(&buf2, pStmt, i, &rc);
193699 }else{
193700 sessionAppendByte(&buf2, 0, &rc);
193701 }
193702
193703 pCsr += nAdvance;
193704 }
193705
193706 if( bNoop ){
193707 pBuf->nBuf = nRewind;
193708 }else{
193709 sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
193710 }
193711 sqlite3_free(buf2.aBuf);
193712
193713 return rc;
193714}
193715
193716/*
193717** Append a DELETE change to the buffer passed as the first argument. Use
193718** the changeset format if argument bPatchset is zero, or the patchset
193719** format otherwise.
193720*/
193721static int sessionAppendDelete(
193722 SessionBuffer *pBuf, /* Buffer to append to */
193723 int bPatchset, /* True for "patchset", 0 for "changeset" */
193724 SessionChange *p, /* Object containing old values */
193725 int nCol, /* Number of columns in table */
193726 u8 *abPK /* Boolean array - true for PK columns */
193727){
193728 int rc = SQLITE_OK;
193729
193730 sessionAppendByte(pBuf, SQLITE_DELETE, &rc);
193731 sessionAppendByte(pBuf, p->bIndirect, &rc);
193732
193733 if( bPatchset==0 ){
193734 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
193735 }else{
193736 int i;
193737 u8 *a = p->aRecord;
193738 for(i=0; i<nCol; i++){
193739 u8 *pStart = a;
193740 int eType = *a++;
193741
193742 switch( eType ){
193743 case 0:
193744 case SQLITE_NULL:
193745 assert( abPK[i]==0 );
193746 break;
193747
193748 case SQLITE_FLOAT:
193749 case SQLITE_INTEGER:
193750 a += 8;
193751 break;
193752
193753 default: {
193754 int n;
193755 a += sessionVarintGet(a, &n);
193756 a += n;
193757 break;
193758 }
193759 }
193760 if( abPK[i] ){
193761 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
193762 }
193763 }
193764 assert( (a - p->aRecord)==p->nRecord );
193765 }
193766
193767 return rc;
193768}
193769
193770/*
193771** Formulate and prepare a SELECT statement to retrieve a row from table
193772** zTab in database zDb based on its primary key. i.e.
193773**
193774** SELECT * FROM zDb.zTab WHERE pk1 = ? AND pk2 = ? AND ...
193775*/
193776static int sessionSelectStmt(
193777 sqlite3 *db, /* Database handle */
193778 const char *zDb, /* Database name */
193779 const char *zTab, /* Table name */
193780 int nCol, /* Number of columns in table */
193781 const char **azCol, /* Names of table columns */
193782 u8 *abPK, /* PRIMARY KEY array */
193783 sqlite3_stmt **ppStmt /* OUT: Prepared SELECT statement */
193784){
193785 int rc = SQLITE_OK;
193786 char *zSql = 0;
193787 int nSql = -1;
193788
193789 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
193790 zSql = sqlite3_mprintf(
193791 "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
193792 "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
193793 );
193794 if( zSql==0 ) rc = SQLITE_NOMEM;
193795 }else{
193796 int i;
193797 const char *zSep = "";
193798 SessionBuffer buf = {0, 0, 0};
193799
193800 sessionAppendStr(&buf, "SELECT * FROM ", &rc);
193801 sessionAppendIdent(&buf, zDb, &rc);
193802 sessionAppendStr(&buf, ".", &rc);
193803 sessionAppendIdent(&buf, zTab, &rc);
193804 sessionAppendStr(&buf, " WHERE ", &rc);
193805 for(i=0; i<nCol; i++){
193806 if( abPK[i] ){
193807 sessionAppendStr(&buf, zSep, &rc);
193808 sessionAppendIdent(&buf, azCol[i], &rc);
193809 sessionAppendStr(&buf, " IS ?", &rc);
193810 sessionAppendInteger(&buf, i+1, &rc);
193811 zSep = " AND ";
193812 }
193813 }
193814 zSql = (char*)buf.aBuf;
193815 nSql = buf.nBuf;
193816 }
193817
193818 if( rc==SQLITE_OK ){
193819 rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
193820 }
193821 sqlite3_free(zSql);
193822 return rc;
193823}
193824
193825/*
193826** Bind the PRIMARY KEY values from the change passed in argument pChange
193827** to the SELECT statement passed as the first argument. The SELECT statement
193828** is as prepared by function sessionSelectStmt().
193829**
193830** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
193831** error code (e.g. SQLITE_NOMEM) otherwise.
193832*/
193833static int sessionSelectBind(
193834 sqlite3_stmt *pSelect, /* SELECT from sessionSelectStmt() */
193835 int nCol, /* Number of columns in table */
193836 u8 *abPK, /* PRIMARY KEY array */
193837 SessionChange *pChange /* Change structure */
193838){
193839 int i;
193840 int rc = SQLITE_OK;
193841 u8 *a = pChange->aRecord;
193842
193843 for(i=0; i<nCol && rc==SQLITE_OK; i++){
193844 int eType = *a++;
193845
193846 switch( eType ){
193847 case 0:
193848 case SQLITE_NULL:
193849 assert( abPK[i]==0 );
193850 break;
193851
193852 case SQLITE_INTEGER: {
193853 if( abPK[i] ){
193854 i64 iVal = sessionGetI64(a);
193855 rc = sqlite3_bind_int64(pSelect, i+1, iVal);
193856 }
193857 a += 8;
193858 break;
193859 }
193860
193861 case SQLITE_FLOAT: {
193862 if( abPK[i] ){
193863 double rVal;
193864 i64 iVal = sessionGetI64(a);
193865 memcpy(&rVal, &iVal, 8);
193866 rc = sqlite3_bind_double(pSelect, i+1, rVal);
193867 }
193868 a += 8;
193869 break;
193870 }
193871
193872 case SQLITE_TEXT: {
193873 int n;
193874 a += sessionVarintGet(a, &n);
193875 if( abPK[i] ){
193876 rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT);
193877 }
193878 a += n;
193879 break;
193880 }
193881
193882 default: {
193883 int n;
193884 assert( eType==SQLITE_BLOB );
193885 a += sessionVarintGet(a, &n);
193886 if( abPK[i] ){
193887 rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT);
193888 }
193889 a += n;
193890 break;
193891 }
193892 }
193893 }
193894
193895 return rc;
193896}
193897
193898/*
193899** This function is a no-op if *pRc is set to other than SQLITE_OK when it
193900** is called. Otherwise, append a serialized table header (part of the binary
193901** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
193902** SQLite error code before returning.
193903*/
193904static void sessionAppendTableHdr(
193905 SessionBuffer *pBuf, /* Append header to this buffer */
193906 int bPatchset, /* Use the patchset format if true */
193907 SessionTable *pTab, /* Table object to append header for */
193908 int *pRc /* IN/OUT: Error code */
193909){
193910 /* Write a table header */
193911 sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
193912 sessionAppendVarint(pBuf, pTab->nCol, pRc);
193913 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
193914 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
193915}
193916
193917/*
193918** Generate either a changeset (if argument bPatchset is zero) or a patchset
193919** (if it is non-zero) based on the current contents of the session object
193920** passed as the first argument.
193921**
193922** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
193923** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
193924** occurs, an SQLite error code is returned and both output variables set
193925** to 0.
193926*/
193927static int sessionGenerateChangeset(
193928 sqlite3_session *pSession, /* Session object */
193929 int bPatchset, /* True for patchset, false for changeset */
193930 int (*xOutput)(void *pOut, const void *pData, int nData),
193931 void *pOut, /* First argument for xOutput */
193932 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
193933 void **ppChangeset /* OUT: Buffer containing changeset */
193934){
193935 sqlite3 *db = pSession->db; /* Source database handle */
193936 SessionTable *pTab; /* Used to iterate through attached tables */
193937 SessionBuffer buf = {0,0,0}; /* Buffer in which to accumlate changeset */
193938 int rc; /* Return code */
193939
193940 assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0 ) );
193941
193942 /* Zero the output variables in case an error occurs. If this session
193943 ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
193944 ** this call will be a no-op. */
193945 if( xOutput==0 ){
193946 *pnChangeset = 0;
193947 *ppChangeset = 0;
193948 }
193949
193950 if( pSession->rc ) return pSession->rc;
193951 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
193952 if( rc!=SQLITE_OK ) return rc;
193953
193954 sqlite3_mutex_enter(sqlite3_db_mutex(db));
193955
193956 for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
193957 if( pTab->nEntry ){
193958 const char *zName = pTab->zName;
193959 int nCol; /* Number of columns in table */
193960 u8 *abPK; /* Primary key array */
193961 const char **azCol = 0; /* Table columns */
193962 int i; /* Used to iterate through hash buckets */
193963 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
193964 int nRewind = buf.nBuf; /* Initial size of write buffer */
193965 int nNoop; /* Size of buffer after writing tbl header */
193966
193967 /* Check the table schema is still Ok. */
193968 rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
193969 if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
193970 rc = SQLITE_SCHEMA;
193971 }
193972
193973 /* Write a table header */
193974 sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
193975
193976 /* Build and compile a statement to execute: */
193977 if( rc==SQLITE_OK ){
193978 rc = sessionSelectStmt(
193979 db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
193980 }
193981
193982 nNoop = buf.nBuf;
193983 for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
193984 SessionChange *p; /* Used to iterate through changes */
193985
193986 for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
193987 rc = sessionSelectBind(pSel, nCol, abPK, p);
193988 if( rc!=SQLITE_OK ) continue;
193989 if( sqlite3_step(pSel)==SQLITE_ROW ){
193990 if( p->op==SQLITE_INSERT ){
193991 int iCol;
193992 sessionAppendByte(&buf, SQLITE_INSERT, &rc);
193993 sessionAppendByte(&buf, p->bIndirect, &rc);
193994 for(iCol=0; iCol<nCol; iCol++){
193995 sessionAppendCol(&buf, pSel, iCol, &rc);
193996 }
193997 }else{
193998 rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
193999 }
194000 }else if( p->op!=SQLITE_INSERT ){
194001 rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
194002 }
194003 if( rc==SQLITE_OK ){
194004 rc = sqlite3_reset(pSel);
194005 }
194006
194007 /* If the buffer is now larger than SESSIONS_STRM_CHUNK_SIZE, pass
194008 ** its contents to the xOutput() callback. */
194009 if( xOutput
194010 && rc==SQLITE_OK
194011 && buf.nBuf>nNoop
194012 && buf.nBuf>SESSIONS_STRM_CHUNK_SIZE
194013 ){
194014 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
194015 nNoop = -1;
194016 buf.nBuf = 0;
194017 }
194018
194019 }
194020 }
194021
194022 sqlite3_finalize(pSel);
194023 if( buf.nBuf==nNoop ){
194024 buf.nBuf = nRewind;
194025 }
194026 sqlite3_free((char*)azCol); /* cast works around VC++ bug */
194027 }
194028 }
194029
194030 if( rc==SQLITE_OK ){
194031 if( xOutput==0 ){
194032 *pnChangeset = buf.nBuf;
194033 *ppChangeset = buf.aBuf;
194034 buf.aBuf = 0;
194035 }else if( buf.nBuf>0 ){
194036 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
194037 }
194038 }
194039
194040 sqlite3_free(buf.aBuf);
194041 sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
194042 sqlite3_mutex_leave(sqlite3_db_mutex(db));
194043 return rc;
194044}
194045
194046/*
194047** Obtain a changeset object containing all changes recorded by the
194048** session object passed as the first argument.
194049**
194050** It is the responsibility of the caller to eventually free the buffer
194051** using sqlite3_free().
194052*/
194053SQLITE_API int sqlite3session_changeset(
194054 sqlite3_session *pSession, /* Session object */
194055 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
194056 void **ppChangeset /* OUT: Buffer containing changeset */
194057){
194058 return sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
194059}
194060
194061/*
194062** Streaming version of sqlite3session_changeset().
194063*/
194064SQLITE_API int sqlite3session_changeset_strm(
194065 sqlite3_session *pSession,
194066 int (*xOutput)(void *pOut, const void *pData, int nData),
194067 void *pOut
194068){
194069 return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
194070}
194071
194072/*
194073** Streaming version of sqlite3session_patchset().
194074*/
194075SQLITE_API int sqlite3session_patchset_strm(
194076 sqlite3_session *pSession,
194077 int (*xOutput)(void *pOut, const void *pData, int nData),
194078 void *pOut
194079){
194080 return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
194081}
194082
194083/*
194084** Obtain a patchset object containing all changes recorded by the
194085** session object passed as the first argument.
194086**
194087** It is the responsibility of the caller to eventually free the buffer
194088** using sqlite3_free().
194089*/
194090SQLITE_API int sqlite3session_patchset(
194091 sqlite3_session *pSession, /* Session object */
194092 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
194093 void **ppPatchset /* OUT: Buffer containing changeset */
194094){
194095 return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
194096}
194097
194098/*
194099** Enable or disable the session object passed as the first argument.
194100*/
194101SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
194102 int ret;
194103 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
194104 if( bEnable>=0 ){
194105 pSession->bEnable = bEnable;
194106 }
194107 ret = pSession->bEnable;
194108 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
194109 return ret;
194110}
194111
194112/*
194113** Enable or disable the session object passed as the first argument.
194114*/
194115SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
194116 int ret;
194117 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
194118 if( bIndirect>=0 ){
194119 pSession->bIndirect = bIndirect;
194120 }
194121 ret = pSession->bIndirect;
194122 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
194123 return ret;
194124}
194125
194126/*
194127** Return true if there have been no changes to monitored tables recorded
194128** by the session object passed as the only argument.
194129*/
194130SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession){
194131 int ret = 0;
194132 SessionTable *pTab;
194133
194134 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
194135 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
194136 ret = (pTab->nEntry>0);
194137 }
194138 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
194139
194140 return (ret==0);
194141}
194142
194143/*
194144** Do the work for either sqlite3changeset_start() or start_strm().
194145*/
194146static int sessionChangesetStart(
194147 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
194148 int (*xInput)(void *pIn, void *pData, int *pnData),
194149 void *pIn,
194150 int nChangeset, /* Size of buffer pChangeset in bytes */
194151 void *pChangeset /* Pointer to buffer containing changeset */
194152){
194153 sqlite3_changeset_iter *pRet; /* Iterator to return */
194154 int nByte; /* Number of bytes to allocate for iterator */
194155
194156 assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
194157
194158 /* Zero the output variable in case an error occurs. */
194159 *pp = 0;
194160
194161 /* Allocate and initialize the iterator structure. */
194162 nByte = sizeof(sqlite3_changeset_iter);
194163 pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
194164 if( !pRet ) return SQLITE_NOMEM;
194165 memset(pRet, 0, sizeof(sqlite3_changeset_iter));
194166 pRet->in.aData = (u8 *)pChangeset;
194167 pRet->in.nData = nChangeset;
194168 pRet->in.xInput = xInput;
194169 pRet->in.pIn = pIn;
194170 pRet->in.bEof = (xInput ? 0 : 1);
194171
194172 /* Populate the output variable and return success. */
194173 *pp = pRet;
194174 return SQLITE_OK;
194175}
194176
194177/*
194178** Create an iterator used to iterate through the contents of a changeset.
194179*/
194180SQLITE_API int sqlite3changeset_start(
194181 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
194182 int nChangeset, /* Size of buffer pChangeset in bytes */
194183 void *pChangeset /* Pointer to buffer containing changeset */
194184){
194185 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset);
194186}
194187
194188/*
194189** Streaming version of sqlite3changeset_start().
194190*/
194191SQLITE_API int sqlite3changeset_start_strm(
194192 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
194193 int (*xInput)(void *pIn, void *pData, int *pnData),
194194 void *pIn
194195){
194196 return sessionChangesetStart(pp, xInput, pIn, 0, 0);
194197}
194198
194199/*
194200** If the SessionInput object passed as the only argument is a streaming
194201** object and the buffer is full, discard some data to free up space.
194202*/
194203static void sessionDiscardData(SessionInput *pIn){
194204 if( pIn->xInput && pIn->iNext>=SESSIONS_STRM_CHUNK_SIZE ){
194205 int nMove = pIn->buf.nBuf - pIn->iNext;
194206 assert( nMove>=0 );
194207 if( nMove>0 ){
194208 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
194209 }
194210 pIn->buf.nBuf -= pIn->iNext;
194211 pIn->iNext = 0;
194212 pIn->nData = pIn->buf.nBuf;
194213 }
194214}
194215
194216/*
194217** Ensure that there are at least nByte bytes available in the buffer. Or,
194218** if there are not nByte bytes remaining in the input, that all available
194219** data is in the buffer.
194220**
194221** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
194222*/
194223static int sessionInputBuffer(SessionInput *pIn, int nByte){
194224 int rc = SQLITE_OK;
194225 if( pIn->xInput ){
194226 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
194227 int nNew = SESSIONS_STRM_CHUNK_SIZE;
194228
194229 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
194230 if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
194231 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
194232 if( nNew==0 ){
194233 pIn->bEof = 1;
194234 }else{
194235 pIn->buf.nBuf += nNew;
194236 }
194237 }
194238
194239 pIn->aData = pIn->buf.aBuf;
194240 pIn->nData = pIn->buf.nBuf;
194241 }
194242 }
194243 return rc;
194244}
194245
194246/*
194247** When this function is called, *ppRec points to the start of a record
194248** that contains nCol values. This function advances the pointer *ppRec
194249** until it points to the byte immediately following that record.
194250*/
194251static void sessionSkipRecord(
194252 u8 **ppRec, /* IN/OUT: Record pointer */
194253 int nCol /* Number of values in record */
194254){
194255 u8 *aRec = *ppRec;
194256 int i;
194257 for(i=0; i<nCol; i++){
194258 int eType = *aRec++;
194259 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
194260 int nByte;
194261 aRec += sessionVarintGet((u8*)aRec, &nByte);
194262 aRec += nByte;
194263 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
194264 aRec += 8;
194265 }
194266 }
194267
194268 *ppRec = aRec;
194269}
194270
194271/*
194272** This function sets the value of the sqlite3_value object passed as the
194273** first argument to a copy of the string or blob held in the aData[]
194274** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
194275** error occurs.
194276*/
194277static int sessionValueSetStr(
194278 sqlite3_value *pVal, /* Set the value of this object */
194279 u8 *aData, /* Buffer containing string or blob data */
194280 int nData, /* Size of buffer aData[] in bytes */
194281 u8 enc /* String encoding (0 for blobs) */
194282){
194283 /* In theory this code could just pass SQLITE_TRANSIENT as the final
194284 ** argument to sqlite3ValueSetStr() and have the copy created
194285 ** automatically. But doing so makes it difficult to detect any OOM
194286 ** error. Hence the code to create the copy externally. */
194287 u8 *aCopy = sqlite3_malloc(nData+1);
194288 if( aCopy==0 ) return SQLITE_NOMEM;
194289 memcpy(aCopy, aData, nData);
194290 sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
194291 return SQLITE_OK;
194292}
194293
194294/*
194295** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
194296** for details.
194297**
194298** When this function is called, *paChange points to the start of the record
194299** to deserialize. Assuming no error occurs, *paChange is set to point to
194300** one byte after the end of the same record before this function returns.
194301** If the argument abPK is NULL, then the record contains nCol values. Or,
194302** if abPK is other than NULL, then the record contains only the PK fields
194303** (in other words, it is a patchset DELETE record).
194304**
194305** If successful, each element of the apOut[] array (allocated by the caller)
194306** is set to point to an sqlite3_value object containing the value read
194307** from the corresponding position in the record. If that value is not
194308** included in the record (i.e. because the record is part of an UPDATE change
194309** and the field was not modified), the corresponding element of apOut[] is
194310** set to NULL.
194311**
194312** It is the responsibility of the caller to free all sqlite_value structures
194313** using sqlite3_free().
194314**
194315** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
194316** The apOut[] array may have been partially populated in this case.
194317*/
194318static int sessionReadRecord(
194319 SessionInput *pIn, /* Input data */
194320 int nCol, /* Number of values in record */
194321 u8 *abPK, /* Array of primary key flags, or NULL */
194322 sqlite3_value **apOut /* Write values to this array */
194323){
194324 int i; /* Used to iterate through columns */
194325 int rc = SQLITE_OK;
194326
194327 for(i=0; i<nCol && rc==SQLITE_OK; i++){
194328 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
194329 if( abPK && abPK[i]==0 ) continue;
194330 rc = sessionInputBuffer(pIn, 9);
194331 if( rc==SQLITE_OK ){
194332 if( pIn->iNext>=pIn->nData ){
194333 rc = SQLITE_CORRUPT_BKPT;
194334 }else{
194335 eType = pIn->aData[pIn->iNext++];
194336 assert( apOut[i]==0 );
194337 if( eType ){
194338 apOut[i] = sqlite3ValueNew(0);
194339 if( !apOut[i] ) rc = SQLITE_NOMEM;
194340 }
194341 }
194342 }
194343
194344 if( rc==SQLITE_OK ){
194345 u8 *aVal = &pIn->aData[pIn->iNext];
194346 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
194347 int nByte;
194348 pIn->iNext += sessionVarintGet(aVal, &nByte);
194349 rc = sessionInputBuffer(pIn, nByte);
194350 if( rc==SQLITE_OK ){
194351 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
194352 rc = SQLITE_CORRUPT_BKPT;
194353 }else{
194354 u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
194355 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
194356 pIn->iNext += nByte;
194357 }
194358 }
194359 }
194360 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
194361 sqlite3_int64 v = sessionGetI64(aVal);
194362 if( eType==SQLITE_INTEGER ){
194363 sqlite3VdbeMemSetInt64(apOut[i], v);
194364 }else{
194365 double d;
194366 memcpy(&d, &v, 8);
194367 sqlite3VdbeMemSetDouble(apOut[i], d);
194368 }
194369 pIn->iNext += 8;
194370 }
194371 }
194372 }
194373
194374 return rc;
194375}
194376
194377/*
194378** The input pointer currently points to the second byte of a table-header.
194379** Specifically, to the following:
194380**
194381** + number of columns in table (varint)
194382** + array of PK flags (1 byte per column),
194383** + table name (nul terminated).
194384**
194385** This function ensures that all of the above is present in the input
194386** buffer (i.e. that it can be accessed without any calls to xInput()).
194387** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
194388** The input pointer is not moved.
194389*/
194390static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
194391 int rc = SQLITE_OK;
194392 int nCol = 0;
194393 int nRead = 0;
194394
194395 rc = sessionInputBuffer(pIn, 9);
194396 if( rc==SQLITE_OK ){
194397 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
194398 /* The hard upper limit for the number of columns in an SQLite
194399 ** database table is, according to sqliteLimit.h, 32676. So
194400 ** consider any table-header that purports to have more than 65536
194401 ** columns to be corrupt. This is convenient because otherwise,
194402 ** if the (nCol>65536) condition below were omitted, a sufficiently
194403 ** large value for nCol may cause nRead to wrap around and become
194404 ** negative. Leading to a crash. */
194405 if( nCol<0 || nCol>65536 ){
194406 rc = SQLITE_CORRUPT_BKPT;
194407 }else{
194408 rc = sessionInputBuffer(pIn, nRead+nCol+100);
194409 nRead += nCol;
194410 }
194411 }
194412
194413 while( rc==SQLITE_OK ){
194414 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
194415 nRead++;
194416 }
194417 if( (pIn->iNext + nRead)<pIn->nData ) break;
194418 rc = sessionInputBuffer(pIn, nRead + 100);
194419 }
194420 *pnByte = nRead+1;
194421 return rc;
194422}
194423
194424/*
194425** The input pointer currently points to the first byte of the first field
194426** of a record consisting of nCol columns. This function ensures the entire
194427** record is buffered. It does not move the input pointer.
194428**
194429** If successful, SQLITE_OK is returned and *pnByte is set to the size of
194430** the record in bytes. Otherwise, an SQLite error code is returned. The
194431** final value of *pnByte is undefined in this case.
194432*/
194433static int sessionChangesetBufferRecord(
194434 SessionInput *pIn, /* Input data */
194435 int nCol, /* Number of columns in record */
194436 int *pnByte /* OUT: Size of record in bytes */
194437){
194438 int rc = SQLITE_OK;
194439 int nByte = 0;
194440 int i;
194441 for(i=0; rc==SQLITE_OK && i<nCol; i++){
194442 int eType;
194443 rc = sessionInputBuffer(pIn, nByte + 10);
194444 if( rc==SQLITE_OK ){
194445 eType = pIn->aData[pIn->iNext + nByte++];
194446 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
194447 int n;
194448 nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
194449 nByte += n;
194450 rc = sessionInputBuffer(pIn, nByte);
194451 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
194452 nByte += 8;
194453 }
194454 }
194455 }
194456 *pnByte = nByte;
194457 return rc;
194458}
194459
194460/*
194461** The input pointer currently points to the second byte of a table-header.
194462** Specifically, to the following:
194463**
194464** + number of columns in table (varint)
194465** + array of PK flags (1 byte per column),
194466** + table name (nul terminated).
194467**
194468** This function decodes the table-header and populates the p->nCol,
194469** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
194470** also allocated or resized according to the new value of p->nCol. The
194471** input pointer is left pointing to the byte following the table header.
194472**
194473** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
194474** is returned and the final values of the various fields enumerated above
194475** are undefined.
194476*/
194477static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
194478 int rc;
194479 int nCopy;
194480 assert( p->rc==SQLITE_OK );
194481
194482 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
194483 if( rc==SQLITE_OK ){
194484 int nByte;
194485 int nVarint;
194486 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
194487 if( p->nCol>0 ){
194488 nCopy -= nVarint;
194489 p->in.iNext += nVarint;
194490 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
194491 p->tblhdr.nBuf = 0;
194492 sessionBufferGrow(&p->tblhdr, nByte, &rc);
194493 }else{
194494 rc = SQLITE_CORRUPT_BKPT;
194495 }
194496 }
194497
194498 if( rc==SQLITE_OK ){
194499 int iPK = sizeof(sqlite3_value*)*p->nCol*2;
194500 memset(p->tblhdr.aBuf, 0, iPK);
194501 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
194502 p->in.iNext += nCopy;
194503 }
194504
194505 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
194506 p->abPK = (u8*)&p->apValue[p->nCol*2];
194507 p->zTab = (char*)&p->abPK[p->nCol];
194508 return (p->rc = rc);
194509}
194510
194511/*
194512** Advance the changeset iterator to the next change.
194513**
194514** If both paRec and pnRec are NULL, then this function works like the public
194515** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
194516** sqlite3changeset_new() and old() APIs may be used to query for values.
194517**
194518** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
194519** record is written to *paRec before returning and the number of bytes in
194520** the record to *pnRec.
194521**
194522** Either way, this function returns SQLITE_ROW if the iterator is
194523** successfully advanced to the next change in the changeset, an SQLite
194524** error code if an error occurs, or SQLITE_DONE if there are no further
194525** changes in the changeset.
194526*/
194527static int sessionChangesetNext(
194528 sqlite3_changeset_iter *p, /* Changeset iterator */
194529 u8 **paRec, /* If non-NULL, store record pointer here */
194530 int *pnRec, /* If non-NULL, store size of record here */
194531 int *pbNew /* If non-NULL, true if new table */
194532){
194533 int i;
194534 u8 op;
194535
194536 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
194537
194538 /* If the iterator is in the error-state, return immediately. */
194539 if( p->rc!=SQLITE_OK ) return p->rc;
194540
194541 /* Free the current contents of p->apValue[], if any. */
194542 if( p->apValue ){
194543 for(i=0; i<p->nCol*2; i++){
194544 sqlite3ValueFree(p->apValue[i]);
194545 }
194546 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
194547 }
194548
194549 /* Make sure the buffer contains at least 10 bytes of input data, or all
194550 ** remaining data if there are less than 10 bytes available. This is
194551 ** sufficient either for the 'T' or 'P' byte and the varint that follows
194552 ** it, or for the two single byte values otherwise. */
194553 p->rc = sessionInputBuffer(&p->in, 2);
194554 if( p->rc!=SQLITE_OK ) return p->rc;
194555
194556 /* If the iterator is already at the end of the changeset, return DONE. */
194557 if( p->in.iNext>=p->in.nData ){
194558 return SQLITE_DONE;
194559 }
194560
194561 sessionDiscardData(&p->in);
194562 p->in.iCurrent = p->in.iNext;
194563
194564 op = p->in.aData[p->in.iNext++];
194565 while( op=='T' || op=='P' ){
194566 if( pbNew ) *pbNew = 1;
194567 p->bPatchset = (op=='P');
194568 if( sessionChangesetReadTblhdr(p) ) return p->rc;
194569 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
194570 p->in.iCurrent = p->in.iNext;
194571 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
194572 op = p->in.aData[p->in.iNext++];
194573 }
194574
194575 if( p->zTab==0 ){
194576 /* The first record in the changeset is not a table header. Must be a
194577 ** corrupt changeset. */
194578 assert( p->in.iNext==1 );
194579 return (p->rc = SQLITE_CORRUPT_BKPT);
194580 }
194581
194582 p->op = op;
194583 p->bIndirect = p->in.aData[p->in.iNext++];
194584 if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
194585 return (p->rc = SQLITE_CORRUPT_BKPT);
194586 }
194587
194588 if( paRec ){
194589 int nVal; /* Number of values to buffer */
194590 if( p->bPatchset==0 && op==SQLITE_UPDATE ){
194591 nVal = p->nCol * 2;
194592 }else if( p->bPatchset && op==SQLITE_DELETE ){
194593 nVal = 0;
194594 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
194595 }else{
194596 nVal = p->nCol;
194597 }
194598 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
194599 if( p->rc!=SQLITE_OK ) return p->rc;
194600 *paRec = &p->in.aData[p->in.iNext];
194601 p->in.iNext += *pnRec;
194602 }else{
194603
194604 /* If this is an UPDATE or DELETE, read the old.* record. */
194605 if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
194606 u8 *abPK = p->bPatchset ? p->abPK : 0;
194607 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, p->apValue);
194608 if( p->rc!=SQLITE_OK ) return p->rc;
194609 }
194610
194611 /* If this is an INSERT or UPDATE, read the new.* record. */
194612 if( p->op!=SQLITE_DELETE ){
194613 p->rc = sessionReadRecord(&p->in, p->nCol, 0, &p->apValue[p->nCol]);
194614 if( p->rc!=SQLITE_OK ) return p->rc;
194615 }
194616
194617 if( p->bPatchset && p->op==SQLITE_UPDATE ){
194618 /* If this is an UPDATE that is part of a patchset, then all PK and
194619 ** modified fields are present in the new.* record. The old.* record
194620 ** is currently completely empty. This block shifts the PK fields from
194621 ** new.* to old.*, to accommodate the code that reads these arrays. */
194622 for(i=0; i<p->nCol; i++){
194623 assert( p->apValue[i]==0 );
194624 if( p->abPK[i] ){
194625 p->apValue[i] = p->apValue[i+p->nCol];
194626 if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
194627 p->apValue[i+p->nCol] = 0;
194628 }
194629 }
194630 }
194631 }
194632
194633 return SQLITE_ROW;
194634}
194635
194636/*
194637** Advance an iterator created by sqlite3changeset_start() to the next
194638** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
194639** or SQLITE_CORRUPT.
194640**
194641** This function may not be called on iterators passed to a conflict handler
194642** callback by changeset_apply().
194643*/
194644SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
194645 return sessionChangesetNext(p, 0, 0, 0);
194646}
194647
194648/*
194649** The following function extracts information on the current change
194650** from a changeset iterator. It may only be called after changeset_next()
194651** has returned SQLITE_ROW.
194652*/
194653SQLITE_API int sqlite3changeset_op(
194654 sqlite3_changeset_iter *pIter, /* Iterator handle */
194655 const char **pzTab, /* OUT: Pointer to table name */
194656 int *pnCol, /* OUT: Number of columns in table */
194657 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
194658 int *pbIndirect /* OUT: True if change is indirect */
194659){
194660 *pOp = pIter->op;
194661 *pnCol = pIter->nCol;
194662 *pzTab = pIter->zTab;
194663 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
194664 return SQLITE_OK;
194665}
194666
194667/*
194668** Return information regarding the PRIMARY KEY and number of columns in
194669** the database table affected by the change that pIter currently points
194670** to. This function may only be called after changeset_next() returns
194671** SQLITE_ROW.
194672*/
194673SQLITE_API int sqlite3changeset_pk(
194674 sqlite3_changeset_iter *pIter, /* Iterator object */
194675 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
194676 int *pnCol /* OUT: Number of entries in output array */
194677){
194678 *pabPK = pIter->abPK;
194679 if( pnCol ) *pnCol = pIter->nCol;
194680 return SQLITE_OK;
194681}
194682
194683/*
194684** This function may only be called while the iterator is pointing to an
194685** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
194686** Otherwise, SQLITE_MISUSE is returned.
194687**
194688** It sets *ppValue to point to an sqlite3_value structure containing the
194689** iVal'th value in the old.* record. Or, if that particular value is not
194690** included in the record (because the change is an UPDATE and the field
194691** was not modified and is not a PK column), set *ppValue to NULL.
194692**
194693** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
194694** not modified. Otherwise, SQLITE_OK.
194695*/
194696SQLITE_API int sqlite3changeset_old(
194697 sqlite3_changeset_iter *pIter, /* Changeset iterator */
194698 int iVal, /* Index of old.* value to retrieve */
194699 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
194700){
194701 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
194702 return SQLITE_MISUSE;
194703 }
194704 if( iVal<0 || iVal>=pIter->nCol ){
194705 return SQLITE_RANGE;
194706 }
194707 *ppValue = pIter->apValue[iVal];
194708 return SQLITE_OK;
194709}
194710
194711/*
194712** This function may only be called while the iterator is pointing to an
194713** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
194714** Otherwise, SQLITE_MISUSE is returned.
194715**
194716** It sets *ppValue to point to an sqlite3_value structure containing the
194717** iVal'th value in the new.* record. Or, if that particular value is not
194718** included in the record (because the change is an UPDATE and the field
194719** was not modified), set *ppValue to NULL.
194720**
194721** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
194722** not modified. Otherwise, SQLITE_OK.
194723*/
194724SQLITE_API int sqlite3changeset_new(
194725 sqlite3_changeset_iter *pIter, /* Changeset iterator */
194726 int iVal, /* Index of new.* value to retrieve */
194727 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
194728){
194729 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
194730 return SQLITE_MISUSE;
194731 }
194732 if( iVal<0 || iVal>=pIter->nCol ){
194733 return SQLITE_RANGE;
194734 }
194735 *ppValue = pIter->apValue[pIter->nCol+iVal];
194736 return SQLITE_OK;
194737}
194738
194739/*
194740** The following two macros are used internally. They are similar to the
194741** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
194742** they omit all error checking and return a pointer to the requested value.
194743*/
194744#define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
194745#define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
194746
194747/*
194748** This function may only be called with a changeset iterator that has been
194749** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
194750** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
194751**
194752** If successful, *ppValue is set to point to an sqlite3_value structure
194753** containing the iVal'th value of the conflicting record.
194754**
194755** If value iVal is out-of-range or some other error occurs, an SQLite error
194756** code is returned. Otherwise, SQLITE_OK.
194757*/
194758SQLITE_API int sqlite3changeset_conflict(
194759 sqlite3_changeset_iter *pIter, /* Changeset iterator */
194760 int iVal, /* Index of conflict record value to fetch */
194761 sqlite3_value **ppValue /* OUT: Value from conflicting row */
194762){
194763 if( !pIter->pConflict ){
194764 return SQLITE_MISUSE;
194765 }
194766 if( iVal<0 || iVal>=pIter->nCol ){
194767 return SQLITE_RANGE;
194768 }
194769 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
194770 return SQLITE_OK;
194771}
194772
194773/*
194774** This function may only be called with an iterator passed to an
194775** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
194776** it sets the output variable to the total number of known foreign key
194777** violations in the destination database and returns SQLITE_OK.
194778**
194779** In all other cases this function returns SQLITE_MISUSE.
194780*/
194781SQLITE_API int sqlite3changeset_fk_conflicts(
194782 sqlite3_changeset_iter *pIter, /* Changeset iterator */
194783 int *pnOut /* OUT: Number of FK violations */
194784){
194785 if( pIter->pConflict || pIter->apValue ){
194786 return SQLITE_MISUSE;
194787 }
194788 *pnOut = pIter->nCol;
194789 return SQLITE_OK;
194790}
194791
194792
194793/*
194794** Finalize an iterator allocated with sqlite3changeset_start().
194795**
194796** This function may not be called on iterators passed to a conflict handler
194797** callback by changeset_apply().
194798*/
194799SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
194800 int rc = SQLITE_OK;
194801 if( p ){
194802 int i; /* Used to iterate through p->apValue[] */
194803 rc = p->rc;
194804 if( p->apValue ){
194805 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
194806 }
194807 sqlite3_free(p->tblhdr.aBuf);
194808 sqlite3_free(p->in.buf.aBuf);
194809 sqlite3_free(p);
194810 }
194811 return rc;
194812}
194813
194814static int sessionChangesetInvert(
194815 SessionInput *pInput, /* Input changeset */
194816 int (*xOutput)(void *pOut, const void *pData, int nData),
194817 void *pOut,
194818 int *pnInverted, /* OUT: Number of bytes in output changeset */
194819 void **ppInverted /* OUT: Inverse of pChangeset */
194820){
194821 int rc = SQLITE_OK; /* Return value */
194822 SessionBuffer sOut; /* Output buffer */
194823 int nCol = 0; /* Number of cols in current table */
194824 u8 *abPK = 0; /* PK array for current table */
194825 sqlite3_value **apVal = 0; /* Space for values for UPDATE inversion */
194826 SessionBuffer sPK = {0, 0, 0}; /* PK array for current table */
194827
194828 /* Initialize the output buffer */
194829 memset(&sOut, 0, sizeof(SessionBuffer));
194830
194831 /* Zero the output variables in case an error occurs. */
194832 if( ppInverted ){
194833 *ppInverted = 0;
194834 *pnInverted = 0;
194835 }
194836
194837 while( 1 ){
194838 u8 eType;
194839
194840 /* Test for EOF. */
194841 if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
194842 if( pInput->iNext>=pInput->nData ) break;
194843 eType = pInput->aData[pInput->iNext];
194844
194845 switch( eType ){
194846 case 'T': {
194847 /* A 'table' record consists of:
194848 **
194849 ** * A constant 'T' character,
194850 ** * Number of columns in said table (a varint),
194851 ** * An array of nCol bytes (sPK),
194852 ** * A nul-terminated table name.
194853 */
194854 int nByte;
194855 int nVar;
194856 pInput->iNext++;
194857 if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
194858 goto finished_invert;
194859 }
194860 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
194861 sPK.nBuf = 0;
194862 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
194863 sessionAppendByte(&sOut, eType, &rc);
194864 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
194865 if( rc ) goto finished_invert;
194866
194867 pInput->iNext += nByte;
194868 sqlite3_free(apVal);
194869 apVal = 0;
194870 abPK = sPK.aBuf;
194871 break;
194872 }
194873
194874 case SQLITE_INSERT:
194875 case SQLITE_DELETE: {
194876 int nByte;
194877 int bIndirect = pInput->aData[pInput->iNext+1];
194878 int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE);
194879 pInput->iNext += 2;
194880 assert( rc==SQLITE_OK );
194881 rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
194882 sessionAppendByte(&sOut, eType2, &rc);
194883 sessionAppendByte(&sOut, bIndirect, &rc);
194884 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
194885 pInput->iNext += nByte;
194886 if( rc ) goto finished_invert;
194887 break;
194888 }
194889
194890 case SQLITE_UPDATE: {
194891 int iCol;
194892
194893 if( 0==apVal ){
194894 apVal = (sqlite3_value **)sqlite3_malloc(sizeof(apVal[0])*nCol*2);
194895 if( 0==apVal ){
194896 rc = SQLITE_NOMEM;
194897 goto finished_invert;
194898 }
194899 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
194900 }
194901
194902 /* Write the header for the new UPDATE change. Same as the original. */
194903 sessionAppendByte(&sOut, eType, &rc);
194904 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
194905
194906 /* Read the old.* and new.* records for the update change. */
194907 pInput->iNext += 2;
194908 rc = sessionReadRecord(pInput, nCol, 0, &apVal[0]);
194909 if( rc==SQLITE_OK ){
194910 rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol]);
194911 }
194912
194913 /* Write the new old.* record. Consists of the PK columns from the
194914 ** original old.* record, and the other values from the original
194915 ** new.* record. */
194916 for(iCol=0; iCol<nCol; iCol++){
194917 sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
194918 sessionAppendValue(&sOut, pVal, &rc);
194919 }
194920
194921 /* Write the new new.* record. Consists of a copy of all values
194922 ** from the original old.* record, except for the PK columns, which
194923 ** are set to "undefined". */
194924 for(iCol=0; iCol<nCol; iCol++){
194925 sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
194926 sessionAppendValue(&sOut, pVal, &rc);
194927 }
194928
194929 for(iCol=0; iCol<nCol*2; iCol++){
194930 sqlite3ValueFree(apVal[iCol]);
194931 }
194932 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
194933 if( rc!=SQLITE_OK ){
194934 goto finished_invert;
194935 }
194936
194937 break;
194938 }
194939
194940 default:
194941 rc = SQLITE_CORRUPT_BKPT;
194942 goto finished_invert;
194943 }
194944
194945 assert( rc==SQLITE_OK );
194946 if( xOutput && sOut.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
194947 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
194948 sOut.nBuf = 0;
194949 if( rc!=SQLITE_OK ) goto finished_invert;
194950 }
194951 }
194952
194953 assert( rc==SQLITE_OK );
194954 if( pnInverted ){
194955 *pnInverted = sOut.nBuf;
194956 *ppInverted = sOut.aBuf;
194957 sOut.aBuf = 0;
194958 }else if( sOut.nBuf>0 ){
194959 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
194960 }
194961
194962 finished_invert:
194963 sqlite3_free(sOut.aBuf);
194964 sqlite3_free(apVal);
194965 sqlite3_free(sPK.aBuf);
194966 return rc;
194967}
194968
194969
194970/*
194971** Invert a changeset object.
194972*/
194973SQLITE_API int sqlite3changeset_invert(
194974 int nChangeset, /* Number of bytes in input */
194975 const void *pChangeset, /* Input changeset */
194976 int *pnInverted, /* OUT: Number of bytes in output changeset */
194977 void **ppInverted /* OUT: Inverse of pChangeset */
194978){
194979 SessionInput sInput;
194980
194981 /* Set up the input stream */
194982 memset(&sInput, 0, sizeof(SessionInput));
194983 sInput.nData = nChangeset;
194984 sInput.aData = (u8*)pChangeset;
194985
194986 return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
194987}
194988
194989/*
194990** Streaming version of sqlite3changeset_invert().
194991*/
194992SQLITE_API int sqlite3changeset_invert_strm(
194993 int (*xInput)(void *pIn, void *pData, int *pnData),
194994 void *pIn,
194995 int (*xOutput)(void *pOut, const void *pData, int nData),
194996 void *pOut
194997){
194998 SessionInput sInput;
194999 int rc;
195000
195001 /* Set up the input stream */
195002 memset(&sInput, 0, sizeof(SessionInput));
195003 sInput.xInput = xInput;
195004 sInput.pIn = pIn;
195005
195006 rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
195007 sqlite3_free(sInput.buf.aBuf);
195008 return rc;
195009}
195010
195011typedef struct SessionApplyCtx SessionApplyCtx;
195012struct SessionApplyCtx {
195013 sqlite3 *db;
195014 sqlite3_stmt *pDelete; /* DELETE statement */
195015 sqlite3_stmt *pUpdate; /* UPDATE statement */
195016 sqlite3_stmt *pInsert; /* INSERT statement */
195017 sqlite3_stmt *pSelect; /* SELECT statement */
195018 int nCol; /* Size of azCol[] and abPK[] arrays */
195019 const char **azCol; /* Array of column names */
195020 u8 *abPK; /* Boolean array - true if column is in PK */
195021 int bStat1; /* True if table is sqlite_stat1 */
195022 int bDeferConstraints; /* True to defer constraints */
195023 SessionBuffer constraints; /* Deferred constraints are stored here */
195024 SessionBuffer rebase; /* Rebase information (if any) here */
195025 int bRebaseStarted; /* If table header is already in rebase */
195026};
195027
195028/*
195029** Formulate a statement to DELETE a row from database db. Assuming a table
195030** structure like this:
195031**
195032** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
195033**
195034** The DELETE statement looks like this:
195035**
195036** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
195037**
195038** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
195039** matching b and d values, or 1 otherwise. The second case comes up if the
195040** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
195041**
195042** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
195043** pointing to the prepared version of the SQL statement.
195044*/
195045static int sessionDeleteRow(
195046 sqlite3 *db, /* Database handle */
195047 const char *zTab, /* Table name */
195048 SessionApplyCtx *p /* Session changeset-apply context */
195049){
195050 int i;
195051 const char *zSep = "";
195052 int rc = SQLITE_OK;
195053 SessionBuffer buf = {0, 0, 0};
195054 int nPk = 0;
195055
195056 sessionAppendStr(&buf, "DELETE FROM ", &rc);
195057 sessionAppendIdent(&buf, zTab, &rc);
195058 sessionAppendStr(&buf, " WHERE ", &rc);
195059
195060 for(i=0; i<p->nCol; i++){
195061 if( p->abPK[i] ){
195062 nPk++;
195063 sessionAppendStr(&buf, zSep, &rc);
195064 sessionAppendIdent(&buf, p->azCol[i], &rc);
195065 sessionAppendStr(&buf, " = ?", &rc);
195066 sessionAppendInteger(&buf, i+1, &rc);
195067 zSep = " AND ";
195068 }
195069 }
195070
195071 if( nPk<p->nCol ){
195072 sessionAppendStr(&buf, " AND (?", &rc);
195073 sessionAppendInteger(&buf, p->nCol+1, &rc);
195074 sessionAppendStr(&buf, " OR ", &rc);
195075
195076 zSep = "";
195077 for(i=0; i<p->nCol; i++){
195078 if( !p->abPK[i] ){
195079 sessionAppendStr(&buf, zSep, &rc);
195080 sessionAppendIdent(&buf, p->azCol[i], &rc);
195081 sessionAppendStr(&buf, " IS ?", &rc);
195082 sessionAppendInteger(&buf, i+1, &rc);
195083 zSep = "AND ";
195084 }
195085 }
195086 sessionAppendStr(&buf, ")", &rc);
195087 }
195088
195089 if( rc==SQLITE_OK ){
195090 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
195091 }
195092 sqlite3_free(buf.aBuf);
195093
195094 return rc;
195095}
195096
195097/*
195098** Formulate and prepare a statement to UPDATE a row from database db.
195099** Assuming a table structure like this:
195100**
195101** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
195102**
195103** The UPDATE statement looks like this:
195104**
195105** UPDATE x SET
195106** a = CASE WHEN ?2 THEN ?3 ELSE a END,
195107** b = CASE WHEN ?5 THEN ?6 ELSE b END,
195108** c = CASE WHEN ?8 THEN ?9 ELSE c END,
195109** d = CASE WHEN ?11 THEN ?12 ELSE d END
195110** WHERE a = ?1 AND c = ?7 AND (?13 OR
195111** (?5==0 OR b IS ?4) AND (?11==0 OR d IS ?10) AND
195112** )
195113**
195114** For each column in the table, there are three variables to bind:
195115**
195116** ?(i*3+1) The old.* value of the column, if any.
195117** ?(i*3+2) A boolean flag indicating that the value is being modified.
195118** ?(i*3+3) The new.* value of the column, if any.
195119**
195120** Also, a boolean flag that, if set to true, causes the statement to update
195121** a row even if the non-PK values do not match. This is required if the
195122** conflict-handler is invoked with CHANGESET_DATA and returns
195123** CHANGESET_REPLACE. This is variable "?(nCol*3+1)".
195124**
195125** If successful, SQLITE_OK is returned and SessionApplyCtx.pUpdate is left
195126** pointing to the prepared version of the SQL statement.
195127*/
195128static int sessionUpdateRow(
195129 sqlite3 *db, /* Database handle */
195130 const char *zTab, /* Table name */
195131 SessionApplyCtx *p /* Session changeset-apply context */
195132){
195133 int rc = SQLITE_OK;
195134 int i;
195135 const char *zSep = "";
195136 SessionBuffer buf = {0, 0, 0};
195137
195138 /* Append "UPDATE tbl SET " */
195139 sessionAppendStr(&buf, "UPDATE ", &rc);
195140 sessionAppendIdent(&buf, zTab, &rc);
195141 sessionAppendStr(&buf, " SET ", &rc);
195142
195143 /* Append the assignments */
195144 for(i=0; i<p->nCol; i++){
195145 sessionAppendStr(&buf, zSep, &rc);
195146 sessionAppendIdent(&buf, p->azCol[i], &rc);
195147 sessionAppendStr(&buf, " = CASE WHEN ?", &rc);
195148 sessionAppendInteger(&buf, i*3+2, &rc);
195149 sessionAppendStr(&buf, " THEN ?", &rc);
195150 sessionAppendInteger(&buf, i*3+3, &rc);
195151 sessionAppendStr(&buf, " ELSE ", &rc);
195152 sessionAppendIdent(&buf, p->azCol[i], &rc);
195153 sessionAppendStr(&buf, " END", &rc);
195154 zSep = ", ";
195155 }
195156
195157 /* Append the PK part of the WHERE clause */
195158 sessionAppendStr(&buf, " WHERE ", &rc);
195159 for(i=0; i<p->nCol; i++){
195160 if( p->abPK[i] ){
195161 sessionAppendIdent(&buf, p->azCol[i], &rc);
195162 sessionAppendStr(&buf, " = ?", &rc);
195163 sessionAppendInteger(&buf, i*3+1, &rc);
195164 sessionAppendStr(&buf, " AND ", &rc);
195165 }
195166 }
195167
195168 /* Append the non-PK part of the WHERE clause */
195169 sessionAppendStr(&buf, " (?", &rc);
195170 sessionAppendInteger(&buf, p->nCol*3+1, &rc);
195171 sessionAppendStr(&buf, " OR 1", &rc);
195172 for(i=0; i<p->nCol; i++){
195173 if( !p->abPK[i] ){
195174 sessionAppendStr(&buf, " AND (?", &rc);
195175 sessionAppendInteger(&buf, i*3+2, &rc);
195176 sessionAppendStr(&buf, "=0 OR ", &rc);
195177 sessionAppendIdent(&buf, p->azCol[i], &rc);
195178 sessionAppendStr(&buf, " IS ?", &rc);
195179 sessionAppendInteger(&buf, i*3+1, &rc);
195180 sessionAppendStr(&buf, ")", &rc);
195181 }
195182 }
195183 sessionAppendStr(&buf, ")", &rc);
195184
195185 if( rc==SQLITE_OK ){
195186 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0);
195187 }
195188 sqlite3_free(buf.aBuf);
195189
195190 return rc;
195191}
195192
195193
195194/*
195195** Formulate and prepare an SQL statement to query table zTab by primary
195196** key. Assuming the following table structure:
195197**
195198** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
195199**
195200** The SELECT statement looks like this:
195201**
195202** SELECT * FROM x WHERE a = ?1 AND c = ?3
195203**
195204** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
195205** pointing to the prepared version of the SQL statement.
195206*/
195207static int sessionSelectRow(
195208 sqlite3 *db, /* Database handle */
195209 const char *zTab, /* Table name */
195210 SessionApplyCtx *p /* Session changeset-apply context */
195211){
195212 return sessionSelectStmt(
195213 db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
195214}
195215
195216/*
195217** Formulate and prepare an INSERT statement to add a record to table zTab.
195218** For example:
195219**
195220** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
195221**
195222** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
195223** pointing to the prepared version of the SQL statement.
195224*/
195225static int sessionInsertRow(
195226 sqlite3 *db, /* Database handle */
195227 const char *zTab, /* Table name */
195228 SessionApplyCtx *p /* Session changeset-apply context */
195229){
195230 int rc = SQLITE_OK;
195231 int i;
195232 SessionBuffer buf = {0, 0, 0};
195233
195234 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
195235 sessionAppendIdent(&buf, zTab, &rc);
195236 sessionAppendStr(&buf, "(", &rc);
195237 for(i=0; i<p->nCol; i++){
195238 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
195239 sessionAppendIdent(&buf, p->azCol[i], &rc);
195240 }
195241
195242 sessionAppendStr(&buf, ") VALUES(?", &rc);
195243 for(i=1; i<p->nCol; i++){
195244 sessionAppendStr(&buf, ", ?", &rc);
195245 }
195246 sessionAppendStr(&buf, ")", &rc);
195247
195248 if( rc==SQLITE_OK ){
195249 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
195250 }
195251 sqlite3_free(buf.aBuf);
195252 return rc;
195253}
195254
195255static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
195256 return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
195257}
195258
195259/*
195260** Prepare statements for applying changes to the sqlite_stat1 table.
195261** These are similar to those created by sessionSelectRow(),
195262** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
195263** other tables.
195264*/
195265static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
195266 int rc = sessionSelectRow(db, "sqlite_stat1", p);
195267 if( rc==SQLITE_OK ){
195268 rc = sessionPrepare(db, &p->pInsert,
195269 "INSERT INTO main.sqlite_stat1 VALUES(?1, "
195270 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
195271 "?3)"
195272 );
195273 }
195274 if( rc==SQLITE_OK ){
195275 rc = sessionPrepare(db, &p->pUpdate,
195276 "UPDATE main.sqlite_stat1 SET "
195277 "tbl = CASE WHEN ?2 THEN ?3 ELSE tbl END, "
195278 "idx = CASE WHEN ?5 THEN ?6 ELSE idx END, "
195279 "stat = CASE WHEN ?8 THEN ?9 ELSE stat END "
195280 "WHERE tbl=?1 AND idx IS "
195281 "CASE WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL ELSE ?4 END "
195282 "AND (?10 OR ?8=0 OR stat IS ?7)"
195283 );
195284 }
195285 if( rc==SQLITE_OK ){
195286 rc = sessionPrepare(db, &p->pDelete,
195287 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
195288 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
195289 "AND (?4 OR stat IS ?3)"
195290 );
195291 }
195292 return rc;
195293}
195294
195295/*
195296** A wrapper around sqlite3_bind_value() that detects an extra problem.
195297** See comments in the body of this function for details.
195298*/
195299static int sessionBindValue(
195300 sqlite3_stmt *pStmt, /* Statement to bind value to */
195301 int i, /* Parameter number to bind to */
195302 sqlite3_value *pVal /* Value to bind */
195303){
195304 int eType = sqlite3_value_type(pVal);
195305 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
195306 ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
195307 ** the (pVal->z) variable remains as it was or the type of the value is
195308 ** set to SQLITE_NULL. */
195309 if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
195310 /* This condition occurs when an earlier OOM in a call to
195311 ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
195312 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
195313 return SQLITE_NOMEM;
195314 }
195315 return sqlite3_bind_value(pStmt, i, pVal);
195316}
195317
195318/*
195319** Iterator pIter must point to an SQLITE_INSERT entry. This function
195320** transfers new.* values from the current iterator entry to statement
195321** pStmt. The table being inserted into has nCol columns.
195322**
195323** New.* value $i from the iterator is bound to variable ($i+1) of
195324** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
195325** are transfered to the statement. Otherwise, if abPK is not NULL, it points
195326** to an array nCol elements in size. In this case only those values for
195327** which abPK[$i] is true are read from the iterator and bound to the
195328** statement.
195329**
195330** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
195331*/
195332static int sessionBindRow(
195333 sqlite3_changeset_iter *pIter, /* Iterator to read values from */
195334 int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
195335 int nCol, /* Number of columns */
195336 u8 *abPK, /* If not NULL, bind only if true */
195337 sqlite3_stmt *pStmt /* Bind values to this statement */
195338){
195339 int i;
195340 int rc = SQLITE_OK;
195341
195342 /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
195343 ** argument iterator points to a suitable entry. Make sure that xValue
195344 ** is one of these to guarantee that it is safe to ignore the return
195345 ** in the code below. */
195346 assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
195347
195348 for(i=0; rc==SQLITE_OK && i<nCol; i++){
195349 if( !abPK || abPK[i] ){
195350 sqlite3_value *pVal;
195351 (void)xValue(pIter, i, &pVal);
195352 if( pVal==0 ){
195353 /* The value in the changeset was "undefined". This indicates a
195354 ** corrupt changeset blob. */
195355 rc = SQLITE_CORRUPT_BKPT;
195356 }else{
195357 rc = sessionBindValue(pStmt, i+1, pVal);
195358 }
195359 }
195360 }
195361 return rc;
195362}
195363
195364/*
195365** SQL statement pSelect is as generated by the sessionSelectRow() function.
195366** This function binds the primary key values from the change that changeset
195367** iterator pIter points to to the SELECT and attempts to seek to the table
195368** entry. If a row is found, the SELECT statement left pointing at the row
195369** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
195370** has occured, the statement is reset and SQLITE_OK is returned. If an
195371** error occurs, the statement is reset and an SQLite error code is returned.
195372**
195373** If this function returns SQLITE_ROW, the caller must eventually reset()
195374** statement pSelect. If any other value is returned, the statement does
195375** not require a reset().
195376**
195377** If the iterator currently points to an INSERT record, bind values from the
195378** new.* record to the SELECT statement. Or, if it points to a DELETE or
195379** UPDATE, bind values from the old.* record.
195380*/
195381static int sessionSeekToRow(
195382 sqlite3 *db, /* Database handle */
195383 sqlite3_changeset_iter *pIter, /* Changeset iterator */
195384 u8 *abPK, /* Primary key flags array */
195385 sqlite3_stmt *pSelect /* SELECT statement from sessionSelectRow() */
195386){
195387 int rc; /* Return code */
195388 int nCol; /* Number of columns in table */
195389 int op; /* Changset operation (SQLITE_UPDATE etc.) */
195390 const char *zDummy; /* Unused */
195391
195392 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
195393 rc = sessionBindRow(pIter,
195394 op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
195395 nCol, abPK, pSelect
195396 );
195397
195398 if( rc==SQLITE_OK ){
195399 rc = sqlite3_step(pSelect);
195400 if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
195401 }
195402
195403 return rc;
195404}
195405
195406/*
195407** This function is called from within sqlite3changset_apply_v2() when
195408** a conflict is encountered and resolved using conflict resolution
195409** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
195410** It adds a conflict resolution record to the buffer in
195411** SessionApplyCtx.rebase, which will eventually be returned to the caller
195412** of apply_v2() as the "rebase" buffer.
195413**
195414** Return SQLITE_OK if successful, or an SQLite error code otherwise.
195415*/
195416static int sessionRebaseAdd(
195417 SessionApplyCtx *p, /* Apply context */
195418 int eType, /* Conflict resolution (OMIT or REPLACE) */
195419 sqlite3_changeset_iter *pIter /* Iterator pointing at current change */
195420){
195421 int rc = SQLITE_OK;
195422 int i;
195423 int eOp = pIter->op;
195424 if( p->bRebaseStarted==0 ){
195425 /* Append a table-header to the rebase buffer */
195426 const char *zTab = pIter->zTab;
195427 sessionAppendByte(&p->rebase, 'T', &rc);
195428 sessionAppendVarint(&p->rebase, p->nCol, &rc);
195429 sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
195430 sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
195431 p->bRebaseStarted = 1;
195432 }
195433
195434 assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
195435 assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
195436
195437 sessionAppendByte(&p->rebase,
195438 (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
195439 );
195440 sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
195441 for(i=0; i<p->nCol; i++){
195442 sqlite3_value *pVal = 0;
195443 if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
195444 sqlite3changeset_old(pIter, i, &pVal);
195445 }else{
195446 sqlite3changeset_new(pIter, i, &pVal);
195447 }
195448 sessionAppendValue(&p->rebase, pVal, &rc);
195449 }
195450
195451 return rc;
195452}
195453
195454/*
195455** Invoke the conflict handler for the change that the changeset iterator
195456** currently points to.
195457**
195458** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
195459** If argument pbReplace is NULL, then the type of conflict handler invoked
195460** depends solely on eType, as follows:
195461**
195462** eType value Value passed to xConflict
195463** -------------------------------------------------
195464** CHANGESET_DATA CHANGESET_NOTFOUND
195465** CHANGESET_CONFLICT CHANGESET_CONSTRAINT
195466**
195467** Or, if pbReplace is not NULL, then an attempt is made to find an existing
195468** record with the same primary key as the record about to be deleted, updated
195469** or inserted. If such a record can be found, it is available to the conflict
195470** handler as the "conflicting" record. In this case the type of conflict
195471** handler invoked is as follows:
195472**
195473** eType value PK Record found? Value passed to xConflict
195474** ----------------------------------------------------------------
195475** CHANGESET_DATA Yes CHANGESET_DATA
195476** CHANGESET_DATA No CHANGESET_NOTFOUND
195477** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT
195478** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT
195479**
195480** If pbReplace is not NULL, and a record with a matching PK is found, and
195481** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
195482** is set to non-zero before returning SQLITE_OK.
195483**
195484** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
195485** returned. Or, if the conflict handler returns an invalid value,
195486** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
195487** this function returns SQLITE_OK.
195488*/
195489static int sessionConflictHandler(
195490 int eType, /* Either CHANGESET_DATA or CONFLICT */
195491 SessionApplyCtx *p, /* changeset_apply() context */
195492 sqlite3_changeset_iter *pIter, /* Changeset iterator */
195493 int(*xConflict)(void *, int, sqlite3_changeset_iter*),
195494 void *pCtx, /* First argument for conflict handler */
195495 int *pbReplace /* OUT: Set to true if PK row is found */
195496){
195497 int res = 0; /* Value returned by conflict handler */
195498 int rc;
195499 int nCol;
195500 int op;
195501 const char *zDummy;
195502
195503 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
195504
195505 assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA );
195506 assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT );
195507 assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND );
195508
195509 /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
195510 if( pbReplace ){
195511 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
195512 }else{
195513 rc = SQLITE_OK;
195514 }
195515
195516 if( rc==SQLITE_ROW ){
195517 /* There exists another row with the new.* primary key. */
195518 pIter->pConflict = p->pSelect;
195519 res = xConflict(pCtx, eType, pIter);
195520 pIter->pConflict = 0;
195521 rc = sqlite3_reset(p->pSelect);
195522 }else if( rc==SQLITE_OK ){
195523 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
195524 /* Instead of invoking the conflict handler, append the change blob
195525 ** to the SessionApplyCtx.constraints buffer. */
195526 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
195527 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
195528 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
195529 return SQLITE_OK;
195530 }else{
195531 /* No other row with the new.* primary key. */
195532 res = xConflict(pCtx, eType+1, pIter);
195533 if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
195534 }
195535 }
195536
195537 if( rc==SQLITE_OK ){
195538 switch( res ){
195539 case SQLITE_CHANGESET_REPLACE:
195540 assert( pbReplace );
195541 *pbReplace = 1;
195542 break;
195543
195544 case SQLITE_CHANGESET_OMIT:
195545 break;
195546
195547 case SQLITE_CHANGESET_ABORT:
195548 rc = SQLITE_ABORT;
195549 break;
195550
195551 default:
195552 rc = SQLITE_MISUSE;
195553 break;
195554 }
195555 if( rc==SQLITE_OK ){
195556 rc = sessionRebaseAdd(p, res, pIter);
195557 }
195558 }
195559
195560 return rc;
195561}
195562
195563/*
195564** Attempt to apply the change that the iterator passed as the first argument
195565** currently points to to the database. If a conflict is encountered, invoke
195566** the conflict handler callback.
195567**
195568** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
195569** one is encountered, update or delete the row with the matching primary key
195570** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
195571** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
195572** to true before returning. In this case the caller will invoke this function
195573** again, this time with pbRetry set to NULL.
195574**
195575** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
195576** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
195577** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
195578** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
195579** before retrying. In this case the caller attempts to remove the conflicting
195580** row before invoking this function again, this time with pbReplace set
195581** to NULL.
195582**
195583** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
195584** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
195585** returned.
195586*/
195587static int sessionApplyOneOp(
195588 sqlite3_changeset_iter *pIter, /* Changeset iterator */
195589 SessionApplyCtx *p, /* changeset_apply() context */
195590 int(*xConflict)(void *, int, sqlite3_changeset_iter *),
195591 void *pCtx, /* First argument for the conflict handler */
195592 int *pbReplace, /* OUT: True to remove PK row and retry */
195593 int *pbRetry /* OUT: True to retry. */
195594){
195595 const char *zDummy;
195596 int op;
195597 int nCol;
195598 int rc = SQLITE_OK;
195599
195600 assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect );
195601 assert( p->azCol && p->abPK );
195602 assert( !pbReplace || *pbReplace==0 );
195603
195604 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
195605
195606 if( op==SQLITE_DELETE ){
195607
195608 /* Bind values to the DELETE statement. If conflict handling is required,
195609 ** bind values for all columns and set bound variable (nCol+1) to true.
195610 ** Or, if conflict handling is not required, bind just the PK column
195611 ** values and, if it exists, set (nCol+1) to false. Conflict handling
195612 ** is not required if:
195613 **
195614 ** * this is a patchset, or
195615 ** * (pbRetry==0), or
195616 ** * all columns of the table are PK columns (in this case there is
195617 ** no (nCol+1) variable to bind to).
195618 */
195619 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
195620 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
195621 if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
195622 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
195623 }
195624 if( rc!=SQLITE_OK ) return rc;
195625
195626 sqlite3_step(p->pDelete);
195627 rc = sqlite3_reset(p->pDelete);
195628 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
195629 rc = sessionConflictHandler(
195630 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
195631 );
195632 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
195633 rc = sessionConflictHandler(
195634 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
195635 );
195636 }
195637
195638 }else if( op==SQLITE_UPDATE ){
195639 int i;
195640
195641 /* Bind values to the UPDATE statement. */
195642 for(i=0; rc==SQLITE_OK && i<nCol; i++){
195643 sqlite3_value *pOld = sessionChangesetOld(pIter, i);
195644 sqlite3_value *pNew = sessionChangesetNew(pIter, i);
195645
195646 sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew);
195647 if( pOld ){
195648 rc = sessionBindValue(p->pUpdate, i*3+1, pOld);
195649 }
195650 if( rc==SQLITE_OK && pNew ){
195651 rc = sessionBindValue(p->pUpdate, i*3+3, pNew);
195652 }
195653 }
195654 if( rc==SQLITE_OK ){
195655 sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0 || pIter->bPatchset);
195656 }
195657 if( rc!=SQLITE_OK ) return rc;
195658
195659 /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
195660 ** the result will be SQLITE_OK with 0 rows modified. */
195661 sqlite3_step(p->pUpdate);
195662 rc = sqlite3_reset(p->pUpdate);
195663
195664 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
195665 /* A NOTFOUND or DATA error. Search the table to see if it contains
195666 ** a row with a matching primary key. If so, this is a DATA conflict.
195667 ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
195668
195669 rc = sessionConflictHandler(
195670 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
195671 );
195672
195673 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
195674 /* This is always a CONSTRAINT conflict. */
195675 rc = sessionConflictHandler(
195676 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
195677 );
195678 }
195679
195680 }else{
195681 assert( op==SQLITE_INSERT );
195682 if( p->bStat1 ){
195683 /* Check if there is a conflicting row. For sqlite_stat1, this needs
195684 ** to be done using a SELECT, as there is no PRIMARY KEY in the
195685 ** database schema to throw an exception if a duplicate is inserted. */
195686 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
195687 if( rc==SQLITE_ROW ){
195688 rc = SQLITE_CONSTRAINT;
195689 sqlite3_reset(p->pSelect);
195690 }
195691 }
195692
195693 if( rc==SQLITE_OK ){
195694 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
195695 if( rc!=SQLITE_OK ) return rc;
195696
195697 sqlite3_step(p->pInsert);
195698 rc = sqlite3_reset(p->pInsert);
195699 }
195700
195701 if( (rc&0xff)==SQLITE_CONSTRAINT ){
195702 rc = sessionConflictHandler(
195703 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
195704 );
195705 }
195706 }
195707
195708 return rc;
195709}
195710
195711/*
195712** Attempt to apply the change that the iterator passed as the first argument
195713** currently points to to the database. If a conflict is encountered, invoke
195714** the conflict handler callback.
195715**
195716** The difference between this function and sessionApplyOne() is that this
195717** function handles the case where the conflict-handler is invoked and
195718** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
195719** retried in some manner.
195720*/
195721static int sessionApplyOneWithRetry(
195722 sqlite3 *db, /* Apply change to "main" db of this handle */
195723 sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */
195724 SessionApplyCtx *pApply, /* Apply context */
195725 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
195726 void *pCtx /* First argument passed to xConflict */
195727){
195728 int bReplace = 0;
195729 int bRetry = 0;
195730 int rc;
195731
195732 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
195733 if( rc==SQLITE_OK ){
195734 /* If the bRetry flag is set, the change has not been applied due to an
195735 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
195736 ** a row with the correct PK is present in the db, but one or more other
195737 ** fields do not contain the expected values) and the conflict handler
195738 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
195739 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
195740 ** the SQLITE_CHANGESET_DATA problem. */
195741 if( bRetry ){
195742 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
195743 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
195744 }
195745
195746 /* If the bReplace flag is set, the change is an INSERT that has not
195747 ** been performed because the database already contains a row with the
195748 ** specified primary key and the conflict handler returned
195749 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
195750 ** before reattempting the INSERT. */
195751 else if( bReplace ){
195752 assert( pIter->op==SQLITE_INSERT );
195753 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
195754 if( rc==SQLITE_OK ){
195755 rc = sessionBindRow(pIter,
195756 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
195757 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
195758 }
195759 if( rc==SQLITE_OK ){
195760 sqlite3_step(pApply->pDelete);
195761 rc = sqlite3_reset(pApply->pDelete);
195762 }
195763 if( rc==SQLITE_OK ){
195764 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
195765 }
195766 if( rc==SQLITE_OK ){
195767 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
195768 }
195769 }
195770 }
195771
195772 return rc;
195773}
195774
195775/*
195776** Retry the changes accumulated in the pApply->constraints buffer.
195777*/
195778static int sessionRetryConstraints(
195779 sqlite3 *db,
195780 int bPatchset,
195781 const char *zTab,
195782 SessionApplyCtx *pApply,
195783 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
195784 void *pCtx /* First argument passed to xConflict */
195785){
195786 int rc = SQLITE_OK;
195787
195788 while( pApply->constraints.nBuf ){
195789 sqlite3_changeset_iter *pIter2 = 0;
195790 SessionBuffer cons = pApply->constraints;
195791 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
195792
195793 rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf);
195794 if( rc==SQLITE_OK ){
195795 int nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
195796 int rc2;
195797 pIter2->bPatchset = bPatchset;
195798 pIter2->zTab = (char*)zTab;
195799 pIter2->nCol = pApply->nCol;
195800 pIter2->abPK = pApply->abPK;
195801 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
195802 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
195803 if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
195804
195805 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){
195806 rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
195807 }
195808
195809 rc2 = sqlite3changeset_finalize(pIter2);
195810 if( rc==SQLITE_OK ) rc = rc2;
195811 }
195812 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
195813
195814 sqlite3_free(cons.aBuf);
195815 if( rc!=SQLITE_OK ) break;
195816 if( pApply->constraints.nBuf>=cons.nBuf ){
195817 /* No progress was made on the last round. */
195818 pApply->bDeferConstraints = 0;
195819 }
195820 }
195821
195822 return rc;
195823}
195824
195825/*
195826** Argument pIter is a changeset iterator that has been initialized, but
195827** not yet passed to sqlite3changeset_next(). This function applies the
195828** changeset to the main database attached to handle "db". The supplied
195829** conflict handler callback is invoked to resolve any conflicts encountered
195830** while applying the change.
195831*/
195832static int sessionChangesetApply(
195833 sqlite3 *db, /* Apply change to "main" db of this handle */
195834 sqlite3_changeset_iter *pIter, /* Changeset to apply */
195835 int(*xFilter)(
195836 void *pCtx, /* Copy of sixth arg to _apply() */
195837 const char *zTab /* Table name */
195838 ),
195839 int(*xConflict)(
195840 void *pCtx, /* Copy of fifth arg to _apply() */
195841 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
195842 sqlite3_changeset_iter *p /* Handle describing change and conflict */
195843 ),
195844 void *pCtx, /* First argument passed to xConflict */
195845 void **ppRebase, int *pnRebase, /* OUT: Rebase information */
195846 int flags /* SESSION_APPLY_XXX flags */
195847){
195848 int schemaMismatch = 0;
195849 int rc = SQLITE_OK; /* Return code */
195850 const char *zTab = 0; /* Name of current table */
195851 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
195852 SessionApplyCtx sApply; /* changeset_apply() context object */
195853 int bPatchset;
195854
195855 assert( xConflict!=0 );
195856
195857 pIter->in.bNoDiscard = 1;
195858 memset(&sApply, 0, sizeof(sApply));
195859 sqlite3_mutex_enter(sqlite3_db_mutex(db));
195860 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
195861 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
195862 }
195863 if( rc==SQLITE_OK ){
195864 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
195865 }
195866 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){
195867 int nCol;
195868 int op;
195869 const char *zNew;
195870
195871 sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
195872
195873 if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
195874 u8 *abPK;
195875
195876 rc = sessionRetryConstraints(
195877 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
195878 );
195879 if( rc!=SQLITE_OK ) break;
195880
195881 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
195882 sqlite3_finalize(sApply.pDelete);
195883 sqlite3_finalize(sApply.pUpdate);
195884 sqlite3_finalize(sApply.pInsert);
195885 sqlite3_finalize(sApply.pSelect);
195886 sApply.db = db;
195887 sApply.pDelete = 0;
195888 sApply.pUpdate = 0;
195889 sApply.pInsert = 0;
195890 sApply.pSelect = 0;
195891 sApply.nCol = 0;
195892 sApply.azCol = 0;
195893 sApply.abPK = 0;
195894 sApply.bStat1 = 0;
195895 sApply.bDeferConstraints = 1;
195896 sApply.bRebaseStarted = 0;
195897 memset(&sApply.constraints, 0, sizeof(SessionBuffer));
195898
195899 /* If an xFilter() callback was specified, invoke it now. If the
195900 ** xFilter callback returns zero, skip this table. If it returns
195901 ** non-zero, proceed. */
195902 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
195903 if( schemaMismatch ){
195904 zTab = sqlite3_mprintf("%s", zNew);
195905 if( zTab==0 ){
195906 rc = SQLITE_NOMEM;
195907 break;
195908 }
195909 nTab = (int)strlen(zTab);
195910 sApply.azCol = (const char **)zTab;
195911 }else{
195912 int nMinCol = 0;
195913 int i;
195914
195915 sqlite3changeset_pk(pIter, &abPK, 0);
195916 rc = sessionTableInfo(
195917 db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
195918 );
195919 if( rc!=SQLITE_OK ) break;
195920 for(i=0; i<sApply.nCol; i++){
195921 if( sApply.abPK[i] ) nMinCol = i+1;
195922 }
195923
195924 if( sApply.nCol==0 ){
195925 schemaMismatch = 1;
195926 sqlite3_log(SQLITE_SCHEMA,
195927 "sqlite3changeset_apply(): no such table: %s", zTab
195928 );
195929 }
195930 else if( sApply.nCol<nCol ){
195931 schemaMismatch = 1;
195932 sqlite3_log(SQLITE_SCHEMA,
195933 "sqlite3changeset_apply(): table %s has %d columns, "
195934 "expected %d or more",
195935 zTab, sApply.nCol, nCol
195936 );
195937 }
195938 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
195939 schemaMismatch = 1;
195940 sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
195941 "primary key mismatch for table %s", zTab
195942 );
195943 }
195944 else{
195945 sApply.nCol = nCol;
195946 if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
195947 if( (rc = sessionStat1Sql(db, &sApply) ) ){
195948 break;
195949 }
195950 sApply.bStat1 = 1;
195951 }else{
195952 if((rc = sessionSelectRow(db, zTab, &sApply))
195953 || (rc = sessionUpdateRow(db, zTab, &sApply))
195954 || (rc = sessionDeleteRow(db, zTab, &sApply))
195955 || (rc = sessionInsertRow(db, zTab, &sApply))
195956 ){
195957 break;
195958 }
195959 sApply.bStat1 = 0;
195960 }
195961 }
195962 nTab = sqlite3Strlen30(zTab);
195963 }
195964 }
195965
195966 /* If there is a schema mismatch on the current table, proceed to the
195967 ** next change. A log message has already been issued. */
195968 if( schemaMismatch ) continue;
195969
195970 rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
195971 }
195972
195973 bPatchset = pIter->bPatchset;
195974 if( rc==SQLITE_OK ){
195975 rc = sqlite3changeset_finalize(pIter);
195976 }else{
195977 sqlite3changeset_finalize(pIter);
195978 }
195979
195980 if( rc==SQLITE_OK ){
195981 rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
195982 }
195983
195984 if( rc==SQLITE_OK ){
195985 int nFk, notUsed;
195986 sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, &notUsed, 0);
195987 if( nFk!=0 ){
195988 int res = SQLITE_CHANGESET_ABORT;
195989 sqlite3_changeset_iter sIter;
195990 memset(&sIter, 0, sizeof(sIter));
195991 sIter.nCol = nFk;
195992 res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
195993 if( res!=SQLITE_CHANGESET_OMIT ){
195994 rc = SQLITE_CONSTRAINT;
195995 }
195996 }
195997 }
195998 sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
195999
196000 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
196001 if( rc==SQLITE_OK ){
196002 rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
196003 }else{
196004 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
196005 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
196006 }
196007 }
196008
196009 if( rc==SQLITE_OK && bPatchset==0 && ppRebase && pnRebase ){
196010 *ppRebase = (void*)sApply.rebase.aBuf;
196011 *pnRebase = sApply.rebase.nBuf;
196012 sApply.rebase.aBuf = 0;
196013 }
196014 sqlite3_finalize(sApply.pInsert);
196015 sqlite3_finalize(sApply.pDelete);
196016 sqlite3_finalize(sApply.pUpdate);
196017 sqlite3_finalize(sApply.pSelect);
196018 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
196019 sqlite3_free((char*)sApply.constraints.aBuf);
196020 sqlite3_free((char*)sApply.rebase.aBuf);
196021 sqlite3_mutex_leave(sqlite3_db_mutex(db));
196022 return rc;
196023}
196024
196025/*
196026** Apply the changeset passed via pChangeset/nChangeset to the main
196027** database attached to handle "db".
196028*/
196029SQLITE_API int sqlite3changeset_apply_v2(
196030 sqlite3 *db, /* Apply change to "main" db of this handle */
196031 int nChangeset, /* Size of changeset in bytes */
196032 void *pChangeset, /* Changeset blob */
196033 int(*xFilter)(
196034 void *pCtx, /* Copy of sixth arg to _apply() */
196035 const char *zTab /* Table name */
196036 ),
196037 int(*xConflict)(
196038 void *pCtx, /* Copy of sixth arg to _apply() */
196039 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
196040 sqlite3_changeset_iter *p /* Handle describing change and conflict */
196041 ),
196042 void *pCtx, /* First argument passed to xConflict */
196043 void **ppRebase, int *pnRebase,
196044 int flags
196045){
196046 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
196047 int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
196048 if( rc==SQLITE_OK ){
196049 rc = sessionChangesetApply(
196050 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
196051 );
196052 }
196053 return rc;
196054}
196055
196056/*
196057** Apply the changeset passed via pChangeset/nChangeset to the main database
196058** attached to handle "db". Invoke the supplied conflict handler callback
196059** to resolve any conflicts encountered while applying the change.
196060*/
196061SQLITE_API int sqlite3changeset_apply(
196062 sqlite3 *db, /* Apply change to "main" db of this handle */
196063 int nChangeset, /* Size of changeset in bytes */
196064 void *pChangeset, /* Changeset blob */
196065 int(*xFilter)(
196066 void *pCtx, /* Copy of sixth arg to _apply() */
196067 const char *zTab /* Table name */
196068 ),
196069 int(*xConflict)(
196070 void *pCtx, /* Copy of fifth arg to _apply() */
196071 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
196072 sqlite3_changeset_iter *p /* Handle describing change and conflict */
196073 ),
196074 void *pCtx /* First argument passed to xConflict */
196075){
196076 return sqlite3changeset_apply_v2(
196077 db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
196078 );
196079}
196080
196081/*
196082** Apply the changeset passed via xInput/pIn to the main database
196083** attached to handle "db". Invoke the supplied conflict handler callback
196084** to resolve any conflicts encountered while applying the change.
196085*/
196086SQLITE_API int sqlite3changeset_apply_v2_strm(
196087 sqlite3 *db, /* Apply change to "main" db of this handle */
196088 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
196089 void *pIn, /* First arg for xInput */
196090 int(*xFilter)(
196091 void *pCtx, /* Copy of sixth arg to _apply() */
196092 const char *zTab /* Table name */
196093 ),
196094 int(*xConflict)(
196095 void *pCtx, /* Copy of sixth arg to _apply() */
196096 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
196097 sqlite3_changeset_iter *p /* Handle describing change and conflict */
196098 ),
196099 void *pCtx, /* First argument passed to xConflict */
196100 void **ppRebase, int *pnRebase,
196101 int flags
196102){
196103 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
196104 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
196105 if( rc==SQLITE_OK ){
196106 rc = sessionChangesetApply(
196107 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
196108 );
196109 }
196110 return rc;
196111}
196112SQLITE_API int sqlite3changeset_apply_strm(
196113 sqlite3 *db, /* Apply change to "main" db of this handle */
196114 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
196115 void *pIn, /* First arg for xInput */
196116 int(*xFilter)(
196117 void *pCtx, /* Copy of sixth arg to _apply() */
196118 const char *zTab /* Table name */
196119 ),
196120 int(*xConflict)(
196121 void *pCtx, /* Copy of sixth arg to _apply() */
196122 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
196123 sqlite3_changeset_iter *p /* Handle describing change and conflict */
196124 ),
196125 void *pCtx /* First argument passed to xConflict */
196126){
196127 return sqlite3changeset_apply_v2_strm(
196128 db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
196129 );
196130}
196131
196132/*
196133** sqlite3_changegroup handle.
196134*/
196135struct sqlite3_changegroup {
196136 int rc; /* Error code */
196137 int bPatch; /* True to accumulate patchsets */
196138 SessionTable *pList; /* List of tables in current patch */
196139};
196140
196141/*
196142** This function is called to merge two changes to the same row together as
196143** part of an sqlite3changeset_concat() operation. A new change object is
196144** allocated and a pointer to it stored in *ppNew.
196145*/
196146static int sessionChangeMerge(
196147 SessionTable *pTab, /* Table structure */
196148 int bRebase, /* True for a rebase hash-table */
196149 int bPatchset, /* True for patchsets */
196150 SessionChange *pExist, /* Existing change */
196151 int op2, /* Second change operation */
196152 int bIndirect, /* True if second change is indirect */
196153 u8 *aRec, /* Second change record */
196154 int nRec, /* Number of bytes in aRec */
196155 SessionChange **ppNew /* OUT: Merged change */
196156){
196157 SessionChange *pNew = 0;
196158 int rc = SQLITE_OK;
196159
196160 if( !pExist ){
196161 pNew = (SessionChange *)sqlite3_malloc(sizeof(SessionChange) + nRec);
196162 if( !pNew ){
196163 return SQLITE_NOMEM;
196164 }
196165 memset(pNew, 0, sizeof(SessionChange));
196166 pNew->op = op2;
196167 pNew->bIndirect = bIndirect;
196168 pNew->aRecord = (u8*)&pNew[1];
196169 if( bIndirect==0 || bRebase==0 ){
196170 pNew->nRecord = nRec;
196171 memcpy(pNew->aRecord, aRec, nRec);
196172 }else{
196173 int i;
196174 u8 *pIn = aRec;
196175 u8 *pOut = pNew->aRecord;
196176 for(i=0; i<pTab->nCol; i++){
196177 int nIn = sessionSerialLen(pIn);
196178 if( *pIn==0 ){
196179 *pOut++ = 0;
196180 }else if( pTab->abPK[i]==0 ){
196181 *pOut++ = 0xFF;
196182 }else{
196183 memcpy(pOut, pIn, nIn);
196184 pOut += nIn;
196185 }
196186 pIn += nIn;
196187 }
196188 pNew->nRecord = pOut - pNew->aRecord;
196189 }
196190 }else if( bRebase ){
196191 if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
196192 *ppNew = pExist;
196193 }else{
196194 int nByte = nRec + pExist->nRecord + sizeof(SessionChange);
196195 pNew = (SessionChange*)sqlite3_malloc(nByte);
196196 if( pNew==0 ){
196197 rc = SQLITE_NOMEM;
196198 }else{
196199 int i;
196200 u8 *a1 = pExist->aRecord;
196201 u8 *a2 = aRec;
196202 u8 *pOut;
196203
196204 memset(pNew, 0, nByte);
196205 pNew->bIndirect = bIndirect || pExist->bIndirect;
196206 pNew->op = op2;
196207 pOut = pNew->aRecord = (u8*)&pNew[1];
196208
196209 for(i=0; i<pTab->nCol; i++){
196210 int n1 = sessionSerialLen(a1);
196211 int n2 = sessionSerialLen(a2);
196212 if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
196213 *pOut++ = 0xFF;
196214 }else if( *a2==0 ){
196215 memcpy(pOut, a1, n1);
196216 pOut += n1;
196217 }else{
196218 memcpy(pOut, a2, n2);
196219 pOut += n2;
196220 }
196221 a1 += n1;
196222 a2 += n2;
196223 }
196224 pNew->nRecord = pOut - pNew->aRecord;
196225 }
196226 sqlite3_free(pExist);
196227 }
196228 }else{
196229 int op1 = pExist->op;
196230
196231 /*
196232 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
196233 ** op1=INSERT, op2=UPDATE -> INSERT.
196234 ** op1=INSERT, op2=DELETE -> (none)
196235 **
196236 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
196237 ** op1=UPDATE, op2=UPDATE -> UPDATE.
196238 ** op1=UPDATE, op2=DELETE -> DELETE.
196239 **
196240 ** op1=DELETE, op2=INSERT -> UPDATE.
196241 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
196242 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
196243 */
196244 if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT)
196245 || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT)
196246 || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE)
196247 || (op1==SQLITE_DELETE && op2==SQLITE_DELETE)
196248 ){
196249 pNew = pExist;
196250 }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){
196251 sqlite3_free(pExist);
196252 assert( pNew==0 );
196253 }else{
196254 u8 *aExist = pExist->aRecord;
196255 int nByte;
196256 u8 *aCsr;
196257
196258 /* Allocate a new SessionChange object. Ensure that the aRecord[]
196259 ** buffer of the new object is large enough to hold any record that
196260 ** may be generated by combining the input records. */
196261 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
196262 pNew = (SessionChange *)sqlite3_malloc(nByte);
196263 if( !pNew ){
196264 sqlite3_free(pExist);
196265 return SQLITE_NOMEM;
196266 }
196267 memset(pNew, 0, sizeof(SessionChange));
196268 pNew->bIndirect = (bIndirect && pExist->bIndirect);
196269 aCsr = pNew->aRecord = (u8 *)&pNew[1];
196270
196271 if( op1==SQLITE_INSERT ){ /* INSERT + UPDATE */
196272 u8 *a1 = aRec;
196273 assert( op2==SQLITE_UPDATE );
196274 pNew->op = SQLITE_INSERT;
196275 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
196276 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
196277 }else if( op1==SQLITE_DELETE ){ /* DELETE + INSERT */
196278 assert( op2==SQLITE_INSERT );
196279 pNew->op = SQLITE_UPDATE;
196280 if( bPatchset ){
196281 memcpy(aCsr, aRec, nRec);
196282 aCsr += nRec;
196283 }else{
196284 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
196285 sqlite3_free(pNew);
196286 pNew = 0;
196287 }
196288 }
196289 }else if( op2==SQLITE_UPDATE ){ /* UPDATE + UPDATE */
196290 u8 *a1 = aExist;
196291 u8 *a2 = aRec;
196292 assert( op1==SQLITE_UPDATE );
196293 if( bPatchset==0 ){
196294 sessionSkipRecord(&a1, pTab->nCol);
196295 sessionSkipRecord(&a2, pTab->nCol);
196296 }
196297 pNew->op = SQLITE_UPDATE;
196298 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
196299 sqlite3_free(pNew);
196300 pNew = 0;
196301 }
196302 }else{ /* UPDATE + DELETE */
196303 assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE );
196304 pNew->op = SQLITE_DELETE;
196305 if( bPatchset ){
196306 memcpy(aCsr, aRec, nRec);
196307 aCsr += nRec;
196308 }else{
196309 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
196310 }
196311 }
196312
196313 if( pNew ){
196314 pNew->nRecord = (int)(aCsr - pNew->aRecord);
196315 }
196316 sqlite3_free(pExist);
196317 }
196318 }
196319
196320 *ppNew = pNew;
196321 return rc;
196322}
196323
196324/*
196325** Add all changes in the changeset traversed by the iterator passed as
196326** the first argument to the changegroup hash tables.
196327*/
196328static int sessionChangesetToHash(
196329 sqlite3_changeset_iter *pIter, /* Iterator to read from */
196330 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
196331 int bRebase /* True if hash table is for rebasing */
196332){
196333 u8 *aRec;
196334 int nRec;
196335 int rc = SQLITE_OK;
196336 SessionTable *pTab = 0;
196337
196338 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
196339 const char *zNew;
196340 int nCol;
196341 int op;
196342 int iHash;
196343 int bIndirect;
196344 SessionChange *pChange;
196345 SessionChange *pExist = 0;
196346 SessionChange **pp;
196347
196348 if( pGrp->pList==0 ){
196349 pGrp->bPatch = pIter->bPatchset;
196350 }else if( pIter->bPatchset!=pGrp->bPatch ){
196351 rc = SQLITE_ERROR;
196352 break;
196353 }
196354
196355 sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
196356 if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
196357 /* Search the list for a matching table */
196358 int nNew = (int)strlen(zNew);
196359 u8 *abPK;
196360
196361 sqlite3changeset_pk(pIter, &abPK, 0);
196362 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
196363 if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
196364 }
196365 if( !pTab ){
196366 SessionTable **ppTab;
196367
196368 pTab = sqlite3_malloc(sizeof(SessionTable) + nCol + nNew+1);
196369 if( !pTab ){
196370 rc = SQLITE_NOMEM;
196371 break;
196372 }
196373 memset(pTab, 0, sizeof(SessionTable));
196374 pTab->nCol = nCol;
196375 pTab->abPK = (u8*)&pTab[1];
196376 memcpy(pTab->abPK, abPK, nCol);
196377 pTab->zName = (char*)&pTab->abPK[nCol];
196378 memcpy(pTab->zName, zNew, nNew+1);
196379
196380 /* The new object must be linked on to the end of the list, not
196381 ** simply added to the start of it. This is to ensure that the
196382 ** tables within the output of sqlite3changegroup_output() are in
196383 ** the right order. */
196384 for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
196385 *ppTab = pTab;
196386 }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
196387 rc = SQLITE_SCHEMA;
196388 break;
196389 }
196390 }
196391
196392 if( sessionGrowHash(pIter->bPatchset, pTab) ){
196393 rc = SQLITE_NOMEM;
196394 break;
196395 }
196396 iHash = sessionChangeHash(
196397 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
196398 );
196399
196400 /* Search for existing entry. If found, remove it from the hash table.
196401 ** Code below may link it back in.
196402 */
196403 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
196404 int bPkOnly1 = 0;
196405 int bPkOnly2 = 0;
196406 if( pIter->bPatchset ){
196407 bPkOnly1 = (*pp)->op==SQLITE_DELETE;
196408 bPkOnly2 = op==SQLITE_DELETE;
196409 }
196410 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
196411 pExist = *pp;
196412 *pp = (*pp)->pNext;
196413 pTab->nEntry--;
196414 break;
196415 }
196416 }
196417
196418 rc = sessionChangeMerge(pTab, bRebase,
196419 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
196420 );
196421 if( rc ) break;
196422 if( pChange ){
196423 pChange->pNext = pTab->apChange[iHash];
196424 pTab->apChange[iHash] = pChange;
196425 pTab->nEntry++;
196426 }
196427 }
196428
196429 if( rc==SQLITE_OK ) rc = pIter->rc;
196430 return rc;
196431}
196432
196433/*
196434** Serialize a changeset (or patchset) based on all changesets (or patchsets)
196435** added to the changegroup object passed as the first argument.
196436**
196437** If xOutput is not NULL, then the changeset/patchset is returned to the
196438** user via one or more calls to xOutput, as with the other streaming
196439** interfaces.
196440**
196441** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
196442** buffer containing the output changeset before this function returns. In
196443** this case (*pnOut) is set to the size of the output buffer in bytes. It
196444** is the responsibility of the caller to free the output buffer using
196445** sqlite3_free() when it is no longer required.
196446**
196447** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
196448** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
196449** are both set to 0 before returning.
196450*/
196451static int sessionChangegroupOutput(
196452 sqlite3_changegroup *pGrp,
196453 int (*xOutput)(void *pOut, const void *pData, int nData),
196454 void *pOut,
196455 int *pnOut,
196456 void **ppOut
196457){
196458 int rc = SQLITE_OK;
196459 SessionBuffer buf = {0, 0, 0};
196460 SessionTable *pTab;
196461 assert( xOutput==0 || (ppOut==0 && pnOut==0) );
196462
196463 /* Create the serialized output changeset based on the contents of the
196464 ** hash tables attached to the SessionTable objects in list p->pList.
196465 */
196466 for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
196467 int i;
196468 if( pTab->nEntry==0 ) continue;
196469
196470 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
196471 for(i=0; i<pTab->nChange; i++){
196472 SessionChange *p;
196473 for(p=pTab->apChange[i]; p; p=p->pNext){
196474 sessionAppendByte(&buf, p->op, &rc);
196475 sessionAppendByte(&buf, p->bIndirect, &rc);
196476 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
196477 }
196478 }
196479
196480 if( rc==SQLITE_OK && xOutput && buf.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
196481 rc = xOutput(pOut, buf.aBuf, buf.nBuf);
196482 buf.nBuf = 0;
196483 }
196484 }
196485
196486 if( rc==SQLITE_OK ){
196487 if( xOutput ){
196488 if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
196489 }else{
196490 *ppOut = buf.aBuf;
196491 *pnOut = buf.nBuf;
196492 buf.aBuf = 0;
196493 }
196494 }
196495 sqlite3_free(buf.aBuf);
196496
196497 return rc;
196498}
196499
196500/*
196501** Allocate a new, empty, sqlite3_changegroup.
196502*/
196503SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp){
196504 int rc = SQLITE_OK; /* Return code */
196505 sqlite3_changegroup *p; /* New object */
196506 p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
196507 if( p==0 ){
196508 rc = SQLITE_NOMEM;
196509 }else{
196510 memset(p, 0, sizeof(sqlite3_changegroup));
196511 }
196512 *pp = p;
196513 return rc;
196514}
196515
196516/*
196517** Add the changeset currently stored in buffer pData, size nData bytes,
196518** to changeset-group p.
196519*/
196520SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
196521 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
196522 int rc; /* Return code */
196523
196524 rc = sqlite3changeset_start(&pIter, nData, pData);
196525 if( rc==SQLITE_OK ){
196526 rc = sessionChangesetToHash(pIter, pGrp, 0);
196527 }
196528 sqlite3changeset_finalize(pIter);
196529 return rc;
196530}
196531
196532/*
196533** Obtain a buffer containing a changeset representing the concatenation
196534** of all changesets added to the group so far.
196535*/
196536SQLITE_API int sqlite3changegroup_output(
196537 sqlite3_changegroup *pGrp,
196538 int *pnData,
196539 void **ppData
196540){
196541 return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
196542}
196543
196544/*
196545** Streaming versions of changegroup_add().
196546*/
196547SQLITE_API int sqlite3changegroup_add_strm(
196548 sqlite3_changegroup *pGrp,
196549 int (*xInput)(void *pIn, void *pData, int *pnData),
196550 void *pIn
196551){
196552 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
196553 int rc; /* Return code */
196554
196555 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
196556 if( rc==SQLITE_OK ){
196557 rc = sessionChangesetToHash(pIter, pGrp, 0);
196558 }
196559 sqlite3changeset_finalize(pIter);
196560 return rc;
196561}
196562
196563/*
196564** Streaming versions of changegroup_output().
196565*/
196566SQLITE_API int sqlite3changegroup_output_strm(
196567 sqlite3_changegroup *pGrp,
196568 int (*xOutput)(void *pOut, const void *pData, int nData),
196569 void *pOut
196570){
196571 return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
196572}
196573
196574/*
196575** Delete a changegroup object.
196576*/
196577SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
196578 if( pGrp ){
196579 sessionDeleteTable(pGrp->pList);
196580 sqlite3_free(pGrp);
196581 }
196582}
196583
196584/*
196585** Combine two changesets together.
196586*/
196587SQLITE_API int sqlite3changeset_concat(
196588 int nLeft, /* Number of bytes in lhs input */
196589 void *pLeft, /* Lhs input changeset */
196590 int nRight /* Number of bytes in rhs input */,
196591 void *pRight, /* Rhs input changeset */
196592 int *pnOut, /* OUT: Number of bytes in output changeset */
196593 void **ppOut /* OUT: changeset (left <concat> right) */
196594){
196595 sqlite3_changegroup *pGrp;
196596 int rc;
196597
196598 rc = sqlite3changegroup_new(&pGrp);
196599 if( rc==SQLITE_OK ){
196600 rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
196601 }
196602 if( rc==SQLITE_OK ){
196603 rc = sqlite3changegroup_add(pGrp, nRight, pRight);
196604 }
196605 if( rc==SQLITE_OK ){
196606 rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
196607 }
196608 sqlite3changegroup_delete(pGrp);
196609
196610 return rc;
196611}
196612
196613/*
196614** Streaming version of sqlite3changeset_concat().
196615*/
196616SQLITE_API int sqlite3changeset_concat_strm(
196617 int (*xInputA)(void *pIn, void *pData, int *pnData),
196618 void *pInA,
196619 int (*xInputB)(void *pIn, void *pData, int *pnData),
196620 void *pInB,
196621 int (*xOutput)(void *pOut, const void *pData, int nData),
196622 void *pOut
196623){
196624 sqlite3_changegroup *pGrp;
196625 int rc;
196626
196627 rc = sqlite3changegroup_new(&pGrp);
196628 if( rc==SQLITE_OK ){
196629 rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
196630 }
196631 if( rc==SQLITE_OK ){
196632 rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
196633 }
196634 if( rc==SQLITE_OK ){
196635 rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
196636 }
196637 sqlite3changegroup_delete(pGrp);
196638
196639 return rc;
196640}
196641
196642/*
196643** Changeset rebaser handle.
196644*/
196645struct sqlite3_rebaser {
196646 sqlite3_changegroup grp; /* Hash table */
196647};
196648
196649/*
196650** Buffers a1 and a2 must both contain a sessions module record nCol
196651** fields in size. This function appends an nCol sessions module
196652** record to buffer pBuf that is a copy of a1, except that for
196653** each field that is undefined in a1[], swap in the field from a2[].
196654*/
196655static void sessionAppendRecordMerge(
196656 SessionBuffer *pBuf, /* Buffer to append to */
196657 int nCol, /* Number of columns in each record */
196658 u8 *a1, int n1, /* Record 1 */
196659 u8 *a2, int n2, /* Record 2 */
196660 int *pRc /* IN/OUT: error code */
196661){
196662 sessionBufferGrow(pBuf, n1+n2, pRc);
196663 if( *pRc==SQLITE_OK ){
196664 int i;
196665 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
196666 for(i=0; i<nCol; i++){
196667 int nn1 = sessionSerialLen(a1);
196668 int nn2 = sessionSerialLen(a2);
196669 if( *a1==0 || *a1==0xFF ){
196670 memcpy(pOut, a2, nn2);
196671 pOut += nn2;
196672 }else{
196673 memcpy(pOut, a1, nn1);
196674 pOut += nn1;
196675 }
196676 a1 += nn1;
196677 a2 += nn2;
196678 }
196679
196680 pBuf->nBuf = pOut-pBuf->aBuf;
196681 assert( pBuf->nBuf<=pBuf->nAlloc );
196682 }
196683}
196684
196685/*
196686** This function is called when rebasing a local UPDATE change against one
196687** or more remote UPDATE changes. The aRec/nRec buffer contains the current
196688** old.* and new.* records for the change. The rebase buffer (a single
196689** record) is in aChange/nChange. The rebased change is appended to buffer
196690** pBuf.
196691**
196692** Rebasing the UPDATE involves:
196693**
196694** * Removing any changes to fields for which the corresponding field
196695** in the rebase buffer is set to "replaced" (type 0xFF). If this
196696** means the UPDATE change updates no fields, nothing is appended
196697** to the output buffer.
196698**
196699** * For each field modified by the local change for which the
196700** corresponding field in the rebase buffer is not "undefined" (0x00)
196701** or "replaced" (0xFF), the old.* value is replaced by the value
196702** in the rebase buffer.
196703*/
196704static void sessionAppendPartialUpdate(
196705 SessionBuffer *pBuf, /* Append record here */
196706 sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */
196707 u8 *aRec, int nRec, /* Local change */
196708 u8 *aChange, int nChange, /* Record to rebase against */
196709 int *pRc /* IN/OUT: Return Code */
196710){
196711 sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
196712 if( *pRc==SQLITE_OK ){
196713 int bData = 0;
196714 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
196715 int i;
196716 u8 *a1 = aRec;
196717 u8 *a2 = aChange;
196718
196719 *pOut++ = SQLITE_UPDATE;
196720 *pOut++ = pIter->bIndirect;
196721 for(i=0; i<pIter->nCol; i++){
196722 int n1 = sessionSerialLen(a1);
196723 int n2 = sessionSerialLen(a2);
196724 if( pIter->abPK[i] || a2[0]==0 ){
196725 if( !pIter->abPK[i] ) bData = 1;
196726 memcpy(pOut, a1, n1);
196727 pOut += n1;
196728 }else if( a2[0]!=0xFF ){
196729 bData = 1;
196730 memcpy(pOut, a2, n2);
196731 pOut += n2;
196732 }else{
196733 *pOut++ = '\0';
196734 }
196735 a1 += n1;
196736 a2 += n2;
196737 }
196738 if( bData ){
196739 a2 = aChange;
196740 for(i=0; i<pIter->nCol; i++){
196741 int n1 = sessionSerialLen(a1);
196742 int n2 = sessionSerialLen(a2);
196743 if( pIter->abPK[i] || a2[0]!=0xFF ){
196744 memcpy(pOut, a1, n1);
196745 pOut += n1;
196746 }else{
196747 *pOut++ = '\0';
196748 }
196749 a1 += n1;
196750 a2 += n2;
196751 }
196752 pBuf->nBuf = (pOut - pBuf->aBuf);
196753 }
196754 }
196755}
196756
196757/*
196758** pIter is configured to iterate through a changeset. This function rebases
196759** that changeset according to the current configuration of the rebaser
196760** object passed as the first argument. If no error occurs and argument xOutput
196761** is not NULL, then the changeset is returned to the caller by invoking
196762** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
196763** then (*ppOut) is set to point to a buffer containing the rebased changeset
196764** before this function returns. In this case (*pnOut) is set to the size of
196765** the buffer in bytes. It is the responsibility of the caller to eventually
196766** free the (*ppOut) buffer using sqlite3_free().
196767**
196768** If an error occurs, an SQLite error code is returned. If ppOut and
196769** pnOut are not NULL, then the two output parameters are set to 0 before
196770** returning.
196771*/
196772static int sessionRebase(
196773 sqlite3_rebaser *p, /* Rebaser hash table */
196774 sqlite3_changeset_iter *pIter, /* Input data */
196775 int (*xOutput)(void *pOut, const void *pData, int nData),
196776 void *pOut, /* Context for xOutput callback */
196777 int *pnOut, /* OUT: Number of bytes in output changeset */
196778 void **ppOut /* OUT: Inverse of pChangeset */
196779){
196780 int rc = SQLITE_OK;
196781 u8 *aRec = 0;
196782 int nRec = 0;
196783 int bNew = 0;
196784 SessionTable *pTab = 0;
196785 SessionBuffer sOut = {0,0,0};
196786
196787 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
196788 SessionChange *pChange = 0;
196789 int bDone = 0;
196790
196791 if( bNew ){
196792 const char *zTab = pIter->zTab;
196793 for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
196794 if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
196795 }
196796 bNew = 0;
196797
196798 /* A patchset may not be rebased */
196799 if( pIter->bPatchset ){
196800 rc = SQLITE_ERROR;
196801 }
196802
196803 /* Append a table header to the output for this new table */
196804 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
196805 sessionAppendVarint(&sOut, pIter->nCol, &rc);
196806 sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
196807 sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
196808 }
196809
196810 if( pTab && rc==SQLITE_OK ){
196811 int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
196812
196813 for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
196814 if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
196815 break;
196816 }
196817 }
196818 }
196819
196820 if( pChange ){
196821 assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
196822 switch( pIter->op ){
196823 case SQLITE_INSERT:
196824 if( pChange->op==SQLITE_INSERT ){
196825 bDone = 1;
196826 if( pChange->bIndirect==0 ){
196827 sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
196828 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
196829 sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
196830 sessionAppendBlob(&sOut, aRec, nRec, &rc);
196831 }
196832 }
196833 break;
196834
196835 case SQLITE_UPDATE:
196836 bDone = 1;
196837 if( pChange->op==SQLITE_DELETE ){
196838 if( pChange->bIndirect==0 ){
196839 u8 *pCsr = aRec;
196840 sessionSkipRecord(&pCsr, pIter->nCol);
196841 sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
196842 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
196843 sessionAppendRecordMerge(&sOut, pIter->nCol,
196844 pCsr, nRec-(pCsr-aRec),
196845 pChange->aRecord, pChange->nRecord, &rc
196846 );
196847 }
196848 }else{
196849 sessionAppendPartialUpdate(&sOut, pIter,
196850 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
196851 );
196852 }
196853 break;
196854
196855 default:
196856 assert( pIter->op==SQLITE_DELETE );
196857 bDone = 1;
196858 if( pChange->op==SQLITE_INSERT ){
196859 sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
196860 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
196861 sessionAppendRecordMerge(&sOut, pIter->nCol,
196862 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
196863 );
196864 }
196865 break;
196866 }
196867 }
196868
196869 if( bDone==0 ){
196870 sessionAppendByte(&sOut, pIter->op, &rc);
196871 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
196872 sessionAppendBlob(&sOut, aRec, nRec, &rc);
196873 }
196874 if( rc==SQLITE_OK && xOutput && sOut.nBuf>SESSIONS_STRM_CHUNK_SIZE ){
196875 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
196876 sOut.nBuf = 0;
196877 }
196878 if( rc ) break;
196879 }
196880
196881 if( rc!=SQLITE_OK ){
196882 sqlite3_free(sOut.aBuf);
196883 memset(&sOut, 0, sizeof(sOut));
196884 }
196885
196886 if( rc==SQLITE_OK ){
196887 if( xOutput ){
196888 if( sOut.nBuf>0 ){
196889 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
196890 }
196891 }else{
196892 *ppOut = (void*)sOut.aBuf;
196893 *pnOut = sOut.nBuf;
196894 sOut.aBuf = 0;
196895 }
196896 }
196897 sqlite3_free(sOut.aBuf);
196898 return rc;
196899}
196900
196901/*
196902** Create a new rebaser object.
196903*/
196904SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
196905 int rc = SQLITE_OK;
196906 sqlite3_rebaser *pNew;
196907
196908 pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
196909 if( pNew==0 ){
196910 rc = SQLITE_NOMEM;
196911 }else{
196912 memset(pNew, 0, sizeof(sqlite3_rebaser));
196913 }
196914 *ppNew = pNew;
196915 return rc;
196916}
196917
196918/*
196919** Call this one or more times to configure a rebaser.
196920*/
196921SQLITE_API int sqlite3rebaser_configure(
196922 sqlite3_rebaser *p,
196923 int nRebase, const void *pRebase
196924){
196925 sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */
196926 int rc; /* Return code */
196927 rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
196928 if( rc==SQLITE_OK ){
196929 rc = sessionChangesetToHash(pIter, &p->grp, 1);
196930 }
196931 sqlite3changeset_finalize(pIter);
196932 return rc;
196933}
196934
196935/*
196936** Rebase a changeset according to current rebaser configuration
196937*/
196938SQLITE_API int sqlite3rebaser_rebase(
196939 sqlite3_rebaser *p,
196940 int nIn, const void *pIn,
196941 int *pnOut, void **ppOut
196942){
196943 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
196944 int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
196945
196946 if( rc==SQLITE_OK ){
196947 rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
196948 sqlite3changeset_finalize(pIter);
196949 }
196950
196951 return rc;
196952}
196953
196954/*
196955** Rebase a changeset according to current rebaser configuration
196956*/
196957SQLITE_API int sqlite3rebaser_rebase_strm(
196958 sqlite3_rebaser *p,
196959 int (*xInput)(void *pIn, void *pData, int *pnData),
196960 void *pIn,
196961 int (*xOutput)(void *pOut, const void *pData, int nData),
196962 void *pOut
196963){
196964 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
196965 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
196966
196967 if( rc==SQLITE_OK ){
196968 rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
196969 sqlite3changeset_finalize(pIter);
196970 }
196971
196972 return rc;
196973}
196974
196975/*
196976** Destroy a rebaser object
196977*/
196978SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
196979 if( p ){
196980 sessionDeleteTable(p->grp.pList);
196981 sqlite3_free(p);
196982 }
196983}
196984
196985#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
196986
196987/************** End of sqlite3session.c **************************************/
196988/************** Begin file fts5.c ********************************************/
196989
196990
196991#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5)
196992
196993#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
196994# define NDEBUG 1
196995#endif
196996#if defined(NDEBUG) && defined(SQLITE_DEBUG)
196997# undef NDEBUG
196998#endif
196999
197000/*
197001** 2014 May 31
197002**
197003** The author disclaims copyright to this source code. In place of
197004** a legal notice, here is a blessing:
197005**
197006** May you do good and not evil.
197007** May you find forgiveness for yourself and forgive others.
197008** May you share freely, never taking more than you give.
197009**
197010******************************************************************************
197011**
197012** Interfaces to extend FTS5. Using the interfaces defined in this file,
197013** FTS5 may be extended with:
197014**
197015** * custom tokenizers, and
197016** * custom auxiliary functions.
197017*/
197018
197019
197020#ifndef _FTS5_H
197021#define _FTS5_H
197022
197023/* #include "sqlite3.h" */
197024
197025#if 0
197026extern "C" {
197027#endif
197028
197029/*************************************************************************
197030** CUSTOM AUXILIARY FUNCTIONS
197031**
197032** Virtual table implementations may overload SQL functions by implementing
197033** the sqlite3_module.xFindFunction() method.
197034*/
197035
197036typedef struct Fts5ExtensionApi Fts5ExtensionApi;
197037typedef struct Fts5Context Fts5Context;
197038typedef struct Fts5PhraseIter Fts5PhraseIter;
197039
197040typedef void (*fts5_extension_function)(
197041 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
197042 Fts5Context *pFts, /* First arg to pass to pApi functions */
197043 sqlite3_context *pCtx, /* Context for returning result/error */
197044 int nVal, /* Number of values in apVal[] array */
197045 sqlite3_value **apVal /* Array of trailing arguments */
197046);
197047
197048struct Fts5PhraseIter {
197049 const unsigned char *a;
197050 const unsigned char *b;
197051};
197052
197053/*
197054** EXTENSION API FUNCTIONS
197055**
197056** xUserData(pFts):
197057** Return a copy of the context pointer the extension function was
197058** registered with.
197059**
197060** xColumnTotalSize(pFts, iCol, pnToken):
197061** If parameter iCol is less than zero, set output variable *pnToken
197062** to the total number of tokens in the FTS5 table. Or, if iCol is
197063** non-negative but less than the number of columns in the table, return
197064** the total number of tokens in column iCol, considering all rows in
197065** the FTS5 table.
197066**
197067** If parameter iCol is greater than or equal to the number of columns
197068** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
197069** an OOM condition or IO error), an appropriate SQLite error code is
197070** returned.
197071**
197072** xColumnCount(pFts):
197073** Return the number of columns in the table.
197074**
197075** xColumnSize(pFts, iCol, pnToken):
197076** If parameter iCol is less than zero, set output variable *pnToken
197077** to the total number of tokens in the current row. Or, if iCol is
197078** non-negative but less than the number of columns in the table, set
197079** *pnToken to the number of tokens in column iCol of the current row.
197080**
197081** If parameter iCol is greater than or equal to the number of columns
197082** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
197083** an OOM condition or IO error), an appropriate SQLite error code is
197084** returned.
197085**
197086** This function may be quite inefficient if used with an FTS5 table
197087** created with the "columnsize=0" option.
197088**
197089** xColumnText:
197090** This function attempts to retrieve the text of column iCol of the
197091** current document. If successful, (*pz) is set to point to a buffer
197092** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
197093** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
197094** if an error occurs, an SQLite error code is returned and the final values
197095** of (*pz) and (*pn) are undefined.
197096**
197097** xPhraseCount:
197098** Returns the number of phrases in the current query expression.
197099**
197100** xPhraseSize:
197101** Returns the number of tokens in phrase iPhrase of the query. Phrases
197102** are numbered starting from zero.
197103**
197104** xInstCount:
197105** Set *pnInst to the total number of occurrences of all phrases within
197106** the query within the current row. Return SQLITE_OK if successful, or
197107** an error code (i.e. SQLITE_NOMEM) if an error occurs.
197108**
197109** This API can be quite slow if used with an FTS5 table created with the
197110** "detail=none" or "detail=column" option. If the FTS5 table is created
197111** with either "detail=none" or "detail=column" and "content=" option
197112** (i.e. if it is a contentless table), then this API always returns 0.
197113**
197114** xInst:
197115** Query for the details of phrase match iIdx within the current row.
197116** Phrase matches are numbered starting from zero, so the iIdx argument
197117** should be greater than or equal to zero and smaller than the value
197118** output by xInstCount().
197119**
197120** Usually, output parameter *piPhrase is set to the phrase number, *piCol
197121** to the column in which it occurs and *piOff the token offset of the
197122** first token of the phrase. The exception is if the table was created
197123** with the offsets=0 option specified. In this case *piOff is always
197124** set to -1.
197125**
197126** Returns SQLITE_OK if successful, or an error code (i.e. SQLITE_NOMEM)
197127** if an error occurs.
197128**
197129** This API can be quite slow if used with an FTS5 table created with the
197130** "detail=none" or "detail=column" option.
197131**
197132** xRowid:
197133** Returns the rowid of the current row.
197134**
197135** xTokenize:
197136** Tokenize text using the tokenizer belonging to the FTS5 table.
197137**
197138** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
197139** This API function is used to query the FTS table for phrase iPhrase
197140** of the current query. Specifically, a query equivalent to:
197141**
197142** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
197143**
197144** with $p set to a phrase equivalent to the phrase iPhrase of the
197145** current query is executed. Any column filter that applies to
197146** phrase iPhrase of the current query is included in $p. For each
197147** row visited, the callback function passed as the fourth argument
197148** is invoked. The context and API objects passed to the callback
197149** function may be used to access the properties of each matched row.
197150** Invoking Api.xUserData() returns a copy of the pointer passed as
197151** the third argument to pUserData.
197152**
197153** If the callback function returns any value other than SQLITE_OK, the
197154** query is abandoned and the xQueryPhrase function returns immediately.
197155** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
197156** Otherwise, the error code is propagated upwards.
197157**
197158** If the query runs to completion without incident, SQLITE_OK is returned.
197159** Or, if some error occurs before the query completes or is aborted by
197160** the callback, an SQLite error code is returned.
197161**
197162**
197163** xSetAuxdata(pFts5, pAux, xDelete)
197164**
197165** Save the pointer passed as the second argument as the extension functions
197166** "auxiliary data". The pointer may then be retrieved by the current or any
197167** future invocation of the same fts5 extension function made as part of
197168** of the same MATCH query using the xGetAuxdata() API.
197169**
197170** Each extension function is allocated a single auxiliary data slot for
197171** each FTS query (MATCH expression). If the extension function is invoked
197172** more than once for a single FTS query, then all invocations share a
197173** single auxiliary data context.
197174**
197175** If there is already an auxiliary data pointer when this function is
197176** invoked, then it is replaced by the new pointer. If an xDelete callback
197177** was specified along with the original pointer, it is invoked at this
197178** point.
197179**
197180** The xDelete callback, if one is specified, is also invoked on the
197181** auxiliary data pointer after the FTS5 query has finished.
197182**
197183** If an error (e.g. an OOM condition) occurs within this function, an
197184** the auxiliary data is set to NULL and an error code returned. If the
197185** xDelete parameter was not NULL, it is invoked on the auxiliary data
197186** pointer before returning.
197187**
197188**
197189** xGetAuxdata(pFts5, bClear)
197190**
197191** Returns the current auxiliary data pointer for the fts5 extension
197192** function. See the xSetAuxdata() method for details.
197193**
197194** If the bClear argument is non-zero, then the auxiliary data is cleared
197195** (set to NULL) before this function returns. In this case the xDelete,
197196** if any, is not invoked.
197197**
197198**
197199** xRowCount(pFts5, pnRow)
197200**
197201** This function is used to retrieve the total number of rows in the table.
197202** In other words, the same value that would be returned by:
197203**
197204** SELECT count(*) FROM ftstable;
197205**
197206** xPhraseFirst()
197207** This function is used, along with type Fts5PhraseIter and the xPhraseNext
197208** method, to iterate through all instances of a single query phrase within
197209** the current row. This is the same information as is accessible via the
197210** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
197211** to use, this API may be faster under some circumstances. To iterate
197212** through instances of phrase iPhrase, use the following code:
197213**
197214** Fts5PhraseIter iter;
197215** int iCol, iOff;
197216** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
197217** iCol>=0;
197218** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
197219** ){
197220** // An instance of phrase iPhrase at offset iOff of column iCol
197221** }
197222**
197223** The Fts5PhraseIter structure is defined above. Applications should not
197224** modify this structure directly - it should only be used as shown above
197225** with the xPhraseFirst() and xPhraseNext() API methods (and by
197226** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
197227**
197228** This API can be quite slow if used with an FTS5 table created with the
197229** "detail=none" or "detail=column" option. If the FTS5 table is created
197230** with either "detail=none" or "detail=column" and "content=" option
197231** (i.e. if it is a contentless table), then this API always iterates
197232** through an empty set (all calls to xPhraseFirst() set iCol to -1).
197233**
197234** xPhraseNext()
197235** See xPhraseFirst above.
197236**
197237** xPhraseFirstColumn()
197238** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
197239** and xPhraseNext() APIs described above. The difference is that instead
197240** of iterating through all instances of a phrase in the current row, these
197241** APIs are used to iterate through the set of columns in the current row
197242** that contain one or more instances of a specified phrase. For example:
197243**
197244** Fts5PhraseIter iter;
197245** int iCol;
197246** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
197247** iCol>=0;
197248** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
197249** ){
197250** // Column iCol contains at least one instance of phrase iPhrase
197251** }
197252**
197253** This API can be quite slow if used with an FTS5 table created with the
197254** "detail=none" option. If the FTS5 table is created with either
197255** "detail=none" "content=" option (i.e. if it is a contentless table),
197256** then this API always iterates through an empty set (all calls to
197257** xPhraseFirstColumn() set iCol to -1).
197258**
197259** The information accessed using this API and its companion
197260** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
197261** (or xInst/xInstCount). The chief advantage of this API is that it is
197262** significantly more efficient than those alternatives when used with
197263** "detail=column" tables.
197264**
197265** xPhraseNextColumn()
197266** See xPhraseFirstColumn above.
197267*/
197268struct Fts5ExtensionApi {
197269 int iVersion; /* Currently always set to 3 */
197270
197271 void *(*xUserData)(Fts5Context*);
197272
197273 int (*xColumnCount)(Fts5Context*);
197274 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
197275 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
197276
197277 int (*xTokenize)(Fts5Context*,
197278 const char *pText, int nText, /* Text to tokenize */
197279 void *pCtx, /* Context passed to xToken() */
197280 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
197281 );
197282
197283 int (*xPhraseCount)(Fts5Context*);
197284 int (*xPhraseSize)(Fts5Context*, int iPhrase);
197285
197286 int (*xInstCount)(Fts5Context*, int *pnInst);
197287 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
197288
197289 sqlite3_int64 (*xRowid)(Fts5Context*);
197290 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
197291 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
197292
197293 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
197294 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
197295 );
197296 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
197297 void *(*xGetAuxdata)(Fts5Context*, int bClear);
197298
197299 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
197300 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
197301
197302 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
197303 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
197304};
197305
197306/*
197307** CUSTOM AUXILIARY FUNCTIONS
197308*************************************************************************/
197309
197310/*************************************************************************
197311** CUSTOM TOKENIZERS
197312**
197313** Applications may also register custom tokenizer types. A tokenizer
197314** is registered by providing fts5 with a populated instance of the
197315** following structure. All structure methods must be defined, setting
197316** any member of the fts5_tokenizer struct to NULL leads to undefined
197317** behaviour. The structure methods are expected to function as follows:
197318**
197319** xCreate:
197320** This function is used to allocate and initialize a tokenizer instance.
197321** A tokenizer instance is required to actually tokenize text.
197322**
197323** The first argument passed to this function is a copy of the (void*)
197324** pointer provided by the application when the fts5_tokenizer object
197325** was registered with FTS5 (the third argument to xCreateTokenizer()).
197326** The second and third arguments are an array of nul-terminated strings
197327** containing the tokenizer arguments, if any, specified following the
197328** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
197329** to create the FTS5 table.
197330**
197331** The final argument is an output variable. If successful, (*ppOut)
197332** should be set to point to the new tokenizer handle and SQLITE_OK
197333** returned. If an error occurs, some value other than SQLITE_OK should
197334** be returned. In this case, fts5 assumes that the final value of *ppOut
197335** is undefined.
197336**
197337** xDelete:
197338** This function is invoked to delete a tokenizer handle previously
197339** allocated using xCreate(). Fts5 guarantees that this function will
197340** be invoked exactly once for each successful call to xCreate().
197341**
197342** xTokenize:
197343** This function is expected to tokenize the nText byte string indicated
197344** by argument pText. pText may or may not be nul-terminated. The first
197345** argument passed to this function is a pointer to an Fts5Tokenizer object
197346** returned by an earlier call to xCreate().
197347**
197348** The second argument indicates the reason that FTS5 is requesting
197349** tokenization of the supplied text. This is always one of the following
197350** four values:
197351**
197352** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
197353** or removed from the FTS table. The tokenizer is being invoked to
197354** determine the set of tokens to add to (or delete from) the
197355** FTS index.
197356**
197357** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
197358** against the FTS index. The tokenizer is being called to tokenize
197359** a bareword or quoted string specified as part of the query.
197360**
197361** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
197362** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
197363** followed by a "*" character, indicating that the last token
197364** returned by the tokenizer will be treated as a token prefix.
197365**
197366** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
197367** satisfy an fts5_api.xTokenize() request made by an auxiliary
197368** function. Or an fts5_api.xColumnSize() request made by the same
197369** on a columnsize=0 database.
197370** </ul>
197371**
197372** For each token in the input string, the supplied callback xToken() must
197373** be invoked. The first argument to it should be a copy of the pointer
197374** passed as the second argument to xTokenize(). The third and fourth
197375** arguments are a pointer to a buffer containing the token text, and the
197376** size of the token in bytes. The 4th and 5th arguments are the byte offsets
197377** of the first byte of and first byte immediately following the text from
197378** which the token is derived within the input.
197379**
197380** The second argument passed to the xToken() callback ("tflags") should
197381** normally be set to 0. The exception is if the tokenizer supports
197382** synonyms. In this case see the discussion below for details.
197383**
197384** FTS5 assumes the xToken() callback is invoked for each token in the
197385** order that they occur within the input text.
197386**
197387** If an xToken() callback returns any value other than SQLITE_OK, then
197388** the tokenization should be abandoned and the xTokenize() method should
197389** immediately return a copy of the xToken() return value. Or, if the
197390** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
197391** if an error occurs with the xTokenize() implementation itself, it
197392** may abandon the tokenization and return any error code other than
197393** SQLITE_OK or SQLITE_DONE.
197394**
197395** SYNONYM SUPPORT
197396**
197397** Custom tokenizers may also support synonyms. Consider a case in which a
197398** user wishes to query for a phrase such as "first place". Using the
197399** built-in tokenizers, the FTS5 query 'first + place' will match instances
197400** of "first place" within the document set, but not alternative forms
197401** such as "1st place". In some applications, it would be better to match
197402** all instances of "first place" or "1st place" regardless of which form
197403** the user specified in the MATCH query text.
197404**
197405** There are several ways to approach this in FTS5:
197406**
197407** <ol><li> By mapping all synonyms to a single token. In this case, the
197408** In the above example, this means that the tokenizer returns the
197409** same token for inputs "first" and "1st". Say that token is in
197410** fact "first", so that when the user inserts the document "I won
197411** 1st place" entries are added to the index for tokens "i", "won",
197412** "first" and "place". If the user then queries for '1st + place',
197413** the tokenizer substitutes "first" for "1st" and the query works
197414** as expected.
197415**
197416** <li> By adding multiple synonyms for a single term to the FTS index.
197417** In this case, when tokenizing query text, the tokenizer may
197418** provide multiple synonyms for a single term within the document.
197419** FTS5 then queries the index for each synonym individually. For
197420** example, faced with the query:
197421**
197422** <codeblock>
197423** ... MATCH 'first place'</codeblock>
197424**
197425** the tokenizer offers both "1st" and "first" as synonyms for the
197426** first token in the MATCH query and FTS5 effectively runs a query
197427** similar to:
197428**
197429** <codeblock>
197430** ... MATCH '(first OR 1st) place'</codeblock>
197431**
197432** except that, for the purposes of auxiliary functions, the query
197433** still appears to contain just two phrases - "(first OR 1st)"
197434** being treated as a single phrase.
197435**
197436** <li> By adding multiple synonyms for a single term to the FTS index.
197437** Using this method, when tokenizing document text, the tokenizer
197438** provides multiple synonyms for each token. So that when a
197439** document such as "I won first place" is tokenized, entries are
197440** added to the FTS index for "i", "won", "first", "1st" and
197441** "place".
197442**
197443** This way, even if the tokenizer does not provide synonyms
197444** when tokenizing query text (it should not - to do would be
197445** inefficient), it doesn't matter if the user queries for
197446** 'first + place' or '1st + place', as there are entries in the
197447** FTS index corresponding to both forms of the first token.
197448** </ol>
197449**
197450** Whether it is parsing document or query text, any call to xToken that
197451** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
197452** is considered to supply a synonym for the previous token. For example,
197453** when parsing the document "I won first place", a tokenizer that supports
197454** synonyms would call xToken() 5 times, as follows:
197455**
197456** <codeblock>
197457** xToken(pCtx, 0, "i", 1, 0, 1);
197458** xToken(pCtx, 0, "won", 3, 2, 5);
197459** xToken(pCtx, 0, "first", 5, 6, 11);
197460** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
197461** xToken(pCtx, 0, "place", 5, 12, 17);
197462**</codeblock>
197463**
197464** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
197465** xToken() is called. Multiple synonyms may be specified for a single token
197466** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
197467** There is no limit to the number of synonyms that may be provided for a
197468** single token.
197469**
197470** In many cases, method (1) above is the best approach. It does not add
197471** extra data to the FTS index or require FTS5 to query for multiple terms,
197472** so it is efficient in terms of disk space and query speed. However, it
197473** does not support prefix queries very well. If, as suggested above, the
197474** token "first" is substituted for "1st" by the tokenizer, then the query:
197475**
197476** <codeblock>
197477** ... MATCH '1s*'</codeblock>
197478**
197479** will not match documents that contain the token "1st" (as the tokenizer
197480** will probably not map "1s" to any prefix of "first").
197481**
197482** For full prefix support, method (3) may be preferred. In this case,
197483** because the index contains entries for both "first" and "1st", prefix
197484** queries such as 'fi*' or '1s*' will match correctly. However, because
197485** extra entries are added to the FTS index, this method uses more space
197486** within the database.
197487**
197488** Method (2) offers a midpoint between (1) and (3). Using this method,
197489** a query such as '1s*' will match documents that contain the literal
197490** token "1st", but not "first" (assuming the tokenizer is not able to
197491** provide synonyms for prefixes). However, a non-prefix query like '1st'
197492** will match against "1st" and "first". This method does not require
197493** extra disk space, as no extra entries are added to the FTS index.
197494** On the other hand, it may require more CPU cycles to run MATCH queries,
197495** as separate queries of the FTS index are required for each synonym.
197496**
197497** When using methods (2) or (3), it is important that the tokenizer only
197498** provide synonyms when tokenizing document text (method (2)) or query
197499** text (method (3)), not both. Doing so will not cause any errors, but is
197500** inefficient.
197501*/
197502typedef struct Fts5Tokenizer Fts5Tokenizer;
197503typedef struct fts5_tokenizer fts5_tokenizer;
197504struct fts5_tokenizer {
197505 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
197506 void (*xDelete)(Fts5Tokenizer*);
197507 int (*xTokenize)(Fts5Tokenizer*,
197508 void *pCtx,
197509 int flags, /* Mask of FTS5_TOKENIZE_* flags */
197510 const char *pText, int nText,
197511 int (*xToken)(
197512 void *pCtx, /* Copy of 2nd argument to xTokenize() */
197513 int tflags, /* Mask of FTS5_TOKEN_* flags */
197514 const char *pToken, /* Pointer to buffer containing token */
197515 int nToken, /* Size of token in bytes */
197516 int iStart, /* Byte offset of token within input text */
197517 int iEnd /* Byte offset of end of token within input text */
197518 )
197519 );
197520};
197521
197522/* Flags that may be passed as the third argument to xTokenize() */
197523#define FTS5_TOKENIZE_QUERY 0x0001
197524#define FTS5_TOKENIZE_PREFIX 0x0002
197525#define FTS5_TOKENIZE_DOCUMENT 0x0004
197526#define FTS5_TOKENIZE_AUX 0x0008
197527
197528/* Flags that may be passed by the tokenizer implementation back to FTS5
197529** as the third argument to the supplied xToken callback. */
197530#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */
197531
197532/*
197533** END OF CUSTOM TOKENIZERS
197534*************************************************************************/
197535
197536/*************************************************************************
197537** FTS5 EXTENSION REGISTRATION API
197538*/
197539typedef struct fts5_api fts5_api;
197540struct fts5_api {
197541 int iVersion; /* Currently always set to 2 */
197542
197543 /* Create a new tokenizer */
197544 int (*xCreateTokenizer)(
197545 fts5_api *pApi,
197546 const char *zName,
197547 void *pContext,
197548 fts5_tokenizer *pTokenizer,
197549 void (*xDestroy)(void*)
197550 );
197551
197552 /* Find an existing tokenizer */
197553 int (*xFindTokenizer)(
197554 fts5_api *pApi,
197555 const char *zName,
197556 void **ppContext,
197557 fts5_tokenizer *pTokenizer
197558 );
197559
197560 /* Create a new auxiliary function */
197561 int (*xCreateFunction)(
197562 fts5_api *pApi,
197563 const char *zName,
197564 void *pContext,
197565 fts5_extension_function xFunction,
197566 void (*xDestroy)(void*)
197567 );
197568};
197569
197570/*
197571** END OF REGISTRATION API
197572*************************************************************************/
197573
197574#if 0
197575} /* end of the 'extern "C"' block */
197576#endif
197577
197578#endif /* _FTS5_H */
197579
197580/*
197581** 2014 May 31
197582**
197583** The author disclaims copyright to this source code. In place of
197584** a legal notice, here is a blessing:
197585**
197586** May you do good and not evil.
197587** May you find forgiveness for yourself and forgive others.
197588** May you share freely, never taking more than you give.
197589**
197590******************************************************************************
197591**
197592*/
197593#ifndef _FTS5INT_H
197594#define _FTS5INT_H
197595
197596/* #include "fts5.h" */
197597/* #include "sqlite3ext.h" */
197598SQLITE_EXTENSION_INIT1
197599
197600/* #include <string.h> */
197601/* #include <assert.h> */
197602
197603#ifndef SQLITE_AMALGAMATION
197604
197605typedef unsigned char u8;
197606typedef unsigned int u32;
197607typedef unsigned short u16;
197608typedef short i16;
197609typedef sqlite3_int64 i64;
197610typedef sqlite3_uint64 u64;
197611
197612#ifndef ArraySize
197613# define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0])))
197614#endif
197615
197616#define testcase(x)
197617#define ALWAYS(x) 1
197618#define NEVER(x) 0
197619
197620#define MIN(x,y) (((x) < (y)) ? (x) : (y))
197621#define MAX(x,y) (((x) > (y)) ? (x) : (y))
197622
197623/*
197624** Constants for the largest and smallest possible 64-bit signed integers.
197625*/
197626# define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
197627# define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
197628
197629#endif
197630
197631/* Truncate very long tokens to this many bytes. Hard limit is
197632** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
197633** field that occurs at the start of each leaf page (see fts5_index.c). */
197634#define FTS5_MAX_TOKEN_SIZE 32768
197635
197636/*
197637** Maximum number of prefix indexes on single FTS5 table. This must be
197638** less than 32. If it is set to anything large than that, an #error
197639** directive in fts5_index.c will cause the build to fail.
197640*/
197641#define FTS5_MAX_PREFIX_INDEXES 31
197642
197643#define FTS5_DEFAULT_NEARDIST 10
197644#define FTS5_DEFAULT_RANK "bm25"
197645
197646/* Name of rank and rowid columns */
197647#define FTS5_RANK_NAME "rank"
197648#define FTS5_ROWID_NAME "rowid"
197649
197650#ifdef SQLITE_DEBUG
197651# define FTS5_CORRUPT sqlite3Fts5Corrupt()
197652static int sqlite3Fts5Corrupt(void);
197653#else
197654# define FTS5_CORRUPT SQLITE_CORRUPT_VTAB
197655#endif
197656
197657/*
197658** The assert_nc() macro is similar to the assert() macro, except that it
197659** is used for assert() conditions that are true only if it can be
197660** guranteed that the database is not corrupt.
197661*/
197662#ifdef SQLITE_DEBUG
197663SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
197664# define assert_nc(x) assert(sqlite3_fts5_may_be_corrupt || (x))
197665#else
197666# define assert_nc(x) assert(x)
197667#endif
197668
197669/* Mark a function parameter as unused, to suppress nuisance compiler
197670** warnings. */
197671#ifndef UNUSED_PARAM
197672# define UNUSED_PARAM(X) (void)(X)
197673#endif
197674
197675#ifndef UNUSED_PARAM2
197676# define UNUSED_PARAM2(X, Y) (void)(X), (void)(Y)
197677#endif
197678
197679typedef struct Fts5Global Fts5Global;
197680typedef struct Fts5Colset Fts5Colset;
197681
197682/* If a NEAR() clump or phrase may only match a specific set of columns,
197683** then an object of the following type is used to record the set of columns.
197684** Each entry in the aiCol[] array is a column that may be matched.
197685**
197686** This object is used by fts5_expr.c and fts5_index.c.
197687*/
197688struct Fts5Colset {
197689 int nCol;
197690 int aiCol[1];
197691};
197692
197693
197694
197695/**************************************************************************
197696** Interface to code in fts5_config.c. fts5_config.c contains contains code
197697** to parse the arguments passed to the CREATE VIRTUAL TABLE statement.
197698*/
197699
197700typedef struct Fts5Config Fts5Config;
197701
197702/*
197703** An instance of the following structure encodes all information that can
197704** be gleaned from the CREATE VIRTUAL TABLE statement.
197705**
197706** And all information loaded from the %_config table.
197707**
197708** nAutomerge:
197709** The minimum number of segments that an auto-merge operation should
197710** attempt to merge together. A value of 1 sets the object to use the
197711** compile time default. Zero disables auto-merge altogether.
197712**
197713** zContent:
197714**
197715** zContentRowid:
197716** The value of the content_rowid= option, if one was specified. Or
197717** the string "rowid" otherwise. This text is not quoted - if it is
197718** used as part of an SQL statement it needs to be quoted appropriately.
197719**
197720** zContentExprlist:
197721**
197722** pzErrmsg:
197723** This exists in order to allow the fts5_index.c module to return a
197724** decent error message if it encounters a file-format version it does
197725** not understand.
197726**
197727** bColumnsize:
197728** True if the %_docsize table is created.
197729**
197730** bPrefixIndex:
197731** This is only used for debugging. If set to false, any prefix indexes
197732** are ignored. This value is configured using:
197733**
197734** INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex);
197735**
197736*/
197737struct Fts5Config {
197738 sqlite3 *db; /* Database handle */
197739 char *zDb; /* Database holding FTS index (e.g. "main") */
197740 char *zName; /* Name of FTS index */
197741 int nCol; /* Number of columns */
197742 char **azCol; /* Column names */
197743 u8 *abUnindexed; /* True for unindexed columns */
197744 int nPrefix; /* Number of prefix indexes */
197745 int *aPrefix; /* Sizes in bytes of nPrefix prefix indexes */
197746 int eContent; /* An FTS5_CONTENT value */
197747 char *zContent; /* content table */
197748 char *zContentRowid; /* "content_rowid=" option value */
197749 int bColumnsize; /* "columnsize=" option value (dflt==1) */
197750 int eDetail; /* FTS5_DETAIL_XXX value */
197751 char *zContentExprlist;
197752 Fts5Tokenizer *pTok;
197753 fts5_tokenizer *pTokApi;
197754
197755 /* Values loaded from the %_config table */
197756 int iCookie; /* Incremented when %_config is modified */
197757 int pgsz; /* Approximate page size used in %_data */
197758 int nAutomerge; /* 'automerge' setting */
197759 int nCrisisMerge; /* Maximum allowed segments per level */
197760 int nUsermerge; /* 'usermerge' setting */
197761 int nHashSize; /* Bytes of memory for in-memory hash */
197762 char *zRank; /* Name of rank function */
197763 char *zRankArgs; /* Arguments to rank function */
197764
197765 /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
197766 char **pzErrmsg;
197767
197768#ifdef SQLITE_DEBUG
197769 int bPrefixIndex; /* True to use prefix-indexes */
197770#endif
197771};
197772
197773/* Current expected value of %_config table 'version' field */
197774#define FTS5_CURRENT_VERSION 4
197775
197776#define FTS5_CONTENT_NORMAL 0
197777#define FTS5_CONTENT_NONE 1
197778#define FTS5_CONTENT_EXTERNAL 2
197779
197780#define FTS5_DETAIL_FULL 0
197781#define FTS5_DETAIL_NONE 1
197782#define FTS5_DETAIL_COLUMNS 2
197783
197784
197785
197786static int sqlite3Fts5ConfigParse(
197787 Fts5Global*, sqlite3*, int, const char **, Fts5Config**, char**
197788);
197789static void sqlite3Fts5ConfigFree(Fts5Config*);
197790
197791static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig);
197792
197793static int sqlite3Fts5Tokenize(
197794 Fts5Config *pConfig, /* FTS5 Configuration object */
197795 int flags, /* FTS5_TOKENIZE_* flags */
197796 const char *pText, int nText, /* Text to tokenize */
197797 void *pCtx, /* Context passed to xToken() */
197798 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
197799);
197800
197801static void sqlite3Fts5Dequote(char *z);
197802
197803/* Load the contents of the %_config table */
197804static int sqlite3Fts5ConfigLoad(Fts5Config*, int);
197805
197806/* Set the value of a single config attribute */
197807static int sqlite3Fts5ConfigSetValue(Fts5Config*, const char*, sqlite3_value*, int*);
197808
197809static int sqlite3Fts5ConfigParseRank(const char*, char**, char**);
197810
197811/*
197812** End of interface to code in fts5_config.c.
197813**************************************************************************/
197814
197815/**************************************************************************
197816** Interface to code in fts5_buffer.c.
197817*/
197818
197819/*
197820** Buffer object for the incremental building of string data.
197821*/
197822typedef struct Fts5Buffer Fts5Buffer;
197823struct Fts5Buffer {
197824 u8 *p;
197825 int n;
197826 int nSpace;
197827};
197828
197829static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
197830static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
197831static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
197832static void sqlite3Fts5BufferAppendString(int *, Fts5Buffer*, const char*);
197833static void sqlite3Fts5BufferFree(Fts5Buffer*);
197834static void sqlite3Fts5BufferZero(Fts5Buffer*);
197835static void sqlite3Fts5BufferSet(int*, Fts5Buffer*, int, const u8*);
197836static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
197837
197838static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
197839
197840#define fts5BufferZero(x) sqlite3Fts5BufferZero(x)
197841#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,c)
197842#define fts5BufferFree(a) sqlite3Fts5BufferFree(a)
197843#define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
197844#define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
197845
197846#define fts5BufferGrow(pRc,pBuf,nn) ( \
197847 (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
197848 sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
197849)
197850
197851/* Write and decode big-endian 32-bit integer values */
197852static void sqlite3Fts5Put32(u8*, int);
197853static int sqlite3Fts5Get32(const u8*);
197854
197855#define FTS5_POS2COLUMN(iPos) (int)(iPos >> 32)
197856#define FTS5_POS2OFFSET(iPos) (int)(iPos & 0xFFFFFFFF)
197857
197858typedef struct Fts5PoslistReader Fts5PoslistReader;
197859struct Fts5PoslistReader {
197860 /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */
197861 const u8 *a; /* Position list to iterate through */
197862 int n; /* Size of buffer at a[] in bytes */
197863 int i; /* Current offset in a[] */
197864
197865 u8 bFlag; /* For client use (any custom purpose) */
197866
197867 /* Output variables */
197868 u8 bEof; /* Set to true at EOF */
197869 i64 iPos; /* (iCol<<32) + iPos */
197870};
197871static int sqlite3Fts5PoslistReaderInit(
197872 const u8 *a, int n, /* Poslist buffer to iterate through */
197873 Fts5PoslistReader *pIter /* Iterator object to initialize */
197874);
197875static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader*);
197876
197877typedef struct Fts5PoslistWriter Fts5PoslistWriter;
197878struct Fts5PoslistWriter {
197879 i64 iPrev;
197880};
197881static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
197882static void sqlite3Fts5PoslistSafeAppend(Fts5Buffer*, i64*, i64);
197883
197884static int sqlite3Fts5PoslistNext64(
197885 const u8 *a, int n, /* Buffer containing poslist */
197886 int *pi, /* IN/OUT: Offset within a[] */
197887 i64 *piOff /* IN/OUT: Current offset */
197888);
197889
197890/* Malloc utility */
197891static void *sqlite3Fts5MallocZero(int *pRc, int nByte);
197892static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn);
197893
197894/* Character set tests (like isspace(), isalpha() etc.) */
197895static int sqlite3Fts5IsBareword(char t);
197896
197897
197898/* Bucket of terms object used by the integrity-check in offsets=0 mode. */
197899typedef struct Fts5Termset Fts5Termset;
197900static int sqlite3Fts5TermsetNew(Fts5Termset**);
197901static int sqlite3Fts5TermsetAdd(Fts5Termset*, int, const char*, int, int *pbPresent);
197902static void sqlite3Fts5TermsetFree(Fts5Termset*);
197903
197904/*
197905** End of interface to code in fts5_buffer.c.
197906**************************************************************************/
197907
197908/**************************************************************************
197909** Interface to code in fts5_index.c. fts5_index.c contains contains code
197910** to access the data stored in the %_data table.
197911*/
197912
197913typedef struct Fts5Index Fts5Index;
197914typedef struct Fts5IndexIter Fts5IndexIter;
197915
197916struct Fts5IndexIter {
197917 i64 iRowid;
197918 const u8 *pData;
197919 int nData;
197920 u8 bEof;
197921};
197922
197923#define sqlite3Fts5IterEof(x) ((x)->bEof)
197924
197925/*
197926** Values used as part of the flags argument passed to IndexQuery().
197927*/
197928#define FTS5INDEX_QUERY_PREFIX 0x0001 /* Prefix query */
197929#define FTS5INDEX_QUERY_DESC 0x0002 /* Docs in descending rowid order */
197930#define FTS5INDEX_QUERY_TEST_NOIDX 0x0004 /* Do not use prefix index */
197931#define FTS5INDEX_QUERY_SCAN 0x0008 /* Scan query (fts5vocab) */
197932
197933/* The following are used internally by the fts5_index.c module. They are
197934** defined here only to make it easier to avoid clashes with the flags
197935** above. */
197936#define FTS5INDEX_QUERY_SKIPEMPTY 0x0010
197937#define FTS5INDEX_QUERY_NOOUTPUT 0x0020
197938
197939/*
197940** Create/destroy an Fts5Index object.
197941*/
197942static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
197943static int sqlite3Fts5IndexClose(Fts5Index *p);
197944
197945/*
197946** Return a simple checksum value based on the arguments.
197947*/
197948static u64 sqlite3Fts5IndexEntryCksum(
197949 i64 iRowid,
197950 int iCol,
197951 int iPos,
197952 int iIdx,
197953 const char *pTerm,
197954 int nTerm
197955);
197956
197957/*
197958** Argument p points to a buffer containing utf-8 text that is n bytes in
197959** size. Return the number of bytes in the nChar character prefix of the
197960** buffer, or 0 if there are less than nChar characters in total.
197961*/
197962static int sqlite3Fts5IndexCharlenToBytelen(
197963 const char *p,
197964 int nByte,
197965 int nChar
197966);
197967
197968/*
197969** Open a new iterator to iterate though all rowids that match the
197970** specified token or token prefix.
197971*/
197972static int sqlite3Fts5IndexQuery(
197973 Fts5Index *p, /* FTS index to query */
197974 const char *pToken, int nToken, /* Token (or prefix) to query for */
197975 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
197976 Fts5Colset *pColset, /* Match these columns only */
197977 Fts5IndexIter **ppIter /* OUT: New iterator object */
197978);
197979
197980/*
197981** The various operations on open token or token prefix iterators opened
197982** using sqlite3Fts5IndexQuery().
197983*/
197984static int sqlite3Fts5IterNext(Fts5IndexIter*);
197985static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
197986
197987/*
197988** Close an iterator opened by sqlite3Fts5IndexQuery().
197989*/
197990static void sqlite3Fts5IterClose(Fts5IndexIter*);
197991
197992/*
197993** This interface is used by the fts5vocab module.
197994*/
197995static const char *sqlite3Fts5IterTerm(Fts5IndexIter*, int*);
197996static int sqlite3Fts5IterNextScan(Fts5IndexIter*);
197997
197998
197999/*
198000** Insert or remove data to or from the index. Each time a document is
198001** added to or removed from the index, this function is called one or more
198002** times.
198003**
198004** For an insert, it must be called once for each token in the new document.
198005** If the operation is a delete, it must be called (at least) once for each
198006** unique token in the document with an iCol value less than zero. The iPos
198007** argument is ignored for a delete.
198008*/
198009static int sqlite3Fts5IndexWrite(
198010 Fts5Index *p, /* Index to write to */
198011 int iCol, /* Column token appears in (-ve -> delete) */
198012 int iPos, /* Position of token within column */
198013 const char *pToken, int nToken /* Token to add or remove to or from index */
198014);
198015
198016/*
198017** Indicate that subsequent calls to sqlite3Fts5IndexWrite() pertain to
198018** document iDocid.
198019*/
198020static int sqlite3Fts5IndexBeginWrite(
198021 Fts5Index *p, /* Index to write to */
198022 int bDelete, /* True if current operation is a delete */
198023 i64 iDocid /* Docid to add or remove data from */
198024);
198025
198026/*
198027** Flush any data stored in the in-memory hash tables to the database.
198028** Also close any open blob handles.
198029*/
198030static int sqlite3Fts5IndexSync(Fts5Index *p);
198031
198032/*
198033** Discard any data stored in the in-memory hash tables. Do not write it
198034** to the database. Additionally, assume that the contents of the %_data
198035** table may have changed on disk. So any in-memory caches of %_data
198036** records must be invalidated.
198037*/
198038static int sqlite3Fts5IndexRollback(Fts5Index *p);
198039
198040/*
198041** Get or set the "averages" values.
198042*/
198043static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize);
198044static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8*, int);
198045
198046/*
198047** Functions called by the storage module as part of integrity-check.
198048*/
198049static int sqlite3Fts5IndexIntegrityCheck(Fts5Index*, u64 cksum);
198050
198051/*
198052** Called during virtual module initialization to register UDF
198053** fts5_decode() with SQLite
198054*/
198055static int sqlite3Fts5IndexInit(sqlite3*);
198056
198057static int sqlite3Fts5IndexSetCookie(Fts5Index*, int);
198058
198059/*
198060** Return the total number of entries read from the %_data table by
198061** this connection since it was created.
198062*/
198063static int sqlite3Fts5IndexReads(Fts5Index *p);
198064
198065static int sqlite3Fts5IndexReinit(Fts5Index *p);
198066static int sqlite3Fts5IndexOptimize(Fts5Index *p);
198067static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
198068static int sqlite3Fts5IndexReset(Fts5Index *p);
198069
198070static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
198071
198072/*
198073** End of interface to code in fts5_index.c.
198074**************************************************************************/
198075
198076/**************************************************************************
198077** Interface to code in fts5_varint.c.
198078*/
198079static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v);
198080static int sqlite3Fts5GetVarintLen(u32 iVal);
198081static u8 sqlite3Fts5GetVarint(const unsigned char*, u64*);
198082static int sqlite3Fts5PutVarint(unsigned char *p, u64 v);
198083
198084#define fts5GetVarint32(a,b) sqlite3Fts5GetVarint32(a,(u32*)&b)
198085#define fts5GetVarint sqlite3Fts5GetVarint
198086
198087#define fts5FastGetVarint32(a, iOff, nVal) { \
198088 nVal = (a)[iOff++]; \
198089 if( nVal & 0x80 ){ \
198090 iOff--; \
198091 iOff += fts5GetVarint32(&(a)[iOff], nVal); \
198092 } \
198093}
198094
198095
198096/*
198097** End of interface to code in fts5_varint.c.
198098**************************************************************************/
198099
198100
198101/**************************************************************************
198102** Interface to code in fts5.c.
198103*/
198104
198105static int sqlite3Fts5GetTokenizer(
198106 Fts5Global*,
198107 const char **azArg,
198108 int nArg,
198109 Fts5Tokenizer**,
198110 fts5_tokenizer**,
198111 char **pzErr
198112);
198113
198114static Fts5Index *sqlite3Fts5IndexFromCsrid(Fts5Global*, i64, Fts5Config **);
198115
198116/*
198117** End of interface to code in fts5.c.
198118**************************************************************************/
198119
198120/**************************************************************************
198121** Interface to code in fts5_hash.c.
198122*/
198123typedef struct Fts5Hash Fts5Hash;
198124
198125/*
198126** Create a hash table, free a hash table.
198127*/
198128static int sqlite3Fts5HashNew(Fts5Config*, Fts5Hash**, int *pnSize);
198129static void sqlite3Fts5HashFree(Fts5Hash*);
198130
198131static int sqlite3Fts5HashWrite(
198132 Fts5Hash*,
198133 i64 iRowid, /* Rowid for this entry */
198134 int iCol, /* Column token appears in (-ve -> delete) */
198135 int iPos, /* Position of token within column */
198136 char bByte,
198137 const char *pToken, int nToken /* Token to add or remove to or from index */
198138);
198139
198140/*
198141** Empty (but do not delete) a hash table.
198142*/
198143static void sqlite3Fts5HashClear(Fts5Hash*);
198144
198145static int sqlite3Fts5HashQuery(
198146 Fts5Hash*, /* Hash table to query */
198147 const char *pTerm, int nTerm, /* Query term */
198148 const u8 **ppDoclist, /* OUT: Pointer to doclist for pTerm */
198149 int *pnDoclist /* OUT: Size of doclist in bytes */
198150);
198151
198152static int sqlite3Fts5HashScanInit(
198153 Fts5Hash*, /* Hash table to query */
198154 const char *pTerm, int nTerm /* Query prefix */
198155);
198156static void sqlite3Fts5HashScanNext(Fts5Hash*);
198157static int sqlite3Fts5HashScanEof(Fts5Hash*);
198158static void sqlite3Fts5HashScanEntry(Fts5Hash *,
198159 const char **pzTerm, /* OUT: term (nul-terminated) */
198160 const u8 **ppDoclist, /* OUT: pointer to doclist */
198161 int *pnDoclist /* OUT: size of doclist in bytes */
198162);
198163
198164
198165/*
198166** End of interface to code in fts5_hash.c.
198167**************************************************************************/
198168
198169/**************************************************************************
198170** Interface to code in fts5_storage.c. fts5_storage.c contains contains
198171** code to access the data stored in the %_content and %_docsize tables.
198172*/
198173
198174#define FTS5_STMT_SCAN_ASC 0 /* SELECT rowid, * FROM ... ORDER BY 1 ASC */
198175#define FTS5_STMT_SCAN_DESC 1 /* SELECT rowid, * FROM ... ORDER BY 1 DESC */
198176#define FTS5_STMT_LOOKUP 2 /* SELECT rowid, * FROM ... WHERE rowid=? */
198177
198178typedef struct Fts5Storage Fts5Storage;
198179
198180static int sqlite3Fts5StorageOpen(Fts5Config*, Fts5Index*, int, Fts5Storage**, char**);
198181static int sqlite3Fts5StorageClose(Fts5Storage *p);
198182static int sqlite3Fts5StorageRename(Fts5Storage*, const char *zName);
198183
198184static int sqlite3Fts5DropAll(Fts5Config*);
198185static int sqlite3Fts5CreateTable(Fts5Config*, const char*, const char*, int, char **);
198186
198187static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**);
198188static int sqlite3Fts5StorageContentInsert(Fts5Storage *p, sqlite3_value**, i64*);
198189static int sqlite3Fts5StorageIndexInsert(Fts5Storage *p, sqlite3_value**, i64);
198190
198191static int sqlite3Fts5StorageIntegrity(Fts5Storage *p);
198192
198193static int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**);
198194static void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*);
198195
198196static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol);
198197static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg);
198198static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow);
198199
198200static int sqlite3Fts5StorageSync(Fts5Storage *p);
198201static int sqlite3Fts5StorageRollback(Fts5Storage *p);
198202
198203static int sqlite3Fts5StorageConfigValue(
198204 Fts5Storage *p, const char*, sqlite3_value*, int
198205);
198206
198207static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p);
198208static int sqlite3Fts5StorageRebuild(Fts5Storage *p);
198209static int sqlite3Fts5StorageOptimize(Fts5Storage *p);
198210static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge);
198211static int sqlite3Fts5StorageReset(Fts5Storage *p);
198212
198213/*
198214** End of interface to code in fts5_storage.c.
198215**************************************************************************/
198216
198217
198218/**************************************************************************
198219** Interface to code in fts5_expr.c.
198220*/
198221typedef struct Fts5Expr Fts5Expr;
198222typedef struct Fts5ExprNode Fts5ExprNode;
198223typedef struct Fts5Parse Fts5Parse;
198224typedef struct Fts5Token Fts5Token;
198225typedef struct Fts5ExprPhrase Fts5ExprPhrase;
198226typedef struct Fts5ExprNearset Fts5ExprNearset;
198227
198228struct Fts5Token {
198229 const char *p; /* Token text (not NULL terminated) */
198230 int n; /* Size of buffer p in bytes */
198231};
198232
198233/* Parse a MATCH expression. */
198234static int sqlite3Fts5ExprNew(
198235 Fts5Config *pConfig,
198236 int iCol, /* Column on LHS of MATCH operator */
198237 const char *zExpr,
198238 Fts5Expr **ppNew,
198239 char **pzErr
198240);
198241
198242/*
198243** for(rc = sqlite3Fts5ExprFirst(pExpr, pIdx, bDesc);
198244** rc==SQLITE_OK && 0==sqlite3Fts5ExprEof(pExpr);
198245** rc = sqlite3Fts5ExprNext(pExpr)
198246** ){
198247** // The document with rowid iRowid matches the expression!
198248** i64 iRowid = sqlite3Fts5ExprRowid(pExpr);
198249** }
198250*/
198251static int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, int bDesc);
198252static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
198253static int sqlite3Fts5ExprEof(Fts5Expr*);
198254static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
198255
198256static void sqlite3Fts5ExprFree(Fts5Expr*);
198257
198258/* Called during startup to register a UDF with SQLite */
198259static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
198260
198261static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
198262static int sqlite3Fts5ExprPhraseSize(Fts5Expr*, int iPhrase);
198263static int sqlite3Fts5ExprPoslist(Fts5Expr*, int, const u8 **);
198264
198265typedef struct Fts5PoslistPopulator Fts5PoslistPopulator;
198266static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr*, int);
198267static int sqlite3Fts5ExprPopulatePoslists(
198268 Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
198269);
198270static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
198271
198272static int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
198273
198274static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
198275
198276/*******************************************
198277** The fts5_expr.c API above this point is used by the other hand-written
198278** C code in this module. The interfaces below this point are called by
198279** the parser code in fts5parse.y. */
198280
198281static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...);
198282
198283static Fts5ExprNode *sqlite3Fts5ParseNode(
198284 Fts5Parse *pParse,
198285 int eType,
198286 Fts5ExprNode *pLeft,
198287 Fts5ExprNode *pRight,
198288 Fts5ExprNearset *pNear
198289);
198290
198291static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
198292 Fts5Parse *pParse,
198293 Fts5ExprNode *pLeft,
198294 Fts5ExprNode *pRight
198295);
198296
198297static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
198298 Fts5Parse *pParse,
198299 Fts5ExprPhrase *pPhrase,
198300 Fts5Token *pToken,
198301 int bPrefix
198302);
198303
198304static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase*);
198305
198306static Fts5ExprNearset *sqlite3Fts5ParseNearset(
198307 Fts5Parse*,
198308 Fts5ExprNearset*,
198309 Fts5ExprPhrase*
198310);
198311
198312static Fts5Colset *sqlite3Fts5ParseColset(
198313 Fts5Parse*,
198314 Fts5Colset*,
198315 Fts5Token *
198316);
198317
198318static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase*);
198319static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset*);
198320static void sqlite3Fts5ParseNodeFree(Fts5ExprNode*);
198321
198322static void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*);
198323static void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNode*, Fts5Colset*);
198324static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse*, Fts5Colset*);
198325static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p);
198326static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*);
198327
198328/*
198329** End of interface to code in fts5_expr.c.
198330**************************************************************************/
198331
198332
198333
198334/**************************************************************************
198335** Interface to code in fts5_aux.c.
198336*/
198337
198338static int sqlite3Fts5AuxInit(fts5_api*);
198339/*
198340** End of interface to code in fts5_aux.c.
198341**************************************************************************/
198342
198343/**************************************************************************
198344** Interface to code in fts5_tokenizer.c.
198345*/
198346
198347static int sqlite3Fts5TokenizerInit(fts5_api*);
198348/*
198349** End of interface to code in fts5_tokenizer.c.
198350**************************************************************************/
198351
198352/**************************************************************************
198353** Interface to code in fts5_vocab.c.
198354*/
198355
198356static int sqlite3Fts5VocabInit(Fts5Global*, sqlite3*);
198357
198358/*
198359** End of interface to code in fts5_vocab.c.
198360**************************************************************************/
198361
198362
198363/**************************************************************************
198364** Interface to automatically generated code in fts5_unicode2.c.
198365*/
198366static int sqlite3Fts5UnicodeIsdiacritic(int c);
198367static int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic);
198368
198369static int sqlite3Fts5UnicodeCatParse(const char*, u8*);
198370static int sqlite3Fts5UnicodeCategory(int iCode);
198371static void sqlite3Fts5UnicodeAscii(u8*, u8*);
198372/*
198373** End of interface to code in fts5_unicode2.c.
198374**************************************************************************/
198375
198376#endif
198377
198378#define FTS5_OR 1
198379#define FTS5_AND 2
198380#define FTS5_NOT 3
198381#define FTS5_TERM 4
198382#define FTS5_COLON 5
198383#define FTS5_MINUS 6
198384#define FTS5_LCP 7
198385#define FTS5_RCP 8
198386#define FTS5_STRING 9
198387#define FTS5_LP 10
198388#define FTS5_RP 11
198389#define FTS5_CARET 12
198390#define FTS5_COMMA 13
198391#define FTS5_PLUS 14
198392#define FTS5_STAR 15
198393
198394/*
198395** 2000-05-29
198396**
198397** The author disclaims copyright to this source code. In place of
198398** a legal notice, here is a blessing:
198399**
198400** May you do good and not evil.
198401** May you find forgiveness for yourself and forgive others.
198402** May you share freely, never taking more than you give.
198403**
198404*************************************************************************
198405** Driver template for the LEMON parser generator.
198406**
198407** The "lemon" program processes an LALR(1) input grammar file, then uses
198408** this template to construct a parser. The "lemon" program inserts text
198409** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
198410** interstitial "-" characters) contained in this template is changed into
198411** the value of the %name directive from the grammar. Otherwise, the content
198412** of this template is copied straight through into the generate parser
198413** source file.
198414**
198415** The following is the concatenation of all %include directives from the
198416** input grammar file:
198417*/
198418/* #include <stdio.h> */
198419/************ Begin %include sections from the grammar ************************/
198420
198421/* #include "fts5Int.h" */
198422/* #include "fts5parse.h" */
198423
198424/*
198425** Disable all error recovery processing in the parser push-down
198426** automaton.
198427*/
198428#define fts5YYNOERRORRECOVERY 1
198429
198430/*
198431** Make fts5yytestcase() the same as testcase()
198432*/
198433#define fts5yytestcase(X) testcase(X)
198434
198435/*
198436** Indicate that sqlite3ParserFree() will never be called with a null
198437** pointer.
198438*/
198439#define fts5YYPARSEFREENOTNULL 1
198440
198441/*
198442** Alternative datatype for the argument to the malloc() routine passed
198443** into sqlite3ParserAlloc(). The default is size_t.
198444*/
198445#define fts5YYMALLOCARGTYPE u64
198446
198447/**************** End of %include directives **********************************/
198448/* These constants specify the various numeric values for terminal symbols
198449** in a format understandable to "makeheaders". This section is blank unless
198450** "lemon" is run with the "-m" command-line option.
198451***************** Begin makeheaders token definitions *************************/
198452/**************** End makeheaders token definitions ***************************/
198453
198454/* The next sections is a series of control #defines.
198455** various aspects of the generated parser.
198456** fts5YYCODETYPE is the data type used to store the integer codes
198457** that represent terminal and non-terminal symbols.
198458** "unsigned char" is used if there are fewer than
198459** 256 symbols. Larger types otherwise.
198460** fts5YYNOCODE is a number of type fts5YYCODETYPE that is not used for
198461** any terminal or nonterminal symbol.
198462** fts5YYFALLBACK If defined, this indicates that one or more tokens
198463** (also known as: "terminal symbols") have fall-back
198464** values which should be used if the original symbol
198465** would not parse. This permits keywords to sometimes
198466** be used as identifiers, for example.
198467** fts5YYACTIONTYPE is the data type used for "action codes" - numbers
198468** that indicate what to do in response to the next
198469** token.
198470** sqlite3Fts5ParserFTS5TOKENTYPE is the data type used for minor type for terminal
198471** symbols. Background: A "minor type" is a semantic
198472** value associated with a terminal or non-terminal
198473** symbols. For example, for an "ID" terminal symbol,
198474** the minor type might be the name of the identifier.
198475** Each non-terminal can have a different minor type.
198476** Terminal symbols all have the same minor type, though.
198477** This macros defines the minor type for terminal
198478** symbols.
198479** fts5YYMINORTYPE is the data type used for all minor types.
198480** This is typically a union of many types, one of
198481** which is sqlite3Fts5ParserFTS5TOKENTYPE. The entry in the union
198482** for terminal symbols is called "fts5yy0".
198483** fts5YYSTACKDEPTH is the maximum depth of the parser's stack. If
198484** zero the stack is dynamically sized using realloc()
198485** sqlite3Fts5ParserARG_SDECL A static variable declaration for the %extra_argument
198486** sqlite3Fts5ParserARG_PDECL A parameter declaration for the %extra_argument
198487** sqlite3Fts5ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
198488** sqlite3Fts5ParserARG_STORE Code to store %extra_argument into fts5yypParser
198489** sqlite3Fts5ParserARG_FETCH Code to extract %extra_argument from fts5yypParser
198490** sqlite3Fts5ParserCTX_* As sqlite3Fts5ParserARG_ except for %extra_context
198491** fts5YYERRORSYMBOL is the code number of the error symbol. If not
198492** defined, then do no error processing.
198493** fts5YYNSTATE the combined number of states.
198494** fts5YYNRULE the number of rules in the grammar
198495** fts5YYNFTS5TOKEN Number of terminal symbols
198496** fts5YY_MAX_SHIFT Maximum value for shift actions
198497** fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
198498** fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
198499** fts5YY_ERROR_ACTION The fts5yy_action[] code for syntax error
198500** fts5YY_ACCEPT_ACTION The fts5yy_action[] code for accept
198501** fts5YY_NO_ACTION The fts5yy_action[] code for no-op
198502** fts5YY_MIN_REDUCE Minimum value for reduce actions
198503** fts5YY_MAX_REDUCE Maximum value for reduce actions
198504*/
198505#ifndef INTERFACE
198506# define INTERFACE 1
198507#endif
198508/************* Begin control #defines *****************************************/
198509#define fts5YYCODETYPE unsigned char
198510#define fts5YYNOCODE 27
198511#define fts5YYACTIONTYPE unsigned char
198512#define sqlite3Fts5ParserFTS5TOKENTYPE Fts5Token
198513typedef union {
198514 int fts5yyinit;
198515 sqlite3Fts5ParserFTS5TOKENTYPE fts5yy0;
198516 int fts5yy4;
198517 Fts5Colset* fts5yy11;
198518 Fts5ExprNode* fts5yy24;
198519 Fts5ExprNearset* fts5yy46;
198520 Fts5ExprPhrase* fts5yy53;
198521} fts5YYMINORTYPE;
198522#ifndef fts5YYSTACKDEPTH
198523#define fts5YYSTACKDEPTH 100
198524#endif
198525#define sqlite3Fts5ParserARG_SDECL Fts5Parse *pParse;
198526#define sqlite3Fts5ParserARG_PDECL ,Fts5Parse *pParse
198527#define sqlite3Fts5ParserARG_PARAM ,pParse
198528#define sqlite3Fts5ParserARG_FETCH Fts5Parse *pParse=fts5yypParser->pParse;
198529#define sqlite3Fts5ParserARG_STORE fts5yypParser->pParse=pParse;
198530#define sqlite3Fts5ParserCTX_SDECL
198531#define sqlite3Fts5ParserCTX_PDECL
198532#define sqlite3Fts5ParserCTX_PARAM
198533#define sqlite3Fts5ParserCTX_FETCH
198534#define sqlite3Fts5ParserCTX_STORE
198535#define fts5YYNSTATE 35
198536#define fts5YYNRULE 28
198537#define fts5YYNFTS5TOKEN 16
198538#define fts5YY_MAX_SHIFT 34
198539#define fts5YY_MIN_SHIFTREDUCE 52
198540#define fts5YY_MAX_SHIFTREDUCE 79
198541#define fts5YY_ERROR_ACTION 80
198542#define fts5YY_ACCEPT_ACTION 81
198543#define fts5YY_NO_ACTION 82
198544#define fts5YY_MIN_REDUCE 83
198545#define fts5YY_MAX_REDUCE 110
198546/************* End control #defines *******************************************/
198547#define fts5YY_NLOOKAHEAD ((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])))
198548
198549/* Define the fts5yytestcase() macro to be a no-op if is not already defined
198550** otherwise.
198551**
198552** Applications can choose to define fts5yytestcase() in the %include section
198553** to a macro that can assist in verifying code coverage. For production
198554** code the fts5yytestcase() macro should be turned off. But it is useful
198555** for testing.
198556*/
198557#ifndef fts5yytestcase
198558# define fts5yytestcase(X)
198559#endif
198560
198561
198562/* Next are the tables used to determine what action to take based on the
198563** current state and lookahead token. These tables are used to implement
198564** functions that take a state number and lookahead value and return an
198565** action integer.
198566**
198567** Suppose the action integer is N. Then the action is determined as
198568** follows
198569**
198570** 0 <= N <= fts5YY_MAX_SHIFT Shift N. That is, push the lookahead
198571** token onto the stack and goto state N.
198572**
198573** N between fts5YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
198574** and fts5YY_MAX_SHIFTREDUCE reduce by rule N-fts5YY_MIN_SHIFTREDUCE.
198575**
198576** N == fts5YY_ERROR_ACTION A syntax error has occurred.
198577**
198578** N == fts5YY_ACCEPT_ACTION The parser accepts its input.
198579**
198580** N == fts5YY_NO_ACTION No such action. Denotes unused
198581** slots in the fts5yy_action[] table.
198582**
198583** N between fts5YY_MIN_REDUCE Reduce by rule N-fts5YY_MIN_REDUCE
198584** and fts5YY_MAX_REDUCE
198585**
198586** The action table is constructed as a single large table named fts5yy_action[].
198587** Given state S and lookahead X, the action is computed as either:
198588**
198589** (A) N = fts5yy_action[ fts5yy_shift_ofst[S] + X ]
198590** (B) N = fts5yy_default[S]
198591**
198592** The (A) formula is preferred. The B formula is used instead if
198593** fts5yy_lookahead[fts5yy_shift_ofst[S]+X] is not equal to X.
198594**
198595** The formulas above are for computing the action when the lookahead is
198596** a terminal symbol. If the lookahead is a non-terminal (as occurs after
198597** a reduce action) then the fts5yy_reduce_ofst[] array is used in place of
198598** the fts5yy_shift_ofst[] array.
198599**
198600** The following are the tables generated in this section:
198601**
198602** fts5yy_action[] A single table containing all actions.
198603** fts5yy_lookahead[] A table containing the lookahead for each entry in
198604** fts5yy_action. Used to detect hash collisions.
198605** fts5yy_shift_ofst[] For each state, the offset into fts5yy_action for
198606** shifting terminals.
198607** fts5yy_reduce_ofst[] For each state, the offset into fts5yy_action for
198608** shifting non-terminals after a reduce.
198609** fts5yy_default[] Default action for each state.
198610**
198611*********** Begin parsing tables **********************************************/
198612#define fts5YY_ACTTAB_COUNT (105)
198613static const fts5YYACTIONTYPE fts5yy_action[] = {
198614 /* 0 */ 81, 20, 96, 6, 28, 99, 98, 26, 26, 18,
198615 /* 10 */ 96, 6, 28, 17, 98, 56, 26, 19, 96, 6,
198616 /* 20 */ 28, 14, 98, 14, 26, 31, 92, 96, 6, 28,
198617 /* 30 */ 108, 98, 25, 26, 21, 96, 6, 28, 78, 98,
198618 /* 40 */ 58, 26, 29, 96, 6, 28, 107, 98, 22, 26,
198619 /* 50 */ 24, 16, 12, 11, 1, 13, 13, 24, 16, 23,
198620 /* 60 */ 11, 33, 34, 13, 97, 8, 27, 32, 98, 7,
198621 /* 70 */ 26, 3, 4, 5, 3, 4, 5, 3, 83, 4,
198622 /* 80 */ 5, 3, 63, 5, 3, 62, 12, 2, 86, 13,
198623 /* 90 */ 9, 30, 10, 10, 54, 57, 75, 78, 78, 53,
198624 /* 100 */ 57, 15, 82, 82, 71,
198625};
198626static const fts5YYCODETYPE fts5yy_lookahead[] = {
198627 /* 0 */ 16, 17, 18, 19, 20, 22, 22, 24, 24, 17,
198628 /* 10 */ 18, 19, 20, 7, 22, 9, 24, 17, 18, 19,
198629 /* 20 */ 20, 9, 22, 9, 24, 13, 17, 18, 19, 20,
198630 /* 30 */ 26, 22, 24, 24, 17, 18, 19, 20, 15, 22,
198631 /* 40 */ 9, 24, 17, 18, 19, 20, 26, 22, 21, 24,
198632 /* 50 */ 6, 7, 9, 9, 10, 12, 12, 6, 7, 21,
198633 /* 60 */ 9, 24, 25, 12, 18, 5, 20, 14, 22, 5,
198634 /* 70 */ 24, 3, 1, 2, 3, 1, 2, 3, 0, 1,
198635 /* 80 */ 2, 3, 11, 2, 3, 11, 9, 10, 5, 12,
198636 /* 90 */ 23, 24, 10, 10, 8, 9, 9, 15, 15, 8,
198637 /* 100 */ 9, 9, 27, 27, 11, 27, 27, 27, 27, 27,
198638 /* 110 */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
198639 /* 120 */ 27,
198640};
198641#define fts5YY_SHIFT_COUNT (34)
198642#define fts5YY_SHIFT_MIN (0)
198643#define fts5YY_SHIFT_MAX (93)
198644static const unsigned char fts5yy_shift_ofst[] = {
198645 /* 0 */ 44, 44, 44, 44, 44, 44, 51, 77, 43, 12,
198646 /* 10 */ 14, 83, 82, 14, 23, 23, 31, 31, 71, 74,
198647 /* 20 */ 78, 81, 86, 91, 6, 53, 53, 60, 64, 68,
198648 /* 30 */ 53, 87, 92, 53, 93,
198649};
198650#define fts5YY_REDUCE_COUNT (17)
198651#define fts5YY_REDUCE_MIN (-17)
198652#define fts5YY_REDUCE_MAX (67)
198653static const signed char fts5yy_reduce_ofst[] = {
198654 /* 0 */ -16, -8, 0, 9, 17, 25, 46, -17, -17, 37,
198655 /* 10 */ 67, 4, 4, 8, 4, 20, 27, 38,
198656};
198657static const fts5YYACTIONTYPE fts5yy_default[] = {
198658 /* 0 */ 80, 80, 80, 80, 80, 80, 95, 80, 80, 105,
198659 /* 10 */ 80, 110, 110, 80, 110, 110, 80, 80, 80, 80,
198660 /* 20 */ 80, 91, 80, 80, 80, 101, 100, 80, 80, 90,
198661 /* 30 */ 103, 80, 80, 104, 80,
198662};
198663/********** End of lemon-generated parsing tables *****************************/
198664
198665/* The next table maps tokens (terminal symbols) into fallback tokens.
198666** If a construct like the following:
198667**
198668** %fallback ID X Y Z.
198669**
198670** appears in the grammar, then ID becomes a fallback token for X, Y,
198671** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
198672** but it does not parse, the type of the token is changed to ID and
198673** the parse is retried before an error is thrown.
198674**
198675** This feature can be used, for example, to cause some keywords in a language
198676** to revert to identifiers if they keyword does not apply in the context where
198677** it appears.
198678*/
198679#ifdef fts5YYFALLBACK
198680static const fts5YYCODETYPE fts5yyFallback[] = {
198681};
198682#endif /* fts5YYFALLBACK */
198683
198684/* The following structure represents a single element of the
198685** parser's stack. Information stored includes:
198686**
198687** + The state number for the parser at this level of the stack.
198688**
198689** + The value of the token stored at this level of the stack.
198690** (In other words, the "major" token.)
198691**
198692** + The semantic value stored at this level of the stack. This is
198693** the information used by the action routines in the grammar.
198694** It is sometimes called the "minor" token.
198695**
198696** After the "shift" half of a SHIFTREDUCE action, the stateno field
198697** actually contains the reduce action for the second half of the
198698** SHIFTREDUCE.
198699*/
198700struct fts5yyStackEntry {
198701 fts5YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
198702 fts5YYCODETYPE major; /* The major token value. This is the code
198703 ** number for the token at this stack level */
198704 fts5YYMINORTYPE minor; /* The user-supplied minor token value. This
198705 ** is the value of the token */
198706};
198707typedef struct fts5yyStackEntry fts5yyStackEntry;
198708
198709/* The state of the parser is completely contained in an instance of
198710** the following structure */
198711struct fts5yyParser {
198712 fts5yyStackEntry *fts5yytos; /* Pointer to top element of the stack */
198713#ifdef fts5YYTRACKMAXSTACKDEPTH
198714 int fts5yyhwm; /* High-water mark of the stack */
198715#endif
198716#ifndef fts5YYNOERRORRECOVERY
198717 int fts5yyerrcnt; /* Shifts left before out of the error */
198718#endif
198719 sqlite3Fts5ParserARG_SDECL /* A place to hold %extra_argument */
198720 sqlite3Fts5ParserCTX_SDECL /* A place to hold %extra_context */
198721#if fts5YYSTACKDEPTH<=0
198722 int fts5yystksz; /* Current side of the stack */
198723 fts5yyStackEntry *fts5yystack; /* The parser's stack */
198724 fts5yyStackEntry fts5yystk0; /* First stack entry */
198725#else
198726 fts5yyStackEntry fts5yystack[fts5YYSTACKDEPTH]; /* The parser's stack */
198727 fts5yyStackEntry *fts5yystackEnd; /* Last entry in the stack */
198728#endif
198729};
198730typedef struct fts5yyParser fts5yyParser;
198731
198732#ifndef NDEBUG
198733/* #include <stdio.h> */
198734static FILE *fts5yyTraceFILE = 0;
198735static char *fts5yyTracePrompt = 0;
198736#endif /* NDEBUG */
198737
198738#ifndef NDEBUG
198739/*
198740** Turn parser tracing on by giving a stream to which to write the trace
198741** and a prompt to preface each trace message. Tracing is turned off
198742** by making either argument NULL
198743**
198744** Inputs:
198745** <ul>
198746** <li> A FILE* to which trace output should be written.
198747** If NULL, then tracing is turned off.
198748** <li> A prefix string written at the beginning of every
198749** line of trace output. If NULL, then tracing is
198750** turned off.
198751** </ul>
198752**
198753** Outputs:
198754** None.
198755*/
198756static void sqlite3Fts5ParserTrace(FILE *TraceFILE, char *zTracePrompt){
198757 fts5yyTraceFILE = TraceFILE;
198758 fts5yyTracePrompt = zTracePrompt;
198759 if( fts5yyTraceFILE==0 ) fts5yyTracePrompt = 0;
198760 else if( fts5yyTracePrompt==0 ) fts5yyTraceFILE = 0;
198761}
198762#endif /* NDEBUG */
198763
198764#if defined(fts5YYCOVERAGE) || !defined(NDEBUG)
198765/* For tracing shifts, the names of all terminals and nonterminals
198766** are required. The following table supplies these names */
198767static const char *const fts5yyTokenName[] = {
198768 /* 0 */ "$",
198769 /* 1 */ "OR",
198770 /* 2 */ "AND",
198771 /* 3 */ "NOT",
198772 /* 4 */ "TERM",
198773 /* 5 */ "COLON",
198774 /* 6 */ "MINUS",
198775 /* 7 */ "LCP",
198776 /* 8 */ "RCP",
198777 /* 9 */ "STRING",
198778 /* 10 */ "LP",
198779 /* 11 */ "RP",
198780 /* 12 */ "CARET",
198781 /* 13 */ "COMMA",
198782 /* 14 */ "PLUS",
198783 /* 15 */ "STAR",
198784 /* 16 */ "input",
198785 /* 17 */ "expr",
198786 /* 18 */ "cnearset",
198787 /* 19 */ "exprlist",
198788 /* 20 */ "colset",
198789 /* 21 */ "colsetlist",
198790 /* 22 */ "nearset",
198791 /* 23 */ "nearphrases",
198792 /* 24 */ "phrase",
198793 /* 25 */ "neardist_opt",
198794 /* 26 */ "star_opt",
198795};
198796#endif /* defined(fts5YYCOVERAGE) || !defined(NDEBUG) */
198797
198798#ifndef NDEBUG
198799/* For tracing reduce actions, the names of all rules are required.
198800*/
198801static const char *const fts5yyRuleName[] = {
198802 /* 0 */ "input ::= expr",
198803 /* 1 */ "colset ::= MINUS LCP colsetlist RCP",
198804 /* 2 */ "colset ::= LCP colsetlist RCP",
198805 /* 3 */ "colset ::= STRING",
198806 /* 4 */ "colset ::= MINUS STRING",
198807 /* 5 */ "colsetlist ::= colsetlist STRING",
198808 /* 6 */ "colsetlist ::= STRING",
198809 /* 7 */ "expr ::= expr AND expr",
198810 /* 8 */ "expr ::= expr OR expr",
198811 /* 9 */ "expr ::= expr NOT expr",
198812 /* 10 */ "expr ::= colset COLON LP expr RP",
198813 /* 11 */ "expr ::= LP expr RP",
198814 /* 12 */ "expr ::= exprlist",
198815 /* 13 */ "exprlist ::= cnearset",
198816 /* 14 */ "exprlist ::= exprlist cnearset",
198817 /* 15 */ "cnearset ::= nearset",
198818 /* 16 */ "cnearset ::= colset COLON nearset",
198819 /* 17 */ "nearset ::= phrase",
198820 /* 18 */ "nearset ::= CARET phrase",
198821 /* 19 */ "nearset ::= STRING LP nearphrases neardist_opt RP",
198822 /* 20 */ "nearphrases ::= phrase",
198823 /* 21 */ "nearphrases ::= nearphrases phrase",
198824 /* 22 */ "neardist_opt ::=",
198825 /* 23 */ "neardist_opt ::= COMMA STRING",
198826 /* 24 */ "phrase ::= phrase PLUS STRING star_opt",
198827 /* 25 */ "phrase ::= STRING star_opt",
198828 /* 26 */ "star_opt ::= STAR",
198829 /* 27 */ "star_opt ::=",
198830};
198831#endif /* NDEBUG */
198832
198833
198834#if fts5YYSTACKDEPTH<=0
198835/*
198836** Try to increase the size of the parser stack. Return the number
198837** of errors. Return 0 on success.
198838*/
198839static int fts5yyGrowStack(fts5yyParser *p){
198840 int newSize;
198841 int idx;
198842 fts5yyStackEntry *pNew;
198843
198844 newSize = p->fts5yystksz*2 + 100;
198845 idx = p->fts5yytos ? (int)(p->fts5yytos - p->fts5yystack) : 0;
198846 if( p->fts5yystack==&p->fts5yystk0 ){
198847 pNew = malloc(newSize*sizeof(pNew[0]));
198848 if( pNew ) pNew[0] = p->fts5yystk0;
198849 }else{
198850 pNew = realloc(p->fts5yystack, newSize*sizeof(pNew[0]));
198851 }
198852 if( pNew ){
198853 p->fts5yystack = pNew;
198854 p->fts5yytos = &p->fts5yystack[idx];
198855#ifndef NDEBUG
198856 if( fts5yyTraceFILE ){
198857 fprintf(fts5yyTraceFILE,"%sStack grows from %d to %d entries.\n",
198858 fts5yyTracePrompt, p->fts5yystksz, newSize);
198859 }
198860#endif
198861 p->fts5yystksz = newSize;
198862 }
198863 return pNew==0;
198864}
198865#endif
198866
198867/* Datatype of the argument to the memory allocated passed as the
198868** second argument to sqlite3Fts5ParserAlloc() below. This can be changed by
198869** putting an appropriate #define in the %include section of the input
198870** grammar.
198871*/
198872#ifndef fts5YYMALLOCARGTYPE
198873# define fts5YYMALLOCARGTYPE size_t
198874#endif
198875
198876/* Initialize a new parser that has already been allocated.
198877*/
198878static void sqlite3Fts5ParserInit(void *fts5yypRawParser sqlite3Fts5ParserCTX_PDECL){
198879 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yypRawParser;
198880 sqlite3Fts5ParserCTX_STORE
198881#ifdef fts5YYTRACKMAXSTACKDEPTH
198882 fts5yypParser->fts5yyhwm = 0;
198883#endif
198884#if fts5YYSTACKDEPTH<=0
198885 fts5yypParser->fts5yytos = NULL;
198886 fts5yypParser->fts5yystack = NULL;
198887 fts5yypParser->fts5yystksz = 0;
198888 if( fts5yyGrowStack(fts5yypParser) ){
198889 fts5yypParser->fts5yystack = &fts5yypParser->fts5yystk0;
198890 fts5yypParser->fts5yystksz = 1;
198891 }
198892#endif
198893#ifndef fts5YYNOERRORRECOVERY
198894 fts5yypParser->fts5yyerrcnt = -1;
198895#endif
198896 fts5yypParser->fts5yytos = fts5yypParser->fts5yystack;
198897 fts5yypParser->fts5yystack[0].stateno = 0;
198898 fts5yypParser->fts5yystack[0].major = 0;
198899#if fts5YYSTACKDEPTH>0
198900 fts5yypParser->fts5yystackEnd = &fts5yypParser->fts5yystack[fts5YYSTACKDEPTH-1];
198901#endif
198902}
198903
198904#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
198905/*
198906** This function allocates a new parser.
198907** The only argument is a pointer to a function which works like
198908** malloc.
198909**
198910** Inputs:
198911** A pointer to the function used to allocate memory.
198912**
198913** Outputs:
198914** A pointer to a parser. This pointer is used in subsequent calls
198915** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
198916*/
198917static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE) sqlite3Fts5ParserCTX_PDECL){
198918 fts5yyParser *fts5yypParser;
198919 fts5yypParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
198920 if( fts5yypParser ){
198921 sqlite3Fts5ParserCTX_STORE
198922 sqlite3Fts5ParserInit(fts5yypParser sqlite3Fts5ParserCTX_PARAM);
198923 }
198924 return (void*)fts5yypParser;
198925}
198926#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
198927
198928
198929/* The following function deletes the "minor type" or semantic value
198930** associated with a symbol. The symbol can be either a terminal
198931** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
198932** a pointer to the value to be deleted. The code used to do the
198933** deletions is derived from the %destructor and/or %token_destructor
198934** directives of the input grammar.
198935*/
198936static void fts5yy_destructor(
198937 fts5yyParser *fts5yypParser, /* The parser */
198938 fts5YYCODETYPE fts5yymajor, /* Type code for object to destroy */
198939 fts5YYMINORTYPE *fts5yypminor /* The object to be destroyed */
198940){
198941 sqlite3Fts5ParserARG_FETCH
198942 sqlite3Fts5ParserCTX_FETCH
198943 switch( fts5yymajor ){
198944 /* Here is inserted the actions which take place when a
198945 ** terminal or non-terminal is destroyed. This can happen
198946 ** when the symbol is popped from the stack during a
198947 ** reduce or during error processing or when a parser is
198948 ** being destroyed before it is finished parsing.
198949 **
198950 ** Note: during a reduce, the only symbols destroyed are those
198951 ** which appear on the RHS of the rule, but which are *not* used
198952 ** inside the C code.
198953 */
198954/********* Begin destructor definitions ***************************************/
198955 case 16: /* input */
198956{
198957 (void)pParse;
198958}
198959 break;
198960 case 17: /* expr */
198961 case 18: /* cnearset */
198962 case 19: /* exprlist */
198963{
198964 sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24));
198965}
198966 break;
198967 case 20: /* colset */
198968 case 21: /* colsetlist */
198969{
198970 sqlite3_free((fts5yypminor->fts5yy11));
198971}
198972 break;
198973 case 22: /* nearset */
198974 case 23: /* nearphrases */
198975{
198976 sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46));
198977}
198978 break;
198979 case 24: /* phrase */
198980{
198981 sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53));
198982}
198983 break;
198984/********* End destructor definitions *****************************************/
198985 default: break; /* If no destructor action specified: do nothing */
198986 }
198987}
198988
198989/*
198990** Pop the parser's stack once.
198991**
198992** If there is a destructor routine associated with the token which
198993** is popped from the stack, then call it.
198994*/
198995static void fts5yy_pop_parser_stack(fts5yyParser *pParser){
198996 fts5yyStackEntry *fts5yytos;
198997 assert( pParser->fts5yytos!=0 );
198998 assert( pParser->fts5yytos > pParser->fts5yystack );
198999 fts5yytos = pParser->fts5yytos--;
199000#ifndef NDEBUG
199001 if( fts5yyTraceFILE ){
199002 fprintf(fts5yyTraceFILE,"%sPopping %s\n",
199003 fts5yyTracePrompt,
199004 fts5yyTokenName[fts5yytos->major]);
199005 }
199006#endif
199007 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
199008}
199009
199010/*
199011** Clear all secondary memory allocations from the parser
199012*/
199013static void sqlite3Fts5ParserFinalize(void *p){
199014 fts5yyParser *pParser = (fts5yyParser*)p;
199015 while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
199016#if fts5YYSTACKDEPTH<=0
199017 if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
199018#endif
199019}
199020
199021#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
199022/*
199023** Deallocate and destroy a parser. Destructors are called for
199024** all stack elements before shutting the parser down.
199025**
199026** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
199027** is defined in a %include section of the input grammar) then it is
199028** assumed that the input pointer is never NULL.
199029*/
199030static void sqlite3Fts5ParserFree(
199031 void *p, /* The parser to be deleted */
199032 void (*freeProc)(void*) /* Function used to reclaim memory */
199033){
199034#ifndef fts5YYPARSEFREENEVERNULL
199035 if( p==0 ) return;
199036#endif
199037 sqlite3Fts5ParserFinalize(p);
199038 (*freeProc)(p);
199039}
199040#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
199041
199042/*
199043** Return the peak depth of the stack for a parser.
199044*/
199045#ifdef fts5YYTRACKMAXSTACKDEPTH
199046static int sqlite3Fts5ParserStackPeak(void *p){
199047 fts5yyParser *pParser = (fts5yyParser*)p;
199048 return pParser->fts5yyhwm;
199049}
199050#endif
199051
199052/* This array of booleans keeps track of the parser statement
199053** coverage. The element fts5yycoverage[X][Y] is set when the parser
199054** is in state X and has a lookahead token Y. In a well-tested
199055** systems, every element of this matrix should end up being set.
199056*/
199057#if defined(fts5YYCOVERAGE)
199058static unsigned char fts5yycoverage[fts5YYNSTATE][fts5YYNFTS5TOKEN];
199059#endif
199060
199061/*
199062** Write into out a description of every state/lookahead combination that
199063**
199064** (1) has not been used by the parser, and
199065** (2) is not a syntax error.
199066**
199067** Return the number of missed state/lookahead combinations.
199068*/
199069#if defined(fts5YYCOVERAGE)
199070static int sqlite3Fts5ParserCoverage(FILE *out){
199071 int stateno, iLookAhead, i;
199072 int nMissed = 0;
199073 for(stateno=0; stateno<fts5YYNSTATE; stateno++){
199074 i = fts5yy_shift_ofst[stateno];
199075 for(iLookAhead=0; iLookAhead<fts5YYNFTS5TOKEN; iLookAhead++){
199076 if( fts5yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
199077 if( fts5yycoverage[stateno][iLookAhead]==0 ) nMissed++;
199078 if( out ){
199079 fprintf(out,"State %d lookahead %s %s\n", stateno,
199080 fts5yyTokenName[iLookAhead],
199081 fts5yycoverage[stateno][iLookAhead] ? "ok" : "missed");
199082 }
199083 }
199084 }
199085 return nMissed;
199086}
199087#endif
199088
199089/*
199090** Find the appropriate action for a parser given the terminal
199091** look-ahead token iLookAhead.
199092*/
199093static fts5YYACTIONTYPE fts5yy_find_shift_action(
199094 fts5YYCODETYPE iLookAhead, /* The look-ahead token */
199095 fts5YYACTIONTYPE stateno /* Current state number */
199096){
199097 int i;
199098
199099 if( stateno>fts5YY_MAX_SHIFT ) return stateno;
199100 assert( stateno <= fts5YY_SHIFT_COUNT );
199101#if defined(fts5YYCOVERAGE)
199102 fts5yycoverage[stateno][iLookAhead] = 1;
199103#endif
199104 do{
199105 i = fts5yy_shift_ofst[stateno];
199106 assert( i>=0 );
199107 /* assert( i+fts5YYNFTS5TOKEN<=(int)fts5YY_NLOOKAHEAD ); */
199108 assert( iLookAhead!=fts5YYNOCODE );
199109 assert( iLookAhead < fts5YYNFTS5TOKEN );
199110 i += iLookAhead;
199111 if( i>=fts5YY_NLOOKAHEAD || fts5yy_lookahead[i]!=iLookAhead ){
199112#ifdef fts5YYFALLBACK
199113 fts5YYCODETYPE iFallback; /* Fallback token */
199114 if( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])
199115 && (iFallback = fts5yyFallback[iLookAhead])!=0 ){
199116#ifndef NDEBUG
199117 if( fts5yyTraceFILE ){
199118 fprintf(fts5yyTraceFILE, "%sFALLBACK %s => %s\n",
199119 fts5yyTracePrompt, fts5yyTokenName[iLookAhead], fts5yyTokenName[iFallback]);
199120 }
199121#endif
199122 assert( fts5yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
199123 iLookAhead = iFallback;
199124 continue;
199125 }
199126#endif
199127#ifdef fts5YYWILDCARD
199128 {
199129 int j = i - iLookAhead + fts5YYWILDCARD;
199130 if(
199131#if fts5YY_SHIFT_MIN+fts5YYWILDCARD<0
199132 j>=0 &&
199133#endif
199134#if fts5YY_SHIFT_MAX+fts5YYWILDCARD>=fts5YY_ACTTAB_COUNT
199135 j<fts5YY_ACTTAB_COUNT &&
199136#endif
199137 j<(int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])) &&
199138 fts5yy_lookahead[j]==fts5YYWILDCARD && iLookAhead>0
199139 ){
199140#ifndef NDEBUG
199141 if( fts5yyTraceFILE ){
199142 fprintf(fts5yyTraceFILE, "%sWILDCARD %s => %s\n",
199143 fts5yyTracePrompt, fts5yyTokenName[iLookAhead],
199144 fts5yyTokenName[fts5YYWILDCARD]);
199145 }
199146#endif /* NDEBUG */
199147 return fts5yy_action[j];
199148 }
199149 }
199150#endif /* fts5YYWILDCARD */
199151 return fts5yy_default[stateno];
199152 }else{
199153 return fts5yy_action[i];
199154 }
199155 }while(1);
199156}
199157
199158/*
199159** Find the appropriate action for a parser given the non-terminal
199160** look-ahead token iLookAhead.
199161*/
199162static fts5YYACTIONTYPE fts5yy_find_reduce_action(
199163 fts5YYACTIONTYPE stateno, /* Current state number */
199164 fts5YYCODETYPE iLookAhead /* The look-ahead token */
199165){
199166 int i;
199167#ifdef fts5YYERRORSYMBOL
199168 if( stateno>fts5YY_REDUCE_COUNT ){
199169 return fts5yy_default[stateno];
199170 }
199171#else
199172 assert( stateno<=fts5YY_REDUCE_COUNT );
199173#endif
199174 i = fts5yy_reduce_ofst[stateno];
199175 assert( iLookAhead!=fts5YYNOCODE );
199176 i += iLookAhead;
199177#ifdef fts5YYERRORSYMBOL
199178 if( i<0 || i>=fts5YY_ACTTAB_COUNT || fts5yy_lookahead[i]!=iLookAhead ){
199179 return fts5yy_default[stateno];
199180 }
199181#else
199182 assert( i>=0 && i<fts5YY_ACTTAB_COUNT );
199183 assert( fts5yy_lookahead[i]==iLookAhead );
199184#endif
199185 return fts5yy_action[i];
199186}
199187
199188/*
199189** The following routine is called if the stack overflows.
199190*/
199191static void fts5yyStackOverflow(fts5yyParser *fts5yypParser){
199192 sqlite3Fts5ParserARG_FETCH
199193 sqlite3Fts5ParserCTX_FETCH
199194#ifndef NDEBUG
199195 if( fts5yyTraceFILE ){
199196 fprintf(fts5yyTraceFILE,"%sStack Overflow!\n",fts5yyTracePrompt);
199197 }
199198#endif
199199 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
199200 /* Here code is inserted which will execute if the parser
199201 ** stack every overflows */
199202/******** Begin %stack_overflow code ******************************************/
199203
199204 sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow");
199205/******** End %stack_overflow code ********************************************/
199206 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument var */
199207 sqlite3Fts5ParserCTX_STORE
199208}
199209
199210/*
199211** Print tracing information for a SHIFT action
199212*/
199213#ifndef NDEBUG
199214static void fts5yyTraceShift(fts5yyParser *fts5yypParser, int fts5yyNewState, const char *zTag){
199215 if( fts5yyTraceFILE ){
199216 if( fts5yyNewState<fts5YYNSTATE ){
199217 fprintf(fts5yyTraceFILE,"%s%s '%s', go to state %d\n",
199218 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
199219 fts5yyNewState);
199220 }else{
199221 fprintf(fts5yyTraceFILE,"%s%s '%s', pending reduce %d\n",
199222 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
199223 fts5yyNewState - fts5YY_MIN_REDUCE);
199224 }
199225 }
199226}
199227#else
199228# define fts5yyTraceShift(X,Y,Z)
199229#endif
199230
199231/*
199232** Perform a shift action.
199233*/
199234static void fts5yy_shift(
199235 fts5yyParser *fts5yypParser, /* The parser to be shifted */
199236 fts5YYACTIONTYPE fts5yyNewState, /* The new state to shift in */
199237 fts5YYCODETYPE fts5yyMajor, /* The major token to shift in */
199238 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyMinor /* The minor token to shift in */
199239){
199240 fts5yyStackEntry *fts5yytos;
199241 fts5yypParser->fts5yytos++;
199242#ifdef fts5YYTRACKMAXSTACKDEPTH
199243 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
199244 fts5yypParser->fts5yyhwm++;
199245 assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack) );
199246 }
199247#endif
199248#if fts5YYSTACKDEPTH>0
199249 if( fts5yypParser->fts5yytos>fts5yypParser->fts5yystackEnd ){
199250 fts5yypParser->fts5yytos--;
199251 fts5yyStackOverflow(fts5yypParser);
199252 return;
199253 }
199254#else
199255 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz] ){
199256 if( fts5yyGrowStack(fts5yypParser) ){
199257 fts5yypParser->fts5yytos--;
199258 fts5yyStackOverflow(fts5yypParser);
199259 return;
199260 }
199261 }
199262#endif
199263 if( fts5yyNewState > fts5YY_MAX_SHIFT ){
199264 fts5yyNewState += fts5YY_MIN_REDUCE - fts5YY_MIN_SHIFTREDUCE;
199265 }
199266 fts5yytos = fts5yypParser->fts5yytos;
199267 fts5yytos->stateno = fts5yyNewState;
199268 fts5yytos->major = fts5yyMajor;
199269 fts5yytos->minor.fts5yy0 = fts5yyMinor;
199270 fts5yyTraceShift(fts5yypParser, fts5yyNewState, "Shift");
199271}
199272
199273/* The following table contains information about every rule that
199274** is used during the reduce.
199275*/
199276static const struct {
199277 fts5YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
199278 signed char nrhs; /* Negative of the number of RHS symbols in the rule */
199279} fts5yyRuleInfo[] = {
199280 { 16, -1 }, /* (0) input ::= expr */
199281 { 20, -4 }, /* (1) colset ::= MINUS LCP colsetlist RCP */
199282 { 20, -3 }, /* (2) colset ::= LCP colsetlist RCP */
199283 { 20, -1 }, /* (3) colset ::= STRING */
199284 { 20, -2 }, /* (4) colset ::= MINUS STRING */
199285 { 21, -2 }, /* (5) colsetlist ::= colsetlist STRING */
199286 { 21, -1 }, /* (6) colsetlist ::= STRING */
199287 { 17, -3 }, /* (7) expr ::= expr AND expr */
199288 { 17, -3 }, /* (8) expr ::= expr OR expr */
199289 { 17, -3 }, /* (9) expr ::= expr NOT expr */
199290 { 17, -5 }, /* (10) expr ::= colset COLON LP expr RP */
199291 { 17, -3 }, /* (11) expr ::= LP expr RP */
199292 { 17, -1 }, /* (12) expr ::= exprlist */
199293 { 19, -1 }, /* (13) exprlist ::= cnearset */
199294 { 19, -2 }, /* (14) exprlist ::= exprlist cnearset */
199295 { 18, -1 }, /* (15) cnearset ::= nearset */
199296 { 18, -3 }, /* (16) cnearset ::= colset COLON nearset */
199297 { 22, -1 }, /* (17) nearset ::= phrase */
199298 { 22, -2 }, /* (18) nearset ::= CARET phrase */
199299 { 22, -5 }, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
199300 { 23, -1 }, /* (20) nearphrases ::= phrase */
199301 { 23, -2 }, /* (21) nearphrases ::= nearphrases phrase */
199302 { 25, 0 }, /* (22) neardist_opt ::= */
199303 { 25, -2 }, /* (23) neardist_opt ::= COMMA STRING */
199304 { 24, -4 }, /* (24) phrase ::= phrase PLUS STRING star_opt */
199305 { 24, -2 }, /* (25) phrase ::= STRING star_opt */
199306 { 26, -1 }, /* (26) star_opt ::= STAR */
199307 { 26, 0 }, /* (27) star_opt ::= */
199308};
199309
199310static void fts5yy_accept(fts5yyParser*); /* Forward Declaration */
199311
199312/*
199313** Perform a reduce action and the shift that must immediately
199314** follow the reduce.
199315**
199316** The fts5yyLookahead and fts5yyLookaheadToken parameters provide reduce actions
199317** access to the lookahead token (if any). The fts5yyLookahead will be fts5YYNOCODE
199318** if the lookahead token has already been consumed. As this procedure is
199319** only called from one place, optimizing compilers will in-line it, which
199320** means that the extra parameters have no performance impact.
199321*/
199322static fts5YYACTIONTYPE fts5yy_reduce(
199323 fts5yyParser *fts5yypParser, /* The parser */
199324 unsigned int fts5yyruleno, /* Number of the rule by which to reduce */
199325 int fts5yyLookahead, /* Lookahead token, or fts5YYNOCODE if none */
199326 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyLookaheadToken /* Value of the lookahead token */
199327 sqlite3Fts5ParserCTX_PDECL /* %extra_context */
199328){
199329 int fts5yygoto; /* The next state */
199330 fts5YYACTIONTYPE fts5yyact; /* The next action */
199331 fts5yyStackEntry *fts5yymsp; /* The top of the parser's stack */
199332 int fts5yysize; /* Amount to pop the stack */
199333 sqlite3Fts5ParserARG_FETCH
199334 (void)fts5yyLookahead;
199335 (void)fts5yyLookaheadToken;
199336 fts5yymsp = fts5yypParser->fts5yytos;
199337#ifndef NDEBUG
199338 if( fts5yyTraceFILE && fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) ){
199339 fts5yysize = fts5yyRuleInfo[fts5yyruleno].nrhs;
199340 if( fts5yysize ){
199341 fprintf(fts5yyTraceFILE, "%sReduce %d [%s], go to state %d.\n",
199342 fts5yyTracePrompt,
199343 fts5yyruleno, fts5yyRuleName[fts5yyruleno], fts5yymsp[fts5yysize].stateno);
199344 }else{
199345 fprintf(fts5yyTraceFILE, "%sReduce %d [%s].\n",
199346 fts5yyTracePrompt, fts5yyruleno, fts5yyRuleName[fts5yyruleno]);
199347 }
199348 }
199349#endif /* NDEBUG */
199350
199351 /* Check that the stack is large enough to grow by a single entry
199352 ** if the RHS of the rule is empty. This ensures that there is room
199353 ** enough on the stack to push the LHS value */
199354 if( fts5yyRuleInfo[fts5yyruleno].nrhs==0 ){
199355#ifdef fts5YYTRACKMAXSTACKDEPTH
199356 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
199357 fts5yypParser->fts5yyhwm++;
199358 assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack));
199359 }
199360#endif
199361#if fts5YYSTACKDEPTH>0
199362 if( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystackEnd ){
199363 fts5yyStackOverflow(fts5yypParser);
199364 /* The call to fts5yyStackOverflow() above pops the stack until it is
199365 ** empty, causing the main parser loop to exit. So the return value
199366 ** is never used and does not matter. */
199367 return 0;
199368 }
199369#else
199370 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz-1] ){
199371 if( fts5yyGrowStack(fts5yypParser) ){
199372 fts5yyStackOverflow(fts5yypParser);
199373 /* The call to fts5yyStackOverflow() above pops the stack until it is
199374 ** empty, causing the main parser loop to exit. So the return value
199375 ** is never used and does not matter. */
199376 return 0;
199377 }
199378 fts5yymsp = fts5yypParser->fts5yytos;
199379 }
199380#endif
199381 }
199382
199383 switch( fts5yyruleno ){
199384 /* Beginning here are the reduction cases. A typical example
199385 ** follows:
199386 ** case 0:
199387 ** #line <lineno> <grammarfile>
199388 ** { ... } // User supplied code
199389 ** #line <lineno> <thisfile>
199390 ** break;
199391 */
199392/********** Begin reduce actions **********************************************/
199393 fts5YYMINORTYPE fts5yylhsminor;
199394 case 0: /* input ::= expr */
199395{ sqlite3Fts5ParseFinished(pParse, fts5yymsp[0].minor.fts5yy24); }
199396 break;
199397 case 1: /* colset ::= MINUS LCP colsetlist RCP */
199398{
199399 fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
199400}
199401 break;
199402 case 2: /* colset ::= LCP colsetlist RCP */
199403{ fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
199404 break;
199405 case 3: /* colset ::= STRING */
199406{
199407 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
199408}
199409 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
199410 break;
199411 case 4: /* colset ::= MINUS STRING */
199412{
199413 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
199414 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
199415}
199416 break;
199417 case 5: /* colsetlist ::= colsetlist STRING */
199418{
199419 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[0].minor.fts5yy0); }
199420 fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
199421 break;
199422 case 6: /* colsetlist ::= STRING */
199423{
199424 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
199425}
199426 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
199427 break;
199428 case 7: /* expr ::= expr AND expr */
199429{
199430 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
199431}
199432 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
199433 break;
199434 case 8: /* expr ::= expr OR expr */
199435{
199436 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
199437}
199438 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
199439 break;
199440 case 9: /* expr ::= expr NOT expr */
199441{
199442 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
199443}
199444 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
199445 break;
199446 case 10: /* expr ::= colset COLON LP expr RP */
199447{
199448 sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[-4].minor.fts5yy11);
199449 fts5yylhsminor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;
199450}
199451 fts5yymsp[-4].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
199452 break;
199453 case 11: /* expr ::= LP expr RP */
199454{fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
199455 break;
199456 case 12: /* expr ::= exprlist */
199457 case 13: /* exprlist ::= cnearset */ fts5yytestcase(fts5yyruleno==13);
199458{fts5yylhsminor.fts5yy24 = fts5yymsp[0].minor.fts5yy24;}
199459 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
199460 break;
199461 case 14: /* exprlist ::= exprlist cnearset */
199462{
199463 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24);
199464}
199465 fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
199466 break;
199467 case 15: /* cnearset ::= nearset */
199468{
199469 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46);
199470}
199471 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
199472 break;
199473 case 16: /* cnearset ::= colset COLON nearset */
199474{
199475 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46);
199476 sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy24, fts5yymsp[-2].minor.fts5yy11);
199477}
199478 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
199479 break;
199480 case 17: /* nearset ::= phrase */
199481{ fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); }
199482 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
199483 break;
199484 case 18: /* nearset ::= CARET phrase */
199485{
199486 sqlite3Fts5ParseSetCaret(fts5yymsp[0].minor.fts5yy53);
199487 fts5yymsp[-1].minor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
199488}
199489 break;
199490 case 19: /* nearset ::= STRING LP nearphrases neardist_opt RP */
199491{
199492 sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
199493 sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
199494 fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
199495}
199496 fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
199497 break;
199498 case 20: /* nearphrases ::= phrase */
199499{
199500 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
199501}
199502 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
199503 break;
199504 case 21: /* nearphrases ::= nearphrases phrase */
199505{
199506 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[0].minor.fts5yy53);
199507}
199508 fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
199509 break;
199510 case 22: /* neardist_opt ::= */
199511{ fts5yymsp[1].minor.fts5yy0.p = 0; fts5yymsp[1].minor.fts5yy0.n = 0; }
199512 break;
199513 case 23: /* neardist_opt ::= COMMA STRING */
199514{ fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
199515 break;
199516 case 24: /* phrase ::= phrase PLUS STRING star_opt */
199517{
199518 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy53, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
199519}
199520 fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
199521 break;
199522 case 25: /* phrase ::= STRING star_opt */
199523{
199524 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
199525}
199526 fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
199527 break;
199528 case 26: /* star_opt ::= STAR */
199529{ fts5yymsp[0].minor.fts5yy4 = 1; }
199530 break;
199531 case 27: /* star_opt ::= */
199532{ fts5yymsp[1].minor.fts5yy4 = 0; }
199533 break;
199534 default:
199535 break;
199536/********** End reduce actions ************************************************/
199537 };
199538 assert( fts5yyruleno<sizeof(fts5yyRuleInfo)/sizeof(fts5yyRuleInfo[0]) );
199539 fts5yygoto = fts5yyRuleInfo[fts5yyruleno].lhs;
199540 fts5yysize = fts5yyRuleInfo[fts5yyruleno].nrhs;
199541 fts5yyact = fts5yy_find_reduce_action(fts5yymsp[fts5yysize].stateno,(fts5YYCODETYPE)fts5yygoto);
199542
199543 /* There are no SHIFTREDUCE actions on nonterminals because the table
199544 ** generator has simplified them to pure REDUCE actions. */
199545 assert( !(fts5yyact>fts5YY_MAX_SHIFT && fts5yyact<=fts5YY_MAX_SHIFTREDUCE) );
199546
199547 /* It is not possible for a REDUCE to be followed by an error */
199548 assert( fts5yyact!=fts5YY_ERROR_ACTION );
199549
199550 fts5yymsp += fts5yysize+1;
199551 fts5yypParser->fts5yytos = fts5yymsp;
199552 fts5yymsp->stateno = (fts5YYACTIONTYPE)fts5yyact;
199553 fts5yymsp->major = (fts5YYCODETYPE)fts5yygoto;
199554 fts5yyTraceShift(fts5yypParser, fts5yyact, "... then shift");
199555 return fts5yyact;
199556}
199557
199558/*
199559** The following code executes when the parse fails
199560*/
199561#ifndef fts5YYNOERRORRECOVERY
199562static void fts5yy_parse_failed(
199563 fts5yyParser *fts5yypParser /* The parser */
199564){
199565 sqlite3Fts5ParserARG_FETCH
199566 sqlite3Fts5ParserCTX_FETCH
199567#ifndef NDEBUG
199568 if( fts5yyTraceFILE ){
199569 fprintf(fts5yyTraceFILE,"%sFail!\n",fts5yyTracePrompt);
199570 }
199571#endif
199572 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
199573 /* Here code is inserted which will be executed whenever the
199574 ** parser fails */
199575/************ Begin %parse_failure code ***************************************/
199576/************ End %parse_failure code *****************************************/
199577 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
199578 sqlite3Fts5ParserCTX_STORE
199579}
199580#endif /* fts5YYNOERRORRECOVERY */
199581
199582/*
199583** The following code executes when a syntax error first occurs.
199584*/
199585static void fts5yy_syntax_error(
199586 fts5yyParser *fts5yypParser, /* The parser */
199587 int fts5yymajor, /* The major type of the error token */
199588 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor /* The minor type of the error token */
199589){
199590 sqlite3Fts5ParserARG_FETCH
199591 sqlite3Fts5ParserCTX_FETCH
199592#define FTS5TOKEN fts5yyminor
199593/************ Begin %syntax_error code ****************************************/
199594
199595 UNUSED_PARAM(fts5yymajor); /* Silence a compiler warning */
199596 sqlite3Fts5ParseError(
199597 pParse, "fts5: syntax error near \"%.*s\"",FTS5TOKEN.n,FTS5TOKEN.p
199598 );
199599/************ End %syntax_error code ******************************************/
199600 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
199601 sqlite3Fts5ParserCTX_STORE
199602}
199603
199604/*
199605** The following is executed when the parser accepts
199606*/
199607static void fts5yy_accept(
199608 fts5yyParser *fts5yypParser /* The parser */
199609){
199610 sqlite3Fts5ParserARG_FETCH
199611 sqlite3Fts5ParserCTX_FETCH
199612#ifndef NDEBUG
199613 if( fts5yyTraceFILE ){
199614 fprintf(fts5yyTraceFILE,"%sAccept!\n",fts5yyTracePrompt);
199615 }
199616#endif
199617#ifndef fts5YYNOERRORRECOVERY
199618 fts5yypParser->fts5yyerrcnt = -1;
199619#endif
199620 assert( fts5yypParser->fts5yytos==fts5yypParser->fts5yystack );
199621 /* Here code is inserted which will be executed whenever the
199622 ** parser accepts */
199623/*********** Begin %parse_accept code *****************************************/
199624/*********** End %parse_accept code *******************************************/
199625 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
199626 sqlite3Fts5ParserCTX_STORE
199627}
199628
199629/* The main parser program.
199630** The first argument is a pointer to a structure obtained from
199631** "sqlite3Fts5ParserAlloc" which describes the current state of the parser.
199632** The second argument is the major token number. The third is
199633** the minor token. The fourth optional argument is whatever the
199634** user wants (and specified in the grammar) and is available for
199635** use by the action routines.
199636**
199637** Inputs:
199638** <ul>
199639** <li> A pointer to the parser (an opaque structure.)
199640** <li> The major token number.
199641** <li> The minor token number.
199642** <li> An option argument of a grammar-specified type.
199643** </ul>
199644**
199645** Outputs:
199646** None.
199647*/
199648static void sqlite3Fts5Parser(
199649 void *fts5yyp, /* The parser */
199650 int fts5yymajor, /* The major token code number */
199651 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor /* The value for the token */
199652 sqlite3Fts5ParserARG_PDECL /* Optional %extra_argument parameter */
199653){
199654 fts5YYMINORTYPE fts5yyminorunion;
199655 fts5YYACTIONTYPE fts5yyact; /* The parser action. */
199656#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
199657 int fts5yyendofinput; /* True if we are at the end of input */
199658#endif
199659#ifdef fts5YYERRORSYMBOL
199660 int fts5yyerrorhit = 0; /* True if fts5yymajor has invoked an error */
199661#endif
199662 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yyp; /* The parser */
199663 sqlite3Fts5ParserCTX_FETCH
199664 sqlite3Fts5ParserARG_STORE
199665
199666 assert( fts5yypParser->fts5yytos!=0 );
199667#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
199668 fts5yyendofinput = (fts5yymajor==0);
199669#endif
199670
199671 fts5yyact = fts5yypParser->fts5yytos->stateno;
199672#ifndef NDEBUG
199673 if( fts5yyTraceFILE ){
199674 if( fts5yyact < fts5YY_MIN_REDUCE ){
199675 fprintf(fts5yyTraceFILE,"%sInput '%s' in state %d\n",
199676 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact);
199677 }else{
199678 fprintf(fts5yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
199679 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact-fts5YY_MIN_REDUCE);
199680 }
199681 }
199682#endif
199683
199684 do{
199685 assert( fts5yyact==fts5yypParser->fts5yytos->stateno );
199686 fts5yyact = fts5yy_find_shift_action((fts5YYCODETYPE)fts5yymajor,fts5yyact);
199687 if( fts5yyact >= fts5YY_MIN_REDUCE ){
199688 fts5yyact = fts5yy_reduce(fts5yypParser,fts5yyact-fts5YY_MIN_REDUCE,fts5yymajor,
199689 fts5yyminor sqlite3Fts5ParserCTX_PARAM);
199690 }else if( fts5yyact <= fts5YY_MAX_SHIFTREDUCE ){
199691 fts5yy_shift(fts5yypParser,fts5yyact,(fts5YYCODETYPE)fts5yymajor,fts5yyminor);
199692#ifndef fts5YYNOERRORRECOVERY
199693 fts5yypParser->fts5yyerrcnt--;
199694#endif
199695 break;
199696 }else if( fts5yyact==fts5YY_ACCEPT_ACTION ){
199697 fts5yypParser->fts5yytos--;
199698 fts5yy_accept(fts5yypParser);
199699 return;
199700 }else{
199701 assert( fts5yyact == fts5YY_ERROR_ACTION );
199702 fts5yyminorunion.fts5yy0 = fts5yyminor;
199703#ifdef fts5YYERRORSYMBOL
199704 int fts5yymx;
199705#endif
199706#ifndef NDEBUG
199707 if( fts5yyTraceFILE ){
199708 fprintf(fts5yyTraceFILE,"%sSyntax Error!\n",fts5yyTracePrompt);
199709 }
199710#endif
199711#ifdef fts5YYERRORSYMBOL
199712 /* A syntax error has occurred.
199713 ** The response to an error depends upon whether or not the
199714 ** grammar defines an error token "ERROR".
199715 **
199716 ** This is what we do if the grammar does define ERROR:
199717 **
199718 ** * Call the %syntax_error function.
199719 **
199720 ** * Begin popping the stack until we enter a state where
199721 ** it is legal to shift the error symbol, then shift
199722 ** the error symbol.
199723 **
199724 ** * Set the error count to three.
199725 **
199726 ** * Begin accepting and shifting new tokens. No new error
199727 ** processing will occur until three tokens have been
199728 ** shifted successfully.
199729 **
199730 */
199731 if( fts5yypParser->fts5yyerrcnt<0 ){
199732 fts5yy_syntax_error(fts5yypParser,fts5yymajor,fts5yyminor);
199733 }
199734 fts5yymx = fts5yypParser->fts5yytos->major;
199735 if( fts5yymx==fts5YYERRORSYMBOL || fts5yyerrorhit ){
199736#ifndef NDEBUG
199737 if( fts5yyTraceFILE ){
199738 fprintf(fts5yyTraceFILE,"%sDiscard input token %s\n",
199739 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor]);
199740 }
199741#endif
199742 fts5yy_destructor(fts5yypParser, (fts5YYCODETYPE)fts5yymajor, &fts5yyminorunion);
199743 fts5yymajor = fts5YYNOCODE;
199744 }else{
199745 while( fts5yypParser->fts5yytos >= fts5yypParser->fts5yystack
199746 && fts5yymx != fts5YYERRORSYMBOL
199747 && (fts5yyact = fts5yy_find_reduce_action(
199748 fts5yypParser->fts5yytos->stateno,
199749 fts5YYERRORSYMBOL)) >= fts5YY_MIN_REDUCE
199750 ){
199751 fts5yy_pop_parser_stack(fts5yypParser);
199752 }
199753 if( fts5yypParser->fts5yytos < fts5yypParser->fts5yystack || fts5yymajor==0 ){
199754 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
199755 fts5yy_parse_failed(fts5yypParser);
199756#ifndef fts5YYNOERRORRECOVERY
199757 fts5yypParser->fts5yyerrcnt = -1;
199758#endif
199759 fts5yymajor = fts5YYNOCODE;
199760 }else if( fts5yymx!=fts5YYERRORSYMBOL ){
199761 fts5yy_shift(fts5yypParser,fts5yyact,fts5YYERRORSYMBOL,fts5yyminor);
199762 }
199763 }
199764 fts5yypParser->fts5yyerrcnt = 3;
199765 fts5yyerrorhit = 1;
199766 if( fts5yymajor==fts5YYNOCODE ) break;
199767 fts5yyact = fts5yypParser->fts5yytos->stateno;
199768#elif defined(fts5YYNOERRORRECOVERY)
199769 /* If the fts5YYNOERRORRECOVERY macro is defined, then do not attempt to
199770 ** do any kind of error recovery. Instead, simply invoke the syntax
199771 ** error routine and continue going as if nothing had happened.
199772 **
199773 ** Applications can set this macro (for example inside %include) if
199774 ** they intend to abandon the parse upon the first syntax error seen.
199775 */
199776 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
199777 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
199778 break;
199779#else /* fts5YYERRORSYMBOL is not defined */
199780 /* This is what we do if the grammar does not define ERROR:
199781 **
199782 ** * Report an error message, and throw away the input token.
199783 **
199784 ** * If the input token is $, then fail the parse.
199785 **
199786 ** As before, subsequent error messages are suppressed until
199787 ** three input tokens have been successfully shifted.
199788 */
199789 if( fts5yypParser->fts5yyerrcnt<=0 ){
199790 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
199791 }
199792 fts5yypParser->fts5yyerrcnt = 3;
199793 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
199794 if( fts5yyendofinput ){
199795 fts5yy_parse_failed(fts5yypParser);
199796#ifndef fts5YYNOERRORRECOVERY
199797 fts5yypParser->fts5yyerrcnt = -1;
199798#endif
199799 }
199800 break;
199801#endif
199802 }
199803 }while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack );
199804#ifndef NDEBUG
199805 if( fts5yyTraceFILE ){
199806 fts5yyStackEntry *i;
199807 char cDiv = '[';
199808 fprintf(fts5yyTraceFILE,"%sReturn. Stack=",fts5yyTracePrompt);
199809 for(i=&fts5yypParser->fts5yystack[1]; i<=fts5yypParser->fts5yytos; i++){
199810 fprintf(fts5yyTraceFILE,"%c%s", cDiv, fts5yyTokenName[i->major]);
199811 cDiv = ' ';
199812 }
199813 fprintf(fts5yyTraceFILE,"]\n");
199814 }
199815#endif
199816 return;
199817}
199818
199819/*
199820** Return the fallback token corresponding to canonical token iToken, or
199821** 0 if iToken has no fallback.
199822*/
199823static int sqlite3Fts5ParserFallback(int iToken){
199824#ifdef fts5YYFALLBACK
199825 if( iToken<(int)(sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])) ){
199826 return fts5yyFallback[iToken];
199827 }
199828#else
199829 (void)iToken;
199830#endif
199831 return 0;
199832}
199833
199834/*
199835** 2014 May 31
199836**
199837** The author disclaims copyright to this source code. In place of
199838** a legal notice, here is a blessing:
199839**
199840** May you do good and not evil.
199841** May you find forgiveness for yourself and forgive others.
199842** May you share freely, never taking more than you give.
199843**
199844******************************************************************************
199845*/
199846
199847
199848/* #include "fts5Int.h" */
199849#include <math.h> /* amalgamator: keep */
199850
199851/*
199852** Object used to iterate through all "coalesced phrase instances" in
199853** a single column of the current row. If the phrase instances in the
199854** column being considered do not overlap, this object simply iterates
199855** through them. Or, if they do overlap (share one or more tokens in
199856** common), each set of overlapping instances is treated as a single
199857** match. See documentation for the highlight() auxiliary function for
199858** details.
199859**
199860** Usage is:
199861**
199862** for(rc = fts5CInstIterNext(pApi, pFts, iCol, &iter);
199863** (rc==SQLITE_OK && 0==fts5CInstIterEof(&iter);
199864** rc = fts5CInstIterNext(&iter)
199865** ){
199866** printf("instance starts at %d, ends at %d\n", iter.iStart, iter.iEnd);
199867** }
199868**
199869*/
199870typedef struct CInstIter CInstIter;
199871struct CInstIter {
199872 const Fts5ExtensionApi *pApi; /* API offered by current FTS version */
199873 Fts5Context *pFts; /* First arg to pass to pApi functions */
199874 int iCol; /* Column to search */
199875 int iInst; /* Next phrase instance index */
199876 int nInst; /* Total number of phrase instances */
199877
199878 /* Output variables */
199879 int iStart; /* First token in coalesced phrase instance */
199880 int iEnd; /* Last token in coalesced phrase instance */
199881};
199882
199883/*
199884** Advance the iterator to the next coalesced phrase instance. Return
199885** an SQLite error code if an error occurs, or SQLITE_OK otherwise.
199886*/
199887static int fts5CInstIterNext(CInstIter *pIter){
199888 int rc = SQLITE_OK;
199889 pIter->iStart = -1;
199890 pIter->iEnd = -1;
199891
199892 while( rc==SQLITE_OK && pIter->iInst<pIter->nInst ){
199893 int ip; int ic; int io;
199894 rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
199895 if( rc==SQLITE_OK ){
199896 if( ic==pIter->iCol ){
199897 int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
199898 if( pIter->iStart<0 ){
199899 pIter->iStart = io;
199900 pIter->iEnd = iEnd;
199901 }else if( io<=pIter->iEnd ){
199902 if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
199903 }else{
199904 break;
199905 }
199906 }
199907 pIter->iInst++;
199908 }
199909 }
199910
199911 return rc;
199912}
199913
199914/*
199915** Initialize the iterator object indicated by the final parameter to
199916** iterate through coalesced phrase instances in column iCol.
199917*/
199918static int fts5CInstIterInit(
199919 const Fts5ExtensionApi *pApi,
199920 Fts5Context *pFts,
199921 int iCol,
199922 CInstIter *pIter
199923){
199924 int rc;
199925
199926 memset(pIter, 0, sizeof(CInstIter));
199927 pIter->pApi = pApi;
199928 pIter->pFts = pFts;
199929 pIter->iCol = iCol;
199930 rc = pApi->xInstCount(pFts, &pIter->nInst);
199931
199932 if( rc==SQLITE_OK ){
199933 rc = fts5CInstIterNext(pIter);
199934 }
199935
199936 return rc;
199937}
199938
199939
199940
199941/*************************************************************************
199942** Start of highlight() implementation.
199943*/
199944typedef struct HighlightContext HighlightContext;
199945struct HighlightContext {
199946 CInstIter iter; /* Coalesced Instance Iterator */
199947 int iPos; /* Current token offset in zIn[] */
199948 int iRangeStart; /* First token to include */
199949 int iRangeEnd; /* If non-zero, last token to include */
199950 const char *zOpen; /* Opening highlight */
199951 const char *zClose; /* Closing highlight */
199952 const char *zIn; /* Input text */
199953 int nIn; /* Size of input text in bytes */
199954 int iOff; /* Current offset within zIn[] */
199955 char *zOut; /* Output value */
199956};
199957
199958/*
199959** Append text to the HighlightContext output string - p->zOut. Argument
199960** z points to a buffer containing n bytes of text to append. If n is
199961** negative, everything up until the first '\0' is appended to the output.
199962**
199963** If *pRc is set to any value other than SQLITE_OK when this function is
199964** called, it is a no-op. If an error (i.e. an OOM condition) is encountered,
199965** *pRc is set to an error code before returning.
199966*/
199967static void fts5HighlightAppend(
199968 int *pRc,
199969 HighlightContext *p,
199970 const char *z, int n
199971){
199972 if( *pRc==SQLITE_OK ){
199973 if( n<0 ) n = (int)strlen(z);
199974 p->zOut = sqlite3_mprintf("%z%.*s", p->zOut, n, z);
199975 if( p->zOut==0 ) *pRc = SQLITE_NOMEM;
199976 }
199977}
199978
199979/*
199980** Tokenizer callback used by implementation of highlight() function.
199981*/
199982static int fts5HighlightCb(
199983 void *pContext, /* Pointer to HighlightContext object */
199984 int tflags, /* Mask of FTS5_TOKEN_* flags */
199985 const char *pToken, /* Buffer containing token */
199986 int nToken, /* Size of token in bytes */
199987 int iStartOff, /* Start offset of token */
199988 int iEndOff /* End offset of token */
199989){
199990 HighlightContext *p = (HighlightContext*)pContext;
199991 int rc = SQLITE_OK;
199992 int iPos;
199993
199994 UNUSED_PARAM2(pToken, nToken);
199995
199996 if( tflags & FTS5_TOKEN_COLOCATED ) return SQLITE_OK;
199997 iPos = p->iPos++;
199998
199999 if( p->iRangeEnd>0 ){
200000 if( iPos<p->iRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK;
200001 if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff;
200002 }
200003
200004 if( iPos==p->iter.iStart ){
200005 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iStartOff - p->iOff);
200006 fts5HighlightAppend(&rc, p, p->zOpen, -1);
200007 p->iOff = iStartOff;
200008 }
200009
200010 if( iPos==p->iter.iEnd ){
200011 if( p->iRangeEnd && p->iter.iStart<p->iRangeStart ){
200012 fts5HighlightAppend(&rc, p, p->zOpen, -1);
200013 }
200014 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
200015 fts5HighlightAppend(&rc, p, p->zClose, -1);
200016 p->iOff = iEndOff;
200017 if( rc==SQLITE_OK ){
200018 rc = fts5CInstIterNext(&p->iter);
200019 }
200020 }
200021
200022 if( p->iRangeEnd>0 && iPos==p->iRangeEnd ){
200023 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
200024 p->iOff = iEndOff;
200025 if( iPos>=p->iter.iStart && iPos<p->iter.iEnd ){
200026 fts5HighlightAppend(&rc, p, p->zClose, -1);
200027 }
200028 }
200029
200030 return rc;
200031}
200032
200033/*
200034** Implementation of highlight() function.
200035*/
200036static void fts5HighlightFunction(
200037 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
200038 Fts5Context *pFts, /* First arg to pass to pApi functions */
200039 sqlite3_context *pCtx, /* Context for returning result/error */
200040 int nVal, /* Number of values in apVal[] array */
200041 sqlite3_value **apVal /* Array of trailing arguments */
200042){
200043 HighlightContext ctx;
200044 int rc;
200045 int iCol;
200046
200047 if( nVal!=3 ){
200048 const char *zErr = "wrong number of arguments to function highlight()";
200049 sqlite3_result_error(pCtx, zErr, -1);
200050 return;
200051 }
200052
200053 iCol = sqlite3_value_int(apVal[0]);
200054 memset(&ctx, 0, sizeof(HighlightContext));
200055 ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
200056 ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
200057 rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
200058
200059 if( ctx.zIn ){
200060 if( rc==SQLITE_OK ){
200061 rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
200062 }
200063
200064 if( rc==SQLITE_OK ){
200065 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
200066 }
200067 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
200068
200069 if( rc==SQLITE_OK ){
200070 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
200071 }
200072 sqlite3_free(ctx.zOut);
200073 }
200074 if( rc!=SQLITE_OK ){
200075 sqlite3_result_error_code(pCtx, rc);
200076 }
200077}
200078/*
200079** End of highlight() implementation.
200080**************************************************************************/
200081
200082/*
200083** Context object passed to the fts5SentenceFinderCb() function.
200084*/
200085typedef struct Fts5SFinder Fts5SFinder;
200086struct Fts5SFinder {
200087 int iPos; /* Current token position */
200088 int nFirstAlloc; /* Allocated size of aFirst[] */
200089 int nFirst; /* Number of entries in aFirst[] */
200090 int *aFirst; /* Array of first token in each sentence */
200091 const char *zDoc; /* Document being tokenized */
200092};
200093
200094/*
200095** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if
200096** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an
200097** error occurs.
200098*/
200099static int fts5SentenceFinderAdd(Fts5SFinder *p, int iAdd){
200100 if( p->nFirstAlloc==p->nFirst ){
200101 int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64;
200102 int *aNew;
200103
200104 aNew = (int*)sqlite3_realloc(p->aFirst, nNew*sizeof(int));
200105 if( aNew==0 ) return SQLITE_NOMEM;
200106 p->aFirst = aNew;
200107 p->nFirstAlloc = nNew;
200108 }
200109 p->aFirst[p->nFirst++] = iAdd;
200110 return SQLITE_OK;
200111}
200112
200113/*
200114** This function is an xTokenize() callback used by the auxiliary snippet()
200115** function. Its job is to identify tokens that are the first in a sentence.
200116** For each such token, an entry is added to the SFinder.aFirst[] array.
200117*/
200118static int fts5SentenceFinderCb(
200119 void *pContext, /* Pointer to HighlightContext object */
200120 int tflags, /* Mask of FTS5_TOKEN_* flags */
200121 const char *pToken, /* Buffer containing token */
200122 int nToken, /* Size of token in bytes */
200123 int iStartOff, /* Start offset of token */
200124 int iEndOff /* End offset of token */
200125){
200126 int rc = SQLITE_OK;
200127
200128 UNUSED_PARAM2(pToken, nToken);
200129 UNUSED_PARAM(iEndOff);
200130
200131 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
200132 Fts5SFinder *p = (Fts5SFinder*)pContext;
200133 if( p->iPos>0 ){
200134 int i;
200135 char c = 0;
200136 for(i=iStartOff-1; i>=0; i--){
200137 c = p->zDoc[i];
200138 if( c!=' ' && c!='\t' && c!='\n' && c!='\r' ) break;
200139 }
200140 if( i!=iStartOff-1 && (c=='.' || c==':') ){
200141 rc = fts5SentenceFinderAdd(p, p->iPos);
200142 }
200143 }else{
200144 rc = fts5SentenceFinderAdd(p, 0);
200145 }
200146 p->iPos++;
200147 }
200148 return rc;
200149}
200150
200151static int fts5SnippetScore(
200152 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
200153 Fts5Context *pFts, /* First arg to pass to pApi functions */
200154 int nDocsize, /* Size of column in tokens */
200155 unsigned char *aSeen, /* Array with one element per query phrase */
200156 int iCol, /* Column to score */
200157 int iPos, /* Starting offset to score */
200158 int nToken, /* Max tokens per snippet */
200159 int *pnScore, /* OUT: Score */
200160 int *piPos /* OUT: Adjusted offset */
200161){
200162 int rc;
200163 int i;
200164 int ip = 0;
200165 int ic = 0;
200166 int iOff = 0;
200167 int iFirst = -1;
200168 int nInst;
200169 int nScore = 0;
200170 int iLast = 0;
200171
200172 rc = pApi->xInstCount(pFts, &nInst);
200173 for(i=0; i<nInst && rc==SQLITE_OK; i++){
200174 rc = pApi->xInst(pFts, i, &ip, &ic, &iOff);
200175 if( rc==SQLITE_OK && ic==iCol && iOff>=iPos && iOff<(iPos+nToken) ){
200176 nScore += (aSeen[ip] ? 1 : 1000);
200177 aSeen[ip] = 1;
200178 if( iFirst<0 ) iFirst = iOff;
200179 iLast = iOff + pApi->xPhraseSize(pFts, ip);
200180 }
200181 }
200182
200183 *pnScore = nScore;
200184 if( piPos ){
200185 int iAdj = iFirst - (nToken - (iLast-iFirst)) / 2;
200186 if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
200187 if( iAdj<0 ) iAdj = 0;
200188 *piPos = iAdj;
200189 }
200190
200191 return rc;
200192}
200193
200194/*
200195** Return the value in pVal interpreted as utf-8 text. Except, if pVal
200196** contains a NULL value, return a pointer to a static string zero
200197** bytes in length instead of a NULL pointer.
200198*/
200199static const char *fts5ValueToText(sqlite3_value *pVal){
200200 const char *zRet = (const char*)sqlite3_value_text(pVal);
200201 return zRet ? zRet : "";
200202}
200203
200204/*
200205** Implementation of snippet() function.
200206*/
200207static void fts5SnippetFunction(
200208 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
200209 Fts5Context *pFts, /* First arg to pass to pApi functions */
200210 sqlite3_context *pCtx, /* Context for returning result/error */
200211 int nVal, /* Number of values in apVal[] array */
200212 sqlite3_value **apVal /* Array of trailing arguments */
200213){
200214 HighlightContext ctx;
200215 int rc = SQLITE_OK; /* Return code */
200216 int iCol; /* 1st argument to snippet() */
200217 const char *zEllips; /* 4th argument to snippet() */
200218 int nToken; /* 5th argument to snippet() */
200219 int nInst = 0; /* Number of instance matches this row */
200220 int i; /* Used to iterate through instances */
200221 int nPhrase; /* Number of phrases in query */
200222 unsigned char *aSeen; /* Array of "seen instance" flags */
200223 int iBestCol; /* Column containing best snippet */
200224 int iBestStart = 0; /* First token of best snippet */
200225 int nBestScore = 0; /* Score of best snippet */
200226 int nColSize = 0; /* Total size of iBestCol in tokens */
200227 Fts5SFinder sFinder; /* Used to find the beginnings of sentences */
200228 int nCol;
200229
200230 if( nVal!=5 ){
200231 const char *zErr = "wrong number of arguments to function snippet()";
200232 sqlite3_result_error(pCtx, zErr, -1);
200233 return;
200234 }
200235
200236 nCol = pApi->xColumnCount(pFts);
200237 memset(&ctx, 0, sizeof(HighlightContext));
200238 iCol = sqlite3_value_int(apVal[0]);
200239 ctx.zOpen = fts5ValueToText(apVal[1]);
200240 ctx.zClose = fts5ValueToText(apVal[2]);
200241 zEllips = fts5ValueToText(apVal[3]);
200242 nToken = sqlite3_value_int(apVal[4]);
200243
200244 iBestCol = (iCol>=0 ? iCol : 0);
200245 nPhrase = pApi->xPhraseCount(pFts);
200246 aSeen = sqlite3_malloc(nPhrase);
200247 if( aSeen==0 ){
200248 rc = SQLITE_NOMEM;
200249 }
200250 if( rc==SQLITE_OK ){
200251 rc = pApi->xInstCount(pFts, &nInst);
200252 }
200253
200254 memset(&sFinder, 0, sizeof(Fts5SFinder));
200255 for(i=0; i<nCol; i++){
200256 if( iCol<0 || iCol==i ){
200257 int nDoc;
200258 int nDocsize;
200259 int ii;
200260 sFinder.iPos = 0;
200261 sFinder.nFirst = 0;
200262 rc = pApi->xColumnText(pFts, i, &sFinder.zDoc, &nDoc);
200263 if( rc!=SQLITE_OK ) break;
200264 rc = pApi->xTokenize(pFts,
200265 sFinder.zDoc, nDoc, (void*)&sFinder,fts5SentenceFinderCb
200266 );
200267 if( rc!=SQLITE_OK ) break;
200268 rc = pApi->xColumnSize(pFts, i, &nDocsize);
200269 if( rc!=SQLITE_OK ) break;
200270
200271 for(ii=0; rc==SQLITE_OK && ii<nInst; ii++){
200272 int ip, ic, io;
200273 int iAdj;
200274 int nScore;
200275 int jj;
200276
200277 rc = pApi->xInst(pFts, ii, &ip, &ic, &io);
200278 if( ic!=i || rc!=SQLITE_OK ) continue;
200279 memset(aSeen, 0, nPhrase);
200280 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
200281 io, nToken, &nScore, &iAdj
200282 );
200283 if( rc==SQLITE_OK && nScore>nBestScore ){
200284 nBestScore = nScore;
200285 iBestCol = i;
200286 iBestStart = iAdj;
200287 nColSize = nDocsize;
200288 }
200289
200290 if( rc==SQLITE_OK && sFinder.nFirst && nDocsize>nToken ){
200291 for(jj=0; jj<(sFinder.nFirst-1); jj++){
200292 if( sFinder.aFirst[jj+1]>io ) break;
200293 }
200294
200295 if( sFinder.aFirst[jj]<io ){
200296 memset(aSeen, 0, nPhrase);
200297 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
200298 sFinder.aFirst[jj], nToken, &nScore, 0
200299 );
200300
200301 nScore += (sFinder.aFirst[jj]==0 ? 120 : 100);
200302 if( rc==SQLITE_OK && nScore>nBestScore ){
200303 nBestScore = nScore;
200304 iBestCol = i;
200305 iBestStart = sFinder.aFirst[jj];
200306 nColSize = nDocsize;
200307 }
200308 }
200309 }
200310 }
200311 }
200312 }
200313
200314 if( rc==SQLITE_OK ){
200315 rc = pApi->xColumnText(pFts, iBestCol, &ctx.zIn, &ctx.nIn);
200316 }
200317 if( rc==SQLITE_OK && nColSize==0 ){
200318 rc = pApi->xColumnSize(pFts, iBestCol, &nColSize);
200319 }
200320 if( ctx.zIn ){
200321 if( rc==SQLITE_OK ){
200322 rc = fts5CInstIterInit(pApi, pFts, iBestCol, &ctx.iter);
200323 }
200324
200325 ctx.iRangeStart = iBestStart;
200326 ctx.iRangeEnd = iBestStart + nToken - 1;
200327
200328 if( iBestStart>0 ){
200329 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
200330 }
200331
200332 /* Advance iterator ctx.iter so that it points to the first coalesced
200333 ** phrase instance at or following position iBestStart. */
200334 while( ctx.iter.iStart>=0 && ctx.iter.iStart<iBestStart && rc==SQLITE_OK ){
200335 rc = fts5CInstIterNext(&ctx.iter);
200336 }
200337
200338 if( rc==SQLITE_OK ){
200339 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
200340 }
200341 if( ctx.iRangeEnd>=(nColSize-1) ){
200342 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
200343 }else{
200344 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
200345 }
200346 }
200347 if( rc==SQLITE_OK ){
200348 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
200349 }else{
200350 sqlite3_result_error_code(pCtx, rc);
200351 }
200352 sqlite3_free(ctx.zOut);
200353 sqlite3_free(aSeen);
200354 sqlite3_free(sFinder.aFirst);
200355}
200356
200357/************************************************************************/
200358
200359/*
200360** The first time the bm25() function is called for a query, an instance
200361** of the following structure is allocated and populated.
200362*/
200363typedef struct Fts5Bm25Data Fts5Bm25Data;
200364struct Fts5Bm25Data {
200365 int nPhrase; /* Number of phrases in query */
200366 double avgdl; /* Average number of tokens in each row */
200367 double *aIDF; /* IDF for each phrase */
200368 double *aFreq; /* Array used to calculate phrase freq. */
200369};
200370
200371/*
200372** Callback used by fts5Bm25GetData() to count the number of rows in the
200373** table matched by each individual phrase within the query.
200374*/
200375static int fts5CountCb(
200376 const Fts5ExtensionApi *pApi,
200377 Fts5Context *pFts,
200378 void *pUserData /* Pointer to sqlite3_int64 variable */
200379){
200380 sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
200381 UNUSED_PARAM2(pApi, pFts);
200382 (*pn)++;
200383 return SQLITE_OK;
200384}
200385
200386/*
200387** Set *ppData to point to the Fts5Bm25Data object for the current query.
200388** If the object has not already been allocated, allocate and populate it
200389** now.
200390*/
200391static int fts5Bm25GetData(
200392 const Fts5ExtensionApi *pApi,
200393 Fts5Context *pFts,
200394 Fts5Bm25Data **ppData /* OUT: bm25-data object for this query */
200395){
200396 int rc = SQLITE_OK; /* Return code */
200397 Fts5Bm25Data *p; /* Object to return */
200398
200399 p = pApi->xGetAuxdata(pFts, 0);
200400 if( p==0 ){
200401 int nPhrase; /* Number of phrases in query */
200402 sqlite3_int64 nRow = 0; /* Number of rows in table */
200403 sqlite3_int64 nToken = 0; /* Number of tokens in table */
200404 int nByte; /* Bytes of space to allocate */
200405 int i;
200406
200407 /* Allocate the Fts5Bm25Data object */
200408 nPhrase = pApi->xPhraseCount(pFts);
200409 nByte = sizeof(Fts5Bm25Data) + nPhrase*2*sizeof(double);
200410 p = (Fts5Bm25Data*)sqlite3_malloc(nByte);
200411 if( p==0 ){
200412 rc = SQLITE_NOMEM;
200413 }else{
200414 memset(p, 0, nByte);
200415 p->nPhrase = nPhrase;
200416 p->aIDF = (double*)&p[1];
200417 p->aFreq = &p->aIDF[nPhrase];
200418 }
200419
200420 /* Calculate the average document length for this FTS5 table */
200421 if( rc==SQLITE_OK ) rc = pApi->xRowCount(pFts, &nRow);
200422 if( rc==SQLITE_OK ) rc = pApi->xColumnTotalSize(pFts, -1, &nToken);
200423 if( rc==SQLITE_OK ) p->avgdl = (double)nToken / (double)nRow;
200424
200425 /* Calculate an IDF for each phrase in the query */
200426 for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
200427 sqlite3_int64 nHit = 0;
200428 rc = pApi->xQueryPhrase(pFts, i, (void*)&nHit, fts5CountCb);
200429 if( rc==SQLITE_OK ){
200430 /* Calculate the IDF (Inverse Document Frequency) for phrase i.
200431 ** This is done using the standard BM25 formula as found on wikipedia:
200432 **
200433 ** IDF = log( (N - nHit + 0.5) / (nHit + 0.5) )
200434 **
200435 ** where "N" is the total number of documents in the set and nHit
200436 ** is the number that contain at least one instance of the phrase
200437 ** under consideration.
200438 **
200439 ** The problem with this is that if (N < 2*nHit), the IDF is
200440 ** negative. Which is undesirable. So the mimimum allowable IDF is
200441 ** (1e-6) - roughly the same as a term that appears in just over
200442 ** half of set of 5,000,000 documents. */
200443 double idf = log( (nRow - nHit + 0.5) / (nHit + 0.5) );
200444 if( idf<=0.0 ) idf = 1e-6;
200445 p->aIDF[i] = idf;
200446 }
200447 }
200448
200449 if( rc!=SQLITE_OK ){
200450 sqlite3_free(p);
200451 }else{
200452 rc = pApi->xSetAuxdata(pFts, p, sqlite3_free);
200453 }
200454 if( rc!=SQLITE_OK ) p = 0;
200455 }
200456 *ppData = p;
200457 return rc;
200458}
200459
200460/*
200461** Implementation of bm25() function.
200462*/
200463static void fts5Bm25Function(
200464 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
200465 Fts5Context *pFts, /* First arg to pass to pApi functions */
200466 sqlite3_context *pCtx, /* Context for returning result/error */
200467 int nVal, /* Number of values in apVal[] array */
200468 sqlite3_value **apVal /* Array of trailing arguments */
200469){
200470 const double k1 = 1.2; /* Constant "k1" from BM25 formula */
200471 const double b = 0.75; /* Constant "b" from BM25 formula */
200472 int rc = SQLITE_OK; /* Error code */
200473 double score = 0.0; /* SQL function return value */
200474 Fts5Bm25Data *pData; /* Values allocated/calculated once only */
200475 int i; /* Iterator variable */
200476 int nInst = 0; /* Value returned by xInstCount() */
200477 double D = 0.0; /* Total number of tokens in row */
200478 double *aFreq = 0; /* Array of phrase freq. for current row */
200479
200480 /* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation)
200481 ** for each phrase in the query for the current row. */
200482 rc = fts5Bm25GetData(pApi, pFts, &pData);
200483 if( rc==SQLITE_OK ){
200484 aFreq = pData->aFreq;
200485 memset(aFreq, 0, sizeof(double) * pData->nPhrase);
200486 rc = pApi->xInstCount(pFts, &nInst);
200487 }
200488 for(i=0; rc==SQLITE_OK && i<nInst; i++){
200489 int ip; int ic; int io;
200490 rc = pApi->xInst(pFts, i, &ip, &ic, &io);
200491 if( rc==SQLITE_OK ){
200492 double w = (nVal > ic) ? sqlite3_value_double(apVal[ic]) : 1.0;
200493 aFreq[ip] += w;
200494 }
200495 }
200496
200497 /* Figure out the total size of the current row in tokens. */
200498 if( rc==SQLITE_OK ){
200499 int nTok;
200500 rc = pApi->xColumnSize(pFts, -1, &nTok);
200501 D = (double)nTok;
200502 }
200503
200504 /* Determine the BM25 score for the current row. */
200505 for(i=0; rc==SQLITE_OK && i<pData->nPhrase; i++){
200506 score += pData->aIDF[i] * (
200507 ( aFreq[i] * (k1 + 1.0) ) /
200508 ( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) )
200509 );
200510 }
200511
200512 /* If no error has occurred, return the calculated score. Otherwise,
200513 ** throw an SQL exception. */
200514 if( rc==SQLITE_OK ){
200515 sqlite3_result_double(pCtx, -1.0 * score);
200516 }else{
200517 sqlite3_result_error_code(pCtx, rc);
200518 }
200519}
200520
200521static int sqlite3Fts5AuxInit(fts5_api *pApi){
200522 struct Builtin {
200523 const char *zFunc; /* Function name (nul-terminated) */
200524 void *pUserData; /* User-data pointer */
200525 fts5_extension_function xFunc;/* Callback function */
200526 void (*xDestroy)(void*); /* Destructor function */
200527 } aBuiltin [] = {
200528 { "snippet", 0, fts5SnippetFunction, 0 },
200529 { "highlight", 0, fts5HighlightFunction, 0 },
200530 { "bm25", 0, fts5Bm25Function, 0 },
200531 };
200532 int rc = SQLITE_OK; /* Return code */
200533 int i; /* To iterate through builtin functions */
200534
200535 for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
200536 rc = pApi->xCreateFunction(pApi,
200537 aBuiltin[i].zFunc,
200538 aBuiltin[i].pUserData,
200539 aBuiltin[i].xFunc,
200540 aBuiltin[i].xDestroy
200541 );
200542 }
200543
200544 return rc;
200545}
200546
200547
200548
200549/*
200550** 2014 May 31
200551**
200552** The author disclaims copyright to this source code. In place of
200553** a legal notice, here is a blessing:
200554**
200555** May you do good and not evil.
200556** May you find forgiveness for yourself and forgive others.
200557** May you share freely, never taking more than you give.
200558**
200559******************************************************************************
200560*/
200561
200562
200563
200564/* #include "fts5Int.h" */
200565
200566static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
200567 if( (u32)pBuf->nSpace<nByte ){
200568 u32 nNew = pBuf->nSpace ? pBuf->nSpace : 64;
200569 u8 *pNew;
200570 while( nNew<nByte ){
200571 nNew = nNew * 2;
200572 }
200573 pNew = sqlite3_realloc(pBuf->p, nNew);
200574 if( pNew==0 ){
200575 *pRc = SQLITE_NOMEM;
200576 return 1;
200577 }else{
200578 pBuf->nSpace = nNew;
200579 pBuf->p = pNew;
200580 }
200581 }
200582 return 0;
200583}
200584
200585
200586/*
200587** Encode value iVal as an SQLite varint and append it to the buffer object
200588** pBuf. If an OOM error occurs, set the error code in p.
200589*/
200590static void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){
200591 if( fts5BufferGrow(pRc, pBuf, 9) ) return;
200592 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iVal);
200593}
200594
200595static void sqlite3Fts5Put32(u8 *aBuf, int iVal){
200596 aBuf[0] = (iVal>>24) & 0x00FF;
200597 aBuf[1] = (iVal>>16) & 0x00FF;
200598 aBuf[2] = (iVal>> 8) & 0x00FF;
200599 aBuf[3] = (iVal>> 0) & 0x00FF;
200600}
200601
200602static int sqlite3Fts5Get32(const u8 *aBuf){
200603 return (aBuf[0] << 24) + (aBuf[1] << 16) + (aBuf[2] << 8) + aBuf[3];
200604}
200605
200606/*
200607** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set
200608** the error code in p. If an error has already occurred when this function
200609** is called, it is a no-op.
200610*/
200611static void sqlite3Fts5BufferAppendBlob(
200612 int *pRc,
200613 Fts5Buffer *pBuf,
200614 u32 nData,
200615 const u8 *pData
200616){
200617 assert_nc( *pRc || nData>=0 );
200618 if( nData ){
200619 if( fts5BufferGrow(pRc, pBuf, nData) ) return;
200620 memcpy(&pBuf->p[pBuf->n], pData, nData);
200621 pBuf->n += nData;
200622 }
200623}
200624
200625/*
200626** Append the nul-terminated string zStr to the buffer pBuf. This function
200627** ensures that the byte following the buffer data is set to 0x00, even
200628** though this byte is not included in the pBuf->n count.
200629*/
200630static void sqlite3Fts5BufferAppendString(
200631 int *pRc,
200632 Fts5Buffer *pBuf,
200633 const char *zStr
200634){
200635 int nStr = (int)strlen(zStr);
200636 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nStr+1, (const u8*)zStr);
200637 pBuf->n--;
200638}
200639
200640/*
200641** Argument zFmt is a printf() style format string. This function performs
200642** the printf() style processing, then appends the results to buffer pBuf.
200643**
200644** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte
200645** following the buffer data is set to 0x00, even though this byte is not
200646** included in the pBuf->n count.
200647*/
200648static void sqlite3Fts5BufferAppendPrintf(
200649 int *pRc,
200650 Fts5Buffer *pBuf,
200651 char *zFmt, ...
200652){
200653 if( *pRc==SQLITE_OK ){
200654 char *zTmp;
200655 va_list ap;
200656 va_start(ap, zFmt);
200657 zTmp = sqlite3_vmprintf(zFmt, ap);
200658 va_end(ap);
200659
200660 if( zTmp==0 ){
200661 *pRc = SQLITE_NOMEM;
200662 }else{
200663 sqlite3Fts5BufferAppendString(pRc, pBuf, zTmp);
200664 sqlite3_free(zTmp);
200665 }
200666 }
200667}
200668
200669static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){
200670 char *zRet = 0;
200671 if( *pRc==SQLITE_OK ){
200672 va_list ap;
200673 va_start(ap, zFmt);
200674 zRet = sqlite3_vmprintf(zFmt, ap);
200675 va_end(ap);
200676 if( zRet==0 ){
200677 *pRc = SQLITE_NOMEM;
200678 }
200679 }
200680 return zRet;
200681}
200682
200683
200684/*
200685** Free any buffer allocated by pBuf. Zero the structure before returning.
200686*/
200687static void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){
200688 sqlite3_free(pBuf->p);
200689 memset(pBuf, 0, sizeof(Fts5Buffer));
200690}
200691
200692/*
200693** Zero the contents of the buffer object. But do not free the associated
200694** memory allocation.
200695*/
200696static void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){
200697 pBuf->n = 0;
200698}
200699
200700/*
200701** Set the buffer to contain nData/pData. If an OOM error occurs, leave an
200702** the error code in p. If an error has already occurred when this function
200703** is called, it is a no-op.
200704*/
200705static void sqlite3Fts5BufferSet(
200706 int *pRc,
200707 Fts5Buffer *pBuf,
200708 int nData,
200709 const u8 *pData
200710){
200711 pBuf->n = 0;
200712 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData);
200713}
200714
200715static int sqlite3Fts5PoslistNext64(
200716 const u8 *a, int n, /* Buffer containing poslist */
200717 int *pi, /* IN/OUT: Offset within a[] */
200718 i64 *piOff /* IN/OUT: Current offset */
200719){
200720 int i = *pi;
200721 if( i>=n ){
200722 /* EOF */
200723 *piOff = -1;
200724 return 1;
200725 }else{
200726 i64 iOff = *piOff;
200727 int iVal;
200728 fts5FastGetVarint32(a, i, iVal);
200729 if( iVal==1 ){
200730 fts5FastGetVarint32(a, i, iVal);
200731 iOff = ((i64)iVal) << 32;
200732 fts5FastGetVarint32(a, i, iVal);
200733 }
200734 *piOff = iOff + (iVal-2);
200735 *pi = i;
200736 return 0;
200737 }
200738}
200739
200740
200741/*
200742** Advance the iterator object passed as the only argument. Return true
200743** if the iterator reaches EOF, or false otherwise.
200744*/
200745static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){
200746 if( sqlite3Fts5PoslistNext64(pIter->a, pIter->n, &pIter->i, &pIter->iPos) ){
200747 pIter->bEof = 1;
200748 }
200749 return pIter->bEof;
200750}
200751
200752static int sqlite3Fts5PoslistReaderInit(
200753 const u8 *a, int n, /* Poslist buffer to iterate through */
200754 Fts5PoslistReader *pIter /* Iterator object to initialize */
200755){
200756 memset(pIter, 0, sizeof(*pIter));
200757 pIter->a = a;
200758 pIter->n = n;
200759 sqlite3Fts5PoslistReaderNext(pIter);
200760 return pIter->bEof;
200761}
200762
200763/*
200764** Append position iPos to the position list being accumulated in buffer
200765** pBuf, which must be already be large enough to hold the new data.
200766** The previous position written to this list is *piPrev. *piPrev is set
200767** to iPos before returning.
200768*/
200769static void sqlite3Fts5PoslistSafeAppend(
200770 Fts5Buffer *pBuf,
200771 i64 *piPrev,
200772 i64 iPos
200773){
200774 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
200775 if( (iPos & colmask) != (*piPrev & colmask) ){
200776 pBuf->p[pBuf->n++] = 1;
200777 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32));
200778 *piPrev = (iPos & colmask);
200779 }
200780 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2);
200781 *piPrev = iPos;
200782}
200783
200784static int sqlite3Fts5PoslistWriterAppend(
200785 Fts5Buffer *pBuf,
200786 Fts5PoslistWriter *pWriter,
200787 i64 iPos
200788){
200789 int rc = 0; /* Initialized only to suppress erroneous warning from Clang */
200790 if( fts5BufferGrow(&rc, pBuf, 5+5+5) ) return rc;
200791 sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos);
200792 return SQLITE_OK;
200793}
200794
200795static void *sqlite3Fts5MallocZero(int *pRc, int nByte){
200796 void *pRet = 0;
200797 if( *pRc==SQLITE_OK ){
200798 pRet = sqlite3_malloc(nByte);
200799 if( pRet==0 ){
200800 if( nByte>0 ) *pRc = SQLITE_NOMEM;
200801 }else{
200802 memset(pRet, 0, nByte);
200803 }
200804 }
200805 return pRet;
200806}
200807
200808/*
200809** Return a nul-terminated copy of the string indicated by pIn. If nIn
200810** is non-negative, then it is the length of the string in bytes. Otherwise,
200811** the length of the string is determined using strlen().
200812**
200813** It is the responsibility of the caller to eventually free the returned
200814** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned.
200815*/
200816static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){
200817 char *zRet = 0;
200818 if( *pRc==SQLITE_OK ){
200819 if( nIn<0 ){
200820 nIn = (int)strlen(pIn);
200821 }
200822 zRet = (char*)sqlite3_malloc(nIn+1);
200823 if( zRet ){
200824 memcpy(zRet, pIn, nIn);
200825 zRet[nIn] = '\0';
200826 }else{
200827 *pRc = SQLITE_NOMEM;
200828 }
200829 }
200830 return zRet;
200831}
200832
200833
200834/*
200835** Return true if character 't' may be part of an FTS5 bareword, or false
200836** otherwise. Characters that may be part of barewords:
200837**
200838** * All non-ASCII characters,
200839** * The 52 upper and lower case ASCII characters, and
200840** * The 10 integer ASCII characters.
200841** * The underscore character "_" (0x5F).
200842** * The unicode "subsitute" character (0x1A).
200843*/
200844static int sqlite3Fts5IsBareword(char t){
200845 u8 aBareword[128] = {
200846 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 .. 0x0F */
200847 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 0x10 .. 0x1F */
200848 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 .. 0x2F */
200849 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30 .. 0x3F */
200850 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 .. 0x4F */
200851 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50 .. 0x5F */
200852 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 .. 0x6F */
200853 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 .. 0x7F */
200854 };
200855
200856 return (t & 0x80) || aBareword[(int)t];
200857}
200858
200859
200860/*************************************************************************
200861*/
200862typedef struct Fts5TermsetEntry Fts5TermsetEntry;
200863struct Fts5TermsetEntry {
200864 char *pTerm;
200865 int nTerm;
200866 int iIdx; /* Index (main or aPrefix[] entry) */
200867 Fts5TermsetEntry *pNext;
200868};
200869
200870struct Fts5Termset {
200871 Fts5TermsetEntry *apHash[512];
200872};
200873
200874static int sqlite3Fts5TermsetNew(Fts5Termset **pp){
200875 int rc = SQLITE_OK;
200876 *pp = sqlite3Fts5MallocZero(&rc, sizeof(Fts5Termset));
200877 return rc;
200878}
200879
200880static int sqlite3Fts5TermsetAdd(
200881 Fts5Termset *p,
200882 int iIdx,
200883 const char *pTerm, int nTerm,
200884 int *pbPresent
200885){
200886 int rc = SQLITE_OK;
200887 *pbPresent = 0;
200888 if( p ){
200889 int i;
200890 u32 hash = 13;
200891 Fts5TermsetEntry *pEntry;
200892
200893 /* Calculate a hash value for this term. This is the same hash checksum
200894 ** used by the fts5_hash.c module. This is not important for correct
200895 ** operation of the module, but is necessary to ensure that some tests
200896 ** designed to produce hash table collisions really do work. */
200897 for(i=nTerm-1; i>=0; i--){
200898 hash = (hash << 3) ^ hash ^ pTerm[i];
200899 }
200900 hash = (hash << 3) ^ hash ^ iIdx;
200901 hash = hash % ArraySize(p->apHash);
200902
200903 for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
200904 if( pEntry->iIdx==iIdx
200905 && pEntry->nTerm==nTerm
200906 && memcmp(pEntry->pTerm, pTerm, nTerm)==0
200907 ){
200908 *pbPresent = 1;
200909 break;
200910 }
200911 }
200912
200913 if( pEntry==0 ){
200914 pEntry = sqlite3Fts5MallocZero(&rc, sizeof(Fts5TermsetEntry) + nTerm);
200915 if( pEntry ){
200916 pEntry->pTerm = (char*)&pEntry[1];
200917 pEntry->nTerm = nTerm;
200918 pEntry->iIdx = iIdx;
200919 memcpy(pEntry->pTerm, pTerm, nTerm);
200920 pEntry->pNext = p->apHash[hash];
200921 p->apHash[hash] = pEntry;
200922 }
200923 }
200924 }
200925
200926 return rc;
200927}
200928
200929static void sqlite3Fts5TermsetFree(Fts5Termset *p){
200930 if( p ){
200931 u32 i;
200932 for(i=0; i<ArraySize(p->apHash); i++){
200933 Fts5TermsetEntry *pEntry = p->apHash[i];
200934 while( pEntry ){
200935 Fts5TermsetEntry *pDel = pEntry;
200936 pEntry = pEntry->pNext;
200937 sqlite3_free(pDel);
200938 }
200939 }
200940 sqlite3_free(p);
200941 }
200942}
200943
200944/*
200945** 2014 Jun 09
200946**
200947** The author disclaims copyright to this source code. In place of
200948** a legal notice, here is a blessing:
200949**
200950** May you do good and not evil.
200951** May you find forgiveness for yourself and forgive others.
200952** May you share freely, never taking more than you give.
200953**
200954******************************************************************************
200955**
200956** This is an SQLite module implementing full-text search.
200957*/
200958
200959
200960/* #include "fts5Int.h" */
200961
200962#define FTS5_DEFAULT_PAGE_SIZE 4050
200963#define FTS5_DEFAULT_AUTOMERGE 4
200964#define FTS5_DEFAULT_USERMERGE 4
200965#define FTS5_DEFAULT_CRISISMERGE 16
200966#define FTS5_DEFAULT_HASHSIZE (1024*1024)
200967
200968/* Maximum allowed page size */
200969#define FTS5_MAX_PAGE_SIZE (128*1024)
200970
200971static int fts5_iswhitespace(char x){
200972 return (x==' ');
200973}
200974
200975static int fts5_isopenquote(char x){
200976 return (x=='"' || x=='\'' || x=='[' || x=='`');
200977}
200978
200979/*
200980** Argument pIn points to a character that is part of a nul-terminated
200981** string. Return a pointer to the first character following *pIn in
200982** the string that is not a white-space character.
200983*/
200984static const char *fts5ConfigSkipWhitespace(const char *pIn){
200985 const char *p = pIn;
200986 if( p ){
200987 while( fts5_iswhitespace(*p) ){ p++; }
200988 }
200989 return p;
200990}
200991
200992/*
200993** Argument pIn points to a character that is part of a nul-terminated
200994** string. Return a pointer to the first character following *pIn in
200995** the string that is not a "bareword" character.
200996*/
200997static const char *fts5ConfigSkipBareword(const char *pIn){
200998 const char *p = pIn;
200999 while ( sqlite3Fts5IsBareword(*p) ) p++;
201000 if( p==pIn ) p = 0;
201001 return p;
201002}
201003
201004static int fts5_isdigit(char a){
201005 return (a>='0' && a<='9');
201006}
201007
201008
201009
201010static const char *fts5ConfigSkipLiteral(const char *pIn){
201011 const char *p = pIn;
201012 switch( *p ){
201013 case 'n': case 'N':
201014 if( sqlite3_strnicmp("null", p, 4)==0 ){
201015 p = &p[4];
201016 }else{
201017 p = 0;
201018 }
201019 break;
201020
201021 case 'x': case 'X':
201022 p++;
201023 if( *p=='\'' ){
201024 p++;
201025 while( (*p>='a' && *p<='f')
201026 || (*p>='A' && *p<='F')
201027 || (*p>='0' && *p<='9')
201028 ){
201029 p++;
201030 }
201031 if( *p=='\'' && 0==((p-pIn)%2) ){
201032 p++;
201033 }else{
201034 p = 0;
201035 }
201036 }else{
201037 p = 0;
201038 }
201039 break;
201040
201041 case '\'':
201042 p++;
201043 while( p ){
201044 if( *p=='\'' ){
201045 p++;
201046 if( *p!='\'' ) break;
201047 }
201048 p++;
201049 if( *p==0 ) p = 0;
201050 }
201051 break;
201052
201053 default:
201054 /* maybe a number */
201055 if( *p=='+' || *p=='-' ) p++;
201056 while( fts5_isdigit(*p) ) p++;
201057
201058 /* At this point, if the literal was an integer, the parse is
201059 ** finished. Or, if it is a floating point value, it may continue
201060 ** with either a decimal point or an 'E' character. */
201061 if( *p=='.' && fts5_isdigit(p[1]) ){
201062 p += 2;
201063 while( fts5_isdigit(*p) ) p++;
201064 }
201065 if( p==pIn ) p = 0;
201066
201067 break;
201068 }
201069
201070 return p;
201071}
201072
201073/*
201074** The first character of the string pointed to by argument z is guaranteed
201075** to be an open-quote character (see function fts5_isopenquote()).
201076**
201077** This function searches for the corresponding close-quote character within
201078** the string and, if found, dequotes the string in place and adds a new
201079** nul-terminator byte.
201080**
201081** If the close-quote is found, the value returned is the byte offset of
201082** the character immediately following it. Or, if the close-quote is not
201083** found, -1 is returned. If -1 is returned, the buffer is left in an
201084** undefined state.
201085*/
201086static int fts5Dequote(char *z){
201087 char q;
201088 int iIn = 1;
201089 int iOut = 0;
201090 q = z[0];
201091
201092 /* Set stack variable q to the close-quote character */
201093 assert( q=='[' || q=='\'' || q=='"' || q=='`' );
201094 if( q=='[' ) q = ']';
201095
201096 while( ALWAYS(z[iIn]) ){
201097 if( z[iIn]==q ){
201098 if( z[iIn+1]!=q ){
201099 /* Character iIn was the close quote. */
201100 iIn++;
201101 break;
201102 }else{
201103 /* Character iIn and iIn+1 form an escaped quote character. Skip
201104 ** the input cursor past both and copy a single quote character
201105 ** to the output buffer. */
201106 iIn += 2;
201107 z[iOut++] = q;
201108 }
201109 }else{
201110 z[iOut++] = z[iIn++];
201111 }
201112 }
201113
201114 z[iOut] = '\0';
201115 return iIn;
201116}
201117
201118/*
201119** Convert an SQL-style quoted string into a normal string by removing
201120** the quote characters. The conversion is done in-place. If the
201121** input does not begin with a quote character, then this routine
201122** is a no-op.
201123**
201124** Examples:
201125**
201126** "abc" becomes abc
201127** 'xyz' becomes xyz
201128** [pqr] becomes pqr
201129** `mno` becomes mno
201130*/
201131static void sqlite3Fts5Dequote(char *z){
201132 char quote; /* Quote character (if any ) */
201133
201134 assert( 0==fts5_iswhitespace(z[0]) );
201135 quote = z[0];
201136 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
201137 fts5Dequote(z);
201138 }
201139}
201140
201141
201142struct Fts5Enum {
201143 const char *zName;
201144 int eVal;
201145};
201146typedef struct Fts5Enum Fts5Enum;
201147
201148static int fts5ConfigSetEnum(
201149 const Fts5Enum *aEnum,
201150 const char *zEnum,
201151 int *peVal
201152){
201153 int nEnum = (int)strlen(zEnum);
201154 int i;
201155 int iVal = -1;
201156
201157 for(i=0; aEnum[i].zName; i++){
201158 if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){
201159 if( iVal>=0 ) return SQLITE_ERROR;
201160 iVal = aEnum[i].eVal;
201161 }
201162 }
201163
201164 *peVal = iVal;
201165 return iVal<0 ? SQLITE_ERROR : SQLITE_OK;
201166}
201167
201168/*
201169** Parse a "special" CREATE VIRTUAL TABLE directive and update
201170** configuration object pConfig as appropriate.
201171**
201172** If successful, object pConfig is updated and SQLITE_OK returned. If
201173** an error occurs, an SQLite error code is returned and an error message
201174** may be left in *pzErr. It is the responsibility of the caller to
201175** eventually free any such error message using sqlite3_free().
201176*/
201177static int fts5ConfigParseSpecial(
201178 Fts5Global *pGlobal,
201179 Fts5Config *pConfig, /* Configuration object to update */
201180 const char *zCmd, /* Special command to parse */
201181 const char *zArg, /* Argument to parse */
201182 char **pzErr /* OUT: Error message */
201183){
201184 int rc = SQLITE_OK;
201185 int nCmd = (int)strlen(zCmd);
201186 if( sqlite3_strnicmp("prefix", zCmd, nCmd)==0 ){
201187 const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES;
201188 const char *p;
201189 int bFirst = 1;
201190 if( pConfig->aPrefix==0 ){
201191 pConfig->aPrefix = sqlite3Fts5MallocZero(&rc, nByte);
201192 if( rc ) return rc;
201193 }
201194
201195 p = zArg;
201196 while( 1 ){
201197 int nPre = 0;
201198
201199 while( p[0]==' ' ) p++;
201200 if( bFirst==0 && p[0]==',' ){
201201 p++;
201202 while( p[0]==' ' ) p++;
201203 }else if( p[0]=='\0' ){
201204 break;
201205 }
201206 if( p[0]<'0' || p[0]>'9' ){
201207 *pzErr = sqlite3_mprintf("malformed prefix=... directive");
201208 rc = SQLITE_ERROR;
201209 break;
201210 }
201211
201212 if( pConfig->nPrefix==FTS5_MAX_PREFIX_INDEXES ){
201213 *pzErr = sqlite3_mprintf(
201214 "too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES
201215 );
201216 rc = SQLITE_ERROR;
201217 break;
201218 }
201219
201220 while( p[0]>='0' && p[0]<='9' && nPre<1000 ){
201221 nPre = nPre*10 + (p[0] - '0');
201222 p++;
201223 }
201224
201225 if( nPre<=0 || nPre>=1000 ){
201226 *pzErr = sqlite3_mprintf("prefix length out of range (max 999)");
201227 rc = SQLITE_ERROR;
201228 break;
201229 }
201230
201231 pConfig->aPrefix[pConfig->nPrefix] = nPre;
201232 pConfig->nPrefix++;
201233 bFirst = 0;
201234 }
201235 assert( pConfig->nPrefix<=FTS5_MAX_PREFIX_INDEXES );
201236 return rc;
201237 }
201238
201239 if( sqlite3_strnicmp("tokenize", zCmd, nCmd)==0 ){
201240 const char *p = (const char*)zArg;
201241 int nArg = (int)strlen(zArg) + 1;
201242 char **azArg = sqlite3Fts5MallocZero(&rc, sizeof(char*) * nArg);
201243 char *pDel = sqlite3Fts5MallocZero(&rc, nArg * 2);
201244 char *pSpace = pDel;
201245
201246 if( azArg && pSpace ){
201247 if( pConfig->pTok ){
201248 *pzErr = sqlite3_mprintf("multiple tokenize=... directives");
201249 rc = SQLITE_ERROR;
201250 }else{
201251 for(nArg=0; p && *p; nArg++){
201252 const char *p2 = fts5ConfigSkipWhitespace(p);
201253 if( *p2=='\'' ){
201254 p = fts5ConfigSkipLiteral(p2);
201255 }else{
201256 p = fts5ConfigSkipBareword(p2);
201257 }
201258 if( p ){
201259 memcpy(pSpace, p2, p-p2);
201260 azArg[nArg] = pSpace;
201261 sqlite3Fts5Dequote(pSpace);
201262 pSpace += (p - p2) + 1;
201263 p = fts5ConfigSkipWhitespace(p);
201264 }
201265 }
201266 if( p==0 ){
201267 *pzErr = sqlite3_mprintf("parse error in tokenize directive");
201268 rc = SQLITE_ERROR;
201269 }else{
201270 rc = sqlite3Fts5GetTokenizer(pGlobal,
201271 (const char**)azArg, nArg, &pConfig->pTok, &pConfig->pTokApi,
201272 pzErr
201273 );
201274 }
201275 }
201276 }
201277
201278 sqlite3_free(azArg);
201279 sqlite3_free(pDel);
201280 return rc;
201281 }
201282
201283 if( sqlite3_strnicmp("content", zCmd, nCmd)==0 ){
201284 if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
201285 *pzErr = sqlite3_mprintf("multiple content=... directives");
201286 rc = SQLITE_ERROR;
201287 }else{
201288 if( zArg[0] ){
201289 pConfig->eContent = FTS5_CONTENT_EXTERNAL;
201290 pConfig->zContent = sqlite3Fts5Mprintf(&rc, "%Q.%Q", pConfig->zDb,zArg);
201291 }else{
201292 pConfig->eContent = FTS5_CONTENT_NONE;
201293 }
201294 }
201295 return rc;
201296 }
201297
201298 if( sqlite3_strnicmp("content_rowid", zCmd, nCmd)==0 ){
201299 if( pConfig->zContentRowid ){
201300 *pzErr = sqlite3_mprintf("multiple content_rowid=... directives");
201301 rc = SQLITE_ERROR;
201302 }else{
201303 pConfig->zContentRowid = sqlite3Fts5Strndup(&rc, zArg, -1);
201304 }
201305 return rc;
201306 }
201307
201308 if( sqlite3_strnicmp("columnsize", zCmd, nCmd)==0 ){
201309 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
201310 *pzErr = sqlite3_mprintf("malformed columnsize=... directive");
201311 rc = SQLITE_ERROR;
201312 }else{
201313 pConfig->bColumnsize = (zArg[0]=='1');
201314 }
201315 return rc;
201316 }
201317
201318 if( sqlite3_strnicmp("detail", zCmd, nCmd)==0 ){
201319 const Fts5Enum aDetail[] = {
201320 { "none", FTS5_DETAIL_NONE },
201321 { "full", FTS5_DETAIL_FULL },
201322 { "columns", FTS5_DETAIL_COLUMNS },
201323 { 0, 0 }
201324 };
201325
201326 if( (rc = fts5ConfigSetEnum(aDetail, zArg, &pConfig->eDetail)) ){
201327 *pzErr = sqlite3_mprintf("malformed detail=... directive");
201328 }
201329 return rc;
201330 }
201331
201332 *pzErr = sqlite3_mprintf("unrecognized option: \"%.*s\"", nCmd, zCmd);
201333 return SQLITE_ERROR;
201334}
201335
201336/*
201337** Allocate an instance of the default tokenizer ("simple") at
201338** Fts5Config.pTokenizer. Return SQLITE_OK if successful, or an SQLite error
201339** code if an error occurs.
201340*/
201341static int fts5ConfigDefaultTokenizer(Fts5Global *pGlobal, Fts5Config *pConfig){
201342 assert( pConfig->pTok==0 && pConfig->pTokApi==0 );
201343 return sqlite3Fts5GetTokenizer(
201344 pGlobal, 0, 0, &pConfig->pTok, &pConfig->pTokApi, 0
201345 );
201346}
201347
201348/*
201349** Gobble up the first bareword or quoted word from the input buffer zIn.
201350** Return a pointer to the character immediately following the last in
201351** the gobbled word if successful, or a NULL pointer otherwise (failed
201352** to find close-quote character).
201353**
201354** Before returning, set pzOut to point to a new buffer containing a
201355** nul-terminated, dequoted copy of the gobbled word. If the word was
201356** quoted, *pbQuoted is also set to 1 before returning.
201357**
201358** If *pRc is other than SQLITE_OK when this function is called, it is
201359** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
201360** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
201361** set if a parse error (failed to find close quote) occurs.
201362*/
201363static const char *fts5ConfigGobbleWord(
201364 int *pRc, /* IN/OUT: Error code */
201365 const char *zIn, /* Buffer to gobble string/bareword from */
201366 char **pzOut, /* OUT: malloc'd buffer containing str/bw */
201367 int *pbQuoted /* OUT: Set to true if dequoting required */
201368){
201369 const char *zRet = 0;
201370
201371 int nIn = (int)strlen(zIn);
201372 char *zOut = sqlite3_malloc(nIn+1);
201373
201374 assert( *pRc==SQLITE_OK );
201375 *pbQuoted = 0;
201376 *pzOut = 0;
201377
201378 if( zOut==0 ){
201379 *pRc = SQLITE_NOMEM;
201380 }else{
201381 memcpy(zOut, zIn, nIn+1);
201382 if( fts5_isopenquote(zOut[0]) ){
201383 int ii = fts5Dequote(zOut);
201384 zRet = &zIn[ii];
201385 *pbQuoted = 1;
201386 }else{
201387 zRet = fts5ConfigSkipBareword(zIn);
201388 if( zRet ){
201389 zOut[zRet-zIn] = '\0';
201390 }
201391 }
201392 }
201393
201394 if( zRet==0 ){
201395 sqlite3_free(zOut);
201396 }else{
201397 *pzOut = zOut;
201398 }
201399
201400 return zRet;
201401}
201402
201403static int fts5ConfigParseColumn(
201404 Fts5Config *p,
201405 char *zCol,
201406 char *zArg,
201407 char **pzErr
201408){
201409 int rc = SQLITE_OK;
201410 if( 0==sqlite3_stricmp(zCol, FTS5_RANK_NAME)
201411 || 0==sqlite3_stricmp(zCol, FTS5_ROWID_NAME)
201412 ){
201413 *pzErr = sqlite3_mprintf("reserved fts5 column name: %s", zCol);
201414 rc = SQLITE_ERROR;
201415 }else if( zArg ){
201416 if( 0==sqlite3_stricmp(zArg, "unindexed") ){
201417 p->abUnindexed[p->nCol] = 1;
201418 }else{
201419 *pzErr = sqlite3_mprintf("unrecognized column option: %s", zArg);
201420 rc = SQLITE_ERROR;
201421 }
201422 }
201423
201424 p->azCol[p->nCol++] = zCol;
201425 return rc;
201426}
201427
201428/*
201429** Populate the Fts5Config.zContentExprlist string.
201430*/
201431static int fts5ConfigMakeExprlist(Fts5Config *p){
201432 int i;
201433 int rc = SQLITE_OK;
201434 Fts5Buffer buf = {0, 0, 0};
201435
201436 sqlite3Fts5BufferAppendPrintf(&rc, &buf, "T.%Q", p->zContentRowid);
201437 if( p->eContent!=FTS5_CONTENT_NONE ){
201438 for(i=0; i<p->nCol; i++){
201439 if( p->eContent==FTS5_CONTENT_EXTERNAL ){
201440 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.%Q", p->azCol[i]);
201441 }else{
201442 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.c%d", i);
201443 }
201444 }
201445 }
201446
201447 assert( p->zContentExprlist==0 );
201448 p->zContentExprlist = (char*)buf.p;
201449 return rc;
201450}
201451
201452/*
201453** Arguments nArg/azArg contain the string arguments passed to the xCreate
201454** or xConnect method of the virtual table. This function attempts to
201455** allocate an instance of Fts5Config containing the results of parsing
201456** those arguments.
201457**
201458** If successful, SQLITE_OK is returned and *ppOut is set to point to the
201459** new Fts5Config object. If an error occurs, an SQLite error code is
201460** returned, *ppOut is set to NULL and an error message may be left in
201461** *pzErr. It is the responsibility of the caller to eventually free any
201462** such error message using sqlite3_free().
201463*/
201464static int sqlite3Fts5ConfigParse(
201465 Fts5Global *pGlobal,
201466 sqlite3 *db,
201467 int nArg, /* Number of arguments */
201468 const char **azArg, /* Array of nArg CREATE VIRTUAL TABLE args */
201469 Fts5Config **ppOut, /* OUT: Results of parse */
201470 char **pzErr /* OUT: Error message */
201471){
201472 int rc = SQLITE_OK; /* Return code */
201473 Fts5Config *pRet; /* New object to return */
201474 int i;
201475 int nByte;
201476
201477 *ppOut = pRet = (Fts5Config*)sqlite3_malloc(sizeof(Fts5Config));
201478 if( pRet==0 ) return SQLITE_NOMEM;
201479 memset(pRet, 0, sizeof(Fts5Config));
201480 pRet->db = db;
201481 pRet->iCookie = -1;
201482
201483 nByte = nArg * (sizeof(char*) + sizeof(u8));
201484 pRet->azCol = (char**)sqlite3Fts5MallocZero(&rc, nByte);
201485 pRet->abUnindexed = (u8*)&pRet->azCol[nArg];
201486 pRet->zDb = sqlite3Fts5Strndup(&rc, azArg[1], -1);
201487 pRet->zName = sqlite3Fts5Strndup(&rc, azArg[2], -1);
201488 pRet->bColumnsize = 1;
201489 pRet->eDetail = FTS5_DETAIL_FULL;
201490#ifdef SQLITE_DEBUG
201491 pRet->bPrefixIndex = 1;
201492#endif
201493 if( rc==SQLITE_OK && sqlite3_stricmp(pRet->zName, FTS5_RANK_NAME)==0 ){
201494 *pzErr = sqlite3_mprintf("reserved fts5 table name: %s", pRet->zName);
201495 rc = SQLITE_ERROR;
201496 }
201497
201498 for(i=3; rc==SQLITE_OK && i<nArg; i++){
201499 const char *zOrig = azArg[i];
201500 const char *z;
201501 char *zOne = 0;
201502 char *zTwo = 0;
201503 int bOption = 0;
201504 int bMustBeCol = 0;
201505
201506 z = fts5ConfigGobbleWord(&rc, zOrig, &zOne, &bMustBeCol);
201507 z = fts5ConfigSkipWhitespace(z);
201508 if( z && *z=='=' ){
201509 bOption = 1;
201510 z++;
201511 if( bMustBeCol ) z = 0;
201512 }
201513 z = fts5ConfigSkipWhitespace(z);
201514 if( z && z[0] ){
201515 int bDummy;
201516 z = fts5ConfigGobbleWord(&rc, z, &zTwo, &bDummy);
201517 if( z && z[0] ) z = 0;
201518 }
201519
201520 if( rc==SQLITE_OK ){
201521 if( z==0 ){
201522 *pzErr = sqlite3_mprintf("parse error in \"%s\"", zOrig);
201523 rc = SQLITE_ERROR;
201524 }else{
201525 if( bOption ){
201526 rc = fts5ConfigParseSpecial(pGlobal, pRet, zOne, zTwo?zTwo:"", pzErr);
201527 }else{
201528 rc = fts5ConfigParseColumn(pRet, zOne, zTwo, pzErr);
201529 zOne = 0;
201530 }
201531 }
201532 }
201533
201534 sqlite3_free(zOne);
201535 sqlite3_free(zTwo);
201536 }
201537
201538 /* If a tokenizer= option was successfully parsed, the tokenizer has
201539 ** already been allocated. Otherwise, allocate an instance of the default
201540 ** tokenizer (unicode61) now. */
201541 if( rc==SQLITE_OK && pRet->pTok==0 ){
201542 rc = fts5ConfigDefaultTokenizer(pGlobal, pRet);
201543 }
201544
201545 /* If no zContent option was specified, fill in the default values. */
201546 if( rc==SQLITE_OK && pRet->zContent==0 ){
201547 const char *zTail = 0;
201548 assert( pRet->eContent==FTS5_CONTENT_NORMAL
201549 || pRet->eContent==FTS5_CONTENT_NONE
201550 );
201551 if( pRet->eContent==FTS5_CONTENT_NORMAL ){
201552 zTail = "content";
201553 }else if( pRet->bColumnsize ){
201554 zTail = "docsize";
201555 }
201556
201557 if( zTail ){
201558 pRet->zContent = sqlite3Fts5Mprintf(
201559 &rc, "%Q.'%q_%s'", pRet->zDb, pRet->zName, zTail
201560 );
201561 }
201562 }
201563
201564 if( rc==SQLITE_OK && pRet->zContentRowid==0 ){
201565 pRet->zContentRowid = sqlite3Fts5Strndup(&rc, "rowid", -1);
201566 }
201567
201568 /* Formulate the zContentExprlist text */
201569 if( rc==SQLITE_OK ){
201570 rc = fts5ConfigMakeExprlist(pRet);
201571 }
201572
201573 if( rc!=SQLITE_OK ){
201574 sqlite3Fts5ConfigFree(pRet);
201575 *ppOut = 0;
201576 }
201577 return rc;
201578}
201579
201580/*
201581** Free the configuration object passed as the only argument.
201582*/
201583static void sqlite3Fts5ConfigFree(Fts5Config *pConfig){
201584 if( pConfig ){
201585 int i;
201586 if( pConfig->pTok ){
201587 pConfig->pTokApi->xDelete(pConfig->pTok);
201588 }
201589 sqlite3_free(pConfig->zDb);
201590 sqlite3_free(pConfig->zName);
201591 for(i=0; i<pConfig->nCol; i++){
201592 sqlite3_free(pConfig->azCol[i]);
201593 }
201594 sqlite3_free(pConfig->azCol);
201595 sqlite3_free(pConfig->aPrefix);
201596 sqlite3_free(pConfig->zRank);
201597 sqlite3_free(pConfig->zRankArgs);
201598 sqlite3_free(pConfig->zContent);
201599 sqlite3_free(pConfig->zContentRowid);
201600 sqlite3_free(pConfig->zContentExprlist);
201601 sqlite3_free(pConfig);
201602 }
201603}
201604
201605/*
201606** Call sqlite3_declare_vtab() based on the contents of the configuration
201607** object passed as the only argument. Return SQLITE_OK if successful, or
201608** an SQLite error code if an error occurs.
201609*/
201610static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){
201611 int i;
201612 int rc = SQLITE_OK;
201613 char *zSql;
201614
201615 zSql = sqlite3Fts5Mprintf(&rc, "CREATE TABLE x(");
201616 for(i=0; zSql && i<pConfig->nCol; i++){
201617 const char *zSep = (i==0?"":", ");
201618 zSql = sqlite3Fts5Mprintf(&rc, "%z%s%Q", zSql, zSep, pConfig->azCol[i]);
201619 }
201620 zSql = sqlite3Fts5Mprintf(&rc, "%z, %Q HIDDEN, %s HIDDEN)",
201621 zSql, pConfig->zName, FTS5_RANK_NAME
201622 );
201623
201624 assert( zSql || rc==SQLITE_NOMEM );
201625 if( zSql ){
201626 rc = sqlite3_declare_vtab(pConfig->db, zSql);
201627 sqlite3_free(zSql);
201628 }
201629
201630 return rc;
201631}
201632
201633/*
201634** Tokenize the text passed via the second and third arguments.
201635**
201636** The callback is invoked once for each token in the input text. The
201637** arguments passed to it are, in order:
201638**
201639** void *pCtx // Copy of 4th argument to sqlite3Fts5Tokenize()
201640** const char *pToken // Pointer to buffer containing token
201641** int nToken // Size of token in bytes
201642** int iStart // Byte offset of start of token within input text
201643** int iEnd // Byte offset of end of token within input text
201644** int iPos // Position of token in input (first token is 0)
201645**
201646** If the callback returns a non-zero value the tokenization is abandoned
201647** and no further callbacks are issued.
201648**
201649** This function returns SQLITE_OK if successful or an SQLite error code
201650** if an error occurs. If the tokenization was abandoned early because
201651** the callback returned SQLITE_DONE, this is not an error and this function
201652** still returns SQLITE_OK. Or, if the tokenization was abandoned early
201653** because the callback returned another non-zero value, it is assumed
201654** to be an SQLite error code and returned to the caller.
201655*/
201656static int sqlite3Fts5Tokenize(
201657 Fts5Config *pConfig, /* FTS5 Configuration object */
201658 int flags, /* FTS5_TOKENIZE_* flags */
201659 const char *pText, int nText, /* Text to tokenize */
201660 void *pCtx, /* Context passed to xToken() */
201661 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
201662){
201663 if( pText==0 ) return SQLITE_OK;
201664 return pConfig->pTokApi->xTokenize(
201665 pConfig->pTok, pCtx, flags, pText, nText, xToken
201666 );
201667}
201668
201669/*
201670** Argument pIn points to the first character in what is expected to be
201671** a comma-separated list of SQL literals followed by a ')' character.
201672** If it actually is this, return a pointer to the ')'. Otherwise, return
201673** NULL to indicate a parse error.
201674*/
201675static const char *fts5ConfigSkipArgs(const char *pIn){
201676 const char *p = pIn;
201677
201678 while( 1 ){
201679 p = fts5ConfigSkipWhitespace(p);
201680 p = fts5ConfigSkipLiteral(p);
201681 p = fts5ConfigSkipWhitespace(p);
201682 if( p==0 || *p==')' ) break;
201683 if( *p!=',' ){
201684 p = 0;
201685 break;
201686 }
201687 p++;
201688 }
201689
201690 return p;
201691}
201692
201693/*
201694** Parameter zIn contains a rank() function specification. The format of
201695** this is:
201696**
201697** + Bareword (function name)
201698** + Open parenthesis - "("
201699** + Zero or more SQL literals in a comma separated list
201700** + Close parenthesis - ")"
201701*/
201702static int sqlite3Fts5ConfigParseRank(
201703 const char *zIn, /* Input string */
201704 char **pzRank, /* OUT: Rank function name */
201705 char **pzRankArgs /* OUT: Rank function arguments */
201706){
201707 const char *p = zIn;
201708 const char *pRank;
201709 char *zRank = 0;
201710 char *zRankArgs = 0;
201711 int rc = SQLITE_OK;
201712
201713 *pzRank = 0;
201714 *pzRankArgs = 0;
201715
201716 if( p==0 ){
201717 rc = SQLITE_ERROR;
201718 }else{
201719 p = fts5ConfigSkipWhitespace(p);
201720 pRank = p;
201721 p = fts5ConfigSkipBareword(p);
201722
201723 if( p ){
201724 zRank = sqlite3Fts5MallocZero(&rc, 1 + p - pRank);
201725 if( zRank ) memcpy(zRank, pRank, p-pRank);
201726 }else{
201727 rc = SQLITE_ERROR;
201728 }
201729
201730 if( rc==SQLITE_OK ){
201731 p = fts5ConfigSkipWhitespace(p);
201732 if( *p!='(' ) rc = SQLITE_ERROR;
201733 p++;
201734 }
201735 if( rc==SQLITE_OK ){
201736 const char *pArgs;
201737 p = fts5ConfigSkipWhitespace(p);
201738 pArgs = p;
201739 if( *p!=')' ){
201740 p = fts5ConfigSkipArgs(p);
201741 if( p==0 ){
201742 rc = SQLITE_ERROR;
201743 }else{
201744 zRankArgs = sqlite3Fts5MallocZero(&rc, 1 + p - pArgs);
201745 if( zRankArgs ) memcpy(zRankArgs, pArgs, p-pArgs);
201746 }
201747 }
201748 }
201749 }
201750
201751 if( rc!=SQLITE_OK ){
201752 sqlite3_free(zRank);
201753 assert( zRankArgs==0 );
201754 }else{
201755 *pzRank = zRank;
201756 *pzRankArgs = zRankArgs;
201757 }
201758 return rc;
201759}
201760
201761static int sqlite3Fts5ConfigSetValue(
201762 Fts5Config *pConfig,
201763 const char *zKey,
201764 sqlite3_value *pVal,
201765 int *pbBadkey
201766){
201767 int rc = SQLITE_OK;
201768
201769 if( 0==sqlite3_stricmp(zKey, "pgsz") ){
201770 int pgsz = 0;
201771 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
201772 pgsz = sqlite3_value_int(pVal);
201773 }
201774 if( pgsz<=0 || pgsz>FTS5_MAX_PAGE_SIZE ){
201775 *pbBadkey = 1;
201776 }else{
201777 pConfig->pgsz = pgsz;
201778 }
201779 }
201780
201781 else if( 0==sqlite3_stricmp(zKey, "hashsize") ){
201782 int nHashSize = -1;
201783 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
201784 nHashSize = sqlite3_value_int(pVal);
201785 }
201786 if( nHashSize<=0 ){
201787 *pbBadkey = 1;
201788 }else{
201789 pConfig->nHashSize = nHashSize;
201790 }
201791 }
201792
201793 else if( 0==sqlite3_stricmp(zKey, "automerge") ){
201794 int nAutomerge = -1;
201795 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
201796 nAutomerge = sqlite3_value_int(pVal);
201797 }
201798 if( nAutomerge<0 || nAutomerge>64 ){
201799 *pbBadkey = 1;
201800 }else{
201801 if( nAutomerge==1 ) nAutomerge = FTS5_DEFAULT_AUTOMERGE;
201802 pConfig->nAutomerge = nAutomerge;
201803 }
201804 }
201805
201806 else if( 0==sqlite3_stricmp(zKey, "usermerge") ){
201807 int nUsermerge = -1;
201808 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
201809 nUsermerge = sqlite3_value_int(pVal);
201810 }
201811 if( nUsermerge<2 || nUsermerge>16 ){
201812 *pbBadkey = 1;
201813 }else{
201814 pConfig->nUsermerge = nUsermerge;
201815 }
201816 }
201817
201818 else if( 0==sqlite3_stricmp(zKey, "crisismerge") ){
201819 int nCrisisMerge = -1;
201820 if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
201821 nCrisisMerge = sqlite3_value_int(pVal);
201822 }
201823 if( nCrisisMerge<0 ){
201824 *pbBadkey = 1;
201825 }else{
201826 if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
201827 pConfig->nCrisisMerge = nCrisisMerge;
201828 }
201829 }
201830
201831 else if( 0==sqlite3_stricmp(zKey, "rank") ){
201832 const char *zIn = (const char*)sqlite3_value_text(pVal);
201833 char *zRank;
201834 char *zRankArgs;
201835 rc = sqlite3Fts5ConfigParseRank(zIn, &zRank, &zRankArgs);
201836 if( rc==SQLITE_OK ){
201837 sqlite3_free(pConfig->zRank);
201838 sqlite3_free(pConfig->zRankArgs);
201839 pConfig->zRank = zRank;
201840 pConfig->zRankArgs = zRankArgs;
201841 }else if( rc==SQLITE_ERROR ){
201842 rc = SQLITE_OK;
201843 *pbBadkey = 1;
201844 }
201845 }else{
201846 *pbBadkey = 1;
201847 }
201848 return rc;
201849}
201850
201851/*
201852** Load the contents of the %_config table into memory.
201853*/
201854static int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){
201855 const char *zSelect = "SELECT k, v FROM %Q.'%q_config'";
201856 char *zSql;
201857 sqlite3_stmt *p = 0;
201858 int rc = SQLITE_OK;
201859 int iVersion = 0;
201860
201861 /* Set default values */
201862 pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE;
201863 pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE;
201864 pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE;
201865 pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
201866 pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE;
201867
201868 zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName);
201869 if( zSql ){
201870 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &p, 0);
201871 sqlite3_free(zSql);
201872 }
201873
201874 assert( rc==SQLITE_OK || p==0 );
201875 if( rc==SQLITE_OK ){
201876 while( SQLITE_ROW==sqlite3_step(p) ){
201877 const char *zK = (const char*)sqlite3_column_text(p, 0);
201878 sqlite3_value *pVal = sqlite3_column_value(p, 1);
201879 if( 0==sqlite3_stricmp(zK, "version") ){
201880 iVersion = sqlite3_value_int(pVal);
201881 }else{
201882 int bDummy = 0;
201883 sqlite3Fts5ConfigSetValue(pConfig, zK, pVal, &bDummy);
201884 }
201885 }
201886 rc = sqlite3_finalize(p);
201887 }
201888
201889 if( rc==SQLITE_OK && iVersion!=FTS5_CURRENT_VERSION ){
201890 rc = SQLITE_ERROR;
201891 if( pConfig->pzErrmsg ){
201892 assert( 0==*pConfig->pzErrmsg );
201893 *pConfig->pzErrmsg = sqlite3_mprintf(
201894 "invalid fts5 file format (found %d, expected %d) - run 'rebuild'",
201895 iVersion, FTS5_CURRENT_VERSION
201896 );
201897 }
201898 }
201899
201900 if( rc==SQLITE_OK ){
201901 pConfig->iCookie = iCookie;
201902 }
201903 return rc;
201904}
201905
201906/*
201907** 2014 May 31
201908**
201909** The author disclaims copyright to this source code. In place of
201910** a legal notice, here is a blessing:
201911**
201912** May you do good and not evil.
201913** May you find forgiveness for yourself and forgive others.
201914** May you share freely, never taking more than you give.
201915**
201916******************************************************************************
201917**
201918*/
201919
201920
201921
201922/* #include "fts5Int.h" */
201923/* #include "fts5parse.h" */
201924
201925/*
201926** All token types in the generated fts5parse.h file are greater than 0.
201927*/
201928#define FTS5_EOF 0
201929
201930#define FTS5_LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
201931
201932typedef struct Fts5ExprTerm Fts5ExprTerm;
201933
201934/*
201935** Functions generated by lemon from fts5parse.y.
201936*/
201937static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
201938static void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
201939static void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
201940#ifndef NDEBUG
201941/* #include <stdio.h> */
201942static void sqlite3Fts5ParserTrace(FILE*, char*);
201943#endif
201944static int sqlite3Fts5ParserFallback(int);
201945
201946
201947struct Fts5Expr {
201948 Fts5Index *pIndex;
201949 Fts5Config *pConfig;
201950 Fts5ExprNode *pRoot;
201951 int bDesc; /* Iterate in descending rowid order */
201952 int nPhrase; /* Number of phrases in expression */
201953 Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */
201954};
201955
201956/*
201957** eType:
201958** Expression node type. Always one of:
201959**
201960** FTS5_AND (nChild, apChild valid)
201961** FTS5_OR (nChild, apChild valid)
201962** FTS5_NOT (nChild, apChild valid)
201963** FTS5_STRING (pNear valid)
201964** FTS5_TERM (pNear valid)
201965*/
201966struct Fts5ExprNode {
201967 int eType; /* Node type */
201968 int bEof; /* True at EOF */
201969 int bNomatch; /* True if entry is not a match */
201970
201971 /* Next method for this node. */
201972 int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
201973
201974 i64 iRowid; /* Current rowid */
201975 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
201976
201977 /* Child nodes. For a NOT node, this array always contains 2 entries. For
201978 ** AND or OR nodes, it contains 2 or more entries. */
201979 int nChild; /* Number of child nodes */
201980 Fts5ExprNode *apChild[1]; /* Array of child nodes */
201981};
201982
201983#define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
201984
201985/*
201986** Invoke the xNext method of an Fts5ExprNode object. This macro should be
201987** used as if it has the same signature as the xNext() methods themselves.
201988*/
201989#define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
201990
201991/*
201992** An instance of the following structure represents a single search term
201993** or term prefix.
201994*/
201995struct Fts5ExprTerm {
201996 u8 bPrefix; /* True for a prefix term */
201997 u8 bFirst; /* True if token must be first in column */
201998 char *zTerm; /* nul-terminated term */
201999 Fts5IndexIter *pIter; /* Iterator for this term */
202000 Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */
202001};
202002
202003/*
202004** A phrase. One or more terms that must appear in a contiguous sequence
202005** within a document for it to match.
202006*/
202007struct Fts5ExprPhrase {
202008 Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */
202009 Fts5Buffer poslist; /* Current position list */
202010 int nTerm; /* Number of entries in aTerm[] */
202011 Fts5ExprTerm aTerm[1]; /* Terms that make up this phrase */
202012};
202013
202014/*
202015** One or more phrases that must appear within a certain token distance of
202016** each other within each matching document.
202017*/
202018struct Fts5ExprNearset {
202019 int nNear; /* NEAR parameter */
202020 Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */
202021 int nPhrase; /* Number of entries in aPhrase[] array */
202022 Fts5ExprPhrase *apPhrase[1]; /* Array of phrase pointers */
202023};
202024
202025
202026/*
202027** Parse context.
202028*/
202029struct Fts5Parse {
202030 Fts5Config *pConfig;
202031 char *zErr;
202032 int rc;
202033 int nPhrase; /* Size of apPhrase array */
202034 Fts5ExprPhrase **apPhrase; /* Array of all phrases */
202035 Fts5ExprNode *pExpr; /* Result of a successful parse */
202036};
202037
202038static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
202039 va_list ap;
202040 va_start(ap, zFmt);
202041 if( pParse->rc==SQLITE_OK ){
202042 pParse->zErr = sqlite3_vmprintf(zFmt, ap);
202043 pParse->rc = SQLITE_ERROR;
202044 }
202045 va_end(ap);
202046}
202047
202048static int fts5ExprIsspace(char t){
202049 return t==' ' || t=='\t' || t=='\n' || t=='\r';
202050}
202051
202052/*
202053** Read the first token from the nul-terminated string at *pz.
202054*/
202055static int fts5ExprGetToken(
202056 Fts5Parse *pParse,
202057 const char **pz, /* IN/OUT: Pointer into buffer */
202058 Fts5Token *pToken
202059){
202060 const char *z = *pz;
202061 int tok;
202062
202063 /* Skip past any whitespace */
202064 while( fts5ExprIsspace(*z) ) z++;
202065
202066 pToken->p = z;
202067 pToken->n = 1;
202068 switch( *z ){
202069 case '(': tok = FTS5_LP; break;
202070 case ')': tok = FTS5_RP; break;
202071 case '{': tok = FTS5_LCP; break;
202072 case '}': tok = FTS5_RCP; break;
202073 case ':': tok = FTS5_COLON; break;
202074 case ',': tok = FTS5_COMMA; break;
202075 case '+': tok = FTS5_PLUS; break;
202076 case '*': tok = FTS5_STAR; break;
202077 case '-': tok = FTS5_MINUS; break;
202078 case '^': tok = FTS5_CARET; break;
202079 case '\0': tok = FTS5_EOF; break;
202080
202081 case '"': {
202082 const char *z2;
202083 tok = FTS5_STRING;
202084
202085 for(z2=&z[1]; 1; z2++){
202086 if( z2[0]=='"' ){
202087 z2++;
202088 if( z2[0]!='"' ) break;
202089 }
202090 if( z2[0]=='\0' ){
202091 sqlite3Fts5ParseError(pParse, "unterminated string");
202092 return FTS5_EOF;
202093 }
202094 }
202095 pToken->n = (z2 - z);
202096 break;
202097 }
202098
202099 default: {
202100 const char *z2;
202101 if( sqlite3Fts5IsBareword(z[0])==0 ){
202102 sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z);
202103 return FTS5_EOF;
202104 }
202105 tok = FTS5_STRING;
202106 for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++);
202107 pToken->n = (z2 - z);
202108 if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR;
202109 if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT;
202110 if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND;
202111 break;
202112 }
202113 }
202114
202115 *pz = &pToken->p[pToken->n];
202116 return tok;
202117}
202118
202119static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc((int)t); }
202120static void fts5ParseFree(void *p){ sqlite3_free(p); }
202121
202122static int sqlite3Fts5ExprNew(
202123 Fts5Config *pConfig, /* FTS5 Configuration */
202124 int iCol,
202125 const char *zExpr, /* Expression text */
202126 Fts5Expr **ppNew,
202127 char **pzErr
202128){
202129 Fts5Parse sParse;
202130 Fts5Token token;
202131 const char *z = zExpr;
202132 int t; /* Next token type */
202133 void *pEngine;
202134 Fts5Expr *pNew;
202135
202136 *ppNew = 0;
202137 *pzErr = 0;
202138 memset(&sParse, 0, sizeof(sParse));
202139 pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc);
202140 if( pEngine==0 ){ return SQLITE_NOMEM; }
202141 sParse.pConfig = pConfig;
202142
202143 do {
202144 t = fts5ExprGetToken(&sParse, &z, &token);
202145 sqlite3Fts5Parser(pEngine, t, token, &sParse);
202146 }while( sParse.rc==SQLITE_OK && t!=FTS5_EOF );
202147 sqlite3Fts5ParserFree(pEngine, fts5ParseFree);
202148
202149 /* If the LHS of the MATCH expression was a user column, apply the
202150 ** implicit column-filter. */
202151 if( iCol<pConfig->nCol && sParse.pExpr && sParse.rc==SQLITE_OK ){
202152 int n = sizeof(Fts5Colset);
202153 Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n);
202154 if( pColset ){
202155 pColset->nCol = 1;
202156 pColset->aiCol[0] = iCol;
202157 sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset);
202158 }
202159 }
202160
202161 assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 );
202162 if( sParse.rc==SQLITE_OK ){
202163 *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
202164 if( pNew==0 ){
202165 sParse.rc = SQLITE_NOMEM;
202166 sqlite3Fts5ParseNodeFree(sParse.pExpr);
202167 }else{
202168 if( !sParse.pExpr ){
202169 const int nByte = sizeof(Fts5ExprNode);
202170 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&sParse.rc, nByte);
202171 if( pNew->pRoot ){
202172 pNew->pRoot->bEof = 1;
202173 }
202174 }else{
202175 pNew->pRoot = sParse.pExpr;
202176 }
202177 pNew->pIndex = 0;
202178 pNew->pConfig = pConfig;
202179 pNew->apExprPhrase = sParse.apPhrase;
202180 pNew->nPhrase = sParse.nPhrase;
202181 sParse.apPhrase = 0;
202182 }
202183 }else{
202184 sqlite3Fts5ParseNodeFree(sParse.pExpr);
202185 }
202186
202187 sqlite3_free(sParse.apPhrase);
202188 *pzErr = sParse.zErr;
202189 return sParse.rc;
202190}
202191
202192/*
202193** Free the expression node object passed as the only argument.
202194*/
202195static void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
202196 if( p ){
202197 int i;
202198 for(i=0; i<p->nChild; i++){
202199 sqlite3Fts5ParseNodeFree(p->apChild[i]);
202200 }
202201 sqlite3Fts5ParseNearsetFree(p->pNear);
202202 sqlite3_free(p);
202203 }
202204}
202205
202206/*
202207** Free the expression object passed as the only argument.
202208*/
202209static void sqlite3Fts5ExprFree(Fts5Expr *p){
202210 if( p ){
202211 sqlite3Fts5ParseNodeFree(p->pRoot);
202212 sqlite3_free(p->apExprPhrase);
202213 sqlite3_free(p);
202214 }
202215}
202216
202217/*
202218** Argument pTerm must be a synonym iterator. Return the current rowid
202219** that it points to.
202220*/
202221static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
202222 i64 iRet = 0;
202223 int bRetValid = 0;
202224 Fts5ExprTerm *p;
202225
202226 assert( pTerm->pSynonym );
202227 assert( bDesc==0 || bDesc==1 );
202228 for(p=pTerm; p; p=p->pSynonym){
202229 if( 0==sqlite3Fts5IterEof(p->pIter) ){
202230 i64 iRowid = p->pIter->iRowid;
202231 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
202232 iRet = iRowid;
202233 bRetValid = 1;
202234 }
202235 }
202236 }
202237
202238 if( pbEof && bRetValid==0 ) *pbEof = 1;
202239 return iRet;
202240}
202241
202242/*
202243** Argument pTerm must be a synonym iterator.
202244*/
202245static int fts5ExprSynonymList(
202246 Fts5ExprTerm *pTerm,
202247 i64 iRowid,
202248 Fts5Buffer *pBuf, /* Use this buffer for space if required */
202249 u8 **pa, int *pn
202250){
202251 Fts5PoslistReader aStatic[4];
202252 Fts5PoslistReader *aIter = aStatic;
202253 int nIter = 0;
202254 int nAlloc = 4;
202255 int rc = SQLITE_OK;
202256 Fts5ExprTerm *p;
202257
202258 assert( pTerm->pSynonym );
202259 for(p=pTerm; p; p=p->pSynonym){
202260 Fts5IndexIter *pIter = p->pIter;
202261 if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){
202262 if( pIter->nData==0 ) continue;
202263 if( nIter==nAlloc ){
202264 int nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
202265 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc(nByte);
202266 if( aNew==0 ){
202267 rc = SQLITE_NOMEM;
202268 goto synonym_poslist_out;
202269 }
202270 memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
202271 nAlloc = nAlloc*2;
202272 if( aIter!=aStatic ) sqlite3_free(aIter);
202273 aIter = aNew;
202274 }
202275 sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
202276 assert( aIter[nIter].bEof==0 );
202277 nIter++;
202278 }
202279 }
202280
202281 if( nIter==1 ){
202282 *pa = (u8*)aIter[0].a;
202283 *pn = aIter[0].n;
202284 }else{
202285 Fts5PoslistWriter writer = {0};
202286 i64 iPrev = -1;
202287 fts5BufferZero(pBuf);
202288 while( 1 ){
202289 int i;
202290 i64 iMin = FTS5_LARGEST_INT64;
202291 for(i=0; i<nIter; i++){
202292 if( aIter[i].bEof==0 ){
202293 if( aIter[i].iPos==iPrev ){
202294 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue;
202295 }
202296 if( aIter[i].iPos<iMin ){
202297 iMin = aIter[i].iPos;
202298 }
202299 }
202300 }
202301 if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break;
202302 rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
202303 iPrev = iMin;
202304 }
202305 if( rc==SQLITE_OK ){
202306 *pa = pBuf->p;
202307 *pn = pBuf->n;
202308 }
202309 }
202310
202311 synonym_poslist_out:
202312 if( aIter!=aStatic ) sqlite3_free(aIter);
202313 return rc;
202314}
202315
202316
202317/*
202318** All individual term iterators in pPhrase are guaranteed to be valid and
202319** pointing to the same rowid when this function is called. This function
202320** checks if the current rowid really is a match, and if so populates
202321** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
202322** is set to true if this is really a match, or false otherwise.
202323**
202324** SQLITE_OK is returned if an error occurs, or an SQLite error code
202325** otherwise. It is not considered an error code if the current rowid is
202326** not a match.
202327*/
202328static int fts5ExprPhraseIsMatch(
202329 Fts5ExprNode *pNode, /* Node pPhrase belongs to */
202330 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
202331 int *pbMatch /* OUT: Set to true if really a match */
202332){
202333 Fts5PoslistWriter writer = {0};
202334 Fts5PoslistReader aStatic[4];
202335 Fts5PoslistReader *aIter = aStatic;
202336 int i;
202337 int rc = SQLITE_OK;
202338 int bFirst = pPhrase->aTerm[0].bFirst;
202339
202340 fts5BufferZero(&pPhrase->poslist);
202341
202342 /* If the aStatic[] array is not large enough, allocate a large array
202343 ** using sqlite3_malloc(). This approach could be improved upon. */
202344 if( pPhrase->nTerm>ArraySize(aStatic) ){
202345 int nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
202346 aIter = (Fts5PoslistReader*)sqlite3_malloc(nByte);
202347 if( !aIter ) return SQLITE_NOMEM;
202348 }
202349 memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
202350
202351 /* Initialize a term iterator for each term in the phrase */
202352 for(i=0; i<pPhrase->nTerm; i++){
202353 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
202354 int n = 0;
202355 int bFlag = 0;
202356 u8 *a = 0;
202357 if( pTerm->pSynonym ){
202358 Fts5Buffer buf = {0, 0, 0};
202359 rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
202360 if( rc ){
202361 sqlite3_free(a);
202362 goto ismatch_out;
202363 }
202364 if( a==buf.p ) bFlag = 1;
202365 }else{
202366 a = (u8*)pTerm->pIter->pData;
202367 n = pTerm->pIter->nData;
202368 }
202369 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
202370 aIter[i].bFlag = (u8)bFlag;
202371 if( aIter[i].bEof ) goto ismatch_out;
202372 }
202373
202374 while( 1 ){
202375 int bMatch;
202376 i64 iPos = aIter[0].iPos;
202377 do {
202378 bMatch = 1;
202379 for(i=0; i<pPhrase->nTerm; i++){
202380 Fts5PoslistReader *pPos = &aIter[i];
202381 i64 iAdj = iPos + i;
202382 if( pPos->iPos!=iAdj ){
202383 bMatch = 0;
202384 while( pPos->iPos<iAdj ){
202385 if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out;
202386 }
202387 if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
202388 }
202389 }
202390 }while( bMatch==0 );
202391
202392 /* Append position iPos to the output */
202393 if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){
202394 rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
202395 if( rc!=SQLITE_OK ) goto ismatch_out;
202396 }
202397
202398 for(i=0; i<pPhrase->nTerm; i++){
202399 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out;
202400 }
202401 }
202402
202403 ismatch_out:
202404 *pbMatch = (pPhrase->poslist.n>0);
202405 for(i=0; i<pPhrase->nTerm; i++){
202406 if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a);
202407 }
202408 if( aIter!=aStatic ) sqlite3_free(aIter);
202409 return rc;
202410}
202411
202412typedef struct Fts5LookaheadReader Fts5LookaheadReader;
202413struct Fts5LookaheadReader {
202414 const u8 *a; /* Buffer containing position list */
202415 int n; /* Size of buffer a[] in bytes */
202416 int i; /* Current offset in position list */
202417 i64 iPos; /* Current position */
202418 i64 iLookahead; /* Next position */
202419};
202420
202421#define FTS5_LOOKAHEAD_EOF (((i64)1) << 62)
202422
202423static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
202424 p->iPos = p->iLookahead;
202425 if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){
202426 p->iLookahead = FTS5_LOOKAHEAD_EOF;
202427 }
202428 return (p->iPos==FTS5_LOOKAHEAD_EOF);
202429}
202430
202431static int fts5LookaheadReaderInit(
202432 const u8 *a, int n, /* Buffer to read position list from */
202433 Fts5LookaheadReader *p /* Iterator object to initialize */
202434){
202435 memset(p, 0, sizeof(Fts5LookaheadReader));
202436 p->a = a;
202437 p->n = n;
202438 fts5LookaheadReaderNext(p);
202439 return fts5LookaheadReaderNext(p);
202440}
202441
202442typedef struct Fts5NearTrimmer Fts5NearTrimmer;
202443struct Fts5NearTrimmer {
202444 Fts5LookaheadReader reader; /* Input iterator */
202445 Fts5PoslistWriter writer; /* Writer context */
202446 Fts5Buffer *pOut; /* Output poslist */
202447};
202448
202449/*
202450** The near-set object passed as the first argument contains more than
202451** one phrase. All phrases currently point to the same row. The
202452** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
202453** tests if the current row contains instances of each phrase sufficiently
202454** close together to meet the NEAR constraint. Non-zero is returned if it
202455** does, or zero otherwise.
202456**
202457** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
202458** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
202459** occurs within this function (*pRc) is set accordingly before returning.
202460** The return value is undefined in both these cases.
202461**
202462** If no error occurs and non-zero (a match) is returned, the position-list
202463** of each phrase object is edited to contain only those entries that
202464** meet the constraint before returning.
202465*/
202466static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
202467 Fts5NearTrimmer aStatic[4];
202468 Fts5NearTrimmer *a = aStatic;
202469 Fts5ExprPhrase **apPhrase = pNear->apPhrase;
202470
202471 int i;
202472 int rc = *pRc;
202473 int bMatch;
202474
202475 assert( pNear->nPhrase>1 );
202476
202477 /* If the aStatic[] array is not large enough, allocate a large array
202478 ** using sqlite3_malloc(). This approach could be improved upon. */
202479 if( pNear->nPhrase>ArraySize(aStatic) ){
202480 int nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
202481 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
202482 }else{
202483 memset(aStatic, 0, sizeof(aStatic));
202484 }
202485 if( rc!=SQLITE_OK ){
202486 *pRc = rc;
202487 return 0;
202488 }
202489
202490 /* Initialize a lookahead iterator for each phrase. After passing the
202491 ** buffer and buffer size to the lookaside-reader init function, zero
202492 ** the phrase poslist buffer. The new poslist for the phrase (containing
202493 ** the same entries as the original with some entries removed on account
202494 ** of the NEAR constraint) is written over the original even as it is
202495 ** being read. This is safe as the entries for the new poslist are a
202496 ** subset of the old, so it is not possible for data yet to be read to
202497 ** be overwritten. */
202498 for(i=0; i<pNear->nPhrase; i++){
202499 Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
202500 fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader);
202501 pPoslist->n = 0;
202502 a[i].pOut = pPoslist;
202503 }
202504
202505 while( 1 ){
202506 int iAdv;
202507 i64 iMin;
202508 i64 iMax;
202509
202510 /* This block advances the phrase iterators until they point to a set of
202511 ** entries that together comprise a match. */
202512 iMax = a[0].reader.iPos;
202513 do {
202514 bMatch = 1;
202515 for(i=0; i<pNear->nPhrase; i++){
202516 Fts5LookaheadReader *pPos = &a[i].reader;
202517 iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
202518 if( pPos->iPos<iMin || pPos->iPos>iMax ){
202519 bMatch = 0;
202520 while( pPos->iPos<iMin ){
202521 if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out;
202522 }
202523 if( pPos->iPos>iMax ) iMax = pPos->iPos;
202524 }
202525 }
202526 }while( bMatch==0 );
202527
202528 /* Add an entry to each output position list */
202529 for(i=0; i<pNear->nPhrase; i++){
202530 i64 iPos = a[i].reader.iPos;
202531 Fts5PoslistWriter *pWriter = &a[i].writer;
202532 if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
202533 sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos);
202534 }
202535 }
202536
202537 iAdv = 0;
202538 iMin = a[0].reader.iLookahead;
202539 for(i=0; i<pNear->nPhrase; i++){
202540 if( a[i].reader.iLookahead < iMin ){
202541 iMin = a[i].reader.iLookahead;
202542 iAdv = i;
202543 }
202544 }
202545 if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out;
202546 }
202547
202548 ismatch_out: {
202549 int bRet = a[0].pOut->n>0;
202550 *pRc = rc;
202551 if( a!=aStatic ) sqlite3_free(a);
202552 return bRet;
202553 }
202554}
202555
202556/*
202557** Advance iterator pIter until it points to a value equal to or laster
202558** than the initial value of *piLast. If this means the iterator points
202559** to a value laster than *piLast, update *piLast to the new lastest value.
202560**
202561** If the iterator reaches EOF, set *pbEof to true before returning. If
202562** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
202563** are set, return a non-zero value. Otherwise, return zero.
202564*/
202565static int fts5ExprAdvanceto(
202566 Fts5IndexIter *pIter, /* Iterator to advance */
202567 int bDesc, /* True if iterator is "rowid DESC" */
202568 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
202569 int *pRc, /* OUT: Error code */
202570 int *pbEof /* OUT: Set to true if EOF */
202571){
202572 i64 iLast = *piLast;
202573 i64 iRowid;
202574
202575 iRowid = pIter->iRowid;
202576 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
202577 int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
202578 if( rc || sqlite3Fts5IterEof(pIter) ){
202579 *pRc = rc;
202580 *pbEof = 1;
202581 return 1;
202582 }
202583 iRowid = pIter->iRowid;
202584 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) );
202585 }
202586 *piLast = iRowid;
202587
202588 return 0;
202589}
202590
202591static int fts5ExprSynonymAdvanceto(
202592 Fts5ExprTerm *pTerm, /* Term iterator to advance */
202593 int bDesc, /* True if iterator is "rowid DESC" */
202594 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
202595 int *pRc /* OUT: Error code */
202596){
202597 int rc = SQLITE_OK;
202598 i64 iLast = *piLast;
202599 Fts5ExprTerm *p;
202600 int bEof = 0;
202601
202602 for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){
202603 if( sqlite3Fts5IterEof(p->pIter)==0 ){
202604 i64 iRowid = p->pIter->iRowid;
202605 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
202606 rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
202607 }
202608 }
202609 }
202610
202611 if( rc!=SQLITE_OK ){
202612 *pRc = rc;
202613 bEof = 1;
202614 }else{
202615 *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof);
202616 }
202617 return bEof;
202618}
202619
202620
202621static int fts5ExprNearTest(
202622 int *pRc,
202623 Fts5Expr *pExpr, /* Expression that pNear is a part of */
202624 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */
202625){
202626 Fts5ExprNearset *pNear = pNode->pNear;
202627 int rc = *pRc;
202628
202629 if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL ){
202630 Fts5ExprTerm *pTerm;
202631 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
202632 pPhrase->poslist.n = 0;
202633 for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
202634 Fts5IndexIter *pIter = pTerm->pIter;
202635 if( sqlite3Fts5IterEof(pIter)==0 ){
202636 if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
202637 pPhrase->poslist.n = 1;
202638 }
202639 }
202640 }
202641 return pPhrase->poslist.n;
202642 }else{
202643 int i;
202644
202645 /* Check that each phrase in the nearset matches the current row.
202646 ** Populate the pPhrase->poslist buffers at the same time. If any
202647 ** phrase is not a match, break out of the loop early. */
202648 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
202649 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
202650 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym
202651 || pNear->pColset || pPhrase->aTerm[0].bFirst
202652 ){
202653 int bMatch = 0;
202654 rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
202655 if( bMatch==0 ) break;
202656 }else{
202657 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
202658 fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
202659 }
202660 }
202661
202662 *pRc = rc;
202663 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
202664 return 1;
202665 }
202666 return 0;
202667 }
202668}
202669
202670
202671/*
202672** Initialize all term iterators in the pNear object. If any term is found
202673** to match no documents at all, return immediately without initializing any
202674** further iterators.
202675**
202676** If an error occurs, return an SQLite error code. Otherwise, return
202677** SQLITE_OK. It is not considered an error if some term matches zero
202678** documents.
202679*/
202680static int fts5ExprNearInitAll(
202681 Fts5Expr *pExpr,
202682 Fts5ExprNode *pNode
202683){
202684 Fts5ExprNearset *pNear = pNode->pNear;
202685 int i;
202686
202687 assert( pNode->bNomatch==0 );
202688 for(i=0; i<pNear->nPhrase; i++){
202689 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
202690 if( pPhrase->nTerm==0 ){
202691 pNode->bEof = 1;
202692 return SQLITE_OK;
202693 }else{
202694 int j;
202695 for(j=0; j<pPhrase->nTerm; j++){
202696 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
202697 Fts5ExprTerm *p;
202698 int bHit = 0;
202699
202700 for(p=pTerm; p; p=p->pSynonym){
202701 int rc;
202702 if( p->pIter ){
202703 sqlite3Fts5IterClose(p->pIter);
202704 p->pIter = 0;
202705 }
202706 rc = sqlite3Fts5IndexQuery(
202707 pExpr->pIndex, p->zTerm, (int)strlen(p->zTerm),
202708 (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
202709 (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0),
202710 pNear->pColset,
202711 &p->pIter
202712 );
202713 assert( (rc==SQLITE_OK)==(p->pIter!=0) );
202714 if( rc!=SQLITE_OK ) return rc;
202715 if( 0==sqlite3Fts5IterEof(p->pIter) ){
202716 bHit = 1;
202717 }
202718 }
202719
202720 if( bHit==0 ){
202721 pNode->bEof = 1;
202722 return SQLITE_OK;
202723 }
202724 }
202725 }
202726 }
202727
202728 pNode->bEof = 0;
202729 return SQLITE_OK;
202730}
202731
202732/*
202733** If pExpr is an ASC iterator, this function returns a value with the
202734** same sign as:
202735**
202736** (iLhs - iRhs)
202737**
202738** Otherwise, if this is a DESC iterator, the opposite is returned:
202739**
202740** (iRhs - iLhs)
202741*/
202742static int fts5RowidCmp(
202743 Fts5Expr *pExpr,
202744 i64 iLhs,
202745 i64 iRhs
202746){
202747 assert( pExpr->bDesc==0 || pExpr->bDesc==1 );
202748 if( pExpr->bDesc==0 ){
202749 if( iLhs<iRhs ) return -1;
202750 return (iLhs > iRhs);
202751 }else{
202752 if( iLhs>iRhs ) return -1;
202753 return (iLhs < iRhs);
202754 }
202755}
202756
202757static void fts5ExprSetEof(Fts5ExprNode *pNode){
202758 int i;
202759 pNode->bEof = 1;
202760 pNode->bNomatch = 0;
202761 for(i=0; i<pNode->nChild; i++){
202762 fts5ExprSetEof(pNode->apChild[i]);
202763 }
202764}
202765
202766static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
202767 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
202768 Fts5ExprNearset *pNear = pNode->pNear;
202769 int i;
202770 for(i=0; i<pNear->nPhrase; i++){
202771 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
202772 pPhrase->poslist.n = 0;
202773 }
202774 }else{
202775 int i;
202776 for(i=0; i<pNode->nChild; i++){
202777 fts5ExprNodeZeroPoslist(pNode->apChild[i]);
202778 }
202779 }
202780}
202781
202782
202783
202784/*
202785** Compare the values currently indicated by the two nodes as follows:
202786**
202787** res = (*p1) - (*p2)
202788**
202789** Nodes that point to values that come later in the iteration order are
202790** considered to be larger. Nodes at EOF are the largest of all.
202791**
202792** This means that if the iteration order is ASC, then numerically larger
202793** rowids are considered larger. Or if it is the default DESC, numerically
202794** smaller rowids are larger.
202795*/
202796static int fts5NodeCompare(
202797 Fts5Expr *pExpr,
202798 Fts5ExprNode *p1,
202799 Fts5ExprNode *p2
202800){
202801 if( p2->bEof ) return -1;
202802 if( p1->bEof ) return +1;
202803 return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
202804}
202805
202806/*
202807** All individual term iterators in pNear are guaranteed to be valid when
202808** this function is called. This function checks if all term iterators
202809** point to the same rowid, and if not, advances them until they do.
202810** If an EOF is reached before this happens, *pbEof is set to true before
202811** returning.
202812**
202813** SQLITE_OK is returned if an error occurs, or an SQLite error code
202814** otherwise. It is not considered an error code if an iterator reaches
202815** EOF.
202816*/
202817static int fts5ExprNodeTest_STRING(
202818 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
202819 Fts5ExprNode *pNode
202820){
202821 Fts5ExprNearset *pNear = pNode->pNear;
202822 Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
202823 int rc = SQLITE_OK;
202824 i64 iLast; /* Lastest rowid any iterator points to */
202825 int i, j; /* Phrase and token index, respectively */
202826 int bMatch; /* True if all terms are at the same rowid */
202827 const int bDesc = pExpr->bDesc;
202828
202829 /* Check that this node should not be FTS5_TERM */
202830 assert( pNear->nPhrase>1
202831 || pNear->apPhrase[0]->nTerm>1
202832 || pNear->apPhrase[0]->aTerm[0].pSynonym
202833 || pNear->apPhrase[0]->aTerm[0].bFirst
202834 );
202835
202836 /* Initialize iLast, the "lastest" rowid any iterator points to. If the
202837 ** iterator skips through rowids in the default ascending order, this means
202838 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
202839 ** means the minimum rowid. */
202840 if( pLeft->aTerm[0].pSynonym ){
202841 iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
202842 }else{
202843 iLast = pLeft->aTerm[0].pIter->iRowid;
202844 }
202845
202846 do {
202847 bMatch = 1;
202848 for(i=0; i<pNear->nPhrase; i++){
202849 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
202850 for(j=0; j<pPhrase->nTerm; j++){
202851 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
202852 if( pTerm->pSynonym ){
202853 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
202854 if( iRowid==iLast ) continue;
202855 bMatch = 0;
202856 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
202857 pNode->bNomatch = 0;
202858 pNode->bEof = 1;
202859 return rc;
202860 }
202861 }else{
202862 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
202863 if( pIter->iRowid==iLast || pIter->bEof ) continue;
202864 bMatch = 0;
202865 if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
202866 return rc;
202867 }
202868 }
202869 }
202870 }
202871 }while( bMatch==0 );
202872
202873 pNode->iRowid = iLast;
202874 pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK);
202875 assert( pNode->bEof==0 || pNode->bNomatch==0 );
202876
202877 return rc;
202878}
202879
202880/*
202881** Advance the first term iterator in the first phrase of pNear. Set output
202882** variable *pbEof to true if it reaches EOF or if an error occurs.
202883**
202884** Return SQLITE_OK if successful, or an SQLite error code if an error
202885** occurs.
202886*/
202887static int fts5ExprNodeNext_STRING(
202888 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
202889 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
202890 int bFromValid,
202891 i64 iFrom
202892){
202893 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
202894 int rc = SQLITE_OK;
202895
202896 pNode->bNomatch = 0;
202897 if( pTerm->pSynonym ){
202898 int bEof = 1;
202899 Fts5ExprTerm *p;
202900
202901 /* Find the firstest rowid any synonym points to. */
202902 i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
202903
202904 /* Advance each iterator that currently points to iRowid. Or, if iFrom
202905 ** is valid - each iterator that points to a rowid before iFrom. */
202906 for(p=pTerm; p; p=p->pSynonym){
202907 if( sqlite3Fts5IterEof(p->pIter)==0 ){
202908 i64 ii = p->pIter->iRowid;
202909 if( ii==iRowid
202910 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
202911 ){
202912 if( bFromValid ){
202913 rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
202914 }else{
202915 rc = sqlite3Fts5IterNext(p->pIter);
202916 }
202917 if( rc!=SQLITE_OK ) break;
202918 if( sqlite3Fts5IterEof(p->pIter)==0 ){
202919 bEof = 0;
202920 }
202921 }else{
202922 bEof = 0;
202923 }
202924 }
202925 }
202926
202927 /* Set the EOF flag if either all synonym iterators are at EOF or an
202928 ** error has occurred. */
202929 pNode->bEof = (rc || bEof);
202930 }else{
202931 Fts5IndexIter *pIter = pTerm->pIter;
202932
202933 assert( Fts5NodeIsString(pNode) );
202934 if( bFromValid ){
202935 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
202936 }else{
202937 rc = sqlite3Fts5IterNext(pIter);
202938 }
202939
202940 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
202941 }
202942
202943 if( pNode->bEof==0 ){
202944 assert( rc==SQLITE_OK );
202945 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
202946 }
202947
202948 return rc;
202949}
202950
202951
202952static int fts5ExprNodeTest_TERM(
202953 Fts5Expr *pExpr, /* Expression that pNear is a part of */
202954 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
202955){
202956 /* As this "NEAR" object is actually a single phrase that consists
202957 ** of a single term only, grab pointers into the poslist managed by the
202958 ** fts5_index.c iterator object. This is much faster than synthesizing
202959 ** a new poslist the way we have to for more complicated phrase or NEAR
202960 ** expressions. */
202961 Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
202962 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
202963
202964 assert( pNode->eType==FTS5_TERM );
202965 assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 );
202966 assert( pPhrase->aTerm[0].pSynonym==0 );
202967
202968 pPhrase->poslist.n = pIter->nData;
202969 if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){
202970 pPhrase->poslist.p = (u8*)pIter->pData;
202971 }
202972 pNode->iRowid = pIter->iRowid;
202973 pNode->bNomatch = (pPhrase->poslist.n==0);
202974 return SQLITE_OK;
202975}
202976
202977/*
202978** xNext() method for a node of type FTS5_TERM.
202979*/
202980static int fts5ExprNodeNext_TERM(
202981 Fts5Expr *pExpr,
202982 Fts5ExprNode *pNode,
202983 int bFromValid,
202984 i64 iFrom
202985){
202986 int rc;
202987 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
202988
202989 assert( pNode->bEof==0 );
202990 if( bFromValid ){
202991 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
202992 }else{
202993 rc = sqlite3Fts5IterNext(pIter);
202994 }
202995 if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
202996 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
202997 }else{
202998 pNode->bEof = 1;
202999 pNode->bNomatch = 0;
203000 }
203001 return rc;
203002}
203003
203004static void fts5ExprNodeTest_OR(
203005 Fts5Expr *pExpr, /* Expression of which pNode is a part */
203006 Fts5ExprNode *pNode /* Expression node to test */
203007){
203008 Fts5ExprNode *pNext = pNode->apChild[0];
203009 int i;
203010
203011 for(i=1; i<pNode->nChild; i++){
203012 Fts5ExprNode *pChild = pNode->apChild[i];
203013 int cmp = fts5NodeCompare(pExpr, pNext, pChild);
203014 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
203015 pNext = pChild;
203016 }
203017 }
203018 pNode->iRowid = pNext->iRowid;
203019 pNode->bEof = pNext->bEof;
203020 pNode->bNomatch = pNext->bNomatch;
203021}
203022
203023static int fts5ExprNodeNext_OR(
203024 Fts5Expr *pExpr,
203025 Fts5ExprNode *pNode,
203026 int bFromValid,
203027 i64 iFrom
203028){
203029 int i;
203030 i64 iLast = pNode->iRowid;
203031
203032 for(i=0; i<pNode->nChild; i++){
203033 Fts5ExprNode *p1 = pNode->apChild[i];
203034 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
203035 if( p1->bEof==0 ){
203036 if( (p1->iRowid==iLast)
203037 || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
203038 ){
203039 int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
203040 if( rc!=SQLITE_OK ){
203041 pNode->bNomatch = 0;
203042 return rc;
203043 }
203044 }
203045 }
203046 }
203047
203048 fts5ExprNodeTest_OR(pExpr, pNode);
203049 return SQLITE_OK;
203050}
203051
203052/*
203053** Argument pNode is an FTS5_AND node.
203054*/
203055static int fts5ExprNodeTest_AND(
203056 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
203057 Fts5ExprNode *pAnd /* FTS5_AND node to advance */
203058){
203059 int iChild;
203060 i64 iLast = pAnd->iRowid;
203061 int rc = SQLITE_OK;
203062 int bMatch;
203063
203064 assert( pAnd->bEof==0 );
203065 do {
203066 pAnd->bNomatch = 0;
203067 bMatch = 1;
203068 for(iChild=0; iChild<pAnd->nChild; iChild++){
203069 Fts5ExprNode *pChild = pAnd->apChild[iChild];
203070 int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
203071 if( cmp>0 ){
203072 /* Advance pChild until it points to iLast or laster */
203073 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
203074 if( rc!=SQLITE_OK ){
203075 pAnd->bNomatch = 0;
203076 return rc;
203077 }
203078 }
203079
203080 /* If the child node is now at EOF, so is the parent AND node. Otherwise,
203081 ** the child node is guaranteed to have advanced at least as far as
203082 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
203083 ** new lastest rowid seen so far. */
203084 assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 );
203085 if( pChild->bEof ){
203086 fts5ExprSetEof(pAnd);
203087 bMatch = 1;
203088 break;
203089 }else if( iLast!=pChild->iRowid ){
203090 bMatch = 0;
203091 iLast = pChild->iRowid;
203092 }
203093
203094 if( pChild->bNomatch ){
203095 pAnd->bNomatch = 1;
203096 }
203097 }
203098 }while( bMatch==0 );
203099
203100 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
203101 fts5ExprNodeZeroPoslist(pAnd);
203102 }
203103 pAnd->iRowid = iLast;
203104 return SQLITE_OK;
203105}
203106
203107static int fts5ExprNodeNext_AND(
203108 Fts5Expr *pExpr,
203109 Fts5ExprNode *pNode,
203110 int bFromValid,
203111 i64 iFrom
203112){
203113 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
203114 if( rc==SQLITE_OK ){
203115 rc = fts5ExprNodeTest_AND(pExpr, pNode);
203116 }else{
203117 pNode->bNomatch = 0;
203118 }
203119 return rc;
203120}
203121
203122static int fts5ExprNodeTest_NOT(
203123 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
203124 Fts5ExprNode *pNode /* FTS5_NOT node to advance */
203125){
203126 int rc = SQLITE_OK;
203127 Fts5ExprNode *p1 = pNode->apChild[0];
203128 Fts5ExprNode *p2 = pNode->apChild[1];
203129 assert( pNode->nChild==2 );
203130
203131 while( rc==SQLITE_OK && p1->bEof==0 ){
203132 int cmp = fts5NodeCompare(pExpr, p1, p2);
203133 if( cmp>0 ){
203134 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
203135 cmp = fts5NodeCompare(pExpr, p1, p2);
203136 }
203137 assert( rc!=SQLITE_OK || cmp<=0 );
203138 if( cmp || p2->bNomatch ) break;
203139 rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
203140 }
203141 pNode->bEof = p1->bEof;
203142 pNode->bNomatch = p1->bNomatch;
203143 pNode->iRowid = p1->iRowid;
203144 if( p1->bEof ){
203145 fts5ExprNodeZeroPoslist(p2);
203146 }
203147 return rc;
203148}
203149
203150static int fts5ExprNodeNext_NOT(
203151 Fts5Expr *pExpr,
203152 Fts5ExprNode *pNode,
203153 int bFromValid,
203154 i64 iFrom
203155){
203156 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
203157 if( rc==SQLITE_OK ){
203158 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
203159 }
203160 if( rc!=SQLITE_OK ){
203161 pNode->bNomatch = 0;
203162 }
203163 return rc;
203164}
203165
203166/*
203167** If pNode currently points to a match, this function returns SQLITE_OK
203168** without modifying it. Otherwise, pNode is advanced until it does point
203169** to a match or EOF is reached.
203170*/
203171static int fts5ExprNodeTest(
203172 Fts5Expr *pExpr, /* Expression of which pNode is a part */
203173 Fts5ExprNode *pNode /* Expression node to test */
203174){
203175 int rc = SQLITE_OK;
203176 if( pNode->bEof==0 ){
203177 switch( pNode->eType ){
203178
203179 case FTS5_STRING: {
203180 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
203181 break;
203182 }
203183
203184 case FTS5_TERM: {
203185 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
203186 break;
203187 }
203188
203189 case FTS5_AND: {
203190 rc = fts5ExprNodeTest_AND(pExpr, pNode);
203191 break;
203192 }
203193
203194 case FTS5_OR: {
203195 fts5ExprNodeTest_OR(pExpr, pNode);
203196 break;
203197 }
203198
203199 default: assert( pNode->eType==FTS5_NOT ); {
203200 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
203201 break;
203202 }
203203 }
203204 }
203205 return rc;
203206}
203207
203208
203209/*
203210** Set node pNode, which is part of expression pExpr, to point to the first
203211** match. If there are no matches, set the Node.bEof flag to indicate EOF.
203212**
203213** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
203214** It is not an error if there are no matches.
203215*/
203216static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
203217 int rc = SQLITE_OK;
203218 pNode->bEof = 0;
203219 pNode->bNomatch = 0;
203220
203221 if( Fts5NodeIsString(pNode) ){
203222 /* Initialize all term iterators in the NEAR object. */
203223 rc = fts5ExprNearInitAll(pExpr, pNode);
203224 }else if( pNode->xNext==0 ){
203225 pNode->bEof = 1;
203226 }else{
203227 int i;
203228 int nEof = 0;
203229 for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){
203230 Fts5ExprNode *pChild = pNode->apChild[i];
203231 rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
203232 assert( pChild->bEof==0 || pChild->bEof==1 );
203233 nEof += pChild->bEof;
203234 }
203235 pNode->iRowid = pNode->apChild[0]->iRowid;
203236
203237 switch( pNode->eType ){
203238 case FTS5_AND:
203239 if( nEof>0 ) fts5ExprSetEof(pNode);
203240 break;
203241
203242 case FTS5_OR:
203243 if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
203244 break;
203245
203246 default:
203247 assert( pNode->eType==FTS5_NOT );
203248 pNode->bEof = pNode->apChild[0]->bEof;
203249 break;
203250 }
203251 }
203252
203253 if( rc==SQLITE_OK ){
203254 rc = fts5ExprNodeTest(pExpr, pNode);
203255 }
203256 return rc;
203257}
203258
203259
203260/*
203261** Begin iterating through the set of documents in index pIdx matched by
203262** the MATCH expression passed as the first argument. If the "bDesc"
203263** parameter is passed a non-zero value, iteration is in descending rowid
203264** order. Or, if it is zero, in ascending order.
203265**
203266** If iterating in ascending rowid order (bDesc==0), the first document
203267** visited is that with the smallest rowid that is larger than or equal
203268** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
203269** then the first document visited must have a rowid smaller than or
203270** equal to iFirst.
203271**
203272** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
203273** is not considered an error if the query does not match any documents.
203274*/
203275static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
203276 Fts5ExprNode *pRoot = p->pRoot;
203277 int rc; /* Return code */
203278
203279 p->pIndex = pIdx;
203280 p->bDesc = bDesc;
203281 rc = fts5ExprNodeFirst(p, pRoot);
203282
203283 /* If not at EOF but the current rowid occurs earlier than iFirst in
203284 ** the iteration order, move to document iFirst or later. */
203285 if( rc==SQLITE_OK
203286 && 0==pRoot->bEof
203287 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0
203288 ){
203289 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
203290 }
203291
203292 /* If the iterator is not at a real match, skip forward until it is. */
203293 while( pRoot->bNomatch ){
203294 assert( pRoot->bEof==0 && rc==SQLITE_OK );
203295 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
203296 }
203297 return rc;
203298}
203299
203300/*
203301** Move to the next document
203302**
203303** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
203304** is not considered an error if the query does not match any documents.
203305*/
203306static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
203307 int rc;
203308 Fts5ExprNode *pRoot = p->pRoot;
203309 assert( pRoot->bEof==0 && pRoot->bNomatch==0 );
203310 do {
203311 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
203312 assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) );
203313 }while( pRoot->bNomatch );
203314 if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
203315 pRoot->bEof = 1;
203316 }
203317 return rc;
203318}
203319
203320static int sqlite3Fts5ExprEof(Fts5Expr *p){
203321 return p->pRoot->bEof;
203322}
203323
203324static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
203325 return p->pRoot->iRowid;
203326}
203327
203328static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
203329 int rc = SQLITE_OK;
203330 *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n);
203331 return rc;
203332}
203333
203334/*
203335** Free the phrase object passed as the only argument.
203336*/
203337static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
203338 if( pPhrase ){
203339 int i;
203340 for(i=0; i<pPhrase->nTerm; i++){
203341 Fts5ExprTerm *pSyn;
203342 Fts5ExprTerm *pNext;
203343 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
203344 sqlite3_free(pTerm->zTerm);
203345 sqlite3Fts5IterClose(pTerm->pIter);
203346 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
203347 pNext = pSyn->pSynonym;
203348 sqlite3Fts5IterClose(pSyn->pIter);
203349 fts5BufferFree((Fts5Buffer*)&pSyn[1]);
203350 sqlite3_free(pSyn);
203351 }
203352 }
203353 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
203354 sqlite3_free(pPhrase);
203355 }
203356}
203357
203358/*
203359** Set the "bFirst" flag on the first token of the phrase passed as the
203360** only argument.
203361*/
203362static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
203363 if( pPhrase && pPhrase->nTerm ){
203364 pPhrase->aTerm[0].bFirst = 1;
203365 }
203366}
203367
203368/*
203369** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
203370** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
203371** appended to it and the results returned.
203372**
203373** If an OOM error occurs, both the pNear and pPhrase objects are freed and
203374** NULL returned.
203375*/
203376static Fts5ExprNearset *sqlite3Fts5ParseNearset(
203377 Fts5Parse *pParse, /* Parse context */
203378 Fts5ExprNearset *pNear, /* Existing nearset, or NULL */
203379 Fts5ExprPhrase *pPhrase /* Recently parsed phrase */
203380){
203381 const int SZALLOC = 8;
203382 Fts5ExprNearset *pRet = 0;
203383
203384 if( pParse->rc==SQLITE_OK ){
203385 if( pPhrase==0 ){
203386 return pNear;
203387 }
203388 if( pNear==0 ){
203389 int nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*);
203390 pRet = sqlite3_malloc(nByte);
203391 if( pRet==0 ){
203392 pParse->rc = SQLITE_NOMEM;
203393 }else{
203394 memset(pRet, 0, nByte);
203395 }
203396 }else if( (pNear->nPhrase % SZALLOC)==0 ){
203397 int nNew = pNear->nPhrase + SZALLOC;
203398 int nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*);
203399
203400 pRet = (Fts5ExprNearset*)sqlite3_realloc(pNear, nByte);
203401 if( pRet==0 ){
203402 pParse->rc = SQLITE_NOMEM;
203403 }
203404 }else{
203405 pRet = pNear;
203406 }
203407 }
203408
203409 if( pRet==0 ){
203410 assert( pParse->rc!=SQLITE_OK );
203411 sqlite3Fts5ParseNearsetFree(pNear);
203412 sqlite3Fts5ParsePhraseFree(pPhrase);
203413 }else{
203414 if( pRet->nPhrase>0 ){
203415 Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
203416 assert( pLast==pParse->apPhrase[pParse->nPhrase-2] );
203417 if( pPhrase->nTerm==0 ){
203418 fts5ExprPhraseFree(pPhrase);
203419 pRet->nPhrase--;
203420 pParse->nPhrase--;
203421 pPhrase = pLast;
203422 }else if( pLast->nTerm==0 ){
203423 fts5ExprPhraseFree(pLast);
203424 pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
203425 pParse->nPhrase--;
203426 pRet->nPhrase--;
203427 }
203428 }
203429 pRet->apPhrase[pRet->nPhrase++] = pPhrase;
203430 }
203431 return pRet;
203432}
203433
203434typedef struct TokenCtx TokenCtx;
203435struct TokenCtx {
203436 Fts5ExprPhrase *pPhrase;
203437 int rc;
203438};
203439
203440/*
203441** Callback for tokenizing terms used by ParseTerm().
203442*/
203443static int fts5ParseTokenize(
203444 void *pContext, /* Pointer to Fts5InsertCtx object */
203445 int tflags, /* Mask of FTS5_TOKEN_* flags */
203446 const char *pToken, /* Buffer containing token */
203447 int nToken, /* Size of token in bytes */
203448 int iUnused1, /* Start offset of token */
203449 int iUnused2 /* End offset of token */
203450){
203451 int rc = SQLITE_OK;
203452 const int SZALLOC = 8;
203453 TokenCtx *pCtx = (TokenCtx*)pContext;
203454 Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
203455
203456 UNUSED_PARAM2(iUnused1, iUnused2);
203457
203458 /* If an error has already occurred, this is a no-op */
203459 if( pCtx->rc!=SQLITE_OK ) return pCtx->rc;
203460 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
203461
203462 if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED) ){
203463 Fts5ExprTerm *pSyn;
203464 int nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
203465 pSyn = (Fts5ExprTerm*)sqlite3_malloc(nByte);
203466 if( pSyn==0 ){
203467 rc = SQLITE_NOMEM;
203468 }else{
203469 memset(pSyn, 0, nByte);
203470 pSyn->zTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
203471 memcpy(pSyn->zTerm, pToken, nToken);
203472 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
203473 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
203474 }
203475 }else{
203476 Fts5ExprTerm *pTerm;
203477 if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
203478 Fts5ExprPhrase *pNew;
203479 int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
203480
203481 pNew = (Fts5ExprPhrase*)sqlite3_realloc(pPhrase,
203482 sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew
203483 );
203484 if( pNew==0 ){
203485 rc = SQLITE_NOMEM;
203486 }else{
203487 if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase));
203488 pCtx->pPhrase = pPhrase = pNew;
203489 pNew->nTerm = nNew - SZALLOC;
203490 }
203491 }
203492
203493 if( rc==SQLITE_OK ){
203494 pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
203495 memset(pTerm, 0, sizeof(Fts5ExprTerm));
203496 pTerm->zTerm = sqlite3Fts5Strndup(&rc, pToken, nToken);
203497 }
203498 }
203499
203500 pCtx->rc = rc;
203501 return rc;
203502}
203503
203504
203505/*
203506** Free the phrase object passed as the only argument.
203507*/
203508static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
203509 fts5ExprPhraseFree(pPhrase);
203510}
203511
203512/*
203513** Free the phrase object passed as the second argument.
203514*/
203515static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
203516 if( pNear ){
203517 int i;
203518 for(i=0; i<pNear->nPhrase; i++){
203519 fts5ExprPhraseFree(pNear->apPhrase[i]);
203520 }
203521 sqlite3_free(pNear->pColset);
203522 sqlite3_free(pNear);
203523 }
203524}
203525
203526static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
203527 assert( pParse->pExpr==0 );
203528 pParse->pExpr = p;
203529}
203530
203531/*
203532** This function is called by the parser to process a string token. The
203533** string may or may not be quoted. In any case it is tokenized and a
203534** phrase object consisting of all tokens returned.
203535*/
203536static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
203537 Fts5Parse *pParse, /* Parse context */
203538 Fts5ExprPhrase *pAppend, /* Phrase to append to */
203539 Fts5Token *pToken, /* String to tokenize */
203540 int bPrefix /* True if there is a trailing "*" */
203541){
203542 Fts5Config *pConfig = pParse->pConfig;
203543 TokenCtx sCtx; /* Context object passed to callback */
203544 int rc; /* Tokenize return code */
203545 char *z = 0;
203546
203547 memset(&sCtx, 0, sizeof(TokenCtx));
203548 sCtx.pPhrase = pAppend;
203549
203550 rc = fts5ParseStringFromToken(pToken, &z);
203551 if( rc==SQLITE_OK ){
203552 int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
203553 int n;
203554 sqlite3Fts5Dequote(z);
203555 n = (int)strlen(z);
203556 rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
203557 }
203558 sqlite3_free(z);
203559 if( rc || (rc = sCtx.rc) ){
203560 pParse->rc = rc;
203561 fts5ExprPhraseFree(sCtx.pPhrase);
203562 sCtx.pPhrase = 0;
203563 }else{
203564
203565 if( pAppend==0 ){
203566 if( (pParse->nPhrase % 8)==0 ){
203567 int nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
203568 Fts5ExprPhrase **apNew;
203569 apNew = (Fts5ExprPhrase**)sqlite3_realloc(pParse->apPhrase, nByte);
203570 if( apNew==0 ){
203571 pParse->rc = SQLITE_NOMEM;
203572 fts5ExprPhraseFree(sCtx.pPhrase);
203573 return 0;
203574 }
203575 pParse->apPhrase = apNew;
203576 }
203577 pParse->nPhrase++;
203578 }
203579
203580 if( sCtx.pPhrase==0 ){
203581 /* This happens when parsing a token or quoted phrase that contains
203582 ** no token characters at all. (e.g ... MATCH '""'). */
203583 sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
203584 }else if( sCtx.pPhrase->nTerm ){
203585 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
203586 }
203587 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
203588 }
203589
203590 return sCtx.pPhrase;
203591}
203592
203593/*
203594** Create a new FTS5 expression by cloning phrase iPhrase of the
203595** expression passed as the second argument.
203596*/
203597static int sqlite3Fts5ExprClonePhrase(
203598 Fts5Expr *pExpr,
203599 int iPhrase,
203600 Fts5Expr **ppNew
203601){
203602 int rc = SQLITE_OK; /* Return code */
203603 Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */
203604 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
203605 TokenCtx sCtx = {0,0}; /* Context object for fts5ParseTokenize */
203606
203607 pOrig = pExpr->apExprPhrase[iPhrase];
203608 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
203609 if( rc==SQLITE_OK ){
203610 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
203611 sizeof(Fts5ExprPhrase*));
203612 }
203613 if( rc==SQLITE_OK ){
203614 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc,
203615 sizeof(Fts5ExprNode));
203616 }
203617 if( rc==SQLITE_OK ){
203618 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
203619 sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
203620 }
203621 if( rc==SQLITE_OK ){
203622 Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
203623 if( pColsetOrig ){
203624 int nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
203625 Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte);
203626 if( pColset ){
203627 memcpy(pColset, pColsetOrig, nByte);
203628 }
203629 pNew->pRoot->pNear->pColset = pColset;
203630 }
203631 }
203632
203633 if( pOrig->nTerm ){
203634 int i; /* Used to iterate through phrase terms */
203635 for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
203636 int tflags = 0;
203637 Fts5ExprTerm *p;
203638 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
203639 const char *zTerm = p->zTerm;
203640 rc = fts5ParseTokenize((void*)&sCtx, tflags, zTerm, (int)strlen(zTerm),
203641 0, 0);
203642 tflags = FTS5_TOKEN_COLOCATED;
203643 }
203644 if( rc==SQLITE_OK ){
203645 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
203646 sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
203647 }
203648 }
203649 }else{
203650 /* This happens when parsing a token or quoted phrase that contains
203651 ** no token characters at all. (e.g ... MATCH '""'). */
203652 sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
203653 }
203654
203655 if( rc==SQLITE_OK ){
203656 /* All the allocations succeeded. Put the expression object together. */
203657 pNew->pIndex = pExpr->pIndex;
203658 pNew->pConfig = pExpr->pConfig;
203659 pNew->nPhrase = 1;
203660 pNew->apExprPhrase[0] = sCtx.pPhrase;
203661 pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
203662 pNew->pRoot->pNear->nPhrase = 1;
203663 sCtx.pPhrase->pNode = pNew->pRoot;
203664
203665 if( pOrig->nTerm==1
203666 && pOrig->aTerm[0].pSynonym==0
203667 && pOrig->aTerm[0].bFirst==0
203668 ){
203669 pNew->pRoot->eType = FTS5_TERM;
203670 pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
203671 }else{
203672 pNew->pRoot->eType = FTS5_STRING;
203673 pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
203674 }
203675 }else{
203676 sqlite3Fts5ExprFree(pNew);
203677 fts5ExprPhraseFree(sCtx.pPhrase);
203678 pNew = 0;
203679 }
203680
203681 *ppNew = pNew;
203682 return rc;
203683}
203684
203685
203686/*
203687** Token pTok has appeared in a MATCH expression where the NEAR operator
203688** is expected. If token pTok does not contain "NEAR", store an error
203689** in the pParse object.
203690*/
203691static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
203692 if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){
203693 sqlite3Fts5ParseError(
203694 pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
203695 );
203696 }
203697}
203698
203699static void sqlite3Fts5ParseSetDistance(
203700 Fts5Parse *pParse,
203701 Fts5ExprNearset *pNear,
203702 Fts5Token *p
203703){
203704 if( pNear ){
203705 int nNear = 0;
203706 int i;
203707 if( p->n ){
203708 for(i=0; i<p->n; i++){
203709 char c = (char)p->p[i];
203710 if( c<'0' || c>'9' ){
203711 sqlite3Fts5ParseError(
203712 pParse, "expected integer, got \"%.*s\"", p->n, p->p
203713 );
203714 return;
203715 }
203716 nNear = nNear * 10 + (p->p[i] - '0');
203717 }
203718 }else{
203719 nNear = FTS5_DEFAULT_NEARDIST;
203720 }
203721 pNear->nNear = nNear;
203722 }
203723}
203724
203725/*
203726** The second argument passed to this function may be NULL, or it may be
203727** an existing Fts5Colset object. This function returns a pointer to
203728** a new colset object containing the contents of (p) with new value column
203729** number iCol appended.
203730**
203731** If an OOM error occurs, store an error code in pParse and return NULL.
203732** The old colset object (if any) is not freed in this case.
203733*/
203734static Fts5Colset *fts5ParseColset(
203735 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
203736 Fts5Colset *p, /* Existing colset object */
203737 int iCol /* New column to add to colset object */
203738){
203739 int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */
203740 Fts5Colset *pNew; /* New colset object to return */
203741
203742 assert( pParse->rc==SQLITE_OK );
203743 assert( iCol>=0 && iCol<pParse->pConfig->nCol );
203744
203745 pNew = sqlite3_realloc(p, sizeof(Fts5Colset) + sizeof(int)*nCol);
203746 if( pNew==0 ){
203747 pParse->rc = SQLITE_NOMEM;
203748 }else{
203749 int *aiCol = pNew->aiCol;
203750 int i, j;
203751 for(i=0; i<nCol; i++){
203752 if( aiCol[i]==iCol ) return pNew;
203753 if( aiCol[i]>iCol ) break;
203754 }
203755 for(j=nCol; j>i; j--){
203756 aiCol[j] = aiCol[j-1];
203757 }
203758 aiCol[i] = iCol;
203759 pNew->nCol = nCol+1;
203760
203761#ifndef NDEBUG
203762 /* Check that the array is in order and contains no duplicate entries. */
203763 for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] );
203764#endif
203765 }
203766
203767 return pNew;
203768}
203769
203770/*
203771** Allocate and return an Fts5Colset object specifying the inverse of
203772** the colset passed as the second argument. Free the colset passed
203773** as the second argument before returning.
203774*/
203775static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
203776 Fts5Colset *pRet;
203777 int nCol = pParse->pConfig->nCol;
203778
203779 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc,
203780 sizeof(Fts5Colset) + sizeof(int)*nCol
203781 );
203782 if( pRet ){
203783 int i;
203784 int iOld = 0;
203785 for(i=0; i<nCol; i++){
203786 if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
203787 pRet->aiCol[pRet->nCol++] = i;
203788 }else{
203789 iOld++;
203790 }
203791 }
203792 }
203793
203794 sqlite3_free(p);
203795 return pRet;
203796}
203797
203798static Fts5Colset *sqlite3Fts5ParseColset(
203799 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
203800 Fts5Colset *pColset, /* Existing colset object */
203801 Fts5Token *p
203802){
203803 Fts5Colset *pRet = 0;
203804 int iCol;
203805 char *z; /* Dequoted copy of token p */
203806
203807 z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n);
203808 if( pParse->rc==SQLITE_OK ){
203809 Fts5Config *pConfig = pParse->pConfig;
203810 sqlite3Fts5Dequote(z);
203811 for(iCol=0; iCol<pConfig->nCol; iCol++){
203812 if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break;
203813 }
203814 if( iCol==pConfig->nCol ){
203815 sqlite3Fts5ParseError(pParse, "no such column: %s", z);
203816 }else{
203817 pRet = fts5ParseColset(pParse, pColset, iCol);
203818 }
203819 sqlite3_free(z);
203820 }
203821
203822 if( pRet==0 ){
203823 assert( pParse->rc!=SQLITE_OK );
203824 sqlite3_free(pColset);
203825 }
203826
203827 return pRet;
203828}
203829
203830/*
203831** If argument pOrig is NULL, or if (*pRc) is set to anything other than
203832** SQLITE_OK when this function is called, NULL is returned.
203833**
203834** Otherwise, a copy of (*pOrig) is made into memory obtained from
203835** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
203836** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
203837*/
203838static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){
203839 Fts5Colset *pRet;
203840 if( pOrig ){
203841 int nByte = sizeof(Fts5Colset) + (pOrig->nCol-1) * sizeof(int);
203842 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte);
203843 if( pRet ){
203844 memcpy(pRet, pOrig, nByte);
203845 }
203846 }else{
203847 pRet = 0;
203848 }
203849 return pRet;
203850}
203851
203852/*
203853** Remove from colset pColset any columns that are not also in colset pMerge.
203854*/
203855static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){
203856 int iIn = 0; /* Next input in pColset */
203857 int iMerge = 0; /* Next input in pMerge */
203858 int iOut = 0; /* Next output slot in pColset */
203859
203860 while( iIn<pColset->nCol && iMerge<pMerge->nCol ){
203861 int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge];
203862 if( iDiff==0 ){
203863 pColset->aiCol[iOut++] = pMerge->aiCol[iMerge];
203864 iMerge++;
203865 iIn++;
203866 }else if( iDiff>0 ){
203867 iMerge++;
203868 }else{
203869 iIn++;
203870 }
203871 }
203872 pColset->nCol = iOut;
203873}
203874
203875/*
203876** Recursively apply colset pColset to expression node pNode and all of
203877** its decendents. If (*ppFree) is not NULL, it contains a spare copy
203878** of pColset. This function may use the spare copy and set (*ppFree) to
203879** zero, or it may create copies of pColset using fts5CloneColset().
203880*/
203881static void fts5ParseSetColset(
203882 Fts5Parse *pParse,
203883 Fts5ExprNode *pNode,
203884 Fts5Colset *pColset,
203885 Fts5Colset **ppFree
203886){
203887 if( pParse->rc==SQLITE_OK ){
203888 assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING
203889 || pNode->eType==FTS5_AND || pNode->eType==FTS5_OR
203890 || pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF
203891 );
203892 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
203893 Fts5ExprNearset *pNear = pNode->pNear;
203894 if( pNear->pColset ){
203895 fts5MergeColset(pNear->pColset, pColset);
203896 if( pNear->pColset->nCol==0 ){
203897 pNode->eType = FTS5_EOF;
203898 pNode->xNext = 0;
203899 }
203900 }else if( *ppFree ){
203901 pNear->pColset = pColset;
203902 *ppFree = 0;
203903 }else{
203904 pNear->pColset = fts5CloneColset(&pParse->rc, pColset);
203905 }
203906 }else{
203907 int i;
203908 assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 );
203909 for(i=0; i<pNode->nChild; i++){
203910 fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree);
203911 }
203912 }
203913 }
203914}
203915
203916/*
203917** Apply colset pColset to expression node pExpr and all of its descendents.
203918*/
203919static void sqlite3Fts5ParseSetColset(
203920 Fts5Parse *pParse,
203921 Fts5ExprNode *pExpr,
203922 Fts5Colset *pColset
203923){
203924 Fts5Colset *pFree = pColset;
203925 if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){
203926 pParse->rc = SQLITE_ERROR;
203927 pParse->zErr = sqlite3_mprintf(
203928 "fts5: column queries are not supported (detail=none)"
203929 );
203930 }else{
203931 fts5ParseSetColset(pParse, pExpr, pColset, &pFree);
203932 }
203933 sqlite3_free(pFree);
203934}
203935
203936static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
203937 switch( pNode->eType ){
203938 case FTS5_STRING: {
203939 Fts5ExprNearset *pNear = pNode->pNear;
203940 if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
203941 && pNear->apPhrase[0]->aTerm[0].pSynonym==0
203942 && pNear->apPhrase[0]->aTerm[0].bFirst==0
203943 ){
203944 pNode->eType = FTS5_TERM;
203945 pNode->xNext = fts5ExprNodeNext_TERM;
203946 }else{
203947 pNode->xNext = fts5ExprNodeNext_STRING;
203948 }
203949 break;
203950 };
203951
203952 case FTS5_OR: {
203953 pNode->xNext = fts5ExprNodeNext_OR;
203954 break;
203955 };
203956
203957 case FTS5_AND: {
203958 pNode->xNext = fts5ExprNodeNext_AND;
203959 break;
203960 };
203961
203962 default: assert( pNode->eType==FTS5_NOT ); {
203963 pNode->xNext = fts5ExprNodeNext_NOT;
203964 break;
203965 };
203966 }
203967}
203968
203969static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
203970 if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
203971 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
203972 memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
203973 p->nChild += pSub->nChild;
203974 sqlite3_free(pSub);
203975 }else{
203976 p->apChild[p->nChild++] = pSub;
203977 }
203978}
203979
203980/*
203981** Allocate and return a new expression object. If anything goes wrong (i.e.
203982** OOM error), leave an error code in pParse and return NULL.
203983*/
203984static Fts5ExprNode *sqlite3Fts5ParseNode(
203985 Fts5Parse *pParse, /* Parse context */
203986 int eType, /* FTS5_STRING, AND, OR or NOT */
203987 Fts5ExprNode *pLeft, /* Left hand child expression */
203988 Fts5ExprNode *pRight, /* Right hand child expression */
203989 Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */
203990){
203991 Fts5ExprNode *pRet = 0;
203992
203993 if( pParse->rc==SQLITE_OK ){
203994 int nChild = 0; /* Number of children of returned node */
203995 int nByte; /* Bytes of space to allocate for this node */
203996
203997 assert( (eType!=FTS5_STRING && !pNear)
203998 || (eType==FTS5_STRING && !pLeft && !pRight)
203999 );
204000 if( eType==FTS5_STRING && pNear==0 ) return 0;
204001 if( eType!=FTS5_STRING && pLeft==0 ) return pRight;
204002 if( eType!=FTS5_STRING && pRight==0 ) return pLeft;
204003
204004 if( eType==FTS5_NOT ){
204005 nChild = 2;
204006 }else if( eType==FTS5_AND || eType==FTS5_OR ){
204007 nChild = 2;
204008 if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
204009 if( pRight->eType==eType ) nChild += pRight->nChild-1;
204010 }
204011
204012 nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1);
204013 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
204014
204015 if( pRet ){
204016 pRet->eType = eType;
204017 pRet->pNear = pNear;
204018 fts5ExprAssignXNext(pRet);
204019 if( eType==FTS5_STRING ){
204020 int iPhrase;
204021 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
204022 pNear->apPhrase[iPhrase]->pNode = pRet;
204023 if( pNear->apPhrase[iPhrase]->nTerm==0 ){
204024 pRet->xNext = 0;
204025 pRet->eType = FTS5_EOF;
204026 }
204027 }
204028
204029 if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){
204030 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
204031 if( pNear->nPhrase!=1
204032 || pPhrase->nTerm>1
204033 || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
204034 ){
204035 assert( pParse->rc==SQLITE_OK );
204036 pParse->rc = SQLITE_ERROR;
204037 assert( pParse->zErr==0 );
204038 pParse->zErr = sqlite3_mprintf(
204039 "fts5: %s queries are not supported (detail!=full)",
204040 pNear->nPhrase==1 ? "phrase": "NEAR"
204041 );
204042 sqlite3_free(pRet);
204043 pRet = 0;
204044 }
204045 }
204046 }else{
204047 fts5ExprAddChildren(pRet, pLeft);
204048 fts5ExprAddChildren(pRet, pRight);
204049 }
204050 }
204051 }
204052
204053 if( pRet==0 ){
204054 assert( pParse->rc!=SQLITE_OK );
204055 sqlite3Fts5ParseNodeFree(pLeft);
204056 sqlite3Fts5ParseNodeFree(pRight);
204057 sqlite3Fts5ParseNearsetFree(pNear);
204058 }
204059 return pRet;
204060}
204061
204062static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
204063 Fts5Parse *pParse, /* Parse context */
204064 Fts5ExprNode *pLeft, /* Left hand child expression */
204065 Fts5ExprNode *pRight /* Right hand child expression */
204066){
204067 Fts5ExprNode *pRet = 0;
204068 Fts5ExprNode *pPrev;
204069
204070 if( pParse->rc ){
204071 sqlite3Fts5ParseNodeFree(pLeft);
204072 sqlite3Fts5ParseNodeFree(pRight);
204073 }else{
204074
204075 assert( pLeft->eType==FTS5_STRING
204076 || pLeft->eType==FTS5_TERM
204077 || pLeft->eType==FTS5_EOF
204078 || pLeft->eType==FTS5_AND
204079 );
204080 assert( pRight->eType==FTS5_STRING
204081 || pRight->eType==FTS5_TERM
204082 || pRight->eType==FTS5_EOF
204083 );
204084
204085 if( pLeft->eType==FTS5_AND ){
204086 pPrev = pLeft->apChild[pLeft->nChild-1];
204087 }else{
204088 pPrev = pLeft;
204089 }
204090 assert( pPrev->eType==FTS5_STRING
204091 || pPrev->eType==FTS5_TERM
204092 || pPrev->eType==FTS5_EOF
204093 );
204094
204095 if( pRight->eType==FTS5_EOF ){
204096 assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] );
204097 sqlite3Fts5ParseNodeFree(pRight);
204098 pRet = pLeft;
204099 pParse->nPhrase--;
204100 }
204101 else if( pPrev->eType==FTS5_EOF ){
204102 Fts5ExprPhrase **ap;
204103
204104 if( pPrev==pLeft ){
204105 pRet = pRight;
204106 }else{
204107 pLeft->apChild[pLeft->nChild-1] = pRight;
204108 pRet = pLeft;
204109 }
204110
204111 ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
204112 assert( ap[0]==pPrev->pNear->apPhrase[0] );
204113 memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
204114 pParse->nPhrase--;
204115
204116 sqlite3Fts5ParseNodeFree(pPrev);
204117 }
204118 else{
204119 pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, 0);
204120 }
204121 }
204122
204123 return pRet;
204124}
204125
204126static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
204127 int nByte = 0;
204128 Fts5ExprTerm *p;
204129 char *zQuoted;
204130
204131 /* Determine the maximum amount of space required. */
204132 for(p=pTerm; p; p=p->pSynonym){
204133 nByte += (int)strlen(pTerm->zTerm) * 2 + 3 + 2;
204134 }
204135 zQuoted = sqlite3_malloc(nByte);
204136
204137 if( zQuoted ){
204138 int i = 0;
204139 for(p=pTerm; p; p=p->pSynonym){
204140 char *zIn = p->zTerm;
204141 zQuoted[i++] = '"';
204142 while( *zIn ){
204143 if( *zIn=='"' ) zQuoted[i++] = '"';
204144 zQuoted[i++] = *zIn++;
204145 }
204146 zQuoted[i++] = '"';
204147 if( p->pSynonym ) zQuoted[i++] = '|';
204148 }
204149 if( pTerm->bPrefix ){
204150 zQuoted[i++] = ' ';
204151 zQuoted[i++] = '*';
204152 }
204153 zQuoted[i++] = '\0';
204154 }
204155 return zQuoted;
204156}
204157
204158static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
204159 char *zNew;
204160 va_list ap;
204161 va_start(ap, zFmt);
204162 zNew = sqlite3_vmprintf(zFmt, ap);
204163 va_end(ap);
204164 if( zApp && zNew ){
204165 char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
204166 sqlite3_free(zNew);
204167 zNew = zNew2;
204168 }
204169 sqlite3_free(zApp);
204170 return zNew;
204171}
204172
204173/*
204174** Compose a tcl-readable representation of expression pExpr. Return a
204175** pointer to a buffer containing that representation. It is the
204176** responsibility of the caller to at some point free the buffer using
204177** sqlite3_free().
204178*/
204179static char *fts5ExprPrintTcl(
204180 Fts5Config *pConfig,
204181 const char *zNearsetCmd,
204182 Fts5ExprNode *pExpr
204183){
204184 char *zRet = 0;
204185 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
204186 Fts5ExprNearset *pNear = pExpr->pNear;
204187 int i;
204188 int iTerm;
204189
204190 zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
204191 if( zRet==0 ) return 0;
204192 if( pNear->pColset ){
204193 int *aiCol = pNear->pColset->aiCol;
204194 int nCol = pNear->pColset->nCol;
204195 if( nCol==1 ){
204196 zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
204197 }else{
204198 zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
204199 for(i=1; i<pNear->pColset->nCol; i++){
204200 zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
204201 }
204202 zRet = fts5PrintfAppend(zRet, "} ");
204203 }
204204 if( zRet==0 ) return 0;
204205 }
204206
204207 if( pNear->nPhrase>1 ){
204208 zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
204209 if( zRet==0 ) return 0;
204210 }
204211
204212 zRet = fts5PrintfAppend(zRet, "--");
204213 if( zRet==0 ) return 0;
204214
204215 for(i=0; i<pNear->nPhrase; i++){
204216 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
204217
204218 zRet = fts5PrintfAppend(zRet, " {");
204219 for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
204220 char *zTerm = pPhrase->aTerm[iTerm].zTerm;
204221 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" ", zTerm);
204222 if( pPhrase->aTerm[iTerm].bPrefix ){
204223 zRet = fts5PrintfAppend(zRet, "*");
204224 }
204225 }
204226
204227 if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
204228 if( zRet==0 ) return 0;
204229 }
204230
204231 }else{
204232 char const *zOp = 0;
204233 int i;
204234 switch( pExpr->eType ){
204235 case FTS5_AND: zOp = "AND"; break;
204236 case FTS5_NOT: zOp = "NOT"; break;
204237 default:
204238 assert( pExpr->eType==FTS5_OR );
204239 zOp = "OR";
204240 break;
204241 }
204242
204243 zRet = sqlite3_mprintf("%s", zOp);
204244 for(i=0; zRet && i<pExpr->nChild; i++){
204245 char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
204246 if( !z ){
204247 sqlite3_free(zRet);
204248 zRet = 0;
204249 }else{
204250 zRet = fts5PrintfAppend(zRet, " [%z]", z);
204251 }
204252 }
204253 }
204254
204255 return zRet;
204256}
204257
204258static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
204259 char *zRet = 0;
204260 if( pExpr->eType==0 ){
204261 return sqlite3_mprintf("\"\"");
204262 }else
204263 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
204264 Fts5ExprNearset *pNear = pExpr->pNear;
204265 int i;
204266 int iTerm;
204267
204268 if( pNear->pColset ){
204269 int iCol = pNear->pColset->aiCol[0];
204270 zRet = fts5PrintfAppend(zRet, "%s : ", pConfig->azCol[iCol]);
204271 if( zRet==0 ) return 0;
204272 }
204273
204274 if( pNear->nPhrase>1 ){
204275 zRet = fts5PrintfAppend(zRet, "NEAR(");
204276 if( zRet==0 ) return 0;
204277 }
204278
204279 for(i=0; i<pNear->nPhrase; i++){
204280 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
204281 if( i!=0 ){
204282 zRet = fts5PrintfAppend(zRet, " ");
204283 if( zRet==0 ) return 0;
204284 }
204285 for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
204286 char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
204287 if( zTerm ){
204288 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
204289 sqlite3_free(zTerm);
204290 }
204291 if( zTerm==0 || zRet==0 ){
204292 sqlite3_free(zRet);
204293 return 0;
204294 }
204295 }
204296 }
204297
204298 if( pNear->nPhrase>1 ){
204299 zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
204300 if( zRet==0 ) return 0;
204301 }
204302
204303 }else{
204304 char const *zOp = 0;
204305 int i;
204306
204307 switch( pExpr->eType ){
204308 case FTS5_AND: zOp = " AND "; break;
204309 case FTS5_NOT: zOp = " NOT "; break;
204310 default:
204311 assert( pExpr->eType==FTS5_OR );
204312 zOp = " OR ";
204313 break;
204314 }
204315
204316 for(i=0; i<pExpr->nChild; i++){
204317 char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
204318 if( z==0 ){
204319 sqlite3_free(zRet);
204320 zRet = 0;
204321 }else{
204322 int e = pExpr->apChild[i]->eType;
204323 int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF);
204324 zRet = fts5PrintfAppend(zRet, "%s%s%z%s",
204325 (i==0 ? "" : zOp),
204326 (b?"(":""), z, (b?")":"")
204327 );
204328 }
204329 if( zRet==0 ) break;
204330 }
204331 }
204332
204333 return zRet;
204334}
204335
204336/*
204337** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
204338** and fts5_expr_tcl() (bTcl!=0).
204339*/
204340static void fts5ExprFunction(
204341 sqlite3_context *pCtx, /* Function call context */
204342 int nArg, /* Number of args */
204343 sqlite3_value **apVal, /* Function arguments */
204344 int bTcl
204345){
204346 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
204347 sqlite3 *db = sqlite3_context_db_handle(pCtx);
204348 const char *zExpr = 0;
204349 char *zErr = 0;
204350 Fts5Expr *pExpr = 0;
204351 int rc;
204352 int i;
204353
204354 const char **azConfig; /* Array of arguments for Fts5Config */
204355 const char *zNearsetCmd = "nearset";
204356 int nConfig; /* Size of azConfig[] */
204357 Fts5Config *pConfig = 0;
204358 int iArg = 1;
204359
204360 if( nArg<1 ){
204361 zErr = sqlite3_mprintf("wrong number of arguments to function %s",
204362 bTcl ? "fts5_expr_tcl" : "fts5_expr"
204363 );
204364 sqlite3_result_error(pCtx, zErr, -1);
204365 sqlite3_free(zErr);
204366 return;
204367 }
204368
204369 if( bTcl && nArg>1 ){
204370 zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
204371 iArg = 2;
204372 }
204373
204374 nConfig = 3 + (nArg-iArg);
204375 azConfig = (const char**)sqlite3_malloc(sizeof(char*) * nConfig);
204376 if( azConfig==0 ){
204377 sqlite3_result_error_nomem(pCtx);
204378 return;
204379 }
204380 azConfig[0] = 0;
204381 azConfig[1] = "main";
204382 azConfig[2] = "tbl";
204383 for(i=3; iArg<nArg; iArg++){
204384 azConfig[i++] = (const char*)sqlite3_value_text(apVal[iArg]);
204385 }
204386
204387 zExpr = (const char*)sqlite3_value_text(apVal[0]);
204388
204389 rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
204390 if( rc==SQLITE_OK ){
204391 rc = sqlite3Fts5ExprNew(pConfig, pConfig->nCol, zExpr, &pExpr, &zErr);
204392 }
204393 if( rc==SQLITE_OK ){
204394 char *zText;
204395 if( pExpr->pRoot->xNext==0 ){
204396 zText = sqlite3_mprintf("");
204397 }else if( bTcl ){
204398 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
204399 }else{
204400 zText = fts5ExprPrint(pConfig, pExpr->pRoot);
204401 }
204402 if( zText==0 ){
204403 rc = SQLITE_NOMEM;
204404 }else{
204405 sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT);
204406 sqlite3_free(zText);
204407 }
204408 }
204409
204410 if( rc!=SQLITE_OK ){
204411 if( zErr ){
204412 sqlite3_result_error(pCtx, zErr, -1);
204413 sqlite3_free(zErr);
204414 }else{
204415 sqlite3_result_error_code(pCtx, rc);
204416 }
204417 }
204418 sqlite3_free((void *)azConfig);
204419 sqlite3Fts5ConfigFree(pConfig);
204420 sqlite3Fts5ExprFree(pExpr);
204421}
204422
204423static void fts5ExprFunctionHr(
204424 sqlite3_context *pCtx, /* Function call context */
204425 int nArg, /* Number of args */
204426 sqlite3_value **apVal /* Function arguments */
204427){
204428 fts5ExprFunction(pCtx, nArg, apVal, 0);
204429}
204430static void fts5ExprFunctionTcl(
204431 sqlite3_context *pCtx, /* Function call context */
204432 int nArg, /* Number of args */
204433 sqlite3_value **apVal /* Function arguments */
204434){
204435 fts5ExprFunction(pCtx, nArg, apVal, 1);
204436}
204437
204438/*
204439** The implementation of an SQLite user-defined-function that accepts a
204440** single integer as an argument. If the integer is an alpha-numeric
204441** unicode code point, 1 is returned. Otherwise 0.
204442*/
204443static void fts5ExprIsAlnum(
204444 sqlite3_context *pCtx, /* Function call context */
204445 int nArg, /* Number of args */
204446 sqlite3_value **apVal /* Function arguments */
204447){
204448 int iCode;
204449 u8 aArr[32];
204450 if( nArg!=1 ){
204451 sqlite3_result_error(pCtx,
204452 "wrong number of arguments to function fts5_isalnum", -1
204453 );
204454 return;
204455 }
204456 memset(aArr, 0, sizeof(aArr));
204457 sqlite3Fts5UnicodeCatParse("L*", aArr);
204458 sqlite3Fts5UnicodeCatParse("N*", aArr);
204459 sqlite3Fts5UnicodeCatParse("Co", aArr);
204460 iCode = sqlite3_value_int(apVal[0]);
204461 sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory(iCode)]);
204462}
204463
204464static void fts5ExprFold(
204465 sqlite3_context *pCtx, /* Function call context */
204466 int nArg, /* Number of args */
204467 sqlite3_value **apVal /* Function arguments */
204468){
204469 if( nArg!=1 && nArg!=2 ){
204470 sqlite3_result_error(pCtx,
204471 "wrong number of arguments to function fts5_fold", -1
204472 );
204473 }else{
204474 int iCode;
204475 int bRemoveDiacritics = 0;
204476 iCode = sqlite3_value_int(apVal[0]);
204477 if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
204478 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
204479 }
204480}
204481
204482/*
204483** This is called during initialization to register the fts5_expr() scalar
204484** UDF with the SQLite handle passed as the only argument.
204485*/
204486static int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
204487 struct Fts5ExprFunc {
204488 const char *z;
204489 void (*x)(sqlite3_context*,int,sqlite3_value**);
204490 } aFunc[] = {
204491 { "fts5_expr", fts5ExprFunctionHr },
204492 { "fts5_expr_tcl", fts5ExprFunctionTcl },
204493 { "fts5_isalnum", fts5ExprIsAlnum },
204494 { "fts5_fold", fts5ExprFold },
204495 };
204496 int i;
204497 int rc = SQLITE_OK;
204498 void *pCtx = (void*)pGlobal;
204499
204500 for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){
204501 struct Fts5ExprFunc *p = &aFunc[i];
204502 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
204503 }
204504
204505 /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
204506 ** sqlite3Fts5ParserFallback() are unused */
204507#ifndef NDEBUG
204508 (void)sqlite3Fts5ParserTrace;
204509#endif
204510 (void)sqlite3Fts5ParserFallback;
204511
204512 return rc;
204513}
204514
204515/*
204516** Return the number of phrases in expression pExpr.
204517*/
204518static int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
204519 return (pExpr ? pExpr->nPhrase : 0);
204520}
204521
204522/*
204523** Return the number of terms in the iPhrase'th phrase in pExpr.
204524*/
204525static int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
204526 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
204527 return pExpr->apExprPhrase[iPhrase]->nTerm;
204528}
204529
204530/*
204531** This function is used to access the current position list for phrase
204532** iPhrase.
204533*/
204534static int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
204535 int nRet;
204536 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
204537 Fts5ExprNode *pNode = pPhrase->pNode;
204538 if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
204539 *pa = pPhrase->poslist.p;
204540 nRet = pPhrase->poslist.n;
204541 }else{
204542 *pa = 0;
204543 nRet = 0;
204544 }
204545 return nRet;
204546}
204547
204548struct Fts5PoslistPopulator {
204549 Fts5PoslistWriter writer;
204550 int bOk; /* True if ok to populate */
204551 int bMiss;
204552};
204553
204554static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
204555 Fts5PoslistPopulator *pRet;
204556 pRet = sqlite3_malloc(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
204557 if( pRet ){
204558 int i;
204559 memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
204560 for(i=0; i<pExpr->nPhrase; i++){
204561 Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
204562 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
204563 assert( pExpr->apExprPhrase[i]->nTerm==1 );
204564 if( bLive &&
204565 (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
204566 ){
204567 pRet[i].bMiss = 1;
204568 }else{
204569 pBuf->n = 0;
204570 }
204571 }
204572 }
204573 return pRet;
204574}
204575
204576struct Fts5ExprCtx {
204577 Fts5Expr *pExpr;
204578 Fts5PoslistPopulator *aPopulator;
204579 i64 iOff;
204580};
204581typedef struct Fts5ExprCtx Fts5ExprCtx;
204582
204583/*
204584** TODO: Make this more efficient!
204585*/
204586static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
204587 int i;
204588 for(i=0; i<pColset->nCol; i++){
204589 if( pColset->aiCol[i]==iCol ) return 1;
204590 }
204591 return 0;
204592}
204593
204594static int fts5ExprPopulatePoslistsCb(
204595 void *pCtx, /* Copy of 2nd argument to xTokenize() */
204596 int tflags, /* Mask of FTS5_TOKEN_* flags */
204597 const char *pToken, /* Pointer to buffer containing token */
204598 int nToken, /* Size of token in bytes */
204599 int iUnused1, /* Byte offset of token within input text */
204600 int iUnused2 /* Byte offset of end of token within input text */
204601){
204602 Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
204603 Fts5Expr *pExpr = p->pExpr;
204604 int i;
204605
204606 UNUSED_PARAM2(iUnused1, iUnused2);
204607
204608 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
204609 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++;
204610 for(i=0; i<pExpr->nPhrase; i++){
204611 Fts5ExprTerm *pTerm;
204612 if( p->aPopulator[i].bOk==0 ) continue;
204613 for(pTerm=&pExpr->apExprPhrase[i]->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
204614 int nTerm = (int)strlen(pTerm->zTerm);
204615 if( (nTerm==nToken || (nTerm<nToken && pTerm->bPrefix))
204616 && memcmp(pTerm->zTerm, pToken, nTerm)==0
204617 ){
204618 int rc = sqlite3Fts5PoslistWriterAppend(
204619 &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
204620 );
204621 if( rc ) return rc;
204622 break;
204623 }
204624 }
204625 }
204626 return SQLITE_OK;
204627}
204628
204629static int sqlite3Fts5ExprPopulatePoslists(
204630 Fts5Config *pConfig,
204631 Fts5Expr *pExpr,
204632 Fts5PoslistPopulator *aPopulator,
204633 int iCol,
204634 const char *z, int n
204635){
204636 int i;
204637 Fts5ExprCtx sCtx;
204638 sCtx.pExpr = pExpr;
204639 sCtx.aPopulator = aPopulator;
204640 sCtx.iOff = (((i64)iCol) << 32) - 1;
204641
204642 for(i=0; i<pExpr->nPhrase; i++){
204643 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
204644 Fts5Colset *pColset = pNode->pNear->pColset;
204645 if( (pColset && 0==fts5ExprColsetTest(pColset, iCol))
204646 || aPopulator[i].bMiss
204647 ){
204648 aPopulator[i].bOk = 0;
204649 }else{
204650 aPopulator[i].bOk = 1;
204651 }
204652 }
204653
204654 return sqlite3Fts5Tokenize(pConfig,
204655 FTS5_TOKENIZE_DOCUMENT, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
204656 );
204657}
204658
204659static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
204660 if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){
204661 pNode->pNear->apPhrase[0]->poslist.n = 0;
204662 }else{
204663 int i;
204664 for(i=0; i<pNode->nChild; i++){
204665 fts5ExprClearPoslists(pNode->apChild[i]);
204666 }
204667 }
204668}
204669
204670static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
204671 pNode->iRowid = iRowid;
204672 pNode->bEof = 0;
204673 switch( pNode->eType ){
204674 case FTS5_TERM:
204675 case FTS5_STRING:
204676 return (pNode->pNear->apPhrase[0]->poslist.n>0);
204677
204678 case FTS5_AND: {
204679 int i;
204680 for(i=0; i<pNode->nChild; i++){
204681 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
204682 fts5ExprClearPoslists(pNode);
204683 return 0;
204684 }
204685 }
204686 break;
204687 }
204688
204689 case FTS5_OR: {
204690 int i;
204691 int bRet = 0;
204692 for(i=0; i<pNode->nChild; i++){
204693 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
204694 bRet = 1;
204695 }
204696 }
204697 return bRet;
204698 }
204699
204700 default: {
204701 assert( pNode->eType==FTS5_NOT );
204702 if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
204703 || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
204704 ){
204705 fts5ExprClearPoslists(pNode);
204706 return 0;
204707 }
204708 break;
204709 }
204710 }
204711 return 1;
204712}
204713
204714static void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
204715 fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
204716}
204717
204718/*
204719** This function is only called for detail=columns tables.
204720*/
204721static int sqlite3Fts5ExprPhraseCollist(
204722 Fts5Expr *pExpr,
204723 int iPhrase,
204724 const u8 **ppCollist,
204725 int *pnCollist
204726){
204727 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
204728 Fts5ExprNode *pNode = pPhrase->pNode;
204729 int rc = SQLITE_OK;
204730
204731 assert( iPhrase>=0 && iPhrase<pExpr->nPhrase );
204732 assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
204733
204734 if( pNode->bEof==0
204735 && pNode->iRowid==pExpr->pRoot->iRowid
204736 && pPhrase->poslist.n>0
204737 ){
204738 Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
204739 if( pTerm->pSynonym ){
204740 Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
204741 rc = fts5ExprSynonymList(
204742 pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
204743 );
204744 }else{
204745 *ppCollist = pPhrase->aTerm[0].pIter->pData;
204746 *pnCollist = pPhrase->aTerm[0].pIter->nData;
204747 }
204748 }else{
204749 *ppCollist = 0;
204750 *pnCollist = 0;
204751 }
204752
204753 return rc;
204754}
204755
204756
204757/*
204758** 2014 August 11
204759**
204760** The author disclaims copyright to this source code. In place of
204761** a legal notice, here is a blessing:
204762**
204763** May you do good and not evil.
204764** May you find forgiveness for yourself and forgive others.
204765** May you share freely, never taking more than you give.
204766**
204767******************************************************************************
204768**
204769*/
204770
204771
204772
204773/* #include "fts5Int.h" */
204774
204775typedef struct Fts5HashEntry Fts5HashEntry;
204776
204777/*
204778** This file contains the implementation of an in-memory hash table used
204779** to accumuluate "term -> doclist" content before it is flused to a level-0
204780** segment.
204781*/
204782
204783
204784struct Fts5Hash {
204785 int eDetail; /* Copy of Fts5Config.eDetail */
204786 int *pnByte; /* Pointer to bytes counter */
204787 int nEntry; /* Number of entries currently in hash */
204788 int nSlot; /* Size of aSlot[] array */
204789 Fts5HashEntry *pScan; /* Current ordered scan item */
204790 Fts5HashEntry **aSlot; /* Array of hash slots */
204791};
204792
204793/*
204794** Each entry in the hash table is represented by an object of the
204795** following type. Each object, its key (a nul-terminated string) and
204796** its current data are stored in a single memory allocation. The
204797** key immediately follows the object in memory. The position list
204798** data immediately follows the key data in memory.
204799**
204800** The data that follows the key is in a similar, but not identical format
204801** to the doclist data stored in the database. It is:
204802**
204803** * Rowid, as a varint
204804** * Position list, without 0x00 terminator.
204805** * Size of previous position list and rowid, as a 4 byte
204806** big-endian integer.
204807**
204808** iRowidOff:
204809** Offset of last rowid written to data area. Relative to first byte of
204810** structure.
204811**
204812** nData:
204813** Bytes of data written since iRowidOff.
204814*/
204815struct Fts5HashEntry {
204816 Fts5HashEntry *pHashNext; /* Next hash entry with same hash-key */
204817 Fts5HashEntry *pScanNext; /* Next entry in sorted order */
204818
204819 int nAlloc; /* Total size of allocation */
204820 int iSzPoslist; /* Offset of space for 4-byte poslist size */
204821 int nData; /* Total bytes of data (incl. structure) */
204822 int nKey; /* Length of key in bytes */
204823 u8 bDel; /* Set delete-flag @ iSzPoslist */
204824 u8 bContent; /* Set content-flag (detail=none mode) */
204825 i16 iCol; /* Column of last value written */
204826 int iPos; /* Position of last value written */
204827 i64 iRowid; /* Rowid of last value written */
204828};
204829
204830/*
204831** Eqivalent to:
204832**
204833** char *fts5EntryKey(Fts5HashEntry *pEntry){ return zKey; }
204834*/
204835#define fts5EntryKey(p) ( ((char *)(&(p)[1])) )
204836
204837
204838/*
204839** Allocate a new hash table.
204840*/
204841static int sqlite3Fts5HashNew(Fts5Config *pConfig, Fts5Hash **ppNew, int *pnByte){
204842 int rc = SQLITE_OK;
204843 Fts5Hash *pNew;
204844
204845 *ppNew = pNew = (Fts5Hash*)sqlite3_malloc(sizeof(Fts5Hash));
204846 if( pNew==0 ){
204847 rc = SQLITE_NOMEM;
204848 }else{
204849 int nByte;
204850 memset(pNew, 0, sizeof(Fts5Hash));
204851 pNew->pnByte = pnByte;
204852 pNew->eDetail = pConfig->eDetail;
204853
204854 pNew->nSlot = 1024;
204855 nByte = sizeof(Fts5HashEntry*) * pNew->nSlot;
204856 pNew->aSlot = (Fts5HashEntry**)sqlite3_malloc(nByte);
204857 if( pNew->aSlot==0 ){
204858 sqlite3_free(pNew);
204859 *ppNew = 0;
204860 rc = SQLITE_NOMEM;
204861 }else{
204862 memset(pNew->aSlot, 0, nByte);
204863 }
204864 }
204865 return rc;
204866}
204867
204868/*
204869** Free a hash table object.
204870*/
204871static void sqlite3Fts5HashFree(Fts5Hash *pHash){
204872 if( pHash ){
204873 sqlite3Fts5HashClear(pHash);
204874 sqlite3_free(pHash->aSlot);
204875 sqlite3_free(pHash);
204876 }
204877}
204878
204879/*
204880** Empty (but do not delete) a hash table.
204881*/
204882static void sqlite3Fts5HashClear(Fts5Hash *pHash){
204883 int i;
204884 for(i=0; i<pHash->nSlot; i++){
204885 Fts5HashEntry *pNext;
204886 Fts5HashEntry *pSlot;
204887 for(pSlot=pHash->aSlot[i]; pSlot; pSlot=pNext){
204888 pNext = pSlot->pHashNext;
204889 sqlite3_free(pSlot);
204890 }
204891 }
204892 memset(pHash->aSlot, 0, pHash->nSlot * sizeof(Fts5HashEntry*));
204893 pHash->nEntry = 0;
204894}
204895
204896static unsigned int fts5HashKey(int nSlot, const u8 *p, int n){
204897 int i;
204898 unsigned int h = 13;
204899 for(i=n-1; i>=0; i--){
204900 h = (h << 3) ^ h ^ p[i];
204901 }
204902 return (h % nSlot);
204903}
204904
204905static unsigned int fts5HashKey2(int nSlot, u8 b, const u8 *p, int n){
204906 int i;
204907 unsigned int h = 13;
204908 for(i=n-1; i>=0; i--){
204909 h = (h << 3) ^ h ^ p[i];
204910 }
204911 h = (h << 3) ^ h ^ b;
204912 return (h % nSlot);
204913}
204914
204915/*
204916** Resize the hash table by doubling the number of slots.
204917*/
204918static int fts5HashResize(Fts5Hash *pHash){
204919 int nNew = pHash->nSlot*2;
204920 int i;
204921 Fts5HashEntry **apNew;
204922 Fts5HashEntry **apOld = pHash->aSlot;
204923
204924 apNew = (Fts5HashEntry**)sqlite3_malloc(nNew*sizeof(Fts5HashEntry*));
204925 if( !apNew ) return SQLITE_NOMEM;
204926 memset(apNew, 0, nNew*sizeof(Fts5HashEntry*));
204927
204928 for(i=0; i<pHash->nSlot; i++){
204929 while( apOld[i] ){
204930 unsigned int iHash;
204931 Fts5HashEntry *p = apOld[i];
204932 apOld[i] = p->pHashNext;
204933 iHash = fts5HashKey(nNew, (u8*)fts5EntryKey(p),
204934 (int)strlen(fts5EntryKey(p)));
204935 p->pHashNext = apNew[iHash];
204936 apNew[iHash] = p;
204937 }
204938 }
204939
204940 sqlite3_free(apOld);
204941 pHash->nSlot = nNew;
204942 pHash->aSlot = apNew;
204943 return SQLITE_OK;
204944}
204945
204946static void fts5HashAddPoslistSize(Fts5Hash *pHash, Fts5HashEntry *p){
204947 if( p->iSzPoslist ){
204948 u8 *pPtr = (u8*)p;
204949 if( pHash->eDetail==FTS5_DETAIL_NONE ){
204950 assert( p->nData==p->iSzPoslist );
204951 if( p->bDel ){
204952 pPtr[p->nData++] = 0x00;
204953 if( p->bContent ){
204954 pPtr[p->nData++] = 0x00;
204955 }
204956 }
204957 }else{
204958 int nSz = (p->nData - p->iSzPoslist - 1); /* Size in bytes */
204959 int nPos = nSz*2 + p->bDel; /* Value of nPos field */
204960
204961 assert( p->bDel==0 || p->bDel==1 );
204962 if( nPos<=127 ){
204963 pPtr[p->iSzPoslist] = (u8)nPos;
204964 }else{
204965 int nByte = sqlite3Fts5GetVarintLen((u32)nPos);
204966 memmove(&pPtr[p->iSzPoslist + nByte], &pPtr[p->iSzPoslist + 1], nSz);
204967 sqlite3Fts5PutVarint(&pPtr[p->iSzPoslist], nPos);
204968 p->nData += (nByte-1);
204969 }
204970 }
204971
204972 p->iSzPoslist = 0;
204973 p->bDel = 0;
204974 p->bContent = 0;
204975 }
204976}
204977
204978/*
204979** Add an entry to the in-memory hash table. The key is the concatenation
204980** of bByte and (pToken/nToken). The value is (iRowid/iCol/iPos).
204981**
204982** (bByte || pToken) -> (iRowid,iCol,iPos)
204983**
204984** Or, if iCol is negative, then the value is a delete marker.
204985*/
204986static int sqlite3Fts5HashWrite(
204987 Fts5Hash *pHash,
204988 i64 iRowid, /* Rowid for this entry */
204989 int iCol, /* Column token appears in (-ve -> delete) */
204990 int iPos, /* Position of token within column */
204991 char bByte, /* First byte of token */
204992 const char *pToken, int nToken /* Token to add or remove to or from index */
204993){
204994 unsigned int iHash;
204995 Fts5HashEntry *p;
204996 u8 *pPtr;
204997 int nIncr = 0; /* Amount to increment (*pHash->pnByte) by */
204998 int bNew; /* If non-delete entry should be written */
204999
205000 bNew = (pHash->eDetail==FTS5_DETAIL_FULL);
205001
205002 /* Attempt to locate an existing hash entry */
205003 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
205004 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
205005 char *zKey = fts5EntryKey(p);
205006 if( zKey[0]==bByte
205007 && p->nKey==nToken
205008 && memcmp(&zKey[1], pToken, nToken)==0
205009 ){
205010 break;
205011 }
205012 }
205013
205014 /* If an existing hash entry cannot be found, create a new one. */
205015 if( p==0 ){
205016 /* Figure out how much space to allocate */
205017 char *zKey;
205018 int nByte = sizeof(Fts5HashEntry) + (nToken+1) + 1 + 64;
205019 if( nByte<128 ) nByte = 128;
205020
205021 /* Grow the Fts5Hash.aSlot[] array if necessary. */
205022 if( (pHash->nEntry*2)>=pHash->nSlot ){
205023 int rc = fts5HashResize(pHash);
205024 if( rc!=SQLITE_OK ) return rc;
205025 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
205026 }
205027
205028 /* Allocate new Fts5HashEntry and add it to the hash table. */
205029 p = (Fts5HashEntry*)sqlite3_malloc(nByte);
205030 if( !p ) return SQLITE_NOMEM;
205031 memset(p, 0, sizeof(Fts5HashEntry));
205032 p->nAlloc = nByte;
205033 zKey = fts5EntryKey(p);
205034 zKey[0] = bByte;
205035 memcpy(&zKey[1], pToken, nToken);
205036 assert( iHash==fts5HashKey(pHash->nSlot, (u8*)zKey, nToken+1) );
205037 p->nKey = nToken;
205038 zKey[nToken+1] = '\0';
205039 p->nData = nToken+1 + 1 + sizeof(Fts5HashEntry);
205040 p->pHashNext = pHash->aSlot[iHash];
205041 pHash->aSlot[iHash] = p;
205042 pHash->nEntry++;
205043
205044 /* Add the first rowid field to the hash-entry */
205045 p->nData += sqlite3Fts5PutVarint(&((u8*)p)[p->nData], iRowid);
205046 p->iRowid = iRowid;
205047
205048 p->iSzPoslist = p->nData;
205049 if( pHash->eDetail!=FTS5_DETAIL_NONE ){
205050 p->nData += 1;
205051 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
205052 }
205053
205054 nIncr += p->nData;
205055 }else{
205056
205057 /* Appending to an existing hash-entry. Check that there is enough
205058 ** space to append the largest possible new entry. Worst case scenario
205059 ** is:
205060 **
205061 ** + 9 bytes for a new rowid,
205062 ** + 4 byte reserved for the "poslist size" varint.
205063 ** + 1 byte for a "new column" byte,
205064 ** + 3 bytes for a new column number (16-bit max) as a varint,
205065 ** + 5 bytes for the new position offset (32-bit max).
205066 */
205067 if( (p->nAlloc - p->nData) < (9 + 4 + 1 + 3 + 5) ){
205068 int nNew = p->nAlloc * 2;
205069 Fts5HashEntry *pNew;
205070 Fts5HashEntry **pp;
205071 pNew = (Fts5HashEntry*)sqlite3_realloc(p, nNew);
205072 if( pNew==0 ) return SQLITE_NOMEM;
205073 pNew->nAlloc = nNew;
205074 for(pp=&pHash->aSlot[iHash]; *pp!=p; pp=&(*pp)->pHashNext);
205075 *pp = pNew;
205076 p = pNew;
205077 }
205078 nIncr -= p->nData;
205079 }
205080 assert( (p->nAlloc - p->nData) >= (9 + 4 + 1 + 3 + 5) );
205081
205082 pPtr = (u8*)p;
205083
205084 /* If this is a new rowid, append the 4-byte size field for the previous
205085 ** entry, and the new rowid for this entry. */
205086 if( iRowid!=p->iRowid ){
205087 fts5HashAddPoslistSize(pHash, p);
205088 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iRowid - p->iRowid);
205089 p->iRowid = iRowid;
205090 bNew = 1;
205091 p->iSzPoslist = p->nData;
205092 if( pHash->eDetail!=FTS5_DETAIL_NONE ){
205093 p->nData += 1;
205094 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
205095 p->iPos = 0;
205096 }
205097 }
205098
205099 if( iCol>=0 ){
205100 if( pHash->eDetail==FTS5_DETAIL_NONE ){
205101 p->bContent = 1;
205102 }else{
205103 /* Append a new column value, if necessary */
205104 assert( iCol>=p->iCol );
205105 if( iCol!=p->iCol ){
205106 if( pHash->eDetail==FTS5_DETAIL_FULL ){
205107 pPtr[p->nData++] = 0x01;
205108 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iCol);
205109 p->iCol = (i16)iCol;
205110 p->iPos = 0;
205111 }else{
205112 bNew = 1;
205113 p->iCol = (i16)(iPos = iCol);
205114 }
205115 }
205116
205117 /* Append the new position offset, if necessary */
205118 if( bNew ){
205119 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iPos - p->iPos + 2);
205120 p->iPos = iPos;
205121 }
205122 }
205123 }else{
205124 /* This is a delete. Set the delete flag. */
205125 p->bDel = 1;
205126 }
205127
205128 nIncr += p->nData;
205129 *pHash->pnByte += nIncr;
205130 return SQLITE_OK;
205131}
205132
205133
205134/*
205135** Arguments pLeft and pRight point to linked-lists of hash-entry objects,
205136** each sorted in key order. This function merges the two lists into a
205137** single list and returns a pointer to its first element.
205138*/
205139static Fts5HashEntry *fts5HashEntryMerge(
205140 Fts5HashEntry *pLeft,
205141 Fts5HashEntry *pRight
205142){
205143 Fts5HashEntry *p1 = pLeft;
205144 Fts5HashEntry *p2 = pRight;
205145 Fts5HashEntry *pRet = 0;
205146 Fts5HashEntry **ppOut = &pRet;
205147
205148 while( p1 || p2 ){
205149 if( p1==0 ){
205150 *ppOut = p2;
205151 p2 = 0;
205152 }else if( p2==0 ){
205153 *ppOut = p1;
205154 p1 = 0;
205155 }else{
205156 int i = 0;
205157 char *zKey1 = fts5EntryKey(p1);
205158 char *zKey2 = fts5EntryKey(p2);
205159 while( zKey1[i]==zKey2[i] ) i++;
205160
205161 if( ((u8)zKey1[i])>((u8)zKey2[i]) ){
205162 /* p2 is smaller */
205163 *ppOut = p2;
205164 ppOut = &p2->pScanNext;
205165 p2 = p2->pScanNext;
205166 }else{
205167 /* p1 is smaller */
205168 *ppOut = p1;
205169 ppOut = &p1->pScanNext;
205170 p1 = p1->pScanNext;
205171 }
205172 *ppOut = 0;
205173 }
205174 }
205175
205176 return pRet;
205177}
205178
205179/*
205180** Extract all tokens from hash table iHash and link them into a list
205181** in sorted order. The hash table is cleared before returning. It is
205182** the responsibility of the caller to free the elements of the returned
205183** list.
205184*/
205185static int fts5HashEntrySort(
205186 Fts5Hash *pHash,
205187 const char *pTerm, int nTerm, /* Query prefix, if any */
205188 Fts5HashEntry **ppSorted
205189){
205190 const int nMergeSlot = 32;
205191 Fts5HashEntry **ap;
205192 Fts5HashEntry *pList;
205193 int iSlot;
205194 int i;
205195
205196 *ppSorted = 0;
205197 ap = sqlite3_malloc(sizeof(Fts5HashEntry*) * nMergeSlot);
205198 if( !ap ) return SQLITE_NOMEM;
205199 memset(ap, 0, sizeof(Fts5HashEntry*) * nMergeSlot);
205200
205201 for(iSlot=0; iSlot<pHash->nSlot; iSlot++){
205202 Fts5HashEntry *pIter;
205203 for(pIter=pHash->aSlot[iSlot]; pIter; pIter=pIter->pHashNext){
205204 if( pTerm==0 || 0==memcmp(fts5EntryKey(pIter), pTerm, nTerm) ){
205205 Fts5HashEntry *pEntry = pIter;
205206 pEntry->pScanNext = 0;
205207 for(i=0; ap[i]; i++){
205208 pEntry = fts5HashEntryMerge(pEntry, ap[i]);
205209 ap[i] = 0;
205210 }
205211 ap[i] = pEntry;
205212 }
205213 }
205214 }
205215
205216 pList = 0;
205217 for(i=0; i<nMergeSlot; i++){
205218 pList = fts5HashEntryMerge(pList, ap[i]);
205219 }
205220
205221 pHash->nEntry = 0;
205222 sqlite3_free(ap);
205223 *ppSorted = pList;
205224 return SQLITE_OK;
205225}
205226
205227/*
205228** Query the hash table for a doclist associated with term pTerm/nTerm.
205229*/
205230static int sqlite3Fts5HashQuery(
205231 Fts5Hash *pHash, /* Hash table to query */
205232 const char *pTerm, int nTerm, /* Query term */
205233 const u8 **ppDoclist, /* OUT: Pointer to doclist for pTerm */
205234 int *pnDoclist /* OUT: Size of doclist in bytes */
205235){
205236 unsigned int iHash = fts5HashKey(pHash->nSlot, (const u8*)pTerm, nTerm);
205237 char *zKey = 0;
205238 Fts5HashEntry *p;
205239
205240 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
205241 zKey = fts5EntryKey(p);
205242 if( memcmp(zKey, pTerm, nTerm)==0 && zKey[nTerm]==0 ) break;
205243 }
205244
205245 if( p ){
205246 fts5HashAddPoslistSize(pHash, p);
205247 *ppDoclist = (const u8*)&zKey[nTerm+1];
205248 *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm + 1);
205249 }else{
205250 *ppDoclist = 0;
205251 *pnDoclist = 0;
205252 }
205253
205254 return SQLITE_OK;
205255}
205256
205257static int sqlite3Fts5HashScanInit(
205258 Fts5Hash *p, /* Hash table to query */
205259 const char *pTerm, int nTerm /* Query prefix */
205260){
205261 return fts5HashEntrySort(p, pTerm, nTerm, &p->pScan);
205262}
205263
205264static void sqlite3Fts5HashScanNext(Fts5Hash *p){
205265 assert( !sqlite3Fts5HashScanEof(p) );
205266 p->pScan = p->pScan->pScanNext;
205267}
205268
205269static int sqlite3Fts5HashScanEof(Fts5Hash *p){
205270 return (p->pScan==0);
205271}
205272
205273static void sqlite3Fts5HashScanEntry(
205274 Fts5Hash *pHash,
205275 const char **pzTerm, /* OUT: term (nul-terminated) */
205276 const u8 **ppDoclist, /* OUT: pointer to doclist */
205277 int *pnDoclist /* OUT: size of doclist in bytes */
205278){
205279 Fts5HashEntry *p;
205280 if( (p = pHash->pScan) ){
205281 char *zKey = fts5EntryKey(p);
205282 int nTerm = (int)strlen(zKey);
205283 fts5HashAddPoslistSize(pHash, p);
205284 *pzTerm = zKey;
205285 *ppDoclist = (const u8*)&zKey[nTerm+1];
205286 *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm + 1);
205287 }else{
205288 *pzTerm = 0;
205289 *ppDoclist = 0;
205290 *pnDoclist = 0;
205291 }
205292}
205293
205294
205295/*
205296** 2014 May 31
205297**
205298** The author disclaims copyright to this source code. In place of
205299** a legal notice, here is a blessing:
205300**
205301** May you do good and not evil.
205302** May you find forgiveness for yourself and forgive others.
205303** May you share freely, never taking more than you give.
205304**
205305******************************************************************************
205306**
205307** Low level access to the FTS index stored in the database file. The
205308** routines in this file file implement all read and write access to the
205309** %_data table. Other parts of the system access this functionality via
205310** the interface defined in fts5Int.h.
205311*/
205312
205313
205314/* #include "fts5Int.h" */
205315
205316/*
205317** Overview:
205318**
205319** The %_data table contains all the FTS indexes for an FTS5 virtual table.
205320** As well as the main term index, there may be up to 31 prefix indexes.
205321** The format is similar to FTS3/4, except that:
205322**
205323** * all segment b-tree leaf data is stored in fixed size page records
205324** (e.g. 1000 bytes). A single doclist may span multiple pages. Care is
205325** taken to ensure it is possible to iterate in either direction through
205326** the entries in a doclist, or to seek to a specific entry within a
205327** doclist, without loading it into memory.
205328**
205329** * large doclists that span many pages have associated "doclist index"
205330** records that contain a copy of the first rowid on each page spanned by
205331** the doclist. This is used to speed up seek operations, and merges of
205332** large doclists with very small doclists.
205333**
205334** * extra fields in the "structure record" record the state of ongoing
205335** incremental merge operations.
205336**
205337*/
205338
205339
205340#define FTS5_OPT_WORK_UNIT 1000 /* Number of leaf pages per optimize step */
205341#define FTS5_WORK_UNIT 64 /* Number of leaf pages in unit of work */
205342
205343#define FTS5_MIN_DLIDX_SIZE 4 /* Add dlidx if this many empty pages */
205344
205345#define FTS5_MAIN_PREFIX '0'
205346
205347#if FTS5_MAX_PREFIX_INDEXES > 31
205348# error "FTS5_MAX_PREFIX_INDEXES is too large"
205349#endif
205350
205351/*
205352** Details:
205353**
205354** The %_data table managed by this module,
205355**
205356** CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB);
205357**
205358** , contains the following 5 types of records. See the comments surrounding
205359** the FTS5_*_ROWID macros below for a description of how %_data rowids are
205360** assigned to each fo them.
205361**
205362** 1. Structure Records:
205363**
205364** The set of segments that make up an index - the index structure - are
205365** recorded in a single record within the %_data table. The record consists
205366** of a single 32-bit configuration cookie value followed by a list of
205367** SQLite varints. If the FTS table features more than one index (because
205368** there are one or more prefix indexes), it is guaranteed that all share
205369** the same cookie value.
205370**
205371** Immediately following the configuration cookie, the record begins with
205372** three varints:
205373**
205374** + number of levels,
205375** + total number of segments on all levels,
205376** + value of write counter.
205377**
205378** Then, for each level from 0 to nMax:
205379**
205380** + number of input segments in ongoing merge.
205381** + total number of segments in level.
205382** + for each segment from oldest to newest:
205383** + segment id (always > 0)
205384** + first leaf page number (often 1, always greater than 0)
205385** + final leaf page number
205386**
205387** 2. The Averages Record:
205388**
205389** A single record within the %_data table. The data is a list of varints.
205390** The first value is the number of rows in the index. Then, for each column
205391** from left to right, the total number of tokens in the column for all
205392** rows of the table.
205393**
205394** 3. Segment leaves:
205395**
205396** TERM/DOCLIST FORMAT:
205397**
205398** Most of each segment leaf is taken up by term/doclist data. The
205399** general format of term/doclist, starting with the first term
205400** on the leaf page, is:
205401**
205402** varint : size of first term
205403** blob: first term data
205404** doclist: first doclist
205405** zero-or-more {
205406** varint: number of bytes in common with previous term
205407** varint: number of bytes of new term data (nNew)
205408** blob: nNew bytes of new term data
205409** doclist: next doclist
205410** }
205411**
205412** doclist format:
205413**
205414** varint: first rowid
205415** poslist: first poslist
205416** zero-or-more {
205417** varint: rowid delta (always > 0)
205418** poslist: next poslist
205419** }
205420**
205421** poslist format:
205422**
205423** varint: size of poslist in bytes multiplied by 2, not including
205424** this field. Plus 1 if this entry carries the "delete" flag.
205425** collist: collist for column 0
205426** zero-or-more {
205427** 0x01 byte
205428** varint: column number (I)
205429** collist: collist for column I
205430** }
205431**
205432** collist format:
205433**
205434** varint: first offset + 2
205435** zero-or-more {
205436** varint: offset delta + 2
205437** }
205438**
205439** PAGE FORMAT
205440**
205441** Each leaf page begins with a 4-byte header containing 2 16-bit
205442** unsigned integer fields in big-endian format. They are:
205443**
205444** * The byte offset of the first rowid on the page, if it exists
205445** and occurs before the first term (otherwise 0).
205446**
205447** * The byte offset of the start of the page footer. If the page
205448** footer is 0 bytes in size, then this field is the same as the
205449** size of the leaf page in bytes.
205450**
205451** The page footer consists of a single varint for each term located
205452** on the page. Each varint is the byte offset of the current term
205453** within the page, delta-compressed against the previous value. In
205454** other words, the first varint in the footer is the byte offset of
205455** the first term, the second is the byte offset of the second less that
205456** of the first, and so on.
205457**
205458** The term/doclist format described above is accurate if the entire
205459** term/doclist data fits on a single leaf page. If this is not the case,
205460** the format is changed in two ways:
205461**
205462** + if the first rowid on a page occurs before the first term, it
205463** is stored as a literal value:
205464**
205465** varint: first rowid
205466**
205467** + the first term on each page is stored in the same way as the
205468** very first term of the segment:
205469**
205470** varint : size of first term
205471** blob: first term data
205472**
205473** 5. Segment doclist indexes:
205474**
205475** Doclist indexes are themselves b-trees, however they usually consist of
205476** a single leaf record only. The format of each doclist index leaf page
205477** is:
205478**
205479** * Flags byte. Bits are:
205480** 0x01: Clear if leaf is also the root page, otherwise set.
205481**
205482** * Page number of fts index leaf page. As a varint.
205483**
205484** * First rowid on page indicated by previous field. As a varint.
205485**
205486** * A list of varints, one for each subsequent termless page. A
205487** positive delta if the termless page contains at least one rowid,
205488** or an 0x00 byte otherwise.
205489**
205490** Internal doclist index nodes are:
205491**
205492** * Flags byte. Bits are:
205493** 0x01: Clear for root page, otherwise set.
205494**
205495** * Page number of first child page. As a varint.
205496**
205497** * Copy of first rowid on page indicated by previous field. As a varint.
205498**
205499** * A list of delta-encoded varints - the first rowid on each subsequent
205500** child page.
205501**
205502*/
205503
205504/*
205505** Rowids for the averages and structure records in the %_data table.
205506*/
205507#define FTS5_AVERAGES_ROWID 1 /* Rowid used for the averages record */
205508#define FTS5_STRUCTURE_ROWID 10 /* The structure record */
205509
205510/*
205511** Macros determining the rowids used by segment leaves and dlidx leaves
205512** and nodes. All nodes and leaves are stored in the %_data table with large
205513** positive rowids.
205514**
205515** Each segment has a unique non-zero 16-bit id.
205516**
205517** The rowid for each segment leaf is found by passing the segment id and
205518** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
205519** sequentially starting from 1.
205520*/
205521#define FTS5_DATA_ID_B 16 /* Max seg id number 65535 */
205522#define FTS5_DATA_DLI_B 1 /* Doclist-index flag (1 bit) */
205523#define FTS5_DATA_HEIGHT_B 5 /* Max dlidx tree height of 32 */
205524#define FTS5_DATA_PAGE_B 31 /* Max page number of 2147483648 */
205525
205526#define fts5_dri(segid, dlidx, height, pgno) ( \
205527 ((i64)(segid) << (FTS5_DATA_PAGE_B+FTS5_DATA_HEIGHT_B+FTS5_DATA_DLI_B)) + \
205528 ((i64)(dlidx) << (FTS5_DATA_PAGE_B + FTS5_DATA_HEIGHT_B)) + \
205529 ((i64)(height) << (FTS5_DATA_PAGE_B)) + \
205530 ((i64)(pgno)) \
205531)
205532
205533#define FTS5_SEGMENT_ROWID(segid, pgno) fts5_dri(segid, 0, 0, pgno)
205534#define FTS5_DLIDX_ROWID(segid, height, pgno) fts5_dri(segid, 1, height, pgno)
205535
205536/*
205537** Maximum segments permitted in a single index
205538*/
205539#define FTS5_MAX_SEGMENT 2000
205540
205541#ifdef SQLITE_DEBUG
205542static int sqlite3Fts5Corrupt() { return SQLITE_CORRUPT_VTAB; }
205543#endif
205544
205545
205546/*
205547** Each time a blob is read from the %_data table, it is padded with this
205548** many zero bytes. This makes it easier to decode the various record formats
205549** without overreading if the records are corrupt.
205550*/
205551#define FTS5_DATA_ZERO_PADDING 8
205552#define FTS5_DATA_PADDING 20
205553
205554typedef struct Fts5Data Fts5Data;
205555typedef struct Fts5DlidxIter Fts5DlidxIter;
205556typedef struct Fts5DlidxLvl Fts5DlidxLvl;
205557typedef struct Fts5DlidxWriter Fts5DlidxWriter;
205558typedef struct Fts5Iter Fts5Iter;
205559typedef struct Fts5PageWriter Fts5PageWriter;
205560typedef struct Fts5SegIter Fts5SegIter;
205561typedef struct Fts5DoclistIter Fts5DoclistIter;
205562typedef struct Fts5SegWriter Fts5SegWriter;
205563typedef struct Fts5Structure Fts5Structure;
205564typedef struct Fts5StructureLevel Fts5StructureLevel;
205565typedef struct Fts5StructureSegment Fts5StructureSegment;
205566
205567struct Fts5Data {
205568 u8 *p; /* Pointer to buffer containing record */
205569 int nn; /* Size of record in bytes */
205570 int szLeaf; /* Size of leaf without page-index */
205571};
205572
205573/*
205574** One object per %_data table.
205575*/
205576struct Fts5Index {
205577 Fts5Config *pConfig; /* Virtual table configuration */
205578 char *zDataTbl; /* Name of %_data table */
205579 int nWorkUnit; /* Leaf pages in a "unit" of work */
205580
205581 /*
205582 ** Variables related to the accumulation of tokens and doclists within the
205583 ** in-memory hash tables before they are flushed to disk.
205584 */
205585 Fts5Hash *pHash; /* Hash table for in-memory data */
205586 int nPendingData; /* Current bytes of pending data */
205587 i64 iWriteRowid; /* Rowid for current doc being written */
205588 int bDelete; /* Current write is a delete */
205589
205590 /* Error state. */
205591 int rc; /* Current error code */
205592
205593 /* State used by the fts5DataXXX() functions. */
205594 sqlite3_blob *pReader; /* RO incr-blob open on %_data table */
205595 sqlite3_stmt *pWriter; /* "INSERT ... %_data VALUES(?,?)" */
205596 sqlite3_stmt *pDeleter; /* "DELETE FROM %_data ... id>=? AND id<=?" */
205597 sqlite3_stmt *pIdxWriter; /* "INSERT ... %_idx VALUES(?,?,?,?)" */
205598 sqlite3_stmt *pIdxDeleter; /* "DELETE FROM %_idx WHERE segid=? */
205599 sqlite3_stmt *pIdxSelect;
205600 int nRead; /* Total number of blocks read */
205601
205602 sqlite3_stmt *pDataVersion;
205603 i64 iStructVersion; /* data_version when pStruct read */
205604 Fts5Structure *pStruct; /* Current db structure (or NULL) */
205605};
205606
205607struct Fts5DoclistIter {
205608 u8 *aEof; /* Pointer to 1 byte past end of doclist */
205609
205610 /* Output variables. aPoslist==0 at EOF */
205611 i64 iRowid;
205612 u8 *aPoslist;
205613 int nPoslist;
205614 int nSize;
205615};
205616
205617/*
205618** The contents of the "structure" record for each index are represented
205619** using an Fts5Structure record in memory. Which uses instances of the
205620** other Fts5StructureXXX types as components.
205621*/
205622struct Fts5StructureSegment {
205623 int iSegid; /* Segment id */
205624 int pgnoFirst; /* First leaf page number in segment */
205625 int pgnoLast; /* Last leaf page number in segment */
205626};
205627struct Fts5StructureLevel {
205628 int nMerge; /* Number of segments in incr-merge */
205629 int nSeg; /* Total number of segments on level */
205630 Fts5StructureSegment *aSeg; /* Array of segments. aSeg[0] is oldest. */
205631};
205632struct Fts5Structure {
205633 int nRef; /* Object reference count */
205634 u64 nWriteCounter; /* Total leaves written to level 0 */
205635 int nSegment; /* Total segments in this structure */
205636 int nLevel; /* Number of levels in this index */
205637 Fts5StructureLevel aLevel[1]; /* Array of nLevel level objects */
205638};
205639
205640/*
205641** An object of type Fts5SegWriter is used to write to segments.
205642*/
205643struct Fts5PageWriter {
205644 int pgno; /* Page number for this page */
205645 int iPrevPgidx; /* Previous value written into pgidx */
205646 Fts5Buffer buf; /* Buffer containing leaf data */
205647 Fts5Buffer pgidx; /* Buffer containing page-index */
205648 Fts5Buffer term; /* Buffer containing previous term on page */
205649};
205650struct Fts5DlidxWriter {
205651 int pgno; /* Page number for this page */
205652 int bPrevValid; /* True if iPrev is valid */
205653 i64 iPrev; /* Previous rowid value written to page */
205654 Fts5Buffer buf; /* Buffer containing page data */
205655};
205656struct Fts5SegWriter {
205657 int iSegid; /* Segid to write to */
205658 Fts5PageWriter writer; /* PageWriter object */
205659 i64 iPrevRowid; /* Previous rowid written to current leaf */
205660 u8 bFirstRowidInDoclist; /* True if next rowid is first in doclist */
205661 u8 bFirstRowidInPage; /* True if next rowid is first in page */
205662 /* TODO1: Can use (writer.pgidx.n==0) instead of bFirstTermInPage */
205663 u8 bFirstTermInPage; /* True if next term will be first in leaf */
205664 int nLeafWritten; /* Number of leaf pages written */
205665 int nEmpty; /* Number of contiguous term-less nodes */
205666
205667 int nDlidx; /* Allocated size of aDlidx[] array */
205668 Fts5DlidxWriter *aDlidx; /* Array of Fts5DlidxWriter objects */
205669
205670 /* Values to insert into the %_idx table */
205671 Fts5Buffer btterm; /* Next term to insert into %_idx table */
205672 int iBtPage; /* Page number corresponding to btterm */
205673};
205674
205675typedef struct Fts5CResult Fts5CResult;
205676struct Fts5CResult {
205677 u16 iFirst; /* aSeg[] index of firstest iterator */
205678 u8 bTermEq; /* True if the terms are equal */
205679};
205680
205681/*
205682** Object for iterating through a single segment, visiting each term/rowid
205683** pair in the segment.
205684**
205685** pSeg:
205686** The segment to iterate through.
205687**
205688** iLeafPgno:
205689** Current leaf page number within segment.
205690**
205691** iLeafOffset:
205692** Byte offset within the current leaf that is the first byte of the
205693** position list data (one byte passed the position-list size field).
205694** rowid field of the current entry. Usually this is the size field of the
205695** position list data. The exception is if the rowid for the current entry
205696** is the last thing on the leaf page.
205697**
205698** pLeaf:
205699** Buffer containing current leaf page data. Set to NULL at EOF.
205700**
205701** iTermLeafPgno, iTermLeafOffset:
205702** Leaf page number containing the last term read from the segment. And
205703** the offset immediately following the term data.
205704**
205705** flags:
205706** Mask of FTS5_SEGITER_XXX values. Interpreted as follows:
205707**
205708** FTS5_SEGITER_ONETERM:
205709** If set, set the iterator to point to EOF after the current doclist
205710** has been exhausted. Do not proceed to the next term in the segment.
205711**
205712** FTS5_SEGITER_REVERSE:
205713** This flag is only ever set if FTS5_SEGITER_ONETERM is also set. If
205714** it is set, iterate through rowid in descending order instead of the
205715** default ascending order.
205716**
205717** iRowidOffset/nRowidOffset/aRowidOffset:
205718** These are used if the FTS5_SEGITER_REVERSE flag is set.
205719**
205720** For each rowid on the page corresponding to the current term, the
205721** corresponding aRowidOffset[] entry is set to the byte offset of the
205722** start of the "position-list-size" field within the page.
205723**
205724** iTermIdx:
205725** Index of current term on iTermLeafPgno.
205726*/
205727struct Fts5SegIter {
205728 Fts5StructureSegment *pSeg; /* Segment to iterate through */
205729 int flags; /* Mask of configuration flags */
205730 int iLeafPgno; /* Current leaf page number */
205731 Fts5Data *pLeaf; /* Current leaf data */
205732 Fts5Data *pNextLeaf; /* Leaf page (iLeafPgno+1) */
205733 int iLeafOffset; /* Byte offset within current leaf */
205734
205735 /* Next method */
205736 void (*xNext)(Fts5Index*, Fts5SegIter*, int*);
205737
205738 /* The page and offset from which the current term was read. The offset
205739 ** is the offset of the first rowid in the current doclist. */
205740 int iTermLeafPgno;
205741 int iTermLeafOffset;
205742
205743 int iPgidxOff; /* Next offset in pgidx */
205744 int iEndofDoclist;
205745
205746 /* The following are only used if the FTS5_SEGITER_REVERSE flag is set. */
205747 int iRowidOffset; /* Current entry in aRowidOffset[] */
205748 int nRowidOffset; /* Allocated size of aRowidOffset[] array */
205749 int *aRowidOffset; /* Array of offset to rowid fields */
205750
205751 Fts5DlidxIter *pDlidx; /* If there is a doclist-index */
205752
205753 /* Variables populated based on current entry. */
205754 Fts5Buffer term; /* Current term */
205755 i64 iRowid; /* Current rowid */
205756 int nPos; /* Number of bytes in current position list */
205757 u8 bDel; /* True if the delete flag is set */
205758};
205759
205760/*
205761** Argument is a pointer to an Fts5Data structure that contains a
205762** leaf page.
205763*/
205764#define ASSERT_SZLEAF_OK(x) assert( \
205765 (x)->szLeaf==(x)->nn || (x)->szLeaf==fts5GetU16(&(x)->p[2]) \
205766)
205767
205768#define FTS5_SEGITER_ONETERM 0x01
205769#define FTS5_SEGITER_REVERSE 0x02
205770
205771/*
205772** Argument is a pointer to an Fts5Data structure that contains a leaf
205773** page. This macro evaluates to true if the leaf contains no terms, or
205774** false if it contains at least one term.
205775*/
205776#define fts5LeafIsTermless(x) ((x)->szLeaf >= (x)->nn)
205777
205778#define fts5LeafTermOff(x, i) (fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2]))
205779
205780#define fts5LeafFirstRowidOff(x) (fts5GetU16((x)->p))
205781
205782/*
205783** Object for iterating through the merged results of one or more segments,
205784** visiting each term/rowid pair in the merged data.
205785**
205786** nSeg is always a power of two greater than or equal to the number of
205787** segments that this object is merging data from. Both the aSeg[] and
205788** aFirst[] arrays are sized at nSeg entries. The aSeg[] array is padded
205789** with zeroed objects - these are handled as if they were iterators opened
205790** on empty segments.
205791**
205792** The results of comparing segments aSeg[N] and aSeg[N+1], where N is an
205793** even number, is stored in aFirst[(nSeg+N)/2]. The "result" of the
205794** comparison in this context is the index of the iterator that currently
205795** points to the smaller term/rowid combination. Iterators at EOF are
205796** considered to be greater than all other iterators.
205797**
205798** aFirst[1] contains the index in aSeg[] of the iterator that points to
205799** the smallest key overall. aFirst[0] is unused.
205800**
205801** poslist:
205802** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
205803** There is no way to tell if this is populated or not.
205804*/
205805struct Fts5Iter {
205806 Fts5IndexIter base; /* Base class containing output vars */
205807
205808 Fts5Index *pIndex; /* Index that owns this iterator */
205809 Fts5Structure *pStruct; /* Database structure for this iterator */
205810 Fts5Buffer poslist; /* Buffer containing current poslist */
205811 Fts5Colset *pColset; /* Restrict matches to these columns */
205812
205813 /* Invoked to set output variables. */
205814 void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
205815
205816 int nSeg; /* Size of aSeg[] array */
205817 int bRev; /* True to iterate in reverse order */
205818 u8 bSkipEmpty; /* True to skip deleted entries */
205819
205820 i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */
205821 Fts5CResult *aFirst; /* Current merge state (see above) */
205822 Fts5SegIter aSeg[1]; /* Array of segment iterators */
205823};
205824
205825
205826/*
205827** An instance of the following type is used to iterate through the contents
205828** of a doclist-index record.
205829**
205830** pData:
205831** Record containing the doclist-index data.
205832**
205833** bEof:
205834** Set to true once iterator has reached EOF.
205835**
205836** iOff:
205837** Set to the current offset within record pData.
205838*/
205839struct Fts5DlidxLvl {
205840 Fts5Data *pData; /* Data for current page of this level */
205841 int iOff; /* Current offset into pData */
205842 int bEof; /* At EOF already */
205843 int iFirstOff; /* Used by reverse iterators */
205844
205845 /* Output variables */
205846 int iLeafPgno; /* Page number of current leaf page */
205847 i64 iRowid; /* First rowid on leaf iLeafPgno */
205848};
205849struct Fts5DlidxIter {
205850 int nLvl;
205851 int iSegid;
205852 Fts5DlidxLvl aLvl[1];
205853};
205854
205855static void fts5PutU16(u8 *aOut, u16 iVal){
205856 aOut[0] = (iVal>>8);
205857 aOut[1] = (iVal&0xFF);
205858}
205859
205860static u16 fts5GetU16(const u8 *aIn){
205861 return ((u16)aIn[0] << 8) + aIn[1];
205862}
205863
205864/*
205865** Allocate and return a buffer at least nByte bytes in size.
205866**
205867** If an OOM error is encountered, return NULL and set the error code in
205868** the Fts5Index handle passed as the first argument.
205869*/
205870static void *fts5IdxMalloc(Fts5Index *p, int nByte){
205871 return sqlite3Fts5MallocZero(&p->rc, nByte);
205872}
205873
205874/*
205875** Compare the contents of the pLeft buffer with the pRight/nRight blob.
205876**
205877** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
205878** +ve if pRight is smaller than pLeft. In other words:
205879**
205880** res = *pLeft - *pRight
205881*/
205882#ifdef SQLITE_DEBUG
205883static int fts5BufferCompareBlob(
205884 Fts5Buffer *pLeft, /* Left hand side of comparison */
205885 const u8 *pRight, int nRight /* Right hand side of comparison */
205886){
205887 int nCmp = MIN(pLeft->n, nRight);
205888 int res = memcmp(pLeft->p, pRight, nCmp);
205889 return (res==0 ? (pLeft->n - nRight) : res);
205890}
205891#endif
205892
205893/*
205894** Compare the contents of the two buffers using memcmp(). If one buffer
205895** is a prefix of the other, it is considered the lesser.
205896**
205897** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
205898** +ve if pRight is smaller than pLeft. In other words:
205899**
205900** res = *pLeft - *pRight
205901*/
205902static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){
205903 int nCmp = MIN(pLeft->n, pRight->n);
205904 int res = memcmp(pLeft->p, pRight->p, nCmp);
205905 return (res==0 ? (pLeft->n - pRight->n) : res);
205906}
205907
205908static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
205909 int ret;
205910 fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret);
205911 return ret;
205912}
205913
205914/*
205915** Close the read-only blob handle, if it is open.
205916*/
205917static void fts5CloseReader(Fts5Index *p){
205918 if( p->pReader ){
205919 sqlite3_blob *pReader = p->pReader;
205920 p->pReader = 0;
205921 sqlite3_blob_close(pReader);
205922 }
205923}
205924
205925/*
205926** Retrieve a record from the %_data table.
205927**
205928** If an error occurs, NULL is returned and an error left in the
205929** Fts5Index object.
205930*/
205931static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){
205932 Fts5Data *pRet = 0;
205933 if( p->rc==SQLITE_OK ){
205934 int rc = SQLITE_OK;
205935
205936 if( p->pReader ){
205937 /* This call may return SQLITE_ABORT if there has been a savepoint
205938 ** rollback since it was last used. In this case a new blob handle
205939 ** is required. */
205940 sqlite3_blob *pBlob = p->pReader;
205941 p->pReader = 0;
205942 rc = sqlite3_blob_reopen(pBlob, iRowid);
205943 assert( p->pReader==0 );
205944 p->pReader = pBlob;
205945 if( rc!=SQLITE_OK ){
205946 fts5CloseReader(p);
205947 }
205948 if( rc==SQLITE_ABORT ) rc = SQLITE_OK;
205949 }
205950
205951 /* If the blob handle is not open at this point, open it and seek
205952 ** to the requested entry. */
205953 if( p->pReader==0 && rc==SQLITE_OK ){
205954 Fts5Config *pConfig = p->pConfig;
205955 rc = sqlite3_blob_open(pConfig->db,
205956 pConfig->zDb, p->zDataTbl, "block", iRowid, 0, &p->pReader
205957 );
205958 }
205959
205960 /* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls
205961 ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead.
205962 ** All the reasons those functions might return SQLITE_ERROR - missing
205963 ** table, missing row, non-blob/text in block column - indicate
205964 ** backing store corruption. */
205965 if( rc==SQLITE_ERROR ) rc = FTS5_CORRUPT;
205966
205967 if( rc==SQLITE_OK ){
205968 u8 *aOut = 0; /* Read blob data into this buffer */
205969 int nByte = sqlite3_blob_bytes(p->pReader);
205970 int nAlloc = sizeof(Fts5Data) + nByte + FTS5_DATA_PADDING;
205971 pRet = (Fts5Data*)sqlite3_malloc(nAlloc);
205972 if( pRet ){
205973 pRet->nn = nByte;
205974 aOut = pRet->p = (u8*)&pRet[1];
205975 }else{
205976 rc = SQLITE_NOMEM;
205977 }
205978
205979 if( rc==SQLITE_OK ){
205980 rc = sqlite3_blob_read(p->pReader, aOut, nByte, 0);
205981 }
205982 if( rc!=SQLITE_OK ){
205983 sqlite3_free(pRet);
205984 pRet = 0;
205985 }else{
205986 /* TODO1: Fix this */
205987 pRet->szLeaf = fts5GetU16(&pRet->p[2]);
205988 }
205989 }
205990 p->rc = rc;
205991 p->nRead++;
205992 }
205993
205994 assert( (pRet==0)==(p->rc!=SQLITE_OK) );
205995 return pRet;
205996}
205997
205998/*
205999** Release a reference to data record returned by an earlier call to
206000** fts5DataRead().
206001*/
206002static void fts5DataRelease(Fts5Data *pData){
206003 sqlite3_free(pData);
206004}
206005
206006static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
206007 Fts5Data *pRet = fts5DataRead(p, iRowid);
206008 if( pRet ){
206009 if( pRet->szLeaf>pRet->nn ){
206010 p->rc = FTS5_CORRUPT;
206011 fts5DataRelease(pRet);
206012 pRet = 0;
206013 }
206014 }
206015 return pRet;
206016}
206017
206018static int fts5IndexPrepareStmt(
206019 Fts5Index *p,
206020 sqlite3_stmt **ppStmt,
206021 char *zSql
206022){
206023 if( p->rc==SQLITE_OK ){
206024 if( zSql ){
206025 p->rc = sqlite3_prepare_v3(p->pConfig->db, zSql, -1,
206026 SQLITE_PREPARE_PERSISTENT, ppStmt, 0);
206027 }else{
206028 p->rc = SQLITE_NOMEM;
206029 }
206030 }
206031 sqlite3_free(zSql);
206032 return p->rc;
206033}
206034
206035
206036/*
206037** INSERT OR REPLACE a record into the %_data table.
206038*/
206039static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){
206040 if( p->rc!=SQLITE_OK ) return;
206041
206042 if( p->pWriter==0 ){
206043 Fts5Config *pConfig = p->pConfig;
206044 fts5IndexPrepareStmt(p, &p->pWriter, sqlite3_mprintf(
206045 "REPLACE INTO '%q'.'%q_data'(id, block) VALUES(?,?)",
206046 pConfig->zDb, pConfig->zName
206047 ));
206048 if( p->rc ) return;
206049 }
206050
206051 sqlite3_bind_int64(p->pWriter, 1, iRowid);
206052 sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC);
206053 sqlite3_step(p->pWriter);
206054 p->rc = sqlite3_reset(p->pWriter);
206055 sqlite3_bind_null(p->pWriter, 2);
206056}
206057
206058/*
206059** Execute the following SQL:
206060**
206061** DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
206062*/
206063static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){
206064 if( p->rc!=SQLITE_OK ) return;
206065
206066 if( p->pDeleter==0 ){
206067 int rc;
206068 Fts5Config *pConfig = p->pConfig;
206069 char *zSql = sqlite3_mprintf(
206070 "DELETE FROM '%q'.'%q_data' WHERE id>=? AND id<=?",
206071 pConfig->zDb, pConfig->zName
206072 );
206073 if( zSql==0 ){
206074 rc = SQLITE_NOMEM;
206075 }else{
206076 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
206077 SQLITE_PREPARE_PERSISTENT, &p->pDeleter, 0);
206078 sqlite3_free(zSql);
206079 }
206080 if( rc!=SQLITE_OK ){
206081 p->rc = rc;
206082 return;
206083 }
206084 }
206085
206086 sqlite3_bind_int64(p->pDeleter, 1, iFirst);
206087 sqlite3_bind_int64(p->pDeleter, 2, iLast);
206088 sqlite3_step(p->pDeleter);
206089 p->rc = sqlite3_reset(p->pDeleter);
206090}
206091
206092/*
206093** Remove all records associated with segment iSegid.
206094*/
206095static void fts5DataRemoveSegment(Fts5Index *p, int iSegid){
206096 i64 iFirst = FTS5_SEGMENT_ROWID(iSegid, 0);
206097 i64 iLast = FTS5_SEGMENT_ROWID(iSegid+1, 0)-1;
206098 fts5DataDelete(p, iFirst, iLast);
206099 if( p->pIdxDeleter==0 ){
206100 Fts5Config *pConfig = p->pConfig;
206101 fts5IndexPrepareStmt(p, &p->pIdxDeleter, sqlite3_mprintf(
206102 "DELETE FROM '%q'.'%q_idx' WHERE segid=?",
206103 pConfig->zDb, pConfig->zName
206104 ));
206105 }
206106 if( p->rc==SQLITE_OK ){
206107 sqlite3_bind_int(p->pIdxDeleter, 1, iSegid);
206108 sqlite3_step(p->pIdxDeleter);
206109 p->rc = sqlite3_reset(p->pIdxDeleter);
206110 }
206111}
206112
206113/*
206114** Release a reference to an Fts5Structure object returned by an earlier
206115** call to fts5StructureRead() or fts5StructureDecode().
206116*/
206117static void fts5StructureRelease(Fts5Structure *pStruct){
206118 if( pStruct && 0>=(--pStruct->nRef) ){
206119 int i;
206120 assert( pStruct->nRef==0 );
206121 for(i=0; i<pStruct->nLevel; i++){
206122 sqlite3_free(pStruct->aLevel[i].aSeg);
206123 }
206124 sqlite3_free(pStruct);
206125 }
206126}
206127
206128static void fts5StructureRef(Fts5Structure *pStruct){
206129 pStruct->nRef++;
206130}
206131
206132/*
206133** Deserialize and return the structure record currently stored in serialized
206134** form within buffer pData/nData.
206135**
206136** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
206137** are over-allocated by one slot. This allows the structure contents
206138** to be more easily edited.
206139**
206140** If an error occurs, *ppOut is set to NULL and an SQLite error code
206141** returned. Otherwise, *ppOut is set to point to the new object and
206142** SQLITE_OK returned.
206143*/
206144static int fts5StructureDecode(
206145 const u8 *pData, /* Buffer containing serialized structure */
206146 int nData, /* Size of buffer pData in bytes */
206147 int *piCookie, /* Configuration cookie value */
206148 Fts5Structure **ppOut /* OUT: Deserialized object */
206149){
206150 int rc = SQLITE_OK;
206151 int i = 0;
206152 int iLvl;
206153 int nLevel = 0;
206154 int nSegment = 0;
206155 int nByte; /* Bytes of space to allocate at pRet */
206156 Fts5Structure *pRet = 0; /* Structure object to return */
206157
206158 /* Grab the cookie value */
206159 if( piCookie ) *piCookie = sqlite3Fts5Get32(pData);
206160 i = 4;
206161
206162 /* Read the total number of levels and segments from the start of the
206163 ** structure record. */
206164 i += fts5GetVarint32(&pData[i], nLevel);
206165 i += fts5GetVarint32(&pData[i], nSegment);
206166 nByte = (
206167 sizeof(Fts5Structure) + /* Main structure */
206168 sizeof(Fts5StructureLevel) * (nLevel-1) /* aLevel[] array */
206169 );
206170 pRet = (Fts5Structure*)sqlite3Fts5MallocZero(&rc, nByte);
206171
206172 if( pRet ){
206173 pRet->nRef = 1;
206174 pRet->nLevel = nLevel;
206175 pRet->nSegment = nSegment;
206176 i += sqlite3Fts5GetVarint(&pData[i], &pRet->nWriteCounter);
206177
206178 for(iLvl=0; rc==SQLITE_OK && iLvl<nLevel; iLvl++){
206179 Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
206180 int nTotal = 0;
206181 int iSeg;
206182
206183 if( i>=nData ){
206184 rc = FTS5_CORRUPT;
206185 }else{
206186 i += fts5GetVarint32(&pData[i], pLvl->nMerge);
206187 i += fts5GetVarint32(&pData[i], nTotal);
206188 assert( nTotal>=pLvl->nMerge );
206189 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc,
206190 nTotal * sizeof(Fts5StructureSegment)
206191 );
206192 }
206193
206194 if( rc==SQLITE_OK ){
206195 pLvl->nSeg = nTotal;
206196 for(iSeg=0; iSeg<nTotal; iSeg++){
206197 if( i>=nData ){
206198 rc = FTS5_CORRUPT;
206199 break;
206200 }
206201 i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].iSegid);
206202 i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].pgnoFirst);
206203 i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].pgnoLast);
206204 }
206205 }
206206 }
206207 if( rc!=SQLITE_OK ){
206208 fts5StructureRelease(pRet);
206209 pRet = 0;
206210 }
206211 }
206212
206213 *ppOut = pRet;
206214 return rc;
206215}
206216
206217/*
206218**
206219*/
206220static void fts5StructureAddLevel(int *pRc, Fts5Structure **ppStruct){
206221 if( *pRc==SQLITE_OK ){
206222 Fts5Structure *pStruct = *ppStruct;
206223 int nLevel = pStruct->nLevel;
206224 int nByte = (
206225 sizeof(Fts5Structure) + /* Main structure */
206226 sizeof(Fts5StructureLevel) * (nLevel+1) /* aLevel[] array */
206227 );
206228
206229 pStruct = sqlite3_realloc(pStruct, nByte);
206230 if( pStruct ){
206231 memset(&pStruct->aLevel[nLevel], 0, sizeof(Fts5StructureLevel));
206232 pStruct->nLevel++;
206233 *ppStruct = pStruct;
206234 }else{
206235 *pRc = SQLITE_NOMEM;
206236 }
206237 }
206238}
206239
206240/*
206241** Extend level iLvl so that there is room for at least nExtra more
206242** segments.
206243*/
206244static void fts5StructureExtendLevel(
206245 int *pRc,
206246 Fts5Structure *pStruct,
206247 int iLvl,
206248 int nExtra,
206249 int bInsert
206250){
206251 if( *pRc==SQLITE_OK ){
206252 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
206253 Fts5StructureSegment *aNew;
206254 int nByte;
206255
206256 nByte = (pLvl->nSeg + nExtra) * sizeof(Fts5StructureSegment);
206257 aNew = sqlite3_realloc(pLvl->aSeg, nByte);
206258 if( aNew ){
206259 if( bInsert==0 ){
206260 memset(&aNew[pLvl->nSeg], 0, sizeof(Fts5StructureSegment) * nExtra);
206261 }else{
206262 int nMove = pLvl->nSeg * sizeof(Fts5StructureSegment);
206263 memmove(&aNew[nExtra], aNew, nMove);
206264 memset(aNew, 0, sizeof(Fts5StructureSegment) * nExtra);
206265 }
206266 pLvl->aSeg = aNew;
206267 }else{
206268 *pRc = SQLITE_NOMEM;
206269 }
206270 }
206271}
206272
206273static Fts5Structure *fts5StructureReadUncached(Fts5Index *p){
206274 Fts5Structure *pRet = 0;
206275 Fts5Config *pConfig = p->pConfig;
206276 int iCookie; /* Configuration cookie */
206277 Fts5Data *pData;
206278
206279 pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID);
206280 if( p->rc==SQLITE_OK ){
206281 /* TODO: Do we need this if the leaf-index is appended? Probably... */
206282 memset(&pData->p[pData->nn], 0, FTS5_DATA_PADDING);
206283 p->rc = fts5StructureDecode(pData->p, pData->nn, &iCookie, &pRet);
206284 if( p->rc==SQLITE_OK && pConfig->iCookie!=iCookie ){
206285 p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie);
206286 }
206287 fts5DataRelease(pData);
206288 if( p->rc!=SQLITE_OK ){
206289 fts5StructureRelease(pRet);
206290 pRet = 0;
206291 }
206292 }
206293
206294 return pRet;
206295}
206296
206297static i64 fts5IndexDataVersion(Fts5Index *p){
206298 i64 iVersion = 0;
206299
206300 if( p->rc==SQLITE_OK ){
206301 if( p->pDataVersion==0 ){
206302 p->rc = fts5IndexPrepareStmt(p, &p->pDataVersion,
206303 sqlite3_mprintf("PRAGMA %Q.data_version", p->pConfig->zDb)
206304 );
206305 if( p->rc ) return 0;
206306 }
206307
206308 if( SQLITE_ROW==sqlite3_step(p->pDataVersion) ){
206309 iVersion = sqlite3_column_int64(p->pDataVersion, 0);
206310 }
206311 p->rc = sqlite3_reset(p->pDataVersion);
206312 }
206313
206314 return iVersion;
206315}
206316
206317/*
206318** Read, deserialize and return the structure record.
206319**
206320** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
206321** are over-allocated as described for function fts5StructureDecode()
206322** above.
206323**
206324** If an error occurs, NULL is returned and an error code left in the
206325** Fts5Index handle. If an error has already occurred when this function
206326** is called, it is a no-op.
206327*/
206328static Fts5Structure *fts5StructureRead(Fts5Index *p){
206329
206330 if( p->pStruct==0 ){
206331 p->iStructVersion = fts5IndexDataVersion(p);
206332 if( p->rc==SQLITE_OK ){
206333 p->pStruct = fts5StructureReadUncached(p);
206334 }
206335 }
206336
206337#if 0
206338 else{
206339 Fts5Structure *pTest = fts5StructureReadUncached(p);
206340 if( pTest ){
206341 int i, j;
206342 assert_nc( p->pStruct->nSegment==pTest->nSegment );
206343 assert_nc( p->pStruct->nLevel==pTest->nLevel );
206344 for(i=0; i<pTest->nLevel; i++){
206345 assert_nc( p->pStruct->aLevel[i].nMerge==pTest->aLevel[i].nMerge );
206346 assert_nc( p->pStruct->aLevel[i].nSeg==pTest->aLevel[i].nSeg );
206347 for(j=0; j<pTest->aLevel[i].nSeg; j++){
206348 Fts5StructureSegment *p1 = &pTest->aLevel[i].aSeg[j];
206349 Fts5StructureSegment *p2 = &p->pStruct->aLevel[i].aSeg[j];
206350 assert_nc( p1->iSegid==p2->iSegid );
206351 assert_nc( p1->pgnoFirst==p2->pgnoFirst );
206352 assert_nc( p1->pgnoLast==p2->pgnoLast );
206353 }
206354 }
206355 fts5StructureRelease(pTest);
206356 }
206357 }
206358#endif
206359
206360 if( p->rc!=SQLITE_OK ) return 0;
206361 assert( p->iStructVersion!=0 );
206362 assert( p->pStruct!=0 );
206363 fts5StructureRef(p->pStruct);
206364 return p->pStruct;
206365}
206366
206367static void fts5StructureInvalidate(Fts5Index *p){
206368 if( p->pStruct ){
206369 fts5StructureRelease(p->pStruct);
206370 p->pStruct = 0;
206371 }
206372}
206373
206374/*
206375** Return the total number of segments in index structure pStruct. This
206376** function is only ever used as part of assert() conditions.
206377*/
206378#ifdef SQLITE_DEBUG
206379static int fts5StructureCountSegments(Fts5Structure *pStruct){
206380 int nSegment = 0; /* Total number of segments */
206381 if( pStruct ){
206382 int iLvl; /* Used to iterate through levels */
206383 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
206384 nSegment += pStruct->aLevel[iLvl].nSeg;
206385 }
206386 }
206387
206388 return nSegment;
206389}
206390#endif
206391
206392#define fts5BufferSafeAppendBlob(pBuf, pBlob, nBlob) { \
206393 assert( (pBuf)->nSpace>=((pBuf)->n+nBlob) ); \
206394 memcpy(&(pBuf)->p[(pBuf)->n], pBlob, nBlob); \
206395 (pBuf)->n += nBlob; \
206396}
206397
206398#define fts5BufferSafeAppendVarint(pBuf, iVal) { \
206399 (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf)->n], (iVal)); \
206400 assert( (pBuf)->nSpace>=(pBuf)->n ); \
206401}
206402
206403
206404/*
206405** Serialize and store the "structure" record.
206406**
206407** If an error occurs, leave an error code in the Fts5Index object. If an
206408** error has already occurred, this function is a no-op.
206409*/
206410static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){
206411 if( p->rc==SQLITE_OK ){
206412 Fts5Buffer buf; /* Buffer to serialize record into */
206413 int iLvl; /* Used to iterate through levels */
206414 int iCookie; /* Cookie value to store */
206415
206416 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
206417 memset(&buf, 0, sizeof(Fts5Buffer));
206418
206419 /* Append the current configuration cookie */
206420 iCookie = p->pConfig->iCookie;
206421 if( iCookie<0 ) iCookie = 0;
206422
206423 if( 0==sqlite3Fts5BufferSize(&p->rc, &buf, 4+9+9+9) ){
206424 sqlite3Fts5Put32(buf.p, iCookie);
206425 buf.n = 4;
206426 fts5BufferSafeAppendVarint(&buf, pStruct->nLevel);
206427 fts5BufferSafeAppendVarint(&buf, pStruct->nSegment);
206428 fts5BufferSafeAppendVarint(&buf, (i64)pStruct->nWriteCounter);
206429 }
206430
206431 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
206432 int iSeg; /* Used to iterate through segments */
206433 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
206434 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nMerge);
206435 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nSeg);
206436 assert( pLvl->nMerge<=pLvl->nSeg );
206437
206438 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
206439 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].iSegid);
206440 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoFirst);
206441 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoLast);
206442 }
206443 }
206444
206445 fts5DataWrite(p, FTS5_STRUCTURE_ROWID, buf.p, buf.n);
206446 fts5BufferFree(&buf);
206447 }
206448}
206449
206450#if 0
206451static void fts5DebugStructure(int*,Fts5Buffer*,Fts5Structure*);
206452static void fts5PrintStructure(const char *zCaption, Fts5Structure *pStruct){
206453 int rc = SQLITE_OK;
206454 Fts5Buffer buf;
206455 memset(&buf, 0, sizeof(buf));
206456 fts5DebugStructure(&rc, &buf, pStruct);
206457 fprintf(stdout, "%s: %s\n", zCaption, buf.p);
206458 fflush(stdout);
206459 fts5BufferFree(&buf);
206460}
206461#else
206462# define fts5PrintStructure(x,y)
206463#endif
206464
206465static int fts5SegmentSize(Fts5StructureSegment *pSeg){
206466 return 1 + pSeg->pgnoLast - pSeg->pgnoFirst;
206467}
206468
206469/*
206470** Return a copy of index structure pStruct. Except, promote as many
206471** segments as possible to level iPromote. If an OOM occurs, NULL is
206472** returned.
206473*/
206474static void fts5StructurePromoteTo(
206475 Fts5Index *p,
206476 int iPromote,
206477 int szPromote,
206478 Fts5Structure *pStruct
206479){
206480 int il, is;
206481 Fts5StructureLevel *pOut = &pStruct->aLevel[iPromote];
206482
206483 if( pOut->nMerge==0 ){
206484 for(il=iPromote+1; il<pStruct->nLevel; il++){
206485 Fts5StructureLevel *pLvl = &pStruct->aLevel[il];
206486 if( pLvl->nMerge ) return;
206487 for(is=pLvl->nSeg-1; is>=0; is--){
206488 int sz = fts5SegmentSize(&pLvl->aSeg[is]);
206489 if( sz>szPromote ) return;
206490 fts5StructureExtendLevel(&p->rc, pStruct, iPromote, 1, 1);
206491 if( p->rc ) return;
206492 memcpy(pOut->aSeg, &pLvl->aSeg[is], sizeof(Fts5StructureSegment));
206493 pOut->nSeg++;
206494 pLvl->nSeg--;
206495 }
206496 }
206497 }
206498}
206499
206500/*
206501** A new segment has just been written to level iLvl of index structure
206502** pStruct. This function determines if any segments should be promoted
206503** as a result. Segments are promoted in two scenarios:
206504**
206505** a) If the segment just written is smaller than one or more segments
206506** within the previous populated level, it is promoted to the previous
206507** populated level.
206508**
206509** b) If the segment just written is larger than the newest segment on
206510** the next populated level, then that segment, and any other adjacent
206511** segments that are also smaller than the one just written, are
206512** promoted.
206513**
206514** If one or more segments are promoted, the structure object is updated
206515** to reflect this.
206516*/
206517static void fts5StructurePromote(
206518 Fts5Index *p, /* FTS5 backend object */
206519 int iLvl, /* Index level just updated */
206520 Fts5Structure *pStruct /* Index structure */
206521){
206522 if( p->rc==SQLITE_OK ){
206523 int iTst;
206524 int iPromote = -1;
206525 int szPromote = 0; /* Promote anything this size or smaller */
206526 Fts5StructureSegment *pSeg; /* Segment just written */
206527 int szSeg; /* Size of segment just written */
206528 int nSeg = pStruct->aLevel[iLvl].nSeg;
206529
206530 if( nSeg==0 ) return;
206531 pSeg = &pStruct->aLevel[iLvl].aSeg[pStruct->aLevel[iLvl].nSeg-1];
206532 szSeg = (1 + pSeg->pgnoLast - pSeg->pgnoFirst);
206533
206534 /* Check for condition (a) */
206535 for(iTst=iLvl-1; iTst>=0 && pStruct->aLevel[iTst].nSeg==0; iTst--);
206536 if( iTst>=0 ){
206537 int i;
206538 int szMax = 0;
206539 Fts5StructureLevel *pTst = &pStruct->aLevel[iTst];
206540 assert( pTst->nMerge==0 );
206541 for(i=0; i<pTst->nSeg; i++){
206542 int sz = pTst->aSeg[i].pgnoLast - pTst->aSeg[i].pgnoFirst + 1;
206543 if( sz>szMax ) szMax = sz;
206544 }
206545 if( szMax>=szSeg ){
206546 /* Condition (a) is true. Promote the newest segment on level
206547 ** iLvl to level iTst. */
206548 iPromote = iTst;
206549 szPromote = szMax;
206550 }
206551 }
206552
206553 /* If condition (a) is not met, assume (b) is true. StructurePromoteTo()
206554 ** is a no-op if it is not. */
206555 if( iPromote<0 ){
206556 iPromote = iLvl;
206557 szPromote = szSeg;
206558 }
206559 fts5StructurePromoteTo(p, iPromote, szPromote, pStruct);
206560 }
206561}
206562
206563
206564/*
206565** Advance the iterator passed as the only argument. If the end of the
206566** doclist-index page is reached, return non-zero.
206567*/
206568static int fts5DlidxLvlNext(Fts5DlidxLvl *pLvl){
206569 Fts5Data *pData = pLvl->pData;
206570
206571 if( pLvl->iOff==0 ){
206572 assert( pLvl->bEof==0 );
206573 pLvl->iOff = 1;
206574 pLvl->iOff += fts5GetVarint32(&pData->p[1], pLvl->iLeafPgno);
206575 pLvl->iOff += fts5GetVarint(&pData->p[pLvl->iOff], (u64*)&pLvl->iRowid);
206576 pLvl->iFirstOff = pLvl->iOff;
206577 }else{
206578 int iOff;
206579 for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
206580 if( pData->p[iOff] ) break;
206581 }
206582
206583 if( iOff<pData->nn ){
206584 i64 iVal;
206585 pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
206586 iOff += fts5GetVarint(&pData->p[iOff], (u64*)&iVal);
206587 pLvl->iRowid += iVal;
206588 pLvl->iOff = iOff;
206589 }else{
206590 pLvl->bEof = 1;
206591 }
206592 }
206593
206594 return pLvl->bEof;
206595}
206596
206597/*
206598** Advance the iterator passed as the only argument.
206599*/
206600static int fts5DlidxIterNextR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
206601 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
206602
206603 assert( iLvl<pIter->nLvl );
206604 if( fts5DlidxLvlNext(pLvl) ){
206605 if( (iLvl+1) < pIter->nLvl ){
206606 fts5DlidxIterNextR(p, pIter, iLvl+1);
206607 if( pLvl[1].bEof==0 ){
206608 fts5DataRelease(pLvl->pData);
206609 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
206610 pLvl->pData = fts5DataRead(p,
206611 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
206612 );
206613 if( pLvl->pData ) fts5DlidxLvlNext(pLvl);
206614 }
206615 }
206616 }
206617
206618 return pIter->aLvl[0].bEof;
206619}
206620static int fts5DlidxIterNext(Fts5Index *p, Fts5DlidxIter *pIter){
206621 return fts5DlidxIterNextR(p, pIter, 0);
206622}
206623
206624/*
206625** The iterator passed as the first argument has the following fields set
206626** as follows. This function sets up the rest of the iterator so that it
206627** points to the first rowid in the doclist-index.
206628**
206629** pData:
206630** pointer to doclist-index record,
206631**
206632** When this function is called pIter->iLeafPgno is the page number the
206633** doclist is associated with (the one featuring the term).
206634*/
206635static int fts5DlidxIterFirst(Fts5DlidxIter *pIter){
206636 int i;
206637 for(i=0; i<pIter->nLvl; i++){
206638 fts5DlidxLvlNext(&pIter->aLvl[i]);
206639 }
206640 return pIter->aLvl[0].bEof;
206641}
206642
206643
206644static int fts5DlidxIterEof(Fts5Index *p, Fts5DlidxIter *pIter){
206645 return p->rc!=SQLITE_OK || pIter->aLvl[0].bEof;
206646}
206647
206648static void fts5DlidxIterLast(Fts5Index *p, Fts5DlidxIter *pIter){
206649 int i;
206650
206651 /* Advance each level to the last entry on the last page */
206652 for(i=pIter->nLvl-1; p->rc==SQLITE_OK && i>=0; i--){
206653 Fts5DlidxLvl *pLvl = &pIter->aLvl[i];
206654 while( fts5DlidxLvlNext(pLvl)==0 );
206655 pLvl->bEof = 0;
206656
206657 if( i>0 ){
206658 Fts5DlidxLvl *pChild = &pLvl[-1];
206659 fts5DataRelease(pChild->pData);
206660 memset(pChild, 0, sizeof(Fts5DlidxLvl));
206661 pChild->pData = fts5DataRead(p,
206662 FTS5_DLIDX_ROWID(pIter->iSegid, i-1, pLvl->iLeafPgno)
206663 );
206664 }
206665 }
206666}
206667
206668/*
206669** Move the iterator passed as the only argument to the previous entry.
206670*/
206671static int fts5DlidxLvlPrev(Fts5DlidxLvl *pLvl){
206672 int iOff = pLvl->iOff;
206673
206674 assert( pLvl->bEof==0 );
206675 if( iOff<=pLvl->iFirstOff ){
206676 pLvl->bEof = 1;
206677 }else{
206678 u8 *a = pLvl->pData->p;
206679 i64 iVal;
206680 int iLimit;
206681 int ii;
206682 int nZero = 0;
206683
206684 /* Currently iOff points to the first byte of a varint. This block
206685 ** decrements iOff until it points to the first byte of the previous
206686 ** varint. Taking care not to read any memory locations that occur
206687 ** before the buffer in memory. */
206688 iLimit = (iOff>9 ? iOff-9 : 0);
206689 for(iOff--; iOff>iLimit; iOff--){
206690 if( (a[iOff-1] & 0x80)==0 ) break;
206691 }
206692
206693 fts5GetVarint(&a[iOff], (u64*)&iVal);
206694 pLvl->iRowid -= iVal;
206695 pLvl->iLeafPgno--;
206696
206697 /* Skip backwards past any 0x00 varints. */
206698 for(ii=iOff-1; ii>=pLvl->iFirstOff && a[ii]==0x00; ii--){
206699 nZero++;
206700 }
206701 if( ii>=pLvl->iFirstOff && (a[ii] & 0x80) ){
206702 /* The byte immediately before the last 0x00 byte has the 0x80 bit
206703 ** set. So the last 0x00 is only a varint 0 if there are 8 more 0x80
206704 ** bytes before a[ii]. */
206705 int bZero = 0; /* True if last 0x00 counts */
206706 if( (ii-8)>=pLvl->iFirstOff ){
206707 int j;
206708 for(j=1; j<=8 && (a[ii-j] & 0x80); j++);
206709 bZero = (j>8);
206710 }
206711 if( bZero==0 ) nZero--;
206712 }
206713 pLvl->iLeafPgno -= nZero;
206714 pLvl->iOff = iOff - nZero;
206715 }
206716
206717 return pLvl->bEof;
206718}
206719
206720static int fts5DlidxIterPrevR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
206721 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
206722
206723 assert( iLvl<pIter->nLvl );
206724 if( fts5DlidxLvlPrev(pLvl) ){
206725 if( (iLvl+1) < pIter->nLvl ){
206726 fts5DlidxIterPrevR(p, pIter, iLvl+1);
206727 if( pLvl[1].bEof==0 ){
206728 fts5DataRelease(pLvl->pData);
206729 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
206730 pLvl->pData = fts5DataRead(p,
206731 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
206732 );
206733 if( pLvl->pData ){
206734 while( fts5DlidxLvlNext(pLvl)==0 );
206735 pLvl->bEof = 0;
206736 }
206737 }
206738 }
206739 }
206740
206741 return pIter->aLvl[0].bEof;
206742}
206743static int fts5DlidxIterPrev(Fts5Index *p, Fts5DlidxIter *pIter){
206744 return fts5DlidxIterPrevR(p, pIter, 0);
206745}
206746
206747/*
206748** Free a doclist-index iterator object allocated by fts5DlidxIterInit().
206749*/
206750static void fts5DlidxIterFree(Fts5DlidxIter *pIter){
206751 if( pIter ){
206752 int i;
206753 for(i=0; i<pIter->nLvl; i++){
206754 fts5DataRelease(pIter->aLvl[i].pData);
206755 }
206756 sqlite3_free(pIter);
206757 }
206758}
206759
206760static Fts5DlidxIter *fts5DlidxIterInit(
206761 Fts5Index *p, /* Fts5 Backend to iterate within */
206762 int bRev, /* True for ORDER BY ASC */
206763 int iSegid, /* Segment id */
206764 int iLeafPg /* Leaf page number to load dlidx for */
206765){
206766 Fts5DlidxIter *pIter = 0;
206767 int i;
206768 int bDone = 0;
206769
206770 for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
206771 int nByte = sizeof(Fts5DlidxIter) + i * sizeof(Fts5DlidxLvl);
206772 Fts5DlidxIter *pNew;
206773
206774 pNew = (Fts5DlidxIter*)sqlite3_realloc(pIter, nByte);
206775 if( pNew==0 ){
206776 p->rc = SQLITE_NOMEM;
206777 }else{
206778 i64 iRowid = FTS5_DLIDX_ROWID(iSegid, i, iLeafPg);
206779 Fts5DlidxLvl *pLvl = &pNew->aLvl[i];
206780 pIter = pNew;
206781 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
206782 pLvl->pData = fts5DataRead(p, iRowid);
206783 if( pLvl->pData && (pLvl->pData->p[0] & 0x0001)==0 ){
206784 bDone = 1;
206785 }
206786 pIter->nLvl = i+1;
206787 }
206788 }
206789
206790 if( p->rc==SQLITE_OK ){
206791 pIter->iSegid = iSegid;
206792 if( bRev==0 ){
206793 fts5DlidxIterFirst(pIter);
206794 }else{
206795 fts5DlidxIterLast(p, pIter);
206796 }
206797 }
206798
206799 if( p->rc!=SQLITE_OK ){
206800 fts5DlidxIterFree(pIter);
206801 pIter = 0;
206802 }
206803
206804 return pIter;
206805}
206806
206807static i64 fts5DlidxIterRowid(Fts5DlidxIter *pIter){
206808 return pIter->aLvl[0].iRowid;
206809}
206810static int fts5DlidxIterPgno(Fts5DlidxIter *pIter){
206811 return pIter->aLvl[0].iLeafPgno;
206812}
206813
206814/*
206815** Load the next leaf page into the segment iterator.
206816*/
206817static void fts5SegIterNextPage(
206818 Fts5Index *p, /* FTS5 backend object */
206819 Fts5SegIter *pIter /* Iterator to advance to next page */
206820){
206821 Fts5Data *pLeaf;
206822 Fts5StructureSegment *pSeg = pIter->pSeg;
206823 fts5DataRelease(pIter->pLeaf);
206824 pIter->iLeafPgno++;
206825 if( pIter->pNextLeaf ){
206826 pIter->pLeaf = pIter->pNextLeaf;
206827 pIter->pNextLeaf = 0;
206828 }else if( pIter->iLeafPgno<=pSeg->pgnoLast ){
206829 pIter->pLeaf = fts5LeafRead(p,
206830 FTS5_SEGMENT_ROWID(pSeg->iSegid, pIter->iLeafPgno)
206831 );
206832 }else{
206833 pIter->pLeaf = 0;
206834 }
206835 pLeaf = pIter->pLeaf;
206836
206837 if( pLeaf ){
206838 pIter->iPgidxOff = pLeaf->szLeaf;
206839 if( fts5LeafIsTermless(pLeaf) ){
206840 pIter->iEndofDoclist = pLeaf->nn+1;
206841 }else{
206842 pIter->iPgidxOff += fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
206843 pIter->iEndofDoclist
206844 );
206845 }
206846 }
206847}
206848
206849/*
206850** Argument p points to a buffer containing a varint to be interpreted as a
206851** position list size field. Read the varint and return the number of bytes
206852** read. Before returning, set *pnSz to the number of bytes in the position
206853** list, and *pbDel to true if the delete flag is set, or false otherwise.
206854*/
206855static int fts5GetPoslistSize(const u8 *p, int *pnSz, int *pbDel){
206856 int nSz;
206857 int n = 0;
206858 fts5FastGetVarint32(p, n, nSz);
206859 assert_nc( nSz>=0 );
206860 *pnSz = nSz/2;
206861 *pbDel = nSz & 0x0001;
206862 return n;
206863}
206864
206865/*
206866** Fts5SegIter.iLeafOffset currently points to the first byte of a
206867** position-list size field. Read the value of the field and store it
206868** in the following variables:
206869**
206870** Fts5SegIter.nPos
206871** Fts5SegIter.bDel
206872**
206873** Leave Fts5SegIter.iLeafOffset pointing to the first byte of the
206874** position list content (if any).
206875*/
206876static void fts5SegIterLoadNPos(Fts5Index *p, Fts5SegIter *pIter){
206877 if( p->rc==SQLITE_OK ){
206878 int iOff = pIter->iLeafOffset; /* Offset to read at */
206879 ASSERT_SZLEAF_OK(pIter->pLeaf);
206880 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
206881 int iEod = MIN(pIter->iEndofDoclist, pIter->pLeaf->szLeaf);
206882 pIter->bDel = 0;
206883 pIter->nPos = 1;
206884 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
206885 pIter->bDel = 1;
206886 iOff++;
206887 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
206888 pIter->nPos = 1;
206889 iOff++;
206890 }else{
206891 pIter->nPos = 0;
206892 }
206893 }
206894 }else{
206895 int nSz;
206896 fts5FastGetVarint32(pIter->pLeaf->p, iOff, nSz);
206897 pIter->bDel = (nSz & 0x0001);
206898 pIter->nPos = nSz>>1;
206899 assert_nc( pIter->nPos>=0 );
206900 }
206901 pIter->iLeafOffset = iOff;
206902 }
206903}
206904
206905static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){
206906 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
206907 int iOff = pIter->iLeafOffset;
206908
206909 ASSERT_SZLEAF_OK(pIter->pLeaf);
206910 if( iOff>=pIter->pLeaf->szLeaf ){
206911 fts5SegIterNextPage(p, pIter);
206912 if( pIter->pLeaf==0 ){
206913 if( p->rc==SQLITE_OK ) p->rc = FTS5_CORRUPT;
206914 return;
206915 }
206916 iOff = 4;
206917 a = pIter->pLeaf->p;
206918 }
206919 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
206920 pIter->iLeafOffset = iOff;
206921}
206922
206923/*
206924** Fts5SegIter.iLeafOffset currently points to the first byte of the
206925** "nSuffix" field of a term. Function parameter nKeep contains the value
206926** of the "nPrefix" field (if there was one - it is passed 0 if this is
206927** the first term in the segment).
206928**
206929** This function populates:
206930**
206931** Fts5SegIter.term
206932** Fts5SegIter.rowid
206933**
206934** accordingly and leaves (Fts5SegIter.iLeafOffset) set to the content of
206935** the first position list. The position list belonging to document
206936** (Fts5SegIter.iRowid).
206937*/
206938static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){
206939 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
206940 int iOff = pIter->iLeafOffset; /* Offset to read at */
206941 int nNew; /* Bytes of new data */
206942
206943 iOff += fts5GetVarint32(&a[iOff], nNew);
206944 if( iOff+nNew>pIter->pLeaf->nn ){
206945 p->rc = FTS5_CORRUPT;
206946 return;
206947 }
206948 pIter->term.n = nKeep;
206949 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
206950 iOff += nNew;
206951 pIter->iTermLeafOffset = iOff;
206952 pIter->iTermLeafPgno = pIter->iLeafPgno;
206953 pIter->iLeafOffset = iOff;
206954
206955 if( pIter->iPgidxOff>=pIter->pLeaf->nn ){
206956 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
206957 }else{
206958 int nExtra;
206959 pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], nExtra);
206960 pIter->iEndofDoclist += nExtra;
206961 }
206962
206963 fts5SegIterLoadRowid(p, pIter);
206964}
206965
206966static void fts5SegIterNext(Fts5Index*, Fts5SegIter*, int*);
206967static void fts5SegIterNext_Reverse(Fts5Index*, Fts5SegIter*, int*);
206968static void fts5SegIterNext_None(Fts5Index*, Fts5SegIter*, int*);
206969
206970static void fts5SegIterSetNext(Fts5Index *p, Fts5SegIter *pIter){
206971 if( pIter->flags & FTS5_SEGITER_REVERSE ){
206972 pIter->xNext = fts5SegIterNext_Reverse;
206973 }else if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
206974 pIter->xNext = fts5SegIterNext_None;
206975 }else{
206976 pIter->xNext = fts5SegIterNext;
206977 }
206978}
206979
206980/*
206981** Initialize the iterator object pIter to iterate through the entries in
206982** segment pSeg. The iterator is left pointing to the first entry when
206983** this function returns.
206984**
206985** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
206986** an error has already occurred when this function is called, it is a no-op.
206987*/
206988static void fts5SegIterInit(
206989 Fts5Index *p, /* FTS index object */
206990 Fts5StructureSegment *pSeg, /* Description of segment */
206991 Fts5SegIter *pIter /* Object to populate */
206992){
206993 if( pSeg->pgnoFirst==0 ){
206994 /* This happens if the segment is being used as an input to an incremental
206995 ** merge and all data has already been "trimmed". See function
206996 ** fts5TrimSegments() for details. In this case leave the iterator empty.
206997 ** The caller will see the (pIter->pLeaf==0) and assume the iterator is
206998 ** at EOF already. */
206999 assert( pIter->pLeaf==0 );
207000 return;
207001 }
207002
207003 if( p->rc==SQLITE_OK ){
207004 memset(pIter, 0, sizeof(*pIter));
207005 fts5SegIterSetNext(p, pIter);
207006 pIter->pSeg = pSeg;
207007 pIter->iLeafPgno = pSeg->pgnoFirst-1;
207008 fts5SegIterNextPage(p, pIter);
207009 }
207010
207011 if( p->rc==SQLITE_OK ){
207012 pIter->iLeafOffset = 4;
207013 assert_nc( pIter->pLeaf->nn>4 );
207014 assert( fts5LeafFirstTermOff(pIter->pLeaf)==4 );
207015 pIter->iPgidxOff = pIter->pLeaf->szLeaf+1;
207016 fts5SegIterLoadTerm(p, pIter, 0);
207017 fts5SegIterLoadNPos(p, pIter);
207018 }
207019}
207020
207021/*
207022** This function is only ever called on iterators created by calls to
207023** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set.
207024**
207025** The iterator is in an unusual state when this function is called: the
207026** Fts5SegIter.iLeafOffset variable is set to the offset of the start of
207027** the position-list size field for the first relevant rowid on the page.
207028** Fts5SegIter.rowid is set, but nPos and bDel are not.
207029**
207030** This function advances the iterator so that it points to the last
207031** relevant rowid on the page and, if necessary, initializes the
207032** aRowidOffset[] and iRowidOffset variables. At this point the iterator
207033** is in its regular state - Fts5SegIter.iLeafOffset points to the first
207034** byte of the position list content associated with said rowid.
207035*/
207036static void fts5SegIterReverseInitPage(Fts5Index *p, Fts5SegIter *pIter){
207037 int eDetail = p->pConfig->eDetail;
207038 int n = pIter->pLeaf->szLeaf;
207039 int i = pIter->iLeafOffset;
207040 u8 *a = pIter->pLeaf->p;
207041 int iRowidOffset = 0;
207042
207043 if( n>pIter->iEndofDoclist ){
207044 n = pIter->iEndofDoclist;
207045 }
207046
207047 ASSERT_SZLEAF_OK(pIter->pLeaf);
207048 while( 1 ){
207049 i64 iDelta = 0;
207050
207051 if( eDetail==FTS5_DETAIL_NONE ){
207052 /* todo */
207053 if( i<n && a[i]==0 ){
207054 i++;
207055 if( i<n && a[i]==0 ) i++;
207056 }
207057 }else{
207058 int nPos;
207059 int bDummy;
207060 i += fts5GetPoslistSize(&a[i], &nPos, &bDummy);
207061 i += nPos;
207062 }
207063 if( i>=n ) break;
207064 i += fts5GetVarint(&a[i], (u64*)&iDelta);
207065 pIter->iRowid += iDelta;
207066
207067 /* If necessary, grow the pIter->aRowidOffset[] array. */
207068 if( iRowidOffset>=pIter->nRowidOffset ){
207069 int nNew = pIter->nRowidOffset + 8;
207070 int *aNew = (int*)sqlite3_realloc(pIter->aRowidOffset, nNew*sizeof(int));
207071 if( aNew==0 ){
207072 p->rc = SQLITE_NOMEM;
207073 break;
207074 }
207075 pIter->aRowidOffset = aNew;
207076 pIter->nRowidOffset = nNew;
207077 }
207078
207079 pIter->aRowidOffset[iRowidOffset++] = pIter->iLeafOffset;
207080 pIter->iLeafOffset = i;
207081 }
207082 pIter->iRowidOffset = iRowidOffset;
207083 fts5SegIterLoadNPos(p, pIter);
207084}
207085
207086/*
207087**
207088*/
207089static void fts5SegIterReverseNewPage(Fts5Index *p, Fts5SegIter *pIter){
207090 assert( pIter->flags & FTS5_SEGITER_REVERSE );
207091 assert( pIter->flags & FTS5_SEGITER_ONETERM );
207092
207093 fts5DataRelease(pIter->pLeaf);
207094 pIter->pLeaf = 0;
207095 while( p->rc==SQLITE_OK && pIter->iLeafPgno>pIter->iTermLeafPgno ){
207096 Fts5Data *pNew;
207097 pIter->iLeafPgno--;
207098 pNew = fts5DataRead(p, FTS5_SEGMENT_ROWID(
207099 pIter->pSeg->iSegid, pIter->iLeafPgno
207100 ));
207101 if( pNew ){
207102 /* iTermLeafOffset may be equal to szLeaf if the term is the last
207103 ** thing on the page - i.e. the first rowid is on the following page.
207104 ** In this case leave pIter->pLeaf==0, this iterator is at EOF. */
207105 if( pIter->iLeafPgno==pIter->iTermLeafPgno ){
207106 assert( pIter->pLeaf==0 );
207107 if( pIter->iTermLeafOffset<pNew->szLeaf ){
207108 pIter->pLeaf = pNew;
207109 pIter->iLeafOffset = pIter->iTermLeafOffset;
207110 }
207111 }else{
207112 int iRowidOff;
207113 iRowidOff = fts5LeafFirstRowidOff(pNew);
207114 if( iRowidOff ){
207115 pIter->pLeaf = pNew;
207116 pIter->iLeafOffset = iRowidOff;
207117 }
207118 }
207119
207120 if( pIter->pLeaf ){
207121 u8 *a = &pIter->pLeaf->p[pIter->iLeafOffset];
207122 pIter->iLeafOffset += fts5GetVarint(a, (u64*)&pIter->iRowid);
207123 break;
207124 }else{
207125 fts5DataRelease(pNew);
207126 }
207127 }
207128 }
207129
207130 if( pIter->pLeaf ){
207131 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
207132 fts5SegIterReverseInitPage(p, pIter);
207133 }
207134}
207135
207136/*
207137** Return true if the iterator passed as the second argument currently
207138** points to a delete marker. A delete marker is an entry with a 0 byte
207139** position-list.
207140*/
207141static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
207142 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
207143 return (p->rc==SQLITE_OK && pSeg->pLeaf && pSeg->nPos==0);
207144}
207145
207146/*
207147** Advance iterator pIter to the next entry.
207148**
207149** This version of fts5SegIterNext() is only used by reverse iterators.
207150*/
207151static void fts5SegIterNext_Reverse(
207152 Fts5Index *p, /* FTS5 backend object */
207153 Fts5SegIter *pIter, /* Iterator to advance */
207154 int *pbUnused /* Unused */
207155){
207156 assert( pIter->flags & FTS5_SEGITER_REVERSE );
207157 assert( pIter->pNextLeaf==0 );
207158 UNUSED_PARAM(pbUnused);
207159
207160 if( pIter->iRowidOffset>0 ){
207161 u8 *a = pIter->pLeaf->p;
207162 int iOff;
207163 i64 iDelta;
207164
207165 pIter->iRowidOffset--;
207166 pIter->iLeafOffset = pIter->aRowidOffset[pIter->iRowidOffset];
207167 fts5SegIterLoadNPos(p, pIter);
207168 iOff = pIter->iLeafOffset;
207169 if( p->pConfig->eDetail!=FTS5_DETAIL_NONE ){
207170 iOff += pIter->nPos;
207171 }
207172 fts5GetVarint(&a[iOff], (u64*)&iDelta);
207173 pIter->iRowid -= iDelta;
207174 }else{
207175 fts5SegIterReverseNewPage(p, pIter);
207176 }
207177}
207178
207179/*
207180** Advance iterator pIter to the next entry.
207181**
207182** This version of fts5SegIterNext() is only used if detail=none and the
207183** iterator is not a reverse direction iterator.
207184*/
207185static void fts5SegIterNext_None(
207186 Fts5Index *p, /* FTS5 backend object */
207187 Fts5SegIter *pIter, /* Iterator to advance */
207188 int *pbNewTerm /* OUT: Set for new term */
207189){
207190 int iOff;
207191
207192 assert( p->rc==SQLITE_OK );
207193 assert( (pIter->flags & FTS5_SEGITER_REVERSE)==0 );
207194 assert( p->pConfig->eDetail==FTS5_DETAIL_NONE );
207195
207196 ASSERT_SZLEAF_OK(pIter->pLeaf);
207197 iOff = pIter->iLeafOffset;
207198
207199 /* Next entry is on the next page */
207200 if( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){
207201 fts5SegIterNextPage(p, pIter);
207202 if( p->rc || pIter->pLeaf==0 ) return;
207203 pIter->iRowid = 0;
207204 iOff = 4;
207205 }
207206
207207 if( iOff<pIter->iEndofDoclist ){
207208 /* Next entry is on the current page */
207209 i64 iDelta;
207210 iOff += sqlite3Fts5GetVarint(&pIter->pLeaf->p[iOff], (u64*)&iDelta);
207211 pIter->iLeafOffset = iOff;
207212 pIter->iRowid += iDelta;
207213 }else if( (pIter->flags & FTS5_SEGITER_ONETERM)==0 ){
207214 if( pIter->pSeg ){
207215 int nKeep = 0;
207216 if( iOff!=fts5LeafFirstTermOff(pIter->pLeaf) ){
207217 iOff += fts5GetVarint32(&pIter->pLeaf->p[iOff], nKeep);
207218 }
207219 pIter->iLeafOffset = iOff;
207220 fts5SegIterLoadTerm(p, pIter, nKeep);
207221 }else{
207222 const u8 *pList = 0;
207223 const char *zTerm = 0;
207224 int nList;
207225 sqlite3Fts5HashScanNext(p->pHash);
207226 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
207227 if( pList==0 ) goto next_none_eof;
207228 pIter->pLeaf->p = (u8*)pList;
207229 pIter->pLeaf->nn = nList;
207230 pIter->pLeaf->szLeaf = nList;
207231 pIter->iEndofDoclist = nList;
207232 sqlite3Fts5BufferSet(&p->rc,&pIter->term, (int)strlen(zTerm), (u8*)zTerm);
207233 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
207234 }
207235
207236 if( pbNewTerm ) *pbNewTerm = 1;
207237 }else{
207238 goto next_none_eof;
207239 }
207240
207241 fts5SegIterLoadNPos(p, pIter);
207242
207243 return;
207244 next_none_eof:
207245 fts5DataRelease(pIter->pLeaf);
207246 pIter->pLeaf = 0;
207247}
207248
207249
207250/*
207251** Advance iterator pIter to the next entry.
207252**
207253** If an error occurs, Fts5Index.rc is set to an appropriate error code. It
207254** is not considered an error if the iterator reaches EOF. If an error has
207255** already occurred when this function is called, it is a no-op.
207256*/
207257static void fts5SegIterNext(
207258 Fts5Index *p, /* FTS5 backend object */
207259 Fts5SegIter *pIter, /* Iterator to advance */
207260 int *pbNewTerm /* OUT: Set for new term */
207261){
207262 Fts5Data *pLeaf = pIter->pLeaf;
207263 int iOff;
207264 int bNewTerm = 0;
207265 int nKeep = 0;
207266 u8 *a;
207267 int n;
207268
207269 assert( pbNewTerm==0 || *pbNewTerm==0 );
207270 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
207271
207272 /* Search for the end of the position list within the current page. */
207273 a = pLeaf->p;
207274 n = pLeaf->szLeaf;
207275
207276 ASSERT_SZLEAF_OK(pLeaf);
207277 iOff = pIter->iLeafOffset + pIter->nPos;
207278
207279 if( iOff<n ){
207280 /* The next entry is on the current page. */
207281 assert_nc( iOff<=pIter->iEndofDoclist );
207282 if( iOff>=pIter->iEndofDoclist ){
207283 bNewTerm = 1;
207284 if( iOff!=fts5LeafFirstTermOff(pLeaf) ){
207285 iOff += fts5GetVarint32(&a[iOff], nKeep);
207286 }
207287 }else{
207288 u64 iDelta;
207289 iOff += sqlite3Fts5GetVarint(&a[iOff], &iDelta);
207290 pIter->iRowid += iDelta;
207291 assert_nc( iDelta>0 );
207292 }
207293 pIter->iLeafOffset = iOff;
207294
207295 }else if( pIter->pSeg==0 ){
207296 const u8 *pList = 0;
207297 const char *zTerm = 0;
207298 int nList = 0;
207299 assert( (pIter->flags & FTS5_SEGITER_ONETERM) || pbNewTerm );
207300 if( 0==(pIter->flags & FTS5_SEGITER_ONETERM) ){
207301 sqlite3Fts5HashScanNext(p->pHash);
207302 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
207303 }
207304 if( pList==0 ){
207305 fts5DataRelease(pIter->pLeaf);
207306 pIter->pLeaf = 0;
207307 }else{
207308 pIter->pLeaf->p = (u8*)pList;
207309 pIter->pLeaf->nn = nList;
207310 pIter->pLeaf->szLeaf = nList;
207311 pIter->iEndofDoclist = nList+1;
207312 sqlite3Fts5BufferSet(&p->rc, &pIter->term, (int)strlen(zTerm),
207313 (u8*)zTerm);
207314 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
207315 *pbNewTerm = 1;
207316 }
207317 }else{
207318 iOff = 0;
207319 /* Next entry is not on the current page */
207320 while( iOff==0 ){
207321 fts5SegIterNextPage(p, pIter);
207322 pLeaf = pIter->pLeaf;
207323 if( pLeaf==0 ) break;
207324 ASSERT_SZLEAF_OK(pLeaf);
207325 if( (iOff = fts5LeafFirstRowidOff(pLeaf)) && iOff<pLeaf->szLeaf ){
207326 iOff += sqlite3Fts5GetVarint(&pLeaf->p[iOff], (u64*)&pIter->iRowid);
207327 pIter->iLeafOffset = iOff;
207328
207329 if( pLeaf->nn>pLeaf->szLeaf ){
207330 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
207331 &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclist
207332 );
207333 }
207334 }
207335 else if( pLeaf->nn>pLeaf->szLeaf ){
207336 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
207337 &pLeaf->p[pLeaf->szLeaf], iOff
207338 );
207339 pIter->iLeafOffset = iOff;
207340 pIter->iEndofDoclist = iOff;
207341 bNewTerm = 1;
207342 }
207343 assert_nc( iOff<pLeaf->szLeaf );
207344 if( iOff>pLeaf->szLeaf ){
207345 p->rc = FTS5_CORRUPT;
207346 return;
207347 }
207348 }
207349 }
207350
207351 /* Check if the iterator is now at EOF. If so, return early. */
207352 if( pIter->pLeaf ){
207353 if( bNewTerm ){
207354 if( pIter->flags & FTS5_SEGITER_ONETERM ){
207355 fts5DataRelease(pIter->pLeaf);
207356 pIter->pLeaf = 0;
207357 }else{
207358 fts5SegIterLoadTerm(p, pIter, nKeep);
207359 fts5SegIterLoadNPos(p, pIter);
207360 if( pbNewTerm ) *pbNewTerm = 1;
207361 }
207362 }else{
207363 /* The following could be done by calling fts5SegIterLoadNPos(). But
207364 ** this block is particularly performance critical, so equivalent
207365 ** code is inlined.
207366 **
207367 ** Later: Switched back to fts5SegIterLoadNPos() because it supports
207368 ** detail=none mode. Not ideal.
207369 */
207370 int nSz;
207371 assert( p->rc==SQLITE_OK );
207372 assert( pIter->iLeafOffset<=pIter->pLeaf->nn );
207373 fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz);
207374 pIter->bDel = (nSz & 0x0001);
207375 pIter->nPos = nSz>>1;
207376 assert_nc( pIter->nPos>=0 );
207377 }
207378 }
207379}
207380
207381#define SWAPVAL(T, a, b) { T tmp; tmp=a; a=b; b=tmp; }
207382
207383#define fts5IndexSkipVarint(a, iOff) { \
207384 int iEnd = iOff+9; \
207385 while( (a[iOff++] & 0x80) && iOff<iEnd ); \
207386}
207387
207388/*
207389** Iterator pIter currently points to the first rowid in a doclist. This
207390** function sets the iterator up so that iterates in reverse order through
207391** the doclist.
207392*/
207393static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){
207394 Fts5DlidxIter *pDlidx = pIter->pDlidx;
207395 Fts5Data *pLast = 0;
207396 int pgnoLast = 0;
207397
207398 if( pDlidx ){
207399 int iSegid = pIter->pSeg->iSegid;
207400 pgnoLast = fts5DlidxIterPgno(pDlidx);
207401 pLast = fts5DataRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast));
207402 }else{
207403 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
207404
207405 /* Currently, Fts5SegIter.iLeafOffset points to the first byte of
207406 ** position-list content for the current rowid. Back it up so that it
207407 ** points to the start of the position-list size field. */
207408 int iPoslist;
207409 if( pIter->iTermLeafPgno==pIter->iLeafPgno ){
207410 iPoslist = pIter->iTermLeafOffset;
207411 }else{
207412 iPoslist = 4;
207413 }
207414 fts5IndexSkipVarint(pLeaf->p, iPoslist);
207415 pIter->iLeafOffset = iPoslist;
207416
207417 /* If this condition is true then the largest rowid for the current
207418 ** term may not be stored on the current page. So search forward to
207419 ** see where said rowid really is. */
207420 if( pIter->iEndofDoclist>=pLeaf->szLeaf ){
207421 int pgno;
207422 Fts5StructureSegment *pSeg = pIter->pSeg;
207423
207424 /* The last rowid in the doclist may not be on the current page. Search
207425 ** forward to find the page containing the last rowid. */
207426 for(pgno=pIter->iLeafPgno+1; !p->rc && pgno<=pSeg->pgnoLast; pgno++){
207427 i64 iAbs = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno);
207428 Fts5Data *pNew = fts5DataRead(p, iAbs);
207429 if( pNew ){
207430 int iRowid, bTermless;
207431 iRowid = fts5LeafFirstRowidOff(pNew);
207432 bTermless = fts5LeafIsTermless(pNew);
207433 if( iRowid ){
207434 SWAPVAL(Fts5Data*, pNew, pLast);
207435 pgnoLast = pgno;
207436 }
207437 fts5DataRelease(pNew);
207438 if( bTermless==0 ) break;
207439 }
207440 }
207441 }
207442 }
207443
207444 /* If pLast is NULL at this point, then the last rowid for this doclist
207445 ** lies on the page currently indicated by the iterator. In this case
207446 ** pIter->iLeafOffset is already set to point to the position-list size
207447 ** field associated with the first relevant rowid on the page.
207448 **
207449 ** Or, if pLast is non-NULL, then it is the page that contains the last
207450 ** rowid. In this case configure the iterator so that it points to the
207451 ** first rowid on this page.
207452 */
207453 if( pLast ){
207454 int iOff;
207455 fts5DataRelease(pIter->pLeaf);
207456 pIter->pLeaf = pLast;
207457 pIter->iLeafPgno = pgnoLast;
207458 iOff = fts5LeafFirstRowidOff(pLast);
207459 iOff += fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid);
207460 pIter->iLeafOffset = iOff;
207461
207462 if( fts5LeafIsTermless(pLast) ){
207463 pIter->iEndofDoclist = pLast->nn+1;
207464 }else{
207465 pIter->iEndofDoclist = fts5LeafFirstTermOff(pLast);
207466 }
207467
207468 }
207469
207470 fts5SegIterReverseInitPage(p, pIter);
207471}
207472
207473/*
207474** Iterator pIter currently points to the first rowid of a doclist.
207475** There is a doclist-index associated with the final term on the current
207476** page. If the current term is the last term on the page, load the
207477** doclist-index from disk and initialize an iterator at (pIter->pDlidx).
207478*/
207479static void fts5SegIterLoadDlidx(Fts5Index *p, Fts5SegIter *pIter){
207480 int iSeg = pIter->pSeg->iSegid;
207481 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
207482 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
207483
207484 assert( pIter->flags & FTS5_SEGITER_ONETERM );
207485 assert( pIter->pDlidx==0 );
207486
207487 /* Check if the current doclist ends on this page. If it does, return
207488 ** early without loading the doclist-index (as it belongs to a different
207489 ** term. */
207490 if( pIter->iTermLeafPgno==pIter->iLeafPgno
207491 && pIter->iEndofDoclist<pLeaf->szLeaf
207492 ){
207493 return;
207494 }
207495
207496 pIter->pDlidx = fts5DlidxIterInit(p, bRev, iSeg, pIter->iTermLeafPgno);
207497}
207498
207499/*
207500** The iterator object passed as the second argument currently contains
207501** no valid values except for the Fts5SegIter.pLeaf member variable. This
207502** function searches the leaf page for a term matching (pTerm/nTerm).
207503**
207504** If the specified term is found on the page, then the iterator is left
207505** pointing to it. If argument bGe is zero and the term is not found,
207506** the iterator is left pointing at EOF.
207507**
207508** If bGe is non-zero and the specified term is not found, then the
207509** iterator is left pointing to the smallest term in the segment that
207510** is larger than the specified term, even if this term is not on the
207511** current page.
207512*/
207513static void fts5LeafSeek(
207514 Fts5Index *p, /* Leave any error code here */
207515 int bGe, /* True for a >= search */
207516 Fts5SegIter *pIter, /* Iterator to seek */
207517 const u8 *pTerm, int nTerm /* Term to search for */
207518){
207519 int iOff;
207520 const u8 *a = pIter->pLeaf->p;
207521 int szLeaf = pIter->pLeaf->szLeaf;
207522 int n = pIter->pLeaf->nn;
207523
207524 int nMatch = 0;
207525 int nKeep = 0;
207526 int nNew = 0;
207527 int iTermOff;
207528 int iPgidx; /* Current offset in pgidx */
207529 int bEndOfPage = 0;
207530
207531 assert( p->rc==SQLITE_OK );
207532
207533 iPgidx = szLeaf;
207534 iPgidx += fts5GetVarint32(&a[iPgidx], iTermOff);
207535 iOff = iTermOff;
207536 if( iOff>n ){
207537 p->rc = FTS5_CORRUPT;
207538 return;
207539 }
207540
207541 while( 1 ){
207542
207543 /* Figure out how many new bytes are in this term */
207544 fts5FastGetVarint32(a, iOff, nNew);
207545 if( nKeep<nMatch ){
207546 goto search_failed;
207547 }
207548
207549 assert( nKeep>=nMatch );
207550 if( nKeep==nMatch ){
207551 int nCmp;
207552 int i;
207553 nCmp = MIN(nNew, nTerm-nMatch);
207554 for(i=0; i<nCmp; i++){
207555 if( a[iOff+i]!=pTerm[nMatch+i] ) break;
207556 }
207557 nMatch += i;
207558
207559 if( nTerm==nMatch ){
207560 if( i==nNew ){
207561 goto search_success;
207562 }else{
207563 goto search_failed;
207564 }
207565 }else if( i<nNew && a[iOff+i]>pTerm[nMatch] ){
207566 goto search_failed;
207567 }
207568 }
207569
207570 if( iPgidx>=n ){
207571 bEndOfPage = 1;
207572 break;
207573 }
207574
207575 iPgidx += fts5GetVarint32(&a[iPgidx], nKeep);
207576 iTermOff += nKeep;
207577 iOff = iTermOff;
207578
207579 if( iOff>=n ){
207580 p->rc = FTS5_CORRUPT;
207581 return;
207582 }
207583
207584 /* Read the nKeep field of the next term. */
207585 fts5FastGetVarint32(a, iOff, nKeep);
207586 }
207587
207588 search_failed:
207589 if( bGe==0 ){
207590 fts5DataRelease(pIter->pLeaf);
207591 pIter->pLeaf = 0;
207592 return;
207593 }else if( bEndOfPage ){
207594 do {
207595 fts5SegIterNextPage(p, pIter);
207596 if( pIter->pLeaf==0 ) return;
207597 a = pIter->pLeaf->p;
207598 if( fts5LeafIsTermless(pIter->pLeaf)==0 ){
207599 iPgidx = pIter->pLeaf->szLeaf;
207600 iPgidx += fts5GetVarint32(&pIter->pLeaf->p[iPgidx], iOff);
207601 if( iOff<4 || iOff>=pIter->pLeaf->szLeaf ){
207602 p->rc = FTS5_CORRUPT;
207603 }else{
207604 nKeep = 0;
207605 iTermOff = iOff;
207606 n = pIter->pLeaf->nn;
207607 iOff += fts5GetVarint32(&a[iOff], nNew);
207608 break;
207609 }
207610 }
207611 }while( 1 );
207612 }
207613
207614 search_success:
207615
207616 pIter->iLeafOffset = iOff + nNew;
207617 pIter->iTermLeafOffset = pIter->iLeafOffset;
207618 pIter->iTermLeafPgno = pIter->iLeafPgno;
207619
207620 fts5BufferSet(&p->rc, &pIter->term, nKeep, pTerm);
207621 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
207622
207623 if( iPgidx>=n ){
207624 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
207625 }else{
207626 int nExtra;
207627 iPgidx += fts5GetVarint32(&a[iPgidx], nExtra);
207628 pIter->iEndofDoclist = iTermOff + nExtra;
207629 }
207630 pIter->iPgidxOff = iPgidx;
207631
207632 fts5SegIterLoadRowid(p, pIter);
207633 fts5SegIterLoadNPos(p, pIter);
207634}
207635
207636static sqlite3_stmt *fts5IdxSelectStmt(Fts5Index *p){
207637 if( p->pIdxSelect==0 ){
207638 Fts5Config *pConfig = p->pConfig;
207639 fts5IndexPrepareStmt(p, &p->pIdxSelect, sqlite3_mprintf(
207640 "SELECT pgno FROM '%q'.'%q_idx' WHERE "
207641 "segid=? AND term<=? ORDER BY term DESC LIMIT 1",
207642 pConfig->zDb, pConfig->zName
207643 ));
207644 }
207645 return p->pIdxSelect;
207646}
207647
207648/*
207649** Initialize the object pIter to point to term pTerm/nTerm within segment
207650** pSeg. If there is no such term in the index, the iterator is set to EOF.
207651**
207652** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
207653** an error has already occurred when this function is called, it is a no-op.
207654*/
207655static void fts5SegIterSeekInit(
207656 Fts5Index *p, /* FTS5 backend */
207657 const u8 *pTerm, int nTerm, /* Term to seek to */
207658 int flags, /* Mask of FTS5INDEX_XXX flags */
207659 Fts5StructureSegment *pSeg, /* Description of segment */
207660 Fts5SegIter *pIter /* Object to populate */
207661){
207662 int iPg = 1;
207663 int bGe = (flags & FTS5INDEX_QUERY_SCAN);
207664 int bDlidx = 0; /* True if there is a doclist-index */
207665 sqlite3_stmt *pIdxSelect = 0;
207666
207667 assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 );
207668 assert( pTerm && nTerm );
207669 memset(pIter, 0, sizeof(*pIter));
207670 pIter->pSeg = pSeg;
207671
207672 /* This block sets stack variable iPg to the leaf page number that may
207673 ** contain term (pTerm/nTerm), if it is present in the segment. */
207674 pIdxSelect = fts5IdxSelectStmt(p);
207675 if( p->rc ) return;
207676 sqlite3_bind_int(pIdxSelect, 1, pSeg->iSegid);
207677 sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC);
207678 if( SQLITE_ROW==sqlite3_step(pIdxSelect) ){
207679 i64 val = sqlite3_column_int(pIdxSelect, 0);
207680 iPg = (int)(val>>1);
207681 bDlidx = (val & 0x0001);
207682 }
207683 p->rc = sqlite3_reset(pIdxSelect);
207684 sqlite3_bind_null(pIdxSelect, 2);
207685
207686 if( iPg<pSeg->pgnoFirst ){
207687 iPg = pSeg->pgnoFirst;
207688 bDlidx = 0;
207689 }
207690
207691 pIter->iLeafPgno = iPg - 1;
207692 fts5SegIterNextPage(p, pIter);
207693
207694 if( pIter->pLeaf ){
207695 fts5LeafSeek(p, bGe, pIter, pTerm, nTerm);
207696 }
207697
207698 if( p->rc==SQLITE_OK && bGe==0 ){
207699 pIter->flags |= FTS5_SEGITER_ONETERM;
207700 if( pIter->pLeaf ){
207701 if( flags & FTS5INDEX_QUERY_DESC ){
207702 pIter->flags |= FTS5_SEGITER_REVERSE;
207703 }
207704 if( bDlidx ){
207705 fts5SegIterLoadDlidx(p, pIter);
207706 }
207707 if( flags & FTS5INDEX_QUERY_DESC ){
207708 fts5SegIterReverse(p, pIter);
207709 }
207710 }
207711 }
207712
207713 fts5SegIterSetNext(p, pIter);
207714
207715 /* Either:
207716 **
207717 ** 1) an error has occurred, or
207718 ** 2) the iterator points to EOF, or
207719 ** 3) the iterator points to an entry with term (pTerm/nTerm), or
207720 ** 4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points
207721 ** to an entry with a term greater than or equal to (pTerm/nTerm).
207722 */
207723 assert( p->rc!=SQLITE_OK /* 1 */
207724 || pIter->pLeaf==0 /* 2 */
207725 || fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)==0 /* 3 */
207726 || (bGe && fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)>0) /* 4 */
207727 );
207728}
207729
207730/*
207731** Initialize the object pIter to point to term pTerm/nTerm within the
207732** in-memory hash table. If there is no such term in the hash-table, the
207733** iterator is set to EOF.
207734**
207735** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
207736** an error has already occurred when this function is called, it is a no-op.
207737*/
207738static void fts5SegIterHashInit(
207739 Fts5Index *p, /* FTS5 backend */
207740 const u8 *pTerm, int nTerm, /* Term to seek to */
207741 int flags, /* Mask of FTS5INDEX_XXX flags */
207742 Fts5SegIter *pIter /* Object to populate */
207743){
207744 const u8 *pList = 0;
207745 int nList = 0;
207746 const u8 *z = 0;
207747 int n = 0;
207748
207749 assert( p->pHash );
207750 assert( p->rc==SQLITE_OK );
207751
207752 if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN) ){
207753 p->rc = sqlite3Fts5HashScanInit(p->pHash, (const char*)pTerm, nTerm);
207754 sqlite3Fts5HashScanEntry(p->pHash, (const char**)&z, &pList, &nList);
207755 n = (z ? (int)strlen((const char*)z) : 0);
207756 }else{
207757 pIter->flags |= FTS5_SEGITER_ONETERM;
207758 sqlite3Fts5HashQuery(p->pHash, (const char*)pTerm, nTerm, &pList, &nList);
207759 z = pTerm;
207760 n = nTerm;
207761 }
207762
207763 if( pList ){
207764 Fts5Data *pLeaf;
207765 sqlite3Fts5BufferSet(&p->rc, &pIter->term, n, z);
207766 pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data));
207767 if( pLeaf==0 ) return;
207768 pLeaf->p = (u8*)pList;
207769 pLeaf->nn = pLeaf->szLeaf = nList;
207770 pIter->pLeaf = pLeaf;
207771 pIter->iLeafOffset = fts5GetVarint(pLeaf->p, (u64*)&pIter->iRowid);
207772 pIter->iEndofDoclist = pLeaf->nn;
207773
207774 if( flags & FTS5INDEX_QUERY_DESC ){
207775 pIter->flags |= FTS5_SEGITER_REVERSE;
207776 fts5SegIterReverseInitPage(p, pIter);
207777 }else{
207778 fts5SegIterLoadNPos(p, pIter);
207779 }
207780 }
207781
207782 fts5SegIterSetNext(p, pIter);
207783}
207784
207785/*
207786** Zero the iterator passed as the only argument.
207787*/
207788static void fts5SegIterClear(Fts5SegIter *pIter){
207789 fts5BufferFree(&pIter->term);
207790 fts5DataRelease(pIter->pLeaf);
207791 fts5DataRelease(pIter->pNextLeaf);
207792 fts5DlidxIterFree(pIter->pDlidx);
207793 sqlite3_free(pIter->aRowidOffset);
207794 memset(pIter, 0, sizeof(Fts5SegIter));
207795}
207796
207797#ifdef SQLITE_DEBUG
207798
207799/*
207800** This function is used as part of the big assert() procedure implemented by
207801** fts5AssertMultiIterSetup(). It ensures that the result currently stored
207802** in *pRes is the correct result of comparing the current positions of the
207803** two iterators.
207804*/
207805static void fts5AssertComparisonResult(
207806 Fts5Iter *pIter,
207807 Fts5SegIter *p1,
207808 Fts5SegIter *p2,
207809 Fts5CResult *pRes
207810){
207811 int i1 = p1 - pIter->aSeg;
207812 int i2 = p2 - pIter->aSeg;
207813
207814 if( p1->pLeaf || p2->pLeaf ){
207815 if( p1->pLeaf==0 ){
207816 assert( pRes->iFirst==i2 );
207817 }else if( p2->pLeaf==0 ){
207818 assert( pRes->iFirst==i1 );
207819 }else{
207820 int nMin = MIN(p1->term.n, p2->term.n);
207821 int res = memcmp(p1->term.p, p2->term.p, nMin);
207822 if( res==0 ) res = p1->term.n - p2->term.n;
207823
207824 if( res==0 ){
207825 assert( pRes->bTermEq==1 );
207826 assert( p1->iRowid!=p2->iRowid );
207827 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : 1;
207828 }else{
207829 assert( pRes->bTermEq==0 );
207830 }
207831
207832 if( res<0 ){
207833 assert( pRes->iFirst==i1 );
207834 }else{
207835 assert( pRes->iFirst==i2 );
207836 }
207837 }
207838 }
207839}
207840
207841/*
207842** This function is a no-op unless SQLITE_DEBUG is defined when this module
207843** is compiled. In that case, this function is essentially an assert()
207844** statement used to verify that the contents of the pIter->aFirst[] array
207845** are correct.
207846*/
207847static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
207848 if( p->rc==SQLITE_OK ){
207849 Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
207850 int i;
207851
207852 assert( (pFirst->pLeaf==0)==pIter->base.bEof );
207853
207854 /* Check that pIter->iSwitchRowid is set correctly. */
207855 for(i=0; i<pIter->nSeg; i++){
207856 Fts5SegIter *p1 = &pIter->aSeg[i];
207857 assert( p1==pFirst
207858 || p1->pLeaf==0
207859 || fts5BufferCompare(&pFirst->term, &p1->term)
207860 || p1->iRowid==pIter->iSwitchRowid
207861 || (p1->iRowid<pIter->iSwitchRowid)==pIter->bRev
207862 );
207863 }
207864
207865 for(i=0; i<pIter->nSeg; i+=2){
207866 Fts5SegIter *p1 = &pIter->aSeg[i];
207867 Fts5SegIter *p2 = &pIter->aSeg[i+1];
207868 Fts5CResult *pRes = &pIter->aFirst[(pIter->nSeg + i) / 2];
207869 fts5AssertComparisonResult(pIter, p1, p2, pRes);
207870 }
207871
207872 for(i=1; i<(pIter->nSeg / 2); i+=2){
207873 Fts5SegIter *p1 = &pIter->aSeg[ pIter->aFirst[i*2].iFirst ];
207874 Fts5SegIter *p2 = &pIter->aSeg[ pIter->aFirst[i*2+1].iFirst ];
207875 Fts5CResult *pRes = &pIter->aFirst[i];
207876 fts5AssertComparisonResult(pIter, p1, p2, pRes);
207877 }
207878 }
207879}
207880#else
207881# define fts5AssertMultiIterSetup(x,y)
207882#endif
207883
207884/*
207885** Do the comparison necessary to populate pIter->aFirst[iOut].
207886**
207887** If the returned value is non-zero, then it is the index of an entry
207888** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
207889** to a key that is a duplicate of another, higher priority,
207890** segment-iterator in the pSeg->aSeg[] array.
207891*/
207892static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
207893 int i1; /* Index of left-hand Fts5SegIter */
207894 int i2; /* Index of right-hand Fts5SegIter */
207895 int iRes;
207896 Fts5SegIter *p1; /* Left-hand Fts5SegIter */
207897 Fts5SegIter *p2; /* Right-hand Fts5SegIter */
207898 Fts5CResult *pRes = &pIter->aFirst[iOut];
207899
207900 assert( iOut<pIter->nSeg && iOut>0 );
207901 assert( pIter->bRev==0 || pIter->bRev==1 );
207902
207903 if( iOut>=(pIter->nSeg/2) ){
207904 i1 = (iOut - pIter->nSeg/2) * 2;
207905 i2 = i1 + 1;
207906 }else{
207907 i1 = pIter->aFirst[iOut*2].iFirst;
207908 i2 = pIter->aFirst[iOut*2+1].iFirst;
207909 }
207910 p1 = &pIter->aSeg[i1];
207911 p2 = &pIter->aSeg[i2];
207912
207913 pRes->bTermEq = 0;
207914 if( p1->pLeaf==0 ){ /* If p1 is at EOF */
207915 iRes = i2;
207916 }else if( p2->pLeaf==0 ){ /* If p2 is at EOF */
207917 iRes = i1;
207918 }else{
207919 int res = fts5BufferCompare(&p1->term, &p2->term);
207920 if( res==0 ){
207921 assert( i2>i1 );
207922 assert( i2!=0 );
207923 pRes->bTermEq = 1;
207924 if( p1->iRowid==p2->iRowid ){
207925 p1->bDel = p2->bDel;
207926 return i2;
207927 }
207928 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1;
207929 }
207930 assert( res!=0 );
207931 if( res<0 ){
207932 iRes = i1;
207933 }else{
207934 iRes = i2;
207935 }
207936 }
207937
207938 pRes->iFirst = (u16)iRes;
207939 return 0;
207940}
207941
207942/*
207943** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
207944** It is an error if leaf iLeafPgno does not exist or contains no rowids.
207945*/
207946static void fts5SegIterGotoPage(
207947 Fts5Index *p, /* FTS5 backend object */
207948 Fts5SegIter *pIter, /* Iterator to advance */
207949 int iLeafPgno
207950){
207951 assert( iLeafPgno>pIter->iLeafPgno );
207952
207953 if( iLeafPgno>pIter->pSeg->pgnoLast ){
207954 p->rc = FTS5_CORRUPT;
207955 }else{
207956 fts5DataRelease(pIter->pNextLeaf);
207957 pIter->pNextLeaf = 0;
207958 pIter->iLeafPgno = iLeafPgno-1;
207959 fts5SegIterNextPage(p, pIter);
207960 assert( p->rc!=SQLITE_OK || pIter->iLeafPgno==iLeafPgno );
207961
207962 if( p->rc==SQLITE_OK ){
207963 int iOff;
207964 u8 *a = pIter->pLeaf->p;
207965 int n = pIter->pLeaf->szLeaf;
207966
207967 iOff = fts5LeafFirstRowidOff(pIter->pLeaf);
207968 if( iOff<4 || iOff>=n ){
207969 p->rc = FTS5_CORRUPT;
207970 }else{
207971 iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
207972 pIter->iLeafOffset = iOff;
207973 fts5SegIterLoadNPos(p, pIter);
207974 }
207975 }
207976 }
207977}
207978
207979/*
207980** Advance the iterator passed as the second argument until it is at or
207981** past rowid iFrom. Regardless of the value of iFrom, the iterator is
207982** always advanced at least once.
207983*/
207984static void fts5SegIterNextFrom(
207985 Fts5Index *p, /* FTS5 backend object */
207986 Fts5SegIter *pIter, /* Iterator to advance */
207987 i64 iMatch /* Advance iterator at least this far */
207988){
207989 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
207990 Fts5DlidxIter *pDlidx = pIter->pDlidx;
207991 int iLeafPgno = pIter->iLeafPgno;
207992 int bMove = 1;
207993
207994 assert( pIter->flags & FTS5_SEGITER_ONETERM );
207995 assert( pIter->pDlidx );
207996 assert( pIter->pLeaf );
207997
207998 if( bRev==0 ){
207999 while( !fts5DlidxIterEof(p, pDlidx) && iMatch>fts5DlidxIterRowid(pDlidx) ){
208000 iLeafPgno = fts5DlidxIterPgno(pDlidx);
208001 fts5DlidxIterNext(p, pDlidx);
208002 }
208003 assert_nc( iLeafPgno>=pIter->iLeafPgno || p->rc );
208004 if( iLeafPgno>pIter->iLeafPgno ){
208005 fts5SegIterGotoPage(p, pIter, iLeafPgno);
208006 bMove = 0;
208007 }
208008 }else{
208009 assert( pIter->pNextLeaf==0 );
208010 assert( iMatch<pIter->iRowid );
208011 while( !fts5DlidxIterEof(p, pDlidx) && iMatch<fts5DlidxIterRowid(pDlidx) ){
208012 fts5DlidxIterPrev(p, pDlidx);
208013 }
208014 iLeafPgno = fts5DlidxIterPgno(pDlidx);
208015
208016 assert( fts5DlidxIterEof(p, pDlidx) || iLeafPgno<=pIter->iLeafPgno );
208017
208018 if( iLeafPgno<pIter->iLeafPgno ){
208019 pIter->iLeafPgno = iLeafPgno+1;
208020 fts5SegIterReverseNewPage(p, pIter);
208021 bMove = 0;
208022 }
208023 }
208024
208025 do{
208026 if( bMove && p->rc==SQLITE_OK ) pIter->xNext(p, pIter, 0);
208027 if( pIter->pLeaf==0 ) break;
208028 if( bRev==0 && pIter->iRowid>=iMatch ) break;
208029 if( bRev!=0 && pIter->iRowid<=iMatch ) break;
208030 bMove = 1;
208031 }while( p->rc==SQLITE_OK );
208032}
208033
208034
208035/*
208036** Free the iterator object passed as the second argument.
208037*/
208038static void fts5MultiIterFree(Fts5Iter *pIter){
208039 if( pIter ){
208040 int i;
208041 for(i=0; i<pIter->nSeg; i++){
208042 fts5SegIterClear(&pIter->aSeg[i]);
208043 }
208044 fts5StructureRelease(pIter->pStruct);
208045 fts5BufferFree(&pIter->poslist);
208046 sqlite3_free(pIter);
208047 }
208048}
208049
208050static void fts5MultiIterAdvanced(
208051 Fts5Index *p, /* FTS5 backend to iterate within */
208052 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
208053 int iChanged, /* Index of sub-iterator just advanced */
208054 int iMinset /* Minimum entry in aFirst[] to set */
208055){
208056 int i;
208057 for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK; i=i/2){
208058 int iEq;
208059 if( (iEq = fts5MultiIterDoCompare(pIter, i)) ){
208060 Fts5SegIter *pSeg = &pIter->aSeg[iEq];
208061 assert( p->rc==SQLITE_OK );
208062 pSeg->xNext(p, pSeg, 0);
208063 i = pIter->nSeg + iEq;
208064 }
208065 }
208066}
208067
208068/*
208069** Sub-iterator iChanged of iterator pIter has just been advanced. It still
208070** points to the same term though - just a different rowid. This function
208071** attempts to update the contents of the pIter->aFirst[] accordingly.
208072** If it does so successfully, 0 is returned. Otherwise 1.
208073**
208074** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
208075** on the iterator instead. That function does the same as this one, except
208076** that it deals with more complicated cases as well.
208077*/
208078static int fts5MultiIterAdvanceRowid(
208079 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
208080 int iChanged, /* Index of sub-iterator just advanced */
208081 Fts5SegIter **ppFirst
208082){
208083 Fts5SegIter *pNew = &pIter->aSeg[iChanged];
208084
208085 if( pNew->iRowid==pIter->iSwitchRowid
208086 || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
208087 ){
208088 int i;
208089 Fts5SegIter *pOther = &pIter->aSeg[iChanged ^ 0x0001];
208090 pIter->iSwitchRowid = pIter->bRev ? SMALLEST_INT64 : LARGEST_INT64;
208091 for(i=(pIter->nSeg+iChanged)/2; 1; i=i/2){
208092 Fts5CResult *pRes = &pIter->aFirst[i];
208093
208094 assert( pNew->pLeaf );
208095 assert( pRes->bTermEq==0 || pOther->pLeaf );
208096
208097 if( pRes->bTermEq ){
208098 if( pNew->iRowid==pOther->iRowid ){
208099 return 1;
208100 }else if( (pOther->iRowid>pNew->iRowid)==pIter->bRev ){
208101 pIter->iSwitchRowid = pOther->iRowid;
208102 pNew = pOther;
208103 }else if( (pOther->iRowid>pIter->iSwitchRowid)==pIter->bRev ){
208104 pIter->iSwitchRowid = pOther->iRowid;
208105 }
208106 }
208107 pRes->iFirst = (u16)(pNew - pIter->aSeg);
208108 if( i==1 ) break;
208109
208110 pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
208111 }
208112 }
208113
208114 *ppFirst = pNew;
208115 return 0;
208116}
208117
208118/*
208119** Set the pIter->bEof variable based on the state of the sub-iterators.
208120*/
208121static void fts5MultiIterSetEof(Fts5Iter *pIter){
208122 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
208123 pIter->base.bEof = pSeg->pLeaf==0;
208124 pIter->iSwitchRowid = pSeg->iRowid;
208125}
208126
208127/*
208128** Move the iterator to the next entry.
208129**
208130** If an error occurs, an error code is left in Fts5Index.rc. It is not
208131** considered an error if the iterator reaches EOF, or if it is already at
208132** EOF when this function is called.
208133*/
208134static void fts5MultiIterNext(
208135 Fts5Index *p,
208136 Fts5Iter *pIter,
208137 int bFrom, /* True if argument iFrom is valid */
208138 i64 iFrom /* Advance at least as far as this */
208139){
208140 int bUseFrom = bFrom;
208141 assert( pIter->base.bEof==0 );
208142 while( p->rc==SQLITE_OK ){
208143 int iFirst = pIter->aFirst[1].iFirst;
208144 int bNewTerm = 0;
208145 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
208146 assert( p->rc==SQLITE_OK );
208147 if( bUseFrom && pSeg->pDlidx ){
208148 fts5SegIterNextFrom(p, pSeg, iFrom);
208149 }else{
208150 pSeg->xNext(p, pSeg, &bNewTerm);
208151 }
208152
208153 if( pSeg->pLeaf==0 || bNewTerm
208154 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
208155 ){
208156 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
208157 fts5MultiIterSetEof(pIter);
208158 pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
208159 if( pSeg->pLeaf==0 ) return;
208160 }
208161
208162 fts5AssertMultiIterSetup(p, pIter);
208163 assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf );
208164 if( pIter->bSkipEmpty==0 || pSeg->nPos ){
208165 pIter->xSetOutputs(pIter, pSeg);
208166 return;
208167 }
208168 bUseFrom = 0;
208169 }
208170}
208171
208172static void fts5MultiIterNext2(
208173 Fts5Index *p,
208174 Fts5Iter *pIter,
208175 int *pbNewTerm /* OUT: True if *might* be new term */
208176){
208177 assert( pIter->bSkipEmpty );
208178 if( p->rc==SQLITE_OK ){
208179 *pbNewTerm = 0;
208180 do{
208181 int iFirst = pIter->aFirst[1].iFirst;
208182 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
208183 int bNewTerm = 0;
208184
208185 assert( p->rc==SQLITE_OK );
208186 pSeg->xNext(p, pSeg, &bNewTerm);
208187 if( pSeg->pLeaf==0 || bNewTerm
208188 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
208189 ){
208190 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
208191 fts5MultiIterSetEof(pIter);
208192 *pbNewTerm = 1;
208193 }
208194 fts5AssertMultiIterSetup(p, pIter);
208195
208196 }while( fts5MultiIterIsEmpty(p, pIter) );
208197 }
208198}
208199
208200static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
208201 UNUSED_PARAM2(pUnused1, pUnused2);
208202}
208203
208204static Fts5Iter *fts5MultiIterAlloc(
208205 Fts5Index *p, /* FTS5 backend to iterate within */
208206 int nSeg
208207){
208208 Fts5Iter *pNew;
208209 int nSlot; /* Power of two >= nSeg */
208210
208211 for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
208212 pNew = fts5IdxMalloc(p,
208213 sizeof(Fts5Iter) + /* pNew */
208214 sizeof(Fts5SegIter) * (nSlot-1) + /* pNew->aSeg[] */
208215 sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */
208216 );
208217 if( pNew ){
208218 pNew->nSeg = nSlot;
208219 pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
208220 pNew->pIndex = p;
208221 pNew->xSetOutputs = fts5IterSetOutputs_Noop;
208222 }
208223 return pNew;
208224}
208225
208226static void fts5PoslistCallback(
208227 Fts5Index *pUnused,
208228 void *pContext,
208229 const u8 *pChunk, int nChunk
208230){
208231 UNUSED_PARAM(pUnused);
208232 assert_nc( nChunk>=0 );
208233 if( nChunk>0 ){
208234 fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk);
208235 }
208236}
208237
208238typedef struct PoslistCallbackCtx PoslistCallbackCtx;
208239struct PoslistCallbackCtx {
208240 Fts5Buffer *pBuf; /* Append to this buffer */
208241 Fts5Colset *pColset; /* Restrict matches to this column */
208242 int eState; /* See above */
208243};
208244
208245typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
208246struct PoslistOffsetsCtx {
208247 Fts5Buffer *pBuf; /* Append to this buffer */
208248 Fts5Colset *pColset; /* Restrict matches to this column */
208249 int iRead;
208250 int iWrite;
208251};
208252
208253/*
208254** TODO: Make this more efficient!
208255*/
208256static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
208257 int i;
208258 for(i=0; i<pColset->nCol; i++){
208259 if( pColset->aiCol[i]==iCol ) return 1;
208260 }
208261 return 0;
208262}
208263
208264static void fts5PoslistOffsetsCallback(
208265 Fts5Index *pUnused,
208266 void *pContext,
208267 const u8 *pChunk, int nChunk
208268){
208269 PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
208270 UNUSED_PARAM(pUnused);
208271 assert_nc( nChunk>=0 );
208272 if( nChunk>0 ){
208273 int i = 0;
208274 while( i<nChunk ){
208275 int iVal;
208276 i += fts5GetVarint32(&pChunk[i], iVal);
208277 iVal += pCtx->iRead - 2;
208278 pCtx->iRead = iVal;
208279 if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
208280 fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite);
208281 pCtx->iWrite = iVal;
208282 }
208283 }
208284 }
208285}
208286
208287static void fts5PoslistFilterCallback(
208288 Fts5Index *pUnused,
208289 void *pContext,
208290 const u8 *pChunk, int nChunk
208291){
208292 PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
208293 UNUSED_PARAM(pUnused);
208294 assert_nc( nChunk>=0 );
208295 if( nChunk>0 ){
208296 /* Search through to find the first varint with value 1. This is the
208297 ** start of the next columns hits. */
208298 int i = 0;
208299 int iStart = 0;
208300
208301 if( pCtx->eState==2 ){
208302 int iCol;
208303 fts5FastGetVarint32(pChunk, i, iCol);
208304 if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
208305 pCtx->eState = 1;
208306 fts5BufferSafeAppendVarint(pCtx->pBuf, 1);
208307 }else{
208308 pCtx->eState = 0;
208309 }
208310 }
208311
208312 do {
208313 while( i<nChunk && pChunk[i]!=0x01 ){
208314 while( pChunk[i] & 0x80 ) i++;
208315 i++;
208316 }
208317 if( pCtx->eState ){
208318 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
208319 }
208320 if( i<nChunk ){
208321 int iCol;
208322 iStart = i;
208323 i++;
208324 if( i>=nChunk ){
208325 pCtx->eState = 2;
208326 }else{
208327 fts5FastGetVarint32(pChunk, i, iCol);
208328 pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
208329 if( pCtx->eState ){
208330 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
208331 iStart = i;
208332 }
208333 }
208334 }
208335 }while( i<nChunk );
208336 }
208337}
208338
208339static void fts5ChunkIterate(
208340 Fts5Index *p, /* Index object */
208341 Fts5SegIter *pSeg, /* Poslist of this iterator */
208342 void *pCtx, /* Context pointer for xChunk callback */
208343 void (*xChunk)(Fts5Index*, void*, const u8*, int)
208344){
208345 int nRem = pSeg->nPos; /* Number of bytes still to come */
208346 Fts5Data *pData = 0;
208347 u8 *pChunk = &pSeg->pLeaf->p[pSeg->iLeafOffset];
208348 int nChunk = MIN(nRem, pSeg->pLeaf->szLeaf - pSeg->iLeafOffset);
208349 int pgno = pSeg->iLeafPgno;
208350 int pgnoSave = 0;
208351
208352 /* This function does notmwork with detail=none databases. */
208353 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
208354
208355 if( (pSeg->flags & FTS5_SEGITER_REVERSE)==0 ){
208356 pgnoSave = pgno+1;
208357 }
208358
208359 while( 1 ){
208360 xChunk(p, pCtx, pChunk, nChunk);
208361 nRem -= nChunk;
208362 fts5DataRelease(pData);
208363 if( nRem<=0 ){
208364 break;
208365 }else{
208366 pgno++;
208367 pData = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno));
208368 if( pData==0 ) break;
208369 pChunk = &pData->p[4];
208370 nChunk = MIN(nRem, pData->szLeaf - 4);
208371 if( pgno==pgnoSave ){
208372 assert( pSeg->pNextLeaf==0 );
208373 pSeg->pNextLeaf = pData;
208374 pData = 0;
208375 }
208376 }
208377 }
208378}
208379
208380/*
208381** Iterator pIter currently points to a valid entry (not EOF). This
208382** function appends the position list data for the current entry to
208383** buffer pBuf. It does not make a copy of the position-list size
208384** field.
208385*/
208386static void fts5SegiterPoslist(
208387 Fts5Index *p,
208388 Fts5SegIter *pSeg,
208389 Fts5Colset *pColset,
208390 Fts5Buffer *pBuf
208391){
208392 if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos) ){
208393 if( pColset==0 ){
208394 fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
208395 }else{
208396 if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){
208397 PoslistCallbackCtx sCtx;
208398 sCtx.pBuf = pBuf;
208399 sCtx.pColset = pColset;
208400 sCtx.eState = fts5IndexColsetTest(pColset, 0);
208401 assert( sCtx.eState==0 || sCtx.eState==1 );
208402 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
208403 }else{
208404 PoslistOffsetsCtx sCtx;
208405 memset(&sCtx, 0, sizeof(sCtx));
208406 sCtx.pBuf = pBuf;
208407 sCtx.pColset = pColset;
208408 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
208409 }
208410 }
208411 }
208412}
208413
208414/*
208415** IN/OUT parameter (*pa) points to a position list n bytes in size. If
208416** the position list contains entries for column iCol, then (*pa) is set
208417** to point to the sub-position-list for that column and the number of
208418** bytes in it returned. Or, if the argument position list does not
208419** contain any entries for column iCol, return 0.
208420*/
208421static int fts5IndexExtractCol(
208422 const u8 **pa, /* IN/OUT: Pointer to poslist */
208423 int n, /* IN: Size of poslist in bytes */
208424 int iCol /* Column to extract from poslist */
208425){
208426 int iCurrent = 0; /* Anything before the first 0x01 is col 0 */
208427 const u8 *p = *pa;
208428 const u8 *pEnd = &p[n]; /* One byte past end of position list */
208429
208430 while( iCol>iCurrent ){
208431 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
208432 ** not part of a varint. Note that it is not possible for a negative
208433 ** or extremely large varint to occur within an uncorrupted position
208434 ** list. So the last byte of each varint may be assumed to have a clear
208435 ** 0x80 bit. */
208436 while( *p!=0x01 ){
208437 while( *p++ & 0x80 );
208438 if( p>=pEnd ) return 0;
208439 }
208440 *pa = p++;
208441 iCurrent = *p++;
208442 if( iCurrent & 0x80 ){
208443 p--;
208444 p += fts5GetVarint32(p, iCurrent);
208445 }
208446 }
208447 if( iCol!=iCurrent ) return 0;
208448
208449 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
208450 ** not part of a varint */
208451 while( p<pEnd && *p!=0x01 ){
208452 while( *p++ & 0x80 );
208453 }
208454
208455 return p - (*pa);
208456}
208457
208458static void fts5IndexExtractColset(
208459 int *pRc,
208460 Fts5Colset *pColset, /* Colset to filter on */
208461 const u8 *pPos, int nPos, /* Position list */
208462 Fts5Buffer *pBuf /* Output buffer */
208463){
208464 if( *pRc==SQLITE_OK ){
208465 int i;
208466 fts5BufferZero(pBuf);
208467 for(i=0; i<pColset->nCol; i++){
208468 const u8 *pSub = pPos;
208469 int nSub = fts5IndexExtractCol(&pSub, nPos, pColset->aiCol[i]);
208470 if( nSub ){
208471 fts5BufferAppendBlob(pRc, pBuf, nSub, pSub);
208472 }
208473 }
208474 }
208475}
208476
208477/*
208478** xSetOutputs callback used by detail=none tables.
208479*/
208480static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
208481 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE );
208482 pIter->base.iRowid = pSeg->iRowid;
208483 pIter->base.nData = pSeg->nPos;
208484}
208485
208486/*
208487** xSetOutputs callback used by detail=full and detail=col tables when no
208488** column filters are specified.
208489*/
208490static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
208491 pIter->base.iRowid = pSeg->iRowid;
208492 pIter->base.nData = pSeg->nPos;
208493
208494 assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE );
208495 assert( pIter->pColset==0 );
208496
208497 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
208498 /* All data is stored on the current page. Populate the output
208499 ** variables to point into the body of the page object. */
208500 pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
208501 }else{
208502 /* The data is distributed over two or more pages. Copy it into the
208503 ** Fts5Iter.poslist buffer and then set the output pointer to point
208504 ** to this buffer. */
208505 fts5BufferZero(&pIter->poslist);
208506 fts5SegiterPoslist(pIter->pIndex, pSeg, 0, &pIter->poslist);
208507 pIter->base.pData = pIter->poslist.p;
208508 }
208509}
208510
208511/*
208512** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match
208513** against no columns at all).
208514*/
208515static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){
208516 UNUSED_PARAM(pSeg);
208517 pIter->base.nData = 0;
208518}
208519
208520/*
208521** xSetOutputs callback used by detail=col when there is a column filter
208522** and there are 100 or more columns. Also called as a fallback from
208523** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
208524*/
208525static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
208526 fts5BufferZero(&pIter->poslist);
208527 fts5SegiterPoslist(pIter->pIndex, pSeg, pIter->pColset, &pIter->poslist);
208528 pIter->base.iRowid = pSeg->iRowid;
208529 pIter->base.pData = pIter->poslist.p;
208530 pIter->base.nData = pIter->poslist.n;
208531}
208532
208533/*
208534** xSetOutputs callback used when:
208535**
208536** * detail=col,
208537** * there is a column filter, and
208538** * the table contains 100 or fewer columns.
208539**
208540** The last point is to ensure all column numbers are stored as
208541** single-byte varints.
208542*/
208543static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
208544
208545 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
208546 assert( pIter->pColset );
208547
208548 if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf ){
208549 fts5IterSetOutputs_Col(pIter, pSeg);
208550 }else{
208551 u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
208552 u8 *pEnd = (u8*)&a[pSeg->nPos];
208553 int iPrev = 0;
208554 int *aiCol = pIter->pColset->aiCol;
208555 int *aiColEnd = &aiCol[pIter->pColset->nCol];
208556
208557 u8 *aOut = pIter->poslist.p;
208558 int iPrevOut = 0;
208559
208560 pIter->base.iRowid = pSeg->iRowid;
208561
208562 while( a<pEnd ){
208563 iPrev += (int)a++[0] - 2;
208564 while( *aiCol<iPrev ){
208565 aiCol++;
208566 if( aiCol==aiColEnd ) goto setoutputs_col_out;
208567 }
208568 if( *aiCol==iPrev ){
208569 *aOut++ = (u8)((iPrev - iPrevOut) + 2);
208570 iPrevOut = iPrev;
208571 }
208572 }
208573
208574setoutputs_col_out:
208575 pIter->base.pData = pIter->poslist.p;
208576 pIter->base.nData = aOut - pIter->poslist.p;
208577 }
208578}
208579
208580/*
208581** xSetOutputs callback used by detail=full when there is a column filter.
208582*/
208583static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
208584 Fts5Colset *pColset = pIter->pColset;
208585 pIter->base.iRowid = pSeg->iRowid;
208586
208587 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL );
208588 assert( pColset );
208589
208590 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
208591 /* All data is stored on the current page. Populate the output
208592 ** variables to point into the body of the page object. */
208593 const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
208594 if( pColset->nCol==1 ){
208595 pIter->base.nData = fts5IndexExtractCol(&a, pSeg->nPos,pColset->aiCol[0]);
208596 pIter->base.pData = a;
208597 }else{
208598 int *pRc = &pIter->pIndex->rc;
208599 fts5BufferZero(&pIter->poslist);
208600 fts5IndexExtractColset(pRc, pColset, a, pSeg->nPos, &pIter->poslist);
208601 pIter->base.pData = pIter->poslist.p;
208602 pIter->base.nData = pIter->poslist.n;
208603 }
208604 }else{
208605 /* The data is distributed over two or more pages. Copy it into the
208606 ** Fts5Iter.poslist buffer and then set the output pointer to point
208607 ** to this buffer. */
208608 fts5BufferZero(&pIter->poslist);
208609 fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
208610 pIter->base.pData = pIter->poslist.p;
208611 pIter->base.nData = pIter->poslist.n;
208612 }
208613}
208614
208615static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
208616 if( *pRc==SQLITE_OK ){
208617 Fts5Config *pConfig = pIter->pIndex->pConfig;
208618 if( pConfig->eDetail==FTS5_DETAIL_NONE ){
208619 pIter->xSetOutputs = fts5IterSetOutputs_None;
208620 }
208621
208622 else if( pIter->pColset==0 ){
208623 pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
208624 }
208625
208626 else if( pIter->pColset->nCol==0 ){
208627 pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset;
208628 }
208629
208630 else if( pConfig->eDetail==FTS5_DETAIL_FULL ){
208631 pIter->xSetOutputs = fts5IterSetOutputs_Full;
208632 }
208633
208634 else{
208635 assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS );
208636 if( pConfig->nCol<=100 ){
208637 pIter->xSetOutputs = fts5IterSetOutputs_Col100;
208638 sqlite3Fts5BufferSize(pRc, &pIter->poslist, pConfig->nCol);
208639 }else{
208640 pIter->xSetOutputs = fts5IterSetOutputs_Col;
208641 }
208642 }
208643 }
208644}
208645
208646
208647/*
208648** Allocate a new Fts5Iter object.
208649**
208650** The new object will be used to iterate through data in structure pStruct.
208651** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
208652** is zero or greater, data from the first nSegment segments on level iLevel
208653** is merged.
208654**
208655** The iterator initially points to the first term/rowid entry in the
208656** iterated data.
208657*/
208658static void fts5MultiIterNew(
208659 Fts5Index *p, /* FTS5 backend to iterate within */
208660 Fts5Structure *pStruct, /* Structure of specific index */
208661 int flags, /* FTS5INDEX_QUERY_XXX flags */
208662 Fts5Colset *pColset, /* Colset to filter on (or NULL) */
208663 const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
208664 int iLevel, /* Level to iterate (-1 for all) */
208665 int nSegment, /* Number of segments to merge (iLevel>=0) */
208666 Fts5Iter **ppOut /* New object */
208667){
208668 int nSeg = 0; /* Number of segment-iters in use */
208669 int iIter = 0; /* */
208670 int iSeg; /* Used to iterate through segments */
208671 Fts5StructureLevel *pLvl;
208672 Fts5Iter *pNew;
208673
208674 assert( (pTerm==0 && nTerm==0) || iLevel<0 );
208675
208676 /* Allocate space for the new multi-seg-iterator. */
208677 if( p->rc==SQLITE_OK ){
208678 if( iLevel<0 ){
208679 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
208680 nSeg = pStruct->nSegment;
208681 nSeg += (p->pHash ? 1 : 0);
208682 }else{
208683 nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment);
208684 }
208685 }
208686 *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
208687 if( pNew==0 ) return;
208688 pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC));
208689 pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY));
208690 pNew->pStruct = pStruct;
208691 pNew->pColset = pColset;
208692 fts5StructureRef(pStruct);
208693 if( (flags & FTS5INDEX_QUERY_NOOUTPUT)==0 ){
208694 fts5IterSetOutputCb(&p->rc, pNew);
208695 }
208696
208697 /* Initialize each of the component segment iterators. */
208698 if( p->rc==SQLITE_OK ){
208699 if( iLevel<0 ){
208700 Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
208701 if( p->pHash ){
208702 /* Add a segment iterator for the current contents of the hash table. */
208703 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
208704 fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
208705 }
208706 for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
208707 for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
208708 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
208709 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
208710 if( pTerm==0 ){
208711 fts5SegIterInit(p, pSeg, pIter);
208712 }else{
208713 fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
208714 }
208715 }
208716 }
208717 }else{
208718 pLvl = &pStruct->aLevel[iLevel];
208719 for(iSeg=nSeg-1; iSeg>=0; iSeg--){
208720 fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
208721 }
208722 }
208723 assert( iIter==nSeg );
208724 }
208725
208726 /* If the above was successful, each component iterators now points
208727 ** to the first entry in its segment. In this case initialize the
208728 ** aFirst[] array. Or, if an error has occurred, free the iterator
208729 ** object and set the output variable to NULL. */
208730 if( p->rc==SQLITE_OK ){
208731 for(iIter=pNew->nSeg-1; iIter>0; iIter--){
208732 int iEq;
208733 if( (iEq = fts5MultiIterDoCompare(pNew, iIter)) ){
208734 Fts5SegIter *pSeg = &pNew->aSeg[iEq];
208735 if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0);
208736 fts5MultiIterAdvanced(p, pNew, iEq, iIter);
208737 }
208738 }
208739 fts5MultiIterSetEof(pNew);
208740 fts5AssertMultiIterSetup(p, pNew);
208741
208742 if( pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pNew) ){
208743 fts5MultiIterNext(p, pNew, 0, 0);
208744 }else if( pNew->base.bEof==0 ){
208745 Fts5SegIter *pSeg = &pNew->aSeg[pNew->aFirst[1].iFirst];
208746 pNew->xSetOutputs(pNew, pSeg);
208747 }
208748
208749 }else{
208750 fts5MultiIterFree(pNew);
208751 *ppOut = 0;
208752 }
208753}
208754
208755/*
208756** Create an Fts5Iter that iterates through the doclist provided
208757** as the second argument.
208758*/
208759static void fts5MultiIterNew2(
208760 Fts5Index *p, /* FTS5 backend to iterate within */
208761 Fts5Data *pData, /* Doclist to iterate through */
208762 int bDesc, /* True for descending rowid order */
208763 Fts5Iter **ppOut /* New object */
208764){
208765 Fts5Iter *pNew;
208766 pNew = fts5MultiIterAlloc(p, 2);
208767 if( pNew ){
208768 Fts5SegIter *pIter = &pNew->aSeg[1];
208769
208770 pIter->flags = FTS5_SEGITER_ONETERM;
208771 if( pData->szLeaf>0 ){
208772 pIter->pLeaf = pData;
208773 pIter->iLeafOffset = fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
208774 pIter->iEndofDoclist = pData->nn;
208775 pNew->aFirst[1].iFirst = 1;
208776 if( bDesc ){
208777 pNew->bRev = 1;
208778 pIter->flags |= FTS5_SEGITER_REVERSE;
208779 fts5SegIterReverseInitPage(p, pIter);
208780 }else{
208781 fts5SegIterLoadNPos(p, pIter);
208782 }
208783 pData = 0;
208784 }else{
208785 pNew->base.bEof = 1;
208786 }
208787 fts5SegIterSetNext(p, pIter);
208788
208789 *ppOut = pNew;
208790 }
208791
208792 fts5DataRelease(pData);
208793}
208794
208795/*
208796** Return true if the iterator is at EOF or if an error has occurred.
208797** False otherwise.
208798*/
208799static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
208800 assert( p->rc
208801 || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof
208802 );
208803 return (p->rc || pIter->base.bEof);
208804}
208805
208806/*
208807** Return the rowid of the entry that the iterator currently points
208808** to. If the iterator points to EOF when this function is called the
208809** results are undefined.
208810*/
208811static i64 fts5MultiIterRowid(Fts5Iter *pIter){
208812 assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf );
208813 return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
208814}
208815
208816/*
208817** Move the iterator to the next entry at or following iMatch.
208818*/
208819static void fts5MultiIterNextFrom(
208820 Fts5Index *p,
208821 Fts5Iter *pIter,
208822 i64 iMatch
208823){
208824 while( 1 ){
208825 i64 iRowid;
208826 fts5MultiIterNext(p, pIter, 1, iMatch);
208827 if( fts5MultiIterEof(p, pIter) ) break;
208828 iRowid = fts5MultiIterRowid(pIter);
208829 if( pIter->bRev==0 && iRowid>=iMatch ) break;
208830 if( pIter->bRev!=0 && iRowid<=iMatch ) break;
208831 }
208832}
208833
208834/*
208835** Return a pointer to a buffer containing the term associated with the
208836** entry that the iterator currently points to.
208837*/
208838static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
208839 Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
208840 *pn = p->term.n;
208841 return p->term.p;
208842}
208843
208844/*
208845** Allocate a new segment-id for the structure pStruct. The new segment
208846** id must be between 1 and 65335 inclusive, and must not be used by
208847** any currently existing segment. If a free segment id cannot be found,
208848** SQLITE_FULL is returned.
208849**
208850** If an error has already occurred, this function is a no-op. 0 is
208851** returned in this case.
208852*/
208853static int fts5AllocateSegid(Fts5Index *p, Fts5Structure *pStruct){
208854 int iSegid = 0;
208855
208856 if( p->rc==SQLITE_OK ){
208857 if( pStruct->nSegment>=FTS5_MAX_SEGMENT ){
208858 p->rc = SQLITE_FULL;
208859 }else{
208860 /* FTS5_MAX_SEGMENT is currently defined as 2000. So the following
208861 ** array is 63 elements, or 252 bytes, in size. */
208862 u32 aUsed[(FTS5_MAX_SEGMENT+31) / 32];
208863 int iLvl, iSeg;
208864 int i;
208865 u32 mask;
208866 memset(aUsed, 0, sizeof(aUsed));
208867 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
208868 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
208869 int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid;
208870 if( iId<=FTS5_MAX_SEGMENT ){
208871 aUsed[(iId-1) / 32] |= 1 << ((iId-1) % 32);
208872 }
208873 }
208874 }
208875
208876 for(i=0; aUsed[i]==0xFFFFFFFF; i++);
208877 mask = aUsed[i];
208878 for(iSegid=0; mask & (1 << iSegid); iSegid++);
208879 iSegid += 1 + i*32;
208880
208881#ifdef SQLITE_DEBUG
208882 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
208883 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
208884 assert( iSegid!=pStruct->aLevel[iLvl].aSeg[iSeg].iSegid );
208885 }
208886 }
208887 assert( iSegid>0 && iSegid<=FTS5_MAX_SEGMENT );
208888
208889 {
208890 sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
208891 if( p->rc==SQLITE_OK ){
208892 u8 aBlob[2] = {0xff, 0xff};
208893 sqlite3_bind_int(pIdxSelect, 1, iSegid);
208894 sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
208895 assert( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
208896 p->rc = sqlite3_reset(pIdxSelect);
208897 sqlite3_bind_null(pIdxSelect, 2);
208898 }
208899 }
208900#endif
208901 }
208902 }
208903
208904 return iSegid;
208905}
208906
208907/*
208908** Discard all data currently cached in the hash-tables.
208909*/
208910static void fts5IndexDiscardData(Fts5Index *p){
208911 assert( p->pHash || p->nPendingData==0 );
208912 if( p->pHash ){
208913 sqlite3Fts5HashClear(p->pHash);
208914 p->nPendingData = 0;
208915 }
208916}
208917
208918/*
208919** Return the size of the prefix, in bytes, that buffer
208920** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
208921**
208922** Buffer (pNew/<length-unknown>) is guaranteed to be greater
208923** than buffer (pOld/nOld).
208924*/
208925static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
208926 int i;
208927 for(i=0; i<nOld; i++){
208928 if( pOld[i]!=pNew[i] ) break;
208929 }
208930 return i;
208931}
208932
208933static void fts5WriteDlidxClear(
208934 Fts5Index *p,
208935 Fts5SegWriter *pWriter,
208936 int bFlush /* If true, write dlidx to disk */
208937){
208938 int i;
208939 assert( bFlush==0 || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n>0) );
208940 for(i=0; i<pWriter->nDlidx; i++){
208941 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
208942 if( pDlidx->buf.n==0 ) break;
208943 if( bFlush ){
208944 assert( pDlidx->pgno!=0 );
208945 fts5DataWrite(p,
208946 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
208947 pDlidx->buf.p, pDlidx->buf.n
208948 );
208949 }
208950 sqlite3Fts5BufferZero(&pDlidx->buf);
208951 pDlidx->bPrevValid = 0;
208952 }
208953}
208954
208955/*
208956** Grow the pWriter->aDlidx[] array to at least nLvl elements in size.
208957** Any new array elements are zeroed before returning.
208958*/
208959static int fts5WriteDlidxGrow(
208960 Fts5Index *p,
208961 Fts5SegWriter *pWriter,
208962 int nLvl
208963){
208964 if( p->rc==SQLITE_OK && nLvl>=pWriter->nDlidx ){
208965 Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc(
208966 pWriter->aDlidx, sizeof(Fts5DlidxWriter) * nLvl
208967 );
208968 if( aDlidx==0 ){
208969 p->rc = SQLITE_NOMEM;
208970 }else{
208971 int nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx);
208972 memset(&aDlidx[pWriter->nDlidx], 0, nByte);
208973 pWriter->aDlidx = aDlidx;
208974 pWriter->nDlidx = nLvl;
208975 }
208976 }
208977 return p->rc;
208978}
208979
208980/*
208981** If the current doclist-index accumulating in pWriter->aDlidx[] is large
208982** enough, flush it to disk and return 1. Otherwise discard it and return
208983** zero.
208984*/
208985static int fts5WriteFlushDlidx(Fts5Index *p, Fts5SegWriter *pWriter){
208986 int bFlag = 0;
208987
208988 /* If there were FTS5_MIN_DLIDX_SIZE or more empty leaf pages written
208989 ** to the database, also write the doclist-index to disk. */
208990 if( pWriter->aDlidx[0].buf.n>0 && pWriter->nEmpty>=FTS5_MIN_DLIDX_SIZE ){
208991 bFlag = 1;
208992 }
208993 fts5WriteDlidxClear(p, pWriter, bFlag);
208994 pWriter->nEmpty = 0;
208995 return bFlag;
208996}
208997
208998/*
208999** This function is called whenever processing of the doclist for the
209000** last term on leaf page (pWriter->iBtPage) is completed.
209001**
209002** The doclist-index for that term is currently stored in-memory within the
209003** Fts5SegWriter.aDlidx[] array. If it is large enough, this function
209004** writes it out to disk. Or, if it is too small to bother with, discards
209005** it.
209006**
209007** Fts5SegWriter.btterm currently contains the first term on page iBtPage.
209008*/
209009static void fts5WriteFlushBtree(Fts5Index *p, Fts5SegWriter *pWriter){
209010 int bFlag;
209011
209012 assert( pWriter->iBtPage || pWriter->nEmpty==0 );
209013 if( pWriter->iBtPage==0 ) return;
209014 bFlag = fts5WriteFlushDlidx(p, pWriter);
209015
209016 if( p->rc==SQLITE_OK ){
209017 const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
209018 /* The following was already done in fts5WriteInit(): */
209019 /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
209020 sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC);
209021 sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
209022 sqlite3_step(p->pIdxWriter);
209023 p->rc = sqlite3_reset(p->pIdxWriter);
209024 sqlite3_bind_null(p->pIdxWriter, 2);
209025 }
209026 pWriter->iBtPage = 0;
209027}
209028
209029/*
209030** This is called once for each leaf page except the first that contains
209031** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
209032** is larger than all terms written to earlier leaves, and equal to or
209033** smaller than the first term on the new leaf.
209034**
209035** If an error occurs, an error code is left in Fts5Index.rc. If an error
209036** has already occurred when this function is called, it is a no-op.
209037*/
209038static void fts5WriteBtreeTerm(
209039 Fts5Index *p, /* FTS5 backend object */
209040 Fts5SegWriter *pWriter, /* Writer object */
209041 int nTerm, const u8 *pTerm /* First term on new page */
209042){
209043 fts5WriteFlushBtree(p, pWriter);
209044 fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm);
209045 pWriter->iBtPage = pWriter->writer.pgno;
209046}
209047
209048/*
209049** This function is called when flushing a leaf page that contains no
209050** terms at all to disk.
209051*/
209052static void fts5WriteBtreeNoTerm(
209053 Fts5Index *p, /* FTS5 backend object */
209054 Fts5SegWriter *pWriter /* Writer object */
209055){
209056 /* If there were no rowids on the leaf page either and the doclist-index
209057 ** has already been started, append an 0x00 byte to it. */
209058 if( pWriter->bFirstRowidInPage && pWriter->aDlidx[0].buf.n>0 ){
209059 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[0];
209060 assert( pDlidx->bPrevValid );
209061 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, 0);
209062 }
209063
209064 /* Increment the "number of sequential leaves without a term" counter. */
209065 pWriter->nEmpty++;
209066}
209067
209068static i64 fts5DlidxExtractFirstRowid(Fts5Buffer *pBuf){
209069 i64 iRowid;
209070 int iOff;
209071
209072 iOff = 1 + fts5GetVarint(&pBuf->p[1], (u64*)&iRowid);
209073 fts5GetVarint(&pBuf->p[iOff], (u64*)&iRowid);
209074 return iRowid;
209075}
209076
209077/*
209078** Rowid iRowid has just been appended to the current leaf page. It is the
209079** first on the page. This function appends an appropriate entry to the current
209080** doclist-index.
209081*/
209082static void fts5WriteDlidxAppend(
209083 Fts5Index *p,
209084 Fts5SegWriter *pWriter,
209085 i64 iRowid
209086){
209087 int i;
209088 int bDone = 0;
209089
209090 for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
209091 i64 iVal;
209092 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
209093
209094 if( pDlidx->buf.n>=p->pConfig->pgsz ){
209095 /* The current doclist-index page is full. Write it to disk and push
209096 ** a copy of iRowid (which will become the first rowid on the next
209097 ** doclist-index leaf page) up into the next level of the b-tree
209098 ** hierarchy. If the node being flushed is currently the root node,
209099 ** also push its first rowid upwards. */
209100 pDlidx->buf.p[0] = 0x01; /* Not the root node */
209101 fts5DataWrite(p,
209102 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
209103 pDlidx->buf.p, pDlidx->buf.n
209104 );
209105 fts5WriteDlidxGrow(p, pWriter, i+2);
209106 pDlidx = &pWriter->aDlidx[i];
209107 if( p->rc==SQLITE_OK && pDlidx[1].buf.n==0 ){
209108 i64 iFirst = fts5DlidxExtractFirstRowid(&pDlidx->buf);
209109
209110 /* This was the root node. Push its first rowid up to the new root. */
209111 pDlidx[1].pgno = pDlidx->pgno;
209112 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, 0);
209113 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, pDlidx->pgno);
209114 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, iFirst);
209115 pDlidx[1].bPrevValid = 1;
209116 pDlidx[1].iPrev = iFirst;
209117 }
209118
209119 sqlite3Fts5BufferZero(&pDlidx->buf);
209120 pDlidx->bPrevValid = 0;
209121 pDlidx->pgno++;
209122 }else{
209123 bDone = 1;
209124 }
209125
209126 if( pDlidx->bPrevValid ){
209127 iVal = iRowid - pDlidx->iPrev;
209128 }else{
209129 i64 iPgno = (i==0 ? pWriter->writer.pgno : pDlidx[-1].pgno);
209130 assert( pDlidx->buf.n==0 );
209131 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, !bDone);
209132 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iPgno);
209133 iVal = iRowid;
209134 }
209135
209136 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iVal);
209137 pDlidx->bPrevValid = 1;
209138 pDlidx->iPrev = iRowid;
209139 }
209140}
209141
209142static void fts5WriteFlushLeaf(Fts5Index *p, Fts5SegWriter *pWriter){
209143 static const u8 zero[] = { 0x00, 0x00, 0x00, 0x00 };
209144 Fts5PageWriter *pPage = &pWriter->writer;
209145 i64 iRowid;
209146
209147 assert( (pPage->pgidx.n==0)==(pWriter->bFirstTermInPage) );
209148
209149 /* Set the szLeaf header field. */
209150 assert( 0==fts5GetU16(&pPage->buf.p[2]) );
209151 fts5PutU16(&pPage->buf.p[2], (u16)pPage->buf.n);
209152
209153 if( pWriter->bFirstTermInPage ){
209154 /* No term was written to this page. */
209155 assert( pPage->pgidx.n==0 );
209156 fts5WriteBtreeNoTerm(p, pWriter);
209157 }else{
209158 /* Append the pgidx to the page buffer. Set the szLeaf header field. */
209159 fts5BufferAppendBlob(&p->rc, &pPage->buf, pPage->pgidx.n, pPage->pgidx.p);
209160 }
209161
209162 /* Write the page out to disk */
209163 iRowid = FTS5_SEGMENT_ROWID(pWriter->iSegid, pPage->pgno);
209164 fts5DataWrite(p, iRowid, pPage->buf.p, pPage->buf.n);
209165
209166 /* Initialize the next page. */
209167 fts5BufferZero(&pPage->buf);
209168 fts5BufferZero(&pPage->pgidx);
209169 fts5BufferAppendBlob(&p->rc, &pPage->buf, 4, zero);
209170 pPage->iPrevPgidx = 0;
209171 pPage->pgno++;
209172
209173 /* Increase the leaves written counter */
209174 pWriter->nLeafWritten++;
209175
209176 /* The new leaf holds no terms or rowids */
209177 pWriter->bFirstTermInPage = 1;
209178 pWriter->bFirstRowidInPage = 1;
209179}
209180
209181/*
209182** Append term pTerm/nTerm to the segment being written by the writer passed
209183** as the second argument.
209184**
209185** If an error occurs, set the Fts5Index.rc error code. If an error has
209186** already occurred, this function is a no-op.
209187*/
209188static void fts5WriteAppendTerm(
209189 Fts5Index *p,
209190 Fts5SegWriter *pWriter,
209191 int nTerm, const u8 *pTerm
209192){
209193 int nPrefix; /* Bytes of prefix compression for term */
209194 Fts5PageWriter *pPage = &pWriter->writer;
209195 Fts5Buffer *pPgidx = &pWriter->writer.pgidx;
209196
209197 assert( p->rc==SQLITE_OK );
209198 assert( pPage->buf.n>=4 );
209199 assert( pPage->buf.n>4 || pWriter->bFirstTermInPage );
209200
209201 /* If the current leaf page is full, flush it to disk. */
209202 if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){
209203 if( pPage->buf.n>4 ){
209204 fts5WriteFlushLeaf(p, pWriter);
209205 }
209206 fts5BufferGrow(&p->rc, &pPage->buf, nTerm+FTS5_DATA_PADDING);
209207 }
209208
209209 /* TODO1: Updating pgidx here. */
209210 pPgidx->n += sqlite3Fts5PutVarint(
209211 &pPgidx->p[pPgidx->n], pPage->buf.n - pPage->iPrevPgidx
209212 );
209213 pPage->iPrevPgidx = pPage->buf.n;
209214#if 0
209215 fts5PutU16(&pPgidx->p[pPgidx->n], pPage->buf.n);
209216 pPgidx->n += 2;
209217#endif
209218
209219 if( pWriter->bFirstTermInPage ){
209220 nPrefix = 0;
209221 if( pPage->pgno!=1 ){
209222 /* This is the first term on a leaf that is not the leftmost leaf in
209223 ** the segment b-tree. In this case it is necessary to add a term to
209224 ** the b-tree hierarchy that is (a) larger than the largest term
209225 ** already written to the segment and (b) smaller than or equal to
209226 ** this term. In other words, a prefix of (pTerm/nTerm) that is one
209227 ** byte longer than the longest prefix (pTerm/nTerm) shares with the
209228 ** previous term.
209229 **
209230 ** Usually, the previous term is available in pPage->term. The exception
209231 ** is if this is the first term written in an incremental-merge step.
209232 ** In this case the previous term is not available, so just write a
209233 ** copy of (pTerm/nTerm) into the parent node. This is slightly
209234 ** inefficient, but still correct. */
209235 int n = nTerm;
209236 if( pPage->term.n ){
209237 n = 1 + fts5PrefixCompress(pPage->term.n, pPage->term.p, pTerm);
209238 }
209239 fts5WriteBtreeTerm(p, pWriter, n, pTerm);
209240 pPage = &pWriter->writer;
209241 }
209242 }else{
209243 nPrefix = fts5PrefixCompress(pPage->term.n, pPage->term.p, pTerm);
209244 fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix);
209245 }
209246
209247 /* Append the number of bytes of new data, then the term data itself
209248 ** to the page. */
209249 fts5BufferAppendVarint(&p->rc, &pPage->buf, nTerm - nPrefix);
209250 fts5BufferAppendBlob(&p->rc, &pPage->buf, nTerm - nPrefix, &pTerm[nPrefix]);
209251
209252 /* Update the Fts5PageWriter.term field. */
209253 fts5BufferSet(&p->rc, &pPage->term, nTerm, pTerm);
209254 pWriter->bFirstTermInPage = 0;
209255
209256 pWriter->bFirstRowidInPage = 0;
209257 pWriter->bFirstRowidInDoclist = 1;
209258
209259 assert( p->rc || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n==0) );
209260 pWriter->aDlidx[0].pgno = pPage->pgno;
209261}
209262
209263/*
209264** Append a rowid and position-list size field to the writers output.
209265*/
209266static void fts5WriteAppendRowid(
209267 Fts5Index *p,
209268 Fts5SegWriter *pWriter,
209269 i64 iRowid
209270){
209271 if( p->rc==SQLITE_OK ){
209272 Fts5PageWriter *pPage = &pWriter->writer;
209273
209274 if( (pPage->buf.n + pPage->pgidx.n)>=p->pConfig->pgsz ){
209275 fts5WriteFlushLeaf(p, pWriter);
209276 }
209277
209278 /* If this is to be the first rowid written to the page, set the
209279 ** rowid-pointer in the page-header. Also append a value to the dlidx
209280 ** buffer, in case a doclist-index is required. */
209281 if( pWriter->bFirstRowidInPage ){
209282 fts5PutU16(pPage->buf.p, (u16)pPage->buf.n);
209283 fts5WriteDlidxAppend(p, pWriter, iRowid);
209284 }
209285
209286 /* Write the rowid. */
209287 if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
209288 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid);
209289 }else{
209290 assert( p->rc || iRowid>pWriter->iPrevRowid );
209291 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid - pWriter->iPrevRowid);
209292 }
209293 pWriter->iPrevRowid = iRowid;
209294 pWriter->bFirstRowidInDoclist = 0;
209295 pWriter->bFirstRowidInPage = 0;
209296 }
209297}
209298
209299static void fts5WriteAppendPoslistData(
209300 Fts5Index *p,
209301 Fts5SegWriter *pWriter,
209302 const u8 *aData,
209303 int nData
209304){
209305 Fts5PageWriter *pPage = &pWriter->writer;
209306 const u8 *a = aData;
209307 int n = nData;
209308
209309 assert( p->pConfig->pgsz>0 );
209310 while( p->rc==SQLITE_OK
209311 && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
209312 ){
209313 int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
209314 int nCopy = 0;
209315 while( nCopy<nReq ){
209316 i64 dummy;
209317 nCopy += fts5GetVarint(&a[nCopy], (u64*)&dummy);
209318 }
209319 fts5BufferAppendBlob(&p->rc, &pPage->buf, nCopy, a);
209320 a += nCopy;
209321 n -= nCopy;
209322 fts5WriteFlushLeaf(p, pWriter);
209323 }
209324 if( n>0 ){
209325 fts5BufferAppendBlob(&p->rc, &pPage->buf, n, a);
209326 }
209327}
209328
209329/*
209330** Flush any data cached by the writer object to the database. Free any
209331** allocations associated with the writer.
209332*/
209333static void fts5WriteFinish(
209334 Fts5Index *p,
209335 Fts5SegWriter *pWriter, /* Writer object */
209336 int *pnLeaf /* OUT: Number of leaf pages in b-tree */
209337){
209338 int i;
209339 Fts5PageWriter *pLeaf = &pWriter->writer;
209340 if( p->rc==SQLITE_OK ){
209341 assert( pLeaf->pgno>=1 );
209342 if( pLeaf->buf.n>4 ){
209343 fts5WriteFlushLeaf(p, pWriter);
209344 }
209345 *pnLeaf = pLeaf->pgno-1;
209346 if( pLeaf->pgno>1 ){
209347 fts5WriteFlushBtree(p, pWriter);
209348 }
209349 }
209350 fts5BufferFree(&pLeaf->term);
209351 fts5BufferFree(&pLeaf->buf);
209352 fts5BufferFree(&pLeaf->pgidx);
209353 fts5BufferFree(&pWriter->btterm);
209354
209355 for(i=0; i<pWriter->nDlidx; i++){
209356 sqlite3Fts5BufferFree(&pWriter->aDlidx[i].buf);
209357 }
209358 sqlite3_free(pWriter->aDlidx);
209359}
209360
209361static void fts5WriteInit(
209362 Fts5Index *p,
209363 Fts5SegWriter *pWriter,
209364 int iSegid
209365){
209366 const int nBuffer = p->pConfig->pgsz + FTS5_DATA_PADDING;
209367
209368 memset(pWriter, 0, sizeof(Fts5SegWriter));
209369 pWriter->iSegid = iSegid;
209370
209371 fts5WriteDlidxGrow(p, pWriter, 1);
209372 pWriter->writer.pgno = 1;
209373 pWriter->bFirstTermInPage = 1;
209374 pWriter->iBtPage = 1;
209375
209376 assert( pWriter->writer.buf.n==0 );
209377 assert( pWriter->writer.pgidx.n==0 );
209378
209379 /* Grow the two buffers to pgsz + padding bytes in size. */
209380 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.pgidx, nBuffer);
209381 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.buf, nBuffer);
209382
209383 if( p->pIdxWriter==0 ){
209384 Fts5Config *pConfig = p->pConfig;
209385 fts5IndexPrepareStmt(p, &p->pIdxWriter, sqlite3_mprintf(
209386 "INSERT INTO '%q'.'%q_idx'(segid,term,pgno) VALUES(?,?,?)",
209387 pConfig->zDb, pConfig->zName
209388 ));
209389 }
209390
209391 if( p->rc==SQLITE_OK ){
209392 /* Initialize the 4-byte leaf-page header to 0x00. */
209393 memset(pWriter->writer.buf.p, 0, 4);
209394 pWriter->writer.buf.n = 4;
209395
209396 /* Bind the current output segment id to the index-writer. This is an
209397 ** optimization over binding the same value over and over as rows are
209398 ** inserted into %_idx by the current writer. */
209399 sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid);
209400 }
209401}
209402
209403/*
209404** Iterator pIter was used to iterate through the input segments of on an
209405** incremental merge operation. This function is called if the incremental
209406** merge step has finished but the input has not been completely exhausted.
209407*/
209408static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
209409 int i;
209410 Fts5Buffer buf;
209411 memset(&buf, 0, sizeof(Fts5Buffer));
209412 for(i=0; i<pIter->nSeg; i++){
209413 Fts5SegIter *pSeg = &pIter->aSeg[i];
209414 if( pSeg->pSeg==0 ){
209415 /* no-op */
209416 }else if( pSeg->pLeaf==0 ){
209417 /* All keys from this input segment have been transfered to the output.
209418 ** Set both the first and last page-numbers to 0 to indicate that the
209419 ** segment is now empty. */
209420 pSeg->pSeg->pgnoLast = 0;
209421 pSeg->pSeg->pgnoFirst = 0;
209422 }else{
209423 int iOff = pSeg->iTermLeafOffset; /* Offset on new first leaf page */
209424 i64 iLeafRowid;
209425 Fts5Data *pData;
209426 int iId = pSeg->pSeg->iSegid;
209427 u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00};
209428
209429 iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno);
209430 pData = fts5DataRead(p, iLeafRowid);
209431 if( pData ){
209432 fts5BufferZero(&buf);
209433 fts5BufferGrow(&p->rc, &buf, pData->nn);
209434 fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr);
209435 fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n);
209436 fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p);
209437 fts5BufferAppendBlob(&p->rc, &buf, pData->szLeaf-iOff, &pData->p[iOff]);
209438 if( p->rc==SQLITE_OK ){
209439 /* Set the szLeaf field */
209440 fts5PutU16(&buf.p[2], (u16)buf.n);
209441 }
209442
209443 /* Set up the new page-index array */
209444 fts5BufferAppendVarint(&p->rc, &buf, 4);
209445 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno
209446 && pSeg->iEndofDoclist<pData->szLeaf
209447 ){
209448 int nDiff = pData->szLeaf - pSeg->iEndofDoclist;
209449 fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4);
209450 fts5BufferAppendBlob(&p->rc, &buf,
209451 pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff]
209452 );
209453 }
209454
209455 fts5DataRelease(pData);
209456 pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno;
209457 fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1), iLeafRowid);
209458 fts5DataWrite(p, iLeafRowid, buf.p, buf.n);
209459 }
209460 }
209461 }
209462 fts5BufferFree(&buf);
209463}
209464
209465static void fts5MergeChunkCallback(
209466 Fts5Index *p,
209467 void *pCtx,
209468 const u8 *pChunk, int nChunk
209469){
209470 Fts5SegWriter *pWriter = (Fts5SegWriter*)pCtx;
209471 fts5WriteAppendPoslistData(p, pWriter, pChunk, nChunk);
209472}
209473
209474/*
209475**
209476*/
209477static void fts5IndexMergeLevel(
209478 Fts5Index *p, /* FTS5 backend object */
209479 Fts5Structure **ppStruct, /* IN/OUT: Stucture of index */
209480 int iLvl, /* Level to read input from */
209481 int *pnRem /* Write up to this many output leaves */
209482){
209483 Fts5Structure *pStruct = *ppStruct;
209484 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
209485 Fts5StructureLevel *pLvlOut;
209486 Fts5Iter *pIter = 0; /* Iterator to read input data */
209487 int nRem = pnRem ? *pnRem : 0; /* Output leaf pages left to write */
209488 int nInput; /* Number of input segments */
209489 Fts5SegWriter writer; /* Writer object */
209490 Fts5StructureSegment *pSeg; /* Output segment */
209491 Fts5Buffer term;
209492 int bOldest; /* True if the output segment is the oldest */
209493 int eDetail = p->pConfig->eDetail;
209494 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
209495 int bTermWritten = 0; /* True if current term already output */
209496
209497 assert( iLvl<pStruct->nLevel );
209498 assert( pLvl->nMerge<=pLvl->nSeg );
209499
209500 memset(&writer, 0, sizeof(Fts5SegWriter));
209501 memset(&term, 0, sizeof(Fts5Buffer));
209502 if( pLvl->nMerge ){
209503 pLvlOut = &pStruct->aLevel[iLvl+1];
209504 assert( pLvlOut->nSeg>0 );
209505 nInput = pLvl->nMerge;
209506 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg-1];
209507
209508 fts5WriteInit(p, &writer, pSeg->iSegid);
209509 writer.writer.pgno = pSeg->pgnoLast+1;
209510 writer.iBtPage = 0;
209511 }else{
209512 int iSegid = fts5AllocateSegid(p, pStruct);
209513
209514 /* Extend the Fts5Structure object as required to ensure the output
209515 ** segment exists. */
209516 if( iLvl==pStruct->nLevel-1 ){
209517 fts5StructureAddLevel(&p->rc, ppStruct);
209518 pStruct = *ppStruct;
209519 }
209520 fts5StructureExtendLevel(&p->rc, pStruct, iLvl+1, 1, 0);
209521 if( p->rc ) return;
209522 pLvl = &pStruct->aLevel[iLvl];
209523 pLvlOut = &pStruct->aLevel[iLvl+1];
209524
209525 fts5WriteInit(p, &writer, iSegid);
209526
209527 /* Add the new segment to the output level */
209528 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg];
209529 pLvlOut->nSeg++;
209530 pSeg->pgnoFirst = 1;
209531 pSeg->iSegid = iSegid;
209532 pStruct->nSegment++;
209533
209534 /* Read input from all segments in the input level */
209535 nInput = pLvl->nSeg;
209536 }
209537 bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
209538
209539 assert( iLvl>=0 );
209540 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, iLvl, nInput, &pIter);
209541 fts5MultiIterEof(p, pIter)==0;
209542 fts5MultiIterNext(p, pIter, 0, 0)
209543 ){
209544 Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
209545 int nPos; /* position-list size field value */
209546 int nTerm;
209547 const u8 *pTerm;
209548
209549 pTerm = fts5MultiIterTerm(pIter, &nTerm);
209550 if( nTerm!=term.n || memcmp(pTerm, term.p, nTerm) ){
209551 if( pnRem && writer.nLeafWritten>nRem ){
209552 break;
209553 }
209554 fts5BufferSet(&p->rc, &term, nTerm, pTerm);
209555 bTermWritten =0;
209556 }
209557
209558 /* Check for key annihilation. */
209559 if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue;
209560
209561 if( p->rc==SQLITE_OK && bTermWritten==0 ){
209562 /* This is a new term. Append a term to the output segment. */
209563 fts5WriteAppendTerm(p, &writer, nTerm, pTerm);
209564 bTermWritten = 1;
209565 }
209566
209567 /* Append the rowid to the output */
209568 /* WRITEPOSLISTSIZE */
209569 fts5WriteAppendRowid(p, &writer, fts5MultiIterRowid(pIter));
209570
209571 if( eDetail==FTS5_DETAIL_NONE ){
209572 if( pSegIter->bDel ){
209573 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
209574 if( pSegIter->nPos>0 ){
209575 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
209576 }
209577 }
209578 }else{
209579 /* Append the position-list data to the output */
209580 nPos = pSegIter->nPos*2 + pSegIter->bDel;
209581 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, nPos);
209582 fts5ChunkIterate(p, pSegIter, (void*)&writer, fts5MergeChunkCallback);
209583 }
209584 }
209585
209586 /* Flush the last leaf page to disk. Set the output segment b-tree height
209587 ** and last leaf page number at the same time. */
209588 fts5WriteFinish(p, &writer, &pSeg->pgnoLast);
209589
209590 if( fts5MultiIterEof(p, pIter) ){
209591 int i;
209592
209593 /* Remove the redundant segments from the %_data table */
209594 for(i=0; i<nInput; i++){
209595 fts5DataRemoveSegment(p, pLvl->aSeg[i].iSegid);
209596 }
209597
209598 /* Remove the redundant segments from the input level */
209599 if( pLvl->nSeg!=nInput ){
209600 int nMove = (pLvl->nSeg - nInput) * sizeof(Fts5StructureSegment);
209601 memmove(pLvl->aSeg, &pLvl->aSeg[nInput], nMove);
209602 }
209603 pStruct->nSegment -= nInput;
209604 pLvl->nSeg -= nInput;
209605 pLvl->nMerge = 0;
209606 if( pSeg->pgnoLast==0 ){
209607 pLvlOut->nSeg--;
209608 pStruct->nSegment--;
209609 }
209610 }else{
209611 assert( pSeg->pgnoLast>0 );
209612 fts5TrimSegments(p, pIter);
209613 pLvl->nMerge = nInput;
209614 }
209615
209616 fts5MultiIterFree(pIter);
209617 fts5BufferFree(&term);
209618 if( pnRem ) *pnRem -= writer.nLeafWritten;
209619}
209620
209621/*
209622** Do up to nPg pages of automerge work on the index.
209623**
209624** Return true if any changes were actually made, or false otherwise.
209625*/
209626static int fts5IndexMerge(
209627 Fts5Index *p, /* FTS5 backend object */
209628 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
209629 int nPg, /* Pages of work to do */
209630 int nMin /* Minimum number of segments to merge */
209631){
209632 int nRem = nPg;
209633 int bRet = 0;
209634 Fts5Structure *pStruct = *ppStruct;
209635 while( nRem>0 && p->rc==SQLITE_OK ){
209636 int iLvl; /* To iterate through levels */
209637 int iBestLvl = 0; /* Level offering the most input segments */
209638 int nBest = 0; /* Number of input segments on best level */
209639
209640 /* Set iBestLvl to the level to read input segments from. */
209641 assert( pStruct->nLevel>0 );
209642 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
209643 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
209644 if( pLvl->nMerge ){
209645 if( pLvl->nMerge>nBest ){
209646 iBestLvl = iLvl;
209647 nBest = pLvl->nMerge;
209648 }
209649 break;
209650 }
209651 if( pLvl->nSeg>nBest ){
209652 nBest = pLvl->nSeg;
209653 iBestLvl = iLvl;
209654 }
209655 }
209656
209657 /* If nBest is still 0, then the index must be empty. */
209658#ifdef SQLITE_DEBUG
209659 for(iLvl=0; nBest==0 && iLvl<pStruct->nLevel; iLvl++){
209660 assert( pStruct->aLevel[iLvl].nSeg==0 );
209661 }
209662#endif
209663
209664 if( nBest<nMin && pStruct->aLevel[iBestLvl].nMerge==0 ){
209665 break;
209666 }
209667 bRet = 1;
209668 fts5IndexMergeLevel(p, &pStruct, iBestLvl, &nRem);
209669 if( p->rc==SQLITE_OK && pStruct->aLevel[iBestLvl].nMerge==0 ){
209670 fts5StructurePromote(p, iBestLvl+1, pStruct);
209671 }
209672 }
209673 *ppStruct = pStruct;
209674 return bRet;
209675}
209676
209677/*
209678** A total of nLeaf leaf pages of data has just been flushed to a level-0
209679** segment. This function updates the write-counter accordingly and, if
209680** necessary, performs incremental merge work.
209681**
209682** If an error occurs, set the Fts5Index.rc error code. If an error has
209683** already occurred, this function is a no-op.
209684*/
209685static void fts5IndexAutomerge(
209686 Fts5Index *p, /* FTS5 backend object */
209687 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
209688 int nLeaf /* Number of output leaves just written */
209689){
209690 if( p->rc==SQLITE_OK && p->pConfig->nAutomerge>0 ){
209691 Fts5Structure *pStruct = *ppStruct;
209692 u64 nWrite; /* Initial value of write-counter */
209693 int nWork; /* Number of work-quanta to perform */
209694 int nRem; /* Number of leaf pages left to write */
209695
209696 /* Update the write-counter. While doing so, set nWork. */
209697 nWrite = pStruct->nWriteCounter;
209698 nWork = (int)(((nWrite + nLeaf) / p->nWorkUnit) - (nWrite / p->nWorkUnit));
209699 pStruct->nWriteCounter += nLeaf;
209700 nRem = (int)(p->nWorkUnit * nWork * pStruct->nLevel);
209701
209702 fts5IndexMerge(p, ppStruct, nRem, p->pConfig->nAutomerge);
209703 }
209704}
209705
209706static void fts5IndexCrisismerge(
209707 Fts5Index *p, /* FTS5 backend object */
209708 Fts5Structure **ppStruct /* IN/OUT: Current structure of index */
209709){
209710 const int nCrisis = p->pConfig->nCrisisMerge;
209711 Fts5Structure *pStruct = *ppStruct;
209712 int iLvl = 0;
209713
209714 assert( p->rc!=SQLITE_OK || pStruct->nLevel>0 );
209715 while( p->rc==SQLITE_OK && pStruct->aLevel[iLvl].nSeg>=nCrisis ){
209716 fts5IndexMergeLevel(p, &pStruct, iLvl, 0);
209717 assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) );
209718 fts5StructurePromote(p, iLvl+1, pStruct);
209719 iLvl++;
209720 }
209721 *ppStruct = pStruct;
209722}
209723
209724static int fts5IndexReturn(Fts5Index *p){
209725 int rc = p->rc;
209726 p->rc = SQLITE_OK;
209727 return rc;
209728}
209729
209730typedef struct Fts5FlushCtx Fts5FlushCtx;
209731struct Fts5FlushCtx {
209732 Fts5Index *pIdx;
209733 Fts5SegWriter writer;
209734};
209735
209736/*
209737** Buffer aBuf[] contains a list of varints, all small enough to fit
209738** in a 32-bit integer. Return the size of the largest prefix of this
209739** list nMax bytes or less in size.
209740*/
209741static int fts5PoslistPrefix(const u8 *aBuf, int nMax){
209742 int ret;
209743 u32 dummy;
209744 ret = fts5GetVarint32(aBuf, dummy);
209745 if( ret<nMax ){
209746 while( 1 ){
209747 int i = fts5GetVarint32(&aBuf[ret], dummy);
209748 if( (ret + i) > nMax ) break;
209749 ret += i;
209750 }
209751 }
209752 return ret;
209753}
209754
209755/*
209756** Flush the contents of in-memory hash table iHash to a new level-0
209757** segment on disk. Also update the corresponding structure record.
209758**
209759** If an error occurs, set the Fts5Index.rc error code. If an error has
209760** already occurred, this function is a no-op.
209761*/
209762static void fts5FlushOneHash(Fts5Index *p){
209763 Fts5Hash *pHash = p->pHash;
209764 Fts5Structure *pStruct;
209765 int iSegid;
209766 int pgnoLast = 0; /* Last leaf page number in segment */
209767
209768 /* Obtain a reference to the index structure and allocate a new segment-id
209769 ** for the new level-0 segment. */
209770 pStruct = fts5StructureRead(p);
209771 iSegid = fts5AllocateSegid(p, pStruct);
209772 fts5StructureInvalidate(p);
209773
209774 if( iSegid ){
209775 const int pgsz = p->pConfig->pgsz;
209776 int eDetail = p->pConfig->eDetail;
209777 Fts5StructureSegment *pSeg; /* New segment within pStruct */
209778 Fts5Buffer *pBuf; /* Buffer in which to assemble leaf page */
209779 Fts5Buffer *pPgidx; /* Buffer in which to assemble pgidx */
209780
209781 Fts5SegWriter writer;
209782 fts5WriteInit(p, &writer, iSegid);
209783
209784 pBuf = &writer.writer.buf;
209785 pPgidx = &writer.writer.pgidx;
209786
209787 /* fts5WriteInit() should have initialized the buffers to (most likely)
209788 ** the maximum space required. */
209789 assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) );
209790 assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) );
209791
209792 /* Begin scanning through hash table entries. This loop runs once for each
209793 ** term/doclist currently stored within the hash table. */
209794 if( p->rc==SQLITE_OK ){
209795 p->rc = sqlite3Fts5HashScanInit(pHash, 0, 0);
209796 }
209797 while( p->rc==SQLITE_OK && 0==sqlite3Fts5HashScanEof(pHash) ){
209798 const char *zTerm; /* Buffer containing term */
209799 const u8 *pDoclist; /* Pointer to doclist for this term */
209800 int nDoclist; /* Size of doclist in bytes */
209801
209802 /* Write the term for this entry to disk. */
209803 sqlite3Fts5HashScanEntry(pHash, &zTerm, &pDoclist, &nDoclist);
209804 fts5WriteAppendTerm(p, &writer, (int)strlen(zTerm), (const u8*)zTerm);
209805
209806 assert( writer.bFirstRowidInPage==0 );
209807 if( pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){
209808 /* The entire doclist will fit on the current leaf. */
209809 fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist);
209810 }else{
209811 i64 iRowid = 0;
209812 i64 iDelta = 0;
209813 int iOff = 0;
209814
209815 /* The entire doclist will not fit on this leaf. The following
209816 ** loop iterates through the poslists that make up the current
209817 ** doclist. */
209818 while( p->rc==SQLITE_OK && iOff<nDoclist ){
209819 iOff += fts5GetVarint(&pDoclist[iOff], (u64*)&iDelta);
209820 iRowid += iDelta;
209821
209822 if( writer.bFirstRowidInPage ){
209823 fts5PutU16(&pBuf->p[0], (u16)pBuf->n); /* first rowid on page */
209824 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowid);
209825 writer.bFirstRowidInPage = 0;
209826 fts5WriteDlidxAppend(p, &writer, iRowid);
209827 }else{
209828 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iDelta);
209829 }
209830 assert( pBuf->n<=pBuf->nSpace );
209831
209832 if( eDetail==FTS5_DETAIL_NONE ){
209833 if( iOff<nDoclist && pDoclist[iOff]==0 ){
209834 pBuf->p[pBuf->n++] = 0;
209835 iOff++;
209836 if( iOff<nDoclist && pDoclist[iOff]==0 ){
209837 pBuf->p[pBuf->n++] = 0;
209838 iOff++;
209839 }
209840 }
209841 if( (pBuf->n + pPgidx->n)>=pgsz ){
209842 fts5WriteFlushLeaf(p, &writer);
209843 }
209844 }else{
209845 int bDummy;
209846 int nPos;
209847 int nCopy = fts5GetPoslistSize(&pDoclist[iOff], &nPos, &bDummy);
209848 nCopy += nPos;
209849 if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){
209850 /* The entire poslist will fit on the current leaf. So copy
209851 ** it in one go. */
209852 fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy);
209853 }else{
209854 /* The entire poslist will not fit on this leaf. So it needs
209855 ** to be broken into sections. The only qualification being
209856 ** that each varint must be stored contiguously. */
209857 const u8 *pPoslist = &pDoclist[iOff];
209858 int iPos = 0;
209859 while( p->rc==SQLITE_OK ){
209860 int nSpace = pgsz - pBuf->n - pPgidx->n;
209861 int n = 0;
209862 if( (nCopy - iPos)<=nSpace ){
209863 n = nCopy - iPos;
209864 }else{
209865 n = fts5PoslistPrefix(&pPoslist[iPos], nSpace);
209866 }
209867 assert( n>0 );
209868 fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n);
209869 iPos += n;
209870 if( (pBuf->n + pPgidx->n)>=pgsz ){
209871 fts5WriteFlushLeaf(p, &writer);
209872 }
209873 if( iPos>=nCopy ) break;
209874 }
209875 }
209876 iOff += nCopy;
209877 }
209878 }
209879 }
209880
209881 /* TODO2: Doclist terminator written here. */
209882 /* pBuf->p[pBuf->n++] = '\0'; */
209883 assert( pBuf->n<=pBuf->nSpace );
209884 sqlite3Fts5HashScanNext(pHash);
209885 }
209886 sqlite3Fts5HashClear(pHash);
209887 fts5WriteFinish(p, &writer, &pgnoLast);
209888
209889 /* Update the Fts5Structure. It is written back to the database by the
209890 ** fts5StructureRelease() call below. */
209891 if( pStruct->nLevel==0 ){
209892 fts5StructureAddLevel(&p->rc, &pStruct);
209893 }
209894 fts5StructureExtendLevel(&p->rc, pStruct, 0, 1, 0);
209895 if( p->rc==SQLITE_OK ){
209896 pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ];
209897 pSeg->iSegid = iSegid;
209898 pSeg->pgnoFirst = 1;
209899 pSeg->pgnoLast = pgnoLast;
209900 pStruct->nSegment++;
209901 }
209902 fts5StructurePromote(p, 0, pStruct);
209903 }
209904
209905 fts5IndexAutomerge(p, &pStruct, pgnoLast);
209906 fts5IndexCrisismerge(p, &pStruct);
209907 fts5StructureWrite(p, pStruct);
209908 fts5StructureRelease(pStruct);
209909}
209910
209911/*
209912** Flush any data stored in the in-memory hash tables to the database.
209913*/
209914static void fts5IndexFlush(Fts5Index *p){
209915 /* Unless it is empty, flush the hash table to disk */
209916 if( p->nPendingData ){
209917 assert( p->pHash );
209918 p->nPendingData = 0;
209919 fts5FlushOneHash(p);
209920 }
209921}
209922
209923static Fts5Structure *fts5IndexOptimizeStruct(
209924 Fts5Index *p,
209925 Fts5Structure *pStruct
209926){
209927 Fts5Structure *pNew = 0;
209928 int nByte = sizeof(Fts5Structure);
209929 int nSeg = pStruct->nSegment;
209930 int i;
209931
209932 /* Figure out if this structure requires optimization. A structure does
209933 ** not require optimization if either:
209934 **
209935 ** + it consists of fewer than two segments, or
209936 ** + all segments are on the same level, or
209937 ** + all segments except one are currently inputs to a merge operation.
209938 **
209939 ** In the first case, return NULL. In the second, increment the ref-count
209940 ** on *pStruct and return a copy of the pointer to it.
209941 */
209942 if( nSeg<2 ) return 0;
209943 for(i=0; i<pStruct->nLevel; i++){
209944 int nThis = pStruct->aLevel[i].nSeg;
209945 if( nThis==nSeg || (nThis==nSeg-1 && pStruct->aLevel[i].nMerge==nThis) ){
209946 fts5StructureRef(pStruct);
209947 return pStruct;
209948 }
209949 assert( pStruct->aLevel[i].nMerge<=nThis );
209950 }
209951
209952 nByte += (pStruct->nLevel+1) * sizeof(Fts5StructureLevel);
209953 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(&p->rc, nByte);
209954
209955 if( pNew ){
209956 Fts5StructureLevel *pLvl;
209957 nByte = nSeg * sizeof(Fts5StructureSegment);
209958 pNew->nLevel = pStruct->nLevel+1;
209959 pNew->nRef = 1;
209960 pNew->nWriteCounter = pStruct->nWriteCounter;
209961 pLvl = &pNew->aLevel[pStruct->nLevel];
209962 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&p->rc, nByte);
209963 if( pLvl->aSeg ){
209964 int iLvl, iSeg;
209965 int iSegOut = 0;
209966 /* Iterate through all segments, from oldest to newest. Add them to
209967 ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest
209968 ** segment in the data structure. */
209969 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
209970 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
209971 pLvl->aSeg[iSegOut] = pStruct->aLevel[iLvl].aSeg[iSeg];
209972 iSegOut++;
209973 }
209974 }
209975 pNew->nSegment = pLvl->nSeg = nSeg;
209976 }else{
209977 sqlite3_free(pNew);
209978 pNew = 0;
209979 }
209980 }
209981
209982 return pNew;
209983}
209984
209985static int sqlite3Fts5IndexOptimize(Fts5Index *p){
209986 Fts5Structure *pStruct;
209987 Fts5Structure *pNew = 0;
209988
209989 assert( p->rc==SQLITE_OK );
209990 fts5IndexFlush(p);
209991 pStruct = fts5StructureRead(p);
209992 fts5StructureInvalidate(p);
209993
209994 if( pStruct ){
209995 pNew = fts5IndexOptimizeStruct(p, pStruct);
209996 }
209997 fts5StructureRelease(pStruct);
209998
209999 assert( pNew==0 || pNew->nSegment>0 );
210000 if( pNew ){
210001 int iLvl;
210002 for(iLvl=0; pNew->aLevel[iLvl].nSeg==0; iLvl++){}
210003 while( p->rc==SQLITE_OK && pNew->aLevel[iLvl].nSeg>0 ){
210004 int nRem = FTS5_OPT_WORK_UNIT;
210005 fts5IndexMergeLevel(p, &pNew, iLvl, &nRem);
210006 }
210007
210008 fts5StructureWrite(p, pNew);
210009 fts5StructureRelease(pNew);
210010 }
210011
210012 return fts5IndexReturn(p);
210013}
210014
210015/*
210016** This is called to implement the special "VALUES('merge', $nMerge)"
210017** INSERT command.
210018*/
210019static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge){
210020 Fts5Structure *pStruct = fts5StructureRead(p);
210021 if( pStruct ){
210022 int nMin = p->pConfig->nUsermerge;
210023 fts5StructureInvalidate(p);
210024 if( nMerge<0 ){
210025 Fts5Structure *pNew = fts5IndexOptimizeStruct(p, pStruct);
210026 fts5StructureRelease(pStruct);
210027 pStruct = pNew;
210028 nMin = 2;
210029 nMerge = nMerge*-1;
210030 }
210031 if( pStruct && pStruct->nLevel ){
210032 if( fts5IndexMerge(p, &pStruct, nMerge, nMin) ){
210033 fts5StructureWrite(p, pStruct);
210034 }
210035 }
210036 fts5StructureRelease(pStruct);
210037 }
210038 return fts5IndexReturn(p);
210039}
210040
210041static void fts5AppendRowid(
210042 Fts5Index *p,
210043 i64 iDelta,
210044 Fts5Iter *pUnused,
210045 Fts5Buffer *pBuf
210046){
210047 UNUSED_PARAM(pUnused);
210048 fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
210049}
210050
210051static void fts5AppendPoslist(
210052 Fts5Index *p,
210053 i64 iDelta,
210054 Fts5Iter *pMulti,
210055 Fts5Buffer *pBuf
210056){
210057 int nData = pMulti->base.nData;
210058 assert( nData>0 );
210059 if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nData+9+9) ){
210060 fts5BufferSafeAppendVarint(pBuf, iDelta);
210061 fts5BufferSafeAppendVarint(pBuf, nData*2);
210062 fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData);
210063 }
210064}
210065
210066
210067static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
210068 u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
210069
210070 assert( pIter->aPoslist );
210071 if( p>=pIter->aEof ){
210072 pIter->aPoslist = 0;
210073 }else{
210074 i64 iDelta;
210075
210076 p += fts5GetVarint(p, (u64*)&iDelta);
210077 pIter->iRowid += iDelta;
210078
210079 /* Read position list size */
210080 if( p[0] & 0x80 ){
210081 int nPos;
210082 pIter->nSize = fts5GetVarint32(p, nPos);
210083 pIter->nPoslist = (nPos>>1);
210084 }else{
210085 pIter->nPoslist = ((int)(p[0])) >> 1;
210086 pIter->nSize = 1;
210087 }
210088
210089 pIter->aPoslist = p;
210090 }
210091}
210092
210093static void fts5DoclistIterInit(
210094 Fts5Buffer *pBuf,
210095 Fts5DoclistIter *pIter
210096){
210097 memset(pIter, 0, sizeof(*pIter));
210098 pIter->aPoslist = pBuf->p;
210099 pIter->aEof = &pBuf->p[pBuf->n];
210100 fts5DoclistIterNext(pIter);
210101}
210102
210103#if 0
210104/*
210105** Append a doclist to buffer pBuf.
210106**
210107** This function assumes that space within the buffer has already been
210108** allocated.
210109*/
210110static void fts5MergeAppendDocid(
210111 Fts5Buffer *pBuf, /* Buffer to write to */
210112 i64 *piLastRowid, /* IN/OUT: Previous rowid written (if any) */
210113 i64 iRowid /* Rowid to append */
210114){
210115 assert( pBuf->n!=0 || (*piLastRowid)==0 );
210116 fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid);
210117 *piLastRowid = iRowid;
210118}
210119#endif
210120
210121#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \
210122 assert( (pBuf)->n!=0 || (iLastRowid)==0 ); \
210123 fts5BufferSafeAppendVarint((pBuf), (iRowid) - (iLastRowid)); \
210124 (iLastRowid) = (iRowid); \
210125}
210126
210127/*
210128** Swap the contents of buffer *p1 with that of *p2.
210129*/
210130static void fts5BufferSwap(Fts5Buffer *p1, Fts5Buffer *p2){
210131 Fts5Buffer tmp = *p1;
210132 *p1 = *p2;
210133 *p2 = tmp;
210134}
210135
210136static void fts5NextRowid(Fts5Buffer *pBuf, int *piOff, i64 *piRowid){
210137 int i = *piOff;
210138 if( i>=pBuf->n ){
210139 *piOff = -1;
210140 }else{
210141 u64 iVal;
210142 *piOff = i + sqlite3Fts5GetVarint(&pBuf->p[i], &iVal);
210143 *piRowid += iVal;
210144 }
210145}
210146
210147/*
210148** This is the equivalent of fts5MergePrefixLists() for detail=none mode.
210149** In this case the buffers consist of a delta-encoded list of rowids only.
210150*/
210151static void fts5MergeRowidLists(
210152 Fts5Index *p, /* FTS5 backend object */
210153 Fts5Buffer *p1, /* First list to merge */
210154 Fts5Buffer *p2 /* Second list to merge */
210155){
210156 int i1 = 0;
210157 int i2 = 0;
210158 i64 iRowid1 = 0;
210159 i64 iRowid2 = 0;
210160 i64 iOut = 0;
210161
210162 Fts5Buffer out;
210163 memset(&out, 0, sizeof(out));
210164 sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n);
210165 if( p->rc ) return;
210166
210167 fts5NextRowid(p1, &i1, &iRowid1);
210168 fts5NextRowid(p2, &i2, &iRowid2);
210169 while( i1>=0 || i2>=0 ){
210170 if( i1>=0 && (i2<0 || iRowid1<iRowid2) ){
210171 assert( iOut==0 || iRowid1>iOut );
210172 fts5BufferSafeAppendVarint(&out, iRowid1 - iOut);
210173 iOut = iRowid1;
210174 fts5NextRowid(p1, &i1, &iRowid1);
210175 }else{
210176 assert( iOut==0 || iRowid2>iOut );
210177 fts5BufferSafeAppendVarint(&out, iRowid2 - iOut);
210178 iOut = iRowid2;
210179 if( i1>=0 && iRowid1==iRowid2 ){
210180 fts5NextRowid(p1, &i1, &iRowid1);
210181 }
210182 fts5NextRowid(p2, &i2, &iRowid2);
210183 }
210184 }
210185
210186 fts5BufferSwap(&out, p1);
210187 fts5BufferFree(&out);
210188}
210189
210190/*
210191** Buffers p1 and p2 contain doclists. This function merges the content
210192** of the two doclists together and sets buffer p1 to the result before
210193** returning.
210194**
210195** If an error occurs, an error code is left in p->rc. If an error has
210196** already occurred, this function is a no-op.
210197*/
210198static void fts5MergePrefixLists(
210199 Fts5Index *p, /* FTS5 backend object */
210200 Fts5Buffer *p1, /* First list to merge */
210201 Fts5Buffer *p2 /* Second list to merge */
210202){
210203 if( p2->n ){
210204 i64 iLastRowid = 0;
210205 Fts5DoclistIter i1;
210206 Fts5DoclistIter i2;
210207 Fts5Buffer out = {0, 0, 0};
210208 Fts5Buffer tmp = {0, 0, 0};
210209
210210 /* The maximum size of the output is equal to the sum of the two
210211 ** input sizes + 1 varint (9 bytes). The extra varint is because if the
210212 ** first rowid in one input is a large negative number, and the first in
210213 ** the other a non-negative number, the delta for the non-negative
210214 ** number will be larger on disk than the literal integer value
210215 ** was. */
210216 if( sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n + 9) ) return;
210217 fts5DoclistIterInit(p1, &i1);
210218 fts5DoclistIterInit(p2, &i2);
210219
210220 while( 1 ){
210221 if( i1.iRowid<i2.iRowid ){
210222 /* Copy entry from i1 */
210223 fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
210224 fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.nPoslist+i1.nSize);
210225 fts5DoclistIterNext(&i1);
210226 if( i1.aPoslist==0 ) break;
210227 }
210228 else if( i2.iRowid!=i1.iRowid ){
210229 /* Copy entry from i2 */
210230 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
210231 fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.nPoslist+i2.nSize);
210232 fts5DoclistIterNext(&i2);
210233 if( i2.aPoslist==0 ) break;
210234 }
210235 else{
210236 /* Merge the two position lists. */
210237 i64 iPos1 = 0;
210238 i64 iPos2 = 0;
210239 int iOff1 = 0;
210240 int iOff2 = 0;
210241 u8 *a1 = &i1.aPoslist[i1.nSize];
210242 u8 *a2 = &i2.aPoslist[i2.nSize];
210243
210244 i64 iPrev = 0;
210245 Fts5PoslistWriter writer;
210246 memset(&writer, 0, sizeof(writer));
210247
210248 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
210249 fts5BufferZero(&tmp);
210250 sqlite3Fts5BufferSize(&p->rc, &tmp, i1.nPoslist + i2.nPoslist);
210251 if( p->rc ) break;
210252
210253 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
210254 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
210255 assert( iPos1>=0 && iPos2>=0 );
210256
210257 if( iPos1<iPos2 ){
210258 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
210259 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
210260 }else{
210261 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
210262 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
210263 }
210264
210265 if( iPos1>=0 && iPos2>=0 ){
210266 while( 1 ){
210267 if( iPos1<iPos2 ){
210268 if( iPos1!=iPrev ){
210269 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
210270 }
210271 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
210272 if( iPos1<0 ) break;
210273 }else{
210274 assert( iPos2!=iPrev );
210275 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
210276 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
210277 if( iPos2<0 ) break;
210278 }
210279 }
210280 }
210281
210282 if( iPos1>=0 ){
210283 if( iPos1!=iPrev ){
210284 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
210285 }
210286 fts5BufferSafeAppendBlob(&tmp, &a1[iOff1], i1.nPoslist-iOff1);
210287 }else{
210288 assert( iPos2>=0 && iPos2!=iPrev );
210289 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
210290 fts5BufferSafeAppendBlob(&tmp, &a2[iOff2], i2.nPoslist-iOff2);
210291 }
210292
210293 /* WRITEPOSLISTSIZE */
210294 fts5BufferSafeAppendVarint(&out, tmp.n * 2);
210295 fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
210296 fts5DoclistIterNext(&i1);
210297 fts5DoclistIterNext(&i2);
210298 if( i1.aPoslist==0 || i2.aPoslist==0 ) break;
210299 }
210300 }
210301
210302 if( i1.aPoslist ){
210303 fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
210304 fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.aEof - i1.aPoslist);
210305 }
210306 else if( i2.aPoslist ){
210307 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
210308 fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.aEof - i2.aPoslist);
210309 }
210310 assert( out.n<=(p1->n+p2->n+9) );
210311
210312 fts5BufferSet(&p->rc, p1, out.n, out.p);
210313 fts5BufferFree(&tmp);
210314 fts5BufferFree(&out);
210315 }
210316}
210317
210318static void fts5SetupPrefixIter(
210319 Fts5Index *p, /* Index to read from */
210320 int bDesc, /* True for "ORDER BY rowid DESC" */
210321 const u8 *pToken, /* Buffer containing prefix to match */
210322 int nToken, /* Size of buffer pToken in bytes */
210323 Fts5Colset *pColset, /* Restrict matches to these columns */
210324 Fts5Iter **ppIter /* OUT: New iterator */
210325){
210326 Fts5Structure *pStruct;
210327 Fts5Buffer *aBuf;
210328 const int nBuf = 32;
210329
210330 void (*xMerge)(Fts5Index*, Fts5Buffer*, Fts5Buffer*);
210331 void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*);
210332 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
210333 xMerge = fts5MergeRowidLists;
210334 xAppend = fts5AppendRowid;
210335 }else{
210336 xMerge = fts5MergePrefixLists;
210337 xAppend = fts5AppendPoslist;
210338 }
210339
210340 aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf);
210341 pStruct = fts5StructureRead(p);
210342
210343 if( aBuf && pStruct ){
210344 const int flags = FTS5INDEX_QUERY_SCAN
210345 | FTS5INDEX_QUERY_SKIPEMPTY
210346 | FTS5INDEX_QUERY_NOOUTPUT;
210347 int i;
210348 i64 iLastRowid = 0;
210349 Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
210350 Fts5Data *pData;
210351 Fts5Buffer doclist;
210352 int bNewTerm = 1;
210353
210354 memset(&doclist, 0, sizeof(doclist));
210355 fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
210356 fts5IterSetOutputCb(&p->rc, p1);
210357 for( /* no-op */ ;
210358 fts5MultiIterEof(p, p1)==0;
210359 fts5MultiIterNext2(p, p1, &bNewTerm)
210360 ){
210361 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
210362 int nTerm = pSeg->term.n;
210363 const u8 *pTerm = pSeg->term.p;
210364 p1->xSetOutputs(p1, pSeg);
210365
210366 assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 );
210367 if( bNewTerm ){
210368 if( nTerm<nToken || memcmp(pToken, pTerm, nToken) ) break;
210369 }
210370
210371 if( p1->base.nData==0 ) continue;
210372
210373 if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){
210374 for(i=0; p->rc==SQLITE_OK && doclist.n; i++){
210375 assert( i<nBuf );
210376 if( aBuf[i].n==0 ){
210377 fts5BufferSwap(&doclist, &aBuf[i]);
210378 fts5BufferZero(&doclist);
210379 }else{
210380 xMerge(p, &doclist, &aBuf[i]);
210381 fts5BufferZero(&aBuf[i]);
210382 }
210383 }
210384 iLastRowid = 0;
210385 }
210386
210387 xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
210388 iLastRowid = p1->base.iRowid;
210389 }
210390
210391 for(i=0; i<nBuf; i++){
210392 if( p->rc==SQLITE_OK ){
210393 xMerge(p, &doclist, &aBuf[i]);
210394 }
210395 fts5BufferFree(&aBuf[i]);
210396 }
210397 fts5MultiIterFree(p1);
210398
210399 pData = fts5IdxMalloc(p, sizeof(Fts5Data) + doclist.n);
210400 if( pData ){
210401 pData->p = (u8*)&pData[1];
210402 pData->nn = pData->szLeaf = doclist.n;
210403 if( doclist.n ) memcpy(pData->p, doclist.p, doclist.n);
210404 fts5MultiIterNew2(p, pData, bDesc, ppIter);
210405 }
210406 fts5BufferFree(&doclist);
210407 }
210408
210409 fts5StructureRelease(pStruct);
210410 sqlite3_free(aBuf);
210411}
210412
210413
210414/*
210415** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain
210416** to the document with rowid iRowid.
210417*/
210418static int sqlite3Fts5IndexBeginWrite(Fts5Index *p, int bDelete, i64 iRowid){
210419 assert( p->rc==SQLITE_OK );
210420
210421 /* Allocate the hash table if it has not already been allocated */
210422 if( p->pHash==0 ){
210423 p->rc = sqlite3Fts5HashNew(p->pConfig, &p->pHash, &p->nPendingData);
210424 }
210425
210426 /* Flush the hash table to disk if required */
210427 if( iRowid<p->iWriteRowid
210428 || (iRowid==p->iWriteRowid && p->bDelete==0)
210429 || (p->nPendingData > p->pConfig->nHashSize)
210430 ){
210431 fts5IndexFlush(p);
210432 }
210433
210434 p->iWriteRowid = iRowid;
210435 p->bDelete = bDelete;
210436 return fts5IndexReturn(p);
210437}
210438
210439/*
210440** Commit data to disk.
210441*/
210442static int sqlite3Fts5IndexSync(Fts5Index *p){
210443 assert( p->rc==SQLITE_OK );
210444 fts5IndexFlush(p);
210445 fts5CloseReader(p);
210446 return fts5IndexReturn(p);
210447}
210448
210449/*
210450** Discard any data stored in the in-memory hash tables. Do not write it
210451** to the database. Additionally, assume that the contents of the %_data
210452** table may have changed on disk. So any in-memory caches of %_data
210453** records must be invalidated.
210454*/
210455static int sqlite3Fts5IndexRollback(Fts5Index *p){
210456 fts5CloseReader(p);
210457 fts5IndexDiscardData(p);
210458 fts5StructureInvalidate(p);
210459 /* assert( p->rc==SQLITE_OK ); */
210460 return SQLITE_OK;
210461}
210462
210463/*
210464** The %_data table is completely empty when this function is called. This
210465** function populates it with the initial structure objects for each index,
210466** and the initial version of the "averages" record (a zero-byte blob).
210467*/
210468static int sqlite3Fts5IndexReinit(Fts5Index *p){
210469 Fts5Structure s;
210470 fts5StructureInvalidate(p);
210471 memset(&s, 0, sizeof(Fts5Structure));
210472 fts5DataWrite(p, FTS5_AVERAGES_ROWID, (const u8*)"", 0);
210473 fts5StructureWrite(p, &s);
210474 return fts5IndexReturn(p);
210475}
210476
210477/*
210478** Open a new Fts5Index handle. If the bCreate argument is true, create
210479** and initialize the underlying %_data table.
210480**
210481** If successful, set *pp to point to the new object and return SQLITE_OK.
210482** Otherwise, set *pp to NULL and return an SQLite error code.
210483*/
210484static int sqlite3Fts5IndexOpen(
210485 Fts5Config *pConfig,
210486 int bCreate,
210487 Fts5Index **pp,
210488 char **pzErr
210489){
210490 int rc = SQLITE_OK;
210491 Fts5Index *p; /* New object */
210492
210493 *pp = p = (Fts5Index*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Index));
210494 if( rc==SQLITE_OK ){
210495 p->pConfig = pConfig;
210496 p->nWorkUnit = FTS5_WORK_UNIT;
210497 p->zDataTbl = sqlite3Fts5Mprintf(&rc, "%s_data", pConfig->zName);
210498 if( p->zDataTbl && bCreate ){
210499 rc = sqlite3Fts5CreateTable(
210500 pConfig, "data", "id INTEGER PRIMARY KEY, block BLOB", 0, pzErr
210501 );
210502 if( rc==SQLITE_OK ){
210503 rc = sqlite3Fts5CreateTable(pConfig, "idx",
210504 "segid, term, pgno, PRIMARY KEY(segid, term)",
210505 1, pzErr
210506 );
210507 }
210508 if( rc==SQLITE_OK ){
210509 rc = sqlite3Fts5IndexReinit(p);
210510 }
210511 }
210512 }
210513
210514 assert( rc!=SQLITE_OK || p->rc==SQLITE_OK );
210515 if( rc ){
210516 sqlite3Fts5IndexClose(p);
210517 *pp = 0;
210518 }
210519 return rc;
210520}
210521
210522/*
210523** Close a handle opened by an earlier call to sqlite3Fts5IndexOpen().
210524*/
210525static int sqlite3Fts5IndexClose(Fts5Index *p){
210526 int rc = SQLITE_OK;
210527 if( p ){
210528 assert( p->pReader==0 );
210529 fts5StructureInvalidate(p);
210530 sqlite3_finalize(p->pWriter);
210531 sqlite3_finalize(p->pDeleter);
210532 sqlite3_finalize(p->pIdxWriter);
210533 sqlite3_finalize(p->pIdxDeleter);
210534 sqlite3_finalize(p->pIdxSelect);
210535 sqlite3_finalize(p->pDataVersion);
210536 sqlite3Fts5HashFree(p->pHash);
210537 sqlite3_free(p->zDataTbl);
210538 sqlite3_free(p);
210539 }
210540 return rc;
210541}
210542
210543/*
210544** Argument p points to a buffer containing utf-8 text that is n bytes in
210545** size. Return the number of bytes in the nChar character prefix of the
210546** buffer, or 0 if there are less than nChar characters in total.
210547*/
210548static int sqlite3Fts5IndexCharlenToBytelen(
210549 const char *p,
210550 int nByte,
210551 int nChar
210552){
210553 int n = 0;
210554 int i;
210555 for(i=0; i<nChar; i++){
210556 if( n>=nByte ) return 0; /* Input contains fewer than nChar chars */
210557 if( (unsigned char)p[n++]>=0xc0 ){
210558 while( (p[n] & 0xc0)==0x80 ){
210559 n++;
210560 if( n>=nByte ) break;
210561 }
210562 }
210563 }
210564 return n;
210565}
210566
210567/*
210568** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of
210569** unicode characters in the string.
210570*/
210571static int fts5IndexCharlen(const char *pIn, int nIn){
210572 int nChar = 0;
210573 int i = 0;
210574 while( i<nIn ){
210575 if( (unsigned char)pIn[i++]>=0xc0 ){
210576 while( i<nIn && (pIn[i] & 0xc0)==0x80 ) i++;
210577 }
210578 nChar++;
210579 }
210580 return nChar;
210581}
210582
210583/*
210584** Insert or remove data to or from the index. Each time a document is
210585** added to or removed from the index, this function is called one or more
210586** times.
210587**
210588** For an insert, it must be called once for each token in the new document.
210589** If the operation is a delete, it must be called (at least) once for each
210590** unique token in the document with an iCol value less than zero. The iPos
210591** argument is ignored for a delete.
210592*/
210593static int sqlite3Fts5IndexWrite(
210594 Fts5Index *p, /* Index to write to */
210595 int iCol, /* Column token appears in (-ve -> delete) */
210596 int iPos, /* Position of token within column */
210597 const char *pToken, int nToken /* Token to add or remove to or from index */
210598){
210599 int i; /* Used to iterate through indexes */
210600 int rc = SQLITE_OK; /* Return code */
210601 Fts5Config *pConfig = p->pConfig;
210602
210603 assert( p->rc==SQLITE_OK );
210604 assert( (iCol<0)==p->bDelete );
210605
210606 /* Add the entry to the main terms index. */
210607 rc = sqlite3Fts5HashWrite(
210608 p->pHash, p->iWriteRowid, iCol, iPos, FTS5_MAIN_PREFIX, pToken, nToken
210609 );
210610
210611 for(i=0; i<pConfig->nPrefix && rc==SQLITE_OK; i++){
210612 const int nChar = pConfig->aPrefix[i];
210613 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
210614 if( nByte ){
210615 rc = sqlite3Fts5HashWrite(p->pHash,
210616 p->iWriteRowid, iCol, iPos, (char)(FTS5_MAIN_PREFIX+i+1), pToken,
210617 nByte
210618 );
210619 }
210620 }
210621
210622 return rc;
210623}
210624
210625/*
210626** Open a new iterator to iterate though all rowid that match the
210627** specified token or token prefix.
210628*/
210629static int sqlite3Fts5IndexQuery(
210630 Fts5Index *p, /* FTS index to query */
210631 const char *pToken, int nToken, /* Token (or prefix) to query for */
210632 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
210633 Fts5Colset *pColset, /* Match these columns only */
210634 Fts5IndexIter **ppIter /* OUT: New iterator object */
210635){
210636 Fts5Config *pConfig = p->pConfig;
210637 Fts5Iter *pRet = 0;
210638 Fts5Buffer buf = {0, 0, 0};
210639
210640 /* If the QUERY_SCAN flag is set, all other flags must be clear. */
210641 assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN );
210642
210643 if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
210644 int iIdx = 0; /* Index to search */
210645 if( nToken ) memcpy(&buf.p[1], pToken, nToken);
210646
210647 /* Figure out which index to search and set iIdx accordingly. If this
210648 ** is a prefix query for which there is no prefix index, set iIdx to
210649 ** greater than pConfig->nPrefix to indicate that the query will be
210650 ** satisfied by scanning multiple terms in the main index.
210651 **
210652 ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
210653 ** prefix-query. Instead of using a prefix-index (if one exists),
210654 ** evaluate the prefix query using the main FTS index. This is used
210655 ** for internal sanity checking by the integrity-check in debug
210656 ** mode only. */
210657#ifdef SQLITE_DEBUG
210658 if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){
210659 assert( flags & FTS5INDEX_QUERY_PREFIX );
210660 iIdx = 1+pConfig->nPrefix;
210661 }else
210662#endif
210663 if( flags & FTS5INDEX_QUERY_PREFIX ){
210664 int nChar = fts5IndexCharlen(pToken, nToken);
210665 for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){
210666 if( pConfig->aPrefix[iIdx-1]==nChar ) break;
210667 }
210668 }
210669
210670 if( iIdx<=pConfig->nPrefix ){
210671 /* Straight index lookup */
210672 Fts5Structure *pStruct = fts5StructureRead(p);
210673 buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx);
210674 if( pStruct ){
210675 fts5MultiIterNew(p, pStruct, flags | FTS5INDEX_QUERY_SKIPEMPTY,
210676 pColset, buf.p, nToken+1, -1, 0, &pRet
210677 );
210678 fts5StructureRelease(pStruct);
210679 }
210680 }else{
210681 /* Scan multiple terms in the main index */
210682 int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0;
210683 buf.p[0] = FTS5_MAIN_PREFIX;
210684 fts5SetupPrefixIter(p, bDesc, buf.p, nToken+1, pColset, &pRet);
210685 assert( p->rc!=SQLITE_OK || pRet->pColset==0 );
210686 fts5IterSetOutputCb(&p->rc, pRet);
210687 if( p->rc==SQLITE_OK ){
210688 Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
210689 if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
210690 }
210691 }
210692
210693 if( p->rc ){
210694 sqlite3Fts5IterClose((Fts5IndexIter*)pRet);
210695 pRet = 0;
210696 fts5CloseReader(p);
210697 }
210698
210699 *ppIter = &pRet->base;
210700 sqlite3Fts5BufferFree(&buf);
210701 }
210702 return fts5IndexReturn(p);
210703}
210704
210705/*
210706** Return true if the iterator passed as the only argument is at EOF.
210707*/
210708/*
210709** Move to the next matching rowid.
210710*/
210711static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
210712 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
210713 assert( pIter->pIndex->rc==SQLITE_OK );
210714 fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
210715 return fts5IndexReturn(pIter->pIndex);
210716}
210717
210718/*
210719** Move to the next matching term/rowid. Used by the fts5vocab module.
210720*/
210721static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
210722 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
210723 Fts5Index *p = pIter->pIndex;
210724
210725 assert( pIter->pIndex->rc==SQLITE_OK );
210726
210727 fts5MultiIterNext(p, pIter, 0, 0);
210728 if( p->rc==SQLITE_OK ){
210729 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
210730 if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX ){
210731 fts5DataRelease(pSeg->pLeaf);
210732 pSeg->pLeaf = 0;
210733 pIter->base.bEof = 1;
210734 }
210735 }
210736
210737 return fts5IndexReturn(pIter->pIndex);
210738}
210739
210740/*
210741** Move to the next matching rowid that occurs at or after iMatch. The
210742** definition of "at or after" depends on whether this iterator iterates
210743** in ascending or descending rowid order.
210744*/
210745static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
210746 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
210747 fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
210748 return fts5IndexReturn(pIter->pIndex);
210749}
210750
210751/*
210752** Return the current term.
210753*/
210754static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
210755 int n;
210756 const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n);
210757 *pn = n-1;
210758 return &z[1];
210759}
210760
210761/*
210762** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
210763*/
210764static void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
210765 if( pIndexIter ){
210766 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
210767 Fts5Index *pIndex = pIter->pIndex;
210768 fts5MultiIterFree(pIter);
210769 fts5CloseReader(pIndex);
210770 }
210771}
210772
210773/*
210774** Read and decode the "averages" record from the database.
210775**
210776** Parameter anSize must point to an array of size nCol, where nCol is
210777** the number of user defined columns in the FTS table.
210778*/
210779static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize){
210780 int nCol = p->pConfig->nCol;
210781 Fts5Data *pData;
210782
210783 *pnRow = 0;
210784 memset(anSize, 0, sizeof(i64) * nCol);
210785 pData = fts5DataRead(p, FTS5_AVERAGES_ROWID);
210786 if( p->rc==SQLITE_OK && pData->nn ){
210787 int i = 0;
210788 int iCol;
210789 i += fts5GetVarint(&pData->p[i], (u64*)pnRow);
210790 for(iCol=0; i<pData->nn && iCol<nCol; iCol++){
210791 i += fts5GetVarint(&pData->p[i], (u64*)&anSize[iCol]);
210792 }
210793 }
210794
210795 fts5DataRelease(pData);
210796 return fts5IndexReturn(p);
210797}
210798
210799/*
210800** Replace the current "averages" record with the contents of the buffer
210801** supplied as the second argument.
210802*/
210803static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){
210804 assert( p->rc==SQLITE_OK );
210805 fts5DataWrite(p, FTS5_AVERAGES_ROWID, pData, nData);
210806 return fts5IndexReturn(p);
210807}
210808
210809/*
210810** Return the total number of blocks this module has read from the %_data
210811** table since it was created.
210812*/
210813static int sqlite3Fts5IndexReads(Fts5Index *p){
210814 return p->nRead;
210815}
210816
210817/*
210818** Set the 32-bit cookie value stored at the start of all structure
210819** records to the value passed as the second argument.
210820**
210821** Return SQLITE_OK if successful, or an SQLite error code if an error
210822** occurs.
210823*/
210824static int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){
210825 int rc; /* Return code */
210826 Fts5Config *pConfig = p->pConfig; /* Configuration object */
210827 u8 aCookie[4]; /* Binary representation of iNew */
210828 sqlite3_blob *pBlob = 0;
210829
210830 assert( p->rc==SQLITE_OK );
210831 sqlite3Fts5Put32(aCookie, iNew);
210832
210833 rc = sqlite3_blob_open(pConfig->db, pConfig->zDb, p->zDataTbl,
210834 "block", FTS5_STRUCTURE_ROWID, 1, &pBlob
210835 );
210836 if( rc==SQLITE_OK ){
210837 sqlite3_blob_write(pBlob, aCookie, 4, 0);
210838 rc = sqlite3_blob_close(pBlob);
210839 }
210840
210841 return rc;
210842}
210843
210844static int sqlite3Fts5IndexLoadConfig(Fts5Index *p){
210845 Fts5Structure *pStruct;
210846 pStruct = fts5StructureRead(p);
210847 fts5StructureRelease(pStruct);
210848 return fts5IndexReturn(p);
210849}
210850
210851
210852/*************************************************************************
210853**************************************************************************
210854** Below this point is the implementation of the integrity-check
210855** functionality.
210856*/
210857
210858/*
210859** Return a simple checksum value based on the arguments.
210860*/
210861static u64 sqlite3Fts5IndexEntryCksum(
210862 i64 iRowid,
210863 int iCol,
210864 int iPos,
210865 int iIdx,
210866 const char *pTerm,
210867 int nTerm
210868){
210869 int i;
210870 u64 ret = iRowid;
210871 ret += (ret<<3) + iCol;
210872 ret += (ret<<3) + iPos;
210873 if( iIdx>=0 ) ret += (ret<<3) + (FTS5_MAIN_PREFIX + iIdx);
210874 for(i=0; i<nTerm; i++) ret += (ret<<3) + pTerm[i];
210875 return ret;
210876}
210877
210878#ifdef SQLITE_DEBUG
210879/*
210880** This function is purely an internal test. It does not contribute to
210881** FTS functionality, or even the integrity-check, in any way.
210882**
210883** Instead, it tests that the same set of pgno/rowid combinations are
210884** visited regardless of whether the doclist-index identified by parameters
210885** iSegid/iLeaf is iterated in forwards or reverse order.
210886*/
210887static void fts5TestDlidxReverse(
210888 Fts5Index *p,
210889 int iSegid, /* Segment id to load from */
210890 int iLeaf /* Load doclist-index for this leaf */
210891){
210892 Fts5DlidxIter *pDlidx = 0;
210893 u64 cksum1 = 13;
210894 u64 cksum2 = 13;
210895
210896 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iLeaf);
210897 fts5DlidxIterEof(p, pDlidx)==0;
210898 fts5DlidxIterNext(p, pDlidx)
210899 ){
210900 i64 iRowid = fts5DlidxIterRowid(pDlidx);
210901 int pgno = fts5DlidxIterPgno(pDlidx);
210902 assert( pgno>iLeaf );
210903 cksum1 += iRowid + ((i64)pgno<<32);
210904 }
210905 fts5DlidxIterFree(pDlidx);
210906 pDlidx = 0;
210907
210908 for(pDlidx=fts5DlidxIterInit(p, 1, iSegid, iLeaf);
210909 fts5DlidxIterEof(p, pDlidx)==0;
210910 fts5DlidxIterPrev(p, pDlidx)
210911 ){
210912 i64 iRowid = fts5DlidxIterRowid(pDlidx);
210913 int pgno = fts5DlidxIterPgno(pDlidx);
210914 assert( fts5DlidxIterPgno(pDlidx)>iLeaf );
210915 cksum2 += iRowid + ((i64)pgno<<32);
210916 }
210917 fts5DlidxIterFree(pDlidx);
210918 pDlidx = 0;
210919
210920 if( p->rc==SQLITE_OK && cksum1!=cksum2 ) p->rc = FTS5_CORRUPT;
210921}
210922
210923static int fts5QueryCksum(
210924 Fts5Index *p, /* Fts5 index object */
210925 int iIdx,
210926 const char *z, /* Index key to query for */
210927 int n, /* Size of index key in bytes */
210928 int flags, /* Flags for Fts5IndexQuery */
210929 u64 *pCksum /* IN/OUT: Checksum value */
210930){
210931 int eDetail = p->pConfig->eDetail;
210932 u64 cksum = *pCksum;
210933 Fts5IndexIter *pIter = 0;
210934 int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIter);
210935
210936 while( rc==SQLITE_OK && 0==sqlite3Fts5IterEof(pIter) ){
210937 i64 rowid = pIter->iRowid;
210938
210939 if( eDetail==FTS5_DETAIL_NONE ){
210940 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
210941 }else{
210942 Fts5PoslistReader sReader;
210943 for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
210944 sReader.bEof==0;
210945 sqlite3Fts5PoslistReaderNext(&sReader)
210946 ){
210947 int iCol = FTS5_POS2COLUMN(sReader.iPos);
210948 int iOff = FTS5_POS2OFFSET(sReader.iPos);
210949 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
210950 }
210951 }
210952 if( rc==SQLITE_OK ){
210953 rc = sqlite3Fts5IterNext(pIter);
210954 }
210955 }
210956 sqlite3Fts5IterClose(pIter);
210957
210958 *pCksum = cksum;
210959 return rc;
210960}
210961
210962
210963/*
210964** This function is also purely an internal test. It does not contribute to
210965** FTS functionality, or even the integrity-check, in any way.
210966*/
210967static void fts5TestTerm(
210968 Fts5Index *p,
210969 Fts5Buffer *pPrev, /* Previous term */
210970 const char *z, int n, /* Possibly new term to test */
210971 u64 expected,
210972 u64 *pCksum
210973){
210974 int rc = p->rc;
210975 if( pPrev->n==0 ){
210976 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
210977 }else
210978 if( rc==SQLITE_OK && (pPrev->n!=n || memcmp(pPrev->p, z, n)) ){
210979 u64 cksum3 = *pCksum;
210980 const char *zTerm = (const char*)&pPrev->p[1]; /* term sans prefix-byte */
210981 int nTerm = pPrev->n-1; /* Size of zTerm in bytes */
210982 int iIdx = (pPrev->p[0] - FTS5_MAIN_PREFIX);
210983 int flags = (iIdx==0 ? 0 : FTS5INDEX_QUERY_PREFIX);
210984 u64 ck1 = 0;
210985 u64 ck2 = 0;
210986
210987 /* Check that the results returned for ASC and DESC queries are
210988 ** the same. If not, call this corruption. */
210989 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, flags, &ck1);
210990 if( rc==SQLITE_OK ){
210991 int f = flags|FTS5INDEX_QUERY_DESC;
210992 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
210993 }
210994 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
210995
210996 /* If this is a prefix query, check that the results returned if the
210997 ** the index is disabled are the same. In both ASC and DESC order.
210998 **
210999 ** This check may only be performed if the hash table is empty. This
211000 ** is because the hash table only supports a single scan query at
211001 ** a time, and the multi-iter loop from which this function is called
211002 ** is already performing such a scan. */
211003 if( p->nPendingData==0 ){
211004 if( iIdx>0 && rc==SQLITE_OK ){
211005 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX;
211006 ck2 = 0;
211007 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
211008 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
211009 }
211010 if( iIdx>0 && rc==SQLITE_OK ){
211011 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX|FTS5INDEX_QUERY_DESC;
211012 ck2 = 0;
211013 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
211014 if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
211015 }
211016 }
211017
211018 cksum3 ^= ck1;
211019 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
211020
211021 if( rc==SQLITE_OK && cksum3!=expected ){
211022 rc = FTS5_CORRUPT;
211023 }
211024 *pCksum = cksum3;
211025 }
211026 p->rc = rc;
211027}
211028
211029#else
211030# define fts5TestDlidxReverse(x,y,z)
211031# define fts5TestTerm(u,v,w,x,y,z)
211032#endif
211033
211034/*
211035** Check that:
211036**
211037** 1) All leaves of pSeg between iFirst and iLast (inclusive) exist and
211038** contain zero terms.
211039** 2) All leaves of pSeg between iNoRowid and iLast (inclusive) exist and
211040** contain zero rowids.
211041*/
211042static void fts5IndexIntegrityCheckEmpty(
211043 Fts5Index *p,
211044 Fts5StructureSegment *pSeg, /* Segment to check internal consistency */
211045 int iFirst,
211046 int iNoRowid,
211047 int iLast
211048){
211049 int i;
211050
211051 /* Now check that the iter.nEmpty leaves following the current leaf
211052 ** (a) exist and (b) contain no terms. */
211053 for(i=iFirst; p->rc==SQLITE_OK && i<=iLast; i++){
211054 Fts5Data *pLeaf = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i));
211055 if( pLeaf ){
211056 if( !fts5LeafIsTermless(pLeaf) ) p->rc = FTS5_CORRUPT;
211057 if( i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf) ) p->rc = FTS5_CORRUPT;
211058 }
211059 fts5DataRelease(pLeaf);
211060 }
211061}
211062
211063static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
211064 int iTermOff = 0;
211065 int ii;
211066
211067 Fts5Buffer buf1 = {0,0,0};
211068 Fts5Buffer buf2 = {0,0,0};
211069
211070 ii = pLeaf->szLeaf;
211071 while( ii<pLeaf->nn && p->rc==SQLITE_OK ){
211072 int res;
211073 int iOff;
211074 int nIncr;
211075
211076 ii += fts5GetVarint32(&pLeaf->p[ii], nIncr);
211077 iTermOff += nIncr;
211078 iOff = iTermOff;
211079
211080 if( iOff>=pLeaf->szLeaf ){
211081 p->rc = FTS5_CORRUPT;
211082 }else if( iTermOff==nIncr ){
211083 int nByte;
211084 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
211085 if( (iOff+nByte)>pLeaf->szLeaf ){
211086 p->rc = FTS5_CORRUPT;
211087 }else{
211088 fts5BufferSet(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
211089 }
211090 }else{
211091 int nKeep, nByte;
211092 iOff += fts5GetVarint32(&pLeaf->p[iOff], nKeep);
211093 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
211094 if( nKeep>buf1.n || (iOff+nByte)>pLeaf->szLeaf ){
211095 p->rc = FTS5_CORRUPT;
211096 }else{
211097 buf1.n = nKeep;
211098 fts5BufferAppendBlob(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
211099 }
211100
211101 if( p->rc==SQLITE_OK ){
211102 res = fts5BufferCompare(&buf1, &buf2);
211103 if( res<=0 ) p->rc = FTS5_CORRUPT;
211104 }
211105 }
211106 fts5BufferSet(&p->rc, &buf2, buf1.n, buf1.p);
211107 }
211108
211109 fts5BufferFree(&buf1);
211110 fts5BufferFree(&buf2);
211111}
211112
211113static void fts5IndexIntegrityCheckSegment(
211114 Fts5Index *p, /* FTS5 backend object */
211115 Fts5StructureSegment *pSeg /* Segment to check internal consistency */
211116){
211117 Fts5Config *pConfig = p->pConfig;
211118 sqlite3_stmt *pStmt = 0;
211119 int rc2;
211120 int iIdxPrevLeaf = pSeg->pgnoFirst-1;
211121 int iDlidxPrevLeaf = pSeg->pgnoLast;
211122
211123 if( pSeg->pgnoFirst==0 ) return;
211124
211125 fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf(
211126 "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d",
211127 pConfig->zDb, pConfig->zName, pSeg->iSegid
211128 ));
211129
211130 /* Iterate through the b-tree hierarchy. */
211131 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
211132 i64 iRow; /* Rowid for this leaf */
211133 Fts5Data *pLeaf; /* Data for this leaf */
211134
211135 int nIdxTerm = sqlite3_column_bytes(pStmt, 1);
211136 const char *zIdxTerm = (const char*)sqlite3_column_text(pStmt, 1);
211137 int iIdxLeaf = sqlite3_column_int(pStmt, 2);
211138 int bIdxDlidx = sqlite3_column_int(pStmt, 3);
211139
211140 /* If the leaf in question has already been trimmed from the segment,
211141 ** ignore this b-tree entry. Otherwise, load it into memory. */
211142 if( iIdxLeaf<pSeg->pgnoFirst ) continue;
211143 iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf);
211144 pLeaf = fts5LeafRead(p, iRow);
211145 if( pLeaf==0 ) break;
211146
211147 /* Check that the leaf contains at least one term, and that it is equal
211148 ** to or larger than the split-key in zIdxTerm. Also check that if there
211149 ** is also a rowid pointer within the leaf page header, it points to a
211150 ** location before the term. */
211151 if( pLeaf->nn<=pLeaf->szLeaf ){
211152 p->rc = FTS5_CORRUPT;
211153 }else{
211154 int iOff; /* Offset of first term on leaf */
211155 int iRowidOff; /* Offset of first rowid on leaf */
211156 int nTerm; /* Size of term on leaf in bytes */
211157 int res; /* Comparison of term and split-key */
211158
211159 iOff = fts5LeafFirstTermOff(pLeaf);
211160 iRowidOff = fts5LeafFirstRowidOff(pLeaf);
211161 if( iRowidOff>=iOff ){
211162 p->rc = FTS5_CORRUPT;
211163 }else{
211164 iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm);
211165 res = memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm));
211166 if( res==0 ) res = nTerm - nIdxTerm;
211167 if( res<0 ) p->rc = FTS5_CORRUPT;
211168 }
211169
211170 fts5IntegrityCheckPgidx(p, pLeaf);
211171 }
211172 fts5DataRelease(pLeaf);
211173 if( p->rc ) break;
211174
211175 /* Now check that the iter.nEmpty leaves following the current leaf
211176 ** (a) exist and (b) contain no terms. */
211177 fts5IndexIntegrityCheckEmpty(
211178 p, pSeg, iIdxPrevLeaf+1, iDlidxPrevLeaf+1, iIdxLeaf-1
211179 );
211180 if( p->rc ) break;
211181
211182 /* If there is a doclist-index, check that it looks right. */
211183 if( bIdxDlidx ){
211184 Fts5DlidxIter *pDlidx = 0; /* For iterating through doclist index */
211185 int iPrevLeaf = iIdxLeaf;
211186 int iSegid = pSeg->iSegid;
211187 int iPg = 0;
211188 i64 iKey;
211189
211190 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iIdxLeaf);
211191 fts5DlidxIterEof(p, pDlidx)==0;
211192 fts5DlidxIterNext(p, pDlidx)
211193 ){
211194
211195 /* Check any rowid-less pages that occur before the current leaf. */
211196 for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pDlidx); iPg++){
211197 iKey = FTS5_SEGMENT_ROWID(iSegid, iPg);
211198 pLeaf = fts5DataRead(p, iKey);
211199 if( pLeaf ){
211200 if( fts5LeafFirstRowidOff(pLeaf)!=0 ) p->rc = FTS5_CORRUPT;
211201 fts5DataRelease(pLeaf);
211202 }
211203 }
211204 iPrevLeaf = fts5DlidxIterPgno(pDlidx);
211205
211206 /* Check that the leaf page indicated by the iterator really does
211207 ** contain the rowid suggested by the same. */
211208 iKey = FTS5_SEGMENT_ROWID(iSegid, iPrevLeaf);
211209 pLeaf = fts5DataRead(p, iKey);
211210 if( pLeaf ){
211211 i64 iRowid;
211212 int iRowidOff = fts5LeafFirstRowidOff(pLeaf);
211213 ASSERT_SZLEAF_OK(pLeaf);
211214 if( iRowidOff>=pLeaf->szLeaf ){
211215 p->rc = FTS5_CORRUPT;
211216 }else{
211217 fts5GetVarint(&pLeaf->p[iRowidOff], (u64*)&iRowid);
211218 if( iRowid!=fts5DlidxIterRowid(pDlidx) ) p->rc = FTS5_CORRUPT;
211219 }
211220 fts5DataRelease(pLeaf);
211221 }
211222 }
211223
211224 iDlidxPrevLeaf = iPg;
211225 fts5DlidxIterFree(pDlidx);
211226 fts5TestDlidxReverse(p, iSegid, iIdxLeaf);
211227 }else{
211228 iDlidxPrevLeaf = pSeg->pgnoLast;
211229 /* TODO: Check there is no doclist index */
211230 }
211231
211232 iIdxPrevLeaf = iIdxLeaf;
211233 }
211234
211235 rc2 = sqlite3_finalize(pStmt);
211236 if( p->rc==SQLITE_OK ) p->rc = rc2;
211237
211238 /* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
211239#if 0
211240 if( p->rc==SQLITE_OK && iter.iLeaf!=pSeg->pgnoLast ){
211241 p->rc = FTS5_CORRUPT;
211242 }
211243#endif
211244}
211245
211246
211247/*
211248** Run internal checks to ensure that the FTS index (a) is internally
211249** consistent and (b) contains entries for which the XOR of the checksums
211250** as calculated by sqlite3Fts5IndexEntryCksum() is cksum.
211251**
211252** Return SQLITE_CORRUPT if any of the internal checks fail, or if the
211253** checksum does not match. Return SQLITE_OK if all checks pass without
211254** error, or some other SQLite error code if another error (e.g. OOM)
211255** occurs.
211256*/
211257static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum){
211258 int eDetail = p->pConfig->eDetail;
211259 u64 cksum2 = 0; /* Checksum based on contents of indexes */
211260 Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */
211261 Fts5Iter *pIter; /* Used to iterate through entire index */
211262 Fts5Structure *pStruct; /* Index structure */
211263
211264#ifdef SQLITE_DEBUG
211265 /* Used by extra internal tests only run if NDEBUG is not defined */
211266 u64 cksum3 = 0; /* Checksum based on contents of indexes */
211267 Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */
211268#endif
211269 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
211270
211271 /* Load the FTS index structure */
211272 pStruct = fts5StructureRead(p);
211273
211274 /* Check that the internal nodes of each segment match the leaves */
211275 if( pStruct ){
211276 int iLvl, iSeg;
211277 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
211278 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
211279 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
211280 fts5IndexIntegrityCheckSegment(p, pSeg);
211281 }
211282 }
211283 }
211284
211285 /* The cksum argument passed to this function is a checksum calculated
211286 ** based on all expected entries in the FTS index (including prefix index
211287 ** entries). This block checks that a checksum calculated based on the
211288 ** actual contents of FTS index is identical.
211289 **
211290 ** Two versions of the same checksum are calculated. The first (stack
211291 ** variable cksum2) based on entries extracted from the full-text index
211292 ** while doing a linear scan of each individual index in turn.
211293 **
211294 ** As each term visited by the linear scans, a separate query for the
211295 ** same term is performed. cksum3 is calculated based on the entries
211296 ** extracted by these queries.
211297 */
211298 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, -1, 0, &pIter);
211299 fts5MultiIterEof(p, pIter)==0;
211300 fts5MultiIterNext(p, pIter, 0, 0)
211301 ){
211302 int n; /* Size of term in bytes */
211303 i64 iPos = 0; /* Position read from poslist */
211304 int iOff = 0; /* Offset within poslist */
211305 i64 iRowid = fts5MultiIterRowid(pIter);
211306 char *z = (char*)fts5MultiIterTerm(pIter, &n);
211307
211308 /* If this is a new term, query for it. Update cksum3 with the results. */
211309 fts5TestTerm(p, &term, z, n, cksum2, &cksum3);
211310
211311 if( eDetail==FTS5_DETAIL_NONE ){
211312 if( 0==fts5MultiIterIsEmpty(p, pIter) ){
211313 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, 0, 0, -1, z, n);
211314 }
211315 }else{
211316 poslist.n = 0;
211317 fts5SegiterPoslist(p, &pIter->aSeg[pIter->aFirst[1].iFirst], 0, &poslist);
211318 while( 0==sqlite3Fts5PoslistNext64(poslist.p, poslist.n, &iOff, &iPos) ){
211319 int iCol = FTS5_POS2COLUMN(iPos);
211320 int iTokOff = FTS5_POS2OFFSET(iPos);
211321 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol, iTokOff, -1, z, n);
211322 }
211323 }
211324 }
211325 fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
211326
211327 fts5MultiIterFree(pIter);
211328 if( p->rc==SQLITE_OK && cksum!=cksum2 ) p->rc = FTS5_CORRUPT;
211329
211330 fts5StructureRelease(pStruct);
211331#ifdef SQLITE_DEBUG
211332 fts5BufferFree(&term);
211333#endif
211334 fts5BufferFree(&poslist);
211335 return fts5IndexReturn(p);
211336}
211337
211338/*************************************************************************
211339**************************************************************************
211340** Below this point is the implementation of the fts5_decode() scalar
211341** function only.
211342*/
211343
211344/*
211345** Decode a segment-data rowid from the %_data table. This function is
211346** the opposite of macro FTS5_SEGMENT_ROWID().
211347*/
211348static void fts5DecodeRowid(
211349 i64 iRowid, /* Rowid from %_data table */
211350 int *piSegid, /* OUT: Segment id */
211351 int *pbDlidx, /* OUT: Dlidx flag */
211352 int *piHeight, /* OUT: Height */
211353 int *piPgno /* OUT: Page number */
211354){
211355 *piPgno = (int)(iRowid & (((i64)1 << FTS5_DATA_PAGE_B) - 1));
211356 iRowid >>= FTS5_DATA_PAGE_B;
211357
211358 *piHeight = (int)(iRowid & (((i64)1 << FTS5_DATA_HEIGHT_B) - 1));
211359 iRowid >>= FTS5_DATA_HEIGHT_B;
211360
211361 *pbDlidx = (int)(iRowid & 0x0001);
211362 iRowid >>= FTS5_DATA_DLI_B;
211363
211364 *piSegid = (int)(iRowid & (((i64)1 << FTS5_DATA_ID_B) - 1));
211365}
211366
211367static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){
211368 int iSegid, iHeight, iPgno, bDlidx; /* Rowid compenents */
211369 fts5DecodeRowid(iKey, &iSegid, &bDlidx, &iHeight, &iPgno);
211370
211371 if( iSegid==0 ){
211372 if( iKey==FTS5_AVERAGES_ROWID ){
211373 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{averages} ");
211374 }else{
211375 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{structure}");
211376 }
211377 }
211378 else{
211379 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%ssegid=%d h=%d pgno=%d}",
211380 bDlidx ? "dlidx " : "", iSegid, iHeight, iPgno
211381 );
211382 }
211383}
211384
211385static void fts5DebugStructure(
211386 int *pRc, /* IN/OUT: error code */
211387 Fts5Buffer *pBuf,
211388 Fts5Structure *p
211389){
211390 int iLvl, iSeg; /* Iterate through levels, segments */
211391
211392 for(iLvl=0; iLvl<p->nLevel; iLvl++){
211393 Fts5StructureLevel *pLvl = &p->aLevel[iLvl];
211394 sqlite3Fts5BufferAppendPrintf(pRc, pBuf,
211395 " {lvl=%d nMerge=%d nSeg=%d", iLvl, pLvl->nMerge, pLvl->nSeg
211396 );
211397 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
211398 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
211399 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d}",
211400 pSeg->iSegid, pSeg->pgnoFirst, pSeg->pgnoLast
211401 );
211402 }
211403 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
211404 }
211405}
211406
211407/*
211408** This is part of the fts5_decode() debugging aid.
211409**
211410** Arguments pBlob/nBlob contain a serialized Fts5Structure object. This
211411** function appends a human-readable representation of the same object
211412** to the buffer passed as the second argument.
211413*/
211414static void fts5DecodeStructure(
211415 int *pRc, /* IN/OUT: error code */
211416 Fts5Buffer *pBuf,
211417 const u8 *pBlob, int nBlob
211418){
211419 int rc; /* Return code */
211420 Fts5Structure *p = 0; /* Decoded structure object */
211421
211422 rc = fts5StructureDecode(pBlob, nBlob, 0, &p);
211423 if( rc!=SQLITE_OK ){
211424 *pRc = rc;
211425 return;
211426 }
211427
211428 fts5DebugStructure(pRc, pBuf, p);
211429 fts5StructureRelease(p);
211430}
211431
211432/*
211433** This is part of the fts5_decode() debugging aid.
211434**
211435** Arguments pBlob/nBlob contain an "averages" record. This function
211436** appends a human-readable representation of record to the buffer passed
211437** as the second argument.
211438*/
211439static void fts5DecodeAverages(
211440 int *pRc, /* IN/OUT: error code */
211441 Fts5Buffer *pBuf,
211442 const u8 *pBlob, int nBlob
211443){
211444 int i = 0;
211445 const char *zSpace = "";
211446
211447 while( i<nBlob ){
211448 u64 iVal;
211449 i += sqlite3Fts5GetVarint(&pBlob[i], &iVal);
211450 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "%s%d", zSpace, (int)iVal);
211451 zSpace = " ";
211452 }
211453}
211454
211455/*
211456** Buffer (a/n) is assumed to contain a list of serialized varints. Read
211457** each varint and append its string representation to buffer pBuf. Return
211458** after either the input buffer is exhausted or a 0 value is read.
211459**
211460** The return value is the number of bytes read from the input buffer.
211461*/
211462static int fts5DecodePoslist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
211463 int iOff = 0;
211464 while( iOff<n ){
211465 int iVal;
211466 iOff += fts5GetVarint32(&a[iOff], iVal);
211467 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %d", iVal);
211468 }
211469 return iOff;
211470}
211471
211472/*
211473** The start of buffer (a/n) contains the start of a doclist. The doclist
211474** may or may not finish within the buffer. This function appends a text
211475** representation of the part of the doclist that is present to buffer
211476** pBuf.
211477**
211478** The return value is the number of bytes read from the input buffer.
211479*/
211480static int fts5DecodeDoclist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
211481 i64 iDocid = 0;
211482 int iOff = 0;
211483
211484 if( n>0 ){
211485 iOff = sqlite3Fts5GetVarint(a, (u64*)&iDocid);
211486 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
211487 }
211488 while( iOff<n ){
211489 int nPos;
211490 int bDel;
211491 iOff += fts5GetPoslistSize(&a[iOff], &nPos, &bDel);
211492 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " nPos=%d%s", nPos, bDel?"*":"");
211493 iOff += fts5DecodePoslist(pRc, pBuf, &a[iOff], MIN(n-iOff, nPos));
211494 if( iOff<n ){
211495 i64 iDelta;
211496 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&iDelta);
211497 iDocid += iDelta;
211498 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
211499 }
211500 }
211501
211502 return iOff;
211503}
211504
211505/*
211506** This function is part of the fts5_decode() debugging function. It is
211507** only ever used with detail=none tables.
211508**
211509** Buffer (pData/nData) contains a doclist in the format used by detail=none
211510** tables. This function appends a human-readable version of that list to
211511** buffer pBuf.
211512**
211513** If *pRc is other than SQLITE_OK when this function is called, it is a
211514** no-op. If an OOM or other error occurs within this function, *pRc is
211515** set to an SQLite error code before returning. The final state of buffer
211516** pBuf is undefined in this case.
211517*/
211518static void fts5DecodeRowidList(
211519 int *pRc, /* IN/OUT: Error code */
211520 Fts5Buffer *pBuf, /* Buffer to append text to */
211521 const u8 *pData, int nData /* Data to decode list-of-rowids from */
211522){
211523 int i = 0;
211524 i64 iRowid = 0;
211525
211526 while( i<nData ){
211527 const char *zApp = "";
211528 u64 iVal;
211529 i += sqlite3Fts5GetVarint(&pData[i], &iVal);
211530 iRowid += iVal;
211531
211532 if( i<nData && pData[i]==0x00 ){
211533 i++;
211534 if( i<nData && pData[i]==0x00 ){
211535 i++;
211536 zApp = "+";
211537 }else{
211538 zApp = "*";
211539 }
211540 }
211541
211542 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp);
211543 }
211544}
211545
211546/*
211547** The implementation of user-defined scalar function fts5_decode().
211548*/
211549static void fts5DecodeFunction(
211550 sqlite3_context *pCtx, /* Function call context */
211551 int nArg, /* Number of args (always 2) */
211552 sqlite3_value **apVal /* Function arguments */
211553){
211554 i64 iRowid; /* Rowid for record being decoded */
211555 int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */
211556 const u8 *aBlob; int n; /* Record to decode */
211557 u8 *a = 0;
211558 Fts5Buffer s; /* Build up text to return here */
211559 int rc = SQLITE_OK; /* Return code */
211560 int nSpace = 0;
211561 int eDetailNone = (sqlite3_user_data(pCtx)!=0);
211562
211563 assert( nArg==2 );
211564 UNUSED_PARAM(nArg);
211565 memset(&s, 0, sizeof(Fts5Buffer));
211566 iRowid = sqlite3_value_int64(apVal[0]);
211567
211568 /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
211569 ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
211570 ** buffer overreads even if the record is corrupt. */
211571 n = sqlite3_value_bytes(apVal[1]);
211572 aBlob = sqlite3_value_blob(apVal[1]);
211573 nSpace = n + FTS5_DATA_ZERO_PADDING;
211574 a = (u8*)sqlite3Fts5MallocZero(&rc, nSpace);
211575 if( a==0 ) goto decode_out;
211576 memcpy(a, aBlob, n);
211577
211578
211579 fts5DecodeRowid(iRowid, &iSegid, &bDlidx, &iHeight, &iPgno);
211580
211581 fts5DebugRowid(&rc, &s, iRowid);
211582 if( bDlidx ){
211583 Fts5Data dlidx;
211584 Fts5DlidxLvl lvl;
211585
211586 dlidx.p = a;
211587 dlidx.nn = n;
211588
211589 memset(&lvl, 0, sizeof(Fts5DlidxLvl));
211590 lvl.pData = &dlidx;
211591 lvl.iLeafPgno = iPgno;
211592
211593 for(fts5DlidxLvlNext(&lvl); lvl.bEof==0; fts5DlidxLvlNext(&lvl)){
211594 sqlite3Fts5BufferAppendPrintf(&rc, &s,
211595 " %d(%lld)", lvl.iLeafPgno, lvl.iRowid
211596 );
211597 }
211598 }else if( iSegid==0 ){
211599 if( iRowid==FTS5_AVERAGES_ROWID ){
211600 fts5DecodeAverages(&rc, &s, a, n);
211601 }else{
211602 fts5DecodeStructure(&rc, &s, a, n);
211603 }
211604 }else if( eDetailNone ){
211605 Fts5Buffer term; /* Current term read from page */
211606 int szLeaf;
211607 int iPgidxOff = szLeaf = fts5GetU16(&a[2]);
211608 int iTermOff;
211609 int nKeep = 0;
211610 int iOff;
211611
211612 memset(&term, 0, sizeof(Fts5Buffer));
211613
211614 /* Decode any entries that occur before the first term. */
211615 if( szLeaf<n ){
211616 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], iTermOff);
211617 }else{
211618 iTermOff = szLeaf;
211619 }
211620 fts5DecodeRowidList(&rc, &s, &a[4], iTermOff-4);
211621
211622 iOff = iTermOff;
211623 while( iOff<szLeaf ){
211624 int nAppend;
211625
211626 /* Read the term data for the next term*/
211627 iOff += fts5GetVarint32(&a[iOff], nAppend);
211628 term.n = nKeep;
211629 fts5BufferAppendBlob(&rc, &term, nAppend, &a[iOff]);
211630 sqlite3Fts5BufferAppendPrintf(
211631 &rc, &s, " term=%.*s", term.n, (const char*)term.p
211632 );
211633 iOff += nAppend;
211634
211635 /* Figure out where the doclist for this term ends */
211636 if( iPgidxOff<n ){
211637 int nIncr;
211638 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nIncr);
211639 iTermOff += nIncr;
211640 }else{
211641 iTermOff = szLeaf;
211642 }
211643
211644 fts5DecodeRowidList(&rc, &s, &a[iOff], iTermOff-iOff);
211645 iOff = iTermOff;
211646 if( iOff<szLeaf ){
211647 iOff += fts5GetVarint32(&a[iOff], nKeep);
211648 }
211649 }
211650
211651 fts5BufferFree(&term);
211652 }else{
211653 Fts5Buffer term; /* Current term read from page */
211654 int szLeaf; /* Offset of pgidx in a[] */
211655 int iPgidxOff;
211656 int iPgidxPrev = 0; /* Previous value read from pgidx */
211657 int iTermOff = 0;
211658 int iRowidOff = 0;
211659 int iOff;
211660 int nDoclist;
211661
211662 memset(&term, 0, sizeof(Fts5Buffer));
211663
211664 if( n<4 ){
211665 sqlite3Fts5BufferSet(&rc, &s, 7, (const u8*)"corrupt");
211666 goto decode_out;
211667 }else{
211668 iRowidOff = fts5GetU16(&a[0]);
211669 iPgidxOff = szLeaf = fts5GetU16(&a[2]);
211670 if( iPgidxOff<n ){
211671 fts5GetVarint32(&a[iPgidxOff], iTermOff);
211672 }
211673 }
211674
211675 /* Decode the position list tail at the start of the page */
211676 if( iRowidOff!=0 ){
211677 iOff = iRowidOff;
211678 }else if( iTermOff!=0 ){
211679 iOff = iTermOff;
211680 }else{
211681 iOff = szLeaf;
211682 }
211683 fts5DecodePoslist(&rc, &s, &a[4], iOff-4);
211684
211685 /* Decode any more doclist data that appears on the page before the
211686 ** first term. */
211687 nDoclist = (iTermOff ? iTermOff : szLeaf) - iOff;
211688 fts5DecodeDoclist(&rc, &s, &a[iOff], nDoclist);
211689
211690 while( iPgidxOff<n ){
211691 int bFirst = (iPgidxOff==szLeaf); /* True for first term on page */
211692 int nByte; /* Bytes of data */
211693 int iEnd;
211694
211695 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nByte);
211696 iPgidxPrev += nByte;
211697 iOff = iPgidxPrev;
211698
211699 if( iPgidxOff<n ){
211700 fts5GetVarint32(&a[iPgidxOff], nByte);
211701 iEnd = iPgidxPrev + nByte;
211702 }else{
211703 iEnd = szLeaf;
211704 }
211705
211706 if( bFirst==0 ){
211707 iOff += fts5GetVarint32(&a[iOff], nByte);
211708 term.n = nByte;
211709 }
211710 iOff += fts5GetVarint32(&a[iOff], nByte);
211711 fts5BufferAppendBlob(&rc, &term, nByte, &a[iOff]);
211712 iOff += nByte;
211713
211714 sqlite3Fts5BufferAppendPrintf(
211715 &rc, &s, " term=%.*s", term.n, (const char*)term.p
211716 );
211717 iOff += fts5DecodeDoclist(&rc, &s, &a[iOff], iEnd-iOff);
211718 }
211719
211720 fts5BufferFree(&term);
211721 }
211722
211723 decode_out:
211724 sqlite3_free(a);
211725 if( rc==SQLITE_OK ){
211726 sqlite3_result_text(pCtx, (const char*)s.p, s.n, SQLITE_TRANSIENT);
211727 }else{
211728 sqlite3_result_error_code(pCtx, rc);
211729 }
211730 fts5BufferFree(&s);
211731}
211732
211733/*
211734** The implementation of user-defined scalar function fts5_rowid().
211735*/
211736static void fts5RowidFunction(
211737 sqlite3_context *pCtx, /* Function call context */
211738 int nArg, /* Number of args (always 2) */
211739 sqlite3_value **apVal /* Function arguments */
211740){
211741 const char *zArg;
211742 if( nArg==0 ){
211743 sqlite3_result_error(pCtx, "should be: fts5_rowid(subject, ....)", -1);
211744 }else{
211745 zArg = (const char*)sqlite3_value_text(apVal[0]);
211746 if( 0==sqlite3_stricmp(zArg, "segment") ){
211747 i64 iRowid;
211748 int segid, pgno;
211749 if( nArg!=3 ){
211750 sqlite3_result_error(pCtx,
211751 "should be: fts5_rowid('segment', segid, pgno))", -1
211752 );
211753 }else{
211754 segid = sqlite3_value_int(apVal[1]);
211755 pgno = sqlite3_value_int(apVal[2]);
211756 iRowid = FTS5_SEGMENT_ROWID(segid, pgno);
211757 sqlite3_result_int64(pCtx, iRowid);
211758 }
211759 }else{
211760 sqlite3_result_error(pCtx,
211761 "first arg to fts5_rowid() must be 'segment'" , -1
211762 );
211763 }
211764 }
211765}
211766
211767/*
211768** This is called as part of registering the FTS5 module with database
211769** connection db. It registers several user-defined scalar functions useful
211770** with FTS5.
211771**
211772** If successful, SQLITE_OK is returned. If an error occurs, some other
211773** SQLite error code is returned instead.
211774*/
211775static int sqlite3Fts5IndexInit(sqlite3 *db){
211776 int rc = sqlite3_create_function(
211777 db, "fts5_decode", 2, SQLITE_UTF8, 0, fts5DecodeFunction, 0, 0
211778 );
211779
211780 if( rc==SQLITE_OK ){
211781 rc = sqlite3_create_function(
211782 db, "fts5_decode_none", 2,
211783 SQLITE_UTF8, (void*)db, fts5DecodeFunction, 0, 0
211784 );
211785 }
211786
211787 if( rc==SQLITE_OK ){
211788 rc = sqlite3_create_function(
211789 db, "fts5_rowid", -1, SQLITE_UTF8, 0, fts5RowidFunction, 0, 0
211790 );
211791 }
211792 return rc;
211793}
211794
211795
211796static int sqlite3Fts5IndexReset(Fts5Index *p){
211797 assert( p->pStruct==0 || p->iStructVersion!=0 );
211798 if( fts5IndexDataVersion(p)!=p->iStructVersion ){
211799 fts5StructureInvalidate(p);
211800 }
211801 return fts5IndexReturn(p);
211802}
211803
211804/*
211805** 2014 Jun 09
211806**
211807** The author disclaims copyright to this source code. In place of
211808** a legal notice, here is a blessing:
211809**
211810** May you do good and not evil.
211811** May you find forgiveness for yourself and forgive others.
211812** May you share freely, never taking more than you give.
211813**
211814******************************************************************************
211815**
211816** This is an SQLite module implementing full-text search.
211817*/
211818
211819
211820/* #include "fts5Int.h" */
211821
211822/*
211823** This variable is set to false when running tests for which the on disk
211824** structures should not be corrupt. Otherwise, true. If it is false, extra
211825** assert() conditions in the fts5 code are activated - conditions that are
211826** only true if it is guaranteed that the fts5 database is not corrupt.
211827*/
211828SQLITE_API int sqlite3_fts5_may_be_corrupt = 1;
211829
211830
211831typedef struct Fts5Auxdata Fts5Auxdata;
211832typedef struct Fts5Auxiliary Fts5Auxiliary;
211833typedef struct Fts5Cursor Fts5Cursor;
211834typedef struct Fts5Sorter Fts5Sorter;
211835typedef struct Fts5Table Fts5Table;
211836typedef struct Fts5TokenizerModule Fts5TokenizerModule;
211837
211838/*
211839** NOTES ON TRANSACTIONS:
211840**
211841** SQLite invokes the following virtual table methods as transactions are
211842** opened and closed by the user:
211843**
211844** xBegin(): Start of a new transaction.
211845** xSync(): Initial part of two-phase commit.
211846** xCommit(): Final part of two-phase commit.
211847** xRollback(): Rollback the transaction.
211848**
211849** Anything that is required as part of a commit that may fail is performed
211850** in the xSync() callback. Current versions of SQLite ignore any errors
211851** returned by xCommit().
211852**
211853** And as sub-transactions are opened/closed:
211854**
211855** xSavepoint(int S): Open savepoint S.
211856** xRelease(int S): Commit and close savepoint S.
211857** xRollbackTo(int S): Rollback to start of savepoint S.
211858**
211859** During a write-transaction the fts5_index.c module may cache some data
211860** in-memory. It is flushed to disk whenever xSync(), xRelease() or
211861** xSavepoint() is called. And discarded whenever xRollback() or xRollbackTo()
211862** is called.
211863**
211864** Additionally, if SQLITE_DEBUG is defined, an instance of the following
211865** structure is used to record the current transaction state. This information
211866** is not required, but it is used in the assert() statements executed by
211867** function fts5CheckTransactionState() (see below).
211868*/
211869struct Fts5TransactionState {
211870 int eState; /* 0==closed, 1==open, 2==synced */
211871 int iSavepoint; /* Number of open savepoints (0 -> none) */
211872};
211873
211874/*
211875** A single object of this type is allocated when the FTS5 module is
211876** registered with a database handle. It is used to store pointers to
211877** all registered FTS5 extensions - tokenizers and auxiliary functions.
211878*/
211879struct Fts5Global {
211880 fts5_api api; /* User visible part of object (see fts5.h) */
211881 sqlite3 *db; /* Associated database connection */
211882 i64 iNextId; /* Used to allocate unique cursor ids */
211883 Fts5Auxiliary *pAux; /* First in list of all aux. functions */
211884 Fts5TokenizerModule *pTok; /* First in list of all tokenizer modules */
211885 Fts5TokenizerModule *pDfltTok; /* Default tokenizer module */
211886 Fts5Cursor *pCsr; /* First in list of all open cursors */
211887};
211888
211889/*
211890** Each auxiliary function registered with the FTS5 module is represented
211891** by an object of the following type. All such objects are stored as part
211892** of the Fts5Global.pAux list.
211893*/
211894struct Fts5Auxiliary {
211895 Fts5Global *pGlobal; /* Global context for this function */
211896 char *zFunc; /* Function name (nul-terminated) */
211897 void *pUserData; /* User-data pointer */
211898 fts5_extension_function xFunc; /* Callback function */
211899 void (*xDestroy)(void*); /* Destructor function */
211900 Fts5Auxiliary *pNext; /* Next registered auxiliary function */
211901};
211902
211903/*
211904** Each tokenizer module registered with the FTS5 module is represented
211905** by an object of the following type. All such objects are stored as part
211906** of the Fts5Global.pTok list.
211907*/
211908struct Fts5TokenizerModule {
211909 char *zName; /* Name of tokenizer */
211910 void *pUserData; /* User pointer passed to xCreate() */
211911 fts5_tokenizer x; /* Tokenizer functions */
211912 void (*xDestroy)(void*); /* Destructor function */
211913 Fts5TokenizerModule *pNext; /* Next registered tokenizer module */
211914};
211915
211916/*
211917** Virtual-table object.
211918*/
211919struct Fts5Table {
211920 sqlite3_vtab base; /* Base class used by SQLite core */
211921 Fts5Config *pConfig; /* Virtual table configuration */
211922 Fts5Index *pIndex; /* Full-text index */
211923 Fts5Storage *pStorage; /* Document store */
211924 Fts5Global *pGlobal; /* Global (connection wide) data */
211925 Fts5Cursor *pSortCsr; /* Sort data from this cursor */
211926#ifdef SQLITE_DEBUG
211927 struct Fts5TransactionState ts;
211928#endif
211929};
211930
211931struct Fts5MatchPhrase {
211932 Fts5Buffer *pPoslist; /* Pointer to current poslist */
211933 int nTerm; /* Size of phrase in terms */
211934};
211935
211936/*
211937** pStmt:
211938** SELECT rowid, <fts> FROM <fts> ORDER BY +rank;
211939**
211940** aIdx[]:
211941** There is one entry in the aIdx[] array for each phrase in the query,
211942** the value of which is the offset within aPoslist[] following the last
211943** byte of the position list for the corresponding phrase.
211944*/
211945struct Fts5Sorter {
211946 sqlite3_stmt *pStmt;
211947 i64 iRowid; /* Current rowid */
211948 const u8 *aPoslist; /* Position lists for current row */
211949 int nIdx; /* Number of entries in aIdx[] */
211950 int aIdx[1]; /* Offsets into aPoslist for current row */
211951};
211952
211953
211954/*
211955** Virtual-table cursor object.
211956**
211957** iSpecial:
211958** If this is a 'special' query (refer to function fts5SpecialMatch()),
211959** then this variable contains the result of the query.
211960**
211961** iFirstRowid, iLastRowid:
211962** These variables are only used for FTS5_PLAN_MATCH cursors. Assuming the
211963** cursor iterates in ascending order of rowids, iFirstRowid is the lower
211964** limit of rowids to return, and iLastRowid the upper. In other words, the
211965** WHERE clause in the user's query might have been:
211966**
211967** <tbl> MATCH <expr> AND rowid BETWEEN $iFirstRowid AND $iLastRowid
211968**
211969** If the cursor iterates in descending order of rowid, iFirstRowid
211970** is the upper limit (i.e. the "first" rowid visited) and iLastRowid
211971** the lower.
211972*/
211973struct Fts5Cursor {
211974 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
211975 Fts5Cursor *pNext; /* Next cursor in Fts5Cursor.pCsr list */
211976 int *aColumnSize; /* Values for xColumnSize() */
211977 i64 iCsrId; /* Cursor id */
211978
211979 /* Zero from this point onwards on cursor reset */
211980 int ePlan; /* FTS5_PLAN_XXX value */
211981 int bDesc; /* True for "ORDER BY rowid DESC" queries */
211982 i64 iFirstRowid; /* Return no rowids earlier than this */
211983 i64 iLastRowid; /* Return no rowids later than this */
211984 sqlite3_stmt *pStmt; /* Statement used to read %_content */
211985 Fts5Expr *pExpr; /* Expression for MATCH queries */
211986 Fts5Sorter *pSorter; /* Sorter for "ORDER BY rank" queries */
211987 int csrflags; /* Mask of cursor flags (see below) */
211988 i64 iSpecial; /* Result of special query */
211989
211990 /* "rank" function. Populated on demand from vtab.xColumn(). */
211991 char *zRank; /* Custom rank function */
211992 char *zRankArgs; /* Custom rank function args */
211993 Fts5Auxiliary *pRank; /* Rank callback (or NULL) */
211994 int nRankArg; /* Number of trailing arguments for rank() */
211995 sqlite3_value **apRankArg; /* Array of trailing arguments */
211996 sqlite3_stmt *pRankArgStmt; /* Origin of objects in apRankArg[] */
211997
211998 /* Auxiliary data storage */
211999 Fts5Auxiliary *pAux; /* Currently executing extension function */
212000 Fts5Auxdata *pAuxdata; /* First in linked list of saved aux-data */
212001
212002 /* Cache used by auxiliary functions xInst() and xInstCount() */
212003 Fts5PoslistReader *aInstIter; /* One for each phrase */
212004 int nInstAlloc; /* Size of aInst[] array (entries / 3) */
212005 int nInstCount; /* Number of phrase instances */
212006 int *aInst; /* 3 integers per phrase instance */
212007};
212008
212009/*
212010** Bits that make up the "idxNum" parameter passed indirectly by
212011** xBestIndex() to xFilter().
212012*/
212013#define FTS5_BI_MATCH 0x0001 /* <tbl> MATCH ? */
212014#define FTS5_BI_RANK 0x0002 /* rank MATCH ? */
212015#define FTS5_BI_ROWID_EQ 0x0004 /* rowid == ? */
212016#define FTS5_BI_ROWID_LE 0x0008 /* rowid <= ? */
212017#define FTS5_BI_ROWID_GE 0x0010 /* rowid >= ? */
212018
212019#define FTS5_BI_ORDER_RANK 0x0020
212020#define FTS5_BI_ORDER_ROWID 0x0040
212021#define FTS5_BI_ORDER_DESC 0x0080
212022
212023/*
212024** Values for Fts5Cursor.csrflags
212025*/
212026#define FTS5CSR_EOF 0x01
212027#define FTS5CSR_REQUIRE_CONTENT 0x02
212028#define FTS5CSR_REQUIRE_DOCSIZE 0x04
212029#define FTS5CSR_REQUIRE_INST 0x08
212030#define FTS5CSR_FREE_ZRANK 0x10
212031#define FTS5CSR_REQUIRE_RESEEK 0x20
212032#define FTS5CSR_REQUIRE_POSLIST 0x40
212033
212034#define BitFlagAllTest(x,y) (((x) & (y))==(y))
212035#define BitFlagTest(x,y) (((x) & (y))!=0)
212036
212037
212038/*
212039** Macros to Set(), Clear() and Test() cursor flags.
212040*/
212041#define CsrFlagSet(pCsr, flag) ((pCsr)->csrflags |= (flag))
212042#define CsrFlagClear(pCsr, flag) ((pCsr)->csrflags &= ~(flag))
212043#define CsrFlagTest(pCsr, flag) ((pCsr)->csrflags & (flag))
212044
212045struct Fts5Auxdata {
212046 Fts5Auxiliary *pAux; /* Extension to which this belongs */
212047 void *pPtr; /* Pointer value */
212048 void(*xDelete)(void*); /* Destructor */
212049 Fts5Auxdata *pNext; /* Next object in linked list */
212050};
212051
212052#ifdef SQLITE_DEBUG
212053#define FTS5_BEGIN 1
212054#define FTS5_SYNC 2
212055#define FTS5_COMMIT 3
212056#define FTS5_ROLLBACK 4
212057#define FTS5_SAVEPOINT 5
212058#define FTS5_RELEASE 6
212059#define FTS5_ROLLBACKTO 7
212060static void fts5CheckTransactionState(Fts5Table *p, int op, int iSavepoint){
212061 switch( op ){
212062 case FTS5_BEGIN:
212063 assert( p->ts.eState==0 );
212064 p->ts.eState = 1;
212065 p->ts.iSavepoint = -1;
212066 break;
212067
212068 case FTS5_SYNC:
212069 assert( p->ts.eState==1 );
212070 p->ts.eState = 2;
212071 break;
212072
212073 case FTS5_COMMIT:
212074 assert( p->ts.eState==2 );
212075 p->ts.eState = 0;
212076 break;
212077
212078 case FTS5_ROLLBACK:
212079 assert( p->ts.eState==1 || p->ts.eState==2 || p->ts.eState==0 );
212080 p->ts.eState = 0;
212081 break;
212082
212083 case FTS5_SAVEPOINT:
212084 assert( p->ts.eState==1 );
212085 assert( iSavepoint>=0 );
212086 assert( iSavepoint>=p->ts.iSavepoint );
212087 p->ts.iSavepoint = iSavepoint;
212088 break;
212089
212090 case FTS5_RELEASE:
212091 assert( p->ts.eState==1 );
212092 assert( iSavepoint>=0 );
212093 assert( iSavepoint<=p->ts.iSavepoint );
212094 p->ts.iSavepoint = iSavepoint-1;
212095 break;
212096
212097 case FTS5_ROLLBACKTO:
212098 assert( p->ts.eState==1 );
212099 assert( iSavepoint>=0 );
212100 assert( iSavepoint<=p->ts.iSavepoint );
212101 p->ts.iSavepoint = iSavepoint;
212102 break;
212103 }
212104}
212105#else
212106# define fts5CheckTransactionState(x,y,z)
212107#endif
212108
212109/*
212110** Return true if pTab is a contentless table.
212111*/
212112static int fts5IsContentless(Fts5Table *pTab){
212113 return pTab->pConfig->eContent==FTS5_CONTENT_NONE;
212114}
212115
212116/*
212117** Delete a virtual table handle allocated by fts5InitVtab().
212118*/
212119static void fts5FreeVtab(Fts5Table *pTab){
212120 if( pTab ){
212121 sqlite3Fts5IndexClose(pTab->pIndex);
212122 sqlite3Fts5StorageClose(pTab->pStorage);
212123 sqlite3Fts5ConfigFree(pTab->pConfig);
212124 sqlite3_free(pTab);
212125 }
212126}
212127
212128/*
212129** The xDisconnect() virtual table method.
212130*/
212131static int fts5DisconnectMethod(sqlite3_vtab *pVtab){
212132 fts5FreeVtab((Fts5Table*)pVtab);
212133 return SQLITE_OK;
212134}
212135
212136/*
212137** The xDestroy() virtual table method.
212138*/
212139static int fts5DestroyMethod(sqlite3_vtab *pVtab){
212140 Fts5Table *pTab = (Fts5Table*)pVtab;
212141 int rc = sqlite3Fts5DropAll(pTab->pConfig);
212142 if( rc==SQLITE_OK ){
212143 fts5FreeVtab((Fts5Table*)pVtab);
212144 }
212145 return rc;
212146}
212147
212148/*
212149** This function is the implementation of both the xConnect and xCreate
212150** methods of the FTS3 virtual table.
212151**
212152** The argv[] array contains the following:
212153**
212154** argv[0] -> module name ("fts5")
212155** argv[1] -> database name
212156** argv[2] -> table name
212157** argv[...] -> "column name" and other module argument fields.
212158*/
212159static int fts5InitVtab(
212160 int bCreate, /* True for xCreate, false for xConnect */
212161 sqlite3 *db, /* The SQLite database connection */
212162 void *pAux, /* Hash table containing tokenizers */
212163 int argc, /* Number of elements in argv array */
212164 const char * const *argv, /* xCreate/xConnect argument array */
212165 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
212166 char **pzErr /* Write any error message here */
212167){
212168 Fts5Global *pGlobal = (Fts5Global*)pAux;
212169 const char **azConfig = (const char**)argv;
212170 int rc = SQLITE_OK; /* Return code */
212171 Fts5Config *pConfig = 0; /* Results of parsing argc/argv */
212172 Fts5Table *pTab = 0; /* New virtual table object */
212173
212174 /* Allocate the new vtab object and parse the configuration */
212175 pTab = (Fts5Table*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Table));
212176 if( rc==SQLITE_OK ){
212177 rc = sqlite3Fts5ConfigParse(pGlobal, db, argc, azConfig, &pConfig, pzErr);
212178 assert( (rc==SQLITE_OK && *pzErr==0) || pConfig==0 );
212179 }
212180 if( rc==SQLITE_OK ){
212181 pTab->pConfig = pConfig;
212182 pTab->pGlobal = pGlobal;
212183 }
212184
212185 /* Open the index sub-system */
212186 if( rc==SQLITE_OK ){
212187 rc = sqlite3Fts5IndexOpen(pConfig, bCreate, &pTab->pIndex, pzErr);
212188 }
212189
212190 /* Open the storage sub-system */
212191 if( rc==SQLITE_OK ){
212192 rc = sqlite3Fts5StorageOpen(
212193 pConfig, pTab->pIndex, bCreate, &pTab->pStorage, pzErr
212194 );
212195 }
212196
212197 /* Call sqlite3_declare_vtab() */
212198 if( rc==SQLITE_OK ){
212199 rc = sqlite3Fts5ConfigDeclareVtab(pConfig);
212200 }
212201
212202 /* Load the initial configuration */
212203 if( rc==SQLITE_OK ){
212204 assert( pConfig->pzErrmsg==0 );
212205 pConfig->pzErrmsg = pzErr;
212206 rc = sqlite3Fts5IndexLoadConfig(pTab->pIndex);
212207 sqlite3Fts5IndexRollback(pTab->pIndex);
212208 pConfig->pzErrmsg = 0;
212209 }
212210
212211 if( rc!=SQLITE_OK ){
212212 fts5FreeVtab(pTab);
212213 pTab = 0;
212214 }else if( bCreate ){
212215 fts5CheckTransactionState(pTab, FTS5_BEGIN, 0);
212216 }
212217 *ppVTab = (sqlite3_vtab*)pTab;
212218 return rc;
212219}
212220
212221/*
212222** The xConnect() and xCreate() methods for the virtual table. All the
212223** work is done in function fts5InitVtab().
212224*/
212225static int fts5ConnectMethod(
212226 sqlite3 *db, /* Database connection */
212227 void *pAux, /* Pointer to tokenizer hash table */
212228 int argc, /* Number of elements in argv array */
212229 const char * const *argv, /* xCreate/xConnect argument array */
212230 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
212231 char **pzErr /* OUT: sqlite3_malloc'd error message */
212232){
212233 return fts5InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
212234}
212235static int fts5CreateMethod(
212236 sqlite3 *db, /* Database connection */
212237 void *pAux, /* Pointer to tokenizer hash table */
212238 int argc, /* Number of elements in argv array */
212239 const char * const *argv, /* xCreate/xConnect argument array */
212240 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
212241 char **pzErr /* OUT: sqlite3_malloc'd error message */
212242){
212243 return fts5InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
212244}
212245
212246/*
212247** The different query plans.
212248*/
212249#define FTS5_PLAN_MATCH 1 /* (<tbl> MATCH ?) */
212250#define FTS5_PLAN_SOURCE 2 /* A source cursor for SORTED_MATCH */
212251#define FTS5_PLAN_SPECIAL 3 /* An internal query */
212252#define FTS5_PLAN_SORTED_MATCH 4 /* (<tbl> MATCH ? ORDER BY rank) */
212253#define FTS5_PLAN_SCAN 5 /* No usable constraint */
212254#define FTS5_PLAN_ROWID 6 /* (rowid = ?) */
212255
212256/*
212257** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
212258** extension is currently being used by a version of SQLite too old to
212259** support index-info flags. In that case this function is a no-op.
212260*/
212261static void fts5SetUniqueFlag(sqlite3_index_info *pIdxInfo){
212262#if SQLITE_VERSION_NUMBER>=3008012
212263#ifndef SQLITE_CORE
212264 if( sqlite3_libversion_number()>=3008012 )
212265#endif
212266 {
212267 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
212268 }
212269#endif
212270}
212271
212272/*
212273** Implementation of the xBestIndex method for FTS5 tables. Within the
212274** WHERE constraint, it searches for the following:
212275**
212276** 1. A MATCH constraint against the special column.
212277** 2. A MATCH constraint against the "rank" column.
212278** 3. An == constraint against the rowid column.
212279** 4. A < or <= constraint against the rowid column.
212280** 5. A > or >= constraint against the rowid column.
212281**
212282** Within the ORDER BY, either:
212283**
212284** 5. ORDER BY rank [ASC|DESC]
212285** 6. ORDER BY rowid [ASC|DESC]
212286**
212287** Costs are assigned as follows:
212288**
212289** a) If an unusable MATCH operator is present in the WHERE clause, the
212290** cost is unconditionally set to 1e50 (a really big number).
212291**
212292** a) If a MATCH operator is present, the cost depends on the other
212293** constraints also present. As follows:
212294**
212295** * No other constraints: cost=1000.0
212296** * One rowid range constraint: cost=750.0
212297** * Both rowid range constraints: cost=500.0
212298** * An == rowid constraint: cost=100.0
212299**
212300** b) Otherwise, if there is no MATCH:
212301**
212302** * No other constraints: cost=1000000.0
212303** * One rowid range constraint: cost=750000.0
212304** * Both rowid range constraints: cost=250000.0
212305** * An == rowid constraint: cost=10.0
212306**
212307** Costs are not modified by the ORDER BY clause.
212308*/
212309static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
212310 Fts5Table *pTab = (Fts5Table*)pVTab;
212311 Fts5Config *pConfig = pTab->pConfig;
212312 const int nCol = pConfig->nCol;
212313 int idxFlags = 0; /* Parameter passed through to xFilter() */
212314 int bHasMatch;
212315 int iNext;
212316 int i;
212317
212318 struct Constraint {
212319 int op; /* Mask against sqlite3_index_constraint.op */
212320 int fts5op; /* FTS5 mask for idxFlags */
212321 int iCol; /* 0==rowid, 1==tbl, 2==rank */
212322 int omit; /* True to omit this if found */
212323 int iConsIndex; /* Index in pInfo->aConstraint[] */
212324 } aConstraint[] = {
212325 {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ,
212326 FTS5_BI_MATCH, 1, 1, -1},
212327 {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ,
212328 FTS5_BI_RANK, 2, 1, -1},
212329 {SQLITE_INDEX_CONSTRAINT_EQ, FTS5_BI_ROWID_EQ, 0, 0, -1},
212330 {SQLITE_INDEX_CONSTRAINT_LT|SQLITE_INDEX_CONSTRAINT_LE,
212331 FTS5_BI_ROWID_LE, 0, 0, -1},
212332 {SQLITE_INDEX_CONSTRAINT_GT|SQLITE_INDEX_CONSTRAINT_GE,
212333 FTS5_BI_ROWID_GE, 0, 0, -1},
212334 };
212335
212336 int aColMap[3];
212337 aColMap[0] = -1;
212338 aColMap[1] = nCol;
212339 aColMap[2] = nCol+1;
212340
212341 assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH );
212342 assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH );
212343 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
212344 assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH );
212345 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
212346
212347 /* Set idxFlags flags for all WHERE clause terms that will be used. */
212348 for(i=0; i<pInfo->nConstraint; i++){
212349 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
212350 int iCol = p->iColumn;
212351
212352 if( (p->op==SQLITE_INDEX_CONSTRAINT_MATCH && iCol>=0 && iCol<=nCol)
212353 || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol==nCol)
212354 ){
212355 /* A MATCH operator or equivalent */
212356 if( p->usable ){
212357 idxFlags = (idxFlags & 0xFFFF) | FTS5_BI_MATCH | (iCol << 16);
212358 aConstraint[0].iConsIndex = i;
212359 }else{
212360 /* As there exists an unusable MATCH constraint this is an
212361 ** unusable plan. Set a prohibitively high cost. */
212362 pInfo->estimatedCost = 1e50;
212363 return SQLITE_OK;
212364 }
212365 }else if( p->op<=SQLITE_INDEX_CONSTRAINT_MATCH ){
212366 int j;
212367 for(j=1; j<ArraySize(aConstraint); j++){
212368 struct Constraint *pC = &aConstraint[j];
212369 if( iCol==aColMap[pC->iCol] && (p->op & pC->op) && p->usable ){
212370 pC->iConsIndex = i;
212371 idxFlags |= pC->fts5op;
212372 }
212373 }
212374 }
212375 }
212376
212377 /* Set idxFlags flags for the ORDER BY clause */
212378 if( pInfo->nOrderBy==1 ){
212379 int iSort = pInfo->aOrderBy[0].iColumn;
212380 if( iSort==(pConfig->nCol+1) && BitFlagTest(idxFlags, FTS5_BI_MATCH) ){
212381 idxFlags |= FTS5_BI_ORDER_RANK;
212382 }else if( iSort==-1 ){
212383 idxFlags |= FTS5_BI_ORDER_ROWID;
212384 }
212385 if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){
212386 pInfo->orderByConsumed = 1;
212387 if( pInfo->aOrderBy[0].desc ){
212388 idxFlags |= FTS5_BI_ORDER_DESC;
212389 }
212390 }
212391 }
212392
212393 /* Calculate the estimated cost based on the flags set in idxFlags. */
212394 bHasMatch = BitFlagTest(idxFlags, FTS5_BI_MATCH);
212395 if( BitFlagTest(idxFlags, FTS5_BI_ROWID_EQ) ){
212396 pInfo->estimatedCost = bHasMatch ? 100.0 : 10.0;
212397 if( bHasMatch==0 ) fts5SetUniqueFlag(pInfo);
212398 }else if( BitFlagAllTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
212399 pInfo->estimatedCost = bHasMatch ? 500.0 : 250000.0;
212400 }else if( BitFlagTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
212401 pInfo->estimatedCost = bHasMatch ? 750.0 : 750000.0;
212402 }else{
212403 pInfo->estimatedCost = bHasMatch ? 1000.0 : 1000000.0;
212404 }
212405
212406 /* Assign argvIndex values to each constraint in use. */
212407 iNext = 1;
212408 for(i=0; i<ArraySize(aConstraint); i++){
212409 struct Constraint *pC = &aConstraint[i];
212410 if( pC->iConsIndex>=0 ){
212411 pInfo->aConstraintUsage[pC->iConsIndex].argvIndex = iNext++;
212412 pInfo->aConstraintUsage[pC->iConsIndex].omit = (unsigned char)pC->omit;
212413 }
212414 }
212415
212416 pInfo->idxNum = idxFlags;
212417 return SQLITE_OK;
212418}
212419
212420static int fts5NewTransaction(Fts5Table *pTab){
212421 Fts5Cursor *pCsr;
212422 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
212423 if( pCsr->base.pVtab==(sqlite3_vtab*)pTab ) return SQLITE_OK;
212424 }
212425 return sqlite3Fts5StorageReset(pTab->pStorage);
212426}
212427
212428/*
212429** Implementation of xOpen method.
212430*/
212431static int fts5OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
212432 Fts5Table *pTab = (Fts5Table*)pVTab;
212433 Fts5Config *pConfig = pTab->pConfig;
212434 Fts5Cursor *pCsr = 0; /* New cursor object */
212435 int nByte; /* Bytes of space to allocate */
212436 int rc; /* Return code */
212437
212438 rc = fts5NewTransaction(pTab);
212439 if( rc==SQLITE_OK ){
212440 nByte = sizeof(Fts5Cursor) + pConfig->nCol * sizeof(int);
212441 pCsr = (Fts5Cursor*)sqlite3_malloc(nByte);
212442 if( pCsr ){
212443 Fts5Global *pGlobal = pTab->pGlobal;
212444 memset(pCsr, 0, nByte);
212445 pCsr->aColumnSize = (int*)&pCsr[1];
212446 pCsr->pNext = pGlobal->pCsr;
212447 pGlobal->pCsr = pCsr;
212448 pCsr->iCsrId = ++pGlobal->iNextId;
212449 }else{
212450 rc = SQLITE_NOMEM;
212451 }
212452 }
212453 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
212454 return rc;
212455}
212456
212457static int fts5StmtType(Fts5Cursor *pCsr){
212458 if( pCsr->ePlan==FTS5_PLAN_SCAN ){
212459 return (pCsr->bDesc) ? FTS5_STMT_SCAN_DESC : FTS5_STMT_SCAN_ASC;
212460 }
212461 return FTS5_STMT_LOOKUP;
212462}
212463
212464/*
212465** This function is called after the cursor passed as the only argument
212466** is moved to point at a different row. It clears all cached data
212467** specific to the previous row stored by the cursor object.
212468*/
212469static void fts5CsrNewrow(Fts5Cursor *pCsr){
212470 CsrFlagSet(pCsr,
212471 FTS5CSR_REQUIRE_CONTENT
212472 | FTS5CSR_REQUIRE_DOCSIZE
212473 | FTS5CSR_REQUIRE_INST
212474 | FTS5CSR_REQUIRE_POSLIST
212475 );
212476}
212477
212478static void fts5FreeCursorComponents(Fts5Cursor *pCsr){
212479 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
212480 Fts5Auxdata *pData;
212481 Fts5Auxdata *pNext;
212482
212483 sqlite3_free(pCsr->aInstIter);
212484 sqlite3_free(pCsr->aInst);
212485 if( pCsr->pStmt ){
212486 int eStmt = fts5StmtType(pCsr);
212487 sqlite3Fts5StorageStmtRelease(pTab->pStorage, eStmt, pCsr->pStmt);
212488 }
212489 if( pCsr->pSorter ){
212490 Fts5Sorter *pSorter = pCsr->pSorter;
212491 sqlite3_finalize(pSorter->pStmt);
212492 sqlite3_free(pSorter);
212493 }
212494
212495 if( pCsr->ePlan!=FTS5_PLAN_SOURCE ){
212496 sqlite3Fts5ExprFree(pCsr->pExpr);
212497 }
212498
212499 for(pData=pCsr->pAuxdata; pData; pData=pNext){
212500 pNext = pData->pNext;
212501 if( pData->xDelete ) pData->xDelete(pData->pPtr);
212502 sqlite3_free(pData);
212503 }
212504
212505 sqlite3_finalize(pCsr->pRankArgStmt);
212506 sqlite3_free(pCsr->apRankArg);
212507
212508 if( CsrFlagTest(pCsr, FTS5CSR_FREE_ZRANK) ){
212509 sqlite3_free(pCsr->zRank);
212510 sqlite3_free(pCsr->zRankArgs);
212511 }
212512
212513 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan - (u8*)pCsr));
212514}
212515
212516
212517/*
212518** Close the cursor. For additional information see the documentation
212519** on the xClose method of the virtual table interface.
212520*/
212521static int fts5CloseMethod(sqlite3_vtab_cursor *pCursor){
212522 if( pCursor ){
212523 Fts5Table *pTab = (Fts5Table*)(pCursor->pVtab);
212524 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
212525 Fts5Cursor **pp;
212526
212527 fts5FreeCursorComponents(pCsr);
212528 /* Remove the cursor from the Fts5Global.pCsr list */
212529 for(pp=&pTab->pGlobal->pCsr; (*pp)!=pCsr; pp=&(*pp)->pNext);
212530 *pp = pCsr->pNext;
212531
212532 sqlite3_free(pCsr);
212533 }
212534 return SQLITE_OK;
212535}
212536
212537static int fts5SorterNext(Fts5Cursor *pCsr){
212538 Fts5Sorter *pSorter = pCsr->pSorter;
212539 int rc;
212540
212541 rc = sqlite3_step(pSorter->pStmt);
212542 if( rc==SQLITE_DONE ){
212543 rc = SQLITE_OK;
212544 CsrFlagSet(pCsr, FTS5CSR_EOF);
212545 }else if( rc==SQLITE_ROW ){
212546 const u8 *a;
212547 const u8 *aBlob;
212548 int nBlob;
212549 int i;
212550 int iOff = 0;
212551 rc = SQLITE_OK;
212552
212553 pSorter->iRowid = sqlite3_column_int64(pSorter->pStmt, 0);
212554 nBlob = sqlite3_column_bytes(pSorter->pStmt, 1);
212555 aBlob = a = sqlite3_column_blob(pSorter->pStmt, 1);
212556
212557 /* nBlob==0 in detail=none mode. */
212558 if( nBlob>0 ){
212559 for(i=0; i<(pSorter->nIdx-1); i++){
212560 int iVal;
212561 a += fts5GetVarint32(a, iVal);
212562 iOff += iVal;
212563 pSorter->aIdx[i] = iOff;
212564 }
212565 pSorter->aIdx[i] = &aBlob[nBlob] - a;
212566 pSorter->aPoslist = a;
212567 }
212568
212569 fts5CsrNewrow(pCsr);
212570 }
212571
212572 return rc;
212573}
212574
212575
212576/*
212577** Set the FTS5CSR_REQUIRE_RESEEK flag on all FTS5_PLAN_MATCH cursors
212578** open on table pTab.
212579*/
212580static void fts5TripCursors(Fts5Table *pTab){
212581 Fts5Cursor *pCsr;
212582 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
212583 if( pCsr->ePlan==FTS5_PLAN_MATCH
212584 && pCsr->base.pVtab==(sqlite3_vtab*)pTab
212585 ){
212586 CsrFlagSet(pCsr, FTS5CSR_REQUIRE_RESEEK);
212587 }
212588 }
212589}
212590
212591/*
212592** If the REQUIRE_RESEEK flag is set on the cursor passed as the first
212593** argument, close and reopen all Fts5IndexIter iterators that the cursor
212594** is using. Then attempt to move the cursor to a rowid equal to or laster
212595** (in the cursors sort order - ASC or DESC) than the current rowid.
212596**
212597** If the new rowid is not equal to the old, set output parameter *pbSkip
212598** to 1 before returning. Otherwise, leave it unchanged.
212599**
212600** Return SQLITE_OK if successful or if no reseek was required, or an
212601** error code if an error occurred.
212602*/
212603static int fts5CursorReseek(Fts5Cursor *pCsr, int *pbSkip){
212604 int rc = SQLITE_OK;
212605 assert( *pbSkip==0 );
212606 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_RESEEK) ){
212607 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
212608 int bDesc = pCsr->bDesc;
212609 i64 iRowid = sqlite3Fts5ExprRowid(pCsr->pExpr);
212610
212611 rc = sqlite3Fts5ExprFirst(pCsr->pExpr, pTab->pIndex, iRowid, bDesc);
212612 if( rc==SQLITE_OK && iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){
212613 *pbSkip = 1;
212614 }
212615
212616 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK);
212617 fts5CsrNewrow(pCsr);
212618 if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
212619 CsrFlagSet(pCsr, FTS5CSR_EOF);
212620 *pbSkip = 1;
212621 }
212622 }
212623 return rc;
212624}
212625
212626
212627/*
212628** Advance the cursor to the next row in the table that matches the
212629** search criteria.
212630**
212631** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
212632** even if we reach end-of-file. The fts5EofMethod() will be called
212633** subsequently to determine whether or not an EOF was hit.
212634*/
212635static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
212636 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
212637 int rc;
212638
212639 assert( (pCsr->ePlan<3)==
212640 (pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE)
212641 );
212642 assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) );
212643
212644 if( pCsr->ePlan<3 ){
212645 int bSkip = 0;
212646 if( (rc = fts5CursorReseek(pCsr, &bSkip)) || bSkip ) return rc;
212647 rc = sqlite3Fts5ExprNext(pCsr->pExpr, pCsr->iLastRowid);
212648 CsrFlagSet(pCsr, sqlite3Fts5ExprEof(pCsr->pExpr));
212649 fts5CsrNewrow(pCsr);
212650 }else{
212651 switch( pCsr->ePlan ){
212652 case FTS5_PLAN_SPECIAL: {
212653 CsrFlagSet(pCsr, FTS5CSR_EOF);
212654 rc = SQLITE_OK;
212655 break;
212656 }
212657
212658 case FTS5_PLAN_SORTED_MATCH: {
212659 rc = fts5SorterNext(pCsr);
212660 break;
212661 }
212662
212663 default:
212664 rc = sqlite3_step(pCsr->pStmt);
212665 if( rc!=SQLITE_ROW ){
212666 CsrFlagSet(pCsr, FTS5CSR_EOF);
212667 rc = sqlite3_reset(pCsr->pStmt);
212668 }else{
212669 rc = SQLITE_OK;
212670 }
212671 break;
212672 }
212673 }
212674
212675 return rc;
212676}
212677
212678
212679static int fts5PrepareStatement(
212680 sqlite3_stmt **ppStmt,
212681 Fts5Config *pConfig,
212682 const char *zFmt,
212683 ...
212684){
212685 sqlite3_stmt *pRet = 0;
212686 int rc;
212687 char *zSql;
212688 va_list ap;
212689
212690 va_start(ap, zFmt);
212691 zSql = sqlite3_vmprintf(zFmt, ap);
212692 if( zSql==0 ){
212693 rc = SQLITE_NOMEM;
212694 }else{
212695 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
212696 SQLITE_PREPARE_PERSISTENT, &pRet, 0);
212697 if( rc!=SQLITE_OK ){
212698 *pConfig->pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(pConfig->db));
212699 }
212700 sqlite3_free(zSql);
212701 }
212702
212703 va_end(ap);
212704 *ppStmt = pRet;
212705 return rc;
212706}
212707
212708static int fts5CursorFirstSorted(Fts5Table *pTab, Fts5Cursor *pCsr, int bDesc){
212709 Fts5Config *pConfig = pTab->pConfig;
212710 Fts5Sorter *pSorter;
212711 int nPhrase;
212712 int nByte;
212713 int rc;
212714 const char *zRank = pCsr->zRank;
212715 const char *zRankArgs = pCsr->zRankArgs;
212716
212717 nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
212718 nByte = sizeof(Fts5Sorter) + sizeof(int) * (nPhrase-1);
212719 pSorter = (Fts5Sorter*)sqlite3_malloc(nByte);
212720 if( pSorter==0 ) return SQLITE_NOMEM;
212721 memset(pSorter, 0, nByte);
212722 pSorter->nIdx = nPhrase;
212723
212724 /* TODO: It would be better to have some system for reusing statement
212725 ** handles here, rather than preparing a new one for each query. But that
212726 ** is not possible as SQLite reference counts the virtual table objects.
212727 ** And since the statement required here reads from this very virtual
212728 ** table, saving it creates a circular reference.
212729 **
212730 ** If SQLite a built-in statement cache, this wouldn't be a problem. */
212731 rc = fts5PrepareStatement(&pSorter->pStmt, pConfig,
212732 "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(%s%s%s) %s",
212733 pConfig->zDb, pConfig->zName, zRank, pConfig->zName,
212734 (zRankArgs ? ", " : ""),
212735 (zRankArgs ? zRankArgs : ""),
212736 bDesc ? "DESC" : "ASC"
212737 );
212738
212739 pCsr->pSorter = pSorter;
212740 if( rc==SQLITE_OK ){
212741 assert( pTab->pSortCsr==0 );
212742 pTab->pSortCsr = pCsr;
212743 rc = fts5SorterNext(pCsr);
212744 pTab->pSortCsr = 0;
212745 }
212746
212747 if( rc!=SQLITE_OK ){
212748 sqlite3_finalize(pSorter->pStmt);
212749 sqlite3_free(pSorter);
212750 pCsr->pSorter = 0;
212751 }
212752
212753 return rc;
212754}
212755
212756static int fts5CursorFirst(Fts5Table *pTab, Fts5Cursor *pCsr, int bDesc){
212757 int rc;
212758 Fts5Expr *pExpr = pCsr->pExpr;
212759 rc = sqlite3Fts5ExprFirst(pExpr, pTab->pIndex, pCsr->iFirstRowid, bDesc);
212760 if( sqlite3Fts5ExprEof(pExpr) ){
212761 CsrFlagSet(pCsr, FTS5CSR_EOF);
212762 }
212763 fts5CsrNewrow(pCsr);
212764 return rc;
212765}
212766
212767/*
212768** Process a "special" query. A special query is identified as one with a
212769** MATCH expression that begins with a '*' character. The remainder of
212770** the text passed to the MATCH operator are used as the special query
212771** parameters.
212772*/
212773static int fts5SpecialMatch(
212774 Fts5Table *pTab,
212775 Fts5Cursor *pCsr,
212776 const char *zQuery
212777){
212778 int rc = SQLITE_OK; /* Return code */
212779 const char *z = zQuery; /* Special query text */
212780 int n; /* Number of bytes in text at z */
212781
212782 while( z[0]==' ' ) z++;
212783 for(n=0; z[n] && z[n]!=' '; n++);
212784
212785 assert( pTab->base.zErrMsg==0 );
212786 pCsr->ePlan = FTS5_PLAN_SPECIAL;
212787
212788 if( 0==sqlite3_strnicmp("reads", z, n) ){
212789 pCsr->iSpecial = sqlite3Fts5IndexReads(pTab->pIndex);
212790 }
212791 else if( 0==sqlite3_strnicmp("id", z, n) ){
212792 pCsr->iSpecial = pCsr->iCsrId;
212793 }
212794 else{
212795 /* An unrecognized directive. Return an error message. */
212796 pTab->base.zErrMsg = sqlite3_mprintf("unknown special query: %.*s", n, z);
212797 rc = SQLITE_ERROR;
212798 }
212799
212800 return rc;
212801}
212802
212803/*
212804** Search for an auxiliary function named zName that can be used with table
212805** pTab. If one is found, return a pointer to the corresponding Fts5Auxiliary
212806** structure. Otherwise, if no such function exists, return NULL.
212807*/
212808static Fts5Auxiliary *fts5FindAuxiliary(Fts5Table *pTab, const char *zName){
212809 Fts5Auxiliary *pAux;
212810
212811 for(pAux=pTab->pGlobal->pAux; pAux; pAux=pAux->pNext){
212812 if( sqlite3_stricmp(zName, pAux->zFunc)==0 ) return pAux;
212813 }
212814
212815 /* No function of the specified name was found. Return 0. */
212816 return 0;
212817}
212818
212819
212820static int fts5FindRankFunction(Fts5Cursor *pCsr){
212821 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
212822 Fts5Config *pConfig = pTab->pConfig;
212823 int rc = SQLITE_OK;
212824 Fts5Auxiliary *pAux = 0;
212825 const char *zRank = pCsr->zRank;
212826 const char *zRankArgs = pCsr->zRankArgs;
212827
212828 if( zRankArgs ){
212829 char *zSql = sqlite3Fts5Mprintf(&rc, "SELECT %s", zRankArgs);
212830 if( zSql ){
212831 sqlite3_stmt *pStmt = 0;
212832 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
212833 SQLITE_PREPARE_PERSISTENT, &pStmt, 0);
212834 sqlite3_free(zSql);
212835 assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 );
212836 if( rc==SQLITE_OK ){
212837 if( SQLITE_ROW==sqlite3_step(pStmt) ){
212838 int nByte;
212839 pCsr->nRankArg = sqlite3_column_count(pStmt);
212840 nByte = sizeof(sqlite3_value*)*pCsr->nRankArg;
212841 pCsr->apRankArg = (sqlite3_value**)sqlite3Fts5MallocZero(&rc, nByte);
212842 if( rc==SQLITE_OK ){
212843 int i;
212844 for(i=0; i<pCsr->nRankArg; i++){
212845 pCsr->apRankArg[i] = sqlite3_column_value(pStmt, i);
212846 }
212847 }
212848 pCsr->pRankArgStmt = pStmt;
212849 }else{
212850 rc = sqlite3_finalize(pStmt);
212851 assert( rc!=SQLITE_OK );
212852 }
212853 }
212854 }
212855 }
212856
212857 if( rc==SQLITE_OK ){
212858 pAux = fts5FindAuxiliary(pTab, zRank);
212859 if( pAux==0 ){
212860 assert( pTab->base.zErrMsg==0 );
212861 pTab->base.zErrMsg = sqlite3_mprintf("no such function: %s", zRank);
212862 rc = SQLITE_ERROR;
212863 }
212864 }
212865
212866 pCsr->pRank = pAux;
212867 return rc;
212868}
212869
212870
212871static int fts5CursorParseRank(
212872 Fts5Config *pConfig,
212873 Fts5Cursor *pCsr,
212874 sqlite3_value *pRank
212875){
212876 int rc = SQLITE_OK;
212877 if( pRank ){
212878 const char *z = (const char*)sqlite3_value_text(pRank);
212879 char *zRank = 0;
212880 char *zRankArgs = 0;
212881
212882 if( z==0 ){
212883 if( sqlite3_value_type(pRank)==SQLITE_NULL ) rc = SQLITE_ERROR;
212884 }else{
212885 rc = sqlite3Fts5ConfigParseRank(z, &zRank, &zRankArgs);
212886 }
212887 if( rc==SQLITE_OK ){
212888 pCsr->zRank = zRank;
212889 pCsr->zRankArgs = zRankArgs;
212890 CsrFlagSet(pCsr, FTS5CSR_FREE_ZRANK);
212891 }else if( rc==SQLITE_ERROR ){
212892 pCsr->base.pVtab->zErrMsg = sqlite3_mprintf(
212893 "parse error in rank function: %s", z
212894 );
212895 }
212896 }else{
212897 if( pConfig->zRank ){
212898 pCsr->zRank = (char*)pConfig->zRank;
212899 pCsr->zRankArgs = (char*)pConfig->zRankArgs;
212900 }else{
212901 pCsr->zRank = (char*)FTS5_DEFAULT_RANK;
212902 pCsr->zRankArgs = 0;
212903 }
212904 }
212905 return rc;
212906}
212907
212908static i64 fts5GetRowidLimit(sqlite3_value *pVal, i64 iDefault){
212909 if( pVal ){
212910 int eType = sqlite3_value_numeric_type(pVal);
212911 if( eType==SQLITE_INTEGER ){
212912 return sqlite3_value_int64(pVal);
212913 }
212914 }
212915 return iDefault;
212916}
212917
212918/*
212919** This is the xFilter interface for the virtual table. See
212920** the virtual table xFilter method documentation for additional
212921** information.
212922**
212923** There are three possible query strategies:
212924**
212925** 1. Full-text search using a MATCH operator.
212926** 2. A by-rowid lookup.
212927** 3. A full-table scan.
212928*/
212929static int fts5FilterMethod(
212930 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
212931 int idxNum, /* Strategy index */
212932 const char *zUnused, /* Unused */
212933 int nVal, /* Number of elements in apVal */
212934 sqlite3_value **apVal /* Arguments for the indexing scheme */
212935){
212936 Fts5Table *pTab = (Fts5Table*)(pCursor->pVtab);
212937 Fts5Config *pConfig = pTab->pConfig;
212938 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
212939 int rc = SQLITE_OK; /* Error code */
212940 int iVal = 0; /* Counter for apVal[] */
212941 int bDesc; /* True if ORDER BY [rank|rowid] DESC */
212942 int bOrderByRank; /* True if ORDER BY rank */
212943 sqlite3_value *pMatch = 0; /* <tbl> MATCH ? expression (or NULL) */
212944 sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
212945 sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
212946 sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
212947 sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
212948 int iCol; /* Column on LHS of MATCH operator */
212949 char **pzErrmsg = pConfig->pzErrmsg;
212950
212951 UNUSED_PARAM(zUnused);
212952 UNUSED_PARAM(nVal);
212953
212954 if( pCsr->ePlan ){
212955 fts5FreeCursorComponents(pCsr);
212956 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
212957 }
212958
212959 assert( pCsr->pStmt==0 );
212960 assert( pCsr->pExpr==0 );
212961 assert( pCsr->csrflags==0 );
212962 assert( pCsr->pRank==0 );
212963 assert( pCsr->zRank==0 );
212964 assert( pCsr->zRankArgs==0 );
212965
212966 assert( pzErrmsg==0 || pzErrmsg==&pTab->base.zErrMsg );
212967 pConfig->pzErrmsg = &pTab->base.zErrMsg;
212968
212969 /* Decode the arguments passed through to this function.
212970 **
212971 ** Note: The following set of if(...) statements must be in the same
212972 ** order as the corresponding entries in the struct at the top of
212973 ** fts5BestIndexMethod(). */
212974 if( BitFlagTest(idxNum, FTS5_BI_MATCH) ) pMatch = apVal[iVal++];
212975 if( BitFlagTest(idxNum, FTS5_BI_RANK) ) pRank = apVal[iVal++];
212976 if( BitFlagTest(idxNum, FTS5_BI_ROWID_EQ) ) pRowidEq = apVal[iVal++];
212977 if( BitFlagTest(idxNum, FTS5_BI_ROWID_LE) ) pRowidLe = apVal[iVal++];
212978 if( BitFlagTest(idxNum, FTS5_BI_ROWID_GE) ) pRowidGe = apVal[iVal++];
212979 iCol = (idxNum>>16);
212980 assert( iCol>=0 && iCol<=pConfig->nCol );
212981 assert( iVal==nVal );
212982 bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK) ? 1 : 0);
212983 pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC) ? 1 : 0);
212984
212985 /* Set the cursor upper and lower rowid limits. Only some strategies
212986 ** actually use them. This is ok, as the xBestIndex() method leaves the
212987 ** sqlite3_index_constraint.omit flag clear for range constraints
212988 ** on the rowid field. */
212989 if( pRowidEq ){
212990 pRowidLe = pRowidGe = pRowidEq;
212991 }
212992 if( bDesc ){
212993 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64);
212994 pCsr->iLastRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64);
212995 }else{
212996 pCsr->iLastRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64);
212997 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64);
212998 }
212999
213000 if( pTab->pSortCsr ){
213001 /* If pSortCsr is non-NULL, then this call is being made as part of
213002 ** processing for a "... MATCH <expr> ORDER BY rank" query (ePlan is
213003 ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
213004 ** return results to the user for this query. The current cursor
213005 ** (pCursor) is used to execute the query issued by function
213006 ** fts5CursorFirstSorted() above. */
213007 assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 );
213008 assert( nVal==0 && pMatch==0 && bOrderByRank==0 && bDesc==0 );
213009 assert( pCsr->iLastRowid==LARGEST_INT64 );
213010 assert( pCsr->iFirstRowid==SMALLEST_INT64 );
213011 if( pTab->pSortCsr->bDesc ){
213012 pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
213013 pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
213014 }else{
213015 pCsr->iLastRowid = pTab->pSortCsr->iLastRowid;
213016 pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
213017 }
213018 pCsr->ePlan = FTS5_PLAN_SOURCE;
213019 pCsr->pExpr = pTab->pSortCsr->pExpr;
213020 rc = fts5CursorFirst(pTab, pCsr, bDesc);
213021 }else if( pMatch ){
213022 const char *zExpr = (const char*)sqlite3_value_text(apVal[0]);
213023 if( zExpr==0 ) zExpr = "";
213024
213025 rc = fts5CursorParseRank(pConfig, pCsr, pRank);
213026 if( rc==SQLITE_OK ){
213027 if( zExpr[0]=='*' ){
213028 /* The user has issued a query of the form "MATCH '*...'". This
213029 ** indicates that the MATCH expression is not a full text query,
213030 ** but a request for an internal parameter. */
213031 rc = fts5SpecialMatch(pTab, pCsr, &zExpr[1]);
213032 }else{
213033 char **pzErr = &pTab->base.zErrMsg;
213034 rc = sqlite3Fts5ExprNew(pConfig, iCol, zExpr, &pCsr->pExpr, pzErr);
213035 if( rc==SQLITE_OK ){
213036 if( bOrderByRank ){
213037 pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
213038 rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
213039 }else{
213040 pCsr->ePlan = FTS5_PLAN_MATCH;
213041 rc = fts5CursorFirst(pTab, pCsr, bDesc);
213042 }
213043 }
213044 }
213045 }
213046 }else if( pConfig->zContent==0 ){
213047 *pConfig->pzErrmsg = sqlite3_mprintf(
213048 "%s: table does not support scanning", pConfig->zName
213049 );
213050 rc = SQLITE_ERROR;
213051 }else{
213052 /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
213053 ** by rowid (ePlan==FTS5_PLAN_ROWID). */
213054 pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID : FTS5_PLAN_SCAN);
213055 rc = sqlite3Fts5StorageStmt(
213056 pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->base.zErrMsg
213057 );
213058 if( rc==SQLITE_OK ){
213059 if( pCsr->ePlan==FTS5_PLAN_ROWID ){
213060 sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
213061 }else{
213062 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid);
213063 sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid);
213064 }
213065 rc = fts5NextMethod(pCursor);
213066 }
213067 }
213068
213069 pConfig->pzErrmsg = pzErrmsg;
213070 return rc;
213071}
213072
213073/*
213074** This is the xEof method of the virtual table. SQLite calls this
213075** routine to find out if it has reached the end of a result set.
213076*/
213077static int fts5EofMethod(sqlite3_vtab_cursor *pCursor){
213078 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
213079 return (CsrFlagTest(pCsr, FTS5CSR_EOF) ? 1 : 0);
213080}
213081
213082/*
213083** Return the rowid that the cursor currently points to.
213084*/
213085static i64 fts5CursorRowid(Fts5Cursor *pCsr){
213086 assert( pCsr->ePlan==FTS5_PLAN_MATCH
213087 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
213088 || pCsr->ePlan==FTS5_PLAN_SOURCE
213089 );
213090 if( pCsr->pSorter ){
213091 return pCsr->pSorter->iRowid;
213092 }else{
213093 return sqlite3Fts5ExprRowid(pCsr->pExpr);
213094 }
213095}
213096
213097/*
213098** This is the xRowid method. The SQLite core calls this routine to
213099** retrieve the rowid for the current row of the result set. fts5
213100** exposes %_content.rowid as the rowid for the virtual table. The
213101** rowid should be written to *pRowid.
213102*/
213103static int fts5RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
213104 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
213105 int ePlan = pCsr->ePlan;
213106
213107 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
213108 switch( ePlan ){
213109 case FTS5_PLAN_SPECIAL:
213110 *pRowid = 0;
213111 break;
213112
213113 case FTS5_PLAN_SOURCE:
213114 case FTS5_PLAN_MATCH:
213115 case FTS5_PLAN_SORTED_MATCH:
213116 *pRowid = fts5CursorRowid(pCsr);
213117 break;
213118
213119 default:
213120 *pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
213121 break;
213122 }
213123
213124 return SQLITE_OK;
213125}
213126
213127/*
213128** If the cursor requires seeking (bSeekRequired flag is set), seek it.
213129** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
213130**
213131** If argument bErrormsg is true and an error occurs, an error message may
213132** be left in sqlite3_vtab.zErrMsg.
213133*/
213134static int fts5SeekCursor(Fts5Cursor *pCsr, int bErrormsg){
213135 int rc = SQLITE_OK;
213136
213137 /* If the cursor does not yet have a statement handle, obtain one now. */
213138 if( pCsr->pStmt==0 ){
213139 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
213140 int eStmt = fts5StmtType(pCsr);
213141 rc = sqlite3Fts5StorageStmt(
213142 pTab->pStorage, eStmt, &pCsr->pStmt, (bErrormsg?&pTab->base.zErrMsg:0)
213143 );
213144 assert( rc!=SQLITE_OK || pTab->base.zErrMsg==0 );
213145 assert( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) );
213146 }
213147
213148 if( rc==SQLITE_OK && CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) ){
213149 assert( pCsr->pExpr );
213150 sqlite3_reset(pCsr->pStmt);
213151 sqlite3_bind_int64(pCsr->pStmt, 1, fts5CursorRowid(pCsr));
213152 rc = sqlite3_step(pCsr->pStmt);
213153 if( rc==SQLITE_ROW ){
213154 rc = SQLITE_OK;
213155 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT);
213156 }else{
213157 rc = sqlite3_reset(pCsr->pStmt);
213158 if( rc==SQLITE_OK ){
213159 rc = FTS5_CORRUPT;
213160 }
213161 }
213162 }
213163 return rc;
213164}
213165
213166static void fts5SetVtabError(Fts5Table *p, const char *zFormat, ...){
213167 va_list ap; /* ... printf arguments */
213168 va_start(ap, zFormat);
213169 assert( p->base.zErrMsg==0 );
213170 p->base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
213171 va_end(ap);
213172}
213173
213174/*
213175** This function is called to handle an FTS INSERT command. In other words,
213176** an INSERT statement of the form:
213177**
213178** INSERT INTO fts(fts) VALUES($pCmd)
213179** INSERT INTO fts(fts, rank) VALUES($pCmd, $pVal)
213180**
213181** Argument pVal is the value assigned to column "fts" by the INSERT
213182** statement. This function returns SQLITE_OK if successful, or an SQLite
213183** error code if an error occurs.
213184**
213185** The commands implemented by this function are documented in the "Special
213186** INSERT Directives" section of the documentation. It should be updated if
213187** more commands are added to this function.
213188*/
213189static int fts5SpecialInsert(
213190 Fts5Table *pTab, /* Fts5 table object */
213191 const char *zCmd, /* Text inserted into table-name column */
213192 sqlite3_value *pVal /* Value inserted into rank column */
213193){
213194 Fts5Config *pConfig = pTab->pConfig;
213195 int rc = SQLITE_OK;
213196 int bError = 0;
213197
213198 if( 0==sqlite3_stricmp("delete-all", zCmd) ){
213199 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
213200 fts5SetVtabError(pTab,
213201 "'delete-all' may only be used with a "
213202 "contentless or external content fts5 table"
213203 );
213204 rc = SQLITE_ERROR;
213205 }else{
213206 rc = sqlite3Fts5StorageDeleteAll(pTab->pStorage);
213207 }
213208 }else if( 0==sqlite3_stricmp("rebuild", zCmd) ){
213209 if( pConfig->eContent==FTS5_CONTENT_NONE ){
213210 fts5SetVtabError(pTab,
213211 "'rebuild' may not be used with a contentless fts5 table"
213212 );
213213 rc = SQLITE_ERROR;
213214 }else{
213215 rc = sqlite3Fts5StorageRebuild(pTab->pStorage);
213216 }
213217 }else if( 0==sqlite3_stricmp("optimize", zCmd) ){
213218 rc = sqlite3Fts5StorageOptimize(pTab->pStorage);
213219 }else if( 0==sqlite3_stricmp("merge", zCmd) ){
213220 int nMerge = sqlite3_value_int(pVal);
213221 rc = sqlite3Fts5StorageMerge(pTab->pStorage, nMerge);
213222 }else if( 0==sqlite3_stricmp("integrity-check", zCmd) ){
213223 rc = sqlite3Fts5StorageIntegrity(pTab->pStorage);
213224#ifdef SQLITE_DEBUG
213225 }else if( 0==sqlite3_stricmp("prefix-index", zCmd) ){
213226 pConfig->bPrefixIndex = sqlite3_value_int(pVal);
213227#endif
213228 }else{
213229 rc = sqlite3Fts5IndexLoadConfig(pTab->pIndex);
213230 if( rc==SQLITE_OK ){
213231 rc = sqlite3Fts5ConfigSetValue(pTab->pConfig, zCmd, pVal, &bError);
213232 }
213233 if( rc==SQLITE_OK ){
213234 if( bError ){
213235 rc = SQLITE_ERROR;
213236 }else{
213237 rc = sqlite3Fts5StorageConfigValue(pTab->pStorage, zCmd, pVal, 0);
213238 }
213239 }
213240 }
213241 return rc;
213242}
213243
213244static int fts5SpecialDelete(
213245 Fts5Table *pTab,
213246 sqlite3_value **apVal
213247){
213248 int rc = SQLITE_OK;
213249 int eType1 = sqlite3_value_type(apVal[1]);
213250 if( eType1==SQLITE_INTEGER ){
213251 sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]);
213252 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, &apVal[2]);
213253 }
213254 return rc;
213255}
213256
213257static void fts5StorageInsert(
213258 int *pRc,
213259 Fts5Table *pTab,
213260 sqlite3_value **apVal,
213261 i64 *piRowid
213262){
213263 int rc = *pRc;
213264 if( rc==SQLITE_OK ){
213265 rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, piRowid);
213266 }
213267 if( rc==SQLITE_OK ){
213268 rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *piRowid);
213269 }
213270 *pRc = rc;
213271}
213272
213273/*
213274** This function is the implementation of the xUpdate callback used by
213275** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
213276** inserted, updated or deleted.
213277**
213278** A delete specifies a single argument - the rowid of the row to remove.
213279**
213280** Update and insert operations pass:
213281**
213282** 1. The "old" rowid, or NULL.
213283** 2. The "new" rowid.
213284** 3. Values for each of the nCol matchable columns.
213285** 4. Values for the two hidden columns (<tablename> and "rank").
213286*/
213287static int fts5UpdateMethod(
213288 sqlite3_vtab *pVtab, /* Virtual table handle */
213289 int nArg, /* Size of argument array */
213290 sqlite3_value **apVal, /* Array of arguments */
213291 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
213292){
213293 Fts5Table *pTab = (Fts5Table*)pVtab;
213294 Fts5Config *pConfig = pTab->pConfig;
213295 int eType0; /* value_type() of apVal[0] */
213296 int rc = SQLITE_OK; /* Return code */
213297
213298 /* A transaction must be open when this is called. */
213299 assert( pTab->ts.eState==1 );
213300
213301 assert( pVtab->zErrMsg==0 );
213302 assert( nArg==1 || nArg==(2+pConfig->nCol+2) );
213303 assert( nArg==1
213304 || sqlite3_value_type(apVal[1])==SQLITE_INTEGER
213305 || sqlite3_value_type(apVal[1])==SQLITE_NULL
213306 );
213307 assert( pTab->pConfig->pzErrmsg==0 );
213308 pTab->pConfig->pzErrmsg = &pTab->base.zErrMsg;
213309
213310 /* Put any active cursors into REQUIRE_SEEK state. */
213311 fts5TripCursors(pTab);
213312
213313 eType0 = sqlite3_value_type(apVal[0]);
213314 if( eType0==SQLITE_NULL
213315 && sqlite3_value_type(apVal[2+pConfig->nCol])!=SQLITE_NULL
213316 ){
213317 /* A "special" INSERT op. These are handled separately. */
213318 const char *z = (const char*)sqlite3_value_text(apVal[2+pConfig->nCol]);
213319 if( pConfig->eContent!=FTS5_CONTENT_NORMAL
213320 && 0==sqlite3_stricmp("delete", z)
213321 ){
213322 rc = fts5SpecialDelete(pTab, apVal);
213323 }else{
213324 rc = fts5SpecialInsert(pTab, z, apVal[2 + pConfig->nCol + 1]);
213325 }
213326 }else{
213327 /* A regular INSERT, UPDATE or DELETE statement. The trick here is that
213328 ** any conflict on the rowid value must be detected before any
213329 ** modifications are made to the database file. There are 4 cases:
213330 **
213331 ** 1) DELETE
213332 ** 2) UPDATE (rowid not modified)
213333 ** 3) UPDATE (rowid modified)
213334 ** 4) INSERT
213335 **
213336 ** Cases 3 and 4 may violate the rowid constraint.
213337 */
213338 int eConflict = SQLITE_ABORT;
213339 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
213340 eConflict = sqlite3_vtab_on_conflict(pConfig->db);
213341 }
213342
213343 assert( eType0==SQLITE_INTEGER || eType0==SQLITE_NULL );
213344 assert( nArg!=1 || eType0==SQLITE_INTEGER );
213345
213346 /* Filter out attempts to run UPDATE or DELETE on contentless tables.
213347 ** This is not suported. */
213348 if( eType0==SQLITE_INTEGER && fts5IsContentless(pTab) ){
213349 pTab->base.zErrMsg = sqlite3_mprintf(
213350 "cannot %s contentless fts5 table: %s",
213351 (nArg>1 ? "UPDATE" : "DELETE from"), pConfig->zName
213352 );
213353 rc = SQLITE_ERROR;
213354 }
213355
213356 /* DELETE */
213357 else if( nArg==1 ){
213358 i64 iDel = sqlite3_value_int64(apVal[0]); /* Rowid to delete */
213359 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, 0);
213360 }
213361
213362 /* INSERT */
213363 else if( eType0!=SQLITE_INTEGER ){
213364 /* If this is a REPLACE, first remove the current entry (if any) */
213365 if( eConflict==SQLITE_REPLACE
213366 && sqlite3_value_type(apVal[1])==SQLITE_INTEGER
213367 ){
213368 i64 iNew = sqlite3_value_int64(apVal[1]); /* Rowid to delete */
213369 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
213370 }
213371 fts5StorageInsert(&rc, pTab, apVal, pRowid);
213372 }
213373
213374 /* UPDATE */
213375 else{
213376 i64 iOld = sqlite3_value_int64(apVal[0]); /* Old rowid */
213377 i64 iNew = sqlite3_value_int64(apVal[1]); /* New rowid */
213378 if( iOld!=iNew ){
213379 if( eConflict==SQLITE_REPLACE ){
213380 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
213381 if( rc==SQLITE_OK ){
213382 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
213383 }
213384 fts5StorageInsert(&rc, pTab, apVal, pRowid);
213385 }else{
213386 rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, pRowid);
213387 if( rc==SQLITE_OK ){
213388 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
213389 }
213390 if( rc==SQLITE_OK ){
213391 rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *pRowid);
213392 }
213393 }
213394 }else{
213395 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
213396 fts5StorageInsert(&rc, pTab, apVal, pRowid);
213397 }
213398 }
213399 }
213400
213401 pTab->pConfig->pzErrmsg = 0;
213402 return rc;
213403}
213404
213405/*
213406** Implementation of xSync() method.
213407*/
213408static int fts5SyncMethod(sqlite3_vtab *pVtab){
213409 int rc;
213410 Fts5Table *pTab = (Fts5Table*)pVtab;
213411 fts5CheckTransactionState(pTab, FTS5_SYNC, 0);
213412 pTab->pConfig->pzErrmsg = &pTab->base.zErrMsg;
213413 fts5TripCursors(pTab);
213414 rc = sqlite3Fts5StorageSync(pTab->pStorage);
213415 pTab->pConfig->pzErrmsg = 0;
213416 return rc;
213417}
213418
213419/*
213420** Implementation of xBegin() method.
213421*/
213422static int fts5BeginMethod(sqlite3_vtab *pVtab){
213423 fts5CheckTransactionState((Fts5Table*)pVtab, FTS5_BEGIN, 0);
213424 fts5NewTransaction((Fts5Table*)pVtab);
213425 return SQLITE_OK;
213426}
213427
213428/*
213429** Implementation of xCommit() method. This is a no-op. The contents of
213430** the pending-terms hash-table have already been flushed into the database
213431** by fts5SyncMethod().
213432*/
213433static int fts5CommitMethod(sqlite3_vtab *pVtab){
213434 UNUSED_PARAM(pVtab); /* Call below is a no-op for NDEBUG builds */
213435 fts5CheckTransactionState((Fts5Table*)pVtab, FTS5_COMMIT, 0);
213436 return SQLITE_OK;
213437}
213438
213439/*
213440** Implementation of xRollback(). Discard the contents of the pending-terms
213441** hash-table. Any changes made to the database are reverted by SQLite.
213442*/
213443static int fts5RollbackMethod(sqlite3_vtab *pVtab){
213444 int rc;
213445 Fts5Table *pTab = (Fts5Table*)pVtab;
213446 fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0);
213447 rc = sqlite3Fts5StorageRollback(pTab->pStorage);
213448 return rc;
213449}
213450
213451static int fts5CsrPoslist(Fts5Cursor*, int, const u8**, int*);
213452
213453static void *fts5ApiUserData(Fts5Context *pCtx){
213454 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213455 return pCsr->pAux->pUserData;
213456}
213457
213458static int fts5ApiColumnCount(Fts5Context *pCtx){
213459 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213460 return ((Fts5Table*)(pCsr->base.pVtab))->pConfig->nCol;
213461}
213462
213463static int fts5ApiColumnTotalSize(
213464 Fts5Context *pCtx,
213465 int iCol,
213466 sqlite3_int64 *pnToken
213467){
213468 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213469 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
213470 return sqlite3Fts5StorageSize(pTab->pStorage, iCol, pnToken);
213471}
213472
213473static int fts5ApiRowCount(Fts5Context *pCtx, i64 *pnRow){
213474 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213475 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
213476 return sqlite3Fts5StorageRowCount(pTab->pStorage, pnRow);
213477}
213478
213479static int fts5ApiTokenize(
213480 Fts5Context *pCtx,
213481 const char *pText, int nText,
213482 void *pUserData,
213483 int (*xToken)(void*, int, const char*, int, int, int)
213484){
213485 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213486 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
213487 return sqlite3Fts5Tokenize(
213488 pTab->pConfig, FTS5_TOKENIZE_AUX, pText, nText, pUserData, xToken
213489 );
213490}
213491
213492static int fts5ApiPhraseCount(Fts5Context *pCtx){
213493 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213494 return sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
213495}
213496
213497static int fts5ApiPhraseSize(Fts5Context *pCtx, int iPhrase){
213498 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213499 return sqlite3Fts5ExprPhraseSize(pCsr->pExpr, iPhrase);
213500}
213501
213502static int fts5ApiColumnText(
213503 Fts5Context *pCtx,
213504 int iCol,
213505 const char **pz,
213506 int *pn
213507){
213508 int rc = SQLITE_OK;
213509 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213510 if( fts5IsContentless((Fts5Table*)(pCsr->base.pVtab)) ){
213511 *pz = 0;
213512 *pn = 0;
213513 }else{
213514 rc = fts5SeekCursor(pCsr, 0);
213515 if( rc==SQLITE_OK ){
213516 *pz = (const char*)sqlite3_column_text(pCsr->pStmt, iCol+1);
213517 *pn = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
213518 }
213519 }
213520 return rc;
213521}
213522
213523static int fts5CsrPoslist(
213524 Fts5Cursor *pCsr,
213525 int iPhrase,
213526 const u8 **pa,
213527 int *pn
213528){
213529 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
213530 int rc = SQLITE_OK;
213531 int bLive = (pCsr->pSorter==0);
213532
213533 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){
213534
213535 if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
213536 Fts5PoslistPopulator *aPopulator;
213537 int i;
213538 aPopulator = sqlite3Fts5ExprClearPoslists(pCsr->pExpr, bLive);
213539 if( aPopulator==0 ) rc = SQLITE_NOMEM;
213540 for(i=0; i<pConfig->nCol && rc==SQLITE_OK; i++){
213541 int n; const char *z;
213542 rc = fts5ApiColumnText((Fts5Context*)pCsr, i, &z, &n);
213543 if( rc==SQLITE_OK ){
213544 rc = sqlite3Fts5ExprPopulatePoslists(
213545 pConfig, pCsr->pExpr, aPopulator, i, z, n
213546 );
213547 }
213548 }
213549 sqlite3_free(aPopulator);
213550
213551 if( pCsr->pSorter ){
213552 sqlite3Fts5ExprCheckPoslists(pCsr->pExpr, pCsr->pSorter->iRowid);
213553 }
213554 }
213555 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST);
213556 }
213557
213558 if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){
213559 Fts5Sorter *pSorter = pCsr->pSorter;
213560 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
213561 *pn = pSorter->aIdx[iPhrase] - i1;
213562 *pa = &pSorter->aPoslist[i1];
213563 }else{
213564 *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa);
213565 }
213566
213567 return rc;
213568}
213569
213570/*
213571** Ensure that the Fts5Cursor.nInstCount and aInst[] variables are populated
213572** correctly for the current view. Return SQLITE_OK if successful, or an
213573** SQLite error code otherwise.
213574*/
213575static int fts5CacheInstArray(Fts5Cursor *pCsr){
213576 int rc = SQLITE_OK;
213577 Fts5PoslistReader *aIter; /* One iterator for each phrase */
213578 int nIter; /* Number of iterators/phrases */
213579
213580 nIter = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
213581 if( pCsr->aInstIter==0 ){
213582 int nByte = sizeof(Fts5PoslistReader) * nIter;
213583 pCsr->aInstIter = (Fts5PoslistReader*)sqlite3Fts5MallocZero(&rc, nByte);
213584 }
213585 aIter = pCsr->aInstIter;
213586
213587 if( aIter ){
213588 int nInst = 0; /* Number instances seen so far */
213589 int i;
213590
213591 /* Initialize all iterators */
213592 for(i=0; i<nIter && rc==SQLITE_OK; i++){
213593 const u8 *a;
213594 int n;
213595 rc = fts5CsrPoslist(pCsr, i, &a, &n);
213596 if( rc==SQLITE_OK ){
213597 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
213598 }
213599 }
213600
213601 if( rc==SQLITE_OK ){
213602 while( 1 ){
213603 int *aInst;
213604 int iBest = -1;
213605 for(i=0; i<nIter; i++){
213606 if( (aIter[i].bEof==0)
213607 && (iBest<0 || aIter[i].iPos<aIter[iBest].iPos)
213608 ){
213609 iBest = i;
213610 }
213611 }
213612 if( iBest<0 ) break;
213613
213614 nInst++;
213615 if( nInst>=pCsr->nInstAlloc ){
213616 pCsr->nInstAlloc = pCsr->nInstAlloc ? pCsr->nInstAlloc*2 : 32;
213617 aInst = (int*)sqlite3_realloc(
213618 pCsr->aInst, pCsr->nInstAlloc*sizeof(int)*3
213619 );
213620 if( aInst ){
213621 pCsr->aInst = aInst;
213622 }else{
213623 rc = SQLITE_NOMEM;
213624 break;
213625 }
213626 }
213627
213628 aInst = &pCsr->aInst[3 * (nInst-1)];
213629 aInst[0] = iBest;
213630 aInst[1] = FTS5_POS2COLUMN(aIter[iBest].iPos);
213631 aInst[2] = FTS5_POS2OFFSET(aIter[iBest].iPos);
213632 sqlite3Fts5PoslistReaderNext(&aIter[iBest]);
213633 }
213634 }
213635
213636 pCsr->nInstCount = nInst;
213637 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_INST);
213638 }
213639 return rc;
213640}
213641
213642static int fts5ApiInstCount(Fts5Context *pCtx, int *pnInst){
213643 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213644 int rc = SQLITE_OK;
213645 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
213646 || SQLITE_OK==(rc = fts5CacheInstArray(pCsr)) ){
213647 *pnInst = pCsr->nInstCount;
213648 }
213649 return rc;
213650}
213651
213652static int fts5ApiInst(
213653 Fts5Context *pCtx,
213654 int iIdx,
213655 int *piPhrase,
213656 int *piCol,
213657 int *piOff
213658){
213659 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213660 int rc = SQLITE_OK;
213661 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
213662 || SQLITE_OK==(rc = fts5CacheInstArray(pCsr))
213663 ){
213664 if( iIdx<0 || iIdx>=pCsr->nInstCount ){
213665 rc = SQLITE_RANGE;
213666#if 0
213667 }else if( fts5IsOffsetless((Fts5Table*)pCsr->base.pVtab) ){
213668 *piPhrase = pCsr->aInst[iIdx*3];
213669 *piCol = pCsr->aInst[iIdx*3 + 2];
213670 *piOff = -1;
213671#endif
213672 }else{
213673 *piPhrase = pCsr->aInst[iIdx*3];
213674 *piCol = pCsr->aInst[iIdx*3 + 1];
213675 *piOff = pCsr->aInst[iIdx*3 + 2];
213676 }
213677 }
213678 return rc;
213679}
213680
213681static sqlite3_int64 fts5ApiRowid(Fts5Context *pCtx){
213682 return fts5CursorRowid((Fts5Cursor*)pCtx);
213683}
213684
213685static int fts5ColumnSizeCb(
213686 void *pContext, /* Pointer to int */
213687 int tflags,
213688 const char *pUnused, /* Buffer containing token */
213689 int nUnused, /* Size of token in bytes */
213690 int iUnused1, /* Start offset of token */
213691 int iUnused2 /* End offset of token */
213692){
213693 int *pCnt = (int*)pContext;
213694 UNUSED_PARAM2(pUnused, nUnused);
213695 UNUSED_PARAM2(iUnused1, iUnused2);
213696 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
213697 (*pCnt)++;
213698 }
213699 return SQLITE_OK;
213700}
213701
213702static int fts5ApiColumnSize(Fts5Context *pCtx, int iCol, int *pnToken){
213703 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213704 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
213705 Fts5Config *pConfig = pTab->pConfig;
213706 int rc = SQLITE_OK;
213707
213708 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_DOCSIZE) ){
213709 if( pConfig->bColumnsize ){
213710 i64 iRowid = fts5CursorRowid(pCsr);
213711 rc = sqlite3Fts5StorageDocsize(pTab->pStorage, iRowid, pCsr->aColumnSize);
213712 }else if( pConfig->zContent==0 ){
213713 int i;
213714 for(i=0; i<pConfig->nCol; i++){
213715 if( pConfig->abUnindexed[i]==0 ){
213716 pCsr->aColumnSize[i] = -1;
213717 }
213718 }
213719 }else{
213720 int i;
213721 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
213722 if( pConfig->abUnindexed[i]==0 ){
213723 const char *z; int n;
213724 void *p = (void*)(&pCsr->aColumnSize[i]);
213725 pCsr->aColumnSize[i] = 0;
213726 rc = fts5ApiColumnText(pCtx, i, &z, &n);
213727 if( rc==SQLITE_OK ){
213728 rc = sqlite3Fts5Tokenize(
213729 pConfig, FTS5_TOKENIZE_AUX, z, n, p, fts5ColumnSizeCb
213730 );
213731 }
213732 }
213733 }
213734 }
213735 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_DOCSIZE);
213736 }
213737 if( iCol<0 ){
213738 int i;
213739 *pnToken = 0;
213740 for(i=0; i<pConfig->nCol; i++){
213741 *pnToken += pCsr->aColumnSize[i];
213742 }
213743 }else if( iCol<pConfig->nCol ){
213744 *pnToken = pCsr->aColumnSize[iCol];
213745 }else{
213746 *pnToken = 0;
213747 rc = SQLITE_RANGE;
213748 }
213749 return rc;
213750}
213751
213752/*
213753** Implementation of the xSetAuxdata() method.
213754*/
213755static int fts5ApiSetAuxdata(
213756 Fts5Context *pCtx, /* Fts5 context */
213757 void *pPtr, /* Pointer to save as auxdata */
213758 void(*xDelete)(void*) /* Destructor for pPtr (or NULL) */
213759){
213760 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213761 Fts5Auxdata *pData;
213762
213763 /* Search through the cursors list of Fts5Auxdata objects for one that
213764 ** corresponds to the currently executing auxiliary function. */
213765 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
213766 if( pData->pAux==pCsr->pAux ) break;
213767 }
213768
213769 if( pData ){
213770 if( pData->xDelete ){
213771 pData->xDelete(pData->pPtr);
213772 }
213773 }else{
213774 int rc = SQLITE_OK;
213775 pData = (Fts5Auxdata*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Auxdata));
213776 if( pData==0 ){
213777 if( xDelete ) xDelete(pPtr);
213778 return rc;
213779 }
213780 pData->pAux = pCsr->pAux;
213781 pData->pNext = pCsr->pAuxdata;
213782 pCsr->pAuxdata = pData;
213783 }
213784
213785 pData->xDelete = xDelete;
213786 pData->pPtr = pPtr;
213787 return SQLITE_OK;
213788}
213789
213790static void *fts5ApiGetAuxdata(Fts5Context *pCtx, int bClear){
213791 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213792 Fts5Auxdata *pData;
213793 void *pRet = 0;
213794
213795 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
213796 if( pData->pAux==pCsr->pAux ) break;
213797 }
213798
213799 if( pData ){
213800 pRet = pData->pPtr;
213801 if( bClear ){
213802 pData->pPtr = 0;
213803 pData->xDelete = 0;
213804 }
213805 }
213806
213807 return pRet;
213808}
213809
213810static void fts5ApiPhraseNext(
213811 Fts5Context *pUnused,
213812 Fts5PhraseIter *pIter,
213813 int *piCol, int *piOff
213814){
213815 UNUSED_PARAM(pUnused);
213816 if( pIter->a>=pIter->b ){
213817 *piCol = -1;
213818 *piOff = -1;
213819 }else{
213820 int iVal;
213821 pIter->a += fts5GetVarint32(pIter->a, iVal);
213822 if( iVal==1 ){
213823 pIter->a += fts5GetVarint32(pIter->a, iVal);
213824 *piCol = iVal;
213825 *piOff = 0;
213826 pIter->a += fts5GetVarint32(pIter->a, iVal);
213827 }
213828 *piOff += (iVal-2);
213829 }
213830}
213831
213832static int fts5ApiPhraseFirst(
213833 Fts5Context *pCtx,
213834 int iPhrase,
213835 Fts5PhraseIter *pIter,
213836 int *piCol, int *piOff
213837){
213838 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213839 int n;
213840 int rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
213841 if( rc==SQLITE_OK ){
213842 pIter->b = &pIter->a[n];
213843 *piCol = 0;
213844 *piOff = 0;
213845 fts5ApiPhraseNext(pCtx, pIter, piCol, piOff);
213846 }
213847 return rc;
213848}
213849
213850static void fts5ApiPhraseNextColumn(
213851 Fts5Context *pCtx,
213852 Fts5PhraseIter *pIter,
213853 int *piCol
213854){
213855 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213856 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
213857
213858 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
213859 if( pIter->a>=pIter->b ){
213860 *piCol = -1;
213861 }else{
213862 int iIncr;
213863 pIter->a += fts5GetVarint32(&pIter->a[0], iIncr);
213864 *piCol += (iIncr-2);
213865 }
213866 }else{
213867 while( 1 ){
213868 int dummy;
213869 if( pIter->a>=pIter->b ){
213870 *piCol = -1;
213871 return;
213872 }
213873 if( pIter->a[0]==0x01 ) break;
213874 pIter->a += fts5GetVarint32(pIter->a, dummy);
213875 }
213876 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
213877 }
213878}
213879
213880static int fts5ApiPhraseFirstColumn(
213881 Fts5Context *pCtx,
213882 int iPhrase,
213883 Fts5PhraseIter *pIter,
213884 int *piCol
213885){
213886 int rc = SQLITE_OK;
213887 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213888 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
213889
213890 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
213891 Fts5Sorter *pSorter = pCsr->pSorter;
213892 int n;
213893 if( pSorter ){
213894 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
213895 n = pSorter->aIdx[iPhrase] - i1;
213896 pIter->a = &pSorter->aPoslist[i1];
213897 }else{
213898 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, iPhrase, &pIter->a, &n);
213899 }
213900 if( rc==SQLITE_OK ){
213901 pIter->b = &pIter->a[n];
213902 *piCol = 0;
213903 fts5ApiPhraseNextColumn(pCtx, pIter, piCol);
213904 }
213905 }else{
213906 int n;
213907 rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
213908 if( rc==SQLITE_OK ){
213909 pIter->b = &pIter->a[n];
213910 if( n<=0 ){
213911 *piCol = -1;
213912 }else if( pIter->a[0]==0x01 ){
213913 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
213914 }else{
213915 *piCol = 0;
213916 }
213917 }
213918 }
213919
213920 return rc;
213921}
213922
213923
213924static int fts5ApiQueryPhrase(Fts5Context*, int, void*,
213925 int(*)(const Fts5ExtensionApi*, Fts5Context*, void*)
213926);
213927
213928static const Fts5ExtensionApi sFts5Api = {
213929 2, /* iVersion */
213930 fts5ApiUserData,
213931 fts5ApiColumnCount,
213932 fts5ApiRowCount,
213933 fts5ApiColumnTotalSize,
213934 fts5ApiTokenize,
213935 fts5ApiPhraseCount,
213936 fts5ApiPhraseSize,
213937 fts5ApiInstCount,
213938 fts5ApiInst,
213939 fts5ApiRowid,
213940 fts5ApiColumnText,
213941 fts5ApiColumnSize,
213942 fts5ApiQueryPhrase,
213943 fts5ApiSetAuxdata,
213944 fts5ApiGetAuxdata,
213945 fts5ApiPhraseFirst,
213946 fts5ApiPhraseNext,
213947 fts5ApiPhraseFirstColumn,
213948 fts5ApiPhraseNextColumn,
213949};
213950
213951/*
213952** Implementation of API function xQueryPhrase().
213953*/
213954static int fts5ApiQueryPhrase(
213955 Fts5Context *pCtx,
213956 int iPhrase,
213957 void *pUserData,
213958 int(*xCallback)(const Fts5ExtensionApi*, Fts5Context*, void*)
213959){
213960 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
213961 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
213962 int rc;
213963 Fts5Cursor *pNew = 0;
213964
213965 rc = fts5OpenMethod(pCsr->base.pVtab, (sqlite3_vtab_cursor**)&pNew);
213966 if( rc==SQLITE_OK ){
213967 pNew->ePlan = FTS5_PLAN_MATCH;
213968 pNew->iFirstRowid = SMALLEST_INT64;
213969 pNew->iLastRowid = LARGEST_INT64;
213970 pNew->base.pVtab = (sqlite3_vtab*)pTab;
213971 rc = sqlite3Fts5ExprClonePhrase(pCsr->pExpr, iPhrase, &pNew->pExpr);
213972 }
213973
213974 if( rc==SQLITE_OK ){
213975 for(rc = fts5CursorFirst(pTab, pNew, 0);
213976 rc==SQLITE_OK && CsrFlagTest(pNew, FTS5CSR_EOF)==0;
213977 rc = fts5NextMethod((sqlite3_vtab_cursor*)pNew)
213978 ){
213979 rc = xCallback(&sFts5Api, (Fts5Context*)pNew, pUserData);
213980 if( rc!=SQLITE_OK ){
213981 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
213982 break;
213983 }
213984 }
213985 }
213986
213987 fts5CloseMethod((sqlite3_vtab_cursor*)pNew);
213988 return rc;
213989}
213990
213991static void fts5ApiInvoke(
213992 Fts5Auxiliary *pAux,
213993 Fts5Cursor *pCsr,
213994 sqlite3_context *context,
213995 int argc,
213996 sqlite3_value **argv
213997){
213998 assert( pCsr->pAux==0 );
213999 pCsr->pAux = pAux;
214000 pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc, argv);
214001 pCsr->pAux = 0;
214002}
214003
214004static Fts5Cursor *fts5CursorFromCsrid(Fts5Global *pGlobal, i64 iCsrId){
214005 Fts5Cursor *pCsr;
214006 for(pCsr=pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
214007 if( pCsr->iCsrId==iCsrId ) break;
214008 }
214009 return pCsr;
214010}
214011
214012static void fts5ApiCallback(
214013 sqlite3_context *context,
214014 int argc,
214015 sqlite3_value **argv
214016){
214017
214018 Fts5Auxiliary *pAux;
214019 Fts5Cursor *pCsr;
214020 i64 iCsrId;
214021
214022 assert( argc>=1 );
214023 pAux = (Fts5Auxiliary*)sqlite3_user_data(context);
214024 iCsrId = sqlite3_value_int64(argv[0]);
214025
214026 pCsr = fts5CursorFromCsrid(pAux->pGlobal, iCsrId);
214027 if( pCsr==0 ){
214028 char *zErr = sqlite3_mprintf("no such cursor: %lld", iCsrId);
214029 sqlite3_result_error(context, zErr, -1);
214030 sqlite3_free(zErr);
214031 }else{
214032 fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]);
214033 }
214034}
214035
214036
214037/*
214038** Given cursor id iId, return a pointer to the corresponding Fts5Index
214039** object. Or NULL If the cursor id does not exist.
214040**
214041** If successful, set *ppConfig to point to the associated config object
214042** before returning.
214043*/
214044static Fts5Index *sqlite3Fts5IndexFromCsrid(
214045 Fts5Global *pGlobal, /* FTS5 global context for db handle */
214046 i64 iCsrId, /* Id of cursor to find */
214047 Fts5Config **ppConfig /* OUT: Configuration object */
214048){
214049 Fts5Cursor *pCsr;
214050 Fts5Table *pTab;
214051
214052 pCsr = fts5CursorFromCsrid(pGlobal, iCsrId);
214053 pTab = (Fts5Table*)pCsr->base.pVtab;
214054 *ppConfig = pTab->pConfig;
214055
214056 return pTab->pIndex;
214057}
214058
214059/*
214060** Return a "position-list blob" corresponding to the current position of
214061** cursor pCsr via sqlite3_result_blob(). A position-list blob contains
214062** the current position-list for each phrase in the query associated with
214063** cursor pCsr.
214064**
214065** A position-list blob begins with (nPhrase-1) varints, where nPhrase is
214066** the number of phrases in the query. Following the varints are the
214067** concatenated position lists for each phrase, in order.
214068**
214069** The first varint (if it exists) contains the size of the position list
214070** for phrase 0. The second (same disclaimer) contains the size of position
214071** list 1. And so on. There is no size field for the final position list,
214072** as it can be derived from the total size of the blob.
214073*/
214074static int fts5PoslistBlob(sqlite3_context *pCtx, Fts5Cursor *pCsr){
214075 int i;
214076 int rc = SQLITE_OK;
214077 int nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
214078 Fts5Buffer val;
214079
214080 memset(&val, 0, sizeof(Fts5Buffer));
214081 switch( ((Fts5Table*)(pCsr->base.pVtab))->pConfig->eDetail ){
214082 case FTS5_DETAIL_FULL:
214083
214084 /* Append the varints */
214085 for(i=0; i<(nPhrase-1); i++){
214086 const u8 *dummy;
214087 int nByte = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &dummy);
214088 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
214089 }
214090
214091 /* Append the position lists */
214092 for(i=0; i<nPhrase; i++){
214093 const u8 *pPoslist;
214094 int nPoslist;
214095 nPoslist = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &pPoslist);
214096 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
214097 }
214098 break;
214099
214100 case FTS5_DETAIL_COLUMNS:
214101
214102 /* Append the varints */
214103 for(i=0; rc==SQLITE_OK && i<(nPhrase-1); i++){
214104 const u8 *dummy;
214105 int nByte;
214106 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &dummy, &nByte);
214107 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
214108 }
214109
214110 /* Append the position lists */
214111 for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
214112 const u8 *pPoslist;
214113 int nPoslist;
214114 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &pPoslist, &nPoslist);
214115 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
214116 }
214117 break;
214118
214119 default:
214120 break;
214121 }
214122
214123 sqlite3_result_blob(pCtx, val.p, val.n, sqlite3_free);
214124 return rc;
214125}
214126
214127/*
214128** This is the xColumn method, called by SQLite to request a value from
214129** the row that the supplied cursor currently points to.
214130*/
214131static int fts5ColumnMethod(
214132 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
214133 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
214134 int iCol /* Index of column to read value from */
214135){
214136 Fts5Table *pTab = (Fts5Table*)(pCursor->pVtab);
214137 Fts5Config *pConfig = pTab->pConfig;
214138 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
214139 int rc = SQLITE_OK;
214140
214141 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
214142
214143 if( pCsr->ePlan==FTS5_PLAN_SPECIAL ){
214144 if( iCol==pConfig->nCol ){
214145 sqlite3_result_int64(pCtx, pCsr->iSpecial);
214146 }
214147 }else
214148
214149 if( iCol==pConfig->nCol ){
214150 /* User is requesting the value of the special column with the same name
214151 ** as the table. Return the cursor integer id number. This value is only
214152 ** useful in that it may be passed as the first argument to an FTS5
214153 ** auxiliary function. */
214154 sqlite3_result_int64(pCtx, pCsr->iCsrId);
214155 }else if( iCol==pConfig->nCol+1 ){
214156
214157 /* The value of the "rank" column. */
214158 if( pCsr->ePlan==FTS5_PLAN_SOURCE ){
214159 fts5PoslistBlob(pCtx, pCsr);
214160 }else if(
214161 pCsr->ePlan==FTS5_PLAN_MATCH
214162 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
214163 ){
214164 if( pCsr->pRank || SQLITE_OK==(rc = fts5FindRankFunction(pCsr)) ){
214165 fts5ApiInvoke(pCsr->pRank, pCsr, pCtx, pCsr->nRankArg, pCsr->apRankArg);
214166 }
214167 }
214168 }else if( !fts5IsContentless(pTab) ){
214169 rc = fts5SeekCursor(pCsr, 1);
214170 if( rc==SQLITE_OK ){
214171 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
214172 }
214173 }
214174 return rc;
214175}
214176
214177
214178/*
214179** This routine implements the xFindFunction method for the FTS3
214180** virtual table.
214181*/
214182static int fts5FindFunctionMethod(
214183 sqlite3_vtab *pVtab, /* Virtual table handle */
214184 int nUnused, /* Number of SQL function arguments */
214185 const char *zName, /* Name of SQL function */
214186 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
214187 void **ppArg /* OUT: User data for *pxFunc */
214188){
214189 Fts5Table *pTab = (Fts5Table*)pVtab;
214190 Fts5Auxiliary *pAux;
214191
214192 UNUSED_PARAM(nUnused);
214193 pAux = fts5FindAuxiliary(pTab, zName);
214194 if( pAux ){
214195 *pxFunc = fts5ApiCallback;
214196 *ppArg = (void*)pAux;
214197 return 1;
214198 }
214199
214200 /* No function of the specified name was found. Return 0. */
214201 return 0;
214202}
214203
214204/*
214205** Implementation of FTS5 xRename method. Rename an fts5 table.
214206*/
214207static int fts5RenameMethod(
214208 sqlite3_vtab *pVtab, /* Virtual table handle */
214209 const char *zName /* New name of table */
214210){
214211 Fts5Table *pTab = (Fts5Table*)pVtab;
214212 return sqlite3Fts5StorageRename(pTab->pStorage, zName);
214213}
214214
214215/*
214216** The xSavepoint() method.
214217**
214218** Flush the contents of the pending-terms table to disk.
214219*/
214220static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
214221 Fts5Table *pTab = (Fts5Table*)pVtab;
214222 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
214223 fts5CheckTransactionState(pTab, FTS5_SAVEPOINT, iSavepoint);
214224 fts5TripCursors(pTab);
214225 return sqlite3Fts5StorageSync(pTab->pStorage);
214226}
214227
214228/*
214229** The xRelease() method.
214230**
214231** This is a no-op.
214232*/
214233static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
214234 Fts5Table *pTab = (Fts5Table*)pVtab;
214235 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
214236 fts5CheckTransactionState(pTab, FTS5_RELEASE, iSavepoint);
214237 fts5TripCursors(pTab);
214238 return sqlite3Fts5StorageSync(pTab->pStorage);
214239}
214240
214241/*
214242** The xRollbackTo() method.
214243**
214244** Discard the contents of the pending terms table.
214245*/
214246static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
214247 Fts5Table *pTab = (Fts5Table*)pVtab;
214248 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
214249 fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
214250 fts5TripCursors(pTab);
214251 return sqlite3Fts5StorageRollback(pTab->pStorage);
214252}
214253
214254/*
214255** Register a new auxiliary function with global context pGlobal.
214256*/
214257static int fts5CreateAux(
214258 fts5_api *pApi, /* Global context (one per db handle) */
214259 const char *zName, /* Name of new function */
214260 void *pUserData, /* User data for aux. function */
214261 fts5_extension_function xFunc, /* Aux. function implementation */
214262 void(*xDestroy)(void*) /* Destructor for pUserData */
214263){
214264 Fts5Global *pGlobal = (Fts5Global*)pApi;
214265 int rc = sqlite3_overload_function(pGlobal->db, zName, -1);
214266 if( rc==SQLITE_OK ){
214267 Fts5Auxiliary *pAux;
214268 int nName; /* Size of zName in bytes, including \0 */
214269 int nByte; /* Bytes of space to allocate */
214270
214271 nName = (int)strlen(zName) + 1;
214272 nByte = sizeof(Fts5Auxiliary) + nName;
214273 pAux = (Fts5Auxiliary*)sqlite3_malloc(nByte);
214274 if( pAux ){
214275 memset(pAux, 0, nByte);
214276 pAux->zFunc = (char*)&pAux[1];
214277 memcpy(pAux->zFunc, zName, nName);
214278 pAux->pGlobal = pGlobal;
214279 pAux->pUserData = pUserData;
214280 pAux->xFunc = xFunc;
214281 pAux->xDestroy = xDestroy;
214282 pAux->pNext = pGlobal->pAux;
214283 pGlobal->pAux = pAux;
214284 }else{
214285 rc = SQLITE_NOMEM;
214286 }
214287 }
214288
214289 return rc;
214290}
214291
214292/*
214293** Register a new tokenizer. This is the implementation of the
214294** fts5_api.xCreateTokenizer() method.
214295*/
214296static int fts5CreateTokenizer(
214297 fts5_api *pApi, /* Global context (one per db handle) */
214298 const char *zName, /* Name of new function */
214299 void *pUserData, /* User data for aux. function */
214300 fts5_tokenizer *pTokenizer, /* Tokenizer implementation */
214301 void(*xDestroy)(void*) /* Destructor for pUserData */
214302){
214303 Fts5Global *pGlobal = (Fts5Global*)pApi;
214304 Fts5TokenizerModule *pNew;
214305 int nName; /* Size of zName and its \0 terminator */
214306 int nByte; /* Bytes of space to allocate */
214307 int rc = SQLITE_OK;
214308
214309 nName = (int)strlen(zName) + 1;
214310 nByte = sizeof(Fts5TokenizerModule) + nName;
214311 pNew = (Fts5TokenizerModule*)sqlite3_malloc(nByte);
214312 if( pNew ){
214313 memset(pNew, 0, nByte);
214314 pNew->zName = (char*)&pNew[1];
214315 memcpy(pNew->zName, zName, nName);
214316 pNew->pUserData = pUserData;
214317 pNew->x = *pTokenizer;
214318 pNew->xDestroy = xDestroy;
214319 pNew->pNext = pGlobal->pTok;
214320 pGlobal->pTok = pNew;
214321 if( pNew->pNext==0 ){
214322 pGlobal->pDfltTok = pNew;
214323 }
214324 }else{
214325 rc = SQLITE_NOMEM;
214326 }
214327
214328 return rc;
214329}
214330
214331static Fts5TokenizerModule *fts5LocateTokenizer(
214332 Fts5Global *pGlobal,
214333 const char *zName
214334){
214335 Fts5TokenizerModule *pMod = 0;
214336
214337 if( zName==0 ){
214338 pMod = pGlobal->pDfltTok;
214339 }else{
214340 for(pMod=pGlobal->pTok; pMod; pMod=pMod->pNext){
214341 if( sqlite3_stricmp(zName, pMod->zName)==0 ) break;
214342 }
214343 }
214344
214345 return pMod;
214346}
214347
214348/*
214349** Find a tokenizer. This is the implementation of the
214350** fts5_api.xFindTokenizer() method.
214351*/
214352static int fts5FindTokenizer(
214353 fts5_api *pApi, /* Global context (one per db handle) */
214354 const char *zName, /* Name of new function */
214355 void **ppUserData,
214356 fts5_tokenizer *pTokenizer /* Populate this object */
214357){
214358 int rc = SQLITE_OK;
214359 Fts5TokenizerModule *pMod;
214360
214361 pMod = fts5LocateTokenizer((Fts5Global*)pApi, zName);
214362 if( pMod ){
214363 *pTokenizer = pMod->x;
214364 *ppUserData = pMod->pUserData;
214365 }else{
214366 memset(pTokenizer, 0, sizeof(fts5_tokenizer));
214367 rc = SQLITE_ERROR;
214368 }
214369
214370 return rc;
214371}
214372
214373static int sqlite3Fts5GetTokenizer(
214374 Fts5Global *pGlobal,
214375 const char **azArg,
214376 int nArg,
214377 Fts5Tokenizer **ppTok,
214378 fts5_tokenizer **ppTokApi,
214379 char **pzErr
214380){
214381 Fts5TokenizerModule *pMod;
214382 int rc = SQLITE_OK;
214383
214384 pMod = fts5LocateTokenizer(pGlobal, nArg==0 ? 0 : azArg[0]);
214385 if( pMod==0 ){
214386 assert( nArg>0 );
214387 rc = SQLITE_ERROR;
214388 *pzErr = sqlite3_mprintf("no such tokenizer: %s", azArg[0]);
214389 }else{
214390 rc = pMod->x.xCreate(pMod->pUserData, &azArg[1], (nArg?nArg-1:0), ppTok);
214391 *ppTokApi = &pMod->x;
214392 if( rc!=SQLITE_OK && pzErr ){
214393 *pzErr = sqlite3_mprintf("error in tokenizer constructor");
214394 }
214395 }
214396
214397 if( rc!=SQLITE_OK ){
214398 *ppTokApi = 0;
214399 *ppTok = 0;
214400 }
214401
214402 return rc;
214403}
214404
214405static void fts5ModuleDestroy(void *pCtx){
214406 Fts5TokenizerModule *pTok, *pNextTok;
214407 Fts5Auxiliary *pAux, *pNextAux;
214408 Fts5Global *pGlobal = (Fts5Global*)pCtx;
214409
214410 for(pAux=pGlobal->pAux; pAux; pAux=pNextAux){
214411 pNextAux = pAux->pNext;
214412 if( pAux->xDestroy ) pAux->xDestroy(pAux->pUserData);
214413 sqlite3_free(pAux);
214414 }
214415
214416 for(pTok=pGlobal->pTok; pTok; pTok=pNextTok){
214417 pNextTok = pTok->pNext;
214418 if( pTok->xDestroy ) pTok->xDestroy(pTok->pUserData);
214419 sqlite3_free(pTok);
214420 }
214421
214422 sqlite3_free(pGlobal);
214423}
214424
214425static void fts5Fts5Func(
214426 sqlite3_context *pCtx, /* Function call context */
214427 int nArg, /* Number of args */
214428 sqlite3_value **apArg /* Function arguments */
214429){
214430 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
214431 fts5_api **ppApi;
214432 UNUSED_PARAM(nArg);
214433 assert( nArg==1 );
214434 ppApi = (fts5_api**)sqlite3_value_pointer(apArg[0], "fts5_api_ptr");
214435 if( ppApi ) *ppApi = &pGlobal->api;
214436}
214437
214438/*
214439** Implementation of fts5_source_id() function.
214440*/
214441static void fts5SourceIdFunc(
214442 sqlite3_context *pCtx, /* Function call context */
214443 int nArg, /* Number of args */
214444 sqlite3_value **apUnused /* Function arguments */
214445){
214446 assert( nArg==0 );
214447 UNUSED_PARAM2(nArg, apUnused);
214448 sqlite3_result_text(pCtx, "fts5: 2018-09-25 19:08:10 fb90e7189ae6d62e77ba3a308ca5d683f90bbe633cf681865365b8e92792d1c7", -1, SQLITE_TRANSIENT);
214449}
214450
214451static int fts5Init(sqlite3 *db){
214452 static const sqlite3_module fts5Mod = {
214453 /* iVersion */ 2,
214454 /* xCreate */ fts5CreateMethod,
214455 /* xConnect */ fts5ConnectMethod,
214456 /* xBestIndex */ fts5BestIndexMethod,
214457 /* xDisconnect */ fts5DisconnectMethod,
214458 /* xDestroy */ fts5DestroyMethod,
214459 /* xOpen */ fts5OpenMethod,
214460 /* xClose */ fts5CloseMethod,
214461 /* xFilter */ fts5FilterMethod,
214462 /* xNext */ fts5NextMethod,
214463 /* xEof */ fts5EofMethod,
214464 /* xColumn */ fts5ColumnMethod,
214465 /* xRowid */ fts5RowidMethod,
214466 /* xUpdate */ fts5UpdateMethod,
214467 /* xBegin */ fts5BeginMethod,
214468 /* xSync */ fts5SyncMethod,
214469 /* xCommit */ fts5CommitMethod,
214470 /* xRollback */ fts5RollbackMethod,
214471 /* xFindFunction */ fts5FindFunctionMethod,
214472 /* xRename */ fts5RenameMethod,
214473 /* xSavepoint */ fts5SavepointMethod,
214474 /* xRelease */ fts5ReleaseMethod,
214475 /* xRollbackTo */ fts5RollbackToMethod,
214476 };
214477
214478 int rc;
214479 Fts5Global *pGlobal = 0;
214480
214481 pGlobal = (Fts5Global*)sqlite3_malloc(sizeof(Fts5Global));
214482 if( pGlobal==0 ){
214483 rc = SQLITE_NOMEM;
214484 }else{
214485 void *p = (void*)pGlobal;
214486 memset(pGlobal, 0, sizeof(Fts5Global));
214487 pGlobal->db = db;
214488 pGlobal->api.iVersion = 2;
214489 pGlobal->api.xCreateFunction = fts5CreateAux;
214490 pGlobal->api.xCreateTokenizer = fts5CreateTokenizer;
214491 pGlobal->api.xFindTokenizer = fts5FindTokenizer;
214492 rc = sqlite3_create_module_v2(db, "fts5", &fts5Mod, p, fts5ModuleDestroy);
214493 if( rc==SQLITE_OK ) rc = sqlite3Fts5IndexInit(db);
214494 if( rc==SQLITE_OK ) rc = sqlite3Fts5ExprInit(pGlobal, db);
214495 if( rc==SQLITE_OK ) rc = sqlite3Fts5AuxInit(&pGlobal->api);
214496 if( rc==SQLITE_OK ) rc = sqlite3Fts5TokenizerInit(&pGlobal->api);
214497 if( rc==SQLITE_OK ) rc = sqlite3Fts5VocabInit(pGlobal, db);
214498 if( rc==SQLITE_OK ){
214499 rc = sqlite3_create_function(
214500 db, "fts5", 1, SQLITE_UTF8, p, fts5Fts5Func, 0, 0
214501 );
214502 }
214503 if( rc==SQLITE_OK ){
214504 rc = sqlite3_create_function(
214505 db, "fts5_source_id", 0, SQLITE_UTF8, p, fts5SourceIdFunc, 0, 0
214506 );
214507 }
214508 }
214509
214510 /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
214511 ** fts5_test_mi.c is compiled and linked into the executable. And call
214512 ** its entry point to enable the matchinfo() demo. */
214513#ifdef SQLITE_FTS5_ENABLE_TEST_MI
214514 if( rc==SQLITE_OK ){
214515 extern int sqlite3Fts5TestRegisterMatchinfo(sqlite3*);
214516 rc = sqlite3Fts5TestRegisterMatchinfo(db);
214517 }
214518#endif
214519
214520 return rc;
214521}
214522
214523/*
214524** The following functions are used to register the module with SQLite. If
214525** this module is being built as part of the SQLite core (SQLITE_CORE is
214526** defined), then sqlite3_open() will call sqlite3Fts5Init() directly.
214527**
214528** Or, if this module is being built as a loadable extension,
214529** sqlite3Fts5Init() is omitted and the two standard entry points
214530** sqlite3_fts_init() and sqlite3_fts5_init() defined instead.
214531*/
214532#ifndef SQLITE_CORE
214533#ifdef _WIN32
214534__declspec(dllexport)
214535#endif
214536SQLITE_API int sqlite3_fts_init(
214537 sqlite3 *db,
214538 char **pzErrMsg,
214539 const sqlite3_api_routines *pApi
214540){
214541 SQLITE_EXTENSION_INIT2(pApi);
214542 (void)pzErrMsg; /* Unused parameter */
214543 return fts5Init(db);
214544}
214545
214546#ifdef _WIN32
214547__declspec(dllexport)
214548#endif
214549SQLITE_API int sqlite3_fts5_init(
214550 sqlite3 *db,
214551 char **pzErrMsg,
214552 const sqlite3_api_routines *pApi
214553){
214554 SQLITE_EXTENSION_INIT2(pApi);
214555 (void)pzErrMsg; /* Unused parameter */
214556 return fts5Init(db);
214557}
214558#else
214559SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3 *db){
214560 return fts5Init(db);
214561}
214562#endif
214563
214564/*
214565** 2014 May 31
214566**
214567** The author disclaims copyright to this source code. In place of
214568** a legal notice, here is a blessing:
214569**
214570** May you do good and not evil.
214571** May you find forgiveness for yourself and forgive others.
214572** May you share freely, never taking more than you give.
214573**
214574******************************************************************************
214575**
214576*/
214577
214578
214579
214580/* #include "fts5Int.h" */
214581
214582struct Fts5Storage {
214583 Fts5Config *pConfig;
214584 Fts5Index *pIndex;
214585 int bTotalsValid; /* True if nTotalRow/aTotalSize[] are valid */
214586 i64 nTotalRow; /* Total number of rows in FTS table */
214587 i64 *aTotalSize; /* Total sizes of each column */
214588 sqlite3_stmt *aStmt[11];
214589};
214590
214591
214592#if FTS5_STMT_SCAN_ASC!=0
214593# error "FTS5_STMT_SCAN_ASC mismatch"
214594#endif
214595#if FTS5_STMT_SCAN_DESC!=1
214596# error "FTS5_STMT_SCAN_DESC mismatch"
214597#endif
214598#if FTS5_STMT_LOOKUP!=2
214599# error "FTS5_STMT_LOOKUP mismatch"
214600#endif
214601
214602#define FTS5_STMT_INSERT_CONTENT 3
214603#define FTS5_STMT_REPLACE_CONTENT 4
214604#define FTS5_STMT_DELETE_CONTENT 5
214605#define FTS5_STMT_REPLACE_DOCSIZE 6
214606#define FTS5_STMT_DELETE_DOCSIZE 7
214607#define FTS5_STMT_LOOKUP_DOCSIZE 8
214608#define FTS5_STMT_REPLACE_CONFIG 9
214609#define FTS5_STMT_SCAN 10
214610
214611/*
214612** Prepare the two insert statements - Fts5Storage.pInsertContent and
214613** Fts5Storage.pInsertDocsize - if they have not already been prepared.
214614** Return SQLITE_OK if successful, or an SQLite error code if an error
214615** occurs.
214616*/
214617static int fts5StorageGetStmt(
214618 Fts5Storage *p, /* Storage handle */
214619 int eStmt, /* FTS5_STMT_XXX constant */
214620 sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */
214621 char **pzErrMsg /* OUT: Error message (if any) */
214622){
214623 int rc = SQLITE_OK;
214624
214625 /* If there is no %_docsize table, there should be no requests for
214626 ** statements to operate on it. */
214627 assert( p->pConfig->bColumnsize || (
214628 eStmt!=FTS5_STMT_REPLACE_DOCSIZE
214629 && eStmt!=FTS5_STMT_DELETE_DOCSIZE
214630 && eStmt!=FTS5_STMT_LOOKUP_DOCSIZE
214631 ));
214632
214633 assert( eStmt>=0 && eStmt<ArraySize(p->aStmt) );
214634 if( p->aStmt[eStmt]==0 ){
214635 const char *azStmt[] = {
214636 "SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC",
214637 "SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC",
214638 "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP */
214639
214640 "INSERT INTO %Q.'%q_content' VALUES(%s)", /* INSERT_CONTENT */
214641 "REPLACE INTO %Q.'%q_content' VALUES(%s)", /* REPLACE_CONTENT */
214642 "DELETE FROM %Q.'%q_content' WHERE id=?", /* DELETE_CONTENT */
214643 "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)", /* REPLACE_DOCSIZE */
214644 "DELETE FROM %Q.'%q_docsize' WHERE id=?", /* DELETE_DOCSIZE */
214645
214646 "SELECT sz FROM %Q.'%q_docsize' WHERE id=?", /* LOOKUP_DOCSIZE */
214647
214648 "REPLACE INTO %Q.'%q_config' VALUES(?,?)", /* REPLACE_CONFIG */
214649 "SELECT %s FROM %s AS T", /* SCAN */
214650 };
214651 Fts5Config *pC = p->pConfig;
214652 char *zSql = 0;
214653
214654 switch( eStmt ){
214655 case FTS5_STMT_SCAN:
214656 zSql = sqlite3_mprintf(azStmt[eStmt],
214657 pC->zContentExprlist, pC->zContent
214658 );
214659 break;
214660
214661 case FTS5_STMT_SCAN_ASC:
214662 case FTS5_STMT_SCAN_DESC:
214663 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zContentExprlist,
214664 pC->zContent, pC->zContentRowid, pC->zContentRowid,
214665 pC->zContentRowid
214666 );
214667 break;
214668
214669 case FTS5_STMT_LOOKUP:
214670 zSql = sqlite3_mprintf(azStmt[eStmt],
214671 pC->zContentExprlist, pC->zContent, pC->zContentRowid
214672 );
214673 break;
214674
214675 case FTS5_STMT_INSERT_CONTENT:
214676 case FTS5_STMT_REPLACE_CONTENT: {
214677 int nCol = pC->nCol + 1;
214678 char *zBind;
214679 int i;
214680
214681 zBind = sqlite3_malloc(1 + nCol*2);
214682 if( zBind ){
214683 for(i=0; i<nCol; i++){
214684 zBind[i*2] = '?';
214685 zBind[i*2 + 1] = ',';
214686 }
214687 zBind[i*2-1] = '\0';
214688 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName, zBind);
214689 sqlite3_free(zBind);
214690 }
214691 break;
214692 }
214693
214694 default:
214695 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName);
214696 break;
214697 }
214698
214699 if( zSql==0 ){
214700 rc = SQLITE_NOMEM;
214701 }else{
214702 rc = sqlite3_prepare_v3(pC->db, zSql, -1,
214703 SQLITE_PREPARE_PERSISTENT, &p->aStmt[eStmt], 0);
214704 sqlite3_free(zSql);
214705 if( rc!=SQLITE_OK && pzErrMsg ){
214706 *pzErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db));
214707 }
214708 }
214709 }
214710
214711 *ppStmt = p->aStmt[eStmt];
214712 sqlite3_reset(*ppStmt);
214713 return rc;
214714}
214715
214716
214717static int fts5ExecPrintf(
214718 sqlite3 *db,
214719 char **pzErr,
214720 const char *zFormat,
214721 ...
214722){
214723 int rc;
214724 va_list ap; /* ... printf arguments */
214725 char *zSql;
214726
214727 va_start(ap, zFormat);
214728 zSql = sqlite3_vmprintf(zFormat, ap);
214729
214730 if( zSql==0 ){
214731 rc = SQLITE_NOMEM;
214732 }else{
214733 rc = sqlite3_exec(db, zSql, 0, 0, pzErr);
214734 sqlite3_free(zSql);
214735 }
214736
214737 va_end(ap);
214738 return rc;
214739}
214740
214741/*
214742** Drop all shadow tables. Return SQLITE_OK if successful or an SQLite error
214743** code otherwise.
214744*/
214745static int sqlite3Fts5DropAll(Fts5Config *pConfig){
214746 int rc = fts5ExecPrintf(pConfig->db, 0,
214747 "DROP TABLE IF EXISTS %Q.'%q_data';"
214748 "DROP TABLE IF EXISTS %Q.'%q_idx';"
214749 "DROP TABLE IF EXISTS %Q.'%q_config';",
214750 pConfig->zDb, pConfig->zName,
214751 pConfig->zDb, pConfig->zName,
214752 pConfig->zDb, pConfig->zName
214753 );
214754 if( rc==SQLITE_OK && pConfig->bColumnsize ){
214755 rc = fts5ExecPrintf(pConfig->db, 0,
214756 "DROP TABLE IF EXISTS %Q.'%q_docsize';",
214757 pConfig->zDb, pConfig->zName
214758 );
214759 }
214760 if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
214761 rc = fts5ExecPrintf(pConfig->db, 0,
214762 "DROP TABLE IF EXISTS %Q.'%q_content';",
214763 pConfig->zDb, pConfig->zName
214764 );
214765 }
214766 return rc;
214767}
214768
214769static void fts5StorageRenameOne(
214770 Fts5Config *pConfig, /* Current FTS5 configuration */
214771 int *pRc, /* IN/OUT: Error code */
214772 const char *zTail, /* Tail of table name e.g. "data", "config" */
214773 const char *zName /* New name of FTS5 table */
214774){
214775 if( *pRc==SQLITE_OK ){
214776 *pRc = fts5ExecPrintf(pConfig->db, 0,
214777 "ALTER TABLE %Q.'%q_%s' RENAME TO '%q_%s';",
214778 pConfig->zDb, pConfig->zName, zTail, zName, zTail
214779 );
214780 }
214781}
214782
214783static int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){
214784 Fts5Config *pConfig = pStorage->pConfig;
214785 int rc = sqlite3Fts5StorageSync(pStorage);
214786
214787 fts5StorageRenameOne(pConfig, &rc, "data", zName);
214788 fts5StorageRenameOne(pConfig, &rc, "idx", zName);
214789 fts5StorageRenameOne(pConfig, &rc, "config", zName);
214790 if( pConfig->bColumnsize ){
214791 fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
214792 }
214793 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
214794 fts5StorageRenameOne(pConfig, &rc, "content", zName);
214795 }
214796 return rc;
214797}
214798
214799/*
214800** Create the shadow table named zPost, with definition zDefn. Return
214801** SQLITE_OK if successful, or an SQLite error code otherwise.
214802*/
214803static int sqlite3Fts5CreateTable(
214804 Fts5Config *pConfig, /* FTS5 configuration */
214805 const char *zPost, /* Shadow table to create (e.g. "content") */
214806 const char *zDefn, /* Columns etc. for shadow table */
214807 int bWithout, /* True for without rowid */
214808 char **pzErr /* OUT: Error message */
214809){
214810 int rc;
214811 char *zErr = 0;
214812
214813 rc = fts5ExecPrintf(pConfig->db, &zErr, "CREATE TABLE %Q.'%q_%q'(%s)%s",
214814 pConfig->zDb, pConfig->zName, zPost, zDefn,
214815#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID
214816 bWithout?" WITHOUT ROWID":
214817#endif
214818 ""
214819 );
214820 if( zErr ){
214821 *pzErr = sqlite3_mprintf(
214822 "fts5: error creating shadow table %q_%s: %s",
214823 pConfig->zName, zPost, zErr
214824 );
214825 sqlite3_free(zErr);
214826 }
214827
214828 return rc;
214829}
214830
214831/*
214832** Open a new Fts5Index handle. If the bCreate argument is true, create
214833** and initialize the underlying tables
214834**
214835** If successful, set *pp to point to the new object and return SQLITE_OK.
214836** Otherwise, set *pp to NULL and return an SQLite error code.
214837*/
214838static int sqlite3Fts5StorageOpen(
214839 Fts5Config *pConfig,
214840 Fts5Index *pIndex,
214841 int bCreate,
214842 Fts5Storage **pp,
214843 char **pzErr /* OUT: Error message */
214844){
214845 int rc = SQLITE_OK;
214846 Fts5Storage *p; /* New object */
214847 int nByte; /* Bytes of space to allocate */
214848
214849 nByte = sizeof(Fts5Storage) /* Fts5Storage object */
214850 + pConfig->nCol * sizeof(i64); /* Fts5Storage.aTotalSize[] */
214851 *pp = p = (Fts5Storage*)sqlite3_malloc(nByte);
214852 if( !p ) return SQLITE_NOMEM;
214853
214854 memset(p, 0, nByte);
214855 p->aTotalSize = (i64*)&p[1];
214856 p->pConfig = pConfig;
214857 p->pIndex = pIndex;
214858
214859 if( bCreate ){
214860 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
214861 int nDefn = 32 + pConfig->nCol*10;
214862 char *zDefn = sqlite3_malloc(32 + pConfig->nCol * 10);
214863 if( zDefn==0 ){
214864 rc = SQLITE_NOMEM;
214865 }else{
214866 int i;
214867 int iOff;
214868 sqlite3_snprintf(nDefn, zDefn, "id INTEGER PRIMARY KEY");
214869 iOff = (int)strlen(zDefn);
214870 for(i=0; i<pConfig->nCol; i++){
214871 sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", c%d", i);
214872 iOff += (int)strlen(&zDefn[iOff]);
214873 }
214874 rc = sqlite3Fts5CreateTable(pConfig, "content", zDefn, 0, pzErr);
214875 }
214876 sqlite3_free(zDefn);
214877 }
214878
214879 if( rc==SQLITE_OK && pConfig->bColumnsize ){
214880 rc = sqlite3Fts5CreateTable(
214881 pConfig, "docsize", "id INTEGER PRIMARY KEY, sz BLOB", 0, pzErr
214882 );
214883 }
214884 if( rc==SQLITE_OK ){
214885 rc = sqlite3Fts5CreateTable(
214886 pConfig, "config", "k PRIMARY KEY, v", 1, pzErr
214887 );
214888 }
214889 if( rc==SQLITE_OK ){
214890 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
214891 }
214892 }
214893
214894 if( rc ){
214895 sqlite3Fts5StorageClose(p);
214896 *pp = 0;
214897 }
214898 return rc;
214899}
214900
214901/*
214902** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen().
214903*/
214904static int sqlite3Fts5StorageClose(Fts5Storage *p){
214905 int rc = SQLITE_OK;
214906 if( p ){
214907 int i;
214908
214909 /* Finalize all SQL statements */
214910 for(i=0; i<ArraySize(p->aStmt); i++){
214911 sqlite3_finalize(p->aStmt[i]);
214912 }
214913
214914 sqlite3_free(p);
214915 }
214916 return rc;
214917}
214918
214919typedef struct Fts5InsertCtx Fts5InsertCtx;
214920struct Fts5InsertCtx {
214921 Fts5Storage *pStorage;
214922 int iCol;
214923 int szCol; /* Size of column value in tokens */
214924};
214925
214926/*
214927** Tokenization callback used when inserting tokens into the FTS index.
214928*/
214929static int fts5StorageInsertCallback(
214930 void *pContext, /* Pointer to Fts5InsertCtx object */
214931 int tflags,
214932 const char *pToken, /* Buffer containing token */
214933 int nToken, /* Size of token in bytes */
214934 int iUnused1, /* Start offset of token */
214935 int iUnused2 /* End offset of token */
214936){
214937 Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
214938 Fts5Index *pIdx = pCtx->pStorage->pIndex;
214939 UNUSED_PARAM2(iUnused1, iUnused2);
214940 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
214941 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
214942 pCtx->szCol++;
214943 }
214944 return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken);
214945}
214946
214947/*
214948** If a row with rowid iDel is present in the %_content table, add the
214949** delete-markers to the FTS index necessary to delete it. Do not actually
214950** remove the %_content row at this time though.
214951*/
214952static int fts5StorageDeleteFromIndex(
214953 Fts5Storage *p,
214954 i64 iDel,
214955 sqlite3_value **apVal
214956){
214957 Fts5Config *pConfig = p->pConfig;
214958 sqlite3_stmt *pSeek = 0; /* SELECT to read row iDel from %_data */
214959 int rc; /* Return code */
214960 int rc2; /* sqlite3_reset() return code */
214961 int iCol;
214962 Fts5InsertCtx ctx;
214963
214964 if( apVal==0 ){
214965 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP, &pSeek, 0);
214966 if( rc!=SQLITE_OK ) return rc;
214967 sqlite3_bind_int64(pSeek, 1, iDel);
214968 if( sqlite3_step(pSeek)!=SQLITE_ROW ){
214969 return sqlite3_reset(pSeek);
214970 }
214971 }
214972
214973 ctx.pStorage = p;
214974 ctx.iCol = -1;
214975 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel);
214976 for(iCol=1; rc==SQLITE_OK && iCol<=pConfig->nCol; iCol++){
214977 if( pConfig->abUnindexed[iCol-1]==0 ){
214978 const char *zText;
214979 int nText;
214980 if( pSeek ){
214981 zText = (const char*)sqlite3_column_text(pSeek, iCol);
214982 nText = sqlite3_column_bytes(pSeek, iCol);
214983 }else{
214984 zText = (const char*)sqlite3_value_text(apVal[iCol-1]);
214985 nText = sqlite3_value_bytes(apVal[iCol-1]);
214986 }
214987 ctx.szCol = 0;
214988 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT,
214989 zText, nText, (void*)&ctx, fts5StorageInsertCallback
214990 );
214991 p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
214992 }
214993 }
214994 p->nTotalRow--;
214995
214996 rc2 = sqlite3_reset(pSeek);
214997 if( rc==SQLITE_OK ) rc = rc2;
214998 return rc;
214999}
215000
215001
215002/*
215003** Insert a record into the %_docsize table. Specifically, do:
215004**
215005** INSERT OR REPLACE INTO %_docsize(id, sz) VALUES(iRowid, pBuf);
215006**
215007** If there is no %_docsize table (as happens if the columnsize=0 option
215008** is specified when the FTS5 table is created), this function is a no-op.
215009*/
215010static int fts5StorageInsertDocsize(
215011 Fts5Storage *p, /* Storage module to write to */
215012 i64 iRowid, /* id value */
215013 Fts5Buffer *pBuf /* sz value */
215014){
215015 int rc = SQLITE_OK;
215016 if( p->pConfig->bColumnsize ){
215017 sqlite3_stmt *pReplace = 0;
215018 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
215019 if( rc==SQLITE_OK ){
215020 sqlite3_bind_int64(pReplace, 1, iRowid);
215021 sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC);
215022 sqlite3_step(pReplace);
215023 rc = sqlite3_reset(pReplace);
215024 sqlite3_bind_null(pReplace, 2);
215025 }
215026 }
215027 return rc;
215028}
215029
215030/*
215031** Load the contents of the "averages" record from disk into the
215032** p->nTotalRow and p->aTotalSize[] variables. If successful, and if
215033** argument bCache is true, set the p->bTotalsValid flag to indicate
215034** that the contents of aTotalSize[] and nTotalRow are valid until
215035** further notice.
215036**
215037** Return SQLITE_OK if successful, or an SQLite error code if an error
215038** occurs.
215039*/
215040static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){
215041 int rc = SQLITE_OK;
215042 if( p->bTotalsValid==0 ){
215043 rc = sqlite3Fts5IndexGetAverages(p->pIndex, &p->nTotalRow, p->aTotalSize);
215044 p->bTotalsValid = bCache;
215045 }
215046 return rc;
215047}
215048
215049/*
215050** Store the current contents of the p->nTotalRow and p->aTotalSize[]
215051** variables in the "averages" record on disk.
215052**
215053** Return SQLITE_OK if successful, or an SQLite error code if an error
215054** occurs.
215055*/
215056static int fts5StorageSaveTotals(Fts5Storage *p){
215057 int nCol = p->pConfig->nCol;
215058 int i;
215059 Fts5Buffer buf;
215060 int rc = SQLITE_OK;
215061 memset(&buf, 0, sizeof(buf));
215062
215063 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->nTotalRow);
215064 for(i=0; i<nCol; i++){
215065 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->aTotalSize[i]);
215066 }
215067 if( rc==SQLITE_OK ){
215068 rc = sqlite3Fts5IndexSetAverages(p->pIndex, buf.p, buf.n);
215069 }
215070 sqlite3_free(buf.p);
215071
215072 return rc;
215073}
215074
215075/*
215076** Remove a row from the FTS table.
215077*/
215078static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){
215079 Fts5Config *pConfig = p->pConfig;
215080 int rc;
215081 sqlite3_stmt *pDel = 0;
215082
215083 assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 );
215084 rc = fts5StorageLoadTotals(p, 1);
215085
215086 /* Delete the index records */
215087 if( rc==SQLITE_OK ){
215088 rc = fts5StorageDeleteFromIndex(p, iDel, apVal);
215089 }
215090
215091 /* Delete the %_docsize record */
215092 if( rc==SQLITE_OK && pConfig->bColumnsize ){
215093 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE, &pDel, 0);
215094 if( rc==SQLITE_OK ){
215095 sqlite3_bind_int64(pDel, 1, iDel);
215096 sqlite3_step(pDel);
215097 rc = sqlite3_reset(pDel);
215098 }
215099 }
215100
215101 /* Delete the %_content record */
215102 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
215103 if( rc==SQLITE_OK ){
215104 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT, &pDel, 0);
215105 }
215106 if( rc==SQLITE_OK ){
215107 sqlite3_bind_int64(pDel, 1, iDel);
215108 sqlite3_step(pDel);
215109 rc = sqlite3_reset(pDel);
215110 }
215111 }
215112
215113 return rc;
215114}
215115
215116/*
215117** Delete all entries in the FTS5 index.
215118*/
215119static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
215120 Fts5Config *pConfig = p->pConfig;
215121 int rc;
215122
215123 /* Delete the contents of the %_data and %_docsize tables. */
215124 rc = fts5ExecPrintf(pConfig->db, 0,
215125 "DELETE FROM %Q.'%q_data';"
215126 "DELETE FROM %Q.'%q_idx';",
215127 pConfig->zDb, pConfig->zName,
215128 pConfig->zDb, pConfig->zName
215129 );
215130 if( rc==SQLITE_OK && pConfig->bColumnsize ){
215131 rc = fts5ExecPrintf(pConfig->db, 0,
215132 "DELETE FROM %Q.'%q_docsize';",
215133 pConfig->zDb, pConfig->zName
215134 );
215135 }
215136
215137 /* Reinitialize the %_data table. This call creates the initial structure
215138 ** and averages records. */
215139 if( rc==SQLITE_OK ){
215140 rc = sqlite3Fts5IndexReinit(p->pIndex);
215141 }
215142 if( rc==SQLITE_OK ){
215143 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
215144 }
215145 return rc;
215146}
215147
215148static int sqlite3Fts5StorageRebuild(Fts5Storage *p){
215149 Fts5Buffer buf = {0,0,0};
215150 Fts5Config *pConfig = p->pConfig;
215151 sqlite3_stmt *pScan = 0;
215152 Fts5InsertCtx ctx;
215153 int rc;
215154
215155 memset(&ctx, 0, sizeof(Fts5InsertCtx));
215156 ctx.pStorage = p;
215157 rc = sqlite3Fts5StorageDeleteAll(p);
215158 if( rc==SQLITE_OK ){
215159 rc = fts5StorageLoadTotals(p, 1);
215160 }
215161
215162 if( rc==SQLITE_OK ){
215163 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0);
215164 }
215165
215166 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pScan) ){
215167 i64 iRowid = sqlite3_column_int64(pScan, 0);
215168
215169 sqlite3Fts5BufferZero(&buf);
215170 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
215171 for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
215172 ctx.szCol = 0;
215173 if( pConfig->abUnindexed[ctx.iCol]==0 ){
215174 rc = sqlite3Fts5Tokenize(pConfig,
215175 FTS5_TOKENIZE_DOCUMENT,
215176 (const char*)sqlite3_column_text(pScan, ctx.iCol+1),
215177 sqlite3_column_bytes(pScan, ctx.iCol+1),
215178 (void*)&ctx,
215179 fts5StorageInsertCallback
215180 );
215181 }
215182 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
215183 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
215184 }
215185 p->nTotalRow++;
215186
215187 if( rc==SQLITE_OK ){
215188 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
215189 }
215190 }
215191 sqlite3_free(buf.p);
215192
215193 /* Write the averages record */
215194 if( rc==SQLITE_OK ){
215195 rc = fts5StorageSaveTotals(p);
215196 }
215197 return rc;
215198}
215199
215200static int sqlite3Fts5StorageOptimize(Fts5Storage *p){
215201 return sqlite3Fts5IndexOptimize(p->pIndex);
215202}
215203
215204static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){
215205 return sqlite3Fts5IndexMerge(p->pIndex, nMerge);
215206}
215207
215208static int sqlite3Fts5StorageReset(Fts5Storage *p){
215209 return sqlite3Fts5IndexReset(p->pIndex);
215210}
215211
215212/*
215213** Allocate a new rowid. This is used for "external content" tables when
215214** a NULL value is inserted into the rowid column. The new rowid is allocated
215215** by inserting a dummy row into the %_docsize table. The dummy will be
215216** overwritten later.
215217**
215218** If the %_docsize table does not exist, SQLITE_MISMATCH is returned. In
215219** this case the user is required to provide a rowid explicitly.
215220*/
215221static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){
215222 int rc = SQLITE_MISMATCH;
215223 if( p->pConfig->bColumnsize ){
215224 sqlite3_stmt *pReplace = 0;
215225 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
215226 if( rc==SQLITE_OK ){
215227 sqlite3_bind_null(pReplace, 1);
215228 sqlite3_bind_null(pReplace, 2);
215229 sqlite3_step(pReplace);
215230 rc = sqlite3_reset(pReplace);
215231 }
215232 if( rc==SQLITE_OK ){
215233 *piRowid = sqlite3_last_insert_rowid(p->pConfig->db);
215234 }
215235 }
215236 return rc;
215237}
215238
215239/*
215240** Insert a new row into the FTS content table.
215241*/
215242static int sqlite3Fts5StorageContentInsert(
215243 Fts5Storage *p,
215244 sqlite3_value **apVal,
215245 i64 *piRowid
215246){
215247 Fts5Config *pConfig = p->pConfig;
215248 int rc = SQLITE_OK;
215249
215250 /* Insert the new row into the %_content table. */
215251 if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
215252 if( sqlite3_value_type(apVal[1])==SQLITE_INTEGER ){
215253 *piRowid = sqlite3_value_int64(apVal[1]);
215254 }else{
215255 rc = fts5StorageNewRowid(p, piRowid);
215256 }
215257 }else{
215258 sqlite3_stmt *pInsert = 0; /* Statement to write %_content table */
215259 int i; /* Counter variable */
215260 rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT, &pInsert, 0);
215261 for(i=1; rc==SQLITE_OK && i<=pConfig->nCol+1; i++){
215262 rc = sqlite3_bind_value(pInsert, i, apVal[i]);
215263 }
215264 if( rc==SQLITE_OK ){
215265 sqlite3_step(pInsert);
215266 rc = sqlite3_reset(pInsert);
215267 }
215268 *piRowid = sqlite3_last_insert_rowid(pConfig->db);
215269 }
215270
215271 return rc;
215272}
215273
215274/*
215275** Insert new entries into the FTS index and %_docsize table.
215276*/
215277static int sqlite3Fts5StorageIndexInsert(
215278 Fts5Storage *p,
215279 sqlite3_value **apVal,
215280 i64 iRowid
215281){
215282 Fts5Config *pConfig = p->pConfig;
215283 int rc = SQLITE_OK; /* Return code */
215284 Fts5InsertCtx ctx; /* Tokenization callback context object */
215285 Fts5Buffer buf; /* Buffer used to build up %_docsize blob */
215286
215287 memset(&buf, 0, sizeof(Fts5Buffer));
215288 ctx.pStorage = p;
215289 rc = fts5StorageLoadTotals(p, 1);
215290
215291 if( rc==SQLITE_OK ){
215292 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
215293 }
215294 for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
215295 ctx.szCol = 0;
215296 if( pConfig->abUnindexed[ctx.iCol]==0 ){
215297 rc = sqlite3Fts5Tokenize(pConfig,
215298 FTS5_TOKENIZE_DOCUMENT,
215299 (const char*)sqlite3_value_text(apVal[ctx.iCol+2]),
215300 sqlite3_value_bytes(apVal[ctx.iCol+2]),
215301 (void*)&ctx,
215302 fts5StorageInsertCallback
215303 );
215304 }
215305 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
215306 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
215307 }
215308 p->nTotalRow++;
215309
215310 /* Write the %_docsize record */
215311 if( rc==SQLITE_OK ){
215312 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
215313 }
215314 sqlite3_free(buf.p);
215315
215316 return rc;
215317}
215318
215319static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
215320 Fts5Config *pConfig = p->pConfig;
215321 char *zSql;
215322 int rc;
215323
215324 zSql = sqlite3_mprintf("SELECT count(*) FROM %Q.'%q_%s'",
215325 pConfig->zDb, pConfig->zName, zSuffix
215326 );
215327 if( zSql==0 ){
215328 rc = SQLITE_NOMEM;
215329 }else{
215330 sqlite3_stmt *pCnt = 0;
215331 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pCnt, 0);
215332 if( rc==SQLITE_OK ){
215333 if( SQLITE_ROW==sqlite3_step(pCnt) ){
215334 *pnRow = sqlite3_column_int64(pCnt, 0);
215335 }
215336 rc = sqlite3_finalize(pCnt);
215337 }
215338 }
215339
215340 sqlite3_free(zSql);
215341 return rc;
215342}
215343
215344/*
215345** Context object used by sqlite3Fts5StorageIntegrity().
215346*/
215347typedef struct Fts5IntegrityCtx Fts5IntegrityCtx;
215348struct Fts5IntegrityCtx {
215349 i64 iRowid;
215350 int iCol;
215351 int szCol;
215352 u64 cksum;
215353 Fts5Termset *pTermset;
215354 Fts5Config *pConfig;
215355};
215356
215357
215358/*
215359** Tokenization callback used by integrity check.
215360*/
215361static int fts5StorageIntegrityCallback(
215362 void *pContext, /* Pointer to Fts5IntegrityCtx object */
215363 int tflags,
215364 const char *pToken, /* Buffer containing token */
215365 int nToken, /* Size of token in bytes */
215366 int iUnused1, /* Start offset of token */
215367 int iUnused2 /* End offset of token */
215368){
215369 Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
215370 Fts5Termset *pTermset = pCtx->pTermset;
215371 int bPresent;
215372 int ii;
215373 int rc = SQLITE_OK;
215374 int iPos;
215375 int iCol;
215376
215377 UNUSED_PARAM2(iUnused1, iUnused2);
215378 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
215379
215380 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
215381 pCtx->szCol++;
215382 }
215383
215384 switch( pCtx->pConfig->eDetail ){
215385 case FTS5_DETAIL_FULL:
215386 iPos = pCtx->szCol-1;
215387 iCol = pCtx->iCol;
215388 break;
215389
215390 case FTS5_DETAIL_COLUMNS:
215391 iPos = pCtx->iCol;
215392 iCol = 0;
215393 break;
215394
215395 default:
215396 assert( pCtx->pConfig->eDetail==FTS5_DETAIL_NONE );
215397 iPos = 0;
215398 iCol = 0;
215399 break;
215400 }
215401
215402 rc = sqlite3Fts5TermsetAdd(pTermset, 0, pToken, nToken, &bPresent);
215403 if( rc==SQLITE_OK && bPresent==0 ){
215404 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
215405 pCtx->iRowid, iCol, iPos, 0, pToken, nToken
215406 );
215407 }
215408
215409 for(ii=0; rc==SQLITE_OK && ii<pCtx->pConfig->nPrefix; ii++){
215410 const int nChar = pCtx->pConfig->aPrefix[ii];
215411 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
215412 if( nByte ){
215413 rc = sqlite3Fts5TermsetAdd(pTermset, ii+1, pToken, nByte, &bPresent);
215414 if( bPresent==0 ){
215415 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
215416 pCtx->iRowid, iCol, iPos, ii+1, pToken, nByte
215417 );
215418 }
215419 }
215420 }
215421
215422 return rc;
215423}
215424
215425/*
215426** Check that the contents of the FTS index match that of the %_content
215427** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return
215428** some other SQLite error code if an error occurs while attempting to
215429** determine this.
215430*/
215431static int sqlite3Fts5StorageIntegrity(Fts5Storage *p){
215432 Fts5Config *pConfig = p->pConfig;
215433 int rc; /* Return code */
215434 int *aColSize; /* Array of size pConfig->nCol */
215435 i64 *aTotalSize; /* Array of size pConfig->nCol */
215436 Fts5IntegrityCtx ctx;
215437 sqlite3_stmt *pScan;
215438
215439 memset(&ctx, 0, sizeof(Fts5IntegrityCtx));
215440 ctx.pConfig = p->pConfig;
215441 aTotalSize = (i64*)sqlite3_malloc(pConfig->nCol * (sizeof(int)+sizeof(i64)));
215442 if( !aTotalSize ) return SQLITE_NOMEM;
215443 aColSize = (int*)&aTotalSize[pConfig->nCol];
215444 memset(aTotalSize, 0, sizeof(i64) * pConfig->nCol);
215445
215446 /* Generate the expected index checksum based on the contents of the
215447 ** %_content table. This block stores the checksum in ctx.cksum. */
215448 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0);
215449 if( rc==SQLITE_OK ){
215450 int rc2;
215451 while( SQLITE_ROW==sqlite3_step(pScan) ){
215452 int i;
215453 ctx.iRowid = sqlite3_column_int64(pScan, 0);
215454 ctx.szCol = 0;
215455 if( pConfig->bColumnsize ){
215456 rc = sqlite3Fts5StorageDocsize(p, ctx.iRowid, aColSize);
215457 }
215458 if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_NONE ){
215459 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
215460 }
215461 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
215462 if( pConfig->abUnindexed[i] ) continue;
215463 ctx.iCol = i;
215464 ctx.szCol = 0;
215465 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
215466 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
215467 }
215468 if( rc==SQLITE_OK ){
215469 rc = sqlite3Fts5Tokenize(pConfig,
215470 FTS5_TOKENIZE_DOCUMENT,
215471 (const char*)sqlite3_column_text(pScan, i+1),
215472 sqlite3_column_bytes(pScan, i+1),
215473 (void*)&ctx,
215474 fts5StorageIntegrityCallback
215475 );
215476 }
215477 if( rc==SQLITE_OK && pConfig->bColumnsize && ctx.szCol!=aColSize[i] ){
215478 rc = FTS5_CORRUPT;
215479 }
215480 aTotalSize[i] += ctx.szCol;
215481 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
215482 sqlite3Fts5TermsetFree(ctx.pTermset);
215483 ctx.pTermset = 0;
215484 }
215485 }
215486 sqlite3Fts5TermsetFree(ctx.pTermset);
215487 ctx.pTermset = 0;
215488
215489 if( rc!=SQLITE_OK ) break;
215490 }
215491 rc2 = sqlite3_reset(pScan);
215492 if( rc==SQLITE_OK ) rc = rc2;
215493 }
215494
215495 /* Test that the "totals" (sometimes called "averages") record looks Ok */
215496 if( rc==SQLITE_OK ){
215497 int i;
215498 rc = fts5StorageLoadTotals(p, 0);
215499 for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
215500 if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT;
215501 }
215502 }
215503
215504 /* Check that the %_docsize and %_content tables contain the expected
215505 ** number of rows. */
215506 if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
215507 i64 nRow = 0;
215508 rc = fts5StorageCount(p, "content", &nRow);
215509 if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
215510 }
215511 if( rc==SQLITE_OK && pConfig->bColumnsize ){
215512 i64 nRow = 0;
215513 rc = fts5StorageCount(p, "docsize", &nRow);
215514 if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
215515 }
215516
215517 /* Pass the expected checksum down to the FTS index module. It will
215518 ** verify, amongst other things, that it matches the checksum generated by
215519 ** inspecting the index itself. */
215520 if( rc==SQLITE_OK ){
215521 rc = sqlite3Fts5IndexIntegrityCheck(p->pIndex, ctx.cksum);
215522 }
215523
215524 sqlite3_free(aTotalSize);
215525 return rc;
215526}
215527
215528/*
215529** Obtain an SQLite statement handle that may be used to read data from the
215530** %_content table.
215531*/
215532static int sqlite3Fts5StorageStmt(
215533 Fts5Storage *p,
215534 int eStmt,
215535 sqlite3_stmt **pp,
215536 char **pzErrMsg
215537){
215538 int rc;
215539 assert( eStmt==FTS5_STMT_SCAN_ASC
215540 || eStmt==FTS5_STMT_SCAN_DESC
215541 || eStmt==FTS5_STMT_LOOKUP
215542 );
215543 rc = fts5StorageGetStmt(p, eStmt, pp, pzErrMsg);
215544 if( rc==SQLITE_OK ){
215545 assert( p->aStmt[eStmt]==*pp );
215546 p->aStmt[eStmt] = 0;
215547 }
215548 return rc;
215549}
215550
215551/*
215552** Release an SQLite statement handle obtained via an earlier call to
215553** sqlite3Fts5StorageStmt(). The eStmt parameter passed to this function
215554** must match that passed to the sqlite3Fts5StorageStmt() call.
215555*/
215556static void sqlite3Fts5StorageStmtRelease(
215557 Fts5Storage *p,
215558 int eStmt,
215559 sqlite3_stmt *pStmt
215560){
215561 assert( eStmt==FTS5_STMT_SCAN_ASC
215562 || eStmt==FTS5_STMT_SCAN_DESC
215563 || eStmt==FTS5_STMT_LOOKUP
215564 );
215565 if( p->aStmt[eStmt]==0 ){
215566 sqlite3_reset(pStmt);
215567 p->aStmt[eStmt] = pStmt;
215568 }else{
215569 sqlite3_finalize(pStmt);
215570 }
215571}
215572
215573static int fts5StorageDecodeSizeArray(
215574 int *aCol, int nCol, /* Array to populate */
215575 const u8 *aBlob, int nBlob /* Record to read varints from */
215576){
215577 int i;
215578 int iOff = 0;
215579 for(i=0; i<nCol; i++){
215580 if( iOff>=nBlob ) return 1;
215581 iOff += fts5GetVarint32(&aBlob[iOff], aCol[i]);
215582 }
215583 return (iOff!=nBlob);
215584}
215585
215586/*
215587** Argument aCol points to an array of integers containing one entry for
215588** each table column. This function reads the %_docsize record for the
215589** specified rowid and populates aCol[] with the results.
215590**
215591** An SQLite error code is returned if an error occurs, or SQLITE_OK
215592** otherwise.
215593*/
215594static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){
215595 int nCol = p->pConfig->nCol; /* Number of user columns in table */
215596 sqlite3_stmt *pLookup = 0; /* Statement to query %_docsize */
215597 int rc; /* Return Code */
215598
215599 assert( p->pConfig->bColumnsize );
215600 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, &pLookup, 0);
215601 if( rc==SQLITE_OK ){
215602 int bCorrupt = 1;
215603 sqlite3_bind_int64(pLookup, 1, iRowid);
215604 if( SQLITE_ROW==sqlite3_step(pLookup) ){
215605 const u8 *aBlob = sqlite3_column_blob(pLookup, 0);
215606 int nBlob = sqlite3_column_bytes(pLookup, 0);
215607 if( 0==fts5StorageDecodeSizeArray(aCol, nCol, aBlob, nBlob) ){
215608 bCorrupt = 0;
215609 }
215610 }
215611 rc = sqlite3_reset(pLookup);
215612 if( bCorrupt && rc==SQLITE_OK ){
215613 rc = FTS5_CORRUPT;
215614 }
215615 }
215616
215617 return rc;
215618}
215619
215620static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
215621 int rc = fts5StorageLoadTotals(p, 0);
215622 if( rc==SQLITE_OK ){
215623 *pnToken = 0;
215624 if( iCol<0 ){
215625 int i;
215626 for(i=0; i<p->pConfig->nCol; i++){
215627 *pnToken += p->aTotalSize[i];
215628 }
215629 }else if( iCol<p->pConfig->nCol ){
215630 *pnToken = p->aTotalSize[iCol];
215631 }else{
215632 rc = SQLITE_RANGE;
215633 }
215634 }
215635 return rc;
215636}
215637
215638static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){
215639 int rc = fts5StorageLoadTotals(p, 0);
215640 if( rc==SQLITE_OK ){
215641 *pnRow = p->nTotalRow;
215642 }
215643 return rc;
215644}
215645
215646/*
215647** Flush any data currently held in-memory to disk.
215648*/
215649static int sqlite3Fts5StorageSync(Fts5Storage *p){
215650 int rc = SQLITE_OK;
215651 i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
215652 if( p->bTotalsValid ){
215653 rc = fts5StorageSaveTotals(p);
215654 p->bTotalsValid = 0;
215655 }
215656 if( rc==SQLITE_OK ){
215657 rc = sqlite3Fts5IndexSync(p->pIndex);
215658 }
215659 sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
215660 return rc;
215661}
215662
215663static int sqlite3Fts5StorageRollback(Fts5Storage *p){
215664 p->bTotalsValid = 0;
215665 return sqlite3Fts5IndexRollback(p->pIndex);
215666}
215667
215668static int sqlite3Fts5StorageConfigValue(
215669 Fts5Storage *p,
215670 const char *z,
215671 sqlite3_value *pVal,
215672 int iVal
215673){
215674 sqlite3_stmt *pReplace = 0;
215675 int rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_CONFIG, &pReplace, 0);
215676 if( rc==SQLITE_OK ){
215677 sqlite3_bind_text(pReplace, 1, z, -1, SQLITE_STATIC);
215678 if( pVal ){
215679 sqlite3_bind_value(pReplace, 2, pVal);
215680 }else{
215681 sqlite3_bind_int(pReplace, 2, iVal);
215682 }
215683 sqlite3_step(pReplace);
215684 rc = sqlite3_reset(pReplace);
215685 sqlite3_bind_null(pReplace, 1);
215686 }
215687 if( rc==SQLITE_OK && pVal ){
215688 int iNew = p->pConfig->iCookie + 1;
215689 rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
215690 if( rc==SQLITE_OK ){
215691 p->pConfig->iCookie = iNew;
215692 }
215693 }
215694 return rc;
215695}
215696
215697/*
215698** 2014 May 31
215699**
215700** The author disclaims copyright to this source code. In place of
215701** a legal notice, here is a blessing:
215702**
215703** May you do good and not evil.
215704** May you find forgiveness for yourself and forgive others.
215705** May you share freely, never taking more than you give.
215706**
215707******************************************************************************
215708*/
215709
215710
215711/* #include "fts5Int.h" */
215712
215713/**************************************************************************
215714** Start of ascii tokenizer implementation.
215715*/
215716
215717/*
215718** For tokenizers with no "unicode" modifier, the set of token characters
215719** is the same as the set of ASCII range alphanumeric characters.
215720*/
215721static unsigned char aAsciiTokenChar[128] = {
215722 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00..0x0F */
215723 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10..0x1F */
215724 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20..0x2F */
215725 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30..0x3F */
215726 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40..0x4F */
215727 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50..0x5F */
215728 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60..0x6F */
215729 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x70..0x7F */
215730};
215731
215732typedef struct AsciiTokenizer AsciiTokenizer;
215733struct AsciiTokenizer {
215734 unsigned char aTokenChar[128];
215735};
215736
215737static void fts5AsciiAddExceptions(
215738 AsciiTokenizer *p,
215739 const char *zArg,
215740 int bTokenChars
215741){
215742 int i;
215743 for(i=0; zArg[i]; i++){
215744 if( (zArg[i] & 0x80)==0 ){
215745 p->aTokenChar[(int)zArg[i]] = (unsigned char)bTokenChars;
215746 }
215747 }
215748}
215749
215750/*
215751** Delete a "ascii" tokenizer.
215752*/
215753static void fts5AsciiDelete(Fts5Tokenizer *p){
215754 sqlite3_free(p);
215755}
215756
215757/*
215758** Create an "ascii" tokenizer.
215759*/
215760static int fts5AsciiCreate(
215761 void *pUnused,
215762 const char **azArg, int nArg,
215763 Fts5Tokenizer **ppOut
215764){
215765 int rc = SQLITE_OK;
215766 AsciiTokenizer *p = 0;
215767 UNUSED_PARAM(pUnused);
215768 if( nArg%2 ){
215769 rc = SQLITE_ERROR;
215770 }else{
215771 p = sqlite3_malloc(sizeof(AsciiTokenizer));
215772 if( p==0 ){
215773 rc = SQLITE_NOMEM;
215774 }else{
215775 int i;
215776 memset(p, 0, sizeof(AsciiTokenizer));
215777 memcpy(p->aTokenChar, aAsciiTokenChar, sizeof(aAsciiTokenChar));
215778 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
215779 const char *zArg = azArg[i+1];
215780 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
215781 fts5AsciiAddExceptions(p, zArg, 1);
215782 }else
215783 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
215784 fts5AsciiAddExceptions(p, zArg, 0);
215785 }else{
215786 rc = SQLITE_ERROR;
215787 }
215788 }
215789 if( rc!=SQLITE_OK ){
215790 fts5AsciiDelete((Fts5Tokenizer*)p);
215791 p = 0;
215792 }
215793 }
215794 }
215795
215796 *ppOut = (Fts5Tokenizer*)p;
215797 return rc;
215798}
215799
215800
215801static void asciiFold(char *aOut, const char *aIn, int nByte){
215802 int i;
215803 for(i=0; i<nByte; i++){
215804 char c = aIn[i];
215805 if( c>='A' && c<='Z' ) c += 32;
215806 aOut[i] = c;
215807 }
215808}
215809
215810/*
215811** Tokenize some text using the ascii tokenizer.
215812*/
215813static int fts5AsciiTokenize(
215814 Fts5Tokenizer *pTokenizer,
215815 void *pCtx,
215816 int iUnused,
215817 const char *pText, int nText,
215818 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
215819){
215820 AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
215821 int rc = SQLITE_OK;
215822 int ie;
215823 int is = 0;
215824
215825 char aFold[64];
215826 int nFold = sizeof(aFold);
215827 char *pFold = aFold;
215828 unsigned char *a = p->aTokenChar;
215829
215830 UNUSED_PARAM(iUnused);
215831
215832 while( is<nText && rc==SQLITE_OK ){
215833 int nByte;
215834
215835 /* Skip any leading divider characters. */
215836 while( is<nText && ((pText[is]&0x80)==0 && a[(int)pText[is]]==0) ){
215837 is++;
215838 }
215839 if( is==nText ) break;
215840
215841 /* Count the token characters */
215842 ie = is+1;
215843 while( ie<nText && ((pText[ie]&0x80) || a[(int)pText[ie]] ) ){
215844 ie++;
215845 }
215846
215847 /* Fold to lower case */
215848 nByte = ie-is;
215849 if( nByte>nFold ){
215850 if( pFold!=aFold ) sqlite3_free(pFold);
215851 pFold = sqlite3_malloc(nByte*2);
215852 if( pFold==0 ){
215853 rc = SQLITE_NOMEM;
215854 break;
215855 }
215856 nFold = nByte*2;
215857 }
215858 asciiFold(pFold, &pText[is], nByte);
215859
215860 /* Invoke the token callback */
215861 rc = xToken(pCtx, 0, pFold, nByte, is, ie);
215862 is = ie+1;
215863 }
215864
215865 if( pFold!=aFold ) sqlite3_free(pFold);
215866 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
215867 return rc;
215868}
215869
215870/**************************************************************************
215871** Start of unicode61 tokenizer implementation.
215872*/
215873
215874
215875/*
215876** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
215877** from the sqlite3 source file utf.c. If this file is compiled as part
215878** of the amalgamation, they are not required.
215879*/
215880#ifndef SQLITE_AMALGAMATION
215881
215882static const unsigned char sqlite3Utf8Trans1[] = {
215883 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
215884 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
215885 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
215886 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
215887 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
215888 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
215889 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
215890 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
215891};
215892
215893#define READ_UTF8(zIn, zTerm, c) \
215894 c = *(zIn++); \
215895 if( c>=0xc0 ){ \
215896 c = sqlite3Utf8Trans1[c-0xc0]; \
215897 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
215898 c = (c<<6) + (0x3f & *(zIn++)); \
215899 } \
215900 if( c<0x80 \
215901 || (c&0xFFFFF800)==0xD800 \
215902 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
215903 }
215904
215905
215906#define WRITE_UTF8(zOut, c) { \
215907 if( c<0x00080 ){ \
215908 *zOut++ = (unsigned char)(c&0xFF); \
215909 } \
215910 else if( c<0x00800 ){ \
215911 *zOut++ = 0xC0 + (unsigned char)((c>>6)&0x1F); \
215912 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
215913 } \
215914 else if( c<0x10000 ){ \
215915 *zOut++ = 0xE0 + (unsigned char)((c>>12)&0x0F); \
215916 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
215917 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
215918 }else{ \
215919 *zOut++ = 0xF0 + (unsigned char)((c>>18) & 0x07); \
215920 *zOut++ = 0x80 + (unsigned char)((c>>12) & 0x3F); \
215921 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
215922 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
215923 } \
215924}
215925
215926#endif /* ifndef SQLITE_AMALGAMATION */
215927
215928typedef struct Unicode61Tokenizer Unicode61Tokenizer;
215929struct Unicode61Tokenizer {
215930 unsigned char aTokenChar[128]; /* ASCII range token characters */
215931 char *aFold; /* Buffer to fold text into */
215932 int nFold; /* Size of aFold[] in bytes */
215933 int bRemoveDiacritic; /* True if remove_diacritics=1 is set */
215934 int nException;
215935 int *aiException;
215936
215937 unsigned char aCategory[32]; /* True for token char categories */
215938};
215939
215940static int fts5UnicodeAddExceptions(
215941 Unicode61Tokenizer *p, /* Tokenizer object */
215942 const char *z, /* Characters to treat as exceptions */
215943 int bTokenChars /* 1 for 'tokenchars', 0 for 'separators' */
215944){
215945 int rc = SQLITE_OK;
215946 int n = (int)strlen(z);
215947 int *aNew;
215948
215949 if( n>0 ){
215950 aNew = (int*)sqlite3_realloc(p->aiException, (n+p->nException)*sizeof(int));
215951 if( aNew ){
215952 int nNew = p->nException;
215953 const unsigned char *zCsr = (const unsigned char*)z;
215954 const unsigned char *zTerm = (const unsigned char*)&z[n];
215955 while( zCsr<zTerm ){
215956 int iCode;
215957 int bToken;
215958 READ_UTF8(zCsr, zTerm, iCode);
215959 if( iCode<128 ){
215960 p->aTokenChar[iCode] = (unsigned char)bTokenChars;
215961 }else{
215962 bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)];
215963 assert( (bToken==0 || bToken==1) );
215964 assert( (bTokenChars==0 || bTokenChars==1) );
215965 if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(iCode)==0 ){
215966 int i;
215967 for(i=0; i<nNew; i++){
215968 if( aNew[i]>iCode ) break;
215969 }
215970 memmove(&aNew[i+1], &aNew[i], (nNew-i)*sizeof(int));
215971 aNew[i] = iCode;
215972 nNew++;
215973 }
215974 }
215975 }
215976 p->aiException = aNew;
215977 p->nException = nNew;
215978 }else{
215979 rc = SQLITE_NOMEM;
215980 }
215981 }
215982
215983 return rc;
215984}
215985
215986/*
215987** Return true if the p->aiException[] array contains the value iCode.
215988*/
215989static int fts5UnicodeIsException(Unicode61Tokenizer *p, int iCode){
215990 if( p->nException>0 ){
215991 int *a = p->aiException;
215992 int iLo = 0;
215993 int iHi = p->nException-1;
215994
215995 while( iHi>=iLo ){
215996 int iTest = (iHi + iLo) / 2;
215997 if( iCode==a[iTest] ){
215998 return 1;
215999 }else if( iCode>a[iTest] ){
216000 iLo = iTest+1;
216001 }else{
216002 iHi = iTest-1;
216003 }
216004 }
216005 }
216006
216007 return 0;
216008}
216009
216010/*
216011** Delete a "unicode61" tokenizer.
216012*/
216013static void fts5UnicodeDelete(Fts5Tokenizer *pTok){
216014 if( pTok ){
216015 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTok;
216016 sqlite3_free(p->aiException);
216017 sqlite3_free(p->aFold);
216018 sqlite3_free(p);
216019 }
216020 return;
216021}
216022
216023static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){
216024 const char *z = zCat;
216025
216026 while( *z ){
216027 while( *z==' ' || *z=='\t' ) z++;
216028 if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){
216029 return SQLITE_ERROR;
216030 }
216031 while( *z!=' ' && *z!='\t' && *z!='\0' ) z++;
216032 }
216033
216034 sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar);
216035 return SQLITE_OK;
216036}
216037
216038/*
216039** Create a "unicode61" tokenizer.
216040*/
216041static int fts5UnicodeCreate(
216042 void *pUnused,
216043 const char **azArg, int nArg,
216044 Fts5Tokenizer **ppOut
216045){
216046 int rc = SQLITE_OK; /* Return code */
216047 Unicode61Tokenizer *p = 0; /* New tokenizer object */
216048
216049 UNUSED_PARAM(pUnused);
216050
216051 if( nArg%2 ){
216052 rc = SQLITE_ERROR;
216053 }else{
216054 p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer));
216055 if( p ){
216056 const char *zCat = "L* N* Co";
216057 int i;
216058 memset(p, 0, sizeof(Unicode61Tokenizer));
216059
216060 p->bRemoveDiacritic = 1;
216061 p->nFold = 64;
216062 p->aFold = sqlite3_malloc(p->nFold * sizeof(char));
216063 if( p->aFold==0 ){
216064 rc = SQLITE_NOMEM;
216065 }
216066
216067 /* Search for a "categories" argument */
216068 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
216069 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
216070 zCat = azArg[i+1];
216071 }
216072 }
216073
216074 if( rc==SQLITE_OK ){
216075 rc = unicodeSetCategories(p, zCat);
216076 }
216077
216078 for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
216079 const char *zArg = azArg[i+1];
216080 if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
216081 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1] ){
216082 rc = SQLITE_ERROR;
216083 }
216084 p->bRemoveDiacritic = (zArg[0]=='1');
216085 }else
216086 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
216087 rc = fts5UnicodeAddExceptions(p, zArg, 1);
216088 }else
216089 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
216090 rc = fts5UnicodeAddExceptions(p, zArg, 0);
216091 }else
216092 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
216093 /* no-op */
216094 }else{
216095 rc = SQLITE_ERROR;
216096 }
216097 }
216098
216099 }else{
216100 rc = SQLITE_NOMEM;
216101 }
216102 if( rc!=SQLITE_OK ){
216103 fts5UnicodeDelete((Fts5Tokenizer*)p);
216104 p = 0;
216105 }
216106 *ppOut = (Fts5Tokenizer*)p;
216107 }
216108 return rc;
216109}
216110
216111/*
216112** Return true if, for the purposes of tokenizing with the tokenizer
216113** passed as the first argument, codepoint iCode is considered a token
216114** character (not a separator).
216115*/
216116static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){
216117 return (
216118 p->aCategory[sqlite3Fts5UnicodeCategory(iCode)]
216119 ^ fts5UnicodeIsException(p, iCode)
216120 );
216121}
216122
216123static int fts5UnicodeTokenize(
216124 Fts5Tokenizer *pTokenizer,
216125 void *pCtx,
216126 int iUnused,
216127 const char *pText, int nText,
216128 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
216129){
216130 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
216131 int rc = SQLITE_OK;
216132 unsigned char *a = p->aTokenChar;
216133
216134 unsigned char *zTerm = (unsigned char*)&pText[nText];
216135 unsigned char *zCsr = (unsigned char *)pText;
216136
216137 /* Output buffer */
216138 char *aFold = p->aFold;
216139 int nFold = p->nFold;
216140 const char *pEnd = &aFold[nFold-6];
216141
216142 UNUSED_PARAM(iUnused);
216143
216144 /* Each iteration of this loop gobbles up a contiguous run of separators,
216145 ** then the next token. */
216146 while( rc==SQLITE_OK ){
216147 int iCode; /* non-ASCII codepoint read from input */
216148 char *zOut = aFold;
216149 int is;
216150 int ie;
216151
216152 /* Skip any separator characters. */
216153 while( 1 ){
216154 if( zCsr>=zTerm ) goto tokenize_done;
216155 if( *zCsr & 0x80 ) {
216156 /* A character outside of the ascii range. Skip past it if it is
216157 ** a separator character. Or break out of the loop if it is not. */
216158 is = zCsr - (unsigned char*)pText;
216159 READ_UTF8(zCsr, zTerm, iCode);
216160 if( fts5UnicodeIsAlnum(p, iCode) ){
216161 goto non_ascii_tokenchar;
216162 }
216163 }else{
216164 if( a[*zCsr] ){
216165 is = zCsr - (unsigned char*)pText;
216166 goto ascii_tokenchar;
216167 }
216168 zCsr++;
216169 }
216170 }
216171
216172 /* Run through the tokenchars. Fold them into the output buffer along
216173 ** the way. */
216174 while( zCsr<zTerm ){
216175
216176 /* Grow the output buffer so that there is sufficient space to fit the
216177 ** largest possible utf-8 character. */
216178 if( zOut>pEnd ){
216179 aFold = sqlite3_malloc(nFold*2);
216180 if( aFold==0 ){
216181 rc = SQLITE_NOMEM;
216182 goto tokenize_done;
216183 }
216184 zOut = &aFold[zOut - p->aFold];
216185 memcpy(aFold, p->aFold, nFold);
216186 sqlite3_free(p->aFold);
216187 p->aFold = aFold;
216188 p->nFold = nFold = nFold*2;
216189 pEnd = &aFold[nFold-6];
216190 }
216191
216192 if( *zCsr & 0x80 ){
216193 /* An non-ascii-range character. Fold it into the output buffer if
216194 ** it is a token character, or break out of the loop if it is not. */
216195 READ_UTF8(zCsr, zTerm, iCode);
216196 if( fts5UnicodeIsAlnum(p,iCode)||sqlite3Fts5UnicodeIsdiacritic(iCode) ){
216197 non_ascii_tokenchar:
216198 iCode = sqlite3Fts5UnicodeFold(iCode, p->bRemoveDiacritic);
216199 if( iCode ) WRITE_UTF8(zOut, iCode);
216200 }else{
216201 break;
216202 }
216203 }else if( a[*zCsr]==0 ){
216204 /* An ascii-range separator character. End of token. */
216205 break;
216206 }else{
216207 ascii_tokenchar:
216208 if( *zCsr>='A' && *zCsr<='Z' ){
216209 *zOut++ = *zCsr + 32;
216210 }else{
216211 *zOut++ = *zCsr;
216212 }
216213 zCsr++;
216214 }
216215 ie = zCsr - (unsigned char*)pText;
216216 }
216217
216218 /* Invoke the token callback */
216219 rc = xToken(pCtx, 0, aFold, zOut-aFold, is, ie);
216220 }
216221
216222 tokenize_done:
216223 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
216224 return rc;
216225}
216226
216227/**************************************************************************
216228** Start of porter stemmer implementation.
216229*/
216230
216231/* Any tokens larger than this (in bytes) are passed through without
216232** stemming. */
216233#define FTS5_PORTER_MAX_TOKEN 64
216234
216235typedef struct PorterTokenizer PorterTokenizer;
216236struct PorterTokenizer {
216237 fts5_tokenizer tokenizer; /* Parent tokenizer module */
216238 Fts5Tokenizer *pTokenizer; /* Parent tokenizer instance */
216239 char aBuf[FTS5_PORTER_MAX_TOKEN + 64];
216240};
216241
216242/*
216243** Delete a "porter" tokenizer.
216244*/
216245static void fts5PorterDelete(Fts5Tokenizer *pTok){
216246 if( pTok ){
216247 PorterTokenizer *p = (PorterTokenizer*)pTok;
216248 if( p->pTokenizer ){
216249 p->tokenizer.xDelete(p->pTokenizer);
216250 }
216251 sqlite3_free(p);
216252 }
216253}
216254
216255/*
216256** Create a "porter" tokenizer.
216257*/
216258static int fts5PorterCreate(
216259 void *pCtx,
216260 const char **azArg, int nArg,
216261 Fts5Tokenizer **ppOut
216262){
216263 fts5_api *pApi = (fts5_api*)pCtx;
216264 int rc = SQLITE_OK;
216265 PorterTokenizer *pRet;
216266 void *pUserdata = 0;
216267 const char *zBase = "unicode61";
216268
216269 if( nArg>0 ){
216270 zBase = azArg[0];
216271 }
216272
216273 pRet = (PorterTokenizer*)sqlite3_malloc(sizeof(PorterTokenizer));
216274 if( pRet ){
216275 memset(pRet, 0, sizeof(PorterTokenizer));
216276 rc = pApi->xFindTokenizer(pApi, zBase, &pUserdata, &pRet->tokenizer);
216277 }else{
216278 rc = SQLITE_NOMEM;
216279 }
216280 if( rc==SQLITE_OK ){
216281 int nArg2 = (nArg>0 ? nArg-1 : 0);
216282 const char **azArg2 = (nArg2 ? &azArg[1] : 0);
216283 rc = pRet->tokenizer.xCreate(pUserdata, azArg2, nArg2, &pRet->pTokenizer);
216284 }
216285
216286 if( rc!=SQLITE_OK ){
216287 fts5PorterDelete((Fts5Tokenizer*)pRet);
216288 pRet = 0;
216289 }
216290 *ppOut = (Fts5Tokenizer*)pRet;
216291 return rc;
216292}
216293
216294typedef struct PorterContext PorterContext;
216295struct PorterContext {
216296 void *pCtx;
216297 int (*xToken)(void*, int, const char*, int, int, int);
216298 char *aBuf;
216299};
216300
216301typedef struct PorterRule PorterRule;
216302struct PorterRule {
216303 const char *zSuffix;
216304 int nSuffix;
216305 int (*xCond)(char *zStem, int nStem);
216306 const char *zOutput;
216307 int nOutput;
216308};
216309
216310#if 0
216311static int fts5PorterApply(char *aBuf, int *pnBuf, PorterRule *aRule){
216312 int ret = -1;
216313 int nBuf = *pnBuf;
216314 PorterRule *p;
216315
216316 for(p=aRule; p->zSuffix; p++){
216317 assert( strlen(p->zSuffix)==p->nSuffix );
216318 assert( strlen(p->zOutput)==p->nOutput );
216319 if( nBuf<p->nSuffix ) continue;
216320 if( 0==memcmp(&aBuf[nBuf - p->nSuffix], p->zSuffix, p->nSuffix) ) break;
216321 }
216322
216323 if( p->zSuffix ){
216324 int nStem = nBuf - p->nSuffix;
216325 if( p->xCond==0 || p->xCond(aBuf, nStem) ){
216326 memcpy(&aBuf[nStem], p->zOutput, p->nOutput);
216327 *pnBuf = nStem + p->nOutput;
216328 ret = p - aRule;
216329 }
216330 }
216331
216332 return ret;
216333}
216334#endif
216335
216336static int fts5PorterIsVowel(char c, int bYIsVowel){
216337 return (
216338 c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || (bYIsVowel && c=='y')
216339 );
216340}
216341
216342static int fts5PorterGobbleVC(char *zStem, int nStem, int bPrevCons){
216343 int i;
216344 int bCons = bPrevCons;
216345
216346 /* Scan for a vowel */
216347 for(i=0; i<nStem; i++){
216348 if( 0==(bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) break;
216349 }
216350
216351 /* Scan for a consonent */
216352 for(i++; i<nStem; i++){
216353 if( (bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) return i+1;
216354 }
216355 return 0;
216356}
216357
216358/* porter rule condition: (m > 0) */
216359static int fts5Porter_MGt0(char *zStem, int nStem){
216360 return !!fts5PorterGobbleVC(zStem, nStem, 0);
216361}
216362
216363/* porter rule condition: (m > 1) */
216364static int fts5Porter_MGt1(char *zStem, int nStem){
216365 int n;
216366 n = fts5PorterGobbleVC(zStem, nStem, 0);
216367 if( n && fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
216368 return 1;
216369 }
216370 return 0;
216371}
216372
216373/* porter rule condition: (m = 1) */
216374static int fts5Porter_MEq1(char *zStem, int nStem){
216375 int n;
216376 n = fts5PorterGobbleVC(zStem, nStem, 0);
216377 if( n && 0==fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
216378 return 1;
216379 }
216380 return 0;
216381}
216382
216383/* porter rule condition: (*o) */
216384static int fts5Porter_Ostar(char *zStem, int nStem){
216385 if( zStem[nStem-1]=='w' || zStem[nStem-1]=='x' || zStem[nStem-1]=='y' ){
216386 return 0;
216387 }else{
216388 int i;
216389 int mask = 0;
216390 int bCons = 0;
216391 for(i=0; i<nStem; i++){
216392 bCons = !fts5PorterIsVowel(zStem[i], bCons);
216393 assert( bCons==0 || bCons==1 );
216394 mask = (mask << 1) + bCons;
216395 }
216396 return ((mask & 0x0007)==0x0005);
216397 }
216398}
216399
216400/* porter rule condition: (m > 1 and (*S or *T)) */
216401static int fts5Porter_MGt1_and_S_or_T(char *zStem, int nStem){
216402 assert( nStem>0 );
216403 return (zStem[nStem-1]=='s' || zStem[nStem-1]=='t')
216404 && fts5Porter_MGt1(zStem, nStem);
216405}
216406
216407/* porter rule condition: (*v*) */
216408static int fts5Porter_Vowel(char *zStem, int nStem){
216409 int i;
216410 for(i=0; i<nStem; i++){
216411 if( fts5PorterIsVowel(zStem[i], i>0) ){
216412 return 1;
216413 }
216414 }
216415 return 0;
216416}
216417
216418
216419/**************************************************************************
216420***************************************************************************
216421** GENERATED CODE STARTS HERE (mkportersteps.tcl)
216422*/
216423
216424static int fts5PorterStep4(char *aBuf, int *pnBuf){
216425 int ret = 0;
216426 int nBuf = *pnBuf;
216427 switch( aBuf[nBuf-2] ){
216428
216429 case 'a':
216430 if( nBuf>2 && 0==memcmp("al", &aBuf[nBuf-2], 2) ){
216431 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
216432 *pnBuf = nBuf - 2;
216433 }
216434 }
216435 break;
216436
216437 case 'c':
216438 if( nBuf>4 && 0==memcmp("ance", &aBuf[nBuf-4], 4) ){
216439 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
216440 *pnBuf = nBuf - 4;
216441 }
216442 }else if( nBuf>4 && 0==memcmp("ence", &aBuf[nBuf-4], 4) ){
216443 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
216444 *pnBuf = nBuf - 4;
216445 }
216446 }
216447 break;
216448
216449 case 'e':
216450 if( nBuf>2 && 0==memcmp("er", &aBuf[nBuf-2], 2) ){
216451 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
216452 *pnBuf = nBuf - 2;
216453 }
216454 }
216455 break;
216456
216457 case 'i':
216458 if( nBuf>2 && 0==memcmp("ic", &aBuf[nBuf-2], 2) ){
216459 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
216460 *pnBuf = nBuf - 2;
216461 }
216462 }
216463 break;
216464
216465 case 'l':
216466 if( nBuf>4 && 0==memcmp("able", &aBuf[nBuf-4], 4) ){
216467 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
216468 *pnBuf = nBuf - 4;
216469 }
216470 }else if( nBuf>4 && 0==memcmp("ible", &aBuf[nBuf-4], 4) ){
216471 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
216472 *pnBuf = nBuf - 4;
216473 }
216474 }
216475 break;
216476
216477 case 'n':
216478 if( nBuf>3 && 0==memcmp("ant", &aBuf[nBuf-3], 3) ){
216479 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
216480 *pnBuf = nBuf - 3;
216481 }
216482 }else if( nBuf>5 && 0==memcmp("ement", &aBuf[nBuf-5], 5) ){
216483 if( fts5Porter_MGt1(aBuf, nBuf-5) ){
216484 *pnBuf = nBuf - 5;
216485 }
216486 }else if( nBuf>4 && 0==memcmp("ment", &aBuf[nBuf-4], 4) ){
216487 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
216488 *pnBuf = nBuf - 4;
216489 }
216490 }else if( nBuf>3 && 0==memcmp("ent", &aBuf[nBuf-3], 3) ){
216491 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
216492 *pnBuf = nBuf - 3;
216493 }
216494 }
216495 break;
216496
216497 case 'o':
216498 if( nBuf>3 && 0==memcmp("ion", &aBuf[nBuf-3], 3) ){
216499 if( fts5Porter_MGt1_and_S_or_T(aBuf, nBuf-3) ){
216500 *pnBuf = nBuf - 3;
216501 }
216502 }else if( nBuf>2 && 0==memcmp("ou", &aBuf[nBuf-2], 2) ){
216503 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
216504 *pnBuf = nBuf - 2;
216505 }
216506 }
216507 break;
216508
216509 case 's':
216510 if( nBuf>3 && 0==memcmp("ism", &aBuf[nBuf-3], 3) ){
216511 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
216512 *pnBuf = nBuf - 3;
216513 }
216514 }
216515 break;
216516
216517 case 't':
216518 if( nBuf>3 && 0==memcmp("ate", &aBuf[nBuf-3], 3) ){
216519 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
216520 *pnBuf = nBuf - 3;
216521 }
216522 }else if( nBuf>3 && 0==memcmp("iti", &aBuf[nBuf-3], 3) ){
216523 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
216524 *pnBuf = nBuf - 3;
216525 }
216526 }
216527 break;
216528
216529 case 'u':
216530 if( nBuf>3 && 0==memcmp("ous", &aBuf[nBuf-3], 3) ){
216531 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
216532 *pnBuf = nBuf - 3;
216533 }
216534 }
216535 break;
216536
216537 case 'v':
216538 if( nBuf>3 && 0==memcmp("ive", &aBuf[nBuf-3], 3) ){
216539 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
216540 *pnBuf = nBuf - 3;
216541 }
216542 }
216543 break;
216544
216545 case 'z':
216546 if( nBuf>3 && 0==memcmp("ize", &aBuf[nBuf-3], 3) ){
216547 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
216548 *pnBuf = nBuf - 3;
216549 }
216550 }
216551 break;
216552
216553 }
216554 return ret;
216555}
216556
216557
216558static int fts5PorterStep1B2(char *aBuf, int *pnBuf){
216559 int ret = 0;
216560 int nBuf = *pnBuf;
216561 switch( aBuf[nBuf-2] ){
216562
216563 case 'a':
216564 if( nBuf>2 && 0==memcmp("at", &aBuf[nBuf-2], 2) ){
216565 memcpy(&aBuf[nBuf-2], "ate", 3);
216566 *pnBuf = nBuf - 2 + 3;
216567 ret = 1;
216568 }
216569 break;
216570
216571 case 'b':
216572 if( nBuf>2 && 0==memcmp("bl", &aBuf[nBuf-2], 2) ){
216573 memcpy(&aBuf[nBuf-2], "ble", 3);
216574 *pnBuf = nBuf - 2 + 3;
216575 ret = 1;
216576 }
216577 break;
216578
216579 case 'i':
216580 if( nBuf>2 && 0==memcmp("iz", &aBuf[nBuf-2], 2) ){
216581 memcpy(&aBuf[nBuf-2], "ize", 3);
216582 *pnBuf = nBuf - 2 + 3;
216583 ret = 1;
216584 }
216585 break;
216586
216587 }
216588 return ret;
216589}
216590
216591
216592static int fts5PorterStep2(char *aBuf, int *pnBuf){
216593 int ret = 0;
216594 int nBuf = *pnBuf;
216595 switch( aBuf[nBuf-2] ){
216596
216597 case 'a':
216598 if( nBuf>7 && 0==memcmp("ational", &aBuf[nBuf-7], 7) ){
216599 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
216600 memcpy(&aBuf[nBuf-7], "ate", 3);
216601 *pnBuf = nBuf - 7 + 3;
216602 }
216603 }else if( nBuf>6 && 0==memcmp("tional", &aBuf[nBuf-6], 6) ){
216604 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
216605 memcpy(&aBuf[nBuf-6], "tion", 4);
216606 *pnBuf = nBuf - 6 + 4;
216607 }
216608 }
216609 break;
216610
216611 case 'c':
216612 if( nBuf>4 && 0==memcmp("enci", &aBuf[nBuf-4], 4) ){
216613 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
216614 memcpy(&aBuf[nBuf-4], "ence", 4);
216615 *pnBuf = nBuf - 4 + 4;
216616 }
216617 }else if( nBuf>4 && 0==memcmp("anci", &aBuf[nBuf-4], 4) ){
216618 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
216619 memcpy(&aBuf[nBuf-4], "ance", 4);
216620 *pnBuf = nBuf - 4 + 4;
216621 }
216622 }
216623 break;
216624
216625 case 'e':
216626 if( nBuf>4 && 0==memcmp("izer", &aBuf[nBuf-4], 4) ){
216627 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
216628 memcpy(&aBuf[nBuf-4], "ize", 3);
216629 *pnBuf = nBuf - 4 + 3;
216630 }
216631 }
216632 break;
216633
216634 case 'g':
216635 if( nBuf>4 && 0==memcmp("logi", &aBuf[nBuf-4], 4) ){
216636 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
216637 memcpy(&aBuf[nBuf-4], "log", 3);
216638 *pnBuf = nBuf - 4 + 3;
216639 }
216640 }
216641 break;
216642
216643 case 'l':
216644 if( nBuf>3 && 0==memcmp("bli", &aBuf[nBuf-3], 3) ){
216645 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
216646 memcpy(&aBuf[nBuf-3], "ble", 3);
216647 *pnBuf = nBuf - 3 + 3;
216648 }
216649 }else if( nBuf>4 && 0==memcmp("alli", &aBuf[nBuf-4], 4) ){
216650 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
216651 memcpy(&aBuf[nBuf-4], "al", 2);
216652 *pnBuf = nBuf - 4 + 2;
216653 }
216654 }else if( nBuf>5 && 0==memcmp("entli", &aBuf[nBuf-5], 5) ){
216655 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
216656 memcpy(&aBuf[nBuf-5], "ent", 3);
216657 *pnBuf = nBuf - 5 + 3;
216658 }
216659 }else if( nBuf>3 && 0==memcmp("eli", &aBuf[nBuf-3], 3) ){
216660 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
216661 memcpy(&aBuf[nBuf-3], "e", 1);
216662 *pnBuf = nBuf - 3 + 1;
216663 }
216664 }else if( nBuf>5 && 0==memcmp("ousli", &aBuf[nBuf-5], 5) ){
216665 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
216666 memcpy(&aBuf[nBuf-5], "ous", 3);
216667 *pnBuf = nBuf - 5 + 3;
216668 }
216669 }
216670 break;
216671
216672 case 'o':
216673 if( nBuf>7 && 0==memcmp("ization", &aBuf[nBuf-7], 7) ){
216674 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
216675 memcpy(&aBuf[nBuf-7], "ize", 3);
216676 *pnBuf = nBuf - 7 + 3;
216677 }
216678 }else if( nBuf>5 && 0==memcmp("ation", &aBuf[nBuf-5], 5) ){
216679 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
216680 memcpy(&aBuf[nBuf-5], "ate", 3);
216681 *pnBuf = nBuf - 5 + 3;
216682 }
216683 }else if( nBuf>4 && 0==memcmp("ator", &aBuf[nBuf-4], 4) ){
216684 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
216685 memcpy(&aBuf[nBuf-4], "ate", 3);
216686 *pnBuf = nBuf - 4 + 3;
216687 }
216688 }
216689 break;
216690
216691 case 's':
216692 if( nBuf>5 && 0==memcmp("alism", &aBuf[nBuf-5], 5) ){
216693 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
216694 memcpy(&aBuf[nBuf-5], "al", 2);
216695 *pnBuf = nBuf - 5 + 2;
216696 }
216697 }else if( nBuf>7 && 0==memcmp("iveness", &aBuf[nBuf-7], 7) ){
216698 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
216699 memcpy(&aBuf[nBuf-7], "ive", 3);
216700 *pnBuf = nBuf - 7 + 3;
216701 }
216702 }else if( nBuf>7 && 0==memcmp("fulness", &aBuf[nBuf-7], 7) ){
216703 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
216704 memcpy(&aBuf[nBuf-7], "ful", 3);
216705 *pnBuf = nBuf - 7 + 3;
216706 }
216707 }else if( nBuf>7 && 0==memcmp("ousness", &aBuf[nBuf-7], 7) ){
216708 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
216709 memcpy(&aBuf[nBuf-7], "ous", 3);
216710 *pnBuf = nBuf - 7 + 3;
216711 }
216712 }
216713 break;
216714
216715 case 't':
216716 if( nBuf>5 && 0==memcmp("aliti", &aBuf[nBuf-5], 5) ){
216717 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
216718 memcpy(&aBuf[nBuf-5], "al", 2);
216719 *pnBuf = nBuf - 5 + 2;
216720 }
216721 }else if( nBuf>5 && 0==memcmp("iviti", &aBuf[nBuf-5], 5) ){
216722 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
216723 memcpy(&aBuf[nBuf-5], "ive", 3);
216724 *pnBuf = nBuf - 5 + 3;
216725 }
216726 }else if( nBuf>6 && 0==memcmp("biliti", &aBuf[nBuf-6], 6) ){
216727 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
216728 memcpy(&aBuf[nBuf-6], "ble", 3);
216729 *pnBuf = nBuf - 6 + 3;
216730 }
216731 }
216732 break;
216733
216734 }
216735 return ret;
216736}
216737
216738
216739static int fts5PorterStep3(char *aBuf, int *pnBuf){
216740 int ret = 0;
216741 int nBuf = *pnBuf;
216742 switch( aBuf[nBuf-2] ){
216743
216744 case 'a':
216745 if( nBuf>4 && 0==memcmp("ical", &aBuf[nBuf-4], 4) ){
216746 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
216747 memcpy(&aBuf[nBuf-4], "ic", 2);
216748 *pnBuf = nBuf - 4 + 2;
216749 }
216750 }
216751 break;
216752
216753 case 's':
216754 if( nBuf>4 && 0==memcmp("ness", &aBuf[nBuf-4], 4) ){
216755 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
216756 *pnBuf = nBuf - 4;
216757 }
216758 }
216759 break;
216760
216761 case 't':
216762 if( nBuf>5 && 0==memcmp("icate", &aBuf[nBuf-5], 5) ){
216763 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
216764 memcpy(&aBuf[nBuf-5], "ic", 2);
216765 *pnBuf = nBuf - 5 + 2;
216766 }
216767 }else if( nBuf>5 && 0==memcmp("iciti", &aBuf[nBuf-5], 5) ){
216768 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
216769 memcpy(&aBuf[nBuf-5], "ic", 2);
216770 *pnBuf = nBuf - 5 + 2;
216771 }
216772 }
216773 break;
216774
216775 case 'u':
216776 if( nBuf>3 && 0==memcmp("ful", &aBuf[nBuf-3], 3) ){
216777 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
216778 *pnBuf = nBuf - 3;
216779 }
216780 }
216781 break;
216782
216783 case 'v':
216784 if( nBuf>5 && 0==memcmp("ative", &aBuf[nBuf-5], 5) ){
216785 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
216786 *pnBuf = nBuf - 5;
216787 }
216788 }
216789 break;
216790
216791 case 'z':
216792 if( nBuf>5 && 0==memcmp("alize", &aBuf[nBuf-5], 5) ){
216793 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
216794 memcpy(&aBuf[nBuf-5], "al", 2);
216795 *pnBuf = nBuf - 5 + 2;
216796 }
216797 }
216798 break;
216799
216800 }
216801 return ret;
216802}
216803
216804
216805static int fts5PorterStep1B(char *aBuf, int *pnBuf){
216806 int ret = 0;
216807 int nBuf = *pnBuf;
216808 switch( aBuf[nBuf-2] ){
216809
216810 case 'e':
216811 if( nBuf>3 && 0==memcmp("eed", &aBuf[nBuf-3], 3) ){
216812 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
216813 memcpy(&aBuf[nBuf-3], "ee", 2);
216814 *pnBuf = nBuf - 3 + 2;
216815 }
216816 }else if( nBuf>2 && 0==memcmp("ed", &aBuf[nBuf-2], 2) ){
216817 if( fts5Porter_Vowel(aBuf, nBuf-2) ){
216818 *pnBuf = nBuf - 2;
216819 ret = 1;
216820 }
216821 }
216822 break;
216823
216824 case 'n':
216825 if( nBuf>3 && 0==memcmp("ing", &aBuf[nBuf-3], 3) ){
216826 if( fts5Porter_Vowel(aBuf, nBuf-3) ){
216827 *pnBuf = nBuf - 3;
216828 ret = 1;
216829 }
216830 }
216831 break;
216832
216833 }
216834 return ret;
216835}
216836
216837/*
216838** GENERATED CODE ENDS HERE (mkportersteps.tcl)
216839***************************************************************************
216840**************************************************************************/
216841
216842static void fts5PorterStep1A(char *aBuf, int *pnBuf){
216843 int nBuf = *pnBuf;
216844 if( aBuf[nBuf-1]=='s' ){
216845 if( aBuf[nBuf-2]=='e' ){
216846 if( (nBuf>4 && aBuf[nBuf-4]=='s' && aBuf[nBuf-3]=='s')
216847 || (nBuf>3 && aBuf[nBuf-3]=='i' )
216848 ){
216849 *pnBuf = nBuf-2;
216850 }else{
216851 *pnBuf = nBuf-1;
216852 }
216853 }
216854 else if( aBuf[nBuf-2]!='s' ){
216855 *pnBuf = nBuf-1;
216856 }
216857 }
216858}
216859
216860static int fts5PorterCb(
216861 void *pCtx,
216862 int tflags,
216863 const char *pToken,
216864 int nToken,
216865 int iStart,
216866 int iEnd
216867){
216868 PorterContext *p = (PorterContext*)pCtx;
216869
216870 char *aBuf;
216871 int nBuf;
216872
216873 if( nToken>FTS5_PORTER_MAX_TOKEN || nToken<3 ) goto pass_through;
216874 aBuf = p->aBuf;
216875 nBuf = nToken;
216876 memcpy(aBuf, pToken, nBuf);
216877
216878 /* Step 1. */
216879 fts5PorterStep1A(aBuf, &nBuf);
216880 if( fts5PorterStep1B(aBuf, &nBuf) ){
216881 if( fts5PorterStep1B2(aBuf, &nBuf)==0 ){
216882 char c = aBuf[nBuf-1];
216883 if( fts5PorterIsVowel(c, 0)==0
216884 && c!='l' && c!='s' && c!='z' && c==aBuf[nBuf-2]
216885 ){
216886 nBuf--;
216887 }else if( fts5Porter_MEq1(aBuf, nBuf) && fts5Porter_Ostar(aBuf, nBuf) ){
216888 aBuf[nBuf++] = 'e';
216889 }
216890 }
216891 }
216892
216893 /* Step 1C. */
216894 if( aBuf[nBuf-1]=='y' && fts5Porter_Vowel(aBuf, nBuf-1) ){
216895 aBuf[nBuf-1] = 'i';
216896 }
216897
216898 /* Steps 2 through 4. */
216899 fts5PorterStep2(aBuf, &nBuf);
216900 fts5PorterStep3(aBuf, &nBuf);
216901 fts5PorterStep4(aBuf, &nBuf);
216902
216903 /* Step 5a. */
216904 assert( nBuf>0 );
216905 if( aBuf[nBuf-1]=='e' ){
216906 if( fts5Porter_MGt1(aBuf, nBuf-1)
216907 || (fts5Porter_MEq1(aBuf, nBuf-1) && !fts5Porter_Ostar(aBuf, nBuf-1))
216908 ){
216909 nBuf--;
216910 }
216911 }
216912
216913 /* Step 5b. */
216914 if( nBuf>1 && aBuf[nBuf-1]=='l'
216915 && aBuf[nBuf-2]=='l' && fts5Porter_MGt1(aBuf, nBuf-1)
216916 ){
216917 nBuf--;
216918 }
216919
216920 return p->xToken(p->pCtx, tflags, aBuf, nBuf, iStart, iEnd);
216921
216922 pass_through:
216923 return p->xToken(p->pCtx, tflags, pToken, nToken, iStart, iEnd);
216924}
216925
216926/*
216927** Tokenize using the porter tokenizer.
216928*/
216929static int fts5PorterTokenize(
216930 Fts5Tokenizer *pTokenizer,
216931 void *pCtx,
216932 int flags,
216933 const char *pText, int nText,
216934 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
216935){
216936 PorterTokenizer *p = (PorterTokenizer*)pTokenizer;
216937 PorterContext sCtx;
216938 sCtx.xToken = xToken;
216939 sCtx.pCtx = pCtx;
216940 sCtx.aBuf = p->aBuf;
216941 return p->tokenizer.xTokenize(
216942 p->pTokenizer, (void*)&sCtx, flags, pText, nText, fts5PorterCb
216943 );
216944}
216945
216946/*
216947** Register all built-in tokenizers with FTS5.
216948*/
216949static int sqlite3Fts5TokenizerInit(fts5_api *pApi){
216950 struct BuiltinTokenizer {
216951 const char *zName;
216952 fts5_tokenizer x;
216953 } aBuiltin[] = {
216954 { "unicode61", {fts5UnicodeCreate, fts5UnicodeDelete, fts5UnicodeTokenize}},
216955 { "ascii", {fts5AsciiCreate, fts5AsciiDelete, fts5AsciiTokenize }},
216956 { "porter", {fts5PorterCreate, fts5PorterDelete, fts5PorterTokenize }},
216957 };
216958
216959 int rc = SQLITE_OK; /* Return code */
216960 int i; /* To iterate through builtin functions */
216961
216962 for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
216963 rc = pApi->xCreateTokenizer(pApi,
216964 aBuiltin[i].zName,
216965 (void*)pApi,
216966 &aBuiltin[i].x,
216967 0
216968 );
216969 }
216970
216971 return rc;
216972}
216973
216974
216975
216976/*
216977** 2012 May 25
216978**
216979** The author disclaims copyright to this source code. In place of
216980** a legal notice, here is a blessing:
216981**
216982** May you do good and not evil.
216983** May you find forgiveness for yourself and forgive others.
216984** May you share freely, never taking more than you give.
216985**
216986******************************************************************************
216987*/
216988
216989/*
216990** DO NOT EDIT THIS MACHINE GENERATED FILE.
216991*/
216992
216993
216994/* #include <assert.h> */
216995
216996
216997
216998/*
216999** If the argument is a codepoint corresponding to a lowercase letter
217000** in the ASCII range with a diacritic added, return the codepoint
217001** of the ASCII letter only. For example, if passed 235 - "LATIN
217002** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
217003** E"). The resuls of passing a codepoint that corresponds to an
217004** uppercase letter are undefined.
217005*/
217006static int fts5_remove_diacritic(int c){
217007 unsigned short aDia[] = {
217008 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
217009 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
217010 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
217011 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
217012 3456, 3696, 3712, 3728, 3744, 3896, 3912, 3928,
217013 3968, 4008, 4040, 4106, 4138, 4170, 4202, 4234,
217014 4266, 4296, 4312, 4344, 4408, 4424, 4472, 4504,
217015 6148, 6198, 6264, 6280, 6360, 6429, 6505, 6529,
217016 61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726,
217017 61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122,
217018 62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536,
217019 62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730,
217020 62924, 63050, 63082, 63274, 63390,
217021 };
217022 char aChar[] = {
217023 '\0', 'a', 'c', 'e', 'i', 'n', 'o', 'u', 'y', 'y', 'a', 'c',
217024 'd', 'e', 'e', 'g', 'h', 'i', 'j', 'k', 'l', 'n', 'o', 'r',
217025 's', 't', 'u', 'u', 'w', 'y', 'z', 'o', 'u', 'a', 'i', 'o',
217026 'u', 'g', 'k', 'o', 'j', 'g', 'n', 'a', 'e', 'i', 'o', 'r',
217027 'u', 's', 't', 'h', 'a', 'e', 'o', 'y', '\0', '\0', '\0', '\0',
217028 '\0', '\0', '\0', '\0', 'a', 'b', 'd', 'd', 'e', 'f', 'g', 'h',
217029 'h', 'i', 'k', 'l', 'l', 'm', 'n', 'p', 'r', 'r', 's', 't',
217030 'u', 'v', 'w', 'w', 'x', 'y', 'z', 'h', 't', 'w', 'y', 'a',
217031 'e', 'i', 'o', 'u', 'y',
217032 };
217033
217034 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
217035 int iRes = 0;
217036 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
217037 int iLo = 0;
217038 while( iHi>=iLo ){
217039 int iTest = (iHi + iLo) / 2;
217040 if( key >= aDia[iTest] ){
217041 iRes = iTest;
217042 iLo = iTest+1;
217043 }else{
217044 iHi = iTest-1;
217045 }
217046 }
217047 assert( key>=aDia[iRes] );
217048 return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
217049}
217050
217051
217052/*
217053** Return true if the argument interpreted as a unicode codepoint
217054** is a diacritical modifier character.
217055*/
217056static int sqlite3Fts5UnicodeIsdiacritic(int c){
217057 unsigned int mask0 = 0x08029FDF;
217058 unsigned int mask1 = 0x000361F8;
217059 if( c<768 || c>817 ) return 0;
217060 return (c < 768+32) ?
217061 (mask0 & (1 << (c-768))) :
217062 (mask1 & (1 << (c-768-32)));
217063}
217064
217065
217066/*
217067** Interpret the argument as a unicode codepoint. If the codepoint
217068** is an upper case character that has a lower case equivalent,
217069** return the codepoint corresponding to the lower case version.
217070** Otherwise, return a copy of the argument.
217071**
217072** The results are undefined if the value passed to this function
217073** is less than zero.
217074*/
217075static int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic){
217076 /* Each entry in the following array defines a rule for folding a range
217077 ** of codepoints to lower case. The rule applies to a range of nRange
217078 ** codepoints starting at codepoint iCode.
217079 **
217080 ** If the least significant bit in flags is clear, then the rule applies
217081 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
217082 ** need to be folded). Or, if it is set, then the rule only applies to
217083 ** every second codepoint in the range, starting with codepoint C.
217084 **
217085 ** The 7 most significant bits in flags are an index into the aiOff[]
217086 ** array. If a specific codepoint C does require folding, then its lower
217087 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
217088 **
217089 ** The contents of this array are generated by parsing the CaseFolding.txt
217090 ** file distributed as part of the "Unicode Character Database". See
217091 ** http://www.unicode.org for details.
217092 */
217093 static const struct TableEntry {
217094 unsigned short iCode;
217095 unsigned char flags;
217096 unsigned char nRange;
217097 } aEntry[] = {
217098 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
217099 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
217100 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
217101 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
217102 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
217103 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
217104 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
217105 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
217106 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
217107 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
217108 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
217109 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
217110 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
217111 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
217112 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
217113 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
217114 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
217115 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
217116 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
217117 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
217118 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
217119 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
217120 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
217121 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
217122 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
217123 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
217124 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
217125 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
217126 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
217127 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
217128 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
217129 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
217130 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
217131 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
217132 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
217133 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
217134 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
217135 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
217136 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
217137 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
217138 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
217139 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
217140 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
217141 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
217142 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
217143 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
217144 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
217145 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
217146 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
217147 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
217148 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
217149 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
217150 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
217151 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
217152 {65313, 14, 26},
217153 };
217154 static const unsigned short aiOff[] = {
217155 1, 2, 8, 15, 16, 26, 28, 32,
217156 37, 38, 40, 48, 63, 64, 69, 71,
217157 79, 80, 116, 202, 203, 205, 206, 207,
217158 209, 210, 211, 213, 214, 217, 218, 219,
217159 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
217160 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
217161 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
217162 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
217163 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
217164 65514, 65521, 65527, 65528, 65529,
217165 };
217166
217167 int ret = c;
217168
217169 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
217170
217171 if( c<128 ){
217172 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
217173 }else if( c<65536 ){
217174 const struct TableEntry *p;
217175 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
217176 int iLo = 0;
217177 int iRes = -1;
217178
217179 assert( c>aEntry[0].iCode );
217180 while( iHi>=iLo ){
217181 int iTest = (iHi + iLo) / 2;
217182 int cmp = (c - aEntry[iTest].iCode);
217183 if( cmp>=0 ){
217184 iRes = iTest;
217185 iLo = iTest+1;
217186 }else{
217187 iHi = iTest-1;
217188 }
217189 }
217190
217191 assert( iRes>=0 && c>=aEntry[iRes].iCode );
217192 p = &aEntry[iRes];
217193 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
217194 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
217195 assert( ret>0 );
217196 }
217197
217198 if( bRemoveDiacritic ) ret = fts5_remove_diacritic(ret);
217199 }
217200
217201 else if( c>=66560 && c<66600 ){
217202 ret = c + 40;
217203 }
217204
217205 return ret;
217206}
217207
217208
217209#if 0
217210static int sqlite3Fts5UnicodeNCat(void) {
217211 return 32;
217212}
217213#endif
217214
217215static int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){
217216 aArray[0] = 1;
217217 switch( zCat[0] ){
217218 case 'C':
217219 switch( zCat[1] ){
217220 case 'c': aArray[1] = 1; break;
217221 case 'f': aArray[2] = 1; break;
217222 case 'n': aArray[3] = 1; break;
217223 case 's': aArray[4] = 1; break;
217224 case 'o': aArray[31] = 1; break;
217225 case '*':
217226 aArray[1] = 1;
217227 aArray[2] = 1;
217228 aArray[3] = 1;
217229 aArray[4] = 1;
217230 aArray[31] = 1;
217231 break;
217232 default: return 1; }
217233 break;
217234
217235 case 'L':
217236 switch( zCat[1] ){
217237 case 'l': aArray[5] = 1; break;
217238 case 'm': aArray[6] = 1; break;
217239 case 'o': aArray[7] = 1; break;
217240 case 't': aArray[8] = 1; break;
217241 case 'u': aArray[9] = 1; break;
217242 case 'C': aArray[30] = 1; break;
217243 case '*':
217244 aArray[5] = 1;
217245 aArray[6] = 1;
217246 aArray[7] = 1;
217247 aArray[8] = 1;
217248 aArray[9] = 1;
217249 aArray[30] = 1;
217250 break;
217251 default: return 1; }
217252 break;
217253
217254 case 'M':
217255 switch( zCat[1] ){
217256 case 'c': aArray[10] = 1; break;
217257 case 'e': aArray[11] = 1; break;
217258 case 'n': aArray[12] = 1; break;
217259 case '*':
217260 aArray[10] = 1;
217261 aArray[11] = 1;
217262 aArray[12] = 1;
217263 break;
217264 default: return 1; }
217265 break;
217266
217267 case 'N':
217268 switch( zCat[1] ){
217269 case 'd': aArray[13] = 1; break;
217270 case 'l': aArray[14] = 1; break;
217271 case 'o': aArray[15] = 1; break;
217272 case '*':
217273 aArray[13] = 1;
217274 aArray[14] = 1;
217275 aArray[15] = 1;
217276 break;
217277 default: return 1; }
217278 break;
217279
217280 case 'P':
217281 switch( zCat[1] ){
217282 case 'c': aArray[16] = 1; break;
217283 case 'd': aArray[17] = 1; break;
217284 case 'e': aArray[18] = 1; break;
217285 case 'f': aArray[19] = 1; break;
217286 case 'i': aArray[20] = 1; break;
217287 case 'o': aArray[21] = 1; break;
217288 case 's': aArray[22] = 1; break;
217289 case '*':
217290 aArray[16] = 1;
217291 aArray[17] = 1;
217292 aArray[18] = 1;
217293 aArray[19] = 1;
217294 aArray[20] = 1;
217295 aArray[21] = 1;
217296 aArray[22] = 1;
217297 break;
217298 default: return 1; }
217299 break;
217300
217301 case 'S':
217302 switch( zCat[1] ){
217303 case 'c': aArray[23] = 1; break;
217304 case 'k': aArray[24] = 1; break;
217305 case 'm': aArray[25] = 1; break;
217306 case 'o': aArray[26] = 1; break;
217307 case '*':
217308 aArray[23] = 1;
217309 aArray[24] = 1;
217310 aArray[25] = 1;
217311 aArray[26] = 1;
217312 break;
217313 default: return 1; }
217314 break;
217315
217316 case 'Z':
217317 switch( zCat[1] ){
217318 case 'l': aArray[27] = 1; break;
217319 case 'p': aArray[28] = 1; break;
217320 case 's': aArray[29] = 1; break;
217321 case '*':
217322 aArray[27] = 1;
217323 aArray[28] = 1;
217324 aArray[29] = 1;
217325 break;
217326 default: return 1; }
217327 break;
217328
217329 }
217330 return 0;
217331}
217332
217333static u16 aFts5UnicodeBlock[] = {
217334 0, 1471, 1753, 1760, 1760, 1760, 1760, 1760, 1760, 1760,
217335 1760, 1760, 1760, 1760, 1760, 1763, 1765,
217336 };
217337static u16 aFts5UnicodeMap[] = {
217338 0, 32, 33, 36, 37, 40, 41, 42, 43, 44,
217339 45, 46, 48, 58, 60, 63, 65, 91, 92, 93,
217340 94, 95, 96, 97, 123, 124, 125, 126, 127, 160,
217341 161, 162, 166, 167, 168, 169, 170, 171, 172, 173,
217342 174, 175, 176, 177, 178, 180, 181, 182, 184, 185,
217343 186, 187, 188, 191, 192, 215, 216, 223, 247, 248,
217344 256, 312, 313, 329, 330, 377, 383, 385, 387, 388,
217345 391, 394, 396, 398, 402, 403, 405, 406, 409, 412,
217346 414, 415, 417, 418, 423, 427, 428, 431, 434, 436,
217347 437, 440, 442, 443, 444, 446, 448, 452, 453, 454,
217348 455, 456, 457, 458, 459, 460, 461, 477, 478, 496,
217349 497, 498, 499, 500, 503, 505, 506, 564, 570, 572,
217350 573, 575, 577, 580, 583, 584, 592, 660, 661, 688,
217351 706, 710, 722, 736, 741, 748, 749, 750, 751, 768,
217352 880, 884, 885, 886, 890, 891, 894, 900, 902, 903,
217353 904, 908, 910, 912, 913, 931, 940, 975, 977, 978,
217354 981, 984, 1008, 1012, 1014, 1015, 1018, 1020, 1021, 1072,
217355 1120, 1154, 1155, 1160, 1162, 1217, 1231, 1232, 1329, 1369,
217356 1370, 1377, 1417, 1418, 1423, 1425, 1470, 1471, 1472, 1473,
217357 1475, 1476, 1478, 1479, 1488, 1520, 1523, 1536, 1542, 1545,
217358 1547, 1548, 1550, 1552, 1563, 1566, 1568, 1600, 1601, 1611,
217359 1632, 1642, 1646, 1648, 1649, 1748, 1749, 1750, 1757, 1758,
217360 1759, 1765, 1767, 1769, 1770, 1774, 1776, 1786, 1789, 1791,
217361 1792, 1807, 1808, 1809, 1810, 1840, 1869, 1958, 1969, 1984,
217362 1994, 2027, 2036, 2038, 2039, 2042, 2048, 2070, 2074, 2075,
217363 2084, 2085, 2088, 2089, 2096, 2112, 2137, 2142, 2208, 2210,
217364 2276, 2304, 2307, 2308, 2362, 2363, 2364, 2365, 2366, 2369,
217365 2377, 2381, 2382, 2384, 2385, 2392, 2402, 2404, 2406, 2416,
217366 2417, 2418, 2425, 2433, 2434, 2437, 2447, 2451, 2474, 2482,
217367 2486, 2492, 2493, 2494, 2497, 2503, 2507, 2509, 2510, 2519,
217368 2524, 2527, 2530, 2534, 2544, 2546, 2548, 2554, 2555, 2561,
217369 2563, 2565, 2575, 2579, 2602, 2610, 2613, 2616, 2620, 2622,
217370 2625, 2631, 2635, 2641, 2649, 2654, 2662, 2672, 2674, 2677,
217371 2689, 2691, 2693, 2703, 2707, 2730, 2738, 2741, 2748, 2749,
217372 2750, 2753, 2759, 2761, 2763, 2765, 2768, 2784, 2786, 2790,
217373 2800, 2801, 2817, 2818, 2821, 2831, 2835, 2858, 2866, 2869,
217374 2876, 2877, 2878, 2879, 2880, 2881, 2887, 2891, 2893, 2902,
217375 2903, 2908, 2911, 2914, 2918, 2928, 2929, 2930, 2946, 2947,
217376 2949, 2958, 2962, 2969, 2972, 2974, 2979, 2984, 2990, 3006,
217377 3008, 3009, 3014, 3018, 3021, 3024, 3031, 3046, 3056, 3059,
217378 3065, 3066, 3073, 3077, 3086, 3090, 3114, 3125, 3133, 3134,
217379 3137, 3142, 3146, 3157, 3160, 3168, 3170, 3174, 3192, 3199,
217380 3202, 3205, 3214, 3218, 3242, 3253, 3260, 3261, 3262, 3263,
217381 3264, 3270, 3271, 3274, 3276, 3285, 3294, 3296, 3298, 3302,
217382 3313, 3330, 3333, 3342, 3346, 3389, 3390, 3393, 3398, 3402,
217383 3405, 3406, 3415, 3424, 3426, 3430, 3440, 3449, 3450, 3458,
217384 3461, 3482, 3507, 3517, 3520, 3530, 3535, 3538, 3542, 3544,
217385 3570, 3572, 3585, 3633, 3634, 3636, 3647, 3648, 3654, 3655,
217386 3663, 3664, 3674, 3713, 3716, 3719, 3722, 3725, 3732, 3737,
217387 3745, 3749, 3751, 3754, 3757, 3761, 3762, 3764, 3771, 3773,
217388 3776, 3782, 3784, 3792, 3804, 3840, 3841, 3844, 3859, 3860,
217389 3861, 3864, 3866, 3872, 3882, 3892, 3893, 3894, 3895, 3896,
217390 3897, 3898, 3899, 3900, 3901, 3902, 3904, 3913, 3953, 3967,
217391 3968, 3973, 3974, 3976, 3981, 3993, 4030, 4038, 4039, 4046,
217392 4048, 4053, 4057, 4096, 4139, 4141, 4145, 4146, 4152, 4153,
217393 4155, 4157, 4159, 4160, 4170, 4176, 4182, 4184, 4186, 4190,
217394 4193, 4194, 4197, 4199, 4206, 4209, 4213, 4226, 4227, 4229,
217395 4231, 4237, 4238, 4239, 4240, 4250, 4253, 4254, 4256, 4295,
217396 4301, 4304, 4347, 4348, 4349, 4682, 4688, 4696, 4698, 4704,
217397 4746, 4752, 4786, 4792, 4800, 4802, 4808, 4824, 4882, 4888,
217398 4957, 4960, 4969, 4992, 5008, 5024, 5120, 5121, 5741, 5743,
217399 5760, 5761, 5787, 5788, 5792, 5867, 5870, 5888, 5902, 5906,
217400 5920, 5938, 5941, 5952, 5970, 5984, 5998, 6002, 6016, 6068,
217401 6070, 6071, 6078, 6086, 6087, 6089, 6100, 6103, 6104, 6107,
217402 6108, 6109, 6112, 6128, 6144, 6150, 6151, 6155, 6158, 6160,
217403 6176, 6211, 6212, 6272, 6313, 6314, 6320, 6400, 6432, 6435,
217404 6439, 6441, 6448, 6450, 6451, 6457, 6464, 6468, 6470, 6480,
217405 6512, 6528, 6576, 6593, 6600, 6608, 6618, 6622, 6656, 6679,
217406 6681, 6686, 6688, 6741, 6742, 6743, 6744, 6752, 6753, 6754,
217407 6755, 6757, 6765, 6771, 6783, 6784, 6800, 6816, 6823, 6824,
217408 6912, 6916, 6917, 6964, 6965, 6966, 6971, 6972, 6973, 6978,
217409 6979, 6981, 6992, 7002, 7009, 7019, 7028, 7040, 7042, 7043,
217410 7073, 7074, 7078, 7080, 7082, 7083, 7084, 7086, 7088, 7098,
217411 7142, 7143, 7144, 7146, 7149, 7150, 7151, 7154, 7164, 7168,
217412 7204, 7212, 7220, 7222, 7227, 7232, 7245, 7248, 7258, 7288,
217413 7294, 7360, 7376, 7379, 7380, 7393, 7394, 7401, 7405, 7406,
217414 7410, 7412, 7413, 7424, 7468, 7531, 7544, 7545, 7579, 7616,
217415 7676, 7680, 7830, 7838, 7936, 7944, 7952, 7960, 7968, 7976,
217416 7984, 7992, 8000, 8008, 8016, 8025, 8027, 8029, 8031, 8033,
217417 8040, 8048, 8064, 8072, 8080, 8088, 8096, 8104, 8112, 8118,
217418 8120, 8124, 8125, 8126, 8127, 8130, 8134, 8136, 8140, 8141,
217419 8144, 8150, 8152, 8157, 8160, 8168, 8173, 8178, 8182, 8184,
217420 8188, 8189, 8192, 8203, 8208, 8214, 8216, 8217, 8218, 8219,
217421 8221, 8222, 8223, 8224, 8232, 8233, 8234, 8239, 8240, 8249,
217422 8250, 8251, 8255, 8257, 8260, 8261, 8262, 8263, 8274, 8275,
217423 8276, 8277, 8287, 8288, 8298, 8304, 8305, 8308, 8314, 8317,
217424 8318, 8319, 8320, 8330, 8333, 8334, 8336, 8352, 8400, 8413,
217425 8417, 8418, 8421, 8448, 8450, 8451, 8455, 8456, 8458, 8459,
217426 8462, 8464, 8467, 8468, 8469, 8470, 8472, 8473, 8478, 8484,
217427 8485, 8486, 8487, 8488, 8489, 8490, 8494, 8495, 8496, 8500,
217428 8501, 8505, 8506, 8508, 8510, 8512, 8517, 8519, 8522, 8523,
217429 8524, 8526, 8527, 8528, 8544, 8579, 8581, 8585, 8592, 8597,
217430 8602, 8604, 8608, 8609, 8611, 8612, 8614, 8615, 8622, 8623,
217431 8654, 8656, 8658, 8659, 8660, 8661, 8692, 8960, 8968, 8972,
217432 8992, 8994, 9001, 9002, 9003, 9084, 9085, 9115, 9140, 9180,
217433 9186, 9216, 9280, 9312, 9372, 9450, 9472, 9655, 9656, 9665,
217434 9666, 9720, 9728, 9839, 9840, 9985, 10088, 10089, 10090, 10091,
217435 10092, 10093, 10094, 10095, 10096, 10097, 10098, 10099, 10100, 10101,
217436 10102, 10132, 10176, 10181, 10182, 10183, 10214, 10215, 10216, 10217,
217437 10218, 10219, 10220, 10221, 10222, 10223, 10224, 10240, 10496, 10627,
217438 10628, 10629, 10630, 10631, 10632, 10633, 10634, 10635, 10636, 10637,
217439 10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647,
217440 10648, 10649, 10712, 10713, 10714, 10715, 10716, 10748, 10749, 10750,
217441 11008, 11056, 11077, 11079, 11088, 11264, 11312, 11360, 11363, 11365,
217442 11367, 11374, 11377, 11378, 11380, 11381, 11383, 11388, 11390, 11393,
217443 11394, 11492, 11493, 11499, 11503, 11506, 11513, 11517, 11518, 11520,
217444 11559, 11565, 11568, 11631, 11632, 11647, 11648, 11680, 11688, 11696,
217445 11704, 11712, 11720, 11728, 11736, 11744, 11776, 11778, 11779, 11780,
217446 11781, 11782, 11785, 11786, 11787, 11788, 11789, 11790, 11799, 11800,
217447 11802, 11803, 11804, 11805, 11806, 11808, 11809, 11810, 11811, 11812,
217448 11813, 11814, 11815, 11816, 11817, 11818, 11823, 11824, 11834, 11904,
217449 11931, 12032, 12272, 12288, 12289, 12292, 12293, 12294, 12295, 12296,
217450 12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 12306,
217451 12308, 12309, 12310, 12311, 12312, 12313, 12314, 12315, 12316, 12317,
217452 12318, 12320, 12321, 12330, 12334, 12336, 12337, 12342, 12344, 12347,
217453 12348, 12349, 12350, 12353, 12441, 12443, 12445, 12447, 12448, 12449,
217454 12539, 12540, 12543, 12549, 12593, 12688, 12690, 12694, 12704, 12736,
217455 12784, 12800, 12832, 12842, 12872, 12880, 12881, 12896, 12928, 12938,
217456 12977, 12992, 13056, 13312, 19893, 19904, 19968, 40908, 40960, 40981,
217457 40982, 42128, 42192, 42232, 42238, 42240, 42508, 42509, 42512, 42528,
217458 42538, 42560, 42606, 42607, 42608, 42611, 42612, 42622, 42623, 42624,
217459 42655, 42656, 42726, 42736, 42738, 42752, 42775, 42784, 42786, 42800,
217460 42802, 42864, 42865, 42873, 42878, 42888, 42889, 42891, 42896, 42912,
217461 43000, 43002, 43003, 43010, 43011, 43014, 43015, 43019, 43020, 43043,
217462 43045, 43047, 43048, 43056, 43062, 43064, 43065, 43072, 43124, 43136,
217463 43138, 43188, 43204, 43214, 43216, 43232, 43250, 43256, 43259, 43264,
217464 43274, 43302, 43310, 43312, 43335, 43346, 43359, 43360, 43392, 43395,
217465 43396, 43443, 43444, 43446, 43450, 43452, 43453, 43457, 43471, 43472,
217466 43486, 43520, 43561, 43567, 43569, 43571, 43573, 43584, 43587, 43588,
217467 43596, 43597, 43600, 43612, 43616, 43632, 43633, 43639, 43642, 43643,
217468 43648, 43696, 43697, 43698, 43701, 43703, 43705, 43710, 43712, 43713,
217469 43714, 43739, 43741, 43742, 43744, 43755, 43756, 43758, 43760, 43762,
217470 43763, 43765, 43766, 43777, 43785, 43793, 43808, 43816, 43968, 44003,
217471 44005, 44006, 44008, 44009, 44011, 44012, 44013, 44016, 44032, 55203,
217472 55216, 55243, 55296, 56191, 56319, 57343, 57344, 63743, 63744, 64112,
217473 64256, 64275, 64285, 64286, 64287, 64297, 64298, 64312, 64318, 64320,
217474 64323, 64326, 64434, 64467, 64830, 64831, 64848, 64914, 65008, 65020,
217475 65021, 65024, 65040, 65047, 65048, 65049, 65056, 65072, 65073, 65075,
217476 65077, 65078, 65079, 65080, 65081, 65082, 65083, 65084, 65085, 65086,
217477 65087, 65088, 65089, 65090, 65091, 65092, 65093, 65095, 65096, 65097,
217478 65101, 65104, 65108, 65112, 65113, 65114, 65115, 65116, 65117, 65118,
217479 65119, 65122, 65123, 65124, 65128, 65129, 65130, 65136, 65142, 65279,
217480 65281, 65284, 65285, 65288, 65289, 65290, 65291, 65292, 65293, 65294,
217481 65296, 65306, 65308, 65311, 65313, 65339, 65340, 65341, 65342, 65343,
217482 65344, 65345, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378,
217483 65379, 65380, 65382, 65392, 65393, 65438, 65440, 65474, 65482, 65490,
217484 65498, 65504, 65506, 65507, 65508, 65509, 65512, 65513, 65517, 65529,
217485 65532, 0, 13, 40, 60, 63, 80, 128, 256, 263,
217486 311, 320, 373, 377, 394, 400, 464, 509, 640, 672,
217487 768, 800, 816, 833, 834, 842, 896, 927, 928, 968,
217488 976, 977, 1024, 1064, 1104, 1184, 2048, 2056, 2058, 2103,
217489 2108, 2111, 2135, 2136, 2304, 2326, 2335, 2336, 2367, 2432,
217490 2494, 2560, 2561, 2565, 2572, 2576, 2581, 2585, 2616, 2623,
217491 2624, 2640, 2656, 2685, 2687, 2816, 2873, 2880, 2904, 2912,
217492 2936, 3072, 3680, 4096, 4097, 4098, 4099, 4152, 4167, 4178,
217493 4198, 4224, 4226, 4227, 4272, 4275, 4279, 4281, 4283, 4285,
217494 4286, 4304, 4336, 4352, 4355, 4391, 4396, 4397, 4406, 4416,
217495 4480, 4482, 4483, 4531, 4534, 4543, 4545, 4549, 4560, 5760,
217496 5803, 5804, 5805, 5806, 5808, 5814, 5815, 5824, 8192, 9216,
217497 9328, 12288, 26624, 28416, 28496, 28497, 28559, 28563, 45056, 53248,
217498 53504, 53545, 53605, 53607, 53610, 53613, 53619, 53627, 53635, 53637,
217499 53644, 53674, 53678, 53760, 53826, 53829, 54016, 54112, 54272, 54298,
217500 54324, 54350, 54358, 54376, 54402, 54428, 54430, 54434, 54437, 54441,
217501 54446, 54454, 54459, 54461, 54469, 54480, 54506, 54532, 54535, 54541,
217502 54550, 54558, 54584, 54587, 54592, 54598, 54602, 54610, 54636, 54662,
217503 54688, 54714, 54740, 54766, 54792, 54818, 54844, 54870, 54896, 54922,
217504 54952, 54977, 54978, 55003, 55004, 55010, 55035, 55036, 55061, 55062,
217505 55068, 55093, 55094, 55119, 55120, 55126, 55151, 55152, 55177, 55178,
217506 55184, 55209, 55210, 55235, 55236, 55242, 55246, 60928, 60933, 60961,
217507 60964, 60967, 60969, 60980, 60985, 60987, 60994, 60999, 61001, 61003,
217508 61005, 61009, 61012, 61015, 61017, 61019, 61021, 61023, 61025, 61028,
217509 61031, 61036, 61044, 61049, 61054, 61056, 61067, 61089, 61093, 61099,
217510 61168, 61440, 61488, 61600, 61617, 61633, 61649, 61696, 61712, 61744,
217511 61808, 61926, 61968, 62016, 62032, 62208, 62256, 62263, 62336, 62368,
217512 62406, 62432, 62464, 62528, 62530, 62713, 62720, 62784, 62800, 62971,
217513 63045, 63104, 63232, 0, 42710, 42752, 46900, 46912, 47133, 63488,
217514 1, 32, 256, 0, 65533,
217515 };
217516static u16 aFts5UnicodeData[] = {
217517 1025, 61, 117, 55, 117, 54, 50, 53, 57, 53,
217518 49, 85, 333, 85, 121, 85, 841, 54, 53, 50,
217519 56, 48, 56, 837, 54, 57, 50, 57, 1057, 61,
217520 53, 151, 58, 53, 56, 58, 39, 52, 57, 34,
217521 58, 56, 58, 57, 79, 56, 37, 85, 56, 47,
217522 39, 51, 111, 53, 745, 57, 233, 773, 57, 261,
217523 1822, 37, 542, 37, 1534, 222, 69, 73, 37, 126,
217524 126, 73, 69, 137, 37, 73, 37, 105, 101, 73,
217525 37, 73, 37, 190, 158, 37, 126, 126, 73, 37,
217526 126, 94, 37, 39, 94, 69, 135, 41, 40, 37,
217527 41, 40, 37, 41, 40, 37, 542, 37, 606, 37,
217528 41, 40, 37, 126, 73, 37, 1886, 197, 73, 37,
217529 73, 69, 126, 105, 37, 286, 2181, 39, 869, 582,
217530 152, 390, 472, 166, 248, 38, 56, 38, 568, 3596,
217531 158, 38, 56, 94, 38, 101, 53, 88, 41, 53,
217532 105, 41, 73, 37, 553, 297, 1125, 94, 37, 105,
217533 101, 798, 133, 94, 57, 126, 94, 37, 1641, 1541,
217534 1118, 58, 172, 75, 1790, 478, 37, 2846, 1225, 38,
217535 213, 1253, 53, 49, 55, 1452, 49, 44, 53, 76,
217536 53, 76, 53, 44, 871, 103, 85, 162, 121, 85,
217537 55, 85, 90, 364, 53, 85, 1031, 38, 327, 684,
217538 333, 149, 71, 44, 3175, 53, 39, 236, 34, 58,
217539 204, 70, 76, 58, 140, 71, 333, 103, 90, 39,
217540 469, 34, 39, 44, 967, 876, 2855, 364, 39, 333,
217541 1063, 300, 70, 58, 117, 38, 711, 140, 38, 300,
217542 38, 108, 38, 172, 501, 807, 108, 53, 39, 359,
217543 876, 108, 42, 1735, 44, 42, 44, 39, 106, 268,
217544 138, 44, 74, 39, 236, 327, 76, 85, 333, 53,
217545 38, 199, 231, 44, 74, 263, 71, 711, 231, 39,
217546 135, 44, 39, 106, 140, 74, 74, 44, 39, 42,
217547 71, 103, 76, 333, 71, 87, 207, 58, 55, 76,
217548 42, 199, 71, 711, 231, 71, 71, 71, 44, 106,
217549 76, 76, 108, 44, 135, 39, 333, 76, 103, 44,
217550 76, 42, 295, 103, 711, 231, 71, 167, 44, 39,
217551 106, 172, 76, 42, 74, 44, 39, 71, 76, 333,
217552 53, 55, 44, 74, 263, 71, 711, 231, 71, 167,
217553 44, 39, 42, 44, 42, 140, 74, 74, 44, 44,
217554 42, 71, 103, 76, 333, 58, 39, 207, 44, 39,
217555 199, 103, 135, 71, 39, 71, 71, 103, 391, 74,
217556 44, 74, 106, 106, 44, 39, 42, 333, 111, 218,
217557 55, 58, 106, 263, 103, 743, 327, 167, 39, 108,
217558 138, 108, 140, 76, 71, 71, 76, 333, 239, 58,
217559 74, 263, 103, 743, 327, 167, 44, 39, 42, 44,
217560 170, 44, 74, 74, 76, 74, 39, 71, 76, 333,
217561 71, 74, 263, 103, 1319, 39, 106, 140, 106, 106,
217562 44, 39, 42, 71, 76, 333, 207, 58, 199, 74,
217563 583, 775, 295, 39, 231, 44, 106, 108, 44, 266,
217564 74, 53, 1543, 44, 71, 236, 55, 199, 38, 268,
217565 53, 333, 85, 71, 39, 71, 39, 39, 135, 231,
217566 103, 39, 39, 71, 135, 44, 71, 204, 76, 39,
217567 167, 38, 204, 333, 135, 39, 122, 501, 58, 53,
217568 122, 76, 218, 333, 335, 58, 44, 58, 44, 58,
217569 44, 54, 50, 54, 50, 74, 263, 1159, 460, 42,
217570 172, 53, 76, 167, 364, 1164, 282, 44, 218, 90,
217571 181, 154, 85, 1383, 74, 140, 42, 204, 42, 76,
217572 74, 76, 39, 333, 213, 199, 74, 76, 135, 108,
217573 39, 106, 71, 234, 103, 140, 423, 44, 74, 76,
217574 202, 44, 39, 42, 333, 106, 44, 90, 1225, 41,
217575 41, 1383, 53, 38, 10631, 135, 231, 39, 135, 1319,
217576 135, 1063, 135, 231, 39, 135, 487, 1831, 135, 2151,
217577 108, 309, 655, 519, 346, 2727, 49, 19847, 85, 551,
217578 61, 839, 54, 50, 2407, 117, 110, 423, 135, 108,
217579 583, 108, 85, 583, 76, 423, 103, 76, 1671, 76,
217580 42, 236, 266, 44, 74, 364, 117, 38, 117, 55,
217581 39, 44, 333, 335, 213, 49, 149, 108, 61, 333,
217582 1127, 38, 1671, 1319, 44, 39, 2247, 935, 108, 138,
217583 76, 106, 74, 44, 202, 108, 58, 85, 333, 967,
217584 167, 1415, 554, 231, 74, 333, 47, 1114, 743, 76,
217585 106, 85, 1703, 42, 44, 42, 236, 44, 42, 44,
217586 74, 268, 202, 332, 44, 333, 333, 245, 38, 213,
217587 140, 42, 1511, 44, 42, 172, 42, 44, 170, 44,
217588 74, 231, 333, 245, 346, 300, 314, 76, 42, 967,
217589 42, 140, 74, 76, 42, 44, 74, 71, 333, 1415,
217590 44, 42, 76, 106, 44, 42, 108, 74, 149, 1159,
217591 266, 268, 74, 76, 181, 333, 103, 333, 967, 198,
217592 85, 277, 108, 53, 428, 42, 236, 135, 44, 135,
217593 74, 44, 71, 1413, 2022, 421, 38, 1093, 1190, 1260,
217594 140, 4830, 261, 3166, 261, 265, 197, 201, 261, 265,
217595 261, 265, 197, 201, 261, 41, 41, 41, 94, 229,
217596 265, 453, 261, 264, 261, 264, 261, 264, 165, 69,
217597 137, 40, 56, 37, 120, 101, 69, 137, 40, 120,
217598 133, 69, 137, 120, 261, 169, 120, 101, 69, 137,
217599 40, 88, 381, 162, 209, 85, 52, 51, 54, 84,
217600 51, 54, 52, 277, 59, 60, 162, 61, 309, 52,
217601 51, 149, 80, 117, 57, 54, 50, 373, 57, 53,
217602 48, 341, 61, 162, 194, 47, 38, 207, 121, 54,
217603 50, 38, 335, 121, 54, 50, 422, 855, 428, 139,
217604 44, 107, 396, 90, 41, 154, 41, 90, 37, 105,
217605 69, 105, 37, 58, 41, 90, 57, 169, 218, 41,
217606 58, 41, 58, 41, 58, 137, 58, 37, 137, 37,
217607 135, 37, 90, 69, 73, 185, 94, 101, 58, 57,
217608 90, 37, 58, 527, 1134, 94, 142, 47, 185, 186,
217609 89, 154, 57, 90, 57, 90, 57, 250, 57, 1018,
217610 89, 90, 57, 58, 57, 1018, 8601, 282, 153, 666,
217611 89, 250, 54, 50, 2618, 57, 986, 825, 1306, 217,
217612 602, 1274, 378, 1935, 2522, 719, 5882, 57, 314, 57,
217613 1754, 281, 3578, 57, 4634, 3322, 54, 50, 54, 50,
217614 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
217615 975, 1434, 185, 54, 50, 1017, 54, 50, 54, 50,
217616 54, 50, 54, 50, 54, 50, 537, 8218, 4217, 54,
217617 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
217618 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
217619 50, 2041, 54, 50, 54, 50, 1049, 54, 50, 8281,
217620 1562, 697, 90, 217, 346, 1513, 1509, 126, 73, 69,
217621 254, 105, 37, 94, 37, 94, 165, 70, 105, 37,
217622 3166, 37, 218, 158, 108, 94, 149, 47, 85, 1221,
217623 37, 37, 1799, 38, 53, 44, 743, 231, 231, 231,
217624 231, 231, 231, 231, 231, 1036, 85, 52, 51, 52,
217625 51, 117, 52, 51, 53, 52, 51, 309, 49, 85,
217626 49, 53, 52, 51, 85, 52, 51, 54, 50, 54,
217627 50, 54, 50, 54, 50, 181, 38, 341, 81, 858,
217628 2874, 6874, 410, 61, 117, 58, 38, 39, 46, 54,
217629 50, 54, 50, 54, 50, 54, 50, 54, 50, 90,
217630 54, 50, 54, 50, 54, 50, 54, 50, 49, 54,
217631 82, 58, 302, 140, 74, 49, 166, 90, 110, 38,
217632 39, 53, 90, 2759, 76, 88, 70, 39, 49, 2887,
217633 53, 102, 39, 1319, 3015, 90, 143, 346, 871, 1178,
217634 519, 1018, 335, 986, 271, 58, 495, 1050, 335, 1274,
217635 495, 2042, 8218, 39, 39, 2074, 39, 39, 679, 38,
217636 36583, 1786, 1287, 198, 85, 8583, 38, 117, 519, 333,
217637 71, 1502, 39, 44, 107, 53, 332, 53, 38, 798,
217638 44, 2247, 334, 76, 213, 760, 294, 88, 478, 69,
217639 2014, 38, 261, 190, 350, 38, 88, 158, 158, 382,
217640 70, 37, 231, 44, 103, 44, 135, 44, 743, 74,
217641 76, 42, 154, 207, 90, 55, 58, 1671, 149, 74,
217642 1607, 522, 44, 85, 333, 588, 199, 117, 39, 333,
217643 903, 268, 85, 743, 364, 74, 53, 935, 108, 42,
217644 1511, 44, 74, 140, 74, 44, 138, 437, 38, 333,
217645 85, 1319, 204, 74, 76, 74, 76, 103, 44, 263,
217646 44, 42, 333, 149, 519, 38, 199, 122, 39, 42,
217647 1543, 44, 39, 108, 71, 76, 167, 76, 39, 44,
217648 39, 71, 38, 85, 359, 42, 76, 74, 85, 39,
217649 70, 42, 44, 199, 199, 199, 231, 231, 1127, 74,
217650 44, 74, 44, 74, 53, 42, 44, 333, 39, 39,
217651 743, 1575, 36, 68, 68, 36, 63, 63, 11719, 3399,
217652 229, 165, 39, 44, 327, 57, 423, 167, 39, 71,
217653 71, 3463, 536, 11623, 54, 50, 2055, 1735, 391, 55,
217654 58, 524, 245, 54, 50, 53, 236, 53, 81, 80,
217655 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
217656 54, 50, 54, 50, 54, 50, 85, 54, 50, 149,
217657 112, 117, 149, 49, 54, 50, 54, 50, 54, 50,
217658 117, 57, 49, 121, 53, 55, 85, 167, 4327, 34,
217659 117, 55, 117, 54, 50, 53, 57, 53, 49, 85,
217660 333, 85, 121, 85, 841, 54, 53, 50, 56, 48,
217661 56, 837, 54, 57, 50, 57, 54, 50, 53, 54,
217662 50, 85, 327, 38, 1447, 70, 999, 199, 199, 199,
217663 103, 87, 57, 56, 58, 87, 58, 153, 90, 98,
217664 90, 391, 839, 615, 71, 487, 455, 3943, 117, 1455,
217665 314, 1710, 143, 570, 47, 410, 1466, 44, 935, 1575,
217666 999, 143, 551, 46, 263, 46, 967, 53, 1159, 263,
217667 53, 174, 1289, 1285, 2503, 333, 199, 39, 1415, 71,
217668 39, 743, 53, 271, 711, 207, 53, 839, 53, 1799,
217669 71, 39, 108, 76, 140, 135, 103, 871, 108, 44,
217670 271, 309, 935, 79, 53, 1735, 245, 711, 271, 615,
217671 271, 2343, 1007, 42, 44, 42, 1703, 492, 245, 655,
217672 333, 76, 42, 1447, 106, 140, 74, 76, 85, 34,
217673 149, 807, 333, 108, 1159, 172, 42, 268, 333, 149,
217674 76, 42, 1543, 106, 300, 74, 135, 149, 333, 1383,
217675 44, 42, 44, 74, 204, 42, 44, 333, 28135, 3182,
217676 149, 34279, 18215, 2215, 39, 1482, 140, 422, 71, 7898,
217677 1274, 1946, 74, 108, 122, 202, 258, 268, 90, 236,
217678 986, 140, 1562, 2138, 108, 58, 2810, 591, 841, 837,
217679 841, 229, 581, 841, 837, 41, 73, 41, 73, 137,
217680 265, 133, 37, 229, 357, 841, 837, 73, 137, 265,
217681 233, 837, 73, 137, 169, 41, 233, 837, 841, 837,
217682 841, 837, 841, 837, 841, 837, 841, 837, 841, 901,
217683 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
217684 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
217685 809, 57, 805, 57, 197, 94, 1613, 135, 871, 71,
217686 39, 39, 327, 135, 39, 39, 39, 39, 39, 39,
217687 103, 71, 39, 39, 39, 39, 39, 39, 71, 39,
217688 135, 231, 135, 135, 39, 327, 551, 103, 167, 551,
217689 89, 1434, 3226, 506, 474, 506, 506, 367, 1018, 1946,
217690 1402, 954, 1402, 314, 90, 1082, 218, 2266, 666, 1210,
217691 186, 570, 2042, 58, 5850, 154, 2010, 154, 794, 2266,
217692 378, 2266, 3738, 39, 39, 39, 39, 39, 39, 17351,
217693 34, 3074, 7692, 63, 63,
217694 };
217695
217696static int sqlite3Fts5UnicodeCategory(int iCode) {
217697 int iRes = -1;
217698 int iHi;
217699 int iLo;
217700 int ret;
217701 u16 iKey;
217702
217703 if( iCode>=(1<<20) ){
217704 return 0;
217705 }
217706 iLo = aFts5UnicodeBlock[(iCode>>16)];
217707 iHi = aFts5UnicodeBlock[1+(iCode>>16)];
217708 iKey = (iCode & 0xFFFF);
217709 while( iHi>iLo ){
217710 int iTest = (iHi + iLo) / 2;
217711 assert( iTest>=iLo && iTest<iHi );
217712 if( iKey>=aFts5UnicodeMap[iTest] ){
217713 iRes = iTest;
217714 iLo = iTest+1;
217715 }else{
217716 iHi = iTest;
217717 }
217718 }
217719
217720 if( iRes<0 ) return 0;
217721 if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0;
217722 ret = aFts5UnicodeData[iRes] & 0x1F;
217723 if( ret!=30 ) return ret;
217724 return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? 5 : 9;
217725}
217726
217727static void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){
217728 int i = 0;
217729 int iTbl = 0;
217730 while( i<128 ){
217731 int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ];
217732 int n = (aFts5UnicodeData[iTbl] >> 5) + i;
217733 for(; i<128 && i<n; i++){
217734 aAscii[i] = (u8)bToken;
217735 }
217736 iTbl++;
217737 }
217738}
217739
217740
217741/*
217742** 2015 May 30
217743**
217744** The author disclaims copyright to this source code. In place of
217745** a legal notice, here is a blessing:
217746**
217747** May you do good and not evil.
217748** May you find forgiveness for yourself and forgive others.
217749** May you share freely, never taking more than you give.
217750**
217751******************************************************************************
217752**
217753** Routines for varint serialization and deserialization.
217754*/
217755
217756
217757/* #include "fts5Int.h" */
217758
217759/*
217760** This is a copy of the sqlite3GetVarint32() routine from the SQLite core.
217761** Except, this version does handle the single byte case that the core
217762** version depends on being handled before its function is called.
217763*/
217764static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v){
217765 u32 a,b;
217766
217767 /* The 1-byte case. Overwhelmingly the most common. */
217768 a = *p;
217769 /* a: p0 (unmasked) */
217770 if (!(a&0x80))
217771 {
217772 /* Values between 0 and 127 */
217773 *v = a;
217774 return 1;
217775 }
217776
217777 /* The 2-byte case */
217778 p++;
217779 b = *p;
217780 /* b: p1 (unmasked) */
217781 if (!(b&0x80))
217782 {
217783 /* Values between 128 and 16383 */
217784 a &= 0x7f;
217785 a = a<<7;
217786 *v = a | b;
217787 return 2;
217788 }
217789
217790 /* The 3-byte case */
217791 p++;
217792 a = a<<14;
217793 a |= *p;
217794 /* a: p0<<14 | p2 (unmasked) */
217795 if (!(a&0x80))
217796 {
217797 /* Values between 16384 and 2097151 */
217798 a &= (0x7f<<14)|(0x7f);
217799 b &= 0x7f;
217800 b = b<<7;
217801 *v = a | b;
217802 return 3;
217803 }
217804
217805 /* A 32-bit varint is used to store size information in btrees.
217806 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
217807 ** A 3-byte varint is sufficient, for example, to record the size
217808 ** of a 1048569-byte BLOB or string.
217809 **
217810 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
217811 ** rare larger cases can be handled by the slower 64-bit varint
217812 ** routine.
217813 */
217814 {
217815 u64 v64;
217816 u8 n;
217817 p -= 2;
217818 n = sqlite3Fts5GetVarint(p, &v64);
217819 *v = (u32)v64;
217820 assert( n>3 && n<=9 );
217821 return n;
217822 }
217823}
217824
217825
217826/*
217827** Bitmasks used by sqlite3GetVarint(). These precomputed constants
217828** are defined here rather than simply putting the constant expressions
217829** inline in order to work around bugs in the RVT compiler.
217830**
217831** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
217832**
217833** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
217834*/
217835#define SLOT_2_0 0x001fc07f
217836#define SLOT_4_2_0 0xf01fc07f
217837
217838/*
217839** Read a 64-bit variable-length integer from memory starting at p[0].
217840** Return the number of bytes read. The value is stored in *v.
217841*/
217842static u8 sqlite3Fts5GetVarint(const unsigned char *p, u64 *v){
217843 u32 a,b,s;
217844
217845 a = *p;
217846 /* a: p0 (unmasked) */
217847 if (!(a&0x80))
217848 {
217849 *v = a;
217850 return 1;
217851 }
217852
217853 p++;
217854 b = *p;
217855 /* b: p1 (unmasked) */
217856 if (!(b&0x80))
217857 {
217858 a &= 0x7f;
217859 a = a<<7;
217860 a |= b;
217861 *v = a;
217862 return 2;
217863 }
217864
217865 /* Verify that constants are precomputed correctly */
217866 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
217867 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
217868
217869 p++;
217870 a = a<<14;
217871 a |= *p;
217872 /* a: p0<<14 | p2 (unmasked) */
217873 if (!(a&0x80))
217874 {
217875 a &= SLOT_2_0;
217876 b &= 0x7f;
217877 b = b<<7;
217878 a |= b;
217879 *v = a;
217880 return 3;
217881 }
217882
217883 /* CSE1 from below */
217884 a &= SLOT_2_0;
217885 p++;
217886 b = b<<14;
217887 b |= *p;
217888 /* b: p1<<14 | p3 (unmasked) */
217889 if (!(b&0x80))
217890 {
217891 b &= SLOT_2_0;
217892 /* moved CSE1 up */
217893 /* a &= (0x7f<<14)|(0x7f); */
217894 a = a<<7;
217895 a |= b;
217896 *v = a;
217897 return 4;
217898 }
217899
217900 /* a: p0<<14 | p2 (masked) */
217901 /* b: p1<<14 | p3 (unmasked) */
217902 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
217903 /* moved CSE1 up */
217904 /* a &= (0x7f<<14)|(0x7f); */
217905 b &= SLOT_2_0;
217906 s = a;
217907 /* s: p0<<14 | p2 (masked) */
217908
217909 p++;
217910 a = a<<14;
217911 a |= *p;
217912 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
217913 if (!(a&0x80))
217914 {
217915 /* we can skip these cause they were (effectively) done above in calc'ing s */
217916 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
217917 /* b &= (0x7f<<14)|(0x7f); */
217918 b = b<<7;
217919 a |= b;
217920 s = s>>18;
217921 *v = ((u64)s)<<32 | a;
217922 return 5;
217923 }
217924
217925 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
217926 s = s<<7;
217927 s |= b;
217928 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
217929
217930 p++;
217931 b = b<<14;
217932 b |= *p;
217933 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
217934 if (!(b&0x80))
217935 {
217936 /* we can skip this cause it was (effectively) done above in calc'ing s */
217937 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
217938 a &= SLOT_2_0;
217939 a = a<<7;
217940 a |= b;
217941 s = s>>18;
217942 *v = ((u64)s)<<32 | a;
217943 return 6;
217944 }
217945
217946 p++;
217947 a = a<<14;
217948 a |= *p;
217949 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
217950 if (!(a&0x80))
217951 {
217952 a &= SLOT_4_2_0;
217953 b &= SLOT_2_0;
217954 b = b<<7;
217955 a |= b;
217956 s = s>>11;
217957 *v = ((u64)s)<<32 | a;
217958 return 7;
217959 }
217960
217961 /* CSE2 from below */
217962 a &= SLOT_2_0;
217963 p++;
217964 b = b<<14;
217965 b |= *p;
217966 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
217967 if (!(b&0x80))
217968 {
217969 b &= SLOT_4_2_0;
217970 /* moved CSE2 up */
217971 /* a &= (0x7f<<14)|(0x7f); */
217972 a = a<<7;
217973 a |= b;
217974 s = s>>4;
217975 *v = ((u64)s)<<32 | a;
217976 return 8;
217977 }
217978
217979 p++;
217980 a = a<<15;
217981 a |= *p;
217982 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
217983
217984 /* moved CSE2 up */
217985 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
217986 b &= SLOT_2_0;
217987 b = b<<8;
217988 a |= b;
217989
217990 s = s<<4;
217991 b = p[-4];
217992 b &= 0x7f;
217993 b = b>>3;
217994 s |= b;
217995
217996 *v = ((u64)s)<<32 | a;
217997
217998 return 9;
217999}
218000
218001/*
218002** The variable-length integer encoding is as follows:
218003**
218004** KEY:
218005** A = 0xxxxxxx 7 bits of data and one flag bit
218006** B = 1xxxxxxx 7 bits of data and one flag bit
218007** C = xxxxxxxx 8 bits of data
218008**
218009** 7 bits - A
218010** 14 bits - BA
218011** 21 bits - BBA
218012** 28 bits - BBBA
218013** 35 bits - BBBBA
218014** 42 bits - BBBBBA
218015** 49 bits - BBBBBBA
218016** 56 bits - BBBBBBBA
218017** 64 bits - BBBBBBBBC
218018*/
218019
218020#ifdef SQLITE_NOINLINE
218021# define FTS5_NOINLINE SQLITE_NOINLINE
218022#else
218023# define FTS5_NOINLINE
218024#endif
218025
218026/*
218027** Write a 64-bit variable-length integer to memory starting at p[0].
218028** The length of data write will be between 1 and 9 bytes. The number
218029** of bytes written is returned.
218030**
218031** A variable-length integer consists of the lower 7 bits of each byte
218032** for all bytes that have the 8th bit set and one byte with the 8th
218033** bit clear. Except, if we get to the 9th byte, it stores the full
218034** 8 bits and is the last byte.
218035*/
218036static int FTS5_NOINLINE fts5PutVarint64(unsigned char *p, u64 v){
218037 int i, j, n;
218038 u8 buf[10];
218039 if( v & (((u64)0xff000000)<<32) ){
218040 p[8] = (u8)v;
218041 v >>= 8;
218042 for(i=7; i>=0; i--){
218043 p[i] = (u8)((v & 0x7f) | 0x80);
218044 v >>= 7;
218045 }
218046 return 9;
218047 }
218048 n = 0;
218049 do{
218050 buf[n++] = (u8)((v & 0x7f) | 0x80);
218051 v >>= 7;
218052 }while( v!=0 );
218053 buf[0] &= 0x7f;
218054 assert( n<=9 );
218055 for(i=0, j=n-1; j>=0; j--, i++){
218056 p[i] = buf[j];
218057 }
218058 return n;
218059}
218060
218061static int sqlite3Fts5PutVarint(unsigned char *p, u64 v){
218062 if( v<=0x7f ){
218063 p[0] = v&0x7f;
218064 return 1;
218065 }
218066 if( v<=0x3fff ){
218067 p[0] = ((v>>7)&0x7f)|0x80;
218068 p[1] = v&0x7f;
218069 return 2;
218070 }
218071 return fts5PutVarint64(p,v);
218072}
218073
218074
218075static int sqlite3Fts5GetVarintLen(u32 iVal){
218076#if 0
218077 if( iVal<(1 << 7 ) ) return 1;
218078#endif
218079 assert( iVal>=(1 << 7) );
218080 if( iVal<(1 << 14) ) return 2;
218081 if( iVal<(1 << 21) ) return 3;
218082 if( iVal<(1 << 28) ) return 4;
218083 return 5;
218084}
218085
218086
218087/*
218088** 2015 May 08
218089**
218090** The author disclaims copyright to this source code. In place of
218091** a legal notice, here is a blessing:
218092**
218093** May you do good and not evil.
218094** May you find forgiveness for yourself and forgive others.
218095** May you share freely, never taking more than you give.
218096**
218097******************************************************************************
218098**
218099** This is an SQLite virtual table module implementing direct access to an
218100** existing FTS5 index. The module may create several different types of
218101** tables:
218102**
218103** col:
218104** CREATE TABLE vocab(term, col, doc, cnt, PRIMARY KEY(term, col));
218105**
218106** One row for each term/column combination. The value of $doc is set to
218107** the number of fts5 rows that contain at least one instance of term
218108** $term within column $col. Field $cnt is set to the total number of
218109** instances of term $term in column $col (in any row of the fts5 table).
218110**
218111** row:
218112** CREATE TABLE vocab(term, doc, cnt, PRIMARY KEY(term));
218113**
218114** One row for each term in the database. The value of $doc is set to
218115** the number of fts5 rows that contain at least one instance of term
218116** $term. Field $cnt is set to the total number of instances of term
218117** $term in the database.
218118**
218119** instance:
218120** CREATE TABLE vocab(term, doc, col, offset, PRIMARY KEY(<all-fields>));
218121**
218122** One row for each term instance in the database.
218123*/
218124
218125
218126/* #include "fts5Int.h" */
218127
218128
218129typedef struct Fts5VocabTable Fts5VocabTable;
218130typedef struct Fts5VocabCursor Fts5VocabCursor;
218131
218132struct Fts5VocabTable {
218133 sqlite3_vtab base;
218134 char *zFts5Tbl; /* Name of fts5 table */
218135 char *zFts5Db; /* Db containing fts5 table */
218136 sqlite3 *db; /* Database handle */
218137 Fts5Global *pGlobal; /* FTS5 global object for this database */
218138 int eType; /* FTS5_VOCAB_COL, ROW or INSTANCE */
218139};
218140
218141struct Fts5VocabCursor {
218142 sqlite3_vtab_cursor base;
218143 sqlite3_stmt *pStmt; /* Statement holding lock on pIndex */
218144 Fts5Index *pIndex; /* Associated FTS5 index */
218145
218146 int bEof; /* True if this cursor is at EOF */
218147 Fts5IndexIter *pIter; /* Term/rowid iterator object */
218148
218149 int nLeTerm; /* Size of zLeTerm in bytes */
218150 char *zLeTerm; /* (term <= $zLeTerm) paramater, or NULL */
218151
218152 /* These are used by 'col' tables only */
218153 Fts5Config *pConfig; /* Fts5 table configuration */
218154 int iCol;
218155 i64 *aCnt;
218156 i64 *aDoc;
218157
218158 /* Output values used by all tables. */
218159 i64 rowid; /* This table's current rowid value */
218160 Fts5Buffer term; /* Current value of 'term' column */
218161
218162 /* Output values Used by 'instance' tables only */
218163 i64 iInstPos;
218164 int iInstOff;
218165};
218166
218167#define FTS5_VOCAB_COL 0
218168#define FTS5_VOCAB_ROW 1
218169#define FTS5_VOCAB_INSTANCE 2
218170
218171#define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt"
218172#define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt"
218173#define FTS5_VOCAB_INST_SCHEMA "term, doc, col, offset"
218174
218175/*
218176** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
218177*/
218178#define FTS5_VOCAB_TERM_EQ 0x01
218179#define FTS5_VOCAB_TERM_GE 0x02
218180#define FTS5_VOCAB_TERM_LE 0x04
218181
218182
218183/*
218184** Translate a string containing an fts5vocab table type to an
218185** FTS5_VOCAB_XXX constant. If successful, set *peType to the output
218186** value and return SQLITE_OK. Otherwise, set *pzErr to an error message
218187** and return SQLITE_ERROR.
218188*/
218189static int fts5VocabTableType(const char *zType, char **pzErr, int *peType){
218190 int rc = SQLITE_OK;
218191 char *zCopy = sqlite3Fts5Strndup(&rc, zType, -1);
218192 if( rc==SQLITE_OK ){
218193 sqlite3Fts5Dequote(zCopy);
218194 if( sqlite3_stricmp(zCopy, "col")==0 ){
218195 *peType = FTS5_VOCAB_COL;
218196 }else
218197
218198 if( sqlite3_stricmp(zCopy, "row")==0 ){
218199 *peType = FTS5_VOCAB_ROW;
218200 }else
218201 if( sqlite3_stricmp(zCopy, "instance")==0 ){
218202 *peType = FTS5_VOCAB_INSTANCE;
218203 }else
218204 {
218205 *pzErr = sqlite3_mprintf("fts5vocab: unknown table type: %Q", zCopy);
218206 rc = SQLITE_ERROR;
218207 }
218208 sqlite3_free(zCopy);
218209 }
218210
218211 return rc;
218212}
218213
218214
218215/*
218216** The xDisconnect() virtual table method.
218217*/
218218static int fts5VocabDisconnectMethod(sqlite3_vtab *pVtab){
218219 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
218220 sqlite3_free(pTab);
218221 return SQLITE_OK;
218222}
218223
218224/*
218225** The xDestroy() virtual table method.
218226*/
218227static int fts5VocabDestroyMethod(sqlite3_vtab *pVtab){
218228 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
218229 sqlite3_free(pTab);
218230 return SQLITE_OK;
218231}
218232
218233/*
218234** This function is the implementation of both the xConnect and xCreate
218235** methods of the FTS3 virtual table.
218236**
218237** The argv[] array contains the following:
218238**
218239** argv[0] -> module name ("fts5vocab")
218240** argv[1] -> database name
218241** argv[2] -> table name
218242**
218243** then:
218244**
218245** argv[3] -> name of fts5 table
218246** argv[4] -> type of fts5vocab table
218247**
218248** or, for tables in the TEMP schema only.
218249**
218250** argv[3] -> name of fts5 tables database
218251** argv[4] -> name of fts5 table
218252** argv[5] -> type of fts5vocab table
218253*/
218254static int fts5VocabInitVtab(
218255 sqlite3 *db, /* The SQLite database connection */
218256 void *pAux, /* Pointer to Fts5Global object */
218257 int argc, /* Number of elements in argv array */
218258 const char * const *argv, /* xCreate/xConnect argument array */
218259 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
218260 char **pzErr /* Write any error message here */
218261){
218262 const char *azSchema[] = {
218263 "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ")",
218264 "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ")",
218265 "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ")"
218266 };
218267
218268 Fts5VocabTable *pRet = 0;
218269 int rc = SQLITE_OK; /* Return code */
218270 int bDb;
218271
218272 bDb = (argc==6 && strlen(argv[1])==4 && memcmp("temp", argv[1], 4)==0);
218273
218274 if( argc!=5 && bDb==0 ){
218275 *pzErr = sqlite3_mprintf("wrong number of vtable arguments");
218276 rc = SQLITE_ERROR;
218277 }else{
218278 int nByte; /* Bytes of space to allocate */
218279 const char *zDb = bDb ? argv[3] : argv[1];
218280 const char *zTab = bDb ? argv[4] : argv[3];
218281 const char *zType = bDb ? argv[5] : argv[4];
218282 int nDb = (int)strlen(zDb)+1;
218283 int nTab = (int)strlen(zTab)+1;
218284 int eType = 0;
218285
218286 rc = fts5VocabTableType(zType, pzErr, &eType);
218287 if( rc==SQLITE_OK ){
218288 assert( eType>=0 && eType<ArraySize(azSchema) );
218289 rc = sqlite3_declare_vtab(db, azSchema[eType]);
218290 }
218291
218292 nByte = sizeof(Fts5VocabTable) + nDb + nTab;
218293 pRet = sqlite3Fts5MallocZero(&rc, nByte);
218294 if( pRet ){
218295 pRet->pGlobal = (Fts5Global*)pAux;
218296 pRet->eType = eType;
218297 pRet->db = db;
218298 pRet->zFts5Tbl = (char*)&pRet[1];
218299 pRet->zFts5Db = &pRet->zFts5Tbl[nTab];
218300 memcpy(pRet->zFts5Tbl, zTab, nTab);
218301 memcpy(pRet->zFts5Db, zDb, nDb);
218302 sqlite3Fts5Dequote(pRet->zFts5Tbl);
218303 sqlite3Fts5Dequote(pRet->zFts5Db);
218304 }
218305 }
218306
218307 *ppVTab = (sqlite3_vtab*)pRet;
218308 return rc;
218309}
218310
218311
218312/*
218313** The xConnect() and xCreate() methods for the virtual table. All the
218314** work is done in function fts5VocabInitVtab().
218315*/
218316static int fts5VocabConnectMethod(
218317 sqlite3 *db, /* Database connection */
218318 void *pAux, /* Pointer to tokenizer hash table */
218319 int argc, /* Number of elements in argv array */
218320 const char * const *argv, /* xCreate/xConnect argument array */
218321 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
218322 char **pzErr /* OUT: sqlite3_malloc'd error message */
218323){
218324 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
218325}
218326static int fts5VocabCreateMethod(
218327 sqlite3 *db, /* Database connection */
218328 void *pAux, /* Pointer to tokenizer hash table */
218329 int argc, /* Number of elements in argv array */
218330 const char * const *argv, /* xCreate/xConnect argument array */
218331 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
218332 char **pzErr /* OUT: sqlite3_malloc'd error message */
218333){
218334 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
218335}
218336
218337/*
218338** Implementation of the xBestIndex method.
218339**
218340** Only constraints of the form:
218341**
218342** term <= ?
218343** term == ?
218344** term >= ?
218345**
218346** are interpreted. Less-than and less-than-or-equal are treated
218347** identically, as are greater-than and greater-than-or-equal.
218348*/
218349static int fts5VocabBestIndexMethod(
218350 sqlite3_vtab *pUnused,
218351 sqlite3_index_info *pInfo
218352){
218353 int i;
218354 int iTermEq = -1;
218355 int iTermGe = -1;
218356 int iTermLe = -1;
218357 int idxNum = 0;
218358 int nArg = 0;
218359
218360 UNUSED_PARAM(pUnused);
218361
218362 for(i=0; i<pInfo->nConstraint; i++){
218363 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
218364 if( p->usable==0 ) continue;
218365 if( p->iColumn==0 ){ /* term column */
218366 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ) iTermEq = i;
218367 if( p->op==SQLITE_INDEX_CONSTRAINT_LE ) iTermLe = i;
218368 if( p->op==SQLITE_INDEX_CONSTRAINT_LT ) iTermLe = i;
218369 if( p->op==SQLITE_INDEX_CONSTRAINT_GE ) iTermGe = i;
218370 if( p->op==SQLITE_INDEX_CONSTRAINT_GT ) iTermGe = i;
218371 }
218372 }
218373
218374 if( iTermEq>=0 ){
218375 idxNum |= FTS5_VOCAB_TERM_EQ;
218376 pInfo->aConstraintUsage[iTermEq].argvIndex = ++nArg;
218377 pInfo->estimatedCost = 100;
218378 }else{
218379 pInfo->estimatedCost = 1000000;
218380 if( iTermGe>=0 ){
218381 idxNum |= FTS5_VOCAB_TERM_GE;
218382 pInfo->aConstraintUsage[iTermGe].argvIndex = ++nArg;
218383 pInfo->estimatedCost = pInfo->estimatedCost / 2;
218384 }
218385 if( iTermLe>=0 ){
218386 idxNum |= FTS5_VOCAB_TERM_LE;
218387 pInfo->aConstraintUsage[iTermLe].argvIndex = ++nArg;
218388 pInfo->estimatedCost = pInfo->estimatedCost / 2;
218389 }
218390 }
218391
218392 /* This virtual table always delivers results in ascending order of
218393 ** the "term" column (column 0). So if the user has requested this
218394 ** specifically - "ORDER BY term" or "ORDER BY term ASC" - set the
218395 ** sqlite3_index_info.orderByConsumed flag to tell the core the results
218396 ** are already in sorted order. */
218397 if( pInfo->nOrderBy==1
218398 && pInfo->aOrderBy[0].iColumn==0
218399 && pInfo->aOrderBy[0].desc==0
218400 ){
218401 pInfo->orderByConsumed = 1;
218402 }
218403
218404 pInfo->idxNum = idxNum;
218405 return SQLITE_OK;
218406}
218407
218408/*
218409** Implementation of xOpen method.
218410*/
218411static int fts5VocabOpenMethod(
218412 sqlite3_vtab *pVTab,
218413 sqlite3_vtab_cursor **ppCsr
218414){
218415 Fts5VocabTable *pTab = (Fts5VocabTable*)pVTab;
218416 Fts5Index *pIndex = 0;
218417 Fts5Config *pConfig = 0;
218418 Fts5VocabCursor *pCsr = 0;
218419 int rc = SQLITE_OK;
218420 sqlite3_stmt *pStmt = 0;
218421 char *zSql = 0;
218422
218423 zSql = sqlite3Fts5Mprintf(&rc,
218424 "SELECT t.%Q FROM %Q.%Q AS t WHERE t.%Q MATCH '*id'",
218425 pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl
218426 );
218427 if( zSql ){
218428 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
218429 }
218430 sqlite3_free(zSql);
218431 assert( rc==SQLITE_OK || pStmt==0 );
218432 if( rc==SQLITE_ERROR ) rc = SQLITE_OK;
218433
218434 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
218435 i64 iId = sqlite3_column_int64(pStmt, 0);
218436 pIndex = sqlite3Fts5IndexFromCsrid(pTab->pGlobal, iId, &pConfig);
218437 }
218438
218439 if( rc==SQLITE_OK && pIndex==0 ){
218440 rc = sqlite3_finalize(pStmt);
218441 pStmt = 0;
218442 if( rc==SQLITE_OK ){
218443 pVTab->zErrMsg = sqlite3_mprintf(
218444 "no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
218445 );
218446 rc = SQLITE_ERROR;
218447 }
218448 }
218449
218450 if( rc==SQLITE_OK ){
218451 int nByte = pConfig->nCol * sizeof(i64) * 2 + sizeof(Fts5VocabCursor);
218452 pCsr = (Fts5VocabCursor*)sqlite3Fts5MallocZero(&rc, nByte);
218453 }
218454
218455 if( pCsr ){
218456 pCsr->pIndex = pIndex;
218457 pCsr->pStmt = pStmt;
218458 pCsr->pConfig = pConfig;
218459 pCsr->aCnt = (i64*)&pCsr[1];
218460 pCsr->aDoc = &pCsr->aCnt[pConfig->nCol];
218461 }else{
218462 sqlite3_finalize(pStmt);
218463 }
218464
218465 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
218466 return rc;
218467}
218468
218469static void fts5VocabResetCursor(Fts5VocabCursor *pCsr){
218470 pCsr->rowid = 0;
218471 sqlite3Fts5IterClose(pCsr->pIter);
218472 pCsr->pIter = 0;
218473 sqlite3_free(pCsr->zLeTerm);
218474 pCsr->nLeTerm = -1;
218475 pCsr->zLeTerm = 0;
218476}
218477
218478/*
218479** Close the cursor. For additional information see the documentation
218480** on the xClose method of the virtual table interface.
218481*/
218482static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){
218483 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
218484 fts5VocabResetCursor(pCsr);
218485 sqlite3Fts5BufferFree(&pCsr->term);
218486 sqlite3_finalize(pCsr->pStmt);
218487 sqlite3_free(pCsr);
218488 return SQLITE_OK;
218489}
218490
218491static int fts5VocabInstanceNewTerm(Fts5VocabCursor *pCsr){
218492 int rc = SQLITE_OK;
218493
218494 if( sqlite3Fts5IterEof(pCsr->pIter) ){
218495 pCsr->bEof = 1;
218496 }else{
218497 const char *zTerm;
218498 int nTerm;
218499 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
218500 if( pCsr->nLeTerm>=0 ){
218501 int nCmp = MIN(nTerm, pCsr->nLeTerm);
218502 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
218503 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
218504 pCsr->bEof = 1;
218505 }
218506 }
218507
218508 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
218509 }
218510 return rc;
218511}
218512
218513static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){
218514 int eDetail = pCsr->pConfig->eDetail;
218515 int rc = SQLITE_OK;
218516 Fts5IndexIter *pIter = pCsr->pIter;
218517 i64 *pp = &pCsr->iInstPos;
218518 int *po = &pCsr->iInstOff;
218519
218520 while( eDetail==FTS5_DETAIL_NONE
218521 || sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp)
218522 ){
218523 pCsr->iInstPos = 0;
218524 pCsr->iInstOff = 0;
218525
218526 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
218527 if( rc==SQLITE_OK ){
218528 rc = fts5VocabInstanceNewTerm(pCsr);
218529 if( eDetail==FTS5_DETAIL_NONE ) break;
218530 }
218531 if( rc ){
218532 pCsr->bEof = 1;
218533 break;
218534 }
218535 }
218536
218537 return rc;
218538}
218539
218540/*
218541** Advance the cursor to the next row in the table.
218542*/
218543static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){
218544 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
218545 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
218546 int rc = SQLITE_OK;
218547 int nCol = pCsr->pConfig->nCol;
218548
218549 pCsr->rowid++;
218550
218551 if( pTab->eType==FTS5_VOCAB_INSTANCE ){
218552 return fts5VocabInstanceNext(pCsr);
218553 }
218554
218555 if( pTab->eType==FTS5_VOCAB_COL ){
218556 for(pCsr->iCol++; pCsr->iCol<nCol; pCsr->iCol++){
218557 if( pCsr->aDoc[pCsr->iCol] ) break;
218558 }
218559 }
218560
218561 if( pTab->eType!=FTS5_VOCAB_COL || pCsr->iCol>=nCol ){
218562 if( sqlite3Fts5IterEof(pCsr->pIter) ){
218563 pCsr->bEof = 1;
218564 }else{
218565 const char *zTerm;
218566 int nTerm;
218567
218568 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
218569 if( pCsr->nLeTerm>=0 ){
218570 int nCmp = MIN(nTerm, pCsr->nLeTerm);
218571 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
218572 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
218573 pCsr->bEof = 1;
218574 return SQLITE_OK;
218575 }
218576 }
218577
218578 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
218579 memset(pCsr->aCnt, 0, nCol * sizeof(i64));
218580 memset(pCsr->aDoc, 0, nCol * sizeof(i64));
218581 pCsr->iCol = 0;
218582
218583 assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW );
218584 while( rc==SQLITE_OK ){
218585 int eDetail = pCsr->pConfig->eDetail;
218586 const u8 *pPos; int nPos; /* Position list */
218587 i64 iPos = 0; /* 64-bit position read from poslist */
218588 int iOff = 0; /* Current offset within position list */
218589
218590 pPos = pCsr->pIter->pData;
218591 nPos = pCsr->pIter->nData;
218592
218593 switch( pTab->eType ){
218594 case FTS5_VOCAB_ROW:
218595 if( eDetail==FTS5_DETAIL_FULL ){
218596 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
218597 pCsr->aCnt[0]++;
218598 }
218599 }
218600 pCsr->aDoc[0]++;
218601 break;
218602
218603 case FTS5_VOCAB_COL:
218604 if( eDetail==FTS5_DETAIL_FULL ){
218605 int iCol = -1;
218606 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
218607 int ii = FTS5_POS2COLUMN(iPos);
218608 pCsr->aCnt[ii]++;
218609 if( iCol!=ii ){
218610 if( ii>=nCol ){
218611 rc = FTS5_CORRUPT;
218612 break;
218613 }
218614 pCsr->aDoc[ii]++;
218615 iCol = ii;
218616 }
218617 }
218618 }else if( eDetail==FTS5_DETAIL_COLUMNS ){
218619 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff,&iPos) ){
218620 assert_nc( iPos>=0 && iPos<nCol );
218621 if( iPos>=nCol ){
218622 rc = FTS5_CORRUPT;
218623 break;
218624 }
218625 pCsr->aDoc[iPos]++;
218626 }
218627 }else{
218628 assert( eDetail==FTS5_DETAIL_NONE );
218629 pCsr->aDoc[0]++;
218630 }
218631 break;
218632
218633 default:
218634 assert( pTab->eType==FTS5_VOCAB_INSTANCE );
218635 break;
218636 }
218637
218638 if( rc==SQLITE_OK ){
218639 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
218640 }
218641 if( pTab->eType==FTS5_VOCAB_INSTANCE ) break;
218642
218643 if( rc==SQLITE_OK ){
218644 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
218645 if( nTerm!=pCsr->term.n || memcmp(zTerm, pCsr->term.p, nTerm) ){
218646 break;
218647 }
218648 if( sqlite3Fts5IterEof(pCsr->pIter) ) break;
218649 }
218650 }
218651 }
218652 }
218653
218654 if( rc==SQLITE_OK && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL ){
218655 while( pCsr->aDoc[pCsr->iCol]==0 ) pCsr->iCol++;
218656 assert( pCsr->iCol<pCsr->pConfig->nCol );
218657 }
218658 return rc;
218659}
218660
218661/*
218662** This is the xFilter implementation for the virtual table.
218663*/
218664static int fts5VocabFilterMethod(
218665 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
218666 int idxNum, /* Strategy index */
218667 const char *zUnused, /* Unused */
218668 int nUnused, /* Number of elements in apVal */
218669 sqlite3_value **apVal /* Arguments for the indexing scheme */
218670){
218671 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
218672 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
218673 int eType = pTab->eType;
218674 int rc = SQLITE_OK;
218675
218676 int iVal = 0;
218677 int f = FTS5INDEX_QUERY_SCAN;
218678 const char *zTerm = 0;
218679 int nTerm = 0;
218680
218681 sqlite3_value *pEq = 0;
218682 sqlite3_value *pGe = 0;
218683 sqlite3_value *pLe = 0;
218684
218685 UNUSED_PARAM2(zUnused, nUnused);
218686
218687 fts5VocabResetCursor(pCsr);
218688 if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++];
218689 if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++];
218690 if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++];
218691
218692 if( pEq ){
218693 zTerm = (const char *)sqlite3_value_text(pEq);
218694 nTerm = sqlite3_value_bytes(pEq);
218695 f = 0;
218696 }else{
218697 if( pGe ){
218698 zTerm = (const char *)sqlite3_value_text(pGe);
218699 nTerm = sqlite3_value_bytes(pGe);
218700 }
218701 if( pLe ){
218702 const char *zCopy = (const char *)sqlite3_value_text(pLe);
218703 pCsr->nLeTerm = sqlite3_value_bytes(pLe);
218704 pCsr->zLeTerm = sqlite3_malloc(pCsr->nLeTerm+1);
218705 if( pCsr->zLeTerm==0 ){
218706 rc = SQLITE_NOMEM;
218707 }else{
218708 memcpy(pCsr->zLeTerm, zCopy, pCsr->nLeTerm+1);
218709 }
218710 }
218711 }
218712
218713 if( rc==SQLITE_OK ){
218714 rc = sqlite3Fts5IndexQuery(pCsr->pIndex, zTerm, nTerm, f, 0, &pCsr->pIter);
218715 }
218716 if( rc==SQLITE_OK && eType==FTS5_VOCAB_INSTANCE ){
218717 rc = fts5VocabInstanceNewTerm(pCsr);
218718 }
218719 if( rc==SQLITE_OK
218720 && !pCsr->bEof
218721 && (eType!=FTS5_VOCAB_INSTANCE || pCsr->pConfig->eDetail!=FTS5_DETAIL_NONE)
218722 ){
218723 rc = fts5VocabNextMethod(pCursor);
218724 }
218725
218726 return rc;
218727}
218728
218729/*
218730** This is the xEof method of the virtual table. SQLite calls this
218731** routine to find out if it has reached the end of a result set.
218732*/
218733static int fts5VocabEofMethod(sqlite3_vtab_cursor *pCursor){
218734 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
218735 return pCsr->bEof;
218736}
218737
218738static int fts5VocabColumnMethod(
218739 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
218740 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
218741 int iCol /* Index of column to read value from */
218742){
218743 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
218744 int eDetail = pCsr->pConfig->eDetail;
218745 int eType = ((Fts5VocabTable*)(pCursor->pVtab))->eType;
218746 i64 iVal = 0;
218747
218748 if( iCol==0 ){
218749 sqlite3_result_text(
218750 pCtx, (const char*)pCsr->term.p, pCsr->term.n, SQLITE_TRANSIENT
218751 );
218752 }else if( eType==FTS5_VOCAB_COL ){
218753 assert( iCol==1 || iCol==2 || iCol==3 );
218754 if( iCol==1 ){
218755 if( eDetail!=FTS5_DETAIL_NONE ){
218756 const char *z = pCsr->pConfig->azCol[pCsr->iCol];
218757 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC);
218758 }
218759 }else if( iCol==2 ){
218760 iVal = pCsr->aDoc[pCsr->iCol];
218761 }else{
218762 iVal = pCsr->aCnt[pCsr->iCol];
218763 }
218764 }else if( eType==FTS5_VOCAB_ROW ){
218765 assert( iCol==1 || iCol==2 );
218766 if( iCol==1 ){
218767 iVal = pCsr->aDoc[0];
218768 }else{
218769 iVal = pCsr->aCnt[0];
218770 }
218771 }else{
218772 assert( eType==FTS5_VOCAB_INSTANCE );
218773 switch( iCol ){
218774 case 1:
218775 sqlite3_result_int64(pCtx, pCsr->pIter->iRowid);
218776 break;
218777 case 2: {
218778 int ii = -1;
218779 if( eDetail==FTS5_DETAIL_FULL ){
218780 ii = FTS5_POS2COLUMN(pCsr->iInstPos);
218781 }else if( eDetail==FTS5_DETAIL_COLUMNS ){
218782 ii = (int)pCsr->iInstPos;
218783 }
218784 if( ii>=0 && ii<pCsr->pConfig->nCol ){
218785 const char *z = pCsr->pConfig->azCol[ii];
218786 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC);
218787 }
218788 break;
218789 }
218790 default: {
218791 assert( iCol==3 );
218792 if( eDetail==FTS5_DETAIL_FULL ){
218793 int ii = FTS5_POS2OFFSET(pCsr->iInstPos);
218794 sqlite3_result_int(pCtx, ii);
218795 }
218796 break;
218797 }
218798 }
218799 }
218800
218801 if( iVal>0 ) sqlite3_result_int64(pCtx, iVal);
218802 return SQLITE_OK;
218803}
218804
218805/*
218806** This is the xRowid method. The SQLite core calls this routine to
218807** retrieve the rowid for the current row of the result set. The
218808** rowid should be written to *pRowid.
218809*/
218810static int fts5VocabRowidMethod(
218811 sqlite3_vtab_cursor *pCursor,
218812 sqlite_int64 *pRowid
218813){
218814 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
218815 *pRowid = pCsr->rowid;
218816 return SQLITE_OK;
218817}
218818
218819static int sqlite3Fts5VocabInit(Fts5Global *pGlobal, sqlite3 *db){
218820 static const sqlite3_module fts5Vocab = {
218821 /* iVersion */ 2,
218822 /* xCreate */ fts5VocabCreateMethod,
218823 /* xConnect */ fts5VocabConnectMethod,
218824 /* xBestIndex */ fts5VocabBestIndexMethod,
218825 /* xDisconnect */ fts5VocabDisconnectMethod,
218826 /* xDestroy */ fts5VocabDestroyMethod,
218827 /* xOpen */ fts5VocabOpenMethod,
218828 /* xClose */ fts5VocabCloseMethod,
218829 /* xFilter */ fts5VocabFilterMethod,
218830 /* xNext */ fts5VocabNextMethod,
218831 /* xEof */ fts5VocabEofMethod,
218832 /* xColumn */ fts5VocabColumnMethod,
218833 /* xRowid */ fts5VocabRowidMethod,
218834 /* xUpdate */ 0,
218835 /* xBegin */ 0,
218836 /* xSync */ 0,
218837 /* xCommit */ 0,
218838 /* xRollback */ 0,
218839 /* xFindFunction */ 0,
218840 /* xRename */ 0,
218841 /* xSavepoint */ 0,
218842 /* xRelease */ 0,
218843 /* xRollbackTo */ 0,
218844 };
218845 void *p = (void*)pGlobal;
218846
218847 return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0);
218848}
218849
218850
218851
218852
218853
218854#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
218855
218856/************** End of fts5.c ************************************************/
218857/************** Begin file stmt.c ********************************************/
218858/*
218859** 2017-05-31
218860**
218861** The author disclaims copyright to this source code. In place of
218862** a legal notice, here is a blessing:
218863**
218864** May you do good and not evil.
218865** May you find forgiveness for yourself and forgive others.
218866** May you share freely, never taking more than you give.
218867**
218868*************************************************************************
218869**
218870** This file demonstrates an eponymous virtual table that returns information
218871** about all prepared statements for the database connection.
218872**
218873** Usage example:
218874**
218875** .load ./stmt
218876** .mode line
218877** .header on
218878** SELECT * FROM stmt;
218879*/
218880#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB)
218881#if !defined(SQLITEINT_H)
218882/* #include "sqlite3ext.h" */
218883#endif
218884SQLITE_EXTENSION_INIT1
218885/* #include <assert.h> */
218886/* #include <string.h> */
218887
218888#ifndef SQLITE_OMIT_VIRTUALTABLE
218889
218890/* stmt_vtab is a subclass of sqlite3_vtab which will
218891** serve as the underlying representation of a stmt virtual table
218892*/
218893typedef struct stmt_vtab stmt_vtab;
218894struct stmt_vtab {
218895 sqlite3_vtab base; /* Base class - must be first */
218896 sqlite3 *db; /* Database connection for this stmt vtab */
218897};
218898
218899/* stmt_cursor is a subclass of sqlite3_vtab_cursor which will
218900** serve as the underlying representation of a cursor that scans
218901** over rows of the result
218902*/
218903typedef struct stmt_cursor stmt_cursor;
218904struct stmt_cursor {
218905 sqlite3_vtab_cursor base; /* Base class - must be first */
218906 sqlite3 *db; /* Database connection for this cursor */
218907 sqlite3_stmt *pStmt; /* Statement cursor is currently pointing at */
218908 sqlite3_int64 iRowid; /* The rowid */
218909};
218910
218911/*
218912** The stmtConnect() method is invoked to create a new
218913** stmt_vtab that describes the stmt virtual table.
218914**
218915** Think of this routine as the constructor for stmt_vtab objects.
218916**
218917** All this routine needs to do is:
218918**
218919** (1) Allocate the stmt_vtab object and initialize all fields.
218920**
218921** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
218922** result set of queries against stmt will look like.
218923*/
218924static int stmtConnect(
218925 sqlite3 *db,
218926 void *pAux,
218927 int argc, const char *const*argv,
218928 sqlite3_vtab **ppVtab,
218929 char **pzErr
218930){
218931 stmt_vtab *pNew;
218932 int rc;
218933
218934/* Column numbers */
218935#define STMT_COLUMN_SQL 0 /* SQL for the statement */
218936#define STMT_COLUMN_NCOL 1 /* Number of result columns */
218937#define STMT_COLUMN_RO 2 /* True if read-only */
218938#define STMT_COLUMN_BUSY 3 /* True if currently busy */
218939#define STMT_COLUMN_NSCAN 4 /* SQLITE_STMTSTATUS_FULLSCAN_STEP */
218940#define STMT_COLUMN_NSORT 5 /* SQLITE_STMTSTATUS_SORT */
218941#define STMT_COLUMN_NAIDX 6 /* SQLITE_STMTSTATUS_AUTOINDEX */
218942#define STMT_COLUMN_NSTEP 7 /* SQLITE_STMTSTATUS_VM_STEP */
218943#define STMT_COLUMN_REPREP 8 /* SQLITE_STMTSTATUS_REPREPARE */
218944#define STMT_COLUMN_RUN 9 /* SQLITE_STMTSTATUS_RUN */
218945#define STMT_COLUMN_MEM 10 /* SQLITE_STMTSTATUS_MEMUSED */
218946
218947
218948 rc = sqlite3_declare_vtab(db,
218949 "CREATE TABLE x(sql,ncol,ro,busy,nscan,nsort,naidx,nstep,"
218950 "reprep,run,mem)");
218951 if( rc==SQLITE_OK ){
218952 pNew = sqlite3_malloc( sizeof(*pNew) );
218953 *ppVtab = (sqlite3_vtab*)pNew;
218954 if( pNew==0 ) return SQLITE_NOMEM;
218955 memset(pNew, 0, sizeof(*pNew));
218956 pNew->db = db;
218957 }
218958 return rc;
218959}
218960
218961/*
218962** This method is the destructor for stmt_cursor objects.
218963*/
218964static int stmtDisconnect(sqlite3_vtab *pVtab){
218965 sqlite3_free(pVtab);
218966 return SQLITE_OK;
218967}
218968
218969/*
218970** Constructor for a new stmt_cursor object.
218971*/
218972static int stmtOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
218973 stmt_cursor *pCur;
218974 pCur = sqlite3_malloc( sizeof(*pCur) );
218975 if( pCur==0 ) return SQLITE_NOMEM;
218976 memset(pCur, 0, sizeof(*pCur));
218977 pCur->db = ((stmt_vtab*)p)->db;
218978 *ppCursor = &pCur->base;
218979 return SQLITE_OK;
218980}
218981
218982/*
218983** Destructor for a stmt_cursor.
218984*/
218985static int stmtClose(sqlite3_vtab_cursor *cur){
218986 sqlite3_free(cur);
218987 return SQLITE_OK;
218988}
218989
218990
218991/*
218992** Advance a stmt_cursor to its next row of output.
218993*/
218994static int stmtNext(sqlite3_vtab_cursor *cur){
218995 stmt_cursor *pCur = (stmt_cursor*)cur;
218996 pCur->iRowid++;
218997 pCur->pStmt = sqlite3_next_stmt(pCur->db, pCur->pStmt);
218998 return SQLITE_OK;
218999}
219000
219001/*
219002** Return values of columns for the row at which the stmt_cursor
219003** is currently pointing.
219004*/
219005static int stmtColumn(
219006 sqlite3_vtab_cursor *cur, /* The cursor */
219007 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
219008 int i /* Which column to return */
219009){
219010 stmt_cursor *pCur = (stmt_cursor*)cur;
219011 switch( i ){
219012 case STMT_COLUMN_SQL: {
219013 sqlite3_result_text(ctx, sqlite3_sql(pCur->pStmt), -1, SQLITE_TRANSIENT);
219014 break;
219015 }
219016 case STMT_COLUMN_NCOL: {
219017 sqlite3_result_int(ctx, sqlite3_column_count(pCur->pStmt));
219018 break;
219019 }
219020 case STMT_COLUMN_RO: {
219021 sqlite3_result_int(ctx, sqlite3_stmt_readonly(pCur->pStmt));
219022 break;
219023 }
219024 case STMT_COLUMN_BUSY: {
219025 sqlite3_result_int(ctx, sqlite3_stmt_busy(pCur->pStmt));
219026 break;
219027 }
219028 case STMT_COLUMN_MEM: {
219029 i = SQLITE_STMTSTATUS_MEMUSED +
219030 STMT_COLUMN_NSCAN - SQLITE_STMTSTATUS_FULLSCAN_STEP;
219031 /* Fall thru */
219032 }
219033 case STMT_COLUMN_NSCAN:
219034 case STMT_COLUMN_NSORT:
219035 case STMT_COLUMN_NAIDX:
219036 case STMT_COLUMN_NSTEP:
219037 case STMT_COLUMN_REPREP:
219038 case STMT_COLUMN_RUN: {
219039 sqlite3_result_int(ctx, sqlite3_stmt_status(pCur->pStmt,
219040 i-STMT_COLUMN_NSCAN+SQLITE_STMTSTATUS_FULLSCAN_STEP, 0));
219041 break;
219042 }
219043 }
219044 return SQLITE_OK;
219045}
219046
219047/*
219048** Return the rowid for the current row. In this implementation, the
219049** rowid is the same as the output value.
219050*/
219051static int stmtRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
219052 stmt_cursor *pCur = (stmt_cursor*)cur;
219053 *pRowid = pCur->iRowid;
219054 return SQLITE_OK;
219055}
219056
219057/*
219058** Return TRUE if the cursor has been moved off of the last
219059** row of output.
219060*/
219061static int stmtEof(sqlite3_vtab_cursor *cur){
219062 stmt_cursor *pCur = (stmt_cursor*)cur;
219063 return pCur->pStmt==0;
219064}
219065
219066/*
219067** This method is called to "rewind" the stmt_cursor object back
219068** to the first row of output. This method is always called at least
219069** once prior to any call to stmtColumn() or stmtRowid() or
219070** stmtEof().
219071*/
219072static int stmtFilter(
219073 sqlite3_vtab_cursor *pVtabCursor,
219074 int idxNum, const char *idxStr,
219075 int argc, sqlite3_value **argv
219076){
219077 stmt_cursor *pCur = (stmt_cursor *)pVtabCursor;
219078 pCur->pStmt = 0;
219079 pCur->iRowid = 0;
219080 return stmtNext(pVtabCursor);
219081}
219082
219083/*
219084** SQLite will invoke this method one or more times while planning a query
219085** that uses the stmt virtual table. This routine needs to create
219086** a query plan for each invocation and compute an estimated cost for that
219087** plan.
219088*/
219089static int stmtBestIndex(
219090 sqlite3_vtab *tab,
219091 sqlite3_index_info *pIdxInfo
219092){
219093 pIdxInfo->estimatedCost = (double)500;
219094 pIdxInfo->estimatedRows = 500;
219095 return SQLITE_OK;
219096}
219097
219098/*
219099** This following structure defines all the methods for the
219100** stmt virtual table.
219101*/
219102static sqlite3_module stmtModule = {
219103 0, /* iVersion */
219104 0, /* xCreate */
219105 stmtConnect, /* xConnect */
219106 stmtBestIndex, /* xBestIndex */
219107 stmtDisconnect, /* xDisconnect */
219108 0, /* xDestroy */
219109 stmtOpen, /* xOpen - open a cursor */
219110 stmtClose, /* xClose - close a cursor */
219111 stmtFilter, /* xFilter - configure scan constraints */
219112 stmtNext, /* xNext - advance a cursor */
219113 stmtEof, /* xEof - check for end of scan */
219114 stmtColumn, /* xColumn - read data */
219115 stmtRowid, /* xRowid - read data */
219116 0, /* xUpdate */
219117 0, /* xBegin */
219118 0, /* xSync */
219119 0, /* xCommit */
219120 0, /* xRollback */
219121 0, /* xFindMethod */
219122 0, /* xRename */
219123 0, /* xSavepoint */
219124 0, /* xRelease */
219125 0, /* xRollbackTo */
219126};
219127
219128#endif /* SQLITE_OMIT_VIRTUALTABLE */
219129
219130SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3 *db){
219131 int rc = SQLITE_OK;
219132#ifndef SQLITE_OMIT_VIRTUALTABLE
219133 rc = sqlite3_create_module(db, "sqlite_stmt", &stmtModule, 0);
219134#endif
219135 return rc;
219136}
219137
219138#ifndef SQLITE_CORE
219139#ifdef _WIN32
219140__declspec(dllexport)
219141#endif
219142SQLITE_API int sqlite3_stmt_init(
219143 sqlite3 *db,
219144 char **pzErrMsg,
219145 const sqlite3_api_routines *pApi
219146){
219147 int rc = SQLITE_OK;
219148 SQLITE_EXTENSION_INIT2(pApi);
219149#ifndef SQLITE_OMIT_VIRTUALTABLE
219150 rc = sqlite3StmtVtabInit(db);
219151#endif
219152 return rc;
219153}
219154#endif /* SQLITE_CORE */
219155#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
219156
219157/************** End of stmt.c ************************************************/
219158#if __LINE__!=219157
219159#undef SQLITE_SOURCE_ID
219160#define SQLITE_SOURCE_ID "2018-09-25 19:08:10 fb90e7189ae6d62e77ba3a308ca5d683f90bbe633cf681865365b8e92792alt2"
219161#endif
219162/* Return the source-id for this library */
219163SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
219164/************************** End of sqlite3.c ******************************/
219165